summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2022-08-30 03:20:04 -0500
committerGitHub <noreply@github.com>2022-08-30 10:20:04 +0200
commit9632360caa5e6511b0ec13cb4c55eb64408232b5 (patch)
treedb4e0d47fb8d4918a3f7361647a4afca13da23e0
parent2c5aa98143fc1bb76299033042b374b2ce148d93 (diff)
Use a macro to compute the size of arrays at compile time (#18044)
* Add ARRAY_SIZE and CEILING utility macros * Apply a coccinelle patch to use ARRAY_SIZE * fix up some straggling items * Fix 'make test:secure' * Enhance ARRAY_SIZE macro to reject acting on pointers The previous definition would not produce a diagnostic for ``` int *p; size_t num_elem = ARRAY_SIZE(p) ``` but the new one will. * explicitly get definition of ARRAY_SIZE * Convert to ARRAY_SIZE when const is involved The following spatch finds additional instances where the array is const and the division is by the size of the type, not the size of the first element: ``` @ rule5a using "empty.iso" @ type T; const T[] E; @@ - (sizeof(E)/sizeof(T)) + ARRAY_SIZE(E) @ rule6a using "empty.iso" @ type T; const T[] E; @@ - sizeof(E)/sizeof(T) + ARRAY_SIZE(E) ``` * New instances of ARRAY_SIZE added since initial spatch run * Use `ARRAY_SIZE` in docs (found by grep) * Manually use ARRAY_SIZE hs_set is expected to be the same size as uint16_t, though it's made of two 8-bit integers * Just like char, sizeof(uint8_t) is guaranteed to be 1 This is at least true on any plausible system where qmk is actually used. Per my understanding it's universally true, assuming that uint8_t exists: https://stackoverflow.com/questions/48655310/can-i-assume-that-sizeofuint8-t-1 * Run qmk-format on core C files touched in this branch Co-authored-by: Stefan Kerkmann <karlk90@pm.me>
-rw-r--r--docs/feature_combo.md2
-rw-r--r--drivers/haptic/solenoid.c5
-rw-r--r--drivers/sensors/pmw33xx_common.c6
-rw-r--r--keyboards/25keys/zinc/keymaps/ginjake/keymap.c2
-rwxr-xr-xkeyboards/acheron/shark/beta/keymaps/gondolindrim/keymap.c2
-rw-r--r--keyboards/adafruit/macropad/lib/ssd1306_sh1106.c2
-rw-r--r--keyboards/crkbd/keymaps/gotham/oled.c2
-rw-r--r--keyboards/crkbd/keymaps/thunderbird2086/oled.c2
-rw-r--r--keyboards/dm9records/plaid/keymaps/brickbots/keymap.c2
-rw-r--r--keyboards/dm9records/plaid/keymaps/default/keymap.c2
-rw-r--r--keyboards/dm9records/plaid/keymaps/stephen-huan/keymap.c2
-rw-r--r--keyboards/dmqdesign/spin/keymaps/spidey3_pad/keymap.c2
-rwxr-xr-xkeyboards/evolv/keymaps/gondolindrim/keymap.c2
-rw-r--r--keyboards/gboards/butterstick/keymaps/default/keymap.c2
-rw-r--r--keyboards/gboards/butterstick/sten.c2
-rw-r--r--keyboards/gboards/engine/keymap_engine.h10
-rw-r--r--keyboards/gboards/g/keymap_engine.h10
-rw-r--r--keyboards/gboards/georgi/keymaps/colemak-dh/keymap.c4
-rw-r--r--keyboards/gboards/georgi/keymaps/default-flipped/keymap.c2
-rw-r--r--keyboards/gboards/georgi/keymaps/default/keymap.c2
-rw-r--r--keyboards/gboards/georgi/keymaps/minimal/keymap.c2
-rw-r--r--keyboards/gboards/georgi/keymaps/norman/keymap.c2
-rw-r--r--keyboards/gboards/georgi/sten.c2
-rw-r--r--keyboards/gmmk/pro/rev1/ansi/keymaps/cedrikl/keymap.c21
-rw-r--r--keyboards/gmmk/pro/rev1/ansi/keymaps/gourdo1/keymap.c2
-rw-r--r--keyboards/gmmk/pro/rev1/ansi/keymaps/paddlegame/keymap.c12
-rw-r--r--keyboards/gmmk/pro/rev1/iso/keymaps/chofstede/keymap.c4
-rw-r--r--keyboards/gmmk/pro/rev1/iso/keymaps/gourdo1/keymap.c2
-rw-r--r--keyboards/gopolar/gg86/keymaps/horrortroll/keymap_stuff.h2
-rw-r--r--keyboards/gopolar/gg86/lib/wave.h2
-rw-r--r--keyboards/handwired/lagrange/keymaps/dpapavas/keymap.c4
-rw-r--r--keyboards/handwired/myskeeb/oled.c2
-rw-r--r--keyboards/handwired/nicekey/keymaps/default/keymap.c2
-rw-r--r--keyboards/handwired/onekey/keymaps/chibios_waiting_test/keymap.c4
-rw-r--r--keyboards/handwired/promethium/keymaps/default/keymap.c2
-rw-r--r--keyboards/handwired/promethium/keymaps/priyadi/keymap.c2
-rw-r--r--keyboards/handwired/symmetric70_proto/matrix_debug/matrix.c2
-rw-r--r--keyboards/horrortroll/handwired_k552/keymaps/default/keymap_stuff.h2
-rw-r--r--keyboards/horrortroll/handwired_k552/keymaps/via/keymap_stuff.h2
-rw-r--r--keyboards/horrortroll/handwired_k552/lib/wave.c2
-rw-r--r--keyboards/lfkeyboards/lfkpad/keymaps/pascalpfeil/keymap.c2
-rw-r--r--keyboards/massdrop/ctrl/keymaps/responsive_pattern/keymap.c3
-rw-r--r--keyboards/mechwild/mercutio/keymaps/fearless_spiff/keymap.c2
-rw-r--r--keyboards/mechwild/mercutio/keymaps/fearless_spiff_en/keymap.c2
-rwxr-xr-xkeyboards/mechwild/mercutio/keymaps/jonavin/keymap.c2
-rw-r--r--keyboards/mechwild/murphpad/keymaps/jonavin/keymap.c2
-rw-r--r--keyboards/mechwild/puckbuddy/puckbuddy.c2
-rw-r--r--keyboards/mitosis/keymaps/datagrok/keymap.c2
-rw-r--r--keyboards/mwstudio/mw65_rgb/keymaps/horrortroll/keymap_stuff.h2
-rw-r--r--keyboards/mxss/keymaps/default/keymap.c2
-rw-r--r--keyboards/mxss/mxss_frontled.c2
-rw-r--r--keyboards/mxss/templates/keymap.c2
-rw-r--r--keyboards/ploopyco/mouse/mouse.c4
-rw-r--r--keyboards/ploopyco/trackball/trackball.c4
-rw-r--r--keyboards/ploopyco/trackball_mini/trackball_mini.c4
-rw-r--r--keyboards/ploopyco/trackball_nano/trackball_nano.c4
-rw-r--r--keyboards/rgbkb/pan/pan.c2
-rw-r--r--keyboards/rgbkb/sol/keymaps/default/keymap.c2
-rw-r--r--keyboards/rgbkb/sol/keymaps/xyverz/keymap.c2
-rw-r--r--keyboards/rocketboard_16/keycode_lookup.c2
-rw-r--r--keyboards/torn/bongocat.c2
-rw-r--r--keyboards/tzarc/djinn/djinn_portscan_matrix.c8
-rw-r--r--keyboards/ymdk/melody96/keymaps/konstantin/keymap.c2
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/SPP.cpp2
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/PS3Multi/PS3Multi.ino2
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino2
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.h2
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.h2
-rw-r--r--platforms/chibios/drivers/ws2812_spi.c6
-rw-r--r--quantum/backlight/backlight_driver_common.c2
-rw-r--r--quantum/backlight/backlight_software.c2
-rw-r--r--quantum/dip_switch.c4
-rw-r--r--quantum/encoder.h8
-rw-r--r--quantum/process_keycode/process_leader.c2
-rw-r--r--quantum/process_keycode/process_unicode_common.c2
-rw-r--r--quantum/rgb_matrix/rgb_matrix_drivers.c2
-rw-r--r--quantum/secure.c3
-rw-r--r--quantum/util.h49
-rw-r--r--tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c2
-rw-r--r--users/brandonschlack/rgb_bs.c2
-rw-r--r--users/curry/oled.c2
-rw-r--r--users/davidkristoffersen/util/functions.c8
-rw-r--r--users/davidkristoffersen/util/functions.h2
-rw-r--r--users/drashna/keyrecords/tap_dance.md2
-rw-r--r--users/drashna/keyrecords/tap_dances.c2
-rw-r--r--users/drashna/keyrecords/unicode.c2
-rw-r--r--users/drashna/oled/oled_stuff.c2
-rw-r--r--users/gourdo1/gourdo1.h3
-rw-r--r--users/jonavin/jonavin.h3
-rw-r--r--users/spidey3/layer_rgb.c2
-rw-r--r--users/uqs/uqs.c20
91 files changed, 178 insertions, 165 deletions
diff --git a/docs/feature_combo.md b/docs/feature_combo.md
index 42d965509b..bb0b5d7aa0 100644
--- a/docs/feature_combo.md
+++ b/docs/feature_combo.md
@@ -255,7 +255,7 @@ bool combo_should_trigger(uint16_t combo_index, combo_t *combo, uint16_t keycode
```
## Variable Length Combos
-If you leave `COMBO_COUNT` undefined in `config.h`, it allows you to programmatically declare the size of the Combo data structure and avoid updating `COMBO_COUNT`. Instead a variable called `COMBO_LEN` has to be set. It can be set with something similar to the following in `keymap.c`: `uint16_t COMBO_LEN = sizeof(key_combos) / sizeof(key_combos[0]);` or by adding `COMBO_LENGTH` as the *last* entry in the combo enum and then `uint16_t COMBO_LEN = COMBO_LENGTH;` as such:
+If you leave `COMBO_COUNT` undefined in `config.h`, it allows you to programmatically declare the size of the Combo data structure and avoid updating `COMBO_COUNT`. Instead a variable called `COMBO_LEN` has to be set. It can be set with something similar to the following in `keymap.c`: `uint16_t COMBO_LEN = ARRAY_SIZE(key_combos);` or by adding `COMBO_LENGTH` as the *last* entry in the combo enum and then `uint16_t COMBO_LEN = COMBO_LENGTH;` as such:
```c
enum myCombos {
...,
diff --git a/drivers/haptic/solenoid.c b/drivers/haptic/solenoid.c
index 637a77da3d..4e43903255 100644
--- a/drivers/haptic/solenoid.c
+++ b/drivers/haptic/solenoid.c
@@ -20,11 +20,12 @@
#include "haptic.h"
#include "gpio.h"
#include "usb_device_state.h"
+#include "util.h"
#include <stdlib.h>
uint8_t solenoid_dwell = SOLENOID_DEFAULT_DWELL;
static pin_t solenoid_pads[] = SOLENOID_PINS;
-#define NUMBER_OF_SOLENOIDS (sizeof(solenoid_pads) / sizeof(pin_t))
+#define NUMBER_OF_SOLENOIDS ARRAY_SIZE(solenoid_pads)
bool solenoid_on[NUMBER_OF_SOLENOIDS] = {false};
bool solenoid_buzzing[NUMBER_OF_SOLENOIDS] = {false};
uint16_t solenoid_start[NUMBER_OF_SOLENOIDS] = {0};
@@ -147,7 +148,7 @@ void solenoid_check(void) {
void solenoid_setup(void) {
#ifdef SOLENOID_PINS_ACTIVE_STATE
bool state_temp[] = SOLENOID_PINS_ACTIVE_STATE;
- uint8_t bound_check = (sizeof(state_temp) / sizeof(bool));
+ uint8_t bound_check = ARRAY_SIZE(state_temp);
#endif
for (uint8_t i = 0; i < NUMBER_OF_SOLENOIDS; i++) {
diff --git a/drivers/sensors/pmw33xx_common.c b/drivers/sensors/pmw33xx_common.c
index 4993cc2d34..b8d4e532ca 100644
--- a/drivers/sensors/pmw33xx_common.c
+++ b/drivers/sensors/pmw33xx_common.c
@@ -17,10 +17,10 @@
extern const uint8_t pmw33xx_firmware_data[PMW33XX_FIRMWARE_LENGTH] PROGMEM;
extern const uint8_t pmw33xx_firmware_signature[3] PROGMEM;
-static const pin_t cs_pins[] = PMW33XX_CS_PINS;
-static bool in_burst[sizeof(cs_pins) / sizeof(pin_t)] = {0};
+static const pin_t cs_pins[] = PMW33XX_CS_PINS;
+static bool in_burst[ARRAY_SIZE(cs_pins)] = {0};
-const size_t pmw33xx_number_of_sensors = sizeof(cs_pins) / sizeof(pin_t);
+const size_t pmw33xx_number_of_sensors = ARRAY_SIZE(cs_pins);
bool __attribute__((cold)) pmw33xx_upload_firmware(uint8_t sensor);
bool __attribute__((cold)) pmw33xx_check_signature(uint8_t sensor);
diff --git a/keyboards/25keys/zinc/keymaps/ginjake/keymap.c b/keyboards/25keys/zinc/keymaps/ginjake/keymap.c
index 93dfc590e9..26190a2a91 100644
--- a/keyboards/25keys/zinc/keymaps/ginjake/keymap.c
+++ b/keyboards/25keys/zinc/keymaps/ginjake/keymap.c
@@ -333,7 +333,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
aqours_num++;
aqours_next_color_timer_count = 0;
target_col = 0;
- if (aqours_num == sizeof(aqours_h) / sizeof(int)) {
+ if (aqours_num == ARRAY_SIZE(aqours_h)) {
aqours_num = 0;
}
}
diff --git a/keyboards/acheron/shark/beta/keymaps/gondolindrim/keymap.c b/keyboards/acheron/shark/beta/keymaps/gondolindrim/keymap.c
index cae98411d6..fd7f556b56 100755
--- a/keyboards/acheron/shark/beta/keymaps/gondolindrim/keymap.c
+++ b/keyboards/acheron/shark/beta/keymaps/gondolindrim/keymap.c
@@ -94,7 +94,7 @@ encoder_mode_t encoder_modes[] = {
// Insert your custom encoder mode here
};
-#define NUM_ENCODER_MODES (sizeof(encoder_modes)/sizeof(encoder_modes[0]))
+#define NUM_ENCODER_MODES ARRAY_SIZE(encoder_modes)
// This counter is used to track what encoder mode is being used at a certain time
int encoder_mode_count = 0;
diff --git a/keyboards/adafruit/macropad/lib/ssd1306_sh1106.c b/keyboards/adafruit/macropad/lib/ssd1306_sh1106.c
index dc1289fdb1..1b05809640 100644
--- a/keyboards/adafruit/macropad/lib/ssd1306_sh1106.c
+++ b/keyboards/adafruit/macropad/lib/ssd1306_sh1106.c
@@ -94,8 +94,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define OLED_ALL_BLOCKS_MASK (((((OLED_BLOCK_TYPE)1 << (OLED_BLOCK_COUNT - 1)) - 1) << 1) | 1)
-#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
-
// spi defines
#define OLED_STATUS_SUCCESS SPI_STATUS_SUCCESS
diff --git a/keyboards/crkbd/keymaps/gotham/oled.c b/keyboards/crkbd/keymaps/gotham/oled.c
index c4ac5e736c..c285a37c5b 100644
--- a/keyboards/crkbd/keymaps/gotham/oled.c
+++ b/keyboards/crkbd/keymaps/gotham/oled.c
@@ -176,7 +176,7 @@ void add_keylog(uint16_t keycode) {
keylog_str[i] = keylog_str[i - 1];
}
- if (keycode < (sizeof(code_to_name) / sizeof(char))) {
+ if (keycode < ARRAY_SIZE(code_to_name)) {
keylog_str[0] = pgm_read_byte(&code_to_name[keycode]);
}
}
diff --git a/keyboards/crkbd/keymaps/thunderbird2086/oled.c b/keyboards/crkbd/keymaps/thunderbird2086/oled.c
index bee9bc26de..108f3e0cdd 100644
--- a/keyboards/crkbd/keymaps/thunderbird2086/oled.c
+++ b/keyboards/crkbd/keymaps/thunderbird2086/oled.c
@@ -189,7 +189,7 @@ void add_keylog(uint16_t keycode) {
keylog_str[i] = keylog_str[i - 1];
}
- if (keycode < (sizeof(code_to_name) / sizeof(char))) {
+ if (keycode < ARRAY_SIZE(code_to_name)) {
keylog_str[0] = pgm_read_byte(&code_to_name[keycode]);
}
}
diff --git a/keyboards/dm9records/plaid/keymaps/brickbots/keymap.c b/keyboards/dm9records/plaid/keymaps/brickbots/keymap.c
index 819b76e739..0442c930fe 100644
--- a/keyboards/dm9records/plaid/keymaps/brickbots/keymap.c
+++ b/keyboards/dm9records/plaid/keymaps/brickbots/keymap.c
@@ -242,7 +242,7 @@ layer_state_t layer_state_set_user(layer_state_t state) {
void led_keypress_update(uint8_t led, uint8_t led_mode, uint16_t keycode, keyrecord_t *record) {
switch (led_mode) {
case LEDMODE_MODS:
- for (int i=0;i<sizeof(modifiers) / sizeof(modifiers[0]);i++) {
+ for (int i=0;i<ARRAY_SIZE(modifiers);i++) {
if(keycode==modifiers[i]) {
if (record->event.pressed) {
writePinHigh(led);
diff --git a/keyboards/dm9records/plaid/keymaps/default/keymap.c b/keyboards/dm9records/plaid/keymaps/default/keymap.c
index d96c250609..aa0bd8b093 100644
--- a/keyboards/dm9records/plaid/keymaps/default/keymap.c
+++ b/keyboards/dm9records/plaid/keymaps/default/keymap.c
@@ -245,7 +245,7 @@ layer_state_t layer_state_set_user(layer_state_t state) {
void led_keypress_update(uint8_t led, uint8_t led_mode, uint16_t keycode, keyrecord_t *record) {
switch (led_mode) {
case LEDMODE_MODS:
- for (int i=0;i<sizeof(modifiers) / sizeof(modifiers[0]);i++) {
+ for (int i=0;i<ARRAY_SIZE(modifiers);i++) {
if(keycode==modifiers[i]) {
if (record->event.pressed) {
writePinHigh(led);
diff --git a/keyboards/dm9records/plaid/keymaps/stephen-huan/keymap.c b/keyboards/dm9records/plaid/keymaps/stephen-huan/keymap.c
index 354599f340..9a0e89bd89 100644
--- a/keyboards/dm9records/plaid/keymaps/stephen-huan/keymap.c
+++ b/keyboards/dm9records/plaid/keymaps/stephen-huan/keymap.c
@@ -184,7 +184,7 @@ layer_state_t layer_state_set_user(layer_state_t state) {
void led_keypress_update(uint8_t led, uint8_t led_mode, uint16_t keycode, keyrecord_t *record) {
switch (led_mode) {
case LEDMODE_MODS:
- for (int i=0;i<sizeof(modifiers) / sizeof(modifiers[0]);i++) {
+ for (int i=0;i<ARRAY_SIZE(modifiers);i++) {
if(keycode==modifiers[i]) {
if (record->event.pressed) {
writePinHigh(led);
diff --git a/keyboards/dmqdesign/spin/keymaps/spidey3_pad/keymap.c b/keyboards/dmqdesign/spin/keymaps/spidey3_pad/keymap.c
index 8c9af7202b..77534b9a75 100644
--- a/keyboards/dmqdesign/spin/keymaps/spidey3_pad/keymap.c
+++ b/keyboards/dmqdesign/spin/keymaps/spidey3_pad/keymap.c
@@ -111,7 +111,7 @@ const rgblight_segment_t *const PROGMEM _rgb_layers[] = {
};
// clang-format off
-const uint8_t PROGMEM _n_rgb_layers = sizeof(_rgb_layers) / sizeof(_rgb_layers[0]) - 1;
+const uint8_t PROGMEM _n_rgb_layers = ARRAY_SIZE(_rgb_layers) - 1;
void clear_rgb_layers(void) {
dprint("clear_rgb_layers()\n");
diff --git a/keyboards/evolv/keymaps/gondolindrim/keymap.c b/keyboards/evolv/keymaps/gondolindrim/keymap.c
index 3ab66bd056..bf0944fbd1 100755
--- a/keyboards/evolv/keymaps/gondolindrim/keymap.c
+++ b/keyboards/evolv/keymaps/gondolindrim/keymap.c
@@ -129,7 +129,7 @@ const encoder_mode_t encoder_modes[] = {
// Insert your custom encoder mode here
};
-#define NUM_ENCODER_MODES (sizeof(encoder_modes)/sizeof(encoder_modes[0])) // DO NOT CHANGE THIS. NUM_ENCODER_MODES calculates how many modes there are.
+#define NUM_ENCODER_MODES ARRAY_SIZE(encoder_modes) // DO NOT CHANGE THIS. NUM_ENCODER_MODES calculates how many modes there are.
// This counter is used to track what encoder mode is being used at a certain time
int encoder_mode_count = 0;
diff --git a/keyboards/gboards/butterstick/keymaps/default/keymap.c b/keyboards/gboards/butterstick/keymaps/default/keymap.c
index 749e9ba071..d0c6af65df 100644
--- a/keyboards/gboards/butterstick/keymaps/default/keymap.c
+++ b/keyboards/gboards/butterstick/keymaps/default/keymap.c
@@ -180,4 +180,4 @@ uint32_t processQwerty(bool lookup) {
}
// Don't fuck with this, thanks.
-size_t keymapsCount = sizeof(keymaps)/sizeof(keymaps[0]);
+size_t keymapsCount = ARRAY_SIZE(keymaps);
diff --git a/keyboards/gboards/butterstick/sten.c b/keyboards/gboards/butterstick/sten.c
index 197abaf92f..8820127848 100644
--- a/keyboards/gboards/butterstick/sten.c
+++ b/keyboards/gboards/butterstick/sten.c
@@ -21,7 +21,7 @@ uint32_t tChord = 0; // Protects state of cChord