Author: MillePlateaux
Date: 2025-04-14T13:10:20-07:00
New Revision: 061f87f75f38e80e7494b5f7235103f7f11a36c3

URL: 
https://github.com/llvm/llvm-project/commit/061f87f75f38e80e7494b5f7235103f7f11a36c3
DIFF: 
https://github.com/llvm/llvm-project/commit/061f87f75f38e80e7494b5f7235103f7f11a36c3.diff

LOG: [Clang][Sema]:Fix musttail attribute on a function with not_tail_called 
attribute has no warning/error (#134465)

The error is emitted when a musttail call is made to a function marked
with the not_tail_called attribute.Closes #133509

Added: 
    clang/test/Sema/attr-musttail.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 840a52a693c7a..f9ba1bcf542ef 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -356,6 +356,8 @@ Improvements to Clang's diagnostics
 - Now correctly diagnose a tentative definition of an array with static
   storage duration in pedantic mode in C. (#GH50661)
 
+- An error is now emitted when a ``musttail`` call is made to a function 
marked with the ``not_tail_called`` attribute. (#GH133509).
+
 Improvements to Clang's time-trace
 ----------------------------------
 

diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 180ca39bc07e9..a9ff821d49405 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3172,6 +3172,8 @@ def note_musttail_mismatch : Note<
     "|has type mismatch at %ordinal3 parameter"
     "%
diff { (expected $ but has $)|}1,2"
     "|has 
diff erent return type%
diff { ($ expected but has $)|}1,2}0">;
+def note_musttail_disabled_by_not_tail_called : Note<
+  "'not_tail_called' attribute prevents being called as a tail call">;
 def err_musttail_callconv_mismatch : Error<
   "cannot perform a tail call to function%select{| %1}0 because it uses an "
   "incompatible calling convention">;

diff  --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index e1b9ccc693bd5..39c2e157591df 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -717,6 +717,13 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr 
&MTA) {
     return false;
   }
 
+  if (const FunctionDecl *CalleeDecl = CE->getDirectCallee();
+      CalleeDecl && CalleeDecl->hasAttr<NotTailCalledAttr>()) {
+    Diag(St->getBeginLoc(), diag::err_musttail_mismatch) << 
/*show-function-callee=*/true << CalleeDecl;
+    Diag(CalleeDecl->getLocation(), 
diag::note_musttail_disabled_by_not_tail_called);
+    return false;
+  }
+
   if (const auto *EWC = dyn_cast<ExprWithCleanups>(E)) {
     if (EWC->cleanupsHaveSideEffects()) {
       Diag(St->getBeginLoc(), diag::err_musttail_needs_trivial_args) << &MTA;

diff  --git a/clang/test/Sema/attr-musttail.cpp 
b/clang/test/Sema/attr-musttail.cpp
new file mode 100644
index 0000000000000..2d0ca937f9b46
--- /dev/null
+++ b/clang/test/Sema/attr-musttail.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -verify -fsyntax-only %s
+
+int __attribute__((not_tail_called)) foo1(int a) {// expected-note 
{{'not_tail_called' attribute prevents being called as a tail call}}
+    return a + 1;  
+}
+
+
+int foo2(int a) {
+    [[clang::musttail]] 
+    return foo1(a);  // expected-error {{cannot perform a tail call to 
function 'foo1' because its signature is incompatible with the calling 
function}} 
+}
+
+int main() {
+    int result = foo2(10);      
+    return 0;
+}
+


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to