[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:24:35 PST 2025


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

>From fecba20439aa92078a213a53cdcde79eb8eee801 Mon Sep 17 00:00:00 2001
From: Devajith Valaparambil Sreeramaswamy
 <devajith.valaparambil.sreeramaswamy at cern.ch>
Date: Fri, 28 Feb 2025 13:13:16 +0100
Subject: [PATCH] [AST] Fix printing of large 64-bit unsigned template
 arguments

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
---
 clang/lib/AST/TemplateBase.cpp                      | 8 +++++++-
 clang/test/AST/ast-print-integral-template-args.cpp | 7 +++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/AST/ast-print-integral-template-args.cpp

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..d1c26ca562ff0
--- /dev/null
+++ b/clang/test/AST/ast-print-integral-template-args.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -ast-print -std=c++17 -Wno-implicitly-unsigned-literal %s -o - | FileCheck %s
+
+template <unsigned long long N>
+struct Foo {};
+
+Foo<9223372036854775810> x;
+// CHECK: template<> struct Foo<9223372036854775810ULL> {



More information about the cfe-commits mailing list