diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9c9badd04..1934f4529 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6709,8 +6709,20 @@ bool Tokenizer::simplifyKnownVariables() std::map constantValues; for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok, "static| const static| %type% %var% = %any% ;")) + if (tok->isName() && Token::Match(tok, "static| const| static| %type% const| %var% = %any% ;")) { + bool isconst = false; + for (const Token *tok2 = tok; tok2->str() != "="; tok2 = tok2->next()) + { + if (tok2->str() == "const") + { + isconst = true; + break; + } + } + if (!isconst) + continue; + Token *tok1 = tok; // start of statement @@ -6723,8 +6735,8 @@ bool Tokenizer::simplifyKnownVariables() if (!tok->isStandardType()) continue; - const Token * const vartok = tok->next(); - const Token * const valuetok = tok->tokAt(3); + const Token * const vartok = (tok->strAt(1) == "const") ? tok->tokAt(2) : tok->next(); + const Token * const valuetok = vartok->tokAt(2); if (valuetok->isNumber() || Token::Match(valuetok, "%str% ;")) { constantValues[vartok->varId()] = valuetok->str(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7557f396d..0a4110712 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -1957,15 +1957,19 @@ private: void simplifyKnownVariables32() { - const char code[] = "void foo() {\n" - " const int x = 0;\n" - " bar(0,x);\n" - "}\n"; - const char expected[] = "void foo ( ) {\n" - ";\n" - "bar ( 0 , 0 ) ;\n" - "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + { + const char code[] = "void foo() {\n" + " const int x = 0;\n" + " bar(0,x);\n" + "}\n"; + const char expected[] = "void foo ( ) {\n;\nbar ( 0 , 0 ) ;\n}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + + { + const char code[] = "static int const SZ = 22; char str[SZ];\n"; + ASSERT_EQUALS("; char str [ 22 ] ;", tokenizeAndStringify(code,true)); + } } void simplifyKnownVariables33()