Skip to content

Commit 985a72b

Browse files
committed
[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
1 parent f471b49 commit 985a72b

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2798,7 +2798,7 @@ static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
27982798
if (Info.checkingForUndefinedBehavior())
27992799
Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
28002800
diag::warn_integer_constant_overflow)
2801-
<< toString(Result, 10) << E->getType();
2801+
<< toString(Result, 10) << E->getType() << E->getSourceRange();
28022802
return HandleOverflow(Info, E, Value, E->getType());
28032803
}
28042804
return true;
@@ -13643,7 +13643,7 @@ bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
1364313643
if (Info.checkingForUndefinedBehavior())
1364413644
Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
1364513645
diag::warn_integer_constant_overflow)
13646-
<< toString(Value, 10) << E->getType();
13646+
<< toString(Value, 10) << E->getType() << E->getSourceRange();
1364713647

1364813648
if (!HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
1364913649
E->getType()))

clang/lib/AST/Interp/Interp.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS,
271271
SmallString<32> Trunc;
272272
Value.trunc(Result.bitWidth()).toString(Trunc, 10);
273273
auto Loc = E->getExprLoc();
274-
S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
274+
S.report(Loc, diag::warn_integer_constant_overflow)
275+
<< Trunc << Type << E->getSourceRange();
275276
return true;
276277
} else {
277278
S.CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
@@ -478,7 +479,8 @@ bool Neg(InterpState &S, CodePtr OpPC) {
478479
SmallString<32> Trunc;
479480
NegatedValue.trunc(Result.bitWidth()).toString(Trunc, 10);
480481
auto Loc = E->getExprLoc();
481-
S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
482+
S.report(Loc, diag::warn_integer_constant_overflow)
483+
<< Trunc << Type << E->getSourceRange();
482484
return true;
483485
}
484486

@@ -531,7 +533,8 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
531533
SmallString<32> Trunc;
532534
APResult.trunc(Result.bitWidth()).toString(Trunc, 10);
533535
auto Loc = E->getExprLoc();
534-
S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
536+
S.report(Loc, diag::warn_integer_constant_overflow)
537+
<< Trunc << Type << E->getSourceRange();
535538
return true;
536539
}
537540

clang/test/Misc/constexpr-source-ranges.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ constexpr int ints(int a, int b, int c, int d) {
3434
}
3535
static_assert(ints(1, div(true, false), 2, div(false, true)) == 1, "");
3636
// CHECK: constexpr-source-ranges.cpp:35:23:{35:23-35:39}
37+
38+
namespace overflow {
39+
// CHECK: :{[[@LINE+1]]:9-[[@LINE+1]]:29}:
40+
int x = -1 + __INT_MAX__ + 2 + 3;
41+
// CHECK: :{[[@LINE+1]]:9-[[@LINE+1]]:19}:
42+
int a = -(1 << 31) + 1;
43+
}

0 commit comments

Comments
 (0)