From 6fd74dce47c79570efc672d97384206cdd491d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 28 Dec 2009 11:23:58 +0100 Subject: [PATCH] exitcode suppressions. Partly fixes #1113 --- lib/cppcheck.cpp | 38 +++++++++++++++++++------ lib/cppcheck.h | 1 + lib/preprocessor.cpp | 2 +- lib/settings.cpp | 6 ++-- lib/settings.h | 68 ++++++++++++++++++++++++++------------------ 5 files changed, 74 insertions(+), 41 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 005cb7ed4..f57a0204e 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -119,7 +119,21 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) std::ifstream f(argv[i]); if (!f.is_open()) throw std::runtime_error("cppcheck: Couldn't open the file \"" + std::string(argv[i]) + "\""); - _settings.suppressions(f); + _settings.nomsg.parseFile(f); + } + + // Filter errors + else if (strcmp(argv[i], "--exitcode-suppressions") == 0) + { + ++i; + + if (i >= argc) + throw std::runtime_error("cppcheck: No file specified for the --exitcode-suppressions option"); + + std::ifstream f(argv[i]); + if (!f.is_open()) + throw std::runtime_error("cppcheck: Couldn't open the file \"" + std::string(argv[i]) + "\""); + _settings.nofail.parseFile(f); } // Enables inline suppressions. @@ -334,9 +348,10 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) "\n" "Syntax:\n" " cppcheck [--all] [--append=file] [--auto-dealloc file.lst] [--enable]\n" - " [--error-exitcode=[n]] [--force] [--help] [-Idir] [-j [jobs]]\n" - " [--quiet] [--style] [--suppressions file.txt] [--inline-suppr]\n" - " [--verbose] [--version] [--xml] [file or path1]\n" + " [--error-exitcode=[n]] [--exitcode-suppressions file] [--force]\n" + " [--help] [-Idir] [-j [jobs]] [--quiet] [--style]\n" + " [--suppressions file.txt] [--inline-suppr] [--verbose]\n" + " [--version] [--xml] [file or path1]\n" " [file or path] ...\n" "\n" "If path is given instead of filename, *.cpp, *.cxx, *.cc, *.c++ and *.c files\n" @@ -366,6 +381,9 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) " if arguments are not valid or if no input files are\n" " provided. Note that your operating system can\n" " modify this value, e.g. 256 can become 0.\n" + " --exitcode-suppressions file\n" + " Used when certain messages should be displayed but\n" + " should not cause a non-zero exitcode.\n" " -f, --force Force checking on files that have \"too many\"\n" " configurations\n" " -h, --help Print this help\n" @@ -410,6 +428,8 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) unsigned int CppCheck::check() { + exitcode = 0; + _checkUnusedFunctions.setErrorLogger(this); std::sort(_filenames.begin(), _filenames.end()); for (unsigned int c = 0; c < _filenames.size(); c++) @@ -487,11 +507,8 @@ unsigned int CppCheck::check() _checkUnusedFunctions.check(); } - - - unsigned int result = static_cast(_errorList.size()); _errorList.clear(); - return result; + return exitcode; } @@ -573,9 +590,12 @@ void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg) line = msg._callStack.back().line; } - if (_settings.isSuppressed(msg._id, file, line)) + if (_settings.nomsg.isSuppressed(msg._id, file, line)) return; + if (!_settings.nofail.isSuppressed(msg._id, file, line)) + exitcode = 1; + _errorList.push_back(errmsg); std::string errmsg2(errmsg); if (_settings._verbose) diff --git a/lib/cppcheck.h b/lib/cppcheck.h index f8f9d59c1..844fe25e9 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -135,6 +135,7 @@ private: */ virtual void reportOut(const std::string &outmsg); + unsigned int exitcode; std::list _errorList; std::ostringstream _errout; Settings _settings; diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 4cdbd425a..d96cac00a 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -202,7 +202,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri { // Add the suppressions. for (size_t j(0); j < suppressionIDs.size(); ++j) - settings->addSuppression(suppressionIDs[j], filename, lineno); + settings->nomsg.addSuppression(suppressionIDs[j], filename, lineno); suppressionIDs.clear(); } diff --git a/lib/settings.cpp b/lib/settings.cpp index 1473a9885..d7cfdbc75 100755 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -59,7 +59,7 @@ void Settings::autoDealloc(std::istream &istr) } } -bool Settings::suppressions(std::istream &istr) +bool Settings::Suppressions::parseFile(std::istream &istr) { std::string line; while (getline(istr, line)) @@ -87,13 +87,13 @@ bool Settings::suppressions(std::istream &istr) return true; } -void Settings::addSuppression(const std::string &errorId, const std::string &file, unsigned int line) +void Settings::Suppressions::addSuppression(const std::string &errorId, const std::string &file, unsigned int line) { _suppressions[errorId][file].push_back(line); _suppressions[errorId][file].sort(); } -bool Settings::isSuppressed(const std::string &errorId, const std::string &file, unsigned int line) +bool Settings::Suppressions::isSuppressed(const std::string &errorId, const std::string &file, unsigned int line) { if (_suppressions.find(errorId) == _suppressions.end()) return false; diff --git a/lib/settings.h b/lib/settings.h index 0918b4238..728c2f681 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -43,9 +43,6 @@ private: /** Code to append in the checks */ std::string _append; - /** List of error which the user doesn't want to see. */ - std::map > > _suppressions; - /** enable extra checks by id */ std::map _enabled; public: @@ -90,28 +87,6 @@ public: /** Add class to list of automatically deallocated classes */ void addAutoAllocClass(const std::string &name); - /** - * Don't show errors listed in the file. - * @param istr Open file stream where errors can be read. - * @return true on success, false in syntax error is noticed. - */ - bool suppressions(std::istream &istr); - - /** - * Don't show this error. If file and/or line are optional. In which case - * the errorId alone is used for filtering. - * @param errorId, the id for the error, e.g. "arrayIndexOutOfBounds" - * @param file File name with the path, e.g. "src/main.cpp" - * @param line number, e.g. "123" - */ - void addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0); - - /** - * Returns true if this message should not be shown to the user. - * @return true if this error is suppressed. - */ - bool isSuppressed(const std::string &errorId, const std::string &file, unsigned int line); - /** is a class automaticly deallocated? */ bool isAutoDealloc(const char classname[]) const; @@ -121,9 +96,6 @@ public: /** get append code */ std::string append() const; - /** enable extra checks by id */ - //std::string enableId; - /** * Returns true if given id is in the list of * enabled extra checks. See addEnabled() @@ -138,6 +110,46 @@ public: * or empty string to enable all. e.g. "style,possibleError" */ void addEnabled(const std::string &str); + + /** class for handling suppressions */ + class Suppressions + { + private: + /** List of error which the user doesn't want to see. */ + std::map > > _suppressions; + public: + /** + * Don't show errors listed in the file. + * @param istr Open file stream where errors can be read. + * @return true on success, false in syntax error is noticed. + */ + bool parseFile(std::istream &istr); + + /** + * Don't show this error. If file and/or line are optional. In which case + * the errorId alone is used for filtering. + * @param errorId, the id for the error, e.g. "arrayIndexOutOfBounds" + * @param file File name with the path, e.g. "src/main.cpp" + * @param line number, e.g. "123" + */ + void addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0); + + /** + * Returns true if this message should not be shown to the user. + * @param errorId, the id for the error, e.g. "arrayIndexOutOfBounds" + * @param file File name with the path, e.g. "src/main.cpp" + * @param line number, e.g. "123" + * @return true if this error is suppressed. + */ + bool isSuppressed(const std::string &errorId, const std::string &file, unsigned int line); + + }; + + /** suppress message */ + Suppressions nomsg; + + /** suppress exitcode */ + Suppressions nofail; }; /// @}