<div dir="ltr">This change appears to have broken a number of compiler-rt coverage tests, e.g. <a href="http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux/builds/17934/steps/test%20standalone%20compiler-rt/logs/stdio">in this run</a>. The source of the error appears to be that llvm-cov is now trying to use a relative path that's not relative to the directory it's in, though I'm not familiar enough with debuginfo/coverage to understand what an appropriate fix is. I've not yet reverted these changes to give you a chance to take a look.</div><br><div class="gmail_quote"><div dir="ltr">On Mon, Dec 3, 2018 at 9:58 AM Adrian Prantl via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: adrian<br>
Date: Mon Dec  3 09:55:27 2018<br>
New Revision: 348154<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=348154&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=348154&view=rev</a><br>
Log:<br>
Avoid emitting redundant or unusable directories in DIFile metadata entries.<br>
<br>
As discussed on llvm-dev recently, Clang currently emits redundant<br>
directories in DIFile entries, such as<br>
<br>
  .file      1 "/Volumes/Data/llvm" "/Volumes/Data/llvm/tools/clang/test/CodeGen/debug-info-abspath.c"<br>
<br>
This patch looks at any common prefix between the compilation<br>
directory and the (absolute) file path and strips the redundant<br>
part. More importantly it leaves the compilation directory empty if<br>
the two paths have no common prefix.<br>
<br>
After this patch the above entry is (assuming a compilation dir of "/Volumes/Data/llvm/_build"):<br>
<br>
  .file 1 "/Volumes/Data/llvm" "tools/clang/test/CodeGen/debug-info-abspath.c"<br>
<br>
When building the FileCheck binary with debug info, this patch makes<br>
the build artifacts ~1kb smaller.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D55085" rel="noreferrer" target="_blank">https://reviews.llvm.org/D55085</a><br>
<br>
Added:<br>
    cfe/trunk/test/CodeGen/debug-info-abspath.c<br>
Modified:<br>
    cfe/trunk/lib/CodeGen/CGDebugInfo.cpp<br>
    cfe/trunk/lib/CodeGen/CodeGenAction.cpp<br>
    cfe/trunk/test/CodeGen/debug-prefix-map.c<br>
    cfe/trunk/test/Modules/module-debuginfo-prefix.m<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=348154&r1=348153&r2=348154&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=348154&r1=348153&r2=348154&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp Mon Dec  3 09:55:27 2018<br>
@@ -181,8 +181,7 @@ void CGDebugInfo::setLocation(SourceLoca<br>
   SourceManager &SM = CGM.getContext().getSourceManager();<br>
   auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());<br>
   PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);<br>
-<br>
-  if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename())<br>
+  if (PCLoc.isInvalid() || Scope->getFile() == getOrCreateFile(CurLoc))<br>
     return;<br>
<br>
   if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {<br>
@@ -410,13 +409,13 @@ llvm::DIFile *CGDebugInfo::getOrCreateFi<br>
   SourceManager &SM = CGM.getContext().getSourceManager();<br>
   PresumedLoc PLoc = SM.getPresumedLoc(Loc);<br>
<br>
-  if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())<br>
+  StringRef FileName = PLoc.getFilename();<br>
+  if (PLoc.isInvalid() || FileName.empty())<br>
     // If the location is not valid then use main input file.<br>
     return getOrCreateMainFile();<br>
<br>
   // Cache the results.<br>
