Fix several bugs with multiline comments

Empty the current line if it's entirely inside a comment.

Don't incorrectly end a block comment at the second line if it doesn't
contain `*/`.

Recognize `/*` to start a multiline comment even if it isn't at the start of
the line.

When stripping off comments, consistently strip off `/*` and `*/`.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-11-17 20:45:39 +01:00
parent 44801627d2
commit 23b4096ecf

View File

@ -478,10 +478,15 @@ class CodeParser():
* in_block_comment indicates whether the line ends inside a block * in_block_comment indicates whether the line ends inside a block
comment that continues on the next line. comment that continues on the next line.
""" """
# Terminate current comment?
# Terminate current multiline comment?
if in_block_comment: if in_block_comment:
line = re.sub(r".*?\*/", r"", line, 1) m = re.search(r"\*/", line)
in_block_comment = False if m:
in_block_comment = False
line = line[m.end(0):]
else:
return '', True
# Remove full comments and string literals. # Remove full comments and string literals.
# Do it all together to handle cases like "/*" correctly. # Do it all together to handle cases like "/*" correctly.
@ -492,10 +497,10 @@ class CodeParser():
# Start an unfinished comment? # Start an unfinished comment?
# (If `/*` was part of a complete comment, it's already been removed.) # (If `/*` was part of a complete comment, it's already been removed.)
m = re.match(r"/\*", line) m = re.search(r"/\*", line)
if m: if m:
in_block_comment = True in_block_comment = True
line = line[:m.end(0)] line = line[:m.start(0)]
return line, in_block_comment return line, in_block_comment