Skip to content

Commit 896c464

Browse files
committed
[SourceKit] Accept swift_version in requests and use to set the swift version for interface generation of an ObjC header
rdar://31432960
1 parent 0db10e9 commit 896c464

File tree

15 files changed

+302
-8
lines changed

15 files changed

+302
-8
lines changed

test/SourceKit/InterfaceGen/Inputs/header2.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ typedef struct {
33
long width;
44
long height;
55
} NUPixelSize;
6+
7+
#if __swift__ >= 40000
8+
void show_only_for_swift_4(void);
9+
#endif

test/SourceKit/InterfaceGen/gen_header.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
// RUN: rm %t.m
66

77
// RUN: echo '#include "header2.h"' > %t.m
8-
// RUN: %sourcekitd-test -req=interface-gen -header %S/Inputs/header2.h -- -fsyntax-only %t.m -I %S/Inputs > %t.header2.response
8+
// RUN: %sourcekitd-test -req=interface-gen -header %S/Inputs/header2.h -swift-version=3 -- -fsyntax-only %t.m -I %S/Inputs > %t.header2.response
99
// RUN: diff -u %s.header2.response %t.header2.response
10+
11+
// RUN: echo '#include "header2.h"' > %t.m
12+
// RUN: %sourcekitd-test -req=interface-gen -header %S/Inputs/header2.h -swift-version=4 -- -fsyntax-only %t.m -I %S/Inputs > %t.header2.swift4.response
13+
// RUN: diff -u %s.header2.swift4.response %t.header2.swift4.response
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
2+
public struct NUPixelSize {
3+
4+
public var width: Int
5+
6+
public var height: Int
7+
8+
public init()
9+
10+
public init(width: Int, height: Int)
11+
}
12+
13+
public func show_only_for_swift_4()
14+
15+
[
16+
{
17+
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
18+
key.offset: 1,
19+
key.length: 6
20+
},
21+
{
22+
key.kind: source.lang.swift.syntaxtype.keyword,
23+
key.offset: 8,
24+
key.length: 6
25+
},
26+
{
27+
key.kind: source.lang.swift.syntaxtype.identifier,
28+
key.offset: 15,
29+
key.length: 11
30+
},
31+
{
32+
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
33+
key.offset: 34,
34+
key.length: 6
35+
},
36+
{
37+
key.kind: source.lang.swift.syntaxtype.keyword,
38+
key.offset: 41,
39+
key.length: 3
40+
},
41+
{
42+
key.kind: source.lang.swift.syntaxtype.identifier,
43+
key.offset: 45,
44+
key.length: 5
45+
},
46+
{
47+
key.kind: source.lang.swift.syntaxtype.typeidentifier,
48+
key.offset: 52,
49+
key.length: 3
50+
},
51+
{
52+
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
53+
key.offset: 61,
54+
key.length: 6
55+
},
56+
{
57+
key.kind: source.lang.swift.syntaxtype.keyword,
58+
key.offset: 68,
59+
key.length: 3
60+
},
61+
{
62+
key.kind: source.lang.swift.syntaxtype.identifier,
63+
key.offset: 72,
64+
key.length: 6
65+
},
66+
{
67+
key.kind: source.lang.swift.syntaxtype.typeidentifier,
68+
key.offset: 80,
69+
key.length: 3
70+
},
71+
{
72+
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
73+
key.offset: 89,
74+
key.length: 6
75+
},
76+
{
77+
key.kind: source.lang.swift.syntaxtype.keyword,
78+
key.offset: 96,
79+
key.length: 4
80+
},
81+
{
82+
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
83+
key.offset: 108,
84+
key.length: 6
85+
},
86+
{
87+
key.kind: source.lang.swift.syntaxtype.keyword,
88+
key.offset: 115,
89+
key.length: 4
90+
},
91+
{
92+
key.kind: source.lang.swift.syntaxtype.identifier,
93+
key.offset: 120,
94+
key.length: 5
95+
},
96+
{
97+
key.kind: source.lang.swift.syntaxtype.typeidentifier,
98+
key.offset: 127,
99+
key.length: 3
100+
},
101+
{
102+
key.kind: source.lang.swift.syntaxtype.identifier,
103+
key.offset: 132,
104+
key.length: 6
105+
},
106+
{
107+
key.kind: source.lang.swift.syntaxtype.typeidentifier,
108+
key.offset: 140,
109+
key.length: 3
110+
},
111+
{
112+
key.kind: source.lang.swift.syntaxtype.attribute.builtin,
113+
key.offset: 148,
114+
key.length: 6
115+
},
116+
{
117+
key.kind: source.lang.swift.syntaxtype.keyword,
118+
key.offset: 155,
119+
key.length: 4
120+
},
121+
{
122+
key.kind: source.lang.swift.syntaxtype.identifier,
123+
key.offset: 160,
124+
key.length: 21
125+
}
126+
]
127+
[
128+
{
129+
key.kind: source.lang.swift.ref.struct,
130+
key.offset: 52,
131+
key.length: 3,
132+
key.is_system: 1
133+
},
134+
{
135+
key.kind: source.lang.swift.ref.struct,
136+
key.offset: 80,
137+
key.length: 3,
138+
key.is_system: 1
139+
},
140+
{
141+
key.kind: source.lang.swift.ref.struct,
142+
key.offset: 127,
143+
key.length: 3,
144+
key.is_system: 1
145+
},
146+
{
147+
key.kind: source.lang.swift.ref.struct,
148+
key.offset: 140,
149+
key.length: 3,
150+
key.is_system: 1
151+
}
152+
]
153+
[
154+
{
155+
key.kind: source.lang.swift.decl.struct,
156+
key.accessibility: source.lang.swift.accessibility.public,
157+
key.name: "NUPixelSize",
158+
key.offset: 8,
159+
key.length: 138,
160+
key.nameoffset: 15,
161+
key.namelength: 11,
162+
key.bodyoffset: 28,
163+
key.bodylength: 117,
164+
key.substructure: [
165+
{
166+
key.kind: source.lang.swift.decl.var.instance,
167+
key.accessibility: source.lang.swift.accessibility.public,
168+
key.setter_accessibility: source.lang.swift.accessibility.public,
169+
key.name: "width",
170+
key.offset: 41,
171+
key.length: 14,
172+
key.typename: "Int",
173+
key.nameoffset: 45,
174+
key.namelength: 5
175+
},
176+
{
177+
key.kind: source.lang.swift.decl.var.instance,
178+
key.accessibility: source.lang.swift.accessibility.public,
179+
key.setter_accessibility: source.lang.swift.accessibility.public,
180+
key.name: "height",
181+
key.offset: 68,
182+
key.length: 15,
183+
key.typename: "Int",
184+
key.nameoffset: 72,
185+
key.namelength: 6
186+
},
187+
{
188+
key.kind: source.lang.swift.decl.function.method.instance,
189+
key.accessibility: source.lang.swift.accessibility.public,
190+
key.name: "init()",
191+
key.offset: 96,
192+
key.length: 6,
193+
key.nameoffset: 96,
194+
key.namelength: 6
195+
},
196+
{
197+
key.kind: source.lang.swift.decl.function.method.instance,
198+
key.accessibility: source.lang.swift.accessibility.public,
199+
key.name: "init(width:height:)",
200+
key.offset: 115,
201+
key.length: 29,
202+
key.nameoffset: 115,
203+
key.namelength: 29,
204+
key.substructure: [
205+
{
206+
key.kind: source.lang.swift.decl.var.parameter,
207+
key.name: "width",
208+
key.offset: 120,
209+
key.length: 10,
210+
key.typename: "Int",
211+
key.nameoffset: 120,
212+
key.namelength: 5
213+
},
214+
{
215+
key.kind: source.lang.swift.decl.var.parameter,
216+
key.name: "height",
217+
key.offset: 132,
218+
key.length: 11,
219+
key.typename: "Int",
220+
key.nameoffset: 132,
221+
key.namelength: 6
222+
}
223+
]
224+
}
225+
]
226+
},
227+
{
228+
key.kind: source.lang.swift.decl.function.free,
229+
key.accessibility: source.lang.swift.accessibility.public,
230+
key.name: "show_only_for_swift_4()",
231+
key.offset: 155,
232+
key.length: 28,
233+
key.nameoffset: 160,
234+
key.namelength: 23
235+
}
236+
]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ class LangSupport {
443443
StringRef Name,
444444
StringRef HeaderName,
445445
ArrayRef<const char *> Args,
446-
bool SynthesizedExtensions) = 0;
446+
bool SynthesizedExtensions,
447+
Optional<unsigned> swiftVersion) = 0;
447448

