diff --git a/src/check.h b/src/check.h index c249aff0f..3bfd13cb7 100644 --- a/src/check.h +++ b/src/check.h @@ -87,20 +87,14 @@ protected: void reportError(const Token *tok, const Severity::e severity, const std::string &id, const std::string &msg) { std::list callstack; - callstack.push_back(tok); + if (tok) + callstack.push_back(tok); reportError(callstack, severity, id, msg); } /** report an error */ void reportError(const std::list &callstack, const Severity::e severity, const std::string &id, std::string msg) { - // No errorLogger => just report the message to stdout - if (_errorLogger == NULL) - { - std::cout << "(" << Severity::stringify(severity) << ") " << msg << std::endl; - return; - } - // If the verbose flag hasn't been given, don't show verbose information if (!_settings || !_settings->_verbose) { @@ -118,7 +112,12 @@ protected: locationList.push_back(loc); } - _errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList, Severity::stringify(severity), msg, id)); + const ErrorLogger::ErrorMessage errmsg(locationList, Severity::stringify(severity), msg, id); + + if (_errorLogger) + _errorLogger->reportErr(errmsg); + else + std::cout << errmsg.toXML() << std::endl; } private: diff --git a/src/checkautovariables.h b/src/checkautovariables.h index 831d93bbf..9d06bbde4 100644 --- a/src/checkautovariables.h +++ b/src/checkautovariables.h @@ -70,7 +70,6 @@ private: void getErrorMessages() { - std::cout << "===auto variables===" << "\n"; reportError(0, Severity::error, "autoVariables", "Wrong assignement of an auto-variable to an effective parameter of a function"); errorReturnPointerToLocalArray(0); } diff --git a/src/checkbufferoverrun.cpp b/src/checkbufferoverrun.cpp index 0a9e614b3..34d718529 100644 --- a/src/checkbufferoverrun.cpp +++ b/src/checkbufferoverrun.cpp @@ -46,9 +46,14 @@ CheckBufferOverrun instance; void CheckBufferOverrun::arrayIndexOutOfBounds(const Token *tok) { - _callStack.push_back(tok); - arrayIndexOutOfBounds(); - _callStack.pop_back(); + if (!tok) + arrayIndexOutOfBounds(); + else + { + _callStack.push_back(tok); + arrayIndexOutOfBounds(); + _callStack.pop_back(); + } } void CheckBufferOverrun::arrayIndexOutOfBounds() diff --git a/src/checkbufferoverrun.h b/src/checkbufferoverrun.h index 7fe6a510c..699df3e51 100644 --- a/src/checkbufferoverrun.h +++ b/src/checkbufferoverrun.h @@ -79,7 +79,6 @@ private: void getErrorMessages() { - std::cout << "===buffer overruns===" << "\n"; arrayIndexOutOfBounds(0); bufferOverrun(0); strncatUsage(0); diff --git a/src/checkclass.h b/src/checkclass.h index ad3f46a16..30b57c5f6 100644 --- a/src/checkclass.h +++ b/src/checkclass.h @@ -114,7 +114,6 @@ private: void getErrorMessages() { - std::cout << "===classes===" << "\n"; noConstructorError(0, "classname"); uninitVarError(0, "classname", "varname"); operatorEqVarError(0, "classname", ""); diff --git a/src/checkdangerousfunctions.h b/src/checkdangerousfunctions.h index 6c5df3375..981a86d97 100644 --- a/src/checkdangerousfunctions.h +++ b/src/checkdangerousfunctions.h @@ -59,7 +59,6 @@ private: void getErrorMessages() { - std::cout << "===dangerous functions===" << "\n"; dangerousFunctionmktemp(0); dangerousFunctiongets(0); dangerousFunctionscanf(0); diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index ee9c68401..9832c5ec6 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -224,19 +224,17 @@ void CheckMemoryLeak::memoryLeak(const Token *tok, const char varname[], AllocTy //--------------------------------------------------------------------------- -ErrorLogger::ErrorMessage CheckMemoryLeak::errmsg(const Token *tok, Severity::e severity, const std::string &id, const std::string &msg) const +void CheckMemoryLeak::reportErr(const Token *tok, Severity::e severity, const std::string &id, const std::string &msg) const { - ErrorLogger::ErrorMessage::FileLocation loc; - loc.line = tok->linenr(); - loc.file = tokenizer->file(tok); + std::list callstack; - std::list callstack; - callstack.push_back(loc); + if (tok) + callstack.push_back(tok); - return ErrorLogger::ErrorMessage(callstack, Severity::stringify(severity), msg, id); + reportErr(callstack, severity, id, msg); } -ErrorLogger::ErrorMessage CheckMemoryLeak::errmsg(const std::list &callstack, Severity::e severity, const std::string &id, const std::string &msg) const +void CheckMemoryLeak::reportErr(const std::list &callstack, Severity::e severity, const std::string &id, const std::string &msg) const { std::list locations; @@ -249,42 +247,47 @@ ErrorLogger::ErrorMessage CheckMemoryLeak::errmsg(const std::list locations.push_back(loc); } - return ErrorLogger::ErrorMessage(locations, Severity::stringify(severity), msg, id); + const ErrorLogger::ErrorMessage errmsg(locations, Severity::stringify(severity), msg, id); + + if (errorLogger) + errorLogger->reportErr(errmsg); + else + std::cout << errmsg.toXML() << std::endl; } void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname) { - errorLogger->reportErr(errmsg(tok, Severity::error, "memleak", "Memory leak: " + varname)); + reportErr(tok, Severity::error, "memleak", "Memory leak: " + varname); } void CheckMemoryLeak::memleakallError(const Token *tok, const std::string &varname) { - errorLogger->reportErr(errmsg(tok, Severity::possibleError, "memleakall", "Memory leak: " + varname)); + reportErr(tok, Severity::possibleError, "memleakall", "Memory leak: " + varname); } void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname) { - errorLogger->reportErr(errmsg(tok, Severity::error, "resourceLeak", "Resource leak: " + varname)); + reportErr(tok, Severity::error, "resourceLeak", "Resource leak: " + varname); } void CheckMemoryLeak::deallocDeallocError(const Token *tok, const std::string &varname) { - errorLogger->reportErr(errmsg(tok, Severity::error, "deallocDealloc", "Deallocating a deallocated pointer: " + varname)); + reportErr(tok, Severity::error, "deallocDealloc", "Deallocating a deallocated pointer: " + varname); } void CheckMemoryLeak::deallocuseError(const Token *tok, const std::string &varname) { - errorLogger->reportErr(errmsg(tok, Severity::error, "deallocuse", "Dereferencing '" + varname + "' after it is deallocated / released")); + reportErr(tok, Severity::error, "deallocuse", "Dereferencing '" + varname + "' after it is deallocated / released"); } void CheckMemoryLeak::mismatchSizeError(const Token *tok, const std::string &sz) { - errorLogger->reportErr(errmsg(tok, Severity::error, "mismatchSize", "The given size " + sz + " is mismatching")); + reportErr(tok, Severity::error, "mismatchSize", "The given size " + sz + " is mismatching"); } void CheckMemoryLeak::mismatchAllocDealloc(const std::list &callstack, const std::string &varname) { - errorLogger->reportErr(errmsg(callstack, Severity::error, "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname)); + reportErr(callstack, Severity::error, "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname); } CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) const diff --git a/src/checkmemoryleak.h b/src/checkmemoryleak.h index 9f6921bdb..dddfec5da 100644 --- a/src/checkmemoryleak.h +++ b/src/checkmemoryleak.h @@ -63,7 +63,7 @@ private: * @param id type of message * @param msg text */ - ErrorLogger::ErrorMessage errmsg(const Token *location, Severity::e severity, const std::string &id, const std::string &msg) const; + void reportErr(const Token *location, Severity::e severity, const std::string &id, const std::string &msg) const; /** * Report error. Similar with the function Check::reportError @@ -72,7 +72,7 @@ private: * @param id type of message * @param msg text */ - ErrorLogger::ErrorMessage errmsg(const std::list &callstack, Severity::e severity, const std::string &id, const std::string &msg) const; + void reportErr(const std::list &callstack, Severity::e severity, const std::string &id, const std::string &msg) const; public: CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e) @@ -233,7 +233,17 @@ private: void checkScope(const Token *Tok1, const char varname[], bool classmember, unsigned int sz); void getErrorMessages() - { } + { + memleakError(0, "varname"); + memleakallError(0, "varname"); + resourceLeakError(0, "varname"); + + deallocDeallocError(0, "varname"); + deallocuseError(0, "varname"); + mismatchSizeError(0, "sz"); + std::list callstack; + mismatchAllocDealloc(callstack, "varname"); + } std::string name() const { diff --git a/src/checkother.h b/src/checkother.h index 1005872dd..8e8f167ac 100644 --- a/src/checkother.h +++ b/src/checkother.h @@ -148,7 +148,6 @@ public: void getErrorMessages() { - std::cout << "===other===" << "\n"; cstyleCastError(0); redundantIfDelete0Error(0); redundantIfRemoveError(0); diff --git a/src/checkstl.h b/src/checkstl.h index 1883a2c40..2f86c361f 100644 --- a/src/checkstl.h +++ b/src/checkstl.h @@ -103,7 +103,6 @@ private: void getErrorMessages() { - std::cout << "===stl===" << "\n"; iteratorsError(0, "container1", "container2"); dereferenceErasedError(0, "iter"); stlOutOfBoundsError(0, "i", "foo"); diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 3394cf910..e4efdbca1 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -223,10 +223,12 @@ std::string CppCheck::parseFromArgs(int argc, const char* const argv[]) else if (strcmp(argv[i], "--errorlist") == 0) { // call all "getErrorMessages" in all registered Check classes + std::cout << ErrorLogger::ErrorMessage::getXMLHeader(); for (std::list::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { (*it)->getErrorMessages(); } + std::cout << ErrorLogger::ErrorMessage::getXMLFooter() << std::endl; return ""; }