semantics_ir_test.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "gmock/gmock.h"
  9. #include "gtest/gtest.h"
  10. #include "toolchain/common/yaml_test_helpers.h"
  11. #include "toolchain/diagnostics/diagnostic_emitter.h"
  12. #include "toolchain/lexer/tokenized_buffer.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. DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer();
  25. llvm::Expected<SourceBuffer> source =
  26. SourceBuffer::CreateFromText("var x: i32 = 0;");
  27. TokenizedBuffer tokens = TokenizedBuffer::Lex(*source, consumer);
  28. ParseTree parse_tree =
  29. ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  30. SemanticsIR builtin_ir = SemanticsIR::MakeBuiltinIR();
  31. SemanticsIR semantics_ir = SemanticsIR::MakeFromParseTree(
  32. builtin_ir, tokens, parse_tree, consumer, /*vlog_stream=*/nullptr);
  33. std::string print_output;
  34. llvm::raw_string_ostream print_stream(print_output);
  35. semantics_ir.Print(print_stream);
  36. print_stream.flush();
  37. // Matches the ID of a node. The numbers may change because of builtin
  38. // cross-references, so this code is only doing loose structural checks.
  39. auto node_id = Yaml::Scalar(MatchesRegex("node\\d+"));
  40. EXPECT_THAT(
  41. Yaml::Value::FromText(print_output),
  42. ElementsAre(Yaml::Mapping(ElementsAre(
  43. Pair("cross_reference_irs_size", "1"),
  44. Pair("callables", Yaml::Sequence(IsEmpty())),
  45. Pair("integer_literals", Yaml::Sequence(ElementsAre("0"))),
  46. Pair("strings", Yaml::Sequence(ElementsAre("x"))),
  47. Pair("nodes",
  48. Yaml::Sequence(AllOf(
  49. // kind is required, other parts are optional.
  50. Each(Yaml::Mapping(Contains(Pair("kind", _)))),
  51. // A 0-arg node.
  52. Contains(Yaml::Mapping(ElementsAre(
  53. Pair("kind", "VarStorage"), Pair("type", node_id)))),
  54. // A 1-arg node.
  55. Contains(Yaml::Mapping(ElementsAre(
  56. Pair("kind", "IntegerLiteral"), Pair("arg0", "int0"),
  57. Pair("type", node_id)))),
  58. // A 2-arg node.
  59. Contains(Yaml::Mapping(ElementsAre(
  60. Pair("kind", "BindName"), Pair("arg0", "str0"),
  61. Pair("arg1", node_id), Pair("type", node_id))))))),
  62. // This production has only one node block.
  63. Pair("node_blocks",
  64. Yaml::Sequence(ElementsAre(Yaml::Sequence(IsEmpty()),
  65. Yaml::Sequence(Each(node_id)))))))));
  66. }
  67. } // namespace
  68. } // namespace Carbon::Testing