-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Warn [[clang::lifetimebound]] misusages on types #118281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Your org has enabled the Graphite merge queue for merging into mainAdd the label “FP Bundles” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link. |
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: Maksim Ivanov (emaxx-google) ChangesEmit 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:
Note that the last example is the case of an unnamed function parameter. While in theory Clang could've supported the Full diff: https://github.com/llvm/llvm-project/pull/118281.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index f32edc5ac06440..200056bddc9b70 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -8612,7 +8612,9 @@ static void HandleLifetimeBoundAttr(TypeProcessingState &State,
CurType = State.getAttributedType(
createSimpleAttr<LifetimeBoundAttr>(State.getSema().Context, Attr),
CurType, CurType);
+ return;
}
+ State.getSema().Diag(Attr.getLoc(), diag::err_attribute_not_type_attr) << Attr << Attr.isRegularKeywordAttribute();
}
static void HandleLifetimeCaptureByAttr(TypeProcessingState &State,
diff --git a/clang/test/SemaCXX/attr-lifetimebound.cpp b/clang/test/SemaCXX/attr-lifetimebound.cpp
index f89b556f5bba08..5f10dea97c29b4 100644
--- a/clang/test/SemaCXX/attr-lifetimebound.cpp
+++ b/clang/test/SemaCXX/attr-lifetimebound.cpp
@@ -9,11 +9,20 @@ namespace usage_invalid {
~A() [[clang::lifetimebound]]; // expected-error {{cannot be applied to a destructor}}
static int *static_class_member() [[clang::lifetimebound]]; // expected-error {{static member function has no implicit object parameter}}
int *explicit_object(this A&) [[clang::lifetimebound]]; // expected-error {{explicit object member function has no implicit object parameter}}
- int not_function [[clang::lifetimebound]]; // expected-error {{only applies to parameters and implicit object parameters}}
- int [[clang::lifetimebound]] also_not_function; // expected-error {{cannot be applied to types}}
+ int attr_on_var [[clang::lifetimebound]]; // expected-error {{only applies to parameters and implicit object parameters}}
+ int [[clang::lifetimebound]] attr_on_int; // expected-error {{cannot be applied to types}}
+ int * [[clang::lifetimebound]] attr_on_int_ptr; // expected-error {{cannot be applied to types}}
+ int * [[clang::lifetimebound]] * attr_on_int_ptr_ptr; // expected-error {{cannot be applied to types}}
+ int (* [[clang::lifetimebound]] attr_on_func_ptr)(); // expected-error {{cannot be applied to types}}
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)'}}
};
int *attr_with_param(int ¶m [[clang::lifetimebound(42)]]); // expected-error {{takes no arguments}}
+
+ void attr_on_ptr_arg(int * [[clang::lifetimebound]] ptr); // expected-error {{cannot be applied to types}}
+ static_assert((int [[clang::lifetimebound]]) 12); // expected-error {{cannot be applied to types}}
+ int* attr_on_unnamed_arg(const int& [[clang::lifetimebound]]); // expected-error {{cannot be applied to types}}
+ template <typename T>
+ int* attr_on_template_ptr_arg(T * [[clang::lifetimebound]] ptr); // expected-error {{cannot be applied to types}}
}
namespace usage_ok {
|
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.
2ba52fc
to
ffba96a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for digging through that! This clearly looks like a good improvement and I wanted to mention that we had users genuinely confused about this before.
I only have a few suggestions, otherwise very supportive of the patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a note in llvm-project/clang/docs/ReleaseNotes.rst?
done - thanks for pointing this out. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good.
Thanks for the reviews! Please merge the PR (I don't have write access). |
Done. |
@emaxx-google Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…18501) This relands #118281 as-is, after it got reverted in commit 356df2d. The reland can go in after we fixed some downstream codebases that had incorrectly placed attributes. Original commit description: > 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]]` analysis for unnamed parameters, it doesn't currently, so the commit at least makes the situation better by highlighting this as a warning instead of a silent ignore - which was reported at #96034.
…vm#118501) This relands llvm#118281 as-is, after it got reverted in commit 356df2d. The reland can go in after we fixed some downstream codebases that had incorrectly placed attributes. Original commit description: > 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]]` analysis for unnamed parameters, it doesn't currently, so the commit at least makes the situation better by highlighting this as a warning instead of a silent ignore - which was reported at llvm#96034.
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:
Note that the last example is the case of an unnamed function parameter. While in theory Clang could've supported the
[[clang::lifetimebound]]
analysis for unnamed parameters, it doesn't currently, so the commit at least makes the situation better by highlighting this as a warning instead of a silent ignore - which was reported at #96034.