diff mbox series

[v2,1/2] configure: Add -Wno-gnu-variable-sized-type-not-at-end

Message ID 20220909035758.17434-2-chenyi.qiang@intel.com
State New
Headers show
Series Update linux headers to v6.0-rc4 and fix the clang build error | expand

Commit Message

Chenyi Qiang Sept. 9, 2022, 3:57 a.m. UTC
In recent linux headers update to v6.0-rc, it switched GNU
'zero-length-array' extension to the C-standard-defined flexible array
member. e.g.

struct kvm_msrs {
        __u32 nmsrs; /* number of msrs in entries */
        __u32 pad;

-       struct kvm_msr_entry entries[0];
+       struct kvm_msr_entry entries[];
};

Those (unlike the GNU zero-length-array) have some extra restrictions like
'this must be put at the end of a struct', which clang build would complain
about. e.g. the current code

struct {
        struct kvm_msrs info;
        struct kvm_msr_entry entries[1];
} msr_data = { }

generates the warning like:

target/i386/kvm/kvm.c:2868:25: error: field 'info' with variable sized
type 'struct kvm_msrs' not at the end of a struct or class is a GNU
extension [-Werror,-Wgnu-variable-sized-type-not-at-end]
        struct kvm_msrs info;
                        ^
In fact, the variable length 'entries[]' field in 'info' is zero-sized in
GNU defined semantics, which can give predictable offset for 'entries[1]'
in local msr_data. The local defined struct is just there to force a stack
allocation large enough for 1 kvm_msr_entry, a clever trick but requires to
turn off this clang warning.

Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Chenyi Qiang <chenyi.qiang@intel.com>
---
 configure | 1 +
 1 file changed, 1 insertion(+)

Comments

Cornelia Huck Sept. 9, 2022, 10:52 a.m. UTC | #1
On Fri, Sep 09 2022, Chenyi Qiang <chenyi.qiang@intel.com> wrote:

> In recent linux headers update to v6.0-rc, it switched GNU

Maybe

"A Linux headers update to v6.0-rc switches some definitions from the
GNU..."

?

> 'zero-length-array' extension to the C-standard-defined flexible array
> member. e.g.
>
> struct kvm_msrs {
>         __u32 nmsrs; /* number of msrs in entries */
>         __u32 pad;
>
> -       struct kvm_msr_entry entries[0];
> +       struct kvm_msr_entry entries[];
> };
>
> Those (unlike the GNU zero-length-array) have some extra restrictions like
> 'this must be put at the end of a struct', which clang build would complain
> about. e.g. the current code
>
> struct {
>         struct kvm_msrs info;
>         struct kvm_msr_entry entries[1];
> } msr_data = { }
>
> generates the warning like:
>
> target/i386/kvm/kvm.c:2868:25: error: field 'info' with variable sized
> type 'struct kvm_msrs' not at the end of a struct or class is a GNU
> extension [-Werror,-Wgnu-variable-sized-type-not-at-end]
>         struct kvm_msrs info;
>                         ^
> In fact, the variable length 'entries[]' field in 'info' is zero-sized in
> GNU defined semantics, which can give predictable offset for 'entries[1]'
> in local msr_data. The local defined struct is just there to force a stack
> allocation large enough for 1 kvm_msr_entry, a clever trick but requires to
> turn off this clang warning.
>
> Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> Signed-off-by: Chenyi Qiang <chenyi.qiang@intel.com>
> ---
>  configure | 1 +
>  1 file changed, 1 insertion(+)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>
diff mbox series

Patch

diff --git a/configure b/configure
index 575dde1c1f..7e0a1a4187 100755
--- a/configure
+++ b/configure
@@ -1258,6 +1258,7 @@  add_to nowarn_flags -Wno-string-plus-int
 add_to nowarn_flags -Wno-typedef-redefinition
 add_to nowarn_flags -Wno-tautological-type-limit-compare
 add_to nowarn_flags -Wno-psabi
+add_to nowarn_flags -Wno-gnu-variable-sized-type-not-at-end
 
 gcc_flags="$warn_flags $nowarn_flags"