Skip to content

Commit

Permalink
Merge branch 'Explorer09-attr-nonnull'
Browse files Browse the repository at this point in the history
  • Loading branch information
natoscott committed Oct 10, 2023
2 parents 83041f3 + 5736fba commit ca41c25
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,31 @@ in the source distribution for its full text.
#ifdef __GNUC__ // defined by GCC and Clang

#define ATTR_FORMAT(type, index, check) __attribute__((format (type, index, check)))
#define ATTR_NONNULL __attribute__((nonnull))
#define ATTR_NORETURN __attribute__((noreturn))
#define ATTR_UNUSED __attribute__((unused))
#define ATTR_MALLOC __attribute__((malloc))

#else /* __GNUC__ */

#define ATTR_FORMAT(type, index, check)
#define ATTR_NONNULL
#define ATTR_NORETURN
#define ATTR_UNUSED
#define ATTR_MALLOC

#endif /* __GNUC__ */

#ifdef HAVE_ATTR_NONNULL

#define ATTR_NONNULL __attribute__((nonnull))
#define ATTR_NONNULL_N(...) __attribute__((nonnull(__VA_ARGS__)))

#else

#define ATTR_NONNULL
#define ATTR_NONNULL_N(...)

#endif /* HAVE_ATTR_NONNULL */

#ifdef HAVE_ATTR_ALLOC_SIZE

#define ATTR_ALLOC_SIZE1(a) __attribute__((alloc_size (a)))
Expand Down
8 changes: 8 additions & 0 deletions XUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,33 @@ void* xReallocArrayZero(void* ptr, size_t prevmemb, size_t newmemb, size_t size)
* String_startsWith gives better performance if strlen(match) can be computed
* at compile time (e.g. when they are immutable string literals). :)
*/
ATTR_NONNULL
static inline bool String_startsWith(const char* s, const char* match) {
return strncmp(s, match, strlen(match)) == 0;
}

bool String_contains_i(const char* s1, const char* s2, bool multi);

ATTR_NONNULL
static inline bool String_eq(const char* s1, const char* s2) {
return strcmp(s1, s2) == 0;
}

ATTR_NONNULL
char* String_cat(const char* s1, const char* s2) ATTR_MALLOC;

ATTR_NONNULL
char* String_trim(const char* in) ATTR_MALLOC;

ATTR_NONNULL_N(1)
char** String_split(const char* s, char sep, size_t* n);

void String_freeArray(char** s);

ATTR_NONNULL
char* String_readLine(FILE* fd) ATTR_MALLOC;

ATTR_NONNULL
static inline char* String_strchrnul(const char* s, int c) {
#ifdef HAVE_STRCHRNUL
return strchrnul(s, c);
Expand All @@ -73,6 +80,7 @@ ATTR_ACCESS3_R(2, 3)
size_t String_safeStrncpy(char* restrict dest, const char* restrict src, size_t size);

ATTR_FORMAT(printf, 2, 3)
ATTR_NONNULL_N(1, 2)
int xAsprintf(char** strp, const char* fmt, ...);

ATTR_FORMAT(printf, 3, 4)
Expand Down
16 changes: 16 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ AC_COMPILE_IFELSE([
AC_MSG_RESULT(no))
CFLAGS="$old_CFLAGS"

AC_MSG_CHECKING(for nonnull)
old_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -Wno-error -Werror=attributes"
AC_COMPILE_IFELSE([
AC_LANG_SOURCE(
[[
/* Attribute supported in GCC 3.3 or later */
__attribute__((nonnull)) int my_strcmp(const char* a, const char* b);
__attribute__((nonnull(1))) long my_strtol(const char* str, char** endptr, int base);
]]
)],
AC_DEFINE([HAVE_ATTR_NONNULL], 1, [The nonnull attribute is supported.])
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))
CFLAGS="$old_CFLAGS"

AC_MSG_CHECKING(for NaN support)
dnl Note: AC_RUN_IFELSE does not try compiling the program at all when
dnl $cross_compiling is 'yes'.
Expand Down

0 comments on commit ca41c25

Please sign in to comment.