yaml_test.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_id = Yaml::Scalar(MatchesRegex(R"(inst[0-9A-F]+)"));
  43. auto inst_block_id =
  44. Yaml::Scalar(MatchesRegex(R"(inst_block([0-9A-F]+|_empty))"));
  45. auto inst_block = Pair(inst_block_id, Yaml::Mapping(Each(Pair(_, inst_id))));
  46. auto constant_id =
  47. Yaml::Scalar(MatchesRegex(R"(concrete_constant\(inst[0-9A-F]+\))"));
  48. auto type_id =
  49. Yaml::Scalar(MatchesRegex(R"(type\((\w+|inst\(\w+\)|inst[0-9A-F]+)\))"));
  50. auto type_builtin = Pair(type_id, Yaml::Mapping(_));
  51. auto file = Yaml::Mapping(ElementsAre(
  52. Pair("names", Yaml::Mapping(SizeIs(2))),
  53. Pair("import_irs", Yaml::Mapping(SizeIs(2))),
  54. Pair("import_ir_insts", Yaml::Mapping(SizeIs(0))),
  55. Pair("clang_decls", Yaml::Mapping(SizeIs(0))),
  56. Pair("name_scopes", Yaml::Mapping(SizeIs(1))),
  57. Pair("entity_names", Yaml::Mapping(SizeIs(1))),
  58. Pair("cpp_global_vars", Yaml::Mapping(SizeIs(0))),
  59. Pair("functions", Yaml::Mapping(SizeIs(1))),
  60. Pair("classes", Yaml::Mapping(SizeIs(0))),
  61. Pair("interfaces", Yaml::Mapping(SizeIs(0))),
  62. Pair("associated_constants", Yaml::Mapping(SizeIs(0))),
  63. Pair("impls", Yaml::Mapping(SizeIs(0))),
  64. Pair("generics", Yaml::Mapping(SizeIs(0))),
  65. Pair("specifics", Yaml::Mapping(SizeIs(0))),
  66. Pair("specific_interfaces", Yaml::Mapping(SizeIs(0))),
  67. Pair("struct_type_fields", Yaml::Mapping(SizeIs(1))),
  68. Pair("types", Yaml::Mapping(Each(type_builtin))),
  69. Pair("facet_types", Yaml::Mapping(SizeIs(0))),
  70. Pair("insts",
  71. Yaml::Mapping(AllOf(
  72. Each(Key(inst_id)),
  73. // kind is required, other parts are optional.
  74. Each(Pair(_, Yaml::Mapping(Contains(Pair("kind", _))))),
  75. // A 0-arg instruction.
  76. Contains(
  77. Pair(_, Yaml::Mapping(ElementsAre(Pair("kind", "Return"))))),
  78. // A 1-arg instruction.
  79. Contains(Pair(_, Yaml::Mapping(ElementsAre(
  80. Pair("kind", "TupleType"),
  81. Pair("arg0", inst_block_id),
  82. Pair("type", "type(TypeType)"))))),
  83. // A 2-arg instruction.
  84. Contains(Pair(_, Yaml::Mapping(ElementsAre(
  85. Pair("kind", "FunctionDecl"),
  86. Pair("arg0", Yaml::Scalar(MatchesRegex(
  87. "function[0-9A-F]+"))),
  88. Pair("arg1", "inst_block_empty"),
  89. Pair("type", type_id)))))))),
  90. Pair("constant_values",
  91. Yaml::Mapping(ElementsAre(
  92. Pair("values",
  93. Yaml::Mapping(AllOf(Each(Pair(inst_id, constant_id))))),
  94. Pair("symbolic_constants", Yaml::Mapping(SizeIs(0)))))),
  95. Pair("inst_blocks",
  96. Yaml::Mapping(ElementsAre(
  97. Pair("inst_block_empty", Yaml::Mapping(IsEmpty())),
  98. Pair("exports", Yaml::Mapping(Each(Pair(_, inst_id)))),
  99. Pair("generated", Yaml::Mapping(IsEmpty())),
  100. Pair("imports", Yaml::Mapping(IsEmpty())),
  101. Pair("global_init", Yaml::Mapping(IsEmpty())),
  102. // There are 5 non-reserved inst blocks.
  103. inst_block, inst_block, inst_block, inst_block, inst_block))),
  104. Pair("value_stores",
  105. Yaml::Mapping(ElementsAre(
  106. Pair("shared_values",
  107. Yaml::Mapping(ElementsAre(
  108. Pair("ints", Yaml::Mapping(SizeIs(0))),
  109. Pair("reals", Yaml::Mapping(SizeIs(0))),
  110. Pair("floats", Yaml::Mapping(SizeIs(0))),
  111. Pair("identifiers", Yaml::Mapping(SizeIs(2))),
  112. Pair("strings", Yaml::Mapping(SizeIs(0)))))))))));
  113. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  114. ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
  115. std::string print_text = print_stream.TakeStr();
  116. EXPECT_THAT(Yaml::Value::FromText(print_text), IsYaml(root))
  117. << "Actual text:\n"
  118. << print_text;
  119. }
  120. } // namespace
  121. } // namespace Carbon::SemIR