Skip to content

Commit dfd0a86

Browse files
committed
[SourceKit] Teach SourceKit to recognized USRs for synthesized extensions.
So that IDE can jump to the synthesized extensions instead of the actual extensions.
1 parent 4b116d0 commit dfd0a86

File tree

15 files changed

+145
-26
lines changed

15 files changed

+145
-26
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
public protocol P1{
3+
associatedtype T1
4+
associatedtype T2
5+
func f1(t : T1) -> T1
6+
func f2(t : T2) -> T2
7+
}
8+
9+
public protocol P2 {
10+
associatedtype P2T1
11+
}
12+
13+
public extension P2 where P2T1 : P2{
14+
public func p2member() {}
15+
}
16+
17+
public protocol P3 {}
18+
19+
public extension P1 where T1 : P2 {
20+
public func ef1(t : T1) {}
21+
public func ef2(t : T2) {}
22+
}
23+
24+
public extension P1 where T1 == P2, T2 : P3 {
25+
public func ef3(t : T1) {}
26+
public func ef4(t : T1) {}
27+
}
28+
29+
public extension P1 where T2 : P3 {
30+
public func ef5(t : T2) {}
31+
}
32+
33+
public struct S2 {}
34+
35+
public struct S1<T> : P1, P2 {
36+
public typealias T1 = T
37+
public typealias T2 = S2
38+
public typealias P2T1 = T
39+
public func f1(t : T1) -> T1 {
40+
return t
41+
}
42+
public func f2(t : T2) -> T2 {
43+
return t
44+
}
45+
}

test/SourceKit/InterfaceGen/gen_swift_module.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import swift_mod_syn
2+
3+
func f(var s : [Int]) {
4+
s.sort()
5+
}
6+
17
// RUN: rm -rf %t.mod
28
// RUN: mkdir %t.mod
39
// RUN: %swift -emit-module -o %t.mod/swift_mod.swiftmodule %S/Inputs/swift_mod.swift -parse-as-library
@@ -7,3 +13,19 @@
713
// RUN: %sourcekitd-test -req=module-groups -module swift_mod -- -I %t.mod | FileCheck -check-prefix=GROUP-EMPTY %s
814
// GROUP-EMPTY: <GROUPS>
915
// GROUP-EMPTY-NEXT: <\GROUPS>
16+
17+
// RUN: %swift -emit-module -o %t.mod/swift_mod_syn.swiftmodule %S/Inputs/swift_mod_syn.swift -parse-as-library
18+
// RUN: %sourcekitd-test -req=interface-gen-open -module swift_mod_syn -- -I %t.mod == -req=cursor -pos=4:7 %s -- %s -I %t.mod | FileCheck -check-prefix=SYNTHESIZED-USR1 %s
19+
// SYNTHESIZED-USR1: s:FesRxs21MutableCollectionTypeWx9Generator7Element_s10ComparablerS_4sortFT_GSaWxS0_S1___::SYNTHESIZED::s:Sa
20+
21+
// RUN: %sourcekitd-test -req=interface-gen-open -module Swift -synthesized-extension \
22+
// RUN: == -req=find-usr -usr "s:FesRxs21MutableCollectionTypeWx9Generator7Element_s10ComparablerS_4sortFT_GSaWxS0_S1___::SYNTHESIZED::s:Sa" | FileCheck -check-prefix=SYNTHESIZED-USR2 %s
23+
// SYNTHESIZED-USR2-NOT: USR NOT FOUND
24+
25+
// RUN: %sourcekitd-test -req=interface-gen-open -module Swift \
26+
// RUN: == -req=find-usr -usr "s:FesRxs21MutableCollectionTypeWx9Generator7Element_s10ComparablerS_4sortFT_GSaWxS0_S1___" | FileCheck -check-prefix=SYNTHESIZED-USR3 %s
27+
// SYNTHESIZED-USR3-NOT: USR NOT FOUND
28+
29+
// RUN: %sourcekitd-test -req=interface-gen-open -module Swift \
30+
// RUN: == -req=find-usr -usr "s:FesRxs21MutableCollectionTypeWx9Generator7Element_s10ComparablerS_4sortFT_GSaWxS0_S1___::SYNTHESIZED::USRDOESNOTEXIST" | FileCheck -check-prefix=SYNTHESIZED-USR4 %s
31+
// SYNTHESIZED-USR4-NOT: USR NOT FOUND

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ class LangSupport {
375375
virtual void anchor();
376376

377377
public:
378+
/// A separator between parts in a synthesized usr.
379+
const static std::string SynthesizedUSRSeparator;
380+
378381
virtual ~LangSupport() { }
379382

380383
virtual void indexSource(StringRef Filename,
@@ -417,12 +420,14 @@ class LangSupport {
417420
StringRef Name,
418421
StringRef ModuleName,
419422
Optional<StringRef> Group,
420-
ArrayRef<const char *> Args) = 0;
423+
ArrayRef<const char *> Args,
424+
bool SynthesizedExtensions) = 0;
421425

422426
virtual void editorOpenHeaderInterface(EditorConsumer &Consumer,
423427
StringRef Name,
424428
StringRef HeaderName,
425-
ArrayRef<const char *> Args) = 0;
429+
ArrayRef<const char *> Args,
430+
bool SynthesizedExtensions) = 0;
426431

