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

Budimir Aranđelović via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 12 01:24:53 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());
----------------
budimirarandjelovicsyrmia wrote:

Diagnose is caught in handleIntIntBinOp() and is emitted for C++ in VerifyIntegerConstantExpression(). There are two reasons why diagnostic could be emitted in case of C++:
	1) evaluating expression as rvalue fails or evaluation result has side effects or it is not an integer; evaluating expression as rvalue fails if function handleIntIntBinOp() returns false
	2) folding is allowed (CanFold == AllowFoldKind::AllowFold); this value depends on place where constant expression is defined; eg. in case on enum folding is allowed and in case of static assertion it is not allowed
Also, diagnostic would not be emitted if Diagnoser is suppresed.

However, in C case there are no diagnostic for integer constant expression.

So I enabled diagnosing in C case (if Diagnoser is not suppressed). In function EvaluateKnownConstIntCheckOverflow() it is expected that evaluating rvalue is successful, so I keep handleIntIntBinOp() returning true.
For C++ diagnostic, I modified handleIntIntBinOp() to return false.

As folding is allowed for enum and not for static assertion, I added in tests to check both enum and static assertion diagnosing.

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


More information about the cfe-commits mailing list