m4-discuss
[Top][All Lists]
Advanced

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

regex tracing [was: Multi-Line Definitions]


From: Eric Blake-1
Subject: regex tracing [was: Multi-Line Definitions]
Date: Mon, 22 Oct 2007 10:33:16 -0700 (PDT)

> Here's something a bit more telling.  With the attached patch, and in the
> coreutils directory,
> 
> $ M4_TRACE_FILE=~/m4.trace M4=~/m4/src/m4 autoconf
> $ wc m4.trace
>  62207  61314 666720 m4.trace
> $ sort -u m4.trace | wc
>     401     405    5619

The concept of regex tracing is useful enough that I'm adding
it to the code under the preprocessor macro DEBUG_REGEX,
as follows.  I suppose I could also track changeword and
--warn-macro-sequence regex, but did not do so here.

From: Eric Blake <address@hidden>
Date: Mon, 22 Oct 2007 11:13:32 -0600
Subject: [PATCH] Add DEBUG_REGEX debugging information.

* src/m4.h (DEBUG_REGEX): New debug macro.
* src/m4.c (main) [DEBUG_REGEX]: Open regex trace file when
requested.
* src/builtin.c (m4_patsubst, m4_regexp, compile_pattern)
[DEBUG_REGEX]: Trace regex usage.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog     |    7 +++++++
 src/builtin.c |   35 +++++++++++++++++++++++++++++++----
 src/m4.c      |   18 ++++++++++++++++++
 src/m4.h      |    1 +
 4 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 33278ae..c8bc357 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2007-10-22  Eric Blake  <address@hidden>
 
+       Add DEBUG_REGEX debugging information.
+       * src/m4.h (DEBUG_REGEX): New debug macro.
+       * src/m4.c (main) [DEBUG_REGEX]: Open regex trace file when
+       requested.
+       * src/builtin.c (m4_patsubst, m4_regexp, compile_pattern)
+       [DEBUG_REGEX]: Trace regex usage.
+
        Never let printf failures go undetected.
        * m4/gnulib-cache.m4: Augment with 'gnulib-tool --import
        xprintf'.
diff --git a/src/builtin.c b/src/builtin.c
index 5983ac2..0b7f5c1 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -252,6 +252,10 @@ typedef struct m4_regex m4_regex;
 /* Storage for the cache of regular expressions.  */
 static m4_regex regex_cache[REGEX_CACHE_SIZE];
 
+#ifdef DEBUG_REGEX
+extern FILE *trace_file;
+#endif /* DEBUG_REGEX */
+
 /*------------------------------------------------------------------.
 | Compile STR, with length LEN, into a regex.  On success, set BUF  |
 | and REGS to the compiled regex.  Compilation is cached, so do not |
@@ -278,12 +282,20 @@ compile_pattern (const char *str, size_t len, struct
re_pattern_buffer **buf,
        *buf = regex_cache[i].buf;
        *regs = &regex_cache[i].regs;
        regex_cache[i].count++;
+#ifdef DEBUG_REGEX
+       if (trace_file)
+         xfprintf (trace_file, "cached:{%s}\n", str);
+#endif /* DEBUG_REGEX */
        return NULL;
       }
 
   /* Next, check if STR can be compiled.  */
   new_buf = xzalloc (sizeof *new_buf);
   msg = re_compile_pattern (str, len, new_buf);
