[clang] Fix absolute file paths with -fdebug-prefix-map
authorKeith Smiley <keithbsmiley@gmail.com>
Thu, 7 Oct 2021 20:25:21 +0000 (13:25 -0700)
committerKeith Smiley <keithbsmiley@gmail.com>
Fri, 8 Oct 2021 17:35:17 +0000 (10:35 -0700)
Previously if you passed an absolute path to clang, where only part of
the path to the file was remapped, it would result in the file's DIFile
being stored with a duplicate path, for example:

```
!DIFile(filename: "./ios/Sources/bar.c", directory: "./ios/Sources")
```

This change handles absolute paths, specifically in the case they are
remapped to something relative, and uses the dirname for the directory,
and basename for the filename.

This also adds a test verifying this behavior for more standard uses as
well.

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

clang/lib/CodeGen/CGDebugInfo.cpp
clang/test/CodeGen/relative-debug-prefix-map.c [new file with mode: 0644]

index 9c98278aa622193f000a684d929db8f748f640fe..42127d652e76675d33a50702377ac02b6dffc1e7 100644 (file)
@@ -446,6 +446,9 @@ CGDebugInfo::createFile(StringRef FileName,
       Dir = DirBuf;
       File = FileBuf;
     }
+  } else if (llvm::sys::path::is_absolute(FileName)) {
+    Dir = llvm::sys::path::parent_path(RemappedFile);
+    File = llvm::sys::path::filename(RemappedFile);
   } else {
     Dir = CurDir;
     File = RemappedFile;
diff --git a/clang/test/CodeGen/relative-debug-prefix-map.c b/clang/test/CodeGen/relative-debug-prefix-map.c
new file mode 100644 (file)
index 0000000..54fee97
--- /dev/null
@@ -0,0 +1,17 @@
+// RUN: mkdir -p %t.nested/dir && cd %t.nested/dir
+// RUN: cp %s %t.nested/dir/main.c
+// RUN: %clang_cc1 -debug-info-kind=standalone -fdebug-prefix-map=%t.nested=. %t.nested/dir/main.c -emit-llvm -o - | FileCheck %s
+//
+// RUN: %clang_cc1 -debug-info-kind=standalone -fdebug-prefix-map=%p=. %s -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-DIRECT
+//
+// RUN: cd %p
+// RUN: %clang_cc1 -debug-info-kind=standalone -fdebug-compilation-dir=. relative-debug-prefix-map.c -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-DIRECT
+
+// CHECK: !DIFile(filename: "main.c", directory: "./dir")
+// CHECK-DIRECT: !DIFile(filename: "relative-debug-prefix-map.c", directory: ".")
+
+int main(int argc, char **argv) {
+  (void)argc;
+  (void)argv;
+  return 0;
+}