Skip to content

Commit 30ed47b

Browse files
committed
Update printing APInt numbers with separators
The new constant interpreter can also emit integer constant overflow warnings. The code was updated to also print these with separators.
1 parent 8aeea6d commit 30ed47b

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,7 +2778,8 @@ static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
27782778
if (Info.checkingForUndefinedBehavior())
27792779
Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
27802780
diag::warn_integer_constant_overflow)
2781-
<< toString(Result, 10, Result.isSigned(), /*formatAsCLiteral=*/false, /*upperCase=*/true, /*insertSeparators=*/true)
2781+
<< toString(Result, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
2782+
/*upperCase=*/true, /*insertSeparators=*/true)
27822783
<< E->getType() << E->getSourceRange();
27832784
return HandleOverflow(Info, E, Value, E->getType());
27842785
}
@@ -13858,7 +13859,8 @@ bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
1385813859
if (Info.checkingForUndefinedBehavior())
1385913860
Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
1386013861
diag::warn_integer_constant_overflow)
13861-
<< toString(Value, 10, Value.isSigned(), false, true, true)
13862+
<< toString(Value, 10, Value.isSigned(), /*formatAsCLiteral=*/false,
13863+
/*upperCase=*/true, /*insertSeparators=*/true)
1386213864
<< E->getType() << E->getSourceRange();
1386313865

1386413866
if (!HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),

clang/lib/AST/Interp/Interp.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS,
285285
QualType Type = E->getType();
286286
if (S.checkingForUndefinedBehavior()) {
287287
SmallString<32> Trunc;
288-
Value.trunc(Result.bitWidth()).toString(Trunc, 10);
288+
Value.trunc(Result.bitWidth())
289+
.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
290+
/*upperCase=*/true, /*insertSeparators=*/true);
289291
auto Loc = E->getExprLoc();
290292
S.report(Loc, diag::warn_integer_constant_overflow)
291293
<< Trunc << Type << E->getSourceRange();
@@ -497,7 +499,9 @@ bool Neg(InterpState &S, CodePtr OpPC) {
497499

498500
if (S.checkingForUndefinedBehavior()) {
499501
SmallString<32> Trunc;
500-
NegatedValue.trunc(Result.bitWidth()).toString(Trunc, 10);
502+
NegatedValue.trunc(Result.bitWidth())
503+
.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
504+
/*upperCase=*/true, /*insertSeparators=*/true);
501505
auto Loc = E->getExprLoc();
502506
S.report(Loc, diag::warn_integer_constant_overflow)
503507
<< Trunc << Type << E->getSourceRange();
@@ -554,7 +558,9 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
554558
QualType Type = E->getType();
555559
if (S.checkingForUndefinedBehavior()) {
556560
SmallString<32> Trunc;
557-
APResult.trunc(Result.bitWidth()).toString(Trunc, 10);
561+
APResult.trunc(Result.bitWidth())
562+
.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
563+
/*upperCase=*/true, /*insertSeparators=*/true);
558564
auto Loc = E->getExprLoc();
559565
S.report(Loc, diag::warn_integer_constant_overflow)
560566
<< Trunc << Type << E->getSourceRange();

clang/test/AST/Interp/c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ int chooseexpr[__builtin_choose_expr(1, 1, expr)];
9191

9292
int somefunc(int i) {
9393
return (i, 65537) * 65537; // all-warning {{left operand of comma operator has no effect}} \
94-
// all-warning {{overflow in expression; result is 131'073}}
94+
// all-warning {{overflow in expression; result is 131'073 with type 'int'}}
9595
}
9696

9797
/// FIXME: The following test is incorrect in the new interpreter.

llvm/lib/Support/APInt.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,9 +2190,6 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
21902190

21912191
// Number of digits in a group between separators.
21922192
unsigned Grouping = (Radix == 8 || Radix == 10) ? 3 : 4;
2193-
if (Radix == 8 || Radix == 10) {
2194-
Grouping = 3;
2195-
}
21962193

21972194
// First, check for a zero value and just short circuit the logic below.
21982195
if (isZero()) {

0 commit comments

Comments
 (0)