Skip to content

[SourceKit] 4.0: Accept swift_version in requests and use to set the swift version for interface generation of an ObjC header #9338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/SourceKit/InterfaceGen/Inputs/header2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ typedef struct {
long width;
long height;
} NUPixelSize;

#if __swift__ >= 40000
void show_only_for_swift_4(void);
#endif
7 changes: 6 additions & 1 deletion test/SourceKit/InterfaceGen/gen_header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
// RUN: rm %t.m

// RUN: echo '#include "header2.h"' > %t.m
// RUN: %sourcekitd-test -req=interface-gen -header %S/Inputs/header2.h -- -fsyntax-only %t.m -I %S/Inputs > %t.header2.response
// RUN: %sourcekitd-test -req=interface-gen -header %S/Inputs/header2.h -swift-version=3 -- -fsyntax-only %t.m -I %S/Inputs > %t.header2.response
// RUN: diff -u %s.header2.response %t.header2.response

// RUN: echo '#include "header2.h"' > %t.m
// 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
// RUN: %FileCheck -input-file %t.header2.swift4.response %s -check-prefix=SWIFT4
// SWIFT4: public func show_only_for_swift_4()
3 changes: 2 additions & 1 deletion tools/SourceKit/include/SourceKit/Core/LangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ class LangSupport {
StringRef Name,
StringRef HeaderName,
ArrayRef<const char *> Args,
bool SynthesizedExtensions) = 0;
bool SynthesizedExtensions,
Optional<unsigned> swiftVersion) = 0;

virtual void editorOpenSwiftSourceInterface(StringRef Name,
StringRef SourceName,
Expand Down
8 changes: 7 additions & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "swift/AST/ASTPrinter.h"
#include "swift/AST/ASTWalker.h"
#include "swift/Basic/Version.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/IDE/ModuleInterfacePrinting.h"
Expand Down Expand Up @@ -800,7 +801,8 @@ void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
StringRef Name,
StringRef HeaderName,
ArrayRef<const char *> Args,
bool SynthesizedExtensions) {
bool SynthesizedExtensions,
Optional<unsigned> swiftVersion) {
CompilerInstance CI;
// Display diagnostics to stderr.
PrintingDiagnosticConsumer PrintDiags;
Expand Down Expand Up @@ -831,6 +833,10 @@ void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
}

Invocation.getClangImporterOptions().ImportForwardDeclarations = true;
if (swiftVersion.hasValue()) {
auto swiftVer = version::Version({swiftVersion.getValue()});
Invocation.getLangOptions().EffectiveLanguageVersion = swiftVer;
}
auto IFaceGenRef = SwiftInterfaceGenContext::create(Name,
/*IsModule=*/false,
HeaderName,
Expand Down
3 changes: 2 additions & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ class SwiftLangSupport : public LangSupport {
StringRef Name,
StringRef HeaderName,
ArrayRef<const char *> Args,
bool SynthesizedExtensions) override;
bool SynthesizedExtensions,
Optional<unsigned> swiftVersion) override;

void editorOpenSwiftSourceInterface(StringRef Name,
StringRef SourceName,
Expand Down
3 changes: 3 additions & 0 deletions tools/SourceKit/tools/sourcekitd-test/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def async : Flag<["-"], "async">,
def end_pos : Separate<["-"], "end-pos">, HelpText<"line:col">;
def end_pos_EQ : Joined<["-"], "end-pos=">, Alias<end_pos>;

def swift_version : Separate<["-"], "swift-version">, HelpText<"the swift version to use">;
def swift_version_EQ : Joined<["-"], "swift-version=">, Alias<swift_version>;

def swift_name : Separate<["-"], "swift-name">,
HelpText<"Swift name to translate from">;

Expand Down
10 changes: 10 additions & 0 deletions tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
break;
}

case OPT_swift_version: {
unsigned ver;
if (StringRef(InputArg->getValue()).getAsInteger(10, ver)) {
llvm::errs() << "error: expected integer for 'swift-version'\n";
return true;
}
SwiftVersion = ver;
break;
}

