Fixed #2373 (Using XML2 in --errorlist output)

This commit is contained in:
Daniel Marjamäki 2010-12-29 12:43:29 +01:00
parent fd27be2440
commit 38e7209d26
21 changed files with 187 additions and 143 deletions

View File

@ -347,6 +347,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
{
//_cppcheck->getErrorMessages();
_showErrorMessages = true;
_settings->_xml = true;
return true;
}

View File

@ -52,7 +52,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
if (parser.GetShowErrorMessages())
{
cppcheck->getErrorMessages();
return true;
std::exit(0);
}
}

View File

@ -95,7 +95,7 @@ public:
virtual void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) = 0;
/** get error messages */
virtual void getErrorMessages() = 0;
virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) = 0;
/** class name, used to generate documentation */
virtual std::string name() const = 0;

View File

@ -87,14 +87,15 @@ private:
void errorReturnAutocstr(const Token *tok);
void errorReturnTempPointer(const Token *tok);
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
errorAutoVariableAssignment(0);
errorReturnPointerToLocalArray(0);
errorReturnReference(0);
errorReturnTempReference(0);
errorReturnAutocstr(0);
errorReturnTempPointer(0);
CheckAutoVariables c(0,settings,errorLogger);
c.errorAutoVariableAssignment(0);
c.errorReturnPointerToLocalArray(0);
c.errorReturnReference(0);
c.errorReturnTempReference(0);
c.errorReturnAutocstr(0);
c.errorReturnTempPointer(0);
}
std::string name() const

View File

@ -188,17 +188,18 @@ public:
void cmdLineArgsError(const Token *tok);
void pointerOutOfBounds(const Token *tok); // UB when result of calculation is out of bounds
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
arrayIndexOutOfBounds(0, 2, 2);
bufferOverrun(0, std::string("buffer"));
strncatUsage(0);
outOfBounds(0, "index");
sizeArgumentAsChar(0);
terminateStrncpyError(0);
negativeIndexError(0, -1);
cmdLineArgsError(0);
pointerOutOfBounds(0);
CheckBufferOverrun c(0, settings, errorLogger);
c.arrayIndexOutOfBounds(0, 2, 2);
c.bufferOverrun(0, std::string("buffer"));
c.strncatUsage(0);
c.outOfBounds(0, "index");
c.sizeArgumentAsChar(0);
c.terminateStrncpyError(0);
c.negativeIndexError(0, -1);
c.cmdLineArgsError(0);
c.pointerOutOfBounds(0);
}
std::string name() const

View File

@ -127,20 +127,21 @@ private:
void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname);
void checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname);
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
noConstructorError(0, "classname", false);
uninitVarError(0, "classname", "varname");
operatorEqVarError(0, "classname", "");
unusedPrivateFunctionError(0, "classname", "funcname");
memsetClassError(0, "memfunc");
memsetStructError(0, "memfunc", "classname");
operatorEqReturnError(0);
//virtualDestructorError(0, "Base", "Derived");
thisSubtractionError(0);
operatorEqRetRefThisError(0);
operatorEqToSelfError(0);
checkConstError(0, "class", "function");
CheckClass c(0, settings, errorLogger);
c.noConstructorError(0, "classname", false);
c.uninitVarError(0, "classname", "varname");
c.operatorEqVarError(0, "classname", "");
c.unusedPrivateFunctionError(0, "classname", "funcname");
c.memsetClassError(0, "memfunc");
c.memsetStructError(0, "memfunc", "classname");
c.operatorEqReturnError(0);
//c.virtualDestructorError(0, "Base", "Derived");
c.thisSubtractionError(0);
c.operatorEqRetRefThisError(0);
c.operatorEqToSelfError(0);
c.checkConstError(0, "class", "function");
}
std::string name() const

View File

@ -78,10 +78,11 @@ private:
}
/** Generate all possible errors (for --errorlist) */
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
destructorsError(0);
deallocThrowError(0, "p");
CheckExceptionSafety c(0, settings, errorLogger);
c.destructorsError(0);
c.deallocThrowError(0, "p");
}
/** Short description of class (for --doc) */

View File

