Fixed #1646 (False positive: array access after return cannot have default loop value)

This commit is contained in:
Daniel Marjamäki 2010-05-05 18:29:56 +02:00
parent 4c95264d29
commit af4c4cef34
2 changed files with 62 additions and 24 deletions

View File

@ -4977,6 +4977,7 @@ bool Tokenizer::simplifyKnownVariables()
} }
Token* bailOutFromLoop = 0; Token* bailOutFromLoop = 0;
int indentlevel3 = indentlevel; // indentlevel for tok3 int indentlevel3 = indentlevel; // indentlevel for tok3
bool ret3 = false;
for (; tok3; tok3 = tok3->next()) for (; tok3; tok3 = tok3->next())
{ {
if (tok3->str() == "{") if (tok3->str() == "{")
@ -5002,6 +5003,19 @@ bool Tokenizer::simplifyKnownVariables()
if (Token::Match(tok3, "; %type% : ;")) if (Token::Match(tok3, "; %type% : ;"))
break; 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())) if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str()))
break; break;

View File

@ -1170,31 +1170,55 @@ private:
void simplifyKnownVariables25() void simplifyKnownVariables25()
{ {
// This testcase is related to ticket #1646 {
const char code[] = "void foo(char *str)\n" // This testcase is related to ticket #1646
"{\n" const char code[] = "void foo(char *str)\n"
" int i;\n" "{\n"
" for (i=0;i<10;++i) {\n" " int i;\n"
" if (*str == 0) goto label;\n" " for (i=0;i<10;++i) {\n"
" }\n" " if (*str == 0) goto label;\n"
" return;\n" " }\n"
"label:\n" " return;\n"
" str[i] = 0;\n" "label:\n"
"}\n"; " str[i] = 0;\n"
"}\n";
// Current result // Current result
ASSERT_EQUALS( ASSERT_EQUALS(
"void foo ( char * str ) " "void foo ( char * str ) "
"{" "{"
" int i ;" " int i ;"
" for ( i = 0 ; i < 10 ; ++ i ) {" " for ( i = 0 ; i < 10 ; ++ i ) {"
" if ( * str == 0 ) { goto label ; }" " if ( * str == 0 ) { goto label ; }"
" }" " }"
" return ;" " return ;"
" label : ;" " label : ;"
" str [ i ] = 0 ; " " str [ i ] = 0 ; "
"}", "}",
simplifyKnownVariables(code)); 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));
}
} }