clang 24.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"
21#include "clang/Basic/Stack.h"
23#include "clang/Basic/Version.h"
24#include "clang/Config/config.h"
41#include "clang/Sema/Sema.h"
48#include "llvm/ADT/IntrusiveRefCntPtr.h"
49#include "llvm/ADT/STLExtras.h"
50#include "llvm/ADT/ScopeExit.h"
51#include "llvm/ADT/Statistic.h"
52#include "llvm/Config/llvm-config.h"
53#include "llvm/Plugins/PassPlugin.h"
54#include "llvm/Support/AdvisoryLock.h"
55#include "llvm/Support/BuryPointer.h"
56#include "llvm/Support/CrashRecoveryContext.h"
57#include "llvm/Support/Errc.h"
58#include "llvm/Support/FileSystem.h"
59#include "llvm/Support/MemoryBuffer.h"
60#include "llvm/Support/Path.h"
61#include "llvm/Support/Signals.h"
62#include "llvm/Support/SmallVectorMemoryBuffer.h"
63#include "llvm/Support/Threading.h"
64#include "llvm/Support/TimeProfiler.h"
65#include "llvm/Support/Timer.h"
66#include "llvm/Support/VirtualFileSystem.h"
67#include "llvm/Support/VirtualOutputBackends.h"
68#include "llvm/Support/VirtualOutputError.h"
69#include "llvm/Support/raw_ostream.h"
70#include "llvm/TargetParser/Host.h"
71#include <optional>
72#include <time.h>
73#include <utility>
74
75using namespace clang;
76
77CompilerInstance::CompilerInstance(
78 std::shared_ptr<CompilerInvocation> Invocation,
79 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
80 std::shared_ptr<ModuleCache> ModCache)
81 : ModuleLoader(/*BuildingModule=*/ModCache != nullptr),
82 Invocation(std::move(Invocation)),
83 ModCache(ModCache ? std::move(ModCache)
85 ThePCHContainerOperations(std::move(PCHContainerOps)) {
86 assert(this->Invocation && "Invocation must not be null");
87}
88
90 assert(OutputFiles.empty() && "Still output files in flight?");
91}
92
94 return (BuildGlobalModuleIndex ||
95 (TheASTReader && TheASTReader->isGlobalIndexUnavailable() &&
96 getFrontendOpts().GenerateGlobalModuleIndex)) &&
97 !DisableGeneratingGlobalModuleIndex;
98}
99
104
106 OwnedVerboseOutputStream.reset();
107 VerboseOutputStream = &Value;
108}
109
110void CompilerInstance::setVerboseOutputStream(std::unique_ptr<raw_ostream> Value) {
111 OwnedVerboseOutputStream.swap(Value);
112 VerboseOutputStream = OwnedVerboseOutputStream.get();
113}
114
117
119 // Create the target instance.
122 if (!hasTarget())
123 return false;
124
125 if (getLangOpts().SYCLIsDevice && !getTarget().getTriple().isGPU()) {
126 getDiagnostics().Report(diag::err_sycl_device_invalid_target)
127 << getTarget().getTriple().str();
128 return false;
129 }
130
131 // Check whether AuxTarget exists, if not, then create TargetInfo for the
132 // other side of CUDA/OpenMP/SYCL compilation.
133 if (!getAuxTarget() &&
134 (getLangOpts().CUDA || getLangOpts().isTargetDevice()) &&
135 !getFrontendOpts().AuxTriple.empty()) {
136 auto &TO = AuxTargetOpts = std::make_unique<TargetOptions>();
137 TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
138 if (getFrontendOpts().AuxTargetCPU)
139 TO->CPU = *getFrontendOpts().AuxTargetCPU;
140 if (getFrontendOpts().AuxTargetFeatures)
141 TO->FeaturesAsWritten = *getFrontendOpts().AuxTargetFeatures;
142 TO->HostTriple = getTarget().getTriple().str();
144 }
145
146 if (!getTarget().hasStrictFP() && !getLangOpts().ExpStrictFP) {
147 if (getLangOpts().RoundingMath) {
148 getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_rounding);
149 getLangOpts().RoundingMath = false;
150 }
151 auto FPExc = getLangOpts().getFPExceptionMode();
152 if (FPExc != LangOptions::FPE_Default && FPExc != LangOptions::FPE_Ignore) {
153 getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_exceptions);
154 getLangOpts().setFPExceptionMode(LangOptions::FPE_Ignore);
155 }
156 // FIXME: can we disable FEnvAccess?
157 }
158
159 // We should do it here because target knows nothing about
160 // language options when it's being created.
161 if (getLangOpts().OpenCL &&
162 !getTarget().validateOpenCLTarget(getLangOpts(), getDiagnostics()))
163 return false;
164
165 // Inform the target of the language options.
166 // FIXME: We shouldn't need to do this, the target should be immutable once
167 // created. This complexity should be lifted elsewhere.
169
170 if (auto *Aux = getAuxTarget())
171 getTarget().setAuxTarget(Aux);
172
173 return true;
174}
175
177 assert(Value == nullptr ||
178 getVirtualFileSystemPtr() == Value->getVirtualFileSystemPtr());
179 FileMgr = std::move(Value);
180}
181
186
187void CompilerInstance::setPreprocessor(std::shared_ptr<Preprocessor> Value) {
188 PP = std::move(Value);
189}
190
193 Context = std::move(Value);
194
195 if (Context && Consumer)
197}
198
200 TheSema.reset(S);
201}
202
203void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
204 Consumer = std::move(Value);
205
206 if (Context && Consumer)
208}
209
213
214std::unique_ptr<Sema> CompilerInstance::takeSema() {
215 return std::move(TheSema);
216}
217
219 return TheASTReader;
220}
222 assert(ModCache.get() == &Reader->getModuleManager().getModuleCache() &&
223 "Expected ASTReader to use the same PCM cache");
224 TheASTReader = std::move(Reader);
225}
226
227std::shared_ptr<ModuleDependencyCollector>
229 return ModuleDepCollector;
230}
231
233 std::shared_ptr<ModuleDependencyCollector> Collector) {
234 ModuleDepCollector = std::move(Collector);
235}
236
237static void collectHeaderMaps(const HeaderSearch &HS,
238 std::shared_ptr<ModuleDependencyCollector> MDC) {
239 SmallVector<std::string, 4> HeaderMapFileNames;
240 HS.getHeaderMapFileNames(HeaderMapFileNames);
241 for (auto &Name : HeaderMapFileNames)
242 MDC->addFile(Name);
243}
244
246 std::shared_ptr<ModuleDependencyCollector> MDC) {
247 const PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
248 if (PPOpts.ImplicitPCHInclude.empty())
249 return;
250
251 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
253 auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude);
254 if (!PCHDir) {
255 MDC->addFile(PCHInclude);
256 return;
257 }
258
259 std::error_code EC;
260 SmallString<128> DirNative;
261 llvm::sys::path::native(PCHDir->getName(), DirNative);
262 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
264 for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
265 Dir != DirEnd && !EC; Dir.increment(EC)) {
266 // Check whether this is an AST file. ASTReader::isAcceptableASTFile is not
267 // used here since we're not interested in validating the PCH at this time,
268 // but only to check whether this is a file containing an AST.
270 Dir->path(), FileMgr, CI.getModuleCache(),
272 /*FindModuleFileExtensions=*/false, Validator,
273 /*ValidateDiagnosticOptions=*/false))
274 MDC->addFile(Dir->path());
275 }
276}
277
279 std::shared_ptr<ModuleDependencyCollector> MDC) {
280 // Collect all VFS found.
282 CI.getVirtualFileSystem().visit([&](llvm::vfs::FileSystem &VFS) {
283 if (auto *RedirectingVFS = dyn_cast<llvm::vfs::RedirectingFileSystem>(&VFS))
284 llvm::vfs::collectVFSEntries(*RedirectingVFS, VFSEntries);
285 });
286
287 for (auto &E : VFSEntries)
288 MDC->addFile(E.VPath, E.RPath);
289}
290
293 bool ShouldOwnClient = false;
294 if (!DC) {
295 DC = new DiagnosticConsumer;
296 ShouldOwnClient = true;
297 }
298
299 DiagnosticOptions DiagOpts;
300 DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DC,
301 ShouldOwnClient);
302
304 std::move(BaseFS));
305 // FIXME: Should this go into createVFSFromCompilerInvocation?
306 if (getFrontendOpts().ShowStats)
307 VFS =
308 llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(std::move(VFS));
309}
310
311// Diagnostics
313 const CodeGenOptions *CodeGenOpts,
314 DiagnosticsEngine &Diags) {
315 std::error_code EC;
316 std::unique_ptr<raw_ostream> StreamOwner;
317 raw_ostream *OS = &llvm::errs();
318 if (DiagOpts.DiagnosticLogFile != "-") {
319 // Create the output stream.
320 auto FileOS = std::make_unique<llvm::raw_fd_ostream>(
321 DiagOpts.DiagnosticLogFile, EC,
322 llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF);
323 if (EC) {
324 Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
325 << DiagOpts.DiagnosticLogFile << EC.message();
326 } else {
327 FileOS->SetUnbuffered();
328 OS = FileOS.get();
329 StreamOwner = std::move(FileOS);
330 }
331 }
332
333 // Chain in the diagnostic client which will log the diagnostics.
334 auto Logger = std::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts,
335 std::move(StreamOwner));
336 if (CodeGenOpts)
337 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
338 if (Diags.ownsClient()) {
339 Diags.setClient(
340 new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
341 } else {
342 Diags.setClient(
343 new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger)));
344 }
345}
346
348 DiagnosticsEngine &Diags,
349 StringRef OutputFile) {
350 auto SerializedConsumer =
351 clang::serialized_diags::create(OutputFile, DiagOpts);
352
353 if (Diags.ownsClient()) {
355 Diags.takeClient(), std::move(SerializedConsumer)));
356 } else {
358 Diags.getClient(), std::move(SerializedConsumer)));
359 }
360}
361
363 bool ShouldOwnClient) {
365 Client, ShouldOwnClient, &getCodeGenOpts());
366}
367
369 llvm::vfs::FileSystem &VFS, DiagnosticOptions &Opts,
370 DiagnosticConsumer *Client, bool ShouldOwnClient,
371 const CodeGenOptions *CodeGenOpts) {
372 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(
373 DiagnosticIDs::create(), Opts);
374
375 // Create the diagnostic client for reporting errors or for
376 // implementing -verify.
377 if (Client) {
378 Diags->setClient(Client, ShouldOwnClient);
379 } else if (Opts.getFormat() == DiagnosticOptions::SARIF) {
380 Diags->setClient(new SARIFDiagnosticPrinter(llvm::errs(), Opts));
381 } else
382 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
383
384 // Chain in -verify checker, if requested.
385 if (Opts.VerifyDiagnostics)
386 Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
387
388 // Chain in -diagnostic-log-file dumper, if requested.
389 if (!Opts.DiagnosticLogFile.empty())
390 SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
391
392 if (!Opts.DiagnosticSerializationFile.empty())
394
395 // Configure our handling of diagnostics.
396 ProcessWarningOptions(*Diags, Opts, VFS);
397
398 return Diags;
399}
400
401// File Manager
402
404 assert(VFS && "CompilerInstance needs a VFS for creating FileManager");
405 FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(), VFS);
406}
407
408// Source Manager
409
411 assert(Diagnostics && "DiagnosticsEngine needed for creating SourceManager");
412 assert(FileMgr && "FileManager needed for creating SourceManager");
413 SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(getDiagnostics(),
415}
416
417// Initialize the remapping of files to alternative contents, e.g.,
418// those specified through other files.
420 SourceManager &SourceMgr,
422 const PreprocessorOptions &InitOpts) {
423 // Remap files in the source manager (with buffers).
424 for (const auto &RB : InitOpts.RemappedFileBuffers) {
425 // Create the file entry for the file that we're mapping from.
426 FileEntryRef FromFile =
427 FileMgr.getVirtualFileRef(RB.first, RB.second->getBufferSize(), 0);
428
429 // Override the contents of the "from" file with the contents of the
430 // "to" file. If the caller owns the buffers, then pass a MemoryBufferRef;
431 // otherwise, pass as a std::unique_ptr<MemoryBuffer> to transfer ownership
432 // to the SourceManager.
433 if (InitOpts.RetainRemappedFileBuffers)
434 SourceMgr.overrideFileContents(FromFile, RB.second->getMemBufferRef());
435 else
436 SourceMgr.overrideFileContents(
437 FromFile, std::unique_ptr<llvm::MemoryBuffer>(RB.second));
438 }
439
440 // Remap files in the source manager (with other files).
441 for (const auto &RF : InitOpts.RemappedFiles) {
442 // Find the file that we're mapping to.
443 OptionalFileEntryRef ToFile = FileMgr.getOptionalFileRef(RF.second);
444 if (!ToFile) {
445 Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second;
446 continue;
447 }
448
449 // Create the file entry for the file that we're mapping from.
450 FileEntryRef FromFile =
451 FileMgr.getVirtualFileRef(RF.first, ToFile->getSize(), 0);
452
453 // Override the contents of the "from" file with the contents of
454 // the "to" file.
455 SourceMgr.overrideFileContents(FromFile, *ToFile);
456 }
457
458 SourceMgr.setOverridenFilesKeepOriginalName(
460}
461
462// Preprocessor
463
466
467 // The AST reader holds a reference to the old preprocessor (if any).
468 TheASTReader.reset();
469
470 // Create the Preprocessor.
471 HeaderSearch *HeaderInfo =
474 PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOpts(),
476 getSourceManager(), *HeaderInfo, *this,
477 /*IdentifierInfoLookup=*/nullptr,
478 /*OwnsHeaderSearch=*/true, TUKind);
480 PP->Initialize(getTarget(), getAuxTarget());
481
482 if (PPOpts.DetailedRecord)
483 PP->createPreprocessingRecord();
484
485 // Apply remappings to the source manager.
486 InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
487 PP->getFileManager(), PPOpts);
488
489 // Predefine macros and configure the preprocessor.
492
493 // Initialize the header search object. In CUDA compilations, we use the aux
494 // triple (the host triple) to initialize our header search, since we need to
495 // find the host headers in order to compile the CUDA code.
496 const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple();
497 if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA &&
498 PP->getAuxTargetInfo())
499 HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
500
501 ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
502 PP->getLangOpts(), *HeaderSearchTriple);
503
504 PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
505
506 if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules) {
507 // FIXME: We already might've computed the context hash and the specific
508 // module cache path in `FrontendAction::BeginSourceFile()` when turning
509 // "-include-pch <DIR>" into "-include-pch <DIR>/<FILE>". Reuse those here.
510 PP->getHeaderSearchInfo().initializeModuleCachePath(
511 getInvocation().computeContextHash());
512 }
513
514 // Handle generating dependencies, if requested.
516 if (!DepOpts.OutputFile.empty())
517 addDependencyCollector(std::make_shared<DependencyFileGenerator>(DepOpts));
518 if (!DepOpts.DOTOutputFile.empty())
520 getHeaderSearchOpts().Sysroot);
521
522 // If we don't have a collector, but we are collecting module dependencies,
523 // then we're the top level compiler instance and need to create one.
524 if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) {
525 ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
527 }
528
529 // If there is a module dep collector, register with other dep collectors
530 // and also (a) collect header maps and (b) TODO: input vfs overlay files.
531 if (ModuleDepCollector) {
532 addDependencyCollector(ModuleDepCollector);
533 collectHeaderMaps(PP->getHeaderSearchInfo(), ModuleDepCollector);
534 collectIncludePCH(*this, ModuleDepCollector);
535 collectVFSEntries(*this, ModuleDepCollector);
536 }
537
538 // Modules need an output manager.
539 if (!hasOutputManager())
541
542 for (auto &Listener : DependencyCollectors)
543 Listener->attachToPreprocessor(*PP);
544
545 // Handle generating header include information, if requested.
546 if (DepOpts.ShowHeaderIncludes)
547 AttachHeaderIncludeGen(*PP, DepOpts);
548 if (!DepOpts.HeaderIncludeOutputFile.empty()) {
549 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
550 if (OutputPath == "-")
551 OutputPath = "";
552 AttachHeaderIncludeGen(*PP, DepOpts,
553 /*ShowAllHeaders=*/true, OutputPath,
554 /*ShowDepth=*/false);
555 }
556
558 AttachHeaderIncludeGen(*PP, DepOpts,
559 /*ShowAllHeaders=*/true, /*OutputPath=*/"",
560 /*ShowDepth=*/true, /*MSStyle=*/true);
561 }
562
563 if (GetDependencyDirectives)
564 PP->setDependencyDirectivesGetter(*GetDependencyDirectives);
565
566 if (auto EC = TextEncoding::setConvertersFromOptions(PP->getTextEncoding(),
567 getLangOpts()))
568 PP->getDiagnostics().Report(clang::diag::err_fe_text_encoding_config)
569 << PP->getTextEncoding().getLiteralEncoding();
570}
571
572// ASTContext
573
576 auto Context = llvm::makeIntrusiveRefCnt<ASTContext>(
577 getLangOpts(), PP.getSourceManager(), PP.getIdentifierTable(),
578 PP.getSelectorTable(), PP.getBuiltinInfo(), PP.TUKind);
579 Context->InitBuiltinTypes(getTarget(), getAuxTarget());
580 setASTContext(std::move(Context));
581}
582
583// ExternalASTSource
584
585namespace {
586// Helper to recursively read the module names for all modules we're adding.
587// We mark these as known and redirect any attempt to load that module to
588// the files we were handed.
589struct ReadModuleNames : ASTReaderListener {
590 Preprocessor &PP;
592
593 ReadModuleNames(Preprocessor &PP) : PP(PP) {}
594
595 void ReadModuleName(StringRef ModuleName) override {
596 // Keep the module name as a string for now. It's not safe to create a new
597 // IdentifierInfo from an ASTReader callback.
598 LoadedModules.push_back(ModuleName.str());
599 }
600
601 void registerAll() {
602 ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
603 for (const std::string &LoadedModule : LoadedModules)
604 MM.cacheModuleLoad(*PP.getIdentifierInfo(LoadedModule),
605 MM.findOrLoadModule(LoadedModule));
606 LoadedModules.clear();
607 }
608
609 void markAllUnavailable() {
610 for (const std::string &LoadedModule : LoadedModules) {
612 LoadedModule)) {
613 M->HasIncompatibleModuleFile = true;
614
615 // Mark module as available if the only reason it was unavailable
616 // was missing headers.
617 SmallVector<Module *, 2> Stack;
618 Stack.push_back(M);
619 while (!Stack.empty()) {
620 Module *Current = Stack.pop_back_val();
621 if (Current->IsUnimportable) continue;
622 Current->IsAvailable = true;
623 auto SubmodulesRange = Current->submodules();
624 llvm::append_range(Stack, SubmodulesRange);
625 }
626 }
627 }
628 LoadedModules.clear();
629 }
630};
631} // namespace
632
634 StringRef Path, DisableValidationForModuleKind DisableValidation,
635 bool AllowPCHWithCompilerErrors, void *DeserializationListener,
636 bool OwnDeserializationListener) {
638 TheASTReader = createPCHExternalASTSource(
639 Path, getHeaderSearchOpts().Sysroot, DisableValidation,
640 AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(),
642 getFrontendOpts().ModuleFileExtensions, DependencyCollectors,
643 DeserializationListener, OwnDeserializationListener, Preamble,
644 getFrontendOpts().UseGlobalModuleIndex);
645}
646
648 StringRef Path, StringRef Sysroot,
649 DisableValidationForModuleKind DisableValidation,
650 bool AllowPCHWithCompilerErrors, Preprocessor &PP, ModuleCache &ModCache,
651 ASTContext &Context, const PCHContainerReader &PCHContainerRdr,
652 const CodeGenOptions &CodeGenOpts,
653 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
654 ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
655 void *DeserializationListener, bool OwnDeserializationListener,
656 bool Preamble, bool UseGlobalModuleIndex) {
657 const HeaderSearchOptions &HSOpts =
658 PP.getHeaderSearchInfo().getHeaderSearchOpts();
659
660 auto Reader = llvm::makeIntrusiveRefCnt<ASTReader>(
661 PP, ModCache, &Context, PCHContainerRdr, CodeGenOpts, Extensions,
662 Sysroot.empty() ? "" : Sysroot.data(), DisableValidation,
663 AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false,
666 HSOpts.ValidateASTInputFilesContent, UseGlobalModuleIndex);
667
668 // We need the external source to be set up before we read the AST, because
669 // eagerly-deserialized declarations may use it.
670 Context.setExternalSource(Reader);
671
672 Reader->setDeserializationListener(
673 static_cast<ASTDeserializationListener *>(DeserializationListener),
674 /*TakeOwnership=*/OwnDeserializationListener);
675
676 for (auto &Listener : DependencyCollectors)
677 Listener->attachToASTReader(*Reader);
678
679 auto Listener = std::make_unique<ReadModuleNames>(PP);
680 auto &ListenerRef = *Listener;
681 ASTReader::ListenerScope ReadModuleNamesListener(*Reader,
682 std::move(Listener));
683
684 switch (Reader->ReadAST(ModuleFileName::makeExplicit(Path),
689 // Set the predefines buffer as suggested by the PCH reader. Typically, the
690 // predefines buffer will be empty.
691 PP.setPredefines(Reader->getSuggestedPredefines());
692 ListenerRef.registerAll();
693 return Reader;
694
696 // Unrecoverable failure: don't even try to process the input file.
697 break;
698
704 // No suitable PCH file could be found. Return an error.
705 break;
706 }
707
708 ListenerRef.markAllUnavailable();
709 Context.setExternalSource(nullptr);
710 return nullptr;
711}
712
713// Code Completion
714
716 StringRef Filename,
717 unsigned Line,
718 unsigned Column) {
719 // Tell the source manager to chop off the given file at a specific
720 // line and column.
721 auto Entry = PP.getFileManager().getOptionalFileRef(Filename);
722 if (!Entry) {
723 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
724 << Filename;
725 return true;
726 }
727
728 // Truncate the named file at the given line/column.
730 return false;
731}
732
735 if (!CompletionConsumer) {
737 getPreprocessor(), Loc.FileName, Loc.Line, Loc.Column,
738 getFrontendOpts().CodeCompleteOpts, llvm::outs()));
739 return;
741 Loc.Line, Loc.Column)) {
743 return;
744 }
745}
746
748 timerGroup.reset(new llvm::TimerGroup("clang", "Clang time report"));
749 FrontendTimer.reset(new llvm::Timer("frontend", "Front end", *timerGroup));
750}
751
754 StringRef Filename,
755 unsigned Line,
756 unsigned Column,
757 const CodeCompleteOptions &Opts,
758 raw_ostream &OS) {
759 if (EnableCodeCompletion(PP, Filename, Line, Column))
760 return nullptr;
761
762 // Set up the creation routine for code-completion.
763 return new PrintingCodeCompleteConsumer(Opts, OS);
764}
765
767 CodeCompleteConsumer *CompletionConsumer) {
768 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
769 TUKind, CompletionConsumer));
770
771 // Set up API notes.
772 TheSema->APINotes.setSwiftVersion(getAPINotesOpts().SwiftVersion);
773
774 // Attach the external sema source if there is any.
775 if (ExternalSemaSrc) {
776 TheSema->addExternalSource(ExternalSemaSrc);
777 ExternalSemaSrc->InitializeSema(*TheSema);
778 }
779
780 // If we're building a module and are supposed to load API notes,
781 // notify the API notes manager.
782 if (auto *currentModule = getPreprocessor().getCurrentModule()) {
783 (void)TheSema->APINotes.loadCurrentModuleAPINotes(
784 currentModule, getLangOpts().APINotesModules,
785 getAPINotesOpts().ModuleSearchPaths);
786 }
787}
788
789// Output Files
790
792 // The ASTConsumer can own streams that write to the output files.
793 assert(!hasASTConsumer() && "ASTConsumer should be reset");
794 if (!EraseFiles) {
795 for (auto &O : OutputFiles)
796 llvm::handleAllErrors(
797 O.keep(),
798 [&](const llvm::vfs::TempFileOutputError &E) {
799 getDiagnostics().Report(diag::err_unable_to_rename_temp)
800 << E.getTempPath() << E.getOutputPath()
801 << E.convertToErrorCode().message();
802 },
803 [&](const llvm::vfs::OutputError &E) {
804 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
805 << E.getOutputPath() << E.convertToErrorCode().message();
806 },
807 [&](const llvm::ErrorInfoBase &EIB) { // Handle any remaining error
808 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
809 << O.getPath() << EIB.message();
810 });
811 }
812 OutputFiles.clear();
813 if (DeleteBuiltModules) {
814 for (auto &Module : BuiltModules)
815 llvm::sys::fs::remove(Module.second);
816 BuiltModules.clear();
817 }
818}
819
820std::unique_ptr<raw_pwrite_stream> CompilerInstance::createDefaultOutputFile(
821 bool Binary, StringRef InFile, StringRef Extension, bool RemoveFileOnSignal,
822 bool CreateMissingDirectories, bool ForceUseTemporary,
823 bool SetOnlyIfDifferent) {
824 StringRef OutputPath = getFrontendOpts().OutputFile;
825 std::optional<SmallString<128>> PathStorage;
826 if (OutputPath.empty()) {
827 if (InFile == "-" || Extension.empty()) {
828 OutputPath = "-";
829 } else {
830 PathStorage.emplace(InFile);
831 llvm::sys::path::replace_extension(*PathStorage, Extension);
832 OutputPath = *PathStorage;
833 }
834 }
835
836 return createOutputFile(OutputPath, Binary, RemoveFileOnSignal,
837 getFrontendOpts().UseTemporary || ForceUseTemporary,
838 CreateMissingDirectories, SetOnlyIfDifferent);
839}
840
841std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() {
842 return std::make_unique<llvm::raw_null_ostream>();
843}
844
845// Output Manager
846
849 assert(!OutputMgr && "Already has an output manager");
850 OutputMgr = std::move(NewOutputs);
851}
852
854 assert(!OutputMgr && "Already has an output manager");
855 OutputMgr = llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>();
856}
857
858llvm::vfs::OutputBackend &CompilerInstance::getOutputManager() {
859 assert(OutputMgr);
860 return *OutputMgr;
861}
862
864 if (!hasOutputManager())
866 return getOutputManager();
867}
868
869std::unique_ptr<raw_pwrite_stream> CompilerInstance::createOutputFile(
870 StringRef OutputPath, bool Binary, bool RemoveFileOnSignal,
871 bool UseTemporary, bool CreateMissingDirectories, bool SetOnlyIfDifferent) {
873 createOutputFileImpl(OutputPath, Binary, RemoveFileOnSignal, UseTemporary,
874 CreateMissingDirectories, SetOnlyIfDifferent);
875 if (OS)
876 return std::move(*OS);
877 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
878 << OutputPath << errorToErrorCode(OS.takeError()).message();
879 return nullptr;
880}
881
883CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary,
884 bool RemoveFileOnSignal,
885 bool UseTemporary,
886 bool CreateMissingDirectories,
887 bool SetOnlyIfDifferent) {
888 assert((!CreateMissingDirectories || UseTemporary) &&
889 "CreateMissingDirectories is only allowed when using temporary files");
890
891 // If '-working-directory' was passed, the output filename should be
892 // relative to that.
893 std::optional<SmallString<128>> AbsPath;
894 if (OutputPath != "-" && !llvm::sys::path::is_absolute(OutputPath)) {
895 assert(hasFileManager() &&
896 "File Manager is required to fix up relative path.\n");
897
898 AbsPath.emplace(OutputPath);
900 OutputPath = *AbsPath;
901 }
902
903 using namespace llvm::vfs;
905 OutputPath,
906 OutputConfig()
907 .setTextWithCRLF(!Binary)
908 .setDiscardOnSignal(RemoveFileOnSignal)
909 .setAtomicWrite(UseTemporary)
910 .setImplyCreateDirectories(UseTemporary && CreateMissingDirectories)
911 .setOnlyIfDifferent(SetOnlyIfDifferent));
912 if (!O)
913 return O.takeError();
914
915 O->discardOnDestroy([](llvm::Error E) { consumeError(std::move(E)); });
916 OutputFiles.push_back(std::move(*O));
917 return OutputFiles.back().createProxy();
918}
919
920// Initialization Utilities
921
926
927// static
929 DiagnosticsEngine &Diags,
930 FileManager &FileMgr,
931 SourceManager &SourceMgr) {
937
938 if (Input.isBuffer()) {
939 SourceMgr.setMainFileID(SourceMgr.createFileID(Input.getBuffer(), Kind));
940 assert(SourceMgr.getMainFileID().isValid() &&
941 "Couldn't establish MainFileID!");
942 return true;
943 }
944
945 StringRef InputFile = Input.getFile();
946
947 // Figure out where to get and map in the main file.
948 auto FileOrErr = InputFile == "-"
949 ? FileMgr.getSTDIN()
950 : FileMgr.getFileRef(InputFile, /*OpenFile=*/true);
951 if (!FileOrErr) {
952 auto EC = llvm::errorToErrorCode(FileOrErr.takeError());
953 if (InputFile != "-")
954 Diags.Report(diag::err_fe_error_reading) << InputFile << EC.message();
955 else
956 Diags.Report(diag::err_fe_error_reading_stdin) << EC.message();
957 return false;
958 }
959
960 SourceMgr.setMainFileID(
961 SourceMgr.createFileID(*FileOrErr, SourceLocation(), Kind));
962
963 assert(SourceMgr.getMainFileID().isValid() &&
964 "Couldn't establish MainFileID!");
965 return true;
966}
967
968// High-Level Operations
969
970void CompilerInstance::PrepareForExecution() {
971 // Set up the frontend timer for -ftime-report. BackendConsumer uses
972 // getTimerGroup() and getFrontendTimer() when TimePasses is set. In the
973 // cc1 driver path this was done in cc1_main before calling
974 // ExecuteCompilerInvocation; we consolidate it here so that all tools
975 // (cc1, clang-repl, libclang, etc.) get consistent behavior.
976 if (getCodeGenOpts().TimePasses && !FrontendTimer) {
978 getFrontendTimer().startTimer();
979 }
980
981 // FIXME: Consider consolidating additional per-instance setup here:
982 // - llvm::timeTraceProfilerInitialize) when TimeTracePath is set.
983 // - Plugin loading (LoadRequestedPlugins) and -mllvm argument processing.
984}
985
987 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
988 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
989 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
990
991 llvm::TimeTraceScope TimeScope("ExecuteCompiler");
992
993 PrepareForExecution();
994
995 // Mark this point as the bottom of the stack if we don't have somewhere
996 // better. We generally expect frontend actions to be invoked with (nearly)
997 // DesiredStackSpace available.
999
1000 raw_ostream &OS = getVerboseOutputStream();
1001
1002 if (!Act.PrepareToExecute(*this))
1003 return false;
1004
1005 if (!createTarget())
1006 return false;
1007
1008 // rewriter project will change target built-in bool type from its default.
1009 if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
1011
1012 // Validate/process some options.
1013 if (getHeaderSearchOpts().Verbose)
1014 OS << "clang -cc1 version " CLANG_VERSION_STRING << " based upon LLVM "
1015 << LLVM_VERSION_STRING << " default target "
1016 << llvm::sys::getDefaultTargetTriple() << "\n";
1017
1018 if (getFrontendOpts().ShowStats || !getFrontendOpts().StatsFile.empty())
1019 llvm::EnableStatistics(false);
1020
1021 // Sort vectors containing toc data and no toc data variables to facilitate
1022 // binary search later.
1023 llvm::sort(getCodeGenOpts().TocDataVarsUserSpecified);
1024 llvm::sort(getCodeGenOpts().NoTocDataVars);
1025
1026 for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) {
1027 // Reset the ID tables if we are reusing the SourceManager and parsing
1028 // regular files.
1029 if (hasSourceManager() && !Act.isModelParsingAction())
1031
1032 ModuleImportResults.clear();
1033
1034 if (Act.BeginSourceFile(*this, FIF)) {
1035 if (llvm::Error Err = Act.Execute()) {
1036 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
1037 }
1038 Act.EndSourceFile();
1039 }
1040 }
1041
1043
1044 if (getFrontendOpts().ShowStats) {
1045 if (hasFileManager()) {
1047 OS << '\n';
1048 }
1049 llvm::PrintStatistics(OS);
1050 }
1051 StringRef StatsFile = getFrontendOpts().StatsFile;
1052 if (!StatsFile.empty()) {
1053 llvm::sys::fs::OpenFlags FileFlags = llvm::sys::fs::OF_TextWithCRLF;
1054 if (getFrontendOpts().AppendStats)
1055 FileFlags |= llvm::sys::fs::OF_Append;
1056 std::error_code EC;
1057 auto StatS =
1058 std::make_unique<llvm::raw_fd_ostream>(StatsFile, EC, FileFlags);
1059 if (EC) {
1060 getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file)
1061 << StatsFile << EC.message();
1062 } else {
1063 llvm::PrintStatisticsJSON(*StatS);
1064 }
1065 }
1066
1067 return !getDiagnostics().getClient()->getNumErrors();
1068}
1069
1071 if (!getDiagnosticOpts().ShowCarets)
1072 return;
1073
1074 raw_ostream &OS = getVerboseOutputStream();
1075
1076 // We can have multiple diagnostics sharing one diagnostic client.
1077 // Get the total number of warnings/errors from the client.
1078 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
1079 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
1080
1081 if (NumWarnings)
1082 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
1083 if (NumWarnings && NumErrors)
1084 OS << " and ";
1085 if (NumErrors)
1086 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
1087 if (NumWarnings || NumErrors) {
1088 OS << " generated";
1089 if (getLangOpts().CUDA) {
1090 if (!getLangOpts().CUDAIsDevice) {
1091 OS << " when compiling for host";
1092 } else {
1093 OS << " when compiling for "
1094 << (!getTargetOpts().CPU.empty() ? getTargetOpts().CPU
1095 : getTarget().getTriple().str());
1096 }
1097 }
1098 OS << ".\n";
1099 }
1100}
1101
1103 // Load any requested plugins.
1104 for (const std::string &Path : getFrontendOpts().Plugins) {
1105 std::string Error;
1106 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
1107 getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
1108 << Path << Error;
1109 }
1110
1111 // Load and store pass plugins for the back-end.
1112 for (const std::string &Path : getCodeGenOpts().PassPlugins) {
1113 if (auto PassPlugin = llvm::PassPlugin::Load(Path)) {
1114 PassPlugins.emplace_back(std::make_unique<llvm::PassPlugin>(*PassPlugin));
1115 } else {
1116 getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
1117 << Path << toString(PassPlugin.takeError());
1118 }
1119 }
1120
1121 // Check if any of the loaded plugins replaces the main AST action
1122 for (const FrontendPluginRegistry::entry &Plugin :
1123 FrontendPluginRegistry::entries()) {
1124 std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
1125 if (P->getActionType() == PluginASTAction::ReplaceAction) {
1127 getFrontendOpts().ActionName = Plugin.getName().str();
1128 break;
1129 }
1130 }
1131}
1132
1133/// Determine the appropriate source input kind based on language
1134/// options.
1136 if (LangOpts.OpenCL)
1137 return Language::OpenCL;
1138 if (LangOpts.CUDA)
1139 return Language::CUDA;
1140 if (LangOpts.ObjC)
1141 return LangOpts.CPlusPlus ? Language::ObjCXX : Language::ObjC;
1142 return LangOpts.CPlusPlus ? Language::CXX : Language::C;
1143}
1144
1145std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
1146 SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
1147 StringRef OriginalModuleMapFile, StringRef ModuleFileName,
1148 std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) {
1149 // Construct a compiler invocation for creating this module.
1150 auto Invocation = std::make_shared<CompilerInvocation>(getInvocation());
1151
1152 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1153
1154 // For any options that aren't intended to affect how a module is built,
1155 // reset them to their default values.
1156 Invocation->resetNonModularOptions();
1157
1158 // Remove any macro definitions that are explicitly ignored by the module.
1159 // They aren't supposed to affect how the module is built anyway.
1160 HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
1161 llvm::erase_if(PPOpts.Macros,
1162 [&HSOpts](const std::pair<std::string, bool> &def) {
1163 StringRef MacroDef = def.first;
1164 return HSOpts.ModulesIgnoreMacros.contains(
1165 llvm::CachedHashString(MacroDef.split('=').first));
1166 });
1167
1168 // If the original compiler invocation had -fmodule-name, pass it through.
1169 Invocation->getLangOpts().ModuleName =
1171
1172 // Note the name of the module we're building.
1173 Invocation->getLangOpts().CurrentModule = std::string(ModuleName);
1174
1175 // If there is a module map file, build the module using the module map.
1176 // Set up the inputs/outputs so that we build the module from its umbrella
1177 // header.
1178 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1179 FrontendOpts.OutputFile = ModuleFileName.str();
1180 FrontendOpts.DisableFree = false;
1181 FrontendOpts.GenerateGlobalModuleIndex = false;
1182 FrontendOpts.BuildingImplicitModule = true;
1183 FrontendOpts.OriginalModuleMap = std::string(OriginalModuleMapFile);
1184 // Force implicitly-built modules to hash the content of the module file.
1185 HSOpts.ModulesHashContent = true;
1186 FrontendOpts.Inputs = {std::move(Input)};
1187
1188 // Don't free the remapped file buffers; they are owned by our caller.
1189 PPOpts.RetainRemappedFileBuffers = true;
1190
1191 DiagnosticOptions &DiagOpts = Invocation->getDiagnosticOpts();
1192
1193 DiagOpts.VerifyDiagnostics = 0;
1194 assert(getInvocation().computeContextHash() ==
1195 Invocation->computeContextHash() &&
1196 "Module hash mismatch!");
1197
1198 std::shared_ptr<ModuleCache> ModCache;
1199 if (ThreadSafeConfig) {
1200 ModCache = ThreadSafeConfig->getModuleCache();
1201 } else {
1202 ModCache = this->ModCache;
1203 }
1204
1205 // Construct a compiler instance that will be used to create the module.
1206 auto InstancePtr = std::make_unique<CompilerInstance>(
1207 std::move(Invocation), getPCHContainerOperations(), std::move(ModCache));
1208 auto &Instance = *InstancePtr;
1209
1210 auto &Inv = Instance.getInvocation();
1211
1212 if (ThreadSafeConfig) {
1213 Instance.setVirtualFileSystem(ThreadSafeConfig->getVFS());
1214 Instance.createFileManager();
1215 } else if (FrontendOpts.ModulesShareFileManager) {
1216 Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
1217 Instance.setFileManager(getFileManagerPtr());
1218 } else {
1219 Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
1220 Instance.createFileManager();
1221 }
1222
1223 if (ThreadSafeConfig) {
1224 Instance.createDiagnostics(&ThreadSafeConfig->getDiagConsumer(),
1225 /*ShouldOwnClient=*/false);
1226 } else {
1227 Instance.createDiagnostics(
1228 new ForwardingDiagnosticConsumer(getDiagnosticClient()),
1229 /*ShouldOwnClient=*/true);
1230 }
1231 if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
1232 Instance.getDiagnostics().setSuppressSystemWarnings(false);
1233
1234 Instance.createSourceManager();
1235 SourceManager &SourceMgr = Instance.getSourceManager();
1236
1237 if (ThreadSafeConfig) {
1238 // Detecting cycles in the module graph is responsibility of the client.
1239 } else {
1240 // Note that this module is part of the module build stack, so that we
1241 // can detect cycles in the module graph.
1242 SourceMgr.setModuleBuildStack(getSourceManager().getModuleBuildStack());
1243 SourceMgr.pushModuleBuildStack(
1244 ModuleName, FullSourceLoc(ImportLoc, getSourceManager()));
1245 }
1246
1247 // Make a copy for the new instance.
1248 Instance.FailedModules = FailedModules;
1249
1250 // Pass along the GenModuleActionWrapper callback.
1251 Instance.setGenModuleActionWrapper(getGenModuleActionWrapper());
1252
1253 if (GetDependencyDirectives)
1254 Instance.GetDependencyDirectives =
1255 GetDependencyDirectives->cloneFor(Instance.getFileManager());
1256
1257 if (ThreadSafeConfig) {
1258 Instance.setModuleDepCollector(ThreadSafeConfig->getModuleDepCollector());
1259 } else {
1260 // If we're collecting module dependencies, we need to share a collector
1261 // between all of the module CompilerInstances. Other than that, we don't
1262 // want to produce any dependency output from the module build.
1263 Instance.setModuleDepCollector(getModuleDepCollector());
1264 }
1265 Inv.getDependencyOutputOpts() = DependencyOutputOptions();
1266
1267 return InstancePtr;
1268}
1269
1270namespace {
1271class PrettyStackTraceBuildModule : public llvm::PrettyStackTraceEntry {
1272 StringRef ModuleName;
1273 StringRef ModuleFileName;
1274
1275public:
1276 PrettyStackTraceBuildModule(StringRef ModuleName, StringRef ModuleFileName)
1277 : ModuleName(ModuleName), ModuleFileName(ModuleFileName) {}
1278 void print(raw_ostream &OS) const override {
1279 OS << "Building module '" << ModuleName << "' as '" << ModuleFileName
1280 << "'\n";
1281 }
1282};
1283} // namespace
1284
1285std::unique_ptr<llvm::MemoryBuffer>
1286CompilerInstance::compileModule(SourceLocation ImportLoc, StringRef ModuleName,
1287 StringRef ModuleFileName,
1288 CompilerInstance &Instance) {
1289 PrettyStackTraceBuildModule CrashInfo(ModuleName, ModuleFileName);
1290 llvm::TimeTraceScope TimeScope("Module Compile", ModuleName);
1291
1292 // Never compile a module that's already finalized - this would cause the
1293 // existing module to be freed, causing crashes if it is later referenced
1294 if (getModuleCache().getInMemoryModuleCache().isPCMFinal(ModuleFileName)) {
1295 getDiagnostics().Report(ImportLoc, diag::err_module_rebuild_finalized)
1296 << ModuleName;
1297 return nullptr;
1298 }
1299
1300 getDiagnostics().Report(ImportLoc, diag::remark_module_build)
1301 << ModuleName << ModuleFileName;
1302
1303 SmallString<0> Buffer;
1304
1305 // Execute the action to actually build the module in-place. Use a separate
1306 // thread so that we get a stack large enough.
1307 uint64_t ParentTID = llvm::get_threadid();
1308 bool Crashed = !llvm::CrashRecoveryContext().RunSafelyOnNewStack(
1309 [&]() {
1311 << "module_compile_thread: parent=" << ParentTID
1312 << " pcm_compile: " << ModuleFileName;
1313
1314 auto OS = std::make_unique<llvm::raw_svector_ostream>(Buffer);
1315
1316 std::unique_ptr<FrontendAction> Action =
1317 std::make_unique<GenerateModuleFromModuleMapAction>(std::move(OS));
1318
1319 if (auto WrapGenModuleAction = Instance.getGenModuleActionWrapper())
1320 Action = WrapGenModuleAction(Instance.getFrontendOpts(),
1321 std::move(Action));
1322
1323 Instance.ExecuteAction(*Action);
1324 },
1326
1327 getDiagnostics().Report(ImportLoc, diag::remark_module_build_done)
1328 << ModuleName;
1329
1330 // Propagate the statistics to the parent FileManager.
1331 if (!getFrontendOpts().ModulesShareFileManager)
1332 getFileManager().AddStats(Instance.getFileManager());
1333
1334 // Propagate the failed modules to the parent instance.
1335 FailedModules = std::move(Instance.FailedModules);
1336
1337 if (Crashed) {
1338 // Clear the ASTConsumer if it hasn't been already, in case it owns streams
1339 // that must be closed before clearing output files.
1340 Instance.setSema(nullptr);
1341 Instance.setASTConsumer(nullptr);
1342
1343 // Delete any remaining temporary files related to Instance.
1344 Instance.clearOutputFiles(/*EraseFiles=*/true);
1345 }
1346
1347 // We've rebuilt a module. If we're allowed to generate or update the global
1348 // module index, record that fact in the importing compiler instance.
1349 if (getFrontendOpts().GenerateGlobalModuleIndex) {
1351 }
1352
1353 if (Crashed)
1354 return nullptr;
1355
1356 // Unless \p AllowPCMWithCompilerErrors is set, return 'failure' if errors
1357 // occurred.
1358 if (Instance.getDiagnostics().hasErrorOccurred() &&
1359 !Instance.getFrontendOpts().AllowPCMWithCompilerErrors)
1360 return nullptr;
1361
1362 return std::make_unique<llvm::SmallVectorMemoryBuffer>(
1363 std::move(Buffer), Instance.getFrontendOpts().OutputFile);
1364}
1365
1368 StringRef Filename = llvm::sys::path::filename(File.getName());
1369 SmallString<128> PublicFilename(File.getDir().getName());
1370 if (Filename == "module_private.map")
1371 llvm::sys::path::append(PublicFilename, "module.map");
1372 else if (Filename == "module.private.modulemap")
1373 llvm::sys::path::append(PublicFilename, "module.modulemap");
1374 else
1375 return std::nullopt;
1376 return FileMgr.getOptionalFileRef(PublicFilename);
1377}
1378
1379std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
1380 SourceLocation ImportLoc, const Module *Module, StringRef ModuleFileName,
1381 std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) {
1382 StringRef ModuleName = Module->getTopLevelModuleName();
1383
1385
1386 // Get or create the module map that we'll use to build this module.
1388 SourceManager &SourceMgr = getSourceManager();
1389
1390 if (FileID ModuleMapFID = ModMap.getContainingModuleMapFileID(Module);
1391 ModuleMapFID.isValid()) {
1392 // We want to use the top-level module map. If we don't, the compiling
1393 // instance may think the containing module map is a top-level one, while
1394 // the importing instance knows it's included from a parent module map via
1395 // the extern directive. This mismatch could bite us later.
1396 SourceLocation Loc = SourceMgr.getIncludeLoc(ModuleMapFID);
1397 while (Loc.isValid() && isModuleMap(SourceMgr.getFileCharacteristic(Loc))) {
1398 ModuleMapFID = SourceMgr.getFileID(Loc);
1399 Loc = SourceMgr.getIncludeLoc(ModuleMapFID);
1400 }
1401
1402 OptionalFileEntryRef ModuleMapFile =
1403 SourceMgr.getFileEntryRefForID(ModuleMapFID);
1404 assert(ModuleMapFile && "Top-level module map with no FileID");
1405
1406 // Canonicalize compilation to start with the public module map. This is
1407 // vital for submodules declarations in the private module maps to be
1408 // correctly parsed when depending on a top level module in the public one.
1409 if (OptionalFileEntryRef PublicMMFile =
1410 getPublicModuleMap(*ModuleMapFile, getFileManager()))
1411 ModuleMapFile = PublicMMFile;
1412
1413 StringRef ModuleMapFilePath = ModuleMapFile->getNameAsRequested();
1414
1415 // Use the systemness of the module map as parsed instead of using the
1416 // IsSystem attribute of the module. If the module has [system] but the
1417 // module map is not in a system path, then this would incorrectly parse
1418 // any other modules in that module map as system too.
1419 const SrcMgr::SLocEntry &SLoc = SourceMgr.getSLocEntry(ModuleMapFID);
1420 bool IsSystem = isSystem(SLoc.getFile().getFileCharacteristic());
1421
1422 // Use the module map where this module resides.
1423 return cloneForModuleCompileImpl(
1424 ImportLoc, ModuleName,
1425 FrontendInputFile(ModuleMapFilePath, IK, IsSystem),
1427 std::move(ThreadSafeConfig));
1428 }
1429
1430 // FIXME: We only need to fake up an input file here as a way of
1431 // transporting the module's directory to the module map parser. We should
1432 // be able to do that more directly, and parse from a memory buffer without
1433 // inventing this file.
1434 SmallString<128> FakeModuleMapFile(Module->Directory->getName());
1435 llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map");
1436
1437 std::string InferredModuleMapContent;
1438 llvm::raw_string_ostream OS(InferredModuleMapContent);
1439 Module->print(OS);
1440
1441 auto Instance = cloneForModuleCompileImpl(
1442 ImportLoc, ModuleName,
1443 FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
1445 std::move(ThreadSafeConfig));
1446
1447 std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
1448 llvm::MemoryBuffer::getMemBufferCopy(InferredModuleMapContent);
1449 FileEntryRef ModuleMapFile = Instance->getFileManager().getVirtualFileRef(
1450 FakeModuleMapFile, InferredModuleMapContent.size(), 0);
1451 Instance->getSourceManager().overrideFileContents(ModuleMapFile,
1452 std::move(ModuleMapBuffer));
1453
1454 return Instance;
1455}
1456
1457/// Read the AST right after compiling the module.
1458/// Returns true on success, false on failure.
1459static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance,
1460 SourceLocation ImportLoc,
1462 Module *Module,
1464 bool *OutOfDate, bool *Missing) {
1465 DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1466
1467 unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
1468 if (OutOfDate)
1469 ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate;
1470
1471 // Try to read the module file, now that we've compiled it.
1472 ASTReader::ASTReadResult ReadResult =
1473 ImportingInstance.getASTReader()->ReadAST(
1475 ModuleLoadCapabilities);
1476 if (ReadResult == ASTReader::Success)
1477 return true;
1478
1479 // The caller wants to handle out-of-date failures.
1480 if (OutOfDate && ReadResult == ASTReader::OutOfDate) {
1481 *OutOfDate = true;
1482 return false;
1483 }
1484
1485 // The caller wants to handle missing module files.
1486 if (Missing && ReadResult == ASTReader::Missing) {
1487 *Missing = true;
1488 return false;
1489 }
1490
1491 // The ASTReader didn't diagnose the error, so conservatively report it.
1492 if (ReadResult == ASTReader::Missing || !Diags.hasErrorOccurred())
1493 Diags.Report(ModuleNameLoc, diag::err_module_not_built)
1494 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1495
1496 return false;
1497}
1498
1499/// Compile a module in a separate compiler instance.
1500/// Returns true on success, false on failure.
1501static bool compileModuleImpl(CompilerInstance &ImportingInstance,
1502 SourceLocation ImportLoc,
1505 std::unique_ptr<llvm::MemoryBuffer> Buffer;
1506
1507 {
1508 auto Instance = ImportingInstance.cloneForModuleCompile(
1510
1511 Buffer = ImportingInstance.compileModule(ModuleNameLoc,
1513 ModuleFileName, *Instance);
1514
1515 if (!Buffer) {
1516 ImportingInstance.getDiagnostics().Report(ModuleNameLoc,
1517 diag::err_module_not_built)
1518 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1519 return false;
1520 }
1521 }
1522
1523 off_t Size;
1524 time_t ModTime;
1525 std::error_code EC = ImportingInstance.getModuleCache().write(
1526 ModuleFileName, *Buffer, Size, ModTime);
1527 if (EC) {
1528 ImportingInstance.getDiagnostics().Report(ModuleNameLoc,
1529 diag::err_module_not_written)
1530 << Module->Name << ModuleFileName << EC.message()
1531 << SourceRange(ImportLoc, ModuleNameLoc);
1532 return false;
1533 }
1534
1535 // The module is built successfully, we can update its timestamp now.
1536 if (ImportingInstance.getPreprocessor()
1541 }
1542
1543 // This isn't strictly necessary, but it's more efficient to extract the AST
1544 // file (which may be wrapped in an object file) now rather than doing so
1545 // repeatedly in the readers.
1546 const PCHContainerReader &Rdr = ImportingInstance.getPCHContainerReader();
1547 StringRef ExtractedBuffer = Rdr.ExtractPCH(*Buffer);
1548 // FIXME: Avoid the copy here by having InMemoryModuleCache accept both the
1549 // owning buffer and the StringRef.
1550 Buffer = llvm::MemoryBuffer::getMemBufferCopy(ExtractedBuffer);
1551
1553 ModuleFileName, std::move(Buffer), Size, ModTime);
1554
1555 return true;
1556}
1557
1558/// The result of `compileModuleBehindLockOrRead()`.
1560 /// We failed to compile the module.
1562 /// We successfully compiled the module and we still need to read it.
1564 /// We failed to read the module file compiled by another instance.
1566 /// We read a module file compiled by another instance.
1568};
1569
1570/// Attempt to compile the module in a separate compiler instance behind a lock
1571/// (to avoid building the same module in multiple compiler instances), or read
1572/// the AST produced by another compiler instance.
1575 SourceLocation ImportLoc,
1578 DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1579
1580 Diags.Report(ModuleNameLoc, diag::remark_module_lock)
1581 << ModuleFileName << Module->Name;
1582
1583 auto &ModuleCache = ImportingInstance.getModuleCache();
1584
1585 while (true) {
1586 auto Lock = ModuleCache.getLock(ModuleFileName);
1587 bool Owned;
1588 if (llvm::Error Err = Lock->tryLock().moveInto(Owned)) {
1589 // ModuleCache takes care of correctness and locks are only necessary for
1590 // performance. Fallback to building the module in case of any lock
1591 // related errors.
1592 Diags.Report(ModuleNameLoc, diag::remark_module_lock_failure)
1593 << Module->Name << toString(std::move(Err));
1594 if (!compileModuleImpl(ImportingInstance, ImportLoc, ModuleNameLoc,
1598 }
1599 if (Owned) {
1600 // We're responsible for building the module ourselves.
1601 if (!compileModuleImpl(ImportingInstance, ImportLoc, ModuleNameLoc,
1605 }
1606
1607 // Someone else is responsible for building the module. Wait for them to
1608 // finish.
1609 unsigned Timeout =
1611 switch (Lock->waitForUnlockFor(std::chrono::seconds(Timeout))) {
1612 case llvm::WaitForUnlockResult::Success:
1613 break; // The interesting case.
1614 case llvm::WaitForUnlockResult::OwnerDied:
1615 continue; // try again to get the lock.
1616 case llvm::WaitForUnlockResult::Timeout:
1617 // Since the InMemoryModuleCache takes care of correctness, we try waiting
1618 // for someone else to complete the build so that it does not happen
1619 // twice. In case of timeout, try to build it ourselves again.
1620 Diags.Report(ModuleNameLoc, diag::remark_module_lock_timeout)
1621 << Module->Name;
1622 // Clear the lock file so that future invocations can make progress.
1623 Lock->unsafeUnlock();
1624 continue;
1625 }
1626
1627 // Read the module that was just written by someone else.
1628 bool OutOfDate = false;
1629 bool Missing = false;
1630 if (readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1631 Module, ModuleFileName, &OutOfDate, &Missing))
1633 if (!OutOfDate && !Missing)
1635
1636 // The module may be missing or out of date in the presence of file system
1637 // races. It may also be out of date if one of its imports depends on header
1638 // search paths that are not consistent with this ImportingInstance.
1639 // Try again...
1640 }
1641}
1642
1643/// Compile a module in a separate compiler instance and read the AST,
1644/// returning true if the module compiles without errors, potentially using a
1645/// lock manager to avoid building the same module in multiple compiler
1646/// instances.
1647static bool compileModuleAndReadAST(CompilerInstance &ImportingInstance,
1648 SourceLocation ImportLoc,
1650 Module *Module,
1652 if (ImportingInstance.getInvocation()
1656 ImportingInstance, ImportLoc, ModuleNameLoc, Module, ModuleFileName)) {
1659 return false;
1661 return true;
1663 // We successfully compiled the module under a lock. Let's read it from
1664 // the in-memory module cache now.
1665 break;
1666 }
1667 } else {
1668 if (!compileModuleImpl(ImportingInstance, ImportLoc, ModuleNameLoc, Module,
1670 return false;
1671 }
1672
1673 return readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1675 /*OutOfDate=*/nullptr, /*Missing=*/nullptr);
1676}
1677
1678/// Diagnose differences between the current definition of the given
1679/// configuration macro and the definition provided on the command line.
1680static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
1681 Module *Mod, SourceLocation ImportLoc) {
1682 IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
1683 SourceManager &SourceMgr = PP.getSourceManager();
1684
1685 // If this identifier has never had a macro definition, then it could
1686 // not have changed.
1687 if (!Id->hadMacroDefinition())
1688 return;
1689 auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id);
1690
1691 // Find the macro definition from the command line.
1692 MacroInfo *CmdLineDefinition = nullptr;
1693 for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
1694 SourceLocation MDLoc = MD->getLocation();
1695 FileID FID = SourceMgr.getFileID(MDLoc);
1696 if (FID.isInvalid())
1697 continue;
1698 // We only care about the predefines buffer, or if the macro is defined
1699 // over the command line transitively through a PCH.
1700 if (FID != PP.getPredefinesFileID() &&
1701 !SourceMgr.isWrittenInCommandLineFile(MDLoc))
1702 continue;
1703 if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
1704 CmdLineDefinition = DMD->getMacroInfo();
1705 break;
1706 }
1707
1708 auto *CurrentDefinition = PP.getMacroInfo(Id);
1709 if (CurrentDefinition == CmdLineDefinition) {
1710 // Macro matches. Nothing to do.
1711 } else if (!CurrentDefinition) {
1712 // This macro was defined on the command line, then #undef'd later.
1713 // Complain.
1714 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1715 << true << ConfigMacro << Mod->getFullModuleName();
1716 auto LatestDef = LatestLocalMD->getDefinition();
1717 assert(LatestDef.isUndefined() &&
1718 "predefined macro went away with no #undef?");
1719 PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
1720 << true;
1721 return;
1722 } else if (!CmdLineDefinition) {
1723 // There was no definition for this macro in the command line,
1724 // but there was a local definition. Complain.
1725 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1726 << false << ConfigMacro << Mod->getFullModuleName();
1727 PP.Diag(CurrentDefinition->getDefinitionLoc(),
1728 diag::note_module_def_undef_here)
1729 << false;
1730 } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
1731 /*Syntactically=*/true)) {
1732 // The macro definitions differ.
1733 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1734 << false << ConfigMacro << Mod->getFullModuleName();
1735 PP.Diag(CurrentDefinition->getDefinitionLoc(),
1736 diag::note_module_def_undef_here)
1737 << false;
1738 }
1739}
1740
1742 SourceLocation ImportLoc) {
1743 clang::Module *TopModule = M->getTopLevelModule();
1744 for (const StringRef ConMacro : TopModule->ConfigMacros) {
1745 checkConfigMacro(PP, ConMacro, M, ImportLoc);
1746 }
1747}
1748
1750 if (TheASTReader)
1751 return;
1752
1753 if (!hasASTContext())
1755
1756 // If we're implicitly building modules but not currently recursively
1757 // building a module, check whether we need to prune the module cache.
1758 if (getSourceManager().getModuleBuildStack().empty() &&
1760 .getHeaderSearchInfo()
1761 .getSpecificModuleCachePath()
1762 .empty())
1763 ModCache->maybePrune(getHeaderSearchOpts().ModuleCachePath,
1764 getHeaderSearchOpts().ModuleCachePruneInterval,
1765 getHeaderSearchOpts().ModuleCachePruneAfter);
1766
1768 std::string Sysroot = HSOpts.Sysroot;
1769 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1770 const FrontendOptions &FEOpts = getFrontendOpts();
1771 std::unique_ptr<llvm::Timer> ReadTimer;
1772
1773 if (timerGroup)
1774 ReadTimer = std::make_unique<llvm::Timer>("reading_modules",
1775 "Reading modules", *timerGroup);
1776 TheASTReader = llvm::makeIntrusiveRefCnt<ASTReader>(
1779 getFrontendOpts().ModuleFileExtensions,
1780 Sysroot.empty() ? "" : Sysroot.c_str(),
1782 /*AllowASTWithCompilerErrors=*/FEOpts.AllowPCMWithCompilerErrors,
1783 /*AllowConfigurationMismatch=*/false,
1787 +getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer));
1788 if (hasASTConsumer()) {
1789 TheASTReader->setDeserializationListener(
1790 getASTConsumer().GetASTDeserializationListener());
1792 getASTConsumer().GetASTMutationListener());
1793 }
1794 getASTContext().setExternalSource(TheASTReader);
1795 if (hasSema())
1796 TheASTReader->InitializeSema(getSema());
1797 if (hasASTConsumer())
1798 TheASTReader->StartTranslationUnit(&getASTConsumer());
1799
1800 for (auto &Listener : DependencyCollectors)
1801 Listener->attachToASTReader(*TheASTReader);
1802}
1803
1806 llvm::Timer Timer;
1807 if (timerGroup)
1808 Timer.init("preloading." + std::string(FileName.str()),
1809 "Preloading " + std::string(FileName.str()), *timerGroup);
1810 llvm::TimeRegion TimeLoading(timerGroup ? &Timer : nullptr);
1811
1812 // If we don't already have an ASTReader, create one now.
1813 if (!TheASTReader)
1815
1816 // If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the
1817 // ASTReader to diagnose it, since it can produce better errors that we can.
1818 bool ConfigMismatchIsRecoverable =
1819 getDiagnostics().getDiagnosticLevel(diag::warn_ast_file_config_mismatch,
1820 SourceLocation()) <=
1822
1823 auto Listener = std::make_unique<ReadModuleNames>(*PP);
1824 auto &ListenerRef = *Listener;
1825 ASTReader::ListenerScope ReadModuleNamesListener(*TheASTReader,
1826 std::move(Listener));
1827
1828 // Try to load the module file.
1829 switch (TheASTReader->ReadAST(
1831 ConfigMismatchIsRecoverable ? ASTReader::ARR_ConfigurationMismatch : 0,
1832 &LoadedModuleFile)) {
1833 case ASTReader::Success:
1834 // We successfully loaded the module file; remember the set of provided
1835 // modules so that we don't try to load implicit modules for them.
1836 ListenerRef.registerAll();
1837 return true;
1838
1840 // Ignore unusable module files.
1842 diag::warn_ast_file_config_mismatch)
1843 << FileName;
1844 // All modules provided by any files we tried and failed to load are now
1845 // unavailable; includes of those modules should now be handled textually.
1846 ListenerRef.markAllUnavailable();
1847 return true;
1848
1849 default:
1850 return false;
1851 }
1852}
1853
1854namespace {
1855enum ModuleSource {
1856 MS_ModuleNotFound,
1857 MS_ModuleCache,
1858 MS_PrebuiltModulePath,
1859 MS_ModuleBuildPragma
1860};
1861} // end namespace
1862
1863/// Select a source for loading the named module and compute the filename to
1864/// load it from.
1865static ModuleSource selectModuleSource(
1866 Module *M, StringRef ModuleName, ModuleFileName &ModuleFilename,
1867 const std::map<std::string, std::string, std::less<>> &BuiltModules,
1868 HeaderSearch &HS) {
1869 assert(ModuleFilename.empty() && "Already has a module source?");
1870
1871 // Check to see if the module has been built as part of this compilation
1872 // via a module build pragma.
1873 auto BuiltModuleIt = BuiltModules.find(ModuleName);
1874 if (BuiltModuleIt != BuiltModules.end()) {
1875 ModuleFilename = ModuleFileName::makeExplicit(BuiltModuleIt->second);
1876 return MS_ModuleBuildPragma;
1877 }
1878
1879 // Try to load the module from the prebuilt module path.
1880 const HeaderSearchOptions &HSOpts = HS.getHeaderSearchOpts();
1881 if (!HSOpts.PrebuiltModuleFiles.empty() ||
1882 !HSOpts.PrebuiltModulePaths.empty()) {
1883 ModuleFilename = HS.getPrebuiltModuleFileName(ModuleName);
1884 if (HSOpts.EnablePrebuiltImplicitModules && ModuleFilename.empty())
1885 ModuleFilename = HS.getPrebuiltImplicitModuleFileName(M);
1886 if (!ModuleFilename.empty())
1887 return MS_PrebuiltModulePath;
1888 }
1889
1890 // Try to load the module from the module cache.
1891 if (M) {
1892 ModuleFilename = HS.getCachedModuleFileName(M);
1893 return MS_ModuleCache;
1894 }
1895
1896 return MS_ModuleNotFound;
1897}
1898
1899ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
1900 StringRef ModuleName, SourceLocation ImportLoc, SourceRange ModuleNameRange,
1901 bool IsInclusionDirective) {
1902 // Search for a module with the given name.
1903 HeaderSearch &HS = PP->getHeaderSearchInfo();
1904 Module *M =
1905 HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
1906
1907 // Check for any configuration macros that have changed. This is done
1908 // immediately before potentially building a module in case this module
1909 // depends on having one of its configuration macros defined to successfully
1910 // build. If this is not done the user will never see the warning.
1911 if (M)
1912 checkConfigMacros(getPreprocessor(), M, ImportLoc);
1913
1914 // Select the source and filename for loading the named module.
1915 ModuleFileName ModuleFilename;
1916 ModuleSource Source =
1917 selectModuleSource(M, ModuleName, ModuleFilename, BuiltModules, HS);
1918 SourceLocation ModuleNameLoc = ModuleNameRange.getBegin();
1919 if (Source == MS_ModuleNotFound) {
1920 // We can't find a module, error out here.
1921 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1922 << ModuleName << ModuleNameRange;
1923 return nullptr;
1924 }
1925 if (ModuleFilename.empty()) {
1926 if (M && M->HasIncompatibleModuleFile) {
1927 // We tried and failed to load a module file for this module. Fall
1928 // back to textual inclusion for its headers.
1930 }
1931
1932 getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled)
1933 << ModuleName;
1934 return nullptr;
1935 }
1936
1937 // Create an ASTReader on demand.
1938 if (!getASTReader())
1940
1941 // Time how long it takes to load the module.
1942 llvm::Timer Timer;
1943 if (timerGroup)
1944 Timer.init("loading." + std::string(ModuleFilename.str()),
1945 "Loading " + std::string(ModuleFilename.str()), *timerGroup);
1946 llvm::TimeRegion TimeLoading(timerGroup ? &Timer : nullptr);
1947 llvm::TimeTraceScope TimeScope("Module Load", ModuleName);
1948
1949 // Try to load the module file. If we are not trying to load from the
1950 // module cache, we don't know how to rebuild modules.
1951 unsigned ARRFlags = Source == MS_ModuleCache
1954 : Source == MS_PrebuiltModulePath
1955 ? 0
1957 switch (getASTReader()->ReadAST(ModuleFilename,
1958 Source == MS_PrebuiltModulePath
1960 : Source == MS_ModuleBuildPragma
1963 ImportLoc, ARRFlags)) {
1964 case ASTReader::Success: {
1965 if (M)
1966 return M;
1967 assert(Source != MS_ModuleCache &&
1968 "missing module, but file loaded from cache");
1969
1970 // A prebuilt module is indexed as a ModuleFile; the Module does not exist
1971 // until the first call to ReadAST. Look it up now.
1972 M = HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
1973
1974 // Check whether M refers to the file in the prebuilt module path.
1975 if (M && M->getASTFileKey() &&
1976 *M->getASTFileKey() ==
1977 getASTReader()->getModuleManager().makeKey(ModuleFilename))
1978 return M;
1979
1980 getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt)
1981 << ModuleName;
1982 return ModuleLoadResult();
1983 }
1984
1986 case ASTReader::Missing:
1987 // The most interesting case.
1988 break;
1989
1991 if (Source == MS_PrebuiltModulePath)
1992 // FIXME: We shouldn't be setting HadFatalFailure below if we only
1993 // produce a warning here!
1994 getDiagnostics().Report(SourceLocation(),
1995 diag::warn_ast_file_config_mismatch)
1996 << ModuleFilename;
1997 // Fall through to error out.
1998 [[fallthrough]];
2002 // FIXME: The ASTReader will already have complained, but can we shoehorn
2003 // that diagnostic information into a more useful form?
2004 return ModuleLoadResult();
2005
2006 case ASTReader::Failure:
2008 return ModuleLoadResult();
2009 }
2010
2011 // ReadAST returned Missing or OutOfDate.
2012 if (Source != MS_ModuleCache) {
2013 // We don't know the desired configuration for this module and don't
2014 // necessarily even have a module map. Since ReadAST already produces
2015 // diagnostics for these two cases, we simply error out here.
2016 return ModuleLoadResult();
2017 }
2018
2019 // The module file is missing or out-of-date. Build it.
2020 assert(M && "missing module, but trying to compile for cache");
2021
2022 // Check whether there is a cycle in the module graph.
2024 ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
2025 for (; Pos != PosEnd; ++Pos) {
2026 if (Pos->first == ModuleName)
2027 break;
2028 }
2029
2030 if (Pos != PosEnd) {
2031 SmallString<256> CyclePath;
2032 for (; Pos != PosEnd; ++Pos) {
2033 CyclePath += Pos->first;
2034 CyclePath += " -> ";
2035 }
2036 CyclePath += ModuleName;
2037
2038 getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
2039 << ModuleName << CyclePath;
2040 return nullptr;
2041 }
2042
2043 // Check whether we have already attempted to build this module (but failed).
2044 if (FailedModules.contains(ModuleName)) {
2045 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
2046 << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
2047 return nullptr;
2048 }
2049
2050 // Try to compile and then read the AST.
2051 if (!compileModuleAndReadAST(*this, ImportLoc, ModuleNameLoc, M,
2052 ModuleFilename)) {
2053 assert(getDiagnostics().hasErrorOccurred() &&
2054 "undiagnosed error in compileModuleAndReadAST");
2055 FailedModules.insert(ModuleName);
2056 return nullptr;
2057 }
2058
2059 // Okay, we've rebuilt and now loaded the module.
2060 return M;
2061}
2062
2065 ModuleIdPath Path,
2067 bool IsInclusionDirective) {
2068 // Determine what file we're searching from.
2069 StringRef ModuleName = Path[0].getIdentifierInfo()->getName();
2070 SourceLocation ModuleNameLoc = Path[0].getLoc();
2071
2072 // If we've already handled this import, just return the cached result.
2073 // This cache eliminates redundant diagnostics when both the preprocessor
2074 // and parser see the same import declaration.
2075 if (ImportLoc.isValid()) {
2076 auto CacheIt = ModuleImportResults.find(ImportLoc);
2077 if (CacheIt != ModuleImportResults.end()) {
2078 if (CacheIt->second && ModuleName != getLangOpts().CurrentModule)
2079 TheASTReader->makeModuleVisible(CacheIt->second, Visibility, ImportLoc);
2080 return CacheIt->second;
2081 }
2082 }
2083
2084 // If we don't already have information on this module, load the module now.
2085 Module *Module = nullptr;
2087 if (auto MaybeModule = MM.getCachedModuleLoad(*Path[0].getIdentifierInfo())) {
2088 // Use the cached result, which may be nullptr.
2089 Module = *MaybeModule;
2090 // Config macros are already checked before building a module, but they need
2091 // to be checked at each import location in case any of the config macros
2092 // have a new value at the current `ImportLoc`.
2093 if (Module)
2095 } else if (ModuleName == getLangOpts().CurrentModule) {
2096 // This is the module we're building.
2097 Module = PP->getHeaderSearchInfo().lookupModule(
2098 ModuleName, ImportLoc, /*AllowSearch*/ true,
2099 /*AllowExtraModuleMapSearch*/ !IsInclusionDirective);
2100
2101 // Config macros do not need to be checked here for two reasons.
2102 // * This will always be textual inclusion, and thus the config macros
2103 // actually do impact the content of the header.
2104 // * `Preprocessor::HandleHeaderIncludeOrImport` will never call this
2105 // function as the `#include` or `#import` is textual.
2106
2107 MM.cacheModuleLoad(*Path[0].getIdentifierInfo(), Module);
2108 } else if (getPreprocessorOpts().SingleModuleParseMode) {
2109 // This mimics how findOrCompileModuleAndReadAST() finds the module.
2111 ModuleName, ImportLoc, true, !IsInclusionDirective);
2112 if (Module) {
2113 if (PPCallbacks *PPCb = getPreprocessor().getPPCallbacks())
2114 PPCb->moduleLoadSkipped(Module);
2115 // Mark the module and its submodules as if they were loaded from a PCM.
2116 // This prevents emission of the "missing submodule" diagnostic below.
2117 std::vector<clang::Module *> Worklist{Module};
2118 while (!Worklist.empty()) {
2119 clang::Module *M = Worklist.back();
2120 Worklist.pop_back();
2121 M->IsFromModuleFile = true;
2122 for (clang::Module *SubM : M->submodules())
2123 Worklist.push_back(SubM);
2124 }
2125 }
2126 MM.cacheModuleLoad(*Path[0].getIdentifierInfo(), Module);
2127 } else {
2128 SourceLocation ModuleNameEndLoc = Path.back().getLoc().getLocWithOffset(
2129 Path.back().getIdentifierInfo()->getLength());
2130 ModuleLoadResult Result = findOrCompileModuleAndReadAST(
2131 ModuleName, ImportLoc, SourceRange{ModuleNameLoc, ModuleNameEndLoc},
2132 IsInclusionDirective);
2133 if (!Result.isNormal())
2134 return Result;
2135 if (!Result)
2136 DisableGeneratingGlobalModuleIndex = true;
2137 Module = Result;
2138 MM.cacheModuleLoad(*Path[0].getIdentifierInfo(), Module);
2139 }
2140
2141 // If we never found the module, fail. Otherwise, verify the module and link
2142 // it up.
2143 if (!Module)
2144 return ModuleLoadResult();
2145
2146 // Verify that the rest of the module path actually corresponds to
2147 // a submodule.
2148 bool MapPrivateSubModToTopLevel = false;
2149 for (unsigned I = 1, N = Path.size(); I != N; ++I) {
2150 StringRef Name = Path[I].getIdentifierInfo()->getName();
2151 clang::Module *Sub = Module->findSubmodule(Name);
2152
2153 // If the user is requesting Foo.Private and it doesn't exist, try to
2154 // match Foo_Private and emit a warning asking for the user to write
2155 // @import Foo_Private instead. FIXME: remove this when existing clients
2156 // migrate off of Foo.Private syntax.
2157 if (!Sub && Name == "Private" && Module == Module->getTopLevelModule()) {
2158 SmallString<128> PrivateModule(Module->Name);
2159 PrivateModule.append("_Private");
2160
2162 auto &II = PP->getIdentifierTable().get(
2163 PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
2164 PrivPath.emplace_back(Path[0].getLoc(), &II);
2165
2167 // If there is a modulemap module or prebuilt module, load it.
2168 if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc, true,
2169 !IsInclusionDirective) ||
2170 selectModuleSource(nullptr, PrivateModule, FileName, BuiltModules,
2171 PP->getHeaderSearchInfo()) != MS_ModuleNotFound)
2172 Sub = loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
2173 if (Sub) {
2174 MapPrivateSubModToTopLevel = true;
2175 PP->markClangModuleAsAffecting(Module);
2176 if (!getDiagnostics().isIgnored(
2177 diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) {
2178 getDiagnostics().Report(Path[I].getLoc(),
2179 diag::warn_no_priv_submodule_use_toplevel)
2180 << Path[I].getIdentifierInfo() << Module->getFullModuleName()
2181 << PrivateModule
2182 << SourceRange(Path[0].getLoc(), Path[I].getLoc())
2183 << FixItHint::CreateReplacement(SourceRange(Path[0].getLoc()),
2184 PrivateModule);
2185 getDiagnostics().Report(Sub->DefinitionLoc,
2186 diag::note_private_top_level_defined);
2187 }
2188 }
2189 }
2190
2191 if (!Sub) {
2192 // Attempt to perform typo correction to find a module name that works.
2194 unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
2195
2196 for (class Module *SubModule : Module->submodules()) {
2197 unsigned ED =
2198 Name.edit_distance(SubModule->Name,
2199 /*AllowReplacements=*/true, BestEditDistance);
2200 if (ED <= BestEditDistance) {
2201 if (ED < BestEditDistance) {
2202 Best.clear();
2203 BestEditDistance = ED;
2204 }
2205
2206 Best.push_back(SubModule->Name);
2207 }
2208 }
2209
2210 // If there was a clear winner, user it.
2211 if (Best.size() == 1) {
2212 getDiagnostics().Report(Path[I].getLoc(),
2213 diag::err_no_submodule_suggest)
2214 << Path[I].getIdentifierInfo() << Module->getFullModuleName()
2215 << Best[0] << SourceRange(Path[0].getLoc(), Path[I - 1].getLoc())
2216 << FixItHint::CreateReplacement(SourceRange(Path[I].getLoc()),
2217 Best[0]);
2218
2219 Sub = Module->findSubmodule(Best[0]);
2220 }
2221 }
2222
2223 if (!Sub) {
2224 // No submodule by this name. Complain, and don't look for further
2225 // submodules.
2226 getDiagnostics().Report(Path[I].getLoc(), diag::err_no_submodule)
2227 << Path[I].getIdentifierInfo() << Module->getFullModuleName()
2228 << SourceRange(Path[0].getLoc(), Path[I - 1].getLoc());
2229 break;
2230 }
2231
2232 Module = Sub;
2233 }
2234
2235 // Make the named module visible, if it's not already part of the module
2236 // we are parsing.
2237 if (ModuleName != getLangOpts().CurrentModule) {
2238 if (!Module->IsFromModuleFile && !MapPrivateSubModToTopLevel) {
2239 // We have an umbrella header or directory that doesn't actually include
2240 // all of the headers within the directory it covers. Complain about
2241 // this missing submodule and recover by forgetting that we ever saw
2242 // this submodule.
2243 // FIXME: Should we detect this at module load time? It seems fairly
2244 // expensive (and rare).
2245 getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
2247 << SourceRange(Path.front().getLoc(), Path.back().getLoc());
2248
2250 }
2251
2252 // Check whether this module is available.
2254 *Module, getDiagnostics())) {
2255 getDiagnostics().Report(ImportLoc, diag::note_module_import_here)
2256 << SourceRange(Path.front().getLoc(), Path.back().getLoc());
2257 ModuleImportResults[ImportLoc] = ModuleLoadResult();
2258 return ModuleLoadResult();
2259 }
2260
2261 TheASTReader->makeModuleVisible(Module, Visibility, ImportLoc);
2262 }
2263
2264 // Resolve any remaining module using export_as for this one.
2267 .getModuleMap()
2269
2270 ModuleImportResults[ImportLoc] = ModuleLoadResult(Module);
2271 return ModuleLoadResult(Module);
2272}
2273
2275 StringRef ModuleName,
2276 StringRef Source) {
2277 // Avoid creating filenames with special characters.
2278 SmallString<128> CleanModuleName(ModuleName);
2279 for (auto &C : CleanModuleName)
2280 if (!isAlphanumeric(C))
2281 C = '_';
2282
2283 // FIXME: Using a randomized filename here means that our intermediate .pcm
2284 // output is nondeterministic (as .pcm files refer to each other by name).
2285 // Can this affect the output in any way?
2287 int FD;
2288 if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
2289 CleanModuleName, "pcm", FD, ModuleFileName)) {
2290 getDiagnostics().Report(ImportLoc, diag::err_fe_unable_to_open_output)
2291 << ModuleFileName << EC.message();
2292 return;
2293 }
2294 std::string ModuleMapFileName = (CleanModuleName + ".map").str();
2295
2296 FrontendInputFile Input(
2297 ModuleMapFileName,
2298 InputKind(getLanguageFromOptions(Invocation->getLangOpts()),
2299 InputKind::ModuleMap, /*Preprocessed*/true));
2300
2301 std::string NullTerminatedSource(Source.str());
2302
2303 auto Other = cloneForModuleCompileImpl(ImportLoc, ModuleName, Input,
2304 StringRef(), ModuleFileName);
2305
2306 // Create a virtual file containing our desired source.
2307 // FIXME: We shouldn't need to do this.
2308 FileEntryRef ModuleMapFile = Other->getFileManager().getVirtualFileRef(
2309 ModuleMapFileName, NullTerminatedSource.size(), 0);
2310 Other->getSourceManager().overrideFileContents(
2311 ModuleMapFile, llvm::MemoryBuffer::getMemBuffer(NullTerminatedSource));
2312
2313 Other->BuiltModules = std::move(BuiltModules);
2314 Other->DeleteBuiltModules = false;
2315
2316 // Build the module, inheriting any modules that we've built locally.
2317 std::unique_ptr<llvm::MemoryBuffer> Buffer =
2318 compileModule(ImportLoc, ModuleName, ModuleFileName, *Other);
2319 BuiltModules = std::move(Other->BuiltModules);
2320
2321 if (Buffer) {
2322 llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true);
2323 BuiltModules[std::string(ModuleName)] = std::string(ModuleFileName);
2324 OS << Buffer->getBuffer();
2325 llvm::sys::RemoveFileOnSignal(ModuleFileName);
2326 }
2327}
2328
2331 SourceLocation ImportLoc) {
2332 if (!TheASTReader)
2334 if (!TheASTReader)
2335 return;
2336
2337 TheASTReader->makeModuleVisible(Mod, Visibility, ImportLoc);
2338}
2339
2341 SourceLocation TriggerLoc) {
2342 if (getPreprocessor()
2343 .getHeaderSearchInfo()
2344 .getSpecificModuleCachePath()
2345 .empty())
2346 return nullptr;
2347 if (!TheASTReader)
2349 // Can't do anything if we don't have the module manager.
2350 if (!TheASTReader)
2351 return nullptr;
2352 // Get an existing global index. This loads it if not already
2353 // loaded.
2354 TheASTReader->loadGlobalIndex();
2355 GlobalModuleIndex *GlobalIndex = TheASTReader->getGlobalIndex();
2356 // If the global index doesn't exist, create it.
2357 if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
2358 hasPreprocessor()) {
2359 llvm::sys::fs::create_directories(
2360 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath());
2361 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
2364 .getHeaderSearchInfo()
2365 .getSpecificModuleCachePath())) {
2366 // FIXME this drops the error on the floor. This code is only used for
2367 // typo correction and drops more than just this one source of errors
2368 // (such as the directory creation failure above). It should handle the
2369 // error.
2370 consumeError(std::move(Err));
2371 return nullptr;
2372 }
2373 TheASTReader->resetForReload();
2374 TheASTReader->loadGlobalIndex();
2375 GlobalIndex = TheASTReader->getGlobalIndex();
2376 }
2377 // For finding modules needing to be imported for fixit messages,
2378 // we need to make the global index cover all modules, so we do that here.
2379 if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) {
2381
2382 // Load modules that were parsed from module maps but not loaded yet.
2383 MMap.loadAllParsedModules();
2384
2385 bool RecreateIndex = false;
2387 E = MMap.module_end(); I != E; ++I) {
2388 Module *TheModule = I->second;
2389 if (!TheModule->getASTFileKey()) {
2391 Path.emplace_back(TriggerLoc,
2392 getPreprocessor().getIdentifierInfo(TheModule->Name));
2393 std::reverse(Path.begin(), Path.end());
2394 // Load a module as hidden. This also adds it to the global index.
2395 loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
2396 RecreateIndex = true;
2397 }
2398 }
2399 if (RecreateIndex) {
2400 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
2403 .getHeaderSearchInfo()
2404 .getSpecificModuleCachePath())) {
2405 // FIXME As above, this drops the error on the floor.
2406 consumeError(std::move(Err));
2407 return nullptr;
2408 }
2409 TheASTReader->resetForReload();
2410 TheASTReader->loadGlobalIndex();
2411 GlobalIndex = TheASTReader->getGlobalIndex();
2412 }
2413 HaveFullGlobalModuleIndex = true;
2414 }
2415 return GlobalIndex;
2416}
2417
2418// Check global module index for missing imports.
2419bool
2421 SourceLocation TriggerLoc) {
2422 // Look for the symbol in non-imported modules, but only if an error
2423 // actually occurred.
2424 if (!buildingModule()) {
2425 // Load global module index, or retrieve a previously loaded one.
2427 TriggerLoc);
2428
2429 // Only if we have a global index.
2430 if (GlobalIndex) {
2431 GlobalModuleIndex::HitSet FoundModules;
2432
2433 // Find the modules that reference the identifier.
2434 // Note that this only finds top-level modules.
2435 // We'll let diagnoseTypo find the actual declaration module.
2436 if (GlobalIndex->lookupIdentifier(Name, FoundModules))
2437 return true;
2438 }
2439 }
2440
2441 return false;
2442}
2443void CompilerInstance::resetAndLeakSema() { llvm::BuryPointer(takeSema()); }
2444
2447 ExternalSemaSrc = std::move(ESS);
2448}
Defines the clang::ASTContext interface.
Defines a logger where each line is written atomically to the file.
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 compileModuleAndReadAST(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, ModuleFileName 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 readASTAfterCompileModule(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, ModuleFileName ModuleFileName, bool *OutOfDate, bool *Missing)
Read the AST right after compiling the module.
static CompileOrReadResult compileModuleBehindLockOrRead(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, ModuleFileName ModuleFileName)
Attempt to compile the module in a separate compiler instance behind a lock (to avoid building the sa...
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)
CompileOrReadResult
The result of compileModuleBehindLockOrRead().
@ Compiled
We successfully compiled the module and we still need to read it.
@ Read
We read a module file compiled by another instance.
@ FailedToRead
We failed to read the module file compiled by another instance.
@ FailedToCompile
We failed to compile 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 ModuleSource selectModuleSource(Module *M, StringRef ModuleName, ModuleFileName &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 compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, SourceLocation ModuleNameLoc, Module *Module, ModuleFileName ModuleFileName)
Compile a module in a separate compiler instance.
static void checkConfigMacros(Preprocessor &PP, Module *M, SourceLocation ImportLoc)
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...
static void print(llvm::raw_ostream &OS, const T &V, const Context &Ctx, QualType Ty)
static StringRef getTriple(const Command &Job)
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:49
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
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.
Abstract interface for callback invocations by the ASTReader.
Definition ASTReader.h:117
RAII object to temporarily add an AST callback listener.
Definition ASTReader.h:1912
@ ARR_Missing
The client can handle an AST file that cannot load because it is missing.
Definition ASTReader.h:1825
@ ARR_None
The client can't handle any AST loading failures.
Definition ASTReader.h:1821
@ ARR_ConfigurationMismatch
The client can handle an AST file that cannot load because it's compiled configuration doesn't match ...
Definition ASTReader.h:1838
@ 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:1829
@ 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:1842
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:447
@ Success
The control block was read successfully.
Definition ASTReader.h:450
@ ConfigurationMismatch
The AST file was written with a different language/target configuration.
Definition ASTReader.h:467
@ OutOfDate
The AST file is out-of-date relative to its input files, and needs to be regenerated.
Definition ASTReader.h:460
@ Failure
The AST file itself appears corrupted.
Definition ASTReader.h:453
@ VersionMismatch
The AST file was written by a different version of Clang.
Definition ASTReader.h:463
@ HadErrors
The AST file has errors.
Definition ASTReader.h:470
@ Missing
The AST file was missing.
Definition ASTReader.h:456
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.
bool loadModuleFile(ModuleFileName FileName, serialization::ModuleFile *&LoadedModuleFile)
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 ...
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="", bool RemoveFileOnSignal=true, bool CreateMissingDirectories=false, bool ForceUseTemporary=false, bool SetOnlyIfDifferent=false)
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
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.
void setExternalSemaSource(IntrusiveRefCntPtr< ExternalSemaSource > ESS)
GenModuleActionWrapperFunc getGenModuleActionWrapper() const
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective) override
Attempt to load the given module.
FileSystemOptions & getFileSystemOpts()
llvm::Timer & getFrontendTimer() const
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.
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()
std::unique_ptr< llvm::MemoryBuffer > 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...
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.
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.
std::unique_ptr< CompilerInstance > cloneForModuleCompile(SourceLocation ImportLoc, const Module *Module, StringRef ModuleFileName, std::optional< ThreadSafeCloneConfig > ThreadSafeConfig=std::nullopt)
Creates a new CompilerInstance for compiling a module.
std::unique_ptr< raw_pwrite_stream > createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, bool UseTemporary, bool CreateMissingDirectories=false, bool SetOnlyIfDifferent=false)
Create a new output file, optionally deriving the output path name, and add it to the list of tracked...
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.
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.
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 computeContextHash() const
Compute the context hash - a string that uniquely identifies compiler settings.
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
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:234
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
bool hasErrorOccurred() const
Definition Diagnostic.h:882
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:622
DiagnosticConsumer * getClient()
Definition Diagnostic.h:614
Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc) const
Based on the way the client configured the DiagnosticsEngine object, classify the specified diagnosti...
Definition Diagnostic.h:976
bool ownsClient() const
Determine whether this DiagnosticsEngine object own its client.
Definition Diagnostic.h:618
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:317
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:52
void AddStats(const FileManager &Other)
Import statistics from a child FileManager and add them to this current FileManager.
void PrintStats() const
static bool fixupRelativePath(const FileSystemOptions &FileSystemOpts, SmallVectorImpl< char > &Path)
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true, bool IsText=true)
Get a FileEntryRef if it exists, without doing anything on error.
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:142
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.
unsigned ImplicitModulesLockTimeoutSeconds
The time in seconds to wait on an implicit module lock before timing out.
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.
ModuleFileName 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...
ModuleFileName getPrebuiltImplicitModuleFileName(Module *Module)
Retrieve the name of the prebuilt module file that should be used to load the given module.
ModuleFileName getCachedModuleFileName(Module *Module)
Retrieve the name of the cached 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.
ModuleMap & getModuleMap()
Retrieve the module map.
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.
llvm::MemoryBuffer & addBuiltPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer, off_t Size, time_t ModTime)
Store a just-built PCM under the Filename.
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:40
The module cache used for compiling modules implicitly.
Definition ModuleCache.h:39
AtomicLineLogger & getLogger()
Definition ModuleCache.h:93
virtual std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size, time_t &ModTime)=0
Write the PCM contents to the given path in the module cache.
virtual InMemoryModuleCache & getInMemoryModuleCache()=0
Returns this process's view of the module cache.
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.
Identifies a module file to be loaded.
Definition Module.h:109
bool empty() const
Checks whether the module file name is empty.
Definition Module.h:194
static ModuleFileName makeExplicit(std::string Name)
Creates a file name for an explicit module.
Definition Module.h:142
StringRef str() const
Returns the plain module file name.
Definition Module.h:188
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:796
module_iterator module_begin() const
Definition ModuleMap.h:798
OptionalFileEntryRef getModuleMapFileForUniquing(const Module *M) const
std::optional< Module * > getCachedModuleLoad(const IdentifierInfo &II)
Return a cached module load.
Definition ModuleMap.h:810
module_iterator module_end() const
Definition ModuleMap.h:799
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:53
void cacheModuleLoad(const IdentifierInfo &II, Module *M)
Cache a module load. M might be nullptr.
Definition ModuleMap.h:805
void loadAllParsedModules()
Module * findOrLoadModule(StringRef Name)
Describes a module or submodule.
Definition Module.h:340
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition Module.h:950
std::vector< std::string > ConfigMacros
The set of "configuration macros", which are macros that (intentionally) change how this module is bu...
Definition Module.h:728
unsigned IsUnimportable
Whether this module has declared itself unimportable, either because it's missing a requirement from ...
Definition Module.h:561
NameVisibilityKind
Describes the visibility of the various names within a particular module.
Definition Module.h:643
@ Hidden
All of the names in this module are hidden.
Definition Module.h:645
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:456
const ModuleFileKey * getASTFileKey() const
The serialized AST file key for this module, if one was created.
Definition Module.h:961
SourceLocation DefinitionLoc
The location of the module definition.
Definition Module.h:346
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers).
Definition Module.h:589
std::string Name
The name of this module.
Definition Module.h:343
llvm::iterator_range< submodule_iterator > submodules()
Definition Module.h:1067
OptionalDirectoryEntryRef Directory
The build directory of this module.
Definition Module.h:394
ModuleRef findSubmodule(StringRef Name) const
Find the submodule with the given name.
Definition Module.cpp:351
unsigned IsFromModuleFile
Whether this module was loaded from a module file.
Definition Module.h:576
unsigned HasIncompatibleModuleFile
Whether we tried and failed to load a module file for this module.
Definition Module.h:565
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:240
unsigned IsAvailable
Whether this module is available in the current translation unit.
Definition Module.h:572
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition Module.h:940
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
virtual llvm::StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const =0
Returns the serialized AST inside the PCH container Buffer.
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition PPCallbacks.h:37
@ 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:869
ASTReaderListenter implementation to set SuggestedPredefines of ASTReader which is required to use a ...
Definition ASTReader.h:360
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.
SourceLocation getBegin() const
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:227
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, TargetOptions &Opts)
Construct a target for the given options.
Definition Targets.cpp:840
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
virtual void setAuxTarget(const TargetInfo *Aux)
void noSignedCharForObjCBool()
Definition TargetInfo.h:948
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.
static std::error_code setConvertersFromOptions(TextEncoding &TE, const clang::LangOptions &Opts)
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:158
Defines the clang::TargetInfo interface.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
@ HeaderSearch
Remove unused header search paths including header maps.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
@ PluginAction
Run a plugin action,.
@ RewriteObjC
ObjC->C Rewriter.
bool Inv(InterpState &S)
Definition Interp.h:827
@ MK_PCH
File is a PCH file treated as such.
Definition ModuleFile.h:52
@ MK_Preamble
File is a PCH file treated as the preamble.
Definition ModuleFile.h:55
@ MK_ExplicitModule
File is an explicitly-loaded module.
Definition ModuleFile.h:49
@ MK_ImplicitModule
File is an implicitly-loaded module.
Definition ModuleFile.h:46
@ MK_PrebuiltModule
File is from a prebuilt module path.
Definition ModuleFile.h:61
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions &DiagOpts, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:196
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.
std::shared_ptr< ModuleCache > createCrossProcessModuleCache()
Creates new ModuleCache backed by a file system directory that may be operated on by multiple process...
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
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)
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition Stack.h:26
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:50
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:1774
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...
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
A source location that has been parsed on the command line.