[clang] [clang] Avoid printing overly large integer/_BitInt numbers in static assertion failure diagnostics #71675 (PR #145053)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 20 08:32:59 PDT 2025
https://github.com/maramatias created https://github.com/llvm/llvm-project/pull/145053
Resolves #71675
DRAFT the unittest is missing.
This implementation does not shorten the output it only changes it to hex.
Performance comparison between origin code and patched code compiling on a RHEL9.6VM with 8cores and 16GB RAM
compiling sample program
```
#include "stdio.h"
#include "fenv.h"
namespace test {
constexpr unsigned _BitInt(__BITINT_MAXWIDTH__ >> 6) F = ~0;
static_assert(F == 1);
}
```
using
`./llvm-project/build/bin/clang++ ut71675.cpp -o ut71675`
**Original code**
user avg over 10 runs - 1.6748
sys avg over 10 runs - 0.0299
**Patched code**
user avg over 10 runs - 0.237
sys avg over 10 runs - 0.0304
>From 162d11edfa476b5e70d75cfcfd16c71eb52adc65 Mon Sep 17 00:00:00 2001
From: Mara Matias <mara.matias1 at gmail.com>
Date: Wed, 18 Jun 2025 09:06:10 -0700
Subject: [PATCH] draft unittest not ready
---
clang/docs/ReleaseNotes.rst | 4 ++++
clang/lib/Sema/SemaDeclCXX.cpp | 10 +++++++++-
clang/test/CXX/dcl.decl/dcl.decl.general/p4-20.cpp | 5 +++++
3 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 12816eed2e8b5..301abb3e5b1fb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -635,6 +635,10 @@ Improvements to Clang's diagnostics
#GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
#GH36703, #GH32903, #GH23312, #GH69874.
+- Improved the performance of static assertions envolving large integers by
+ using hex format instead of string.
+
+ Fixes #GH71675
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 16645ecf411e5..0e053671836c8 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -47,6 +47,7 @@
#include "clang/Sema/SemaOpenMP.h"
#include "clang/Sema/Template.h"
#include "clang/Sema/TemplateDeduction.h"
+#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
@@ -17575,7 +17576,14 @@ static bool ConvertAPValueToString(const APValue &V, QualType T,
break;
}
}
- V.getInt().toString(Str);
+
+ llvm::APSInt vInt = V.getInt();
+ if (vInt > std::numeric_limits<uint64_t>::max() ||
+ vInt < std::numeric_limits<int64_t>::min()) {
+ vInt.toString(Str,16);
+ } else {
+ vInt.toString(Str);
+ }
}
break;
diff --git a/clang/test/CXX/dcl.decl/dcl.decl.general/p4-20.cpp b/clang/test/CXX/dcl.decl/dcl.decl.general/p4-20.cpp
index c7596218db537..14a4d024b8aa8 100644
--- a/clang/test/CXX/dcl.decl/dcl.decl.general/p4-20.cpp
+++ b/clang/test/CXX/dcl.decl/dcl.decl.general/p4-20.cpp
@@ -35,6 +35,11 @@ struct S {
void member() requires(sizeof(T) > 1);
};
+namespace GH71675 {
+ constexpr unsigned _BitInt(__BITINT_MAXWIDTH__ >> 6) F = ~0;
+ static_assert(F == 1); // no-error
+}
+
template<typename T>
void ContainingFunction() {
// expected-error at +1 {{non-templated function cannot have a requires clause}}
More information about the cfe-commits
mailing list