Skip to content

Commit f0e500a

Browse files
authored
merge main into amd-staging (llvm#1305)
2 parents 809c9fa + 9f0c9f8 commit f0e500a

File tree

66 files changed

+613
-277
lines changed

Some content is hidden

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

66 files changed

+613
-277
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,33 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
113113
const auto OnClassWithEndsWithFunction = ClassTypeWithMethod(
114114
"ends_with_fun", "ends_with", "endsWith", "endswith", "EndsWith");
115115

116-
// Case 1: X.find(Y) [!=]= 0 -> starts_with.
116+
// Case 1: X.find(Y, [0], [LEN(Y)]) [!=]= 0 -> starts_with.
117117
const auto FindExpr = cxxMemberCallExpr(
118-
anyOf(argumentCountIs(1), hasArgument(1, ZeroLiteral)),
119118
callee(
120119
cxxMethodDecl(hasName("find"), ofClass(OnClassWithStartsWithFunction))
121120
.bind("find_fun")),
122-
hasArgument(0, expr().bind("needle")));
123-
124-
// Case 2: X.rfind(Y, 0) [!=]= 0 -> starts_with.
121+
hasArgument(0, expr().bind("needle")),
122+
anyOf(
123+
// Detect the expression: X.find(Y);
124+
argumentCountIs(1),
125+
// Detect the expression: X.find(Y, 0);
126+
allOf(argumentCountIs(2), hasArgument(1, ZeroLiteral)),
127+
// Detect the expression: X.find(Y, 0, LEN(Y));
128+
allOf(argumentCountIs(3), hasArgument(1, ZeroLiteral),
129+
hasArgument(2, lengthExprForStringNode("needle")))));
130+
131+
// Case 2: X.rfind(Y, 0, [LEN(Y)]) [!=]= 0 -> starts_with.
125132
const auto RFindExpr = cxxMemberCallExpr(
126-
hasArgument(1, ZeroLiteral),
127133
callee(cxxMethodDecl(hasName("rfind"),
128134
ofClass(OnClassWithStartsWithFunction))
129135
.bind("find_fun")),
130-
hasArgument(0, expr().bind("needle")));
136+
hasArgument(0, expr().bind("needle")),
137+
anyOf(
138+
// Detect the expression: X.rfind(Y, 0);
139+
allOf(argumentCountIs(2), hasArgument(1, ZeroLiteral)),
140+
// Detect the expression: X.rfind(Y, 0, LEN(Y));
141+
allOf(argumentCountIs(3), hasArgument(1, ZeroLiteral),
142+
hasArgument(2, lengthExprForStringNode("needle")))));
131143

132144
// Case 3: X.compare(0, LEN(Y), Y) [!=]= 0 -> starts_with.
133145
const auto CompareExpr = cxxMemberCallExpr(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ Changes in existing checks
175175
<clang-tidy/checks/modernize/use-ranges>` check by updating suppress
176176
warnings logic for ``nullptr`` in ``std::find``.
177177

178+
- Improved :doc:`modernize-use-starts-ends-with
179+
<clang-tidy/checks/modernize/use-starts-ends-with>` check by adding more
180+
matched scenarios of ``find`` and ``rfind`` methods and fixing false
181+
positives when those methods were called with 3 arguments.
182+
178183
- Improved :doc:`modernize-use-std-numbers
179184
<clang-tidy/checks/modernize/use-std-numbers>` check to support math
180185
functions of different precisions.

clang-tools-extra/docs/clang-tidy/checks/modernize/use-starts-ends-with.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Covered scenarios:
1313
Expression Replacement
1414
---------------------------------------------------- ---------------------
1515
``u.find(v) == 0`` ``u.starts_with(v)``
16+
``u.find(v, 0) == 0`` ``u.starts_with(v)``
17+
``u.find(v, 0, v.size()) == 0`` ``u.starts_with(v)``
1618
``u.rfind(v, 0) != 0`` ``!u.starts_with(v)``
19+
``u.rfind(v, 0, v.size()) != 0`` ``!u.starts_with(v)``
1720
``u.compare(0, v.size(), v) == 0`` ``u.starts_with(v)``
1821
``u.substr(0, v.size()) == v`` ``u.starts_with(v)``
1922
``v != u.substr(0, v.size())`` ``!u.starts_with(v)``

clang-tools-extra/test/clang-tidy/checkers/modernize/use-starts-ends-with.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,42 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
236236
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use ends_with
237237
// CHECK-FIXES: s.ends_with(suffix);
238238

239+
s.find("a", 0) == 0;
240+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
241+
// CHECK-FIXES: s.starts_with("a");
242+
243+
s.find(s, ZERO) == 0;
244+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
245+
// CHECK-FIXES: s.starts_with(s);
246+
247+
s.find(s, 0) == ZERO;
248+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
249+
// CHECK-FIXES: s.starts_with(s);
250+
251+
s.find("aaa", 0, 3) == 0;
252+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
253+
// CHECK-FIXES: s.starts_with("aaa");
254+
255+
s.find("aaa", ZERO, 3) == 0;
256+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
257+
// CHECK-FIXES: s.starts_with("aaa");
258+
259+
s.find("aaa", ZERO, strlen(("aaa"))) == ZERO;
260+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
261+
// CHECK-FIXES: s.starts_with("aaa");
262+
263+
s.rfind("aaa", 0, 3) != 0;
264+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
265+
// CHECK-FIXES: !s.starts_with("aaa");
266+
267+
s.rfind("aaa", ZERO, 3) != 0;
268+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
269+
// CHECK-FIXES: !s.starts_with("aaa");
270+
271+
s.rfind("aaa", ZERO, strlen(("aaa"))) == ZERO;
272+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
273+
// CHECK-FIXES: s.starts_with("aaa");
274+
239275
struct S {
240276
std::string s;
241277
} t;
@@ -261,6 +297,12 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
261297

262298
#define STRING s
263299
if (0 == STRING.find("ala")) { /* do something */}
300+
301+
// Cases when literal-size and size parameters are different are not being matched.
302+
s.find("aaa", 0, 2) == 0;
303+
s.find("aaa", 0, strlen("aa")) == 0;
304+
s.rfind("aaa", 0, 2) == 0;
305+
s.rfind("aaa", 0, strlen("aa")) == 0;
264306
}
265307

266308
void test_substr() {

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,9 @@ DEF_TRAVERSE_TYPE(RValueReferenceType,
10051005

10061006
DEF_TRAVERSE_TYPE(MemberPointerType, {
10071007
TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
1008-
TRY_TO(TraverseDecl(T->getMostRecentCXXRecordDecl()));
1008+
if (T->isSugared())
1009+
TRY_TO(TraverseType(
1010+
QualType(T->getMostRecentCXXRecordDecl()->getTypeForDecl(), 0)));
10091011
TRY_TO(TraverseType(T->getPointeeType()));
10101012
})
10111013

clang/include/clang/Sema/Sema.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12620,6 +12620,10 @@ class Sema final : public SemaBase {
1262012620
void MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
1262112621
bool OnlyDeduced, unsigned Depth,
1262212622
llvm::SmallBitVector &Used);
12623+
12624+
void MarkUsedTemplateParameters(ArrayRef<TemplateArgument> TemplateArgs,
12625+
unsigned Depth, llvm::SmallBitVector &Used);
12626+
1262312627
void
1262412628
MarkDeducedTemplateParameters(const FunctionTemplateDecl *FunctionTemplate,
1262512629
llvm::SmallBitVector &Deduced) {

clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ class ExplodedNodeSet {
485485
if (empty())
486486
Impl = S.Impl;
487487
else
488-
Impl.insert(S.begin(), S.end());
488+
Impl.insert_range(S);
489489
}
490490

491491
iterator begin() { return Impl.begin(); }

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,7 +3356,8 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
33563356
if (E->isArray())
33573357
Desc = nullptr; // We're not going to use it in this case.
33583358
else
3359-
Desc = P.createDescriptor(E, *ElemT, Descriptor::InlineDescMD,
3359+
Desc = P.createDescriptor(E, *ElemT, /*SourceTy=*/nullptr,
3360+
Descriptor::InlineDescMD,
33603361
/*IsConst=*/false, /*IsTemporary=*/false,
33613362
/*IsMutable=*/false);
33623363
} else {
@@ -4260,8 +4261,8 @@ unsigned Compiler<Emitter>::allocateLocalPrimitive(
42604261
// FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
42614262
// (int){12} in C. Consider using Expr::isTemporaryObject() instead
42624263
// or isa<MaterializeTemporaryExpr>().
4263-
Descriptor *D = P.createDescriptor(Src, Ty, Descriptor::InlineDescMD, IsConst,
4264-
isa<const Expr *>(Src));
4264+
Descriptor *D = P.createDescriptor(Src, Ty, nullptr, Descriptor::InlineDescMD,
4265+
IsConst, isa<const Expr *>(Src));
42654266
Scope::Local Local = this->createLocal(D);
42664267
if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
42674268
Locals.insert({VD, Local});

clang/lib/AST/ByteCode/Descriptor.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,10 @@ static BlockMoveFn getMoveArrayPrim(PrimType Type) {
329329
}
330330

331331
/// Primitives.
332-
Descriptor::Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD,
333-
bool IsConst, bool IsTemporary, bool IsMutable)
334-
: Source(D), ElemSize(primSize(Type)), Size(ElemSize),
332+
Descriptor::Descriptor(const DeclTy &D, const Type *SourceTy, PrimType Type,
333+
MetadataSize MD, bool IsConst, bool IsTemporary,
334+
bool IsMutable)
335+
: Source(D), SourceType(SourceTy), ElemSize(primSize(Type)), Size(ElemSize),
335336
MDSize(MD.value_or(0)), AllocSize(align(Size + MDSize)), PrimT(Type),
336337
IsConst(IsConst), IsMutable(IsMutable), IsTemporary(IsTemporary),
337338
CtorFn(getCtorPrim(Type)), DtorFn(getDtorPrim(Type)),

clang/lib/AST/ByteCode/Descriptor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ struct Descriptor final {
175175
const BlockMoveFn MoveFn = nullptr;
176176

177177
/// Allocates a descriptor for a primitive.
178-
Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, bool IsConst,
179-
bool IsTemporary, bool IsMutable);
178+
Descriptor(const DeclTy &D, const Type *SourceTy, PrimType Type,
179+
MetadataSize MD, bool IsConst, bool IsTemporary, bool IsMutable);
180180

181181
/// Allocates a descriptor for an array of primitives.
182182
Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,

0 commit comments

Comments
 (0)