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, LocDeclsTy *>;
179 FileDeclsTy FileDecls;
182 std::string OriginalSourceFile;
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();
443 void setPreprocessor(std::shared_ptr<Preprocessor> pp);
452 void enableSourceFileDiagnostics();
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;
495 StringRef getMainFileName()
const;
498 StringRef getASTFileName()
const;
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);
532 void addFileLevelDecl(
Decl *D);
537 void findFileRegionDecls(
FileID File,
unsigned Offset,
unsigned Length,
551 unsigned Line,
unsigned Col)
const;
574 mapLocationFromPreamble(R.
getEnd()));
580 mapLocationToPreamble(R.
getEnd()));
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>
632 getLocalPreprocessingEntities()
const;
642 bool visitLocalTopLevelDecls(
void *context,
DeclVisitorFn Fn);
649 bool isModuleFile()
const;
651 std::unique_ptr<llvm::MemoryBuffer>
652 getBufferForFile(StringRef
Filename, std::string *ErrorStr =
nullptr);
665 static std::unique_ptr<ASTUnit>
666 create(std::shared_ptr<CompilerInvocation> CI,
691 static std::unique_ptr<ASTUnit> LoadFromASTFile(
697 bool AllowPCHWithCompilerErrors =
false,
698 bool UserFilesAreVolatile =
false);
715 bool LoadFromCompilerInvocation(
716 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
717 unsigned PrecompilePreambleAfterNParses,
749 static ASTUnit *LoadFromCompilerInvocationAction(
750 std::shared_ptr<CompilerInvocation> CI,
751 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
754 bool Persistent =
true, StringRef ResourceFilesPath = StringRef(),
755 bool OnlyLocalDecls =
false,
757 unsigned PrecompilePreambleAfterNParses = 0,
758 bool CacheCodeCompletionResults =
false,
759 bool IncludeBriefCommentsInCodeCompletion =
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);
818 static ASTUnit *LoadFromCommandLine(
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,
858 void ResetForParse();
880 void CodeComplete(StringRef File,
unsigned Line,
unsigned Column,
882 bool IncludeCodePatterns,
bool IncludeBriefComments,
884 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
894 bool Save(StringRef File);
899 bool serialize(raw_ostream &OS);
904 #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.
const Preprocessor & getPreprocessor() const
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
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()
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
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.
CaptureDiagsKind
Enumerates the available kinds for capturing diagnostics.
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.
unsigned getPreambleCounterForTests() const
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