-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Diagnose [[nodiscard]] return types in Objective-C++ #142541
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
Open
halbi2
wants to merge
4
commits into
llvm:main
Choose a base branch
from
halbi2:diagno
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
|
||
#include "clang/AST/ExprObjC.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/AST/Attr.h" | ||
#include "clang/AST/ComputeDependence.h" | ||
#include "clang/AST/SelectorLocationsKind.h" | ||
#include "clang/AST/Type.h" | ||
|
@@ -272,6 +273,26 @@ QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const { | |
return Ctx.getReferenceQualifiedType(this); | ||
} | ||
|
||
std::pair<const NamedDecl *, const WarnUnusedResultAttr *> | ||
ObjCMessageExpr::getUnusedResultAttr(ASTContext &Ctx) const { | ||
// If the callee is marked nodiscard, return that attribute | ||
if (const ObjCMethodDecl *MD = getMethodDecl()) | ||
if (const auto *A = MD->getAttr<WarnUnusedResultAttr>()) | ||
return {nullptr, A}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't this return the MethodDecl here? |
||
|
||
// If the return type is a struct, union, or enum that is marked nodiscard, | ||
// then return the return type attribute. | ||
if (const TagDecl *TD = getCallReturnType(Ctx)->getAsTagDecl()) | ||
if (const auto *A = TD->getAttr<WarnUnusedResultAttr>()) | ||
return {TD, A}; | ||
|
||
for (const auto *TD = getCallReturnType(Ctx)->getAs<TypedefType>(); TD; | ||
TD = TD->desugar()->getAs<TypedefType>()) | ||
if (const auto *A = TD->getDecl()->getAttr<WarnUnusedResultAttr>()) | ||
return {TD->getDecl(), A}; | ||
return {nullptr, nullptr}; | ||
} | ||
|
||
SourceRange ObjCMessageExpr::getReceiverRange() const { | ||
switch (getReceiverKind()) { | ||
case Instance: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify %s | ||
|
||
struct [[nodiscard]] expected {}; | ||
|
||
typedef struct expected E; | ||
|
||
@interface INTF | ||
- (int) a [[nodiscard]]; | ||
+ (int) b [[nodiscard]]; | ||
- (struct expected) c; | ||
+ (struct expected) d; | ||
- (E) e; | ||
+ (E) f; | ||
- (void) g [[nodiscard]]; // expected-warning {{attribute 'nodiscard' cannot be applied to Objective-C method without return value}} | ||
@end | ||
|
||
void foo(INTF *a) { | ||
[a a]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
[INTF b]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
[a c]; // expected-warning {{ignoring return value of type 'expected' declared with 'nodiscard' attribute}} | ||
[INTF d]; // expected-warning {{ignoring return value of type 'expected' declared with 'nodiscard' attribute}} | ||
[a e]; // expected-warning {{ignoring return value of type 'expected' declared with 'nodiscard' attribute}} | ||
[INTF f]; // expected-warning {{ignoring return value of type 'expected' declared with 'nodiscard' attribute}} | ||
[a g]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify %s | ||
|
||
template<class T> | ||
struct [[nodiscard]] expected {}; | ||
|
||
using E = expected<int>; | ||
|
||
@interface INTF | ||
- (int) a [[nodiscard]]; | ||
+ (int) b [[nodiscard]]; | ||
- (expected<int>) c; | ||
+ (expected<int>) d; | ||
- (E) e; | ||
+ (E) f; | ||
- (void) g [[nodiscard]]; // expected-warning {{attribute 'nodiscard' cannot be applied to Objective-C method without return value}} | ||
@end | ||
|
||
void foo(INTF *a) { | ||
[a a]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
[INTF b]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
[a c]; // expected-warning {{ignoring return value of type 'expected<int>' declared with 'nodiscard' attribute}} | ||
[INTF d]; // expected-warning {{ignoring return value of type 'expected<int>' declared with 'nodiscard' attribute}} | ||
[a e]; // expected-warning {{ignoring return value of type 'expected<int>' declared with 'nodiscard' attribute}} | ||
[INTF f]; // expected-warning {{ignoring return value of type 'expected<int>' declared with 'nodiscard' attribute}} | ||
[a g]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't this be
{MD, A}
?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.
It is this way in CallExpr::getUnusedResultAttribute too. Probably the NamedDecl is supposed to be set only when it is a type declaration.
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 better explain that? What happens if this DOES set the
MD
correctly? Do we have a test that shows the behavior change?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.
Yes, if this is changed to
return {MD, A}
then SemaObjC/method-warn-unused-attribute.m fails withinstead of the expected
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.
For more context, see #112521, but tl;dr all of this is because we want to issue an unused result diagnostic in two different situations: either because the function is declared
nodiscard
or because the return type is declarednodiscard
. If both arenodiscard
, then we prefer printing the function as the cause instead of the type.Uh oh!
There was an error while loading. Please reload this page.
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.
Specifically,
{null, Attribute}
means ‘the function isnodiscard
’;{TD, Attribute}
means ‘the type isnodiscard
, please includeTD
in the diagnostic;{null, null}
means ‘neither isnodiscard
’.I thought there was a comment about this somewhere, but if not should probably add one
Uh oh!
There was an error while loading. Please reload this page.
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.
The attribute I believe is returned so we can get the diagnostic message from it but don’t quote me on that.