@@ -569,6 +569,7 @@ matchRelationalIntegerConstantExpr(StringRef Id) {
569
569
std::string SwapId = (Id + " -swap" ).str ();
570
570
std::string NegateId = (Id + " -negate" ).str ();
571
571
std::string OverloadId = (Id + " -overload" ).str ();
572
+ std::string ConstId = (Id + " -const" ).str ();
572
573
573
574
const auto RelationalExpr = ignoringParenImpCasts (binaryOperator (
574
575
isComparisonOperator (), expr ().bind (Id),
@@ -600,7 +601,9 @@ matchRelationalIntegerConstantExpr(StringRef Id) {
600
601
cxxOperatorCallExpr (
601
602
hasAnyOverloadedOperatorName (" ==" , " !=" , " <" , " <=" , " >" , " >=" ),
602
603
// Filter noisy false positives.
603
- unless (isMacro ()), unless (isInTemplateInstantiation ()))
604
+ unless (isMacro ()), unless (isInTemplateInstantiation ()),
605
+ anyOf (hasLHS (ignoringParenImpCasts (integerLiteral ().bind (ConstId))),
606
+ hasRHS (ignoringParenImpCasts (integerLiteral ().bind (ConstId)))))
604
607
.bind (OverloadId);
605
608
606
609
return anyOf (RelationalExpr, CastExpr, NegateRelationalExpr,
@@ -674,16 +677,38 @@ static bool retrieveRelationalIntegerConstantExpr(
674
677
if (canOverloadedOperatorArgsBeModified (OverloadedOperatorExpr, false ))
675
678
return false ;
676
679
680
+ bool IntegerConstantIsFirstArg = false ;
681
+
677
682
if (const auto *Arg = OverloadedOperatorExpr->getArg (1 )) {
678
683
if (!Arg->isValueDependent () &&
679
- !Arg->isIntegerConstantExpr (*Result.Context ))
680
- return false ;
681
- }
682
- Symbol = OverloadedOperatorExpr->getArg (0 );
684
+ !Arg->isIntegerConstantExpr (*Result.Context )) {
685
+ IntegerConstantIsFirstArg = true ;
686
+ if (const auto *Arg = OverloadedOperatorExpr->getArg (0 )) {
687
+ if (!Arg->isValueDependent () &&
688
+ !Arg->isIntegerConstantExpr (*Result.Context ))
689
+ return false ;
690
+ } else
691
+ return false ;
692
+ }
693
+ } else
694
+ return false ;
695
+
696
+ Symbol = OverloadedOperatorExpr->getArg (IntegerConstantIsFirstArg ? 1 : 0 );
683
697
OperandExpr = OverloadedOperatorExpr;
684
698
Opcode = BinaryOperator::getOverloadedOpcode (OverloadedOperatorExpr->getOperator ());
685
699
686
- return BinaryOperator::isComparisonOp (Opcode);
700
+ if (!retrieveIntegerConstantExpr (Result, Id, Value, ConstExpr))
701
+ return false ;
702
+
703
+ if (!BinaryOperator::isComparisonOp (Opcode))
704
+ return false ;
705
+
706
+ // The call site of this function expects the constant on the RHS,
707
+ // so change the opcode accordingly.
708
+ if (IntegerConstantIsFirstArg)
709
+ Opcode = BinaryOperator::reverseComparisonOp (Opcode);
710
+
711
+ return true ;
687
712
} else {
688
713
return false ;
689
714
}
0 commit comments