Skip to content

Commit 8589275

Browse files
committed
[clang] Avoid printing overly large integer/_BitInt numbers in static assertion failure diagnostics #71675
1 parent f8ee577 commit 8589275

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,10 @@ Improvements to Clang's diagnostics
642642
#GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
643643
#GH36703, #GH32903, #GH23312, #GH69874.
644644

645+
- Improved the performance of static assertions envolving large integers by
646+
using hex format instead of string.
647+
648+
Fixes #GH71675
645649

646650
Improvements to Clang's time-trace
647651
----------------------------------

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "clang/Sema/SemaOpenMP.h"
4848
#include "clang/Sema/Template.h"
4949
#include "clang/Sema/TemplateDeduction.h"
50+
#include "llvm/ADT/APSInt.h"
5051
#include "llvm/ADT/ArrayRef.h"
5152
#include "llvm/ADT/STLExtras.h"
5253
#include "llvm/ADT/StringExtras.h"
@@ -17575,7 +17576,21 @@ static bool ConvertAPValueToString(const APValue &V, QualType T,
1757517576
break;
1757617577
}
1757717578
}
17578-
V.getInt().toString(Str);
17579+
17580+
llvm::APSInt vInt = V.getInt();
17581+
if (llvm::APSInt::compareValues(
17582+
vInt, llvm::APSInt::getUnsigned(
17583+
std::numeric_limits<uint64_t>::max())) >= 0 ||
17584+
vInt < std::numeric_limits<int64_t>::min()) {
17585+
// The value of cutSize is not special, it is just a number of
17586+
// characters that gives us enough info without losing readability
17587+
const int cutSize = 20;
17588+
vInt.toString(Str, 16);
17589+
Str.erase(Str.begin() + cutSize, Str.end() - cutSize);
17590+
Str.insert(Str.begin() + cutSize, 3, '.');
17591+
} else {
17592+
vInt.toString(Str);
17593+
}
1757917594
}
1758017595

1758117596
break;

clang/test/AST/ByteCode/intap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ typedef unsigned __int128 uint128_t;
7979
static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
8080
static_assert(UINT128_MAX == -1, "");
8181
static_assert(UINT128_MAX == 1, ""); // both-error {{static assertion failed}} \
82-
// both-note {{'340282366920938463463374607431768211455 == 1'}}
82+
// both-note {{'FFFFFFFFFFFFFFFFFFFF...FFFFFFFFFFFFFFFFFFFF == 1'}}
8383

8484
static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
8585
static_assert(INT128_MAX != 0, "");
8686
static_assert(INT128_MAX == 0, ""); // both-error {{failed}} \
87-
// both-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}
87+
// both-note {{evaluates to '7FFFFFFFFFFFFFFFFFFF...FFFFFFFFFFFFFFFFFFFF == 0'}}
8888
static const __int128_t INT128_MIN = -INT128_MAX - 1;
8989

9090

@@ -105,14 +105,14 @@ namespace i128 {
105105
static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
106106
static_assert(UINT128_MAX == -1, "");
107107
static_assert(UINT128_MAX == 1, ""); // both-error {{static assertion failed}} \
108-
// both-note {{'340282366920938463463374607431768211455 == 1'}}
108+
// both-note {{'FFFFFFFFFFFFFFFFFFFF...FFFFFFFFFFFFFFFFFFFF == 1'}}
109109

110110
constexpr uint128_t TooMuch = UINT128_MAX * 2;
111111

112112
static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
113113
static_assert(INT128_MAX != 0, "");
114114
static_assert(INT128_MAX == 0, ""); // both-error {{failed}} \
115-
// both-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}
115+
// both-note {{evaluates to '7FFFFFFFFFFFFFFFFFFF...FFFFFFFFFFFFFFFFFFFF == 0'}}
116116

117117
constexpr int128_t TooMuch2 = INT128_MAX * INT128_MAX; // both-error {{must be initialized by a constant expression}} \
118118
// both-note {{value 28948022309329048855892746252171976962977213799489202546401021394546514198529 is outside the range of representable}}

clang/test/Sema/enum.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ enum GH59352 { // expected-warning {{enumeration values exceed range of largest
196196
BigVal = 66666666666666666666wb
197197
};
198198
_Static_assert(BigVal == 66666666666666666666wb); /* expected-error {{static assertion failed due to requirement 'BigVal == 66666666666666666666wb'}}
199-
expected-note {{expression evaluates to '11326434445538011818 == 66666666666666666666'}}
199+
expected-note {{expression evaluates to '11326434445538011818 == 39D2F941E420AAAAA<U+0000><U+0000><U+0000>...<U+0000><U+0000><U+0000>39D2F941E420AAAAA'}}
200200
*/
201201
_Static_assert(
202202
_Generic(BigVal, // expected-error {{static assertion failed}}

clang/test/SemaCXX/static-assert.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ int f() {
255255
}
256256
}
257257

258+
namespace GH71675 {
259+
constexpr unsigned _BitInt(__BITINT_MAXWIDTH__ >> 6) F = ~0; // expected-warning {{Clang extension}}
260+
static_assert(F == 1,""); // expected-error {{static assertion failed due to requirement 'F == 1'}} \
261+
// expected-note {{expression evaluates to 'FFFFFFFFFFFFFFFFFFFF...FFFFFFFFFFFFFFFFFFFF == 1'}}
262+
} // namespace GH71675
263+
258264
namespace Diagnostics {
259265
/// No notes for literals.
260266
static_assert(false, ""); // expected-error {{failed}}

0 commit comments

Comments
 (0)