From 9a6569fde1c3d81a03530f78d640c0276c4778db Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 26 Jul 2016 16:27:55 +0200 Subject: [PATCH] Always set ErrorMessage::file0 to ensure that the source file that cppcheck is checking when an error occurs can be identified Removed unused function and unused includes from preprocessor.h/cpp --- cli/cppcheckexecutor.cpp | 3 ++- lib/checkbufferoverrun.cpp | 1 + lib/checkunusedfunctions.cpp | 2 +- lib/cppcheck.cpp | 17 +++++++++------- lib/errorlogger.cpp | 8 +++++--- lib/errorlogger.h | 4 ++-- lib/preprocessor.cpp | 23 +++------------------- lib/preprocessor.h | 10 ---------- lib/valueflow.cpp | 4 ++-- test/testerrorlogger.cpp | 38 ++++++++++++++++++------------------ test/testpreprocessor.cpp | 1 + 11 files changed, 46 insertions(+), 65 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index fa5e880e1..3867bcf50 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -790,7 +790,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha "std.cfg should be available in " + cfgfolder + " or the CFGDIR " "should be configured."); #endif - ErrorLogger::ErrorMessage errmsg(callstack, Severity::information, msg+" "+details, "failedToLoadCfg", false); + ErrorLogger::ErrorMessage errmsg(callstack, emptyString, Severity::information, msg+" "+details, "failedToLoadCfg", false); reportErr(errmsg); return EXIT_FAILURE; } @@ -863,6 +863,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha if (settings.isEnabled("missingInclude") && (Preprocessor::missingIncludeFlag || Preprocessor::missingSystemIncludeFlag)) { const std::list callStack; ErrorLogger::ErrorMessage msg(callStack, + emptyString, Severity::information, "Cppcheck cannot find all the include files (use --check-config for details)\n" "Cppcheck cannot find all the include files. Cppcheck can check the code without the " diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 6e6a08fa0..49799ca31 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1981,6 +1981,7 @@ void CheckBufferOverrun::analyseWholeProgram(const std::list & ostr << "Array " << it->first << '[' << sz->second << "] accessed at index " << it->second.index << " which is out of bounds"; const ErrorLogger::ErrorMessage errmsg(locationList, + emptyString, Severity::error, ostr.str(), "arrayIndexOutOfBounds", diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 5616784ff..9cc3e1d17 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -252,7 +252,7 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger, locationList.push_back(fileLoc); } - const ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, "The function '" + funcname + "' is never used.", "unusedFunction", CWE561, false); + const ErrorLogger::ErrorMessage errmsg(locationList, emptyString, Severity::style, "The function '" + funcname + "' is never used.", "unusedFunction", CWE561, false); if (errorLogger) errorLogger->reportErr(errmsg); else diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 1676b2936..9fd0b1286 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -308,16 +308,17 @@ unsigned int CppCheck::processFile(const std::string& filename, std::istream& fi loc.setfile(fixedpath); } else { ErrorLogger::ErrorMessage::FileLocation loc2; - loc2.setfile(Path::toNativeSeparators(filename.c_str())); + loc2.setfile(Path::toNativeSeparators(filename)); locationList.push_back(loc2); loc.setfile(_tokenizer.list.getSourceFilePath()); } locationList.push_back(loc); - const ErrorLogger::ErrorMessage errmsg(locationList, - Severity::error, - e.errorMessage, - e.id, - false); + ErrorLogger::ErrorMessage errmsg(locationList, + _tokenizer.list.getSourceFilePath(), + Severity::error, + e.errorMessage, + e.id, + false); reportErr(errmsg); } @@ -360,6 +361,7 @@ void CppCheck::internalError(const std::string &filename, const std::string &msg callstack.push_back(loc1); ErrorLogger::ErrorMessage errmsg(callstack, + emptyString, Severity::information, fullmsg, "internalError", @@ -557,6 +559,7 @@ void CppCheck::tooManyConfigsError(const std::string &file, const std::size_t nu ErrorLogger::ErrorMessage errmsg(loclist, + emptyString, Severity::information, msg.str(), "toomanyconfigs", CWE398, @@ -567,7 +570,6 @@ void CppCheck::tooManyConfigsError(const std::string &file, const std::size_t nu void CppCheck::purgedConfigurationMessage(const std::string &file, const std::string& configuration) { - tooManyConfigs = false; if (_settings.isEnabled("information") && file.empty()) @@ -581,6 +583,7 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st } ErrorLogger::ErrorMessage errmsg(loclist, + emptyString, Severity::information, "The configuration '" + configuration + "' was not checked because its code equals another one.", "purgedConfiguration", diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index ef1bfd87f..def486834 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -48,9 +48,10 @@ ErrorLogger::ErrorMessage::ErrorMessage() { } -ErrorLogger::ErrorMessage::ErrorMessage(const std::list &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive) : +ErrorLogger::ErrorMessage::ErrorMessage(const std::list &callStack, const std::string& file0_, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive) : _callStack(callStack), // locations for this error message _id(id), // set the message id + file0(file0_), _severity(severity), // severity for this error message _cwe(0U), _inconclusive(inconclusive) @@ -61,9 +62,10 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list &callStack -ErrorLogger::ErrorMessage::ErrorMessage(const std::list &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive) : +ErrorLogger::ErrorMessage::ErrorMessage(const std::list &callStack, const std::string& file0_, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive) : _callStack(callStack), // locations for this error message _id(id), // set the message id + file0(file0_), _severity(severity), // severity for this error message _cwe(cwe.id), _inconclusive(inconclusive) @@ -413,7 +415,7 @@ void ErrorLogger::reportUnmatchedSuppressions(const std::list callStack = make_container< std::list > () << ErrorLogger::ErrorMessage::FileLocation(i->file, i->line); - reportErr(ErrorLogger::ErrorMessage(callStack, Severity::information, "Unmatched suppression: " + i->id, "unmatchedSuppression", false)); + reportErr(ErrorLogger::ErrorMessage(callStack, emptyString, Severity::information, "Unmatched suppression: " + i->id, "unmatchedSuppression", false)); } } diff --git a/lib/errorlogger.h b/lib/errorlogger.h index fdeb0de3c..82b7dcbaf 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -208,8 +208,8 @@ public: std::string _file; }; - ErrorMessage(const std::list &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive); - ErrorMessage(const std::list &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive); + ErrorMessage(const std::list &callStack, const std::string& file0, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive); + ErrorMessage(const std::list &callStack, const std::string& file0, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive); ErrorMessage(const std::list& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive); ErrorMessage(const std::list& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, const CWE &cwe, bool inconclusive); ErrorMessage(); diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 38a9c1f15..f88fdb82c 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -25,12 +25,10 @@ #include #include -#include #include #include #include #include -#include /** * Remove heading and trailing whitespaces from the input parameter. @@ -69,21 +67,6 @@ Preprocessor::~Preprocessor() delete it->second; } -void Preprocessor::writeError(const std::string &fileName, const unsigned int linenr, ErrorLogger *errorLogger, const std::string &errorType, const std::string &errorText) -{ - if (!errorLogger) - return; - - std::list locationList; - ErrorLogger::ErrorMessage::FileLocation loc(fileName, linenr); - locationList.push_back(loc); - errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList, - Severity::error, - errorText, - errorType, - false)); -} - static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_settings) { @@ -657,6 +640,7 @@ void Preprocessor::error(const std::string &filename, unsigned int linenr, const locationList.push_back(loc); } _errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList, + file0, Severity::error, msg, "preprocessorErrorDirective", @@ -685,13 +669,12 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line loc.setfile(Path::toNativeSeparators(filename)); locationList.push_back(loc); } - ErrorLogger::ErrorMessage errmsg(locationList, Severity::information, + ErrorLogger::ErrorMessage errmsg(locationList, file0, Severity::information, (headerType==SystemHeader) ? "Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results." : "Include file: \"" + header + "\" not found.", (headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude", false); - errmsg.file0 = file0; _errorLogger->reportInfo(errmsg); } } @@ -763,7 +746,7 @@ void Preprocessor::validateCfgError(const std::string &cfg, const std::string &m std::list locationList; ErrorLogger::ErrorMessage::FileLocation loc(file0, 1); locationList.push_back(loc); - ErrorLogger::ErrorMessage errmsg(locationList, Severity::information, "Skipping configuration '" + cfg + "' since the value of '" + macro + "' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.", id, false); + ErrorLogger::ErrorMessage errmsg(locationList, file0, Severity::information, "Skipping configuration '" + cfg + "' since the value of '" + macro + "' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.", id, false); _errorLogger->reportInfo(errmsg); } diff --git a/lib/preprocessor.h b/lib/preprocessor.h index 826994ba5..1fd039898 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -155,16 +155,6 @@ public: bool validateCfg(const std::string &code, const std::string &cfg); void validateCfgError(const std::string &cfg, const std::string ¯o); - /** - * report error - * @param fileName name of file that the error was found in - * @param linenr linenr in file - * @param errorLogger Error logger to write error to - * @param errorType id string for error - * @param errorText Plain text - */ - static void writeError(const std::string &fileName, const unsigned int linenr, ErrorLogger *errorLogger, const std::string &errorType, const std::string &errorText); - private: /** diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d322b9997..3e7b243b5 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -82,8 +82,8 @@ static void execute(const Token *expr, static void bailout(TokenList *tokenlist, ErrorLogger *errorLogger, const Token *tok, const std::string &what) { std::list callstack; - callstack.push_back(ErrorLogger::ErrorMessage::FileLocation(tok,tokenlist)); - ErrorLogger::ErrorMessage errmsg(callstack, Severity::debug, "ValueFlow bailout: " + what, "valueFlowBailout", false); + callstack.push_back(ErrorLogger::ErrorMessage::FileLocation(tok, tokenlist)); + ErrorLogger::ErrorMessage errmsg(callstack, tokenlist->getSourceFilePath(), Severity::debug, "ValueFlow bailout: " + what, "valueFlowBailout", false); errorLogger->reportErr(errmsg); } diff --git a/test/testerrorlogger.cpp b/test/testerrorlogger.cpp index fb5b50058..e1d96ac59 100644 --- a/test/testerrorlogger.cpp +++ b/test/testerrorlogger.cpp @@ -111,7 +111,7 @@ private: void ErrorMessageConstruct() const { std::list locs(1, fooCpp5); - ErrorMessage msg(locs, Severity::error, "Programming error.", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.", "errorId", false); ASSERT_EQUALS(1, (int)msg._callStack.size()); ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Programming error.", msg.verboseMessage()); @@ -123,7 +123,7 @@ private: std::list locs; locs.push_back(fooCpp5); locs.push_back(barCpp8); - ErrorMessage msg(locs, Severity::error, "Programming error.", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.", "errorId", false); ASSERT_EQUALS(2, (int)msg._callStack.size()); ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Programming error.", msg.verboseMessage()); @@ -133,7 +133,7 @@ private: void ErrorMessageVerbose() const { std::list locs(1, fooCpp5); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ASSERT_EQUALS(1, (int)msg._callStack.size()); ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Verbose error", msg.verboseMessage()); @@ -145,7 +145,7 @@ private: std::list locs; locs.push_back(fooCpp5); locs.push_back(barCpp8); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ASSERT_EQUALS(2, (int)msg._callStack.size()); ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Verbose error", msg.verboseMessage()); @@ -155,7 +155,7 @@ private: void CustomFormat() const { std::list locs(1, fooCpp5); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ASSERT_EQUALS(1, (int)msg._callStack.size()); ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Verbose error", msg.verboseMessage()); @@ -165,7 +165,7 @@ private: void CustomFormat2() const { std::list locs(1, fooCpp5); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ASSERT_EQUALS(1, (int)msg._callStack.size()); ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Verbose error", msg.verboseMessage()); @@ -178,7 +178,7 @@ private: std::list locs; locs.push_back(fooCpp5); locs.push_back(barCpp8); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ASSERT_EQUALS(2, (int)msg._callStack.size()); ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Verbose error", msg.verboseMessage()); @@ -188,7 +188,7 @@ private: void ToXml() const { std::list locs(1, fooCpp5); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ASSERT_EQUALS("\n", ErrorLogger::ErrorMessage::getXMLHeader(1)); ASSERT_EQUALS("", ErrorLogger::ErrorMessage::getXMLFooter(1)); ASSERT_EQUALS(" ", msg.toXML(false,1)); @@ -198,7 +198,7 @@ private: std::list locs; locs.push_back(fooCpp5); locs.push_back(barCpp8); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ASSERT_EQUALS("\n", ErrorLogger::ErrorMessage::getXMLHeader(1)); ASSERT_EQUALS("", ErrorLogger::ErrorMessage::getXMLFooter(1)); ASSERT_EQUALS(" ", msg.toXML(false,1)); @@ -206,7 +206,7 @@ private: void ToVerboseXml() const { std::list locs(1, fooCpp5); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ASSERT_EQUALS("\n", ErrorLogger::ErrorMessage::getXMLHeader(1)); ASSERT_EQUALS("", ErrorLogger::ErrorMessage::getXMLFooter(1)); ASSERT_EQUALS(" ", msg.toXML(true,1)); @@ -216,7 +216,7 @@ private: std::list locs; locs.push_back(fooCpp5); locs.push_back(barCpp8); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ASSERT_EQUALS("\n", ErrorLogger::ErrorMessage::getXMLHeader(1)); ASSERT_EQUALS("", ErrorLogger::ErrorMessage::getXMLFooter(1)); ASSERT_EQUALS(" ", msg.toXML(true,1)); @@ -224,7 +224,7 @@ private: void ToXmlV2() const { std::list locs(1, fooCpp5); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); std::string header("\n\n"); header += " locs; locs.push_back(fooCpp5); locs.push_back(barCpp8); - ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); std::string header("\n\n"); header += " locs; - ErrorMessage msg(locs, Severity::error, "Programming error.\nComparing \"\203\" with \"\003\"", "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nComparing \"\203\" with \"\003\"", "errorId", false); const std::string message(" "); ASSERT_EQUALS(message, msg.toXML(false, 2)); } @@ -266,9 +266,9 @@ private: const char code1[]="äöü"; const char code2[]="\x12\x00\x00\x01"; std::list locs; - ErrorMessage msg1(locs, Severity::error, std::string("Programming error.\nReading \"")+code1+"\"", "errorId", false); + ErrorMessage msg1(locs, emptyString, Severity::error, std::string("Programming error.\nReading \"")+code1+"\"", "errorId", false); ASSERT_EQUALS(" ", msg1.toXML(false, 2)); - ErrorMessage msg2(locs, Severity::error, std::string("Programming error.\nReading \"")+code2+"\"", "errorId", false); + ErrorMessage msg2(locs, emptyString, Severity::error, std::string("Programming error.\nReading \"")+code2+"\"", "errorId", false); ASSERT_EQUALS(" ", msg2.toXML(false, 2)); } } @@ -278,7 +278,7 @@ private: std::list locs(1, fooCpp5); // Inconclusive error message - ErrorMessage msg(locs, Severity::error, "Programming error", "errorId", true); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error", "errorId", true); // Don't save inconclusive messages if the xml version is 1 ASSERT_EQUALS("", msg.toXML(false, 1)); @@ -293,7 +293,7 @@ private: void SerializeInconclusiveMessage() const { // Inconclusive error message std::list locs; - ErrorMessage msg(locs, Severity::error, "Programming error", "errorId", true); + ErrorMessage msg(locs, emptyString, Severity::error, "Programming error", "errorId", true); ASSERT_EQUALS("7 errorId" "5 error" "1 0" @@ -318,7 +318,7 @@ private: void SerializeSanitize() const { std::list locs; - ErrorMessage msg(locs, Severity::error, std::string("Illegal character in \"foo\001bar\""), "errorId", false); + ErrorMessage msg(locs, emptyString, Severity::error, std::string("Illegal character in \"foo\001bar\""), "errorId", false); ASSERT_EQUALS("7 errorId" "5 error" diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 0995841ab..1467dae54 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -61,6 +61,7 @@ public: ErrorLogger::ErrorMessage::FileLocation loc(msg.location.file(), msg.location.line); locationList.push_back(loc); errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList, + emptyString, Severity::error, msg.msg, "preprocessorError",