diff --git a/src/tokenize.cpp b/src/tokenize.cpp index b66c0b3e1..a061e3fac 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -3167,12 +3167,16 @@ bool Tokenizer::simplifyKnownVariables() else if (tok2->previous()->str() != "*" && (Token::Match(tok2, "%var% = %num% ;") || - Token::Match(tok2, "%var% = %bool% ;"))) + Token::Match(tok2, "%var% = %str% ;") || + Token::Match(tok2, "%var% = %bool% ;") || + Token::Match(tok2, "%var% = %var% ;"))) { unsigned int varid = tok2->varId(); if (varid == 0) continue; + const bool pointeralias(tok2->tokAt(2)->isName()); + std::string value(tok2->strAt(2)); Token* bailOutFromLoop = 0; int indentlevel3 = indentlevel; // indentlevel for tok3 @@ -3189,6 +3193,9 @@ bool Tokenizer::simplifyKnownVariables() break; } + if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str())) + break; + // Stop if something like 'while (--var)' is found if (tok3->str() == "while" || tok3->str() == "do") { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 9fc7d33c3..c791668db 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -127,6 +127,8 @@ private: TEST_CASE(simplifyTypedef5) TEST_CASE(reverseArraySyntax) TEST_CASE(simplify_numeric_condition); + + TEST_CASE(pointeralias); } std::string tok(const char code[], bool simplify = true) @@ -1978,6 +1980,46 @@ private: ASSERT_EQUALS("void f ( ) { { ; } }", tok(code)); } } + + + void pointeralias() + { + { + const char code[] = "void f()\n" + "{\n" + " char buf[100];\n" + " char *p = buf;\n" + " x(p);\n" + "}\n"; + + const char expected[] = "void f ( ) " + "{ " + "char buf [ 100 ] ; " + "char * p ; p = buf ; " + "x ( buf ) ; " + "}"; + + ASSERT_EQUALS(expected, tok(code)); + } + + { + const char code[] = "void f(char *p1)\n" + "{\n" + " char *p = p1;\n" + " p1 = 0;" + " x(p);\n" + "}\n"; + + const char expected[] = "void f ( char * p1 ) " + "{ " + "char * p ; p = p1 ; " + "p1 = 0 ; " + "x ( p ) ; " + "}"; + + ASSERT_EQUALS(expected, tok(code)); + } + } }; REGISTER_TEST(TestSimplifyTokens)