Subject: bug#24100: [PATCH 4/4] Hardcode regex syntax to remove dead code handling different syntax

From: Michal Nazarewicz <[email-masked]>
Date: Thu, 28 Jul 2016 20:07:17 +0200

Emacs only ever uses its own regex syntax so support for other syntaxes
is never used.  Hardcode the syntax so that the compilar can detect such
dead code and remove it from compiled code.

The only exception is RE_NO_POSIX_BACKTRACKING which can be separatelly
specified.  Handle this separatelly with a function argument (replacing
now unnecessary syntax argument).

With this patchset, size of Emacs binary on x86_64 machine is reduced by
around 60 kB:

	new-sizes:-rwx------ 3 mpn eng 30254720 Jul 27 23:31 src/emacs
	old-sizes:-rwx------ 3 mpn eng 30314828 Jul 27 23:29 src/emacs

* src/regex.h (re_pattern_buffer): Don’t define syntax field #ifdef emacs.
(re_compile_pattern): Replace syntax with posix_backtracking argument.

* src/regex.c (print_compiled_pattern): Don’t print syntax #ifdef emacs.
(regex_compile): #ifdef emacs, replace syntax argument with
posix_backtracking which is now used instead of testing for
RE_NO_POSIX_BACKTRACKING syntax.
(re_match_2_internal): Don’t access bufp->syntax #ifndef emacs.
(re_compile_pattern): Replace syntax with posix_backtracking argument.

* src/search.c (compile_pattern_1): Pass boolean posix_backtracking
instead of syntax to re_compile_pattern.
---
 src/regex.c  | 40 +++++++++++++++++++++++++++++++---------
 src/regex.h  |  5 +++--
 src/search.c |  4 +---
 3 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/src/regex.c b/src/regex.c
index c32a62f..8dafb11 100644
--- a/src/regex.c
+++ b/src/regex.c
@@ -1108,7 +1108,9 @@ print_compiled_pattern (struct re_pattern_buffer *bufp)
   printf ("no_sub: %d\t", bufp->no_sub);
   printf ("not_bol: %d\t", bufp->not_bol);
   printf ("not_eol: %d\t", bufp->not_eol);
+#ifndef emacs
   printf ("syntax: %lx\n", bufp->syntax);
+#endif
   fflush (stdout);
   /* Perhaps we should print the translate table?  */
 }