-  const char *fname = PLoc.getFilename();<br>
-  auto It = DIFileCache.find(fname);<br>
+  auto It = DIFileCache.find(FileName.data());<br>
<br>
   if (It != DIFileCache.end()) {<br>
     // Verify that the information still exists.<br>
@@ -431,11 +430,41 @@ llvm::DIFile *CGDebugInfo::getOrCreateFi<br>
   if (CSKind)<br>
     CSInfo.emplace(*CSKind, Checksum);<br>
<br>
-  llvm::DIFile *F = DBuilder.createFile(<br>
-      remapDIPath(PLoc.getFilename()), remapDIPath(getCurrentDirname()), CSInfo,<br>
-      getSource(SM, SM.getFileID(Loc)));<br>
+  StringRef Dir;<br>
+  StringRef File;<br>
+  std::string RemappedFile = remapDIPath(FileName);<br>
+  std::string CurDir = remapDIPath(getCurrentDirname());<br>
+  SmallString<128> DirBuf;<br>
+  SmallString<128> FileBuf;<br>
+  if (llvm::sys::path::is_absolute(RemappedFile)) {<br>
+    // Strip the common prefix (if it is more than just "/") from current<br>
+    // directory and FileName for a more space-efficient encoding.<br>
+    auto FileIt = llvm::sys::path::begin(RemappedFile);<br>
+    auto FileE = llvm::sys::path::end(RemappedFile);<br>
+    auto CurDirIt = llvm::sys::path::begin(CurDir);<br>
+    auto CurDirE = llvm::sys::path::end(CurDir);<br>
+    for (; CurDirIt != CurDirE && *CurDirIt == *FileIt; ++CurDirIt, ++FileIt)<br>
+      llvm::sys::path::append(DirBuf, *CurDirIt);<br>
+    if (std::distance(llvm::sys::path::begin(CurDir), CurDirIt) == 1) {<br>
+      // The common prefix only the root; stripping it would cause<br>
+      // LLVM diagnostic locations to be more confusing.<br>
+      Dir = {};<br>
+      File = RemappedFile;<br>
+    } else {<br>
+      for (; FileIt != FileE; ++FileIt)<br>
+        llvm::sys::path::append(FileBuf, *FileIt);<br>
+      Dir = DirBuf;<br>
+      File = FileBuf;<br>
+    }<br>
+  } else {<br>
+    Dir = CurDir;<br>
+    File = RemappedFile;<br>
+  }<br>
+  llvm::DIFile *F =<br>
+      DBuilder.createFile(File, Dir, CSInfo,<br>
+                          getSource(SM, SM.getFileID(Loc)));<br>
<br>
-  DIFileCache[fname].reset(F);<br>
+  DIFileCache[FileName.data()].reset(F);<br>
   return F;<br>
 }<br>
<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CodeGenAction.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenAction.cpp?rev=348154&r1=348153&r2=348154&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenAction.cpp?rev=348154&r1=348153&r2=348154&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CodeGenAction.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CodeGenAction.cpp Mon Dec  3 09:55:27 2018<br>
@@ -549,12 +549,16 @@ const FullSourceLoc BackendConsumer::get<br>
   SourceLocation DILoc;<br>
<br>
   if (D.isLocationAvailable()) {<br>
-    D.getLocation(&Filename, &Line, &Column);<br>
-    const FileEntry *FE = FileMgr.getFile(Filename);<br>
-    if (FE && Line > 0) {<br>
-      // If -gcolumn-info was not used, Column will be 0. This upsets the<br>
-      // source manager, so pass 1 if Column is not set.<br>
-      DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);<br>
+    D.getLocation(Filename, Line, Column);<br>
+    if (Line > 0) {<br>
+      const FileEntry *FE = FileMgr.getFile(Filename);<br>
+      if (!FE)<br>
+        FE = FileMgr.getFile(D.getAbsolutePath());<br>
+      if (FE) {<br>
+        // If -gcolumn-info was not used, Column will be 0. This upsets the<br>
+        // source manager, so pass 1 if Column is not set.<br>
+        DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);<br>
+      }<br>
     }<br>
     BadDebugInfo = DILoc.isInvalid();<br>
   }<br>
<br>
Added: cfe/trunk/test/CodeGen/debug-info-abspath.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/debug-info-abspath.c?rev=348154&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/debug-info-abspath.c?rev=348154&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/test/CodeGen/debug-info-abspath.c (added)<br>
+++ cfe/trunk/test/CodeGen/debug-info-abspath.c Mon Dec  3 09:55:27 2018<br>
@@ -0,0 +1,15 @@<br>
+// RUN: %clang_cc1 -debug-info-kind=limited -triple %itanium_abi_triple \<br>
+// RUN:   %s -emit-llvm -o - | FileCheck %s<br>
+<br>
+// RUN: cp %s %t.c<br>
+// RUN: %clang_cc1 -debug-info-kind=limited -triple %itanium_abi_triple \<br>
+// RUN:   %t.c -emit-llvm -o - | FileCheck %s --check-prefix=INTREE<br>
+void foo() {}<br>
+<br>
+// Since %s is an absolute path, directory should be a nonempty<br>
+// prefix, but the CodeGen part should be part of the filename.<br>
+<br>
+// CHECK: DIFile(filename: "{{.*}}CodeGen{{.*}}debug-info-abspath.c"<br>
+// CHECK-SAME:   directory: "{{.+}}")<br>
+<br>
+// INTREE: DIFile({{.*}}directory: "{{.+}}CodeGen{{.*}}")<br>
<br>
Modified: cfe/trunk/test/CodeGen/debug-prefix-map.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/debug-prefix-map.c?rev=348154&r1=348153&r2=348154&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/debug-prefix-map.c?rev=348154&r1=348153&r2=348154&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/CodeGen/debug-prefix-map.c (original)<br>
+++ cfe/trunk/test/CodeGen/debug-prefix-map.c Mon Dec  3 09:55:27 2018<br>
@@ -17,18 +17,22 @@ void test_rewrite_includes() {<br>
 }<br>
