]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Fix more warnings
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012
3  *     Dale Weiler
4  *     Wolfgang Bumiller
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 "gmqcc.h"
25 #include "lexer.h"
26
27 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
28 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
29
30 uint32_t    opts_O        = 1;
31 const char *opts_output   = "progs.dat";
32 int         opts_standard = COMPILER_GMQCC;
33 bool        opts_debug    = false;
34 bool        opts_memchk   = false;
35 bool        opts_dumpfin  = false;
36 bool        opts_dump     = false;
37 bool        opts_werror   = false;
38 bool        opts_forcecrc = false;
39 bool        opts_pp_only  = false;
40 size_t      opts_max_array_size = 1024;
41
42 uint16_t    opts_forced_crc;
43
44 static bool opts_output_wasset = false;
45
46 /* set by the standard */
47 const oper_info *operators      = NULL;
48 size_t           operator_count = 0;
49
50 typedef struct { char *filename; int type; } argitem;
51 static argitem *items = NULL;
52
53 #define TYPE_QC  0
54 #define TYPE_ASM 1
55 #define TYPE_SRC 2
56
57 static const char *app_name;
58
59 static int usage() {
60     con_out("usage: %s [options] [files...]", app_name);
61     con_out("options:\n"
62             "  -h, --help             show this help message\n"
63             "  -debug                 turns on compiler debug messages\n"
64             "  -memchk                turns on compiler memory leak check\n");
65     con_out("  -o, --output=file      output file, defaults to progs.dat\n"
66             "  -a filename            add an asm file to be assembled\n"
67             "  -s filename            add a progs.src file to be used\n");
68     con_out("  -E                     stop after preprocessing\n");
69     con_out("  -f<flag>               enable a flag\n"
70             "  -fno-<flag>            disable a flag\n"
71             "  -std standard          select one of the following standards\n"
72             "       -std=qcc          original QuakeC\n"
73             "       -std=fteqcc       fteqcc QuakeC\n"
74             "       -std=gmqcc        this compiler (default)\n");
75     con_out("  -W<warning>            enable a warning\n"
76             "  -Wno-<warning>         disable a warning\n"
77             "  -Wall                  enable all warnings\n"
78             "  -Werror                treat warnings as errors\n");
79     con_out("  -force-crc=num         force a specific checksum into the header\n");
80     con_out("\n");
81     con_out("flags:\n"
82             "  -fadjust-vector-fields\n"
83             "            when assigning a vector field, its _y and _z fields also get assigned\n"
84            );
85     return -1;
86 }
87
88 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
89     size_t i;
90
91     for (i = 0; i < listsize; ++i) {
92         if (!strcmp(name, list[i].name)) {
93             longbit lb = list[i].bit;
94 #if 0
95             if (on)
96                 flags[lb.idx] |= (1<<(lb.bit));
97             else
98                 flags[lb.idx] &= ~(1<<(lb.bit));
99 #else
100             if (on)
101                 flags[0] |= (1<<lb);
102             else
103                 flags[0] &= ~(1<<(lb));
104 #endif
105             return true;
106         }
107     }
108     return false;
109 }
110 static bool options_setflag(const char *name, bool on) {
111     return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
112 }
113 static bool options_setwarn(const char *name, bool on) {
114     return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
115 }
116
117 static bool options_witharg(int *argc_, char ***argv_, char **out) {
118     int  argc   = *argc_;
119     char **argv = *argv_;
120
121     if (argv[0][2]) {
122         *out = argv[0]+2;
123         return true;
124     }
125     /* eat up the next */
126     if (argc < 2) /* no parameter was provided */
127         return false;
128
129     *out = argv[1];
130     --*argc_;
131     ++*argv_;
132     return true;
133 }
134
135 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
136     int  argc   = *argc_;
137     char **argv = *argv_;
138
139     size_t len = strlen(optname);
140
141     if (strncmp(argv[0]+ds, optname, len))
142         return false;
143
144     /* it's --optname, check how the parameter is supplied */
145     if (argv[0][ds+len] == '=') {
146         /* using --opt=param */
147         *out = argv[0]+ds+len+1;
148         return true;
149     }
150
151     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
152         return false;
153
154     /* using --opt param */
155     *out = argv[1];
156     --*argc_;
157     ++*argv_;
158     return true;
159 }
160 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
161     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
162 }
163 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
164     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
165 }
166
167 static void options_set(uint32_t *flags, size_t idx, bool on)
168 {
169     longbit lb = LONGBIT(idx);
170 #if 0
171     if (on)
172         flags[lb.idx] |= (1<<(lb.bit));
173     else
174         flags[lb.idx] &= ~(1<<(lb.bit));
175 #else
176     if (on)
177         flags[0] |= (1<<(lb));
178     else
179         flags[0] &= ~(1<<(lb));
180 #endif
181 }
182
183 static bool options_parse(int argc, char **argv) {
184     bool argend = false;
185     size_t itr;
186     char  buffer[1024];
187     char *redirout = (char*)stdout;
188     char *redirerr = (char*)stderr;
189
190     while (!argend && argc > 1) {
191         char *argarg;
192         argitem item;
193
194         ++argv;
195         --argc;
196
197         if (argv[0][0] == '-') {
198     /* All gcc-type long options */
199             if (options_long_gcc("std", &argc, &argv, &argarg)) {
200                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
201                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
202                     opts_standard = COMPILER_GMQCC;
203                 } else if (!strcmp(argarg, "qcc")) {
204                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
205                     opts_standard = COMPILER_QCC;
206                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
207                     options_set(opts_flags, FTEPP,                true);
208                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
209                     opts_standard = COMPILER_FTEQCC;
210                 } else if (!strcmp(argarg, "qccx")) {
211                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
212                     opts_standard = COMPILER_QCCX;
213                 } else {
214                     con_out("Unknown standard: %s\n", argarg);
215                     return false;
216                 }
217                 continue;
218             }
219             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
220                 opts_forcecrc = true;
221                 opts_forced_crc = strtol(argarg, NULL, 0);
222                 continue;
223             }
224             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
225                 con_change(redirout, redirerr);
226                 continue;
227             }
228             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
229                 con_change(redirout, redirerr);
230                 continue;
231             }
232
233             if (!strcmp(argv[0]+1, "debug")) {
234                 opts_debug = true;
235                 continue;
236             }
237             if (!strcmp(argv[0]+1, "dump")) {
238                 opts_dump = true;
239                 continue;
240             }
241             if (!strcmp(argv[0]+1, "dumpfin")) {
242                 opts_dumpfin = true;
243                 continue;
244             }
245             if (!strcmp(argv[0]+1, "memchk")) {
246                 opts_memchk = true;
247                 continue;
248             }
249             if (!strcmp(argv[0]+1, "nocolor")) {
250                 con_color(0);
251                 continue;
252             }
253
254             switch (argv[0][1]) {
255                 /* -h, show usage but exit with 0 */
256                 case 'h':
257                     usage();
258                     exit(0);
259                     /* break; never reached because of exit(0) */
260
261                 case 'E':
262                     opts_pp_only = true;
263                     break;
264
265                 /* handle all -fflags */
266                 case 'f':
267                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
268                     if (!strcmp(argv[0]+2, "HELP")) {
269                         con_out("Possible flags:\n");
270                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
271                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
272                             con_out(" -f%s\n", buffer);
273                         }
274                         exit(0);
275                     }
276                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
277                         if (!options_setflag(argv[0]+5, false)) {
278                             con_out("unknown flag: %s\n", argv[0]+2);
279                             return false;
280                         }
281                     }
282                     else if (!options_setflag(argv[0]+2, true)) {
283                         con_out("unknown flag: %s\n", argv[0]+2);
284                         return false;
285                     }
286                     break;
287                 case 'W':
288                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
289                     if (!strcmp(argv[0]+2, "HELP")) {
290                         con_out("Possible warnings:\n");
291                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
292                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
293                             con_out(" -W%s\n", buffer);
294                         }
295                         exit(0);
296                     }
297                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
298                         opts_werror = false;
299                         break;
300                     }
301                     else if (!strcmp(argv[0]+2, "ERROR")) {
302                         opts_werror = true;
303                         break;
304                     }
305                     else if (!strcmp(argv[0]+2, "NONE")) {
306                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
307                             opts_warn[itr] = 0;
308                         break;
309                     }
310                     else if (!strcmp(argv[0]+2, "ALL")) {
311                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
312                             opts_warn[itr] = 0xFFFFFFFFL;
313                         break;
314                     }
315                     if (!strncmp(argv[0]+2, "NO_", 3)) {
316                         if (!options_setwarn(argv[0]+5, false)) {
317                             con_out("unknown warning: %s\n", argv[0]+2);
318                             return false;
319                         }
320                     }
321                     else if (!options_setwarn(argv[0]+2, true)) {
322                         con_out("unknown warning: %s\n", argv[0]+2);
323                         return false;
324                     }
325                     break;
326
327                 case 'O':
328                     if (!options_witharg(&argc, &argv, &argarg)) {
329                         con_out("option -O requires a numerical argument\n");
330                         return false;
331                     }
332                     opts_O = atoi(argarg);
333                     break;
334
335                 case 'o':
336                     if (!options_witharg(&argc, &argv, &argarg)) {
337                         con_out("option -o requires an argument: the output file name\n");
338                         return false;
339                     }
340                     opts_output = argarg;
341                     opts_output_wasset = true;
342                     break;
343
344                 case 'a':
345                 case 's':
346                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
347                     if (!options_witharg(&argc, &argv, &argarg)) {
348                         con_out("option -a requires a filename %s\n",
349                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
350                         return false;
351                     }
352                     item.filename = argarg;
353                     vec_push(items, item);
354                     break;
355
356                 case '-':
357                     if (!argv[0][2]) {
358                         /* anything following -- is considered a non-option argument */
359                         argend = true;
360                         break;
361                     }
362             /* All long options without arguments */
363                     else if (!strcmp(argv[0]+2, "help")) {
364                         usage();
365                         exit(0);
366                     }
367                     else {
368             /* All long options with arguments */
369                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
370                             opts_output = argarg;
371                             opts_output_wasset = true;
372                         } else {
373                             con_out("Unknown parameter: %s\n", argv[0]);
374                             return false;
375                         }
376                     }
377                     break;
378
379                 default:
380                     con_out("Unknown parameter: %s\n", argv[0]);
381                     return false;
382             }
383         }
384         else
385         {
386             /* it's a QC filename */
387             item.filename = argv[0];
388             item.type     = TYPE_QC;
389             vec_push(items, item);
390         }
391     }
392     return true;
393 }
394
395 /* returns the line number, or -1 on error */
396 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
397     int    len;
398     char  *line;
399     char  *start;
400     char  *end;
401
402     line = *out;
403     len = util_getline(&line, alen, src);
404     if (len == -1)
405         return false;
406
407     /* start at first non-blank */
408     for (start = line; isspace(*start); ++start) {}
409     /* end at the first non-blank */
410     for (end = start;  *end && !isspace(*end);  ++end)   {}
411
412     *out = line;
413     /* move the actual filename to the beginning */
414     while (start != end) {
415         *line++ = *start++;
416     }
417     *line = 0;
418     return true;
419 }
420
421 int main(int argc, char **argv) {
422     size_t itr;
423     int retval = 0;
424     bool opts_output_free = false;
425     bool progs_src = false;
426     FILE *outfile = NULL;
427
428     app_name = argv[0];
429     con_init();
430
431     /* default options / warn flags */
432     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
433     options_set(opts_warn, WARN_EXTENSIONS, true);
434     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
435     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
436     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
437     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
438     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
439     options_set(opts_warn, WARN_VOID_VARIABLES, true);
440     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
441     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
442     options_set(opts_warn, WARN_FRAME_MACROS, true);
443     options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
444     options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
445     options_set(opts_warn, WARN_END_SYS_FIELDS, true);
446     options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
447     options_set(opts_warn, WARN_PREPROCESSOR, true);
448     options_set(opts_warn, WARN_MULTIFILE_IF, true);
449
450     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
451     options_set(opts_flags, FTEPP, false);
452
453     if (!options_parse(argc, argv)) {
454         return usage();
455     }
456
457     /* the standard decides which set of operators to use */
458     if (opts_standard == COMPILER_GMQCC) {
459         operators = c_operators;
460         operator_count = c_operator_count;
461     } else {
462         operators = qcc_operators;
463         operator_count = qcc_operator_count;
464     }
465
466     if (opts_dump) {
467         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
468             con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
469         }
470         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
471             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
472         }
473         con_out("output = %s\n", opts_output);
474         con_out("optimization level = %i\n", (int)opts_O);
475         con_out("standard = %i\n", opts_standard);
476     }
477
478     if (opts_pp_only) {
479         if (opts_output_wasset) {
480             outfile = util_fopen(opts_output, "wb");
481             if (!outfile) {
482                 con_err("failed to open `%s` for writing\n", opts_output);
483                 retval = 1;
484                 goto cleanup;
485             }
486         }
487         else
488             outfile = stdout;
489     }
490
491     if (!opts_pp_only) {
492         if (!parser_init()) {
493             con_err("failed to initialize parser\n");
494             retval = 1;
495             goto cleanup;
496         }
497     }
498     if (opts_pp_only || OPTS_FLAG(FTEPP)) {
499         if (!ftepp_init()) {
500             con_err("failed to initialize parser\n");
501             retval = 1;
502             goto cleanup;
503         }
504     }
505
506     util_debug("COM", "starting ...\n");
507
508     if (!vec_size(items)) {
509         FILE *src;
510         char *line;
511         size_t linelen = 0;
512
513         progs_src = true;
514
515         src = util_fopen("progs.src", "rb");
516         if (!src) {
517             con_err("failed to open `progs.src` for reading\n");
518             retval = 1;
519             goto cleanup;
520         }
521
522         line = NULL;
523         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
524             con_err("illformatted progs.src file: expected output filename in first line\n");
525             retval = 1;
526             goto srcdone;
527         }
528
529         if (!opts_output_wasset) {
530             opts_output = util_strdup(line);
531             opts_output_free = true;
532         }
533
534         while (progs_nextline(&line, &linelen, src)) {
535             argitem item;
536             if (!line[0] || (line[0] == '/' && line[1] == '/'))
537                 continue;
538             item.filename = util_strdup(line);
539             item.type     = TYPE_QC;
540             vec_push(items, item);
541         }
542
543 srcdone:
544         fclose(src);
545         mem_d(line);
546     }
547
548     if (retval)
549         goto cleanup;
550
551     if (vec_size(items)) {
552         if (!opts_pp_only) {
553             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
554             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
555         }
556         for (itr = 0; itr < vec_size(items); ++itr) {
557             if (!opts_pp_only) {
558                 con_out("  item: %s (%s)\n",
559                        items[itr].filename,
560                        ( (items[itr].type == TYPE_QC ? "qc" :
561                          (items[itr].type == TYPE_ASM ? "asm" :
562                          (items[itr].type == TYPE_SRC ? "progs.src" :
563                          ("unknown"))))));
564             }
565
566             if (opts_pp_only) {
567                 if (!ftepp_preprocess_file(items[itr].filename)) {
568                     retval = 1;
569                     goto cleanup;
570                 }
571                 fprintf(outfile, "%s", ftepp_get());
572                 ftepp_flush();
573             }
574             else {
575                 if (OPTS_FLAG(FTEPP)) {
576                     const char *data;
577                     if (!ftepp_preprocess_file(items[itr].filename)) {
578                         retval = 1;
579                         goto cleanup;
580                     }
581                     data = ftepp_get();
582                     if (!parser_compile_string_len(items[itr].filename, data, vec_size(data)-1)) {
583                         retval = 1;
584                         goto cleanup;
585                     }
586                     ftepp_flush();
587                 }
588                 else {
589                     if (!parser_compile_file(items[itr].filename)) {
590                         retval = 1;
591                         goto cleanup;
592                     }
593                 }
594             }
595
596             if (progs_src) {
597                 mem_d(items[itr].filename);
598                 items[itr].filename = NULL;
599             }
600         }
601
602         ftepp_finish();
603         if (!opts_pp_only) {
604             if (!parser_finish(opts_output)) {
605                 retval = 1;
606                 goto cleanup;
607             }
608         }
609     }
610
611     /* stuff */
612
613 cleanup:
614     util_debug("COM", "cleaning ...\n");
615     ftepp_finish();
616     con_close();
617     vec_free(items);
618
619     if (!opts_pp_only)
620         parser_cleanup();
621     if (opts_output_free)
622         mem_d((char*)opts_output);
623
624     lex_cleanup();
625     util_meminfo();
626     return retval;
627 }