tree_test.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/testing/yaml_test_helpers.h"
  16. namespace Carbon::Parse {
  17. namespace {
  18. using ::Carbon::Testing::TestRawOstream;
  19. using ::testing::ElementsAre;
  20. using ::testing::Pair;
  21. namespace Yaml = ::Carbon::Testing::Yaml;
  22. class TreeTest : public ::testing::Test {
  23. protected:
  24. auto GetSourceBuffer(llvm::StringRef t) -> SourceBuffer& {
  25. CARBON_CHECK(fs_.addFile("test.carbon", /*ModificationTime=*/0,
  26. llvm::MemoryBuffer::getMemBuffer(t)));
  27. source_storage_.push_front(std::move(
  28. *SourceBuffer::CreateFromFile(fs_, "test.carbon", consumer_)));
  29. return source_storage_.front();
  30. }
  31. auto GetTokenizedBuffer(llvm::StringRef t) -> Lex::TokenizedBuffer& {
  32. token_storage_.push_front(
  33. Lex::Lex(value_stores_, GetSourceBuffer(t), consumer_));
  34. return token_storage_.front();
  35. }
  36. SharedValueStores value_stores_;
  37. llvm::vfs::InMemoryFileSystem fs_;
  38. std::forward_list<SourceBuffer> source_storage_;
  39. std::forward_list<Lex::TokenizedBuffer> token_storage_;
  40. DiagnosticConsumer& consumer_ = ConsoleDiagnosticConsumer();
  41. };
  42. TEST_F(TreeTest, IsValid) {
  43. Lex::TokenizedBuffer& tokens = GetTokenizedBuffer("");
  44. Tree tree = Parse(tokens, consumer_, /*vlog_stream=*/nullptr);
  45. EXPECT_TRUE((*tree.postorder().begin()).is_valid());
  46. }
  47. TEST_F(TreeTest, AsAndTryAs) {
  48. Lex::TokenizedBuffer& tokens = GetTokenizedBuffer("fn F();");
  49. Tree tree = Parse(tokens, consumer_, /*vlog_stream=*/nullptr);
  50. ASSERT_FALSE(tree.has_errors());
  51. auto it = tree.roots().begin();
  52. // A FileEnd node, so won't match.
  53. NodeId n = *it;
  54. // NodeIdForKind
  55. std::optional<FunctionDeclId> fn_decl_id = tree.TryAs<FunctionDeclId>(n);
  56. EXPECT_FALSE(fn_decl_id.has_value());
  57. // NodeIdOneOf
  58. std::optional<AnyFunctionDeclId> any_fn_decl_id =
  59. tree.TryAs<AnyFunctionDeclId>(n);
  60. EXPECT_FALSE(any_fn_decl_id.has_value());
  61. // NodeIdInCategory
  62. std::optional<AnyDeclId> any_decl_id = tree.TryAs<AnyDeclId>(n);
  63. EXPECT_FALSE(any_decl_id.has_value());
  64. ++it;
  65. n = *it;
  66. // A FunctionDecl node, so will match.
  67. fn_decl_id = tree.TryAs<FunctionDeclId>(n);
  68. ASSERT_TRUE(fn_decl_id.has_value());
  69. EXPECT_TRUE(*fn_decl_id == n);
  70. // Under normal usage, this function should be used with `auto`, but for
  71. // a test it is nice to verify that it is returning the expected type.
  72. // NOLINTNEXTLINE(modernize-use-auto).
  73. FunctionDeclId fn_decl_id2 = tree.As<FunctionDeclId>(n);
  74. EXPECT_TRUE(*fn_decl_id == fn_decl_id2);
  75. any_fn_decl_id = tree.TryAs<AnyFunctionDeclId>(n);
  76. ASSERT_TRUE(any_fn_decl_id.has_value());
  77. EXPECT_TRUE(*any_fn_decl_id == n);
  78. // NOLINTNEXTLINE(modernize-use-auto).
  79. AnyFunctionDeclId any_fn_decl_id2 = tree.As<AnyFunctionDeclId>(n);
  80. EXPECT_TRUE(*any_fn_decl_id == any_fn_decl_id2);
  81. any_decl_id = tree.TryAs<AnyDeclId>(n);
  82. ASSERT_TRUE(any_decl_id.has_value());
  83. EXPECT_TRUE(*any_decl_id == n);
  84. // NOLINTNEXTLINE(modernize-use-auto).
  85. AnyDeclId any_decl_id2 = tree.As<AnyDeclId>(n);
  86. EXPECT_TRUE(*any_decl_id == any_decl_id2);
  87. }
  88. TEST_F(TreeTest, PrintPostorderAsYAML) {
  89. Lex::TokenizedBuffer& tokens = GetTokenizedBuffer("fn F();");
  90. Tree tree = Parse(tokens, consumer_, /*vlog_stream=*/nullptr);
  91. EXPECT_FALSE(tree.has_errors());
  92. TestRawOstream print_stream;
  93. tree.Print(print_stream);
  94. auto file = Yaml::Sequence(ElementsAre(
  95. Yaml::Mapping(ElementsAre(Pair("kind", "FileStart"), Pair("text", ""))),
  96. Yaml::Mapping(
  97. ElementsAre(Pair("kind", "FunctionIntroducer"), Pair("text", "fn"))),
  98. Yaml::Mapping(
  99. ElementsAre(Pair("kind", "IdentifierName"), Pair("text", "F"))),
  100. Yaml::Mapping(
  101. ElementsAre(Pair("kind", "TuplePatternStart"), Pair("text", "("))),
  102. Yaml::Mapping(ElementsAre(Pair("kind", "TuplePattern"), Pair("text", ")"),
  103. Pair("subtree_size", "2"))),
  104. Yaml::Mapping(ElementsAre(Pair("kind", "FunctionDecl"), Pair("text", ";"),
  105. Pair("subtree_size", "5"))),
  106. Yaml::Mapping(ElementsAre(Pair("kind", "FileEnd"), Pair("text", "")))));
  107. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  108. ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
  109. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  110. IsYaml(ElementsAre(root)));
  111. }
  112. TEST_F(TreeTest, PrintPreorderAsYAML) {
  113. Lex::TokenizedBuffer& tokens = GetTokenizedBuffer("fn F();");
  114. Tree tree = Parse(tokens, consumer_, /*vlog_stream=*/nullptr);
  115. EXPECT_FALSE(tree.has_errors());
  116. TestRawOstream print_stream;
  117. tree.Print(print_stream, /*preorder=*/true);
  118. auto param_list = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  119. ElementsAre(Pair("node_index", "3"), Pair("kind", "TuplePatternStart"),
  120. Pair("text", "(")))));
  121. auto function_decl = Yaml::Sequence(ElementsAre(
  122. Yaml::Mapping(ElementsAre(Pair("node_index", "1"),
  123. Pair("kind", "FunctionIntroducer"),
  124. Pair("text", "fn"))),
  125. Yaml::Mapping(ElementsAre(Pair("node_index", "2"),
  126. Pair("kind", "IdentifierName"),
  127. Pair("text", "F"))),
  128. Yaml::Mapping(ElementsAre(Pair("node_index", "4"),
  129. Pair("kind", "TuplePattern"), Pair("text", ")"),
  130. Pair("subtree_size", "2"),
  131. Pair("children", param_list)))));
  132. auto file = Yaml::Sequence(ElementsAre(
  133. Yaml::Mapping(ElementsAre(Pair("node_index", "0"),
  134. Pair("kind", "FileStart"), Pair("text", ""))),
  135. Yaml::Mapping(ElementsAre(Pair("node_index", "5"),
  136. Pair("kind", "FunctionDecl"), Pair("text", ";"),
  137. Pair("subtree_size", "5"),
  138. Pair("children", function_decl))),
  139. Yaml::Mapping(ElementsAre(Pair("node_index", "6"),
  140. Pair("kind", "FileEnd"), Pair("text", "")))));
  141. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  142. ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
  143. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  144. IsYaml(ElementsAre(root)));
  145. }
  146. TEST_F(TreeTest, HighRecursion) {
  147. std::string code = "fn Foo() { return ";
  148. code.append(10000, '(');
  149. code.append(10000, ')');
  150. code += "; }";
  151. Lex::TokenizedBuffer& tokens = GetTokenizedBuffer(code);
  152. ASSERT_FALSE(tokens.has_errors());
  153. Testing::MockDiagnosticConsumer consumer;
  154. Tree tree = Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  155. EXPECT_FALSE(tree.has_errors());
  156. }
  157. } // namespace
  158. } // namespace Carbon::Parse