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" 59 class ASTDeserializationListener;
60 class ASTMutationListener;
62 class CompilerInstance;
63 class CompilerInvocation;
70 class MemoryBufferCache;
71 class PCHContainerOperations;
72 class PCHContainerReader;
74 class PreprocessorOptions;
103 std::vector<std::pair<unsigned, unsigned>>
Ranges;
108 std::shared_ptr<LangOptions> LangOpts;
113 std::unique_ptr<HeaderSearch> HeaderInfo;
115 std::shared_ptr<Preprocessor> PP;
117 std::shared_ptr<TargetOptions> TargetOpts;
118 std::shared_ptr<HeaderSearchOptions> HSOpts;
119 std::shared_ptr<PreprocessorOptions> PPOpts;
121 bool HadModuleLoaderFatalFailure =
false;
124 std::unique_ptr<ASTWriterData> WriterData;
130 std::unique_ptr<ASTConsumer> Consumer;
134 std::unique_ptr<Sema> TheSema;
138 std::shared_ptr<CompilerInvocation> Invocation;
146 bool OnlyLocalDecls =
false;
149 bool CaptureDiagnostics =
false;
161 bool OwnsRemappedFileBuffers =
true;
170 std::vector<Decl*> TopLevelDecls;
174 using FileDeclsTy = llvm::DenseMap<FileID, LocDeclsTy *>;
178 FileDeclsTy FileDecls;
181 std::string OriginalSourceFile;
199 unsigned NumStoredDiagnosticsFromDriver = 0;
210 unsigned PreambleRebuildCounter = 0;
218 llvm::StringMap<SourceLocation> PreambleSrcLocCache;
226 std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
234 unsigned NumWarningsInPreamble = 0;
238 std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
241 bool ShouldCacheCodeCompletionResults : 1;
245 bool IncludeBriefCommentsInCodeCompletion : 1;
249 bool UserFilesAreVolatile : 1;
252 ASTUnit &AST,
bool CaptureDiagnostics);
254 void TranslateStoredDiagnostics(
FileManager &FileMgr,
259 void clearFileLevelDecls();
303 return CachedCompletionTypes;
307 std::shared_ptr<GlobalCodeCompletionAllocator>
309 return CachedCompletionAllocator;
314 CCTUInfo = llvm::make_unique<CodeCompletionTUInfo>(
315 std::make_shared<GlobalCodeCompletionAllocator>());
321 std::shared_ptr<GlobalCodeCompletionAllocator> CachedCompletionAllocator;
323 std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
326 std::vector<CachedCodeCompletionResult> CachedCompletionResults;
330 llvm::StringMap<unsigned> CachedCompletionTypes;
337 unsigned CompletionCacheTopLevelHashValue = 0;
344 unsigned PreambleTopLevelHashValue = 0;
348 unsigned CurrentTopLevelHashValue = 0;
352 unsigned UnsafeToFree : 1;
359 void CacheCodeCompletionResults();
362 void ClearCachedCompletionResults();
364 explicit ASTUnit(
bool MainFileIsAST);
366 bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
367 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
370 std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
371 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
374 unsigned MaxLines = 0);
375 void RealizeTopLevelDeclsFromPreamble();
388 class ConcurrencyState {
398 ConcurrencyState ConcurrencyCheckValue;
408 Self.ConcurrencyCheckValue.start();
412 Self.ConcurrencyCheckValue.finish();
439 void setPreprocessor(std::shared_ptr<Preprocessor> pp);
448 void enableSourceFileDiagnostics();
450 bool hasSema()
const {
return (
bool)TheSema; }
453 assert(TheSema &&
"ASTUnit does not have a Sema object!");
458 assert(LangOpts &&
"ASTUnit does not have language options");
463 assert(HSOpts &&
"ASTUnit does not have header search options");
468 assert(PPOpts &&
"ASTUnit does not have preprocessor options");
480 return OriginalSourceFile;
491 StringRef getMainFileName()
const;
494 StringRef getASTFileName()
const;
499 assert(!isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
500 if (!TopLevelDeclsInPreamble.empty())
501 RealizeTopLevelDeclsFromPreamble();
502 return TopLevelDecls.begin();
506 assert(!isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
507 if (!TopLevelDeclsInPreamble.empty())
508 RealizeTopLevelDeclsFromPreamble();
509 return TopLevelDecls.end();
513 assert(!isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
514 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
518 assert(!isMainFileAST() &&
"Invalid call for AST based ASTUnit!");
519 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
524 TopLevelDecls.push_back(D);
528 void addFileLevelDecl(
Decl *D);
533 void findFileRegionDecls(
FileID File,
unsigned Offset,
unsigned Length,
547 unsigned Line,
unsigned Col)
const;
570 mapLocationFromPreamble(R.
getEnd()));
576 mapLocationToPreamble(R.
getEnd()));
584 return StoredDiagnostics.begin();
588 return StoredDiagnostics.begin();
592 return StoredDiagnostics.end();
596 return StoredDiagnostics.end();
602 if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
603 NumStoredDiagnosticsFromDriver = 0;
604 return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
608 std::vector<CachedCodeCompletionResult>::iterator;
611 return CachedCompletionResults.begin();
615 return CachedCompletionResults.end();
619 return CachedCompletionResults.size();
625 llvm::iterator_range<PreprocessingRecord::iterator>
626 getLocalPreprocessingEntities()
const;
636 bool visitLocalTopLevelDecls(
void *context,
DeclVisitorFn Fn);
643 bool isModuleFile()
const;
645 std::unique_ptr<llvm::MemoryBuffer>
646 getBufferForFile(StringRef
Filename, std::string *ErrorStr =
nullptr);
659 static std::unique_ptr<ASTUnit>
660 create(std::shared_ptr<CompilerInvocation> CI,
662 bool UserFilesAreVolatile);
685 static std::unique_ptr<ASTUnit> LoadFromASTFile(
690 bool CaptureDiagnostics =
false,
bool AllowPCHWithCompilerErrors =
false,
691 bool UserFilesAreVolatile =
false);
708 bool LoadFromCompilerInvocation(
709 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
710 unsigned PrecompilePreambleAfterNParses,
742 static ASTUnit *LoadFromCompilerInvocationAction(
743 std::shared_ptr<CompilerInvocation> CI,
744 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
747 bool Persistent =
true, StringRef ResourceFilesPath = StringRef(),
748 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
749 unsigned PrecompilePreambleAfterNParses = 0,
750 bool CacheCodeCompletionResults =
false,
751 bool IncludeBriefCommentsInCodeCompletion =
false,
752 bool UserFilesAreVolatile =
false,
753 std::unique_ptr<ASTUnit> *ErrAST =
nullptr);
769 static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
770 std::shared_ptr<CompilerInvocation> CI,
771 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
773 bool OnlyLocalDecls =
false,
bool CaptureDiagnostics =
false,
774 unsigned PrecompilePreambleAfterNParses = 0,
776 bool CacheCodeCompletionResults =
false,
777 bool IncludeBriefCommentsInCodeCompletion =
false,
778 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,
821 SkipFunctionBodiesScope::None,
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.
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