mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
High level, replacing `Id::Invalid` with `Id::None` and `Id::is_valid` with `Id::has_value` for clarity, as discussed [here](https://discord.com/channels/655572317891461132/655578254970716160/1331664574545395794). The `IntId` refactoring is needed together with `AnyIdBase` because it's also used with `ValueStore`. Note, trying to be careful not to rewrite `EnumBase::InvalidIndex`, or `is_valid` in general (e.g., `IdKind::is_valid`). I've tried to sequence commits here: 1. Automatic replacements: - `((?:Id|Index)(?: |::|\(|Base(?:\(|::)))Invalid((?:Index)?\W)` -> `$1None$2` - `<invalid>` -> `<none>` - `InvalidNodeId` -> `NoneNodeId` - `/\*invalid\*/` -> `/*none*/` - `id((?:_|\(\))(?:\.|->))is_valid` -> `id$1has_value` 2. Manual edits: - In `int.h` and `int_test.cpp` - `IntT` has `is_value`, which I'm renaming to `is_embedded_value`. - Manual edits to comments in this file. - `AnyIdBase` and `IdBase` - Declaration of `is_valid` -> `has_value`, `InvalidIndex` -> `NoneIndex`. - In `ids.h` and `ids.cpp` - `is_valid` -> `has_value` - `// An explicitly invalid ID.` -> `// An ID with no value.`; similar for index - Various math on `InvalidIndex` -> `NoneIndex` - Various mentions of "valid" in comments - In `value_store.h`, for `IdT::Invalid`, plus one comment - In `impl.h` and `tokenized_buffer.h`, we had different initialization of `::None` values (versus `ids.h` syntax) that I fixed manually. - Spot checks to compile - Particularly where `is_valid` replacements didn't catch spots due to different naming. 3. Autoupdate tests 4. verbose.carbon (NOAUTOUPDATE) 5. Comment spot checks Note there are probably other mentions of "Invalid" that should be swept up, but I'd like to argue for merging and separating out remaining cleanup since this is so sweeping (and likely to hit merge conflicts from churn). We'll probably have lingering mentions of "invalid" for a bit regardless, just because there are uses of "invalid" in non-Id APIs.
172 lines
6.6 KiB
C++
172 lines
6.6 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 "toolchain/parse/tree.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <forward_list>
|
|
|
|
#include "common/raw_string_ostream.h"
|
|
#include "toolchain/base/shared_value_stores.h"
|
|
#include "toolchain/diagnostics/diagnostic_emitter.h"
|
|
#include "toolchain/diagnostics/mocks.h"
|
|
#include "toolchain/lex/lex.h"
|
|
#include "toolchain/lex/tokenized_buffer.h"
|
|
#include "toolchain/parse/parse.h"
|
|
#include "toolchain/parse/tree_and_subtrees.h"
|
|
#include "toolchain/testing/compile_helper.h"
|
|
#include "toolchain/testing/yaml_test_helpers.h"
|
|
|
|
namespace Carbon::Parse {
|
|
namespace {
|
|
|
|
using ::testing::ElementsAre;
|
|
using ::testing::Pair;
|
|
|
|
namespace Yaml = ::Carbon::Testing::Yaml;
|
|
|
|
class TreeTest : public ::testing::Test {
|
|
public:
|
|
Testing::CompileHelper compile_helper_;
|
|
};
|
|
|
|
TEST_F(TreeTest, IsValid) {
|
|
Tree& tree = compile_helper_.GetTree("");
|
|
EXPECT_TRUE((*tree.postorder().begin()).has_value());
|
|
}
|
|
|
|
TEST_F(TreeTest, AsAndTryAs) {
|
|
auto [tokens, tree_and_subtrees] =
|
|
compile_helper_.GetTokenizedBufferWithTreeAndSubtrees("fn F();");
|
|
const auto& tree = tree_and_subtrees.tree();
|
|
ASSERT_FALSE(tree.has_errors());
|
|
auto it = tree_and_subtrees.roots().begin();
|
|
// A FileEnd node, so won't match.
|
|
NodeId n = *it;
|
|
|
|
// NodeIdForKind
|
|
std::optional<FunctionDeclId> fn_decl_id = tree.TryAs<FunctionDeclId>(n);
|
|
EXPECT_FALSE(fn_decl_id.has_value());
|
|
// NodeIdOneOf
|
|
std::optional<AnyFunctionDeclId> any_fn_decl_id =
|
|
tree.TryAs<AnyFunctionDeclId>(n);
|
|
EXPECT_FALSE(any_fn_decl_id.has_value());
|
|
// NodeIdInCategory
|
|
std::optional<AnyDeclId> any_decl_id = tree.TryAs<AnyDeclId>(n);
|
|
EXPECT_FALSE(any_decl_id.has_value());
|
|
|
|
++it;
|
|
n = *it;
|
|
// A FunctionDecl node, so will match.
|
|
fn_decl_id = tree.TryAs<FunctionDeclId>(n);
|
|
ASSERT_TRUE(fn_decl_id.has_value());
|
|
EXPECT_TRUE(*fn_decl_id == n);
|
|
// Under normal usage, this function should be used with `auto`, but for
|
|
// a test it is nice to verify that it is returning the expected type.
|
|
// NOLINTNEXTLINE(modernize-use-auto).
|
|
FunctionDeclId fn_decl_id2 = tree.As<FunctionDeclId>(n);
|
|
EXPECT_TRUE(*fn_decl_id == fn_decl_id2);
|
|
|
|
any_fn_decl_id = tree.TryAs<AnyFunctionDeclId>(n);
|
|
ASSERT_TRUE(any_fn_decl_id.has_value());
|
|
EXPECT_TRUE(*any_fn_decl_id == n);
|
|
// NOLINTNEXTLINE(modernize-use-auto).
|
|
AnyFunctionDeclId any_fn_decl_id2 = tree.As<AnyFunctionDeclId>(n);
|
|
EXPECT_TRUE(*any_fn_decl_id == any_fn_decl_id2);
|
|
|
|
any_decl_id = tree.TryAs<AnyDeclId>(n);
|
|
ASSERT_TRUE(any_decl_id.has_value());
|
|
EXPECT_TRUE(*any_decl_id == n);
|
|
// NOLINTNEXTLINE(modernize-use-auto).
|
|
AnyDeclId any_decl_id2 = tree.As<AnyDeclId>(n);
|
|
EXPECT_TRUE(*any_decl_id == any_decl_id2);
|
|
}
|
|
|
|
TEST_F(TreeTest, PrintPostorderAsYAML) {
|
|
auto [tokens, tree_and_subtrees] =
|
|
compile_helper_.GetTokenizedBufferWithTreeAndSubtrees("fn F();");
|
|
EXPECT_FALSE(tree_and_subtrees.tree().has_errors());
|
|
RawStringOstream print_stream;
|
|
tree_and_subtrees.tree().Print(print_stream);
|
|
|
|
auto file = Yaml::Sequence(ElementsAre(
|
|
Yaml::Mapping(ElementsAre(Pair("kind", "FileStart"), Pair("text", ""))),
|
|
Yaml::Mapping(
|
|
ElementsAre(Pair("kind", "FunctionIntroducer"), Pair("text", "fn"))),
|
|
Yaml::Mapping(ElementsAre(Pair("kind", "IdentifierNameBeforeParams"),
|
|
Pair("text", "F"))),
|
|
Yaml::Mapping(
|
|
ElementsAre(Pair("kind", "TuplePatternStart"), Pair("text", "("))),
|
|
Yaml::Mapping(ElementsAre(Pair("kind", "TuplePattern"), Pair("text", ")"),
|
|
Pair("subtree_size", "2"))),
|
|
Yaml::Mapping(ElementsAre(Pair("kind", "FunctionDecl"), Pair("text", ";"),
|
|
Pair("subtree_size", "5"))),
|
|
Yaml::Mapping(ElementsAre(Pair("kind", "FileEnd"), Pair("text", "")))));
|
|
|
|
auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
|
|
ElementsAre(Pair("filename", tokens.source().filename().str()),
|
|
Pair("parse_tree", file)))));
|
|
|
|
EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
|
|
IsYaml(ElementsAre(root)));
|
|
}
|
|
|
|
TEST_F(TreeTest, PrintPreorderAsYAML) {
|
|
auto [tokens, tree_and_subtrees] =
|
|
compile_helper_.GetTokenizedBufferWithTreeAndSubtrees("fn F();");
|
|
EXPECT_FALSE(tree_and_subtrees.tree().has_errors());
|
|
RawStringOstream print_stream;
|
|
tree_and_subtrees.PrintPreorder(print_stream);
|
|
|
|
auto param_list = Yaml::Sequence(ElementsAre(Yaml::Mapping(
|
|
ElementsAre(Pair("node_index", "3"), Pair("kind", "TuplePatternStart"),
|
|
Pair("text", "(")))));
|
|
|
|
auto function_decl = Yaml::Sequence(ElementsAre(
|
|
Yaml::Mapping(ElementsAre(Pair("node_index", "1"),
|
|
Pair("kind", "FunctionIntroducer"),
|
|
Pair("text", "fn"))),
|
|
Yaml::Mapping(ElementsAre(Pair("node_index", "2"),
|
|
Pair("kind", "IdentifierNameBeforeParams"),
|
|
Pair("text", "F"))),
|
|
Yaml::Mapping(ElementsAre(Pair("node_index", "4"),
|
|
Pair("kind", "TuplePattern"), Pair("text", ")"),
|
|
Pair("subtree_size", "2"),
|
|
Pair("children", param_list)))));
|
|
|
|
auto file = Yaml::Sequence(ElementsAre(
|
|
Yaml::Mapping(ElementsAre(Pair("node_index", "0"),
|
|
Pair("kind", "FileStart"), Pair("text", ""))),
|
|
Yaml::Mapping(ElementsAre(Pair("node_index", "5"),
|
|
Pair("kind", "FunctionDecl"), Pair("text", ";"),
|
|
Pair("subtree_size", "5"),
|
|
Pair("children", function_decl))),
|
|
Yaml::Mapping(ElementsAre(Pair("node_index", "6"),
|
|
Pair("kind", "FileEnd"), Pair("text", "")))));
|
|
|
|
auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
|
|
ElementsAre(Pair("filename", tokens.source().filename().str()),
|
|
Pair("parse_tree", file)))));
|
|
|
|
EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
|
|
IsYaml(ElementsAre(root)));
|
|
}
|
|
|
|
TEST_F(TreeTest, HighRecursion) {
|
|
std::string code = "fn Foo() { return ";
|
|
code.append(10000, '(');
|
|
code.append(10000, ')');
|
|
code += "; }";
|
|
Lex::TokenizedBuffer& tokens = compile_helper_.GetTokenizedBuffer(code);
|
|
ASSERT_FALSE(tokens.has_errors());
|
|
Testing::MockDiagnosticConsumer consumer;
|
|
Tree tree = Parse(tokens, consumer, /*vlog_stream=*/nullptr);
|
|
EXPECT_FALSE(tree.has_errors());
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Parse
|