Skip to content

Commit

Permalink
Regards #146, regards #141: sorted out aliasing-related preprocessor …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
eyalroz committed Jan 24, 2023
1 parent c7cb01d commit ceb6db5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>**Note:** If you build the library with this option turned on, you must also have written<br>`#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 1`<br>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.<br>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|
Expand Down
4 changes: 2 additions & 2 deletions src/printf/printf.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @author (c) Eyal Rozenberg <eyalroz1@gmx.com>
* 2021-2022, Haifa, Palestine/Israel
* 2021-2023, Haifa, Palestine/Israel
* @author (c) Marco Paland (info@paland.com)
* 2014-2019, PALANDesign Hannover, Germany
*
Expand Down Expand Up @@ -54,7 +54,7 @@
#include <stdbool.h>
#endif // __cplusplus

#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
# define printf_ printf
# define sprintf_ sprintf
# define vsprintf_ vsprintf
Expand Down
10 changes: 7 additions & 3 deletions src/printf/printf.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @author (c) Eyal Rozenberg <eyalroz1@gmx.com>
* 2021-2022, Haifa, Palestine/Israel
* 2021-2023, Haifa, Palestine/Israel
* @author (c) Marco Paland (info@paland.com)
* 2014-2019, PALANDesign Hannover, Germany
*
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion test/aliasing.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
11 changes: 8 additions & 3 deletions test/autotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ceb6db5

Please sign in to comment.