From a96028b43bfe45ac186278e683ae89293d7af38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 31 Aug 2011 19:45:20 +0200 Subject: [PATCH] Fixed #3058 (False positive: Uninitialized variable: data) --- lib/tokenize.cpp | 27 +++++++++++++++++++++++++-- test/testtokenize.cpp | 30 ++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 815a79570..d5f1ad837 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7075,14 +7075,37 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign // Stop if there is a pointer alias and a shadow variable is // declared in an inner scope (#3058) if (valueIsPointer && tok3->varId() > 0 && - tok3->previous() && tok3->previous()->isName() && + tok3->previous() && (tok3->previous()->isName() || tok3->previous()->str() == "*") && valueToken->str() == "&" && valueToken->next() && valueToken->next()->isName() && tok3->str() == valueToken->next()->str() && tok3->varId() > valueToken->next()->varId()) { - break; + // more checking if this is a variable declaration + bool decl = true; + for (const Token *tok4 = tok3->previous(); tok4; tok4 = tok4->previous()) + { + if (Token::Match(tok4, "[;{}]")) + break; + + else if (tok4->isName()) + { + if (tok4->varId() > 0) + { + decl = false; + break; + } + } + + else if (!Token::Match(tok4, "[&*]")) + { + decl = false; + break; + } + } + if (decl) + break; } // Stop if label is found diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 4c43840cf..f14310ce4 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2162,15 +2162,29 @@ private: void simplifyKnownVariables43() { - const char code[] = "void f() {\n" - " int a, *p; p = &a;\n" - " { int a = *p; }\n" - "}"; - const char expected[] = "void f ( ) {\n" - "int a ; int * p ; p = & a ;\n" - "{ int a ; a = * p ; }\n" + { + const char code[] = "void f() {\n" + " int a, *p; p = &a;\n" + " { int a = *p; }\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + const char expected[] = "void f ( ) {\n" + "int a ; int * p ; p = & a ;\n" + "{ int a ; a = * p ; }\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + + { + const char code[] = "void f() {\n" + " int *a, **p; p = &a;\n" + " { int *a = *p; }\n" + "}"; + const char expected[] = "void f ( ) {\n" + "int * a ; int * * p ; p = & a ;\n" + "{ int * a ; a = * p ; }\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } } void simplifyKnownVariablesBailOutAssign1()