Skip to content

Commit 71477f0

Browse files
authored
Merge pull request swiftlang#12702 from fjricci/objc_interop
Objc interop
2 parents b57a9cb + 1822c88 commit 71477f0

File tree

3 files changed

+62
-49
lines changed

3 files changed

+62
-49
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -430,49 +430,54 @@ static void
430430
getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
431431
ASTContext &ctx,
432432
const ClangImporterOptions &importerOpts) {
433-
const llvm::Triple &triple = ctx.LangOpts.Target;
433+
const auto &LangOpts = ctx.LangOpts;
434+
const llvm::Triple &triple = LangOpts.Target;
434435
SearchPathOptions &searchPathOpts = ctx.SearchPathOpts;
435436

436437
auto languageVersion = ctx.LangOpts.EffectiveLanguageVersion;
437438

438-
if (llvm::sys::path::extension(importerOpts.BridgingHeader).endswith(
439-
PCH_EXTENSION)) {
440-
invocationArgStrs.insert(
441-
invocationArgStrs.end(),
442-
{ "-include-pch", importerOpts.BridgingHeader }
443-
);
439+
if (llvm::sys::path::extension(importerOpts.BridgingHeader)
440+
.endswith(PCH_EXTENSION)) {
441+
invocationArgStrs.insert(invocationArgStrs.end(), {
442+
"-include-pch", importerOpts.BridgingHeader
443+
});
444444
}
445445

446446
// Construct the invocation arguments for the current target.
447447
// Add target-independent options first.
448-
invocationArgStrs.insert(
449-
invocationArgStrs.end(),
450-
{
448+
invocationArgStrs.insert(invocationArgStrs.end(), {
449+
// Enable modules
450+
"-fmodules",
451+
"-Werror=non-modular-include-in-framework-module",
452+
"-Xclang", "-fmodule-feature", "-Xclang", "swift",
451453

452-
// Enable modules
453-
"-fmodules",
454-
"-Werror=non-modular-include-in-framework-module",
455-
"-Xclang", "-fmodule-feature", "-Xclang", "swift",
454+
// Don't emit LLVM IR.
455+
"-fsyntax-only",
456456

457-
// Don't emit LLVM IR.
458-
"-fsyntax-only",
457+
// Enable block support.
458+
"-fblocks",
459459

460-
// Enable block support.
461-
"-fblocks",
460+
languageVersion.preprocessorDefinition("__swift__", {10000, 100, 1}),
462461

463-
languageVersion.preprocessorDefinition("__swift__", {10000, 100, 1}),
462+
"-fretain-comments-from-system-headers",
464463

465-
"-fretain-comments-from-system-headers",
464+
SHIMS_INCLUDE_FLAG, searchPathOpts.RuntimeResourcePath,
465+
});
466466

467-
SHIMS_INCLUDE_FLAG, searchPathOpts.RuntimeResourcePath,
468-
});
467+
if (LangOpts.EnableObjCInterop) {
468+
invocationArgStrs.insert(invocationArgStrs.end(),
469+
{"-x", "objective-c", "-std=gnu11", "-fobjc-arc"});
470+
// TODO: Investigate whether 7.0 is a suitable default version.
471+
if (!triple.isOSDarwin())
472+
invocationArgStrs.insert(invocationArgStrs.end(),
473+
{"-fobjc-runtime=ios-7.0"});
474+
} else {
475+
invocationArgStrs.insert(invocationArgStrs.end(), {"-x", "c", "-std=gnu11"});
476+
}
469477

470478
// Set C language options.
471479
if (triple.isOSDarwin()) {
472480
invocationArgStrs.insert(invocationArgStrs.end(), {
473-
// Darwin uses Objective-C ARC.
474-
"-x", "objective-c", "-std=gnu11", "-fobjc-arc",
475-
476481
// Define macros that Swift bridging headers use.
477482
"-DSWIFT_CLASS_EXTRA=__attribute__((annotate(\""
478483
SWIFT_NATIVE_ANNOTATION_STRING "\")))",
@@ -518,24 +523,16 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
518523
"-DSWIFT_SDK_OVERLAY_UIKIT_EPOCH=2",
519524
});
520525

521-
// Get the version of this compiler and pass it to
522-
// C/Objective-C declarations.
526+
// Get the version of this compiler and pass it to C/Objective-C
527+
// declarations.
523528
auto V = version::Version::getCurrentCompilerVersion();
524529
if (!V.empty()) {
525530
invocationArgStrs.insert(invocationArgStrs.end(), {
526531
V.preprocessorDefinition("__SWIFT_COMPILER_VERSION",
527-
{1000000000, /*ignored*/0, 1000000, 1000, 1}),
532+
{1000000000, /*ignored*/ 0, 1000000, 1000, 1}),
528533
});
529534
}
530535
} else {
531-
invocationArgStrs.insert(invocationArgStrs.end(), {
532-
// Non-Darwin platforms don't use the Objective-C runtime, so they can
533-
// not import Objective-C modules.
534-
//
535-
// Just use the most feature-rich C language mode.
536-
"-x", "c", "-std=gnu11",
537-
});
538-
539536
// The module map used for Glibc depends on the target we're compiling for,
540537
// and is not included in the resource directory with the other implicit
541538
// module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.

test/IDE/complete_from_clang_framework.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=SWIFT_COMPLETIONS | %FileCheck %s -check-prefix=SWIFT_COMPLETIONS
1+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=SWIFT_COMPLETIONS | %FileCheck %s -check-prefix=SWIFT_COMPLETIONS
22

3-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=FW_UNQUAL_1 > %t.compl.txt
3+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=FW_UNQUAL_1 > %t.compl.txt
44
// RUN: %FileCheck %s -check-prefix=CLANG_FOO < %t.compl.txt
55
// RUN: %FileCheck %s -check-prefix=CLANG_FOO_SUB < %t.compl.txt
66
// RUN: %FileCheck %s -check-prefix=CLANG_FOO_HELPER < %t.compl.txt
77
// RUN: %FileCheck %s -check-prefix=CLANG_FOO_HELPER_SUB < %t.compl.txt
88
// RUN: %FileCheck %s -check-prefix=CLANG_BAR < %t.compl.txt
99
// RUN: %FileCheck %s -check-prefix=CLANG_BOTH_FOO_BAR < %t.compl.txt
1010

11-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_QUAL_FOO_1 > %t.compl.txt
11+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=CLANG_QUAL_FOO_1 > %t.compl.txt
1212
// RUN: %FileCheck %s -check-prefix=CLANG_FOO < %t.compl.txt
1313
// RUN: %FileCheck %s -check-prefix=CLANG_FOO_SUB < %t.compl.txt
1414
// RUN: %FileCheck %s -check-prefix=CLANG_QUAL_FOO_NEGATIVE < %t.compl.txt
1515

16-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_QUAL_BAR_1 > %t.compl.txt
16+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=CLANG_QUAL_BAR_1 > %t.compl.txt
1717
// RUN: %FileCheck %s -check-prefix=CLANG_QUAL_BAR_1 < %t.compl.txt
1818
// RUN: %FileCheck %s -check-prefix=CLANG_BAR < %t.compl.txt
1919
// RUN: %FileCheck %s -check-prefix=CLANG_QUAL_BAR_NEGATIVE < %t.compl.txt
2020

21-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_QUAL_FOO_2 | %FileCheck %s -check-prefix=CLANG_QUAL_FOO_2
21+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=CLANG_QUAL_FOO_2 | %FileCheck %s -check-prefix=CLANG_QUAL_FOO_2
2222

23-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=FUNCTION_CALL_1 | %FileCheck %s -check-prefix=FUNCTION_CALL_1
24-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=FUNCTION_CALL_2 | %FileCheck %s -check-prefix=FUNCTION_CALL_2
23+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=FUNCTION_CALL_1 | %FileCheck %s -check-prefix=FUNCTION_CALL_1
24+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=FUNCTION_CALL_2 | %FileCheck %s -check-prefix=FUNCTION_CALL_2
2525

26-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_STRUCT_MEMBERS_1 | %FileCheck %s -check-prefix=CLANG_STRUCT_MEMBERS_1
27-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_CLASS_MEMBERS_1 | %FileCheck %s -check-prefix=CLANG_CLASS_MEMBERS_1
28-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_CLASS_MEMBERS_2 | %FileCheck %s -check-prefix=CLANG_CLASS_MEMBERS_2
29-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=CLANG_INSTANCE_MEMBERS_1 | %FileCheck %s -check-prefix=CLANG_INSTANCE_MEMBERS_1
30-
31-
// XFAIL: linux
26+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=CLANG_STRUCT_MEMBERS_1 | %FileCheck %s -check-prefix=CLANG_STRUCT_MEMBERS_1
27+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=CLANG_CLASS_MEMBERS_1 | %FileCheck %s -check-prefix=CLANG_CLASS_MEMBERS_1
28+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=CLANG_CLASS_MEMBERS_2 | %FileCheck %s -check-prefix=CLANG_CLASS_MEMBERS_2
29+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -enable-objc-interop -code-completion-token=CLANG_INSTANCE_MEMBERS_1 | %FileCheck %s -check-prefix=CLANG_INSTANCE_MEMBERS_1
3230

3331
import Foo
3432
// Don't import FooHelper directly in this test!

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,16 @@ static llvm::cl::opt<bool> DebugConstraintSolver("debug-constraints",
636636
static llvm::cl::opt<bool>
637637
IncludeLocals("include-locals", llvm::cl::desc("Index local symbols too."),
638638
llvm::cl::cat(Category), llvm::cl::init(false));
639+
640+
static llvm::cl::opt<bool>
641+
EnableObjCInterop("enable-objc-interop",
642+
llvm::cl::desc("Enable ObjC interop."),
643+
llvm::cl::cat(Category), llvm::cl::init(false));
644+
645+
static llvm::cl::opt<bool>
646+
DisableObjCInterop("disable-objc-interop",
647+
llvm::cl::desc("Disable ObjC interop."),
648+
llvm::cl::cat(Category), llvm::cl::init(false));
639649
} // namespace options
640650

641651
static std::unique_ptr<llvm::MemoryBuffer>
@@ -3006,6 +3016,14 @@ int main(int argc, char *argv[]) {
30063016
InitInvok.getLangOptions().EffectiveLanguageVersion = actual.getValue();
30073017
}
30083018
}
3019+
if (options::DisableObjCInterop) {
3020+
InitInvok.getLangOptions().EnableObjCInterop = false;
3021+
} else if (options::EnableObjCInterop) {
3022+
InitInvok.getLangOptions().EnableObjCInterop = true;
3023+
} else {
3024+
InitInvok.getLangOptions().EnableObjCInterop =
3025+
llvm::Triple(InitInvok.getTargetTriple()).isOSDarwin();
3026+
}
30093027
InitInvok.getClangImporterOptions().ModuleCachePath =
30103028
options::ModuleCachePath;
30113029
InitInvok.getClangImporterOptions().PrecompiledHeaderOutputDir =

0 commit comments

Comments
 (0)