diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c01a765b4..2135a42a3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6029,6 +6029,19 @@ bool Tokenizer::simplifyKnownVariables() if (Token::Match(tok3->next(), ". %var% (")) break; + // suppress debug-warning when assignment + if (Token::simpleMatch(tok3->next(), "=")) + break; + + // taking address of variable.. + if (Token::Match(tok3->tokAt(-2), "return|= & %var% ;")) + break; + + // parameter in function call.. + if (Token::Match(tok3->tokAt(-2), "%var% ( %var% ,|)") || + Token::Match(tok3->previous(), ", %var% ,|)")) + break; + std::list locationList; ErrorLogger::ErrorMessage::FileLocation loc; loc.line = tok3->linenr(); diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 1f6acf903..8efb061f3 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -38,7 +38,9 @@ private: void check(const char code[]) { // Tokenize.. - Tokenizer tokenizer; + Settings settings; + settings.debugwarnings = true; + Tokenizer tokenizer(&settings, 0); std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); tokenizer.simplifyTokenList(); @@ -53,7 +55,6 @@ private: errout.str(""); // Check auto variables - Settings settings; CheckAutoVariables checkAutoVariables(&tokenizer, &settings, this); checkAutoVariables.autoVariables(); checkAutoVariables.returnPointerToLocalArray(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index cc8eb7aee..4831b7799 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -123,6 +123,7 @@ private: TEST_CASE(simplifyKnownVariables30); TEST_CASE(simplifyKnownVariables31); TEST_CASE(simplifyKnownVariables32); // const + TEST_CASE(simplifyKnownVariablesBailOutAssign); TEST_CASE(simplifyKnownVariablesBailOutFor1); TEST_CASE(simplifyKnownVariablesBailOutFor2); TEST_CASE(simplifyKnownVariablesBailOutMemberFunction); @@ -299,8 +300,12 @@ private: std::string tokenizeAndStringify(const char code[], bool simplify = false) { + errout.str(""); + // tokenize.. - Tokenizer tokenizer(0, this); + Settings settings; + settings.debugwarnings = true; + Tokenizer tokenizer(&settings, this); std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); if (simplify) @@ -1871,6 +1876,21 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } + void simplifyKnownVariablesBailOutAssign() + { + const char code[] = "int foo() {\n" + " int i; i = 0;\n" + " if (x) { i = 10; }\n" + " return i;\n" + "}\n"; + const char expected[] = "int foo ( ) {\n" + "int i ; i = 0 ;\n" + "if ( x ) { i = 10 ; }\n" + "return i ;\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + void simplifyKnownVariablesBailOutFor1() { const char code[] = "void foo() {\n"