Skip to content

Conversation

@bricknerb
Copy link
Contributor

Fixes: #107556

…on a function parameter while the function returns void.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 23, 2024

@llvm/pr-subscribers-clang

Author: Boaz Brickner (bricknerb)

Changes

Fixes: #107556


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

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+3)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+13-1)
  • (modified) clang/test/SemaCXX/attr-lifetimebound.cpp (+1-2)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 8e4718008ece72..a1c20b22e736ed 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10097,6 +10097,9 @@ def err_lifetimebound_no_object_param : Error<
 def err_lifetimebound_ctor_dtor : Error<
   "'lifetimebound' attribute cannot be applied to a "
   "%select{constructor|destructor}0">;
+def err_lifetimebound_void_return_type : Error<
+  "'lifetimebound' attribute cannot be applied to a parameter of a function "
+  "that returns void; did you mean 'lifetime_capture_by(X)'">;
 
 // CHECK: returning address/reference of stack memory
 def warn_ret_stack_addr_ref : Warning<
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 229c9080d558ec..e611bf60718bc5 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -6940,7 +6940,7 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
     }
   }
 
-  // Check the attributes on the function type, if any.
+  // Check the attributes on the function type and function params, if any.
   if (const auto *FD = dyn_cast<FunctionDecl>(&ND)) {
     // Don't declare this variable in the second operand of the for-statement;
     // GCC miscompiles that by ending its lifetime before evaluating the
@@ -6970,6 +6970,18 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
         }
       }
     }
+
+    for (unsigned int I = 0; I < FD->getNumParams(); ++I) {
+      const ParmVarDecl *P = FD->getParamDecl(I);
+
+      // The [[lifetimebound]] attribute can be applied to a function parameter
+      // only if the function returns a value.
+      if (auto *A = P->getAttr<LifetimeBoundAttr>()) {
+        if (!isa<CXXConstructorDecl>(FD) && FD->getReturnType()->isVoidType()) {
+          S.Diag(A->getLocation(), diag::err_lifetimebound_void_return_type);
+        }
+      }
+    }
   }
 }
 
diff --git a/clang/test/SemaCXX/attr-lifetimebound.cpp b/clang/test/SemaCXX/attr-lifetimebound.cpp
index 1c5c79777c71c8..f790559b6b4769 100644
--- a/clang/test/SemaCXX/attr-lifetimebound.cpp
+++ b/clang/test/SemaCXX/attr-lifetimebound.cpp
@@ -1,8 +1,7 @@
 // RUN: %clang_cc1 -std=c++23 -verify %s
 
 namespace usage_invalid {
-  // FIXME: Should we diagnose a void return type?
-  void voidreturn(int &param [[clang::lifetimebound]]);
+  void voidreturn(int &param [[clang::lifetimebound]]); // expected-error {{'lifetimebound' attribute cannot be applied to a parameter of a function that returns void; did you mean 'lifetime_capture_by(X)'}}
 
   int *not_class_member() [[clang::lifetimebound]]; // expected-error {{non-member function has no implicit object parameter}}
   struct A {

@usx95 usx95 self-requested a review October 23, 2024 14:03
@bricknerb bricknerb changed the title [clang] Output a warning when [[lifetimebound]] attribute is applied on a function parameter while the function returns void [clang] Output an error when [[lifetimebound]] attribute is applied on a function parameter while the function returns void Oct 23, 2024
…ype is dependent on template argument but actually void.
… void. This should diagnose but currently isn't. Added a FIXME comment.
…ror diagnostics that is generated when a function that returns void has [[clang::lifetimebound]] applied to one or more of its parameters.
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo comment. Thanks!

Copy link
Contributor

@usx95 usx95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@usx95 usx95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit for one more test.

@bricknerb bricknerb merged commit 9043bdb into llvm:main Oct 25, 2024
9 checks passed
@bricknerb bricknerb deleted the lifetime2 branch October 25, 2024 19:31
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…n a function parameter while the function returns void (llvm#113460)

Fixes: llvm#107556
ziyao233 added a commit to ziyao233/eweos-packages that referenced this pull request May 12, 2025
LLVM 20 starts to emit errors on void functions whose parameters are
applied with [[lifetimebound]]. Let's simply remove the modifier from
affected sites.

Reference: llvm/llvm-project#113460
Link: desktop-app/tg_owt#150
YukariChiba pushed a commit to eweOS/packages that referenced this pull request May 12, 2025
LLVM 20 starts to emit errors on void functions whose parameters are
applied with [[lifetimebound]]. Let's simply remove the modifier from
affected sites.

Reference: llvm/llvm-project#113460
Link: desktop-app/tg_owt#150
mid-kid pushed a commit to mid-kid/tg_owt that referenced this pull request Jul 5, 2025
LLVM 20 errors on void functions whose parameters are applied
[[lifetimebound]].

Link: llvm/llvm-project#113460
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang] Clang must diagnose the use of [[lifetimebound]] annotation in void-returning functions

4 participants