unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michal Nazarewicz <mina86@mina86.com>
To: 24100@debbugs.gnu.org
Subject: bug#24100: [PATCH 2/4] Get rid of re_set_syntax
Date: Thu, 28 Jul 2016 20:07:15 +0200	[thread overview]
Message-ID: <1469729237-14208-2-git-send-email-mina86@mina86.com> (raw)
In-Reply-To: <1469729237-14208-1-git-send-email-mina86@mina86.com>

Instead of using a global variable for storing regex syntax, pass it
to re_compile_pattern.  This is only enabled when compiling Emacs (i.e.
‘#ifdef emacs’).

* src/regex.h (re_set_syntax): Declare only #ifndef emacs.
(re_compile_pattern): Now takes syntax argument #ifdef emacs.

* src/regex.c (re_syntax_options): Define only #ifndef emacs.
(re_compile_pattern): Use the new syntax argument #ifdef emacs.

* src/search.c (compile_pattern_1): Don’t use re_set_syntax and
instead pass syntax to re_compile_pattern directly.
---
 src/regex.c  | 12 +++++++++++-
 src/regex.h  | 14 ++++++++++++++
 src/search.c | 10 ++++------
 3 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/regex.c b/src/regex.c
index 261d299..4edc064 100644
--- a/src/regex.c
+++ b/src/regex.c
@@ -1149,6 +1149,8 @@ print_double_string (re_char *where, re_char *string1, ssize_t size1,
 
 #endif /* not DEBUG */
 \f
+#ifndef emacs
+
 /* Set by `re_set_syntax' to the current regexp syntax to recognize.  Can
    also be assigned to arbitrarily: each pattern buffer stores its own
    syntax, so it can be changed between regex compilations.  */
@@ -1174,6 +1176,8 @@ re_set_syntax (reg_syntax_t syntax)
 }
 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
 
+#endif
+
 /* Regexp to use to replace spaces, or NULL meaning don't.  */
 static const_re_char *whitespace_regexp;
 
@@ -6271,8 +6275,14 @@ 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,
+#endif
 		    struct re_pattern_buffer *bufp)
 {
+#ifndef emacs
+  const reg_syntax_t syntax = re_syntax_options;
+#endif
   reg_errcode_t ret;
 
   /* GNU code is written to assume at least RE_NREGS registers will be set
@@ -6284,7 +6294,7 @@ re_compile_pattern (const char *pattern, size_t length,
      setting no_sub.  */
   bufp->no_sub = 0;
 
-  ret = regex_compile ((re_char*) pattern, length, re_syntax_options, bufp);
+  ret = regex_compile ((re_char*) pattern, length, syntax, bufp);
 
   if (!ret)
     return NULL;
diff --git a/src/regex.h b/src/regex.h
index 01b659a..4497333 100644
--- a/src/regex.h
+++ b/src/regex.h
@@ -20,6 +20,13 @@
 #ifndef _REGEX_H
 #define _REGEX_H 1
 
+#if defined emacs && (defined _REGEX_RE_COMP || defined _LIBC)
+/* We’re not defining re_set_syntax and using a different prototype of
+   re_compile_pattern when building Emacs so fail compilation early with
+   a (somewhat helpful) error message when conflict is detected. */
+# error "_REGEX_RE_COMP nor _LIBC can be defined if emacs is defined."
+#endif
+
 /* Allow the use in C++ code.  */
 #ifdef __cplusplus
 extern "C" {
@@ -453,14 +460,21 @@ typedef struct
 \f
 /* Declarations for routines.  */
 
+#ifndef emacs
+
 /* Sets the current default syntax to SYNTAX, and return the old syntax.
    You can also simply assign to the `re_syntax_options' variable.  */
 extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
 
+#endif
+
 /* Compile the regular expression PATTERN, with length LENGTH
    and syntax given by the global `re_syntax_options', into the buffer
    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,
+#endif
 				       struct re_pattern_buffer *__buffer);
 
 
diff --git a/src/search.c b/src/search.c
index 7cb18a2..f041952 100644
--- a/src/search.c
+++ b/src/search.c
@@ -113,8 +113,8 @@ static void
 compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
 		   Lisp_Object translate, bool posix)
 {
+  reg_syntax_t syntax;
   char *val;
-  reg_syntax_t old;
 
   cp->regexp = Qnil;
   cp->buf.translate = (! NILP (translate) ? translate : make_number (0));
@@ -131,16 +131,15 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
      Using BLOCK_INPUT here means the debugger won't run if an error occurs.
      So let's turn it off.  */
   /*  BLOCK_INPUT;  */
-  old = re_set_syntax (RE_SYNTAX_EMACS
-		       | (posix ? 0 : RE_NO_POSIX_BACKTRACKING));
 
   if (STRINGP (Vsearch_spaces_regexp))
     re_set_whitespace_regexp (SSDATA (Vsearch_spaces_regexp));
   else
     re_set_whitespace_regexp (NULL);
 
-  val = (char *) re_compile_pattern (SSDATA (pattern),
-				     SBYTES (pattern), &cp->buf);
+  syntax = RE_SYNTAX_EMACS | (posix ? 0 : RE_NO_POSIX_BACKTRACKING);
+  val = (char *) re_compile_pattern (SSDATA (pattern), SBYTES (pattern),
+				     syntax, &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.  */
@@ -148,7 +147,6 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
 
   re_set_whitespace_regexp (NULL);
 
-  re_set_syntax (old);
   /* unblock_input ();  */
   if (val)
     xsignal1 (Qinvalid_regexp, build_string (val));
-- 
2.8.0.rc3.226.g39d4020






  reply	other threads:[~2016-07-28 18:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-28 17:00 bug#24100: [PATCH 0/4] Some regex dead-code elimination Michal Nazarewicz
2016-07-28 18:07 ` bug#24100: [PATCH 1/4] Remove dead opcodes in regex bytecode Michal Nazarewicz
2016-07-28 18:07   ` Michal Nazarewicz [this message]
2016-07-28 18:07   ` bug#24100: [PATCH 3/4] Get rid of re_set_whitespace_regexp Michal Nazarewicz
2016-07-28 18:07   ` bug#24100: [PATCH 4/4] Hardcode regex syntax to remove dead code handling different syntax Michal Nazarewicz
2016-08-02 16:06 ` bug#24100: [PATCH 0/4] Some regex dead-code elimination Michal Nazarewicz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1469729237-14208-2-git-send-email-mina86@mina86.com \
    --to=mina86@mina86.com \
    --cc=24100@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).