Tokenizer::simplifyKnownVariables: Fixed bad simplification in for loop header
This commit is contained in:
parent
7ca742c454
commit
75ec97ad23
|
@ -6388,13 +6388,16 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
// parse the block of code..
|
// parse the block of code..
|
||||||
int indentlevel = 0;
|
int indentlevel = 0;
|
||||||
Token *tok2 = tok;
|
Token *tok2 = tok;
|
||||||
|
bool forhead = false;
|
||||||
for (; tok2; tok2 = tok2->next()) {
|
for (; tok2; tok2 = tok2->next()) {
|
||||||
if (Token::Match(tok2, "[;{}] float|double %var% ;")) {
|
if (Token::Match(tok2, "[;{}] float|double %var% ;")) {
|
||||||
floatvars.insert(tok2->tokAt(2)->varId());
|
floatvars.insert(tok2->tokAt(2)->varId());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tok2->str() == "{")
|
if (tok2->str() == "{") {
|
||||||
|
forhead = false;
|
||||||
++indentlevel;
|
++indentlevel;
|
||||||
|
}
|
||||||
|
|
||||||
else if (tok2->str() == "}") {
|
else if (tok2->str() == "}") {
|
||||||
--indentlevel;
|
--indentlevel;
|
||||||
|
@ -6402,6 +6405,9 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (Token::simpleMatch(tok2, "for ("))
|
||||||
|
forhead = true;
|
||||||
|
|
||||||
else if (tok2->previous()->str() != "*" && !Token::Match(tok2->tokAt(-2), "* --|++") &&
|
else if (tok2->previous()->str() != "*" && !Token::Match(tok2->tokAt(-2), "* --|++") &&
|
||||||
(Token::Match(tok2, "%var% = %bool%|%char%|%num%|%str%|%var% ;") ||
|
(Token::Match(tok2, "%var% = %bool%|%char%|%num%|%str%|%var% ;") ||
|
||||||
Token::Match(tok2, "%var% [ ] = %str% ;") ||
|
Token::Match(tok2, "%var% [ ] = %str% ;") ||
|
||||||
|
@ -6435,6 +6441,8 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
if (Token::Match(tok3->tokAt(-2), "for ( %type%"))
|
if (Token::Match(tok3->tokAt(-2), "for ( %type%"))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (forhead && Token::Match(tok2->previous(), ", %var% ="))
|
||||||
|
continue;
|
||||||
|
|
||||||
// struct name..
|
// struct name..
|
||||||
if (Token::Match(tok2, "%varid% = &| %varid%", tok2->varId()))
|
if (Token::Match(tok2, "%varid% = &| %varid%", tok2->varId()))
|
||||||
|
|
|
@ -200,6 +200,7 @@ private:
|
||||||
TEST_CASE(simplifyKnownVariables56); // ticket #5301 - >>
|
TEST_CASE(simplifyKnownVariables56); // ticket #5301 - >>
|
||||||
TEST_CASE(simplifyKnownVariables57); // ticket #4724
|
TEST_CASE(simplifyKnownVariables57); // ticket #4724
|
||||||
TEST_CASE(simplifyKnownVariables58); // ticket #5268
|
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(simplifyKnownVariablesIfEq1); // if (a==5) => a is 5 in the block
|
||||||
TEST_CASE(simplifyKnownVariablesIfEq2); // if (a==5) { buf[a++] = 0; }
|
TEST_CASE(simplifyKnownVariablesIfEq2); // if (a==5) { buf[a++] = 0; }
|
||||||
TEST_CASE(simplifyKnownVariablesIfEq3); // #4708 - 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));
|
"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() {
|
void simplifyKnownVariablesIfEq1() {
|
||||||
const char code[] = "void f(int x) {\n"
|
const char code[] = "void f(int x) {\n"
|
||||||
" if (x==5) {\n"
|
" if (x==5) {\n"
|
||||||
|
|
Loading…
Reference in New Issue