clang  14.0.0git
Classes | Public Member Functions | List of all members
clang::syntax::TreeBuilder Class Reference

A helper class for constructing the syntax tree while traversing a clang AST. More...

Public Member Functions

 TreeBuilder (syntax::Arena &Arena)
 
llvm::BumpPtrAllocator & allocator ()
 
const SourceManagersourceManager () const
 
void foldNode (ArrayRef< syntax::Token > Range, syntax::Tree *New, ASTPtr From)
 Populate children for New node, assuming it covers tokens from Range. More...
 
void foldNode (ArrayRef< syntax::Token > Range, syntax::Tree *New, TypeLoc L)
 
void foldNode (llvm::ArrayRef< syntax::Token > Range, syntax::Tree *New, NestedNameSpecifierLoc From)
 
void foldList (ArrayRef< syntax::Token > SuperRange, syntax::List *New, ASTPtr From)
 Populate children for New list, assuming it covers tokens from a subrange of SuperRange. More...
 
void noticeDeclWithoutSemicolon (Decl *D)
 Notifies that we should not consume trailing semicolon when computing token range of D. More...
 
void markStmtChild (Stmt *Child, NodeRole Role)
 Mark the Child node with a corresponding Role. More...
 
void markExprChild (Expr *Child, NodeRole Role)
 Should be called for expressions in non-statement position to avoid wrapping into expression statement. More...
 
void markChildToken (SourceLocation Loc, NodeRole R)
 Set role for a token starting at Loc. More...
 
void markChildToken (const syntax::Token *T, NodeRole R)
 Set role for T. More...
 
void markChild (syntax::Node *N, NodeRole R)
 Set role for N. More...
 
void markChild (ASTPtr N, NodeRole R)
 Set role for the syntax node matching N. More...
 
void markChild (NestedNameSpecifierLoc N, NodeRole R)
 Set role for the syntax node matching N. More...
 
syntax::TranslationUnit * finalize () &&
 Finish building the tree and consume the root node. More...
 
const syntax::TokenfindToken (SourceLocation L) const
 Finds a token starting at L. The token must exist if L is valid. More...
 
ArrayRef< syntax::TokengetRange (SourceRange Range) const
 Finds the syntax tokens corresponding to the SourceRange. More...
 
ArrayRef< syntax::TokengetRange (SourceLocation First, SourceLocation Last) const
 Finds the syntax tokens corresponding to the passed source locations. More...
 
ArrayRef< syntax::TokengetTemplateRange (const ClassTemplateSpecializationDecl *D) const
 
bool isResponsibleForCreatingDeclaration (const Decl *D) const
 Returns true if D is the last declarator in a chain and is thus reponsible for creating SimpleDeclaration for the whole chain. More...
 
ArrayRef< syntax::TokengetDeclarationRange (Decl *D)
 
ArrayRef< syntax::TokengetExprRange (const Expr *E) const
 
ArrayRef< syntax::TokengetStmtRange (const Stmt *S) const
 Find the adjusted range for the statement, consuming the trailing semicolon when needed. More...
 

Detailed Description

A helper class for constructing the syntax tree while traversing a clang AST.

At each point of the traversal we maintain a list of pending nodes. Initially all tokens are added as pending nodes. When processing a clang AST node, the clients need to:

Note that all children are expected to be processed when building a node.

Call finalize() to finish building the tree and consume the root node.

Definition at line 367 of file BuildTree.cpp.

Constructor & Destructor Documentation

◆ TreeBuilder()

clang::syntax::TreeBuilder::TreeBuilder ( syntax::Arena Arena)
inline

Member Function Documentation

◆ allocator()

llvm::BumpPtrAllocator& clang::syntax::TreeBuilder::allocator ( )
inline

Definition at line 374 of file BuildTree.cpp.

References clang::syntax::Arena::getAllocator().

◆ finalize()

syntax::TranslationUnit* clang::syntax::TreeBuilder::finalize ( ) &&
inline

Finish building the tree and consume the root node.

Definition at line 437 of file BuildTree.cpp.

◆ findToken()

const syntax::Token * clang::syntax::TreeBuilder::findToken ( SourceLocation  L) const

Finds a token starting at L. The token must exist if L is valid.

Definition at line 1740 of file BuildTree.cpp.

References clang::SourceLocation::isInvalid().

Referenced by clang::syntax::TreeBuilder::getRange().

◆ foldList()

void clang::syntax::TreeBuilder::foldList ( ArrayRef< syntax::Token SuperRange,
syntax::List New,
ASTPtr  From 
)
inline

Populate children for New list, assuming it covers tokens from a subrange of SuperRange.

Definition at line 403 of file BuildTree.cpp.

◆ foldNode() [1/3]

void clang::syntax::TreeBuilder::foldNode ( ArrayRef< syntax::Token Range,
syntax::Tree New,
ASTPtr  From 
)
inline

Populate children for New node, assuming it covers tokens from Range.

Definition at line 381 of file BuildTree.cpp.

Referenced by clang::syntax::TreeBuilder::foldNode().

