Fixed #8054 (Tokenizer::simplifyKnownVariables(): Wrong simplification for global variables)

This commit is contained in:
Daniel Marjamäki 2017-07-08 22:12:01 +02:00
parent 6c2002a150
commit 6417be4a71
2 changed files with 38 additions and 10 deletions

View File

@ -6242,7 +6242,8 @@ bool Tokenizer::simplifyKnownVariables()
}
}
// variable id for float/double variables
// variable id for local, float/double, array variables
std::set<unsigned int> localvars;
std::set<unsigned int> floatvars;
std::set<unsigned int> arrays;
@ -6253,20 +6254,33 @@ bool Tokenizer::simplifyKnownVariables()
if (!start)
continue;
for (const Token *tok2 = start->previous(); tok2 && !Token::Match(tok2, "[;{}]"); tok2 = tok2->previous()) {
if (tok2->varId() != 0)
localvars.insert(tok2->varId());
}
tok = start;
// parse the block of code..
int indentlevel = 0;
Token *tok2 = tok;
for (; tok2; tok2 = tok2->next()) {
if (Token::Match(tok2, "[;{}] float|double %name% ;")) {
floatvars.insert(tok2->tokAt(2)->varId());
}
if (Token::Match(tok2, "[;{}] %type% *| %name% [")) {
const Token *nameToken = tok2->tokAt(2);
if (nameToken->str() == "*")
nameToken = nameToken->next();
arrays.insert(nameToken->varId());
if (Token::Match(tok2, "[;{}] %type% %name%|*")) {
bool isfloat = false;
bool ispointer = false;
const Token *vartok = tok2->next();
while (Token::Match(vartok, "%name%|* %name%|*")) {
if (Token::Match(vartok, "float|double"))
isfloat = true;
if (vartok->str() == "*")
ispointer = true;
vartok = vartok->next();
}
if (Token::Match(vartok, "%var% ;|["))
localvars.insert(vartok->varId());
if (isfloat && !ispointer && Token::Match(vartok, "%var% ;"))
floatvars.insert(vartok->varId());
if (Token::Match(vartok, "%var% ["))
arrays.insert(vartok->varId());
}
if (tok2->str() == "{")
@ -6290,6 +6304,9 @@ bool Tokenizer::simplifyKnownVariables()
if (varid == 0)
continue;
if (Token::Match(tok2->previous(), "[;{}]") && localvars.find(varid) == localvars.end())
continue;
// initialization of static variable => the value is not *known*
{
bool isstatic = false;

View File

@ -203,6 +203,7 @@ private:
TEST_CASE(simplifyKnownVariablesFloat); // #2454 - float variable
TEST_CASE(simplifyKnownVariablesClassMember); // #2815 - value of class member may be changed by function call
TEST_CASE(simplifyKnownVariablesFunctionCalls); // Function calls (don't assume pass by reference)
TEST_CASE(simplifyKnownVariablesGlobalVars);
TEST_CASE(simplifyKnownVariablesReturn); // 3500 - return
TEST_CASE(simplifyKnownVariablesPointerAliasFunctionCall); // #7440
TEST_CASE(simplifyExternC);
@ -2949,6 +2950,16 @@ private:
}
}
void simplifyKnownVariablesGlobalVars() {
// #8054
const char code[] = "static int x;"
"void f() {"
" x = 123;"
" while (!x) { dostuff(); }"
"}";
ASSERT_EQUALS("static int x ; void f ( ) { x = 123 ; while ( ! x ) { dostuff ( ) ; } }", tokenizeAndStringify(code,true));
}
void simplifyKnownVariablesReturn() {
const char code[] = "int a() {"
" int x = 123;"