@@ -2493,125 +2493,137 @@ void SwiftLangSupport::findRelatedIdentifiersInFile(
2493
2493
StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
2494
2494
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
2495
2495
SourceKitCancellationToken CancellationToken,
2496
- std::function<void (const RequestResult<ArrayRef<RelatedIdentInfo>> &)>
2497
- Receiver) {
2496
+ std::function<void (const RequestResult<RelatedIdentsResult> &)> Receiver) {
2498
2497
2499
2498
std::string Error;
2500
2499
SwiftInvocationRef Invok =
2501
2500
ASTMgr->getTypecheckInvocation (Args, PrimaryFilePath, Error);
2502
2501
if (!Invok) {
2503
2502
LOG_WARN_FUNC (" failed to create an ASTInvocation: " << Error);
2504
- Receiver (RequestResult<ArrayRef<RelatedIdentInfo> >::fromError (Error));
2503
+ Receiver (RequestResult<RelatedIdentsResult >::fromError (Error));
2505
2504
return ;
2506
2505
}
2507
2506
2508
2507
class RelatedIdConsumer : public SwiftASTConsumer {
2509
2508
std::string InputFile;
2510
2509
unsigned Offset;
2511
- std::function<void (const RequestResult<ArrayRef<RelatedIdentInfo>> &)>
2512
- Receiver;
2510
+ std::function<void (const RequestResult<RelatedIdentsResult> &)> Receiver;
2513
2511
SwiftInvocationRef Invok;
2514
2512
2513
+ #if SWIFT_BUILD_SWIFT_SYNTAX
2514
+ // FIXME: Don't silently eat errors here.
2515
+ RelatedIdentsResult getRelatedIdents (SourceFile *SrcFile,
2516
+ CompilerInstance &CompInst) {
2517
+ unsigned BufferID = SrcFile->getBufferID ().value ();
2518
+ SourceLoc Loc = Lexer::getLocForStartOfToken (CompInst.getSourceMgr (),
2519
+ BufferID, Offset);
2520
+ if (Loc.isInvalid ())
2521
+ return RelatedIdentsResult::empty ();
2522
+
2523
+ SourceManager &SrcMgr = CompInst.getASTContext ().SourceMgr ;
2524
+
2525
+ ResolvedCursorInfoPtr CursorInfo =
2526
+ evaluateOrDefault (CompInst.getASTContext ().evaluator ,
2527
+ CursorInfoRequest{CursorInfoOwner (SrcFile, Loc)},
2528
+ new ResolvedCursorInfo ());
2529
+ auto ValueRefCursorInfo =
2530
+ dyn_cast<ResolvedValueRefCursorInfo>(CursorInfo);
2531
+ if (!ValueRefCursorInfo)
2532
+ return RelatedIdentsResult::empty ();
2533
+ if (ValueRefCursorInfo->isKeywordArgument ())
2534
+ return RelatedIdentsResult::empty ();
2535
+
2536
+ ValueDecl *VD = ValueRefCursorInfo->typeOrValue ();
2537
+ if (!VD)
2538
+ return RelatedIdentsResult::empty (); // This was a module reference.
2539
+
2540
+ // Only accept pointing to an identifier.
2541
+ if (!ValueRefCursorInfo->isRef () &&
2542
+ (isa<ConstructorDecl>(VD) || isa<DestructorDecl>(VD) ||
2543
+ isa<SubscriptDecl>(VD)))
2544
+ return RelatedIdentsResult::empty ();
2545
+ if (VD->isOperator ())
2546
+ return RelatedIdentsResult::empty ();
2547
+
2548
+ llvm::Optional<RenameInfo> Info = getRenameInfo (CursorInfo);
2549
+
2550
+ if (!Info) {
2551
+ return RelatedIdentsResult::empty ();
2552
+ }
2553
+
2554
+ RenameLocs Locs = localRenameLocs (SrcFile, Info->VD );
2555
+
2556
+ std::string OldName = Locs.getLocations ().front ().OldName .str ();
2557
+ #ifndef NDEBUG
2558
+ for (auto loc : Locs.getLocations ()) {
2559
+ assert (loc.OldName == OldName &&
2560
+ " Found related identfiers with different names?" );
2561
+ }
2562
+ #endif
2563
+
2564
+ // Ignore any errors produced by `resolveRenameLocations` since, if some
2565
+ // symbol failed to resolve, we still want to return all the other
2566
+ // symbols. This makes related idents more fault-tolerant.
2567
+ DiagnosticEngine Diags (SrcMgr);
2568
+
2569
+ std::vector<ResolvedLoc> ResolvedLocs = resolveRenameLocations (
2570
+ Locs.getLocations (), /* NewName=*/ StringRef (), *SrcFile, Diags);
2571
+
2572
+ SmallVector<RelatedIdentInfo, 8 > Ranges;
2573
+ assert (ResolvedLocs.size () == Locs.getLocations ().size ());
2574
+ for (auto [RenameLoc, ResolvedLoc] :
2575
+ llvm::zip_equal (Locs.getLocations (), ResolvedLocs)) {
2576
+ if (ResolvedLoc.range .isInvalid ()) {
2577
+ continue ;
2578
+ }
2579
+ unsigned Offset =
2580
+ SrcMgr.getLocOffsetInBuffer (ResolvedLoc.range .getStart (), BufferID);
2581
+ Ranges.push_back (
2582
+ {Offset, ResolvedLoc.range .getByteLength (), RenameLoc.Usage });
2583
+ }
2584
+
2585
+ return RelatedIdentsResult (Ranges, OldName);
2586
+ }
2587
+ #endif
2588
+
2515
2589
public:
2516
2590
RelatedIdConsumer (
2517
2591
StringRef InputFile, unsigned Offset,
2518
- std::function<void (const RequestResult<ArrayRef<RelatedIdentInfo> > &)>
2592
+ std::function<void (const RequestResult<RelatedIdentsResult > &)>
2519
2593
Receiver,
2520
2594
SwiftInvocationRef Invok)
2521
2595
: InputFile(InputFile.str()), Offset(Offset),
2522
2596
Receiver (std::move(Receiver)), Invok(Invok) {}
2523
2597
2524
- // FIXME: Don't silently eat errors here.
2525
2598
void handlePrimaryAST (ASTUnitRef AstUnit) override {
2526
- using ResultType = RequestResult<ArrayRef<RelatedIdentInfo> >;
2599
+ using ResultType = RequestResult<RelatedIdentsResult >;
2527
2600
#if !SWIFT_BUILD_SWIFT_SYNTAX
2528
- Receiver (
2529
- ResultType::fromError ( " relatedidents is not supported because "
2530
- " sourcekitd was built without swift-syntax" ) );
2601
+ ResultType::fromError (
2602
+ " relatedidents is not supported because sourcekitd was built without "
2603
+ " swift-syntax" );
2531
2604
return ;
2532
2605
#else
2533
2606
auto &CompInst = AstUnit->getCompilerInstance ();
2534
2607
2535
2608
auto *SrcFile = retrieveInputFile (InputFile, CompInst);
2536
2609
if (!SrcFile) {
2537
- Receiver (RequestResult<ArrayRef<RelatedIdentInfo> >::fromError (
2610
+ Receiver (RequestResult<RelatedIdentsResult >::fromError (
2538
2611
" Unable to find input file" ));
2539
2612
return ;
2540
2613
}
2541
2614
2542
- SmallVector<RelatedIdentInfo, 8 > Ranges;
2543
-
2544
- auto Action = [&]() {
2545
- unsigned BufferID = SrcFile->getBufferID ().value ();
2546
- SourceLoc Loc =
2547
- Lexer::getLocForStartOfToken (CompInst.getSourceMgr (), BufferID, Offset);
2548
- if (Loc.isInvalid ())
2549
- return ;
2550
-
2551
- SourceManager &SrcMgr = CompInst.getASTContext ().SourceMgr ;
2552
-
2553
- ResolvedCursorInfoPtr CursorInfo =
2554
- evaluateOrDefault (CompInst.getASTContext ().evaluator ,
2555
- CursorInfoRequest{CursorInfoOwner (SrcFile, Loc)},
2556
- new ResolvedCursorInfo ());
2557
- auto ValueRefCursorInfo =
2558
- dyn_cast<ResolvedValueRefCursorInfo>(CursorInfo);
2559
- if (!ValueRefCursorInfo)
2560
- return ;
2561
- if (ValueRefCursorInfo->isKeywordArgument ())
2562
- return ;
2563
-
2564
- ValueDecl *VD = ValueRefCursorInfo->typeOrValue ();
2565
- if (!VD)
2566
- return ; // This was a module reference.
2567
-
2568
- // Only accept pointing to an identifier.
2569
- if (!ValueRefCursorInfo->isRef () &&
2570
- (isa<ConstructorDecl>(VD) || isa<DestructorDecl>(VD) ||
2571
- isa<SubscriptDecl>(VD)))
2572
- return ;
2573
- if (VD->isOperator ())
2574
- return ;
2575
-
2576
- llvm::Optional<RenameInfo> Info = getRenameInfo (CursorInfo);
2577
-
2578
- if (!Info) {
2579
- return ;
2580
- }
2581
-
2582
- RenameLocs Locs = localRenameLocs (SrcFile, Info->VD );
2583
-
2584
- // Ignore any errors produced by `resolveRenameLocations` since, if some
2585
- // symbol failed to resolve, we still want to return all the other
2586
- // symbols. This makes related idents more fault-tolerant.
2587
- DiagnosticEngine Diags (SrcMgr);
2588
-
2589
- std::vector<ResolvedLoc> ResolvedLocs = resolveRenameLocations (
2590
- Locs.getLocations (), /* NewName=*/ StringRef (), *SrcFile, Diags);
2591
-
2592
- assert (ResolvedLocs.size () == Locs.getLocations ().size ());
2593
- for (auto ResolvedLoc : ResolvedLocs) {
2594
- if (ResolvedLoc.range .isInvalid ()) {
2595
- continue ;
2596
- }
2597
- unsigned Offset = SrcMgr.getLocOffsetInBuffer (
2598
- ResolvedLoc.range .getStart (), BufferID);
2599
- Ranges.push_back (
2600
- {Offset, ResolvedLoc.range .getByteLength (), RenameLoc.Usage });
2601
- }
2602
- };
2603
- Action ();
2604
- Receiver (ResultType::fromResult (Ranges));
2615
+ RelatedIdentsResult Result = getRelatedIdents (SrcFile, CompInst);
2616
+ Receiver (ResultType::fromResult (Result));
2605
2617
#endif
2606
2618
}
2607
2619
2608
2620
void cancelled () override {
2609
- Receiver (RequestResult<ArrayRef<RelatedIdentInfo> >::cancelled ());
2621
+ Receiver (RequestResult<RelatedIdentsResult >::cancelled ());
2610
2622
}
2611
2623
2612
2624
void failed (StringRef Error) override {
2613
2625
LOG_WARN_FUNC (" related idents failed: " << Error);
2614
- Receiver (RequestResult<ArrayRef<RelatedIdentInfo> >::fromError (Error));
2626
+ Receiver (RequestResult<RelatedIdentsResult >::fromError (Error));
2615
2627
}
2616
2628
2617
2629
static CaseStmt *getCaseStmtOfCanonicalVar (Decl *D) {
0 commit comments