From 44182a52fda35320d4e2349b5999a0cac9ef55fd Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Thu, 10 Sep 2015 11:48:05 +0300 Subject: [PATCH] Fix 'comparison of non-null parameter is always false' warning (Clang) * alloc.c (GC_set_stop_func, GC_try_to_collect): Use NONNULL_ARG_NOT_NULL to check argument marked as non-null (in function declaration) is actually non-null. * finalize.c (GC_general_register_disappearing_link, GC_register_long_link, GC_move_disappearing_link, GC_move_long_link): Likewise. * misc.c (GC_set_warn_proc, GC_set_abort_func, GC_set_oom_fn): Likewise. * include/private/gc_priv.h (NONNULL_ARG_NOT_NULL): New macro. --- alloc.c | 4 ++-- finalize.c | 10 ++++++---- include/private/gc_priv.h | 8 ++++++++ misc.c | 6 +++--- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/alloc.c b/alloc.c index 2de0610f..c6b47669 100644 --- a/alloc.c +++ b/alloc.c @@ -149,7 +149,7 @@ STATIC GC_stop_func GC_default_stop_func = GC_never_stop_func; GC_API void GC_CALL GC_set_stop_func(GC_stop_func stop_func) { DCL_LOCK_STATE; - GC_ASSERT(stop_func != 0); + GC_ASSERT(NONNULL_ARG_NOT_NULL(stop_func)); LOCK(); GC_default_stop_func = stop_func; UNLOCK(); @@ -999,7 +999,7 @@ STATIC GC_bool GC_try_to_collect_general(GC_stop_func stop_func, /* Externally callable routines to invoke full, stop-the-world collection. */ GC_API int GC_CALL GC_try_to_collect(GC_stop_func stop_func) { - GC_ASSERT(stop_func != 0); + GC_ASSERT(NONNULL_ARG_NOT_NULL(stop_func)); return (int)GC_try_to_collect_general(stop_func, FALSE); } diff --git a/finalize.c b/finalize.c index c2e8af02..970775f2 100644 --- a/finalize.c +++ b/finalize.c @@ -211,7 +211,7 @@ STATIC int GC_register_disappearing_link_inner( GC_API int GC_CALL GC_general_register_disappearing_link(void * * link, const void * obj) { - if (((word)link & (ALIGNMENT-1)) != 0 || NULL == link) + if (((word)link & (ALIGNMENT-1)) != 0 || !NONNULL_ARG_NOT_NULL(link)) ABORT("Bad arg to GC_general_register_disappearing_link"); return GC_register_disappearing_link_inner(&GC_dl_hashtbl, link, obj); } @@ -266,7 +266,7 @@ GC_API int GC_CALL GC_unregister_disappearing_link(void * * link) #ifndef GC_LONG_REFS_NOT_NEEDED GC_API int GC_CALL GC_register_long_link(void * * link, const void * obj) { - if (((word)link & (ALIGNMENT-1)) != 0 || NULL == link) + if (((word)link & (ALIGNMENT-1)) != 0 || !NONNULL_ARG_NOT_NULL(link)) ABORT("Bad arg to GC_register_long_link"); return GC_register_disappearing_link_inner(&GC_ll_hashtbl, link, obj); } @@ -345,7 +345,8 @@ GC_API int GC_CALL GC_unregister_disappearing_link(void * * link) int result; DCL_LOCK_STATE; - if (((word)new_link & (ALIGNMENT-1)) != 0 || new_link == NULL) + if (((word)new_link & (ALIGNMENT-1)) != 0 + || !NONNULL_ARG_NOT_NULL(new_link)) ABORT("Bad new_link arg to GC_move_disappearing_link"); if (((word)link & (ALIGNMENT-1)) != 0) return GC_NOT_FOUND; /* Nothing to do. */ @@ -362,7 +363,8 @@ GC_API int GC_CALL GC_unregister_disappearing_link(void * * link) int result; DCL_LOCK_STATE; - if (((word)new_link & (ALIGNMENT-1)) != 0 || new_link == NULL) + if (((word)new_link & (ALIGNMENT-1)) != 0 + || !NONNULL_ARG_NOT_NULL(new_link)) ABORT("Bad new_link arg to GC_move_disappearing_link"); if (((word)link & (ALIGNMENT-1)) != 0) return GC_NOT_FOUND; /* Nothing to do. */ diff --git a/include/private/gc_priv.h b/include/private/gc_priv.h index dd2bf747..d267c7e5 100644 --- a/include/private/gc_priv.h +++ b/include/private/gc_priv.h @@ -2328,6 +2328,14 @@ GC_INNER ptr_t GC_store_debug_info(ptr_t p, word sz, const char *str, # define GC_STATIC_ASSERT(expr) (void)sizeof(char[(expr)? 1 : -1]) #endif +/* Runtime check for an argument declared as non-null is actually not null. */ +#if defined(__GNUC__) && __GNUC__ >= 4 + /* Workaround tautological-pointer-compare Clang warning. */ +# define NONNULL_ARG_NOT_NULL(arg) (*(volatile void **)&(arg) != NULL) +#else +# define NONNULL_ARG_NOT_NULL(arg) (NULL != (arg)) +#endif + #define COND_DUMP_CHECKS \ do { \ GC_ASSERT(GC_compute_large_free_bytes() == GC_large_free_bytes); \ diff --git a/misc.c b/misc.c index a5d7d28c..73c4e21e 100644 --- a/misc.c +++ b/misc.c @@ -1612,7 +1612,7 @@ GC_API void GC_CALLBACK GC_ignore_warn_proc(char *msg, GC_word arg) GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc p) { DCL_LOCK_STATE; - GC_ASSERT(p != 0); + GC_ASSERT(NONNULL_ARG_NOT_NULL(p)); # ifdef GC_WIN32_THREADS # ifdef CYGWIN32 /* Need explicit GC_INIT call */ @@ -1700,7 +1700,7 @@ GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void) GC_API void GC_CALL GC_set_abort_func(GC_abort_func fn) { DCL_LOCK_STATE; - GC_ASSERT(fn != 0); + GC_ASSERT(NONNULL_ARG_NOT_NULL(fn)); LOCK(); GC_on_abort = fn; UNLOCK(); @@ -1990,7 +1990,7 @@ GC_API GC_word GC_CALL GC_get_gc_no(void) GC_API void GC_CALL GC_set_oom_fn(GC_oom_func fn) { - GC_ASSERT(fn != 0); + GC_ASSERT(NONNULL_ARG_NOT_NULL(fn)); DCL_LOCK_STATE; LOCK(); GC_oom_fn = fn; -- 2.40.0