From db472075a4fb82bf6a491848941a5b9c92526c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 4 Oct 2020 14:30:35 +0200 Subject: [PATCH] Enable -Wcast-qual compiler warning --- FunctionBar.c | 16 ++++++++-------- FunctionBar.h | 5 ++++- Macros.h | 16 ++++++++++++++++ configure.ac | 1 + dragonflybsd/DragonFlyBSDProcess.c | 12 ++++++------ freebsd/FreeBSDProcess.c | 12 ++++++------ freebsd/FreeBSDProcessList.c | 3 +++ openbsd/OpenBSDProcess.c | 12 ++++++------ solaris/SolarisProcess.c | 12 ++++++------ 9 files changed, 56 insertions(+), 33 deletions(-) diff --git a/FunctionBar.c b/FunctionBar.c index 55279c21..df1fab8f 100644 --- a/FunctionBar.c +++ b/FunctionBar.c @@ -40,18 +40,18 @@ FunctionBar* FunctionBar_new(const char* const* functions, const char* const* ke } if (keys && events) { this->staticData = false; - this->keys = xCalloc(15, sizeof(char*)); + this->keys.keys = xCalloc(15, sizeof(char*)); this->events = xCalloc(15, sizeof(int)); int i = 0; while (i < 15 && functions[i]) { - this->keys[i] = xStrdup(keys[i]); + this->keys.keys[i] = xStrdup(keys[i]); this->events[i] = events[i]; i++; } this->size = i; } else { this->staticData = true; - this->keys = (char**) FunctionBar_FKeys; + this->keys.constKeys = FunctionBar_FKeys; this->events = FunctionBar_FEvents; this->size = 10; } @@ -65,9 +65,9 @@ void FunctionBar_delete(FunctionBar* this) { free(this->functions); if (!this->staticData) { for (int i = 0; i < this->size; i++) { - free(this->keys[i]); + free(this->keys.keys[i]); } - free(this->keys); + free(this->keys.keys); free(this->events); } free(this); @@ -93,8 +93,8 @@ void FunctionBar_drawAttr(const FunctionBar* this, char* buffer, int attr) { int x = 0; for (int i = 0; i < this->size; i++) { attrset(CRT_colors[FUNCTION_KEY]); - mvaddstr(LINES-1, x, this->keys[i]); - x += strlen(this->keys[i]); + mvaddstr(LINES-1, x, this->keys.constKeys[i]); + x += strlen(this->keys.constKeys[i]); attrset(CRT_colors[FUNCTION_BAR]); mvaddstr(LINES-1, x, this->functions[i]); x += strlen(this->functions[i]); @@ -113,7 +113,7 @@ void FunctionBar_drawAttr(const FunctionBar* this, char* buffer, int attr) { int FunctionBar_synthesizeEvent(const FunctionBar* this, int pos) { int x = 0; for (int i = 0; i < this->size; i++) { - x += strlen(this->keys[i]); + x += strlen(this->keys.constKeys[i]); x += strlen(this->functions[i]); if (pos < x) { return this->events[i]; diff --git a/FunctionBar.h b/FunctionBar.h index 8fa74404..c650c1a4 100644 --- a/FunctionBar.h +++ b/FunctionBar.h @@ -12,7 +12,10 @@ in the source distribution for its full text. typedef struct FunctionBar_ { int size; char** functions; - char** keys; + union { + char** keys; + const char* const* constKeys; + } keys; int* events; bool staticData; } FunctionBar; diff --git a/Macros.h b/Macros.h index 33e727d8..bb3a90e0 100644 --- a/Macros.h +++ b/Macros.h @@ -33,4 +33,20 @@ #endif /* __GNUC__ */ +// ignore casts discarding const specifier, e.g. +// const char [] -> char * / void * +// const char *[2]' -> char *const * +#ifdef __clang__ +#define IGNORE_WCASTQUAL_BEGIN _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wcast-qual\"") +#define IGNORE_WCASTQUAL_END _Pragma("clang diagnostic pop") +#elif defined(__GNUC__) +#define IGNORE_WCASTQUAL_BEGIN _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wcast-qual\"") +#define IGNORE_WCASTQUAL_END _Pragma("GCC diagnostic pop") +#else +#define IGNORE_WCASTQUAL_BEGIN +#define IGNORE_WCASTQUAL_END +#endif + #endif diff --git a/configure.ac b/configure.ac index ce4e96b4..7982d648 100644 --- a/configure.ac +++ b/configure.ac @@ -296,6 +296,7 @@ fi AM_CFLAGS="\ -Wall\ -Wcast-align\ + -Wcast-qual\ -Wextra\ -Wfloat-equal\ -Wmissing-format-attribute\ diff --git a/dragonflybsd/DragonFlyBSDProcess.c b/dragonflybsd/DragonFlyBSDProcess.c index 972c77c2..df320e08 100644 --- a/dragonflybsd/DragonFlyBSDProcess.c +++ b/dragonflybsd/DragonFlyBSDProcess.c @@ -108,14 +108,14 @@ void DragonFlyBSDProcess_writeField(Process* this, RichString* str, ProcessField } long DragonFlyBSDProcess_compare(const void* v1, const void* v2) { - DragonFlyBSDProcess *p1, *p2; - Settings *settings = ((Process*)v1)->settings; + const DragonFlyBSDProcess *p1, *p2; + const Settings *settings = ((const Process*)v1)->settings; if (settings->direction == 1) { - p1 = (DragonFlyBSDProcess*)v1; - p2 = (DragonFlyBSDProcess*)v2; + p1 = (const DragonFlyBSDProcess*)v1; + p2 = (const DragonFlyBSDProcess*)v2; } else { - p2 = (DragonFlyBSDProcess*)v1; - p1 = (DragonFlyBSDProcess*)v2; + p2 = (const DragonFlyBSDProcess*)v1; + p1 = (const DragonFlyBSDProcess*)v2; } switch ((int) settings->sortKey) { // add Platform-specific fields here diff --git a/freebsd/FreeBSDProcess.c b/freebsd/FreeBSDProcess.c index 399c5f69..767f485b 100644 --- a/freebsd/FreeBSDProcess.c +++ b/freebsd/FreeBSDProcess.c @@ -107,14 +107,14 @@ void FreeBSDProcess_writeField(Process* this, RichString* str, ProcessField fiel } long FreeBSDProcess_compare(const void* v1, const void* v2) { - FreeBSDProcess *p1, *p2; - Settings *settings = ((Process*)v1)->settings; + const FreeBSDProcess *p1, *p2; + const Settings *settings = ((const Process*)v1)->settings; if (settings->direction == 1) { - p1 = (FreeBSDProcess*)v1; - p2 = (FreeBSDProcess*)v2; + p1 = (const FreeBSDProcess*)v1; + p2 = (const FreeBSDProcess*)v2; } else { - p2 = (FreeBSDProcess*)v1; - p1 = (FreeBSDProcess*)v2; + p2 = (const FreeBSDProcess*)v1; + p1 = (const FreeBSDProcess*)v2; } switch ((int) settings->sortKey) { // add FreeBSD-specific fields here diff --git a/freebsd/FreeBSDProcessList.c b/freebsd/FreeBSDProcessList.c index cd9897fb..f9377a2d 100644 --- a/freebsd/FreeBSDProcessList.c +++ b/freebsd/FreeBSDProcessList.c @@ -5,6 +5,7 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ +#include "Macros.h" #include "ProcessList.h" #include "FreeBSDProcessList.h" #include "FreeBSDProcess.h" @@ -338,6 +339,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) { if (kproc->ki_jid != 0 ){ memset(jnamebuf, 0, sizeof(jnamebuf)); +IGNORE_WCASTQUAL_BEGIN *(const void **)&jiov[0].iov_base = "jid"; jiov[0].iov_len = sizeof("jid"); jiov[1].iov_base = &kproc->ki_jid; @@ -350,6 +352,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) { jiov[4].iov_len = sizeof("errmsg"); jiov[5].iov_base = jail_errmsg; jiov[5].iov_len = JAIL_ERRMSGLEN; +IGNORE_WCASTQUAL_END jail_errmsg[0] = 0; jid = jail_get(jiov, 6, 0); if (jid < 0) { diff --git a/openbsd/OpenBSDProcess.c b/openbsd/OpenBSDProcess.c index 6f52adf0..94ce4828 100644 --- a/openbsd/OpenBSDProcess.c +++ b/openbsd/OpenBSDProcess.c @@ -193,14 +193,14 @@ void OpenBSDProcess_writeField(Process* this, RichString* str, ProcessField fiel } long OpenBSDProcess_compare(const void* v1, const void* v2) { - OpenBSDProcess *p1, *p2; - Settings *settings = ((Process*)v1)->settings; + const OpenBSDProcess *p1, *p2; + const Settings *settings = ((const Process*)v1)->settings; if (settings->direction == 1) { - p1 = (OpenBSDProcess*)v1; - p2 = (OpenBSDProcess*)v2; + p1 = (const OpenBSDProcess*)v1; + p2 = (const OpenBSDProcess*)v2; } else { - p2 = (OpenBSDProcess*)v1; - p1 = (OpenBSDProcess*)v2; + p2 = (const OpenBSDProcess*)v1; + p1 = (const OpenBSDProcess*)v2; } switch (settings->sortKey) { // add OpenBSD-specific fields here diff --git a/solaris/SolarisProcess.c b/solaris/SolarisProcess.c index 89b1690d..d0a75dfe 100644 --- a/solaris/SolarisProcess.c +++ b/solaris/SolarisProcess.c @@ -117,14 +117,14 @@ void SolarisProcess_writeField(Process* this, RichString* str, ProcessField fiel } long SolarisProcess_compare(const void* v1, const void* v2) { - SolarisProcess *p1, *p2; - Settings* settings = ((Process*)v1)->settings; + const SolarisProcess *p1, *p2; + const Settings* settings = ((const Process*)v1)->settings; if (settings->direction == 1) { - p1 = (SolarisProcess*)v1; - p2 = (SolarisProcess*)v2; + p1 = (const SolarisProcess*)v1; + p2 = (const SolarisProcess*)v2; } else { - p2 = (SolarisProcess*)v1; - p1 = (SolarisProcess*)v2; + p2 = (const SolarisProcess*)v1; + p1 = (const SolarisProcess*)v2; } switch ((int) settings->sortKey) { case ZONEID: