Skip to content

Commit 2039398

Browse files
committed
nested comment block as error
1 parent 0c67f33 commit 2039398

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

tools/mkbuildoptglobals.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,13 @@ def filter(self, rec):
176176

177177

178178
class ParsingException(Exception):
179-
def __init__(self, file: Optional[str], lineno: int, line: str):
179+
def __init__(self, file: Optional[str], lineno: int, line: str, start: int = 0):
180180
self.file = file
181181
self.lineno = lineno
182182
self.line = line
183+
if start < 0:
184+
start = 0
185+
self.start = start
183186

184187
def __str__(self):
185188
out = ""
@@ -191,7 +194,9 @@ def __str__(self):
191194
out += f"\n\n{lineno} {self.line}"
192195

193196
out += f'\n{" " * len(lineno)} '
194-
out += "^" * len(self.line.strip())
197+
if self.start:
198+
out += " " * self.start
199+
out += "^" * (len(self.line.strip()) - self.start)
195200

196201
return out
197202

@@ -204,6 +209,10 @@ class InvalidSyntax(ParsingException):
204209
pass
205210

206211

212+
class InvalidComment(ParsingException):
213+
pass
214+
215+
207216
def extract_build_opt(name: str, dst: TextIO, src: TextIO):
208217
"""
209218
Read src line by line and extract matching 'create-file' directives.
@@ -254,12 +263,18 @@ def extract_build_opt(name: str, dst: TextIO, src: TextIO):
254263
state = IN_RAW
255264
continue
256265

257-
if line.startswith(("#", "//", "*")):
266+
if not line:
258267
continue
259268

260-
if not line:
269+
# allow standalone comments on each line
270+
if line.startswith(("#", "//", "*")):
261271
continue
262272

273+
# but not nested ones. this should get caught by -Wcomment, too
274+
for comment in ("/*", "*/"):
275+
if comment in line:
276+
raise InvalidComment(None, n, raw_line, raw_line.index(comment))
277+
263278
block.append(f"{line}\n")
264279

265280
if state != IN_RAW:

tools/test_mkbuildoptglobals.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import unittest
66

77
from typing import TextIO
8-
from mkbuildoptglobals import extract_build_opt, InvalidSignature, InvalidSyntax
8+
from mkbuildoptglobals import extract_build_opt, InvalidSignature, InvalidSyntax, InvalidComment
99
import contextlib
1010

1111

@@ -281,6 +281,29 @@ def testParseSignatureSpace(self):
281281

282282
self.assertFalse(dst.getvalue())
283283

284+
def testCommentBlock(self):
285+
src = io.StringIO(
286+
r"""
287+
/*@create-file:commented.opt@
288+
-ffoo /* wat? */
289+
-fbar
290+
/* oopsie */
291+
*/
292+
line 1
293+
line 2
294+
"""
295+
)
296+
dst = io.StringIO()
297+
with self.assertRaises(InvalidComment) as raises:
298+
extract_build_opt("commented.opt", dst, src)
299+
300+
self.assertFalse(dst.getvalue())
301+
302+
e = raises.exception
303+
self.assertFalse(e.file)
304+
self.assertEqual(3, e.lineno)
305+
self.assertIn("wat?", e.line)
306+
284307

285308
if __name__ == "__main__":
286309
unittest.main()

0 commit comments

Comments
 (0)