From ec5e94434a9975b4be21e67c2d10381696aa49c3 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Thu, 5 Sep 2024 08:52:07 +0100 Subject: [PATCH] Sonar fixes --- rapidxml.hpp | 102 ++++++++++++++++++--------------------- rapidxml_predicates.hpp | 12 ++--- rapidxml_print.hpp | 17 ++++--- sonar-project.properties | 2 +- test/manipulations.cpp | 8 +-- test/xpath.cpp | 2 +- 6 files changed, 69 insertions(+), 74 deletions(-) diff --git a/rapidxml.hpp b/rapidxml.hpp index 1fcd54a..6ee2982 100644 --- a/rapidxml.hpp +++ b/rapidxml.hpp @@ -102,7 +102,7 @@ namespace rapidxml class eof_error : public parse_error { public: - eof_error(const char * what, void * where) : parse_error(what, where) {} + using parse_error::parse_error; }; class validation_error : public std::runtime_error @@ -114,26 +114,22 @@ namespace rapidxml class xmlns_unbound : public validation_error { public: - explicit xmlns_unbound(const char * what) - : validation_error(what) {} + using validation_error::validation_error; }; class duplicate_attribute : public validation_error { public: - explicit duplicate_attribute(const char * what) - : validation_error(what) {} + using validation_error::validation_error; }; class attr_xmlns_unbound : public xmlns_unbound { public: - explicit attr_xmlns_unbound(const char * what) - : xmlns_unbound(what) {} + using xmlns_unbound::xmlns_unbound; }; class element_xmlns_unbound : public xmlns_unbound { public: - explicit element_xmlns_unbound(const char * what) - : xmlns_unbound(what) {} + using xmlns_unbound::xmlns_unbound; }; } @@ -168,7 +164,7 @@ namespace rapidxml //! Enumeration listing all node types produced by the parser. //! Use xml_node::type() function to query node type. - enum node_type + enum class node_type { node_document, //!< A document node. Name and value are empty. node_element, //!< An element node. Name contains element name. Value contains text of first data node. @@ -200,13 +196,6 @@ namespace rapidxml //! See xml_document::parse() function. const int parse_no_element_values = 0x2; - //! Parse flag instructing the parser to not place zero terminators after strings in the source text. - //! By default zero terminators are placed, modifying source text. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - // const int parse_no_string_terminators = 0x4; - //! Parse flag instructing the parser to not translate entities in the source text. //! By default entities are translated, modifying source text. //! Can be combined with other flags by use of | operator. @@ -383,14 +372,16 @@ namespace rapidxml public: //! \cond internal - typedef void *(alloc_func)(std::size_t); // Type of user-defined function used to allocate memory - typedef void (free_func)(void *); // Type of user-defined function used to free memory + using alloc_func = void * (*)(std::size_t); // Type of user-defined function used to allocate memory + using free_func = void (*)(void *); // Type of user-defined function used to free memory //! \endcond //! Constructs empty pool with default allocator functions. - memory_pool() : m_static_memory(0) { + memory_pool() { init(); } + memory_pool(memory_pool const &) = delete; + memory_pool(memory_pool &&) = delete; //! Destroys pool and frees all the memory. //! This causes memory occupied by nodes allocated by the pool to be freed. @@ -651,9 +642,9 @@ namespace rapidxml void *m_begin = nullptr; // Start of raw memory making up current pool void *m_ptr = nullptr; // First free byte in current pool std::size_t m_space = RAPIDXML_STATIC_POOL_SIZE; // Available space remaining - char m_static_memory[RAPIDXML_STATIC_POOL_SIZE]; // Static raw memory - alloc_func *m_alloc_func = nullptr; // Allocator function, or 0 if default is to be used - free_func *m_free_func = nullptr; // Free function, or 0 if default is to be used + char m_static_memory[RAPIDXML_STATIC_POOL_SIZE] = {}; // Static raw memory + alloc_func m_alloc_func = nullptr; // Allocator function, or 0 if default is to be used + free_func m_free_func = nullptr; // Free function, or 0 if default is to be used view_type m_nullstr; view_type m_xmlns_xml; view_type m_xmlns_xmlns; @@ -906,6 +897,7 @@ namespace rapidxml template class xml_node: public xml_base { + using enum node_type; public: using view_type = std::basic_string_view; @@ -1297,12 +1289,12 @@ namespace rapidxml } else { - child->m_prev_sibling = 0; + child->m_prev_sibling = nullptr; m_first_node = child; } m_last_node = child; child->m_parent = this; - child->m_next_sibling = 0; + child->m_next_sibling = nullptr; return child; } optional_ptr> append_node(optional_ptr> ptr) { @@ -1332,7 +1324,7 @@ namespace rapidxml dirty(); if (where == m_first_node) prepend_node(child); - else if (where == 0) + else if (!where) append_node(child); else { @@ -1370,10 +1362,10 @@ namespace rapidxml xml_node *child = m_first_node; m_first_node = child->m_next_sibling; if (child->m_next_sibling) - child->m_next_sibling->m_prev_sibling = 0; + child->m_next_sibling->m_prev_sibling = nullptr; else - m_last_node = 0; - child->m_parent = 0; + m_last_node = nullptr; + child->m_parent = nullptr; } //! Removes last child of the node. @@ -1387,11 +1379,11 @@ namespace rapidxml if (child->m_prev_sibling) { m_last_node = child->m_prev_sibling; - child->m_prev_sibling->m_next_sibling = 0; + child->m_prev_sibling->m_next_sibling = nullptr; } else - m_first_node = 0; - child->m_parent = 0; + m_first_node = nullptr; + child->m_parent = nullptr; } //! Removes specified child from the node @@ -1409,7 +1401,7 @@ namespace rapidxml { where->m_prev_sibling->m_next_sibling = where->m_next_sibling; where->m_next_sibling->m_prev_sibling = where->m_prev_sibling; - where->m_parent = 0; + where->m_parent = nullptr; } } @@ -1418,8 +1410,9 @@ namespace rapidxml { if (!m_first_node) return; dirty(); - for (xml_node *node = m_first_node; node; node = node->m_next_sibling) + for (xml_node *node = m_first_node; node; node = node->m_next_sibling) { node->m_parent = nullptr; + } m_first_node = nullptr; m_last_node = nullptr; } @@ -1437,12 +1430,12 @@ namespace rapidxml } else { - attribute->m_next_attribute = 0; + attribute->m_next_attribute = nullptr; m_last_attribute = attribute; } m_first_attribute = attribute; attribute->m_parent = this; - attribute->m_prev_attribute = 0; + attribute->m_prev_attribute = nullptr; } //! Appends a new attribute to the node. @@ -1458,12 +1451,12 @@ namespace rapidxml } else { - attribute->m_prev_attribute = 0; + attribute->m_prev_attribute = nullptr; m_first_attribute = attribute; } m_last_attribute = attribute; attribute->m_parent = this; - attribute->m_next_attribute = 0; + attribute->m_next_attribute = nullptr; } //! Inserts a new attribute at specified place inside the node. @@ -1477,7 +1470,7 @@ namespace rapidxml dirty_parent(); if (where == m_first_attribute) prepend_attribute(attribute); - else if (where == 0) + else if (!where) append_attribute(attribute); else { @@ -1502,8 +1495,8 @@ namespace rapidxml attribute->m_next_attribute->m_prev_attribute = 0; } else - m_last_attribute = 0; - attribute->m_parent = 0; + m_last_attribute = nullptr; + attribute->m_parent = nullptr; m_first_attribute = attribute->m_next_attribute; } @@ -1521,8 +1514,8 @@ namespace rapidxml m_last_attribute = attribute->m_prev_attribute; } else - m_first_attribute = 0; - attribute->m_parent = 0; + m_first_attribute = nullptr; + attribute->m_parent = nullptr; } //! Removes specified attribute from node. @@ -1539,7 +1532,7 @@ namespace rapidxml { where->m_prev_attribute->m_next_attribute = where->m_next_attribute; where->m_next_attribute->m_prev_attribute = where->m_prev_attribute; - where->m_parent = 0; + where->m_parent = nullptr; } } @@ -1548,8 +1541,9 @@ namespace rapidxml { if (!m_first_attribute) return; dirty_parent(); - for (xml_attribute *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute) + for (xml_attribute *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute) { attribute->m_parent = nullptr; + } m_first_attribute = nullptr; } @@ -1626,6 +1620,7 @@ namespace rapidxml template class xml_document: public xml_node, public memory_pool { + using enum node_type; public: using view_type = std::basic_string_view; @@ -1677,7 +1672,7 @@ namespace rapidxml parse_bom(text); // Parse children - while (1) + while (true) { // Skip whitespace before node skip(text); @@ -1690,9 +1685,8 @@ namespace rapidxml ++text; // Skip '<' if (xml_node *node = parse_node(text)) { this->append_node(node); - if (Flags & (parse_open_only|parse_parse_one)) { - if (node->type() == node_element) - break; + if (Flags & (parse_open_only|parse_parse_one) && node->type() == node_element) { + break; } } } @@ -1714,8 +1708,8 @@ namespace rapidxml template view_type decode_data_value_low(view_type const & v) { - Ch * init = const_cast(v.data()); - Ch * first = init; + auto * init = const_cast(v.data()); + auto * first = init; if (Flags & parse_normalize_whitespace) { skip(first); } else { @@ -1723,9 +1717,9 @@ namespace rapidxml } if (*first == '<') return v; auto buf = this->allocate_string(v); - Ch * start = const_cast(buf.data()); - Ch * tmp = start; - Ch * end = (Flags & parse_normalize_whitespace) ? + auto * start = const_cast(buf.data()); + auto * tmp = start; + auto * end = (Flags & parse_normalize_whitespace) ? skip_and_expand_character_refs(tmp) : skip_and_expand_character_refs(tmp); // Trim trailing whitespace if flag is set; leading was already trimmed by whitespace skip after > diff --git a/rapidxml_predicates.hpp b/rapidxml_predicates.hpp index 83f63b1..1528ecc 100644 --- a/rapidxml_predicates.hpp +++ b/rapidxml_predicates.hpp @@ -30,7 +30,7 @@ namespace rapidxml { bool do_match(const xml_node & t) override { if (m_xmlns.has_value() && t.xmlns() != m_xmlns.value()) return false; - return (t.type() == node_element) && (t.name() == m_name || m_name == "*"); + return (t.type() == node_type::node_element) && (t.name() == m_name || m_name == "*"); } }; @@ -43,7 +43,7 @@ namespace rapidxml { : xpath_base(), m_value(v) {} bool do_match(const xml_node & t) override { - return (t.type() == node_element) && (t.value() == m_value); + return (t.type() == node_type::node_element) && (t.value() == m_value); } }; @@ -56,7 +56,7 @@ namespace rapidxml { : xpath_base(), m_xmlns(v) {} bool do_match(const xml_node & t) override { - return (t.type() == node_element) && (t.xmlns() == m_xmlns); + return (t.type() == node_type::node_element) && (t.xmlns() == m_xmlns); } }; @@ -74,7 +74,7 @@ namespace rapidxml { : xpath_base(), m_name(n), m_value(v), m_xmlns(x) {} bool do_match(const xml_node & t) override { - if (t.type() != node_element) return false; + if (t.type() != node_type::node_element) return false; for (auto const & attr : t.attributes()) { if (m_xmlns.has_value()) { if (m_name == "*" || attr.local_name() != m_name) continue; @@ -100,7 +100,7 @@ namespace rapidxml { } bool do_match(const xml_node & t) override { - return t.type() == node_document || t.type() == node_element; + return t.type() == node_type::node_document || t.type() == node_type::node_element; } }; @@ -117,7 +117,7 @@ namespace rapidxml { } bool do_match(const xml_node & t) override { - return t.type() == node_document || t.type() == node_element; + return t.type() == node_type::node_document || t.type() == node_type::node_element; } }; diff --git a/rapidxml_print.hpp b/rapidxml_print.hpp index ddc8dd4..52ba72a 100644 --- a/rapidxml_print.hpp +++ b/rapidxml_print.hpp @@ -163,7 +163,7 @@ namespace rapidxml template inline OutIt print_data_node(OutIt out, const optional_ptr> node, int flags, int indent) { - assert(node->type() == node_data); + assert(node->type() == node_type::node_data); if (!(flags & print_no_indenting)) out = fill_chars(out, indent, Ch('\t')); if (!node->value_decoded()) { @@ -178,7 +178,7 @@ namespace rapidxml template inline OutIt print_cdata_node(OutIt out, const optional_ptr> node, int flags, int indent) { - assert(node->type() == node_cdata); + assert(node->type() == node_type::node_cdata); if (!(flags & print_no_indenting)) out = fill_chars(out, indent, Ch('\t')); *out = Ch('<'); ++out; @@ -201,7 +201,7 @@ namespace rapidxml template inline OutIt print_element_node(OutIt out, const optional_ptr> node, int flags, int indent) { - assert(node->type() == node_element); + assert(node->type() == node_type::node_element); // Print element name and attributes, if any if (!(flags & print_no_indenting)) @@ -241,7 +241,7 @@ namespace rapidxml } else { out = copy_and_expand_chars(node->value(), Ch(0), out); } - } else if (!child->next_sibling() && child->type() == node_data) { + } else if (!child->next_sibling() && child->type() == node_type::node_data) { // If node has a sole data child, only print its value without indenting if (!child->value_decoded()) { out = copy_chars(child->value_raw(), out); @@ -298,7 +298,7 @@ namespace rapidxml template inline OutIt print_comment_node(OutIt out, const optional_ptr> node, int flags, int indent) { - assert(node->type() == node_comment); + assert(node->type() == node_type::node_comment); if (!(flags & print_no_indenting)) out = fill_chars(out, indent, Ch('\t')); *out = Ch('<'), ++out; @@ -316,7 +316,7 @@ namespace rapidxml template inline OutIt print_doctype_node(OutIt out, const optional_ptr> node, int flags, int indent) { - assert(node->type() == node_doctype); + assert(node->type() == node_type::node_doctype); if (!(flags & print_no_indenting)) out = fill_chars(out, indent, Ch('\t')); *out = Ch('<'), ++out; @@ -338,7 +338,7 @@ namespace rapidxml template inline OutIt print_pi_node(OutIt out, const optional_ptr> node, int flags, int indent) { - assert(node->type() == node_pi); + assert(node->type() == node_type::node_pi); if (!(flags & print_no_indenting)) out = fill_chars(out, indent, Ch('\t')); *out = Ch('<'), ++out; @@ -355,7 +355,7 @@ namespace rapidxml template inline OutIt print_literal_node(OutIt out, const optional_ptr> node, int flags, int indent) { - assert(node->type() == node_literal); + assert(node->type() == node_type::node_literal); if (!(flags & print_no_indenting)) out = fill_chars(out, indent, Ch('\t')); out = copy_chars(node->value(), out); @@ -370,6 +370,7 @@ namespace rapidxml // Print proper node type switch (node->type()) { + using enum node_type; // Document case node_document: diff --git a/sonar-project.properties b/sonar-project.properties index a067ab7..4cd4424 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -13,4 +13,4 @@ sonar.organization=dwd-github #sonar.sourceEncoding=UTF-8 -sonar.exclusions=**/deps/**, **/_deps/**, **/sentry-native/** +sonar.exclusions=**/deps/**, **/_deps/**, **/sentry-native/**, **/*.html diff --git a/test/manipulations.cpp b/test/manipulations.cpp index d1b4c4b..d33954f 100644 --- a/test/manipulations.cpp +++ b/test/manipulations.cpp @@ -17,7 +17,7 @@ namespace { TEST(Create, Node) { rapidxml::xml_document<> doc; - auto node = doc.allocate_node(rapidxml::node_element, "fish", "cakes"); + auto node = doc.allocate_node(rapidxml::node_type::node_element, "fish", "cakes"); doc.append_node(node); EXPECT_EQ( @@ -28,7 +28,7 @@ TEST(Create, Node) { TEST(Create, NodeEmpty) { rapidxml::xml_document<> doc; - auto node = doc.allocate_node(rapidxml::node_element, "fish"); + auto node = doc.allocate_node(rapidxml::node_type::node_element, "fish"); doc.append_node(node); EXPECT_EQ( @@ -39,7 +39,7 @@ TEST(Create, NodeEmpty) { TEST(Create, Node2) { rapidxml::xml_document<> doc; - auto node = doc.allocate_node(rapidxml::node_element, "fish", "cakes"); + auto node = doc.allocate_node(rapidxml::node_type::node_element, "fish", "cakes"); doc.append_node(node); EXPECT_EQ( @@ -56,7 +56,7 @@ std::string const & fn() { TEST(Create, NodeAttr) { rapidxml::xml_document<> doc; - auto node = doc.allocate_node(rapidxml::node_element, "fish", "cakes"); + auto node = doc.allocate_node(rapidxml::node_type::node_element, "fish", "cakes"); auto haddock = doc.allocate_attribute("id", "haddock"); node->append_attribute(haddock); doc.append_node(node); diff --git a/test/xpath.cpp b/test/xpath.cpp index de58d91..abe7bc7 100644 --- a/test/xpath.cpp +++ b/test/xpath.cpp @@ -83,7 +83,7 @@ TEST(XPathFirst, simple_all) { auto xp = rapidxml::xpath<>::parse(sv); auto r = xp->first(doc); ASSERT_TRUE(r); - EXPECT_EQ(r->type(), rapidxml::node_document); + EXPECT_EQ(r->type(), rapidxml::node_type::node_document); } TEST(XPathFirst, simple_any) {