Skip to content

Commit fda0751

Browse files
committed
[SourceKit] In interface-gen request, allow clients to send SourceKit an interested USR from which we can infer the group name.
1 parent 41efb3d commit fda0751

20 files changed

+110
-13
lines changed

include/swift/AST/Module.h

+5
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,11 @@ class FileUnit : public DeclContext {
663663
return None;
664664
}
665665

666+
virtual Optional<StringRef>
667+
getGroupNameByUSR(StringRef USR) const {
668+
return None;
669+
}
670+
666671
virtual void collectAllGroups(std::vector<StringRef> &Names) const {}
667672

668673
/// Returns an implementation-defined "discriminator" for \p D, which

include/swift/IDE/ModuleInterfacePrinting.h

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ typedef OptionSet<ModuleTraversal> ModuleTraversalOptions;
4343
ArrayRef<StringRef> collectModuleGroups(ModuleDecl *M,
4444
std::vector<StringRef> &Scratch);
4545

46+
Optional<StringRef>
47+
findGroupNameForUSR(ModuleDecl *M, StringRef USR);
48+
4649
void printModuleInterface(ModuleDecl *M, Optional<StringRef> Group,
4750
ModuleTraversalOptions TraversalOptions,
4851
ASTPrinter &Printer, const PrintOptions &Options,

include/swift/Serialization/ModuleFile.h

+1
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ class ModuleFile : public LazyMemberLoader {
634634
void collectAllGroups(std::vector<StringRef> &Names) const;
635635
Optional<CommentInfo> getCommentForDecl(const Decl *D) const;
636636
Optional<CommentInfo> getCommentForDeclByUSR(StringRef USR) const;
637+
Optional<StringRef> getGroupNameByUSR(StringRef USR) const;
637638

638639
Identifier getDiscriminatorForPrivateValue(const ValueDecl *D);
639640

include/swift/Serialization/SerializedModuleLoader.h

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ class SerializedASTFile final : public LoadedFile {
145145

146146
Optional<unsigned> getSourceOrderForDecl(const Decl *D) const override;
147147

148+
Optional<StringRef> getGroupNameByUSR(StringRef USR) const override;
149+
148150
void collectAllGroups(std::vector<StringRef> &Names) const override;
149151

150152
virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const override;

lib/IDE/ModuleInterfacePrinting.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ swift::ide::collectModuleGroups(Module *M, std::vector<StringRef> &Scratch) {
182182
return llvm::makeArrayRef(Scratch);
183183
}
184184

185+
Optional<StringRef>
186+
swift::ide::findGroupNameForUSR(ModuleDecl *M, StringRef USR) {
187+
for (auto File : M->getFiles()) {
188+
if (auto Name = File->getGroupNameByUSR(USR)) {
189+
return Name;
190+
}
191+
}
192+
return None;
193+
}
194+
185195
void swift::ide::printSubmoduleInterface(
186196
Module *M,
187197
ArrayRef<StringRef> FullModuleName,

lib/Serialization/ModuleFile.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,14 @@ ModuleFile::getCommentForDeclByUSR(StringRef USR) const {
16461646
return *I;
16471647
}
16481648

1649+
Optional<StringRef>
1650+
ModuleFile::getGroupNameByUSR(StringRef USR) const {
1651+
if (auto Comment = getCommentForDeclByUSR(USR)) {
1652+
return getGroupNameById(Comment.getValue().Group);
1653+
}
1654+
return None;
1655+
}
1656+
16491657
Identifier ModuleFile::getDiscriminatorForPrivateValue(const ValueDecl *D) {
16501658
Identifier discriminator = PrivateDiscriminatorsByValue.lookup(D);
16511659
assert(!discriminator.empty() && "no discriminator found for decl");

lib/Serialization/SerializedModuleLoader.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ SerializedASTFile::collectAllGroups(std::vector<StringRef> &Names) const {
511511
File.collectAllGroups(Names);
512512
};
513513

514+
Optional<StringRef>
515+
SerializedASTFile::getGroupNameByUSR(StringRef USR) const {
516+
return File.getGroupNameByUSR(USR);
517+
}
518+
514519
void
515520
SerializedASTFile::getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {
516521
File.getTopLevelDecls(results);

test/SourceKit/CursorInfo/cursor_stdlib.swift

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func foo2(var a : [S1]) {
1919
}
2020

2121
import Swift
22+
func foo3(a: Float, b: Bool) {}
2223

2324
// RUN: %sourcekitd-test -req=cursor -pos=3:18 %s -- %s %mcp_opt %clang-importer-sdk | FileCheck -check-prefix=CHECK-OVERLAY %s
2425
// CHECK-OVERLAY: source.lang.swift.ref.var.global
@@ -61,3 +62,12 @@ import Swift
6162
// CHECK-MODULE-GROUP1-DAG: Collection
6263
// CHECK-MODULE-GROUP1-DAG: Collection/Array
6364
// CHECK-MODULE-GROUP1: MODULE GROUPS END
65+
66+
// RUN: %sourcekitd-test -req=cursor -pos=7:21 %s -- %s %mcp_opt %clang-importer-sdk | FileCheck -check-prefix=CHECK-INT1 %s
67+
// CHECK-INT1: s:Si
68+
69+
// RUN: %sourcekitd-test -req=cursor -pos=22:17 %s -- %s %mcp_opt %clang-importer-sdk | FileCheck -check-prefix=CHECK-FLOAT1 %s
70+
// CHECK-FLOAT1: s:Sf
71+
72+
// RUN: %sourcekitd-test -req=cursor -pos=22:25 %s -- %s %mcp_opt %clang-importer-sdk | FileCheck -check-prefix=CHECK-BOOL1 %s
73+
// CHECK-BOOL1: s:Sb

test/SourceKit/InterfaceGen/gen_stdlib.swift

+23
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,27 @@ var x: Int
5353
// CHECK-BOOL-NOT: Zip2Iterator
5454
// CHECK-BOOL-NOT: Zip2Sequence
5555
// CHECK-BOOL-NOT: struct Int
56+
// CHECK-BOOL-NOT: struct Float
5657
// CHECK-BOOL-NOT: extension String
58+
59+
// RUN: %sourcekitd-test -req=interface-gen -module Swift -interested-usr s:Sb > %t.Bool.response
60+
// RUN: FileCheck -check-prefix=CHECK-BOOL -input-file %t.Bool.response %s
61+
62+
// RUN: %sourcekitd-test -req=interface-gen -module Swift -interested-usr s:Si > %t.Int.response
63+
// RUN: FileCheck -check-prefix=CHECK-INT -input-file %t.Int.response %s
64+
65+
// CHECK-INT: struct Int
66+
// CHECK-INT: extension Int
67+
// CHECK-INT-NOT: Zip2Iterator
68+
// CHECK-INT-NOT: Zip2Sequence
69+
// CHECK-INT-NOT: struct Bool
70+
// CHECK-INT-NOT: struct Float
71+
72+
// RUN: %sourcekitd-test -req=interface-gen -module Swift -interested-usr s:Sf > %t.Float.response
73+
// RUN: FileCheck -check-prefix=CHECK-FLOAT -input-file %t.Float.response %s
74+
75+
// CHECK-FLOAT: struct Float
76+
// CHECK-FLOAT-NOT: Zip2Iterator
77+
// CHECK-FLOAT-NOT: Zip2Sequence
78+
// CHECK-FLOAT-NOT: struct Bool
79+
// CHECK-FLOAT-NOT: struct Int

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ class LangSupport {
426426
StringRef ModuleName,
427427
Optional<StringRef> Group,
428428
ArrayRef<const char *> Args,
429-
bool SynthesizedExtensions) = 0;
429+
bool SynthesizedExtensions,
430+
Optional<StringRef> InterestedUSR) = 0;
430431

431432
virtual void editorOpenHeaderInterface(EditorConsumer &Consumer,
432433
StringRef Name,

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ static bool getModuleInterfaceInfo(ASTContext &Ctx,
275275
Optional<StringRef> Group,
276276
SwiftInterfaceGenContext::Implementation &Impl,
277277
std::string &ErrMsg,
278-
bool SynthesizedExtensions) {
278+
bool SynthesizedExtensions,
279+
Optional<StringRef> InterestedUSR) {
279280
Module *&Mod = Impl.Mod;
280281
SourceTextInfo &Info = Impl.Info;
281282

@@ -316,6 +317,9 @@ static bool getModuleInterfaceInfo(ASTContext &Ctx,
316317
SmallString<128> Text;
317318
llvm::raw_svector_ostream OS(Text);
318319
AnnotatingPrinter Printer(Info, OS);
320+
if (!Group && InterestedUSR) {
321+
Group = findGroupNameForUSR(Mod, InterestedUSR.getValue());
322+
}
319323
printSubmoduleInterface(Mod, SplitModuleName,
320324
Group.hasValue() ? llvm::makeArrayRef(Group.getValue()) : ArrayRef<StringRef>(),
321325
TraversalOptions,
@@ -376,7 +380,8 @@ SwiftInterfaceGenContext::create(StringRef DocumentName,
376380
Optional<StringRef> Group,
377381
CompilerInvocation Invocation,
378382
std::string &ErrMsg,
379-
bool SynthesizedExtensions) {
383+
bool SynthesizedExtensions,
384+
Optional<StringRef> InterestedUSR) {
380385
SwiftInterfaceGenContextRef IFaceGenCtx{ new SwiftInterfaceGenContext() };
381386
IFaceGenCtx->Impl.DocumentName = DocumentName;
382387
IFaceGenCtx->Impl.IsModule = IsModule;
@@ -405,7 +410,7 @@ SwiftInterfaceGenContext::create(StringRef DocumentName,
405410

406411
if (IsModule) {
407412
if (getModuleInterfaceInfo(Ctx, ModuleOrHeaderName, Group, IFaceGenCtx->Impl,
408-
ErrMsg, SynthesizedExtensions))
413+
ErrMsg, SynthesizedExtensions, InterestedUSR))
409414
return nullptr;
410415
} else {
411416
auto &FEOpts = Invocation.getFrontendOptions();
@@ -585,7 +590,8 @@ void SwiftLangSupport::editorOpenInterface(EditorConsumer &Consumer,
585590
StringRef ModuleName,
586591
Optional<StringRef> Group,
587592
ArrayRef<const char *> Args,
588-
bool SynthesizedExtensions) {
593+
bool SynthesizedExtensions,
594+
Optional<StringRef> InterestedUSR) {
589595
CompilerInstance CI;
590596
// Display diagnostics to stderr.
591597
PrintingDiagnosticConsumer PrintDiags;
@@ -619,7 +625,8 @@ void SwiftLangSupport::editorOpenInterface(EditorConsumer &Consumer,
619625
Group,
620626
Invocation,
621627
ErrMsg,
622-
SynthesizedExtensions);
628+
SynthesizedExtensions,
629+
InterestedUSR);
623630
if (!IFaceGenRef) {
624631
Consumer.handleRequestError(ErrMsg.c_str());
625632
return;
@@ -725,7 +732,8 @@ void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
725732
None,
726733
Invocation,
727734
Error,
728-
SynthesizedExtensions);
735+
SynthesizedExtensions,
736+
None);
729737
if (!IFaceGenRef) {
730738
Consumer.handleRequestError(Error.c_str());
731739
return;

tools/SourceKit/lib/SwiftLang/SwiftInterfaceGenContext.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class SwiftInterfaceGenContext :
3939
Optional<StringRef> Group,
4040
swift::CompilerInvocation Invocation,
4141
std::string &ErrorMsg,
42-
bool SynthesizedExtensions);
42+
bool SynthesizedExtensions,
43+
Optional<StringRef> InterestedUSR);
4344

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

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ class SwiftLangSupport : public LangSupport {
313313
StringRef ModuleName,
314314
Optional<StringRef> Group,
315315
ArrayRef<const char *> Args,
316-
bool SynthesizedExtensions) override;
316+
bool SynthesizedExtensions,
317+
Optional<StringRef> InterestedUSR) override;
317318

318319
void editorOpenHeaderInterface(EditorConsumer &Consumer,
319320
StringRef Name,

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

+3
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,6 @@ def simplified_demangling : Flag<["-"], "simplified-demangling">,
7878

7979
def synthesized_extension : Flag<["-"], "synthesized-extension">,
8080
HelpText<"Print synthesized extensions when generating interface">;
81+
82+
def interested_usr : Separate<["-"], "interested-usr">,
83+
HelpText<"Interested USR to calculate the containing group">;

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

+4
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
182182
ModuleGroupName = InputArg->getValue();
183183
break;
184184

185+
case OPT_interested_usr:
186+
InterestedUSR = InputArg->getValue();
187+
break;
188+
185189
case OPT_header:
186190
HeaderPath = InputArg->getValue();
187191
break;

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

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct TestOptions {
5959
std::string JsonRequestPath;
6060
llvm::Optional<std::string> SourceText;
6161
std::string ModuleGroupName;
62+
std::string InterestedUSR;
6263
unsigned Line = 0;
6364
unsigned Col = 0;
6465
unsigned Offset = 0;

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

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static sourcekitd_uid_t KeyLength;
9292
static sourcekitd_uid_t KeySourceText;
9393
static sourcekitd_uid_t KeyUSR;
9494
static sourcekitd_uid_t KeyOriginalUSR;
95+
static sourcekitd_uid_t KeyInterestedUSR;
9596
static sourcekitd_uid_t KeyTypename;
9697
static sourcekitd_uid_t KeyOverrides;
9798
static sourcekitd_uid_t KeyRelatedDecls;
@@ -186,6 +187,7 @@ static int skt_main(int argc, const char **argv) {
186187
KeySourceText = sourcekitd_uid_get_from_cstr("key.sourcetext");
187188
KeyUSR = sourcekitd_uid_get_from_cstr("key.usr");
188189
KeyOriginalUSR = sourcekitd_uid_get_from_cstr("key.original_usr");
190+
KeyInterestedUSR = sourcekitd_uid_get_from_cstr("key.interested_usr");
189191
KeyTypename = sourcekitd_uid_get_from_cstr("key.typename");
190192
KeyOverrides = sourcekitd_uid_get_from_cstr("key.overrides");
191193
KeyRelatedDecls = sourcekitd_uid_get_from_cstr("key.related_decls");
@@ -593,6 +595,9 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
593595
if (!Opts.ModuleGroupName.empty())
594596
sourcekitd_request_dictionary_set_string(Req, KeyGroupName,
595597
Opts.ModuleGroupName.c_str());
598+
if (!Opts.InterestedUSR.empty())
599+
sourcekitd_request_dictionary_set_string(Req, KeyInterestedUSR,
600+
Opts.InterestedUSR.c_str());
596601
break;
597602

598603
case SourceKitRequest::FindInterfaceDoc:

tools/SourceKit/tools/sourcekitd/lib/API/DictionaryKeys.h

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern SourceKit::UIdent KeyAccessibility;
4444
extern SourceKit::UIdent KeySetterAccessibility;
4545
extern SourceKit::UIdent KeyUSR;
4646
extern SourceKit::UIdent KeyOriginalUSR;
47+
extern SourceKit::UIdent KeyInterestedUSR;
4748
extern SourceKit::UIdent KeyLine;
4849
extern SourceKit::UIdent KeyColumn;
4950
extern SourceKit::UIdent KeyReceiverUSR;

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ editorOpen(StringRef Name, llvm::MemoryBuffer *Buf, bool EnableSyntaxMap,
191191
static sourcekitd_response_t
192192
editorOpenInterface(StringRef Name, StringRef ModuleName,
193193
Optional<StringRef> Group, ArrayRef<const char *> Args,
194-
bool SynthesizedExtensions);
194+
bool SynthesizedExtensions,
195+
Optional<StringRef> InterestedUSR);
195196

196197
static sourcekitd_response_t
197198
editorOpenHeaderInterface(StringRef Name, StringRef HeaderName,
@@ -474,8 +475,9 @@ void handleRequestImpl(sourcekitd_object_t ReqObj, ResponseReceiver Rec) {
474475
int64_t SynthesizedExtension = false;
475476
Req.getInt64(KeySynthesizedExtension, SynthesizedExtension,
476477
/*isOptional=*/true);
478+
Optional<StringRef> InterestedUSR = Req.getString(KeyInterestedUSR);
477479
return Rec(editorOpenInterface(*Name, *ModuleName, GroupName, Args,
478-
SynthesizedExtension));
480+
SynthesizedExtension, InterestedUSR));
479481
}
480482

481483
if (ReqUID == RequestEditorOpenHeaderInterface) {
@@ -1785,14 +1787,15 @@ editorOpen(StringRef Name, llvm::MemoryBuffer *Buf, bool EnableSyntaxMap,
17851787
static sourcekitd_response_t
17861788
editorOpenInterface(StringRef Name, StringRef ModuleName,
17871789
Optional<StringRef> Group, ArrayRef<const char *> Args,
1788-
bool SynthesizedExtensions) {
1790+
bool SynthesizedExtensions,
1791+
Optional<StringRef> InterestedUSR) {
17891792
SKEditorConsumer EditC(/*EnableSyntaxMap=*/true,
17901793
/*EnableStructure=*/true,
17911794
/*EnableDiagnostics=*/false,
17921795
/*SyntacticOnly=*/false);
17931796
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
17941797
Lang.editorOpenInterface(EditC, Name, ModuleName, Group, Args,
1795-
SynthesizedExtensions);
1798+
SynthesizedExtensions, InterestedUSR);
17961799
return EditC.createResponse();
17971800
}
17981801

tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ UIdent sourcekitd::KeyAccessibility("key.accessibility");
5757
UIdent sourcekitd::KeySetterAccessibility("key.setter_accessibility");
5858
UIdent sourcekitd::KeyUSR("key.usr");
5959
UIdent sourcekitd::KeyOriginalUSR("key.original_usr");
60+
UIdent sourcekitd::KeyInterestedUSR("key.interested_usr");
6061
UIdent sourcekitd::KeyLine("key.line");
6162
UIdent sourcekitd::KeyColumn("key.column");
6263
UIdent sourcekitd::KeyReceiverUSR("key.receiver_usr");
@@ -144,6 +145,7 @@ static UIdent *OrderedKeys[] = {
144145
&KeyName,
145146
&KeyUSR,
146147
&KeyOriginalUSR,
148+
&KeyInterestedUSR,
147149
&KeyGenericParams,
148150
&KeyGenericRequirements,
149151
&KeyDocFullAsXML,

0 commit comments

Comments
 (0)