Skip to content

Commit 1e19165

Browse files
author
Eduardo Caldas
committed
[SyntaxTree][Synthesis] Fix allocation in createTree for more general use
Prior to this change `createTree` could not create arbitrary syntax trees. Now it dispatches to the constructor of the concrete syntax tree according to the `NodeKind` passed as argument. This allows reuse inside the Synthesis API. # Please enter the commit message for your changes. Lines starting Differential Revision: https://reviews.llvm.org/D87820
1 parent 7d593d0 commit 1e19165

File tree

2 files changed

+141
-9
lines changed

2 files changed

+141
-9
lines changed

clang/include/clang/Tooling/Syntax/BuildTree.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ syntax::Leaf *createLeaf(syntax::Arena &A, tok::TokenKind K,
3535
syntax::Leaf *createLeaf(syntax::Arena &A, tok::TokenKind K);
3636

3737
// Synthesis of Trees
38+
/// Creates the concrete syntax node according to the specified `NodeKind` `K`.
39+
/// Returns it as a pointer to the base class `Tree`.
3840
syntax::Tree *
39-
createTree(Arena &A,
41+
createTree(syntax::Arena &A,
4042
std::vector<std::pair<syntax::Node *, syntax::NodeRole>> Children,
4143
syntax::NodeKind K);
4244

4345
// Synthesis of Syntax Nodes
44-
clang::syntax::EmptyStatement *createEmptyStatement(clang::syntax::Arena &A);
46+
syntax::EmptyStatement *createEmptyStatement(syntax::Arena &A);
4547

4648
} // namespace syntax
4749
} // namespace clang

clang/lib/Tooling/Syntax/Synthesis.cpp

Lines changed: 137 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,144 @@ syntax::Leaf *clang::syntax::createLeaf(syntax::Arena &A, tok::TokenKind K) {
5252
return createLeaf(A, K, Spelling);
5353
}
5454

