[PATCH] maint: fix "mixing declarations and code" errors

Roman Bogorodskiy posted 1 patch 3 weeks, 4 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20221108193531.15914-1-bogorodskiy@gmail.com
src/bhyve/bhyve_device.c    |  6 ++++--
tests/bhyvexml2argvmock.c   |  4 ++--
tests/domaincapstest.c      |  3 ++-
tests/networkxml2conftest.c | 16 +++++++++-------
4 files changed, 17 insertions(+), 12 deletions(-)
[PATCH] maint: fix "mixing declarations and code" errors
Posted by Roman Bogorodskiy 3 weeks, 4 days ago
clang 14.0.5 complains:

../src/bhyve/bhyve_device.c:42:29: error: mixing declarations and code
is incompatible with standards before C99
[-Werror,-Wdeclaration-after-statement]
    virDomainPCIAddressSet *addrs = opaque;
                            ^
1 error generated.

And a few similar errors in some other places, mainly bhyve related.
Apply a trivial fix to resolve that.

Signed-off-by: Roman Bogorodskiy <bogorodskiy@gmail.com>
---
 src/bhyve/bhyve_device.c    |  6 ++++--
 tests/bhyvexml2argvmock.c   |  4 ++--
 tests/domaincapstest.c      |  3 ++-
 tests/networkxml2conftest.c | 16 +++++++++-------
 4 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/src/bhyve/bhyve_device.c b/src/bhyve/bhyve_device.c
index 5654028ca5..e4d14c4102 100644
--- a/src/bhyve/bhyve_device.c
+++ b/src/bhyve/bhyve_device.c
@@ -36,11 +36,13 @@ bhyveCollectPCIAddress(virDomainDef *def G_GNUC_UNUSED,
                        virDomainDeviceInfo *info,
                        void *opaque)
 {
+    virDomainPCIAddressSet *addrs = NULL;
+    virPCIDeviceAddress *addr = NULL;
     if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE)
         return 0;
 
-    virDomainPCIAddressSet *addrs = opaque;
-    virPCIDeviceAddress *addr = &info->addr.pci;
+    addrs = opaque;
+    addr = &info->addr.pci;
 
     if (addr->domain == 0 && addr->bus == 0 && addr->slot == 0) {
             return 0;
diff --git a/tests/bhyvexml2argvmock.c b/tests/bhyvexml2argvmock.c
index 9b77f97e5f..fe76564d51 100644
--- a/tests/bhyvexml2argvmock.c
+++ b/tests/bhyvexml2argvmock.c
@@ -25,10 +25,10 @@ init_syms(void)
 DIR *
 opendir(const char *path)
 {
-    init_syms();
-
     g_autofree char *path_override = NULL;
 
+    init_syms();
+
     if (STREQ(path, "fakefirmwaredir")) {
         path_override = g_strdup(FAKEFIRMWAREDIR);
     } else if (STREQ(path, "fakefirmwareemptydir")) {
diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c
index b4cb1894c2..b3cf4426f3 100644
--- a/tests/domaincapstest.c
+++ b/tests/domaincapstest.c
@@ -397,8 +397,9 @@ mymain(void)
 #define DO_TEST_BHYVE(Name, Emulator, BhyveCaps, Type) \
     do { \
         g_autofree char *name = NULL; \
+        struct testData data; \
         name = g_strdup_printf("bhyve_%s.x86_64", Name); \
-        struct testData data = { \
+        data = (struct testData) { \
             .name = name, \
             .emulator = Emulator, \
             .arch = "x86_64", \
diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c
index 726f073ddc..d18985e060 100644
--- a/tests/networkxml2conftest.c
+++ b/tests/networkxml2conftest.c
@@ -50,14 +50,16 @@ testCompareXMLToConfFiles(const char *inxml, const char *outconf,
 
     /* Any changes to this function ^^ should be reflected here too. */
 #ifndef __linux__
-    char * tmp;
+    {
+        char * tmp;
 
-    if (!(tmp = virStringReplace(confactual,
-                                 "except-interface=lo0\n",
-                                 "except-interface=lo\n")))
-        goto fail;
-    VIR_FREE(confactual);
-    confactual = g_steal_pointer(&tmp);
+        if (!(tmp = virStringReplace(confactual,
+                                     "except-interface=lo0\n",
+                                     "except-interface=lo\n")))
+            goto fail;
+        VIR_FREE(confactual);
+        confactual = g_steal_pointer(&tmp);
+    }
 #endif
 
     if (virTestCompareToFile(confactual, outconf) < 0)
-- 
2.38.0
Re: [PATCH] maint: fix "mixing declarations and code" errors
Posted by Daniel P. Berrangé 3 weeks, 4 days ago
On Tue, Nov 08, 2022 at 08:35:31PM +0100, Roman Bogorodskiy wrote:
> clang 14.0.5 complains:
> 
> ../src/bhyve/bhyve_device.c:42:29: error: mixing declarations and code
> is incompatible with standards before C99

Err, we set std=gnu99  so it shouldn't be complaining about that.

> [-Werror,-Wdeclaration-after-statement]
>     virDomainPCIAddressSet *addrs = opaque;
>                             ^
> 1 error generated.
> 
> And a few similar errors in some other places, mainly bhyve related.
> Apply a trivial fix to resolve that.


None the less I don't like the mixing of decls/code, as it has very
confusing/misleading semantics when combined with goto's that jump
over a decl

> 
> Signed-off-by: Roman Bogorodskiy <bogorodskiy@gmail.com>

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Re: [PATCH] maint: fix "mixing declarations and code" errors
Posted by Michal Prívozník 3 weeks, 4 days ago
On 11/9/22 09:15, Daniel P. Berrangé wrote:
> On Tue, Nov 08, 2022 at 08:35:31PM +0100, Roman Bogorodskiy wrote:
>> clang 14.0.5 complains:
>>
>> ../src/bhyve/bhyve_device.c:42:29: error: mixing declarations and code
>> is incompatible with standards before C99
> 
> Err, we set std=gnu99  so it shouldn't be complaining about that.

But we also enable -Wdeclaration-after-statement which checks for the
same thing. I wonder how could gcc not identify these.

> 
>> [-Werror,-Wdeclaration-after-statement]
>>     virDomainPCIAddressSet *addrs = opaque;
>>                             ^
>> 1 error generated.
>>
>> And a few similar errors in some other places, mainly bhyve related.
>> Apply a trivial fix to resolve that.
> 
> 
> None the less I don't like the mixing of decls/code, as it has very
> confusing/misleading semantics when combined with goto's that jump
> over a decl


Yep.

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Michal

Re: [PATCH] maint: fix "mixing declarations and code" errors
Posted by Daniel P. Berrangé 3 weeks, 4 days ago
On Wed, Nov 09, 2022 at 10:26:57AM +0100, Michal Prívozník wrote:
> On 11/9/22 09:15, Daniel P. Berrangé wrote:
> > On Tue, Nov 08, 2022 at 08:35:31PM +0100, Roman Bogorodskiy wrote:
> >> clang 14.0.5 complains:
> >>
> >> ../src/bhyve/bhyve_device.c:42:29: error: mixing declarations and code
> >> is incompatible with standards before C99
> > 
> > Err, we set std=gnu99  so it shouldn't be complaining about that.
> 
> But we also enable -Wdeclaration-after-statement which checks for the
> same thing. I wonder how could gcc not identify these.

We don't build bhyve driver on Linux, and the FreeBSD build uses
clang IIRC.


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|