yaml_test_helpers.cpp 3.6 KB

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