class TreeBuilder

Declaration

class TreeBuilder { /* full declaration omitted */ };

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: - create a corresponding syntax node, - assign roles to all pending child nodes with 'markChild' and 'markChildToken', - replace the child nodes with the new syntax node in the pending list with 'foldNode'. 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.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:367

Member Variables

private syntax::Arena& Arena
private clang::syntax::TokenBufferTokenManager& TBTM
private llvm::DenseMap<SourceLocation, const syntax::Token*> LocationToToken
To quickly find tokens by their start location.
private clang::syntax::TreeBuilder::Forest Pending
private llvm::DenseSet<Decl*> DeclsWithoutSemicolons
private anonymous struct / union Mapping

Method Overview

  • public TreeBuilder(syntax::Arena & Arena, clang::syntax::TokenBufferTokenManager & TBTM)
  • public llvm::BumpPtrAllocator & allocator()
  • public syntax::TranslationUnit * finalize() &&
  • public const syntax::Token * findToken(clang::SourceLocation L) const
  • public void foldList(ArrayRef<syntax::Token> SuperRange, syntax::List * New, (anonymous namespace)::ASTPtr From)
  • public void foldNode(ArrayRef<syntax::Token> Range, syntax::Tree * New, (anonymous namespace)::ASTPtr From)
  • public void foldNode(ArrayRef<syntax::Token> Range, syntax::Tree * New, clang::TypeLoc L)
  • public void foldNode(llvm::ArrayRef<syntax::Token> Range, syntax::Tree * New, clang::NestedNameSpecifierLoc From)
  • public ArrayRef<syntax::Token> getDeclarationRange(clang::Decl * D)
  • public ArrayRef<syntax::Token> getExprRange(const clang::Expr * E) const
  • public ArrayRef<syntax::Token> getRange(clang::SourceLocation First, clang::SourceLocation Last) const
  • public ArrayRef<syntax::Token> getRange(clang::SourceRange Range) const
  • public ArrayRef<syntax::Token> getStmtRange(const clang::Stmt * S) const
  • public ArrayRef<syntax::Token> getTemplateRange(const clang::ClassTemplateSpecializationDecl * D) const
  • public bool isResponsibleForCreatingDeclaration(const clang::Decl * D) const
  • public void markChild(clang::NestedNameSpecifierLoc N, clang::syntax::NodeRole R)
  • public void markChild((anonymous namespace)::ASTPtr N, clang::syntax::NodeRole R)
  • public void markChild(syntax::Node * N, clang::syntax::NodeRole R)
  • public void markChildToken(const syntax::Token * T, clang::syntax::NodeRole R)
  • public void markChildToken(clang::SourceLocation Loc, clang::syntax::NodeRole R)
  • public void markExprChild(clang::Expr * Child, clang::syntax::NodeRole Role)
  • public void markStmtChild(clang::Stmt * Child, clang::syntax::NodeRole Role)
  • private ArrayRef<syntax::Token> maybeAppendSemicolon(ArrayRef<syntax::Token> Tokens, const clang::Decl * D) const
  • public void noticeDeclWithoutSemicolon(clang::Decl * D)
  • private void setRole(syntax::Node * N, clang::syntax::NodeRole R)
  • public const clang::SourceManager & sourceManager() const
  • private std::string str()
  • private ArrayRef<syntax::Token> withTrailingSemicolon(ArrayRef<syntax::Token> Tokens) const

Methods

TreeBuilder(
    syntax::Arena& Arena,
    clang::syntax::TokenBufferTokenManager& TBTM)

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:369

Parameters

syntax::Arena& Arena
clang::syntax::TokenBufferTokenManager& TBTM

llvm::BumpPtrAllocator& allocator()

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:377

syntax::TranslationUnit* finalize() &&

Description

Finish building the tree and consume the root node.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:440

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

Description

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:455

Parameters

clang::SourceLocation L

void foldList(ArrayRef<syntax::Token> SuperRange,
              syntax::List* New,
              (anonymous namespace)::ASTPtr From)

Description

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:406

Parameters

ArrayRef<syntax::Token> SuperRange
syntax::List* New
(anonymous namespace)::ASTPtr From

void foldNode(ArrayRef<syntax::Token> Range,
              syntax::Tree* New,
              (anonymous namespace)::ASTPtr From)

Description

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:384

Parameters

ArrayRef<syntax::Token> Range
syntax::Tree* New
(anonymous namespace)::ASTPtr From

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:391

Parameters

ArrayRef<syntax::Token> Range
syntax::Tree* New
clang::TypeLoc L

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:396

Parameters

llvm::ArrayRef<syntax::Token> Range
syntax::Tree* New
clang::NestedNameSpecifierLoc From

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:509

Parameters

clang::Decl* D

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:519

Parameters

const clang::Expr* E

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

Description

Finds the syntax tokens corresponding to the passed source locations.\p First is the start position of the first token and \p Last is the start position of the last token.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:466

Parameters

clang::SourceLocation First
clang::SourceLocation Last

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

Description

Finds the syntax tokens corresponding to the \p SourceRange.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:458

Parameters

clang::SourceRange Range

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

Description

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:525

Parameters

const clang::Stmt* S

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:476

Parameters

const clang::ClassTemplateSpecializationDecl* D

bool isResponsibleForCreatingDeclaration(
    const clang::Decl* D) const

Description

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:483

Parameters

const clang::Decl* D

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

Description

Set role for the syntax node matching \p N.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:437

Parameters

clang::NestedNameSpecifierLoc N
clang::syntax::NodeRole R

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

Description

Set role for the syntax node matching \p N.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:435

Parameters

(anonymous namespace)::ASTPtr N
clang::syntax::NodeRole R

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

Description

Set role for \p N.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:433

Parameters

syntax::Node* N
clang::syntax::NodeRole R

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

Description

Set role for \p T.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:430

Parameters

const syntax::Token* T
clang::syntax::NodeRole R

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

Description

Set role for a token starting at \p Loc.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:428

Parameters

clang::SourceLocation Loc
clang::syntax::NodeRole R

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

Description

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:426

Parameters

clang::Expr* Child
clang::syntax::NodeRole Role

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

Description

Mark the \p Child node with a corresponding \p 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.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:423

Parameters

clang::Stmt* Child
clang::syntax::NodeRole Role

ArrayRef<syntax::Token> maybeAppendSemicolon(
    ArrayRef<syntax::Token> Tokens,
    const clang::Decl* D) const

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:538

Parameters

ArrayRef<syntax::Token> Tokens
const clang::Decl* D

void noticeDeclWithoutSemicolon(clang::Decl* D)

Description

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

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:417

Parameters

clang::Decl* D

void setRole(syntax::Node* N,
             clang::syntax::NodeRole R)

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:559

Parameters

syntax::Node* N
clang::syntax::NodeRole R

const clang::SourceManager& sourceManager() const

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:378

std::string str()

Description

For debugging purposes.

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:692

ArrayRef<syntax::Token> withTrailingSemicolon(
    ArrayRef<syntax::Token> Tokens) const

Declared at: clang/lib/Tooling/Syntax/BuildTree.cpp:550

Parameters

ArrayRef<syntax::Token> Tokens