Skip to contentSkip to conversation

tcg/optimize: Add fallthrough annotations

PATCH In review Thomas Huth 4 participants 4 messages Oct 29, 2020 – Oct 30, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcg/optimize: Add fallthrough annotations

To be able to compile this file with -Werror=implicit-fallthrough, we need to add some fallthrough annotations to the case statements that might fall through. Unfortunately, the typical "/* fallthrough */" comments do not work here as expected since some case labels are wrapped in macros and the compiler fails to match the comments in this case. But using __attribute__((fallthrough)) seems to work fine, so let's use that instead.

---
 tcg/optimize.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tcg/optimize.c +10 -0
index 220f4601d5..c2768c9770 100644
@@ -26,6 +26,12 @@
 #include "qemu/osdep.h"
 #include "tcg/tcg-op.h"
 
+#if __has_attribute(fallthrough)
+# define fallthrough() __attribute__((fallthrough))
+#else
+# define fallthrough() do {} while (0)
+#endif
+
 #define CASE_OP_32_64(x)                        \
         glue(glue(case INDEX_op_, x), _i32):    \
         glue(glue(case INDEX_op_, x), _i64)
@@ -855,6 +861,7 @@ void tcg_optimize(TCGContext *s)
             if ((arg_info(op->args[1])->mask & 0x80) != 0) {
                 break;
             }
+            fallthrough();
         CASE_OP_32_64(ext8u):
             mask = 0xff;
             goto and_const;
@@ -862,6 +869,7 @@ void tcg_optimize(TCGContext *s)
             if ((arg_info(op->args[1])->mask & 0x8000) != 0) {
                 break;
             }
+            fallthrough();
         CASE_OP_32_64(ext16u):
             mask = 0xffff;
             goto and_const;
@@ -869,6 +877,7 @@ void tcg_optimize(TCGContext *s)
             if ((arg_info(op->args[1])->mask & 0x80000000) != 0) {
                 break;
             }
+            fallthrough();
         case INDEX_op_ext32u_i64:
             mask = 0xffffffffU;
             goto and_const;
@@ -886,6 +895,7 @@ void tcg_optimize(TCGContext *s)
             if ((arg_info(op->args[1])->mask & 0x80000000) != 0) {
                 break;
             }
+            fallthrough();
         case INDEX_op_extu_i32_i64:
             /* We do not compute affected as it is a size changing op.  */
             mask = (uint32_t)arg_info(op->args[1])->mask;
Signature · 1 line
2.18.2
#1 lore raw
Quoted 18 lines · Thomas Huth

To be able to compile this file with -Werror=implicit-fallthrough, we need to add some fallthrough annotations to the case statements that might fall through. Unfortunately, the typical "/* fallthrough */" comments do not work here as expected since some case labels are wrapped in macros and the compiler fails to match the comments in this case. But using __attribute__((fallthrough)) seems to work fine, so let's use that instead.

---
 tcg/optimize.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tcg/optimize.c +0 -0
index 220f4601d5..c2768c9770 100644

tcg/optimize.c line 33

Quoted 8 lines
tcg/optimize.c +5 -0
@@ -26,6 +26,12 @@
 #include "qemu/osdep.h"
 #include "tcg/tcg-op.h"
 
+#if __has_attribute(fallthrough)
+# define fallthrough() __attribute__((fallthrough))
+#else
+# define fallthrough() do {} while (0)
+#endif

This looks something we can reuse, what about adding it as QEMU_FALLTHROUGH in "qemu/compiler.h"?

Quoted 37 lines
tcg/optimize.c +5 -0
+
 #define CASE_OP_32_64(x)                        \
         glue(glue(case INDEX_op_, x), _i32):    \
         glue(glue(case INDEX_op_, x), _i64)
@@ -855,6 +861,7 @@ void tcg_optimize(TCGContext *s)
             if ((arg_info(op->args[1])->mask & 0x80) != 0) {
                 break;
             }
+            fallthrough();
         CASE_OP_32_64(ext8u):
             mask = 0xff;
             goto and_const;
@@ -862,6 +869,7 @@ void tcg_optimize(TCGContext *s)
             if ((arg_info(op->args[1])->mask & 0x8000) != 0) {
                 break;
             }
+            fallthrough();
         CASE_OP_32_64(ext16u):
             mask = 0xffff;
             goto and_const;