448449
virtual void editorOpenSwiftSourceInterface(StringRef Name,
449450
StringRef SourceName,

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "swift/AST/ASTPrinter.h"
1818
#include "swift/AST/ASTWalker.h"
19+
#include "swift/Basic/Version.h"
1920
#include "swift/Frontend/Frontend.h"
2021
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
2122
#include "swift/IDE/ModuleInterfacePrinting.h"
@@ -800,7 +801,8 @@ void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
800801
StringRef Name,
801802
StringRef HeaderName,
802803
ArrayRef<const char *> Args,
803-
bool SynthesizedExtensions) {
804+
bool SynthesizedExtensions,
805+
Optional<unsigned> swiftVersion) {
804806
CompilerInstance CI;
805807
// Display diagnostics to stderr.
806808
PrintingDiagnosticConsumer PrintDiags;
@@ -831,6 +833,10 @@ void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
831833
}
832834

833835
Invocation.getClangImporterOptions().ImportForwardDeclarations = true;
836+
if (swiftVersion.hasValue()) {
837+
auto swiftVer = version::Version({swiftVersion.getValue()});
838+
Invocation.getLangOptions().EffectiveLanguageVersion = swiftVer;
839+
}
834840
auto IFaceGenRef = SwiftInterfaceGenContext::create(Name,
835841
/*IsModule=*/false,
836842
HeaderName,

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ class SwiftLangSupport : public LangSupport {
358358
StringRef Name,
359359
StringRef HeaderName,
360360
ArrayRef<const char *> Args,
361-
bool SynthesizedExtensions) override;
361+
bool SynthesizedExtensions,
362+
Optional<unsigned> swiftVersion) override;
362363

