From b30d7d26eac28ebedcf861b2086c397cf24a91cc Mon Sep 17 00:00:00 2001 From: lisa neigut Date: Tue, 23 Jul 2019 15:53:44 -0500 Subject: [PATCH] bolt-gen: handle enums Add parsing know-how for enum fields. This is necessary for internally defined wire generators. Enums are denoted by prefixing the field with an `e:`. Ex: msgdata,msg_name,field_name,e:enum_type, --- tools/generate-bolts.py | 9 ++++++--- tools/test/Makefile | 5 ++++- tools/test/enum.c | 18 ++++++++++++++++++ tools/test/enum.h | 15 +++++++++++++++ tools/test/test_cases | 5 +++-- 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 tools/test/enum.c create mode 100644 tools/test/enum.h diff --git a/tools/generate-bolts.py b/tools/generate-bolts.py index 096f1d0ba..b0495bcc2 100755 --- a/tools/generate-bolts.py +++ b/tools/generate-bolts.py @@ -238,13 +238,16 @@ class Type(FieldSet): def __init__(self, name): FieldSet.__init__(self) - self.name = name + self.name, self.is_enum = self.parse_name(name) self.depends_on = {} - # FIXME: internal msgs can be enums - self.is_enum = False self.type_comments = [] self.tlv = False + def parse_name(self, name): + if name.startswith('e:'): + return name[2:], True + return name, False + def add_data_field(self, field_name, type_obj, count=1, is_extension=[], comments=[], optional=False): FieldSet.add_data_field(self, field_name, type_obj, count, diff --git a/tools/test/Makefile b/tools/test/Makefile index c54095eef..f02f07fad 100644 --- a/tools/test/Makefile +++ b/tools/test/Makefile @@ -7,6 +7,7 @@ # and run a test case. check-units: check-tools +TOOL_TEST_INCL_SRC := tools/test/enum.c TOOL_GEN_SRC := tools/test/gen_test.c tools/test/gen_print.c TOOL_GEN_HEADER := tools/test/gen_test.h tools/test/gen_print.h TOOL_TEST_SRC := $(wildcard tools/test/run-*.c) @@ -14,7 +15,8 @@ TOOL_TEST_OBJS := $(TOOL_TEST_SRC:.c=.o) TOOL_TEST_PROGRAMS := $(TOOL_TEST_OBJS:.o=) TOOL_TEST_COMMON_OBJS := \ - common/utils.o + common/utils.o \ + tools/test/enum.o TOOLS_WIRE_DEPS := $(BOLT_DEPS) tools/test/test_cases $(wildcard tools/gen/*_template) @@ -22,6 +24,7 @@ $(TOOL_TEST_SRC) $(TOOL_GEN_SRC): $(TOOL_GEN_HEADER) $(TOOL_TEST_OBJS): $(TOOL_GEN_SRC) $(TOOL_TEST_PROGRAMS): $(TOOL_TEST_COMMON_OBJS) $(TOOL_GEN_SRC:.c=.o) $(TOOL_GEN_SRC) $(TOOL_GEN_HEADER): $(TOOLS_WIRE_DEPS) +$(TOOL_GEN_SRC:.c=.o): $(TOOL_TEST_INCL_SRC:.c=.o) tools/test/gen_test.h: tools/generate-bolts.py --page header $@ test_type < tools/test/test_cases > $@ diff --git a/tools/test/enum.c b/tools/test/enum.c new file mode 100644 index 000000000..67cef3c22 --- /dev/null +++ b/tools/test/enum.c @@ -0,0 +1,18 @@ +#include "enum.h" +#include + +void towire_test_enum(u8 **pptr, const enum test_enum test_enum) +{ + printf("this would have been the towire for enum %u\n", test_enum); +} + +enum test_enum fromwire_test_enum(const u8 **cursor, size_t *max) +{ + printf("fromwire_test_enum at %ld\n", *max); + return TEST_ONE; +} + +void printwire_test_enum(const char *fieldname, const enum test_enum *test_enum) +{ + printf("%u\n", *test_enum); +} diff --git a/tools/test/enum.h b/tools/test/enum.h new file mode 100644 index 000000000..5867d910d --- /dev/null +++ b/tools/test/enum.h @@ -0,0 +1,15 @@ +#ifndef LIGHTNING_TOOLS_TEST_ENUM_H +#define LIGHTNING_TOOLS_TEST_ENUM_H +#include +#include + +enum test_enum { + TEST_ONE, + TEST_TWO, +}; + +void towire_test_enum(u8 **pptr, const enum test_enum test_enum); +enum test_enum fromwire_test_enum(const u8 **cursor, size_t *max); +void printwire_test_enum(const char *fieldname, const enum test_enum *test_enum); + +#endif /* LIGHTNING_TOOLS_TEST_ENUM_H */ diff --git a/tools/test/test_cases b/tools/test/test_cases index eb87c3c1b..212900e34 100644 --- a/tools/test/test_cases +++ b/tools/test/test_cases @@ -1,6 +1,5 @@ #include -# TODO: add enums! -# TODO: msgs expansions +#include "enum.h" # AUTOGENERATED MOCKS START # AUTOGENERATED MOCKS END @@ -26,6 +25,8 @@ msgdata,test_msg,len_varsize_struct,u16, msgdata,test_msg,test_varsize_struct_varlen,test_features,len_varsize_struct # assignable msgdata,test_msg,test_assignable,u16, +# enum +msgdata,test_msg,test_enum,e:test_enum, # test struct msgdata,test_msg,test_struct,test_short_id, # test var-size struct