[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
@@ -356,12 +356,10 @@ bool AddSubMulHelper(InterpState &S, CodePtr OpPC,
unsigned Bits, const T &LHS,
if (S.checkingForUndefinedBehavior()) {
const Expr *E = S.Current->getExpr(OpPC);
QualType Type = E->getType();
-SmallString<32> Trunc;
-Value.trunc(Result.bitWidth())
-.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
- /*UpperCase=*/true, /*InsertSeparators=*/true);
S.report(E->getExprLoc(), diag::warn_integer_constant_overflow)
-<< Trunc << Type << E->getSourceRange();
+<< formatDiagnosticInteger(Value.trunc(Result.bitWidth()),
+ Result.isSigned())
Sirraide wrote:
> outside of implementations of operator<<
As well as the two functions that format `int64_t`/`uint64_t` of course
https://github.com/llvm/llvm-project/pull/211785
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
@@ -356,12 +356,10 @@ bool AddSubMulHelper(InterpState &S, CodePtr OpPC,
unsigned Bits, const T &LHS,
if (S.checkingForUndefinedBehavior()) {
const Expr *E = S.Current->getExpr(OpPC);
QualType Type = E->getType();
-SmallString<32> Trunc;
-Value.trunc(Result.bitWidth())
-.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
- /*UpperCase=*/true, /*InsertSeparators=*/true);
S.report(E->getExprLoc(), diag::warn_integer_constant_overflow)
-<< Trunc << Type << E->getSourceRange();
+<< formatDiagnosticInteger(Value.trunc(Result.bitWidth()),
+ Result.isSigned())
Sirraide wrote:
Why are we calling the function manually here when that’s already done in the
`operator<<` implementations? This should just pass the APInt/APSInt or
whatever this is directly to the diagnostic builder
https://github.com/llvm/llvm-project/pull/211785
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
@@ -1429,19 +1429,33 @@ inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, return DB; } +inline constexpr uint64_t DiagnosticIntegerSeparatorThreshold = 100'000; + +inline std::string formatDiagnosticInteger(const llvm::APInt &Val, Sirraide wrote: I feel like the implementation of this should go in a `.cpp` file as it’s getting a bit long https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
@@ -1429,19 +1429,33 @@ inline const StreamingDiagnostic &operator<<(const
StreamingDiagnostic &DB,
return DB;
}
+inline constexpr uint64_t DiagnosticIntegerSeparatorThreshold = 100'000;
+
+inline std::string formatDiagnosticInteger(const llvm::APInt &Val,
+ bool Signed) {
+ bool Small = false;
+ if (Signed) {
+if (Val.getSignificantBits() <= 64) {
+ int64_t S = Val.getSExtValue();
+ uint64_t Magnitude = S < 0 ? -static_cast(S) : uint64_t(S);
+ Small = Magnitude < DiagnosticIntegerSeparatorThreshold;
+}
+ } else if (Val.getActiveBits() <= 64) {
+Small = Val.getZExtValue() < DiagnosticIntegerSeparatorThreshold;
+ }
Sirraide wrote:
```suggestion
if (Val.isSingleWord()) {
if (Signed)
Small = !Val.isMinSignedValue() && std::abs(Val.getSExtValue()) <
DiagnosticIntegerSeparatorThreshold;
else
Small = Val.getZExtValue() < DiagnosticIntegerSeparatorThreshold;
}
```
This deals with the issue that `-INT_MIN` overflows and is a bit simpler imo;
it also avoids hard-coding that the word size of an `APInt` is 64 bits, but
granted, we do that in quite a few places elsewhere...
https://github.com/llvm/llvm-project/pull/211785
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
@@ -356,12 +356,10 @@ bool AddSubMulHelper(InterpState &S, CodePtr OpPC,
unsigned Bits, const T &LHS,
if (S.checkingForUndefinedBehavior()) {
const Expr *E = S.Current->getExpr(OpPC);
QualType Type = E->getType();
-SmallString<32> Trunc;
-Value.trunc(Result.bitWidth())
-.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
- /*UpperCase=*/true, /*InsertSeparators=*/true);
S.report(E->getExprLoc(), diag::warn_integer_constant_overflow)
-<< Trunc << Type << E->getSourceRange();
+<< formatDiagnosticInteger(Value.trunc(Result.bitWidth()),
+ Result.isSigned())
Sirraide wrote:
The same applies to the other places below; I don’t think we should ever call
`formatDiagnosticInteger()` outside of implementations of `operator<<`
https://github.com/llvm/llvm-project/pull/211785
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
@@ -1429,19 +1429,33 @@ inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, return DB; } +inline constexpr uint64_t DiagnosticIntegerSeparatorThreshold = 100'000; Sirraide wrote: Can you move this into `formatDiagnosticInteger` https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
@@ -1429,19 +1429,33 @@ inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, return DB; } +inline constexpr uint64_t DiagnosticIntegerSeparatorThreshold = 100'000; + +inline std::string formatDiagnosticInteger(const llvm::APInt &Val, Sirraide wrote: Also, I feel like it should return a `SmallString<32>` rather than a `std::string` https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
https://github.com/Sirraide edited https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
https://github.com/Sirraide requested changes to this pull request. There are a lot of calls to `formatDiagnosticInteger()` that should be removed https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
https://github.com/AaronBallman commented: I think the next steps for this are: 1) bring up to date with ToT and ensure existing all tests pass, 2) add additional test coverage showing the behavioral change, possibly with FIXME comments in cases where we think the behavior could be better, 3) add a release note. >From there, we can see if we're happy with the state of things or whether >further changes are needed. https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
VachanVY wrote: > @AaronBallman @shafik ping. I will proceed with fixing the test cases by > adding the modified diagonostic message once I know I'm going in the right > direction. ping https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
VachanVY wrote: @AaronBallman @shafik ping. I will proceed with fixing the test cases by adding the modified diagonostic message once I know I'm going in the right direction. https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
https://github.com/VachanVY updated
https://github.com/llvm/llvm-project/pull/211785
>From f2f3a679bb89cdab7070e607507c1e70ca8c456c Mon Sep 17 00:00:00 2001
From: Vachan V Y
Date: Mon, 27 Jul 2026 22:37:27 +0530
Subject: [PATCH] [Clang] Add thousands-separator to large integers; change the
diagnostics engine
---
clang/include/clang/AST/OptionalDiagnostic.h | 5 ++--
clang/include/clang/Basic/Diagnostic.h| 26 ++-
.../include/clang/Basic/DiagnosticASTKinds.td | 2 +-
clang/lib/AST/ByteCode/Interp.h | 24 +++--
clang/lib/AST/ByteCode/InterpHelpers.h| 4 +--
clang/lib/AST/ExprConstant.cpp| 10 +++
clang/lib/Basic/Diagnostic.cpp| 21 +--
clang/lib/Sema/SemaDecl.cpp | 5 ++--
clang/lib/Sema/SemaExprCXX.cpp| 4 +--
clang/lib/Sema/SemaType.cpp | 4 +--
10 files changed, 61 insertions(+), 44 deletions(-)
diff --git a/clang/include/clang/AST/OptionalDiagnostic.h
b/clang/include/clang/AST/OptionalDiagnostic.h
index c9a2d19f4ebce..b21955bf0088b 100644
--- a/clang/include/clang/AST/OptionalDiagnostic.h
+++ b/clang/include/clang/AST/OptionalDiagnostic.h
@@ -39,9 +39,8 @@ class OptionalDiagnostic {
OptionalDiagnostic &operator<<(const llvm::APSInt &I) {
if (Diag) {
- SmallVector Buffer;
- I.toString(Buffer);
- *Diag << StringRef(Buffer.data(), Buffer.size());
+ std::string Str = formatDiagnosticInteger(I, I.isSigned());
+ *Diag << StringRef(Str);
}
return *this;
}
diff --git a/clang/include/clang/Basic/Diagnostic.h
b/clang/include/clang/Basic/Diagnostic.h
index 66e79e3b4300b..82285d2bac1aa 100644
--- a/clang/include/clang/Basic/Diagnostic.h
+++ b/clang/include/clang/Basic/Diagnostic.h
@@ -1402,19 +1402,33 @@ inline const StreamingDiagnostic &operator<<(const
StreamingDiagnostic &DB,
return DB;
}
+inline constexpr uint64_t DiagnosticIntegerSeparatorThreshold = 100'000;
+
+inline std::string formatDiagnosticInteger(const llvm::APInt &Val,
+ bool Signed) {
+ bool Small = false;
+ if (Signed) {
+if (Val.getSignificantBits() <= 64) {
+ int64_t S = Val.getSExtValue();
+ uint64_t Magnitude = S < 0 ? -static_cast(S) : uint64_t(S);
+ Small = Magnitude < DiagnosticIntegerSeparatorThreshold;
+}
+ } else if (Val.getActiveBits() <= 64) {
+Small = Val.getZExtValue() < DiagnosticIntegerSeparatorThreshold;
+ }
+ return toString(Val, /*Radix=*/10, Signed, /*formatAsCLiteral=*/false,
+ /*UpperCase=*/true, /*InsertSeparators=*/!Small);
+}
+
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
const llvm::APSInt &Int) {
- DB.AddString(toString(Int, /*Radix=*/10, Int.isSigned(),
-/*formatAsCLiteral=*/false,
-/*UpperCase=*/true, /*InsertSeparators=*/true));
+ DB.AddString(formatDiagnosticInteger(Int, Int.isSigned()));
return DB;
}
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
const llvm::APInt &Int) {
- DB.AddString(toString(Int, /*Radix=*/10, /*Signed=*/false,
-/*formatAsCLiteral=*/false,
-/*UpperCase=*/true, /*InsertSeparators=*/true));
+ DB.AddString(formatDiagnosticInteger(Int, /*Signed=*/false));
return DB;
}
diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td
b/clang/include/clang/Basic/DiagnosticASTKinds.td
index f86f0157b2b1f..658750d25251a 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -355,7 +355,7 @@ def note_constexpr_new_negative : Note<
def note_constexpr_new_too_large : Note<
"cannot allocate array; evaluated array bound %0 is too large">;
def note_constexpr_new_exceeds_limits : Note<
- "cannot allocate array; evaluated array bound %0 exceeds the limit (%1); "
+ "cannot allocate array; evaluated array bound %0 exceeds the limit of %1; "
"use '-fconstexpr-steps' to increase this limit">;
def note_constexpr_new_too_small : Note<
"cannot allocate array; evaluated array bound %0 is too small to hold "
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 405f4a29ec982..cc57ddb61d1e5 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -356,12 +356,10 @@ bool AddSubMulHelper(InterpState &S, CodePtr OpPC,
unsigned Bits, const T &LHS,
if (S.checkingForUndefinedBehavior()) {
const Expr *E = S.Current->getExpr(OpPC);
QualType Type = E->getType();
-SmallString<32> Trunc;
-Value.trunc(Result.bitWidth())
-.toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
- /*UpperCase=*/true, /*InsertSeparators=*/true);
S.report(E->getExprLoc(),
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
AaronBallman wrote: > > I feel like most big numbers in diagnostics won’t be _that_ big > > And if we end up having to deal w/ _really_ large numbers, we run into... > [other problems](https://github.com/llvm/llvm-project/issues/71675) Yeah, this feels like we're dancing around the same root cause for both issues -- printing really large numbers isn't necessarily helpful. I think this PR is reasonable though; some of the values are getting easier to read, some might be getting harder to read but were already hard to read. I don't think any are getting harder to read that were previously easy to read, but this is pretty subjective stuff too. https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
Sirraide wrote: > I feel like most big numbers in diagnostics won’t be _that_ big And if we end up having to deal w/ _really_ large numbers, we run into... [other problems](#71675) https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
Sirraide wrote: > Before: > > ``` > value > 28948022309329048855892746252171976962977213799489202546401021394546514198529 > is outside the range of representable > ``` > > After: > > ``` > value > 28'948'022'309'329'048'855'892'746'252'171'976'962'977'213'799'489'202'546'401'021'394'546'514'198'529 > is outside the range of representable > ``` > > I don't think either is readable. :-D I mean, at that point we might want to consider printing like 2.89e76 or sth like that, but I feel like most big numbers in diagnostics won’t be *that* big https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
AaronBallman wrote: It's hard to say whether this improves the diagnostic or not, tbh! Before: ``` value 28948022309329048855892746252171976962977213799489202546401021394546514198529 is outside the range of representable ``` After: ``` value 28'948'022'309'329'048'855'892'746'252'171'976'962'977'213'799'489'202'546'401'021'394'546'514'198'529 is outside the range of representable ``` I don't think either is readable. :-D There are some other interesting cases in the failures, the one that I think is most interesting would be when trying to display values that encode a date. e.g., `20260728` turns into `20'260'728` when it could actually be easier to read as `2026'07'28`. Not certain we want to get clever here though, but it is worth thinking about. https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
AaronBallman wrote: > > I don't think we should play whack-a-mole trying to find all the places we > > could potentially print a large integer value and update the diagnostic > > call sites. I think this is something the diagnostics engine should handle > > when deciding how to render an integer so that the behavior is consistent > > across diagnostics. Otherwise we'll have to do the same dance for > > `note_constexpr_new_too_large`, `err_attribute_size_too_large`, and so on. > > Can we just enable that by default for all diagnostics? Or is there one where > that’s obviously wrong for some reason? It's not that it's wrong, it's more that it can be distracting depending on the user and the situation. e.g. `1'234'567` is easier to read than `1234567` but not everyone will agree that `1'234` is easier to read than `1234`. There's also the problem that we single quote syntactic constructs and so if you end up with some cases like `'1'234'` the separator can be harder to read. That said, I'm not certain what a good heuristic is, but "always use separators" isn't it. Also, we emit hex values sometimes, that might have a more obvious heuristic in some ways than decimal values. https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
Sirraide wrote: > I don't think we should play whack-a-mole trying to find all the places we > could potentially print a large integer value and update the diagnostic call > sites. I think this is something the diagnostics engine should handle when > deciding how to render an integer so that the behavior is consistent across > diagnostics. Otherwise we'll have to do the same dance for > `note_constexpr_new_too_large`, `err_attribute_size_too_large`, and so on. Can we just enable that by default for all diagnostics? Or is there one where that’s obviously wrong for some reason? https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
AaronBallman wrote: > That said, I'm not certain what a good heuristic is, Actually, I would not be surprised if there's some Ux literature out there on this we could lean on. Absent that... For hex values, I think it might make sense to always split every quad. e.g., `0xF`, `0xF000`, `0xF000'F`, `0xF000'F000'F`, etc. For binary values, probably every quad as well, like `0b1000'1`. For decimal values, I think maybe it makes sense to do triples once we get to hundreds of thousands. e.g., `1000`, `1`, `100'000`, `1'000'000` because that at least carves out values like `1024` which I think would be more distracting as `1'024`. https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
cor3ntin wrote: > Can we just enable that by default for all diagnostics? I think we can https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
https://github.com/AaronBallman requested changes to this pull request. I don't think we should play whack-a-mole trying to find all the places we could potentially print a large integer value and update the diagnostic call sites. I think this is something the diagnostics engine should handle when deciding how to render an integer so that the behavior is consistent across diagnostics. Otherwise we'll have to do the same dance for `note_constexpr_new_too_large`, `err_attribute_size_too_large`, and so on. https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
zwuis wrote: Please use existing facilities to add thousands-separators, not implement a new one. https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
https://github.com/VachanVY updated
https://github.com/llvm/llvm-project/pull/211785
>From 5116d7949696f434790773fa95831cff75cf4852 Mon Sep 17 00:00:00 2001
From: Vachan V Y
Date: Fri, 24 Jul 2026 18:36:19 +0530
Subject: [PATCH 1/2] [Clang] Add thousands-separator to "cannot allocate
array; evaluated array bound [...] exceeds the limit" diagnostic
---
clang/docs/ReleaseNotes.md | 5 +
clang/include/clang/Basic/DiagnosticASTKinds.td | 2 +-
clang/lib/AST/ByteCode/InterpHelpers.h | 17 -
clang/lib/AST/ExprConstant.cpp | 17 -
clang/test/AST/ByteCode/dynalloc-limits.cpp | 4 ++--
.../SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp | 4 ++--
6 files changed, 42 insertions(+), 7 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 052838ef0af0d..634adef8a947e 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -123,6 +123,11 @@ latest release, please see the [Clang Web
Site](https://clang.llvm.org) or the
### Improvements to Clang's diagnostics
+- The diagnostic emitted when a constant-evaluated array allocation exceeds the
+ limit now formats the evaluated bound and the limit with thousands separators
+ and uses the clearer wording "exceeds the limit of" (e.g. "evaluated array
+ bound 1,048,577 exceeds the limit of 1,048,576"). (#GH211770)
+
### Improvements to Clang's time-trace
### Improvements to Coverage Mapping
diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td
b/clang/include/clang/Basic/DiagnosticASTKinds.td
index f86f0157b2b1f..658750d25251a 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -355,7 +355,7 @@ def note_constexpr_new_negative : Note<
def note_constexpr_new_too_large : Note<
"cannot allocate array; evaluated array bound %0 is too large">;
def note_constexpr_new_exceeds_limits : Note<
- "cannot allocate array; evaluated array bound %0 exceeds the limit (%1); "
+ "cannot allocate array; evaluated array bound %0 exceeds the limit of %1; "
"use '-fconstexpr-steps' to increase this limit">;
def note_constexpr_new_too_small : Note<
"cannot allocate array; evaluated array bound %0 is too small to hold "
diff --git a/clang/lib/AST/ByteCode/InterpHelpers.h
b/clang/lib/AST/ByteCode/InterpHelpers.h
index 4d908d1e44546..960801ad891f1 100644
--- a/clang/lib/AST/ByteCode/InterpHelpers.h
+++ b/clang/lib/AST/ByteCode/InterpHelpers.h
@@ -90,12 +90,27 @@ bool handleOverflow(InterpState &S, CodePtr OpPC, const T
&SrcValue) {
return S.noteUndefinedBehavior();
}
+/// Format an integer with a thousands separator (',') for use in diagnostics,
+inline std::string toStringWithThousandsSeparator(uint64_t Value) {
+ std::string Digits = std::to_string(Value);
+ std::string Result;
+ size_t Len = Digits.size();
+ Result.reserve(Len + (Len - 1) / 3);
+ for (size_t I = 0; I < Len; ++I) {
+if (I != 0 && (Len - I) % 3 == 0)
+ Result.push_back(',');
+Result.push_back(Digits[I]);
+ }
+ return Result;
+}
+
inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
if (Limit != 0 && NumElems > Limit) {
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_new_exceeds_limits)
-<< NumElems << Limit;
+<< toStringWithThousandsSeparator(NumElems)
+<< toStringWithThousandsSeparator(Limit);
return false;
}
return true;
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 574dd8b04e779..cebb84965f081 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -79,6 +79,20 @@ using llvm::APSInt;
using llvm::APFloat;
using llvm::FixedPointSemantics;
+/// Format an integer with a thousands separator (',') for use in diagnostics,
+static std::string toStringWithThousandsSeparator(uint64_t Value) {
+ std::string Digits = std::to_string(Value);
+ std::string Result;
+ size_t Len = Digits.size();
+ Result.reserve(Len + (Len - 1) / 3);
+ for (size_t I = 0; I < Len; ++I) {
+if (I != 0 && (Len - I) % 3 == 0)
+ Result.push_back(',');
+Result.push_back(Digits[I]);
+ }
+ return Result;
+}
+
namespace {
struct LValue;
class CallStackFrame;
@@ -976,7 +990,8 @@ namespace {
if (Limit != 0 && ElemCount > Limit) {
if (Diag)
FFDiag(Loc, diag::note_constexpr_new_exceeds_limits)
- << ElemCount << Limit;
+ << toStringWithThousandsSeparator(ElemCount)
+ << toStringWithThousandsSeparator(Limit);
return false;
}
return true;
diff --git a/clang/test/AST/ByteCode/dynalloc-limits.cpp
b/clang/test/AST/ByteCode/dynalloc-limits.cpp
index ed2d4faf974b1..decf1e2b24c3d 100644
--- a/clang/test/AST/ByteCode/dynalloc-limits.cpp
+++ b/clang/test/AST/ByteCode/dynalloc-limits.cpp
@@
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
VachanVY wrote: > Please use existing facilities to add thousands-separators, not implement a > new one. @zwuis do you mean `APInt::toString(..., /*InsertSeparators=*/true` (exposed via `llvm::toString` in `llvm/ADT/StringExtras.h`) ? https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clang
Author: Vachan (VachanVY)
Changes
fix #211770
---
Full diff: https://github.com/llvm/llvm-project/pull/211785.diff
6 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+5)
- (modified) clang/include/clang/Basic/DiagnosticASTKinds.td (+1-1)
- (modified) clang/lib/AST/ByteCode/InterpHelpers.h (+16-1)
- (modified) clang/lib/AST/ExprConstant.cpp (+16-1)
- (modified) clang/test/AST/ByteCode/dynalloc-limits.cpp (+2-2)
- (modified) clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp (+2-2)
``diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 83a2b10d96046..8249e0649d105 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -159,6 +159,11 @@ features cannot lower the translation-unit ABI level;
### Improvements to Clang's diagnostics
+- The diagnostic emitted when a constant-evaluated array allocation exceeds the
+ limit now formats the evaluated bound and the limit with thousands separators
+ and uses the clearer wording "exceeds the limit of" (e.g. "evaluated array
+ bound 1,048,577 exceeds the limit of 1,048,576"). (#GH211770)
+
- More consistent rendering of Unicode characters in diagnostic messages.
- Fixed bug in `-Wdocumentation` so that it correctly handles explicit
diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td
b/clang/include/clang/Basic/DiagnosticASTKinds.td
index f86f0157b2b1f..658750d25251a 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -355,7 +355,7 @@ def note_constexpr_new_negative : Note<
def note_constexpr_new_too_large : Note<
"cannot allocate array; evaluated array bound %0 is too large">;
def note_constexpr_new_exceeds_limits : Note<
- "cannot allocate array; evaluated array bound %0 exceeds the limit (%1); "
+ "cannot allocate array; evaluated array bound %0 exceeds the limit of %1; "
"use '-fconstexpr-steps' to increase this limit">;
def note_constexpr_new_too_small : Note<
"cannot allocate array; evaluated array bound %0 is too small to hold "
diff --git a/clang/lib/AST/ByteCode/InterpHelpers.h
b/clang/lib/AST/ByteCode/InterpHelpers.h
index 4d908d1e44546..960801ad891f1 100644
--- a/clang/lib/AST/ByteCode/InterpHelpers.h
+++ b/clang/lib/AST/ByteCode/InterpHelpers.h
@@ -90,12 +90,27 @@ bool handleOverflow(InterpState &S, CodePtr OpPC, const T
&SrcValue) {
return S.noteUndefinedBehavior();
}
+/// Format an integer with a thousands separator (',') for use in diagnostics,
+inline std::string toStringWithThousandsSeparator(uint64_t Value) {
+ std::string Digits = std::to_string(Value);
+ std::string Result;
+ size_t Len = Digits.size();
+ Result.reserve(Len + (Len - 1) / 3);
+ for (size_t I = 0; I < Len; ++I) {
+if (I != 0 && (Len - I) % 3 == 0)
+ Result.push_back(',');
+Result.push_back(Digits[I]);
+ }
+ return Result;
+}
+
inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
if (Limit != 0 && NumElems > Limit) {
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_new_exceeds_limits)
-<< NumElems << Limit;
+<< toStringWithThousandsSeparator(NumElems)
+<< toStringWithThousandsSeparator(Limit);
return false;
}
return true;
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 574dd8b04e779..cebb84965f081 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -79,6 +79,20 @@ using llvm::APSInt;
using llvm::APFloat;
using llvm::FixedPointSemantics;
+/// Format an integer with a thousands separator (',') for use in diagnostics,
+static std::string toStringWithThousandsSeparator(uint64_t Value) {
+ std::string Digits = std::to_string(Value);
+ std::string Result;
+ size_t Len = Digits.size();
+ Result.reserve(Len + (Len - 1) / 3);
+ for (size_t I = 0; I < Len; ++I) {
+if (I != 0 && (Len - I) % 3 == 0)
+ Result.push_back(',');
+Result.push_back(Digits[I]);
+ }
+ return Result;
+}
+
namespace {
struct LValue;
class CallStackFrame;
@@ -976,7 +990,8 @@ namespace {
if (Limit != 0 && ElemCount > Limit) {
if (Diag)
FFDiag(Loc, diag::note_constexpr_new_exceeds_limits)
- << ElemCount << Limit;
+ << toStringWithThousandsSeparator(ElemCount)
+ << toStringWithThousandsSeparator(Limit);
return false;
}
return true;
diff --git a/clang/test/AST/ByteCode/dynalloc-limits.cpp
b/clang/test/AST/ByteCode/dynalloc-limits.cpp
index ed2d4faf974b1..decf1e2b24c3d 100644
--- a/clang/test/AST/ByteCode/dynalloc-limits.cpp
+++ b/clang/test/AST/ByteCode/dynalloc-limits.cpp
@@ -54,7 +54,7 @@ constexpr std::size_t s = S(~0UL)[42]; //
both-error {{constexpr va
constexpr std::size_t ssmall = S(100)[42];
constexpr std::size_t
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
https://github.com/VachanVY ready_for_review https://github.com/llvm/llvm-project/pull/211785 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
https://github.com/VachanVY updated
https://github.com/llvm/llvm-project/pull/211785
>From 5116d7949696f434790773fa95831cff75cf4852 Mon Sep 17 00:00:00 2001
From: Vachan V Y
Date: Fri, 24 Jul 2026 18:36:19 +0530
Subject: [PATCH] [Clang] Add thousands-separator to "cannot allocate array;
evaluated array bound [...] exceeds the limit" diagnostic
---
clang/docs/ReleaseNotes.md | 5 +
clang/include/clang/Basic/DiagnosticASTKinds.td | 2 +-
clang/lib/AST/ByteCode/InterpHelpers.h | 17 -
clang/lib/AST/ExprConstant.cpp | 17 -
clang/test/AST/ByteCode/dynalloc-limits.cpp | 4 ++--
.../SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp | 4 ++--
6 files changed, 42 insertions(+), 7 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 052838ef0af0d..634adef8a947e 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -123,6 +123,11 @@ latest release, please see the [Clang Web
Site](https://clang.llvm.org) or the
### Improvements to Clang's diagnostics
+- The diagnostic emitted when a constant-evaluated array allocation exceeds the
+ limit now formats the evaluated bound and the limit with thousands separators
+ and uses the clearer wording "exceeds the limit of" (e.g. "evaluated array
+ bound 1,048,577 exceeds the limit of 1,048,576"). (#GH211770)
+
### Improvements to Clang's time-trace
### Improvements to Coverage Mapping
diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td
b/clang/include/clang/Basic/DiagnosticASTKinds.td
index f86f0157b2b1f..658750d25251a 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -355,7 +355,7 @@ def note_constexpr_new_negative : Note<
def note_constexpr_new_too_large : Note<
"cannot allocate array; evaluated array bound %0 is too large">;
def note_constexpr_new_exceeds_limits : Note<
- "cannot allocate array; evaluated array bound %0 exceeds the limit (%1); "
+ "cannot allocate array; evaluated array bound %0 exceeds the limit of %1; "
"use '-fconstexpr-steps' to increase this limit">;
def note_constexpr_new_too_small : Note<
"cannot allocate array; evaluated array bound %0 is too small to hold "
diff --git a/clang/lib/AST/ByteCode/InterpHelpers.h
b/clang/lib/AST/ByteCode/InterpHelpers.h
index 4d908d1e44546..960801ad891f1 100644
--- a/clang/lib/AST/ByteCode/InterpHelpers.h
+++ b/clang/lib/AST/ByteCode/InterpHelpers.h
@@ -90,12 +90,27 @@ bool handleOverflow(InterpState &S, CodePtr OpPC, const T
&SrcValue) {
return S.noteUndefinedBehavior();
}
+/// Format an integer with a thousands separator (',') for use in diagnostics,
+inline std::string toStringWithThousandsSeparator(uint64_t Value) {
+ std::string Digits = std::to_string(Value);
+ std::string Result;
+ size_t Len = Digits.size();
+ Result.reserve(Len + (Len - 1) / 3);
+ for (size_t I = 0; I < Len; ++I) {
+if (I != 0 && (Len - I) % 3 == 0)
+ Result.push_back(',');
+Result.push_back(Digits[I]);
+ }
+ return Result;
+}
+
inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
if (Limit != 0 && NumElems > Limit) {
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_new_exceeds_limits)
-<< NumElems << Limit;
+<< toStringWithThousandsSeparator(NumElems)
+<< toStringWithThousandsSeparator(Limit);
return false;
}
return true;
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 574dd8b04e779..cebb84965f081 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -79,6 +79,20 @@ using llvm::APSInt;
using llvm::APFloat;
using llvm::FixedPointSemantics;
+/// Format an integer with a thousands separator (',') for use in diagnostics,
+static std::string toStringWithThousandsSeparator(uint64_t Value) {
+ std::string Digits = std::to_string(Value);
+ std::string Result;
+ size_t Len = Digits.size();
+ Result.reserve(Len + (Len - 1) / 3);
+ for (size_t I = 0; I < Len; ++I) {
+if (I != 0 && (Len - I) % 3 == 0)
+ Result.push_back(',');
+Result.push_back(Digits[I]);
+ }
+ return Result;
+}
+
namespace {
struct LValue;
class CallStackFrame;
@@ -976,7 +990,8 @@ namespace {
if (Limit != 0 && ElemCount > Limit) {
if (Diag)
FFDiag(Loc, diag::note_constexpr_new_exceeds_limits)
- << ElemCount << Limit;
+ << toStringWithThousandsSeparator(ElemCount)
+ << toStringWithThousandsSeparator(Limit);
return false;
}
return true;
diff --git a/clang/test/AST/ByteCode/dynalloc-limits.cpp
b/clang/test/AST/ByteCode/dynalloc-limits.cpp
index ed2d4faf974b1..decf1e2b24c3d 100644
--- a/clang/test/AST/ByteCode/dynalloc-limits.cpp
+++ b/clang/test/AST/ByteCode/dynalloc-limits.cpp
@@ -54,7
[clang] [Clang] Add thousands-separator to "cannot allocate array; evaluated array bound [...] exceeds the limit" diagnostic (PR #211785)
https://github.com/VachanVY created
https://github.com/llvm/llvm-project/pull/211785
fix #211770
>From 5116d7949696f434790773fa95831cff75cf4852 Mon Sep 17 00:00:00 2001
From: Vachan V Y
Date: Fri, 24 Jul 2026 18:36:19 +0530
Subject: [PATCH] [Clang] Add thousands-separator to "cannot allocate array;
evaluated array bound [...] exceeds the limit" diagnostic
---
clang/docs/ReleaseNotes.md | 5 +
clang/include/clang/Basic/DiagnosticASTKinds.td | 2 +-
clang/lib/AST/ByteCode/InterpHelpers.h | 17 -
clang/lib/AST/ExprConstant.cpp | 17 -
clang/test/AST/ByteCode/dynalloc-limits.cpp | 4 ++--
.../SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp | 4 ++--
6 files changed, 42 insertions(+), 7 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 052838ef0af0d..634adef8a947e 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -123,6 +123,11 @@ latest release, please see the [Clang Web
Site](https://clang.llvm.org) or the
### Improvements to Clang's diagnostics
+- The diagnostic emitted when a constant-evaluated array allocation exceeds the
+ limit now formats the evaluated bound and the limit with thousands separators
+ and uses the clearer wording "exceeds the limit of" (e.g. "evaluated array
+ bound 1,048,577 exceeds the limit of 1,048,576"). (#GH211770)
+
### Improvements to Clang's time-trace
### Improvements to Coverage Mapping
diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td
b/clang/include/clang/Basic/DiagnosticASTKinds.td
index f86f0157b2b1f..658750d25251a 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -355,7 +355,7 @@ def note_constexpr_new_negative : Note<
def note_constexpr_new_too_large : Note<
"cannot allocate array; evaluated array bound %0 is too large">;
def note_constexpr_new_exceeds_limits : Note<
- "cannot allocate array; evaluated array bound %0 exceeds the limit (%1); "
+ "cannot allocate array; evaluated array bound %0 exceeds the limit of %1; "
"use '-fconstexpr-steps' to increase this limit">;
def note_constexpr_new_too_small : Note<
"cannot allocate array; evaluated array bound %0 is too small to hold "
diff --git a/clang/lib/AST/ByteCode/InterpHelpers.h
b/clang/lib/AST/ByteCode/InterpHelpers.h
index 4d908d1e44546..960801ad891f1 100644
--- a/clang/lib/AST/ByteCode/InterpHelpers.h
+++ b/clang/lib/AST/ByteCode/InterpHelpers.h
@@ -90,12 +90,27 @@ bool handleOverflow(InterpState &S, CodePtr OpPC, const T
&SrcValue) {
return S.noteUndefinedBehavior();
}
+/// Format an integer with a thousands separator (',') for use in diagnostics,
+inline std::string toStringWithThousandsSeparator(uint64_t Value) {
+ std::string Digits = std::to_string(Value);
+ std::string Result;
+ size_t Len = Digits.size();
+ Result.reserve(Len + (Len - 1) / 3);
+ for (size_t I = 0; I < Len; ++I) {
+if (I != 0 && (Len - I) % 3 == 0)
+ Result.push_back(',');
+Result.push_back(Digits[I]);
+ }
+ return Result;
+}
+
inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
if (Limit != 0 && NumElems > Limit) {
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_new_exceeds_limits)
-<< NumElems << Limit;
+<< toStringWithThousandsSeparator(NumElems)
+<< toStringWithThousandsSeparator(Limit);
return false;
}
return true;
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 574dd8b04e779..cebb84965f081 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -79,6 +79,20 @@ using llvm::APSInt;
using llvm::APFloat;
using llvm::FixedPointSemantics;
+/// Format an integer with a thousands separator (',') for use in diagnostics,
+static std::string toStringWithThousandsSeparator(uint64_t Value) {
+ std::string Digits = std::to_string(Value);
+ std::string Result;
+ size_t Len = Digits.size();
+ Result.reserve(Len + (Len - 1) / 3);
+ for (size_t I = 0; I < Len; ++I) {
+if (I != 0 && (Len - I) % 3 == 0)
+ Result.push_back(',');
+Result.push_back(Digits[I]);
+ }
+ return Result;
+}
+
namespace {
struct LValue;
class CallStackFrame;
@@ -976,7 +990,8 @@ namespace {
if (Limit != 0 && ElemCount > Limit) {
if (Diag)
FFDiag(Loc, diag::note_constexpr_new_exceeds_limits)
- << ElemCount << Limit;
+ << toStringWithThousandsSeparator(ElemCount)
+ << toStringWithThousandsSeparator(Limit);
return false;
}
return true;
diff --git a/clang/test/AST/ByteCode/dynalloc-limits.cpp
b/clang/test/AST/ByteCode/dynalloc-limits.cpp
index ed2d4faf974b1..decf1e2b24c3d 100644
--- a/clang/test/AST/ByteCode/dynalloc-limits.cpp
+++
