tools: Make indent_arm_assembly.pl able to reformat a file in place

This allows using the tool for one-off reindentations without needing
the check_arm_indent.sh script (e.g. for use outside of ffmpeg),
without having to pipe the file through stdin/stdout.
This commit is contained in:
Martin Storsjö
2025-11-18 12:13:37 +02:00
parent 140b4f28c3
commit 8b71eeb72e

View File

@@ -32,8 +32,11 @@
# - Optionally align operand columns vertically according to their # - Optionally align operand columns vertically according to their
# maximum width (accommodating for e.g. x0 vs x10, or v0.8b vs v16.16b). # maximum width (accommodating for e.g. x0 vs x10, or v0.8b vs v16.16b).
# #
# The input code is passed to stdin, and the reformatted code is written # The script can be executed as "indent_arm_assembly.pl file [outfile]".
# on stdout. # If no outfile is specified, the given file is overwritten in place.
#
# Alternatively, the if no file parameters are given, the script reads input
# code on stdin, and outputs the reformatted code on stdout.
use strict; use strict;
@@ -41,6 +44,8 @@ my $indent_operands = 0;
my $instr_indent = 8; my $instr_indent = 8;
my $operand_indent = 24; my $operand_indent = 24;
my $match_indent = 0; my $match_indent = 0;
my $file;
my $outfile;
while (@ARGV) { while (@ARGV) {
my $opt = shift; my $opt = shift;
@@ -54,7 +59,13 @@ while (@ARGV) {
} elsif ($opt eq "-match-indent") { } elsif ($opt eq "-match-indent") {
$match_indent = 1; $match_indent = 1;
} else { } else {
die "Unrecognized parameter $opt\n"; if (!$file) {
$file = $opt;
} elsif (!$outfile) {
$outfile = $opt;
} else {
die "Unrecognized parameter $opt\n";
}
} }
} }
@@ -130,7 +141,26 @@ sub columns {
return indentcolumns($rest, 3); return indentcolumns($rest, 3);
} }
while (<STDIN>) { my $in;
my $out;
my $tempfile;
if ($file) {
open(INPUT, "$file") or die "Unable to open $file: $!";
$in = *INPUT;
if ($outfile) {
open(OUTPUT, ">$outfile") or die "Unable to open $outfile: $!";
} else {
$tempfile = "$file.tmp";
open(OUTPUT, ">$tempfile") or die "Unable to open $tempfile: $!";
}
$out = *OUTPUT;
} else {
$in = *STDIN;
$out = *STDOUT;
}
while (<$in>) {
# Trim off trailing whitespace. # Trim off trailing whitespace.
chomp; chomp;
if (/^([\.\w\d]+:)?(\s+)([\w\\][\w\\\.]*)(?:(\s+)(.*)|$)/) { if (/^([\.\w\d]+:)?(\s+)([\w\\][\w\\\.]*)(?:(\s+)(.*)|$)/) {
@@ -201,5 +231,13 @@ while (<STDIN>) {
$_ = $label . $indent . $instr . $operand_space . $rest; $_ = $label . $indent . $instr . $operand_space . $rest;
} }
} }
print $_ . "\n"; print $out $_ . "\n";
}
if ($file) {
close(INPUT);
close(OUTPUT);
}
if ($tempfile) {
rename($tempfile, $file);
} }