semantics_ir_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <string>
  7. #include "llvm/Support/MemoryBuffer.h"
  8. #include "llvm/Support/VirtualFileSystem.h"
  9. #include "llvm/Support/raw_ostream.h"
  10. #include "testing/base/test_raw_ostream.h"
  11. #include "toolchain/base/yaml_test_helpers.h"
  12. #include "toolchain/driver/driver.h"
  13. namespace Carbon::Testing {
  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. TEST(SemanticsIRTest, YAML) {
  25. llvm::vfs::InMemoryFileSystem fs;
  26. CARBON_CHECK(fs.addFile(
  27. "test.carbon", /*ModificationTime=*/0,
  28. llvm::MemoryBuffer::getMemBuffer("fn F() { var x: i32 = 0; return; }")));
  29. TestRawOstream print_stream;
  30. Driver d(fs, print_stream, llvm::errs());
  31. d.RunCommand(
  32. {"compile", "--phase=check", "--dump-raw-semantics-ir", "test.carbon"});
  33. // Matches the ID of a node. The numbers may change because of builtin
  34. // cross-references, so this code is only doing loose structural checks.
  35. auto node_id = Yaml::Scalar(MatchesRegex(R"(node\+\d+)"));
  36. auto node_builtin = Yaml::Scalar(MatchesRegex(R"(node\w+)"));
  37. auto type_id = Yaml::Scalar(MatchesRegex(R"(type\d+)"));
  38. EXPECT_THAT(
  39. Yaml::Value::FromText(print_stream.TakeStr()),
  40. ElementsAre(Yaml::Mapping(ElementsAre(
  41. Pair("cross_reference_irs_size", "1"),
  42. Pair("functions", Yaml::Sequence(SizeIs(1))),
  43. Pair("integer_literals", Yaml::Sequence(ElementsAre("0"))),
  44. Pair("real_literals", Yaml::Sequence(IsEmpty())),
  45. Pair("strings", Yaml::Sequence(ElementsAre("F", "x"))),
  46. Pair("types", Yaml::Sequence(ElementsAre(node_builtin))),
  47. Pair("type_blocks", Yaml::Sequence(IsEmpty())),
  48. Pair("nodes",
  49. Yaml::Sequence(AllOf(
  50. // kind is required, other parts are optional.
  51. Each(Yaml::Mapping(Contains(Pair("kind", _)))),
  52. // A 0-arg node.
  53. Contains(Yaml::Mapping(ElementsAre(Pair("kind", "Return")))),
  54. // A 1-arg node.
  55. Contains(Yaml::Mapping(ElementsAre(
  56. Pair("kind", "IntegerLiteral"), Pair("arg0", "int0"),
  57. Pair("type", type_id)))),
  58. // A 2-arg node.
  59. Contains(Yaml::Mapping(ElementsAre(
  60. Pair("kind", "Assign"), Pair("arg0", node_id),
  61. Pair("arg1", node_id))))))),
  62. // This production has only two node blocks.
  63. Pair("node_blocks",
  64. Yaml::Sequence(ElementsAre(Yaml::Sequence(IsEmpty()),
  65. Yaml::Sequence(Each(node_id)),
  66. Yaml::Sequence(Each(node_id)))))))));
  67. }
  68. } // namespace
  69. } // namespace Carbon::Testing