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