Skip to content

Commit 4388b56

Browse files
committed
Refactor unwind table driver interface to expose default level. NFC.
1 parent fe7244f commit 4388b56

19 files changed

+95
-53
lines changed

clang/include/clang/Driver/ToolChain.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ class ToolChain {
108108
UNW_Libgcc
109109
};
110110

111+
enum class UnwindTableLevel {
112+
None,
113+
Synchronous,
114+
Asynchronous,
115+
};
116+
111117
enum RTTIMode {
112118
RM_Enabled,
113119
RM_Disabled,
@@ -495,9 +501,9 @@ class ToolChain {
495501
/// Returns true if gcov instrumentation (-fprofile-arcs or --coverage) is on.
496502
static bool needsGCovInstrumentation(const llvm::opt::ArgList &Args);
497503

498-
/// IsUnwindTablesDefault - Does this tool chain use -funwind-tables
499-
/// by default.
500-
virtual bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const;
504+
/// How detailed should the unwind tables be by default.
505+
virtual UnwindTableLevel
506+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const;
501507

502508
/// Test whether this toolchain supports outline atomics by default.
503509
virtual bool

clang/lib/Driver/ToolChain.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ std::string ToolChain::getInputFilename(const InputInfo &Input) const {
287287
return Input.getFilename();
288288
}
289289

290-
bool ToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
291-
return false;
290+
ToolChain::UnwindTableLevel
291+
ToolChain::getDefaultUnwindTableLevel(const ArgList &Args) const {
292+
return UnwindTableLevel::None;
292293
}
293294

294295
Tool *ToolChain::getClang() const {

clang/lib/Driver/ToolChains/Clang.cpp

+20-11
Original file line numberDiff line numberDiff line change
@@ -5461,17 +5461,26 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
54615461
// -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
54625462
// complicated ways.
54635463
auto SanitizeArgs = TC.getSanitizerArgs(Args);
5464-
bool AsyncUnwindTables = Args.hasFlag(
5465-
options::OPT_fasynchronous_unwind_tables,
5466-
options::OPT_fno_asynchronous_unwind_tables,
5467-
(TC.IsUnwindTablesDefault(Args) || SanitizeArgs.needsUnwindTables()) &&
5468-
!Freestanding);
5469-
bool UnwindTables = Args.hasFlag(options::OPT_funwind_tables,
5470-
options::OPT_fno_unwind_tables, false);
5471-
if (AsyncUnwindTables)
5472-
CmdArgs.push_back("-funwind-tables=2");
5473-
else if (UnwindTables)
5464+
auto UnwindTables = TC.getDefaultUnwindTableLevel(Args);
5465+
5466+
if (Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
5467+
options::OPT_fno_asynchronous_unwind_tables,
5468+
SanitizeArgs.needsUnwindTables()) &&
5469+
!Freestanding)
5470+
UnwindTables = ToolChain::UnwindTableLevel::Asynchronous;
5471+
else if (Args.hasFlag(options::OPT_funwind_tables,
5472+
options::OPT_fno_unwind_tables, false))
5473+
UnwindTables = ToolChain::UnwindTableLevel::Synchronous;
5474+
else if (Args.hasFlag(options::OPT_fno_unwind_tables,
5475+
options::OPT_fno_asynchronous_unwind_tables,
5476+
options::OPT_funwind_tables, false) || Freestanding)
5477+
UnwindTables = ToolChain::UnwindTableLevel::None;
5478+
5479+
5480+
if (UnwindTables == ToolChain::UnwindTableLevel::Synchronous)
54745481
CmdArgs.push_back("-funwind-tables=1");
5482+
else if (UnwindTables == ToolChain::UnwindTableLevel::Asynchronous)
5483+
CmdArgs.push_back("-funwind-tables=2");
54755484

54765485
// Prepare `-aux-target-cpu` and `-aux-target-feature` unless
54775486
// `--gpu-use-aux-triple-only` is specified.
@@ -7293,7 +7302,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
72937302
CmdArgs.push_back("-faddrsig");
72947303

72957304
if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) &&
7296-
(EH || AsyncUnwindTables || UnwindTables ||
7305+
(EH || UnwindTables != ToolChain::UnwindTableLevel::None ||
72977306
DebugInfoKind != codegenoptions::NoDebugInfo))
72987307
CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1");
72997308

clang/lib/Driver/ToolChains/CrossWindows.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ CrossWindowsToolChain::CrossWindowsToolChain(const Driver &D,
213213
const llvm::opt::ArgList &Args)
214214
: Generic_GCC(D, T, Args) {}
215215

216-
bool CrossWindowsToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
216+
ToolChain::UnwindTableLevel
217+
CrossWindowsToolChain::getDefaultUnwindTableLevel(const ArgList &Args) const {
217218
// FIXME: all non-x86 targets need unwind tables, however, LLVM currently does
218219
// not know how to emit them.
219-
return getArch() == llvm::Triple::x86_64;
220+
return getArch() == llvm::Triple::x86_64 ? UnwindTableLevel::Asynchronous : UnwindTableLevel::None;
220221
}
221222

222223
bool CrossWindowsToolChain::isPICDefault() const {

clang/lib/Driver/ToolChains/CrossWindows.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class LLVM_LIBRARY_VISIBILITY CrossWindowsToolChain : public Generic_GCC {
5555
const llvm::opt::ArgList &Args);
5656

5757
bool IsIntegratedAssemblerDefault() const override { return true; }
58-
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
58+
UnwindTableLevel
59+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
5960
bool isPICDefault() const override;
6061
bool isPIEDefault(const llvm::opt::ArgList &Args) const override;
6162
bool isPICDefaultForced() const override;

clang/lib/Driver/ToolChains/Darwin.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -2925,13 +2925,16 @@ Darwin::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
29252925
return DAL;
29262926
}
29272927

2928-
bool MachO::IsUnwindTablesDefault(const ArgList &Args) const {
2928+
ToolChain::UnwindTableLevel MachO::getDefaultUnwindTableLevel(const ArgList &Args) const {
29292929
// Unwind tables are not emitted if -fno-exceptions is supplied (except when
29302930
// targeting x86_64).
2931-
return getArch() == llvm::Triple::x86_64 ||
2932-
(GetExceptionModel(Args) != llvm::ExceptionHandling::SjLj &&
2933-
Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
2934-
true));
2931+
if (getArch() == llvm::Triple::x86_64 ||
2932+
(GetExceptionModel(Args) != llvm::ExceptionHandling::SjLj &&
2933+
Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
2934+
true)))
2935+
return UnwindTableLevel::Asynchronous;
2936+
2937+
return UnwindTableLevel::None;
29352938
}
29362939

