From af23a9dcbbb3ac1530a1238257c6da8fc6ecf566 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Mon, 10 Oct 2011 15:13:47 +0200 Subject: [PATCH] Fixed ticket #3140 (Improve Tokenizer: Remove dead for loop 'for (int i = 0; i < 0; ++i)') --- lib/tokenize.cpp | 9 +++++---- test/testsimplifytokens.cpp | 10 ++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 47a1f9ccc..acf5e423f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -10001,10 +10001,11 @@ void Tokenizer::simplifyWhile0() // while (0) const bool while0(Token::Match(tok, "while ( 0|false )")); - // for (0) - const bool for0(Token::Match(tok, "for ( %var% = %num% ; %var% < %num% ;") && - tok->strAt(2) == tok->strAt(6) && - tok->strAt(4) == tok->strAt(8)); + // for (0) - not banal, ticket #3140 + const bool for0((Token::Match(tok, "for ( %var% = %num% ; %var% < %num% ;") && + tok->strAt(2) == tok->strAt(6) && tok->strAt(4) == tok->strAt(8)) || + (Token::Match(tok->tokAt(2), "%type% %var% = %num% ; %var% < %num% ;") && + tok->strAt(3) == tok->strAt(7) && tok->strAt(5) == tok->strAt(9))); if (!while0 && !for0) continue; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 4e4d0a0ec..936c92e1f 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -294,6 +294,8 @@ private: // simplify "while (0)" TEST_CASE(while0); + // ticket #3140 + TEST_CASE(while0for); TEST_CASE(while1); TEST_CASE(enum1); @@ -6344,9 +6346,17 @@ private: ASSERT_EQUALS("; do { continue ; } while ( false ) ;", tok("; do { continue ; } while (0);")); ASSERT_EQUALS("; do { break ; } while ( false ) ;", tok("; do { break; } while (0);")); ASSERT_EQUALS(";", tok("; while (false) { a; }")); + } + void while0for() + { // for (condition is always false) ASSERT_EQUALS("void f ( ) { ; }", tok("void f() { int i; for (i = 0; i < 0; i++) { a; } }")); + //ticket #3140 + ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (int i = 0; i < 0; i++) { a; } }")); + ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (unsigned int i = 0; i < 0; i++) { a; } }")); + ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (long long i = 0; i < 0; i++) { a; } }")); + ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (signed long long i = 0; i < 0; i++) { a; } }")); } void while1()