llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Vachan (VachanVY) <details> <summary>Changes</summary> 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<std::size_t>(~0UL)[42]; // both-error {{constexpr va constexpr std::size_t ssmall = S<std::size_t>(100)[42]; constexpr std::size_t s5 = S<std::size_t>(1025)[42]; // both-error {{constexpr variable 's5' must be initialized by a constant expression}} \ - // both-note@#alloc {{cannot allocate array; evaluated array bound 1025 exceeds the limit (1024); use '-fconstexpr-steps' to increase this limit}} \ + // both-note@#alloc {{cannot allocate array; evaluated array bound 1,025 exceeds the limit of 1,024; use '-fconstexpr-steps' to increase this limit}} \ // both-note@#call {{in call to 'this->alloc.allocate(1025)'}} \ // both-note {{in call}} @@ -62,7 +62,7 @@ constexpr std::size_t s5 = S<std::size_t>(1025)[42]; // both-error {{constexpr v template <auto N> constexpr int stack_array() { - [[maybe_unused]] char BIG[N] = {1}; // both-note {{cannot allocate array; evaluated array bound 1025 exceeds the limit (1024)}} + [[maybe_unused]] char BIG[N] = {1}; // both-note {{cannot allocate array; evaluated array bound 1,025 exceeds the limit of 1,024}} return BIG[N-1]; } diff --git a/clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp b/clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp index 8bcb4d45d4af8..dd133ca4b14e0 100644 --- a/clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp +++ b/clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp @@ -65,7 +65,7 @@ constexpr std::size_t s4 = S<std::size_t>(1024)[42]; // expected-error {{constex constexpr std::size_t s5 = S<std::size_t>(1025)[42]; // expected-error{{constexpr variable 's5' must be initialized by a constant expression}} \ - // expected-note@#alloc {{cannot allocate array; evaluated array bound 1025 exceeds the limit (1024); use '-fconstexpr-steps' to increase this limit}} \ + // expected-note@#alloc {{cannot allocate array; evaluated array bound 1,025 exceeds the limit of 1,024; use '-fconstexpr-steps' to increase this limit}} \ // expected-note@#call {{in call to 'this->alloc.allocate(1025)'}} \ // expected-note {{in call}} @@ -75,7 +75,7 @@ constexpr std::size_t s5 = S<std::size_t>(1025)[42]; // expected-error{{constexp template <auto N> constexpr int stack_array() { - [[maybe_unused]] char BIG[N] = {1}; // expected-note 3{{cannot allocate array; evaluated array bound 1025 exceeds the limit (1024); use '-fconstexpr-steps' to increase this limit}} + [[maybe_unused]] char BIG[N] = {1}; // expected-note 3{{cannot allocate array; evaluated array bound 1,025 exceeds the limit of 1,024; use '-fconstexpr-steps' to increase this limit}} return BIG[N-1]; } `````````` </details> https://github.com/llvm/llvm-project/pull/211785 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
