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