Skip to content

Commit 1a6d0f3

Browse files
committed
Merge commit '07e6c1609d0a57f7ddc0537b7794be2e0296658b' into llvmspirv_pulldown
2 parents 5ccb6a4 + 07e6c16 commit 1a6d0f3

File tree

1,004 files changed

+68640
-13064
lines changed

Some content is hidden

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

1,004 files changed

+68640
-13064
lines changed

.github/workflows/libcxx-build-and-test.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,17 @@ jobs:
6161
]
6262
cc: [ 'clang-19' ]
6363
cxx: [ 'clang++-19' ]
64-
clang_tidy: [ 'ON' ]
6564
include:
6665
- config: 'generic-gcc'
6766
cc: 'gcc-13'
6867
cxx: 'g++-13'
69-
clang_tidy: 'OFF'
7068
steps:
7169
- uses: actions/checkout@v4
7270
- name: ${{ matrix.config }}.${{ matrix.cxx }}
7371
run: libcxx/utils/ci/run-buildbot ${{ matrix.config }}
7472
env:
7573
CC: ${{ matrix.cc }}
7674
CXX: ${{ matrix.cxx }}
77-
ENABLE_CLANG_TIDY: ${{ matrix.clang_tidy }}
7875
- uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # v4.3.0
7976
if: always()
8077
with:
@@ -102,28 +99,23 @@ jobs:
10299
]
103100
cc: [ 'clang-19' ]
104101
cxx: [ 'clang++-19' ]
105-
clang_tidy: [ 'ON' ]
106102
include:
107103
- config: 'generic-gcc-cxx11'
108104
cc: 'gcc-13'
109105
cxx: 'g++-13'
110-
clang_tidy: 'OFF'
111106
- config: 'generic-cxx23'
112107
cc: 'clang-17'
113108
cxx: 'clang++-17'
114-
clang_tidy: 'OFF'
115109
- config: 'generic-cxx26'
116110
cc: 'clang-18'
117111
cxx: 'clang++-18'
118-
clang_tidy: 'ON'
119112
steps:
120113
- uses: actions/checkout@v4
121114
- name: ${{ matrix.config }}
122115
run: libcxx/utils/ci/run-buildbot ${{ matrix.config }}
123116
env:
124117
CC: ${{ matrix.cc }}
125118
CXX: ${{ matrix.cxx }}
126-
ENABLE_CLANG_TIDY: ${{ matrix.clang_tidy }}
127119
- uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # v4.3.0
128120
if: always() # Upload artifacts even if the build or test suite fails
129121
with:
@@ -188,7 +180,6 @@ jobs:
188180
env:
189181
CC: clang-19
190182
CXX: clang++-19
191-
ENABLE_CLANG_TIDY: "OFF"
192183
- uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # v4.3.0
193184
if: always()
194185
with:

clang-tools-extra/clang-tidy/ClangTidyCheck.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ void ClangTidyCheck::OptionsView::storeInt(ClangTidyOptions::OptionMap &Options,
139139
store(Options, LocalName, llvm::itostr(Value));
140140
}
141141

142+
void ClangTidyCheck::OptionsView::storeUnsigned(
143+
ClangTidyOptions::OptionMap &Options, StringRef LocalName,
144+
uint64_t Value) const {
145+
store(Options, LocalName, llvm::utostr(Value));
146+
}
147+
142148
template <>
143149
void ClangTidyCheck::OptionsView::store<bool>(
144150
ClangTidyOptions::OptionMap &Options, StringRef LocalName,

clang-tools-extra/clang-tidy/ClangTidyCheck.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,10 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
411411
std::enable_if_t<std::is_integral_v<T>>
412412
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
413413
T Value) const {
414-
storeInt(Options, LocalName, Value);
414+
if constexpr (std::is_signed_v<T>)
415+
storeInt(Options, LocalName, Value);
416+
else
417+
storeUnsigned(Options, LocalName, Value);
415418
}
416419

417420
/// Stores an option with the check-local name \p LocalName with
@@ -422,7 +425,7 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
422425
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
423426
std::optional<T> Value) const {
424427
if (Value)
425-
storeInt(Options, LocalName, *Value);
428+
store(Options, LocalName, *Value);
426429
else
427430
store(Options, LocalName, "none");
428431
}
@@ -470,6 +473,8 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
470473
void storeInt(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
471474
int64_t Value) const;
472475

476+
void storeUnsigned(ClangTidyOptions::OptionMap &Options,
477+
StringRef LocalName, uint64_t Value) const;
473478

