Files
carbon-lang/toolchain/base/yaml_test_helpers.cpp
T
Jon Ross-Perkins b9df8ca765 Manual cleanup of toolchain clang-tidy/clangd warnings. (#3157)
A lot of this is more boring "remove unused header", plus some other
minor cleanups. I think the most significant changes were:

- yaml_test_helpers.cpp is doing a switch on an unsigned int, comparing
to enum values.
-
[bugprone-switch-missing-default-case](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/switch-missing-default-case.html)
is unhappy with EnumBase, but correctly identified
yaml_test_helpers.cpp, so I'm opting to address it rather than disabling
it even though it needs NOLINT in several locations as a result, in
addition to what I think are some low-value `default` cases. I'd be fine
going the other way with this too and disabling it globally (I could see
it being noisier in the explorer).
- MarkInitializerFor swaps the argument names between the .h and .cpp. I
think the .cpp had the order as intended.
- There's a new-ish
[performance-enum-size](https://clang.llvm.org/extra/clang-tidy/checks/performance/enum-size.html)
which I'm basically treating as "add int8_t to enums".

My main motivation here is to just clean up as many of these as I can so
that I stop seeing them in vscode.
2023-08-25 22:45:57 +00:00

113 lines
3.2 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/base/yaml_test_helpers.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/YAMLParser.h"
namespace Carbon::Testing::Yaml {
static auto Parse(llvm::yaml::Node* node) -> Value {
if (!node) {
return Value{ErrorValue()};
}
// getType returns an unsigned int which should map to the enum.
switch (static_cast<llvm::yaml::Node::NodeKind>(node->getType())) {
case llvm::yaml::Node::NK_Null:
return Value{NullValue()};
case llvm::yaml::Node::NK_Scalar: {
llvm::SmallString<128> storage;
return Value{
llvm::cast<llvm::yaml::ScalarNode>(*node).getValue(storage).str()};
}
case llvm::yaml::Node::NK_BlockScalar:
return Value{
llvm::cast<llvm::yaml::BlockScalarNode>(*node).getValue().str()};
case llvm::yaml::Node::NK_Mapping: {
MappingValue v;
for (llvm::yaml::KeyValueNode& kv :
llvm::cast<llvm::yaml::MappingNode>(*node)) {
Value key = Parse(kv.getKey());
Value value = Parse(kv.getValue());
v.emplace_back(std::move(key), std::move(value));
}
return Value{std::move(v)};
}
case llvm::yaml::Node::NK_Sequence: {
SequenceValue v;
for (llvm::yaml::Node& n : llvm::cast<llvm::yaml::SequenceNode>(*node)) {
v.push_back(Parse(&n));
}
return Value{std::move(v)};
}
case llvm::yaml::Node::NK_Alias:
return Value{AliasValue()};
case llvm::yaml::Node::NK_KeyValue:
llvm_unreachable("should only exist as child of mapping");
}
llvm_unreachable("unknown yaml node kind");
}
auto Value::FromText(llvm::StringRef text) -> SequenceValue {
llvm::SourceMgr sm;
llvm::yaml::Stream yaml_stream(text, sm);
SequenceValue result;
for (llvm::yaml::Document& document : yaml_stream) {
result.push_back(Parse(document.getRoot()));
}
return result;
}
auto operator<<(std::ostream& os, const Value& v) -> std::ostream& {
// Variant visitor that prints the value in the form of code to recreate the
// value.
struct Printer {
auto operator()(NullValue /*v*/) -> void { out << "Yaml::NullValue()"; }
auto operator()(AliasValue /*v*/) -> void { out << "Yaml::AliasValue()"; }
auto operator()(ErrorValue /*v*/) -> void { out << "Yaml::ErrorValue()"; }
auto operator()(const ScalarValue& v) -> void { out << std::quoted(v); }
auto operator()(const MappingValue& v) -> void {
out << "Yaml::MappingValue{";
bool first = true;
for (const auto& [key, value] : v) {
if (first) {
first = false;
} else {
out << ", ";
}
out << "{" << key << ", " << value << "}";
}
out << "}";
}
auto operator()(const SequenceValue& v) -> void {
out << "Yaml::SequenceValue{";
bool first = true;
for (const auto& value : v) {
if (first) {
first = false;
} else {
out << ", ";
}
out << value;
}
out << "}";
}
std::ostream& out;
};
std::visit(Printer{os}, v);
return os;
}
} // namespace Carbon::Testing::Yaml