]> git.xonotic.org Git - xonotic/gmqcc.git/blob - opts.cpp
Rewrite constant folder in C++
[xonotic/gmqcc.git] / opts.cpp
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "gmqcc.h"
5
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,
9 #  include "opts.def"
10     0
11 };
12
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) },
16 #  include "opts.def"
17     { nullptr, LONGBIT(0) }
18 };
19
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) },
23 #  include "opts.def"
24     { nullptr, LONGBIT(0) }
25 };
26
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) },
30 #  include "opts.def"
31     { nullptr, LONGBIT(0) }
32 };
33
34 unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS];
35 opts_cmd_t   opts; /* command line options */
36
37 static void opts_setdefault(void) {
38     memset(&opts, 0, sizeof(opts_cmd_t));
39     OPTS_OPTION_STR(OPTION_PROGSRC)     = "progs.src";
40
41     /* warnings */
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);
73
74     /* flags */
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);
80
81     /* options */
82     OPTS_OPTION_U32(OPTION_STATE_FPS) = 10;
83 }
84
85 void opts_backup_non_Wall() {
86     size_t i;
87     for (i = 0; i <= WARN_DEBUG; ++i)
88         opts_set(opts.warn_backup, i, OPTS_WARN(i));
89 }
90
91 void opts_restore_non_Wall() {
92     size_t i;
93     for (i = 0; i <= WARN_DEBUG; ++i)
94         opts_set(opts.warn, i, OPTS_GENERIC(opts.warn_backup, i));
95 }
96
97 void opts_backup_non_Werror_all() {
98     size_t i;
99     for (i = 0; i <= WARN_DEBUG; ++i)
100         opts_set(opts.werror_backup, i, OPTS_WERROR(i));
101 }
102
103 void opts_restore_non_Werror_all() {
104     size_t i;
105     for (i = 0; i <= WARN_DEBUG; ++i)
106         opts_set(opts.werror, i, OPTS_GENERIC(opts.werror_backup, i));
107 }
108
109 void opts_init(const char *output, int standard, size_t arraysize) {
110     opts_setdefault();
111
112     OPTS_OPTION_STR(OPTION_OUTPUT)         = output;
113     OPTS_OPTION_U32(OPTION_STANDARD)       = standard;
114     OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE) = arraysize;
115 }
116
117 static bool opts_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def_t *list, size_t listsize) {
118     size_t i;
119
120     for (i = 0; i < listsize; ++i) {
121         if (!strcmp(name, list[i].name)) {
122             longbit lb = list[i].bit;
123
124             if (on)
125                 flags[lb.idx] |= (1<<(lb.bit));
126             else
127                 flags[lb.idx] &= ~(1<<(lb.bit));
128
129             return true;
130         }
131     }
132     return false;
133 }
134 bool opts_setflag  (const char *name, bool on) {
135     return opts_setflag_all(name, on, opts.flags,        opts_flag_list, COUNT_FLAGS);
136 }
137 bool opts_setwarn  (const char *name, bool on) {
138     return opts_setflag_all(name, on, opts.warn,         opts_warn_list, COUNT_WARNINGS);
139 }
140 bool opts_setwerror(const char *name, bool on) {
141     return opts_setflag_all(name, on, opts.werror,       opts_warn_list, COUNT_WARNINGS);
142 }
143 bool opts_setoptim (const char *name, bool on) {
144     return opts_setflag_all(name, on, opts.optimization, opts_opt_list,  COUNT_OPTIMIZATIONS);
145 }
146
147 void opts_set(uint32_t *flags, size_t idx, bool on) {
148     longbit lb;
149     LONGBIT_SET(lb, idx);
150
151     if (on)
152         flags[lb.idx] |= (1<<(lb.bit));
153     else
154         flags[lb.idx] &= ~(1<<(lb.bit));
155 }
156
157 void opts_setoptimlevel(unsigned int level) {
158     size_t i;
159     for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
160         opts_set(opts.optimization, i, level >= opts_opt_oflag[i]);
161
162     if (!level)
163         opts.optimizeoff = true;
164 }
165
166 /*
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
171  * from a progs.src.
172  */
173 static char *opts_ini_rstrip(char *s) {
174     char *p = s + strlen(s) - 1;
175     while (p > s && util_isspace(*p))
176         *p = '\0', p--;
177     return s;
178 }
179
180 static char *opts_ini_lskip(const char *s) {
181     while (*s && util_isspace(*s))
182         s++;
183     return (char*)s;
184 }
185
186 static char *opts_ini_next(const char *s, char c) {
187     bool last = false;
188     while (*s && *s != c && !(last && *s == ';'))
189         last = !!util_isspace(*s), s++;
190
191     return (char*)s;
192 }
193
194 static size_t opts_ini_parse (
195     FILE *filehandle,
196     char *(*loadhandle)(const char *, const char *, const char *, char **),
197     char **errorhandle,
198     char **parse_file
199 ) {
200     size_t linesize;
201     size_t lineno             = 1;
202     size_t error              = 0;
203     char  *line               = nullptr;
204     char   section_data[2048] = "";
205     char   oldname_data[2048] = "";
206
207     /* parsing and reading variables */
208     char *parse_beg;
209     char *parse_end;
210     char *read_name;
211     char *read_value;
212
213     while (util_getline(&line, &linesize, filehandle) != EOF) {
214         parse_beg = line;
215
216         /* handle BOM */
217         if (lineno == 1 && (
218                 (unsigned char)parse_beg[0] == 0xEF &&
219                 (unsigned char)parse_beg[1] == 0xBB &&
220                 (unsigned char)parse_beg[2] == 0xBF
221             )
222         ) {
223             parse_beg ++; /* 0xEF */
224             parse_beg ++; /* 0xBB */
225             parse_beg ++; /* 0xBF */
226         }
227
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 == '[') {
231             /* section found */
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';
237             } else if (!error) {
238                 /* otherwise set error to the current line number */
239                 error = lineno;
240             }
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, ':');
245
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')) == ';')
251                     * parse_end = '\0';
252                 opts_ini_rstrip(read_value);
253
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';
257
258                 if ((*errorhandle = loadhandle(section_data, read_name, read_value, parse_file)) && !error)
259                     error = lineno;
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);
266                     error = lineno;
267                 } else {
268                     read_name = opts_ini_rstrip(parse_beg);
269                     if ((*errorhandle = loadhandle(section_data, read_name, read_name, parse_file)) && !error)
270                         error = lineno;
271                 }
272             } else if (!error) {
273                 /* otherwise set error to the current line number */
274                 error = lineno;
275             }
276         }
277         lineno++;
278     }
279     mem_d(line);
280     return error;
281
282 }
283
284 /*
285  * returns true/false for a char that contains ("true" or "false" or numeric 0/1)
286  */
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);
291 }
292
293 static char *opts_ini_load(const char *section, const char *name, const char *value, char **parse_file) {
294     char *error = nullptr;
295     bool  found = false;
296
297     /*
298      * undef all of these because they may still be defined like in my
299      * case they where.
300      */
301     #undef GMQCC_TYPE_FLAGS
302     #undef GMQCC_TYPE_OPTIMIZATIONS
303     #undef GMQCC_TYPE_WARNS
304
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");
310         found = true;
311         if (!file) {
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);
315         } else {
316             if (opts_ini_parse(file, &opts_ini_load, &error, parse_file) != 0)
317                 found = false;
318             /* Change the file name */
319             mem_d(*parse_file);
320             *parse_file = util_strdup(value);
321             fclose(file);
322         }
323     }
324
325     /* flags */
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));                 \
330         found = true;                                                  \
331     }
332     #include "opts.def"
333
334     /* warnings */
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));           \
339         found = true;                                                  \
340     }
341     #include "opts.def"
342
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));         \
348         found = true;                                                  \
349     }
350     #include "opts.def"
351
352     /* optimizations */
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));  \
357         found = true;                                                  \
358     }
359     #include "opts.def"
360
361     /* nothing was found ever! */
362     if (!found) {
363         if (strcmp(section, "includes") &&
364             strcmp(section, "flags")    &&
365             strcmp(section, "warnings") &&
366             strcmp(section, "optimizations"))
367         {
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, '`');
380         } else {
381             static const char *expected_something = "expected something";
382             vec_append(error, strlen(expected_something), expected_something);
383         }
384     }
385     vec_push(error, '\0');
386     return error;
387 }
388
389 /*
390  * Actual loading subsystem, this finds the ini or cfg file, and properly
391  * loads it and executes it to set compiler options.
392  */
393 void opts_ini_init(const char *file) {
394     /*
395      * Possible matches are:
396      *  gmqcc.ini
397      *  gmqcc.cfg
398      */
399     char       *error = nullptr;
400     char       *parse_file = nullptr;
401     size_t     line;
402     FILE  *ini;
403
404     if (!file) {
405         /* try ini */
406         if (!(ini = fopen((file = "gmqcc.ini"), "r")))
407             /* try cfg */
408             if (!(ini = fopen((file = "gmqcc.cfg"), "r")))
409                 return;
410     } else if (!(ini = fopen(file, "r")))
411         return;
412
413     con_out("found ini file `%s`\n", file);
414
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);
419         vec_free(error);
420     }
421     mem_d(parse_file);
422
423     fclose(ini);
424 }