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"
33 #include "llvm/Support/Path.h"
38 #include <sys/types.h>
50 class CodeCompleteConsumer;
51 class CompilerInvocation;
52 class CompilerInstance;
54 class DiagnosticsEngine;
59 class PCHContainerOperations;
60 class PCHContainerReader;
64 class ASTDeserializationListener;
83 std::vector<std::pair<unsigned, unsigned> >
Ranges;
88 std::shared_ptr<LangOptions> LangOpts;
92 std::unique_ptr<HeaderSearch> HeaderInfo;
96 std::shared_ptr<TargetOptions> TargetOpts;
99 bool HadModuleLoaderFatalFailure;
102 std::unique_ptr<ASTWriterData> WriterData;
108 std::unique_ptr<ASTConsumer> Consumer;
112 std::unique_ptr<Sema> TheSema;
124 bool CaptureDiagnostics;
136 bool OwnsRemappedFileBuffers;
145 std::vector<Decl*> TopLevelDecls;
149 typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy;
153 FileDeclsTy FileDecls;
156 std::string OriginalSourceFile;
174 unsigned NumStoredDiagnosticsFromDriver;
185 unsigned PreambleRebuildCounter;
190 std::vector<char> Buffer;
191 mutable unsigned NumLines;
198 Buffer.assign(begin, end);
202 void clear() { Buffer.clear(); File =
nullptr; NumLines = 0; }
204 size_t size()
const {
return Buffer.size(); }
205 bool empty()
const {
return Buffer.empty(); }
222 void countLines()
const;
252 return !(LHS == RHS);
259 PreambleData Preamble;
265 bool PreambleEndsAtStartOfLine;
272 llvm::StringMap<PreambleFileHash> FilesInPreamble;
277 std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
282 std::unique_ptr<llvm::MemoryBuffer> PreambleBuffer;
290 unsigned NumWarningsInPreamble;
294 std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
297 bool ShouldCacheCodeCompletionResults : 1;
301 bool IncludeBriefCommentsInCodeCompletion : 1;
305 bool UserFilesAreVolatile : 1;
311 ASTUnit &AST,
bool CaptureDiagnostics);
313 void TranslateStoredDiagnostics(
FileManager &FileMgr,
318 void clearFileLevelDecls();
362 return CachedCompletionTypes;
368 return CachedCompletionAllocator;
381 CachedCompletionAllocator;
383 std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
386 std::vector<CachedCodeCompletionResult> CachedCompletionResults;
390 llvm::StringMap<unsigned> CachedCompletionTypes;
397 unsigned CompletionCacheTopLevelHashValue;
404 unsigned PreambleTopLevelHashValue;
408 unsigned CurrentTopLevelHashValue;
412 unsigned UnsafeToFree : 1;
416 void CacheCodeCompletionResults();
419 void ClearCachedCompletionResults();
422 void operator=(
const ASTUnit &) =
delete;
424 explicit ASTUnit(
bool MainFileIsAST);
426 void CleanTemporaryFiles();
427 bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
428 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer);
430 struct ComputedPreamble {
431 llvm::MemoryBuffer *
Buffer;
432 std::unique_ptr<llvm::MemoryBuffer> Owner;
434 bool PreambleEndsAtStartOfLine;
435 ComputedPreamble(llvm::MemoryBuffer *
Buffer,
436 std::unique_ptr<llvm::MemoryBuffer> Owner,
unsigned Size,
437 bool PreambleEndsAtStartOfLine)
438 : Buffer(Buffer), Owner(std::move(Owner)), Size(Size),
439 PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {}
440 ComputedPreamble(ComputedPreamble &&C)
441 :
Buffer(C.
Buffer), Owner(std::move(C.Owner)), Size(C.Size),
442 PreambleEndsAtStartOfLine(C.PreambleEndsAtStartOfLine) {}
444 ComputedPreamble ComputePreamble(CompilerInvocation &Invocation,
447 std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
448 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
449 const CompilerInvocation &PreambleInvocationIn,
bool AllowRebuild =
true,
450 unsigned MaxLines = 0);
451 void RealizeTopLevelDeclsFromPreamble();
455 void transferASTDataFromCompilerInstance(CompilerInstance &CI);
464 class ConcurrencyState {
474 ConcurrencyState ConcurrencyCheckValue;
484 Self.ConcurrencyCheckValue.start();
487 Self.ConcurrencyCheckValue.finish();
514 bool hasSema()
const {
return (
bool)TheSema; }
516 assert(TheSema &&
"ASTUnit does not have a Sema object!");
521 assert(LangOpts &&
" ASTUnit does not have language options");
531 return OriginalSourceFile;
555 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
556 if (!TopLevelDeclsInPreamble.empty())
557 RealizeTopLevelDeclsFromPreamble();
558 return TopLevelDecls.begin();
562 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
563 if (!TopLevelDeclsInPreamble.empty())
564 RealizeTopLevelDeclsFromPreamble();
565 return TopLevelDecls.end();
569 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
570 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
574 assert(!
isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
575 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
580 TopLevelDecls.push_back(D);
595 TopLevelDeclsInPreamble.push_back(D);
609 unsigned Line,
unsigned Col)
const;
645 return StoredDiagnostics.begin();
648 return StoredDiagnostics.begin();
651 return StoredDiagnostics.end();
654 return StoredDiagnostics.end();
659 if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
660 NumStoredDiagnosticsFromDriver = 0;
661 return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
668 return CachedCompletionResults.begin();
672 return CachedCompletionResults.end();
676 return CachedCompletionResults.size();
682 llvm::iterator_range<PreprocessingRecord::iterator>
702 std::unique_ptr<llvm::MemoryBuffer>
715 bool CaptureDiagnostics,
716 bool UserFilesAreVolatile);
733 bool CaptureDiagnostics =
false,
bool AllowPCHWithCompilerErrors =
false,
734 bool UserFilesAreVolatile =
false);
746 bool LoadFromCompilerInvocation(
747 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
748 unsigned PrecompilePreambleAfterNParses);
782 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
785 bool Persistent =
true, StringRef ResourceFilesPath = StringRef(),
786 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
787 unsigned PrecompilePreambleAfterNParses = 0,
788 bool CacheCodeCompletionResults =
false,
789 bool IncludeBriefCommentsInCodeCompletion =
false,
790 bool UserFilesAreVolatile =
false,
791 std::unique_ptr<ASTUnit> *ErrAST =
nullptr);
807 static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
809 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
811 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
812 unsigned PrecompilePreambleAfterNParses = 0,
814 bool CacheCodeCompletionResults =
false,
815 bool IncludeBriefCommentsInCodeCompletion =
false,
816 bool UserFilesAreVolatile =
false);
842 const char **ArgBegin,
const char **ArgEnd,
843 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
845 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
847 bool RemappedFilesKeepOriginalName =
true,
848 unsigned PrecompilePreambleAfterNParses = 0,
850 bool CacheCodeCompletionResults =
false,
851 bool IncludeBriefCommentsInCodeCompletion =
false,
852 bool AllowPCHWithCompilerErrors =
false,
bool SkipFunctionBodies =
false,
853 bool UserFilesAreVolatile =
false,
bool ForSerialization =
false,
855 std::unique_ptr<ASTUnit> *ErrAST =
nullptr);
862 bool Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
887 bool IncludeCodePatterns,
bool IncludeBriefComments,
889 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
899 bool Save(StringRef File);
908 bool IsInclusionDirective)
override {
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
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...
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.
Defines the clang::FileManager interface and associated types.
IntrusiveRefCntPtr< GlobalCodeCompletionAllocator > getCachedCompletionAllocator()
Retrieve the allocator used to cache global code completions.
const DiagnosticsEngine & getDiagnostics() const
CXAvailabilityKind Availability
The availability of this code-completion result.
Defines the SourceManager interface.
Abstract base class for actions which can be performed by the frontend.
SourceLocation getEndOfPreambleFileID()
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
unsigned getNumLines() const
FileManager & getFileManager()
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
std::unique_ptr< llvm::MemoryBuffer > Buffer
Allocator for a cached set of global code completions.
void assign(const FileEntry *F, const char *begin, const char *end)
bool(* DeclVisitorFn)(void *context, const Decl *D)
Type for a function iterating over a number of declarations.
bool getOnlyLocalDecls() const
Data used to determine if a file used in the preamble has been changed.
void addTopLevelDeclFromPreamble(serialization::DeclID D)
Add a new top-level declaration, identified by its ID in the precompiled preamble.
bool isModuleFile()
Returns true if the ASTUnit was constructed from a serialized module file.
bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Check global module index for missing imports.
static PreambleFileHash createForMemoryBuffer(const llvm::MemoryBuffer *Buffer)
static ASTUnit * create(CompilerInvocation *CI, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, bool CaptureDiagnostics, bool UserFilesAreVolatile)
Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
friend bool operator!=(const PreambleFileHash &LHS, const PreambleFileHash &RHS)
std::vector< std::pair< unsigned, unsigned > > Ranges
void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, SourceLocation ImportLoc) override
Make the given module visible.
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.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
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)
static ASTUnit * LoadFromCompilerInvocationAction(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...
bool BeforePreviousInsertions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Describes a module or submodule.
const SourceManager & getSourceManager() const
SourceRange mapRangeFromPreamble(SourceRange R)
DiagnosticsEngine::Level Level
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
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.
Visibility
Describes the different kinds of visibility that a declaration may have.
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.
Describes the result of attempting to load a module.
void setPreprocessor(Preprocessor *pp)
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective) override
Attempt to load the given module.
const FileManager & getFileManager() const
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.
GlobalModuleIndex * loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Load, create, or return global module.
std::pair< unsigned, unsigned > RemoveRange
off_t Size
All files have size set.
unsigned & getCurrentTopLevelHashValue()
Retrieve a reference to the current top-level name hash value.
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 UserFilesAreVolatile=false, bool ForSerialization=false, llvm::Optional< StringRef > ModuleFormat=llvm::None, std::unique_ptr< ASTUnit > *ErrAST=nullptr)
LoadFromCommandLine - Create an ASTUnit from a vector of command line arguments, which must specify e...
std::vector< StandaloneFixIt > FixIts
StringRef getOriginalSourceFileName()
static PreambleFileHash createForFile(off_t Size, time_t ModTime)
FileID getPreambleFileID() const
Get the file ID for the precompiled preamble if there is one.
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
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...
const TemplateArgument * iterator
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)
Defines the clang::TargetOptions class.
const PreambleData & getPreambleData() const
unsigned cached_completion_size() const
SourceRange mapRangeToPreamble(SourceRange R)
SourceLocation getBegin() const
const FileSystemOptions & getFileSystemOpts() const
A global index for a set of module files, providing information about the identifiers within those mo...
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)
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...
bool Reparse(std::shared_ptr< PCHContainerOperations > PCHContainerOps, ArrayRef< RemappedFile > RemappedFiles=None)
Reparse the source files using the same command-line options that were originally used to produce thi...
CodeCompletionString * Completion
The code-completion string corresponding to this completion result.
const Preprocessor & getPreprocessor() const
std::vector< Decl * >::iterator top_level_iterator
ASTMutationListener * getASTMutationListener()
ASTContext & getASTContext()
Helper class for holding the data necessary to invoke the compiler.
llvm::MD5::MD5Result MD5
Memory buffers have MD5 instead of modification time.
void addTemporaryFile(StringRef TempFile)
Add a temporary file that the ASTUnit depends on.
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()
time_t ModTime
Modification time is set for files that are on disk.
Defines the clang::FileSystemOptions interface.
SourceManager & getSourceManager()
SourceRange getSourceRange(const SourceManager &SM) const
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.
NameVisibilityKind
Describes the visibility of the various names within a particular module.
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 ...
const char * getBufferStart() const
SimplifiedTypeClass
A simplified classification of types used when determining "similar" types for code completion...
unsigned Priority
The priority given to this code-completion result.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
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...
friend bool operator==(const PreambleFileHash &LHS, const PreambleFileHash &RHS)
static std::unique_ptr< ASTUnit > LoadFromASTFile(const std::string &Filename, const PCHContainerReader &PCHContainerRdr, 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.
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.