Skip to content

Commit 5d64bb8

Browse files
committed
cbprintf: Remove support for toolchains without _Generic
As part of the transition from C99 to C17 as the minimum supported C standard version, remove the workarounds in the formatted output subsystem to maintain compatibility with toolchains that do not support the _Generic keyword. Signed-off-by: Carles Cufi <[email protected]>
1 parent c9ad3d4 commit 5d64bb8

File tree

8 files changed

+3
-111
lines changed

8 files changed

+3
-111
lines changed

doc/services/formatted_output.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ Package can be created using two methods:
8181
package.
8282
* static - types of arguments are detected at compile time by the preprocessor
8383
and package is created as simple assignments to a provided memory. This method
84-
is significantly faster than runtime (more than 15 times) but has following
85-
limitations: requires ``_Generic`` keyword (C11 feature) to be supported by
86-
the compiler and cannot distinguish between ``%p`` and ``%s`` if char pointer
84+
is significantly faster than runtime (more than 15 times) but has a
85+
significant limitation: it cannot distinguish between ``%p`` and ``%s`` if char pointer
8786
is used. It treats all (unsigned) char pointers as ``%s`` thus it will attempt
8887
to append string to a package. It can be handled correctly during conversion
8988
from **transient** package to **self-contained** package using
@@ -174,7 +173,6 @@ formatting since address changes whenever package is copied.
174173
Limitations and recommendations
175174
===============================
176175

177-
* C11 ``_Generic`` support is required by the compiler to use static (fast) packaging.
178176
* It is recommended to cast any character pointer used with ``%p`` format specifier to
179177
other pointer type (e.g. ``void *``). If format string is not accessible then only
180178
static packaging is possible and it will append all detected strings. Character pointer

doc/services/logging/index.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,6 @@ The are following recommendations:
869869

870870
* Enable :kconfig:option:`CONFIG_LOG_SPEED` to slightly speed up deferred logging at the
871871
cost of slight increase in memory footprint.
872-
* Compiler with C11 ``_Generic`` keyword support is recommended. Logging
873-
performance is significantly degraded without it. See :ref:`cbprintf_packaging`.
874872
* It is recommended to cast pointer to ``const char *`` when it is used with ``%s``
875873
format specifier and it points to a constant string.
876874
* It is recommended to cast pointer to ``char *`` when it is used with ``%s``

include/zephyr/sys/cbprintf.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@
1717
#include <stdio.h>
1818
#endif /* CONFIG_CBPRINTF_LIBC_SUBSTS */
1919

20-
/* Determine if _Generic is supported using macro from toolchain.h.
21-
*
22-
* @note Z_C_GENERIC is also set for C++ where functionality is implemented
23-
* using overloading and templates.
24-
*/
25-
#ifndef Z_C_GENERIC
26-
#if defined(__cplusplus) || TOOLCHAIN_HAS_C_GENERIC
27-
#define Z_C_GENERIC 1
28-
#else
29-
#define Z_C_GENERIC 0
30-
#endif
31-
#endif
32-
3320
#ifdef __xtensa__
3421
#define Z_PKG_HDR_EXT_XTENSA_ALIGNMENT 8
3522
#ifdef CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS
@@ -120,7 +107,6 @@ BUILD_ASSERT(sizeof(struct cbprintf_package_hdr_ext) % Z_PKG_HDR_EXT_XTENSA_ALIG
120107
* @endcond
121108
*/
122109

123-
/* Z_C_GENERIC is used there */
124110
#include <zephyr/sys/cbprintf_internal.h>
125111

126112
#ifdef __cplusplus

include/zephyr/sys/cbprintf_internal.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ extern "C" {
534534
* @retval 1 if string must be packaged at runtime.
535535
* @retval 0 if string can be statically packaged.
536536
*/
537-
#if Z_C_GENERIC
538537
#define Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ...) ({\
539538
int _rv; \
540539
if ((flags) & CBPRINTF_PACKAGE_ADD_RW_STR_POS) { \
@@ -544,9 +543,6 @@ extern "C" {
544543
} \
545544
_rv; \
546545
})
547-
#else
548-
#define Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ...) 1
549-
#endif
550546

551547
/** @brief Get storage size for given argument.
552548
*
@@ -864,23 +860,10 @@ do { \
864860
} \
865861
} while (false)
866862

867-
#if Z_C_GENERIC
868863
#define Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
869864
... /* fmt, ... */) \
870865
Z_CBPRINTF_STATIC_PACKAGE_GENERIC(packaged, inlen, outlen, \
871866
align_offset, flags, __VA_ARGS__)
872-
#else
873-
#define Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
874-
... /* fmt, ... */) \
875-
do { \
876-
/* Small trick needed to avoid warning on always true */ \
877-
if (((uintptr_t)packaged + 1) != 1) { \
878-
outlen = cbprintf_package(packaged, inlen, flags, __VA_ARGS__); \
879-
} else { \
880-
outlen = cbprintf_package(NULL, align_offset, flags, __VA_ARGS__); \
881-
} \
882-
} while (false)
883-
#endif /* Z_C_GENERIC */
884867

