Fixed #3754 (Tokenizer::simplifyKnownVariables: wrong for loop simplification)

This commit is contained in:
Daniel Marjamäki 2012-04-25 19:23:17 +02:00
parent 08ae15e42d
commit f84a4344ae
2 changed files with 19 additions and 1 deletions

View File

@ -6094,7 +6094,11 @@ bool Tokenizer::simplifyKnownVariablesGetData(unsigned int varid, Token **_tok2,
Token *tok2 = *_tok2;
Token *tok3 = *_tok3;
if (Token::Match(tok2->tokAt(-2), "for ( %varid% = %num% ; %varid% <|<= %num% ; ++| %varid% ++| ) {", varid)) {
if (Token::Match(tok2->tokAt(-2), "for (")) {
// only specific for loops is handled
if (!Token::Match(tok2, "%varid% = %num% ; %varid% <|<= %num% ; ++| %varid% ++| ) {", varid))
return false;
// is there a "break" in the for loop?
bool hasbreak = false;
const Token* end4 = tok2->linkAt(-1)->linkAt(1);

View File

@ -150,6 +150,7 @@ private:
TEST_CASE(simplifyKnownVariables45); // ticket #3281 - static constant variable not simplified
TEST_CASE(simplifyKnownVariables46); // ticket #3587 - >>
TEST_CASE(simplifyKnownVariables47); // ticket #3627 - >>
TEST_CASE(simplifyKnownVariables48); // ticket #3754 - wrong simplification in for loop header
TEST_CASE(simplifyKnownVariablesIfEq1); // if (a==5) => a is 5 in the block
TEST_CASE(simplifyKnownVariablesIfEq2); // if (a==5) { buf[a++] = 0; }
TEST_CASE(simplifyKnownVariablesBailOutAssign1);
@ -2280,6 +2281,19 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.cpp"));
}
void simplifyKnownVariables48() {
// #3754
const char code[] = "void f(int sz) {\n"
" int i;\n"
" for (i = 0; ((i<sz) && (sz>3)); ++i) { }\n"
"}";
const char expected[] = "void f ( int sz ) {\n"
"int i ;\n"
"for ( i = 0 ; ( i < sz ) && ( 3 < sz ) ; ++ i ) { }\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c"));
}
void simplifyKnownVariablesIfEq1() {
const char code[] = "void f(int x) {\n"
" if (x==5) {\n"