[clang] [Clang] prevent recovery call expression from proceeding with explicit attributes and undeclared templates (PR #107786)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 18 05:07:18 PDT 2024


https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/107786

>From 8774b4b073b5ebb52bdf35a7c44c613c6fcf56fc Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Fri, 13 Sep 2024 17:55:15 +0300
Subject: [PATCH] [Clang] prevent recovery call expression from proceeding with
 explicit attributes and undeclared templates

---
 clang/docs/ReleaseNotes.rst                   |  3 +-
 clang/lib/Sema/SemaTemplate.cpp               |  5 +++
 .../SemaTemplate/recovery-crash-cxx20.cpp     | 32 +++++++++++++++++++
 3 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaTemplate/recovery-crash-cxx20.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3929a9fb599259..6ff2e613700c15 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -386,7 +386,8 @@ Bug Fixes to C++ Support
 - Fixed a crash in the typo correction of an invalid CTAD guide. (#GH107887)
 - Fixed a crash when clang tries to subtitute parameter pack while retaining the parameter
   pack. #GH63819, #GH107560
-
+- Fixed an assertion failure when invoking recovery call expressions with explicit attributes
+  and undeclared templates. (#GH107047, #GH49093)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index e5ea02a919f4eb..71433d1694be89 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4460,6 +4460,11 @@ ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
                                   R.getAsSingle<ConceptDecl>(), TemplateArgs);
   }
 
+  if (TemplateArgs && R.getAsSingle<FunctionDecl>() &&
+      R.getAsSingle<FunctionDecl>()->getTemplateSpecializationKind() ==
+          TemplateSpecializationKind::TSK_Undeclared)
+    return ExprError();
+
   // We don't want lookup warnings at this point.
   R.suppressDiagnostics();
 
diff --git a/clang/test/SemaTemplate/recovery-crash-cxx20.cpp b/clang/test/SemaTemplate/recovery-crash-cxx20.cpp
new file mode 100644
index 00000000000000..dd92ddcd36ab02
--- /dev/null
+++ b/clang/test/SemaTemplate/recovery-crash-cxx20.cpp
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
+
+namespace GH49093 {
+  class B {
+  public:
+    static int a() { return 0; } // expected-note {{member is declared here}}
+    decltype(a< 0 >(0)) test;    // expected-error {{member 'a' used before its declaration}}
+  };
+
+  struct C {
+      static int a() { return 0; } // expected-note {{member is declared here}}
+      decltype(a < 0 > (0)) test;  // expected-error {{member 'a' used before its declaration}}
+  };
+
+  void test_is_bool(bool t) {}
+  void test_is_bool(int t) {}
+
+  int main() {
+    B b;
+    test_is_bool(b.test);
+
+    C c;
+    test_is_bool(c.test);
+  }
+}
+
+namespace GH107047 {
+  struct A {
+    static constexpr auto test() { return 1; } // expected-note {{member is declared here}}
+    static constexpr int s = test< 1 >();      // expected-error {{member 'test' used before its declaration}}
+  };
+}



More information about the cfe-commits mailing list