Skip to content

Commit

Permalink
++ and -- should have UNIQUE exprid
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Nov 27, 2023
1 parent b7da13c commit b7074e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ namespace {
}
};
using ExprIdMap = std::map<ExprIdKey, nonneg int>;
void setParentExprId(Token* tok, ExprIdMap& exprIdMap, nonneg int &id) {
void setParentExprId(Token* tok, bool cpp, ExprIdMap& exprIdMap, nonneg int &id) {
if (!tok->astParent() || tok->astParent()->isControlFlowKeyword())
return;
const Token* op1 = tok->astParent()->astOperand1();
Expand All @@ -1591,10 +1591,10 @@ namespace {
if (op2 && op2->exprId() == 0)
return;

if (tok->astParent()->isExpandedMacro()) {
if (tok->astParent()->isExpandedMacro() || Token::Match(tok->astParent(), "++|--")) {
tok->astParent()->exprId(id);
++id;
setParentExprId(tok->astParent(), exprIdMap, id);
setParentExprId(tok->astParent(), cpp, exprIdMap, id);
return;
}

Expand All @@ -1612,10 +1612,12 @@ namespace {
if (key.operand1 > key.operand2 && key.operand2 &&
Token::Match(tok->astParent(), "%or%|%oror%|+|*|&|&&|^|==|!=")) {
// In C++ the order of operands of + might matter
if (key.parentOp != "+" ||
!tok->astParent()->valueType() ||
tok->astParent()->valueType()->isIntegral() ||
tok->astParent()->valueType()->isFloat())
if (!cpp ||
key.parentOp != "+" ||
!tok->astParent()->valueType() ||
tok->astParent()->valueType()->isIntegral() ||
tok->astParent()->valueType()->isFloat() ||
tok->astParent()->valueType()->pointer > 0)
std::swap(key.operand1, key.operand2);
}

Expand All @@ -1630,7 +1632,7 @@ namespace {
} else {
tok->astParent()->exprId(it->second);
}
setParentExprId(tok->astParent(), exprIdMap, id);
setParentExprId(tok->astParent(), cpp, exprIdMap, id);
i1 = 1 + refs1.size();
break;
}
Expand Down Expand Up @@ -1709,7 +1711,7 @@ void SymbolDatabase::createSymbolDatabaseExprIds()
if (tok->varId() > 0) {
tok->exprId(tok->varId());
if (tok->astParent() && tok->astParent()->exprId() == 0)
setParentExprId(tok, exprIdMap, id);
setParentExprId(tok, mTokenizer.isCPP(), exprIdMap, id);
} else if (tok->astParent() && !tok->astOperand1() && !tok->astOperand2()) {
if (tok->tokType() == Token::Type::eBracket)
continue;
Expand All @@ -1732,7 +1734,7 @@ void SymbolDatabase::createSymbolDatabaseExprIds()
}
}

setParentExprId(tok, exprIdMap, id);
setParentExprId(tok, mTokenizer.isCPP(), exprIdMap, id);
}
}
for (Token* tok = const_cast<Token*>(scope->bodyStart); tok != scope->bodyEnd; tok = tok->next()) {
Expand Down
14 changes: 14 additions & 0 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class TestVarID : public TestFixture {
TEST_CASE(exprid3);
TEST_CASE(exprid4);
TEST_CASE(exprid5);
TEST_CASE(exprid6);

TEST_CASE(structuredBindings);
}
Expand Down Expand Up @@ -3929,6 +3930,19 @@ class TestVarID : public TestFixture {
ASSERT_EQUALS(expected, tokenizeExpr(code));
}

void exprid6() {
// ++ and -- should have UNIQUE exprid
const char code[] = "void foo(int *a) {\n"
" *a++ = 0;\n"
" if (*a++ == 32) {}\n"
"}\n";
const char expected[] = "1: void foo ( int * a ) {\n"
"2: *@UNIQUE a@1 ++@UNIQUE = 0 ;\n"
"3: if ( *@UNIQUE a@1 ++@UNIQUE ==@UNIQUE 32 ) { }\n"
"4: }\n";
ASSERT_EQUALS(expected, tokenizeExpr(code));
}

void structuredBindings() {
const char code[] = "int foo() { auto [x,y] = xy(); return x+y; }";
ASSERT_EQUALS("1: int foo ( ) { auto [ x@1 , y@2 ] = xy ( ) ; return x@1 + y@2 ; }\n",
Expand Down

0 comments on commit b7074e6

Please sign in to comment.