From f30d4d9b34420f577852f90b4e0b4e85783efe67 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 17 May 2019 12:05:19 +0200 Subject: [PATCH] More accurate parsing of #define directives Support continuation lines and remove comments. --- scripts/generate_psa_constants.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/generate_psa_constants.py b/scripts/generate_psa_constants.py index 91d0b29d6..a3cd130a0 100755 --- a/scripts/generate_psa_constants.py +++ b/scripts/generate_psa_constants.py @@ -210,7 +210,7 @@ class MacroCollector: # and the expansion in group 3. _define_directive_re = re.compile(r'\s*#\s*define\s+(\w+)' + r'(?:\s+|\((\w+)\)\s*)' + - r'(.+)(?:/[*/])?') + r'(.+)') def read_line(self, line): """Parse a C header line and record the PSA identifier it defines if any. @@ -222,6 +222,7 @@ class MacroCollector: if not m: return name, parameter, expansion = m.groups() + expansion = re.sub(r'/\*.*?\*/|//.*', r' ', expansion) if name.endswith('_FLAG') or name.endswith('MASK'): # Macro only to build actual values return @@ -274,6 +275,9 @@ class MacroCollector: def read_file(self, header_file): for line in header_file: + while line.endswith('\\\n'): + cont = next(header_file) + line = line[:-2] + cont self.read_line(line) @staticmethod