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

Commit dd9e3a3

Browse files
committed
Toolchain: Normalize dwarf, sjlj and seh eh
This is a re-apply of r319294. adds -fseh-exceptions and -fdwarf-exceptions flags clang will check if the user has specified an exception model flag, in the absense of specifying the exception model clang will then check the driver default and append the model flag for that target to cc1 -fno-exceptions has a higher priority then specifying the model move __SEH__ macro definitions out of Targets into InitPreprocessor behind the -fseh-exceptions flag move __ARM_DWARF_EH__ macrodefinitions out of verious targets and into InitPreprocessor behind the -fdwarf-exceptions flag and arm|thumb check remove unused USESEHExceptions from the MinGW Driver fold USESjLjExceptions into a new GetExceptionModel function that gives the toolchain classes more flexibility with eh models Reviewers: rnk, mstorsjo Differential Revision: https://reviews.llvm.org/D39673 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319297 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e9d94f5 commit dd9e3a3

26 files changed

+149
-63
lines changed

docs/ClangCommandLineReference.rst

+8
Original file line numberDiff line numberDiff line change
@@ -1706,10 +1706,18 @@ Which overload candidates to show when overload resolution fails: best\|all; def
17061706

17071707
Enable C++14 sized global deallocation functions
17081708

1709+
.. option:: -fdwarf-exceptions
1710+
1711+
Use DWARF style exceptions
1712+
17091713
.. option:: -fsjlj-exceptions
17101714

17111715
Use SjLj style exceptions
17121716

1717+
.. option:: -fseh-exceptions
1718+
1719+
Use SEH style exceptions
1720+
17131721
.. option:: -fslp-vectorize, -fno-slp-vectorize, -ftree-slp-vectorize
17141722

17151723
Enable the superword-level parallelism vectorization passes

include/clang/Basic/LangOptions.def

+2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ LANGOPT(ZVector , 1, 0, "System z vector extensions")
124124
LANGOPT(Exceptions , 1, 0, "exception handling")
125125
LANGOPT(ObjCExceptions , 1, 0, "Objective-C exceptions")
126126
LANGOPT(CXXExceptions , 1, 0, "C++ exceptions")
127+
LANGOPT(DWARFExceptions , 1, 0, "dwarf exception handling")
127128
LANGOPT(SjLjExceptions , 1, 0, "setjmp-longjump exception handling")
129+
LANGOPT(SEHExceptions , 1, 0, "SEH .xdata exception handling")
128130
LANGOPT(ExternCNoUnwind , 1, 0, "Assume extern C functions don't unwind")
129131
LANGOPT(TraditionalCPP , 1, 0, "traditional CPP emulation")
130132
LANGOPT(RTTI , 1, 1, "run-time type information")

include/clang/Driver/Options.td

+4
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,12 @@ def fencoding_EQ : Joined<["-"], "fencoding=">, Group<f_Group>;
800800
def ferror_limit_EQ : Joined<["-"], "ferror-limit=">, Group<f_Group>, Flags<[CoreOption]>;
801801
def fexceptions : Flag<["-"], "fexceptions">, Group<f_Group>, Flags<[CC1Option]>,
802802
HelpText<"Enable support for exception handling">;
803+
def fdwarf_exceptions : Flag<["-"], "fdwarf-exceptions">, Group<f_Group>,
804+
Flags<[CC1Option]>, HelpText<"Use DWARF style exceptions">;
803805
def fsjlj_exceptions : Flag<["-"], "fsjlj-exceptions">, Group<f_Group>,
804806
Flags<[CC1Option]>, HelpText<"Use SjLj style exceptions">;
807+
def fseh_exceptions : Flag<["-"], "fseh-exceptions">, Group<f_Group>,
808+
Flags<[CC1Option]>, HelpText<"Use SEH style exceptions">;
805809
def fexcess_precision_EQ : Joined<["-"], "fexcess-precision=">,
806810
Group<clang_ignored_gcc_optimization_f_Group>;
807811
def : Flag<["-"], "fexpensive-optimizations">, Group<clang_ignored_gcc_optimization_f_Group>;

include/clang/Driver/ToolChain.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,9 @@ class ToolChain {
397397
return llvm::DebuggerKind::GDB;
398398
}
399399

