From 7bf0622ae09b2829dc7c304b2b251d1fa00d4c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 25 Dec 2009 20:12:06 +0100 Subject: [PATCH] ExecutionPath: Refactoring the interface --- lib/checkmemoryleak.cpp | 35 ++++------------------------------ lib/checkother.cpp | 42 ++++++++--------------------------------- lib/executionpath.cpp | 40 ++++++++++++++++++++++++++++++++++++--- lib/executionpath.h | 6 +++++- 4 files changed, 54 insertions(+), 69 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 45fccd802..b4382744d 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2653,9 +2653,8 @@ private: return &tok; } -public: /** going out of scope - all execution paths end */ - static void end(const std::list &checks, const Token *tok) + void end(const std::list &checks, const Token *tok) const { bool foundError = false; ret(checks, tok, foundError); @@ -2665,34 +2664,8 @@ public: void CheckMemoryLeakInFunction::localleaks() { - for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) - { - if (tok->str() != ")") - continue; - - // Start of implementation.. - if (Token::Match(tok, ") const| {")) - { - // goto the "{" - tok = tok->next(); - if (tok->str() == "const") - tok = tok->next(); - - // Check this scope.. - std::list checks; - checks.push_back(new CheckLocalLeaks(this)); - checkExecutionPaths(tok->next(), checks); - CheckLocalLeaks::end(checks, tok->link()); - while (!checks.empty()) - { - delete checks.back(); - checks.pop_back(); - } - - // skip this scope - it has been checked - tok = tok->link(); - } - } - + // Check this scope.. + CheckLocalLeaks c(this); + checkExecutionPaths(_tokenizer->tokens(), &c); } diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f82fea122..2210dda5e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1630,42 +1630,16 @@ private: void CheckOther::executionPaths() { - // start of function: ") const| {" - for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) + // Check for null pointer errors.. { - if (tok->str() != ")") - continue; - if (!Token::Match(tok, ") const| {")) - continue; - while (tok->str() != "{") - tok = tok->next(); + CheckNullpointer c(this); + checkExecutionPaths(_tokenizer->tokens(), &c); + } - // check for null pointer errors.. - { - std::list checks; - checks.push_back(new CheckNullpointer(this)); - checkExecutionPaths(tok->next(), checks); - while (!checks.empty()) - { - delete checks.back(); - checks.pop_back(); - } - } - - // check if variable is accessed uninitialized.. - { - std::list checks; - checks.push_back(new CheckUninitVar(this)); - checkExecutionPaths(tok->next(), checks); - while (!checks.empty()) - { - delete checks.back(); - checks.pop_back(); - } - } - - // skip function body.. - tok = tok->link(); + // check if variable is accessed uninitialized.. + { + CheckUninitVar c(this); + checkExecutionPaths(_tokenizer->tokens(), &c); } } diff --git a/lib/executionpath.cpp b/lib/executionpath.cpp index e2072c560..26b1f9f3d 100644 --- a/lib/executionpath.cpp +++ b/lib/executionpath.cpp @@ -48,7 +48,7 @@ bool ExecutionPath::parseCondition(const Token &tok, std::list } -const Token *checkExecutionPaths(const Token *tok, std::list &checks) +static const Token *checkExecutionPaths_(const Token *tok, std::list &checks) { const std::auto_ptr check(checks.front()->copy()); @@ -118,7 +118,7 @@ const Token *checkExecutionPaths(const Token *tok, std::list &c std::list::iterator it; for (it = checks.begin(); it != checks.end(); ++it) c.push_back((*it)->copy()); - const Token *tokerr = checkExecutionPaths(tok->next(), c); + const Token *tokerr = checkExecutionPaths_(tok->next(), c); if (tokerr) return tokerr; while (!c.empty()) @@ -141,7 +141,7 @@ const Token *checkExecutionPaths(const Token *tok, std::list &c continue; // there is no "if".. - const Token *tokerr = checkExecutionPaths(tok->next(), checks); + const Token *tokerr = checkExecutionPaths_(tok->next(), checks); if (tokerr) return tokerr; @@ -187,3 +187,37 @@ const Token *checkExecutionPaths(const Token *tok, std::list &c return 0; } + +void checkExecutionPaths(const Token *tok, ExecutionPath *c) +{ + for (; tok; tok = tok->next()) + { + if (tok->str() != ")") + continue; + + // Start of implementation.. + if (Token::Match(tok, ") const| {")) + { + // goto the "{" + tok = tok->next(); + if (tok->str() == "const") + tok = tok->next(); + + std::list checks; + checks.push_back(c->copy()); + checkExecutionPaths_(tok, checks); + + c->end(checks, tok->link()); + + while (!checks.empty()) + { + delete checks.back(); + checks.pop_back(); + } + + // skip this scope - it has been checked + tok = tok->link(); + } + } +} + diff --git a/lib/executionpath.h b/lib/executionpath.h index c6088e814..1a60ea497 100644 --- a/lib/executionpath.h +++ b/lib/executionpath.h @@ -93,10 +93,14 @@ public: * @return true => bail out all checking **/ virtual bool parseCondition(const Token &tok, std::list &checks) const; + + /** going out of scope - all execution paths end */ + virtual void end(const std::list & /*checks*/, const Token * /*tok*/) const + { } }; -const Token *checkExecutionPaths(const Token *tok, std::list &checks); +void checkExecutionPaths(const Token *tok, ExecutionPath *c); #endif