mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
tlv: add tlv fields to enum declarations in implementation file
'.c' wire format files include case statements to print the names of enums. Include such methods for the enums pertaining to tlv's as well.
This commit is contained in:
parent
d51ad50032
commit
d9904c95ab
@ -248,6 +248,31 @@ towire_impl_templ = """u8 *towire_{name}(const tal_t *ctx{args})
|
||||
}}
|
||||
"""
|
||||
|
||||
towire_tlv_templ = """u8 *towire_{name}(const tal_t *ctx{args})
|
||||
{{
|
||||
{field_decls}
|
||||
\tu8 *p = tal_arr(ctx, u8, 0);
|
||||
\ttowire_u16(&p, {enumname});
|
||||
\ttowire_u16(&p, {len});
|
||||
{subcalls}
|
||||
|
||||
\treturn memcheck(p, tal_count(p));
|
||||
}}
|
||||
"""
|
||||
|
||||
fromwire_tlv_templ = """bool frowire_{name}({ctx}const void *p{args})
|
||||
{{
|
||||
{fields}
|
||||
\tconst u8 *cursor = p;
|
||||
\tsize_t plen = tal_count(p);
|
||||
|
||||
\tif (frmwire_u16(&cursor, &plen) != {enum.name})
|
||||
\t\treturn false;
|
||||
{subcalls}
|
||||
\treturn cursor != NULL;
|
||||
}}
|
||||
"""
|
||||
|
||||
printwire_header_templ = """void printwire_{name}(const char *fieldname, const u8 *cursor);
|
||||
"""
|
||||
printwire_impl_templ = """void printwire_{name}(const char *fieldname, const u8 *cursor)
|
||||
@ -739,7 +764,7 @@ for line in fileinput.input(options.files):
|
||||
comments = []
|
||||
|
||||
|
||||
def construct_enums(msgs):
|
||||
def construct_hdr_enums(msgs):
|
||||
enums = ""
|
||||
for m in msgs:
|
||||
for c in m.comments:
|
||||
@ -748,19 +773,39 @@ def construct_enums(msgs):
|
||||
return enums
|
||||
|
||||
|
||||
def construct_impl_enums(msgs):
|
||||
return '\n\t'.join(['case {enum.name}: return "{enum.name}";'.format(enum=m.enum) for m in msgs])
|
||||
|
||||
|
||||
def enum_header(enums, enumname):
|
||||
return enum_header_template.format(
|
||||
return format_enums(enum_header_template, enums, enumname)
|
||||
|
||||
|
||||
def enum_impl(enums, enumname):
|
||||
return format_enums(enum_impl_template, enums, enumname)
|
||||
|
||||
|
||||
def format_enums(template, enums, enumname):
|
||||
return template.format(
|
||||
enums=enums,
|
||||
enumname=enumname)
|
||||
|
||||
|
||||
def build_enums(toplevel_enumname, toplevel_enums, tlv_fields):
|
||||
def build_hdr_enums(toplevel_enumname, messages, tlv_fields):
|
||||
enum_set = ""
|
||||
enum_set += enum_header(toplevel_enums, toplevel_enumname)
|
||||
enum_set += enum_header(construct_hdr_enums(messages), toplevel_enumname)
|
||||
for field_name, messages in tlv_fields.items():
|
||||
enum_set += "\n"
|
||||
enums = construct_enums(messages)
|
||||
enum_set += enum_header(enums, field_name + '_type')
|
||||
enum_set += enum_header(construct_hdr_enums(messages), field_name + '_type')
|
||||
return enum_set
|
||||
|
||||
|
||||
def build_impl_enums(toplevel_enumname, messages, tlv_fields):
|
||||
enum_set = ""
|
||||
enum_set += enum_impl(construct_impl_enums(messages), toplevel_enumname)
|
||||
for field_name, messages in tlv_fields.items():
|
||||
enum_set += "\n"
|
||||
enum_set += enum_impl(construct_impl_enums(messages), field_name + '_type')
|
||||
return enum_set
|
||||
|
||||
|
||||
@ -770,6 +815,20 @@ enum_header_template = """enum {enumname} {{
|
||||
const char *{enumname}_name(int e);
|
||||
"""
|
||||
|
||||
enum_impl_template = """
|
||||
const char *{enumname}_name(int e)
|
||||
{{
|
||||
\tstatic char invalidbuf[sizeof("INVALID ") + STR_MAX_CHARS(e)];
|
||||
|
||||
\tswitch ((enum {enumname})e) {{
|
||||
\t{enums}
|
||||
\t}}
|
||||
|
||||
\tsnprintf(invalidbuf, sizeof(invalidbuf), "INVALID %i", e);
|
||||
\treturn invalidbuf;
|
||||
}}
|
||||
"""
|
||||
|
||||
header_template = """/* This file was generated by generate-wire.py */
|
||||
/* Do not modify this file! Modify the _csv file it was generated from. */
|
||||
#ifndef LIGHTNING_{idem}
|
||||
@ -777,7 +836,7 @@ header_template = """/* This file was generated by generate-wire.py */
|
||||
#include <ccan/tal/tal.h>
|
||||
#include <wire/wire.h>
|
||||
{includes}
|
||||
{formatted_enums}
|
||||
{formatted_hdr_enums}
|
||||
{func_decls}
|
||||
#endif /* LIGHTNING_{idem} */
|
||||
"""
|
||||
@ -788,19 +847,7 @@ impl_template = """/* This file was generated by generate-wire.py */
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const char *{enumname}_name(int e)
|
||||
{{
|
||||
\tstatic char invalidbuf[sizeof("INVALID ") + STR_MAX_CHARS(e)];
|
||||
|
||||
\tswitch ((enum {enumname})e) {{
|
||||
\t{cases}
|
||||
\t}}
|
||||
|
||||
\tsnprintf(invalidbuf, sizeof(invalidbuf), "INVALID %i", e);
|
||||
\treturn invalidbuf;
|
||||
}}
|
||||
|
||||
{formatted_impl_enums}
|
||||
{func_decls}
|
||||
"""
|
||||
|
||||
@ -850,10 +897,9 @@ else:
|
||||
template = impl_template
|
||||
|
||||
# Dump out enum, sorted by value order.
|
||||
enums = construct_enums(messages)
|
||||
built_enums = build_enums(options.enumname, enums, tlv_fields)
|
||||
built_hdr_enums = build_hdr_enums(options.enumname, messages, tlv_fields)
|
||||
built_impl_enums = build_impl_enums(options.enumname, messages, tlv_fields)
|
||||
includes = '\n'.join(includes)
|
||||
cases = ['case {enum.name}: return "{enum.name}";'.format(enum=m.enum) for m in messages]
|
||||
printcases = ['case {enum.name}: printf("{enum.name}:\\n"); printwire_{name}("{name}", msg); return;'.format(enum=m.enum, name=m.name) for m in messages]
|
||||
|
||||
if options.printwire:
|
||||
@ -865,11 +911,10 @@ else:
|
||||
|
||||
print(template.format(
|
||||
headerfilename=options.headerfilename,
|
||||
cases='\n\t'.join(cases),
|
||||
printcases='\n\t'.join(printcases),
|
||||
idem=idem,
|
||||
includes=includes,
|
||||
enumname=options.enumname,
|
||||
enums=enums,
|
||||
formatted_enums=built_enums,
|
||||
formatted_hdr_enums=built_hdr_enums,
|
||||
formatted_impl_enums=built_impl_enums,
|
||||
func_decls='\n'.join(decls)))
|
||||
|
Loading…
Reference in New Issue
Block a user