<div dir="ltr">On 28 March 2013 10:24, Chad Rosier <span dir="ltr"><<a href="mailto:mcrosier@apple.com" target="_blank">mcrosier@apple.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div style="word-wrap:break-word">Hi Nick,<div>I've run into a bug with -fdebug-compilation-dir and I was hoping you could help me come up with a solution.</div><div>Specifically, could tell me how gcc behaves when stat(pwd) and stat(.) differ?  I'm running into this very case.  If</div>

<div>you need a test case, I'd be happy to provide one.  Also, do you know why gcc prefers ::getenv("PWD") as</div><div>opposed to something like getcwd()?</div></div></blockquote><div><br></div><div style>
Great questions! The comment in gcc/libiberty says "Use the PWD environment variable if it's set correctly, since this is faster and gives more uniform answers to the user."</div>
<div style><br></div><div style>When stat(pwd) and stat(.) differ in inode or dev, gcc calls getcwd(). Specifically, gcc checks for the presence of the PWD environment variable, that its value starts with a '/' character, that they can stat $PWD and stat ".", and that the inode and containing devices match. If any of those fail, it falls back to getcwd, complete with a loop to determine the path length (in the absence of MAXPATHLEN). If getcwd fails, the debug info will contain "." for the path.</div>

<div style><br></div><div style>Hope that helps!</div><div style><br></div><div style>Nick<br></div><div style><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div style="word-wrap:break-word"><span class=""><font color="#888888"><div><br></div><div> Chad</div></font></span><div><div class="h5"><div><br></div><div><br><div><div>On Oct 20, 2011, at 7:32 PM, Nick Lewycky <<a href="mailto:nicholas@mxc.ca" target="_blank">nicholas@mxc.ca</a>> wrote:</div>

<br><blockquote type="cite"><div style="letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Author: nicholas<br>Date: Thu Oct 20 21:32:14 2011<br>New Revision: 142633<br>

<br>URL:<span> </span><a href="http://llvm.org/viewvc/llvm-project?rev=142633&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=142633&view=rev</a><br>Log:<br>Take DW_AT_comp_dir from $PWD when it's present and starts with a '/'. This is<br>

closer to what GCC does, except that GCC also checks that the inodes for $PWD<br>and '.' match.<br><br>Added:<br>   cfe/trunk/test/CodeGen/debug-info-compilation-dir.c<br>   cfe/trunk/test/Driver/debug.c<br>Modified:<br>

   cfe/trunk/include/clang/Driver/CC1Options.td<br>   cfe/trunk/include/clang/Frontend/CodeGenOptions.h<br>   cfe/trunk/lib/CodeGen/CGDebugInfo.cpp<br>   cfe/trunk/lib/Driver/Tools.cpp<br>   cfe/trunk/lib/Frontend/CompilerInvocation.cpp<br>

<br>Modified: cfe/trunk/include/clang/Driver/CC1Options.td<br>URL:<span> </span><a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=142633&r1=142632&r2=142633&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=142633&r1=142632&r2=142633&view=diff</a><br>

==============================================================================<br>--- cfe/trunk/include/clang/Driver/CC1Options.td (original)<br>+++ cfe/trunk/include/clang/Driver/CC1Options.td Thu Oct 20 21:32:14 2011<br>

@@ -110,6 +110,8 @@<br>  HelpText<"Don't run the LLVM IR verifier pass">;<br>def disable_red_zone : Flag<"-disable-red-zone">,<br>  HelpText<"Do not emit code that uses the red zone.">;<br>

+def fdebug_compilation_dir : Separate<"-fdebug-compilation-dir">,<br>+  HelpText<"The compilation directory to embed in the debug info.">;<br>def dwarf_debug_flags : Separate<"-dwarf-debug-flags">,<br>

  HelpText<"The string to embed in the Dwarf debug flags record.">;<br>def fforbid_guard_variables : Flag<"-fforbid-guard-variables">,<br><br>Modified: cfe/trunk/include/clang/Frontend/CodeGenOptions.h<br>

URL:<span> </span><a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CodeGenOptions.h?rev=142633&r1=142632&r2=142633&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CodeGenOptions.h?rev=142633&r1=142632&r2=142633&view=diff</a><br>

==============================================================================<br>--- cfe/trunk/include/clang/Frontend/CodeGenOptions.h (original)<br>+++ cfe/trunk/include/clang/Frontend/CodeGenOptions.h Thu Oct 20 21:32:14 2011<br>

@@ -115,6 +115,9 @@<br>  /// Enable additional debugging information.<br>  std::string DebugPass;<br><br>+  /// The string to embed in debug information as the current working directory.<br>+  std::string DebugCompilationDir;<br>

+<br>  /// The string to embed in the debug information for the compile unit, if<br>  /// non-empty.<br>  std::string DwarfDebugFlags;<br><br>Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp<br>URL:<span> </span><a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=142633&r1=142632&r2=142633&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=142633&r1=142632&r2=142633&view=diff</a><br>

