aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/macros.h
diff options
context:
space:
mode:
authorFelipe Oliveira Carvalho <felipekde@gmail.com>2014-12-13 11:10:19 -0300
committerFelipe Oliveira Carvalho <felipekde@gmail.com>2014-12-18 15:41:37 -0300
commit3c857900fe9a7eff87aa4fbe6e7b80ba602ad2bd (patch)
tree01a9c192b058d769e4b2422cba8930a7f67a0cab /src/nvim/macros.h
parentbd19cc4f8fc3d02a030caba911a1af008585f688 (diff)
downloadrneovim-3c857900fe9a7eff87aa4fbe6e7b80ba602ad2bd.tar.gz
rneovim-3c857900fe9a7eff87aa4fbe6e7b80ba602ad2bd.tar.bz2
rneovim-3c857900fe9a7eff87aa4fbe6e7b80ba602ad2bd.zip
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
Diffstat (limited to 'src/nvim/macros.h')
-rw-r--r--src/nvim/macros.h8
1 files changed, 8 insertions, 0 deletions
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