Skip to content

Commit 4233aed

Browse files
committed
refactor warnings/errors again (this is the last time i promise)
this version allows using "goto references" on the error/warning definitions. means i had to complicate the error/warn throwing code a little though
1 parent f099fa9 commit 4233aed

7 files changed

Lines changed: 236 additions & 224 deletions

File tree

docs/parse_warnings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import re
33
import string
44

5-
warning_pattern = re.compile(r'WRN\((.*?),\s*"(.*)"\s*,\s*(true|false)\)')
6-
error_pattern = re.compile(r'ERR\((.*?),\s*"(.*)"\)')
5+
warning_pattern = re.compile(r'WRN\(warn_(.*?),\s*"(.*)"\s*,\s*(true|false)\)')
6+
error_pattern = re.compile(r'ERR\(err_(.*?),\s*"(.*)"\)')
77
escaping = str.maketrans({x: '\\'+x for x in string.punctuation})
88

99
def escape(s):

src/asar/errors.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
#include "std-includes.h"
55
#include "errors.h"
66
#include "interface-shared.h"
7+
#include "libstr.h"
78

89
void error_impl(const char* error_id, const char* fmt_string, int whichpass, ...) {
910
va_list args; va_start(args, whichpass);
1011
char error_buffer[1024];
1112
vsnprintf(error_buffer, sizeof(error_buffer), fmt_string, args);
1213

13-
error_interface(error_id, whichpass, error_buffer);
14+
// convert err_xxx -> Exxx
15+
string errname = STR "E" + (error_id + 4);
16+
17+
error_interface(errname.data(), whichpass, error_buffer);
1418
}

src/asar/errors.h

Lines changed: 189 additions & 188 deletions
Large diffs are not rendered by default.

src/asar/interface-cli.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ void error_interface(const char* errid, int whichpass, const char * e_)
125125
errnum++;
126126
const char* current_block = get_current_block();
127127
// don't show current block if the error came from an error command or limit reached
128-
bool show_block = (current_block && (strcmp(errid, "error_command") != 0 && strcmp(errid, "limit_reached") != 0));
129-
bool show_stack = strcmp(errid, "limit_reached") != 0;
128+
bool show_block = (current_block && (strcmp(errid, "Eerror_command") != 0 && strcmp(errid, "Elimit_reached") != 0));
129+
bool show_stack = strcmp(errid, "Elimit_reached") != 0;
130130
string location;
131131
string details;
132132
get_current_line_details(&location, &details, !show_block);
@@ -146,12 +146,13 @@ static bool warned=false;
146146
void warn(int errid, const char * e_)
147147
{
148148
const char* current_block = get_current_block();
149+
const char* warnname = get_warning_name((asar_warning_id)errid);
149150
// don't show current block if the warning came from a warn command
150-
bool show_block = (current_block && (errid != warn_id_warn_command));
151+
bool show_block = (current_block && (strcmp(warnname, "Wwarn_command") != 0));
151152
string location;
152153
string details;
153154
get_current_line_details(&location, &details, !show_block);
154-
string warning_string = location+"warning: (" + get_warning_name((asar_warning_id)errid) + "): " + e_;
155+
string warning_string = location+"warning: (" + warnname + "): " + e_;
155156
string details_string = details + get_callstack() + "\n";
156157
set_text_color(errloc, &warning_string, ansi_text_color::BRIGHT_YELLOW);
157158
fputs(warning_string, errloc);

src/asar/interface-lib.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,18 @@ void error_interface(const char* errid, int whichpass, const char * e_)
139139
if (ismath) matherror = e_;
140140
else if (pass == whichpass) {
141141
// don't show current block if the error came from an error command
142-
bool show_block = (strcmp(errid, "error_command") != 0);
142+
bool show_block = (strcmp(errid, "Eerror_command") != 0);
143143
fillerror(errors[numerror++], errid, STR "error: (" + errid + "): ", e_, show_block);
144144
}
145145
else {}//ignore anything else
146146
}
147147

