tree_test.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, 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(ElementsAre(Pair("kind", "IdentifierNameBeforeParams"),
  90. Pair("text", "F"))),
  91. Yaml::Mapping(ElementsAre(Pair("kind", "ExplicitParamListStart"),
  92. Pair("text", "("))),
  93. Yaml::Mapping(ElementsAre(Pair("kind", "ExplicitParamList"),
  94. Pair("text", ")"), Pair("subtree_size", "2"))),
  95. Yaml::Mapping(ElementsAre(Pair("kind", "FunctionDecl"), Pair("text", ";"),
  96. Pair("subtree_size", "5"))),
  97. Yaml::Mapping(ElementsAre(Pair("kind", "FileEnd"), Pair("text", "")))));
  98. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  99. ElementsAre(Pair("filename", tokens.source().filename().str()),
  100. Pair("parse_tree", file)))));
  101. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  102. IsYaml(ElementsAre(root)));
  103. }
  104. TEST_F(TreeTest, PrintPreorderAsYaml) {
  105. auto [tokens, tree_and_subtrees] =
  106. compile_helper_.GetTokenizedBufferWithTreeAndSubtrees("fn F();");
  107. EXPECT_FALSE(tree_and_subtrees.tree().has_errors());
  108. RawStringOstream print_stream;
  109. tree_and_subtrees.PrintPreorder(print_stream);
  110. auto param_list = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  111. ElementsAre(Pair("node_index", "3"),
  112. Pair("kind", "ExplicitParamListStart"), Pair("text", "(")))));
  113. auto function_decl = Yaml::Sequence(ElementsAre(
  114. Yaml::Mapping(ElementsAre(Pair("node_index", "1"),
  115. Pair("kind", "FunctionIntroducer"),
  116. Pair("text", "fn"))),
  117. Yaml::Mapping(ElementsAre(Pair("node_index", "2"),
  118. Pair("kind", "IdentifierNameBeforeParams"),
  119. Pair("text", "F"))),
  120. Yaml::Mapping(ElementsAre(Pair("node_index", "4"),
  121. Pair("kind", "ExplicitParamList"),
  122. Pair("text", ")"), Pair("subtree_size", "2"),
  123. Pair("children", param_list)))));
  124. auto file = Yaml::Sequence(ElementsAre(
  125. Yaml::Mapping(ElementsAre(Pair("node_index", "0"),
  126. Pair("kind", "FileStart"), Pair("text", ""))),
  127. Yaml::Mapping(ElementsAre(Pair("node_index", "5"),
  128. Pair("kind", "FunctionDecl"), Pair("text", ";"),
  129. Pair("subtree_size", "5"),
  130. Pair("children", function_decl))),
  131. Yaml::Mapping(ElementsAre(Pair("node_index", "6"),
  132. Pair("kind", "FileEnd"), Pair("text", "")))));
  133. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  134. ElementsAre(Pair("filename", tokens.source().filename().str()),
  135. Pair("parse_tree", file)))));
  136. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  137. IsYaml(ElementsAre(root)));
  138. }
  139. TEST_F(TreeTest, HighRecursion) {
  140. std::string code = "fn Foo() { return ";
  141. code.append(10000, '(');
  142. code.append(10000, ')');
  143. code += "; }";
  144. Lex::TokenizedBuffer& tokens = compile_helper_.GetTokenizedBuffer(code);
  145. ASSERT_FALSE(tokens.has_errors());
  146. Testing::MockDiagnosticConsumer consumer;
  147. Parse::ParseOptions options;
  148. options.consumer = &consumer;
  149. Tree tree = Parse(tokens, options);
  150. EXPECT_FALSE(tree.has_errors());
  151. }
  152. } // namespace
  153. } // namespace Carbon::Parse