@ -180,7 +180,10 @@ public:
: Check(tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog)
{
// get the symbol database
symbolDatabase = tokenizr->getSymbolDatabase();
if (tokenizr)
symbolDatabase = tokenizr->getSymbolDatabase();
else
symbolDatabase = 0;
}
/** @brief run all simplified checks */
@ -307,17 +310,19 @@ public:
void checkScope(const Token *Tok1, const std::string &varname, unsigned int varid, bool classmember, unsigned int sz);
/** Report all possible errors (for the --errorlist) */
void getErrorMessages()
void getErrorMessages(ErrorLogger *e, const Settings *settings)
{
memleakError(0, "varname");
resourceLeakError(0, "varname");
CheckMemoryLeakInFunction c(0, settings, e);
deallocDeallocError(0, "varname");
deallocuseError(0, "varname");
mismatchSizeError(0, "sz");
c.memleakError(0, "varname");
c.resourceLeakError(0, "varname");
c.deallocDeallocError(0, "varname");
c.deallocuseError(0, "varname");
c.mismatchSizeError(0, "sz");
std::list<const Token *> callstack;
mismatchAllocDealloc(callstack, "varname");
memleakUponReallocFailureError(0, "varname");
c.mismatchAllocDealloc(callstack, "varname");
c.memleakUponReallocFailureError(0, "varname");
}
/**
@ -385,7 +390,7 @@ private:
void checkPublicFunctions(const SymbolDatabase::SpaceInfo *spaceinfo, const Token *classtok);
void publicAllocationError(const Token *tok, const std::string &varname);
void getErrorMessages()
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/)
{ }
std::string name() const
@ -423,7 +428,7 @@ public:
private:
void getErrorMessages()
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/)
{ }
std::string name() const
@ -463,7 +468,7 @@ private:
void functionCallLeak(const Token *loc, const std::string &alloc, const std::string &functionCall);
void getErrorMessages()
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/)
{ }
std::string name() const

View File

@ -101,9 +101,10 @@ public:
void nullPointerError(const Token *tok, const std::string &varname);
void nullPointerError(const Token *tok, const std::string &varname, const unsigned int line);
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
nullPointerError(0, "pointer");
CheckNullPointer c(0, settings, errorLogger);
c.nullPointerError(0, "pointer");
}
std::string name() const

View File

@ -112,12 +112,14 @@ private:
}
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
CheckObsoleteFunctions c(0, settings, errorLogger);
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
for (; it!=itend; ++it)
{
reportError(0, Severity::style, "obsoleteFunctions"+it->first, it->second);
c.reportError(0, Severity::style, "obsoleteFunctions"+it->first, it->second);
}
}

View File

@ -184,38 +184,40 @@ public:
void incorrectLogicOperatorError(const Token *tok);
void misusedScopeObjectError(const Token *tok, const std::string &varname);
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
CheckOther c(0, settings, errorLogger);
// error
sprintfOverlappingDataError(0, "varname");
udivError(0);
zerodivError(0);
mathfunctionCallError(0);
fflushOnInputStreamError(0, "stdin");
misusedScopeObjectError(NULL, "varname");
c.sprintfOverlappingDataError(0, "varname");
c.udivError(0);
c.zerodivError(0);
c.mathfunctionCallError(0);
c.fflushOnInputStreamError(0, "stdin");
c.misusedScopeObjectError(NULL, "varname");
// style/warning
cstyleCastError(0);
dangerousUsageStrtolError(0);
unusedStructMemberError(0, "structname", "variable");
passedByValueError(0, "parametername");
constStatementError(0, "type");
charArrayIndexError(0);
charBitOpError(0);
variableScopeError(0, "varname");
conditionAlwaysTrueFalse(0, "true/false");
strPlusChar(0);
sizeofsizeofError(0);
sizeofCalculationError(0);
redundantAssignmentInSwitchError(0, "varname");
selfAssignmentError(0, "varname");
assignmentInAssertError(0, "varname");
invalidScanfError(0);
incorrectLogicOperatorError(0);
unusedVariableError(0, "varname");
allocatedButUnusedVariableError(0, "varname");
unreadVariableError(0, "varname");
unassignedVariableError(0, "varname");
c.cstyleCastError(0);
c.dangerousUsageStrtolError(0);
c.unusedStructMemberError(0, "structname", "variable");
c.passedByValueError(0, "parametername");
c.constStatementError(0, "type");
c.charArrayIndexError(0);
c.charBitOpError(0);
c.variableScopeError(0, "varname");
c.conditionAlwaysTrueFalse(0, "true/false");
c.strPlusChar(0);
c.sizeofsizeofError(0);
c.sizeofCalculationError(0);
c.redundantAssignmentInSwitchError(0, "varname");
c.selfAssignmentError(0, "varname");
c.assignmentInAssertError(0, "varname");
c.invalidScanfError(0);
c.incorrectLogicOperatorError(0);
c.unusedVariableError(0, "varname");
c.allocatedButUnusedVariableError(0, "varname");
c.unreadVariableError(0, "varname");
c.unassignedVariableError(0, "varname");
}
std::string name() const

View File

@ -56,9 +56,10 @@ private:
/** Report Error */
void postfixOperatorError(const Token *tok);
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
postfixOperatorError(0);
CheckPostfixOperator c(0, settings, errorLogger);
c.postfixOperatorError(0);
}
std::string name() const

