Exclusively use re.search() to avoid confusion with .match()

Also fix newline being removed when lines were concatenated

Signed-off-by: Yuto Takano <yuto.takano@arm.com>
This commit is contained in:
Yuto Takano 2021-08-16 11:34:10 +01:00
parent 9d9c6dc46e
commit 90bc026913

View File

@ -407,16 +407,16 @@ class CodeParser():
# Match typedefs and brackets only when they are at the # Match typedefs and brackets only when they are at the
# beginning of the line -- if they are indented, they might # beginning of the line -- if they are indented, they might
# be sub-structures within structs, etc. # be sub-structures within structs, etc.
if state == 0 and re.match(r"^(typedef +)?enum +{", line): if state == 0 and re.search(r"^(typedef +)?enum +{", line):
state = 1 state = 1
elif state == 0 and re.match(r"^(typedef +)?enum", line): elif state == 0 and re.search(r"^(typedef +)?enum", line):
state = 2 state = 2
elif state == 2 and re.match(r"^{", line): elif state == 2 and re.search(r"^{", line):
state = 1 state = 1
elif state == 1 and re.match(r"^}", line): elif state == 1 and re.search(r"^}", line):
state = 0 state = 0
elif state == 1 and not re.match(r" *#", line): elif state == 1 and not re.search(r"^ *#", line):
enum_const = re.match(r" *(?P<enum_const>\w+)", line) enum_const = re.search(r"^ *(?P<enum_const>\w+)", line)
if not enum_const: if not enum_const:
continue continue
@ -433,8 +433,6 @@ class CodeParser():
Parse all lines of a header where a function/enum/struct/union/typedef Parse all lines of a header where a function/enum/struct/union/typedef
identifier is declared, based on some heuristics. Highly dependent on identifier is declared, based on some heuristics. Highly dependent on
formatting style. formatting style.
Note: .match() checks at the beginning of the string (implicit ^), while
.search() checks throughout.
Args: Args:
* include: A List of glob expressions to look for files through. * include: A List of glob expressions to look for files through.
@ -459,12 +457,12 @@ class CodeParser():
) )
exclusion_lines = re.compile( exclusion_lines = re.compile(
r"^(" r"^("
r"extern +\"C\"|" r"extern +\"C\"|"
r"(typedef +)?(struct|union|enum)( *{)?$|" r"(typedef +)?(struct|union|enum)( *{)?$|"
r"} *;?$|" r"} *;?$|"
r"$|" r"$|"
r"//|" r"//|"
r"#" r"#"
r")" r")"
) )
@ -493,7 +491,7 @@ class CodeParser():
previous_line = "" previous_line = ""
continue continue
if exclusion_lines.match(line): if exclusion_lines.search(line):
previous_line = "" previous_line = ""
continue continue
@ -501,14 +499,14 @@ class CodeParser():
# characters (or underscore, asterisk, or, open bracket), # characters (or underscore, asterisk, or, open bracket),
# and nothing else, high chance it's a declaration that # and nothing else, high chance it's a declaration that
# continues on the next line # continues on the next line
if re.match(r"^([\w\*\(]+\s+)+$", line): if re.search(r"^([\w\*\(]+\s+)+$", line):
previous_line += line previous_line += line
continue continue
# If previous line seemed to start an unfinished declaration # If previous line seemed to start an unfinished declaration
# (as above), concat and treat them as one. # (as above), concat and treat them as one.
if previous_line: if previous_line:
line = previous_line.strip() + " " + line.strip() line = previous_line.strip() + " " + line.strip() + "\n"
previous_line = "" previous_line = ""
# Skip parsing if line has a space in front = heuristic to # Skip parsing if line has a space in front = heuristic to
@ -626,8 +624,8 @@ class CodeParser():
).stdout ).stdout
for line in nm_output.splitlines(): for line in nm_output.splitlines():
if not nm_undefined_regex.match(line): if not nm_undefined_regex.search(line):
symbol = nm_valid_regex.match(line) symbol = nm_valid_regex.search(line)
if (symbol and not symbol.group("symbol").startswith(exclusions)): if (symbol and not symbol.group("symbol").startswith(exclusions)):
symbols.append(symbol.group("symbol")) symbols.append(symbol.group("symbol"))
else: else:
@ -718,10 +716,10 @@ class NameChecker():
problems = [] problems = []
for item_match in self.parse_result[group_to_check]: for item_match in self.parse_result[group_to_check]:
if not re.match(check_pattern, item_match.name): if not re.search(check_pattern, item_match.name):
problems.append(PatternMismatch(check_pattern, item_match)) problems.append(PatternMismatch(check_pattern, item_match))
# Double underscore is a reserved identifier, never to be used # Double underscore should not be used for names
if re.match(r".*__.*", item_match.name): if re.search(r".*__.*", item_match.name):
problems.append(PatternMismatch("double underscore", item_match)) problems.append(PatternMismatch("double underscore", item_match))
self.output_check_result( self.output_check_result(