From 3f1f075421a49457857841e5eb61b2db5b8bfa19 Mon Sep 17 00:00:00 2001 From: lisa neigut Date: Fri, 20 Sep 2019 12:49:45 -0500 Subject: [PATCH] tools: add ability to wrap wire messages with ifs Makes it possible to hide wire messages behind EXPERIMENTAL_FEATURES flag. --- tools/gen/header_template | 6 ++++++ tools/gen/impl_template | 6 ++++++ tools/generate-wire.py | 14 ++++++++++++++ tools/test/enum.h | 2 ++ tools/test/test_cases | 4 ++++ 5 files changed, 32 insertions(+) diff --git a/tools/gen/header_template b/tools/gen/header_template index d77dc9d72..b1e492fb7 100644 --- a/tools/gen/header_template +++ b/tools/gen/header_template @@ -85,12 +85,18 @@ void fromwire_${subtype.name}(const u8 **cursor, size_t *plen, ${subtype.type_na % endfor % endif % for msg in messages: + % if msg.if_token: +#if ${msg.if_token} + % endif /* WIRE: ${msg.name.upper()} */ % for c in msg.msg_comments: /* ${c} */ % endfor u8 *towire_${msg.name}(const tal_t *ctx${''.join([f.arg_desc_to() for f in msg.fields.values()])}); bool fromwire_${msg.name}(${'const tal_t *ctx, ' if msg.needs_context() else ''}const void *p${''.join([f.arg_desc_from() for f in msg.fields.values()])}); + % if msg.if_token: +#endif /* ${msg.if_token} */ + % endif % endfor diff --git a/tools/gen/impl_template b/tools/gen/impl_template index 735f97fcb..169f0332d 100644 --- a/tools/gen/impl_template +++ b/tools/gen/impl_template @@ -210,6 +210,9 @@ ${static}const struct tlv_record_type tlvs_${tlv.name}[] = { % endfor ## END TLV's % for msg in messages: ## START Wire Messages + % if msg.if_token: +#if ${msg.if_token} + % endif /* WIRE: ${msg.name.upper()} */ % for c in msg.msg_comments: /*${c} */ @@ -322,4 +325,7 @@ bool fromwire_${msg.name}(${'const tal_t *ctx, ' if msg.needs_context() else ''} % endfor return cursor != NULL; } + % if msg.if_token: +#endif /* ${msg.if_token} */ + % endif % endfor ## END Wire Messages diff --git a/tools/generate-wire.py b/tools/generate-wire.py index 01c4a9574..6883d014f 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -356,6 +356,7 @@ class Message(FieldSet): self.struct_prefix = struct_prefix self.enumname = None self.msg_comments = comments + self.if_token = None def has_option(self): return self.option is not None @@ -369,6 +370,9 @@ class Message(FieldSet): return self.struct_prefix + "_" + self.name return self.name + def add_if(self, if_token): + self.if_token = if_token + class Tlv(object): def __init__(self, name): @@ -518,6 +522,7 @@ def main(options, args=None, output=sys.stdout, lines=None): genline = next_line(args, lines) comment_set = [] + token_name = None # Create a new 'master' that serves as the coordinator for the file generation master = Master() @@ -530,8 +535,12 @@ def main(options, args=None, output=sys.stdout, lines=None): if not bool(line): master.add_comments(comment_set) comment_set = [] + token_name = None continue + if len(tokens) > 2: + token_name = tokens[1] + if token_type == 'subtype': subtype, _, _ = master.add_type(tokens[1]) @@ -632,6 +641,11 @@ def main(options, args=None, output=sys.stdout, lines=None): comment_set = [] elif token_type.startswith('#include'): master.add_include(token_type) + elif token_type.startswith('#if'): + msg = master.find_message(token_name) + if (msg): + if_token = token_type[token_type.index(' ') + 1:] + msg.add_if(if_token) elif token_type.startswith('#'): comment_set.append(token_type[1:]) else: diff --git a/tools/test/enum.h b/tools/test/enum.h index 5867d910d..dee45be09 100644 --- a/tools/test/enum.h +++ b/tools/test/enum.h @@ -3,6 +3,8 @@ #include #include +#define TEST_IFDEF 0 + enum test_enum { TEST_ONE, TEST_TWO, diff --git a/tools/test/test_cases b/tools/test/test_cases index 20eebe644..ef085c85f 100644 --- a/tools/test/test_cases +++ b/tools/test/test_cases @@ -47,6 +47,10 @@ msgdata,test_msg,test_sbt_arrays,subtype_arrays, msgdata,test_msg,extension_1,test_features,,option_short_id msgdata,test_msg,extension_2,test_short_id,,option_one,option_two +msgtype,test_ifdef,100 +#ifdef TEST_IFDEF +msgdata,test_ifdef,is_def,u32, + msgtype,test_tlv1,2 msgdata,test_tlv1,test_struct,test_short_id, msgdata,test_tlv1,tlv,test_n1,