pineforge-codegen 1.6.1__py3-none-any.whl → 1.6.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -345,6 +345,25 @@ INLINE_HISTORY_KINDS = (
345
345
  )
346
346
 
347
347
 
348
+ # Symbols the generated-C++ allowlist (``cpp_allowlist.py``) refuses when
349
+ # they appear bare. They are legal Pine identifiers (``free`` as in free
350
+ # margin, ``system``/``socket`` as ordinary words), so a script declaration
351
+ # of one must not reach the generated C++ as a bare libc name: the checker
352
+ # would refuse the TU, and the member would shadow the callee. Reserving
353
+ # them makes ``_safe_name`` emit ``pf_safe_<name>`` instead.
354
+ LIBC_RESERVED_NAMES = frozenset("""
355
+ __asm __asm__ _Pragma
356
+ system popen pclose fork vfork posix_spawn posix_spawnp syscall
357
+ mmap mprotect ptrace
358
+ exec execl execle execlp execv execve execvp execvpe execveat
359
+ dlopen dlsym dlclose
360
+ fopen freopen fdopen open openat
361
+ socket connect bind listen accept getaddrinfo
362
+ getenv setenv unsetenv tzset putenv secure_getenv __secure_getenv
363
+ filesystem fstream ifstream ofstream
364
+ malloc calloc realloc free memcpy memmove memset
365
+ """.split())
366
+
348
367
  # HOST_MEMBER_NAMES (codegen/host_members.py, derived by
349
368
  # scripts/gen_host_members.py from the emitter and the host header): the host
350
369
  # members the generated class reads or writes unqualified. A script identifier
