Skip to content

Commit 7315fce

Browse files
authored
Merge branch 'main' into tracking
2 parents 933e581 + 1862e3c commit 7315fce

File tree

268 files changed

+7009
-2628
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+7009
-2628
lines changed

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -555,22 +555,15 @@ bool NarrowingConversionsCheck::handleConditionalOperator(
555555
// We have an expression like so: `output = cond ? lhs : rhs`
556556
// From the point of view of narrowing conversion we treat it as two
557557
// expressions `output = lhs` and `output = rhs`.
558-
handleConditionalOperatorArgument(Context, Lhs, CO->getLHS());
559-
handleConditionalOperatorArgument(Context, Lhs, CO->getRHS());
558+
handleBinaryOperator(Context, CO->getLHS()->getExprLoc(), Lhs,
559+
*CO->getLHS());
560+
handleBinaryOperator(Context, CO->getRHS()->getExprLoc(), Lhs,
561+
*CO->getRHS());
560562
return true;
561563
}
562564
return false;
563565
}
564566

565-
void NarrowingConversionsCheck::handleConditionalOperatorArgument(
566-
const ASTContext &Context, const Expr &Lhs, const Expr *Arg) {
567-
if (const auto *ICE = llvm::dyn_cast<ImplicitCastExpr>(Arg))
568-
if (!Arg->getIntegerConstantExpr(Context))
569-
Arg = ICE->getSubExpr();
570-
571-
handleBinaryOperator(Context, Arg->getExprLoc(), Lhs, *Arg);
572-
}
573-
574567
void NarrowingConversionsCheck::handleImplicitCast(
575568
const ASTContext &Context, const ImplicitCastExpr &Cast) {
576569
if (Cast.getExprLoc().isMacroID())

clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ class NarrowingConversionsCheck : public ClangTidyCheck {
8585
bool handleConditionalOperator(const ASTContext &Context, const Expr &Lhs,
8686
const Expr &Rhs);
8787

88-
void handleConditionalOperatorArgument(const ASTContext &Context,
89-
const Expr &Lhs, const Expr *Arg);
9088
void handleImplicitCast(const ASTContext &Context,
9189
const ImplicitCastExpr &Cast);
9290

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

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

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

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

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

114118
auto HasOneUnionField = fieldCountOfKindIsOne(UnionField, UnionMatchBindName);
115119
auto HasOneEnumField = fieldCountOfKindIsOne(EnumField, TagMatchBindName);

clang-tools-extra/clang-tidy/tool/run-clang-tidy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def print_profile_data(aggregated_data: Dict[str, float]) -> None:
236236
checkers.items(), key=lambda x: x[1]["user"] + x[1]["sys"], reverse=True
237237
)
238238

239-
def print_stderr(*args, **kwargs) -> None:
239+
def print_stderr(*args: Any, **kwargs: Any) -> None:
240240
print(*args, file=sys.stderr, **kwargs)
241241

242242
print_stderr(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,6 @@ Changes in existing checks
134134
<clang-tidy/checks/bugprone/infinite-loop>` check by adding detection for
135135
variables introduced by structured bindings.
136136

137-
- Improved :doc:`bugprone-narrowing-conversions
138-
<clang-tidy/checks/bugprone/narrowing-conversions>` check by fixing
139-
false positive from analysis of a conditional expression in C.
140-
141137
- Improved :doc:`bugprone-reserved-identifier
142138
<clang-tidy/checks/bugprone/reserved-identifier>` check by ignoring
143139
declarations in system headers.
@@ -146,6 +142,12 @@ Changes in existing checks
146142
<clang-tidy/checks/bugprone/signed-char-misuse>` check by fixing
147143
false positives on C23 enums with the fixed underlying type of signed char.
148144

145+
- Improved :doc:`bugprone-tagged-union-member-count
146+
<clang-tidy/checks/bugprone/tagged-union-member-count>` by fixing a false
147+
positive when enums or unions from system header files or the ``std``
148+
namespace are treated as the tag or the data part of a user-defined
149+
tagged union respectively.
150+
149151
- Improved :doc:`bugprone-unhandled-self-assignment
150152
<clang-tidy/checks/bugprone/unhandled-self-assignment>` check by adding
151153
an additional matcher that generalizes the copy-and-swap idiom pattern

clang-tools-extra/docs/clang-tidy/checks/bugprone/tagged-union-member-count.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ different from the number of data members inside the union.
99
A struct or a class is considered to be a tagged union if it has
1010
exactly one union data member and exactly one enum data member and
1111
any number of other data members that are neither unions or enums.
12+
Furthermore, the types of the union and the enum members must
13+
not come from system header files nor the ``std`` namespace.
1214

1315
Example:
1416

@@ -28,6 +30,25 @@ Example:
2830
} Data;
2931
};
3032
33+
The following example illustrates the exception for unions and enums from
34+
system header files and the ``std`` namespace.
35+
36+
.. code-block:: c++
37+
38+
#include <pthread.h>
39+
40+
struct NotTaggedUnion {
41+
enum MyEnum { MyEnumConstant1, MyEnumConstant2 } En;
42+
pthread_mutex_t Mutex;
43+
};
44+
45+
The ``pthread_mutex_t`` type may be defined as a union behind a ``typedef``,
46+
in which case the check could mistake this type as a user-defined tagged union.
47+
After all, it has exactly one enum data member and exactly one union data member.
48+
To avoid false-positive cases originating from this, unions and enums from
49+
system headers and the ``std`` namespace are ignored when pinpointing the
50+
union part and the enum part of a potential user-defined tagged union.
51+
3152
How enum constants are counted
3253
------------------------------
3354

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#define __SIZEOF_PTHREAD_MUTEX_T 40
2+
3+
namespace std {
4+
typedef union {
5+
struct __pthread_mutex_s {
6+
int __lock;
7+
unsigned int __count;
8+
} __data;
9+
char __size[__SIZEOF_PTHREAD_MUTEX_T];
10+
long int __align;
11+
} pthread_mutex_t;
12+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#define __SIZEOF_PTHREAD_MUTEX_T 40
2+
3+
typedef union {
4+
struct __pthread_mutex_s {
5+
int __lock;
6+
unsigned int __count;
7+
} __data;
8+
char __size[__SIZEOF_PTHREAD_MUTEX_T];
9+
long int __align;
10+
} pthread_mutex_t;

clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-conditional-expressions.c

Lines changed: 0 additions & 21 deletions
This file was deleted.

clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-conditional-expressions.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)