885868
#ifdef __cplusplus
886869
}

lib/os/cbprintf_packaged.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
#include <zephyr/logging/log.h>
1818
LOG_MODULE_REGISTER(cbprintf_package, CONFIG_CBPRINTF_PACKAGE_LOG_LEVEL);
1919

20-
#if defined(CONFIG_CBPRINTF_PACKAGE_SUPPORT_TAGGED_ARGUMENTS) && \
21-
!Z_C_GENERIC
22-
#error "CONFIG_CBPRINTF_PACKAGE_SUPPORT_TAGGED_ARGUMENTS " \
23-
"requires toolchain to support _Generic!"
24-
#endif
25-
2620
/**
2721
* @brief Check if address is in read only section.
2822
*

tests/lib/cbprintf_package/src/main.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ static void unpack(const char *desc, struct out_buffer *buf,
5959

6060
#define TEST_PACKAGING(flags, fmt, ...) do { \
6161
int must_runtime = CBPRINTF_MUST_RUNTIME_PACKAGE(flags, fmt, __VA_ARGS__); \
62-
zassert_equal(must_runtime, !Z_C_GENERIC); \
6362
snprintfcb(compare_buf, sizeof(compare_buf), fmt, __VA_ARGS__); \
6463
printk("-----------------------------------------\n"); \
6564
printk("%s\n", compare_buf); \
@@ -683,14 +682,6 @@ ZTEST(cbprintf_package, test_cbprintf_ro_rw_loc_const_char_ptr)
683682

684683
static void cbprintf_rw_loc_const_char_ptr(bool keep_ro_str)
685684
{
686-
/* Test requires that static packaging is applied. Runtime packaging
687-
* cannot be tricked because it checks pointers against read only
688-
* section.
689-
*/
690-
if (Z_C_GENERIC == 0) {
691-
ztest_test_skip();
692-
}
693-
694685
int slen, slen2;
695686
int clen, clen2;
696687
static const char test_str[] = "test %s %d %s";
@@ -778,10 +769,6 @@ ZTEST(cbprintf_package, test_cbprintf_must_runtime_package)
778769
{
779770
int rv;
780771

781-
if (Z_C_GENERIC == 0) {
782-
ztest_test_skip();
783-
}
784-
785772
rv = CBPRINTF_MUST_RUNTIME_PACKAGE(0, "test");
786773
zassert_equal(rv, 0);
787774

@@ -1006,7 +993,6 @@ static void *print_size_and_alignment_info(void)
1006993
printk("alignof: int=%zu long=%zu ptr=%zu long long=%zu double=%zu long double=%zu\n",
1007994
__alignof__(int), __alignof__(long), __alignof__(void *), __alignof__(long long),
1008995
__alignof__(double), __alignof__(long double));
1009-
printk("%s C11 _Generic\n", Z_C_GENERIC ? "With" : "Without");
1010996
#endif
1011997

1012998
return NULL;

tests/lib/cbprintf_package/testcase.yaml

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ tests:
1111
integration_platforms:
1212
- native_sim
1313

14-
libraries.cbprintf.package_no_generic:
15-
extra_configs:
16-
- CONFIG_CBPRINTF_COMPLETE=y
17-
- CONFIG_COMPILER_OPT="-DZ_C_GENERIC=0"
18-
integration_platforms:
19-
- native_sim
20-
2114
libraries.cbprintf.package_fp:
2215
filter: CONFIG_CPU_HAS_FPU
2316
extra_configs:
@@ -76,14 +69,6 @@ tests:
7669
integration_platforms:
7770
- native_sim
7871

79-
libraries.cbprintf.package_no_generic_cpp:
80-
extra_configs:
81-
- CONFIG_CPP=y
82-
- CONFIG_CBPRINTF_COMPLETE=y
83-
- CONFIG_COMPILER_OPT="-DZ_C_GENERIC=0"
84-
integration_platforms:
85-
- native_sim
86-
8772
libraries.cbprintf.package_fp_cpp:
8873
filter: CONFIG_CPU_HAS_FPU
8974
extra_configs:
@@ -148,16 +133,6 @@ tests:
148133
integration_platforms:
149134
- qemu_x86
150135

151-
libraries.cbprintf.package_no_generic.picolibc:
152-
filter: CONFIG_PICOLIBC_SUPPORTED
153-
tags: picolibc
154-
extra_configs:
155-
- CONFIG_CBPRINTF_COMPLETE=y
156-
- CONFIG_COMPILER_OPT="-DZ_C_GENERIC=0"
157-
- CONFIG_PICOLIBC=y
158-
integration_platforms:
159-
- qemu_x86
160-
161136
libraries.cbprintf.package_fp.picolibc:
162137
filter: CONFIG_CPU_HAS_FPU and CONFIG_PICOLIBC_SUPPORTED
163138
tags: picolibc
@@ -180,17 +155,6 @@ tests:
180155
integration_platforms:
181156
- qemu_x86
182157

183-
libraries.cbprintf.package_no_generic_cpp.picolibc:
184-
filter: CONFIG_PICOLIBC_SUPPORTED
185-
tags: picolibc
186-
extra_configs:
187-
- CONFIG_CPP=y
188-
- CONFIG_CBPRINTF_COMPLETE=y
189-
- CONFIG_COMPILER_OPT="-DZ_C_GENERIC=0"
190-
- CONFIG_PICOLIBC=y
191-
integration_platforms:
192-
- qemu_x86
193-
194158
libraries.cbprintf.package_fp_cpp.picolibc:
195159
filter: CONFIG_CPU_HAS_FPU and CONFIG_PICOLIBC_SUPPORTED
196160
tags: picolibc

tests/unit/cbprintf/main.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
#else
3636
#define USE_PACKAGED 0
3737
#endif
38-
#if (VIA_TWISTER & 0x800) != 0
39-
#define AVOID_C_GENERIC 1
40-
#endif
4138
#if (VIA_TWISTER & 0x1000) != 0
4239
#define PKG_ALIGN_OFFSET sizeof(void *)
4340
#endif
@@ -63,10 +60,6 @@
6360
#define ENABLED_USE_PACKAGED false
6461
#endif
6562

66-
#if AVOID_C_GENERIC
67-
#define Z_C_GENERIC 0
68-
#endif
69-
7063
#ifndef PACKAGE_FLAGS
7164
#define PACKAGE_FLAGS 0
7265
#endif
@@ -1181,11 +1174,6 @@ ZTEST(prf, test_cbprintf_package_rw_string_indexes)
11811174
return;
11821175
}
11831176

1184-
if (!Z_C_GENERIC) {
1185-
/* runtime packaging will not detect ro strings. */
1186-
return;
1187-
}
1188-
11891177
int len0, len1;
11901178
static const char *test_str = "test %d %s";
11911179
static const char *test_str1 = "lorem ipsum";
@@ -1249,10 +1237,6 @@ ZTEST(prf, test_cbprintf_fsc_package)
12491237
return;
12501238
}
12511239

1252-
if (!Z_C_GENERIC) {
1253-
/* runtime packaging will not detect ro strings. */
1254-
return;
1255-
}
12561240

12571241
char test_str[] = "test %d %s";
12581242
const char *test_str1 = "lorem ipsum";
@@ -1449,8 +1433,7 @@ static void *cbprintf_setup(void)
14491433
TC_PRINT(" NANO\n");
14501434
}
14511435
if (ENABLED_USE_PACKAGED) {
1452-
TC_PRINT(" PACKAGED %s C11 _Generic\n",
1453-
Z_C_GENERIC ? "with" : "without");
1436+
TC_PRINT(" PACKAGED\n");
14541437
} else {
14551438
TC_PRINT(" VA_LIST\n");
14561439
}

0 commit comments

Comments
 (0)