==============================================================================<br>--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp (original)<br>+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp Thu Oct 20 21:32:14 2011<br>@@ -250,6 +250,9 @@<br>

}<br><br>StringRef CGDebugInfo::getCurrentDirname() {<br>+  if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())<br>+    return CGM.getCodeGenOpts().DebugCompilationDir;<br>+<br>  if (!CWDName.empty())<br>    return CWDName;<br>

  llvm::SmallString<256> CWD;<br><br>Modified: cfe/trunk/lib/Driver/Tools.cpp<br>URL:<span> </span><a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=142633&r1=142632&r2=142633&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=142633&r1=142632&r2=142633&view=diff</a><br>

==============================================================================<br>--- cfe/trunk/lib/Driver/Tools.cpp (original)<br>+++ cfe/trunk/lib/Driver/Tools.cpp Thu Oct 20 21:32:14 2011<br>@@ -1625,6 +1625,16 @@<br>
  if (ShouldDisableDwarfDirectory(Args, getToolChain()))<br>
    CmdArgs.push_back("-fno-dwarf-directory-asm");<br><br>+  if (const char *pwd = ::getenv("PWD")) {<br>+    // GCC also verifies that stat(pwd) and stat(".") have the same inode<br>+    // number. Not doing those because stats are slow, but we could.<br>

+    if (pwd[0] == '/') {<br>+      std::string CompDir = pwd;<br>+      CmdArgs.push_back("-fdebug-compilation-dir");<br>+      CmdArgs.push_back(Args.MakeArgString(CompDir));<br>+    }<br>+  }<br>+<br>

  if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {<br>    CmdArgs.push_back("-ftemplate-depth");<br>    CmdArgs.push_back(A->getValue(Args));<br><br>Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp<br>

URL:<span> </span><a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=142633&r1=142632&r2=142633&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=142633&r1=142632&r2=142633&view=diff</a><br>

==============================================================================<br>--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)<br>+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Thu Oct 20 21:32:14 2011<br>

@@ -131,6 +131,10 @@<br>    Res.push_back("-disable-llvm-optzns");<br>  if (Opts.DisableRedZone)<br>    Res.push_back("-disable-red-zone");<br>+  if (!Opts.DebugCompilationDir.empty()) {<br>+    Res.push_back("-fdebug-compilation-dir");<br>

+    Res.push_back(Opts.DebugCompilationDir);<br>+  }<br>  if (!Opts.DwarfDebugFlags.empty()) {<br>    Res.push_back("-dwarf-debug-flags");<br>    Res.push_back(Opts.DwarfDebugFlags);<br>@@ -1071,6 +1075,7 @@<br>

  Opts.EmitGcovArcs = Args.hasArg(OPT_femit_coverage_data);<br>  Opts.EmitGcovNotes = Args.hasArg(OPT_femit_coverage_notes);<br>  Opts.CoverageFile = Args.getLastArgValue(OPT_coverage_file);<br>+  Opts.DebugCompilationDir = Args.getLastArgValue(OPT_fdebug_compilation_dir);<br>

<br>  if (Arg *A = Args.getLastArg(OPT_fobjc_dispatch_method_EQ)) {<br>    StringRef Name = A->getValue(Args);<br><br>Added: cfe/trunk/test/CodeGen/debug-info-compilation-dir.c<br>URL:<span> </span><a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/debug-info-compilation-dir.c?rev=142633&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/debug-info-compilation-dir.c?rev=142633&view=auto</a><br>

==============================================================================<br>--- cfe/trunk/test/CodeGen/debug-info-compilation-dir.c (added)<br>+++ cfe/trunk/test/CodeGen/debug-info-compilation-dir.c Thu Oct 20 21:32:14 2011<br>

@@ -0,0 +1,4 @@<br>+// RUN: %clang_cc1 -fdebug-compilation-dir /nonsense -emit-llvm -g %s -o - | \<br>+// RUN:   grep nonsense<br>+<br>+// RUN: %clang_cc1 -emit-llvm -g %s -o - | grep %S<br><br>Added: cfe/trunk/test/Driver/debug.c<br>

URL:<span> </span><a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/debug.c?rev=142633&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/debug.c?rev=142633&view=auto</a><br>

==============================================================================<br>--- cfe/trunk/test/Driver/debug.c (added)<br>+++ cfe/trunk/test/Driver/debug.c Thu Oct 20 21:32:14 2011<br>@@ -0,0 +1,8 @@<br>+// RUN: %clang -### -g %s -c 2>&1 | grep '"-fdebug-compilation-dir" "'%S'"'<br>

+// RUN: PWD=/foo %clang -### -g %s -c 2>&1 | grep '"-fdebug-compilation-dir" "/foo"'<br>+<br>+// This test uses grep instead of FileCheck so that we get %S -> dirname<br>+// substitution.<br>

+<br>+// "PWD=/foo gcc" wouldn't necessarily work. You would need to pick a different<br>+// path to the same directory (try a symlink).<br><br><br>_______________________________________________<br>cfe-commits mailing list<br>

<a href="mailto:cfe-commits@cs.uiuc.edu" target="_blank">cfe-commits@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a></div>

</blockquote></div><br></div></div></div></div><br>_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
<br></blockquote></div><br></div></div>