[clang-tools-extra] [clang-tidy] Workaround for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov created https://github.com/llvm/llvm-project/pull/104540 Fixes #102945 >From 5c5f8496341e7b97faf62dad2ba7e0b210bde11b Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 9 Aug 2024 23:40:07 -0400 Subject: [PATCH] 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("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 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 } ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Workaround for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov updated https://github.com/llvm/llvm-project/pull/104540 >From 5c5f8496341e7b97faf62dad2ba7e0b210bde11b Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 9 Aug 2024 23:40:07 -0400 Subject: [PATCH 1/2] 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("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 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 509a17dedc92f78c35aaff602246c38611a68417 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 16 Aug 2024 12:35:30 -0400 Subject: [PATCH 2/2] Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp Co-authored-by: Piotr Zegar --- .../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("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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Workaround for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov updated https://github.com/llvm/llvm-project/pull/104540 >From 47db38f62940e6bca72a51f519a5b78da25a38c2 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 9 Aug 2024 23:40:07 -0400 Subject: [PATCH 1/4] 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("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 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 933219ac6c8c6608bf338c85b05fd756b100342c Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 16 Aug 2024 12:35:30 -0400 Subject: [PATCH 2/4] Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp Co-authored-by: Piotr Zegar --- .../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("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 1a2dae46c383f28a9bcb8ca4fa09c6cc4921e7d4 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 16 Aug 2024 12:37:07 -0400 Subject: [PATCH 3/4] 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("expr"); SourceLocation Loc = Matched->getMemberLoc(); if (Loc.isInvalid()) - Loc = Matched->getBeginLoc(); - diag(Loc, "do not access members of unions; use (boost::)
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov edited https://github.com/llvm/llvm-project/pull/104540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov edited https://github.com/llvm/llvm-project/pull/104540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov updated https://github.com/llvm/llvm-project/pull/104540 >From 47db38f62940e6bca72a51f519a5b78da25a38c2 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 9 Aug 2024 23:40:07 -0400 Subject: [PATCH 1/4] 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("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 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 933219ac6c8c6608bf338c85b05fd756b100342c Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 16 Aug 2024 12:35:30 -0400 Subject: [PATCH 2/4] Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp Co-authored-by: Piotr Zegar --- .../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("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 1a2dae46c383f28a9bcb8ca4fa09c6cc4921e7d4 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 16 Aug 2024 12:37:07 -0400 Subject: [PATCH 3/4] 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("expr"); SourceLocation Loc = Matched->getMemberLoc(); if (Loc.isInvalid()) - Loc = Matched->getBeginLoc(); - diag(Loc, "do not access members of unions; use (boost::)
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
ksromanov wrote: > This is still missing a release note, see Many thanks! This is my first PR to `llvm`, I didn't know about it, sorry. https://github.com/llvm/llvm-project/pull/104540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov updated https://github.com/llvm/llvm-project/pull/104540 >From 31ed1cf97be643b3a6c9a05d4e461789c37e2408 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 9 Aug 2024 23:40:07 -0400 Subject: [PATCH 1/5] 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("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 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 f271361fba45dbe5fc5a8d566c60e7265d30 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 16 Aug 2024 12:35:30 -0400 Subject: [PATCH 2/5] Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp Co-authored-by: Piotr Zegar --- .../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("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 Date: Fri, 16 Aug 2024 12:37:07 -0400 Subject: [PATCH 3/5] 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("expr"); SourceLocation Loc = Matched->getMemberLoc(); if (Loc.isInvalid()) - Loc = Matched->getBeginLoc(); - diag(Loc, "do not access members of unions; use (boost::)
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov updated https://github.com/llvm/llvm-project/pull/104540 >From 31ed1cf97be643b3a6c9a05d4e461789c37e2408 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 9 Aug 2024 23:40:07 -0400 Subject: [PATCH 1/6] 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("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 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 f271361fba45dbe5fc5a8d566c60e7265d30 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 16 Aug 2024 12:35:30 -0400 Subject: [PATCH 2/6] Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp Co-authored-by: Piotr Zegar --- .../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("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 Date: Fri, 16 Aug 2024 12:37:07 -0400 Subject: [PATCH 3/6] 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("expr"); SourceLocation Loc = Matched->getMemberLoc(); if (Loc.isInvalid()) - Loc = Matched->getBeginLoc(); - diag(Loc, "do not access members of unions; use (boost::)
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
@@ -116,6 +116,10 @@ Changes in existing checks ` check to remove `->`, when redundant `get()` is removed. +- Fixed :doc:`cppcoreguidelines-pro-type-union-access + ` check to + report location even when member location is not valid. ksromanov wrote: Thanks! Articles will always be a problem for me. :-( https://github.com/llvm/llvm-project/pull/104540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov updated https://github.com/llvm/llvm-project/pull/104540 >From 31ed1cf97be643b3a6c9a05d4e461789c37e2408 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 9 Aug 2024 23:40:07 -0400 Subject: [PATCH 1/6] 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("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 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 f271361fba45dbe5fc5a8d566c60e7265d30 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 16 Aug 2024 12:35:30 -0400 Subject: [PATCH 2/6] Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp Co-authored-by: Piotr Zegar --- .../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("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 Date: Fri, 16 Aug 2024 12:37:07 -0400 Subject: [PATCH 3/6] 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("expr"); SourceLocation Loc = Matched->getMemberLoc(); if (Loc.isInvalid()) - Loc = Matched->getBeginLoc(); - diag(Loc, "do not access members of unions; use (boost::)
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
ksromanov wrote: @PiotrZSL would you please review once again? https://github.com/llvm/llvm-project/pull/104540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
@@ -141,6 +141,10 @@ New check aliases Changes in existing checks ^^ +- Fixed :doc:`cppcoreguidelines-pro-type-union-access + ` check to + report a location even when the member location is not valid. ksromanov wrote: Done. https://github.com/llvm/llvm-project/pull/104540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov updated https://github.com/llvm/llvm-project/pull/104540 >From 31ed1cf97be643b3a6c9a05d4e461789c37e2408 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 9 Aug 2024 23:40:07 -0400 Subject: [PATCH 1/6] 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("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 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 f271361fba45dbe5fc5a8d566c60e7265d30 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov Date: Fri, 16 Aug 2024 12:35:30 -0400 Subject: [PATCH 2/6] Update clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp Co-authored-by: Piotr Zegar --- .../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("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 Date: Fri, 16 Aug 2024 12:37:07 -0400 Subject: [PATCH 3/6] 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("expr"); SourceLocation Loc = Matched->getMemberLoc(); if (Loc.isInvalid()) - Loc = Matched->getBeginLoc(); - diag(Loc, "do not access members of unions; use (boost::)
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
ksromanov wrote: All of the issues addressed long time ago. https://github.com/llvm/llvm-project/pull/104540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
ksromanov wrote: > 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! https://github.com/llvm/llvm-project/pull/104540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
https://github.com/ksromanov updated https://github.com/llvm/llvm-project/pull/104540 >From 31ed1cf97be643b3a6c9a05d4e461789c37e2408 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov 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("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 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 f271361fba45dbe5fc5a8d566c60e7265d30 Mon Sep 17 00:00:00 2001 From: Konstantin Romanov 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 --- .../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("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 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("expr"); SourceLocation Loc = Matched->getMemberLoc(); if (Loc.isInvalid()) - Loc = Matched->getBeginLoc(); - diag(Loc, "do not access members of unions; use (boost::)
[clang-tools-extra] [clang-tidy] Fix for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
ksromanov wrote: Thanks a lot! https://github.com/llvm/llvm-project/pull/104540 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits