Fixed #1646 (False positive: array access after return cannot have default loop value)
This commit is contained in:
parent
2bced1d90c
commit
f87eb774eb
|
@ -5067,6 +5067,29 @@ bool Tokenizer::simplifyKnownVariables()
|
|||
|
||||
if (Token::Match(tok2->tokAt(-2), "for ( %varid% = %num% ; %varid% <|<= %num% ; ++| %varid% ++| ) {", varid))
|
||||
{
|
||||
// is there a "break" in the for loop?
|
||||
bool hasbreak = false;
|
||||
unsigned int indentlevel4 = 0; // indentlevel for tok4
|
||||
for (const Token *tok4 = tok2->previous()->link(); tok4; tok4 = tok4->next())
|
||||
{
|
||||
if (tok4->str() == "{")
|
||||
++indentlevel4;
|
||||
else if (tok4->str() == "}")
|
||||
{
|
||||
if (indentlevel4 <= 1)
|
||||
break;
|
||||
--indentlevel4;
|
||||
}
|
||||
else if (tok4->str() == "break")
|
||||
{
|
||||
hasbreak = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hasbreak)
|
||||
break;
|
||||
|
||||
// no break => the value of the counter value is known after the for loop..
|
||||
const std::string compareop = tok2->strAt(5);
|
||||
if (compareop == "<")
|
||||
value = tok2->strAt(6);
|
||||
|
|
|
@ -1151,6 +1151,7 @@ private:
|
|||
}
|
||||
|
||||
void simplifyKnownVariables24()
|
||||
{
|
||||
{
|
||||
// This testcase is related to ticket #1596
|
||||
const char code[] = "void foo()\n"
|
||||
|
@ -1160,7 +1161,6 @@ private:
|
|||
" a[c] = 0;\n"
|
||||
"}\n";
|
||||
|
||||
// Current result
|
||||
ASSERT_EQUALS(
|
||||
"void foo ( ) "
|
||||
"{"
|
||||
|
@ -1171,6 +1171,31 @@ private:
|
|||
simplifyKnownVariables(code));
|
||||
}
|
||||
|
||||
{
|
||||
// #1692 - unknown counter value after for loop
|
||||
const char code[] = "void foo(const char s[])\n"
|
||||
"{\n"
|
||||
" int x[3];\n"
|
||||
" int i;\n"
|
||||
" for (i = 0; i < 3; ++i) {\n"
|
||||
" if (s[i]) break;\n"
|
||||
" }"
|
||||
" if (i < 3) x[i] = 0;\n"
|
||||
"}\n";
|
||||
ASSERT_EQUALS(
|
||||
"void foo ( const char s [ ] ) "
|
||||
"{"
|
||||
" int x [ 3 ] ;"
|
||||
" int i ;"
|
||||
" for ( i = 0 ; i < 3 ; ++ i ) {"
|
||||
" if ( s [ i ] ) { break ; }"
|
||||
" }"
|
||||
" if ( i < 3 ) { x [ i ] = 0 ; } "
|
||||
"}",
|
||||
simplifyKnownVariables(code));
|
||||
}
|
||||
}
|
||||
|
||||
void simplifyKnownVariables25()
|
||||
{
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue