util: auto-declare the element variable in ARRAY_FOR_EACH
authorPeter Hutterer <peter.hutterer@who-t.net>
Mon, 7 Mar 2022 04:44:21 +0000 (14:44 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Wed, 9 Mar 2022 00:16:07 +0000 (10:16 +1000)
All cases we have in our code base have an otherwise unused variable to
loop through the array. Let's auto-declare this as part of the loop.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
18 files changed:
src/evdev-mt-touchpad.c
src/evdev-tablet-pad.c
src/evdev-tablet.c
src/quirks.c
src/util-macros.h
src/util-prop-parsers.c
src/util-time.h
test/litest.c
test/test-device.c
test/test-keyboard.c
test/test-quirks.c
test/test-touchpad.c
test/test-utils.c
tools/libinput-debug-gui.c
tools/libinput-debug-tablet.c
tools/libinput-record.c
udev/libinput-fuzz-extract.c
udev/libinput-fuzz-to-zero.c

index 4386bdad5f335a580b551dbae41d6fb3513b1bed..ea9c8f1e91553682950c93512237941846d3fe25 100644 (file)
@@ -2885,7 +2885,6 @@ tp_init_slots(struct tp_dispatch *tp,
                { BTN_TOOL_TRIPLETAP, 3 },
                { BTN_TOOL_DOUBLETAP, 2 },
        };
-       struct map *m;
        unsigned int i, n_btn_tool_touches = 1;
 
        absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
index fdce73222a868ede379b33c1c88e63a26cabaa3e..991c2851aec5f50809def7cebe87fbd5e6e06020 100644 (file)
@@ -642,7 +642,6 @@ pad_init_keys(struct pad_dispatch *pad, struct evdev_device *device)
                KEY_ONSCREEN_KEYBOARD,
                KEY_CONTROLPANEL,
        };
-       unsigned int *code;
 
        /* Wacom's keys are the only ones we know anything about */
        if (libevdev_get_id_vendor(device->evdev) != VENDOR_ID_WACOM)
index 25bb57d6dde6a0a57c48f594044b129b47a82e06..58cc46d8dc80483112ecd9382fa0fbe16ce19b59 100644 (file)
@@ -2026,7 +2026,6 @@ tablet_proximity_out_quirk_timer_func(uint64_t now, void *data)
                  .code = SYN_REPORT,
                  .value = 0 },
        };
