Skip to contentSkip to conversation

use ARRAY_SIZE() when possible

PATCH In review Namhyung Kim 3 participants 4 messages Dec 9, 2010 – Dec 10, 2010 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 use ARRAY_SIZE() when possible

Convert (sizeof arr / sizeof arr[0]) to ARRAY_SIZE(arr).

---
 lib.c         |    8 ++++----
 pre-process.c |    4 ++--
 show-parse.c  |    4 ++--
 3 files changed, 8 insertions(+), 8 deletions(-)
lib.c +4 -4
index c4a6f87..1d96129 100644
@@ -437,7 +437,7 @@ static char **handle_onoff_switch(char *arg, char **next, const struct warning w
 
 static char **handle_switch_W(char *arg, char **next)
 {
-	char ** ret = handle_onoff_switch(arg, next, warnings, sizeof warnings/sizeof warnings[0]);
+	char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
 	if (ret)
 		return ret;
 
@@ -453,7 +453,7 @@ static struct warning debugs[] = {
 
 static char **handle_switch_v(char *arg, char **next)
 {
-	char ** ret = handle_onoff_switch(arg, next, debugs, sizeof debugs/sizeof debugs[0]);
+	char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
 	if (ret)
 		return ret;
 
@@ -477,7 +477,7 @@ static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
 
 static void handle_switch_W_finalize(void)
 {
-	handle_onoff_switch_finalize(warnings, sizeof(warnings) / sizeof(warnings[0]));
+	handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
 
 	/* default Wdeclarationafterstatement based on the C dialect */
 	if (-1 == Wdeclarationafterstatement)
@@ -504,7 +504,7 @@ static void handle_switch_W_finalize(void)
 
 static void handle_switch_v_finalize(void)
 {
-	handle_onoff_switch_finalize(debugs, sizeof(debugs) / sizeof(debugs[0]));
+	handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
 }
 
 static char **handle_switch_U(char *arg, char **next)
pre-process.c +2 -2
index 656acaa..603cc00 100644
@@ -1718,13 +1718,13 @@ static void init_preprocessor(void)
 		{ "elif",	handle_elif },
 	};
 
-	for (i = 0; i < (sizeof (normal) / sizeof (normal[0])); i++) {
+	for (i = 0; i < ARRAY_SIZE(normal); i++) {
 		struct symbol *sym;
 		sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
 		sym->handler = normal[i].handler;
 		sym->normal = 1;
 	}
-	for (i = 0; i < (sizeof (special) / sizeof (special[0])); i++) {
+	for (i = 0; i < ARRAY_SIZE(special); i++) {
 		struct symbol *sym;
 		sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
 		sym->handler = special[i].handler;
show-parse.c +2 -2
index c97debe..73cc86a 100644
@@ -239,7 +239,7 @@ const char *builtin_typename(struct symbol *sym)
 {
 	int i;
 
-	for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++)
+	for (i = 0; i < ARRAY_SIZE(typenames); i++)
 		if (typenames[i].sym == sym)
 			return typenames[i].name;
 	return NULL;
@@ -249,7 +249,7 @@ const char *builtin_ctypename(struct ctype *ctype)
 {
 	int i;
 
-	for (i = 0; i < sizeof(typenames)/sizeof(typenames[0]); i++)
+	for (i = 0; i < ARRAY_SIZE(typenames); i++)
 		if (&typenames[i].sym->ctype == ctype)
 			return typenames[i].name;
 	return NULL;
Signature · 1 line
1.7.2.2
#1 lore raw
Quoted 2 lines · Namhyung Kim

Convert (sizeof arr / sizeof arr[0]) to ARRAY_SIZE(arr).

would that not be a cadidate for coccinelle/spatch rather than a potentially error prone and repetitive manual patch?
array.cocci from the current 0.4.2 coccinelle distribution below should du the job.
Might make sense to add a coccinelle directory in sparse to handle such cases ?

hofrat

// Use the macro ARRAY_SIZE when possible
//
// Confidence: High
// Copyright: (C) Gilles Muller, Julia Lawall, EMN, INRIA, DIKU. GPLv2. // URL: http://coccinelle.lip6.fr/rules/array.html
// Options: -I ... -all_includes can give more complete results virtual org
virtual patch

@i@
@@

#include <linux/kernel.h>

/////////////////////////////////////
/////////////////////////////////////
@depends on i && patch && !org@
type T;
T[] E;
@@

- (sizeof(E)/sizeof(*E))
+ ARRAY_SIZE(E)

@depends on i && patch && !org@
type T;
T[] E;
@@

- (sizeof(E)/sizeof(E[...]))
+ ARRAY_SIZE(E)

@depends on i && patch && !org@
type T;
T[] E;
@@

- (sizeof(E)/sizeof(T))
+ ARRAY_SIZE(E)

@n_patch depends on patch && !org@
identifier AS,E;
@@

- #define AS(E) ARRAY_SIZE(E)

@ depends on patch && !org@
expression E;
identifier n_patch.AS;
@@

- AS(E)
+ ARRAY_SIZE(E)

/////////////////////////////////////
/////////////////////////////////////
@arr_ptr depends on i && !patch && org@
type T;
T[] E;
position p;
@@

(sizeof(E@p)/sizeof(*E))

@arr_tab depends on i && !patch && org@
type T;
T[] E;
position p;
@@

(sizeof(E@p)/sizeof(E[...]))

@arr_typ depends on i && !patch && org@
type T;
T[] E;
position p;
@@

(sizeof(E@p)/sizeof(T))

@n_org depends on !patch && org@
identifier AS,E;
@@

#define AS(E) ARRAY_SIZE(E)

@arr_def depends on !patch && org@
expression E;
identifier n_org.AS;
position p;
@@

AS@p(E)

@script:python@
p << arr_ptr.p;
e << arr_ptr.E;
@@
cocci.print_main(e,p)

@script:python@
p << arr_tab.p;
e << arr_tab.E;
@@
cocci.print_main(e,p)

@script:python@
p << arr_typ.p;
e << arr_typ.E;
@@
cocci.print_main(e,p)

@script:python@
p << arr_def.p;
e << arr_def.E;
@@
cocci.print_main(e,p)

maintainer
#2 lore raw
Quoted 10 lines · Nicholas Mc Guire
Quoted 2 lines · Namhyung Kim

Convert (sizeof arr / sizeof arr[0]) to ARRAY_SIZE(arr).

would that not be a cadidate for coccinelle/spatch rather than a potentially error prone and repetitive manual patch?
array.cocci from the current 0.4.2 coccinelle distribution below should du the job.
Might make sense to add a coccinelle directory in sparse to handle such cases ?

Interesting. How do I use the provided script? I assume I need to invoke coccinelle some how.

Chris