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:
Felipe Oliveira Carvalho
2014-12-13 11:10:19 -03:00
parent bd19cc4f8f
commit 3c857900fe
12 changed files with 30 additions and 23 deletions

View File

@@ -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];
}