1//===- DependencyScannerImpl.cpp - Implements module dependency scanning --===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/DependencyScanning/DependencyScannerImpl.h"
10#include "clang/Basic/DiagnosticFrontend.h"
11#include "clang/Basic/DiagnosticSerialization.h"
12#include "clang/DependencyScanning/DependencyScanningFilesystem.h"
13#include "clang/DependencyScanning/DependencyScanningService.h"
14#include "clang/DependencyScanning/DependencyScanningWorker.h"
15#include "clang/Driver/Driver.h"
16#include "clang/Frontend/FrontendActions.h"
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/ScopeExit.h"
19#include "llvm/Support/AdvisoryLock.h"
20#include "llvm/Support/CrashRecoveryContext.h"
21#include "llvm/Support/VirtualFileSystem.h"
22#include "llvm/TargetParser/Host.h"
23
24#include <mutex>
25#include <thread>
26
27using namespace clang;
28using namespace dependencies;
29
30namespace {
31/// Forwards the gatherered dependencies to the consumer.
32class DependencyConsumerForwarder : public DependencyFileGenerator {
33public:
34 DependencyConsumerForwarder(std::unique_ptr<DependencyOutputOptions> Opts,
35 StringRef WorkingDirectory, DependencyConsumer &C)
36 : DependencyFileGenerator(*Opts), WorkingDirectory(WorkingDirectory),
37 Opts(std::move(Opts)), C(C) {}
38
39 void finishedMainFile(DiagnosticsEngine &Diags) override {
40 C.handleDependencyOutputOpts(Opts: *Opts);
41 llvm::SmallString<256> CanonPath;
42 for (const auto &File : getDependencies()) {
43 CanonPath = File;
44 llvm::sys::path::remove_dots(path&: CanonPath, /*remove_dot_dot=*/true);
45 llvm::sys::path::make_absolute(current_directory: WorkingDirectory, path&: CanonPath);
46 C.handleFileDependency(Filename: CanonPath);
47 }
48 }
49
50private:
51 StringRef WorkingDirectory;
52 std::unique_ptr<DependencyOutputOptions> Opts;
53 DependencyConsumer &C;
54};
55
56static bool checkHeaderSearchPaths(const HeaderSearchOptions &HSOpts,
57 const HeaderSearchOptions &ExistingHSOpts,
58 DiagnosticsEngine *Diags,
59 const LangOptions &LangOpts) {
60 if (LangOpts.Modules) {
61 if (HSOpts.VFSOverlayFiles != ExistingHSOpts.VFSOverlayFiles) {
62 if (Diags) {
63 Diags->Report(DiagID: diag::warn_pch_vfsoverlay_mismatch);
64 auto VFSNote = [&](int Type, ArrayRef<std::string> VFSOverlays) {
65 if (VFSOverlays.empty()) {
66 Diags->Report(DiagID: diag::note_pch_vfsoverlay_empty) << Type;
67 } else {
68 std::string Files = llvm::join(R&: VFSOverlays, Separator: "\n");
69 Diags->Report(DiagID: diag::note_pch_vfsoverlay_files) << Type << Files;
70 }
71 };
72 VFSNote(0, HSOpts.VFSOverlayFiles);
73 VFSNote(1, ExistingHSOpts.VFSOverlayFiles);
74 }
75 }
76 }
77 return false;
78}
79
80using PrebuiltModuleFilesT = decltype(HeaderSearchOptions::PrebuiltModuleFiles);
81
82/// A listener that collects the imported modules and the input
83/// files. While visiting, collect vfsoverlays and file inputs that determine
84/// whether prebuilt modules fully resolve in stable directories.
85class PrebuiltModuleListener : public ASTReaderListener {
86public:
87 PrebuiltModuleListener(PrebuiltModuleFilesT &PrebuiltModuleFiles,
88 llvm::SmallVector<std::string> &NewModuleFiles,
89 PrebuiltModulesAttrsMap &PrebuiltModulesASTMap,
90 const HeaderSearchOptions &HSOpts,
91 const LangOptions &LangOpts, DiagnosticsEngine &Diags,
92 const ArrayRef<StringRef> StableDirs)
93 : PrebuiltModuleFiles(PrebuiltModuleFiles),
94 NewModuleFiles(NewModuleFiles),
95 PrebuiltModulesASTMap(PrebuiltModulesASTMap), ExistingHSOpts(HSOpts),
96 ExistingLangOpts(LangOpts), Diags(Diags), StableDirs(StableDirs) {}
97
98 bool needsImportVisitation() const override { return true; }
99 bool needsInputFileVisitation() override { return true; }
100 bool needsSystemInputFileVisitation() override { return true; }
101
102 /// Accumulate the modules are transitively depended on by the initial
103 /// prebuilt module.
104 void visitImport(StringRef ModuleName, StringRef Filename) override {
105 if (PrebuiltModuleFiles.insert(x: {ModuleName.str(), Filename.str()}).second)
106 NewModuleFiles.push_back(Elt: Filename.str());
107
108 auto PrebuiltMapEntry = PrebuiltModulesASTMap.try_emplace(Key: Filename);
109 PrebuiltModuleASTAttrs &PrebuiltModule = PrebuiltMapEntry.first->second;
110 if (PrebuiltMapEntry.second)
111 PrebuiltModule.setInStableDir(!StableDirs.empty());
112
113 if (auto It = PrebuiltModulesASTMap.find(Key: CurrentFile);
114 It != PrebuiltModulesASTMap.end() && CurrentFile != Filename)
115 PrebuiltModule.addDependent(ModuleFile: It->getKey());
116 }
117
118 /// For each input file discovered, check whether it's external path is in a
119 /// stable directory. Traversal is stopped if the current module is not
120 /// considered stable.
121 bool visitInputFileAsRequested(StringRef FilenameAsRequested,
122 StringRef Filename, bool isSystem,
123 bool isOverridden, time_t StoredTime,
124 bool isExplicitModule) override {
125 if (StableDirs.empty())
126 return false;
127 auto PrebuiltEntryIt = PrebuiltModulesASTMap.find(Key: CurrentFile);
128 if ((PrebuiltEntryIt == PrebuiltModulesASTMap.end()) ||
129 (!PrebuiltEntryIt->second.isInStableDir()))
130 return false;
131
132 PrebuiltEntryIt->second.setInStableDir(
133 isPathInStableDir(Directories: StableDirs, Input: Filename));
134 return PrebuiltEntryIt->second.isInStableDir();
135 }
136
137 /// Update which module that is being actively traversed.
138 void visitModuleFile(StringRef Filename,
139 serialization::ModuleKind Kind) override {
140 // If the CurrentFile is not
141 // considered stable, update any of it's transitive dependents.
142 auto PrebuiltEntryIt = PrebuiltModulesASTMap.find(Key: CurrentFile);
143 if ((PrebuiltEntryIt != PrebuiltModulesASTMap.end()) &&
144 !PrebuiltEntryIt->second.isInStableDir())
145 PrebuiltEntryIt->second.updateDependentsNotInStableDirs(
146 PrebuiltModulesMap&: PrebuiltModulesASTMap);
147 CurrentFile = Filename;
148 }
149
150 /// Check the header search options for a given module when considering
151 /// if the module comes from stable directories.
152 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
153 StringRef ModuleFilename, StringRef ContextHash,
154 bool Complain) override {
155
156 auto PrebuiltMapEntry = PrebuiltModulesASTMap.try_emplace(Key: CurrentFile);
157 PrebuiltModuleASTAttrs &PrebuiltModule = PrebuiltMapEntry.first->second;
158 if (PrebuiltMapEntry.second)
159 PrebuiltModule.setInStableDir(!StableDirs.empty());
160
161 if (PrebuiltModule.isInStableDir())
162 PrebuiltModule.setInStableDir(areOptionsInStableDir(Directories: StableDirs, HSOpts));
163
164 return false;
165 }
166
167 /// Accumulate vfsoverlays used to build these prebuilt modules.
168 bool ReadHeaderSearchPaths(const HeaderSearchOptions &HSOpts,
169 bool Complain) override {
170
171 auto PrebuiltMapEntry = PrebuiltModulesASTMap.try_emplace(Key: CurrentFile);
172 PrebuiltModuleASTAttrs &PrebuiltModule = PrebuiltMapEntry.first->second;
173 if (PrebuiltMapEntry.second)
174 PrebuiltModule.setInStableDir(!StableDirs.empty());
175
176 PrebuiltModule.setVFS(
177 llvm::StringSet<>(llvm::from_range, HSOpts.VFSOverlayFiles));
178
179 return checkHeaderSearchPaths(
180 HSOpts, ExistingHSOpts, Diags: Complain ? &Diags : nullptr, LangOpts: ExistingLangOpts);
181 }
182
183private:
184 PrebuiltModuleFilesT &PrebuiltModuleFiles;
185 llvm::SmallVector<std::string> &NewModuleFiles;
186 PrebuiltModulesAttrsMap &PrebuiltModulesASTMap;
187 const HeaderSearchOptions &ExistingHSOpts;
188 const LangOptions &ExistingLangOpts;
189 DiagnosticsEngine &Diags;
190 std::string CurrentFile;
191 const ArrayRef<StringRef> StableDirs;
192};
193
194/// Visit the given prebuilt module and collect all of the modules it
195/// transitively imports and contributing input files.
196static bool visitPrebuiltModule(StringRef PrebuiltModuleFilename,
197 CompilerInstance &CI,
198 PrebuiltModuleFilesT &ModuleFiles,
199 PrebuiltModulesAttrsMap &PrebuiltModulesASTMap,
200 DiagnosticsEngine &Diags,
201 const ArrayRef<StringRef> StableDirs) {
202 // List of module files to be processed.
203 llvm::SmallVector<std::string> Worklist;
204
205 PrebuiltModuleListener Listener(ModuleFiles, Worklist, PrebuiltModulesASTMap,
206 CI.getHeaderSearchOpts(), CI.getLangOpts(),
207 Diags, StableDirs);
208
209 Listener.visitModuleFile(Filename: PrebuiltModuleFilename,
210 Kind: serialization::MK_ExplicitModule);
211 if (ASTReader::readASTFileControlBlock(
212 Filename: PrebuiltModuleFilename, FileMgr&: CI.getFileManager(), ModCache: CI.getModuleCache(),
213 PCHContainerRdr: CI.getPCHContainerReader(),
214 /*FindModuleFileExtensions=*/false, Listener,
215 /*ValidateDiagnosticOptions=*/false, ClientLoadCapabilities: ASTReader::ARR_OutOfDate))
216 return true;
217
218 while (!Worklist.empty()) {
219 Listener.visitModuleFile(Filename: Worklist.back(), Kind: serialization::MK_ExplicitModule);
220 if (ASTReader::readASTFileControlBlock(
221 Filename: Worklist.pop_back_val(), FileMgr&: CI.getFileManager(), ModCache: CI.getModuleCache(),
222 PCHContainerRdr: CI.getPCHContainerReader(),
223 /*FindModuleFileExtensions=*/false, Listener,
224 /*ValidateDiagnosticOptions=*/false))
225 return true;
226 }
227 return false;
228}
229
230/// Transform arbitrary file name into an object-like file name.
231static std::string makeObjFileName(StringRef FileName) {
232 SmallString<128> ObjFileName(FileName);
233 llvm::sys::path::replace_extension(path&: ObjFileName, extension: "o");
234 return std::string(ObjFileName);
235}
236
237/// Deduce the dependency target based on the output file and input files.
238static std::string
239deduceDepTarget(const std::string &OutputFile,
240 const SmallVectorImpl<FrontendInputFile> &InputFiles) {
241 if (OutputFile != "-")
242 return OutputFile;
243
244 if (InputFiles.empty() || !InputFiles.front().isFile())
245 return "clang-scan-deps\\ dependency";
246
247 return makeObjFileName(FileName: InputFiles.front().getFile());
248}
249
250// Clang implements -D and -U by splatting text into a predefines buffer. This
251// allows constructs such as `-DFඞ=3 "-D F\u{0D9E} 4 3 2”` to be accepted and
252// define the same macro, or adding C++ style comments before the macro name.
253//
254// This function checks that the first non-space characters in the macro
255// obviously form an identifier that can be uniqued on without lexing. Failing
256// to do this could lead to changing the final definition of a macro.
257//
258// We could set up a preprocessor and actually lex the name, but that's very
259// heavyweight for a situation that will almost never happen in practice.
260static std::optional<StringRef> getSimpleMacroName(StringRef Macro) {
261 StringRef Name = Macro.split(Separator: "=").first.ltrim(Chars: " \t");
262 std::size_t I = 0;
263
264 auto FinishName = [&]() -> std::optional<StringRef> {
265 StringRef SimpleName = Name.slice(Start: 0, End: I);
266 if (SimpleName.empty())
267 return std::nullopt;
268 return SimpleName;
269 };
270
271 for (; I != Name.size(); ++I) {
272 switch (Name[I]) {
273 case '(': // Start of macro parameter list
274 case ' ': // End of macro name
275 case '\t':
276 return FinishName();
277 case '_':
278 continue;
279 default:
280 if (llvm::isAlnum(C: Name[I]))
281 continue;
282 return std::nullopt;
283 }
284 }
285 return FinishName();
286}
287} // namespace
288
289void dependencies::canonicalizeDefines(PreprocessorOptions &PPOpts) {
290 using MacroOpt = std::pair<StringRef, std::size_t>;
291 std::vector<MacroOpt> SimpleNames;
292 SimpleNames.reserve(n: PPOpts.Macros.size());
293 std::size_t Index = 0;
294 for (const auto &M : PPOpts.Macros) {
295 auto SName = getSimpleMacroName(Macro: M.first);
296 // Skip optimizing if we can't guarantee we can preserve relative order.
297 if (!SName)
298 return;
299 SimpleNames.emplace_back(args&: *SName, args&: Index);
300 ++Index;
301 }
302
303 llvm::stable_sort(Range&: SimpleNames, C: llvm::less_first());
304 // Keep the last instance of each macro name by going in reverse
305 auto NewEnd = std::unique(
306 first: SimpleNames.rbegin(), last: SimpleNames.rend(),
307 binary_pred: [](const MacroOpt &A, const MacroOpt &B) { return A.first == B.first; });
308 SimpleNames.erase(first: SimpleNames.begin(), last: NewEnd.base());
309
310 // Apply permutation.
311 decltype(PPOpts.Macros) NewMacros;
312 NewMacros.reserve(n: SimpleNames.size());
313 for (std::size_t I = 0, E = SimpleNames.size(); I != E; ++I) {
314 std::size_t OriginalIndex = SimpleNames[I].second;
315 // We still emit undefines here as they may be undefining a predefined macro
316 NewMacros.push_back(x: std::move(PPOpts.Macros[OriginalIndex]));
317 }
318 std::swap(x&: PPOpts.Macros, y&: NewMacros);
319}
320
321namespace {
322class ScanningDependencyDirectivesGetter : public DependencyDirectivesGetter {
323 DependencyScanningWorkerFilesystem *DepFS;
324
325public:
326 ScanningDependencyDirectivesGetter(FileManager &FileMgr) : DepFS(nullptr) {
327 FileMgr.getVirtualFileSystem().visit(Callback: [&](llvm::vfs::FileSystem &FS) {
328 auto *DFS = llvm::dyn_cast<DependencyScanningWorkerFilesystem>(Val: &FS);
329 if (DFS) {
330 assert(!DepFS && "Found multiple scanning VFSs");
331 DepFS = DFS;
332 }
333 });
334 assert(DepFS && "Did not find scanning VFS");
335 }
336
337 std::unique_ptr<DependencyDirectivesGetter>
338 cloneFor(FileManager &FileMgr) override {
339 return std::make_unique<ScanningDependencyDirectivesGetter>(args&: FileMgr);
340 }
341
342 std::optional<ArrayRef<dependency_directives_scan::Directive>>
343 operator()(FileEntryRef File) override {
344 return DepFS->getDirectiveTokens(Path: File.getName());
345 }
346};
347
348/// Sanitize diagnostic options for dependency scan.
349void sanitizeDiagOpts(DiagnosticOptions &DiagOpts) {
350 // Don't print 'X warnings and Y errors generated'.
351 DiagOpts.ShowCarets = false;
352 // Don't write out diagnostic file.
353 DiagOpts.DiagnosticSerializationFile.clear();
354 // Don't emit warnings except for scanning specific warnings.
355 // TODO: It would be useful to add a more principled way to ignore all
356 // warnings that come from source code. The issue is that we need to
357 // ignore warnings that could be surpressed by
358 // `#pragma clang diagnostic`, while still allowing some scanning
359 // warnings for things we're not ready to turn into errors yet.
360 // See `test/ClangScanDeps/diagnostic-pragmas.c` for an example.
361 llvm::erase_if(C&: DiagOpts.Warnings, P: [](StringRef Warning) {
362 return llvm::StringSwitch<bool>(Warning)
363 .Cases(CaseStrings: {"pch-vfs-diff", "error=pch-vfs-diff"}, Value: false)
364 .StartsWith(S: "no-error=", Value: false)
365 .Default(Value: true);
366 });
367}
368} // namespace
369
370std::unique_ptr<DiagnosticOptions>
371dependencies::createDiagOptions(ArrayRef<std::string> CommandLine) {
372 std::vector<const char *> CLI;
373 for (const std::string &Arg : CommandLine)
374 CLI.push_back(x: Arg.c_str());
375 auto DiagOpts = CreateAndPopulateDiagOpts(Argv: CLI);
376 sanitizeDiagOpts(DiagOpts&: *DiagOpts);
377 return DiagOpts;
378}
379
380DiagnosticsEngineWithDiagOpts::DiagnosticsEngineWithDiagOpts(
381 ArrayRef<std::string> CommandLine,
382 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, DiagnosticConsumer &DC) {
383 std::vector<const char *> CCommandLine(CommandLine.size(), nullptr);
384 llvm::transform(Range&: CommandLine, d_first: CCommandLine.begin(),
385 F: [](const std::string &Str) { return Str.c_str(); });
386 DiagOpts = CreateAndPopulateDiagOpts(Argv: CCommandLine);
387 sanitizeDiagOpts(DiagOpts&: *DiagOpts);
388 DiagEngine = CompilerInstance::createDiagnostics(VFS&: *FS, Opts&: *DiagOpts, Client: &DC,
389 /*ShouldOwnClient=*/false);
390}
391
392std::unique_ptr<CompilerInvocation>
393dependencies::createCompilerInvocation(ArrayRef<std::string> CommandLine,
394 DiagnosticsEngine &Diags) {
395 llvm::opt::ArgStringList Argv;
396 for (const std::string &Str : ArrayRef(CommandLine).drop_front())
397 Argv.push_back(Elt: Str.c_str());
398
399 auto Invocation = std::make_unique<CompilerInvocation>();
400 if (!CompilerInvocation::CreateFromArgs(Res&: *Invocation, CommandLineArgs: Argv, Diags)) {
401 // FIXME: Should we just go on like cc1_main does?
402 return nullptr;
403 }
404 return Invocation;
405}
406
407void dependencies::initializeScanCompilerInstance(
408 CompilerInstance &ScanInstance,
409 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
410 DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
411 IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS) {
412 ScanInstance.setBuildingModule(false);
413 ScanInstance.createVirtualFileSystem(BaseFS: FS, DC: DiagConsumer);
414 ScanInstance.createDiagnostics(Client: DiagConsumer, /*ShouldOwnClient=*/false);
415 ScanInstance.createFileManager();
416 ScanInstance.createSourceManager();
417
418 // Use DepFS for getting the dependency directives if requested to do so.
419 if (Service.getOpts().Mode == ScanningMode::DependencyDirectivesScan) {
420 DepFS->resetBypassedPathPrefix();
421 SmallString<256> ModulesCachePath;
422 normalizeModuleCachePath(FileMgr&: ScanInstance.getFileManager(),
423 Path: ScanInstance.getHeaderSearchOpts().ModuleCachePath,
424 NormalizedPath&: ModulesCachePath);
425 if (!ModulesCachePath.empty())
426 DepFS->setBypassedPathPrefix(ModulesCachePath);
427
428 ScanInstance.setDependencyDirectivesGetter(
429 std::make_unique<ScanningDependencyDirectivesGetter>(
430 args&: ScanInstance.getFileManager()));
431 }
432}
433
434std::shared_ptr<CompilerInvocation> dependencies::createScanCompilerInvocation(
435 const CompilerInvocation &Invocation,
436 const DependencyScanningService &Service,
437 DependencyActionController &Controller) {
438 auto ScanInvocation = std::make_shared<CompilerInvocation>(args: Invocation);
439
440 sanitizeDiagOpts(DiagOpts&: ScanInvocation->getDiagnosticOpts());
441
442 ScanInvocation->getPreprocessorOpts().AllowPCHWithDifferentModulesCachePath =
443 true;
444
445 if (ScanInvocation->getHeaderSearchOpts().ModulesValidateOncePerBuildSession)
446 ScanInvocation->getHeaderSearchOpts().BuildSessionTimestamp =
447 Service.getOpts().BuildSessionTimestamp;
448
449 ScanInvocation->getFrontendOpts().DisableFree = false;
450 ScanInvocation->getFrontendOpts().GenerateGlobalModuleIndex = false;
451 ScanInvocation->getFrontendOpts().UseGlobalModuleIndex = false;
452 ScanInvocation->getFrontendOpts().GenReducedBMI = false;
453 ScanInvocation->getFrontendOpts().ModuleOutputPath.clear();
454 // This will prevent us compiling individual modules asynchronously since
455 // FileManager is not thread-safe, but it does improve performance for now.
456 ScanInvocation->getFrontendOpts().ModulesShareFileManager = true;
457 ScanInvocation->getHeaderSearchOpts().ModuleFormat = "raw";
458 ScanInvocation->getHeaderSearchOpts().ModulesIncludeVFSUsage =
459 any(Val: Service.getOpts().OptimizeArgs & ScanningOptimizations::VFS);
460
461 // Consider different header search and diagnostic options to create
462 // different modules. This avoids the unsound aliasing of module PCMs.
463 //
464 // TODO: Implement diagnostic bucketing to reduce the impact of strict
465 // context hashing.
466 ScanInvocation->getHeaderSearchOpts().ModulesStrictContextHash = true;
467 ScanInvocation->getHeaderSearchOpts().ModulesSerializeOnlyPreprocessor = true;
468 ScanInvocation->getHeaderSearchOpts().ModulesSkipDiagnosticOptions = true;
469 ScanInvocation->getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = true;
470 ScanInvocation->getHeaderSearchOpts().ModulesSkipPragmaDiagnosticMappings =
471 true;
472 ScanInvocation->getHeaderSearchOpts().ModulesForceValidateUserHeaders = false;
473
474 // Ensure that the scanner does not create new dependency collectors,
475 // and thus won't write out the extra '.d' files to disk.
476 ScanInvocation->getDependencyOutputOpts() = {};
477
478 Controller.initializeScanInvocation(ScanInvocation&: *ScanInvocation);
479
480 return ScanInvocation;
481}
482
483llvm::SmallVector<StringRef>
484dependencies::getInitialStableDirs(const CompilerInstance &ScanInstance) {
485 // Create a collection of stable directories derived from the ScanInstance
486 // for determining whether module dependencies would fully resolve from
487 // those directories.
488 llvm::SmallVector<StringRef> StableDirs;
489 const StringRef Sysroot = ScanInstance.getHeaderSearchOpts().Sysroot;
490 if (!Sysroot.empty() && (llvm::sys::path::root_directory(path: Sysroot) != Sysroot))
491 StableDirs = {Sysroot, ScanInstance.getHeaderSearchOpts().ResourceDir};
492 return StableDirs;
493}
494
495std::optional<PrebuiltModulesAttrsMap>
496dependencies::computePrebuiltModulesASTMap(
497 CompilerInstance &ScanInstance, llvm::SmallVector<StringRef> &StableDirs) {
498 // Store a mapping of prebuilt module files and their properties like header
499 // search options. This will prevent the implicit build to create duplicate
500 // modules and will force reuse of the existing prebuilt module files
501 // instead.
502 PrebuiltModulesAttrsMap PrebuiltModulesASTMap;
503
504 if (!ScanInstance.getPreprocessorOpts().ImplicitPCHInclude.empty())
505 if (visitPrebuiltModule(
506 PrebuiltModuleFilename: ScanInstance.getPreprocessorOpts().ImplicitPCHInclude, CI&: ScanInstance,
507 ModuleFiles&: ScanInstance.getHeaderSearchOpts().PrebuiltModuleFiles,
508 PrebuiltModulesASTMap, Diags&: ScanInstance.getDiagnostics(), StableDirs))
509 return {};
510
511 return PrebuiltModulesASTMap;
512}
513
514std::unique_ptr<DependencyOutputOptions>
515dependencies::createDependencyOutputOptions(
516 const CompilerInvocation &Invocation) {
517 auto Opts = std::make_unique<DependencyOutputOptions>(
518 args: Invocation.getDependencyOutputOpts());
519 // We need at least one -MT equivalent for the generator of make dependency
520 // files to work.
521 if (Opts->Targets.empty())
522 Opts->Targets = {deduceDepTarget(OutputFile: Invocation.getFrontendOpts().OutputFile,
523 InputFiles: Invocation.getFrontendOpts().Inputs)};
524 Opts->IncludeSystemHeaders = true;
525
526 return Opts;
527}
528
529std::shared_ptr<ModuleDepCollector>
530dependencies::initializeScanInstanceDependencyCollector(
531 CompilerInstance &ScanInstance,
532 std::unique_ptr<DependencyOutputOptions> DepOutputOpts,
533 StringRef WorkingDirectory, DependencyConsumer &Consumer,
534 DependencyScanningService &Service, CompilerInvocation &Inv,
535 DependencyActionController &Controller,
536 PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
537 llvm::SmallVector<StringRef> &StableDirs) {
538 std::shared_ptr<ModuleDepCollector> MDC;
539 switch (Service.getOpts().Format) {
540 case ScanningOutputFormat::Make:
541 ScanInstance.addDependencyCollector(
542 Listener: std::make_shared<DependencyConsumerForwarder>(
543 args: std::move(DepOutputOpts), args&: WorkingDirectory, args&: Consumer));
544 break;
545 case ScanningOutputFormat::P1689:
546 case ScanningOutputFormat::Full:
547 MDC = std::make_shared<ModuleDepCollector>(
548 args&: Service, args: std::move(DepOutputOpts), args&: ScanInstance, args&: Consumer, args&: Controller,
549 args&: Inv, args: std::move(PrebuiltModulesASTMap), args&: StableDirs);
550 ScanInstance.addDependencyCollector(Listener: MDC);
551 break;
552 }
553
554 return MDC;
555}
556
557/// Manages (and terminates) the asynchronous compilation of modules.
558class AsyncModuleCompiles {
559 std::mutex Mutex;
560 bool Stop = false;
561 // FIXME: Have the service own a thread pool and use that instead.
562 std::vector<std::thread> Compiles;
563
564public:
565 /// Registers the module compilation, unless this instance is about to be
566 /// destroyed.
567 void add(llvm::unique_function<void()> Compile) {
568 std::lock_guard<std::mutex> Lock(Mutex);
569 if (!Stop)
570 Compiles.emplace_back(args: std::move(Compile));
571 }
572
573 ~AsyncModuleCompiles() {
574 {
575 // Prevent registration of further module compiles.
576 std::lock_guard<std::mutex> Lock(Mutex);
577 Stop = true;
578 }
579
580 // Wait for outstanding module compiles to finish.
581 for (std::thread &Compile : Compiles)
582 Compile.join();
583 }
584};
585
586struct SingleModuleWithAsyncModuleCompiles : PreprocessOnlyAction {
587 DependencyScanningService &Service;
588 DependencyActionController &Controller;
589 AsyncModuleCompiles &Compiles;
590
591 SingleModuleWithAsyncModuleCompiles(DependencyScanningService &Service,
592 DependencyActionController &Controller,
593 AsyncModuleCompiles &Compiles)
594 : Service(Service), Controller(Controller), Compiles(Compiles) {}
595
596 bool BeginSourceFileAction(CompilerInstance &CI) override;
597};
598
599/// The preprocessor callback that takes care of initiating an asynchronous
600/// module compilation if needed.
601struct AsyncModuleCompile : PPCallbacks {
602 CompilerInstance &CI;
603 DependencyScanningService &Service;
604 DependencyActionController &Controller;
605 AsyncModuleCompiles &Compiles;
606
607 AsyncModuleCompile(CompilerInstance &CI, DependencyScanningService &Service,
608 DependencyActionController &Controller,
609 AsyncModuleCompiles &Compiles)
610 : CI(CI), Service(Service), Controller(Controller), Compiles(Compiles) {}
611
612 void moduleLoadSkipped(Module *M) override {
613 M = M->getTopLevelModule();
614
615 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
616 ModuleCache &ModCache = CI.getModuleCache();
617 std::string ModuleFileName = HS.getCachedModuleFileName(Module: M);
618
619 uint64_t Timestamp = ModCache.getModuleTimestamp(ModuleFilename: ModuleFileName);
620 // Someone else already built/validated the PCM.
621 if (Timestamp > CI.getHeaderSearchOpts().BuildSessionTimestamp)
622 return;
623
624 if (!CI.getASTReader())
625 CI.createASTReader();
626 SmallVector<ASTReader::ImportedModule, 0> Imported;
627 // Only calling ReadASTCore() to avoid the expensive eager deserialization
628 // of the clang::Module objects in ReadAST().
629 // FIXME: Consider doing this in the new thread depending on how expensive
630 // the read turns out to be.
631 switch (CI.getASTReader()->ReadASTCore(
632 FileName: ModuleFileName, Type: serialization::MK_ImplicitModule, ImportLoc: SourceLocation(),
633 ImportedBy: nullptr, Loaded&: Imported, ExpectedSize: {}, ExpectedModTime: {}, ExpectedSignature: {},
634 ClientLoadCapabilities: ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing |
635 ASTReader::ARR_TreatModuleWithErrorsAsOutOfDate)) {
636 case ASTReader::Success:
637 // We successfully read a valid, up-to-date PCM.
638 // FIXME: This could update the timestamp. Regular calls to
639 // ASTReader::ReadAST() would do so unless they encountered corrupted
640 // AST block, corrupted extension block, or did not read the expected
641 // top-level module.
642 return;
643 case ASTReader::OutOfDate:
644 case ASTReader::Missing:
645 // The most interesting case.
646 break;
647 default:
648 // Let the regular scan diagnose this.
649 return;
650 }
651
652 ModCache.prepareForGetLock(ModuleFilename: ModuleFileName);
653 auto Lock = ModCache.getLock(ModuleFilename: ModuleFileName);
654 bool Owned;
655 llvm::Error LockErr = Lock->tryLock().moveInto(Value&: Owned);
656 // Someone else is building the PCM right now.
657 if (!LockErr && !Owned)
658 return;
659 // We should build the PCM.
660 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
661 llvm::makeIntrusiveRefCnt<DependencyScanningWorkerFilesystem>(
662 A&: Service.getSharedCache(), A: Service.getOpts().MakeVFS());
663 VFS = createVFSFromCompilerInvocation(CI: CI.getInvocation(),
664 Diags&: CI.getDiagnostics(), BaseFS: std::move(VFS));
665 auto DC = std::make_unique<DiagnosticConsumer>();
666 auto MC = makeInProcessModuleCache(Entries&: Service.getModuleCacheEntries());
667 CompilerInstance::ThreadSafeCloneConfig CloneConfig(std::move(VFS), *DC,
668 std::move(MC));
669 auto ModCI1 = CI.cloneForModuleCompile(ImportLoc: SourceLocation(), Module: M, ModuleFileName,
670 ThreadSafeConfig: CloneConfig);
671 auto ModCI2 = CI.cloneForModuleCompile(ImportLoc: SourceLocation(), Module: M, ModuleFileName,
672 ThreadSafeConfig: CloneConfig);
673
674 auto ModController = Controller.clone();
675
676 // Note: This lock belongs to a module cache that might not outlive the
677 // thread. This works, because the in-process lock only refers to an object
678 // managed by the service, which does outlive the thread.
679 Compiles.add(Compile: [Lock = std::move(Lock), ModCI1 = std::move(ModCI1),
680 ModCI2 = std::move(ModCI2), DC = std::move(DC),
681 ModController = std::move(ModController), Service = &Service,
682 Compiles = &Compiles] {
683 llvm::CrashRecoveryContext CRC;
684 (void)CRC.RunSafely(Fn: [&] {
685 // Quickly discovers and compiles modules for the real scan below.
686 SingleModuleWithAsyncModuleCompiles Action1(*Service, *ModController,
687 *Compiles);
688 (void)ModCI1->ExecuteAction(Act&: Action1);
689 // The real scan below.
690 ModCI2->getPreprocessorOpts().SingleModuleParseMode = false;
691 GenerateModuleFromModuleMapAction Action2;
692 (void)ModCI2->ExecuteAction(Act&: Action2);
693 });
694 });
695 }
696};
697
698/// Runs the preprocessor on a TU with single-module-parse-mode and compiles
699/// modules asynchronously without blocking or importing them.
700struct SingleTUWithAsyncModuleCompiles : PreprocessOnlyAction {
701 DependencyScanningService &Service;
702 DependencyActionController &Controller;
703 AsyncModuleCompiles &Compiles;
704
705 SingleTUWithAsyncModuleCompiles(DependencyScanningService &Service,
706 DependencyActionController &Controller,
707 AsyncModuleCompiles &Compiles)
708 : Service(Service), Controller(Controller), Compiles(Compiles) {}
709
710 bool BeginSourceFileAction(CompilerInstance &CI) override {
711 CI.getInvocation().getPreprocessorOpts().SingleModuleParseMode = true;
712 CI.getPreprocessor().addPPCallbacks(C: std::make_unique<AsyncModuleCompile>(
713 args&: CI, args&: Service, args&: Controller, args&: Compiles));
714 return true;
715 }
716};
717
718bool SingleModuleWithAsyncModuleCompiles::BeginSourceFileAction(
719 CompilerInstance &CI) {
720 CI.getInvocation().getPreprocessorOpts().SingleModuleParseMode = true;
721 CI.getPreprocessor().addPPCallbacks(
722 C: std::make_unique<AsyncModuleCompile>(args&: CI, args&: Service, args&: Controller, args&: Compiles));
723 return true;
724}
725
726bool DependencyScanningAction::runInvocation(
727 std::string Executable,
728 std::unique_ptr<CompilerInvocation> OriginalInvocation,
729 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
730 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
731 DiagnosticConsumer *DiagConsumer) {
732 // Making sure that we canonicalize the defines early to avoid unnecessary
733 // variants in both the scanner and in the resulting explicit command lines.
734 if (any(Val: Service.getOpts().OptimizeArgs & ScanningOptimizations::Macros))
735 canonicalizeDefines(PPOpts&: OriginalInvocation->getPreprocessorOpts());
736
737 if (Scanned) {
738 CompilerInstance &ScanInstance = *ScanInstanceStorage;
739
740 // Scanning runs once for the first -cc1 invocation in a chain of driver
741 // jobs. For any dependent jobs, reuse the scanning result and just
742 // update the new invocation.
743 // FIXME: to support multi-arch builds, each arch requires a separate scan
744 if (MDC)
745 MDC->applyDiscoveredDependencies(CI&: *OriginalInvocation);
746
747 if (!Controller.finalize(ScanInstance, NewInvocation&: *OriginalInvocation))
748 return false;
749
750 Consumer.handleBuildCommand(
751 Cmd: {.Executable: Executable, .Arguments: OriginalInvocation->getCC1CommandLine()});
752 return true;
753 }
754
755 Scanned = true;
756
757 // Create a compiler instance to handle the actual work.
758 auto ScanInvocation =
759 createScanCompilerInvocation(Invocation: *OriginalInvocation, Service, Controller);
760
761 // Quickly discovers and compiles modules for the real scan below.
762 std::optional<AsyncModuleCompiles> AsyncCompiles;
763 if (Service.getOpts().AsyncScanModules) {
764 auto ModCache = makeInProcessModuleCache(Entries&: Service.getModuleCacheEntries());
765 auto ScanInstanceStorage = std::make_unique<CompilerInstance>(
766 args: std::make_shared<CompilerInvocation>(args&: *ScanInvocation), args&: PCHContainerOps,
767 args: std::move(ModCache));
768 CompilerInstance &ScanInstance = *ScanInstanceStorage;
769
770 DiagnosticConsumer DiagConsumer;
771 initializeScanCompilerInstance(ScanInstance, FS, DiagConsumer: &DiagConsumer, Service,
772 DepFS);
773
774 // FIXME: Do this only once.
775 SmallVector<StringRef> StableDirs = getInitialStableDirs(ScanInstance);
776 auto MaybePrebuiltModulesASTMap =
777 computePrebuiltModulesASTMap(ScanInstance, StableDirs);
778 if (!MaybePrebuiltModulesASTMap)
779 return false;
780
781 // Normally this would be handled by GeneratePCHAction
782 if (ScanInstance.getFrontendOpts().ProgramAction == frontend::GeneratePCH)
783 ScanInstance.getLangOpts().CompilingPCH = true;
784
785 AsyncCompiles.emplace();
786 SingleTUWithAsyncModuleCompiles Action(Service, Controller, *AsyncCompiles);
787 (void)ScanInstance.ExecuteAction(Act&: Action);
788 }
789
790 auto ModCache = makeInProcessModuleCache(Entries&: Service.getModuleCacheEntries());
791 ScanInstanceStorage.emplace(args: std::move(ScanInvocation),
792 args: std::move(PCHContainerOps), args: std::move(ModCache));
793 CompilerInstance &ScanInstance = *ScanInstanceStorage;
794
795 initializeScanCompilerInstance(ScanInstance, FS, DiagConsumer, Service,
796 DepFS);
797
798 llvm::SmallVector<StringRef> StableDirs = getInitialStableDirs(ScanInstance);
799 auto MaybePrebuiltModulesASTMap =
800 computePrebuiltModulesASTMap(ScanInstance, StableDirs);
801 if (!MaybePrebuiltModulesASTMap)
802 return false;
803
804 auto DepOutputOpts = createDependencyOutputOptions(Invocation: *OriginalInvocation);
805
806 MDC = initializeScanInstanceDependencyCollector(
807 ScanInstance, DepOutputOpts: std::move(DepOutputOpts), WorkingDirectory, Consumer,
808 Service, Inv&: *OriginalInvocation, Controller, PrebuiltModulesASTMap: *MaybePrebuiltModulesASTMap,
809 StableDirs);
810
811 if (ScanInstance.getDiagnostics().hasErrorOccurred())
812 return false;
813
814 if (!Controller.initialize(ScanInstance, NewInvocation&: *OriginalInvocation))
815 return false;
816
817 ReadPCHAndPreprocessAction Action;
818 const bool Result = ScanInstance.ExecuteAction(Act&: Action);
819
820 if (Result) {
821 if (MDC)
822 MDC->applyDiscoveredDependencies(CI&: *OriginalInvocation);
823
824 if (!Controller.finalize(ScanInstance, NewInvocation&: *OriginalInvocation))
825 return false;
826
827 Consumer.handleBuildCommand(
828 Cmd: {.Executable: Executable, .Arguments: OriginalInvocation->getCC1CommandLine()});
829 }
830
831 return Result;
832}
833