tree_test.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/parse/tree.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <forward_list>
  8. #include "testing/base/test_raw_ostream.h"
  9. #include "toolchain/base/value_store.h"
  10. #include "toolchain/diagnostics/diagnostic_emitter.h"
  11. #include "toolchain/diagnostics/mocks.h"
  12. #include "toolchain/lex/lex.h"
  13. #include "toolchain/lex/tokenized_buffer.h"
  14. #include "toolchain/parse/parse.h"
  15. #include "toolchain/parse/tree_and_subtrees.h"
  16. #include "toolchain/testing/compile_helper.h"
  17. #include "toolchain/testing/yaml_test_helpers.h"
  18. namespace Carbon::Parse {
  19. namespace {
  20. using ::Carbon::Testing::TestRawOstream;
  21. using ::testing::ElementsAre;
  22. using ::testing::Pair;
  23. namespace Yaml = ::Carbon::Testing::Yaml;
  24. class TreeTest : public ::testing::Test {
  25. protected:
  26. Testing::CompileHelper compile_helper_;
  27. };
  28. TEST_F(TreeTest, IsValid) {
  29. Tree& tree = compile_helper_.GetTree("");
  30. EXPECT_TRUE((*tree.postorder().begin()).is_valid());
  31. }
  32. TEST_F(TreeTest, AsAndTryAs) {
  33. auto [tokens, tree_and_subtrees] =
  34. compile_helper_.GetTokenizedBufferWithTreeAndSubtrees("fn F();");
  35. const auto& tree = tree_and_subtrees.tree();
  36. ASSERT_FALSE(tree.has_errors());
  37. auto it = tree_and_subtrees.roots().begin();
  38. // A FileEnd node, so won't match.
  39. NodeId n = *it;
  40. // NodeIdForKind
  41. std::optional<FunctionDeclId> fn_decl_id = tree.TryAs<FunctionDeclId>(n);
  42. EXPECT_FALSE(fn_decl_id.has_value());
  43. // NodeIdOneOf
  44. std::optional<AnyFunctionDeclId> any_fn_decl_id =
  45. tree.TryAs<AnyFunctionDeclId>(n);
  46. EXPECT_FALSE(any_fn_decl_id.has_value());
  47. // NodeIdInCategory
  48. std::optional<AnyDeclId> any_decl_id = tree.TryAs<AnyDeclId>(n);
  49. EXPECT_FALSE(any_decl_id.has_value());
  50. ++it;
  51. n = *it;
  52. // A FunctionDecl node, so will match.
  53. fn_decl_id = tree.TryAs<FunctionDeclId>(n);
  54. ASSERT_TRUE(fn_decl_id.has_value());
  55. EXPECT_TRUE(*fn_decl_id == n);
  56. // Under normal usage, this function should be used with `auto`, but for
  57. // a test it is nice to verify that it is returning the expected type.
  58. // NOLINTNEXTLINE(modernize-use-auto).
  59. FunctionDeclId fn_decl_id2 = tree.As<FunctionDeclId>(n);
  60. EXPECT_TRUE(*fn_decl_id == fn_decl_id2);
  61. any_fn_decl_id = tree.TryAs<AnyFunctionDeclId>(n);
  62. ASSERT_TRUE(any_fn_decl_id.has_value());
  63. EXPECT_TRUE(*any_fn_decl_id == n);
  64. // NOLINTNEXTLINE(modernize-use-auto).
  65. AnyFunctionDeclId any_fn_decl_id2 = tree.As<AnyFunctionDeclId>(n);
  66. EXPECT_TRUE(*any_fn_decl_id == any_fn_decl_id2);
  67. any_decl_id = tree.TryAs<AnyDeclId>(n);
  68. ASSERT_TRUE(any_decl_id.has_value());
  69. EXPECT_TRUE(*any_decl_id == n);
  70. // NOLINTNEXTLINE(modernize-use-auto).
  71. AnyDeclId any_decl_id2 = tree.As<AnyDeclId>(n);
  72. EXPECT_TRUE(*any_decl_id == any_decl_id2);
  73. }
  74. TEST_F(TreeTest, PrintPostorderAsYAML) {
  75. auto [tokens, tree_and_subtrees] =
  76. compile_helper_.GetTokenizedBufferWithTreeAndSubtrees("fn F();");
  77. EXPECT_FALSE(tree_and_subtrees.tree().has_errors());
  78. TestRawOstream print_stream;
  79. tree_and_subtrees.tree().Print(print_stream);
  80. auto file = Yaml::Sequence(ElementsAre(
  81. Yaml::Mapping(ElementsAre(Pair("kind", "FileStart"), Pair("text", ""))),
  82. Yaml::Mapping(
  83. ElementsAre(Pair("kind", "FunctionIntroducer"), Pair("text", "fn"))),
  84. Yaml::Mapping(
  85. ElementsAre(Pair("kind", "IdentifierName"), Pair("text", "F"))),
  86. Yaml::Mapping(
  87. ElementsAre(Pair("kind", "TuplePatternStart"), Pair("text", "("))),
  88. Yaml::Mapping(ElementsAre(Pair("kind", "TuplePattern"), Pair("text", ")"),
  89. Pair("subtree_size", "2"))),
  90. Yaml::Mapping(ElementsAre(Pair("kind", "FunctionDecl"), Pair("text", ";"),
  91. Pair("subtree_size", "5"))),
  92. Yaml::Mapping(ElementsAre(Pair("kind", "FileEnd"), Pair("text", "")))));
  93. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  94. ElementsAre(Pair("filename", tokens.source().filename().str()),
  95. Pair("parse_tree", file)))));
  96. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  97. IsYaml(ElementsAre(root)));
  98. }
  99. TEST_F(TreeTest, PrintPreorderAsYAML) {
  100. auto [tokens, tree_and_subtrees] =
  101. compile_helper_.GetTokenizedBufferWithTreeAndSubtrees("fn F();");
  102. EXPECT_FALSE(tree_and_subtrees.tree().has_errors());
  103. TestRawOstream print_stream;
  104. tree_and_subtrees.PrintPreorder(print_stream);
  105. auto param_list = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  106. ElementsAre(Pair("node_index", "3"), Pair("kind", "TuplePatternStart"),
  107. Pair("text", "(")))));
  108. auto function_decl = Yaml::Sequence(ElementsAre(
  109. Yaml::Mapping(ElementsAre(Pair("node_index", "1"),
  110. Pair("kind", "FunctionIntroducer"),
  111. Pair("text", "fn"))),
  112. Yaml::Mapping(ElementsAre(Pair("node_index", "2"),
  113. Pair("kind", "IdentifierName"),
  114. Pair("text", "F"))),
  115. Yaml::Mapping(ElementsAre(Pair("node_index", "4"),
  116. Pair("kind", "TuplePattern"), Pair("text", ")"),
  117. Pair("subtree_size", "2"),
  118. Pair("children", param_list)))));
  119. auto file = Yaml::Sequence(ElementsAre(
  120. Yaml::Mapping(ElementsAre(Pair("node_index", "0"),
  121. Pair("kind", "FileStart"), Pair("text", ""))),
  122. Yaml::Mapping(ElementsAre(Pair("node_index", "5"),
  123. Pair("kind", "FunctionDecl"), Pair("text", ";"),
  124. Pair("subtree_size", "5"),
  125. Pair("children", function_decl))),
  126. Yaml::Mapping(ElementsAre(Pair("node_index", "6"),
  127. Pair("kind", "FileEnd"), Pair("text", "")))));
  128. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  129. ElementsAre(Pair("filename", tokens.source().filename().str()),
  130. Pair("parse_tree", file)))));
  131. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  132. IsYaml(ElementsAre(root)));
  133. }
  134. TEST_F(TreeTest, HighRecursion) {
  135. std::string code = "fn Foo() { return ";
  136. code.append(10000, '(');
  137. code.append(10000, ')');
  138. code += "; }";
  139. Lex::TokenizedBuffer& tokens = compile_helper_.GetTokenizedBuffer(code);
  140. ASSERT_FALSE(tokens.has_errors());
  141. Testing::MockDiagnosticConsumer consumer;
  142. Tree tree = Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  143. EXPECT_FALSE(tree.has_errors());
  144. }
  145. } // namespace
  146. } // namespace Carbon::Parse