@@ -352,7 +371,8 @@ INLINE_HISTORY_KINDS = (
352
371
  # close > open`` became the value of ``session.isfirstbar``).
353
372
  CPP_RESERVED = set(
354
373
  LEGACY_CPP_RESERVED | CPP_KEYWORDS | CPP_CONTEXTUAL |
355
- CPP_STANDARD_MACROS | CPP_EMITTER_NAMES | HOST_MEMBER_NAMES
374
+ CPP_STANDARD_MACROS | CPP_EMITTER_NAMES | HOST_MEMBER_NAMES |
375
+ LIBC_RESERVED_NAMES
356
376
  )
357
377
 
358
378
  # Temporaries the emitter declares in the C++ it generates around a user's
@@ -0,0 +1,804 @@
1
+ """Allowlist checker for the C++ codegen emits.
2
+
3
+ ``check_generated_cpp(text)`` tokenizes a generated translation unit and
4
+ returns a :class:`Finding` for every construct the generator may not emit.
5
+ The scan works on tokens -- comments, ordinary and raw string literals (and
6
+ their delimiters), character literals, identifiers, preprocessor lines,
7
+ digraphs, trigraphs, line splices and NUL -- never on raw text, so text a
8
+ Pine script lands inside a literal or a comment is inert.
9
+
10
+ The allowed ``#include`` set is read from ``codegen/emit_top.py`` at import
11
+ time; the allowed ``#define`` lines and fixed ``#error`` texts are read from
12
+ ``codegen/emit_top.py`` and ``codegen/run_stops.py``. They therefore cannot
13
+ silently drift from the emitter.
14
+
15
+ Two generated constructs are allowed by exact shape (see ASSESSMENT.md
16
+ section 1f): the ``timestamp(...)`` and time-field timezone lambdas in
17
+ ``codegen/visit_call.py`` and ``codegen/tables.py``. Their environment
18
+ calls (``getenv``/``setenv``/``unsetenv``/``tzset``) are accepted only when
19
+ the whole fixed statement run matches the emitted template token for token.
20
+ Every other environment access is refused.
21
+
22
+ CLI: ``pf-cpp-allowlist [FILE ...]`` reads standard input when no file is
23
+ named and exits 1 when any finding is reported.
24
+ """
25
+
26
+ from __future__ import annotations
27
+
28
+ import argparse
29
+ import ast
30
+ import re
31
+ import sys
32
+ from dataclasses import dataclass
33
+ from pathlib import Path
34
+
35
+ _PACKAGE_DIR = Path(__file__).resolve().parent
36
+ _EMIT_TOP = _PACKAGE_DIR / "codegen" / "emit_top.py"
37
+ _RUN_STOPS = _PACKAGE_DIR / "codegen" / "run_stops.py"
38
+
39
+ #: Reasons are fixed per rule: no token, file name or script text is ever
40
+ #: interpolated into a finding's message (ASSESSMENT.md section 3b).
41
+ REASONS: dict[str, str] = {
42
+ "asm": "inline assembly is not allowed in generated C++",
43
+ "include": "#include is not in the generated-C++ include allowlist",
44
+ "pragma": "preprocessor pragmas are not allowed in generated C++",
45
+ "define": "#define is outside codegen's fixed emitted macro set",
46
+ "undef": "#undef is outside codegen's fixed emitted macro set",
47
+ "directive": "preprocessor directive is not part of codegen's fixed emitted set",
48
+ "extern-c": "extern is only allowed as the generated C export block",
49
+ "process": "process or system-call primitive is not allowed in generated C++",
50
+ "dlopen": "dynamic loading is not allowed in generated C++",
51
+ "file-io": "file I/O is not allowed in generated C++",
52
+ "filesystem-stream": "standard file or filesystem streams are not allowed in generated C++",
53
+ "socket": "socket primitives are not allowed in generated C++",
54
+ "env": "environment access is not allowed outside the sealed timezone templates",
55
+ "function-pointer": "a function pointer to a libc function is not allowed in generated C++",
56
+ "reinterpret-cast": "reinterpret_cast is not allowed in generated C++",
57
+ "trigraph": "trigraphs are not allowed in generated C++",
58
+ "digraph": "digraph tokens are not allowed in generated C++",
59
+ "line-splice": "line splices are not allowed in generated C++",
60
+ "nul": "NUL bytes are not allowed in generated C++",
61
+ "escape": "a backslash outside a literal, comment or directive is not allowed",
62
+ "non-ascii-identifier": "identifiers must be ASCII",
63
+ "numeric-udl": "user-defined numeric literals are not allowed in generated C++",
64
+ "numeric-separator": "digit separators are not allowed in generated C++",
65
+ "raw-string": "raw string literals are not allowed in generated C++",
66
+ "malformed-literal": "string or character literal is not terminated",
67
+ }
68
+
69
+
70
+ @dataclass(frozen=True)
71
+ class Finding:
72
+ """One refused construct. ``line`` and ``column`` are 1-based."""
73
+
74
+ line: int
75
+ column: int
76
+ rule: str
77
+ reason: str
78
+
79
+
80
+ def _normalise_pp(line: str) -> str:
81
+ return " ".join(line.strip().split())
82
+
83
+
84
+ def _string_constants(path: Path) -> list[tuple[str, int]]:
85
+ """String constants of a Python source file with their first line."""
86
+ try:
87
+ tree = ast.parse(path.read_text(encoding="utf-8"))
88
+ except (OSError, SyntaxError):
89
+ return []
90
+ return [
91
+ (node.value, node.lineno)
92
+ for node in ast.walk(tree)
93
+ if isinstance(node, ast.Constant) and isinstance(node.value, str)
94
+ ]
95
+
96
+
97
+ def _directive_lines(path: Path) -> list[tuple[str, int]]:
98
+ """Normalized preprocessor lines spelled by one emitter module."""
99
+ out: list[tuple[str, int]] = []
100
+ for value, lineno in _string_constants(path):
101
+ for line in value.splitlines():
102
+ stripped = line.strip()
103
+ if stripped.startswith("#"):
104
+ out.append((_normalise_pp(stripped), lineno))
105
+ return out
106
+
107
+
108
+ _INCLUDE_DIRECTIVE_RE = re.compile(r"^#\s*include\s*<([^>]+)>$")
109
+ _DEFINE_NAME_RE = re.compile(r"^#\s*define\s+([A-Za-z_][A-Za-z0-9_]*)")
110
+
111
+
112
+ def _load_policy() -> tuple[
113
+ frozenset[str], frozenset[str], frozenset[str], frozenset[str]
114
+ ]:
115
+ emit = _directive_lines(_EMIT_TOP)
116
+ stops = _directive_lines(_RUN_STOPS)
117
+
118
+ includes: set[str] = set()
119
+ for line, _lineno in emit:
120
+ match = _INCLUDE_DIRECTIVE_RE.match(line)
121
+ if match:
122
+ includes.add(match.group(1))
123
+
124
+ define_lines: set[str] = set()
125
+ for line, _lineno in emit + stops:
126
+ if line.startswith("#define "):
127
+ define_lines.add(line)
128
+ define_names: set[str] = set()
129
+ for line in define_lines:
130
+ match = _DEFINE_NAME_RE.match(line)
131
+ if match:
132
+ define_names.add(match.group(1))
133
+
134
+ error_lines = frozenset(
135
+ line for line, _lineno in emit if line.startswith("#error ")
136
+ )
137
+ return (
138
+ frozenset(includes),
139
+ frozenset(define_lines),
140
+ frozenset(define_names),
141
+ error_lines,
142
+ )
143
+
144
+
145
+ (
146
+ ALLOWED_INCLUDES,
147
+ _ALLOWED_DEFINE_LINES,
148
+ _ALLOWED_DEFINE_NAMES,
149
+ _ALLOWED_ERROR_LINES,
150
+ ) = _load_policy()
151
+
152
+ # Handled as fixed shapes, never by a guessed allowlist.
153
+ _ALLOWED_CONTROL_DIRECTIVES = frozenset(
154
+ {"if", "ifdef", "ifndef", "elif", "else", "endif"}
155
+ )
156
+
157
+ # Identifier-level refusals. A member name (``bar.open``) is never a libc
158
+ # call, so member accesses are exempt below.
159
+ _ASM_NAMES = frozenset({"asm", "__asm", "__asm__"})
160
+ _PROCESS_NAMES = frozenset(
161
+ {
162
+ "system", "popen", "pclose", "fork", "vfork", "posix_spawn",
163
+ "posix_spawnp", "syscall", "mmap", "mprotect", "ptrace",
164
+ }
165
+ )
166
+ _EXEC_NAMES = frozenset(
167
+ {
168
+ "exec", "execl", "execle", "execlp", "execv", "execve", "execvp",
169
+ "execvpe", "execveat",
170
+ }
171
+ )
172
+ _DL_NAMES = frozenset({"dlopen", "dlsym", "dlclose"})
173
+ _FILE_IO_NAMES = frozenset(
174
+ {"fopen", "freopen", "fdopen", "open", "openat"}
175
+ )
176
+ _SOCKET_NAMES = frozenset(
177
+ {"socket", "connect", "bind", "listen", "accept", "getaddrinfo"}
178
+ )
179
+ _ENV_NAMES = frozenset(
180
+ {
181
+ "getenv", "setenv", "unsetenv", "tzset", "putenv",
182
+ "secure_getenv", "__secure_getenv",
183
+ }
184
+ )
185
+ _STREAM_NAMES = frozenset(
186
+ {"filesystem", "fstream", "ifstream", "ofstream"}
187
+ )
188
+
189
+ # Taking the address of one of these is a function pointer to libc even when
190
+ # the bare name alone is not otherwise refused (``&malloc``).
191
+ _LIBC_ADDRESS_NAMES = frozenset(
192
+ {
193
+ "malloc", "calloc", "realloc", "free", "memcpy", "memmove", "memset",
194
+ "tzset", "getenv", "setenv", "unsetenv", "putenv", "secure_getenv",
195
+ "__secure_getenv", "system",
196
+ "popen", "pclose", "fork", "vfork", "posix_spawn", "posix_spawnp",
197
+ "exec", "execl", "execle", "execlp", "execv", "execve", "execvp",
198
+ "execvpe", "execveat", "dlopen", "dlsym", "dlclose", "syscall",
199
+ "socket", "connect", "bind", "listen", "accept", "getaddrinfo",
200
+ "open", "openat", "fopen", "freopen", "fdopen", "fclose", "mmap",
201
+ "mprotect", "ptrace",
202
+ }
203
+ )
204
+
205
+ # Bare occurrences of the memory primitives are a function pointer through
206
+ # C++'s function-to-pointer decay (``auto p = malloc;``); every other libc
207
+ # name above is already refused wherever it appears by its own rule.
208
+ _LIBC_DECAY_NAMES = frozenset(
209
+ {"malloc", "calloc", "realloc", "free", "memcpy", "memmove", "memset"}
210
+ )
211
+
212
+ # The two sealed timezone statement runs (ASSESSMENT.md section 1f). They
213
+ # start at the non-UTC ``else`` branch and are matched token for token
214
+ # against the strings in ``codegen/tables.py`` (tz_time_field_lambda) and
215
+ # ``codegen/visit_call.py`` (_timestamp_calendar_lambda). Only identifiers
216
+ # inside a matched run may name the environment calls.
217
+ _SEALED_TZ_FIELD_RUN = (
218
+ "else", "{", "static", "std", ":", ":", "mutex", "_pf_tz_mu", ";",
219
+ "std", ":", ":", "lock_guard", "<", "std", ":", ":", "mutex", ">",
220
+ "_pf_tz_lock", "(", "_pf_tz_mu", ")", ";",
221
+ "const", "char", "*", "_old", "=", "std", ":", ":", "getenv", "(",
222
+ '"TZ"', ")", ";",
223
+ "std", ":", ":", "string", "_old_tz", "=", "_old", "?", "_old", ":",
224
+ '""', ";",
225
+ "bool", "_had_old", "=", "(", "_old", "!", "=", "nullptr", ")", ";",
226
+ ":", ":", "setenv", "(", '"TZ"', ",", "_tz", ".", "c_str", "(",
227
+ ")", ",", "1", ")", ";",
228
+ ":", ":", "tzset", "(", ")", ";",
229
+ "localtime_r", "(", "&", "_secs", ",", "&", "tm_buf", ")", ";",
230
+ "if", "(", "_had_old", ")", "{",
231
+ ":", ":", "setenv", "(", '"TZ"', ",", "_old_tz", ".", "c_str", "(",
232
+ ")", ",", "1", ")", ";", "}",
233
+ "else", "{", ":", ":", "unsetenv", "(", '"TZ"', ")", ";", "}",
234
+ ":", ":", "tzset", "(", ")", ";",
235
+ "}",
236
+ )
237
+
238
+ _SEALED_TZ_CALENDAR_RUN = (
239
+ "else", "{", "static", "std", ":", ":", "mutex", "_pf_ts_mu", ";",
240
+ "std", ":", ":", "lock_guard", "<", "std", ":", ":", "mutex", ">",
241
+ "_pf_ts_mu_lock", "(", "_pf_ts_mu", ")", ";",
242
+ "const", "char", "*", "_old", "=", "std", ":", ":", "getenv", "(",
243
+ '"TZ"', ")", ";",
244
+ "std", ":", ":", "string", "_old_tz", "=", "_old", "?", "_old", ":",
245
+ '""', ";",
246
+ "bool", "_had_old", "=", "(", "_old", "!", "=", "nullptr", ")", ";",
247
+ ":", ":", "setenv", "(", '"TZ"', ",", "_tz", ".", "c_str", "(",
248
+ ")", ",", "1", ")", ";",
249
+ ":", ":", "tzset", "(", ")", ";",
250
+ "_res", "=", "(", "int64_t", ")", "mktime", "(", "&", "t", ")", "*",
251
+ "1000", ";",
252
+ "if", "(", "_had_old", ")", "{",
253
+ ":", ":", "setenv", "(", '"TZ"', ",", "_old_tz", ".", "c_str", "(",
254
+ ")", ",", "1", ")", ";", "}",
255
+ "else", "{", ":", ":", "unsetenv", "(", '"TZ"', ")", ";", "}",
256
+ ":", ":", "tzset", "(", ")", ";",
257
+ "}",
258
+ )
259
+
260
+ _SEALED_TZ_RUNS = (_SEALED_TZ_FIELD_RUN, _SEALED_TZ_CALENDAR_RUN)
261
+
262
+ _NUMERIC_UDL_RE = re.compile(
263
+ r"^(?:0[xX][0-9A-Fa-f]+|0[bB][01]+|(?:\d+\.?\d*|\.\d+)"
264
+ r"(?:[eEpP][+-]?\d+)?)(_[A-Za-z_]\w*)$"
265
+ )
266
+
267
+ _RAW_PREFIXES = ("u8R\"", "uR\"", "UR\"", "LR\"", "R\"")
268
+ _QUOTED_PREFIXES = (
269
+ ("u8\"", '"'), ("u\"", '"'), ("U\"", '"'), ("L\"", '"'), ('"', '"'),
270
+ ("u8'", "'"), ("u'", "'"), ("U'", "'"), ("L'", "'"), ("'", "'"),
271
+ )
272
+
273
+ _TRIGRAPH_FOLLOWERS = frozenset("=/'()!<>-")
274
+ _DIGRAPHS = ("%:%:", "<:", ":>", "<%", "%>", "%:", "##")
275
+
276
+
277
+ @dataclass(frozen=True)
278
+ class _Token:
279
+ kind: str
280
+ text: str
281
+ line: int
282
+ column: int
283
+
284
+
285
+ class _Tokenizer:
286
+ def __init__(self, text: str) -> None:
287
+ self.text = text
288
+ self.n = len(text)
289
+ self.i = 0
290
+ self.line = 1
291
+ self.column = 1
292
+ self.bol = True
293
+ self.tokens: list[_Token] = []
294
+ self.findings: list[Finding] = []
295
+
296
+ # -- location helpers -------------------------------------------------
297
+ def _advance(self, count: int = 1) -> None:
298
+ for _ in range(count):
299
+ if self.i >= self.n:
300
+ return
301
+ ch = self.text[self.i]
302
+ self.i += 1
303
+ if ch == "\n":
304
+ self.line += 1
305
+ self.column = 1
306
+ self.bol = True
307
+ else:
308
+ self.column += 1
309
+
310
+ def _finding(self, rule: str, line: int, column: int) -> None:
311
+ self.findings.append(Finding(line, column, rule, REASONS[rule]))
312
+
313
+ def _splice_len(self, at: int) -> int:
314
+ text = self.text
315
+ if at >= self.n or text[at] != "\\":
316
+ return 0
317
+ if at + 1 < self.n and text[at + 1] == "\n":
318
+ return 2
319
+ if (at + 2 < self.n and text[at + 1] == "\r"
320
+ and text[at + 2] == "\n"):
321
+ return 3
322
+ if at + 1 < self.n and text[at + 1] == "\r":
323
+ return 2
324
+ return 0
325
+
326
+ def _skip_splice(self, length: int) -> None:
327
+ self._finding("line-splice", self.line, self.column)
328
+ # A splice joins two physical lines: the logical line continues, so
329
+ # ``bol`` is deliberately left unchanged.
330
+ self.i += length
331
+ self.line += 1
332
+ self.column = 1
333
+
334
+ # -- character classes ------------------------------------------------
335
+ @staticmethod
336
+ def _is_ident_start(ch: str) -> bool:
337
+ return ch == "_" or ch.isalpha() or ord(ch) >= 128
338
+
339
+ @staticmethod
340
+ def _is_ident_char(ch: str) -> bool:
341
+ return ch == "_" or ch.isalnum() or ord(ch) >= 128
342
+
343
+ # -- sub-scanners -----------------------------------------------------
344
+ def _scan_line_comment(self) -> None:
345
+ self._advance(2)
346
+ while self.i < self.n:
347
+ ch = self.text[self.i]
348
+ if ch == "\n":
349
+ return
350
+ splice = self._splice_len(self.i)
351
+ if splice:
352
+ self._skip_splice(splice)
353
+ continue
354
+ if ch == "\x00":
355
+ self._finding("nul", self.line, self.column)
356
+ self._advance()
357
+ continue
358
+ self._advance()
359
+
360
+ def _scan_block_comment(self) -> None:
361
+ self._advance(2)
362
+ while self.i < self.n:
363
+ if self.text.startswith("*/", self.i):
364
+ self._advance(2)
365
+ return
366
+ splice = self._splice_len(self.i)
367
+ if splice:
368
+ self._skip_splice(splice)
369
+ continue
370
+ ch = self.text[self.i]
371
+ if ch == "\x00":
372
+ self._finding("nul", self.line, self.column)
373
+ self._advance()
374
+ continue
375
+ self._advance()
376
+
377
+ def _scan_directive(self) -> None:
378
+ start_line, start_column = self.line, self.column
379
+ parts: list[str] = []
380
+ while self.i < self.n:
381
+ ch = self.text[self.i]
382
+ if ch == "\n":
383
+ break
384
+ splice = self._splice_len(self.i)
385
+ if splice:
386
+ self._skip_splice(splice)
387
+ parts.append(" ")
388
+ continue
389
+ if ch == "\x00":
390
+ self._finding("nul", self.line, self.column)
391
+ self._advance()
392
+ continue
393
+ parts.append(ch)
394
+ self._advance()
395
+ line_text = "".join(parts)
396
+ self.tokens.append(
397
+ _Token("directive", line_text, start_line, start_column)
398
+ )
399
+ self._check_directive(line_text, start_line, start_column)
400
+
401
+ def _check_directive(self, line_text: str, line: int, column: int) -> None:
402
+ normalised = _normalise_pp(line_text)
403
+ match = re.match(
404
+ r"#\s*([A-Za-z_][A-Za-z0-9_]*)(.*)$", line_text, re.S
405
+ )
406
+ if not match:
407
+ self._finding("directive", line, column)
408
+ return
409
+ name = match.group(1)
410
+ rest = match.group(2)
411
+
412
+ if name == "include":
413
+ stripped = rest.strip()
414
+ if not stripped.startswith("<"):
415
+ self._finding("include", line, column)
416
+ return
417
+ close = stripped.find(">")
418
+ if close < 0:
419
+ self._finding("include", line, column)
420
+ return
421
+ header = stripped[1:close]
422
+ if header not in ALLOWED_INCLUDES:
423
+ self._finding("include", line, column)
424
+ elif name == "define":
425
+ if normalised not in _ALLOWED_DEFINE_LINES:
426
+ self._finding("define", line, column)
427
+ elif name == "undef":
428
+ undef = re.match(
429
+ r"#\s*undef\s+([A-Za-z_][A-Za-z0-9_]*)", line_text
430
+ )
431
+ if undef is None or undef.group(1) not in _ALLOWED_DEFINE_NAMES:
432
+ self._finding("undef", line, column)
433
+ elif name == "error":
434
+ if normalised not in _ALLOWED_ERROR_LINES:
435
+ self._finding("directive", line, column)
436
+ elif name == "pragma":
437
+ self._finding("pragma", line, column)
438
+ elif name in _ALLOWED_CONTROL_DIRECTIVES:
439
+ pass
440
+ else:
441
+ self._finding("directive", line, column)
442
+
443
+ def _string_prefix(self) -> tuple[int, str, bool] | None:
444
+ for prefix in _RAW_PREFIXES:
445
+ if self.text.startswith(prefix, self.i):
446
+ if self.i == 0 or not self._is_ident_char(self.text[self.i - 1]):
447
+ return len(prefix), '"', True
448
+ for prefix, quote in _QUOTED_PREFIXES:
449
+ if self.text.startswith(prefix, self.i):
450
+ if len(prefix) == 1:
451
+ return 1, quote, False
452
+ if self.i == 0 or not self._is_ident_char(self.text[self.i - 1]):
453
+ return len(prefix), quote, False
454
+ return None
455
+
456
+ def _scan_literal(self, prefix: tuple[int, str, bool]) -> None:
457
+ start = self.i
458
+ start_line, start_column = self.line, self.column
459
+ length, quote, is_raw = prefix
460
+ self._advance(length)
461
+
462
+ if is_raw:
463
+ self._finding("raw-string", start_line, start_column)
464
+ delimiter: list[str] = []
465
+ valid = True
466
+ while self.i < self.n:
467
+ ch = self.text[self.i]
468
+ if ch == "(":
469
+ self._advance()
470
+ break
471
+ if len(delimiter) >= 16 or ch in " ()\\\t\r\n\v\f":
472
+ valid = False
473
+ break
474
+ delimiter.append(ch)
475
+ self._advance()
476
+ else:
477
+ valid = False
478
+ if not valid:
479
+ self._finding("malformed-literal", start_line, start_column)
480
+ # A broken raw literal is refused already; consume to EOF so
481
+ # its contents are not re-read as code.
482
+ while self.i < self.n:
483
+ if self.text[self.i] == "\x00":
484
+ self._finding("nul", self.line, self.column)
485
+ self._advance()
486
+ else:
487
+ closing = ")" + "".join(delimiter) + '"'
488
+ found = self.text.find(closing, self.i)
489
+ if found < 0:
490
+ self._finding("malformed-literal", start_line, start_column)
491
+ while self.i < self.n:
492
+ if self.text[self.i] == "\x00":
493
+ self._finding("nul", self.line, self.column)
494
+ self._advance()
495
+ else:
496
+ end = found + len(closing)
497
+ while self.i < end:
498
+ if self.text[self.i] == "\x00":
499
+ self._finding("nul", self.line, self.column)
500
+ self._advance()
501
+ self.tokens.append(
502
+ _Token("string", self.text[start:self.i],
503
+ start_line, start_column)
504
+ )
505
+ self.bol = False
506
+ return
507
+
508
+ kind = "string" if quote == '"' else "char"
509
+ while self.i < self.n:
510
+ ch = self.text[self.i]
511
+ if ch == "\\":
512
+ splice = self._splice_len(self.i)
513
+ if splice:
514
+ self._skip_splice(splice)
515
+ continue
516
+ if self.i + 1 < self.n and self.text[self.i + 1] == "\x00":
517
+ self._finding("nul", self.line, self.column + 1)
518
+ self._advance(2)
519
+ continue
520
+ if ch == "\x00":
521
+ self._finding("nul", self.line, self.column)
522
+ self._advance()
523
+ continue
524
+ if ch == "\n":
525
+ self._finding("malformed-literal", start_line, start_column)
526
+ break
527
+ self._advance()
528
+ if ch == quote:
529
+ break
530
+ self.tokens.append(
531
+ _Token(kind, self.text[start:self.i], start_line, start_column)
532
+ )
533
+ self.bol = False
534
+
535
+ def _scan_identifier(self) -> None:
536
+ start_line, start_column = self.line, self.column
537
+ start = self.i
538
+ while self.i < self.n and self._is_ident_char(self.text[self.i]):
539
+ self._advance()
540
+ word = self.text[start:self.i]
541
+ for offset, ch in enumerate(word):
542
+ if ord(ch) > 127:
543
+ self._finding(
544
+ "non-ascii-identifier",
545
+ start_line,
546
+ start_column + offset,
547
+ )
548
+ break
549
+ self.tokens.append(_Token("id", word, start_line, start_column))
550
+ self.bol = False
551
+
552
+ def _scan_number(self) -> None:
553
+ start_line, start_column = self.line, self.column
554
+ start = self.i
555
+ while self.i < self.n:
556
+ ch = self.text[self.i]
557
+ if ch == "'":
558
+ # C++14 digit separator ([lex.ppnumber]): consume it as part
559
+ # of the pp-number, never as a character literal, so text
560
+ # after it cannot be hidden from the identifier scan.
561
+ following = (
562
+ self.text[self.i + 1] if self.i + 1 < self.n else ""
563
+ )
564
+ if (following and (
565
+ following.isalnum() or following == "_"
566
+ or ord(following) >= 128
567
+ )):
568
+ self._finding("numeric-separator", self.line, self.column)
569
+ self._advance()
570
+ continue
571
+ break
572
+ if ch.isalnum() or ch in "_.":
573
+ self._advance()
574
+ continue
575
+ if (ch in "+-" and self.i > start
576
+ and self.text[self.i - 1] in "eEpP"):
577
+ self._advance()
578
+ continue
579
+ break
580
+ word = self.text[start:self.i]
581
+ if _NUMERIC_UDL_RE.match(word):
582
+ self._finding("numeric-udl", start_line, start_column)
583
+ self.tokens.append(_Token("number", word, start_line, start_column))
584
+ self.bol = False
585
+
586
+ def _digraph_length(self) -> int:
587
+ # [lex.pptoken] max-munch exception: ``<::`` is an ordinary ``<``
588
+ # followed by ``::`` unless the character after ``<::`` is another
589
+ # ``:`` or a ``>``. Codegen emits that sequence for qualified type
590
+ # members (``std::vector<::pineforge::checked_settings::Setting>``).
591
+ if self.text.startswith("<::", self.i):
592
+ after = self.text[self.i + 3] if self.i + 3 < self.n else ""
593
+ if after not in (":", ">"):
594
+ return 0
595
+ for digraph in _DIGRAPHS:
596
+ if self.text.startswith(digraph, self.i):
597
+ return len(digraph)
598
+ return 0
599
+
600
+ # -- main loop --------------------------------------------------------
601
+ def tokenize(self) -> list[Finding]:
602
+ text = self.text
603
+ while self.i < self.n:
604
+ start_line, start_column = self.line, self.column
605
+ ch = text[self.i]
606
+
607
+ if ch == "\x00":
608
+ self._finding("nul", start_line, start_column)
609
+ self._advance()
610
+ self.bol = False
611
+ continue
612
+
613
+ splice = self._splice_len(self.i)
614
+ if splice:
615
+ self._skip_splice(splice)
616
+ continue
617
+ if ch == "\\":
618
+ # A universal-character-name can spell a non-ASCII identifier
619
+ # without a non-ASCII byte; no generated code holds a bare
620
+ # backslash outside a literal, comment or directive.
621
+ self._finding("escape", start_line, start_column)
622
+ self._advance()
623
+ self.bol = False
624
+ continue
625
+
626
+ if ch in " \t\v\f\r":
627
+ self._advance()
628
+ continue
629
+ if ch == "\n":
630
+ self._advance()
631
+ continue
632
+
633
+ if ch == "#" and self.bol:
634
+ self._scan_directive()
635
+ continue
636
+ if text.startswith("//", self.i):
637
+ self._scan_line_comment()
638
+ continue
639
+ if text.startswith("/*", self.i):
640
+ self._scan_block_comment()
641
+ continue
642
+
643
+ prefix = self._string_prefix()
644
+ if prefix is not None:
645
+ self._scan_literal(prefix)
646
+ continue
647
+
648
+ if (ch == "?" and self.i + 2 < self.n
649
+ and text[self.i + 1] == "?"
650
+ and text[self.i + 2] in _TRIGRAPH_FOLLOWERS):
651
+ self._finding("trigraph", start_line, start_column)
652
+ self._advance(3)
653
+ self.bol = False
654
+ continue
655
+
656
+ digraph_len = self._digraph_length()
657
+ if digraph_len:
658
+ self._finding("digraph", start_line, start_column)
659
+ self._advance(digraph_len)
660
+ self.bol = False
661
+ continue
662
+
663
+ if self._is_ident_start(ch):
664
+ self._scan_identifier()
665
+ continue
666
+ if ch.isdigit() or (
667
+ ch == "." and self.i + 1 < self.n
668
+ and text[self.i + 1].isdigit()
669
+ ):
670
+ self._scan_number()
671
+ continue
672
+
673
+ self.tokens.append(_Token("punct", ch, start_line, start_column))
674
+ self._advance()
675
+ self.bol = False
676
+
677
+ self._check_tokens()
678
+ self.findings.sort(key=lambda f: (f.line, f.column, f.rule))
679
+ return self.findings
680
+
681
+ # -- whole-token checks ----------------------------------------------
682
+ def _sealed_tz_tokens(self) -> set[int]:
683
+ texts = [token.text for token in self.tokens]
684
+ sealed: set[int] = set()
685
+ for pattern in _SEALED_TZ_RUNS:
686
+ length = len(pattern)
687
+ for start in range(0, len(texts) - length + 1):
688
+ if texts[start:start + length] == list(pattern):
689
+ sealed.update(range(start, start + length))
690
+ return sealed
691
+
692
+ @staticmethod
693
+ def _takes_address(tokens: list[_Token], index: int) -> bool:
694
+ """``&name``, ``&::name`` or ``&std::name`` (not a reference type)."""
695
+ before = index - 1
696
+ while before >= 0 and tokens[before].text == ":":
697
+ before -= 1
698
+ if before >= 0 and tokens[before].text == "std":
699
+ before -= 1
700
+ return before >= 0 and tokens[before].text == "&"
701
+
702
+ def _check_tokens(self) -> None:
703
+ sealed = self._sealed_tz_tokens()
704
+ extern_block_seen = False
705
+ tokens = self.tokens
706
+
707
+ for index, token in enumerate(tokens):
708
+ if token.kind != "id":
709
+ continue
710
+ name = token.text
711
+ previous = tokens[index - 1].text if index else ""
712
+ before_previous = tokens[index - 2].text if index >= 2 else ""
713
+
714
+ if name == "extern":
715
+ following = tokens[index + 1] if index + 1 < len(tokens) else None
716
+ opening = tokens[index + 2] if index + 2 < len(tokens) else None
717
+ if (not extern_block_seen
718
+ and following is not None
719
+ and following.kind == "string"
720
+ and following.text == '"C"'
721
+ and opening is not None
722
+ and opening.text == "{"):
723
+ extern_block_seen = True
724
+ continue
725
+ self._finding("extern-c", token.line, token.column)
726
+ continue
727
+
728
+ # A member access (``bar.open``, ``ptr->open``) names an object's
729
+ # member, never the libc function of the same spelling.
730
+ if previous == "." or (previous == ">" and before_previous == "-"):
731
+ continue
732
+
733
+ following = tokens[index + 1].text if index + 1 < len(tokens) else ""
734
+ if name in _LIBC_ADDRESS_NAMES and (
735
+ self._takes_address(tokens, index)
736
+ or (name in _LIBC_DECAY_NAMES and following != "(")
737
+ ):
738
+ self._finding("function-pointer", token.line, token.column)
739
+ continue
740
+
741
+ if name in _ASM_NAMES:
742
+ self._finding("asm", token.line, token.column)
743
+ elif name == "_Pragma":
744
+ self._finding("pragma", token.line, token.column)
745
+ elif name == "reinterpret_cast":
746
+ self._finding("reinterpret-cast", token.line, token.column)
747
+ elif name in _PROCESS_NAMES or name in _EXEC_NAMES:
748
+ self._finding("process", token.line, token.column)
749
+ elif name in _DL_NAMES:
750
+ self._finding("dlopen", token.line, token.column)
751
+ elif name in _FILE_IO_NAMES:
752
+ self._finding("file-io", token.line, token.column)
753
+ elif name in _SOCKET_NAMES:
754
+ self._finding("socket", token.line, token.column)
755
+ elif name in _STREAM_NAMES:
756
+ self._finding("filesystem-stream", token.line, token.column)
757
+ elif name in _ENV_NAMES and index not in sealed:
758
+ self._finding("env", token.line, token.column)
759
+
760
+
761
+ def check_generated_cpp(text: str) -> list[Finding]:
762
+ """Return every refused construct in generated C++ ``text``.
763
+
764
+ An empty list means the translation unit contains only constructs the
765
+ allowlist permits.
766
+ """
767
+ return _Tokenizer(text).tokenize()
768
+
769
+
770
+ def main(argv: list[str] | None = None) -> int:
771
+ """``pf-cpp-allowlist [FILE ...]``; standard input when none is named."""
772
+ parser = argparse.ArgumentParser(
773
+ prog="pf-cpp-allowlist",
774
+ description="Check generated C++ against the codegen allowlist.",
775
+ )
776
+ parser.add_argument(
777
+ "files",
778
+ nargs="*",
779
+ metavar="FILE",
780
+ help="generated .cpp files (default: standard input)",
781
+ )
782
+ args = parser.parse_args(argv)
783
+ paths = args.files or ["-"]
784
+ failed = False
785
+ for path in paths:
786
+ if path == "-":
787
+ text = sys.stdin.read()
788
+ label = "<stdin>"
789
+ else:
790
+ text = Path(path).read_text(encoding="utf-8")
791
+ label = path
792
+ findings = check_generated_cpp(text)
793
+ for finding in findings:
794
+ print(
795
+ f"{label}:{finding.line}:{finding.column}: "
796
+ f"{finding.rule}: {finding.reason}"
797
+ )
798
+ if findings:
799
+ failed = True
800
+ return 1 if failed else 0
801
+
802
+
803
+ if __name__ == "__main__":
804
+ raise SystemExit(main())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: pineforge-codegen
3
- Version: 1.6.1
3
+ Version: 1.6.2
4
4
  Summary: PineScript v6 to C++ transpiler that targets the pineforge-engine runtime.
5
5
  Project-URL: Homepage, https://github.com/pineforge-4pass/pineforge-codegen-oss
6
6
  Project-URL: Issues, https://github.com/pineforge-4pass/pineforge-codegen-oss/issues
@@ -18,9 +18,9 @@ License: PineForge Source License 1.2
18
18
  definition of investment capital now covers the capital in such an
19
19
  account, real or simulated.
20
20
 
21
- Copyright 2025-2026 pineforge, LLC
21
+ Copyright 2025-2026 PINEFORGE LLC
22
22
 
23
- Required Notice: Copyright 2025-2026 pineforge, LLC (https://github.com/pineforge-4pass/pineforge-codegen-oss)
23
+ Required Notice: Copyright 2025-2026 PINEFORGE LLC (https://github.com/pineforge-4pass/pineforge-codegen-oss)
24
24
 
25
25
  The software is free of charge for the permitted purposes below, including
26
26
  Personal Trading. Commercial Use, defined below, needs a separate commercial
@@ -251,7 +251,7 @@ License: PineForge Source License 1.2
251
251
 
252
252
  ## Definitions
253
253
 
254
- The **licensor** is pineforge, LLC, and the **software** is the software the
254
+ The **licensor** is PINEFORGE LLC, and the **software** is the software the
255
255
  licensor makes available under these terms.
256
256
 
257
257
  **You** refers to the individual or entity agreeing to these terms.
@@ -931,7 +931,7 @@ use `python -m pytest --collect-only -q` for the current collection count.
931
931
  ## License
932
932
 
933
933
  Source-available under the [PineForge Source License 1.2](https://github.com/pineforge-4pass/pineforge-codegen-oss/blob/main/LICENSE);
934
- the `LICENSE` file is the controlling text. The licensor is pineforge, LLC.
934
+ the `LICENSE` file is the controlling text. The licensor is PINEFORGE LLC.
935
935
 
936
936
  - **Free for noncommercial use:** any noncommercial purpose, and use by a
937
937
  charitable organization, educational institution, public research
@@ -3,6 +3,7 @@ pineforge_codegen/ast_nodes.py,sha256=aBCFzm-VqyJq_acQU1WFczxfVbOlaK9eVJQu_nXZvw
3
3
  pineforge_codegen/block_locals.py,sha256=ikft5a5mOUnAcOrW5tIjylyCf7jyUmCKSGPklxJXydY,7220
4
4
  pineforge_codegen/builtin_keywords.py,sha256=3tcIfKuyC_T9pJQRolvol1yPbVctRdHbErbaso9x7tE,1885
5
5
  pineforge_codegen/collection_history.py,sha256=r0Rnw4wADJVyu47E6cmRGpzyjeSc7raDWOm6e9YxM9A,84054
6
+ pineforge_codegen/cpp_allowlist.py,sha256=cOi7orYSXq69mWMioIs1HwvQHUey_w4jboUUFiJsR3Y,30636
6
7
  pineforge_codegen/diagnostic_codes.py,sha256=38jCN8dbWWsxYnpP51n68lrjeNnSa91cC4M4n83vCN0,14522
7
8
  pineforge_codegen/diagnostics_catalog.json,sha256=oW5tCwV9-hjvUtNQb1bwG80p94eaBZfxf_gknyHYKwI,367871
8
9
  pineforge_codegen/errors.py,sha256=ZUBCx2BA8PS2G8GNoVjxgzlZm8kqiq42As_Q744b61k,4291
@@ -44,7 +45,7 @@ pineforge_codegen/codegen/collection_history.py,sha256=ajb4VV1_tlcmNyOaIvFZGkBA1
44
45
  pineforge_codegen/codegen/constant_fold.py,sha256=39QQnkiccwAVYmg3hUDjra5LqvOwKrVmu1HJKE03qlU,5098
45
46
  pineforge_codegen/codegen/drawing.py,sha256=mnDWeTdERMauYqObiq2QOzVuzKOfos6i7qXP2QcUim4,38289
46
47
  pineforge_codegen/codegen/emit_top.py,sha256=OttQ9NzcqMCKHBbkfdyv-JVXV7Ro6eOSQY-F9m251IE,154334
47
- pineforge_codegen/codegen/helpers.py,sha256=tBOsNYlMBcGDQAAGH_gaW0AfFBGs6-HzDut2mIxgIaA,31630
48
+ pineforge_codegen/codegen/helpers.py,sha256=1Ypmc-MxR86te88CRPQl3g3yoit4J8aKZo76Dz4-SVQ,32601
48
49
  pineforge_codegen/codegen/helpers_syminfo.py,sha256=GFrx9i2HXSCmGIpIds0cFrtJ6IQiznfXxH8aI8h3DQ4,4878
49
50
  pineforge_codegen/codegen/host_members.py,sha256=hmbkxs22NHAcRQ3l4WD7fU_GcdloNgQRgPTebk5ckcc,4599
50
51
  pineforge_codegen/codegen/input.py,sha256=KlitUiphXryWKiPjfUqhk2VLyTVliyiEt_JqkLtABqw,30091
@@ -59,7 +60,8 @@ pineforge_codegen/codegen/types.py,sha256=lC-HVTYE-knXHcJSzVRTSMnl4o_1ZaCtKVmRHK
59
60
  pineforge_codegen/codegen/visit_call.py,sha256=eXR_8iPfL40egxcE39myBGgMwlBtOEJczU_oEnpJQQo,195250
60
61
  pineforge_codegen/codegen/visit_expr.py,sha256=nLFf4ErbvEAKJZYjeQUiDyh_H3_8ICEsk_h6VholoG4,111358
61
62
  pineforge_codegen/codegen/visit_stmt.py,sha256=czS0z7QuskBeCjpy9i2srg2Z8nR0VdXqho3-AVIeZ1U,118214
62
- pineforge_codegen-1.6.1.dist-info/METADATA,sha256=OYQ9pTd0oU-NxHnMmovug8_5VFxpb-_y_uS7xJLz6gs,54550
63
- pineforge_codegen-1.6.1.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
64
- pineforge_codegen-1.6.1.dist-info/licenses/LICENSE,sha256=jOb1NwvEgyuqWgdxUE6jTtQGRZqCz261Ws0d_LRztvw,11164
65
- pineforge_codegen-1.6.1.dist-info/RECORD,,
63
+ pineforge_codegen-1.6.2.dist-info/METADATA,sha256=Pi_zOSA1rJwvl2Aq-AOrkidIvfPdX400flX-yUGfvOs,54546
64
+ pineforge_codegen-1.6.2.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
65
+ pineforge_codegen-1.6.2.dist-info/entry_points.txt,sha256=dSUCCmwV4UpL1PyC6THPdQ-l3ddue7ZH1RgRHpASvhY,74
66
+ pineforge_codegen-1.6.2.dist-info/licenses/LICENSE,sha256=X0M0ankijg7iZuoI78biEG1kr6klX8NgzDKZAyaPcg4,11161
67
+ pineforge_codegen-1.6.2.dist-info/RECORD,,
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ pf-cpp-allowlist = pineforge_codegen.cpp_allowlist:main
@@ -9,9 +9,9 @@ person's own account or capital. In the Commercial Use section, the
9
9
  definition of investment capital now covers the capital in such an
10
10
  account, real or simulated.
11
11
 
12
- Copyright 2025-2026 pineforge, LLC
12
+ Copyright 2025-2026 PINEFORGE LLC
13
13
 
14
- Required Notice: Copyright 2025-2026 pineforge, LLC (https://github.com/pineforge-4pass/pineforge-codegen-oss)
14
+ Required Notice: Copyright 2025-2026 PINEFORGE LLC (https://github.com/pineforge-4pass/pineforge-codegen-oss)
15
15
 
16
16
  The software is free of charge for the permitted purposes below, including
17
17
  Personal Trading. Commercial Use, defined below, needs a separate commercial
@@ -242,7 +242,7 @@ kind of legal claim.***
242
242
 
243
243
  ## Definitions
244
244
 
245
- The **licensor** is pineforge, LLC, and the **software** is the software the
245
+ The **licensor** is PINEFORGE LLC, and the **software** is the software the
246
246
  licensor makes available under these terms.
247
247
 
248
248
  **You** refers to the individual or entity agreeing to these terms.