[PATCH v2 2/2] docs/devel: Mention enum typedefs forward-declaration is not allowed

Philippe Mathieu-Daudé posted 2 patches 1 week, 2 days ago
Maintainers: "Alex Bennée" <alex.bennee@linaro.org>, "Daniel P. Berrangé" <berrange@redhat.com>, Thomas Huth <thuth@redhat.com>, Markus Armbruster <armbru@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Pierrick Bouvier <pierrick.bouvier@linaro.org>, Alistair Francis <alistair@alistair23.me>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Peter Maydell <peter.maydell@linaro.org>, Palmer Dabbelt <palmer@dabbelt.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <dbarboza@ventanamicro.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, "Michael S. Tsirkin" <mst@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Stefano Stabellini <sstabellini@kernel.org>, Anthony PERARD <anthony@xenproject.org>, Paul Durrant <paul@xen.org>, Paolo Bonzini <pbonzini@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Christian Borntraeger <borntraeger@linux.ibm.com>, Jared Rossi <jrossi@linux.ibm.com>, Zhuoying Cai <zycai@linux.ibm.com>, Michael Roth <michael.roth@amd.com>, Fabiano Rosas <farosas@suse.de>, Laurent Vivier <lvivier@redhat.com>, Eduardo Habkost <eduardo@habkost.net>
[PATCH v2 2/2] docs/devel: Mention enum typedefs forward-declaration is not allowed
Posted by Philippe Mathieu-Daudé 1 week, 2 days ago
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é <philmd@linaro.org>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
---
 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