[PATCH] D97743: Define __GCC_HAVE_DWARF2_CFI_ASM to 1 on ELF/Mach-O if CC1 -munwind-tables is specified

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 1 16:08:08 PST 2021


MaskRay created this revision.
MaskRay added reviewers: dexonsmith, jansvoboda11.
MaskRay requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

  % diff <(gcc -fno-asynchronous-unwind-tables -dM -E a.c) <(gcc -dM -E a.c)
  130a131
  > #define __GCC_HAVE_DWARF2_CFI_ASM 1

This macro is useful because code can decide whether inline asm should include `.cfi_*` directives.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97743

Files:
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/Preprocessor/unwind-tables.c


Index: clang/test/Preprocessor/unwind-tables.c
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/unwind-tables.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 %s -verify -munwind-tables -DCFI_ASM -triple x86_64-unknown-elf
+// RUN: %clang_cc1 %s -verify -munwind-tables -DCFI_ASM -triple aarch64-apple-darwin
+// RUN: %clang_cc1 %s -verify -munwind-tables -triple x86_64-windows
+
+// expected-no-diagnostics
+
+#ifdef CFI_ASM
+  #if __GCC_HAVE_DWARF2_CFI_ASM != 1
+  #error "__GCC_HAVE_DWARF2_CFI_ASM not defined"
+  #endif
+#else
+  #ifdef __GCC_HAVE_DWARF2_CFI_ASM
+  #error "__GCC_HAVE_DWARF2_CFI_ASM defined"
+  #endif
+#endif
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -4045,8 +4045,8 @@
 
 static bool ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
                                   DiagnosticsEngine &Diags,
-                                  frontend::ActionKind Action,
-                                  const FrontendOptions &FrontendOpts) {
+                                  const FrontendOptions &FrontendOpts,
+                                  const llvm::Triple &T) {
   PreprocessorOptions *PreprocessorOpts = &Opts;
   bool Success = true;
 
@@ -4101,6 +4101,10 @@
       Opts.addMacroDef("__CET__=3");
   }
 
+  if (Args.hasArg(OPT_munwind_tables) &&
+      (T.isOSBinFormatELF() || T.isOSBinFormatMachO()))
+    Opts.addMacroDef("__GCC_HAVE_DWARF2_CFI_ASM=1");
+
   // Add macros from the command line.
   for (const auto *A : Args.filtered(OPT_D, OPT_U)) {
     if (A->getOption().matches(OPT_D))
@@ -4130,7 +4134,7 @@
   // Always avoid lexing editor placeholders when we're just running the
   // preprocessor as we never want to emit the
   // "editor placeholder in source file" error in PP only mode.
-  if (isStrictlyPreprocessorAction(Action))
+  if (isStrictlyPreprocessorAction(FrontendOpts.ProgramAction))
     Opts.LexEditorPlaceholders = false;
 
   return Success;
@@ -4325,8 +4329,7 @@
       !LangOpts.Sanitize.has(SanitizerKind::KernelMemory);
 
   ParsePreprocessorArgs(Res.getPreprocessorOpts(), Args, Diags,
-                        Res.getFrontendOpts().ProgramAction,
-                        Res.getFrontendOpts());
+                        Res.getFrontendOpts(), T);
   ParsePreprocessorOutputArgs(Res.getPreprocessorOutputOpts(), Args, Diags,
                               Res.getFrontendOpts().ProgramAction);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97743.327311.patch
Type: text/x-patch
Size: 2599 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210302/c762d48b/attachment.bin>


More information about the cfe-commits mailing list