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
24 #include "diagnostic.h"
26 #include "cppdefault.h"
27 #include "c-incpath.h"
32 #ifndef DOLLARS_IN_IDENTIFIERS
33 # define DOLLARS_IN_IDENTIFIERS true
36 #ifndef TARGET_SYSTEM_ROOT
37 # define TARGET_SYSTEM_ROOT NULL
40 static int saved_lineno;
43 static cpp_options *cpp_opts;
46 static const char *this_input_filename;
48 /* Filename and stream for preprocessed output. */
49 static const char *out_fname;
50 static FILE *out_stream;
52 /* Append dependencies to deps_file. */
53 static bool deps_append;
55 /* If dependency switches (-MF etc.) have been given. */
56 static bool deps_seen;
61 /* Dependency output file. */
62 static const char *deps_file;
64 /* The prefix given by -iprefix, if any. */
65 static const char *iprefix;
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 const struct cl_option *option = &cl_options[scode];
197 enum opt_code code = (enum opt_code) scode;
206 #if 0 // pch not supported by sdcpp
207 case OPT__output_pch_:
213 defer_opt (code, arg);
217 cpp_opts->discard_comments = 0;
221 cpp_opts->discard_comments = 0;
222 cpp_opts->discard_comments_in_macro_exp = 0;
226 defer_opt (code, arg);
230 cpp_opts->print_include_names = 1;
234 if (strcmp (arg, "-"))
235 add_path (xstrdup (arg), BRACKET, 0, true);
238 if (quote_chain_split)
239 error ("-I- specified twice");
240 quote_chain_split = true;
241 split_quote_chain ();
242 inform ("obsolete option -I- used, please use -iquote instead");
248 /* When doing dependencies with -M or -MM, suppress normal
249 preprocessed output, but still do -dM etc. as software
250 depends on this. Preprocessed output does occur if -MD, -MMD
251 or environment var dependency generation is used. */
252 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
254 cpp_opts->inhibit_warnings = 1;
259 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
270 cpp_opts->deps.missing_files = true;
275 cpp_opts->deps.phony_targets = true;
281 defer_opt (code, arg);
285 flag_no_line_commands = 1;
288 case OPT_fworking_directory:
289 flag_working_directory = value;
293 defer_opt (code, arg);
297 cpp_opts->warn_trigraphs = value;
298 cpp_opts->warn_comments = value;
299 cpp_opts->warn_num_sign_change = value;
300 cpp_opts->warn_multichar = value; /* Was C++ only. */
305 cpp_opts->warn_comments = value;
308 case OPT_Wdeprecated:
309 cpp_opts->warn_deprecated = value;
312 case OPT_Wendif_labels:
313 cpp_opts->warn_endif_labels = value;
317 cpp_opts->warnings_are_errors = value;
318 global_dc->warning_as_error_requested = value;
322 /* Silently ignore for now. */
325 #if 0 // pch not supported by sdcpp
326 case OPT_Winvalid_pch:
327 cpp_opts->warn_invalid_pch = value;
331 case OPT_Wsystem_headers:
332 cpp_opts->warn_system_headers = value;
335 case OPT_Wtraditional:
336 cpp_opts->warn_traditional = value;
340 cpp_opts->warn_trigraphs = value;
344 cpp_opts->warn_undef = value;
347 case OPT_Wunused_macros:
348 warn_unused_macros = value;
351 case OPT_Wvariadic_macros:
352 warn_variadic_macros = value;
356 set_std_c89 (false, true);
363 case OPT_fdollars_in_identifiers:
364 cpp_opts->dollars_in_ident = value;
367 case OPT_fsigned_char:
368 flag_signed_char = value;
371 case OPT_funsigned_char:
372 flag_signed_char = !value;
375 #if 0 // pch not supported by sdcpp
377 cpp_opts->restore_pch_deps = value;
380 case OPT_fpch_preprocess:
381 flag_pch_preprocess = value;
385 case OPT_fpreprocessed:
386 cpp_opts->preprocessed = value;
389 case OPT_fshow_column:
390 cpp_opts->show_column = value;
394 /* It is documented that we silently ignore silly values. */
395 if (value >= 1 && value <= 100)
396 cpp_opts->tabstop = value;
399 case OPT_fexec_charset_:
400 cpp_opts->narrow_charset = arg;
403 case OPT_fwide_exec_charset_:
404 cpp_opts->wide_charset = arg;
407 case OPT_finput_charset_:
408 cpp_opts->input_charset = arg;
412 add_path (xstrdup (arg), AFTER, 0, true);
417 defer_opt (code, arg);
425 add_path (xstrdup (arg), QUOTE, 0, true);
433 add_path (xstrdup (arg), SYSTEM, 0, true);
436 case OPT_iwithprefix:
437 add_prefixed_path (arg, SYSTEM);
440 case OPT_iwithprefixbefore:
441 add_prefixed_path (arg, BRACKET);
445 cpp_set_lang (parse_in, CLK_ASM);
446 cpp_opts->dollars_in_ident = false;
461 error ("output filename specified twice");
466 cpp_opts->obj_ext = arg;
469 /* We need to handle the -pedantic switches here, rather than in
470 sdcpp_common_post_options, so that a subsequent -Wno-endif-labels
471 is not overridden. */
472 case OPT_pedantic_errors:
473 cpp_opts->pedantic_errors = 1;
476 cpp_opts->pedantic = 1;
477 cpp_opts->warn_endif_labels = 1;
481 case OPT_pedantic_parse_number:
482 cpp_opts->pedantic_parse_number = 1;
485 #if 0 // pch not supported by sdcpp
486 case OPT_print_pch_checksum:
487 c_common_print_pch_checksum (stdout);
488 exit_after_options = true;
497 case OPT_std_iso9899_1990:
498 case OPT_std_iso9899_199409:
499 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
503 case OPT_std_iso9899_1999:
504 set_std_c99 (true /* ISO */);
508 cpp_opts->trigraphs = 1;
511 case OPT_traditional_cpp:
512 cpp_opts->traditional = 1;
516 cpp_opts->inhibit_warnings = 1;
527 /* Post-switch processing. */
529 sdcpp_common_post_options (const char **pfilename)
531 struct cpp_callbacks *cb;
533 /* Canonicalize the input and output filenames. */
534 if (in_fnames == NULL)
536 in_fnames = XNEWVEC (const char *, 1);
539 else if (strcmp (in_fnames[0], "-") == 0)
542 if (out_fname == NULL || !strcmp (out_fname, "-"))
545 if (cpp_opts->deps.style == DEPS_NONE)
546 check_deps_environment_vars ();
548 handle_deferred_opts ();
550 sanitize_cpp_opts ();
552 register_include_chains (parse_in, sysroot, iprefix,
553 std_inc, 0, verbose);
555 /* Open the output now. We must do so even if flag_no_output is
556 on, because there may be other output than from the actual
557 preprocessing (e.g. from -dM). */
558 if (out_fname[0] == '\0')
561 out_stream = fopen (out_fname, "w");
563 if (out_stream == NULL)
565 fatal_error ("opening output file %s: %m", out_fname);
569 if (num_in_fnames > 1)
570 error ("too many filenames given. Type %s --help for usage",
573 init_pp_output (out_stream);
575 cb = cpp_get_callbacks (parse_in);
576 cb->file_change = cb_file_change;
577 cb->dir_change = cb_dir_change;
578 cpp_post_options (parse_in);
580 input_location = UNKNOWN_LOCATION;
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 common to C, ObjC and C++. */
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;