git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: Jeff King <peff@peff.net>
Cc: "Derrick Stolee" <derrickstolee@github.com>,
	git@vger.kernel.org, "Jonathan Tan" <jonathantanmy@google.com>,
	"René Scharfe" <l.s.r@web.de>
Subject: Re: [PATCH 0/5] cleaning up read_object() family of functions
Date: Thu, 12 Jan 2023 17:22:04 +0100	[thread overview]
Message-ID: <230112.86v8lbzpj1.gmgdl@evledraar.gmail.com> (raw)
In-Reply-To: <Y8AyTE3OS7HCAzKH@coredump.intra.peff.net>


On Thu, Jan 12 2023, Jeff King wrote:

> On Thu, Jan 12, 2023 at 10:21:46AM +0100, Ævar Arnfjörð Bjarmason wrote:
>
>> I agree that it's probably not worth it here, but I think you're just
>> tying yourself in knots in trying to define these macros in terms of
>> each other. This sort of thing will work if you just do:
>> 	
>> 	diff --git a/object-store.h b/object-store.h
>> 	index e894cee61ba..bfcd2482dc5 100644
>> 	--- a/object-store.h
>> 	+++ b/object-store.h
>> 	@@ -418,8 +418,8 @@ struct object_info {
>> 	  * Initializer for a "struct object_info" that wants no items. You may
>> 	  * also memset() the memory to all-zeroes.
>> 	  */
>> 	-#define OBJECT_INFO(...) { 0, __VA_ARGS__ }
>> 	-#define OBJECT_INFO_INIT OBJECT_INFO()
>> 	+#define OBJECT_INFO_INIT { 0 }
>> 	+#define OBJECT_INFO(...) { __VA_ARGS__ }
>
> Right, that works because the initializer is just "0", which the
> compiler can do for us implicitly. I agree it works here to omit, but as
> a general solution, it doesn't.
>
>> Which is just a twist on René's suggestion from [1], i.e.:
>> 
>> 	#define CHILD_PROCESS_INIT_EX(...) { .args = STRVEC_INIT, __VA_ARGS__ }
>>
>> In that case we always need to rely on the "args" being init'd, and the
>> GCC warning you note is a feature, its initialization is "private", and
>> you should never override it.
>
> Right, and it works here because you'd never want to init .args to
> anything else (which I think is what you mean by "private"). But in the
> general case the defaults can't set something that the caller might want
> to override, because the compiler's warning doesn't know the difference
> between "override" and "oops, you specified this twice".
>
> It's mostly a non-issue because we tend to prefer 0-initialization when
> possible, but I think as a general technique this is probably opening a
> can of worms for little benefit.

You're right in the general case, although I think that if we did
encounter such a use-case a perfectly good solution would be to just
suppress the GCC-specific warning with the relevant GCC-specific macro
magic, this being perfectly valid C, just something it (rightly, as it's
almost always a mistake) complains about.

But I can't think of a case where this would matter for us in practice.

We have members like "struct strbuf"'s "buf", which always needs to be
init'd, but never "maybe by the user", so the pattern above would work
there.

Then we have things like "strdup_strings" which we might imagine that
the user would override (with a hypothetical "struct string_list" that
took more arguments, but in those cases we could just add another init
macro, as "STRING_LIST_INIT_{DUP,NODUP}" does.

For any such member we could always just invert its boolean state, if it
came to that, couldn't we?

Anyway, I agree that it's not worth pursuing this in this case.

But I think it's a neat pattern that we might find use for sooner than
later for something else.

I don't think it's worth the churn to change it at this point (except
maybe with a sufficiently clever coccinelle rule), but I think it's
already "worth it" in the case of the run-command API, if we were adding
that code today under current constraints (i.e. being able to use C99
macro features).

  reply	other threads:[~2023-01-12 16:29 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-07 13:48 [PATCH 0/5] cleaning up read_object() family of functions Jeff King
2023-01-07 13:48 ` [PATCH 1/5] object-file: inline calls to read_object() Jeff King
2023-01-12  9:13   ` Ævar Arnfjörð Bjarmason
2023-01-12 16:06     ` [PATCH] object-file: fix indent-with-space Jeff King
2023-01-12 16:08       ` Ævar Arnfjörð Bjarmason
2023-01-13 17:40         ` Junio C Hamano
2023-01-07 13:49 ` [PATCH 2/5] streaming: inline call to read_object_file_extended() Jeff King
2023-01-07 13:50 ` [PATCH 3/5] read_object_file_extended(): drop lookup_replace option Jeff King
2023-01-07 13:50 ` [PATCH 4/5] repo_read_object_file(): stop wrapping read_object_file_extended() Jeff King
2023-01-07 13:50 ` [PATCH 5/5] packfile: inline custom read_object() Jeff King
2023-01-12  9:01   ` Ævar Arnfjörð Bjarmason
2023-01-12 16:29     ` Jeff King
2023-01-09 15:09 ` [PATCH 0/5] cleaning up read_object() family of functions Derrick Stolee
2023-01-11 18:26   ` Jeff King
2023-01-11 20:17     ` Derrick Stolee
2023-01-11 20:30       ` Jeff King
2023-01-12  9:21     ` Ævar Arnfjörð Bjarmason
2023-01-12 16:16       ` Jeff King
2023-01-12 16:22         ` Ævar Arnfjörð Bjarmason [this message]
2023-01-12 16:53           ` Jeff King

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=230112.86v8lbzpj1.gmgdl@evledraar.gmail.com \
    --to=avarab@gmail.com \
    --cc=derrickstolee@github.com \
    --cc=git@vger.kernel.org \
    --cc=jonathantanmy@google.com \
    --cc=l.s.r@web.de \
    --cc=peff@peff.net \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).