Skip to content

Commit 2ba52fc

Browse files
committed
[clang] Warn [[clang::lifetimebound]] misusages on types
Emit the "cannot be applied to types" warning instead of silently ignoring the attribute when it's attempted to be used on a type (instead of a function argument or the function definition). Before this commit, the warning has been printed when the attribute was (mis)used on a decl-specifier, but not in other places in a declarator. Examples where the warning starts being emitted with this commit: int * [[clang::lifetimebound]] x; void f(int * [[clang::lifetimebound]] x); void g(int * [[clang::lifetimebound]]); Note that the last example is the case of an unnamed function parameter. While in theory Clang could've supported the [[clang::lifetimebound]], it doesn't currently, so the commit at least makes the situation better by highlighting this as a warning instead of a silent ignore.
1 parent e477989 commit 2ba52fc

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

clang/lib/Sema/SemaType.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8612,7 +8612,9 @@ static void HandleLifetimeBoundAttr(TypeProcessingState &State,
86128612
CurType = State.getAttributedType(
86138613
createSimpleAttr<LifetimeBoundAttr>(State.getSema().Context, Attr),
86148614
CurType, CurType);
8615+
return;
86158616
}
8617+
State.getSema().Diag(Attr.getLoc(), diag::err_attribute_not_type_attr) << Attr << Attr.isRegularKeywordAttribute();
86168618
}
86178619

86188620
static void HandleLifetimeCaptureByAttr(TypeProcessingState &State,

clang/test/SemaCXX/attr-lifetimebound.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ namespace usage_invalid {
99
~A() [[clang::lifetimebound]]; // expected-error {{cannot be applied to a destructor}}
1010
static int *static_class_member() [[clang::lifetimebound]]; // expected-error {{static member function has no implicit object parameter}}
1111
int *explicit_object(this A&) [[clang::lifetimebound]]; // expected-error {{explicit object member function has no implicit object parameter}}
12-
int not_function [[clang::lifetimebound]]; // expected-error {{only applies to parameters and implicit object parameters}}
13-
int [[clang::lifetimebound]] also_not_function; // expected-error {{cannot be applied to types}}
12+
int attr_on_var [[clang::lifetimebound]]; // expected-error {{only applies to parameters and implicit object parameters}}
13+
int [[clang::lifetimebound]] attr_on_int; // expected-error {{cannot be applied to types}}
14+
int * [[clang::lifetimebound]] attr_on_int_ptr; // expected-error {{cannot be applied to types}}
15+
int * [[clang::lifetimebound]] * attr_on_int_ptr_ptr; // expected-error {{cannot be applied to types}}
16+
int (* [[clang::lifetimebound]] attr_on_func_ptr)(); // expected-error {{cannot be applied to types}}
1417
void void_return_member() [[clang::lifetimebound]]; // expected-error {{'lifetimebound' attribute cannot be applied to an implicit object parameter of a function that returns void; did you mean 'lifetime_capture_by(X)'}}
1518
};
1619
int *attr_with_param(int &param [[clang::lifetimebound(42)]]); // expected-error {{takes no arguments}}
20+
21+
void attr_on_ptr_arg(int * [[clang::lifetimebound]] ptr); // expected-error {{cannot be applied to types}}
22+
static_assert((int [[clang::lifetimebound]]) 12); // expected-error {{cannot be applied to types}}
23+
int* attr_on_unnamed_arg(const int& [[clang::lifetimebound]]); // expected-error {{cannot be applied to types}}
24+
template <typename T>
25+
int* attr_on_template_ptr_arg(T * [[clang::lifetimebound]] ptr); // expected-error {{cannot be applied to types}}
1726
}
1827

1928
namespace usage_ok {

0 commit comments

Comments
 (0)