Fixed #1596 ('Index out of bounds' false positive)

This commit is contained in:
Daniel Marjamäki 2010-04-15 19:09:19 +02:00
parent 01aa414f32
commit 3fa2137ee3
3 changed files with 42 additions and 4 deletions

View File

@ -5048,7 +5048,7 @@ bool Tokenizer::simplifyKnownVariables()
} }
} }
if (Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value))
{ {
const std::string op(tok3->strAt(2)); const std::string op(tok3->strAt(2));
if (Token::Match(tok3, "[{};] %any% %any% ;")) if (Token::Match(tok3, "[{};] %any% %any% ;"))
@ -5066,7 +5066,7 @@ bool Tokenizer::simplifyKnownVariables()
ret = true; ret = true;
} }
if (Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) && if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) &&
!Token::Match(tok3->tokAt(3), "[.[]")) !Token::Match(tok3->tokAt(3), "[.[]"))
{ {
incdec(value, tok3->strAt(1)); incdec(value, tok3->strAt(1));

View File

@ -608,7 +608,8 @@ private:
" if (c>0) { c++; }\n" " if (c>0) { c++; }\n"
" c++;\n" " c++;\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code)); TODO_ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code));
ASSERT_EQUALS("void f ( int & c ) { c = 1 ; ; { c ++ ; } c ++ ; }", tok(code));
} }
@ -622,7 +623,8 @@ private:
" if (c>0) { ++c; }\n" " if (c>0) { ++c; }\n"
" ++c;\n" " ++c;\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code)); TODO_ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code));
ASSERT_EQUALS("void f ( int & c ) { c = 1 ; ; { ++ c ; } ++ c ; }", tok(code));
} }
{ {

View File

@ -100,6 +100,7 @@ private:
TEST_CASE(simplifyKnownVariables20); TEST_CASE(simplifyKnownVariables20);
TEST_CASE(simplifyKnownVariables21); TEST_CASE(simplifyKnownVariables21);
TEST_CASE(simplifyKnownVariables22); TEST_CASE(simplifyKnownVariables22);
TEST_CASE(simplifyKnownVariables23);
TEST_CASE(match1); TEST_CASE(match1);
@ -1079,6 +1080,41 @@ private:
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
void simplifyKnownVariables23()
{
// This testcase is related to ticket #1596
const char code[] = "void foo(int x)\n"
"{\n"
" int a[10], c = 0;\n"
" if (x) {\n"
" a[c] = 0;\n"
" c++;\n"
" } else {\n"
" a[c] = 0;\n"
" }\n"
"}\n";
// wanted result
TODO_ASSERT_EQUALS(
"void foo ( int x ) "
"{"
" int a [ 10 ] ; int c ; c = 0 ;"
" if ( x ) { a [ 0 ] = 0 ; c = 1 ; }"
" else { a [ 0 ] = 0 ; } "
"}",
simplifyKnownVariables(code));
// Current result
ASSERT_EQUALS(
"void foo ( int x ) "
"{"
" int a [ 10 ] ; int c ; c = 0 ;"
" if ( x ) { a [ 0 ] = 0 ; c ++ ; }"
" else { a [ c ] = 0 ; } "
"}",
simplifyKnownVariables(code));
}
void match1() void match1()
{ {
// Match "%var% | %var%" // Match "%var% | %var%"