case OPT_line:
if (StringRef(InputArg->getValue()).getAsInteger(10, Line)) {
llvm::errs() << "error: expected integer for 'line'\n";
Expand Down
1 change: 1 addition & 0 deletions tools/SourceKit/tools/sourcekitd-test/TestOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct TestOptions {
unsigned EndCol = 0;
unsigned Offset = 0;
unsigned Length = 0;
llvm::Optional<unsigned> SwiftVersion;
llvm::Optional<std::string> ReplaceText;
std::string ModuleName;
std::string HeaderPath;
Expand Down
8 changes: 8 additions & 0 deletions tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ static sourcekitd_uid_t KeyBaseName;
static sourcekitd_uid_t KeyArgNames;
static sourcekitd_uid_t KeySelectorPieces;
static sourcekitd_uid_t KeyNameKind;
static sourcekitd_uid_t KeySwiftVersion;

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

KeySwiftVersion = sourcekitd_uid_get_from_cstr("key.swift_version");

SemaDiagnosticStage = sourcekitd_uid_get_from_cstr("source.diagnostic.stage.swift.sema");

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

if (Opts.SwiftVersion.hasValue()) {
sourcekitd_request_dictionary_set_int64(Req, KeySwiftVersion,
Opts.SwiftVersion.getValue());
}

if (Opts.PrintRequest)
sourcekitd_request_description_dump(Req);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class RequestDict {
llvm::function_ref<bool(RequestDict)> applier);

bool getInt64(SourceKit::UIdent Key, int64_t &Val, bool isOptional);
Optional<int64_t> getOptionalInt64(SourceKit::UIdent Key);
};

void initialize();
Expand Down
2 changes: 2 additions & 0 deletions tools/SourceKit/tools/sourcekitd/lib/API/DictionaryKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ extern SourceKit::UIdent KeySelectorPieces;
extern SourceKit::UIdent KeyNameKind;
extern SourceKit::UIdent KeyLocalizationKey;

extern SourceKit::UIdent KeySwiftVersion;

/// \brief Used for determining the printing order of dictionary keys.
bool compareDictKeys(SourceKit::UIdent LHS, SourceKit::UIdent RHS);

Expand Down
14 changes: 10 additions & 4 deletions tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ editorOpenInterface(StringRef Name, StringRef ModuleName,
static sourcekitd_response_t
editorOpenHeaderInterface(StringRef Name, StringRef HeaderName,
ArrayRef<const char *> Args,
bool SynthesizedExtensions);
bool SynthesizedExtensions,
Optional<unsigned> swiftVersion);

static void
editorOpenSwiftSourceInterface(StringRef Name, StringRef SourceName,
Expand Down Expand Up @@ -524,8 +525,12 @@ void handleRequestImpl(sourcekitd_object_t ReqObj, ResponseReceiver Rec) {
int64_t SynthesizedExtension = false;
Req.getInt64(KeySynthesizedExtension, SynthesizedExtension,
/*isOptional=*/true);
Optional<int64_t> swiftVerVal = Req.getOptionalInt64(KeySwiftVersion);
Optional<unsigned> swiftVer;
if (swiftVerVal.hasValue())
swiftVer = *swiftVerVal;
return Rec(editorOpenHeaderInterface(*Name, *HeaderName, Args,
SynthesizedExtension));
SynthesizedExtension, swiftVer));
}

if (ReqUID == RequestEditorOpenSwiftSourceInterface) {
Expand Down Expand Up @@ -2016,14 +2021,15 @@ static sourcekitd_response_t editorConvertMarkupToXML(StringRef Source) {
static sourcekitd_response_t
editorOpenHeaderInterface(StringRef Name, StringRef HeaderName,
ArrayRef<const char *> Args,
bool SynthesizedExtensions) {
bool SynthesizedExtensions,
Optional<unsigned> swiftVersion) {
SKEditorConsumer EditC(/*EnableSyntaxMap=*/true,
/*EnableStructure=*/true,
/*EnableDiagnostics=*/false,
/*SyntacticOnly=*/false);
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
Lang.editorOpenHeaderInterface(EditC, Name, HeaderName, Args,
SynthesizedExtensions);
SynthesizedExtensions, swiftVersion);
return EditC.createResponse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ UIdent sourcekitd::KeySelectorPieces("key.selectorpieces");
UIdent sourcekitd::KeyNameKind("key.namekind");
UIdent sourcekitd::KeyLocalizationKey("key.localization_key");

UIdent sourcekitd::KeySwiftVersion("key.swift_version");

/// \brief Order for the keys to use when emitting the debug description of
/// dictionaries.
static UIdent *OrderedKeys[] = {
Expand Down Expand Up @@ -243,7 +245,9 @@ static UIdent *OrderedKeys[] = {
&KeyArgNames,
&KeySelectorPieces,
&KeyNameKind,
&KeyLocalizationKey,

&KeySwiftVersion,
};

static unsigned findPrintOrderForDictKey(UIdent Key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,13 @@ bool RequestDict::getInt64(SourceKit::UIdent Key, int64_t &Val,
return false;
}

Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) {
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
if (!Object)
return None;
return Object->getInt64().getValueOr(0);
}

sourcekitd_response_t
sourcekitd::createErrorRequestInvalid(const char *Description) {
return retained(new SKDError(SOURCEKITD_ERROR_REQUEST_INVALID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ bool RequestDict::getInt64(SourceKit::UIdent Key, int64_t &Val,
return false;
}

Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) {
xpc_object_t xobj = xpc_dictionary_get_value(Dict, Key.c_str());
if (!xobj)
return None;
return xpc_int64_get_value(xobj);
}

sourcekitd_response_t
sourcekitd::createErrorRequestInvalid(const char *Description) {
return CustomXPCData::createErrorRequestInvalid(Description).getXObj();
Expand Down