=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
vbvictor wrote:
Could you please make your email public as per
https://llvm.org/docs/DeveloperPolicy.html#email-addresses.
I'd merge after that.
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
https://github.com/tigbr updated
https://github.com/llvm/llvm-project/pull/135831
>From 61847fc005370b3083be8e5a4f82ee08a0f091d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH 1/8] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++
.../bugprone/tagged-union-member-count.rst| 22 +++
.../bugprone/tagged-union-member-count.c | 13 +++
.../bugprone/tagged-union-member-count.cpp| 13 +++
.../bugprone/tagged-union-member-count.m | 13 +++
.../bugprone/tagged-union-member-count.mm | 13 +++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index c1ea63cda5003..ddbb14e3ac62b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -105,11 +105,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto HasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto HasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
---
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data
member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
--
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the un
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
https://github.com/tigbr updated
https://github.com/llvm/llvm-project/pull/135831
>From 61847fc005370b3083be8e5a4f82ee08a0f091d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH 1/7] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++
.../bugprone/tagged-union-member-count.rst| 22 +++
.../bugprone/tagged-union-member-count.c | 13 +++
.../bugprone/tagged-union-member-count.cpp| 13 +++
.../bugprone/tagged-union-member-count.m | 13 +++
.../bugprone/tagged-union-member-count.mm | 13 +++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index c1ea63cda5003..ddbb14e3ac62b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -105,11 +105,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto HasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto HasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
---
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data
member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
--
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged unio
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=
Message-ID:
In-Reply-To:
@@ -110,6 +110,19 @@ Changes in existing checks
` check by fixing
false positives on C23 enums with the fixed underlying type of signed char.
+- Improved :doc:`bugprone-tagged-union-member-count
+ ` by fixing a false
+ positive when enums or unions from system header files or the ``std``
+ namespace are treated as the tag or the data part of a user-defined
+ tagged union respectively.
+
+- Improved :doc:`bugprone-unchecked-optional-access
vbvictor wrote:
This one I guess is a rebase artifact, need to be deleted
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=
Message-ID:
In-Reply-To:
@@ -110,6 +110,19 @@ Changes in existing checks
` check by fixing
false positives on C23 enums with the fixed underlying type of signed char.
+- Improved :doc:`bugprone-tagged-union-member-count
+ ` by fixing a false
+ positive when enums or unions from system header files or the ``std``
+ namespace are treated as the tag or the data part of a user-defined
+ tagged union respectively.
+
+- Improved :doc:`bugprone-unchecked-optional-access
EugeneZelenko wrote:
Rebase artifact.
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
https://github.com/tigbr updated
https://github.com/llvm/llvm-project/pull/135831
>From 61847fc005370b3083be8e5a4f82ee08a0f091d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH 1/6] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++
.../bugprone/tagged-union-member-count.rst| 22 +++
.../bugprone/tagged-union-member-count.c | 13 +++
.../bugprone/tagged-union-member-count.cpp| 13 +++
.../bugprone/tagged-union-member-count.m | 13 +++
.../bugprone/tagged-union-member-count.mm | 13 +++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index c1ea63cda5003..ddbb14e3ac62b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -105,11 +105,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto HasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto HasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
---
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data
member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
--
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This s
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=
Message-ID:
In-Reply-To:
@@ -9,6 +9,8 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+Furthermore, the types of the union and the enum members must
+not come from a system header files or the ``std`` namespace
5chmidti wrote:
Given the block below, this might be redundant. (Also, wording)
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
tigbr wrote:
Thank you all for the reviews so far. Please let me know if you any further
suggestions.
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
https://github.com/tigbr updated
https://github.com/llvm/llvm-project/pull/135831
>From 525459a04dd6e7d0079095ac531c7cd712ac91d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH 1/6] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++
.../bugprone/tagged-union-member-count.rst| 22 +++
.../bugprone/tagged-union-member-count.c | 13 +++
.../bugprone/tagged-union-member-count.cpp| 13 +++
.../bugprone/tagged-union-member-count.m | 13 +++
.../bugprone/tagged-union-member-count.mm | 13 +++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index db99ef3786e5f..b91da7db39463 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
---
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data
member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
--
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This s
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=
Message-ID:
In-Reply-To:
@@ -140,6 +140,12 @@ Changes in existing checks
calls of ``std::string`` constructor with char pointer, start position and
length parameters.
+- Improved :doc:`bugprone-tagged-union-member-count
+ ` by fixing a false
+ positive when typedefed enums or unions from system header files or the
5chmidti wrote:
not just aliased unions and enums, all unions and enums
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
@@ -147,3 +148,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
5chmidti wrote:
nit: its not just aliases, all unions from system headers are treated like this
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the ``std`` namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
5chmidti wrote:
Maybe `Union and enum data members that are from system header files or
the ``std`` namespace are not considered to be the union member or tagging enum
member respectively of a user-defined tagged union type.`
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
https://github.com/tigbr updated
https://github.com/llvm/llvm-project/pull/135831
>From 525459a04dd6e7d0079095ac531c7cd712ac91d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH 1/5] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++
.../bugprone/tagged-union-member-count.rst| 22 +++
.../bugprone/tagged-union-member-count.c | 13 +++
.../bugprone/tagged-union-member-count.cpp| 13 +++
.../bugprone/tagged-union-member-count.m | 13 +++
.../bugprone/tagged-union-member-count.mm | 13 +++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index db99ef3786e5f..b91da7db39463 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
---
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data
member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
--
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-define
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
https://github.com/vbvictor commented:
LGMT, with nit.
Please wait for a review from someone with write access who will help merge
this PR.
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=
Message-ID:
In-Reply-To:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the ``std`` namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The ``pthread_mutex_t`` type may be defined as a union behind a typedef,
vbvictor wrote:
```suggestion
The ``pthread_mutex_t`` type may be defined as a union behind a ``typedef``,
```
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
https://github.com/tigbr updated
https://github.com/llvm/llvm-project/pull/135831
>From 525459a04dd6e7d0079095ac531c7cd712ac91d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH 1/4] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++
.../bugprone/tagged-union-member-count.rst| 22 +++
.../bugprone/tagged-union-member-count.c | 13 +++
.../bugprone/tagged-union-member-count.cpp| 13 +++
.../bugprone/tagged-union-member-count.m | 13 +++
.../bugprone/tagged-union-member-count.mm | 13 +++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index db99ef3786e5f..b91da7db39463 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
---
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data
member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
--
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthre
https://github.com/tigbr updated
https://github.com/llvm/llvm-project/pull/135831
>From 525459a04dd6e7d0079095ac531c7cd712ac91d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH 1/2] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++
.../bugprone/tagged-union-member-count.rst| 22 +++
.../bugprone/tagged-union-member-count.c | 13 +++
.../bugprone/tagged-union-member-count.cpp| 13 +++
.../bugprone/tagged-union-member-count.m | 13 +++
.../bugprone/tagged-union-member-count.mm | 13 +++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index db99ef3786e5f..b91da7db39463 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
---
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data
member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
--
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typedefed union.
+struct SystemTypedefedUnionDataMemberShouldBeIgnored {
+ pthread_mutex_t Mutex;
+ enu
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
vbvictor wrote:
I'm not sure if we need to check explicitly if some `struct`/`enum` comes from
`std` since all `std` namespace is already placed in system headers unless some
niece cases like `std::hash<>`. I'm okay leaving it as is, but please then add
test with `std` namespace because now only system headers are tested.
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?=,=?utf-8?b?R8OhYm9yIFTDs3RodsOhcmk=?Message-ID:
In-Reply-To:
https://github.com/tigbr updated
https://github.com/llvm/llvm-project/pull/135831
>From 525459a04dd6e7d0079095ac531c7cd712ac91d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH 1/3] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++
.../bugprone/tagged-union-member-count.rst| 22 +++
.../bugprone/tagged-union-member-count.c | 13 +++
.../bugprone/tagged-union-member-count.cpp| 13 +++
.../bugprone/tagged-union-member-count.m | 13 +++
.../bugprone/tagged-union-member-count.mm | 13 +++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index db99ef3786e5f..b91da7db39463 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
---
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data
member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
--
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typede
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
EugeneZelenko wrote:
```suggestion
the ``std`` namespace are not considered to make up the tagged union part
```
https://github.com/llvm/llvm-project/pull/135831
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
https://github.com/tigbr updated
https://github.com/llvm/llvm-project/pull/135831
>From 525459a04dd6e7d0079095ac531c7cd712ac91d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp | 12 ++
.../bugprone/tagged-union-member-count.rst| 22 +++
.../bugprone/tagged-union-member-count.c | 13 +++
.../bugprone/tagged-union-member-count.cpp| 13 +++
.../bugprone/tagged-union-member-count.m | 13 +++
.../bugprone/tagged-union-member-count.mm | 13 +++
6 files changed, 82 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index db99ef3786e5f..b91da7db39463 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
index 2f1036c10345e..b47a49543143b 100644
---
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
+++
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst
@@ -9,6 +9,9 @@ different from the number of data members inside the union.
A struct or a class is considered to be a tagged union if it has
exactly one union data member and exactly one enum data member and
any number of other data members that are neither unions or enums.
+The union and enum data members that are from system header files or
+the std namespace are not considered to make up the tagged union part
+of a user-defined tagged union type.
Example:
@@ -28,6 +31,25 @@ Example:
} Data;
};
+The following example illustrates the exception for unions and enums from
+system header files and the std namespace.
+
+.. code-block:: c++
+
+ #include
+
+ struct NotTaggedUnion {
+enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
+pthread_mutex_t Mutex;
+ };
+
+The pthread_mutex_t type may be defined as a union behind a typedef,
+in which case the check could mistake this type as a user-defined tagged union.
+After all it has exactly one enum data member and exactly one union data
member.
+To avoid false-positive cases originating from this, unions and enums from
+system headers and the std namespace are ignored when pinpointing the
+union part and the enum part of a potential user-defined tagged union.
+
How enum constants are counted
--
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typedefed union.
+struct SystemTypedefedUnionDataMemberShouldBeIgnored {
+ pthread_mutex_t Mutex;
+ enum {
llvmbot wrote:
@llvm/pr-subscribers-clang-tidy
Author: None (tigbr)
Changes
This patch implements a fix for the false-positive case described in [issue
#134840](https://github.com/llvm/llvm-project/issues/134840) for the
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
clang-tidy check.
The example given in the linked issue was the following:
```C
#include
typedef enum {
MYENUM_ONE,
MYENUM_TWO,
} myEnumT;
typedef struct {
pthread_mutex_t mtx;
myEnumT my_enum;
} myTypeT;
```
The
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
check emits the following a warning for this struct:
```
:8:9: warning: tagged union has more data members (3) than tags
(2)! [bugprone-tagged-union-member-count]
8 | typedef struct {
| ^
1 warning generated.
```
The issue is that `pthread_mutex_t` can be a union behind a typedef like the
following:
```C
typedef union
{
struct __pthread_mutex_s __data;
char __size[__SIZEOF_PTHREAD_MUTEX_T];
long int __align;
} pthread_mutex_t;
```
>From the checker's point of view therefore `myTypeT` contains a data member
>with an enum type and another data member with a union type and starts
>analyzing it like a user-defined tagged union.
The proposed solution is that the types from system headers and the std
namespace are no longer candidates to be the enum part or the union part of a
user-defined tagged union. This filtering for enums and the std namespace may
not be strictly necessary in this example, however I added it preemptively out
of (perhaps unnecessary) caution.
---
Full diff: https://github.com/llvm/llvm-project/pull/135831.diff
5 Files Affected:
- (modified)
clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp (+8-4)
- (modified)
clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
(+13)
- (modified)
clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
(+13)
- (modified)
clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m
(+13)
- (modified)
clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm
(+13)
``diff
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index db99ef3786e5f..b91da7db39463 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typedefed union.
+struct SystemTypedefedUnionDataMemberShouldBeIgnored {
+ pthread_mutex_t Mutex;
+ enum {
+MyEnum
+ } EnumField;
+};
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
index 25827e8c8de0c..f21c23b87ae44 100644
---
a/clang-tools-extra/te
llvmbot wrote:
@llvm/pr-subscribers-clang-tools-extra
Author: None (tigbr)
Changes
This patch implements a fix for the false-positive case described in [issue
#134840](https://github.com/llvm/llvm-project/issues/134840) for the
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
clang-tidy check.
The example given in the linked issue was the following:
```C
#include
typedef enum {
MYENUM_ONE,
MYENUM_TWO,
} myEnumT;
typedef struct {
pthread_mutex_t mtx;
myEnumT my_enum;
} myTypeT;
```
The
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
check emits the following a warning for this struct:
```
:8:9: warning: tagged union has more data members (3) than tags
(2)! [bugprone-tagged-union-member-count]
8 | typedef struct {
| ^
1 warning generated.
```
The issue is that `pthread_mutex_t` can be a union behind a typedef like the
following:
```C
typedef union
{
struct __pthread_mutex_s __data;
char __size[__SIZEOF_PTHREAD_MUTEX_T];
long int __align;
} pthread_mutex_t;
```
>From the checker's point of view therefore `myTypeT` contains a data member
>with an enum type and another data member with a union type and starts
>analyzing it like a user-defined tagged union.
The proposed solution is that the types from system headers and the std
namespace are no longer candidates to be the enum part or the union part of a
user-defined tagged union. This filtering for enums and the std namespace may
not be strictly necessary in this example, however I added it preemptively out
of (perhaps unnecessary) caution.
---
Full diff: https://github.com/llvm/llvm-project/pull/135831.diff
5 Files Affected:
- (modified)
clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp (+8-4)
- (modified)
clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
(+13)
- (modified)
clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
(+13)
- (modified)
clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m
(+13)
- (modified)
clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm
(+13)
``diff
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index db99ef3786e5f..b91da7db39463 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typedefed union.
+struct SystemTypedefedUnionDataMemberShouldBeIgnored {
+ pthread_mutex_t Mutex;
+ enum {
+MyEnum
+ } EnumField;
+};
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp
index 25827e8c8de0c..f21c23b87ae44 100644
---
a/clang-tools-e
https://github.com/tigbr created
https://github.com/llvm/llvm-project/pull/135831
This patch implements a fix for the false-positive case described in [issue
#134840](https://github.com/llvm/llvm-project/issues/134840) for the
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
clang-tidy check.
The example given in the linked issue was the following:
```C
#include
typedef enum {
MYENUM_ONE,
MYENUM_TWO,
} myEnumT;
typedef struct {
pthread_mutex_t mtx;
myEnumT my_enum;
} myTypeT;
```
The
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
check emits the following a warning for this struct:
```
:8:9: warning: tagged union has more data members (3) than tags (2)!
[bugprone-tagged-union-member-count]
8 | typedef struct {
| ^
1 warning generated.
```
The issue is that `pthread_mutex_t` can be a union behind a typedef like the
following:
```C
typedef union
{
struct __pthread_mutex_s __data;
char __size[__SIZEOF_PTHREAD_MUTEX_T];
long int __align;
} pthread_mutex_t;
```
>From the checker's point of view therefore `myTypeT` contains a data member
>with an enum type and another data member with a union type and starts
>analyzing it like a user-defined tagged union.
The proposed solution is that the types from system headers and the std
namespace are no longer candidates to be the enum part or the union part of a
user-defined tagged union. This filtering for enums and the std namespace may
not be strictly necessary in this example, however I added it preemptively out
of (perhaps unnecessary) caution.
>From d69cd614e6a4a082b5a591ba9ed4305736449dfe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3thv=C3=A1ri?=
Date: Mon, 14 Apr 2025 17:09:07 +0200
Subject: [PATCH] [clang-tidy] Fix bugprone-tagged-union-member-count
false-positive
Types from system headers and the std namespace are no longer considered as
the enum part or the union part of a user-defined tagged union.
Fixes #134840
---
.../bugprone/TaggedUnionMemberCountCheck.cpp| 12
.../checkers/bugprone/tagged-union-member-count.c | 13 +
.../checkers/bugprone/tagged-union-member-count.cpp | 13 +
.../checkers/bugprone/tagged-union-member-count.m | 13 +
.../checkers/bugprone/tagged-union-member-count.mm | 13 +
5 files changed, 60 insertions(+), 4 deletions(-)
diff --git
a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index db99ef3786e5f..b91da7db39463 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
- auto UnionField = fieldDecl(hasType(qualType(
- hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion(;
+ auto NotFromSystemHeaderOrStdNamespace =
+ unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
- auto EnumField = fieldDecl(hasType(
- qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()));
+ auto UnionField =
+ fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
+ recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)));
+
+ auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)));
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField,
UnionMatchBindName);
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
index 60c93c553baca..96255c7fdd4fe 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
+++
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c
@@ -147,3 +147,16 @@ struct Name {\
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data
members (4) than tags (3)
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
+
+// Typedefed unions from system header files should be ignored when
+// we are trying to pinpoint the union part in a user-defined tagged union.
+#include "pthread.h"
+
+// This should not be analyzed as a user-defined tagged union,
+// even though pthread_mutex_t may be declared as a typedefed union.
+struct SystemTypedefedUnionDataMemberShouldBeIgnored {
+ pthread_mutex_t Mutex;
+ enum {
+MyEnum
+ } EnumField;
+};
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/bugpr