From af4c4cef3491d2a8527937fd7c1a0571565bccd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 5 May 2010 18:29:56 +0200 Subject: [PATCH] Fixed #1646 (False positive: array access after return cannot have default loop value) --- lib/tokenize.cpp | 14 +++++++++ test/testtokenize.cpp | 72 ++++++++++++++++++++++++++++--------------- 2 files changed, 62 insertions(+), 24 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ade40a9a9..b45927123 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4977,6 +4977,7 @@ bool Tokenizer::simplifyKnownVariables() } Token* bailOutFromLoop = 0; int indentlevel3 = indentlevel; // indentlevel for tok3 + bool ret3 = false; for (; tok3; tok3 = tok3->next()) { if (tok3->str() == "{") @@ -5002,6 +5003,19 @@ bool Tokenizer::simplifyKnownVariables() if (Token::Match(tok3, "; %type% : ;")) break; + // Stop if return is found .. + if (indentlevel3 == 1) + { + if (tok3->str() == "return") + ret3 = true; + else if (tok3->str() == ";") + { + if (ret3 && !Token::simpleMatch(tok3->next(), "}")) + break; + ret3 = false; + } + } + if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str())) break; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 27d98eaf4..945901699 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -1170,31 +1170,55 @@ private: void simplifyKnownVariables25() { - // This testcase is related to ticket #1646 - const char code[] = "void foo(char *str)\n" - "{\n" - " int i;\n" - " for (i=0;i<10;++i) {\n" - " if (*str == 0) goto label;\n" - " }\n" - " return;\n" - "label:\n" - " str[i] = 0;\n" - "}\n"; + { + // This testcase is related to ticket #1646 + const char code[] = "void foo(char *str)\n" + "{\n" + " int i;\n" + " for (i=0;i<10;++i) {\n" + " if (*str == 0) goto label;\n" + " }\n" + " return;\n" + "label:\n" + " str[i] = 0;\n" + "}\n"; - // Current result - ASSERT_EQUALS( - "void foo ( char * str ) " - "{" - " int i ;" - " for ( i = 0 ; i < 10 ; ++ i ) {" - " if ( * str == 0 ) { goto label ; }" - " }" - " return ;" - " label : ;" - " str [ i ] = 0 ; " - "}", - simplifyKnownVariables(code)); + // Current result + ASSERT_EQUALS( + "void foo ( char * str ) " + "{" + " int i ;" + " for ( i = 0 ; i < 10 ; ++ i ) {" + " if ( * str == 0 ) { goto label ; }" + " }" + " return ;" + " label : ;" + " str [ i ] = 0 ; " + "}", + simplifyKnownVariables(code)); + } + + { + // This testcase is related to ticket #1646 + const char code[] = "void foo(char *str)\n" + "{\n" + " int i;\n" + " for (i=0;i<10;++i) { }\n" + " return;\n" + " str[i] = 0;\n" + "}\n"; + + // Current result + ASSERT_EQUALS( + "void foo ( char * str ) " + "{" + " int i ;" + " for ( i = 0 ; i < 10 ; ++ i ) { }" + " return ;" + " str [ i ] = 0 ; " + "}", + simplifyKnownVariables(code)); + } }