--addon: Use json for addon output

This commit is contained in:
Daniel Marjamäki 2019-06-24 19:28:52 +02:00
parent 66e0f06494
commit c97dc79815
3 changed files with 33 additions and 52 deletions

View File

@ -9,6 +9,7 @@ License: No restrictions, use this as you need.
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import argparse import argparse
from fnmatch import fnmatch from fnmatch import fnmatch
import json
import sys import sys
@ -847,11 +848,19 @@ def simpleMatch(token, pattern):
return True return True
def reportError(location, severity, message, addon, errorId): def reportError(location, severity, message, addon, errorId, extra=''):
if '--cli' in sys.argv: if '--cli' in sys.argv:
errout = sys.stdout msg = { 'file': location.file,
loc = '[%s:%i:%i]' % (location.file, location.linenr, location.col) 'linenr': location.linenr,
'col': location.col,
'severity': severity,
'message': message,
'addon': addon,
'errorId': errorId,
'extra': extra}
sys.stdout.write(json.dumps(msg) + '\n')
else: else:
errout = sys.stderr
loc = '[%s:%i]' % (location.file, location.linenr) loc = '[%s:%i]' % (location.file, location.linenr)
errout.write('%s (%s) %s [%s-%s]\n' % (loc, severity, message, addon, errorId)) if len(extra) > 0:
message += ' (' + extra + ')'
sys.stderr.write('%s (%s) %s [%s-%s]\n' % (loc, severity, message, addon, errorId))

View File

@ -32,6 +32,7 @@
#include "tokenlist.h" #include "tokenlist.h"
#include "version.h" #include "version.h"
#define PICOJSON_USE_INT64
#include <picojson.h> #include <picojson.h>
#include <simplecpp.h> #include <simplecpp.h>
#include <tinyxml2.h> #include <tinyxml2.h>
@ -617,66 +618,36 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
reportOut(failedToGetAddonInfo); reportOut(failedToGetAddonInfo);
continue; continue;
} }
const std::string &results = executeAddon(addonInfo, dumpFile); const std::string results = executeAddon(addonInfo, dumpFile);
for (std::string::size_type pos = 0; pos < results.size();) { std::istringstream istr(results);
const std::string::size_type pos2 = results.find("\n", pos); std::string line;
if (pos2 == std::string::npos)
break;
const std::string::size_type pos1 = pos; while (std::getline(istr, line)) {
pos = pos2 + 1; if (line.compare(0,1,"{") != 0)
if (pos1 + 5 > pos2)
continue;
if (results[pos1] != '[')
continue;
if (results[pos2-1] != ']')
continue; continue;
// Example line: picojson::value res;
// [test.cpp:123]: (style) some problem [abc-someProblem] std::istringstream istr2(line);
const std::string line = results.substr(pos1, pos2-pos1); istr2 >> res;
if (!res.is<picojson::object>())
// Line must start with [filename:line:column]: (
const std::string::size_type loc1 = 1;
const std::string::size_type loc4 = line.find("]");
if (loc4 + 5 >= line.size() || line.compare(loc4, 3, "] (", 0, 3) != 0)
continue;
const std::string::size_type loc3 = line.rfind(':', loc4);
if (loc3 == std::string::npos)
continue;
const std::string::size_type loc2 = line.rfind(':', loc3 - 1);
if (loc2 == std::string::npos)
continue; continue;
// Then there must be a (severity) picojson::object obj = res.get<picojson::object>();
const std::string::size_type sev1 = loc4 + 3;
const std::string::size_type sev2 = line.find(")", sev1);
if (sev2 == std::string::npos)
continue;
// line must end with [addon-x] const std::string filename = obj["file"].get<std::string>();
const std::string::size_type id1 = line.rfind("[" + addonInfo.name + "-"); const int64_t lineNumber = obj["linenr"].get<int64_t>();
if (id1 == std::string::npos || id1 < sev2) const int64_t column = obj["col"].get<int64_t>();
continue;
ErrorLogger::ErrorMessage errmsg; ErrorLogger::ErrorMessage errmsg;
const std::string filename = line.substr(loc1, loc2-loc1);
const int lineNumber = std::atoi(line.c_str() + loc2 + 1);
const int column = std::atoi(line.c_str() + loc3 + 1);
errmsg._callStack.emplace_back(ErrorLogger::ErrorMessage::FileLocation(filename, lineNumber)); errmsg._callStack.emplace_back(ErrorLogger::ErrorMessage::FileLocation(filename, lineNumber));
errmsg._callStack.back().col = column; errmsg._callStack.back().col = column;
errmsg._id = line.substr(id1+1, line.size()-id1-2); errmsg._id = obj["errorId"].get<std::string>();
std::string text = line.substr(sev2 + 2, id1 - sev2 - 2); const std::string text = obj["message"].get<std::string>();
if (text[0] == ' ')
text = text.substr(1);
if (endsWith(text, " ", 1))
text = text.erase(text.size() - 1);
errmsg.setmsg(text); errmsg.setmsg(text);
const std::string sev = line.substr(sev1, sev2-sev1); const std::string severity = obj["severity"].get<std::string>();
errmsg._severity = Severity::fromString(sev); errmsg._severity = Severity::fromString(severity);
if (errmsg._severity == Severity::SeverityType::none) if (errmsg._severity == Severity::SeverityType::none)
continue; continue;
errmsg.file0 = filename; errmsg.file0 = filename;

View File

@ -25,6 +25,7 @@
#include "tokenize.h" #include "tokenize.h"
#include "tokenlist.h" #include "tokenlist.h"
#include "utils.h" #include "utils.h"
#define PICOJSON_USE_INT64
#include <picojson.h> #include <picojson.h>
#include <cstring> #include <cstring>