Files
carbon-lang/toolchain/semantics/semantics_ir_test.cpp
T
Jon Ross-Perkins 67da700dd5 Split Semantics into Check and SemIR namespaces (#3138)
Splits IR files into SemIR, and logic files into Check. These will be
split into separate directories as part of a later move; the namespaces
are being done first in order to vet the switch, and hopefully make
conflicts a little easier to manage due to the substantial renames.

A lot of this is just automated removal of Semantics prefixes from
names, adding namespace references where needed. A few special-cases
are:

- SemanticsIR -> SemIR::File
- A few things were discussed, like Unit, CompileUnit, or CompiledUnit.
Unit was too vague for chandlerc, and I thought CompileUnit might lead
to incorrect inferences (CompilationUnit would be more precise, but
typically written as SemIR::CompilationUnit which is pretty long). File
seemed to be a short name that we could agree on.
- SemanticsIRFormatter -> SemIR::Formatter
- FormatSemanticsIR -> SemIR::FormatFile
- SemanticsFileTest -> CheckFileTest
- It remains in the Testing namespace, where just "FileTest" might be
too broad a name.
- SemanticsDeclarationNameStack::Context ->
Check::DeclarationNameStack::NameContext
  - This avoids a Check::Context name shadowing.

Changes check_internal.h to include ostream.h to improve finding of
Print/operator<< (otherwise it didn't compile).

This is part of #3070
2023-08-23 22:51:23 +00:00

79 lines
3.1 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "testing/base/test_raw_ostream.h"
#include "toolchain/base/yaml_test_helpers.h"
#include "toolchain/driver/driver.h"
namespace Carbon::Testing {
namespace {
using ::testing::_;
using ::testing::AllOf;
using ::testing::Contains;
using ::testing::Each;
using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::MatchesRegex;
using ::testing::Pair;
using ::testing::SizeIs;
TEST(SemIRTest, YAML) {
llvm::vfs::InMemoryFileSystem fs;
CARBON_CHECK(fs.addFile(
"test.carbon", /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBuffer("fn F() { var x: i32 = 0; return; }")));
TestRawOstream print_stream;
Driver d(fs, print_stream, llvm::errs());
d.RunCommand(
{"compile", "--phase=check", "--dump-raw-semantics-ir", "test.carbon"});
// Matches the ID of a node. The numbers may change because of builtin
// cross-references, so this code is only doing loose structural checks.
auto node_id = Yaml::Scalar(MatchesRegex(R"(node\+\d+)"));
auto node_builtin = Yaml::Scalar(MatchesRegex(R"(node\w+)"));
auto type_id = Yaml::Scalar(MatchesRegex(R"(type\d+)"));
EXPECT_THAT(
Yaml::Value::FromText(print_stream.TakeStr()),
ElementsAre(Yaml::Mapping(ElementsAre(
Pair("cross_reference_irs_size", "1"),
Pair("functions", Yaml::Sequence(SizeIs(1))),
Pair("integer_literals", Yaml::Sequence(ElementsAre("0"))),
Pair("real_literals", Yaml::Sequence(IsEmpty())),
Pair("strings", Yaml::Sequence(ElementsAre("F", "x"))),
Pair("types", Yaml::Sequence(ElementsAre(node_builtin))),
Pair("type_blocks", Yaml::Sequence(IsEmpty())),
Pair("nodes",
Yaml::Sequence(AllOf(
// kind is required, other parts are optional.
Each(Yaml::Mapping(Contains(Pair("kind", _)))),
// A 0-arg node.
Contains(Yaml::Mapping(ElementsAre(Pair("kind", "Return")))),
// A 1-arg node.
Contains(Yaml::Mapping(ElementsAre(
Pair("kind", "IntegerLiteral"), Pair("arg0", "int0"),
Pair("type", type_id)))),
// A 2-arg node.
Contains(Yaml::Mapping(ElementsAre(
Pair("kind", "Assign"), Pair("arg0", node_id),
Pair("arg1", node_id))))))),
// This production has only two node blocks.
Pair("node_blocks",
Yaml::Sequence(ElementsAre(Yaml::Sequence(IsEmpty()),
Yaml::Sequence(Each(node_id)),
Yaml::Sequence(Each(node_id)))))))));
}
} // namespace
} // namespace Carbon::Testing