Fixed #6540 (Tokenizer::simplifyKnownVariables: Missing varid for simplification in conditional code)
This commit is contained in:
parent
49099e78ff
commit
86cdc8e7a6
|
@ -6550,38 +6550,6 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, structname, value, valueVarId, valueIsPointer, valueToken, indentlevel);
|
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% ) ;")) {
|
else if (Token::Match(tok2, "strcpy|sprintf ( %name% , %str% ) ;")) {
|
||||||
const unsigned int varid(tok2->tokAt(2)->varId());
|
const unsigned int varid(tok2->tokAt(2)->varId());
|
||||||
if (varid == 0)
|
if (varid == 0)
|
||||||
|
|
|
@ -187,9 +187,6 @@ private:
|
||||||
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(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(simplifyKnownVariablesBailOutAssign1);
|
||||||
TEST_CASE(simplifyKnownVariablesBailOutAssign2);
|
TEST_CASE(simplifyKnownVariablesBailOutAssign2);
|
||||||
TEST_CASE(simplifyKnownVariablesBailOutAssign3); // #4395 - nested assignments
|
TEST_CASE(simplifyKnownVariablesBailOutAssign3); // #4395 - nested assignments
|
||||||
|
@ -2749,56 +2746,6 @@ private:
|
||||||
"}", tokenizeAndStringify(code, true));
|
"}", 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() {
|
void simplifyKnownVariablesBailOutAssign1() {
|
||||||
const char code[] = "int foo() {\n"
|
const char code[] = "int foo() {\n"
|
||||||
" int i; i = 0;\n"
|
" int i; i = 0;\n"
|
||||||
|
|
Loading…
Reference in New Issue