clang 22.0.0git
CompilerInstance.cpp
Go to the documentation of this file.
1//===--- CompilerInstance.cpp ---------------------------------------------===//
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
12#include "clang/AST/Decl.h"
19#include "clang/Basic/Stack.h"
21#include "clang/Basic/Version.h"
22#include "clang/Config/config.h"
39#include "clang/Sema/Sema.h"
44#include "llvm/ADT/IntrusiveRefCntPtr.h"
45#include "llvm/ADT/STLExtras.h"
46#include "llvm/ADT/ScopeExit.h"
47#include "llvm/ADT/Statistic.h"
48#include "llvm/Config/llvm-config.h"
49#include "llvm/Support/AdvisoryLock.h"
50#include "llvm/Support/BuryPointer.h"
51#include "llvm/Support/CrashRecoveryContext.h"
52#include "llvm/Support/Errc.h"
53#include "llvm/Support/FileSystem.h"
54#include "llvm/Support/MemoryBuffer.h"
55#include "llvm/Support/Path.h"
56#include "llvm/Support/Signals.h"
57#include "llvm/Support/TimeProfiler.h"
58#include "llvm/Support/Timer.h"
59#include "llvm/Support/VirtualFileSystem.h"
60#include "llvm/Support/VirtualOutputBackends.h"
61#include "llvm/Support/VirtualOutputError.h"
62#include "llvm/Support/raw_ostream.h"
63#include "llvm/TargetParser/Host.h"
64#include <optional>
65#include <time.h>
66#include <utility>
67
68using namespace clang;
69
70CompilerInstance::CompilerInstance(
71 std::shared_ptr<CompilerInvocation> Invocation,
72 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
73 ModuleCache *ModCache)
74 : ModuleLoader(/*BuildingModule=*/ModCache),
75 Invocation(std::move(Invocation)),
76 ModCache(ModCache ? ModCache : createCrossProcessModuleCache()),
77 ThePCHContainerOperations(std::move(PCHContainerOps)) {
78 assert(this->Invocation && "Invocation must not be null");
79}
80
82 assert(OutputFiles.empty() && "Still output files in flight?");
83}
84
86 return (BuildGlobalModuleIndex ||
87 (TheASTReader && TheASTReader->isGlobalIndexUnavailable() &&
88 getFrontendOpts().GenerateGlobalModuleIndex)) &&
89 !DisableGeneratingGlobalModuleIndex;
90}
91
96
98 OwnedVerboseOutputStream.reset();
99 VerboseOutputStream = &Value;
100}
101
102void CompilerInstance::setVerboseOutputStream(std::unique_ptr<raw_ostream> Value) {
103 OwnedVerboseOutputStream.swap(Value);
104 VerboseOutputStream = OwnedVerboseOutputStream.get();
105}
106
109
111 // Create the target instance.
114 if (!hasTarget())
115 return false;
116
117 // Check whether AuxTarget exists, if not, then create TargetInfo for the
118 // other side of CUDA/OpenMP/SYCL compilation.
119 if (!getAuxTarget() &&
120 (getLangOpts().CUDA || getLangOpts().isTargetDevice()) &&
121 !getFrontendOpts().AuxTriple.empty()) {
122 auto &TO = AuxTargetOpts = std::make_unique<TargetOptions>();
123 TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
124 if (getFrontendOpts().AuxTargetCPU)
125 TO->CPU = *getFrontendOpts().AuxTargetCPU;
126 if (getFrontendOpts().AuxTargetFeatures)
127 TO->FeaturesAsWritten = *getFrontendOpts().AuxTargetFeatures;
128 TO->HostTriple = getTarget().getTriple().str();
130 }
131
132 if (!getTarget().hasStrictFP() && !getLangOpts().ExpStrictFP) {
133 if (getLangOpts().RoundingMath) {
134 getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_rounding);
135 getLangOpts().RoundingMath = false;
136 }
137 auto FPExc = getLangOpts().getFPExceptionMode();
138 if (FPExc != LangOptions::FPE_Default && FPExc != LangOptions::FPE_Ignore) {
139 getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_exceptions);
140 getLangOpts().setFPExceptionMode(LangOptions::FPE_Ignore);
141 }
142 // FIXME: can we disable FEnvAccess?
143 }
144
145 // We should do it here because target knows nothing about
146 // language options when it's being created.
147 if (getLangOpts().OpenCL &&
148 !getTarget().validateOpenCLTarget(getLangOpts(), getDiagnostics()))
149 return false;
150
151 // Inform the target of the language options.
152 // FIXME: We shouldn't need to do this, the target should be immutable once
153 // created. This complexity should be lifted elsewhere.
155
156 if (auto *Aux = getAuxTarget())
157 getTarget().setAuxTarget(Aux);
158
159 return true;
160}
161
164 setVirtualFileSystem(Value->getVirtualFileSystemPtr());
165 assert(Value == nullptr ||
166 getVirtualFileSystemPtr() == Value->getVirtualFileSystemPtr());
167 FileMgr = std::move(Value);
168}
169
174
175void CompilerInstance::setPreprocessor(std::shared_ptr<Preprocessor> Value) {
176 PP = std::move(Value);
177}
178
181 Context = std::move(Value);
182
183 if (Context && Consumer)
185}
186
188 TheSema.reset(S);
189}
190
191void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
192 Consumer = std::move(Value);
193
194 if (Context && Consumer)
196}
197
201
202std::unique_ptr<Sema> CompilerInstance::takeSema() {
203 return std::move(TheSema);
204}
205
207 return TheASTReader;
208}
210 assert(ModCache.get() == &Reader->getModuleManager().getModuleCache() &&
211 "Expected ASTReader to use the same PCM cache");
212 TheASTReader = std::move(Reader);
213}
214
215std::shared_ptr<ModuleDependencyCollector>
217 return ModuleDepCollector;
218}
219
221 std::shared_ptr<ModuleDependencyCollector> Collector) {
222 ModuleDepCollector = std::move(Collector);
223}
224
225static void collectHeaderMaps(const HeaderSearch &HS,
226 std::shared_ptr<ModuleDependencyCollector> MDC) {
227 SmallVector<std::string, 4> HeaderMapFileNames;
228 HS.getHeaderMapFileNames(HeaderMapFileNames);
229 for (auto &Name : HeaderMapFileNames)
230 MDC->addFile(Name);
231}
232
234 std::shared_ptr<ModuleDependencyCollector> MDC) {
235 const PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
236 if (PPOpts.ImplicitPCHInclude.empty())
237 return;
238
239 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
241 auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude);
242 if (!PCHDir) {
243 MDC->addFile(PCHInclude);
244 return;
245 }
246
247 std::error_code EC;
248 SmallString<128> DirNative;
249 llvm::sys::path::native(PCHDir->getName(), DirNative);
250 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
252 for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
253 Dir != DirEnd && !EC; Dir.increment(EC)) {
254 // Check whether this is an AST file. ASTReader::isAcceptableASTFile is not
255 // used here since we're not interested in validating the PCH at this time,
256 // but only to check whether this is a file containing an AST.
258 Dir->path(), FileMgr, CI.getModuleCache(),
260 /*FindModuleFileExtensions=*/false, Validator,
261 /*ValidateDiagnosticOptions=*/false))
262 MDC->addFile(Dir->path());
263 }
264}
265
267 std::shared_ptr<ModuleDependencyCollector> MDC) {
268 // Collect all VFS found.
270 CI.getVirtualFileSystem().visit([&](llvm::vfs::FileSystem &VFS) {
271 if (auto *RedirectingVFS = dyn_cast<llvm::vfs::RedirectingFileSystem>(&VFS))
272 llvm::vfs::collectVFSEntries(*RedirectingVFS, VFSEntries);
273 });
274
275 for (auto &E : VFSEntries)
276 MDC->addFile(E.VPath, E.RPath);
277}
278
281 DiagnosticOptions DiagOpts;
282 DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DC,
283 /*ShouldOwnClient=*/false);
284
286 std::move(BaseFS));
287 // FIXME: Should this go into createVFSFromCompilerInvocation?
288 if (getFrontendOpts().ShowStats)
289 VFS =
290 llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(std::move(VFS));
291}
292
293// Diagnostics
295 const CodeGenOptions *CodeGenOpts,
296 DiagnosticsEngine &Diags) {
297 std::error_code EC;
298 std::unique_ptr<raw_ostream> StreamOwner;
299 raw_ostream *OS = &llvm::errs();
300 if (DiagOpts.DiagnosticLogFile != "-") {
301 // Create the output stream.
302 auto FileOS = std::make_unique<llvm::raw_fd_ostream>(
303 DiagOpts.DiagnosticLogFile, EC,
304 llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF);
305 if (EC) {
306 Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
307 << DiagOpts.DiagnosticLogFile << EC.message();
308 } else {
309 FileOS->SetUnbuffered();
310 OS = FileOS.get();
311 StreamOwner = std::move(FileOS);
312 }
313 }
314
315 // Chain in the diagnostic client which will log the diagnostics.
316 auto Logger = std::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts,
317 std::move(StreamOwner));
318 if (CodeGenOpts)
319 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
320 if (Diags.ownsClient()) {
321 Diags.setClient(
322 new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
323 } else {
324 Diags.setClient(
325 new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger)));
326 }
327}
328
330 DiagnosticsEngine &Diags,
331 StringRef OutputFile) {
332 auto SerializedConsumer =
333 clang::serialized_diags::create(OutputFile, DiagOpts);
334
335 if (Diags.ownsClient()) {
337 Diags.takeClient(), std::move(SerializedConsumer)));
338 } else {
340 Diags.getClient(), std::move(SerializedConsumer)));
341 }
342}
343
345 bool ShouldOwnClient) {
347 Client, ShouldOwnClient, &getCodeGenOpts());
348}
349
351 llvm::vfs::FileSystem &VFS, DiagnosticOptions &Opts,
352 DiagnosticConsumer *Client, bool ShouldOwnClient,
353 const CodeGenOptions *CodeGenOpts) {
354 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(
355 DiagnosticIDs::create(), Opts);
356
357 // Create the diagnostic client for reporting errors or for
358 // implementing -verify.
359 if (Client) {
360 Diags->setClient(Client, ShouldOwnClient);
361 } else if (Opts.getFormat() == DiagnosticOptions::SARIF) {
362 Diags->setClient(new SARIFDiagnosticPrinter(llvm::errs(), Opts));
363 } else
364 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
365
366 // Chain in -verify checker, if requested.
367 if (Opts.VerifyDiagnostics)
368 Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
369
370 // Chain in -diagnostic-log-file dumper, if requested.
371 if (!Opts.DiagnosticLogFile.empty())
372 SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
373
374 if (!Opts.DiagnosticSerializationFile.empty())
376
377 // Configure our handling of diagnostics.
378 ProcessWarningOptions(*Diags, Opts, VFS);
379
380 return Diags;
381}
382
383// File Manager
384
386 assert(VFS && "CompilerInstance needs a VFS for creating FileManager");
387 FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(), VFS);
388}
389
390// Source Manager
391
393 assert(Diagnostics && "DiagnosticsEngine needed for creating SourceManager");
394 assert(FileMgr && "FileManager needed for creating SourceManager");
395 SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(getDiagnostics(),
397}
398
399// Initialize the remapping of files to alternative contents, e.g.,
400// those specified through other files.
402 SourceManager &SourceMgr,
404 const PreprocessorOptions &InitOpts) {
405 // Remap files in the source manager (with buffers).
406 for (const auto &RB : InitOpts.RemappedFileBuffers) {
407 // Create the file entry for the file that we're mapping from.
408 FileEntryRef FromFile =
409 FileMgr.getVirtualFileRef(RB.first, RB.second->getBufferSize(), 0);
410
411 // Override the contents of the "from" file with the contents of the
412 // "to" file. If the caller owns the buffers, then pass a MemoryBufferRef;
413 // otherwise, pass as a std::unique_ptr<MemoryBuffer> to transfer ownership
414 // to the SourceManager.
415 if (InitOpts.RetainRemappedFileBuffers)
416 SourceMgr.overrideFileContents(FromFile, RB.second->getMemBufferRef());
417 else
418 SourceMgr.overrideFileContents(
419 FromFile, std::unique_ptr<llvm::MemoryBuffer>(RB.second));
420 }
421
422 // Remap files in the source manager (with other files).
423 for (const auto &RF : InitOpts.RemappedFiles) {
424 // Find the file that we're mapping to.
425 OptionalFileEntryRef ToFile = FileMgr.getOptionalFileRef(RF.second);
426 if (!ToFile) {
427 Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second;
428 continue;
429 }
430
431 // Create the file entry for the file that we're mapping from.
432 FileEntryRef FromFile =
433 FileMgr.getVirtualFileRef(RF.first, ToFile->getSize(), 0);
434
435 // Override the contents of the "from" file with the contents of
436 // the "to" file.
437 SourceMgr.overrideFileContents(FromFile, *ToFile);
438 }
439
440 SourceMgr.setOverridenFilesKeepOriginalName(
442}
443
444// Preprocessor
445
448
449 // The AST reader holds a reference to the old preprocessor (if any).
450 TheASTReader.reset();
451
452 // Create the Preprocessor.
453 HeaderSearch *HeaderInfo =
456 PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOpts(),
458 getSourceManager(), *HeaderInfo, *this,
459 /*IdentifierInfoLookup=*/nullptr,
460 /*OwnsHeaderSearch=*/true, TUKind);
462 PP->Initialize(getTarget(), getAuxTarget());
463
464 if (PPOpts.DetailedRecord)
465 PP->createPreprocessingRecord();
466
467 // Apply remappings to the source manager.
468 InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
469 PP->getFileManager(), PPOpts);
470
471 // Predefine macros and configure the preprocessor.
474
475 // Initialize the header search object. In CUDA compilations, we use the aux
476 // triple (the host triple) to initialize our header search, since we need to
477 // find the host headers in order to compile the CUDA code.
478 const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple();
479 if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA &&
480 PP->getAuxTargetInfo())
481 HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
482
483 ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
484 PP->getLangOpts(), *HeaderSearchTriple);
485
486 PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
487
488 if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules) {
489 std::string ModuleHash = getInvocation().getModuleHash();
490 PP->getHeaderSearchInfo().setModuleHash(ModuleHash);
491 PP->getHeaderSearchInfo().setModuleCachePath(
492 getSpecificModuleCachePath(ModuleHash));
493 }
494
495 // Handle generating dependencies, if requested.
497 if (!DepOpts.OutputFile.empty())
498 addDependencyCollector(std::make_shared<DependencyFileGenerator>(DepOpts));
499 if (!DepOpts.DOTOutputFile.empty())
501 getHeaderSearchOpts().Sysroot);
502
503 // If we don't have a collector, but we are collecting module dependencies,
504 // then we're the top level compiler instance and need to create one.
505 if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) {
506 ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
508 }
509
510 // If there is a module dep collector, register with other dep collectors
511 // and also (a) collect header maps and (b) TODO: input vfs overlay files.
512 if (ModuleDepCollector) {
513 addDependencyCollector(ModuleDepCollector);
514 collectHeaderMaps(PP->getHeaderSearchInfo(), ModuleDepCollector);
515 collectIncludePCH(*this, ModuleDepCollector);
516 collectVFSEntries(*this, ModuleDepCollector);
517 }
518
519 // Modules need an output manager.
520 if (!hasOutputManager())
522
523 for (auto &Listener : DependencyCollectors)
524 Listener->attachToPreprocessor(*PP);
525
526 // Handle generating header include information, if requested.
527 if (DepOpts.ShowHeaderIncludes)
528 AttachHeaderIncludeGen(*PP, DepOpts);
529 if (!DepOpts.HeaderIncludeOutputFile.empty()) {
530 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
531 if (OutputPath == "-")
532 OutputPath = "";
533 AttachHeaderIncludeGen(*PP, DepOpts,
534 /*ShowAllHeaders=*/true, OutputPath,
535 /*ShowDepth=*/false);
536 }
537
539 AttachHeaderIncludeGen(*PP, DepOpts,
540 /*ShowAllHeaders=*/true, /*OutputPath=*/"",
541 /*ShowDepth=*/true, /*MSStyle=*/true);
542 }
543
544 if (GetDependencyDirectives)
545 PP->setDependencyDirectivesGetter(*GetDependencyDirectives);
546}
547
548std::string CompilerInstance::getSpecificModuleCachePath(StringRef ModuleHash) {
549 assert(FileMgr && "Specific module cache path requires a FileManager");
550
551 if (getHeaderSearchOpts().ModuleCachePath.empty())
552 return "";
553
554 // Set up the module path, including the hash for the module-creation options.
555 SmallString<256> SpecificModuleCache;
556 normalizeModuleCachePath(*FileMgr, getHeaderSearchOpts().ModuleCachePath,
557 SpecificModuleCache);
558 if (!getHeaderSearchOpts().DisableModuleHash)
559 llvm::sys::path::append(SpecificModuleCache, ModuleHash);
560 return std::string(SpecificModuleCache);
561}
562
563// ASTContext
564
567 auto Context = llvm::makeIntrusiveRefCnt<ASTContext>(
568 getLangOpts(), PP.getSourceManager(), PP.getIdentifierTable(),
569 PP.getSelectorTable(), PP.getBuiltinInfo(), PP.TUKind);
570 Context->InitBuiltinTypes(getTarget(), getAuxTarget());
571 setASTContext(std::move(Context));
572}
573
574// ExternalASTSource
575
576namespace {
577// Helper to recursively read the module names for all modules we're adding.
578// We mark these as known and redirect any attempt to load that module to
579// the files we were handed.
580struct ReadModuleNames : ASTReaderListener {
581 Preprocessor &PP;
583
584 ReadModuleNames(Preprocessor &PP) : PP(PP) {}
585
586 void ReadModuleName(StringRef ModuleName) override {
587 // Keep the module name as a string for now. It's not safe to create a new
588 // IdentifierInfo from an ASTReader callback.
589 LoadedModules.push_back(ModuleName.str());
590 }
591
592 void registerAll() {
593 ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
594 for (const std::string &LoadedModule : LoadedModules)
595 MM.cacheModuleLoad(*PP.getIdentifierInfo(LoadedModule),
596 MM.findOrLoadModule(LoadedModule));
597 LoadedModules.clear();
598 }
599
600 void markAllUnavailable() {
601 for (const std::string &LoadedModule : LoadedModules) {
603 LoadedModule)) {
604 M->HasIncompatibleModuleFile = true;
605
606 // Mark module as available if the only reason it was unavailable
607 // was missing headers.
608 SmallVector<Module *, 2> Stack;
609 Stack.push_back(M);
610 while (!Stack.empty()) {
611 Module *Current = Stack.pop_back_val();
612 if (Current->IsUnimportable) continue;
613 Current->IsAvailable = true;
614 auto SubmodulesRange = Current->submodules();
615 llvm::append_range(Stack, SubmodulesRange);
616 }
617 }
618 }
619 LoadedModules.clear();
620 }
621};
622} // namespace
623
625 StringRef Path, DisableValidationForModuleKind DisableValidation,
626 bool AllowPCHWithCompilerErrors, void *DeserializationListener,
627 bool OwnDeserializationListener) {
629 TheASTReader = createPCHExternalASTSource(
630 Path, getHeaderSearchOpts().Sysroot, DisableValidation,
631 AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(),
633 getFrontendOpts().ModuleFileExtensions, DependencyCollectors,
634 DeserializationListener, OwnDeserializationListener, Preamble,
635 getFrontendOpts().UseGlobalModuleIndex);
636}
637
639 StringRef Path, StringRef Sysroot,
640 DisableValidationForModuleKind DisableValidation,
641 bool AllowPCHWithCompilerErrors, Preprocessor &PP, ModuleCache &ModCache,
642 ASTContext &Context, const PCHContainerReader &PCHContainerRdr,
643 const CodeGenOptions &CodeGenOpts,
644 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
645 ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
646 void *DeserializationListener, bool OwnDeserializationListener,
647 bool Preamble, bool UseGlobalModuleIndex) {
648 const HeaderSearchOptions &HSOpts =
649 PP.getHeaderSearchInfo().getHeaderSearchOpts();
650
651 auto Reader = llvm::makeIntrusiveRefCnt<ASTReader>(
652 PP, ModCache, &Context, PCHContainerRdr, CodeGenOpts, Extensions,
653 Sysroot.empty() ? "" : Sysroot.data(), DisableValidation,
654 AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false,
657 HSOpts.ValidateASTInputFilesContent, UseGlobalModuleIndex);
658
659 // We need the external source to be set up before we read the AST, because
660 // eagerly-deserialized declarations may use it.
661 Context.setExternalSource(Reader);
662
663 Reader->setDeserializationListener(
664 static_cast<ASTDeserializationListener *>(DeserializationListener),
665 /*TakeOwnership=*/OwnDeserializationListener);
666
667 for (auto &Listener : DependencyCollectors)
668 Listener->attachToASTReader(*Reader);
669
670 auto Listener = std::make_unique<ReadModuleNames>(PP);
671 auto &ListenerRef = *Listener;
672 ASTReader::ListenerScope ReadModuleNamesListener(*Reader,
673 std::move(Listener));
674
675 switch (Reader->ReadAST(Path,
681 // Set the predefines buffer as suggested by the PCH reader. Typically, the
682 // predefines buffer will be empty.
683 PP.setPredefines(Reader->getSuggestedPredefines());
684 ListenerRef.registerAll();
685 return Reader;
686
688 // Unrecoverable failure: don't even try to process the input file.
689 break;
690
696 // No suitable PCH file could be found. Return an error.
697 break;
698 }
699
700 ListenerRef.markAllUnavailable();
701 Context.setExternalSource(nullptr);
702 return nullptr;
703}
704
705// Code Completion
706
708 StringRef Filename,
709 unsigned Line,
710 unsigned Column) {
711 // Tell the source manager to chop off the given file at a specific
712 // line and column.
713 auto Entry = PP.getFileManager().getOptionalFileRef(Filename);
714 if (!Entry) {
715 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
716 << Filename;
717 return true;
718 }
719
720 // Truncate the named file at the given line/column.
722 return false;
723}
724
727 if (!CompletionConsumer) {
729 getPreprocessor(), Loc.FileName, Loc.Line, Loc.Column,
730 getFrontendOpts().CodeCompleteOpts, llvm::outs()));
731 return;
733 Loc.Line, Loc.Column)) {
735 return;
736 }
737}
738
740 timerGroup.reset(new llvm::TimerGroup("clang", "Clang time report"));
741 FrontendTimer.reset(new llvm::Timer("frontend", "Front end", *timerGroup));
742}
743
746 StringRef Filename,
747 unsigned Line,
748 unsigned Column,
749 const CodeCompleteOptions &Opts,
750 raw_ostream &OS) {
751 if (EnableCodeCompletion(PP, Filename, Line, Column))
752 return nullptr;
753
754 // Set up the creation routine for code-completion.
755 return new PrintingCodeCompleteConsumer(Opts, OS);
756}
757
759 CodeCompleteConsumer *CompletionConsumer) {
760 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
761 TUKind, CompletionConsumer));
762
763 // Set up API notes.
764 TheSema->APINotes.setSwiftVersion(getAPINotesOpts().SwiftVersion);
765
766 // Attach the external sema source if there is any.
767 if (ExternalSemaSrc) {
768 TheSema->addExternalSource(ExternalSemaSrc);
769 ExternalSemaSrc->InitializeSema(*TheSema);
770 }
771
772 // If we're building a module and are supposed to load API notes,
773 // notify the API notes manager.
774 if (auto *currentModule = getPreprocessor().getCurrentModule()) {
775 (void)TheSema->APINotes.loadCurrentModuleAPINotes(
776 currentModule, getLangOpts().APINotesModules,
777 getAPINotesOpts().ModuleSearchPaths);
778 }
779}
780
781// Output Files
782
784 // The ASTConsumer can own streams that write to the output files.
785 assert(!hasASTConsumer() && "ASTConsumer should be reset");
786 if (!EraseFiles) {
787 for (auto &O : OutputFiles)
788 llvm::handleAllErrors(
789 O.keep(),
790 [&](const llvm::vfs::TempFileOutputError &E) {
791 getDiagnostics().Report(diag::err_unable_to_rename_temp)
792 << E.getTempPath() << E.getOutputPath()
793 << E.convertToErrorCode().message();
794 },
795 [&](const llvm::vfs::OutputError &E) {
796 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
797 << E.getOutputPath() << E.convertToErrorCode().message();
798 },
799 [&](const llvm::ErrorInfoBase &EIB) { // Handle any remaining error
800 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
801 << O.getPath() << EIB.message();
802 });
803 }
804 OutputFiles.clear();
805 if (DeleteBuiltModules) {
806 for (auto &Module : BuiltModules)
807 llvm::sys::fs::remove(Module.second);
808 BuiltModules.clear();
809 }
810}
811
812std::unique_ptr<raw_pwrite_stream> CompilerInstance::createDefaultOutputFile(
813 bool Binary, StringRef InFile, StringRef Extension, bool RemoveFileOnSignal,
814 bool CreateMissingDirectories, bool ForceUseTemporary) {
815 StringRef OutputPath = getFrontendOpts().OutputFile;
816 std::optional<SmallString<128>> PathStorage;
817 if (OutputPath.empty()) {
818 if (InFile == "-" || Extension.empty()) {
819 OutputPath = "-";
820 } else {
821 PathStorage.emplace(InFile);
822 llvm::sys::path::replace_extension(*PathStorage, Extension);
823 OutputPath = *PathStorage;
824 }
825 }
826
827 return createOutputFile(OutputPath, Binary, RemoveFileOnSignal,
828 getFrontendOpts().UseTemporary || ForceUseTemporary,
829 CreateMissingDirectories);
830}
831
832std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() {
833 return std::make_unique<llvm::raw_null_ostream>();
834}
835
836// Output Manager
837
840 assert(!OutputMgr && "Already has an output manager");
841 OutputMgr = std::move(NewOutputs);
842}
843
845 assert(!OutputMgr && "Already has an output manager");
846 OutputMgr = llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>();
847}
848
849llvm::vfs::OutputBackend &CompilerInstance::getOutputManager() {
850 assert(OutputMgr);
851 return *OutputMgr;
852}
853
855 if (!hasOutputManager())
857 return getOutputManager();
858}
859
860std::unique_ptr<raw_pwrite_stream>
862 bool RemoveFileOnSignal, bool UseTemporary,
863 bool CreateMissingDirectories) {
865 createOutputFileImpl(OutputPath, Binary, RemoveFileOnSignal, UseTemporary,
866 CreateMissingDirectories);
867 if (OS)
868 return std::move(*OS);
869 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
870 << OutputPath << errorToErrorCode(OS.takeError()).message();
871 return nullptr;
872}
873
875CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary,
876 bool RemoveFileOnSignal,
877 bool UseTemporary,
878 bool CreateMissingDirectories) {
879 assert((!CreateMissingDirectories || UseTemporary) &&
880 "CreateMissingDirectories is only allowed when using temporary files");
881
882 // If '-working-directory' was passed, the output filename should be
883 // relative to that.
884 std::optional<SmallString<128>> AbsPath;
885 if (OutputPath != "-" && !llvm::sys::path::is_absolute(OutputPath)) {
886 assert(hasFileManager() &&
887 "File Manager is required to fix up relative path.\n");
888
889 AbsPath.emplace(OutputPath);
890 FileMgr->FixupRelativePath(*AbsPath);
891 OutputPath = *AbsPath;
892 }
893
894 using namespace llvm::vfs;
896 OutputPath,
897 OutputConfig()
898 .setTextWithCRLF(!Binary)
899 .setDiscardOnSignal(RemoveFileOnSignal)
900 .setAtomicWrite(UseTemporary)
901 .setImplyCreateDirectories(UseTemporary && CreateMissingDirectories));
902 if (!O)
903 return O.takeError();
904
905 O->discardOnDestroy([](llvm::Error E) { consumeError(std::move(E)); });
906 OutputFiles.push_back(std::move(*O));
907 return OutputFiles.back().createProxy();
908}
909
910// Initialization Utilities
911
916
917// static
919 DiagnosticsEngine &Diags,
920 FileManager &FileMgr,
921 SourceManager &SourceMgr) {
927
928 if (Input.isBuffer()) {
929 SourceMgr.setMainFileID(SourceMgr.createFileID(Input.getBuffer(), Kind));
930 assert(SourceMgr.getMainFileID().isValid() &&
931 "Couldn't establish MainFileID!");
932 return true;
933 }
934
935 StringRef InputFile = Input.getFile();
936
937 // Figure out where to get and map in the main file.
938 auto FileOrErr = InputFile == "-"
939 ? FileMgr.getSTDIN()
940 : FileMgr.getFileRef(InputFile, /*OpenFile=*/true);
941 if (!FileOrErr) {
942 auto EC = llvm::errorToErrorCode(FileOrErr.takeError());
943 if (InputFile != "-")
944 Diags.Report(diag::err_fe_error_reading) << InputFile << EC.message();
945 else
946 Diags.Report(diag::err_fe_error_reading_stdin) << EC.message();
947 return false;
948 }
949
950 SourceMgr.setMainFileID(
951 SourceMgr.createFileID(*FileOrErr, SourceLocation(), Kind));
952
953 assert(SourceMgr.getMainFileID().isValid() &&
954 "Couldn't establish MainFileID!");
955 return true;
956}
957
958// High-Level Operations
959
961 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
962 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
963 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
964
965 // Mark this point as the bottom of the stack if we don't have somewhere
966 // better. We generally expect frontend actions to be invoked with (nearly)
967 // DesiredStackSpace available.
969
970 auto FinishDiagnosticClient = llvm::make_scope_exit([&]() {
971 // Notify the diagnostic client that all files were processed.
973 });
974
975 raw_ostream &OS = getVerboseOutputStream();
976
977 if (!Act.PrepareToExecute(*this))
978 return false;
979
980 if (!createTarget())
981 return false;
982
983 // rewriter project will change target built-in bool type from its default.
984 if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
986
987 // Validate/process some options.
988 if (getHeaderSearchOpts().Verbose)
989 OS << "clang -cc1 version " CLANG_VERSION_STRING << " based upon LLVM "
990 << LLVM_VERSION_STRING << " default target "
991 << llvm::sys::getDefaultTargetTriple() << "\n";
992
993 if (getFrontendOpts().ShowStats || !getFrontendOpts().StatsFile.empty())
994 llvm::EnableStatistics(false);
995
996 // Sort vectors containing toc data and no toc data variables to facilitate
997 // binary search later.
998 llvm::sort(getCodeGenOpts().TocDataVarsUserSpecified);
999 llvm::sort(getCodeGenOpts().NoTocDataVars);
1000
1001 for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) {
1002 // Reset the ID tables if we are reusing the SourceManager and parsing
1003 // regular files.
1004 if (hasSourceManager() && !Act.isModelParsingAction())
1006
1007 if (Act.BeginSourceFile(*this, FIF)) {
1008 if (llvm::Error Err = Act.Execute()) {
1009 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
1010 }
1011 Act.EndSourceFile();
1012 }
1013 }
1014
1016
1017 if (getFrontendOpts().ShowStats) {
1018 if (hasFileManager()) {
1020 OS << '\n';
1021 }
1022 llvm::PrintStatistics(OS);
1023 }
1024 StringRef StatsFile = getFrontendOpts().StatsFile;
1025 if (!StatsFile.empty()) {
1026 llvm::sys::fs::OpenFlags FileFlags = llvm::sys::fs::OF_TextWithCRLF;
1027 if (getFrontendOpts().AppendStats)
1028 FileFlags |= llvm::sys::fs::OF_Append;
1029 std::error_code EC;
1030 auto StatS =
1031 std::make_unique<llvm::raw_fd_ostream>(StatsFile, EC, FileFlags);
1032 if (EC) {
1033 getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file)
1034 << StatsFile << EC.message();
1035 } else {
1036 llvm::PrintStatisticsJSON(*StatS);
1037 }
1038 }
1039
1040 return !getDiagnostics().getClient()->getNumErrors();
1041}
1042
1044 if (!getDiagnosticOpts().ShowCarets)
1045 return;
1046
1047 raw_ostream &OS = getVerboseOutputStream();
1048
1049 // We can have multiple diagnostics sharing one diagnostic client.
1050 // Get the total number of warnings/errors from the client.
1051 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
1052 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
1053
1054 if (NumWarnings)
1055 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
1056 if (NumWarnings && NumErrors)
1057 OS << " and ";
1058 if (NumErrors)
1059 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
1060 if (NumWarnings || NumErrors) {
1061 OS << " generated";
1062 if (getLangOpts().CUDA) {
1063 if (!getLangOpts().CUDAIsDevice) {
1064 OS << " when compiling for host";
1065 } else {
1066 OS << " when compiling for " << getTargetOpts().CPU;
1067 }
1068 }
1069 OS << ".\n";
1070 }
1071}
1072
1074 // Load any requested plugins.
1075 for (const std::string &Path : getFrontendOpts().Plugins) {
1076 std::string Error;
1077 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
1078 getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
1079 << Path << Error;
1080 }
1081
1082 // Check if any of the loaded plugins replaces the main AST action
1083 for (const FrontendPluginRegistry::entry &Plugin :
1084 FrontendPluginRegistry::entries()) {
1085 std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
1086 if (P->getActionType() == PluginASTAction::ReplaceAction) {
1088 getFrontendOpts().ActionName = Plugin.getName().str();
1089 break;
1090 }
1091 }
1092}
1093
1094/// Determine the appropriate source input kind based on language
1095/// options.
1097 if (LangOpts.OpenCL)
1098 return Language::OpenCL;
1099 if (LangOpts.CUDA)
1100 return Language::CUDA;
1101 if (LangOpts.ObjC)
1102 return LangOpts.CPlusPlus ? Language::ObjCXX : Language::ObjC;
1103 return LangOpts.CPlusPlus ? Language::CXX : Language::C;
1104}
1105
1106std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
1107 SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
1108 StringRef OriginalModuleMapFile, StringRef ModuleFileName,
1109 std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) {
1110 // Construct a compiler invocation for creating this module.
1111 auto Invocation = std::make_shared<CompilerInvocation>(getInvocation());
1112
1113 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1114
1115 // For any options that aren't intended to affect how a module is built,
1116 // reset them to their default values.
1117 Invocation->resetNonModularOptions();
1118
1119 // Remove any macro definitions that are explicitly ignored by the module.
1120 // They aren't supposed to affect how the module is built anyway.
1121 HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
1122 llvm::erase_if(PPOpts.Macros,
1123 [&HSOpts](const std::pair<std::string, bool> &def) {
1124 StringRef MacroDef = def.first;
1125 return HSOpts.ModulesIgnoreMacros.contains(
1126 llvm::CachedHashString(MacroDef.split('=').first));
1127 });
1128
1129 // If the original compiler invocation had -fmodule-name, pass it through.
1130 Invocation->getLangOpts().ModuleName =
1132
1133 // Note the name of the module we're building.
1134 Invocation->getLangOpts().CurrentModule = std::string(ModuleName);
1135
1136 // If there is a module map file, build the module using the module map.
1137 // Set up the inputs/outputs so that we build the module from its umbrella
1138 // header.
1139 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1140 FrontendOpts.OutputFile = ModuleFileName.str();
1141 FrontendOpts.DisableFree = false;
1142 FrontendOpts.GenerateGlobalModuleIndex = false;
1143 FrontendOpts.BuildingImplicitModule = true;
1144 FrontendOpts.OriginalModuleMap = std::string(OriginalModuleMapFile);
1145 // Force implicitly-built modules to hash the content of the module file.
1146 HSOpts.ModulesHashContent = true;
1147 FrontendOpts.Inputs = {std::move(Input)};
1148
1149 // Don't free the remapped file buffers; they are owned by our caller.
1150 PPOpts.RetainRemappedFileBuffers = true;
1151
1152 DiagnosticOptions &DiagOpts = Invocation->getDiagnosticOpts();
1153
1154 DiagOpts.VerifyDiagnostics = 0;
1155 assert(getInvocation().getModuleHash() == Invocation->getModuleHash() &&
1156 "Module hash mismatch!");
1157
1158 // Construct a compiler instance that will be used to actually create the
1159 // module. Since we're sharing an in-memory module cache,
1160 // CompilerInstance::CompilerInstance is responsible for finalizing the
1161 // buffers to prevent use-after-frees.
1162 auto InstancePtr = std::make_unique<CompilerInstance>(
1163 std::move(Invocation), getPCHContainerOperations(), &getModuleCache());
1164 auto &Instance = *InstancePtr;
1165
1166 auto &Inv = Instance.getInvocation();
1167
1168 if (ThreadSafeConfig) {
1169 Instance.setVirtualFileSystem(ThreadSafeConfig->getVFS());
1170 Instance.createFileManager();
1171 } else if (FrontendOpts.ModulesShareFileManager) {
1172 Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
1173 Instance.setFileManager(getFileManagerPtr());
1174 } else {
1175 Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
1176 Instance.createFileManager();
1177 }
1178
1179 if (ThreadSafeConfig) {
1180 Instance.createDiagnostics(&ThreadSafeConfig->getDiagConsumer(),
1181 /*ShouldOwnClient=*/false);
1182 } else {
1183 Instance.createDiagnostics(
1184 new ForwardingDiagnosticConsumer(getDiagnosticClient()),
1185 /*ShouldOwnClient=*/true);
1186 }
1187 if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
1188 Instance.getDiagnostics().setSuppressSystemWarnings(false);
1189
1190 Instance.createSourceManager();
1191 SourceManager &SourceMgr = Instance.getSourceManager();
1192
1193 if (ThreadSafeConfig) {
1194 // Detecting cycles in the module graph is responsibility of the client.
1195 } else {
1196 // Note that this module is part of the module build stack, so that we
1197 // can detect cycles in the module graph.
1198 SourceMgr.setModuleBuildStack(getSourceManager().getModuleBuildStack());
1199 SourceMgr.pushModuleBuildStack(
1200 ModuleName, FullSourceLoc(ImportLoc, getSourceManager()));
1201 }
1202
1203 // Make a copy for the new instance.
1204 Instance.FailedModules = FailedModules;
1205
1206 if (GetDependencyDirectives)
1207 Instance.GetDependencyDirectives =
1208 GetDependencyDirectives->cloneFor(Instance.getFileManager());
1209
1210 if (ThreadSafeConfig) {
1211 Instance.setModuleDepCollector(ThreadSafeConfig->getModuleDepCollector());
1212 } else {
1213 // If we're collecting module dependencies, we need to share a collector
1214 // between all of the module CompilerInstances. Other than that, we don't
1215 // want to produce any dependency output from the module build.
1216 Instance.setModuleDepCollector(getModuleDepCollector());
1217 }
1218 Inv.getDependencyOutputOpts() = DependencyOutputOptions();
1219
1220 return InstancePtr;
1221}
1222
1224 StringRef ModuleName,
1225 StringRef ModuleFileName,
1226 CompilerInstance &Instance) {
1227 llvm::TimeTraceScope TimeScope("Module Compile", ModuleName);
1228
1229 // Never compile a module that's already finalized - this would cause the
1230 // existing module to be freed, causing crashes if it is later referenced
1231 if (getModuleCache().getInMemoryModuleCache().isPCMFinal(ModuleFileName)) {
1232 getDiagnostics().Report(ImportLoc, diag::err_module_rebuild_finalized)
1233 << ModuleName;
1234 return false;
1235 }
1236
1237 getDiagnostics().Report(ImportLoc, diag::remark_module_build)
1238 << ModuleName << ModuleFileName;
1239
1240 // Execute the action to actually build the module in-place. Use a separate
1241 // thread so that we get a stack large enough.
1242 bool Crashed = !llvm::CrashRecoveryContext().RunSafelyOnNewStack(
1243 [&]() {
1245 Instance.ExecuteAction(Action);
1246 },
1248
1249 getDiagnostics().Report(ImportLoc, diag::remark_module_build_done)
1250 << ModuleName;
1251
1252 // Propagate the statistics to the parent FileManager.
1253 if (!getFrontendOpts().ModulesShareFileManager)
1254 getFileManager().AddStats(Instance.getFileManager());
1255
1256 // Propagate the failed modules to the parent instance.
1257 FailedModules = std::move(Instance.FailedModules);
1258
1259 if (Crashed) {
1260 // Clear the ASTConsumer if it hasn't been already, in case it owns streams
1261 // that must be closed before clearing output files.
1262 Instance.setSema(nullptr);
1263 Instance.setASTConsumer(nullptr);
1264
1265 // Delete any remaining temporary files related to Instance.
1266 Instance.clearOutputFiles(/*EraseFiles=*/true);
1267 }
1268
1269 // We've rebuilt a module. If we're allowed to generate or update the global
1270 // module index, record that fact in the importing compiler instance.
1271 if (getFrontendOpts().GenerateGlobalModuleIndex) {
1273 }
1274
1275 // If \p AllowPCMWithCompilerErrors is set return 'success' even if errors
1276 // occurred.
1277 return !Instance.getDiagnostics().hasErrorOccurred() ||
1278 Instance.getFrontendOpts().AllowPCMWithCompilerErrors;
1279}
1280
1283 StringRef Filename = llvm::sys::path::filename(File.getName());
1284 SmallString<128> PublicFilename(File.getDir().getName());
1285 if (Filename == "module_private.map")
1286 llvm::sys::path::append(PublicFilename, "module.map");
1287 else if (Filename == "module.private.modulemap")
1288 llvm::sys::path::append(PublicFilename, "module.modulemap");
1289 else
1290 return std::nullopt;
1291 return FileMgr.getOptionalFileRef(PublicFilename);
1292}
1293
1294std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
1295 SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName,
1296 std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) {
1297 StringRef ModuleName = Module->getTopLevelModuleName();
1298
1300
1301 // Get or create the module map that we'll use to build this module.
1303 SourceManager &SourceMgr = getSourceManager();
1304
1305 if (FileID ModuleMapFID = ModMap.getContainingModuleMapFileID(Module);
1306 ModuleMapFID.isValid()) {
1307 // We want to use the top-level module map. If we don't, the compiling
1308 // instance may think the containing module map is a top-level one, while
1309 // the importing instance knows it's included from a parent module map via
1310 // the extern directive. This mismatch could bite us later.
1311 SourceLocation Loc = SourceMgr.getIncludeLoc(ModuleMapFID);
1312 while (Loc.isValid() && isModuleMap(SourceMgr.getFileCharacteristic(Loc))) {
1313 ModuleMapFID = SourceMgr.getFileID(Loc);
1314 Loc = SourceMgr.getIncludeLoc(ModuleMapFID);
1315 }
1316
1317 OptionalFileEntryRef ModuleMapFile =
1318 SourceMgr.getFileEntryRefForID(ModuleMapFID);
1319 assert(ModuleMapFile && "Top-level module map with no FileID");
1320
1321 // Canonicalize compilation to start with the public module map. This is
1322 // vital for submodules declarations in the private module maps to be
1323 // correctly parsed when depending on a top level module in the public one.
1324 if (OptionalFileEntryRef PublicMMFile =
1325 getPublicModuleMap(*ModuleMapFile, getFileManager()))
1326 ModuleMapFile = PublicMMFile;
1327
1328 StringRef ModuleMapFilePath = ModuleMapFile->getNameAsRequested();
1329
1330 // Use the systemness of the module map as parsed instead of using the
1331 // IsSystem attribute of the module. If the module has [system] but the
1332 // module map is not in a system path, then this would incorrectly parse
1333 // any other modules in that module map as system too.
1334 const SrcMgr::SLocEntry &SLoc = SourceMgr.getSLocEntry(ModuleMapFID);
1335 bool IsSystem = isSystem(SLoc.getFile().getFileCharacteristic());
1336
1337 // Use the module map where this module resides.
1338 return cloneForModuleCompileImpl(
1339 ImportLoc, ModuleName,
1340 FrontendInputFile(ModuleMapFilePath, IK, IsSystem),
1341 ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName,
1342 std::move(ThreadSafeConfig));
1343 }
1344
1345 // FIXME: We only need to fake up an input file here as a way of
1346 // transporting the module's directory to the module map parser. We should
1347 // be able to do that more directly, and parse from a memory buffer without
1348 // inventing this file.
1349 SmallString<128> FakeModuleMapFile(Module->Directory->getName());
1350 llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map");
1351
1352 std::string InferredModuleMapContent;
1353 llvm::raw_string_ostream OS(InferredModuleMapContent);
1354 Module->print(OS);
1355
1356 auto Instance = cloneForModuleCompileImpl(
1357 ImportLoc, ModuleName,
1358 FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
1359 ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName,
1360 std::move(ThreadSafeConfig));
1361
1362 std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
1363 llvm::MemoryBuffer::getMemBufferCopy(InferredModuleMapContent);
1364 FileEntryRef ModuleMapFile = Instance->getFileManager().getVirtualFileRef(
1365 FakeModuleMapFile, InferredModuleMapContent.size(), 0);
1366 Instance->getSourceManager().overrideFileContents(ModuleMapFile,
1367 std::move(ModuleMapBuffer));
1368
1369 return Instance;
1370}
1371
1372/// Read the AST right after compiling the module.
1373static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance,
1374 SourceLocation ImportLoc,
1375 SourceLocation ModuleNameLoc,
1376 Module *Module, StringRef ModuleFileName,
1377 bool *OutOfDate, bool *Missing) {
1378 DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1379
1380 unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
1381 if (OutOfDate)
1382 ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate;
1383
1384 // Try to read the module file, now that we've compiled it.
1385 ASTReader::ASTReadResult ReadResult =
1386 ImportingInstance.getASTReader()->ReadAST(
1387 ModuleFileName, serialization::MK_ImplicitModule, ImportLoc,
1388 ModuleLoadCapabilities);
1389 if (ReadResult == ASTReader::Success)
1390 return true;
1391
1392 // The caller wants to handle out-of-date failures.
1393 if (OutOfDate && ReadResult == ASTReader::OutOfDate) {
1394 *OutOfDate = true;
1395 return false;
1396 }
1397
1398 // The caller wants to handle missing module files.
1399 if (Missing && ReadResult == ASTReader::Missing) {
1400 *Missing = true;
1401 return false;
1402 }
1403
1404 // The ASTReader didn't diagnose the error, so conservatively report it.
1405 if (ReadResult == ASTReader::Missing || !Diags.hasErrorOccurred())
1406 Diags.Report(ModuleNameLoc, diag::err_module_not_built)
1407 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1408
1409 return false;
1410}
1411
1412/// Compile a module in a separate compiler instance and read the AST,
1413/// returning true if the module compiles without errors.
1414static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance,
1415 SourceLocation ImportLoc,
1416 SourceLocation ModuleNameLoc,
1417 Module *Module,
1418 StringRef ModuleFileName) {
1419 {
1420 auto Instance = ImportingInstance.cloneForModuleCompile(
1421 ModuleNameLoc, Module, ModuleFileName);
1422
1423 if (!ImportingInstance.compileModule(ModuleNameLoc,
1425 ModuleFileName, *Instance)) {
1426 ImportingInstance.getDiagnostics().Report(ModuleNameLoc,
1427 diag::err_module_not_built)
1428 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1429 return false;
1430 }
1431 }
1432
1433 // The module is built successfully, we can update its timestamp now.
1434 if (ImportingInstance.getPreprocessor()
1438 ImportingInstance.getModuleCache().updateModuleTimestamp(ModuleFileName);
1439 }
1440
1441 return readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1442 Module, ModuleFileName,
1443 /*OutOfDate=*/nullptr, /*Missing=*/nullptr);
1444}
1445
1446/// Compile a module in a separate compiler instance and read the AST,
1447/// returning true if the module compiles without errors, using a lock manager
1448/// to avoid building the same module in multiple compiler instances.
1449///
1450/// Uses a lock file manager and exponential backoff to reduce the chances that
1451/// multiple instances will compete to create the same module. On timeout,
1452/// deletes the lock file in order to avoid deadlock from crashing processes or
1453/// bugs in the lock file manager.
1455 CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
1456 SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName) {
1457 DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1458
1459 Diags.Report(ModuleNameLoc, diag::remark_module_lock)
1460 << ModuleFileName << Module->Name;
1461
1462 auto &ModuleCache = ImportingInstance.getModuleCache();
1463 ModuleCache.prepareForGetLock(ModuleFileName);
1464
1465 while (true) {
1466 auto Lock = ModuleCache.getLock(ModuleFileName);
1467 bool Owned;
1468 if (llvm::Error Err = Lock->tryLock().moveInto(Owned)) {
1469 // ModuleCache takes care of correctness and locks are only necessary for
1470 // performance. Fallback to building the module in case of any lock
1471 // related errors.
1472 Diags.Report(ModuleNameLoc, diag::remark_module_lock_failure)
1473 << Module->Name << toString(std::move(Err));
1474 return compileModuleAndReadASTImpl(ImportingInstance, ImportLoc,
1475 ModuleNameLoc, Module, ModuleFileName);
1476 }
1477 if (Owned) {
1478 // We're responsible for building the module ourselves.
1479 return compileModuleAndReadASTImpl(ImportingInstance, ImportLoc,
1480 ModuleNameLoc, Module, ModuleFileName);
1481 }
1482
1483 // Someone else is responsible for building the module. Wait for them to
1484 // finish.
1485 switch (Lock->waitForUnlockFor(std::chrono::seconds(90))) {
1486 case llvm::WaitForUnlockResult::Success:
1487 break; // The interesting case.
1488 case llvm::WaitForUnlockResult::OwnerDied:
1489 continue; // try again to get the lock.
1490 case llvm::WaitForUnlockResult::Timeout:
1491 // Since the InMemoryModuleCache takes care of correctness, we try waiting
1492 // for someone else to complete the build so that it does not happen
1493 // twice. In case of timeout, build it ourselves.
1494 Diags.Report(ModuleNameLoc, diag::remark_module_lock_timeout)
1495 << Module->Name;
1496 // Clear the lock file so that future invocations can make progress.
1497 Lock->unsafeMaybeUnlock();
1498 continue;
1499 }
1500
1501 // Read the module that was just written by someone else.
1502 bool OutOfDate = false;
1503 bool Missing = false;
1504 if (readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1505 Module, ModuleFileName, &OutOfDate, &Missing))
1506 return true;
1507 if (!OutOfDate && !Missing)
1508 return false;
1509
1510 // The module may be missing or out of date in the presence of file system
1511 // races. It may also be out of date if one of its imports depends on header
1512 // search paths that are not consistent with this ImportingInstance.
1513 // Try again...
1514 }
1515}
1516
1517/// Compile a module in a separate compiler instance and read the AST,
1518/// returning true if the module compiles without errors, potentially using a
1519/// lock manager to avoid building the same module in multiple compiler
1520/// instances.
1521static bool compileModuleAndReadAST(CompilerInstance &ImportingInstance,
1522 SourceLocation ImportLoc,
1523 SourceLocation ModuleNameLoc,
1524 Module *Module, StringRef ModuleFileName) {
1525 return ImportingInstance.getInvocation()
1528 ? compileModuleAndReadASTBehindLock(ImportingInstance, ImportLoc,
1529 ModuleNameLoc, Module,
1530 ModuleFileName)
1531 : compileModuleAndReadASTImpl(ImportingInstance, ImportLoc,
1532 ModuleNameLoc, Module,
1533 ModuleFileName);
1534}
1535
1536/// Diagnose differences between the current definition of the given
1537/// configuration macro and the definition provided on the command line.
1538static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
1539 Module *Mod, SourceLocation ImportLoc) {
1540 IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
1541 SourceManager &SourceMgr = PP.getSourceManager();
1542
1543 // If this identifier has never had a macro definition, then it could
1544 // not have changed.
1545 if (!Id->hadMacroDefinition())
1546 return;
1547 auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id);
1548
1549 // Find the macro definition from the command line.
1550 MacroInfo *CmdLineDefinition = nullptr;
1551 for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
1552 // We only care about the predefines buffer.
1553 FileID FID = SourceMgr.getFileID(MD->getLocation());
1554 if (FID.isInvalid() || FID != PP.getPredefinesFileID())
1555 continue;
1556 if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
1557 CmdLineDefinition = DMD->getMacroInfo();
1558 break;
1559 }
1560
1561 auto *CurrentDefinition = PP.getMacroInfo(Id);
1562 if (CurrentDefinition == CmdLineDefinition) {
1563 // Macro matches. Nothing to do.
1564 } else if (!CurrentDefinition) {
1565 // This macro was defined on the command line, then #undef'd later.
1566 // Complain.
1567 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1568 << true << ConfigMacro << Mod->getFullModuleName();
1569 auto LatestDef = LatestLocalMD->getDefinition();
1570 assert(LatestDef.isUndefined() &&
1571 "predefined macro went away with no #undef?");
1572 PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
1573 << true;
1574 return;
1575 } else if (!CmdLineDefinition) {
1576 // There was no definition for this macro in the predefines buffer,
1577 // but there was a local definition. Complain.
1578 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1579 << false << ConfigMacro << Mod->getFullModuleName();
1580 PP.Diag(CurrentDefinition->getDefinitionLoc(),
1581 diag::note_module_def_undef_here)
1582 << false;
1583 } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
1584 /*Syntactically=*/true)) {
1585 // The macro definitions differ.
1586 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1587 << false << ConfigMacro << Mod->getFullModuleName();
1588 PP.Diag(CurrentDefinition->getDefinitionLoc(),
1589 diag::note_module_def_undef_here)
1590 << false;
1591 }
1592}
1593
1595 SourceLocation ImportLoc) {
1596 clang::Module *TopModule = M->getTopLevelModule();
1597 for (const StringRef ConMacro : TopModule->ConfigMacros) {
1598 checkConfigMacro(PP, ConMacro, M, ImportLoc);
1599 }
1600}
1601
1603 if (TheASTReader)
1604 return;
1605
1606 if (!hasASTContext())
1608
1609 // If we're implicitly building modules but not currently recursively
1610 // building a module, check whether we need to prune the module cache.
1611 if (getSourceManager().getModuleBuildStack().empty() &&
1612 !getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty())
1613 ModCache->maybePrune(getHeaderSearchOpts().ModuleCachePath,
1614 getHeaderSearchOpts().ModuleCachePruneInterval,
1615 getHeaderSearchOpts().ModuleCachePruneAfter);
1616
1618 std::string Sysroot = HSOpts.Sysroot;
1619 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1620 const FrontendOptions &FEOpts = getFrontendOpts();
1621 std::unique_ptr<llvm::Timer> ReadTimer;
1622
1623 if (timerGroup)
1624 ReadTimer = std::make_unique<llvm::Timer>("reading_modules",
1625 "Reading modules", *timerGroup);
1626 TheASTReader = llvm::makeIntrusiveRefCnt<ASTReader>(
1629 getFrontendOpts().ModuleFileExtensions,
1630 Sysroot.empty() ? "" : Sysroot.c_str(),
1632 /*AllowASTWithCompilerErrors=*/FEOpts.AllowPCMWithCompilerErrors,
1633 /*AllowConfigurationMismatch=*/false,
1637 +getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer));
1638 if (hasASTConsumer()) {
1639 TheASTReader->setDeserializationListener(
1640 getASTConsumer().GetASTDeserializationListener());
1642 getASTConsumer().GetASTMutationListener());
1643 }
1644 getASTContext().setExternalSource(TheASTReader);
1645 if (hasSema())
1646 TheASTReader->InitializeSema(getSema());
1647 if (hasASTConsumer())
1648 TheASTReader->StartTranslationUnit(&getASTConsumer());
1649
1650 for (auto &Listener : DependencyCollectors)
1651 Listener->attachToASTReader(*TheASTReader);
1652}
1653
1655 StringRef FileName, serialization::ModuleFile *&LoadedModuleFile) {
1656 llvm::Timer Timer;
1657 if (timerGroup)
1658 Timer.init("preloading." + FileName.str(), "Preloading " + FileName.str(),
1659 *timerGroup);
1660 llvm::TimeRegion TimeLoading(timerGroup ? &Timer : nullptr);
1661
1662 // If we don't already have an ASTReader, create one now.
1663 if (!TheASTReader)
1665
1666 // If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the
1667 // ASTReader to diagnose it, since it can produce better errors that we can.
1668 bool ConfigMismatchIsRecoverable =
1669 getDiagnostics().getDiagnosticLevel(diag::warn_module_config_mismatch,
1672
1673 auto Listener = std::make_unique<ReadModuleNames>(*PP);
1674 auto &ListenerRef = *Listener;
1675 ASTReader::ListenerScope ReadModuleNamesListener(*TheASTReader,
1676 std::move(Listener));
1677
1678 // Try to load the module file.
1679 switch (TheASTReader->ReadAST(
1681 ConfigMismatchIsRecoverable ? ASTReader::ARR_ConfigurationMismatch : 0,
1682 &LoadedModuleFile)) {
1683 case ASTReader::Success:
1684 // We successfully loaded the module file; remember the set of provided
1685 // modules so that we don't try to load implicit modules for them.
1686 ListenerRef.registerAll();
1687 return true;
1688
1690 // Ignore unusable module files.
1691 getDiagnostics().Report(SourceLocation(), diag::warn_module_config_mismatch)
1692 << FileName;
1693 // All modules provided by any files we tried and failed to load are now
1694 // unavailable; includes of those modules should now be handled textually.
1695 ListenerRef.markAllUnavailable();
1696 return true;
1697
1698 default:
1699 return false;
1700 }
1701}
1702
1703namespace {
1704enum ModuleSource {
1705 MS_ModuleNotFound,
1706 MS_ModuleCache,
1707 MS_PrebuiltModulePath,
1708 MS_ModuleBuildPragma
1709};
1710} // end namespace
1711
1712/// Select a source for loading the named module and compute the filename to
1713/// load it from.
1714static ModuleSource selectModuleSource(
1715 Module *M, StringRef ModuleName, std::string &ModuleFilename,
1716 const std::map<std::string, std::string, std::less<>> &BuiltModules,
1717 HeaderSearch &HS) {
1718 assert(ModuleFilename.empty() && "Already has a module source?");
1719
1720 // Check to see if the module has been built as part of this compilation
1721 // via a module build pragma.
1722 auto BuiltModuleIt = BuiltModules.find(ModuleName);
1723 if (BuiltModuleIt != BuiltModules.end()) {
1724 ModuleFilename = BuiltModuleIt->second;
1725 return MS_ModuleBuildPragma;
1726 }
1727
1728 // Try to load the module from the prebuilt module path.
1729 const HeaderSearchOptions &HSOpts = HS.getHeaderSearchOpts();
1730 if (!HSOpts.PrebuiltModuleFiles.empty() ||
1731 !HSOpts.PrebuiltModulePaths.empty()) {
1732 ModuleFilename = HS.getPrebuiltModuleFileName(ModuleName);
1733 if (HSOpts.EnablePrebuiltImplicitModules && ModuleFilename.empty())
1734 ModuleFilename = HS.getPrebuiltImplicitModuleFileName(M);
1735 if (!ModuleFilename.empty())
1736 return MS_PrebuiltModulePath;
1737 }
1738
1739 // Try to load the module from the module cache.
1740 if (M) {
1741 ModuleFilename = HS.getCachedModuleFileName(M);
1742 return MS_ModuleCache;
1743 }
1744
1745 return MS_ModuleNotFound;
1746}
1747
1748ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
1749 StringRef ModuleName, SourceLocation ImportLoc,
1750 SourceLocation ModuleNameLoc, bool IsInclusionDirective) {
1751 // Search for a module with the given name.
1752 HeaderSearch &HS = PP->getHeaderSearchInfo();
1753 Module *M =
1754 HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
1755
1756 // Check for any configuration macros that have changed. This is done
1757 // immediately before potentially building a module in case this module
1758 // depends on having one of its configuration macros defined to successfully
1759 // build. If this is not done the user will never see the warning.
1760 if (M)
1761 checkConfigMacros(getPreprocessor(), M, ImportLoc);
1762
1763 // Select the source and filename for loading the named module.
1764 std::string ModuleFilename;
1765 ModuleSource Source =
1766 selectModuleSource(M, ModuleName, ModuleFilename, BuiltModules, HS);
1767 if (Source == MS_ModuleNotFound) {
1768 // We can't find a module, error out here.
1769 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1770 << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
1771 return nullptr;
1772 }
1773 if (ModuleFilename.empty()) {
1774 if (M && M->HasIncompatibleModuleFile) {
1775 // We tried and failed to load a module file for this module. Fall
1776 // back to textual inclusion for its headers.
1778 }
1779
1780 getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled)
1781 << ModuleName;
1782 return nullptr;
1783 }
1784
1785 // Create an ASTReader on demand.
1786 if (!getASTReader())
1788
1789 // Time how long it takes to load the module.
1790 llvm::Timer Timer;
1791 if (timerGroup)
1792 Timer.init("loading." + ModuleFilename, "Loading " + ModuleFilename,
1793 *timerGroup);
1794 llvm::TimeRegion TimeLoading(timerGroup ? &Timer : nullptr);
1795 llvm::TimeTraceScope TimeScope("Module Load", ModuleName);
1796
1797 // Try to load the module file. If we are not trying to load from the
1798 // module cache, we don't know how to rebuild modules.
1799 unsigned ARRFlags = Source == MS_ModuleCache
1802 : Source == MS_PrebuiltModulePath
1803 ? 0
1805 switch (getASTReader()->ReadAST(ModuleFilename,
1806 Source == MS_PrebuiltModulePath
1808 : Source == MS_ModuleBuildPragma
1811 ImportLoc, ARRFlags)) {
1812 case ASTReader::Success: {
1813 if (M)
1814 return M;
1815 assert(Source != MS_ModuleCache &&
1816 "missing module, but file loaded from cache");
1817
1818 // A prebuilt module is indexed as a ModuleFile; the Module does not exist
1819 // until the first call to ReadAST. Look it up now.
1820 M = HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
1821
1822 // Check whether M refers to the file in the prebuilt module path.
1823 if (M && M->getASTFile())
1824 if (auto ModuleFile = FileMgr->getOptionalFileRef(ModuleFilename))
1825 if (*ModuleFile == M->getASTFile())
1826 return M;
1827
1828 getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt)
1829 << ModuleName;
1830 return ModuleLoadResult();
1831 }
1832
1834 case ASTReader::Missing:
1835 // The most interesting case.
1836 break;
1837
1839 if (Source == MS_PrebuiltModulePath)
1840 // FIXME: We shouldn't be setting HadFatalFailure below if we only
1841 // produce a warning here!
1842 getDiagnostics().Report(SourceLocation(),
1843 diag::warn_module_config_mismatch)
1844 << ModuleFilename;
1845 // Fall through to error out.
1846 [[fallthrough]];
1850 // FIXME: The ASTReader will already have complained, but can we shoehorn
1851 // that diagnostic information into a more useful form?
1852 return ModuleLoadResult();
1853
1854 case ASTReader::Failure:
1856 return ModuleLoadResult();
1857 }
1858
1859 // ReadAST returned Missing or OutOfDate.
1860 if (Source != MS_ModuleCache) {
1861 // We don't know the desired configuration for this module and don't
1862 // necessarily even have a module map. Since ReadAST already produces
1863 // diagnostics for these two cases, we simply error out here.
1864 return ModuleLoadResult();
1865 }
1866
1867 // The module file is missing or out-of-date. Build it.
1868 assert(M && "missing module, but trying to compile for cache");
1869
1870 // Check whether there is a cycle in the module graph.
1872 ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
1873 for (; Pos != PosEnd; ++Pos) {
1874 if (Pos->first == ModuleName)
1875 break;
1876 }
1877
1878 if (Pos != PosEnd) {
1879 SmallString<256> CyclePath;
1880 for (; Pos != PosEnd; ++Pos) {
1881 CyclePath += Pos->first;
1882 CyclePath += " -> ";
1883 }
1884 CyclePath += ModuleName;
1885
1886 getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
1887 << ModuleName << CyclePath;
1888 return nullptr;
1889 }
1890
1891 // Check whether we have already attempted to build this module (but failed).
1892 if (FailedModules.contains(ModuleName)) {
1893 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
1894 << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
1895 return nullptr;
1896 }
1897
1898 // Try to compile and then read the AST.
1899 if (!compileModuleAndReadAST(*this, ImportLoc, ModuleNameLoc, M,
1900 ModuleFilename)) {
1901 assert(getDiagnostics().hasErrorOccurred() &&
1902 "undiagnosed error in compileModuleAndReadAST");
1903 FailedModules.insert(ModuleName);
1904 return nullptr;
1905 }
1906
1907 // Okay, we've rebuilt and now loaded the module.
1908 return M;
1909}
1910
1913 ModuleIdPath Path,
1915 bool IsInclusionDirective) {
1916 // Determine what file we're searching from.
1917 StringRef ModuleName = Path[0].getIdentifierInfo()->getName();
1918 SourceLocation ModuleNameLoc = Path[0].getLoc();
1919
1920 // If we've already handled this import, just return the cached result.
1921 // This one-element cache is important to eliminate redundant diagnostics
1922 // when both the preprocessor and parser see the same import declaration.
1923 if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) {
1924 // Make the named module visible.
1925 if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule)
1926 TheASTReader->makeModuleVisible(LastModuleImportResult, Visibility,
1927 ImportLoc);
1928 return LastModuleImportResult;
1929 }
1930
1931 // If we don't already have information on this module, load the module now.
1932 Module *Module = nullptr;
1934 if (auto MaybeModule = MM.getCachedModuleLoad(*Path[0].getIdentifierInfo())) {
1935 // Use the cached result, which may be nullptr.
1936 Module = *MaybeModule;
1937 // Config macros are already checked before building a module, but they need
1938 // to be checked at each import location in case any of the config macros
1939 // have a new value at the current `ImportLoc`.
1940 if (Module)
1942 } else if (ModuleName == getLangOpts().CurrentModule) {
1943 // This is the module we're building.
1944 Module = PP->getHeaderSearchInfo().lookupModule(
1945 ModuleName, ImportLoc, /*AllowSearch*/ true,
1946 /*AllowExtraModuleMapSearch*/ !IsInclusionDirective);
1947
1948 // Config macros do not need to be checked here for two reasons.
1949 // * This will always be textual inclusion, and thus the config macros
1950 // actually do impact the content of the header.
1951 // * `Preprocessor::HandleHeaderIncludeOrImport` will never call this
1952 // function as the `#include` or `#import` is textual.
1953
1954 MM.cacheModuleLoad(*Path[0].getIdentifierInfo(), Module);
1955 } else {
1956 ModuleLoadResult Result = findOrCompileModuleAndReadAST(
1957 ModuleName, ImportLoc, ModuleNameLoc, IsInclusionDirective);
1958 if (!Result.isNormal())
1959 return Result;
1960 if (!Result)
1961 DisableGeneratingGlobalModuleIndex = true;
1962 Module = Result;
1963 MM.cacheModuleLoad(*Path[0].getIdentifierInfo(), Module);
1964 }
1965
1966 // If we never found the module, fail. Otherwise, verify the module and link
1967 // it up.
1968 if (!Module)
1969 return ModuleLoadResult();
1970
1971 // Verify that the rest of the module path actually corresponds to
1972 // a submodule.
1973 bool MapPrivateSubModToTopLevel = false;
1974 for (unsigned I = 1, N = Path.size(); I != N; ++I) {
1975 StringRef Name = Path[I].getIdentifierInfo()->getName();
1976 clang::Module *Sub = Module->findSubmodule(Name);
1977
1978 // If the user is requesting Foo.Private and it doesn't exist, try to
1979 // match Foo_Private and emit a warning asking for the user to write
1980 // @import Foo_Private instead. FIXME: remove this when existing clients
1981 // migrate off of Foo.Private syntax.
1982 if (!Sub && Name == "Private" && Module == Module->getTopLevelModule()) {
1983 SmallString<128> PrivateModule(Module->Name);
1984 PrivateModule.append("_Private");
1985
1987 auto &II = PP->getIdentifierTable().get(
1988 PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
1989 PrivPath.emplace_back(Path[0].getLoc(), &II);
1990
1991 std::string FileName;
1992 // If there is a modulemap module or prebuilt module, load it.
1993 if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc, true,
1994 !IsInclusionDirective) ||
1995 selectModuleSource(nullptr, PrivateModule, FileName, BuiltModules,
1996 PP->getHeaderSearchInfo()) != MS_ModuleNotFound)
1997 Sub = loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
1998 if (Sub) {
1999 MapPrivateSubModToTopLevel = true;
2000 PP->markClangModuleAsAffecting(Module);
2001 if (!getDiagnostics().isIgnored(
2002 diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) {
2003 getDiagnostics().Report(Path[I].getLoc(),
2004 diag::warn_no_priv_submodule_use_toplevel)
2005 << Path[I].getIdentifierInfo() << Module->getFullModuleName()
2006 << PrivateModule
2007 << SourceRange(Path[0].getLoc(), Path[I].getLoc())
2008 << FixItHint::CreateReplacement(SourceRange(Path[0].getLoc()),
2009 PrivateModule);
2010 getDiagnostics().Report(Sub->DefinitionLoc,
2011 diag::note_private_top_level_defined);
2012 }
2013 }
2014 }
2015
2016 if (!Sub) {
2017 // Attempt to perform typo correction to find a module name that works.
2019 unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
2020
2021 for (class Module *SubModule : Module->submodules()) {
2022 unsigned ED =
2023 Name.edit_distance(SubModule->Name,
2024 /*AllowReplacements=*/true, BestEditDistance);
2025 if (ED <= BestEditDistance) {
2026 if (ED < BestEditDistance) {
2027 Best.clear();
2028 BestEditDistance = ED;
2029 }
2030
2031 Best.push_back(SubModule->Name);
2032 }
2033 }
2034
2035 // If there was a clear winner, user it.
2036 if (Best.size() == 1) {
2037 getDiagnostics().Report(Path[I].getLoc(),
2038 diag::err_no_submodule_suggest)
2039 << Path[I].getIdentifierInfo() << Module->getFullModuleName()
2040 << Best[0] << SourceRange(Path[0].getLoc(), Path[I - 1].getLoc())
2041 << FixItHint::CreateReplacement(SourceRange(Path[I].getLoc()),
2042 Best[0]);
2043
2044 Sub = Module->findSubmodule(Best[0]);
2045 }
2046 }
2047
2048 if (!Sub) {
2049 // No submodule by this name. Complain, and don't look for further
2050 // submodules.
2051 getDiagnostics().Report(Path[I].getLoc(), diag::err_no_submodule)
2052 << Path[I].getIdentifierInfo() << Module->getFullModuleName()
2053 << SourceRange(Path[0].getLoc(), Path[I - 1].getLoc());
2054 break;
2055 }
2056
2057 Module = Sub;
2058 }
2059
2060 // Make the named module visible, if it's not already part of the module
2061 // we are parsing.
2062 if (ModuleName != getLangOpts().CurrentModule) {
2063 if (!Module->IsFromModuleFile && !MapPrivateSubModToTopLevel) {
2064 // We have an umbrella header or directory that doesn't actually include
2065 // all of the headers within the directory it covers. Complain about
2066 // this missing submodule and recover by forgetting that we ever saw
2067 // this submodule.
2068 // FIXME: Should we detect this at module load time? It seems fairly
2069 // expensive (and rare).
2070 getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
2072 << SourceRange(Path.front().getLoc(), Path.back().getLoc());
2073
2075 }
2076
2077 // Check whether this module is available.
2079 *Module, getDiagnostics())) {
2080 getDiagnostics().Report(ImportLoc, diag::note_module_import_here)
2081 << SourceRange(Path.front().getLoc(), Path.back().getLoc());
2082 LastModuleImportLoc = ImportLoc;
2083 LastModuleImportResult = ModuleLoadResult();
2084 return ModuleLoadResult();
2085 }
2086
2087 TheASTReader->makeModuleVisible(Module, Visibility, ImportLoc);
2088 }
2089
2090 // Resolve any remaining module using export_as for this one.
2093 .getModuleMap()
2095
2096 LastModuleImportLoc = ImportLoc;
2097 LastModuleImportResult = ModuleLoadResult(Module);
2098 return LastModuleImportResult;
2099}
2100
2102 StringRef ModuleName,
2103 StringRef Source) {
2104 // Avoid creating filenames with special characters.
2105 SmallString<128> CleanModuleName(ModuleName);
2106 for (auto &C : CleanModuleName)
2107 if (!isAlphanumeric(C))
2108 C = '_';
2109
2110 // FIXME: Using a randomized filename here means that our intermediate .pcm
2111 // output is nondeterministic (as .pcm files refer to each other by name).
2112 // Can this affect the output in any way?
2113 SmallString<128> ModuleFileName;
2114 if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
2115 CleanModuleName, "pcm", ModuleFileName)) {
2116 getDiagnostics().Report(ImportLoc, diag::err_fe_unable_to_open_output)
2117 << ModuleFileName << EC.message();
2118 return;
2119 }
2120 std::string ModuleMapFileName = (CleanModuleName + ".map").str();
2121
2122 FrontendInputFile Input(
2123 ModuleMapFileName,
2124 InputKind(getLanguageFromOptions(Invocation->getLangOpts()),
2125 InputKind::ModuleMap, /*Preprocessed*/true));
2126
2127 std::string NullTerminatedSource(Source.str());
2128
2129 auto Other = cloneForModuleCompileImpl(ImportLoc, ModuleName, Input,
2130 StringRef(), ModuleFileName);
2131
2132 // Create a virtual file containing our desired source.
2133 // FIXME: We shouldn't need to do this.
2134 FileEntryRef ModuleMapFile = Other->getFileManager().getVirtualFileRef(
2135 ModuleMapFileName, NullTerminatedSource.size(), 0);
2136 Other->getSourceManager().overrideFileContents(
2137 ModuleMapFile, llvm::MemoryBuffer::getMemBuffer(NullTerminatedSource));
2138
2139 Other->BuiltModules = std::move(BuiltModules);
2140 Other->DeleteBuiltModules = false;
2141
2142 // Build the module, inheriting any modules that we've built locally.
2143 bool Success = compileModule(ImportLoc, ModuleName, ModuleFileName, *Other);
2144
2145 BuiltModules = std::move(Other->BuiltModules);
2146
2147 if (Success) {
2148 BuiltModules[std::string(ModuleName)] = std::string(ModuleFileName);
2149 llvm::sys::RemoveFileOnSignal(ModuleFileName);
2150 }
2151}
2152
2155 SourceLocation ImportLoc) {
2156 if (!TheASTReader)
2158 if (!TheASTReader)
2159 return;
2160
2161 TheASTReader->makeModuleVisible(Mod, Visibility, ImportLoc);
2162}
2163
2165 SourceLocation TriggerLoc) {
2166 if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty())
2167 return nullptr;
2168 if (!TheASTReader)
2170 // Can't do anything if we don't have the module manager.
2171 if (!TheASTReader)
2172 return nullptr;
2173 // Get an existing global index. This loads it if not already
2174 // loaded.
2175 TheASTReader->loadGlobalIndex();
2176 GlobalModuleIndex *GlobalIndex = TheASTReader->getGlobalIndex();
2177 // If the global index doesn't exist, create it.
2178 if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
2179 hasPreprocessor()) {
2180 llvm::sys::fs::create_directories(
2181 getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
2182 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
2184 getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) {
2185 // FIXME this drops the error on the floor. This code is only used for
2186 // typo correction and drops more than just this one source of errors
2187 // (such as the directory creation failure above). It should handle the
2188 // error.
2189 consumeError(std::move(Err));
2190 return nullptr;
2191 }
2192 TheASTReader->resetForReload();
2193 TheASTReader->loadGlobalIndex();
2194 GlobalIndex = TheASTReader->getGlobalIndex();
2195 }
2196 // For finding modules needing to be imported for fixit messages,
2197 // we need to make the global index cover all modules, so we do that here.
2198 if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) {
2200 bool RecreateIndex = false;
2202 E = MMap.module_end(); I != E; ++I) {
2203 Module *TheModule = I->second;
2204 OptionalFileEntryRef Entry = TheModule->getASTFile();
2205 if (!Entry) {
2207 Path.emplace_back(TriggerLoc,
2208 getPreprocessor().getIdentifierInfo(TheModule->Name));
2209 std::reverse(Path.begin(), Path.end());
2210 // Load a module as hidden. This also adds it to the global index.
2211 loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
2212 RecreateIndex = true;
2213 }
2214 }
2215 if (RecreateIndex) {
2216 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
2218 getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) {
2219 // FIXME As above, this drops the error on the floor.
2220 consumeError(std::move(Err));
2221 return nullptr;
2222 }
2223 TheASTReader->resetForReload();
2224 TheASTReader->loadGlobalIndex();
2225 GlobalIndex = TheASTReader->getGlobalIndex();
2226 }
2227 HaveFullGlobalModuleIndex = true;
2228 }
2229 return GlobalIndex;
2230}
2231
2232// Check global module index for missing imports.
2233bool
2235 SourceLocation TriggerLoc) {
2236 // Look for the symbol in non-imported modules, but only if an error
2237 // actually occurred.
2238 if (!buildingModule()) {
2239 // Load global module index, or retrieve a previously loaded one.
2241 TriggerLoc);
2242
2243 // Only if we have a global index.
2244 if (GlobalIndex) {
2245 GlobalModuleIndex::HitSet FoundModules;
2246
2247 // Find the modules that reference the identifier.
2248 // Note that this only finds top-level modules.
2249 // We'll let diagnoseTypo find the actual declaration module.
2250 if (GlobalIndex->lookupIdentifier(Name, FoundModules))
2251 return true;
2252 }
2253 }
2254
2255 return false;
2256}
2257void CompilerInstance::resetAndLeakSema() { llvm::BuryPointer(takeSema()); }
2258
2261 ExternalSemaSrc = std::move(ESS);
2262}
Defines the clang::ASTContext interface.
Defines the Diagnostic-related interfaces.
static void collectVFSEntries(CompilerInstance &CI, std::shared_ptr< ModuleDependencyCollector > MDC)
static bool EnableCodeCompletion(Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column)
static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName)
Compile a module in a separate compiler instance and read the AST, returning true if the module compi...
static void SetupSerializedDiagnostics(DiagnosticOptions &DiagOpts, DiagnosticsEngine &Diags, StringRef OutputFile)
static bool compileModuleAndReadASTBehindLock(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName)
Compile a module in a separate compiler instance and read the AST, returning true if the module compi...
static Language getLanguageFromOptions(const LangOptions &LangOpts)
Determine the appropriate source input kind based on language options.
static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro, Module *Mod, SourceLocation ImportLoc)
Diagnose differences between the current definition of the given configuration macro and the definiti...
static void collectHeaderMaps(const HeaderSearch &HS, std::shared_ptr< ModuleDependencyCollector > MDC)
static ModuleSource selectModuleSource(Module *M, StringRef ModuleName, std::string &ModuleFilename, const std::map< std::string, std::string, std::less<> > &BuiltModules, HeaderSearch &HS)
Select a source for loading the named module and compute the filename to load it from.
static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName, bool *OutOfDate, bool *Missing)
Read the AST right after compiling the module.
static void InitializeFileRemapping(DiagnosticsEngine &Diags, SourceManager &SourceMgr, FileManager &FileMgr, const PreprocessorOptions &InitOpts)
static void collectIncludePCH(CompilerInstance &CI, std::shared_ptr< ModuleDependencyCollector > MDC)
static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File, FileManager &FileMgr)
static void checkConfigMacros(Preprocessor &PP, Module *M, SourceLocation ImportLoc)
static bool compileModuleAndReadAST(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, StringRef ModuleFileName)
Compile a module in a separate compiler instance and read the AST, returning true if the module compi...
static void SetUpDiagnosticLog(DiagnosticOptions &DiagOpts, const CodeGenOptions *CodeGenOpts, DiagnosticsEngine &Diags)
Defines the clang::FileManager interface and associated types.
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
Defines the clang::Preprocessor interface.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
Defines the SourceManager interface.
Defines utilities for dealing with stack allocation and stack space.
Defines version macros and version-related utility functions for Clang.
virtual void Initialize(ASTContext &Context)
Initialize - This is called to initialize the consumer, providing the ASTContext.
Definition ASTConsumer.h:48
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
void setASTMutationListener(ASTMutationListener *Listener)
Attach an AST mutation listener to the AST context.
void setExternalSource(IntrusiveRefCntPtr< ExternalASTSource > Source)
Attach an external AST source to the AST context.
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Abstract interface for callback invocations by the ASTReader.
Definition ASTReader.h:117
RAII object to temporarily add an AST callback listener.
Definition ASTReader.h:1920
@ ARR_Missing
The client can handle an AST file that cannot load because it is missing.
Definition ASTReader.h:1833
@ ARR_None
The client can't handle any AST loading failures.
Definition ASTReader.h:1829
@ ARR_ConfigurationMismatch
The client can handle an AST file that cannot load because it's compiled configuration doesn't match ...
Definition ASTReader.h:1846
@ 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:1837
@ 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:1850
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.
ASTReadResult
The result of reading the control block of an AST file, which can fail for various reasons.
Definition ASTReader.h:450
@ Success
The control block was read successfully.
Definition ASTReader.h:453
@ ConfigurationMismatch
The AST file was written with a different language/target configuration.
Definition ASTReader.h:470
@ OutOfDate
The AST file is out-of-date relative to its input files, and needs to be regenerated.
Definition ASTReader.h:463
@ Failure
The AST file itself appears corrupted.
Definition ASTReader.h:456
@ VersionMismatch
The AST file was written by a different version of Clang.
Definition ASTReader.h:466
@ HadErrors
The AST file has errors.
Definition ASTReader.h:473
@ Missing
The AST file was missing.
Definition ASTReader.h:459
ChainedDiagnosticConsumer - Chain two diagnostic clients so that diagnostics go to the first client a...
Abstract interface for a consumer of code-completion information.
Options controlling the behavior of code completion.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
std::string DwarfDebugFlags
The string to embed in the debug information for the compile unit, if non-empty.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void createPCHExternalASTSource(StringRef Path, DisableValidationForModuleKind DisableValidation, bool AllowPCHWithCompilerErrors, void *DeserializationListener, bool OwnDeserializationListener)
Create an external AST source to read a PCH file and attach it to the AST context.
DiagnosticConsumer & getDiagnosticClient() const
void createPreprocessor(TranslationUnitKind TUKind)
Create the preprocessor, using the invocation, file, and source managers, and replace any existing on...
bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Check global module index for missing imports.
void setOutputManager(IntrusiveRefCntPtr< llvm::vfs::OutputBackend > NewOutputs)
Set the output manager.
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
DependencyOutputOptions & getDependencyOutputOpts()
TargetInfo * getAuxTarget() const
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
GlobalModuleIndex * loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Load, create, or return global module.
raw_ostream & getVerboseOutputStream()
Get the current stream for verbose output.
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="", bool RemoveFileOnSignal=true, bool CreateMissingDirectories=false, bool ForceUseTemporary=false)
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
void setExternalSemaSource(IntrusiveRefCntPtr< ExternalSemaSource > ESS)
bool compileModule(SourceLocation ImportLoc, StringRef ModuleName, StringRef ModuleFileName, CompilerInstance &Instance)
Compile a module file for the given module, using the options provided by the importing compiler inst...
std::string getSpecificModuleCachePath()
std::unique_ptr< CompilerInstance > cloneForModuleCompile(SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName, std::optional< ThreadSafeCloneConfig > ThreadSafeConfig=std::nullopt)
Creates a new CompilerInstance for compiling a module.
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective) override
Attempt to load the given module.
FileSystemOptions & getFileSystemOpts()
bool InitializeSourceManager(const FrontendInputFile &Input)
InitializeSourceManager - Initialize the source manager to set InputFile as the main file.
void createFileManager()
Create the file manager and replace any existing one with it.
void setVirtualFileSystem(IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
Use the given file system.
FileManager & getFileManager() const
Return the current file manager to the caller.
void setBuildGlobalModuleIndex(bool Build)
Set the flag indicating whether we should (re)build the global module index.
void createOutputManager()
Create an output manager.
std::unique_ptr< Sema > takeSema()
void printDiagnosticStats()
At the end of a compilation, print the number of warnings/errors.
void setASTConsumer(std::unique_ptr< ASTConsumer > Value)
setASTConsumer - Replace the current AST consumer; the compiler instance takes ownership of Value.
PreprocessorOutputOptions & getPreprocessorOutputOpts()
IntrusiveRefCntPtr< FileManager > getFileManagerPtr() const
ModuleCache & getModuleCache() const
IntrusiveRefCntPtr< ASTReader > getASTReader() const
void setTarget(TargetInfo *Value)
Replace the current Target.
void setModuleDepCollector(std::shared_ptr< ModuleDependencyCollector > Collector)
void addDependencyCollector(std::shared_ptr< DependencyCollector > Listener)
void createASTContext()
Create the AST context.
std::unique_ptr< raw_pwrite_stream > createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, bool UseTemporary, bool CreateMissingDirectories=false)
Create a new output file, optionally deriving the output path name, and add it to the list of tracked...
void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName, StringRef Source) override
Attempt to create the given module from the specified source buffer.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
ASTContext & getASTContext() const
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr() const
void LoadRequestedPlugins()
Load the list of plugins requested in the FrontendOptions.
TargetOptions & getTargetOpts()
void createVirtualFileSystem(IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS=llvm::vfs::getRealFileSystem(), DiagnosticConsumer *DC=nullptr)
Create a virtual file system instance based on the invocation.
void setASTReader(IntrusiveRefCntPtr< ASTReader > Reader)
FrontendOptions & getFrontendOpts()
std::shared_ptr< ModuleDependencyCollector > getModuleDepCollector() const
void setSema(Sema *S)
Replace the current Sema; the compiler instance takes ownership of S.
void setSourceManager(llvm::IntrusiveRefCntPtr< SourceManager > Value)
setSourceManager - Replace the current source manager.
void setASTContext(llvm::IntrusiveRefCntPtr< ASTContext > Value)
setASTContext - Replace the current AST context.
HeaderSearchOptions & getHeaderSearchOpts()
void createFrontendTimer()
Create the frontend timer and replace any existing one with it.
void createSourceManager()
Create the source manager and replace any existing one with it.
CompilerInvocation & getInvocation()
void setVerboseOutputStream(raw_ostream &Value)
Replace the current stream for verbose output.
PreprocessorOptions & getPreprocessorOpts()
ASTConsumer & getASTConsumer() const
void setFileManager(IntrusiveRefCntPtr< FileManager > Value)
Replace the current file manager and virtual file system.
TargetInfo & getTarget() const
llvm::vfs::OutputBackend & getOutputManager()
llvm::vfs::FileSystem & getVirtualFileSystem() const
void createCodeCompletionConsumer()
Create a code completion consumer using the invocation; note that this will cause the source manager ...
void setCodeCompletionConsumer(CodeCompleteConsumer *Value)
setCodeCompletionConsumer - Replace the current code completion consumer; the compiler instance takes...
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
void clearOutputFiles(bool EraseFiles)
clearOutputFiles - Clear the output file list.
DiagnosticOptions & getDiagnosticOpts()
llvm::vfs::OutputBackend & getOrCreateOutputManager()
CodeGenOptions & getCodeGenOpts()
SourceManager & getSourceManager() const
Return the current source manager.
void setDiagnostics(llvm::IntrusiveRefCntPtr< DiagnosticsEngine > Value)
setDiagnostics - Replace the current diagnostics engine.
bool shouldBuildGlobalModuleIndex() const
Indicates whether we should (re)build the global module index.
APINotesOptions & getAPINotesOpts()
std::unique_ptr< raw_pwrite_stream > createNullOutputFile()
void setAuxTarget(TargetInfo *Value)
Replace the current AuxTarget.
void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, SourceLocation ImportLoc) override
Make the given module visible.
bool loadModuleFile(StringRef FileName, serialization::ModuleFile *&LoadedModuleFile)
void setPreprocessor(std::shared_ptr< Preprocessor > Value)
Replace the current preprocessor.
void createSema(TranslationUnitKind TUKind, CodeCompleteConsumer *CompletionConsumer)
Create the Sema object to be used for parsing.
LangOptions & getLangOpts()
Mutable getters.
FrontendOptions & getFrontendOpts()
std::string getModuleHash() const
Retrieve a module hash string that is suitable for uniquely identifying the conditions under which th...
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
ShowIncludesDestination ShowIncludesDest
Destination of cl.exe style /showIncludes info.
std::string DOTOutputFile
The file to write GraphViz-formatted header dependencies to.
std::string ModuleDependencyOutputDir
The directory to copy module dependencies to when collecting them.
std::string OutputFile
The file to write dependency output to.
std::string HeaderIncludeOutputFile
The file to write header include output to.
unsigned ShowHeaderIncludes
Show header inclusions (-H).
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
unsigned getNumErrors() const
virtual void finish()
Callback to inform the diagnostic client that processing of all source files has ended.
unsigned getNumWarnings() const
static llvm::IntrusiveRefCntPtr< DiagnosticIDs > create()
Options for controlling the compiler diagnostics engine.
std::string DiagnosticLogFile
The file to log diagnostic output to.
std::vector< std::string > SystemHeaderWarningsModules
The list of -Wsystem-headers-in-module=... options used to override whether -Wsystem-headers is enabl...
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:232
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
bool hasErrorOccurred() const
Definition Diagnostic.h:872
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
std::unique_ptr< DiagnosticConsumer > takeClient()
Return the current diagnostic client along with ownership of that client.
Definition Diagnostic.h:615
DiagnosticConsumer * getClient()
Definition Diagnostic.h:607
Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc) const
Based on the way the client configured the DiagnosticsEngine object, classify the specified diagnosti...
Definition Diagnostic.h:966
bool ownsClient() const
Determine whether this DiagnosticsEngine object own its client.
Definition Diagnostic.h:611
StringRef getName() const
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
off_t getSize() const
Definition FileEntry.h:350
StringRef getName() const
The name of this FileEntry.
Definition FileEntry.h:61
StringRef getNameAsRequested() const
The name of this FileEntry, as originally requested without applying any remappings for VFS 'use-exte...
Definition FileEntry.h:68
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool isValid() const
bool isInvalid() const
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:53
void AddStats(const FileManager &Other)
Import statistics from a child FileManager and add them to this current FileManager.
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Get a FileEntryRef if it exists, without doing anything on error.
void PrintStats() const
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition Diagnostic.h:140
Abstract base class for actions which can be performed by the frontend.
virtual void EndSourceFile()
Perform any per-file post processing, deallocate per-file objects, and run statistics and output file...
bool PrepareToExecute(CompilerInstance &CI)
Prepare the action to execute on the given compiler instance.
llvm::Error Execute()
Set the source manager's main input file, and run the action.
bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input)
Prepare the action for processing the input file Input.
virtual bool isModelParsingAction() const
Is this action invoked on a model file?
An input file for the front end.
llvm::MemoryBufferRef getBuffer() const
InputKind getKind() const
StringRef getFile() const
FrontendOptions - Options for controlling the behavior of the frontend.
unsigned BuildingImplicitModule
Whether we are performing an implicit module build.
unsigned AllowPCMWithCompilerErrors
Output (and read) PCM files regardless of compiler errors.
unsigned BuildingImplicitModuleUsesLock
Whether to use a filesystem lock when building implicit modules.
unsigned ModulesShareFileManager
Whether to share the FileManager when building modules.
std::optional< std::string > AuxTargetCPU
Auxiliary target CPU for CUDA/HIP compilation.
std::string StatsFile
Filename to write statistics to.
std::string OutputFile
The output file, if any.
std::string ActionName
The name of the action to run when using a plugin action.
ParsedSourceLocation CodeCompletionAt
If given, enable code completion at the provided location.
std::string OriginalModuleMap
When the input is a module map, the original module map file from which that map was inferred,...
unsigned GenerateGlobalModuleIndex
Whether we can generate the global module index if needed.
unsigned DisableFree
Disable memory freeing on exit.
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
frontend::ActionKind ProgramAction
The frontend action to perform.
std::optional< std::vector< std::string > > AuxTargetFeatures
Auxiliary target features for CUDA/HIP compilation.
A global index for a set of module files, providing information about the identifiers within those mo...
llvm::SmallPtrSet< ModuleFile *, 4 > HitSet
A set of module files in which we found a result.
bool lookupIdentifier(llvm::StringRef Name, HitSet &Hits)
Look for all of the module files with information about the given identifier, e.g....
static llvm::Error writeIndex(FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, llvm::StringRef Path)
Write a global index into the given.
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
unsigned ModulesForceValidateUserHeaders
Whether to force the validation of user input files when a module is loaded (even despite the build s...
std::map< std::string, std::string, std::less<> > PrebuiltModuleFiles
The mapping of module names to prebuilt module files.
std::vector< std::string > PrebuiltModulePaths
The directories used to load prebuilt module files.
unsigned ModulesValidateSystemHeaders
Whether to validate system input files when a module is loaded.
unsigned EnablePrebuiltImplicitModules
Also search for prebuilt implicit modules in the prebuilt module cache path.
std::string Sysroot
If non-empty, the directory to use as a "virtual system root" for include paths.
unsigned ModulesValidateOncePerBuildSession
If true, skip verifying input files used by modules if the module was already verified during this bu...
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
Module * lookupModule(StringRef ModuleName, SourceLocation ImportLoc=SourceLocation(), bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
void getHeaderMapFileNames(SmallVectorImpl< std::string > &Names) const
Get filenames for all registered header maps.
std::string getPrebuiltImplicitModuleFileName(Module *Module)
Retrieve the name of the prebuilt module file that should be used to load the given module.
const HeaderSearchOptions & getHeaderSearchOpts() const
Retrieve the header-search options with which this header search was initialized.
std::string getCachedModuleFileName(Module *Module)
Retrieve the name of the cached module file that should be used to load the given module.
ModuleMap & getModuleMap()
Retrieve the module map.
std::string getPrebuiltModuleFileName(StringRef ModuleName, bool FileMapOnly=false)
Retrieve the name of the prebuilt module file that should be used to load a module with the given nam...
One of these records is kept for each identifier that is lexed.
bool hadMacroDefinition() const
Returns true if this identifier was #defined to some value at any moment.
The kind of a file that we've been handed as an input.
Format getFormat() const
@ FPE_Default
Used internally to represent initial unspecified value.
@ FPE_Ignore
Assume that floating-point exceptions are masked.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
std::string ModuleName
The module currently being compiled as specified by -fmodule-name.
Encapsulates the data about a macro definition (e.g.
Definition MacroInfo.h:39
The module cache used for compiling modules implicitly.
Definition ModuleCache.h:26
virtual void prepareForGetLock(StringRef ModuleFilename)=0
May perform any work that only needs to be performed once for multiple calls getLock() with the same ...
virtual void updateModuleTimestamp(StringRef ModuleFilename)=0
Updates 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.
Describes the result of attempting to load a module.
bool buildingModule() const
Returns true if this instance is building a module.
ModuleLoader(bool BuildingModule=false)
llvm::StringMap< Module * >::const_iterator module_iterator
Definition ModuleMap.h:745
module_iterator module_begin() const
Definition ModuleMap.h:747
OptionalFileEntryRef getModuleMapFileForUniquing(const Module *M) const
std::optional< Module * > getCachedModuleLoad(const IdentifierInfo &II)
Return a cached module load.
Definition ModuleMap.h:759
module_iterator module_end() const
Definition ModuleMap.h:748
FileID getContainingModuleMapFileID(const Module *Module) const
Retrieve the module map file containing the definition of the given module.
void resolveLinkAsDependencies(Module *Mod)
Use PendingLinkAsModule information to mark top level link names that are going to be replaced by exp...
Definition ModuleMap.cpp:51
void cacheModuleLoad(const IdentifierInfo &II, Module *M)
Cache a module load. M might be nullptr.
Definition ModuleMap.h:754
Module * findOrLoadModule(StringRef Name)
Describes a module or submodule.
Definition Module.h:144
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition Module.h:732
Module * findSubmodule(StringRef Name) const
Find the submodule with the given name.
Definition Module.cpp:350
std::vector< std::string > ConfigMacros
The set of "configuration macros", which are macros that (intentionally) change how this module is bu...
Definition Module.h:528
unsigned IsUnimportable
Whether this module has declared itself unimportable, either because it's missing a requirement from ...
Definition Module.h:361
NameVisibilityKind
Describes the visibility of the various names within a particular module.
Definition Module.h:443
@ Hidden
All of the names in this module are hidden.
Definition Module.h:445
void print(raw_ostream &OS, unsigned Indent=0, bool Dump=false) const
Print the module map for this module to the given stream.
Definition Module.cpp:463
SourceLocation DefinitionLoc
The location of the module definition.
Definition Module.h:150
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers).
Definition Module.h:389
std::string Name
The name of this module.
Definition Module.h:147
llvm::iterator_range< submodule_iterator > submodules()
Definition Module.h:838
OptionalDirectoryEntryRef Directory
The build directory of this module.
Definition Module.h:198
unsigned IsFromModuleFile
Whether this module was loaded from a module file.
Definition Module.h:376
unsigned HasIncompatibleModuleFile
Whether we tried and failed to load a module file for this module.
Definition Module.h:365
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition Module.cpp:239
unsigned IsAvailable
Whether this module is available in the current translation unit.
Definition Module.h:372
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition Module.h:722
OptionalFileEntryRef getASTFile() const
The serialized AST file for this module, if one was created.
Definition Module.h:737
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
@ ReplaceAction
Replace the main action.
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
std::vector< std::pair< std::string, std::string > > RemappedFiles
The set of file remappings, which take existing files on the system (the first part of each pair) and...
std::pair< unsigned, bool > PrecompiledPreambleBytes
If non-zero, the implicit PCH include is actually a precompiled preamble that covers this number of b...
bool RemappedFilesKeepOriginalName
True if the SourceManager should report the original file name for contents of files that were remapp...
bool RetainRemappedFileBuffers
Whether the compiler instance should retain (i.e., not free) the buffers associated with remapped fil...
bool DetailedRecord
Whether we should maintain a detailed record of all macro definitions and expansions.
std::string ImplicitPCHInclude
The implicit PCH included at the start of the translation unit, or empty.
DisableValidationForModuleKind DisablePCHOrModuleValidation
Whether to disable most of the normal validation performed on precompiled headers and module files.
std::vector< std::pair< std::string, bool > > Macros
std::vector< std::pair< std::string, llvm::MemoryBuffer * > > RemappedFileBuffers
The set of file-to-buffer remappings, which take existing files on the system (the first part of each...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
MacroDirective * getLocalMacroDirectiveHistory(const IdentifierInfo *II) const
Given an identifier, return the latest non-imported macro directive for that identifier.
bool SetCodeCompletionPoint(FileEntryRef File, unsigned Line, unsigned Column)
Specify the point at which code-completion will be performed.
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
SourceManager & getSourceManager() const
static bool checkModuleIsAvailable(const LangOptions &LangOpts, const TargetInfo &TargetInfo, const Module &M, DiagnosticsEngine &Diags)
Check that the given module is available, producing a diagnostic if not.
FileManager & getFileManager() const
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
HeaderSearch & getHeaderSearchInfo() const
DiagnosticsEngine & getDiagnostics() const
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
A simple code-completion consumer that prints the results it receives in a simple format.
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:854
ASTReaderListenter implementation to set SuggestedPredefines of ASTReader which is required to use a ...
Definition ASTReader.h:362
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
ModuleBuildStack getModuleBuildStack() const
Retrieve the module build stack.
A trivial tuple used to represent a source range.
CharacteristicKind getFileCharacteristic() const
Return whether this is a system header or not.
This is a discriminated union of FileInfo and ExpansionInfo.
const FileInfo & getFile() const
Exposes information about the current target.
Definition TargetInfo.h:226
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, TargetOptions &Opts)
Construct a target for the given options.
Definition Targets.cpp:784
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
virtual void setAuxTarget(const TargetInfo *Aux)
void noSignedCharForObjCBool()
Definition TargetInfo.h:936
virtual void adjust(DiagnosticsEngine &Diags, LangOptions &Opts, const TargetInfo *Aux)
Set forced language options.
std::string CPU
If given, the name of the target CPU to generate code for.
VerifyDiagnosticConsumer - Create a diagnostic client which will use markers in the input source to c...
Information about a module that has been loaded by the ASTReader.
Definition ModuleFile.h:130
Defines the clang::TargetInfo interface.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
@ PluginAction
Run a plugin action,.
@ RewriteObjC
ObjC->C Rewriter.
bool Inv(InterpState &S, CodePtr OpPC)
Definition Interp.h:738
@ MK_PCH
File is a PCH file treated as such.
Definition ModuleFile.h:51
@ MK_Preamble
File is a PCH file treated as the preamble.
Definition ModuleFile.h:54
@ MK_ExplicitModule
File is an explicitly-loaded module.
Definition ModuleFile.h:48
@ MK_ImplicitModule
File is an implicitly-loaded module.
Definition ModuleFile.h:45
@ MK_PrebuiltModule
File is from a prebuilt module path.
Definition ModuleFile.h:60
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions &DiagOpts, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
@ HeaderSearch
Remove unused header search paths including header maps.
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
ArrayRef< IdentifierLoc > ModuleIdPath
A sequence of identifier/location pairs used to describe a particular module or submodule,...
ArrayRef< std::pair< std::string, FullSourceLoc > > ModuleBuildStack
The stack used when building modules on demand, which is used to provide a link between the source ma...
void ApplyHeaderSearchOptions(HeaderSearch &HS, const HeaderSearchOptions &HSOpts, const LangOptions &Lang, const llvm::Triple &triple)
Apply the header search options to get given HeaderSearch object.
@ Success
Annotation was successful.
Definition Parser.h:65
void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts, const PCHContainerReader &PCHContainerRdr, const FrontendOptions &FEOpts, const CodeGenOptions &CodeGenOpts)
InitializePreprocessor - Initialize the preprocessor getting it and the environment ready to process ...
LLVM_READONLY bool isAlphanumeric(unsigned char c)
Return true if this character is an ASCII letter or digit: [a-zA-Z0-9].
Definition CharInfo.h:138
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
Language
The language for the input, used to select and validate the language standard and possible actions.
@ C
Languages that the frontend can parse and compile.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
void normalizeModuleCachePath(FileManager &FileMgr, StringRef Path, SmallVectorImpl< char > &NormalizedPath)
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition Stack.h:26
IntrusiveRefCntPtr< ModuleCache > createCrossProcessModuleCache()
Creates new ModuleCache backed by a file system directory that may be operated on by multiple process...
void noteBottomOfStack(bool ForceSet=false)
Call this once on each thread, as soon after starting the thread as feasible, to note the approximate...
Definition Stack.cpp:20
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, llvm::vfs::FileSystem &VFS, bool ReportDiags=true)
ProcessWarningOptions - Initialize the diagnostic client and process the warning options specified on...
Definition Warnings.cpp:46
TranslationUnitKind
Describes the kind of translation unit being processed.
void AttachHeaderIncludeGen(Preprocessor &PP, const DependencyOutputOptions &DepOpts, bool ShowAllHeaders=false, StringRef OutputPath={}, bool ShowDepth=true, bool MSStyle=false)
AttachHeaderIncludeGen - Create a header include list generator, and attach it to the given preproces...
DisableValidationForModuleKind
Whether to disable the normal validation performed on precompiled headers and module files when they ...
@ Other
Other implicit parameter.
Definition Decl.h:1746
Visibility
Describes the different kinds of visibility that a declaration may have.
Definition Visibility.h:34
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...
A source location that has been parsed on the command line.