Skip to content

Commit b7074e6

Browse files
committed
++ and -- should have UNIQUE exprid
1 parent b7da13c commit b7074e6

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ namespace {
15811581
}
15821582
};
15831583
using ExprIdMap = std::map<ExprIdKey, nonneg int>;
1584-
void setParentExprId(Token* tok, ExprIdMap& exprIdMap, nonneg int &id) {
1584+
void setParentExprId(Token* tok, bool cpp, ExprIdMap& exprIdMap, nonneg int &id) {
15851585
if (!tok->astParent() || tok->astParent()->isControlFlowKeyword())
15861586
return;
15871587
const Token* op1 = tok->astParent()->astOperand1();
@@ -1591,10 +1591,10 @@ namespace {
15911591
if (op2 && op2->exprId() == 0)
15921592
return;
15931593

1594-
if (tok->astParent()->isExpandedMacro()) {
1594+
if (tok->astParent()->isExpandedMacro() || Token::Match(tok->astParent(), "++|--")) {
15951595
tok->astParent()->exprId(id);
15961596
++id;
1597-
setParentExprId(tok->astParent(), exprIdMap, id);
1597+
setParentExprId(tok->astParent(), cpp, exprIdMap, id);
15981598
return;
15991599
}
16001600

@@ -1612,10 +1612,12 @@ namespace {
16121612
if (key.operand1 > key.operand2 && key.operand2 &&
16131613
Token::Match(tok->astParent(), "%or%|%oror%|+|*|&|&&|^|==|!=")) {
16141614
// In C++ the order of operands of + might matter
1615-
if (key.parentOp != "+" ||
1616-
!tok->astParent()->valueType() ||
1617-
tok->astParent()->valueType()->isIntegral() ||
1618-
tok->astParent()->valueType()->isFloat())
1615+
if (!cpp ||
1616+
key.parentOp != "+" ||
1617+
!tok->astParent()->valueType() ||
1618+
tok->astParent()->valueType()->isIntegral() ||
1619+
tok->astParent()->valueType()->isFloat() ||
1620+
tok->astParent()->valueType()->pointer > 0)
16191621
std::swap(key.operand1, key.operand2);
16201622
}
16211623

@@ -1630,7 +1632,7 @@ namespace {
16301632
} else {
16311633
tok->astParent()->exprId(it->second);
16321634
}
1633-
setParentExprId(tok->astParent(), exprIdMap, id);
1635+
setParentExprId(tok->astParent(), cpp, exprIdMap, id);
16341636
i1 = 1 + refs1.size();
16351637
break;
16361638
}
@@ -1709,7 +1711,7 @@ void SymbolDatabase::createSymbolDatabaseExprIds()
17091711
if (tok->varId() > 0) {
17101712
tok->exprId(tok->varId());
17111713
if (tok->astParent() && tok->astParent()->exprId() == 0)
1712-
setParentExprId(tok, exprIdMap, id);
1714+
setParentExprId(tok, mTokenizer.isCPP(), exprIdMap, id);
17131715
} else if (tok->astParent() && !tok->astOperand1() && !tok->astOperand2()) {
17141716
if (tok->tokType() == Token::Type::eBracket)
17151717
continue;
@@ -1732,7 +1734,7 @@ void SymbolDatabase::createSymbolDatabaseExprIds()
17321734
}
17331735
}
17341736

1735-
setParentExprId(tok, exprIdMap, id);
1737+
setParentExprId(tok, mTokenizer.isCPP(), exprIdMap, id);
17361738
}
17371739
}
17381740
for (Token* tok = const_cast<Token*>(scope->bodyStart); tok != scope->bodyEnd; tok = tok->next()) {

test/testvarid.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ class TestVarID : public TestFixture {
240240
TEST_CASE(exprid3);
241241
TEST_CASE(exprid4);
242242
TEST_CASE(exprid5);
243+
TEST_CASE(exprid6);
243244

244245
TEST_CASE(structuredBindings);
245246
}
@@ -3929,6 +3930,19 @@ class TestVarID : public TestFixture {
39293930
ASSERT_EQUALS(expected, tokenizeExpr(code));
39303931
}
39313932

3933+
void exprid6() {
3934+
// ++ and -- should have UNIQUE exprid
3935+
const char code[] = "void foo(int *a) {\n"
3936+
" *a++ = 0;\n"
3937+
" if (*a++ == 32) {}\n"
3938+
"}\n";
3939+
const char expected[] = "1: void foo ( int * a ) {\n"
3940+
"2: *@UNIQUE a@1 ++@UNIQUE = 0 ;\n"
3941+
"3: if ( *@UNIQUE a@1 ++@UNIQUE ==@UNIQUE 32 ) { }\n"
3942+
"4: }\n";
3943+
ASSERT_EQUALS(expected, tokenizeExpr(code));
3944+
}
3945+
39323946
void structuredBindings() {
39333947
const char code[] = "int foo() { auto [x,y] = xy(); return x+y; }";
39343948
ASSERT_EQUALS("1: int foo ( ) { auto [ x@1 , y@2 ] = xy ( ) ; return x@1 + y@2 ; }\n",

0 commit comments

Comments
 (0)