clang 23.0.0git
DependencyScannerImpl.cpp
Go to the documentation of this file.
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
16#include "llvm/ADT/IntrusiveRefCntPtr.h"
17#include "llvm/ADT/ScopeExit.h"
18#include "llvm/Option/Option.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);
41 llvm::SmallString<256> CanonPath;
42 for (const auto &File : getDependencies()) {
43 CanonPath = File;
44 llvm::sys::path::remove_dots(CanonPath, /*remove_dot_dot=*/true);
45 llvm::sys::path::make_absolute(WorkingDirectory, CanonPath);
46 C.handleFileDependency(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(diag::warn_pch_vfsoverlay_mismatch);
64 auto VFSNote = [&](int Type, ArrayRef<std::string> VFSOverlays) {
65 if (VFSOverlays.empty()) {
66 Diags->Report(diag::note_pch_vfsoverlay_empty) << Type;
67 } else {
68 std::string Files = llvm::join(VFSOverlays, "\n");
69 Diags->Report(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({ModuleName.str(), Filename.str()}).second)
106 NewModuleFiles.push_back(Filename.str());
107
108 auto PrebuiltMapEntry = PrebuiltModulesASTMap.try_emplace(Filename);
109 PrebuiltModuleASTAttrs &PrebuiltModule = PrebuiltMapEntry.first->second;
110 if (PrebuiltMapEntry.second)
111 PrebuiltModule.setInStableDir(!StableDirs.empty());
112
113 if (auto It = PrebuiltModulesASTMap.find(CurrentFile);
114 It != PrebuiltModulesASTMap.end() && CurrentFile != Filename)
115 PrebuiltModule.addDependent(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(CurrentFile);
128 if ((PrebuiltEntryIt == PrebuiltModulesASTMap.end()) ||
129 (!PrebuiltEntryIt->second.isInStableDir()))
130 return false;
131
132 PrebuiltEntryIt->second.setInStableDir(
133 isPathInStableDir(StableDirs, Filename));
134 return PrebuiltEntryIt->second.isInStableDir();
135 }
136
137 /// Update which module that is being actively traversed.
138 void visitModuleFile(ModuleFileName Filename, serialization::ModuleKind Kind,
139 bool DirectlyImported) override {
140 // If the CurrentFile is not
141 // considered stable, update any of it's transitive dependents.
142 auto PrebuiltEntryIt = PrebuiltModulesASTMap.find(CurrentFile);
143 if ((PrebuiltEntryIt != PrebuiltModulesASTMap.end()) &&
144 !PrebuiltEntryIt->second.isInStableDir())
145 PrebuiltEntryIt->second.updateDependentsNotInStableDirs(
146 PrebuiltModulesASTMap);
147 CurrentFile = Filename.str();
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(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(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(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, Complain ? &Diags : nullptr, 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,
198 PrebuiltModuleFilesT &ModuleFiles,
199 PrebuiltModulesAttrsMap &PrebuiltModulesASTMap,
200 DiagnosticsEngine &Diags,
201 const ArrayRef<StringRef> StableDirs) {
202 // List of module files to be processed.
204
205 PrebuiltModuleListener Listener(ModuleFiles, Worklist, PrebuiltModulesASTMap,
207 Diags, StableDirs);
208
209 Listener.visitModuleFile(ModuleFileName::makeExplicit(PrebuiltModuleFilename),
211 /*DirectlyImported=*/true);
213 PrebuiltModuleFilename, CI.getFileManager(), CI.getModuleCache(),
215 /*FindModuleFileExtensions=*/false, Listener,
216 /*ValidateDiagnosticOptions=*/false, ASTReader::ARR_OutOfDate))
217 return true;
218
219 while (!Worklist.empty()) {
220 // FIXME: This is assuming the PCH only refers to explicitly-built modules,
221 // which technically is not guaranteed. To remove the assumption, we'd need
222 // to also rework how the module files are handled to the scan, specifically
223 // change the values of HeaderSearchOptions::PrebuiltModuleFiles from plain
224 // paths to ModuleFileName.
225 Listener.visitModuleFile(ModuleFileName::makeExplicit(Worklist.back()),
227 /*DirectlyImported=*/false);
229 Worklist.pop_back_val(), CI.getFileManager(), CI.getModuleCache(),
231 /*FindModuleFileExtensions=*/false, Listener,
232 /*ValidateDiagnosticOptions=*/false))
233 return true;
234 }
235 return false;
236}
237
238/// Transform arbitrary file name into an object-like file name.
239static std::string makeObjFileName(StringRef FileName) {
240 SmallString<128> ObjFileName(FileName);
241 llvm::sys::path::replace_extension(ObjFileName, "o");
242 return std::string(ObjFileName);
243}
244
245/// Deduce the dependency target based on the output file and input files.
246static std::string
247deduceDepTarget(const std::string &OutputFile,
248 const SmallVectorImpl<FrontendInputFile> &InputFiles) {
249 if (OutputFile != "-")
250 return OutputFile;
251
252 if (InputFiles.empty() || !InputFiles.front().isFile())
253 return "clang-scan-deps\\ dependency";
254
255 return makeObjFileName(InputFiles.front().getFile());
256}
257
258// Clang implements -D and -U by splatting text into a predefines buffer. This
259// allows constructs such as `-DFඞ=3 "-D F\u{0D9E} 4 3 2”` to be accepted and
260// define the same macro, or adding C++ style comments before the macro name.
261//
262// This function checks that the first non-space characters in the macro
263// obviously form an identifier that can be uniqued on without lexing. Failing
264// to do this could lead to changing the final definition of a macro.
265//
266// We could set up a preprocessor and actually lex the name, but that's very
267// heavyweight for a situation that will almost never happen in practice.
268static std::optional<StringRef> getSimpleMacroName(StringRef Macro) {
269 StringRef Name = Macro.split("=").first.ltrim(" \t");
270 std::size_t I = 0;
271
272 auto FinishName = [&]() -> std::optional<StringRef> {
273 StringRef SimpleName = Name.slice(0, I);
274 if (SimpleName.empty())
275 return std::nullopt;
276 return SimpleName;
277 };
278
279 for (; I != Name.size(); ++I) {
280 switch (Name[I]) {
281 case '(': // Start of macro parameter list
282 case ' ': // End of macro name
283 case '\t':
284 return FinishName();
285 case '_':
286 continue;
287 default:
288 if (llvm::isAlnum(Name[I]))
289 continue;
290 return std::nullopt;
291 }
292 }
293 return FinishName();
294}
295} // namespace
296
298 using MacroOpt = std::pair<StringRef, std::size_t>;
299 std::vector<MacroOpt> SimpleNames;
300 SimpleNames.reserve(PPOpts.Macros.size());
301 std::size_t Index = 0;
302 for (const auto &M : PPOpts.Macros) {
303 auto SName = getSimpleMacroName(M.first);
304 // Skip optimizing if we can't guarantee we can preserve relative order.
305 if (!SName)
306 return;
307 SimpleNames.emplace_back(*SName, Index);
308 ++Index;
309 }
310
311 llvm::stable_sort(SimpleNames, llvm::less_first());
312 // Keep the last instance of each macro name by going in reverse
313 auto NewEnd = std::unique(
314 SimpleNames.rbegin(), SimpleNames.rend(),
315 [](const MacroOpt &A, const MacroOpt &B) { return A.first == B.first; });
316 SimpleNames.erase(SimpleNames.begin(), NewEnd.base());
317
318 // Apply permutation.
319 decltype(PPOpts.Macros) NewMacros;
320 NewMacros.reserve(SimpleNames.size());
321 for (std::size_t I = 0, E = SimpleNames.size(); I != E; ++I) {
322 std::size_t OriginalIndex = SimpleNames[I].second;
323 // We still emit undefines here as they may be undefining a predefined macro
324 NewMacros.push_back(std::move(PPOpts.Macros[OriginalIndex]));
325 }
326 std::swap(PPOpts.Macros, NewMacros);
327}
328
329namespace {
330class ScanningDependencyDirectivesGetter : public DependencyDirectivesGetter {
332
333public:
334 ScanningDependencyDirectivesGetter(FileManager &FileMgr) : DepFS(nullptr) {
335 FileMgr.getVirtualFileSystem().visit([&](llvm::vfs::FileSystem &FS) {
336 auto *DFS = llvm::dyn_cast<DependencyScanningWorkerFilesystem>(&FS);
337 if (DFS) {
338 assert(!DepFS && "Found multiple scanning VFSs");
339 DepFS = DFS;
340 }
341 });
342 assert(DepFS && "Did not find scanning VFS");
343 }
344
345 std::unique_ptr<DependencyDirectivesGetter>
346 cloneFor(FileManager &FileMgr) override {
347 return std::make_unique<ScanningDependencyDirectivesGetter>(FileMgr);
348 }
349
350 std::optional<ArrayRef<dependency_directives_scan::Directive>>
351 operator()(FileEntryRef File) override {
352 return DepFS->getDirectiveTokens(File.getName());
353 }
354};
355
356/// Sanitize diagnostic options for dependency scan.
357void sanitizeDiagOpts(DiagnosticOptions &DiagOpts) {
358 // Don't print 'X warnings and Y errors generated'.
359 DiagOpts.ShowCarets = false;
360 // Don't write out diagnostic file.
361 DiagOpts.DiagnosticSerializationFile.clear();
362 // Don't emit warnings except for scanning specific warnings.
363 // TODO: It would be useful to add a more principled way to ignore all
364 // warnings that come from source code. The issue is that we need to
365 // ignore warnings that could be surpressed by
366 // `#pragma clang diagnostic`, while still allowing some scanning
367 // warnings for things we're not ready to turn into errors yet.
368 // See `test/ClangScanDeps/diagnostic-pragmas.c` for an example.
369 llvm::erase_if(DiagOpts.Warnings, [](StringRef Warning) {
370 return llvm::StringSwitch<bool>(Warning)
371 .Cases({"pch-vfs-diff", "error=pch-vfs-diff"}, false)
372 .StartsWith("no-error=", false)
373 .Default(true);
374 });
375}
376} // namespace
377
378std::unique_ptr<DiagnosticOptions>
380 std::vector<const char *> CLI;
381 for (const std::string &Arg : CommandLine)
382 CLI.push_back(Arg.c_str());
383 auto DiagOpts = CreateAndPopulateDiagOpts(CLI);
384 sanitizeDiagOpts(*DiagOpts);
385 return DiagOpts;
386}
387
389 ArrayRef<std::string> CommandLine,
391 std::vector<const char *> CCommandLine(CommandLine.size(), nullptr);
392 llvm::transform(CommandLine, CCommandLine.begin(),
393 [](const std::string &Str) { return Str.c_str(); });
394 DiagOpts = CreateAndPopulateDiagOpts(CCommandLine);
395 sanitizeDiagOpts(*DiagOpts);
397 /*ShouldOwnClient=*/false);
398}
399
400std::unique_ptr<CompilerInvocation>
402 DiagnosticsEngine &Diags) {
403 llvm::opt::ArgStringList Argv;
404 for (const std::string &Str : ArrayRef(CommandLine).drop_front())
405 Argv.push_back(Str.c_str());
406
407 auto Invocation = std::make_unique<CompilerInvocation>();
408 if (!CompilerInvocation::CreateFromArgs(*Invocation, Argv, Diags)) {
409 // FIXME: Should we just go on like cc1_main does?
410 return nullptr;
411 }
412 return Invocation;
413}
414
416 CompilerInstance &ScanInstance,
418 DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
420 ScanInstance.setBuildingModule(false);
421 ScanInstance.createVirtualFileSystem(FS, DiagConsumer);
422 ScanInstance.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
424 ScanInstance.getDiagnostics().setIgnoreAllWarnings(true);
425 ScanInstance.createFileManager();
426 ScanInstance.createSourceManager();
427
428 // Use DepFS for getting the dependency directives if requested to do so.
431 std::make_unique<ScanningDependencyDirectivesGetter>(
432 ScanInstance.getFileManager()));
433}
434
435std::shared_ptr<CompilerInvocation> dependencies::createScanCompilerInvocation(
436 const CompilerInvocation &Invocation,
437 const DependencyScanningService &Service,
438 DependencyActionController &Controller) {
439 auto ScanInvocation = std::make_shared<CompilerInvocation>(Invocation);
440
441 sanitizeDiagOpts(ScanInvocation->getDiagnosticOpts());
442
443 ScanInvocation->getPreprocessorOpts().AllowPCHWithDifferentModulesCachePath =
444 true;
445
446 if (ScanInvocation->getHeaderSearchOpts().ModulesValidateOncePerBuildSession)
447 ScanInvocation->getHeaderSearchOpts().BuildSessionTimestamp =
449
450 ScanInvocation->getFrontendOpts().DisableFree = false;
451 ScanInvocation->getFrontendOpts().GenerateGlobalModuleIndex = false;
452 ScanInvocation->getFrontendOpts().UseGlobalModuleIndex = false;
453 ScanInvocation->getFrontendOpts().GenReducedBMI = false;
454 ScanInvocation->getFrontendOpts().ModuleOutputPath.clear();
455 // This will prevent us compiling individual modules asynchronously since
456 // FileManager is not thread-safe, but it does improve performance for now.
457 ScanInvocation->getFrontendOpts().ModulesShareFileManager = true;
458 ScanInvocation->getHeaderSearchOpts().ModuleFormat = "raw";
459 ScanInvocation->getHeaderSearchOpts().ModulesIncludeVFSUsage =
461
462 // Consider different header search and diagnostic options to create
463 // different modules. This avoids the unsound aliasing of module PCMs.
464 //
465 // TODO: Implement diagnostic bucketing to reduce the impact of strict
466 // context hashing.
467 ScanInvocation->getHeaderSearchOpts().ModulesStrictContextHash = true;
468 ScanInvocation->getHeaderSearchOpts().ModulesSerializeOnlyPreprocessor = true;
469 ScanInvocation->getHeaderSearchOpts().ModulesSkipDiagnosticOptions = true;
470 ScanInvocation->getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = true;
471 ScanInvocation->getHeaderSearchOpts().ModulesSkipPragmaDiagnosticMappings =
472 true;
473 ScanInvocation->getHeaderSearchOpts().ModulesForceValidateUserHeaders = false;
474
475 // Ensure that the scanner does not create new dependency collectors,
476 // and thus won't write out the extra '.d' files to disk.
477 ScanInvocation->getDependencyOutputOpts() = {};
478
479 Controller.initializeScanInvocation(*ScanInvocation);
480
481 return ScanInvocation;
482}
483
486 // Create a collection of stable directories derived from the ScanInstance
487 // for determining whether module dependencies would fully resolve from
488 // those directories.
490 const StringRef Sysroot = ScanInstance.getHeaderSearchOpts().Sysroot;
491 if (!Sysroot.empty() && (llvm::sys::path::root_directory(Sysroot) != Sysroot))
492 StableDirs = {Sysroot, ScanInstance.getHeaderSearchOpts().ResourceDir};
493 return StableDirs;
494}
495
496std::optional<PrebuiltModulesAttrsMap>
498 CompilerInstance &ScanInstance, llvm::SmallVector<StringRef> &StableDirs) {
499 // Store a mapping of prebuilt module files and their properties like header
500 // search options. This will prevent the implicit build to create duplicate
501 // modules and will force reuse of the existing prebuilt module files
502 // instead.
503 PrebuiltModulesAttrsMap PrebuiltModulesASTMap;
504
505 if (!ScanInstance.getPreprocessorOpts().ImplicitPCHInclude.empty())
506 if (visitPrebuiltModule(
507 ScanInstance.getPreprocessorOpts().ImplicitPCHInclude, ScanInstance,
509 PrebuiltModulesASTMap, ScanInstance.getDiagnostics(), StableDirs))
510 return {};
511
512 return PrebuiltModulesASTMap;
513}
514
515std::unique_ptr<DependencyOutputOptions>
517 const CompilerInvocation &Invocation) {
518 auto Opts = std::make_unique<DependencyOutputOptions>(
519 Invocation.getDependencyOutputOpts());
520 // We need at least one -MT equivalent for the generator of make dependency
521 // files to work.
522 if (Opts->Targets.empty())
523 Opts->Targets = {deduceDepTarget(Invocation.getFrontendOpts().OutputFile,
524 Invocation.getFrontendOpts().Inputs)};
525 Opts->IncludeSystemHeaders = true;
526
527 return Opts;
528}
529
530std::shared_ptr<ModuleDepCollector>
532 CompilerInstance &ScanInstance,
533 std::unique_ptr<DependencyOutputOptions> DepOutputOpts,
534 StringRef WorkingDirectory, DependencyConsumer &Consumer,
536 DependencyActionController &Controller,
537 PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
538 llvm::SmallVector<StringRef> &StableDirs) {
539 std::shared_ptr<ModuleDepCollector> MDC;
540 switch (Service.getOpts().Format) {
542 ScanInstance.addDependencyCollector(
543 std::make_shared<DependencyConsumerForwarder>(
544 std::move(DepOutputOpts), WorkingDirectory, Consumer));
545 break;
548 MDC = std::make_shared<ModuleDepCollector>(
549 Service, std::move(DepOutputOpts), ScanInstance, Consumer, Controller,
550 Inv, std::move(PrebuiltModulesASTMap), StableDirs);
551 ScanInstance.addDependencyCollector(MDC);
552 break;
553 }
554
555 return MDC;
556}
557
558/// Manages (and terminates) the asynchronous compilation of modules.
560 std::mutex Mutex;
561 bool Stop = false;
562 // FIXME: Have the service own a thread pool and use that instead.
563 std::vector<std::thread> Compiles;
564
565public:
566 /// Registers the module compilation, unless this instance is about to be
567 /// destroyed.
568 void add(llvm::unique_function<void()> Compile) {
569 std::lock_guard<std::mutex> Lock(Mutex);
570 if (!Stop)
571 Compiles.emplace_back(std::move(Compile));
572 }
573
575 {
576 // Prevent registration of further module compiles.
577 std::lock_guard<std::mutex> Lock(Mutex);
578 Stop = true;
579 }
580
581 // Wait for outstanding module compiles to finish.
582 for (std::thread &Compile : Compiles)
583 Compile.join();
584 }
585};
586
599
600/// The preprocessor callback that takes care of initiating an asynchronous
601/// module compilation if needed.
607
612
613 void moduleLoadSkipped(Module *M) override {
614 M = M->getTopLevelModule();
615
616 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
617 ModuleCache &ModCache = CI.getModuleCache();
619
620 uint64_t Timestamp = ModCache.getModuleTimestamp(ModuleFileName);
621 // Someone else already built/validated the PCM.
622 if (Timestamp > CI.getHeaderSearchOpts().BuildSessionTimestamp)
623 return;
624
625 if (!CI.getASTReader())
626 CI.createASTReader();
628 // Only calling ReadASTCore() to avoid the expensive eager deserialization
629 // of the clang::Module objects in ReadAST().
630 // FIXME: Consider doing this in the new thread depending on how expensive
631 // the read turns out to be.
632 switch (CI.getASTReader()->ReadASTCore(
634 nullptr, Imported, {}, {}, {},
638 // We successfully read a valid, up-to-date PCM.
639 // FIXME: This could update the timestamp. Regular calls to
640 // ASTReader::ReadAST() would do so unless they encountered corrupted
641 // AST block, corrupted extension block, or did not read the expected
642 // top-level module.
643 return;
646 // The most interesting case.
647 break;
648 default:
649 // Let the regular scan diagnose this.
650 return;
651 }
652
653 auto Lock = ModCache.getLock(ModuleFileName);
654 bool Owned;
655 llvm::Error LockErr = Lock->tryLock().moveInto(Owned);
656 // Someone else is building the PCM right now.
657 if (!LockErr && !Owned)
658 return;
659 // We should build the PCM.
661 llvm::makeIntrusiveRefCnt<DependencyScanningWorkerFilesystem>(
662 Service.getSharedCache(), Service.getOpts().MakeVFS());
663 VFS = createVFSFromCompilerInvocation(CI.getInvocation(),
664 CI.getDiagnostics(), std::move(VFS));
665 auto DC = std::make_unique<DiagnosticConsumer>();
666 auto MC = makeInProcessModuleCache(Service.getModuleCacheEntries());
667 CompilerInstance::ThreadSafeCloneConfig CloneConfig(std::move(VFS), *DC,
668 std::move(MC));
669 auto ModCI1 = CI.cloneForModuleCompile(SourceLocation(), M, ModuleFileName,
670 CloneConfig);
671 auto ModCI2 = CI.cloneForModuleCompile(SourceLocation(), M, ModuleFileName,
672 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([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([&] {
685 // Quickly discovers and compiles modules for the real scan below.
686 SingleModuleWithAsyncModuleCompiles Action1(*Service, *ModController,
687 *Compiles);
688 (void)ModCI1->ExecuteAction(Action1);
689 // The real scan below.
690 ModCI2->getPreprocessorOpts().SingleModuleParseMode = false;
691 GenerateModuleFromModuleMapAction Action2;
692 (void)ModCI2->ExecuteAction(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.
717
719 CompilerInstance &CI) {
722 std::make_unique<AsyncModuleCompile>(CI, Service, Controller, Compiles));
723 return true;
724}
725
727 std::string Executable,
728 std::unique_ptr<CompilerInvocation> OriginalInvocation,
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(Service.getOpts().OptimizeArgs & ScanningOptimizations::Macros))
735 canonicalizeDefines(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(*OriginalInvocation);
746
747 if (!Controller.finalize(ScanInstance, *OriginalInvocation))
748 return false;
749
750 Consumer.handleBuildCommand(
751 {Executable, 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(*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(Service.getModuleCacheEntries());
765 auto ScanInstanceStorage = std::make_unique<CompilerInstance>(
766 std::make_shared<CompilerInvocation>(*ScanInvocation), PCHContainerOps,
767 std::move(ModCache));
768 CompilerInstance &ScanInstance = *ScanInstanceStorage;
769
770 DiagnosticConsumer DiagConsumer;
771 initializeScanCompilerInstance(ScanInstance, FS, &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
783 ScanInstance.getLangOpts().CompilingPCH = true;
784
785 AsyncCompiles.emplace();
786 SingleTUWithAsyncModuleCompiles Action(Service, Controller, *AsyncCompiles);
787 (void)ScanInstance.ExecuteAction(Action);
788 }
789
790 auto ModCache = makeInProcessModuleCache(Service.getModuleCacheEntries());
791 ScanInstanceStorage.emplace(std::move(ScanInvocation),
792 std::move(PCHContainerOps), 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(*OriginalInvocation);
805
807 ScanInstance, std::move(DepOutputOpts), WorkingDirectory, Consumer,
808 Service, *OriginalInvocation, Controller, *MaybePrebuiltModulesASTMap,
809 StableDirs);
810
811 if (ScanInstance.getDiagnostics().hasErrorOccurred())
812 return false;
813
814 if (!Controller.initialize(ScanInstance, *OriginalInvocation))
815 return false;
816
818 const bool Result = ScanInstance.ExecuteAction(Action);
819
820 if (Result) {
821 if (MDC)
822 MDC->applyDiscoveredDependencies(*OriginalInvocation);
823
824 if (!Controller.finalize(ScanInstance, *OriginalInvocation))
825 return false;
826
827 Consumer.handleBuildCommand(
828 {Executable, OriginalInvocation->getCC1CommandLine()});
829 }
830
831 return Result;
832}
Manages (and terminates) the asynchronous compilation of modules.
void add(llvm::unique_function< void()> Compile)
Registers the module compilation, unless this instance is about to be destroyed.
Abstract interface for callback invocations by the ASTReader.
Definition ASTReader.h:117
@ ARR_Missing
The client can handle an AST file that cannot load because it is missing.
Definition ASTReader.h:1854
@ ARR_OutOfDate
The client can handle an AST file that cannot load because it is out-of-date relative to its input fi...
Definition ASTReader.h:1858
@ ARR_TreatModuleWithErrorsAsOutOfDate
If a module file is marked with errors treat it as out-of-date so the caller can rebuild it.
Definition ASTReader.h:1871
static bool readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache, const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions, ASTReaderListener &Listener, bool ValidateDiagnosticOptions, unsigned ClientLoadCapabilities=ARR_ConfigurationMismatch|ARR_OutOfDate)
Read the control block for the named AST file.
@ Success
The control block was read successfully.
Definition ASTReader.h:451
@ OutOfDate
The AST file is out-of-date relative to its input files, and needs to be regenerated.
Definition ASTReader.h:461
@ Missing
The AST file was missing.
Definition ASTReader.h:457
Configuration object for making the result of cloneForModuleCompile() thread-safe.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
void createFileManager()
Create the file manager and replace any existing one with it.
FileManager & getFileManager() const
Return the current file manager to the caller.
ModuleCache & getModuleCache() const
void addDependencyCollector(std::shared_ptr< DependencyCollector > Listener)
Preprocessor & getPreprocessor() const
Return the current preprocessor.
void createVirtualFileSystem(IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS=llvm::vfs::getRealFileSystem(), DiagnosticConsumer *DC=nullptr)
Create a virtual file system instance based on the invocation.
FrontendOptions & getFrontendOpts()
HeaderSearchOptions & getHeaderSearchOpts()
void createSourceManager()
Create the source manager and replace any existing one with it.
CompilerInvocation & getInvocation()
PreprocessorOptions & getPreprocessorOpts()
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
void setDependencyDirectivesGetter(std::unique_ptr< DependencyDirectivesGetter > Getter)
Helper class for holding the data necessary to invoke the compiler.
PreprocessorOptions & getPreprocessorOpts()
static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef< const char * > CommandLineArgs, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Create a compiler invocation from a list of input options.
DependencyOutputOptions & getDependencyOutputOpts()
FrontendOptions & getFrontendOpts()
Functor that returns the dependency directives for a given file.
Builds a dependency file when attached to a Preprocessor (for includes) and ASTReader (for module imp...
Definition Utils.h:105
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Options for controlling the compiler diagnostics engine.
std::vector< std::string > Warnings
The list of -W... options used to alter the diagnostic mappings, with the prefixes removed.
std::string DiagnosticSerializationFile
The file to serialize diagnostics to (non-appending).
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
bool hasErrorOccurred() const
Definition Diagnostic.h:881
void setIgnoreAllWarnings(bool Val)
When set to true, any unmapped warnings are ignored.
Definition Diagnostic.h:689
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:53
std::string OutputFile
The output file, if any.
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
frontend::ActionKind ProgramAction
The frontend action to perform.
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
std::map< std::string, std::string, std::less<> > PrebuiltModuleFiles
The mapping of module names to prebuilt module files.
std::string Sysroot
If non-empty, the directory to use as a "virtual system root" for include paths.
std::vector< std::string > VFSOverlayFiles
The set of user-provided virtual filesystem overlay files.
std::string ResourceDir
The directory which holds the compiler resource files (builtin includes, etc.).
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
ModuleFileName getCachedModuleFileName(Module *Module)
Retrieve the name of the cached module file that should be used to load the given module.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
The module cache used for compiling modules implicitly.
Definition ModuleCache.h:37
virtual std::time_t getModuleTimestamp(StringRef ModuleFilename)=0
Returns the timestamp denoting the last time inputs of the module file were validated.
virtual std::unique_ptr< llvm::AdvisoryLock > getLock(StringRef ModuleFilename)=0
Returns lock for the given module file. The lock is initially unlocked.
Identifies a module file to be loaded.
Definition Module.h:99
static ModuleFileName makeExplicit(std::string Name)
Creates a file name for an explicit module.
Definition Module.h:108
StringRef str() const
Returns the plain module file name.
Definition Module.h:141
void setBuildingModule(bool BuildingModuleFlag)
Flag indicating whether this instance is building a module.
Describes a module or submodule.
Definition Module.h:237
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition Module.h:819
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition PPCallbacks.h:37
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
std::string ImplicitPCHInclude
The implicit PCH included at the start of the translation unit, or empty.
bool SingleModuleParseMode
When enabled, preprocessor is in a mode for parsing a single module only.
std::vector< std::pair< std::string, bool > > Macros
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Preprocessor-based frontend action that also loads PCH files.
Encodes a location in the source.
The base class of the type hierarchy.
Definition TypeBase.h:1871
Dependency scanner callbacks that are used during scanning to influence the behaviour of the scan - f...
virtual void initializeScanInvocation(CompilerInvocation &ScanInvocation)
Initializes the scan invocation.
bool runInvocation(std::string Executable, std::unique_ptr< CompilerInvocation > Invocation, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagConsumer)
The dependency scanning service contains shared configuration and state that is used by the individua...
const DependencyScanningServiceOptions & getOpts() const
A virtual file system optimized for the dependency discovery.
void setVFS(llvm::StringSet<> &&VFS)
Update the VFSMap to the one discovered from serializing the AST file.
bool isInStableDir() const
Read-only access to whether the module is made up of dependencies in stable directories.
void addDependent(StringRef ModuleFile)
Add a direct dependent module file, so it can be updated if the current module is from stable directo...
void setInStableDir(bool V=false)
Update whether the prebuilt module resolves entirely in a stable directories.
SmallVector< StringRef > getInitialStableDirs(const CompilerInstance &ScanInstance)
bool areOptionsInStableDir(const ArrayRef< StringRef > Directories, const HeaderSearchOptions &HSOpts)
Determine if options collected from a module's compilation can safely be considered as stable.
@ VFS
Remove unused -ivfsoverlay arguments.
std::shared_ptr< CompilerInvocation > createScanCompilerInvocation(const CompilerInvocation &Invocation, const DependencyScanningService &Service, DependencyActionController &Controller)
Creates a CompilerInvocation suitable for the dependency scanner.
void initializeScanCompilerInstance(CompilerInstance &ScanInstance, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service, IntrusiveRefCntPtr< DependencyScanningWorkerFilesystem > DepFS)
llvm::StringMap< PrebuiltModuleASTAttrs > PrebuiltModulesAttrsMap
Attributes loaded from AST files of prebuilt modules collected prior to ModuleDepCollector creation.
std::unique_ptr< CompilerInvocation > createCompilerInvocation(ArrayRef< std::string > CommandLine, DiagnosticsEngine &Diags)
void canonicalizeDefines(PreprocessorOptions &PPOpts)
Canonicalizes command-line macro defines (e.g. removing "-DX -UX").
std::unique_ptr< DependencyOutputOptions > createDependencyOutputOptions(const CompilerInvocation &Invocation)
Creates dependency output options to be reported to the dependency consumer, deducing missing informa...
std::optional< PrebuiltModulesAttrsMap > computePrebuiltModulesASTMap(CompilerInstance &ScanInstance, SmallVector< StringRef > &StableDirs)
std::shared_ptr< ModuleCache > makeInProcessModuleCache(ModuleCacheEntries &Entries)
std::shared_ptr< ModuleDepCollector > initializeScanInstanceDependencyCollector(CompilerInstance &ScanInstance, std::unique_ptr< DependencyOutputOptions > DepOutputOpts, StringRef WorkingDirectory, DependencyConsumer &Consumer, DependencyScanningService &Service, CompilerInvocation &Inv, DependencyActionController &Controller, PrebuiltModulesAttrsMap PrebuiltModulesASTMap, llvm::SmallVector< StringRef > &StableDirs)
Create the dependency collector that will collect the produced dependencies.
std::unique_ptr< DiagnosticOptions > createDiagOptions(ArrayRef< std::string > CommandLine)
bool isPathInStableDir(const ArrayRef< StringRef > Directories, const StringRef Input)
Determine if Input can be resolved within a stable directory.
@ Make
This is the Makefile compatible dep format.
@ Full
This outputs the full clang module dependency graph suitable for use for explicitly building modules.
@ P1689
This outputs the dependency graph for standard c++ modules in P1689R5 format.
@ DependencyDirectivesScan
This mode is used to compute the dependencies by running the preprocessor with special kind of lexing...
@ GeneratePCH
Generate pre-compiled header.
ModuleKind
Specifies the kind of module that has been loaded.
Definition ModuleFile.h:44
@ MK_ExplicitModule
File is an explicitly-loaded module.
Definition ModuleFile.h:49
@ MK_ImplicitModule
File is an implicitly-loaded module.
Definition ModuleFile.h:46
The JSON file list parser is used to communicate input to InstallAPI.
std::unique_ptr< DiagnosticOptions > CreateAndPopulateDiagOpts(ArrayRef< const char * > Argv)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Result
The result type of a method or function.
Definition TypeBase.h:905
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
int __ovld __cnfn any(char)
Returns 1 if the most significant bit in any component of x is set; otherwise returns 0.
DependencyActionController & Controller
AsyncModuleCompile(CompilerInstance &CI, DependencyScanningService &Service, DependencyActionController &Controller, AsyncModuleCompiles &Compiles)
void moduleLoadSkipped(Module *M) override
Callback invoked whenever a module load was skipped due to enabled single-module-parse-mode.
AsyncModuleCompiles & Compiles
DependencyScanningService & Service
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
SingleModuleWithAsyncModuleCompiles(DependencyScanningService &Service, DependencyActionController &Controller, AsyncModuleCompiles &Compiles)
Runs the preprocessor on a TU with single-module-parse-mode and compiles modules asynchronously witho...
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
SingleTUWithAsyncModuleCompiles(DependencyScanningService &Service, DependencyActionController &Controller, AsyncModuleCompiles &Compiles)
DependencyScanningService & Service
DependencyActionController & Controller
ScanningMode Mode
Whether to use optimized dependency directive scan or full preprocessing.
std::time_t BuildSessionTimestamp
The build session timestamp for validate-once-per-build-session logic.
ScanningOptimizations OptimizeArgs
How to optimize resulting explicit module command lines.
ScanningOutputFormat Format
What output format are we expected to produce.
IntrusiveRefCntPtr< DiagnosticsEngine > DiagEngine
DiagnosticsEngineWithDiagOpts(ArrayRef< std::string > CommandLine, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, DiagnosticConsumer &DC)