refactor(uncrustify): enable formatting for regexp and indent files (#18549)

The formatting for these files were originally disabled as to signal
that "we don't own these files", meaning we intentionally want to
minimize the amount of work put in these files as the return will be
very little. This unfortunately conflicts with other refactoring efforts
that happen to touch these files, and it's easier to simply enable
formatting.
This commit is contained in:
dundargoc
2022-05-14 20:35:39 +02:00
committed by GitHub
parent 83da441d16
commit 0adc66171a
4 changed files with 3826 additions and 2355 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,6 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
// uncrustify:off
/*
* Handling of regular expressions: vim_regcomp(), vim_regexec(), vim_regsub()
*/
@@ -18,21 +16,21 @@
#include <stdbool.h>
#include <string.h>
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/regexp.h"
#include "nvim/charset.h"
#include "nvim/eval.h"
#include "nvim/eval/userfunc.h"
#include "nvim/ex_cmds2.h"
#include "nvim/garray.h"
#include "nvim/mark.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/os/input.h"
#include "nvim/plines.h"
#include "nvim/garray.h"
#include "nvim/regexp.h"
#include "nvim/strings.h"
#include "nvim/vim.h"
#ifdef REGEXP_DEBUG
// show/save debugging data when BT engine is used
@@ -62,15 +60,17 @@ typedef void (*(*fptr_T)(int *, int))(void);
static int no_Magic(int x)
{
if (is_Magic(x))
if (is_Magic(x)) {
return un_Magic(x);
}
return x;
}
static int toggle_Magic(int x)
{
if (is_Magic(x))
if (is_Magic(x)) {
return un_Magic(x);
}
return Magic(x);
}
@@ -93,8 +93,7 @@ static int toggle_Magic(int x)
return (semsg((const char *)(m), (c) ? "" : "\\", (a)), rc_did_emsg = true, (void *)NULL)
#define EMSG2_RET_FAIL(m, c) \
return (semsg((m), (c) ? "" : "\\"), rc_did_emsg = true, FAIL)
#define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_( \
"E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
#define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
#define MAX_LIMIT (32767L << 16L)
@@ -128,10 +127,12 @@ static char_u e_regexp_number_after_dot_pos_search[]
/// Return MULTI_MULT if c is a multi "multi" operator.
static int re_multi_type(int c)
{
if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
if (c == Magic('@') || c == Magic('=') || c == Magic('?')) {
return MULTI_ONE;
if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
}
if (c == Magic('*') || c == Magic('+') || c == Magic('{')) {
return MULTI_MULT;
}
return NOT_MULTI;
}
@@ -163,10 +164,14 @@ static char_u REGEXP_ABBR[] = "nrtebdoxuU";
static int backslash_trans(int c)
{
switch (c) {
case 'r': return CAR;
case 't': return TAB;
case 'e': return ESC;
case 'b': return BS;
case 'r':
return CAR;
case 't':
return TAB;
case 'e':
return ESC;
case 'b':
return BS;
}
return c;
}
@@ -223,12 +228,13 @@ static int get_char_class(char_u **pp)
int i;
if ((*pp)[1] == ':') {
for (i = 0; i < (int)ARRAY_SIZE(class_names); ++i)
for (i = 0; i < (int)ARRAY_SIZE(class_names); i++) {
if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0) {
*pp += STRLEN(class_names[i]) + 2;
return i;
}
}
}
return CLASS_NONE;
}
@@ -253,27 +259,29 @@ static void init_class_tab(void)
int i;
static int done = false;
if (done)
if (done) {
return;
}
for (i = 0; i < 256; ++i) {
if (i >= '0' && i <= '7')
for (i = 0; i < 256; i++) {
if (i >= '0' && i <= '7') {
class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
else if (i >= '8' && i <= '9')
} else if (i >= '8' && i <= '9') {
class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
else if (i >= 'a' && i <= 'f')
} else if (i >= 'a' && i <= 'f') {
class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
else if (i >= 'g' && i <= 'z')
} else if (i >= 'g' && i <= 'z') {
class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
else if (i >= 'A' && i <= 'F')
} else if (i >= 'A' && i <= 'F') {
class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
else if (i >= 'G' && i <= 'Z')
} else if (i >= 'G' && i <= 'Z') {
class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
else if (i == '_')
} else if (i == '_') {
class_tab[i] = RI_WORD + RI_HEAD;
else
} else {
class_tab[i] = 0;
}
}
class_tab[' '] |= RI_WHITE;
class_tab['\t'] |= RI_WHITE;
done = true;
@@ -320,6 +328,8 @@ static int reg_strict; // "[abc" is illegal
* META contains all characters that may be magic, except '^' and '$'.
*/
// uncrustify:off
// META[] is used often enough to justify turning it into a table.
static char_u META_flags[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -338,6 +348,8 @@ static char_u META_flags[] = {
1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
};
// uncrustify:on
static int curchr; // currently parsed character
// Previous character. Note: prevchr is sometimes -1 when we are not at the
// start, eg in /[ ^I]^ the pattern was never found even if it existed,
@@ -490,10 +502,11 @@ char_u *skip_regexp(char_u *startp, int dirc, int magic, char_u **newp)
int mymagic;
char_u *p = startp;
if (magic)
if (magic) {
mymagic = MAGIC_ON;
else
} else {
mymagic = MAGIC_OFF;
}
get_cpo_flags();
for (; p[0] != NUL; MB_PTR_ADV(p)) {
@@ -503,8 +516,9 @@ char_u *skip_regexp(char_u *startp, int dirc, int magic, char_u **newp)
if ((p[0] == '[' && mymagic >= MAGIC_ON)
|| (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF)) {
p = skip_anyof(p + 1);
if (p[0] == NUL)
if (p[0] == NUL) {
break;
}
} else if (p[0] == '\\' && p[1] != NUL) {
if (dirc == '?' && newp != NULL && p[1] == '?') {
// change "\?" to "?", make a copy first.
@@ -686,8 +700,7 @@ static int peekchr(void)
}
}
break;
case '\\':
{
case '\\': {
int c = regparse[1];
if (c == NUL) {
@@ -712,13 +725,11 @@ static int peekchr(void)
* Handle abbreviations, like "\t" for TAB -- webb
*/
curchr = backslash_trans(c);
} else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
} else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^')) {
curchr = toggle_Magic(c);
else {
/*
* Next character can never be (made) magic?
* Then backslashing it won't do anything.
*/
} else {
// Next character can never be (made) magic?
// Then backslashing it won't do anything.
curchr = utf_ptr2char((char *)regparse + 1);
}
break;
@@ -816,15 +827,17 @@ static int64_t gethexchrs(int maxinputlen)
for (i = 0; i < maxinputlen; ++i) {
c = regparse[0];
if (!ascii_isxdigit(c))
if (!ascii_isxdigit(c)) {
break;
}
nr <<= 4;
nr |= hex2nr(c);
++regparse;
}
if (i == 0)
if (i == 0) {
return -1;
}
return nr;
}
@@ -840,16 +853,18 @@ static int64_t getdecchrs(void)
for (i = 0;; ++i) {
c = regparse[0];
if (c < '0' || c > '9')
if (c < '0' || c > '9') {
break;
}
nr *= 10;
nr += c - '0';
regparse++;
curchr = -1; // no longer valid
}
if (i == 0)
if (i == 0) {
return -1;
}
return nr;
}
@@ -869,15 +884,17 @@ static int64_t getoctchrs(void)
for (i = 0; i < 3 && nr < 040; i++) { // -V536
c = regparse[0];
if (c < '0' || c > '7')
if (c < '0' || c > '7') {
break;
}
nr <<= 3;
nr |= hex2nr(c);
++regparse;
}
if (i == 0)
if (i == 0) {
return -1;
}
return nr;
}
@@ -1063,8 +1080,9 @@ static reg_extmatch_T *make_extmatch(void)
*/
reg_extmatch_T *ref_extmatch(reg_extmatch_T *em)
{
if (em != NULL)
if (em != NULL) {
em->refcnt++;
}
return em;
}
@@ -1077,8 +1095,9 @@ void unref_extmatch(reg_extmatch_T *em)
int i;
if (em != NULL && --em->refcnt <= 0) {
for (i = 0; i < NSUBEXP; ++i)
for (i = 0; i < NSUBEXP; i++) {
xfree(em->matches[i]);
}
xfree(em);
}
}
@@ -1087,8 +1106,7 @@ void unref_extmatch(reg_extmatch_T *em)
static int reg_prev_class(void)
{
if (rex.input > rex.line) {
return mb_get_class_tab(
rex.input - 1 - utf_head_off(rex.line, rex.input - 1),
return mb_get_class_tab(rex.input - 1 - utf_head_off(rex.line, rex.input - 1),
rex.reg_buf->b_chartab);
}
return -1;
@@ -1147,10 +1165,12 @@ static bool reg_match_visual(void)
} else if (mode == Ctrl_V) {
getvvcol(wp, &top, &start, NULL, &end);
getvvcol(wp, &bot, &start2, NULL, &end2);
if (start2 < start)
if (start2 < start) {
start = start2;
if (end2 > end)
}
if (end2 > end) {
end = end2;
}
if (top.col == MAXCOL || bot.col == MAXCOL || curswant == MAXCOL) {
end = MAXCOL;
}
@@ -1241,20 +1261,17 @@ static void reg_nextline(void)
* If "bytelen" is not NULL, it is set to the byte length of the match in the
* last line.
*/
static int match_with_backref(
linenr_T start_lnum,
colnr_T start_col,
linenr_T end_lnum,
colnr_T end_col,
int *bytelen)
static int match_with_backref(linenr_T start_lnum, colnr_T start_col, linenr_T end_lnum,
colnr_T end_col, int *bytelen)
{
linenr_T clnum = start_lnum;
colnr_T ccol = start_col;
int len;
char_u *p;
if (bytelen != NULL)
if (bytelen != NULL) {
*bytelen = 0;
}
for (;;) {
// Since getting one line may invalidate the other, need to make copy.
// Slow!
@@ -1275,10 +1292,11 @@ static int match_with_backref(
p = reg_getline(clnum);
assert(p);
if (clnum == end_lnum)
if (clnum == end_lnum) {
len = end_col - ccol;
else
} else {
len = (int)STRLEN(p + ccol);
}
if (cstrncmp(p + ccol, rex.input, &len) != 0) {
return RA_NOMATCH; // doesn't match
@@ -1295,13 +1313,15 @@ static int match_with_backref(
// Advance to next line.
reg_nextline();
if (bytelen != NULL)
if (bytelen != NULL) {
*bytelen = 0;
++clnum;
}
clnum++;
ccol = 0;
if (got_int)
if (got_int) {
return RA_FAIL;
}
}
// found a match! Note that rex.line may now point to a copy of the line,
// that should not matter.
@@ -1435,9 +1455,10 @@ static int cstrncmp(char_u *s1, char_u *s2, int *n)
}
}
result = c2 - c1;
if (result == 0)
if (result == 0) {
*n = (int)(str2 - s2);
}
}
return result;
}
@@ -1605,8 +1626,7 @@ static regsubmatch_T rsm; // can only be used when can_f_submatch is true
/// Put the submatches in "argv[argskip]" which is a list passed into
/// call_func() by vim_regsub_both().
static int fill_submatch_list(int argc FUNC_ATTR_UNUSED, typval_T *argv,
int argskip, int argcount)
static int fill_submatch_list(int argc FUNC_ATTR_UNUSED, typval_T *argv, int argskip, int argcount)
FUNC_ATTR_NONNULL_ALL
{
typval_T *listarg = argv + argskip;
@@ -1658,8 +1678,8 @@ static void clear_submatch_list(staticList10_T *sl)
/// references invalid!
///
/// Returns the size of the replacement, including terminating NUL.
int vim_regsub(regmatch_T *rmp, char_u *source, typval_T *expr, char_u *dest,
int copy, int magic, int backslash)
int vim_regsub(regmatch_T *rmp, char_u *source, typval_T *expr, char_u *dest, int copy, int magic,
int backslash)
{
regexec_T rex_save;
bool rex_in_use_save = rex_in_use;
@@ -1685,8 +1705,8 @@ int vim_regsub(regmatch_T *rmp, char_u *source, typval_T *expr, char_u *dest,
return result;
}
int vim_regsub_multi(regmmatch_T *rmp, linenr_T lnum, char_u *source, char_u *dest,
int copy, int magic, int backslash)
int vim_regsub_multi(regmmatch_T *rmp, linenr_T lnum, char_u *source, char_u *dest, int copy,
int magic, int backslash)
{
regexec_T rex_save;
bool rex_in_use_save = rex_in_use;
@@ -1713,8 +1733,8 @@ int vim_regsub_multi(regmmatch_T *rmp, linenr_T lnum, char_u *source, char_u *de
return result;
}
static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest,
int copy, int magic, int backslash)
static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest, int copy, int magic,
int backslash)
{
char_u *src;
char_u *dst;
@@ -1737,8 +1757,9 @@ static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest,
emsg(_(e_null));
return 0;
}
if (prog_magic_wrong())
if (prog_magic_wrong()) {
return 0;
}
src = source;
dst = dest;
@@ -1851,11 +1872,11 @@ static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest,
rsm = rsm_save;
}
}
} else
} else {
while ((c = *src++) != NUL) {
if (c == '&' && magic)
if (c == '&' && magic) {
no = 0;
else if (c == '\\' && *src != NUL) {
} else if (c == '\\' && *src != NUL) {
if (*src == '&' && !magic) {
++src;
no = 0;
@@ -1863,16 +1884,21 @@ static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest,
no = *src++ - '0';
} else if (vim_strchr((char_u *)"uUlLeE", *src)) {
switch (*src++) {
case 'u': func_one = (fptr_T)do_upper;
case 'u':
func_one = (fptr_T)do_upper;
continue;
case 'U': func_all = (fptr_T)do_Upper;
case 'U':
func_all = (fptr_T)do_Upper;
continue;
case 'l': func_one = (fptr_T)do_lower;
case 'l':
func_one = (fptr_T)do_lower;
continue;
case 'L': func_all = (fptr_T)do_Lower;
case 'L':
func_all = (fptr_T)do_Lower;
continue;
case 'e':
case 'E': func_one = func_all = (fptr_T)NULL;
case 'E':
func_one = func_all = (fptr_T)NULL;
continue;
}
}
@@ -1894,12 +1920,16 @@ static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest,
if (c == '\\' && *src != NUL) {
// Check for abbreviations -- webb
switch (*src) {
case 'r': c = CAR; ++src; break;
case 'n': c = NL; ++src; break;
case 't': c = TAB; ++src; break;
case 'r':
c = CAR; ++src; break;
case 'n':
c = NL; ++src; break;
case 't':
c = TAB; ++src; break;
// Oh no! \e already has meaning in subst pat :-(
// case 'e': c = ESC; ++src; break;
case 'b': c = Ctrl_H; ++src; break;
case 'b':
c = Ctrl_H; ++src; break;
// If "backslash" is true the backslash will be removed
// later. Used to insert a literal CR.
@@ -2042,8 +2072,10 @@ static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest,
no = -1;
}
}
if (copy)
}
if (copy) {
*dst = NUL;
}
exit:
return (int)((dst - dest) + 1 - num_escaped);
@@ -2083,8 +2115,9 @@ char_u *reg_submatch(int no)
int round;
linenr_T lnum;
if (!can_f_submatch || no < 0)
if (!can_f_submatch || no < 0) {
return NULL;
}
if (rsm.sm_match == NULL) {
ssize_t len;
@@ -2123,12 +2156,14 @@ char_u *reg_submatch(int no)
lnum++;
while (lnum < rsm.sm_mmatch->endpos[no].lnum) {
s = reg_getline_submatch(lnum++);
if (round == 2)
if (round == 2) {
STRCPY(retval + len, s);
}
len += STRLEN(s);
if (round == 2)
if (round == 2) {
retval[len] = '\n';
++len;
}
len++;
}
if (round == 2) {
STRNCPY(retval + len, reg_getline_submatch(lnum),
@@ -2273,8 +2308,7 @@ regprog_T *vim_regcomp(char_u *expr_arg, int re_flags)
regname[newengine]);
#endif
} else {
emsg(_(
"E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
emsg(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
regexp_engine = AUTOMATIC_ENGINE;
}
}
@@ -2339,9 +2373,10 @@ regprog_T *vim_regcomp(char_u *expr_arg, int re_flags)
*/
void vim_regfree(regprog_T *prog)
{
if (prog != NULL)
if (prog != NULL) {
prog->engine->regfree(prog);
}
}
#if defined(EXITFREE)
@@ -2462,16 +2497,17 @@ bool vim_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col)
/// Note: "rmp->regprog" may be freed and changed, even set to NULL.
/// Uses curbuf for line count and 'iskeyword'.
///
/// Return zero if there is no match. Return number of lines contained in the
/// @param win window in which to search or NULL
/// @param buf buffer in which to search
/// @param lnum nr of line to start looking for match
/// @param col column to start looking for match
/// @param tm timeout limit or NULL
/// @param timed_out flag is set when timeout limit reached
///
/// @return zero if there is no match. Return number of lines contained in the
/// match otherwise.
long vim_regexec_multi(
regmmatch_T *rmp,
win_T *win, // window in which to search or NULL
buf_T *buf, // buffer in which to search
linenr_T lnum, // nr of line to start looking for match
colnr_T col, // column to start looking for match
proftime_T *tm, // timeout limit or NULL
int *timed_out) // flag is set when timeout limit reached
long vim_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col,
proftime_T *tm, int *timed_out)
FUNC_ATTR_NONNULL_ARG(1)
{
regexec_T rex_save;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff