Ticket #4959: Simplify {in,de}crements of known variables.
This commit is contained in:
parent
538e4bd798
commit
5743416d3e
|
@ -6233,12 +6233,24 @@ bool Tokenizer::simplifyKnownVariables()
|
|||
const std::string structname;
|
||||
|
||||
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 bool valueIsPointer = false;
|
||||
|
||||
Token *scopeStart = tok2->tokAt(6);
|
||||
ret |= simplifyKnownVariablesSimplify(&scopeStart, scopeStart, varid, structname, value, valueIsPointer, valueVarId, valueToken, -1);
|
||||
// Insert a "%var% = %num% ;" at the beginning of the scope as simplifyKnownVariablesSimplify might compute an updated value
|
||||
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% ) ;")) {
|
||||
|
@ -6352,9 +6364,6 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
|
|||
|
||||
bool ret = false;
|
||||
|
||||
// skip increments and decrements if the given indentlevel is -1
|
||||
const bool skipincdec = (indentlevel == -1);
|
||||
|
||||
Token* bailOutFromLoop = 0;
|
||||
int indentlevel3 = indentlevel;
|
||||
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));
|
||||
if (Token::Match(tok3, "[{};] %any% %any% ;")) {
|
||||
tok3->deleteNext(3);
|
||||
|
@ -6713,7 +6722,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
|
|||
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), "[.[]")) {
|
||||
incdec(value, tok3->next()->str());
|
||||
(*tok2)->tokAt(2)->str(value);
|
||||
|
|
|
@ -2753,12 +2753,6 @@ private:
|
|||
" buf[x--] = 0;\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
|
||||
const char expected[] = "void f ( int x ) {\n"
|
||||
"if ( x == 5 ) {\n"
|
||||
|
@ -2766,7 +2760,7 @@ private:
|
|||
"buf [ 6 ] = 0 ;\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() {
|
||||
|
@ -2777,23 +2771,15 @@ private:
|
|||
" buf[--x] = 0;\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"
|
||||
"if ( x == 5 ) {\n"
|
||||
"if ( x == 5 ) { "
|
||||
"x = 6 ;\n"
|
||||
"buf [ 6 ] = 0 ;\n"
|
||||
"buf [ 7 ] = 0 ;\n"
|
||||
"buf [ 6 ] = 0 ;\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() {
|
||||
|
|
Loading…
Reference in New Issue