diff --git a/lib/checkexceptionsafety.cpp b/lib/checkexceptionsafety.cpp index 0e0b584ce..80ce3ab72 100644 --- a/lib/checkexceptionsafety.cpp +++ b/lib/checkexceptionsafety.cpp @@ -84,7 +84,7 @@ static bool autodealloc(const Token * const C, const Token * const tokens) void CheckExceptionSafety::unsafeNew() { - if (_settings->enableId != "*" && _settings->enableId.find(",exceptNew,") == std::string::npos) + if (!_settings->isEnabled("exceptNew")) return; // Inspect initializer lists.. @@ -197,7 +197,7 @@ void CheckExceptionSafety::unsafeNew() void CheckExceptionSafety::realloc() { - if (_settings->enableId != "*" && _settings->enableId.find(",exceptRealloc,") == std::string::npos) + if (!_settings->isEnabled("exceptRealloc")) return; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index a5f15bd4f..1dffa395d 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #ifdef __GNUC__ @@ -99,7 +98,7 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) // Show all messages else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0) - _settings._showAll = true; + _settings.addEnabled("possibleError"); // Only print something when there are errors else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0) @@ -107,7 +106,7 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) // Checking coding style else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--style") == 0) - _settings._checkCodingStyle = true; + _settings.addEnabled("style"); // Filter errors else if (strcmp(argv[i], "--suppressions") == 0) @@ -137,10 +136,7 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) // Check if there are unused functions else if (strcmp(argv[i], "--unused-functions") == 0) - { - if (_settings.enableId != "*") - _settings.enableId += ",unusedFunctions,"; - } + _settings.addEnabled("unusedFunctions"); // Append userdefined code to checked source code else if (strncmp(argv[i], "--append=", 9) == 0) @@ -161,29 +157,18 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) else if (strcmp(argv[i], "--enable") == 0) - { - _settings.enableId = "*"; - _settings._showAll = true; - _settings._checkCodingStyle = true; - } + _settings.addEnabled(""); else if (strncmp(argv[i], "--enable=", 9) == 0) { - if (_settings.enableId != "*") - { - std::string s(9 + argv[i]); - if (s[0] == '\"') - s[0] = ','; - std::string::size_type pos = s.find("\""); - if (pos != std::string::npos) - s.erase(pos, 1); - _settings.enableId += "," + s + ","; + std::string s(9 + argv[i]); + if (s[0] == '\"') + s.erase(0, 1); + std::string::size_type pos = s.find("\""); + if (pos != std::string::npos) + s.erase(pos, 1); - if (_settings.enableId.find(",style,") != std::string::npos) - _settings._checkCodingStyle = true; - if (_settings.enableId.find(",possibleError,") != std::string::npos) - _settings._showAll = true; - } + _settings.addEnabled(s); } // --error-exitcode=1 @@ -335,9 +320,9 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) pathnames.push_back(argv[i]); } - if (_settings.enableId.find(",unusedFunctions,") != std::string::npos && _settings._jobs > 1) + if (_settings.isEnabled("unusedFunctions") && _settings._jobs > 1) { - throw std::runtime_error("cppcheck: error: the unusedFunctions check can't be used with -j option."); + reportOut("unusedFunctions check can't be used with -j option, so it was disabled."); } if (pathnames.size() > 0) @@ -495,7 +480,7 @@ unsigned int CppCheck::check() // This generates false positives - especially for libraries _settings._verbose = false; - if (_settings.enableId != "*" && _settings.enableId.find(",unusedFunctions,") != std::string::npos) + if (_settings.isEnabled("unusedFunctions") && _settings._jobs == 1) { _errout.str(""); if (_settings._errorsOnly == false) @@ -555,7 +540,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) } - if (_settings.enableId != "*" || _settings.enableId.find(",unusedFunctions,") != std::string::npos) + if (_settings.isEnabled("unusedFunctions") && _settings._jobs == 1) _checkUnusedFunctions.parseTokens(_tokenizer); // call all "runSimplifiedChecks" in all registered Check classes diff --git a/lib/settings.cpp b/lib/settings.cpp index 761df5df9..4bc29c809 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -113,6 +113,44 @@ bool Settings::isSuppressed(const std::string &errorId, const std::string &file, return true; } +void Settings::addEnabled(const std::string &str) +{ + if (str.length() == 0) + { + _enabled["*"] = true; + } + else + { + std::string::size_type prevPos = 0; + std::string::size_type pos = 0; + while ((pos = str.find_first_of(",", pos)) != std::string::npos) + { + if (prevPos != pos) + _enabled[str.substr(prevPos, pos-prevPos)] = true; + + ++pos; + prevPos = pos; + } + + if (prevPos < str.length()) + _enabled[str.substr(prevPos, pos-prevPos)] = true; + } + + if (isEnabled("style")) + _checkCodingStyle = true; + if (isEnabled("possibleError")) + _showAll = true; +} + +bool Settings::isEnabled(const std::string &str) const +{ + if (_enabled.find("*") != _enabled.end() || + _enabled.find(str) != _enabled.end()) + return true; + else + return false; +} + void Settings::addAutoAllocClass(const std::string &name) { _autoDealloc.insert(name); diff --git a/lib/settings.h b/lib/settings.h index e287ba04b..27ef2e407 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -46,6 +46,8 @@ private: /** List of error which the user doesn't want to see. */ std::map > > _suppressions; + /** enable extra checks by id */ + std::map _enabled; public: Settings(); virtual ~Settings(); @@ -119,7 +121,22 @@ public: std::string append() const; /** enable extra checks by id */ - std::string enableId; + //std::string enableId; + + /** + * Returns true if given id is in the list of + * enabled extra checks. See addEnabled() + * @param str id for the extra check, e.g. "style" + * @return true if the check is enabled. + */ + bool isEnabled(const std::string &str) const; + + /** + * Enable extra checks by id. See isEnabled() + * @param str single id or list of id values to be enabled + * or empty string to enable all. e.g. "style,possibleError" + */ + void addEnabled(const std::string &str); }; /// @} diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index fdc8c3c3a..6e41fe764 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -53,8 +53,7 @@ private: // Check char variable usage.. Settings settings; - settings._checkCodingStyle = true; - settings.enableId = "*"; + settings.addEnabled(""); CheckExceptionSafety checkExceptionSafety(&tokenizer, &settings, this); checkExceptionSafety.runSimplifiedChecks(&tokenizer, &settings, this); }