tree_test.cpp 6.6 KB

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