427432
virtual void editorOpenSwiftSourceInterface(StringRef Name,
428433
StringRef SourceName,

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ class AnnotatingPrinter : public StreamPrinter {
116116
// synthesize target to the original USR.
117117
std::string TargetUSR;
118118

119-
// A separator between the target usr and the real usr.
120-
std::string Separator = "::SYNTHESIZED::";
121-
122119
public:
123120
AnnotatingPrinter(SourceTextInfo &Info, llvm::raw_ostream &OS)
124121
: StreamPrinter(OS), Info(Info) { }
@@ -160,7 +157,7 @@ class AnnotatingPrinter : public StreamPrinter {
160157

161158
// Append target's USR if this is a member of a synthesized extension.
162159
if (!TargetUSR.empty()) {
163-
OS << Separator;
160+
OS << LangSupport::SynthesizedUSRSeparator;
164161
OS << TargetUSR;
165162
}
166163
StringRef USR = OS.str();
@@ -275,7 +272,8 @@ static bool getModuleInterfaceInfo(ASTContext &Ctx,
275272
StringRef ModuleName,
276273
Optional<StringRef> Group,
277274
SwiftInterfaceGenContext::Implementation &Impl,
278-
std::string &ErrMsg) {
275+
std::string &ErrMsg,
276+
bool SynthesizedExtensions) {
279277
Module *&Mod = Impl.Mod;
280278
SourceTextInfo &Info = Impl.Info;
281279

@@ -318,7 +316,7 @@ static bool getModuleInterfaceInfo(ASTContext &Ctx,
318316
AnnotatingPrinter Printer(Info, OS);
319317
printSubmoduleInterface(Mod, SplitModuleName, Group,
320318
TraversalOptions,
321-
Printer, Options, false);
319+
Printer, Options, SynthesizedExtensions);
322320

323321
Info.Text = OS.str();
324322
return false;
@@ -374,7 +372,8 @@ SwiftInterfaceGenContext::create(StringRef DocumentName,
374372
StringRef ModuleOrHeaderName,
375373
Optional<StringRef> Group,
376374
CompilerInvocation Invocation,
377-
std::string &ErrMsg) {
375+
std::string &ErrMsg,
376+
bool SynthesizedExtensions) {
378377
SwiftInterfaceGenContextRef IFaceGenCtx{ new SwiftInterfaceGenContext() };
379378
IFaceGenCtx->Impl.DocumentName = DocumentName;
380379
IFaceGenCtx->Impl.IsModule = IsModule;
@@ -403,7 +402,7 @@ SwiftInterfaceGenContext::create(StringRef DocumentName,
403402

404403
if (IsModule) {
405404
if (getModuleInterfaceInfo(Ctx, ModuleOrHeaderName, Group, IFaceGenCtx->Impl,
406-
ErrMsg))
405+
ErrMsg, SynthesizedExtensions))
407406
return nullptr;
408407
} else {
409408
auto &FEOpts = Invocation.getFrontendOptions();
@@ -582,7 +581,8 @@ void SwiftLangSupport::editorOpenInterface(EditorConsumer &Consumer,
582581
StringRef Name,
583582
StringRef ModuleName,
584583
Optional<StringRef> Group,
585-
ArrayRef<const char *> Args) {
584+
ArrayRef<const char *> Args,
585+
bool SynthesizedExtensions) {
586586
CompilerInstance CI;
587587
// Display diagnostics to stderr.
588588
PrintingDiagnosticConsumer PrintDiags;
@@ -615,7 +615,8 @@ void SwiftLangSupport::editorOpenInterface(EditorConsumer &Consumer,
615615
ModuleName,
616616
Group,
617617
Invocation,
618-
ErrMsg);
618+
ErrMsg,
619+
SynthesizedExtensions);
619620
if (!IFaceGenRef) {
620621
Consumer.handleRequestError(ErrMsg.c_str());
621622
return;
@@ -683,7 +684,8 @@ void SwiftLangSupport::editorOpenSwiftSourceInterface(StringRef Name,
683684
void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
684685
StringRef Name,
685686
StringRef HeaderName,
686-
ArrayRef<const char *> Args) {
687+
ArrayRef<const char *> Args,
688+
bool SynthesizedExtensions) {
687689
CompilerInstance CI;
688690
// Display diagnostics to stderr.
689691
PrintingDiagnosticConsumer PrintDiags;
@@ -719,7 +721,8 @@ void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
719721
HeaderName,
720722
None,
721723
Invocation,
722-
Error);
724+
Error,
725+
SynthesizedExtensions);
723726
if (!IFaceGenRef) {
724727
Consumer.handleRequestError(Error.c_str());
725728
return;

tools/SourceKit/lib/SwiftLang/SwiftInterfaceGenContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class SwiftInterfaceGenContext :
3838
StringRef ModuleOrHeaderName,
3939
Optional<StringRef> Group,
4040
swift::CompilerInvocation Invocation,
41-
std::string &ErrorMsg);
41+
std::string &ErrorMsg,
42+
bool SynthesizedExtensions);
4243

4344
static SwiftInterfaceGenContextRef createForSwiftSource(StringRef DocumentName,
4445
StringRef SourceFileName,

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ LangSupport::createSwiftLangSupport(SourceKit::Context &SKCtx) {
149149
return std::unique_ptr<LangSupport>(new SwiftLangSupport(SKCtx));
150150
}
151151

152+
const std::string LangSupport::SynthesizedUSRSeparator = "::SYNTHESIZED::";
153+
152154
namespace {
153155

154156
class UIdentVisitor : public ASTVisitor<UIdentVisitor,

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,14 @@ class SwiftLangSupport : public LangSupport {
307307
StringRef Name,
308308
StringRef ModuleName,
309309
Optional<StringRef> Group,
310-
ArrayRef<const char *> Args) override;
310+
ArrayRef<const char *> Args,
311+
bool SynthesizedExtensions) override;
311312

312313
void editorOpenHeaderInterface(EditorConsumer &Consumer,
313314
StringRef Name,
314315
StringRef HeaderName,
315-
ArrayRef<const char *> Args) override;
316+
ArrayRef<const char *> Args,
317+
bool SynthesizedExtensions) override;
316318

317319
void editorOpenSwiftSourceInterface(StringRef Name,
318320
StringRef SourceName,

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
341341
{
342342
llvm::raw_svector_ostream OS(SS);
343343
SwiftLangSupport::printUSR(VD, OS);
344+
if (BaseType){
345+
if(auto Target = BaseType->getAnyNominal()) {
346+
OS << LangSupport::SynthesizedUSRSeparator;
347+
SwiftLangSupport::printUSR(Target, OS);
348+
}
349+
}
344350
}
345351
unsigned USREnd = SS.size();
346352

tools/SourceKit/tools/sourcekitd-test/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,6 @@ def group_name : Separate<["-"], "group-name">,
7272

7373
def simplified_demangling : Flag<["-"], "simplified-demangling">,
7474
HelpText<"Use simplified demangling for the 'demangle' request">;
75+
76+
def synthesized_extension : Flag<["-"], "synthesized-extension">,
77+
HelpText<"Print synthesized extensions when generating interface">;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
228228
SimplifiedDemangling = true;
229229
break;
230230

231+
case OPT_synthesized_extension:
232+
SynthesizedExtensions = true;
233+
break;
234+
231235
case OPT_UNKNOWN:
232236
llvm::errs() << "error: unknown argument: "
233237
<< InputArg->getAsString(ParsedArgs) << '\n';

0 commit comments

Comments
 (0)