Skip to content

Commit 963a5a3

Browse files
committed
py, unix: Allow to compile with -Wsign-compare.
See issue adafruit#699.
1 parent f12ea7c commit 963a5a3

File tree

23 files changed

+53
-51
lines changed

23 files changed

+53
-51
lines changed

py/asmx86.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ void asm_x86_label_assign(asm_x86_t *as, mp_uint_t label) {
398398
assert(label < as->max_num_labels);
399399
if (as->pass < ASM_X86_PASS_EMIT) {
400400
// assign label offset
401-
assert(as->label_offsets[label] == -1);
401+
assert(as->label_offsets[label] == (mp_uint_t)-1);
402402
as->label_offsets[label] = as->code_offset;
403403
} else {
404404
// ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
@@ -407,15 +407,15 @@ void asm_x86_label_assign(asm_x86_t *as, mp_uint_t label) {
407407
}
408408
}
409409

410-
STATIC mp_uint_t get_label_dest(asm_x86_t *as, int label) {
410+
STATIC mp_uint_t get_label_dest(asm_x86_t *as, mp_uint_t label) {
411411
assert(label < as->max_num_labels);
412412
return as->label_offsets[label];
413413
}
414414

415415
void asm_x86_jmp_label(asm_x86_t *as, mp_uint_t label) {
416416
mp_uint_t dest = get_label_dest(as, label);
417417
mp_int_t rel = dest - as->code_offset;
418-
if (dest != -1 && rel < 0) {
418+
if (dest != (mp_uint_t)-1 && rel < 0) {
419419
// is a backwards jump, so we know the size of the jump on the first pass
420420
// calculate rel assuming 8 bit relative jump
421421
rel -= 2;
@@ -437,7 +437,7 @@ void asm_x86_jmp_label(asm_x86_t *as, mp_uint_t label) {
437437
void asm_x86_jcc_label(asm_x86_t *as, mp_uint_t jcc_type, mp_uint_t label) {
438438
mp_uint_t dest = get_label_dest(as, label);
439439
mp_int_t rel = dest - as->code_offset;
440-
if (dest != -1 && rel < 0) {
440+
if (dest != (mp_uint_t)-1 && rel < 0) {
441441
// is a backwards jump, so we know the size of the jump on the first pass
442442
// calculate rel assuming 8 bit relative jump
443443
rel -= 2;

py/bc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, mp_uint_t
116116
// Apply processing and check below only if we don't have kwargs,
117117
// otherwise, kw handling code below has own extensive checks.
118118
if (n_kw == 0 && !self->has_def_kw_args) {
119-
if (n_args >= self->n_pos_args - self->n_def_args) {
119+
if (n_args >= (mp_uint_t)(self->n_pos_args - self->n_def_args)) {
120120
// given enough arguments, but may need to use some default arguments
121121
for (mp_uint_t i = n_args; i < self->n_pos_args; i++) {
122122
code_state->state[n_state - 1 - i] = self->extra_args[i - (self->n_pos_args - self->n_def_args)];

py/binary.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
256256
}
257257
}
258258

259-
mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, val);
259+
mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
260260
}
261261

262262
void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) {

py/builtinimport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
203203
nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "Invalid relative import"));
204204
}
205205

206-
uint new_mod_l = (mod_len == 0 ? p - this_name : p - this_name + 1 + mod_len);
206+
uint new_mod_l = (mod_len == 0 ? (size_t)(p - this_name) : (size_t)(p - this_name) + 1 + mod_len);
207207
char *new_mod = alloca(new_mod_l);
208208
memcpy(new_mod, this_name, p - this_name);
209209
if (mod_len != 0) {

py/compile.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
216216
mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
217217
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
218218
// int << int
219-
if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
219+
if (!(arg1 >= (mp_int_t)BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
220220
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
221221
}
222222
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
223223
// int >> int
224-
if (arg1 >= BITS_PER_WORD) {
224+
if (arg1 >= (mp_int_t)BITS_PER_WORD) {
225225
// Shifting to big amounts is underfined behavior
226226
// in C and is CPU-dependent; propagate sign bit.
227227
arg1 = BITS_PER_WORD - 1;
@@ -386,8 +386,8 @@ STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse
386386
return scope;
387387
}
388388

389-
STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
390-
if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
389+
STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
390+
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
391391
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
392392
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
393393
for (int i = 0; i < num_nodes; i++) {
@@ -398,7 +398,7 @@ STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn
398398
}
399399
}
400400

401-
STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
401+
STATIC int list_get(mp_parse_node_t *pn, pn_kind_t pn_kind, mp_parse_node_t **nodes) {
402402
if (MP_PARSE_NODE_IS_NULL(*pn)) {
403403
*nodes = NULL;
404404
return 0;
@@ -811,14 +811,14 @@ STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num
811811
uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
812812

813813
// look for star expression
814-
int have_star_index = -1;
814+
uint have_star_index = -1;
815815
if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
816816
EMIT_ARG(unpack_ex, 0, num_tail);
817817
have_star_index = 0;
818818
}
819-
for (int i = 0; i < num_tail; i++) {
819+
for (uint i = 0; i < num_tail; i++) {
820820
if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
821-
if (have_star_index < 0) {
821+
if (have_star_index == (uint)-1) {
822822
EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
823823
have_star_index = num_head + i;
824824
} else {
@@ -827,7 +827,7 @@ STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num
827827
}
828828
}
829829
}
830-
if (have_star_index < 0) {
830+
if (have_star_index == (uint)-1) {
831831
EMIT_ARG(unpack_sequence, num_head + num_tail);
832832
}
833833
if (num_head != 0) {
@@ -837,7 +837,7 @@ STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num
837837
c_assign(comp, node_head, ASSIGN_STORE);
838838
}
839839
}
840-
for (int i = 0; i < num_tail; i++) {
840+
for (uint i = 0; i < num_tail; i++) {
841841
if (num_head + i == have_star_index) {
842842
c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
843843
} else {

py/emitbc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ STATIC void emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
424424
assert(l < emit->max_num_labels);
425425
if (emit->pass < MP_PASS_EMIT) {
426426
// assign label offset
427-
assert(emit->label_offsets[l] == -1);
427+
assert(emit->label_offsets[l] == (mp_uint_t)-1);
428428
emit->label_offsets[l] = emit->bytecode_offset;
429429
} else {
430430
// ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT

py/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) {
657657
// need to check for this special token in many places in the compiler.
658658
// TODO improve speed of these string comparisons
659659
//for (mp_int_t i = 0; tok_kw[i] != NULL; i++) {
660-
for (mp_int_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
660+
for (size_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
661661
if (str_strn_equal(tok_kw[i], lex->vstr.buf, lex->vstr.len)) {
662662
if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
663663
// tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"

py/map.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const mp_map_t mp_const_empty_map = {
4747
STATIC uint32_t doubling_primes[] = {0, 7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607};
4848

4949
STATIC mp_uint_t get_doubling_prime_greater_or_equal_to(mp_uint_t x) {
50-
for (int i = 0; i < MP_ARRAY_SIZE(doubling_primes); i++) {
50+
for (size_t i = 0; i < MP_ARRAY_SIZE(doubling_primes); i++) {
5151
if (doubling_primes[i] >= x) {
5252
return doubling_primes[i];
5353
}

py/mpz.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ mp_int_t mpz_hash(const mpz_t *z) {
13701370
}
13711371

13721372
bool mpz_as_int_checked(const mpz_t *i, mp_int_t *value) {
1373-
mp_int_t val = 0;
1373+
mp_uint_t val = 0;
13741374
mpz_dig_t *d = i->dig + i->len;
13751375

13761376
while (d-- > i->dig) {

py/obj.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,11 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index,
393393
if (is_slice) {
394394
if (i < 0) {
395395
i = 0;
396-
} else if (i > len) {
396+
} else if ((mp_uint_t)i > len) {
397397
i = len;
398398
}
399399
} else {
400-
if (i < 0 || i >= len) {
400+
if (i < 0 || (mp_uint_t)i >= len) {
401401
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
402402
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "index out of range"));
403403
} else {

0 commit comments

Comments
 (0)