@@ -1558,9 +1560,11 @@ do {									\
 /* Subroutine declarations and macros for regex_compile.  */
 
 static reg_errcode_t regex_compile (re_char *pattern, size_t size,
-				    reg_syntax_t syntax,
 #ifdef emacs
+				    bool posix_backtracking,
 				    const char *whitespace_regexp,
+#else
+				    reg_syntax_t syntax,
 #endif
 				    struct re_pattern_buffer *bufp);
 static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
@@ -2426,9 +2430,14 @@ do {									\
   } while (0)
 
 static reg_errcode_t
-regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
+regex_compile (const_re_char *pattern, size_t size,
 #ifdef emacs
+# define syntax RE_SYNTAX_EMACS
+	       bool posix_backtracking,
 	       const char *whitespace_regexp,
+#else
+	       reg_syntax_t syntax,
+# define posix_backtracking (!(syntax & RE_NO_POSIX_BACKTRACKING))
 #endif
 	       struct re_pattern_buffer *bufp)
 {
@@ -2518,7 +2527,9 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
   range_table_work.allocated = 0;
 
   /* Initialize the pattern buffer.  */
+#ifndef emacs
   bufp->syntax = syntax;
+#endif
   bufp->fastmap_accurate = 0;
   bufp->not_bol = bufp->not_eol = 0;
   bufp->used_syntax = 0;
@@ -3645,7 +3656,7 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
 
   /* If we don't want backtracking, force success
      the first time we reach the end of the compiled pattern.  */
-  if (syntax & RE_NO_POSIX_BACKTRACKING)
+  if (!posix_backtracking)
     BUF_PUSH (succeed);
 
   /* We have succeeded; set the length of the buffer.  */
@@ -3680,6 +3691,12 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
 #endif /* not MATCH_MAY_ALLOCATE */
 
   FREE_STACK_RETURN (REG_NOERROR);
+
+#ifdef emacs
+# undef syntax
+#else
+# undef posix_backtracking
+#endif
 } /* regex_compile */
 
 /* Subroutines for `regex_compile'.  */
@@ -5442,6 +5459,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
 	  {
 	    int buf_charlen;
 	    re_wchar_t buf_ch;
+	    reg_syntax_t syntax;
 
 	    DEBUG_PRINT ("EXECUTING anychar.\n");
 
@@ -5450,10 +5468,14 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
 						target_multibyte);
 	    buf_ch = TRANSLATE (buf_ch);
 
-	    if ((!(bufp->syntax & RE_DOT_NEWLINE)
-		 && buf_ch == '\n')
-		|| ((bufp->syntax & RE_DOT_NOT_NULL)
-		    && buf_ch == '\000'))
+#ifdef emacs
+	    syntax = RE_SYNTAX_EMACS;
+#else
+	    syntax = bufp->syntax;
+#endif
+
+	    if ((!(syntax & RE_DOT_NEWLINE) && buf_ch == '\n')
+		|| ((syntax & RE_DOT_NOT_NULL) && buf_ch == '\000'))
 	      goto fail;
 
 	    DEBUG_PRINT ("  Matched \"%d\".\n", *d);
@@ -6281,7 +6303,7 @@ bcmp_translate (const_re_char *s1, const_re_char *s2, register ssize_t len,
 const char *
 re_compile_pattern (const char *pattern, size_t length,
 #ifdef emacs
-		    reg_syntax_t syntax, const char *whitespace_regexp,
+		    bool posix_backtracking, const char *whitespace_regexp,
 #endif
 		    struct re_pattern_buffer *bufp)
 {
@@ -6298,7 +6320,7 @@ re_compile_pattern (const char *pattern, size_t length,
 
   ret = regex_compile ((re_char*) pattern, length,
 #ifdef emacs
-		       syntax,
+		       posix_backtracking,
 		       whitespace_regexp,
 #else
 		       re_syntax_options,
diff --git a/src/regex.h b/src/regex.h
index af9480d..b672d3f 100644
--- a/src/regex.h
+++ b/src/regex.h
@@ -354,9 +354,10 @@ struct re_pattern_buffer
 	/* Number of bytes actually used in `buffer'.  */
   size_t used;
 
+#ifndef emacs
         /* Syntax setting with which the pattern was compiled.  */
   reg_syntax_t syntax;
-
+#endif
         /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
            the fastmap, if there is one, to skip over impossible
            starting points for matches.  */
@@ -473,7 +474,7 @@ extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
    BUFFER.  Return NULL if successful, and an error string if not.  */
 extern const char *re_compile_pattern (const char *__pattern, size_t __length,
 #ifdef emacs
-				       reg_syntax_t syntax,
+				       bool posix_backtracking,
 				       const char *whitespace_regexp,
 #endif
 				       struct re_pattern_buffer *__buffer);
diff --git a/src/search.c b/src/search.c
index c7556a9..7f2b4f9 100644
--- a/src/search.c
+++ b/src/search.c
@@ -114,7 +114,6 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
 		   Lisp_Object translate, bool posix)
 {
   const char *whitespace_regexp;
-  reg_syntax_t syntax;
   char *val;
 
   cp->regexp = Qnil;
@@ -133,12 +132,11 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
      So let's turn it off.  */
   /*  BLOCK_INPUT;  */
 
-  syntax = RE_SYNTAX_EMACS | (posix ? 0 : RE_NO_POSIX_BACKTRACKING);
   whitespace_regexp = STRINGP (Vsearch_spaces_regexp) ?
     SSDATA (Vsearch_spaces_regexp) : NULL;
 
   val = (char *) re_compile_pattern (SSDATA (pattern), SBYTES (pattern),
-				     syntax, whitespace_regexp, &cp->buf);
+				     posix, whitespace_regexp, &cp->buf);
 
   /* If the compiled pattern hard codes some of the contents of the
      syntax-table, it can only be reused with *this* syntax table.  */
-- 
2.8.0.rc3.226.g39d4020