474479
std::string NamePrefix;
475480
const ClangTidyOptions::OptionMap &CheckOptions;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "PosixReturnCheck.h"
5555
#include "RedundantBranchConditionCheck.h"
5656
#include "ReservedIdentifierCheck.h"
57+
#include "ReturnConstRefFromParameterCheck.h"
5758
#include "SharedPtrArrayMismatchCheck.h"
5859
#include "SignalHandlerCheck.h"
5960
#include "SignedCharMisuseCheck.h"
@@ -137,6 +138,8 @@ class BugproneModule : public ClangTidyModule {
137138
"bugprone-inaccurate-erase");
138139
CheckFactories.registerCheck<IncorrectEnableIfCheck>(
139140
"bugprone-incorrect-enable-if");
141+
CheckFactories.registerCheck<ReturnConstRefFromParameterCheck>(
142+
"bugprone-return-const-ref-from-parameter");
140143
CheckFactories.registerCheck<SwitchMissingDefaultCaseCheck>(
141144
"bugprone-switch-missing-default-case");
142145
CheckFactories.registerCheck<IncDecInConditionsCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_clang_library(clangTidyBugproneModule
2626
ImplicitWideningOfMultiplicationResultCheck.cpp
2727
InaccurateEraseCheck.cpp
2828
IncorrectEnableIfCheck.cpp
29+
ReturnConstRefFromParameterCheck.cpp
2930
SuspiciousStringviewDataUsageCheck.cpp
3031
SwitchMissingDefaultCaseCheck.cpp
3132
IncDecInConditionsCheck.cpp
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--- ReturnConstRefFromParameterCheck.cpp - clang-tidy ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "ReturnConstRefFromParameterCheck.h"
10+
#include "../utils/Matchers.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/ASTMatchers/ASTMatchers.h"
13+
14+
using namespace clang::ast_matchers;
15+
16+
namespace clang::tidy::bugprone {
17+
18+
void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
19+
Finder->addMatcher(
20+
returnStmt(hasReturnValue(declRefExpr(to(parmVarDecl(hasType(
21+
hasCanonicalType(matchers::isReferenceToConst())))))))
22+
.bind("ret"),
23+
this);
24+
}
25+
26+
void ReturnConstRefFromParameterCheck::check(
27+
const MatchFinder::MatchResult &Result) {
28+
const auto *R = Result.Nodes.getNodeAs<ReturnStmt>("ret");
29+
diag(R->getRetValue()->getBeginLoc(),
30+
"returning a constant reference parameter may cause a use-after-free "
31+
"when the parameter is constructed from a temporary");
32+
}
33+
34+
} // namespace clang::tidy::bugprone
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- ReturnConstRefFromParameterCheck.h - clang-tidy --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RETURNCONSTREFFROMPARAMETERCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RETURNCONSTREFFROMPARAMETERCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::bugprone {
15+
16+
/// Detects return statements that return a constant reference parameter as
17+
/// constant reference. This may cause use-after-free errors if the caller uses
18+
/// xvalues as arguments.
19+
///
20+
/// For the user-facing documentation see:
21+
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/return-const-ref-from-parameter.html
22+
class ReturnConstRefFromParameterCheck : public ClangTidyCheck {
23+
public:
24+
ReturnConstRefFromParameterCheck(StringRef Name, ClangTidyContext *Context)
25+
: ClangTidyCheck(Name, Context) {}
26+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
27+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
28+
std::optional<TraversalKind> getCheckTraversalKind() const override {
29+
// Use 'AsIs' to make sure the return type is exactly the same as the
30+
// parameter type.
31+
return TK_AsIs;
32+
}
33+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
34+
return LangOpts.CPlusPlus;
35+
}
36+
};
37+
38+
} // namespace clang::tidy::bugprone
39+
40+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RETURNCONSTREFFROMPARAMETERCHECK_H

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

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
4343
callee(cxxMethodDecl(hasName("find")).bind("find_fun")),
4444
// ... on a class with a starts_with function.
4545
on(hasType(
46-
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))));
46+
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))),
47+
// Bind search expression.
48+
hasArgument(0, expr().bind("search_expr")));
4749

