Fix comment parsing
Fix cases like
```
/*short comment*/ /*long
comment */
int mbedtls_foo;
```
where the previous code thought that the second line started outside of a
comment and ended inside of a comment.
I believe that the new code strips comments correctly. It also strips string
literals, just in case.
Fixes #5191.
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/tests/scripts/check_names.py b/tests/scripts/check_names.py
index ac2490f..94f4cb1 100755
--- a/tests/scripts/check_names.py
+++ b/tests/scripts/check_names.py
@@ -509,18 +509,19 @@
previous_line = ""
for line_no, line in enumerate(header):
- # Skip parsing this line if a block comment ends on it,
- # but don't skip if it has just started -- there is a chance
- # it ends on the same line.
- if re.search(r"/\*", line):
- in_block_comment = not in_block_comment
- if re.search(r"\*/", line):
- in_block_comment = not in_block_comment
- continue
-
+ # Terminate current comment?
if in_block_comment:
- previous_line = ""
- continue
+ line = re.sub(r".*?\*/", r"", line, 1)
+ in_block_comment = False
+ # Remove full comments and string literals
+ line = re.sub(r'/\*.*?\*/|(")(?:[^\\\"]|\\.)*"',
+ lambda s: '""' if s.group(1) else ' ',
+ line)
+ # Start an unfinished comment?
+ m = re.match(r"/\*", line)
+ if m:
+ in_block_comment = True
+ line = line[:m.end(0)]
if exclusion_lines.search(line):
previous_line = ""