Skip to content

Commit 2e05ad7

Browse files
authored
Merge pull request #9338 from akyrtzi/interface-gen-header-swift-version-4.0
[SourceKit] 4.0: Accept swift_version in requests and use to set the swift version for interface generation of an ObjC header
2 parents 8d14ee2 + 233f00c commit 2e05ad7

File tree

15 files changed

+74
-8
lines changed

15 files changed

+74
-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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@
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: %FileCheck -input-file %t.header2.swift4.response %s -check-prefix=SWIFT4
14+
// SWIFT4: public func show_only_for_swift_4()

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

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ editorOpenInterface(StringRef Name, StringRef ModuleName,
225225
static sourcekitd_response_t
226226
editorOpenHeaderInterface(StringRef Name, StringRef HeaderName,
227227
ArrayRef<const char *> Args,
228-
bool SynthesizedExtensions);
228+
bool SynthesizedExtensions,
229+
Optional<unsigned> swiftVersion);
229230

230231
static void
231232
editorOpenSwiftSourceInterface(StringRef Name, StringRef SourceName,
@@ -524,8 +525,12 @@ void handleRequestImpl(sourcekitd_object_t ReqObj, ResponseReceiver Rec) {
524525
int64_t SynthesizedExtension = false;
525526
Req.getInt64(KeySynthesizedExtension, SynthesizedExtension,
526527
/*isOptional=*/true);
528+
Optional<int64_t> swiftVerVal = Req.getOptionalInt64(KeySwiftVersion);
529+
Optional<unsigned> swiftVer;
530+
if (swiftVerVal.hasValue())
531+
swiftVer = *swiftVerVal;
527532
return Rec(editorOpenHeaderInterface(*Name, *HeaderName, Args,
528-
SynthesizedExtension));
533+
SynthesizedExtension, swiftVer));
529534
}
530535

531536
if (ReqUID == RequestEditorOpenSwiftSourceInterface) {
@@ -2016,14 +2021,15 @@ static sourcekitd_response_t editorConvertMarkupToXML(StringRef Source) {
20162021
static sourcekitd_response_t
20172022
editorOpenHeaderInterface(StringRef Name, StringRef HeaderName,
20182023
ArrayRef<const char *> Args,
2019-
bool SynthesizedExtensions) {
2024+
bool SynthesizedExtensions,
2025+
Optional<unsigned> swiftVersion) {
20202026
SKEditorConsumer EditC(/*EnableSyntaxMap=*/true,
20212027
/*EnableStructure=*/true,
20222028
/*EnableDiagnostics=*/false,
20232029
/*SyntacticOnly=*/false);
20242030
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
20252031
Lang.editorOpenHeaderInterface(EditC, Name, HeaderName, Args,
2026-
SynthesizedExtensions);
2032+
SynthesizedExtensions, swiftVersion);
20272033
return EditC.createResponse();
20282034
}
20292035

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ UIdent sourcekitd::KeySelectorPieces("key.selectorpieces");
145145
UIdent sourcekitd::KeyNameKind("key.namekind");
146146
UIdent sourcekitd::KeyLocalizationKey("key.localization_key");
147147

148+
UIdent sourcekitd::KeySwiftVersion("key.swift_version");
149+
148150
/// \brief Order for the keys to use when emitting the debug description of
149151
/// dictionaries.
150152
static UIdent *OrderedKeys[] = {
@@ -243,7 +245,9 @@ static UIdent *OrderedKeys[] = {
243245
&KeyArgNames,
244246
&KeySelectorPieces,
245247
&KeyNameKind,
248+
&KeyLocalizationKey,
246249

250+
&KeySwiftVersion,
247251
};
248252

249253
static unsigned findPrintOrderForDictKey(UIdent Key) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,13 @@ bool RequestDict::getInt64(SourceKit::UIdent Key, int64_t &Val,
760760
return false;
761761
}
762762

763+
Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) {
764+
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
765+
if (!Object)
766+
return None;
767+
return Object->getInt64().getValueOr(0);
768+
}
769+
763770
sourcekitd_response_t
764771
sourcekitd::createErrorRequestInvalid(const char *Description) {
765772
return retained(new SKDError(SOURCEKITD_ERROR_REQUEST_INVALID,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ bool RequestDict::getInt64(SourceKit::UIdent Key, int64_t &Val,
351351
return false;
352352
}
353353

354+
Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) {
355+
xpc_object_t xobj = xpc_dictionary_get_value(Dict, Key.c_str());
356+
if (!xobj)
357+
return None;
358+
return xpc_int64_get_value(xobj);
359+
}
360+
354361
sourcekitd_response_t
355362
sourcekitd::createErrorRequestInvalid(const char *Description) {
356363
return CustomXPCData::createErrorRequestInvalid(Description).getXObj();

0 commit comments

Comments
 (0)