Skip to content

Commit 7021180

Browse files
committed
Address code review and try fixing the test on windows.
1 parent 5c53c84 commit 7021180

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class ModuleDepCollector final : public DependencyCollector {
317317

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

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ static void optimizeDiagnosticOpts(DiagnosticOptions &Opts,
129129
Opts.Remarks.clear();
130130
}
131131

132+
static void optimizeCWD(FileSystemOptions &FSOpts, CodeGenOptions &CGOpts,
133+
const std::string &CWD) {
134+
FSOpts.WorkingDir.clear();
135+
if (CGOpts.DwarfVersion) {
136+
// It is necessary to explicitly set the DebugCompilationDir
137+
// to a common directory (e.g. root) if IgnoreCWD is true.
138+
// When IgnoreCWD is true, the module's content should not
139+
// depend on the current working directory. However, if dwarf
140+
// information is needed (when CGOpts.DwarfVersion is
141+
// non-zero), then CGOpts.DebugCompilationDir must be
142+
// populated, because otherwise the current working directory
143+
// will be automatically embedded in the dwarf information in
144+
// the pcm, contradicting the assumption that it is safe to
145+
// ignore the CWD. Thus in such cases,
146+
// CGOpts.DebugCompilationDir is explicitly set to a common
147+
// directory.
148+
CGOpts.DebugCompilationDir = llvm::sys::path::root_path(CWD);
149+
}
150+
}
151+
132152
static std::vector<std::string> splitString(std::string S, char Separator) {
133153
SmallVector<StringRef> Segments;
134154
StringRef(S).split(Segments, Separator, /*MaxSplit=*/-1, /*KeepEmpty=*/false);
@@ -489,26 +509,8 @@ static std::string getModuleContextHash(const ModuleDeps &MD,
489509
HashBuilder.add(getClangFullRepositoryVersion());
490510
HashBuilder.add(serialization::VERSION_MAJOR, serialization::VERSION_MINOR);
491511
llvm::ErrorOr<std::string> CWD = VFS.getCurrentWorkingDirectory();
492-
auto &FSOpts = const_cast<FileSystemOptions &>(CI.getFileSystemOpts());
493512
if (CWD && !IgnoreCWD)
494513
HashBuilder.add(*CWD);
495-
else {
496-
FSOpts.WorkingDir.clear();
497-
auto &CGOpts = const_cast<CodeGenOptions &>(CI.getCodeGenOpts());
498-
if (CGOpts.DwarfVersion && CWD) {
499-
// It is necessary to explicitly set the DebugCompilationDir
500-
// to a common directory (e.g. root) if IgnoreCWD is true.
501-
// When IgnoreCWD is true, the module's content should not depend
502-
// on the current working directory. However, if dwarf information
503-
// is needed (when CGOpts.DwarfVersion is non-zero), and if
504-
// CGOpts.DebugCompilationDir is not explicitly set,
505-
// the current working directory will be automatically embedded
506-
// in the dwarf information in the pcm, contradicting the assumption
507-
// that it is safe to ignore the CWD. Thus in such cases,
508-
// CGOpts.DebugCompilationDir is explicitly set to a common directory.
509-
CGOpts.DebugCompilationDir = llvm::sys::path::root_path(*CWD);
510-
}
511-
}
512514

513515
// Hash the BuildInvocation without any input files.
514516
SmallString<0> ArgVec;
@@ -539,9 +541,7 @@ static std::string getModuleContextHash(const ModuleDeps &MD,
539541
}
540542

541543
void ModuleDepCollector::associateWithContextHash(
542-
const CowCompilerInvocation &CI, ModuleDeps &Deps) {
543-
bool IgnoreCWD = any(OptimizeArgs & ScanningOptimizations::IgnoreCWD) &&
544-
isSafeToIgnoreCWD(CI);
544+
const CowCompilerInvocation &CI, bool IgnoreCWD, ModuleDeps &Deps) {
545545
Deps.ID.ContextHash =
546546
getModuleContextHash(Deps, CI, EagerLoadModules, IgnoreCWD,
547547
ScanInstance.getVirtualFileSystem());
@@ -741,6 +741,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
741741
MD.ModuleMapFileDeps.emplace_back(*ResolvedFilenameAsRequested);
742742
});
743743

744+
bool IgnoreCWD = false;
744745
CowCompilerInvocation CI =
745746
MDC.getInvocationAdjustedForModuleBuildWithoutOutputs(
746747
MD, [&](CowCompilerInvocation &BuildInvocation) {
@@ -750,13 +751,26 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
750751
*MDC.ScanInstance.getASTReader(), *MF,
751752
MDC.PrebuiltModuleVFSMap,
752753
MDC.OptimizeArgs);
754+
753755
if (any(MDC.OptimizeArgs & ScanningOptimizations::SystemWarnings))
754756
optimizeDiagnosticOpts(
755757
BuildInvocation.getMutDiagnosticOpts(),
756758
BuildInvocation.getFrontendOpts().IsSystemModule);
759+
760+
IgnoreCWD =
761+
any(MDC.OptimizeArgs & ScanningOptimizations::IgnoreCWD) &&
762+
isSafeToIgnoreCWD(BuildInvocation);
763+
if (IgnoreCWD) {
764+
llvm::ErrorOr<std::string> CWD =
765+
MDC.ScanInstance.getVirtualFileSystem()
766+
.getCurrentWorkingDirectory();
767+
if (CWD)
768+
optimizeCWD(BuildInvocation.getMutFileSystemOpts(),
769+
BuildInvocation.getMutCodeGenOpts(), *CWD);
770+
}
757771
});
758772

759-
MDC.associateWithContextHash(CI, MD);
773+
MDC.associateWithContextHash(CI, IgnoreCWD, MD);
760774

761775
// Finish the compiler invocation. Requires dependencies and the context hash.
762776
MDC.addOutputPaths(CI, MD);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//--- cdb.json.in
99
[{
1010
"directory": "DIR",
11-
"command": "clang -target x86_64-apple-darwin -c -g -gmodules DIR/tu.c -fmodules -fmodules-cache-path=DIR/cache -IDIR/include/ -fdebug-compilation-dir=DIR -o DIR/tu.o",
11+
"command": "clang -c -g -gmodules DIR/tu.c -fmodules -fmodules-cache-path=DIR/cache -IDIR/include/ -fdebug-compilation-dir=DIR -o DIR/tu.o",
1212
"file": "DIR/tu.c"
1313
}]
1414

@@ -26,5 +26,5 @@ module mod {
2626
// directory when current working directory optimization is in effect.
2727
// CHECK: "modules": [
2828
// CHECK: "command-line": [
29-
// CHECK: "-fdebug-compilation-dir=/",
29+
// CHECK: "-fdebug-compilation-dir={{\/|.*:}}",
3030
// CHECK: "translation-units": [

0 commit comments

Comments
 (0)