[clang] 0cdc60f - [Clang] Handle null templated declaration in visibility lookup (#234892)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 00:26:16 PST 2026


Author: Amrutha Tamminana
Date: 2026-11-26T08:26:10Z
New Revision: 0cdc60f93939500ca646296279ec1f60a64f33ab

URL: https://github.com/llvm/llvm-project/commit/0cdc60f93939500ca646296279ec1f60a64f33ab
DIFF: https://github.com/llvm/llvm-project/commit/0cdc60f93939500ca646296279ec1f60a64f33ab.diff

LOG: [Clang] Handle null templated declaration in visibility lookup (#234892)

Fixes #232739.

Clang can crash during code generation when a builtin template, such as
`__type_pack_element` or `__make_integer_seq`, is used as a template
template argument of an emitted entity.

During visibility lookup, `getVisibilityOf` attempts to retrieve the
templated declaration from a `TemplateDecl`. However, a
`BuiltinTemplateDecl` does not have a templated declaration, so
`getTemplatedDecl()` can return null.

This patch checks that the templated declaration is non-null before
recursively querying its visibility, avoiding the null dereference.

Added a regression test covering both `__type_pack_element` and
`__make_integer_seq` as template template arguments during code
generation.

Added: 
    clang/test/CodeGenCXX/builtin-template-linkage.cpp

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/AST/Decl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 4bebce45232cef..cd06024116c80c 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1383,6 +1383,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 
 #### Miscellaneous Clang Crashes Fixed
 
+- Fixed a crash during code generation when builtin templates such as
+  `__type_pack_element` or `__make_integer_seq` were used as template
+  template arguments of emitted entities. (#GH232739)
+
 - Fixed a crash in preprocessor conditionals containing `_Pragma`.
 - Fixed a crash when Microsoft extensions were enabled and an unterminated
   `__identifier` expression reached the end of a preprocessing directive or

diff  --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index e89938d230dbbf..a50c4a6375253a 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -1306,8 +1306,10 @@ getExplicitVisibilityAux(const NamedDecl *ND,
   }
 
   // The visibility of a template is stored in the templated decl.
-  if (const auto *TD = dyn_cast<TemplateDecl>(ND))
-    return getVisibilityOf(TD->getTemplatedDecl(), kind);
+  if (const auto *TD = dyn_cast<TemplateDecl>(ND)) {
+    if (const NamedDecl *Templated = TD->getTemplatedDecl())
+      return getVisibilityOf(Templated, kind);
+  }
 
   return std::nullopt;
 }

diff  --git a/clang/test/CodeGenCXX/builtin-template-linkage.cpp b/clang/test/CodeGenCXX/builtin-template-linkage.cpp
new file mode 100644
index 00000000000000..37d7152c73c3f6
--- /dev/null
+++ b/clang/test/CodeGenCXX/builtin-template-linkage.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s
+// CHECK-DAG: @_ZN1QI19__type_pack_elementE1vE = linkonce_odr global
+// CHECK-DAG: @_ZTV1RI18__make_integer_seqE = linkonce_odr constant
+// CHECK-DAG: @r = global %struct.R
+// CHECK: define linkonce_odr void @_ZN1RI18__make_integer_seqE1fEv
+
+template <template <unsigned long, class...> class> struct Q {
+  static int v;
+};
+template <template <unsigned long, class...> class M> int Q<M>::v;
+int *p = &Q<__type_pack_element>::v;
+
+template <template <template <class T, T...> class, class T, T> class>
+struct R {
+  virtual void f() {}
+};
+
+R<__make_integer_seq> r;


        


More information about the cfe-commits mailing list