[clang] [AST] Fix printing of large 64-bit unsigned template arguments (PR #129235)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 28 04:15:16 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Devajith (devajithvs)

<details>
<summary>Changes</summary>

Clang omits the `ULL` suffix when printing large unsigned 64-bit template arguments. This patch ensures that values with the high bit set are explicitly printed with `ULL`.`

Based on the original patch by Axel Naumann

---
Full diff: https://github.com/llvm/llvm-project/pull/129235.diff


2 Files Affected:

- (modified) clang/lib/AST/TemplateBase.cpp (+7-1) 
- (added) clang/test/AST/ast-print-integral-template-args.cpp (+7) 


``````````diff
diff --git a/clang/lib/AST/TemplateBase.cpp b/clang/lib/AST/TemplateBase.cpp
index 0eef8f305fcb3..934e8f1f044f5 100644
--- a/clang/lib/AST/TemplateBase.cpp
+++ b/clang/lib/AST/TemplateBase.cpp
@@ -130,8 +130,14 @@ static void printIntegral(const TemplateArgument &TemplArg, raw_ostream &Out,
     } else
       Out << "(" << T->getCanonicalTypeInternal().getAsString(Policy) << ")"
           << Val;
-  } else
+  } else {
     Out << Val;
+    // Handle cases where the value is too large to fit into the underlying type
+    // i.e. where the unsignedness matters.
+    if (T->isBuiltinType())
+      if (Val.isUnsigned() && Val.getBitWidth() == 64 && Val.countLeadingOnes())
+        Out << "ULL";
+  }
 }
 
 static unsigned getArrayDepth(QualType type) {
diff --git a/clang/test/AST/ast-print-integral-template-args.cpp b/clang/test/AST/ast-print-integral-template-args.cpp
new file mode 100644
index 0000000000000..7bec0557a4304
--- /dev/null
+++ b/clang/test/AST/ast-print-integral-template-args.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -ast-print -std=c++17 %s -o - | FileCheck %s
+
+template <unsigned long long N>
+struct Foo {};
+
+Foo<9223372036854775810> x;
+// CHECK: template<> struct Foo<9223372036854775810ULL> {

``````````

</details>


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


More information about the cfe-commits mailing list