[clang] 0604d86 - Darwin: introduce a global override for debug prefix map entries.

Adrian Prantl via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 16 08:36:47 PST 2022


Author: Adrian Prantl
Date: 2022-02-16T08:36:26-08:00
New Revision: 0604d86c07ab8a28b95df689aff4ddd26347f35f

URL: https://github.com/llvm/llvm-project/commit/0604d86c07ab8a28b95df689aff4ddd26347f35f
DIFF: https://github.com/llvm/llvm-project/commit/0604d86c07ab8a28b95df689aff4ddd26347f35f.diff

LOG: Darwin: introduce a global override for debug prefix map entries.

This patch adds a new Darwin clang driver environment variable in the
spirit of RC_DEBUG_OPTIONS, called RC_DEBUG_PREFIX_MAP, which allows a
meta build tool to add one additional -fdebug-prefix-map entry without
the knowledge of the build system.

rdar://85224675

Differential Revision: https://reviews.llvm.org/D119850

Added: 
    clang/test/Driver/darwin-debug-prefix-map.c
    clang/test/Driver/darwin-debug-prefix-map.s

Modified: 
    clang/include/clang/Driver/ToolChain.h
    clang/lib/Driver/ToolChains/Clang.cpp
    clang/lib/Driver/ToolChains/Darwin.cpp
    clang/lib/Driver/ToolChains/Darwin.h

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h
index bfc46af002657..adf1753e8d3a6 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -513,6 +513,9 @@ class ToolChain {
   /// compile unit information.
   virtual bool UseDwarfDebugFlags() const { return false; }
 
+  /// Add an additional -fdebug-prefix-map entry.
+  virtual std::string GetGlobalDebugPathRemapping() const { return {}; }
+  
   // Return the DWARF version to emit, in the absence of arguments
   // to the contrary.
   virtual unsigned GetDefaultDwarfVersion() const { return 5; }

diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 945f626977799..de289683596be 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -668,17 +668,24 @@ static void addDebugObjectName(const ArgList &Args, ArgStringList &CmdArgs,
 }
 
 /// Add a CC1 and CC1AS option to specify the debug file path prefix map.
-static void addDebugPrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) {
-  for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
-                                    options::OPT_fdebug_prefix_map_EQ)) {
-    StringRef Map = A->getValue();
+static void addDebugPrefixMapArg(const Driver &D, const ToolChain &TC,
+                                 const ArgList &Args, ArgStringList &CmdArgs) {
+  auto AddOneArg = [&](StringRef Map, StringRef Name) {
     if (!Map.contains('='))
-      D.Diag(diag::err_drv_invalid_argument_to_option)
-          << Map << A->getOption().getName();
+      D.Diag(diag::err_drv_invalid_argument_to_option) << Map << Name;
     else
       CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map));
+  };
+
+  for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
+                                    options::OPT_fdebug_prefix_map_EQ)) {
+    AddOneArg(A->getValue(), A->getOption().getName());
     A->claim();
   }
+  std::string GlobalRemapEntry = TC.GetGlobalDebugPathRemapping();
+  if (GlobalRemapEntry.empty())
+    return;
+  AddOneArg(GlobalRemapEntry, "environment");
 }
 
 /// Add a CC1 and CC1AS option to specify the macro file path prefix map.
@@ -5717,7 +5724,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   const char *DebugCompilationDir =
       addDebugCompDirArg(Args, CmdArgs, D.getVFS());
 
-  addDebugPrefixMapArg(D, Args, CmdArgs);
+  addDebugPrefixMapArg(D, TC, Args, CmdArgs);
 
   if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
                                options::OPT_ftemplate_depth_EQ)) {
@@ -7785,7 +7792,8 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
     DebugInfoKind = (WantDebug ? codegenoptions::DebugInfoConstructor
                                : codegenoptions::NoDebugInfo);
 
-    addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs);
+    addDebugPrefixMapArg(getToolChain().getDriver(), getToolChain(), Args,
+                         CmdArgs);
 
     // Set the AT_producer to the clang version when using the integrated
     // assembler on assembly source files.

diff  --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 3c408010b5b19..dc75b2b4621bb 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2843,6 +2843,12 @@ bool MachO::UseDwarfDebugFlags() const {
   return false;
 }
 
+std::string MachO::GetGlobalDebugPathRemapping() const {
+  if (const char *S = ::getenv("RC_DEBUG_PREFIX_MAP"))
+    return S;
+  return {};
+}
+
 llvm::ExceptionHandling Darwin::GetExceptionModel(const ArgList &Args) const {
   // Darwin uses SjLj exceptions on ARM.
   if (getTriple().getArch() != llvm::Triple::arm &&

diff  --git a/clang/lib/Driver/ToolChains/Darwin.h b/clang/lib/Driver/ToolChains/Darwin.h
index d487f8cd41ee9..a5f8e45640b5a 100644
--- a/clang/lib/Driver/ToolChains/Darwin.h
+++ b/clang/lib/Driver/ToolChains/Darwin.h
@@ -267,6 +267,7 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
   bool SupportsProfiling() const override;
 
   bool UseDwarfDebugFlags() const override;
+  std::string GetGlobalDebugPathRemapping() const override;
 
   llvm::ExceptionHandling
   GetExceptionModel(const llvm::opt::ArgList &Args) const override {

diff  --git a/clang/test/Driver/darwin-debug-prefix-map.c b/clang/test/Driver/darwin-debug-prefix-map.c
new file mode 100644
index 0000000000000..2a30ce5b79889
--- /dev/null
+++ b/clang/test/Driver/darwin-debug-prefix-map.c
@@ -0,0 +1,6 @@
+// RUN: env RC_DEBUG_PREFIX_MAP=old=new \
+// RUN:  %clang -target arm64-apple-darwin -### -c -g %s 2>&1 | FileCheck %s
+// RUN: env RC_DEBUG_PREFIX_MAP=illegal \
+// RUN:  %clang -target arm64-apple-darwin -### -c -g %s 2>&1 | FileCheck %s --check-prefix=ERR
+// CHECK: "-fdebug-prefix-map=old=new" 
+// ERR: invalid argument 'illegal'

diff  --git a/clang/test/Driver/darwin-debug-prefix-map.s b/clang/test/Driver/darwin-debug-prefix-map.s
new file mode 100644
index 0000000000000..2a30ce5b79889
--- /dev/null
+++ b/clang/test/Driver/darwin-debug-prefix-map.s
@@ -0,0 +1,6 @@
+// RUN: env RC_DEBUG_PREFIX_MAP=old=new \
+// RUN:  %clang -target arm64-apple-darwin -### -c -g %s 2>&1 | FileCheck %s
+// RUN: env RC_DEBUG_PREFIX_MAP=illegal \
+// RUN:  %clang -target arm64-apple-darwin -### -c -g %s 2>&1 | FileCheck %s --check-prefix=ERR
+// CHECK: "-fdebug-prefix-map=old=new" 
+// ERR: invalid argument 'illegal'


        


More information about the cfe-commits mailing list