1
0
forked from aniani/vim

patch 7.4.1964

Problem:    The quickfix init function is too big.
Solution:   Factor out parsing 'errorformat' to a separate function. (Yegappan
            Lakshmanan)
This commit is contained in:
Bram Moolenaar
2016-06-26 22:05:54 +02:00
parent 97ff9b9cff
commit 688e3d1fd9
2 changed files with 288 additions and 226 deletions

View File

@@ -205,74 +205,6 @@ qf_grow_linebuf(char_u **growbuf, int *growbufsiz, int newsz, int *allocsz)
return *growbuf;
}
/*
* Read the errorfile "efile" into memory, line by line, building the error
* list.
* Alternative: when "efile" is NULL read errors from buffer "buf".
* Alternative: when "tv" is not NULL get errors from the string or list.
* Always use 'errorformat' from "buf" if there is a local value.
* Then "lnumfirst" and "lnumlast" specify the range of lines to use.
* Set the title of the list to "qf_title".
* Return -1 for error, number of errors for success.
*/
static int
qf_init_ext(
qf_info_T *qi,
char_u *efile,
buf_T *buf,
typval_T *tv,
char_u *errorformat,
int newlist, /* TRUE: start a new error list */
linenr_T lnumfirst, /* first line number to use */
linenr_T lnumlast, /* last line number to use */
char_u *qf_title)
{
char_u *namebuf;
char_u *errmsg;
int errmsglen;
char_u *pattern;
char_u *fmtstr = NULL;
char_u *growbuf = NULL;
int growbuflen;
int growbufsiz = 0;
char_u *linebuf = NULL;
int linelen = 0;
int discard;
int col = 0;
char_u use_viscol = FALSE;
int type = 0;
int valid;
linenr_T buflnum = lnumfirst;
long lnum = 0L;
int enr = 0;
FILE *fd = NULL;
#ifdef FEAT_WINDOWS
qfline_T *old_last = NULL;
#endif
char_u *efmp;
efm_T *fmt_first = NULL;
efm_T *fmt_last = NULL;
efm_T *fmt_ptr;
efm_T *fmt_start = NULL;
char_u *efm;
char_u *ptr;
char_u *srcptr;
int len;
int i;
int round;
int idx = 0;
int multiline = FALSE;
int multiignore = FALSE;
int multiscan = FALSE;
int retval = -1; /* default: return error flag */
char_u *directory = NULL;
char_u *currfile = NULL;
char_u *tail = NULL;
char_u *p_buf = NULL;
char_u *p_str = NULL;
listitem_T *p_li = NULL;
struct dir_stack_T *file_stack = NULL;
regmatch_T regmatch;
static struct fmtpattern
{
char_u convchar;
@@ -291,78 +223,27 @@ qf_init_ext(
{'s', ".\\+"}
};
namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
errmsglen = CMDBUFFSIZE + 1;
errmsg = alloc_id(errmsglen, aid_qf_errmsg);
pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
if (namebuf == NULL || errmsg == NULL || pattern == NULL)
goto qf_init_end;
if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
/*
* Converts a 'errorformat' string to regular expression pattern
*/
static int
efm_to_regpat(
char_u *efm,
int len,
efm_T *fmt_ptr,
char_u *regpat,
char_u *errmsg)
{
EMSG2(_(e_openerrf), efile);
goto qf_init_end;
}
if (newlist || qi->qf_curlist == qi->qf_listcount)
/* make place for a new list */
qf_new_list(qi, qf_title);
#ifdef FEAT_WINDOWS
else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
{
/* Adding to existing list, use last entry. */
old_last = qi->qf_lists[qi->qf_curlist].qf_last;
}
#endif
/*
* Each part of the format string is copied and modified from errorformat to
* regex prog. Only a few % characters are allowed.
*/
/* Use the local value of 'errorformat' if it's set. */
if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
efm = buf->b_p_efm;
else
efm = errorformat;
/*
* Get some space to modify the format string into.
*/
i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
for (round = FMT_PATTERNS; round > 0; )
i += (int)STRLEN(fmt_pat[--round].pattern);
#ifdef COLON_IN_FILENAME
i += 12; /* "%f" can become twelve chars longer */
#else
i += 2; /* "%f" can become two chars longer */
#endif
if ((fmtstr = alloc(i)) == NULL)
goto error2;
while (efm[0] != NUL)
{
/*
* Allocate a new eformat structure and put it at the end of the list
*/
fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
if (fmt_ptr == NULL)
goto error2;
if (fmt_first == NULL) /* first one */
fmt_first = fmt_ptr;
else
fmt_last->next = fmt_ptr;
fmt_last = fmt_ptr;
/*
* Isolate one part in the 'errorformat' option
*/
for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
if (efm[len] == '\\' && efm[len + 1] != NUL)
++len;
char_u *ptr;
char_u *efmp;
char_u *srcptr;
int round;
int idx = 0;
/*
* Build regexp pattern from current 'errorformat' option
*/
ptr = fmtstr;
ptr = regpat;
*ptr++ = '^';
round = 0;
for (efmp = efm; efmp < efm + len; ++efmp)
@@ -380,7 +261,7 @@ qf_init_ext(
sprintf((char *)errmsg,
_("E372: Too many %%%c in format string"), *efmp);
EMSG(errmsg);
goto error2;
return -1;
}
if ((idx
&& idx < 6
@@ -393,7 +274,7 @@ qf_init_ext(
sprintf((char *)errmsg,
_("E373: Unexpected %%%c in format string"), *efmp);
EMSG(errmsg);
goto error2;
return -1;
}
fmt_ptr->addr[idx] = (char_u)++round;
*ptr++ = '\\';
@@ -454,7 +335,7 @@ qf_init_ext(
if (efmp == efm + len)
{
EMSG(_("E374: Missing ] in format string"));
goto error2;
return -1;
}
}
}
@@ -469,7 +350,7 @@ qf_init_ext(
sprintf((char *)errmsg,
_("E375: Unsupported %%%c in format string"), *efmp);
EMSG(errmsg);
goto error2;
return -1;
}
}
else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
@@ -489,7 +370,7 @@ qf_init_ext(
sprintf((char *)errmsg,
_("E376: Invalid %%%c in format string prefix"), *efmp);
EMSG(errmsg);
goto error2;
return -1;
}
}
else
@@ -497,7 +378,7 @@ qf_init_ext(
sprintf((char *)errmsg,
_("E377: Invalid %%%c in format string"), *efmp);
EMSG(errmsg);
goto error2;
return -1;
}
}
else /* copy normal character */
@@ -512,19 +393,204 @@ qf_init_ext(
}
*ptr++ = '$';
*ptr = NUL;
return 0;
}
static void
free_efm_list(efm_T **efm_first)
{
efm_T *efm_ptr;
for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
{
*efm_first = efm_ptr->next;
vim_regfree(efm_ptr->prog);
vim_free(efm_ptr);
}
}
/* Parse 'errorformat' option */
static efm_T *
parse_efm_option(char_u *efm)
{
char_u *errmsg = NULL;
int errmsglen;
efm_T *fmt_ptr = NULL;
efm_T *fmt_first = NULL;
efm_T *fmt_last = NULL;
char_u *fmtstr = NULL;
int len;
int i;
int round;
errmsglen = CMDBUFFSIZE + 1;
errmsg = alloc_id(errmsglen, aid_qf_errmsg);
if (errmsg == NULL)
goto parse_efm_end;
/*
* Get some space to modify the format string into.
*/
i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
for (round = FMT_PATTERNS; round > 0; )
i += (int)STRLEN(fmt_pat[--round].pattern);
#ifdef COLON_IN_FILENAME
i += 12; /* "%f" can become twelve chars longer */
#else
i += 2; /* "%f" can become two chars longer */
#endif
if ((fmtstr = alloc(i)) == NULL)
goto parse_efm_error;
while (efm[0] != NUL)
{
/*
* Allocate a new eformat structure and put it at the end of the list
*/
fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
if (fmt_ptr == NULL)
goto parse_efm_error;
if (fmt_first == NULL) /* first one */
fmt_first = fmt_ptr;
else
fmt_last->next = fmt_ptr;
fmt_last = fmt_ptr;
/*
* Isolate one part in the 'errorformat' option
*/
for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
if (efm[len] == '\\' && efm[len + 1] != NUL)
++len;
if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
goto parse_efm_error;
if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
goto error2;
goto parse_efm_error;
/*
* Advance to next part
*/
efm = skip_to_option_part(efm + len); /* skip comma and spaces */
}
if (fmt_first == NULL) /* nothing found */
{
EMSG(_("E378: 'errorformat' contains no pattern"));
goto error2;
goto parse_efm_end;
parse_efm_error:
free_efm_list(&fmt_first);
parse_efm_end:
vim_free(fmtstr);
vim_free(errmsg);
return fmt_first;
}
/*
* Read the errorfile "efile" into memory, line by line, building the error
* list.
* Alternative: when "efile" is NULL read errors from buffer "buf".
* Alternative: when "tv" is not NULL get errors from the string or list.
* Always use 'errorformat' from "buf" if there is a local value.
* Then "lnumfirst" and "lnumlast" specify the range of lines to use.
* Set the title of the list to "qf_title".
* Return -1 for error, number of errors for success.
*/
static int
qf_init_ext(
qf_info_T *qi,
char_u *efile,
buf_T *buf,
typval_T *tv,
char_u *errorformat,
int newlist, /* TRUE: start a new error list */
linenr_T lnumfirst, /* first line number to use */
linenr_T lnumlast, /* last line number to use */
char_u *qf_title)
{
char_u *namebuf;
char_u *errmsg;
int errmsglen;
char_u *pattern;
char_u *growbuf = NULL;
int growbuflen;
int growbufsiz = 0;
char_u *linebuf = NULL;
int linelen = 0;
int discard;
int col = 0;
char_u use_viscol = FALSE;
int type = 0;
int valid;
linenr_T buflnum = lnumfirst;
long lnum = 0L;
int enr = 0;
FILE *fd = NULL;
#ifdef FEAT_WINDOWS
qfline_T *old_last = NULL;
#endif
efm_T *fmt_first = NULL;
efm_T *fmt_ptr;
efm_T *fmt_start = NULL;
char_u *efm;
char_u *ptr;
int len;
int i;
int idx = 0;
int multiline = FALSE;
int multiignore = FALSE;
int multiscan = FALSE;
int retval = -1; /* default: return error flag */
char_u *directory = NULL;
char_u *currfile = NULL;
char_u *tail = NULL;
char_u *p_buf = NULL;
char_u *p_str = NULL;
listitem_T *p_li = NULL;
struct dir_stack_T *file_stack = NULL;
regmatch_T regmatch;
namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
errmsglen = CMDBUFFSIZE + 1;
errmsg = alloc_id(errmsglen, aid_qf_errmsg);
pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
if (namebuf == NULL || errmsg == NULL || pattern == NULL)
goto qf_init_end;
if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
{
EMSG2(_(e_openerrf), efile);
goto qf_init_end;
}
if (newlist || qi->qf_curlist == qi->qf_listcount)
/* make place for a new list */
qf_new_list(qi, qf_title);
#ifdef FEAT_WINDOWS
else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
{
/* Adding to existing list, use last entry. */
old_last = qi->qf_lists[qi->qf_curlist].qf_last;
}
#endif
/*
* Each part of the format string is copied and modified from errorformat to
* regex prog. Only a few % characters are allowed.
*/
/* Use the local value of 'errorformat' if it's set. */
if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
efm = buf->b_p_efm;
else
efm = errorformat;
fmt_first = parse_efm_option(efm);
if (fmt_first == NULL) /* nothing found */
goto error2;
/*
* got_int is reset here, because it was probably set when killing the
* ":make" command, but we still want to read the errorfile then.
@@ -1046,19 +1112,13 @@ error2:
qf_init_ok:
if (fd != NULL)
fclose(fd);
for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
{
fmt_first = fmt_ptr->next;
vim_regfree(fmt_ptr->prog);
vim_free(fmt_ptr);
}
free_efm_list(&fmt_first);
qf_clean_dir_stack(&dir_stack);
qf_clean_dir_stack(&file_stack);
qf_init_end:
vim_free(namebuf);
vim_free(errmsg);
vim_free(pattern);
vim_free(fmtstr);
vim_free(growbuf);
#ifdef FEAT_WINDOWS

View File

@@ -753,6 +753,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1964,
/**/
1963,
/**/