|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# Detect bare 'grep' used as a test assertion where 'test_grep' |
| 4 | +# should be used, and '! test_grep' where 'test_grep !' should |
| 5 | +# be used. Uses the shared shell parser to correctly identify |
| 6 | +# command boundaries, avoiding false positives from 'grep' in |
| 7 | +# arguments, strings, heredocs, or command substitutions. |
| 8 | + |
| 9 | +use strict; |
| 10 | +use warnings; |
| 11 | +use File::Basename; |
| 12 | + |
| 13 | +my $_lib = dirname($0) . "/lib-shell-parser.pl"; |
| 14 | +$_lib = "./$_lib" unless $_lib =~ m{^/}; |
| 15 | +do $_lib or die "$0: failed to load $_lib: $@$!\n"; |
| 16 | + |
| 17 | +my $exit_code = 0; |
| 18 | + |
| 19 | +package CheckGrepParser; |
| 20 | +our @ISA = ('ScriptParser'); |
| 21 | + |
| 22 | +# Tokens that mark the start of a new command. |
| 23 | +my %cmd_start; |
| 24 | +@cmd_start{qw(&& || ;; do then else elif), "\n", '{', '('} = (); |
| 25 | + |
| 26 | +# Subset of cmd_start that also ends a pipeline. |
| 27 | +my %pipe_end; |
| 28 | +@pipe_end{qw(&& || ;;)} = (); |
| 29 | + |
| 30 | +# Tokens indicating grep is used as a filter (pipe or redirect). |
| 31 | +my %filter_op; |
| 32 | +@filter_op{'|', '>', '>>', '<'} = (); |
| 33 | + |
| 34 | +# Check if the raw source line has a lint-ok annotation. |
| 35 | +# Also check adjacent lines to handle backslash-continuation |
| 36 | +# line number offsets from the parser. |
| 37 | +sub lint_ok { |
| 38 | + my ($raw_lines, $ln) = @_; |
| 39 | + for my $l ($ln - 1, $ln, $ln + 1) { |
| 40 | + next if $l < 1 || $l > @$raw_lines; |
| 41 | + return 1 if $raw_lines->[$l - 1] =~ /lint-ok/; |
| 42 | + } |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +# Check if the grep at position $pos pipes its output |
| 47 | +# or redirects I/O (i.e. used as a filter, not assertion). |
| 48 | +sub is_filter { |
| 49 | + my ($tokens, $pos) = @_; |
| 50 | + for (my $j = $pos + 1; $j < @$tokens; $j++) { |
| 51 | + my $t = $tokens->[$j]->[0]; |
| 52 | + return 0 if exists $cmd_start{$t}; |
| 53 | + return 1 if exists $filter_op{$t}; |
| 54 | + } |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +sub check_test { |
| 59 | + my $self = shift @_; |
| 60 | + my $title = ScriptParser::unwrap(shift @_); |
| 61 | + my $body_token = shift @_; |
| 62 | + my $lineno = $body_token->[3]; |
| 63 | + my $body = ScriptParser::unwrap($body_token); |
| 64 | + if ($body eq '-') { |
| 65 | + my $herebody = shift @_; |
| 66 | + if ($herebody) { |
| 67 | + $body = $herebody->{content}; |
| 68 | + $lineno = $herebody->{start_line}; |
| 69 | + } |
| 70 | + } |
| 71 | + return unless $body; |
| 72 | + |
| 73 | + # For double-quoted bodies, build a table mapping inner-parser |
| 74 | + # line numbers to cumulative line-splice adjustments. Line |
| 75 | + # splices (\<newline>) consume a source line without emitting |
| 76 | + # a newline into the body text, so the inner parser's line |
| 77 | + # count falls behind the source. |
| 78 | + my $raw_lines = $self->{raw_lines}; |
| 79 | + my @splice_adj; |
| 80 | + if ($body_token->[0] =~ /^"/) { |
| 81 | + my $adj = 0; |
| 82 | + my $inner = 0; |
| 83 | + for my $src_ln ($lineno .. ($body_token->[4] || $lineno)) { |
| 84 | + my $line = $raw_lines->[$src_ln - 1]; |
| 85 | + if (defined($line) && $line =~ /(\\*)\s*$/ && |
| 86 | + length($1) % 2 == 1) { |
| 87 | + $adj++; |
| 88 | + } else { |
| 89 | + $splice_adj[++$inner] = $adj; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + my $parser = ShellParser->new(\$body); |
| 95 | + my @tokens = $parser->parse(); |
| 96 | + |
| 97 | + my $file = $self->{file}; |
| 98 | + my $cmd_pos = 1; |
| 99 | + my $in_pipe = 0; |
| 100 | + |
| 101 | + for (my $i = 0; $i < @tokens; $i++) { |
| 102 | + my $text = $tokens[$i]->[0]; |
| 103 | + my $rel = $tokens[$i]->[3] || 0; |
| 104 | + my $ln = $rel + ($lineno || 1) - 1; |
| 105 | + $ln += $splice_adj[$rel] if @splice_adj && $rel <= $#splice_adj; |
| 106 | + |
| 107 | + if (exists $cmd_start{$text}) { |
| 108 | + $cmd_pos = 1; |
| 109 | + $in_pipe = 0 if exists $pipe_end{$text}; |
| 110 | + next; |
| 111 | + } |
| 112 | + if ($text eq '|') { |
| 113 | + $cmd_pos = 1; |
| 114 | + $in_pipe = 1; |
| 115 | + next; |
| 116 | + } |
| 117 | + if ($text eq '}' || $text eq ')') { |
| 118 | + $cmd_pos = 0; |
| 119 | + next; |
| 120 | + } |
| 121 | + |
| 122 | + next unless $cmd_pos; |
| 123 | + |
| 124 | + # '!' is a negation prefix, stay in command position |
| 125 | + if ($text eq '!') { |
| 126 | + if ($i + 1 < @tokens && |
| 127 | + $tokens[$i + 1]->[0] eq 'test_grep' && |
| 128 | + !lint_ok($raw_lines, $ln)) { |
| 129 | + print "$file:$ln: error: ", |
| 130 | + 'use "test_grep !" instead of ', |
| 131 | + '"! test_grep"', "\n"; |
| 132 | + $exit_code = 1; |
| 133 | + } |
| 134 | + next; |
| 135 | + } |
| 136 | + |
| 137 | + # bare grep at command position, not in a pipeline |
| 138 | + if ($text eq 'grep' && !$in_pipe && |
| 139 | + !is_filter(\@tokens, $i) && |
| 140 | + !lint_ok($raw_lines, $ln)) { |
| 141 | + print "$file:$ln: error: ", |
| 142 | + "bare grep outside pipeline ", |
| 143 | + "(use test_grep)\n"; |
| 144 | + $exit_code = 1; |
| 145 | + } |
| 146 | + |
| 147 | + $cmd_pos = 0; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +package main; |
| 152 | + |
| 153 | +for my $file (@ARGV) { |
| 154 | + open(my $fh, '<:unix:crlf', $file) or die "$0: $file: $!\n"; |
| 155 | + my @raw_lines = <$fh>; |
| 156 | + close $fh; |
| 157 | + my $s = join('', @raw_lines); |
| 158 | + my $parser = CheckGrepParser->new(\$s); |
| 159 | + $parser->{file} = $file; |
| 160 | + $parser->{raw_lines} = \@raw_lines; |
| 161 | + $parser->parse(); |
| 162 | +} |
| 163 | +exit $exit_code; |
0 commit comments