@@ -1611,6 +1611,26 @@ static std::string getIncompleteNameID(const Token* tok)
16111611}
16121612
16131613namespace {
1614+ int getExprIdForOperand (const Token* tok) {
1615+ if (!tok)
1616+ return 0 ;
1617+
1618+ int otherExprId = 0 ;
1619+
1620+ // Look through all referenced tokens.
1621+ // If two exprIds are found and one matches tok->exprId(), return the other.
1622+ // Otherwise, default to returning tok->exprId().
1623+ for (const auto & ref: followAllReferences (tok)) {
1624+ const int refExprId = ref.token ->exprId ();
1625+ if (refExprId != 0 && refExprId != tok->exprId ()) {
1626+ if (otherExprId != 0 && otherExprId != refExprId)
1627+ return tok->exprId ();
1628+ otherExprId = refExprId;
1629+ }
1630+ }
1631+ return otherExprId != 0 ? otherExprId : tok->exprId ();
1632+ }
1633+
16141634 struct ExprIdKey {
16151635 std::string parentOp;
16161636 nonneg int operand1;
@@ -1645,8 +1665,8 @@ namespace {
16451665
16461666 ExprIdKey key;
16471667 key.parentOp = tok->astParent ()->str ();
1648- key.operand1 = op1 ? op1-> exprId () : 0 ;
1649- key.operand2 = op2 ? op2-> exprId () : 0 ;
1668+ key.operand1 = getExprIdForOperand (op1) ;
1669+ key.operand2 = getExprIdForOperand (op2) ;
16501670
16511671 if (tok->astParent ()->isCast () && tok->astParent ()->str () == " (" ) {
16521672 const Token* typeStartToken;
@@ -1665,19 +1685,6 @@ namespace {
16651685 key.parentOp += type;
16661686 }
16671687
1668- for (const auto & ref: followAllReferences (op1)) {
1669- if (ref.token ->exprId () != 0 ) { // cppcheck-suppress useStlAlgorithm
1670- key.operand1 = ref.token ->exprId ();
1671- break ;
1672- }
1673- }
1674- for (const auto & ref: followAllReferences (op2)) {
1675- if (ref.token ->exprId () != 0 ) { // cppcheck-suppress useStlAlgorithm
1676- key.operand2 = ref.token ->exprId ();
1677- break ;
1678- }
1679- }
1680-
16811688 if (key.operand1 > key.operand2 && key.operand2 &&
16821689 Token::Match (tok->astParent (), " %or%|%oror%|+|*|&|&&|^|==|!=" )) {
16831690 // In C++ the order of operands of + might matter
0 commit comments