9#ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
10#define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/IntrusiveRefCntPtr.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/BuryPointer.h"
25#include "llvm/Support/FileSystem.h"
42class CodeCompleteConsumer;
43class DiagnosticsEngine;
44class DiagnosticConsumer;
47class InMemoryModuleCache;
75 std::shared_ptr<CompilerInvocation> Invocation;
96 std::shared_ptr<Preprocessor> PP;
105 std::unique_ptr<ASTConsumer> Consumer;
108 std::unique_ptr<CodeCompleteConsumer> CompletionConsumer;
111 std::unique_ptr<Sema> TheSema;
114 std::unique_ptr<llvm::TimerGroup> FrontendTimerGroup;
117 std::unique_ptr<llvm::Timer> FrontendTimer;
123 std::shared_ptr<ModuleDependencyCollector> ModuleDepCollector;
126 std::shared_ptr<PCHContainerOperations> ThePCHContainerOperations;
128 std::vector<std::shared_ptr<DependencyCollector>> DependencyCollectors;
132 std::map<std::string, std::string, std::less<>> BuiltModules;
135 bool DeleteBuiltModules =
true;
147 bool BuildGlobalModuleIndex =
false;
150 bool HaveFullGlobalModuleIndex =
false;
153 bool DisableGeneratingGlobalModuleIndex =
false;
156 std::unique_ptr<raw_ostream> OwnedVerboseOutputStream;
159 raw_ostream *VerboseOutputStream = &llvm::errs();
168 std::optional<llvm::sys::fs::TempFile>
File;
170 OutputFile(std::string filename,
171 std::optional<llvm::sys::fs::TempFile> file)
172 :
Filename(std::move(filename)),
File(std::move(file)) {}
176 std::list<OutputFile> OutputFiles;
179 std::unique_ptr<llvm::raw_pwrite_stream> OutputStream;
185 std::shared_ptr<PCHContainerOperations> PCHContainerOps =
186 std::make_shared<PCHContainerOperations>(),
232 assert(Invocation &&
"Compiler instance has no invocation!");
245 BuildGlobalModuleIndex = Build;
253 return Invocation->getAnalyzerOpts();
257 return Invocation->getCodeGenOpts();
260 return Invocation->getCodeGenOpts();
264 return Invocation->getDependencyOutputOpts();
267 return Invocation->getDependencyOutputOpts();
271 return Invocation->getDiagnosticOpts();
274 return Invocation->getDiagnosticOpts();
278 return Invocation->getFileSystemOpts();
281 return Invocation->getFileSystemOpts();
285 return Invocation->getFrontendOpts();
288 return Invocation->getFrontendOpts();
292 return Invocation->getHeaderSearchOpts();
295 return Invocation->getHeaderSearchOpts();
298 return Invocation->getHeaderSearchOptsPtr();
302 return *Invocation->getLangOpts();
305 return *Invocation->getLangOpts();
309 return Invocation->getPreprocessorOpts();
312 return Invocation->getPreprocessorOpts();
316 return Invocation->getPreprocessorOutputOpts();
319 return Invocation->getPreprocessorOutputOpts();
323 return Invocation->getTargetOpts();
326 return Invocation->getTargetOpts();
337 assert(Diagnostics &&
"Compiler instance has no diagnostics!");
345 assert(Diagnostics && Diagnostics->getClient() &&
346 "Compiler instance has no diagnostic client!");
347 return *Diagnostics->getClient();
362 return *VerboseOutputStream;
372 assert(
Target &&
"Compiler instance has no target!");
405 assert(FileMgr &&
"Compiler instance has no file manager!");
410 llvm::BuryPointer(FileMgr.get());
411 FileMgr.resetWithoutRelease();
425 assert(SourceMgr &&
"Compiler instance has no source manager!");
430 llvm::BuryPointer(SourceMgr.get());
431 SourceMgr.resetWithoutRelease();
445 assert(PP &&
"Compiler instance has no preprocessor!");
452 llvm::BuryPointer(
new std::shared_ptr<Preprocessor>(PP));
465 assert(Context &&
"Compiler instance has no AST context!");
470 llvm::BuryPointer(Context.get());
471 Context.resetWithoutRelease();
488 assert(Consumer &&
"Compiler instance has no AST consumer!");
503 bool hasSema()
const {
return (
bool)TheSema; }
506 assert(TheSema &&
"Compiler instance has no Sema object!");
522 std::shared_ptr<ModuleDependencyCollector> Collector);
525 return ThePCHContainerOperations;
531 assert(Invocation &&
"cannot determine module format without invocation");
533 auto *Writer = ThePCHContainerOperations->getWriterOrNull(Format);
536 Diagnostics->Report(diag::err_module_format_unhandled) << Format;
537 llvm::report_fatal_error(
"unknown module format");
545 assert(Invocation &&
"cannot determine module format without invocation");
547 auto *Reader = ThePCHContainerOperations->getReaderOrNull(Format);
550 Diagnostics->Report(diag::err_module_format_unhandled) << Format;
551 llvm::report_fatal_error(
"unknown module format");
563 assert(CompletionConsumer &&
564 "Compiler instance has no code completion consumer!");
565 return *CompletionConsumer;
579 assert(FrontendTimer &&
"Compiler instance has no frontend timer!");
580 return *FrontendTimer;
610 bool ShouldOwnClient =
true);
633 bool ShouldOwnClient =
true,
661 bool AllowPCHWithCompilerErrors,
void *DeserializationListener,
662 bool OwnDeserializationListener);
668 StringRef Path, StringRef Sysroot,
673 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
674 ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
675 void *DeserializationListener,
bool OwnDeserializationListener,
676 bool Preamble,
bool UseGlobalModuleIndex);
706 bool Binary =
true, StringRef BaseInput =
"", StringRef Extension =
"",
707 bool RemoveFileOnSignal =
true,
bool CreateMissingDirectories =
false,
708 bool ForceUseTemporary =
false);
714 std::unique_ptr<raw_pwrite_stream>
715 createOutputFile(StringRef OutputPath,
bool Binary,
bool RemoveFileOnSignal,
716 bool UseTemporary,
bool CreateMissingDirectories =
false);
737 createOutputFileImpl(StringRef OutputPath,
bool Binary,
738 bool RemoveFileOnSignal,
bool UseTemporary,
739 bool CreateMissingDirectories);
766 OutputStream = std::move(OutStream);
770 return std::move(OutputStream);
792 bool IsInclusionDirective);
797 bool IsInclusionDirective)
override;
800 StringRef Source)
override;
814 DependencyCollectors.push_back(std::move(Listener));
Defines the Diagnostic-related interfaces.
Defines the SourceManager interface.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
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...
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
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
~CompilerInstance() override
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 createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
FileManager * createFileManager(IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
Create the file manager and replace any existing one with it.
const HeaderSearchOptions & getHeaderSearchOpts() const
DependencyOutputOptions & getDependencyOutputOpts()
bool hasFileManager() const
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.
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)
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.
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
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 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.
std::unique_ptr< Sema > takeSema()
const PreprocessorOptions & getPreprocessorOpts() const
bool loadModuleFile(StringRef FileName)
void resetAndLeakSourceManager()
void setASTConsumer(std::unique_ptr< ASTConsumer > Value)
setASTConsumer - Replace the current AST consumer; the compiler instance takes ownership of Value.
PreprocessorOutputOptions & getPreprocessorOutputOpts()
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
IntrusiveRefCntPtr< ASTReader > getASTReader() const
void setTarget(TargetInfo *Value)
Replace the current Target.
void setModuleDepCollector(std::shared_ptr< ModuleDependencyCollector > Collector)
InMemoryModuleCache & getModuleCache() const
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...
bool hasASTContext() const
void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName, StringRef Source) override
Attempt to create the given module from the specified source buffer.
void resetAndLeakFileManager()
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr() const
void setASTContext(ASTContext *Value)
setASTContext - Replace the current AST context.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
ASTContext & getASTContext() const
void LoadRequestedPlugins()
Load the list of plugins requested in the FrontendOptions.
TargetOptions & getTargetOpts()
void setASTReader(IntrusiveRefCntPtr< ASTReader > Reader)
std::unique_ptr< llvm::raw_pwrite_stream > takeOutputStream()
bool hasInvocation() const
FrontendOptions & getFrontendOpts()
const FrontendOptions & getFrontendOpts() const
std::shared_ptr< ModuleDependencyCollector > getModuleDepCollector() const
bool hasDiagnostics() const
bool hadModuleLoaderFatalFailure() const
void setSema(Sema *S)
Replace the current Sema; the compiler instance takes ownership of S.
HeaderSearchOptions & getHeaderSearchOpts()
void createFrontendTimer()
Create the frontend timer and replace any existing one with it.
void setDiagnostics(DiagnosticsEngine *Value)
setDiagnostics - Replace the current diagnostics engine.
bool hasCodeCompletionConsumer() const
void resetAndLeakASTContext()
bool hasFrontendTimer() const
const LangOptions & getLangOpts() const
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.
void resetAndLeakPreprocessor()
AnalyzerOptionsRef getAnalyzerOpts()
std::unique_ptr< ASTConsumer > takeASTConsumer()
takeASTConsumer - Remove the current AST consumer and give ownership to the caller.
PreprocessorOptions & getPreprocessorOpts()
ASTConsumer & getASTConsumer() const
TargetInfo & getTarget() const
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()
LangOptions & getLangOpts()
CodeGenOptions & getCodeGenOpts()
SourceManager & getSourceManager() const
Return the current source manager.
CodeCompleteConsumer & getCodeCompletionConsumer() const
bool shouldBuildGlobalModuleIndex() const
Indicates whether we should (re)build the global module index.
bool hasSourceManager() const
bool hasASTConsumer() const
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 hasPreprocessor() const
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.
Implements support for file system lookup, file system caching, and directory search management.
Keeps track of options that affect how file operations are performed.
Abstract base class for actions which can be performed by the frontend.
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...
In-memory cache for modules.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Describes the result of attempting to load a module.
Abstract interface for a module loader.
Describes a module or submodule.
NameVisibilityKind
Describes the visibility of the various names within a particular module.
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.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Exposes information about the current target.
Options for controlling the target.
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.
YAML serialization mapping.