148148
void warn(int errid, const char * str)
149149
{
150+
const char* warnname = get_warning_name((asar_warning_id)errid);
150151
// don't show current block if the warning came from a warn command
151-
bool show_block = (errid != warn_id_warn_command);
152-
fillerror(warnings[numwarn++], get_warning_name((asar_warning_id)errid), STR "warning: (" + get_warning_name((asar_warning_id)errid) + "): ", str, show_block);
152+
bool show_block = (strcmp(warnname, "Wwarn_command") != 0);
153+
fillerror(warnings[numwarn++], warnname, STR "warning: (" + warnname + "): ", str, show_block);
153154
}
154155

155156
static autoarray<labeldata> ldata;

src/asar/warnings.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
#include <vector>
2+
#include "libstr.h"
23

34
// construct a mapping between warning ids and warning names.
45
struct warn_t;
56
std::vector<warn_t> all_warnings;
67
int warning_id_end = 0;
78
struct warn_t {
8-
const char *name;
9+
string name;
910
bool exists;
1011
bool is_default_enabled;
1112
warn_t() : name(nullptr), exists(false), is_default_enabled(false) {}
12-
warn_t(int id, const char *name, bool is_default_enabled)
13-
: name(name), exists(true), is_default_enabled(is_default_enabled) {
13+
warn_t(int id, const char *name_in, bool is_default_enabled)
14+
: exists(true), is_default_enabled(is_default_enabled) {
1415
if((int)all_warnings.size() <= id) all_warnings.resize(id + 1);
16+
// convert warn_xxx -> Wxxx
17+
name = STR "W" + (name_in + 5);
1518
all_warnings[id] = *this;
1619
warning_id_end = all_warnings.size();
1720
}
1821
};
1922

20-
#define SAVE_WARN_NAME(id, name, is_enabled) static warn_t _useless_##id { id, name, is_enabled }
23+
// i love this fucking language
24+
#define CAT(a,b) a##b
25+
#define CAT2(a,b) CAT(a,b)
26+
#define SAVE_WARN_NAME(id, name, is_enabled) static warn_t CAT2(_useless_,__LINE__) { id, name, is_enabled }
2127
#include "warnings.h"
2228

2329
#include "asar.h"
@@ -63,7 +69,7 @@ const char* get_warning_name(asar_warning_id warnid)
6369
const warn_t& warning = all_warnings[warnid];
6470
assert(warning.exists);
6571

66-
return warning.name;
72+
return warning.name.data();
6773
}
6874

6975

