Tokenizer::simplifyKnownVariables: Fixed bad simplification in for loop header

This commit is contained in:
Daniel Marjamäki 2014-08-17 07:39:42 +02:00
parent 7ca742c454
commit 75ec97ad23
2 changed files with 25 additions and 1 deletions

View File

@ -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()))

View File

@ -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"