Skip to content

Commit

Permalink
lib: make assertions visible to clang static analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
ebiggers committed Mar 18, 2024
1 parent 93e41f8 commit 1ef0db7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
7 changes: 7 additions & 0 deletions common_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ typedef size_t machine_word_t;
# define MAYBE_UNUSED
#endif

/* NORETURN - mark a function as never returning, e.g. due to calling abort() */
#if defined(__GNUC__) || __has_attribute(noreturn)
# define NORETURN __attribute__((noreturn))
#else
# define NORETURN
#endif

/*
* restrict - hint that writes only occur through the given pointer.
*
Expand Down
12 changes: 10 additions & 2 deletions lib/lib_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,23 @@ int memcmp(const void *s1, const void *s2, size_t n);

#undef LIBDEFLATE_ENABLE_ASSERTIONS
#else
#include <string.h>
# include <string.h>
/*
* To prevent false positive static analyzer warnings, ensure that assertions
* are visible to the static analyzer.
*/
# ifdef __clang_analyzer__
# define LIBDEFLATE_ENABLE_ASSERTIONS
# endif
#endif

/*
* Runtime assertion support. Don't enable this in production builds; it may
* hurt performance significantly.
*/
#ifdef LIBDEFLATE_ENABLE_ASSERTIONS
void libdeflate_assertion_failed(const char *expr, const char *file, int line);
NORETURN void
libdeflate_assertion_failed(const char *expr, const char *file, int line);
#define ASSERT(expr) { if (unlikely(!(expr))) \
libdeflate_assertion_failed(#expr, __FILE__, __LINE__); }
#else
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ memcmp(const void *s1, const void *s2, size_t n)
#ifdef LIBDEFLATE_ENABLE_ASSERTIONS
#include <stdio.h>
#include <stdlib.h>
void
NORETURN void
libdeflate_assertion_failed(const char *expr, const char *file, int line)
{
fprintf(stderr, "Assertion failed: %s at %s:%d\n", expr, file, line);
Expand Down
2 changes: 1 addition & 1 deletion programs/test_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#endif

/* Abort with an error message */
_noreturn void
NORETURN void
assertion_failed(const char *expr, const char *file, int line)
{
msg("Assertion failed: %s at %s:%d", expr, file, line);
Expand Down
8 changes: 1 addition & 7 deletions programs/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@

#include <zlib.h> /* for comparison purposes */

#if defined(__GNUC__) || __has_attribute(noreturn)
# define _noreturn __attribute__((noreturn))
#else
# define _noreturn
#endif

void _noreturn
NORETURN void
assertion_failed(const char *expr, const char *file, int line);

#define ASSERT(expr) { if (unlikely(!(expr))) \
Expand Down

0 comments on commit 1ef0db7

Please sign in to comment.