Skip to content

Commit

Permalink
tools: add ability to wrap wire messages with ifs
Browse files Browse the repository at this point in the history
Makes it possible to hide wire messages behind EXPERIMENTAL_FEATURES
flag.
  • Loading branch information
niftynei authored and rustyrussell committed Oct 10, 2019
1 parent 9e29a47 commit 3f1f075
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tools/gen/header_template
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions tools/gen/impl_template
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions tools/generate-wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def __init__(self, name, number, option=[], enum_prefix='wire',
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
Expand All @@ -369,6 +370,9 @@ def struct_name(self):
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):
Expand Down Expand Up @@ -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()
Expand All @@ -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])

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions tools/test/enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <ccan/short_types/short_types.h>
#include <stdlib.h>

#define TEST_IFDEF 0

enum test_enum {
TEST_ONE,
TEST_TWO,
Expand Down
4 changes: 4 additions & 0 deletions tools/test/test_cases
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3f1f075

Please sign in to comment.