Skip to content

Commit d5803c5

Browse files
committed
Fixes #54: Fixed the mess I created in the not-really-a-fix for issue #14. Added a test for whether aliasing works, in which the standard library function names are used, without including the <stdio.h> or any other standard library headers.
1 parent 72804b4 commit d5803c5

File tree

6 files changed

+117
-14
lines changed

6 files changed

+117
-14
lines changed

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ foreach(opt
2929
set("PRINTF_${opt}" 1)
3030
else()
3131
set("PRINTF_${opt}" 0)
32-
message(STATUS "PRINTF_${opt} 0")
3332
endif()
3433
endforeach()
3534

printf.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,26 @@
3939
#include <stdbool.h>
4040
#include <stdint.h>
4141

42-
#include "printf.h"
43-
44-
4542
// Define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
4643
// printf_config.h header file
47-
// default: undefined
4844
#ifdef PRINTF_INCLUDE_CONFIG_H
45+
#if PRINTF_INCLUDE_CONFIG_H
4946
#include "printf_config.h"
5047
#endif
48+
#endif
49+
50+
#include "printf.h"
51+
52+
#ifdef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
53+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
54+
# define printf_ printf
55+
# define sprintf_ sprintf
56+
# define vsprintf_ vsprintf
57+
# define snprintf_ snprintf
58+
# define vsnprintf_ vsnprintf
59+
# define vprintf_ vprintf
60+
#endif
61+
#endif
5162

5263

5364
// 'ntoa' conversion buffer size, this must be big enough to hold one converted
@@ -1148,3 +1159,4 @@ int vfctprintf(void (*out)(char character, void* arg), void* arg, const char* fo
11481159
const out_fct_wrap_type out_fct_wrap = { out, arg };
11491160
return _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va);
11501161
}
1162+

printf.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ __attribute__((format(__printf__, (one_based_format_index), (first_arg))))
5757
#endif
5858

5959
#ifdef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
60-
# define printf_ printf_
61-
# define sprintf_ sprintf_
62-
# define vsprintf_ vsprintf_
63-
# define snprintf_ snprintf_
64-
# define vsnprintf_ vsnprintf_
65-
# define vprintf_ vprintf_
60+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
61+
# define printf_ printf
62+
# define sprintf_ sprintf
63+
# define vsprintf_ vsprintf
64+
# define snprintf_ snprintf
65+
# define vsnprintf_ vsnprintf
66+
# define vprintf_ vprintf
67+
#endif
6668
#endif
67-
6869

6970
/**
7071
* Output a character to a custom device like UART, used by the printf() function
@@ -137,12 +138,14 @@ int vfctprintf(void (*out)(char character, void* arg), void* arg, const char* fo
137138
#endif
138139

139140
#ifdef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
141+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
140142
# undef printf_
141143
# undef sprintf_
142144
# undef vsprintf_
143145
# undef snprintf_
144146
# undef vsnprintf_
145147
# undef vprintf_
146148
#endif
149+
#endif
147150

148151
#endif // PRINTF_H_

test/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,19 @@ foreach(tgt ${test_targets})
2020
if (TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
2121
target_compile_definitions(${tgt} PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
2222
endif()
23+
endforeach()
2324

25+
add_executable(aliasing "aliasing.c")
26+
set_target_properties(
27+
${tgt}
28+
PROPERTIES
29+
C_STANDARD 11
30+
C_STANDARD_REQUIRED YES
31+
C_EXTENSIONS NO
32+
)
33+
list(APPEND test_targets aliasing)
2434

35+
foreach(tgt ${test_targets})
2536
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
2637
target_compile_options(test_suite PRIVATE /W4)
2738
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
@@ -70,5 +81,15 @@ add_test(
7081
target_link_libraries(autotest PRIVATE printf)
7182
target_include_directories(autotest PRIVATE "$<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>")
7283

84+
add_test(
85+
NAME "${PROJECT_NAME}.aliasing"
86+
COMMAND "aliasing"
87+
)
88+
target_link_libraries(aliasing PRIVATE printf)
89+
target_include_directories(aliasing PRIVATE "$<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>")
90+
91+
92+
93+
7394
# Not running autotest by default - it's randomized after all.
7495

test/aliasing.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <printf_config.h>
2+
3+
// Note: If we include <printf.h>, that will likely get us the
4+
// the file /usr/include/printf.h on Linux systems
5+
#include "../printf.h"
6+
7+
int strcmp_(const char* lhs, const char* rhs)
8+
{
9+
while (1) {
10+
char lhs_c = *lhs++;
11+
char rhs_c = *rhs++;
12+
if (lhs_c != rhs_c) { return lhs_c - rhs_c; }
13+
if (lhs_c == '\0') { return 0; }
14+
}
15+
}
16+
17+
enum { BufferSize = 100 };
18+
char buffer[BufferSize];
19+
20+
void _putchar(char c)
21+
{
22+
for(char* ptr = buffer; ptr - buffer < BufferSize; ptr++) {
23+
if (*ptr == '\0') {
24+
*ptr++ = c;
25+
*ptr++ = '\0';
26+
return;
27+
}
28+
}
29+
}
30+
31+
void clear_buffer()
32+
{
33+
for(int i = 0; i < BufferSize; i++) {
34+
buffer[i] = '\0';
35+
}
36+
}
37+
38+
int main()
39+
{
40+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
41+
clear_buffer();
42+
printf("printf'ing an integer: %d and a string: %s\n", 12, "Hello world");
43+
const char* expected = "sprintf'ing an integer: 12 and a string: Hello world\n";
44+
if (strcmp_(buffer, expected) != 0) {
45+
return(-1);
46+
}
47+
clear_buffer();
48+
sprintf(buffer, "sprintf'ing an integer: %d and a string: %s\n", 34, "quick brown fox");
49+
expected = "sprintf'ing an integer: 34 and a string: quick brown fox\n";
50+
if (strcmp_(buffer, expected) != 0) {
51+
return(-1);
52+
}
53+
#endif
54+
return 0;
55+
}

test/autotest.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,22 @@
1111
#endif
1212
#include <assert.h>
1313

14-
#undef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
15-
#include "../printf.h"
1614
#include "printf_config.h"
15+
#include "../printf.h"
16+
17+
#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
18+
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 0
19+
#endif
20+
21+
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES
22+
# define printf_ printf
23+
# define sprintf_ sprintf
24+
# define vsprintf_ vsprintf
25+
# define snprintf_ snprintf
26+
# define vsnprintf_ vsnprintf
27+
# define vprintf_ vprintf
28+
#endif
29+
1730

1831
//*******************************************************
1932
// Defines

0 commit comments

Comments
 (0)