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
This commit is contained in:
PKEuS 2016-07-26 16:27:55 +02:00
parent 560e7d618d
commit 9a6569fde1
11 changed files with 46 additions and 65 deletions

View File

@ -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<ErrorLogger::ErrorMessage::FileLocation> 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 "

View File

@ -1981,6 +1981,7 @@ void CheckBufferOverrun::analyseWholeProgram(const std::list<Check::FileInfo*> &
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",

View File

@ -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

View File

@ -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",

View File

@ -48,9 +48,10 @@ ErrorLogger::ErrorMessage::ErrorMessage()
{
}
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive) :
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &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<FileLocation> &callStack
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive) :
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &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<Suppressions::Supp
const std::list<ErrorLogger::ErrorMessage::FileLocation> callStack = make_container< std::list<ErrorLogger::ErrorMessage::FileLocation> > ()
<< 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));
}
}

View File

@ -208,8 +208,8 @@ public:
std::string _file;
};
ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive);
ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive);
ErrorMessage(const std::list<FileLocation> &callStack, const std::string& file0, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive);
ErrorMessage(const std::list<FileLocation> &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<const Token*>& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive);
ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, const CWE &cwe, bool inconclusive);
ErrorMessage();

View File

@ -25,12 +25,10 @@
#include <algorithm>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <vector>
#include <set>
#include <stack>
/**
* 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<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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);
}

View File

@ -155,16 +155,6 @@ public:
bool validateCfg(const std::string &code, const std::string &cfg);
void validateCfgError(const std::string &cfg, const std::string &macro);
/**
* 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:
/**

View File

@ -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<ErrorLogger::ErrorMessage::FileLocation> 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);
}

View File

@ -111,7 +111,7 @@ private:
void ErrorMessageConstruct() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS(" <error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
@ -198,7 +198,7 @@ private:
std::list<ErrorLogger::ErrorMessage::FileLocation> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS(" <error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
@ -206,7 +206,7 @@ private:
void ToVerboseXml() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS(" <error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
@ -216,7 +216,7 @@ private:
std::list<ErrorLogger::ErrorMessage::FileLocation> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS(" <error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
@ -224,7 +224,7 @@ private:
void ToXmlV2() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"2\">\n");
header += " <cppcheck version=\"";
header += CppCheck::version();
@ -241,7 +241,7 @@ private:
std::list<ErrorLogger::ErrorMessage::FileLocation> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"2\">\n");
header += " <cppcheck version=\"";
header += CppCheck::version();
@ -258,7 +258,7 @@ private:
void ToXmlV2Encoding() const {
{
std::list<ErrorLogger::ErrorMessage::FileLocation> 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(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Comparing &quot;\\203&quot; with &quot;\\003&quot;\"/>");
ASSERT_EQUALS(message, msg.toXML(false, 2));
}
@ -266,9 +266,9 @@ private:
const char code1[]="äöü";
const char code2[]="\x12\x00\x00\x01";
std::list<ErrorLogger::ErrorMessage::FileLocation> 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(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Reading &quot;\\303\\244\\303\\266\\303\\274&quot;\"/>", 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(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Reading &quot;\\022&quot;\"/>", msg2.toXML(false, 2));
}
}
@ -278,7 +278,7 @@ private:
std::list<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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<ErrorLogger::ErrorMessage::FileLocation> 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"

View File

@ -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",