Skip to contentSkip to conversation

scsi: Address spurious clang warning

PATCH In review John Snow 3 participants 5 messages Nov 27, 2018 – Nov 28, 2018

lore
Share & tools
lore

John Snow

author
#0 lore raw

PATCH 1/1 scsi: Address spurious clang warning

Some versions of Clang prior to 6.0 (and some builds of clang after, such as 6.0.1-2.fc28) fail to recognize { 0 } as a valid initializer for a struct with subobjects when -Wmissing-braces is enabled.

https://bugs.llvm.org/show_bug.cgi?id=21689 and
https://reviews.llvm.org/rL314499 suggests this should be fixed in 6.0, but it might not be the case for older versions or downstream versions.

For now, follow the precedent of ebf2a499 and replace the standard { 0 } with the accepted { } to silence this warning and allow the build to work under clang 6.0.1-2.fc28, and builds prior to 6.0.

---

What I am actually less clear on is why this appears to be a problem only now; since the introduction of { 0 } was in 2.11. It might be a regression only in the fedora distribution of Clang 6.0.

With apologies to Paolo, who hates these patches.

---
 scsi/qemu-pr-helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scsi/qemu-pr-helper.c +1 -1
index ce40008bfc..e7af637232 100644
@@ -236,7 +236,7 @@ static void dm_init(void)
         perror("Cannot open " CONTROL_PATH);
         exit(1);
     }
-    struct dm_ioctl dm = { 0 };
+    struct dm_ioctl dm = { };
     if (!dm_ioctl(DM_VERSION, &dm)) {
         perror("ioctl");
         exit(1);
Signature · 1 line
2.17.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · John Snow

Some versions of Clang prior to 6.0 (and some builds of clang after, such as 6.0.1-2.fc28) fail to recognize { 0 } as a valid initializer for a struct with subobjects when -Wmissing-braces is enabled.

https://bugs.llvm.org/show_bug.cgi?id=21689 and
https://reviews.llvm.org/rL314499 suggests this should be fixed in 6.0, but it might not be the case for older versions or downstream versions.

Quoted 6 lines

For now, follow the precedent of ebf2a499 and replace the standard { 0 } with the accepted { } to silence this warning and allow the build to work under clang 6.0.1-2.fc28, and builds prior to 6.0.

I'm okay if this goes into -rc3 as a build-fix; I'm also okay if it slips to 4.0.

Quoted 5 lines

---

What I am actually less clear on is why this appears to be a problem only now; since the introduction of { 0 } was in 2.11. It might be a regression only in the fedora distribution of Clang 6.0.

Or even a redefinition of struct dm_ioctl in some header where you are just now picking up a new struct layout that tickles the Clang issue in relation to the previous layout (since it is possible to have two structs that are ABI-compatible but where only one of the two has a nested substruct).

scsi/qemu-pr-helper.c static void dm_init(void) · line 239

Quoted 7 lines

+++ b/scsi/qemu-pr-helper.c

scsi/qemu-pr-helper.c +1 -1
@@ -236,7 +236,7 @@ static void dm_init(void)
          perror("Cannot open " CONTROL_PATH);
          exit(1);
      }
-    struct dm_ioctl dm = { 0 };
+    struct dm_ioctl dm = { };

Random thought: would it be worth having "qemu/compiler.h" define a macro:

#if ...broken clang
#define ZERO_INIT {}
#else
#define ZERO_INIT {0}
#endif

and then rewrite all our '= { 0? }' initializers into '= ZERO_INIT'? Or is that aesthetically too ugly?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 14 lines · Eric Blake
Quoted 12 lines · John Snow

Some versions of Clang prior to 6.0 (and some builds of clang after, such as 6.0.1-2.fc28) fail to recognize { 0 } as a valid initializer for a struct with subobjects when -Wmissing-braces is enabled.

https://bugs.llvm.org/show_bug.cgi?id=21689 and
https://reviews.llvm.org/rL314499 suggests this should be fixed in 6.0, but it might not be the case for older versions or downstream versions.

For now, follow the precedent of ebf2a499 and replace the standard { 0 } with the accepted { } to silence this warning and allow the build to work under clang 6.0.1-2.fc28, and builds prior to 6.0.

Quoted 13 lines
Quoted 2 lines

I'm okay if this goes into -rc3 as a build-fix; I'm also okay if it slips to 4.0.

Quoted 5 lines

