Skip to content

Commit 4b00780

Browse files
[clang][ScanDeps] Clear compilation directory if needed (llvm#150129)
During explicit module build, clear the compilation directory flags when CWD optimization is on. Now clang CodeGen will not add compilation directories to DIFile when the corresponding flags are not passed and make debug info not dependent on CWD.
1 parent 65dc7bb commit 4b00780

File tree

2 files changed

+9
-27
lines changed

2 files changed

+9
-27
lines changed

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,8 @@ static void optimizeDiagnosticOpts(DiagnosticOptions &Opts,
155155

156156
static void optimizeCWD(CowCompilerInvocation &BuildInvocation, StringRef CWD) {
157157
BuildInvocation.getMutFileSystemOpts().WorkingDir.clear();
158-
if (BuildInvocation.getCodeGenOpts().DwarfVersion) {
159-
// It is necessary to explicitly set the DebugCompilationDir
160-
// to a common directory (e.g. root) if IgnoreCWD is true.
161-
// When IgnoreCWD is true, the module's content should not
162-
// depend on the current working directory. However, if dwarf
163-
// information is needed (when CGOpts.DwarfVersion is
164-
// non-zero), then CGOpts.DebugCompilationDir must be
165-
// populated, because otherwise the current working directory
166-
// will be automatically embedded in the dwarf information in
167-
// the pcm, contradicting the assumption that it is safe to
168-
// ignore the CWD. Thus in such cases,
169-
// CGOpts.DebugCompilationDir is explicitly set to a common
170-
// directory.
171-
// FIXME: It is still excessive to create a copy of
172-
// CodeGenOpts for each module. Since we do not modify the
173-
// CodeGenOpts otherwise per module, the following code
174-
// ends up generating identical CodeGenOpts for each module
175-
// with DebugCompilationDir pointing to the root directory.
176-
// We can optimize this away by creating a _single_ copy of
177-
// CodeGenOpts whose DebugCompilationDir points to the root
178-
// directory and reuse it across modules.
179-
BuildInvocation.getMutCodeGenOpts().DebugCompilationDir =
180-
llvm::sys::path::root_path(CWD);
181-
}
158+
BuildInvocation.getMutCodeGenOpts().DebugCompilationDir.clear();
159+
BuildInvocation.getMutCodeGenOpts().CoverageCompilationDir.clear();
182160
}
183161

184162
static std::vector<std::string> splitString(std::string S, char Separator) {

clang/test/ClangScanDeps/modules-debug-dir.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
// REQUIRES: shell
2-
31
// RUN: rm -rf %t
42
// RUN: split-file %s %t
53
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.in > %t/cdb.json
64
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format \
75
// RUN: experimental-full -optimize-args=all > %t/result.json
86
// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck %s
97

8+
// RUN: %deps-to-rsp %t/result.json --module-name=mod > %t/mod.rsp
9+
// RUN: %clang @%t/mod.rsp -o %t/mod.pcm
10+
// RUN: llvm-dwarfdump --debug-info %t/mod.pcm | FileCheck %s --check-prefix=DWARF
11+
// DWARF: DW_TAG_compile_unit
12+
// DWARF-NOT: DW_AT_comp_dir
13+
1014
//--- cdb.json.in
1115
[{
1216
"directory": "DIR",
@@ -28,5 +32,5 @@ module mod {
2832
// directory when current working directory optimization is in effect.
2933
// CHECK: "modules": [
3034
// CHECK: "command-line": [
31-
// CHECK: "-fdebug-compilation-dir={{\/|.*:(\\)?}}",
35+
// CHECK-NOT: -fdebug-compilation-dir
3236
// CHECK: "translation-units": [

0 commit comments

Comments
 (0)