1 /* Preprocess only, using cpplib. 2 Copyright (C) 1995-2013 Free Software Foundation, Inc. 3 Written by Per Bothner, 1994-95. 4 5 This program is free software; you can redistribute it and/or modify it 6 under the terms of the GNU General Public License as published by the 7 Free Software Foundation; either version 3, or (at your option) any 8 later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; 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 "cpplib.h" 23 #include "../libcpp/internal.h" 24 #include "tree.h" 25 #include "c-common.h" /* For flags. */ 26 #include "c-pragma.h" /* For parse_in. */ 27 28 /* Encapsulates state used to convert a stream of tokens into a text 29 file. */ 30 static struct 31 { 32 FILE *outf; /* Stream to write to. */ 33 const cpp_token *prev; /* Previous token. */ 34 const cpp_token *source; /* Source token for spacing. */ 35 int src_line; /* Line number currently being written. */ 36 unsigned char printed; /* Nonzero if something output at line. */ 37 bool first_time; /* pp_file_change hasn't been called yet. */ 38 const char *src_file; /* Current source file. */ 39 } print; 40 41 /* Defined and undefined macros being queued for output with -dU at 42 the next newline. */ 43 typedef struct macro_queue 44 { 45 struct macro_queue *next; /* Next macro in the list. */ 46 char *macro; /* The name of the macro if not 47 defined, the full definition if 48 defined. */ 49 } macro_queue; 50 static macro_queue *define_queue, *undef_queue; 51 52 /* General output routines. */ 53 static void scan_translation_unit (cpp_reader *); 54 static void print_lines_directives_only (int, const void *, size_t); 55 static void scan_translation_unit_directives_only (cpp_reader *); 56 static void scan_translation_unit_trad (cpp_reader *); 57 static void account_for_newlines (const unsigned char *, size_t); 58 static int dump_macro (cpp_reader *, cpp_hashnode *, void *); 59 static void dump_queued_macros (cpp_reader *); 60 61 static void print_line_1 (source_location, const char*, FILE *); 62 static void print_line (source_location, const char *); 63 static void maybe_print_line_1 (source_location, FILE *); 64 static void maybe_print_line (source_location); 65 static void do_line_change (cpp_reader *, const cpp_token *, 66 source_location, int); 67 68 /* Callback routines for the parser. Most of these are active only 69 in specific modes. */ 70 static void cb_line_change (cpp_reader *, const cpp_token *, int); 71 static void cb_define (cpp_reader *, source_location, cpp_hashnode *); 72 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *); 73 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *); 74 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *); 75 static void cb_include (cpp_reader *, source_location, const unsigned char *, 76 const char *, int, const cpp_token **); 77 static void cb_ident (cpp_reader *, source_location, const cpp_string *); 78 static void cb_def_pragma (cpp_reader *, source_location); 79 static void cb_read_pch (cpp_reader *pfile, const char *name, 80 int fd, const char *orig_name); 81 82 /* Preprocess and output. */ 83 void 84 preprocess_file (cpp_reader *pfile) 85 { 86 /* A successful cpp_read_main_file guarantees that we can call 87 cpp_scan_nooutput or cpp_get_token next. */ 88 if (flag_no_output && pfile->buffer) 89 { 90 /* Scan -included buffers, then the main file. */ 91 while (pfile->buffer->prev) 92 cpp_scan_nooutput (pfile); 93 cpp_scan_nooutput (pfile); 94 } 95 else if (cpp_get_options (pfile)->traditional) 96 scan_translation_unit_trad (pfile); 97 else if (cpp_get_options (pfile)->directives_only 98 && !cpp_get_options (pfile)->preprocessed) 99 scan_translation_unit_directives_only (pfile); 100 else 101 scan_translation_unit (pfile); 102 103 /* -dM command line option. Should this be elsewhere? */ 104 if (flag_dump_macros == 'M') 105 cpp_forall_identifiers (pfile, dump_macro, NULL); 106 107 /* Flush any pending output. */ 108 if (print.printed) 109 putc ('\n', print.outf); 110 } 111 112 /* Set up the callbacks as appropriate. */ 113 void 114 init_pp_output (FILE *out_stream) 115 { 116 cpp_callbacks *cb = cpp_get_callbacks (parse_in); 117 118 if (!flag_no_output) 119 { 120 cb->line_change = cb_line_change; 121 /* Don't emit #pragma or #ident directives if we are processing 122 assembly language; the assembler may choke on them. */ 123 if (cpp_get_options (parse_in)->lang != CLK_ASM) 124 { 125 cb->ident = cb_ident; 126 cb->def_pragma = cb_def_pragma; 127 } 128 } 129 130 if (flag_dump_includes) 131 cb->include = cb_include; 132 133 if (flag_pch_preprocess) 134 { 135 cb->valid_pch = c_common_valid_pch; 136 cb->read_pch = cb_read_pch; 137 } 138 139 if (flag_dump_macros == 'N' || flag_dump_macros == 'D') 140 { 141 cb->define = cb_define; 142 cb->undef = cb_undef; 143 } 144 145 if (flag_dump_macros == 'U') 146 { 147 cb->before_define = dump_queued_macros; 148 cb->used_define = cb_used_define; 149 cb->used_undef = cb_used_undef; 150 } 151 152 /* Initialize the print structure. */ 153 print.src_line = 1; 154 print.printed = 0; 155 print.prev = 0; 156 print.outf = out_stream; 157 print.first_time = 1; 158 print.src_file = ""; 159 } 160 161 /* Writes out the preprocessed file, handling spacing and paste 162 avoidance issues. */ 163 static void 164 scan_translation_unit (cpp_reader *pfile) 165 { 166 bool avoid_paste = false; 167 bool do_line_adjustments 168 = cpp_get_options (parse_in)->lang != CLK_ASM 169 && !flag_no_line_commands; 170 bool in_pragma = false; 171 172 print.source = NULL; 173 for (;;) 174 { 175 source_location loc; 176 const cpp_token *token = cpp_get_token_with_location (pfile, &loc); 177 178 if (token->type == CPP_PADDING) 179 { 180 avoid_paste = true; 181 if (print.source == NULL 182 || (!(print.source->flags & PREV_WHITE) 183 && token->val.source == NULL)) 184 print.source = token->val.source; 185 continue; 186 } 187 188 if (token->type == CPP_EOF) 189 break; 190 191 /* Subtle logic to output a space if and only if necessary. */ 192 if (avoid_paste) 193 { 194 int src_line = LOCATION_LINE (loc); 195 196 if (print.source == NULL) 197 print.source = token; 198 199 if (src_line != print.src_line 200 && do_line_adjustments 201 && !in_pragma) 202 { 203 do_line_change (pfile, token, loc, false); 204 putc (' ', print.outf); 205 } 206 else if (print.source->flags & PREV_WHITE 207 || (print.prev 208 && cpp_avoid_paste (pfile, print.prev, token)) 209 || (print.prev == NULL && token->type == CPP_HASH)) 210 putc (' ', print.outf); 211 } 212 else if (token->flags & PREV_WHITE) 213 { 214 int src_line = LOCATION_LINE (loc); 215 216 if (src_line != print.src_line 217 && do_line_adjustments 218 && !in_pragma) 219 do_line_change (pfile, token, loc, false); 220 putc (' ', print.outf); 221 } 222 223 avoid_paste = false; 224 print.source = NULL; 225 print.prev = token; 226 if (token->type == CPP_PRAGMA) 227 { 228 const char *space; 229 const char *name; 230 231 maybe_print_line (token->src_loc); 232 fputs ("#pragma ", print.outf); 233 c_pp_lookup_pragma (token->val.pragma, &space, &name); 234 if (space) 235 fprintf (print.outf, "%s %s", space, name); 236 else 237 fprintf (print.outf, "%s", name); 238 print.printed = 1; 239 in_pragma = true; 240 } 241 else if (token->type == CPP_PRAGMA_EOL) 242 { 243 maybe_print_line (token->src_loc); 244 in_pragma = false; 245 } 246 else 247 { 248 if (cpp_get_options (parse_in)->debug) 249 linemap_dump_location (line_table, token->src_loc, 250 print.outf); 251 cpp_output_token (token, print.outf); 252 } 253 254 if (token->type == CPP_COMMENT) 255 account_for_newlines (token->val.str.text, token->val.str.len); 256 } 257 } 258 259 static void 260 print_lines_directives_only (int lines, const void *buf, size_t size) 261 { 262 print.src_line += lines; 263 fwrite (buf, 1, size, print.outf); 264 } 265 266 /* Writes out the preprocessed file, handling spacing and paste 267 avoidance issues. */ 268 static void 269 scan_translation_unit_directives_only (cpp_reader *pfile) 270 { 271 struct _cpp_dir_only_callbacks cb; 272 273 cb.print_lines = print_lines_directives_only; 274 cb.maybe_print_line = maybe_print_line; 275 276 _cpp_preprocess_dir_only (pfile, &cb); 277 } 278 279 /* Adjust print.src_line for newlines embedded in output. */ 280 static void 281 account_for_newlines (const unsigned char *str, size_t len) 282 { 283 while (len--) 284 if (*str++ == '\n') 285 print.src_line++; 286 } 287 288 /* Writes out a traditionally preprocessed file. */ 289 static void 290 scan_translation_unit_trad (cpp_reader *pfile) 291 { 292 while (_cpp_read_logical_line_trad (pfile)) 293 { 294 size_t len = pfile->out.cur - pfile->out.base; 295 maybe_print_line (pfile->out.first_line); 296 fwrite (pfile->out.base, 1, len, print.outf); 297 print.printed = 1; 298 if (!CPP_OPTION (pfile, discard_comments)) 299 account_for_newlines (pfile->out.base, len); 300 } 301 } 302 303 /* If the token read on logical line LINE needs to be output on a 304 different line to the current one, output the required newlines or 305 a line marker, and return 1. Otherwise return 0. */ 306 307 static void 308 maybe_print_line_1 (source_location src_loc, FILE *stream) 309 { 310 int src_line = LOCATION_LINE (src_loc); 311 const char *src_file = LOCATION_FILE (src_loc); 312 313 /* End the previous line of text. */ 314 if (print.printed) 315 { 316 putc ('\n', stream); 317 print.src_line++; 318 print.printed = 0; 319 } 320 321 if (!flag_no_line_commands 322 && src_line >= print.src_line 323 && src_line < print.src_line + 8 324 && strcmp (src_file, print.src_file) == 0) 325 { 326 while (src_line > print.src_line) 327 { 328 putc ('\n', stream); 329 print.src_line++; 330 } 331 } 332 else 333 print_line_1 (src_loc, "", stream); 334 335 } 336 337 /* If the token read on logical line LINE needs to be output on a 338 different line to the current one, output the required newlines or 339 a line marker, and return 1. Otherwise return 0. */ 340 341 static void 342 maybe_print_line (source_location src_loc) 343 { 344 if (cpp_get_options (parse_in)->debug) 345 linemap_dump_location (line_table, src_loc, 346 print.outf); 347 maybe_print_line_1 (src_loc, print.outf); 348 } 349 350 /* Output a line marker for logical line LINE. Special flags are "1" 351 or "2" indicating entering or leaving a file. */ 352 353 static void 354 print_line_1 (source_location src_loc, const char *special_flags, FILE *stream) 355 { 356 /* End any previous line of text. */ 357 if (print.printed) 358 putc ('\n', stream); 359 print.printed = 0; 360 361 if (!flag_no_line_commands) 362 { 363 const char *file_path = LOCATION_FILE (src_loc); 364 int sysp; 365 size_t to_file_len = strlen (file_path); 366 unsigned char *to_file_quoted = 367 (unsigned char *) alloca (to_file_len * 4 + 1); 368 unsigned char *p; 369 370 print.src_line = LOCATION_LINE (src_loc); 371 print.src_file = file_path; 372 373 /* cpp_quote_string does not nul-terminate, so we have to do it 374 ourselves. */ 375 p = cpp_quote_string (to_file_quoted, 376 (const unsigned char *) file_path, 377 to_file_len); 378 *p = '\0'; 379 fprintf (stream, "# %u \"%s\"%s", 380 print.src_line == 0 ? 1 : print.src_line, 381 to_file_quoted, special_flags); 382 383 sysp = in_system_header_at (src_loc); 384 if (sysp == 2) 385 fputs (" 3 4", stream); 386 else if (sysp == 1) 387 fputs (" 3", stream); 388 389 putc ('\n', stream); 390 } 391 } 392 393 /* Output a line marker for logical line LINE. Special flags are "1" 394 or "2" indicating entering or leaving a file. */ 395 396 static void 397 print_line (source_location src_loc, const char *special_flags) 398 { 399 if (cpp_get_options (parse_in)->debug) 400 linemap_dump_location (line_table, src_loc, 401 print.outf); 402 print_line_1 (src_loc, special_flags, print.outf); 403 } 404 405 /* Helper function for cb_line_change and scan_translation_unit. */ 406 static void 407 do_line_change (cpp_reader *pfile, const cpp_token *token, 408 source_location src_loc, int parsing_args) 409 { 410 if (define_queue || undef_queue) 411 dump_queued_macros (pfile); 412 413 if (token->type == CPP_EOF || parsing_args) 414 return; 415 416 maybe_print_line (src_loc); 417 print.prev = 0; 418 print.source = 0; 419 420 /* Supply enough spaces to put this token in its original column, 421 one space per column greater than 2, since scan_translation_unit 422 will provide a space if PREV_WHITE. Don't bother trying to 423 reconstruct tabs; we can't get it right in general, and nothing 424 ought to care. Some things do care; the fault lies with them. */ 425 if (!CPP_OPTION (pfile, traditional)) 426 { 427 int spaces = LOCATION_COLUMN (src_loc) - 2; 428 print.printed = 1; 429 430 while (-- spaces >= 0) 431 putc (' ', print.outf); 432 } 433 } 434 435 /* Called when a line of output is started. TOKEN is the first token 436 of the line, and at end of file will be CPP_EOF. */ 437 static void 438 cb_line_change (cpp_reader *pfile, const cpp_token *token, 439 int parsing_args) 440 { 441 do_line_change (pfile, token, token->src_loc, parsing_args); 442 } 443 444 static void 445 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 446 const cpp_string *str) 447 { 448 maybe_print_line (line); 449 fprintf (print.outf, "#ident %s\n", str->text); 450 print.src_line++; 451 } 452 453 static void 454 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node) 455 { 456 const struct line_map *map; 457 458 maybe_print_line (line); 459 fputs ("#define ", print.outf); 460 461 /* 'D' is whole definition; 'N' is name only. */ 462 if (flag_dump_macros == 'D') 463 fputs ((const char *) cpp_macro_definition (pfile, node), 464 print.outf); 465 else 466 fputs ((const char *) NODE_NAME (node), print.outf); 467 468 putc ('\n', print.outf); 469 linemap_resolve_location (line_table, line, 470 LRK_MACRO_DEFINITION_LOCATION, 471 &map); 472 if (LINEMAP_LINE (map) != 0) 473 print.src_line++; 474 } 475 476 static void 477 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 478 cpp_hashnode *node) 479 { 480 maybe_print_line (line); 481 fprintf (print.outf, "#undef %s\n", NODE_NAME (node)); 482 print.src_line++; 483 } 484 485 static void 486 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED, 487 cpp_hashnode *node) 488 { 489 macro_queue *q; 490 if (node->flags & NODE_BUILTIN) 491 return; 492 q = XNEW (macro_queue); 493 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node)); 494 q->next = define_queue; 495 define_queue = q; 496 } 497 498 static void 499 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, 500 source_location line ATTRIBUTE_UNUSED, 501 cpp_hashnode *node) 502 { 503 macro_queue *q; 504 q = XNEW (macro_queue); 505 q->macro = xstrdup ((const char *) NODE_NAME (node)); 506 q->next = undef_queue; 507 undef_queue = q; 508 } 509 510 static void 511 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED) 512 { 513 macro_queue *q; 514 515 /* End the previous line of text. */ 516 if (print.printed) 517 { 518 putc ('\n', print.outf); 519 print.src_line++; 520 print.printed = 0; 521 } 522 523 for (q = define_queue; q;) 524 { 525 macro_queue *oq; 526 fputs ("#define ", print.outf); 527 fputs (q->macro, print.outf); 528 putc ('\n', print.outf); 529 print.src_line++; 530 oq = q; 531 q = q->next; 532 free (oq->macro); 533 free (oq); 534 } 535 define_queue = NULL; 536 for (q = undef_queue; q;) 537 { 538 macro_queue *oq; 539 fprintf (print.outf, "#undef %s\n", q->macro); 540 print.src_line++; 541 oq = q; 542 q = q->next; 543 free (oq->macro); 544 free (oq); 545 } 546 undef_queue = NULL; 547 } 548 549 static void 550 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 551 const unsigned char *dir, const char *header, int angle_brackets, 552 const cpp_token **comments) 553 { 554 maybe_print_line (line); 555 if (angle_brackets) 556 fprintf (print.outf, "#%s <%s>", dir, header); 557 else 558 fprintf (print.outf, "#%s \"%s\"", dir, header); 559 560 if (comments != NULL) 561 { 562 while (*comments != NULL) 563 { 564 if ((*comments)->flags & PREV_WHITE) 565 putc (' ', print.outf); 566 cpp_output_token (*comments, print.outf); 567 ++comments; 568 } 569 } 570 571 putc ('\n', print.outf); 572 print.src_line++; 573 } 574 575 /* Callback called when -fworking-director and -E to emit working 576 directory in cpp output file. */ 577 578 void 579 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir) 580 { 581 size_t to_file_len = strlen (dir); 582 unsigned char *to_file_quoted = 583 (unsigned char *) alloca (to_file_len * 4 + 1); 584 unsigned char *p; 585 586 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */ 587 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len); 588 *p = '\0'; 589 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted); 590 } 591 592 /* The file name, line number or system header flags have changed, as 593 described in MAP. */ 594 595 void 596 pp_file_change (const struct line_map *map) 597 { 598 const char *flags = ""; 599 600 if (flag_no_line_commands) 601 return; 602 603 if (map != NULL) 604 { 605 input_location = map->start_location; 606 if (print.first_time) 607 { 608 /* Avoid printing foo.i when the main file is foo.c. */ 609 if (!cpp_get_options (parse_in)->preprocessed) 610 print_line (map->start_location, flags); 611 print.first_time = 0; 612 } 613 else 614 { 615 /* Bring current file to correct line when entering a new file. */ 616 if (map->reason == LC_ENTER) 617 { 618 const struct line_map *from = INCLUDED_FROM (line_table, map); 619 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from)); 620 } 621 if (map->reason == LC_ENTER) 622 flags = " 1"; 623 else if (map->reason == LC_LEAVE) 624 flags = " 2"; 625 print_line (map->start_location, flags); 626 } 627 } 628 } 629 630 /* Copy a #pragma directive to the preprocessed output. */ 631 static void 632 cb_def_pragma (cpp_reader *pfile, source_location line) 633 { 634 maybe_print_line (line); 635 fputs ("#pragma ", print.outf); 636 cpp_output_line (pfile, print.outf); 637 print.src_line++; 638 } 639 640 /* Dump out the hash table. */ 641 static int 642 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED) 643 { 644 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) 645 { 646 fputs ("#define ", print.outf); 647 fputs ((const char *) cpp_macro_definition (pfile, node), 648 print.outf); 649 putc ('\n', print.outf); 650 print.src_line++; 651 } 652 653 return 1; 654 } 655 656 /* Load in the PCH file NAME, open on FD. It was originally searched for 657 by ORIG_NAME. Also, print out a #include command so that the PCH 658 file can be loaded when the preprocessed output is compiled. */ 659 660 static void 661 cb_read_pch (cpp_reader *pfile, const char *name, 662 int fd, const char *orig_name ATTRIBUTE_UNUSED) 663 { 664 c_common_read_pch (pfile, name, fd, orig_name); 665 666 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name); 667 print.src_line++; 668 } 669