Skip to content

Commit ae5bfa0

Browse files
authored
[clang] Output an error when [[lifetimebound]] attribute is applied on a function implicit object parameter while the function returns void (#114203)
Fixes: #107556
1 parent 0b9f1cc commit ae5bfa0

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

clang/docs/ReleaseNotes.rst

+2-2
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -10103,9 +10103,12 @@ def err_lifetimebound_no_object_param : Error<
1010310103
def err_lifetimebound_ctor_dtor : Error<
1010410104
"'lifetimebound' attribute cannot be applied to a "
1010510105
"%select{constructor|destructor}0">;
10106-
def err_lifetimebound_void_return_type : Error<
10106+
def err_lifetimebound_parameter_void_return_type : Error<
1010710107
"'lifetimebound' attribute cannot be applied to a parameter of a function "
1010810108
"that returns void">;
10109+
def err_lifetimebound_implicit_object_parameter_void_return_type : Error<
10110+
"'lifetimebound' attribute cannot be applied to an implicit object "
10111+
"parameter of a function that returns void">;
1010910112

1011010113
// CHECK: returning address/reference of stack memory
1011110114
def warn_ret_stack_addr_ref : Warning<

clang/lib/Sema/SemaDecl.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -6965,6 +6965,11 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
69656965
} else if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) {
69666966
S.Diag(A->getLocation(), diag::err_lifetimebound_ctor_dtor)
69676967
<< isa<CXXDestructorDecl>(MD) << A->getRange();
6968+
} else if (MD->getReturnType()->isVoidType()) {
6969+
S.Diag(
6970+
MD->getLocation(),
6971+
diag::
6972+
err_lifetimebound_implicit_object_parameter_void_return_type);
69686973
}
69696974
}
69706975
}
@@ -6976,7 +6981,8 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
69766981
// only if the function returns a value.
69776982
if (auto *A = P->getAttr<LifetimeBoundAttr>()) {
69786983
if (!isa<CXXConstructorDecl>(FD) && FD->getReturnType()->isVoidType()) {
6979-
S.Diag(A->getLocation(), diag::err_lifetimebound_void_return_type);
6984+
S.Diag(A->getLocation(),
6985+
diag::err_lifetimebound_parameter_void_return_type);
69806986
}
69816987
}
69826988
}

clang/test/SemaCXX/attr-lifetimebound.cpp

+1-2
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)