Separate the parsing and generation parts of MacroCollector

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-01-25 21:36:53 +01:00
parent 58fd20c464
commit 7bb85c2640

View file

@ -205,11 +205,8 @@ BIT_TEST_TEMPLATE = '''\
}\
'''
class MacroCollector:
class PSAMacroCollector:
"""Collect PSA crypto macro definitions from C header files.
1. Call `read_file` on the input header file(s).
2. Call `write_file` to write ``psa_constant_names_generated.c``.
"""
def __init__(self):
@ -303,6 +300,13 @@ class MacroCollector:
line = re.sub(self._nonascii_re, rb'', line).decode('ascii')
self.read_line(line)
class CaseBuilder(PSAMacroCollector):
"""Collect PSA crypto macro definitions and write value recognition functions.
1. Call `read_file` on the input header file(s).
2. Call `write_file` to write ``psa_constant_names_generated.c``.
"""
@staticmethod
def _make_return_case(name):
return 'case %(name)s: return "%(name)s";' % {'name': name}
@ -404,7 +408,7 @@ class MacroCollector:
output_file.write(OUTPUT_TEMPLATE % data)
def generate_psa_constants(header_file_names, output_file_name):
collector = MacroCollector()
collector = CaseBuilder()
for header_file_name in header_file_names:
with open(header_file_name, 'rb') as header_file:
collector.read_file(header_file)