Go to the documentation of this file.
13 #ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
14 #define LLVM_CLANG_FRONTEND_ASTUNIT_H
31 #include "llvm/ADT/ArrayRef.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/IntrusiveRefCntPtr.h"
34 #include "llvm/ADT/None.h"
35 #include "llvm/ADT/Optional.h"
36 #include "llvm/ADT/STLExtras.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/StringMap.h"
39 #include "llvm/ADT/StringRef.h"
40 #include "llvm/ADT/iterator_range.h"
63 class ASTDeserializationListener;
64 class ASTMutationListener;
66 class CompilerInstance;
67 class CompilerInvocation;
74 class InMemoryModuleCache;
75 class PCHContainerOperations;
76 class PCHContainerReader;
78 class PreprocessorOptions;
104 std::vector<std::pair<unsigned, unsigned>>
Ranges;
109 std::shared_ptr<LangOptions> LangOpts;
114 std::unique_ptr<HeaderSearch> HeaderInfo;
116 std::shared_ptr<Preprocessor> PP;
118 std::shared_ptr<TargetOptions> TargetOpts;
119 std::shared_ptr<HeaderSearchOptions> HSOpts;
120 std::shared_ptr<PreprocessorOptions> PPOpts;
122 bool HadModuleLoaderFatalFailure =
false;
125 std::unique_ptr<ASTWriterData> WriterData;
131 std::unique_ptr<ASTConsumer> Consumer;
135 std::unique_ptr<Sema> TheSema;
139 std::shared_ptr<CompilerInvocation> Invocation;
147 bool OnlyLocalDecls =
false;
162 bool OwnsRemappedFileBuffers =
true;
171 std::vector<Decl*> TopLevelDecls;
175 using FileDeclsTy = llvm::DenseMap<FileID, std::unique_ptr<LocDeclsTy>>;
179 FileDeclsTy FileDecls;
200 unsigned NumStoredDiagnosticsFromDriver = 0;
211 unsigned PreambleRebuildCountdown = 0;
214 unsigned PreambleCounter = 0;
222 llvm::StringMap<SourceLocation> PreambleSrcLocCache;
230 std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
238 unsigned NumWarningsInPreamble = 0;
242 std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
245 bool ShouldCacheCodeCompletionResults : 1;
249 bool IncludeBriefCommentsInCodeCompletion : 1;
253 bool UserFilesAreVolatile : 1;
258 void TranslateStoredDiagnostics(
FileManager &FileMgr,
263 void clearFileLevelDecls();
307 return CachedCompletionTypes;
311 std::shared_ptr<GlobalCodeCompletionAllocator>
313 return CachedCompletionAllocator;
318 CCTUInfo = std::make_unique<CodeCompletionTUInfo>(
319 std::make_shared<GlobalCodeCompletionAllocator>());
325 std::shared_ptr<GlobalCodeCompletionAllocator> CachedCompletionAllocator;
327 std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
330 std::vector<CachedCodeCompletionResult> CachedCompletionResults;
334 llvm::StringMap<unsigned> CachedCompletionTypes;
341 unsigned CompletionCacheTopLevelHashValue = 0;
348 unsigned PreambleTopLevelHashValue = 0;
352 unsigned CurrentTopLevelHashValue = 0;
356 unsigned UnsafeToFree : 1;
363 void CacheCodeCompletionResults();
366 void ClearCachedCompletionResults();
368 explicit ASTUnit(
bool MainFileIsAST);
370 bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
371 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
374 std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
375 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
378 unsigned MaxLines = 0);
379 void RealizeTopLevelDeclsFromPreamble();
392 class ConcurrencyState {
402 ConcurrencyState ConcurrencyCheckValue;
412 Self.ConcurrencyCheckValue.start();
416 Self.ConcurrencyCheckValue.finish();
454 bool hasSema()
const {
return (
bool)TheSema; }
457 assert(TheSema &&
"ASTUnit does not have a Sema object!");
462 assert(LangOpts &&
"ASTUnit does not have language options");
467 assert(HSOpts &&
"ASTUnit does not have header search options");
472 assert(PPOpts &&
"ASTUnit does not have preprocessor options");
484 return OriginalSourceFile;
503 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
504 if (!TopLevelDeclsInPreamble.empty())
505 RealizeTopLevelDeclsFromPreamble();
506 return TopLevelDecls.begin();
510 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
511 if (!TopLevelDeclsInPreamble.empty())
512 RealizeTopLevelDeclsFromPreamble();
513 return TopLevelDecls.end();
517 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
518 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
522 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
523 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
528 TopLevelDecls.push_back(D);
551 unsigned Line,
unsigned Col)
const;
590 return StoredDiagnostics.begin();
594 return StoredDiagnostics.begin();
598 return StoredDiagnostics.end();
602 return StoredDiagnostics.end();
608 if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
609 NumStoredDiagnosticsFromDriver = 0;
610 return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
614 std::vector<CachedCodeCompletionResult>::iterator;
617 return CachedCompletionResults.begin();
621 return CachedCompletionResults.end();
625 return CachedCompletionResults.size();
631 llvm::iterator_range<PreprocessingRecord::iterator>
651 std::unique_ptr<llvm::MemoryBuffer>
665 static std::unique_ptr<ASTUnit>
666 create(std::shared_ptr<CompilerInvocation> CI,
691 static std::unique_ptr<ASTUnit>
696 bool UseDebugInfo =
false,
bool OnlyLocalDecls =
false,
698 bool AllowASTWithCompilerErrors =
false,
699 bool UserFilesAreVolatile =
false);
716 bool LoadFromCompilerInvocation(
717 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
718 unsigned PrecompilePreambleAfterNParses,
751 std::shared_ptr<CompilerInvocation> CI,
752 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
755 bool Persistent =
true, StringRef ResourceFilesPath = StringRef(),
756 bool OnlyLocalDecls =
false,
758 unsigned PrecompilePreambleAfterNParses = 0,
759 bool CacheCodeCompletionResults =
false,
760 bool UserFilesAreVolatile =
false,
761 std::unique_ptr<ASTUnit> *ErrAST =
nullptr);
777 static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
778 std::shared_ptr<CompilerInvocation> CI,
779 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
781 bool OnlyLocalDecls =
false,
783 unsigned PrecompilePreambleAfterNParses = 0,
785 bool CacheCodeCompletionResults =
false,
786 bool IncludeBriefCommentsInCodeCompletion =
false,
787 bool UserFilesAreVolatile =
false);
819 const char **ArgBegin,
const char **ArgEnd,
820 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
822 bool OnlyLocalDecls =
false,
825 bool RemappedFilesKeepOriginalName =
true,
826 unsigned PrecompilePreambleAfterNParses = 0,
828 bool CacheCodeCompletionResults =
false,
829 bool IncludeBriefCommentsInCodeCompletion =
false,
830 bool AllowPCHWithCompilerErrors =
false,
833 bool SingleFileParse =
false,
bool UserFilesAreVolatile =
false,
834 bool ForSerialization =
false,
835 bool RetainExcludedConditionalBlocks =
false,
837 std::unique_ptr<ASTUnit> *ErrAST =
nullptr,
851 bool Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
882 bool IncludeCodePatterns,
bool IncludeBriefComments,
884 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
894 bool Save(StringRef File);
904 #endif // LLVM_CLANG_FRONTEND_ASTUNIT_H
top_level_iterator top_level_begin()
IntrusiveRefCntPtr< ASTReader > getASTReader() const
Level
The level of the diagnostic, after it has been through mapping.
bool top_level_empty() const
Utility class for loading a ASTContext from an AST file.
SourceLocation mapLocationFromPreamble(SourceLocation Loc) const
If Loc is a loaded location from the preamble, returns the corresponding local location of the main f...
YAML serialization mapping.
CXAvailabilityKind Availability
The availability of this code-completion result.
stored_diag_const_iterator stored_diag_end() const
bool isUnsafeToFree() const
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, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, unsigned PrecompilePreambleAfterNParses=0, bool CacheCodeCompletionResults=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...
A trivial tuple used to represent a source range.
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
void setOwnsRemappedFileBuffers(bool val)
std::vector< CachedCodeCompletionResult >::iterator cached_completion_iterator
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 getOnlyLocalDecls() const
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.
SourceLocation getBegin() const
bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn)
Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declara...
StringRef getOriginalSourceFileName() const
@ LoadASTOnly
Load the AST, but do not restore Sema state.
bool isInMainFileID(SourceLocation Loc) const
ASTUnit & operator=(const ASTUnit &)=delete
llvm::StringMap< unsigned > & getCachedCompletionTypes()
Retrieve the mapping from formatted type names to unique type identifiers.
Concrete class used by the front-end to report problems and issues.
void setUnsafeToFree(bool Value)
SkipFunctionBodiesScope
Enumerates the available scopes for skipping function bodies.
bool(*)(void *context, const Decl *D) DeclVisitorFn
Type for a function iterating over a number of declarations.
ASTMutationListener * getASTMutationListener()
void enableSourceFileDiagnostics()
Enable source-range based diagnostic messages.
std::vector< std::pair< unsigned, unsigned > > Ranges
TranslationUnitKind getTranslationUnitKind() const
Determine what kind of translation unit this AST represents.
CXCursorKind Kind
The libclang cursor kind corresponding to this code-completion result.
unsigned Type
The type of a non-macro completion result, stored as a unique integer used by the string map of cache...
bool getOwnsRemappedFileBuffers() const
bool Save(StringRef File)
Save this translation unit to a file with the given name.
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
SourceLocation getEndOfPreambleFileID() const
This class handles loading and caching of source files into memory.
SourceManager & getSourceManager()
Keeps track of options that affect how file operations are performed.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
const ASTContext & getASTContext() const
llvm::iterator_range< PreprocessingRecord::iterator > getLocalPreprocessingEntities() const
Returns an iterator range for the local preprocessing entities of the local Preprocessor,...
ASTContext & getASTContext()
SourceRange mapRangeFromPreamble(SourceRange R) const
DiagnosticsEngine::Level Level
const HeaderSearchOptions & getHeaderSearchOpts() const
top_level_iterator top_level_end()
const SourceManager & getSourceManager() const
uint64_t ShowInContexts
A bitmask that indicates which code-completion contexts should contain this completion result.
bool Reparse(std::shared_ptr< PCHContainerOperations > PCHContainerOps, ArrayRef< RemappedFile > RemappedFiles=None, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
Reparse the source files using the same command-line options that were originally used to produce thi...
cached_completion_iterator cached_completion_begin()
unsigned cached_completion_size() const
SourceLocation getStartOfMainFileID() const
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, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, bool AllowASTWithCompilerErrors=false, bool UserFilesAreVolatile=false)
Create a ASTUnit from an AST file.
std::shared_ptr< Preprocessor > getPreprocessorPtr() const
SourceLocation getEnd() const
std::pair< unsigned, unsigned > RemoveRange
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
DiagnosticsEngine & getDiagnostics()
const DiagnosticsEngine & getDiagnostics() const
InputKind getInputKind() const
Determine the input kind this AST unit represents.
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
stored_diag_iterator stored_diag_begin()
stored_diag_iterator stored_diag_end()
std::vector< StandaloneFixIt > FixIts
stored_diag_iterator stored_diag_afterDriver_begin()
SourceRange mapRangeToPreamble(SourceRange R) const
void ResetForParse()
Free data that will be re-generated on the next parse.
bool BeforePreviousInsertions
const LangOptions & getLangOpts() const
const Preprocessor & getPreprocessor() const
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.
const AnnotatedLine * Line
unsigned getPreambleCounterForTests() const
SourceLocation getLocation(const FileEntry *File, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
const PreprocessorOptions & getPreprocessorOpts() const
Cached information about one file (either on disk or in the virtual file system).
A "string" used to describe how code completion can be performed for an entity.
std::vector< Decl * >::iterator top_level_iterator
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
StringRef getASTFileName() const
If this ASTUnit came from an AST file, returns the filename for it.
void addTopLevelDecl(Decl *D)
Add a new top-level declaration.
CXAvailabilityKind
Describes the availability of a particular entity, which indicates whether the use of this entity wil...
Decl - This represents one declaration (or definition), e.g.
const FileManager & getFileManager() const
static std::unique_ptr< ASTUnit > create(std::shared_ptr< CompilerInvocation > CI, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, CaptureDiagsKind CaptureDiagnostics, bool UserFilesAreVolatile)
Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
@ LoadPreprocessorOnly
Load options and the preprocessor state.
CXCursorKind
Describes the kind of entity that a cursor refers to.
FileManager & getFileManager()
Sema - This implements semantic analysis and AST building for C.
Abstract interface for a consumer of code-completion information.
CodeCompletionString * Completion
The code-completion string corresponding to this completion result.
std::unique_ptr< llvm::MemoryBuffer > getBufferForFile(StringRef Filename, std::string *ErrorStr=nullptr)
Preprocessor & getPreprocessor()
Abstract interface for a module loader.
unsigned stored_diag_size() const
SimplifiedTypeClass
A simplified classification of types used when determining "similar" types for code completion.
SourceLocation mapLocationToPreamble(SourceLocation Loc) const
If Loc is a local location of the main file but inside the preamble chunk, returns the corresponding ...
stored_diag_const_iterator stored_diag_begin() const
const FileSystemOptions & getFileSystemOpts() const
A module loader that doesn't know how to create or load modules.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
ConcurrencyCheck(ASTUnit &Self)
TranslationUnitKind
Describes the kind of translation unit being processed.
cached_completion_iterator cached_completion_end()
Abstract base class for actions which can be performed by the frontend.
Implements support for file system lookup, file system caching, and directory search management.
bool serialize(raw_ostream &OS)
Serialize this translation unit with the given output stream.
void addFileLevelDecl(Decl *D)
Add a new local file-level declaration.
CaptureDiagsKind
Enumerates the available kinds for capturing diagnostics.
bool isInPreambleFileID(SourceLocation Loc) const
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Helper class for holding the data necessary to invoke the compiler.
StringRef getMainFileName() const
std::pair< unsigned, unsigned > InsertFromRange
@ LoadEverything
Load everything, including Sema.
SimplifiedTypeClass TypeClass
The simplified type class for a non-macro completion result.
@ AllWithoutNonErrorsFromIncludes
void setASTContext(ASTContext *ctx)
void setPreprocessor(std::shared_ptr< Preprocessor > pp)
@ TU_Complete
The translation unit is a complete translation unit.
const FileEntry * getPCHFile()
Get the PCH file if one was included.
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.
bool isMainFileAST() const
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
ASTDeserializationListener * getDeserializationListener()
unsigned & getCurrentTopLevelHashValue()
Retrieve a reference to the current top-level name hash value.
A cached code-completion result, which may be introduced in one of many different contexts.
unsigned Priority
The priority given to this code-completion result.
bool isModuleFile() const
Returns true if the ASTUnit was constructed from a serialized module file.
std::shared_ptr< GlobalCodeCompletionAllocator > getCachedCompletionAllocator()
Retrieve the allocator used to cache global code completions.
std::size_t top_level_size() const
CodeCompletionTUInfo & getCodeCompletionTUInfo()
static ASTUnit * LoadFromCommandLine(const char **ArgBegin, const char **ArgEnd, std::shared_ptr< PCHContainerOperations > PCHContainerOps, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, StringRef ResourceFilesPath, bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, ArrayRef< RemappedFile > RemappedFiles=None, bool RemappedFilesKeepOriginalName=true, unsigned PrecompilePreambleAfterNParses=0, TranslationUnitKind TUKind=TU_Complete, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool AllowPCHWithCompilerErrors=false, SkipFunctionBodiesScope SkipFunctionBodies=SkipFunctionBodiesScope::None, bool SingleFileParse=false, bool UserFilesAreVolatile=false, bool ForSerialization=false, bool RetainExcludedConditionalBlocks=false, llvm::Optional< StringRef > ModuleFormat=llvm::None, std::unique_ptr< ASTUnit > *ErrAST=nullptr, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
LoadFromCommandLine - Create an ASTUnit from a vector of command line arguments, which must specify e...