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