From: Markus Armbruster <armbru@redhat.com>
To: "Philippe Mathieu-Daudé" <philmd@linaro.org>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
qemu-devel@nongnu.org, "Hanna Czenczek" <hreitz@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Thomas Huth" <thuth@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Vladimir Sementsov-Ogievskiy" <vsementsov@yandex-team.ru>
Subject: Re: [PATCH] docs/style: allow C99 mixed declarations
Date: Wed, 07 Feb 2024 08:37:41 +0100 [thread overview]
Message-ID: <87msscn2re.fsf@pond.sub.org> (raw)
In-Reply-To: <683557e9-fa84-4384-961f-7c29daafe860@linaro.org> ("Philippe Mathieu-Daudé"'s message of "Wed, 7 Feb 2024 06:28:01 +0100")
Philippe Mathieu-Daudé <philmd@linaro.org> writes:
> On 6/2/24 06:53, Markus Armbruster wrote:
>> Daniel P. Berrangé <berrange@redhat.com> writes:
>>
>>> On Mon, Feb 05, 2024 at 12:18:19PM -0500, Stefan Hajnoczi wrote:
>>>> C99 mixed declarations support interleaving of local variable
>>>> declarations and code.
>>>>
>>>> The coding style "generally" forbids C99 mixed declarations with some
>>>> exceptions to the rule. This rule is not checked by checkpatch.pl and
>>>> naturally there are violations in the source tree.
>>>>
>>>> While contemplating adding another exception, I came to the conclusion
>>>> that the best location for declarations depends on context. Let the
>>>> programmer declare variables where it is best for legibility. Don't try
>>>> to define all possible scenarios/exceptions.
> ...
>
>>> Even if the compiler does reliably warn, I think the code pattern
>>> remains misleading to contributors, as the flow control flaw is
>>> very non-obvious.
>>
>> Yup. Strong dislike.
>>
>>> Rather than accept the status quo and remove the coding guideline,
>>> I think we should strengthen the guidelines, such that it is
>>> explicitly forbidden in any method that uses 'goto'. Personally
>>> I'd go all the way to -Werror=declaration-after-statement, as
>>
>> I support this.
>>
>>> while C99 mixed decl is appealing,
>>
>> Not to me.
>>
>> I much prefer declarations and statements to be visually distinct.
>> Putting declarations first and separating from statements them with a
>> blank line accomplishes that. Less necessary in languages where
>> declarations are syntactically obvious.
>
> But we already implicitly suggest C99, see commit ae7c80a7bd
> ("error: New macro ERRP_GUARD()"):
>
> * To use ERRP_GUARD(), add it right at the beginning of the function.
> * @errp can then be used without worrying about the argument being
> * NULL or &error_fatal.
>
> #define ERRP_GUARD() \
> g_auto(ErrorPropagator) _auto_errp_prop = {.errp = errp}; \
> do { \
> if (!errp || errp == &error_fatal) { \
> errp = &_auto_errp_prop.local_err; \
> } \
> } while (0)
We can make ERRP_GUARD() expand into just a declaration:
#define ERRP_GUARD() \
g_auto(ErrorPropagator) _auto_errp_prop = { \
.errp = errp, \
.local_err = ((!errp || errp == &error_fatal \
? errp = &_auto_errp_prop.local_err \
: NULL), \
NULL) }
> Or commit 5626f8c6d4 ("rcu: Add automatically released rcu_read_lock
> variants") with WITH_RCU_READ*:
>
> util/aio-posix.c:540:5: error: mixing declarations and code is
> incompatible with standards before C99
> [-Werror,-Wdeclaration-after-statement]
> RCU_READ_LOCK_GUARD();
> ^
> include/qemu/rcu.h:189:28: note: expanded from macro 'RCU_READ_LOCK_GUARD'
> g_autoptr(RCUReadAuto) _rcu_read_auto __attribute__((unused)) =
> rcu_read_auto_lock()
> ^
Valid example; RCU_READ_LOCK_GUARD() expands into a declaration.
To enable -Wdeclaration-after-statement, we'd have to futz around with
_Pragma.
next prev parent reply other threads:[~2024-02-07 7:38 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-05 17:18 [PATCH] docs/style: allow C99 mixed declarations Stefan Hajnoczi
2024-02-05 17:41 ` Daniel P. Berrangé
2024-02-05 17:55 ` Peter Maydell
2024-02-05 18:12 ` Samuel Tardieu
2024-02-05 19:06 ` Stefan Hajnoczi
2024-02-05 19:17 ` Daniel P. Berrangé
2024-02-06 5:53 ` Markus Armbruster
2024-02-07 5:28 ` Philippe Mathieu-Daudé
2024-02-07 7:37 ` Markus Armbruster [this message]
2024-02-05 23:17 ` Alex Bennée
2024-02-06 13:48 ` Stefan Hajnoczi
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=87msscn2re.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=berrange@redhat.com \
--cc=hreitz@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=stefanha@redhat.com \
--cc=thuth@redhat.com \
--cc=vsementsov@yandex-team.ru \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.