Skip to content

Commit e189226

Browse files
committed
clang-tidy: Miscellaneous fixes
1 parent 5cfd36a commit e189226

17 files changed

+55
-27
lines changed

.clang-tidy

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Checks:
1616
- performance-*
1717
- google-*
1818
- -google-readability-braces-around-statements
19+
- -google-readability-todo
1920
# - misc-*
2021
# - -misc-include-cleaner
2122
# ExtraArgs:
@@ -47,5 +48,6 @@ CheckOptions:
4748
llvm-else-after-return.WarnOnUnfixable: 'false'
4849
llvm-qualified-auto.AddConstToQualified: 'false'
4950
bugprone-suspicious-enum-usage.StrictMode: 'true'
51+
bugprone-signed-char-misuse.CharTypdefsToIgnore: 'int8_t'
5052
SystemHeaders: false
5153
...

bench/bench.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ predict(long long t1, long long t2, struct estimate *est)
127127
{
128128
const long long t = fit(t1, t2);
129129
est->elapsed = t;
130-
est->stdev = truncl(sqrtl((long double)sqr(t1 - t) + (long double)sqr(t2 - 2 * t)));
130+
est->stdev =
131+
llroundl(sqrtl((long double)sqr(t1 - t) + (long double)sqr(t2 - 2 * t)));
131132
}
132133

133134
#define high(t, prec) ((t) + (prec))

bench/keysym-case-mappings.c

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ main(void)
5555
functions[f].toLower(ks);
5656
functions[f].toUpper(ks);
5757
}
58+
/* Avoid dangling pointers
59+
* NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) */
5860
iter = xkb_keysym_iterator_unref(iter);
5961
}
6062
bench_stop(&bench);

