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();
421 void setPreprocessor(std::shared_ptr<Preprocessor> pp);
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;
463 StringRef getMainFileName()
const;
466 StringRef getASTFileName()
const;
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);
500 void addFileLevelDecl(
Decl *D);
505 void findFileRegionDecls(
FileID File,
unsigned Offset,
unsigned Length,
519 unsigned Line,
unsigned Col)
const;
542 mapLocationFromPreamble(R.
getEnd()));
548 mapLocationToPreamble(R.
getEnd()));
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>
593 getLocalPreprocessingEntities()
const;
597 typedef bool (*DeclVisitorFn)(
void *context,
const Decl *D);
603 bool visitLocalTopLevelDecls(
void *context, DeclVisitorFn Fn);
610 bool isModuleFile()
const;
612 std::unique_ptr<llvm::MemoryBuffer>
613 getBufferForFile(StringRef
Filename, std::string *ErrorStr =
nullptr);
626 static std::unique_ptr<ASTUnit>
627 create(std::shared_ptr<CompilerInvocation> CI,
629 bool UserFilesAreVolatile);
650 static std::unique_ptr<ASTUnit> LoadFromASTFile(
655 bool CaptureDiagnostics =
false,
bool AllowPCHWithCompilerErrors =
false,
656 bool UserFilesAreVolatile =
false);
673 bool LoadFromCompilerInvocation(
674 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
675 unsigned PrecompilePreambleAfterNParses,
708 static ASTUnit *LoadFromCompilerInvocationAction(
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);
774 static ASTUnit *LoadFromCommandLine(
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,
810 void ResetForParse();
832 void CodeComplete(StringRef File,
unsigned Line,
unsigned Column,
834 bool IncludeCodePatterns,
bool IncludeBriefComments,
836 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
846 bool Save(StringRef File);
851 bool serialize(raw_ostream &OS);
SourceRange mapRangeToPreamble(SourceRange R) const
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...
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.
Implements support for file system lookup, file system caching, and directory search management...
const LangOptions & getLangOpts() 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 Preprocessor & getPreprocessor() const
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
const PreprocessorOptions & getPreprocessorOpts() 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.
Decl - This represents one declaration (or definition), e.g.
const FileManager & getFileManager() const
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...
const FileSystemOptions & getFileSystemOpts() const
std::vector< std::pair< unsigned, unsigned > > Ranges
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.
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()
bool BeforePreviousInsertions
stored_diag_const_iterator stored_diag_begin() const
unsigned stored_diag_size() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
DiagnosticsEngine::Level Level
Load the AST, but do not restore Sema state.
uint64_t ShowInContexts
A bitmask that indicates which code-completion contexts should contain this completion result...
Load options and the preprocessor state.
Concrete class used by the front-end to report problems and issues.
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)
Sema - This implements semantic analysis and AST building for C.
std::size_t top_level_size() const
std::shared_ptr< GlobalCodeCompletionAllocator > getCachedCompletionAllocator()
Retrieve the allocator used to cache global code completions.
CodeCompletionTUInfo & getCodeCompletionTUInfo()
Defines the clang::LangOptions interface.
bool isMainFileAST() const
std::pair< unsigned, unsigned > RemoveRange
unsigned & getCurrentTopLevelHashValue()
Retrieve a reference to the current top-level name hash value.
const AnnotatedLine * Line
std::vector< StandaloneFixIt > FixIts
stored_diag_const_iterator stored_diag_end() const
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
SourceLocation getEnd() const
void setOwnsRemappedFileBuffers(bool val)
top_level_iterator top_level_begin()
const StoredDiagnostic * stored_diag_const_iterator
bool getOnlyLocalDecls() const
bool top_level_empty() const
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
StringRef getOriginalSourceFileName() const
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...
bool isUnsafeToFree() const
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.
TranslationUnitKind getTranslationUnitKind() const
Determine what kind of translation unit this AST represents.
StoredDiagnostic * stored_diag_iterator
bool getOwnsRemappedFileBuffers() const
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...
Dataflow Directional Tag Classes.
CodeCompletionString * Completion
The code-completion string corresponding to this completion result.
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
std::vector< Decl * >::iterator top_level_iterator
ASTContext & getASTContext()
Helper class for holding the data necessary to invoke the compiler.
const HeaderSearchOptions & getHeaderSearchOpts() const
SourceRange mapRangeFromPreamble(SourceRange R) const
top_level_iterator top_level_end()
Abstract interface for a module loader.
cached_completion_iterator cached_completion_begin()
unsigned cached_completion_size() const
Defines the clang::FileSystemOptions interface.
SourceManager & getSourceManager()
std::shared_ptr< Preprocessor > getPreprocessorPtr() const
const ASTContext & getASTContext() const
Keeps track of options that affect how file operations are performed.
DiagnosticsEngine & getDiagnostics()
const DiagnosticsEngine & getDiagnostics() const
const SourceManager & getSourceManager() const
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.
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.
SimplifiedTypeClass
A simplified classification of types used when determining "similar" types for code completion...
unsigned Priority
The priority given to this code-completion result.
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
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.