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

@@ -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 */