400-
/// UseSjLjExceptions - Does this tool chain use SjLj exceptions.
401-
virtual bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const {
402-
return false;
403-
}
400+
/// GetExceptionModel - Return the tool chain exception model.
401+
virtual llvm::ExceptionHandling
402+
GetExceptionModel(const llvm::opt::ArgList &Args) const;
404403

405404
/// SupportsEmbeddedBitcode - Does this tool chain support embedded bitcode.
406405
virtual bool SupportsEmbeddedBitcode() const {

lib/Basic/Targets/ARM.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,6 @@ void MinGWARMTargetInfo::getTargetDefines(const LangOptions &Opts,
10001000
MacroBuilder &Builder) const {
10011001
WindowsARMTargetInfo::getTargetDefines(Opts, Builder);
10021002
Builder.defineMacro("_ARM_");
1003-
Builder.defineMacro("__ARM_DWARF_EH__");
10041003
}
10051004

10061005
CygwinARMTargetInfo::CygwinARMTargetInfo(const llvm::Triple &Triple,

lib/Basic/Targets/OSTargets.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
133133
if (Triple.isOSDarwin())
134134
Builder.defineMacro("__MACH__");
135135

136-
// The Watch ABI uses Dwarf EH.
137-
if (Triple.isWatchABI())
138-
Builder.defineMacro("__ARM_DWARF_EH__");
139-
140136
PlatformMinVersion = VersionTuple(Maj, Min, Rev);
141137
}
142138
} // namespace targets

lib/Basic/Targets/OSTargets.h

-11
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,6 @@ class LLVM_LIBRARY_VISIBILITY NetBSDTargetInfo : public OSTargetInfo<Target> {
358358
Builder.defineMacro("__ELF__");
359359
if (Opts.POSIXThreads)
360360
Builder.defineMacro("_REENTRANT");
361-
362-
switch (Triple.getArch()) {
363-
default:
364-
break;
365-
case llvm::Triple::arm:
366-
case llvm::Triple::armeb:
367-
case llvm::Triple::thumb:
368-
case llvm::Triple::thumbeb:
369-
Builder.defineMacro("__ARM_DWARF_EH__");
370-
break;
371-
}
372361
}
373362

374363
public:

lib/Basic/Targets/X86.h

-13
Original file line numberDiff line numberDiff line change
@@ -710,15 +710,6 @@ class LLVM_LIBRARY_VISIBILITY MinGWX86_64TargetInfo
710710
LongDoubleFormat = &llvm::APFloat::x87DoubleExtended();
711711
HasFloat128 = true;
712712
}
713-
714-
void getTargetDefines(const LangOptions &Opts,
715-
MacroBuilder &Builder) const override {
716-
WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
717-
718-
// GCC defines this macro when it is using __gxx_personality_seh0.
719-
if (!Opts.SjLjExceptions)
720-
Builder.defineMacro("__SEH__");
721-
}
722713
};
723714

724715
// x86-64 Cygwin target
@@ -740,10 +731,6 @@ class LLVM_LIBRARY_VISIBILITY CygwinX86_64TargetInfo : public X86_64TargetInfo {
740731
DefineStd(Builder, "unix", Opts);
741732
if (Opts.CPlusPlus)
742733
Builder.defineMacro("_GNU_SOURCE");
743-
744-
// GCC defines this macro when it is using __gxx_personality_seh0.
745-
if (!Opts.SjLjExceptions)
746-
Builder.defineMacro("__SEH__");
747734
}
748735
};
749736

lib/CodeGen/BackendUtil.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ static void initTargetOptions(llvm::TargetOptions &Options,
424424

425425
if (LangOpts.SjLjExceptions)
426426
Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
427+
if (LangOpts.SEHExceptions)
428+
Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
429+
if (LangOpts.DWARFExceptions)
430+
Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
427431

428432
Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
429433
Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;

lib/CodeGen/CGException.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,11 @@ EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr };
112112
const EHPersonality
113113
EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr };
114114

