git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] strbuf: add compound literal test balloon
@ 2025-07-14 13:27 Phillip Wood
  2025-07-14 14:26 ` Junio C Hamano
  0 siblings, 1 reply; 15+ messages in thread
From: Phillip Wood @ 2025-07-14 13:27 UTC (permalink / raw)
  To: git; +Cc: Phillip Wood

From: Phillip Wood <phillip.wood@dunelm.org.uk>

A C99 compound literal creates an unnamed object whose value is given by
an initializer list. This allows us to simplify code where we cannot use
a designated initalizer because the values of some members of the object
need to be calculated first. For example this code from builtin/rebase.c

	struct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
	struct reset_head_opts ropts = { 0 };
	int ret;

	strbuf_addf(&branch_reflog, "%s (finish): %s onto %s",
		    opts->reflog_action,
		    opts->head_name, oid_to_hex(&opts->onto->object.oid));
	strbuf_addf(&head_reflog, "%s (finish): returning to %s",
		    opts->reflog_action, opts->head_name);
	ropts.branch = opts->head_name;
	ropts.flags = RESET_HEAD_REFS_ONLY;
	ropts.branch_msg = branch_reflog.buf;
	ropts.head_msg = head_reflog.buf;
	ret = reset_head(the_repository, &ropts);

can be be simplified to

	struct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
	int ret;

	strbuf_addf(&branch_reflog, "%s (finish): %s onto %s",
		    opts->reflog_action,
		    opts->head_name, oid_to_hex(&opts->onto->object.oid));
	strbuf_addf(&head_reflog, "%s (finish): returning to %s",
		    opts->reflog_action, opts->head_name);
        ret = reset_head(the_repository, &(struct reset_head_opts) {
                .branch = opts->head_name,
        	.flags = RESET_HEAD_REFS_ONLY,
        	.branch_msg = branch_reflog.buf,
        	.head_msg = head_reflog.buf,
        });

The result is more readable as one can see the value of each member
of the object being passed to the function at the call site rather
than building the object piecemeal in the preceding lines.

A common pattern in our code base is to define a struct together
with an initializer macro to initialize automatic variables and an
initializer function to initialize dynamically allocated instances
of the struct. Typically the initializer function for "struct foo"
looks like

        void foo_init(struct foo *f)
        {
                struct foo blank = FOO_INIT;
                memcpy(f, &blank, sizeof(*f));
        }

which enables us to reuse the initializer macro FOO_INIT to initalize
dynamically allocated objects. By using a compound literal we can
simplify this to

        void foo_init(struct foo *f)
        {
                *f = (struct foo) FOO_INIT;
        }

Let's add a test balloon to check for compiler support by changing
strbuf_init() to use a compound literal in the hope of using this
feature more widely in the future.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
Base-Commit: a30f80fde927d70950b3b4d1820813480968fb0d
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fcompound-literal-test-balloon%2Fv1
View-Changes-At: https://github.com/phillipwood/git/compare/a30f80fde...7ac55a509
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/compound-literal-test-balloon/v1

 strbuf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index f30fdc69843..c93c1208c93 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -68,8 +68,7 @@ char strbuf_slopbuf[1];
 
 void strbuf_init(struct strbuf *sb, size_t hint)
 {
-	struct strbuf blank = STRBUF_INIT;
-	memcpy(sb, &blank, sizeof(*sb));
+	*sb = (struct strbuf) STRBUF_INIT;
 	if (hint)
 		strbuf_grow(sb, hint);
 }
-- 
2.49.0.897.gfad3eb7d210


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2025-07-26 23:15 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-14 13:27 [PATCH] strbuf: add compound literal test balloon Phillip Wood
2025-07-14 14:26 ` Junio C Hamano
2025-07-15  8:53   ` Patrick Steinhardt
2025-07-15  9:44     ` Phillip Wood
2025-07-15 16:24     ` Junio C Hamano
2025-07-16 14:29       ` Phillip Wood
2025-07-23 18:01         ` Junio C Hamano
2025-07-23 19:31           ` [PATCH] CodingGuidelines: document test balloons in flight Junio C Hamano
2025-07-24  6:55             ` Patrick Steinhardt
2025-07-24 16:39               ` Junio C Hamano
2025-07-24 16:55                 ` Collin Funk
2025-07-26 23:15                   ` Junio C Hamano
2025-07-24 14:26             ` Phillip Wood
2025-07-24 16:23               ` Junio C Hamano
2025-07-16 14:32   ` [PATCH] strbuf: add compound literal test balloon Phillip Wood

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).