From b45b6b442f0e3f821921a48f957944f485adfd1b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 10 Sep 2021 20:11:27 -0400 Subject: Fix conversion warnings --- benchmark/dict_bench.c | 4 ++-- benchmark/tree_bench.c | 45 ++++++++++++++++++++++++--------------------- include/zix/sem.h | 5 +++-- meson.build | 3 +-- src/btree.c | 2 +- test/btree_test.c | 2 +- test/test_data.h | 13 ++++++++++++- test/tree_test.c | 50 +++++++++++++++++++++++++++----------------------- 8 files changed, 71 insertions(+), 53 deletions(-) diff --git a/benchmark/dict_bench.c b/benchmark/dict_bench.c index 90b0abc..3ba33f9 100644 --- a/benchmark/dict_bench.c +++ b/benchmark/dict_bench.c @@ -163,7 +163,7 @@ main(int argc, char** argv) // GHashTable struct timespec search_start = bench_start(); for (size_t i = 0; i < n; ++i) { - const size_t index = lcg64(seed + i) % n; + const size_t index = (size_t)(lcg64(seed + i) % n); char* match = (char*)g_hash_table_lookup(hash, strings[index]); if (!!strcmp(match, strings[index])) { return test_fail("Bad match for `%s'\n", strings[index]); @@ -174,7 +174,7 @@ main(int argc, char** argv) // ZixHash search_start = bench_start(); for (size_t i = 0; i < n; ++i) { - const size_t index = lcg64(seed + i) % n; + const size_t index = (size_t)(lcg64(seed + i) % n); const ZixChunk key = {strings[index], lengths[index] + 1}; const ZixChunk* match = NULL; if (!(match = (const ZixChunk*)zix_hash_find(zhash, &key))) { diff --git a/benchmark/tree_bench.c b/benchmark/tree_bench.c index 4da7228..f57538d 100644 --- a/benchmark/tree_bench.c +++ b/benchmark/tree_bench.c @@ -87,17 +87,18 @@ bench_zix_tree(size_t n_elems, { start_test("ZixTree"); - intptr_t r = 0; + uintptr_t r = 0; ZixTreeIter* ti = NULL; ZixTree* t = zix_tree_new(false, int_cmp, NULL, NULL); // Insert n_elems elements struct timespec insert_start = bench_start(); for (size_t i = 0; i < n_elems; i++) { - r = unique_rand(i); - int status = zix_tree_insert(t, (void*)r, &ti); + r = unique_rand(i); + + ZixStatus status = zix_tree_insert(t, (void*)r, &ti); if (status) { - return test_fail("Failed to insert %" PRIdPTR "\n", r); + return test_fail("Failed to insert %" PRIuPTR "\n", r); } } fprintf(insert_dat, "\t%lf", bench_end(&insert_start)); @@ -107,10 +108,10 @@ bench_zix_tree(size_t n_elems, for (size_t i = 0; i < n_elems; i++) { r = unique_rand(i); if (zix_tree_find(t, (void*)r, &ti)) { - return test_fail("Failed to find %" PRIdPTR "\n", r); + return test_fail("Failed to find %" PRIuPTR "\n", r); } - if ((intptr_t)zix_tree_get(ti) != r) { - return test_fail("Failed to get %" PRIdPTR "\n", r); + if ((uintptr_t)zix_tree_get(ti) != r) { + return test_fail("Failed to get %" PRIuPTR "\n", r); } } fprintf(search_dat, "\t%lf", bench_end(&search_start)); @@ -131,10 +132,10 @@ bench_zix_tree(size_t n_elems, ZixTreeIter* item = NULL; if (zix_tree_find(t, (void*)r, &item)) { - return test_fail("Failed to find %" PRIdPTR " to delete\n", r); + return test_fail("Failed to find %" PRIuPTR " to delete\n", r); } if (zix_tree_remove(t, item)) { - return test_fail("Failed to remove %" PRIdPTR "\n", r); + return test_fail("Failed to remove %" PRIuPTR "\n", r); } } fprintf(del_dat, "\t%lf", bench_end(&del_start)); @@ -153,17 +154,18 @@ bench_zix_btree(size_t n_elems, { start_test("ZixBTree"); - intptr_t r = 0; + uintptr_t r = 0u; ZixBTreeIter* ti = NULL; ZixBTree* t = zix_btree_new(int_cmp, NULL, NULL); // Insert n_elems elements struct timespec insert_start = bench_start(); for (size_t i = 0; i < n_elems; i++) { - r = unique_rand(i); - int status = zix_btree_insert(t, (void*)r); + r = unique_rand(i); + + ZixStatus status = zix_btree_insert(t, (void*)r); if (status) { - return test_fail("Failed to insert %" PRIdPTR "\n", r); + return test_fail("Failed to insert %" PRIuPTR "\n", r); } } fprintf(insert_dat, "\t%lf", bench_end(&insert_start)); @@ -173,10 +175,10 @@ bench_zix_btree(size_t n_elems, for (size_t i = 0; i < n_elems; i++) { r = unique_rand(i); if (zix_btree_find(t, (void*)r, &ti)) { - return test_fail("Failed to find %" PRIdPTR "\n", r); + return test_fail("Failed to find %" PRIuPTR "\n", r); } - if ((intptr_t)zix_btree_get(ti) != r) { - return test_fail("Failed to get %" PRIdPTR "\n", r); + if ((uintptr_t)zix_btree_get(ti) != r) { + return test_fail("Failed to get %" PRIuPTR "\n", r); } } fprintf(search_dat, "\t%lf", bench_end(&search_start)); @@ -198,7 +200,7 @@ bench_zix_btree(size_t n_elems, void* removed = NULL; if (zix_btree_remove(t, (void*)r, &removed, NULL)) { - return test_fail("Failed to remove %" PRIdPTR "\n", r); + return test_fail("Failed to remove %" PRIuPTR "\n", r); } } fprintf(del_dat, "\t%lf", bench_end(&del_start)); @@ -217,17 +219,18 @@ bench_glib(size_t n_elems, { start_test("GSequence"); - intptr_t r = 0; + uintptr_t r = 0u; GSequence* t = g_sequence_new(NULL); // Insert n_elems elements struct timespec insert_start = bench_start(); for (size_t i = 0; i < n_elems; ++i) { r = unique_rand(i); + GSequenceIter* iter = g_sequence_insert_sorted(t, (void*)r, g_int_cmp, NULL); if (!iter || g_sequence_iter_is_end(iter)) { - return test_fail("Failed to insert %" PRIdPTR "\n", r); + return test_fail("Failed to insert %" PRIuPTR "\n", r); } } fprintf(insert_dat, "\t%lf", bench_end(&insert_start)); @@ -238,7 +241,7 @@ bench_glib(size_t n_elems, r = unique_rand(i); GSequenceIter* iter = g_sequence_lookup(t, (void*)r, g_int_cmp, NULL); if (!iter || g_sequence_iter_is_end(iter)) { - return test_fail("Failed to find %" PRIdPTR "\n", r); + return test_fail("Failed to find %" PRIuPTR "\n", r); } } fprintf(search_dat, "\t%lf", bench_end(&search_start)); @@ -258,7 +261,7 @@ bench_glib(size_t n_elems, r = unique_rand(i); GSequenceIter* iter = g_sequence_lookup(t, (void*)r, g_int_cmp, NULL); if (!iter || g_sequence_iter_is_end(iter)) { - return test_fail("Failed to remove %" PRIdPTR "\n", r); + return test_fail("Failed to remove %" PRIuPTR "\n", r); } g_sequence_remove(iter); } diff --git a/include/zix/sem.h b/include/zix/sem.h index 5b018fc..c773ef1 100644 --- a/include/zix/sem.h +++ b/include/zix/sem.h @@ -108,7 +108,8 @@ struct ZixSemImpl { static inline ZixStatus zix_sem_init(ZixSem* sem, unsigned val) { - return semaphore_create(mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, val) + return semaphore_create( + mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, (int)val) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; } @@ -150,7 +151,7 @@ struct ZixSemImpl { static inline ZixStatus zix_sem_init(ZixSem* sem, unsigned initial) { - sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL); + sem->sem = CreateSemaphore(NULL, (LONG)initial, LONG_MAX, NULL); return (sem->sem) ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR; } diff --git a/meson.build b/meson.build index bc32e80..8be6321 100644 --- a/meson.build +++ b/meson.build @@ -28,7 +28,6 @@ if get_option('strict') c_warnings = [ '-Weverything', '-Wno-bad-function-cast', - '-Wno-conversion', '-Wno-padded', '-Wno-reserved-id-macro', ] @@ -44,7 +43,7 @@ if get_option('strict') '-Wattribute-alias=2', '-Wcast-align=strict', '-Wcast-qual', - # '-Wconversion', + '-Wconversion', '-Wdate-time', '-Wdisabled-optimization', '-Wdouble-promotion', diff --git a/src/btree.c b/src/btree.c index 9e6d285..49b7516 100644 --- a/src/btree.c +++ b/src/btree.c @@ -581,7 +581,7 @@ zix_btree_remove_max(ZixBTree* const t, ZixBTreeNode* n) while (!n->is_leaf) { if (zix_btree_node_is_minimal(zix_btree_child(n, n->n_vals))) { // Leftmost child is minimal, must expand - if (!zix_btree_node_is_minimal(zix_btree_child(n, n->n_vals - 1))) { + if (!zix_btree_node_is_minimal(zix_btree_child(n, n->n_vals - 1u))) { // Child's left sibling has at least one key to steal n = zix_btree_rotate_right(n, n->n_vals); } else { diff --git a/test/btree_test.c b/test/btree_test.c index 5184392..6914053 100644 --- a/test/btree_test.c +++ b/test/btree_test.c @@ -515,7 +515,7 @@ main(int argc, char** argv) const unsigned n_tests = 3u; const size_t n_elems = (argc > 1) ? strtoul(argv[1], NULL, 10) : 524288u; - printf("Running %u tests with %lu elements", n_tests, n_elems); + printf("Running %u tests with %" PRIuPTR " elements", n_tests, n_elems); for (unsigned i = 0; i < n_tests; ++i) { printf("."); fflush(stdout); diff --git a/test/test_data.h b/test/test_data.h index 9c9a51d..405b716 100644 --- a/test/test_data.h +++ b/test/test_data.h @@ -40,6 +40,17 @@ lcg64(const uint64_t i) return (a * i) + c; } +/// Linear Congruential Generator for making random pointer-sized integers +static inline uintptr_t +lcg(const uintptr_t i) +{ +#if UINTPTR_MAX >= UINT64_MAX + return lcg64(i); +#else + return lcg32(i); +#endif +} + /// Return a pseudo-pseudo-pseudo-random-ish integer with no duplicates static inline size_t unique_rand(size_t i) @@ -52,7 +63,7 @@ unique_rand(size_t i) return i; // Values >= prime are mapped to themselves } - const size_t residue = ((uint64_t)i * i) % prime; + const size_t residue = (size_t)(((uint64_t)i * i) % prime); return (i <= prime / 2) ? residue : prime - residue; } diff --git a/test/tree_test.c b/test/tree_test.c index 4b044a1..7368e10 100644 --- a/test/tree_test.c +++ b/test/tree_test.c @@ -31,8 +31,8 @@ static unsigned seed = 1; static int int_cmp(const void* a, const void* b, const void* ZIX_UNUSED(user_data)) { - const intptr_t ia = (intptr_t)a; - const intptr_t ib = (intptr_t)b; + const uintptr_t ia = (uintptr_t)a; + const uintptr_t ib = (uintptr_t)b; return ia < ib ? -1 : ia > ib ? 1 : 0; } @@ -47,7 +47,7 @@ ith_elem(unsigned test_num, size_t n_elems, size_t i) return n_elems - i; // Decreasing (worse case) case 2: default: - return lcg64(seed + i) % 100; // Random + return lcg(seed + i) % 100u; // Random } } @@ -60,22 +60,24 @@ test_fail(void) static int stress(unsigned test_num, size_t n_elems) { - intptr_t r = 0; + uintptr_t r = 0u; ZixTreeIter* ti = NULL; ZixTree* t = zix_tree_new(true, int_cmp, NULL, NULL); // Insert n_elems elements for (size_t i = 0; i < n_elems; ++i) { - r = ith_elem(test_num, n_elems, i); - int status = zix_tree_insert(t, (void*)r, &ti); + r = ith_elem(test_num, n_elems, i); + + ZixStatus status = zix_tree_insert(t, (void*)r, &ti); if (status) { fprintf(stderr, "Insert failed\n"); return test_fail(); } - if ((intptr_t)zix_tree_get(ti) != r) { + + if ((uintptr_t)zix_tree_get(ti) != r) { fprintf(stderr, - "Data corrupt (%" PRIdPTR " != %" PRIdPTR ")\n", - (intptr_t)zix_tree_get(ti), + "Data corrupt (%" PRIuPTR " != %" PRIuPTR ")\n", + (uintptr_t)zix_tree_get(ti), r); return test_fail(); } @@ -97,24 +99,24 @@ stress(unsigned test_num, size_t n_elems) fprintf(stderr, "Find failed\n"); return test_fail(); } - if ((intptr_t)zix_tree_get(ti) != r) { + if ((uintptr_t)zix_tree_get(ti) != r) { fprintf(stderr, - "Data corrupt (%" PRIdPTR " != %" PRIdPTR ")\n", - (intptr_t)zix_tree_get(ti), + "Data corrupt (%" PRIuPTR " != %" PRIuPTR ")\n", + (uintptr_t)zix_tree_get(ti), r); return test_fail(); } } // Iterate over all elements - size_t i = 0; - intptr_t last = -1; + size_t i = 0; + uintptr_t last = 0; for (ZixTreeIter* iter = zix_tree_begin(t); !zix_tree_iter_is_end(iter); iter = zix_tree_iter_next(iter), ++i) { - const intptr_t iter_data = (intptr_t)zix_tree_get(iter); + const uintptr_t iter_data = (uintptr_t)zix_tree_get(iter); if (iter_data < last) { fprintf(stderr, - "Iter corrupt (%" PRIdPTR " < %" PRIdPTR ")\n", + "Iter corrupt (%" PRIuPTR " < %" PRIuPTR ")\n", iter_data, last); return test_fail(); @@ -134,10 +136,10 @@ stress(unsigned test_num, size_t n_elems) last = INTPTR_MAX; for (ZixTreeIter* iter = zix_tree_rbegin(t); !zix_tree_iter_is_rend(iter); iter = zix_tree_iter_prev(iter), ++i) { - const intptr_t iter_data = (intptr_t)zix_tree_get(iter); + const uintptr_t iter_data = (uintptr_t)zix_tree_get(iter); if (iter_data > last) { fprintf(stderr, - "Iter corrupt (%" PRIdPTR " < %" PRIdPTR ")\n", + "Iter corrupt (%" PRIuPTR " < %" PRIuPTR ")\n", iter_data, last); return test_fail(); @@ -167,16 +169,18 @@ stress(unsigned test_num, size_t n_elems) // Insert n_elems elements again (to test non-empty destruction) for (size_t e = 0; e < n_elems; ++e) { - r = ith_elem(test_num, n_elems, e); - int status = zix_tree_insert(t, (void*)r, &ti); + r = ith_elem(test_num, n_elems, e); + + ZixStatus status = zix_tree_insert(t, (void*)r, &ti); if (status) { fprintf(stderr, "Insert failed\n"); return test_fail(); } - if ((intptr_t)zix_tree_get(ti) != r) { + + if ((uintptr_t)zix_tree_get(ti) != r) { fprintf(stderr, - "Data corrupt (%" PRIdPTR " != %" PRIdPTR ")\n", - (intptr_t)zix_tree_get(ti), + "Data corrupt (%" PRIuPTR " != %" PRIuPTR ")\n", + (uintptr_t)zix_tree_get(ti), r); return test_fail(); } -- cgit v1.2.1