115-
/// On Win64, use libgcc's SEH personality function. We fall back to dwarf on
116-
/// other platforms, unless the user asked for SjLj exceptions.
117-
static bool useLibGCCSEHPersonality(const llvm::Triple &T) {
118-
return T.isOSWindows() && T.getArch() == llvm::Triple::x86_64;
119-
}
120-
121115
static const EHPersonality &getCPersonality(const llvm::Triple &T,
122116
const LangOptions &L) {
123117
if (L.SjLjExceptions)
124118
return EHPersonality::GNU_C_SJLJ;
125-
else if (useLibGCCSEHPersonality(T))
119+
if (L.SEHExceptions)
126120
return EHPersonality::GNU_C_SEH;
127121
return EHPersonality::GNU_C;
128122
}
@@ -144,7 +138,7 @@ static const EHPersonality &getObjCPersonality(const llvm::Triple &T,
144138
case ObjCRuntime::ObjFW:
145139
if (L.SjLjExceptions)
146140
return EHPersonality::GNU_ObjC_SJLJ;
147-
else if (useLibGCCSEHPersonality(T))
141+
if (L.SEHExceptions)
148142
return EHPersonality::GNU_ObjC_SEH;
149143
return EHPersonality::GNU_ObjC;
150144
}
@@ -155,7 +149,7 @@ static const EHPersonality &getCXXPersonality(const llvm::Triple &T,
155149
const LangOptions &L) {
156150
if (L.SjLjExceptions)
157151
return EHPersonality::GNU_CPlusPlus_SJLJ;
158-
else if (useLibGCCSEHPersonality(T))
152+
if (L.SEHExceptions)
159153
return EHPersonality::GNU_CPlusPlus_SEH;
160154
return EHPersonality::GNU_CPlusPlus;
161155
}
@@ -211,6 +205,10 @@ const EHPersonality &EHPersonality::get(CodeGenModule &CGM,
211205
if (T.isWindowsMSVCEnvironment() && !L.ObjC1) {
212206
if (L.SjLjExceptions)
213207
return EHPersonality::GNU_CPlusPlus_SJLJ;
208+
if (L.SEHExceptions)
209+
return EHPersonality::GNU_CPlusPlus_SEH;
210+
if (L.DWARFExceptions)
211+
return EHPersonality::GNU_CPlusPlus;
214212
else
215213
return EHPersonality::MSVC_CxxFrameHandler3;
216214
}

lib/Driver/ToolChain.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "llvm/Support/ErrorHandling.h"
2828
#include "llvm/Support/FileSystem.h"
2929
#include "llvm/Support/Path.h"
30+
#include "llvm/MC/MCAsmInfo.h"
31+
#include "llvm/MC/MCRegisterInfo.h"
3032
#include "llvm/Support/TargetParser.h"
3133
#include "llvm/Support/TargetRegistry.h"
3234

@@ -449,6 +451,13 @@ ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
449451
VersionTuple());
450452
}
451453

