summaryrefslogtreecommitdiff
path: root/users
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 /users
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>
Diffstat (limited to 'users')
-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
12 files changed, 21 insertions, 29 deletions
diff --git a/users/brandonschlack/rgb_bs.c b/users/brandonschlack/rgb_bs.c
index 1abf785b49..94842021cd 100644
--- a/users/brandonschlack/rgb_bs.c
+++ b/users/brandonschlack/rgb_bs.c
@@ -75,7 +75,7 @@ void rgb_theme_step_reverse(void) {
rgb_theme_color_t get_rgb_theme_color(uint8_t index) {
rgb_theme_t theme = get_rgb_theme();
- size_t rgb_theme_color_max = sizeof theme.colors / sizeof *theme.colors;
+ size_t rgb_theme_color_max = ARRAY_SIZE(theme.colors);
if (index == _ADJUST) {
return default_adjust;
diff --git a/users/curry/oled.c b/users/curry/oled.c
index 5a8f0de61d..89112af121 100644
--- a/users/curry/oled.c
+++ b/users/curry/oled.c
@@ -41,7 +41,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/users/davidkristoffersen/util/functions.c b/users/davidkristoffersen/util/functions.c
index 781d8f214d..3ab4ace2d6 100644
--- a/users/davidkristoffersen/util/functions.c
+++ b/users/davidkristoffersen/util/functions.c
@@ -33,14 +33,14 @@ code_set_t EN_SHIFT_CODES [] = {
const shift_code_t SHIFT_CODES [] = {
#ifdef LAYER_NO
{.lang = LAYER_NO,
- .size = ARR_LEN(NO_SHIFT_CODES),
+ .size = ARRAY_SIZE(NO_SHIFT_CODES),
.codes = NO_SHIFT_CODES},
#endif
{.lang = LAYER_EN,
- .size = ARR_LEN(EN_SHIFT_CODES),
+ .size = ARRAY_SIZE(EN_SHIFT_CODES),
.codes = EN_SHIFT_CODES},
};
-const int SHIFT_CODES_SIZE = ARR_LEN(SHIFT_CODES);
+const int SHIFT_CODES_SIZE = ARRAY_SIZE(SHIFT_CODES);
#endif
#ifdef LAYER_NO
@@ -72,7 +72,7 @@ const code_set_t EN2NO_CODES [] = {
{KC_DLR, NO_DLR},
{KC_GRV, NO_GRV}
};
-const int EN2NO_CODES_SIZE = ARR_LEN(EN2NO_CODES);
+const int EN2NO_CODES_SIZE = ARRAY_SIZE(EN2NO_CODES);
#endif
// Check if layer is an active default layer
diff --git a/users/davidkristoffersen/util/functions.h b/users/davidkristoffersen/util/functions.h
index eee1dadc57..5fef010694 100644
--- a/users/davidkristoffersen/util/functions.h
+++ b/users/davidkristoffersen/util/functions.h
@@ -15,8 +15,6 @@
// Return false if test equal false
#define HANDLE_FALSE(bool) if (!bool) return false;
-// Generic array lenght define
-#define ARR_LEN(arr) (sizeof(arr) / sizeof(arr)[0])
// Printf-like functionality for send_string
#define SEND_VAR(str, ...) \
do { \
diff --git a/users/drashna/keyrecords/tap_dance.md b/users/drashna/keyrecords/tap_dance.md
index 0bf67cbd5a..fef1435918 100644
--- a/users/drashna/keyrecords/tap_dance.md
+++ b/users/drashna/keyrecords/tap_dance.md
@@ -88,7 +88,7 @@ void diablo_tapdance_master(qk_tap_dance_state_t *state, void *user_data) {
diablo_timer[diablo_keys->index].keycode = diablo_keys->keycode;
// if the tapdance is hit more than the number of elemints in the array, reset
- if (state->count >= (sizeof(diablo_times) / sizeof(uint8_t) ) ) {
+ if (state->count >= ARRAY_SIZE(diablo_times) ) {
diablo_timer[diablo_keys->index].key_interval = 0;
reset_tap_dance(state);
} else { // else set the interval (tapdance count starts at 1, array starts at 0, so offset by one)
diff --git a/users/drashna/keyrecords/tap_dances.c b/users/drashna/keyrecords/tap_dances.c
index 6caf6b6b3e..7bdea3cae3 100644
--- a/users/drashna/keyrecords/tap_dances.c
+++ b/users/drashna/keyrecords/tap_dances.c
@@ -23,7 +23,7 @@ void diablo_tapdance_master(qk_tap_dance_state_t *state, void *user_data) {
diablo_timer[diablo_keys->index].keycode = diablo_keys->keycode;
// if the tapdance is hit more than the number of elemints in the array, reset
- if (state->count >= (sizeof(diablo_times) / sizeof(uint8_t))) {
+ if (state->count >= ARRAY_SIZE(diablo_times)) {
diablo_timer[diablo_keys->index].key_interval = 0;
reset_tap_dance(state);
} else { // else set the interval (tapdance count starts at 1, array starts at 0, so offset by one)
diff --git a/users/drashna/keyrecords/unicode.c b/users/drashna/keyrecords/unicode.c
index b3fc71cb09..bfce38f555 100644
--- a/users/drashna/keyrecords/unicode.c
+++ b/users/drashna/keyrecords/unicode.c
@@ -43,7 +43,7 @@ typedef uint32_t (*translator_function_t)(bool is_shifted, uint32_t keycode);
static inline uint32_t translator_name(bool is_shifted, uint32_t keycode) { \
static const uint32_t translation[] = {__VA_ARGS__}; \
uint32_t ret = keycode; \
- if ((keycode - KC_A) < (sizeof(translation) / sizeof(uint32_t))) { \
+ if ((keycode - KC_A) < ARRAY_SIZE(translation)) { \
ret = translation[keycode - KC_A]; \
} \
return ret; \
diff --git a/users/drashna/oled/oled_stuff.c b/users/drashna/oled/oled_stuff.c
index e082f8ab3e..68801b80a4 100644
--- a/users/drashna/oled/oled_stuff.c
+++ b/users/drashna/oled/oled_stuff.c
@@ -87,7 +87,7 @@ void add_keylog(uint16_t keycode, keyrecord_t *record) {
memmove(keylog_str, keylog_str + 1, OLED_KEYLOGGER_LENGTH - 1);
- if (keycode < (sizeof(code_to_name) / sizeof(char))) {
+ if (keycode < ARRAY_SIZE(code_to_name)) {
keylog_str[(OLED_KEYLOGGER_LENGTH - 1)] = pgm_read_byte(&code_to_name[keycode]);
}
diff --git a/users/gourdo1/gourdo1.h b/users/gourdo1/gourdo1.h
index ecf6eaf25d..a198fd0805 100644
--- a/users/gourdo1/gourdo1.h
+++ b/users/gourdo1/gourdo1.h
@@ -17,9 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
-// DEFINE MACROS
-#define ARRAYSIZE(arr) sizeof(arr) / sizeof(arr[0])
-
// LAYERS -- Note: to avoid compile problems, make sure total layers matches DYNAMIC_KEYMAP_LAYER_COUNT defined in config.h (where _COLEMAK layer is defined)
enum custom_user_layers {
_BASE,
diff --git a/users/jonavin/jonavin.h b/users/jonavin/jonavin.h
index ab2ce0dff2..c3e383bd3a 100644
--- a/users/jonavin/jonavin.h
+++ b/users/jonavin/jonavin.h
@@ -17,9 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
-// DEFINE MACROS
-#define ARRAYSIZE(arr) sizeof(arr)/sizeof(arr[0])
-
// LAYERS
enum custom_user_layers {
diff --git a/users/spidey3/layer_rgb.c b/users/spidey3/layer_rgb.c
index c867468194..b34b91679d 100644
--- a/users/spidey3/layer_rgb.c
+++ b/users/spidey3/layer_rgb.c
@@ -97,7 +97,7 @@ const rgblight_segment_t *const PROGMEM _rgb_layers[] = {
// clang-format on
-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() {
for (uint8_t i = 0; i < _n_rgb_layers; i++) {
diff --git a/users/uqs/uqs.c b/users/uqs/uqs.c
index 72284143c6..7e2d09e0f6 100644
--- a/users/uqs/uqs.c
+++ b/users/uqs/uqs.c
@@ -78,9 +78,9 @@ const rgblight_segment_t* const PROGMEM my_rgb_layers[] = {
my_rgb_segments[L_MOUSE],
};
-_Static_assert(sizeof(my_rgb_layers) / sizeof(my_rgb_layers[0]) ==
- sizeof(my_rgb_segments) / sizeof(my_rgb_segments[0]),
- "Number of rgb_segment definitions does not match up!");
+_Static_assert(ARRAY_SIZE(my_rgb_layers) ==
+ ARRAY_SIZE(my_rgb_segments),
+ "Number of rgb_segment definitions does not match up!");
#endif
#ifdef COMBO_ENABLE
@@ -125,7 +125,7 @@ const uint16_t PROGMEM my_combos[][4] = {
{KC_BTN1, KC_BTN2, KC_BTN3, COMBO_END},
};
-const uint16_t COMBO_LEN = sizeof(my_action_combos) / sizeof(my_action_combos[0]) + sizeof(my_combos) / sizeof(my_combos[0]);
+const uint16_t COMBO_LEN = ARRAY_SIZE(my_action_combos) + ARRAY_SIZE(my_combos);
#define MY_ACTION_COMBO(ck) \
[ck] = { .keys = &(my_action_combos[ck][0]) }
@@ -162,11 +162,11 @@ combo_t key_combos[] = {
MY_COMBO(14),
};
-_Static_assert(sizeof(key_combos) / sizeof(key_combos[0]) ==
- (sizeof(my_action_combos) / sizeof(my_action_combos[0]) + sizeof(my_combos) / sizeof(my_combos[0])),
- "Number of combo definitions does not match up!");
+_Static_assert(ARRAY_SIZE(key_combos) ==
+ (ARRAY_SIZE(my_action_combos) + ARRAY_SIZE(my_combos)),
+ "Number of combo definitions does not match up!");
#else
-combo_t key_combos[sizeof(my_action_combos) / sizeof(my_action_combos[0]) + sizeof(my_combos) / sizeof(my_combos[0])];
+combo_t key_combos[ARRAY_SIZE(my_action_combos) + ARRAY_SIZE(my_combos)];
#endif
void process_combo_event(uint16_t combo_index, bool pressed) {
@@ -235,10 +235,10 @@ void keyboard_post_init_user(void) {
#endif
#if defined(COMBO_ENABLE) && !defined(COMBO_STATICALLY)
uint8_t i = 0;
- for (; i < sizeof(my_action_combos) / sizeof(my_action_combos[0]); i++) {
+ for (; i < ARRAY_SIZE(my_action_combos); i++) {
key_combos[i].keys = &(my_action_combos[i][0]);
}
- for (uint8_t j = 0; j < sizeof(my_combos) / sizeof(my_combos[0]); j++, i++) {
+ for (uint8_t j = 0; j < ARRAY_SIZE(my_combos); j++, i++) {
key_combos[i].keycode = my_combos[j][0];
key_combos[i].keys = &(my_combos[j][1]);
}