Skip to content

Commit

Permalink
cleanup and improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Mar 30, 2024
1 parent f58d9af commit 1207891
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 40 deletions.
30 changes: 14 additions & 16 deletions src/c4/yml/event_handler_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ struct EventHandlerTree

void finish_parse()
{
/** This pointer is temporary. Remember that:
*
* - this handler object may be held by the user
* - it may be used with a temporary tree inside the parse function
* - when the parse function returns the temporary tree, its address
* will change
*
* As a result, the user could try to read the tree from m_tree, and
* end up reading the stale temporary object.
*
* So it is better to clear it here; then the user will get an obvious
* segfault to read from m_tree. */
m_tree = nullptr;
}

Expand Down Expand Up @@ -298,13 +310,9 @@ struct EventHandlerTree
/** @name YAML stream functions */
/** @{ */

void begin_stream()
{
}
C4_ALWAYS_INLINE void begin_stream() {}

void end_stream()
{
}
C4_ALWAYS_INLINE void end_stream() {}

/** @} */

Expand Down Expand Up @@ -797,16 +805,6 @@ struct EventHandlerTree
}
}

C4_ALWAYS_INLINE bool has_key_tag() const
{
return _has_any_(KEYTAG);
}

C4_ALWAYS_INLINE bool has_val_tag() const
{
return _has_any_(VALTAG);
}

/** @} */

public:
Expand Down
38 changes: 14 additions & 24 deletions test/test_suite/test_suite_event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ struct EventHandlerYamlStd
return result;
}

bool _should_push_on_begin_doc() const
{
const bool is_root = (m_stack.size() == 1u);
{
return is_root && (_has_any_(DOC) || m_curr->ev_has_children);
}
}

bool _should_pop_on_end_doc() const
{
const bool is_root = (m_stack.size() == 1u);
return !is_root && _has_any_(DOC);
}

/** @} */

public:
Expand Down Expand Up @@ -368,20 +382,6 @@ struct EventHandlerYamlStd
/** @name YAML document functions */
/** @{ */

bool _should_push_on_begin_doc() const
{
const bool is_root = (m_stack.size() == 1u);
{
return is_root && (_has_any_(DOC) || m_curr->ev_has_children);
}
}

bool _should_pop_on_end_doc() const
{
const bool is_root = (m_stack.size() == 1u);
return !is_root && _has_any_(DOC);
}

/** called at the beginning of parsing a non-empty stream */
void begin_doc()
{
Expand Down Expand Up @@ -834,16 +834,6 @@ struct EventHandlerYamlStd
}
}

C4_ALWAYS_INLINE bool has_key_tag() const
{
return _has_any_(KEYTAG);
}

C4_ALWAYS_INLINE bool has_val_tag() const
{
return _has_any_(VALTAG);
}

/** @} */

public:
Expand Down

0 comments on commit 1207891

Please sign in to comment.