qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tcg/optimize: Add fallthrough annotations
@ 2020-10-29 12:28 Thomas Huth
  2020-10-29 12:37 ` Philippe Mathieu-Daudé
  2020-10-29 20:07 ` Richard Henderson
  0 siblings, 2 replies; 4+ messages in thread
From: Thomas Huth @ 2020-10-29 12:28 UTC (permalink / raw)
  To: Richard Henderson, qemu-devel; +Cc: Chen Qun

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.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 tcg/optimize.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tcg/optimize.c b/tcg/optimize.c
index 220f4601d5..c2768c9770 100644
--- a/tcg/optimize.c
+++ b/tcg/optimize.c
@@ -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;
-- 
2.18.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] tcg/optimize: Add fallthrough annotations
  2020-10-29 12:28 [PATCH] tcg/optimize: Add fallthrough annotations Thomas Huth
@ 2020-10-29 12:37 ` Philippe Mathieu-Daudé
  2020-10-29 20:07 ` Richard Henderson
  1 sibling, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-29 12:37 UTC (permalink / raw)
  To: Thomas Huth, Richard Henderson, qemu-devel; +Cc: Chen Qun

On 10/29/20 1:28 PM, Thomas Huth wrote:
> 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.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  tcg/optimize.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/tcg/optimize.c b/tcg/optimize.c
> index 220f4601d5..c2768c9770 100644
> --- a/tcg/optimize.c
> +++ b/tcg/optimize.c
> @@ -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"?

> +
>  #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;
> 



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tcg/optimize: Add fallthrough annotations
  2020-10-29 12:28 [PATCH] tcg/optimize: Add fallthrough annotations Thomas Huth
  2020-10-29 12:37 ` Philippe Mathieu-Daudé
@ 2020-10-29 20:07 ` Richard Henderson
  2020-10-30  2:43   ` Chenqun (kuhn)
  1 sibling, 1 reply; 4+ messages in thread
From: Richard Henderson @ 2020-10-29 20:07 UTC (permalink / raw)
  To: Thomas Huth, Richard Henderson, qemu-devel; +Cc: Chen Qun

On 10/29/20 5:28 AM, Thomas Huth wrote:
> 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~


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH] tcg/optimize: Add fallthrough annotations
  2020-10-29 20:07 ` Richard Henderson
@ 2020-10-30  2:43   ` Chenqun (kuhn)
  0 siblings, 0 replies; 4+ messages in thread
From: Chenqun (kuhn) @ 2020-10-30  2:43 UTC (permalink / raw)
  To: Richard Henderson, Thomas Huth, Richard Henderson,
	qemu-devel@nongnu.org, Philippe Mathieu-Daudé

> -----Original Message-----
> From: Richard Henderson [mailto:richard.henderson@linaro.org]
> Sent: Friday, October 30, 2020 4:07 AM
> To: Thomas Huth <thuth@redhat.com>; Richard Henderson <rth@twiddle.net>;
> qemu-devel@nongnu.org
> Cc: Chenqun (kuhn) <kuhn.chenqun@huawei.com>
> Subject: Re: [PATCH] tcg/optimize: Add fallthrough annotations
> 
> On 10/29/20 5:28 AM, Thomas Huth wrote:
> > 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.
> 
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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-10-30  2:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-29 12:28 [PATCH] tcg/optimize: Add fallthrough annotations Thomas Huth
2020-10-29 12:37 ` Philippe Mathieu-Daudé
2020-10-29 20:07 ` Richard Henderson
2020-10-30  2:43   ` Chenqun (kuhn)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).