| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use Getopt::Std; |
| 5 |
use strict; |
| 6 |
|
| 7 |
sub HELP_MESSAGE |
| 8 |
{ |
| 9 |
my $fh = shift; |
| 10 |
print $fh <<"EOF" |
| 11 |
Usage: $0 [options] [files] |
| 12 |
|
| 13 |
Correct indentation, bracing, and other code style in the Crawl repository. |
| 14 |
|
| 15 |
If no files are specified, defaults to files found beneath the current |
| 16 |
directory, modified by the following options (which have no effect if file |
| 17 |
arguments were provided): |
| 18 |
-a Check the whole repository, not just the current directory. |
| 19 |
-m Check only files that have been modified and added to the index. |
| 20 |
-M Check only files that have been modified. |
| 21 |
|
| 22 |
Other options are: |
| 23 |
-n Dry run: Do not actually modify files. |
| 24 |
-A Do not add missing braces around blocks. |
| 25 |
-h, --help Display this help and exit. |
| 26 |
|
| 27 |
Exit status: |
| 28 |
0 if there were no style errors, or if errors were corrected. |
| 29 |
1 if there are still style errors (only with the -n option). |
| 30 |
2 if an unknown option was supplied. |
| 31 |
EOF |
| 32 |
} |
| 33 |
|
| 34 |
our ($opt_a, $opt_A, $opt_m, $opt_M, $opt_n, $opt_h); |
| 35 |
|
| 36 |
# Send --help to stdout, and exit (with success) when it is provided. |
| 37 |
$Getopt::Std::STANDARD_HELP_VERSION = 1; |
| 38 |
# Fail, and display help to stderr, on a bad option. |
| 39 |
getopts('aAmMnh') |
| 40 |
or do { HELP_MESSAGE(\*STDERR); exit 2; }; |
| 41 |
# Make -h work the same as --help. |
| 42 |
$opt_h and do { HELP_MESSAGE(\*STDOUT); exit 0; }; |
| 43 |
|
| 44 |
my $top_level = $opt_a; |
| 45 |
my $add_braces = !$opt_A; |
| 46 |
my $modified_only = $opt_m || $opt_M; |
| 47 |
my $modified_cached = $opt_m && !$opt_M; |
| 48 |
my $dry_run = $opt_n; |
| 49 |
|
| 50 |
my $any_bad = 0; |
| 51 |
|
| 52 |
my @files = @ARGV; |
| 53 |
undef $/; |
| 54 |
unless (@files) |
| 55 |
{ |
| 56 |
my $git_cmd = "git ls-files"; |
| 57 |
if ($modified_only) |
| 58 |
{ |
| 59 |
my $cached = $modified_cached ? "--cached" : ""; |
| 60 |
$git_cmd = "git diff-index -M --name-only $cached --relative HEAD"; |
| 61 |
} |
| 62 |
|
| 63 |
if ($top_level) |
| 64 |
{ |
| 65 |
my $tldir = `git rev-parse --show-toplevel`; |
| 66 |
do { local $/ = "\n"; chomp $tldir; }; |
| 67 |
chdir($tldir) or die "(-a) cannot chdir to $tldir: $!"; |
| 68 |
} |
| 69 |
@files = (grep /\.(cc|h)$/ && !/(^|\/)prebuilt\//, split /\n/, `$git_cmd`); |
| 70 |
} |
| 71 |
|
| 72 |
sub rebrace($$$) |
| 73 |
{ |
| 74 |
my ($condition, $indent, $body) = @_; |
| 75 |
my $orig = "$condition$body"; |
| 76 |
|
| 77 |
# Don't add braces if it's only two lines. |
| 78 |
return $orig unless ($orig =~ /\n.*\n.*\n/) or ($condition =~ /^ *do/); |
| 79 |
|
| 80 |
# Don't add braces if the "body" consists of only comments. |
| 81 |
return $orig unless $body =~ m&^\s++(?!//)&s; |
| 82 |
|
| 83 |
return "$condition$indent\{\n$body$indent\}\n"; |
| 84 |
} |
| 85 |
|
| 86 |
for my $f (@files) |
| 87 |
{ |
| 88 |
next if $f =~ /catch_amalgamated\.cc/; |
| 89 |
open F, "<", $f or (print(STDERR "Can't read $f\n"), next); |
| 90 |
my $old = $_ = <F>; |
| 91 |
close F; |
| 92 |
|
| 93 |
# Eliminate braces around one-line blocks. |
| 94 |
s&^( +(?:if|while|for|else)\b[^\n]*)\n +{\n( *[^/ }][^\n]*)\n +}$&$1\n$2&msg; |
| 95 |
|
| 96 |
# Add braces in blocks that need them. |
| 97 |
s/# $1 = the keyword and condition |
| 98 |
( |
| 99 |
# $2 = first-line indent |
| 100 |
^(\s+) (?:if|while|for|else)\b \s* |
| 101 |
# $3 = nested parenthesised expression, possibly multi-line |
| 102 |
( |
| 103 |
\( |
| 104 |
(?: [^()]* (?3))* |
| 105 |
[^()]* |
| 106 |
\) |
| 107 |
) \s*\n |
| 108 |
) |
| 109 |
# $4 = the body |
| 110 |
( |
| 111 |
# Higher indent than the keyword. |
| 112 |
\2 \s++ |
| 113 |
# Not another flow control statement or braces |
| 114 |
(?! { | (?:if|while|for|do|else)\b) [^\n]*\n |
| 115 |
# And possibly more lines of higher indent than the keyword |
| 116 |
(?: \2 \s+ [^\n]* \n)* |
| 117 |
) |
| 118 |
/rebrace($1,$2,$4)/egsmx if $add_braces; |
| 119 |
|
| 120 |
s/# $1 = the keyword |
| 121 |
( |
| 122 |
# $2 = first-line indent |
| 123 |
^(\s+) do\b \s*\n |
| 124 |
) |
| 125 |
# $3 = the body |
| 126 |
( |
| 127 |
# Higher indent than the keyword. |
| 128 |
\2 \s++ |
| 129 |
# Not another flow control statement or braces |
| 130 |
(?! { | (?:if|while|for|do|else)\b) [^\n]*\n |
| 131 |
# And possibly more lines of higher indent than the keyword |
| 132 |
(?: \2 \s+ [^\n]* \n)* |
| 133 |
) |
| 134 |
/rebrace($1,$2,$3)/egsmx if $add_braces; |
| 135 |
|
| 136 |
# return is not a function, eliminate totally enclosing parentheses. |
| 137 |
# This part handles parentheses-less payloads. |
| 138 |
while (/^( *)return \(([^()]+)\);/sm) |
| 139 |
{ |
| 140 |
# Done this roundabout way to properly unindent multiline blocks. |
| 141 |
my $prev = "$`$1return "; |
| 142 |
my $next = ";$'"; |
| 143 |
my $cur = $2; |
| 144 |
$cur =~ s/\n /\n/sg; |
| 145 |
$_ = "$prev$cur$next"; |
| 146 |
} |
| 147 |
|
| 148 |
# return (x) where x contains parentheses. |
| 149 |
# Looks like no one told Larry Wall properties of regular expression, |
| 150 |
# including the part where they can't do arbitrarily nested parentheses. |
| 151 |
while (/^(\s*)return\s+ |
| 152 |
\(( |
| 153 |
(?: [^()]*+ |
| 154 |
(\( (?: [^()]++ | (?3) )* \)) |
| 155 |
)+ |
| 156 |
[^()]*+ |
| 157 |
)\);/sxm) |
| 158 |
{ |
| 159 |
my $prev = "$`$1return "; |
| 160 |
my $next = ";$'"; |
| 161 |
my $cur = "$2"; |
| 162 |
$cur =~ s/\n /\n/sg; |
| 163 |
$_ = "$prev$cur$next"; |
| 164 |
} |
| 165 |
|
| 166 |
if ($old ne $_) |
| 167 |
{ |
| 168 |
print "$f\n"; |
| 169 |
$any_bad = 1; |
| 170 |
if (!$dry_run) |
| 171 |
{ |
| 172 |
open F, ">", $f or die "Can't write $f\n"; |
| 173 |
print F; |
| 174 |
close F; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if ($dry_run and $any_bad) { |
| 180 |
print "Found unnecessary braces/brackets in the above files.\n"; |
| 181 |
print "Re-run this command (without -n) to automatically remove them.\n"; |
| 182 |
exit 1; |
| 183 |
} |