1132718Skan/* Preprocess only, using cpplib. 2132718Skan Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 3132718Skan Free Software Foundation, Inc. 4132718Skan Written by Per Bothner, 1994-95. 5132718Skan 6132718SkanThis program is free software; you can redistribute it and/or modify it 7132718Skanunder the terms of the GNU General Public License as published by the 8132718SkanFree Software Foundation; either version 2, or (at your option) any 9132718Skanlater version. 10132718Skan 11132718SkanThis program is distributed in the hope that it will be useful, 12132718Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 13132718SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14132718SkanGNU General Public License for more details. 15132718Skan 16132718SkanYou should have received a copy of the GNU General Public License 17132718Skanalong with this program; if not, write to the Free Software 18169689SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 19132718Skan 20132718Skan#include "config.h" 21132718Skan#include "system.h" 22132718Skan#include "coretypes.h" 23132718Skan#include "tm.h" 24132718Skan#include "cpplib.h" 25169689Skan#include "../libcpp/internal.h" 26132718Skan#include "tree.h" 27132718Skan#include "c-common.h" /* For flags. */ 28132718Skan#include "c-pragma.h" /* For parse_in. */ 29132718Skan 30132718Skan/* Encapsulates state used to convert a stream of tokens into a text 31132718Skan file. */ 32132718Skanstatic struct 33132718Skan{ 34132718Skan FILE *outf; /* Stream to write to. */ 35132718Skan const cpp_token *prev; /* Previous token. */ 36132718Skan const cpp_token *source; /* Source token for spacing. */ 37169689Skan int src_line; /* Line number currently being written. */ 38132718Skan unsigned char printed; /* Nonzero if something output at line. */ 39169689Skan bool first_time; /* pp_file_change hasn't been called yet. */ 40132718Skan} print; 41132718Skan 42132718Skan/* General output routines. */ 43132718Skanstatic void scan_translation_unit (cpp_reader *); 44259405Spfgstatic void print_lines_directives_only (int, const void *, size_t); 45259405Spfgstatic void scan_translation_unit_directives_only (cpp_reader *); 46132718Skanstatic void scan_translation_unit_trad (cpp_reader *); 47132718Skanstatic void account_for_newlines (const unsigned char *, size_t); 48132718Skanstatic int dump_macro (cpp_reader *, cpp_hashnode *, void *); 49132718Skan 50169689Skanstatic void print_line (source_location, const char *); 51169689Skanstatic void maybe_print_line (source_location); 52132718Skan 53132718Skan/* Callback routines for the parser. Most of these are active only 54132718Skan in specific modes. */ 55132718Skanstatic void cb_line_change (cpp_reader *, const cpp_token *, int); 56169689Skanstatic void cb_define (cpp_reader *, source_location, cpp_hashnode *); 57169689Skanstatic void cb_undef (cpp_reader *, source_location, cpp_hashnode *); 58169689Skanstatic void cb_include (cpp_reader *, source_location, const unsigned char *, 59169689Skan const char *, int, const cpp_token **); 60169689Skanstatic void cb_ident (cpp_reader *, source_location, const cpp_string *); 61169689Skanstatic void cb_def_pragma (cpp_reader *, source_location); 62169689Skanstatic void cb_read_pch (cpp_reader *pfile, const char *name, 63169689Skan int fd, const char *orig_name); 64132718Skan 65132718Skan/* Preprocess and output. */ 66132718Skanvoid 67132718Skanpreprocess_file (cpp_reader *pfile) 68132718Skan{ 69132718Skan /* A successful cpp_read_main_file guarantees that we can call 70132718Skan cpp_scan_nooutput or cpp_get_token next. */ 71132718Skan if (flag_no_output) 72132718Skan { 73132718Skan /* Scan -included buffers, then the main file. */ 74132718Skan while (pfile->buffer->prev) 75132718Skan cpp_scan_nooutput (pfile); 76132718Skan cpp_scan_nooutput (pfile); 77132718Skan } 78132718Skan else if (cpp_get_options (pfile)->traditional) 79132718Skan scan_translation_unit_trad (pfile); 80259405Spfg else if (cpp_get_options (pfile)->directives_only 81259405Spfg && !cpp_get_options (pfile)->preprocessed) 82259405Spfg scan_translation_unit_directives_only (pfile); 83132718Skan else 84132718Skan scan_translation_unit (pfile); 85132718Skan 86132718Skan /* -dM command line option. Should this be elsewhere? */ 87132718Skan if (flag_dump_macros == 'M') 88132718Skan cpp_forall_identifiers (pfile, dump_macro, NULL); 89132718Skan 90132718Skan /* Flush any pending output. */ 91132718Skan if (print.printed) 92132718Skan putc ('\n', print.outf); 93132718Skan} 94132718Skan 95132718Skan/* Set up the callbacks as appropriate. */ 96132718Skanvoid 97132718Skaninit_pp_output (FILE *out_stream) 98132718Skan{ 99132718Skan cpp_callbacks *cb = cpp_get_callbacks (parse_in); 100132718Skan 101132718Skan if (!flag_no_output) 102132718Skan { 103132718Skan cb->line_change = cb_line_change; 104132718Skan /* Don't emit #pragma or #ident directives if we are processing 105132718Skan assembly language; the assembler may choke on them. */ 106132718Skan if (cpp_get_options (parse_in)->lang != CLK_ASM) 107132718Skan { 108132718Skan cb->ident = cb_ident; 109132718Skan cb->def_pragma = cb_def_pragma; 110132718Skan } 111132718Skan } 112132718Skan 113132718Skan if (flag_dump_includes) 114132718Skan cb->include = cb_include; 115132718Skan 116169689Skan if (flag_pch_preprocess) 117169689Skan { 118169689Skan cb->valid_pch = c_common_valid_pch; 119169689Skan cb->read_pch = cb_read_pch; 120169689Skan } 121169689Skan 122132718Skan if (flag_dump_macros == 'N' || flag_dump_macros == 'D') 123132718Skan { 124132718Skan cb->define = cb_define; 125132718Skan cb->undef = cb_undef; 126132718Skan } 127132718Skan 128169689Skan /* Initialize the print structure. Setting print.src_line to -1 here is 129132718Skan a trick to guarantee that the first token of the file will cause 130132718Skan a linemarker to be output by maybe_print_line. */ 131169689Skan print.src_line = -1; 132132718Skan print.printed = 0; 133132718Skan print.prev = 0; 134132718Skan print.outf = out_stream; 135169689Skan print.first_time = 1; 136132718Skan} 137132718Skan 138132718Skan/* Writes out the preprocessed file, handling spacing and paste 139132718Skan avoidance issues. */ 140132718Skanstatic void 141132718Skanscan_translation_unit (cpp_reader *pfile) 142132718Skan{ 143132718Skan bool avoid_paste = false; 144132718Skan 145132718Skan print.source = NULL; 146132718Skan for (;;) 147132718Skan { 148132718Skan const cpp_token *token = cpp_get_token (pfile); 149132718Skan 150132718Skan if (token->type == CPP_PADDING) 151132718Skan { 152132718Skan avoid_paste = true; 153132718Skan if (print.source == NULL 154132718Skan || (!(print.source->flags & PREV_WHITE) 155132718Skan && token->val.source == NULL)) 156132718Skan print.source = token->val.source; 157132718Skan continue; 158132718Skan } 159132718Skan 160132718Skan if (token->type == CPP_EOF) 161132718Skan break; 162132718Skan 163132718Skan /* Subtle logic to output a space if and only if necessary. */ 164132718Skan if (avoid_paste) 165132718Skan { 166132718Skan if (print.source == NULL) 167132718Skan print.source = token; 168132718Skan if (print.source->flags & PREV_WHITE 169132718Skan || (print.prev 170132718Skan && cpp_avoid_paste (pfile, print.prev, token)) 171132718Skan || (print.prev == NULL && token->type == CPP_HASH)) 172132718Skan putc (' ', print.outf); 173132718Skan } 174132718Skan else if (token->flags & PREV_WHITE) 175132718Skan putc (' ', print.outf); 176132718Skan 177132718Skan avoid_paste = false; 178132718Skan print.source = NULL; 179132718Skan print.prev = token; 180132718Skan cpp_output_token (token, print.outf); 181132718Skan 182132718Skan if (token->type == CPP_COMMENT) 183132718Skan account_for_newlines (token->val.str.text, token->val.str.len); 184132718Skan } 185132718Skan} 186132718Skan 187259405Spfgstatic void 188259405Spfgprint_lines_directives_only (int lines, const void *buf, size_t size) 189259405Spfg{ 190259405Spfg print.src_line += lines; 191259405Spfg fwrite (buf, 1, size, print.outf); 192259405Spfg} 193259405Spfg 194259405Spfg/* Writes out the preprocessed file, handling spacing and paste 195259405Spfg avoidance issues. */ 196259405Spfgstatic void 197259405Spfgscan_translation_unit_directives_only (cpp_reader *pfile) 198259405Spfg{ 199259405Spfg struct _cpp_dir_only_callbacks cb; 200259405Spfg 201259405Spfg cb.print_lines = print_lines_directives_only; 202259405Spfg cb.maybe_print_line = maybe_print_line; 203259405Spfg 204259405Spfg _cpp_preprocess_dir_only (pfile, &cb); 205259405Spfg} 206259405Spfg 207169689Skan/* Adjust print.src_line for newlines embedded in output. */ 208132718Skanstatic void 209132718Skanaccount_for_newlines (const unsigned char *str, size_t len) 210132718Skan{ 211132718Skan while (len--) 212132718Skan if (*str++ == '\n') 213169689Skan print.src_line++; 214132718Skan} 215132718Skan 216132718Skan/* Writes out a traditionally preprocessed file. */ 217132718Skanstatic void 218132718Skanscan_translation_unit_trad (cpp_reader *pfile) 219132718Skan{ 220132718Skan while (_cpp_read_logical_line_trad (pfile)) 221132718Skan { 222132718Skan size_t len = pfile->out.cur - pfile->out.base; 223169689Skan maybe_print_line (pfile->out.first_line); 224132718Skan fwrite (pfile->out.base, 1, len, print.outf); 225132718Skan print.printed = 1; 226132718Skan if (!CPP_OPTION (pfile, discard_comments)) 227132718Skan account_for_newlines (pfile->out.base, len); 228132718Skan } 229132718Skan} 230132718Skan 231132718Skan/* If the token read on logical line LINE needs to be output on a 232132718Skan different line to the current one, output the required newlines or 233132718Skan a line marker, and return 1. Otherwise return 0. */ 234132718Skanstatic void 235169689Skanmaybe_print_line (source_location src_loc) 236132718Skan{ 237169689Skan const struct line_map *map = linemap_lookup (&line_table, src_loc); 238169689Skan int src_line = SOURCE_LINE (map, src_loc); 239132718Skan /* End the previous line of text. */ 240132718Skan if (print.printed) 241132718Skan { 242132718Skan putc ('\n', print.outf); 243169689Skan print.src_line++; 244132718Skan print.printed = 0; 245132718Skan } 246132718Skan 247169689Skan if (src_line >= print.src_line && src_line < print.src_line + 8) 248132718Skan { 249169689Skan while (src_line > print.src_line) 250132718Skan { 251132718Skan putc ('\n', print.outf); 252169689Skan print.src_line++; 253132718Skan } 254132718Skan } 255132718Skan else 256169689Skan print_line (src_loc, ""); 257132718Skan} 258132718Skan 259132718Skan/* Output a line marker for logical line LINE. Special flags are "1" 260132718Skan or "2" indicating entering or leaving a file. */ 261132718Skanstatic void 262169689Skanprint_line (source_location src_loc, const char *special_flags) 263132718Skan{ 264132718Skan /* End any previous line of text. */ 265132718Skan if (print.printed) 266132718Skan putc ('\n', print.outf); 267132718Skan print.printed = 0; 268132718Skan 269132718Skan if (!flag_no_line_commands) 270132718Skan { 271169689Skan const struct line_map *map = linemap_lookup (&line_table, src_loc); 272169689Skan 273132718Skan size_t to_file_len = strlen (map->to_file); 274169689Skan unsigned char *to_file_quoted = 275169689Skan (unsigned char *) alloca (to_file_len * 4 + 1); 276132718Skan unsigned char *p; 277132718Skan 278169689Skan print.src_line = SOURCE_LINE (map, src_loc); 279169689Skan 280132718Skan /* cpp_quote_string does not nul-terminate, so we have to do it 281132718Skan ourselves. */ 282132718Skan p = cpp_quote_string (to_file_quoted, 283169689Skan (unsigned char *) map->to_file, to_file_len); 284132718Skan *p = '\0'; 285132718Skan fprintf (print.outf, "# %u \"%s\"%s", 286169689Skan print.src_line == 0 ? 1 : print.src_line, 287132718Skan to_file_quoted, special_flags); 288132718Skan 289132718Skan if (map->sysp == 2) 290132718Skan fputs (" 3 4", print.outf); 291132718Skan else if (map->sysp == 1) 292132718Skan fputs (" 3", print.outf); 293132718Skan 294132718Skan putc ('\n', print.outf); 295132718Skan } 296132718Skan} 297132718Skan 298132718Skan/* Called when a line of output is started. TOKEN is the first token 299132718Skan of the line, and at end of file will be CPP_EOF. */ 300132718Skanstatic void 301132718Skancb_line_change (cpp_reader *pfile, const cpp_token *token, 302132718Skan int parsing_args) 303132718Skan{ 304169689Skan source_location src_loc = token->src_loc; 305169689Skan 306132718Skan if (token->type == CPP_EOF || parsing_args) 307132718Skan return; 308132718Skan 309169689Skan maybe_print_line (src_loc); 310132718Skan print.prev = 0; 311132718Skan print.source = 0; 312132718Skan 313132718Skan /* Supply enough spaces to put this token in its original column, 314132718Skan one space per column greater than 2, since scan_translation_unit 315132718Skan will provide a space if PREV_WHITE. Don't bother trying to 316132718Skan reconstruct tabs; we can't get it right in general, and nothing 317132718Skan ought to care. Some things do care; the fault lies with them. */ 318132718Skan if (!CPP_OPTION (pfile, traditional)) 319132718Skan { 320169689Skan const struct line_map *map = linemap_lookup (&line_table, src_loc); 321169689Skan int spaces = SOURCE_COLUMN (map, src_loc) - 2; 322132718Skan print.printed = 1; 323132718Skan 324169689Skan while (-- spaces >= 0) 325169689Skan putc (' ', print.outf); 326132718Skan } 327132718Skan} 328132718Skan 329132718Skanstatic void 330169689Skancb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 331132718Skan const cpp_string *str) 332132718Skan{ 333169689Skan maybe_print_line (line); 334136527Skan fprintf (print.outf, "#ident %s\n", str->text); 335169689Skan print.src_line++; 336132718Skan} 337132718Skan 338132718Skanstatic void 339169689Skancb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node) 340132718Skan{ 341169689Skan maybe_print_line (line); 342132718Skan fputs ("#define ", print.outf); 343132718Skan 344132718Skan /* 'D' is whole definition; 'N' is name only. */ 345132718Skan if (flag_dump_macros == 'D') 346132718Skan fputs ((const char *) cpp_macro_definition (pfile, node), 347132718Skan print.outf); 348132718Skan else 349132718Skan fputs ((const char *) NODE_NAME (node), print.outf); 350132718Skan 351132718Skan putc ('\n', print.outf); 352169689Skan if (linemap_lookup (&line_table, line)->to_line != 0) 353169689Skan print.src_line++; 354132718Skan} 355132718Skan 356132718Skanstatic void 357169689Skancb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 358132718Skan cpp_hashnode *node) 359132718Skan{ 360169689Skan maybe_print_line (line); 361132718Skan fprintf (print.outf, "#undef %s\n", NODE_NAME (node)); 362169689Skan print.src_line++; 363132718Skan} 364132718Skan 365132718Skanstatic void 366169689Skancb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, 367169689Skan const unsigned char *dir, const char *header, int angle_brackets, 368169689Skan const cpp_token **comments) 369132718Skan{ 370169689Skan maybe_print_line (line); 371132718Skan if (angle_brackets) 372169689Skan fprintf (print.outf, "#%s <%s>", dir, header); 373132718Skan else 374169689Skan fprintf (print.outf, "#%s \"%s\"", dir, header); 375169689Skan 376169689Skan if (comments != NULL) 377169689Skan { 378169689Skan while (*comments != NULL) 379169689Skan { 380169689Skan if ((*comments)->flags & PREV_WHITE) 381169689Skan putc (' ', print.outf); 382169689Skan cpp_output_token (*comments, print.outf); 383169689Skan ++comments; 384169689Skan } 385169689Skan } 386169689Skan 387169689Skan putc ('\n', print.outf); 388169689Skan print.src_line++; 389132718Skan} 390132718Skan 391132718Skan/* Callback called when -fworking-director and -E to emit working 392169689Skan directory in cpp output file. */ 393132718Skan 394132718Skanvoid 395132718Skanpp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir) 396132718Skan{ 397132718Skan size_t to_file_len = strlen (dir); 398169689Skan unsigned char *to_file_quoted = 399169689Skan (unsigned char *) alloca (to_file_len * 4 + 1); 400132718Skan unsigned char *p; 401132718Skan 402169689Skan /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */ 403132718Skan p = cpp_quote_string (to_file_quoted, (unsigned char *) dir, to_file_len); 404132718Skan *p = '\0'; 405132718Skan fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted); 406132718Skan} 407132718Skan 408132718Skan/* The file name, line number or system header flags have changed, as 409169689Skan described in MAP. */ 410132718Skan 411132718Skanvoid 412132718Skanpp_file_change (const struct line_map *map) 413132718Skan{ 414132718Skan const char *flags = ""; 415132718Skan 416146895Skan if (flag_no_line_commands) 417132718Skan return; 418132718Skan 419132718Skan if (map != NULL) 420132718Skan { 421169689Skan if (print.first_time) 422132718Skan { 423132718Skan /* Avoid printing foo.i when the main file is foo.c. */ 424132718Skan if (!cpp_get_options (parse_in)->preprocessed) 425169689Skan print_line (map->start_location, flags); 426169689Skan print.first_time = 0; 427132718Skan } 428132718Skan else 429132718Skan { 430132718Skan /* Bring current file to correct line when entering a new file. */ 431132718Skan if (map->reason == LC_ENTER) 432169689Skan { 433169689Skan const struct line_map *from = INCLUDED_FROM (&line_table, map); 434169689Skan maybe_print_line (LAST_SOURCE_LINE_LOCATION (from)); 435169689Skan } 436132718Skan if (map->reason == LC_ENTER) 437132718Skan flags = " 1"; 438132718Skan else if (map->reason == LC_LEAVE) 439132718Skan flags = " 2"; 440169689Skan print_line (map->start_location, flags); 441132718Skan } 442132718Skan } 443132718Skan} 444132718Skan 445132718Skan/* Copy a #pragma directive to the preprocessed output. */ 446132718Skanstatic void 447169689Skancb_def_pragma (cpp_reader *pfile, source_location line) 448132718Skan{ 449169689Skan maybe_print_line (line); 450132718Skan fputs ("#pragma ", print.outf); 451132718Skan cpp_output_line (pfile, print.outf); 452169689Skan print.src_line++; 453132718Skan} 454132718Skan 455132718Skan/* Dump out the hash table. */ 456132718Skanstatic int 457132718Skandump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED) 458132718Skan{ 459132718Skan if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) 460132718Skan { 461132718Skan fputs ("#define ", print.outf); 462132718Skan fputs ((const char *) cpp_macro_definition (pfile, node), 463132718Skan print.outf); 464132718Skan putc ('\n', print.outf); 465169689Skan print.src_line++; 466132718Skan } 467132718Skan 468132718Skan return 1; 469132718Skan} 470169689Skan 471169689Skan/* Load in the PCH file NAME, open on FD. It was originally searched for 472169689Skan by ORIG_NAME. Also, print out a #include command so that the PCH 473169689Skan file can be loaded when the preprocessed output is compiled. */ 474169689Skan 475169689Skanstatic void 476169689Skancb_read_pch (cpp_reader *pfile, const char *name, 477169689Skan int fd, const char *orig_name ATTRIBUTE_UNUSED) 478169689Skan{ 479169689Skan c_common_read_pch (pfile, name, fd, orig_name); 480169689Skan 481169689Skan fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name); 482169689Skan print.src_line++; 483169689Skan} 484