@@ -92,7 +98,7 @@ asar_warning_id parse_warning_id_from_string(const char* string)
9298
}
9399
for(int i = 0; i < warning_id_end; i++)
94100
{
95-
if(all_warnings[i].exists && !stricmpwithlower(pos, all_warnings[i].name+1))
101+
if(all_warnings[i].exists && !stricmpwithlower(pos, all_warnings[i].name.data()+1))
96102
{
97103
return asar_warning_id(i);
98104
}

src/asar/warnings.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,28 @@ void warn_impl(int warn_id, const char* fmt_string, int whichpass, ...);
1414
#endif
1515

1616
#define WRN(id, fmt_string, is_enabled) \
17-
static constexpr int warn_id_##id = __LINE__ - _warn_startline; \
18-
SAVE_WARN_NAME(warn_id_##id, "W" #id, is_enabled); \
19-
template<typename... Ts> inline void warn_ ## id(int whichpass, Ts... args) { \
20-
warn_impl(warn_id_##id, fmt_string, whichpass, args...); \
17+
SAVE_WARN_NAME(__LINE__ - _warn_startline, #id, is_enabled); \
18+
template<typename... Ts> inline void id(int whichpass, Ts... args) { \
19+
warn_impl(__LINE__ - _warn_startline, fmt_string, whichpass, args...); \
2120
}
2221

2322
static constexpr int _warn_startline = __LINE__;
24-
WRN(relative_path_used, "Relative %s path passed to asar_patch_ex() - please use absolute paths only to prevent undefined behavior!", true)
25-
WRN(rom_too_short, "ROM is too short to have a title. (Expected '%s')", true)
26-
WRN(rom_title_incorrect, "ROM title is incorrect. Expected '%s', got '%s'.", true)
27-
WRN(spc700_assuming_8_bit, "This opcode does not exist with 16-bit parameters, assuming 8-bit.", true)
28-
WRN(assuming_address_mode, "The addressing mode %s is not valid for this instruction, assuming %s.%s", true)
29-
WRN(set_middle_byte, "It would be wise to set the 008000 bit of this address.", true)
30-
WRN(freespace_leaked, "This freespace appears to be leaked.", true)
31-
WRN(warn_command, "warn command%s", true)
32-
WRN(implicitly_sized_immediate, "Implicitly sized immediate.", false)
33-
WRN(check_memory_file, "Accessing file '%s' which is not in memory while Wcheck_memory_file is enabled.", false)
34-
WRN(datasize_last_label, "Datasize used on last detected label '%s'.", true)
35-
WRN(datasize_exceeds_size, "Datasize exceeds 0xFFFF for label '%s'.", true)
36-
WRN(mapper_already_set, "A mapper has already been selected.", true)
37-
WRN(feature_deprecated, "DEPRECATION NOTIFICATION: Feature \"%s\" is deprecated and will be REMOVED in the future. Please update your code to conform to newer styles. Suggested work around: %s.", true)
38-
WRN(invalid_warning_id, "Warning '%s' (passed to %s) doesn't exist.", true)
39-
WRN(byte_order_mark_utf8, "UTF-8 byte order mark detected and skipped.", true)
23+
WRN(warn_relative_path_used, "Relative %s path passed to asar_patch_ex() - please use absolute paths only to prevent undefined behavior!", true)
24+
WRN(warn_rom_too_short, "ROM is too short to have a title. (Expected '%s')", true)
25+
WRN(warn_rom_title_incorrect, "ROM title is incorrect. Expected '%s', got '%s'.", true)
26+
WRN(warn_spc700_assuming_8_bit, "This opcode does not exist with 16-bit parameters, assuming 8-bit.", true)
27+
WRN(warn_assuming_address_mode, "The addressing mode %s is not valid for this instruction, assuming %s.%s", true)
28+
WRN(warn_set_middle_byte, "It would be wise to set the 008000 bit of this address.", true)
29+
WRN(warn_freespace_leaked, "This freespace appears to be leaked.", true)
30+
WRN(warn_warn_command, "warn command%s", true)
31+
WRN(warn_implicitly_sized_immediate, "Implicitly sized immediate.", false)
32+
WRN(warn_check_memory_file, "Accessing file '%s' which is not in memory while Wcheck_memory_file is enabled.", false)
33+
WRN(warn_datasize_last_label, "Datasize used on last detected label '%s'.", true)
34+
WRN(warn_datasize_exceeds_size, "Datasize exceeds 0xFFFF for label '%s'.", true)
35+
WRN(warn_mapper_already_set, "A mapper has already been selected.", true)
36+
WRN(warn_feature_deprecated, "DEPRECATION NOTIFICATION: Feature \"%s\" is deprecated and will be REMOVED in the future. Please update your code to conform to newer styles. Suggested work around: %s.", true)
37+
WRN(warn_invalid_warning_id, "Warning '%s' (passed to %s) doesn't exist.", true)
38+
WRN(warn_byte_order_mark_utf8, "UTF-8 byte order mark detected and skipped.", true)
4039

4140
#undef WRN
4241
#define throw_warning(whichpass, id, ...) id(whichpass, ## __VA_ARGS__)

0 commit comments

Comments
 (0)