---

What I am actually less clear on is why this appears to be a problem only now; since the introduction of { 0 } was in 2.11. It might be a regression only in the fedora distribution of Clang 6.0.

Upstream clang 6.0 should have this bug fixed (ie it does not warn about this construct), so it is odd that Fedora's does not.

Quoted 16 lines

Or even a redefinition of struct dm_ioctl in some header where you are just now picking up a new struct layout that tickles the Clang issue in relation to the previous layout (since it is possible to have two structs that are ABI-compatible but where only one of the two has a nested substruct).

scsi/qemu-pr-helper.c static void dm_init(void) · line 239

Quoted 7 lines

+++ b/scsi/qemu-pr-helper.c

scsi/qemu-pr-helper.c +1 -1
@@ -236,7 +236,7 @@ static void dm_init(void)
          perror("Cannot open " CONTROL_PATH);
          exit(1);
      }
-    struct dm_ioctl dm = { 0 };
+    struct dm_ioctl dm = { };

Random thought: would it be worth having "qemu/compiler.h" define a macro:

Quoted 8 lines

#if ...broken clang
#define ZERO_INIT {}
#else
#define ZERO_INIT {0}
#endif

and then rewrite all our '= { 0? }' initializers into '= ZERO_INIT'? Or is that aesthetically too ugly?

I think that's worse than just writing {} everywhere (which does work on every compiler we need to support, including older gcc which also used to warn here).

thanks
-- PMM

author
#3 lore raw
In reply to Eric Blake
Quoted 43 lines · Eric Blake
Quoted 8 lines · John Snow

Some versions of Clang prior to 6.0 (and some builds of clang after, such as 6.0.1-2.fc28) fail to recognize { 0 } as a valid initializer for a struct with subobjects when -Wmissing-braces is enabled.

https://bugs.llvm.org/show_bug.cgi?id=21689 and
https://reviews.llvm.org/rL314499 suggests this should be fixed in 6.0, but it might not be the case for older versions or downstream versions.

Quoted 6 lines

For now, follow the precedent of ebf2a499 and replace the standard { 0 } with the accepted { } to silence this warning and allow the build to work under clang 6.0.1-2.fc28, and builds prior to 6.0.

I'm okay if this goes into -rc3 as a build-fix; I'm also okay if it slips to 4.0.

Quoted 5 lines

---

What I am actually less clear on is why this appears to be a problem only now; since the introduction of { 0 } was in 2.11. It might be a regression only in the fedora distribution of Clang 6.0.

Or even a redefinition of struct dm_ioctl in some header where you are just now picking up a new struct layout that tickles the Clang issue in relation to the previous layout (since it is possible to have two structs that are ABI-compatible but where only one of the two has a nested substruct).

scsi/qemu-pr-helper.c static void dm_init(void) · line 239

Quoted 7 lines

+++ b/scsi/qemu-pr-helper.c

scsi/qemu-pr-helper.c +1 -1
@@ -236,7 +236,7 @@ static void dm_init(void)
           perror("Cannot open " CONTROL_PATH);
           exit(1);
       }
-    struct dm_ioctl dm = { 0 };
+    struct dm_ioctl dm = { };

Random thought: would it be worth having "qemu/compiler.h" define a macro:

Quoted 9 lines

#if ...broken clang
#define ZERO_INIT {}
#else
#define ZERO_INIT {0}
#endif

and then rewrite all our '= { 0? }' initializers into '= ZERO_INIT'?  Or is that aesthetically too ugly?

Obscures perfectly legitimate C code without solving anything, IMO. As much code as can reflect "naked" C89/C99/GNU99, the better.

--js

#4 lore raw
In reply to John Snow
Quoted 9 lines · John Snow

Some versions of Clang prior to 6.0 (and some builds of clang after, such as 6.0.1-2.fc28) fail to recognize { 0 } as a valid initializer for a struct with subobjects when -Wmissing-braces is enabled.

https://bugs.llvm.org/show_bug.cgi?id=21689 and
https://reviews.llvm.org/rL314499 suggests this should be fixed in 6.0, but it might not be the case for older versions or downstream versions.

Quoted 5 lines

For now, follow the precedent of ebf2a499 and replace the standard { 0 } with the accepted { } to silence this warning and allow the build to work under clang 6.0.1-2.fc28, and builds prior to 6.0.

Applied to master as a build fix for rc3, thanks.

-- PMM