aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Tooling/Syntax/TreeTest.cpp
diff options
context:
space:
mode:
authorEduardo Caldas <ecaldas@google.com>2020-09-16 08:03:03 +0000
committerEduardo Caldas <ecaldas@google.com>2020-09-22 06:47:36 +0000
commitaf582c9b0f3a09b6a1b5101fd30dcbcef5c188b0 (patch)
treea1a0c4d767d4d1995db01f0841a4e57f589b4e89 /clang/unittests/Tooling/Syntax/TreeTest.cpp
parent1dc7836aed134b4543bad6aa54f15cc0e51a627f (diff)
downloadllvm-af582c9b0f3a09b6a1b5101fd30dcbcef5c188b0.zip
llvm-af582c9b0f3a09b6a1b5101fd30dcbcef5c188b0.tar.gz
llvm-af582c9b0f3a09b6a1b5101fd30dcbcef5c188b0.tar.bz2
[SyntaxTree] Test `findFirstLeaf` and `findLastLeaf`
* Introduce `TreeTest.cpp` to unit test `Tree.h` * Add `generateAllTreesWithShape` to generating test cases * Add tests for `findFirstLeaf` and `findLastLeaf` * Fix implementations of `findFirstLeaf` and `findLastLeaf` that had been broken when empty `Tree` were present. Differential Revision: https://reviews.llvm.org/D87779
Diffstat (limited to 'clang/unittests/Tooling/Syntax/TreeTest.cpp')
-rw-r--r--clang/unittests/Tooling/Syntax/TreeTest.cpp125
1 files changed, 125 insertions, 0 deletions
diff --git a/clang/unittests/Tooling/Syntax/TreeTest.cpp b/clang/unittests/Tooling/Syntax/TreeTest.cpp
new file mode 100644
index 0000000..2448db3
--- /dev/null
+++ b/clang/unittests/Tooling/Syntax/TreeTest.cpp
@@ -0,0 +1,125 @@
+//===- TreeTest.cpp ---------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Tooling/Syntax/Tree.h"
+#include "TreeTestBase.h"
+#include "clang/Tooling/Syntax/BuildTree.h"
+#include "gtest/gtest.h"
+
+using namespace clang;
+using namespace clang::syntax;
+
+namespace {
+
+class TreeTest : public SyntaxTreeTest {
+private:
+ Tree *createTree(ArrayRef<const Node *> Children) {
+ std::vector<std::pair<Node *, NodeRole>> ChildrenWithRoles;
+ ChildrenWithRoles.reserve(Children.size());
+ for (const auto *Child : Children) {
+ ChildrenWithRoles.push_back(
+ std::make_pair(deepCopy(*Arena, Child), NodeRole::Unknown));
+ }
+ return clang::syntax::createTree(*Arena, ChildrenWithRoles,
+ NodeKind::UnknownExpression);
+ }
+
+ // Generate Forests by combining `Children` into `ParentCount` Trees.
+ //
+ // We do this recursively.
+ std::vector<std::vector<const Tree *>>
+ generateAllForests(ArrayRef<const Node *> Children, unsigned ParentCount) {
+ assert(ParentCount > 0);
+ // If there is only one Parent node, then combine `Children` under
+ // this Parent.
+ if (ParentCount == 1)
+ return {{createTree(Children)}};
+
+ // Otherwise, combine `ChildrenCount` children under the last parent and
+ // solve the smaller problem without these children and this parent. Do this
+ // for every `ChildrenCount` and combine the results.
+ std::vector<std::vector<const Tree *>> AllForests;
+ for (unsigned ChildrenCount = 0; ChildrenCount <= Children.size();
+ ++ChildrenCount) {
+ auto *LastParent = createTree(Children.take_back(ChildrenCount));
+ for (auto &Forest : generateAllForests(Children.drop_back(ChildrenCount),
+ ParentCount - 1)) {
+ Forest.push_back(LastParent);
+ AllForests.push_back(Forest);
+ }
+ }
+ return AllForests;
+ }
+
+protected:
+ // Generates all trees with a `Base` of `Node`s and `NodeCountPerLayer`
+ // `Node`s per layer. An example of Tree with `Base` = {`(`, `)`} and
+ // `NodeCountPerLayer` = {2, 2}:
+ // Tree
+ // |-Tree
+ // `-Tree
+ // |-Tree
+ // | `-'('
+ // `-Tree
+ // `-')'
+ std::vector<const Tree *>
+ generateAllTreesWithShape(ArrayRef<const Node *> Base,
+ ArrayRef<unsigned> NodeCountPerLayer) {
+ // We compute the solution per layer. A layer is a collection of bases,
+ // where each base has the same number of nodes, given by
+ // `NodeCountPerLayer`.
+ auto GenerateNextLayer = [this](ArrayRef<std::vector<const Node *>> Layer,
+ unsigned NextLayerNodeCount) {
+ std::vector<std::vector<const Node *>> NextLayer;
+ for (const auto &Base : Layer) {
+ for (const auto &NextBase :
+ generateAllForests(Base, NextLayerNodeCount)) {
+ NextLayer.push_back(
+ std::vector<const Node *>(NextBase.begin(), NextBase.end()));
+ }
+ }
+ return NextLayer;
+ };
+
+ std::vector<std::vector<const Node *>> Layer = {Base};
+ for (auto NodeCount : NodeCountPerLayer)
+ Layer = GenerateNextLayer(Layer, NodeCount);
+
+ std::vector<const Tree *> AllTrees;
+ AllTrees.reserve(Layer.size());
+ for (const auto &Base : Layer)
+ AllTrees.push_back(createTree(Base));
+
+ return AllTrees;
+ }
+};
+
+INSTANTIATE_TEST_CASE_P(TreeTests, TreeTest,
+ ::testing::ValuesIn(allTestClangConfigs()), );
+
+TEST_P(TreeTest, FirstLeaf) {
+ buildTree("", GetParam());
+ std::vector<const Node *> Leafs = {createLeaf(*Arena, tok::l_paren),
+ createLeaf(*Arena, tok::r_paren)};
+ for (const auto *Tree : generateAllTreesWithShape(Leafs, {3u})) {
+ ASSERT_TRUE(Tree->findFirstLeaf() != nullptr);
+ EXPECT_EQ(Tree->findFirstLeaf()->getToken()->kind(), tok::l_paren);
+ }
+}
+
+TEST_P(TreeTest, LastLeaf) {
+ buildTree("", GetParam());
+ std::vector<const Node *> Leafs = {createLeaf(*Arena, tok::l_paren),
+ createLeaf(*Arena, tok::r_paren)};
+ for (const auto *Tree : generateAllTreesWithShape(Leafs, {3u})) {
+ ASSERT_TRUE(Tree->findLastLeaf() != nullptr);
+ EXPECT_EQ(Tree->findLastLeaf()->getToken()->kind(), tok::r_paren);
+ }
+}
+
+} // namespace