14 #ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H 15 #define LLVM_CLANG_FRONTEND_ASTUNIT_H 32 #include "llvm/ADT/ArrayRef.h" 33 #include "llvm/ADT/DenseMap.h" 34 #include "llvm/ADT/IntrusiveRefCntPtr.h" 35 #include "llvm/ADT/None.h" 36 #include "llvm/ADT/Optional.h" 37 #include "llvm/ADT/STLExtras.h" 38 #include "llvm/ADT/SmallVector.h" 39 #include "llvm/ADT/StringMap.h" 40 #include "llvm/ADT/StringRef.h" 41 #include "llvm/ADT/iterator_range.h" 64 class ASTDeserializationListener;
65 class ASTMutationListener;
67 class CompilerInstance;
68 class CompilerInvocation;
75 class MemoryBufferCache;
76 class PCHContainerOperations;
77 class PCHContainerReader;
79 class PreprocessorOptions;
102 std::vector<std::pair<unsigned, unsigned>>
Ranges;
107 std::shared_ptr<LangOptions> LangOpts;
112 std::unique_ptr<HeaderSearch> HeaderInfo;
114 std::shared_ptr<Preprocessor> PP;
116 std::shared_ptr<TargetOptions> TargetOpts;
117 std::shared_ptr<HeaderSearchOptions> HSOpts;
118 std::shared_ptr<PreprocessorOptions> PPOpts;
120 bool HadModuleLoaderFatalFailure =
false;
123 std::unique_ptr<ASTWriterData> WriterData;
129 std::unique_ptr<ASTConsumer> Consumer;
133 std::unique_ptr<Sema> TheSema;
137 std::shared_ptr<CompilerInvocation> Invocation;
145 bool OnlyLocalDecls =
false;
148 bool CaptureDiagnostics =
false;
160 bool OwnsRemappedFileBuffers =
true;
169 std::vector<Decl*> TopLevelDecls;
173 using FileDeclsTy = llvm::DenseMap<FileID, LocDeclsTy *>;
177 FileDeclsTy FileDecls;
180 std::string OriginalSourceFile;
198 unsigned NumStoredDiagnosticsFromDriver = 0;
209 unsigned PreambleRebuildCounter = 0;
217 llvm::StringMap<SourceLocation> PreambleSrcLocCache;
225 std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
233 unsigned NumWarningsInPreamble = 0;
237 std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
240 bool ShouldCacheCodeCompletionResults : 1;
244 bool IncludeBriefCommentsInCodeCompletion : 1;
248 bool UserFilesAreVolatile : 1;
251 ASTUnit &AST,
bool CaptureDiagnostics);
253 void TranslateStoredDiagnostics(
FileManager &FileMgr,
258 void clearFileLevelDecls();
302 return CachedCompletionTypes;
306 std::shared_ptr<GlobalCodeCompletionAllocator>
308 return CachedCompletionAllocator;
313 CCTUInfo = llvm::make_unique<CodeCompletionTUInfo>(
314 std::make_shared<GlobalCodeCompletionAllocator>());
320 std::shared_ptr<GlobalCodeCompletionAllocator> CachedCompletionAllocator;
322 std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
325 std::vector<CachedCodeCompletionResult> CachedCompletionResults;
329 llvm::StringMap<unsigned> CachedCompletionTypes;
336 unsigned CompletionCacheTopLevelHashValue = 0;
343 unsigned PreambleTopLevelHashValue = 0;
347 unsigned CurrentTopLevelHashValue = 0;
351 unsigned UnsafeToFree : 1;
358 void CacheCodeCompletionResults();
361 void ClearCachedCompletionResults();
363 explicit ASTUnit(
bool MainFileIsAST);
365 bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
366 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
369 std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
370 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
373 unsigned MaxLines = 0);
374 void RealizeTopLevelDeclsFromPreamble();
387 class ConcurrencyState {
397 ConcurrencyState ConcurrencyCheckValue;
407 Self.ConcurrencyCheckValue.start();
411 Self.ConcurrencyCheckValue.finish();
438 void setPreprocessor(std::shared_ptr<Preprocessor> pp);
447 void enableSourceFileDiagnostics();
449 bool hasSema()
const {
return (
bool)TheSema; }
452 assert(TheSema &&
"ASTUnit does not have a Sema object!");
457 assert(LangOpts &&
"ASTUnit does not have language options");
462 assert(HSOpts &&
"ASTUnit does not have header search options");
467 assert(PPOpts &&
"ASTUnit does not have preprocessor options");
479 return OriginalSourceFile;
490 StringRef getMainFileName()
const;
493 StringRef getASTFileName()
const;
498 assert(!isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
499 if (!TopLevelDeclsInPreamble.empty())
500 RealizeTopLevelDeclsFromPreamble();
501 return TopLevelDecls.begin();
505 assert(!isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
506 if (!TopLevelDeclsInPreamble.empty())
507 RealizeTopLevelDeclsFromPreamble();
508 return TopLevelDecls.end();
512 assert(!isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
513 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
517 assert(!isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
518 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
523 TopLevelDecls.push_back(D);
527 void addFileLevelDecl(
Decl *D);
532 void findFileRegionDecls(
FileID File,
unsigned Offset,
unsigned Length,
546 unsigned Line,
unsigned Col)
const;
569 mapLocationFromPreamble(R.
getEnd()));
575 mapLocationToPreamble(R.
getEnd()));
583 return StoredDiagnostics.begin();
587 return StoredDiagnostics.begin();
591 return StoredDiagnostics.end();
595 return StoredDiagnostics.end();
601 if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
602 NumStoredDiagnosticsFromDriver = 0;
603 return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
607 std::vector<CachedCodeCompletionResult>::iterator;
610 return CachedCompletionResults.begin();
614 return CachedCompletionResults.end();
618 return CachedCompletionResults.size();
624 llvm::iterator_range<PreprocessingRecord::iterator>
625 getLocalPreprocessingEntities()
const;
635 bool visitLocalTopLevelDecls(
void *context,
DeclVisitorFn Fn);
642 bool isModuleFile()
const;
644 std::unique_ptr<llvm::MemoryBuffer>
645 getBufferForFile(StringRef
Filename, std::string *ErrorStr =
nullptr);
658 static std::unique_ptr<ASTUnit>
659 create(std::shared_ptr<CompilerInvocation> CI,
661 bool UserFilesAreVolatile);
684 static std::unique_ptr<ASTUnit> LoadFromASTFile(
689 bool CaptureDiagnostics =
false,
bool AllowPCHWithCompilerErrors =
false,
690 bool UserFilesAreVolatile =
false);
707 bool LoadFromCompilerInvocation(
708 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
709 unsigned PrecompilePreambleAfterNParses,
741 static ASTUnit *LoadFromCompilerInvocationAction(
742 std::shared_ptr<CompilerInvocation> CI,
743 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
746 bool Persistent =
true, StringRef ResourceFilesPath = StringRef(),
747 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
748 unsigned PrecompilePreambleAfterNParses = 0,
749 bool CacheCodeCompletionResults =
false,
750 bool IncludeBriefCommentsInCodeCompletion =
false,
751 bool UserFilesAreVolatile =
false,
752 std::unique_ptr<ASTUnit> *ErrAST =
nullptr);
768 static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
769 std::shared_ptr<CompilerInvocation> CI,
770 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
772 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
773 unsigned PrecompilePreambleAfterNParses = 0,
775 bool CacheCodeCompletionResults =
false,
776 bool IncludeBriefCommentsInCodeCompletion =
false,
777 bool UserFilesAreVolatile =
false);
808 static ASTUnit *LoadFromCommandLine(
809 const char **ArgBegin,
const char **ArgEnd,
810 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
812 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
814 bool RemappedFilesKeepOriginalName =
true,
815 unsigned PrecompilePreambleAfterNParses = 0,
817 bool CacheCodeCompletionResults =
false,
818 bool IncludeBriefCommentsInCodeCompletion =
false,
819 bool AllowPCHWithCompilerErrors =
false,
822 bool SingleFileParse =
false,
bool UserFilesAreVolatile =
false,
823 bool ForSerialization =
false,
825 std::unique_ptr<ASTUnit> *ErrAST =
nullptr,
839 bool Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
846 void ResetForParse();
868 void CodeComplete(StringRef File,
unsigned Line,
unsigned Column,
870 bool IncludeCodePatterns,
bool IncludeBriefComments,
872 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
882 bool Save(StringRef File);
887 bool serialize(raw_ostream &OS);
892 #endif // LLVM_CLANG_FRONTEND_ASTUNIT_H 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.
SkipFunctionBodiesScope
Enumerates the available scopes for skipping function bodies.
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...
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Load options and the preprocessor state.
Concrete class used by the front-end to report problems and issues.
Defines the Diagnostic-related interfaces.
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()
bool getOnlyLocalDecls() const
bool top_level_empty() 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...
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
StringRef getOriginalSourceFileName() const
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). ...
std::vector< CachedCodeCompletionResult >::iterator cached_completion_iterator
void setUnsafeToFree(bool Value)
Defines the clang::TargetOptions class.
constexpr XRayInstrMask None
TranslationUnitKind getTranslationUnitKind() const
Determine what kind of translation unit this AST represents.
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.
bool(*)(void *context, const Decl *D) DeclVisitorFn
Type for a function iterating over a number of declarations.
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()
Defines the clang::SourceLocation class and associated facilities.
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...
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.
std::vector< Decl * >::iterator top_level_iterator