Simplify some regex definitions

Use '|'.join([comma-separated list]) rather than r'...|' r'...|'. This way
there's less risk of forgetting a '|'. Pylint will yell if we forget a comma
between list elements.

Use match rather than search + mandatory start anchor for EXCLUSION_LINES.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-11-17 20:23:18 +01:00
parent e833998b58
commit c8fc67f341

View file

@ -457,32 +457,30 @@ class CodeParser():
return enum_consts return enum_consts
IDENTIFIER_REGEX = re.compile( IDENTIFIER_REGEX = re.compile('|'.join([
# Match " something(a" or " *something(a". Functions. # Match " something(a" or " *something(a". Functions.
# Assumptions: # Assumptions:
# - function definition from return type to one of its arguments is # - function definition from return type to one of its arguments is
# all on one line # all on one line
# - function definition line only contains alphanumeric, asterisk, # - function definition line only contains alphanumeric, asterisk,
# underscore, and open bracket # underscore, and open bracket
r".* \**(\w+) *\( *\w|" r".* \**(\w+) *\( *\w",
# Match "(*something)(". # Match "(*something)(".
r".*\( *\* *(\w+) *\) *\(|" r".*\( *\* *(\w+) *\) *\(",
# Match names of named data structures. # Match names of named data structures.
r"(?:typedef +)?(?:struct|union|enum) +(\w+)(?: *{)?$|" r"(?:typedef +)?(?:struct|union|enum) +(\w+)(?: *{)?$",
# Match names of typedef instances, after closing bracket. # Match names of typedef instances, after closing bracket.
r"}? *(\w+)[;[].*" r"}? *(\w+)[;[].*",
) ]))
# The regex below is indented for clarity. # The regex below is indented for clarity.
EXCLUSION_LINES = re.compile( EXCLUSION_LINES = re.compile("|".join([
r"^(" r"extern +\"C\"",
r"extern +\"C\"|" # pylint: disable=bad-continuation r"(typedef +)?(struct|union|enum)( *{)?$",
r"(typedef +)?(struct|union|enum)( *{)?$|" r"} *;?$",
r"} *;?$|" r"$",
r"$|" r"//",
r"//|" r"#",
r"#" ]))
r")"
)
def parse_identifiers_in_file(self, header_file, identifiers): def parse_identifiers_in_file(self, header_file, identifiers):
""" """
@ -515,7 +513,7 @@ class CodeParser():
in_block_comment = True in_block_comment = True
line = line[:m.end(0)] line = line[:m.end(0)]
if self.EXCLUSION_LINES.search(line): if self.EXCLUSION_LINES.match(line):
previous_line = "" previous_line = ""
continue continue