[clang] 2fe5b15 - [Clang][Sema] improve sema check of clang::musttail attribute (#77727)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 17 04:59:05 PST 2024


Author: Qizhi Hu
Date: 2024-01-17T20:59:01+08:00
New Revision: 2fe5b157189906cac86c300f1a4c89614087c0f4

URL: https://github.com/llvm/llvm-project/commit/2fe5b157189906cac86c300f1a4c89614087c0f4
DIFF: https://github.com/llvm/llvm-project/commit/2fe5b157189906cac86c300f1a4c89614087c0f4.diff

LOG: [Clang][Sema] improve sema check of clang::musttail attribute (#77727)

Call function with no-return attribute generates code with unreachable
instruction instead of return and if the call statement has
`clang::musttail` attribute, it would crash in `VerifyPass`. Check this
condition in Sema ahead.
Fix [issue](https://github.com/llvm/llvm-project/issues/76631)

Co-authored-by: huqizhi <836744285 at qq.com>

Added: 
    clang/test/SemaCXX/PR76631.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaStmt.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 73730252002451..83fe442dbc15fb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -768,6 +768,9 @@ Bug Fixes in This Version
 - Fix crashes when using the binding decl from an invalid structured binding.
   Fixes (`#67495 <https://github.com/llvm/llvm-project/issues/67495>`_) and
   (`#72198 <https://github.com/llvm/llvm-project/issues/72198>`_)
+- Fix assertion failure when call noreturn-attribute function with musttail
+  attribute.
+  Fixes (`#76631 <https://github.com/llvm/llvm-project/issues/76631>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 991c72cad33cad..03b0122d1c08f7 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3113,6 +3113,8 @@ def err_musttail_scope : Error<
   "cannot perform a tail call from this return statement">;
 def err_musttail_no_variadic : Error<
   "%0 attribute may not be used with variadic functions">;
+def err_musttail_no_return : Error<
+  "%0 attribute may not be used with no-return-attribute functions">;
 
 def err_nsobject_attribute : Error<
   "'NSObject' attribute is for pointer types only">;

diff  --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 21efe25ed84a3d..9e7c8c7e4e8c12 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -786,6 +786,12 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) {
     return false;
   }
 
+  const auto *CalleeDecl = CE->getCalleeDecl();
+  if (CalleeDecl && CalleeDecl->hasAttr<CXX11NoReturnAttr>()) {
+    Diag(St->getBeginLoc(), diag::err_musttail_no_return) << &MTA;
+    return false;
+  }
+
   // Caller and callee must match in whether they have a "this" parameter.
   if (CallerType.This.isNull() != CalleeType.This.isNull()) {
     if (const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {

diff  --git a/clang/test/SemaCXX/PR76631.cpp b/clang/test/SemaCXX/PR76631.cpp
new file mode 100644
index 00000000000000..947fa3fc2635e6
--- /dev/null
+++ b/clang/test/SemaCXX/PR76631.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
+
+[[noreturn]] void throw_int() {
+  throw int(); // expected-error {{cannot use 'throw' with exceptions disabled}}
+}
+
+void throw_int_wrapper() {
+  [[clang::musttail]] return throw_int(); // expected-error {{'musttail' attribute may not be used with no-return-attribute functions}}
+}


        


More information about the cfe-commits mailing list