yaml_test.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <gmock/gmock.h>
  5. #include <gtest/gtest.h>
  6. #include "common/ostream.h"
  7. #include "common/raw_string_ostream.h"
  8. #include "llvm/Support/MemoryBuffer.h"
  9. #include "llvm/Support/VirtualFileSystem.h"
  10. #include "testing/base/global_exe_path.h"
  11. #include "toolchain/driver/driver.h"
  12. #include "toolchain/testing/yaml_test_helpers.h"
  13. namespace Carbon::SemIR {
  14. namespace {
  15. using ::testing::_;
  16. using ::testing::AllOf;
  17. using ::testing::Contains;
  18. using ::testing::Each;
  19. using ::testing::ElementsAre;
  20. using ::testing::Ge;
  21. using ::testing::IsEmpty;
  22. using ::testing::MatchesRegex;
  23. using ::testing::Pair;
  24. using ::testing::SizeIs;
  25. namespace Yaml = ::Carbon::Testing::Yaml;
  26. TEST(SemIRTest, YAML) {
  27. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> fs =
  28. new llvm::vfs::InMemoryFileSystem;
  29. CARBON_CHECK(fs->addFile(
  30. "test.carbon", /*ModificationTime=*/0,
  31. llvm::MemoryBuffer::getMemBuffer("fn F() { var x: () = (); return; }")));
  32. const auto install_paths =
  33. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  34. RawStringOstream print_stream;
  35. Driver d(fs, &install_paths, print_stream, llvm::errs());
  36. auto run_result =
  37. d.RunCommand({"compile", "--no-prelude-import", "--phase=check",
  38. "--dump-raw-sem-ir", "test.carbon"});
  39. EXPECT_TRUE(run_result.success);
  40. // Matches the ID of an instruction. Instruction counts may change as various
  41. // support changes, so this code is only doing loose structural checks.
  42. auto type_block_id = Yaml::Scalar(MatchesRegex(R"(type_block\d+)"));
  43. auto inst_id = Yaml::Scalar(MatchesRegex(R"(inst\d+)"));
  44. auto constant_id =
  45. Yaml::Scalar(MatchesRegex(R"(template_constant\(inst(\w+|\+\d+)\))"));
  46. auto inst_builtin = Yaml::Scalar(MatchesRegex(R"(inst\(\w+\))"));
  47. auto type_id =
  48. Yaml::Scalar(MatchesRegex(R"(type\((\w+|inst\(\w+\)|inst\d+)\))"));
  49. auto type_builtin = Pair(type_id, Yaml::Mapping(_));
  50. auto file = Yaml::Mapping(ElementsAre(
  51. Pair("import_irs", Yaml::Mapping(SizeIs(1))),
  52. Pair("import_ir_insts", Yaml::Mapping(SizeIs(0))),
  53. Pair("name_scopes", Yaml::Mapping(SizeIs(1))),
  54. Pair("entity_names", Yaml::Mapping(SizeIs(1))),
  55. Pair("functions", Yaml::Mapping(SizeIs(1))),
  56. Pair("classes", Yaml::Mapping(SizeIs(0))),
  57. Pair("generics", Yaml::Mapping(SizeIs(0))),
  58. Pair("specifics", Yaml::Mapping(SizeIs(0))),
  59. Pair("struct_type_fields", Yaml::Mapping(SizeIs(1))),
  60. Pair("types", Yaml::Mapping(Each(type_builtin))),
  61. Pair("type_blocks", Yaml::Mapping(SizeIs(Ge(1)))),
  62. Pair("insts",
  63. Yaml::Mapping(AllOf(
  64. Each(Key(inst_id)),
  65. // kind is required, other parts are optional.
  66. Each(Pair(_, Yaml::Mapping(Contains(Pair("kind", _))))),
  67. // A 0-arg instruction.
  68. Contains(
  69. Pair(_, Yaml::Mapping(ElementsAre(Pair("kind", "Return"))))),
  70. // A 1-arg instruction.
  71. Contains(Pair(_, Yaml::Mapping(ElementsAre(
  72. Pair("kind", "TupleType"),
  73. Pair("arg0", type_block_id),
  74. Pair("type", "type(TypeType)"))))),
  75. // A 2-arg instruction.
  76. Contains(Pair(
  77. _, Yaml::Mapping(ElementsAre(Pair("kind", "Assign"),
  78. Pair("arg0", inst_id),
  79. Pair("arg1", inst_id)))))))),
  80. Pair("constant_values",
  81. Yaml::Mapping(AllOf(Each(Pair(inst_id, constant_id))))),
  82. Pair("symbolic_constants", Yaml::Mapping(SizeIs(0))),
  83. Pair("inst_blocks",
  84. Yaml::Mapping(ElementsAre(
  85. Pair("inst_block_empty", Yaml::Mapping(IsEmpty())),
  86. Pair("exports", Yaml::Mapping(Each(Pair(_, inst_id)))),
  87. Pair("import_refs", Yaml::Mapping(IsEmpty())),
  88. Pair("global_init", Yaml::Mapping(IsEmpty())),
  89. Pair("inst_block4", Yaml::Mapping(Each(Pair(_, inst_id)))),
  90. Pair("inst_block5", Yaml::Mapping(Each(Pair(_, inst_id)))),
  91. Pair("inst_block6", Yaml::Mapping(Each(Pair(_, inst_id)))),
  92. Pair("inst_block7", Yaml::Mapping(Each(Pair(_, inst_id)))),
  93. Pair("inst_block8", Yaml::Mapping(Each(Pair(_, inst_id)))))))));
  94. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  95. ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
  96. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()), IsYaml(root));
  97. }
  98. } // namespace
  99. } // namespace Carbon::SemIR