]> granicus.if.org Git - clang/commitdiff
Take DW_AT_comp_dir from $PWD when it's present and starts with a '/'. This is
authorNick Lewycky <nicholas@mxc.ca>
Fri, 21 Oct 2011 02:32:14 +0000 (02:32 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Fri, 21 Oct 2011 02:32:14 +0000 (02:32 +0000)
closer to what GCC does, except that GCC also checks that the inodes for $PWD
and '.' match.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@142633 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Driver/CC1Options.td
include/clang/Frontend/CodeGenOptions.h
lib/CodeGen/CGDebugInfo.cpp
lib/Driver/Tools.cpp
lib/Frontend/CompilerInvocation.cpp
test/CodeGen/debug-info-compilation-dir.c [new file with mode: 0644]
test/Driver/debug.c [new file with mode: 0644]

index 710072510954c801c0456f540d45df2426d94f7e..4b39d6ec550cff7231a7775377eabb3f280154a2 100644 (file)
@@ -110,6 +110,8 @@ def disable_llvm_verifier : Flag<"-disable-llvm-verifier">,
   HelpText<"Don't run the LLVM IR verifier pass">;
 def disable_red_zone : Flag<"-disable-red-zone">,
   HelpText<"Do not emit code that uses the red zone.">;
+def fdebug_compilation_dir : Separate<"-fdebug-compilation-dir">,
+  HelpText<"The compilation directory to embed in the debug info.">;
 def dwarf_debug_flags : Separate<"-dwarf-debug-flags">,
   HelpText<"The string to embed in the Dwarf debug flags record.">;
 def fforbid_guard_variables : Flag<"-fforbid-guard-variables">,
index ecfe49fa4600a8ad52e537b58b42942fdc0bbbc8..5b698889f6eda3de5d4512fd0365647252cd8c3a 100644 (file)
@@ -115,6 +115,9 @@ public:
   /// Enable additional debugging information.
   std::string DebugPass;
 
+  /// The string to embed in debug information as the current working directory.
+  std::string DebugCompilationDir;
+
   /// The string to embed in the debug information for the compile unit, if
   /// non-empty.
   std::string DwarfDebugFlags;
index d4b566bb20f73edc252257b2dcffe30ed25df2ac..2e1d12d8ce8b4681c82330d290c8f257137205f3 100644 (file)
@@ -250,6 +250,9 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
 }
 
 StringRef CGDebugInfo::getCurrentDirname() {
+  if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
+    return CGM.getCodeGenOpts().DebugCompilationDir;
+
   if (!CWDName.empty())
     return CWDName;
   llvm::SmallString<256> CWD;
index 2dcb61bfb2437f5e11c5f3f60fc1237dab2fe7ae..ad32bc7d1b4e1a03ba6114afacabe015a1b91259 100644 (file)
@@ -1625,6 +1625,16 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   if (ShouldDisableDwarfDirectory(Args, getToolChain()))
     CmdArgs.push_back("-fno-dwarf-directory-asm");
 
+  if (const char *pwd = ::getenv("PWD")) {
+    // GCC also verifies that stat(pwd) and stat(".") have the same inode
+    // number. Not doing those because stats are slow, but we could.
+    if (pwd[0] == '/') {
+      std::string CompDir = pwd;
+      CmdArgs.push_back("-fdebug-compilation-dir");
+      CmdArgs.push_back(Args.MakeArgString(CompDir));
+    }
+  }
+
   if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
     CmdArgs.push_back("-ftemplate-depth");
     CmdArgs.push_back(A->getValue(Args));
index 05d2e692e5553a27821e04fa9d0f54a02cba52d9..76ceb9b0a5e3caf0e67b1119ec0623999d17fc36 100644 (file)
@@ -131,6 +131,10 @@ static void CodeGenOptsToArgs(const CodeGenOptions &Opts,
     Res.push_back("-disable-llvm-optzns");
   if (Opts.DisableRedZone)
     Res.push_back("-disable-red-zone");
+  if (!Opts.DebugCompilationDir.empty()) {
+    Res.push_back("-fdebug-compilation-dir");
+    Res.push_back(Opts.DebugCompilationDir);
+  }
   if (!Opts.DwarfDebugFlags.empty()) {
     Res.push_back("-dwarf-debug-flags");
     Res.push_back(Opts.DwarfDebugFlags);
@@ -1071,6 +1075,7 @@ static void ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
   Opts.EmitGcovArcs = Args.hasArg(OPT_femit_coverage_data);
   Opts.EmitGcovNotes = Args.hasArg(OPT_femit_coverage_notes);
   Opts.CoverageFile = Args.getLastArgValue(OPT_coverage_file);
+  Opts.DebugCompilationDir = Args.getLastArgValue(OPT_fdebug_compilation_dir);
 
   if (Arg *A = Args.getLastArg(OPT_fobjc_dispatch_method_EQ)) {
     StringRef Name = A->getValue(Args);
diff --git a/test/CodeGen/debug-info-compilation-dir.c b/test/CodeGen/debug-info-compilation-dir.c
new file mode 100644 (file)
index 0000000..4ffa155
--- /dev/null
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -fdebug-compilation-dir /nonsense -emit-llvm -g %s -o - | \
+// RUN:   grep nonsense
+
+// RUN: %clang_cc1 -emit-llvm -g %s -o - | grep %S
diff --git a/test/Driver/debug.c b/test/Driver/debug.c
new file mode 100644 (file)
index 0000000..2ed8921
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: %clang -### -g %s -c 2>&1 | grep '"-fdebug-compilation-dir" "'%S'"'
+// RUN: PWD=/foo %clang -### -g %s -c 2>&1 | grep '"-fdebug-compilation-dir" "/foo"'
+
+// This test uses grep instead of FileCheck so that we get %S -> dirname
+// substitution.
+
+// "PWD=/foo gcc" wouldn't necessarily work. You would need to pick a different
+// path to the same directory (try a symlink).