4850
const auto RFindExpr = cxxMemberCallExpr(
4951
// A method call with a second argument of zero...
@@ -52,15 +54,68 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
5254
callee(cxxMethodDecl(hasName("rfind")).bind("find_fun")),
5355
// ... on a class with a starts_with function.
5456
on(hasType(
55-
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))));
57+
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))),
58+
// Bind search expression.
59+
hasArgument(0, expr().bind("search_expr")));
60+
61+
// Match a string literal and an integer or strlen() call matching the length.
62+
const auto HasStringLiteralAndLengthArgs = [](const auto StringArgIndex,
63+
const auto LengthArgIndex) {
64+
return allOf(
65+
hasArgument(StringArgIndex, stringLiteral().bind("string_literal_arg")),
66+
hasArgument(LengthArgIndex,
67+
anyOf(integerLiteral().bind("integer_literal_size_arg"),
68+
callExpr(callee(functionDecl(parameterCountIs(1),
69+
hasName("strlen"))),
70+
hasArgument(0, stringLiteral().bind(
71+
"strlen_arg"))))));
72+
};
73+
74+
// Match a string variable and a call to length() or size().
75+
const auto HasStringVariableAndSizeCallArgs = [](const auto StringArgIndex,
76+
const auto LengthArgIndex) {
77+
return allOf(
78+
hasArgument(StringArgIndex, declRefExpr(hasDeclaration(
79+
decl().bind("string_var_decl")))),
80+
hasArgument(LengthArgIndex,
81+
cxxMemberCallExpr(
82+
callee(cxxMethodDecl(isConst(), parameterCountIs(0),
83+
hasAnyName("size", "length"))),
84+
on(declRefExpr(
85+
to(decl(equalsBoundNode("string_var_decl"))))))));
86+
};
5687

57-
const auto FindOrRFindExpr =
58-
cxxMemberCallExpr(anyOf(FindExpr, RFindExpr)).bind("find_expr");
88+
// Match either one of the two cases above.
89+
const auto HasStringAndLengthArgs =
90+
[HasStringLiteralAndLengthArgs, HasStringVariableAndSizeCallArgs](
91+
const auto StringArgIndex, const auto LengthArgIndex) {
92+
return anyOf(
93+
HasStringLiteralAndLengthArgs(StringArgIndex, LengthArgIndex),
94+
HasStringVariableAndSizeCallArgs(StringArgIndex, LengthArgIndex));
95+
};
96+
97+
const auto CompareExpr = cxxMemberCallExpr(
98+
// A method call with three arguments...
99+
argumentCountIs(3),
100+
// ... where the first argument is zero...
101+
hasArgument(0, ZeroLiteral),
102+
// ... named compare...
103+
callee(cxxMethodDecl(hasName("compare")).bind("find_fun")),
104+
// ... on a class with a starts_with function...
105+
on(hasType(
106+
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))),
107+
// ... where the third argument is some string and the second a length.
108+
HasStringAndLengthArgs(2, 1),
109+
// Bind search expression.
110+
hasArgument(2, expr().bind("search_expr")));
59111

60112
Finder->addMatcher(
61-
// Match [=!]= with a zero on one side and a string.(r?)find on the other.
62-
binaryOperator(hasAnyOperatorName("==", "!="),
63-
hasOperands(FindOrRFindExpr, ZeroLiteral))
113+
// Match [=!]= with a zero on one side and (r?)find|compare on the other.
114+
binaryOperator(
115+
hasAnyOperatorName("==", "!="),
116+
hasOperands(cxxMemberCallExpr(anyOf(FindExpr, RFindExpr, CompareExpr))
117+
.bind("find_expr"),
118+
ZeroLiteral))
64119
.bind("expr"),
65120
this);
66121
}
@@ -69,23 +124,42 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
69124
const auto *ComparisonExpr = Result.Nodes.getNodeAs<BinaryOperator>("expr");
70125
const auto *FindExpr = Result.Nodes.getNodeAs<CXXMemberCallExpr>("find_expr");
71126
const auto *FindFun = Result.Nodes.getNodeAs<CXXMethodDecl>("find_fun");
127+
const auto *SearchExpr = Result.Nodes.getNodeAs<Expr>("search_expr");
72128
const auto *StartsWithFunction =
73129
Result.Nodes.getNodeAs<CXXMethodDecl>("starts_with_fun");
74130

131+
const auto *StringLiteralArg =
132+
Result.Nodes.getNodeAs<StringLiteral>("string_literal_arg");
133+
const auto *IntegerLiteralSizeArg =
134+
Result.Nodes.getNodeAs<IntegerLiteral>("integer_literal_size_arg");
135+
const auto *StrlenArg = Result.Nodes.getNodeAs<StringLiteral>("strlen_arg");
136+
137+
// Filter out compare cases where the length does not match string literal.
138+
if (StringLiteralArg && IntegerLiteralSizeArg &&
139+
StringLiteralArg->getLength() !=
140+
IntegerLiteralSizeArg->getValue().getZExtValue()) {
141+
return;
142+
}
143+
144+
if (StringLiteralArg && StrlenArg &&
145+
StringLiteralArg->getLength() != StrlenArg->getLength()) {
146+
return;
147+
}
148+
75149
if (ComparisonExpr->getBeginLoc().isMacroID()) {
76150
return;
77151
}
78152

