mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Define and use the ARRAY_SIZE macro
A similar macro is defined in the Linux kernel [1]. To refactor the code I used a slightly modified Coccinelle script I found in [2]. ```diff // Use the macro ARRAY_SIZE when possible // // Confidence: High // Copyright: (C) Gilles Muller, Julia Lawall, EMN, DIKU. GPLv2. // URL: http://www.emn.fr/x-info/coccinelle/rules/array.html // Options: -I ... -all_includes can give more complete results @@ type T; T[] E; @@ - (sizeof(E)/sizeof(*E)) + ARRAY_SIZE(E) @@ type T; T[] E; @@ - (sizeof(E)/sizeof(E[...])) + ARRAY_SIZE(E) @@ type T; T[] E; @@ - (sizeof(E)/sizeof(T)) + ARRAY_SIZE(E) @n@ identifier AS,E; @@ - #define AS(E) ARRAY_SIZE(E) @@ expression E; identifier n.AS; @@ - AS(E) + ARRAY_SIZE(E) ``` `spatch --in-place --sp-file array_size.cocci -I src/ -I build/include/ -I build/src/nvim/auto/ src/nvim/*.c` [1] http://lxr.free-electrons.com/source/include/linux/kernel.h#L54 [2] http://www.emn.fr/z-info/coccinelle/rules/#macros
This commit is contained in:
parent
bd19cc4f8f
commit
3c857900fe
@ -6652,7 +6652,7 @@ char_u *get_function_name(expand_T *xp, int idx)
|
|||||||
if (name != NULL)
|
if (name != NULL)
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
if (++intidx < (int)(sizeof(functions) / sizeof(struct fst))) {
|
if (++intidx < (int)ARRAY_SIZE(functions)) {
|
||||||
STRCPY(IObuff, functions[intidx].f_name);
|
STRCPY(IObuff, functions[intidx].f_name);
|
||||||
STRCAT(IObuff, "(");
|
STRCAT(IObuff, "(");
|
||||||
if (functions[intidx].f_max_argc == 0)
|
if (functions[intidx].f_max_argc == 0)
|
||||||
@ -6695,7 +6695,7 @@ find_internal_func (
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
int first = 0;
|
int first = 0;
|
||||||
int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
|
int last = (int)ARRAY_SIZE(functions) - 1;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find the function name in the table. Binary search.
|
* Find the function name in the table. Binary search.
|
||||||
|
@ -4954,7 +4954,7 @@ int find_help_tags(char_u *arg, int *num_matches, char_u ***matches, int keep_la
|
|||||||
* Recognize a few exceptions to the rule. Some strings that contain '*'
|
* Recognize a few exceptions to the rule. Some strings that contain '*'
|
||||||
* with "star". Otherwise '*' is recognized as a wildcard.
|
* with "star". Otherwise '*' is recognized as a wildcard.
|
||||||
*/
|
*/
|
||||||
for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; )
|
for (i = (int)ARRAY_SIZE(mtable); --i >= 0; )
|
||||||
if (STRCMP(arg, mtable[i]) == 0) {
|
if (STRCMP(arg, mtable[i]) == 0) {
|
||||||
STRCPY(d, rtable[i]);
|
STRCPY(d, rtable[i]);
|
||||||
break;
|
break;
|
||||||
|
@ -2281,7 +2281,7 @@ int modifier_len(char_u *cmd)
|
|||||||
|
|
||||||
if (VIM_ISDIGIT(*cmd))
|
if (VIM_ISDIGIT(*cmd))
|
||||||
p = skipwhite(skipdigits(cmd));
|
p = skipwhite(skipdigits(cmd));
|
||||||
for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i) {
|
for (i = 0; i < (int)ARRAY_SIZE(cmdmods); ++i) {
|
||||||
for (j = 0; p[j] != NUL; ++j)
|
for (j = 0; p[j] != NUL; ++j)
|
||||||
if (p[j] != cmdmods[i].name[j])
|
if (p[j] != cmdmods[i].name[j])
|
||||||
break;
|
break;
|
||||||
@ -2306,7 +2306,7 @@ int cmd_exists(char_u *name)
|
|||||||
char_u *p;
|
char_u *p;
|
||||||
|
|
||||||
/* Check command modifiers. */
|
/* Check command modifiers. */
|
||||||
for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i) {
|
for (i = 0; i < (int)ARRAY_SIZE(cmdmods); ++i) {
|
||||||
for (j = 0; name[j] != NUL; ++j)
|
for (j = 0; name[j] != NUL; ++j)
|
||||||
if (name[j] != cmdmods[i].name[j])
|
if (name[j] != cmdmods[i].name[j])
|
||||||
break;
|
break;
|
||||||
@ -4974,7 +4974,7 @@ char_u *get_user_cmd_flags(expand_T *xp, int idx)
|
|||||||
{"bang", "bar", "buffer", "complete", "count",
|
{"bang", "bar", "buffer", "complete", "count",
|
||||||
"nargs", "range", "register"};
|
"nargs", "range", "register"};
|
||||||
|
|
||||||
if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
|
if (idx >= (int)ARRAY_SIZE(user_cmd_flags))
|
||||||
return NULL;
|
return NULL;
|
||||||
return (char_u *)user_cmd_flags[idx];
|
return (char_u *)user_cmd_flags[idx];
|
||||||
}
|
}
|
||||||
@ -4986,7 +4986,7 @@ char_u *get_user_cmd_nargs(expand_T *xp, int idx)
|
|||||||
{
|
{
|
||||||
static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
|
static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
|
||||||
|
|
||||||
if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
|
if (idx >= (int)ARRAY_SIZE(user_cmd_nargs))
|
||||||
return NULL;
|
return NULL;
|
||||||
return (char_u *)user_cmd_nargs[idx];
|
return (char_u *)user_cmd_nargs[idx];
|
||||||
}
|
}
|
||||||
|
@ -3715,7 +3715,7 @@ ExpandFromContext (
|
|||||||
* right function to do the expansion.
|
* right function to do the expansion.
|
||||||
*/
|
*/
|
||||||
ret = FAIL;
|
ret = FAIL;
|
||||||
for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
|
for (i = 0; i < (int)ARRAY_SIZE(tab); ++i)
|
||||||
if (xp->xp_context == tab[i].context) {
|
if (xp->xp_context == tab[i].context) {
|
||||||
if (tab[i].ic) {
|
if (tab[i].ic) {
|
||||||
regmatch.rm_ic = TRUE;
|
regmatch.rm_ic = TRUE;
|
||||||
@ -4155,7 +4155,7 @@ static char_u *get_history_arg(expand_T *xp, int idx)
|
|||||||
static char_u compl[2] = { NUL, NUL };
|
static char_u compl[2] = { NUL, NUL };
|
||||||
char *short_names = ":=@>?/";
|
char *short_names = ":=@>?/";
|
||||||
int short_names_count = (int)STRLEN(short_names);
|
int short_names_count = (int)STRLEN(short_names);
|
||||||
int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
|
int history_name_count = ARRAY_SIZE(history_names) - 1;
|
||||||
|
|
||||||
if (idx < short_names_count) {
|
if (idx < short_names_count) {
|
||||||
compl[0] = (char_u)short_names[idx];
|
compl[0] = (char_u)short_names[idx];
|
||||||
|
@ -303,7 +303,7 @@ static int cin_isinit(void)
|
|||||||
for (;; ) {
|
for (;; ) {
|
||||||
int i, l;
|
int i, l;
|
||||||
|
|
||||||
for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) {
|
for (i = 0; i < (int)ARRAY_SIZE(skip); ++i) {
|
||||||
l = (int)strlen(skip[i]);
|
l = (int)strlen(skip[i]);
|
||||||
if (cin_starts_with(s, skip[i])) {
|
if (cin_starts_with(s, skip[i])) {
|
||||||
s = cin_skipcomment(s + l);
|
s = cin_skipcomment(s + l);
|
||||||
|
@ -284,8 +284,7 @@ static struct key_name_entry {
|
|||||||
{0, NULL}
|
{0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / \
|
#define KEY_NAMES_TABLE_LEN ARRAY_SIZE(key_names_table)
|
||||||
sizeof(struct key_name_entry))
|
|
||||||
|
|
||||||
static struct mousetable {
|
static struct mousetable {
|
||||||
int pseudo_code; /* Code for pseudo mouse event */
|
int pseudo_code; /* Code for pseudo mouse event */
|
||||||
|
@ -152,4 +152,12 @@
|
|||||||
|
|
||||||
# define RESET_BINDING(wp) (wp)->w_p_scb = FALSE; (wp)->w_p_crb = FALSE
|
# define RESET_BINDING(wp) (wp)->w_p_scb = FALSE; (wp)->w_p_crb = FALSE
|
||||||
|
|
||||||
|
/// Calculate the length of a C array.
|
||||||
|
///
|
||||||
|
/// This should be called with a real array. Calling this with a pointer is an
|
||||||
|
/// error. A mechanism to detect many (though not all) of those errors at compile
|
||||||
|
/// time is implemented. It works by the second division producing a division by
|
||||||
|
/// zero in those cases (-Wdiv-by-zero in GCC).
|
||||||
|
#define ARRAY_SIZE(arr) ((sizeof(arr)/sizeof((arr)[0])) / ((size_t)(!(sizeof(arr) % sizeof((arr)[0])))))
|
||||||
|
|
||||||
#endif // NVIM_MACROS_H
|
#endif // NVIM_MACROS_H
|
||||||
|
@ -2138,7 +2138,7 @@ int utf_class(int c)
|
|||||||
{0x2f800, 0x2fa1f, 0x4e00}, /* CJK Ideographs */
|
{0x2f800, 0x2fa1f, 0x4e00}, /* CJK Ideographs */
|
||||||
};
|
};
|
||||||
int bot = 0;
|
int bot = 0;
|
||||||
int top = sizeof(classes) / sizeof(struct clinterval) - 1;
|
int top = ARRAY_SIZE(classes) - 1;
|
||||||
int mid;
|
int mid;
|
||||||
|
|
||||||
/* First quick check for Latin1 characters, use 'iskeyword'. */
|
/* First quick check for Latin1 characters, use 'iskeyword'. */
|
||||||
|
@ -1883,7 +1883,7 @@ void set_init_1(void)
|
|||||||
int mustfree;
|
int mustfree;
|
||||||
|
|
||||||
ga_init(&ga, 1, 100);
|
ga_init(&ga, 1, 100);
|
||||||
for (n = 0; n < (long)(sizeof(names) / sizeof(char *)); ++n) {
|
for (n = 0; n < (long)ARRAY_SIZE(names); ++n) {
|
||||||
mustfree = FALSE;
|
mustfree = FALSE;
|
||||||
# ifdef UNIX
|
# ifdef UNIX
|
||||||
if (*names[n] == NUL)
|
if (*names[n] == NUL)
|
||||||
@ -4723,10 +4723,10 @@ static char_u *set_chars_option(char_u **varp)
|
|||||||
|
|
||||||
if (varp == &p_lcs) {
|
if (varp == &p_lcs) {
|
||||||
tab = lcstab;
|
tab = lcstab;
|
||||||
entries = sizeof(lcstab) / sizeof(struct charstab);
|
entries = ARRAY_SIZE(lcstab);
|
||||||
} else {
|
} else {
|
||||||
tab = filltab;
|
tab = filltab;
|
||||||
entries = sizeof(filltab) / sizeof(struct charstab);
|
entries = ARRAY_SIZE(filltab);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* first round: check for valid value, second round: assign values */
|
/* first round: check for valid value, second round: assign values */
|
||||||
@ -7250,7 +7250,7 @@ int ExpandSettings(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***
|
|||||||
for (loop = 0; loop <= 1; ++loop) {
|
for (loop = 0; loop <= 1; ++loop) {
|
||||||
regmatch->rm_ic = ic;
|
regmatch->rm_ic = ic;
|
||||||
if (xp->xp_context != EXPAND_BOOL_SETTINGS) {
|
if (xp->xp_context != EXPAND_BOOL_SETTINGS) {
|
||||||
for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
|
for (match = 0; match < (int)ARRAY_SIZE(names);
|
||||||
++match)
|
++match)
|
||||||
if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0)) {
|
if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0)) {
|
||||||
if (loop == 0)
|
if (loop == 0)
|
||||||
|
@ -602,7 +602,7 @@ static int get_char_class(char_u **pp)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
if ((*pp)[1] == ':') {
|
if ((*pp)[1] == ':') {
|
||||||
for (i = 0; i < (int)(sizeof(class_names) / sizeof(*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) {
|
if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0) {
|
||||||
*pp += STRLEN(class_names[i]) + 2;
|
*pp += STRLEN(class_names[i]) + 2;
|
||||||
return i;
|
return i;
|
||||||
|
@ -3943,7 +3943,7 @@ get_syn_options (
|
|||||||
if (strchr(first_letters, *arg) == NULL)
|
if (strchr(first_letters, *arg) == NULL)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
for (fidx = sizeof(flagtab) / sizeof(struct flag); --fidx >= 0; ) {
|
for (fidx = ARRAY_SIZE(flagtab); --fidx >= 0; ) {
|
||||||
p = flagtab[fidx].name;
|
p = flagtab[fidx].name;
|
||||||
int i;
|
int i;
|
||||||
for (i = 0, len = 0; p[i] != NUL; i += 2, ++len)
|
for (i = 0, len = 0; p[i] != NUL; i += 2, ++len)
|
||||||
@ -6295,7 +6295,7 @@ do_highlight (
|
|||||||
attr = 0;
|
attr = 0;
|
||||||
off = 0;
|
off = 0;
|
||||||
while (arg[off] != NUL) {
|
while (arg[off] != NUL) {
|
||||||
for (i = sizeof(hl_attr_table) / sizeof(int); --i >= 0; ) {
|
for (i = ARRAY_SIZE(hl_attr_table); --i >= 0; ) {
|
||||||
len = (int)STRLEN(hl_name_table[i]);
|
len = (int)STRLEN(hl_name_table[i]);
|
||||||
if (STRNICMP(arg + off, hl_name_table[i], len) == 0) {
|
if (STRNICMP(arg + off, hl_name_table[i], len) == 0) {
|
||||||
attr |= hl_attr_table[i];
|
attr |= hl_attr_table[i];
|
||||||
@ -6416,7 +6416,7 @@ do_highlight (
|
|||||||
|
|
||||||
/* reduce calls to STRICMP a bit, it can be slow */
|
/* reduce calls to STRICMP a bit, it can be slow */
|
||||||
off = TOUPPER_ASC(*arg);
|
off = TOUPPER_ASC(*arg);
|
||||||
for (i = (sizeof(color_names) / sizeof(char *)); --i >= 0; )
|
for (i = ARRAY_SIZE(color_names); --i >= 0; )
|
||||||
if (off == color_names[i][0]
|
if (off == color_names[i][0]
|
||||||
&& STRICMP(arg + 1, color_names[i] + 1) == 0)
|
&& STRICMP(arg + 1, color_names[i] + 1) == 0)
|
||||||
break;
|
break;
|
||||||
|
@ -986,7 +986,7 @@ void intro_message(int colon)
|
|||||||
};
|
};
|
||||||
|
|
||||||
// blanklines = screen height - # message lines
|
// blanklines = screen height - # message lines
|
||||||
blanklines = (int)Rows - ((sizeof(lines) / sizeof(char *)) - 1);
|
blanklines = (int)Rows - (ARRAY_SIZE(lines) - 1);
|
||||||
|
|
||||||
// Don't overwrite a statusline. Depends on 'cmdheight'.
|
// Don't overwrite a statusline. Depends on 'cmdheight'.
|
||||||
if (p_ls > 1) {
|
if (p_ls > 1) {
|
||||||
@ -1006,7 +1006,7 @@ void intro_message(int colon)
|
|||||||
row = blanklines / 2;
|
row = blanklines / 2;
|
||||||
|
|
||||||
if (((row >= 2) && (Columns >= 50)) || colon) {
|
if (((row >= 2) && (Columns >= 50)) || colon) {
|
||||||
for (i = 0; i < (int)(sizeof(lines) / sizeof(char *)); ++i) {
|
for (i = 0; i < (int)ARRAY_SIZE(lines); ++i) {
|
||||||
p = lines[i];
|
p = lines[i];
|
||||||
|
|
||||||
if (sponsor != 0) {
|
if (sponsor != 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user