Skip to content

Commit 5b767a9

Browse files
author
iclsrc
committed
Merge from 'master' to 'sycl-web' (#6)
CONFLICT (content): Merge conflict in clang/include/clang/Driver/Driver.h
2 parents 0b4eed2 + bb26838 commit 5b767a9

File tree

425 files changed

+12313
-7466
lines changed

Some content is hidden

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

425 files changed

+12313
-7466
lines changed

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

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ namespace modernize {
3535
namespace {
3636

3737
enum BindArgumentKind { BK_Temporary, BK_Placeholder, BK_CallExpr, BK_Other };
38-
enum CaptureMode { CM_None, CM_ByRef, CM_ByValue, CM_InitExpression };
38+
enum CaptureMode { CM_None, CM_ByRef, CM_ByValue };
39+
enum CaptureExpr { CE_None, CE_Var, CE_InitExpression };
3940

4041
enum CallableType {
4142
CT_Other, // unknown
@@ -60,6 +61,10 @@ struct BindArgument {
6061
// captured.
6162
CaptureMode CM = CM_None;
6263

64+
// Whether the argument is a simple variable (we can capture it directly),
65+
// or an expression (we must introduce a capture variable).
66+
CaptureExpr CE = CE_None;
67+
6368
// The exact spelling of this argument in the source code.
6469
StringRef SourceTokens;
6570

@@ -86,6 +91,7 @@ struct CallableInfo {
8691
CallableType Type = CT_Other;
8792
CallableMaterializationKind Materialization = CMK_Other;
8893
CaptureMode CM = CM_None;
94+
CaptureExpr CE = CE_None;
8995
StringRef SourceTokens;
9096
std::string CaptureIdentifier;
9197
std::string UsageIdentifier;
@@ -102,6 +108,12 @@ struct LambdaProperties {
102108

103109
} // end namespace
104110

111+
static bool tryCaptureAsLocalVariable(const MatchFinder::MatchResult &Result,
112+
BindArgument &B, const Expr *E);
113+
114+
static bool tryCaptureAsMemberVariable(const MatchFinder::MatchResult &Result,
115+
BindArgument &B, const Expr *E);
116+
105117
static const Expr *ignoreTemporariesAndPointers(const Expr *E) {
106118
if (const auto *T = dyn_cast<UnaryOperator>(E))
107119
return ignoreTemporariesAndPointers(T->getSubExpr());
@@ -148,12 +160,22 @@ initializeBindArgumentForCallExpr(const MatchFinder::MatchResult &Result,
148160
// std::ref(x) means to capture x by reference.
149161
if (isCallExprNamed(CE, "boost::ref") || isCallExprNamed(CE, "std::ref")) {
150162
B.Kind = BK_Other;
163+
if (tryCaptureAsLocalVariable(Result, B, CE->getArg(0)) ||
164+
tryCaptureAsMemberVariable(Result, B, CE->getArg(0))) {
165+
B.CE = CE_Var;
166+
} else {
167+
// The argument to std::ref is an expression that produces a reference.
168+
// Create a capture reference to hold it.
169+
B.CE = CE_InitExpression;
170+
B.UsageIdentifier = "capture" + llvm::utostr(CaptureIndex++);
171+
}
172+
// Strip off the reference wrapper.
173+
B.SourceTokens = getSourceTextForExpr(Result, CE->getArg(0));
151174
B.CM = CM_ByRef;
152-
B.UsageIdentifier =
153-
std::string(getSourceTextForExpr(Result, CE->getArg(0)));
154175
} else {
155176
B.Kind = BK_CallExpr;
156-
B.CM = CM_InitExpression;
177+
B.CM = CM_ByValue;
178+
B.CE = CE_InitExpression;
157179
B.UsageIdentifier = "capture" + llvm::utostr(CaptureIndex++);
158180
}
159181
B.CaptureIdentifier = B.UsageIdentifier;
@@ -204,6 +226,7 @@ static bool tryCaptureAsMemberVariable(const MatchFinder::MatchResult &Result,
204226

205227
E = E->IgnoreImplicit();
206228
if (isa<CXXThisExpr>(E)) {
229+
// E is a direct use of "this".
207230
B.CM = CM_ByValue;
208231
B.UsageIdentifier = std::string(getSourceTextForExpr(Result, E));
209232
B.CaptureIdentifier = "this";
@@ -217,10 +240,15 @@ static bool tryCaptureAsMemberVariable(const MatchFinder::MatchResult &Result,
217240
if (!ME->isLValue() || !isa<FieldDecl>(ME->getMemberDecl()))
218241
return false;
219242

220-
B.CM = CM_ByValue;
221-
B.UsageIdentifier = std::string(getSourceTextForExpr(Result, E));
222-
B.CaptureIdentifier = "this";
223-
return true;
243+
if (isa<CXXThisExpr>(ME->getBase())) {
244+
// E refers to a data member without an explicit "this".
245+
B.CM = CM_ByValue;
246+
B.UsageIdentifier = std::string(getSourceTextForExpr(Result, E));
247+
B.CaptureIdentifier = "this";
248+
return true;
249+
}
250+
251+
return false;
224252
}
225253

226254
static SmallVector<BindArgument, 4>
@@ -251,7 +279,10 @@ buildBindArguments(const MatchFinder::MatchResult &Result,
251279
B.IsUsed = true;
252280

253281
SmallVector<StringRef, 2> Matches;
254-
if (MatchPlaceholder.match(B.SourceTokens, &Matches)) {
282+
const auto *DRE = dyn_cast<DeclRefExpr>(E);
283+
if (MatchPlaceholder.match(B.SourceTokens, &Matches) ||
284+
// Check for match with qualifiers removed.
285+
(DRE && MatchPlaceholder.match(DRE->getDecl()->getName(), &Matches))) {
255286
B.Kind = BK_Placeholder;
256287
B.PlaceHolderIndex = std::stoi(std::string(Matches[1]));
257288
B.UsageIdentifier = "PH" + llvm::utostr(B.PlaceHolderIndex);
@@ -273,11 +304,13 @@ buildBindArguments(const MatchFinder::MatchResult &Result,
273304
// safe.
274305
B.Kind = BK_Other;
275306
if (IsObjectPtr) {
276-
B.CM = CM_InitExpression;
307+
B.CE = CE_InitExpression;
308+
B.CM = CM_ByValue;
277309
B.UsageIdentifier = "ObjectPtr";
278310
B.CaptureIdentifier = B.UsageIdentifier;
279311
} else if (anyDescendantIsLocal(B.E)) {
280-
B.CM = CM_InitExpression;
312+
B.CE = CE_InitExpression;
313+
B.CM = CM_ByValue;
281314
B.CaptureIdentifier = "capture" + llvm::utostr(CaptureIndex++);
282315
B.UsageIdentifier = B.CaptureIdentifier;
283316
}
@@ -337,9 +370,12 @@ static void addFunctionCallArgs(ArrayRef<BindArgument> Args,
337370

338371
Stream << Delimiter;
339372

340-
if (B.Kind == BK_Placeholder || B.CM != CM_None)
373+
if (B.Kind == BK_Placeholder) {
374+
Stream << "std::forward<decltype(" << B.UsageIdentifier << ")>";
375+
Stream << "(" << B.UsageIdentifier << ")";
376+
} else if (B.CM != CM_None)
341377
Stream << B.UsageIdentifier;
342-
else if (B.CM == CM_None)
378+
else
343379
Stream << B.SourceTokens;
344380

345381
Delimiter = ", ";
@@ -357,9 +393,9 @@ static bool isPlaceHolderIndexRepeated(const ArrayRef<BindArgument> Args) {
357393
return false;
358394
}
359395

360-
static std::vector<const CXXMethodDecl *>
396+
static std::vector<const FunctionDecl *>
361397
findCandidateCallOperators(const CXXRecordDecl *RecordDecl, size_t NumArgs) {
362-
std::vector<const CXXMethodDecl *> Candidates;
398+
std::vector<const FunctionDecl *> Candidates;
363399

364400
for (const clang::CXXMethodDecl *Method : RecordDecl->methods()) {
365401
OverloadedOperatorKind OOK = Method->getOverloadedOperator();
@@ -373,6 +409,23 @@ findCandidateCallOperators(const CXXRecordDecl *RecordDecl, size_t NumArgs) {
373409
Candidates.push_back(Method);
374410
}
375411

412+
// Find templated operator(), if any.
413+
for (const clang::Decl *D : RecordDecl->decls()) {
414+
const auto *FTD = dyn_cast<FunctionTemplateDecl>(D);
415+
if (!FTD)
416+
continue;
417+
const FunctionDecl *FD = FTD->getTemplatedDecl();
418+
419+
OverloadedOperatorKind OOK = FD->getOverloadedOperator();
420+
if (OOK != OverloadedOperatorKind::OO_Call)
421+
continue;
422+
423+
if (FD->getNumParams() > NumArgs)
424+
continue;
425+
426+
Candidates.push_back(FD);
427+
}
428+
376429
return Candidates;
377430
}
378431

@@ -407,7 +460,7 @@ static bool isFixitSupported(const CallableInfo &Callee,
407460

408461
const FunctionDecl *getCallOperator(const CXXRecordDecl *Callable,
409462
size_t NumArgs) {
410-
std::vector<const CXXMethodDecl *> Candidates =
463+
std::vector<const FunctionDecl *> Candidates =
411464
findCandidateCallOperators(Callable, NumArgs);
412465
if (Candidates.size() != 1)
413466
return nullptr;
@@ -466,11 +519,15 @@ getCallableMaterialization(const MatchFinder::MatchResult &Result) {
466519

467520
const auto *NoTemporaries = ignoreTemporariesAndPointers(CallableExpr);
468521

469-
if (isa<CallExpr>(NoTemporaries))
522+
const auto *CE = dyn_cast<CXXConstructExpr>(NoTemporaries);
523+
const auto *FC = dyn_cast<CXXFunctionalCastExpr>(NoTemporaries);
524+
if ((isa<CallExpr>(NoTemporaries)) || (CE && (CE->getNumArgs() > 0)) ||
525+
(FC && (FC->getCastKind() == CK_ConstructorConversion)))
526+
// CE is something that looks like a call, with arguments - either
527+
// a function call or a constructor invocation.
470528
return CMK_CallExpression;
471529

472-
if (isa<CXXFunctionalCastExpr>(NoTemporaries) ||
473-
isa<CXXConstructExpr>(NoTemporaries))
530+
if (isa<CXXFunctionalCastExpr>(NoTemporaries) || CE)
474531
return CMK_Function;
475532

476533
if (const auto *DRE = dyn_cast<DeclRefExpr>(NoTemporaries)) {
@@ -503,13 +560,15 @@ getLambdaProperties(const MatchFinder::MatchResult &Result) {
503560
getCallMethodDecl(Result, LP.Callable.Type, LP.Callable.Materialization);
504561
LP.Callable.SourceTokens = getSourceTextForExpr(Result, CalleeExpr);
505562
if (LP.Callable.Materialization == CMK_VariableRef) {
563+
LP.Callable.CE = CE_Var;
506564
LP.Callable.CM = CM_ByValue;
507565
LP.Callable.UsageIdentifier =
508566
std::string(getSourceTextForExpr(Result, CalleeExpr));
509567
LP.Callable.CaptureIdentifier = std::string(
510568
getSourceTextForExpr(Result, ignoreTemporariesAndPointers(CalleeExpr)));
511569
} else if (LP.Callable.Materialization == CMK_CallExpression) {
512-
LP.Callable.CM = CM_InitExpression;
570+
LP.Callable.CE = CE_InitExpression;
571+
LP.Callable.CM = CM_ByValue;
513572
LP.Callable.UsageIdentifier = "Func";
514573
LP.Callable.CaptureIdentifier = "Func";
515574
LP.Callable.CaptureInitializer = getSourceTextForExpr(Result, CalleeExpr);
@@ -523,7 +582,7 @@ getLambdaProperties(const MatchFinder::MatchResult &Result) {
523582
}
524583

525584
static bool emitCapture(llvm::StringSet<> &CaptureSet, StringRef Delimiter,
526-
CaptureMode CM, StringRef Identifier,
585+
CaptureMode CM, CaptureExpr CE, StringRef Identifier,
527586
StringRef InitExpression, raw_ostream &Stream) {
528587
if (CM == CM_None)
529588
return false;
@@ -537,7 +596,7 @@ static bool emitCapture(llvm::StringSet<> &CaptureSet, StringRef Delimiter,
537596
if (CM == CM_ByRef)
538597
Stream << "&";
539598
Stream << Identifier;
540-
if (CM == CM_InitExpression)
599+
if (CE == CE_InitExpression)
541600
Stream << " = " << InitExpression;
542601

543602
CaptureSet.insert(Identifier);
@@ -550,17 +609,17 @@ static void emitCaptureList(const LambdaProperties &LP,
550609
llvm::StringSet<> CaptureSet;
551610
bool AnyCapturesEmitted = false;
552611

553-
AnyCapturesEmitted = emitCapture(CaptureSet, "", LP.Callable.CM,
554-
LP.Callable.CaptureIdentifier,
555-
LP.Callable.CaptureInitializer, Stream);
612+
AnyCapturesEmitted = emitCapture(
613+
CaptureSet, "", LP.Callable.CM, LP.Callable.CE,
614+
LP.Callable.CaptureIdentifier, LP.Callable.CaptureInitializer, Stream);
556615

557616
for (const BindArgument &B : LP.BindArguments) {
558617
if (B.CM == CM_None || !B.IsUsed)
559618
continue;
560619

561620
StringRef Delimiter = AnyCapturesEmitted ? ", " : "";
562621

563-
if (emitCapture(CaptureSet, Delimiter, B.CM, B.CaptureIdentifier,
622+
if (emitCapture(CaptureSet, Delimiter, B.CM, B.CE, B.CaptureIdentifier,
564623
B.SourceTokens, Stream))
565624
AnyCapturesEmitted = true;
566625
}
@@ -635,19 +694,18 @@ void AvoidBindCheck::check(const MatchFinder::MatchResult &Result) {
635694
Stream << MethodDecl->getName();
636695
} else {
637696
Stream << " { return ";
638-
switch (LP.Callable.CM) {
639-
case CM_ByValue:
640-
case CM_ByRef:
697+
switch (LP.Callable.CE) {
698+
case CE_Var:
641699
if (LP.Callable.UsageIdentifier != LP.Callable.CaptureIdentifier) {
642700
Stream << "(" << LP.Callable.UsageIdentifier << ")";
643701
break;
644702
}
645703
LLVM_FALLTHROUGH;
646-
case CM_InitExpression:
704+
case CE_InitExpression:
647705
Stream << LP.Callable.UsageIdentifier;
648706
break;
649707
default:
650-
Ref->printPretty(Stream, nullptr, Result.Context->getPrintingPolicy());
708+
Stream << getSourceTextForExpr(Result, Ref);
651709
}
652710
}
653711

clang-tools-extra/clangd/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ add_clang_library(clangDaemon
3636
CollectMacros.cpp
3737
CompileCommands.cpp
3838
Compiler.cpp
39+
ConfigYAML.cpp
3940
Diagnostics.cpp
4041
DraftStore.cpp
4142
ExpectedTypes.cpp

0 commit comments

Comments
 (0)