Skip to content

Commit 78dfd17

Browse files
committed
Merge from 'main' to 'sycl-web' (151 commits)
CONFLICT (content): Merge conflict in clang/include/clang/Driver/Options.td
2 parents ed21504 + 1c0958b commit 78dfd17

File tree

422 files changed

+12705
-3167
lines changed

Some content is hidden

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

422 files changed

+12705
-3167
lines changed

bolt/test/lit.local.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
host_linux_triple = config.target_triple.split("-")[0] + "-unknown-linux-gnu"
2-
common_linker_flags = "-fuse-ld=lld -Wl,--unresolved-symbols=ignore-all"
3-
flags = f"--target={host_linux_triple} {common_linker_flags}"
2+
common_linker_flags = "-fuse-ld=lld -Wl,--unresolved-symbols=ignore-all -pie"
3+
flags = f"--target={host_linux_triple} -fPIE {common_linker_flags}"
44

55
config.substitutions.insert(0, ("%cflags", f"%cflags {flags}"))
66
config.substitutions.insert(0, ("%cxxflags", f"%cxxflags {flags}"))

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "ForwardingReferenceOverloadCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
12-
#include <algorithm>
1312

1413
using namespace clang::ast_matchers;
1514

@@ -19,14 +18,14 @@ namespace {
1918
// Check if the given type is related to std::enable_if.
2019
AST_MATCHER(QualType, isEnableIf) {
2120
auto CheckTemplate = [](const TemplateSpecializationType *Spec) {
22-
if (!Spec || !Spec->getTemplateName().getAsTemplateDecl()) {
21+
if (!Spec)
2322
return false;
24-
}
25-
const NamedDecl *TypeDecl =
26-
Spec->getTemplateName().getAsTemplateDecl()->getTemplatedDecl();
27-
return TypeDecl->isInStdNamespace() &&
28-
(TypeDecl->getName() == "enable_if" ||
29-
TypeDecl->getName() == "enable_if_t");
23+
24+
const TemplateDecl *TDecl = Spec->getTemplateName().getAsTemplateDecl();
25+
26+
return TDecl && TDecl->isInStdNamespace() &&
27+
(TDecl->getName() == "enable_if" ||
28+
TDecl->getName() == "enable_if_t");
3029
};
3130
const Type *BaseType = Node.getTypePtr();
3231
// Case: pointer or reference to enable_if.

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

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ AST_MATCHER_P2(Expr, hasSizeOfDescendant, int, Depth,
4848
return false;
4949
}
5050

51+
AST_MATCHER(Expr, offsetOfExpr) { return isa<OffsetOfExpr>(Node); }
52+
5153
CharUnits getSizeOfType(const ASTContext &Ctx, const Type *Ty) {
5254
if (!Ty || Ty->isIncompleteType() || Ty->isDependentType() ||
5355
isa<DependentSizedArrayType>(Ty) || !Ty->isConstantSizeType())
@@ -221,17 +223,15 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
221223
const auto ElemType =
222224
arrayType(hasElementType(recordType().bind("elem-type")));
223225
const auto ElemPtrType = pointerType(pointee(type().bind("elem-ptr-type")));
226+
const auto SizeofDivideExpr = binaryOperator(
227+
hasOperatorName("/"),
228+
hasLHS(
229+
ignoringParenImpCasts(sizeOfExpr(hasArgumentOfType(hasCanonicalType(
230+
type(anyOf(ElemType, ElemPtrType, type())).bind("num-type")))))),
231+
hasRHS(ignoringParenImpCasts(sizeOfExpr(
232+
hasArgumentOfType(hasCanonicalType(type().bind("denom-type")))))));
224233

225-
Finder->addMatcher(
226-
binaryOperator(
227-
hasOperatorName("/"),
228-
hasLHS(ignoringParenImpCasts(sizeOfExpr(hasArgumentOfType(
229-
hasCanonicalType(type(anyOf(ElemType, ElemPtrType, type()))
230-
.bind("num-type")))))),
231-
hasRHS(ignoringParenImpCasts(sizeOfExpr(
232-
hasArgumentOfType(hasCanonicalType(type().bind("denom-type")))))))
233-
.bind("sizeof-divide-expr"),
234-
this);
234+
Finder->addMatcher(SizeofDivideExpr.bind("sizeof-divide-expr"), this);
235235

236236
// Detect expression like: sizeof(...) * sizeof(...)); most likely an error.
237237
Finder->addMatcher(binaryOperator(hasOperatorName("*"),
@@ -257,8 +257,9 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
257257
.bind("sizeof-sizeof-expr"),
258258
this);
259259

260-
// Detect sizeof in pointer arithmetic like: N * sizeof(S) == P1 - P2 or
261-
// (P1 - P2) / sizeof(S) where P1 and P2 are pointers to type S.
260+
// Detect sizeof usage in comparisons involving pointer arithmetics, such as
261+
// N * sizeof(T) == P1 - P2 or (P1 - P2) / sizeof(T), where P1 and P2 are
262+
// pointers to a type T.
262263
const auto PtrDiffExpr = binaryOperator(
263264
hasOperatorName("-"),
264265
hasLHS(hasType(hasUnqualifiedDesugaredType(pointerType(pointee(
@@ -285,6 +286,47 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
285286
hasRHS(ignoringParenImpCasts(SizeOfExpr.bind("sizeof-ptr-div-expr"))))
286287
.bind("sizeof-in-ptr-arithmetic-div"),
287288
this);
289+
290+
// SEI CERT ARR39-C. Do not add or subtract a scaled integer to a pointer.
291+
// Detect sizeof, alignof and offsetof usage in pointer arithmetics where
292+
// they are used to scale the numeric distance, which is scaled again by
293+
// the pointer arithmetic operator. This can result in forming invalid
294+
// offsets.
295+
//
296+
// Examples, where P is a pointer, N is some integer (both compile-time and
297+
// run-time): P + sizeof(T), P + sizeof(*P), P + N * sizeof(*P).
298+
//
299+
// This check does not warn on cases where the pointee type is "1 byte",
300+
// as those cases can often come from generics and also do not constitute a
301+
// problem because the size does not affect the scale used.
302+
const auto InterestingPtrTyForPtrArithmetic =
303+
pointerType(pointee(qualType().bind("pointee-type")));
304+
const auto SizeofLikeScaleExpr =
305+
expr(anyOf(unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf)),
306+
unaryExprOrTypeTraitExpr(ofKind(UETT_AlignOf)),
307+
offsetOfExpr()))
308+
.bind("sizeof-in-ptr-arithmetic-scale-expr");
309+
const auto PtrArithmeticIntegerScaleExpr = binaryOperator(
310+
hasAnyOperatorName("*", "/"),
311+
// sizeof(...) * sizeof(...) and sizeof(...) / sizeof(...) is handled
312+
// by this check on another path.
313+
hasOperands(expr(hasType(isInteger()), unless(SizeofLikeScaleExpr)),
314+
SizeofLikeScaleExpr));
315+
const auto PtrArithmeticScaledIntegerExpr =
316+
expr(anyOf(SizeofLikeScaleExpr, PtrArithmeticIntegerScaleExpr),
317+
unless(SizeofDivideExpr));
318+
319+
Finder->addMatcher(
320+
expr(anyOf(
321+
binaryOperator(hasAnyOperatorName("+", "-"),
322+
hasOperands(hasType(InterestingPtrTyForPtrArithmetic),
323+
PtrArithmeticScaledIntegerExpr))
324+
.bind("sizeof-in-ptr-arithmetic-plusminus"),
325+
binaryOperator(hasAnyOperatorName("+=", "-="),
326+
hasLHS(hasType(InterestingPtrTyForPtrArithmetic)),
327+
hasRHS(PtrArithmeticScaledIntegerExpr))
328+
.bind("sizeof-in-ptr-arithmetic-plusminus"))),
329+
this);
288330
}
289331

290332
void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
@@ -409,6 +451,43 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
409451
<< SizeOfExpr->getSourceRange() << E->getOperatorLoc()
410452
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
411453
}
454+
} else if (const auto *E = Result.Nodes.getNodeAs<BinaryOperator>(
455+
"sizeof-in-ptr-arithmetic-plusminus")) {
456+
const auto *PointeeTy = Result.Nodes.getNodeAs<QualType>("pointee-type");
457+
const auto *ScaleExpr =
458+
Result.Nodes.getNodeAs<Expr>("sizeof-in-ptr-arithmetic-scale-expr");
459+
const CharUnits PointeeSize = getSizeOfType(Ctx, PointeeTy->getTypePtr());
460+
const int ScaleKind = [ScaleExpr]() {
461+
if (const auto *UTTE = dyn_cast<UnaryExprOrTypeTraitExpr>(ScaleExpr))
462+
switch (UTTE->getKind()) {
463+
case UETT_SizeOf:
464+
return 0;
465+
case UETT_AlignOf:
466+
return 1;
467+
default:
468+
return -1;
469+
}
470+
471+
if (isa<OffsetOfExpr>(ScaleExpr))
472+
return 2;
473+
474+
return -1;
475+
}();
476+
477+
if (ScaleKind != -1 && PointeeSize > CharUnits::One()) {
478+
diag(E->getExprLoc(),
479+
"suspicious usage of '%select{sizeof|alignof|offsetof}0(...)' in "
480+
"pointer arithmetic; this scaled value will be scaled again by the "
481+
"'%1' operator")
482+
<< ScaleKind << E->getOpcodeStr() << ScaleExpr->getSourceRange();
483+
diag(E->getExprLoc(),
484+
"'%0' in pointer arithmetic internally scales with 'sizeof(%1)' == "
485+
"%2",
486+
DiagnosticIDs::Note)
487+
<< E->getOpcodeStr()
488+
<< PointeeTy->getAsString(Ctx.getPrintingPolicy())
489+
<< PointeeSize.getQuantity();
490+
}
412491
}
413492
}
414493

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace clang::tidy::bugprone {
1515

16-
/// Find suspicious usages of sizeof expression.
16+
/// Find suspicious usages of sizeof expressions.
1717
///
1818
/// For the user-facing documentation see:
1919
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/sizeof-expression.html

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../bugprone/ReservedIdentifierCheck.h"
1515
#include "../bugprone/SignalHandlerCheck.h"
1616
#include "../bugprone/SignedCharMisuseCheck.h"
17+
#include "../bugprone/SizeofExpressionCheck.h"
1718
#include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
1819
#include "../bugprone/SuspiciousMemoryComparisonCheck.h"
1920
#include "../bugprone/UnhandledSelfAssignmentCheck.h"
@@ -281,6 +282,9 @@ class CERTModule : public ClangTidyModule {
281282
"cert-oop58-cpp");
282283

283284
// C checkers
285+
// ARR
286+
CheckFactories.registerCheck<bugprone::SizeofExpressionCheck>(
287+
"cert-arr39-c");
284288
// CON
285289
CheckFactories.registerCheck<bugprone::SpuriouslyWakeUpFunctionsCheck>(
286290
"cert-con36-c");
@@ -332,6 +336,12 @@ class CERTModule : public ClangTidyModule {
332336
ClangTidyOptions getModuleOptions() override {
333337
ClangTidyOptions Options;
334338
ClangTidyOptions::OptionMap &Opts = Options.CheckOptions;
339+
Opts["cert-arr39-c.WarnOnSizeOfConstant"] = "false";
340+
Opts["cert-arr39-c.WarnOnSizeOfIntegerExpression"] = "false";
341+
Opts["cert-arr39-c.WarnOnSizeOfThis"] = "false";
342+
Opts["cert-arr39-c.WarnOnSizeOfCompareToConstant"] = "false";
343+
Opts["cert-arr39-c.WarnOnSizeOfPointer"] = "false";
344+
Opts["cert-arr39-c.WarnOnSizeOfPointerToAggregate"] = "false";
335345
Opts["cert-dcl16-c.NewSuffixes"] = "L;LL;LU;LLU";
336346
Opts["cert-err33-c.CheckedFunctions"] = CertErr33CCheckedFunctions;
337347
Opts["cert-err33-c.AllowCastToVoid"] = "true";

clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void DefinitionsInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
102102
// inline is not allowed for main function.
103103
if (FD->isMain())
104104
return;
105-
diag(FD->getLocation(), /*Description=*/"make as 'inline'",
105+
diag(FD->getLocation(), "mark the definition as 'inline'",
106106
DiagnosticIDs::Note)
107107
<< FixItHint::CreateInsertion(FD->getInnerLocStart(), "inline ");
108108
} else if (const auto *VD = dyn_cast<VarDecl>(ND)) {

clang-tools-extra/clang-tidy/modernize/MinMaxUseInitializerListCheck.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ static FindArgsResult findArgs(const CallExpr *Call) {
7272
return Result;
7373
}
7474

75-
static SmallVector<FixItHint>
75+
// Returns `true` as `first` only if a nested call to `std::min` or
76+
// `std::max` was found. Checking if `FixItHint`s were generated is not enough,
77+
// as the explicit casts that the check introduces may be generated without a
78+
// nested `std::min` or `std::max` call.
79+
static std::pair<bool, SmallVector<FixItHint>>
7680
generateReplacements(const MatchFinder::MatchResult &Match,
7781
const CallExpr *TopCall, const FindArgsResult &Result,
7882
const bool IgnoreNonTrivialTypes,
@@ -91,13 +95,15 @@ generateReplacements(const MatchFinder::MatchResult &Match,
9195
const bool IsResultTypeTrivial = ResultType.isTrivialType(*Match.Context);
9296

9397
if ((!IsResultTypeTrivial && IgnoreNonTrivialTypes))
94-
return FixItHints;
98+
return {false, FixItHints};
9599

96100
if (IsResultTypeTrivial &&
97101
static_cast<std::uint64_t>(
98102
Match.Context->getTypeSizeInChars(ResultType).getQuantity()) >
99103
IgnoreTrivialTypesOfSizeAbove)
100-
return FixItHints;
104+
return {false, FixItHints};
105+
106+
bool FoundNestedCall = false;
101107

102108
for (const Expr *Arg : Result.Args) {
103109
const auto *InnerCall = dyn_cast<CallExpr>(Arg->IgnoreParenImpCasts());
@@ -146,6 +152,9 @@ generateReplacements(const MatchFinder::MatchResult &Match,
146152
*Match.Context))
147153
continue;
148154

155+
// We have found a nested call
156+
FoundNestedCall = true;
157+
149158
// remove the function call
150159
FixItHints.push_back(
151160
FixItHint::CreateRemoval(InnerCall->getCallee()->getSourceRange()));
@@ -168,7 +177,7 @@ generateReplacements(const MatchFinder::MatchResult &Match,
168177
CharSourceRange::getTokenRange(InnerResult.First->getEndLoc())));
169178
}
170179

171-
const SmallVector<FixItHint> InnerReplacements = generateReplacements(
180+
const auto [_, InnerReplacements] = generateReplacements(
172181
Match, InnerCall, InnerResult, IgnoreNonTrivialTypes,
173182
IgnoreTrivialTypesOfSizeAbove);
174183

@@ -189,7 +198,7 @@ generateReplacements(const MatchFinder::MatchResult &Match,
189198
}
190199
}
191200

192-
return FixItHints;
201+
return {FoundNestedCall, FixItHints};
193202
}
194203

195204
MinMaxUseInitializerListCheck::MinMaxUseInitializerListCheck(
@@ -238,11 +247,11 @@ void MinMaxUseInitializerListCheck::check(
238247
const auto *TopCall = Match.Nodes.getNodeAs<CallExpr>("topCall");
239248

240249
const FindArgsResult Result = findArgs(TopCall);
241-
const SmallVector<FixItHint> Replacements =
250+
const auto [FoundNestedCall, Replacements] =
242251
generateReplacements(Match, TopCall, Result, IgnoreNonTrivialTypes,
243252
IgnoreTrivialTypesOfSizeAbove);
244253

245-
if (Replacements.empty())
254+
if (!FoundNestedCall)
246255
return;
247256

248257
const DiagnosticBuilder Diagnostic =

clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,18 @@ void EnumInitialValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
141141
}
142142

143143
void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
144-
Finder->addMatcher(
145-
enumDecl(unless(isMacro()), unless(hasConsistentInitialValues()))
146-
.bind("inconsistent"),
147-
this);
144+
Finder->addMatcher(enumDecl(isDefinition(), unless(isMacro()),
145+
unless(hasConsistentInitialValues()))
146+
.bind("inconsistent"),
147+
this);
148148
if (!AllowExplicitZeroFirstInitialValue)
149149
Finder->addMatcher(
150-
enumDecl(hasZeroInitialValueForFirstEnumerator()).bind("zero_first"),
150+
enumDecl(isDefinition(), hasZeroInitialValueForFirstEnumerator())
151+
.bind("zero_first"),
151152
this);
152153
if (!AllowExplicitSequentialInitialValues)
153-
Finder->addMatcher(enumDecl(unless(isMacro()), hasSequentialInitialValues())
154+
Finder->addMatcher(enumDecl(isDefinition(), unless(isMacro()),
155+
hasSequentialInitialValues())
154156
.bind("sequential"),
155157
this);
156158
}
@@ -159,7 +161,7 @@ void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
159161
if (const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("inconsistent")) {
160162
DiagnosticBuilder Diag =
161163
diag(Enum->getBeginLoc(),
162-
"inital values in enum %0 are not consistent, consider explicit "
164+
"initial values in enum %0 are not consistent, consider explicit "
163165
"initialization of all, none or only the first enumerator")
164166
<< Enum;
165167
for (const EnumConstantDecl *ECD : Enum->enumerators())

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,38 @@ New checks
104104
New check aliases
105105
^^^^^^^^^^^^^^^^^
106106

107+
- New alias :doc:`cert-arr39-c <clang-tidy/checks/cert/arr39-c>` to
108+
:doc:`bugprone-sizeof-expression
109+
<clang-tidy/checks/bugprone/sizeof-expression>` was added.
110+
107111
Changes in existing checks
108112
^^^^^^^^^^^^^^^^^^^^^^^^^^
109113

110114
- Improved :doc:`bugprone-casting-through-void
111115
<clang-tidy/checks/bugprone/casting-through-void>` check to suggest replacing
112116
the offending code with ``reinterpret_cast``, to more clearly express intent.
113117

114-
- Improved :doc:`cert-flp30-c<clang-tidy/checks/cert/flp30-c>` check to
118+
- Improved :doc:`bugprone-forwarding-reference-overload
119+
<clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
120+
a crash when determining if an ``enable_if[_t]`` was found.
121+
122+
- Improved :doc:`bugprone-sizeof-expression
123+
<clang-tidy/checks/bugprone/sizeof-expression>` check to find suspicious
124+
usages of ``sizeof()``, ``alignof()``, and ``offsetof()`` when adding or
125+
subtracting from a pointer.
126+
127+
- Improved :doc:`cert-flp30-c <clang-tidy/checks/cert/flp30-c>` check to
115128
fix false positive that floating point variable is only used in increment
116129
expression.
117130

118131
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
119-
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to avoid
120-
false positive when member initialization depends on a structured binging
121-
variable.
132+
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
133+
avoid false positive when member initialization depends on a structured
134+
binding variable.
135+
136+
- Improved :doc:`misc-definitions-in-headers
137+
<clang-tidy/checks/misc/definitions-in-headers>` check by rewording the
138+
diagnostic note that suggests adding ``inline``.
122139

123140
- Improved :doc:`modernize-use-std-format
124141
<clang-tidy/checks/modernize/use-std-format>` check to support replacing
@@ -128,10 +145,20 @@ Changes in existing checks
128145
<clang-tidy/checks/misc/unconventional-assign-operator>` check to avoid
129146
false positive for C++23 deducing this.
130147

148+
- Improved :doc:`modernize-min-max-use-initializer-list
149+
<clang-tidy/checks/modernize/min-max-use-initializer-list>` check by fixing
150+
a false positive when only an implicit conversion happened inside an
151+
initializer list.
152+
131153
- Improved :doc:`modernize-use-std-print
132154
<clang-tidy/checks/modernize/use-std-print>` check to support replacing
133155
member function calls too.
134156

157+
- Improved :doc:`readability-enum-initial-value
158+
<clang-tidy/checks/readability/enum-initial-value>` check by only issuing
159+
diagnostics for the definition of an ``enum``, and by fixing a typo in the
160+
diagnostic.
161+
135162
- Improved :doc:`performance-avoid-endl
136163
<clang-tidy/checks/performance/avoid-endl>` check to use ``std::endl`` as
137164
placeholder when lexer cannot get source text.

0 commit comments

Comments
 (0)