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) },
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) },
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) },
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;
115 OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = 16;
118 static bool opts_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def_t *list, size_t listsize) {
121 for (i = 0; i < listsize; ++i) {
122 if (!strcmp(name, list[i].name)) {
123 longbit lb = list[i].bit;
126 flags[lb.idx] |= (1<<(lb.bit));
128 flags[lb.idx] &= ~(1<<(lb.bit));
135 bool opts_setflag (const char *name, bool on) {
136 return opts_setflag_all(name, on, opts.flags, opts_flag_list, COUNT_FLAGS);
138 bool opts_setwarn (const char *name, bool on) {
139 return opts_setflag_all(name, on, opts.warn, opts_warn_list, COUNT_WARNINGS);
141 bool opts_setwerror(const char *name, bool on) {
142 return opts_setflag_all(name, on, opts.werror, opts_warn_list, COUNT_WARNINGS);
144 bool opts_setoptim (const char *name, bool on) {
145 return opts_setflag_all(name, on, opts.optimization, opts_opt_list, COUNT_OPTIMIZATIONS);
148 void opts_set(uint32_t *flags, size_t idx, bool on) {
150 LONGBIT_SET(lb, idx);
153 flags[lb.idx] |= (1<<(lb.bit));
155 flags[lb.idx] &= ~(1<<(lb.bit));
158 void opts_setoptimlevel(unsigned int level) {
160 for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
161 opts_set(opts.optimization, i, level >= opts_opt_oflag[i]);
164 opts.optimizeoff = true;
168 * Standard configuration parser and subsystem. Yes, optionally you may
169 * create ini files or cfg (the driver accepts both) for a project opposed
170 * to supplying just a progs.src (since you also may need to supply command
171 * line arguments or set the options of the compiler) [which cannot be done
174 static char *opts_ini_rstrip(char *s) {
175 char *p = s + strlen(s) - 1;
176 while (p > s && util_isspace(*p))
181 static char *opts_ini_lskip(const char *s) {
182 while (*s && util_isspace(*s))
187 static char *opts_ini_next(const char *s, char c) {
189 while (*s && *s != c && !(last && *s == ';'))
190 last = !!util_isspace(*s), s++;
195 static size_t opts_ini_parse (
197 char *(*loadhandle)(const char *, const char *, const char *, char **),
205 char section_data[2048] = "";
206 char oldname_data[2048] = "";
208 /* parsing and reading variables */
214 while (util_getline(&line, &linesize, filehandle) != EOF) {
219 (unsigned char)parse_beg[0] == 0xEF &&
220 (unsigned char)parse_beg[1] == 0xBB &&
221 (unsigned char)parse_beg[2] == 0xBF
224 parse_beg ++; /* 0xEF */
225 parse_beg ++; /* 0xBB */
226 parse_beg ++; /* 0xBF */
229 if (*(parse_beg = opts_ini_lskip(opts_ini_rstrip(parse_beg))) == ';' || *parse_beg == '#') {
230 /* ignore '#' is a perl extension */
231 } else if (*parse_beg == '[') {
233 if (*(parse_end = opts_ini_next(parse_beg + 1, ']')) == ']') {
234 * parse_end = '\0'; /* terminate bro */
235 util_strncpy(section_data, parse_beg + 1, sizeof(section_data));
236 section_data[sizeof(section_data) - 1] = '\0';
237 *oldname_data = '\0';
239 /* otherwise set error to the current line number */
242 } else if (*parse_beg && *parse_beg != ';') {
243 /* not a comment, must be a name value pair :) */
244 if (*(parse_end = opts_ini_next(parse_beg, '=')) != '=')
245 parse_end = opts_ini_next(parse_beg, ':');
247 if (*parse_end == '=' || *parse_end == ':') {
248 *parse_end = '\0'; /* terminate bro */
249 read_name = opts_ini_rstrip(parse_beg);
250 read_value = opts_ini_lskip(parse_end + 1);
251 if (*(parse_end = opts_ini_next(read_value, '\0')) == ';')
253 opts_ini_rstrip(read_value);
255 /* valid name value pair, lets call down to handler */
256 util_strncpy(oldname_data, read_name, sizeof(oldname_data));
257 oldname_data[sizeof(oldname_data) - 1] ='\0';
259 if ((*errorhandle = loadhandle(section_data, read_name, read_value, parse_file)) && !error)
261 } else if (!strcmp(section_data, "includes")) {
262 /* Includes are special */
263 if (*(parse_end = opts_ini_next(parse_beg, '=')) == '='
264 || *(parse_end = opts_ini_next(parse_beg, ':')) == ':') {
265 static const char *invalid_include = "invalid use of include";
266 vec_append(*errorhandle, strlen(invalid_include), invalid_include);
269 read_name = opts_ini_rstrip(parse_beg);
270 if ((*errorhandle = loadhandle(section_data, read_name, read_name, parse_file)) && !error)
274 /* otherwise set error to the current line number */
286 * returns true/false for a char that contains ("true" or "false" or numeric 0/1)
288 static bool opts_ini_bool(const char *value) {
289 if (!strcmp(value, "true")) return true;
290 if (!strcmp(value, "false")) return false;
291 return !!strtol(value, NULL, 10);
294 static char *opts_ini_load(const char *section, const char *name, const char *value, char **parse_file) {
299 * undef all of these because they may still be defined like in my
302 #undef GMQCC_TYPE_FLAGS
303 #undef GMQCC_TYPE_OPTIMIZATIONS
304 #undef GMQCC_TYPE_WARNS
306 /* deal with includes */
307 if (!strcmp(section, "includes")) {
308 static const char *include_error_beg = "failed to open file `";
309 static const char *include_error_end = "' for inclusion";
310 FILE *file = fopen(value, "r");
313 vec_append(error, strlen(include_error_beg), include_error_beg);
314 vec_append(error, strlen(value), value);
315 vec_append(error, strlen(include_error_end), include_error_end);
317 if (opts_ini_parse(file, &opts_ini_load, &error, parse_file) != 0)
319 /* Change the file name */
321 *parse_file = util_strdup(value);
327 #define GMQCC_TYPE_FLAGS
328 #define GMQCC_DEFINE_FLAG(X) \
329 if (!strcmp(section, "flags") && !strcmp(name, #X)) { \
330 opts_set(opts.flags, X, opts_ini_bool(value)); \
336 #define GMQCC_TYPE_WARNS
337 #define GMQCC_DEFINE_FLAG(X) \
338 if (!strcmp(section, "warnings") && !strcmp(name, #X)) { \
339 opts_set(opts.warn, WARN_##X, opts_ini_bool(value)); \
344 /* Werror-individuals */
345 #define GMQCC_TYPE_WARNS
346 #define GMQCC_DEFINE_FLAG(X) \
347 if (!strcmp(section, "errors") && !strcmp(name, #X)) { \
348 opts_set(opts.werror, WARN_##X, opts_ini_bool(value)); \
354 #define GMQCC_TYPE_OPTIMIZATIONS
355 #define GMQCC_DEFINE_FLAG(X,Y) \
356 if (!strcmp(section, "optimizations") && !strcmp(name, #X)) { \
357 opts_set(opts.optimization, OPTIM_##X, opts_ini_bool(value)); \
362 /* nothing was found ever! */
364 if (strcmp(section, "includes") &&
365 strcmp(section, "flags") &&
366 strcmp(section, "warnings") &&
367 strcmp(section, "optimizations"))
369 static const char *invalid_section = "invalid_section `";
370 vec_append(error, strlen(invalid_section), invalid_section);
371 vec_append(error, strlen(section), section);
372 vec_push(error, '`');
373 } else if (strcmp(section, "includes")) {
374 static const char *invalid_variable = "invalid_variable `";
375 static const char *in_section = "` in section: `";
376 vec_append(error, strlen(invalid_variable), invalid_variable);
377 vec_append(error, strlen(name), name);
378 vec_append(error, strlen(in_section), in_section);
379 vec_append(error, strlen(section), section);
380 vec_push(error, '`');
382 static const char *expected_something = "expected something";
383 vec_append(error, strlen(expected_something), expected_something);
386 vec_push(error, '\0');
391 * Actual loading subsystem, this finds the ini or cfg file, and properly
392 * loads it and executes it to set compiler options.
394 void opts_ini_init(const char *file) {
396 * Possible matches are:
401 char *parse_file = NULL;
407 if (!(ini = fopen((file = "gmqcc.ini"), "r")))
409 if (!(ini = fopen((file = "gmqcc.cfg"), "r")))
411 } else if (!(ini = fopen(file, "r")))
414 con_out("found ini file `%s`\n", file);
416 parse_file = util_strdup(file);
417 if ((line = opts_ini_parse(ini, &opts_ini_load, &error, &parse_file)) != 0) {
418 /* there was a parse error with the ini file */
419 con_printmsg(LVL_ERROR, parse_file, line, 0 /*TODO: column for ini error*/, "error", error);