diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a90489e94..26be867a0 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5805,6 +5805,18 @@ bool Tokenizer::simplifyKnownVariables() const Token * const vartok = (tok->next() && tok->next()->str() == "const") ? tok->tokAt(2) : tok->next(); const Token * const valuetok = vartok->tokAt(2); if (Token::Match(valuetok, "%bool%|%char%|%num%|%str% ;")) { + //check if there's not a reference usage inside the code + bool withreference = false; + for (const Token *tok2 = valuetok->tokAt(2); tok2; tok2 = tok2->next()) { + if (Token::Match(tok2,"(|[|,|{|=|return|%op% & %varid%", vartok->varId())) { + withreference = true; + break; + } + } + //don't simplify 'f(&x)' to 'f(&100)' + if (withreference) + continue; + constantValues[vartok->varId()] = valuetok->str(); // remove statement diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index aeecc2680..ad21596b8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2518,6 +2518,26 @@ private: "}"; ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } + { + //don't simplify '&x'! + const char code[] = "const char * foo ( ) {\n" + "const char x1 = 'b' ;\n" + "f ( & x1 ) ;\n" + "const char x2 = 'b' ;\n" + "f ( y , & x2 ) ;\n" + "const char x3 = 'b' ;\n" + "t = & x3 ;\n" + "const char x4 = 'b' ;\n" + "t = y + & x4 ;\n" + "const char x5 = 'b' ;\n" + "z [ & x5 ] = y ;\n" + "const char x6 = 'b' ;\n" + "v = { & x6 } ;\n" + "const char x7 = 'b' ;\n" + "return & x7 ;\n" + "}"; + ASSERT_EQUALS(code, tokenizeAndStringify(code, true)); + } } void simplifyKnownVariablesIfEq1() {