Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 9f084a3

Browse files
committed
Change the driver's logic about Objective-C runtimes: abstract out a
structure to hold inferred information, then propagate each invididual bit down to -cc1. Separate the bits of "supports weak" and "has a native ARC runtime"; make the latter a CodeGenOption. The tool chain is still driving this decision, because it's the place that has the required deployment target information on Darwin, but at least it's better-factored now. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@134453 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9670e17 commit 9f084a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+171
-128
lines changed

include/clang/Basic/LangOptions.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class LangOptions {
131131
// FIXME: This is just a temporary option, for testing purposes.
132132
unsigned NoBitFieldTypeAlign : 1;
133133
unsigned ObjCAutoRefCount : 1; // Objective C automated reference counting
134-
unsigned ObjCNoAutoRefCountRuntime : 1; // ARC w/o extra runtime support
134+
unsigned ObjCRuntimeHasWeak : 1; // The ARC runtime supports __weak
135135
unsigned ObjCInferRelatedReturnType : 1; // Infer Objective-C related return
136136
// types
137137
unsigned FakeAddressSpaceMap : 1; // Use a fake address space map, for
@@ -176,7 +176,8 @@ class LangOptions {
176176
Trigraphs = BCPLComment = Bool = DollarIdents = AsmPreprocessor = 0;
177177
GNUMode = GNUKeywords = ImplicitInt = Digraphs = 0;
178178
HexFloats = 0;
179-
ObjCAutoRefCount = ObjCNoAutoRefCountRuntime = 0;
179+
ObjCAutoRefCount = 0;
180+
ObjCRuntimeHasWeak = 0;
180181
ObjCInferRelatedReturnType = 0;
181182
GC = ObjC1 = ObjC2 = ObjCNonFragileABI = ObjCNonFragileABI2 = 0;
182183
AppleKext = 0;

include/clang/Driver/CC1Options.td

+4-2
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,12 @@ def fobjc_arc : Flag<"-fobjc-arc">,
494494
HelpText<"Synthesize retain and release calls for Objective-C pointers">;
495495
def fobjc_arc_cxxlib_EQ : Joined<"-fobjc-arc-cxxlib=">,
496496
HelpText<"Objective-C++ Automatic Reference Counting standard library kind">;
497-
def fobjc_no_arc_runtime : Flag<"-fobjc-no-arc-runtime">,
498-
HelpText<"Implement -fobjc-arc without any extra runtime support">;
499497
def fobjc_arc_exceptions : Flag<"-fobjc-arc-exceptions">,
500498
HelpText<"Use EH-safe code when synthesizing retains and releases in -fobjc-arc">;
499+
def fobjc_runtime_has_arc : Flag<"-fobjc-runtime-has-arc">,
500+
HelpText<"The target Objective-C runtime provides ARC entrypoints">;
501+
def fobjc_runtime_has_weak : Flag<"-fobjc-runtime-has-weak">,
502+
HelpText<"The target Objective-C runtime supports ARC weak operations">;
501503
def fobjc_gc : Flag<"-fobjc-gc">,
502504
HelpText<"Enable Objective-C garbage collection">;
503505
def fobjc_gc_only : Flag<"-fobjc-gc-only">,

include/clang/Driver/ToolChain.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace driver {
2626
class HostInfo;
2727
class InputArgList;
2828
class JobAction;
29+
class ObjCRuntime;
2930
class Tool;
3031

3132
/// ToolChain - Access to tools for a single platform.
@@ -166,9 +167,6 @@ class ToolChain {
166167
/// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
167168
virtual bool UseSjLjExceptions() const { return false; }
168169

169-
/// HasARCRuntime - Does this tool chain provide a specialized ARC runtime.
170-
virtual bool HasARCRuntime() const { return true; }
171-
172170
/// ComputeLLVMTriple - Return the LLVM target triple to use, after taking
173171
/// command line arguments into account.
174172
virtual std::string ComputeLLVMTriple(const ArgList &Args) const;
@@ -180,6 +178,12 @@ class ToolChain {
180178
/// Clang.
181179
virtual std::string ComputeEffectiveClangTriple(const ArgList &Args) const;
182180

181+
/// configureObjCRuntime - Configure the known properties of the
182+
/// Objective-C runtime for this platform.
183+
///
184+
/// FIXME: this doesn't really belong here.
185+
virtual void configureObjCRuntime(ObjCRuntime &runtime) const;
186+
183187
// GetCXXStdlibType - Determine the C++ standard library type to use with the
184188
// given compilation arguments.
185189
virtual CXXStdlibType GetCXXStdlibType(const ArgList &Args) const;

include/clang/Frontend/CodeGenOptions.h

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class CodeGenOptions {
7676
unsigned NoNaNsFPMath : 1; /// Assume FP arguments, results not NaN.
7777
unsigned NoZeroInitializedInBSS : 1; /// -fno-zero-initialized-in-bss
7878
unsigned ObjCDispatchMethod : 2; /// Method of Objective-C dispatch to use.
79+
unsigned ObjCRuntimeHasARC : 1; /// The target runtime supports ARC natively
7980
unsigned OmitLeafFramePointer : 1; /// Set when -momit-leaf-frame-pointer is
8081
/// enabled.
8182
unsigned OptimizationLevel : 3; /// The -O[0-4] option specified.

lib/ARCMigrate/ARCMT.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ CompilerInvocation *createInvocationForMigration(CompilerInvocation &origCI) {
189189
CInvok->getDiagnosticOpts().ErrorLimit = 0;
190190
CInvok->getDiagnosticOpts().Warnings.push_back(
191191
"error=arc-unsafe-retained-assign");
192-
CInvok->getLangOpts().ObjCNoAutoRefCountRuntime = !HasARCRuntime(origCI);
192+
CInvok->getLangOpts().ObjCRuntimeHasWeak = HasARCRuntime(origCI);
193193

194194
return CInvok.take();
195195
}

lib/ARCMigrate/TransBlockObjCVariable.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ class RootBlockObjCVarRewriter :
9898
BlocksAttr *attr = var->getAttr<BlocksAttr>();
9999
if(!attr)
100100
continue;
101-
bool hasARCRuntime = !Pass.Ctx.getLangOptions().ObjCNoAutoRefCountRuntime;
101+
bool hasWeak = Pass.Ctx.getLangOptions().ObjCRuntimeHasWeak;
102102
SourceManager &SM = Pass.Ctx.getSourceManager();
103103
Transaction Trans(Pass.TA);
104104
Pass.TA.replaceText(SM.getInstantiationLoc(attr->getLocation()),
105105
"__block",
106-
hasARCRuntime ? "__weak" : "__unsafe_unretained");
106+
hasWeak ? "__weak" : "__unsafe_unretained");
107107
}
108108

109109
}

lib/ARCMigrate/TransProperties.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class AssignPropertiesTrans {
112112
}
113113

114114
void applyWeak(PropData &prop) {
115-
assert(!Pass.Ctx.getLangOptions().ObjCNoAutoRefCountRuntime);
115+
assert(Pass.Ctx.getLangOptions().ObjCRuntimeHasWeak);
116116

117117
Transaction Trans(Pass.TA);
118118
Pass.TA.insert(prop.IvarD->getLocation(), "__weak ");
@@ -157,7 +157,7 @@ class AssignPropertiesTrans {
157157
// There is a "error: existing ivar for assign property must be
158158
// __unsafe_unretained"; fix it.
159159

160-
if (Pass.Ctx.getLangOptions().ObjCNoAutoRefCountRuntime) {
160+
if (!Pass.Ctx.getLangOptions().ObjCRuntimeHasWeak) {
161161
// We will just add __unsafe_unretained to the ivar.
162162
Transaction Trans(Pass.TA);
163163
Pass.TA.insert(ivarD->getLocation(), "__unsafe_unretained ");

lib/CodeGen/CGObjC.cpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM,
13111311

13121312
// In -fobjc-no-arc-runtime, emit weak references to the runtime
13131313
// support library.
1314-
if (CGM.getLangOptions().ObjCNoAutoRefCountRuntime)
1314+
if (!CGM.getCodeGenOpts().ObjCRuntimeHasARC)
13151315
if (llvm::Function *f = dyn_cast<llvm::Function>(fn))
13161316
f->setLinkage(llvm::Function::ExternalWeakLinkage);
13171317

@@ -2457,13 +2457,7 @@ void CodeGenFunction::EmitObjCAutoreleasePoolStmt(
24572457

24582458
// Keep track of the current cleanup stack depth.
24592459
RunCleanupsScope Scope(*this);
2460-
const llvm::Triple Triple = getContext().Target.getTriple();
2461-
if (CGM.getLangOptions().ObjCAutoRefCount ||
2462-
(CGM.isTargetDarwin() &&
2463-
((Triple.getArch() == llvm::Triple::x86_64 &&
2464-
!Triple.isMacOSXVersionLT(10,7,0))
2465-
|| (Triple.getEnvironmentName() == "iphoneos" &&
2466-
!Triple.isOSVersionLT(5,0))))) {
2460+
if (CGM.getCodeGenOpts().ObjCRuntimeHasARC) {
24672461
llvm::Value *token = EmitObjCAutoreleasePoolPush();
24682462
EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
24692463
} else {

lib/Driver/Compilation.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "clang/Driver/ArgList.h"
1414
#include "clang/Driver/Driver.h"
1515
#include "clang/Driver/DriverDiagnostic.h"
16+
#include "clang/Driver/ObjCRuntime.h"
1617
#include "clang/Driver/Options.h"
1718
#include "clang/Driver/ToolChain.h"
1819

lib/Driver/ToolChain.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#include "clang/Driver/Driver.h"
1616
#include "clang/Driver/DriverDiagnostic.h"
1717
#include "clang/Driver/HostInfo.h"
18+
#include "clang/Driver/ObjCRuntime.h"
1819
#include "clang/Driver/Options.h"
20+
#include "llvm/Support/ErrorHandling.h"
1921

2022
using namespace clang::driver;
2123

@@ -47,6 +49,23 @@ bool ToolChain::HasNativeLLVMSupport() const {
4749
return false;
4850
}
4951

52+
void ToolChain::configureObjCRuntime(ObjCRuntime &runtime) const {
53+
switch (runtime.getKind()) {
54+
case ObjCRuntime::NeXT:
55+
// Assume a minimal NeXT runtime.
56+
runtime.HasARC = false;
57+
runtime.HasWeak = false;
58+
return;
59+
60+
case ObjCRuntime::GNU:
61+
// Assume a maximal GNU runtime.
62+
runtime.HasARC = true;
63+
runtime.HasWeak = true;
64+
return;
65+
}
66+
llvm_unreachable("invalid runtime kind!");
67+
}
68+
5069
/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
5170
//
5271
// FIXME: tblgen this.

lib/Driver/ToolChains.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/Driver/Driver.h"
2020
#include "clang/Driver/DriverDiagnostic.h"
2121
#include "clang/Driver/HostInfo.h"
22+
#include "clang/Driver/ObjCRuntime.h"
2223
#include "clang/Driver/OptTable.h"
2324
#include "clang/Driver/Option.h"
2425
#include "clang/Driver/Options.h"
@@ -74,8 +75,7 @@ bool Darwin::HasNativeLLVMSupport() const {
7475
return true;
7576
}
7677

77-
/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
78-
bool Darwin::HasARCRuntime() const {
78+
bool Darwin::hasARCRuntime() const {
7979
// FIXME: Remove this once there is a proper way to detect an ARC runtime
8080
// for the simulator.
8181
switch (ARCRuntimeForSimulator) {
@@ -93,6 +93,14 @@ bool Darwin::HasARCRuntime() const {
9393
return !isMacosxVersionLT(10, 7);
9494
}
9595

96+
/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
97+
void Darwin::configureObjCRuntime(ObjCRuntime &runtime) const {
98+
if (runtime.getKind() != ObjCRuntime::NeXT)
99+
return ToolChain::configureObjCRuntime(runtime);
100+
101+
runtime.HasARC = runtime.HasWeak = hasARCRuntime();
102+
}
103+
96104
// FIXME: Can we tablegen this?
97105
static const char *GetArmArchForMArch(llvm::StringRef Value) {
98106
if (Value == "armv6k")

lib/Driver/ToolChains.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
8080
/// initialized.
8181
std::string MacosxVersionMin;
8282

83+
bool hasARCRuntime() const;
84+
8385
private:
8486
void AddDeploymentTarget(DerivedArgList &Args) const;
8587

@@ -184,7 +186,7 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
184186

185187
virtual bool HasNativeLLVMSupport() const;
186188

187-
virtual bool HasARCRuntime() const;
189+
virtual void configureObjCRuntime(ObjCRuntime &runtime) const;
188190

189191
virtual DerivedArgList *TranslateArgs(const DerivedArgList &Args,
190192
const char *BoundArch) const;

lib/Driver/Tools.cpp

+58-51
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Driver/Compilation.h"
1818
#include "clang/Driver/Job.h"
1919
#include "clang/Driver/HostInfo.h"
20+
#include "clang/Driver/ObjCRuntime.h"
2021
#include "clang/Driver/Option.h"
2122
#include "clang/Driver/Options.h"
2223
#include "clang/Driver/ToolChain.h"
@@ -1600,47 +1601,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
16001601
options::OPT_fno_lax_vector_conversions))
16011602
CmdArgs.push_back("-fno-lax-vector-conversions");
16021603

1603-
// Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
1604-
// NOTE: This logic is duplicated in ToolChains.cpp.
1605-
bool ARC = isObjCAutoRefCount(Args);
1606-
if (ARC) {
1607-
CmdArgs.push_back("-fobjc-arc");
1608-
1609-
// Certain deployment targets don't have runtime support.
1610-
if (!getToolChain().HasARCRuntime())
1611-
CmdArgs.push_back("-fobjc-no-arc-runtime");
1612-
1613-
// Allow the user to enable full exceptions code emission.
1614-
// We define off for Objective-CC, on for Objective-C++.
1615-
if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
1616-
options::OPT_fno_objc_arc_exceptions,
1617-
/*default*/ types::isCXX(InputType)))
1618-
CmdArgs.push_back("-fobjc-arc-exceptions");
1619-
}
1620-
1621-
// -fobjc-infer-related-result-type is the default, except in the Objective-C
1622-
// rewriter.
1623-
if (IsRewriter)
1624-
CmdArgs.push_back("-fno-objc-infer-related-result-type");
1625-
1626-
// Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1627-
// takes precedence.
1628-
const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1629-
if (!GCArg)
1630-
GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1631-
if (GCArg) {
1632-
if (ARC) {
1633-
D.Diag(clang::diag::err_drv_objc_gc_arr)
1634-
<< GCArg->getAsString(Args);
1635-
} else if (getToolChain().SupportsObjCGC()) {
1636-
GCArg->render(Args, CmdArgs);
1637-
} else {
1638-
// FIXME: We should move this to a hard error.
1639-
D.Diag(clang::diag::warn_drv_objc_gc_unsupported)
1640-
<< GCArg->getAsString(Args);
1641-
}
1642-
}
1643-
16441604
if (Args.getLastArg(options::OPT_fapple_kext))
16451605
CmdArgs.push_back("-fapple-kext");
16461606

@@ -1805,17 +1765,23 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
18051765
false))
18061766
CmdArgs.push_back("-fgnu89-inline");
18071767

1808-
// -fnext-runtime defaults to on Darwin and when rewriting Objective-C, and is
1809-
// -the -cc1 default.
1810-
bool NeXTRuntimeIsDefault =
1811-
IsRewriter || getToolChain().getTriple().getOS() == llvm::Triple::Darwin;
1812-
if (!Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
1813-
NeXTRuntimeIsDefault))
1814-
CmdArgs.push_back("-fgnu-runtime");
1815-
18161768
// -fobjc-nonfragile-abi=0 is default.
1769+
ObjCRuntime objCRuntime;
18171770
unsigned objcABIVersion = 0;
18181771
if (types::isObjC(InputType)) {
1772+
bool NeXTRuntimeIsDefault
1773+
= (IsRewriter || getToolChain().getTriple().isOSDarwin());
1774+
if (Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
1775+
NeXTRuntimeIsDefault))
1776+
objCRuntime.setKind(ObjCRuntime::NeXT);
1777+
else
1778+
objCRuntime.setKind(ObjCRuntime::GNU);
1779+
getToolChain().configureObjCRuntime(objCRuntime);
1780+
if (objCRuntime.HasARC)
1781+
CmdArgs.push_back("-fobjc-runtime-has-arc");
1782+
if (objCRuntime.HasWeak)
1783+
CmdArgs.push_back("-fobjc-runtime-has-weak");
1784+
18191785
// Compute the Objective-C ABI "version" to use. Version numbers are
18201786
// slightly confusing for historical reasons:
18211787
// 1 - Traditional "fragile" ABI
@@ -1890,6 +1856,43 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
18901856
#endif
18911857
}
18921858

1859+
// Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
1860+
// NOTE: This logic is duplicated in ToolChains.cpp.
1861+
bool ARC = isObjCAutoRefCount(Args);
1862+
if (ARC) {
1863+
CmdArgs.push_back("-fobjc-arc");
1864+
1865+
// Allow the user to enable full exceptions code emission.
1866+
// We define off for Objective-CC, on for Objective-C++.
1867+
if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
1868+
options::OPT_fno_objc_arc_exceptions,
1869+
/*default*/ types::isCXX(InputType)))
1870+
CmdArgs.push_back("-fobjc-arc-exceptions");
1871+
}
1872+
1873+
// -fobjc-infer-related-result-type is the default, except in the Objective-C
1874+
// rewriter.
1875+
if (IsRewriter)
1876+
CmdArgs.push_back("-fno-objc-infer-related-result-type");
1877+
1878+
// Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1879+
// takes precedence.
1880+
const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1881+
if (!GCArg)
1882+
GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1883+
if (GCArg) {
1884+
if (ARC) {
1885+
D.Diag(clang::diag::err_drv_objc_gc_arr)
1886+
<< GCArg->getAsString(Args);
1887+
} else if (getToolChain().SupportsObjCGC()) {
1888+
GCArg->render(Args, CmdArgs);
1889+
} else {
1890+
// FIXME: We should move this to a hard error.
1891+
D.Diag(clang::diag::warn_drv_objc_gc_unsupported)
1892+
<< GCArg->getAsString(Args);
1893+
}
1894+
}
1895+
18931896
// Add exception args.
18941897
addExceptionArgs(Args, InputType, getToolChain().getTriple(),
18951898
KernelOrKext, IsRewriter, objcABIVersion, CmdArgs);
@@ -3237,8 +3240,12 @@ void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
32373240
// In ARC, if we don't have runtime support, link in the runtime
32383241
// stubs. We have to do this *before* adding any of the normal
32393242
// linker inputs so that its initializer gets run first.
3240-
if (!getDarwinToolChain().HasARCRuntime() && isObjCAutoRefCount(Args))
3241-
getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
3243+
if (isObjCAutoRefCount(Args)) {
3244+
ObjCRuntime runtime;
3245+
getDarwinToolChain().configureObjCRuntime(runtime);
3246+
if (!runtime.HasARC)
3247+
getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
3248+
}
32423249

32433250
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
32443251

0 commit comments

Comments
 (0)