[clang] [clang] Emit bad shift warnings (PR #70307)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 26 06:24:21 PDT 2024


================
@@ -11444,9 +11444,12 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
   llvm::APSInt Right = RHSResult.Val.getInt();
 
   if (Right.isNegative()) {
-    S.DiagRuntimeBehavior(Loc, RHS.get(),
-                          S.PDiag(diag::warn_shift_negative)
-                            << RHS.get()->getSourceRange());
+    if (S.ExprEvalContexts.back().isConstantEvaluated())
+      S.Diag(Loc, diag::warn_shift_negative) << RHS.get()->getSourceRange();
+    else
+      S.DiagRuntimeBehavior(Loc, RHS.get(),
+                            S.PDiag(diag::warn_shift_negative)
+                              << RHS.get()->getSourceRange());
----------------
AaronBallman wrote:

I still don't think this is the correct fix -- I think the issue is that ExprConstant.cpp needs to be taught about these as undefined behaviors. Notice how GCC correctly emits the pedantic diagnostic but Clang does not: https://godbolt.org/z/arb8Y9G9q

When I step through in the debugger, it seems that we do catch the issue in `handleIntIntBinOp()` but we're suppressing diagnostics for it in C more broadly. That seems more likely to be the root cause of the issue. (Basically, we want the diagnostics to be split between SemaExpr.cpp for runtime reachable diagnostics and ExprConstant.cpp for constant expressions.)

CC @tbaederr for opinions on constant expression behavior here.

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


More information about the cfe-commits mailing list