summaryrefslogtreecommitdiff
path: root/lib9p/protogen/c_format.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib9p/protogen/c_format.py')
-rw-r--r--lib9p/protogen/c_format.py37
1 files changed, 28 insertions, 9 deletions
diff --git a/lib9p/protogen/c_format.py b/lib9p/protogen/c_format.py
index a1bcbf3..4a809d1 100644
--- a/lib9p/protogen/c_format.py
+++ b/lib9p/protogen/c_format.py
@@ -21,6 +21,19 @@ def bf_numname(typ: idl.Bitfield, num: idl.BitNum, base: str) -> str:
return c9util.Ident(c9util.add_prefix(prefix, base))
+def ext_printf(line: str) -> str:
+ assert line.startswith("\t")
+ assert line.endswith("\n")
+ # It sucks that %v trips -Wformat and -Wformat-extra-args
+ # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47781
+ ret = "#pragma GCC diagnostic push\n"
+ ret += '#pragma GCC diagnostic ignored "-Wformat"\n'
+ ret += '#pragma GCC diagnostic ignored "-Wformat-extra-args"\n'
+ ret += line
+ ret += "#pragma GCC diagnostic pop\n"
+ return ret
+
+
def gen_c_format(versions: set[str], typs: list[idl.UserType]) -> str:
ret = """
/* *_format *******************************************************************/
@@ -93,12 +106,9 @@ def gen_c_format(versions: set[str], typs: list[idl.UserType]) -> str:
ret += "\t\tfmt_state_putchar(state, '0');\n"
ret += "\tfmt_state_putchar(state, ')');\n"
case idl.Struct(typname="s"): # SPECIAL(string)
- ret += "\t/* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47781 */\n"
- ret += "#pragma GCC diagnostic push\n"
- ret += '#pragma GCC diagnostic ignored "-Wformat"\n'
- ret += '#pragma GCC diagnostic ignored "-Wformat-extra-args"\n'
- ret += '\tfmt_state_printf(state, "%.*q", self->len, self->utf8);\n'
- ret += "#pragma GCC diagnostic pop\n"
+ ret += ext_printf(
+ '\tfmt_state_printf(state, "%.*q", self->len, self->utf8);\n'
+ )
case idl.Struct(): # and idl.Message():
if isinstance(typ, idl.Message):
ret += f'\tfmt_state_puts(state, "{typ.typname} {{");\n'
@@ -109,15 +119,24 @@ def gen_c_format(versions: set[str], typs: list[idl.UserType]) -> str:
continue
ret += cutil.ifdef_push(2, c9util.ver_ifdef(member.in_versions))
if member.cnt:
- if member.typ.static_size == 1: # SPECIAL (data)
- ret += f'\tfmt_state_puts(state, " {member.membname}=<bytedata>");\n'
- continue
if isinstance(member.cnt, int):
cnt_str = str(member.cnt)
cnt_typ = "size_t"
else:
cnt_str = f"self->{member.cnt.membname}"
cnt_typ = c9util.typename(member.cnt.typ)
+ if member.typ.static_size == 1: # SPECIAL (data)
+ ret += f"\tif (is_valid_utf8_without_nul((uint8_t *)self->{member.membname}, (size_t){cnt_str})) {{\n"
+ ret += ext_printf(
+ f'\t\tfmt_state_printf(state, " {member.membname}=%.*q%s",\n'
+ f"\t\t\t(int)({cnt_str} < 50 ? {cnt_str} : 50),\n"
+ f"\t\t\t(char *)self->{member.membname},\n"
+ f'\t\t\t{cnt_str} < 50 ? "" : "...");\n'
+ )
+ ret += "\t} else {\n"
+ ret += f'\t\tfmt_state_puts(state, " {member.membname}=<bytedata>");\n'
+ ret += "\t}\n"
+ continue
ret += f'\tfmt_state_puts(state, " {member.membname}=[");\n'
ret += f"\tfor ({cnt_typ} i = 0; i < {cnt_str}; i++) {{\n"
ret += "\t\tif (i)\n"