semantics_ir_test.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/semantics/semantics_ir.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <forward_list>
  8. #include "toolchain/common/yaml_test_helpers.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. #include "toolchain/lexer/tokenized_buffer.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. using ::testing::_;
  14. using ::testing::AllOf;
  15. using ::testing::Contains;
  16. using ::testing::Each;
  17. using ::testing::ElementsAre;
  18. using ::testing::IsEmpty;
  19. using ::testing::MatchesRegex;
  20. using ::testing::Pair;
  21. TEST(SemanticsIRTest, YAML) {
  22. DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer();
  23. llvm::Expected<SourceBuffer> source =
  24. SourceBuffer::CreateFromText("var x: i32 = 0;");
  25. TokenizedBuffer tokens = TokenizedBuffer::Lex(*source, consumer);
  26. ParseTree parse_tree =
  27. ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  28. SemanticsIR builtin_ir = SemanticsIR::MakeBuiltinIR();
  29. SemanticsIR semantics_ir = SemanticsIR::MakeFromParseTree(
  30. builtin_ir, tokens, parse_tree, consumer, /*vlog_stream=*/nullptr);
  31. std::string print_output;
  32. llvm::raw_string_ostream print_stream(print_output);
  33. semantics_ir.Print(print_stream);
  34. print_stream.flush();
  35. // Matches the ID of a node. The numbers may change because of builtin
  36. // cross-references, so this code is only doing loose structural checks.
  37. auto node_id = Yaml::Scalar(MatchesRegex("node\\d+"));
  38. EXPECT_THAT(
  39. Yaml::Value::FromText(print_output),
  40. ElementsAre(Yaml::Mapping(ElementsAre(
  41. Pair("cross_reference_irs_size", "1"),
  42. Pair("calls", Yaml::Sequence(IsEmpty())),
  43. Pair("callables", Yaml::Sequence(IsEmpty())),
  44. Pair("integer_literals", Yaml::Sequence(ElementsAre("0"))),
  45. Pair("real_literals", Yaml::Sequence(IsEmpty())),
  46. Pair("strings", Yaml::Sequence(ElementsAre("x"))),
  47. Pair("nodes",
  48. Yaml::Sequence(AllOf(
  49. // kind is required, other parts are optional.
  50. Each(Yaml::Mapping(Contains(Pair("kind", _)))),
  51. // A 0-arg node.
  52. Contains(Yaml::Mapping(ElementsAre(
  53. Pair("kind", "VarStorage"), Pair("type", node_id)))),
  54. // A 1-arg node.
  55. Contains(Yaml::Mapping(ElementsAre(
  56. Pair("kind", "IntegerLiteral"), Pair("arg0", "int0"),
  57. Pair("type", node_id)))),
  58. // A 2-arg node.
  59. Contains(Yaml::Mapping(ElementsAre(
  60. Pair("kind", "BindName"), Pair("arg0", "str0"),
  61. Pair("arg1", node_id), Pair("type", node_id))))))),
  62. // This production has only one node block.
  63. Pair("node_blocks",
  64. Yaml::Sequence(ElementsAre(Yaml::Sequence(IsEmpty()),
  65. Yaml::Sequence(Each(node_id)))))))));
  66. }
  67. } // namespace
  68. } // namespace Carbon::Testing