From 1997ab63fb66a55341de0311e1b309419a32d724 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 31 Dec 2018 11:48:42 -0500 Subject: Fix various warnings and conversion issues --- src/base64.c | 8 ++++---- src/model.c | 6 +++--- src/n3.c | 4 ++-- src/node.c | 14 +++----------- src/reader.c | 2 +- src/serdi.c | 12 ++++++------ src/stack.h | 2 +- src/string.c | 8 ++++---- src/string_utils.h | 8 ++++---- src/uri.c | 7 ++++--- src/world.c | 2 +- src/writer.c | 8 ++++---- tests/model_test.c | 4 ++-- tests/serd_test.c | 34 ++++++++++++++++++++++++---------- wscript | 10 ---------- 15 files changed, 63 insertions(+), 66 deletions(-) diff --git a/src/base64.c b/src/base64.c index c9e958c2..bcf6b361 100644 --- a/src/base64.c +++ b/src/base64.c @@ -39,7 +39,7 @@ static const uint8_t b64_map[] = for decoding, shifted up by 47 to be in the range of printable ASCII. A '$' is a placeholder for characters not in the base64 alphabet. */ -static const char b64_unmap[] = +static const uint8_t b64_unmap[] = "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$m$$$ncdefghijkl$$$$$$" "$/0123456789:;<=>?@ABCDEFGH$$$$$$IJKLMNOPQRSTUVWXYZ[\\]^_`ab$$$$" "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" @@ -96,7 +96,7 @@ serd_base64_encode(char* const str, static inline uint8_t unmap(const uint8_t in) { - return (uint8_t)(b64_unmap[in] - 47); + return (uint8_t)(b64_unmap[in] - 47u); } /** Decode 4 base64 characters to 3 raw bytes. */ @@ -106,7 +106,7 @@ decode_chunk(const uint8_t in[4], uint8_t out[3]) out[0] = (uint8_t)(((unmap(in[0]) << 2)) | unmap(in[1]) >> 4); out[1] = (uint8_t)(((unmap(in[1]) << 4) & 0xF0) | unmap(in[2]) >> 2); out[2] = (uint8_t)(((unmap(in[2]) << 6) & 0xC0) | unmap(in[3])); - return 1 + (in[2] != '=') + ((in[2] != '=') && (in[3] != '=')); + return 1u + (in[2] != '=') + ((in[2] != '=') && (in[3] != '=')); } SerdStatus @@ -119,7 +119,7 @@ serd_base64_decode(void* buf, size_t* size, const char* str, size_t len) uint8_t in[] = "===="; size_t n_in = 0; for (; i < len && n_in < 4; ++n_in) { - for (; i < len && !is_base64(ustr[i]); ++i) {} // Skip junk + for (; i < len && !is_base64(str[i]); ++i) {} // Skip junk in[n_in] = ustr[i++]; } if (n_in > 1) { diff --git a/src/model.c b/src/model.c index cdd44ac1..7e0ab2a8 100644 --- a/src/model.c +++ b/src/model.c @@ -110,9 +110,9 @@ serd_model_best_index(const SerdModel* model, { const bool graph_search = (pat[SERD_GRAPH] != 0); - const unsigned sig = ((pat[0] ? 1 : 0) * 0x100 + - (pat[1] ? 1 : 0) * 0x010 + - (pat[2] ? 1 : 0) * 0x001); + const unsigned sig = ((pat[0] ? 1u : 0u) * 0x100 + + (pat[1] ? 1u : 0u) * 0x010 + + (pat[2] ? 1u : 0u) * 0x001); SerdOrder good[2] = { (SerdOrder)-1, (SerdOrder)-1 }; diff --git a/src/n3.c b/src/n3.c index 5df6cc30..2094432b 100644 --- a/src/n3.c +++ b/src/n3.c @@ -291,14 +291,14 @@ read_ws_star(SerdReader* reader) } static inline bool -peek_delim(SerdReader* reader, const char delim) +peek_delim(SerdReader* reader, const uint8_t delim) { read_ws_star(reader); return peek_byte(reader) == delim; } static inline bool -eat_delim(SerdReader* reader, const char delim) +eat_delim(SerdReader* reader, const uint8_t delim) { if (peek_delim(reader, delim)) { eat_byte_safe(reader, delim); diff --git a/src/node.c b/src/node.c index ea571922..cc0b0a30 100644 --- a/src/node.c +++ b/src/node.c @@ -31,15 +31,6 @@ #include #include -#ifdef _WIN32 -# ifndef isnan -# define isnan(x) _isnan(x) -# endif -# ifndef isinf -# define isinf(x) (!_finite(x)) -# endif -#endif - static const size_t serd_node_align = sizeof(SerdNode); static SerdNode* @@ -107,7 +98,7 @@ serd_node_malloc(size_t n_bytes, SerdNodeFlags flags, SerdNodeType type) node->n_bytes = 0; node->flags = flags; node->type = type; - assert((intptr_t)node % serd_node_align == 0); + assert((uintptr_t)node % serd_node_align == 0u); return node; } @@ -467,6 +458,7 @@ is_uri_path_char(const char c) if (is_alpha(c) || is_digit(c)) { return true; } + switch (c) { case '-': case '.': case '_': case '~': // unreserved case ':': case '@': // pchar @@ -582,7 +574,7 @@ serd_digits(double abs) SerdNode* serd_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype) { - if (isnan(d) || isinf(d)) { + if (!isfinite(d)) { return NULL; } diff --git a/src/reader.c b/src/reader.c index d2ce61d3..d4c516eb 100644 --- a/src/reader.c +++ b/src/reader.c @@ -264,7 +264,7 @@ serd_reader_start_file(SerdReader* reader, const char* uri, bool bulk) (SerdStreamCloseFunc)fclose, fd, name, - bulk ? SERD_PAGE_SIZE : 1); + bulk ? SERD_PAGE_SIZE : 1u); serd_node_free(name); return st; } diff --git a/src/serdi.c b/src/serdi.c index 76496bac..15b97d04 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -206,14 +206,14 @@ main(int argc, char** argv) SerdEnv* env = serd_env_new(base); const SerdWriterFlags writer_flags = - ((ascii ? SERD_STYLE_ASCII : 0) | // - (full_uris ? (SERD_STYLE_UNQUALIFIED | SERD_STYLE_UNRESOLVED) : 0)); + ((ascii ? SERD_STYLE_ASCII : 0u) | // + (full_uris ? (SERD_STYLE_UNQUALIFIED | SERD_STYLE_UNRESOLVED) : 0u)); const SerdSerialisationFlags serialisation_flags = - no_inline ? SERD_NO_INLINE_OBJECTS : 0; + no_inline ? SERD_NO_INLINE_OBJECTS : 0u; SerdByteSink* byte_sink = serd_byte_sink_new( - (SerdWriteFunc)fwrite, out_fd, bulk_write ? 4096 : 1); + (SerdWriteFunc)fwrite, out_fd, bulk_write ? 4096u : 1u); SerdWriter* writer = serd_writer_new(world, output_syntax, @@ -228,8 +228,8 @@ main(int argc, char** argv) const SerdSink* sink = NULL; if (use_model) { const SerdModelFlags flags = - SERD_INDEX_SPO | (input_has_graphs ? SERD_INDEX_GRAPHS : 0) | - (no_inline ? 0 : SERD_INDEX_OPS); + SERD_INDEX_SPO | (input_has_graphs ? SERD_INDEX_GRAPHS : 0u) | + (no_inline ? 0u : SERD_INDEX_OPS); model = serd_model_new(world, flags); inserter = serd_inserter_new(model, env, NULL); sink = inserter; diff --git a/src/stack.h b/src/stack.h index c8716ebd..dbb196ae 100644 --- a/src/stack.h +++ b/src/stack.h @@ -48,7 +48,7 @@ serd_stack_new(size_t size) } static inline bool -serd_stack_is_empty(SerdStack* stack) +serd_stack_is_empty(const SerdStack* stack) { return stack->size <= SERD_STACK_BOTTOM; } diff --git a/src/string.c b/src/string.c index b465bf8d..1b106841 100644 --- a/src/string.c +++ b/src/string.c @@ -117,16 +117,16 @@ serd_strtod(const char* str, size_t* end) { double result = 0.0; -#define SET_END(index) do { if (end) { *end = index; } } while (0) +#define SET_END(index) do { if (end) { *end = (size_t)(index); } } while (0) if (!strcmp(str, "NaN")) { - SET_END(3); + SET_END(3u); return NAN; } else if (!strcmp(str, "-INF")) { - SET_END(4); + SET_END(4u); return -INFINITY; } else if (!strcmp(str, "INF")) { - SET_END(3); + SET_END(3u); return INFINITY; } diff --git a/src/string_utils.h b/src/string_utils.h index 7bbfd2b3..4bd36721 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -75,7 +75,7 @@ is_space(const char c) } static inline bool -is_base64(const char c) +is_base64(const int c) { return is_alpha(c) || is_digit(c) || c == '+' || c == '/' || c == '='; } @@ -120,9 +120,9 @@ utf8_num_bytes(const uint8_t c) static inline uint32_t parse_counted_utf8_char(const uint8_t* utf8, size_t size) { - uint32_t c = utf8[0] & ((1u << (8 - size)) - 1); + uint32_t c = utf8[0] & ((1u << (8u - size)) - 1u); for (size_t i = 1; i < size; ++i) { - const uint8_t in = utf8[i] & 0x3F; + const uint8_t in = utf8[i] & 0x3Fu; c = (c << 6) | in; } return c; @@ -137,7 +137,7 @@ parse_utf8_char(const uint8_t* utf8, size_t* size) return parse_counted_utf8_char(utf8, *size); default: *size = 0; - return 0; + return 0u; } } diff --git a/src/uri.c b/src/uri.c index 7b4b89e4..41a2074c 100644 --- a/src/uri.c +++ b/src/uri.c @@ -41,8 +41,9 @@ serd_file_uri_parse(const char* uri, char** hostname) return NULL; } if (hostname) { - *hostname = (char*)calloc((size_t)(path - auth + 1), 1); - memcpy(*hostname, auth, (size_t)(path - auth)); + const size_t len = (size_t)(path - auth); + *hostname = (char*)calloc(len + 1, 1); + memcpy(*hostname, auth, len); } } } @@ -59,7 +60,7 @@ serd_file_uri_parse(const char* uri, char** hostname) ++s; } else if (is_hexdig(*(s + 1)) && is_hexdig(*(s + 2))) { const char code[3] = {*(s + 1), *(s + 2), 0}; - const char c = (char)strtoul((const char*)code, NULL, 16); + const char c = (char)strtoul(code, NULL, 16); serd_buffer_sink(&c, 1, 1, &buffer); s += 2; } else { diff --git a/src/world.c b/src/world.c index 690ef2a4..45f31ea6 100644 --- a/src/world.c +++ b/src/world.c @@ -231,7 +231,7 @@ serd_world_get_blank(SerdWorld* world) { char* buf = serd_node_buffer(world->blank_node); memset(buf, 0, BLANK_CHARS + 1); - world->blank_node->n_bytes = snprintf( + world->blank_node->n_bytes = (size_t)snprintf( buf, BLANK_CHARS, "b%u", ++world->next_blank_id); return world->blank_node; } diff --git a/src/writer.c b/src/writer.c index 6b7e39b5..99a9beac 100644 --- a/src/writer.c +++ b/src/writer.c @@ -117,7 +117,7 @@ struct SerdWriterImpl { SerdLogFunc log_func; void* log_handle; WriteContext context; - unsigned indent; + int indent; char* bprefix; size_t bprefix_len; Sep last_sep; @@ -353,7 +353,7 @@ write_text(SerdWriter* writer, TextContext ctx, break; // Reached end } - const uint8_t in = utf8[i++]; + const char in = utf8[i++]; if (ctx == WRITE_LONG_STRING) { switch (in) { case '\\': len += sink("\\\\", 2, writer); continue; @@ -414,7 +414,7 @@ static void write_newline(SerdWriter* writer) { sink("\n", 1, writer); - for (unsigned i = 0; i < writer->indent; ++i) { + for (int i = 0; i < writer->indent; ++i) { sink("\t", 1, writer); } } @@ -426,7 +426,7 @@ write_sep(SerdWriter* writer, const Sep sep) // Adjust indent, but tolerate if it would become negative writer->indent = - ((rule->indent >= 0 || writer->indent >= (unsigned)-rule->indent) + ((rule->indent >= 0 || writer->indent >= -rule->indent) ? writer->indent + rule->indent : 0); diff --git a/tests/model_test.c b/tests/model_test.c index a6614ac1..aa3af753 100644 --- a/tests/model_test.c +++ b/tests/model_test.c @@ -606,7 +606,7 @@ static int test_triple_index_read(SerdWorld* world, const unsigned n_quads) { for (unsigned i = 0; i < 6; ++i) { - SerdModel* model = serd_model_new(world, (1 << i)); + SerdModel* model = serd_model_new(world, (1u << i)); generate(world, model, n_quads, 0); assert(!test_read(world, model, 0, n_quads)); serd_model_free(model); @@ -619,7 +619,7 @@ static int test_quad_index_read(SerdWorld* world, const unsigned n_quads) { for (unsigned i = 0; i < 6; ++i) { - SerdModel* model = serd_model_new(world, (1 << i) | SERD_INDEX_GRAPHS); + SerdModel* model = serd_model_new(world, (1u << i) | SERD_INDEX_GRAPHS); const SerdNode* graph = uri(world, 42); generate(world, model, n_quads, graph); assert(!test_read(world, model, graph, n_quads)); diff --git a/tests/serd_test.c b/tests/serd_test.c index 34463f7e..30f433fd 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -257,13 +257,27 @@ test_string_to_double(void) static void test_double_to_node(void) { - const double dbl_test_nums[] = { - 0.0, 9.0, 10.0, .01, 2.05, -16.00001, 5.000000005, 0.0000000001, NAN, INFINITY - }; - - const char* dbl_test_strs[] = { - "0.0", "9.0", "10.0", "0.01", "2.05", "-16.00001", "5.00000001", "0.0", NULL, NULL - }; + const double dbl_test_nums[] = { 0.0, + 9.0, + 10.0, + .01, + 2.05, + -16.00001, + 5.000000005, + 0.0000000001, + (double)NAN, + (double)INFINITY }; + + const char* dbl_test_strs[] = { "0.0", + "9.0", + "10.0", + "0.01", + "2.05", + "-16.00001", + "5.00000001", + "0.0", + NULL, + NULL }; for (size_t i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) { SerdNode* node = serd_new_decimal(dbl_test_nums[i], 8, NULL); @@ -665,9 +679,9 @@ test_writer(const char* const path) } // Write statements with bad UTF-8 (should be replaced) - const char bad_str[] = { (char)0xFF, (char)0x90, 'h', 'i', 0 }; - SerdNode* bad_lit = serd_new_string(bad_str); - SerdNode* bad_uri = serd_new_uri(bad_str); + const uint8_t bad_str[] = { 0xFF, 0x90, 'h', 'i', 0 }; + SerdNode* bad_lit = serd_new_string((const char*)bad_str); + SerdNode* bad_uri = serd_new_uri((const char*)bad_str); assert(!serd_sink_write(iface, 0, s, p, bad_lit, 0)); assert(!serd_sink_write(iface, 0, s, p, bad_uri, 0)); serd_node_free(bad_uri); diff --git a/wscript b/wscript index 22665436..b089e627 100644 --- a/wscript +++ b/wscript @@ -56,7 +56,6 @@ def configure(conf): autowaf.add_compiler_flags(conf.env, '*', { 'clang': [ '-Wno-cast-align', - '-Wno-cast-qual', '-Wno-covered-switch-default', '-Wno-disabled-macro-expansion', '-Wno-double-promotion', @@ -64,23 +63,14 @@ def configure(conf): '-Wno-implicit-fallthrough', '-Wno-padded', '-Wno-reserved-id-macro', - '-Wno-sign-conversion', ], 'gcc': [ '-Wno-cast-align', - '-Wno-cast-qual', - '-Wno-float-conversion', - '-Wno-inline', '-Wno-padded', - '-Wno-sign-conversion', - '-Wno-suggest-attribute=const', - '-Wno-suggest-attribute=malloc', - '-Wno-suggest-attribute=pure', ], 'msvc': [ '/wd4061', # enumerator in switch is not explicitly handled '/wd4200', # nonstandard: zero-sized array in struct/union - '/wd4365', # signed/unsigned mismatch '/wd4514', # unreferenced inline function has been removed '/wd4710', # function not inlined '/wd4711', # function selected for automatic inline expansion -- cgit v1.2.1