yaml_test_helpers.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/testing/yaml_test_helpers.h"
  5. #include "common/raw_string_ostream.h"
  6. #include "llvm/ADT/SmallString.h"
  7. #include "llvm/Support/YAMLParser.h"
  8. namespace Carbon::Testing::Yaml {
  9. // This is for tests, so we should be okay with the recursion here.
  10. // NOLINTNEXTLINE(misc-no-recursion)
  11. static auto Parse(llvm::yaml::Node* node) -> Value {
  12. CARBON_CHECK(node != nullptr);
  13. // getType returns an unsigned int which should map to the enum.
  14. switch (static_cast<llvm::yaml::Node::NodeKind>(node->getType())) {
  15. case llvm::yaml::Node::NK_Null:
  16. return Value(NullValue());
  17. case llvm::yaml::Node::NK_Scalar: {
  18. llvm::SmallString<128> storage;
  19. return Value{
  20. llvm::cast<llvm::yaml::ScalarNode>(*node).getValue(storage).str()};
  21. }
  22. case llvm::yaml::Node::NK_BlockScalar:
  23. return Value{
  24. llvm::cast<llvm::yaml::BlockScalarNode>(*node).getValue().str()};
  25. case llvm::yaml::Node::NK_Mapping: {
  26. MappingValue v;
  27. for (llvm::yaml::KeyValueNode& kv :
  28. llvm::cast<llvm::yaml::MappingNode>(*node)) {
  29. Value key = Parse(kv.getKey());
  30. Value value = Parse(kv.getValue());
  31. v.emplace_back(std::move(key), std::move(value));
  32. }
  33. return Value(std::move(v));
  34. }
  35. case llvm::yaml::Node::NK_Sequence: {
  36. SequenceValue v;
  37. for (llvm::yaml::Node& n : llvm::cast<llvm::yaml::SequenceNode>(*node)) {
  38. v.push_back(Parse(&n));
  39. }
  40. return Value(std::move(v));
  41. }
  42. case llvm::yaml::Node::NK_Alias:
  43. return Value(AliasValue());
  44. case llvm::yaml::Node::NK_KeyValue:
  45. llvm_unreachable("should only exist as child of mapping");
  46. }
  47. llvm_unreachable("unknown yaml node kind");
  48. }
  49. auto Value::FromText(llvm::StringRef text) -> ErrorOr<SequenceValue> {
  50. llvm::SourceMgr sm;
  51. std::optional<std::string> error_message;
  52. sm.setDiagHandler(
  53. [](const llvm::SMDiagnostic& diag, void* context) -> void {
  54. auto* error_message = static_cast<std::optional<std::string>*>(context);
  55. RawStringOstream stream;
  56. diag.print(/*ProgName=*/nullptr, stream, /*ShowColors=*/false);
  57. *error_message = stream.TakeStr();
  58. },
  59. &error_message);
  60. llvm::yaml::Stream yaml_stream(text, sm);
  61. SequenceValue result;
  62. for (llvm::yaml::Document& document : yaml_stream) {
  63. result.push_back(Parse(document.getRoot()));
  64. if (error_message) {
  65. return Error(*error_message);
  66. }
  67. }
  68. return result;
  69. }
  70. auto operator<<(std::ostream& os, const Value& v) -> std::ostream& {
  71. // Variant visitor that prints the value in the form of code to recreate the
  72. // value.
  73. struct Printer {
  74. auto operator()(NullValue /*v*/) -> void { out << "Yaml::NullValue()"; }
  75. auto operator()(AliasValue /*v*/) -> void { out << "Yaml::AliasValue()"; }
  76. auto operator()(const ScalarValue& v) -> void { out << std::quoted(v); }
  77. auto operator()(const MappingValue& v) -> void {
  78. out << "Yaml::MappingValue{";
  79. bool first = true;
  80. for (const auto& [key, value] : v) {
  81. if (first) {
  82. first = false;
  83. } else {
  84. out << ", ";
  85. }
  86. out << "{" << key << ", " << value << "}";
  87. }
  88. out << "}";
  89. }
  90. auto operator()(const SequenceValue& v) -> void {
  91. out << "Yaml::SequenceValue{";
  92. bool first = true;
  93. for (const auto& value : v) {
  94. if (first) {
  95. first = false;
  96. } else {
  97. out << ", ";
  98. }
  99. out << value;
  100. }
  101. out << "}";
  102. }
  103. std::ostream& out;
  104. };
  105. std::visit(Printer{.out = os}, v);
  106. return os;
  107. }
  108. } // namespace Carbon::Testing::Yaml