Fix #276 (simplification: Variable value)
http://apps.sourceforge.net/trac/cppcheck/ticket/276
This commit is contained in:
parent
cb209bbd41
commit
0f59ef9064
|
@ -2099,14 +2099,40 @@ bool Tokenizer::simplifyKnownVariables()
|
|||
continue;
|
||||
|
||||
std::string value(tok2->strAt(2));
|
||||
|
||||
Token* bailOutFromLoop = 0;
|
||||
for (Token *tok3 = tok2->next(); tok3; tok3 = tok3->next())
|
||||
{
|
||||
// Perhaps it's a loop => bail out
|
||||
if (tok3->str() == "{" && Token::Match(tok3->previous(), ")"))
|
||||
if (bailOutFromLoop)
|
||||
{
|
||||
// This could be a loop, skip it, but only if it doesn't contain
|
||||
// the variable we are checking for. If it contains the variable
|
||||
// we will bail out.
|
||||
if (tok3->varId() == varid)
|
||||
{
|
||||
// Continue
|
||||
tok2 = bailOutFromLoop;
|
||||
break;
|
||||
}
|
||||
else if (tok3 == bailOutFromLoop)
|
||||
{
|
||||
// We have skipped the loop
|
||||
bailOutFromLoop = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
else if (tok3->str() == "{" && Token::Match(tok3->previous(), ")"))
|
||||
{
|
||||
// There is a possible loop after the assignment. Try to skip it.
|
||||
bailOutFromLoop = tok3->link();
|
||||
continue;
|
||||
}
|
||||
else if (tok3->str() == "}" && Token::Match(tok3->link()->previous(), ")"))
|
||||
{
|
||||
// Assignment was in the middle of possible loop, bail out.
|
||||
break;
|
||||
}
|
||||
|
||||
// Variable is used somehow in a non-defined pattern => bail out
|
||||
if (tok3->varId() == varid)
|
||||
|
|
|
@ -697,6 +697,31 @@ private:
|
|||
ostr << " " << tok->str();
|
||||
ASSERT_EQUALS(std::string(" void f ( ) { bool b ; b = false ; { b = false ; } { b = true ; } if ( true ) { a ( ) ; } }"), ostr.str());
|
||||
}
|
||||
|
||||
{
|
||||
const char code[] = "void f()\n"
|
||||
"{\n"
|
||||
" int b=0;\n"
|
||||
" b = 1;\n"
|
||||
" for( int i = 0; i < 10; i++ )"
|
||||
" {\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" a(b);\n"
|
||||
"}\n";
|
||||
// tokenize..
|
||||
OurTokenizer tokenizer;
|
||||
std::istringstream istr(code);
|
||||
tokenizer.tokenize(istr, "test.cpp");
|
||||
|
||||
tokenizer.setVarId();
|
||||
tokenizer.simplifyKnownVariables();
|
||||
|
||||
std::ostringstream ostr;
|
||||
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
|
||||
ostr << " " << tok->str();
|
||||
ASSERT_EQUALS(std::string(" void f ( ) { int b ; b = 0 ; b = 1 ; for ( int i = 0 ; i < 10 ; i ++ ) { } a ( 1 ) ; }"), ostr.str());
|
||||
}
|
||||
}
|
||||
|
||||
void multiCompare()
|
||||
|
|
Loading…
Reference in New Issue