diff --git a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h index c2071a6eadedd..b41efa254342e 100644 --- a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h +++ b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h @@ -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); }; diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp index 1c5f4c4b50ab6..7da3ec71f2da4 100644 --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -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 splitString(std::string S, char Separator) { SmallVector Segments; StringRef(S).split(Segments, Separator, /*MaxSplit=*/-1, /*KeepEmpty=*/false); @@ -489,11 +517,8 @@ static std::string getModuleContextHash(const ModuleDeps &MD, HashBuilder.add(getClangFullRepositoryVersion()); HashBuilder.add(serialization::VERSION_MAJOR, serialization::VERSION_MINOR); llvm::ErrorOr CWD = VFS.getCurrentWorkingDirectory(); - auto &FSOpts = const_cast(CI.getFileSystemOpts()); if (CWD && !IgnoreCWD) HashBuilder.add(*CWD); - else - FSOpts.WorkingDir.clear(); // Hash the BuildInvocation without any input files. SmallString<0> ArgVec; @@ -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()); @@ -726,6 +749,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) { MD.ModuleMapFileDeps.emplace_back(*ResolvedFilenameAsRequested); }); + bool IgnoreCWD = false; CowCompilerInvocation CI = MDC.getInvocationAdjustedForModuleBuildWithoutOutputs( MD, [&](CowCompilerInvocation &BuildInvocation) { @@ -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 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); diff --git a/clang/test/ClangScanDeps/modules-debug-dir.c b/clang/test/ClangScanDeps/modules-debug-dir.c new file mode 100644 index 0000000000000..fadec1ad52e35 --- /dev/null +++ b/clang/test/ClangScanDeps/modules-debug-dir.c @@ -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": [