libbpf: Fix single-line struct definition output in btf_dump
authorAndrii Nakryiko <andrii@kernel.org>
Mon, 12 Dec 2022 21:15:00 +0000 (13:15 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 14 Dec 2022 23:05:12 +0000 (00:05 +0100)
btf_dump APIs emit unnecessary tabs when emitting struct/union
definition that fits on the single line. Before this patch we'd get:

struct blah {<tab>};

This patch fixes this and makes sure that we get more natural:

struct blah {};

Fixes: 44a726c3f23c ("bpftool: Print newline before '}' for struct with padding only fields")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20221212211505.558851-2-andrii@kernel.org
tools/lib/bpf/btf_dump.c

index deb2bc9a0a7b0504a25cd728b936068d79f4e261..69e80ee5f70e2595b93893ba8f6f0158d64f6ac0 100644 (file)
@@ -959,9 +959,12 @@ static void btf_dump_emit_struct_def(struct btf_dump *d,
         * Keep `struct empty {}` on a single line,
         * only print newline when there are regular or padding fields.
         */
-       if (vlen || t->size)
+       if (vlen || t->size) {
                btf_dump_printf(d, "\n");
-       btf_dump_printf(d, "%s}", pfx(lvl));
+               btf_dump_printf(d, "%s}", pfx(lvl));
+       } else {
+               btf_dump_printf(d, "}");
+       }
        if (packed)
                btf_dump_printf(d, " __attribute__((packed))");
 }