ExecutionPath: Refactoring the interface

This commit is contained in:
Daniel Marjamäki 2009-12-25 20:12:06 +01:00
parent b5291825ce
commit 7bf0622ae0
4 changed files with 54 additions and 69 deletions

View File

@ -2653,9 +2653,8 @@ private:
return &tok; return &tok;
} }
public:
/** going out of scope - all execution paths end */ /** going out of scope - all execution paths end */
static void end(const std::list<ExecutionPath *> &checks, const Token *tok) void end(const std::list<ExecutionPath *> &checks, const Token *tok) const
{ {
bool foundError = false; bool foundError = false;
ret(checks, tok, foundError); ret(checks, tok, foundError);
@ -2665,34 +2664,8 @@ public:
void CheckMemoryLeakInFunction::localleaks() 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.. // Check this scope..
std::list<ExecutionPath *> checks; CheckLocalLeaks c(this);
checks.push_back(new CheckLocalLeaks(this)); checkExecutionPaths(_tokenizer->tokens(), &c);
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();
}
}
} }

View File

@ -1630,42 +1630,16 @@ private:
void CheckOther::executionPaths() void CheckOther::executionPaths()
{ {
// start of function: ") const| {" // Check for null pointer errors..
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
if (tok->str() != ")") CheckNullpointer c(this);
continue; checkExecutionPaths(_tokenizer->tokens(), &c);
if (!Token::Match(tok, ") const| {"))
continue;
while (tok->str() != "{")
tok = tok->next();
// check for null pointer errors..
{
std::list<ExecutionPath *> 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.. // check if variable is accessed uninitialized..
{ {
std::list<ExecutionPath *> checks; CheckUninitVar c(this);
checks.push_back(new CheckUninitVar(this)); checkExecutionPaths(_tokenizer->tokens(), &c);
checkExecutionPaths(tok->next(), checks);
while (!checks.empty())
{
delete checks.back();
checks.pop_back();
}
}
// skip function body..
tok = tok->link();
} }
} }

View File

@ -48,7 +48,7 @@ bool ExecutionPath::parseCondition(const Token &tok, std::list<ExecutionPath *>
} }
const Token *checkExecutionPaths(const Token *tok, std::list<ExecutionPath *> &checks) static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPath *> &checks)
{ {
const std::auto_ptr<ExecutionPath> check(checks.front()->copy()); const std::auto_ptr<ExecutionPath> check(checks.front()->copy());
@ -118,7 +118,7 @@ const Token *checkExecutionPaths(const Token *tok, std::list<ExecutionPath *> &c
std::list<ExecutionPath *>::iterator it; std::list<ExecutionPath *>::iterator it;
for (it = checks.begin(); it != checks.end(); ++it) for (it = checks.begin(); it != checks.end(); ++it)
c.push_back((*it)->copy()); c.push_back((*it)->copy());
const Token *tokerr = checkExecutionPaths(tok->next(), c); const Token *tokerr = checkExecutionPaths_(tok->next(), c);
if (tokerr) if (tokerr)
return tokerr; return tokerr;
while (!c.empty()) while (!c.empty())
@ -141,7 +141,7 @@ const Token *checkExecutionPaths(const Token *tok, std::list<ExecutionPath *> &c
continue; continue;
// there is no "if".. // there is no "if"..
const Token *tokerr = checkExecutionPaths(tok->next(), checks); const Token *tokerr = checkExecutionPaths_(tok->next(), checks);
if (tokerr) if (tokerr)
return tokerr; return tokerr;
@ -187,3 +187,37 @@ const Token *checkExecutionPaths(const Token *tok, std::list<ExecutionPath *> &c
return 0; 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<ExecutionPath *> 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();
}
}
}

View File

@ -93,10 +93,14 @@ public:
* @return true => bail out all checking * @return true => bail out all checking
**/ **/
virtual bool parseCondition(const Token &tok, std::list<ExecutionPath *> &checks) const; virtual bool parseCondition(const Token &tok, std::list<ExecutionPath *> &checks) const;
/** going out of scope - all execution paths end */
virtual void end(const std::list<ExecutionPath *> & /*checks*/, const Token * /*tok*/) const
{ }
}; };
const Token *checkExecutionPaths(const Token *tok, std::list<ExecutionPath *> &checks); void checkExecutionPaths(const Token *tok, ExecutionPath *c);
#endif #endif