[llvm] [clang] [clang-tools-extra] [CLANG] Add warning when INF or NAN are used in a binary operation or as function argument in fast math mode. (PR #76873)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 15 05:14:16 PST 2024


================
@@ -13861,6 +13897,36 @@ Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType,
     CheckPPCMMAType(RetValExp->getType(), ReturnLoc);
 }
 
+/// Diagnose comparison to NAN or INFINITY in fast math modes.
+/// The comparison to NaN or INFINITY is always false in
+/// fast modes: float evaluation will not result in inf or nan.
+void Sema::CheckInfNaNFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS,
----------------
AaronBallman wrote:

The basic idea I have is that it's UB to generate a constant infinity or NaN if infinity/NaN is disabled. Instead of performing special logic for float comparisons, I think the logic should live in `Sema::ActOnNumericConstant()` so that we capture any time a float or infinity constant is used. e.g. `inf + 1` should also diagnose.

Then the tautological comparison component to this could be implemented by looking for calls to `__builtin_isinf` or `__builtin_isnan` and claim those are tautologically false (as a follow-up patch). WDYT?

https://github.com/llvm/llvm-project/pull/76873


More information about the cfe-commits mailing list