Skip to content

Commit 3c08498

Browse files
[clang][CodeGen] Remove CWD fallback in compilation directory (#150130)
CWD is queried in clang driver and passed to clang cc1 via flags when needed. Respect the cc1 flags and do not repeated checking current working directory in CodeGen.
1 parent 9de4e06 commit 3c08498

12 files changed

+24
-69
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -643,16 +643,7 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
643643
}
644644

645645
StringRef CGDebugInfo::getCurrentDirname() {
646-
if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
647-
return CGM.getCodeGenOpts().DebugCompilationDir;
648-
649-
if (!CWDName.empty())
650-
return CWDName;
651-
llvm::ErrorOr<std::string> CWD =
652-
CGM.getFileSystem()->getCurrentWorkingDirectory();
653-
if (!CWD)
654-
return StringRef();
655-
return CWDName = internString(*CWD);
646+
return CGM.getCodeGenOpts().DebugCompilationDir;
656647
}
657648

658649
void CGDebugInfo::CreateCompileUnit() {
@@ -3248,6 +3239,9 @@ llvm::DIModule *CGDebugInfo::getOrCreateModuleRef(ASTSourceDescriptor Mod,
32483239
std::string Remapped = remapDIPath(Path);
32493240
StringRef Relative(Remapped);
32503241
StringRef CompDir = TheCU->getDirectory();
3242+
if (CompDir.empty())
3243+
return Remapped;
3244+
32513245
if (Relative.consume_front(CompDir))
32523246
Relative.consume_front(llvm::sys::path::get_separator());
32533247

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ class CGDebugInfo {
158158
/// This is a storage for names that are constructed on demand. For
159159
/// example, C++ destructors, C++ operators etc..
160160
llvm::BumpPtrAllocator DebugInfoNames;
161-
StringRef CWDName;
162161

163162
llvm::DenseMap<const char *, llvm::TrackingMDRef> DIFileCache;
164163
llvm::DenseMap<const FunctionDecl *, llvm::TrackingMDRef> SPCache;

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,12 +2449,7 @@ CoverageMappingModuleGen::CoverageMappingModuleGen(
24492449
: CGM(CGM), SourceInfo(SourceInfo) {}
24502450

24512451
std::string CoverageMappingModuleGen::getCurrentDirname() {
2452-
if (!CGM.getCodeGenOpts().CoverageCompilationDir.empty())
2453-
return CGM.getCodeGenOpts().CoverageCompilationDir;
2454-
2455-
SmallString<256> CWD;
2456-
llvm::sys::fs::current_path(CWD);
2457-
return CWD.str().str();
2452+
return CGM.getCodeGenOpts().CoverageCompilationDir;
24582453
}
24592454

24602455
std::string CoverageMappingModuleGen::normalizeFilename(StringRef Filename) {

clang/test/CodeGen/debug-info-abspath.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@
22
// RUN: cp %s %t/UNIQUEISH_SENTINEL/debug-info-abspath.c
33

44
// RUN: %clang_cc1 -debug-info-kind=limited -triple %itanium_abi_triple \
5+
// RUN: -fdebug-compilation-dir=%t/UNIQUEISH_SENTINEL/debug-info-abspath.c \
56
// RUN: %t/UNIQUEISH_SENTINEL/debug-info-abspath.c -emit-llvm -o - \
67
// RUN: | FileCheck %s
78

89
// RUN: cp %s %t.c
910
// RUN: %clang_cc1 -debug-info-kind=limited -triple %itanium_abi_triple \
11+
// RUN: -fdebug-compilation-dir=%t \
1012
// RUN: %t.c -emit-llvm -o - | FileCheck %s --check-prefix=INTREE
1113

12-
// RUN: cd %t/UNIQUEISH_SENTINEL
13-
// RUN: %clang_cc1 -debug-info-kind=limited -triple %itanium_abi_triple \
14-
// RUN: debug-info-abspath.c -emit-llvm -o - \
15-
// RUN: | FileCheck %s --check-prefix=CURDIR
16-
// RUN: %clang_cc1 -debug-info-kind=limited -triple %itanium_abi_triple \
17-
// RUN: %s -emit-llvm -o - | FileCheck %s --check-prefix=CURDIR
18-
1914
void foo(void) {}
2015

2116
// Since %s is an absolute path, directory should be the common
@@ -28,7 +23,3 @@ void foo(void) {}
2823

2924
// INTREE: = distinct !DISubprogram({{.*}}![[SPFILE:[0-9]+]]
3025
// INTREE: DIFile({{.*}}directory: "{{.+}}CodeGen{{.*}}")
31-
32-
// CURDIR: = distinct !DICompileUnit({{.*}}file: ![[CUFILE:[0-9]+]]
33-
// CURDIR: ![[CUFILE]] = !DIFile({{.*}}directory: "{{.+}}UNIQUEISH_SENTINEL")
34-

clang/test/CodeGen/debug-info-compilation-dir.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@
77
// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck -check-prefix=CHECK-DIR %s
88
// CHECK-DIR: CodeGen
99

10+
/// Test path remapping.
11+
// RUN: %clang_cc1 -fdebug-compilation-dir=%S -main-file-name %s -emit-llvm -debug-info-kind=limited %s -o - | FileCheck -check-prefix=CHECK-ABS %s
12+
// CHECK-ABS: DIFile(filename: "{{.*}}debug-info-compilation-dir.c", directory: "{{.*}}CodeGen")
13+
14+
// RUN: %clang_cc1 -main-file-name %s -emit-llvm -debug-info-kind=limited %s -o - | FileCheck -check-prefix=CHECK-NOMAP %s
15+
// CHECK-NOMAP: DIFile(filename: "{{.*}}debug-info-compilation-dir.c", directory: "")
16+

clang/test/CodeGen/debug-prefix-map.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// RUN: rm -rf %t && mkdir -p %t/a/b && cp %s %t/a/b/c.c
1313
// RUN: %clang_cc1 -emit-llvm -debug-info-kind=standalone -I%S -fdebug-prefix-map=%t/a/b=y -fdebug-prefix-map=%t/a=x %t/a/b/c.c -o - | FileCheck %s --check-prefix=CHECK-X
1414
// RUN: %clang_cc1 -emit-llvm -debug-info-kind=standalone -I%S -fdebug-prefix-map=%t/a=x -fdebug-prefix-map=%t/a/b=y %t/a/b/c.c -o - | FileCheck %s --check-prefix=CHECK-Y
15+
// RUN: %clang_cc1 -emit-llvm -debug-info-kind=standalone -I%S -main-file-name %t/a/b/c.c -fdebug-compilation-dir=%t/a -fdebug-prefix-map=%t/a=x -fdebug-prefix-map=%t/a/b=y %t/a/b/c.c -o - | FileCheck %s --check-prefix=CHECK-REMAP-Y
1516

1617
#include "Inputs/stdio.h"
1718

@@ -26,9 +27,9 @@ void test_rewrite_includes(void) {
2627
vprintf("string", argp);
2728
}
2829

29-
// CHECK-NO-MAIN-FILE-NAME: !DIFile(filename: "{{/|.:\\\\}}UNLIKELY_PATH{{/|\\\\}}empty{{/|\\\\}}<stdin>",
3030
// CHECK-NO-MAIN-FILE-NAME: !DIFile(filename: "{{/|.:\\\\}}UNLIKELY_PATH{{/|\\\\}}empty{{/|\\\\}}{{.*}}",
3131
// CHECK-NO-MAIN-FILE-NAME-SAME: directory: "")
32+
// CHECK-NO-MAIN-FILE-NAME: !DIFile(filename: "{{/|.:\\\\}}UNLIKELY_PATH{{/|\\\\}}empty{{/|\\\\}}<stdin>",
3233
// CHECK-NO-MAIN-FILE-NAME: !DIFile(filename: "{{/|.:\\\\}}UNLIKELY_PATH{{/|\\\\}}empty{{/|\\\\}}Inputs{{/|\\\\}}stdio.h",
3334
// CHECK-NO-MAIN-FILE-NAME-SAME: directory: "")
3435
// CHECK-NO-MAIN-FILE-NAME-NOT: !DIFile(filename:
@@ -54,3 +55,5 @@ void test_rewrite_includes(void) {
5455

5556
// CHECK-X: !DIFile(filename: "x{{/|\\\\}}b{{/|\\\\}}c.c", directory: "")
5657
// CHECK-Y: !DIFile(filename: "y{{/|\\\\}}c.c", directory: "")
58+
59+
// CHECK-REMAP-Y: !DIFile(filename: "y{{/|\\\\}}c.c", directory: "x")

clang/test/CodeGenCXX/debug-info-function-context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited -triple x86_64-pc-linux-gnu %s \
1+
// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited -triple x86_64-pc-linux-gnu %s -fdebug-compilation-dir=%S \
22
// RUN: -dwarf-version=5 -main-file-name debug-info-function-context.cpp -o - | FileCheck %s
33

44
struct C {

clang/test/CodeGenCXX/difile_entry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// Test that we canonicalize the DIFile.
44
// RUN: rm -rf %t && mkdir %t && cd %t
55
// RUN: cp %s .
6-
// RUN: %clang_cc1 -triple %itanium_abi_triple -main-file-name difile_entry.cpp -debug-info-kind=limited %t/difile_entry.cpp -std=c++11 -emit-llvm -o - | FileCheck %s
6+
// RUN: %clang_cc1 -triple %itanium_abi_triple -main-file-name difile_entry.cpp -fdebug-compilation-dir=%t -debug-info-kind=limited %t/difile_entry.cpp -std=c++11 -emit-llvm -o - | FileCheck %s
77
int x();
88
static int i = x();
99

clang/test/PCH/debug-info-pch-container-path.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// RUN: -triple %itanium_abi_triple \
1010
// RUN: -fdebug-prefix-map=%t=BUILD \
1111
// RUN: -fdebug-prefix-map=%S=SOURCE \
12+
// RUN: -fdebug-compilation-dir=%t \
1213
// RUN: -o %t/prefix.ll %S/debug-info-limited-struct.h \
1314
// RUN: -mllvm -debug-only=pchcontainer &>%t-container.ll
1415
// RUN: cat %t-container.ll | FileCheck %s

clang/test/PCH/debug-info-pch-path.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
// RUN: %clang_cc1 -debug-info-kind=standalone \
6666
// RUN: -dwarf-ext-refs -fmodule-format=obj \
6767
// RUN: -triple %itanium_abi_triple \
68-
// RUN: -include-pch %t/prefix.pch %s -emit-llvm -o %t.abs.ll %s
68+
// RUN: -include-pch %t/prefix.pch %s -emit-llvm \
69+
// RUN: -fdebug-compilation-dir=%t -o %t.abs.ll %s
6970
// RUN: cat %t.abs.ll | FileCheck %s --check-prefix=CHECK-ABS
7071

7172
// CHECK-ABS: !DICompileUnit

0 commit comments

Comments
 (0)