363364
void editorOpenSwiftSourceInterface(StringRef Name,
364365
StringRef SourceName,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def async : Flag<["-"], "async">,
9494
def end_pos : Separate<["-"], "end-pos">, HelpText<"line:col">;
9595
def end_pos_EQ : Joined<["-"], "end-pos=">, Alias<end_pos>;
9696

97+
def swift_version : Separate<["-"], "swift-version">, HelpText<"the swift version to use">;
98+
def swift_version_EQ : Joined<["-"], "swift-version=">, Alias<swift_version>;
99+
97100
def swift_name : Separate<["-"], "swift-name">,
98101
HelpText<"Swift name to translate from">;
99102

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
179179
break;
180180
}
181181

182+
case OPT_swift_version: {
183+
unsigned ver;
184+
if (StringRef(InputArg->getValue()).getAsInteger(10, ver)) {
185+
llvm::errs() << "error: expected integer for 'swift-version'\n";
186+
return true;
187+
}
188+
SwiftVersion = ver;
189+
break;
190+
}
191+
182192
case OPT_line:
183193
if (StringRef(InputArg->getValue()).getAsInteger(10, Line)) {
184194
llvm::errs() << "error: expected integer for 'line'\n";

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct TestOptions {
6969
unsigned EndCol = 0;
7070
unsigned Offset = 0;
7171
unsigned Length = 0;
72+
llvm::Optional<unsigned> SwiftVersion;
7273
llvm::Optional<std::string> ReplaceText;
7374
std::string ModuleName;
7475
std::string HeaderPath;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ static sourcekitd_uid_t KeyBaseName;
140140
static sourcekitd_uid_t KeyArgNames;
141141
static sourcekitd_uid_t KeySelectorPieces;
142142
static sourcekitd_uid_t KeyNameKind;
143+
static sourcekitd_uid_t KeySwiftVersion;
143144

144145
static sourcekitd_uid_t RequestProtocolVersion;
145146
static sourcekitd_uid_t RequestDemangle;
@@ -271,6 +272,8 @@ static int skt_main(int argc, const char **argv) {
271272
KeySelectorPieces = sourcekitd_uid_get_from_cstr("key.selectorpieces");
272273
KeyNameKind = sourcekitd_uid_get_from_cstr("key.namekind");
273274

275+
KeySwiftVersion = sourcekitd_uid_get_from_cstr("key.swift_version");
276+
274277
SemaDiagnosticStage = sourcekitd_uid_get_from_cstr("source.diagnostic.stage.swift.sema");
275278

276279
NoteDocUpdate = sourcekitd_uid_get_from_cstr("source.notification.editor.documentupdate");
@@ -826,6 +829,11 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
826829
Opts.HeaderPath.c_str());
827830
}
828831

832+
if (Opts.SwiftVersion.hasValue()) {
833+
sourcekitd_request_dictionary_set_int64(Req, KeySwiftVersion,
834+
Opts.SwiftVersion.getValue());
835+
}
836+
829837
if (Opts.PrintRequest)
830838
sourcekitd_request_description_dump(Req);
831839

tools/SourceKit/tools/sourcekitd/include/sourcekitd/Internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class RequestDict {
142142
llvm::function_ref<bool(RequestDict)> applier);
143143

144144
bool getInt64(SourceKit::UIdent Key, int64_t &Val, bool isOptional);
145+
Optional<int64_t> getOptionalInt64(SourceKit::UIdent Key);
145146
};
146147

147148
void initialize();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ extern SourceKit::UIdent KeySelectorPieces;
132132
extern SourceKit::UIdent KeyNameKind;
133133
extern SourceKit::UIdent KeyLocalizationKey;
134134

135+
extern SourceKit::UIdent KeySwiftVersion;
136+
135137
/// \brief Used for determining the printing order of dictionary keys.
136138
bool compareDictKeys(SourceKit::UIdent LHS, SourceKit::UIdent RHS);
137139

0 commit comments

Comments
 (0)