Skip to content

Commit 75598a7

Browse files
committed
drop partial data, handle missing closing comment
1 parent 26316d3 commit 75598a7

File tree

2 files changed

+149
-16
lines changed

2 files changed

+149
-16
lines changed

tools/mkbuildoptglobals.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,17 @@ def extract_build_opt(name: str, dst: TextIO, src: TextIO):
401401
IN_SKIP_OPT = 3
402402

403403
state = IN_RAW
404+
block = [] # type: List[str]
404405

405406
for n, raw_line in enumerate(src, start=1):
406407
line = raw_line.strip().rstrip()
407408

408409
if state == IN_SKIP_OPT:
409410
if line.startswith("*/"):
410411
state = IN_RAW
412+
for line in block:
413+
dst.write(line)
414+
block = []
411415
continue
412416

413417
if line.startswith("/*@"):
@@ -436,7 +440,13 @@ def extract_build_opt(name: str, dst: TextIO, src: TextIO):
436440
if not line:
437441
continue
438442

439-
dst.write(f"{line}\n")
443+
block.append(f"{line}\n")
444+
445+
if state != IN_RAW:
446+
raise InvalidSyntax(None, n, raw_line)
447+
448+
for line in block:
449+
dst.write(line)
440450

441451

442452
def extract_build_opt_from_path(dst: TextIO, name: str, p: pathlib.Path):

tools/test_mkbuildoptglobals.py

Lines changed: 138 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,113 @@
88
from mkbuildoptglobals import extract_build_opt, InvalidSignature, InvalidSyntax
99
import contextlib
1010

11+
1112
@contextlib.contextmanager
1213
def buffer(init: str):
1314
yield io.StringIO(init), io.StringIO()
1415

