[v2,3/7] chardev: Use goto/label instead of do/break/while(0)

Message ID 20171201232433.25193-4-eblake@redhat.com
State New
Headers show
Series
  • macro do/while (0) cleanup
Related show

Commit Message

Eric Blake Dec. 1, 2017, 11:24 p.m.
Use of a do/while(0) control flow in order to permit an early break
is an unusual paradigm, and triggers a false positive with a planned
future syntax check against 'while (0);'.  Rewrite the code to use a
goto instead.  This patch temporarily keeps an extra level of
indentation to highlight the change; the next patch cleans it up.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 chardev/char-serial.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

Comments

Marc-André Lureau Dec. 3, 2017, 2:20 p.m. | #1
On Sat, Dec 2, 2017 at 12:24 AM, Eric Blake <eblake@redhat.com> wrote:
> Use of a do/while(0) control flow in order to permit an early break
> is an unusual paradigm, and triggers a false positive with a planned
> future syntax check against 'while (0);'.  Rewrite the code to use a
> goto instead.  This patch temporarily keeps an extra level of
> indentation to highlight the change; the next patch cleans it up.
>
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>


>  chardev/char-serial.c | 13 ++++++++++---
>  1 file changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/chardev/char-serial.c b/chardev/char-serial.c
> index 2f8f83821d..10162f9fa3 100644
> --- a/chardev/char-serial.c
> +++ b/chardev/char-serial.c
> @@ -64,9 +64,14 @@ static void tty_serial_init(int fd, int speed,
>  #endif
>      tcgetattr(fd, &tty);
>
> -#define check_speed(val) if (speed <= val) { spd = B##val; break; }
> +#define check_speed(val) \
> +    if (speed <= val) {  \
> +        spd = B##val;    \
> +        goto done;       \
> +    }
> +
>      speed = speed * 10 / 11;
> -    do {
> +    {
>          check_speed(50);
>          check_speed(75);
>          check_speed(110);
> @@ -125,8 +130,10 @@ static void tty_serial_init(int fd, int speed,
>          check_speed(4000000);
>  #endif
>          spd = B115200;
> -    } while (0);
> +    }
>
> +#undef check_speed
> + done:
>      cfsetispeed(&tty, spd);
>      cfsetospeed(&tty, spd);
>
> --
> 2.14.3
>
>

Patch

diff --git a/chardev/char-serial.c b/chardev/char-serial.c
index 2f8f83821d..10162f9fa3 100644
--- a/chardev/char-serial.c
+++ b/chardev/char-serial.c
@@ -64,9 +64,14 @@  static void tty_serial_init(int fd, int speed,
 #endif
     tcgetattr(fd, &tty);

-#define check_speed(val) if (speed <= val) { spd = B##val; break; }
+#define check_speed(val) \
+    if (speed <= val) {  \
+        spd = B##val;    \
+        goto done;       \
+    }
+
     speed = speed * 10 / 11;
-    do {
+    {
         check_speed(50);
         check_speed(75);
         check_speed(110);
@@ -125,8 +130,10 @@  static void tty_serial_init(int fd, int speed,
         check_speed(4000000);
 #endif
         spd = B115200;
-    } while (0);
+    }

+#undef check_speed
+ done:
     cfsetispeed(&tty, spd);
     cfsetospeed(&tty, spd);