Skip to content

Commit ae34279

Browse files
committed
[clang] Output an error when [[lifetimebound]] attribute is applied on a function implicit object parameter while the function returns void
Fixes: llvm#107556
1 parent 652988b commit ae34279

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ C++ Specific Potentially Breaking Changes
140140
unsigned operator""_udl_name(unsigned long long);
141141

142142
- Clang will now produce an error diagnostic when [[clang::lifetimebound]] is
143-
applied on a parameter of a function that returns void. This was previously
144-
ignored and had no effect. (#GH107556)
143+
applied on a parameter or an implicit object parameter of a function that
144+
returns void. This was previously ignored and had no effect. (#GH107556)
145145

146146
.. code-block:: c++
147147

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10101,9 +10101,12 @@ def err_lifetimebound_no_object_param : Error<
1010110101
def err_lifetimebound_ctor_dtor : Error<
1010210102
"'lifetimebound' attribute cannot be applied to a "
1010310103
"%select{constructor|destructor}0">;
10104-
def err_lifetimebound_void_return_type : Error<
10104+
def err_lifetimebound_parameter_void_return_type : Error<
1010510105
"'lifetimebound' attribute cannot be applied to a parameter of a function "
1010610106
"that returns void">;
10107+
def err_lifetimebound_implicit_object_parameter_void_return_type : Error<
10108+
"'lifetimebound' attribute cannot be applied to an implicit object "
10109+
"parameter of a function that returns void">;
1010710110

1010810111
// CHECK: returning address/reference of stack memory
1010910112
def warn_ret_stack_addr_ref : Warning<

clang/lib/Sema/SemaDecl.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6967,6 +6967,11 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
69676967
} else if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) {
69686968
S.Diag(A->getLocation(), diag::err_lifetimebound_ctor_dtor)
69696969
<< isa<CXXDestructorDecl>(MD) << A->getRange();
6970+
} else if (FD->getReturnType()->isVoidType()) {
6971+
S.Diag(
6972+
FD->getLocation(),
6973+
diag::
6974+
err_lifetimebound_implicit_object_parameter_void_return_type);
69706975
}
69716976
}
69726977
}
@@ -6978,7 +6983,8 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
69786983
// only if the function returns a value.
69796984
if (auto *A = P->getAttr<LifetimeBoundAttr>()) {
69806985
if (!isa<CXXConstructorDecl>(FD) && FD->getReturnType()->isVoidType()) {
6981-
S.Diag(A->getLocation(), diag::err_lifetimebound_void_return_type);
6986+
S.Diag(A->getLocation(),
6987+
diag::err_lifetimebound_parameter_void_return_type);
69826988
}
69836989
}
69846990
}

clang/test/SemaCXX/attr-lifetimebound.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ namespace usage_invalid {
1111
int *explicit_object(this A&) [[clang::lifetimebound]]; // expected-error {{explicit object member function has no implicit object parameter}}
1212
int not_function [[clang::lifetimebound]]; // expected-error {{only applies to parameters and implicit object parameters}}
1313
int [[clang::lifetimebound]] also_not_function; // expected-error {{cannot be applied to types}}
14-
// FIXME: Should diagnose a void return type.
15-
void void_return_member() [[clang::lifetimebound]];
14+
void void_return_member() [[clang::lifetimebound]]; // expected-error {{'lifetimebound' attribute cannot be applied to an implicit object parameter of a function that returns void}}
1615
};
1716
int *attr_with_param(int &param [[clang::lifetimebound(42)]]); // expected-error {{takes no arguments}}
1817
}

0 commit comments

Comments
 (0)