c-opts.c revision 146908
1/* C/ObjC/C++ command line option handling.
2   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3   Contributed by Neil Booth.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING.  If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA.  */
21
22/* $FreeBSD: head/contrib/gcc/c-opts.c 146908 2005-06-03 04:02:20Z kan $ */
23
24#include "config.h"
25#include "system.h"
26#include "coretypes.h"
27#include "tm.h"
28#include "tree.h"
29#include "c-common.h"
30#include "c-pragma.h"
31#include "flags.h"
32#include "toplev.h"
33#include "langhooks.h"
34#include "tree-inline.h"
35#include "diagnostic.h"
36#include "intl.h"
37#include "cppdefault.h"
38#include "c-incpath.h"
39#include "debug.h"		/* For debug_hooks.  */
40#include "opts.h"
41#include "options.h"
42
43#ifndef DOLLARS_IN_IDENTIFIERS
44# define DOLLARS_IN_IDENTIFIERS true
45#endif
46
47#ifndef TARGET_SYSTEM_ROOT
48# define TARGET_SYSTEM_ROOT NULL
49#endif
50
51static int saved_lineno;
52
53/* CPP's options.  */
54static cpp_options *cpp_opts;
55
56/* Input filename.  */
57static const char *this_input_filename;
58
59/* Filename and stream for preprocessed output.  */
60static const char *out_fname;
61static FILE *out_stream;
62
63/* Append dependencies to deps_file.  */
64static bool deps_append;
65
66/* If dependency switches (-MF etc.) have been given.  */
67static bool deps_seen;
68
69/* If -v seen.  */
70static bool verbose;
71
72/* Dependency output file.  */
73static const char *deps_file;
74
75/* The prefix given by -iprefix, if any.  */
76static const char *iprefix;
77
78/* The system root, if any.  Overridden by -isysroot.  */
79static const char *sysroot = TARGET_SYSTEM_ROOT;
80
81/* Zero disables all standard directories for headers.  */
82static bool std_inc = true;
83
84/* Zero disables the C++-specific standard directories for headers.  */
85static bool std_cxx_inc = true;
86
87/* If the quote chain has been split by -I-.  */
88static bool quote_chain_split;
89
90/* If -Wunused-macros.  */
91static bool warn_unused_macros;
92
93/* Number of deferred options.  */
94static size_t deferred_count;
95
96/* Number of deferred options scanned for -include.  */
97static size_t include_cursor;
98
99/* Permit Fotran front-end options.  */
100static bool permit_fortran_options;
101
102static void set_Wimplicit (int);
103static void handle_OPT_d (const char *);
104static void set_std_cxx98 (int);
105static void set_std_c89 (int, int);
106static void set_std_c99 (int);
107static void check_deps_environment_vars (void);
108static void handle_deferred_opts (void);
109static void sanitize_cpp_opts (void);
110static void add_prefixed_path (const char *, size_t);
111static void push_command_line_include (void);
112static void cb_file_change (cpp_reader *, const struct line_map *);
113static void cb_dir_change (cpp_reader *, const char *);
114static void finish_options (void);
115
116#ifndef STDC_0_IN_SYSTEM_HEADERS
117#define STDC_0_IN_SYSTEM_HEADERS 0
118#endif
119
120/* Holds switches parsed by c_common_handle_option (), but whose
121   handling is deferred to c_common_post_options ().  */
122static void defer_opt (enum opt_code, const char *);
123static struct deferred_opt
124{
125  enum opt_code code;
126  const char *arg;
127} *deferred_opts;
128
129/* Complain that switch CODE expects an argument but none was
130   provided.  OPT was the command-line option.  Return FALSE to get
131   the default message in opts.c, TRUE if we provide a specialized
132   one.  */
133bool
134c_common_missing_argument (const char *opt, size_t code)
135{
136  switch (code)
137    {
138    default:
139      /* Pick up the default message.  */
140      return false;
141
142    case OPT_fconstant_string_class_:
143      error ("no class name specified with \"%s\"", opt);
144      break;
145
146    case OPT_A:
147      error ("assertion missing after \"%s\"", opt);
148      break;
149
150    case OPT_D:
151    case OPT_U:
152      error ("macro name missing after \"%s\"", opt);
153      break;
154
155    case OPT_I:
156    case OPT_idirafter:
157    case OPT_isysroot:
158    case OPT_isystem:
159      error ("missing path after \"%s\"", opt);
160      break;
161
162    case OPT_MF:
163    case OPT_MD:
164    case OPT_MMD:
165    case OPT_include:
166    case OPT_imacros:
167    case OPT_o:
168      error ("missing filename after \"%s\"", opt);
169      break;
170
171    case OPT_MQ:
172    case OPT_MT:
173      error ("missing makefile target after \"%s\"", opt);
174      break;
175    }
176
177  return true;
178}
179
180/* Defer option CODE with argument ARG.  */
181static void
182defer_opt (enum opt_code code, const char *arg)
183{
184  deferred_opts[deferred_count].code = code;
185  deferred_opts[deferred_count].arg = arg;
186  deferred_count++;
187}
188
189/* Common initialization before parsing options.  */
190unsigned int
191c_common_init_options (unsigned int argc, const char **argv)
192{
193  static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
194  unsigned int i, result;
195
196  /* This is conditionalized only because that is the way the front
197     ends used to do it.  Maybe this should be unconditional?  */
198  if (c_dialect_cxx ())
199    {
200      /* By default wrap lines at 80 characters.  Is getenv
201	 ("COLUMNS") preferable?  */
202      diagnostic_line_cutoff (global_dc) = 80;
203      /* By default, emit location information once for every
204	 diagnostic message.  */
205      diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
206    }
207
208  parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
209				ident_hash);
210
211  cpp_opts = cpp_get_options (parse_in);
212  cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
213  cpp_opts->objc = c_dialect_objc ();
214
215  /* Reset to avoid warnings on internal definitions.  We set it just
216     before passing on command-line options to cpplib.  */
217  cpp_opts->warn_dollars = 0;
218
219  flag_const_strings = c_dialect_cxx ();
220  flag_exceptions = c_dialect_cxx ();
221  warn_pointer_arith = c_dialect_cxx ();
222
223  deferred_opts = xmalloc (argc * sizeof (struct deferred_opt));
224
225  result = lang_flags[c_language];
226
227  if (c_language == clk_c)
228    {
229      for (i = 1; i < argc; i++)
230	{
231	  /* If preprocessing assembly language, accept any of the C-family
232	     front end options since the driver may pass them through.  */
233	  if (! strcmp (argv[i], "-lang-asm"))
234	    result |= CL_C | CL_ObjC | CL_CXX | CL_ObjCXX;
235#ifdef CL_F77
236	  /* If potentially preprocessing Fortran we have to accept its
237	     front end options since the driver may them through.  */
238	  else if (! strcmp (argv[i], "-traditional-cpp"))
239	    {
240	      permit_fortran_options = true;
241	      result |= CL_F77;
242	    }
243#endif
244	}
245    }
246  return result;
247}
248
249/* Handle switch SCODE with argument ARG.  VALUE is true, unless no-
250   form of an -f or -W option was given.  Returns 0 if the switch was
251   invalid, a negative number to prevent language-independent
252   processing in toplev.c (a hack necessary for the short-term).  */
253int
254c_common_handle_option (size_t scode, const char *arg, int value)
255{
256  const struct cl_option *option = &cl_options[scode];
257  enum opt_code code = (enum opt_code) scode;
258  int result = 1;
259
260  switch (code)
261    {
262    default:
263      result = permit_fortran_options;
264      break;
265
266    case OPT__output_pch_:
267      pch_file = arg;
268      break;
269
270    case OPT_A:
271      defer_opt (code, arg);
272      break;
273
274    case OPT_C:
275      cpp_opts->discard_comments = 0;
276      break;
277
278    case OPT_CC:
279      cpp_opts->discard_comments = 0;
280      cpp_opts->discard_comments_in_macro_exp = 0;
281      break;
282
283    case OPT_D:
284      defer_opt (code, arg);
285      break;
286
287    case OPT_E:
288      flag_preprocess_only = 1;
289      break;
290
291    case OPT_H:
292      cpp_opts->print_include_names = 1;
293      break;
294
295    case OPT_I:
296      if (strcmp (arg, "-"))
297	add_path (xstrdup (arg), BRACKET, 0);
298      else
299	{
300	  if (quote_chain_split)
301	    error ("-I- specified twice");
302	  quote_chain_split = true;
303	  split_quote_chain ();
304	}
305      break;
306
307    case OPT_M:
308    case OPT_MM:
309      /* When doing dependencies with -M or -MM, suppress normal
310	 preprocessed output, but still do -dM etc. as software
311	 depends on this.  Preprocessed output does occur if -MD, -MMD
312	 or environment var dependency generation is used.  */
313      cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
314      flag_no_output = 1;
315      cpp_opts->inhibit_warnings = 1;
316      break;
317
318    case OPT_MD:
319    case OPT_MMD:
320      cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
321      deps_file = arg;
322      break;
323
324    case OPT_MF:
325      deps_seen = true;
326      deps_file = arg;
327      break;
328
329    case OPT_MG:
330      deps_seen = true;
331      cpp_opts->deps.missing_files = true;
332      break;
333
334    case OPT_MP:
335      deps_seen = true;
336      cpp_opts->deps.phony_targets = true;
337      break;
338
339    case OPT_MQ:
340    case OPT_MT:
341      deps_seen = true;
342      defer_opt (code, arg);
343      break;
344
345    case OPT_P:
346      flag_no_line_commands = 1;
347      break;
348
349    case OPT_fworking_directory:
350      flag_working_directory = value;
351      break;
352
353    case OPT_U:
354      defer_opt (code, arg);
355      break;
356
357    case OPT_Wabi:
358      warn_abi = value;
359      break;
360
361    case OPT_Wall:
362      set_Wunused (value);
363      set_Wformat (value);
364      set_Wimplicit (value);
365      warn_char_subscripts = value;
366      warn_missing_braces = value;
367      warn_parentheses = value;
368      warn_return_type = value;
369      warn_sequence_point = value;	/* Was C only.  */
370      if (c_dialect_cxx ())
371	warn_sign_compare = value;
372      warn_switch = value;
373      warn_strict_aliasing = value;
374
375      /* Only warn about unknown pragmas that are not in system
376	 headers.  */
377      warn_unknown_pragmas = value;
378
379      /* We save the value of warn_uninitialized, since if they put
380	 -Wuninitialized on the command line, we need to generate a
381	 warning about not using it without also specifying -O.  */
382      if (warn_uninitialized != 1)
383	warn_uninitialized = (value ? 2 : 0);
384
385      if (!c_dialect_cxx ())
386	/* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
387	   can turn it off only if it's not explicit.  */
388	warn_main = value * 2;
389      else
390	{
391	  /* C++-specific warnings.  */
392	  warn_nonvdtor = value;
393	  warn_reorder = value;
394	  warn_nontemplate_friend = value;
395	}
396
397      cpp_opts->warn_trigraphs = value;
398      cpp_opts->warn_comments = value;
399      cpp_opts->warn_num_sign_change = value;
400      cpp_opts->warn_multichar = value;	/* Was C++ only.  */
401      break;
402
403    case OPT_Wbad_function_cast:
404      warn_bad_function_cast = value;
405      break;
406
407    case OPT_Wcast_qual:
408      warn_cast_qual = value;
409      break;
410
411    case OPT_Wchar_subscripts:
412      warn_char_subscripts = value;
413      break;
414
415    case OPT_Wcomment:
416    case OPT_Wcomments:
417      cpp_opts->warn_comments = value;
418      break;
419
420    case OPT_Wconversion:
421      warn_conversion = value;
422      break;
423
424    case OPT_Wctor_dtor_privacy:
425      warn_ctor_dtor_privacy = value;
426      break;
427
428    case OPT_Wdeclaration_after_statement:
429      warn_declaration_after_statement = value;
430      break;
431
432    case OPT_Wdeprecated:
433      warn_deprecated = value;
434      cpp_opts->warn_deprecated = value;
435      break;
436
437    case OPT_Wdiv_by_zero:
438      warn_div_by_zero = value;
439      break;
440
441    case OPT_Weffc__:
442      warn_ecpp = value;
443      break;
444
445    case OPT_Wendif_labels:
446      cpp_opts->warn_endif_labels = value;
447      break;
448
449    case OPT_Werror:
450      cpp_opts->warnings_are_errors = value;
451      break;
452
453    case OPT_Werror_implicit_function_declaration:
454      mesg_implicit_function_declaration = 2;
455      break;
456
457    case OPT_Wfloat_equal:
458      warn_float_equal = value;
459      break;
460
461    case OPT_Wformat:
462      set_Wformat (value);
463      break;
464
465    case OPT_Wformat_:
466      set_Wformat (atoi (arg));
467      break;
468
469    case OPT_Wformat_extra_args:
470      warn_format_extra_args = value;
471      break;
472
473    case OPT_Wformat_nonliteral:
474      warn_format_nonliteral = value;
475      break;
476
477    case OPT_Wformat_security:
478      warn_format_security = value;
479      break;
480
481    case OPT_Wformat_y2k:
482      warn_format_y2k = value;
483      break;
484
485    case OPT_Wformat_zero_length:
486      warn_format_zero_length = value;
487      break;
488
489    case OPT_Winit_self:
490      warn_init_self = value;
491      break;
492
493    case OPT_Wimplicit:
494      set_Wimplicit (value);
495      break;
496
497    case OPT_Wimplicit_function_declaration:
498      mesg_implicit_function_declaration = value;
499      break;
500
501    case OPT_Wimplicit_int:
502      warn_implicit_int = value;
503      break;
504
505    case OPT_Wimport:
506      /* Silently ignore for now.  */
507      break;
508
509    case OPT_Winvalid_offsetof:
510      warn_invalid_offsetof = value;
511      break;
512
513    case OPT_Winvalid_pch:
514      cpp_opts->warn_invalid_pch = value;
515      break;
516
517    case OPT_Wlong_long:
518      warn_long_long = value;
519      break;
520
521    case OPT_Wmain:
522      if (value)
523	warn_main = 1;
524      else
525	warn_main = -1;
526      break;
527
528    case OPT_Wmissing_braces:
529      warn_missing_braces = value;
530      break;
531
532    case OPT_Wmissing_declarations:
533      warn_missing_declarations = value;
534      break;
535
536    case OPT_Wmissing_format_attribute:
537      warn_missing_format_attribute = value;
538      break;
539
540    case OPT_Wmissing_prototypes:
541      warn_missing_prototypes = value;
542      break;
543
544    case OPT_Wmultichar:
545      cpp_opts->warn_multichar = value;
546      break;
547
548    case OPT_Wnested_externs:
549      warn_nested_externs = value;
550      break;
551
552    case OPT_Wnon_template_friend:
553      warn_nontemplate_friend = value;
554      break;
555
556    case OPT_Wnon_virtual_dtor:
557      warn_nonvdtor = value;
558      break;
559
560    case OPT_Wnonnull:
561      warn_nonnull = value;
562      break;
563
564    case OPT_Wold_style_definition:
565      warn_old_style_definition = value;
566      break;
567
568    case OPT_Wold_style_cast:
569      warn_old_style_cast = value;
570      break;
571
572    case OPT_Woverloaded_virtual:
573      warn_overloaded_virtual = value;
574      break;
575
576    case OPT_Wparentheses:
577      warn_parentheses = value;
578      break;
579
580    case OPT_Wpmf_conversions:
581      warn_pmf2ptr = value;
582      break;
583
584    case OPT_Wpointer_arith:
585      warn_pointer_arith = value;
586      break;
587
588    case OPT_Wprotocol:
589      warn_protocol = value;
590      break;
591
592    case OPT_Wselector:
593      warn_selector = value;
594      break;
595
596    case OPT_Wredundant_decls:
597      warn_redundant_decls = value;
598      break;
599
600    case OPT_Wreorder:
601      warn_reorder = value;
602      break;
603
604    case OPT_Wreturn_type:
605      warn_return_type = value;
606      break;
607
608    case OPT_Wsequence_point:
609      warn_sequence_point = value;
610      break;
611
612    case OPT_Wsign_compare:
613      warn_sign_compare = value;
614      break;
615
616    case OPT_Wsign_promo:
617      warn_sign_promo = value;
618      break;
619
620    case OPT_Wstrict_prototypes:
621      warn_strict_prototypes = value;
622      break;
623
624    case OPT_Wsynth:
625      warn_synth = value;
626      break;
627
628    case OPT_Wsystem_headers:
629      cpp_opts->warn_system_headers = value;
630      break;
631
632    case OPT_Wtraditional:
633      warn_traditional = value;
634      cpp_opts->warn_traditional = value;
635      break;
636
637    case OPT_Wtrigraphs:
638      cpp_opts->warn_trigraphs = value;
639      break;
640
641    case OPT_Wundeclared_selector:
642      warn_undeclared_selector = value;
643      break;
644
645    case OPT_Wundef:
646      cpp_opts->warn_undef = value;
647      break;
648
649    case OPT_Wunknown_pragmas:
650      /* Set to greater than 1, so that even unknown pragmas in
651	 system headers will be warned about.  */
652      warn_unknown_pragmas = value * 2;
653      break;
654
655    case OPT_Wunused_macros:
656      warn_unused_macros = value;
657      break;
658
659    case OPT_Wwrite_strings:
660      if (!c_dialect_cxx ())
661	flag_const_strings = value;
662      else
663	warn_write_strings = value;
664      break;
665
666    case OPT_ansi:
667      if (!c_dialect_cxx ())
668	set_std_c89 (false, true);
669      else
670	set_std_cxx98 (true);
671      break;
672
673    case OPT_d:
674      handle_OPT_d (arg);
675      break;
676
677    case OPT_fcond_mismatch:
678      if (!c_dialect_cxx ())
679	{
680	  flag_cond_mismatch = value;
681	  break;
682	}
683      /* Fall through.  */
684
685    case OPT_fall_virtual:
686    case OPT_falt_external_templates:
687    case OPT_fenum_int_equiv:
688    case OPT_fexternal_templates:
689    case OPT_fguiding_decls:
690    case OPT_fhonor_std:
691    case OPT_fhuge_objects:
692    case OPT_flabels_ok:
693    case OPT_fname_mangling_version_:
694    case OPT_fnew_abi:
695    case OPT_fnonnull_objects:
696    case OPT_fsquangle:
697    case OPT_fstrict_prototype:
698    case OPT_fthis_is_variable:
699    case OPT_fvtable_thunks:
700    case OPT_fxref:
701    case OPT_fvtable_gc:
702      warning ("switch \"%s\" is no longer supported", option->opt_text);
703      break;
704
705    case OPT_faccess_control:
706      flag_access_control = value;
707      break;
708
709    case OPT_fasm:
710      flag_no_asm = !value;
711      break;
712
713    case OPT_fbuiltin:
714      flag_no_builtin = !value;
715      break;
716
717    case OPT_fbuiltin_:
718      if (value)
719	result = 0;
720      else
721	disable_builtin_function (arg);
722      break;
723
724    case OPT_fdollars_in_identifiers:
725      cpp_opts->dollars_in_ident = value;
726      break;
727
728    case OPT_fdump_:
729      if (!dump_switch_p (arg))
730	result = 0;
731      break;
732
733    case OPT_ffreestanding:
734      value = !value;
735      /* Fall through....  */
736    case OPT_fhosted:
737      flag_hosted = value;
738      flag_no_builtin = !value;
739      /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
740      if (!value && warn_main == 2)
741	warn_main = 0;
742      break;
743
744    case OPT_fshort_double:
745      flag_short_double = value;
746      break;
747
748    case OPT_fshort_enums:
749      flag_short_enums = value;
750      break;
751
752    case OPT_fshort_wchar:
753      flag_short_wchar = value;
754      break;
755
756    case OPT_fsigned_bitfields:
757      flag_signed_bitfields = value;
758      explicit_flag_signed_bitfields = 1;
759      break;
760
761    case OPT_fsigned_char:
762      flag_signed_char = value;
763      break;
764
765    case OPT_funsigned_bitfields:
766      flag_signed_bitfields = !value;
767      explicit_flag_signed_bitfields = 1;
768      break;
769
770    case OPT_funsigned_char:
771      flag_signed_char = !value;
772      break;
773
774    case OPT_fcheck_new:
775      flag_check_new = value;
776      break;
777
778    case OPT_fconserve_space:
779      flag_conserve_space = value;
780      break;
781
782    case OPT_fconst_strings:
783      flag_const_strings = value;
784      break;
785
786    case OPT_fconstant_string_class_:
787      constant_string_class_name = arg;
788      break;
789
790    case OPT_fdefault_inline:
791      flag_default_inline = value;
792      break;
793
794    case OPT_felide_constructors:
795      flag_elide_constructors = value;
796      break;
797
798    case OPT_fenforce_eh_specs:
799      flag_enforce_eh_specs = value;
800      break;
801
802    case OPT_ffixed_form:
803    case OPT_ffixed_line_length_:
804      /* Fortran front end options ignored when preprocessing only.  */
805      if (!flag_preprocess_only)
806        result = 0;
807      break;
808
809    case OPT_ffor_scope:
810      flag_new_for_scope = value;
811      break;
812
813    case OPT_fgnu_keywords:
814      flag_no_gnu_keywords = !value;
815      break;
816
817    case OPT_fgnu_runtime:
818      flag_next_runtime = !value;
819      break;
820
821    case OPT_fhandle_exceptions:
822      warning ("-fhandle-exceptions has been renamed -fexceptions (and is now on by default)");
823      flag_exceptions = value;
824      break;
825
826    case OPT_fimplement_inlines:
827      flag_implement_inlines = value;
828      break;
829
830    case OPT_fimplicit_inline_templates:
831      flag_implicit_inline_templates = value;
832      break;
833
834    case OPT_fimplicit_templates:
835      flag_implicit_templates = value;
836      break;
837
838    case OPT_fms_extensions:
839      flag_ms_extensions = value;
840      break;
841
842    case OPT_fnext_runtime:
843      flag_next_runtime = value;
844      break;
845
846    case OPT_fnil_receivers:
847      flag_nil_receivers = value;
848      break;
849
850    case OPT_fnonansi_builtins:
851      flag_no_nonansi_builtin = !value;
852      break;
853
854    case OPT_fobjc_exceptions:
855      flag_objc_exceptions = value;
856      break;
857
858    case OPT_foperator_names:
859      cpp_opts->operator_names = value;
860      break;
861
862    case OPT_foptional_diags:
863      flag_optional_diags = value;
864      break;
865
866    case OPT_fpch_deps:
867      cpp_opts->restore_pch_deps = value;
868      break;
869
870    case OPT_fpermissive:
871      flag_permissive = value;
872      break;
873
874    case OPT_fpreprocessed:
875      cpp_opts->preprocessed = value;
876      break;
877
878    case OPT_freplace_objc_classes:
879      flag_replace_objc_classes = value;
880      break;
881
882    case OPT_frepo:
883      flag_use_repository = value;
884      if (value)
885	flag_implicit_templates = 0;
886      break;
887
888    case OPT_frtti:
889      flag_rtti = value;
890      break;
891
892    case OPT_fshow_column:
893      cpp_opts->show_column = value;
894      break;
895
896    case OPT_fstats:
897      flag_detailed_statistics = value;
898      break;
899
900    case OPT_ftabstop_:
901      /* It is documented that we silently ignore silly values.  */
902      if (value >= 1 && value <= 100)
903	cpp_opts->tabstop = value;
904      break;
905
906    case OPT_fexec_charset_:
907      cpp_opts->narrow_charset = arg;
908      break;
909
910    case OPT_fwide_exec_charset_:
911      cpp_opts->wide_charset = arg;
912      break;
913
914    case OPT_finput_charset_:
915      cpp_opts->input_charset = arg;
916      break;
917
918    case OPT_ftemplate_depth_:
919      max_tinst_depth = value;
920      break;
921
922    case OPT_fuse_cxa_atexit:
923      flag_use_cxa_atexit = value;
924      break;
925
926    case OPT_fweak:
927      flag_weak = value;
928      break;
929
930    case OPT_fzero_link:
931      flag_zero_link = value;
932      break;
933
934    case OPT_gen_decls:
935      flag_gen_declaration = 1;
936      break;
937
938    case OPT_idirafter:
939      add_path (xstrdup (arg), AFTER, 0);
940      break;
941
942    case OPT_imacros:
943    case OPT_include:
944      defer_opt (code, arg);
945      break;
946
947    case OPT_iprefix:
948      iprefix = arg;
949      break;
950
951    case OPT_isysroot:
952      sysroot = arg;
953      break;
954
955    case OPT_isystem:
956      add_path (xstrdup (arg), SYSTEM, 0);
957      break;
958
959    case OPT_iwithprefix:
960      add_prefixed_path (arg, SYSTEM);
961      break;
962
963    case OPT_iwithprefixbefore:
964      add_prefixed_path (arg, BRACKET);
965      break;
966
967    case OPT_lang_asm:
968      cpp_set_lang (parse_in, CLK_ASM);
969      cpp_opts->dollars_in_ident = false;
970      break;
971
972    case OPT_lang_objc:
973      cpp_opts->objc = 1;
974      break;
975
976    case OPT_nostdinc:
977      std_inc = false;
978      break;
979
980    case OPT_nostdinc__:
981      std_cxx_inc = false;
982      break;
983
984    case OPT_o:
985      if (!out_fname)
986	out_fname = arg;
987      else
988	error ("output filename specified twice");
989      break;
990
991      /* We need to handle the -pedantic switches here, rather than in
992	 c_common_post_options, so that a subsequent -Wno-endif-labels
993	 is not overridden.  */
994    case OPT_pedantic_errors:
995      cpp_opts->pedantic_errors = 1;
996      /* Fall through.  */
997    case OPT_pedantic:
998      cpp_opts->pedantic = 1;
999      cpp_opts->warn_endif_labels = 1;
1000      break;
1001
1002    case OPT_print_objc_runtime_info:
1003      print_struct_values = 1;
1004      break;
1005
1006    case OPT_remap:
1007      cpp_opts->remap = 1;
1008      break;
1009
1010    case OPT_std_c__98:
1011    case OPT_std_gnu__98:
1012      set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
1013      break;
1014
1015    case OPT_std_c89:
1016    case OPT_std_iso9899_1990:
1017    case OPT_std_iso9899_199409:
1018      set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
1019      break;
1020
1021    case OPT_std_gnu89:
1022      set_std_c89 (false /* c94 */, false /* ISO */);
1023      break;
1024
1025    case OPT_std_c99:
1026    case OPT_std_c9x:
1027    case OPT_std_iso9899_1999:
1028    case OPT_std_iso9899_199x:
1029      set_std_c99 (true /* ISO */);
1030      break;
1031
1032    case OPT_std_gnu99:
1033    case OPT_std_gnu9x:
1034      set_std_c99 (false /* ISO */);
1035      break;
1036
1037    case OPT_trigraphs:
1038      cpp_opts->trigraphs = 1;
1039      break;
1040
1041    case OPT_traditional_cpp:
1042      cpp_opts->traditional = 1;
1043      break;
1044
1045    case OPT_undef:
1046      flag_undef = 1;
1047      break;
1048
1049    case OPT_w:
1050      cpp_opts->inhibit_warnings = 1;
1051      break;
1052
1053    case OPT_v:
1054      verbose = true;
1055      break;
1056
1057    case OPT_fformat_extensions:
1058      break;
1059    }
1060
1061  return result;
1062}
1063
1064/* Post-switch processing.  */
1065bool
1066c_common_post_options (const char **pfilename)
1067{
1068  struct cpp_callbacks *cb;
1069
1070  /* Canonicalize the input and output filenames.  */
1071  if (in_fnames == NULL)
1072    {
1073      in_fnames = xmalloc (sizeof (in_fnames[0]));
1074      in_fnames[0] = "";
1075    }
1076  else if (strcmp (in_fnames[0], "-") == 0)
1077    in_fnames[0] = "";
1078
1079  if (out_fname == NULL || !strcmp (out_fname, "-"))
1080    out_fname = "";
1081
1082  if (cpp_opts->deps.style == DEPS_NONE)
1083    check_deps_environment_vars ();
1084
1085  handle_deferred_opts ();
1086
1087  sanitize_cpp_opts ();
1088
1089  register_include_chains (parse_in, sysroot, iprefix,
1090			   std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
1091
1092  flag_inline_trees = 1;
1093
1094  /* Use tree inlining if possible.  Function instrumentation is only
1095     done in the RTL level, so we disable tree inlining.  */
1096  if (flag_instrument_function_entry_exit)
1097    {
1098      flag_no_inline = 1;
1099      flag_really_no_inline = 1;
1100    }
1101  else
1102    {
1103      if (!flag_no_inline)
1104	flag_no_inline = 1;
1105      if (flag_inline_functions)
1106	{
1107	  flag_inline_trees = 2;
1108	  flag_inline_functions = 0;
1109	}
1110    }
1111
1112  /* -Wextra implies -Wsign-compare, but not if explicitly
1113      overridden.  */
1114  if (warn_sign_compare == -1)
1115    warn_sign_compare = extra_warnings;
1116
1117  /* Special format checking options don't work without -Wformat; warn if
1118     they are used.  */
1119  if (warn_format_y2k && !warn_format)
1120    warning ("-Wformat-y2k ignored without -Wformat");
1121  if (warn_format_extra_args && !warn_format)
1122    warning ("-Wformat-extra-args ignored without -Wformat");
1123  if (warn_format_zero_length && !warn_format)
1124    warning ("-Wformat-zero-length ignored without -Wformat");
1125  if (warn_format_nonliteral && !warn_format)
1126    warning ("-Wformat-nonliteral ignored without -Wformat");
1127  if (warn_format_security && !warn_format)
1128    warning ("-Wformat-security ignored without -Wformat");
1129  if (warn_missing_format_attribute && !warn_format)
1130    warning ("-Wmissing-format-attribute ignored without -Wformat");
1131
1132  if (flag_preprocess_only)
1133    {
1134      /* Open the output now.  We must do so even if flag_no_output is
1135	 on, because there may be other output than from the actual
1136	 preprocessing (e.g. from -dM).  */
1137      if (out_fname[0] == '\0')
1138	out_stream = stdout;
1139      else
1140	out_stream = fopen (out_fname, "w");
1141
1142      if (out_stream == NULL)
1143	{
1144	  fatal_error ("opening output file %s: %m", out_fname);
1145	  return false;
1146	}
1147
1148      if (num_in_fnames > 1)
1149	error ("too many filenames given.  Type %s --help for usage",
1150	       progname);
1151
1152      init_pp_output (out_stream);
1153    }
1154  else
1155    {
1156      init_c_lex ();
1157
1158      /* Yuk.  WTF is this?  I do know ObjC relies on it somewhere.  */
1159      input_line = 0;
1160    }
1161
1162  cb = cpp_get_callbacks (parse_in);
1163  cb->file_change = cb_file_change;
1164  cb->dir_change = cb_dir_change;
1165  cpp_post_options (parse_in);
1166
1167  saved_lineno = input_line;
1168  input_line = 0;
1169
1170  /* If an error has occurred in cpplib, note it so we fail
1171     immediately.  */
1172  errorcount += cpp_errors (parse_in);
1173
1174  *pfilename = this_input_filename
1175    = cpp_read_main_file (parse_in, in_fnames[0]);
1176  /* Don't do any compilation or preprocessing if there is no input file.  */
1177  if (this_input_filename == NULL)
1178    {
1179      errorcount++;
1180      return false;
1181    }
1182
1183  if (flag_working_directory
1184      && flag_preprocess_only && ! flag_no_line_commands)
1185    pp_dir_change (parse_in, get_src_pwd ());
1186
1187  return flag_preprocess_only;
1188}
1189
1190/* Front end initialization common to C, ObjC and C++.  */
1191bool
1192c_common_init (void)
1193{
1194  input_line = saved_lineno;
1195
1196  /* Set up preprocessor arithmetic.  Must be done after call to
1197     c_common_nodes_and_builtins for type nodes to be good.  */
1198  cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1199  cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1200  cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1201  cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1202  cpp_opts->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
1203  cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
1204
1205  /* This can't happen until after wchar_precision and bytes_big_endian
1206     are known.  */
1207  cpp_init_iconv (parse_in);
1208
1209  if (flag_preprocess_only)
1210    {
1211      finish_options ();
1212      preprocess_file (parse_in);
1213      return false;
1214    }
1215
1216  /* Has to wait until now so that cpplib has its hash table.  */
1217  init_pragma ();
1218
1219  return true;
1220}
1221
1222/* Initialize the integrated preprocessor after debug output has been
1223   initialized; loop over each input file.  */
1224void
1225c_common_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
1226{
1227  unsigned file_index;
1228
1229#if YYDEBUG != 0
1230  yydebug = set_yydebug;
1231#else
1232  warning ("YYDEBUG not defined");
1233#endif
1234
1235  file_index = 0;
1236
1237  do
1238    {
1239      if (file_index > 0)
1240	{
1241	  /* Reset the state of the parser.  */
1242	  c_reset_state();
1243
1244	  /* Reset cpplib's macros and start a new file.  */
1245	  cpp_undef_all (parse_in);
1246	  main_input_filename = this_input_filename
1247	    = cpp_read_main_file (parse_in, in_fnames[file_index]);
1248	  if (this_input_filename == NULL)
1249	    break;
1250	}
1251      finish_options ();
1252      if (file_index == 0)
1253	pch_init();
1254      c_parse_file ();
1255
1256      file_index++;
1257    } while (file_index < num_in_fnames);
1258
1259  finish_file ();
1260}
1261
1262/* Common finish hook for the C, ObjC and C++ front ends.  */
1263void
1264c_common_finish (void)
1265{
1266  FILE *deps_stream = NULL;
1267
1268  if (cpp_opts->deps.style != DEPS_NONE)
1269    {
1270      /* If -M or -MM was seen without -MF, default output to the
1271	 output stream.  */
1272      if (!deps_file)
1273	deps_stream = out_stream;
1274      else
1275	{
1276	  deps_stream = fopen (deps_file, deps_append ? "a": "w");
1277	  if (!deps_stream)
1278	    fatal_error ("opening dependency file %s: %m", deps_file);
1279	}
1280    }
1281
1282  /* For performance, avoid tearing down cpplib's internal structures
1283     with cpp_destroy ().  */
1284  errorcount += cpp_finish (parse_in, deps_stream);
1285
1286  if (deps_stream && deps_stream != out_stream
1287      && (ferror (deps_stream) || fclose (deps_stream)))
1288    fatal_error ("closing dependency file %s: %m", deps_file);
1289
1290  if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1291    fatal_error ("when writing output to %s: %m", out_fname);
1292}
1293
1294/* Either of two environment variables can specify output of
1295   dependencies.  Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1296   DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1297   and DEPS_TARGET is the target to mention in the deps.  They also
1298   result in dependency information being appended to the output file
1299   rather than overwriting it, and like Sun's compiler
1300   SUNPRO_DEPENDENCIES suppresses the dependency on the main file.  */
1301static void
1302check_deps_environment_vars (void)
1303{
1304  char *spec;
1305
1306  GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
1307  if (spec)
1308    cpp_opts->deps.style = DEPS_USER;
1309  else
1310    {
1311      GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
1312      if (spec)
1313	{
1314	  cpp_opts->deps.style = DEPS_SYSTEM;
1315	  cpp_opts->deps.ignore_main_file = true;
1316	}
1317    }
1318
1319  if (spec)
1320    {
1321      /* Find the space before the DEPS_TARGET, if there is one.  */
1322      char *s = strchr (spec, ' ');
1323      if (s)
1324	{
1325	  /* Let the caller perform MAKE quoting.  */
1326	  defer_opt (OPT_MT, s + 1);
1327	  *s = '\0';
1328	}
1329
1330      /* Command line -MF overrides environment variables and default.  */
1331      if (!deps_file)
1332	deps_file = spec;
1333
1334      deps_append = 1;
1335    }
1336}
1337
1338/* Handle deferred command line switches.  */
1339static void
1340handle_deferred_opts (void)
1341{
1342  size_t i;
1343
1344  for (i = 0; i < deferred_count; i++)
1345    {
1346      struct deferred_opt *opt = &deferred_opts[i];
1347
1348      if (opt->code == OPT_MT || opt->code == OPT_MQ)
1349	cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
1350    }
1351}
1352
1353/* These settings are appropriate for GCC, but not necessarily so for
1354   cpplib as a library.  */
1355static void
1356sanitize_cpp_opts (void)
1357{
1358  /* If we don't know what style of dependencies to output, complain
1359     if any other dependency switches have been given.  */
1360  if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1361    error ("to generate dependencies you must specify either -M or -MM");
1362
1363  /* -dM and dependencies suppress normal output; do it here so that
1364     the last -d[MDN] switch overrides earlier ones.  */
1365  if (flag_dump_macros == 'M')
1366    flag_no_output = 1;
1367
1368  /* Disable -dD, -dN and -dI if normal output is suppressed.  Allow
1369     -dM since at least glibc relies on -M -dM to work.  */
1370  /* Also, flag_no_output implies flag_no_line_commands, always. */
1371  if (flag_no_output)
1372    {
1373      if (flag_dump_macros != 'M')
1374	flag_dump_macros = 0;
1375      flag_dump_includes = 0;
1376      flag_no_line_commands = 1;
1377    }
1378
1379  cpp_opts->unsigned_char = !flag_signed_char;
1380  cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1381
1382  /* We want -Wno-long-long to override -pedantic -std=non-c99
1383     and/or -Wtraditional, whatever the ordering.  */
1384  cpp_opts->warn_long_long
1385    = warn_long_long && ((!flag_isoc99 && pedantic) || warn_traditional);
1386
1387  /* If we're generating preprocessor output, emit current directory
1388     if explicitly requested or if debugging information is enabled.
1389     ??? Maybe we should only do it for debugging formats that
1390     actually output the current directory?  */
1391  if (flag_working_directory == -1)
1392    flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
1393}
1394
1395/* Add include path with a prefix at the front of its name.  */
1396static void
1397add_prefixed_path (const char *suffix, size_t chain)
1398{
1399  char *path;
1400  const char *prefix;
1401  size_t prefix_len, suffix_len;
1402
1403  suffix_len = strlen (suffix);
1404  prefix     = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1405  prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1406
1407  path = xmalloc (prefix_len + suffix_len + 1);
1408  memcpy (path, prefix, prefix_len);
1409  memcpy (path + prefix_len, suffix, suffix_len);
1410  path[prefix_len + suffix_len] = '\0';
1411
1412  add_path (path, chain, 0);
1413}
1414
1415/* Handle -D, -U, -A, -imacros, and the first -include.  */
1416static void
1417finish_options (void)
1418{
1419  if (!cpp_opts->preprocessed)
1420    {
1421      size_t i;
1422
1423      cpp_change_file (parse_in, LC_RENAME, _("<built-in>"));
1424      cpp_init_builtins (parse_in, flag_hosted);
1425      c_cpp_builtins (parse_in);
1426
1427      /* We're about to send user input to cpplib, so make it warn for
1428	 things that we previously (when we sent it internal definitions)
1429	 told it to not warn.
1430
1431	 C99 permits implementation-defined characters in identifiers.
1432	 The documented meaning of -std= is to turn off extensions that
1433	 conflict with the specified standard, and since a strictly
1434	 conforming program cannot contain a '$', we do not condition
1435	 their acceptance on the -std= setting.  */
1436      cpp_opts->warn_dollars = (cpp_opts->pedantic && !cpp_opts->c99);
1437
1438      cpp_change_file (parse_in, LC_RENAME, _("<command line>"));
1439      for (i = 0; i < deferred_count; i++)
1440	{
1441	  struct deferred_opt *opt = &deferred_opts[i];
1442
1443	  if (opt->code == OPT_D)
1444	    cpp_define (parse_in, opt->arg);
1445	  else if (opt->code == OPT_U)
1446	    cpp_undef (parse_in, opt->arg);
1447	  else if (opt->code == OPT_A)
1448	    {
1449	      if (opt->arg[0] == '-')
1450		cpp_unassert (parse_in, opt->arg + 1);
1451	      else
1452		cpp_assert (parse_in, opt->arg);
1453	    }
1454	}
1455
1456      /* Handle -imacros after -D and -U.  */
1457      for (i = 0; i < deferred_count; i++)
1458	{
1459	  struct deferred_opt *opt = &deferred_opts[i];
1460
1461	  if (opt->code == OPT_imacros
1462	      && cpp_push_include (parse_in, opt->arg))
1463	    {
1464	      /* Disable push_command_line_include callback for now.  */
1465	      include_cursor = deferred_count + 1;
1466	      cpp_scan_nooutput (parse_in);
1467	    }
1468	}
1469    }
1470
1471  include_cursor = 0;
1472  push_command_line_include ();
1473}
1474
1475/* Give CPP the next file given by -include, if any.  */
1476static void
1477push_command_line_include (void)
1478{
1479  while (include_cursor < deferred_count)
1480    {
1481      struct deferred_opt *opt = &deferred_opts[include_cursor++];
1482
1483      if (! cpp_opts->preprocessed && opt->code == OPT_include
1484	  && cpp_push_include (parse_in, opt->arg))
1485	return;
1486    }
1487
1488  if (include_cursor == deferred_count)
1489    {
1490      include_cursor++;
1491      /* -Wunused-macros should only warn about macros defined hereafter.  */
1492      cpp_opts->warn_unused_macros = warn_unused_macros;
1493      /* Restore the line map from <command line>.  */
1494      if (! cpp_opts->preprocessed)
1495	cpp_change_file (parse_in, LC_RENAME, main_input_filename);
1496
1497      /* Set this here so the client can change the option if it wishes,
1498	 and after stacking the main file so we don't trace the main file.  */
1499      cpp_get_line_maps (parse_in)->trace_includes
1500	= cpp_opts->print_include_names;
1501    }
1502}
1503
1504/* File change callback.  Has to handle -include files.  */
1505static void
1506cb_file_change (cpp_reader *pfile ATTRIBUTE_UNUSED,
1507		const struct line_map *new_map)
1508{
1509  if (flag_preprocess_only)
1510    pp_file_change (new_map);
1511  else
1512    fe_file_change (new_map);
1513
1514  if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
1515    push_command_line_include ();
1516}
1517
1518void
1519cb_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1520{
1521  if (! set_src_pwd (dir))
1522    warning ("too late for # directive to set debug directory");
1523}
1524
1525/* Set the C 89 standard (with 1994 amendments if C94, without GNU
1526   extensions if ISO).  There is no concept of gnu94.  */
1527static void
1528set_std_c89 (int c94, int iso)
1529{
1530  cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1531  flag_iso = iso;
1532  flag_no_asm = iso;
1533  flag_no_gnu_keywords = iso;
1534  flag_no_nonansi_builtin = iso;
1535  flag_isoc94 = c94;
1536  flag_isoc99 = 0;
1537  flag_writable_strings = 0;
1538}
1539
1540/* Set the C 99 standard (without GNU extensions if ISO).  */
1541static void
1542set_std_c99 (int iso)
1543{
1544  cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1545  flag_no_asm = iso;
1546  flag_no_nonansi_builtin = iso;
1547  flag_iso = iso;
1548  flag_isoc99 = 1;
1549  flag_isoc94 = 1;
1550  flag_writable_strings = 0;
1551}
1552
1553/* Set the C++ 98 standard (without GNU extensions if ISO).  */
1554static void
1555set_std_cxx98 (int iso)
1556{
1557  cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1558  flag_no_gnu_keywords = iso;
1559  flag_no_nonansi_builtin = iso;
1560  flag_iso = iso;
1561}
1562
1563/* Handle setting implicit to ON.  */
1564static void
1565set_Wimplicit (int on)
1566{
1567  warn_implicit = on;
1568  warn_implicit_int = on;
1569  if (on)
1570    {
1571      if (mesg_implicit_function_declaration != 2)
1572	mesg_implicit_function_declaration = 1;
1573    }
1574  else
1575    mesg_implicit_function_declaration = 0;
1576}
1577
1578/* Args to -d specify what to dump.  Silently ignore
1579   unrecognized options; they may be aimed at toplev.c.  */
1580static void
1581handle_OPT_d (const char *arg)
1582{
1583  char c;
1584
1585  while ((c = *arg++) != '\0')
1586    switch (c)
1587      {
1588      case 'M':			/* Dump macros only.  */
1589      case 'N':			/* Dump names.  */
1590      case 'D':			/* Dump definitions.  */
1591	flag_dump_macros = c;
1592	break;
1593
1594      case 'I':
1595	flag_dump_includes = 1;
1596	break;
1597      }
1598}
1599