1/* Copyright (C) 2008-2015 Free Software Foundation, Inc. 2 3This file is part of GCC. 4 5GCC is free software; you can redistribute it and/or modify it under 6the terms of the GNU General Public License as published by the Free 7Software Foundation; either version 3, or (at your option) any later 8version. 9 10GCC is distributed in the hope that it will be useful, but WITHOUT ANY 11WARRANTY; without even the implied warranty of MERCHANTABILITY or 12FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13for more details. 14 15You should have received a copy of the GNU General Public License 16along with GCC; see the file COPYING3. If not see 17<http://www.gnu.org/licenses/>. */ 18 19#include "config.h" 20#include "system.h" 21#include "coretypes.h" 22#include "tm.h" 23#include "hash-set.h" 24#include "machmode.h" 25#include "vec.h" 26#include "double-int.h" 27#include "input.h" 28#include "alias.h" 29#include "symtab.h" 30#include "wide-int.h" 31#include "inchash.h" 32#include "tree.h" 33#include "version.h" 34#include "flags.h" 35 36 37#include "options.h" 38#include "gfortran.h" 39#include "tm_p.h" /* Target prototypes. */ 40#include "target.h" 41#include "toplev.h" 42#include "diagnostic.h" 43 44#include "../../libcpp/internal.h" 45#include "cpp.h" 46#include "incpath.h" 47#include "cppbuiltin.h" 48#include "mkdeps.h" 49 50#ifndef TARGET_SYSTEM_ROOT 51# define TARGET_SYSTEM_ROOT NULL 52#endif 53 54#ifndef TARGET_CPU_CPP_BUILTINS 55# define TARGET_CPU_CPP_BUILTINS() 56#endif 57 58#ifndef TARGET_OS_CPP_BUILTINS 59# define TARGET_OS_CPP_BUILTINS() 60#endif 61 62#ifndef TARGET_OBJFMT_CPP_BUILTINS 63# define TARGET_OBJFMT_CPP_BUILTINS() 64#endif 65 66 67/* Holds switches parsed by gfc_cpp_handle_option (), but whose 68 handling is deferred to gfc_cpp_init (). */ 69typedef struct 70{ 71 enum opt_code code; 72 const char *arg; 73} 74gfc_cpp_deferred_opt_t; 75 76 77/* Defined and undefined macros being queued for output with -dU at 78 the next newline. */ 79typedef struct gfc_cpp_macro_queue 80{ 81 struct gfc_cpp_macro_queue *next; /* Next macro in the list. */ 82 char *macro; /* The name of the macro if not 83 defined, the full definition if 84 defined. */ 85} gfc_cpp_macro_queue; 86static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue; 87 88struct gfc_cpp_option_data 89{ 90 /* Argument of -cpp, implied by SPEC; 91 if NULL, preprocessing disabled. */ 92 const char *temporary_filename; 93 94 const char *output_filename; /* -o <arg> */ 95 int preprocess_only; /* -E */ 96 int discard_comments; /* -C */ 97 int discard_comments_in_macro_exp; /* -CC */ 98 int print_include_names; /* -H */ 99 int no_line_commands; /* -P */ 100 char dump_macros; /* -d[DMNU] */ 101 int dump_includes; /* -dI */ 102 int working_directory; /* -fworking-directory */ 103 int no_predefined; /* -undef */ 104 int standard_include_paths; /* -nostdinc */ 105 int verbose; /* -v */ 106 int deps; /* -M */ 107 int deps_skip_system; /* -MM */ 108 const char *deps_filename; /* -M[M]D */ 109 const char *deps_filename_user; /* -MF <arg> */ 110 int deps_missing_are_generated; /* -MG */ 111 int deps_phony; /* -MP */ 112 int warn_date_time; /* -Wdate-time */ 113 114 const char *multilib; /* -imultilib <dir> */ 115 const char *prefix; /* -iprefix <dir> */ 116 const char *sysroot; /* -isysroot <dir> */ 117 118 /* Options whose handling needs to be deferred until the 119 appropriate cpp-objects are created: 120 -A predicate=answer 121 -D <macro>[=<val>] 122 -U <macro> */ 123 gfc_cpp_deferred_opt_t *deferred_opt; 124 int deferred_opt_count; 125} 126gfc_cpp_option; 127 128/* Structures used with libcpp: */ 129static cpp_options *cpp_option = NULL; 130static cpp_reader *cpp_in = NULL; 131 132/* Encapsulates state used to convert a stream of cpp-tokens into 133 a text file. */ 134static struct 135{ 136 FILE *outf; /* Stream to write to. */ 137 const cpp_token *prev; /* Previous token. */ 138 const cpp_token *source; /* Source token for spacing. */ 139 int src_line; /* Line number currently being written. */ 140 unsigned char printed; /* Nonzero if something output at line. */ 141 bool first_time; /* cb_file_change hasn't been called yet. */ 142} print; 143 144/* General output routines. */ 145static void scan_translation_unit (cpp_reader *); 146static void scan_translation_unit_trad (cpp_reader *); 147 148/* Callback routines for the parser. Most of these are active only 149 in specific modes. */ 150static void cb_file_change (cpp_reader *, const struct line_map *); 151static void cb_line_change (cpp_reader *, const cpp_token *, int); 152static void cb_define (cpp_reader *, source_location, cpp_hashnode *); 153static void cb_undef (cpp_reader *, source_location, cpp_hashnode *); 154static void cb_def_pragma (cpp_reader *, source_location); 155static void cb_include (cpp_reader *, source_location, const unsigned char *, 156 const char *, int, const cpp_token **); 157static void cb_ident (cpp_reader *, source_location, const cpp_string *); 158static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *); 159static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *); 160static bool cb_cpp_error (cpp_reader *, int, int, location_t, unsigned int, 161 const char *, va_list *) 162 ATTRIBUTE_GCC_DIAG(6,0); 163void pp_dir_change (cpp_reader *, const char *); 164 165static int dump_macro (cpp_reader *, cpp_hashnode *, void *); 166static void dump_queued_macros (cpp_reader *); 167 168 169static void 170cpp_define_builtins (cpp_reader *pfile) 171{ 172 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted' 173 in C, defines __STDC_HOSTED__?! */ 174 cpp_init_builtins (pfile, 0); 175 176 /* Initialize GFORTRAN specific builtins. 177 These are documented. */ 178 define_language_independent_builtin_macros (pfile); 179 cpp_define (pfile, "__GFORTRAN__=1"); 180 cpp_define (pfile, "_LANGUAGE_FORTRAN=1"); 181 182 if (flag_openacc) 183 cpp_define (pfile, "_OPENACC=201306"); 184 185 if (flag_openmp) 186 cpp_define (pfile, "_OPENMP=201307"); 187 188 /* The defines below are necessary for the TARGET_* macros. 189 190 FIXME: Note that builtin_define_std() actually is a function 191 in c-cppbuiltin.c which uses flags undefined for Fortran. 192 Let's skip this for now. If needed, one needs to look into it 193 once more. */ 194 195# define builtin_define(TXT) cpp_define (pfile, TXT) 196# define builtin_define_std(TXT) 197# define builtin_assert(TXT) cpp_assert (pfile, TXT) 198 199 /* FIXME: Pandora's Box 200 Using the macros below results in multiple breakages: 201 - mingw will fail to compile this file as dependent macros 202 assume to be used in c-cppbuiltin.c only. Further, they use 203 flags only valid/defined in C (same as noted above). 204 [config/i386/mingw32.h, config/i386/cygming.h] 205 - other platforms (not as popular) break similarly 206 [grep for 'builtin_define_with_int_value' in gcc/config/] 207 208 TARGET_CPU_CPP_BUILTINS (); 209 TARGET_OS_CPP_BUILTINS (); 210 TARGET_OBJFMT_CPP_BUILTINS (); */ 211 212#undef builtin_define 213#undef builtin_define_std 214#undef builtin_assert 215} 216 217bool 218gfc_cpp_enabled (void) 219{ 220 return gfc_cpp_option.temporary_filename != NULL; 221} 222 223bool 224gfc_cpp_preprocess_only (void) 225{ 226 return gfc_cpp_option.preprocess_only; 227} 228 229bool 230gfc_cpp_makedep (void) 231{ 232 return gfc_cpp_option.deps; 233} 234 235void 236gfc_cpp_add_dep (const char *name, bool system) 237{ 238 if (!gfc_cpp_option.deps_skip_system || !system) 239 deps_add_dep (cpp_get_deps (cpp_in), name); 240} 241 242void 243gfc_cpp_add_target (const char *name) 244{ 245 deps_add_target (cpp_get_deps (cpp_in), name, 0); 246} 247 248 249const char * 250gfc_cpp_temporary_file (void) 251{ 252 return gfc_cpp_option.temporary_filename; 253} 254 255void 256gfc_cpp_init_options (unsigned int decoded_options_count, 257 struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED) 258{ 259 /* Do not create any objects from libcpp here. If no 260 preprocessing is requested, this would be wasted 261 time and effort. 262 263 See gfc_cpp_post_options() instead. */ 264 265 gfc_cpp_option.temporary_filename = NULL; 266 gfc_cpp_option.output_filename = NULL; 267 gfc_cpp_option.preprocess_only = 0; 268 gfc_cpp_option.discard_comments = 1; 269 gfc_cpp_option.discard_comments_in_macro_exp = 1; 270 gfc_cpp_option.print_include_names = 0; 271 gfc_cpp_option.no_line_commands = 0; 272 gfc_cpp_option.dump_macros = '\0'; 273 gfc_cpp_option.dump_includes = 0; 274 gfc_cpp_option.working_directory = -1; 275 gfc_cpp_option.no_predefined = 0; 276 gfc_cpp_option.standard_include_paths = 1; 277 gfc_cpp_option.verbose = 0; 278 gfc_cpp_option.warn_date_time = 0; 279 gfc_cpp_option.deps = 0; 280 gfc_cpp_option.deps_skip_system = 0; 281 gfc_cpp_option.deps_phony = 0; 282 gfc_cpp_option.deps_missing_are_generated = 0; 283 gfc_cpp_option.deps_filename = NULL; 284 gfc_cpp_option.deps_filename_user = NULL; 285 286 gfc_cpp_option.multilib = NULL; 287 gfc_cpp_option.prefix = NULL; 288 gfc_cpp_option.sysroot = TARGET_SYSTEM_ROOT; 289 290 gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t, 291 decoded_options_count); 292 gfc_cpp_option.deferred_opt_count = 0; 293} 294 295int 296gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED) 297{ 298 int result = 1; 299 enum opt_code code = (enum opt_code) scode; 300 301 switch (code) 302 { 303 default: 304 result = 0; 305 break; 306 307 case OPT_cpp_: 308 gfc_cpp_option.temporary_filename = arg; 309 break; 310 311 case OPT_nocpp: 312 gfc_cpp_option.temporary_filename = 0L; 313 break; 314 315 case OPT_d: 316 for ( ; *arg; ++arg) 317 switch (*arg) 318 { 319 case 'D': 320 case 'M': 321 case 'N': 322 case 'U': 323 gfc_cpp_option.dump_macros = *arg; 324 break; 325 326 case 'I': 327 gfc_cpp_option.dump_includes = 1; 328 break; 329 } 330 break; 331 332 case OPT_fworking_directory: 333 gfc_cpp_option.working_directory = value; 334 break; 335 336 case OPT_idirafter: 337 gfc_cpp_add_include_path_after (xstrdup(arg), true); 338 break; 339 340 case OPT_imultilib: 341 gfc_cpp_option.multilib = arg; 342 break; 343 344 case OPT_iprefix: 345 gfc_cpp_option.prefix = arg; 346 break; 347 348 case OPT_isysroot: 349 gfc_cpp_option.sysroot = arg; 350 break; 351 352 case OPT_iquote: 353 case OPT_isystem: 354 gfc_cpp_add_include_path (xstrdup(arg), true); 355 break; 356 357 case OPT_nostdinc: 358 gfc_cpp_option.standard_include_paths = value; 359 break; 360 361 case OPT_o: 362 if (!gfc_cpp_option.output_filename) 363 gfc_cpp_option.output_filename = arg; 364 else 365 gfc_fatal_error ("output filename specified twice"); 366 break; 367 368 case OPT_undef: 369 gfc_cpp_option.no_predefined = value; 370 break; 371 372 case OPT_v: 373 gfc_cpp_option.verbose = value; 374 break; 375 376 case OPT_Wdate_time: 377 gfc_cpp_option.warn_date_time = value; 378 break; 379 380 case OPT_A: 381 case OPT_D: 382 case OPT_U: 383 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code; 384 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg; 385 gfc_cpp_option.deferred_opt_count++; 386 break; 387 388 case OPT_C: 389 gfc_cpp_option.discard_comments = 0; 390 break; 391 392 case OPT_CC: 393 gfc_cpp_option.discard_comments = 0; 394 gfc_cpp_option.discard_comments_in_macro_exp = 0; 395 break; 396 397 case OPT_E: 398 gfc_cpp_option.preprocess_only = 1; 399 break; 400 401 case OPT_H: 402 gfc_cpp_option.print_include_names = 1; 403 break; 404 405 case OPT_MM: 406 gfc_cpp_option.deps_skip_system = 1; 407 /* fall through */ 408 409 case OPT_M: 410 gfc_cpp_option.deps = 1; 411 break; 412 413 case OPT_MMD: 414 gfc_cpp_option.deps_skip_system = 1; 415 /* fall through */ 416 417 case OPT_MD: 418 gfc_cpp_option.deps = 1; 419 gfc_cpp_option.deps_filename = arg; 420 break; 421 422 case OPT_MF: 423 /* If specified multiple times, last one wins. */ 424 gfc_cpp_option.deps_filename_user = arg; 425 break; 426 427 case OPT_MG: 428 gfc_cpp_option.deps_missing_are_generated = 1; 429 break; 430 431 case OPT_MP: 432 gfc_cpp_option.deps_phony = 1; 433 break; 434 435 case OPT_MQ: 436 case OPT_MT: 437 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code; 438 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg; 439 gfc_cpp_option.deferred_opt_count++; 440 break; 441 442 case OPT_P: 443 gfc_cpp_option.no_line_commands = 1; 444 break; 445 } 446 447 return result; 448} 449 450 451void 452gfc_cpp_post_options (void) 453{ 454 /* Any preprocessing-related option without '-cpp' is considered 455 an error. */ 456 if (!gfc_cpp_enabled () 457 && (gfc_cpp_preprocess_only () 458 || gfc_cpp_makedep () 459 || !gfc_cpp_option.discard_comments 460 || !gfc_cpp_option.discard_comments_in_macro_exp 461 || gfc_cpp_option.print_include_names 462 || gfc_cpp_option.no_line_commands 463 || gfc_cpp_option.dump_macros 464 || gfc_cpp_option.dump_includes)) 465 gfc_fatal_error ("To enable preprocessing, use %<-cpp%>"); 466 467 if (!gfc_cpp_enabled ()) 468 return; 469 470 cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table); 471 gcc_assert (cpp_in); 472 473 /* The cpp_options-structure defines far more flags than those set here. 474 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for 475 inter-option dependencies that may need to be enforced. */ 476 cpp_option = cpp_get_options (cpp_in); 477 gcc_assert (cpp_option); 478 479 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */ 480 cpp_option->traditional = 1; 481 cpp_option->cplusplus_comments = 0; 482 483 cpp_option->cpp_pedantic = pedantic; 484 485 cpp_option->dollars_in_ident = flag_dollar_ok; 486 cpp_option->discard_comments = gfc_cpp_option.discard_comments; 487 cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp; 488 cpp_option->print_include_names = gfc_cpp_option.print_include_names; 489 cpp_option->preprocessed = gfc_option.flag_preprocessed; 490 cpp_option->warn_date_time = gfc_cpp_option.warn_date_time; 491 492 if (gfc_cpp_makedep ()) 493 { 494 cpp_option->deps.style = DEPS_USER; 495 cpp_option->deps.phony_targets = gfc_cpp_option.deps_phony; 496 cpp_option->deps.missing_files = gfc_cpp_option.deps_missing_are_generated; 497 498 /* -MF <arg> overrides -M[M]D. */ 499 if (gfc_cpp_option.deps_filename_user) 500 gfc_cpp_option.deps_filename = gfc_cpp_option.deps_filename_user; 501 } 502 503 if (gfc_cpp_option.working_directory == -1) 504 gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE); 505 506 cpp_post_options (cpp_in); 507 508 gfc_cpp_register_include_paths (); 509} 510 511 512void 513gfc_cpp_init_0 (void) 514{ 515 struct cpp_callbacks *cb; 516 517 cb = cpp_get_callbacks (cpp_in); 518 cb->file_change = cb_file_change; 519 cb->line_change = cb_line_change; 520 cb->ident = cb_ident; 521 cb->def_pragma = cb_def_pragma; 522 cb->error = cb_cpp_error; 523 524 if (gfc_cpp_option.dump_includes) 525 cb->include = cb_include; 526 527 if ((gfc_cpp_option.dump_macros == 'D') 528 || (gfc_cpp_option.dump_macros == 'N')) 529 { 530 cb->define = cb_define; 531 cb->undef = cb_undef; 532 } 533 534 if (gfc_cpp_option.dump_macros == 'U') 535 { 536 cb->before_define = dump_queued_macros; 537 cb->used_define = cb_used_define; 538 cb->used_undef = cb_used_undef; 539 } 540 541 /* Initialize the print structure. Setting print.src_line to -1 here is 542 a trick to guarantee that the first token of the file will cause 543 a linemarker to be output by maybe_print_line. */ 544 print.src_line = -1; 545 print.printed = 0; 546 print.prev = 0; 547 print.first_time = 1; 548 549 if (gfc_cpp_preprocess_only ()) 550 { 551 if (gfc_cpp_option.output_filename) 552 { 553 /* This needs cheating: with "-E -o <file>", the user wants the 554 preprocessed output in <file>. However, if nothing is done 555 about it <file> is also used for assembler output. Hence, it 556 is necessary to redirect assembler output (actually nothing 557 as -E implies -fsyntax-only) to another file, otherwise the 558 output from preprocessing is lost. */ 559 asm_file_name = gfc_cpp_option.temporary_filename; 560 561 print.outf = fopen (gfc_cpp_option.output_filename, "w"); 562 if (print.outf == NULL) 563 gfc_fatal_error ("opening output file %qs: %s", 564 gfc_cpp_option.output_filename, 565 xstrerror (errno)); 566 } 567 else 568 print.outf = stdout; 569 } 570 else 571 { 572 print.outf = fopen (gfc_cpp_option.temporary_filename, "w"); 573 if (print.outf == NULL) 574 gfc_fatal_error ("opening output file %qs: %s", 575 gfc_cpp_option.temporary_filename, xstrerror (errno)); 576 } 577 578 gcc_assert(cpp_in); 579 if (!cpp_read_main_file (cpp_in, gfc_source_file)) 580 errorcount++; 581} 582 583void 584gfc_cpp_init (void) 585{ 586 int i; 587 588 if (gfc_option.flag_preprocessed) 589 return; 590 591 cpp_change_file (cpp_in, LC_RENAME, _("<built-in>")); 592 if (!gfc_cpp_option.no_predefined) 593 { 594 /* Make sure all of the builtins about to be declared have 595 BUILTINS_LOCATION has their source_location. */ 596 source_location builtins_loc = BUILTINS_LOCATION; 597 cpp_force_token_locations (cpp_in, &builtins_loc); 598 599 cpp_define_builtins (cpp_in); 600 601 cpp_stop_forcing_token_locations (cpp_in); 602 } 603 604 /* Handle deferred options from command-line. */ 605 cpp_change_file (cpp_in, LC_RENAME, _("<command-line>")); 606 607 for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++) 608 { 609 gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i]; 610 611 if (opt->code == OPT_D) 612 cpp_define (cpp_in, opt->arg); 613 else if (opt->code == OPT_U) 614 cpp_undef (cpp_in, opt->arg); 615 else if (opt->code == OPT_A) 616 { 617 if (opt->arg[0] == '-') 618 cpp_unassert (cpp_in, opt->arg + 1); 619 else 620 cpp_assert (cpp_in, opt->arg); 621 } 622 else if (opt->code == OPT_MT || opt->code == OPT_MQ) 623 deps_add_target (cpp_get_deps (cpp_in), 624 opt->arg, opt->code == OPT_MQ); 625 } 626 627 if (gfc_cpp_option.working_directory 628 && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands) 629 pp_dir_change (cpp_in, get_src_pwd ()); 630} 631 632bool 633gfc_cpp_preprocess (const char *source_file) 634{ 635 if (!gfc_cpp_enabled ()) 636 return false; 637 638 cpp_change_file (cpp_in, LC_RENAME, source_file); 639 640 if (cpp_option->traditional) 641 scan_translation_unit_trad (cpp_in); 642 else 643 scan_translation_unit (cpp_in); 644 645 /* -dM command line option. */ 646 if (gfc_cpp_preprocess_only () && 647 gfc_cpp_option.dump_macros == 'M') 648 { 649 putc ('\n', print.outf); 650 cpp_forall_identifiers (cpp_in, dump_macro, NULL); 651 } 652 653 putc ('\n', print.outf); 654 655 if (!gfc_cpp_preprocess_only () 656 || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename)) 657 fclose (print.outf); 658 659 return true; 660} 661 662void 663gfc_cpp_done (void) 664{ 665 if (!gfc_cpp_enabled ()) 666 return; 667 668 gcc_assert (cpp_in); 669 670 if (gfc_cpp_makedep ()) 671 { 672 if (gfc_cpp_option.deps_filename) 673 { 674 FILE *f = fopen (gfc_cpp_option.deps_filename, "w"); 675 if (f) 676 { 677 cpp_finish (cpp_in, f); 678 fclose (f); 679 } 680 else 681 gfc_fatal_error ("opening output file %qs: %s", 682 gfc_cpp_option.deps_filename, 683 xstrerror (errno)); 684 } 685 else 686 cpp_finish (cpp_in, stdout); 687 } 688 689 cpp_undef_all (cpp_in); 690 cpp_clear_file_cache (cpp_in); 691} 692 693/* PATH must be malloc-ed and NULL-terminated. */ 694void 695gfc_cpp_add_include_path (char *path, bool user_supplied) 696{ 697 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system 698 include path. Fortran does not define any system include paths. */ 699 int cxx_aware = 0; 700 701 add_path (path, BRACKET, cxx_aware, user_supplied); 702} 703 704void 705gfc_cpp_add_include_path_after (char *path, bool user_supplied) 706{ 707 int cxx_aware = 0; 708 add_path (path, AFTER, cxx_aware, user_supplied); 709} 710 711void 712gfc_cpp_register_include_paths (void) 713{ 714 int cxx_stdinc = 0; 715 register_include_chains (cpp_in, gfc_cpp_option.sysroot, 716 gfc_cpp_option.prefix, gfc_cpp_option.multilib, 717 gfc_cpp_option.standard_include_paths, cxx_stdinc, 718 gfc_cpp_option.verbose); 719} 720 721 722 723static void scan_translation_unit_trad (cpp_reader *); 724static void account_for_newlines (const unsigned char *, size_t); 725static int dump_macro (cpp_reader *, cpp_hashnode *, void *); 726 727static void print_line (source_location, const char *); 728static void maybe_print_line (source_location); 729 730 731/* Writes out the preprocessed file, handling spacing and paste 732 avoidance issues. */ 733static void 734scan_translation_unit (cpp_reader *pfile) 735{ 736 bool avoid_paste = false; 737 738 print.source = NULL; 739 for (;;) 740 { 741 const cpp_token *token = cpp_get_token (pfile); 742 743 if (token->type == CPP_PADDING) 744 { 745 avoid_paste = true; 746 if (print.source == NULL 747 || (!(print.source->flags & PREV_WHITE) 748 && token->val.source == NULL)) 749 print.source = token->val.source; 750 continue; 751 } 752 753 if (token->type == CPP_EOF) 754 break; 755 756 /* Subtle logic to output a space if and only if necessary. */ 757 if (avoid_paste) 758 { 759 if (print.source == NULL) 760 print.source = token; 761 if (print.source->flags & PREV_WHITE 762 || (print.prev 763 && cpp_avoid_paste (pfile, print.prev, token)) 764 || (print.prev == NULL && token->type == CPP_HASH)) 765 putc (' ', print.outf); 766 } 767 else if (token->flags & PREV_WHITE) 768 putc (' ', print.outf); 769 770 avoid_paste = false; 771 print.source = NULL; 772 print.prev = token; 773 cpp_output_token (token, print.outf); 774 775 if (token->type == CPP_COMMENT) 776 account_for_newlines (token->val.str.text, token->val.str.len); 777 } 778} 779 780/* Adjust print.src_line for newlines embedded in output. */ 781static void 782account_for_newlines (const unsigned char *str, size_t len) 783{ 784 while (len--) 785 if (*str++ == '\n') 786 print.src_line++; 787} 788 789/* Writes out a traditionally preprocessed file. */ 790static void 791scan_translation_unit_trad (cpp_reader *pfile) 792{ 793 while (_cpp_read_logical_line_trad (pfile)) 794 { 795 size_t len = pfile->out.cur - pfile->out.base; 796 maybe_print_line (pfile->out.first_line); 797 fwrite (pfile->out.base, 1, len, print.outf); 798 print.printed = 1; 799 if (!CPP_OPTION (pfile, discard_comments)) 800 account_for_newlines (pfile->out.base, len); 801 } 802} 803 804/* If the token read on logical line LINE needs to be output on a 805 different line to the current one, output the required newlines or 806 a line marker. */ 807static void 808maybe_print_line (source_location src_loc) 809{ 810 const struct line_map *map = linemap_lookup (line_table, src_loc); 811 int src_line = SOURCE_LINE (map, src_loc); 812 813 /* End the previous line of text. */ 814 if (print.printed) 815 { 816 putc ('\n', print.outf); 817 print.src_line++; 818 print.printed = 0; 819 } 820 821 if (src_line >= print.src_line && src_line < print.src_line + 8) 822 { 823 while (src_line > print.src_line) 824 { 825 putc ('\n', print.outf); 826 print.src_line++; 827 } 828 } 829 else 830 print_line (src_loc, ""); 831} 832 833/* Output a line marker for logical line LINE. Special flags are "1" 834 or "2" indicating entering or leaving a file. */ 835static void 836print_line (source_location src_loc, const char *special_flags) 837{ 838 /* End any previous line of text. */ 839 if (print.printed) 840 putc ('\n', print.outf); 841 print.printed = 0; 842 843 if (!gfc_cpp_option.no_line_commands) 844 { 845 expanded_location loc; 846 size_t to_file_len; 847 unsigned char *to_file_quoted; 848 unsigned char *p; 849 int sysp; 850 851 loc = expand_location (src_loc); 852 to_file_len = strlen (loc.file); 853 to_file_quoted = (unsigned char *) alloca (to_file_len * 4 + 1); 854 855 print.src_line = loc.line; 856 857 /* cpp_quote_string does not nul-terminate, so we have to do it 858 ourselves. */ 859 p = cpp_quote_string (to_file_quoted, 860 (const unsigned char *) loc.file, to_file_len); 861 *p = '\0'; 862 fprintf (print.outf, "# %u \"%s\"%s", 863 print.src_line == 0 ? 1 : print.src_line, 864 to_file_quoted, special_flags); 865 866 sysp = in_system_header_at (src_loc); 867 if (sysp == 2) 868 fputs (" 3 4", print.outf); 869 else if (sysp == 1) 870 fputs (" 3", print.outf); 871 872 putc ('\n', print.outf); 873 } 874} 875 876static void 877cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map) 878{ 879 const char *flags = ""; 880 881 if (gfc_cpp_option.no_line_commands) 882 return; 883 884 if (!map) 885 return; 886 887 if (print.first_time) 888 { 889 /* Avoid printing foo.i when the main file is foo.c. */ 890 if (!cpp_get_options (cpp_in)->preprocessed) 891 print_line (map->start_location, flags); 892 print.first_time = 0; 893 } 894 else 895 { 896 /* Bring current file to correct line when entering a new file. */ 897 if (map->reason == LC_ENTER) 898 { 899 const struct line_map *from = INCLUDED_FROM (line_table, map); 900 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from)); 901 } 902 if (map->reason == LC_ENTER) 903 flags = " 1"; 904 else if (map->reason == LC_LEAVE) 905 flags = " 2"; 906 print_line (map->start_location, flags); 907 } 908 909} 910 911/* Called when a line of output is started. TOKEN is the first token 912 of the line, and at end of file will be CPP_EOF. */ 913static void 914cb_line_change (cpp_reader *pfile, const cpp_token *token, 915 int parsing_args) 916{ 917 source_location src_loc = token->src_loc; 918 919 if (token->type == CPP_EOF || parsing_args) 920 return; 921 922 maybe_print_line (src_loc); 923 print.prev = 0; 924 print.source = 0; 925 926 /* Supply enough spaces to put this token in its original column, 927 one space per column greater than 2, since scan_translation_unit 928 will provide a space if PREV_WHITE. Don't bother trying to 929 reconstruct tabs; we can't get it right in general, and nothing 930 ought to care. Some things do care; the fault lies with them. */ 931 if (!CPP_OPTION (pfile, traditional)) 932 { 933 const struct line_map *map = linemap_lookup (line_table, src_loc); 934 int spaces = SOURCE_COLUMN (map, src_loc) - 2; 935 print.printed = 1; 936 937 while (-- spaces >= 0) 938 putc (' ', print.outf); 939 } 940} 941 942static void 943cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 944 const cpp_string *str) 945{ 946 maybe_print_line (line); 947 fprintf (print.outf, "#ident %s\n", str->text); 948 print.src_line++; 949} 950 951static void 952cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 953 cpp_hashnode *node ATTRIBUTE_UNUSED) 954{ 955 maybe_print_line (line); 956 fputs ("#define ", print.outf); 957 958 /* 'D' is whole definition; 'N' is name only. */ 959 if (gfc_cpp_option.dump_macros == 'D') 960 fputs ((const char *) cpp_macro_definition (pfile, node), 961 print.outf); 962 else 963 fputs ((const char *) NODE_NAME (node), print.outf); 964 965 putc ('\n', print.outf); 966 if (LOCATION_LINE (line) != 0) 967 print.src_line++; 968} 969 970static void 971cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 972 cpp_hashnode *node) 973{ 974 maybe_print_line (line); 975 fprintf (print.outf, "#undef %s\n", NODE_NAME (node)); 976 print.src_line++; 977} 978 979static void 980cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 981 const unsigned char *dir, const char *header, int angle_brackets, 982 const cpp_token **comments) 983{ 984 maybe_print_line (line); 985 if (angle_brackets) 986 fprintf (print.outf, "#%s <%s>", dir, header); 987 else 988 fprintf (print.outf, "#%s \"%s\"", dir, header); 989 990 if (comments != NULL) 991 { 992 while (*comments != NULL) 993 { 994 if ((*comments)->flags & PREV_WHITE) 995 putc (' ', print.outf); 996 cpp_output_token (*comments, print.outf); 997 ++comments; 998 } 999 } 1000 1001 putc ('\n', print.outf); 1002 print.src_line++; 1003} 1004 1005/* Dump out the hash table. */ 1006static int 1007dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED) 1008{ 1009 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) 1010 { 1011 fputs ("#define ", print.outf); 1012 fputs ((const char *) cpp_macro_definition (pfile, node), 1013 print.outf); 1014 putc ('\n', print.outf); 1015 print.src_line++; 1016 } 1017 1018 return 1; 1019} 1020 1021static void 1022cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED, 1023 cpp_hashnode *node) 1024{ 1025 gfc_cpp_macro_queue *q; 1026 q = XNEW (gfc_cpp_macro_queue); 1027 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node)); 1028 q->next = cpp_define_queue; 1029 cpp_define_queue = q; 1030} 1031 1032/* Callback from cpp_error for PFILE to print diagnostics from the 1033 preprocessor. The diagnostic is of type LEVEL, with REASON set 1034 to the reason code if LEVEL is represents a warning, at location 1035 LOCATION, with column number possibly overridden by COLUMN_OVERRIDE 1036 if not zero; MSG is the translated message and AP the arguments. 1037 Returns true if a diagnostic was emitted, false otherwise. */ 1038 1039static bool 1040cb_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, 1041 location_t location, unsigned int column_override, 1042 const char *msg, va_list *ap) 1043{ 1044 diagnostic_info diagnostic; 1045 diagnostic_t dlevel; 1046 bool save_warn_system_headers = global_dc->dc_warn_system_headers; 1047 bool ret; 1048 1049 switch (level) 1050 { 1051 case CPP_DL_WARNING_SYSHDR: 1052 global_dc->dc_warn_system_headers = 1; 1053 /* Fall through. */ 1054 case CPP_DL_WARNING: 1055 dlevel = DK_WARNING; 1056 break; 1057 case CPP_DL_PEDWARN: 1058 dlevel = DK_PEDWARN; 1059 break; 1060 case CPP_DL_ERROR: 1061 dlevel = DK_ERROR; 1062 break; 1063 case CPP_DL_ICE: 1064 dlevel = DK_ICE; 1065 break; 1066 case CPP_DL_NOTE: 1067 dlevel = DK_NOTE; 1068 break; 1069 case CPP_DL_FATAL: 1070 dlevel = DK_FATAL; 1071 break; 1072 default: 1073 gcc_unreachable (); 1074 } 1075 diagnostic_set_info_translated (&diagnostic, msg, ap, 1076 location, dlevel); 1077 if (column_override) 1078 diagnostic_override_column (&diagnostic, column_override); 1079 if (reason == CPP_W_WARNING_DIRECTIVE) 1080 diagnostic_override_option_index (&diagnostic, OPT_Wcpp); 1081 ret = report_diagnostic (&diagnostic); 1082 if (level == CPP_DL_WARNING_SYSHDR) 1083 global_dc->dc_warn_system_headers = save_warn_system_headers; 1084 return ret; 1085} 1086 1087/* Callback called when -fworking-director and -E to emit working 1088 directory in cpp output file. */ 1089 1090void 1091pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir) 1092{ 1093 size_t to_file_len = strlen (dir); 1094 unsigned char *to_file_quoted = 1095 (unsigned char *) alloca (to_file_len * 4 + 1); 1096 unsigned char *p; 1097 1098 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */ 1099 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len); 1100 *p = '\0'; 1101 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted); 1102} 1103 1104/* Copy a #pragma directive to the preprocessed output. */ 1105static void 1106cb_def_pragma (cpp_reader *pfile, source_location line) 1107{ 1108 maybe_print_line (line); 1109 fputs ("#pragma ", print.outf); 1110 cpp_output_line (pfile, print.outf); 1111 print.src_line++; 1112} 1113 1114static void 1115cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, 1116 source_location line ATTRIBUTE_UNUSED, 1117 cpp_hashnode *node) 1118{ 1119 gfc_cpp_macro_queue *q; 1120 q = XNEW (gfc_cpp_macro_queue); 1121 q->macro = xstrdup ((const char *) NODE_NAME (node)); 1122 q->next = cpp_undefine_queue; 1123 cpp_undefine_queue = q; 1124} 1125 1126static void 1127dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED) 1128{ 1129 gfc_cpp_macro_queue *q; 1130 1131 /* End the previous line of text. */ 1132 if (print.printed) 1133 { 1134 putc ('\n', print.outf); 1135 print.src_line++; 1136 print.printed = 0; 1137 } 1138 1139 for (q = cpp_define_queue; q;) 1140 { 1141 gfc_cpp_macro_queue *oq; 1142 fputs ("#define ", print.outf); 1143 fputs (q->macro, print.outf); 1144 putc ('\n', print.outf); 1145 print.src_line++; 1146 oq = q; 1147 q = q->next; 1148 free (oq->macro); 1149 free (oq); 1150 } 1151 cpp_define_queue = NULL; 1152 for (q = cpp_undefine_queue; q;) 1153 { 1154 gfc_cpp_macro_queue *oq; 1155 fprintf (print.outf, "#undef %s\n", q->macro); 1156 print.src_line++; 1157 oq = q; 1158 q = q->next; 1159 free (oq->macro); 1160 free (oq); 1161 } 1162 cpp_undefine_queue = NULL; 1163} 1164