29372940
bool MachO::UseDwarfDebugFlags() const {

clang/lib/Driver/ToolChains/Darwin.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
254254

255255
bool UseObjCMixedDispatch() const override { return true; }
256256

257-
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
257+
UnwindTableLevel
258+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
258259

259260
RuntimeLibType GetDefaultRuntimeLibType() const override {
260261
return ToolChain::RLT_CompilerRT;

clang/lib/Driver/ToolChains/FreeBSD.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,10 @@ llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const {
473473

474474
bool FreeBSD::HasNativeLLVMSupport() const { return true; }
475475

476-
bool FreeBSD::IsUnwindTablesDefault(const ArgList &Args) const { return true; }
476+
ToolChain::UnwindTableLevel
477+
FreeBSD::getDefaultUnwindTableLevel(const ArgList &Args) const {
478+
return UnwindTableLevel::Asynchronous;
479+
}
477480

478481
bool FreeBSD::isPIEDefault(const llvm::opt::ArgList &Args) const {
479482
return getSanitizerArgs(Args).requiresPIE();

clang/lib/Driver/ToolChains/FreeBSD.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
7373

7474
llvm::ExceptionHandling
7575
GetExceptionModel(const llvm::opt::ArgList &Args) const override;
76-
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
76+
UnwindTableLevel
77+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
7778
bool isPIEDefault(const llvm::opt::ArgList &Args) const override;
7879
SanitizerMask getSupportedSanitizers() const override;
7980
unsigned GetDefaultDwarfVersion() const override;

clang/lib/Driver/ToolChains/Fuchsia.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ class LLVM_LIBRARY_VISIBILITY Fuchsia : public ToolChain {
5050
CXXStdlibType GetDefaultCXXStdlibType() const override {
5151
return ToolChain::CST_Libcxx;
5252
}
53-
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override {
54-
return true;
53+
UnwindTableLevel
54+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override {
55+
return UnwindTableLevel::Asynchronous;
5556
}
5657
bool isPICDefault() const override { return false; }
5758
bool isPIEDefault(const llvm::opt::ArgList &Args) const override {

clang/lib/Driver/ToolChains/Gnu.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -2827,7 +2827,8 @@ void Generic_GCC::printVerboseInfo(raw_ostream &OS) const {
28272827
RocmInstallation.print(OS);
28282828
}
28292829

2830-
bool Generic_GCC::IsUnwindTablesDefault(const ArgList &Args) const {
2830+
ToolChain::UnwindTableLevel
2831+
Generic_GCC::getDefaultUnwindTableLevel(const ArgList &Args) const {
28312832
switch (getArch()) {
28322833
case llvm::Triple::aarch64:
28332834
case llvm::Triple::ppc:
@@ -2836,9 +2837,9 @@ bool Generic_GCC::IsUnwindTablesDefault(const ArgList &Args) const {
28362837
case llvm::Triple::ppc64le:
28372838
case llvm::Triple::x86:
28382839
case llvm::Triple::x86_64:
2839-
return true;
2840+
return UnwindTableLevel::Asynchronous;
28402841
default:
2841-
return false;
2842+
return UnwindTableLevel::None;
28422843
}
28432844
}
28442845

clang/lib/Driver/ToolChains/Gnu.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
296296

297297
void printVerboseInfo(raw_ostream &OS) const override;
298298

299-
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
299+
UnwindTableLevel
300+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
300301
bool isPICDefault() const override;
301302
bool isPIEDefault(const llvm::opt::ArgList &Args) const override;
302303
bool isPICDefaultForced() const override;

clang/lib/Driver/ToolChains/MSVC.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -450,16 +450,20 @@ bool MSVCToolChain::IsIntegratedAssemblerDefault() const {
450450
return true;
451451
}
452452

453-
bool MSVCToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
453+
ToolChain::UnwindTableLevel
454+
MSVCToolChain::getDefaultUnwindTableLevel(const ArgList &Args) const {
454455
// Don't emit unwind tables by default for MachO targets.
455456
if (getTriple().isOSBinFormatMachO())
456-
return false;
457+
return UnwindTableLevel::None;
457458

458459
// All non-x86_32 Windows targets require unwind tables. However, LLVM
459460
// doesn't know how to generate them for all targets, so only enable
460461
// the ones that are actually implemented.
461-
return getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::arm ||
462-
getArch() == llvm::Triple::thumb || getArch() == llvm::Triple::aarch64;
462+
if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::arm ||
463+
getArch() == llvm::Triple::thumb || getArch() == llvm::Triple::aarch64)
464+
return UnwindTableLevel::Asynchronous;
465+
466+
return UnwindTableLevel::None;
463467
}
464468

465469
bool MSVCToolChain::isPICDefault() const {

clang/lib/Driver/ToolChains/MSVC.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class LLVM_LIBRARY_VISIBILITY MSVCToolChain : public ToolChain {
5151
Action::OffloadKind DeviceOffloadKind) const override;
5252

5353
bool IsIntegratedAssemblerDefault() const override;
54-
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
54+
UnwindTableLevel
55+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
5556
bool isPICDefault() const override;
5657
bool isPIEDefault(const llvm::opt::ArgList &Args) const override;
5758
bool isPICDefaultForced() const override;

clang/lib/Driver/ToolChains/MinGW.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,19 @@ bool toolchains::MinGW::HasNativeLLVMSupport() const {
493493
return NativeLLVMSupport;
494494
}
495495

496-
bool toolchains::MinGW::IsUnwindTablesDefault(const ArgList &Args) const {
496+
ToolChain::UnwindTableLevel
497+
toolchains::MinGW::getDefaultUnwindTableLevel(const ArgList &Args) const {
497498
Arg *ExceptionArg = Args.getLastArg(options::OPT_fsjlj_exceptions,
498499
options::OPT_fseh_exceptions,
499500
options::OPT_fdwarf_exceptions);
500501
if (ExceptionArg &&
501502
ExceptionArg->getOption().matches(options::OPT_fseh_exceptions))
502-
return true;
503-
return getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::arm ||
504-
getArch() == llvm::Triple::thumb || getArch() == llvm::Triple::aarch64;
503+
return UnwindTableLevel::Asynchronous;
504+
505+
if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::arm ||
506+
getArch() == llvm::Triple::thumb || getArch() == llvm::Triple::aarch64)
507+
return UnwindTableLevel::Asynchronous;
508+
return UnwindTableLevel::None;
505509
}
506510

507511
bool toolchains::MinGW::isPICDefault() const {

clang/lib/Driver/ToolChains/MinGW.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class LLVM_LIBRARY_VISIBILITY MinGW : public ToolChain {
6666
bool HasNativeLLVMSupport() const override;
6767

6868
bool IsIntegratedAssemblerDefault() const override;
69-
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
69+
UnwindTableLevel
70+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
7071
bool isPICDefault() const override;
7172
bool isPIEDefault(const llvm::opt::ArgList &Args) const override;
7273
bool isPICDefaultForced() const override;

clang/lib/Driver/ToolChains/NetBSD.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ class LLVM_LIBRARY_VISIBILITY NetBSD : public Generic_ELF {
6565
const llvm::opt::ArgList &DriverArgs,
6666
llvm::opt::ArgStringList &CC1Args) const override;
6767

68-
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override {
69-
return true;
68+
UnwindTableLevel
69+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override {
70+
return UnwindTableLevel::Asynchronous;
7071
}
7172

7273
llvm::ExceptionHandling GetExceptionModel(

clang/lib/Driver/ToolChains/OpenBSD.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,12 @@ Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
363363

364364
bool OpenBSD::HasNativeLLVMSupport() const { return true; }
365365

366-
bool OpenBSD::IsUnwindTablesDefault(const ArgList &Args) const {
367-
switch (getArch()) {
368-
case llvm::Triple::arm:
369-
return false;
370-
default:
371-
return true;
372-
}
366+
ToolChain::UnwindTableLevel
367+
OpenBSD::getDefaultUnwindTableLevel(const ArgList &Args) const {
368+
switch (getArch()) {
369+
case llvm::Triple::arm:
370+
return UnwindTableLevel::None;
371+
default:
372+
return UnwindTableLevel::Asynchronous;
373+
}
373374
}

clang/lib/Driver/ToolChains/OpenBSD.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ class LLVM_LIBRARY_VISIBILITY OpenBSD : public Generic_ELF {
8282
std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component,
8383
FileType Type = ToolChain::FT_Static) const override;
8484

85-
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
85+
UnwindTableLevel
86+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
8687

8788
LangOptions::StackProtectorMode
8889
GetDefaultStackProtectorLevel(bool KernelOrKext) const override {

0 commit comments

Comments
 (0)