Skip to content

Commit

Permalink
[clang] Use separator for large numeric values in overflow diagnostic…
Browse files Browse the repository at this point in the history
… (#80939)

Add functionality to APInt::toString() that allows it to insert
separators between groups of digits, using the C++ literal
separator ' between groups.

Fixes issue #58228

Reviewers:  @AaronBallman, @cjdb, @tbaederr
  • Loading branch information
Atousa authored Mar 5, 2024
1 parent 341d674 commit c00c901
Show file tree
Hide file tree
Showing 16 changed files with 195 additions and 126 deletions.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ Improvements to Clang's diagnostics
- The ``-Wshorten-64-to-32`` diagnostic is now grouped under ``-Wimplicit-int-conversion`` instead
of ``-Wconversion``. Fixes #GH69444.

- Clang now uses thousand separators when printing large numbers in integer overflow diagnostics.
Fixes #GH80939.

- Clang now diagnoses friend declarations with an ``enum`` elaborated-type-specifier in language modes after C++98.

- Added diagnostics for C11 keywords being incompatible with language standards
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2778,7 +2778,9 @@ 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() << E->getSourceRange();
<< toString(Result, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
/*UpperCase=*/true, /*InsertSeparators=*/true)
<< E->getType() << E->getSourceRange();
return HandleOverflow(Info, E, Value, E->getType());
}
return true;
Expand Down Expand Up @@ -13910,7 +13912,9 @@ 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() << E->getSourceRange();
<< toString(Value, 10, Value.isSigned(), /*formatAsCLiteral=*/false,
/*UpperCase=*/true, /*InsertSeparators=*/true)
<< E->getType() << E->getSourceRange();

