eduucaldas created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
eduucaldas requested review of this revision.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87526

Files:
  clang/include/clang/Tooling/Syntax/BuildTree.h
  clang/lib/Tooling/Syntax/Synthesis.cpp
  clang/unittests/Tooling/Syntax/SynthesisTest.cpp

Index: clang/unittests/Tooling/Syntax/SynthesisTest.cpp
===================================================================
--- clang/unittests/Tooling/Syntax/SynthesisTest.cpp
+++ clang/unittests/Tooling/Syntax/SynthesisTest.cpp
@@ -80,6 +80,32 @@
   )txt"));
 }
 
+TEST_P(SynthesisTest, Tree_Empty) {
+  buildTree("", GetParam());
+
+  auto *Tree = createEmptyTree(*Arena);
+
+  EXPECT_TRUE(treeDumpEqual(Tree, R"txt(
+UnknownExpression Detached synthesized
+  )txt"));
+}
+
+TEST_P(SynthesisTest, Tree_Simple) {
+  buildTree("", GetParam());
+
+  auto *IdA = createLeaf(*Arena, tok::identifier, "a");
+  auto *Comma = createLeaf(*Arena, tok::comma);
+  auto *IdB = createLeaf(*Arena, tok::identifier, "b");
+  auto *Tree = foldChildren(*Arena, {IdA, Comma, IdB});
+
+  EXPECT_TRUE(treeDumpEqual(Tree, R"txt(
+UnknownExpression Detached synthesized
+|-'a' synthesized
+|-',' synthesized
+`-'b' synthesized
+  )txt"));
+}
+
 TEST_P(SynthesisTest, Statement_EmptyStatement) {
   buildTree("", GetParam());
 
Index: clang/lib/Tooling/Syntax/Synthesis.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Synthesis.cpp
+++ clang/lib/Tooling/Syntax/Synthesis.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 #include "clang/Basic/TokenKinds.h"
 #include "clang/Tooling/Syntax/BuildTree.h"
+#include "clang/Tooling/Syntax/Nodes.h"
 
 using namespace clang;
 
@@ -50,6 +51,42 @@
   return createLeaf(A, K, Spelling);
 }
 
+syntax::Tree *clang::syntax::foldChildren(
+    syntax::Arena &A,
+    std::vector<std::pair<syntax::Node *, syntax::NodeRole>> Children,
+    syntax::NodeKind K) {
+  auto *T = new (A.getAllocator()) syntax::Tree(K);
+  FactoryImpl::setCanModify(T);
+  for (auto ChildIt = Children.rbegin(); ChildIt != Children.rend();
+       std::advance(ChildIt, 1))
+    FactoryImpl::prependChildLowLevel(T, ChildIt->first, ChildIt->second);
+
+  T->assertInvariants();
+  return T;
+}
+
+// For testing purposes
+syntax::Tree *clang::syntax::foldChildren(syntax::Arena &A,
+                                          std::vector<syntax::Node *> Children,
+                                          syntax::NodeKind K) {
+  auto *T = new (A.getAllocator()) syntax::Tree(K);
+  FactoryImpl::setCanModify(T);
+  for (auto ChildIt = Children.rbegin(); ChildIt != Children.rend();
+       std::advance(ChildIt, 1))
+    FactoryImpl::prependChildLowLevel(T, *ChildIt, NodeRole::Unknown);
+
+  T->assertInvariants();
+  return T;
+}
+
+syntax::Tree *clang::syntax::createEmptyTree(syntax::Arena &A,
+                                             syntax::NodeKind K) {
+  auto *T = new (A.getAllocator()) syntax::Tree(K);
+  FactoryImpl::setCanModify(T);
+  T->assertInvariants();
+  return T;
+}
+
 syntax::EmptyStatement *clang::syntax::createEmptyStatement(syntax::Arena &A) {
   auto *S = new (A.getAllocator()) syntax::EmptyStatement;
   FactoryImpl::setCanModify(S);
Index: clang/include/clang/Tooling/Syntax/BuildTree.h
===================================================================
--- clang/include/clang/Tooling/Syntax/BuildTree.h
+++ clang/include/clang/Tooling/Syntax/BuildTree.h
@@ -34,6 +34,15 @@
 /// this token
 syntax::Leaf *createLeaf(syntax::Arena &A, tok::TokenKind K);
 
+// Synthesis of Trees
+syntax::Tree *
+foldChildren(Arena &A, std::vector<std::pair<syntax::Node *, syntax::NodeRole>>,
+             syntax::NodeKind);
+syntax::Tree *foldChildren(Arena &A, std::vector<syntax::Node *> Children,
+                           syntax::NodeKind K = NodeKind::UnknownExpression);
+syntax::Tree *createEmptyTree(Arena &A,
+                              syntax::NodeKind K = NodeKind::UnknownExpression);
+
 // Synthesis of Syntax Nodes
 clang::syntax::EmptyStatement *createEmptyStatement(clang::syntax::Arena &A);
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D87526: [SyntaxTree... Eduardo Caldas via Phabricator via cfe-commits

Reply via email to