diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -404,7 +404,7 @@ /// master flag, -fexceptions and also language specific flags to enable/disable /// C++ and Objective-C exceptions. This makes it possible to for example /// disable C++ exceptions but enable Objective-C exceptions. -static void addExceptionArgs(const ArgList &Args, types::ID InputType, +static bool addExceptionArgs(const ArgList &Args, types::ID InputType, const ToolChain &TC, bool KernelOrKext, const ObjCRuntime &objcRuntime, ArgStringList &CmdArgs) { @@ -419,7 +419,7 @@ Args.ClaimAllArgs(options::OPT_fno_objc_exceptions); Args.ClaimAllArgs(options::OPT_fcxx_exceptions); Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions); - return; + return false; } // See if the user explicitly enabled exceptions. @@ -462,6 +462,7 @@ if (EH) CmdArgs.push_back("-fexceptions"); + return EH; } static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC, @@ -4971,14 +4972,15 @@ // This is a coarse approximation of what llvm-gcc actually does, both // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more // complicated ways. - bool AsynchronousUnwindTables = + bool UnwindTables = Args.hasFlag(options::OPT_fasynchronous_unwind_tables, options::OPT_fno_asynchronous_unwind_tables, (TC.IsUnwindTablesDefault(Args) || TC.getSanitizerArgs().needsUnwindTables()) && !Freestanding); - if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables, - AsynchronousUnwindTables)) + UnwindTables = Args.hasFlag(options::OPT_funwind_tables, + options::OPT_fno_unwind_tables, UnwindTables); + if (UnwindTables) CmdArgs.push_back("-munwind-tables"); // Prepare `-aux-target-cpu` and `-aux-target-feature` unless @@ -6039,8 +6041,9 @@ CmdArgs.push_back("-fapplication-extension"); // Handle GCC-style exception args. + bool EH = false; if (!C.getDriver().IsCLMode()) - addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs); + EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs); // Handle exception personalities Arg *A = Args.getLastArg( @@ -6600,6 +6603,10 @@ !TC.getTriple().isAndroid() && TC.useIntegratedAs())) CmdArgs.push_back("-faddrsig"); + if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) && + (EH || UnwindTables || DebugInfoKind != codegenoptions::NoDebugInfo)) + CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1"); + if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) { std::string Str = A->getAsString(Args); if (!TC.getTriple().isOSBinFormatELF()) diff --git a/clang/test/Preprocessor/unwind-tables.c b/clang/test/Preprocessor/unwind-tables.c new file mode 100644 --- /dev/null +++ b/clang/test/Preprocessor/unwind-tables.c @@ -0,0 +1,10 @@ +// RUN: %clang %s -dM -E -target x86_64-windows | FileCheck %s --check-prefix=NO +// RUN: %clang %s -dM -E -target x86_64 -fno-asynchronous-unwind-tables | FileCheck %s --check-prefix=NO + +// RUN: %clang %s -dM -E -target x86_64 | FileCheck %s +// RUN: %clang %s -dM -E -target aarch64-apple-darwin | FileCheck %s +// RUN: %clang %s -dM -E -target x86_64 -fno-asynchronous-unwind-tables -g | FileCheck %s +// RUN: %clang %s -dM -E -target x86_64 -fno-asynchronous-unwind-tables -fexceptions | FileCheck %s + +// NO-NOT: #define __GCC_HAVE_DWARF2_CFI_ASM +// CHECK: #define __GCC_HAVE_DWARF2_CFI_ASM 1