@@ -869,6 +877,7 @@ void tcg_optimize(TCGContext *s)
             if ((arg_info(op->args[1])->mask & 0x80000000) != 0) {
                 break;
             }
+            fallthrough();
         case INDEX_op_ext32u_i64:
             mask = 0xffffffffU;
             goto and_const;
@@ -886,6 +895,7 @@ void tcg_optimize(TCGContext *s)
             if ((arg_info(op->args[1])->mask & 0x80000000) != 0) {
                 break;
             }
+            fallthrough();
         case INDEX_op_extu_i32_i64:
             /* We do not compute affected as it is a size changing op.  */
             mask = (uint32_t)arg_info(op->args[1])->mask;
#2 lore raw
In reply to Thomas Huth
Quoted 7 lines · Thomas Huth

To be able to compile this file with -Werror=implicit-fallthrough, we need to add some fallthrough annotations to the case statements that might fall through. Unfortunately, the typical "/* fallthrough */" comments do not work here as expected since some case labels are wrapped in macros and the compiler fails to match the comments in this case. But using __attribute__((fallthrough)) seems to work fine, so let's use that instead.

Why would the macro matter? It expands to two case statements with nothing in between them.

This sounds like a compiler bug that should be reported.

r~

#3 lore raw
Quoted 17 lines

-----Original Message-----
From: Richard Henderson [mailto:[email protected]]
Sent: Friday, October 30, 2020 4:07 AM
To: Thomas Huth <[email protected]>; Richard Henderson <[email protected]>; [email protected]
Cc: Chenqun (kuhn) <[email protected]>
Subject: Re: [PATCH] tcg/optimize: Add fallthrough annotations

Quoted 7 lines · Thomas Huth

To be able to compile this file with -Werror=implicit-fallthrough, we need to add some fallthrough annotations to the case statements that might fall through. Unfortunately, the typical "/* fallthrough */" comments do not work here as expected since some case labels are wrapped in macros and the compiler fails to match the comments in this case. But using __attribute__((fallthrough)) seems to work fine, so let's use that instead.

Quoted 5 lines

Why would the macro matter? It expands to two case statements with nothing in between them.

This sounds like a compiler bug that should be reported.

Hi all,
I have queried the GCC options description about the Wimplicit-fallthrough and verified it.
The value of Wimplicit-fallthrough ranges from 0 to 5.
The value 0 is to ignore all warnings, which is certainly not what we need.
If the value is set to 1 or 2, most fall through on the QEMU can take effect.
Eg:/* FALLTHRU */、/* fallthru */、/* fall-through */、/* FALLTHOUGH */、/* fall through */、/* fallthrough */..

When the value ranges from 3 to 5, more fallthrough comments become invalid as the value increases.

So, I agree with Philippe's suggestion to add a QEMU_FALLTHROUGH to unify this compiler property.

Thanks,
Chen Qun

Additional gcc information is as follows:
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
-Wimplicit-fallthrough is the same as -Wimplicit-fallthrough=3 and -Wno-implicit-fallthrough is the same as -Wimplicit-fallthrough=0.

The option argument n specifies what kind of comments are accepted:
-Wimplicit-fallthrough=0 disables the warning altogether.
-Wimplicit-fallthrough=1 matches .* regular expression, any comment is used as fallthrough comment. -Wimplicit-fallthrough=2 case insensitively matches .*falls?[ \t-]*thr(ough|u).* regular expression. -Wimplicit-fallthrough=3 case sensitively matches one of the following regular expressions:
-fallthrough
@fallthrough@
lint -fallthrough[ \t]*
[ \t.!]*(ELSE,? |INTENTIONAL(LY)? )?
FALL(S | |-)?THR(OUGH|U)[ \t.!]*(-[^\n\r]*)?
[ \t.!]*(Else,? |Intentional(ly)? )?
Fall((s | |-)[Tt]|t)hr(ough|u)[ \t.!]*(-[^\n\r]*)?
[ \t.!]*([Ee]lse,? |[Ii]ntentional(ly)? )?
fall(s | |-)?thr(ough|u)[ \t.!]*(-[^\n\r]*)?
-Wimplicit-fallthrough=4 case sensitively matches one of the following regular expressions:
-fallthrough
@fallthrough@
lint -fallthrough[ \t]*
[ \t]*FALLTHR(OUGH|U)[ \t]*
-Wimplicit-fallthrough=5 doesn’t recognize any comments as fallthrough comments, only attributes disable the warning.