From 9cb32daff2f730dfc225b3a6207727c670862d2a Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Wed, 15 Jan 2025 20:09:32 +0100 Subject: [PATCH] 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. (cherry picked from commit e092aabf714717dd135e5767a49b78c428e49878) --- admin/coccinelle/arrayelts.cocci | 21 +++++++++++++++++++++ src/comp.c | 2 +- src/msdos.c | 4 ++-- src/sysdep.c | 4 ++-- src/w32fns.c | 8 +++----- src/w32term.c | 8 ++++---- 6 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 admin/coccinelle/arrayelts.cocci diff --git a/admin/coccinelle/arrayelts.cocci b/admin/coccinelle/arrayelts.cocci new file mode 100644 index 00000000000..5376a94bd85 --- /dev/null +++ b/admin/coccinelle/arrayelts.cocci @@ -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) diff --git a/src/comp.c b/src/comp.c index 97c7ea2efac..2603a2f4334 100644 --- a/src/comp.c +++ b/src/comp.c @@ -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); } diff --git a/src/msdos.c b/src/msdos.c index 6ee35b9e853..63a5400bc7d 100644 --- a/src/msdos.c +++ b/src/msdos.c @@ -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; diff --git a/src/sysdep.c b/src/sysdep.c index 188b3c3958a..3d9c49d9280 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -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; diff --git a/src/w32fns.c b/src/w32fns.c index c7963d2c616..452740f46ca 100644 --- a/src/w32fns.c +++ b/src/w32fns.c @@ -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; diff --git a/src/w32term.c b/src/w32term.c index c81779b8517..cb7bc7e4540 100644 --- a/src/w32term.c +++ b/src/w32term.c @@ -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; -- 2.39.5