Fixed #3117 (Tokenizer::simplifyKnownVariables : Don't simplify static variable that is changed)
This commit is contained in:
parent
d3b27c40fc
commit
085a6285fa
|
@ -6573,6 +6573,21 @@ bool Tokenizer::simplifyKnownVariables()
|
|||
if (varid == 0)
|
||||
continue;
|
||||
|
||||
// initialization of static variable => the value is not *known*
|
||||
{
|
||||
bool isstatic = false;
|
||||
const Token *decl = tok2->previous();
|
||||
while (decl && (decl->isName() || decl->str() == "*")) {
|
||||
if (decl->str() == "static") {
|
||||
isstatic = true;
|
||||
break;
|
||||
}
|
||||
decl = decl->previous();
|
||||
}
|
||||
if (isstatic)
|
||||
continue;
|
||||
}
|
||||
|
||||
// skip loop variable
|
||||
if (Token::Match(tok2->tokAt(-2), "(|:: %type%")) {
|
||||
const Token *tok3 = tok2->previous();
|
||||
|
|
|
@ -135,6 +135,7 @@ private:
|
|||
TEST_CASE(simplifyKnownVariables41); // p=&x; if (p) ..
|
||||
TEST_CASE(simplifyKnownVariables42); // ticket #2031 - known string value after strcpy
|
||||
TEST_CASE(simplifyKnownVariables43);
|
||||
TEST_CASE(simplifyKnownVariables44); // ticket #3117 - don't simplify static variables
|
||||
TEST_CASE(simplifyKnownVariablesBailOutAssign1);
|
||||
TEST_CASE(simplifyKnownVariablesBailOutAssign2);
|
||||
TEST_CASE(simplifyKnownVariablesBailOutFor1);
|
||||
|
@ -2107,6 +2108,18 @@ private:
|
|||
}
|
||||
}
|
||||
|
||||
void simplifyKnownVariables44() {
|
||||
const char code[] = "void a() {\n"
|
||||
" static int i = 10;\n"
|
||||
" b(i++);\n"
|
||||
"}";
|
||||
const char expected[] = "void a ( ) {\n"
|
||||
"static int i = 10 ;\n"
|
||||
"b ( i ++ ) ;\n"
|
||||
"}";
|
||||
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
|
||||
}
|
||||
|
||||
void simplifyKnownVariablesBailOutAssign1() {
|
||||
const char code[] = "int foo() {\n"
|
||||
" int i; i = 0;\n"
|
||||
|
|
Loading…
Reference in New Issue