<br>
 // CHECK-NO-MAIN-FILE-NAME: !DIFile(filename: "/var/empty{{/|\\5C}}<stdin>"<br>
-// CHECK-NO-MAIN-FILE-NAME: !DIFile(filename: "/var/empty{{[/\\]}}{{.*}}"<br>
-// CHECK-NO-MAIN-FILE-NAME: !DIFile(filename: "/var/empty{{[/\\]}}Inputs/stdio.h"<br>
+// CHECK-NO-MAIN-FILE-NAME: !DIFile(filename: "/var/empty{{[/\\]}}{{.*}}",<br>
+// CHECK-NO-MAIN-FILE-NAME-SAME:    directory: "")<br>
+// CHECK-NO-MAIN-FILE-NAME: !DIFile(filename: "/var/empty{{[/\\]}}Inputs/stdio.h",<br>
+// CHECK-NO-MAIN-FILE-NAME-SAME:    directory: "")<br>
 // CHECK-NO-MAIN-FILE-NAME-NOT: !DIFile(filename:<br>
<br>
 // CHECK-EVIL: !DIFile(filename: "/var=empty{{[/\\]}}{{.*}}"<br>
-// CHECK-EVIL: !DIFile(filename: "/var=empty{{[/\\]}}Inputs/stdio.h"<br>
+// CHECK-EVIL: !DIFile(filename: "/var=empty{{[/\\]}}{{.*}}Inputs/stdio.h",<br>
+// CHECK-EVIL-SAME:    directory: "")<br>
 // CHECK-EVIL-NOT: !DIFile(filename:<br>
<br>
 // CHECK: !DIFile(filename: "/var/empty{{[/\\]}}{{.*}}"<br>
-// CHECK: !DIFile(filename: "/var/empty{{[/\\]}}Inputs/stdio.h"<br>
+// CHECK: !DIFile(filename: "/var/empty{{[/\\]}}{{.*}}Inputs/stdio.h",<br>
+// CHECK-SAME:    directory: "")<br>
 // CHECK-NOT: !DIFile(filename:<br>
<br>
-// CHECK-COMPILATION-DIR: !DIFile(filename: "/var/empty{{[/\\]}}{{.*}}", directory: "/var/empty")<br>
-// CHECK-COMPILATION-DIR: !DIFile(filename: "/var/empty{{[/\\]}}Inputs/stdio.h", directory: "/var/empty")<br>
+// CHECK-COMPILATION-DIR: !DIFile(filename: "{{.*}}", directory: "/var/empty")<br>
+// CHECK-COMPILATION-DIR: !DIFile(filename: "Inputs/stdio.h", directory: "/var/empty")<br>
 // CHECK-COMPILATION-DIR-NOT: !DIFile(filename:<br>
<br>
Modified: cfe/trunk/test/Modules/module-debuginfo-prefix.m<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/module-debuginfo-prefix.m?rev=348154&r1=348153&r2=348154&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/module-debuginfo-prefix.m?rev=348154&r1=348153&r2=348154&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/Modules/module-debuginfo-prefix.m (original)<br>
+++ cfe/trunk/test/Modules/module-debuginfo-prefix.m Mon Dec  3 09:55:27 2018<br>
@@ -20,4 +20,4 @@<br>
 @import DebugObjC;<br>
 #endif<br>
<br>
-// CHECK: !DIFile({{.*}}"/OVERRIDE/DebugObjC.h"<br>
+// CHECK: !DIFile(filename: "/OVERRIDE/DebugObjC.h", directory: "")<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>