https://github.com/amrutha-tamminana created https://github.com/llvm/llvm-project/pull/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. **Testing** - The new regression test passes with the change and reproduces the crash without it. - `clang/test/CodeGenCXX/builtin-template-linkage.cpp`: passed. - `clang/test/SemaTemplate` and `clang/test/AST` test suites were also run. >From a8972e218c3dbc1c33cef807e257763802ec134b Mon Sep 17 00:00:00 2001 From: Amrutha <[email protected]> Date: Fri, 23 Oct 2026 09:01:17 +0000 Subject: [PATCH] [Clang] Handle null templated declaration in visibility lookup --- clang/lib/AST/Decl.cpp | 6 ++++-- .../CodeGenCXX/builtin-template-linkage.cpp | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 clang/test/CodeGenCXX/builtin-template-linkage.cpp diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index ddf71ad1199af6..9ca825494683c6 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -1305,8 +1305,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; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
