Ruby 3.4.4p34 (2025-05-14 revision a38531fd3f617bf734ef7d6c595325f69985ea1d)
options.c
1#include "prism/options.h"
2
7pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data) {
8 options->shebang_callback = shebang_callback;
9 options->shebang_callback_data = shebang_callback_data;
10}
11
16pm_options_filepath_set(pm_options_t *options, const char *filepath) {
17 pm_string_constant_init(&options->filepath, filepath, strlen(filepath));
18}
19
24pm_options_encoding_set(pm_options_t *options, const char *encoding) {
25 pm_string_constant_init(&options->encoding, encoding, strlen(encoding));
26}
27
32pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked) {
33 options->encoding_locked = encoding_locked;
34}
35
40pm_options_line_set(pm_options_t *options, int32_t line) {
41 options->line = line;
42}
43
48pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal) {
50}
51
56pm_options_command_line_set(pm_options_t *options, uint8_t command_line) {
57 options->command_line = command_line;
58}
59
63static inline bool
64is_number(const char *string, size_t length) {
65 return pm_strspn_decimal_digit((const uint8_t *) string, (ptrdiff_t) length) == length;
66}
67
74pm_options_version_set(pm_options_t *options, const char *version, size_t length) {
75 if (version == NULL) {
77 return true;
78 }
79
80 if (length == 3) {
81 if (strncmp(version, "3.3", 3) == 0) {
83 return true;
84 }
85
86 if (strncmp(version, "3.4", 3) == 0) {
88 return true;
89 }
90
91 return false;
92 }
93
94 if (length >= 4) {
95 if (strncmp(version, "3.3.", 4) == 0 && is_number(version + 4, length - 4)) {
97 return true;
98 }
99
100 if (strncmp(version, "3.4.", 4) == 0 && is_number(version + 4, length - 4)) {
102 return true;
103 }
104 }
105
106 if (length >= 6) {
107 if (strncmp(version, "latest", 7) == 0) { // 7 to compare the \0 as well
109 return true;
110 }
111 }
112
113 return false;
114}
115
120pm_options_main_script_set(pm_options_t *options, bool main_script) {
121 options->main_script = main_script;
122}
123
128pm_options_partial_script_set(pm_options_t *options, bool partial_script) {
129 options->partial_script = partial_script;
130}
131
132// For some reason, GCC analyzer thinks we're leaking allocated scopes and
133// locals here, even though we definitely aren't. This is a false positive.
134// Ideally we wouldn't need to suppress this.
135#if defined(__GNUC__) && (__GNUC__ >= 10)
136#pragma GCC diagnostic push
137#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
138#endif
139
144pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
145 options->scopes_count = scopes_count;
146 options->scopes = xcalloc(scopes_count, sizeof(pm_options_scope_t));
147 return options->scopes != NULL;
148}
149
154pm_options_scope_get(const pm_options_t *options, size_t index) {
155 return &options->scopes[index];
156}
157
163pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
164 scope->locals_count = locals_count;
165 scope->locals = xcalloc(locals_count, sizeof(pm_string_t));
167 return scope->locals != NULL;
168}
169
174pm_options_scope_local_get(const pm_options_scope_t *scope, size_t index) {
175 return &scope->locals[index];
176}
177
182pm_options_scope_forwarding_set(pm_options_scope_t *scope, uint8_t forwarding) {
183 scope->forwarding = forwarding;
184}
185
190pm_options_free(pm_options_t *options) {
191 pm_string_free(&options->filepath);
192 pm_string_free(&options->encoding);
193
194 for (size_t scope_index = 0; scope_index < options->scopes_count; scope_index++) {
195 pm_options_scope_t *scope = &options->scopes[scope_index];
196
197 for (size_t local_index = 0; local_index < scope->locals_count; local_index++) {
198 pm_string_free(&scope->locals[local_index]);
199 }
200
201 xfree(scope->locals);
202 }
203
204 xfree(options->scopes);
205}
206
212static uint32_t
213pm_options_read_u32(const char *data) {
214 if (((uintptr_t) data) % sizeof(uint32_t) == 0) {
215 return *((uint32_t *) data);
216 } else {
217 uint32_t value;
218 memcpy(&value, data, sizeof(uint32_t));
219 return value;
220 }
221}
222
228static int32_t
229pm_options_read_s32(const char *data) {
230 if (((uintptr_t) data) % sizeof(int32_t) == 0) {
231 return *((int32_t *) data);
232 } else {
233 int32_t value;
234 memcpy(&value, data, sizeof(int32_t));
235 return value;
236 }
237}
238
246void
247pm_options_read(pm_options_t *options, const char *data) {
248 options->line = 1; // default
249 if (data == NULL) return;
250
251 uint32_t filepath_length = pm_options_read_u32(data);
252 data += 4;
253
254 if (filepath_length > 0) {
255 pm_string_constant_init(&options->filepath, data, filepath_length);
256 data += filepath_length;
257 }
258
259 options->line = pm_options_read_s32(data);
260 data += 4;
261
262 uint32_t encoding_length = pm_options_read_u32(data);
263 data += 4;
264
265 if (encoding_length > 0) {
266 pm_string_constant_init(&options->encoding, data, encoding_length);
267 data += encoding_length;
268 }
269
270 options->frozen_string_literal = (int8_t) *data++;
271 options->command_line = (uint8_t) *data++;
272 options->version = (pm_options_version_t) *data++;
273 options->encoding_locked = ((uint8_t) *data++) > 0;
274 options->main_script = ((uint8_t) *data++) > 0;
275 options->partial_script = ((uint8_t) *data++) > 0;
276
277 uint32_t scopes_count = pm_options_read_u32(data);
278 data += 4;
279
280 if (scopes_count > 0) {
281 if (!pm_options_scopes_init(options, scopes_count)) return;
282
283 for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
284 uint32_t locals_count = pm_options_read_u32(data);
285 data += 4;
286
287 pm_options_scope_t *scope = &options->scopes[scope_index];
288 if (!pm_options_scope_init(scope, locals_count)) {
289 pm_options_free(options);
290 return;
291 }
292
293 uint8_t forwarding = (uint8_t) *data++;
294 pm_options_scope_forwarding_set(&options->scopes[scope_index], forwarding);
295
296 for (size_t local_index = 0; local_index < locals_count; local_index++) {
297 uint32_t local_length = pm_options_read_u32(data);
298 data += 4;
299
300 pm_string_constant_init(&scope->locals[local_index], data, local_length);
301 data += local_length;
302 }
303 }
304 }
305}
306
307#if defined(__GNUC__) && (__GNUC__ >= 10)
308#pragma GCC diagnostic pop
309#endif
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
The options that can be passed to parsing.
struct pm_options_scope pm_options_scope_t
A scope of locals surrounding the code that is being parsed.
struct pm_options pm_options_t
The options that can be passed to the parser.
void(* pm_options_shebang_callback_t)(struct pm_options *options, const uint8_t *source, size_t length, void *shebang_callback_data)
The callback called when additional switches are found in a shebang comment that need to be processed...
Definition options.h:77
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE
The default value for parameters.
Definition options.h:48
#define PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED
String literals should be made frozen.
Definition options.h:20
#define PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED
String literals should be made mutable.
Definition options.h:31
pm_options_version_t
The version of Ruby syntax that we should be parsing with.
Definition options.h:84
@ PM_OPTIONS_VERSION_CRUBY_3_3
The vendored version of prism in CRuby 3.3.x.
Definition options.h:89
@ PM_OPTIONS_VERSION_LATEST
The current version of prism.
Definition options.h:86
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:50
size_t locals_count
The number of locals in the scope.
Definition options.h:38
uint8_t forwarding
Flags for the set of forwarding parameters in this scope.
Definition options.h:44
pm_string_t * locals
The names of the locals in the scope.
Definition options.h:41
uint8_t command_line
A bitset of the various options that were set on the command line.
Definition options.h:144
void * shebang_callback_data
Any additional data that should be passed along to the shebang callback if one was set.
Definition options.h:106
bool encoding_locked
Whether or not the encoding magic comments should be respected.
Definition options.h:160
pm_options_scope_t * scopes
The scopes surrounding the code that is being parsed.
Definition options.h:134
bool main_script
When the file being parsed is the main script, the shebang will be considered for command-line flags ...
Definition options.h:167
pm_string_t encoding
The name of the encoding that the source file is in.
Definition options.h:121
int32_t line
The line within the file that the parse starts on.
Definition options.h:115
pm_options_shebang_callback_t shebang_callback
The callback to call when additional switches are found in a shebang comment.
Definition options.h:100
int8_t frozen_string_literal
Whether or not the frozen string literal option has been set.
Definition options.h:153
bool partial_script
When the file being parsed is considered a "partial" script, jumps will not be marked as errors if th...
Definition options.h:177
size_t scopes_count
The number of scopes surrounding the code that is being parsed.
Definition options.h:126
pm_string_t filepath
The name of the file that is currently being parsed.
Definition options.h:109
pm_options_version_t version
The version of prism that we should be parsing with.
Definition options.h:141
A generic string type that can have various ownership semantics.
Definition pm_string.h:33