if (!HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
E->getType()))
Expand Down
12 changes: 9 additions & 3 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS,
QualType Type = E->getType();
if (S.checkingForUndefinedBehavior()) {
SmallString<32> Trunc;
Value.trunc(Result.bitWidth()).toString(Trunc, 10);
Value.trunc(Result.bitWidth())
.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
/*UpperCase=*/true, /*InsertSeparators=*/true);
auto Loc = E->getExprLoc();
S.report(Loc, diag::warn_integer_constant_overflow)
<< Trunc << Type << E->getSourceRange();
Expand Down Expand Up @@ -499,7 +501,9 @@ bool Neg(InterpState &S, CodePtr OpPC) {

if (S.checkingForUndefinedBehavior()) {
SmallString<32> Trunc;
NegatedValue.trunc(Result.bitWidth()).toString(Trunc, 10);
NegatedValue.trunc(Result.bitWidth())
.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
/*UpperCase=*/true, /*InsertSeparators=*/true);
auto Loc = E->getExprLoc();
S.report(Loc, diag::warn_integer_constant_overflow)
<< Trunc << Type << E->getSourceRange();
Expand Down Expand Up @@ -561,7 +565,9 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
QualType Type = E->getType();
if (S.checkingForUndefinedBehavior()) {
SmallString<32> Trunc;
APResult.trunc(Result.bitWidth()).toString(Trunc, 10);
APResult.trunc(Result.bitWidth())
.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
/*UpperCase=*/true, /*InsertSeparators=*/true);
auto Loc = E->getExprLoc();
S.report(Loc, diag::warn_integer_constant_overflow)
<< Trunc << Type << E->getSourceRange();
Expand Down
2 changes: 1 addition & 1 deletion clang/test/AST/Interp/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int chooseexpr[__builtin_choose_expr(1, 1, expr)];

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

/// FIXME: The following test is incorrect in the new interpreter.
Expand Down
2 changes: 1 addition & 1 deletion clang/test/C/drs/dr0xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ _Static_assert(__builtin_types_compatible_p(struct S { int a; }, union U { int a
*/
void dr031(int i) {
switch (i) {
case __INT_MAX__ + 1: break; /* expected-warning {{overflow in expression; result is -2147483648 with type 'int'}} */
case __INT_MAX__ + 1: break; /* expected-warning {{overflow in expression; result is -2'147'483'648 with type 'int'}} */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wswitch"
/* Silence the targets which issue:
Expand Down
2 changes: 1 addition & 1 deletion clang/test/C/drs/dr2xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void dr258(void) {
void dr261(void) {
/* This is still an integer constant expression despite the overflow. */
enum e1 {
ex1 = __INT_MAX__ + 1 /* expected-warning {{overflow in expression; result is -2147483648 with type 'int'}} */
ex1 = __INT_MAX__ + 1 /* expected-warning {{overflow in expression; result is -2'147'483'648 with type 'int'}} */
};

/* This is not an integer constant expression, because of the comma operator,
Expand Down
100 changes: 50 additions & 50 deletions clang/test/Sema/integer-overflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,169 +11,169 @@ uint64_t f0(uint64_t);
uint64_t f1(uint64_t, uint32_t);
uint64_t f2(uint64_t, ...);

static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536'870'912 with type 'int'}}

uint64_t check_integer_overflows(int i) {
// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
uint64_t overflow = 4608 * 1024 * 1024,
// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
overflow2 = (uint64_t)(4608 * 1024 * 1024),
// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
overflow3 = (uint64_t)(4608 * 1024 * 1024 * i),
// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
overflow4 = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL),
// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
multi_overflow = (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024));

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
overflow += overflow2 = overflow3 = (uint64_t)(4608 * 1024 * 1024);
// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
overflow += overflow2 = overflow3 = 4608 * 1024 * 1024;

uint64_t not_overflow = 4608 * 1024 * 1024ULL;
uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);

// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
overflow = 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0;

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
overflow = 0 ? 0 : 4608 * 1024 * 1024;

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
if (4608 * 1024 * 1024)
return 0;

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
if ((uint64_t)(4608 * 1024 * 1024))
return 1;

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
if ((uint64_t)(4608 * 1024 * 1024))
return 2;

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
if ((uint64_t)(4608 * 1024 * 1024 * i))
return 3;

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))
return 4;

// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)))
return 5;

switch (i) {
// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
case 4608 * 1024 * 1024:
return 6;
// expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 537'919'488 with type 'int'}}
case (uint64_t)(4609 * 1024 * 1024):
return 7;
// expected-error@+1 {{expression is not an integer constant expression}}
case ((uint64_t)(4608 * 1024 * 1024 * i)):
return 8;
// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):
return 9;
// expected-warning@+2 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+2 2{{overflow in expression; result is 536'870'912 with type 'int'}}
// expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}}
case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))):
return 10;
}

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
while (4608 * 1024 * 1024);

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
while ((uint64_t)(4608 * 1024 * 1024));

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
while ((uint64_t)(4608 * 1024 * 1024));

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
while ((uint64_t)(4608 * 1024 * 1024 * i));

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));

// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
do { } while (4608 * 1024 * 1024);

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
do { } while ((uint64_t)(4608 * 1024 * 1024));

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
do { } while ((uint64_t)(4608 * 1024 * 1024));

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
do { } while ((uint64_t)(4608 * 1024 * 1024 * i));

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));

// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));

// expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}
// expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}
// expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}
for (uint64_t i = 4608 * 1024 * 1024;
(uint64_t)(4608 * 1024 * 1024);
i += (uint64_t)(4608 * 1024 * 1024 * i));

// expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}
// expected-warning@+3 2{{overflow in expression; result is 536'870'912 with type 'int'}}
// expected-warning@+3 2{{overflow in expression; result is 536'870'912 with type 'int'}}
for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL);
((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))));

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
_Complex long long x = 4608 * 1024 * 1024;

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
(__real__ x) = 4608 * 1024 * 1024;

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
(__imag__ x) = 4608 * 1024 * 1024;

// expected-warning@+4 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+4 {{overflow in expression; result is 536'870'912 with type 'int'}}
// expected-warning@+3 {{array index 536870912 is past the end of the array (that has type 'uint64_t[10]' (aka 'unsigned long long[10]'))}}
// expected-note@+1 {{array 'a' declared here}}
uint64_t a[10];
a[4608 * 1024 * 1024] = 1i;

// expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+2 {{overflow in expression; result is 536'870'912 with type 'int'}}
uint64_t *b;
uint64_t b2 = b[4608 * 1024 * 1024] + 1;

// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
(void)((i ? (4608 * 1024 * 1024) : (4608 * 1024 * 1024)) + 1);

// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}
return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
}

void check_integer_overflows_in_function_calls(void) {
// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
(void)f0(4608 * 1024 * 1024);

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
uint64_t x = f0(4608 * 1024 * 1024);

// expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+2 {{overflow in expression; result is 536'870'912 with type 'int'}}
uint64_t (*f0_ptr)(uint64_t) = &f0;
(void)(*f0_ptr)(4608 * 1024 * 1024);

// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}
(void)f2(0, f0(4608 * 1024 * 1024));
}
void check_integer_overflows_in_array_size(void) {
int arr[4608 * 1024 * 1024]; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
int arr[4608 * 1024 * 1024]; // expected-warning {{overflow in expression; result is 536'870'912 with type 'int'}}
}

struct s {
Expand Down
6 changes: 3 additions & 3 deletions clang/test/Sema/switch-1.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int f(int i) {
switch (i) {
case 2147483647 + 2:
#if (__cplusplus <= 199711L) // C or C++03 or earlier modes
// expected-warning@-2 {{overflow in expression; result is -2147483647 with type 'int'}}
// expected-warning@-2 {{overflow in expression; result is -2'147'483'647 with type 'int'}}
#else
// expected-error@-4 {{case value is not a constant expression}} \
// expected-note@-4 {{value 2147483649 is outside the range of representable values of type 'int'}}
Expand All @@ -23,7 +23,7 @@ int f(int i) {
return 2;
case (123456 *789012) + 1:
#if (__cplusplus <= 199711L)
// expected-warning@-2 {{overflow in expression; result is -1375982336 with type 'int'}}
// expected-warning@-2 {{overflow in expression; result is -1'375'982'336 with type 'int'}}
#else
// expected-error@-4 {{case value is not a constant expression}} \
// expected-note@-4 {{value 97408265472 is outside the range of representable values of type 'int'}}
Expand All @@ -47,7 +47,7 @@ int f(int i) {
case 2147483647:
return 0;
}
return (i, 65537) * 65537; // expected-warning {{overflow in expression; result is 131073 with type 'int'}} \
return (i, 65537) * 65537; // expected-warning {{overflow in expression; result is 131'073 with type 'int'}} \
// expected-warning {{left operand of comma operator has no effect}}
}

Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaCXX/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ enum { overflow = 123456 * 234567 };
// expected-warning@-2 {{not an integral constant expression}}
// expected-note@-3 {{value 28958703552 is outside the range of representable values}}
#else
// expected-warning@-5 {{overflow in expression; result is -1106067520 with type 'int'}}
// expected-warning@-5 {{overflow in expression; result is -1'106'067'520 with type 'int'}}
#endif

// FIXME: This is not consistent with the above case.
Expand All @@ -112,7 +112,7 @@ enum NoFold : int { overflow2 = 123456 * 234567 };
// expected-error@-2 {{enumerator value is not a constant expression}}
// expected-note@-3 {{value 28958703552 is outside the range of representable values}}
#else
// expected-warning@-5 {{overflow in expression; result is -1106067520 with type 'int'}}
// expected-warning@-5 {{overflow in expression; result is -1'106'067'520 with type 'int'}}
// expected-warning@-6 {{extension}}
#endif

Expand Down
Loading

0 comments on commit c00c901

Please sign in to comment.