[clang] 985a72b - [clang][Diagnostics] Provide source range to integer-overflow warnings

Takuya Shimizu via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 19 06:05:46 PDT 2023


Author: Takuya Shimizu
Date: 2023-08-19T22:05:12+09:00
New Revision: 985a72b6b3e74f0d29780b2a97b5817473338ffe

URL: https://github.com/llvm/llvm-project/commit/985a72b6b3e74f0d29780b2a97b5817473338ffe
DIFF: https://github.com/llvm/llvm-project/commit/985a72b6b3e74f0d29780b2a97b5817473338ffe.diff

LOG: [clang][Diagnostics] Provide source range to integer-overflow warnings

BEFORE:

```
overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    1 | int x = __INT_MAX__ + 1 + 3;
      |                     ^
overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    2 | int a = -(1 << 31) + 1;
      |         ^
```
AFTER:

```
overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    1 | int x = __INT_MAX__ + 1 + 3;
      |         ~~~~~~~~~~~~^~~
overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    2 | int a = -(1 << 31) + 1;
      |         ^~~~~~~~~~
```

Reviewed By: tbaeder
Differential Revision: https://reviews.llvm.org/D157383

Added: 
    

Modified: 
    clang/lib/AST/ExprConstant.cpp
    clang/lib/AST/Interp/Interp.h
    clang/test/Misc/constexpr-source-ranges.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 9ee9fccfd461f5..9f4c758b81303c 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2798,7 +2798,7 @@ static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
     if (Info.checkingForUndefinedBehavior())
       Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
                                        diag::warn_integer_constant_overflow)
-          << toString(Result, 10) << E->getType();
+          << toString(Result, 10) << E->getType() << E->getSourceRange();
     return HandleOverflow(Info, E, Value, E->getType());
   }
   return true;
@@ -13643,7 +13643,7 @@ bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
       if (Info.checkingForUndefinedBehavior())
         Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
                                          diag::warn_integer_constant_overflow)
-            << toString(Value, 10) << E->getType();
+            << toString(Value, 10) << E->getType() << E->getSourceRange();
 
       if (!HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
                           E->getType()))

diff  --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index aebab9023a3580..79995b5a74897c 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -271,7 +271,8 @@ bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS,
     SmallString<32> Trunc;
     Value.trunc(Result.bitWidth()).toString(Trunc, 10);
     auto Loc = E->getExprLoc();
-    S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+    S.report(Loc, diag::warn_integer_constant_overflow)
+        << Trunc << Type << E->getSourceRange();
     return true;
   } else {
     S.CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
@@ -478,7 +479,8 @@ bool Neg(InterpState &S, CodePtr OpPC) {
     SmallString<32> Trunc;
     NegatedValue.trunc(Result.bitWidth()).toString(Trunc, 10);
     auto Loc = E->getExprLoc();
-    S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+    S.report(Loc, diag::warn_integer_constant_overflow)
+        << Trunc << Type << E->getSourceRange();
     return true;
   }
 
@@ -531,7 +533,8 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
     SmallString<32> Trunc;
     APResult.trunc(Result.bitWidth()).toString(Trunc, 10);
     auto Loc = E->getExprLoc();
-    S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+    S.report(Loc, diag::warn_integer_constant_overflow)
+        << Trunc << Type << E->getSourceRange();
     return true;
   }
 

diff  --git a/clang/test/Misc/constexpr-source-ranges.cpp b/clang/test/Misc/constexpr-source-ranges.cpp
index 15fd54847f4762..f21373eff3a95c 100644
--- a/clang/test/Misc/constexpr-source-ranges.cpp
+++ b/clang/test/Misc/constexpr-source-ranges.cpp
@@ -34,3 +34,10 @@ constexpr int ints(int a, int b, int c, int d) {
 }
 static_assert(ints(1, div(true, false), 2, div(false, true)) == 1, "");
 // CHECK: constexpr-source-ranges.cpp:35:23:{35:23-35:39}
+
+namespace overflow {
+// CHECK:      :{[[@LINE+1]]:9-[[@LINE+1]]:29}:
+int x = -1 + __INT_MAX__ + 2 + 3;
+// CHECK:      :{[[@LINE+1]]:9-[[@LINE+1]]:19}:
+int a = -(1 << 31) + 1;
+}


        


More information about the cfe-commits mailing list