diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d5f967ea9..162eb4d25 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index b1b27f46c..ef4921ab5 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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"