=?utf-8?q?FĂ©lix?= Cloutier <fclout...@apple.com> Message-ID: In-Reply-To: <llvm/llvm-project/pull/65684/cl...@github.com>
================ @@ -14160,10 +14193,58 @@ static void AnalyzeComparison(Sema &S, BinaryOperator *E) { return; } - S.DiagRuntimeBehavior(E->getOperatorLoc(), E, - S.PDiag(diag::warn_mixed_sign_comparison) - << LHS->getType() << RHS->getType() - << LHS->getSourceRange() << RHS->getSourceRange()); + // For `S < U`, propose something like `S < 0 || (typeof(U))S < U`. + // The whole operation needs to have no side effects, since the signed + // operand will be duplicated, and the unsigned operand might not execute + // anymore. + auto MixedSignDiag = S.PDiag(diag::warn_mixed_sign_comparison) + << (signedOperand == RHS) << signedOperand->getType() + << T << signedOperand->getSourceRange(); + if (!E->HasSideEffects(S.Context)) { + bool ParenthesizeCheck = + !TopLevelExpr || E != TopLevelExpr->IgnoreImplicit(); + std::string Prefix; + llvm::raw_string_ostream PrefixS(Prefix); + if (ParenthesizeCheck) + PrefixS << '('; + auto TokRange = + CharSourceRange::getTokenRange(signedOperand->getSourceRange()); + bool Invalid; + PrefixS << Lexer::getSourceText(TokRange, S.SourceMgr, S.getLangOpts(), + &Invalid); + if (!Invalid) { + bool ParenthesizeForCast = S.getLangOpts().CPlusPlus || + isa<BinaryOperator>(signedOperand) || + isa<ConditionalOperator>(signedOperand); ---------------- AaronBallman wrote: ```suggestion isa<BinaryOperator, ConditionalOperator>(signedOperand); ``` https://github.com/llvm/llvm-project/pull/65684 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits