From 92a4d4cb4eb4a6103f68646a38f8caa29d6df3e3 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sat, 22 Nov 2014 15:41:40 +0100 Subject: Fix and test node_check --- src/blocks.c | 2 +- src/node.c | 55 ++++++++++++++++++++++++++++++------------------------- src/node.h | 4 +++- 3 files changed, 34 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/blocks.c b/src/blocks.c index 58162b5..ab9f667 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -818,7 +818,7 @@ cmark_node *cmark_finish(cmark_doc_parser *parser) finalize_document(parser); strbuf_free(parser->curline); #if CMARK_DEBUG_NODES - if (cmark_node_check(parser->root)) { + if (cmark_node_check(parser->root, stderr)) { abort(); } #endif diff --git a/src/node.c b/src/node.c index bb9cacc..a79dd82 100644 --- a/src/node.c +++ b/src/node.c @@ -558,58 +558,63 @@ cmark_node_append_child(cmark_node *node, cmark_node *child) } static void -S_print_error(cmark_node *node, const char *elem) +S_print_error(FILE *out, cmark_node *node, const char *elem) { - fprintf(stderr, "Invalid '%s' in node type %s at %d:%d\n", elem, + if (out == NULL) { + return; + } + fprintf(out, "Invalid '%s' in node type %s at %d:%d\n", elem, S_type_string(node), node->start_line, node->start_column); } int -cmark_node_check(cmark_node *node) +cmark_node_check(cmark_node *node, FILE *out) { - cmark_node *cur = node; + cmark_node *cur; int errors = 0; - while (cur) { + if (!node) { + return 0; + } + + cur = node; + while (true) { if (cur->first_child) { if (cur->first_child->parent != cur) { - S_print_error(cur->first_child, "parent"); + S_print_error(out, cur->first_child, "parent"); cur->first_child->parent = cur; ++errors; } cur = cur->first_child; + continue; + } + + next_sibling: + if (cur == node) { + break; } - else if (cur->next) { + if (cur->next) { if (cur->next->prev != cur) { - S_print_error(cur->next, "prev"); + S_print_error(out, cur->next, "prev"); cur->next->prev = cur; ++errors; } if (cur->next->parent != cur->parent) { - S_print_error(cur->next, "parent"); + S_print_error(out, cur->next, "parent"); cur->next->parent = cur->parent; ++errors; } cur = cur->next; + continue; } - else { - if (cur->parent->last_child != cur) { - S_print_error(cur->parent, "last_child"); - cur->parent->last_child = cur; - ++errors; - } - cmark_node *ancestor = cur->parent; - cur = NULL; - - while (ancestor != node->parent) { - if (ancestor->next) { - cur = ancestor->next; - break; - } - ancestor = ancestor->parent; - } + if (cur->parent->last_child != cur) { + S_print_error(out, cur->parent, "last_child"); + cur->parent->last_child = cur; + ++errors; } + cur = cur->parent; + goto next_sibling; } return errors; diff --git a/src/node.h b/src/node.h index 2d7f0a1..d1245a5 100644 --- a/src/node.h +++ b/src/node.h @@ -5,6 +5,8 @@ extern "C" { #endif +#include + #include "cmark.h" #include "buffer.h" #include "chunk.h" @@ -62,7 +64,7 @@ struct cmark_node { }; CMARK_EXPORT int -cmark_node_check(cmark_node *node); +cmark_node_check(cmark_node *node, FILE *out); #ifdef __cplusplus } -- cgit v1.2.3