From 25391cb562fdb96321fea865bea68f860f9c3b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 7 Nov 2010 08:10:09 +0100 Subject: [PATCH] Tokenizer: Bailout simplifyKnownVariables for loop variables --- lib/tokenize.cpp | 7 ++++++- test/testtokenize.cpp | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 88263f687..b43027f54 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5773,7 +5773,6 @@ bool Tokenizer::simplifyKnownVariables() Token *tok2 = tok; for (; tok2; tok2 = tok2->next()) { - if (tok2->str() == "{") ++indentlevel; @@ -5798,6 +5797,12 @@ bool Tokenizer::simplifyKnownVariables() if (varid == 0) continue; + if (Token::Match(tok2->tokAt(-3), "for ( %type% %var% = %num% ;")) + { + // skip loop variable + continue; + } + if (tok2->str() == tok2->strAt(2)) continue; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 6b4dcfa82..79f6c235c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -122,7 +122,8 @@ private: TEST_CASE(simplifyKnownVariables29); // ticket #1811 TEST_CASE(simplifyKnownVariables30); TEST_CASE(simplifyKnownVariables31); - TEST_CASE(simplifyKnownVariablesBailOut1); + TEST_CASE(simplifyKnownVariablesBailOutFor); + TEST_CASE(simplifyKnownVariablesBailOutMemberFunction); TEST_CASE(varid1); TEST_CASE(varid2); @@ -1855,7 +1856,18 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } - void simplifyKnownVariablesBailOut1() + void simplifyKnownVariablesBailOutFor() + { + const char code[] = "void foo() {\n" + " for (int i = 0; i < 10; ++i) { }\n" + "}\n"; + const char expected[] = "void foo ( ) {\n" + "for ( int i = 0 ; i < 10 ; ++ i ) { }\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + + void simplifyKnownVariablesBailOutMemberFunction() { const char code[] = "void foo(obj a) {\n" " obj b = a;\n"