Fixed #1646 (False positive: array access after return cannot have default loop value)
This commit is contained in:
parent
4c95264d29
commit
af4c4cef34
|
@ -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;
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue