diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.c | 4 | ||||
-rw-r--r-- | src/nvim/ex_cmds.c | 2 | ||||
-rw-r--r-- | src/nvim/ex_docmd.c | 8 | ||||
-rw-r--r-- | src/nvim/ex_getln.c | 4 | ||||
-rw-r--r-- | src/nvim/indent_c.c | 2 | ||||
-rw-r--r-- | src/nvim/keymap.c | 3 | ||||
-rw-r--r-- | src/nvim/macros.h | 8 | ||||
-rw-r--r-- | src/nvim/mbyte.c | 2 | ||||
-rw-r--r-- | src/nvim/option.c | 8 | ||||
-rw-r--r-- | src/nvim/regexp.c | 2 | ||||
-rw-r--r-- | src/nvim/syntax.c | 6 | ||||
-rw-r--r-- | src/nvim/version.c | 4 |
12 files changed, 30 insertions, 23 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 71cb83e566..caba4464df 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -6652,7 +6652,7 @@ char_u *get_function_name(expand_T *xp, int idx) if (name != NULL) return name; } - if (++intidx < (int)(sizeof(functions) / sizeof(struct fst))) { + if (++intidx < (int)ARRAY_SIZE(functions)) { STRCPY(IObuff, functions[intidx].f_name); STRCAT(IObuff, "("); if (functions[intidx].f_max_argc == 0) @@ -6695,7 +6695,7 @@ find_internal_func ( ) { 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. diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 049934d680..d849d87b19 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -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 '*' * 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) { STRCPY(d, rtable[i]); break; diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 65a0017e20..1e63210394 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -2281,7 +2281,7 @@ int modifier_len(char_u *cmd) if (VIM_ISDIGIT(*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) if (p[j] != cmdmods[i].name[j]) break; @@ -2306,7 +2306,7 @@ int cmd_exists(char_u *name) char_u *p; /* 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) if (name[j] != cmdmods[i].name[j]) break; @@ -4974,7 +4974,7 @@ char_u *get_user_cmd_flags(expand_T *xp, int idx) {"bang", "bar", "buffer", "complete", "count", "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 (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", "*", "?", "+"}; - if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))) + if (idx >= (int)ARRAY_SIZE(user_cmd_nargs)) return NULL; return (char_u *)user_cmd_nargs[idx]; } diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index aed0484356..a19cb36d12 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -3715,7 +3715,7 @@ ExpandFromContext ( * right function to do the expansion. */ 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 (tab[i].ic) { 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 }; char *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) { compl[0] = (char_u)short_names[idx]; diff --git a/src/nvim/indent_c.c b/src/nvim/indent_c.c index 6648a9f7c6..d6101357e3 100644 --- a/src/nvim/indent_c.c +++ b/src/nvim/indent_c.c @@ -303,7 +303,7 @@ static int cin_isinit(void) for (;; ) { 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]); if (cin_starts_with(s, skip[i])) { s = cin_skipcomment(s + l); diff --git a/src/nvim/keymap.c b/src/nvim/keymap.c index 45977b96ae..9c5160ffb2 100644 --- a/src/nvim/keymap.c +++ b/src/nvim/keymap.c @@ -284,8 +284,7 @@ static struct key_name_entry { {0, NULL} }; -#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / \ - sizeof(struct key_name_entry)) +#define KEY_NAMES_TABLE_LEN ARRAY_SIZE(key_names_table) static struct mousetable { int pseudo_code; /* Code for pseudo mouse event */ diff --git a/src/nvim/macros.h b/src/nvim/macros.h index 215ad3a1f7..7dd8120d09 100644 --- a/src/nvim/macros.h +++ b/src/nvim/macros.h @@ -152,4 +152,12 @@ # 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 diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index db4516527a..08d9e50208 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -2138,7 +2138,7 @@ int utf_class(int c) {0x2f800, 0x2fa1f, 0x4e00}, /* CJK Ideographs */ }; int bot = 0; - int top = sizeof(classes) / sizeof(struct clinterval) - 1; + int top = ARRAY_SIZE(classes) - 1; int mid; /* First quick check for Latin1 characters, use 'iskeyword'. */ diff --git a/src/nvim/option.c b/src/nvim/option.c index 2b3c87511e..fe2c756c01 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1883,7 +1883,7 @@ void set_init_1(void) int mustfree; 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; # ifdef UNIX if (*names[n] == NUL) @@ -4723,10 +4723,10 @@ static char_u *set_chars_option(char_u **varp) if (varp == &p_lcs) { tab = lcstab; - entries = sizeof(lcstab) / sizeof(struct charstab); + entries = ARRAY_SIZE(lcstab); } else { tab = filltab; - entries = sizeof(filltab) / sizeof(struct charstab); + entries = ARRAY_SIZE(filltab); } /* 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) { regmatch->rm_ic = ic; 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) if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0)) { if (loop == 0) diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index 69c2119697..dd7af63ce0 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -602,7 +602,7 @@ static int get_char_class(char_u **pp) int i; 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) { *pp += STRLEN(class_names[i]) + 2; return i; diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index 3deda0a8c9..6787ca8080 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -3943,7 +3943,7 @@ get_syn_options ( if (strchr(first_letters, *arg) == NULL) break; - for (fidx = sizeof(flagtab) / sizeof(struct flag); --fidx >= 0; ) { + for (fidx = ARRAY_SIZE(flagtab); --fidx >= 0; ) { p = flagtab[fidx].name; int i; for (i = 0, len = 0; p[i] != NUL; i += 2, ++len) @@ -6295,7 +6295,7 @@ do_highlight ( attr = 0; off = 0; 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]); if (STRNICMP(arg + off, hl_name_table[i], len) == 0) { attr |= hl_attr_table[i]; @@ -6416,7 +6416,7 @@ do_highlight ( /* reduce calls to STRICMP a bit, it can be slow */ 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] && STRICMP(arg + 1, color_names[i] + 1) == 0) break; diff --git a/src/nvim/version.c b/src/nvim/version.c index de4f6ffd87..43d05e968f 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -986,7 +986,7 @@ void intro_message(int colon) }; // 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'. if (p_ls > 1) { @@ -1006,7 +1006,7 @@ void intro_message(int colon) row = blanklines / 2; 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]; if (sponsor != 0) { |