From 6ffe0a4c99b9f93f588fab95345ab7635ffa91a8 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 1 Jun 2019 20:20:11 -0400 Subject: [PATCH] Added functionality to mark printf() functions for compilers with warning checks, and marked such. Sadly it appears ms_printf is ignored with -pedantic, so we'll need to stop using I32... --- common/uipriv.h | 12 ++++++++++-- sharedbits/alloc_impl.h | 5 ++++- sharedbits/printfwarn_header.h | 14 ++++++++++++++ sharedbits/strsafe_strncpy_impl.h | 5 ++++- test/lib/testing.h | 6 +++++- test/lib/testingpriv.h | 20 ++++++++++++++++---- 6 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 sharedbits/printfwarn_header.h diff --git a/common/uipriv.h b/common/uipriv.h index 44b493f0..8a240c6a 100644 --- a/common/uipriv.h +++ b/common/uipriv.h @@ -28,9 +28,14 @@ extern "C" { #define uiprivSizetPrintf "zu" #endif +#include "../sharedbits/printfwarn_header.h" +#define uiprivPrintfFunc(decl, fmtpos, appos) sharedbitsPrintfFunc(decl, fmtpos, appos) + // main.c extern bool uiprivSysInit(void *options, uiInitError *err); -extern bool uiprivInitReturnErrorf(uiInitError *err, const char *fmt, ...); +uiprivPrintfFunc( + extern bool uiprivInitReturnErrorf(uiInitError *err, const char *fmt, ...), + 2, 3); extern void uiprivSysQueueMain(void (*f)(void *data), void *data); extern bool uiprivCheckInitializedAndThreadImpl(const char *func); #define uiprivCheckInitializedAndThread() uiprivCheckInitializedAndThreadImpl(uiprivFunc) @@ -49,7 +54,9 @@ extern bool uiprivSysCheckThread(void); #undef sharedbitsPrefix // errors.c -extern void uiprivInternalError(const char *fmt, ...); +uiprivPrintfFunc( + extern void uiprivInternalError(const char *fmt, ...), + 1, 2); enum { uiprivProgrammerErrorNotInitialized, // arguments: uiprivFunc uiprivProgrammerErrorWrongThread, // arguments: uiprivFunc @@ -63,6 +70,7 @@ enum { uiprivProgrammerErrorRecursiveEventFire, // no arguments uiprivNumProgrammerErrors, }; +// TODO drop the enum and make the above all format strings extern void uiprivProgrammerError(unsigned int which, ...); extern void uiprivReportError(const char *prefix, const char *msg, const char *suffix, bool internal); diff --git a/sharedbits/alloc_impl.h b/sharedbits/alloc_impl.h index 36c64bbc..6e6e3e05 100644 --- a/sharedbits/alloc_impl.h +++ b/sharedbits/alloc_impl.h @@ -3,7 +3,10 @@ #include "start.h" -extern void sharedbitsPrefixName(InternalError)(const char *fmt, ...); +#include "printfwarn_header.h" +sharedbitsPrintfFunc( + extern void sharedbitsPrefixName(InternalError)(const char *fmt, ...), + 1, 2); void *sharedbitsPrefixName(Alloc)(size_t n, const char *what) { diff --git a/sharedbits/printfwarn_header.h b/sharedbits/printfwarn_header.h new file mode 100644 index 00000000..c859c146 --- /dev/null +++ b/sharedbits/printfwarn_header.h @@ -0,0 +1,14 @@ +// 1 june 2019 + +#ifdef __GNUC__ +#ifdef _WIN32 +#define sharedbitsPrintfFunc(decl, fmtpos, appos) \ + decl __attribute__((format(ms_printf, fmtpos, appos))) +#else +#define sharedbitsPrintfFunc(decl, fmtpos, appos) \ + decl __attribute__((format(printf, fmtpos, appos))) +#endif +#else +#define sharedbitsPrintfFunc(decl, fmtpos, appos) \ + decl +#endif diff --git a/sharedbits/strsafe_strncpy_impl.h b/sharedbits/strsafe_strncpy_impl.h index d4ee9fb4..2c49a9af 100644 --- a/sharedbits/strsafe_strncpy_impl.h +++ b/sharedbits/strsafe_strncpy_impl.h @@ -8,12 +8,15 @@ #define sharedbitsprivInternalError sharedbitsInternalError #else #define sharedbitsprivInternalError sharedbitsPrefixName(InternalError) +#include "printfwarn_header.h" #ifdef sharedbitsStatic sharedbitsStatic #else extern #endif -void sharedbitsprivInternalError(const char *fmt, ...); +sharedbitsPrintfFunc( + void sharedbitsprivInternalError(const char *fmt, ...), + 1, 2); #endif #endif diff --git a/test/lib/testing.h b/test/lib/testing.h index db183d1d..faeb3f24 100644 --- a/test/lib/testing.h +++ b/test/lib/testing.h @@ -76,5 +76,9 @@ extern void testingprivSetRegisterTest(testingSet **pset, const char *, void (*) #define testingprivExpand(x) x #define testingprivTLogfThen(then, t, ...) ((testingprivTLogfFull(t, __FILE__, __LINE__, __VA_ARGS__)), (then(t))) #define testingprivTLogvfThen(then, t, format, ap) ((testingprivTLogvfFull(t, __FILE__, __LINE__, format, ap)), (then(t))) -extern void testingprivTLogfFull(testingT *, const char *, long, const char *, ...); +#include "../../sharedbits/printfwarn_header.h" +sharedbitsPrintfFunc( + extern void testingprivTLogfFull(testingT *, const char *, long, const char *, ...), + 4, 5); +#undef sharedbitsPrintfFunc extern void testingprivTLogvfFull(testingT *, const char *, long, const char *, va_list); diff --git a/test/lib/testingpriv.h b/test/lib/testingpriv.h index 5be4fd2d..f9e41f6f 100644 --- a/test/lib/testingpriv.h +++ b/test/lib/testingpriv.h @@ -1,6 +1,10 @@ // 19 may 2019 -extern void testingprivInternalError(const char *fmt, ...); +#include "../../sharedbits/printfwarn_header.h" + +sharedbitsPrintfFunc( + extern void testingprivInternalError(const char *fmt, ...), + 1, 2); #define sharedbitsPrefix testingpriv @@ -18,10 +22,14 @@ extern void testingprivInternalError(const char *fmt, ...); #undef sharedbitsPrefix extern int testingprivVsnprintf(char *s, size_t n, const char *fmt, va_list ap); -extern int testingprivSnprintf(char *s, size_t n, const char *fmt, ...); +sharedbitsPrintfFunc( + extern int testingprivSnprintf(char *s, size_t n, const char *fmt, ...), + 3, 4); extern char *testingprivStrdup(const char *s); extern char *testingprivVsmprintf(const char *fmt, va_list ap); -extern char *testingprivSmprintf(const char *fmt, ...); +sharedbitsPrintfFunc( + extern char *testingprivSmprintf(const char *fmt, ...), + 1, 2); // a testingprivOutbuf of NULL writes directly to stdout typedef struct testingprivOutbuf testingprivOutbuf; @@ -29,6 +37,10 @@ extern testingprivOutbuf *testingprivNewOutbuf(void); extern void testingprivOutbufFree(testingprivOutbuf *o); extern void testingprivOutbufVprintf(testingprivOutbuf *o, const char *fmt, va_list ap); extern void testingprivOutbufVprintfIndented(testingprivOutbuf *o, const char *fmt, va_list ap); -extern void testingprivOutbufPrintf(testingprivOutbuf *o, const char *fmt, ...); +sharedbitsPrintfFunc( + extern void testingprivOutbufPrintf(testingprivOutbuf *o, const char *fmt, ...), + 2, 3); extern void testingprivOutbufAppendOutbuf(testingprivOutbuf *o, testingprivOutbuf *src); extern const char *testingprivOutbufString(testingprivOutbuf *o); + +#undef sharedbitsPrintfFunc