tests: remove uses of `xmlKeepBlanksDefault()`
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 30 Apr 2024 19:19:32 +0000 (15:19 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 1 May 2024 14:59:58 +0000 (10:59 -0400)
When compiling against libxml 2.12.6, I get:

      CXX      extract_xml-extract_xml.o
    /home/simark/src/lttng-tools/tests/utils/xml-utils/extract_xml.cpp: In function ‘int main(int, char**)’:
    /home/simark/src/lttng-tools/tests/utils/xml-utils/extract_xml.cpp:256:29: error: ‘int xmlKeepBlanksDefault(int)’ is deprecated [-Werror=deprecated-declarations]
      256 |         xmlKeepBlanksDefault(0);
          |         ~~~~~~~~~~~~~~~~~~~~^~~
    In file included from /home/simark/src/lttng-tools/tests/utils/xml-utils/extract_xml.cpp:29:
    /usr/include/libxml2/libxml/parser.h:957:17: note: declared here
      957 |                 xmlKeepBlanksDefault    (int val);
          |                 ^~~~~~~~~~~~~~~~~~~~

The documentation[1] suggests moving to "the modern options API with
XML_PARSE_NOBLANKS", do that.

Add a new `xml_parser_ctx_uptr` RAII object to manage the lifetime of
the `xmlParserCtxt` object.

[1] https://gnome.pages.gitlab.gnome.org/libxml2/devhelp/libxml2-parser.html#xmlKeepBlanksDefault

Change-Id: I7f9e70d2245fa333e01296aa2b4e3efa4e7fbefa
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/utils/xml-utils/Makefile.am
tests/utils/xml-utils/common.hpp [new file with mode: 0644]
tests/utils/xml-utils/extract_xml.cpp
tests/utils/xml-utils/pretty_xml.cpp

index eec519d3128d5e5799f8f12e7532d5a4db02ef42..5890eef27533c185f1c7033c85b8a0f14f707b3c 100644 (file)
@@ -1,5 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
+EXTRA_DIST = common.hpp
+
 noinst_PROGRAMS = validate_xml extract_xml pretty_xml
 validate_xml_SOURCES = validate_xml.cpp
 validate_xml_CPPFLAGS = $(libxml2_CFLAGS) $(AM_CPPFLAGS)
diff --git a/tests/utils/xml-utils/common.hpp b/tests/utils/xml-utils/common.hpp
new file mode 100644 (file)
index 0000000..5acb1b1
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2024 EfficiOS Inc.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef TESTS_UTILS_XML_UTILS_COMMON_HPP
+#define TESTS_UTILS_XML_UTILS_COMMON_HPP
+
+#include "common/make-unique-wrapper.hpp"
+
+#include <libxml/parser.h>
+#include <memory>
+
+using xml_parser_ctx_uptr = std::unique_ptr<
+       xmlParserCtxt,
+       lttng::memory::create_deleter_class<xmlParserCtxt, xmlFreeParserCtxt>::deleter>;
+
+#endif /* TESTS_UTILS_XML_UTILS_COMMON_HPP */
index 3995dde48a27d9d96485f148222b883f58f4ef10..ad319d3c46c10c01cda3cebd62ba92ef8fbf9993 100644 (file)
@@ -24,6 +24,8 @@
  *     node;b;
  *     node;c;
  */
+#include "common.hpp"
+
 #include <common/defaults.hpp>
 
 #include <libxml/parser.h>
@@ -176,8 +178,15 @@ static int extract_xpath(const char *xml_path, const xmlChar *xpath)
        LTTNG_ASSERT(xml_path);
        LTTNG_ASSERT(xpath);
 
+       xml_parser_ctx_uptr parserCtx{ xmlNewParserCtxt() };
+
+       if (!parserCtx) {
+               fprintf(stderr, "ERR: could not allocate an XML parser context\n");
+               return -1;
+       }
+
        /* Parse the xml file */
-       doc = xmlParseFile(xml_path);
+       doc = xmlCtxtReadFile(parserCtx.get(), xml_path, nullptr, XML_PARSE_NOBLANKS);
        if (!doc) {
                fprintf(stderr, "ERR parsing: xml file invalid \"%s\"\n", xml_path);
                return -1;
@@ -253,7 +262,6 @@ int main(int argc, char **argv)
 
        /* Init libxml */
        xmlInitParser();
-       xmlKeepBlanksDefault(0);
        if (access(argv[optind], F_OK)) {
                fprintf(stderr, "ERR:%s\n", "Xml path not valid");
                return -1;
index 3f296f0235342ca17b5c2e032984899b7071f93b..7eb4710caf78dcc02c285800a4af02a57b48718a 100644 (file)
  * This allows a more human friendly format for xml testing when problems occur.
  */
 
+#include "common.hpp"
+
 #include <libxml/parser.h>
+#include <unistd.h>
 
-int main(void)
+int main()
 {
        xmlDocPtr doc = NULL;
 
        /* Init libxml. */
        xmlInitParser();
-       xmlKeepBlanksDefault(0);
 
-       /* Parse the XML document from stdin. */
-       doc = xmlParseFile("-");
-       if (!doc) {
-               fprintf(stderr, "ERR parsing: xml input invalid");
-               return -1;
-       }
+       {
+               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);
+               xmlDocFormatDump(stdout, doc, 1);
+
+               xmlFreeDoc(doc);
+       }
 
-       xmlFreeDoc(doc);
        /* Shutdown libxml. */
        xmlCleanupParser();
 
This page took 0.027985 seconds and 4 git commands to generate.