Ticket #4959: Simplify {in,de}crements of known variables.

This commit is contained in:
Simon Martin 2013-08-16 23:51:00 +02:00
parent 538e4bd798
commit 5743416d3e
2 changed files with 20 additions and 25 deletions

View File

@ -6233,12 +6233,24 @@ bool Tokenizer::simplifyKnownVariables()
const std::string structname; const std::string structname;
const Token *valueToken = tok2->tokAt(3); const Token *valueToken = tok2->tokAt(3);
std::string value(tok2->strAt(3)); std::string value(tok2->strAt(3)), savedValue = value;
const unsigned int valueVarId = 0; const unsigned int valueVarId = 0;
const bool valueIsPointer = false; const bool valueIsPointer = false;
Token *scopeStart = tok2->tokAt(6); // Insert a "%var% = %num% ;" at the beginning of the scope as simplifyKnownVariablesSimplify might compute an updated value
ret |= simplifyKnownVariablesSimplify(&scopeStart, scopeStart, varid, structname, value, valueIsPointer, valueVarId, valueToken, -1); Token *scopeStart = tok2->tokAt(5);
scopeStart->insertToken(tok2->tokAt(1)->str()); 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->tokAt(2)->str() == savedValue) {
Token::eraseTokens(tok2->tokAt(5), scopeStart->next());
}
} }
else if (Token::Match(tok2, "strcpy|sprintf ( %var% , %str% ) ;")) { else if (Token::Match(tok2, "strcpy|sprintf ( %var% , %str% ) ;")) {
@ -6352,9 +6364,6 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
bool ret = false; bool ret = false;
// skip increments and decrements if the given indentlevel is -1
const bool skipincdec = (indentlevel == -1);
Token* bailOutFromLoop = 0; Token* bailOutFromLoop = 0;
int indentlevel3 = indentlevel; int indentlevel3 = indentlevel;
bool ret3 = false; bool ret3 = false;
@ -6695,7 +6704,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
} }
} }
if (!skipincdec && indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) { if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) {
const std::string op(tok3->strAt(2)); const std::string op(tok3->strAt(2));
if (Token::Match(tok3, "[{};] %any% %any% ;")) { if (Token::Match(tok3, "[{};] %any% %any% ;")) {
tok3->deleteNext(3); tok3->deleteNext(3);
@ -6713,7 +6722,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
ret = true; ret = true;
} }
if (!skipincdec && indentlevel == indentlevel3 && Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) && if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) &&
!Token::Match(tok3->tokAt(3), "[.[]")) { !Token::Match(tok3->tokAt(3), "[.[]")) {
incdec(value, tok3->next()->str()); incdec(value, tok3->next()->str());
(*tok2)->tokAt(2)->str(value); (*tok2)->tokAt(2)->str(value);

View File

@ -2753,12 +2753,6 @@ private:
" buf[x--] = 0;\n" " buf[x--] = 0;\n"
" }\n" " }\n"
"}"; "}";
const char current[] = "void f ( int x ) {\n"
"if ( x == 5 ) {\n"
"buf [ x ++ ] = 0 ;\n"
"buf [ x -- ] = 0 ;\n"
"}\n"
"}";
// Increment and decrements should be computed // Increment and decrements should be computed
const char expected[] = "void f ( int x ) {\n" const char expected[] = "void f ( int x ) {\n"
"if ( x == 5 ) {\n" "if ( x == 5 ) {\n"
@ -2766,7 +2760,7 @@ private:
"buf [ 6 ] = 0 ;\n" "buf [ 6 ] = 0 ;\n"
"}\n" "}\n"
"}"; "}";
TODO_ASSERT_EQUALS(expected, current, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c")); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c"));
} }
void simplifyKnownVariablesIfEq3() { void simplifyKnownVariablesIfEq3() {
@ -2777,23 +2771,15 @@ private:
" buf[--x] = 0;\n" " buf[--x] = 0;\n"
" }\n" " }\n"
"}"; "}";
const char current[] = "void f ( int x ) {\n"
"if ( x == 5 ) {\n"
"buf [ ++ x ] = 0 ;\n"
"buf [ ++ x ] = 0 ;\n"
"buf [ -- x ] = 0 ;\n"
"}\n"
"}";
// Increment and decrements should be computed
const char expected[] = "void f ( int x ) {\n" const char expected[] = "void f ( int x ) {\n"
"if ( x == 5 ) {\n" "if ( x == 5 ) { "
"x = 6 ;\n" "x = 6 ;\n"
"buf [ 6 ] = 0 ;\n" "buf [ 6 ] = 0 ;\n"
"buf [ 7 ] = 0 ;\n" "buf [ 7 ] = 0 ;\n"
"buf [ 6 ] = 0 ;\n" "buf [ 6 ] = 0 ;\n"
"}\n" "}\n"
"}"; "}";
TODO_ASSERT_EQUALS(expected, current, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c")); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c"));
} }
void simplifyKnownVariablesBailOutAssign1() { void simplifyKnownVariablesBailOutAssign1() {