yaml_test.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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::IsEmpty;
  21. using ::testing::MatchesRegex;
  22. using ::testing::Pair;
  23. using ::testing::SizeIs;
  24. namespace Yaml = ::Carbon::Testing::Yaml;
  25. TEST(SemIRTest, Yaml) {
  26. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> fs =
  27. new llvm::vfs::InMemoryFileSystem;
  28. CARBON_CHECK(fs->addFile(
  29. "test.carbon", /*ModificationTime=*/0,
  30. llvm::MemoryBuffer::getMemBuffer("fn F() { let x: () = (); return; }")));
  31. const auto install_paths =
  32. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  33. RawStringOstream print_stream;
  34. Driver driver(fs, &install_paths, /*input_stream=*/nullptr, &print_stream,
  35. &llvm::errs());
  36. auto run_result =
  37. driver.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 inst_block_id =
  43. Yaml::Scalar(MatchesRegex(R"(inst_block([0-9A-F]+|_empty))"));
  44. auto inst_id = Yaml::Scalar(MatchesRegex(R"(inst[0-9A-F]+)"));
  45. auto constant_id =
  46. Yaml::Scalar(MatchesRegex(R"(concrete_constant\(inst[0-9A-F]+\))"));
  47. auto type_id =
  48. Yaml::Scalar(MatchesRegex(R"(type\((\w+|inst\(\w+\)|inst[0-9A-F]+)\))"));
  49. auto type_builtin = Pair(type_id, Yaml::Mapping(_));
  50. auto file = Yaml::Mapping(ElementsAre(
  51. Pair("names", Yaml::Mapping(SizeIs(2))),
  52. Pair("import_irs", Yaml::Mapping(SizeIs(2))),
  53. Pair("import_ir_insts", Yaml::Mapping(SizeIs(0))),
  54. Pair("clang_decls", Yaml::Mapping(SizeIs(0))),
  55. Pair("name_scopes", Yaml::Mapping(SizeIs(1))),
  56. Pair("entity_names", Yaml::Mapping(SizeIs(1))),
  57. Pair("cpp_global_vars", Yaml::Mapping(SizeIs(0))),
  58. Pair("functions", Yaml::Mapping(SizeIs(1))),
  59. Pair("classes", Yaml::Mapping(SizeIs(0))),
  60. Pair("interfaces", Yaml::Mapping(SizeIs(0))),
  61. Pair("associated_constants", Yaml::Mapping(SizeIs(0))),
  62. Pair("impls", Yaml::Mapping(SizeIs(0))),
  63. Pair("generics", Yaml::Mapping(SizeIs(0))),
  64. Pair("specifics", Yaml::Mapping(SizeIs(0))),
  65. Pair("specific_interfaces", Yaml::Mapping(SizeIs(0))),
  66. Pair("struct_type_fields", Yaml::Mapping(SizeIs(1))),
  67. Pair("types", Yaml::Mapping(Each(type_builtin))),
  68. Pair("facet_types", Yaml::Mapping(SizeIs(0))),
  69. Pair("insts", Yaml::Mapping(AllOf(
  70. Each(Key(inst_id)),
  71. // kind is required, other parts are optional.
  72. Each(Pair(_, Yaml::Mapping(Contains(Pair("kind", _))))),
  73. // A 0-arg instruction.
  74. Contains(Pair(_, Yaml::Mapping(ElementsAre(
  75. Pair("kind", "Return"))))),
  76. // A 1-arg instruction.
  77. Contains(Pair(_, Yaml::Mapping(ElementsAre(
  78. Pair("kind", "TupleType"),
  79. Pair("arg0", inst_block_id),
  80. Pair("type", "type(TypeType)"))))),
  81. // A 2-arg instruction.
  82. Contains(Pair(_, Yaml::Mapping(ElementsAre(
  83. Pair("kind", "FunctionDecl"),
  84. Pair("arg0", "function60000000"),
  85. Pair("arg1", "inst_block_empty"),
  86. Pair("type", type_id)))))))),
  87. Pair("constant_values",
  88. Yaml::Mapping(ElementsAre(
  89. Pair("values",
  90. Yaml::Mapping(AllOf(Each(Pair(inst_id, constant_id))))),
  91. Pair("symbolic_constants", Yaml::Mapping(SizeIs(0)))))),
  92. Pair(
  93. "inst_blocks",
  94. Yaml::Mapping(ElementsAre(
  95. Pair("inst_block_empty", Yaml::Mapping(IsEmpty())),
  96. Pair("exports", Yaml::Mapping(Each(Pair(_, inst_id)))),
  97. Pair("imports", Yaml::Mapping(IsEmpty())),
  98. Pair("global_init", Yaml::Mapping(IsEmpty())),
  99. Pair("inst_block60000004", Yaml::Mapping(Each(Pair(_, inst_id)))),
  100. Pair("inst_block60000005", Yaml::Mapping(Each(Pair(_, inst_id)))),
  101. Pair("inst_block60000006", Yaml::Mapping(Each(Pair(_, inst_id)))),
  102. Pair("inst_block60000007", Yaml::Mapping(Each(Pair(_, inst_id)))),
  103. Pair("inst_block60000008",
  104. Yaml::Mapping(Each(Pair(_, inst_id))))))),
  105. Pair("value_stores",
  106. Yaml::Mapping(ElementsAre(
  107. Pair("shared_values",
  108. Yaml::Mapping(ElementsAre(
  109. Pair("ints", Yaml::Mapping(SizeIs(0))),
  110. Pair("reals", Yaml::Mapping(SizeIs(0))),
  111. Pair("floats", Yaml::Mapping(SizeIs(0))),
  112. Pair("identifiers", Yaml::Mapping(SizeIs(2))),
  113. Pair("strings", Yaml::Mapping(SizeIs(0)))))))))));
  114. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  115. ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
  116. std::string print_text = print_stream.TakeStr();
  117. EXPECT_THAT(Yaml::Value::FromText(print_text), IsYaml(root))
  118. << "Actual text:\n"
  119. << print_text;
  120. }
  121. } // namespace
  122. } // namespace Carbon::SemIR