10 #ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
11 #define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
24 #include "llvm/ADT/StringRef.h"
40 class CodeCompleteConsumer;
41 class DiagnosticsEngine;
42 class DiagnosticConsumer;
43 class ExternalASTSource;
47 class MemoryBufferCache;
74 std::shared_ptr<CompilerInvocation> Invocation;
98 std::shared_ptr<Preprocessor> PP;
107 std::unique_ptr<ASTConsumer> Consumer;
110 std::unique_ptr<CodeCompleteConsumer> CompletionConsumer;
113 std::unique_ptr<Sema> TheSema;
116 std::unique_ptr<llvm::TimerGroup> FrontendTimerGroup;
119 std::unique_ptr<llvm::Timer> FrontendTimer;
125 std::shared_ptr<ModuleDependencyCollector> ModuleDepCollector;
128 std::shared_ptr<PCHContainerOperations> ThePCHContainerOperations;
131 std::unique_ptr<DependencyFileGenerator> TheDependencyFileGenerator;
133 std::vector<std::shared_ptr<DependencyCollector>> DependencyCollectors;
137 llvm::DenseMap<const IdentifierInfo *, Module *> KnownModules;
141 std::map<std::string, std::string> BuiltModules;
144 bool DeleteBuiltModules =
true;
156 bool BuildGlobalModuleIndex =
false;
159 bool HaveFullGlobalModuleIndex =
false;
162 bool ModuleBuildFailed =
false;
171 std::string TempFilename;
173 OutputFile(std::string filename, std::string tempFilename)
174 :
Filename(std::move(filename)), TempFilename(std::move(tempFilename)) {
181 std::unique_ptr<llvm::raw_fd_ostream> NonSeekStream;
184 std::list<OutputFile> OutputFiles;
190 std::shared_ptr<PCHContainerOperations> PCHContainerOps =
191 std::make_shared<PCHContainerOperations>(),
237 assert(Invocation &&
"Compiler instance has no invocation!");
250 BuildGlobalModuleIndex = Build;
258 return Invocation->getAnalyzerOpts();
262 return Invocation->getCodeGenOpts();
265 return Invocation->getCodeGenOpts();
269 return Invocation->getDependencyOutputOpts();
272 return Invocation->getDependencyOutputOpts();
276 return Invocation->getDiagnosticOpts();
279 return Invocation->getDiagnosticOpts();
283 return Invocation->getFileSystemOpts();
286 return Invocation->getFileSystemOpts();
290 return Invocation->getFrontendOpts();
293 return Invocation->getFrontendOpts();
297 return Invocation->getHeaderSearchOpts();
300 return Invocation->getHeaderSearchOpts();
303 return Invocation->getHeaderSearchOptsPtr();
307 return *Invocation->getLangOpts();
310 return *Invocation->getLangOpts();
314 return Invocation->getPreprocessorOpts();
317 return Invocation->getPreprocessorOpts();
321 return Invocation->getPreprocessorOutputOpts();
324 return Invocation->getPreprocessorOutputOpts();
328 return Invocation->getTargetOpts();
331 return Invocation->getTargetOpts();
342 assert(Diagnostics &&
"Compiler instance has no diagnostics!");
350 assert(Diagnostics && Diagnostics->getClient() &&
351 "Compiler instance has no diagnostic client!");
352 return *Diagnostics->getClient();
362 assert(Target &&
"Compiler instance has no target!");
386 "Compiler instance has no virtual file system");
387 return *VirtualFileSystem;
395 VirtualFileSystem = std::move(FS);
406 assert(FileMgr &&
"Compiler instance has no file manager!");
412 FileMgr.resetWithoutRelease();
426 assert(SourceMgr &&
"Compiler instance has no source manager!");
432 SourceMgr.resetWithoutRelease();
446 assert(PP &&
"Compiler instance has no preprocessor!");
453 BuryPointer(
new std::shared_ptr<Preprocessor>(PP));
466 assert(Context &&
"Compiler instance has no AST context!");
472 Context.resetWithoutRelease();
489 assert(Consumer &&
"Compiler instance has no AST consumer!");
504 bool hasSema()
const {
return (
bool)TheSema; }
507 assert(TheSema &&
"Compiler instance has no Sema object!");
523 std::shared_ptr<ModuleDependencyCollector> Collector);
526 return ThePCHContainerOperations;
532 assert(Invocation &&
"cannot determine module format without invocation");
534 auto *Writer = ThePCHContainerOperations->getWriterOrNull(Format);
537 Diagnostics->Report(diag::err_module_format_unhandled) << Format;
538 llvm::report_fatal_error(
"unknown module format");
546 assert(Invocation &&
"cannot determine module format without invocation");
548 auto *Reader = ThePCHContainerOperations->getReaderOrNull(Format);
551 Diagnostics->Report(diag::err_module_format_unhandled) << Format;
552 llvm::report_fatal_error(
"unknown module format");
564 assert(CompletionConsumer &&
565 "Compiler instance has no code completion consumer!");
566 return *CompletionConsumer;
580 assert(FrontendTimer &&
"Compiler instance has no frontend timer!");
581 return *FrontendTimer;
616 bool ShouldOwnClient =
true);
639 bool ShouldOwnClient =
true,
660 bool AllowPCHWithCompilerErrors,
661 void *DeserializationListener,
662 bool OwnDeserializationListener);
668 StringRef Path, StringRef Sysroot,
bool DisablePCHValidation,
671 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
673 ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
674 void *DeserializationListener,
bool OwnDeserializationListener,
675 bool Preamble,
bool UseGlobalModuleIndex);
703 std::unique_ptr<raw_pwrite_stream>
705 StringRef Extension =
"");
711 std::unique_ptr<raw_pwrite_stream>
712 createOutputFile(StringRef OutputPath,
bool Binary,
bool RemoveFileOnSignal,
713 StringRef BaseInput, StringRef Extension,
bool UseTemporary,
714 bool CreateMissingDirectories =
false);
741 std::unique_ptr<raw_pwrite_stream>
743 bool RemoveFileOnSignal, StringRef BaseInput,
744 StringRef Extension,
bool UseTemporary,
745 bool CreateMissingDirectories, std::string *ResultPathName,
746 std::string *TempPathName);
781 bool IsInclusionDirective)
override;
784 StringRef Source)
override;
798 DependencyCollectors.push_back(std::move(Listener));
LangOptions & getLangOpts()
void resetAndLeakFileManager()
bool hasFileManager() const
ASTContext & getASTContext() const
CompilerInvocation & getInvocation()
PreprocessorOptions & getPreprocessorOpts()
void createCodeCompletionConsumer()
Create a code completion consumer using the invocation; note that this will cause the source manager ...
bool hasASTConsumer() const
void createFrontendTimer()
Create the frontend timer and replace any existing one with it.
Implements support for file system lookup, file system caching, and directory search management...
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
void setCodeCompletionConsumer(CodeCompleteConsumer *Value)
setCodeCompletionConsumer - Replace the current code completion consumer; the compiler instance takes...
vfs::FileSystem & getVirtualFileSystem() const
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
Defines the SourceManager interface.
void createSema(TranslationUnitKind TUKind, CodeCompleteConsumer *CompletionConsumer)
Create the Sema object to be used for parsing.
Abstract base class for actions which can be performed by the frontend.
DiagnosticOptions & getDiagnosticOpts()
bool hasCodeCompletionConsumer() const
bool InitializeSourceManager(const FrontendInputFile &Input)
InitializeSourceManager - Initialize the source manager to set InputFile as the main file...
void setAuxTarget(TargetInfo *Value)
Replace the current AuxTarget.
bool hasInvocation() const
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
void setModuleManager(IntrusiveRefCntPtr< ASTReader > Reader)
TargetInfo & getTarget() const
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
SourceManager & getSourceManager() const
Return the current source manager.
Options for controlling the target.
Manage memory buffers across multiple users.
const DependencyOutputOptions & getDependencyOutputOpts() const
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
DependencyOutputOptions & getDependencyOutputOpts()
const HeaderSearchOptions & getHeaderSearchOpts() const
void setExternalSemaSource(IntrusiveRefCntPtr< ExternalSemaSource > ESS)
The virtual file system interface.
void loadModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName, StringRef Source) override
Attempt to load the given module from the specified source buffer.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
void createFileManager()
Create the file manager and replace any existing one with it.
void createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
void setASTContext(ASTContext *Value)
setASTContext - Replace the current AST context.
CodeGenOptions & getCodeGenOpts()
std::shared_ptr< Preprocessor > getPreprocessorPtr()
const PreprocessorOptions & getPreprocessorOpts() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
ASTConsumer & getASTConsumer() const
Describes a module or submodule.
void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, SourceLocation ImportLoc) override
Make the given module visible.
void setBuildGlobalModuleIndex(bool Build)
Set the flag indicating whether we should (re)build the global module index.
This abstract interface provides operations for creating containers for serialized ASTs (precompiled ...
FrontendOptions & getFrontendOpts()
Visibility
Describes the different kinds of visibility that a declaration may have.
DiagnosticConsumer & getDiagnosticClient() const
PreprocessorOutputOptions & getPreprocessorOutputOpts()
Concrete class used by the front-end to report problems and issues.
Defines the Diagnostic-related interfaces.
void setASTConsumer(std::unique_ptr< ASTConsumer > Value)
setASTConsumer - Replace the current AST consumer; the compiler instance takes ownership of Value...
IntrusiveRefCntPtr< ASTReader > getModuleManager() const
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
HeaderSearchOptions & getHeaderSearchOpts()
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
Preprocessor & getPreprocessor() const
Return the current preprocessor.
Builds a depdenency file when attached to a Preprocessor (for includes) and ASTReader (for module imp...
AnalyzerOptionsRef getAnalyzerOpts()
void createPCHExternalASTSource(StringRef Path, bool DisablePCHValidation, bool AllowPCHWithCompilerErrors, void *DeserializationListener, bool OwnDeserializationListener)
Create an external AST source to read a PCH file and attach it to the AST context.
bool hasVirtualFileSystem() const
Sema - This implements semantic analysis and AST building for C.
void setVirtualFileSystem(IntrusiveRefCntPtr< vfs::FileSystem > FS)
Replace the current virtual file system.
Describes the result of attempting to load a module.
void resetAndLeakPreprocessor()
PreprocessorOutputOptions - Options for controlling the C preprocessor output (e.g., -E).
Exposes information about the current target.
void addDependencyCollector(std::shared_ptr< DependencyCollector > Listener)
bool hadModuleLoaderFatalFailure() const
std::string getSpecificModuleCachePath()
bool hasDiagnostics() const
void createASTContext()
Create the AST context.
const FrontendOptions & getFrontendOpts() const
void setSema(Sema *S)
Replace the current Sema; the compiler instance takes ownership of S.
MemoryBufferCache & getPCMCache() const
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
void addOutputFile(OutputFile &&OutFile)
addOutputFile - Add an output file onto the list of tracked output files.
std::unique_ptr< raw_pwrite_stream > createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, StringRef BaseInput, StringRef Extension, bool UseTemporary, bool CreateMissingDirectories=false)
Create a new output file and add it to the list of tracked output files, optionally deriving the outp...
bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Check global module index for missing imports.
std::unique_ptr< Sema > takeSema()
bool hasSourceManager() const
FileSystemOptions & getFileSystemOpts()
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object...
const TargetOptions & getTargetOpts() const
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
void resetAndLeakSourceManager()
const PreprocessorOutputOptions & getPreprocessorOutputOpts() const
Options for controlling the compiler diagnostics engine.
std::unique_ptr< raw_pwrite_stream > createNullOutputFile()
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
void createPreprocessor(TranslationUnitKind TUKind)
Create the preprocessor, using the invocation, file, and source managers, and replace any existing on...
A global index for a set of module files, providing information about the identifiers within those mo...
TargetInfo * getAuxTarget() const
Abstract interface for a consumer of code-completion information.
Options controlling the behavior of code completion.
bool hasFrontendTimer() const
void setPreprocessor(std::shared_ptr< Preprocessor > Value)
Replace the current preprocessor.
const LangOptions & getLangOpts() const
const DiagnosticOptions & getDiagnosticOpts() const
FileManager & getFileManager() const
Return the current file manager to the caller.
bool loadModuleFile(StringRef FileName)
std::shared_ptr< HeaderSearchOptions > getHeaderSearchOptsPtr() const
Helper class for holding the data necessary to invoke the compiler.
FrontendOptions - Options for controlling the behavior of the frontend.
Abstract interface for a module loader.
void clearOutputFiles(bool EraseFiles)
clearOutputFiles - Clear the output file list.
void BuryPointer(const void *Ptr)
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
CodeCompleteConsumer & getCodeCompletionConsumer() const
bool hasASTContext() const
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="")
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
void setTarget(TargetInfo *Value)
Replace the current Target.
Keeps track of options that affect how file operations are performed.
const PCHContainerWriter & getPCHContainerWriter() const
Return the appropriate PCHContainerWriter depending on the current CodeGenOptions.
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
~CompilerInstance() override
bool shouldBuildGlobalModuleIndex() const
Indicates whether we should (re)build the global module index.
void resetAndLeakASTContext()
llvm::Timer & getFrontendTimer() const
std::shared_ptr< ModuleDependencyCollector > getModuleDepCollector() const
TranslationUnitKind
Describes the kind of translation unit being processed.
const FileSystemOptions & getFileSystemOpts() const
NameVisibilityKind
Describes the visibility of the various names within a particular module.
const CodeGenOptions & getCodeGenOpts() const
bool hasPreprocessor() const
std::unique_ptr< ASTConsumer > takeASTConsumer()
takeASTConsumer - Remove the current AST consumer and give ownership to the caller.
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective) override
Attempt to load the given module.
void createModuleManager()
}
void setModuleDepCollector(std::shared_ptr< ModuleDependencyCollector > Collector)
GlobalModuleIndex * loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Load, create, or return global module.
TargetOptions & getTargetOpts()
This class handles loading and caching of source files into memory.
void setDiagnostics(DiagnosticsEngine *Value)
setDiagnostics - Replace the current diagnostics engine.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.