parse_tree_test.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/parser/parse_tree.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <forward_list>
  8. #include "llvm/Support/FormatVariadic.h"
  9. #include "toolchain/common/yaml_test_helpers.h"
  10. #include "toolchain/diagnostics/diagnostic_emitter.h"
  11. #include "toolchain/diagnostics/mocks.h"
  12. #include "toolchain/lexer/tokenized_buffer.h"
  13. namespace Carbon::Testing {
  14. namespace {
  15. using ::testing::AtLeast;
  16. using ::testing::ElementsAre;
  17. using ::testing::Eq;
  18. class ParseTreeTest : public ::testing::Test {
  19. protected:
  20. auto GetSourceBuffer(llvm::Twine t) -> SourceBuffer& {
  21. source_storage.push_front(
  22. std::move(*SourceBuffer::CreateFromText(t.str())));
  23. return source_storage.front();
  24. }
  25. auto GetTokenizedBuffer(llvm::Twine t) -> TokenizedBuffer& {
  26. token_storage.push_front(
  27. TokenizedBuffer::Lex(GetSourceBuffer(t), consumer));
  28. return token_storage.front();
  29. }
  30. std::forward_list<SourceBuffer> source_storage;
  31. std::forward_list<TokenizedBuffer> token_storage;
  32. DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer();
  33. };
  34. TEST_F(ParseTreeTest, DefaultInvalid) {
  35. ParseTree::Node node;
  36. EXPECT_FALSE(node.is_valid());
  37. }
  38. TEST_F(ParseTreeTest, IsValid) {
  39. TokenizedBuffer tokens = GetTokenizedBuffer("");
  40. ParseTree tree = ParseTree::Parse(tokens, consumer);
  41. EXPECT_TRUE((*tree.postorder().begin()).is_valid());
  42. }
  43. TEST_F(ParseTreeTest, OperatorWhitespaceErrors) {
  44. // Test dispositions: Recovered means we issued an error but recovered a
  45. // proper parse tree; Failed means we didn't fully recover from the error.
  46. enum Kind { Valid, Recovered, Failed };
  47. struct Testcase {
  48. const char* input;
  49. Kind kind;
  50. } testcases[] = {
  51. {"var v: Type = i8*;", Valid},
  52. {"var v: Type = i8 *;", Recovered},
  53. {"var v: Type = i8* ;", Valid},
  54. {"var v: Type = i8 * ;", Recovered},
  55. {"var n: i8 = n * n;", Valid},
  56. {"var n: i8 = n*n;", Valid},
  57. {"var n: i8 = (n)*3;", Valid},
  58. {"var n: i8 = 3*(n);", Valid},
  59. {"var n: i8 = n *n;", Recovered},
  60. // TODO: We could figure out that this first Failed example is infix
  61. // with one-token lookahead.
  62. {"var n: i8 = n* n;", Failed},
  63. {"var n: i8 = n* -n;", Failed},
  64. {"var n: i8 = n* *p;", Failed},
  65. // TODO: We try to form (n*)*p and reject due to missing parentheses
  66. // before we notice the missing whitespace around the second `*`.
  67. // It'd be better to (somehow) form n*(*p) and reject due to the missing
  68. // whitespace around the first `*`.
  69. {"var n: i8 = n**p;", Failed},
  70. {"var n: i8 = -n;", Valid},
  71. {"var n: i8 = - n;", Recovered},
  72. {"var n: i8 =-n;", Valid},
  73. {"var n: i8 =- n;", Recovered},
  74. {"var n: i8 = F(i8 *);", Recovered},
  75. {"var n: i8 = F(i8 *, 0);", Recovered},
  76. };
  77. for (auto [input, kind] : testcases) {
  78. TokenizedBuffer tokens = GetTokenizedBuffer(input);
  79. ErrorTrackingDiagnosticConsumer error_tracker(consumer);
  80. ParseTree tree = ParseTree::Parse(tokens, error_tracker);
  81. EXPECT_THAT(tree.has_errors(), Eq(kind == Failed)) << input;
  82. EXPECT_THAT(error_tracker.seen_error(), Eq(kind != Valid)) << input;
  83. }
  84. }
  85. TEST_F(ParseTreeTest, StructErrors) {
  86. struct Testcase {
  87. llvm::StringLiteral input;
  88. ::testing::Matcher<const Diagnostic&> diag_matcher;
  89. };
  90. Testcase testcases[] = {
  91. {"var x: {i32} = {};",
  92. IsDiagnosticMessage("Expected `.field: type` or `.field = value`.")},
  93. {"var x: {a} = {};",
  94. IsDiagnosticMessage("Expected `.field: type` or `.field = value`.")},
  95. {"var x: {a:} = {};",
  96. IsDiagnosticMessage("Expected `.field: type` or `.field = value`.")},
  97. {"var x: {a=} = {};",
  98. IsDiagnosticMessage("Expected `.field: type` or `.field = value`.")},
  99. {"var x: {.} = {};",
  100. IsDiagnosticMessage("Expected identifier after `.`.")},
  101. {"var x: {.\"hello\" = 0, .y = 4} = {};",
  102. IsDiagnosticMessage("Expected identifier after `.`.")},
  103. {"var x: {.\"hello\": i32, .y: i32} = {};",
  104. IsDiagnosticMessage("Expected identifier after `.`.")},
  105. {"var x: {.a} = {};",
  106. IsDiagnosticMessage("Expected `.field: type` or `.field = value`.")},
  107. {"var x: {.a:} = {};", IsDiagnosticMessage("Expected expression.")},
  108. {"var x: {.a=} = {};", IsDiagnosticMessage("Expected expression.")},
  109. {"var x: {.a: i32, .b = 0} = {};",
  110. IsDiagnosticMessage("Expected `.field: type`.")},
  111. {"var x: {.a = 0, b: i32} = {};",
  112. IsDiagnosticMessage("Expected `.field = value`.")},
  113. {"var x: {,} = {};",
  114. IsDiagnosticMessage("Expected `.field: type` or `.field = value`.")},
  115. {"var x: {.a: i32,,} = {};",
  116. IsDiagnosticMessage("Expected `.field: type`.")},
  117. {"var x: {.a = 0,,} = {};",
  118. IsDiagnosticMessage("Expected `.field = value`.")},
  119. {"var x: {.a: i32 banana} = {.a = 0};",
  120. IsDiagnosticMessage("Expected `,` or `}`.")},
  121. {"var x: {.a: i32} = {.a = 0 banana};",
  122. IsDiagnosticMessage("Expected `,` or `}`.")},
  123. };
  124. for (const Testcase& testcase : testcases) {
  125. TokenizedBuffer tokens = GetTokenizedBuffer(testcase.input);
  126. Testing::MockDiagnosticConsumer consumer;
  127. EXPECT_CALL(consumer, HandleDiagnostic(testcase.diag_matcher));
  128. ParseTree tree = ParseTree::Parse(tokens, consumer);
  129. EXPECT_TRUE(tree.has_errors());
  130. }
  131. }
  132. TEST_F(ParseTreeTest, PrintingAsYAML) {
  133. TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  134. ParseTree tree = ParseTree::Parse(tokens, consumer);
  135. EXPECT_FALSE(tree.has_errors());
  136. std::string print_output;
  137. llvm::raw_string_ostream print_stream(print_output);
  138. tree.Print(print_stream);
  139. print_stream.flush();
  140. auto parameter_list = Yaml::SequenceValue{
  141. Yaml::MappingValue{
  142. {"node_index", "2"}, {"kind", "ParameterListEnd"}, {"text", ")"}},
  143. };
  144. auto function_decl = Yaml::SequenceValue{
  145. Yaml::MappingValue{
  146. {"node_index", "0"}, {"kind", "FunctionIntroducer"}, {"text", "fn"}},
  147. Yaml::MappingValue{
  148. {"node_index", "1"}, {"kind", "DeclaredName"}, {"text", "F"}},
  149. Yaml::MappingValue{{"node_index", "3"},
  150. {"kind", "ParameterList"},
  151. {"text", "("},
  152. {"subtree_size", "2"},
  153. {"children", parameter_list}},
  154. };
  155. auto file = Yaml::SequenceValue{
  156. Yaml::MappingValue{{"node_index", "4"},
  157. {"kind", "FunctionDeclaration"},
  158. {"text", ";"},
  159. {"subtree_size", "5"},
  160. {"children", function_decl}},
  161. Yaml::MappingValue{
  162. {"node_index", "5"}, {"kind", "FileEnd"}, {"text", ""}},
  163. };
  164. EXPECT_THAT(Yaml::Value::FromText(print_output), ElementsAre(file));
  165. }
  166. TEST_F(ParseTreeTest, RecursionLimit) {
  167. std::string code = "fn Foo() { return ";
  168. code.append(10000, '(');
  169. code.append(10000, ')');
  170. code += "; }";
  171. TokenizedBuffer tokens = GetTokenizedBuffer(code);
  172. ASSERT_FALSE(tokens.has_errors());
  173. Testing::MockDiagnosticConsumer consumer;
  174. // Recursion might be exceeded multiple times due to quirks in parse tree
  175. // handling; we only need to be sure it's hit at least once for test
  176. // correctness.
  177. EXPECT_CALL(consumer, HandleDiagnostic(IsDiagnosticMessage(
  178. llvm::formatv("Exceeded recursion limit ({0})",
  179. ParseTree::StackDepthLimit)
  180. .str())))
  181. .Times(AtLeast(1));
  182. ParseTree tree = ParseTree::Parse(tokens, consumer);
  183. EXPECT_TRUE(tree.has_errors());
  184. }
  185. TEST_F(ParseTreeTest, ParsePostfixExpressionRegression) {
  186. // Stack depth errors could cause ParsePostfixExpression to infinitely loop
  187. // when calling children and those children error. Because of the fragility of
  188. // stack depth, this tries a few different values.
  189. for (int n = 0; n <= 10; ++n) {
  190. std::string code = "var x: auto = ";
  191. code.append(ParseTree::StackDepthLimit - n, '*');
  192. code += "(z);";
  193. TokenizedBuffer tokens = GetTokenizedBuffer(code);
  194. ASSERT_FALSE(tokens.has_errors());
  195. ParseTree tree = ParseTree::Parse(tokens, consumer);
  196. EXPECT_TRUE(tree.has_errors());
  197. }
  198. }
  199. TEST_F(ParseTreeTest, PackageErrors) {
  200. struct TestCase {
  201. llvm::StringLiteral input;
  202. ::testing::Matcher<const Diagnostic&> diag_matcher;
  203. };
  204. TestCase testcases[] = {
  205. {"package;", IsDiagnosticMessage("Expected identifier after `package`.")},
  206. {"package fn;",
  207. IsDiagnosticMessage("Expected identifier after `package`.")},
  208. {"package library \"Shapes\" api;",
  209. IsDiagnosticMessage("Expected identifier after `package`.")},
  210. {"package Geometry library Shapes api;",
  211. IsDiagnosticMessage(
  212. "Expected a string literal to specify the library name.")},
  213. {"package Geometry \"Shapes\" api;",
  214. IsDiagnosticMessage("Missing `library` keyword.")},
  215. {"package Geometry api",
  216. IsDiagnosticMessage("Expected `;` to end package directive.")},
  217. {"package Geometry;", IsDiagnosticMessage("Expected a `api` or `impl`.")},
  218. {R"(package Foo library "bar" "baz";)",
  219. IsDiagnosticMessage("Expected a `api` or `impl`.")}};
  220. for (const TestCase& testcase : testcases) {
  221. TokenizedBuffer tokens = GetTokenizedBuffer(testcase.input);
  222. Testing::MockDiagnosticConsumer consumer;
  223. EXPECT_CALL(consumer, HandleDiagnostic(testcase.diag_matcher));
  224. ParseTree tree = ParseTree::Parse(tokens, consumer);
  225. EXPECT_TRUE(tree.has_errors());
  226. }
  227. }
  228. } // namespace
  229. } // namespace Carbon::Testing