diff options
author | Jim Meyering <meyering@fb.com> | 2016-07-16 10:51:31 -0700 |
---|---|---|
committer | Jim Meyering <meyering@fb.com> | 2016-07-25 17:52:06 -0700 |
commit | f13c169e13aed279baa00d9ced9d93fe24775493 (patch) | |
tree | ba882a57281665a5d836d6abaa6a3e1d41569f37 /tests/filename-lineno.pl | |
parent | 4443fda05ace21b2a2da2d80b09313cc976afe59 (diff) | |
download | grep-f13c169e13aed279baa00d9ced9d93fe24775493.tar.gz |
grep: print "filename:lineno:" in invalid-regex diagnostic
Determining the file name and line number is a little tricky because
of the way the regular expressions are all concatenated onto a newline-
separated list. By the time grep would compile regular expressions,
the <filename,lineno> origin of each regexp was no longer available.
This patch adds a list of filename,first_lineno pairs, one per input
source, by which we can then map the ordinal regexp number to a
filename,lineno pair for the diagnostic.
* src/dfasearch.c (GEAcompile): When diagnosing an invalid regexp
specified via -f FILE, include the "FILENAME:LINENO: " prefix.
Also, when there are two or more lines with compilation failures,
diagnose all of them, rather than stopping after the first.
* src/grep.h (pattern_file_name): Declare it.
* src/grep.c: (struct FL_pair): Define type.
(fl_pair, n_fl_pair_slots, n_pattern_files, patfile_lineno):
Define globals.
(fl_add, pattern_file_name): Define functions.
(main): Call fl_add for each type of the following: -e argument,
-f argument, command-line-specified (without -e) regexp.
* tests/filename-lineno.pl: New file.
* tests/Makefile.am (TESTS): Add it.
* NEWS (Improvements): Mention this.
Initially reported by Gunnar Wolf in https://bugs.debian.org/525214
Forwarded to grep's bug list by Santiago Ruano Rincón as
http://debbugs.gnu.org/23965
Diffstat (limited to 'tests/filename-lineno.pl')
-rwxr-xr-x | tests/filename-lineno.pl | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/filename-lineno.pl b/tests/filename-lineno.pl new file mode 100755 index 00000000..3827f4da --- /dev/null +++ b/tests/filename-lineno.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl +# Prior to 2.26, an invalid regexp in a -f-specified file would elicit +# a diagnostic like "Unmatched [ or [^", with no indication of the +# file or line number from which the offending regular expression came. +# With 2.26, now, each such diagnostic has a "FILENAME:LINENO: " prefix. + +# Copyright (C) 2016 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +use strict; + +(my $program_name = $0) =~ s|.*/||; + +my $prog = 'grep'; + +# Turn off localization of executable's output. +@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; + +# There are at least two variants of one diagnostic: +# - Unmatched [, [^, [:, [., or [= +# - Unmatched [ or [^ +# Transform each to this: "Unmatched [..." +my $err_subst = {ERR_SUBST => 's/(: Unmatched \[).*/$1.../'}; + +my @Tests = + ( + # Show that grep now includes filename:lineno in the diagnostic: + ['invalid-re', '-f g', {AUX=>{g=>"1\n2\n3\n4[[\n"}}, {EXIT=>2}, + $err_subst, + {ERR => "$prog: g:4: Unmatched [...\n"}, + ], + + # Show that with two or more errors, grep now prints all diagnostics: + ['invalid-re-2-files', '-f g -f h', {EXIT=>2}, + {AUX=>{g=>"1\n2[[\n3\n4[[\n"}}, + {AUX=>{h=>"\n\n[[\n"}}, + $err_subst, + {ERR => "$prog: g:2: Unmatched [...\n" + . "$prog: g:4: Unmatched [...\n" + . "$prog: h:3: Unmatched [...\n" + }, + ], + + # Like the above, but on the other lines. + ['invalid-re-2-files2', '-f g -f h', {EXIT=>2}, + {AUX=>{g=>"1[[\n2\n3[[\n4\n"}}, + {AUX=>{h=>"[[\n[[\n\n"}}, + $err_subst, + {ERR => "$prog: g:1: Unmatched [...\n" + . "$prog: g:3: Unmatched [...\n" + . "$prog: h:1: Unmatched [...\n" + . "$prog: h:2: Unmatched [...\n" + }, + ], + + # Show that with two '-e'-specified erroneous regexps, + # there is no file name or line number. + ['invalid-re-2e', '-e "[[" -e "[["', {EXIT=>2}, + $err_subst, + {ERR => "$prog: Unmatched [...\n" x 2}, + ], + ); + +my $save_temps = $ENV{DEBUG}; +my $verbose = $ENV{VERBOSE}; + +my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); +exit $fail; |