Skip to content

Commit

Permalink
Merge remote-tracking branch 'joshklod/format-attr'
Browse files Browse the repository at this point in the history
* Fixed a few more (GCC) warnings.
* Separated the warning-triggering cases into test cases of their own.
* Added a CMake option for whether or not to test the warning-triggering cases.
  • Loading branch information
Eyal Rozenberg committed Jun 30, 2021
2 parents 88431d7 + 761ac4d commit 145f64a
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 59 deletions.
23 changes: 16 additions & 7 deletions printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
extern "C" {
#endif

#ifdef __GNUC__
# define ATTR_PRINTF(one_based_format_index, first_arg) \
__attribute__((format(__printf__, (one_based_format_index), (first_arg))))
# define ATTR_VPRINTF(one_based_format_index) ATTR_PRINTF(one_based_format_index, 0)
#else
# define ATTR_PRINTF(one_based_format_index, first_arg)
# define ATTR_VPRINTF(one_based_format_index)
#endif


/**
* Output a character to a custom device like UART, used by the printf() function
Expand All @@ -58,7 +67,7 @@ void _putchar(char character);
* \return The number of characters that are written into the array, not counting the terminating null character
*/
#define printf printf_
int printf_(const char* format, ...);
int printf_(const char* format, ...) ATTR_PRINTF(1, 2);


/**
Expand All @@ -71,8 +80,8 @@ int printf_(const char* format, ...);
*/
#define sprintf sprintf_
#define vsprintf vsprintf_
int sprintf_(char* buffer, const char* format, ...);
int vsprintf_(char* buffer, const char* format, va_list va);
int sprintf_(char* buffer, const char* format, ...) ATTR_PRINTF(2, 3);
int vsprintf_(char* buffer, const char* format, va_list va) ATTR_VPRINTF(2);


/**
Expand All @@ -87,8 +96,8 @@ int vsprintf_(char* buffer, const char* format, va_list va);
*/
#define snprintf snprintf_
#define vsnprintf vsnprintf_
int snprintf_(char* buffer, size_t count, const char* format, ...);
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
int snprintf_(char* buffer, size_t count, const char* format, ...) ATTR_PRINTF(3, 4);
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va) ATTR_VPRINTF(3);


/**
Expand All @@ -110,8 +119,8 @@ int vprintf_(const char* format, va_list va);
* \param va A value identifying a variable arguments list
* \return The number of characters that are sent to the output function, not counting the terminating null character
*/
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);
int vfctprintf(void (*out)(char character, void* arg), void* arg, const char* format, va_list va);
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...) ATTR_PRINTF(3, 4);
int vfctprintf(void (*out)(char character, void* arg), void* arg, const char* format, va_list va) ATTR_VPRINTF(3);

#ifdef __cplusplus
}
Expand Down
6 changes: 6 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ set_target_properties(
CXX_EXTENSIONS NO
)

option(TEST_BROKEN_FORMATS "Include tests using non-standard-compliant format strings?" ON)
// ... don't worry, we'll suppress the compiler warnings for those.
if (TEST_BROKEN_FORMATS)
target_compile_definitions(test_suite PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
endif()

target_link_libraries(test_suite PRIVATE mpaland-printf)

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
Expand Down
Loading

0 comments on commit 145f64a

Please sign in to comment.