From 7107d1db0b3361a3e880d903a45920b64a05e9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Wed, 9 Sep 2020 21:35:15 +0200 Subject: [PATCH] Refactor __attribute__ usage Use internal macros for compatibility with non GNUC compilers. --- CRT.h | 4 +++- Macros.h | 16 ++++++++++++++++ XAlloc.c | 8 -------- XAlloc.h | 10 +--------- dragonflybsd/DragonFlyBSDProcessList.c | 4 +--- dragonflybsd/DragonFlyBSDProcessList.h | 2 -- zfs/ZfsArcStats.c | 4 +++- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/CRT.h b/CRT.h index 4199837e..d7d01e2c 100644 --- a/CRT.h +++ b/CRT.h @@ -7,6 +7,8 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ +#include "Macros.h" + #include #define KEY_WHEELUP KEY_F(20) @@ -107,7 +109,7 @@ typedef enum ColorElements_ { LAST_COLORELEMENT } ColorElements; -void CRT_fatalError(const char* note) __attribute__ ((noreturn)); +void CRT_fatalError(const char* note) ATTR_NORETURN; extern struct sigaction old_sigsegv_handler; void CRT_handleSIGSEGV(int sgn); diff --git a/Macros.h b/Macros.h index cb84b291..ef4e8908 100644 --- a/Macros.h +++ b/Macros.h @@ -13,4 +13,20 @@ #define CLAMP(x, low, high) (((x) > (high)) ? (high) : MAXIMUM(x, low)) #endif +#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)) + +#else /* __GNUC__ */ + +#define ATTR_FORMAT(type, index, check) +#define ATTR_NONNULL +#define ATTR_NORETURN +#define ATTR_UNUSED + +#endif /* __GNUC__ */ + #endif diff --git a/XAlloc.c b/XAlloc.c index 38616dfc..c0fa7547 100644 --- a/XAlloc.c +++ b/XAlloc.c @@ -53,14 +53,6 @@ void* xRealloc(void* ptr, size_t size) { # define xStrdup(str_) (assert(str_), xStrdup_(str_)) #endif -#ifndef __has_attribute // Clang's macro -# define __has_attribute(x) 0 -#endif -#if (__has_attribute(nonnull) || \ - ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))) -char* xStrdup_(const char* str) __attribute__((nonnull)); -#endif // __has_attribute(nonnull) || GNU C 3.3 or later - char* xStrdup_(const char* str) { char* data = strdup(str); if (!data) { diff --git a/XAlloc.h b/XAlloc.h index b9334238..3a2c658c 100644 --- a/XAlloc.h +++ b/XAlloc.h @@ -31,14 +31,6 @@ void* xRealloc(void* ptr, size_t size); # define xStrdup(str_) (assert(str_), xStrdup_(str_)) #endif -#ifndef __has_attribute // Clang's macro -# define __has_attribute(x) 0 -#endif -#if (__has_attribute(nonnull) || \ - ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))) -char* xStrdup_(const char* str) __attribute__((nonnull)); -#endif // __has_attribute(nonnull) || GNU C 3.3 or later - -char* xStrdup_(const char* str); +char* xStrdup_(const char* str) ATTR_NONNULL; #endif diff --git a/dragonflybsd/DragonFlyBSDProcessList.c b/dragonflybsd/DragonFlyBSDProcessList.c index cd5526a7..8901bc45 100644 --- a/dragonflybsd/DragonFlyBSDProcessList.c +++ b/dragonflybsd/DragonFlyBSDProcessList.c @@ -22,8 +22,6 @@ in the source distribution for its full text. #include -#define _UNUSED_ __attribute__((unused)) - static int MIB_hw_physmem[2]; static int MIB_vm_stats_vm_v_page_count[4]; static int pageSize; @@ -377,7 +375,7 @@ void ProcessList_goThroughEntries(ProcessList* this) { for (int i = 0; i < count; i++) { struct kinfo_proc* kproc = &kprocs[i]; bool preExisting = false; - bool _UNUSED_ isIdleProcess = false; + bool ATTR_UNUSED isIdleProcess = false; // note: dragonflybsd kernel processes all have the same pid, so we misuse the kernel thread address to give them a unique identifier Process* proc = ProcessList_getProcess(this, kproc->kp_ktaddr ? (pid_t)kproc->kp_ktaddr : kproc->kp_pid, &preExisting, (Process_New) DragonFlyBSDProcess_new); diff --git a/dragonflybsd/DragonFlyBSDProcessList.h b/dragonflybsd/DragonFlyBSDProcessList.h index 84ab1c5a..9665a60f 100644 --- a/dragonflybsd/DragonFlyBSDProcessList.h +++ b/dragonflybsd/DragonFlyBSDProcessList.h @@ -51,8 +51,6 @@ typedef struct DragonFlyBSDProcessList_ { Hashtable *jails; } DragonFlyBSDProcessList; -#define _UNUSED_ __attribute__((unused)) - ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId); void ProcessList_delete(ProcessList* this); diff --git a/zfs/ZfsArcStats.c b/zfs/ZfsArcStats.c index bfed07d3..6c02c2c1 100644 --- a/zfs/ZfsArcStats.c +++ b/zfs/ZfsArcStats.c @@ -21,4 +21,6 @@ typedef struct ZfsArcStats_ { } ZfsArcStats; }*/ -static int make_iso_compilers_happy __attribute__((unused)); +#include "Macros.h" + +static int make_iso_compilers_happy ATTR_UNUSED;