View File

@ -154,22 +154,23 @@ private:
void sizeError(const Token *tok);
void redundantIfRemoveError(const Token *tok);
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
invalidIteratorError(0, "iterator");
iteratorsError(0, "container1", "container2");
mismatchingContainersError(0);
dereferenceErasedError(0, "iter");
stlOutOfBoundsError(0, "i", "foo");
eraseError(0);
invalidIteratorError(0, "push_back|push_front|insert", "iterator");
invalidPointerError(0, "pointer");
stlBoundriesError(0, "container");
if_findError(0, false);
if_findError(0, true);
string_c_strError(0);
sizeError(0);
redundantIfRemoveError(0);
CheckStl c(0, settings, errorLogger);
c.invalidIteratorError(0, "iterator");
c.iteratorsError(0, "container1", "container2");
c.mismatchingContainersError(0);
c.dereferenceErasedError(0, "iter");
c.stlOutOfBoundsError(0, "i", "foo");
c.eraseError(0);
c.invalidIteratorError(0, "push_back|push_front|insert", "iterator");
c.invalidPointerError(0, "pointer");
c.stlBoundriesError(0, "container");
c.if_findError(0, false);
c.if_findError(0, true);
c.string_c_strError(0);
c.sizeError(0);
c.redundantIfRemoveError(0);
}
std::string name() const

View File

@ -77,12 +77,14 @@ public:
void uninitdataError(const Token *tok, const std::string &varname);
void uninitvarError(const Token *tok, const std::string &varname);
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
CheckUninitVar c(0, settings, errorLogger);
// error
uninitstringError(0, "varname");
uninitdataError(0, "varname");
uninitvarError(0, "varname");
c.uninitstringError(0, "varname");
c.uninitdataError(0, "varname");
c.uninitvarError(0, "varname");
}
std::string name() const

View File

@ -50,9 +50,10 @@ public:
private:
void getErrorMessages()
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
unusedFunctionError(0, "", "funcName");
CheckUnusedFunctions c(0, settings, errorLogger);
c.unusedFunctionError(errorLogger, "", "funcName");
}
/**

View File

@ -436,16 +436,14 @@ void CppCheck::reportStatus(unsigned int /*index*/, unsigned int /*max*/)
void CppCheck::getErrorMessages()
{
// call all "getErrorMessages" in all registered Check classes
std::cout << ErrorLogger::ErrorMessage::getXMLHeader(1);
std::cout << ErrorLogger::ErrorMessage::getXMLHeader(_settings._xml_version);
for (std::list<Check *>::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it)
{
(*it)->getErrorMessages();
}
(*it)->getErrorMessages(this, &_settings);
Tokenizer tokenizer(&_settings, 0);
tokenizer.getErrorMessages();
tokenizer.getErrorMessages(this, &_settings);
Preprocessor::getErrorMessages(std::cout);
Preprocessor::getErrorMessages(this, &_settings);
std::cout << ErrorLogger::ErrorMessage::getXMLFooter() << std::endl;
}

View File

