From 75ec97ad23f92f32b15c5da57fe6a144dbeb33c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 17 Aug 2014 07:39:42 +0200 Subject: [PATCH] Tokenizer::simplifyKnownVariables: Fixed bad simplification in for loop header --- lib/tokenize.cpp | 10 +++++++++- test/testtokenize.cpp | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f3778a28b..8efb4262c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6388,13 +6388,16 @@ bool Tokenizer::simplifyKnownVariables() // parse the block of code.. int indentlevel = 0; Token *tok2 = tok; + bool forhead = false; for (; tok2; tok2 = tok2->next()) { if (Token::Match(tok2, "[;{}] float|double %var% ;")) { floatvars.insert(tok2->tokAt(2)->varId()); } - if (tok2->str() == "{") + if (tok2->str() == "{") { + forhead = false; ++indentlevel; + } else if (tok2->str() == "}") { --indentlevel; @@ -6402,6 +6405,9 @@ bool Tokenizer::simplifyKnownVariables() break; } + else if (Token::simpleMatch(tok2, "for (")) + forhead = true; + else if (tok2->previous()->str() != "*" && !Token::Match(tok2->tokAt(-2), "* --|++") && (Token::Match(tok2, "%var% = %bool%|%char%|%num%|%str%|%var% ;") || Token::Match(tok2, "%var% [ ] = %str% ;") || @@ -6435,6 +6441,8 @@ bool Tokenizer::simplifyKnownVariables() if (Token::Match(tok3->tokAt(-2), "for ( %type%")) continue; } + if (forhead && Token::Match(tok2->previous(), ", %var% =")) + continue; // struct name.. if (Token::Match(tok2, "%varid% = &| %varid%", tok2->varId())) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index a3a27ba23..3f6a857be 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -200,6 +200,7 @@ private: TEST_CASE(simplifyKnownVariables56); // ticket #5301 - >> TEST_CASE(simplifyKnownVariables57); // ticket #4724 TEST_CASE(simplifyKnownVariables58); // ticket #5268 + TEST_CASE(simplifyKnownVariables59); // skip for header TEST_CASE(simplifyKnownVariablesIfEq1); // if (a==5) => a is 5 in the block TEST_CASE(simplifyKnownVariablesIfEq2); // if (a==5) { buf[a++] = 0; } TEST_CASE(simplifyKnownVariablesIfEq3); // #4708 - if (a==5) { buf[--a] = 0; } @@ -3019,6 +3020,21 @@ private: "int baz ( ) { return 2 ; }", tokenizeAndStringify(code, true)); } + void simplifyKnownVariables59() { // #5062 - for head + const char code[] = "void f() {\n" + " int a[3], i, j;\n" + " for(i = 0, j = 1; i < 3, j < 12; i++,j++) {\n" + " a[i] = 0;\n" + " }\n" + "}"; + ASSERT_EQUALS("void f ( ) {\n" + "int a [ 3 ] ; int i ; int j ;\n" + "for ( i = 0 , j = 1 ; i < 3 , j < 12 ; i ++ , j ++ ) {\n" + "a [ i ] = 0 ;\n" + "}\n" + "}", tokenizeAndStringify(code, true)); + } + void simplifyKnownVariablesIfEq1() { const char code[] = "void f(int x) {\n" " if (x==5) {\n"