Prefer 'ARRAYELTS (x)' to 'sizeof x / sizeof *x'

* src/comp.c (emit_limple_insn):
* src/msdos.c (dos_set_keyboard, dos_rawgetc):
* src/sysdep.c (convert_speed, list_system_processes):
* src/w32fns.c (deliver_wm_chars, Fx_file_dialog):
* src/w32term.c (record_event, w32_read_socket):
Prefer 'ARRAYELTS (x)' to 'sizeof x / sizeof *x'.

* admin/coccinelle/arrayelts.cocci: New file.
This commit is contained in:
Stefan Kangas 2025-01-15 20:09:32 +01:00
parent 16c89c5ae5
commit e092aabf71
6 changed files with 33 additions and 14 deletions

View file

@ -0,0 +1,21 @@
// Use the ARRAYELTS macro where possible.
@@
type T;
T[] E;
@@
- (sizeof (E) / sizeof (E[...]))
+ ARRAYELTS (E)
@@
type T;
T[] E;
@@
- (sizeof (E) / sizeof (T))
+ ARRAYELTS (E)
@@
type T;
T[] E;
@@
- (sizeof (E) / sizeof (*E))
+ ARRAYELTS (E)

View file

@ -2279,7 +2279,7 @@ emit_limple_insn (Lisp_Object insn)
ptrdiff_t i = 0;
FOR_EACH_TAIL (p)
{
if (i == sizeof (arg) / sizeof (Lisp_Object))
if (i == ARRAYELTS (arg))
break;
arg[i++] = XCAR (p);
}

View file

@ -2069,7 +2069,7 @@ dos_set_keyboard (int code, int always)
keyboard_map_all = always;
dos_keyboard_layout = 1;
for (i = 0; i < (sizeof (keyboard_layout_list)/sizeof (struct keyboard_layout_list)); i++)
for (i = 0; i < ARRAYELTS (keyboard_layout_list); i++)
if (code == keyboard_layout_list[i].country_code)
{
keyboard = keyboard_layout_list[i].keyboard_map;
@ -2512,7 +2512,7 @@ dos_rawgetc (void)
one. */
if (code == -1)
{
if (sc >= (sizeof (ibmpc_translate_map) / sizeof (short)))
if (sc >= ARRAYELTS (ibmpc_translate_map))
continue;
if ((code = ibmpc_translate_map[sc]) == Ignore)
continue;

View file

@ -3160,7 +3160,7 @@ static const struct speed_struct speeds[] =
static speed_t
convert_speed (speed_t speed)
{
for (size_t i = 0; i < sizeof speeds / sizeof speeds[0]; i++)
for (size_t i = 0; i < ARRAYELTS (speeds); i++)
{
if (speed == speeds[i].internal)
return speed;
@ -3380,7 +3380,7 @@ list_system_processes (void)
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PROC};
#endif
size_t len;
size_t mibsize = sizeof mib / sizeof mib[0];
size_t mibsize = ARRAYELTS (mib);
struct kinfo_proc *procs;
size_t i;

View file

@ -4154,7 +4154,7 @@ deliver_wm_chars (int do_translate, HWND hwnd, UINT msg, UINT wParam,
windows_msg.time = GetMessageTime ();
TranslateMessage (&windows_msg);
}
count = get_wm_chars (hwnd, buf, sizeof (buf)/sizeof (*buf), 1,
count = get_wm_chars (hwnd, buf, ARRAYELTS (buf), 1,
/* The message may have been synthesized by
who knows what; be conservative. */
modifier_set (VK_LCONTROL)
@ -8379,8 +8379,7 @@ DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
file_details_w->lStructSize = sizeof (*file_details_w);
/* Set up the inout parameter for the selected file name. */
file_details_w->lpstrFile = filename_buf_w;
file_details_w->nMaxFile =
sizeof (filename_buf_w) / sizeof (*filename_buf_w);
file_details_w->nMaxFile = ARRAYELTS (filename_buf_w);
file_details_w->hwndOwner = FRAME_W32_WINDOW (f);
/* Undocumented Bug in Common File Dialog:
If a filter is not specified, shell links are not resolved. */
@ -8413,8 +8412,7 @@ DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
else
file_details_a->lStructSize = sizeof (*file_details_a);
file_details_a->lpstrFile = filename_buf_a;
file_details_a->nMaxFile =
sizeof (filename_buf_a) / sizeof (*filename_buf_a);
file_details_a->nMaxFile = ARRAYELTS (filename_buf_a);
file_details_a->hwndOwner = FRAME_W32_WINDOW (f);
file_details_a->lpstrFilter = filter_a;
file_details_a->lpstrInitialDir = dir_a;

View file

@ -276,7 +276,7 @@ int event_record_index;
record_event (char *locus, int type)
{
if (event_record_index == sizeof (event_record) / sizeof (struct record))
if (event_record_index == ARRAYELTS (event_record))
event_record_index = 0;
event_record[event_record_index].locus = locus;
@ -5259,7 +5259,7 @@ w32_read_socket (struct terminal *terminal,
hlinfo->mouse_face_hidden = true;
}
if (temp_index == sizeof temp_buffer / sizeof (short))
if (temp_index == ARRAYELTS (temp_buffer))
temp_index = 0;
temp_buffer[temp_index++] = msg.msg.wParam;
inev.kind = NON_ASCII_KEYSTROKE_EVENT;
@ -5285,7 +5285,7 @@ w32_read_socket (struct terminal *terminal,
hlinfo->mouse_face_hidden = true;
}
if (temp_index == sizeof temp_buffer / sizeof (short))
if (temp_index == ARRAYELTS (temp_buffer))
temp_index = 0;
temp_buffer[temp_index++] = msg.msg.wParam;
@ -5400,7 +5400,7 @@ w32_read_socket (struct terminal *terminal,
hlinfo->mouse_face_hidden = true;
}
if (temp_index == sizeof temp_buffer / sizeof (short))
if (temp_index == ARRAYELTS (temp_buffer))
temp_index = 0;
temp_buffer[temp_index++] = msg.msg.wParam;
inev.kind = MULTIMEDIA_KEY_EVENT;