From: Simon Marchi Date: Tue, 30 Apr 2024 19:19:32 +0000 (-0400) Subject: tests: remove uses of `xmlKeepBlanksDefault()` X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=051356a81761338485706e779d046d7fb8dbfe1d;p=lttng-tools.git tests: remove uses of `xmlKeepBlanksDefault()` 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 Signed-off-by: Jérémie Galarneau --- diff --git a/tests/utils/xml-utils/Makefile.am b/tests/utils/xml-utils/Makefile.am index eec519d31..5890eef27 100644 --- a/tests/utils/xml-utils/Makefile.am +++ b/tests/utils/xml-utils/Makefile.am @@ -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 index 000000000..5acb1b1be --- /dev/null +++ b/tests/utils/xml-utils/common.hpp @@ -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 +#include + +using xml_parser_ctx_uptr = std::unique_ptr< + xmlParserCtxt, + lttng::memory::create_deleter_class::deleter>; + +#endif /* TESTS_UTILS_XML_UTILS_COMMON_HPP */ diff --git a/tests/utils/xml-utils/extract_xml.cpp b/tests/utils/xml-utils/extract_xml.cpp index 3995dde48..ad319d3c4 100644 --- a/tests/utils/xml-utils/extract_xml.cpp +++ b/tests/utils/xml-utils/extract_xml.cpp @@ -24,6 +24,8 @@ * node;b; * node;c; */ +#include "common.hpp" + #include #include @@ -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; diff --git a/tests/utils/xml-utils/pretty_xml.cpp b/tests/utils/xml-utils/pretty_xml.cpp index 3f296f023..7eb4710ca 100644 --- a/tests/utils/xml-utils/pretty_xml.cpp +++ b/tests/utils/xml-utils/pretty_xml.cpp @@ -10,26 +10,34 @@ * This allows a more human friendly format for xml testing when problems occur. */ +#include "common.hpp" + #include +#include -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();