From 8a5bc4007100cbd296aa642cd42f803a9c23bf71 Mon Sep 17 00:00:00 2001 From: Eric Blake 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 --- 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 33278aef..c8bc3575 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2007-10-22 Eric Blake + 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 5983ac2a..0b7f5c13 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 = ®ex_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, ®s); 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, ®s); if (msg != NULL) { diff --git a/src/m4.c b/src/m4.c index f22788ed..401ce4b7 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 a0b54f62..ce7bdc07 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 -- 2.11.4.GIT