Line | Count | Source |
1 | | #include "prism/internal/options.h" |
2 | | |
3 | | #include "prism/compiler/inline.h" |
4 | | |
5 | | #include "prism/internal/allocator.h" |
6 | | #include "prism/internal/char.h" |
7 | | #include "prism/internal/oom.h" |
8 | | #include "prism/internal/stringy.h" |
9 | | |
10 | | #include <stdlib.h> |
11 | | #include <string.h> |
12 | | |
13 | | /** |
14 | | * Allocate a new options struct. If the options struct cannot be allocated, |
15 | | * this function aborts the process. |
16 | | */ |
17 | | pm_options_t * |
18 | 131k | pm_options_new(void) { |
19 | 131k | pm_options_t *options = xcalloc(1, sizeof(pm_options_t)); |
20 | 131k | if (options == NULL) PRISM_OOM0 (); Branch (20:9): [True: 0, False: 131k]
|
21 | | |
22 | 131k | options->line = 1; |
23 | 131k | options->stack_budget = SIZE_MAX; |
24 | 131k | return options; |
25 | 131k | } |
26 | | |
27 | | /** |
28 | | * Free the internal memory associated with the options. |
29 | | */ |
30 | | void |
31 | 119k | pm_options_cleanup(pm_options_t *options) { |
32 | 156k | for (size_t scope_index = 0; scope_index < options->scopes_count; scope_index++36.9k ) { Branch (32:34): [True: 36.9k, False: 119k]
|
33 | 36.9k | pm_options_scope_t *scope = &options->scopes[scope_index]; |
34 | 36.9k | xfree_sized(scope->locals, scope->locals_count * sizeof(pm_string_t)); |
35 | 36.9k | } |
36 | | |
37 | 119k | xfree_sized(options->scopes, options->scopes_count * sizeof(pm_options_scope_t)); |
38 | 119k | } |
39 | | |
40 | | /** |
41 | | * Free both the held memory of the given options struct and the struct itself. |
42 | | * |
43 | | * @param options The options struct to free. |
44 | | */ |
45 | | void |
46 | 119k | pm_options_free(pm_options_t *options) { |
47 | 119k | pm_options_cleanup(options); |
48 | 119k | xfree_sized(options, sizeof(pm_options_t)); |
49 | 119k | } |
50 | | |
51 | | /** |
52 | | * Set the shebang callback option on the given options struct. |
53 | | */ |
54 | | void |
55 | | pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data) { |
56 | | options->shebang_callback = shebang_callback; |
57 | | options->shebang_callback_data = shebang_callback_data; |
58 | | } |
59 | | |
60 | | /** |
61 | | * Get the filepath option on the given options struct. |
62 | | */ |
63 | | const pm_string_t * |
64 | | pm_options_filepath(const pm_options_t *options) { |
65 | | return &options->filepath; |
66 | | } |
67 | | |
68 | | /** |
69 | | * Set the filepath option on the given options struct. |
70 | | */ |
71 | | void |
72 | | pm_options_filepath_set(pm_options_t *options, const char *filepath) { |
73 | | pm_string_init(&options->filepath, (const uint8_t *) filepath, strlen(filepath)); |
74 | | } |
75 | | |
76 | | /** |
77 | | * Set the encoding option on the given options struct. |
78 | | */ |
79 | | void |
80 | | pm_options_encoding_set(pm_options_t *options, const char *encoding) { |
81 | | pm_string_init(&options->encoding, (const uint8_t *) encoding, strlen(encoding)); |
82 | | } |
83 | | |
84 | | /** |
85 | | * Set the encoding_locked option on the given options struct. |
86 | | */ |
87 | | void |
88 | 131k | pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked) { |
89 | 131k | options->encoding_locked = encoding_locked; |
90 | 131k | } |
91 | | |
92 | | /** |
93 | | * Set the line option on the given options struct. |
94 | | */ |
95 | | void |
96 | 131k | pm_options_line_set(pm_options_t *options, int32_t line) { |
97 | 131k | options->line = line; |
98 | 131k | } |
99 | | |
100 | | /** |
101 | | * Set the frozen string literal option on the given options struct. |
102 | | */ |
103 | | void |
104 | 13.1k | pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal) { |
105 | 13.1k | options->frozen_string_literal = frozen_string_literal ? PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED9.15k : PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED4.00k ; Branch (105:38): [True: 9.15k, False: 4.00k]
|
106 | 13.1k | } |
107 | | |
108 | | /** |
109 | | * Sets the command line option on the given options struct. |
110 | | */ |
111 | | void |
112 | 131k | pm_options_command_line_set(pm_options_t *options, uint8_t command_line) { |
113 | 131k | options->command_line = command_line; |
114 | 131k | } |
115 | | |
116 | | /** |
117 | | * Checks if the given slice represents a number. |
118 | | */ |
119 | | static PRISM_INLINE bool |
120 | 0 | is_number(const char *string, size_t length) { |
121 | 0 | return pm_strspn_decimal_digit((const uint8_t *) string, (ptrdiff_t) length) == length; |
122 | 0 | } |
123 | | |
124 | | /** |
125 | | * Set the version option on the given options struct by parsing the given |
126 | | * string. If the string contains an invalid option, this returns false. |
127 | | * Otherwise, it returns true. |
128 | | */ |
129 | | bool |
130 | 131k | pm_options_version_set(pm_options_t *options, const char *version, size_t length) { |
131 | 131k | if (version == NULL) { Branch (131:9): [True: 0, False: 131k]
|
132 | 0 | options->version = PM_OPTIONS_VERSION_LATEST; |
133 | 0 | return true; |
134 | 0 | } |
135 | | |
136 | 131k | if (length == 3) { Branch (136:9): [True: 131k, False: 0]
|
137 | 131k | if (memcmp(version, "3.3", 3) == 0) { Branch (137:13): [True: 10.3k, False: 121k]
|
138 | 10.3k | options->version = PM_OPTIONS_VERSION_CRUBY_3_3; |
139 | 10.3k | return true; |
140 | 10.3k | } |
141 | | |
142 | 121k | if (memcmp(version, "3.4", 3) == 0) { Branch (142:13): [True: 5.73k, False: 115k]
|
143 | 5.73k | options->version = PM_OPTIONS_VERSION_CRUBY_3_4; |
144 | 5.73k | return true; |
145 | 5.73k | } |
146 | | |
147 | 115k | if (memcmp(version, "3.5", 3) == 0 || memcmp(version, "4.0", 3) == 0108k ) { Branch (147:13): [True: 7.11k, False: 108k]
Branch (147:47): [True: 0, False: 108k]
|
148 | 7.11k | options->version = PM_OPTIONS_VERSION_CRUBY_4_0; |
149 | 7.11k | return true; |
150 | 7.11k | } |
151 | | |
152 | 108k | if (memcmp(version, "4.1", 3) == 0) { Branch (152:13): [True: 108k, False: 0]
|
153 | 108k | options->version = PM_OPTIONS_VERSION_CRUBY_4_1; |
154 | 108k | return true; |
155 | 108k | } |
156 | | |
157 | 0 | return false; |
158 | 108k | } |
159 | | |
160 | 0 | if (length >= 4 && is_number(version + 4, length - 4)) { Branch (160:9): [True: 0, False: 0]
Branch (160:24): [True: 0, False: 0]
|
161 | 0 | if (memcmp(version, "3.3.", 4) == 0) { Branch (161:13): [True: 0, False: 0]
|
162 | 0 | options->version = PM_OPTIONS_VERSION_CRUBY_3_3; |
163 | 0 | return true; |
164 | 0 | } |
165 | | |
166 | 0 | if (memcmp(version, "3.4.", 4) == 0) { Branch (166:13): [True: 0, False: 0]
|
167 | 0 | options->version = PM_OPTIONS_VERSION_CRUBY_3_4; |
168 | 0 | return true; |
169 | 0 | } |
170 | | |
171 | 0 | if (memcmp(version, "3.5.", 4) == 0 || memcmp(version, "4.0.", 4) == 0) { Branch (171:13): [True: 0, False: 0]
Branch (171:48): [True: 0, False: 0]
|
172 | 0 | options->version = PM_OPTIONS_VERSION_CRUBY_4_0; |
173 | 0 | return true; |
174 | 0 | } |
175 | | |
176 | 0 | if (memcmp(version, "4.1.", 4) == 0) { Branch (176:13): [True: 0, False: 0]
|
177 | 0 | options->version = PM_OPTIONS_VERSION_CRUBY_4_1; |
178 | 0 | return true; |
179 | 0 | } |
180 | 0 | } |
181 | | |
182 | 0 | if (length == 6) { Branch (182:9): [True: 0, False: 0]
|
183 | 0 | if (memcmp(version, "latest", 6) == 0) { Branch (183:13): [True: 0, False: 0]
|
184 | 0 | options->version = PM_OPTIONS_VERSION_LATEST; |
185 | 0 | return true; |
186 | 0 | } |
187 | 0 | } |
188 | | |
189 | 0 | return false; |
190 | 0 | } |
191 | | |
192 | | /** |
193 | | * Set the version option on the given options struct to the lowest version of |
194 | | * Ruby that prism supports. |
195 | | */ |
196 | | void |
197 | | pm_options_version_set_lowest(pm_options_t *options) { |
198 | | options->version = PM_OPTIONS_VERSION_CRUBY_3_3; |
199 | | } |
200 | | |
201 | | /** |
202 | | * Set the version option on the given options struct to the highest version of |
203 | | * Ruby that prism supports. |
204 | | */ |
205 | | void |
206 | | pm_options_version_set_highest(pm_options_t *options) { |
207 | | options->version = PM_OPTIONS_VERSION_LATEST; |
208 | | } |
209 | | |
210 | | /** |
211 | | * Set the main script option on the given options struct. |
212 | | */ |
213 | | void |
214 | 131k | pm_options_main_script_set(pm_options_t *options, bool main_script) { |
215 | 131k | options->main_script = main_script; |
216 | 131k | } |
217 | | |
218 | | /** |
219 | | * Set the partial script option on the given options struct. |
220 | | */ |
221 | | void |
222 | 131k | pm_options_partial_script_set(pm_options_t *options, bool partial_script) { |
223 | 131k | options->partial_script = partial_script; |
224 | 131k | } |
225 | | |
226 | | /** |
227 | | * Get the freeze option on the given options struct. |
228 | | */ |
229 | | bool |
230 | | pm_options_freeze(const pm_options_t *options) { |
231 | | return options->freeze; |
232 | | } |
233 | | |
234 | | /** |
235 | | * Set the freeze option on the given options struct. |
236 | | */ |
237 | | void |
238 | 131k | pm_options_freeze_set(pm_options_t *options, bool freeze) { |
239 | 131k | options->freeze = freeze; |
240 | 131k | } |
241 | | |
242 | | /** |
243 | | * Get the attach_comments option on the given options struct. |
244 | | */ |
245 | | bool |
246 | | pm_options_attach_comments(const pm_options_t *options) { |
247 | | return options->attach_comments; |
248 | | } |
249 | | |
250 | | /** |
251 | | * Set the attach_comments option on the given options struct. |
252 | | */ |
253 | | void |
254 | 131k | pm_options_attach_comments_set(pm_options_t *options, bool attach_comments) { |
255 | 131k | options->attach_comments = attach_comments; |
256 | 131k | } |
257 | | |
258 | | /** |
259 | | * Get the raise_error option on the given options struct. |
260 | | */ |
261 | | uint8_t pm_options_raise_error(const pm_options_t *options) { |
262 | | return options->raise_error; |
263 | | } |
264 | | |
265 | | /** |
266 | | * Set the raise_error option on the given options struct. |
267 | | */ |
268 | | void pm_options_raise_error_set(pm_options_t *options, uint8_t raise_error) { |
269 | | options->raise_error = raise_error; |
270 | | } |
271 | | |
272 | | /** |
273 | | * Set the stack_budget option on the given options struct. |
274 | | */ |
275 | | void |
276 | | pm_options_stack_budget_set(pm_options_t *options, size_t stack_budget) { |
277 | | options->stack_budget = stack_budget; |
278 | | } |
279 | | |
280 | | // For some reason, GCC analyzer thinks we're leaking allocated scopes and |
281 | | // locals here, even though we definitely aren't. This is a false positive. |
282 | | // Ideally we wouldn't need to suppress this. |
283 | | #if defined(__GNUC__) && (__GNUC__ >= 10) |
284 | | #pragma GCC diagnostic push |
285 | | #pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak" |
286 | | #endif |
287 | | |
288 | | /** |
289 | | * Allocate and zero out the scopes array on the given options struct. |
290 | | */ |
291 | | bool |
292 | 18.5k | pm_options_scopes_init(pm_options_t *options, size_t scopes_count) { |
293 | 18.5k | options->scopes_count = scopes_count; |
294 | 18.5k | options->scopes = xcalloc(scopes_count, sizeof(pm_options_scope_t)); |
295 | 18.5k | return options->scopes != NULL; |
296 | 18.5k | } |
297 | | |
298 | | /** |
299 | | * Return a constant pointer to the scope at the given index within the given |
300 | | * options. |
301 | | */ |
302 | | const pm_options_scope_t * |
303 | 40.2k | pm_options_scope(const pm_options_t *options, size_t index) { |
304 | 40.2k | return &options->scopes[index]; |
305 | 40.2k | } |
306 | | |
307 | | /** |
308 | | * Return a mutable pointer to the scope at the given index within the given |
309 | | * options. |
310 | | */ |
311 | | pm_options_scope_t * |
312 | 40.2k | pm_options_scope_mut(pm_options_t *options, size_t index) { |
313 | 40.2k | return &options->scopes[index]; |
314 | 40.2k | } |
315 | | |
316 | | /** |
317 | | * Create a new options scope struct. This will hold a set of locals that are in |
318 | | * scope surrounding the code that is being parsed. |
319 | | */ |
320 | | void |
321 | 40.2k | pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) { |
322 | 40.2k | scope->locals_count = locals_count; |
323 | 40.2k | scope->locals = xcalloc(locals_count, sizeof(pm_string_t)); |
324 | 40.2k | scope->forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE; |
325 | 40.2k | if (scope->locals == NULL) PRISM_OOM0 (); Branch (325:9): [True: 0, False: 40.2k]
|
326 | 40.2k | } |
327 | | |
328 | | /** |
329 | | * Return a constant pointer to the local at the given index within the given |
330 | | * scope. |
331 | | */ |
332 | | const pm_string_t * |
333 | 99.0k | pm_options_scope_local(const pm_options_scope_t *scope, size_t index) { |
334 | 99.0k | return &scope->locals[index]; |
335 | 99.0k | } |
336 | | |
337 | | /** |
338 | | * Return a mutable pointer to the local at the given index within the given |
339 | | * scope. |
340 | | */ |
341 | | pm_string_t * |
342 | 99.0k | pm_options_scope_local_mut(pm_options_scope_t *scope, size_t index) { |
343 | 99.0k | return &scope->locals[index]; |
344 | 99.0k | } |
345 | | |
346 | | /** |
347 | | * Set the forwarding option on the given scope struct. |
348 | | */ |
349 | | void |
350 | 40.2k | pm_options_scope_forwarding_set(pm_options_scope_t *scope, uint8_t forwarding) { |
351 | 40.2k | scope->forwarding = forwarding; |
352 | 40.2k | } |
353 | | |
354 | | /** |
355 | | * Read a 32-bit unsigned integer from a pointer. This function is used to read |
356 | | * the options that are passed into the parser from the Ruby implementation. It |
357 | | * handles aligned and unaligned reads. |
358 | | */ |
359 | | static uint32_t |
360 | | pm_options_read_u32(const char *data) { |
361 | | if (((uintptr_t) data) % sizeof(uint32_t) == 0) { |
362 | | return *((uint32_t *) data); |
363 | | } else { |
364 | | uint32_t value; |
365 | | memcpy(&value, data, sizeof(uint32_t)); |
366 | | return value; |
367 | | } |
368 | | } |
369 | | |
370 | | /** |
371 | | * Read a 32-bit signed integer from a pointer. This function is used to read |
372 | | * the options that are passed into the parser from the Ruby implementation. It |
373 | | * handles aligned and unaligned reads. |
374 | | */ |
375 | | static int32_t |
376 | | pm_options_read_s32(const char *data) { |
377 | | if (((uintptr_t) data) % sizeof(int32_t) == 0) { |
378 | | return *((int32_t *) data); |
379 | | } else { |
380 | | int32_t value; |
381 | | memcpy(&value, data, sizeof(int32_t)); |
382 | | return value; |
383 | | } |
384 | | } |
385 | | |
386 | | /** |
387 | | * Deserialize an options struct from the given binary string. This is used to |
388 | | * pass options to the parser from an FFI call so that consumers of the library |
389 | | * from an FFI perspective don't have to worry about the structure of our |
390 | | * options structs. Since the source of these calls will be from Ruby |
391 | | * implementation internals we assume it is from a trusted source. |
392 | | */ |
393 | | void |
394 | | pm_options_read(pm_options_t *options, const char *data) { |
395 | | options->line = 1; // default |
396 | | options->stack_budget = SIZE_MAX; // default |
397 | | if (data == NULL) return; |
398 | | |
399 | | uint32_t filepath_length = pm_options_read_u32(data); |
400 | | data += 4; |
401 | | |
402 | | if (filepath_length > 0) { |
403 | | pm_string_init(&options->filepath, (const uint8_t *) data, filepath_length); |
404 | | data += filepath_length; |
405 | | } |
406 | | |
407 | | options->line = pm_options_read_s32(data); |
408 | | data += 4; |
409 | | |
410 | | uint32_t encoding_length = pm_options_read_u32(data); |
411 | | data += 4; |
412 | | |
413 | | if (encoding_length > 0) { |
414 | | pm_string_init(&options->encoding, (const uint8_t *) data, encoding_length); |
415 | | data += encoding_length; |
416 | | } |
417 | | |
418 | | options->frozen_string_literal = (int8_t) *data++; |
419 | | options->command_line = (uint8_t) *data++; |
420 | | options->version = (pm_options_version_t) *data++; |
421 | | options->encoding_locked = ((uint8_t) *data++) > 0; |
422 | | options->main_script = ((uint8_t) *data++) > 0; |
423 | | options->partial_script = ((uint8_t) *data++) > 0; |
424 | | options->freeze = ((uint8_t) *data++) > 0; |
425 | | options->attach_comments = ((uint8_t) *data++) > 0; |
426 | | |
427 | | uint32_t scopes_count = pm_options_read_u32(data); |
428 | | data += 4; |
429 | | |
430 | | if (scopes_count > 0) { |
431 | | if (!pm_options_scopes_init(options, scopes_count)) return; |
432 | | |
433 | | for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) { |
434 | | uint32_t locals_count = pm_options_read_u32(data); |
435 | | data += 4; |
436 | | |
437 | | pm_options_scope_t *scope = &options->scopes[scope_index]; |
438 | | pm_options_scope_init(scope, locals_count); |
439 | | |
440 | | uint8_t forwarding = (uint8_t) *data++; |
441 | | pm_options_scope_forwarding_set(&options->scopes[scope_index], forwarding); |
442 | | |
443 | | for (size_t local_index = 0; local_index < locals_count; local_index++) { |
444 | | uint32_t local_length = pm_options_read_u32(data); |
445 | | data += 4; |
446 | | |
447 | | pm_string_init(&scope->locals[local_index], (const uint8_t *) data, local_length); |
448 | | data += local_length; |
449 | | } |
450 | | } |
451 | | } |
452 | | } |
453 | | |
454 | | #if defined(__GNUC__) && (__GNUC__ >= 10) |
455 | | #pragma GCC diagnostic pop |
456 | | #endif |