tree_test.cpp 6.8 KB

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