]> git.xonotic.org Git - xonotic/gmqcc.git/blob - opts.c
This is supposted in C99
[xonotic/gmqcc.git] / opts.c
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     { NULL, 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     { NULL, 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     { NULL, 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     OPTS_OPTION_U16(OPTION_MEMDUMPCOLS)    = 16;
116 }
117
118 static bool opts_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def_t *list, size_t listsize) {
119     size_t i;
120
121     for (i = 0; i < listsize; ++i) {
122         if (!strcmp(name, list[i].name)) {
123             longbit lb = list[i].bit;
124
125             if (on)
126                 flags[lb.idx] |= (1<<(lb.bit));
127             else
128                 flags[lb.idx] &= ~(1<<(lb.bit));
129
130             return true;
131         }
132     }
133     return false;
134 }
135 bool opts_setflag  (const char *name, bool on) {
136     return opts_setflag_all(name, on, opts.flags,        opts_flag_list, COUNT_FLAGS);
137 }
138 bool opts_setwarn  (const char *name, bool on) {
139     return opts_setflag_all(name, on, opts.warn,         opts_warn_list, COUNT_WARNINGS);
140 }
141 bool opts_setwerror(const char *name, bool on) {
142     return opts_setflag_all(name, on, opts.werror,       opts_warn_list, COUNT_WARNINGS);
143 }
144 bool opts_setoptim (const char *name, bool on) {
145     return opts_setflag_all(name, on, opts.optimization, opts_opt_list,  COUNT_OPTIMIZATIONS);
146 }
147
148 void opts_set(uint32_t *flags, size_t idx, bool on) {
149     longbit lb;
150     LONGBIT_SET(lb, idx);
151
152     if (on)
153         flags[lb.idx] |= (1<<(lb.bit));
154     else
155         flags[lb.idx] &= ~(1<<(lb.bit));
156 }
157
158 void opts_setoptimlevel(unsigned int level) {
159     size_t i;
160     for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
161         opts_set(opts.optimization, i, level >= opts_opt_oflag[i]);
162
163     if (!level)
164         opts.optimizeoff = true;
165 }
166
167 /*
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
172  * from a progs.src.
173  */
174 static char *opts_ini_rstrip(char *s) {
175     char *p = s + strlen(s) - 1;
176     while (p > s && util_isspace(*p))
177         *p = '\0', p--;
178     return s;
179 }
180
181 static char *opts_ini_lskip(const char *s) {
182     while (*s && util_isspace(*s))
183         s++;
184     return (char*)s;
185 }
186
187 static char *opts_ini_next(const char *s, char c) {
188     bool last = false;
189     while (*s && *s != c && !(last && *s == ';'))
190         last = !!util_isspace(*s), s++;
191
192     return (char*)s;
193 }
194
195 static size_t opts_ini_parse (
196     FILE *filehandle,
197     char *(*loadhandle)(const char *, const char *, const char *, char **),
198     char **errorhandle,
199     char **parse_file
200 ) {
201     size_t linesize;
202     size_t lineno             = 1;
203     size_t error              = 0;
204     char  *line               = NULL;
205     char   section_data[2048] = "";
206     char   oldname_data[2048] = "";
207
208     /* parsing and reading variables */
209     char *parse_beg;
210     char *parse_end;
211     char *read_name;
212     char *read_value;
213
214     while (util_getline(&line, &linesize, filehandle) != EOF) {
215         parse_beg = line;
216
217         /* handle BOM */
218         if (lineno == 1 && (
219                 (unsigned char)parse_beg[0] == 0xEF &&
220                 (unsigned char)parse_beg[1] == 0xBB &&
221                 (unsigned char)parse_beg[2] == 0xBF
222             )
223         ) {
224             parse_beg ++; /* 0xEF */
225             parse_beg ++; /* 0xBB */
226             parse_beg ++; /* 0xBF */
227         }
228
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 == '[') {
232             /* section found */
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';
238             } else if (!error) {
239                 /* otherwise set error to the current line number */
240                 error = lineno;
241             }
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, ':');
246
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')) == ';')
252                     * parse_end = '\0';
253                 opts_ini_rstrip(read_value);
254
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';
258
259                 if ((*errorhandle = loadhandle(section_data, read_name, read_value, parse_file)) && !error)
260                     error = lineno;
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);
267                     error = lineno;
268                 } else {
269                     read_name = opts_ini_rstrip(parse_beg);
270                     if ((*errorhandle = loadhandle(section_data, read_name, read_name, parse_file)) && !error)
271                         error = lineno;
272                 }
273             } else if (!error) {
274                 /* otherwise set error to the current line number */
275                 error = lineno;
276             }
277         }
278         lineno++;
279     }
280     mem_d(line);
281     return error;
282
283 }
284
285 /*
286  * returns true/false for a char that contains ("true" or "false" or numeric 0/1)
287  */
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);
292 }
293
294 static char *opts_ini_load(const char *section, const char *name, const char *value, char **parse_file) {
295     char *error = NULL;
296     bool  found = false;
297
298     /*
299      * undef all of these because they may still be defined like in my
300      * case they where.
301      */
302     #undef GMQCC_TYPE_FLAGS
303     #undef GMQCC_TYPE_OPTIMIZATIONS
304     #undef GMQCC_TYPE_WARNS
305
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");
311         found = true;
312         if (!file) {
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);
316         } else {
317             if (opts_ini_parse(file, &opts_ini_load, &error, parse_file) != 0)
318                 found = false;
319             /* Change the file name */
320             mem_d(*parse_file);
321             *parse_file = util_strdup(value);
322             fclose(file);
323         }
324     }
325
326     /* flags */
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));                 \
331         found = true;                                                  \
332     }
333     #include "opts.def"
334
335     /* warnings */
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));           \
340         found = true;                                                  \
341     }
342     #include "opts.def"
343
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));         \
349         found = true;                                                  \
350     }
351     #include "opts.def"
352
353     /* optimizations */
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));  \
358         found = true;                                                  \
359     }
360     #include "opts.def"
361
362     /* nothing was found ever! */
363     if (!found) {
364         if (strcmp(section, "includes") &&
365             strcmp(section, "flags")    &&
366             strcmp(section, "warnings") &&
367             strcmp(section, "optimizations"))
368         {
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, '`');
381         } else {
382             static const char *expected_something = "expected something";
383             vec_append(error, strlen(expected_something), expected_something);
384         }
385     }
386     vec_push(error, '\0');
387     return error;
388 }
389
390 /*
391  * Actual loading subsystem, this finds the ini or cfg file, and properly
392  * loads it and executes it to set compiler options.
393  */
394 void opts_ini_init(const char *file) {
395     /*
396      * Possible matches are:
397      *  gmqcc.ini
398      *  gmqcc.cfg
399      */
400     char       *error = NULL;
401     char       *parse_file = NULL;
402     size_t     line;
403     FILE  *ini;
404
405     if (!file) {
406         /* try ini */
407         if (!(ini = fopen((file = "gmqcc.ini"), "r")))
408             /* try cfg */
409             if (!(ini = fopen((file = "gmqcc.cfg"), "r")))
410                 return;
411     } else if (!(ini = fopen(file, "r")))
412         return;
413
414     con_out("found ini file `%s`\n", file);
415
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);
420         vec_free(error);
421     }
422     mem_d(parse_file);
423
424     fclose(ini);
425 }