@ -1399,9 +1399,9 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
{
if (settings && !settings->userDefines.empty())
{
std::ostringstream errmsg;
errmsg << line;
writeError(filename, lineno, errorLogger, "preprocessorErrorDirective", errmsg.str());
Settings settings2(*settings);
Preprocessor preprocessor(&settings2, errorLogger);
preprocessor.error(filename, lineno, line);
}
return "";
}
@ -1433,6 +1433,22 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
return expandMacros(ret.str(), filename, errorLogger);
}
void Preprocessor::error(const std::string &filename, unsigned int linenr, const std::string &msg)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
if (!filename.empty())
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = linenr;
loc.setfile(filename);
locationList.push_back(loc);
}
_errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList,
Severity::error,
msg,
"preprocessorErrorDirective"));
}
Preprocessor::HeaderTypes Preprocessor::getHeaderFileName(std::string &str)
{
std::string result;
@ -1606,24 +1622,36 @@ void Preprocessor::handleIncludes(std::string &code,
}
}
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = linenr; /** @todo set correct line */
loc.setfile(Path::toNativeSeparators(filePath));
locationList.push_back(loc);
// If the missing include is a system header then this is
// currently a debug-message.
const Severity::SeverityType severity = (headerType == UserHeader) ? Severity::information : Severity::debug;
const std::string id = (headerType == UserHeader) ? "missingInclude" : "debug";
ErrorLogger::ErrorMessage errmsg(locationList, severity, "Include file: \"" + filename + "\" not found.", id);
errmsg.file0 = file0;
_errorLogger->reportErr(errmsg);
missingInclude(Path::toNativeSeparators(filePath),
linenr,
filename,
headerType == UserHeader);
}
}
}
}
// Report that include is missing
void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, bool userheader)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
if (!filename.empty())
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = linenr;
loc.setfile(filename);
locationList.push_back(loc);
}
// If the missing include is a system header then this is
// currently a debug-message.
const Severity::SeverityType severity = userheader ? Severity::information : Severity::debug;
const std::string id = userheader ? "missingInclude" : "debug";
ErrorLogger::ErrorMessage errmsg(locationList, severity, "Include file: \"" + header + "\" not found.", id);
errmsg.file0 = file0;
_errorLogger->reportErr(errmsg);
}
/**
* Skip string in line. A string begins and ends with either a &quot; or a &apos;
* @param line the string
@ -2405,18 +2433,11 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
return ostr.str();
}
void Preprocessor::getErrorMessages(std::ostream &ostr)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::style,
"Include file: \"\" not found.",
"missingInclude");
ostr << errmsg.toXML(false, 1) << std::endl;
const ErrorLogger::ErrorMessage errmsg2(locationList,
Severity::error,
"#error ...",
"preprocessorErrorDirective");
ostr << errmsg2.toXML(false, 1) << std::endl;
void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
Settings settings2(*settings);
Preprocessor preprocessor(&settings2, errorLogger);
preprocessor.missingInclude("", 1, "", true);
preprocessor.error("", 1, "#error message"); // #error ..
}

View File

@ -196,9 +196,13 @@ public:
*/
static bool match_cfg_def(const std::map<std::string, std::string> &cfg, std::string def);
static void getErrorMessages(std::ostream &ostr);
static void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings);
private:
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, bool userheader);
void error(const std::string &filename, unsigned int linenr, const std::string &msg);
/**
* Search includes from code and append code from the included
* file

View File

@ -8224,10 +8224,11 @@ void Tokenizer::simplifyConst()
}
}
void Tokenizer::getErrorMessages()
void Tokenizer::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
syntaxError(0, ' ');
cppcheckError(0);
Tokenizer t(settings, errorLogger);
t.syntaxError(0, ' ');
t.cppcheckError(0);
}
/** find pattern */

View File

@ -133,7 +133,7 @@ public:
/**
* get error messages
*/
virtual void getErrorMessages();
virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings);
/** Simplify assignment in function call "f(x=g());" => "x=g();f(x);"
*/

View File

@ -2763,7 +2763,7 @@ private:
{
// Ticket #2292: segmentation fault when using --errorlist
CheckBufferOverrun c;
c.getErrorMessages();
c.getErrorMessages(this, 0);
}
};