Skip to content

Commit f58ea20

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

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,11 @@ Improvements to Clang's diagnostics
647647
#GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
648648
#GH36703, #GH32903, #GH23312, #GH69874.
649649

650-
650+
- Improved the performance of static assertions envolving large integers by
651+
using hex format instead of string.
652+
653+
Fixes #GH71675
654+
651655
Improvements to Clang's time-trace
652656
----------------------------------
653657

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
@@ -87,12 +87,12 @@ typedef unsigned __int128 uint128_t;
8787
static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
8888
static_assert(UINT128_MAX == -1, "");
8989
static_assert(UINT128_MAX == 1, ""); // both-error {{static assertion failed}} \
90-
// both-note {{'340282366920938463463374607431768211455 == 1'}}
90+
// both-note {{'FFFFFFFFFFFFFFFFFFFF...FFFFFFFFFFFFFFFFFFFF == 1'}}
9191

9292
static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
9393
static_assert(INT128_MAX != 0, "");
9494
static_assert(INT128_MAX == 0, ""); // both-error {{failed}} \
95-
// both-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}
95+
// both-note {{evaluates to '7FFFFFFFFFFFFFFFFFFF...FFFFFFFFFFFFFFFFFFFF == 0'}}
9696
static const __int128_t INT128_MIN = -INT128_MAX - 1;
9797

9898

@@ -113,14 +113,14 @@ namespace i128 {
113113
static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
114114
static_assert(UINT128_MAX == -1, "");
115115
static_assert(UINT128_MAX == 1, ""); // both-error {{static assertion failed}} \
116-
// both-note {{'340282366920938463463374607431768211455 == 1'}}
116+
// both-note {{'FFFFFFFFFFFFFFFFFFFF...FFFFFFFFFFFFFFFFFFFF == 1'}}
117117

118118
constexpr uint128_t TooMuch = UINT128_MAX * 2;
119119

120120
static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
121121
static_assert(INT128_MAX != 0, "");
122122
static_assert(INT128_MAX == 0, ""); // both-error {{failed}} \
123-
// both-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}
123+
// both-note {{evaluates to '7FFFFFFFFFFFFFFFFFFF...FFFFFFFFFFFFFFFFFFFF == 0'}}
124124

125125
constexpr int128_t TooMuch2 = INT128_MAX * INT128_MAX; // both-error {{must be initialized by a constant expression}} \
126126
// 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)