clang  13.0.0git
Classes | Enumerations | Functions
clang::syntax Namespace Reference

Classes

class  Arena
 A memory arena for syntax trees. More...
 
class  ArraySubscript
 Array size specified inside a declarator. More...
 
class  BinaryOperatorExpression
 <lhs> <operator> <rhs> More...
 
class  BreakStatement
 break; More...
 
class  CallArguments
 Models arguments of a function call. More...
 
class  CaseStatement
 case : <body> More...
 
class  CompoundStatement
 { statement1; statement2; … } More...
 
class  ContinueStatement
 continue; More...
 
class  Declaration
 A declaration that can appear at the top-level. More...
 
class  DeclarationStatement
 E.g. 'int a, b = 10;'. More...
 
class  Declarator
 Covers a name, an initializer and a part of the type outside declaration specifiers. More...
 
class  DeclaratorList
 
class  DefaultStatement
 default: <body> More...
 
class  EmptyDeclaration
 A semicolon in the top-level context. Does not declare anything. More...
 
class  EmptyStatement
 The no-op statement, i.e. ';'. More...
 
class  ExplicitTemplateInstantiation
 template <declaration> Examples: template struct X<int> template void foo<int>() template int var<double> More...
 
class  ExpressionStatement
 Expression in a statement position, e.g. More...
 
class  FactoryImpl
 Exposes private syntax tree APIs required to implement node synthesis. More...
 
struct  FileRange
 A half-open character range inside a particular file, the start offset is included and the end offset is excluded from the range. More...
 
class  ForStatement
 for (<init>; <cond>; <increment>) <body> More...
 
class  IfStatement
 if (cond) <then-statement> else <else-statement> FIXME: add condition that models 'expression or variable declaration' More...
 
class  Leaf
 A leaf node points to a single token inside the expanded token stream. More...
 
class  LinkageSpecificationDeclaration
 extern <string-literal> declaration extern <string-literal> { <decls> } More...
 
class  List
 A list of Elements separated or terminated by a fixed token. More...
 
class  MemberPointer
 Member pointer inside a declarator E.g. More...
 
class  MutationsImpl
 
class  NamespaceAliasDefinition
 namespace <name> = <namespace-reference> More...
 
class  NamespaceDefinition
 namespace <name> { <decls> } More...
 
class  NestedNameSpecifier
 Models a nested-name-specifier. More...
 
class  Node
 A node in a syntax tree. More...
 
class  ParameterDeclarationList
 Models a parameter-declaration-list which appears within parameters-and-qualifiers. More...
 
class  ParametersAndQualifiers
 Parameter list for a function type and a trailing return type, if the function has one. More...
 
class  ParenDeclarator
 Declarator inside parentheses. More...
 
class  PostfixUnaryOperatorExpression
 <operand> <operator> More...
 
class  PrefixUnaryOperatorExpression
 <operator> <operand> More...
 
class  RangeBasedForStatement
 for (<decl> : <init>) <body> More...
 
class  ReturnStatement
 return <expr>; return; More...
 
