2 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
44 #include "directive.h"
50 .input_allow_dollars = false,
57 .output_linenumbers = true,
58 .output_cheaplinenumbers = false,
59 .output_retain_comments = false,
63 .depend_report_system = false,
64 .depend_assume_generated = false,
65 .depend_issue_fakerules = false,
66 .depend_quote_target = true,
67 .depend_target = NULL,
70 .do_macrolist = false,
71 .macrolist_include_stddef = false,
72 .macrolist_include_expansions = false,
75 .trace_namesonly = false,
76 .trace_indented = false,
79 struct warns warns = {
86 ////////////////////////////////////////////////////////////
89 struct commandline_macro {
93 const char *expansion;
96 static struct array commandline_macros;
100 commandline_macros_init(void)
102 array_init(&commandline_macros);
107 commandline_macros_cleanup(void)
110 struct commandline_macro *cm;
112 num = array_num(&commandline_macros);
113 for (i=0; i<num; i++) {
114 cm = array_get(&commandline_macros, i);
115 dofree(cm, sizeof(*cm));
117 array_setsize(&commandline_macros, 0);
119 array_cleanup(&commandline_macros);
124 commandline_macro_add(const struct place *p, const char *macro,
125 const struct place *p2, const char *expansion)
127 struct commandline_macro *cm;
129 cm = domalloc(sizeof(*cm));
133 cm->expansion = expansion;
135 array_add(&commandline_macros, cm, NULL);
140 commandline_def(const struct place *p, char *str)
146 complain(NULL, "-D: macro name expected");
150 val = strchr(str, '=');
158 place_addcolumns(&p2, strlen(str));
160 place_setbuiltin(&p2, 1);
162 commandline_macro_add(p, str, &p2, val ? val : "1");
167 commandline_undef(const struct place *p, char *str)
170 complain(NULL, "-U: macro name expected");
173 commandline_macro_add(p, str, p, NULL);
178 apply_commandline_macros(void)
180 struct commandline_macro *cm;
183 num = array_num(&commandline_macros);
184 for (i=0; i<num; i++) {
185 cm = array_get(&commandline_macros, i);
186 if (cm->expansion != NULL) {
187 macro_define_plain(&cm->where, cm->macro,
188 &cm->where2, cm->expansion);
190 macro_undef(cm->macro);
192 dofree(cm, sizeof(*cm));
194 array_setsize(&commandline_macros, 0);
199 apply_magic_macro(unsigned num, const char *name)
203 place_setbuiltin(&p, num);
204 macro_define_magic(&p, name);
209 apply_builtin_macro(unsigned num, const char *name, const char *val)
213 place_setbuiltin(&p, num);
214 macro_define_plain(&p, name, &p, val);
219 apply_builtin_macros(void)
223 apply_magic_macro(n++, "__FILE__");
224 apply_magic_macro(n++, "__LINE__");
227 apply_builtin_macro(n++, CONFIG_OS, "1");
230 apply_builtin_macro(n++, CONFIG_OS_2, "1");
234 apply_builtin_macro(n++, CONFIG_CPU, "1");
237 apply_builtin_macro(n++, CONFIG_CPU_2, "1");
241 apply_builtin_macro(n++, CONFIG_SIZE, "1");
244 apply_builtin_macro(n++, CONFIG_BINFMT, "1");
247 #ifdef CONFIG_COMPILER
248 apply_builtin_macro(n++, CONFIG_COMPILER, VERSION_MAJOR);
249 apply_builtin_macro(n++, CONFIG_COMPILER_MINOR, VERSION_MINOR);
250 apply_builtin_macro(n++, "__VERSION__", VERSION_LONG);
254 ////////////////////////////////////////////////////////////
255 // extra included files
257 struct commandline_file {
260 bool suppress_output;
263 static struct array commandline_files;
267 commandline_files_init(void)
269 array_init(&commandline_files);
274 commandline_files_cleanup(void)
277 struct commandline_file *cf;
279 num = array_num(&commandline_files);
280 for (i=0; i<num; i++) {
281 cf = array_get(&commandline_files, i);
283 dofree(cf, sizeof(*cf));
286 array_setsize(&commandline_files, 0);
288 array_cleanup(&commandline_files);
293 commandline_addfile(const struct place *p, char *name, bool suppress_output)
295 struct commandline_file *cf;
297 cf = domalloc(sizeof(*cf));
300 cf->suppress_output = suppress_output;
301 array_add(&commandline_files, cf, NULL);
306 commandline_addfile_output(const struct place *p, char *name)
308 commandline_addfile(p, name, false);
313 commandline_addfile_nooutput(const struct place *p, char *name)
315 commandline_addfile(p, name, true);
320 read_commandline_files(void)
322 struct commandline_file *cf;
326 num = array_num(&commandline_files);
327 for (i=0; i<num; i++) {
328 cf = array_get(&commandline_files, i);
329 array_set(&commandline_files, i, NULL);
330 if (cf->suppress_output) {
331 save = mode.do_output;
332 mode.do_output = false;
333 file_readquote(&cf->where, cf->name);
334 mode.do_output = save;
336 file_readquote(&cf->where, cf->name);
338 dofree(cf, sizeof(*cf));
340 array_setsize(&commandline_files, 0);
343 ////////////////////////////////////////////////////////////
344 // include path accumulation
346 static struct stringarray incpath_quote;
347 static struct stringarray incpath_user;
348 static struct stringarray incpath_system;
349 static struct stringarray incpath_late;
350 static const char *sysroot;
356 stringarray_init(&incpath_quote);
357 stringarray_init(&incpath_user);
358 stringarray_init(&incpath_system);
359 stringarray_init(&incpath_late);
364 incpath_cleanup(void)
366 stringarray_setsize(&incpath_quote, 0);
367 stringarray_setsize(&incpath_user, 0);
368 stringarray_setsize(&incpath_system, 0);
369 stringarray_setsize(&incpath_late, 0);
371 stringarray_cleanup(&incpath_quote);
372 stringarray_cleanup(&incpath_user);
373 stringarray_cleanup(&incpath_system);
374 stringarray_cleanup(&incpath_late);
379 commandline_isysroot(const struct place *p, char *dir)
387 commandline_addincpath(struct stringarray *arr, char *s)
390 complain(NULL, "Empty include directory");
393 stringarray_add(arr, s, NULL);
398 commandline_addincpath_quote(const struct place *p, char *dir)
401 commandline_addincpath(&incpath_quote, dir);
406 commandline_addincpath_user(const struct place *p, char *dir)
409 commandline_addincpath(&incpath_user, dir);
414 commandline_addincpath_system(const struct place *p, char *dir)
417 commandline_addincpath(&incpath_system, dir);
422 commandline_addincpath_late(const struct place *p, char *dir)
425 commandline_addincpath(&incpath_late, dir);
430 loadincludepath(void)
436 num = stringarray_num(&incpath_quote);
437 for (i=0; i<num; i++) {
438 dir = stringarray_get(&incpath_quote, i);
439 files_addquotepath(dir, false);
441 files_addquotepath(NULL, false);
443 num = stringarray_num(&incpath_user);
444 for (i=0; i<num; i++) {
445 dir = stringarray_get(&incpath_user, i);
446 files_addquotepath(dir, false);
447 files_addbracketpath(dir, false);
450 if (mode.do_stdinc) {
451 if (sysroot != NULL) {
452 t = dostrdup3(sysroot, "/", CONFIG_LOCALINCLUDE);
456 dir = CONFIG_LOCALINCLUDE;
458 files_addquotepath(dir, true);
459 files_addbracketpath(dir, true);
461 if (sysroot != NULL) {
462 t = dostrdup3(sysroot, "/", CONFIG_SYSTEMINCLUDE);
466 dir = CONFIG_SYSTEMINCLUDE;
468 files_addquotepath(dir, true);
469 files_addbracketpath(dir, true);
472 num = stringarray_num(&incpath_system);
473 for (i=0; i<num; i++) {
474 dir = stringarray_get(&incpath_system, i);
475 files_addquotepath(dir, true);
476 files_addbracketpath(dir, true);
479 num = stringarray_num(&incpath_late);
480 for (i=0; i<num; i++) {
481 dir = stringarray_get(&incpath_late, i);
482 files_addquotepath(dir, false);
483 files_addbracketpath(dir, false);
487 ////////////////////////////////////////////////////////////
488 // silly commandline stuff
490 static const char *commandline_prefix;
494 commandline_setprefix(const struct place *p, char *prefix)
497 commandline_prefix = prefix;
502 commandline_addincpath_user_withprefix(const struct place *p, char *dir)
506 if (commandline_prefix == NULL) {
507 complain(NULL, "-iprefix needed");
510 s = dostrdup3(commandline_prefix, "/", dir);
512 commandline_addincpath_user(p, s);
517 commandline_addincpath_late_withprefix(const struct place *p, char *dir)
521 if (commandline_prefix == NULL) {
522 complain(NULL, "-iprefix needed");
525 s = dostrdup3(commandline_prefix, "/", dir);
527 commandline_addincpath_late(p, s);
532 commandline_setstd(const struct place *p, char *std)
536 if (!strcmp(std, "krc")) {
539 complain(NULL, "Standard %s not supported by this preprocessor", std);
545 commandline_setlang(const struct place *p, char *lang)
549 if (!strcmp(lang, "c") || !strcmp(lang, "assembler-with-cpp")) {
552 complain(NULL, "Language %s not supported by this preprocessor", lang);
556 ////////////////////////////////////////////////////////////
561 commandline_iremap(const struct place *p, char *str)
566 complain(NULL, "-iremap not supported");
572 commandline_tabstop(const struct place *p, char *s)
581 /* should not happen */
582 complain(NULL, "Invalid tabstop");
587 val = strtoul(t, &t, 10);
588 if (errno || *t != '\0') {
589 complain(NULL, "Invalid tabstop");
593 complain(NULL, "Preposterously large tabstop");
596 mode.input_tabstop = val;
607 mode.do_macrolist = true;
608 mode.macrolist_include_stddef = false;
609 mode.macrolist_include_expansions = true;
616 mode.do_macrolist = true;
617 mode.macrolist_include_stddef = true;
618 mode.macrolist_include_expansions = true;
619 mode.do_output = false;
626 mode.do_macrolist = true;
627 mode.macrolist_include_stddef = false;
628 mode.macrolist_include_expansions = false;
639 mode.do_trace = true;
640 mode.trace_namesonly = false;
641 mode.trace_indented = false;
648 mode.do_trace = true;
649 mode.trace_namesonly = true;
650 mode.trace_indented = true;
659 commandline_setdependtarget(const struct place *p, char *str)
662 mode.depend_target = str;
663 mode.depend_quote_target = false;
668 commandline_setdependtarget_quoted(const struct place *p, char *str)
671 mode.depend_target = str;
672 mode.depend_quote_target = true;
677 commandline_setdependoutput(const struct place *p, char *str)
680 mode.depend_file = str;
687 mode.do_depend = true;
688 mode.depend_report_system = true;
689 mode.do_output = false;
696 mode.do_depend = true;
697 mode.depend_report_system = false;
698 mode.do_output = false;
705 mode.do_depend = true;
706 mode.depend_report_system = true;
711 commandline_MMD(void)
713 mode.do_depend = true;
714 mode.depend_report_system = false;
719 commandline_wall(void)
721 warns.nestcomment = true;
728 commandline_wnoall(void)
730 warns.nestcomment = false;
732 warns.unused = false;
737 commandline_wnone(void)
739 warns.nestcomment = false;
740 warns.endiflabels = false;
742 warns.unused = false;
745 ////////////////////////////////////////////////////////////
748 struct ignore_option {
763 struct prefix_option {
765 void (*func)(const struct place *, char *);
770 void (*func)(const struct place *, char *);
773 static const struct ignore_option ignore_options[] = {
777 static const unsigned num_ignore_options = HOWMANY(ignore_options);
779 static const struct flag_option flag_options[] = {
780 { "C", &mode.output_retain_comments, true },
781 { "CC", &mode.output_retain_comments, true },
782 { "MG", &mode.depend_assume_generated, true },
783 { "MP", &mode.depend_issue_fakerules, true },
784 { "P", &mode.output_linenumbers, false },
785 { "Wcomment", &warns.nestcomment, true },
786 { "Wendif-labels", &warns.endiflabels, true },
787 { "Werror", &mode.werror, true },
788 { "Wno-comment", &warns.nestcomment, false },
789 { "Wno-endif-labels", &warns.endiflabels, false },
790 { "Wno-error", &mode.werror, false },
791 { "Wno-undef", &warns.undef, false },
792 { "Wno-unused-macros", &warns.unused, false },
793 { "Wundef", &warns.undef, true },
794 { "Wunused-macros", &warns.unused, true },
795 { "fdollars-in-identifiers", &mode.input_allow_dollars, true },
796 { "fno-dollars-in-identifiers", &mode.input_allow_dollars, false },
797 { "nostdinc", &mode.do_stdinc, false },
798 { "p", &mode.output_cheaplinenumbers, true },
799 { "undef", &mode.do_stddef, false },
801 static const unsigned num_flag_options = HOWMANY(flag_options);
803 static const struct act_option act_options[] = {
804 { "H", commandline_H },
805 { "M", commandline_M },
806 { "MD", commandline_MD },
807 { "MM", commandline_MM },
808 { "MMD", commandline_MMD },
809 { "Wall", commandline_wall },
810 { "Wno-all", commandline_wnoall },
811 { "dD", commandline_dD },
812 { "dI", commandline_dI },
813 { "dM", commandline_dM },
814 { "dN", commandline_dN },
815 { "w", commandline_wnone },
817 static const unsigned num_act_options = HOWMANY(act_options);
819 static const struct prefix_option prefix_options[] = {
820 { "D", commandline_def },
821 { "I", commandline_addincpath_user },
822 { "U", commandline_undef },
823 { "ftabstop=", commandline_tabstop },
824 { "std=", commandline_setstd },
826 static const unsigned num_prefix_options = HOWMANY(prefix_options);
828 static const struct arg_option arg_options[] = {
829 { "MF", commandline_setdependoutput },
830 { "MQ", commandline_setdependtarget_quoted },
831 { "MT", commandline_setdependtarget },
832 { "debuglog", debuglog_open },
833 { "idirafter", commandline_addincpath_late },
834 { "imacros", commandline_addfile_nooutput },
835 { "include", commandline_addfile_output },
836 { "iprefix", commandline_setprefix },
837 { "iquote", commandline_addincpath_quote },
838 { "iremap", commandline_iremap },
839 { "isysroot", commandline_isysroot },
840 { "isystem", commandline_addincpath_system },
841 { "iwithprefix", commandline_addincpath_late_withprefix },
842 { "iwithprefixbefore", commandline_addincpath_user_withprefix },
843 { "x", commandline_setlang },
845 static const unsigned num_arg_options = HOWMANY(arg_options);
849 check_ignore_option(const char *opt)
854 for (i=0; i<num_ignore_options; i++) {
855 r = strcmp(opt, ignore_options[i].string);
868 check_flag_option(const char *opt)
873 for (i=0; i<num_flag_options; i++) {
874 r = strcmp(opt, flag_options[i].string);
876 *flag_options[i].flag = flag_options[i].setto;
888 check_act_option(const char *opt)
893 for (i=0; i<num_act_options; i++) {
894 r = strcmp(opt, act_options[i].string);
896 act_options[i].func();
908 check_prefix_option(const struct place *p, char *opt)
913 for (i=0; i<num_prefix_options; i++) {
914 len = strlen(prefix_options[i].string);
915 r = strncmp(opt, prefix_options[i].string, len);
917 prefix_options[i].func(p, opt + len);
929 check_arg_option(const char *opt, const struct place *argplace, char *arg)
934 for (i=0; i<num_arg_options; i++) {
935 r = strcmp(opt, arg_options[i].string);
939 "Option -%s requires an argument",
943 arg_options[i].func(argplace, arg);
955 usage(const char *progname, const char *fmt, ...)
959 fprintf(stderr, "%s: ", progname);
961 vfprintf(stderr, fmt, ap);
963 fprintf(stderr, "\n");
965 fprintf(stderr, "usage: %s [options] [infile [outfile]]\n", progname);
966 fprintf(stderr, "Common options:\n");
967 fprintf(stderr, " -C Retain comments\n");
968 fprintf(stderr, " -Dmacro[=def] Predefine macro\n");
969 fprintf(stderr, " -Idir Add to include path\n");
970 fprintf(stderr, " -M Issue depend info\n");
971 fprintf(stderr, " -MD Issue depend info and output\n");
972 fprintf(stderr, " -MM -M w/o system headers\n");
973 fprintf(stderr, " -MMD -MD w/o system headers\n");
974 fprintf(stderr, " -nostdinc Drop default include path\n");
975 fprintf(stderr, " -Umacro Undefine macro\n");
976 fprintf(stderr, " -undef Undefine everything\n");
977 fprintf(stderr, " -Wall Enable all warnings\n");
978 fprintf(stderr, " -Werror Make warnings into errors\n");
979 fprintf(stderr, " -w Disable all warnings\n");
983 ////////////////////////////////////////////////////////////
986 static struct stringarray freestrings;
992 stringarray_init(&freestrings);
995 commandline_macros_init();
996 commandline_files_init();
1011 directive_cleanup();
1015 commandline_files_cleanup();
1016 commandline_macros_cleanup();
1020 num = stringarray_num(&freestrings);
1021 for (i=0; i<num; i++) {
1022 dostrfree(stringarray_get(&freestrings, i));
1024 stringarray_setsize(&freestrings, 0);
1025 stringarray_cleanup(&freestrings);
1036 freestringlater(char *s)
1038 stringarray_add(&freestrings, s, NULL);
1041 ////////////////////////////////////////////////////////////
1045 main(int argc, char *argv[])
1047 const char *progname;
1048 const char *inputfile = NULL;
1049 const char *outputfile = NULL;
1050 struct place cmdplace;
1053 progname = strrchr(argv[0], '/');
1054 progname = progname == NULL ? argv[0] : progname + 1;
1055 complain_init(progname);
1059 for (i=1; i<argc; i++) {
1060 if (argv[i][0] != '-' || argv[i][1] == 0) {
1063 place_setcommandline(&cmdplace, i, 1);
1064 if (check_ignore_option(argv[i]+1)) {
1067 if (check_flag_option(argv[i]+1)) {
1070 if (check_act_option(argv[i]+1)) {
1073 if (check_prefix_option(&cmdplace, argv[i]+1)) {
1076 place_setcommandline(&cmdplace, i+1, 1);
1077 if (check_arg_option(argv[i]+1, &cmdplace, argv[i+1])) {
1081 usage(progname, "Invalid option %s", argv[i]);
1084 inputfile = argv[i++];
1085 if (!strcmp(inputfile, "-")) {
1090 outputfile = argv[i++];
1091 if (!strcmp(outputfile, "-")) {
1096 usage(progname, "Extra non-option argument %s", argv[i]);
1099 mode.output_file = outputfile;
1102 apply_builtin_macros();
1103 apply_commandline_macros();
1104 read_commandline_files();
1105 place_setnowhere(&cmdplace);
1106 file_readabsolute(&cmdplace, inputfile);
1109 if (complain_failed()) {
1110 return EXIT_FAILURE;
1112 return EXIT_SUCCESS;