From d45186d64526e35dc0bde26c1a75e2921ee7c344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 27 Aug 2010 20:28:00 +0200 Subject: [PATCH] Added --debug-warnings that we can use to enable various debug warnings --- lib/checkclass.cpp | 8 +++---- lib/checkclass.h | 2 +- lib/checkmemoryleak.cpp | 11 +++++----- lib/cppcheck.cpp | 6 +++++- lib/preprocessor.cpp | 8 +++++-- lib/settings.cpp | 2 +- lib/settings.h | 5 ++++- lib/tokenize.cpp | 43 ++++++++++++++++++++++++------------- test/testmemleak.cpp | 2 +- test/testpreprocessor.cpp | 4 ++-- test/testsimplifytokens.cpp | 7 ++---- 11 files changed, 60 insertions(+), 38 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 5dc72cda0..dd868d033 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -76,7 +76,7 @@ void CheckClass::createSymbolDatabase() // only create variable list and base list if not namespace if (!new_info->isNamespace) { - new_info->getVarList(); + new_info->getVarList(_settings->debugwarnings); // goto initial '{' tok2 = initBaseInfo(new_info, tok); @@ -417,7 +417,7 @@ const Token *CheckClass::initBaseInfo(SpaceInfo *info, const Token *tok) return tok2; } -void CheckClass::SpaceInfo::getVarList() +void CheckClass::SpaceInfo::getVarList(bool debugwarnings) { // Get variable list.. const Token *tok1 = classDef; @@ -629,9 +629,9 @@ void CheckClass::SpaceInfo::getVarList() // If the vartok was set in the if-blocks above, create a entry for this variable.. if (vartok && vartok->str() != "operator") { - if (vartok->varId() == 0) + if (vartok->varId() == 0 && debugwarnings) { - check->reportError(vartok, Severity::error, "cppcheckError", "Internal error. CheckClass::SpaceInfo::getVarList found variable \'" + vartok->str() + "\' with varid 0."); + check->reportError(vartok, Severity::debug, "debug", "CheckClass::SpaceInfo::getVarList found variable \'" + vartok->str() + "\' with varid 0."); } varlist.push_back(Var(vartok, false, varaccess, isMutable, isStatic, isClass)); diff --git a/lib/checkclass.h b/lib/checkclass.h index d1d3e2653..263ee1697 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -241,7 +241,7 @@ private: void markAllVar(bool value); /** @brief initialize varlist */ - void getVarList(); + void getVarList(bool debugwarnings); /** * @brief parse a scope for a constructor or member function and set the "init" flags in the provided varlist diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 13b2ea25a..5e0db0211 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2153,7 +2153,7 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string simplifycode(tok); - if (_settings->_debug && _settings->_verbose) + if (_settings->debug && _settings->_verbose) { tok->printOut(("Checkmemoryleak: simplifycode result for: " + varname).c_str()); } @@ -2183,7 +2183,7 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string } // detect cases that "simplifycode" don't handle well.. - else if (_settings->_debug) + else if (_settings->debugwarnings) { Token *first = tok; while (first && first->str() == ";") @@ -2204,10 +2204,11 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string // Unhandled case.. if (! noerr) { - std::cout << "Token listing..\n "; + std::ostringstream errmsg; + errmsg << "inconclusive leak: "; for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) - std::cout << " " << tok2->str(); - std::cout << "\n"; + errmsg << " " << tok2->str(); + reportError(_tokenizer->tokens(), Severity::debug, "debug", errmsg.str()); } } diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 5439c3d7b..35ba613ee 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -246,7 +246,11 @@ bool CppCheck::parseFromArgs(int argc, const char* const argv[]) // Flag used for various purposes during debugging else if (strcmp(argv[i], "--debug") == 0) - _settings._debug = true; + _settings.debug = _settings.debugwarnings = true; + + // Show debug warnings + else if (strcmp(argv[i], "--debug-warnings") == 0) + _settings.debugwarnings = true; // Inconclusive checking - keep this for compatibility but don't // handle it diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 0ff3bd059..33479eb89 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1030,8 +1030,12 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const if (unhandled) { // unhandled ifdef configuration.. - if (_errorLogger && _settings && _settings->_debug) - _errorLogger->reportOut("unhandled configuration: " + *it); + if (_errorLogger && _settings && _settings->debugwarnings) + { + std::list locationList; + const ErrorLogger::ErrorMessage errmsg(locationList, Severity::debug, "unhandled configuration: " + *it, "debug"); + _errorLogger->reportErr(errmsg); + } ret.erase(it++); } diff --git a/lib/settings.cpp b/lib/settings.cpp index 068e1a6ef..9d2b113f4 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -28,7 +28,7 @@ Settings::Settings() { - _debug = false; + debug = debugwarnings = false; _checkCodingStyle = false; _errorsOnly = false; _inlineSuppressions = false; diff --git a/lib/settings.h b/lib/settings.h index c9310a97c..7dbcd5ff9 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -49,7 +49,10 @@ public: Settings(); /** @brief Is --debug given? */ - bool _debug; + bool debug; + + /** @brief Is --debug-warnings given? */ + bool debugwarnings; /** @brief Inconclusive checks - for debugging of Cppcheck */ bool inconclusive; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a921d4abf..6eeb7e3e5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -637,7 +637,15 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name) void Tokenizer::unsupportedTypedef(const Token *tok) const { -#ifndef NDEBUG +// ###### The ifdef will be removed soon - the message will only be shown if --debug-warnings are given. ####### +#ifdef NDEBUG + if (!_settings) + return; + + if (!_settings->debugwarnings) + return; +#endif + std::ostringstream str; const Token *tok1 = tok; while (tok && tok->str() != ";") @@ -656,15 +664,14 @@ void Tokenizer::unsupportedTypedef(const Token *tok) const locationList.push_back(loc); const ErrorLogger::ErrorMessage errmsg(locationList, - Severity::error, + Severity::debug, "Failed to parse \'" + str.str() + "\'. The checking continues anyway.", - "cppcheckError"); + "debug"); if (_errorLogger) _errorLogger->reportErr(errmsg); else Check::reportError(errmsg); -#endif } struct SpaceInfo @@ -2405,19 +2412,25 @@ void Tokenizer::simplifyTemplates() namepos = 3; else { -#ifndef NDEBUG // debug message that we bail out.. - if (_settings && _settings->_debug) + if (_settings && _settings->debugwarnings) { - std::cout << "simplifyTemplates debug-information: bailing out: " - << file(tok->tokAt(namepos)) - << ": " - << tok->tokAt(namepos)->linenr() - << ": " - << tok->tokAt(namepos)->str() - << std::endl; + std::list locationList; + ErrorLogger::ErrorMessage::FileLocation loc; + loc.line = tok->linenr(); + loc.setfile(file(tok)); + locationList.push_back(loc); + + const ErrorLogger::ErrorMessage errmsg(locationList, + Severity::debug, + "simplifyTemplates: bailing out", + "debug"); + + if (_errorLogger) + _errorLogger->reportErr(errmsg); + else + Check::reportError(errmsg); } -#endif continue; } if ((tok->tokAt(namepos)->str() == "*" || tok->tokAt(namepos)->str() == "&")) @@ -3910,7 +3923,7 @@ bool Tokenizer::simplifyTokenList() removeRedundantAssignment(); simplifyComma(); - if (_settings && _settings->_debug) + if (_settings && _settings->debug) { _tokens->printOut(0, _files); } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 4d9e91c98..8b0e8434f 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -741,7 +741,7 @@ private: { // Tokenize.. Settings settings; - settings._debug = true; + settings.debug = settings.debugwarnings = true; Tokenizer tokenizer(&settings, NULL); std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index cf1546369..5dcee6329 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -1082,7 +1082,7 @@ private: std::istringstream istr(filedata); std::map actual; Settings settings; - settings._debug = true; + settings.debug = settings.debugwarnings = true; settings._verbose = true; Preprocessor preprocessor(&settings, this); preprocessor.preprocess(istr, actual, "file.c"); @@ -1092,7 +1092,7 @@ private: ASSERT_EQUALS("\n\n\n", actual[""]); // the "defined(DEF_10) || defined(DEF_11)" are not handled correctly.. - ASSERT_EQUALS("unhandled configuration: defined(DEF_10)||defined(DEF_11)\n", output.str()); + ASSERT_EQUALS("(debug) unhandled configuration: defined(DEF_10)||defined(DEF_11)\n", errout.str()); TODO_ASSERT_EQUALS(2, actual.size()); TODO_ASSERT_EQUALS("\na1;\n\n", actual["DEF_10"]); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 76b35200f..d65dd32fc 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -3704,6 +3704,7 @@ private: Settings settings; settings.inconclusive = true; settings._checkCodingStyle = true; + settings.debugwarnings = true; // show warnings about unhandled typedef Tokenizer tokenizer(&settings, this); std::istringstream istr(code); errout.str(""); @@ -4236,11 +4237,7 @@ private: // this is invalid C so just make sure it doesn't crash checkSimplifyTypedef(code); -#ifndef NDEBUG - ASSERT_EQUALS("[test.cpp:1]: (error) Failed to parse 'typedef int ( * int ( * ) ( ) ) ( ) ;'. The checking continues anyway.\n", errout.str()); -#else - ASSERT_EQUALS("", errout.str()); -#endif + ASSERT_EQUALS("[test.cpp:1]: (debug) Failed to parse 'typedef int ( * int ( * ) ( ) ) ( ) ;'. The checking continues anyway.\n", errout.str()); } {