Skip to content

Commit 5305875

Browse files
committed
Merge from 'main' to 'sycl-web' (161 commits)
2 parents ccc1c90 + 7415799 commit 5305875

File tree

773 files changed

+24605
-13820
lines changed

Some content is hidden

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

773 files changed

+24605
-13820
lines changed

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119
if: steps.docs-changed-subprojects.outputs.libc_any_changed == 'true'
120120
run: |
121121
cmake -B libc-build -GNinja -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RUNTIMES="libc" -DLLVM_ENABLE_SPHINX=ON ./runtimes
122-
TZ=UTC ninja -C docs-libc-html
122+
TZ=UTC ninja -C libc-build docs-libc-html
123123
- name: Build LLD docs
124124
if: steps.docs-changed-subprojects.outputs.lld_any_changed == 'true'
125125
run: |

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,35 @@ UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
130130
"::std::error_condition;"
131131
"::std::errc;"
132132
"::std::expected;"
133-
"::boost::system::error_code"))) {}
133+
"::boost::system::error_code"))),
134+
AllowCastToVoid(Options.get("AllowCastToVoid", false)) {}
134135

135136
void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
136137
Options.store(Opts, "CheckedFunctions", CheckedFunctions);
137138
Options.store(Opts, "CheckedReturnTypes",
138139
utils::options::serializeStringList(CheckedReturnTypes));
140+
Options.store(Opts, "AllowCastToVoid", AllowCastToVoid);
139141
}
140142

141143
void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
142144
auto FunVec = utils::options::parseStringList(CheckedFunctions);
143145

144-
auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
145-
callExpr(callee(functionDecl(
146-
// Don't match void overloads of checked functions.
147-
unless(returns(voidType())),
148-
anyOf(isInstantiatedFrom(hasAnyName(FunVec)),
149-
returns(hasCanonicalType(hasDeclaration(
150-
namedDecl(matchers::matchesAnyListedName(
151-
CheckedReturnTypes)))))))))
152-
.bind("match"))));
146+
auto MatchedDirectCallExpr =
147+
expr(callExpr(callee(functionDecl(
148+
// Don't match void overloads of checked functions.
149+
unless(returns(voidType())),
150+
anyOf(isInstantiatedFrom(hasAnyName(FunVec)),
151+
returns(hasCanonicalType(hasDeclaration(
152+
namedDecl(matchers::matchesAnyListedName(
153+
CheckedReturnTypes)))))))))
154+
.bind("match"));
155+
156+
auto CheckCastToVoid =
157+
AllowCastToVoid ? castExpr(unless(hasCastKind(CK_ToVoid))) : castExpr();
158+
auto MatchedCallExpr = expr(
159+
anyOf(MatchedDirectCallExpr,
160+
explicitCastExpr(unless(cxxFunctionalCastExpr()), CheckCastToVoid,
161+
hasSourceExpression(MatchedDirectCallExpr))));
153162

