From 117ae267046ed564684793270b82767c585c724b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 9 Jan 2024 21:09:56 -0500 Subject: Avoid regressions in clang nullability checks Clang 15 (and still as of 16) lost the ability to understand null checks in conditionals, which is supposed to suppress these warnings. For now, work around some, and suppress others. The suppression boilerplate here is noisy and ugly, and hopefully temporary. It should be removed once the issue is fixed in clang. See https://github.com/llvm/llvm-project/issues/63018 --- .clang-format | 2 ++ src/uri.c | 9 +++++++++ src/warnings.h | 23 +++++++++++++++++++++++ src/writer.c | 16 +++++++++++++++- test/test_node.c | 8 +++++--- test/test_uri.c | 4 +++- 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/warnings.h diff --git a/.clang-format b/.clang-format index caf02057..26b10123 100644 --- a/.clang-format +++ b/.clang-format @@ -34,5 +34,7 @@ AttributeMacros: StatementMacros: - SERD_DEPRECATED_BY - SERD_LOG_FUNC + - SERD_DISABLE_NULL_WARNINGS + - SERD_RESTORE_WARNINGS - _Pragma ... diff --git a/src/uri.c b/src/uri.c index b45f7e16..897d0462 100644 --- a/src/uri.c +++ b/src/uri.c @@ -3,6 +3,7 @@ #include "string_utils.h" #include "uri_utils.h" +#include "warnings.h" #include "serd/serd.h" @@ -336,6 +337,8 @@ write_path_tail(SerdSink sink, const SerdURI* const uri, const size_t i) { + SERD_DISABLE_NULL_WARNINGS + size_t len = 0; if (i < uri->path_base.len) { len += sink(uri->path_base.buf + i, uri->path_base.len - i, stream); @@ -351,6 +354,8 @@ write_path_tail(SerdSink sink, } return len; + + SERD_RESTORE_WARNINGS } /** Write the path of `uri` relative to the path of `base`. */ @@ -419,6 +424,8 @@ serd_uri_serialise_relative(const SerdURI* const uri, len = write_rel_path(sink, stream, uri, base); } + SERD_DISABLE_NULL_WARNINGS + if (!relative || (!len && base->query.buf)) { if (uri->scheme.buf) { len += sink(uri->scheme.buf, uri->scheme.len, stream); @@ -452,6 +459,8 @@ serd_uri_serialise_relative(const SerdURI* const uri, len += sink(uri->fragment.buf, uri->fragment.len, stream); } + SERD_RESTORE_WARNINGS + return len; } diff --git a/src/warnings.h b/src/warnings.h new file mode 100644 index 00000000..4fdec095 --- /dev/null +++ b/src/warnings.h @@ -0,0 +1,23 @@ +// Copyright 2019-2024 David Robillard +// SPDX-License-Identifier: ISC + +#ifndef SERD_SRC_WARNINGS_H +#define SERD_SRC_WARNINGS_H + +#if defined(__clang__) + +/// Clang 15 null checking regressed, so we need to suppress it sometimes +# define SERD_DISABLE_NULL_WARNINGS \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wnullable-to-nonnull-conversion\"") + +# define SERD_RESTORE_WARNINGS _Pragma("clang diagnostic pop") + +#else + +# define SERD_DISABLE_NULL_WARNINGS +# define SERD_RESTORE_WARNINGS + +#endif + +#endif // SERD_SRC_WARNINGS_H diff --git a/src/writer.c b/src/writer.c index 2a6d7c31..bdcb8e8e 100644 --- a/src/writer.c +++ b/src/writer.c @@ -8,6 +8,7 @@ #include "string_utils.h" #include "try.h" #include "uri_utils.h" +#include "warnings.h" #include "serd/serd.h" @@ -762,7 +763,9 @@ write_uri_node(SerdWriter* const writer, SerdURI uri; SerdURI abs_uri; serd_env_get_base_uri(writer->env, &in_base_uri); + SERD_DISABLE_NULL_WARNINGS serd_uri_parse(node->buf, &uri); + SERD_RESTORE_WARNINGS serd_uri_resolve(&uri, &in_base_uri, &abs_uri); bool rooted = uri_is_under(&writer->base_uri, &writer->root_uri); SerdURI* root = rooted ? &writer->root_uri : &writer->base_uri; @@ -976,6 +979,8 @@ serd_writer_write_statement(SerdWriter* writer, return SERD_SUCCESS; } + SERD_DISABLE_NULL_WARNINGS + // Separate graphs if necessary if ((graph && !serd_node_equals(graph, &writer->context.graph)) || (!graph && writer->context.graph.type)) { @@ -989,6 +994,8 @@ serd_writer_write_statement(SerdWriter* writer, } } + SERD_RESTORE_WARNINGS + if ((flags & SERD_LIST_CONT)) { // Continue a list if (!strcmp((const char*)predicate->buf, NS_RDF "first") && @@ -1106,11 +1113,14 @@ serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) TRY(st, write_sep(writer, SEP_ANON_END)); pop_context(writer); - if (serd_node_equals(node, &writer->context.subject)) { + SERD_DISABLE_NULL_WARNINGS + + if (node && serd_node_equals(node, &writer->context.subject)) { // Now-finished anonymous node is the new subject with no other context writer->context.predicate.type = SERD_NOTHING; } + SERD_RESTORE_WARNINGS return st; } @@ -1201,7 +1211,9 @@ serd_writer_set_root_uri(SerdWriter* writer, const SerdNode* uri) if (uri && uri->buf) { writer->root_node = serd_node_copy(uri); + SERD_DISABLE_NULL_WARNINGS serd_uri_parse(uri->buf, &writer->root_uri); + SERD_RESTORE_WARNINGS } else { writer->root_node = SERD_NODE_NULL; writer->root_uri = SERD_URI_NULL; @@ -1240,7 +1252,9 @@ serd_writer_free(SerdWriter* writer) return; } + SERD_DISABLE_NULL_WARNINGS serd_writer_finish(writer); + SERD_RESTORE_WARNINGS free_context(&writer->context); free_anon_stack(writer); serd_stack_free(&writer->anon_stack); diff --git a/test/test_node.c b/test/test_node.c index 67958cc5..f08363cb 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -121,14 +121,16 @@ test_blob_to_node(void) data[i] = (uint8_t)((size + i) % 256); } - SerdNode blob = serd_node_new_blob(data, size, size % 5); + SerdNode blob = serd_node_new_blob(data, size, size % 5); + const uint8_t* const blob_str = blob.buf; + assert(blob_str); assert(blob.n_bytes == blob.n_chars); - assert(blob.n_bytes == strlen((const char*)blob.buf)); + assert(blob.n_bytes == strlen((const char*)blob_str)); size_t out_size = 0; uint8_t* out = - (uint8_t*)serd_base64_decode(blob.buf, blob.n_bytes, &out_size); + (uint8_t*)serd_base64_decode(blob_str, blob.n_bytes, &out_size); assert(out_size == size); for (size_t i = 0; i < size; ++i) { diff --git a/test/test_uri.c b/test/test_uri.c index ac24a2be..cc81b40e 100644 --- a/test/test_uri.c +++ b/test/test_uri.c @@ -48,7 +48,9 @@ test_file_uri(const char* const hostname, SerdNode node = serd_node_new_file_uri(USTR(path), USTR(hostname), 0, escape); uint8_t* out_hostname = NULL; - uint8_t* out_path = serd_file_uri_parse(node.buf, &out_hostname); + uint8_t* out_path = + serd_file_uri_parse((const uint8_t*)node.buf, &out_hostname); + assert(!strcmp((const char*)node.buf, expected_uri)); assert((hostname && out_hostname) || (!hostname && !out_hostname)); assert(!hostname || !strcmp(hostname, (const char*)out_hostname)); -- cgit v1.2.1