clang 22.0.0git
clang::ASTUnit Class Reference

Utility class for loading a ASTContext from an AST file. More...

#include "clang/Frontend/ASTUnit.h"

Classes

struct  ASTWriterData
struct  CachedCodeCompletionResult
 A cached code-completion result, which may be introduced in one of many different contexts. More...
class  ConcurrencyCheck
struct  StandaloneDiagnostic
struct  StandaloneFixIt

Public Types

enum  WhatToLoad { LoadPreprocessorOnly , LoadASTOnly , LoadEverything }
using top_level_iterator = std::vector<Decl *>::iterator
using stored_diag_iterator = StoredDiagnostic *
using stored_diag_const_iterator = const StoredDiagnostic *
using diags_range = llvm::iterator_range<stored_diag_iterator>
using const_diags_range = llvm::iterator_range<stored_diag_const_iterator>
using cached_completion_iterator
using DeclVisitorFn = bool (*)(void *context, const Decl *D)
 Type for a function iterating over a number of declarations.
using RemappedFile = std::pair<std::string, llvm::MemoryBuffer *>
 A mapping from a file name to the memory buffer that stores the remapped contents of that file.

Public Member Functions

llvm::StringMap< unsigned > & getCachedCompletionTypes ()
 Retrieve the mapping from formatted type names to unique type identifiers.
std::shared_ptr< GlobalCodeCompletionAllocatorgetCachedCompletionAllocator ()
 Retrieve the allocator used to cache global code completions.
CodeCompletionTUInfogetCodeCompletionTUInfo ()
 ASTUnit (const ASTUnit &)=delete
ASTUnitoperator= (const ASTUnit &)=delete
 ~ASTUnit ()
bool isMainFileAST () const
bool isUnsafeToFree () const
void setUnsafeToFree (bool Value)
const DiagnosticsEnginegetDiagnostics () const
DiagnosticsEnginegetDiagnostics ()
llvm::IntrusiveRefCntPtr< DiagnosticsEnginegetDiagnosticsPtr ()
const SourceManagergetSourceManager () const
SourceManagergetSourceManager ()
llvm::IntrusiveRefCntPtr< SourceManagergetSourceManagerPtr ()
const PreprocessorgetPreprocessor () const
PreprocessorgetPreprocessor ()
std::shared_ptr< PreprocessorgetPreprocessorPtr () const
const ASTContextgetASTContext () const
ASTContextgetASTContext ()
llvm::IntrusiveRefCntPtr< ASTContextgetASTContextPtr ()
void setASTContext (llvm::IntrusiveRefCntPtr< ASTContext > ctx)
void setPreprocessor (std::shared_ptr< Preprocessor > pp)
void enableSourceFileDiagnostics ()
 Enable source-range based diagnostic messages.
bool hasSema () const
SemagetSema () const
const LangOptionsgetLangOpts () const
const HeaderSearchOptionsgetHeaderSearchOpts () const
const PreprocessorOptionsgetPreprocessorOpts () const
const FileManagergetFileManager () const
FileManagergetFileManager ()
IntrusiveRefCntPtr< FileManagergetFileManagerPtr ()
const FileSystemOptionsgetFileSystemOpts () const
IntrusiveRefCntPtr< ASTReadergetASTReader () const
StringRef getOriginalSourceFileName () const
ASTMutationListenergetASTMutationListener ()
ASTDeserializationListenergetDeserializationListener ()
bool getOnlyLocalDecls () const
bool getOwnsRemappedFileBuffers () const
void setOwnsRemappedFileBuffers (bool val)
StringRef getMainFileName () const
StringRef getASTFileName () const
 If this ASTUnit came from an AST file, returns the filename for it.
top_level_iterator top_level_begin ()
top_level_iterator top_level_end ()
std::size_t top_level_size () const
bool top_level_empty () const
void addTopLevelDecl (Decl *D)
 Add a new top-level declaration.
void addFileLevelDecl (Decl *D)
 Add a new local file-level declaration.
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.
unsignedgetCurrentTopLevelHashValue ()
 Retrieve a reference to the current top-level name hash value.
SourceLocation getLocation (const FileEntry *File, unsigned Line, unsigned Col) const
 Get the source location for the given file:line:col triplet.
SourceLocation getLocation (const FileEntry *File, unsigned Offset) const
 Get the source location for the given file:offset pair.
SourceLocation mapLocationFromPreamble (SourceLocation Loc) const
 If Loc is a loaded location from the preamble, returns the corresponding local location of the main file, otherwise it returns Loc.
SourceLocation mapLocationToPreamble (SourceLocation Loc) const
 If Loc is a local location of the main file but inside the preamble chunk, returns the corresponding loaded location from the preamble, otherwise it returns Loc.
bool isInPreambleFileID (SourceLocation Loc) const
bool isInMainFileID (SourceLocation Loc) const
SourceLocation getStartOfMainFileID () const
SourceLocation getEndOfPreambleFileID () const
SourceRange mapRangeFromPreamble (SourceRange R) const
SourceRange mapRangeToPreamble (SourceRange R) const
unsigned getPreambleCounterForTests () const
stored_diag_const_iterator stored_diag_begin () const
stored_diag_iterator stored_diag_begin ()
stored_diag_const_iterator stored_diag_end () const
stored_diag_iterator stored_diag_end ()
diags_range storedDiagnostics ()
const_diags_range storedDiagnostics () const
unsigned stored_diag_size () const
stored_diag_iterator stored_diag_afterDriver_begin ()
cached_completion_iterator cached_completion_begin ()
cached_completion_iterator cached_completion_end ()
unsigned cached_completion_size () const
llvm::iterator_range< PreprocessingRecord::iteratorgetLocalPreprocessingEntities () 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.
bool visitLocalTopLevelDecls (void *context, DeclVisitorFn Fn)
 Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declarations of the primary module if this is an AST file).