55+
namespace {
56+
// Allocates the concrete syntax `Tree` according to its `NodeKind`.
57+
syntax::Tree *allocateTree(syntax::Arena &A, syntax::NodeKind Kind) {
58+
switch (Kind) {
59+
case syntax::NodeKind::Leaf:
60+
assert(false);
61+
case syntax::NodeKind::TranslationUnit:
62+
return new (A.getAllocator()) syntax::TranslationUnit;
63+
case syntax::NodeKind::UnknownExpression:
64+
return new (A.getAllocator()) syntax::UnknownExpression;
65+
case syntax::NodeKind::ParenExpression:
66+
return new (A.getAllocator()) syntax::ParenExpression;
67+
case syntax::NodeKind::ThisExpression:
68+
return new (A.getAllocator()) syntax::ThisExpression;
69+
case syntax::NodeKind::IntegerLiteralExpression:
70+
return new (A.getAllocator()) syntax::IntegerLiteralExpression;
71+
case syntax::NodeKind::CharacterLiteralExpression:
72+
return new (A.getAllocator()) syntax::CharacterLiteralExpression;
73+
case syntax::NodeKind::FloatingLiteralExpression:
74+
return new (A.getAllocator()) syntax::FloatingLiteralExpression;
75+
case syntax::NodeKind::StringLiteralExpression:
76+
return new (A.getAllocator()) syntax::StringLiteralExpression;
77+
case syntax::NodeKind::BoolLiteralExpression:
78+
return new (A.getAllocator()) syntax::BoolLiteralExpression;
79+
case syntax::NodeKind::CxxNullPtrExpression:
80+
return new (A.getAllocator()) syntax::CxxNullPtrExpression;
81+
case syntax::NodeKind::IntegerUserDefinedLiteralExpression:
82+
return new (A.getAllocator()) syntax::IntegerUserDefinedLiteralExpression;
83+
case syntax::NodeKind::FloatUserDefinedLiteralExpression:
84+
return new (A.getAllocator()) syntax::FloatUserDefinedLiteralExpression;
85+
case syntax::NodeKind::CharUserDefinedLiteralExpression:
86+
return new (A.getAllocator()) syntax::CharUserDefinedLiteralExpression;
87+
case syntax::NodeKind::StringUserDefinedLiteralExpression:
88+
return new (A.getAllocator()) syntax::StringUserDefinedLiteralExpression;
89+
case syntax::NodeKind::PrefixUnaryOperatorExpression:
90+
return new (A.getAllocator()) syntax::PrefixUnaryOperatorExpression;
91+
case syntax::NodeKind::PostfixUnaryOperatorExpression:
92+
return new (A.getAllocator()) syntax::PostfixUnaryOperatorExpression;
93+
case syntax::NodeKind::BinaryOperatorExpression:
94+
return new (A.getAllocator()) syntax::BinaryOperatorExpression;
95+
case syntax::NodeKind::UnqualifiedId:
96+
return new (A.getAllocator()) syntax::UnqualifiedId;
97+
case syntax::NodeKind::IdExpression:
98+
return new (A.getAllocator()) syntax::IdExpression;
99+
case syntax::NodeKind::CallExpression:
100+
return new (A.getAllocator()) syntax::CallExpression;
101+
case syntax::NodeKind::UnknownStatement:
102+
return new (A.getAllocator()) syntax::UnknownStatement;
103+
case syntax::NodeKind::DeclarationStatement:
104+
return new (A.getAllocator()) syntax::DeclarationStatement;
105+
case syntax::NodeKind::EmptyStatement:
106+
return new (A.getAllocator()) syntax::EmptyStatement;
107+
case syntax::NodeKind::SwitchStatement:
108+
return new (A.getAllocator()) syntax::SwitchStatement;
109+
case syntax::NodeKind::CaseStatement:
110+
return new (A.getAllocator()) syntax::CaseStatement;
111+
case syntax::NodeKind::DefaultStatement:
112+
return new (A.getAllocator()) syntax::DefaultStatement;
113+
case syntax::NodeKind::IfStatement:
114+
return new (A.getAllocator()) syntax::IfStatement;
115+
case syntax::NodeKind::ForStatement:
116+
return new (A.getAllocator()) syntax::ForStatement;
117+
case syntax::NodeKind::WhileStatement:
118+
return new (A.getAllocator()) syntax::WhileStatement;
119+
case syntax::NodeKind::ContinueStatement:
120+
return new (A.getAllocator()) syntax::ContinueStatement;
121+
case syntax::NodeKind::BreakStatement:
122+
return new (A.getAllocator()) syntax::BreakStatement;
123+
case syntax::NodeKind::ReturnStatement:
124+
return new (A.getAllocator()) syntax::ReturnStatement;
125+
case syntax::NodeKind::RangeBasedForStatement:
126+
return new (A.getAllocator()) syntax::RangeBasedForStatement;
127+
case syntax::NodeKind::ExpressionStatement:
128+
return new (A.getAllocator()) syntax::ExpressionStatement;
129+
case syntax::NodeKind::CompoundStatement:
130+
return new (A.getAllocator()) syntax::CompoundStatement;
131+
case syntax::NodeKind::UnknownDeclaration:
132+
return new (A.getAllocator()) syntax::UnknownDeclaration;
133+
case syntax::NodeKind::EmptyDeclaration:
134+
return new (A.getAllocator()) syntax::EmptyDeclaration;
135+
case syntax::NodeKind::StaticAssertDeclaration:
136+
return new (A.getAllocator()) syntax::StaticAssertDeclaration;
137+
case syntax::NodeKind::LinkageSpecificationDeclaration:
138+
return new (A.getAllocator()) syntax::LinkageSpecificationDeclaration;
139+
case syntax::NodeKind::SimpleDeclaration:
140+
return new (A.getAllocator()) syntax::SimpleDeclaration;
141+
case syntax::NodeKind::TemplateDeclaration:
142+
return new (A.getAllocator()) syntax::TemplateDeclaration;
143+
case syntax::NodeKind::ExplicitTemplateInstantiation:
144+
return new (A.getAllocator()) syntax::ExplicitTemplateInstantiation;
145+
case syntax::NodeKind::NamespaceDefinition:
146+
return new (A.getAllocator()) syntax::NamespaceDefinition;
147+
case syntax::NodeKind::NamespaceAliasDefinition:
148+
return new (A.getAllocator()) syntax::NamespaceAliasDefinition;
149+
case syntax::NodeKind::UsingNamespaceDirective:
150+
return new (A.getAllocator()) syntax::UsingNamespaceDirective;
151+
case syntax::NodeKind::UsingDeclaration:
152+
return new (A.getAllocator()) syntax::UsingDeclaration;
153+
case syntax::NodeKind::TypeAliasDeclaration:
154+
return new (A.getAllocator()) syntax::TypeAliasDeclaration;
155+
case syntax::NodeKind::SimpleDeclarator:
156+
return new (A.getAllocator()) syntax::SimpleDeclarator;
157+
case syntax::NodeKind::ParenDeclarator:
158+
return new (A.getAllocator()) syntax::ParenDeclarator;
159+
case syntax::NodeKind::ArraySubscript:
160+
return new (A.getAllocator()) syntax::ArraySubscript;
161+
case syntax::NodeKind::TrailingReturnType:
162+
return new (A.getAllocator()) syntax::TrailingReturnType;
163+
case syntax::NodeKind::ParametersAndQualifiers:
164+
return new (A.getAllocator()) syntax::ParametersAndQualifiers;
165+
case syntax::NodeKind::MemberPointer:
166+
return new (A.getAllocator()) syntax::MemberPointer;
167+
case syntax::NodeKind::GlobalNameSpecifier:
168+
return new (A.getAllocator()) syntax::GlobalNameSpecifier;
169+
case syntax::NodeKind::DecltypeNameSpecifier:
170+
return new (A.getAllocator()) syntax::DecltypeNameSpecifier;
171+
case syntax::NodeKind::IdentifierNameSpecifier:
172+
return new (A.getAllocator()) syntax::IdentifierNameSpecifier;
173+
case syntax::NodeKind::SimpleTemplateNameSpecifier:
174+
return new (A.getAllocator()) syntax::SimpleTemplateNameSpecifier;
175+
case syntax::NodeKind::NestedNameSpecifier:
176+
return new (A.getAllocator()) syntax::NestedNameSpecifier;
177+
case syntax::NodeKind::MemberExpression:
178+
return new (A.getAllocator()) syntax::MemberExpression;
179+
case syntax::NodeKind::CallArguments:
180+
return new (A.getAllocator()) syntax::CallArguments;
181+
case syntax::NodeKind::ParameterDeclarationList:
182+
return new (A.getAllocator()) syntax::ParameterDeclarationList;
183+
}
184+
llvm_unreachable("unknown node kind");
185+
}
186+
} // namespace
187+
55188
syntax::Tree *clang::syntax::createTree(
56189
syntax::Arena &A,
57190
std::vector<std::pair<syntax::Node *, syntax::NodeRole>> Children,
58191
syntax::NodeKind K) {
59-
auto *T = new (A.getAllocator()) syntax::Tree(K);
192+
auto *T = allocateTree(A, K);
60193
FactoryImpl::setCanModify(T);
61194
for (auto ChildIt = Children.rbegin(); ChildIt != Children.rend();
62195
std::advance(ChildIt, 1))
@@ -67,10 +200,7 @@ syntax::Tree *clang::syntax::createTree(
67200
}
68201

69202
syntax::EmptyStatement *clang::syntax::createEmptyStatement(syntax::Arena &A) {
70-
auto *S = new (A.getAllocator()) syntax::EmptyStatement;
71-
FactoryImpl::setCanModify(S);
72-
FactoryImpl::prependChildLowLevel(S, createLeaf(A, tok::semi),
73-
NodeRole::Unknown);
74-
S->assertInvariants();
75-
return S;
203+
return cast<EmptyStatement>(
204+
createTree(A, {{createLeaf(A, tok::semi), NodeRole::Unknown}},
205+
NodeKind::EmptyStatement));
76206
}

0 commit comments

Comments
 (0)