]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Add ARRAY_SIZE() macro
authorJohan Malm <jgm323@gmail.com>
Sat, 16 Sep 2023 21:25:41 +0000 (22:25 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Tue, 19 Sep 2023 21:03:59 +0000 (22:03 +0100)
include/common/array-size.h [new file with mode: 0644]
include/ssd-internal.h
scripts/checkpatch.pl
src/cursor.c
src/layers.c
src/output.c
src/ssd/ssd_titlebar.c
src/theme.c

diff --git a/include/common/array-size.h b/include/common/array-size.h
new file mode 100644 (file)
index 0000000..6744394
--- /dev/null
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef LABWC_ARRAY_SIZE_H
+#define LABWC_ARRAY_SIZE_H
+
+/**
+ * ARRAY_SIZE() - Get the number of elements in array.
+ * @arr: array to be sized
+ *
+ * This does not work on pointers.
+ *
+ * Recent versions of GCC and clang support -Werror=sizeof-pointer-div
+ * and thus avoids using constructs such as:
+ *
+ * #define same_type(a, b) (__builtin_types_compatible_p(typeof(a), typeof(b)) == 1)
+ * #define ARRAY_SIZE(a) ({ static_assert(!same_type(a, &(a)[0])); sizeof(a) / sizeof(a[0]); })
+ */
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+#endif /* LABWC_ARRAY_SIZE_H */
index 148337edad92952484bdb15c64575d3d1c28981a..9fe0ebfb68a7360882452927aed3849690057102 100644 (file)
@@ -3,13 +3,14 @@
 #define LABWC_SSD_INTERNAL_H
 
 #include <wlr/util/box.h>
+#include "common/array-size.h"
 #include "ssd.h"
 
 #define FOR_EACH(tmp, ...) \
 { \
        __typeof__(tmp) _x[] = { __VA_ARGS__, NULL }; \
        size_t _i = 0; \
-       for ((tmp) = _x[_i]; _i < sizeof(_x) / sizeof(_x[0]) - 1; (tmp) = _x[++_i])
+       for ((tmp) = _x[_i]; _i < ARRAY_SIZE(_x) - 1; (tmp) = _x[++_i])
 
 #define FOR_EACH_END }
 
index 72b1fba4aa82ada9c87295466850bb2ab5a40fcd..e6b701f1145146cd849fcdd2a764898e9ad1ff89 100755 (executable)
@@ -62,7 +62,6 @@ my @ignore = (
        "OPEN_ENDED_LINE",
        "MACRO_ARG_REUSE",
        "PREFER_FALLTHROUGH",
-       "ARRAY_SIZE",
        "INITIALISED_STATIC",
        "UNNECESSARY_ELSE",
 );
index f6f8b1d70cfbeb5f44465fb4019621e17798f850..ce102502c1064e5fdbfc62fab0b5dbcea5546155 100644 (file)
@@ -7,6 +7,7 @@
 #include <wlr/types/wlr_primary_selection.h>
 #include <wlr/util/region.h>
 #include "action.h"
+#include "common/array-size.h"
 #include "common/mem.h"
 #include "common/scene-helpers.h"
 #include "config/mousebind.h"
@@ -51,14 +52,12 @@ static const char * const cursors_x11[] = {
        "left_side"
 };
 
-#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
 static_assert(
        ARRAY_SIZE(cursors_xdg) == LAB_CURSOR_COUNT,
        "XDG cursor names are out of sync");
 static_assert(
        ARRAY_SIZE(cursors_x11) == LAB_CURSOR_COUNT,
        "X11 cursor names are out of sync");
-#undef ARRAY_SIZE
 
 enum lab_cursors
 cursor_get_from_edge(uint32_t resize_edges)
index 2d750f22b840d79a5f9c67712c65c21890510a2f..b6ae4bb0e9afe30208b5051222b43cf97eab68d3 100644 (file)
@@ -14,6 +14,7 @@
 #include <wayland-server.h>
 #include <wlr/types/wlr_layer_shell_v1.h>
 #include <wlr/util/log.h>
+#include "common/array-size.h"
 #include "common/list.h"
 #include "common/mem.h"
 #include "config/rcxml.h"
@@ -76,8 +77,7 @@ layers_arrange(struct output *output)
                return;
        }
 
-       int nr_layers = sizeof(output->layer_tree) / sizeof(output->layer_tree[0]);
-       for (int i = 0; i < nr_layers; i++) {
+       for (size_t i = 0; i < ARRAY_SIZE(output->layer_tree); i++) {
                struct wlr_scene_tree *layer = output->layer_tree[i];
 
                /*
index 5db45963c9cb0d429a2a9f87294ff6aa7e497da3..1677995b23f5d13050e04796b296abcb1177f4c1 100644 (file)
@@ -16,6 +16,7 @@
 #include <wlr/types/wlr_scene.h>
 #include <wlr/util/region.h>
 #include <wlr/util/log.h>
+#include "common/array-size.h"
 #include "common/mem.h"
 #include "labwc.h"
 #include "layers.h"
@@ -48,8 +49,7 @@ output_destroy_notify(struct wl_listener *listener, void *data)
        wl_list_remove(&output->frame.link);
        wl_list_remove(&output->destroy.link);
 
-       int nr_layers = sizeof(output->layer_tree) / sizeof(output->layer_tree[0]);
-       for (int i = 0; i < nr_layers; i++) {
+       for (size_t i = 0; i < ARRAY_SIZE(output->layer_tree); i++) {
                wlr_scene_node_destroy(&output->layer_tree[i]->node);
        }
        wlr_scene_node_destroy(&output->layer_popup_tree->node);
@@ -186,8 +186,7 @@ new_output_notify(struct wl_listener *listener, void *data)
         * Create layer-trees (background, bottom, top and overlay) and
         * a layer-popup-tree.
         */
-       int nr_layers = sizeof(output->layer_tree) / sizeof(output->layer_tree[0]);
-       for (int i = 0; i < nr_layers; i++) {
+       for (size_t i = 0; i < ARRAY_SIZE(output->layer_tree); i++) {
                output->layer_tree[i] =
                        wlr_scene_tree_create(&server->scene->tree);
                node_descriptor_create(&output->layer_tree[i]->node,
index e66949fd4480f425035434e1fff175cd6a79a149..b0aaa2d0ac3918b6b46e8b3f7a5719bff6b3a78a 100644 (file)
@@ -105,7 +105,7 @@ set_squared_corners(struct ssd *ssd, bool enable)
        enum ssd_part_type ssd_type[2] = { LAB_SSD_BUTTON_WINDOW_MENU, LAB_SSD_BUTTON_CLOSE };
 
        FOR_EACH_STATE(ssd, subtree) {
-               for (size_t i = 0; i < sizeof(ssd_type) / sizeof(ssd_type[0]); i++) {
+               for (size_t i = 0; i < ARRAY_SIZE(ssd_type); i++) {
                        part = ssd_get_part(&subtree->parts, ssd_type[i]);
                        struct ssd_button *button = node_ssd_button_from_node(part->node);
 
index fe2c754b74c9b3e51ad5147e4045c0cc2a1d8b97..37dc803751ed2345857393be3c9508db4f177e81 100644 (file)
@@ -19,6 +19,7 @@
 #include <wlr/util/box.h>
 #include <wlr/util/log.h>
 #include <strings.h>
+#include "common/array-size.h"
 #include "common/dir.h"
 #include "common/font.h"
 #include "common/graphic-helpers.h"
@@ -109,7 +110,7 @@ load_buttons(struct theme *theme)
        };
 
        char filename[4096] = {0};
-       for (size_t i = 0; i < sizeof(buttons) / sizeof(buttons[0]); ++i) {
+       for (size_t i = 0; i < ARRAY_SIZE(buttons); ++i) {
                struct button *b = &buttons[i];
 
                drop(b->active.buffer);