-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid #104540
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be 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 If you have received no comments on your PR for a week, you can request a review 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-tools-extra Author: Konstantin Romanov (ksromanov) ChangesFixes #102945 Full diff: https://github.com/llvm/llvm-project/pull/104540.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
index 1ed444e630ec25..0e9185956b7aa8 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
@@ -23,8 +23,12 @@ void ProTypeUnionAccessCheck::registerMatchers(MatchFinder *Finder) {
void ProTypeUnionAccessCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Matched = Result.Nodes.getNodeAs<MemberExpr>("expr");
- diag(Matched->getMemberLoc(),
- "do not access members of unions; use (boost::)variant instead");
+ if (auto MemberLoc = Matched->getMemberLoc(); MemberLoc.isValid())
+ diag(MemberLoc,
+ "do not access members of unions; use (boost::)variant instead");
+ else
+ diag(Matched->getBeginLoc(),
+ "do not access members of unions; use (boost::)variant instead");
}
} // namespace clang::tidy::cppcoreguidelines
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp
index 6abc22b9e4345e..46bb06ba2c8fbe 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp
@@ -5,6 +5,10 @@ union U {
char union_member2;
} u;
+union W {
+ template <class TP> operator TP *() const;
+};
+
struct S {
int non_union_member;
union {
@@ -20,6 +24,7 @@ void f(char);
void f2(U);
void f3(U&);
void f4(U*);
+W f5();
void check()
{
@@ -38,4 +43,6 @@ void check()
f2(u); // OK
f3(u); // OK
f4(&u); // OK
+ void *ret = f5();
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; use (boost::)variant instead
}
|
@llvm/pr-subscribers-clang-tidy Author: Konstantin Romanov (ksromanov) ChangesFixes #102945 Full diff: https://github.com/llvm/llvm-project/pull/104540.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
index 1ed444e630ec25..0e9185956b7aa8 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
@@ -23,8 +23,12 @@ void ProTypeUnionAccessCheck::registerMatchers(MatchFinder *Finder) {
void ProTypeUnionAccessCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Matched = Result.Nodes.getNodeAs<MemberExpr>("expr");
- diag(Matched->getMemberLoc(),
- "do not access members of unions; use (boost::)variant instead");
+ if (auto MemberLoc = Matched->getMemberLoc(); MemberLoc.isValid())
+ diag(MemberLoc,
+ "do not access members of unions; use (boost::)variant instead");
+ else
+ diag(Matched->getBeginLoc(),
+ "do not access members of unions; use (boost::)variant instead");
}
} // namespace clang::tidy::cppcoreguidelines
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp
index 6abc22b9e4345e..46bb06ba2c8fbe 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp
@@ -5,6 +5,10 @@ union U {
char union_member2;
} u;
+union W {
+ template <class TP> operator TP *() const;
+};
+
struct S {
int non_union_member;
union {
@@ -20,6 +24,7 @@ void f(char);
void f2(U);
void f3(U&);
void f4(U*);
+W f5();
void check()
{
@@ -38,4 +43,6 @@ void check()
f2(u); // OK
f3(u); // OK
f4(&u); // OK
+ void *ret = f5();
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; use (boost::)variant instead
}
|
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.
Missing release notes entry, about what's fixed in check.
For me this is an full-fix, not an workaround.
clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
Outdated
Show resolved
Hide resolved
509a17d
to
8f07de0
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.
This is still missing a release note, see
Changes in existing checks |
Functionality-wise, this looks good
Many thanks! This is my first PR to |
…essCheck.cpp Co-authored-by: Piotr Zegar <me@piotrzegar.pl>
aee44f9
to
ed70da9
Compare
@PiotrZSL would you please review once again? |
All of the issues addressed long time ago. |
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! Minus alphabetical ordering in release notes. I'll merge this tomorrow to give time to others just in case.
FWIW this issue actually triggers a crash/assertion failure when those are enabled:
clang-tidy: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp:178: DiagnosticBuilder clang::tidy::ClangTidyContext::diag(StringRef, SourceLocation, StringRef, DiagnosticIDs::Level): Assertion `Loc.isValid()' failed.
[...]
There's a CI failure but looks unrelated. If you have a minute please rebase/merge main to trigger the checks again and hopefully the problem disappears. We can also just keep an eye on the buildbots post-merge tomorrow. EDIT: Turns out I can just do it, never knew I could press that button on the PR. Anyway should re-trigger things :). |
Thank you! |
@ksromanov 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! |
Thanks a lot! |
[cppcoreguidelines-pro-type-union-access] clang-tidy check issues a warning, which location points to the union field access using
getMemberLoc()
. However, sometimesgetMemberLoc()
returns invalid location, for instance when the accessed union member is not a data field, but a function.The main problem is that the check issues warning without location information is only confusing people, when the checked file contains lots of includes. This situation often occurs in practice.
This PR addresses this situation, returning the original expression location if member location is not valid.
Fixes #102945