14 #ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
15 #define LLVM_CLANG_FRONTEND_ASTUNIT_H
29 #include "llvm/ADT/IntrusiveRefCntPtr.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringMap.h"
32 #include "llvm/Support/MD5.h"
36 #include <sys/types.h>
48 class CompilerInvocation;
49 class CompilerInstance;
51 class DiagnosticsEngine;
56 class MemoryBufferCache;
58 class PreprocessorOptions;
59 class PCHContainerOperations;
60 class PCHContainerReader;
63 class ASTDeserializationListener;
86 std::vector<std::pair<unsigned, unsigned> >
Ranges;
91 std::shared_ptr<LangOptions> LangOpts;
96 std::unique_ptr<HeaderSearch> HeaderInfo;
98 std::shared_ptr<Preprocessor> PP;
100 std::shared_ptr<TargetOptions> TargetOpts;
101 std::shared_ptr<HeaderSearchOptions> HSOpts;
102 std::shared_ptr<PreprocessorOptions> PPOpts;
104 bool HadModuleLoaderFatalFailure;
107 std::unique_ptr<ASTWriterData> WriterData;
113 std::unique_ptr<ASTConsumer> Consumer;
117 std::unique_ptr<Sema> TheSema;
121 std::shared_ptr<CompilerInvocation> Invocation;
132 bool CaptureDiagnostics;
144 bool OwnsRemappedFileBuffers;
153 std::vector<Decl*> TopLevelDecls;
157 typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy;
161 FileDeclsTy FileDecls;
164 std::string OriginalSourceFile;
182 unsigned NumStoredDiagnosticsFromDriver;
193 unsigned PreambleRebuildCounter;
201 llvm::StringMap<SourceLocation> PreambleSrcLocCache;
210 std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
218 unsigned NumWarningsInPreamble;
222 std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
225 bool ShouldCacheCodeCompletionResults : 1;
229 bool IncludeBriefCommentsInCodeCompletion : 1;
233 bool UserFilesAreVolatile : 1;
236 ASTUnit &AST,
bool CaptureDiagnostics);
238 void TranslateStoredDiagnostics(
FileManager &FileMgr,
243 void clearFileLevelDecls();
287 return CachedCompletionTypes;
291 std::shared_ptr<GlobalCodeCompletionAllocator>
293 return CachedCompletionAllocator;
298 CCTUInfo = llvm::make_unique<CodeCompletionTUInfo>(
299 std::make_shared<GlobalCodeCompletionAllocator>());
305 std::shared_ptr<GlobalCodeCompletionAllocator> CachedCompletionAllocator;
307 std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
310 std::vector<CachedCodeCompletionResult> CachedCompletionResults;
314 llvm::StringMap<unsigned> CachedCompletionTypes;
321 unsigned CompletionCacheTopLevelHashValue;
328 unsigned PreambleTopLevelHashValue;
332 unsigned CurrentTopLevelHashValue;
336 unsigned UnsafeToFree : 1;
340 void CacheCodeCompletionResults();
343 void ClearCachedCompletionResults();
346 void operator=(
const ASTUnit &) =
delete;
348 explicit ASTUnit(
bool MainFileIsAST);
350 bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
351 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
354 std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
355 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
358 unsigned MaxLines = 0);
359 void RealizeTopLevelDeclsFromPreamble();
372 class ConcurrencyState {
382 ConcurrencyState ConcurrencyCheckValue;
392 Self.ConcurrencyCheckValue.start();
395 Self.ConcurrencyCheckValue.finish();
423 bool hasSema()
const {
return (
bool)TheSema; }
425 assert(TheSema &&
"ASTUnit does not have a Sema object!");
430 assert(LangOpts &&
"ASTUnit does not have language options");
435 assert(HSOpts &&
"ASTUnit does not have header search options");
440 assert(PPOpts &&
"ASTUnit does not have preprocessor options");
452 return OriginalSourceFile;
471 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
472 if (!TopLevelDeclsInPreamble.empty())
473 RealizeTopLevelDeclsFromPreamble();
474 return TopLevelDecls.begin();
478 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
479 if (!TopLevelDeclsInPreamble.empty())
480 RealizeTopLevelDeclsFromPreamble();
481 return TopLevelDecls.end();
485 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
486 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
490 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
491 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
496 TopLevelDecls.push_back(D);
519 unsigned Line,
unsigned Col)
const;
555 return StoredDiagnostics.begin();
558 return StoredDiagnostics.begin();
561 return StoredDiagnostics.end();
564 return StoredDiagnostics.end();
569 if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
570 NumStoredDiagnosticsFromDriver = 0;
571 return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
574 typedef std::vector<CachedCodeCompletionResult>::iterator
578 return CachedCompletionResults.begin();
582 return CachedCompletionResults.end();
586 return CachedCompletionResults.size();
592 llvm::iterator_range<PreprocessingRecord::iterator>
612 std::unique_ptr<llvm::MemoryBuffer>
626 static std::unique_ptr<ASTUnit>
627 create(std::shared_ptr<CompilerInvocation> CI,
629 bool UserFilesAreVolatile);
655 bool CaptureDiagnostics =
false,
bool AllowPCHWithCompilerErrors =
false,
656 bool UserFilesAreVolatile =
false);
673 bool LoadFromCompilerInvocation(
674 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
675 unsigned PrecompilePreambleAfterNParses,
709 std::shared_ptr<CompilerInvocation> CI,
710 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
713 bool Persistent =
true, StringRef ResourceFilesPath = StringRef(),
714 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
715 unsigned PrecompilePreambleAfterNParses = 0,
716 bool CacheCodeCompletionResults =
false,
717 bool IncludeBriefCommentsInCodeCompletion =
false,
718 bool UserFilesAreVolatile =
false,
719 std::unique_ptr<ASTUnit> *ErrAST =
nullptr);
735 static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
736 std::shared_ptr<CompilerInvocation> CI,
737 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
739 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
740 unsigned PrecompilePreambleAfterNParses = 0,
742 bool CacheCodeCompletionResults =
false,
743 bool IncludeBriefCommentsInCodeCompletion =
false,
744 bool UserFilesAreVolatile =
false);
775 const char **ArgBegin,
const char **ArgEnd,
776 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
778 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
780 bool RemappedFilesKeepOriginalName =
true,
781 unsigned PrecompilePreambleAfterNParses = 0,
783 bool CacheCodeCompletionResults =
false,
784 bool IncludeBriefCommentsInCodeCompletion =
false,
785 bool AllowPCHWithCompilerErrors =
false,
bool SkipFunctionBodies =
false,
786 bool SingleFileParse =
false,
787 bool UserFilesAreVolatile =
false,
bool ForSerialization =
false,
789 std::unique_ptr<ASTUnit> *ErrAST =
nullptr,
803 bool Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
834 bool IncludeCodePatterns,
bool IncludeBriefComments,
836 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
846 bool Save(StringRef File);
stored_diag_iterator stored_diag_afterDriver_begin()
Defines the clang::ASTContext interface.
CXAvailabilityKind
Describes the availability of a particular entity, which indicates whether the use of this entity wil...
SourceLocation getEnd() const
StringRef getMainFileName() const
void ResetForParse()
Free data that will be re-generated on the next parse.
llvm::iterator_range< PreprocessingRecord::iterator > getLocalPreprocessingEntities() const
Returns an iterator range for the local preprocessing entities of the local Preprocessor, if this is a parsed source file, or the loaded preprocessing entities of the primary module if this is an AST file.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
bool isInMainFileID(SourceLocation Loc)
Implements support for file system lookup, file system caching, and directory search management...
const PreprocessorOptions & getPreprocessorOpts() const
std::pair< unsigned, unsigned > InsertFromRange
void addTopLevelDecl(Decl *D)
Add a new top-level declaration.
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
const DiagnosticsEngine & getDiagnostics() const
CXAvailabilityKind Availability
The availability of this code-completion result.
Defines the SourceManager interface.
Load everything, including Sema.
Abstract base class for actions which can be performed by the frontend.
Decl - This represents one declaration (or definition), e.g.
SourceLocation getEndOfPreambleFileID()
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
FileManager & getFileManager()
A module loader that doesn't know how to load modules.
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
static std::unique_ptr< ASTUnit > LoadFromASTFile(const std::string &Filename, const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, const FileSystemOptions &FileSystemOpts, bool UseDebugInfo=false, bool OnlyLocalDecls=false, ArrayRef< RemappedFile > RemappedFiles=None, bool CaptureDiagnostics=false, bool AllowPCHWithCompilerErrors=false, bool UserFilesAreVolatile=false)
Create a ASTUnit from an AST file.
bool(* DeclVisitorFn)(void *context, const Decl *D)
Type for a function iterating over a number of declarations.
bool getOnlyLocalDecls() const
bool isModuleFile()
Returns true if the ASTUnit was constructed from a serialized module file.
static std::unique_ptr< ASTUnit > create(std::shared_ptr< CompilerInvocation > CI, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, bool CaptureDiagnostics, bool UserFilesAreVolatile)
Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
std::vector< std::pair< unsigned, unsigned > > Ranges
std::shared_ptr< Preprocessor > getPreprocessorPtr() const
cached_completion_iterator cached_completion_end()
unsigned Type
The type of a non-macro completion result, stored as a unique integer used by the string map of cache...
CXCursorKind Kind
The libclang cursor kind corresponding to this code-completion result.
stored_diag_const_iterator stored_diag_begin() const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Utility class for loading a ASTContext from an AST file.
A "string" used to describe how code completion can be performed for an entity.
Preprocessor & getPreprocessor()
unsigned stored_diag_size() const
std::unique_ptr< llvm::MemoryBuffer > getBufferForFile(StringRef Filename, std::string *ErrorStr=nullptr)
bool BeforePreviousInsertions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
const SourceManager & getSourceManager() const
SourceRange mapRangeFromPreamble(SourceRange R)
DiagnosticsEngine::Level Level
Load the AST, but do not restore Sema state.
static ASTUnit * LoadFromCommandLine(const char **ArgBegin, const char **ArgEnd, std::shared_ptr< PCHContainerOperations > PCHContainerOps, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, StringRef ResourceFilesPath, bool OnlyLocalDecls=false, bool CaptureDiagnostics=false, ArrayRef< RemappedFile > RemappedFiles=None, bool RemappedFilesKeepOriginalName=true, unsigned PrecompilePreambleAfterNParses=0, TranslationUnitKind TUKind=TU_Complete, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool AllowPCHWithCompilerErrors=false, bool SkipFunctionBodies=false, bool SingleFileParse=false, bool UserFilesAreVolatile=false, bool ForSerialization=false, llvm::Optional< StringRef > ModuleFormat=llvm::None, std::unique_ptr< ASTUnit > *ErrAST=nullptr, IntrusiveRefCntPtr< vfs::FileSystem > VFS=nullptr)
LoadFromCommandLine - Create an ASTUnit from a vector of command line arguments, which must specify e...
void setPreprocessor(std::shared_ptr< Preprocessor > pp)
uint64_t ShowInContexts
A bitmask that indicates which code-completion contexts should contain this completion result...
const FileEntry * getPCHFile()
Get the PCH file if one was included.
Load options and the preprocessor state.
bool serialize(raw_ostream &OS)
Serialize this translation unit with the given output stream.
Concrete class used by the front-end to report problems and issues.
bool getOwnsRemappedFileBuffers() const
void addFileLevelDecl(Decl *D)
Add a new local file-level declaration.
ASTDeserializationListener * getDeserializationListener()
ConcurrencyCheck(ASTUnit &Self)
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
CXCursorKind
Describes the kind of entity that a cursor refers to.
void setASTContext(ASTContext *ctx)
const LangOptions & getLangOpts() const
Sema - This implements semantic analysis and AST building for C.
StringRef getASTFileName() const
If this ASTUnit came from an AST file, returns the filename for it.
const FileManager & getFileManager() const
std::shared_ptr< GlobalCodeCompletionAllocator > getCachedCompletionAllocator()
Retrieve the allocator used to cache global code completions.
void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length, SmallVectorImpl< Decl * > &Decls)
Get the decls that are contained in a file in the Offset/Length range.
CodeCompletionTUInfo & getCodeCompletionTUInfo()
Defines the clang::LangOptions interface.
std::pair< unsigned, unsigned > RemoveRange
unsigned & getCurrentTopLevelHashValue()
Retrieve a reference to the current top-level name hash value.
std::vector< StandaloneFixIt > FixIts
StringRef getOriginalSourceFileName()
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
stored_diag_const_iterator stored_diag_end() const
void setOwnsRemappedFileBuffers(bool val)
top_level_iterator top_level_begin()
const StoredDiagnostic * stored_diag_const_iterator
bool Reparse(std::shared_ptr< PCHContainerOperations > PCHContainerOps, ArrayRef< RemappedFile > RemappedFiles=None, IntrusiveRefCntPtr< vfs::FileSystem > VFS=nullptr)
Reparse the source files using the same command-line options that were originally used to produce thi...
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
std::pair< std::string, llvm::MemoryBuffer * > RemappedFile
A mapping from a file name to the memory buffer that stores the remapped contents of that file...
llvm::StringMap< unsigned > & getCachedCompletionTypes()
Retrieve the mapping from formatted type names to unique type identifiers.
Cached information about one file (either on disk or in the virtual file system). ...
void setUnsafeToFree(bool Value)
InputKind getInputKind() const
Determine the input kind this AST unit represents.
Defines the clang::TargetOptions class.
unsigned cached_completion_size() const
SourceRange mapRangeToPreamble(SourceRange R)
SourceLocation getBegin() const
const FileSystemOptions & getFileSystemOpts() const
bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn)
Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declara...
StoredDiagnostic * stored_diag_iterator
const ASTContext & getASTContext() const
bool isInPreambleFileID(SourceLocation Loc)
static ASTUnit * LoadFromCompilerInvocationAction(std::shared_ptr< CompilerInvocation > CI, std::shared_ptr< PCHContainerOperations > PCHContainerOps, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, FrontendAction *Action=nullptr, ASTUnit *Unit=nullptr, bool Persistent=true, StringRef ResourceFilesPath=StringRef(), bool OnlyLocalDecls=false, bool CaptureDiagnostics=false, unsigned PrecompilePreambleAfterNParses=0, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool UserFilesAreVolatile=false, std::unique_ptr< ASTUnit > *ErrAST=nullptr)
Create an ASTUnit from a source file, via a CompilerInvocation object, by invoking the optionally pro...
Abstract interface for a consumer of code-completion information.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
CodeCompletionString * Completion
The code-completion string corresponding to this completion result.
const Preprocessor & getPreprocessor() const
const HeaderSearchOptions & getHeaderSearchOpts() const
IntrusiveRefCntPtr< ASTReader > getASTReader() const
std::vector< Decl * >::iterator top_level_iterator
ASTMutationListener * getASTMutationListener()
ASTContext & getASTContext()
Helper class for holding the data necessary to invoke the compiler.
top_level_iterator top_level_end()
Abstract interface for a module loader.
bool isMainFileAST() const
SourceLocation getStartOfMainFileID()
SourceLocation mapLocationFromPreamble(SourceLocation Loc)
If Loc is a loaded location from the preamble, returns the corresponding local location of the main f...
bool Save(StringRef File)
Save this translation unit to a file with the given name.
TranslationUnitKind getTranslationUnitKind() const
Determine what kind of translation unit this AST represents.
cached_completion_iterator cached_completion_begin()
Defines the clang::FileSystemOptions interface.
SourceManager & getSourceManager()
Keeps track of options that affect how file operations are performed.
DiagnosticsEngine & getDiagnostics()
bool isUnsafeToFree() const
SourceLocation getLocation(const FileEntry *File, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
SimplifiedTypeClass TypeClass
The simplified type class for a non-macro completion result.
stored_diag_iterator stored_diag_end()
Level
The level of the diagnostic, after it has been through mapping.
std::size_t top_level_size() const
A cached code-completion result, which may be introduced in one of many different contexts...
std::vector< CachedCodeCompletionResult >::iterator cached_completion_iterator
TranslationUnitKind
Describes the kind of translation unit being processed.
The translation unit is a complete translation unit.
SourceLocation mapLocationToPreamble(SourceLocation Loc)
If Loc is a local location of the main file but inside the preamble chunk, returns the corresponding ...
SimplifiedTypeClass
A simplified classification of types used when determining "similar" types for code completion...
unsigned Priority
The priority given to this code-completion result.
bool top_level_empty() const
A trivial tuple used to represent a source range.
void CodeComplete(StringRef File, unsigned Line, unsigned Column, ArrayRef< RemappedFile > RemappedFiles, bool IncludeMacros, bool IncludeCodePatterns, bool IncludeBriefComments, CodeCompleteConsumer &Consumer, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, FileManager &FileMgr, SmallVectorImpl< StoredDiagnostic > &StoredDiagnostics, SmallVectorImpl< const llvm::MemoryBuffer * > &OwnedBuffers)
Perform code completion at the given file, line, and column within this translation unit...
stored_diag_iterator stored_diag_begin()
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.