From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40054) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YDb07-0004gY-Lg for qemu-devel@nongnu.org; Tue, 20 Jan 2015 10:45:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YDb06-0007Qw-NW for qemu-devel@nongnu.org; Tue, 20 Jan 2015 10:45:55 -0500 Received: from mnementh.archaic.org.uk ([2001:8b0:1d0::1]:54959) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YDb06-0007MQ-Gu for qemu-devel@nongnu.org; Tue, 20 Jan 2015 10:45:54 -0500 Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.80) (envelope-from ) id 1YDazz-0004Ub-CV for qemu-devel@nongnu.org; Tue, 20 Jan 2015 15:45:47 +0000 From: Peter Maydell Date: Tue, 20 Jan 2015 15:45:30 +0000 Message-Id: <1421768747-17240-2-git-send-email-peter.maydell@linaro.org> In-Reply-To: <1421768747-17240-1-git-send-email-peter.maydell@linaro.org> References: <1421768747-17240-1-git-send-email-peter.maydell@linaro.org> Subject: [Qemu-devel] [PULL 01/18] scripts/qapi-types.py: Add dummy member to empty structs List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Make sure that all generated C structs have at least one field; this avoids potential issues with attempting to malloc space for zero-length structs in C (g_malloc(sizeof struct) would return NULL). It also avoids an incompatibility with C++ (where an empty struct is size 1); that isn't important to us now but might be in future. Generated empty structures look like this: struct Abort { char qapi_dummy_field_for_empty_struct; }; This silences clang warnings like: ./qapi-types.h:3752:1: warning: empty struct has size 0 in C, size 1 in C++ [-Wextern-c-compat] struct Abort ^ Signed-off-by: Peter Maydell Reviewed-by: Eric Blake Message-id: 1419359069-16611-1-git-send-email-peter.maydell@linaro.org --- scripts/qapi-types.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py index d2f815b..1eb272d 100644 --- a/scripts/qapi-types.py +++ b/scripts/qapi-types.py @@ -99,6 +99,14 @@ struct %(name)s ret += generate_struct_fields(members) + # Make sure that all structs have at least one field; this avoids + # potential issues with attempting to malloc space for zero-length structs + # in C, and also incompatibility with C++ (where an empty struct is size 1). + if not base and not members: + ret += mcgen(''' + char qapi_dummy_field_for_empty_struct; +''') + if len(fieldname): fieldname = " " + fieldname ret += mcgen(''' -- 1.9.1