semantics_ir_test.cpp 3.4 KB

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