Fixed #4708: prefix {in,de}crement too eagerly computed.

This commit is contained in:
Simon Martin 2013-08-14 23:47:05 +02:00
parent f95b29db06
commit bf09191b78
2 changed files with 40 additions and 3 deletions

View File

@ -6713,7 +6713,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
ret = true;
}
if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) &&
if (!skipincdec && 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);

View File

@ -178,6 +178,7 @@ private:
TEST_CASE(simplifyKnownVariables53); // references
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(simplifyKnownVariablesBailOutAssign2);
TEST_CASE(simplifyKnownVariablesBailOutAssign3); // #4395 - nested assignments
@ -2749,14 +2750,50 @@ private:
const char code[] = "void f(int x) {\n"
" if (x==5) {\n"
" buf[x++] = 0;\n"
" buf[x--] = 0;\n"
" }\n"
"}";
const char expected[] = "void f ( int x ) {\n"
const char current[] = "void f ( int x ) {\n"
"if ( x == 5 ) {\n"
"buf [ x ++ ] = 0 ;\n"
"buf [ x -- ] = 0 ;\n"
"}\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c"));
// 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"
"}";
TODO_ASSERT_EQUALS(expected, current, 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 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"
"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"));
}
void simplifyKnownVariablesBailOutAssign1() {