Skip to content

Commit e1db8c6

Browse files
committed
Fixes mpaland#141: Standard library function name aliasing now has three options: NONE, SOFT, HARD.
* `test_suite` program now supports NONE and HARD aliasing (but not SOFT).
1 parent ced7254 commit e1db8c6

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

CMakeLists.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,34 @@ option(SUPPORT_EXPONENTIAL_SPECIFIERS "Support exponential floating poin
2020
option(SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS "Support the I + bit size integer specifiers (%I8, %I16, %I32, %I64) as in Microsoft Visual C++" ON)
2121
option(SUPPORT_WRITEBACK_SPECIFIER "Support the length write-back specifier (%n)" ON)
2222
option(SUPPORT_LONG_LONG "Support long long integral types (allows for the ll length modifier and affects %p)" ON)
23-
option(ALIAS_STANDARD_FUNCTION_NAMES "Alias the standard library function names (printf, sprintf etc.) to the library's functions" ON)
23+
24+
set(ALIASING_MODES NONE HARD SOFT)
25+
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")
26+
set_property(CACHE ALIAS_STANDARD_FUNCTION_NAMES PROPERTY STRINGS ${ALIASING_MODES})
27+
28+
#option(ALIAS_STANDARD_FUNCTION_NAMES "Alias the standard library function names (printf, sprintf etc.) to the library's functions" ON)
29+
30+
if (NOT ${ALIAS_STANDARD_FUNCTION_NAMES} STREQUAL NONE)
31+
set("ALIAS_STANDARD_FUNCTION_NAMES_${ALIAS_STANDARD_FUNCTION_NAMES}" 1)
32+
endif()
2433

2534
foreach(opt
2635
SUPPORT_DECIMAL_SPECIFIERS
2736
SUPPORT_EXPONENTIAL_SPECIFIERS
2837
SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS
2938
SUPPORT_WRITEBACK_SPECIFIER
3039
SUPPORT_LONG_LONG
31-
ALIAS_STANDARD_FUNCTION_NAMES)
40+
ALIAS_STANDARD_FUNCTION_NAMES_SOFT
41+
ALIAS_STANDARD_FUNCTION_NAMES_HARD)
3242
if (${${opt}})
3343
set("PRINTF_${opt}" 1)
3444
else()
3545
set("PRINTF_${opt}" 0)
3646
endif()
3747
endforeach()
3848

49+
50+
3951
# Numeric defines which go into printf_config.h
4052

4153
set(PRINTF_INTEGER_BUFFER_SIZE "32" CACHE STRING "Integer to string conversion buffer size")

printf_config.h.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#define PRINTF_SUPPORT_WRITEBACK_SPECIFIER @PRINTF_SUPPORT_WRITEBACK_SPECIFIER@
88
#define PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS @PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS@
99
#define PRINTF_SUPPORT_LONG_LONG @PRINTF_SUPPORT_LONG_LONG@
10-
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES @PRINTF_ALIAS_STANDARD_FUNCTION_NAMES@
10+
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT @PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT@
11+
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD @PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD@
1112

1213
#define PRINTF_INTEGER_BUFFER_SIZE @PRINTF_INTEGER_BUFFER_SIZE@
1314
#define PRINTF_DECIMAL_BUFFER_SIZE @PRINTF_DECIMAL_BUFFER_SIZE@

src/printf/printf.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ __attribute__((format(printf, (one_based_format_index), (first_arg))))
6666
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 0
6767
#endif
6868

69-
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
69+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
7070
# define printf_ printf
7171
# define sprintf_ sprintf
7272
# define vsprintf_ vsprintf
@@ -194,13 +194,22 @@ int vfctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char
194194
} // extern "C"
195195
#endif
196196

197-
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
197+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
198198
# undef printf_
199199
# undef sprintf_
200200
# undef vsprintf_
201201
# undef snprintf_
202202
# undef vsnprintf_
203203
# undef vprintf_
204+
#else
205+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
206+
# define printf printf_
207+
# define sprintf sprintf_
208+
# define vsprintf vsprintf_
209+
# define snprintf snprintf_
210+
# define vsnprintf vsnprintf_
211+
# define vprintf vprintf_
212+
#endif
204213
#endif
205214

206215
#endif // PRINTF_H_

test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.9)
33
enable_language(CXX)
44

55
set(test_targets autotest)
6-
if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
6+
if (NOT ALIAS_STANDARD_FUNCTION_NAMES STREQUAL SOFT)
77
list(APPEND test_targets test_suite)
88
endif()
99

@@ -81,7 +81,7 @@ foreach(tgt ${test_targets})
8181

8282
endforeach()
8383

84-
if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
84+
if (TARGET test_suite)
8585
# These two lines are necessary, since the test suite does not actually use the
8686
# compiled library - it includes the library's source .c file; and that means we
8787
# need to include the generated config.h file.

test/test_suite.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
#endif
3535
#include <printf/printf.c>
3636

37+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
38+
// Disable aliasing so as not to interfere with the standard library headers
39+
# undef printf
40+
# undef sprintf_
41+
# undef vsprintf_
42+
# undef snprintf_
43+
# undef vsnprintf_
44+
# undef vprintf_
45+
#endif
46+
3747
// use the 'catch' test framework
3848
#define CATCH_CONFIG_MAIN
3949
#include "catch.hpp"
@@ -55,6 +65,17 @@ typedef SSIZE_T ssize_t;
5565
// Let's just cross our fingers and hope `ssize_t` is defined.
5666
#endif
5767

68+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT
69+
// Re-enable aliasing
70+
# define printf printf_
71+
# define sprintf sprintf_
72+
# define vsprintf vsprintf_
73+
# define snprintf snprintf_
74+
# define vsnprintf vsnprintf_
75+
# define vprintf vprintf_
76+
#endif
77+
78+
5879
#define CAPTURE_AND_PRINT(printer_, ...) \
5980
do { \
6081
INFO( #printer_ << \

0 commit comments

Comments
 (0)