Skip to content

Commit

Permalink
Add printf format attribute (#1097)
Browse files Browse the repository at this point in the history
as suggested by -Wsuggest-attribute=format

Handle MinGW and MSVC printf format attributes

---------

Co-authored-by: Mike Taves <[email protected]>
  • Loading branch information
strk and mwtoews authored May 24, 2024
1 parent 6de25db commit 93e74e5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ target_compile_definitions(geos_developer_cxx_flags

target_compile_options(geos_developer_cxx_flags
INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:-W4>
$<$<CXX_COMPILER_ID:MSVC>:-W4> # consider -analyze
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Werror -pedantic -Wall -Wextra -Wno-long-long -Wcast-align -Wconversion -Wsign-conversion -Wchar-subscripts -Wdouble-promotion -Wpointer-arith -Wformat -Wformat-security -Wshadow -Wuninitialized -Wunused-parameter -fno-common>
$<$<CXX_COMPILER_ID:GNU>:-fno-implicit-inline-templates -Wno-psabi>
$<$<CXX_COMPILER_ID:GNU>:-fno-implicit-inline-templates -Wno-psabi -Wsuggest-attribute=format>
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wno-unknown-warning-option>
)

Expand Down
3 changes: 2 additions & 1 deletion capi/geos_c.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ typedef struct GEOSContextHandle_HS *GEOSContextHandle_t;
*
* \param fmt the message format template
*/
typedef void (*GEOSMessageHandler)(const char *fmt, ...);
typedef void (*GEOSMessageHandler)(GEOS_PRINTF_FORMAT const char *fmt, ...)
GEOS_PRINTF_FORMAT_ATTR(1, 2);

/**
* A GEOS message handler function.
Expand Down
18 changes: 16 additions & 2 deletions capi/geos_ts_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,22 @@ typedef struct GEOSContextHandle_HS {
}

void
NOTICE_MESSAGE(const char *fmt, ...)
NOTICE_MESSAGE(GEOS_PRINTF_FORMAT const char *fmt, ...) GEOS_PRINTF_FORMAT_ATTR(2, 3)
{
if(nullptr == noticeMessageOld && nullptr == noticeMessageNew) {
return;
}

va_list args;
va_start(args, fmt);
#ifdef __MINGW32__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
#endif
int result = vsnprintf(msgBuffer, sizeof(msgBuffer) - 1, fmt, args);
#ifdef __MINGW32__
#pragma GCC diagnostic pop
#endif
va_end(args);

if(result > 0) {
Expand All @@ -311,15 +318,22 @@ typedef struct GEOSContextHandle_HS {
}

void
ERROR_MESSAGE(const char *fmt, ...)
ERROR_MESSAGE(GEOS_PRINTF_FORMAT const char *fmt, ...) GEOS_PRINTF_FORMAT_ATTR(2, 3)
{
if(nullptr == errorMessageOld && nullptr == errorMessageNew) {
return;
}

va_list args;
va_start(args, fmt);
#ifdef __MINGW32__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
#endif
int result = vsnprintf(msgBuffer, sizeof(msgBuffer) - 1, fmt, args);
#ifdef __MINGW32__
#pragma GCC diagnostic pop
#endif
va_end(args);

if(result > 0) {
Expand Down
18 changes: 18 additions & 0 deletions include/geos/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,21 @@
#if defined(_MSC_VER)
# pragma warning(disable: 4251) // identifier : class type needs to have dll-interface to be used by clients of class type2
#endif


/**********************************************************************
* Portability macros
**********************************************************************/

#ifdef _MSC_VER
#include <sal.h>
#define GEOS_PRINTF_FORMAT _Printf_format_string_
#define GEOS_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
#elif __GNUC__
#define GEOS_PRINTF_FORMAT /**/
#define GEOS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
__attribute__((format(printf, format_param, dots_param)))
#else
#define GEOS_PRINTF_FORMAT /**/
#define GEOS_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
#endif
9 changes: 8 additions & 1 deletion tests/unit/capi/capi_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@ namespace capitest {
finishGEOS();
}

static void notice(const char* fmt, ...)
static void notice(GEOS_PRINTF_FORMAT const char* fmt, ...) GEOS_PRINTF_FORMAT_ATTR(1, 2)
{
std::fprintf(stdout, "NOTICE: ");

va_list ap;
va_start(ap, fmt);
#ifdef __MINGW32__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
#endif
std::vfprintf(stdout, fmt, ap);
#ifdef __MINGW32__
#pragma GCC diagnostic pop
#endif
va_end(ap);

std::fprintf(stdout, "\n");
Expand Down

0 comments on commit 93e74e5

Please sign in to comment.