79153
const bool Neg = ComparisonExpr->getOpcode() == BO_NE;
80154

81155
auto Diagnostic =
82-
diag(FindExpr->getBeginLoc(), "use %0 instead of %1() %select{==|!=}2 0")
156+
diag(FindExpr->getExprLoc(), "use %0 instead of %1() %select{==|!=}2 0")
83157
<< StartsWithFunction->getName() << FindFun->getName() << Neg;
84158

85-
// Remove possible zero second argument and ' [!=]= 0' suffix.
159+
// Remove possible arguments after search expression and ' [!=]= 0' suffix.
86160
Diagnostic << FixItHint::CreateReplacement(
87161
CharSourceRange::getTokenRange(
88-
Lexer::getLocForEndOfToken(FindExpr->getArg(0)->getEndLoc(), 0,
162+
Lexer::getLocForEndOfToken(SearchExpr->getEndLoc(), 0,
89163
*Result.SourceManager, getLangOpts()),
90164
ComparisonExpr->getEndLoc()),
91165
")");
@@ -94,11 +168,12 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
94168
Diagnostic << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
95169
ComparisonExpr->getBeginLoc(), FindExpr->getBeginLoc()));
96170

97-
// Replace '(r?)find' with 'starts_with'.
171+
// Replace method name by 'starts_with'.
172+
// Remove possible arguments before search expression.
98173
Diagnostic << FixItHint::CreateReplacement(
99-
CharSourceRange::getTokenRange(FindExpr->getExprLoc(),
100-
FindExpr->getExprLoc()),
101-
StartsWithFunction->getName());
174+
CharSourceRange::getCharRange(FindExpr->getExprLoc(),
175+
SearchExpr->getBeginLoc()),
176+
(StartsWithFunction->getName() + "(").str());
102177

103178
// Add possible negation '!'.
104179
if (Neg) {

clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
namespace clang::tidy::modernize {
1515

16-
/// Checks whether a ``find`` or ``rfind`` result is compared with 0 and
17-
/// suggests replacing with ``starts_with`` when the method exists in the class.
18-
/// Notably, this will work with ``std::string`` and ``std::string_view``.
16+
/// Checks for common roundabout ways to express ``starts_with`` and
17+
/// ``ends_with`` and suggests replacing with ``starts_with`` when the method is
18+
/// available. Notably, this will work with ``std::string`` and
19+
/// ``std::string_view``.
1920
///
2021
/// For the user-facing documentation see:
2122
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-starts-ends-with.html

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ Improvements to clang-tidy
102102
similar fashion to what `-header-filter` does for header files.
103103
- Improved :program:`check_clang_tidy.py` script. Added argument `-export-fixes`
104104
to aid in clang-tidy and test development.
105+
- Fixed bug where big values for unsigned check options overflowed into negative values
106+
when being printed with ``--dump-config``.
105107

106108
- Fixed ``--verify-config`` option not properly parsing checks when using the
107109
literal operator in the ``.clang-tidy`` config.
@@ -115,6 +117,13 @@ New checks
115117
Detects error-prone Curiously Recurring Template Pattern usage, when the CRTP
116118
can be constructed outside itself and the derived class.
117119

120+
- New :doc:`bugprone-return-const-ref-from-parameter
121+
<clang-tidy/checks/bugprone/return-const-ref-from-parameter>` check.
122+
123+
Detects return statements that return a constant reference parameter as constant
124+
reference. This may cause use-after-free errors if the caller uses xvalues as
125+
arguments.
126+
118127
- New :doc:`bugprone-suspicious-stringview-data-usage
119128
<clang-tidy/checks/bugprone/suspicious-stringview-data-usage>` check.
120129

@@ -264,6 +273,10 @@ Changes in existing checks
264273
<clang-tidy/checks/modernize/use-override>` check to also remove any trailing
265274
whitespace when deleting the ``virtual`` keyword.
266275

276+
- Improved :doc:`modernize-use-starts-ends-with
277+
<clang-tidy/checks/modernize/use-starts-ends-with>` check to also handle
278+
calls to ``compare`` method.
279+
267280
- Improved :doc:`modernize-use-using <clang-tidy/checks/modernize/use-using>`
268281
check by adding support for detection of typedefs declared on function level.
269282

0 commit comments

Comments
 (0)