diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 661a02f858e6..197fe07cb012 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5613,7 +5613,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_fexperimental_new_pass_manager, options::OPT_fno_experimental_new_pass_manager); - ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind); + ObjCRuntime Runtime = AddObjCRuntimeArgs(Args, Inputs, CmdArgs, rewriteKind); RenderObjCOptions(TC, D, RawTriple, Args, Runtime, rewriteKind != RK_None, Input, CmdArgs); @@ -6257,6 +6257,7 @@ Clang::~Clang() {} /// /// Returns true if the runtime is non-fragile. ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, + const InputInfoList &inputs, ArgStringList &cmdArgs, RewriteKind rewriteKind) const { // Look for the controlling runtime option. @@ -6380,8 +6381,11 @@ ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple()); } - cmdArgs.push_back( - args.MakeArgString("-fobjc-runtime=" + runtime.getAsString())); + if (llvm::any_of(inputs, [](const InputInfo &input) { + return types::isObjC(input.getType()); + })) + cmdArgs.push_back( + args.MakeArgString("-fobjc-runtime=" + runtime.getAsString())); return runtime; } diff --git a/clang/lib/Driver/ToolChains/Clang.h b/clang/lib/Driver/ToolChains/Clang.h index b345c02489d4..48100c2fc6ec 100644 --- a/clang/lib/Driver/ToolChains/Clang.h +++ b/clang/lib/Driver/ToolChains/Clang.h @@ -77,6 +77,7 @@ private: enum RewriteKind { RK_None, RK_Fragile, RK_NonFragile }; ObjCRuntime AddObjCRuntimeArgs(const llvm::opt::ArgList &args, + const InputInfoList &inputs, llvm::opt::ArgStringList &cmdArgs, RewriteKind rewrite) const; diff --git a/clang/test/Driver/darwin-objc-options.m b/clang/test/Driver/darwin-objc-options.m index 3e21fb38c0a9..60827f2937ed 100644 --- a/clang/test/Driver/darwin-objc-options.m +++ b/clang/test/Driver/darwin-objc-options.m @@ -31,5 +31,12 @@ // CHECK-CHECK-I386_IOS-NOT: -fobjc-dispatch-method // CHECK-CHECK-I386_IOS: darwin-objc-options +/// Don't add -fobjc-runtime for non-ObjC input. +// RUN: touch %t.c +// RUN: %clang -target x86_64-apple-darwin -x objective-c -S -### %t.c 2>&1 | FileCheck --check-prefix=F %s +// RUN: %clang -target x86_64-apple-darwin -S -### %t.c 2>&1 | FileCheck --check-prefix=NO_F %s +// F: -fobjc-runtime= +// NO_F-NOT: -fobjc-runtime= + // Don't crash with an unexpected target triple. // RUN: %clang -target i386-apple-ios7 -S -### %s