Skip to content

Commit 251f6d2

Browse files
committed
Fixed cppcheck-opensource#6898 (Token::expressionString: wrong output when first token is post-incremented)
1 parent 42a406a commit 251f6d2

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

lib/token.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,8 @@ std::string Token::expressionString() const
11911191
{
11921192
const Token * const top = this;
11931193
const Token *start = top;
1194-
while (start->astOperand1() && (start->astOperand2() || Token::simpleMatch(start, "( )")))
1194+
while (start->astOperand1() &&
1195+
(start->astOperand2() || !start->isUnaryPreOp() || Token::simpleMatch(start, "( )")))
11951196
start = start->astOperand1();
11961197
const Token *end = top;
11971198
while (end->astOperand1() && (end->astOperand2() || end->isUnaryPreOp())) {
@@ -1208,7 +1209,6 @@ std::string Token::expressionString() const
12081209
ret += " ";
12091210
}
12101211
return ret + end->str();
1211-
12121212
}
12131213

12141214
static void astStringXml(const Token *tok, std::size_t indent, std::ostream &out)

test/testcondition.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,13 @@ class TestCondition : public TestFixture {
15211521
"}");
15221522
ASSERT_EQUALS("[test.cpp:4]: (style) Condition !x is always true\n", errout.str());
15231523

1524+
check("void f() {\n" // #6898 (Token::expressionString)
1525+
" int x = 0;\n"
1526+
" A(x++ == 1);\n"
1527+
" A(x++ == 2);\n"
1528+
"}");
1529+
ASSERT_EQUALS("[test.cpp:4]: (style) Condition x++==2 is always false\n", errout.str());
1530+
15241531
// Avoid FP when condition comes from macro
15251532
check("void f() {\n"
15261533
" int x = 0;\n"

0 commit comments

Comments
 (0)