-       struct input_event *e;
 
        if (tablet_has_status(tablet, TABLET_TOOL_IN_CONTACT) ||
            tablet_has_status(tablet, TABLET_BUTTONS_DOWN)) {
index bccbaa24e83e69a07474401a79d598f231230bd7..9c5efaa6b69ae08d8dc0d8c5128358dbfab9cf08 100644 (file)
@@ -1338,7 +1338,6 @@ match_fill_udev_type(struct match *m,
                { "ID_INPUT_KEYBOARD", UDEV_KEYBOARD },
                { "ID_INPUT_KEY", UDEV_KEYBOARD },
        };
-       struct ut_map *map;
 
        ARRAY_FOR_EACH(mappings, map) {
                if (udev_prop(device, map->prop))
index 978d0f393579c18a12bac6e59a67d044dc363c28..004b0ccfcbd08ace7320b0b8802b1b5b631e8231 100644 (file)
 #include "config.h"
 
 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
+/**
+ * Iterate through the array _arr, assigning the variable elem to each
+ * element. elem only exists within the loop.
+ */
 #define ARRAY_FOR_EACH(_arr, _elem) \
-       for (size_t _i = 0; _i < ARRAY_LENGTH(_arr) && (_elem = &_arr[_i]); _i++)
+       for (__typeof__((_arr)[0]) *_elem = _arr; \
+            _elem < (_arr) + ARRAY_LENGTH(_arr); \
+            _elem++)
 
 #define min(a, b) (((a) < (b)) ? (a) : (b))
 #define max(a, b) (((a) > (b)) ? (a) : (b))
index 62902520f95a124f1c0a01b1bc945027b1cfc2bb..598e40c9e47315518865efd81991e94461bdbcd2 100644 (file)
@@ -321,7 +321,6 @@ parse_evcode_string(const char *s, int *type_out, int *code_out)
                        { "REL_", EV_REL },
                        { "SW_", EV_SW },
                };
-               struct map *m;
                bool found = false;
 
                ARRAY_FOR_EACH(map, m) {
index c239e9935a4f90729c1a346f695eb6fe65290444..a5ac4b5714205ab9c02cce45a755923fd7862016 100644 (file)
@@ -116,7 +116,6 @@ to_human_time(uint64_t us)
                {"h", 60, 48},
                {"d", 24, ~0},
        };
-       struct c *c;
        uint64_t value = us;
 
        ARRAY_FOR_EACH(conversion, c) {
index 4f4739d27f25e38b39249878425bb7220a9c747a..54b15bef54c6a87df156269f576fb7a56d571742 100644 (file)
@@ -2734,7 +2734,6 @@ litest_button_click(struct litest_device *d,
                    unsigned int button,
                    bool is_press)
 {
-       struct input_event *ev;
        struct input_event click[] = {
                { .type = EV_KEY, .code = button, .value = is_press ? 1 : 0 },
                { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
@@ -2803,7 +2802,6 @@ litest_button_scroll_locked(struct litest_device *dev,
 void
 litest_keyboard_key(struct litest_device *d, unsigned int key, bool is_press)
 {
-       struct input_event *ev;
        struct input_event click[] = {
                { .type = EV_KEY, .code = key, .value = is_press ? 1 : 0 },
                { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
index 8c1679d58bd221361b3712490fe4dc88ea025ade..3b153436d959fa8a79147abe503f0b0e0a47b38b 100644 (file)
@@ -1448,7 +1448,7 @@ debug_log_handler(struct libinput *libinput,
                  const char *format,
                  va_list args)
 {
-       char *message, **dmsg;
+       char *message;
        int n;
 
        if (priority != LIBINPUT_LOG_PRIORITY_DEBUG)
@@ -1542,7 +1542,6 @@ START_TEST(device_capability_at_least_one)
                LIBINPUT_DEVICE_CAP_GESTURE,
                LIBINPUT_DEVICE_CAP_SWITCH,
        };
-       enum libinput_device_capability *cap;
        int ncaps = 0;
 
        ARRAY_FOR_EACH(caps, cap) {
index 55c5c3655bcb333a28c224d63ac3d09ab2dc0a85..0cb8a80dd1618a7d924e7cb9306d2cdd005fbe5e 100644 (file)
@@ -111,7 +111,6 @@ START_TEST(keyboard_ignore_no_pressed_release)
                EV_KEY, KEY_A,
                -1, -1,
        };
-       enum libinput_key_state *state;
        enum libinput_key_state expected_states[] = {
                LIBINPUT_KEY_STATE_PRESSED,
                LIBINPUT_KEY_STATE_RELEASED,
index ac6a68d7c6398743a1487d90703f878ae829ded9..dc9bcd09b9a396fc85f5ee10537365e7a70c9f43 100644 (file)
@@ -561,7 +561,6 @@ START_TEST(quirks_parse_vendor_invalid)
        "MatchVendor=123\n"
        "ModelAppleTouchpad=1\n",
        };
-       const char **qf;
 
        ARRAY_FOR_EACH(quirks_file, qf) {
                struct data_dir dd = make_data_dir(*qf);
@@ -622,7 +621,6 @@ START_TEST(quirks_parse_product_invalid)
        "MatchProduct=123\n"
        "ModelAppleTouchpad=1\n",
        };
-       const char **qf;
 
        ARRAY_FOR_EACH(quirks_file, qf) {
                struct data_dir dd = make_data_dir(*qf);
@@ -683,7 +681,6 @@ START_TEST(quirks_parse_version_invalid)
        "MatchVersion=123\n"
        "ModelAppleTouchpad=1\n",
        };
-       const char **qf;
 
        ARRAY_FOR_EACH(quirks_file, qf) {
                struct data_dir dd = make_data_dir(*qf);
@@ -747,7 +744,6 @@ START_TEST(quirks_parse_name_invalid)
        "MatchName=\n"
        "ModelAppleTouchpad=1\n",
        };
-       const char **qf;
 
        ARRAY_FOR_EACH(quirks_file, qf) {
                struct data_dir dd = make_data_dir(*qf);
@@ -821,7 +817,6 @@ START_TEST(quirks_parse_udev_invalid)
        "MatchUdevType=123\n"
        "ModelAppleTouchpad=1\n",
        };
-       const char **qf;
 
        ARRAY_FOR_EACH(quirks_file, qf) {
                struct data_dir dd = make_data_dir(*qf);
@@ -878,7 +873,6 @@ START_TEST(quirks_parse_dmi_invalid)
        "MatchDMIModalias=foo\n"
        "ModelAppleTouchpad=1\n",
        };
-       const char **qf;
 
        ARRAY_FOR_EACH(quirks_file, qf) {
                struct data_dir dd = make_data_dir(*qf);
@@ -965,7 +959,6 @@ START_TEST(quirks_parse_dimension_attr)
                QUIRK_ATTR_SIZE_HINT,
                QUIRK_ATTR_RESOLUTION_HINT,
        };
-       enum quirk *a;
        struct qtest_dim test_values[] = {
                { "10x10", true, 10, 10 },
                { "20x30", true, 20, 30 },
@@ -975,7 +968,6 @@ START_TEST(quirks_parse_dimension_attr)
                { "0x00", false, 0, 0 },
                { "0xa0", false, 0, 0 },
        };
-       struct qtest_dim *t;
 
        ARRAY_FOR_EACH(attrs, a) {
                ARRAY_FOR_EACH(test_values, t) {
@@ -1011,7 +1003,6 @@ START_TEST(quirks_parse_range_attr)
                QUIRK_ATTR_TOUCH_SIZE_RANGE,
                QUIRK_ATTR_PRESSURE_RANGE,
        };
-       enum quirk *a;
        struct qtest_range test_values[] = {
                { "20:10", true, 20, 10 },
                { "30:5", true, 30, 5 },
@@ -1029,7 +1020,6 @@ START_TEST(quirks_parse_range_attr)
                { "0xa0", false, 0, 0 },
                { "0x10:0x5", false, 0, 0 },
        };
-       struct qtest_range *t;
 
        ARRAY_FOR_EACH(attrs, a) {
                ARRAY_FOR_EACH(test_values, t) {
@@ -1066,7 +1056,6 @@ START_TEST(quirks_parse_uint_attr)
                QUIRK_ATTR_PALM_PRESSURE_THRESHOLD,
                QUIRK_ATTR_THUMB_PRESSURE_THRESHOLD,
        };
-       enum quirk *a;
        struct qtest_uint test_values[] = {
                { "10", true, 10 },
                { "0", true, 0 },
@@ -1078,7 +1067,6 @@ START_TEST(quirks_parse_uint_attr)
                { "0xab", false, 0 },
                { "ab", false, 0 },
        };
-       struct qtest_uint *t;
 
        ARRAY_FOR_EACH(attrs, a) {
                ARRAY_FOR_EACH(test_values, t) {
@@ -1112,7 +1100,6 @@ START_TEST(quirks_parse_double_attr)
        enum quirk attrs[] = {
                QUIRK_ATTR_TRACKPOINT_MULTIPLIER,
        };
-       enum quirk *a;
        struct qtest_double test_values[] = {
                { "10", true, 10.0 },
                { "10.0", true, 10.0 },
@@ -1132,7 +1119,6 @@ START_TEST(quirks_parse_double_attr)
                { "10:5", false, 0 },
                { "10x5", false, 0 },
        };
-       struct qtest_double *t;
 
        ARRAY_FOR_EACH(attrs, a) {
                ARRAY_FOR_EACH(test_values, t) {
@@ -1167,7 +1153,6 @@ START_TEST(quirks_parse_string_attr)
                QUIRK_ATTR_LID_SWITCH_RELIABILITY,
                QUIRK_ATTR_KEYBOARD_INTEGRATION,
        };
-       enum quirk *a;
        struct qtest_str test_values[] = {
                { "below", QUIRK_ATTR_TPKBCOMBO_LAYOUT },
                { "reliable", QUIRK_ATTR_LID_SWITCH_RELIABILITY },
@@ -1185,7 +1170,6 @@ START_TEST(quirks_parse_string_attr)
                { "0xa", 0 },
                { "0.0", 0 },
        };
-       struct qtest_str *t;
 
        ARRAY_FOR_EACH(attrs, a) {
                ARRAY_FOR_EACH(test_values, t) {
@@ -1219,7 +1203,6 @@ START_TEST(quirks_parse_bool_attr)
                QUIRK_ATTR_USE_VELOCITY_AVERAGING,
                QUIRK_ATTR_TABLET_SMOOTHING,
        };
-       enum quirk *a;
        struct qtest_bool test_values[] = {
                { "0", true, false },
                { "1", true, true },
@@ -1227,7 +1210,6 @@ START_TEST(quirks_parse_bool_attr)
                { "-1", false, false },
                { "a", false, false },
        };
-       struct qtest_bool *t;
 
        ARRAY_FOR_EACH(attrs, a) {
                ARRAY_FOR_EACH(test_values, t) {
index 161cfcaad4f81b479015036f6a8e05355a80b447..b55d979f09857f8964be88745f60942f4314f5a9 100644 (file)
@@ -3346,7 +3346,6 @@ START_TEST(touchpad_trackpoint_buttons)
                { BTN_1, BTN_RIGHT },
                { BTN_2, BTN_MIDDLE },
        };
-       const struct buttons *b;
 
        trackpoint = litest_add_device(li,
                                       LITEST_TRACKPOINT);
@@ -4197,7 +4196,6 @@ START_TEST(touchpad_dwt_modifier_no_dwt)
                KEY_RIGHTMETA,
                KEY_LEFTMETA,
        };
-       unsigned int *key;
 
        if (!has_disable_while_typing(touchpad))
                return;
@@ -4243,7 +4241,6 @@ START_TEST(touchpad_dwt_modifier_combo_no_dwt)
                KEY_RIGHTMETA,
                KEY_LEFTMETA,
        };
-       unsigned int *key;
 
        if (!has_disable_while_typing(touchpad))
                return;
@@ -4293,7 +4290,6 @@ START_TEST(touchpad_dwt_modifier_combo_dwt_after)
                KEY_RIGHTMETA,
                KEY_LEFTMETA,
        };
-       unsigned int *key;
 
        if (!has_disable_while_typing(touchpad))
                return;
@@ -4347,7 +4343,6 @@ START_TEST(touchpad_dwt_modifier_combo_dwt_remains)
                KEY_RIGHTMETA,
                KEY_LEFTMETA,
        };
-       unsigned int *key;
 
        if (!has_disable_while_typing(touchpad))
                return;
index 989adecd65db9bf52870a73994239d2e1bf82655..ca804a9437ea6961904a24236cb0647662cbafcc 100644 (file)
 
 #include "check-double-macros.h"
 
+START_TEST(array_for_each)
+{
+       int ai[6];
+       char ac[10];
+       struct as {
+               int a;
+               char b;
+               int *ptr;
+       } as[32];
+
+       for (size_t i = 0; i < 6; i++)
+               ai[i] = 20 + i;
+       for (size_t i = 0; i < 10; i++)
+               ac[i] = 100 + i;
+       for (size_t i = 0; i < 32; i++) {
+               as[i].a = 10 + i;
+               as[i].b = 20 + i;
+               as[i].ptr = (int*)0xab + i;
+       }
+
+       int iexpected = 20;
+       ARRAY_FOR_EACH(ai, entry) {
+               ck_assert_int_eq(*entry, iexpected);
+               ++iexpected;
+       }
+       ck_assert_int_eq(iexpected, 26);
+
+       int cexpected = 100;
+       ARRAY_FOR_EACH(ac, entry) {
+               ck_assert_int_eq(*entry, cexpected);
+               ++cexpected;
+       }
+       ck_assert_int_eq(cexpected, 110);
+
+       struct as sexpected = {
+               .a = 10,
+               .b = 20,
+               .ptr = (int*)0xab,
+       };
+       ARRAY_FOR_EACH(as, entry) {
+               ck_assert_int_eq(entry->a, sexpected.a);
+               ck_assert_int_eq(entry->b, sexpected.b);
+               ck_assert_ptr_eq(entry->ptr, sexpected.ptr);
+               ++sexpected.a;
+               ++sexpected.b;
+               ++sexpected.ptr;
+       }
+       ck_assert_int_eq(sexpected.a, 42);
+}
+END_TEST
+
 START_TEST(bitfield_helpers)
 {
        /* This value has a bit set on all of the word boundaries we want to
@@ -678,7 +729,6 @@ START_TEST(evdev_abs_parser)
                { .which = 0, .prop = ":asb::::" },
                { .which = 0, .prop = "foo" },
        };
-       struct test *t;
 
        ARRAY_FOR_EACH(tests, t) {
                struct input_absinfo abs;
@@ -1071,7 +1121,6 @@ START_TEST(strargv_test)
                { 1, {NULL, NULL}, 0 },
                { 3, {"hello", NULL, "World"}, 0 },
        };
-       struct argv_test *t;
 
        ARRAY_FOR_EACH(tests, t) {
                char **strv = strv_from_argv(t->argc, t->argv);
@@ -1414,7 +1463,6 @@ START_TEST(basename_test)
                { "/bar", "bar" },
                { "", NULL },
        };
-       struct test *t;
 
        ARRAY_FOR_EACH(tests, t) {
                const char *result = safe_basename(t->path);
@@ -1442,7 +1490,6 @@ START_TEST(trunkname_test)
                { "/bar", "bar" },
                { "", "" },
        };
-       struct test *t;
 
        ARRAY_FOR_EACH(tests, t) {
                char *result = trunkname(t->path);
@@ -1461,6 +1508,8 @@ litest_utils_suite(void)
        s = suite_create("litest:utils");
        tc = tcase_create("utils");
 
+       tcase_add_test(tc, array_for_each);
+
        tcase_add_test(tc, bitfield_helpers);
        tcase_add_test(tc, matrix_helpers);
        tcase_add_test(tc, ratelimit_helpers);
index 0ef661457e1c703dab1bd85ddc3819ced18be9e6..b8685c8cd4e83bd0d0d092e7e772882c4b13c4ba 100644 (file)
@@ -603,8 +603,6 @@ draw_scrollbars(struct window *w, cairo_t *cr)
 static inline void
 draw_touchpoints(struct window *w, cairo_t *cr)
 {
-       struct touch *t;
-
        cairo_save(cr);
        ARRAY_FOR_EACH(w->touches, t) {
                if (t->state == TOUCH_ACTIVE)
@@ -1155,7 +1153,6 @@ window_init(struct window *w)
 static void
 window_cleanup(struct window *w)
 {
-       struct libinput_device **dev;
        ARRAY_FOR_EACH(w->devices, dev) {
                if (*dev)
                        libinput_device_unref(*dev);
@@ -1165,8 +1162,6 @@ window_cleanup(struct window *w)
 static void
 change_ptraccel(struct window *w, double amount)
 {
-       struct libinput_device **dev;
-
        ARRAY_FOR_EACH(w->devices, dev) {
                double speed;
                enum libinput_config_status status;
@@ -1341,7 +1336,6 @@ static void
 handle_event_device_notify(struct libinput_event *ev)
 {
        struct libinput_device *dev = libinput_event_get_device(ev);
-       struct libinput_device **device;
        struct libinput *li;
        struct window *w;
        const char *type;
index 73948de2ecf010080e7289212962121c79e0d142..05d3b9a1760d1652d6874025753868289c8bcb97 100644 (file)
@@ -307,7 +307,6 @@ handle_tablet_button_event(struct context *ctx, struct libinput_event *ev)
 {
        struct libinput_event_tablet_tool *t = libinput_event_get_tablet_tool_event(ev);
        unsigned int button = libinput_event_tablet_tool_get_button(t);
-       unsigned int *btn;
        enum libinput_button_state state = libinput_event_tablet_tool_get_button_state(t);
 
        ARRAY_FOR_EACH(ctx->buttons_down, btn) {
index 7adebdf2e5775c9aef699304d7a9875742cc43fa..b31b6fe347ededbe9079d677c337a19a8f1137fe 100644 (file)
@@ -1820,7 +1820,6 @@ print_libinput_description(struct record_device *dev)
                {LIBINPUT_DEVICE_CAP_GESTURE, "gesture"},
                {LIBINPUT_DEVICE_CAP_SWITCH, "switch"},
        };
-       struct cap *cap;
        const char *sep = "";
 
        if (!device)
index 48ef79ce46294dd62136405724ccee4d91d112dd..6158e35743326e6880392cb734233acfbcfb99eb 100644 (file)
@@ -46,7 +46,6 @@ handle_absfuzz(struct udev_device *device)
        struct libevdev *evdev = NULL;
        int fd = -1;
        int rc;
-       unsigned int *code;
        unsigned int axes[] = {ABS_X,
                               ABS_Y,
                               ABS_MT_POSITION_X,
@@ -88,7 +87,6 @@ out:
 static void
 handle_evdev_abs(struct udev_device *device)
 {
-       unsigned int *code;
        unsigned int axes[] = {ABS_X,
                               ABS_Y,
                               ABS_MT_POSITION_X,
index a8767bed0c5501765bf2292ffd6090b191c33319..e2e9a0363dcb9f814c0c1acd60214e8f0a2fb2aa 100644 (file)
@@ -39,7 +39,6 @@ reset_absfuzz_to_zero(struct udev_device *device)
        struct libevdev *evdev = NULL;
        int fd = -1;
        int rc;
-       unsigned int *code;
        unsigned int axes[] = {ABS_X,
                               ABS_Y,
                               ABS_MT_POSITION_X,