1
0
Fork 0

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>
This commit is contained in:
Jeff Epler 2022-08-30 03:20:04 -05:00 committed by GitHub
parent 2c5aa98143
commit 9632360caa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
91 changed files with 177 additions and 164 deletions

View file

@ -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]);
}