llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Amrutha Tamminana (amrutha-tamminana)

<details>
<summary>Changes</summary>

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.

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


2 Files Affected:

- (modified) clang/lib/AST/Decl.cpp (+4-2) 
- (added) clang/test/CodeGenCXX/builtin-template-linkage.cpp (+18) 


``````````diff
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index ddf71ad1199af..9ca825494683c 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 0000000000000..37d7152c73c3f
--- /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;

``````````

</details>


https://github.com/llvm/llvm-project/pull/234892
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to