[Libguestfs] [libnbd PATCH 14/23] generator/C: print_trace_enter: generate commas as suffixes, not prefixes

Laszlo Ersek lersek at redhat.com
Tue Apr 25 07:10:58 UTC 2023


The iteration creating the "args" values and the iteration creating the
"optargs" values both print commas as prefixes; in other words, whenever
any pattern matches, the "pr" that's invoked starts with a ", " substring.

For the sake of a subsequent patch, print commas as suffixes, not as
prefixes:

- Once we're done outputting the format string, calculate the total number
  of arguments (args + optargs).

- If there is at least one argument to print, follow the format string
  with a comma-space (", ").

- Turn both iterations (i.e., over "args" and "optargs") into numbered
  iterations (i.e., use "List.iteri").

- From each pattern's own "pr" call, strip the comma-space (", ") prefix.

- After the "match", see if the (opt)arg just handled was the last one
  against the *total* number of arguments; if not, print the comma-space
  as a suffix.

This patch is purely refactoring, it does not change the generated output.

The command "git show --color-words" is somewhat helpful for viewing the
patch.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2172516
Signed-off-by: Laszlo Ersek <lersek at redhat.com>
---
 generator/C.ml | 45 ++++++++++++--------
 1 file changed, 27 insertions(+), 18 deletions(-)

diff --git a/generator/C.ml b/generator/C.ml
index 38b7580b39af..b4118bf41b3a 100644
--- a/generator/C.ml
+++ b/generator/C.ml
@@ -774,32 +774,41 @@ let
       | OFlags (n, _, _) -> pr " %s=0x%%x" n
     ) optargs;
     pr "\"";
-    List.iter (
-      fun arg ->
+    let num_args = List.length args
+    and num_optargs = List.length optargs in
+    let num_allargs = num_args + num_optargs in
+    if num_allargs > 0 then
+      pr ", ";
+    List.iteri (
+      fun i arg ->
         (match arg with
-         | Bool n -> pr ", %s ? \"true\" : \"false\"" n
+         | Bool n -> pr "%s ? \"true\" : \"false\"" n
          | BytesOut (n, count)
-         | BytesPersistOut (n, count) -> pr ", %s" count
+         | BytesPersistOut (n, count) -> pr "%s" count
          | BytesIn (n, count)
          | BytesPersistIn (n, count) ->
-            pr ", %s_printable ? %s_printable : \"\", %s" n n count
-         | Closure _ -> pr ", \"<fun>\""
-         | Enum (n, _) -> pr ", %s" n
-         | Flags (n, _) -> pr ", %s" n
-         | Fd n | Int n | Int64 n | SizeT n -> pr ", %s" n
-         | SockAddrAndLen (_, len) -> pr ", (int) %s" len
+            pr "%s_printable ? %s_printable : \"\", %s" n n count
+         | Closure _ -> pr "\"<fun>\""
+         | Enum (n, _) -> pr "%s" n
+         | Flags (n, _) -> pr "%s" n
+         | Fd n | Int n | Int64 n | SizeT n -> pr "%s" n
+         | SockAddrAndLen (_, len) -> pr "(int) %s" len
          | Path n | String n | StringList n ->
-            pr ", %s_printable ? %s_printable : \"\"" n n
-         | UInt n | UInt32 n | UInt64 n | UIntPtr n -> pr ", %s" n
-        )
+            pr "%s_printable ? %s_printable : \"\"" n n
+         | UInt n | UInt32 n | UInt64 n | UIntPtr n -> pr "%s" n
+        );
+        if i < num_allargs - 1 then
+          pr ", "
     ) args;
-    List.iter (
-      fun optarg ->
+    List.iteri (
+      fun i optarg ->
         (match optarg with
          | OClosure { cbname } ->
-            pr ", CALLBACK_IS_NULL (%s_callback) ? \"<fun>\" : \"NULL\"" cbname
-         | OFlags (n, _, _) -> pr ", %s" n
-        )
+            pr "CALLBACK_IS_NULL (%s_callback) ? \"<fun>\" : \"NULL\"" cbname
+         | OFlags (n, _, _) -> pr "%s" n
+        );
+        if num_args + i < num_allargs - 1 then
+          pr ", "
     ) optargs;
     pr ");\n";
     List.iter (



More information about the Libguestfs mailing list