OptionalFileEntryRef getPCHFile ()
 Get the PCH file if one was included.
bool isModuleFile () const
 Returns true if the ASTUnit was constructed from a serialized module file.
std::unique_ptr< llvm::MemoryBuffer > getBufferForFile (StringRef Filename, std::string *ErrorStr=nullptr)
TranslationUnitKind getTranslationUnitKind () const
 Determine what kind of translation unit this AST represents.
InputKind getInputKind () const
 Determine the input kind this AST unit represents.
bool Reparse (std::shared_ptr< PCHContainerOperations > PCHContainerOps, ArrayRef< RemappedFile > RemappedFiles={}, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
 Reparse the source files using the same command-line options that were originally used to produce this translation unit.
void ResetForParse ()
 Free data that will be re-generated on the next parse.
void CodeComplete (StringRef File, unsigned Line, unsigned Column, ArrayRef< RemappedFile > RemappedFiles, bool IncludeMacros, bool IncludeCodePatterns, bool IncludeBriefComments, CodeCompleteConsumer &Consumer, std::shared_ptr< PCHContainerOperations > PCHContainerOps, llvm::IntrusiveRefCntPtr< DiagnosticsEngine > Diag, LangOptions &LangOpts, llvm::IntrusiveRefCntPtr< SourceManager > SourceMgr, llvm::IntrusiveRefCntPtr< FileManager > FileMgr, SmallVectorImpl< StoredDiagnostic > &StoredDiagnostics, SmallVectorImpl< const llvm::MemoryBuffer * > &OwnedBuffers, std::unique_ptr< SyntaxOnlyAction > Act)
 Perform code completion at the given file, line, and column within this translation unit.
bool Save (StringRef File)
 Save this translation unit to a file with the given name.
bool serialize (raw_ostream &OS)
 Serialize this translation unit with the given output stream.

Static Public Member Functions

static std::unique_ptr< ASTUnitcreate (std::shared_ptr< CompilerInvocation > CI, std::shared_ptr< DiagnosticOptions > DiagOpts, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, CaptureDiagsKind CaptureDiagnostics, bool UserFilesAreVolatile)
 Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
static std::unique_ptr< ASTUnitLoadFromASTFile (StringRef Filename, const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad, std::shared_ptr< DiagnosticOptions > DiagOpts, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, const FileSystemOptions &FileSystemOpts, const HeaderSearchOptions &HSOpts, const LangOptions *LangOpts=nullptr, bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, bool AllowASTWithCompilerErrors=false, bool UserFilesAreVolatile=false, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=llvm::vfs::getRealFileSystem())
 Create a ASTUnit from an AST file.
static ASTUnitLoadFromCompilerInvocationAction (std::shared_ptr< CompilerInvocation > CI, std::shared_ptr< PCHContainerOperations > PCHContainerOps, std::shared_ptr< DiagnosticOptions > DiagOpts, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, FrontendAction *Action=nullptr, ASTUnit *Unit=nullptr, bool Persistent=true, StringRef ResourceFilesPath=StringRef(), bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, unsigned PrecompilePreambleAfterNParses=0, bool CacheCodeCompletionResults=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 provided ASTFrontendAction.
static std::unique_ptr< ASTUnitLoadFromCompilerInvocation (std::shared_ptr< CompilerInvocation > CI, std::shared_ptr< PCHContainerOperations > PCHContainerOps, std::shared_ptr< DiagnosticOptions > DiagOpts, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, IntrusiveRefCntPtr< FileManager > FileMgr, bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, unsigned PrecompilePreambleAfterNParses=0, TranslationUnitKind TUKind=TU_Complete, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool UserFilesAreVolatile=false)
 LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a CompilerInvocation object.
static std::unique_ptr< ASTUnitLoadFromCommandLine (const char **ArgBegin, const char **ArgEnd, std::shared_ptr< PCHContainerOperations > PCHContainerOps, std::shared_ptr< DiagnosticOptions > DiagOpts, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, StringRef ResourceFilesPath, bool StorePreamblesInMemory=false, StringRef PreambleStoragePath=StringRef(), bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, ArrayRef< RemappedFile > RemappedFiles={}, bool RemappedFilesKeepOriginalName=true, unsigned PrecompilePreambleAfterNParses=0, TranslationUnitKind TUKind=TU_Complete, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool AllowPCHWithCompilerErrors=false, SkipFunctionBodiesScope SkipFunctionBodies=SkipFunctionBodiesScope::None, bool SingleFileParse=false, bool UserFilesAreVolatile=false, bool ForSerialization=false, bool RetainExcludedConditionalBlocks=false, std::optional< StringRef > ModuleFormat=std::nullopt, std::unique_ptr< ASTUnit > *ErrAST=nullptr, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
 LoadFromCommandLine - Create an ASTUnit from a vector of command line arguments, which must specify exactly one source file.

Friends

class ConcurrencyCheck

Detailed Description

Utility class for loading a ASTContext from an AST file.

Definition at line 90 of file ASTUnit.h.

Member Typedef Documentation

◆ cached_completion_iterator

Initial value:
std::vector<CachedCodeCompletionResult>::iterator

Definition at line 651 of file ASTUnit.h.

◆ const_diags_range

Definition at line 633 of file ASTUnit.h.

◆ DeclVisitorFn

using clang::ASTUnit::DeclVisitorFn = bool (*)(void *context, const Decl *D)

Type for a function iterating over a number of declarations.

Returns
true to continue iteration and false to abort.

Definition at line 674 of file ASTUnit.h.

◆ diags_range

using clang::ASTUnit::diags_range = llvm::iterator_range<stored_diag_iterator>

Definition at line 632 of file ASTUnit.h.

◆ RemappedFile

using clang::ASTUnit::RemappedFile = std::pair<std::string, llvm::MemoryBuffer *>

A mapping from a file name to the memory buffer that stores the remapped contents of that file.

Definition at line 700 of file ASTUnit.h.

◆ stored_diag_const_iterator

Definition at line 614 of file ASTUnit.h.

◆ stored_diag_iterator

Definition at line 613 of file ASTUnit.h.

◆ top_level_iterator

using clang::ASTUnit::top_level_iterator = std::vector<Decl *>::iterator

Definition at line 527 of file ASTUnit.h.

Member Enumeration Documentation

◆ WhatToLoad

Enumerator
LoadPreprocessorOnly 

Load options and the preprocessor state.

LoadASTOnly 

Load the AST, but do not restore Sema state.

LoadEverything 

Load everything, including Sema.

Definition at line 709 of file ASTUnit.h.

Constructor & Destructor Documentation

◆ ASTUnit()

clang::ASTUnit::ASTUnit ( const ASTUnit & )
delete

◆ ~ASTUnit()

Member Function Documentation

◆ addFileLevelDecl()

void ASTUnit::addFileLevelDecl ( Decl * D)

◆ addTopLevelDecl()

void clang::ASTUnit::addTopLevelDecl ( Decl * D)
inline

Add a new top-level declaration.

Definition at line 554 of file ASTUnit.h.

◆ cached_completion_begin()

cached_completion_iterator clang::ASTUnit::cached_completion_begin ( )
inline

Definition at line 654 of file ASTUnit.h.

◆ cached_completion_end()

cached_completion_iterator clang::ASTUnit::cached_completion_end ( )
inline

Definition at line 658 of file ASTUnit.h.

◆ cached_completion_size()

unsigned clang::ASTUnit::cached_completion_size ( ) const
inline

Definition at line 662 of file ASTUnit.h.

◆ CodeComplete()

void ASTUnit::CodeComplete ( StringRef File,
unsigned Line,
unsigned Column,
ArrayRef< RemappedFile > RemappedFiles,
bool IncludeMacros,
bool IncludeCodePatterns,
bool IncludeBriefComments,
CodeCompleteConsumer & Consumer,
std::shared_ptr< PCHContainerOperations > PCHContainerOps,
llvm::IntrusiveRefCntPtr< DiagnosticsEngine > Diag,
LangOptions & LangOpts,
llvm::IntrusiveRefCntPtr< SourceManager > SourceMgr,
llvm::IntrusiveRefCntPtr< FileManager > FileMgr,
SmallVectorImpl< StoredDiagnostic > & StoredDiagnostics,
SmallVectorImpl< const llvm::MemoryBuffer * > & OwnedBuffers,
std::unique_ptr< SyntaxOnlyAction > Act )

Perform code completion at the given file, line, and column within this translation unit.

Parameters
FileThe file in which code completion will occur.
LineThe line at which code completion will occur.
ColumnThe column at which code completion will occur.
IncludeMacrosWhether to include macros in the code-completion results.
IncludeCodePatternsWhether to include code patterns (such as a for loop) in the code-completion results.
IncludeBriefCommentsWhether to include brief documentation within the set of code completions returned.
ActIf supplied, this argument is used to parse the input file, allowing customized parsing by overriding SyntaxOnlyAction lifecycle methods.

FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and OwnedBuffers parameters are all disgusting hacks. They will go away.

Definition at line 2210 of file ASTUnit.cpp.

References clang::PreprocessorOptions::addRemappedFile(), clang::All, clang::Capture, clang::PreprocessorOptions::clearRemappedFiles(), clang::FrontendOptions::CodeCompleteOpts, clang::FrontendOptions::CodeCompletionAt, clang::ParsedSourceLocation::Column, clang::Column, clang::PreprocessorOptions::DetailedRecord, Diag(), clang::File, clang::ParsedSourceLocation::FileName, clang::CodeCompleteOptions::IncludeBriefComments, clang::CodeCompleteOptions::IncludeCodePatterns, clang::CodeCompleteOptions::IncludeFixIts, clang::CodeCompleteOptions::IncludeGlobals, clang::CodeCompleteOptions::IncludeMacros, clang::ParsedSourceLocation::Line, clang::Line, clang::LLVM_IR, clang::CodeCompleteOptions::LoadExternal, clang::PreprocessorOptions::PrecompiledPreambleBytes, clang::ProcessWarningOptions(), clang::PreprocessorOptions::RetainRemappedFileBuffers, and clang::InputKind::Source.

◆ create()

std::unique_ptr< ASTUnit > ASTUnit::create ( std::shared_ptr< CompilerInvocation > CI,
std::shared_ptr< DiagnosticOptions > DiagOpts,
IntrusiveRefCntPtr< DiagnosticsEngine > Diags,
CaptureDiagsKind CaptureDiagnostics,
bool UserFilesAreVolatile )
static

Create a ASTUnit. Gets ownership of the passed CompilerInvocation.

Definition at line 1546 of file ASTUnit.cpp.

References clang::createCrossProcessModuleCache(), and clang::createVFSFromCompilerInvocation().

Referenced by LoadFromCompilerInvocationAction().

◆ enableSourceFileDiagnostics()

void ASTUnit::enableSourceFileDiagnostics ( )

Enable source-range based diagnostic messages.

If diagnostic messages with source-range information are to be expected and AST comes not from file (e.g. after LoadFromCompilerInvocation) this function has to be called. The function is to be called only once and the AST should be associated with the same source file afterwards.

Definition at line 275 of file ASTUnit.cpp.

References clang::DiagnosticConsumer::BeginSourceFile(), clang::DiagnosticsEngine::getClient(), and getDiagnostics().

◆ findFileRegionDecls()

void ASTUnit::findFileRegionDecls ( FileID File,
unsigned Offset,
unsigned Length,
SmallVectorImpl< Decl * > & Decls )

Get the decls that are contained in a file in the Offset/Length range.

Length can be 0 to indicate a point at Offset instead of a range.

Definition at line 2509 of file ASTUnit.cpp.

References clang::File.

◆ getASTContext() [1/2]

ASTContext & clang::ASTUnit::getASTContext ( )
inline

Definition at line 463 of file ASTUnit.h.

◆ getASTContext() [2/2]

const ASTContext & clang::ASTUnit::getASTContext ( ) const
inline

Definition at line 462 of file ASTUnit.h.

Referenced by clang::index::indexASTUnit().

◆ getASTContextPtr()

llvm::IntrusiveRefCntPtr< ASTContext > clang::ASTUnit::getASTContextPtr ( )
inline

Definition at line 464 of file ASTUnit.h.

◆ getASTFileName()

StringRef ASTUnit::getASTFileName ( ) const

If this ASTUnit came from an AST file, returns the filename for it.

Definition at line 1536 of file ASTUnit.cpp.

References clang::serialization::ModuleFile::FileName, and isMainFileAST().

◆ getASTMutationListener()

ASTMutationListener * ASTUnit::getASTMutationListener ( )

Definition at line 775 of file ASTUnit.cpp.

◆ getASTReader()

IntrusiveRefCntPtr< ASTReader > ASTUnit::getASTReader ( ) const

Definition at line 771 of file ASTUnit.cpp.

Referenced by clang::DumpModuleInfoAction::ExecuteAction().

◆ getBufferForFile()

std::unique_ptr< llvm::MemoryBuffer > ASTUnit::getBufferForFile ( StringRef Filename,
std::string * ErrorStr = nullptr )

Definition at line 788 of file ASTUnit.cpp.

◆ getCachedCompletionAllocator()

std::shared_ptr< GlobalCodeCompletionAllocator > clang::ASTUnit::getCachedCompletionAllocator ( )
inline

Retrieve the allocator used to cache global code completions.

Definition at line 328 of file ASTUnit.h.

◆ getCachedCompletionTypes()

llvm::StringMap< unsigned > & clang::ASTUnit::getCachedCompletionTypes ( )
inline

Retrieve the mapping from formatted type names to unique type identifiers.

Definition at line 322 of file ASTUnit.h.

◆ getCodeCompletionTUInfo()

CodeCompletionTUInfo & clang::ASTUnit::getCodeCompletionTUInfo ( )
inline

Definition at line 332 of file ASTUnit.h.

◆ getCurrentTopLevelHashValue()

unsigned & clang::ASTUnit::getCurrentTopLevelHashValue ( )
inline

Retrieve a reference to the current top-level name hash value.

Note: This is used internally by the top-level tracking action

Definition at line 570 of file ASTUnit.h.

Referenced by LoadFromCompilerInvocationAction().

◆ getDeserializationListener()

ASTDeserializationListener * ASTUnit::getDeserializationListener ( )

Definition at line 781 of file ASTUnit.cpp.

◆ getDiagnostics() [1/2]

DiagnosticsEngine & clang::ASTUnit::getDiagnostics ( )
inline

Definition at line 447 of file ASTUnit.h.

◆ getDiagnostics() [2/2]

const DiagnosticsEngine & clang::ASTUnit::getDiagnostics ( ) const
inline

◆ getDiagnosticsPtr()

llvm::IntrusiveRefCntPtr< DiagnosticsEngine > clang::ASTUnit::getDiagnosticsPtr ( )
inline

Definition at line 448 of file ASTUnit.h.

Referenced by LoadFromCompilerInvocationAction().

◆ getEndOfPreambleFileID()

SourceLocation ASTUnit::getEndOfPreambleFileID ( ) const

Definition at line 2630 of file ASTUnit.cpp.

References clang::FileID::isInvalid().

◆ getFileManager() [1/2]

FileManager & clang::ASTUnit::getFileManager ( )
inline

Definition at line 503 of file ASTUnit.h.

◆ getFileManager() [2/2]

const FileManager & clang::ASTUnit::getFileManager ( ) const
inline

Definition at line 502 of file ASTUnit.h.

Referenced by LoadFromCompilerInvocationAction().

◆ getFileManagerPtr()

IntrusiveRefCntPtr< FileManager > clang::ASTUnit::getFileManagerPtr ( )
inline

Definition at line 504 of file ASTUnit.h.

Referenced by LoadFromCompilerInvocationAction().

◆ getFileSystemOpts()

const FileSystemOptions & clang::ASTUnit::getFileSystemOpts ( ) const
inline

Definition at line 506 of file ASTUnit.h.

◆ getHeaderSearchOpts()

const HeaderSearchOptions & clang::ASTUnit::getHeaderSearchOpts ( ) const
inline

Definition at line 492 of file ASTUnit.h.

◆ getInputKind()

InputKind ASTUnit::getInputKind ( ) const

◆ getLangOpts()

const LangOptions & clang::ASTUnit::getLangOpts ( ) const
inline

◆ getLocalPreprocessingEntities()

llvm::iterator_range< PreprocessingRecord::iterator > ASTUnit::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.

Definition at line 2653 of file ASTUnit.cpp.

References isMainFileAST().

◆ getLocation() [1/2]

SourceLocation ASTUnit::getLocation ( const FileEntry * File,
unsigned Line,
unsigned Col ) const

Get the source location for the given file:line:col triplet.

The difference with SourceManager::getLocation is that this method checks whether the requested location points inside the precompiled preamble in which case the returned source location will be a "loaded" one.

Definition at line 2552 of file ASTUnit.cpp.

References clang::File, getSourceManager(), clang::Line, and SM.

◆ getLocation() [2/2]

SourceLocation ASTUnit::getLocation ( const FileEntry * File,
unsigned Offset ) const

Get the source location for the given file:offset pair.

Definition at line 2559 of file ASTUnit.cpp.

References clang::File, clang::SourceLocation::getLocWithOffset(), getSourceManager(), and SM.

◆ getMainFileName()

StringRef ASTUnit::getMainFileName ( ) const

◆ getOnlyLocalDecls()

bool clang::ASTUnit::getOnlyLocalDecls ( ) const
inline

Definition at line 517 of file ASTUnit.h.

◆ getOriginalSourceFileName()

StringRef clang::ASTUnit::getOriginalSourceFileName ( ) const
inline

Definition at line 510 of file ASTUnit.h.

◆ getOwnsRemappedFileBuffers()

bool clang::ASTUnit::getOwnsRemappedFileBuffers ( ) const
inline

Definition at line 519 of file ASTUnit.h.

◆ getPCHFile()

◆ getPreambleCounterForTests()

unsigned clang::ASTUnit::getPreambleCounterForTests ( ) const
inline

Definition at line 610 of file ASTUnit.h.

◆ getPreprocessor() [1/2]

Preprocessor & clang::ASTUnit::getPreprocessor ( )
inline

Definition at line 459 of file ASTUnit.h.

◆ getPreprocessor() [2/2]

const Preprocessor & clang::ASTUnit::getPreprocessor ( ) const
inline

Definition at line 458 of file ASTUnit.h.

Referenced by clang::index::indexASTUnit().

◆ getPreprocessorOpts()

const PreprocessorOptions & clang::ASTUnit::getPreprocessorOpts ( ) const
inline

Definition at line 497 of file ASTUnit.h.

◆ getPreprocessorPtr()

std::shared_ptr< Preprocessor > clang::ASTUnit::getPreprocessorPtr ( ) const
inline

Definition at line 460 of file ASTUnit.h.

Referenced by clang::index::indexASTUnit().

◆ getSema()

Sema & clang::ASTUnit::getSema ( ) const
inline

Definition at line 482 of file ASTUnit.h.

Referenced by serialize().

◆ getSourceManager() [1/2]

SourceManager & clang::ASTUnit::getSourceManager ( )
inline

Definition at line 453 of file ASTUnit.h.

◆ getSourceManager() [2/2]

const SourceManager & clang::ASTUnit::getSourceManager ( ) const
inline

Definition at line 452 of file ASTUnit.h.

Referenced by getLocation(), and getLocation().

◆ getSourceManagerPtr()

llvm::IntrusiveRefCntPtr< SourceManager > clang::ASTUnit::getSourceManagerPtr ( )
inline

Definition at line 454 of file ASTUnit.h.

Referenced by LoadFromCompilerInvocationAction().

◆ getStartOfMainFileID()

SourceLocation ASTUnit::getStartOfMainFileID ( ) const

Definition at line 2641 of file ASTUnit.cpp.

References clang::FileID::isInvalid().

◆ getTranslationUnitKind()

TranslationUnitKind clang::ASTUnit::getTranslationUnitKind ( ) const
inline

Determine what kind of translation unit this AST represents.

Definition at line 693 of file ASTUnit.h.

◆ hasSema()

bool clang::ASTUnit::hasSema ( ) const
inline

Definition at line 480 of file ASTUnit.h.

◆ isInMainFileID()

bool ASTUnit::isInMainFileID ( SourceLocation Loc) const

Definition at line 2619 of file ASTUnit.cpp.

References clang::FileID::isInvalid(), and clang::SourceLocation::isInvalid().

◆ isInPreambleFileID()

bool ASTUnit::isInPreambleFileID ( SourceLocation Loc) const

Definition at line 2608 of file ASTUnit.cpp.

References clang::FileID::isInvalid(), and clang::SourceLocation::isInvalid().

◆ isMainFileAST()

bool clang::ASTUnit::isMainFileAST ( ) const
inline

◆ isModuleFile()

bool ASTUnit::isModuleFile ( ) const

Returns true if the ASTUnit was constructed from a serialized module file.

Definition at line 2717 of file ASTUnit.cpp.

References getLangOpts(), clang::LangOptions::isCompilingModule(), and isMainFileAST().

◆ isUnsafeToFree()

bool clang::ASTUnit::isUnsafeToFree ( ) const
inline

Definition at line 443 of file ASTUnit.h.

◆ LoadFromASTFile()

std::unique_ptr< ASTUnit > ASTUnit::LoadFromASTFile ( StringRef Filename,
const PCHContainerReader & PCHContainerRdr,
WhatToLoad ToLoad,
std::shared_ptr< DiagnosticOptions > DiagOpts,
IntrusiveRefCntPtr< DiagnosticsEngine > Diags,
const FileSystemOptions & FileSystemOpts,
const HeaderSearchOptions & HSOpts,
const LangOptions * LangOpts = nullptr,
bool OnlyLocalDecls = false,
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
bool AllowASTWithCompilerErrors = false,
bool UserFilesAreVolatile = false,
IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS = llvm::vfs::getRealFileSystem() )
static

Create a ASTUnit from an AST file.

Parameters
Filename- The AST file to load.
PCHContainerRdr- The PCHContainerOperations to use for loading and creating modules.
Diags- The diagnostics engine to use for reporting errors; its lifetime is expected to extend past that of the returned ASTUnit.
Returns
- The initialized ASTUnit or null if the AST failed to load.

Definition at line 809 of file ASTUnit.cpp.

References clang::All, clang::ASTReader::ARR_None, clang::ASTReader::ConfigurationMismatch, clang::createCrossProcessModuleCache(), clang::ASTReader::Failure, clang::PCHContainerReader::getFormats(), clang::ASTReader::HadErrors, clang::Module::isNamedModule(), LoadASTOnly, LoadEverything, clang::ASTReader::Missing, clang::serialization::MK_MainFile, clang::None, clang::ASTReader::OutOfDate, clang::ASTReader::Success, and clang::ASTReader::VersionMismatch.

Referenced by clang::FrontendAction::BeginSourceFile(), and clang::ASTMergeAction::ExecuteAction().

◆ LoadFromCommandLine()

std::unique_ptr< ASTUnit > ASTUnit::LoadFromCommandLine ( const char ** ArgBegin,
const char ** ArgEnd,
std::shared_ptr< PCHContainerOperations > PCHContainerOps,
std::shared_ptr< DiagnosticOptions > DiagOpts,
IntrusiveRefCntPtr< DiagnosticsEngine > Diags,
StringRef ResourceFilesPath,
bool StorePreamblesInMemory = false,
StringRef PreambleStoragePath = StringRef(),
bool OnlyLocalDecls = false,
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
ArrayRef< RemappedFile > RemappedFiles = {},
bool RemappedFilesKeepOriginalName = true,
unsigned PrecompilePreambleAfterNParses = 0,
TranslationUnitKind TUKind = TU_Complete,
bool CacheCodeCompletionResults = false,
bool IncludeBriefCommentsInCodeCompletion = false,
bool AllowPCHWithCompilerErrors = false,
SkipFunctionBodiesScope SkipFunctionBodies = SkipFunctionBodiesScope::None,
bool SingleFileParse = false,
bool UserFilesAreVolatile = false,
bool ForSerialization = false,
bool RetainExcludedConditionalBlocks = false,
std::optional< StringRef > ModuleFormat = std::nullopt,
std::unique_ptr< ASTUnit > * ErrAST = nullptr,
IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS = nullptr )
static

LoadFromCommandLine - Create an ASTUnit from a vector of command line arguments, which must specify exactly one source file.

Parameters
ArgBegin- The beginning of the argument vector.
ArgEnd- The end of the argument vector.
PCHContainerOps- The PCHContainerOperations to use for loading and creating modules.
Diags- The diagnostics engine to use for reporting errors; its lifetime is expected to extend past that of the returned ASTUnit.
ResourceFilesPath- The path to the compiler resource files.
StorePreamblesInMemory- Whether to store PCH in memory. If false, PCH are stored in temporary files.
PreambleStoragePath- The path to a directory, in which to create temporary PCH files. If empty, the default system temporary directory is used. This parameter is ignored if StorePreamblesInMemory is true.
ModuleFormat- If provided, uses the specific module format.
ErrAST- If non-null and parsing failed without any AST to return (e.g. because the PCH could not be loaded), this accepts the ASTUnit mainly to allow the caller to see the diagnostics.
VFS- A llvm::vfs::FileSystem to be used for all file accesses. Note that preamble is saved to a temporary directory on a RealFileSystem, so in order for it to be loaded correctly, VFS should have access to it(i.e., be an overlay over RealFileSystem). RealFileSystem will be used if VFS is nullptr.

Definition at line 1784 of file ASTUnit.cpp.

References clang::Capture, clang::createCrossProcessModuleCache(), clang::createInvocation(), clang::createVFSFromCompilerInvocation(), clang::CreateInvocationOptions::Diags, clang::PreambleAndMainFile, clang::CreateInvocationOptions::ProbePrecompiled, clang::PreprocessorOptions::RemappedFilesKeepOriginalName, and clang::CreateInvocationOptions::VFS.

◆ LoadFromCompilerInvocation()

std::unique_ptr< ASTUnit > ASTUnit::LoadFromCompilerInvocation ( std::shared_ptr< CompilerInvocation > CI,
std::shared_ptr< PCHContainerOperations > PCHContainerOps,
std::shared_ptr< DiagnosticOptions > DiagOpts,
IntrusiveRefCntPtr< DiagnosticsEngine > Diags,
IntrusiveRefCntPtr< FileManager > FileMgr,
bool OnlyLocalDecls = false,
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
unsigned PrecompilePreambleAfterNParses = 0,
TranslationUnitKind TUKind = TU_Complete,
bool CacheCodeCompletionResults = false,
bool IncludeBriefCommentsInCodeCompletion = false,
bool UserFilesAreVolatile = false )
static

LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a CompilerInvocation object.

Parameters
CI- The compiler invocation to use; it must have exactly one input source file. The ASTUnit takes ownership of the CompilerInvocation object.
PCHContainerOps- The PCHContainerOperations to use for loading and creating modules.
Diags- The diagnostics engine to use for reporting errors; its lifetime is expected to extend past that of the returned ASTUnit.

Definition at line 1744 of file ASTUnit.cpp.

◆ LoadFromCompilerInvocationAction()

ASTUnit * ASTUnit::LoadFromCompilerInvocationAction ( std::shared_ptr< CompilerInvocation > CI,
std::shared_ptr< PCHContainerOperations > PCHContainerOps,
std::shared_ptr< DiagnosticOptions > DiagOpts,
IntrusiveRefCntPtr< DiagnosticsEngine > Diags,
FrontendAction * Action = nullptr,
ASTUnit * Unit = nullptr,
bool Persistent = true,
StringRef ResourceFilesPath = StringRef(),
bool OnlyLocalDecls = false,
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
unsigned PrecompilePreambleAfterNParses = 0,
bool CacheCodeCompletionResults = false,
bool UserFilesAreVolatile = false,
std::unique_ptr< ASTUnit > * ErrAST = nullptr )
static

Create an ASTUnit from a source file, via a CompilerInvocation object, by invoking the optionally provided ASTFrontendAction.

Parameters
CI- The compiler invocation to use; it must have exactly one input source file. The ASTUnit takes ownership of the CompilerInvocation object.
PCHContainerOps- The PCHContainerOperations to use for loading and creating modules.
Diags- The diagnostics engine to use for reporting errors; its lifetime is expected to extend past that of the returned ASTUnit.
Action- The ASTFrontendAction to invoke. Its ownership is not transferred.
Unit- optionally an already created ASTUnit. Its ownership is not transferred.
Persistent- if true the returned ASTUnit will be complete. false means the caller is only interested in getting info through the provided
See also
Action.
Parameters
ErrAST- If non-null and parsing failed without any AST to return (e.g. because the PCH could not be loaded), this accepts the ASTUnit mainly to allow the caller to see the diagnostics. This will only receive an ASTUnit if a new one was created. If an already created ASTUnit was passed in Unit then the caller can check that.

Definition at line 1569 of file ASTUnit.cpp.

References clang::FrontendAction::BeginSourceFile(), create(), clang::FrontendAction::EndSourceFile(), clang::FrontendAction::Execute(), getCurrentTopLevelHashValue(), getDiagnostics(), getDiagnosticsPtr(), getFileManager(), getFileManagerPtr(), getSourceManagerPtr(), clang::FrontendAction::getTranslationUnitKind(), clang::FileManager::getVirtualFileSystem(), clang::LLVM_IR, clang::ProcessWarningOptions(), clang::InputKind::Source, and clang::TU_Complete.

Referenced by clang::ReplCodeCompleter::codeComplete().

◆ mapLocationFromPreamble()

SourceLocation ASTUnit::mapLocationFromPreamble ( SourceLocation Loc) const

If Loc is a loaded location from the preamble, returns the corresponding local location of the main file, otherwise it returns Loc.

If.

  • Loc is a loaded location from the preamble, returns the corresponding local location of the main file, otherwise it returns
  • Loc.

Definition at line 2569 of file ASTUnit.cpp.

References clang::SourceLocation::getLocWithOffset(), clang::FileID::isInvalid(), and clang::SourceLocation::isInvalid().

Referenced by mapRangeFromPreamble().

◆ mapLocationToPreamble()

SourceLocation ASTUnit::mapLocationToPreamble ( SourceLocation Loc) const

If Loc is a local location of the main file but inside the preamble chunk, returns the corresponding loaded location from the preamble, otherwise it returns Loc.

If.

  • Loc is a local location of the main file but inside the preamble chunk, returns the corresponding loaded location from the preamble, otherwise it returns
  • Loc.

Definition at line 2590 of file ASTUnit.cpp.

References clang::SourceLocation::getLocWithOffset(), clang::FileID::isInvalid(), and clang::SourceLocation::isInvalid().

Referenced by mapRangeToPreamble().

◆ mapRangeFromPreamble()

SourceRange clang::ASTUnit::mapRangeFromPreamble ( SourceRange R) const
inline

◆ mapRangeToPreamble()

SourceRange clang::ASTUnit::mapRangeToPreamble ( SourceRange R) const
inline

◆ operator=()

ASTUnit & clang::ASTUnit::operator= ( const ASTUnit & )
delete

◆ Reparse()

bool ASTUnit::Reparse ( std::shared_ptr< PCHContainerOperations > PCHContainerOps,
ArrayRef< RemappedFile > RemappedFiles = {},
IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS = nullptr )

Reparse the source files using the same command-line options that were originally used to produce this translation unit.

Parameters
VFS- A llvm::vfs::FileSystem to be used for all file accesses. Note that preamble is saved to a temporary directory on a RealFileSystem, so in order for it to be loaded correctly, VFS should give an access to this(i.e. be an overlay over RealFileSystem). FileMgr->getVirtualFileSystem() will be used if VFS is nullptr.
Returns
True if a failure occurred that causes the ASTUnit not to contain any translation-unit information, false otherwise.

Definition at line 1894 of file ASTUnit.cpp.

References clang::PreprocessorOptions::clearRemappedFiles(), getDiagnostics(), getMainFileName(), clang::Parse, clang::ProcessWarningOptions(), clang::DiagnosticsEngine::Reset(), clang::Result, and clang::DiagnosticsEngine::setNumWarnings().

◆ ResetForParse()

void ASTUnit::ResetForParse ( )

Free data that will be re-generated on the next parse.

Preamble-related data is not affected.

Definition at line 1953 of file ASTUnit.cpp.

◆ Save()

bool ASTUnit::Save ( StringRef File)

Save this translation unit to a file with the given name.

Returns
true if there was a file error or false if the save was successful.

Definition at line 2369 of file ASTUnit.cpp.

References clang::File, and serialize().

◆ serialize()

bool ASTUnit::serialize ( raw_ostream & OS)

Serialize this translation unit with the given output stream.

Returns
True if an error occurred, false otherwise.

Definition at line 2400 of file ASTUnit.cpp.

References clang::createCrossProcessModuleCache(), getSema(), and serializeUnit().

Referenced by Save().

◆ setASTContext()

void clang::ASTUnit::setASTContext ( llvm::IntrusiveRefCntPtr< ASTContext > ctx)
inline

Definition at line 466 of file ASTUnit.h.

◆ setOwnsRemappedFileBuffers()

void clang::ASTUnit::setOwnsRemappedFileBuffers ( bool val)
inline

Definition at line 520 of file ASTUnit.h.

◆ setPreprocessor()

void ASTUnit::setPreprocessor ( std::shared_ptr< Preprocessor > pp)

Definition at line 271 of file ASTUnit.cpp.

◆ setUnsafeToFree()

void clang::ASTUnit::setUnsafeToFree ( bool Value)
inline

Definition at line 444 of file ASTUnit.h.

◆ stored_diag_afterDriver_begin()

stored_diag_iterator clang::ASTUnit::stored_diag_afterDriver_begin ( )
inline

Definition at line 645 of file ASTUnit.h.

◆ stored_diag_begin() [1/2]

stored_diag_iterator clang::ASTUnit::stored_diag_begin ( )
inline

Definition at line 620 of file ASTUnit.h.

◆ stored_diag_begin() [2/2]

stored_diag_const_iterator clang::ASTUnit::stored_diag_begin ( ) const
inline

Definition at line 616 of file ASTUnit.h.

Referenced by storedDiagnostics(), and storedDiagnostics().

◆ stored_diag_end() [1/2]

stored_diag_iterator clang::ASTUnit::stored_diag_end ( )
inline

Definition at line 628 of file ASTUnit.h.

◆ stored_diag_end() [2/2]

stored_diag_const_iterator clang::ASTUnit::stored_diag_end ( ) const
inline

Definition at line 624 of file ASTUnit.h.

Referenced by storedDiagnostics(), and storedDiagnostics().

◆ stored_diag_size()

unsigned clang::ASTUnit::stored_diag_size ( ) const
inline

Definition at line 643 of file ASTUnit.h.

◆ storedDiagnostics() [1/2]

diags_range clang::ASTUnit::storedDiagnostics ( )
inline

Definition at line 635 of file ASTUnit.h.

References stored_diag_begin(), and stored_diag_end().

◆ storedDiagnostics() [2/2]

const_diags_range clang::ASTUnit::storedDiagnostics ( ) const
inline

Definition at line 639 of file ASTUnit.h.

References stored_diag_begin(), and stored_diag_end().

◆ top_level_begin()

top_level_iterator clang::ASTUnit::top_level_begin ( )
inline

Definition at line 529 of file ASTUnit.h.

References isMainFileAST().

Referenced by visitLocalTopLevelDecls().

◆ top_level_empty()

bool clang::ASTUnit::top_level_empty ( ) const
inline

Definition at line 548 of file ASTUnit.h.

References isMainFileAST().

◆ top_level_end()

top_level_iterator clang::ASTUnit::top_level_end ( )
inline

Definition at line 536 of file ASTUnit.h.

References isMainFileAST().

Referenced by visitLocalTopLevelDecls().

◆ top_level_size()

std::size_t clang::ASTUnit::top_level_size ( ) const
inline

Definition at line 543 of file ASTUnit.h.

References isMainFileAST().

◆ visitLocalTopLevelDecls()

bool ASTUnit::visitLocalTopLevelDecls ( void * context,
DeclVisitorFn Fn )

Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declarations of the primary module if this is an AST file).

Returns
true if the iteration was complete or false if it was aborted.

Definition at line 2667 of file ASTUnit.cpp.

References isMainFileAST(), top_level_begin(), and top_level_end().

Referenced by indexTranslationUnit().

◆ ConcurrencyCheck

friend class ConcurrencyCheck
friend

Definition at line 422 of file ASTUnit.h.


The documentation for this class was generated from the following files: