From ee5b8945379eedd2be2f6eaaaf52d5ce1f312012 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 15 Aug 2019 08:06:46 -0400 Subject: [PATCH] DNAYaml: Get rid of trivial uses of goto We can just wrap the handling within a lambda function and then execute it on the error paths. --- src/athena/DNAYaml.cpp | 50 +++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/athena/DNAYaml.cpp b/src/athena/DNAYaml.cpp index 7fd3c50..43997ee 100644 --- a/src/athena/DNAYaml.cpp +++ b/src/athena/DNAYaml.cpp @@ -957,40 +957,50 @@ static yaml_mapping_style_t MappingStyle(const YAMLNode& node) { } bool YAMLDocWriter::RecursiveFinish(yaml_emitter_t* doc, const YAMLNode& node) { + const auto handleError = [doc] { + HandleYAMLEmitterError(doc); + return false; + }; + yaml_event_t event; if (node.m_type == YAML_SCALAR_NODE) { if (!yaml_scalar_event_initialize(&event, nullptr, nullptr, (yaml_char_t*)node.m_scalarString.c_str(), node.m_scalarString.length(), true, true, ScalarStyle(node)) || - !yaml_emitter_emit(doc, &event)) - goto err; + !yaml_emitter_emit(doc, &event)) { + return handleError(); + } } else if (node.m_type == YAML_SEQUENCE_NODE) { if (!yaml_sequence_start_event_initialize(&event, nullptr, nullptr, 1, SequenceStyle(node)) || - !yaml_emitter_emit(doc, &event)) - goto err; - for (const auto& item : node.m_seqChildren) { - if (!RecursiveFinish(doc, *item)) - goto err; + !yaml_emitter_emit(doc, &event)) { + return handleError(); + } + for (const auto& item : node.m_seqChildren) { + if (!RecursiveFinish(doc, *item)) { + return handleError(); + } + } + if (!yaml_sequence_end_event_initialize(&event) || !yaml_emitter_emit(doc, &event)) { + return handleError(); } - if (!yaml_sequence_end_event_initialize(&event) || !yaml_emitter_emit(doc, &event)) - goto err; } else if (node.m_type == YAML_MAPPING_NODE) { if (!yaml_mapping_start_event_initialize(&event, nullptr, nullptr, true, MappingStyle(node)) || - !yaml_emitter_emit(doc, &event)) - goto err; + !yaml_emitter_emit(doc, &event)) { + return handleError(); + } for (const auto& item : node.m_mapChildren) { - if (!EmitKeyScalar(doc, item.first.c_str())) - goto err; - if (!RecursiveFinish(doc, *item.second)) - goto err; + if (!EmitKeyScalar(doc, item.first.c_str())) { + return handleError(); + } + if (!RecursiveFinish(doc, *item.second)) { + return handleError(); + } } event.type = YAML_MAPPING_END_EVENT; - if (!yaml_mapping_end_event_initialize(&event) || !yaml_emitter_emit(doc, &event)) - goto err; + if (!yaml_mapping_end_event_initialize(&event) || !yaml_emitter_emit(doc, &event)) { + return handleError(); + } } return true; -err: - HandleYAMLEmitterError(doc); - return false; } static const std::string base64_chars =