Do not allow enum typedef forward-declaration to comply with C99 standard chapter §6.7.2.2 point 4:
Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration. Update checkpatch.pl to catch further additions. Signed-off-by: Philippe Mathieu-Daudé <[email protected]> Reviewed-by: Pierrick Bouvier <[email protected]> --- docs/devel/style.rst | 11 +++++++++++ scripts/checkpatch.pl | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/docs/devel/style.rst b/docs/devel/style.rst index 12e509d10de..5ab5c21447d 100644 --- a/docs/devel/style.rst +++ b/docs/devel/style.rst @@ -416,6 +416,17 @@ definitions instead of typedefs in headers and function prototypes; this avoids problems with duplicated typedefs and reduces the need to include headers from other headers. +Enumeration (enum) type can not be forward declared as typedef, because +C compilers should be able to know the size of enums before hand. Simply +define the typedef along with the enum: + +.. code-block:: c + + typedef enum MyEnum { + FOO, + BAR, + } MyEnum; + Bitfields --------- diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 3a9557417f7..119b2a6c002 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -2414,6 +2414,11 @@ sub process { ERROR("missing space after $1 definition\n" . $herecurr); } +# forward declared enum typedef + if ($line =~ /^.\s*typedef\s+enum(?:\s+$Ident)?(?:\s+$Ident)?;/) { + ERROR("forward declared enum typedef\n" . $herecurr); + } + # check for spacing round square brackets; allowed: # 1. with a type on the left -- int [] a; # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, -- 2.52.0
