summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2025-03-24 02:17:21 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2025-03-25 12:43:16 -0600
commitf17c709c6e35a0bea471a4e8ae7d2183b4a1f32d (patch)
tree1220b9feef6f3d0572124176bc2c05b3d184771d
parente9677b4744452dcc001f1dd6c21c98ca23b6731f (diff)
lib9p: protogen: marshal, unmarshal: collapse identitical if's
-rw-r--r--lib9p/9p.generated.c72
-rw-r--r--lib9p/protogen/c_marshal.py17
-rw-r--r--lib9p/protogen/c_unmarshal.py19
3 files changed, 26 insertions, 82 deletions
diff --git a/lib9p/9p.generated.c b/lib9p/9p.generated.c
index 36e1e14..500d71b 100644
--- a/lib9p/9p.generated.c
+++ b/lib9p/9p.generated.c
@@ -2052,20 +2052,8 @@ static void unmarshal_stat(struct _unmarshal_ctx *ctx, struct lib9p_stat *out) {
if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U16LE(ctx, out->file_extension.len);
UNMARSHAL_BYTES(ctx, out->file_extension.utf8, out->file_extension.len);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U32LE(ctx, out->file_owner_n_uid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U32LE(ctx, out->file_owner_n_gid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U32LE(ctx, out->file_last_modified_n_uid);
}
#endif /* CONFIG_9P_ENABLE_9P2000_u */
@@ -2336,20 +2324,8 @@ static void unmarshal_Rstat(struct _unmarshal_ctx *ctx, struct lib9p_msg_Rstat *
if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U16LE(ctx, out->stat.file_extension.len);
UNMARSHAL_BYTES(ctx, out->stat.file_extension.utf8, out->stat.file_extension.len);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U32LE(ctx, out->stat.file_owner_n_uid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U32LE(ctx, out->stat.file_owner_n_gid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U32LE(ctx, out->stat.file_last_modified_n_uid);
}
#endif /* CONFIG_9P_ENABLE_9P2000_u */
@@ -2383,20 +2359,8 @@ static void unmarshal_Twstat(struct _unmarshal_ctx *ctx, struct lib9p_msg_Twstat
if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U16LE(ctx, out->stat.file_extension.len);
UNMARSHAL_BYTES(ctx, out->stat.file_extension.utf8, out->stat.file_extension.len);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U32LE(ctx, out->stat.file_owner_n_uid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U32LE(ctx, out->stat.file_owner_n_gid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
UNMARSHAL_U32LE(ctx, out->stat.file_last_modified_n_uid);
}
#endif /* CONFIG_9P_ENABLE_9P2000_u */
@@ -2948,20 +2912,8 @@ static bool marshal_stat(struct _marshal_ctx *ctx, struct lib9p_stat *val) {
if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U16LE(ctx, val->file_extension.len);
MARSHAL_BYTES(ctx, val->file_extension.utf8, val->file_extension.len);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U32LE(ctx, val->file_owner_n_uid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U32LE(ctx, val->file_owner_n_gid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U32LE(ctx, val->file_last_modified_n_uid);
}
#endif /* CONFIG_9P_ENABLE_9P2000_u */
@@ -3534,20 +3486,8 @@ static bool marshal_Rstat(struct _marshal_ctx *ctx, struct lib9p_msg_Rstat *val)
if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U16LE(ctx, val->stat.file_extension.len);
MARSHAL_BYTES_ZEROCOPY(ctx, val->stat.file_extension.utf8, val->stat.file_extension.len);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U32LE(ctx, val->stat.file_owner_n_uid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U32LE(ctx, val->stat.file_owner_n_gid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U32LE(ctx, val->stat.file_last_modified_n_uid);
}
#endif /* CONFIG_9P_ENABLE_9P2000_u */
@@ -3605,20 +3545,8 @@ static bool marshal_Twstat(struct _marshal_ctx *ctx, struct lib9p_msg_Twstat *va
if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U16LE(ctx, val->stat.file_extension.len);
MARSHAL_BYTES_ZEROCOPY(ctx, val->stat.file_extension.utf8, val->stat.file_extension.len);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U32LE(ctx, val->stat.file_owner_n_uid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U32LE(ctx, val->stat.file_owner_n_gid);
- }
-#endif /* CONFIG_9P_ENABLE_9P2000_u */
-#if CONFIG_9P_ENABLE_9P2000_u
- if (is_ver(ctx, 9P2000_u)) {
MARSHAL_U32LE(ctx, val->stat.file_last_modified_n_uid);
}
#endif /* CONFIG_9P_ENABLE_9P2000_u */
diff --git a/lib9p/protogen/c_marshal.py b/lib9p/protogen/c_marshal.py
index 38a2feb..36a09d4 100644
--- a/lib9p/protogen/c_marshal.py
+++ b/lib9p/protogen/c_marshal.py
@@ -254,6 +254,8 @@ def gen_c_marshal(versions: set[str], typs: list[idl.UserType]) -> str:
nonlocal indent_stack
nonlocal indent_stack_len
while len(indent_stack) > indent_stack_len:
+ if len(indent_stack) == indent_stack_len + 1 and indent_stack[-1].ifdef:
+ break
ret += f"{'\t'*(indent_lvl()-1)}}}\n"
if indent_stack.pop().ifdef:
ret += cutil.ifdef_pop(ifdef_lvl())
@@ -302,11 +304,14 @@ def gen_c_marshal(versions: set[str], typs: list[idl.UserType]) -> str:
child = path.elems[-1]
parent = path.elems[-2].typ if len(path.elems) > 1 else path.root
if child.in_versions < parent.in_versions:
- ret += cutil.ifdef_push(
+ if line := cutil.ifdef_push(
ifdef_lvl() + 1, c9util.ver_ifdef(child.in_versions)
- )
- ret += f"{'\t'*indent_lvl()}if ({c9util.ver_cond(child.in_versions)}) {{\n"
- indent_stack.append(IndentLevel(ifdef=True))
+ ):
+ ret += line
+ ret += (
+ f"{'\t'*indent_lvl()}if ({c9util.ver_cond(child.in_versions)}) {{\n"
+ )
+ indent_stack.append(IndentLevel(ifdef=True))
if child.cnt:
cnt_path = path.parent().add(child.cnt)
if child.typ.static_size == 1: # SPECIAL (zerocopy)
@@ -369,6 +374,10 @@ def gen_c_marshal(versions: set[str], typs: list[idl.UserType]) -> str:
# Pass 2 - write data
indent_stack = [IndentLevel(ifdef=True)]
idlutil.walk(typ, handle)
+ while len(indent_stack) > 1:
+ ret += f"{'\t'*(indent_lvl()-1)}}}\n"
+ if indent_stack.pop().ifdef:
+ ret += cutil.ifdef_pop(ifdef_lvl())
# Return
ret += "\treturn false;\n"
diff --git a/lib9p/protogen/c_unmarshal.py b/lib9p/protogen/c_unmarshal.py
index d076352..ea484b0 100644
--- a/lib9p/protogen/c_unmarshal.py
+++ b/lib9p/protogen/c_unmarshal.py
@@ -72,6 +72,8 @@ def gen_c_unmarshal(versions: set[str], typs: list[idl.UserType]) -> str:
nonlocal indent_stack
nonlocal indent_stack_len
while len(indent_stack) > indent_stack_len:
+ if len(indent_stack) == indent_stack_len + 1 and indent_stack[-1].ifdef:
+ break
ret += f"{'\t'*(indent_lvl()-1)}}}\n"
if indent_stack.pop().ifdef:
ret += cutil.ifdef_pop(ifdef_lvl())
@@ -82,11 +84,14 @@ def gen_c_unmarshal(versions: set[str], typs: list[idl.UserType]) -> str:
child = path.elems[-1]
parent = path.elems[-2].typ if len(path.elems) > 1 else path.root
if child.in_versions < parent.in_versions:
- ret += cutil.ifdef_push(
+ if line := cutil.ifdef_push(
ifdef_lvl() + 1, c9util.ver_ifdef(child.in_versions)
- )
- ret += f"{'\t'*indent_lvl()}if ({c9util.ver_cond(child.in_versions)}) {{\n"
- indent_stack.append(IndentLevel(ifdef=True))
+ ):
+ ret += line
+ ret += (
+ f"{'\t'*indent_lvl()}if ({c9util.ver_cond(child.in_versions)}) {{\n"
+ )
+ indent_stack.append(IndentLevel(ifdef=True))
if child.cnt:
cnt_path = path.parent().add(child.cnt)
if child.typ.static_size == 1: # SPECIAL (zerocopy)
@@ -119,7 +124,9 @@ def gen_c_unmarshal(versions: set[str], typs: list[idl.UserType]) -> str:
indent_stack = [IndentLevel(ifdef=True)]
idlutil.walk(typ, handle)
-
- ret += "}\n"
+ while len(indent_stack) > 0:
+ ret += f"{'\t'*(indent_lvl()-1)}}}\n"
+ if indent_stack.pop().ifdef and indent_stack:
+ ret += cutil.ifdef_pop(ifdef_lvl())
ret += cutil.ifdef_pop(0)
return ret