Skip to content

Commit 34eea60

Browse files
committed
[SourceKit/CursorInfo] Add paths for external files to result
.swiftsourceinfo files contain the serialized location for declarations. Use this when outputting locations in cursor info so that clients need not perform an extra index lookup for external modules. Actual offset/line/column will be added in a later patch as we only want to include those if the loaded file is up-to-date with respect to the file on disk. Resolves rdar://75582627 when optimize_for_ide is 0 (ie. .swiftsourceinfo files are read).
1 parent df3350c commit 34eea60

File tree

9 files changed

+32
-38
lines changed

9 files changed

+32
-38
lines changed

lib/IDE/CommentConversion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ visitDocComment(const DocComment *DC, TypeOrExtensionDecl SynthesizedTarget) {
315315

316316
{
317317
// Print line and column number.
318-
auto Loc = D->getLoc();
318+
auto Loc = D->getLoc(/*SerializedOK=*/true);
319319
if (Loc.isValid()) {
320320
const auto &SM = D->getASTContext().SourceMgr;
321321
StringRef FileName = SM.getDisplayNameForLoc(Loc);

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -836,15 +836,17 @@ static unsigned getCharLength(SourceManager &SM, SourceRange TokenRange) {
836836
return SM.getByteDistance(TokenRange.Start, CharEndLoc);
837837
}
838838

839-
void swift::ide::getLocationInfo(const ValueDecl *VD,
840-
llvm::Optional<std::pair<unsigned, unsigned>> &DeclarationLoc,
841-
StringRef &Filename) {
839+
void swift::ide::getLocationInfo(
840+
const ValueDecl *VD,
841+
llvm::Optional<std::pair<unsigned, unsigned>> &DeclarationLoc,
842+
StringRef &Filename) {
842843
ASTContext &Ctx = VD->getASTContext();
843844
SourceManager &SM = Ctx.SourceMgr;
844845

845846
auto ClangNode = VD->getClangNode();
846847

847-
if (VD->getLoc().isValid()) {
848+
SourceLoc Loc = VD->getLoc(/*SerializedOK=*/false);
849+
if (Loc.isValid()) {
848850
auto getSignatureRange = [&](const ValueDecl *VD) -> Optional<unsigned> {
849851
if (auto FD = dyn_cast<AbstractFunctionDecl>(VD)) {
850852
SourceRange R = FD->getSignatureSourceRange();
@@ -859,14 +861,18 @@ void swift::ide::getLocationInfo(const ValueDecl *VD,
859861
} else if (VD->hasName()) {
860862
NameLen = VD->getBaseName().userFacingName().size();
861863
} else {
862-
NameLen = getCharLength(SM, VD->getLoc());
864+
NameLen = getCharLength(SM, Loc);
863865
}
864866

865-
unsigned DeclBufID = SM.findBufferContainingLoc(VD->getLoc());
866-
DeclarationLoc = { SM.getLocOffsetInBuffer(VD->getLoc(), DeclBufID),
867+
unsigned DeclBufID = SM.findBufferContainingLoc(Loc);
868+
DeclarationLoc = { SM.getLocOffsetInBuffer(Loc, DeclBufID),
867869
NameLen };
868870
Filename = SM.getIdentifierForBuffer(DeclBufID);
869871

872+
} else if (auto *Positions =
873+
VD->getSerializedLocs(/*Resolve=*/false).Positions) {
874+
Filename = Positions->SourceFilePath;
875+
// TODO: Add location if it is up-to-date
870876
} else if (ClangNode) {
871877
ClangImporter *Importer =
872878
static_cast<ClangImporter*>(Ctx.getClangModuleLoader());

test/SourceKit/CursorInfo/cursor_info.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ func checkAnyIsAKeyword(x: Any) {}
283283
// CHECK5-NEXT: source.lang.swift
284284
// CHECK5-NEXT: (Int) -> (){{$}}
285285

286-
// RUN: %sourcekitd-test -req=cursor -pos=9:32 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK6 %s
287-
// CHECK6: source.lang.swift.ref.function.free ()
286+
// RUN: %sourcekitd-test -req=global-config -req-opts=optimize_for_ide=0 == -req=cursor -pos=9:32 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK6 %s
287+
// CHECK6: source.lang.swift.ref.function.free ({{.*}}/FooSwiftModule.swift)
288288
// CHECK6-NEXT: fooSwiftFunc
289289
// CHECK6-NEXT: s:14FooSwiftModule03fooB4FuncSiyF
290290
// CHECK6-NEXT: source.lang.swift
@@ -293,7 +293,7 @@ func checkAnyIsAKeyword(x: Any) {}
293293
// CHECK6-NEXT: FooSwiftModule
294294
// CHECK6-NEXT: <Declaration>func fooSwiftFunc() -&gt; <Type usr="s:Si">Int</Type></Declaration>
295295
// CHECK6-NEXT: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>fooSwiftFunc</decl.name>() -&gt; <decl.function.returntype><ref.struct usr="s:Si">Int</ref.struct></decl.function.returntype></decl.function.free>
296-
// CHECK6-NEXT: {{^}}<Function><Name>fooSwiftFunc()</Name><USR>s:14FooSwiftModule03fooB4FuncSiyF</USR><Declaration>func fooSwiftFunc() -&gt; Int</Declaration><CommentParts><Abstract><Para>This is ‘fooSwiftFunc’ from ‘FooSwiftModule’.</Para></Abstract></CommentParts></Function>{{$}}
296+
// CHECK6-NEXT: {{^}}<Function file="{{.*}}/FooSwiftModule.swift" line="2" column="13"><Name>fooSwiftFunc()</Name><USR>s:14FooSwiftModule03fooB4FuncSiyF</USR><Declaration>func fooSwiftFunc() -&gt; Int</Declaration><CommentParts><Abstract><Para>This is ‘fooSwiftFunc’ from ‘FooSwiftModule’.</Para></Abstract></CommentParts></Function>{{$}}
297297

298298
// RUN: %sourcekitd-test -req=cursor -pos=14:10 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK7 %s
299299
// CHECK7: source.lang.swift.ref.struct (13:8-13:10)

test/SourceKit/CursorInfo/cursor_symbol_graph_referenced.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension Parent {
1919

2020
// RUN: %empty-directory(%t)
2121
// RUN: echo 'public struct FromSomeModule {} ' > %t/SomeModule.swift
22-
// RUN: %target-build-swift %t/SomeModule.swift -module-name SomeModule -emit-module -emit-module-path %t/
22+
// RUN: %target-build-swift %t/SomeModule.swift -emit-module -module-name SomeModule -o %t/SomeModule.swiftmodule
2323

2424
// References should cover symbols from the symbol graph declaration fragments, even if not present in the original source.
2525
// RUN: %sourcekitd-test -req=cursor -pos=3:5 -req-opts=retrieve_symbol_graph=1 %s -- %s -target %target-triple -I %t | %FileCheck -check-prefixes=GLOBAL %s
@@ -133,7 +133,7 @@ extension Parent {
133133

134134

135135
// References to unsupported types (like generic parameters) should be ignored.
136-
// RUN: %sourcekitd-test -req=cursor -pos=11:14 -req-opts=retrieve_symbol_graph=1 %s -- %s -target %target-triple -I %t | %FileCheck -check-prefixes=NESTED %s
136+
// RUN: %sourcekitd-test -req=global-config -req-opts=optimize_for_ide=0 == -req=cursor -pos=11:14 -req-opts=retrieve_symbol_graph=1 %s -- %s -target %target-triple -I %t | %FileCheck -check-prefixes=NESTED %s
137137
//
138138
// NESTED: SYMBOL GRAPH BEGIN
139139
// NESTED: "declarationFragments": [
@@ -225,8 +225,7 @@ extension Parent {
225225
// NESTED-NEXT: [[ExtInner_USR]] | internal | {{.*}}cursor_symbol_graph_referenced.swift | cursor_symbol_graph_referenced | User | NonSPI | source.lang.swift
226226
// NESTED-NEXT: Parent swift.struct s:30cursor_symbol_graph_referenced6ParentV
227227
// NESTED-NEXT: ExtInner swift.struct [[ExtInner_USR]]
228-
// FIXME: We should get the file path via the swiftsourceinfo file for user modules (rdar://75582627)
229-
// NESTED-NEXT: [[FromSomeModule_USR]] | public | <empty> | SomeModule | User | NonSPI | source.lang.swift
228+
// NESTED-NEXT: [[FromSomeModule_USR]] | public | {{.*}}/SomeModule.swift | SomeModule | User | NonSPI | source.lang.swift
230229
// NESTED-NEXT: FromSomeModule swift.struct [[FromSomeModule_USR]]
231230
// NESTED-NEXT: REFERENCED DECLS END
232231

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ struct CursorSymbolInfo {
444444
/// Stores the Symbol Graph title, kind, and USR of the parent contexts of the
445445
/// symbol under the cursor.
446446
ArrayRef<ParentInfo> ParentContexts;
447-
/// The set of decls referenced in the symbol graph delcaration fragments.
447+
/// The set of decls referenced in the symbol graph declaration fragments.
448448
ArrayRef<ReferencedDeclInfo> ReferencedSymbols;
449449
/// For calls this lists the USRs of the receiver types (multiple only in the
450450
/// case that the base is a protocol composition).

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,11 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
899899

900900
ASTContext &Ctx = FI.VD->getASTContext();
901901
StringRef Filename = "";
902-
if (auto Loc = FI.VD->getLoc(/*SerializedOK=*/true)) {
902+
if (auto Loc = FI.VD->getLoc(/*SerializedOK=*/false)) {
903903
Filename = Ctx.SourceMgr.getDisplayNameForLoc(Loc);
904+
} else if (auto *Positions =
905+
FI.VD->getSerializedLocs(/*Resolve=*/false).Positions) {
906+
Filename = Positions->SourceFilePath;
904907
} else if (auto ClangNode = FI.VD->getClangNode()) {
905908
auto Loc = ClangNode.getLocation();
906909
if (Loc.isValid()) {

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,16 +1728,18 @@ struct ResponseSymbolInfo {
17281728
}
17291729

17301730
OS << Kind << " (";
1731+
if (CurrentFilename != StringRef(FilePath))
1732+
OS << FilePath;
17311733
if (Offset.hasValue()) {
17321734
if (CurrentFilename != StringRef(FilePath))
1733-
OS << FilePath << ":";
1735+
OS << ':';
17341736
auto LineCol = resolveToLineCol(Offset.getValue(), FilePath, VFSFiles);
17351737
OS << LineCol.first << ':' << LineCol.second;
17361738
auto EndLineCol =
17371739
resolveToLineCol(Offset.getValue() + Length, FilePath, VFSFiles);
17381740
OS << '-' << EndLineCol.first << ':' << EndLineCol.second;
17391741
}
1740-
OS << ")" << '\n';
1742+
OS << ")\n";
17411743

17421744
OS << Name << '\n';
17431745
if (USR)

tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,11 +1764,11 @@ static void addCursorSymbolInfo(const CursorSymbolInfo &Symbol,
17641764
Elem.set(KeyModuleName, Symbol.ModuleName);
17651765
if (!Symbol.ModuleInterfaceName.empty())
17661766
Elem.set(KeyModuleInterfaceName, Symbol.ModuleInterfaceName);
1767+
if (!Symbol.Filename.empty())
1768+
Elem.set(KeyFilePath, Symbol.Filename);
17671769
if (Symbol.DeclarationLoc.hasValue()) {
17681770
Elem.set(KeyOffset, Symbol.DeclarationLoc.getValue().first);
17691771
Elem.set(KeyLength, Symbol.DeclarationLoc.getValue().second);
1770-
if (!Symbol.Filename.empty())
1771-
Elem.set(KeyFilePath, Symbol.Filename);
17721772
}
17731773

17741774
if (!Symbol.OverrideUSRs.empty()) {

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3047,30 +3047,16 @@ class ASTCommentPrinter : public ASTWalker {
30473047
}
30483048
}
30493049

3050-
void printSerializedLoc(Decl *D) {
3051-
auto moduleLoc = cast<FileUnit>(D->getDeclContext()->getModuleScopeContext())->
3052-
getBasicLocsForDecl(D);
3053-
if (!moduleLoc.hasValue())
3054-
return;
3055-
if (!moduleLoc->Loc.isValid())
3056-
return;
3057-
OS << moduleLoc->SourceFilePath
3058-
<< ":" << moduleLoc->Loc.Line
3059-
<< ":" << moduleLoc->Loc.Column << ": ";
3060-
}
3061-
30623050
bool walkToDeclPre(Decl *D) override {
30633051
if (D->isImplicit())
30643052
return true;
30653053

30663054
if (auto *VD = dyn_cast<ValueDecl>(D)) {
3067-
SourceLoc Loc = D->getLoc();
3055+
SourceLoc Loc = D->getLoc(/*SerializedOK=*/true);
30683056
if (Loc.isValid()) {
30693057
auto LineAndColumn = SM.getPresumedLineAndColumnForLoc(Loc);
30703058
OS << SM.getDisplayNameForLoc(Loc)
30713059
<< ":" << LineAndColumn.first << ":" << LineAndColumn.second << ": ";
3072-
} else {
3073-
printSerializedLoc(D);
30743060
}
30753061
OS << Decl::getKindName(VD->getKind()) << "/";
30763062
printDeclName(VD);
@@ -3088,8 +3074,6 @@ class ASTCommentPrinter : public ASTWalker {
30883074
auto LineAndColumn = SM.getPresumedLineAndColumnForLoc(Loc);
30893075
OS << SM.getDisplayNameForLoc(Loc)
30903076
<< ":" << LineAndColumn.first << ":" << LineAndColumn.second << ": ";
3091-
} else {
3092-
printSerializedLoc(D);
30933077
}
30943078
OS << Decl::getKindName(D->getKind()) << "/";
30953079
OS << " ";

0 commit comments

Comments
 (0)