semantics_ir_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. TEST(SemanticsIRTest, YAML) {
  24. llvm::vfs::InMemoryFileSystem fs;
  25. CARBON_CHECK(fs.addFile("test.carbon", /*ModificationTime=*/0,
  26. llvm::MemoryBuffer::getMemBuffer("var x: i32 = 0;")));
  27. TestRawOstream print_stream;
  28. Driver d(fs, print_stream, llvm::errs());
  29. d.RunFullCommand({"dump", "raw-semantics-ir", "test.carbon"});
  30. // Matches the ID of a node. The numbers may change because of builtin
  31. // cross-references, so this code is only doing loose structural checks.
  32. auto node_id = Yaml::Scalar(MatchesRegex(R"(node\+\d+)"));
  33. auto node_builtin = Yaml::Scalar(MatchesRegex(R"(node\w+)"));
  34. auto type_id = Yaml::Scalar(MatchesRegex(R"(type\d+)"));
  35. EXPECT_THAT(
  36. Yaml::Value::FromText(print_stream.TakeStr()),
  37. ElementsAre(Yaml::Mapping(ElementsAre(
  38. Pair("cross_reference_irs_size", "1"),
  39. Pair("functions", Yaml::Sequence(IsEmpty())),
  40. Pair("integer_literals", Yaml::Sequence(ElementsAre("0"))),
  41. Pair("real_literals", Yaml::Sequence(IsEmpty())),
  42. Pair("strings", Yaml::Sequence(ElementsAre("x"))),
  43. Pair("types", Yaml::Sequence(ElementsAre(node_builtin))),
  44. Pair("type_blocks", Yaml::Sequence(IsEmpty())),
  45. Pair("nodes",
  46. Yaml::Sequence(AllOf(
  47. // kind is required, other parts are optional.
  48. Each(Yaml::Mapping(Contains(Pair("kind", _)))),
  49. // A 0-arg node.
  50. Contains(Yaml::Mapping(ElementsAre(
  51. Pair("kind", "VarStorage"), Pair("type", type_id)))),
  52. // A 1-arg node.
  53. Contains(Yaml::Mapping(ElementsAre(
  54. Pair("kind", "IntegerLiteral"), Pair("arg0", "int0"),
  55. Pair("type", type_id)))),
  56. // A 2-arg node.
  57. Contains(Yaml::Mapping(ElementsAre(
  58. Pair("kind", "BindName"), Pair("arg0", "str0"),
  59. Pair("arg1", node_id), Pair("type", type_id))))))),
  60. // This production has only one node block.
  61. Pair("node_blocks",
  62. Yaml::Sequence(ElementsAre(Yaml::Sequence(IsEmpty()),
  63. Yaml::Sequence(Each(node_id)))))))));
  64. }
  65. } // namespace
  66. } // namespace Carbon::Testing