6 const unsigned int opts_opt_oflag[COUNT_OPTIMIZATIONS+1] = {
7 # define GMQCC_TYPE_OPTIMIZATIONS
8 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) MIN_O,
13 const opts_flag_def_t opts_opt_list[COUNT_OPTIMIZATIONS+1] = {
14 # define GMQCC_TYPE_OPTIMIZATIONS
15 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) { #NAME, LONGBIT(OPTIM_##NAME) },
17 { nullptr, LONGBIT(0) }
20 const opts_flag_def_t opts_warn_list[COUNT_WARNINGS+1] = {
21 # define GMQCC_TYPE_WARNS
22 # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(WARN_##X) },
24 { nullptr, LONGBIT(0) }
27 const opts_flag_def_t opts_flag_list[COUNT_FLAGS+1] = {
28 # define GMQCC_TYPE_FLAGS
29 # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(X) },
31 { nullptr, LONGBIT(0) }
34 unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS];
35 opts_cmd_t opts; /* command line options */
37 static void opts_setdefault(void) {
38 memset(&opts, 0, sizeof(opts_cmd_t));
39 OPTS_OPTION_STR(OPTION_PROGSRC) = "progs.src";
42 opts_set(opts.warn, WARN_UNUSED_VARIABLE, true);
43 opts_set(opts.warn, WARN_USED_UNINITIALIZED, true);
44 opts_set(opts.warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
45 opts_set(opts.warn, WARN_EXTENSIONS, true);
46 opts_set(opts.warn, WARN_FIELD_REDECLARED, true);
47 opts_set(opts.warn, WARN_MISSING_RETURN_VALUES, true);
48 opts_set(opts.warn, WARN_INVALID_PARAMETER_COUNT, true);
49 opts_set(opts.warn, WARN_LOCAL_CONSTANTS, true);
50 opts_set(opts.warn, WARN_VOID_VARIABLES, true);
51 opts_set(opts.warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
52 opts_set(opts.warn, WARN_VARIADIC_FUNCTION, true);
53 opts_set(opts.warn, WARN_FRAME_MACROS, true);
54 opts_set(opts.warn, WARN_EFFECTLESS_STATEMENT, true);
55 opts_set(opts.warn, WARN_END_SYS_FIELDS, true);
56 opts_set(opts.warn, WARN_ASSIGN_FUNCTION_TYPES, true);
57 opts_set(opts.warn, WARN_CPP, true);
58 opts_set(opts.warn, WARN_MULTIFILE_IF, true);
59 opts_set(opts.warn, WARN_DOUBLE_DECLARATION, true);
60 opts_set(opts.warn, WARN_CONST_VAR, true);
61 opts_set(opts.warn, WARN_MULTIBYTE_CHARACTER, true);
62 opts_set(opts.warn, WARN_UNKNOWN_PRAGMAS, true);
63 opts_set(opts.warn, WARN_UNREACHABLE_CODE, true);
64 opts_set(opts.warn, WARN_UNKNOWN_ATTRIBUTE, true);
65 opts_set(opts.warn, WARN_RESERVED_NAMES, true);
66 opts_set(opts.warn, WARN_UNINITIALIZED_CONSTANT, true);
67 opts_set(opts.warn, WARN_DEPRECATED, true);
68 opts_set(opts.warn, WARN_PARENTHESIS, true);
69 opts_set(opts.warn, WARN_CONST_OVERWRITE, true);
70 opts_set(opts.warn, WARN_DIRECTIVE_INMACRO, true);
71 opts_set(opts.warn, WARN_BUILTINS, true);
72 opts_set(opts.warn, WARN_INEXACT_COMPARES, true);
75 opts_set(opts.flags, ADJUST_VECTOR_FIELDS, true);
76 opts_set(opts.flags, CORRECT_TERNARY, true);
77 opts_set(opts.flags, BAIL_ON_WERROR, true);
78 opts_set(opts.flags, LEGACY_VECTOR_MATHS, true);
79 opts_set(opts.flags, DARKPLACES_STRING_TABLE_BUG, true);
82 OPTS_OPTION_U32(OPTION_STATE_FPS) = 10;
85 void opts_backup_non_Wall() {
87 for (i = 0; i <= WARN_DEBUG; ++i)
88 opts_set(opts.warn_backup, i, OPTS_WARN(i));
91 void opts_restore_non_Wall() {
93 for (i = 0; i <= WARN_DEBUG; ++i)
94 opts_set(opts.warn, i, OPTS_GENERIC(opts.warn_backup, i));
97 void opts_backup_non_Werror_all() {
99 for (i = 0; i <= WARN_DEBUG; ++i)
100 opts_set(opts.werror_backup, i, OPTS_WERROR(i));
103 void opts_restore_non_Werror_all() {
105 for (i = 0; i <= WARN_DEBUG; ++i)
106 opts_set(opts.werror, i, OPTS_GENERIC(opts.werror_backup, i));
109 void opts_init(const char *output, int standard, size_t arraysize) {
112 OPTS_OPTION_STR(OPTION_OUTPUT) = output;
113 OPTS_OPTION_U32(OPTION_STANDARD) = standard;
114 OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE) = arraysize;
117 static bool opts_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def_t *list, size_t listsize) {
120 for (i = 0; i < listsize; ++i) {
121 if (!strcmp(name, list[i].name)) {
122 longbit lb = list[i].bit;
125 flags[lb.idx] |= (1<<(lb.bit));
127 flags[lb.idx] &= ~(1<<(lb.bit));
134 bool opts_setflag (const char *name, bool on) {
135 return opts_setflag_all(name, on, opts.flags, opts_flag_list, COUNT_FLAGS);
137 bool opts_setwarn (const char *name, bool on) {
138 return opts_setflag_all(name, on, opts.warn, opts_warn_list, COUNT_WARNINGS);
140 bool opts_setwerror(const char *name, bool on) {
141 return opts_setflag_all(name, on, opts.werror, opts_warn_list, COUNT_WARNINGS);
143 bool opts_setoptim (const char *name, bool on) {
144 return opts_setflag_all(name, on, opts.optimization, opts_opt_list, COUNT_OPTIMIZATIONS);
147 void opts_set(uint32_t *flags, size_t idx, bool on) {
149 LONGBIT_SET(lb, idx);
152 flags[lb.idx] |= (1<<(lb.bit));
154 flags[lb.idx] &= ~(1<<(lb.bit));
157 void opts_setoptimlevel(unsigned int level) {
159 for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
160 opts_set(opts.optimization, i, level >= opts_opt_oflag[i]);
163 opts.optimizeoff = true;
167 * Standard configuration parser and subsystem. Yes, optionally you may
168 * create ini files or cfg (the driver accepts both) for a project opposed
169 * to supplying just a progs.src (since you also may need to supply command
170 * line arguments or set the options of the compiler) [which cannot be done
173 static char *opts_ini_rstrip(char *s) {
174 char *p = s + strlen(s) - 1;
175 while (p > s && util_isspace(*p))
180 static char *opts_ini_lskip(const char *s) {
181 while (*s && util_isspace(*s))
186 static char *opts_ini_next(const char *s, char c) {
188 while (*s && *s != c && !(last && *s == ';'))
189 last = !!util_isspace(*s), s++;
194 static size_t opts_ini_parse (
196 char *(*loadhandle)(const char *, const char *, const char *, char **),
203 char *line = nullptr;
204 char section_data[2048] = "";
205 char oldname_data[2048] = "";
207 /* parsing and reading variables */
213 while (util_getline(&line, &linesize, filehandle) != EOF) {
218 (unsigned char)parse_beg[0] == 0xEF &&
219 (unsigned char)parse_beg[1] == 0xBB &&
220 (unsigned char)parse_beg[2] == 0xBF
223 parse_beg ++; /* 0xEF */
224 parse_beg ++; /* 0xBB */
225 parse_beg ++; /* 0xBF */
228 if (*(parse_beg = opts_ini_lskip(opts_ini_rstrip(parse_beg))) == ';' || *parse_beg == '#') {
229 /* ignore '#' is a perl extension */
230 } else if (*parse_beg == '[') {
232 if (*(parse_end = opts_ini_next(parse_beg + 1, ']')) == ']') {
233 * parse_end = '\0'; /* terminate bro */
234 util_strncpy(section_data, parse_beg + 1, sizeof(section_data));
235 section_data[sizeof(section_data) - 1] = '\0';
236 *oldname_data = '\0';
238 /* otherwise set error to the current line number */
241 } else if (*parse_beg && *parse_beg != ';') {
242 /* not a comment, must be a name value pair :) */
243 if (*(parse_end = opts_ini_next(parse_beg, '=')) != '=')
244 parse_end = opts_ini_next(parse_beg, ':');
246 if (*parse_end == '=' || *parse_end == ':') {
247 *parse_end = '\0'; /* terminate bro */
248 read_name = opts_ini_rstrip(parse_beg);
249 read_value = opts_ini_lskip(parse_end + 1);
250 if (*(parse_end = opts_ini_next(read_value, '\0')) == ';')
252 opts_ini_rstrip(read_value);
254 /* valid name value pair, lets call down to handler */
255 util_strncpy(oldname_data, read_name, sizeof(oldname_data));
256 oldname_data[sizeof(oldname_data) - 1] ='\0';
258 if ((*errorhandle = loadhandle(section_data, read_name, read_value, parse_file)) && !error)
260 } else if (!strcmp(section_data, "includes")) {
261 /* Includes are special */
262 if (*(parse_end = opts_ini_next(parse_beg, '=')) == '='
263 || *(parse_end = opts_ini_next(parse_beg, ':')) == ':') {
264 static const char *invalid_include = "invalid use of include";
265 vec_append(*errorhandle, strlen(invalid_include), invalid_include);
268 read_name = opts_ini_rstrip(parse_beg);
269 if ((*errorhandle = loadhandle(section_data, read_name, read_name, parse_file)) && !error)
273 /* otherwise set error to the current line number */
285 * returns true/false for a char that contains ("true" or "false" or numeric 0/1)
287 static bool opts_ini_bool(const char *value) {
288 if (!strcmp(value, "true")) return true;
289 if (!strcmp(value, "false")) return false;
290 return !!strtol(value, nullptr, 10);
293 static char *opts_ini_load(const char *section, const char *name, const char *value, char **parse_file) {
294 char *error = nullptr;
298 * undef all of these because they may still be defined like in my
301 #undef GMQCC_TYPE_FLAGS
302 #undef GMQCC_TYPE_OPTIMIZATIONS
303 #undef GMQCC_TYPE_WARNS
305 /* deal with includes */
306 if (!strcmp(section, "includes")) {
307 static const char *include_error_beg = "failed to open file `";
308 static const char *include_error_end = "' for inclusion";
309 FILE *file = fopen(value, "r");
312 vec_append(error, strlen(include_error_beg), include_error_beg);
313 vec_append(error, strlen(value), value);
314 vec_append(error, strlen(include_error_end), include_error_end);
316 if (opts_ini_parse(file, &opts_ini_load, &error, parse_file) != 0)
318 /* Change the file name */
320 *parse_file = util_strdup(value);
326 #define GMQCC_TYPE_FLAGS
327 #define GMQCC_DEFINE_FLAG(X) \
328 if (!strcmp(section, "flags") && !strcmp(name, #X)) { \
329 opts_set(opts.flags, X, opts_ini_bool(value)); \
335 #define GMQCC_TYPE_WARNS
336 #define GMQCC_DEFINE_FLAG(X) \
337 if (!strcmp(section, "warnings") && !strcmp(name, #X)) { \
338 opts_set(opts.warn, WARN_##X, opts_ini_bool(value)); \
343 /* Werror-individuals */
344 #define GMQCC_TYPE_WARNS
345 #define GMQCC_DEFINE_FLAG(X) \
346 if (!strcmp(section, "errors") && !strcmp(name, #X)) { \
347 opts_set(opts.werror, WARN_##X, opts_ini_bool(value)); \
353 #define GMQCC_TYPE_OPTIMIZATIONS
354 #define GMQCC_DEFINE_FLAG(X,Y) \
355 if (!strcmp(section, "optimizations") && !strcmp(name, #X)) { \
356 opts_set(opts.optimization, OPTIM_##X, opts_ini_bool(value)); \
361 /* nothing was found ever! */
363 if (strcmp(section, "includes") &&
364 strcmp(section, "flags") &&
365 strcmp(section, "warnings") &&
366 strcmp(section, "optimizations"))
368 static const char *invalid_section = "invalid_section `";
369 vec_append(error, strlen(invalid_section), invalid_section);
370 vec_append(error, strlen(section), section);
371 vec_push(error, '`');
372 } else if (strcmp(section, "includes")) {
373 static const char *invalid_variable = "invalid_variable `";
374 static const char *in_section = "` in section: `";
375 vec_append(error, strlen(invalid_variable), invalid_variable);
376 vec_append(error, strlen(name), name);
377 vec_append(error, strlen(in_section), in_section);
378 vec_append(error, strlen(section), section);
379 vec_push(error, '`');
381 static const char *expected_something = "expected something";
382 vec_append(error, strlen(expected_something), expected_something);
385 vec_push(error, '\0');
390 * Actual loading subsystem, this finds the ini or cfg file, and properly
391 * loads it and executes it to set compiler options.
393 void opts_ini_init(const char *file) {
395 * Possible matches are:
399 char *error = nullptr;
400 char *parse_file = nullptr;
406 if (!(ini = fopen((file = "gmqcc.ini"), "r")))
408 if (!(ini = fopen((file = "gmqcc.cfg"), "r")))
410 } else if (!(ini = fopen(file, "r")))
413 con_out("found ini file `%s`\n", file);
415 parse_file = util_strdup(file);
416 if ((line = opts_ini_parse(ini, &opts_ini_load, &error, &parse_file)) != 0) {
417 /* there was a parse error with the ini file */
418 con_printmsg(LVL_ERROR, parse_file, line, 0 /*TODO: column for ini error*/, "error", error);