semantics_ir_test.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "toolchain/semantics/semantics_ir.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <forward_list>
  8. #include "toolchain/common/yaml_test_helpers.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. #include "toolchain/lexer/tokenized_buffer.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. using ::testing::_;
  14. using ::testing::AllOf;
  15. using ::testing::Contains;
  16. using ::testing::Each;
  17. using ::testing::ElementsAre;
  18. using ::testing::IsEmpty;
  19. using ::testing::MatchesRegex;
  20. using ::testing::Pair;
  21. TEST(SemanticsIRTest, YAML) {
  22. DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer();
  23. llvm::Expected<SourceBuffer> source =
  24. SourceBuffer::CreateFromText("var x: i32 = 0;");
  25. TokenizedBuffer tokens = TokenizedBuffer::Lex(*source, consumer);
  26. ParseTree parse_tree =
  27. ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  28. SemanticsIR builtin_ir = SemanticsIR::MakeBuiltinIR();
  29. SemanticsIR semantics_ir = SemanticsIR::MakeFromParseTree(
  30. builtin_ir, tokens, parse_tree, consumer, /*vlog_stream=*/nullptr);
  31. std::string print_output;
  32. llvm::raw_string_ostream print_stream(print_output);
  33. semantics_ir.Print(print_stream);
  34. print_stream.flush();
  35. // Matches the ID of a node. The numbers may change because of builtin
  36. // cross-references, so this code is only doing loose structural checks.
  37. auto node_id = Yaml::Scalar(MatchesRegex("node\\d+"));
  38. EXPECT_THAT(
  39. Yaml::Value::FromText(print_output),
  40. ElementsAre(Yaml::Mapping(ElementsAre(
  41. Pair("cross_reference_irs_size", "1"),
  42. Pair("callables", Yaml::Sequence(IsEmpty())),
  43. Pair("integer_literals", Yaml::Sequence(ElementsAre("0"))),
  44. Pair("strings", Yaml::Sequence(ElementsAre("x"))),
  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", node_id)))),
  52. // A 1-arg node.
  53. Contains(Yaml::Mapping(ElementsAre(
  54. Pair("kind", "IntegerLiteral"), Pair("arg0", "int0"),
  55. Pair("type", node_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", node_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