asar coverage - build #98


src/asar/
File: src/asar/errors.cpp
Date: 2024-01-19 19:03:15
Lines:
34/37
91.9%
Functions:
7/7
100.0%
Branches:
19/30
63.3%

Line Branch Exec Source
1 #include "asar.h"
2 #include <cassert>
3 #include <cstdarg>
4
5 #include "interface-shared.h"
6
7 static int asar_num_errors = 0;
8
9 struct asar_error_mapping
10 {
11 asar_error_id errid;
12 const char* name;
13 const char* message;
14
15 52779 asar_error_mapping(asar_error_id inerrid, const char* iname, const char* inmessage)
16 52779 {
17 52779 ++asar_num_errors;
18
19 52779 errid = inerrid;
20 52779 name = iname;
21 52779 message = inmessage;
22
23 // RPG Hacker: Sanity check. This makes sure that the order
24 // of entries in asar_errors matches with the order of
25 // constants in asar_error_id. This is important because
26 // we access asar_errors as an array.
27 // Would love to do this via static_assert(), but can't
28 // think of a way to do so.
29
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52779 times.
52779 assert(errid - error_id_start == asar_num_errors);
30 52779 }
31 };
32
33 // Keep in sync with asar_error_id.
34 // Both, enum mapping and order, must match.
35 #define ERR(name) error_id_ ## name, "E" #name
36 static asar_error_mapping asar_errors[] =
37 {
38 { ERR(limit_reached), "Over %d errors detected. Aborting." },
39 { ERR(werror), "One or more warnings was detected with werror on." },
40
41 { ERR(16mb_rom_limit), "Can't create ROMs larger than 16MB." },
42 { ERR(buffer_too_small), "The given buffer is too small to contain the resulting ROM." },
43 { ERR(params_null), "params passed to asar_patch_ex() is null." },
44 { ERR(params_invalid_size), "Size of params passed to asar_patch_ex() is invalid." },
45 { ERR(cmdl_define_invalid), "Invalid define name in %s: '%s'." },
46 { ERR(cmdl_define_override), "%s '%s' overrides a previous define. Did you specify the same define twice?" },
47 { ERR(create_rom_failed), "Couldn't create ROM." },
48 { ERR(open_rom_failed), "Couldn't open ROM." },
49 { ERR(open_rom_not_smw_extension), "Doesn't look like an SMW ROM. (Maybe its extension is wrong?)" },
50 { ERR(open_rom_not_smw_header), "Doesn't look like an SMW ROM. (Maybe it's headered?)" },
51 { ERR(stddefines_no_identifier), "stddefines.txt contains a line with a value, but no identifier." },
52 { ERR(stddefine_after_closing_quote), "Broken std defines. (Something after closing quote)" },
53
54 { ERR(failed_to_open_file), "Failed to open file '%s'." },
55 { ERR(file_not_found), "File '%s' wasn't found." },
56 { ERR(readfile_1_to_4_bytes), "Can only read chunks of 1 to 4 bytes." },
57 { ERR(canreadfile_0_bytes), "Number of bytes to check must be > 0." },
58 { ERR(file_offset_out_of_bounds), "File offset %s out of bounds for file '%s'." },
59 { ERR(rep_at_file_end), "rep at the end of a file" },
60
61 { ERR(mismatched_parentheses), "Mismatched parentheses." },
62 { ERR(invalid_hex_value), "Invalid hex value." },
63 { ERR(invalid_binary_value), "Invalid binary value." },
64 { ERR(invalid_character), "Invalid character." },
65 { ERR(string_literal_not_terminated), "String literal not terminated." },
66 { ERR(malformed_function_call), "Malformed function call." },
67 { ERR(invalid_number), "Invalid number." },
68 { ERR(garbage_near_quoted_string), "Garbage near quoted string." },
69 { ERR(mismatched_quotes), "Mismatched quotes." },
70
71 { ERR(rom_too_short), "ROM is too short to have a title. (Expected '%s')" },
72 { ERR(rom_title_incorrect), "ROM title is incorrect. Expected '%s', got '%s'." },
73
74 { ERR(bank_border_crossed), "A bank border was crossed, SNES address $%06X." },
75
76 { ERR(start_of_file), "This command may only be used at the start of a file." },
77 { ERR(xkas_asar_conflict), "Using @xkas and @asar in the same patch is not supported." },
78 { ERR(invalid_version_number), "Invalid version number." },
79 { ERR(asar_too_old), "This version of Asar is too old for this patch." },
80
81 { ERR(relative_branch_out_of_bounds), "Relative branch out of bounds. (Distance is %s)." },
82 { ERR(snes_address_doesnt_map_to_rom), "SNES address %s doesn't map to ROM." },
83 { ERR(snes_address_out_of_bounds), "SNES address %s out of bounds." },
84 { ERR(invalid_tcall), "Invalid tcall." },
85 { ERR(use_xplus), "Use (x+) instead." },
86 { ERR(opcode_length_too_long), "Opcode length is too long." },
87 { ERR(superfx_invalid_short_address), "Invalid short address parameter: $%s. (Must be even number and $0000-$01FE)" },
88 { ERR(superfx_invalid_register), "Invalid register for opcode; valid range is %d-%d." },
89 { ERR(invalid_opcode_length), "Invalid opcode length specification." },
90 { ERR(invalid_mapper), "Invalid mapper." },
91
92 { ERR(nan), "NaN encountered." },
93 { ERR(division_by_zero), "Division by zero." },
94 { ERR(modulo_by_zero), "Modulo by zero." },
95 { ERR(unknown_operator), "Unknown operator." },
96 { ERR(invalid_input), "Invalid input." },
97
98 { ERR(invalid_function_name), "Invalid function name." },
99 { ERR(function_not_found), "Function '%s' wasn't found." },
100 { ERR(function_redefined), "Function '%s' redefined." },
101 { ERR(broken_function_declaration), "Broken function declaration." },
102 { ERR(wrong_num_parameters), "Wrong number of parameters to function." },
103 { ERR(invalid_param_name), "Invalid parameter name." },
104 { ERR(math_invalid_type), "Wrong type for parameter %s, expected %s." },
105
106 { ERR(invalid_label_name), "Invalid label name." },
107 { ERR(label_not_found), "Label '%s' wasn't found." },
108 { ERR(label_redefined), "Label '%s' redefined." },
109 { ERR(broken_label_definition), "Broken label definition." },
110 { ERR(label_cross_assignment), "Setting labels to other non-static labels is not valid." },
111 { ERR(macro_label_outside_of_macro), "Macro label outside of a macro." },
112 { ERR(label_on_third_pass), "Internal error: A label was created on the third pass. Please create an issue on the official GitHub repository and attach a patch which reproduces the error." },
113 { ERR(label_moving), "Internal error: A label is moving around. Please create an issue on the official GitHub repository and attach a patch which reproduces the error." },
114
115 { ERR(invalid_namespace_name), "Invalid namespace name." },
116 { ERR(invalid_namespace_use), "Invalid use of namespace command." },
117
118 { ERR(invalid_struct_name), "Invalid struct name." },
119 { ERR(struct_not_found), "Struct '%s' wasn't found." },
120 { ERR(struct_redefined), "Struct '%s' redefined." },
121 { ERR(struct_invalid_parent_name), "Invalid parent name." },
122 { ERR(invalid_label_missing_closer), "Invalid label name, missing array closer." },
123 { ERR(multiple_subscript_operators), "Multiple subscript operators is invalid." },
124 { ERR(invalid_subscript), "Invalid array subscript after first scope resolution." },
125 { ERR(label_missing_parent), "This label has no parent." },
126 { ERR(array_invalid_inside_structs), "Array syntax invalid inside structs." },
127 { ERR(struct_without_endstruct), "struct without matching endstruct." },
128 { ERR(nested_struct), "Can not nest structs." },
129 { ERR(missing_struct_params), "Missing struct parameters." },
130 { ERR(too_many_struct_params), "Too many struct parameters." },
131 { ERR(missing_extends), "Missing extends keyword." },
132 { ERR(invalid_endstruct_count), "Invalid endstruct parameter count." },
133 { ERR(expected_align), "Expected align parameter." },
134 { ERR(endstruct_without_struct), "endstruct can only be used in combination with struct." },
135 { ERR(alignment_too_small), "Alignment must be >= 1." },
136
137 { ERR(invalid_define_name), "Invalid define name." },
138 { ERR(define_not_found), "Define '%s' wasn't found." },
139 { ERR(broken_define_declaration), "Broken define declaration." },
140 { ERR(overriding_builtin_define), "Trying to set define '%s', which is the name of a built-in define and thus can't be modified." },
141 { ERR(define_label_math), "!Define #= Label is not allowed with non-static labels." },
142 { ERR(mismatched_braces), "Mismatched braces." },
143
144 { ERR(invalid_macro_name), "Invalid macro name." },
145 { ERR(macro_not_found), "Macro '%s' wasn't found." },
146 { ERR(macro_redefined), "Macro '%s' redefined." },
147 { ERR(broken_macro_declaration), "Broken macro declaration." },
148 { ERR(invalid_macro_param_name), "Invalid macro parameter name." },
149 { ERR(macro_param_not_found), "Macro parameter '%s' wasn't found.%s" },
150 { ERR(macro_param_redefined), "Macro parameter '%s' redefined" },
151 { ERR(broken_macro_usage), "Broken macro usage." },
152 { ERR(macro_wrong_num_params), "Wrong number of parameters to macro." },
153 { ERR(broken_macro_contents), "Broken macro contents." },
154 { ERR(rep_at_macro_end), "rep or if at the end of a macro." },
155 { ERR(nested_macro_definition), "Nested macro definition: Trying to define a macro inside '%s', which is not supported." },
156 { ERR(misplaced_endmacro), "Misplaced endmacro." },
157 { ERR(unclosed_macro), "Unclosed macro: '%s'." },
158
159 { ERR(label_in_conditional), "Non-static label in %s command." },
160 { ERR(broken_conditional), "Broken %s command." },
161 { ERR(invalid_condition), "Invalid condition." },
162 { ERR(misplaced_elseif), "Misplaced elseif." },
163 { ERR(elseif_in_while), "Can't use elseif in a while loop." },
164 { ERR(elseif_in_singleline), "Can't use elseif on single-line statements." },
165 { ERR(misplaced_endif), "Misplaced endif." },
166 { ERR(misplaced_else), "Misplaced else." },
167 { ERR(else_in_while_loop), "Can't use else in a while loop." },
168 { ERR(unclosed_if), "Unclosed if statement." },
169
170 { ERR(unknown_command), "Unknown command." },
171 { ERR(command_disabled), "This command is disabled." },
172
173 { ERR(broken_incbin), "Broken incbin command." },
174 { ERR(incbin_64kb_limit), "Can't include more than 64 kilobytes at once." },
175 { ERR(recursion_limit), "Recursion limit reached." },
176 { ERR(command_in_non_root_file), "This command may only be used in the root file." },
177 { ERR(cant_be_main_file), "This file may not be used as the main file.%s" },
178 { ERR(no_labels_here), "Can't use non-static labels here." },
179
180 { ERR(invalid_freespace_request), "Invalid freespace request." },
181 { ERR(no_banks_with_ram_mirrors), "No banks contain the RAM mirrors in hirom or exhirom." },
182 { ERR(no_freespace_norom), "Can't find freespace in norom." },
183 { ERR(static_freespace_autoclean), "A static freespace must be targeted by at least one autoclean." },
184 { ERR(static_freespace_growing), "A static freespace may not grow." },
185 { ERR(no_freespace_in_mapped_banks), "No freespace found in the mapped banks. (Requested size: %s)" },
186 { ERR(no_freespace), "No freespace found. (Requested size: %s)" },
187 { ERR(freespace_limit_reached), "A patch may not contain more than %d freespaces." },
188
189 { ERR(prot_not_at_freespace_start), "PROT must be used at the start of a freespace block." },
190 { ERR(prot_too_many_entries), "Too many entries to PROT." },
191
192 { ERR(autoclean_in_freespace), "autoclean used in freespace." },
193 { ERR(autoclean_label_at_freespace_end), "Don't autoclean a label at the end of a freespace block, you'll remove some stuff you're not supposed to remove." },
194 { ERR(broken_autoclean), "Broken autoclean command." },
195
196 { ERR(pulltable_without_table), "Using pulltable when there is no table on the stack yet." },
197 { ERR(invalid_table_file), "Invalid table file. Invalid entry on line: %i" },
198
199 { ERR(pad_in_freespace), "pad does not make sense in a freespaced code." },
200
201 { ERR(org_label_invalid), "org Label is not valid." },
202 { ERR(org_label_forward), "org Label is only valid for labels earlier in the patch." },
203
204 { ERR(skip_label_invalid), "skip Label is not valid." },
205
206 { ERR(spc700_inline_no_base), "base is not implemented for architecture spc700-inline." },
207 { ERR(base_label_invalid), "base Label is not valid." },
208
209 { ERR(rep_label), "rep Label is not valid." },
210
211 { ERR(pushpc_without_pullpc), "pushpc without matching pullpc." },
212 { ERR(pullpc_without_pushpc), "pullpc without matching pushpc." },
213 { ERR(pullpc_different_arch), "pullpc in another architecture than the pushpc." },
214 { ERR(pullbase_without_pushbase), "pullbase without matching pushbase." },
215
216 { ERR(invalid_math), "Invalid math command." },
217 { ERR(invalid_warn), "Invalid warn command." },
218 { ERR(invalid_check), "Invalid check command." },
219
220 { ERR(warnpc_in_freespace), "warnpc used in freespace." },
221 { ERR(warnpc_broken_param), "Broken warnpc parameter." },
222 { ERR(warnpc_failed), "warnpc failed: Current position (%s) is after end position (%s)." },
223 { ERR(warnpc_failed_equal), "warnpc failed: Current position (%s) is equal to end position (%s)." },
224
225 { ERR(assertion_failed), "Assertion failed%s" },
226
227 { ERR(error_command), "error command%s" },
228
229 { ERR(invalid_print_function_syntax), "Invalid printable string syntax." },
230 { ERR(unknown_variable), "Unknown variable." },
231
232 { ERR(invalid_warning_id), "Warning '%s' (passed to %s) doesn't exist." },
233
234 { ERR(pushwarnings_without_pullwarnings), "warnings push without matching warnings pull." },
235 { ERR(pullwarnings_without_pushwarnings), "warnings pull without matching warnings push." },
236
237 { ERR(failed_to_open_file_access_denied), "Failed to open file '%s'. Access denied." },
238 { ERR(failed_to_open_not_regular_file), "Failed to open file '%s'. Not a regular file (did you try to use a directory?)" },
239
240 { ERR(bad_dp_base), "The dp base should be page aligned (i.e. a multiple of 256)"},
241 { ERR(bad_dp_optimize), "Bad dp optimize value %s, expected: [none, ram, always]"},
242 { ERR(bad_address_optimize), "Bad dp optimize value %s, expected: [default, ram, mirrors]"},
243 { ERR(bad_optimize), "Bad optimize value %s, expected: [dp, address]"},
244
245 { ERR(require_parameter), "Missing required function parameter"},
246 { ERR(expected_parameter), "Not enough parameters in calling of function %s"},
247 { ERR(unexpected_parameter), "Too many parameters in calling of function %s"},
248 { ERR(duplicate_param_name), "Duplicated parameter name: %s in creation of function %s"},
249
250 { ERR(invalid_alignment), "Invalid alignment. Expected a power of 2." },
251 { ERR(alignment_too_big), "Requested alignment too large." },
252
253 { ERR(negative_shift), "Bitshift amount is negative." },
254
255 { ERR(macro_not_varadic), "Invalid use of %s, active macro is not variadic." },
256 { ERR(vararg_sizeof_nomacro), "Invalid use of sizeof(...), no active macro." },
257 { ERR(macro_wrong_min_params), "Variadic macro call with too few parameters" },
258 { ERR(vararg_out_of_bounds), "Variadic macro parameter requested is out of bounds." },
259 { ERR(vararg_must_be_last), "Variadic macro parameter must be the last parameter." },
260 { ERR(invalid_global_label), "Global label definition contains an invalid label [%s]."},
261
262 { ERR(spc700_addr_out_of_range), "Address %s out of range for instruction, valid range is 0000-1FFF" },
263 { ERR(label_ambiguous), "Label (%s) location is ambiguous due to straddling optimization border." },
264
265 { ERR(feature_unavaliable_in_spcblock), "This feature may not be used while an spcblock is active" },
266 { ERR(endspcblock_without_spcblock), "Use of endspcblock without matching spcblock" },
267 { ERR(missing_endspcblock), "Use of endspcblock without matching spcblock" },
268 { ERR(spcblock_bad_arch), "spcblock only valid inside spc700 arch" },
269 { ERR(spcblock_inside_struct), "Can not start an spcblock while a struct is still open" },
270 { ERR(spcblock_too_few_args), "Too few args passed to spcblock" },
271 { ERR(spcblock_too_many_args), "Too many args passed to spcblock" },
272 { ERR(unknown_spcblock_type), "Unknown spcblock format" },
273 { ERR(custom_spcblock_missing_macro), "Custom spcblock types must refer to a valid macro" },
274 { ERR(spcblock_macro_doesnt_exist), "Macro specified to custom spcblock was not found"},
275 { ERR(extra_spcblock_arg_for_type), "Only custom spcblock type takes a fourth argument" },
276 { ERR(spcblock_macro_must_be_varadic), "Custom spcblock macros must be variadic" },
277 { ERR(spcblock_macro_invalid_static_args), "Custom spcblock macros must have three static arguments" },
278 { ERR(spcblock_custom_types_incomplete), "Custom spcblock types are not yet supported. One day." },
279 { ERR(startpos_without_spcblock), "The startpos command is only valid in spcblocks" },
280 { ERR(internal_error), "An internal asar error occured (%s). Send help." },
281
282 { ERR(pushns_without_pullns), "pushns without matching pullns." },
283 { ERR(pullns_without_pushns), "pullns without matching pushns." },
284
285 { ERR(label_forward), "The use of forward labels is not allowed in this context." },
286 { ERR(undefined_char), "'%s' is not defined in the character table" },
287
288 { ERR(invalid_utf8), "Invalid text encoding detected. Asar expects UTF-8-encoded text. Please re-save this file in a text editor of choice using UTF-8 encoding." },
289 { ERR(cmdl_utf16_to_utf8_failed), "UTF-16 to UTF-8 string conversion failed: %s." },
290
291 { ERR(broken_command), "Broken %s command. %s" },
292 { ERR(phantom_error), "A phantom error occurred. This is an Asar bug, please report it: https://github.com/RPGHacker/asar/issues" },
293
294 { ERR(oob), "Operation out of bounds: Requested index %d for object of size %d" },
295
296 { ERR(unclosed_vararg), "Variadic macro parameter wasn't closed properly." },
297 { ERR(invalid_vararg), "Trying to use variadic macro parameter syntax to resolve a non variadic argument." },
298
299 { ERR(macro_param_outside_macro), "Reference to macro parameter outside of macro" },
300 { ERR(invalid_depth_resolve), "Invalid %s resolution depth: Trying to backwards-resolve a %s using %i '^', but current scope only supports up to %i '^'." },
301
302 { ERR(platform_paths), "Platform-specific paths aren'supported. Please use platform-independent paths (use / instead of \\)." },
303
304 { ERR(bad_single_line_for), "Single-line for loop not allowed here." },
305 { ERR(broken_for_loop), "Broken for loop command: %s" },
306
307 { ERR(missing_org), "Missing org or freespace command." },
308
309 { ERR(unclosed_block_comment), "Unclosed block comment." },
310 };
311 // RPG Hacker: Sanity check. This makes sure that the element count of asar_error
312 // matches with the number of constants in asar_error_id. This is important, because
313 // we are going to access asar_warnings as an array.
314 static_assert(sizeof(asar_errors) / sizeof(asar_errors[0]) == error_id_count, "asar_errors and asar_error_id are not in sync");
315
316 template<typename t>
317 2882 void asar_error_template(asar_error_id errid, int whichpass, const char* message)
318 {
319 try
320 {
321
1/2
✓ Branch 0 taken 1441 times.
✗ Branch 1 not taken.
2882 error_interface((int)errid, whichpass, message);
322 t err;
323 2882 throw err;
324 }
325
2/2
✓ Branch 0 taken 1333 times.
✓ Branch 1 taken 108 times.
2882 catch (errnull&) {}
326 216 }
327
328 #if !defined(__clang__)
329 void(*shutupgcc1)(asar_error_id, int, const char*) = asar_error_template<errnull>;
330 void(*shutupgcc2)(asar_error_id, int, const char*) = asar_error_template<errblock>;
331 void(*shutupgcc3)(asar_error_id, int, const char*) = asar_error_template<errline>;
332 void(*shutupgcc4)(asar_error_id, int, const char*) = asar_error_template<errfatal>;
333 #endif
334
335 1441 void asar_throw_error(int whichpass, asar_error_type type, asar_error_id errid, ...)
336 {
337
3/6
✓ Branch 0 taken 1441 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✓ Branch 3 taken 717 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1441 assert(errid > error_id_start && errid < error_id_end);
338
339 1441 const asar_error_mapping& error = asar_errors[errid - error_id_start - 1];
340
341 char error_buffer[1024];
342 va_list args;
343 1441 va_start(args, errid);
344
345 #if defined(__clang__)
346 # pragma clang diagnostic push
347 // "format string is not a literal".
348 // The pointer we're passing here should always point to a string literal,
349 // thus, I think, we can safely ignore this here.
350 # pragma clang diagnostic ignored "-Wformat-nonliteral"
351 #endif
352
353
1/2
✓ Branch 0 taken 717 times.
✗ Branch 1 not taken.
1441 vsnprintf(error_buffer, sizeof(error_buffer), error.message, args);
354
355 #if defined(__clang__)
356 # pragma clang diagnostic pop
357 #endif
358
359 1441 va_end(args);
360
361
4/4
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1201 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 24 times.
1441 switch (type)
362 {
363 108 case error_type_null:
364
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 asar_error_template<errnull>(errid, whichpass, error_buffer);
365 108 break;
366 1201 case error_type_block:
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
1201 asar_error_template<errblock>(errid, whichpass, error_buffer);
368 break;
369 108 case error_type_line:
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 asar_error_template<errline>(errid, whichpass, error_buffer);
371 break;
372 24 case error_type_fatal:
373 default:
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 asar_error_template<errfatal>(errid, whichpass, error_buffer);
375 break;
376 }
377 108 }
378
379 994 const char* get_error_name(asar_error_id errid)
380 {
381
3/4
✓ Branch 0 taken 994 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 499 times.
✓ Branch 3 taken 495 times.
994 assert(errid > error_id_start && errid < error_id_end);
382
383 994 const asar_error_mapping& error = asar_errors[errid - error_id_start - 1];
384
385 994 return error.name;
386 }
387