Skip to content

Commit 4405d65

Browse files
committed
[clang] Output a warning when [[lifetimebound]] attribute is applied on a function parameter while the function returns void.
1 parent ad70f3e commit 4405d65

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10097,6 +10097,9 @@ def err_lifetimebound_no_object_param : Error<
1009710097
def err_lifetimebound_ctor_dtor : Error<
1009810098
"'lifetimebound' attribute cannot be applied to a "
1009910099
"%select{constructor|destructor}0">;
10100+
def err_lifetimebound_void_return_type : Error<
10101+
"'lifetimebound' attribute cannot be applied to a parameter of a function "
10102+
"that returns void; did you mean 'lifetime_capture_by(X)'">;
1010010103

1010110104
// CHECK: returning address/reference of stack memory
1010210105
def warn_ret_stack_addr_ref : Warning<

clang/lib/Sema/SemaDecl.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6940,7 +6940,7 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
69406940
}
69416941
}
69426942

6943-
// Check the attributes on the function type, if any.
6943+
// Check the attributes on the function type and function params, if any.
69446944
if (const auto *FD = dyn_cast<FunctionDecl>(&ND)) {
69456945
// Don't declare this variable in the second operand of the for-statement;
69466946
// GCC miscompiles that by ending its lifetime before evaluating the
@@ -6970,6 +6970,18 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
69706970
}
69716971
}
69726972
}
6973+
6974+
for (unsigned int I = 0; I < FD->getNumParams(); ++I) {
6975+
const ParmVarDecl *P = FD->getParamDecl(I);
6976+
6977+
// The [[lifetimebound]] attribute can be applied to a function parameter
6978+
// only if the function returns a value.
6979+
if (auto *A = P->getAttr<LifetimeBoundAttr>()) {
6980+
if (!isa<CXXConstructorDecl>(FD) && FD->getReturnType()->isVoidType()) {
6981+
S.Diag(A->getLocation(), diag::err_lifetimebound_void_return_type);
6982+
}
6983+
}
6984+
}
69736985
}
69746986
}
69756987

clang/test/SemaCXX/attr-lifetimebound.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// RUN: %clang_cc1 -std=c++23 -verify %s
22

33
namespace usage_invalid {
4-
// FIXME: Should we diagnose a void return type?
5-
void voidreturn(int &param [[clang::lifetimebound]]);
4+
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)'}}
65

76
int *not_class_member() [[clang::lifetimebound]]; // expected-error {{non-member function has no implicit object parameter}}
87
struct A {

0 commit comments

Comments
 (0)