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