16+
1517
class TestExtractBuildOpt(unittest.TestCase):
18+
def testNoSource(self):
19+
src = io.StringIO()
20+
dst = io.StringIO()
21+
22+
extract_build_opt("anything.opt", dst, src)
23+
self.assertFalse(dst.getvalue())
24+
25+
def testSomeSource(self):
26+
src = io.StringIO("12345")
27+
dst = io.StringIO()
28+
29+
extract_build_opt("numbers.opt", dst, src)
30+
self.assertFalse(dst.getvalue())
31+
32+
def testNoOpt(self):
33+
src = io.StringIO(
34+
r"""
35+
#include <cstdio>
36+
int main() {
37+
puts("hello world");
38+
return 0;
39+
}
40+
"""
41+
)
42+
43+
dst = io.StringIO()
44+
45+
extract_build_opt("something.opt", dst, src)
46+
self.assertFalse(dst.getvalue())
47+
48+
def testAfterOpt(self):
49+
src = io.StringIO(
50+
r"""
51+
int main() {
52+
puts("hello world");
53+
return 0;
54+
}
55+
/*@create-file:after.opt@
56+
-fhello-world
57+
*/
58+
"""
59+
)
60+
61+
dst = io.StringIO()
62+
63+
extract_build_opt("after.opt", dst, src)
64+
self.assertEqual("-fhello-world\n", dst.getvalue())
65+
66+
def testEmptyBlock(self):
67+
src = io.StringIO(
68+
r"""
69+
70+
71+
/*@create-file:empty.opt@
72+
*/
73+
74+
"""
75+
)
76+
77+
dst = io.StringIO()
78+
79+
extract_build_opt("empty.opt", dst, src)
80+
self.assertFalse(dst.getvalue())
81+
1682
def testParseOnce(self):
1783
src = io.StringIO(
18-
"""
19-
/*@create-file:build.opt@
84+
r"""
85+
/*@create-file:special.opt@
2086
-fspecial-option
87+
*/
88+
"""
89+
)
90+
91+
dst = io.StringIO()
92+
93+
extract_build_opt("special.opt", dst, src)
94+
self.assertEqual("-fspecial-option\n", dst.getvalue())
95+
96+
def testParseOnceLines(self):
97+
src = io.StringIO(
98+
r"""
99+
/*@create-file:build.opt@
100+
-DFOO="arbitrary definition 1"
101+
#comment
102+
103+
// comment
104+
105+
-DBAR="arbitrary definition 2"
106+
// finalize this
21107
*/
22108
"""
23109
)
24110

25111
dst = io.StringIO()
26112

27113
extract_build_opt("build.opt", dst, src)
28-
self.assertEqual("-fspecial-option\n", dst.getvalue())
114+
self.assertEqual(
115+
'-DFOO="arbitrary definition 1"\n' '-DBAR="arbitrary definition 2"\n',
116+
dst.getvalue(),
117+
)
29118

30119
def testParseSecond(self):
31120
src = io.StringIO(
@@ -54,18 +143,20 @@ def testParseSecond(self):
54143
def testMultiple(self):
55144
src = io.StringIO(
56145
r"""
146+
#include <cstdio>
147+
int foo() { return 111; }
148+
57149
/*@ create-file:foo.opt @
58150
-ffoo
59151
*/
60-
152+
#define INTERMIXED_DATA
61153
/*@create-file:foo.opt:debug@
62154
-fbaz
63155
*/
64156
65157
/*@create-file:bar.opt:debug@
66158
-DUNUSED
67159
*/
68-
69160
/*@create-file:foo.opt@
70161
-mbar
71162
*/
@@ -74,6 +165,7 @@ def testMultiple(self):
74165
-DALSO_UNUSED
75166
*/
76167
168+
77169
"""
78170
)
79171

@@ -107,21 +199,21 @@ def testInvalidSignature(self):
107199
with self.assertRaises(InvalidSignature) as raises:
108200
extract_build_opt("bar.opt", dst, src)
109201

110-
self.assertEqual("", dst.getvalue())
202+
self.assertFalse(dst.getvalue())
111203

112204
e = raises.exception
113205
self.assertFalse(e.file)
114206
self.assertEqual(12, e.lineno)
115207
self.assertEqual("/*@make-file:bar.opt@\n", e.line)
116208

117-
def testPartialDest(self):
209+
def testPartialInvalidSyntax(self):
118210
src = io.StringIO(
119211
r"""
120-
/*@create-file:foo.opt@
212+
/*@create-file:syntax.opt@
121213
-DIMPORTANT_FLAG
122214
-DANOTHER_FLAG=123
123215
*/
124-
/*@ create-file:foo.opt @
216+
/*@ create-file:syntax.opt @
125217
/*@oops
126218
-mthis-fails
127219
*/
@@ -130,29 +222,60 @@ def testPartialDest(self):
130222

131223
dst = io.StringIO()
132224
with self.assertRaises(InvalidSyntax) as raises:
133-
extract_build_opt("foo.opt", dst, src)
225+
extract_build_opt("syntax.opt", dst, src)
226+
227+
self.assertFalse(dst.getvalue())
134228

135229
e = raises.exception
136230
self.assertFalse(e.file)
137231
self.assertEqual(7, e.lineno)
138232
self.assertEqual("/*@oops\n", e.line)
139-
self.assertEqual("-DIMPORTANT_FLAG\n-DANOTHER_FLAG=123\n", dst.getvalue())
233+
234+
def testPartialUnclosed(self):
235+
src = io.StringIO(
236+
r"""
237+
/*@create-file:unclosed.opt@
238+
line 1
239+
line 2
240+
"""
241+
)
242+
dst = io.StringIO()
243+
with self.assertRaises(InvalidSyntax) as raises:
244+
extract_build_opt("unclosed.opt", dst, src)
245+
246+
self.assertFalse(dst.getvalue())
140247

141248
def testParseSignatureSpace(self):
142-
with buffer(r"""
249+
with buffer(
250+
r"""
143251
/*@ create-file:test.opt @
144252
-ftest-test-test
145253
*/
146-
""") as (src, dst):
254+
"""
255+
) as (src, dst):
147256
extract_build_opt("test.opt", dst, src)
148257
self.assertEqual("-ftest-test-test\n", dst.getvalue())
149258

150-
with buffer(r"""
259+
with buffer(
260+
r"""
151261
/*@create-file:test.opt
152262
@
153263
-ftest-test-test
154264
*/
155-
""") as (src, dst):
265+
"""
266+
) as (src, dst):
267+
with self.assertRaises(InvalidSyntax) as raises:
268+
extract_build_opt("test.opt", dst, src)
269+
270+
self.assertFalse(dst.getvalue())
271+
272+
with buffer(
273+
r"""
274+
/*@create-file:test.opt
275+
-ftest-test-test
276+
*/
277+
"""
278+
) as (src, dst):
156279
with self.assertRaises(InvalidSyntax) as raises:
157280
extract_build_opt("test.opt", dst, src)
158281

0 commit comments

Comments
 (0)