diff --git a/addons/naming.py b/addons/naming.py index 2dfe1c8fb..8739c1a6b 100755 --- a/addons/naming.py +++ b/addons/naming.py @@ -20,6 +20,7 @@ def validate_regex(expr): exit(1) +CLI = ('--cli' in sys.argv) RE_VARNAME = None RE_PRIVATE_MEMBER_VARIABLE = None RE_FUNCTIONNAME = None @@ -37,11 +38,14 @@ for arg in sys.argv[1:]: FoundError = False -def reportError(token, severity, msg): +def reportError(token, severity, msg, errorId): global FoundError FoundError = True - sys.stderr.write( - '[' + token.file + ':' + str(token.linenr) + '] (' + severity + ') naming.py: ' + msg + '\n') + msg = '[' + token.file + ':' + str(token.linenr) + ']: (' + severity + ') ' + msg + ' [naming-' + errorId + ']' + if CLI: + print(msg) + else: + sys.stderr.write(msg + '\n') for arg in sys.argv[1:]: if not arg[-5:] == '.dump': @@ -57,7 +61,7 @@ for arg in sys.argv[1:]: res = re.match(RE_VARNAME, var.nameToken.str) if not res: reportError(var.typeStartToken, 'style', 'Variable ' + - var.nameToken.str + ' violates naming convention') + var.nameToken.str + ' violates naming convention', 'varname') if RE_PRIVATE_MEMBER_VARIABLE: for var in cfg.variables: if (var.access is None) or var.access != 'Private': @@ -65,16 +69,16 @@ for arg in sys.argv[1:]: res = re.match(RE_PRIVATE_MEMBER_VARIABLE, var.nameToken.str) if not res: reportError(var.typeStartToken, 'style', 'Private member variable ' + - var.nameToken.str + ' violates naming convention') + var.nameToken.str + ' violates naming convention', 'privateMemberVariable') if RE_FUNCTIONNAME: for scope in cfg.scopes: if scope.type == 'Function': res = re.match(RE_FUNCTIONNAME, scope.className) if not res: reportError( - scope.bodyStart, 'style', 'Function ' + scope.className + ' violates naming convention') + scope.bodyStart, 'style', 'Function ' + scope.className + ' violates naming convention', 'functionName') -if FoundError: +if FoundError and not CLI: print('FoundError') sys.exit(1) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index e0030958d..76da892b1 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -57,6 +57,36 @@ static TimerResults S_timerResults; // CWE ids used static const CWE CWE398(398U); // Indicator of Poor Code Quality +namespace { + struct AddonInfo { + std::string script; + std::string args; + + std::string getAddonInfo(const std::string &fileName) { + if (!endsWith(fileName, ".json", 5)) { + script = fileName; + return ""; + } + std::ifstream fin(fileName); + if (!fin.is_open()) + return "Failed to open " + fileName; + picojson::value json; + fin >> json; + if (!json.is()) + return "Loading " + fileName + " failed. Bad json."; + picojson::object obj = json.get(); + if (obj.count("args")) { + if (!obj["args"].is()) + return "Loading " + fileName + " failed. args must be array."; + for (const picojson::value &v : obj["args"].get()) + args += " " + v.get(); + } + script = obj["addon"].get(); + return ""; + } + }; +} + static std::vector split(const std::string &str, const std::string &sep) { std::vector ret; @@ -515,7 +545,13 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string fdump.close(); for (const std::string &addon : mSettings.addons) { - const std::string &results = executeAddon(addon, "", dumpFile); + struct AddonInfo addonInfo; + const std::string errmsg = addonInfo.getAddonInfo(addon); + if (!errmsg.empty()) { + reportOut(errmsg); + continue; + } + const std::string &results = executeAddon(addonInfo.script, addonInfo.args, dumpFile); for (std::string::size_type pos = 0; pos < results.size();) { const std::string::size_type pos2 = results.find("\n", pos); if (pos2 == std::string::npos) @@ -551,7 +587,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string continue; // line must end with [addon-x] - const std::string::size_type id1 = line.rfind("[" + addon + "-"); + const std::string::size_type id1 = line.rfind("[" + addonInfo.script + "-"); if (id1 == std::string::npos || id1 < loc3) continue; @@ -952,31 +988,6 @@ void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &token std::string CppCheck::executeAddon(const std::string &addon, const std::string &args, const std::string &dumpFile) { - if (endsWith(addon, ".json", 5)) { - std::ifstream fin(addon); - if (!fin.is_open()) { - reportOut("Failed to open " + addon); - return ""; - } - picojson::value json; - fin >> json; - if (!json.is()) { - reportOut("Loading " + addon + " failed. Bad json."); - return ""; - } - picojson::object obj = json.get(); - std::string args; - if (obj.count("args")) { - if (!obj["args"].is()) { - reportOut("Loading " + addon + " failed. args must be array."); - return ""; - } - for (const picojson::value &v : obj["args"].get()) - args += " " + v.get(); - } - return executeAddon(obj["addon"].get(), args, dumpFile); - } - const std::string addonFile = "addons/" + addon + ".py"; const std::string cmd = "python " + addonFile + " --cli" + args + " " + dumpFile;