]> git.ipfire.org Git - thirdparty/git.git/blobdiff - grep.c
grep: add support for the PCRE v1 JIT API
[thirdparty/git.git] / grep.c
diff --git a/grep.c b/grep.c
index 47cee4506703f5dd2b7db84e09dae93adf99fbaf..ffe95995eee4351cabe8bfd956617e7dfa97d0eb 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -178,26 +178,23 @@ static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, st
 
        case GREP_PATTERN_TYPE_BRE:
                opt->fixed = 0;
-               opt->pcre = 0;
-               opt->regflags &= ~REG_EXTENDED;
+               opt->pcre1 = 0;
                break;
 
        case GREP_PATTERN_TYPE_ERE:
                opt->fixed = 0;
-               opt->pcre = 0;
+               opt->pcre1 = 0;
                opt->regflags |= REG_EXTENDED;
                break;
 
        case GREP_PATTERN_TYPE_FIXED:
                opt->fixed = 1;
-               opt->pcre = 0;
-               opt->regflags &= ~REG_EXTENDED;
+               opt->pcre1 = 0;
                break;
 
        case GREP_PATTERN_TYPE_PCRE:
                opt->fixed = 0;
-               opt->pcre = 1;
-               opt->regflags &= ~REG_EXTENDED;
+               opt->pcre1 = 1;
                break;
        }
 }
@@ -324,8 +321,32 @@ static NORETURN void compile_regexp_failed(const struct grep_pat *p,
        die("%s'%s': %s", where, p->pattern, error);
 }
 
-#ifdef USE_LIBPCRE
-static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
+static int is_fixed(const char *s, size_t len)
+{
+       size_t i;
+
+       for (i = 0; i < len; i++) {
+               if (is_regex_special(s[i]))
+                       return 0;
+       }
+
+       return 1;
+}
+
+static int has_null(const char *s, size_t len)
+{
+       /*
+        * regcomp cannot accept patterns with NULs so when using it
+        * we consider any pattern containing a NUL fixed.
+        */
+       if (memchr(s, 0, len))
+               return 1;
+
+       return 0;
+}
+
+#ifdef USE_LIBPCRE1
+static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
 {
        const char *error;
        int erroffset;
@@ -333,23 +354,36 @@ static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
 
        if (opt->ignore_case) {
                if (has_non_ascii(p->pattern))
-                       p->pcre_tables = pcre_maketables();
+                       p->pcre1_tables = pcre_maketables();
                options |= PCRE_CASELESS;
        }
        if (is_utf8_locale() && has_non_ascii(p->pattern))
                options |= PCRE_UTF8;
 
-       p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
-                                     p->pcre_tables);
-       if (!p->pcre_regexp)
+       p->pcre1_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
+                                     p->pcre1_tables);
+       if (!p->pcre1_regexp)
                compile_regexp_failed(p, error);
 
-       p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
-       if (!p->pcre_extra_info && error)
+       p->pcre1_extra_info = pcre_study(p->pcre1_regexp, PCRE_STUDY_JIT_COMPILE, &error);
+       if (!p->pcre1_extra_info && error)
                die("%s", error);
+
+#ifdef PCRE_CONFIG_JIT
+       pcre_config(PCRE_CONFIG_JIT, &p->pcre1_jit_on);
+       if (p->pcre1_jit_on == 1) {
+               p->pcre1_jit_stack = pcre_jit_stack_alloc(1, 1024 * 1024);
+               if (!p->pcre1_jit_stack)
+                       die("Couldn't allocate PCRE JIT stack");
+               pcre_assign_jit_stack(p->pcre1_extra_info, NULL, p->pcre1_jit_stack);
+       } else if (p->pcre1_jit_on != 0) {
+               die("BUG: The pcre1_jit_on variable should be 0 or 1, not %d",
+                   p->pcre1_jit_on);
+       }
+#endif
 }
 
