Merge branch 'ab/pcre-v2'
authorJunio C Hamano <gitster@pobox.com>
Wed, 15 Nov 2017 03:14:34 +0000 (12:14 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Nov 2017 03:14:34 +0000 (12:14 +0900)
Building with NO_LIBPCRE1_JIT did not disable it, which has been fixed.

* ab/pcre-v2:
grep: fix NO_LIBPCRE1_JIT to fully disable JIT

1  2 
grep.c
grep.h
diff --combined grep.c
index ce6a48e634105154ca4bb267b4eb744c74d4d803,56a7ec46ef2d0c6d6e777641bd37cf51cdd48bce..d0b9b6cdfa74537ed67dcfef4ae486466d7185f8
--- 1/grep.c
--- 2/grep.c
+++ b/grep.c
@@@ -1,5 -1,4 +1,5 @@@
  #include "cache.h"
 +#include "config.h"
  #include "grep.h"
  #include "userdiff.h"
  #include "xdiff-interface.h"
@@@ -35,8 -34,10 +35,8 @@@ void init_grep_defaults(void
        memset(opt, 0, sizeof(*opt));
        opt->relative = 1;
        opt->pathname = 1;
 -      opt->regflags = REG_NEWLINE;
        opt->max_depth = -1;
        opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
 -      opt->extended_regexp_option = 0;
        color_set(opt->color_context, "");
        color_set(opt->color_filename, "");
        color_set(opt->color_function, "");
@@@ -77,7 -78,10 +77,7 @@@ int grep_config(const char *var, const 
                return -1;
  
        if (!strcmp(var, "grep.extendedregexp")) {
 -              if (git_config_bool(var, value))
 -                      opt->extended_regexp_option = 1;
 -              else
 -                      opt->extended_regexp_option = 0;
 +              opt->extended_regexp_option = git_config_bool(var, value);
                return 0;
        }
  
@@@ -152,6 -156,7 +152,6 @@@ void grep_init(struct grep_opt *opt, co
        opt->linenum = def->linenum;
        opt->max_depth = def->max_depth;
        opt->pathname = def->pathname;
 -      opt->regflags = def->regflags;
        opt->relative = def->relative;
        opt->output = def->output;
  
  
  static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
  {
 +      /*
 +       * When committing to the pattern type by setting the relevant
 +       * fields in grep_opt it's generally not necessary to zero out
 +       * the fields we're not choosing, since they won't have been
 +       * set by anything. The extended_regexp_option field is the
 +       * only exception to this.
 +       *
 +       * This is because in the process of parsing grep.patternType
 +       * & grep.extendedRegexp we set opt->pattern_type_option and
 +       * opt->extended_regexp_option, respectively. We then
 +       * internally use opt->extended_regexp_option to see if we're
 +       * compiling an ERE. It must be unset if that's not actually
 +       * the case.
 +       */
 +      if (pattern_type != GREP_PATTERN_TYPE_ERE &&
 +          opt->extended_regexp_option)
 +              opt->extended_regexp_option = 0;
 +
        switch (pattern_type) {
        case GREP_PATTERN_TYPE_UNSPECIFIED:
                /* fall through */
  
        case GREP_PATTERN_TYPE_BRE:
 -              opt->fixed = 0;
 -              opt->pcre1 = 0;
 -              opt->pcre2 = 0;
                break;
  
        case GREP_PATTERN_TYPE_ERE:
 -              opt->fixed = 0;
 -              opt->pcre1 = 0;
 -              opt->pcre2 = 0;
 -              opt->regflags |= REG_EXTENDED;
 +              opt->extended_regexp_option = 1;
                break;
  
        case GREP_PATTERN_TYPE_FIXED:
                opt->fixed = 1;
 -              opt->pcre1 = 0;
 -              opt->pcre2 = 0;
                break;
  
        case GREP_PATTERN_TYPE_PCRE:
 -              opt->fixed = 0;
  #ifdef USE_LIBPCRE2
 -              opt->pcre1 = 0;
                opt->pcre2 = 1;
  #else
                /*
                 * "cannot use Perl-compatible regexes[...]".
                 */
                opt->pcre1 = 1;
 -              opt->pcre2 = 0;
  #endif
                break;
        }
@@@ -223,11 -221,6 +223,11 @@@ void grep_commit_pattern_type(enum grep
        else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
                grep_set_pattern_type_option(opt->pattern_type_option, opt);
        else if (opt->extended_regexp_option)
 +              /*
 +               * This branch *must* happen after setting from the
 +               * opt->pattern_type_option above, we don't want
 +               * grep.extendedRegexp to override grep.patternType!
 +               */
                grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
  }
  
@@@ -387,7 -380,7 +387,7 @@@ static void compile_pcre1_regexp(struc
        if (!p->pcre1_regexp)
                compile_regexp_failed(p, error);
  
-       p->pcre1_extra_info = pcre_study(p->pcre1_regexp, PCRE_STUDY_JIT_COMPILE, &error);
+       p->pcre1_extra_info = pcre_study(p->pcre1_regexp, GIT_PCRE_STUDY_JIT_COMPILE, &error);
        if (!p->pcre1_extra_info && error)
                die("%s", error);
  
@@@ -593,7 -586,7 +593,7 @@@ static void compile_fixed_regexp(struc
  {
        struct strbuf sb = STRBUF_INIT;
        int err;
 -      int regflags = opt->regflags;
 +      int regflags = 0;
  
        basic_regex_quote_buf(&sb, p->pattern);
        if (opt->ignore_case)
  
  static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
  {
 -      int icase, ascii_only;
 +      int ascii_only;
        int err;
 +      int regflags = REG_NEWLINE;
  
        p->word_regexp = opt->word_regexp;
        p->ignore_case = opt->ignore_case;
 -      icase          = opt->regflags & REG_ICASE || p->ignore_case;
        ascii_only     = !has_non_ascii(p->pattern);
  
        /*
        if (opt->fixed ||
            has_null(p->pattern, p->patternlen) ||
            is_fixed(p->pattern, p->patternlen))
 -              p->fixed = !icase || ascii_only;
 -      else
 -              p->fixed = 0;
 +              p->fixed = !p->ignore_case || ascii_only;
  
        if (p->fixed) {
 -              p->kws = kwsalloc(icase ? tolower_trans_tbl : NULL);
 +              p->kws = kwsalloc(p->ignore_case ? tolower_trans_tbl : NULL);
                kwsincr(p->kws, p->pattern, p->patternlen);
                kwsprep(p->kws);
                return;
                return;
        }
  
 -      err = regcomp(&p->regexp, p->pattern, opt->regflags);
 +      if (p->ignore_case)
 +              regflags |= REG_ICASE;
 +      if (opt->extended_regexp_option)
 +              regflags |= REG_EXTENDED;
 +      err = regcomp(&p->regexp, p->pattern, regflags);
        if (err) {
                char errbuf[1024];
                regerror(err, &p->regexp, errbuf, 1024);
@@@ -1593,11 -1584,11 +1593,11 @@@ static int fill_textconv_grep(struct us
         */
        df = alloc_filespec(gs->path);
        switch (gs->type) {
 -      case GREP_SOURCE_SHA1:
 +      case GREP_SOURCE_OID:
                fill_filespec(df, gs->identifier, 1, 0100644);
                break;
        case GREP_SOURCE_FILE:
 -              fill_filespec(df, null_sha1, 0, 0100644);
 +              fill_filespec(df, &null_oid, 0, 0100644);
                break;
        default:
                die("BUG: attempt to textconv something without a path?");
@@@ -1821,7 -1812,7 +1821,7 @@@ static int grep_source_1(struct grep_op
                return 0;
  
        if (opt->status_only)
 -              return 0;
 +              return opt->unmatch_name_only;
        if (opt->unmatch_name_only) {
                /* We did not see any hit, so we want to show this */
                show_name(opt, gs->name);
@@@ -1927,8 -1918,19 +1927,8 @@@ void grep_source_init(struct grep_sourc
        case GREP_SOURCE_FILE:
                gs->identifier = xstrdup(identifier);
                break;
 -      case GREP_SOURCE_SUBMODULE:
 -              if (!identifier) {
 -                      gs->identifier = NULL;
 -                      break;
 -              }
 -              /*
 -               * FALL THROUGH
 -               * If the identifier is non-NULL (in the submodule case) it
 -               * will be a SHA1 that needs to be copied.
 -               */
 -      case GREP_SOURCE_SHA1:
 -              gs->identifier = xmalloc(20);
 -              hashcpy(gs->identifier, identifier);
 +      case GREP_SOURCE_OID:
 +              gs->identifier = oiddup(identifier);
                break;
        case GREP_SOURCE_BUF:
                gs->identifier = NULL;
  
  void grep_source_clear(struct grep_source *gs)
  {
 -      free(gs->name);
 -      gs->name = NULL;
 -      free(gs->path);
 -      gs->path = NULL;
 -      free(gs->identifier);
 -      gs->identifier = NULL;
 +      FREE_AND_NULL(gs->name);
 +      FREE_AND_NULL(gs->path);
 +      FREE_AND_NULL(gs->identifier);
        grep_source_clear_data(gs);
  }
  
@@@ -1948,8 -1953,10 +1948,8 @@@ void grep_source_clear_data(struct grep
  {
        switch (gs->type) {
        case GREP_SOURCE_FILE:
 -      case GREP_SOURCE_SHA1:
 -      case GREP_SOURCE_SUBMODULE:
 -              free(gs->buf);
 -              gs->buf = NULL;
 +      case GREP_SOURCE_OID:
 +              FREE_AND_NULL(gs->buf);
                gs->size = 0;
                break;
        case GREP_SOURCE_BUF:
        }
  }
  
 -static int grep_source_load_sha1(struct grep_source *gs)
 +static int grep_source_load_oid(struct grep_source *gs)
  {
        enum object_type type;
  
        if (!gs->buf)
                return error(_("'%s': unable to read %s"),
                             gs->name,
 -                           sha1_to_hex(gs->identifier));
 +                           oid_to_hex(gs->identifier));
        return 0;
  }
  
@@@ -2015,10 -2022,12 +2015,10 @@@ static int grep_source_load(struct grep
        switch (gs->type) {
        case GREP_SOURCE_FILE:
                return grep_source_load_file(gs);
 -      case GREP_SOURCE_SHA1:
 -              return grep_source_load_sha1(gs);
 +      case GREP_SOURCE_OID:
 +              return grep_source_load_oid(gs);
        case GREP_SOURCE_BUF:
                return gs->buf ? 0 : -1;
 -      case GREP_SOURCE_SUBMODULE:
 -              break;
        }
        die("BUG: invalid grep_source type to load");
  }
diff --combined grep.h
index 52aecfab6ebdd6c5565e79db6062b7d7a99cf3df,377f6ba560b95907db5182a14d2c3f6571bec26a..399381c908c2c93cbcf447498fb435e517dddde0
--- 1/grep.h
--- 2/grep.h
+++ b/grep.h
@@@ -7,11 -7,12 +7,12 @@@
  #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
  #ifndef NO_LIBPCRE1_JIT
  #define GIT_PCRE1_USE_JIT
+ #define GIT_PCRE_STUDY_JIT_COMPILE PCRE_STUDY_JIT_COMPILE
  #endif
  #endif
  #endif
- #ifndef PCRE_STUDY_JIT_COMPILE
- #define PCRE_STUDY_JIT_COMPILE 0
+ #ifndef GIT_PCRE_STUDY_JIT_COMPILE
+ #define GIT_PCRE_STUDY_JIT_COMPILE 0
  #endif
  #if PCRE_MAJOR <= 8 && PCRE_MINOR < 20
  typedef int pcre_jit_stack;
@@@ -162,6 -163,7 +163,6 @@@ struct grep_opt 
        char color_match_selected[COLOR_MAXLEN];
        char color_selected[COLOR_MAXLEN];
        char color_sep[COLOR_MAXLEN];
 -      int regflags;
        unsigned pre_context;
        unsigned post_context;
        unsigned last_shown;
@@@ -190,9 -192,10 +191,9 @@@ struct grep_source 
        char *name;
  
        enum grep_source_type {
 -              GREP_SOURCE_SHA1,
 +              GREP_SOURCE_OID,
                GREP_SOURCE_FILE,
                GREP_SOURCE_BUF,
 -              GREP_SOURCE_SUBMODULE,
        } type;
        void *identifier;