From 3fa2137ee351b8f05b172611907bb4843f480d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 15 Apr 2010 19:09:19 +0200 Subject: [PATCH] Fixed #1596 ('Index out of bounds' false positive) --- lib/tokenize.cpp | 4 ++-- test/testsimplifytokens.cpp | 6 ++++-- test/testtokenize.cpp | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e73e9dcd4..addc5f32c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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)); if (Token::Match(tok3, "[{};] %any% %any% ;")) @@ -5066,7 +5066,7 @@ bool Tokenizer::simplifyKnownVariables() 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), "[.[]")) { incdec(value, tok3->strAt(1)); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 2c27100cd..706d88a78 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -608,7 +608,8 @@ private: " if (c>0) { c++; }\n" " c++;\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" " ++c;\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)); } { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 6d78e98ad..b7c941e44 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -100,6 +100,7 @@ private: TEST_CASE(simplifyKnownVariables20); TEST_CASE(simplifyKnownVariables21); TEST_CASE(simplifyKnownVariables22); + TEST_CASE(simplifyKnownVariables23); TEST_CASE(match1); @@ -1079,6 +1080,41 @@ private: 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() { // Match "%var% | %var%"