]> git.xonotic.org Git - xonotic/gmqcc.git/blob - opts.c
Don't emit whitespace when stringifying TOKEN_WHITE.
[xonotic/gmqcc.git] / opts.c
1 /*
2  * Copyright (C) 2012, 2013, 2014
3  *     Wolfgang Bumiller
4  *     Dale Weiler
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is furnished to do
11  * so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "gmqcc.h"
28
29 const unsigned int opts_opt_oflag[COUNT_OPTIMIZATIONS+1] = {
30 # define GMQCC_TYPE_OPTIMIZATIONS
31 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) MIN_O,
32 #  include "opts.def"
33     0
34 };
35
36 const opts_flag_def_t opts_opt_list[COUNT_OPTIMIZATIONS+1] = {
37 # define GMQCC_TYPE_OPTIMIZATIONS
38 # define GMQCC_DEFINE_FLAG(NAME, MIN_O) { #NAME, LONGBIT(OPTIM_##NAME) },
39 #  include "opts.def"
40     { NULL, LONGBIT(0) }
41 };
42
43 const opts_flag_def_t opts_warn_list[COUNT_WARNINGS+1] = {
44 # define GMQCC_TYPE_WARNS
45 # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(WARN_##X) },
46 #  include "opts.def"
47     { NULL, LONGBIT(0) }
48 };
49
50 const opts_flag_def_t opts_flag_list[COUNT_FLAGS+1] = {
51 # define GMQCC_TYPE_FLAGS
52 # define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(X) },
53 #  include "opts.def"
54     { NULL, LONGBIT(0) }
55 };
56
57 unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS];
58 opts_cmd_t   opts; /* command line options */
59
60 static void opts_setdefault(void) {
61     memset(&opts, 0, sizeof(opts_cmd_t));
62     OPTS_OPTION_BOOL(OPTION_CORRECTION) = true;
63     OPTS_OPTION_STR(OPTION_PROGSRC)     = "progs.src";
64
65     /* warnings */
66     opts_set(opts.warn,  WARN_UNUSED_VARIABLE,           true);
67     opts_set(opts.warn,  WARN_USED_UNINITIALIZED,        true);
68     opts_set(opts.warn,  WARN_UNKNOWN_CONTROL_SEQUENCE,  true);
69     opts_set(opts.warn,  WARN_EXTENSIONS,                true);
70     opts_set(opts.warn,  WARN_FIELD_REDECLARED,          true);
71     opts_set(opts.warn,  WARN_MISSING_RETURN_VALUES,     true);
72     opts_set(opts.warn,  WARN_INVALID_PARAMETER_COUNT,   true);
73     opts_set(opts.warn,  WARN_LOCAL_CONSTANTS,           true);
74     opts_set(opts.warn,  WARN_VOID_VARIABLES,            true);
75     opts_set(opts.warn,  WARN_IMPLICIT_FUNCTION_POINTER, true);
76     opts_set(opts.warn,  WARN_VARIADIC_FUNCTION,         true);
77     opts_set(opts.warn,  WARN_FRAME_MACROS,              true);
78     opts_set(opts.warn,  WARN_EFFECTLESS_STATEMENT,      true);
79     opts_set(opts.warn,  WARN_END_SYS_FIELDS,            true);
80     opts_set(opts.warn,  WARN_ASSIGN_FUNCTION_TYPES,     true);
81     opts_set(opts.warn,  WARN_CPP,                       true);
82     opts_set(opts.warn,  WARN_MULTIFILE_IF,              true);
83     opts_set(opts.warn,  WARN_DOUBLE_DECLARATION,        true);
84     opts_set(opts.warn,  WARN_CONST_VAR,                 true);
85     opts_set(opts.warn,  WARN_MULTIBYTE_CHARACTER,       true);
86     opts_set(opts.warn,  WARN_UNKNOWN_PRAGMAS,           true);
87     opts_set(opts.warn,  WARN_UNREACHABLE_CODE,          true);
88     opts_set(opts.warn,  WARN_UNKNOWN_ATTRIBUTE,         true);
89     opts_set(opts.warn,  WARN_RESERVED_NAMES,            true);
90     opts_set(opts.warn,  WARN_UNINITIALIZED_CONSTANT,    true);
91     opts_set(opts.warn,  WARN_DEPRECATED,                true);
92     opts_set(opts.warn,  WARN_PARENTHESIS,               true);
93     opts_set(opts.warn,  WARN_CONST_OVERWRITE,           true);
94     opts_set(opts.warn,  WARN_DIRECTIVE_INMACRO,         true);
95     opts_set(opts.warn,  WARN_BUILTINS,                  true);
96     opts_set(opts.warn,  WARN_INEXACT_COMPARES,          true);
97
98     /* flags */
99     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,           true);
100     opts_set(opts.flags, CORRECT_TERNARY,                true);
101     opts_set(opts.flags, BAIL_ON_WERROR,                 true);
102     opts_set(opts.flags, LEGACY_VECTOR_MATHS,            true);
103     opts_set(opts.flags, DARKPLACES_STRING_TABLE_BUG,    true);
104
105     /* options */
106     OPTS_OPTION_U32(OPTION_STATE_FPS) = 10;
107 }
108
109 void opts_backup_non_Wall() {
110     size_t i;
111     for (i = 0; i <= WARN_DEBUG; ++i)
112         opts_set(opts.warn_backup, i, OPTS_WARN(i));
113 }
114
115 void opts_restore_non_Wall() {
116     size_t i;
117     for (i = 0; i <= WARN_DEBUG; ++i)
118         opts_set(opts.warn, i, OPTS_GENERIC(opts.warn_backup, i));
119 }
120
121 void opts_backup_non_Werror_all() {
122     size_t i;
123     for (i = 0; i <= WARN_DEBUG; ++i)
124         opts_set(opts.werror_backup, i, OPTS_WERROR(i));
125 }
126
127 void opts_restore_non_Werror_all() {
128     size_t i;
129     for (i = 0; i <= WARN_DEBUG; ++i)
130         opts_set(opts.werror, i, OPTS_GENERIC(opts.werror_backup, i));
131 }
132
133 void opts_init(const char *output, int standard, size_t arraysize) {
134     opts_setdefault();
135
136     OPTS_OPTION_STR(OPTION_OUTPUT)         = output;
137     OPTS_OPTION_U32(OPTION_STANDARD)       = standard;
138     OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE) = arraysize;
139     OPTS_OPTION_U16(OPTION_MEMDUMPCOLS)    = 16;
140 }
141
142 static bool opts_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def_t *list, size_t listsize) {
143     size_t i;
144
145     for (i = 0; i < listsize; ++i) {
146         if (!strcmp(name, list[i].name)) {
147             longbit lb = list[i].bit;
148
149             if (on)
150                 flags[lb.idx] |= (1<<(lb.bit));
151             else
152                 flags[lb.idx] &= ~(1<<(lb.bit));
153
154             return true;
155         }
156     }
157     return false;
158 }
159 bool opts_setflag  (const char *name, bool on) {
160     return opts_setflag_all(name, on, opts.flags,        opts_flag_list, COUNT_FLAGS);
161 }
162 bool opts_setwarn  (const char *name, bool on) {
163     return opts_setflag_all(name, on, opts.warn,         opts_warn_list, COUNT_WARNINGS);
164 }
165 bool opts_setwerror(const char *name, bool on) {
166     return opts_setflag_all(name, on, opts.werror,       opts_warn_list, COUNT_WARNINGS);
167 }
168 bool opts_setoptim (const char *name, bool on) {
169     return opts_setflag_all(name, on, opts.optimization, opts_opt_list,  COUNT_OPTIMIZATIONS);
170 }
171
172 void opts_set(uint32_t *flags, size_t idx, bool on) {
173     longbit lb;
174     LONGBIT_SET(lb, idx);
175
176     if (on)
177         flags[lb.idx] |= (1<<(lb.bit));
178     else
179         flags[lb.idx] &= ~(1<<(lb.bit));
180 }
181
182 void opts_setoptimlevel(unsigned int level) {
183     size_t i;
184     for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
185         opts_set(opts.optimization, i, level >= opts_opt_oflag[i]);
186
187     if (!level)
188         opts.optimizeoff = true;
189 }
190
191 /*
192  * Standard configuration parser and subsystem.  Yes, optionally you may
193  * create ini files or cfg (the driver accepts both) for a project opposed
194  * to supplying just a progs.src (since you also may need to supply command
195  * line arguments or set the options of the compiler) [which cannot be done
196  * from a progs.src.
197  */
198 static char *opts_ini_rstrip(char *s) {
199     char *p = s + strlen(s) - 1;
200     while (p > s && util_isspace(*p))
201         *p = '\0', p--;
202     return s;
203 }
204
205 static char *opts_ini_lskip(const char *s) {
206     while (*s && util_isspace(*s))
207         s++;
208     return (char*)s;
209 }
210
211 static char *opts_ini_next(const char *s, char c) {
212     bool last = false;
213     while (*s && *s != c && !(last && *s == ';'))
214         last = !!util_isspace(*s), s++;
215
216     return (char*)s;
217 }
218
219 static size_t opts_ini_parse (
220     fs_file_t *filehandle,
221     char *(*loadhandle)(const char *, const char *, const char *, char **),
222     char **errorhandle,
223     char **parse_file
224 ) {
225     size_t linesize;
226     size_t lineno             = 1;
227     size_t error              = 0;
228     char  *line               = NULL;
229     char   section_data[2048] = "";
230     char   oldname_data[2048] = "";
231
232     /* parsing and reading variables */
233     char *parse_beg;
234     char *parse_end;
235     char *read_name;
236     char *read_value;
237
238     while (fs_file_getline(&line, &linesize, filehandle) != FS_FILE_EOF) {
239         parse_beg = line;
240
241         /* handle BOM */
242         if (lineno == 1 && (
243                 (unsigned char)parse_beg[0] == 0xEF &&
244                 (unsigned char)parse_beg[1] == 0xBB &&
245                 (unsigned char)parse_beg[2] == 0xBF
246             )
247         ) {
248             parse_beg ++; /* 0xEF */
249             parse_beg ++; /* 0xBB */
250             parse_beg ++; /* 0xBF */
251         }
252
253         if (*(parse_beg = opts_ini_lskip(opts_ini_rstrip(parse_beg))) == ';' || *parse_beg == '#') {
254             /* ignore '#' is a perl extension */
255         } else if (*parse_beg == '[') {
256             /* section found */
257             if (*(parse_end = opts_ini_next(parse_beg + 1, ']')) == ']') {
258                 * parse_end = '\0'; /* terminate bro */
259                 util_strncpy(section_data, parse_beg + 1, sizeof(section_data));
260                 section_data[sizeof(section_data) - 1] = '\0';
261                 *oldname_data                          = '\0';
262             } else if (!error) {
263                 /* otherwise set error to the current line number */
264                 error = lineno;
265             }
266         } else if (*parse_beg && *parse_beg != ';') {
267             /* not a comment, must be a name value pair :) */
268             if (*(parse_end = opts_ini_next(parse_beg, '=')) != '=')
269                 parse_end = opts_ini_next(parse_beg, ':');
270
271             if (*parse_end == '=' || *parse_end == ':') {
272                 *parse_end = '\0'; /* terminate bro */
273                 read_name  = opts_ini_rstrip(parse_beg);
274                 read_value = opts_ini_lskip(parse_end + 1);
275                 if (*(parse_end = opts_ini_next(read_value, '\0')) == ';')
276                     * parse_end = '\0';
277                 opts_ini_rstrip(read_value);
278
279                 /* valid name value pair, lets call down to handler */
280                 util_strncpy(oldname_data, read_name, sizeof(oldname_data));
281                 oldname_data[sizeof(oldname_data) - 1] ='\0';
282
283                 if ((*errorhandle = loadhandle(section_data, read_name, read_value, parse_file)) && !error)
284                     error = lineno;
285             } else if (!strcmp(section_data, "includes")) {
286                 /* Includes are special */
287                 if (*(parse_end = opts_ini_next(parse_beg, '=')) == '='
288                 ||  *(parse_end = opts_ini_next(parse_beg, ':')) == ':') {
289                     static const char *invalid_include = "invalid use of include";
290                     vec_append(*errorhandle, strlen(invalid_include), invalid_include);
291                     error = lineno;
292                 } else {
293                     read_name = opts_ini_rstrip(parse_beg);
294                     if ((*errorhandle = loadhandle(section_data, read_name, read_name, parse_file)) && !error)
295                         error = lineno;
296                 }
297             } else if (!error) {
298                 /* otherwise set error to the current line number */
299                 error = lineno;
300             }
301         }
302         lineno++;
303     }
304     mem_d(line);
305     return error;
306
307 }
308
309 /*
310  * returns true/false for a char that contains ("true" or "false" or numeric 0/1)
311  */
312 static bool opts_ini_bool(const char *value) {
313     if (!strcmp(value, "true"))  return true;
314     if (!strcmp(value, "false")) return false;
315     return !!strtol(value, NULL, 10);
316 }
317
318 static char *opts_ini_load(const char *section, const char *name, const char *value, char **parse_file) {
319     char *error = NULL;
320     bool  found = false;
321
322     /*
323      * undef all of these because they may still be defined like in my
324      * case they where.
325      */
326     #undef GMQCC_TYPE_FLAGS
327     #undef GMQCC_TYPE_OPTIMIZATIONS
328     #undef GMQCC_TYPE_WARNS
329
330     /* deal with includes */
331     if (!strcmp(section, "includes")) {
332         static const char *include_error_beg = "failed to open file `";
333         static const char *include_error_end = "' for inclusion";
334         fs_file_t *file = fs_file_open(value, "r");
335         found = true;
336         if (!file) {
337             vec_append(error, strlen(include_error_beg), include_error_beg);
338             vec_append(error, strlen(value), value);
339             vec_append(error, strlen(include_error_end), include_error_end);
340         } else {
341             if (opts_ini_parse(file, &opts_ini_load, &error, parse_file) != 0)
342                 found = false;
343             /* Change the file name */
344             mem_d(*parse_file);
345             *parse_file = util_strdup(value);
346             fs_file_close(file);
347         }
348     }
349
350     /* flags */
351     #define GMQCC_TYPE_FLAGS
352     #define GMQCC_DEFINE_FLAG(X)                                       \
353     if (!strcmp(section, "flags") && !strcmp(name, #X)) {              \
354         opts_set(opts.flags, X, opts_ini_bool(value));                 \
355         found = true;                                                  \
356     }
357     #include "opts.def"
358
359     /* warnings */
360     #define GMQCC_TYPE_WARNS
361     #define GMQCC_DEFINE_FLAG(X)                                       \
362     if (!strcmp(section, "warnings") && !strcmp(name, #X)) {           \
363         opts_set(opts.warn, WARN_##X, opts_ini_bool(value));           \
364         found = true;                                                  \
365     }
366     #include "opts.def"
367
368     /* Werror-individuals */
369     #define GMQCC_TYPE_WARNS
370     #define GMQCC_DEFINE_FLAG(X)                                       \
371     if (!strcmp(section, "errors") && !strcmp(name, #X)) {             \
372         opts_set(opts.werror, WARN_##X, opts_ini_bool(value));         \
373         found = true;                                                  \
374     }
375     #include "opts.def"
376
377     /* optimizations */
378     #define GMQCC_TYPE_OPTIMIZATIONS
379     #define GMQCC_DEFINE_FLAG(X,Y)                                     \
380     if (!strcmp(section, "optimizations") && !strcmp(name, #X)) {      \
381         opts_set(opts.optimization, OPTIM_##X, opts_ini_bool(value));  \
382         found = true;                                                  \
383     }
384     #include "opts.def"
385
386     /* nothing was found ever! */
387     if (!found) {
388         if (strcmp(section, "includes") &&
389             strcmp(section, "flags")    &&
390             strcmp(section, "warnings") &&
391             strcmp(section, "optimizations"))
392         {
393             static const char *invalid_section = "invalid_section `";
394             vec_append(error, strlen(invalid_section), invalid_section);
395             vec_append(error, strlen(section), section);
396             vec_push(error, '`');
397         } else if (strcmp(section, "includes")) {
398             static const char *invalid_variable = "invalid_variable `";
399             static const char *in_section = "` in section: `";
400             vec_append(error, strlen(invalid_variable), invalid_variable);
401             vec_append(error, strlen(name), name);
402             vec_append(error, strlen(in_section), in_section);
403             vec_append(error, strlen(section), section);
404             vec_push(error, '`');
405         } else {
406             static const char *expected_something = "expected something";
407             vec_append(error, strlen(expected_something), expected_something);
408         }
409     }
410     vec_push(error, '\0');
411     return error;
412 }
413
414 /*
415  * Actual loading subsystem, this finds the ini or cfg file, and properly
416  * loads it and executes it to set compiler options.
417  */
418 void opts_ini_init(const char *file) {
419     /*
420      * Possible matches are:
421      *  gmqcc.ini
422      *  gmqcc.cfg
423      */
424     char       *error = NULL;
425     char       *parse_file = NULL;
426     size_t     line;
427     fs_file_t  *ini;
428
429     if (!file) {
430         /* try ini */
431         if (!(ini = fs_file_open((file = "gmqcc.ini"), "r")))
432             /* try cfg */
433             if (!(ini = fs_file_open((file = "gmqcc.cfg"), "r")))
434                 return;
435     } else if (!(ini = fs_file_open(file, "r")))
436         return;
437
438     con_out("found ini file `%s`\n", file);
439
440     parse_file = util_strdup(file);
441     if ((line = opts_ini_parse(ini, &opts_ini_load, &error, &parse_file)) != 0) {
442         /* there was a parse error with the ini file */
443         con_printmsg(LVL_ERROR, parse_file, line, 0 /*TODO: column for ini error*/, "error", error);
444         vec_free(error);
445     }
446     mem_d(parse_file);
447
448     fs_file_close(ini);
449 }