◆ foldNode() [2/3]

void clang::syntax::TreeBuilder::foldNode ( ArrayRef< syntax::Token Range,
syntax::Tree New,
TypeLoc  L 
)
inline

Definition at line 388 of file BuildTree.cpp.

References clang::syntax::TreeBuilder::foldNode().

◆ foldNode() [3/3]

void clang::syntax::TreeBuilder::foldNode ( llvm::ArrayRef< syntax::Token Range,
syntax::Tree New,
NestedNameSpecifierLoc  From 
)
inline

Definition at line 393 of file BuildTree.cpp.

◆ getDeclarationRange()

ArrayRef<syntax::Token> clang::syntax::TreeBuilder::getDeclarationRange ( Decl D)
inline

Definition at line 506 of file BuildTree.cpp.

◆ getExprRange()

ArrayRef<syntax::Token> clang::syntax::TreeBuilder::getExprRange ( const Expr E) const
inline

◆ getRange() [1/2]

ArrayRef<syntax::Token> clang::syntax::TreeBuilder::getRange ( SourceLocation  First,
SourceLocation  Last 
) const
inline

Finds the syntax tokens corresponding to the passed source locations.

First is the start position of the first token and Last is the start position of the last token.

Definition at line 463 of file BuildTree.cpp.

References clang::syntax::TreeBuilder::findToken(), clang::First, clang::syntax::Arena::getSourceManager(), clang::SourceManager::isBeforeInTranslationUnit(), and clang::Last.

◆ getRange() [2/2]

ArrayRef<syntax::Token> clang::syntax::TreeBuilder::getRange ( SourceRange  Range) const
inline

Finds the syntax tokens corresponding to the SourceRange.

Definition at line 455 of file BuildTree.cpp.

Referenced by clang::syntax::TreeBuilder::getExprRange().

◆ getStmtRange()

ArrayRef<syntax::Token> clang::syntax::TreeBuilder::getStmtRange ( const Stmt S) const
inline

Find the adjusted range for the statement, consuming the trailing semicolon when needed.

Definition at line 522 of file BuildTree.cpp.

◆ getTemplateRange()

ArrayRef<syntax::Token> clang::syntax::TreeBuilder::getTemplateRange ( const ClassTemplateSpecializationDecl D) const
inline

Definition at line 473 of file BuildTree.cpp.

◆ isResponsibleForCreatingDeclaration()

bool clang::syntax::TreeBuilder::isResponsibleForCreatingDeclaration ( const Decl D) const
inline

Returns true if D is the last declarator in a chain and is thus reponsible for creating SimpleDeclaration for the whole chain.

Definition at line 480 of file BuildTree.cpp.

◆ markChild() [1/3]

void clang::syntax::TreeBuilder::markChild ( ASTPtr  N,
NodeRole  R 
)

Set role for the syntax node matching N.

Definition at line 1700 of file BuildTree.cpp.

◆ markChild() [2/3]

void clang::syntax::TreeBuilder::markChild ( NestedNameSpecifierLoc  N,
NodeRole  R 
)

Set role for the syntax node matching N.

Definition at line 1705 of file BuildTree.cpp.

◆ markChild() [3/3]

void clang::syntax::TreeBuilder::markChild ( syntax::Node N,
NodeRole  R 
)

Set role for N.

Definition at line 1695 of file BuildTree.cpp.

◆ markChildToken() [1/2]

void clang::syntax::TreeBuilder::markChildToken ( const syntax::Token T,
NodeRole  R 
)

Set role for T.

Definition at line 1689 of file BuildTree.cpp.

◆ markChildToken() [2/2]

void clang::syntax::TreeBuilder::markChildToken ( SourceLocation  Loc,
NodeRole  R 
)

Set role for a token starting at Loc.

Definition at line 1683 of file BuildTree.cpp.

References clang::SourceLocation::isInvalid().

◆ markExprChild()

void clang::syntax::TreeBuilder::markExprChild ( Expr Child,
NodeRole  Role 
)

Should be called for expressions in non-statement position to avoid wrapping into expression statement.

Definition at line 1730 of file BuildTree.cpp.

References IgnoreImplicit().

◆ markStmtChild()

void clang::syntax::TreeBuilder::markStmtChild ( Stmt Child,
NodeRole  Role 
)

Mark the Child node with a corresponding Role.

All marked children should be consumed by foldNode. When called on expressions (clang::Expr is derived from clang::Stmt), wraps expressions into expression statement.

Definition at line 1711 of file BuildTree.cpp.

◆ noticeDeclWithoutSemicolon()

void clang::syntax::TreeBuilder::noticeDeclWithoutSemicolon ( Decl D)

Notifies that we should not consume trailing semicolon when computing token range of D.

Definition at line 1679 of file BuildTree.cpp.

◆ sourceManager()

const SourceManager& clang::syntax::TreeBuilder::sourceManager ( ) const
inline

Definition at line 375 of file BuildTree.cpp.

References clang::syntax::Arena::getSourceManager().


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