tree_test.cpp 6.6 KB

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