]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-opts.c
Update copyright years.
[thirdparty/gcc.git] / gcc / c-family / c-opts.c
CommitLineData
428a5a4e 1/* C/ObjC/C++ command line option handling.
f1717362 2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
428a5a4e 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
8c4c00c1 9Software Foundation; either version 3, or (at your option) any later
428a5a4e 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
8c4c00c1 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
428a5a4e 20
21#include "config.h"
22#include "system.h"
805e22b2 23#include "coretypes.h"
e51764ad 24#include "tm.h"
4cba6f60 25#include "c-target.h"
428a5a4e 26#include "c-common.h"
4cba6f60 27#include "tm_p.h" /* For C_COMMON_OVERRIDE_OPTIONS. */
28#include "diagnostic.h"
428a5a4e 29#include "c-pragma.h"
30#include "flags.h"
31#include "toplev.h"
32#include "langhooks.h"
399d4f80 33#include "tree-diagnostic.h" /* for virt_loc_aware_diagnostic_finalizer */
45c365b1 34#include "intl.h"
e69f4d0e 35#include "cppdefault.h"
2ecc6bc5 36#include "incpath.h"
b717e161 37#include "debug.h" /* For debug_hooks. */
5457b645 38#include "opts.h"
0391a567 39#include "plugin.h" /* For PLUGIN_INCLUDE_FILE event. */
3eb3f293 40#include "mkdeps.h"
8f7fba47 41#include "dumpfile.h"
e69f4d0e 42
14e33bb6 43#ifndef DOLLARS_IN_IDENTIFIERS
44# define DOLLARS_IN_IDENTIFIERS true
45#endif
46
e69f4d0e 47#ifndef TARGET_SYSTEM_ROOT
48# define TARGET_SYSTEM_ROOT NULL
49#endif
428a5a4e 50
d6f6a4e9 51#ifndef TARGET_OPTF
52#define TARGET_OPTF(ARG)
53#endif
54
61191376 55/* CPP's options. */
caefd295 56cpp_options *cpp_opts;
72db3845 57
79cf3ec1 58/* Input filename. */
9f8e7348 59static const char *this_input_filename;
79cf3ec1 60
6019c708 61/* Filename and stream for preprocessed output. */
62static const char *out_fname;
63static FILE *out_stream;
64
65/* Append dependencies to deps_file. */
66static bool deps_append;
67
61191376 68/* If dependency switches (-MF etc.) have been given. */
69static bool deps_seen;
70
e69f4d0e 71/* If -v seen. */
72static bool verbose;
73
61191376 74/* Dependency output file. */
75static const char *deps_file;
76
e69f4d0e 77/* The prefix given by -iprefix, if any. */
78static const char *iprefix;
79
72779020 80/* The multilib directory given by -imultilib, if any. */
81static const char *imultilib;
82
e69f4d0e 83/* The system root, if any. Overridden by -isysroot. */
84static const char *sysroot = TARGET_SYSTEM_ROOT;
85
86/* Zero disables all standard directories for headers. */
87static bool std_inc = true;
88
89/* Zero disables the C++-specific standard directories for headers. */
90static bool std_cxx_inc = true;
91
92/* If the quote chain has been split by -I-. */
93static bool quote_chain_split;
94
4838a8b6 95/* Number of deferred options. */
96static size_t deferred_count;
61191376 97
b717e161 98/* Number of deferred options scanned for -include. */
99static size_t include_cursor;
100
8f7fba47 101/* Dump files/flags to use during parsing. */
102static FILE *original_dump_file = NULL;
103static int original_dump_flags;
104static FILE *class_dump_file = NULL;
105static int class_dump_flags;
106
6adc88f8 107/* Whether any standard preincluded header has been preincluded. */
108static bool done_preinclude;
109
2b30d46c 110static void handle_OPT_d (const char *);
111static void set_std_cxx98 (int);
0d84dc2d 112static void set_std_cxx11 (int);
4e454776 113static void set_std_cxx14 (int);
4b53bc0f 114static void set_std_cxx1z (int);
2b30d46c 115static void set_std_c89 (int, int);
116static void set_std_c99 (int);
32074525 117static void set_std_c11 (int);
2b30d46c 118static void check_deps_environment_vars (void);
119static void handle_deferred_opts (void);
120static void sanitize_cpp_opts (void);
121static void add_prefixed_path (const char *, size_t);
122static void push_command_line_include (void);
551e34da 123static void cb_file_change (cpp_reader *, const line_map_ordinary *);
d732fcf0 124static void cb_dir_change (cpp_reader *, const char *);
79396169 125static void c_finish_options (void);
79cf3ec1 126
127#ifndef STDC_0_IN_SYSTEM_HEADERS
128#define STDC_0_IN_SYSTEM_HEADERS 0
129#endif
428a5a4e 130
5457b645 131/* Holds switches parsed by c_common_handle_option (), but whose
d716ce75 132 handling is deferred to c_common_post_options (). */
2b30d46c 133static void defer_opt (enum opt_code, const char *);
61191376 134static struct deferred_opt
135{
136 enum opt_code code;
137 const char *arg;
138} *deferred_opts;
139
1f3db819 140
c123f04d 141extern const unsigned int
1f3db819 142c_family_lang_mask = (CL_C | CL_CXX | CL_ObjC | CL_ObjCXX);
143
61191376 144/* Defer option CODE with argument ARG. */
145static void
2b30d46c 146defer_opt (enum opt_code code, const char *arg)
61191376 147{
61191376 148 deferred_opts[deferred_count].code = code;
149 deferred_opts[deferred_count].arg = arg;
150 deferred_count++;
151}
152
e88d34f6 153/* Return language mask for option parsing. */
4838a8b6 154unsigned int
e88d34f6 155c_common_option_lang_mask (void)
428a5a4e 156{
4838a8b6 157 static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
c0f19401 158
e88d34f6 159 return lang_flags[c_language];
160}
161
399d4f80 162/* Diagnostic finalizer for C/C++/Objective-C/Objective-C++. */
163static void
164c_diagnostic_finalizer (diagnostic_context *context,
165 diagnostic_info *diagnostic)
166{
167 diagnostic_show_locus (context, diagnostic);
168 /* By default print macro expansion contexts in the diagnostic
169 finalizer -- for tokens resulting from macro expansion. */
170 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
171 pp_destroy_prefix (context->printer);
172 pp_newline_and_flush (context->printer);
173}
174
32ecf960 175/* Common default settings for diagnostics. */
e88d34f6 176void
32ecf960 177c_common_diagnostics_set_defaults (diagnostic_context *context)
e88d34f6 178{
399d4f80 179 diagnostic_finalizer (context) = c_diagnostic_finalizer;
e88d34f6 180 context->opt_permissive = OPT_fpermissive;
181}
182
183/* Whether options from all C-family languages should be accepted
184 quietly. */
185static bool accept_all_c_family_options = false;
186
187/* Return whether to complain about a wrong-language option. */
188bool
189c_common_complain_wrong_lang_p (const struct cl_option *option)
190{
191 if (accept_all_c_family_options
192 && (option->flags & c_family_lang_mask))
193 return false;
194
195 return true;
196}
197
f3f006ad 198/* Initialize options structure OPTS. */
199void
200c_common_init_options_struct (struct gcc_options *opts)
201{
202 opts->x_flag_exceptions = c_dialect_cxx ();
203 opts->x_warn_pointer_arith = c_dialect_cxx ();
204 opts->x_warn_write_strings = c_dialect_cxx ();
205 opts->x_flag_warn_unused_result = true;
206
207 /* By default, C99-like requirements for complex multiply and divide. */
208 opts->x_flag_complex_method = 2;
209}
210
e88d34f6 211/* Common initialization before calling option handlers. */
212void
213c_common_init_options (unsigned int decoded_options_count,
214 struct cl_decoded_option *decoded_options)
215{
216 unsigned int i;
217 struct cpp_callbacks *cb;
3c6a9715 218
c0f19401 219 parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
931b0a0f 220 ident_hash, line_table);
7f5f3953 221 cb = cpp_get_callbacks (parse_in);
222 cb->error = c_cpp_error;
c0f19401 223
72db3845 224 cpp_opts = cpp_get_options (parse_in);
14e33bb6 225 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
c0f19401 226 cpp_opts->objc = c_dialect_objc ();
f0c2775b 227
228 /* Reset to avoid warnings on internal definitions. We set it just
229 before passing on command-line options to cpplib. */
230 cpp_opts->warn_dollars = 0;
231
e88d34f6 232 deferred_opts = XNEWVEC (struct deferred_opt, decoded_options_count);
b95a705c 233
9278f8d3 234 if (c_language == clk_c)
b95a705c 235 {
b2601928 236 /* The default for C is gnu11. */
237 set_std_c11 (false /* ISO */);
238
9278f8d3 239 /* If preprocessing assembly language, accept any of the C-family
240 front end options since the driver may pass them through. */
e88d34f6 241 for (i = 1; i < decoded_options_count; i++)
242 if (decoded_options[i].opt_index == OPT_lang_asm)
9278f8d3 243 {
e88d34f6 244 accept_all_c_family_options = true;
9278f8d3 245 break;
246 }
b95a705c 247 }
a96cefb2 248
249 global_dc->colorize_source_p = true;
428a5a4e 250}
251
e7aa92b2 252/* Handle switch SCODE with argument ARG. VALUE is true, unless no-
b78351e5 253 form of an -f or -W option was given. Returns false if the switch was
254 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
255bool
1f3db819 256c_common_handle_option (size_t scode, const char *arg, int value,
3c6c0e40 257 int kind, location_t loc,
258 const struct cl_option_handlers *handlers)
428a5a4e 259{
5457b645 260 const struct cl_option *option = &cl_options[scode];
261 enum opt_code code = (enum opt_code) scode;
b78351e5 262 bool result = true;
5457b645 263
bc7ab021 264 /* Prevent resetting the language standard to a C dialect when the driver
265 has already determined that we're looking at assembler input. */
266 bool preprocessing_asm_p = (cpp_get_options (parse_in)->lang == CLK_ASM);
48e1416a 267
803894d2 268 switch (code)
428a5a4e 269 {
5457b645 270 default:
1f3db819 271 if (cl_options[code].flags & c_family_lang_mask)
b9fc964a 272 {
273 if ((option->flags & CL_TARGET)
274 && ! targetcm.handle_c_option (scode, arg, value))
b78351e5 275 result = false;
b9fc964a 276 break;
277 }
b78351e5 278 result = false;
b95a705c 279 break;
428a5a4e 280
803894d2 281 case OPT__output_pch_:
573aba85 282 pch_file = arg;
283 break;
284
fc3c7720 285 case OPT_A:
286 defer_opt (code, arg);
287 break;
288
45c365b1 289 case OPT_C:
290 cpp_opts->discard_comments = 0;
291 break;
292
293 case OPT_CC:
294 cpp_opts->discard_comments = 0;
295 cpp_opts->discard_comments_in_macro_exp = 0;
296 break;
297
fc3c7720 298 case OPT_D:
299 defer_opt (code, arg);
300 break;
301
45c365b1 302 case OPT_H:
303 cpp_opts->print_include_names = 1;
304 break;
305
065e625b 306 case OPT_F:
307 TARGET_OPTF (xstrdup (arg));
308 break;
309
e69f4d0e 310 case OPT_I:
311 if (strcmp (arg, "-"))
06fd18c9 312 add_path (xstrdup (arg), BRACKET, 0, true);
e69f4d0e 313 else
314 {
315 if (quote_chain_split)
316 error ("-I- specified twice");
317 quote_chain_split = true;
318 split_quote_chain ();
5bcc316e 319 inform (input_location, "obsolete option -I- used, please use -iquote instead");
e69f4d0e 320 }
321 break;
322
61191376 323 case OPT_M:
324 case OPT_MM:
325 /* When doing dependencies with -M or -MM, suppress normal
326 preprocessed output, but still do -dM etc. as software
327 depends on this. Preprocessed output does occur if -MD, -MMD
328 or environment var dependency generation is used. */
329 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
f7070933 330 flag_no_output = 1;
61191376 331 break;
332
89c69892 333 case OPT_MD:
334 case OPT_MMD:
335 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
c25f2689 336 cpp_opts->deps.need_preprocessor_output = true;
61191376 337 deps_file = arg;
338 break;
339
340 case OPT_MF:
341 deps_seen = true;
342 deps_file = arg;
343 break;
344
345 case OPT_MG:
346 deps_seen = true;
347 cpp_opts->deps.missing_files = true;
348 break;
349
350 case OPT_MP:
351 deps_seen = true;
352 cpp_opts->deps.phony_targets = true;
353 break;
354
355 case OPT_MQ:
356 case OPT_MT:
357 deps_seen = true;
358 defer_opt (code, arg);
359 break;
360
45c365b1 361 case OPT_P:
f7070933 362 flag_no_line_commands = 1;
45c365b1 363 break;
364
fc3c7720 365 case OPT_U:
366 defer_opt (code, arg);
367 break;
368
428a5a4e 369 case OPT_Wall:
77b27208 370 /* ??? Don't add new options here. Use LangEnabledBy in c.opt. */
72db3845 371
837277ab 372 cpp_opts->warn_num_sign_change = value;
428a5a4e 373 break;
374
428a5a4e 375 case OPT_Wunknown_pragmas:
376 /* Set to greater than 1, so that even unknown pragmas in
2b30d46c 377 system headers will be warned about. */
45efa6b9 378 /* ??? There is no way to handle this automatically for now. */
837277ab 379 warn_unknown_pragmas = value * 2;
428a5a4e 380 break;
381
3ae4c9f3 382 case OPT_ansi:
c0f19401 383 if (!c_dialect_cxx ())
3ae4c9f3 384 set_std_c89 (false, true);
385 else
386 set_std_cxx98 (true);
387 break;
428a5a4e 388
45c365b1 389 case OPT_d:
390 handle_OPT_d (arg);
391 break;
392
74b777e5 393 case OPT_Wabi_:
394 warn_abi = true;
395 if (value == 1)
396 {
397 warning (0, "%<-Wabi=1%> is not supported, using =2");
398 value = 2;
399 }
229a58b1 400 warn_abi_version = value;
401 if (flag_abi_compat_version == -1)
402 flag_abi_compat_version = value;
74b777e5 403 break;
404
52bc861d 405 case OPT_fcanonical_system_headers:
406 cpp_opts->canonical_system_headers = value;
407 break;
408
428a5a4e 409 case OPT_fcond_mismatch:
c0f19401 410 if (!c_dialect_cxx ())
428a5a4e 411 {
837277ab 412 flag_cond_mismatch = value;
428a5a4e 413 break;
414 }
c3ceba8e 415 warning (0, "switch %qs is no longer supported", option->opt_text);
428a5a4e 416 break;
417
428a5a4e 418 case OPT_fbuiltin_:
837277ab 419 if (value)
b78351e5 420 result = false;
45c365b1 421 else
422 disable_builtin_function (arg);
428a5a4e 423 break;
424
fcde64dc 425 case OPT_fdirectives_only:
8d3e018c 426 cpp_opts->directives_only = value;
fcde64dc 427 break;
428
428a5a4e 429 case OPT_fdollars_in_identifiers:
837277ab 430 cpp_opts->dollars_in_ident = value;
428a5a4e 431 break;
432
428a5a4e 433 case OPT_ffreestanding:
837277ab 434 value = !value;
b4b174c3 435 /* Fall through.... */
428a5a4e 436 case OPT_fhosted:
837277ab 437 flag_hosted = value;
438 flag_no_builtin = !value;
428a5a4e 439 break;
440
803894d2 441 case OPT_fconstant_string_class_:
45c365b1 442 constant_string_class_name = arg;
428a5a4e 443 break;
444
865c4e44 445 case OPT_fextended_identifiers:
446 cpp_opts->extended_identifiers = value;
447 break;
448
72db3845 449 case OPT_foperator_names:
837277ab 450 cpp_opts->operator_names = value;
72db3845 451 break;
452
573aba85 453 case OPT_fpch_deps:
837277ab 454 cpp_opts->restore_pch_deps = value;
573aba85 455 break;
456
d718b525 457 case OPT_fpch_preprocess:
458 flag_pch_preprocess = value;
459 break;
460
428a5a4e 461 case OPT_fpermissive:
3a283672 462 flag_permissive = value;
463 global_dc->permissive = value;
428a5a4e 464 break;
465
72db3845 466 case OPT_fpreprocessed:
837277ab 467 cpp_opts->preprocessed = value;
72db3845 468 break;
469
62db153a 470 case OPT_fdebug_cpp:
471 cpp_opts->debug = 1;
472 break;
473
ce70f433 474 case OPT_ftrack_macro_expansion:
475 if (value)
476 value = 2;
477 /* Fall Through. */
478
479 case OPT_ftrack_macro_expansion_:
480 if (arg && *arg != '\0')
481 cpp_opts->track_macro_expansion = value;
482 else
483 cpp_opts->track_macro_expansion = 2;
484 break;
485
428a5a4e 486 case OPT_frepo:
837277ab 487 flag_use_repository = value;
488 if (value)
428a5a4e 489 flag_implicit_templates = 0;
490 break;
491
803894d2 492 case OPT_ftabstop_:
72db3845 493 /* It is documented that we silently ignore silly values. */
837277ab 494 if (value >= 1 && value <= 100)
495 cpp_opts->tabstop = value;
72db3845 496 break;
497
ebc03810 498 case OPT_fexec_charset_:
499 cpp_opts->narrow_charset = arg;
500 break;
501
502 case OPT_fwide_exec_charset_:
503 cpp_opts->wide_charset = arg;
504 break;
505
57ba96e9 506 case OPT_finput_charset_:
507 cpp_opts->input_charset = arg;
508 break;
509
803894d2 510 case OPT_ftemplate_depth_:
837277ab 511 max_tinst_depth = value;
428a5a4e 512 break;
513
b212f378 514 case OPT_fvisibility_inlines_hidden:
515 visibility_options.inlines_hidden = value;
516 break;
428a5a4e 517
0e4744ac 518 case OPT_femit_struct_debug_baseonly:
9faf44d6 519 set_struct_debug_option (&global_options, loc, "base");
0e4744ac 520 break;
521
522 case OPT_femit_struct_debug_reduced:
9faf44d6 523 set_struct_debug_option (&global_options, loc,
d7175aef 524 "dir:ord:sys,dir:gen:any,ind:base");
0e4744ac 525 break;
526
527 case OPT_femit_struct_debug_detailed_:
9faf44d6 528 set_struct_debug_option (&global_options, loc, arg);
0e4744ac 529 break;
530
2dd00636 531 case OPT_fext_numeric_literals:
532 cpp_opts->ext_numeric_literals = value;
533 break;
534
e69f4d0e 535 case OPT_idirafter:
06fd18c9 536 add_path (xstrdup (arg), AFTER, 0, true);
e69f4d0e 537 break;
538
d41b6698 539 case OPT_imacros:
b717e161 540 case OPT_include:
541 defer_opt (code, arg);
542 break;
543
72779020 544 case OPT_imultilib:
545 imultilib = arg;
546 break;
547
e69f4d0e 548 case OPT_iprefix:
549 iprefix = arg;
550 break;
551
a55b2364 552 case OPT_iquote:
06fd18c9 553 add_path (xstrdup (arg), QUOTE, 0, true);
a55b2364 554 break;
555
e69f4d0e 556 case OPT_isysroot:
557 sysroot = arg;
558 break;
559
560 case OPT_isystem:
06fd18c9 561 add_path (xstrdup (arg), SYSTEM, 0, true);
e69f4d0e 562 break;
563
564 case OPT_iwithprefix:
565 add_prefixed_path (arg, SYSTEM);
566 break;
567
568 case OPT_iwithprefixbefore:
569 add_prefixed_path (arg, BRACKET);
570 break;
571
3ae4c9f3 572 case OPT_lang_asm:
573 cpp_set_lang (parse_in, CLK_ASM);
d7518d5f 574 cpp_opts->dollars_in_ident = false;
3ae4c9f3 575 break;
576
45c365b1 577 case OPT_nostdinc:
e69f4d0e 578 std_inc = false;
45c365b1 579 break;
580
803894d2 581 case OPT_nostdinc__:
e69f4d0e 582 std_cxx_inc = false;
45c365b1 583 break;
584
dfeeca46 585 case OPT_o:
6019c708 586 if (!out_fname)
587 out_fname = arg;
dfeeca46 588 else
2f423865 589 error ("output filename specified twice");
dfeeca46 590 break;
591
428a5a4e 592 case OPT_print_objc_runtime_info:
593 print_struct_values = 1;
594 break;
595
dfeeca46 596 case OPT_remap:
597 cpp_opts->remap = 1;
428a5a4e 598 break;
599
803894d2 600 case OPT_std_c__98:
601 case OPT_std_gnu__98:
bc7ab021 602 if (!preprocessing_asm_p)
603 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
428a5a4e 604 break;
605
0d84dc2d 606 case OPT_std_c__11:
607 case OPT_std_gnu__11:
0fe6eeac 608 if (!preprocessing_asm_p)
2dd00636 609 {
610 set_std_cxx11 (code == OPT_std_c__11 /* ISO */);
611 if (code == OPT_std_c__11)
612 cpp_opts->ext_numeric_literals = 0;
613 }
0fe6eeac 614 break;
615
c35e53f1 616 case OPT_std_c__14:
617 case OPT_std_gnu__14:
543efdbe 618 if (!preprocessing_asm_p)
2dd00636 619 {
c35e53f1 620 set_std_cxx14 (code == OPT_std_c__14 /* ISO */);
621 if (code == OPT_std_c__14)
2dd00636 622 cpp_opts->ext_numeric_literals = 0;
623 }
543efdbe 624 break;
625
4b53bc0f 626 case OPT_std_c__1z:
627 case OPT_std_gnu__1z:
628 if (!preprocessing_asm_p)
629 {
630 set_std_cxx1z (code == OPT_std_c__1z /* ISO */);
631 if (code == OPT_std_c__1z)
632 cpp_opts->ext_numeric_literals = 0;
633 }
634 break;
635
32f1f35b 636 case OPT_std_c90:
dfeeca46 637 case OPT_std_iso9899_199409:
bc7ab021 638 if (!preprocessing_asm_p)
639 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
3ae4c9f3 640 break;
641
32f1f35b 642 case OPT_std_gnu90:
bc7ab021 643 if (!preprocessing_asm_p)
644 set_std_c89 (false /* c94 */, false /* ISO */);
428a5a4e 645 break;
646
647 case OPT_std_c99:
bc7ab021 648 if (!preprocessing_asm_p)
649 set_std_c99 (true /* ISO */);
428a5a4e 650 break;
651
428a5a4e 652 case OPT_std_gnu99:
bc7ab021 653 if (!preprocessing_asm_p)
654 set_std_c99 (false /* ISO */);
428a5a4e 655 break;
656
32074525 657 case OPT_std_c11:
39012afb 658 if (!preprocessing_asm_p)
32074525 659 set_std_c11 (true /* ISO */);
39012afb 660 break;
661
32074525 662 case OPT_std_gnu11:
39012afb 663 if (!preprocessing_asm_p)
32074525 664 set_std_c11 (false /* ISO */);
39012afb 665 break;
666
45c365b1 667 case OPT_trigraphs:
668 cpp_opts->trigraphs = 1;
669 break;
670
671 case OPT_traditional_cpp:
672 cpp_opts->traditional = 1;
673 break;
674
45c365b1 675 case OPT_v:
e69f4d0e 676 verbose = true;
45c365b1 677 break;
678 }
428a5a4e 679
d3b7ee7c 680 switch (c_language)
681 {
682 case clk_c:
683 C_handle_option_auto (&global_options, &global_options_set,
684 scode, arg, value,
685 c_family_lang_mask, kind,
686 loc, handlers, global_dc);
687 break;
688
689 case clk_objc:
690 ObjC_handle_option_auto (&global_options, &global_options_set,
691 scode, arg, value,
692 c_family_lang_mask, kind,
693 loc, handlers, global_dc);
694 break;
695
696 case clk_cxx:
697 CXX_handle_option_auto (&global_options, &global_options_set,
698 scode, arg, value,
699 c_family_lang_mask, kind,
700 loc, handlers, global_dc);
701 break;
702
703 case clk_objcxx:
704 ObjCXX_handle_option_auto (&global_options, &global_options_set,
705 scode, arg, value,
706 c_family_lang_mask, kind,
707 loc, handlers, global_dc);
708 break;
709
710 default:
711 gcc_unreachable ();
712 }
3636964b 713
714 cpp_handle_option_auto (&global_options, scode, cpp_opts);
428a5a4e 715 return result;
716}
717
c94b1d0e 718/* Default implementation of TARGET_HANDLE_C_OPTION. */
719
720bool
721default_handle_c_option (size_t code ATTRIBUTE_UNUSED,
722 const char *arg ATTRIBUTE_UNUSED,
723 int value ATTRIBUTE_UNUSED)
724{
725 return false;
726}
727
428a5a4e 728/* Post-switch processing. */
729bool
d732fcf0 730c_common_post_options (const char **pfilename)
428a5a4e 731{
d732fcf0 732 struct cpp_callbacks *cb;
733
79cf3ec1 734 /* Canonicalize the input and output filenames. */
40109983 735 if (in_fnames == NULL)
736 {
9318f22c 737 in_fnames = XNEWVEC (const char *, 1);
40109983 738 in_fnames[0] = "";
739 }
740 else if (strcmp (in_fnames[0], "-") == 0)
741 in_fnames[0] = "";
79cf3ec1 742
6019c708 743 if (out_fname == NULL || !strcmp (out_fname, "-"))
744 out_fname = "";
745
eb0921ba 746 if (cpp_opts->deps.style == DEPS_NONE)
6019c708 747 check_deps_environment_vars ();
748
61191376 749 handle_deferred_opts ();
6019c708 750
61191376 751 sanitize_cpp_opts ();
79cf3ec1 752
72779020 753 register_include_chains (parse_in, sysroot, iprefix, imultilib,
c0f19401 754 std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
e69f4d0e 755
3bf418bd 756#ifdef C_COMMON_OVERRIDE_OPTIONS
757 /* Some machines may reject certain combinations of C
758 language-specific options. */
759 C_COMMON_OVERRIDE_OPTIONS;
760#endif
761
c6418a4e 762 /* Excess precision other than "fast" requires front-end
763 support. */
764 if (c_dialect_cxx ())
765 {
766 if (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
767 && TARGET_FLT_EVAL_METHOD_NON_DEFAULT)
768 sorry ("-fexcess-precision=standard for C++");
769 flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
770 }
771 else if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
772 flag_excess_precision_cmdline = (flag_iso
773 ? EXCESS_PRECISION_STANDARD
774 : EXCESS_PRECISION_FAST);
775
4f8f4cb8 776 /* ISO C restricts floating-point expression contraction to within
777 source-language expressions (-ffp-contract=on, currently an alias
778 for -ffp-contract=off). */
779 if (flag_iso
780 && !c_dialect_cxx ()
781 && (global_options_set.x_flag_fp_contract_mode
95093889 782 == (enum fp_contract_mode) 0)
783 && flag_unsafe_math_optimizations == 0)
4f8f4cb8 784 flag_fp_contract_mode = FP_CONTRACT_OFF;
785
11950bdd 786 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
787 inline semantics are not supported in GNU89 or C89 mode. */
788 if (flag_gnu89_inline == -1)
789 flag_gnu89_inline = !flag_isoc99;
790 else if (!flag_gnu89_inline && !flag_isoc99)
791 error ("-fno-gnu89-inline is only supported in GNU99 or C99 mode");
792
e24e8df7 793 /* Default to ObjC sjlj exception handling if NeXT runtime. */
794 if (flag_objc_sjlj_exceptions < 0)
795 flag_objc_sjlj_exceptions = flag_next_runtime;
796 if (flag_objc_exceptions && !flag_objc_sjlj_exceptions)
797 flag_exceptions = 1;
798
bbb88b31 799 /* If -ffreestanding, -fno-hosted or -fno-builtin then disable
800 pattern recognition. */
801 if (!global_options_set.x_flag_tree_loop_distribute_patterns
802 && flag_no_builtin)
803 flag_tree_loop_distribute_patterns = 0;
804
e4cc057f 805 /* Set C++ standard to C++14 if not specified on the command line. */
806 if (c_dialect_cxx () && cxx_dialect == cxx_unset)
807 set_std_cxx14 (/*ISO*/false);
808
29438999 809 /* -Woverlength-strings is off by default, but is enabled by -Wpedantic.
1d752508 810 It is never enabled in C++, as the minimum limit is not normative
811 in that standard. */
45efa6b9 812 if (c_dialect_cxx ())
1d752508 813 warn_overlength_strings = 0;
814
23efcc02 815 /* Wmain is enabled by default in C++ but not in C. */
816 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
45efa6b9 817 even if -Wall or -Wpedantic was given (warn_main will be 2 if set
818 by -Wall, 1 if set by -Wmain). */
23efcc02 819 if (warn_main == -1)
820 warn_main = (c_dialect_cxx () && flag_hosted) ? 1 : 0;
821 else if (warn_main == 2)
822 warn_main = flag_hosted ? 1 : 0;
823
45efa6b9 824 /* In C, -Wall and -Wc++-compat enable -Wenum-compare; if it has not
825 yet been set, it is disabled by default. In C++, it is enabled
826 by default. */
841fdaa6 827 if (warn_enum_compare == -1)
828 warn_enum_compare = c_dialect_cxx () ? 1 : 0;
829
7a6a48c9 830 /* -Wpacked-bitfield-compat is on by default for the C languages. The
831 warning is issued in stor-layout.c which is not part of the front-end so
832 we need to selectively turn it on here. */
833 if (warn_packed_bitfield_compat == -1)
834 warn_packed_bitfield_compat = 1;
aa509316 835
428a5a4e 836 /* Special format checking options don't work without -Wformat; warn if
837 they are used. */
8b6866af 838 if (!warn_format)
839 {
840 warning (OPT_Wformat_y2k,
841 "-Wformat-y2k ignored without -Wformat");
842 warning (OPT_Wformat_extra_args,
843 "-Wformat-extra-args ignored without -Wformat");
844 warning (OPT_Wformat_zero_length,
845 "-Wformat-zero-length ignored without -Wformat");
846 warning (OPT_Wformat_nonliteral,
847 "-Wformat-nonliteral ignored without -Wformat");
0e9b4e74 848 warning (OPT_Wformat_contains_nul,
849 "-Wformat-contains-nul ignored without -Wformat");
8b6866af 850 warning (OPT_Wformat_security,
851 "-Wformat-security ignored without -Wformat");
8b6866af 852 }
428a5a4e 853
3ba510aa 854 /* -Wimplicit-function-declaration is enabled by default for C99. */
48e1416a 855 if (warn_implicit_function_declaration == -1)
3ba510aa 856 warn_implicit_function_declaration = flag_isoc99;
45b9d334 857
e610d2b2 858 /* -Wimplicit-int is enabled by default for C99. */
859 if (warn_implicit_int == -1)
860 warn_implicit_int = flag_isoc99;
861
8d669e79 862 /* -Wshift-overflow is enabled by default in C99 and C++11 modes. */
863 if (warn_shift_overflow == -1)
864 warn_shift_overflow = cxx_dialect >= cxx11 || flag_isoc99;
865
cdc64059 866 /* -Wshift-negative-value is enabled by -Wextra in C99 and C++11 modes. */
867 if (warn_shift_negative_value == -1)
868 warn_shift_negative_value = (extra_warnings
869 && (cxx_dialect >= cxx11 || flag_isoc99));
870
468088ac 871 /* Declone C++ 'structors if -Os. */
872 if (flag_declone_ctor_dtor == -1)
873 flag_declone_ctor_dtor = optimize_size;
874
229a58b1 875 if (warn_abi_version == -1)
876 {
877 if (flag_abi_compat_version != -1)
878 warn_abi_version = flag_abi_compat_version;
879 else
880 warn_abi_version = 0;
881 }
882
74b777e5 883 if (flag_abi_compat_version == 1)
884 {
885 warning (0, "%<-fabi-compat-version=1%> is not supported, using =2");
886 flag_abi_compat_version = 2;
887 }
888 else if (flag_abi_compat_version == -1)
889 {
229a58b1 890 /* Generate compatibility aliases for ABI v8 (5.1) by default. */
891 flag_abi_compat_version
892 = (flag_abi_version == 0 ? 8 : 0);
74b777e5 893 }
894
1d524ff7 895 /* Change flag_abi_version to be the actual current ABI level for the
896 benefit of c_cpp_builtins. */
897 if (flag_abi_version == 0)
42af8c2a 898 flag_abi_version = 10;
1d524ff7 899
60777f69 900 if (cxx_dialect >= cxx11)
7e783eb3 901 {
902 /* If we're allowing C++0x constructs, don't warn about C++98
903 identifiers which are keywords in C++0x. */
1c5f7aba 904 warn_cxx11_compat = 0;
3c665566 905 cpp_opts->cpp_warn_cxx11_compat = 0;
7e783eb3 906
907 if (warn_narrowing == -1)
908 warn_narrowing = 1;
909 }
910 else if (warn_narrowing == -1)
911 warn_narrowing = 0;
e1db7b20 912
d1856d2c 913 /* Global sized deallocation is new in C++14. */
914 if (flag_sized_deallocation == -1)
915 flag_sized_deallocation = (cxx_dialect >= cxx14);
916
1c4973d7 917 if (flag_extern_tls_init)
918 {
919#if !defined (ASM_OUTPUT_DEF) || !SUPPORTS_WEAK
920 /* Lazy TLS initialization for a variable in another TU requires
921 alias and weak reference support. */
922 if (flag_extern_tls_init > 0)
923 sorry ("external TLS initialization functions not supported "
924 "on this target");
925 flag_extern_tls_init = 0;
926#else
927 flag_extern_tls_init = 1;
928#endif
929 }
930
79cf3ec1 931 if (flag_preprocess_only)
932 {
f7070933 933 /* Open the output now. We must do so even if flag_no_output is
934 on, because there may be other output than from the actual
935 preprocessing (e.g. from -dM). */
936 if (out_fname[0] == '\0')
937 out_stream = stdout;
938 else
939 out_stream = fopen (out_fname, "w");
940
941 if (out_stream == NULL)
e920deaf 942 {
c05be867 943 fatal_error (input_location, "opening output file %s: %m", out_fname);
fcc56321 944 return false;
e920deaf 945 }
f7070933 946
40109983 947 if (num_in_fnames > 1)
948 error ("too many filenames given. Type %s --help for usage",
949 progname);
950
e920deaf 951 init_pp_output (out_stream);
79cf3ec1 952 }
e920deaf 953 else
954 {
955 init_c_lex ();
8d67b0c7 956
957 /* When writing a PCH file, avoid reading some other PCH file,
958 because the default address space slot then can't be used
959 for the output PCH file. */
960 if (pch_file)
19426fe1 961 {
962 c_common_no_more_pch ();
963 /* Only -g0 and -gdwarf* are supported with PCH, for other
964 debug formats we warn here and refuse to load any PCH files. */
965 if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
966 warning (OPT_Wdeprecated,
967 "the \"%s\" debug format cannot be used with "
968 "pre-compiled headers", debug_type_names[write_symbols]);
969 }
970 else if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
8d67b0c7 971 c_common_no_more_pch ();
79cf3ec1 972
e920deaf 973 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
9a6486a6 974 input_location = UNKNOWN_LOCATION;
e920deaf 975 }
f7070933 976
d732fcf0 977 cb = cpp_get_callbacks (parse_in);
978 cb->file_change = cb_file_change;
979 cb->dir_change = cb_dir_change;
c7f96907 980 cpp_post_options (parse_in);
3636964b 981 init_global_opts_from_cpp (&global_options, cpp_get_options (parse_in));
b717e161 982
9a6486a6 983 input_location = UNKNOWN_LOCATION;
03bde601 984
d732fcf0 985 *pfilename = this_input_filename
986 = cpp_read_main_file (parse_in, in_fnames[0]);
75d034d9 987 /* Don't do any compilation or preprocessing if there is no input file. */
d732fcf0 988 if (this_input_filename == NULL)
75d034d9 989 {
990 errorcount++;
991 return false;
992 }
d732fcf0 993
c796cff1 994 if (flag_working_directory
84166705 995 && flag_preprocess_only && !flag_no_line_commands)
d732fcf0 996 pp_dir_change (parse_in, get_src_pwd ());
997
c779d8cc 998 /* Disable LTO output when outputting a precompiled header. */
999 if (pch_file && flag_lto)
1000 {
1001 flag_lto = 0;
1002 flag_generate_lto = 0;
1003 }
1004
03bde601 1005 return flag_preprocess_only;
1006}
1007
1008/* Front end initialization common to C, ObjC and C++. */
1009bool
2b30d46c 1010c_common_init (void)
03bde601 1011{
03bde601 1012 /* Set up preprocessor arithmetic. Must be done after call to
1013 c_common_nodes_and_builtins for type nodes to be good. */
1014 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1015 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1016 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1017 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
78a8ed03 1018 cpp_opts->unsigned_wchar = TYPE_UNSIGNED (wchar_type_node);
ebc03810 1019 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
1020
1021 /* This can't happen until after wchar_precision and bytes_big_endian
1022 are known. */
1023 cpp_init_iconv (parse_in);
79cf3ec1 1024
f1035767 1025 if (version_flag)
3169c57a 1026 {
1027 int i;
1028 fputs ("Compiler executable checksum: ", stderr);
1029 for (i = 0; i < 16; i++)
1030 fprintf (stderr, "%02x", executable_checksum[i]);
1031 putc ('\n', stderr);
1032 }
f1035767 1033
146a4308 1034 /* Has to wait until now so that cpplib has its hash table. */
1035 init_pragma ();
1036
e920deaf 1037 if (flag_preprocess_only)
1038 {
79396169 1039 c_finish_options ();
d732fcf0 1040 preprocess_file (parse_in);
03bde601 1041 return false;
e920deaf 1042 }
1043
03bde601 1044 return true;
6019c708 1045}
1046
40109983 1047/* Initialize the integrated preprocessor after debug output has been
1048 initialized; loop over each input file. */
b717e161 1049void
b8ba44e7 1050c_common_parse_file (void)
b717e161 1051{
b3e83e1e 1052 unsigned int i;
1053
b3e83e1e 1054 i = 0;
1055 for (;;)
1056 {
79396169 1057 c_finish_options ();
8f7fba47 1058 /* Open the dump files to use for the original and class dump output
1059 here, to be used during parsing for the current file. */
1060 original_dump_file = dump_begin (TDI_original, &original_dump_flags);
1061 class_dump_file = dump_begin (TDI_class, &class_dump_flags);
b3e83e1e 1062 pch_init ();
1063 push_file_scope ();
1064 c_parse_file ();
b3e83e1e 1065 pop_file_scope ();
7a4afb3f 1066 /* And end the main input file, if the debug writer wants it */
1067 if (debug_hooks->start_end_main_source_file)
1068 (*debug_hooks->end_source_file) (0);
b3e83e1e 1069 if (++i >= num_in_fnames)
1070 break;
1071 cpp_undef_all (parse_in);
64cb8c90 1072 cpp_clear_file_cache (parse_in);
b3e83e1e 1073 this_input_filename
1074 = cpp_read_main_file (parse_in, in_fnames[i]);
8f7fba47 1075 if (original_dump_file)
1076 {
1077 dump_end (TDI_original, original_dump_file);
1078 original_dump_file = NULL;
1079 }
1080 if (class_dump_file)
1081 {
1082 dump_end (TDI_class, class_dump_file);
1083 class_dump_file = NULL;
1084 }
b3e83e1e 1085 /* If an input file is missing, abandon further compilation.
a0c938f0 1086 cpplib has issued a diagnostic. */
b3e83e1e 1087 if (!this_input_filename)
1088 break;
1089 }
3a1c9df2 1090
1091 c_parse_final_cleanups ();
b717e161 1092}
8f7fba47 1093
1094/* Returns the appropriate dump file for PHASE to dump with FLAGS. */
1095FILE *
1096get_dump_info (int phase, int *flags)
1097{
1098 gcc_assert (phase == TDI_original || phase == TDI_class);
1099 if (phase == TDI_original)
1100 {
1101 *flags = original_dump_flags;
1102 return original_dump_file;
1103 }
1104 else
1105 {
1106 *flags = class_dump_flags;
1107 return class_dump_file;
1108 }
1109}
b717e161 1110
6019c708 1111/* Common finish hook for the C, ObjC and C++ front ends. */
1112void
2b30d46c 1113c_common_finish (void)
6019c708 1114{
1115 FILE *deps_stream = NULL;
1116
7f5f3953 1117 /* Don't write the deps file if there are errors. */
852f689e 1118 if (cpp_opts->deps.style != DEPS_NONE && !seen_error ())
6019c708 1119 {
1120 /* If -M or -MM was seen without -MF, default output to the
1121 output stream. */
61191376 1122 if (!deps_file)
6019c708 1123 deps_stream = out_stream;
1124 else
1125 {
61191376 1126 deps_stream = fopen (deps_file, deps_append ? "a": "w");
6019c708 1127 if (!deps_stream)
c05be867 1128 fatal_error (input_location, "opening dependency file %s: %m",
1129 deps_file);
6019c708 1130 }
1131 }
1132
1133 /* For performance, avoid tearing down cpplib's internal structures
1134 with cpp_destroy (). */
7f5f3953 1135 cpp_finish (parse_in, deps_stream);
6019c708 1136
1137 if (deps_stream && deps_stream != out_stream
1138 && (ferror (deps_stream) || fclose (deps_stream)))
c05be867 1139 fatal_error (input_location, "closing dependency file %s: %m", deps_file);
6019c708 1140
1141 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
c05be867 1142 fatal_error (input_location, "when writing output to %s: %m", out_fname);
6019c708 1143}
1144
6019c708 1145/* Either of two environment variables can specify output of
1146 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1147 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1148 and DEPS_TARGET is the target to mention in the deps. They also
1149 result in dependency information being appended to the output file
3c556f04 1150 rather than overwriting it, and like Sun's compiler
1151 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
6019c708 1152static void
2b30d46c 1153check_deps_environment_vars (void)
6019c708 1154{
1155 char *spec;
1156
967958e4 1157 spec = getenv ("DEPENDENCIES_OUTPUT");
6019c708 1158 if (spec)
61191376 1159 cpp_opts->deps.style = DEPS_USER;
6019c708 1160 else
1161 {
967958e4 1162 spec = getenv ("SUNPRO_DEPENDENCIES");
6019c708 1163 if (spec)
3c556f04 1164 {
1165 cpp_opts->deps.style = DEPS_SYSTEM;
1166 cpp_opts->deps.ignore_main_file = true;
1167 }
6019c708 1168 }
1169
1170 if (spec)
1171 {
1172 /* Find the space before the DEPS_TARGET, if there is one. */
1173 char *s = strchr (spec, ' ');
1174 if (s)
1175 {
1176 /* Let the caller perform MAKE quoting. */
61191376 1177 defer_opt (OPT_MT, s + 1);
6019c708 1178 *s = '\0';
1179 }
1180
1181 /* Command line -MF overrides environment variables and default. */
61191376 1182 if (!deps_file)
1183 deps_file = spec;
6019c708 1184
61191376 1185 deps_append = 1;
33b4f63d 1186 deps_seen = true;
61191376 1187 }
1188}
1189
1190/* Handle deferred command line switches. */
1191static void
2b30d46c 1192handle_deferred_opts (void)
61191376 1193{
1194 size_t i;
3eb3f293 1195 struct deps *deps;
1196
1197 /* Avoid allocating the deps buffer if we don't need it.
1198 (This flag may be true without there having been -MT or -MQ
1199 options, but we'll still need the deps buffer.) */
1200 if (!deps_seen)
1201 return;
1202
1203 deps = cpp_get_deps (parse_in);
61191376 1204
1205 for (i = 0; i < deferred_count; i++)
1206 {
1207 struct deferred_opt *opt = &deferred_opts[i];
1208
fc3c7720 1209 if (opt->code == OPT_MT || opt->code == OPT_MQ)
3eb3f293 1210 deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
61191376 1211 }
61191376 1212}
1213
1214/* These settings are appropriate for GCC, but not necessarily so for
1215 cpplib as a library. */
1216static void
2b30d46c 1217sanitize_cpp_opts (void)
61191376 1218{
1219 /* If we don't know what style of dependencies to output, complain
1220 if any other dependency switches have been given. */
1221 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1222 error ("to generate dependencies you must specify either -M or -MM");
1223
1224 /* -dM and dependencies suppress normal output; do it here so that
1225 the last -d[MDN] switch overrides earlier ones. */
f7070933 1226 if (flag_dump_macros == 'M')
1227 flag_no_output = 1;
61191376 1228
fcde64dc 1229 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1230 to perform proper macro expansion. */
1231 if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros)
1232 flag_dump_macros = 'D';
1233
61191376 1234 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1235 -dM since at least glibc relies on -M -dM to work. */
f7f07c95 1236 /* Also, flag_no_output implies flag_no_line_commands, always. */
f7070933 1237 if (flag_no_output)
61191376 1238 {
f7070933 1239 if (flag_dump_macros != 'M')
1240 flag_dump_macros = 0;
1241 flag_dump_includes = 0;
22119a94 1242 flag_no_line_commands = 1;
6019c708 1243 }
14984eca 1244 else if (cpp_opts->deps.missing_files)
1245 error ("-MG may only be used with -M or -MM");
61191376 1246
1247 cpp_opts->unsigned_char = !flag_signed_char;
1248 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1249
9ab71c6b 1250 /* Wlong-long is disabled by default. It is enabled by:
29438999 1251 [-Wpedantic | -Wtraditional] -std=[gnu|c]++98 ; or
04afd878 1252 [-Wpedantic | -Wtraditional] -std=non-c99
1253
1254 Either -Wlong-long or -Wno-long-long override any other settings.
1255 ??? These conditions should be handled in c.opt. */
1256 if (warn_long_long == -1)
1257 {
1258 warn_long_long = ((pedantic || warn_traditional)
1259 && (c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99));
1260 cpp_opts->cpp_warn_long_long = warn_long_long;
1261 }
e7aa92b2 1262
1263 /* If we're generating preprocessor output, emit current directory
1264 if explicitly requested or if debugging information is enabled.
1265 ??? Maybe we should only do it for debugging formats that
1266 actually output the current directory? */
1267 if (flag_working_directory == -1)
1268 flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
fcde64dc 1269
1270 if (cpp_opts->directives_only)
1271 {
45efa6b9 1272 if (cpp_warn_unused_macros)
fcde64dc 1273 error ("-fdirectives-only is incompatible with -Wunused_macros");
1274 if (cpp_opts->traditional)
1275 error ("-fdirectives-only is incompatible with -traditional");
1276 }
61191376 1277}
1278
e69f4d0e 1279/* Add include path with a prefix at the front of its name. */
1280static void
2b30d46c 1281add_prefixed_path (const char *suffix, size_t chain)
e69f4d0e 1282{
181a72b4 1283 char *path;
e69f4d0e 1284 const char *prefix;
181a72b4 1285 size_t prefix_len, suffix_len;
e69f4d0e 1286
181a72b4 1287 suffix_len = strlen (suffix);
1288 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1289 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1290
9318f22c 1291 path = (char *) xmalloc (prefix_len + suffix_len + 1);
181a72b4 1292 memcpy (path, prefix, prefix_len);
1293 memcpy (path + prefix_len, suffix, suffix_len);
1294 path[prefix_len + suffix_len] = '\0';
1295
06fd18c9 1296 add_path (path, chain, 0, false);
e69f4d0e 1297}
1298
d732fcf0 1299/* Handle -D, -U, -A, -imacros, and the first -include. */
1300static void
79396169 1301c_finish_options (void)
d41b6698 1302{
d41b6698 1303 if (!cpp_opts->preprocessed)
1304 {
fc3c7720 1305 size_t i;
1306
446bdf5f 1307 cb_file_change (parse_in,
551e34da 1308 linemap_check_ordinary (linemap_add (line_table,
1309 LC_RENAME, 0,
1310 _("<built-in>"),
1311 0)));
446bdf5f 1312 /* Make sure all of the builtins about to be declared have
1313 BUILTINS_LOCATION has their source_location. */
1314 source_location builtins_loc = BUILTINS_LOCATION;
1315 cpp_force_token_locations (parse_in, &builtins_loc);
1316
1317 cpp_init_builtins (parse_in, flag_hosted);
1318 c_cpp_builtins (parse_in);
1319
1320 cpp_stop_forcing_token_locations (parse_in);
f0c2775b 1321
1322 /* We're about to send user input to cpplib, so make it warn for
1323 things that we previously (when we sent it internal definitions)
1324 told it to not warn.
1325
1326 C99 permits implementation-defined characters in identifiers.
1327 The documented meaning of -std= is to turn off extensions that
1328 conflict with the specified standard, and since a strictly
1329 conforming program cannot contain a '$', we do not condition
1330 their acceptance on the -std= setting. */
5ae82d58 1331 cpp_opts->warn_dollars = (cpp_opts->cpp_pedantic && !cpp_opts->c99);
f0c2775b 1332
2e1e73b6 1333 cb_file_change (parse_in,
551e34da 1334 linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
1335 _("<command-line>"), 0)));
2e1e73b6 1336
fc3c7720 1337 for (i = 0; i < deferred_count; i++)
1338 {
1339 struct deferred_opt *opt = &deferred_opts[i];
1340
1341 if (opt->code == OPT_D)
1342 cpp_define (parse_in, opt->arg);
1343 else if (opt->code == OPT_U)
1344 cpp_undef (parse_in, opt->arg);
1345 else if (opt->code == OPT_A)
1346 {
1347 if (opt->arg[0] == '-')
1348 cpp_unassert (parse_in, opt->arg + 1);
1349 else
1350 cpp_assert (parse_in, opt->arg);
1351 }
1352 }
d41b6698 1353
dd690367 1354 /* Start the main input file, if the debug writer wants it. */
1355 if (debug_hooks->start_end_main_source_file
1356 && !flag_preprocess_only)
1357 (*debug_hooks->start_source_file) (0, this_input_filename);
1358
fc3c7720 1359 /* Handle -imacros after -D and -U. */
d41b6698 1360 for (i = 0; i < deferred_count; i++)
1361 {
1362 struct deferred_opt *opt = &deferred_opts[i];
1363
1364 if (opt->code == OPT_imacros
1365 && cpp_push_include (parse_in, opt->arg))
5980b792 1366 {
8b332087 1367 /* Disable push_command_line_include callback for now. */
5980b792 1368 include_cursor = deferred_count + 1;
1369 cpp_scan_nooutput (parse_in);
1370 }
d41b6698 1371 }
1372 }
dd690367 1373 else
1374 {
1375 if (cpp_opts->directives_only)
1376 cpp_init_special_builtins (parse_in);
1377
1378 /* Start the main input file, if the debug writer wants it. */
1379 if (debug_hooks->start_end_main_source_file
1380 && !flag_preprocess_only)
1381 (*debug_hooks->start_source_file) (0, this_input_filename);
1382 }
d41b6698 1383
26670692 1384 include_cursor = 0;
d41b6698 1385 push_command_line_include ();
1386}
1387
b717e161 1388/* Give CPP the next file given by -include, if any. */
1389static void
2b30d46c 1390push_command_line_include (void)
b717e161 1391{
9140d56f 1392 /* This can happen if disabled by -imacros for example.
1393 Punt so that we don't set "<command-line>" as the filename for
1394 the header. */
1395 if (include_cursor > deferred_count)
1396 return;
1397
6adc88f8 1398 if (!done_preinclude)
1399 {
1400 done_preinclude = true;
1401 if (flag_hosted && std_inc && !cpp_opts->preprocessed)
1402 {
1403 const char *preinc = targetcm.c_preinclude ();
1404 if (preinc && cpp_push_default_include (parse_in, preinc))
1405 return;
1406 }
1407 }
1408
1409 pch_cpp_save_state ();
1410
b717e161 1411 while (include_cursor < deferred_count)
1412 {
1413 struct deferred_opt *opt = &deferred_opts[include_cursor++];
2b30d46c 1414
84166705 1415 if (!cpp_opts->preprocessed && opt->code == OPT_include
e2df3e75 1416 && cpp_push_include (parse_in, opt->arg))
b717e161 1417 return;
1418 }
1419
1420 if (include_cursor == deferred_count)
1421 {
e2df3e75 1422 include_cursor++;
b717e161 1423 /* -Wunused-macros should only warn about macros defined hereafter. */
45efa6b9 1424 cpp_opts->warn_unused_macros = cpp_warn_unused_macros;
d732fcf0 1425 /* Restore the line map from <command line>. */
84166705 1426 if (!cpp_opts->preprocessed)
860e5532 1427 cpp_change_file (parse_in, LC_RENAME, this_input_filename);
d732fcf0 1428
1429 /* Set this here so the client can change the option if it wishes,
1430 and after stacking the main file so we don't trace the main file. */
931b0a0f 1431 line_table->trace_includes = cpp_opts->print_include_names;
b717e161 1432 }
1433}
1434
1435/* File change callback. Has to handle -include files. */
1436static void
9a03a746 1437cb_file_change (cpp_reader * ARG_UNUSED (pfile),
551e34da 1438 const line_map_ordinary *new_map)
b717e161 1439{
1440 if (flag_preprocess_only)
1441 pp_file_change (new_map);
1442 else
1443 fe_file_change (new_map);
1444
0391a567 1445 if (new_map
1446 && (new_map->reason == LC_ENTER || new_map->reason == LC_RENAME))
1447 {
1448 /* Signal to plugins that a file is included. This could happen
1449 several times with the same file path, e.g. because of
1450 several '#include' or '#line' directives... */
1451 invoke_plugin_callbacks
1452 (PLUGIN_INCLUDE_FILE,
1453 const_cast<char*> (ORDINARY_MAP_FILE_NAME (new_map)));
1454 }
1455
e2df3e75 1456 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
6adc88f8 1457 {
1458 pch_cpp_save_state ();
1459 push_command_line_include ();
1460 }
b717e161 1461}
1462
d732fcf0 1463void
9a03a746 1464cb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir)
d732fcf0 1465{
84166705 1466 if (!set_src_pwd (dir))
c3ceba8e 1467 warning (0, "too late for # directive to set debug directory");
d732fcf0 1468}
1469
61191376 1470/* Set the C 89 standard (with 1994 amendments if C94, without GNU
1471 extensions if ISO). There is no concept of gnu94. */
1472static void
2b30d46c 1473set_std_c89 (int c94, int iso)
61191376 1474{
1475 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1476 flag_iso = iso;
1477 flag_no_asm = iso;
1478 flag_no_gnu_keywords = iso;
1479 flag_no_nonansi_builtin = iso;
61191376 1480 flag_isoc94 = c94;
1481 flag_isoc99 = 0;
32074525 1482 flag_isoc11 = 0;
3a4a2292 1483 lang_hooks.name = "GNU C89";
6019c708 1484}
1485
dfeeca46 1486/* Set the C 99 standard (without GNU extensions if ISO). */
1487static void
2b30d46c 1488set_std_c99 (int iso)
dfeeca46 1489{
1490 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1491 flag_no_asm = iso;
1492 flag_no_nonansi_builtin = iso;
dfeeca46 1493 flag_iso = iso;
32074525 1494 flag_isoc11 = 0;
39012afb 1495 flag_isoc99 = 1;
1496 flag_isoc94 = 1;
3a4a2292 1497 lang_hooks.name = "GNU C99";
39012afb 1498}
1499
32074525 1500/* Set the C 11 standard (without GNU extensions if ISO). */
39012afb 1501static void
32074525 1502set_std_c11 (int iso)
39012afb 1503{
32074525 1504 cpp_set_lang (parse_in, iso ? CLK_STDC11: CLK_GNUC11);
39012afb 1505 flag_no_asm = iso;
1506 flag_no_nonansi_builtin = iso;
1507 flag_iso = iso;
32074525 1508 flag_isoc11 = 1;
dfeeca46 1509 flag_isoc99 = 1;
1510 flag_isoc94 = 1;
3a4a2292 1511 lang_hooks.name = "GNU C11";
dfeeca46 1512}
1513
3ae4c9f3 1514/* Set the C++ 98 standard (without GNU extensions if ISO). */
1515static void
2b30d46c 1516set_std_cxx98 (int iso)
3ae4c9f3 1517{
1518 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1519 flag_no_gnu_keywords = iso;
1520 flag_no_nonansi_builtin = iso;
3ae4c9f3 1521 flag_iso = iso;
6dcdb5de 1522 cxx_dialect = cxx98;
3a4a2292 1523 lang_hooks.name = "GNU C++98";
3ae4c9f3 1524}
1525
0d84dc2d 1526/* Set the C++ 2011 standard (without GNU extensions if ISO). */
0fe6eeac 1527static void
0d84dc2d 1528set_std_cxx11 (int iso)
0fe6eeac 1529{
0d84dc2d 1530 cpp_set_lang (parse_in, iso ? CLK_CXX11: CLK_GNUCXX11);
0fe6eeac 1531 flag_no_gnu_keywords = iso;
1532 flag_no_nonansi_builtin = iso;
1533 flag_iso = iso;
0d84dc2d 1534 /* C++11 includes the C99 standard library. */
dc251364 1535 flag_isoc94 = 1;
1536 flag_isoc99 = 1;
0d84dc2d 1537 cxx_dialect = cxx11;
3a4a2292 1538 lang_hooks.name = "GNU C++11";
0fe6eeac 1539}
1540
c35e53f1 1541/* Set the C++ 2014 draft standard (without GNU extensions if ISO). */
543efdbe 1542static void
4e454776 1543set_std_cxx14 (int iso)
543efdbe 1544{
4e454776 1545 cpp_set_lang (parse_in, iso ? CLK_CXX14: CLK_GNUCXX14);
543efdbe 1546 flag_no_gnu_keywords = iso;
1547 flag_no_nonansi_builtin = iso;
1548 flag_iso = iso;
1549 /* C++11 includes the C99 standard library. */
1550 flag_isoc94 = 1;
1551 flag_isoc99 = 1;
4e454776 1552 cxx_dialect = cxx14;
3a4a2292 1553 lang_hooks.name = "GNU C++14";
543efdbe 1554}
1555
4b53bc0f 1556/* Set the C++ 201z draft standard (without GNU extensions if ISO). */
1557static void
1558set_std_cxx1z (int iso)
1559{
4e454776 1560 cpp_set_lang (parse_in, iso ? CLK_CXX1Z: CLK_GNUCXX1Z);
4b53bc0f 1561 flag_no_gnu_keywords = iso;
1562 flag_no_nonansi_builtin = iso;
1563 flag_iso = iso;
1564 /* C++11 includes the C99 standard library. */
1565 flag_isoc94 = 1;
1566 flag_isoc99 = 1;
56c12fd4 1567 /* Enable concepts by default. */
1568 flag_concepts = 1;
4e454776 1569 flag_isoc11 = 1;
4b53bc0f 1570 cxx_dialect = cxx1z;
3a4a2292 1571 lang_hooks.name = "GNU C++14"; /* Pretend C++14 till standarization. */
4b53bc0f 1572}
1573
45c365b1 1574/* Args to -d specify what to dump. Silently ignore
a2f10574 1575 unrecognized options; they may be aimed at toplev.c. */
428a5a4e 1576static void
2b30d46c 1577handle_OPT_d (const char *arg)
428a5a4e 1578{
45c365b1 1579 char c;
1580
1581 while ((c = *arg++) != '\0')
1582 switch (c)
1583 {
f7070933 1584 case 'M': /* Dump macros only. */
1585 case 'N': /* Dump names. */
1586 case 'D': /* Dump definitions. */
34c3de48 1587 case 'U': /* Dump used macros. */
f7070933 1588 flag_dump_macros = c;
45c365b1 1589 break;
1590
1591 case 'I':
f7070933 1592 flag_dump_includes = 1;
45c365b1 1593 break;
1594 }
428a5a4e 1595}