semantics_ir_test.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/util/test_raw_ostream.h"
  11. #include "toolchain/common/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", "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("nodes",
  45. Yaml::Sequence(AllOf(
  46. // kind is required, other parts are optional.
  47. Each(Yaml::Mapping(Contains(Pair("kind", _)))),
  48. // A 0-arg node.
  49. Contains(Yaml::Mapping(ElementsAre(
  50. Pair("kind", "VarStorage"), Pair("type", type_id)))),
  51. // A 1-arg node.
  52. Contains(Yaml::Mapping(ElementsAre(
  53. Pair("kind", "IntegerLiteral"), Pair("arg0", "int0"),
  54. Pair("type", type_id)))),
  55. // A 2-arg node.
  56. Contains(Yaml::Mapping(ElementsAre(
  57. Pair("kind", "BindName"), Pair("arg0", "str0"),
  58. Pair("arg1", node_id), Pair("type", type_id))))))),
  59. // This production has only one node block.
  60. Pair("node_blocks",
  61. Yaml::Sequence(ElementsAre(Yaml::Sequence(IsEmpty()),
  62. Yaml::Sequence(Each(node_id)))))))));
  63. }
  64. } // namespace
  65. } // namespace Carbon::Testing