mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 18:40:12 +01:00
I was wondering, instead of treating `misc` differently and enabling specific checks, maybe we can flip that since we actually seem okay with most of the checks? The main check I'm enabling, with significant edits here, is `misc-no-recursion`. But maybe this is helpful to enable, even with the necessary NOLINTs, since we want to avoid recursion in the toolchain? This PR shows some example fixes in subst.cpp (which are more stylistic, since the code shouldn't actually have recursed due to its structure; I think we could remove the warning on TryResolveInst the same way). Some also just don't seem worth fixing, like those in tests files (I didn't see a way to exclude files in .clang-tidy, so instead I'm using NOLINTBEGIN). But I think we might actually want to fix inst_namer, and there's enough in convert that I didn't look closely. Also, I made some protected -> private style fixes based on `misc-non-private-member-variables-in-classes` (this is also how I noticed `class Real` versus `struct Real`). With node_stack, it looks like the `protected` wasn't even used. [Per style](https://google.github.io/styleguide/cppguide.html#Access_Control), data members should be private outside tests. But since we can't trivially exclude `protected` members in tests, I'm turning it off -- I don't view it as offering enough benefit on the whole. migrate_cpp issues are preexisting (I believe we just aren't monitoring it), but changes there make `bazel build --config=clang-tidy -k //...` work cleanly. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
124 lines
3.7 KiB
C++
124 lines
3.7 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/testing/yaml_test_helpers.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
#include "llvm/Support/YAMLParser.h"
|
|
|
|
namespace Carbon::Testing::Yaml {
|
|
|
|
// This is for tests, so we should be okay with the recursion here.
|
|
// NOLINTNEXTLINE(misc-no-recursion)
|
|
static auto Parse(llvm::yaml::Node* node) -> Value {
|
|
CARBON_CHECK(node != nullptr);
|
|
|
|
// 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) -> ErrorOr<SequenceValue> {
|
|
llvm::SourceMgr sm;
|
|
std::optional<std::string> error_message;
|
|
sm.setDiagHandler(
|
|
[](const llvm::SMDiagnostic& diag, void* context) -> void {
|
|
auto* error_message = static_cast<std::optional<std::string>*>(context);
|
|
*error_message = std::string();
|
|
llvm::raw_string_ostream stream(**error_message);
|
|
diag.print(/*ProgName=*/nullptr, stream, /*ShowColors=*/false);
|
|
},
|
|
&error_message);
|
|
llvm::yaml::Stream yaml_stream(text, sm);
|
|
|
|
SequenceValue result;
|
|
for (llvm::yaml::Document& document : yaml_stream) {
|
|
result.push_back(Parse(document.getRoot()));
|
|
if (error_message) {
|
|
return Error(*error_message);
|
|
}
|
|
}
|
|
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()(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{.out = os}, v);
|
|
return os;
|
|
}
|
|
|
|
} // namespace Carbon::Testing::Yaml
|