move clang-format-xs into utils

This commit is contained in:
Ailin Nemui 2021-01-06 22:22:00 +01:00
commit 7379833b53
5 changed files with 1 additions and 1 deletions

View file

@ -0,0 +1,117 @@
#!/bin/bash
set -eu
srcdir=$(dirname "$(readlink -f "$0")")
test -z "$srcdir" && srcdir=.
CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
options=()
files=()
lines=()
inplace=0;xml=0;
filename=tmp.1.c
offsets=
off_opts=()
opts=$(getopt -n clang-format-xs -s bash -a -o in -l Werror,assume-filename:,cursor:,dry-run,dump-config,fallback-style:,ferror-limit:,help,length:,lines:,offset:,output-replacement-xmls,sort-includes,style:,verbose,version -- "$@")
if [ $? -ne 0 ]; then exit 1; fi
eval set -- "$opts"; unset opts
while :; do
case "$1" in
--offset)
offsets="$offsets${offsets:+ }$2"
off_opts+=("$1" "$2")
shift 2
continue
;;
--length)
offsets="$offsets:$2"
off_opts+=("$1" "$2")
shift 2
continue
;;
--assume-filename)
filename="$2"
options+=("$1" "$2")
shift 2
continue
;;
--lines)
lines+=("$2")
options+=("$1" "$2")
shift 2
continue
;;
--output-replacements-xml)
xml=1
shift
continue
;;
-i)
inplace=1
shift
continue
;;
--)
shift
break
;;
*)
options+=("$1")
shift
continue
;;
esac
done
files=("$@")
export IN_LINES="${lines[*]}"
export OFFSETS="$offsets"
options_o=("${options[@]}")
if [[ $inplace = 1 ]]; then
options_o=("-i" "${options[@]}")
fi
if [[ $xml = 1 ]]; then
options_o=("-output-replacements-xml" "${options[@]}")
fi
options_o+=("${off_opts[@]}")
do_xs() {
perl "$srcdir"/format-xs-1.pl "$1" > "$1".1.c
$CLANG_FORMAT -i "${options[@]}" -- "$1".1.c
if [[ $xml = 1 ]]; then
perl "$srcdir"/format-xs-2.pl "$1".1.c > "$1".1.xs
perl "$srcdir"/format-xs-xml.pl <(diff -U0 <(od -An -tu1 -w1 -v "$1") <(od -An -tu1 -w1 -v "$1".1.xs))
rm "$1".1.xs
elif [[ $inplace = 1 ]]; then
perl "$srcdir"/format-xs-2.pl "$1".1.c > "$1"
else
perl "$srcdir"/format-xs-2.pl "$1".1.c
fi
rm "$1".1.c
}
if [[ ${#files[@]} -eq 0 ]]; then
case "$filename" in
*.xs)
cat > "$filename".1.xs
do_xs "$filename".1.xs
rm "$filename".1.xs
;;
*)
$CLANG_FORMAT "${options_o[@]}"
;;
esac
else
for file in "${files[@]}"; do
case "$file" in
*.xs)
do_xs "$file"
;;
*)
$CLANG_FORMAT "${options_o[@]}" -- "$file"
;;
esac
done
fi

View file

@ -0,0 +1,73 @@
use strict;
use warnings;
my $in_code = 1;
my @lines = $ENV{IN_LINES} =~ /(\d+):(\d+)/g;
sub in_lines {
if (@lines) {
for (my $i = 0; $i < @lines; $i += 2) {
if ($_[0] >= $lines[$i] && $_[0] <= $lines[$i + 1]) {
return 1;
}
}
return '';
} else {
return 1;
}
}
while (<>) {
chomp;
my $copy;
my $prot = 0;
my $code = $in_code;
if (/^#(define|undef|include|if)/) {
$copy = 1;
}
elsif (/^#[* ]/) {
$prot = 1;
}
elsif (/^[A-Z_]+\s*=/) {
$prot = 2;
$in_code = 0;
}
elsif (/^((PP)?CODE|PREINIT):/) {
$prot = 1;
$in_code = 3;
}
elsif (/^[A-Z_]*:/) {
$prot = 1;
$in_code = 0;
}
elsif (/^[\w:* ]+\s*$/) {
$in_code = 0;
}
if ($prot || (!$copy && !$in_code)) {
if ($code == 2) {
print "}/* -xs- */";
}
if ($prot == 2) {
print "/* clang-format off */";
}
s/^\s+/\t/ if in_lines($.);
print "/* =xs= $_ */";
if ($prot == 2) {
print "/* clang-format on */";
}
if ($in_code == 3) {
print "{";
$in_code = 2;
}
print "\n";
}
elsif ($copy) {
print "$_\n";
}
elsif ($in_code) {
print "$_\n";
}
}
if ($in_code) {
print "}/* -xs- */\n";
}

View file

@ -0,0 +1,10 @@
use strict;
use warnings;
my $doc = do { local $/; <> };
$doc =~ s{\}[ \t]*/[*] -xs- [*]/\s*}{}g;
$doc =~ s{^[ \t]*?/[*] =xs= (.*?) [*]/(\s*\{$)?}{$1}gms;
$doc =~ s{^[ \t]*?/[*] clang-format off [*]//[*] =xs= (.*?) [*]/\s?/[*] clang-format on [*]/(\s*\{$)?}{$1}gms;
print $doc;

View file

@ -0,0 +1,44 @@
use strict;
use warnings;
my @diff = <>;
if (@diff && $diff[0] !~ /^---/) {
die "Not valid diff output";
}
my @offs = $ENV{OFFSETS} =~ /(\d+):(\d+)/g;
sub in_off {
if (@offs) {
for (my $i = 0; $i < @offs; $i += 2) {
if ($_[0] + $_[1] >= $offs[$i] && $_[0] <= $offs[$i] + $offs[$i + 1]) {
return 1;
}
}
return '';
} else {
return 1;
}
}
print "<?xml version='1.0'?>
<replacements xml:space='preserve' incomplete_format='false'>\n";
my $open;
for (@diff) {
if (/^\@\@ -(\d+)(,(\d+))? /) {
if ($open) {
print "</replacement>\n";
}
if (in_off($1-($3//1?1:0), $3//1)) {
print "<replacement offset='@{[$1-($3//1?1:0)]}' length='@{[$3//1]}'>";
$open = 1;
} else {
$open = 0;
}
}
elsif (/^[+] +(\d+)/ && $open) {
print "&#$1;";
}
}
if ($open) {
print "</replacement>\n";
}
print "</replacements>\n";