[clang] [llvm] [clang] Avoid printing overly large integer/_BitInt numbers in static assertion failure diagnostics #71675 (PR #145053)

Erich Keane via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 7 06:48:02 PDT 2025


================
@@ -2278,8 +2285,28 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
     }
   } else {
     int Pos = 0;
+    // The value of cutOffSize is not special, it is just a number of
+    // characters that gives us enough info without losing readability.
+    constexpr int cutOffSize = 20;
     while (Tmp.getBoolValue()) {
       uint64_t Digit;
+      if (truncate && Pos == cutOffSize) {
+        unsigned numDigits = (int32_t)(Tmp.logBase2()/log2(Radix))+1;
+        if(numDigits-cutOffSize > 0) {
+          // Calculating pow of exponents over 300000 takes a long time.
+          // To keep note printing time short(under 3s), values with more digits
+          // will only return the last 20 digits.
+          if(numDigits < 300000) {
+            APInt divider = APIntOps::pow(APInt(Tmp.getBitWidth(),Radix),numDigits-cutOffSize);
+            Tmp = Tmp.udiv(divider);
+            Str.append(3,'.');
+          }
+          else {
+            Str.append(3,'.');
----------------
erichkeane wrote:

Would there be any way to find the 'first' digit or something in this case?  I find myself wondering if there is a 'better' way here... 

IS there any special-casing we can do here for base-10?  I DO find myself wondering if `pow` is the best way for Radix==10.  I know that V<<3 + v << 1 is the same as *10, I wonder if someone better at math than I could come up with a way to apply that as a way to do the 10^N as a simple set of shifts/math? 

https://github.com/llvm/llvm-project/pull/145053


More information about the llvm-commits mailing list