clang 22.0.0git
CompilerInstance.h
Go to the documentation of this file.
1//===-- CompilerInstance.h - Clang Compiler Instance ------------*- C++ -*-===//
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
9#ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
10#define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
11
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/IntrusiveRefCntPtr.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/Support/BuryPointer.h"
27#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/VirtualFileSystem.h"
29#include "llvm/Support/VirtualOutputBackend.h"
30#include <cassert>
31#include <list>
32#include <memory>
33#include <optional>
34#include <string>
35#include <utility>
36
37namespace llvm {
38class raw_fd_ostream;
39class PassPlugin;
40class Timer;
41class TimerGroup;
42}
43
44namespace clang {
45class ASTContext;
46class ASTReader;
47
48namespace serialization {
49class ModuleFile;
50}
51
55class FileManager;
56class FrontendAction;
57class Module;
58class ModuleCache;
59class Preprocessor;
60class Sema;
61class SourceManager;
62class TargetInfo;
64
65/// CompilerInstance - Helper class for managing a single instance of the Clang
66/// compiler.
67///
68/// The CompilerInstance serves two purposes:
69/// (1) It manages the various objects which are necessary to run the compiler,
70/// for example the preprocessor, the target information, and the AST
71/// context.
72/// (2) It provides utility routines for constructing and manipulating the
73/// common Clang objects.
74///
75/// The compiler instance generally owns the instance of all the objects that it
76/// manages. However, clients can still share objects by manually setting the
77/// object and retaking ownership prior to destroying the CompilerInstance.
78///
79/// The compiler instance is intended to simplify clients, but not to lock them
80/// in to the compiler instance for everything. When possible, utility functions
81/// come in two forms; a short form that reuses the CompilerInstance objects,
82/// and a long form that takes explicit instances of any required objects.
83class CompilerInstance : public ModuleLoader {
84 /// The options used in this compiler instance.
85 std::shared_ptr<CompilerInvocation> Invocation;
86
87 /// The virtual file system instance.
89
90 /// The diagnostics engine instance.
92
93 /// The target being compiled for.
95
96 /// Options for the auxiliary target.
97 std::unique_ptr<TargetOptions> AuxTargetOpts;
98
99 /// Auxiliary Target info.
101
102 /// The file manager.
104
105 /// The output manager.
107
108 /// The source manager.
110
111 /// The cache of PCM files.
113
114 /// Functor for getting the dependency preprocessor directives of a file.
115 std::unique_ptr<DependencyDirectivesGetter> GetDependencyDirectives;
116
117 /// The preprocessor.
118 std::shared_ptr<Preprocessor> PP;
119
120 /// The AST context.
122
123 /// An optional sema source that will be attached to sema.
125
126 /// The AST consumer.
127 std::unique_ptr<ASTConsumer> Consumer;
128
129 /// The code completion consumer.
130 std::unique_ptr<CodeCompleteConsumer> CompletionConsumer;
131
132 /// The semantic analysis object.
133 std::unique_ptr<Sema> TheSema;
134
135 /// Back-end pass plugins.
136 std::vector<std::unique_ptr<llvm::PassPlugin>> PassPlugins;
137
138 /// The frontend timer group.
139 std::unique_ptr<llvm::TimerGroup> timerGroup;
140
141 /// The frontend timer.
142 std::unique_ptr<llvm::Timer> FrontendTimer;
143
144 /// The ASTReader, if one exists.
146
147 /// The module dependency collector for crashdumps
148 std::shared_ptr<ModuleDependencyCollector> ModuleDepCollector;
149
150 /// The module provider.
151 std::shared_ptr<PCHContainerOperations> ThePCHContainerOperations;
152
153 std::vector<std::shared_ptr<DependencyCollector>> DependencyCollectors;
154
155 /// The set of modules that failed to build.
156 ///
157 /// This value will be passed among all of the compiler instances created
158 /// to (re)build modules, so that once a module fails to build anywhere,
159 /// other instances will see that the module has failed and won't try to
160 /// build it again.
161 llvm::StringSet<> FailedModules;
162
163 /// The set of top-level modules that has already been built on the
164 /// fly as part of this overall compilation action.
165 std::map<std::string, std::string, std::less<>> BuiltModules;
166
167 /// Should we delete the BuiltModules when we're done?
168 bool DeleteBuiltModules = true;
169
170 /// The location of the module-import keyword for the last module
171 /// import.
172 SourceLocation LastModuleImportLoc;
173
174 /// The result of the last module import.
175 ///
176 ModuleLoadResult LastModuleImportResult;
177
178 /// Whether we should (re)build the global module index once we
179 /// have finished with this translation unit.
180 bool BuildGlobalModuleIndex = false;
181
182 /// We have a full global module index, with all modules.
183 bool HaveFullGlobalModuleIndex = false;
184
185 /// One or more modules failed to build.
186 bool DisableGeneratingGlobalModuleIndex = false;
187
188 /// The stream for verbose output if owned, otherwise nullptr.
189 std::unique_ptr<raw_ostream> OwnedVerboseOutputStream;
190
191 /// The stream for verbose output.
192 raw_ostream *VerboseOutputStream = &llvm::errs();
193
194 /// The list of active output files.
195 std::list<llvm::vfs::OutputFile> OutputFiles;
196
197 /// Force an output buffer.
198 std::unique_ptr<llvm::raw_pwrite_stream> OutputStream;
199
200 CompilerInstance(const CompilerInstance &) = delete;
201 void operator=(const CompilerInstance &) = delete;
202public:
203 explicit CompilerInstance(
204 std::shared_ptr<CompilerInvocation> Invocation =
205 std::make_shared<CompilerInvocation>(),
206 std::shared_ptr<PCHContainerOperations> PCHContainerOps =
207 std::make_shared<PCHContainerOperations>(),
208 ModuleCache *ModCache = nullptr);
209 ~CompilerInstance() override;
210
211 /// @name High-Level Operations
212 /// @{
213
214 /// ExecuteAction - Execute the provided action against the compiler's
215 /// CompilerInvocation object.
216 ///
217 /// This function makes the following assumptions:
218 ///
219 /// - The invocation options should be initialized. This function does not
220 /// handle the '-help' or '-version' options, clients should handle those
221 /// directly.
222 ///
223 /// - The diagnostics engine should have already been created by the client.
224 ///
225 /// - No other CompilerInstance state should have been initialized (this is
226 /// an unchecked error).
227 ///
228 /// - Clients should have initialized any LLVM target features that may be
229 /// required.
230 ///
231 /// - Clients should eventually call llvm_shutdown() upon the completion of
232 /// this routine to ensure that any managed objects are properly destroyed.
233 ///
234 /// Note that this routine may write output to 'stderr'.
235 ///
236 /// \param Act - The action to execute.
237 /// \return - True on success.
238 //
239 // FIXME: Eliminate the llvm_shutdown requirement, that should either be part
240 // of the context or else not CompilerInstance specific.
241 bool ExecuteAction(FrontendAction &Act);
242
243 /// At the end of a compilation, print the number of warnings/errors.
245
246 /// Load the list of plugins requested in the \c FrontendOptions.
248
249 /// @}
250 /// @name Compiler Invocation and Options
251 /// @{
252
253 CompilerInvocation &getInvocation() { return *Invocation; }
254
255 std::shared_ptr<CompilerInvocation> getInvocationPtr() { return Invocation; }
256
257 /// Indicates whether we should (re)build the global module index.
258 bool shouldBuildGlobalModuleIndex() const;
259
260 /// Set the flag indicating whether we should (re)build the global
261 /// module index.
262 void setBuildGlobalModuleIndex(bool Build) {
263 BuildGlobalModuleIndex = Build;
264 }
265
266 /// @}
267 /// @name Forwarding Methods
268 /// @{
269
270 AnalyzerOptions &getAnalyzerOpts() { return Invocation->getAnalyzerOpts(); }
271
273 return Invocation->getCodeGenOpts();
274 }
276 return Invocation->getCodeGenOpts();
277 }
278
280 return Invocation->getDependencyOutputOpts();
281 }
283 return Invocation->getDependencyOutputOpts();
284 }
285
287 return Invocation->getDiagnosticOpts();
288 }
290 return Invocation->getDiagnosticOpts();
291 }
292
294 return Invocation->getFileSystemOpts();
295 }
297 return Invocation->getFileSystemOpts();
298 }
299
301 return Invocation->getFrontendOpts();
302 }
304 return Invocation->getFrontendOpts();
305 }
306
308 return Invocation->getHeaderSearchOpts();
309 }
311 return Invocation->getHeaderSearchOpts();
312 }
313
314 APINotesOptions &getAPINotesOpts() { return Invocation->getAPINotesOpts(); }
316 return Invocation->getAPINotesOpts();
317 }
318
319 LangOptions &getLangOpts() { return Invocation->getLangOpts(); }
320 const LangOptions &getLangOpts() const { return Invocation->getLangOpts(); }
321
323 return Invocation->getPreprocessorOpts();
324 }
326 return Invocation->getPreprocessorOpts();
327 }
328
330 return Invocation->getPreprocessorOutputOpts();
331 }
333 return Invocation->getPreprocessorOutputOpts();
334 }
335
337 return Invocation->getTargetOpts();
338 }
340 return Invocation->getTargetOpts();
341 }
342
343 /// @}
344 /// @name Diagnostics Engine
345 /// @{
346
347 bool hasDiagnostics() const { return Diagnostics != nullptr; }
348
349 /// Get the current diagnostics engine.
351 assert(Diagnostics && "Compiler instance has no diagnostics!");
352 return *Diagnostics;
353 }
354
356 assert(Diagnostics && "Compiler instance has no diagnostics!");
357 return Diagnostics;
358 }
359
360 /// setDiagnostics - Replace the current diagnostics engine.
362
364 assert(Diagnostics && Diagnostics->getClient() &&
365 "Compiler instance has no diagnostic client!");
366 return *Diagnostics->getClient();
367 }
368
369 /// @}
370 /// @name VerboseOutputStream
371 /// @{
372
373 /// Replace the current stream for verbose output.
374 void setVerboseOutputStream(raw_ostream &Value);
375
376 /// Replace the current stream for verbose output.
377 void setVerboseOutputStream(std::unique_ptr<raw_ostream> Value);
378
379 /// Get the current stream for verbose output.
380 raw_ostream &getVerboseOutputStream() {
381 return *VerboseOutputStream;
382 }
383
384 /// @}
385 /// @name Target Info
386 /// @{
387
388 bool hasTarget() const { return Target != nullptr; }
389
391 assert(Target && "Compiler instance has no target!");
392 return *Target;
393 }
394
396 assert(Target && "Compiler instance has no target!");
397 return Target;
398 }
399
400 /// Replace the current Target.
402
403 /// @}
404 /// @name AuxTarget Info
405 /// @{
406
407 TargetInfo *getAuxTarget() const { return AuxTarget.get(); }
408
409 /// Replace the current AuxTarget.
411
412 // Create Target and AuxTarget based on current options
413 bool createTarget();
414
415 /// @}
416 /// @name Virtual File System
417 /// @{
418
419 bool hasVirtualFileSystem() const { return VFS != nullptr; }
420
421 /// Create a virtual file system instance based on the invocation.
422 ///
423 /// @param BaseFS The file system that may be used when configuring the final
424 /// file system, and act as the underlying file system. Must not
425 /// be NULL.
426 /// @param DC If non-NULL, the diagnostic consumer to be used in case
427 /// configuring the file system emits diagnostics. Note that the
428 /// DiagnosticsEngine using the consumer won't obey the
429 /// --warning-suppression-mappings= flag.
431 BaseFS = llvm::vfs::getRealFileSystem(),
432 DiagnosticConsumer *DC = nullptr);
433
434 /// Use the given file system.
436 VFS = std::move(FS);
437 }
438
439 llvm::vfs::FileSystem &getVirtualFileSystem() const { return *VFS; }
440
444
445 /// @}
446 /// @name File Manager
447 /// @{
448
449 bool hasFileManager() const { return FileMgr != nullptr; }
450
451 /// Return the current file manager to the caller.
453 assert(FileMgr && "Compiler instance has no file manager!");
454 return *FileMgr;
455 }
456
458 assert(FileMgr && "Compiler instance has no file manager!");
459 return FileMgr;
460 }
461
463 llvm::BuryPointer(FileMgr.get());
464 FileMgr.resetWithoutRelease();
465 }
466
467 /// Replace the current file manager.
469
470 /// @}
471 /// @name Output Manager
472 /// @{
473
474 /// Set the output manager.
475 void
477
478 /// Create an output manager.
479 void createOutputManager();
480
481 bool hasOutputManager() const { return bool(OutputMgr); }
482
483 llvm::vfs::OutputBackend &getOutputManager();
484 llvm::vfs::OutputBackend &getOrCreateOutputManager();
485
486 /// @}
487 /// @name Source Manager
488 /// @{
489
490 bool hasSourceManager() const { return SourceMgr != nullptr; }
491
492 /// Return the current source manager.
494 assert(SourceMgr && "Compiler instance has no source manager!");
495 return *SourceMgr;
496 }
497
499 assert(SourceMgr && "Compiler instance has no source manager!");
500 return SourceMgr;
501 }
502
504 llvm::BuryPointer(SourceMgr.get());
505 SourceMgr.resetWithoutRelease();
506 }
507
508 /// setSourceManager - Replace the current source manager.
510
511 /// @}
512 /// @name Preprocessor
513 /// @{
514
515 bool hasPreprocessor() const { return PP != nullptr; }
516
517 /// Return the current preprocessor.
519 assert(PP && "Compiler instance has no preprocessor!");
520 return *PP;
521 }
522
523 std::shared_ptr<Preprocessor> getPreprocessorPtr() { return PP; }
524
526 llvm::BuryPointer(new std::shared_ptr<Preprocessor>(PP));
527 }
528
529 /// Replace the current preprocessor.
530 void setPreprocessor(std::shared_ptr<Preprocessor> Value);
531
532 /// @}
533 /// @name ASTContext
534 /// @{
535
536 bool hasASTContext() const { return Context != nullptr; }
537
539 assert(Context && "Compiler instance has no AST context!");
540 return *Context;
541 }
542
544 assert(Context && "Compiler instance has no AST context!");
545 return Context;
546 }
547
549 llvm::BuryPointer(Context.get());
550 Context.resetWithoutRelease();
551 }
552
553 /// setASTContext - Replace the current AST context.
555
556 /// Replace the current Sema; the compiler instance takes ownership
557 /// of S.
558 void setSema(Sema *S);
559
560 /// @}
561 /// @name ASTConsumer
562 /// @{
563
564 bool hasASTConsumer() const { return (bool)Consumer; }
565
567 assert(Consumer && "Compiler instance has no AST consumer!");
568 return *Consumer;
569 }
570
571 /// takeASTConsumer - Remove the current AST consumer and give ownership to
572 /// the caller.
573 std::unique_ptr<ASTConsumer> takeASTConsumer() { return std::move(Consumer); }
574
575 /// setASTConsumer - Replace the current AST consumer; the compiler instance
576 /// takes ownership of \p Value.
577 void setASTConsumer(std::unique_ptr<ASTConsumer> Value);
578
579 /// @}
580 /// @name Semantic analysis
581 /// @{
582 bool hasSema() const { return (bool)TheSema; }
583
584 Sema &getSema() const {
585 assert(TheSema && "Compiler instance has no Sema object!");
586 return *TheSema;
587 }
588
589 std::unique_ptr<Sema> takeSema();
590 void resetAndLeakSema();
591
592 /// @}
593 /// @name Module Management
594 /// @{
595
598
599 std::shared_ptr<ModuleDependencyCollector> getModuleDepCollector() const;
601 std::shared_ptr<ModuleDependencyCollector> Collector);
602
603 std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const {
604 return ThePCHContainerOperations;
605 }
606
607 /// Return the appropriate PCHContainerWriter depending on the
608 /// current CodeGenOptions.
610 assert(Invocation && "cannot determine module format without invocation");
611 StringRef Format = getHeaderSearchOpts().ModuleFormat;
612 auto *Writer = ThePCHContainerOperations->getWriterOrNull(Format);
613 if (!Writer) {
614 if (Diagnostics)
615 Diagnostics->Report(diag::err_module_format_unhandled) << Format;
616 llvm::report_fatal_error("unknown module format");
617 }
618 return *Writer;
619 }
620
621 /// Return the appropriate PCHContainerReader depending on the
622 /// current CodeGenOptions.
624 assert(Invocation && "cannot determine module format without invocation");
625 StringRef Format = getHeaderSearchOpts().ModuleFormat;
626 auto *Reader = ThePCHContainerOperations->getReaderOrNull(Format);
627 if (!Reader) {
628 if (Diagnostics)
629 Diagnostics->Report(diag::err_module_format_unhandled) << Format;
630 llvm::report_fatal_error("unknown module format");
631 }
632 return *Reader;
633 }
634
635 /// @}
636 /// @name Code Completion
637 /// @{
638
639 bool hasCodeCompletionConsumer() const { return (bool)CompletionConsumer; }
640
642 assert(CompletionConsumer &&
643 "Compiler instance has no code completion consumer!");
644 return *CompletionConsumer;
645 }
646
647 /// setCodeCompletionConsumer - Replace the current code completion consumer;
648 /// the compiler instance takes ownership of \p Value.
650
651 /// }
652 /// @name Back-end Pass Plugins
653 /// @{
654
656 return PassPlugins;
657 }
658
659 /// @}
660 /// @name Frontend timer
661 /// @{
662
663 llvm::TimerGroup &getTimerGroup() const { return *timerGroup; }
664
665 llvm::Timer &getFrontendTimer() const {
666 assert(FrontendTimer && "Compiler instance has no frontend timer!");
667 return *FrontendTimer;
668 }
669
670 /// }
671 /// @name Output Files
672 /// @{
673
674 /// clearOutputFiles - Clear the output file list. The underlying output
675 /// streams must have been closed beforehand.
676 ///
677 /// \param EraseFiles - If true, attempt to erase the files from disk.
678 void clearOutputFiles(bool EraseFiles);
679
680 /// @}
681 /// @name Construction Utility Methods
682 /// @{
683
684 /// Create the diagnostics engine using the invocation's diagnostic options
685 /// and replace any existing one with it.
686 ///
687 /// Note that this routine also replaces the diagnostic client,
688 /// allocating one if one is not provided.
689 ///
690 /// \param Client If non-NULL, a diagnostic client that will be
691 /// attached to (and, then, owned by) the DiagnosticsEngine inside this AST
692 /// unit.
693 ///
694 /// \param ShouldOwnClient If Client is non-NULL, specifies whether
695 /// the diagnostic object should take ownership of the client.
696 void createDiagnostics(DiagnosticConsumer *Client = nullptr,
697 bool ShouldOwnClient = true);
698
699 /// Create a DiagnosticsEngine object.
700 ///
701 /// If no diagnostic client is provided, this creates a
702 /// DiagnosticConsumer that is owned by the returned diagnostic
703 /// object, if using directly the caller is responsible for
704 /// releasing the returned DiagnosticsEngine's client eventually.
705 ///
706 /// \param VFS The file system used to load the suppression mappings file.
707 ///
708 /// \param Opts - The diagnostic options; note that the created text
709 /// diagnostic object contains a reference to these options.
710 ///
711 /// \param Client If non-NULL, a diagnostic client that will be
712 /// attached to (and, then, owned by) the returned DiagnosticsEngine
713 /// object. If NULL, the returned DiagnosticsEngine will own a newly-created
714 /// client.
715 ///
716 /// \param CodeGenOpts If non-NULL, the code gen options in use, which may be
717 /// used by some diagnostics printers (for logging purposes only).
718 ///
719 /// \return The new object on success, or null on failure.
721 createDiagnostics(llvm::vfs::FileSystem &VFS, DiagnosticOptions &Opts,
722 DiagnosticConsumer *Client = nullptr,
723 bool ShouldOwnClient = true,
724 const CodeGenOptions *CodeGenOpts = nullptr);
725
726 /// Create the file manager and replace any existing one with it.
727 void createFileManager();
728
729 /// Create the source manager and replace any existing one with it.
730 void createSourceManager();
731
732 /// Create the preprocessor, using the invocation, file, and source managers,
733 /// and replace any existing one with it.
735
737 std::unique_ptr<DependencyDirectivesGetter> Getter) {
738 GetDependencyDirectives = std::move(Getter);
739 }
740
741 std::string getSpecificModuleCachePath(StringRef ModuleHash);
743 return getSpecificModuleCachePath(getInvocation().getModuleHash());
744 }
745
746 /// Create the AST context.
747 void createASTContext();
748
749 /// Create an external AST source to read a PCH file and attach it to the AST
750 /// context.
752 StringRef Path, DisableValidationForModuleKind DisableValidation,
753 bool AllowPCHWithCompilerErrors, void *DeserializationListener,
754 bool OwnDeserializationListener);
755
756 /// Create an external AST source to read a PCH file.
757 ///
758 /// \return - The new object on success, or null on failure.
760 StringRef Path, StringRef Sysroot,
761 DisableValidationForModuleKind DisableValidation,
762 bool AllowPCHWithCompilerErrors, Preprocessor &PP, ModuleCache &ModCache,
763 ASTContext &Context, const PCHContainerReader &PCHContainerRdr,
764 const CodeGenOptions &CodeGenOpts,
765 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
766 ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
767 void *DeserializationListener, bool OwnDeserializationListener,
768 bool Preamble, bool UseGlobalModuleIndex);
769
770 /// Create a code completion consumer using the invocation; note that this
771 /// will cause the source manager to truncate the input source file at the
772 /// completion point.
774
775 /// Create a code completion consumer to print code completion results, at
776 /// \p Filename, \p Line, and \p Column, to the given output stream \p OS.
778 Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column,
779 const CodeCompleteOptions &Opts, raw_ostream &OS);
780
781 /// Create the Sema object to be used for parsing.
783 CodeCompleteConsumer *CompletionConsumer);
784
785 /// Create the frontend timer and replace any existing one with it.
786 void createFrontendTimer();
787
788 /// Create the default output file (from the invocation's options) and add it
789 /// to the list of tracked output files.
790 ///
791 /// The files created by this are usually removed on signal, and, depending
792 /// on FrontendOptions, may also use a temporary file (that is, the data is
793 /// written to a temporary file which will atomically replace the target
794 /// output on success).
795 ///
796 /// \return - Null on error.
797 std::unique_ptr<raw_pwrite_stream> createDefaultOutputFile(
798 bool Binary = true, StringRef BaseInput = "", StringRef Extension = "",
799 bool RemoveFileOnSignal = true, bool CreateMissingDirectories = false,
800 bool ForceUseTemporary = false);
801
802 /// Create a new output file, optionally deriving the output path name, and
803 /// add it to the list of tracked output files.
804 ///
805 /// \return - Null on error.
806 std::unique_ptr<raw_pwrite_stream>
807 createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal,
808 bool UseTemporary, bool CreateMissingDirectories = false);
809
810private:
811 /// Create a new output file and add it to the list of tracked output files.
812 ///
813 /// If \p OutputPath is empty, then createOutputFile will derive an output
814 /// path location as \p BaseInput, with any suffix removed, and \p Extension
815 /// appended. If \p OutputPath is not stdout and \p UseTemporary
816 /// is true, createOutputFile will create a new temporary file that must be
817 /// renamed to \p OutputPath in the end.
818 ///
819 /// \param OutputPath - If given, the path to the output file.
820 /// \param Binary - The mode to open the file in.
821 /// \param RemoveFileOnSignal - Whether the file should be registered with
822 /// llvm::sys::RemoveFileOnSignal. Note that this is not safe for
823 /// multithreaded use, as the underlying signal mechanism is not reentrant
824 /// \param UseTemporary - Create a new temporary file that must be renamed to
825 /// OutputPath in the end.
826 /// \param CreateMissingDirectories - When \p UseTemporary is true, create
827 /// missing directories in the output path.
829 createOutputFileImpl(StringRef OutputPath, bool Binary,
830 bool RemoveFileOnSignal, bool UseTemporary,
831 bool CreateMissingDirectories);
832
833public:
834 std::unique_ptr<raw_pwrite_stream> createNullOutputFile();
835
836 /// @}
837 /// @name Initialization Utility Methods
838 /// @{
839
840 /// InitializeSourceManager - Initialize the source manager to set InputFile
841 /// as the main file.
842 ///
843 /// \return True on success.
845
846 /// InitializeSourceManager - Initialize the source manager to set InputFile
847 /// as the main file.
848 ///
849 /// \return True on success.
850 static bool InitializeSourceManager(const FrontendInputFile &Input,
851 DiagnosticsEngine &Diags,
853 SourceManager &SourceMgr);
854
855 /// @}
856
857 void setOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> OutStream) {
858 OutputStream = std::move(OutStream);
859 }
860
861 std::unique_ptr<llvm::raw_pwrite_stream> takeOutputStream() {
862 return std::move(OutputStream);
863 }
864
865 void createASTReader();
866
867 bool loadModuleFile(StringRef FileName,
868 serialization::ModuleFile *&LoadedModuleFile);
869
870 /// Configuration object for making the result of \c cloneForModuleCompile()
871 /// thread-safe.
874 DiagnosticConsumer &DiagConsumer;
875 std::shared_ptr<ModuleDependencyCollector> ModuleDepCollector;
876
877 public:
880 DiagnosticConsumer &DiagConsumer,
881 std::shared_ptr<ModuleDependencyCollector> ModuleDepCollector = nullptr)
882 : VFS(std::move(VFS)), DiagConsumer(DiagConsumer),
883 ModuleDepCollector(std::move(ModuleDepCollector)) {
884 assert(this->VFS && "Clone config requires non-null VFS");
885 }
886
888 DiagnosticConsumer &getDiagConsumer() const { return DiagConsumer; }
889 std::shared_ptr<ModuleDependencyCollector> getModuleDepCollector() const {
890 return ModuleDepCollector;
891 }
892 };
893
894private:
895 /// Find a module, potentially compiling it, before reading its AST. This is
896 /// the guts of loadModule.
897 ///
898 /// For prebuilt modules, the Module is not expected to exist in
899 /// HeaderSearch's ModuleMap. If a ModuleFile by that name is in the
900 /// ModuleManager, then it will be loaded and looked up.
901 ///
902 /// For implicit modules, the Module is expected to already be in the
903 /// ModuleMap. First attempt to load it from the given path on disk. If that
904 /// fails, defer to compileModuleAndReadAST, which will first build and then
905 /// load it.
906 ModuleLoadResult findOrCompileModuleAndReadAST(StringRef ModuleName,
907 SourceLocation ImportLoc,
908 SourceLocation ModuleNameLoc,
909 bool IsInclusionDirective);
910
911 /// Creates a \c CompilerInstance for compiling a module.
912 ///
913 /// This expects a properly initialized \c FrontendInputFile.
914 std::unique_ptr<CompilerInstance> cloneForModuleCompileImpl(
915 SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
916 StringRef OriginalModuleMapFile, StringRef ModuleFileName,
917 std::optional<ThreadSafeCloneConfig> ThreadSafeConfig = std::nullopt);
918
919public:
920 /// Creates a new \c CompilerInstance for compiling a module.
921 ///
922 /// This takes care of creating appropriate \c FrontendInputFile for
923 /// public/private frameworks, inferred modules and such.
924 ///
925 /// The \c ThreadSafeConfig takes precedence over the \c DiagnosticConsumer
926 /// and \c FileSystem of this instance (and disables \c FileManager sharing).
927 std::unique_ptr<CompilerInstance> cloneForModuleCompile(
928 SourceLocation ImportLoc, const Module *Module, StringRef ModuleFileName,
929 std::optional<ThreadSafeCloneConfig> ThreadSafeConfig = std::nullopt);
930
931 /// Compile a module file for the given module, using the options
932 /// provided by the importing compiler instance. Returns true if the module
933 /// was built without errors.
934 // FIXME: This should be private, but it's called from static non-member
935 // functions in the implementation file.
936 bool compileModule(SourceLocation ImportLoc, StringRef ModuleName,
937 StringRef ModuleFileName, CompilerInstance &Instance);
938
941 bool IsInclusionDirective) override;
942
943 void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName,
944 StringRef Source) override;
945
947 SourceLocation ImportLoc) override;
948
952
954
955 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override;
956
957 void addDependencyCollector(std::shared_ptr<DependencyCollector> Listener) {
958 DependencyCollectors.push_back(std::move(Listener));
959 }
960
961 void clearDependencyCollectors() { DependencyCollectors.clear(); }
962
963 std::vector<std::shared_ptr<DependencyCollector>> &getDependencyCollectors() {
964 return DependencyCollectors;
965 }
966
968
969 ModuleCache &getModuleCache() const { return *ModCache; }
970};
971
972} // end namespace clang
973
974#endif
Defines the Diagnostic-related interfaces.
This is the interface for scanning header and source files to get the minimum necessary preprocessor ...
Defines the SourceManager interface.
Tracks various options which control how API notes are found and handled.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition ASTConsumer.h:35
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
Reads an AST files chain containing the contents of a translation unit.
Definition ASTReader.h:430
Stores options for the analyzer from the command line.
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::shared_ptr< ModuleDependencyCollector > getModuleDepCollector() const
ThreadSafeCloneConfig(IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, DiagnosticConsumer &DiagConsumer, std::shared_ptr< ModuleDependencyCollector > ModuleDepCollector=nullptr)
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVFS() const
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void createPCHExternalASTSource(StringRef Path, DisableValidationForModuleKind DisableValidation, bool AllowPCHWithCompilerErrors, void *DeserializationListener, bool OwnDeserializationListener)
Create an external AST source to read a PCH file and attach it to the AST context.
DiagnosticConsumer & getDiagnosticClient() const
void createPreprocessor(TranslationUnitKind TUKind)
Create the preprocessor, using the invocation, file, and source managers, and replace any existing on...
AnalyzerOptions & getAnalyzerOpts()
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 ...
const HeaderSearchOptions & getHeaderSearchOpts() const
DependencyOutputOptions & getDependencyOutputOpts()
const DependencyOutputOptions & getDependencyOutputOpts() const
std::shared_ptr< Preprocessor > getPreprocessorPtr()
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.
llvm::TimerGroup & getTimerGroup() const
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="", bool RemoveFileOnSignal=true, bool CreateMissingDirectories=false, bool ForceUseTemporary=false)
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
void setExternalSemaSource(IntrusiveRefCntPtr< ExternalSemaSource > ESS)
bool compileModule(SourceLocation ImportLoc, StringRef ModuleName, StringRef ModuleFileName, CompilerInstance &Instance)
Compile a module file for the given module, using the options provided by the importing compiler inst...
const DiagnosticOptions & getDiagnosticOpts() const
std::string getSpecificModuleCachePath()
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective) override
Attempt to load the given module.
const CodeGenOptions & getCodeGenOpts() const
FileSystemOptions & getFileSystemOpts()
llvm::Timer & getFrontendTimer() const
const TargetOptions & getTargetOpts() 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.
void setVirtualFileSystem(IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
Use the given file system.
void setOutputStream(std::unique_ptr< llvm::raw_pwrite_stream > OutStream)
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.
IntrusiveRefCntPtr< DiagnosticsEngine > getDiagnosticsPtr() const
void createOutputManager()
Create an output manager.
std::unique_ptr< Sema > takeSema()
const PreprocessorOptions & getPreprocessorOpts() const
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
IntrusiveRefCntPtr< ASTContext > getASTContextPtr() const
ModuleCache & getModuleCache() const
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
IntrusiveRefCntPtr< ASTReader > getASTReader() const
void setTarget(TargetInfo *Value)
Replace the current Target.
void setModuleDepCollector(std::shared_ptr< ModuleDependencyCollector > Collector)
void addDependencyCollector(std::shared_ptr< DependencyCollector > Listener)
void createASTContext()
Create the AST context.
std::unique_ptr< raw_pwrite_stream > createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, bool UseTemporary, bool CreateMissingDirectories=false)
Create a new output file, optionally deriving the output path name, and add it to the list of tracked...
void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName, StringRef Source) override
Attempt to create the given module from the specified source buffer.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
ASTContext & getASTContext() const
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr() const
void LoadRequestedPlugins()
Load the list of plugins requested in the FrontendOptions.
TargetOptions & getTargetOpts()
IntrusiveRefCntPtr< TargetInfo > getTargetPtr() const
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)
std::unique_ptr< llvm::raw_pwrite_stream > takeOutputStream()
FrontendOptions & getFrontendOpts()
const FrontendOptions & getFrontendOpts() const
std::shared_ptr< ModuleDependencyCollector > getModuleDepCollector() const
std::shared_ptr< CompilerInvocation > getInvocationPtr()
bool hadModuleLoaderFatalFailure() 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.
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.
bool hasCodeCompletionConsumer() const
std::vector< std::shared_ptr< DependencyCollector > > & getDependencyCollectors()
const LangOptions & getLangOpts() const
void createSourceManager()
Create the source manager and replace any existing one with it.
CompilerInvocation & getInvocation()
const PCHContainerWriter & getPCHContainerWriter() const
Return the appropriate PCHContainerWriter depending on the current CodeGenOptions.
void setVerboseOutputStream(raw_ostream &Value)
Replace the current stream for verbose output.
std::unique_ptr< ASTConsumer > takeASTConsumer()
takeASTConsumer - Remove the current AST consumer and give ownership to the caller.
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
const FileSystemOptions & getFileSystemOpts() 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::ArrayRef< std::unique_ptr< llvm::PassPlugin > > getPassPlugins() const
llvm::vfs::OutputBackend & getOrCreateOutputManager()
const APINotesOptions & getAPINotesOpts() const
CodeGenOptions & getCodeGenOpts()
SourceManager & getSourceManager() const
Return the current source manager.
IntrusiveRefCntPtr< SourceManager > getSourceManagerPtr() const
CodeCompleteConsumer & getCodeCompletionConsumer() const
void setDiagnostics(llvm::IntrusiveRefCntPtr< DiagnosticsEngine > Value)
setDiagnostics - Replace the current diagnostics engine.
bool shouldBuildGlobalModuleIndex() const
Indicates whether we should (re)build the global module index.
APINotesOptions & getAPINotesOpts()
std::unique_ptr< raw_pwrite_stream > createNullOutputFile()
void setAuxTarget(TargetInfo *Value)
Replace the current AuxTarget.
void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, SourceLocation ImportLoc) override
Make the given module visible.
bool loadModuleFile(StringRef FileName, serialization::ModuleFile *&LoadedModuleFile)
void setDependencyDirectivesGetter(std::unique_ptr< DependencyDirectivesGetter > Getter)
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.
Helper class for holding the data necessary to invoke the compiler.
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Options for controlling the compiler diagnostics engine.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:232
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:53
Keeps track of options that affect how file operations are performed.
Abstract base class for actions which can be performed by the frontend.
An input file for the front end.
FrontendOptions - Options for controlling the behavior of the frontend.
A global index for a set of module files, providing information about the identifiers within those mo...
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
std::string ModuleFormat
The module/pch container format.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
The module cache used for compiling modules implicitly.
Definition ModuleCache.h:26
Describes the result of attempting to load a module.
ModuleLoader(bool BuildingModule=false)
Describes a module or submodule.
Definition Module.h:144
NameVisibilityKind
Describes the visibility of the various names within a particular module.
Definition Module.h:443
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
This abstract interface provides operations for creating containers for serialized ASTs (precompiled ...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
PreprocessorOutputOptions - Options for controlling the C preprocessor output (e.g....
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:855
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Exposes information about the current target.
Definition TargetInfo.h:226
Options for controlling the target.
Information about a module that has been loaded by the ASTReader.
Definition ModuleFile.h:130
#define bool
Definition gpuintrin.h:32
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
ArrayRef< IdentifierLoc > ModuleIdPath
A sequence of identifier/location pairs used to describe a particular module or submodule,...
TranslationUnitKind
Describes the kind of translation unit being processed.
DisableValidationForModuleKind
Whether to disable the normal validation performed on precompiled headers and module files when they ...
Visibility
Describes the different kinds of visibility that a declaration may have.
Definition Visibility.h:34
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30