Skip to content
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
69 changes: 33 additions & 36 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,49 +430,54 @@ static void
getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
ASTContext &ctx,
const ClangImporterOptions &importerOpts) {
const llvm::Triple &triple = ctx.LangOpts.Target;
const auto &LangOpts = ctx.LangOpts;
const llvm::Triple &triple = LangOpts.Target;
SearchPathOptions &searchPathOpts = ctx.SearchPathOpts;

auto languageVersion = ctx.LangOpts.EffectiveLanguageVersion;

if (llvm::sys::path::extension(importerOpts.BridgingHeader).endswith(
PCH_EXTENSION)) {
invocationArgStrs.insert(
invocationArgStrs.end(),
{ "-include-pch", importerOpts.BridgingHeader }
);
if (llvm::sys::path::extension(importerOpts.BridgingHeader)
.endswith(PCH_EXTENSION)) {
invocationArgStrs.insert(invocationArgStrs.end(), {
"-include-pch", importerOpts.BridgingHeader
});
}

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

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

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

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

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

"-fretain-comments-from-system-headers",
SHIMS_INCLUDE_FLAG, searchPathOpts.RuntimeResourcePath,
});

SHIMS_INCLUDE_FLAG, searchPathOpts.RuntimeResourcePath,
});
if (LangOpts.EnableObjCInterop) {
invocationArgStrs.insert(invocationArgStrs.end(),
{"-x", "objective-c", "-std=gnu11", "-fobjc-arc"});
// TODO: Investigate whether 7.0 is a suitable default version.
if (!triple.isOSDarwin())
invocationArgStrs.insert(invocationArgStrs.end(),
{"-fobjc-runtime=ios-7.0"});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will let us make some progress, but eventually you'll need to decide which Objective-C runtime(s) to target outside of Darwin. For example, Clang supports GNUstep, and one could imagine surfacing Clang's understanding of the Objective-C runtime so that Swift could build on top of it (rather than duplicating it).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have much time to work on it, but I'm happy to provide advice to anyone who wants to add support for the GNUstep runtime (and add any missing runtime features that Swift needs).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until we get GNUStep to the point where the object layout works in a compatible manner (or change the swift object layout), I think that not permitting control over the runtime is quite reasonable.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are Swift's requirements documented anywhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Off the top of my head two of the biggest requirements are:

  • Swift wants to generate ObjC class structures (at compile time and at runtime) with additional Swift data prepended and appended.
  • Swift wants control of a bit in the ObjC class structure in order to mark ObjC classes that are also Swift classes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second requirement is easy - we have a flags field and can reserve a bit for Swift to use.

The first is a bit harder because the clang generates these structures without providing hooks to add fields. The structure is defined here.

This will change in the next version of the runtime, and the current prototype for the new ABI would make this more difficult, because it puts all of the class structures in a section and the loader in the runtime expects them to be contiguous in memory, so adding other fields would mean that the runtime would need to be aware of the structure.

I'm also rearranging some of the fields to split the structure into ones that are likely to be modified at run time and ones that aren't, based on feedback from Microsoft that the runtime generates more CoW faults at start than they'd like - I'm not sure where the Swift fields would like to go, but if it's on the immutable part then that's probably easier, because that will be reachable via a pointer to the start from the mutable part, and that can easily be a pointer to the middle of a larger structure.

} else {
invocationArgStrs.insert(invocationArgStrs.end(), {"-x", "c", "-std=gnu11"});
}

