@@ -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+
132152static 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
541543void 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);
0 commit comments