Skip to content

Commit 06cacbc

Browse files
committed
ClangImporter: allow control over ObjCInterop
Select the language for the clang importer based on the `-enable-objc-interop` flag rather than target. This paves the road to enabling ObjC tests on non-Darwin targets as well as building on Darwin without ObjC interop.
1 parent 30895f7 commit 06cacbc

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ 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;
@@ -463,12 +464,24 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
463464
SHIMS_INCLUDE_FLAG, searchPathOpts.RuntimeResourcePath,
464465
});
465466

467+
if (LangOpts.EnableObjCInterop)
468+
invocationArgStrs.insert(invocationArgStrs.end(),
469+
{"-x", "objective-c", "-std=gnu11", "-fobjc-arc"});
470+
else
471+
invocationArgStrs.insert(invocationArgStrs.end(), {"-x", "c", "-std=gnu11"});
472+
473+
// Get the version of this compiler and pass it to C/Objective-C declarations.
474+
auto V = version::Version::getCurrentCompilerVersion();
475+
if (!V.empty()) {
476+
invocationArgStrs.insert(invocationArgStrs.end(), {
477+
V.preprocessorDefinition("__SWIFT_COMPILER_VERSION",
478+
{1000000000, /*ignored*/ 0, 1000000, 1000, 1}),
479+
});
480+
}
481+
466482
// Set C language options.
467483
if (triple.isOSDarwin()) {
468484
invocationArgStrs.insert(invocationArgStrs.end(), {
469-
// Darwin uses Objective-C ARC.
470-
"-x", "objective-c", "-std=gnu11", "-fobjc-arc",
471-
472485
// Define macros that Swift bridging headers use.
473486
"-DSWIFT_CLASS_EXTRA=__attribute__((annotate(\""
474487
SWIFT_NATIVE_ANNOTATION_STRING "\")))",
@@ -513,25 +526,7 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
513526
// Request new APIs from UIKit.
514527
"-DSWIFT_SDK_OVERLAY_UIKIT_EPOCH=2",
515528
});
516-
517-
// Get the version of this compiler and pass it to
518-
// C/Objective-C declarations.
519-
auto V = version::Version::getCurrentCompilerVersion();
520-
if (!V.empty()) {
521-
invocationArgStrs.insert(invocationArgStrs.end(), {
522-
V.preprocessorDefinition("__SWIFT_COMPILER_VERSION",
523-
{1000000000, /*ignored*/0, 1000000, 1000, 1}),
524-
});
525-
}
526529
} else {
527-
invocationArgStrs.insert(invocationArgStrs.end(), {
528-
// Non-Darwin platforms don't use the Objective-C runtime, so they can
529-
// not import Objective-C modules.
530-
//
531-
// Just use the most feature-rich C language mode.
532-
"-x", "c", "-std=gnu11",
533-
});
534-
535530
// The module map used for Glibc depends on the target we're compiling for,
536531
// and is not included in the resource directory with the other implicit
537532
// module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.

0 commit comments

Comments
 (0)