https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/104540
>From 31ed1cf97be643b3a6c9a05d4e461789c37e2408 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov <konstantinsroma...@gmail.com> Date: Fri, 9 Aug 2024 23:40:07 -0400 Subject: [PATCH 1/7] Workaround for cppcoreguidelines-pro-type-union-access if member location is invalid. --- .../cppcoreguidelines/ProTypeUnionAccessCheck.cpp | 8 ++++++-- .../checkers/cppcoreguidelines/pro-type-union-access.cpp | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) 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 } >From f2713615555fba45dbe5fc5a8d566c60e7265d30 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov <konstantinsroma...@gmail.com> Date: Fri, 16 Aug 2024 12:35:30 -0400 Subject: [PATCH 2/7] Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp Co-authored-by: Piotr Zegar <m...@piotrzegar.pl> --- .../cppcoreguidelines/ProTypeUnionAccessCheck.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp index 0e9185956b7aa8..a5b8d1e9a1696c 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp @@ -23,12 +23,10 @@ void ProTypeUnionAccessCheck::registerMatchers(MatchFinder *Finder) { void ProTypeUnionAccessCheck::check(const MatchFinder::MatchResult &Result) { const auto *Matched = Result.Nodes.getNodeAs<MemberExpr>("expr"); - 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"); + SourceLocation Loc = Matched->getMemberLoc(); + if (Loc.isInvalid()) + Loc = Matched->getBeginLoc(); + diag(Loc, "do not access members of unions; use (boost::)variant instead"); } } // namespace clang::tidy::cppcoreguidelines >From 87f1f4cd3575fd0215b48b02f5aa03ae40251525 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov <konstantinsroma...@gmail.com> Date: Fri, 16 Aug 2024 12:37:07 -0400 Subject: [PATCH 3/7] Address comments. --- .../clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp index a5b8d1e9a1696c..c29c4eb60f9d1a 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp @@ -25,8 +25,9 @@ void ProTypeUnionAccessCheck::check(const MatchFinder::MatchResult &Result) { const auto *Matched = Result.Nodes.getNodeAs<MemberExpr>("expr"); SourceLocation Loc = Matched->getMemberLoc(); if (Loc.isInvalid()) - Loc = Matched->getBeginLoc(); - diag(Loc, "do not access members of unions; use (boost::)variant instead"); + Loc = Matched->getBeginLoc(); + diag(Loc, "do not access members of unions; consider using (boost::)variant " + "instead"); } } // namespace clang::tidy::cppcoreguidelines >From c76eda3beb5f8fe06f7e36f295189610793118dd Mon Sep 17 00:00:00 2001 From: Konstantin Romanov <konstantinsroma...@gmail.com> Date: Fri, 16 Aug 2024 14:03:53 -0400 Subject: [PATCH 4/7] Update test. --- .../cppcoreguidelines/pro-type-union-access.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 46bb06ba2c8fbe..2823d38c9b69e4 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 @@ -29,13 +29,13 @@ W f5(); void check() { u.union_member1 = true; - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access] + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not access members of unions; consider using (boost::)variant instead [cppcoreguidelines-pro-type-union-access] auto b = u.union_member2; - // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not access members of unions; use (boost::)variant instead + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not access members of unions; consider using (boost::)variant instead auto a = &s.union_member; - // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; use (boost::)variant instead + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; consider using (boost::)variant instead f(s.u.union_member2); - // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not access members of unions; use (boost::)variant instead + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not access members of unions; consider using (boost::)variant instead s.non_union_member = 2; // OK @@ -44,5 +44,5 @@ void check() 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 + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; consider using (boost::)variant instead } >From ed70da9f4eb45ebec6fcba0701f6e3c03dfaf920 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov <konstantinsroma...@gmail.com> Date: Sat, 24 Aug 2024 11:14:16 -0400 Subject: [PATCH 5/7] Add note to ReleaseNotes.rst. --- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 1b025e8f90f7ba..5164705f43115c 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -116,6 +116,10 @@ Changes in existing checks <clang-tidy/checks/readability/redundant-smartptr-get>` check to remove `->`, when redundant `get()` is removed. +- Fixed :doc:`cppcoreguidelines-pro-type-union-access + <clang-tidy/checks/cppcoreguidelines/pro-type-union-access>` check to + report location even when member location is not valid. + Removed checks ^^^^^^^^^^^^^^ >From 89360e51e1f7a0692f79b1faf41783af1ebc208e Mon Sep 17 00:00:00 2001 From: Konstantin Romanov <konstantinsroma...@gmail.com> Date: Sat, 24 Aug 2024 12:14:06 -0400 Subject: [PATCH 6/7] Add articles. --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 5164705f43115c..6796697a31f83a 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -118,7 +118,7 @@ Changes in existing checks - Fixed :doc:`cppcoreguidelines-pro-type-union-access <clang-tidy/checks/cppcoreguidelines/pro-type-union-access>` check to - report location even when member location is not valid. + report a location even when the member location is not valid. Removed checks ^^^^^^^^^^^^^^ >From e0f3baedc1354b14aca46d4f03af20cc41f6b12f Mon Sep 17 00:00:00 2001 From: Konstantin Romanov <konstantinsroma...@gmail.com> Date: Mon, 21 Oct 2024 15:39:21 -0400 Subject: [PATCH 7/7] Put note in ReleaseNotes according to alphabetical order. --- clang-tools-extra/docs/ReleaseNotes.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 1cf87b990324f6..9afe497fb3d8bf 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -141,10 +141,6 @@ New check aliases Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ -- Fixed :doc:`cppcoreguidelines-pro-type-union-access - <clang-tidy/checks/cppcoreguidelines/pro-type-union-access>` check to - report a location even when the member location is not valid. - - Improved :doc:`bugprone-casting-through-void <clang-tidy/checks/bugprone/casting-through-void>` check to suggest replacing the offending code with ``reinterpret_cast``, to more clearly express intent. @@ -184,6 +180,10 @@ Changes in existing checks avoid false positive when member initialization depends on a structured binding variable. +- Fixed :doc:`cppcoreguidelines-pro-type-union-access + <clang-tidy/checks/cppcoreguidelines/pro-type-union-access>` check to + report a location even when the member location is not valid. + - Improved :doc:`misc-definitions-in-headers <clang-tidy/checks/misc/definitions-in-headers>` check by rewording the diagnostic note that suggests adding ``inline``. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits