]> git.eshelyaron.com Git - emacs.git/commitdiff
Prefer 'ARRAYELTS (x)' to 'sizeof x / sizeof *x'
authorStefan Kangas <stefankangas@gmail.com>
Wed, 15 Jan 2025 19:09:32 +0000 (20:09 +0100)
committerEshel Yaron <me@eshelyaron.com>
Fri, 17 Jan 2025 11:34:53 +0000 (12:34 +0100)
* 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 [new file with mode: 0644]
src/comp.c
src/msdos.c
src/sysdep.c
src/w32fns.c
src/w32term.c

diff --git a/admin/coccinelle/arrayelts.cocci b/admin/coccinelle/arrayelts.cocci
new file mode 100644 (file)
index 0000000..5376a94
--- /dev/null
@@ -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)
index 97c7ea2efac226867ec8bbf7146605b287c59607..2603a2f4334f87333233af8f3a097f5292522a51 100644 (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);
     }
index 6ee35b9e853115d5e0720891160ccb5832a77d11..63a5400bc7dc4e083754a1d3783e7319246e7852 100644 (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;
index 188b3c3958a93becf61ec5c4201fd4a099588c41..3d9c49d928044785d0cd0abaa4bd8b832918a69b 100644 (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;
 
index c7963d2c616aec7188d9aa628248e78f6d16c804..452740f46ca2e1c656b4b0f405e21f5bc696d968 100644 (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;
index c81779b8517f115bc1c05e7436ce7655602784d7..cb7bc7e454007b37f4765e34c7e8575f1b4c56e9 100644 (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;