[clang] [clang][ExprConst] Add diagnostics for invalid binary arithmetic (PR #118475)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 3 11:01:44 PST 2024


================
@@ -14548,8 +14548,21 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
         return Error(E);
       const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();
       const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();
-      if (!LHSExpr || !RHSExpr)
-        return Error(E);
+      if (!LHSExpr || !RHSExpr) {
+        std::string LHS = LHSValue.toString(Info.Ctx, E->getLHS()->getType());
+        std::string RHS = RHSValue.toString(Info.Ctx, E->getRHS()->getType());
+        Info.FFDiag(E, diag::note_constexpr_pointer_arith_unspecified)
+            << LHS << RHS;
+        return false;
+      }
+
+      if (ArePotentiallyOverlappingStringLiterals(Info, LHSValue, RHSValue)) {
+        std::string LHS = LHSValue.toString(Info.Ctx, E->getLHS()->getType());
+        std::string RHS = RHSValue.toString(Info.Ctx, E->getRHS()->getType());
+        Info.FFDiag(E, diag::note_constexpr_literal_arith) << LHS << RHS;
----------------
zygoloid wrote:

Do you think it's worth factoring out a lambda to do this `toString` work that's shared between these two diagnostics? (I'd imagine we'd want the same thing for the other two calls to `Error(E)` for pointer subtractions.)

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


More information about the cfe-commits mailing list