Fixed #8054 (Tokenizer::simplifyKnownVariables(): Wrong simplification for global variables)
This commit is contained in:
parent
6c2002a150
commit
6417be4a71
|
@ -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> floatvars;
|
||||||
std::set<unsigned int> arrays;
|
std::set<unsigned int> arrays;
|
||||||
|
|
||||||
|
@ -6253,20 +6254,33 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
if (!start)
|
if (!start)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
for (const Token *tok2 = start->previous(); tok2 && !Token::Match(tok2, "[;{}]"); tok2 = tok2->previous()) {
|
||||||
|
if (tok2->varId() != 0)
|
||||||
|
localvars.insert(tok2->varId());
|
||||||
|
}
|
||||||
|
|
||||||
tok = start;
|
tok = start;
|
||||||
// parse the block of code..
|
// parse the block of code..
|
||||||
int indentlevel = 0;
|
int indentlevel = 0;
|
||||||
Token *tok2 = tok;
|
Token *tok2 = tok;
|
||||||
for (; tok2; tok2 = tok2->next()) {
|
for (; tok2; tok2 = tok2->next()) {
|
||||||
if (Token::Match(tok2, "[;{}] float|double %name% ;")) {
|
if (Token::Match(tok2, "[;{}] %type% %name%|*")) {
|
||||||
floatvars.insert(tok2->tokAt(2)->varId());
|
bool isfloat = false;
|
||||||
}
|
bool ispointer = false;
|
||||||
|
const Token *vartok = tok2->next();
|
||||||
if (Token::Match(tok2, "[;{}] %type% *| %name% [")) {
|
while (Token::Match(vartok, "%name%|* %name%|*")) {
|
||||||
const Token *nameToken = tok2->tokAt(2);
|
if (Token::Match(vartok, "float|double"))
|
||||||
if (nameToken->str() == "*")
|
isfloat = true;
|
||||||
nameToken = nameToken->next();
|
if (vartok->str() == "*")
|
||||||
arrays.insert(nameToken->varId());
|
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() == "{")
|
if (tok2->str() == "{")
|
||||||
|
@ -6290,6 +6304,9 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
if (varid == 0)
|
if (varid == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if (Token::Match(tok2->previous(), "[;{}]") && localvars.find(varid) == localvars.end())
|
||||||
|
continue;
|
||||||
|
|
||||||
// initialization of static variable => the value is not *known*
|
// initialization of static variable => the value is not *known*
|
||||||
{
|
{
|
||||||
bool isstatic = false;
|
bool isstatic = false;
|
||||||
|
|
|
@ -203,6 +203,7 @@ private:
|
||||||
TEST_CASE(simplifyKnownVariablesFloat); // #2454 - float variable
|
TEST_CASE(simplifyKnownVariablesFloat); // #2454 - float variable
|
||||||
TEST_CASE(simplifyKnownVariablesClassMember); // #2815 - value of class member may be changed by function call
|
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(simplifyKnownVariablesFunctionCalls); // Function calls (don't assume pass by reference)
|
||||||
|
TEST_CASE(simplifyKnownVariablesGlobalVars);
|
||||||
TEST_CASE(simplifyKnownVariablesReturn); // 3500 - return
|
TEST_CASE(simplifyKnownVariablesReturn); // 3500 - return
|
||||||
TEST_CASE(simplifyKnownVariablesPointerAliasFunctionCall); // #7440
|
TEST_CASE(simplifyKnownVariablesPointerAliasFunctionCall); // #7440
|
||||||
TEST_CASE(simplifyExternC);
|
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() {
|
void simplifyKnownVariablesReturn() {
|
||||||
const char code[] = "int a() {"
|
const char code[] = "int a() {"
|
||||||
" int x = 123;"
|
" int x = 123;"
|
||||||
|
|
Loading…
Reference in New Issue