diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 99c811f61..608f05dc0 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6757,7 +6757,8 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign tok3 = tok3->previous(); tok3->deleteThis(); ret = true; - } + } else if (Token::Match(valueToken, "& %name% ;")) + tok3->insertToken("&", true); } if (Token::simpleMatch(tok3, "= {")) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index cac367d52..f13ce0c61 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -181,6 +181,7 @@ private: TEST_CASE(simplifyKnownVariables57); // ticket #4724 TEST_CASE(simplifyKnownVariables58); // ticket #5268 TEST_CASE(simplifyKnownVariables59); // skip for header + TEST_CASE(simplifyKnownVariables60); // #6829 TEST_CASE(simplifyKnownVariablesBailOutAssign1); TEST_CASE(simplifyKnownVariablesBailOutAssign2); TEST_CASE(simplifyKnownVariablesBailOutAssign3); // #4395 - nested assignments @@ -2675,6 +2676,21 @@ private: "}", tokenizeAndStringify(code, true)); } + void simplifyKnownVariables60() { // #6829 + const char code[] = "void f() {\n" + " int i = 1;\n" + " const int * const constPtrToConst = &i;\n" + " std::cout << *constPtrToConst << std::endl;\n" + " std::cout << constPtrToConst << std::endl;\n" + "}"; + ASSERT_EQUALS("void f ( ) {\n" + "int i ; i = 1 ;\n" + "const int * constPtrToConst ; constPtrToConst = & i ;\n" + "std :: cout << i << std :: endl ;\n" + "std :: cout << & i << std :: endl ;\n" + "}", tokenizeAndStringify(code, true)); + } + void simplifyKnownVariablesBailOutAssign1() { const char code[] = "int foo() {\n" " int i; i = 0;\n"