From: Stefan Weil <weil@mail.berlios.de>
To: Anthony Liguori <aliguori@us.ibm.com>,
Markus Armbruster <armbru@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense)
Date: Wed, 26 Aug 2009 15:52:32 +0200 [thread overview]
Message-ID: <4A953E20.8080806@mail.berlios.de> (raw)
In-Reply-To: <87tyzxnwvb.fsf@pike.pond.sub.org>
Markus Armbruster schrieb:
> Stefan Weil <Stefan.Weil@weilnetz.de> writes:
>
>> Juan Quintela schrieb:
>>> Signed-off-by: Juan Quintela <quintela@redhat.com>
>>> ---
>>> hw/eepro100.c | 6 +++---
>>> 1 files changed, 3 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/hw/eepro100.c b/hw/eepro100.c
>>> index 0031d36..09083c2 100644
>>> --- a/hw/eepro100.c
>>> +++ b/hw/eepro100.c
>>> @@ -507,7 +507,7 @@ static void nic_selective_reset(EEPRO100State * s)
>>>
>>> static void nic_reset(void *opaque)
>>> {
>>> - EEPRO100State *s = (EEPRO100State *) opaque;
>>> + EEPRO100State *s = opaque;
>>> logout("%p\n", s);
>>> static int first;
>>> if (!first) {
>>> @@ -1544,7 +1544,7 @@ static ssize_t nic_receive(VLANClientState
>>> *vc, const uint8_t * buf, size_t size
>>>
>>> static int nic_load(QEMUFile * f, void *opaque, int version_id)
>>> {
>>> - EEPRO100State *s = (EEPRO100State *) opaque;
>>> + EEPRO100State *s = opaque;
>>> int i;
>>> int ret;
>>>
>>> @@ -1634,7 +1634,7 @@ static int nic_load(QEMUFile * f, void
>>> *opaque, int version_id)
>>>
>>> static void nic_save(QEMUFile * f, void *opaque)
>>> {
>>> - EEPRO100State *s = (EEPRO100State *) opaque;
>>> + EEPRO100State *s = opaque;
>>> int i;
>>>
>>> if (s->pci_dev)
>>>
>> I wrote these type casts, and I think they make sense.
>> In C++ code, they are even mandatory.
>
> Yes, but this isn't C++.
>
>> I think the arguments why C++ requires this kind of
>> type casts apply to C code (like in Qemu) as well.
>>
>> If it is possible with no or very litte efforts to write
>> code which is C and C++ compatible, I prefer to do so.
>
> I respectfully disagree. Casts from "void *" to "T *" are pure noise.
> Getting into the habit of writing noise casts runs the risk of silencing
> warnings that flag real type errors.
Hello
Do you only disagree with my first sentence or with both sentences?
Currently, I seem to be alone with my opinion (at least in qemu-devel).
Nevertheless, the designers of C++ thought that casts from void * to
T * were something very important. I don't know the history of their
decision. I personally think that deriving a data type T from some
bytes in memory which can contain anything is an operation which is
worth being documented by the programmer, and this is exactly what
the cast does.
My main reason why I try to write portable code is my personal
experience with code reuse and the future development of compilers.
It is quite possible that some day C compilers will require
type casts like C++ compilers do today.
And even today I want to be able to share C code from QEMU with
program code written in C++ (which makes a large part of my
business work).
Anthony, there is already a mixture of coding styles in QEMU
(also regarding type casts). This is not surprising for an
open source project with many contributors. Do we really
need additional regulations? I think the existing ones
(those in CODING_STYLE) are very good, and they are sufficient.
I'd appreciate it very much if all code in QEMU would
respect this documented coding style. Today, it does not,
and there was an agreement that we do not write commits which
only change the coding style (at least for white space).
I suggest to stick to this agreement for non white space
coding style, too.
Let me give one more C/C++ example. Today, many data structures
are declared like this: typedef struct T { ... } T;
There is nothing wrong with it, but it can be improved in
several ways:
* The declaration does not work with C++ (yes, I know that many
programmers are not interested in C++ compatibility for QEMU).
* The declaration allows variable declarations using struct T var;
or just T var; (which is the QEMU style). I think a declaration
which does not enforce the correct style is less good.
* The declaration contains noise (bad argument, but many people
like speaking of noise) :-)
Why don't we declare structures like this: typedef struct { ... } T;?
I suggest this to be the new coding style for structure declarations
because it is shorter, C++ compatible and unambiguous.
Kind regards
Stefan
next prev parent reply other threads:[~2009-08-26 13:53 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-24 11:03 [Qemu-devel] [PATCH 00/22] Indirection Cleanup Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 01/22] eepro100: convert casts to DO_UPCAST() Juan Quintela
2009-08-24 12:59 ` Stefan Weil
2009-08-24 12:59 ` [Qemu-devel] " Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense Juan Quintela
2009-08-24 12:56 ` Stefan Weil
2009-08-24 13:04 ` [Qemu-devel] " Juan Quintela
2009-08-24 13:51 ` Anthony Liguori
2009-08-24 13:51 ` [Qemu-devel] " Markus Armbruster
2009-08-26 13:52 ` Stefan Weil [this message]
2009-08-26 14:49 ` [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense) Gerd Hoffmann
2009-08-26 17:04 ` Jamie Lokier
2009-08-26 18:37 ` malc
2009-08-26 19:03 ` Jamie Lokier
2009-08-26 19:26 ` malc
2009-08-26 21:00 ` Jamie Lokier
2009-08-26 15:01 ` [Qemu-devel] Coding style, C++ compatible code Markus Armbruster
2009-08-26 15:19 ` Anthony Liguori
2009-08-26 15:20 ` Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense) Måns Rullgård
2009-08-26 15:58 ` Reimar Döffinger
2009-08-26 16:08 ` Avi Kivity
2009-09-03 12:05 ` Stuart Brady
2009-08-26 15:54 ` [Qemu-devel] " Avi Kivity
2009-08-26 17:36 ` Jamie Lokier
2009-08-24 14:05 ` [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense Gerd Hoffmann
2009-08-24 11:03 ` [Qemu-devel] [PATCH 03/22] eepro100: Remove unused indirection of PCIDevice Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 04/22] es1370: Remove unused indirection of PCIES1370State and ES1370State Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 05/22] ne2000: remove casts from void * Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 06/22] ne2000: pci_dev has this very value with the right type Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 07/22] ne2000: Remove unneeded double indirection of PCINE2000State Juan Quintela
2009-08-24 12:40 ` Gerd Hoffmann
2009-08-24 12:51 ` [Qemu-devel] " Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 08/22] ne2000: change pci_dev to is_pci Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 09/22] pci: remove casts from void * Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 10/22] rtl8139: Remove unneeded double indirection of PCIRTL8139State Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 11/22] rtl8139: remove pointless cast from void * Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 12/22] lsi53c895a: " Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 13/22] lsi53c895a: use DO_UPCAST to cast from PCIDevice Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 14/22] lsi53c895a: rename PCIDevice field from pci_dev to dev (consistence) Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 15/22] lsi53c895a: LSIState is a PCIDevice is a DeviceHost Juan Quintela
2009-08-24 12:44 ` Gerd Hoffmann
2009-08-24 11:03 ` [Qemu-devel] [PATCH 16/22] usb-ohci: Remove unneeded double indirection of OHCIPCIState Juan Quintela
2009-08-24 12:46 ` Gerd Hoffmann
2009-08-24 11:03 ` [Qemu-devel] [PATCH 17/22] cirrus_vga: Remove unneeded double indirection of PCICirrusVGAState Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 18/22] cirrus_vga: remove pointless cast from void * Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 19/22] cirrus_vga: change use of pci_dev for is_pci Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 20/22] Introduce vga_common_reset() to be able to typcheck vga_reset() Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 21/22] vga: Rename vga_state -> vga Juan Quintela
2009-08-24 11:03 ` [Qemu-devel] [PATCH 22/22] Everything outside of vga.c should use VGACommonState Juan Quintela
2009-08-24 12:37 ` [Qemu-devel] [PATCH 00/22] Indirection Cleanup Gerd Hoffmann
2009-08-24 12:56 ` [Qemu-devel] " Juan Quintela
-- strict thread matches above, loose matches on Subject: below --
2009-08-26 16:58 [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense) Kent Harris
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4A953E20.8080806@mail.berlios.de \
--to=weil@mail.berlios.de \
--cc=aliguori@us.ibm.com \
--cc=armbru@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).