1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Neil Booth.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "cppdefault.h"
26 #include "c-incpath.h"
31 #ifndef DOLLARS_IN_IDENTIFIERS
32 # define DOLLARS_IN_IDENTIFIERS true
35 #ifndef TARGET_SYSTEM_ROOT
36 # define TARGET_SYSTEM_ROOT NULL
40 static cpp_options *cpp_opts;
43 static const char *this_input_filename;
45 /* Filename and stream for preprocessed output. */
46 static const char *out_fname;
47 static FILE *out_stream;
49 /* Append dependencies to deps_file. */
50 static bool deps_append;
52 /* If dependency switches (-MF etc.) have been given. */
53 static bool deps_seen;
58 /* Dependency output file. */
59 static const char *deps_file;
61 /* The prefix given by -iprefix, if any. */
62 static const char *iprefix;
64 /* The multilib directory given by -imultilib, if any. */
65 static const char *imultilib;
67 /* The system root, if any. Overridden by -isysroot. */
68 static const char *sysroot = TARGET_SYSTEM_ROOT;
70 /* Zero disables all standard directories for headers. */
71 static bool std_inc = true;
73 /* If the quote chain has been split by -I-. */
74 static bool quote_chain_split;
76 /* If -Wunused-macros. */
77 static bool warn_unused_macros;
79 /* If -Wvariadic-macros. */
80 static bool warn_variadic_macros = true;
82 /* Number of deferred options. */
83 static size_t deferred_count;
85 /* Number of deferred options scanned for -include. */
86 static size_t include_cursor;
88 static void handle_OPT_d (const char *);
89 static void set_std_c89 (int, int);
90 static void set_std_c99 (int);
91 static void check_deps_environment_vars (void);
92 static void handle_deferred_opts (void);
93 static void sanitize_cpp_opts (void);
94 static void add_prefixed_path (const char *, size_t);
95 static void push_command_line_include (void);
96 static void cb_file_change (cpp_reader *, const struct line_map *);
97 static void cb_dir_change (cpp_reader *, const char *);
98 static void finish_options (void);
100 #ifndef STDC_0_IN_SYSTEM_HEADERS
101 #define STDC_0_IN_SYSTEM_HEADERS 0
104 /* Holds switches parsed by sdcpp_common_handle_option (), but whose
105 handling is deferred to sdcpp_common_post_options (). */
106 static void defer_opt (enum opt_code, const char *);
107 static struct deferred_opt
113 /* Complain that switch CODE expects an argument but none was
114 provided. OPT was the command-line option. Return FALSE to get
115 the default message in opts.c, TRUE if we provide a specialized
118 sdcpp_common_missing_argument (const char *opt, size_t code)
123 /* Pick up the default message. */
127 error ("assertion missing after \"%s\"", opt);
132 error ("macro name missing after \"%s\"", opt);
140 error ("missing path after \"%s\"", opt);
149 error ("missing filename after \"%s\"", opt);
154 error ("missing makefile target after \"%s\"", opt);
161 /* Defer option CODE with argument ARG. */
163 defer_opt (enum opt_code code, const char *arg)
165 deferred_opts[deferred_count].code = code;
166 deferred_opts[deferred_count].arg = arg;
170 /* Common initialization before parsing options. */
172 sdcpp_common_init_options (unsigned int argc, const char **argv ATTRIBUTE_UNUSED)
174 parse_in = cpp_create_reader (CLK_GNUC89, NULL, &line_table);
176 cpp_opts = cpp_get_options (parse_in);
177 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
180 /* Reset to avoid warnings on internal definitions. We set it just
181 before passing on command-line options to cpplib. */
182 cpp_opts->warn_dollars = 0;
184 deferred_opts = XNEWVEC (struct deferred_opt, argc);
189 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
190 form of an -f or -W option was given. Returns 0 if the switch was
191 invalid, a negative number to prevent language-independent
192 processing in toplev.c (a hack necessary for the short-term). */
194 sdcpp_common_handle_option (size_t scode, const char *arg, int value)
196 enum opt_code code = (enum opt_code) scode;
205 #if 0 // pch not supported by sdcpp
206 case OPT__output_pch_:
212 defer_opt (code, arg);
216 cpp_opts->discard_comments = 0;
220 cpp_opts->discard_comments = 0;
221 cpp_opts->discard_comments_in_macro_exp = 0;
225 defer_opt (code, arg);
229 cpp_opts->print_include_names = 1;
233 if (strcmp (arg, "-"))
234 add_path (xstrdup (arg), BRACKET, 0, true);
237 if (quote_chain_split)
238 error ("-I- specified twice");
239 quote_chain_split = true;
240 split_quote_chain ();
241 inform ("obsolete option -I- used, please use -iquote instead");
247 /* When doing dependencies with -M or -MM, suppress normal
248 preprocessed output, but still do -dM etc. as software
249 depends on this. Preprocessed output does occur if -MD, -MMD
250 or environment var dependency generation is used. */
251 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
253 cpp_opts->inhibit_warnings = 1;
258 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
269 cpp_opts->deps.missing_files = true;
274 cpp_opts->deps.phony_targets = true;
280 defer_opt (code, arg);
284 flag_no_line_commands = 1;
287 case OPT_fworking_directory:
288 flag_working_directory = value;
292 defer_opt (code, arg);
296 cpp_opts->warn_trigraphs = value;
297 cpp_opts->warn_comments = value;
298 cpp_opts->warn_num_sign_change = value;
299 cpp_opts->warn_multichar = value; /* Was C++ only. */
304 cpp_opts->warn_comments = value;
307 case OPT_Wdeprecated:
308 cpp_opts->warn_deprecated = value;
311 case OPT_Wendif_labels:
312 cpp_opts->warn_endif_labels = value;
316 cpp_opts->warnings_are_errors = value;
320 /* Silently ignore for now. */
323 #if 0 // pch not supported by sdcpp
324 case OPT_Winvalid_pch:
325 cpp_opts->warn_invalid_pch = value;
329 case OPT_Wsystem_headers:
330 cpp_opts->warn_system_headers = value;
333 case OPT_Wtraditional:
334 cpp_opts->warn_traditional = value;
338 cpp_opts->warn_trigraphs = value;
342 cpp_opts->warn_undef = value;
345 case OPT_Wunused_macros:
346 warn_unused_macros = value;
349 case OPT_Wvariadic_macros:
350 warn_variadic_macros = value;
354 set_std_c89 (false, true);
361 case OPT_fdollars_in_identifiers:
362 cpp_opts->dollars_in_ident = value;
365 case OPT_fsigned_char:
366 flag_signed_char = value;
369 case OPT_funsigned_char:
370 flag_signed_char = !value;
373 #if 0 // pch not supported by sdcpp
375 cpp_opts->restore_pch_deps = value;
378 case OPT_fpch_preprocess:
379 flag_pch_preprocess = value;
383 case OPT_fpreprocessed:
384 cpp_opts->preprocessed = value;
387 case OPT_fshow_column:
388 cpp_opts->show_column = value;
392 /* It is documented that we silently ignore silly values. */
393 if (value >= 1 && value <= 100)
394 cpp_opts->tabstop = value;
397 case OPT_fexec_charset_:
398 cpp_opts->narrow_charset = arg;
401 case OPT_fwide_exec_charset_:
402 cpp_opts->wide_charset = arg;
405 case OPT_finput_charset_:
406 cpp_opts->input_charset = arg;
410 add_path (xstrdup (arg), AFTER, 0, true);
415 defer_opt (code, arg);
427 add_path (xstrdup (arg), QUOTE, 0, true);
435 add_path (xstrdup (arg), SYSTEM, 0, true);
438 case OPT_iwithprefix:
439 add_prefixed_path (arg, SYSTEM);
442 case OPT_iwithprefixbefore:
443 add_prefixed_path (arg, BRACKET);
447 cpp_set_lang (parse_in, CLK_ASM);
448 cpp_opts->dollars_in_ident = false;
463 error ("output filename specified twice");
468 cpp_opts->obj_ext = arg;
471 /* We need to handle the -pedantic switches here, rather than in
472 sdcpp_common_post_options, so that a subsequent -Wno-endif-labels
473 is not overridden. */
474 case OPT_pedantic_errors:
475 cpp_opts->pedantic_errors = 1;
478 cpp_opts->pedantic = 1;
479 cpp_opts->warn_endif_labels = 1;
483 case OPT_pedantic_parse_number:
484 cpp_opts->pedantic_parse_number = 1;
487 #if 0 // pch not supported by sdcpp
488 case OPT_print_pch_checksum:
489 c_common_print_pch_checksum (stdout);
490 exit_after_options = true;
499 case OPT_std_iso9899_1990:
500 case OPT_std_iso9899_199409:
501 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
505 case OPT_std_iso9899_1999:
506 set_std_c99 (true /* ISO */);
510 cpp_opts->trigraphs = 1;
513 case OPT_traditional_cpp:
514 cpp_opts->traditional = 1;
518 cpp_opts->inhibit_warnings = 1;
529 /* Post-switch processing. */
531 sdcpp_common_post_options (const char **pfilename)
533 struct cpp_callbacks *cb;
535 /* Canonicalize the input and output filenames. */
536 if (in_fnames == NULL)
538 in_fnames = XNEWVEC (const char *, 1);
541 else if (strcmp (in_fnames[0], "-") == 0)
544 if (out_fname == NULL || !strcmp (out_fname, "-"))
547 if (cpp_opts->deps.style == DEPS_NONE)
548 check_deps_environment_vars ();
550 handle_deferred_opts ();
552 sanitize_cpp_opts ();
554 register_include_chains (parse_in, sysroot, iprefix, imultilib,
555 std_inc, 0, verbose);
557 /* Open the output now. We must do so even if flag_no_output is
558 on, because there may be other output than from the actual
559 preprocessing (e.g. from -dM). */
560 if (out_fname[0] == '\0')
563 out_stream = fopen (out_fname, "w");
565 if (out_stream == NULL)
567 fatal_error ("opening output file %s: %m", out_fname);
571 if (num_in_fnames > 1)
572 error ("too many filenames given. Type %s --help for usage",
575 init_pp_output (out_stream);
577 cb = cpp_get_callbacks (parse_in);
578 cb->file_change = cb_file_change;
579 cb->dir_change = cb_dir_change;
580 cpp_post_options (parse_in);
582 /* If an error has occurred in cpplib, note it so we fail
584 errorcount += cpp_errors (parse_in);
586 *pfilename = this_input_filename
587 = cpp_read_main_file (parse_in, in_fnames[0]);
588 /* Don't do any compilation or preprocessing if there is no input file. */
589 if (this_input_filename == NULL)
595 if (flag_working_directory && !flag_no_line_commands)
596 pp_dir_change (parse_in, get_src_pwd ());
601 /* Front end initialization. */
603 sdcpp_common_init (void)
605 /* Default CPP arithmetic to something sensible for the host for the
606 benefit of dumb users like fix-header. */
607 cpp_opts->precision = CHAR_BIT * sizeof (long);
608 cpp_opts->char_precision = CHAR_BIT;
609 cpp_opts->int_precision = CHAR_BIT * sizeof (int);
610 cpp_opts->wchar_precision = CHAR_BIT * sizeof (int);
611 cpp_opts->unsigned_wchar = 1;
612 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
614 /* This can't happen until after wchar_precision and bytes_big_endian
616 cpp_init_iconv (parse_in);
620 c_common_print_pch_checksum (stderr);
624 preprocess_file (parse_in);
628 /* Common finish hook for the C, ObjC and C++ front ends. */
630 sdcpp_common_finish (void)
632 FILE *deps_stream = NULL;
634 if (cpp_opts->deps.style != DEPS_NONE)
636 /* If -M or -MM was seen without -MF, default output to the
639 deps_stream = out_stream;
642 deps_stream = fopen (deps_file, deps_append ? "a": "w");
644 fatal_error ("opening dependency file %s: %m", deps_file);
648 /* For performance, avoid tearing down cpplib's internal structures
649 with cpp_destroy (). */
650 errorcount += cpp_finish (parse_in, deps_stream);
652 if (deps_stream && deps_stream != out_stream
653 && (ferror (deps_stream) || fclose (deps_stream)))
654 fatal_error ("closing dependency file %s: %m", deps_file);
656 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
657 fatal_error ("when writing output to %s: %m", out_fname);
660 /* Either of two environment variables can specify output of
661 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
662 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
663 and DEPS_TARGET is the target to mention in the deps. They also
664 result in dependency information being appended to the output file
665 rather than overwriting it, and like Sun's compiler
666 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
668 check_deps_environment_vars (void)
672 GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
674 cpp_opts->deps.style = DEPS_USER;
677 GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
680 cpp_opts->deps.style = DEPS_SYSTEM;
681 cpp_opts->deps.ignore_main_file = true;
687 /* Find the space before the DEPS_TARGET, if there is one. */
688 char *s = strchr (spec, ' ');
691 /* Let the caller perform MAKE quoting. */
692 defer_opt (OPT_MT, s + 1);
696 /* Command line -MF overrides environment variables and default. */
705 /* Handle deferred command line switches. */
707 handle_deferred_opts (void)
712 /* Avoid allocating the deps buffer if we don't need it.
713 (This flag may be true without there having been -MT or -MQ
714 options, but we'll still need the deps buffer.) */
718 deps = cpp_get_deps (parse_in);
720 for (i = 0; i < deferred_count; i++)
722 struct deferred_opt *opt = &deferred_opts[i];
724 if (opt->code == OPT_MT || opt->code == OPT_MQ)
725 deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
729 /* These settings are appropriate for GCC, but not necessarily so for
730 cpplib as a library. */
732 sanitize_cpp_opts (void)
734 /* If we don't know what style of dependencies to output, complain
735 if any other dependency switches have been given. */
736 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
737 error ("to generate dependencies you must specify either -M or -MM");
739 /* -dM and dependencies suppress normal output; do it here so that
740 the last -d[MDN] switch overrides earlier ones. */
741 if (flag_dump_macros == 'M')
744 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
745 -dM since at least glibc relies on -M -dM to work. */
746 /* Also, flag_no_output implies flag_no_line_commands, always. */
749 if (flag_dump_macros != 'M')
750 flag_dump_macros = 0;
751 flag_dump_includes = 0;
752 flag_no_line_commands = 1;
755 cpp_opts->unsigned_char = !flag_signed_char;
756 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
758 /* Similarly with -Wno-variadic-macros. No check for c99 here, since
759 this also turns off warnings about GCCs extension. */
760 cpp_opts->warn_variadic_macros
761 = warn_variadic_macros && (pedantic || warn_traditional);
763 /* If we're generating preprocessor output, emit current directory
764 if explicitly requested */
765 if (flag_working_directory == -1)
766 flag_working_directory = 0;
769 /* Add include path with a prefix at the front of its name. */
771 add_prefixed_path (const char *suffix, size_t chain)
775 size_t prefix_len, suffix_len;
777 suffix_len = strlen (suffix);
778 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
779 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
781 path = (char *) xmalloc (prefix_len + suffix_len + 1);
782 memcpy (path, prefix, prefix_len);
783 memcpy (path + prefix_len, suffix, suffix_len);
784 path[prefix_len + suffix_len] = '\0';
786 add_path (path, chain, 0, false);
789 /* Handle -D, -U, -A, -imacros, and the first -include. */
791 finish_options (void)
793 if (!cpp_opts->preprocessed)
797 cb_file_change (parse_in,
798 linemap_add (&line_table, LC_RENAME, 0,
799 _("<built-in>"), 0));
801 cpp_init_builtins (parse_in, 0 /*flag_hosted*/);
803 /* We're about to send user input to cpplib, so make it warn for
804 things that we previously (when we sent it internal definitions)
807 C99 permits implementation-defined characters in identifiers.
808 The documented meaning of -std= is to turn off extensions that
809 conflict with the specified standard, and since a strictly
810 conforming program cannot contain a '$', we do not condition
811 their acceptance on the -std= setting. */
812 cpp_opts->warn_dollars = (cpp_opts->pedantic && !cpp_opts->c99);
814 cpp_change_file (parse_in, LC_RENAME, _("<command line>"));
815 for (i = 0; i < deferred_count; i++)
817 struct deferred_opt *opt = &deferred_opts[i];
819 if (opt->code == OPT_D)
820 cpp_define (parse_in, opt->arg);
821 else if (opt->code == OPT_U)
822 cpp_undef (parse_in, opt->arg);
823 else if (opt->code == OPT_A)
825 if (opt->arg[0] == '-')
826 cpp_unassert (parse_in, opt->arg + 1);
828 cpp_assert (parse_in, opt->arg);
832 /* Handle -imacros after -D and -U. */
833 for (i = 0; i < deferred_count; i++)
835 struct deferred_opt *opt = &deferred_opts[i];
837 if (opt->code == OPT_imacros
838 && cpp_push_include (parse_in, opt->arg))
840 /* Disable push_command_line_include callback for now. */
841 include_cursor = deferred_count + 1;
842 cpp_scan_nooutput (parse_in);
848 push_command_line_include ();
851 /* Give CPP the next file given by -include, if any. */
853 push_command_line_include (void)
855 while (include_cursor < deferred_count)
857 struct deferred_opt *opt = &deferred_opts[include_cursor++];
859 if (!cpp_opts->preprocessed && opt->code == OPT_include
860 && cpp_push_include (parse_in, opt->arg))
864 if (include_cursor == deferred_count)
867 /* -Wunused-macros should only warn about macros defined hereafter. */
868 cpp_opts->warn_unused_macros = warn_unused_macros;
869 /* Restore the line map from <command line>. */
870 if (!cpp_opts->preprocessed)
871 cpp_change_file (parse_in, LC_RENAME, this_input_filename);
873 /* Set this here so the client can change the option if it wishes,
874 and after stacking the main file so we don't trace the main file. */
875 line_table.trace_includes = cpp_opts->print_include_names;
879 /* File change callback. Has to handle -include files. */
881 cb_file_change (cpp_reader * ARG_UNUSED (pfile),
882 const struct line_map *new_map)
884 pp_file_change (new_map);
886 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
887 push_command_line_include ();
891 cb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir)
893 if (!set_src_pwd (dir))
894 warning (0, "too late for # directive to set debug directory");
897 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
898 extensions if ISO). There is no concept of gnu94. */
900 set_std_c89 (int c94, int iso)
902 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
905 /* Set the C 99 standard (without GNU extensions if ISO). */
907 set_std_c99 (int iso)
909 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
912 /* Args to -d specify what to dump. Silently ignore
913 unrecognized options; they may be aimed at toplev.c. */
915 handle_OPT_d (const char *arg)
919 while ((c = *arg++) != '\0')
922 case 'M': /* Dump macros only. */
923 case 'N': /* Dump names. */
924 case 'D': /* Dump definitions. */
925 flag_dump_macros = c;
929 flag_dump_includes = 1;