-static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
+static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
                regmatch_t *match, int eflags)
 {
        int ovector[30], ret, flags = 0;
@@ -357,8 +391,19 @@ static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
        if (eflags & REG_NOTBOL)
                flags |= PCRE_NOTBOL;
 
-       ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
-                       0, flags, ovector, ARRAY_SIZE(ovector));
+#ifdef PCRE_CONFIG_JIT
+       if (p->pcre1_jit_on) {
+               ret = pcre_jit_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
+                                   eol - line, 0, flags, ovector,
+                                   ARRAY_SIZE(ovector), p->pcre1_jit_stack);
+       } else
+#endif
+       {
+               ret = pcre_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
+                               eol - line, 0, flags, ovector,
+                               ARRAY_SIZE(ovector));
+       }
+
        if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
                die("pcre_exec failed with error code %d", ret);
        if (ret > 0) {
@@ -370,55 +415,44 @@ static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
        return ret;
 }
 
-static void free_pcre_regexp(struct grep_pat *p)
+static void free_pcre1_regexp(struct grep_pat *p)
 {
-       pcre_free(p->pcre_regexp);
-       pcre_free(p->pcre_extra_info);
-       pcre_free((void *)p->pcre_tables);
+       pcre_free(p->pcre1_regexp);
+#ifdef PCRE_CONFIG_JIT
+       if (p->pcre1_jit_on) {
+               pcre_free_study(p->pcre1_extra_info);
+               pcre_jit_stack_free(p->pcre1_jit_stack);
+       } else
+#endif
+       {
+               pcre_free(p->pcre1_extra_info);
+       }
+       pcre_free((void *)p->pcre1_tables);
 }
-#else /* !USE_LIBPCRE */
-static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
+#else /* !USE_LIBPCRE1 */
+static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
 {
        die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
 }
 
-static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
+static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
                regmatch_t *match, int eflags)
 {
        return 1;
 }
 
-static void free_pcre_regexp(struct grep_pat *p)
-{
-}
-#endif /* !USE_LIBPCRE */
-
-static int is_fixed(const char *s, size_t len)
+static void free_pcre1_regexp(struct grep_pat *p)
 {
-       size_t i;
-
-       /* regcomp cannot accept patterns with NULs so we
-        * consider any pattern containing a NUL fixed.
-        */
-       if (memchr(s, 0, len))
-               return 1;
-
-       for (i = 0; i < len; i++) {
-               if (is_regex_special(s[i]))
-                       return 0;
-       }
-
-       return 1;
 }
+#endif /* !USE_LIBPCRE1 */
 
 static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
 {
        struct strbuf sb = STRBUF_INIT;
        int err;
-       int regflags;
+       int regflags = opt->regflags;
 
        basic_regex_quote_buf(&sb, p->pattern);
-       regflags = opt->regflags & ~REG_EXTENDED;
        if (opt->ignore_case)
                regflags |= REG_ICASE;
        err = regcomp(&p->regexp, sb.buf, regflags);
@@ -455,7 +489,9 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
         * simple string match using kws.  p->fixed tells us if we
         * want to use kws.
         */
-       if (opt->fixed || is_fixed(p->pattern, p->patternlen))
+       if (opt->fixed ||
+           has_null(p->pattern, p->patternlen) ||
+           is_fixed(p->pattern, p->patternlen))
                p->fixed = !icase || ascii_only;
        else
                p->fixed = 0;
@@ -475,8 +511,8 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
                return;
        }
 
-       if (opt->pcre) {
-               compile_pcre_regexp(p, opt);
+       if (opt->pcre1) {
+               compile_pcre1_regexp(p, opt);
                return;
        }
 
@@ -832,8 +868,8 @@ void free_grep_patterns(struct grep_opt *opt)
                case GREP_PATTERN_BODY:
                        if (p->kws)
                                kwsfree(p->kws);
-                       else if (p->pcre_regexp)
-                               free_pcre_regexp(p);
+                       else if (p->pcre1_regexp)
+                               free_pcre1_regexp(p);
                        else
                                regfree(&p->regexp);
                        free(p->pattern);
@@ -912,8 +948,8 @@ static int patmatch(struct grep_pat *p, char *line, char *eol,
 
        if (p->fixed)
                hit = !fixmatch(p, line, eol, match);
-       else if (p->pcre_regexp)
-               hit = !pcrematch(p, line, eol, match, eflags);
+       else if (p->pcre1_regexp)
+               hit = !pcre1match(p, line, eol, match, eflags);
        else
                hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
                                   eflags);