Clean-up: modernize pretty_xml.cpp master
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 1 May 2024 15:21:04 +0000 (11:21 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 1 May 2024 16:08:00 +0000 (16:08 +0000)
Since pretty_xml was converted to C++, clang-tidy had a number of
grievances such as the use of NULL (instead of nullptr).

Since the file is very small, it is modernized to address those issues:
  - Wrap libxml2 resources in RAII wrappers
  - Use C++ IO APIs in lieu of fprintf

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: Ie90e3e05130f7916f411e0de36e8aac72a0f790c

tests/utils/xml-utils/common.hpp
tests/utils/xml-utils/extract_xml.cpp
tests/utils/xml-utils/pretty_xml.cpp

index 5acb1b1beafd48a70395d859a7ac64dd0df6b80d..01a9b5207b7bdaf2463b7e478de37ae9b4ae5956 100644 (file)
 #include <libxml/parser.h>
 #include <memory>
 
-using xml_parser_ctx_uptr = std::unique_ptr<
+namespace lttng {
+namespace libxml {
+
+using parser_ctx_uptr = std::unique_ptr<
        xmlParserCtxt,
        lttng::memory::create_deleter_class<xmlParserCtxt, xmlFreeParserCtxt>::deleter>;
+using doc_uptr =
+       std::unique_ptr<xmlDoc, lttng::memory::create_deleter_class<xmlDoc, xmlFreeDoc>::deleter>;
+
+/*
+ * Manage the global parser context of libxml2.
+ * There should only be one instance of this class per process.
+ */
+class global_parser_context {
+public:
+       global_parser_context()
+       {
+               xmlInitParser();
+       }
+
+       ~global_parser_context()
+       {
+               xmlCleanupParser();
+       }
 
+       /* Deactivate copy and assignment. */
+       global_parser_context(const global_parser_context&) = delete;
+       global_parser_context(global_parser_context&&) = delete;
+       global_parser_context& operator=(const global_parser_context&) = delete;
+       global_parser_context& operator=(global_parser_context&&) = delete;
+};
+} /* namespace libxml */
+} /* namespace lttng */
 #endif /* TESTS_UTILS_XML_UTILS_COMMON_HPP */
index ad319d3c46c10c01cda3cebd62ba92ef8fbf9993..280f2ed0284b80736ce92485479c488ce75aad1b 100644 (file)
@@ -38,6 +38,8 @@
 #include <string.h>
 #include <unistd.h>
 
+namespace ll = lttng::libxml;
+
 #if defined(LIBXML_XPATH_ENABLED)
 
 static int opt_verbose;
@@ -178,7 +180,7 @@ static int extract_xpath(const char *xml_path, const xmlChar *xpath)
        LTTNG_ASSERT(xml_path);
        LTTNG_ASSERT(xpath);
 
-       xml_parser_ctx_uptr parserCtx{ xmlNewParserCtxt() };
+       ll::parser_ctx_uptr parserCtx{ xmlNewParserCtxt() };
 
        if (!parserCtx) {
                fprintf(stderr, "ERR: could not allocate an XML parser context\n");
index 7eb4710caf78dcc02c285800a4af02a57b48718a..8a6e967a71da8c73c687fcf6c3cec23d88ce4fad 100644 (file)
 
 #include "common.hpp"
 
+#include <common/scope-exit.hpp>
+
+#include <iostream>
 #include <libxml/parser.h>
 #include <unistd.h>
 
+namespace ll = lttng::libxml;
+
 int main()
 {
-       xmlDocPtr doc = NULL;
-
-       /* Init libxml. */
-       xmlInitParser();
-
-       {
-               xml_parser_ctx_uptr parserCtx{ xmlNewParserCtxt() };
-
-               /* Parse the XML document from stdin. */
-               doc = xmlCtxtReadFd(
-                       parserCtx.get(), STDIN_FILENO, nullptr, nullptr, XML_PARSE_NOBLANKS);
-               if (!doc) {
-                       fprintf(stderr, "ERR parsing: xml input invalid");
-                       return -1;
-               }
-
-               xmlDocFormatDump(stdout, doc, 1);
-
-               xmlFreeDoc(doc);
+       const ll::global_parser_context global_parser_context;
+       const ll::parser_ctx_uptr parserCtx{ xmlNewParserCtxt() };
+
+       /* Parse the XML document from stdin. */
+       const ll::doc_uptr doc{ xmlCtxtReadFd(
+               parserCtx.get(), STDIN_FILENO, nullptr, nullptr, XML_PARSE_NOBLANKS) };
+       if (!doc) {
+               std::cerr << "Error: invalid XML input on stdin\n";
+               return -1;
        }
 
-       /* Shutdown libxml. */
-       xmlCleanupParser();
+       xmlDocFormatDump(stdout, doc.get(), 1);
 
        return 0;
 }
This page took 0.029122 seconds and 4 git commands to generate.