diff --git a/.gitignore b/.gitignore index dcc56bad..c44b26b5 100644 --- a/.gitignore +++ b/.gitignore @@ -86,6 +86,7 @@ /examples/macro_cast /examples/macro_container_of /examples/macro_packing +/examples/macro_static_test /examples/memmap /examples/multiboot2_header_macro /examples/ntoa diff --git a/ChangeLog b/ChangeLog index 169f8237..e659c8cc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2022-12-11 Alex Kotov + + * include/kernaux/macro.h: Macro "KERNAUX_STATIC_TEST" has been added + 2022-12-10 Alex Kotov * include/kernaux/macro.h: Macros "KERNAUX_CAST_(VAR|CONST)" have been added diff --git a/README.md b/README.md index a3281810..69e5b446 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,9 @@ zero). Work-in-progress APIs can change at any time. * [Macros](/include/kernaux/macro.h) (*non-breaking since* **0.6.0**) * [Example: packing](/examples/macro_packing.c) * [Example: CAST\_\*](/examples/macro_cast.c); - * [Example: CONTAINER_OF](/examples/macro_container_of.c) + * [Example: CONTAINER\_OF](/examples/macro_container_of.c) * [Example: BITS](/examples/macro_bits.c) + * [Example: STATIC\_TEST\*](/examples/macro_static_test.c) * [Assertions](/include/kernaux/assert.h) (*non-breaking since* **0.4.0**) * [Example: Assert](/examples/assert.c) * [Example: Panic](/examples/panic.c) diff --git a/examples/Makefile.am b/examples/Makefile.am index f3c6dcf8..dc336713 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -77,6 +77,14 @@ TESTS += macro_packing macro_packing_LDADD = $(top_builddir)/libkernaux.la macro_packing_SOURCES = main.c macro_packing.c +##################### +# macro_static_test # +##################### + +TESTS += macro_static_test +macro_static_test_LDADD = $(top_builddir)/libkernaux.la +macro_static_test_SOURCES = main.c macro_static_test.c + ########## # memmap # ########## diff --git a/examples/macro_static_test.c b/examples/macro_static_test.c new file mode 100644 index 00000000..c3bc054e --- /dev/null +++ b/examples/macro_static_test.c @@ -0,0 +1,28 @@ +#include + +#include + +KERNAUX_STATIC_TEST(uint8_t_size, sizeof(uint8_t) == 1); +KERNAUX_STATIC_TEST(uint16_t_size, sizeof(uint16_t) == 2); +KERNAUX_STATIC_TEST(uint32_t_size, sizeof(uint32_t) == 4); +KERNAUX_STATIC_TEST(uint64_t_size, sizeof(uint64_t) == 8); + +#include + +struct Foo { + uint8_t a; + uint32_t b; +} KERNAUX_PACKED; + +KERNAUX_STATIC_TEST_STRUCT_SIZE(Foo, 5); + +union Bar { + uint8_t a; + uint16_t b; +} KERNAUX_PACKED; + +KERNAUX_STATIC_TEST_UNION_SIZE(Bar, 2); + +#include + +void example_main() {} diff --git a/include/kernaux/macro.h b/include/kernaux/macro.h index 820c43cc..4fb17955 100644 --- a/include/kernaux/macro.h +++ b/include/kernaux/macro.h @@ -50,19 +50,16 @@ extern "C" { * Static assertions * *********************/ +#define KERNAUX_STATIC_TEST(name, cond) \ + KERNAUX_UNUSED \ + static const int \ + _kernaux_static_test_##name[(cond) ? 1 : -1] + #define KERNAUX_STATIC_TEST_STRUCT_SIZE(name, size) \ -KERNAUX_UNUSED \ -static const int \ -_kernaux_static_test_struct_size_##name[ \ - sizeof(struct name) == (size) ? 1 : -1 \ -] + KERNAUX_STATIC_TEST(struct_size_##name, sizeof(struct name) == (size)) #define KERNAUX_STATIC_TEST_UNION_SIZE(name, size) \ -KERNAUX_UNUSED \ -static const int \ -_kernaux_static_test_union_size_##name[ \ - sizeof(union name) == (size) ? 1 : -1 \ -] + KERNAUX_STATIC_TEST(union_size_##name, sizeof(union name) == (size)) /***************** * Simple values * @@ -89,17 +86,9 @@ _kernaux_static_test_union_size_##name[ \ *********************/ #define KERNAUX_CAST_VAR(type, name, value) \ - { \ - KERNAUX_UNUSED \ - static const int _kernaux_static_test_cast_pos_##name[ \ - sizeof(value) <= sizeof(type) ? 1 : -1 \ - ]; \ - KERNAUX_UNUSED \ - static const int _kernaux_static_test_cast_neg_##name[ \ - sizeof(-(value)) <= sizeof(type) ? 1 : -1 \ - ]; \ - } \ - type name = (type)(value); \ + KERNAUX_STATIC_TEST(cast_pos_##name, sizeof(value) <= sizeof(type)); \ + KERNAUX_STATIC_TEST(cast_neg_##name, sizeof(-(value)) <= sizeof(type)); \ + type name = (type)(value); \ do {} while (0) #define KERNAUX_CAST_CONST(type, name, value) \ diff --git a/make/checks.am b/make/checks.am index bfb0b861..cd68bd15 100644 --- a/make/checks.am +++ b/make/checks.am @@ -21,7 +21,7 @@ CPPCHECK_INC = \ CPPCHECK_SUPPRESS = \ --suppress='constArgument:$(top_srcdir)/examples/macro_cast.c' \ - --suppress='unusedStructMember:$(top_srcdir)/examples/multiboot2_header_macro.c' \ + --suppress='unusedStructMember:$(top_srcdir)/examples/*.c' \ --suppress='unusedStructMember:$(top_srcdir)/tests/test_multiboot2_info_*.c' CPPCHECK_PATHS = \