Skip to content

Commit d69cd61

Browse files
committed
[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
1 parent dbeb7c1 commit d69cd61

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,15 @@ void TaggedUnionMemberCountCheck::storeOptions(
106106

107107
void TaggedUnionMemberCountCheck::registerMatchers(MatchFinder *Finder) {
108108

109-
auto UnionField = fieldDecl(hasType(qualType(
110-
hasCanonicalType(recordType(hasDeclaration(recordDecl(isUnion())))))));
109+
auto NotFromSystemHeaderOrStdNamespace =
110+
unless(anyOf(isExpansionInSystemHeader(), isInStdNamespace()));
111111

112-
auto EnumField = fieldDecl(hasType(
113-
qualType(hasCanonicalType(enumType(hasDeclaration(enumDecl()))))));
112+
auto UnionField =
113+
fieldDecl(hasType(qualType(hasCanonicalType(recordType(hasDeclaration(
114+
recordDecl(isUnion(), NotFromSystemHeaderOrStdNamespace)))))));
115+
116+
auto EnumField = fieldDecl(hasType(qualType(hasCanonicalType(
117+
enumType(hasDeclaration(enumDecl(NotFromSystemHeaderOrStdNamespace)))))));
114118

115119
auto hasOneUnionField = fieldCountOfKindIsOne(UnionField, UnionMatchBindName);
116120
auto hasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);

clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,16 @@ struct Name {\
147147

148148
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data members (4) than tags (3)
149149
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
150+
151+
// Typedefed unions from system header files should be ignored when
152+
// we are trying to pinpoint the union part in a user-defined tagged union.
153+
#include "pthread.h"
154+
155+
// This should not be analyzed as a user-defined tagged union,
156+
// even though pthread_mutex_t may be declared as a typedefed union.
157+
struct SystemTypedefedUnionDataMemberShouldBeIgnored {
158+
pthread_mutex_t Mutex;
159+
enum {
160+
MyEnum
161+
} EnumField;
162+
};

clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,16 @@ void DoNotMatchLambdas() {
308308
} u;
309309
auto L = [e, u] () {};
310310
}
311+
312+
// Typedefed unions from system header files should be ignored when
313+
// we are trying to pinpoint the union part in a user-defined tagged union.
314+
#include "pthread.h"
315+
316+
// This should not be analyzed as a user-defined tagged union,
317+
// even though pthread_mutex_t may be declared as a typedefed union.
318+
struct SystemTypedefedUnionDataMemberShouldBeIgnored {
319+
pthread_mutex_t Mutex;
320+
enum {
321+
MyEnum
322+
} EnumField;
323+
};

clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,16 @@
147147

148148
// CHECK-MESSAGES: :[[@LINE+1]]:44: warning: tagged union has more data members (4) than tags (3)
149149
DECLARE_TAGGED_UNION_STRUCT(Tags3, Union4, TaggedUnionStructFromMacro);
150+
151+
// Typedefed unions from system header files should be ignored when
152+
// we are trying to pinpoint the union part in a user-defined tagged union.
153+
#include "pthread.h"
154+
155+
// This should not be analyzed as a user-defined tagged union,
156+
// even though pthread_mutex_t may be declared as a typedefed union.
157+
struct SystemTypedefedUnionDataMemberShouldBeIgnored {
158+
pthread_mutex_t Mutex;
159+
enum {
160+
MyEnum
161+
} EnumField;
162+
};

clang-tools-extra/test/clang-tidy/checkers/bugprone/tagged-union-member-count.mm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,16 @@ void DoNotMatchLambdas() {
307307
} u;
308308
auto L = [e, u] () {};
309309
}
310+
311+
// Typedefed unions from system header files should be ignored when
312+
// we are trying to pinpoint the union part in a user-defined tagged union.
313+
#include "pthread.h"
314+
315+
// This should not be analyzed as a user-defined tagged union,
316+
// even though pthread_mutex_t may be declared as a typedefed union.
317+
struct SystemTypedefedUnionDataMemberShouldBeIgnored {
318+
pthread_mutex_t Mutex;
319+
enum {
320+
MyEnum
321+
} EnumField;
322+
};

0 commit comments

Comments
 (0)