tree_test.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/tokenized_buffer.h"
  13. #include "toolchain/testing/yaml_test_helpers.h"
  14. namespace Carbon::Parse {
  15. namespace {
  16. using ::Carbon::Testing::TestRawOstream;
  17. using ::testing::ElementsAre;
  18. using ::testing::Pair;
  19. namespace Yaml = ::Carbon::Testing::Yaml;
  20. class TreeTest : public ::testing::Test {
  21. protected:
  22. auto GetSourceBuffer(llvm::StringRef t) -> SourceBuffer& {
  23. CARBON_CHECK(fs_.addFile("test.carbon", /*ModificationTime=*/0,
  24. llvm::MemoryBuffer::getMemBuffer(t)));
  25. source_storage_.push_front(std::move(
  26. *SourceBuffer::CreateFromFile(fs_, "test.carbon", consumer_)));
  27. return source_storage_.front();
  28. }
  29. auto GetTokenizedBuffer(llvm::StringRef t) -> Lex::TokenizedBuffer& {
  30. token_storage_.push_front(Lex::TokenizedBuffer::Lex(
  31. value_stores_, GetSourceBuffer(t), consumer_));
  32. return token_storage_.front();
  33. }
  34. SharedValueStores value_stores_;
  35. llvm::vfs::InMemoryFileSystem fs_;
  36. std::forward_list<SourceBuffer> source_storage_;
  37. std::forward_list<Lex::TokenizedBuffer> token_storage_;
  38. DiagnosticConsumer& consumer_ = ConsoleDiagnosticConsumer();
  39. };
  40. TEST_F(TreeTest, IsValid) {
  41. Lex::TokenizedBuffer& tokens = GetTokenizedBuffer("");
  42. Tree tree = Tree::Parse(tokens, consumer_, /*vlog_stream=*/nullptr);
  43. EXPECT_TRUE((*tree.postorder().begin()).is_valid());
  44. }
  45. TEST_F(TreeTest, PrintPostorderAsYAML) {
  46. Lex::TokenizedBuffer& tokens = GetTokenizedBuffer("fn F();");
  47. Tree tree = Tree::Parse(tokens, consumer_, /*vlog_stream=*/nullptr);
  48. EXPECT_FALSE(tree.has_errors());
  49. TestRawOstream print_stream;
  50. tree.Print(print_stream);
  51. auto file = Yaml::Sequence(ElementsAre(
  52. Yaml::Mapping(ElementsAre(Pair("kind", "FileStart"), Pair("text", ""))),
  53. Yaml::Mapping(
  54. ElementsAre(Pair("kind", "FunctionIntroducer"), Pair("text", "fn"))),
  55. Yaml::Mapping(ElementsAre(Pair("kind", "Name"), Pair("text", "F"))),
  56. Yaml::Mapping(
  57. ElementsAre(Pair("kind", "ParameterListStart"), Pair("text", "("))),
  58. Yaml::Mapping(ElementsAre(Pair("kind", "ParameterList"),
  59. Pair("text", ")"), Pair("subtree_size", "2"))),
  60. Yaml::Mapping(ElementsAre(Pair("kind", "FunctionDeclaration"),
  61. Pair("text", ";"), Pair("subtree_size", "5"))),
  62. Yaml::Mapping(ElementsAre(Pair("kind", "FileEnd"), Pair("text", "")))));
  63. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  64. ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
  65. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  66. IsYaml(ElementsAre(root)));
  67. }
  68. TEST_F(TreeTest, PrintPreorderAsYAML) {
  69. Lex::TokenizedBuffer& tokens = GetTokenizedBuffer("fn F();");
  70. Tree tree = Tree::Parse(tokens, consumer_, /*vlog_stream=*/nullptr);
  71. EXPECT_FALSE(tree.has_errors());
  72. TestRawOstream print_stream;
  73. tree.Print(print_stream, /*preorder=*/true);
  74. auto parameter_list = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  75. ElementsAre(Pair("node_index", "3"), Pair("kind", "ParameterListStart"),
  76. Pair("text", "(")))));
  77. auto function_decl = Yaml::Sequence(ElementsAre(
  78. Yaml::Mapping(ElementsAre(Pair("node_index", "1"),
  79. Pair("kind", "FunctionIntroducer"),
  80. Pair("text", "fn"))),
  81. Yaml::Mapping(ElementsAre(Pair("node_index", "2"), Pair("kind", "Name"),
  82. Pair("text", "F"))),
  83. Yaml::Mapping(ElementsAre(Pair("node_index", "4"),
  84. Pair("kind", "ParameterList"),
  85. Pair("text", ")"), Pair("subtree_size", "2"),
  86. Pair("children", parameter_list)))));
  87. auto file = Yaml::Sequence(ElementsAre(
  88. Yaml::Mapping(ElementsAre(Pair("node_index", "0"),
  89. Pair("kind", "FileStart"), Pair("text", ""))),
  90. Yaml::Mapping(ElementsAre(Pair("node_index", "5"),
  91. Pair("kind", "FunctionDeclaration"),
  92. Pair("text", ";"), Pair("subtree_size", "5"),
  93. Pair("children", function_decl))),
  94. Yaml::Mapping(ElementsAre(Pair("node_index", "6"),
  95. Pair("kind", "FileEnd"), Pair("text", "")))));
  96. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  97. ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
  98. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  99. IsYaml(ElementsAre(root)));
  100. }
  101. TEST_F(TreeTest, HighRecursion) {
  102. std::string code = "fn Foo() { return ";
  103. code.append(10000, '(');
  104. code.append(10000, ')');
  105. code += "; }";
  106. Lex::TokenizedBuffer& tokens = GetTokenizedBuffer(code);
  107. ASSERT_FALSE(tokens.has_errors());
  108. Testing::MockDiagnosticConsumer consumer;
  109. Tree tree = Tree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  110. EXPECT_FALSE(tree.has_errors());
  111. }
  112. } // namespace
  113. } // namespace Carbon::Parse