// Set C language options.
if (triple.isOSDarwin()) {
invocationArgStrs.insert(invocationArgStrs.end(), {
// Darwin uses Objective-C ARC.
"-x", "objective-c", "-std=gnu11", "-fobjc-arc",

// Define macros that Swift bridging headers use.
"-DSWIFT_CLASS_EXTRA=__attribute__((annotate(\""
SWIFT_NATIVE_ANNOTATION_STRING "\")))",
Expand Down Expand Up @@ -518,24 +523,16 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
"-DSWIFT_SDK_OVERLAY_UIKIT_EPOCH=2",
});

// Get the version of this compiler and pass it to
// C/Objective-C declarations.
// Get the version of this compiler and pass it to C/Objective-C
// declarations.
auto V = version::Version::getCurrentCompilerVersion();
if (!V.empty()) {
invocationArgStrs.insert(invocationArgStrs.end(), {
V.preprocessorDefinition("__SWIFT_COMPILER_VERSION",
{1000000000, /*ignored*/0, 1000000, 1000, 1}),
{1000000000, /*ignored*/ 0, 1000000, 1000, 1}),
});
}
} else {
invocationArgStrs.insert(invocationArgStrs.end(), {
// Non-Darwin platforms don't use the Objective-C runtime, so they can
// not import Objective-C modules.
//
// Just use the most feature-rich C language mode.
"-x", "c", "-std=gnu11",
});

// The module map used for Glibc depends on the target we're compiling for,
// and is not included in the resource directory with the other implicit
// module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
Expand Down
24 changes: 11 additions & 13 deletions test/IDE/complete_from_clang_framework.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
// 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
// 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

// RUN: %target-swift-ide-test -code-completion -source-filename %s -F %S/Inputs/mock-sdk -code-completion-token=FW_UNQUAL_1 > %t.compl.txt
// 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
// RUN: %FileCheck %s -check-prefix=CLANG_FOO < %t.compl.txt
// RUN: %FileCheck %s -check-prefix=CLANG_FOO_SUB < %t.compl.txt
// RUN: %FileCheck %s -check-prefix=CLANG_FOO_HELPER < %t.compl.txt
// RUN: %FileCheck %s -check-prefix=CLANG_FOO_HELPER_SUB < %t.compl.txt
// RUN: %FileCheck %s -check-prefix=CLANG_BAR < %t.compl.txt
// RUN: %FileCheck %s -check-prefix=CLANG_BOTH_FOO_BAR < %t.compl.txt

// 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
// 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
// RUN: %FileCheck %s -check-prefix=CLANG_FOO < %t.compl.txt
// RUN: %FileCheck %s -check-prefix=CLANG_FOO_SUB < %t.compl.txt
// RUN: %FileCheck %s -check-prefix=CLANG_QUAL_FOO_NEGATIVE < %t.compl.txt

// 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
// 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
// RUN: %FileCheck %s -check-prefix=CLANG_QUAL_BAR_1 < %t.compl.txt
// RUN: %FileCheck %s -check-prefix=CLANG_BAR < %t.compl.txt
// RUN: %FileCheck %s -check-prefix=CLANG_QUAL_BAR_NEGATIVE < %t.compl.txt

// 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
// 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

// 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
// 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
// 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
// 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

// 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
// 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
// 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
// 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

// XFAIL: linux
// 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
// 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
// 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
// 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

import Foo
// Don't import FooHelper directly in this test!
Expand Down
18 changes: 18 additions & 0 deletions tools/swift-ide-test/swift-ide-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,16 @@ static llvm::cl::opt<bool> DebugConstraintSolver("debug-constraints",
static llvm::cl::opt<bool>
IncludeLocals("include-locals", llvm::cl::desc("Index local symbols too."),
llvm::cl::cat(Category), llvm::cl::init(false));

static llvm::cl::opt<bool>
EnableObjCInterop("enable-objc-interop",
llvm::cl::desc("Enable ObjC interop."),
llvm::cl::cat(Category), llvm::cl::init(false));

static llvm::cl::opt<bool>
DisableObjCInterop("disable-objc-interop",
llvm::cl::desc("Disable ObjC interop."),
llvm::cl::cat(Category), llvm::cl::init(false));
} // namespace options

static std::unique_ptr<llvm::MemoryBuffer>
Expand Down Expand Up @@ -3005,6 +3015,14 @@ int main(int argc, char *argv[]) {
InitInvok.getLangOptions().EffectiveLanguageVersion = actual.getValue();
}
}
if (options::DisableObjCInterop) {
InitInvok.getLangOptions().EnableObjCInterop = false;
} else if (options::EnableObjCInterop) {
InitInvok.getLangOptions().EnableObjCInterop = true;
} else {
InitInvok.getLangOptions().EnableObjCInterop =
llvm::Triple(InitInvok.getTargetTriple()).isOSDarwin();
}
InitInvok.getClangImporterOptions().ModuleCachePath =
options::ModuleCachePath;
InitInvok.getClangImporterOptions().PrecompiledHeaderOutputDir =
Expand Down