class  SimpleDeclaration
 Groups multiple declarators (e.g. More...
 
class  SimpleDeclarator
 A top-level declarator without parentheses. More...
 
class  Statement
 An abstract node for C++ statements, e.g. More...
 
class  StaticAssertDeclaration
 static_assert(<condition>, <message>) static_assert(<condition>) More...
 
class  SwitchStatement
 switch (<cond>) <body> More...
 
class  TemplateDeclaration
 template <template-parameters> <declaration> More...
 
class  Token
 A token coming directly from a file or from a macro invocation. More...
 
class  TokenBuffer
 A list of tokens obtained by preprocessing a text buffer and operations to map between the expanded and spelled tokens, i.e. More...
 
class  TokenCollector
 Collects tokens for the main file while running the frontend action. More...
 
class  TrailingReturnType
 Trailing return type after the parameter list, including the arrow token. More...
 
class  Tree
 A node that has children and represents a syntactic language construct. More...
 
class  TreeBuilder
 A helper class for constructing the syntax tree while traversing a clang AST. More...
 
class  TypeAliasDeclaration
 using <name> = <type> More...
 
class  UnaryOperatorExpression
 An abstract class for prefix and postfix unary operators. More...
 
class  UnknownDeclaration
 Declaration of an unknown kind, e.g. not yet supported in syntax trees. More...
 
class  UnknownExpression
 An expression of an unknown kind, i.e. More...
 
class  UnknownStatement
 A statement of an unknown kind, i.e. More...
 
class  UnqualifiedId
 Models an unqualified-id. More...
 
class  UsingDeclaration
 using <scope>::<name> using typename <scope>::<name> More...
 
class  UsingNamespaceDirective
 using namespace <name> More...
 
class  WhileStatement
 while (<cond>) <body> More...
 

Enumerations

enum  NodeKind : uint16_t { NodeKind::CONCRETE_NODE }
 A kind of a syntax node, used for implementing casts. More...
 
enum  NodeRole : uint8_t {
  NodeRole::Detached, NodeRole::Unknown, NodeRole::OpenParen, NodeRole::CloseParen,
  NodeRole::IntroducerKeyword, NodeRole::LiteralToken, NodeRole::ArrowToken, NodeRole::ExternKeyword,
  NodeRole::TemplateKeyword, NodeRole::BodyStatement, NodeRole::ListElement, NodeRole::ListDelimiter,
  NodeRole::OperatorToken, NodeRole::Operand, NodeRole::LeftHandSide, NodeRole::RightHandSide,
  NodeRole::ReturnValue, NodeRole::CaseValue, NodeRole::ThenStatement, NodeRole::ElseKeyword,
  NodeRole::ElseStatement, NodeRole::Expression, NodeRole::Statement, NodeRole::Condition,
  NodeRole::Message, NodeRole::Declarator, NodeRole::Declaration, NodeRole::Size,
  NodeRole::Parameters, NodeRole::TrailingReturn, NodeRole::UnqualifiedId, NodeRole::Qualifier,
  NodeRole::SubExpression, NodeRole::Object, NodeRole::AccessToken, NodeRole::Member,
  NodeRole::Callee, NodeRole::Arguments, NodeRole::Declarators
}
 A relation between a parent and child node, e.g. More...
 

Functions

syntax::TranslationUnit * buildSyntaxTree (Arena &A, ASTContext &Context)
 Build a syntax tree for the main file. More...
 
syntax::LeafcreateLeaf (syntax::Arena &A, tok::TokenKind K, StringRef Spelling)
 Create Leaf from token with Spelling and assert it has the desired TokenKind. More...
 
syntax::LeafcreateLeaf (syntax::Arena &A, tok::TokenKind K)
 Infer the token spelling from its TokenKind, then create Leaf from this token. More...
 
syntax::TreecreateTree (syntax::Arena &A, ArrayRef< std::pair< syntax::Node *, syntax::NodeRole >> Children, syntax::NodeKind K)
 Creates the concrete syntax node according to the specified NodeKind K. More...
 
syntax::EmptyStatementcreateEmptyStatement (syntax::Arena &A)
 
syntax::NodedeepCopyExpandingMacros (syntax::Arena &A, const syntax::Node *N)
 Creates a completely independent copy of N with its macros expanded. More...
 
tooling::Replacements computeReplacements (const Arena &A, const syntax::TranslationUnit &TU)
 Computes textual replacements required to mimic the tree modifications made to the syntax tree. More...
 
void removeStatement (syntax::Arena &A, syntax::Statement *S)
 Removes a statement or replaces it with an empty statement where one is required syntactically. More...
 
raw_ostream & operator<< (raw_ostream &OS, NodeKind K)
 For debugging purposes. More...
 
raw_ostream & operator<< (raw_ostream &OS, NodeRole R)
 For debugging purposes. More...
 
llvm::raw_ostream & operator<< (llvm::raw_ostream &OS, const FileRange &R)
 For debugging purposes. More...
 
llvm::raw_ostream & operator<< (llvm::raw_ostream &OS, const Token &T)
 For debugging purposes. Equivalent to a call to Token::str(). More...
 
llvm::ArrayRef< syntax::TokenspelledTokensTouching (SourceLocation Loc, const syntax::TokenBuffer &Tokens)
 The spelled tokens that overlap or touch a spelling location Loc. More...
 
llvm::ArrayRef< syntax::TokenspelledTokensTouching (SourceLocation Loc, llvm::ArrayRef< syntax::Token > Tokens)
 
const syntax::TokenspelledIdentifierTouching (SourceLocation Loc, llvm::ArrayRef< syntax::Token > Tokens)
 The identifier token that overlaps or touches a spelling location Loc. More...
 
const syntax::TokenspelledIdentifierTouching (SourceLocation Loc, const syntax::TokenBuffer &Tokens)
 
std::vector< syntax::Tokentokenize (FileID FID, const SourceManager &SM, const LangOptions &LO)
 Lex the text buffer, corresponding to FID, in raw mode and record the resulting spelled tokens. More...
 
std::vector< syntax::Tokentokenize (const FileRange &FR, const SourceManager &SM, const LangOptions &LO)
 Similar to one above, instead of whole file tokenizes a part of it. More...
 
bool operator== (const Tree::ConstChildIterator &A, const Tree::ConstChildIterator &B)
 

Enumeration Type Documentation

◆ NodeKind

enum clang::syntax::NodeKind : uint16_t
strong

A kind of a syntax node, used for implementing casts.

The ordering and blocks of enumerator constants must correspond to the inheritance hierarchy of syntax::Node.

Enumerator
CONCRETE_NODE 

Definition at line 37 of file Nodes.h.

◆ NodeRole

enum clang::syntax::NodeRole : uint8_t
strong

A relation between a parent and child node, e.g.

'left-hand-side of a binary expression'. Used for implementing accessors.

In general NodeRoles should be named the same as their accessors.

Some roles describe parent/child relations that occur multiple times in language grammar. We define only one role to describe all instances of such recurring relations. For example, grammar for both "if" and "while" statements requires an opening paren and a closing paren. The opening paren token is assigned the OpenParen role regardless of whether it appears as a child of IfStatement or WhileStatement node. More generally, when grammar requires a certain fixed token (like a specific keyword, or an opening paren), we define a role for this token and use it across all grammar rules with the same requirement. Names of such reusable roles end with a ~Token or a ~Keyword suffix.

Enumerator
Detached 

A node without a parent.

Unknown 

Children of an unknown semantic nature, e.g. skipped tokens, comments.

OpenParen 

An opening parenthesis in argument lists and blocks, e.g. '{', '(', etc.

CloseParen 

A closing parenthesis in argument lists and blocks, e.g. '}', ')', etc.

IntroducerKeyword 

A keywords that introduces some grammar construct, e.g. 'if', 'try', etc.

LiteralToken 

A token that represents a literal, e.g. 'nullptr', '1', 'true', etc.

ArrowToken 

Tokens or Keywords.

ExternKeyword 
TemplateKeyword 
BodyStatement 

An inner statement for those that have only a single child of kind statement, e.g.

loop body for while, for, etc; inner statement for case, default, etc.

ListElement 

List API roles.

ListDelimiter 
OperatorToken 
Operand 
LeftHandSide 
RightHandSide 
ReturnValue 
CaseValue 
ThenStatement 
ElseKeyword 
ElseStatement 
Expression 
Statement 
Condition 
Message 
Declarator 
Declaration 
Size 
Parameters 
TrailingReturn 
UnqualifiedId 
Qualifier 
SubExpression 
Object 
AccessToken 
Member 
Callee 
Arguments 
Declarators 

Definition at line 59 of file Nodes.h.

Function Documentation

◆ buildSyntaxTree()

syntax::TranslationUnit * clang::syntax::buildSyntaxTree ( Arena A,
ASTContext Context 
)

Build a syntax tree for the main file.

This usually covers the whole TranslationUnitDecl, but can be restricted by the ASTContext's traversal scope.

Definition at line 1748 of file BuildTree.cpp.

◆ computeReplacements()

tooling::Replacements clang::syntax::computeReplacements ( const Arena A,
const syntax::TranslationUnit &  TU 
)

Computes textual replacements required to mimic the tree modifications made to the syntax tree.

Definition at line 86 of file ComputeReplacements.cpp.

References clang::tooling::Replacements::add(), emitReplacement(), clang::syntax::Arena::getSourceManager(), clang::syntax::Arena::getTokenBuffer(), SM, and string().

◆ createEmptyStatement()

syntax::EmptyStatement * clang::syntax::createEmptyStatement ( syntax::Arena A)

Definition at line 227 of file Synthesis.cpp.

References createLeaf(), createTree(), and clang::prec::Unknown.

◆ createLeaf() [1/2]

syntax::Leaf * clang::syntax::createLeaf ( syntax::Arena A,
tok::TokenKind  K 
)

Infer the token spelling from its TokenKind, then create Leaf from this token.

Definition at line 52 of file Synthesis.cpp.

References createLeaf(), clang::tok::getKeywordSpelling(), and clang::tok::getPunctuatorSpelling().

◆ createLeaf() [2/2]

syntax::Leaf * clang::syntax::createLeaf ( syntax::Arena A,
tok::TokenKind  K,
StringRef  Spelling 
)

Create Leaf from token with Spelling and assert it has the desired TokenKind.

Definition at line 37 of file Synthesis.cpp.

Referenced by createEmptyStatement(), createLeaf(), and deepCopyExpandingMacros().

◆ createTree()

syntax::Tree * clang::syntax::createTree ( syntax::Arena A,
ArrayRef< std::pair< syntax::Node *, syntax::NodeRole >>  Children,
syntax::NodeKind  K 
)

Creates the concrete syntax node according to the specified NodeKind K.

Returns it as a pointer to the base class Tree.

Definition at line 197 of file Synthesis.cpp.

Referenced by createEmptyStatement(), and deepCopyExpandingMacros().

◆ deepCopyExpandingMacros()

syntax::Node * clang::syntax::deepCopyExpandingMacros ( syntax::Arena A,
const syntax::Node N 
)

Creates a completely independent copy of N with its macros expanded.

The copy is:

  • Detached, i.e. Parent == NextSibling == nullptr and Role == Detached.
  • Synthesized, i.e. Original == false.

Definition at line 210 of file Synthesis.cpp.

References createLeaf(), createTree(), clang::syntax::Node::getKind(), and clang::syntax::Arena::getSourceManager().

◆ operator<<() [1/4]

llvm::raw_ostream & clang::syntax::operator<< ( llvm::raw_ostream &  OS,
const FileRange R 
)

◆ operator<<() [2/4]

llvm::raw_ostream & clang::syntax::operator<< ( llvm::raw_ostream &  OS,
const Token T 
)

For debugging purposes. Equivalent to a call to Token::str().

Definition at line 138 of file Tokens.cpp.

References clang::syntax::Token::str().

◆ operator<<() [3/4]

raw_ostream & clang::syntax::operator<< ( raw_ostream &  OS,
NodeKind  K 
)

For debugging purposes.

Definition at line 13 of file Nodes.cpp.

◆ operator<<() [4/4]

raw_ostream & clang::syntax::operator<< ( raw_ostream &  OS,
NodeRole  R 
)

◆ operator==()

bool clang::syntax::operator== ( const Tree::ConstChildIterator A,
const Tree::ConstChildIterator B 
)
inline

Definition at line 275 of file Tree.h.

◆ removeStatement()

void clang::syntax::removeStatement ( syntax::Arena A,
syntax::Statement S 
)

Removes a statement or replaces it with an empty statement where one is required syntactically.

E.g., in the following example: if (cond) { foo(); } else bar(); One can remove foo(); completely and to remove bar(); we would need to replace it with an empty statement. EXPECTS: S->canModify() == true

Definition at line 80 of file Mutations.cpp.

◆ spelledIdentifierTouching() [1/2]

const syntax::Token * clang::syntax::spelledIdentifierTouching ( SourceLocation  Loc,
const syntax::TokenBuffer Tokens 
)

Definition at line 508 of file Tokens.cpp.

◆ spelledIdentifierTouching() [2/2]

const syntax::Token * clang::syntax::spelledIdentifierTouching ( SourceLocation  Loc,
llvm::ArrayRef< syntax::Token Tokens 
)

The identifier token that overlaps or touches a spelling location Loc.

If there is none, returns nullptr.

Definition at line 498 of file Tokens.cpp.

◆ spelledTokensTouching() [1/2]

llvm::ArrayRef< syntax::Token > clang::syntax::spelledTokensTouching ( SourceLocation  Loc,
const syntax::TokenBuffer Tokens 
)

The spelled tokens that overlap or touch a spelling location Loc.

This always returns 0-2 tokens.

Definition at line 491 of file Tokens.cpp.

◆ spelledTokensTouching() [2/2]

llvm::ArrayRef< syntax::Token > clang::syntax::spelledTokensTouching ( SourceLocation  Loc,
llvm::ArrayRef< syntax::Token Tokens 
)

Definition at line 477 of file Tokens.cpp.

◆ tokenize() [1/2]

std::vector< syntax::Token > clang::syntax::tokenize ( const FileRange FR,
const SourceManager SM,
const LangOptions LO 
)

Similar to one above, instead of whole file tokenizes a part of it.

Note that, the first token might be incomplete if FR.startOffset is not at the beginning of a token, and the last token returned will start before the FR.endOffset but might end after it.

Definition at line 529 of file Tokens.cpp.

◆ tokenize() [2/2]

std::vector< syntax::Token > clang::syntax::tokenize ( FileID  FID,
const SourceManager SM,
const LangOptions LO 
)

Lex the text buffer, corresponding to FID, in raw mode and record the resulting spelled tokens.

Does minimal post-processing on raw identifiers, setting the appropriate token kind (instead of the raw_identifier reported by lexer in raw mode). This is a very low-level function, most users should prefer to use TokenCollector. Lexing in raw mode produces wildly different results from what one might expect when running a C++ frontend, e.g. preprocessor does not run at all. The result will not have a 'eof' token at the end.

Definition at line 562 of file Tokens.cpp.

References SM.