bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/3] dfa: warn on [:space:] and similar


From: Paolo Bonzini
Subject: [PATCH 2/3] dfa: warn on [:space:] and similar
Date: Sun, 15 Aug 2010 10:54:19 -0400

* src/dfa.c (parse_bracket_exp): Warn on regular expressions such as
[:space:].
* src/dfa.h (dfawarn): New prototype.
* src/dfasearch.c (dfawarn): New.
* NEWS: Document.
---
 NEWS            |    9 +++++++++
 src/dfa.c       |   16 ++++++++++++++++
 src/dfa.h       |    6 ++++++
 src/dfasearch.c |    7 +++++++
 4 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/NEWS b/NEWS
index e64ec40..536923c 100644
--- a/NEWS
+++ b/NEWS
@@ -24,6 +24,15 @@ GNU grep NEWS                                    -*- outline 
-*-
   --mmap was meant to be ignored in 2.6.x, but it was instead
   removed by mistake.  [bug introduced in 2.6]
 
+** New features
+
+  grep now will warn for very common regular expression mistakes,
+  such as using [:space:] instead of [[:space:]].  Warnings are
+  disabled by POSIXLY_CORRECT.  They are also disabled when stdout
+  is not a TTY, thus minimizing the chance of extraneous output
+  in a script.  However, the rules for enabling/disabling warnings
+  are experimental and subject to change in future releases.
+
 * Noteworthy changes in release 2.6.3 (2010-04-02) [stable]
 
 ** Bug fixes
diff --git a/src/dfa.c b/src/dfa.c
index f53e797..fbcef66 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -752,6 +752,13 @@ parse_bracket_exp (void)
   int c, c1, c2;
   charclass ccl;
 
+  /* Used to warn about [:space:].
+     Bit 0 = first character is a colon.
+     Bit 1 = last character is a colon.
+     Bit 2 = includes any other character but a colon.
+     Bit 3 = includes ranges, char/equiv classes or collation elements.  */
+  int colon_warning_state;
+
 #if MBS_SUPPORT
   wint_t wc, wc1, wc2;
 
@@ -790,9 +797,11 @@ parse_bracket_exp (void)
   else
     invert = 0;
 
+  colon_warning_state = (c == ':');
   do
     {
       c1 = EOF; /* mark c1 is not initialized".  */
+      colon_warning_state &= ~2;
 
       /* Note that if we're looking at some other [:...:] construct,
          we just treat it as a bunch of ordinary characters.  We can do
@@ -890,6 +899,7 @@ parse_bracket_exp (void)
                     }
                 }
 #endif
+              colon_warning_state |= 8;
 
               /* Fetch new lookahead character.  */
               FETCH_WC (c1, wc1, _("unbalanced ["));
@@ -975,10 +985,13 @@ parse_bracket_exp (void)
                     setbit_case_fold (c, ccl);
             }
 
+          colon_warning_state |= 8;
           FETCH_WC(c1, wc1, _("unbalanced ["));
           continue;
         }
 
+      colon_warning_state |= (c == ':') ? 2 : 4;
+
 #if MBS_SUPPORT
       /* Build normal characters.  */
       setbit_case_fold (wc, ccl);
@@ -1018,6 +1031,9 @@ parse_bracket_exp (void)
 #endif
          (c = c1) != ']'));
 
+  if (colon_warning_state == 7)
+    dfawarn ("character classes syntax is [[:space:]], not [:space:]");
+
 #if MBS_SUPPORT
   if (MB_CUR_MAX > 1
       && (!using_utf8()
diff --git a/src/dfa.h b/src/dfa.h
index 0a8ad42..b8f3078 100644
--- a/src/dfa.h
+++ b/src/dfa.h
@@ -90,6 +90,12 @@ extern void dfastate (int, struct dfa *, int []);
 
 /* Error handling. */
 
+/* dfawarn() is called by the regexp routines whenever a regex is compiled
+   that likely doesn't do what the user wanted.  It takes a single
+   argument, a NUL-terminated string describing the situation.  The user
+   must supply a dfawarn.  */
+extern void dfawarn (const char *);
+
 /* dfaerror() is called by the regexp routines whenever an error occurs.  It
    takes a single argument, a NUL-terminated string describing the error.
    The user must supply a dfaerror.  */
diff --git a/src/dfasearch.c b/src/dfasearch.c
index 4ccb6dc..bc2bb2f 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -46,6 +46,13 @@ static struct patterns *patterns;
 static size_t pcount;
 
 void
+dfawarn (char const *mesg)
+{
+  if (!no_warnings)
+    error (0, 0, _("warning: %s"), mesg);
+}
+
+void
 dfaerror (char const *mesg)
 {
   error (EXIT_TROUBLE, 0, "%s", mesg);
-- 
1.7.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]