Skip to content

Commit 368b584

Browse files
avargitster
authored andcommitted
common-main.c: call exit(), don't return
Change the main() function to call "exit()" instead of ending with a "return" statement. The "exit()" function is our own wrapper that calls trace2_cmd_exit_fl() for us, from git-compat-util.h: #define exit(code) exit(trace2_cmd_exit_fl(__FILE__, __LINE__, (code))) That "exit()" wrapper has been in use ever since ee4512e (trace2: create new combined trace facility, 2019-02-22). This changes nothing about how we "exit()", as we'd invoke "trace2_cmd_exit_fl()" in both cases due to the wrapper, this change makes it easier to reason about this code, as we're now always obviously relying on our "exit()" wrapper. There is already code immediately downstream of our "main()" which has a hard reliance on that, e.g. the various "exit()" calls downstream of "cmd_main()" in "git.c". We even had a comment in "t/helper/test-trace2.c" that seemed to be confused about how the "exit()" wrapper interacted with uses of "return", even though it was introduced in the same trace2 series in a15860d (trace2: t/helper/test-trace2, t0210.sh, t0211.sh, t0212.sh, 2019-02-22), after the aforementioned ee4512e. Perhaps it pre-dated the "exit()" wrapper? This change makes the "trace2_cmd_exit()" macro orphaned, we now always use "trace2_cmd_exit_fl()" directly, but let's keep that simpler example in place. Even if we're unlikely to get another "main()" other than the one in our "common-main.c", there's some value in having the API documentation and example discuss a simpler version that doesn't require an "exit()" wrapper macro. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent abe6bb3 commit 368b584

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

common-main.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ int main(int argc, const char **argv)
5151

5252
result = cmd_main(argc, argv);
5353

54-
trace2_cmd_exit(result);
55-
56-
return result;
54+
/*
55+
* We define exit() to call trace2_cmd_exit_fl() in
56+
* git-compat-util.h. Whether we reach this or exit()
57+
* elsewhere we'll always run our trace2 exit handler.
58+
*/
59+
exit(result);
5760
}

t/helper/test-trace2.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,9 @@ static int print_usage(void)
262262
* [] the "cmd_name" event has been generated.
263263
* [] this writes various "def_param" events for interesting config values.
264264
*
265-
* We further assume that if we return (rather than exit()), trace2_cmd_exit()
266-
* will be called by test-tool.c:cmd_main().
265+
* We return from here and let test-tool.c::cmd_main() pass the exit
266+
* code to common-main.c::main(), which will use it to call
267+
* trace2_cmd_exit().
267268
*/
268269
int cmd__trace2(int argc, const char **argv)
269270
{

0 commit comments

Comments
 (0)