-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang modules] Setting DebugCompilationDir
when it is safe to ignore current working directory
#128446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clang Author: Qiongsi Wu (qiongsiwu) ChangesThis PR explicitly sets This fixes a problem where a PCM file's embedded debug information can lead to compilation failure. The compiler may have decided it is indeed safe to ignore the current working directory. In this case, the PCM file's content is functionally correct regardless of the current working directory because no inputs use relative paths (see #124786). However, a PCM may contain debug info. If debug info is requested, the compiler uses the current working directory value to set
This PR resets the Full diff: https://github.com/llvm/llvm-project/pull/128446.diff 2 Files Affected:
diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
index 1c5f4c4b50ab6..8a94535d3806c 100644
--- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
+++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
@@ -492,8 +492,23 @@ static std::string getModuleContextHash(const ModuleDeps &MD,
auto &FSOpts = const_cast<FileSystemOptions &>(CI.getFileSystemOpts());
if (CWD && !IgnoreCWD)
HashBuilder.add(*CWD);
- else
+ else {
FSOpts.WorkingDir.clear();
+ auto &CGOpts = const_cast<CodeGenOptions &>(CI.getCodeGenOpts());
+ if (CGOpts.DwarfVersion && CWD) {
+ // It is necessary to explicitly set the DebugCompilationDir
+ // to a common directory (e.g. root) if IgnoreCWD is true.
+ // When IgnoreCWD is true, the module's content should not depend
+ // on the current working directory. However, if dwarf information
+ // is needed (when CGOpts.DwarfVersion is non-zero), and if
+ // CGOpts.DebugCompilationDir is not explicitly set,
+ // the current working directory will be automatically embedded
+ // in the dwarf information in the pcm, contradicting the assumption
+ // that it is safe to ignore the CWD. Thus in such cases,
+ // CGOpts.DebugCompilationDir is explicitly set to a common directory.
+ CGOpts.DebugCompilationDir = llvm::sys::path::root_path(*CWD);
+ }
+ }
// Hash the BuildInvocation without any input files.
SmallString<0> ArgVec;
diff --git a/clang/test/ClangScanDeps/modules-debug-dir.c b/clang/test/ClangScanDeps/modules-debug-dir.c
new file mode 100644
index 0000000000000..580f72205e68f
--- /dev/null
+++ b/clang/test/ClangScanDeps/modules-debug-dir.c
@@ -0,0 +1,22 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.in > %t/cdb.json
+// RUN: clang-scan-deps -compilation-database %t/cdb.json -format \
+// RUN: experimental-full > %t/result.json
+
+//--- cdb.json.in
+[{
+ "directory": "DIR",
+ "command": "clang -g -fdebug-info-for-profiling DIR/tu.c -fmodules -fmodules-cache-path=DIR/cache -IDIR/include/ -o DIR/tu.o",
+ "file": "DIR/tu.c"
+}]
+
+//--- include/module.modulemap
+module mod {
+ header "mod.h"
+}
+
+//--- include/mod.h
+
+//--- tu.c
+#include "mod.h"
|
Note to reviewers:
Thanks! |
@adrian-prantl for the debug directory option since that might interact how debugger working with the option. I think we need a solution approved by debugger forks for how to avoid stamping CWD into debug info and when it is safe to do so (for example, we guarantee all the paths in the TU/module are absolute path). |
Or maybe it makes more sense to set the working directory to SYSROOT, or to the input header's path, or the directory that contains the output pcm? Update: setting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving the correctness of the change up for other reviewers. My only gripe is the const_cast
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense on the surface. I'll pull this change and run lldb tests.
// on the current working directory. However, if dwarf information | ||
// is needed (when CGOpts.DwarfVersion is non-zero), and if | ||
// CGOpts.DebugCompilationDir is not explicitly set, | ||
// the current working directory will be automatically embedded | ||
// in the dwarf information in the pcm, contradicting the assumption | ||
// that it is safe to ignore the CWD. Thus in such cases, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When reading this comment, I was tripped up on "if CGOpts.DebugCompilationDir is not explicitly set", thinking that this should mean a if (!CGOpts.DebugCompilationDir)
somewhere in this function.
But I understand now. What do you think of this wording?
// on the current working directory. However, if dwarf information | |
// is needed (when CGOpts.DwarfVersion is non-zero), and if | |
// CGOpts.DebugCompilationDir is not explicitly set, | |
// the current working directory will be automatically embedded | |
// in the dwarf information in the pcm, contradicting the assumption | |
// that it is safe to ignore the CWD. Thus in such cases, | |
// on the current working directory. However, if dwarf information | |
// is needed (when CGOpts.DwarfVersion is non-zero), then | |
// CGOpts.DebugCompilationDir must be populated, because otherwise | |
// the current working directory will be automatically embedded | |
// in the dwarf information in the pcm, contradicting the assumption | |
// that it is safe to ignore the CWD. Thus in such cases, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this updated wording (before I didn't understand the intention) the patch looks safe to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does lldb use the CWD encoded in the PCM? Would it better/possible to avoid setting CGOpts.DebugCompilationDir
when IgnoreCWD == true
?
I'm not as concerned now. The important factors seem to be that
- the CWD in explicitly built PCM's shouldn't need to be directly referenced in the debugger.
- Even if
CGOpts.DebugCompilationDir
was empty here, it would still be written later which may incur more side effects.
Thanks for the explanation folks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I understand now. What do you think of this wording?
Thanks @kastiglione ! The comment modification looks great. It is in the PR now.
Ping for review. Thanks so much! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, @jansvoboda11 should also sign off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
…re current working directory (llvm#128446) This PR explicitly sets `DebugCompilationDir` to the system's root directory if it is safe to ignore the current working directory. This fixes a problem where a PCM file's embedded debug information can lead to compilation failure. The compiler may have decided it is indeed safe to ignore the current working directory. In this case, the PCM file's content is functionally correct regardless of the current working directory because no inputs use relative paths (see llvm#124786). However, a PCM may contain debug info. If debug info is requested, the compiler uses the current working directory value to set `DW_AT_comp_dir`. This may lead to the following situation: 1. Two different compilations need the same PCM file. 2. The PCM file is compiled assuming a working directory, which is embedded in the debug info, but otherwise has no effect. 3. The second compilation assumes a different working directory, and expects an identically-sized pcm file. However, it cannot find such a PCM, because the existing PCM file has been compiled assuming a different `DW_AT_comp_dir `, which is embedded in the debug info. This PR resets the `DebugCompilationDir` if it is functionally safe to ignore the working directory so the above situation is avoided, since all debug information will share the same working directory. rdar://145249881 (cherry picked from commit 7f482aa) Conflicts: clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
[clang modules] Setting `DebugCompilationDir` when it is safe to ignore current working directory (llvm#128446)
This PR explicitly sets
DebugCompilationDir
to the system's root directory if it is safe to ignore the current working directory.This fixes a problem where a PCM file's embedded debug information can lead to compilation failure. The compiler may have decided it is indeed safe to ignore the current working directory. In this case, the PCM file's content is functionally correct regardless of the current working directory because no inputs use relative paths (see #124786). However, a PCM may contain debug info. If debug info is requested, the compiler uses the current working directory value to set
DW_AT_comp_dir
. This may lead to the following situation:DW_AT_comp_dir
, which is embedded in the debug info.This PR resets the
DebugCompilationDir
if it is functionally safe to ignore the working directory so the above situation is avoided, since all debug information will share the same working directory.rdar://145249881