1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA
24 #include "diagnostic.h"
26 #include "cppdefault.h"
27 #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
39 static int saved_lineno;
42 static cpp_options *cpp_opts;
45 static const char *this_input_filename;
47 /* Filename and stream for preprocessed output. */
48 static const char *out_fname;
49 static FILE *out_stream;
51 /* Append dependencies to deps_file. */
52 static bool deps_append;
54 /* If dependency switches (-MF etc.) have been given. */
55 static bool deps_seen;
60 /* Dependency output file. */
61 static const char *deps_file;
63 /* The prefix given by -iprefix, if any. */
64 static const char *iprefix;
66 /* The system root, if any. Overridden by -isysroot. */
67 static const char *sysroot = TARGET_SYSTEM_ROOT;
69 /* Zero disables all standard directories for headers. */
70 static bool std_inc = true;
72 /* If the quote chain has been split by -I-. */
73 static bool quote_chain_split;
75 /* If -Wunused-macros. */
76 static bool warn_unused_macros;
78 /* Number of deferred options. */
79 static size_t deferred_count;
81 /* Number of deferred options scanned for -include. */
82 static size_t include_cursor;
84 static void handle_OPT_d (const char *);
85 static void set_std_c89 (int, int);
86 static void set_std_c99 (int);
87 static void check_deps_environment_vars (void);
88 static void handle_deferred_opts (void);
89 static void sanitize_cpp_opts (void);
90 static void add_prefixed_path (const char *, size_t);
91 static void push_command_line_include (void);
92 static void cb_file_change (cpp_reader *, const struct line_map *);
93 static void cb_dir_change (cpp_reader *, const char *);
94 static void finish_options (void);
96 #ifndef STDC_0_IN_SYSTEM_HEADERS
97 #define STDC_0_IN_SYSTEM_HEADERS 0
100 /* Holds switches parsed by sdcpp_common_handle_option (), but whose
101 handling is deferred to sdcpp_common_post_options (). */
102 static void defer_opt (enum opt_code, const char *);
103 static struct deferred_opt
109 /* Complain that switch CODE expects an argument but none was
110 provided. OPT was the command-line option. Return FALSE to get
111 the default message in opts.c, TRUE if we provide a specialized
114 sdcpp_common_missing_argument (const char *opt, size_t code)
119 /* Pick up the default message. */
123 error ("assertion missing after \"%s\"", opt);
128 error ("macro name missing after \"%s\"", opt);
135 error ("missing path after \"%s\"", opt);
144 error ("missing filename after \"%s\"", opt);
149 error ("missing makefile target after \"%s\"", opt);
156 /* Defer option CODE with argument ARG. */
158 defer_opt (enum opt_code code, const char *arg)
160 deferred_opts[deferred_count].code = code;
161 deferred_opts[deferred_count].arg = arg;
165 /* Common initialization before parsing options. */
167 sdcpp_common_init_options (unsigned int argc, const char **argv ATTRIBUTE_UNUSED)
169 parse_in = cpp_create_reader (CLK_GNUC89, NULL);
171 cpp_opts = cpp_get_options (parse_in);
172 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
175 /* Reset to avoid warnings on internal definitions. We set it just
176 before passing on command-line options to cpplib. */
177 cpp_opts->warn_dollars = 0;
179 deferred_opts = xmalloc (argc * sizeof (struct deferred_opt));
184 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
185 form of an -f or -W option was given. Returns 0 if the switch was
186 invalid, a negative number to prevent language-independent
187 processing in toplev.c (a hack necessary for the short-term). */
189 sdcpp_common_handle_option (size_t scode, const char *arg, int value)
191 const struct cl_option *option = &cl_options[scode];
192 enum opt_code code = (enum opt_code) scode;
201 #if 0 // pch not supported by sdcpp
202 case OPT__output_pch_:
208 defer_opt (code, arg);
212 cpp_opts->discard_comments = 0;
216 cpp_opts->discard_comments = 0;
217 cpp_opts->discard_comments_in_macro_exp = 0;
221 defer_opt (code, arg);
225 cpp_opts->print_include_names = 1;
229 if (strcmp (arg, "-"))
230 add_path (xstrdup (arg), BRACKET, 0);
233 if (quote_chain_split)
234 error ("-I- specified twice");
235 quote_chain_split = true;
236 split_quote_chain ();
242 /* When doing dependencies with -M or -MM, suppress normal
243 preprocessed output, but still do -dM etc. as software
244 depends on this. Preprocessed output does occur if -MD, -MMD
245 or environment var dependency generation is used. */
246 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
248 cpp_opts->inhibit_warnings = 1;
253 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
264 cpp_opts->deps.missing_files = true;
269 cpp_opts->deps.phony_targets = true;
275 defer_opt (code, arg);
279 flag_no_line_commands = 1;
282 case OPT_fworking_directory:
283 flag_working_directory = value;
287 defer_opt (code, arg);
291 cpp_opts->warn_trigraphs = value;
292 cpp_opts->warn_comments = value;
293 cpp_opts->warn_num_sign_change = value;
294 cpp_opts->warn_multichar = value; /* Was C++ only. */
299 cpp_opts->warn_comments = value;
302 case OPT_Wdeprecated:
303 cpp_opts->warn_deprecated = value;
306 case OPT_Wendif_labels:
307 cpp_opts->warn_endif_labels = value;
311 cpp_opts->warnings_are_errors = value;
315 /* Silently ignore for now. */
318 #if 0 // pch not supported by sdcpp
319 case OPT_Winvalid_pch:
320 cpp_opts->warn_invalid_pch = value;
324 case OPT_Wsystem_headers:
325 cpp_opts->warn_system_headers = value;
329 cpp_opts->warn_trigraphs = value;
333 cpp_opts->warn_undef = value;
336 case OPT_Wunused_macros:
337 warn_unused_macros = value;
341 set_std_c89 (false, true);
348 case OPT_fdollars_in_identifiers:
349 cpp_opts->dollars_in_ident = value;
352 case OPT_fsigned_char:
353 flag_signed_char = value;
356 case OPT_funsigned_char:
357 flag_signed_char = !value;
360 #if 0 // pch not supported by sdcpp
362 cpp_opts->restore_pch_deps = value;
366 case OPT_fpreprocessed:
367 cpp_opts->preprocessed = value;
370 case OPT_fshow_column:
371 cpp_opts->show_column = value;
375 /* It is documented that we silently ignore silly values. */
376 if (value >= 1 && value <= 100)
377 cpp_opts->tabstop = value;
380 case OPT_fexec_charset_:
381 cpp_opts->narrow_charset = arg;
384 case OPT_fwide_exec_charset_:
385 cpp_opts->wide_charset = arg;
388 case OPT_finput_charset_:
389 cpp_opts->input_charset = arg;
393 add_path (xstrdup (arg), AFTER, 0);
398 defer_opt (code, arg);
410 add_path (xstrdup (arg), SYSTEM, 0);
413 case OPT_iwithprefix:
414 add_prefixed_path (arg, SYSTEM);
417 case OPT_iwithprefixbefore:
418 add_prefixed_path (arg, BRACKET);
422 cpp_set_lang (parse_in, CLK_ASM);
423 cpp_opts->dollars_in_ident = false;
438 error ("output filename specified twice");
443 cpp_opts->obj_ext = arg;
446 /* We need to handle the -pedantic switches here, rather than in
447 sdcpp_common_post_options, so that a subsequent -Wno-endif-labels
448 is not overridden. */
449 case OPT_pedantic_errors:
450 cpp_opts->pedantic_errors = 1;
453 cpp_opts->pedantic = 1;
454 cpp_opts->warn_endif_labels = 1;
458 case OPT_pedantic_parse_number:
459 cpp_opts->pedantic_parse_number = 1;
467 case OPT_std_iso9899_1990:
468 case OPT_std_iso9899_199409:
469 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
473 case OPT_std_iso9899_1999:
474 set_std_c99 (true /* ISO */);
478 cpp_opts->trigraphs = 1;
481 case OPT_traditional_cpp:
482 cpp_opts->traditional = 1;
486 cpp_opts->inhibit_warnings = 1;
497 /* Post-switch processing. */
499 sdcpp_common_post_options (const char **pfilename)
501 struct cpp_callbacks *cb;
503 /* Canonicalize the input and output filenames. */
504 if (in_fnames == NULL)
506 in_fnames = xmalloc (sizeof (in_fnames[0]));
509 else if (strcmp (in_fnames[0], "-") == 0)
512 if (out_fname == NULL || !strcmp (out_fname, "-"))
515 if (cpp_opts->deps.style == DEPS_NONE)
516 check_deps_environment_vars ();
518 handle_deferred_opts ();
520 sanitize_cpp_opts ();
522 register_include_chains (parse_in, sysroot, iprefix,
523 std_inc, 0, verbose);
525 /* Open the output now. We must do so even if flag_no_output is
526 on, because there may be other output than from the actual
527 preprocessing (e.g. from -dM). */
528 if (out_fname[0] == '\0')
531 out_stream = fopen (out_fname, "w");
533 if (out_stream == NULL)
535 fatal_error ("opening output file %s: %m", out_fname);
539 if (num_in_fnames > 1)
540 error ("too many filenames given. Type %s --help for usage",
543 init_pp_output (out_stream);
545 cb = cpp_get_callbacks (parse_in);
546 cb->file_change = cb_file_change;
547 cb->dir_change = cb_dir_change;
548 cpp_post_options (parse_in);
550 saved_lineno = input_line;
553 /* If an error has occurred in cpplib, note it so we fail
555 errorcount += cpp_errors (parse_in);
557 *pfilename = this_input_filename
558 = cpp_read_main_file (parse_in, in_fnames[0]);
559 /* Don't do any compilation or preprocessing if there is no input file. */
560 if (this_input_filename == NULL)
566 if (flag_working_directory && ! flag_no_line_commands)
567 pp_dir_change (parse_in, get_src_pwd ());
572 /* Front end initialization common to C, ObjC and C++. */
574 sdcpp_common_init (void)
576 input_line = saved_lineno;
578 /* Default CPP arithmetic to something sensible for the host for the
579 benefit of dumb users like fix-header. */
580 cpp_opts->precision = CHAR_BIT * sizeof (long);
581 cpp_opts->char_precision = CHAR_BIT;
582 cpp_opts->int_precision = CHAR_BIT * sizeof (int);
583 cpp_opts->wchar_precision = CHAR_BIT * sizeof (int);
584 cpp_opts->unsigned_wchar = 1;
585 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
587 /* This can't happen until after wchar_precision and bytes_big_endian
589 cpp_init_iconv (parse_in);
592 preprocess_file (parse_in);
596 /* Common finish hook for the C, ObjC and C++ front ends. */
598 sdcpp_common_finish (void)
600 FILE *deps_stream = NULL;
602 if (cpp_opts->deps.style != DEPS_NONE)
604 /* If -M or -MM was seen without -MF, default output to the
607 deps_stream = out_stream;
610 deps_stream = fopen (deps_file, deps_append ? "a": "w");
612 fatal_error ("opening dependency file %s: %m", deps_file);
616 /* For performance, avoid tearing down cpplib's internal structures
617 with cpp_destroy (). */
618 errorcount += cpp_finish (parse_in, deps_stream);
620 if (deps_stream && deps_stream != out_stream
621 && (ferror (deps_stream) || fclose (deps_stream)))
622 fatal_error ("closing dependency file %s: %m", deps_file);
624 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
625 fatal_error ("when writing output to %s: %m", out_fname);
628 /* Either of two environment variables can specify output of
629 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
630 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
631 and DEPS_TARGET is the target to mention in the deps. They also
632 result in dependency information being appended to the output file
633 rather than overwriting it, and like Sun's compiler
634 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
636 check_deps_environment_vars (void)
640 GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
642 cpp_opts->deps.style = DEPS_USER;
645 GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
648 cpp_opts->deps.style = DEPS_SYSTEM;
649 cpp_opts->deps.ignore_main_file = true;
655 /* Find the space before the DEPS_TARGET, if there is one. */
656 char *s = strchr (spec, ' ');
659 /* Let the caller perform MAKE quoting. */
660 defer_opt (OPT_MT, s + 1);
664 /* Command line -MF overrides environment variables and default. */
672 /* Handle deferred command line switches. */
674 handle_deferred_opts (void)
678 for (i = 0; i < deferred_count; i++)
680 struct deferred_opt *opt = &deferred_opts[i];
682 if (opt->code == OPT_MT || opt->code == OPT_MQ)
683 cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
687 /* These settings are appropriate for GCC, but not necessarily so for
688 cpplib as a library. */
690 sanitize_cpp_opts (void)
692 /* If we don't know what style of dependencies to output, complain
693 if any other dependency switches have been given. */
694 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
695 error ("to generate dependencies you must specify either -M or -MM");
697 /* -dM and dependencies suppress normal output; do it here so that
698 the last -d[MDN] switch overrides earlier ones. */
699 if (flag_dump_macros == 'M')
702 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
703 -dM since at least glibc relies on -M -dM to work. */
704 /* Also, flag_no_output implies flag_no_line_commands, always. */
707 if (flag_dump_macros != 'M')
708 flag_dump_macros = 0;
709 flag_dump_includes = 0;
710 flag_no_line_commands = 1;
713 cpp_opts->unsigned_char = !flag_signed_char;
714 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
716 /* If we're generating preprocessor output, emit current directory
717 if explicitly requested */
718 if (flag_working_directory == -1)
719 flag_working_directory = 0;
722 /* Add include path with a prefix at the front of its name. */
724 add_prefixed_path (const char *suffix, size_t chain)
728 size_t prefix_len, suffix_len;
730 suffix_len = strlen (suffix);
731 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
732 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
734 path = xmalloc (prefix_len + suffix_len + 1);
735 memcpy (path, prefix, prefix_len);
736 memcpy (path + prefix_len, suffix, suffix_len);
737 path[prefix_len + suffix_len] = '\0';
739 add_path (path, chain, 0);
742 /* Handle -D, -U, -A, -imacros, and the first -include. */
744 finish_options (void)
746 if (!cpp_opts->preprocessed)
750 cpp_change_file (parse_in, LC_RENAME, _("<built-in>"));
751 cpp_init_builtins (parse_in, 0 /*flag_hosted*/);
753 /* We're about to send user input to cpplib, so make it warn for
754 things that we previously (when we sent it internal definitions)
757 C99 permits implementation-defined characters in identifiers.
758 The documented meaning of -std= is to turn off extensions that
759 conflict with the specified standard, and since a strictly
760 conforming program cannot contain a '$', we do not condition
761 their acceptance on the -std= setting. */
762 cpp_opts->warn_dollars = (cpp_opts->pedantic && !cpp_opts->c99);
764 cpp_change_file (parse_in, LC_RENAME, _("<command line>"));
765 for (i = 0; i < deferred_count; i++)
767 struct deferred_opt *opt = &deferred_opts[i];
769 if (opt->code == OPT_D)
770 cpp_define (parse_in, opt->arg);
771 else if (opt->code == OPT_U)
772 cpp_undef (parse_in, opt->arg);
773 else if (opt->code == OPT_A)
775 if (opt->arg[0] == '-')
776 cpp_unassert (parse_in, opt->arg + 1);
778 cpp_assert (parse_in, opt->arg);
782 /* Handle -imacros after -D and -U. */
783 for (i = 0; i < deferred_count; i++)
785 struct deferred_opt *opt = &deferred_opts[i];
787 if (opt->code == OPT_imacros
788 && cpp_push_include (parse_in, opt->arg))
790 /* Disable push_command_line_include callback for now. */
791 include_cursor = deferred_count + 1;
792 cpp_scan_nooutput (parse_in);
798 push_command_line_include ();
801 /* Give CPP the next file given by -include, if any. */
803 push_command_line_include (void)
805 while (include_cursor < deferred_count)
807 struct deferred_opt *opt = &deferred_opts[include_cursor++];
809 if (! cpp_opts->preprocessed && opt->code == OPT_include
810 && cpp_push_include (parse_in, opt->arg))
814 if (include_cursor == deferred_count)
817 /* -Wunused-macros should only warn about macros defined hereafter. */
818 cpp_opts->warn_unused_macros = warn_unused_macros;
819 /* Restore the line map from <command line>. */
820 if (! cpp_opts->preprocessed)
821 cpp_change_file (parse_in, LC_RENAME, main_input_filename);
823 /* Set this here so the client can change the option if it wishes,
824 and after stacking the main file so we don't trace the main file. */
825 cpp_get_line_maps (parse_in)->trace_includes
826 = cpp_opts->print_include_names;
830 /* File change callback. Has to handle -include files. */
832 cb_file_change (cpp_reader *pfile ATTRIBUTE_UNUSED,
833 const struct line_map *new_map)
835 pp_file_change (new_map);
837 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
838 push_command_line_include ();
842 cb_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
844 if (! set_src_pwd (dir))
845 warning ("too late for # directive to set debug directory");
848 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
849 extensions if ISO). There is no concept of gnu94. */
851 set_std_c89 (int c94, int iso)
853 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
856 /* Set the C 99 standard (without GNU extensions if ISO). */
858 set_std_c99 (int iso)
860 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
863 /* Args to -d specify what to dump. Silently ignore
864 unrecognized options; they may be aimed at toplev.c. */
866 handle_OPT_d (const char *arg)
870 while ((c = *arg++) != '\0')
873 case 'M': /* Dump macros only. */
874 case 'N': /* Dump names. */
875 case 'D': /* Dump definitions. */
876 flag_dump_macros = c;
880 flag_dump_includes = 1;