[RFC,2/9] Add pass to drop empty XML elements
Commit Message
Certain elements in ABI XML are effectvely containers and can be
dropped if empty and their attributes don't carry ABI information.
- elf-variable-symbols: pure container
- elf-function-symbols: pure container
- namespace-decl: has a name
- abi-instr: compilation unit (path etc.)
- abi-corpus: binary object (architecture)
- abi-corpus-group: binary objects (architecture)
It could be argued that abi-corpus (or abi-corpus-group) should be
kept around to hold the architecture of an object or set of objects.
However, if a binary object has no symbols (say, if it is empty), it
hardly matters what the architecture is.
Note that:
- abidiff rejects XML files with an XML declaration at the top
- abidiff rejects completely empty files
Resolving the first would make the second moot. In the meantime, we
avoid dropping top-level elements.
* scripts/abitidy.pl (drop_if_empty): New variable containing
the tags of elements that can be dropped if empty.
(drop_empty): New Function that removes empty elements, except
top-level ones.
Signed-off-by: Giuliano Procida <gprocida@google.com>
---
scripts/abitidy.pl | 43 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 42 insertions(+), 1 deletion(-)
@@ -105,20 +105,58 @@ sub indent($indent, $node) {
}
}
+# Remove an XML element and any preceeding comment.
+sub remove_node($node) {
+ my $prev = $node->previousSibling();
+ if ($prev && $prev->nodeType == XML_COMMENT_NODE) {
+ $prev->unbindNode();
+ }
+ $node->unbindNode();
+}
+
+# These container elements can be dropped if empty.
+my %drop_if_empty = map { $_ => undef } qw(
+ elf-variable-symbols
+ elf-function-symbols
+ namespace-decl
+ abi-instr
+ abi-corpus
+ abi-corpus-group
+);
+
+# This is a XML DOM traversal as we want post-order traversal so we
+# delete nodes that become empty during the process.
+sub drop_empty;
+sub drop_empty($node) {
+ my $node_name = $node->getName();
+ for my $child ($node->childNodes()) {
+ drop_empty($child);
+ }
+ if (!$node->hasChildNodes() && $node->nodeType == XML_ELEMENT_NODE && exists $drop_if_empty{$node->getName()}) {
+ # Until abidiff accepts empty ABIs, avoid dropping top-level elements.
+ if ($node->parentNode->nodeType == XML_ELEMENT_NODE) {
+ remove_node($node);
+ }
+ }
+}
+
# Parse arguments.
my $input_opt;
my $output_opt;
my $all_opt;
+my $drop_opt;
GetOptions('i|input=s' => \$input_opt,
'o|output=s' => \$output_opt,
'a|all' => sub {
- 1
+ $drop_opt = 1
},
+ 'd|drop-empty!' => \$drop_opt,
) and !@ARGV or die("usage: $0",
map { (' ', $_) } (
'[-i|--input file]',
'[-o|--output file]',
'[-a|--all]',
+ '[-d|--[no-]drop-empty]',
), "\n");
exit 0 unless defined $input_opt;
@@ -131,6 +169,9 @@ close $input;
# This simplifies DOM analysis and manipulation.
strip_text($dom);
+# Drop empty elements.
+drop_empty($dom) if $drop_opt;
+
exit 0 unless defined $output_opt;
# Reformat for human consumption.