diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-01-17 12:06:17 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-01-17 12:19:24 -0700 |
commit | 319987dc70d012c711109589328887c117112965 (patch) | |
tree | 77c6285f93ec6089037a4af025c247847dbe7fd7 | |
parent | 6e48efec92b3357b2b762d14994d2f812a3b6ef7 (diff) |
lib9p: idl.gen: Fix c_ver_cond() compiling with versions disabled
-rw-r--r-- | lib9p/9p.generated.c | 67 | ||||
-rwxr-xr-x | lib9p/idl.gen | 23 |
2 files changed, 68 insertions, 22 deletions
diff --git a/lib9p/9p.generated.c b/lib9p/9p.generated.c index 67644e4..df00f7b 100644 --- a/lib9p/9p.generated.c +++ b/lib9p/9p.generated.c @@ -11,6 +11,31 @@ #include "internal.h" +/* utilities ******************************************************************/ + +/** + * is_ver(ctx, ver) is essentially `(ctx->ctx->version == LIB9P_VER_##ver)`, + * but compiles correctly (to `false`) even if `LIB9P_VER_##ver` isn't defined + * (because `!CONFIG_9P_ENABLE_##ver`). This is useful when `||`ing + * several version checks together. + */ +#define is_ver(ctx, ver) _is_ver_##ver(ctx->ctx->version) +#if CONFIG_9P_ENABLE_9P2000 + #define _is_ver_9P2000(v) (v == LIB9P_VER_9P2000) +#else + #define _is_ver_9P2000(v) false +#endif +#if CONFIG_9P_ENABLE_9P2000_e + #define _is_ver_9P2000_e(v) (v == LIB9P_VER_9P2000_e) +#else + #define _is_ver_9P2000_e(v) false +#endif +#if CONFIG_9P_ENABLE_9P2000_u + #define _is_ver_9P2000_u(v) (v == LIB9P_VER_9P2000_u) +#else + #define _is_ver_9P2000_u(v) false +#endif + /* strings ********************************************************************/ static const char *version_strs[LIB9P_VER_NUM] = { @@ -493,7 +518,7 @@ LM_FLATTEN static bool validate_Rerror(struct _validate_ctx *ctx) { || validate_tag(ctx) || validate_s(ctx) #if CONFIG_9P_ENABLE_9P2000_u - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && validate_4(ctx) ) + || ( is_ver(ctx, 9P2000_u) && validate_4(ctx) ) #endif /* CONFIG_9P_ENABLE_9P2000_u */ || ({ uint32_t exp = ctx->net_offset - _size_offset; (((uint32_t)size) != exp) && lib9p_errorf(ctx->ctx, LINUX_EBADMSG, "size value is wrong (actual:%"PRIu32" != correct:%"PRIu32")", (uint32_t)size, exp); }) @@ -585,7 +610,7 @@ LM_FLATTEN static bool validate_Tauth(struct _validate_ctx *ctx) { || validate_s(ctx) || validate_s(ctx) #if CONFIG_9P_ENABLE_9P2000_u - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && validate_nuid(ctx) ) + || ( is_ver(ctx, 9P2000_u) && validate_nuid(ctx) ) #endif /* CONFIG_9P_ENABLE_9P2000_u */ || ({ uint32_t exp = ctx->net_offset - _size_offset; (((uint32_t)size) != exp) && lib9p_errorf(ctx->ctx, LINUX_EBADMSG, "size value is wrong (actual:%"PRIu32" != correct:%"PRIu32")", (uint32_t)size, exp); }) @@ -607,7 +632,7 @@ LM_FLATTEN static bool validate_Tattach(struct _validate_ctx *ctx) { || validate_s(ctx) || validate_s(ctx) #if CONFIG_9P_ENABLE_9P2000_u - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && validate_nuid(ctx) ) + || ( is_ver(ctx, 9P2000_u) && validate_nuid(ctx) ) #endif /* CONFIG_9P_ENABLE_9P2000_u */ || ({ uint32_t exp = ctx->net_offset - _size_offset; (((uint32_t)size) != exp) && lib9p_errorf(ctx->ctx, LINUX_EBADMSG, "size value is wrong (actual:%"PRIu32" != correct:%"PRIu32")", (uint32_t)size, exp); }) @@ -669,10 +694,10 @@ LM_ALWAYS_INLINE static bool validate_stat(struct _validate_ctx *ctx) { || validate_s(ctx) || validate_s(ctx) #if CONFIG_9P_ENABLE_9P2000_u - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && validate_s(ctx) ) - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && validate_nuid(ctx) ) - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && validate_nuid(ctx) ) - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && validate_nuid(ctx) ) + || ( is_ver(ctx, 9P2000_u) && validate_s(ctx) ) + || ( is_ver(ctx, 9P2000_u) && validate_nuid(ctx) ) + || ( is_ver(ctx, 9P2000_u) && validate_nuid(ctx) ) + || ( is_ver(ctx, 9P2000_u) && validate_nuid(ctx) ) #endif /* CONFIG_9P_ENABLE_9P2000_u */ || ({ uint16_t exp = ctx->net_offset - _kern_type_offset; (((uint16_t)stat_size) != exp) && lib9p_errorf(ctx->ctx, LINUX_EBADMSG, "stat_size value is wrong (actual:%"PRIu16" != correct:%"PRIu16")", (uint16_t)stat_size, exp); }) @@ -1040,7 +1065,7 @@ LM_FLATTEN static void unmarshal_Rerror(struct _unmarshal_ctx *ctx, struct lib9p unmarshal_tag(ctx, &out->tag); unmarshal_s(ctx, &out->ename); #if CONFIG_9P_ENABLE_9P2000_u - if ( (ctx->ctx->version==LIB9P_VER_9P2000_u) ) unmarshal_4(ctx, &out->errno); + if ( is_ver(ctx, 9P2000_u) ) unmarshal_4(ctx, &out->errno); #endif /* CONFIG_9P_ENABLE_9P2000_u */ } @@ -1105,7 +1130,7 @@ LM_FLATTEN static void unmarshal_Tauth(struct _unmarshal_ctx *ctx, struct lib9p_ unmarshal_s(ctx, &out->uname); unmarshal_s(ctx, &out->aname); #if CONFIG_9P_ENABLE_9P2000_u - if ( (ctx->ctx->version==LIB9P_VER_9P2000_u) ) unmarshal_nuid(ctx, &out->n_uid); + if ( is_ver(ctx, 9P2000_u) ) unmarshal_nuid(ctx, &out->n_uid); #endif /* CONFIG_9P_ENABLE_9P2000_u */ } @@ -1119,7 +1144,7 @@ LM_FLATTEN static void unmarshal_Tattach(struct _unmarshal_ctx *ctx, struct lib9 unmarshal_s(ctx, &out->uname); unmarshal_s(ctx, &out->aname); #if CONFIG_9P_ENABLE_9P2000_u - if ( (ctx->ctx->version==LIB9P_VER_9P2000_u) ) unmarshal_nuid(ctx, &out->n_uid); + if ( is_ver(ctx, 9P2000_u) ) unmarshal_nuid(ctx, &out->n_uid); #endif /* CONFIG_9P_ENABLE_9P2000_u */ } @@ -1158,10 +1183,10 @@ LM_ALWAYS_INLINE static void unmarshal_stat(struct _unmarshal_ctx *ctx, struct l unmarshal_s(ctx, &out->file_owner_gid); unmarshal_s(ctx, &out->file_last_modified_uid); #if CONFIG_9P_ENABLE_9P2000_u - if ( (ctx->ctx->version==LIB9P_VER_9P2000_u) ) unmarshal_s(ctx, &out->file_extension); - if ( (ctx->ctx->version==LIB9P_VER_9P2000_u) ) unmarshal_nuid(ctx, &out->file_owner_n_uid); - if ( (ctx->ctx->version==LIB9P_VER_9P2000_u) ) unmarshal_nuid(ctx, &out->file_owner_n_gid); - if ( (ctx->ctx->version==LIB9P_VER_9P2000_u) ) unmarshal_nuid(ctx, &out->file_last_modified_n_uid); + if ( is_ver(ctx, 9P2000_u) ) unmarshal_s(ctx, &out->file_extension); + if ( is_ver(ctx, 9P2000_u) ) unmarshal_nuid(ctx, &out->file_owner_n_uid); + if ( is_ver(ctx, 9P2000_u) ) unmarshal_nuid(ctx, &out->file_owner_n_gid); + if ( is_ver(ctx, 9P2000_u) ) unmarshal_nuid(ctx, &out->file_last_modified_n_uid); #endif /* CONFIG_9P_ENABLE_9P2000_u */ } @@ -1577,7 +1602,7 @@ LM_FLATTEN static bool marshal_Rerror(struct _marshal_ctx *ctx, struct lib9p_msg || marshal_tag(ctx, &val->tag) || marshal_s(ctx, &val->ename) #if CONFIG_9P_ENABLE_9P2000_u - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && marshal_4(ctx, &val->errno) ) + || ( is_ver(ctx, 9P2000_u) && marshal_4(ctx, &val->errno) ) #endif /* CONFIG_9P_ENABLE_9P2000_u */ || ({ encode_u32le(ctx->net_offset - _size_offset, &ctx->net_bytes[_size_offset]); false; }) || ({ encode_u8le(107, &ctx->net_bytes[_typ_offset]); false; }) @@ -1663,7 +1688,7 @@ LM_FLATTEN static bool marshal_Tauth(struct _marshal_ctx *ctx, struct lib9p_msg_ || marshal_s(ctx, &val->uname) || marshal_s(ctx, &val->aname) #if CONFIG_9P_ENABLE_9P2000_u - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && marshal_nuid(ctx, &val->n_uid) ) + || ( is_ver(ctx, 9P2000_u) && marshal_nuid(ctx, &val->n_uid) ) #endif /* CONFIG_9P_ENABLE_9P2000_u */ || ({ encode_u32le(ctx->net_offset - _size_offset, &ctx->net_bytes[_size_offset]); false; }) || ({ encode_u8le(102, &ctx->net_bytes[_typ_offset]); false; }) @@ -1682,7 +1707,7 @@ LM_FLATTEN static bool marshal_Tattach(struct _marshal_ctx *ctx, struct lib9p_ms || marshal_s(ctx, &val->uname) || marshal_s(ctx, &val->aname) #if CONFIG_9P_ENABLE_9P2000_u - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && marshal_nuid(ctx, &val->n_uid) ) + || ( is_ver(ctx, 9P2000_u) && marshal_nuid(ctx, &val->n_uid) ) #endif /* CONFIG_9P_ENABLE_9P2000_u */ || ({ encode_u32le(ctx->net_offset - _size_offset, &ctx->net_bytes[_size_offset]); false; }) || ({ encode_u8le(104, &ctx->net_bytes[_typ_offset]); false; }) @@ -1736,10 +1761,10 @@ LM_ALWAYS_INLINE static bool marshal_stat(struct _marshal_ctx *ctx, struct lib9p || marshal_s(ctx, &val->file_owner_gid) || marshal_s(ctx, &val->file_last_modified_uid) #if CONFIG_9P_ENABLE_9P2000_u - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && marshal_s(ctx, &val->file_extension) ) - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && marshal_nuid(ctx, &val->file_owner_n_uid) ) - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && marshal_nuid(ctx, &val->file_owner_n_gid) ) - || ( (ctx->ctx->version==LIB9P_VER_9P2000_u) && marshal_nuid(ctx, &val->file_last_modified_n_uid) ) + || ( is_ver(ctx, 9P2000_u) && marshal_s(ctx, &val->file_extension) ) + || ( is_ver(ctx, 9P2000_u) && marshal_nuid(ctx, &val->file_owner_n_uid) ) + || ( is_ver(ctx, 9P2000_u) && marshal_nuid(ctx, &val->file_owner_n_gid) ) + || ( is_ver(ctx, 9P2000_u) && marshal_nuid(ctx, &val->file_last_modified_n_uid) ) #endif /* CONFIG_9P_ENABLE_9P2000_u */ || ({ encode_u16le(ctx->net_offset - _kern_type_offset, &ctx->net_bytes[_stat_size_offset]); false; }) ; diff --git a/lib9p/idl.gen b/lib9p/idl.gen index 31f6527..47ca49a 100755 --- a/lib9p/idl.gen +++ b/lib9p/idl.gen @@ -62,7 +62,8 @@ def c_ver_ifdef(versions: set[str]) -> str: def c_ver_cond(versions: set[str]) -> str: if len(versions) == 1: - return f"(ctx->ctx->version=={c_ver_enum(next(v for v in versions))})" + v = next(v for v in versions) + return f"is_ver(ctx, {v.replace('.', '_')})" return "( " + (" || ".join(c_ver_cond({v}) for v in sorted(versions))) + " )" @@ -318,12 +319,32 @@ def gen_c(versions: set[str], typs: list[idl.Type]) -> str: #include "internal.h" """ + # utilities ################################################################ + ret += f""" +/* utilities ******************************************************************/ +""" + def used(arg: str) -> str: return arg def unused(arg: str) -> str: return f"LM_UNUSED({arg})" + ret += "\n" + ret += "/**\n" + ret += f" * is_ver(ctx, ver) is essentially `(ctx->ctx->version == {idprefix.upper()}VER_##ver)`,\n" + ret += f" * but compiles correctly (to `false`) even if `{idprefix.upper()}VER_##ver` isn't defined\n" + ret += " * (because `!CONFIG_9P_ENABLE_##ver`). This is useful when `||`ing\n" + ret += " * several version checks together.\n" + ret += " */\n" + ret += "#define is_ver(ctx, ver) _is_ver_##ver(ctx->ctx->version)\n" + for v in sorted(versions): + ret += f"#if CONFIG_9P_ENABLE_{v.replace('.', '_')}\n" + ret += f"\t#define _is_ver_{v.replace('.', '_')}(v) (v == {c_ver_enum(v)})\n" + ret += "#else\n" + ret += f"\t#define _is_ver_{v.replace('.', '_')}(v) false\n" + ret += "#endif\n" + # strings ################################################################## ret += f""" /* strings ********************************************************************/ |