c-opts.c revision 259890
1117395Skan/* C/ObjC/C++ command line option handling. 2169699Skan Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 3169699Skan Free Software Foundation, Inc. 4117395Skan Contributed by Neil Booth. 5117395Skan 6117395SkanThis file is part of GCC. 7117395Skan 8117395SkanGCC is free software; you can redistribute it and/or modify it under 9117395Skanthe terms of the GNU General Public License as published by the Free 10117395SkanSoftware Foundation; either version 2, or (at your option) any later 11117395Skanversion. 12117395Skan 13117395SkanGCC is distributed in the hope that it will be useful, but WITHOUT ANY 14117395SkanWARRANTY; without even the implied warranty of MERCHANTABILITY or 15117395SkanFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16117395Skanfor more details. 17117395Skan 18117395SkanYou should have received a copy of the GNU General Public License 19117395Skanalong with GCC; see the file COPYING. If not, write to the Free 20169699SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 21169699Skan02110-1301, USA. */ 22117395Skan 23132811Skan/* $FreeBSD: stable/10/contrib/gcc/c-opts.c 259890 2013-12-25 21:59:56Z pfg $ */ 24189824Sdas/* Merged C99 inline changes from gcc trunk 122565 2007-03-05 */ 25132811Skan 26117395Skan#include "config.h" 27117395Skan#include "system.h" 28132718Skan#include "coretypes.h" 29132718Skan#include "tm.h" 30117395Skan#include "tree.h" 31117395Skan#include "c-common.h" 32117395Skan#include "c-pragma.h" 33117395Skan#include "flags.h" 34117395Skan#include "toplev.h" 35117395Skan#include "langhooks.h" 36117395Skan#include "tree-inline.h" 37117395Skan#include "diagnostic.h" 38117395Skan#include "intl.h" 39132718Skan#include "cppdefault.h" 40132718Skan#include "c-incpath.h" 41132718Skan#include "debug.h" /* For debug_hooks. */ 42132718Skan#include "opts.h" 43132718Skan#include "options.h" 44169699Skan#include "mkdeps.h" 45117395Skan 46132718Skan#ifndef DOLLARS_IN_IDENTIFIERS 47132718Skan# define DOLLARS_IN_IDENTIFIERS true 48132718Skan#endif 49132718Skan 50132718Skan#ifndef TARGET_SYSTEM_ROOT 51132718Skan# define TARGET_SYSTEM_ROOT NULL 52132718Skan#endif 53132718Skan 54169699Skan#ifndef TARGET_OPTF 55169699Skan#define TARGET_OPTF(ARG) 56169699Skan#endif 57132718Skan 58117395Skan/* CPP's options. */ 59117395Skanstatic cpp_options *cpp_opts; 60117395Skan 61117395Skan/* Input filename. */ 62132718Skanstatic const char *this_input_filename; 63117395Skan 64117395Skan/* Filename and stream for preprocessed output. */ 65117395Skanstatic const char *out_fname; 66117395Skanstatic FILE *out_stream; 67117395Skan 68117395Skan/* Append dependencies to deps_file. */ 69117395Skanstatic bool deps_append; 70117395Skan 71117395Skan/* If dependency switches (-MF etc.) have been given. */ 72117395Skanstatic bool deps_seen; 73117395Skan 74132718Skan/* If -v seen. */ 75132718Skanstatic bool verbose; 76132718Skan 77169699Skan/* If -lang-fortran seen. */ 78169699Skanstatic bool lang_fortran = false; 79169699Skan 80117395Skan/* Dependency output file. */ 81117395Skanstatic const char *deps_file; 82117395Skan 83132718Skan/* The prefix given by -iprefix, if any. */ 84132718Skanstatic const char *iprefix; 85117395Skan 86169699Skan/* The multilib directory given by -imultilib, if any. */ 87169699Skanstatic const char *imultilib; 88169699Skan 89132718Skan/* The system root, if any. Overridden by -isysroot. */ 90132718Skanstatic const char *sysroot = TARGET_SYSTEM_ROOT; 91117395Skan 92132718Skan/* Zero disables all standard directories for headers. */ 93132718Skanstatic bool std_inc = true; 94117395Skan 95132718Skan/* Zero disables the C++-specific standard directories for headers. */ 96132718Skanstatic bool std_cxx_inc = true; 97117395Skan 98132718Skan/* If the quote chain has been split by -I-. */ 99132718Skanstatic bool quote_chain_split; 100117395Skan 101132718Skan/* If -Wunused-macros. */ 102132718Skanstatic bool warn_unused_macros; 103117395Skan 104169699Skan/* If -Wvariadic-macros. */ 105169699Skanstatic bool warn_variadic_macros = true; 106169699Skan 107132718Skan/* Number of deferred options. */ 108132718Skanstatic size_t deferred_count; 109117395Skan 110132718Skan/* Number of deferred options scanned for -include. */ 111132718Skanstatic size_t include_cursor; 112117395Skan 113132718Skanstatic void set_Wimplicit (int); 114132718Skanstatic void handle_OPT_d (const char *); 115132718Skanstatic void set_std_cxx98 (int); 116132718Skanstatic void set_std_c89 (int, int); 117132718Skanstatic void set_std_c99 (int); 118132718Skanstatic void check_deps_environment_vars (void); 119132718Skanstatic void handle_deferred_opts (void); 120132718Skanstatic void sanitize_cpp_opts (void); 121132718Skanstatic void add_prefixed_path (const char *, size_t); 122132718Skanstatic void push_command_line_include (void); 123132718Skanstatic void cb_file_change (cpp_reader *, const struct line_map *); 124132718Skanstatic void cb_dir_change (cpp_reader *, const char *); 125132718Skanstatic void finish_options (void); 126117395Skan 127132718Skan#ifndef STDC_0_IN_SYSTEM_HEADERS 128132718Skan#define STDC_0_IN_SYSTEM_HEADERS 0 129117395Skan#endif 130117395Skan 131132718Skan/* Holds switches parsed by c_common_handle_option (), but whose 132132718Skan handling is deferred to c_common_post_options (). */ 133132718Skanstatic void defer_opt (enum opt_code, const char *); 134117395Skanstatic struct deferred_opt 135117395Skan{ 136117395Skan enum opt_code code; 137117395Skan const char *arg; 138117395Skan} *deferred_opts; 139117395Skan 140132718Skan/* Complain that switch CODE expects an argument but none was 141132718Skan provided. OPT was the command-line option. Return FALSE to get 142132718Skan the default message in opts.c, TRUE if we provide a specialized 143132718Skan one. */ 144132718Skanbool 145132718Skanc_common_missing_argument (const char *opt, size_t code) 146117395Skan{ 147132718Skan switch (code) 148117395Skan { 149117395Skan default: 150132718Skan /* Pick up the default message. */ 151132718Skan return false; 152132718Skan 153132718Skan case OPT_fconstant_string_class_: 154169699Skan error ("no class name specified with %qs", opt); 155117395Skan break; 156117395Skan 157132718Skan case OPT_A: 158169699Skan error ("assertion missing after %qs", opt); 159117395Skan break; 160117395Skan 161132718Skan case OPT_D: 162132718Skan case OPT_U: 163169699Skan error ("macro name missing after %qs", opt); 164132718Skan break; 165132718Skan 166169699Skan case OPT_F: 167132718Skan case OPT_I: 168132718Skan case OPT_idirafter: 169132718Skan case OPT_isysroot: 170132718Skan case OPT_isystem: 171169699Skan case OPT_iquote: 172169699Skan error ("missing path after %qs", opt); 173132718Skan break; 174132718Skan 175117395Skan case OPT_MF: 176117395Skan case OPT_MD: 177117395Skan case OPT_MMD: 178132718Skan case OPT_include: 179132718Skan case OPT_imacros: 180117395Skan case OPT_o: 181169699Skan error ("missing filename after %qs", opt); 182117395Skan break; 183117395Skan 184117395Skan case OPT_MQ: 185117395Skan case OPT_MT: 186169699Skan error ("missing makefile target after %qs", opt); 187117395Skan break; 188117395Skan } 189117395Skan 190132718Skan return true; 191117395Skan} 192117395Skan 193117395Skan/* Defer option CODE with argument ARG. */ 194117395Skanstatic void 195132718Skandefer_opt (enum opt_code code, const char *arg) 196117395Skan{ 197117395Skan deferred_opts[deferred_count].code = code; 198117395Skan deferred_opts[deferred_count].arg = arg; 199117395Skan deferred_count++; 200117395Skan} 201117395Skan 202117395Skan/* Common initialization before parsing options. */ 203132718Skanunsigned int 204146908Skanc_common_init_options (unsigned int argc, const char **argv) 205117395Skan{ 206132718Skan static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX}; 207146908Skan unsigned int i, result; 208117395Skan 209132718Skan /* This is conditionalized only because that is the way the front 210132718Skan ends used to do it. Maybe this should be unconditional? */ 211132718Skan if (c_dialect_cxx ()) 212132718Skan { 213132718Skan /* By default wrap lines at 80 characters. Is getenv 214132718Skan ("COLUMNS") preferable? */ 215132718Skan diagnostic_line_cutoff (global_dc) = 80; 216132718Skan /* By default, emit location information once for every 217132718Skan diagnostic message. */ 218132718Skan diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE; 219132718Skan } 220117395Skan 221132718Skan parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89, 222169699Skan ident_hash, &line_table); 223132718Skan 224117395Skan cpp_opts = cpp_get_options (parse_in); 225132718Skan cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS; 226132718Skan cpp_opts->objc = c_dialect_objc (); 227117395Skan 228132718Skan /* Reset to avoid warnings on internal definitions. We set it just 229132718Skan before passing on command-line options to cpplib. */ 230132718Skan cpp_opts->warn_dollars = 0; 231117395Skan 232132718Skan flag_exceptions = c_dialect_cxx (); 233132718Skan warn_pointer_arith = c_dialect_cxx (); 234169699Skan warn_write_strings = c_dialect_cxx(); 235117395Skan 236169699Skan deferred_opts = XNEWVEC (struct deferred_opt, argc); 237117395Skan 238132718Skan result = lang_flags[c_language]; 239117395Skan 240146908Skan if (c_language == clk_c) 241146908Skan { 242169699Skan /* If preprocessing assembly language, accept any of the C-family 243169699Skan front end options since the driver may pass them through. */ 244146908Skan for (i = 1; i < argc; i++) 245169699Skan if (! strcmp (argv[i], "-lang-asm")) 246169699Skan { 247169699Skan result |= CL_C | CL_ObjC | CL_CXX | CL_ObjCXX; 248169699Skan break; 249169699Skan } 250169699Skan 251169699Skan#ifdef CL_Fortran 252169699Skan for (i = 1; i < argc; i++) 253169699Skan if (! strcmp (argv[i], "-lang-fortran")) 254146908Skan { 255169699Skan result |= CL_Fortran; 256169699Skan break; 257169699Skan } 258146908Skan#endif 259117395Skan } 260169699Skan 261132718Skan return result; 262132718Skan} 263117395Skan 264132718Skan/* Handle switch SCODE with argument ARG. VALUE is true, unless no- 265132718Skan form of an -f or -W option was given. Returns 0 if the switch was 266132718Skan invalid, a negative number to prevent language-independent 267132718Skan processing in toplev.c (a hack necessary for the short-term). */ 268132718Skanint 269132718Skanc_common_handle_option (size_t scode, const char *arg, int value) 270132718Skan{ 271132718Skan const struct cl_option *option = &cl_options[scode]; 272132718Skan enum opt_code code = (enum opt_code) scode; 273132718Skan int result = 1; 274117395Skan 275169699Skan /* Prevent resetting the language standard to a C dialect when the driver 276169699Skan has already determined that we're looking at assembler input. */ 277169699Skan bool preprocessing_asm_p = (cpp_get_options (parse_in)->lang == CLK_ASM); 278169699Skan 279132718Skan switch (code) 280117395Skan { 281132718Skan default: 282169699Skan if (cl_options[code].flags & (CL_C | CL_CXX | CL_ObjC | CL_ObjCXX)) 283169699Skan break; 284169699Skan#ifdef CL_Fortran 285169699Skan if (lang_fortran && (cl_options[code].flags & (CL_Fortran))) 286169699Skan break; 287169699Skan#endif 288169699Skan result = 0; 289132718Skan break; 290117395Skan 291132718Skan case OPT__output_pch_: 292132718Skan pch_file = arg; 293117395Skan break; 294117395Skan 295132718Skan case OPT_A: 296132718Skan defer_opt (code, arg); 297117395Skan break; 298117395Skan 299117395Skan case OPT_C: 300117395Skan cpp_opts->discard_comments = 0; 301117395Skan break; 302117395Skan 303117395Skan case OPT_CC: 304117395Skan cpp_opts->discard_comments = 0; 305117395Skan cpp_opts->discard_comments_in_macro_exp = 0; 306117395Skan break; 307117395Skan 308132718Skan case OPT_D: 309132718Skan defer_opt (code, arg); 310132718Skan break; 311132718Skan 312117395Skan case OPT_E: 313117395Skan flag_preprocess_only = 1; 314117395Skan break; 315117395Skan 316117395Skan case OPT_H: 317117395Skan cpp_opts->print_include_names = 1; 318117395Skan break; 319117395Skan 320169699Skan case OPT_F: 321169699Skan TARGET_OPTF (xstrdup (arg)); 322169699Skan break; 323169699Skan 324132718Skan case OPT_I: 325132718Skan if (strcmp (arg, "-")) 326169699Skan add_path (xstrdup (arg), BRACKET, 0, true); 327132718Skan else 328132718Skan { 329132718Skan if (quote_chain_split) 330132718Skan error ("-I- specified twice"); 331132718Skan quote_chain_split = true; 332132718Skan split_quote_chain (); 333169699Skan inform ("obsolete option -I- used, please use -iquote instead"); 334132718Skan } 335132718Skan break; 336132718Skan 337117395Skan case OPT_M: 338117395Skan case OPT_MM: 339117395Skan /* When doing dependencies with -M or -MM, suppress normal 340117395Skan preprocessed output, but still do -dM etc. as software 341117395Skan depends on this. Preprocessed output does occur if -MD, -MMD 342117395Skan or environment var dependency generation is used. */ 343117395Skan cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER); 344132718Skan flag_no_output = 1; 345117395Skan cpp_opts->inhibit_warnings = 1; 346117395Skan break; 347117395Skan 348117395Skan case OPT_MD: 349117395Skan case OPT_MMD: 350117395Skan cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER); 351117395Skan deps_file = arg; 352117395Skan break; 353117395Skan 354117395Skan case OPT_MF: 355117395Skan deps_seen = true; 356117395Skan deps_file = arg; 357117395Skan break; 358117395Skan 359117395Skan case OPT_MG: 360117395Skan deps_seen = true; 361117395Skan cpp_opts->deps.missing_files = true; 362117395Skan break; 363117395Skan 364117395Skan case OPT_MP: 365117395Skan deps_seen = true; 366117395Skan cpp_opts->deps.phony_targets = true; 367117395Skan break; 368117395Skan 369117395Skan case OPT_MQ: 370117395Skan case OPT_MT: 371117395Skan deps_seen = true; 372117395Skan defer_opt (code, arg); 373117395Skan break; 374117395Skan 375117395Skan case OPT_P: 376132718Skan flag_no_line_commands = 1; 377117395Skan break; 378117395Skan 379132718Skan case OPT_fworking_directory: 380132718Skan flag_working_directory = value; 381132718Skan break; 382132718Skan 383132718Skan case OPT_U: 384132718Skan defer_opt (code, arg); 385132718Skan break; 386132718Skan 387117395Skan case OPT_Wall: 388132718Skan set_Wunused (value); 389132718Skan set_Wformat (value); 390132718Skan set_Wimplicit (value); 391132718Skan warn_char_subscripts = value; 392132718Skan warn_missing_braces = value; 393132718Skan warn_parentheses = value; 394132718Skan warn_return_type = value; 395132718Skan warn_sequence_point = value; /* Was C only. */ 396132718Skan if (c_dialect_cxx ()) 397132718Skan warn_sign_compare = value; 398132718Skan warn_switch = value; 399259405Spfg set_warn_strict_aliasing (value); 400169699Skan warn_strict_overflow = value; 401169699Skan warn_address = value; 402132718Skan 403117395Skan /* Only warn about unknown pragmas that are not in system 404132718Skan headers. */ 405132718Skan warn_unknown_pragmas = value; 406117395Skan 407117395Skan /* We save the value of warn_uninitialized, since if they put 408117395Skan -Wuninitialized on the command line, we need to generate a 409117395Skan warning about not using it without also specifying -O. */ 410117395Skan if (warn_uninitialized != 1) 411132718Skan warn_uninitialized = (value ? 2 : 0); 412117395Skan 413132718Skan if (!c_dialect_cxx ()) 414117395Skan /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding 415117395Skan can turn it off only if it's not explicit. */ 416132718Skan warn_main = value * 2; 417117395Skan else 418117395Skan { 419117395Skan /* C++-specific warnings. */ 420132718Skan warn_reorder = value; 421132718Skan warn_nontemplate_friend = value; 422117395Skan } 423117395Skan 424132718Skan cpp_opts->warn_trigraphs = value; 425132718Skan cpp_opts->warn_comments = value; 426132718Skan cpp_opts->warn_num_sign_change = value; 427132718Skan cpp_opts->warn_multichar = value; /* Was C++ only. */ 428117395Skan 429169699Skan if (warn_pointer_sign == -1) 430169699Skan warn_pointer_sign = 1; 431117395Skan break; 432117395Skan 433117395Skan case OPT_Wcomment: 434117395Skan case OPT_Wcomments: 435132718Skan cpp_opts->warn_comments = value; 436117395Skan break; 437117395Skan 438117395Skan case OPT_Wdeprecated: 439132718Skan cpp_opts->warn_deprecated = value; 440117395Skan break; 441117395Skan 442117395Skan case OPT_Wendif_labels: 443132718Skan cpp_opts->warn_endif_labels = value; 444117395Skan break; 445117395Skan 446117395Skan case OPT_Werror: 447132718Skan cpp_opts->warnings_are_errors = value; 448169699Skan global_dc->warning_as_error_requested = value; 449117395Skan break; 450117395Skan 451132718Skan case OPT_Werror_implicit_function_declaration: 452132718Skan mesg_implicit_function_declaration = 2; 453117395Skan break; 454117395Skan 455117395Skan case OPT_Wformat: 456132718Skan set_Wformat (value); 457117395Skan break; 458117395Skan 459132718Skan case OPT_Wformat_: 460117395Skan set_Wformat (atoi (arg)); 461117395Skan break; 462117395Skan 463117395Skan case OPT_Wimplicit: 464132718Skan set_Wimplicit (value); 465117395Skan break; 466117395Skan 467117395Skan case OPT_Wimport: 468132718Skan /* Silently ignore for now. */ 469117395Skan break; 470117395Skan 471132718Skan case OPT_Winvalid_pch: 472132718Skan cpp_opts->warn_invalid_pch = value; 473132718Skan break; 474132718Skan 475117395Skan case OPT_Wmain: 476132718Skan if (value) 477117395Skan warn_main = 1; 478117395Skan else 479117395Skan warn_main = -1; 480117395Skan break; 481117395Skan 482169699Skan case OPT_Wmissing_include_dirs: 483169699Skan cpp_opts->warn_missing_include_dirs = value; 484117395Skan break; 485117395Skan 486117395Skan case OPT_Wmultichar: 487132718Skan cpp_opts->warn_multichar = value; 488117395Skan break; 489117395Skan 490259890Spfg /* APPLE LOCAL begin -Wnewline-eof */ 491259890Spfg case OPT_Wnewline_eof: 492259890Spfg cpp_opts->warn_newline_at_eof = value; 493259890Spfg break; 494259890Spfg /* APPLE LOCAL end -Wnewline-eof */ 495259890Spfg 496169699Skan case OPT_Wnormalized_: 497169699Skan if (!value || (arg && strcasecmp (arg, "none") == 0)) 498169699Skan cpp_opts->warn_normalize = normalized_none; 499169699Skan else if (!arg || strcasecmp (arg, "nfkc") == 0) 500169699Skan cpp_opts->warn_normalize = normalized_KC; 501169699Skan else if (strcasecmp (arg, "id") == 0) 502169699Skan cpp_opts->warn_normalize = normalized_identifier_C; 503169699Skan else if (strcasecmp (arg, "nfc") == 0) 504169699Skan cpp_opts->warn_normalize = normalized_C; 505169699Skan else 506169699Skan error ("argument %qs to %<-Wnormalized%> not recognized", arg); 507117395Skan break; 508117395Skan 509117395Skan case OPT_Wreturn_type: 510132718Skan warn_return_type = value; 511117395Skan break; 512117395Skan 513169699Skan case OPT_Wstrict_null_sentinel: 514169699Skan warn_strict_null_sentinel = value; 515117395Skan break; 516117395Skan 517117395Skan case OPT_Wsystem_headers: 518132718Skan cpp_opts->warn_system_headers = value; 519117395Skan break; 520117395Skan 521117395Skan case OPT_Wtraditional: 522132718Skan cpp_opts->warn_traditional = value; 523117395Skan break; 524117395Skan 525117395Skan case OPT_Wtrigraphs: 526132718Skan cpp_opts->warn_trigraphs = value; 527117395Skan break; 528117395Skan 529117395Skan case OPT_Wundef: 530132718Skan cpp_opts->warn_undef = value; 531117395Skan break; 532117395Skan 533117395Skan case OPT_Wunknown_pragmas: 534117395Skan /* Set to greater than 1, so that even unknown pragmas in 535132718Skan system headers will be warned about. */ 536132718Skan warn_unknown_pragmas = value * 2; 537117395Skan break; 538117395Skan 539117395Skan case OPT_Wunused_macros: 540132718Skan warn_unused_macros = value; 541117395Skan break; 542117395Skan 543169699Skan case OPT_Wvariadic_macros: 544169699Skan warn_variadic_macros = value; 545169699Skan break; 546169699Skan 547117395Skan case OPT_Wwrite_strings: 548169699Skan warn_write_strings = value; 549117395Skan break; 550132718Skan 551169699Skan case OPT_Weffc__: 552169699Skan warn_ecpp = value; 553169699Skan if (value) 554169699Skan warn_nonvdtor = true; 555169699Skan break; 556169699Skan 557117395Skan case OPT_ansi: 558132718Skan if (!c_dialect_cxx ()) 559117395Skan set_std_c89 (false, true); 560117395Skan else 561117395Skan set_std_cxx98 (true); 562117395Skan break; 563117395Skan 564117395Skan case OPT_d: 565117395Skan handle_OPT_d (arg); 566117395Skan break; 567117395Skan 568117395Skan case OPT_fcond_mismatch: 569132718Skan if (!c_dialect_cxx ()) 570117395Skan { 571132718Skan flag_cond_mismatch = value; 572117395Skan break; 573117395Skan } 574117395Skan /* Fall through. */ 575117395Skan 576117395Skan case OPT_fall_virtual: 577132718Skan case OPT_falt_external_templates: 578117395Skan case OPT_fenum_int_equiv: 579132718Skan case OPT_fexternal_templates: 580117395Skan case OPT_fguiding_decls: 581117395Skan case OPT_fhonor_std: 582117395Skan case OPT_fhuge_objects: 583117395Skan case OPT_flabels_ok: 584132718Skan case OPT_fname_mangling_version_: 585117395Skan case OPT_fnew_abi: 586117395Skan case OPT_fnonnull_objects: 587117395Skan case OPT_fsquangle: 588117395Skan case OPT_fstrict_prototype: 589117395Skan case OPT_fthis_is_variable: 590117395Skan case OPT_fvtable_thunks: 591117395Skan case OPT_fxref: 592132718Skan case OPT_fvtable_gc: 593169699Skan warning (0, "switch %qs is no longer supported", option->opt_text); 594117395Skan break; 595117395Skan 596117395Skan case OPT_faccess_control: 597132718Skan flag_access_control = value; 598117395Skan break; 599117395Skan 600117395Skan case OPT_fasm: 601132718Skan flag_no_asm = !value; 602117395Skan break; 603117395Skan 604117395Skan case OPT_fbuiltin: 605132718Skan flag_no_builtin = !value; 606117395Skan break; 607117395Skan 608117395Skan case OPT_fbuiltin_: 609132718Skan if (value) 610117395Skan result = 0; 611117395Skan else 612117395Skan disable_builtin_function (arg); 613117395Skan break; 614117395Skan 615259405Spfg case OPT_fdirectives_only: 616259405Spfg cpp_opts->directives_only = 1; 617259405Spfg break; 618259405Spfg 619117395Skan case OPT_fdollars_in_identifiers: 620132718Skan cpp_opts->dollars_in_ident = value; 621117395Skan break; 622117395Skan 623117395Skan case OPT_ffreestanding: 624132718Skan value = !value; 625132718Skan /* Fall through.... */ 626117395Skan case OPT_fhosted: 627132718Skan flag_hosted = value; 628132718Skan flag_no_builtin = !value; 629117395Skan /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */ 630132718Skan if (!value && warn_main == 2) 631117395Skan warn_main = 0; 632117395Skan break; 633117395Skan 634117395Skan case OPT_fshort_double: 635132718Skan flag_short_double = value; 636117395Skan break; 637117395Skan 638117395Skan case OPT_fshort_enums: 639132718Skan flag_short_enums = value; 640117395Skan break; 641117395Skan 642117395Skan case OPT_fshort_wchar: 643132718Skan flag_short_wchar = value; 644117395Skan break; 645117395Skan 646117395Skan case OPT_fsigned_bitfields: 647132718Skan flag_signed_bitfields = value; 648117395Skan break; 649117395Skan 650117395Skan case OPT_fsigned_char: 651132718Skan flag_signed_char = value; 652117395Skan break; 653117395Skan 654117395Skan case OPT_funsigned_bitfields: 655132718Skan flag_signed_bitfields = !value; 656117395Skan break; 657117395Skan 658117395Skan case OPT_funsigned_char: 659132718Skan flag_signed_char = !value; 660117395Skan break; 661117395Skan 662117395Skan case OPT_fcheck_new: 663132718Skan flag_check_new = value; 664117395Skan break; 665117395Skan 666117395Skan case OPT_fconserve_space: 667132718Skan flag_conserve_space = value; 668117395Skan break; 669117395Skan 670132718Skan case OPT_fconstant_string_class_: 671117395Skan constant_string_class_name = arg; 672117395Skan break; 673117395Skan 674117395Skan case OPT_fdefault_inline: 675132718Skan flag_default_inline = value; 676117395Skan break; 677117395Skan 678117395Skan case OPT_felide_constructors: 679132718Skan flag_elide_constructors = value; 680117395Skan break; 681117395Skan 682117395Skan case OPT_fenforce_eh_specs: 683132718Skan flag_enforce_eh_specs = value; 684117395Skan break; 685117395Skan 686169699Skan case OPT_fextended_identifiers: 687169699Skan cpp_opts->extended_identifiers = value; 688117395Skan break; 689117395Skan 690117395Skan case OPT_ffor_scope: 691132718Skan flag_new_for_scope = value; 692117395Skan break; 693117395Skan 694117395Skan case OPT_fgnu_keywords: 695132718Skan flag_no_gnu_keywords = !value; 696117395Skan break; 697117395Skan 698117395Skan case OPT_fgnu_runtime: 699132718Skan flag_next_runtime = !value; 700117395Skan break; 701117395Skan 702117395Skan case OPT_fhandle_exceptions: 703169699Skan warning (0, "-fhandle-exceptions has been renamed -fexceptions (and is now on by default)"); 704132718Skan flag_exceptions = value; 705117395Skan break; 706117395Skan 707117395Skan case OPT_fimplement_inlines: 708132718Skan flag_implement_inlines = value; 709117395Skan break; 710117395Skan 711117395Skan case OPT_fimplicit_inline_templates: 712132718Skan flag_implicit_inline_templates = value; 713117395Skan break; 714117395Skan 715117395Skan case OPT_fimplicit_templates: 716132718Skan flag_implicit_templates = value; 717117395Skan break; 718117395Skan 719117395Skan case OPT_fms_extensions: 720132718Skan flag_ms_extensions = value; 721117395Skan break; 722117395Skan 723117395Skan case OPT_fnext_runtime: 724132718Skan flag_next_runtime = value; 725117395Skan break; 726117395Skan 727132718Skan case OPT_fnil_receivers: 728132718Skan flag_nil_receivers = value; 729132718Skan break; 730132718Skan 731117395Skan case OPT_fnonansi_builtins: 732132718Skan flag_no_nonansi_builtin = !value; 733117395Skan break; 734117395Skan 735117395Skan case OPT_foperator_names: 736132718Skan cpp_opts->operator_names = value; 737117395Skan break; 738117395Skan 739117395Skan case OPT_foptional_diags: 740132718Skan flag_optional_diags = value; 741117395Skan break; 742117395Skan 743132718Skan case OPT_fpch_deps: 744132718Skan cpp_opts->restore_pch_deps = value; 745132718Skan break; 746132718Skan 747169699Skan case OPT_fpch_preprocess: 748169699Skan flag_pch_preprocess = value; 749169699Skan break; 750169699Skan 751117395Skan case OPT_fpermissive: 752132718Skan flag_permissive = value; 753117395Skan break; 754117395Skan 755117395Skan case OPT_fpreprocessed: 756132718Skan cpp_opts->preprocessed = value; 757117395Skan break; 758117395Skan 759132718Skan case OPT_freplace_objc_classes: 760132718Skan flag_replace_objc_classes = value; 761132718Skan break; 762169699Skan 763117395Skan case OPT_frepo: 764132718Skan flag_use_repository = value; 765132718Skan if (value) 766117395Skan flag_implicit_templates = 0; 767117395Skan break; 768117395Skan 769117395Skan case OPT_frtti: 770132718Skan flag_rtti = value; 771117395Skan break; 772117395Skan 773117395Skan case OPT_fshow_column: 774132718Skan cpp_opts->show_column = value; 775117395Skan break; 776117395Skan 777117395Skan case OPT_fstats: 778132718Skan flag_detailed_statistics = value; 779117395Skan break; 780117395Skan 781132718Skan case OPT_ftabstop_: 782117395Skan /* It is documented that we silently ignore silly values. */ 783132718Skan if (value >= 1 && value <= 100) 784132718Skan cpp_opts->tabstop = value; 785117395Skan break; 786117395Skan 787132718Skan case OPT_fexec_charset_: 788132718Skan cpp_opts->narrow_charset = arg; 789117395Skan break; 790117395Skan 791132718Skan case OPT_fwide_exec_charset_: 792132718Skan cpp_opts->wide_charset = arg; 793117395Skan break; 794117395Skan 795132718Skan case OPT_finput_charset_: 796132718Skan cpp_opts->input_charset = arg; 797132718Skan break; 798132718Skan 799132718Skan case OPT_ftemplate_depth_: 800132718Skan max_tinst_depth = value; 801132718Skan break; 802132718Skan 803117395Skan case OPT_fuse_cxa_atexit: 804132718Skan flag_use_cxa_atexit = value; 805117395Skan break; 806169699Skan 807169699Skan case OPT_fuse_cxa_get_exception_ptr: 808169699Skan flag_use_cxa_get_exception_ptr = value; 809169699Skan break; 810117395Skan 811169699Skan case OPT_fvisibility_inlines_hidden: 812169699Skan visibility_options.inlines_hidden = value; 813169699Skan break; 814169699Skan 815117395Skan case OPT_fweak: 816132718Skan flag_weak = value; 817117395Skan break; 818117395Skan 819169699Skan case OPT_fthreadsafe_statics: 820169699Skan flag_threadsafe_statics = value; 821169699Skan break; 822169699Skan 823132718Skan case OPT_fzero_link: 824132718Skan flag_zero_link = value; 825132718Skan break; 826132718Skan 827117395Skan case OPT_gen_decls: 828117395Skan flag_gen_declaration = 1; 829117395Skan break; 830117395Skan 831259268Spfg case OPT_femit_struct_debug_baseonly: 832259268Spfg set_struct_debug_option ("base"); 833259268Spfg break; 834259268Spfg 835259268Spfg case OPT_femit_struct_debug_reduced: 836259268Spfg set_struct_debug_option ("dir:ord:sys,dir:gen:any,ind:base"); 837259268Spfg break; 838259268Spfg 839259268Spfg case OPT_femit_struct_debug_detailed_: 840259268Spfg set_struct_debug_option (arg); 841259268Spfg break; 842259268Spfg 843132718Skan case OPT_idirafter: 844169699Skan add_path (xstrdup (arg), AFTER, 0, true); 845132718Skan break; 846132718Skan 847132718Skan case OPT_imacros: 848132718Skan case OPT_include: 849132718Skan defer_opt (code, arg); 850132718Skan break; 851132718Skan 852169699Skan case OPT_imultilib: 853169699Skan imultilib = arg; 854169699Skan break; 855169699Skan 856132718Skan case OPT_iprefix: 857132718Skan iprefix = arg; 858132718Skan break; 859132718Skan 860169699Skan case OPT_iquote: 861169699Skan add_path (xstrdup (arg), QUOTE, 0, true); 862169699Skan break; 863169699Skan 864132718Skan case OPT_isysroot: 865132718Skan sysroot = arg; 866132718Skan break; 867132718Skan 868132718Skan case OPT_isystem: 869169699Skan add_path (xstrdup (arg), SYSTEM, 0, true); 870132718Skan break; 871132718Skan 872132718Skan case OPT_iwithprefix: 873132718Skan add_prefixed_path (arg, SYSTEM); 874132718Skan break; 875132718Skan 876132718Skan case OPT_iwithprefixbefore: 877132718Skan add_prefixed_path (arg, BRACKET); 878132718Skan break; 879132718Skan 880117395Skan case OPT_lang_asm: 881117395Skan cpp_set_lang (parse_in, CLK_ASM); 882132718Skan cpp_opts->dollars_in_ident = false; 883117395Skan break; 884117395Skan 885169699Skan case OPT_lang_fortran: 886169699Skan lang_fortran = true; 887169699Skan break; 888169699Skan 889117395Skan case OPT_lang_objc: 890117395Skan cpp_opts->objc = 1; 891117395Skan break; 892117395Skan 893117395Skan case OPT_nostdinc: 894132718Skan std_inc = false; 895117395Skan break; 896117395Skan 897132718Skan case OPT_nostdinc__: 898132718Skan std_cxx_inc = false; 899117395Skan break; 900117395Skan 901117395Skan case OPT_o: 902117395Skan if (!out_fname) 903117395Skan out_fname = arg; 904117395Skan else 905132718Skan error ("output filename specified twice"); 906117395Skan break; 907117395Skan 908117395Skan /* We need to handle the -pedantic switches here, rather than in 909117395Skan c_common_post_options, so that a subsequent -Wno-endif-labels 910117395Skan is not overridden. */ 911117395Skan case OPT_pedantic_errors: 912117395Skan cpp_opts->pedantic_errors = 1; 913132718Skan /* Fall through. */ 914117395Skan case OPT_pedantic: 915117395Skan cpp_opts->pedantic = 1; 916117395Skan cpp_opts->warn_endif_labels = 1; 917169699Skan if (warn_pointer_sign == -1) 918169699Skan warn_pointer_sign = 1; 919169699Skan if (warn_overlength_strings == -1) 920169699Skan warn_overlength_strings = 1; 921117395Skan break; 922117395Skan 923117395Skan case OPT_print_objc_runtime_info: 924117395Skan print_struct_values = 1; 925117395Skan break; 926117395Skan 927169699Skan case OPT_print_pch_checksum: 928169699Skan c_common_print_pch_checksum (stdout); 929169699Skan exit_after_options = true; 930169699Skan break; 931169699Skan 932117395Skan case OPT_remap: 933117395Skan cpp_opts->remap = 1; 934117395Skan break; 935117395Skan 936132718Skan case OPT_std_c__98: 937132718Skan case OPT_std_gnu__98: 938169699Skan if (!preprocessing_asm_p) 939169699Skan set_std_cxx98 (code == OPT_std_c__98 /* ISO */); 940117395Skan break; 941117395Skan 942117395Skan case OPT_std_c89: 943117395Skan case OPT_std_iso9899_1990: 944117395Skan case OPT_std_iso9899_199409: 945169699Skan if (!preprocessing_asm_p) 946169699Skan set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */); 947117395Skan break; 948117395Skan 949117395Skan case OPT_std_gnu89: 950169699Skan if (!preprocessing_asm_p) 951169699Skan set_std_c89 (false /* c94 */, false /* ISO */); 952117395Skan break; 953117395Skan 954117395Skan case OPT_std_c99: 955117395Skan case OPT_std_c9x: 956117395Skan case OPT_std_iso9899_1999: 957117395Skan case OPT_std_iso9899_199x: 958169699Skan if (!preprocessing_asm_p) 959169699Skan set_std_c99 (true /* ISO */); 960117395Skan break; 961117395Skan 962117395Skan case OPT_std_gnu99: 963117395Skan case OPT_std_gnu9x: 964169699Skan if (!preprocessing_asm_p) 965169699Skan set_std_c99 (false /* ISO */); 966117395Skan break; 967117395Skan 968117395Skan case OPT_trigraphs: 969117395Skan cpp_opts->trigraphs = 1; 970117395Skan break; 971117395Skan 972117395Skan case OPT_traditional_cpp: 973117395Skan cpp_opts->traditional = 1; 974117395Skan break; 975117395Skan 976117395Skan case OPT_undef: 977117395Skan flag_undef = 1; 978117395Skan break; 979117395Skan 980117395Skan case OPT_w: 981117395Skan cpp_opts->inhibit_warnings = 1; 982117395Skan break; 983117395Skan 984117395Skan case OPT_v: 985132718Skan verbose = true; 986117395Skan break; 987117395Skan } 988117395Skan 989117395Skan return result; 990117395Skan} 991117395Skan 992117395Skan/* Post-switch processing. */ 993117395Skanbool 994132718Skanc_common_post_options (const char **pfilename) 995117395Skan{ 996132718Skan struct cpp_callbacks *cb; 997132718Skan 998117395Skan /* Canonicalize the input and output filenames. */ 999132718Skan if (in_fnames == NULL) 1000132718Skan { 1001169699Skan in_fnames = XNEWVEC (const char *, 1); 1002132718Skan in_fnames[0] = ""; 1003132718Skan } 1004132718Skan else if (strcmp (in_fnames[0], "-") == 0) 1005132718Skan in_fnames[0] = ""; 1006117395Skan 1007117395Skan if (out_fname == NULL || !strcmp (out_fname, "-")) 1008117395Skan out_fname = ""; 1009117395Skan 1010117395Skan if (cpp_opts->deps.style == DEPS_NONE) 1011117395Skan check_deps_environment_vars (); 1012117395Skan 1013117395Skan handle_deferred_opts (); 1014117395Skan 1015117395Skan sanitize_cpp_opts (); 1016117395Skan 1017169699Skan register_include_chains (parse_in, sysroot, iprefix, imultilib, 1018132718Skan std_inc, std_cxx_inc && c_dialect_cxx (), verbose); 1019132718Skan 1020169699Skan#ifdef C_COMMON_OVERRIDE_OPTIONS 1021169699Skan /* Some machines may reject certain combinations of C 1022169699Skan language-specific options. */ 1023169699Skan C_COMMON_OVERRIDE_OPTIONS; 1024169699Skan#endif 1025169699Skan 1026117395Skan flag_inline_trees = 1; 1027117395Skan 1028169699Skan /* Use tree inlining. */ 1029169699Skan if (!flag_no_inline) 1030169699Skan flag_no_inline = 1; 1031169699Skan if (flag_inline_functions) 1032169699Skan flag_inline_trees = 2; 1033117395Skan 1034189824Sdas /* By default we use C99 inline semantics in GNU99 or C99 mode. C99 1035189824Sdas inline semantics are not supported in GNU89 or C89 mode. */ 1036189824Sdas if (flag_gnu89_inline == -1) 1037189824Sdas flag_gnu89_inline = !flag_isoc99; 1038189824Sdas else if (!flag_gnu89_inline && !flag_isoc99) 1039189824Sdas error ("-fno-gnu89-inline is only supported in GNU99 or C99 mode"); 1040169699Skan 1041169699Skan /* If we are given more than one input file, we must use 1042169699Skan unit-at-a-time mode. */ 1043169699Skan if (num_in_fnames > 1) 1044169699Skan flag_unit_at_a_time = 1; 1045169699Skan 1046169699Skan /* Default to ObjC sjlj exception handling if NeXT runtime. */ 1047169699Skan if (flag_objc_sjlj_exceptions < 0) 1048169699Skan flag_objc_sjlj_exceptions = flag_next_runtime; 1049169699Skan if (flag_objc_exceptions && !flag_objc_sjlj_exceptions) 1050169699Skan flag_exceptions = 1; 1051169699Skan 1052169699Skan /* -Wextra implies -Wsign-compare, -Wmissing-field-initializers and 1053169699Skan -Woverride-init, but not if explicitly overridden. */ 1054132718Skan if (warn_sign_compare == -1) 1055132718Skan warn_sign_compare = extra_warnings; 1056169699Skan if (warn_missing_field_initializers == -1) 1057169699Skan warn_missing_field_initializers = extra_warnings; 1058169699Skan if (warn_override_init == -1) 1059169699Skan warn_override_init = extra_warnings; 1060132718Skan 1061169699Skan /* -Wpointer_sign is disabled by default, but it is enabled if any 1062169699Skan of -Wall or -pedantic are given. */ 1063169699Skan if (warn_pointer_sign == -1) 1064169699Skan warn_pointer_sign = 0; 1065169699Skan 1066169699Skan /* -Woverlength-strings is off by default, but is enabled by -pedantic. 1067169699Skan It is never enabled in C++, as the minimum limit is not normative 1068169699Skan in that standard. */ 1069169699Skan if (warn_overlength_strings == -1 || c_dialect_cxx ()) 1070169699Skan warn_overlength_strings = 0; 1071169699Skan 1072117395Skan /* Special format checking options don't work without -Wformat; warn if 1073117395Skan they are used. */ 1074169699Skan if (!warn_format) 1075169699Skan { 1076169699Skan warning (OPT_Wformat_y2k, 1077169699Skan "-Wformat-y2k ignored without -Wformat"); 1078169699Skan warning (OPT_Wformat_extra_args, 1079169699Skan "-Wformat-extra-args ignored without -Wformat"); 1080169699Skan warning (OPT_Wformat_zero_length, 1081169699Skan "-Wformat-zero-length ignored without -Wformat"); 1082169699Skan warning (OPT_Wformat_nonliteral, 1083169699Skan "-Wformat-nonliteral ignored without -Wformat"); 1084169699Skan warning (OPT_Wformat_security, 1085169699Skan "-Wformat-security ignored without -Wformat"); 1086169699Skan } 1087117395Skan 1088169699Skan /* C99 requires special handling of complex multiplication and division; 1089169699Skan -ffast-math and -fcx-limited-range are handled in process_options. */ 1090169699Skan if (flag_isoc99) 1091169699Skan flag_complex_method = 2; 1092169699Skan 1093132718Skan if (flag_preprocess_only) 1094132718Skan { 1095132718Skan /* Open the output now. We must do so even if flag_no_output is 1096132718Skan on, because there may be other output than from the actual 1097132718Skan preprocessing (e.g. from -dM). */ 1098132718Skan if (out_fname[0] == '\0') 1099132718Skan out_stream = stdout; 1100132718Skan else 1101132718Skan out_stream = fopen (out_fname, "w"); 1102132718Skan 1103132718Skan if (out_stream == NULL) 1104132718Skan { 1105132718Skan fatal_error ("opening output file %s: %m", out_fname); 1106132718Skan return false; 1107132718Skan } 1108132718Skan 1109132718Skan if (num_in_fnames > 1) 1110132718Skan error ("too many filenames given. Type %s --help for usage", 1111132718Skan progname); 1112132718Skan 1113132718Skan init_pp_output (out_stream); 1114132718Skan } 1115132718Skan else 1116132718Skan { 1117132718Skan init_c_lex (); 1118132718Skan 1119132718Skan /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */ 1120169699Skan input_location = UNKNOWN_LOCATION; 1121132718Skan } 1122132718Skan 1123132718Skan cb = cpp_get_callbacks (parse_in); 1124132718Skan cb->file_change = cb_file_change; 1125132718Skan cb->dir_change = cb_dir_change; 1126132718Skan cpp_post_options (parse_in); 1127132718Skan 1128169699Skan input_location = UNKNOWN_LOCATION; 1129132718Skan 1130117395Skan /* If an error has occurred in cpplib, note it so we fail 1131117395Skan immediately. */ 1132117395Skan errorcount += cpp_errors (parse_in); 1133117395Skan 1134132718Skan *pfilename = this_input_filename 1135132718Skan = cpp_read_main_file (parse_in, in_fnames[0]); 1136146908Skan /* Don't do any compilation or preprocessing if there is no input file. */ 1137132718Skan if (this_input_filename == NULL) 1138146908Skan { 1139146908Skan errorcount++; 1140146908Skan return false; 1141146908Skan } 1142117395Skan 1143132718Skan if (flag_working_directory 1144169699Skan && flag_preprocess_only && !flag_no_line_commands) 1145132718Skan pp_dir_change (parse_in, get_src_pwd ()); 1146117395Skan 1147132718Skan return flag_preprocess_only; 1148117395Skan} 1149117395Skan 1150117395Skan/* Front end initialization common to C, ObjC and C++. */ 1151132718Skanbool 1152132718Skanc_common_init (void) 1153117395Skan{ 1154117395Skan /* Set up preprocessor arithmetic. Must be done after call to 1155117395Skan c_common_nodes_and_builtins for type nodes to be good. */ 1156117395Skan cpp_opts->precision = TYPE_PRECISION (intmax_type_node); 1157117395Skan cpp_opts->char_precision = TYPE_PRECISION (char_type_node); 1158117395Skan cpp_opts->int_precision = TYPE_PRECISION (integer_type_node); 1159117395Skan cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node); 1160169699Skan cpp_opts->unsigned_wchar = TYPE_UNSIGNED (wchar_type_node); 1161132718Skan cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN; 1162117395Skan 1163132718Skan /* This can't happen until after wchar_precision and bytes_big_endian 1164132718Skan are known. */ 1165132718Skan cpp_init_iconv (parse_in); 1166117395Skan 1167169699Skan if (version_flag) 1168169699Skan c_common_print_pch_checksum (stderr); 1169169699Skan 1170117395Skan if (flag_preprocess_only) 1171117395Skan { 1172132718Skan finish_options (); 1173132718Skan preprocess_file (parse_in); 1174132718Skan return false; 1175117395Skan } 1176117395Skan 1177132718Skan /* Has to wait until now so that cpplib has its hash table. */ 1178117395Skan init_pragma (); 1179117395Skan 1180132718Skan return true; 1181117395Skan} 1182117395Skan 1183132718Skan/* Initialize the integrated preprocessor after debug output has been 1184132718Skan initialized; loop over each input file. */ 1185132718Skanvoid 1186169699Skanc_common_parse_file (int set_yydebug) 1187132718Skan{ 1188169699Skan unsigned int i; 1189169699Skan 1190169699Skan /* Enable parser debugging, if requested and we can. If requested 1191169699Skan and we can't, notify the user. */ 1192132718Skan#if YYDEBUG != 0 1193132718Skan yydebug = set_yydebug; 1194132718Skan#else 1195169699Skan if (set_yydebug) 1196169699Skan warning (0, "YYDEBUG was not defined at build time, -dy ignored"); 1197132718Skan#endif 1198132718Skan 1199169699Skan i = 0; 1200169699Skan for (;;) 1201132718Skan { 1202169699Skan /* Start the main input file, if the debug writer wants it. */ 1203169699Skan if (debug_hooks->start_end_main_source_file) 1204169699Skan (*debug_hooks->start_source_file) (0, this_input_filename); 1205132718Skan finish_options (); 1206169699Skan pch_init (); 1207169699Skan push_file_scope (); 1208132718Skan c_parse_file (); 1209169699Skan finish_file (); 1210169699Skan pop_file_scope (); 1211169699Skan /* And end the main input file, if the debug writer wants it */ 1212169699Skan if (debug_hooks->start_end_main_source_file) 1213169699Skan (*debug_hooks->end_source_file) (0); 1214169699Skan if (++i >= num_in_fnames) 1215169699Skan break; 1216169699Skan cpp_undef_all (parse_in); 1217169699Skan this_input_filename 1218169699Skan = cpp_read_main_file (parse_in, in_fnames[i]); 1219169699Skan /* If an input file is missing, abandon further compilation. 1220169699Skan cpplib has issued a diagnostic. */ 1221169699Skan if (!this_input_filename) 1222169699Skan break; 1223169699Skan } 1224132718Skan} 1225132718Skan 1226117395Skan/* Common finish hook for the C, ObjC and C++ front ends. */ 1227117395Skanvoid 1228132718Skanc_common_finish (void) 1229117395Skan{ 1230117395Skan FILE *deps_stream = NULL; 1231117395Skan 1232117395Skan if (cpp_opts->deps.style != DEPS_NONE) 1233117395Skan { 1234117395Skan /* If -M or -MM was seen without -MF, default output to the 1235117395Skan output stream. */ 1236117395Skan if (!deps_file) 1237117395Skan deps_stream = out_stream; 1238117395Skan else 1239117395Skan { 1240117395Skan deps_stream = fopen (deps_file, deps_append ? "a": "w"); 1241117395Skan if (!deps_stream) 1242132718Skan fatal_error ("opening dependency file %s: %m", deps_file); 1243117395Skan } 1244117395Skan } 1245117395Skan 1246117395Skan /* For performance, avoid tearing down cpplib's internal structures 1247117395Skan with cpp_destroy (). */ 1248117395Skan errorcount += cpp_finish (parse_in, deps_stream); 1249117395Skan 1250117395Skan if (deps_stream && deps_stream != out_stream 1251117395Skan && (ferror (deps_stream) || fclose (deps_stream))) 1252132718Skan fatal_error ("closing dependency file %s: %m", deps_file); 1253117395Skan 1254117395Skan if (out_stream && (ferror (out_stream) || fclose (out_stream))) 1255132718Skan fatal_error ("when writing output to %s: %m", out_fname); 1256117395Skan} 1257117395Skan 1258117395Skan/* Either of two environment variables can specify output of 1259117395Skan dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE 1260117395Skan DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to 1261117395Skan and DEPS_TARGET is the target to mention in the deps. They also 1262117395Skan result in dependency information being appended to the output file 1263117395Skan rather than overwriting it, and like Sun's compiler 1264117395Skan SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */ 1265117395Skanstatic void 1266132718Skancheck_deps_environment_vars (void) 1267117395Skan{ 1268117395Skan char *spec; 1269117395Skan 1270117395Skan GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT"); 1271117395Skan if (spec) 1272117395Skan cpp_opts->deps.style = DEPS_USER; 1273117395Skan else 1274117395Skan { 1275117395Skan GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES"); 1276117395Skan if (spec) 1277117395Skan { 1278117395Skan cpp_opts->deps.style = DEPS_SYSTEM; 1279117395Skan cpp_opts->deps.ignore_main_file = true; 1280117395Skan } 1281117395Skan } 1282117395Skan 1283117395Skan if (spec) 1284117395Skan { 1285117395Skan /* Find the space before the DEPS_TARGET, if there is one. */ 1286117395Skan char *s = strchr (spec, ' '); 1287117395Skan if (s) 1288117395Skan { 1289117395Skan /* Let the caller perform MAKE quoting. */ 1290117395Skan defer_opt (OPT_MT, s + 1); 1291117395Skan *s = '\0'; 1292117395Skan } 1293117395Skan 1294117395Skan /* Command line -MF overrides environment variables and default. */ 1295117395Skan if (!deps_file) 1296117395Skan deps_file = spec; 1297117395Skan 1298117395Skan deps_append = 1; 1299169699Skan deps_seen = true; 1300117395Skan } 1301117395Skan} 1302117395Skan 1303117395Skan/* Handle deferred command line switches. */ 1304117395Skanstatic void 1305132718Skanhandle_deferred_opts (void) 1306117395Skan{ 1307117395Skan size_t i; 1308169699Skan struct deps *deps; 1309117395Skan 1310169699Skan /* Avoid allocating the deps buffer if we don't need it. 1311169699Skan (This flag may be true without there having been -MT or -MQ 1312169699Skan options, but we'll still need the deps buffer.) */ 1313169699Skan if (!deps_seen) 1314169699Skan return; 1315169699Skan 1316169699Skan deps = cpp_get_deps (parse_in); 1317169699Skan 1318117395Skan for (i = 0; i < deferred_count; i++) 1319117395Skan { 1320117395Skan struct deferred_opt *opt = &deferred_opts[i]; 1321117395Skan 1322132718Skan if (opt->code == OPT_MT || opt->code == OPT_MQ) 1323169699Skan deps_add_target (deps, opt->arg, opt->code == OPT_MQ); 1324117395Skan } 1325117395Skan} 1326117395Skan 1327117395Skan/* These settings are appropriate for GCC, but not necessarily so for 1328117395Skan cpplib as a library. */ 1329117395Skanstatic void 1330132718Skansanitize_cpp_opts (void) 1331117395Skan{ 1332117395Skan /* If we don't know what style of dependencies to output, complain 1333117395Skan if any other dependency switches have been given. */ 1334117395Skan if (deps_seen && cpp_opts->deps.style == DEPS_NONE) 1335117395Skan error ("to generate dependencies you must specify either -M or -MM"); 1336117395Skan 1337117395Skan /* -dM and dependencies suppress normal output; do it here so that 1338117395Skan the last -d[MDN] switch overrides earlier ones. */ 1339132718Skan if (flag_dump_macros == 'M') 1340132718Skan flag_no_output = 1; 1341117395Skan 1342259405Spfg /* By default, -fdirectives-only implies -dD. This allows subsequent phases 1343259405Spfg to perform proper macro expansion. */ 1344259405Spfg if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros) 1345259405Spfg flag_dump_macros = 'D'; 1346259405Spfg 1347117395Skan /* Disable -dD, -dN and -dI if normal output is suppressed. Allow 1348117395Skan -dM since at least glibc relies on -M -dM to work. */ 1349169699Skan /* Also, flag_no_output implies flag_no_line_commands, always. */ 1350132718Skan if (flag_no_output) 1351117395Skan { 1352132718Skan if (flag_dump_macros != 'M') 1353132718Skan flag_dump_macros = 0; 1354132718Skan flag_dump_includes = 0; 1355146908Skan flag_no_line_commands = 1; 1356117395Skan } 1357117395Skan 1358117395Skan cpp_opts->unsigned_char = !flag_signed_char; 1359117395Skan cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS; 1360117395Skan 1361117395Skan /* We want -Wno-long-long to override -pedantic -std=non-c99 1362117395Skan and/or -Wtraditional, whatever the ordering. */ 1363117395Skan cpp_opts->warn_long_long 1364117395Skan = warn_long_long && ((!flag_isoc99 && pedantic) || warn_traditional); 1365132718Skan 1366169699Skan /* Similarly with -Wno-variadic-macros. No check for c99 here, since 1367169699Skan this also turns off warnings about GCCs extension. */ 1368169699Skan cpp_opts->warn_variadic_macros 1369169699Skan = warn_variadic_macros && (pedantic || warn_traditional); 1370169699Skan 1371132718Skan /* If we're generating preprocessor output, emit current directory 1372132718Skan if explicitly requested or if debugging information is enabled. 1373132718Skan ??? Maybe we should only do it for debugging formats that 1374132718Skan actually output the current directory? */ 1375132718Skan if (flag_working_directory == -1) 1376132718Skan flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE); 1377259405Spfg 1378259405Spfg if (cpp_opts->directives_only) 1379259405Spfg { 1380259405Spfg if (warn_unused_macros) 1381259405Spfg error ("-fdirectives-only is incompatible with -Wunused_macros"); 1382259405Spfg if (cpp_opts->traditional) 1383259405Spfg error ("-fdirectives-only is incompatible with -traditional"); 1384259405Spfg } 1385117395Skan} 1386117395Skan 1387132718Skan/* Add include path with a prefix at the front of its name. */ 1388132718Skanstatic void 1389132718Skanadd_prefixed_path (const char *suffix, size_t chain) 1390132718Skan{ 1391132718Skan char *path; 1392132718Skan const char *prefix; 1393132718Skan size_t prefix_len, suffix_len; 1394132718Skan 1395132718Skan suffix_len = strlen (suffix); 1396132718Skan prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR; 1397132718Skan prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len; 1398132718Skan 1399169699Skan path = (char *) xmalloc (prefix_len + suffix_len + 1); 1400132718Skan memcpy (path, prefix, prefix_len); 1401132718Skan memcpy (path + prefix_len, suffix, suffix_len); 1402132718Skan path[prefix_len + suffix_len] = '\0'; 1403132718Skan 1404169699Skan add_path (path, chain, 0, false); 1405132718Skan} 1406132718Skan 1407132718Skan/* Handle -D, -U, -A, -imacros, and the first -include. */ 1408132718Skanstatic void 1409132718Skanfinish_options (void) 1410132718Skan{ 1411132718Skan if (!cpp_opts->preprocessed) 1412132718Skan { 1413132718Skan size_t i; 1414132718Skan 1415169699Skan cb_file_change (parse_in, 1416169699Skan linemap_add (&line_table, LC_RENAME, 0, 1417169699Skan _("<built-in>"), 0)); 1418169699Skan 1419132718Skan cpp_init_builtins (parse_in, flag_hosted); 1420132718Skan c_cpp_builtins (parse_in); 1421132718Skan 1422132718Skan /* We're about to send user input to cpplib, so make it warn for 1423132718Skan things that we previously (when we sent it internal definitions) 1424132718Skan told it to not warn. 1425132718Skan 1426132718Skan C99 permits implementation-defined characters in identifiers. 1427132718Skan The documented meaning of -std= is to turn off extensions that 1428132718Skan conflict with the specified standard, and since a strictly 1429132718Skan conforming program cannot contain a '$', we do not condition 1430132718Skan their acceptance on the -std= setting. */ 1431132718Skan cpp_opts->warn_dollars = (cpp_opts->pedantic && !cpp_opts->c99); 1432132718Skan 1433169699Skan cb_file_change (parse_in, 1434169699Skan linemap_add (&line_table, LC_RENAME, 0, 1435169699Skan _("<command-line>"), 0)); 1436169699Skan 1437132718Skan for (i = 0; i < deferred_count; i++) 1438132718Skan { 1439132718Skan struct deferred_opt *opt = &deferred_opts[i]; 1440132718Skan 1441132718Skan if (opt->code == OPT_D) 1442132718Skan cpp_define (parse_in, opt->arg); 1443132718Skan else if (opt->code == OPT_U) 1444132718Skan cpp_undef (parse_in, opt->arg); 1445132718Skan else if (opt->code == OPT_A) 1446132718Skan { 1447132718Skan if (opt->arg[0] == '-') 1448132718Skan cpp_unassert (parse_in, opt->arg + 1); 1449132718Skan else 1450132718Skan cpp_assert (parse_in, opt->arg); 1451132718Skan } 1452132718Skan } 1453132718Skan 1454132718Skan /* Handle -imacros after -D and -U. */ 1455132718Skan for (i = 0; i < deferred_count; i++) 1456132718Skan { 1457132718Skan struct deferred_opt *opt = &deferred_opts[i]; 1458132718Skan 1459132718Skan if (opt->code == OPT_imacros 1460132718Skan && cpp_push_include (parse_in, opt->arg)) 1461132718Skan { 1462132718Skan /* Disable push_command_line_include callback for now. */ 1463132718Skan include_cursor = deferred_count + 1; 1464132718Skan cpp_scan_nooutput (parse_in); 1465132718Skan } 1466132718Skan } 1467132718Skan } 1468259405Spfg else if (cpp_opts->directives_only) 1469259405Spfg cpp_init_special_builtins (parse_in); 1470132718Skan 1471132718Skan include_cursor = 0; 1472132718Skan push_command_line_include (); 1473132718Skan} 1474132718Skan 1475132718Skan/* Give CPP the next file given by -include, if any. */ 1476132718Skanstatic void 1477132718Skanpush_command_line_include (void) 1478132718Skan{ 1479132718Skan while (include_cursor < deferred_count) 1480132718Skan { 1481132718Skan struct deferred_opt *opt = &deferred_opts[include_cursor++]; 1482132718Skan 1483169699Skan if (!cpp_opts->preprocessed && opt->code == OPT_include 1484132718Skan && cpp_push_include (parse_in, opt->arg)) 1485132718Skan return; 1486132718Skan } 1487132718Skan 1488132718Skan if (include_cursor == deferred_count) 1489132718Skan { 1490132718Skan include_cursor++; 1491132718Skan /* -Wunused-macros should only warn about macros defined hereafter. */ 1492132718Skan cpp_opts->warn_unused_macros = warn_unused_macros; 1493132718Skan /* Restore the line map from <command line>. */ 1494169699Skan if (!cpp_opts->preprocessed) 1495169699Skan cpp_change_file (parse_in, LC_RENAME, this_input_filename); 1496132718Skan 1497132718Skan /* Set this here so the client can change the option if it wishes, 1498132718Skan and after stacking the main file so we don't trace the main file. */ 1499169699Skan line_table.trace_includes = cpp_opts->print_include_names; 1500132718Skan } 1501132718Skan} 1502132718Skan 1503132718Skan/* File change callback. Has to handle -include files. */ 1504132718Skanstatic void 1505169699Skancb_file_change (cpp_reader * ARG_UNUSED (pfile), 1506132718Skan const struct line_map *new_map) 1507132718Skan{ 1508132718Skan if (flag_preprocess_only) 1509132718Skan pp_file_change (new_map); 1510132718Skan else 1511132718Skan fe_file_change (new_map); 1512132718Skan 1513132718Skan if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map))) 1514132718Skan push_command_line_include (); 1515132718Skan} 1516132718Skan 1517132718Skanvoid 1518169699Skancb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir) 1519132718Skan{ 1520169699Skan if (!set_src_pwd (dir)) 1521169699Skan warning (0, "too late for # directive to set debug directory"); 1522132718Skan} 1523132718Skan 1524117395Skan/* Set the C 89 standard (with 1994 amendments if C94, without GNU 1525117395Skan extensions if ISO). There is no concept of gnu94. */ 1526117395Skanstatic void 1527132718Skanset_std_c89 (int c94, int iso) 1528117395Skan{ 1529117395Skan cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89); 1530117395Skan flag_iso = iso; 1531117395Skan flag_no_asm = iso; 1532117395Skan flag_no_gnu_keywords = iso; 1533117395Skan flag_no_nonansi_builtin = iso; 1534117395Skan flag_isoc94 = c94; 1535117395Skan flag_isoc99 = 0; 1536117395Skan} 1537117395Skan 1538117395Skan/* Set the C 99 standard (without GNU extensions if ISO). */ 1539117395Skanstatic void 1540132718Skanset_std_c99 (int iso) 1541117395Skan{ 1542117395Skan cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99); 1543117395Skan flag_no_asm = iso; 1544117395Skan flag_no_nonansi_builtin = iso; 1545117395Skan flag_iso = iso; 1546117395Skan flag_isoc99 = 1; 1547117395Skan flag_isoc94 = 1; 1548117395Skan} 1549117395Skan 1550117395Skan/* Set the C++ 98 standard (without GNU extensions if ISO). */ 1551117395Skanstatic void 1552132718Skanset_std_cxx98 (int iso) 1553117395Skan{ 1554117395Skan cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX); 1555117395Skan flag_no_gnu_keywords = iso; 1556117395Skan flag_no_nonansi_builtin = iso; 1557117395Skan flag_iso = iso; 1558117395Skan} 1559117395Skan 1560117395Skan/* Handle setting implicit to ON. */ 1561117395Skanstatic void 1562132718Skanset_Wimplicit (int on) 1563117395Skan{ 1564117395Skan warn_implicit = on; 1565117395Skan warn_implicit_int = on; 1566117395Skan if (on) 1567117395Skan { 1568117395Skan if (mesg_implicit_function_declaration != 2) 1569117395Skan mesg_implicit_function_declaration = 1; 1570117395Skan } 1571117395Skan else 1572117395Skan mesg_implicit_function_declaration = 0; 1573117395Skan} 1574117395Skan 1575117395Skan/* Args to -d specify what to dump. Silently ignore 1576117395Skan unrecognized options; they may be aimed at toplev.c. */ 1577117395Skanstatic void 1578132718Skanhandle_OPT_d (const char *arg) 1579117395Skan{ 1580117395Skan char c; 1581117395Skan 1582117395Skan while ((c = *arg++) != '\0') 1583117395Skan switch (c) 1584117395Skan { 1585132718Skan case 'M': /* Dump macros only. */ 1586132718Skan case 'N': /* Dump names. */ 1587132718Skan case 'D': /* Dump definitions. */ 1588132718Skan flag_dump_macros = c; 1589117395Skan break; 1590117395Skan 1591117395Skan case 'I': 1592132718Skan flag_dump_includes = 1; 1593117395Skan break; 1594117395Skan } 1595117395Skan} 1596