Skip to content

Commit

Permalink
bolt-gen: handle enums
Browse files Browse the repository at this point in the history
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,
  • Loading branch information
niftynei authored and rustyrussell committed Jul 24, 2019
1 parent cfd56d8 commit b30d7d2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
9 changes: 6 additions & 3 deletions tools/generate-bolts.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,16 @@ def true_type(type_name, field_name=None):

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,
Expand Down
5 changes: 4 additions & 1 deletion tools/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@
# 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)
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)

$(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 > $@
Expand Down
18 changes: 18 additions & 0 deletions tools/test/enum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "enum.h"
#include <stdio.h>

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);
}
15 changes: 15 additions & 0 deletions tools/test/enum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef LIGHTNING_TOOLS_TEST_ENUM_H
#define LIGHTNING_TOOLS_TEST_ENUM_H
#include <ccan/short_types/short_types.h>
#include <stdlib.h>

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 */
5 changes: 3 additions & 2 deletions tools/test/test_cases
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <ccan/short_types/short_types.h>
# TODO: add enums!
# TODO: msgs expansions
#include "enum.h"
# AUTOGENERATED MOCKS START
# AUTOGENERATED MOCKS END

Expand All @@ -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
Expand Down

0 comments on commit b30d7d2

Please sign in to comment.