-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang][Sema] Refine unused-member-function diagnostic message for constructors #84515
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
guillem-bartrina-sonarsource
wants to merge
5
commits into
llvm:main
Choose a base branch
from
guillem-bartrina-sonarsource:gb/unused-constructor
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
5 commits
Select commit
Hold shift + click to select a range
473e8bb
[clang][Sema] Refine unused-member-function diagnostic message for co…
guillem-bartrina-sonarsource 0a73493
Remove constructor name from diagnostic
guillem-bartrina-sonarsource 0e5e098
Add release note
guillem-bartrina-sonarsource e53a966
Rephrase release note
guillem-bartrina-sonarsource edf945a
Merge branch 'main' into gb/unused-constructor
guillem-bartrina-sonarsource 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,10 +76,33 @@ struct S { | |
struct SVS : public VS { | ||
void vm() { } | ||
}; | ||
|
||
struct CS { | ||
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. When switching the diagnostic approach, be sure to add test coverage for unused copy/move constructors, copy/move assignment, and destructors (you can get an unused destructor if you only use |
||
CS() {} | ||
CS(bool a) {} | ||
CS(int b) {} // expected-warning{{unused constructor}} | ||
CS(float c); | ||
}; | ||
|
||
struct DCS : public CS { | ||
DCS() = default; // expected-warning{{unused constructor}} | ||
DCS(bool a) : CS(a) {} // expected-warning{{unused constructor}} | ||
DCS(const DCS&) {} | ||
DCS(DCS&&) {} // expected-warning{{unused constructor}} | ||
}; | ||
|
||
template<typename T> | ||
struct TCS { | ||
TCS(); | ||
}; | ||
template <typename T> TCS<T>::TCS() {} | ||
template <> TCS<int>::TCS() {} // expected-warning{{unused constructor}} | ||
} | ||
|
||
void S::m3() {} // expected-warning{{unused member function 'm3'}} | ||
|
||
CS::CS(float c) {} // expected-warning{{unused constructor}} | ||
|
||
static inline void f4() {} // expected-warning{{unused function 'f4'}} | ||
const unsigned int cx = 0; // expected-warning{{unused variable 'cx'}} | ||
const unsigned int cy = 0; | ||
|
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.
I would recommend using
%sub{select_special_member_kind}1
instead of manually spelling out constructor; seenote_member_synthesized_at
for an example of it being used.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.
Hello @AaronBallman, and thanks for this recommendation.
I clearly see the advantages it brings, namely refining the message also for other special members and avoiding having to do the type test for the different nodes at the diagnostic creation site.
However, note that only default constructors are considered “special members” (see
CXXSpecialMemberKind
). Therefore, using this trick would not avoid having to manually spellconstructor
in the template because it would still be necessary to treat them specially. The trick would just refine the message for other special members with little additional effort.The template would then be something like
And the implementation
WDYT?
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 supports more than just a default constructor:
llvm-project/clang/include/clang/Sema/Sema.h
Line 422 in d0fb835
So I was thinking something more like:
and
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,
select_special_member_kind
supports more than the default constructor: all other special members (copy constructor, move constructor, copy assignment operator, move assignment operator and destructor).However, this PR was originally to refine the diagnostic message for all constructors, not just the default, copy and move ones.
With your approach, which only uses
select_special_member_kind
,Only the message will be refined for constructors with an arrow, because the other constructors are not considered “special members”.