Make GCC's implicit fall-through static analysis stricter by requiring the use of the fallthrough attribute statement instead of comments.
This makes the QEMU code style more consistent. Signed-off-by: Emmanouil Pitsidianakis <manos.pitsidiana...@linaro.org> --- docs/devel/style.rst | 23 +++++++++++++++++++++++ meson.build | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/devel/style.rst b/docs/devel/style.rst index 2f68b50079..f473dd24e9 100644 --- a/docs/devel/style.rst +++ b/docs/devel/style.rst @@ -612,28 +612,51 @@ While this generally results in simpler, less leak-prone code, there are still some caveats to beware of * Variables declared with g_auto* MUST always be initialized, otherwise the cleanup function will use uninitialized stack memory * If a variable declared with g_auto* holds a value which must live beyond the life of the function, that value must be saved and the original variable NULL'd out. This can be simpler using g_steal_pointer .. code-block:: c char *somefunc(void) { g_autofree char *foo = g_strdup_printf("foo%", "wibble"); g_autoptr (GList) bar = ..... if (eek) { return NULL; } return g_steal_pointer(&foo); } +Implicit switch case fall-through +================================= + +The C language allows switch cases to "fall-through" when a "break" statement +is missing at the end of a case. This, however, introduces ambiguity in the +code, as it's not always clear if the missing break is intentional or a bug. + +As this behaviour allows for bugs we do not allow "implicit fall-through". + +In order to identify intentional fall-through cases, we have adopted a +pseudo-keyword macro 'fallthrough' which expands to gcc's extension +__attribute__((__fallthrough__)). `Statement Attributes +<https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html>`_ + +All switch/case blocks must end in one of: + +.. code-block:: c + + break; + fallthrough; + continue; + goto <label>; + return [expression]; QEMU Specific Idioms ******************** diff --git a/meson.build b/meson.build index 79aef19bdc..e8805f0e0c 100644 --- a/meson.build +++ b/meson.build @@ -438,28 +438,28 @@ add_global_link_arguments(qemu_ldflags, native: false, language: all_languages) warn_flags = [ '-Wundef', '-Wwrite-strings', '-Wmissing-prototypes', '-Wstrict-prototypes', '-Wredundant-decls', '-Wold-style-declaration', '-Wold-style-definition', '-Wtype-limits', '-Wformat-security', '-Wformat-y2k', '-Winit-self', '-Wignored-qualifiers', '-Wempty-body', '-Wnested-externs', '-Wendif-labels', '-Wexpansion-to-defined', - '-Wimplicit-fallthrough=2', + '-Wimplicit-fallthrough=5', '-Wmissing-format-attribute', '-Wno-initializer-overrides', '-Wno-missing-include-dirs', '-Wno-shift-negative-value', '-Wno-string-plus-int', '-Wno-typedef-redefinition', '-Wno-tautological-type-limit-compare', '-Wno-psabi', '-Wno-gnu-variable-sized-type-not-at-end', ] -- 2.39.2