From 86cdc8e7a67af3cfbd136833a015291505a963b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 2 Mar 2015 06:18:00 +0100 Subject: [PATCH] Fixed #6540 (Tokenizer::simplifyKnownVariables: Missing varid for simplification in conditional code) --- lib/tokenize.cpp | 32 -------------------------- test/testtokenize.cpp | 53 ------------------------------------------- 2 files changed, 85 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 726236bbc..74f36dd08 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6550,38 +6550,6 @@ bool Tokenizer::simplifyKnownVariables() ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, valueToken, indentlevel); } - else if (Token::Match(tok2, "( %name% == %num% ) {")) { - const unsigned int varid = tok2->next()->varId(); - if (varid == 0) - continue; - - const std::string structname; - - const Token *valueToken = tok2->tokAt(3); - std::string value(tok2->strAt(3)), savedValue = value; - const unsigned int valueVarId = 0; - const bool valueIsPointer = false; - - // Insert a "%name% = %num% ;" at the beginning of the scope as simplifyKnownVariablesSimplify might compute an updated value - Token *scopeStart = tok2->tokAt(5); - scopeStart->insertToken(tok2->strAt(1)); - scopeStart = scopeStart->next(); - Token* artificialAssignment = scopeStart; - scopeStart->insertToken("="); - scopeStart = scopeStart->next(); - scopeStart->insertToken(valueToken->str()); - scopeStart = scopeStart->next(); - scopeStart->insertToken(";"); - scopeStart = scopeStart->next(); - - ret |= simplifyKnownVariablesSimplify(&artificialAssignment, tok2->tokAt(6), varid, structname, value, valueIsPointer, valueVarId, valueToken, -1); - - // Remove the artificial assignment if no modification was done - if (artificialAssignment->strAt(2) == savedValue) { - Token::eraseTokens(tok2->tokAt(5), scopeStart->next()); - } - } - else if (Token::Match(tok2, "strcpy|sprintf ( %name% , %str% ) ;")) { const unsigned int varid(tok2->tokAt(2)->varId()); if (varid == 0) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ff3b74d78..c79716c0d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -187,9 +187,6 @@ private: 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; } TEST_CASE(simplifyKnownVariablesBailOutAssign1); TEST_CASE(simplifyKnownVariablesBailOutAssign2); TEST_CASE(simplifyKnownVariablesBailOutAssign3); // #4395 - nested assignments @@ -2749,56 +2746,6 @@ private: "}", tokenizeAndStringify(code, true)); } - void simplifyKnownVariablesIfEq1() { - const char code[] = "void f(int x) {\n" - " if (x==5) {\n" - " return x;\n" - " }\n" - "}"; - const char expected[] = "void f ( int x ) {\n" - "if ( x == 5 ) {\n" - "return 5 ;\n" - "}\n" - "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c")); - } - - void simplifyKnownVariablesIfEq2() { - const char code[] = "void f(int x) {\n" - " if (x==5) {\n" - " buf[x++] = 0;\n" - " buf[x--] = 0;\n" - " }\n" - "}"; - // Increment and decrements should be computed - const char expected[] = "void f ( int x ) {\n" - "if ( x == 5 ) {\n" - "buf [ 5 ] = 0 ;\n" - "buf [ 6 ] = 0 ;\n" - "}\n" - "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c")); - } - - void simplifyKnownVariablesIfEq3() { - const char code[] = "void f(int x) {\n" - " if (x==5) {\n" - " buf[++x] = 0;\n" - " buf[++x] = 0;\n" - " buf[--x] = 0;\n" - " }\n" - "}"; - const char expected[] = "void f ( int x ) {\n" - "if ( x == 5 ) { " - "x = 6 ;\n" - "buf [ 6 ] = 0 ;\n" - "buf [ 7 ] = 0 ;\n" - "buf [ 6 ] = 0 ;\n" - "}\n" - "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c")); - } - void simplifyKnownVariablesBailOutAssign1() { const char code[] = "int foo() {\n" " int i; i = 0;\n"