454+
llvm::ExceptionHandling
455+
ToolChain::GetExceptionModel(const llvm::opt::ArgList &Args) const {
456+
if (Triple.isOSWindows() && Triple.getArch() != llvm::Triple::x86)
457+
return llvm::ExceptionHandling::WinEH;
458+
return llvm::ExceptionHandling::None;
459+
}
460+
452461
bool ToolChain::isThreadModelSupported(const StringRef Model) const {
453462
if (Model == "single") {
454463
// FIXME: 'single' is only supported on ARM and WebAssembly so far.

lib/Driver/ToolChains/Clang.cpp

+27-3
Original file line numberDiff line numberDiff line change
@@ -4166,9 +4166,33 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
41664166
addExceptionArgs(Args, InputType, getToolChain(), KernelOrKext, Runtime,
41674167
CmdArgs);
41684168

4169-
if (Args.hasArg(options::OPT_fsjlj_exceptions) ||
4170-
getToolChain().UseSjLjExceptions(Args))
4171-
CmdArgs.push_back("-fsjlj-exceptions");
4169+
// Handle exception personalities
4170+
Arg *A = Args.getLastArg(options::OPT_fsjlj_exceptions,
4171+
options::OPT_fseh_exceptions,
4172+
options::OPT_fdwarf_exceptions);
4173+
if (A) {
4174+
const Option &Opt = A->getOption();
4175+
if (Opt.matches(options::OPT_fsjlj_exceptions))
4176+
CmdArgs.push_back("-fsjlj-exceptions");
4177+
if (Opt.matches(options::OPT_fseh_exceptions))
4178+
CmdArgs.push_back("-fseh-exceptions");
4179+
if (Opt.matches(options::OPT_fdwarf_exceptions))
4180+
CmdArgs.push_back("-fdwarf-exceptions");
4181+
} else {
4182+
switch(getToolChain().GetExceptionModel(Args)) {
4183+
default:
4184+
break;
4185+
case llvm::ExceptionHandling::DwarfCFI:
4186+
CmdArgs.push_back("-fdwarf-exceptions");
4187+
break;
4188+
case llvm::ExceptionHandling::SjLj:
4189+
CmdArgs.push_back("-fsjlj-exceptions");
4190+
break;
4191+
case llvm::ExceptionHandling::WinEH:
4192+
CmdArgs.push_back("-fseh-exceptions");
4193+
break;
4194+
}
4195+
}
41724196

41734197
// C++ "sane" operator new.
41744198
if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,

lib/Driver/ToolChains/Darwin.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ bool MachO::IsUnwindTablesDefault(const ArgList &Args) const {
18811881
// Unwind tables are not emitted if -fno-exceptions is supplied (except when
18821882
// targeting x86_64).
18831883
return getArch() == llvm::Triple::x86_64 ||
1884-
(!UseSjLjExceptions(Args) &&
1884+
(GetExceptionModel(Args) != llvm::ExceptionHandling::SjLj &&
18851885
Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
18861886
true));
18871887
}
@@ -1892,15 +1892,18 @@ bool MachO::UseDwarfDebugFlags() const {
18921892
return false;
18931893
}
18941894

1895-
bool Darwin::UseSjLjExceptions(const ArgList &Args) const {
1895+
llvm::ExceptionHandling Darwin::GetExceptionModel(const ArgList &Args) const {
18961896
// Darwin uses SjLj exceptions on ARM.
18971897
if (getTriple().getArch() != llvm::Triple::arm &&
18981898
getTriple().getArch() != llvm::Triple::thumb)
1899-
return false;
1899+
return llvm::ExceptionHandling::None;
19001900

19011901
// Only watchOS uses the new DWARF/Compact unwinding method.
19021902
llvm::Triple Triple(ComputeLLVMTriple(Args));
1903-
return !Triple.isWatchABI();
1903+
if(Triple.isWatchABI())
1904+
return llvm::ExceptionHandling::DwarfCFI;
1905+
1906+
return llvm::ExceptionHandling::SjLj;
19041907
}
19051908

19061909
bool Darwin::SupportsEmbeddedBitcode() const {

lib/Driver/ToolChains/Darwin.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,9 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
247247

248248
bool UseDwarfDebugFlags() const override;
249249

250-
bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const override {
251-
return false;
250+
llvm::ExceptionHandling
251+
GetExceptionModel(const llvm::opt::ArgList &Args) const override {
252+
return llvm::ExceptionHandling::None;
252253
}
253254

254255
/// }
@@ -455,7 +456,8 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
455456

456457
void CheckObjCARC() const override;
457458

458-
bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const override;
459+
llvm::ExceptionHandling GetExceptionModel(
460+
const llvm::opt::ArgList &Args) const override;
459461

460462
bool SupportsEmbeddedBitcode() const override;
461463

lib/Driver/ToolChains/FreeBSD.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,18 @@ Tool *FreeBSD::buildAssembler() const {
359359

360360
Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
361361

362-
bool FreeBSD::UseSjLjExceptions(const ArgList &Args) const {
362+
llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const {
363363
// FreeBSD uses SjLj exceptions on ARM oabi.
364364
switch (getTriple().getEnvironment()) {
365365
case llvm::Triple::GNUEABIHF:
366366
case llvm::Triple::GNUEABI:
367367
case llvm::Triple::EABI:
368-
return false;
369-
368+
return llvm::ExceptionHandling::None;
370369
default:
371-
return (getTriple().getArch() == llvm::Triple::arm ||
372-
getTriple().getArch() == llvm::Triple::thumb);
370+
if (getTriple().getArch() == llvm::Triple::arm ||
371+
getTriple().getArch() == llvm::Triple::thumb)
372+
return llvm::ExceptionHandling::SjLj;
373+
return llvm::ExceptionHandling::None;
373374
}
374375
}
375376

lib/Driver/ToolChains/FreeBSD.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
6666
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
6767
llvm::opt::ArgStringList &CmdArgs) const override;
6868

69-
bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const override;
69+
llvm::ExceptionHandling GetExceptionModel(
70+
const llvm::opt::ArgList &Args) const override;
7071
bool isPIEDefault() const override;
7172
SanitizerMask getSupportedSanitizers() const override;
7273
unsigned GetDefaultDwarfVersion() const override { return 2; }

lib/Driver/ToolChains/MinGW.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,11 @@ bool toolchains::MinGW::isPICDefaultForced() const {
367367
return getArch() == llvm::Triple::x86_64;
368368
}
369369

370-
bool toolchains::MinGW::UseSEHExceptions() const {
371-
return getArch() == llvm::Triple::x86_64;
370+
llvm::ExceptionHandling
371+
toolchains::MinGW::GetExceptionModel(const ArgList &Args) const {
372+
if (getArch() == llvm::Triple::x86_64)
373+
return llvm::ExceptionHandling::WinEH;
374+
return llvm::ExceptionHandling::DwarfCFI;
372375
}
373376

374377
void toolchains::MinGW::AddCudaIncludeArgs(const ArgList &DriverArgs,

lib/Driver/ToolChains/MinGW.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ class LLVM_LIBRARY_VISIBILITY MinGW : public ToolChain {
6464
bool isPICDefault() const override;
6565
bool isPIEDefault() const override;
6666
bool isPICDefaultForced() const override;
67-
bool UseSEHExceptions() const;
67+
68+
llvm::ExceptionHandling GetExceptionModel(
69+
const llvm::opt::ArgList &Args) const override;
6870

6971
void
7072
AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,

lib/Driver/ToolChains/NetBSD.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,15 @@ void NetBSD::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
416416
"", DriverArgs, CC1Args);
417417
}
418418

419+
llvm::ExceptionHandling NetBSD::GetExceptionModel(const ArgList &Args) const {
420+
// NetBSD uses Dwarf exceptions on ARM.
421+
llvm::Triple::ArchType TArch = getTriple().getArch();
422+
if (TArch == llvm::Triple::arm || TArch == llvm::Triple::armeb ||
423+
TArch == llvm::Triple::thumb || TArch == llvm::Triple::thumbeb)
424+
return llvm::ExceptionHandling::DwarfCFI;
425+
return llvm::ExceptionHandling::None;
426+
}
427+
419428
SanitizerMask NetBSD::getSupportedSanitizers() const {
420429
const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
421430
const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;

lib/Driver/ToolChains/NetBSD.h

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
6969
return true;
7070
}
7171

72+
llvm::ExceptionHandling GetExceptionModel(
73+
const llvm::opt::ArgList &Args) const override;
74+
7275
SanitizerMask getSupportedSanitizers() const override;
7376

7477
protected:

lib/Frontend/CompilerInvocation.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,18 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
21412141
Opts.Exceptions = Args.hasArg(OPT_fexceptions);
21422142
Opts.ObjCExceptions = Args.hasArg(OPT_fobjc_exceptions);
21432143
Opts.CXXExceptions = Args.hasArg(OPT_fcxx_exceptions);
2144-
Opts.SjLjExceptions = Args.hasArg(OPT_fsjlj_exceptions);
2144+
2145+
// Handle exception personalities
2146+
Arg *A = Args.getLastArg(options::OPT_fsjlj_exceptions,
2147+
options::OPT_fseh_exceptions,
2148+
options::OPT_fdwarf_exceptions);
2149+
if (A) {
2150+
const Option &Opt = A->getOption();
2151+
Opts.SjLjExceptions = Opt.matches(options::OPT_fsjlj_exceptions);
2152+
Opts.SEHExceptions = Opt.matches(options::OPT_fseh_exceptions);
2153+
Opts.DWARFExceptions = Opt.matches(options::OPT_fdwarf_exceptions);
2154+
}
2155+
21452156
Opts.ExternCNoUnwind = Args.hasArg(OPT_fexternc_nounwind);
21462157
Opts.TraditionalCPP = Args.hasArg(OPT_traditional_cpp);
21472158

0 commit comments

Comments
 (0)