Skip to content

Commit f59e987

Browse files
committed
fix(scanner): address -Wswitch-outside-range warnings when char is unsigned
C doesn't define the sign of the `char` type, so some architectures (x86) use `signed char` where as others use `unsigned char`. On architectures where unsigned char is used or if compiled with "-funsigned-char", we see the below warnings: src/scanner.c: In function ‘scn_dir_tag_pfx’: src/scanner.c:470:13: warning: case label value is less than minimum value for type [-Wswitch-outside-range] 470 | case SCN_FAIL: | ^~~~ src/scanner.c: In function ‘scn_tag’: src/scanner.c:512:17: warning: case label value is less than minimum value for type [-Wswitch-outside-range] 512 | case SCN_FAIL: | ^~~~ src/scanner.c:526:17: warning: case label value is less than minimum value for type [-Wswitch-outside-range] 526 | case SCN_FAIL: | ^~~~ Define a new `ScanStatus` enum to use instead of independent `#define`s, and update all the functions which previously returned `char` to instead return `ScanStatus`.
1 parent 4463985 commit f59e987

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/scanner.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ typedef enum {
6767

6868
// clang-format on
6969

70-
#define SCN_SUCC 1
71-
#define SCN_STOP 0
72-
#define SCN_FAIL (-1)
70+
typedef enum {
71+
SCN_FAIL = -1,
72+
SCN_STOP,
73+
SCN_SUCC,
74+
} ScanStatus;
7375

7476
#define IND_ROT 'r'
7577
#define IND_MAP 'm'
@@ -326,7 +328,7 @@ static inline bool is_ns_tag_char(int32_t c) {
326328

327329
static inline bool is_ns_anchor_char(int32_t c) { return is_ns_char(c) && !is_c_flow_indicator(c); }
328330

329-
static char scn_uri_esc(Scanner *scanner, TSLexer *lexer) {
331+
static ScanStatus scn_uri_esc(Scanner *scanner, TSLexer *lexer) {
330332
if (lexer->lookahead != '%') {
331333
return SCN_STOP;
332334
}
@@ -343,15 +345,15 @@ static char scn_uri_esc(Scanner *scanner, TSLexer *lexer) {
343345
return SCN_SUCC;
344346
}
345347

346-
static char scn_ns_uri_char(Scanner *scanner, TSLexer *lexer) {
348+
static ScanStatus scn_ns_uri_char(Scanner *scanner, TSLexer *lexer) {
347349
if (is_ns_uri_char(lexer->lookahead)) {
348350
adv(scanner, lexer);
349351
return SCN_SUCC;
350352
}
351353
return scn_uri_esc(scanner, lexer);
352354
}
353355

354-
static char scn_ns_tag_char(Scanner *scanner, TSLexer *lexer) {
356+
static ScanStatus scn_ns_tag_char(Scanner *scanner, TSLexer *lexer) {
355357
if (is_ns_tag_char(lexer->lookahead)) {
356358
adv(scanner, lexer);
357359
return SCN_SUCC;
@@ -778,7 +780,7 @@ static bool scn_blk_str_cnt(Scanner *scanner, TSLexer *lexer, TSSymbol result_sy
778780
RET_SYM(result_symbol);
779781
}
780782

781-
static char scn_pln_cnt(Scanner *scanner, TSLexer *lexer, bool (*is_plain_safe)(int32_t)) {
783+
static ScanStatus scn_pln_cnt(Scanner *scanner, TSLexer *lexer, bool (*is_plain_safe)(int32_t)) {
782784
bool is_cur_wsp = is_wsp(scanner->cur_chr);
783785
bool is_cur_saf = is_plain_safe(scanner->cur_chr);
784786
bool is_lka_wsp = is_wsp(lexer->lookahead);

0 commit comments

Comments
 (0)