fprintf: Support skipping modifier

When doing BTF comparisons between functions defined in multiple CUs, it
was noticed a few critical functions failed prototype comparisons due to
multiple "const" modifiers; for example:

function mismatch for 'memchr_inv'('memchr_inv'): 'void * ()(const const void  * , int, size_t)' != 'void * ()(const void  *, int, size_t)'

function mismatch for 'strnlen'('strnlen'): '__kernel_size_t ()(const const char  * , __kernel_size_t)' != '__kernel_size_t ()(const char  *, size_t)'

(note the "const const" in the first parameter.)

As such it would be useful to omit modifiers for comparison
purposes.  Also noted was the fact that for the "no_parm_names"
case, an extra space was being emitted in some cases, also
throwing off string comparisons of prototypes.

Committer notes:

We need to check if conf is NULL in tag__ptr_name() before accessing
conf->skip_emitting_modifier, to avoid a segfault noticed when testing
with 'btfdiff vmlinux'.

Ditto for __tag__name(), but there we can use pconf-> instead as that
one is guaranteed to have the 'conf' contents or point to a default
conf_fprintf struct.

Also put back the removed whitespace after "const", making it "const "
in __tag__name(), to fix a problem reported by Eduard Zingerman.

Link: https://lore.kernel.org/bpf/87964239858beb2fe8e2d625953a3606161c85b3.camel@gmail.com
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Tested-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alan Maguire <alan.maguire@oracle.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Hao Luo <haoluo@google.com>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: KP Singh <kpsingh@chromium.org>
Cc: Kui-Feng Lee <sinquersw@gmail.com>
Cc: Martin KaFai Lau <martin.lau@kernel.org>
Cc: Song Liu <songliubraving@fb.com>
Cc: Stanislav Fomichev <sdf@google.com>
Cc: Timo Beckers <timo@incline.eu>
Cc: Yonghong Song <yhs@fb.com>
Cc: bpf@vger.kernel.org
Link: https://lore.kernel.org/bpf/1678459850-16140-3-git-send-email-alan.maguire@oracle.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
diff --git a/dwarves.h b/dwarves.h
index d04a36d..7a319d1 100644
--- a/dwarves.h
+++ b/dwarves.h
@@ -134,6 +134,7 @@
 	uint8_t	   strip_inline:1;
 	uint8_t	   skip_emitting_atomic_typedefs:1;
 	uint8_t	   skip_emitting_errors:1;
+	uint8_t    skip_emitting_modifier:1;
 };
 
 struct cus;
diff --git a/dwarves_fprintf.c b/dwarves_fprintf.c
index 5c6bf9c..e8399e7 100644
--- a/dwarves_fprintf.c
+++ b/dwarves_fprintf.c
@@ -506,7 +506,8 @@
 				struct tag *next_type = cu__type(cu, type->type);
 
 				if (next_type && tag__is_pointer(next_type)) {
-					const_pointer = "const ";
+					if (!(conf && conf->skip_emitting_modifier))
+						const_pointer = "const ";
 					type = next_type;
 				}
 			}
@@ -580,13 +581,16 @@
 				   *type_str = __tag__name(type, cu, tmpbf,
 							   sizeof(tmpbf),
 							   pconf);
-			switch (tag->tag) {
-			case DW_TAG_volatile_type: prefix = "volatile "; break;
-			case DW_TAG_const_type:    prefix = "const ";	 break;
-			case DW_TAG_restrict_type: suffix = " restrict"; break;
-			case DW_TAG_atomic_type:   prefix = "_Atomic ";  break;
+			if (!pconf->skip_emitting_modifier) {
+				switch (tag->tag) {
+				case DW_TAG_volatile_type: prefix = "volatile "; break;
+				case DW_TAG_const_type: prefix = "const "; break;
+				case DW_TAG_restrict_type: suffix = " restrict"; break;
+				case DW_TAG_atomic_type:   prefix = "_Atomic ";  break;
+				}
 			}
-			snprintf(bf, len, "%s%s%s ", prefix, type_str, suffix);
+			snprintf(bf, len, "%s%s%s%s", prefix, type_str, suffix,
+				 pconf->no_parm_names ? "" : " ");
 		}
 		break;
 	case DW_TAG_array_type:
@@ -818,9 +822,11 @@
 	case DW_TAG_const_type:
 		modifier = "const";
 print_modifier: {
-		size_t modifier_printed = fprintf(fp, "%s ", modifier);
-		tconf.type_spacing -= modifier_printed;
-		printed		   += modifier_printed;
+		if (!conf->skip_emitting_modifier) {
+			size_t modifier_printed = fprintf(fp, "%s ", modifier);
+			tconf.type_spacing -= modifier_printed;
+			printed		   += modifier_printed;
+		}
 
 		struct tag *ttype = cu__type(cu, type->type);
 		if (ttype) {