From ceb6db56c87cde2fd68f5cd4356f31ca77c924aa Mon Sep 17 00:00:00 2001 From: Eyal Rozenberg Date: Sat, 21 Jan 2023 19:56:18 +0200 Subject: [PATCH] Regards #146, regards #141: sorted out aliasing-related preprocessor definition mess: With the introduction of three options for standard library function aliasing behavior (none, soft and hard), we now have two boolean preprocessor definitions, `PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT` and `PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD` - but some of the code was still expecting `PRINTF_ALIAS_STANDARD_FUNCTION_NAMES`. Now making sure both of these are set and respected. * Updated the README.md --- README.md | 2 +- src/printf/printf.c | 4 ++-- src/printf/printf.h | 10 +++++++--- test/CMakeLists.txt | 3 +++ test/aliasing.c | 2 +- test/autotest.cpp | 11 ++++++++--- 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 626e4209..61bd8312 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ Options used both in CMake and in the library source code via a preprocessor def | Option name | Default | Description | |----------------------------------------|---------|--------------| -| PRINTF_ALIAS_STANDARD_FUNCTION_NAMES | NO | Alias the standard library function names (`printf()`, `sprintf()` etc.) to the library's functions.
**Note:** If you build the library with this option turned on, you must also have written
`#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 1`
before including the `printf.h` header. | +| PRINTF_ALIAS_STANDARD_FUNCTION_NAMES | NONE | Alias the standard library function names (`printf()`, `sprintf()` etc.) to the library's functions.
The possible values are `NONE`, `SOFT` and `HARD`. With Soft aliasing, the library's object files contain symbols which do not clash with the standard library's: `printf_`, `sprintd_` etc; and a macro in `printf.h` replaces usages of `printf()`, `sprintf()` etc. with the underscored versions. With Hard aliasing, no such macro is used, and the library's object files contain `printf`, `sprintf` etc. - and thus cannot be linked together with a full-fledged standard library. **Note:** The preprocessort definitions `#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT` and `#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD` should be defined to have the same values when using the library as when having compiled the list. | | PRINTF_INTEGER_BUFFER_SIZE | 32 | ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack. | | PRINTF_DECIMAL_BUFFER_SIZE | 32 | ftoa (float) conversion buffer size. This must be big enough to hold one converted float number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack. | | PRINTF_DEFAULT_FLOAT_PRECISION | 6 | Define the default floating point precision| diff --git a/src/printf/printf.c b/src/printf/printf.c index 5f083a80..8066e966 100644 --- a/src/printf/printf.c +++ b/src/printf/printf.c @@ -1,6 +1,6 @@ /** * @author (c) Eyal Rozenberg - * 2021-2022, Haifa, Palestine/Israel + * 2021-2023, Haifa, Palestine/Israel * @author (c) Marco Paland (info@paland.com) * 2014-2019, PALANDesign Hannover, Germany * @@ -54,7 +54,7 @@ #include #endif // __cplusplus -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES +#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD # define printf_ printf # define sprintf_ sprintf # define vsprintf_ vsprintf diff --git a/src/printf/printf.h b/src/printf/printf.h index fb43fe2a..dbca4210 100644 --- a/src/printf/printf.h +++ b/src/printf/printf.h @@ -1,6 +1,6 @@ /** * @author (c) Eyal Rozenberg - * 2021-2022, Haifa, Palestine/Israel + * 2021-2023, Haifa, Palestine/Israel * @author (c) Marco Paland (info@paland.com) * 2014-2019, PALANDesign Hannover, Germany * @@ -62,8 +62,12 @@ __attribute__((format(printf, (one_based_format_index), (first_arg)))) # define ATTR_VPRINTF(one_based_format_index) #endif -#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES -#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 0 +#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT +#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT 0 +#endif + +#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD +#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD 0 #endif #if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 60a1e560..ce3ac4e0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -33,6 +33,9 @@ set_target_properties( C_STANDARD_REQUIRED YES C_EXTENSIONS NO ) +if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_C_COMPILER_ID} STREQUAL "Clang") + target_compile_options(aliasing PRIVATE -fno-builtin-printf) +endif() list(APPEND test_targets aliasing) foreach(tgt ${test_targets}) diff --git a/test/aliasing.c b/test/aliasing.c index 4d227c09..0b8ce904 100644 --- a/test/aliasing.c +++ b/test/aliasing.c @@ -34,7 +34,7 @@ void clear_buffer(void) int main(void) { -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES +#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT || PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD clear_buffer(); printf("printf'ing an integer: %d and a string: %s\n", 12, "Hello world"); const char* expected = "printf'ing an integer: 12 and a string: Hello world\n"; diff --git a/test/autotest.cpp b/test/autotest.cpp index f5138bc8..d0a61af6 100644 --- a/test/autotest.cpp +++ b/test/autotest.cpp @@ -14,11 +14,15 @@ #include "printf_config.h" #include "../src/printf/printf.h" -#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES -#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 0 +#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT +#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT 0 #endif -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES +#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD +#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD 0 +#endif + +#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT || PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD # define printf_ printf # define sprintf_ sprintf # define vsprintf_ vsprintf @@ -599,6 +603,7 @@ static void test_g(void) fprintf(dst, "mpa = \"%s\"\n", tst_buf); } + float rand_float(float a, float b) { float random = ((float) rand()) / (float) RAND_MAX;