Debian Codegraph @

src/deps/miniasync/utils/check_whitespace

raw
1 #!/usr/bin/env perl
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright 2015-2021, Intel Corporation
4
5 #
6 # check_whitespace -- scrub source tree for whitespace errors
7 #
8
9 use strict;
10 use warnings;
11
12 use File::Basename;
13 use File::Find;
14 use Encode;
15 use v5.16;
16
17 my $Me = $0;
18 $Me =~ s,.*/,,;
19
20 $SIG{HUP} = $SIG{INT} = $SIG{TERM} = $SIG{__DIE__} = sub {
21 die @_ if $^S;
22
23 my $errstr = shift;
24
25 die "$Me: ERROR: $errstr";
26 };
27
28 my $Errcount = 0;
29
30 #
31 # err -- emit error, keep total error count
32 #
33 sub err {
34 warn @_, "\n";
35 $Errcount++;
36 }
37
38 #
39 # decode_file_as_string -- slurp an entire file into memory and decode
40 #
41 sub decode_file_as_string {
42 my ($full, $file) = @_;
43 my $fh;
44 open($fh, '<', $full) or die "$full $!\n";
45
46 local $/;
47 $_ = <$fh>;
48 close $fh;
49
50 # check known encodings or die
51 my $decoded;
52 my @encodings = ("UTF-8", "UTF-16", "UTF-16LE", "UTF-16BE");
53
54 foreach my $enc (@encodings) {
55 eval { $decoded = decode( $enc, $_, Encode::FB_CROAK ) };
56
57 if (!$@) {
58 $decoded =~ s/\R/\n/g;
59 return $decoded;
60 }
61 }
62
63 die "$Me: ERROR: Unknown file encoding";
64 }
65
66 #
67 # check_whitespace -- run the checks on the given file
68 #
69 sub check_whitespace {
70 my ($full, $file) = @_;
71
72 my $line = 0;
73 my $eol;
74 my $nf = 0;
75 my $fstr = decode_file_as_string($full, $file);
76 my $empty = 0;
77 my $is_python = $full =~ /\.py$/;
78
79 for (split /^/, $fstr) {
80 $line++;
81 if (!$is_python && /^$/) {
82 $empty++;
83 if ($empty > 1) {
84 err("$full:$line: ERROR duplicated empty line");
85 }
86 } else {
87 $empty = 0;
88 }
89
90 $eol = /[\n]/s;
91 if (/^\.nf$/) {
92 err("$full:$line: ERROR: nested .nf") if $nf;
93 $nf = 1;
94 } elsif (/^\.fi$/) {
95 $nf = 0;
96 } elsif ($nf == 0) {
97 chomp;
98 err("$full:$line: ERROR: trailing whitespace") if /\s$/;
99 err("$full:$line: ERROR: spaces before tabs") if / \t/;
100 }
101 }
102
103 err("$full:$line: .nf without .fi") if $nf;
104 err("$full:$line: noeol") unless $eol;
105 }
106
107 sub check_whitespace_with_exc {
108 my ($full) = @_;
109
110 $_ = $full;
111
112 return 0 if /^[.\/]*src\/common\/queue\.h/;
113 return 0 if /^[.\/]*src\/core\/valgrind\/.*\.h/;
114
115 $_ = basename($full);
116
117 return 0 unless /^(README.*|LICENSE.*|Makefile.*|CMakeLists.txt|.gitignore|TEST.*|RUNTESTS|check_whitespace|.*\.([chp13s]|sh|map|cpp|hpp|inc|PS1|ps1|py|md|cmake))$/;
118 return 0 if -z;
119
120 check_whitespace($full, $_);
121 return 1;
122 }
123
124 my $verbose = 0;
125 my $force = 0;
126 my $recursive = 0;
127
128 sub check {
129 my ($file) = @_;
130 my $r;
131
132 if ($force) {
133 $r = check_whitespace($file, basename($file));
134 } else {
135 $r = check_whitespace_with_exc($file);
136 }
137
138 if ($verbose) {
139 if ($r == 0) {
140 printf("skipped $file\n");
141 } else {
142 printf("checked $file\n");
143 }
144 }
145 }
146
147 my @files = ();
148
149 foreach my $arg (@ARGV) {
150 if ($arg eq '-v') {
151 $verbose = 1;
152 next;
153 }
154 if ($arg eq '-f') {
155 $force = 1;
156 next;
157 }
158 if ($arg eq '-r') {
159 $recursive = 1;
160 next;
161 }
162 if ($arg eq '-g') {
163 @files = `git ls-tree -r --name-only HEAD`;
164 chomp(@files);
165 next;
166 }
167 if ($arg eq '-h') {
168 printf "Options:
169 -g - check all files tracked by git
170 -r dir - recursively check all files in specified directory
171 -v verbose - print whether file was checked or not
172 -f force - disable blacklist\n";
173 exit 1;
174 }
175
176 if ($recursive == 1) {
177 find(sub {
178 my $full = $File::Find::name;
179
180 if (!$force &&
181 ($full eq './.git' ||
182 $full eq './src/debug' ||
183 $full eq './src/nondebug' ||
184 $full eq './rpmbuild' ||
185 $full eq './dpkgbuild')) {
186 $File::Find::prune = 1;
187 return;
188 }
189
190 return unless -f;
191
192 push @files, $full;
193 }, $arg);
194
195 $recursive = 0;
196 next;
197 }
198
199 push @files, $arg;
200 }
201
202 if (!@files) {
203 printf "Empty file list!\n";
204 }
205
206 foreach (@files) {
207 check($_);
208 }
209
210 exit $Errcount;