2 # Copyright (c) 2007-2017 Olly Betts
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to
6 # deal in the Software without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 # sell copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 if (defined $ARGV[0] && $ARGV[0] eq '--help') {
30 Nit-pick Xapian patches.
32 A patch can be supplied on stdin, or one or more patch files listed on the
35 Produces output suitable for use with vim's quick-fix mode, and similar
36 features in other editors.
40 git diff master.. | xapian-check-patch > tmp.qf
50 my ($type, $msg, $fullline) = @_;
51 print "$fnm:$lineno: $type: $msg";
52 if (defined $fullline) {
63 # SVN property changes don't have an "Index: [...]" line.
65 my $check_trailing = 0;
66 my $check_space_tab = 0;
69 my $header_guard_macro;
70 my $last_first_char = '';
73 if (/^Index: (.+)/ || m!^diff --git a/.+ b/(.+)!) {
79 $header_guard_macro = undef;
83 if ($fnm =~ /\.cc$/) {
84 if ($fnm !~ m!\b(?:cdb|portability/)! &&
85 $fnm !~ m!\bcommon/getopt\.cc$! &&
86 $fnm !~ m!\bomega/md5\.cc$! &&
87 $fnm !~ m!\bcommon/msvc_dirent\.cc$!) {
89 $want_tabs = 1 unless ($fnm =~ m!\blanguages/steminternal\.cc$!);
91 } elsif ($fnm =~ /\.c$/) {
92 if ($fnm !~ m!\blanguages/compiler/! &&
93 $fnm !~ m!/lemon\.c$! &&
94 $fnm !~ m!/xapdep\.c$!) {
98 } elsif ($fnm =~ /\.h$/) {
99 if ($fnm !~ m!\binclude/xapian/intrusive_ptr\.h! &&
100 $fnm !~ m!\blanguages/compiler/! &&
101 $fnm !~ m!\bcommon/msvc_dirent\.h$!) {
103 $want_tabs = 1 unless ($fnm =~ m!/omega/cdb!);
105 } elsif ($fnm =~ /\.py(?:\.in)?$/) {
108 } elsif ($fnm =~ m!(?:^|/)ChangeLog\b!) {
114 $fnm !~ m!\bcommon/msvc_dirent\.! &&
115 $fnm !~ m!/lemon\.c$! &&
116 $fnm !~ m!/queryparser\.lt$! &&
118 $fnm !~ m!/testdata/etext\.txt$!;
121 # print STDERR "$fnm: lang=" . ($lang // "UNKNOWN") . "\;
124 my $pre3 = substr($_, 0, 3);
125 if ($pre3 eq '@@ ') {
126 /^\@\@ -\d+,\d+ \+(\d+),\d+\b/ and $lineno = $1;
130 if ($pre3 eq '---' || $pre3 eq '+++') {
134 if (defined $lang && ($lang eq 'c++' || $lang eq 'h' || $lang eq 'c')) {
135 # Uncomment commented out parameter names: foo(int /*bar*/) -> foo(int bar)
136 s!/\*([A-Za-z_][A-Za-z_0-9]*)\*/([,)])!$1$2!g;
138 # Check for comments without a space before the comment text.
139 if (m!^\+.*\s/[*/]{1,2}[A-Za-z0-9]!) {
140 diagnostic
('error', "Missing space between comment characters and comment text", $fullline);
144 if (s!/(?:\*.*?\*/|/.*)!!g) {
151 # Trim content of comments ending on this line:
152 if (s!^(.).*\*/!$1*/!) {
158 # Drop comment content for "*" continuation lines (when /* isn't in hunk):
159 s/^(.)(\s*\*).*/$1$2/;
161 } elsif (defined $lang && $lang eq 'py') {
168 # Replace multiple spaces before line continuation marker:
171 if (defined $lang && ($lang eq 'c++' || $lang eq 'h' || $lang eq 'c') && !(/^[-+]\s*\#/)) {
172 if (m
,^\
+\s
+LOGCALL
(?
:_
[A
-Z0
-9]+)*\
([^"]*"[^"]*(?<!operator)\(,) {
173 diagnostic('error', "Don
't include parentheses in debug logging method/class name", $fullline);
175 # Replace string literals containing escaped quotes:
177 my $quote = substr($_, $-[0], 1);
181 QUOTELOOP: while (1) {
182 if ($i >= length($_)) {
183 $_ = substr($_, 0, $start) . "X
\n";
186 my $c = substr($_, $i, 1);
188 $_ = substr($_, 0, $start) . "X
" . substr($_, $i);
190 # See if there's another string after this one:
191 while ($i != length($_)) {
192 $c = substr($_, $i, 1);
194 if ($c eq '"' || $c eq "'") {
205 $c = substr($_, $i, 1);
207 ++$i while (substr($_, $i, 1) =~ /^[A-Fa-f0-9]$/);
209 } elsif ($c =~ /^[0-7]/) {
211 ++$i while ($i - $j <= 3 && substr($_, $i, 1) =~ /^[0-7]$/);
213 } elsif ($c eq '"' || $c eq "'") {
222 if ($check_trailing && /^\+.*[ \t]$/) {
223 diagnostic('error', "added
/changed line has trailing whitespace
", $fullline);
225 if ($check_space_tab && /^\+.* \t/) {
226 diagnostic('error', "added
/changed line has space before tab
", $fullline);
228 if ($want_tabs == 1 and /^\+\t* {8}/) {
229 diagnostic('error', "added
/changed line uses spaces
for indentation rather than tab
", $fullline);
231 if (!$want_tabs and /^\+ *\t/) {
232 diagnostic('error', "added
/changed line uses tab
for indentation rather than spaces
", $fullline);
234 if ((!defined $lang || $lang ne 'changelog') && $fullline =~ /^([-+]).*\bFIX(?:ME)\b/) {
235 # Break up the string in the regexp above and messages below to avoid
236 # this triggering on its own code!
238 # Not an error, but interesting information.
239 diagnostic('info', "FIX
"."ME removed
", $fullline);
241 # Not an error, but not good.
242 diagnostic('warning', "FIX
"."ME added
", $fullline);
245 if (defined $lang && $lang ne 'changelog' && /^\+.*\\([abcefp]|brief|code|deprecated|endcode|exception|file|internal|li|param|private|return|todo)\b/) {
246 diagnostic('error', "Doxygen command
'\\$1' introduced by
'\\' not '\@'", $fullline);
248 if (defined $lang && $lang ne 'changelog' && /^\+.*@\s+([abcefp]|brief|code|deprecated|endcode|exception|file|internal|li|param|private|return|todo)\b/) {
249 diagnostic('error', "Broken Doxygen command
: whitespace between
'\@' and '$1'", $fullline);
251 if (defined $lang && ($lang eq 'c++' || $lang eq 'h' || $lang eq 'c')) {
252 if ($check_space_tab && /^\+( (?:| | | ))[^ \t#].*(?:[^)];|[^);,])\n/) {
253 # Exclude lines ending ');', ')', or ',' to avoid reporting for wrapped function arguments.
254 diagnostic('error', "line indented by
".length($1)." spaces
", $fullline);
256 if (m!^\+\s*(case|catch|class|do|for|if|namespace|struct|switch|try|union)\b([^ ]| \s)!) {
257 diagnostic('error', "'$1' not followed by exactly one space
", $fullline);
259 if (m!^\+.*;[^\s\\]!) {
260 diagnostic('error', "Missing space after
';'", $fullline);
262 if (m!^\+.*[^(;]\s;!) {
263 # Stuff like this is OK: for ( ; ; ) {
264 # though for that exact case I'd suggest: while (true) {
265 diagnostic('error', "Whitespace before
';'", $fullline);
267 if (m!^\+.*?\b(return)\b([^ ;]| \s)!) {
268 diagnostic('error', "'$1' not followed by exactly one space
", $fullline);
270 if (m!^\+.*?\b(else)\b([^ \n]| \s)!) {
271 diagnostic('error', "'$1' not followed by exactly one space
", $fullline);
273 if (m!^\+.*?\b(while)\b([^ ]| \s)!) {
274 diagnostic('error', "'$1' not followed by exactly one space
", $fullline);
276 if (m!^\+.*?(?:}|}\s{2,}|}\t|^[^}]*)\b(catch)\b!) {
277 diagnostic('error', "'$1' not preceded by exactly
'} '", $fullline);
279 if (m!^\+.*?(?:}|}\s{2,}|}\t)\b(else|while)\b!) {
280 diagnostic('error', "'}' and '$1' not separated by exactly one space
", $fullline);
282 if (m!^\+.*\((?: [^;]|\t)!) {
283 # Allow: for ( ; i != 10; ++i)
284 diagnostic('error', "Whitespace after
'('", $fullline);
286 if (m!^\+.*\H.*\h\)!) {
287 diagnostic('error', "Whitespace before
')'", $fullline);
289 if (m!^\+.*;\s*(\w+)([-+]{2})\)!) {
290 diagnostic('error', "Prefer
'$2$1' to
'$1$2'", $fullline);
292 if (m,^\+\s*[^#].*[\w)](?!-[->]|\+\+)((?:\&\&|\|\||<<|>>|[-+/*%~=<>!&|^])=?|[?]),) {
296 if (substr($_, $pre[1] - 8, 8) eq 'operator') {
298 } elsif ($op eq '>' && substr($_, 0, $pre[1]) =~ /[A-Za-z0-9_]</) {
299 # y = static_cast<char>(x);
300 } elsif ($op eq '>') {
301 } elsif ($op eq '<' && substr($_, $pre[1] - 1, 1) =~ /^[A-Za-z0-9_]$/ && substr($_, $post[1]) =~ />/) {
302 # y = static_cast<char>(x);
303 } elsif ($op eq '<' &&
304 substr($_, 0, $pre[1]) =~ /\b(?:list|map|multimap|multiset|priority_queue|set|template|unordered_map|unordered_set|vector)$/) {
305 # y = priority_queue<Foo*,
307 # template<typename A,
309 } elsif ($op eq '&&' && substr($_, $pre[1] - 4, 4) eq 'auto') {
311 } elsif (($op eq '<<' || $op eq '>>') &&
312 substr($_, 0, $pre[1]) =~ /\b(?:0x[0-9a-fA-F]+|[0-9]+)$/ &&
313 substr($_, $post[1]) =~ /^(?:0x[0-9a-fA-F]+|[0-9]+)\b/) {
315 } elsif (($op eq '-' || $op eq '+') &&
316 substr($_, 0, $pre[1]) =~ /[0-9]\.?e$/) {
318 } elsif ($op eq '>>' &&
319 /[A-Za-z0-9_]<.+</) {
320 # vector<vector<int>> v;
321 } elsif ($op =~ /^[*&|]$/) {
322 # FIXME: *: const char* x;
323 # FIXME: &: const char& x;
326 diagnostic('error', "Missing space before
'$op'", $fullline);
329 if (m@^\+\s*[^#\s].*?((?:\&\&|\|\||<<|>>|[-+/*%~=<>!&|^])=?|[?:,])(?<!(?:-[->]|\+\+|::))(?:[\w\(\.\{!"']| \s)@) {
333 if ($op eq '~' && substr($_, $post[1]) =~ /^[A-Za-z][A-Za-z0-9_]*\(/) {
334 # Destructor - e.g. ~Foo();
335 } elsif (($op eq '-' || $op eq '+' || $op eq '!' || $op eq '~') &&
336 substr($_, 0, $pre[1]) =~ m@(?:[-+/*%~=<>&|,;?:] |[\[(]|\b(?:return|case) |^\+\s*)$@) {
337 # Unary -, +, !, ~: e.g. foo = +1; bar = x * (-y); baz = a * -b;
338 } elsif ($op eq ',' && (
339 /\b(?:AssertRel(?:Paranoid)?|TEST_REL)\(/ ||
342 } elsif ($op eq '>>' &&
343 /[A-Za-z0-9_]<.+</) {
344 # vector<vector<int>>&
345 } elsif ($op =~ /^[*&<>|]$/) {
346 # FIXME: *: const char *x;
347 # FIXME: *: const char &x;
348 # FIXME: < >: y = static_cast<char>(x);
350 } elsif (substr($_, $pre[1] - 8, 8) eq 'operator
') {
352 } elsif (($op eq '<<' || $op eq '>>') &&
353 substr($_, 0, $pre[1]) =~ /\b(?:0x[0-9a-fA-F]+|[0-9]+)$/ &&
354 substr($_, $post[1]) =~ /^(?:0x[0-9a-fA-F]+|[0-9]+)\b/) {
356 } elsif (($op eq '-' || $op eq '+') &&
357 substr($_, 0, $pre[1]) =~ /[0-9]\.?e$/) {
360 diagnostic('error
', "Should have exactly one space after '$op'", $fullline);
364 diagnostic('error
', "Extra ';' at end of line", $fullline);
366 if (m@^\+[^#]*?[^#\h] +(,|->)@) {
367 diagnostic('error
', "Space before '$1'", $fullline);
369 if (m,^\+[^#]*?[^#\h] ,) {
370 diagnostic('error
', "Multiple spaces", $fullline);
372 if (m!^\+(?:.*[;{])?\s*/[/*]{1,2}\w!) {
373 diagnostic('error
', "added/changed line has comment without whitespace before the text", $fullline);
376 diagnostic('error
', "No space between ')' and '{'", $fullline);
378 if ($fnm !~ m!/(?:md5|posixy_wrapper|perftest)\.cc$! &&
379 m,^\+.*[^\w\.>]([a-z][a-z0-9]*[A-Z]\w*),) {
381 if ($symbol eq 'gzFile
' || $symbol eq 'uInt
' || $symbol =~ /^(?:de|in)flate[A-Z]/) {
382 # Whitelist symbols from APIs we use.
383 } elsif ($symbol =~ /^[gs]et[A-Z]$/) {
384 # For now, allow setD(), etc.
385 } elsif ($symbol =~ /^h(?:File|Read|Write|Pipe|Client)$/ || $symbol eq 'fdwCtrlType
' || $symbol eq 'pShutdownSocket
') {
386 # Platform specific names, allow for now.
388 diagnostic('error
', "camelCase identifier '$1' - Xapian coding convention is to use lower case and underscores for variables and functions, and CamelCase for class names", $fullline);
391 if ($lineno == 1 && m!^\+!) {
392 if (m!^/\*\* \@file (\S+)!) {
394 if (length $fnm == length $at_file ||
395 (length $fnm > length $at_file && substr($fnm, -length $at_file - 1, 1) eq '/') &&
396 substr($fnm, -length $at_file) eq $at_file) {
399 diagnostic('error
', "\@file doesn't match filename
", $fullline);
402 diagnostic('error', "\
@file missing
", $fullline);
405 if (/^\+.*\b(?:class|struct)\b.*:\s*$/) {
406 diagnostic('error', "Inheritance list
split after
':', should be before
", $fullline);
408 # Try to distinguish ternary operator (?:) correctly split after ":" vs
409 # constructor initialiser list incorrectly split after ":".
410 my $last_in_ternary = $in_ternary;
411 $in_ternary = / \?(?: |$)/;
412 if (!$last_in_ternary && !$in_ternary && /^\+.*\)\s*:\s*$/) {
413 diagnostic('error', "Constructor initialiser list
split after
':', should be before
", $fullline);
415 if (m,^\+\s+([-+/%^]|[&|]{2})\s,) {
416 diagnostic('error', "Expression
split before operator
'$1', should be after
", $fullline);
419 if (m!^\+\s*#\s*(ifndef|define|endif\s*/[*/])\s+((?:[A-Z]+_INCLUDED)?_?\w+_[Hh]\b)!) {
420 my ($type, $guard) = ($1, $2);
422 if (!defined $header_guard_macro) {
423 if ($type eq 'ifndef') {
424 $header_guard_macro = [$type, $guard];
425 my $expected_guard = uc $fnm;
426 $expected_guard =~ s![-.]!_!g;
428 if (length($expected_guard) > length($guard) &&
429 substr($expected_guard, -length($guard) - 1, 1) eq '/' &&
430 substr($expected_guard, -length($guard)) eq $guard) {
433 for my $i (1 .. length($guard)) {
434 my $ch_e = substr($expected_guard, -$i, 1);
435 my $ch_g = substr($guard, -$i, 1);
436 next if ($ch_e eq $ch_g);
437 last if ($ch_e ne '/' || $ch_g ne '_');
442 diagnostic('error', "include guard macro should match filename
", $fullline);
444 my $prefix = 'XAPIAN_INCLUDED_';
445 if ($fnm =~ m!.*omega/(?:.*/)?!) {
446 $prefix = 'OMEGA_INCLUDED_';
448 #} elsif ($fnm =~ s!.*xapian-core/.*/!!) {
449 # $expected_guard = "XAPIAN_INCLUDED_
" . $expected_guard;
450 #} elsif ($fnm =~ s!.*xapian-letor/.*/!!) {
451 #$expected_guard = "XAPIAN_INCLUDED_
" . $expected_guard;
452 if (defined $cut && $cut == -1) {
453 diagnostic('error', "include guard macro should
use prefix
'$prefix'", $fullline);
454 } elsif (defined $cut && substr($guard, 0, length($guard) - $cut + 1) ne $prefix) {
455 diagnostic('error', "include guard macro should
use prefix
'$prefix'", $fullline);
456 } elsif ($guard !~ /^\Q$prefix\E/) {
457 diagnostic('error', "include guard macro should
use prefix
'$prefix'", $fullline);
461 if (!($type eq 'define' && $header_guard_macro->[0] ne 'ifndef')) {
462 my $expected_guard = $header_guard_macro->[1];
463 $header_guard_macro->[0] = $type;
464 if ($guard ne $expected_guard) {
465 diagnostic('error', "include guard macro should be
$expected_guard", $fullline);
471 if (m!^\+\s*#\s*define\s+[A-Z]\+_INCLUDED_!) {
472 diagnostic('error', "include guard macro
defined in non
-header
", $fullline);
475 } elsif (defined $lang && $lang eq 'py') {
477 diagnostic('error', "';' at end of line of python code
", $fullline);
480 if ($fnm !~ m!xapian-check-patch|ChangeLog|NEWS|stemming/.*/(?:voc|output)\.txt$!) {
481 if (/^\+.*?\b(xapain|the the|initialsing|ipv5|outputing)\b/i ||
482 # Cases which just need to be the prefix of a word
483 /^\+.*?\b((?:deafult|parm|peform|acessor|comptib|seach|seperat|seprat|separater|iteratat|calulat|delimitor|charactor)[a-z]*\b)/i ||
484 # Case-sensitive cases
485 /^\+.*?\b(and and)\b/) {
486 diagnostic('error', "Typo
'$1'", $fullline);
490 my $first_char = substr($fullline, 0, 1);
491 if ($first_char eq ' ') {
493 } elsif ($first_char eq '+') {
496 } elsif ($first_char eq '-') {
498 } elsif ($first_char eq '\\') {
499 # "\ No newline at end of file
" - if preceded by a "+" line, this means
500 # that the patch leaves the file missing a newline at the end.
501 if ($last_first_char eq '+') {
502 diagnostic('error', 'No newline at end of file');
505 $last_first_char = $first_char;
507 if (scalar keys %count) {
508 for (sort keys %count) {
509 print STDERR "$_ count
:\t$count{$_}\n";
513 print STDERR <<"__END__
";
514 Files patched:\t$files
515 Lines added:\t$add_lines
516 Lines removed:\t$del_lines
518 exit(exists $count{'error'} ? 1 : 0);