+#ifdef DEBUG_REGEX
+  if (trace_file)
+    xfprintf (trace_file, "compile:{%s}\n", str);
+#endif /* DEBUG_REGEX */
   if (msg)
     {
       regfree (new_buf);
@@ -313,6 +325,10 @@ compile_pattern (const char *str, size_t len, struct
re_pattern_buffer **buf,
   victim->len = len;
   if (victim->str)
     {
+#ifdef DEBUG_REGEX
+      if (trace_file)
+       xfprintf (trace_file, "flush:{%s}\n", victim->str);
+#endif /* DEBUG_REGEX */
       free (victim->str);
       regfree (victim->buf);
       free (victim->buf);
@@ -1958,10 +1974,10 @@ m4_translit (struct obstack *obs, int argc,
token_data **argv)
     }
 }
 
-/*----------------------------------------------------------------------.
-| Frontend for printf like formatting.  The function format () lives in |
-| the file format.c.                                                   |
-`----------------------------------------------------------------------*/
+/*--------------------------------------------------------------.
+| Frontend for *printf like formatting.  The function format () |
+| lives in the file format.c.                                   |
+`--------------------------------------------------------------*/
 
 static void
 m4_format (struct obstack *obs, int argc, token_data **argv)
@@ -2097,6 +2113,12 @@ m4_regexp (struct obstack *obs, int argc, token_data
**argv)
       return;
     }
 
+#ifdef DEBUG_REGEX
+  if (trace_file)
+    xfprintf (trace_file, "r:{%s}:%s%s%s\n", regexp,
+             argc == 3 ? "" : "{", repl, argc == 3 ? "" : "}");
+#endif /* DEBUG_REGEX */
+
   msg = compile_pattern (regexp, strlen (regexp), &buf, &regs);
   if (msg != NULL)
     {
@@ -2161,6 +2183,11 @@ m4_patsubst (struct obstack *obs, int argc,
token_data **argv)
       return;
     }
 
+#ifdef DEBUG_REGEX
+  if (trace_file)
+    xfprintf (trace_file, "p:{%s}:{%s}\n", regexp, repl);
+#endif /* DEBUG_REGEX */
+
   msg = compile_pattern (regexp, strlen (regexp), &buf, &regs);
   if (msg != NULL)
     {
diff --git a/src/m4.c b/src/m4.c
index f22788e..401ce4b 100644
--- a/src/m4.c
+++ b/src/m4.c
@@ -318,6 +318,10 @@ process_file (const char *name)
 #define OPTSTRING "-B:D:EF:GH:I:L:N:PQR:S:T:U:d::eil:o:st:"
 #endif
 
+#ifdef DEBUG_REGEX
+FILE *trace_file;
+#endif /* DEBUG_REGEX */
+
 int
 main (int argc, char *const *argv, char *const *envp)
 {
@@ -338,6 +342,16 @@ main (int argc, char *const *argv, char *const *envp)
   retcode = EXIT_SUCCESS;
   atexit (close_stdin);
 
+#ifdef DEBUG_REGEX
+  {
+    const char *name = getenv ("M4_TRACE_FILE");
+    if (name)
+      trace_file = fopen (name, "a");
+    if (trace_file)
+      fputs ("m4:\n", trace_file);
+  }
+#endif /* DEBUG_REGEX */
+
   include_init ();
   debug_init ();
 #ifdef USE_STACKOVF
@@ -591,5 +605,9 @@ main (int argc, char *const *argv, char *const *envp)
     }
   output_exit ();
   free_regex ();
+#ifdef DEBUG_REGEX
+  if (trace_file)
+    fclose (trace_file);
+#endif /* DEBUG_REGEX */
   exit (retcode);
 }
diff --git a/src/m4.h b/src/m4.h
index a0b54f6..ce7bdc0 100644
--- a/src/m4.h
+++ b/src/m4.h
@@ -462,6 +462,7 @@ void reload_frozen_state (const char *);
 # define DEBUG_INPUT  1
 # define DEBUG_MACRO  1
 # define DEBUG_OUTPUT 1
+# define DEBUG_REGEX  1
 # define DEBUG_STKOVF 1
 # define DEBUG_SYM    1
 #endif
-- 
1.5.3.2


-- 
View this message in context: 
http://www.nabble.com/Re%3A-Multi-Line-Definitions-tf4540504.html#a13348510
Sent from the Gnu - M4 - Discuss mailing list archive at Nabble.com.





reply via email to

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