Skip to content

[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

Merged
merged 7 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class ModuleDepCollector final : public DependencyCollector {

/// Compute the context hash for \p Deps, and create the mapping
/// \c ModuleDepsByID[Deps.ID] = &Deps.
void associateWithContextHash(const CowCompilerInvocation &CI,
void associateWithContextHash(const CowCompilerInvocation &CI, bool IgnoreCWD,
ModuleDeps &Deps);
};

Expand Down
50 changes: 43 additions & 7 deletions clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ static void optimizeDiagnosticOpts(DiagnosticOptions &Opts,
Opts.Remarks.clear();
}

static void optimizeCWD(CowCompilerInvocation &BuildInvocation, StringRef CWD) {
BuildInvocation.getMutFileSystemOpts().WorkingDir.clear();
if (BuildInvocation.getCodeGenOpts().DwarfVersion) {
// 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), 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,
// CGOpts.DebugCompilationDir is explicitly set to a common
// directory.
// FIXME: It is still excessive to create a copy of
// CodeGenOpts for each module. Since we do not modify the
// CodeGenOpts otherwise per module, the following code
// ends up generating identical CodeGenOpts for each module
// with DebugCompilationDir pointing to the root directory.
// We can optimize this away by creating a _single_ copy of
// CodeGenOpts whose DebugCompilationDir points to the root
// directory and reuse it across modules.
BuildInvocation.getMutCodeGenOpts().DebugCompilationDir =
llvm::sys::path::root_path(CWD);
}
}

static std::vector<std::string> splitString(std::string S, char Separator) {
SmallVector<StringRef> Segments;
StringRef(S).split(Segments, Separator, /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Expand Down Expand Up @@ -489,11 +517,8 @@ static std::string getModuleContextHash(const ModuleDeps &MD,
HashBuilder.add(getClangFullRepositoryVersion());
HashBuilder.add(serialization::VERSION_MAJOR, serialization::VERSION_MINOR);
llvm::ErrorOr<std::string> CWD = VFS.getCurrentWorkingDirectory();
auto &FSOpts = const_cast<FileSystemOptions &>(CI.getFileSystemOpts());
if (CWD && !IgnoreCWD)
HashBuilder.add(*CWD);
else
FSOpts.WorkingDir.clear();

// Hash the BuildInvocation without any input files.
SmallString<0> ArgVec;
Expand Down Expand Up @@ -524,9 +549,7 @@ static std::string getModuleContextHash(const ModuleDeps &MD,
}

void ModuleDepCollector::associateWithContextHash(
const CowCompilerInvocation &CI, ModuleDeps &Deps) {
bool IgnoreCWD = any(OptimizeArgs & ScanningOptimizations::IgnoreCWD) &&
isSafeToIgnoreCWD(CI);
const CowCompilerInvocation &CI, bool IgnoreCWD, ModuleDeps &Deps) {
Deps.ID.ContextHash =
getModuleContextHash(Deps, CI, EagerLoadModules, IgnoreCWD,
ScanInstance.getVirtualFileSystem());
Expand Down Expand Up @@ -726,6 +749,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
MD.ModuleMapFileDeps.emplace_back(*ResolvedFilenameAsRequested);
});

bool IgnoreCWD = false;
CowCompilerInvocation CI =
MDC.getInvocationAdjustedForModuleBuildWithoutOutputs(
MD, [&](CowCompilerInvocation &BuildInvocation) {
Expand All @@ -735,13 +759,25 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
*MDC.ScanInstance.getASTReader(), *MF,
MDC.PrebuiltModuleVFSMap,
MDC.OptimizeArgs);

if (any(MDC.OptimizeArgs & ScanningOptimizations::SystemWarnings))
optimizeDiagnosticOpts(
BuildInvocation.getMutDiagnosticOpts(),
BuildInvocation.getFrontendOpts().IsSystemModule);

IgnoreCWD =
any(MDC.OptimizeArgs & ScanningOptimizations::IgnoreCWD) &&
isSafeToIgnoreCWD(BuildInvocation);
if (IgnoreCWD) {
llvm::ErrorOr<std::string> CWD =
MDC.ScanInstance.getVirtualFileSystem()
.getCurrentWorkingDirectory();
if (CWD)
optimizeCWD(BuildInvocation, *CWD);
}
});

MDC.associateWithContextHash(CI, MD);
MDC.associateWithContextHash(CI, IgnoreCWD, MD);

// Finish the compiler invocation. Requires dependencies and the context hash.
MDC.addOutputPaths(CI, MD);
Expand Down
32 changes: 32 additions & 0 deletions clang/test/ClangScanDeps/modules-debug-dir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// REQUIRES: shell

// 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
// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck %s

//--- cdb.json.in
[{
"directory": "DIR",
"command": "clang -c -g -gmodules DIR/tu.c -fmodules -fmodules-cache-path=DIR/cache -IDIR/include/ -fdebug-compilation-dir=DIR -o DIR/tu.o",
"file": "DIR/tu.c"
}]

//--- include/module.modulemap
module mod {
header "mod.h"
}

//--- include/mod.h

//--- tu.c
#include "mod.h"

// Check the -fdebug-compilation-dir used for the module is the root
// directory when current working directory optimization is in effect.
// CHECK: "modules": [
// CHECK: "command-line": [
// CHECK: "-fdebug-compilation-dir={{\/|.*:(\\)?}}",
// CHECK: "translation-units": [