git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 08/22] run-command.c: use C99 "for (TYPE VAR = ..." syntax where useful

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Oct 12, 2022, 13:04 UTC
Message-ID
<ae08523f-13ac-d9aa-b787-f136a88b5804@gmail.com>
In-Reply-To
<patch-v2-08.22-279b0430c5d-20221012T084850Z-avarab@gmail.com>
Hi Ævar
On 12/10/2022 10:01, Ævar Arnfjörð Bjarmason wrote:
Show 45 quoted lines
> Refactor code in run-command.c where the "i" iteration variable is
> being compared to an unsigned type, or where it's being shadowed.
> 
> In a subsequent commit the type of the "n" variable will be changed,
> this also helps to avoid some of the warnings we've had under
> "DEVOPTS=extra-all" since 8d133a4653a (strvec: use size_t to store nr
> and alloc, 2021-09-11).
> 
> Per the thread ending at [1] we already have this C99 syntax in the
> v2.38.0 release, per 6563706568b (CodingGuidelines: give deadline for
> "for (int i = 0; ...", 2022-03-30) we should re-visit the wider use of
> this syntax for November 2022, meaning within the window of v2.39.0.
> 
> As of writing it's earlier than that deadline and per [1] we want to
> "avoid open[ing] the floodgate and deliberately add more [this C99
> syntax]". But in this case the use of the syntax solves a real problem
> of conflating types, which we'd otherwise need to avoid by using
> differently named iteration variables (and the associated larger
> refactoring).
> 
> 1. https://lore.kernel.org/git/xmqqy1wgwkbj.fsf@gitster.g/
> 
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>   run-command.c | 12 +++++-------
>   1 file changed, 5 insertions(+), 7 deletions(-)
> 
> diff --git a/run-command.c b/run-command.c
> index bd45828fe2c..7b27e4de78d 100644
> --- a/run-command.c
> +++ b/run-command.c
> @@ -443,7 +443,6 @@ static char **prep_childenv(const char *const *deltaenv)
>   	struct string_list env = STRING_LIST_INIT_DUP;
>   	struct strbuf key = STRBUF_INIT;
>   	const char *const *p;
> -	int i;
>   
>   	/* Construct a sorted string list consisting of the current environ */
>   	for (p = (const char *const *) environ; p && *p; p++) {
> @@ -476,7 +475,7 @@ static char **prep_childenv(const char *const *deltaenv)
>   
>   	/* Create an array of 'char *' to be used as the childenv */
>   	ALLOC_ARRAY(childenv, env.nr + 1);
> -	for (i = 0; i < env.nr; i++)
> +	for (size_t i = 0; i < env.nr; i++)

In this case we're changing the type to avoid a signed/unsigned comparison. We could achieve this by just changing the type of i above.

Show 17 quoted lines
>   		childenv[i] = env.items[i].util;
>   	childenv[env.nr] = NULL;
>   
> @@ -581,7 +580,6 @@ static void trace_add_env(struct strbuf *dst, const char *const *deltaenv)
>   {
>   	struct string_list envs = STRING_LIST_INIT_DUP;
>   	const char *const *e;
> -	int i;
>   	int printed_unset = 0;
>   
>   	/* Last one wins, see run-command.c:prep_childenv() for context */
> @@ -599,7 +597,7 @@ static void trace_add_env(struct strbuf *dst, const char *const *deltaenv)
>   	}
>   
>   	/* "unset X Y...;" */
> -	for (i = 0; i < envs.nr; i++) {
> +	for (size_t i = 0; i < envs.nr; i++) {
Ditto
Show 9 quoted lines
>   		const char *var = envs.items[i].string;
>   		const char *val = envs.items[i].util;
>   
> @@ -616,7 +614,7 @@ static void trace_add_env(struct strbuf *dst, const char *const *deltaenv)
>   		strbuf_addch(dst, ';');
>   
>   	/* ... followed by "A=B C=D ..." */
> -	for (i = 0; i < envs.nr; i++) {
> +	for (size_t i = 0; i < envs.nr; i++) {
Ditto
Show 18 quoted lines
>   		const char *var = envs.items[i].string;
>   		const char *val = envs.items[i].util;
>   		const char *oldval;
> @@ -1789,7 +1787,7 @@ void run_processes_parallel(int n,
>   			    task_finished_fn task_finished,
>   			    void *pp_cb)
>   {
> -	int i, code;
> +	int code;
>   	int output_timeout = 100;
>   	int spawn_cap = 4;
>   	int ungroup = run_processes_parallel_ungroup;
> @@ -1801,7 +1799,7 @@ void run_processes_parallel(int n,
>   	pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb,
>   		ungroup);
>   	while (1) {
> -		for (i = 0;
> +		for (int i = 0;
Here we are moving the declaration to the loop.

Am I missing something, I don't understand how these changes match this description in the commit message

 > But in this case the use of the syntax solves a real problem
 > of conflating types, which we'd otherwise need to avoid by using
 > differently named iteration variables (and the associated larger
 > refactoring).

Where are the cases where we'd need differently named variables if we did not use this syntax. In each case we delete the old declaration.

While I can see the point in avoiding the signed/unsigned comparisons I don't think it is strictly related to the topic of this series.

Best Wishes
Phillip
>   		    i < spawn_cap && !pp.shutdown &&
>   		    pp.nr_processes < pp.max_processes;
>   		    i++) {
Previous: Ævar Arnfjörð BjarmasonNext: Junio C Hamano
Message 35 of 79 in “run-command API: pass functions & opts via struct”
  1. 00/15 run-command API: pass functions & opts via structÆvar Arnfjörð Bjarmason, Sep 30, 2022
  2. 01/15 hook tests: fix redirection logic error in 96e7225b310Ævar Arnfjörð Bjarmason, Sep 30, 2022
  3. 02/15 submodule tests: reset "trace.out" between "grep" invocationsÆvar Arnfjörð Bjarmason, Sep 30, 2022
  4. 03/15 run-command tests: test stdout of run_command_parallel()Ævar Arnfjörð Bjarmason, Sep 30, 2022
  5. 06/15 run-command API: have "run_processes_parallel{,_tr2}()" return voidÆvar Arnfjörð Bjarmason, Sep 30, 2022
  6. Phillip WoodOct 7, 2022
  7. 05/15 run-command tests: use "return", not "exit"Ævar Arnfjörð Bjarmason, Sep 30, 2022
  8. Phillip WoodOct 7, 2022
  9. 04/15 run-command test helper: use "else if" patternÆvar Arnfjörð Bjarmason, Sep 30, 2022
  10. 09/15 run-command.c: add an initializer for "struct parallel_processes"Ævar Arnfjörð Bjarmason, Sep 30, 2022
  11. 07/15 run-command API: make "jobs" parameter an "unsigned int"Ævar Arnfjörð Bjarmason, Sep 30, 2022
  12. Calvin WanOct 4, 2022
  13. Phillip WoodOct 7, 2022
  14. 13/15 run-command API: move *_tr2() users to "run_processes_parallel()"Ævar Arnfjörð Bjarmason, Sep 30, 2022
  15. 10/15 run-command API: add nascent "struct run_process_parallel_opts"Ævar Arnfjörð Bjarmason, Sep 30, 2022
  16. Phillip WoodOct 7, 2022
  17. 08/15 run-command API: don't fall back on online_cpus()Ævar Arnfjörð Bjarmason, Sep 30, 2022
  18. Phillip WoodOct 7, 2022
  19. 11/15 run-command API: make run_process_parallel{,_tr2}() thin wrappersÆvar Arnfjörð Bjarmason, Sep 30, 2022
  20. 14/15 run-command.c: don't copy *_fn to "struct parallel_processes"Ævar Arnfjörð Bjarmason, Sep 30, 2022
  21. 12/15 run-command API: have run_process_parallel() take an "opts" structÆvar Arnfjörð Bjarmason, Sep 30, 2022
  22. 15/15 run-command.c: don't copy "ungroup" to "struct parallel_processes"Ævar Arnfjörð Bjarmason, Sep 30, 2022
  23. Calvin WanOct 4, 2022
  24. Phillip WoodOct 7, 2022
  25. Junio C HamanoOct 7, 2022
  26. 00/22 run-command API: pass functions & opts via structÆvar Arnfjörð Bjarmason, Oct 12, 2022
  27. 01/22 hook tests: fix redirection logic error in 96e7225b310Ævar Arnfjörð Bjarmason, Oct 12, 2022
  28. 02/22 submodule tests: reset "trace.out" between "grep" invocationsÆvar Arnfjörð Bjarmason, Oct 12, 2022
  29. 04/22 run-command test helper: use "else if" patternÆvar Arnfjörð Bjarmason, Oct 12, 2022
  30. 03/22 run-command tests: test stdout of run_command_parallel()Ævar Arnfjörð Bjarmason, Oct 12, 2022
  31. 05/22 run-command API: have "run_processes_parallel{,_tr2}()" return voidÆvar Arnfjörð Bjarmason, Oct 12, 2022
  32. 06/22 run-command tests: use "return", not "exit"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  33. 07/22 run-command.c: remove dead assignment in while-loopÆvar Arnfjörð Bjarmason, Oct 12, 2022
  34. 08/22 run-command.c: use C99 "for (TYPE VAR = ..." syntax where usefulÆvar Arnfjörð Bjarmason, Oct 12, 2022
  35. Phillip WoodOct 12, 2022
  36. Junio C HamanoOct 12, 2022
  37. 10/22 run-command API: don't fall back on online_cpus()Ævar Arnfjörð Bjarmason, Oct 12, 2022
  38. Phillip WoodOct 12, 2022
  39. 11/22 run-command.c: use designated init for pp_init(), add "const"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  40. 09/22 run-command API: make "n" parameter a "size_t"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  41. Phillip WoodOct 12, 2022
  42. 13/22 run-command API: make run_process_parallel{,_tr2}() thin wrappersÆvar Arnfjörð Bjarmason, Oct 12, 2022
  43. Phillip WoodOct 12, 2022
  44. 12/22 run-command API: add nascent "struct run_process_parallel_opts"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  45. 14/22 run-command API: have run_process_parallel() take an "opts" structÆvar Arnfjörð Bjarmason, Oct 12, 2022
  46. 15/22 run-command API: move *_tr2() users to "run_processes_parallel()"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  47. 16/22 run-command.c: make "struct parallel_processes" const if possibleÆvar Arnfjörð Bjarmason, Oct 12, 2022
  48. 17/22 run-command.c: don't copy *_fn to "struct parallel_processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  49. 18/22 run-command.c: don't copy "ungroup" to "struct parallel_processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  50. 19/22 run-command.c: don't copy "data" to "struct parallel_processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  51. 21/22 run-command.c: pass "opts" further down, and use "opts->processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  52. 20/22 run-command.c: use "opts->processes", not "pp->max_processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  53. 22/22 run-command.c: remove "pp->max_processes", add "const" to signal() handlerÆvar Arnfjörð Bjarmason, Oct 12, 2022
  54. Ævar Arnfjörð BjarmasonOct 12, 2022
  55. Phillip WoodOct 12, 2022
  56. 00/15 run-command API: pass functions & opts via structÆvar Arnfjörð Bjarmason, Oct 12, 2022
  57. 01/15 run-command test helper: use "else if" patternÆvar Arnfjörð Bjarmason, Oct 12, 2022
  58. 02/15 run-command API: have "run_processes_parallel{,_tr2}()" return voidÆvar Arnfjörð Bjarmason, Oct 12, 2022
  59. 03/15 run-command tests: use "return", not "exit"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  60. 04/15 run-command API: make "n" parameter a "size_t"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  61. Phillip WoodOct 14, 2022
  62. 05/15 run-command API: don't fall back on online_cpus()Ævar Arnfjörð Bjarmason, Oct 12, 2022
  63. 06/15 run-command.c: use designated init for pp_init(), add "const"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  64. 08/15 run-command API: move *_tr2() users to "run_processes_parallel()"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  65. 07/15 run-command API: have run_process_parallel() take an "opts" structÆvar Arnfjörð Bjarmason, Oct 12, 2022
  66. Phillip WoodOct 14, 2022
  67. 09/15 run-command.c: make "struct parallel_processes" const if possibleÆvar Arnfjörð Bjarmason, Oct 12, 2022
  68. 13/15 run-command.c: use "opts->processes", not "pp->max_processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  69. 11/15 run-command.c: don't copy "ungroup" to "struct parallel_processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  70. 10/15 run-command.c: don't copy *_fn to "struct parallel_processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  71. 14/15 run-command.c: pass "opts" further down, and use "opts->processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  72. 15/15 run-command.c: remove "max_processes", add "const" to signal() handlerÆvar Arnfjörð Bjarmason, Oct 12, 2022
  73. Glen ChooOct 13, 2022
  74. 12/15 run-command.c: don't copy "data" to "struct parallel_processes"Ævar Arnfjörð Bjarmason, Oct 12, 2022
  75. Calvin WanOct 13, 2022
  76. Junio C HamanoOct 13, 2022
  77. Phillip WoodOct 14, 2022
  78. Ævar Arnfjörð BjarmasonOct 14, 2022
  79. Junio C HamanoOct 14, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.