From 3adba586bc8ffb4c1ad1ca39f85e737dd102658d Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Thu, 27 Feb 2025 12:38:28 +0800 Subject: [PATCH 1/2] Be aware of reserved keywords in C The list of reserved C keywords has been refined up to C23 standard. Change-Id: I35cde1d00219d7590ad188baf1b362698ee416a7 --- scripts/aspell-pws | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/scripts/aspell-pws b/scripts/aspell-pws index 4355f539f..c5db81fdd 100644 --- a/scripts/aspell-pws +++ b/scripts/aspell-pws @@ -338,3 +338,15 @@ riscv info cpu libvirt +preprocessor +sizeof +alignas +alignof +constexpr +enum +char +nullptr +typedef +BitInt +noreturn +pragma From da37e9ede704a8c356e7dc3d0aee58ac4c8b5b3a Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Thu, 27 Feb 2025 12:40:04 +0800 Subject: [PATCH 2/2] Avoid evaluating the sizeof in C preprocessor The C preprocessor operates purely on text, substituting macros and evaluating simple arithmetic expressions involving literal constants. It does not understand types, so it cannot evaluate operators like sizeof that require type information and compile-time type checking. Change-Id: Id8b453db78571d67a8b7f95b240b71df212d7796 --- CONTRIBUTING.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 504eff587..cd214baec 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -766,15 +766,13 @@ typedef struct { uint16_t periodic : 1; /* offset 6 bit 0 */ } mytimer_t; -/* Preprocessor check of timer register layout byte count. */ -#if (sizeof(mytimer_t) != 8) -#error mytimer_t struct size incorrect (expected 8 bytes) -#endif +_Static_assert(sizeof(mytimer_t) == 8, + "mytimer_t struct size incorrect (expected 8 bytes)"); ``` To enhance portability, use standard-defined types (e.g., `uint16_t`, `uint32_t`) and avoid relying on compiler-specific behavior. Where precise control over memory layout is required, such as in embedded systems or when interfacing with hardware, -always verify the structure size and layout using static assertions or preprocessor checks. +always verify the structure size and layout using static assertions. #### Avoid extreme portability