clang 20.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. 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  TokenBufferTokenManager
 A TokenBuffer-powered token manager. More...
 
class  TokenCollector
 Collects tokens for the main file while running the frontend action. More...
 
class  TokenManager
 Defines interfaces for operating "Token" in the clang syntax-tree. 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 class  NodeKind : uint16_t { CONCRETE_NODE }
 A kind of a syntax node, used for implementing casts. More...
 
enum class  NodeRole : uint8_t {
  Detached , Unknown , OpenParen , CloseParen ,
  IntroducerKeyword , LiteralToken , ArrowToken , ExternKeyword ,
  TemplateKeyword , BodyStatement , ListElement , 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
}
 A relation between a parent and child node, e.g. More...
 

Functions

syntax::TranslationUnit * buildSyntaxTree (Arena &A, TokenBufferTokenManager &TBTM, ASTContext &Context)
 Build a syntax tree for the main file.
 
syntax::LeafcreateLeaf (syntax::Arena &A, TokenBufferTokenManager &TBTM, tok::TokenKind K, StringRef Spelling)
 Create Leaf from token with Spelling and assert it has the desired TokenKind.
 
syntax::LeafcreateLeaf (syntax::Arena &A, TokenBufferTokenManager &TBTM, tok::TokenKind K)
 Infer the token spelling from its TokenKind, then create Leaf from this token.
 
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.
 
syntax::EmptyStatementcreateEmptyStatement (syntax::Arena &A, TokenBufferTokenManager &TBTM)
 
syntax::NodedeepCopyExpandingMacros (syntax::Arena &A, TokenBufferTokenManager &TBTM, const syntax::Node *N)
 Creates a completely independent copy of N with its macros expanded.
 
tooling::Replacements computeReplacements (const TokenBufferTokenManager &TBTM, const syntax::TranslationUnit &TU)
 Computes textual replacements required to mimic the tree modifications made to the syntax tree.
 
void removeStatement (syntax::Arena &A, TokenBufferTokenManager &TBTM, syntax::Statement *S)
 Removes a statement or replaces it with an empty statement where one is required syntactically.
 
raw_ostream & operator<< (raw_ostream &OS, NodeKind K)
 For debugging purposes.
 
raw_ostream & operator<< (raw_ostream &OS, NodeRole R)
 For debugging purposes.
 
llvm::raw_ostream & operator<< (llvm::raw_ostream &OS, const FileRange &R)
 For debugging purposes.
 
llvm::raw_ostream & operator<< (llvm::raw_ostream &OS, const Token &T)
 For debugging purposes. Equivalent to a call to Token::str().
 
llvm::ArrayRef< syntax::TokenspelledTokensTouching (SourceLocation Loc, const syntax::TokenBuffer &Tokens)
 The spelled tokens that overlap or touch a spelling location Loc.
 
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.
 
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.
 
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.
 

Enumeration Type Documentation

◆ NodeKind

enum class 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 32 of file Nodes.h.

◆ NodeRole

enum class 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 54 of file Nodes.h.

Function Documentation

◆ buildSyntaxTree()

syntax::TranslationUnit * clang::syntax::buildSyntaxTree ( Arena A,
TokenBufferTokenManager TBTM,
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 1754 of file BuildTree.cpp.

◆ computeReplacements()

tooling::Replacements clang::syntax::computeReplacements ( const TokenBufferTokenManager TBTM,
const syntax::TranslationUnit &  TU 
)

◆ createEmptyStatement()

syntax::EmptyStatement * clang::syntax::createEmptyStatement ( syntax::Arena A,
TokenBufferTokenManager TBTM 
)

Definition at line 235 of file Synthesis.cpp.

References createLeaf(), and createTree().

Referenced by removeStatement().

◆ createLeaf() [1/2]

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

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

Definition at line 57 of file Synthesis.cpp.

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

◆ createLeaf() [2/2]

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

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

Definition at line 40 of file Synthesis.cpp.

References clang::syntax::Node::assertInvariants(), clang::syntax::Arena::getAllocator(), and clang::syntax::FactoryImpl::setCanModify().

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 204 of file Synthesis.cpp.

References clang::T.

Referenced by createEmptyStatement(), and deepCopyExpandingMacros().

◆ deepCopyExpandingMacros()

syntax::Node * clang::syntax::deepCopyExpandingMacros ( syntax::Arena A,
TokenBufferTokenManager TBTM,
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 217 of file Synthesis.cpp.

References createLeaf(), createTree(), deepCopyExpandingMacros(), clang::syntax::Node::getKind(), clang::syntax::TokenBufferTokenManager::getText(), clang::syntax::TokenBufferTokenManager::getToken(), clang::syntax::Token::kind(), and clang::T.

Referenced by deepCopyExpandingMacros().

◆ 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 180 of file Tokens.cpp.

References clang::T.

◆ 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 
)

For debugging purposes.

Definition at line 23 of file Nodes.cpp.

◆ removeStatement()

void clang::syntax::removeStatement ( syntax::Arena A,
TokenBufferTokenManager TBTM,
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 79 of file Mutations.cpp.

References createEmptyStatement().

◆ spelledIdentifierTouching() [1/2]

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

Definition at line 554 of file Tokens.cpp.

References Loc, and spelledIdentifierTouching().

◆ 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 544 of file Tokens.cpp.

References Loc, and spelledTokensTouching().

Referenced by spelledIdentifierTouching().

◆ 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 537 of file Tokens.cpp.

References Loc, and spelledTokensTouching().

Referenced by spelledIdentifierTouching(), and spelledTokensTouching().

◆ spelledTokensTouching() [2/2]

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

◆ 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 575 of file Tokens.cpp.

References clang::syntax::FileRange::beginOffset(), clang::syntax::FileRange::endOffset(), clang::syntax::FileRange::file(), clang::IdentifierTable::get(), clang::Lexer::getCurrentBufferOffset(), clang::IdentifierInfo::getTokenID(), clang::Lexer::LexFromRawLexer(), SM, and clang::T.

◆ 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 608 of file Tokens.cpp.

References SM, and tokenize().

Referenced by tokenize().