scripts/update-keysyms-names-handling.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ def get_keysyms(path: Path) -> dict[int, list[Keysym]]:
175175
{
176176
size_t sum = 0;
177177
for (size_t i = 0; key[i] != '\0'; i++)
178-
sum += T[i % $NS] * key[i];
178+
sum += (size_t) (T[i % $NS] * key[i]);
179179
return sum % $NG;
180180
}
181181
182-
static size_t
182+
static inline size_t
183183
keysym_name_perfect_hash(const char *key)
184184
{
185185
return (

src/ks_tables.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -2916,11 +2916,11 @@ keysym_name_hash_f(const char *key, const char *T)
29162916
{
29172917
size_t sum = 0;
29182918
for (size_t i = 0; key[i] != '\0'; i++)
2919-
sum += T[i % 32] * key[i];
2919+
sum += (size_t) (T[i % 32] * key[i]);
29202920
return sum % 4857;
29212921
}
29222922

2923-
static size_t
2923+
static inline size_t
29242924
keysym_name_perfect_hash(const char *key)
29252925
{
29262926
return (

src/scanner-utils.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ scanner_hex(struct scanner *s, uint8_t *out)
206206
int i;
207207
for (i = 0, *out = 0; is_xdigit(scanner_peek(s)) && i < 2; i++) {
208208
const char c = scanner_next(s);
209-
const char offset = (c >= '0' && c <= '9' ? '0' :
210-
c >= 'a' && c <= 'f' ? 'a' - 10 : 'A' - 10);
209+
const char offset = (char) (c >= '0' && c <= '9' ? '0' :
210+
c >= 'a' && c <= 'f' ? 'a' - 10
211+
: 'A' - 10);
211212
*out = *out * 16 + c - offset;
212213
}
213214
return i > 0;

src/utf8.c

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ utf32_to_utf8(uint32_t unichar, char *buffer)
2222
int count, shift, length;
2323
uint8_t head;
2424

25+
/* NOLINTBEGIN(bugprone-branch-clone) */
2526
if (unichar <= 0x007f) {
2627
buffer[0] = (char) unichar;
2728
buffer[1] = '\0';
@@ -46,6 +47,7 @@ utf32_to_utf8(uint32_t unichar, char *buffer)
4647
else {
4748
goto ill_formed_code_unit_subsequence;
4849
}
50+
/* NOLINTEND(bugprone-branch-clone) */
4951

5052
for (count = length - 1, shift = 0; count > 0; count--, shift += 6)
5153
buffer[count] = (char)(0x80 | ((unichar >> shift) & 0x3f));
@@ -73,6 +75,7 @@ is_valid_utf8(const char *ss, size_t len)
7375
* We can optimize if needed. */
7476
while (i < len)
7577
{
78+
/* NOLINTBEGIN(bugprone-branch-clone) */
7679
if (s[i] <= 0x7F) {
7780
tail_bytes = 0;
7881
}
@@ -115,6 +118,7 @@ is_valid_utf8(const char *ss, size_t len)
115118
else {
116119
return false;
117120
}
121+
/* NOLINTEND(bugprone-branch-clone) */
118122

119123
i++;
120124

src/xkbcomp/keymap.c

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ FindInterpForKey(struct xkb_keymap *keymap, const struct xkb_key *key,
118118
struct xkb_sym_interpret const **previous_interp;
119119
darray_foreach(previous_interp, *interprets) {
120120
if (*previous_interp == interp) {
121+
/* Keep as a safeguard against refactoring
122+
* NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) */
121123
found = false;
122124
log_warn(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
123125
"Repeated interpretation ignored for keysym "

src/xkbcomp/rules.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,14 @@ extract_layout_index(const char *s, size_t max_len, xkb_layout_index_t *out)
496496
}
497497
/* Numeric index */
498498

499-
#define parse_layout_int_index(s, endptr, index, out) \
500-
char *endptr; \
501-
long index = strtol(&(s)[1], &endptr, 10); \
502-
if (index < 0 || endptr[0] != ']' || index > XKB_MAX_GROUPS) \
503-
return -1; \
504-
/* To zero-based index. */ \
505-
*(out) = (xkb_layout_index_t)index - 1; \
506-
return (int)(endptr - (s) + 1) /* == length "[index]" */
499+
#define parse_layout_int_index(s, endptr, index, out) \
500+
char *(endptr); \
501+
long (index) = strtol(&(s)[1], &(endptr), 10); \
502+
if ((index) < 0 || (endptr)[0] != ']' || (index) > XKB_MAX_GROUPS) \
503+
return -1; \
504+
/* To zero-based index. */ \
505+
*(out) = (xkb_layout_index_t)(index) - 1; \
506+
return (int)((endptr) - (s) + 1) /* == length "[index]" */
507507

508508
parse_layout_int_index(s, endptr, index, out);
509509
}
@@ -1135,9 +1135,9 @@ append_expanded_kccgst_value(struct matcher *m, struct scanner *s,
11351135
* Appending +bar to +foo -> +foo+bar
11361136
*/
11371137

1138-
ch = (darray_empty(expanded) ? '\0' : darray_item(expanded, 0));
1138+
ch = (char) (darray_empty(expanded) ? '\0' : darray_item(expanded, 0));
11391139
expanded_plus = is_merge_mode_prefix(ch);
1140-
ch = (darray_empty(*to) ? '\0' : darray_item(*to, 0));
1140+
ch = (char) (darray_empty(*to) ? '\0' : darray_item(*to, 0));
11411141
to_plus = is_merge_mode_prefix(ch);
11421142

11431143
if (expanded_plus || darray_empty(*to))

src/xkbcomp/scanner.c

+4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ number(struct scanner *s, int64_t *out, int *out_tok)
3535
else if (is_float) {
3636
/* The parser currently just ignores floats, so the cast is
3737
* fine - the value doesn't matter. */
38+
#ifdef __GNUC__
3839
#pragma GCC diagnostic push
3940
#pragma GCC diagnostic ignored "-Wbad-function-cast"
41+
#endif
4042
x = (long long int) strtold(start, &end);
43+
#ifdef __GNUC__
4144
#pragma GCC diagnostic pop
45+
#endif
4246
}
4347
else
4448
x = strtoll(start, &end, 10);

src/xkbcomp/symbols.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,11 @@ SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
11601160
return false;
11611161
}
11621162

1163-
if (group_to_use >= darray_size(info->group_names))
1163+
if (group_to_use >= darray_size(info->group_names)) {
1164+
/* Avoid clang-tidy false positive */
1165+
assert(darray_size(info->group_names) < group_to_use + 1);
11641166
darray_resize0(info->group_names, group_to_use + 1);
1167+
}
11651168
darray_item(info->group_names, group_to_use) = name;
11661169

11671170
return true;

test/common.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void
3737
test_init(void)
3838
{
3939
/* Make stdout always unbuffered, to ensure we always get it entirely */
40-
setbuf(stdout, NULL);
40+
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
4141
}
4242

4343
void
@@ -500,7 +500,6 @@ test_compile_output(struct xkb_context *ctx,
500500
if (k > 0)
501501
continue;
502502
/* Test round trip */
503-
free(got);
504503
keymap = compile_buffer(ctx, expected, strlen(expected),
505504
compile_buffer_private);
506505
if (!keymap) {
@@ -509,6 +508,7 @@ test_compile_output(struct xkb_context *ctx,
509508
success = false;
510509
break;
511510
}
511+
free(got);
512512
got = xkb_keymap_get_as_string(keymap,
513513
XKB_KEYMAP_USE_ORIGINAL_FORMAT);
514514
if (!got) {

test/keymap.c

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "evdev-scancodes.h"
1515
#include "test.h"
16+
#include "xkbcommon/xkbcommon-keysyms.h"
1617
#include "keymap.h"
1718

1819
#define KEY_LVL3 84
@@ -334,6 +335,10 @@ test_multiple_actions_per_level(void)
334335
assert(keymap);
335336

336337
kc = xkb_keymap_key_by_name(keymap, "LCTL");
338+
keysyms_count = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 0, &keysyms);
339+
assert(keysyms_count == 2);
340+
assert(keysyms[0] == XKB_KEY_Control_L);
341+
assert(keysyms[1] == XKB_KEY_ISO_Group_Shift);
337342
ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
338343
level3 = xkb_keymap_mod_get_index(keymap, "Mod5");
339344
state = xkb_state_new(keymap);

test/merge_modes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ enum update_files {
123123
: ARRAY_SIZE(keymap_test_str), \
124124
/* Local and global merge modes use the same result file */ \
125125
GOLDEN_TESTS_OUTPUTS #merge_mode map file_suffix ".xkb", \
126-
!!update)); \
126+
!!(update))); \
127127
} while (0)
128128

129129
/* Helper to create a test for each merge mode */

test/registry.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ find_models(struct rxkb_context *ctx, ...)
323323
va_start(args, ctx);
324324
name = va_arg(args, const char *);
325325
while(name) {
326-
assert(++idx < 20); /* safety guard */
326+
++idx;
327+
assert(idx < 20); /* safety guard */
327328
if (!find_model(ctx, name))
328329
goto out;
329330
name = va_arg(args, const char *);
@@ -371,7 +372,8 @@ find_layouts(struct rxkb_context *ctx, ...)
371372
name = va_arg(args, const char *);
372373
variant = va_arg(args, const char *);
373374
while(name) {
374-
assert(++idx < 20); /* safety guard */
375+
++idx;
376+
assert(idx < 20); /* safety guard */
375377
if (!find_layout(ctx, name, variant))
376378
goto out;
377379
name = va_arg(args, const char *);
@@ -398,7 +400,8 @@ check_layouts_order(struct rxkb_context *ctx, ...)
398400
layout = va_arg(args, const char *);
399401
variant = va_arg(args, const char *);
400402
while(layout) {
401-
assert(++idx < 20); /* safety guard */
403+
++idx;
404+
assert(idx < 20); /* safety guard */
402405
if (!l) {
403406
fprintf(stderr, "ERROR: expected layout #%d \"%s(%s)\", got none\n",
404407
idx, layout, variant ? variant : "-");
@@ -478,7 +481,8 @@ find_options(struct rxkb_context *ctx, ...)
478481
grp = va_arg(args, const char *);
479482
opt = va_arg(args, const char *);
480483
while(grp) {
481-
assert(++idx < 20); /* safety guard */
484+
++idx;
485+
assert(idx < 20); /* safety guard */
482486
if (!find_option(ctx, grp, opt))
483487
goto out;
484488
grp = va_arg(args, const char *);

test/rules-file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ main(int argc, char *argv[])
266266

267267
#define ENTRY2(_rules, _model, _layout, _variant, _options, \
268268
_keycodes, _types, _compat, _symbols, _count, _should_fail) \
269-
{ .rules = (_rules), .model = _model, \
269+
{ .rules = (_rules), .model = (_model), \
270270
.layout = (_layout), .variant = (_variant), .options = (_options), \
271271
.keycodes = (_keycodes), .types = (_types), .compat = (_compat), \
272272
.symbols = (_symbols) , .explicit_layouts = (_count), \

test/xvfb-wrapper.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ xvfb_wrapper(int (*test_func)(char* display))
9999

100100
/* Retrieve the display number: Xvfd writes the display number as a newline-
101101
* terminated string; copy this number to form a proper display string. */
102-
rewind(display_fd);
102+
fseek(display_fd, 0, SEEK_SET);
103103
length = fread(&display[1], 1, sizeof(display) - 1, display_fd);
104104
if (length <= 0) {
105105
fprintf(stderr, "fread error: length=%zu\n", length);

0 commit comments

Comments
 (0)