154163
auto UnusedInCompoundStmt =
155164
compoundStmt(forEach(MatchedCallExpr),
@@ -178,8 +187,13 @@ void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
178187
void UnusedReturnValueCheck::check(const MatchFinder::MatchResult &Result) {
179188
if (const auto *Matched = Result.Nodes.getNodeAs<CallExpr>("match")) {
180189
diag(Matched->getBeginLoc(),
181-
"the value returned by this function should be used")
190+
"the value returned by this function should not be disregarded; "
191+
"neglecting it may lead to errors")
182192
<< Matched->getSourceRange();
193+
194+
if (!AllowCastToVoid)
195+
return;
196+
183197
diag(Matched->getBeginLoc(),
184198
"cast the expression to void to silence this warning",
185199
DiagnosticIDs::Note);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ class UnusedReturnValueCheck : public ClangTidyCheck {
2424
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
2525
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2626
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
27+
std::optional<TraversalKind> getCheckTraversalKind() const override {
28+
return TK_IgnoreUnlessSpelledInSource;
29+
}
2730

2831
private:
2932
std::string CheckedFunctions;
3033
const std::vector<StringRef> CheckedReturnTypes;
34+
const bool AllowCastToVoid;
3135
};
3236

3337
} // namespace clang::tidy::bugprone

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class CERTModule : public ClangTidyModule {
328328
ClangTidyOptions::OptionMap &Opts = Options.CheckOptions;
329329
Opts["cert-dcl16-c.NewSuffixes"] = "L;LL;LU;LLU";
330330
Opts["cert-err33-c.CheckedFunctions"] = CertErr33CCheckedFunctions;
331+
Opts["cert-err33-c.AllowCastToVoid"] = "true";
331332
Opts["cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField"] = "false";
332333
Opts["cert-str34-c.DiagnoseSignedUnsignedCharComparisons"] = "false";
333334
return Options;

clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
#include "PreferMemberInitializerCheck.h"
1010
#include "clang/AST/ASTContext.h"
11+
#include "clang/AST/Decl.h"
1112
#include "clang/ASTMatchers/ASTMatchFinder.h"
1213
#include "clang/Lex/Lexer.h"
14+
#include "llvm/ADT/DenseMap.h"
1315

1416
using namespace clang::ast_matchers;
1517

@@ -54,31 +56,66 @@ static bool shouldBeDefaultMemberInitializer(const Expr *Value) {
5456
}
5557

5658
namespace {
59+
5760
AST_MATCHER_P(FieldDecl, indexNotLessThan, unsigned, Index) {
5861
return Node.getFieldIndex() >= Index;
5962
}
63+
64+
enum class AssignedLevel {
65+
// Field is not assigned.
66+
None,
67+
// Field is assigned.
68+
Default,
69+
// Assignment of field has side effect:
70+
// - assign to reference.
71+
// FIXME: support other side effect.
72+
HasSideEffect,
73+
// Assignment of field has data dependence.
74+
HasDependence,
75+
};
76+
6077
} // namespace
6178

79+
static bool canAdvanceAssignment(AssignedLevel Level) {
80+
return Level == AssignedLevel::None || Level == AssignedLevel::Default;
81+
}
82+
6283
// Checks if Field is initialised using a field that will be initialised after
6384
// it.
6485
// TODO: Probably should guard against function calls that could have side
65-
// effects or if they do reference another field that's initialized before this
66-
// field, but is modified before the assignment.
67-
static bool isSafeAssignment(const FieldDecl *Field, const Expr *Init,
68-
const CXXConstructorDecl *Context) {
86+
// effects or if they do reference another field that's initialized before
87+
// this field, but is modified before the assignment.
88+
static void updateAssignmentLevel(
89+
const FieldDecl *Field, const Expr *Init, const CXXConstructorDecl *Ctor,
90+
llvm::DenseMap<const FieldDecl *, AssignedLevel> &AssignedFields) {
91+
auto It = AssignedFields.find(Field);
92+
if (It == AssignedFields.end())
93+
It = AssignedFields.insert({Field, AssignedLevel::None}).first;
94+
95+
if (!canAdvanceAssignment(It->second))
96+
// fast path for already decided field.
97+
return;
98+
99+
if (Field->getType().getCanonicalType()->isReferenceType()) {
100+
// assign to reference type twice cannot be simplified to once.
101+
It->second = AssignedLevel::HasSideEffect;
102+
return;
103+
}
69104

70105
auto MemberMatcher =
71106
memberExpr(hasObjectExpression(cxxThisExpr()),
72107
member(fieldDecl(indexNotLessThan(Field->getFieldIndex()))));
73-
74108
auto DeclMatcher = declRefExpr(
75-
to(varDecl(unless(parmVarDecl()), hasDeclContext(equalsNode(Context)))));
76-
77-
return match(expr(anyOf(MemberMatcher, DeclMatcher,
78-
hasDescendant(MemberMatcher),
79-
hasDescendant(DeclMatcher))),
80-
*Init, Field->getASTContext())
81-
.empty();
109+
to(varDecl(unless(parmVarDecl()), hasDeclContext(equalsNode(Ctor)))));
110+
const bool HasDependence = !match(expr(anyOf(MemberMatcher, DeclMatcher,
111+
hasDescendant(MemberMatcher),
112+
hasDescendant(DeclMatcher))),
113+
*Init, Field->getASTContext())
114+
.empty();
115+
if (HasDependence) {
116+
It->second = AssignedLevel::HasDependence;
117+
return;
118+
}
82119
}
83120

84121
static std::pair<const FieldDecl *, const Expr *>
@@ -99,9 +136,9 @@ isAssignmentToMemberOf(const CXXRecordDecl *Rec, const Stmt *S,
99136
if (!isa<CXXThisExpr>(ME->getBase()))
100137
return std::make_pair(nullptr, nullptr);
101138
const Expr *Init = BO->getRHS()->IgnoreParenImpCasts();
102-
if (isSafeAssignment(Field, Init, Ctor))
103-
return std::make_pair(Field, Init);
104-
} else if (const auto *COCE = dyn_cast<CXXOperatorCallExpr>(S)) {
139+
return std::make_pair(Field, Init);
140+
}
141+
if (const auto *COCE = dyn_cast<CXXOperatorCallExpr>(S)) {
105142
if (COCE->getOperator() != OO_Equal)
106143
return std::make_pair(nullptr, nullptr);
107144

@@ -117,10 +154,8 @@ isAssignmentToMemberOf(const CXXRecordDecl *Rec, const Stmt *S,
117154
if (!isa<CXXThisExpr>(ME->getBase()))
118155
return std::make_pair(nullptr, nullptr);
119156
const Expr *Init = COCE->getArg(1)->IgnoreParenImpCasts();
120-
if (isSafeAssignment(Field, Init, Ctor))
121-
return std::make_pair(Field, Init);
157+
return std::make_pair(Field, Init);
122158
}
123-
124159
return std::make_pair(nullptr, nullptr);
125160
}
126161

@@ -156,6 +191,12 @@ void PreferMemberInitializerCheck::check(
156191
const CXXRecordDecl *Class = Ctor->getParent();
157192
bool FirstToCtorInits = true;
158193

194+
llvm::DenseMap<const FieldDecl *, AssignedLevel> AssignedFields{};
195+
196+
for (const CXXCtorInitializer *Init : Ctor->inits())
197+
if (FieldDecl *Field = Init->getMember())
198+
updateAssignmentLevel(Field, Init->getInit(), Ctor, AssignedFields);
199+
159200
for (const Stmt *S : Body->body()) {
160201
if (S->getBeginLoc().isMacroID()) {
161202
StringRef MacroName = Lexer::getImmediateMacroName(
@@ -180,6 +221,9 @@ void PreferMemberInitializerCheck::check(
180221
std::tie(Field, InitValue) = isAssignmentToMemberOf(Class, S, Ctor);
181222
if (!Field)
182223
continue;
224+
updateAssignmentLevel(Field, InitValue, Ctor, AssignedFields);
225+
if (!canAdvanceAssignment(AssignedFields[Field]))
226+
continue;
183227
const bool IsInDefaultMemberInitializer =
184228
IsUseDefaultMemberInitEnabled && getLangOpts().CPlusPlus11 &&
185229
Ctor->isDefaultConstructor() &&

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeStaticCastDowncastCheck.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,24 @@ using namespace clang::ast_matchers;
1414

1515
namespace clang::tidy::cppcoreguidelines {
1616

17+
ProTypeStaticCastDowncastCheck::ProTypeStaticCastDowncastCheck(
18+
StringRef Name, ClangTidyContext *Context)
19+
: ClangTidyCheck(Name, Context),
20+
StrictMode(Options.getLocalOrGlobal("StrictMode", true)) {}
21+
22+
void ProTypeStaticCastDowncastCheck::storeOptions(
23+
ClangTidyOptions::OptionMap &Opts) {
24+
Options.store(Opts, "StrictMode", StrictMode);
25+
}
26+
1727
void ProTypeStaticCastDowncastCheck::registerMatchers(MatchFinder *Finder) {
1828
Finder->addMatcher(
19-
cxxStaticCastExpr(unless(isInTemplateInstantiation())).bind("cast"),
20-
this);
29+
cxxStaticCastExpr(hasCastKind(CK_BaseToDerived)).bind("cast"), this);
2130
}
2231

2332
void ProTypeStaticCastDowncastCheck::check(
2433
const MatchFinder::MatchResult &Result) {
2534
const auto *MatchedCast = Result.Nodes.getNodeAs<CXXStaticCastExpr>("cast");
26-
if (MatchedCast->getCastKind() != CK_BaseToDerived)
27-
return;
2835

2936
QualType SourceType = MatchedCast->getSubExpr()->getType();
3037
const auto *SourceDecl = SourceType->getPointeeCXXRecordDecl();
@@ -33,15 +40,20 @@ void ProTypeStaticCastDowncastCheck::check(
3340
if (!SourceDecl)
3441
return;
3542

36-
if (SourceDecl->isPolymorphic())
43+
if (SourceDecl->isPolymorphic()) {
3744
diag(MatchedCast->getOperatorLoc(),
3845
"do not use static_cast to downcast from a base to a derived class; "
3946
"use dynamic_cast instead")
4047
<< FixItHint::CreateReplacement(MatchedCast->getOperatorLoc(),
4148
"dynamic_cast");
42-
else
43-
diag(MatchedCast->getOperatorLoc(),
44-
"do not use static_cast to downcast from a base to a derived class");
49+
return;
50+
}
51+
52+
if (!StrictMode)
53+
return;
54+
55+
diag(MatchedCast->getOperatorLoc(),
56+
"do not use static_cast to downcast from a base to a derived class");
4557
}
4658

4759
} // namespace clang::tidy::cppcoreguidelines

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeStaticCastDowncastCheck.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ namespace clang::tidy::cppcoreguidelines {
2020
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/pro-type-static-cast-downcast.html
2121
class ProTypeStaticCastDowncastCheck : public ClangTidyCheck {
2222
public:
23-
ProTypeStaticCastDowncastCheck(StringRef Name, ClangTidyContext *Context)
24-
: ClangTidyCheck(Name, Context) {}
23+
ProTypeStaticCastDowncastCheck(StringRef Name, ClangTidyContext *Context);
2524
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
2625
return LangOpts.CPlusPlus;
2726
}
2827
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2928
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
29+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
30+
std::optional<TraversalKind> getCheckTraversalKind() const override {
31+
return TK_IgnoreUnlessSpelledInSource;
32+
}
33+
34+
private:
35+
const bool StrictMode;
3036
};
3137

3238
} // namespace clang::tidy::cppcoreguidelines

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ Changes in existing checks
232232
<clang-tidy/checks/bugprone/undefined-memory-manipulation>` check to support
233233
fixed-size arrays of non-trivial types.
234234

235+
- Improved :doc:`bugprone-unused-return-value
236+
<clang-tidy/checks/bugprone/unused-return-value>` check diagnostic message,
237+
added support for detection of unused results when cast to non-``void`` type.
238+
Casting to ``void`` no longer suppresses issues by default, control this
239+
behavior with the new `AllowCastToVoid` option.
240+
235241
- Improved :doc:`cppcoreguidelines-avoid-non-const-global-variables
236242
<clang-tidy/checks/cppcoreguidelines/avoid-non-const-global-variables>` check
237243
to ignore ``static`` variables declared within the scope of
@@ -250,7 +256,8 @@ Changes in existing checks
250256

251257
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
252258
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
253-
ignore delegate constructors.
259+
ignore delegate constructors and ignore re-assignment for reference or when
260+
initialization depend on field that is initialized before.
254261

255262
- Improved :doc:`cppcoreguidelines-pro-bounds-array-to-pointer-decay
256263
<clang-tidy/checks/cppcoreguidelines/pro-bounds-array-to-pointer-decay>` check
@@ -269,6 +276,11 @@ Changes in existing checks
269276
<clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check to ignore
270277
dependent delegate constructors.
271278

279+
- Improved :doc:`cppcoreguidelines-pro-type-static-cast-downcast
280+
<clang-tidy/checks/cppcoreguidelines/pro-type-static-cast-downcast>` check to
281+
disregard casts on non-polymorphic types when the `StrictMode` option is set
282+
to `false`.
283+
272284
- Improved :doc:`cppcoreguidelines-pro-type-vararg
273285
<clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check to ignore
274286
false-positives in unevaluated context (e.g., ``decltype``, ``sizeof``, ...).

clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-return-value.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,9 @@ Options
5252
By default the following function return types are checked:
5353
`::std::error_code`, `::std::error_condition`, `::std::errc`, `::std::expected`, `::boost::system::error_code`
5454

55+
.. option:: AllowCastToVoid
56+
57+
Controls whether casting return values to ``void`` is permitted. Default: `false`.
58+
5559
:doc:`cert-err33-c <../cert/err33-c>` is an alias of this check that checks a
5660
fixed and large set of standard library functions.

clang-tools-extra/docs/clang-tidy/checks/cert/err33-c.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ functions are checked:
189189
This check is an alias of check :doc:`bugprone-unused-return-value <../bugprone/unused-return-value>`
190190
with a fixed set of functions.
191191

192+
Suppressing issues by casting to ``void`` is enabled by default and can be
193+
disabled by setting `AllowCastToVoid` option to ``false``.
194+
192195
The check corresponds to a part of CERT C Coding Standard rule `ERR33-C.
193196
Detect and handle standard library errors
194197
<https://wiki.sei.cmu.edu/confluence/display/c/ERR33-C.+Detect+and+handle+standard+library+errors>`_.

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-type-static-cast-downcast.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ unrelated type ``Z``.
1414
This rule is part of the `Type safety (Type.2)
1515
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Pro-type-downcast>`_
1616
profile from the C++ Core Guidelines.
17+
18+
Options
19+
-------
20+
21+
.. option:: StrictMode
22+
23+
When set to `false`, no warnings are emitted for casts on non-polymorphic
24+
types. Default is `true`.

clang-tools-extra/test/clang-tidy/checkers/bugprone/string-constructor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ void Test() {
4949
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter
5050
std::string q2("test", 200);
5151
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
52+
std::string t1("test", 5);
53+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
5254
std::string q3(kText, 200);
5355
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
5456
std::string q4(kText2, 200);
@@ -97,6 +99,8 @@ void Valid() {
9799
std::string s1("test", 4);
98100
std::string s2("test", 3);
99101
std::string s3("test");
102+
std::string s4("test\000", 5);
103+
std::string s6("te" "st", 4);
100104

101105
std::string_view emptyv();
102106
std::string_view sv1("test", 4);

0 commit comments

Comments
 (0)