pineforge-codegen 1.6.1__tar.gz → 1.6.2rc1__tar.gz

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.
Files changed (75) hide show
  1. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/PKG-INFO +1 -1
  2. pineforge_codegen-1.6.2rc1/VERSION +1 -0
  3. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/helpers.py +21 -1
  4. pineforge_codegen-1.6.2rc1/pineforge_codegen/cpp_allowlist.py +804 -0
  5. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pyproject.toml +3 -0
  6. pineforge_codegen-1.6.1/VERSION +0 -1
  7. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/.gitignore +0 -0
  8. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/CHANGELOG.md +0 -0
  9. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/CONTRIBUTING.md +0 -0
  10. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/LEGAL.md +0 -0
  11. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/LICENSE +0 -0
  12. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/README.md +0 -0
  13. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/docs/PUBLIC_CONTRACT.md +0 -0
  14. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/docs/codegen-coverage-gaps.md +0 -0
  15. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/docs/pine-cap-activation.md +0 -0
  16. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/__init__.py +0 -0
  17. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/analyzer/__init__.py +0 -0
  18. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/analyzer/base.py +0 -0
  19. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/analyzer/call_handlers.py +0 -0
  20. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/analyzer/contracts.py +0 -0
  21. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/analyzer/diagnostics.py +0 -0
  22. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/analyzer/tables.py +0 -0
  23. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/analyzer/types.py +0 -0
  24. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/ast_nodes.py +0 -0
  25. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/block_locals.py +0 -0
  26. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/builtin_keywords.py +0 -0
  27. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/__init__.py +0 -0
  28. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/base.py +0 -0
  29. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/capabilities.py +0 -0
  30. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/checked_settings.py +0 -0
  31. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/collection_history.py +0 -0
  32. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/constant_fold.py +0 -0
  33. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/drawing.py +0 -0
  34. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/emit_top.py +0 -0
  35. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/helpers_syminfo.py +0 -0
  36. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/host_members.py +0 -0
  37. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/input.py +0 -0
  38. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/order_shapes.py +0 -0
  39. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/run_stops.py +0 -0
  40. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/security.py +0 -0
  41. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/session_market.py +0 -0
  42. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/ta.py +0 -0
  43. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/tables.py +0 -0
  44. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/tv_number_format.py +0 -0
  45. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/types.py +0 -0
  46. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/visit_call.py +0 -0
  47. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/visit_expr.py +0 -0
  48. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/codegen/visit_stmt.py +0 -0
  49. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/collection_history.py +0 -0
  50. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/diagnostic_codes.py +0 -0
  51. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/diagnostics_catalog.json +0 -0
  52. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/errors.py +0 -0
  53. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/external_requests.py +0 -0
  54. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/finite_ta_length.py +0 -0
  55. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/lexer.py +0 -0
  56. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/library_inline.py +0 -0
  57. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/library_modules.py +0 -0
  58. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/library_v5.py +0 -0
  59. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/limits.py +0 -0
  60. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/matrix_overloads.py +0 -0
  61. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/method_binding.py +0 -0
  62. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/parser.py +0 -0
  63. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/pine_libraries.py +0 -0
  64. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/pine_spelling.py +0 -0
  65. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/pragmas.py +0 -0
  66. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/request_discovery.py +0 -0
  67. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/request_feed_inventory.py +0 -0
  68. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/security_contexts.py +0 -0
  69. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/session_reads.py +0 -0
  70. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/signatures.py +0 -0
  71. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/support_checker.py +0 -0
  72. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/symbols.py +0 -0
  73. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/timeframe_token_cli.py +0 -0
  74. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/tokens.py +0 -0
  75. {pineforge_codegen-1.6.1 → pineforge_codegen-1.6.2rc1}/pineforge_codegen/tv_input_choices.py +0 -0
@@ -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-rc.1
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
@@ -0,0 +1 @@
1
+ 1.6.2-rc.1
@@ -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())
@@ -38,6 +38,9 @@ dev = [
38
38
  "pytest>=7.0",
39
39
  ]
40
40
 
41
+ [project.scripts]
42
+ pf-cpp-allowlist = "pineforge_codegen.cpp_allowlist:main"
43
+
41
44
  [project.urls]
42
45
  Homepage = "https://github.com/pineforge-4pass/pineforge-codegen-oss"
43
46
  Issues = "https://github.com/pineforge-4pass/pineforge-codegen-oss/issues"
@@ -1 +0,0 @@
1
- 1.6.1