Skip to content

Commit

Permalink
Fixes mpaland#141: Standard library function name aliasing now has th…
Browse files Browse the repository at this point in the history
…ree options: NONE, SOFT, HARD.

* `test_suite` program now supports NONE and HARD aliasing (but not SOFT).
  • Loading branch information
eyalroz committed Oct 17, 2022
1 parent ced7254 commit e1db8c6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
16 changes: 14 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,34 @@ option(SUPPORT_EXPONENTIAL_SPECIFIERS "Support exponential floating poin
option(SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS "Support the I + bit size integer specifiers (%I8, %I16, %I32, %I64) as in Microsoft Visual C++" ON)
option(SUPPORT_WRITEBACK_SPECIFIER "Support the length write-back specifier (%n)" ON)
option(SUPPORT_LONG_LONG "Support long long integral types (allows for the ll length modifier and affects %p)" ON)
option(ALIAS_STANDARD_FUNCTION_NAMES "Alias the standard library function names (printf, sprintf etc.) to the library's functions" ON)

set(ALIASING_MODES NONE HARD SOFT)
set(ALIAS_STANDARD_FUNCTION_NAMES NONE CACHE STRING "Alias the standard library function names (printf, sprintf etc.) to the library's functions - concretely, via a macro, or not at all")
set_property(CACHE ALIAS_STANDARD_FUNCTION_NAMES PROPERTY STRINGS ${ALIASING_MODES})

#option(ALIAS_STANDARD_FUNCTION_NAMES "Alias the standard library function names (printf, sprintf etc.) to the library's functions" ON)

if (NOT ${ALIAS_STANDARD_FUNCTION_NAMES} STREQUAL NONE)
set("ALIAS_STANDARD_FUNCTION_NAMES_${ALIAS_STANDARD_FUNCTION_NAMES}" 1)
endif()

foreach(opt
SUPPORT_DECIMAL_SPECIFIERS
SUPPORT_EXPONENTIAL_SPECIFIERS
SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS
SUPPORT_WRITEBACK_SPECIFIER
SUPPORT_LONG_LONG
ALIAS_STANDARD_FUNCTION_NAMES)
ALIAS_STANDARD_FUNCTION_NAMES_SOFT
ALIAS_STANDARD_FUNCTION_NAMES_HARD)
if (${${opt}})
set("PRINTF_${opt}" 1)
else()
set("PRINTF_${opt}" 0)
endif()
endforeach()



# Numeric defines which go into printf_config.h

set(PRINTF_INTEGER_BUFFER_SIZE "32" CACHE STRING "Integer to string conversion buffer size")
Expand Down
3 changes: 2 additions & 1 deletion printf_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#define PRINTF_SUPPORT_WRITEBACK_SPECIFIER @PRINTF_SUPPORT_WRITEBACK_SPECIFIER@
#define PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS @PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS@
#define PRINTF_SUPPORT_LONG_LONG @PRINTF_SUPPORT_LONG_LONG@
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES @PRINTF_ALIAS_STANDARD_FUNCTION_NAMES@
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT @PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT@
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD @PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD@

#define PRINTF_INTEGER_BUFFER_SIZE @PRINTF_INTEGER_BUFFER_SIZE@
#define PRINTF_DECIMAL_BUFFER_SIZE @PRINTF_DECIMAL_BUFFER_SIZE@
Expand Down
13 changes: 11 additions & 2 deletions src/printf/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ __attribute__((format(printf, (one_based_format_index), (first_arg))))
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 0
#endif

#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
# define printf_ printf
# define sprintf_ sprintf
# define vsprintf_ vsprintf
Expand Down Expand Up @@ -194,13 +194,22 @@ int vfctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char
} // extern "C"
#endif

#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
# undef printf_
# undef sprintf_
# undef vsprintf_
# undef snprintf_
# undef vsnprintf_
# undef vprintf_
#else
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
# define printf printf_
# define sprintf sprintf_
# define vsprintf vsprintf_
# define snprintf snprintf_
# define vsnprintf vsnprintf_
# define vprintf vprintf_
#endif
#endif

#endif // PRINTF_H_
4 changes: 2 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.9)
enable_language(CXX)

set(test_targets autotest)
if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
if (NOT ALIAS_STANDARD_FUNCTION_NAMES STREQUAL SOFT)
list(APPEND test_targets test_suite)
endif()

Expand Down Expand Up @@ -81,7 +81,7 @@ foreach(tgt ${test_targets})

endforeach()

if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
if (TARGET test_suite)
# These two lines are necessary, since the test suite does not actually use the
# compiled library - it includes the library's source .c file; and that means we
# need to include the generated config.h file.
Expand Down
21 changes: 21 additions & 0 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
#endif
#include <printf/printf.c>

#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
// Disable aliasing so as not to interfere with the standard library headers
# undef printf
# undef sprintf_
# undef vsprintf_
# undef snprintf_
# undef vsnprintf_
# undef vprintf_
#endif

// use the 'catch' test framework
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
Expand All @@ -55,6 +65,17 @@ typedef SSIZE_T ssize_t;
// Let's just cross our fingers and hope `ssize_t` is defined.
#endif

#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
// Re-enable aliasing
# define printf printf_
# define sprintf sprintf_
# define vsprintf vsprintf_
# define snprintf snprintf_
# define vsnprintf vsnprintf_
# define vprintf vprintf_
#endif


#define CAPTURE_AND_PRINT(printer_, ...) \
do { \
INFO( #printer_ << \
Expand Down

0 comments on commit e1db8c6

Please sign in to comment.