From 96ae010e48d30c18ab27035f53ac21763eb3851c Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 21 Jan 2012 10:08:09 +0100 Subject: [PATCH] - Correctly set Scope::function variable in symboldatabase - Refactorizations - Fixed some cppcheck warnings --- cli/cmdlineparser.cpp | 4 ++-- cli/cmdlineparser.h | 4 ++-- lib/checkclass.cpp | 11 ++--------- lib/checkother.cpp | 4 ++-- lib/checkunusedvar.cpp | 2 +- lib/symboldatabase.cpp | 8 ++++++-- lib/templatesimplifier.cpp | 2 +- test/testcppcheck.cpp | 3 --- test/testsuite.h | 1 - test/testsymboldatabase.cpp | 8 ++++++++ 10 files changed, 24 insertions(+), 23 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index d9e1e805f..f395ddd3d 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -90,7 +90,7 @@ CmdLineParser::CmdLineParser(Settings *settings) { } -void CmdLineParser::PrintMessage(const std::string &message) +void CmdLineParser::PrintMessage(const std::string &message) const { std::cout << message << std::endl; } @@ -682,7 +682,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) return true; } -void CmdLineParser::PrintHelp() +void CmdLineParser::PrintHelp() const { std::cout << "Cppcheck - A tool for static C/C++ code analysis\n" "\n" diff --git a/cli/cmdlineparser.h b/cli/cmdlineparser.h index 6e080b4c0..e17bd1dce 100644 --- a/cli/cmdlineparser.h +++ b/cli/cmdlineparser.h @@ -98,12 +98,12 @@ protected: /** * Print help text to the console. */ - void PrintHelp(); + void PrintHelp() const; /** * Print message (to console?). */ - void PrintMessage(const std::string &message); + void PrintMessage(const std::string &message) const; private: Settings *_settings; diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 45bed0809..a741c64c8 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -637,16 +637,9 @@ void CheckClass::noMemset() std::list::const_iterator scope; for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { - std::list::const_iterator func; - - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { - // only check functions with bodies - if (!func->hasBody) - continue; - + if (scope->type == Scope::eFunction) { // Locate all 'memset' tokens.. - const Token *end = func->start->link(); - for (const Token *tok = func->start; tok && tok != end; tok = tok->next()) { + for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) { if (!Token::Match(tok, "memset|memcpy|memmove")) continue; diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 148a3766b..17fd51e68 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1922,8 +1922,8 @@ void CheckOther::checkConstantFunctionParameter() const SymbolDatabase * const symbolDatabase = _tokenizer->getSymbolDatabase(); for (std::list::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { - for (std::list::const_iterator j = i->functionList.begin(); j != i->functionList.end(); ++j) { - for (const Token* tok = j->arg->next(); tok; tok = tok->nextArgument()) { + if (i->type == Scope::eFunction && i->function) { + for (const Token* tok = i->function->arg->next(); tok; tok = tok->nextArgument()) { // TODO: False negatives. This pattern only checks for string. // Investigate if there are other classes in the std // namespace and add them to the pattern. There are diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 0a4008be9..f28b60ec5 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -81,7 +81,7 @@ public: void clear() { _varUsage.clear(); } - const VariableMap &varUsage() { + const VariableMap &varUsage() const { return _varUsage; } void addVar(const Token *name, VariableType type, const Scope *scope, bool write_); diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 7de7462ce..c9c6cb2f5 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -410,8 +410,10 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti addNewFunction(&scope, &tok); - if (scope) + if (scope) { old_scope->functionList.push_back(function); + scope->function = &old_scope->functionList.back(); + } } // syntax error @@ -461,8 +463,10 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti addNewFunction(&scope, &tok1); - if (scope) + if (scope) { old_scope->functionList.push_back(function); + scope->function = &old_scope->functionList.back(); + } } // syntax error? diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 82a1696c8..96395de3a 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -917,7 +917,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) tok->str(MathLib::calculate(tok->str(), tok->strAt(2), tok->next()->str()[0])); } catch (InternalError &e) { e.token = tok; - throw e; + throw; } } diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index 95f590f8c..90c8f3e9b 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -53,9 +53,6 @@ private: void reportErr(const ErrorLogger::ErrorMessage &msg) { id.push_back(msg._id); } - - void reportStatus(unsigned int /*fileindex*/, unsigned int /*filecount*/, long /*sizedone*/, long /*sizetotal*/) { - } }; void run() { diff --git a/test/testsuite.h b/test/testsuite.h index e0c9032e6..05ac0635e 100644 --- a/test/testsuite.h +++ b/test/testsuite.h @@ -61,7 +61,6 @@ protected: public: virtual void reportOut(const std::string &outmsg); virtual void reportErr(const ErrorLogger::ErrorMessage &msg); - virtual void reportStatus(unsigned int /*fileindex*/, unsigned int /*filecount*/, long /*sizedone*/, long /*sizetotal*/) {} void run(const std::string &str); TestFixture(const std::string &_name); diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 8e8e3a3da..72d6f1b5f 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -677,6 +677,10 @@ private: } } + ASSERT(scope != 0); + if (!scope) + return; + ASSERT_EQUALS("X", scope->className); // The class has a constructor but the implementation _is not_ seen @@ -707,6 +711,10 @@ private: } } + ASSERT(scope != 0); + if (!scope) + return; + ASSERT_EQUALS("X", scope->className); // The class has a constructor and the implementation _is_ seen