Skip to content

Commit bda9272

Browse files
[clang][ScanDeps] Clear compilation directory if needed (#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 78c57c9 commit bda9272

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
@@ -144,30 +144,8 @@ static void optimizeDiagnosticOpts(DiagnosticOptions &Opts,
144144

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

173151
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)