Fix date
[official-gcc.git] / gcc / c-family / c-ppoutput.c
blobd1c92379f62fee4fe3bd2a9d156c2ff623806d29
1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
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.
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.
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/>. */
19 #include "config.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "c-common.h" /* For flags. */
23 #include "../libcpp/internal.h"
24 #include "c-pragma.h" /* For parse_in. */
26 /* Encapsulates state used to convert a stream of tokens into a text
27 file. */
28 static struct
30 FILE *outf; /* Stream to write to. */
31 const cpp_token *prev; /* Previous token. */
32 const cpp_token *source; /* Source token for spacing. */
33 int src_line; /* Line number currently being written. */
34 bool printed; /* True if something output at line. */
35 bool first_time; /* pp_file_change hasn't been called yet. */
36 bool prev_was_system_token; /* True if the previous token was a
37 system token.*/
38 const char *src_file; /* Current source file. */
39 } print;
41 /* Defined and undefined macros being queued for output with -dU at
42 the next newline. */
43 struct macro_queue
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. */
50 static macro_queue *define_queue, *undef_queue;
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 *);
61 static bool print_line_1 (source_location, const char*, FILE *);
62 static bool print_line (source_location, const char *);
63 static bool maybe_print_line_1 (source_location, FILE *);
64 static bool maybe_print_line (source_location);
65 static bool do_line_change (cpp_reader *, const cpp_token *,
66 source_location, int);
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);
82 /* Preprocess and output. */
83 void
84 preprocess_file (cpp_reader *pfile)
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)
90 /* Scan -included buffers, then the main file. */
91 while (pfile->buffer->prev)
92 cpp_scan_nooutput (pfile);
93 cpp_scan_nooutput (pfile);
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);
103 /* -dM command line option. Should this be elsewhere? */
104 if (flag_dump_macros == 'M')
105 cpp_forall_identifiers (pfile, dump_macro, NULL);
107 /* Flush any pending output. */
108 if (print.printed)
109 putc ('\n', print.outf);
112 /* Set up the callbacks as appropriate. */
113 void
114 init_pp_output (FILE *out_stream)
116 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
118 if (!flag_no_output)
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)
125 cb->ident = cb_ident;
126 cb->def_pragma = cb_def_pragma;
130 if (flag_dump_includes)
131 cb->include = cb_include;
133 if (flag_pch_preprocess)
135 cb->valid_pch = c_common_valid_pch;
136 cb->read_pch = cb_read_pch;
139 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
141 cb->define = cb_define;
142 cb->undef = cb_undef;
145 if (flag_dump_macros == 'U')
147 cb->before_define = dump_queued_macros;
148 cb->used_define = cb_used_define;
149 cb->used_undef = cb_used_undef;
152 cb->has_attribute = c_common_has_attribute;
153 cb->get_source_date_epoch = cb_get_source_date_epoch;
155 /* Initialize the print structure. */
156 print.src_line = 1;
157 print.printed = false;
158 print.prev = 0;
159 print.outf = out_stream;
160 print.first_time = 1;
161 print.src_file = "";
162 print.prev_was_system_token = false;
165 /* Writes out the preprocessed file, handling spacing and paste
166 avoidance issues. */
167 static void
168 scan_translation_unit (cpp_reader *pfile)
170 bool avoid_paste = false;
171 bool do_line_adjustments
172 = cpp_get_options (parse_in)->lang != CLK_ASM
173 && !flag_no_line_commands;
174 bool in_pragma = false;
175 bool line_marker_emitted = false;
177 print.source = NULL;
178 for (;;)
180 source_location loc;
181 const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
183 if (token->type == CPP_PADDING)
185 avoid_paste = true;
186 if (print.source == NULL
187 || (!(print.source->flags & PREV_WHITE)
188 && token->val.source == NULL))
189 print.source = token->val.source;
190 continue;
193 if (token->type == CPP_EOF)
194 break;
196 /* Subtle logic to output a space if and only if necessary. */
197 if (avoid_paste)
199 int src_line = LOCATION_LINE (loc);
201 if (print.source == NULL)
202 print.source = token;
204 if (src_line != print.src_line
205 && do_line_adjustments
206 && !in_pragma)
208 line_marker_emitted = do_line_change (pfile, token, loc, false);
209 putc (' ', print.outf);
210 print.printed = true;
212 else if (print.source->flags & PREV_WHITE
213 || (print.prev
214 && cpp_avoid_paste (pfile, print.prev, token))
215 || (print.prev == NULL && token->type == CPP_HASH))
217 putc (' ', print.outf);
218 print.printed = true;
221 else if (token->flags & PREV_WHITE)
223 int src_line = LOCATION_LINE (loc);
225 if (src_line != print.src_line
226 && do_line_adjustments
227 && !in_pragma)
228 line_marker_emitted = do_line_change (pfile, token, loc, false);
229 putc (' ', print.outf);
230 print.printed = true;
233 avoid_paste = false;
234 print.source = NULL;
235 print.prev = token;
236 if (token->type == CPP_PRAGMA)
238 const char *space;
239 const char *name;
241 line_marker_emitted = maybe_print_line (token->src_loc);
242 fputs ("#pragma ", print.outf);
243 c_pp_lookup_pragma (token->val.pragma, &space, &name);
244 if (space)
245 fprintf (print.outf, "%s %s", space, name);
246 else
247 fprintf (print.outf, "%s", name);
248 print.printed = true;
249 in_pragma = true;
251 else if (token->type == CPP_PRAGMA_EOL)
253 maybe_print_line (token->src_loc);
254 in_pragma = false;
256 else
258 if (cpp_get_options (parse_in)->debug)
259 linemap_dump_location (line_table, token->src_loc, print.outf);
261 if (do_line_adjustments
262 && !in_pragma
263 && !line_marker_emitted
264 && print.prev_was_system_token != !!in_system_header_at (loc)
265 && !is_location_from_builtin_token (loc))
266 /* The system-ness of this token is different from the one
267 of the previous token. Let's emit a line change to
268 mark the new system-ness before we emit the token. */
270 do_line_change (pfile, token, loc, false);
271 print.prev_was_system_token = !!in_system_header_at (loc);
273 cpp_output_token (token, print.outf);
274 line_marker_emitted = false;
275 print.printed = true;
278 /* CPP_COMMENT tokens and raw-string literal tokens can
279 have embedded new-line characters. Rather than enumerating
280 all the possible token types just check if token uses
281 val.str union member. */
282 if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
283 account_for_newlines (token->val.str.text, token->val.str.len);
287 static void
288 print_lines_directives_only (int lines, const void *buf, size_t size)
290 print.src_line += lines;
291 fwrite (buf, 1, size, print.outf);
294 /* Writes out the preprocessed file, handling spacing and paste
295 avoidance issues. */
296 static void
297 scan_translation_unit_directives_only (cpp_reader *pfile)
299 struct _cpp_dir_only_callbacks cb;
301 cb.print_lines = print_lines_directives_only;
302 cb.maybe_print_line = (void (*) (source_location)) maybe_print_line;
304 _cpp_preprocess_dir_only (pfile, &cb);
307 /* Adjust print.src_line for newlines embedded in output. */
308 static void
309 account_for_newlines (const unsigned char *str, size_t len)
311 while (len--)
312 if (*str++ == '\n')
313 print.src_line++;
316 /* Writes out a traditionally preprocessed file. */
317 static void
318 scan_translation_unit_trad (cpp_reader *pfile)
320 while (_cpp_read_logical_line_trad (pfile))
322 size_t len = pfile->out.cur - pfile->out.base;
323 maybe_print_line (pfile->out.first_line);
324 fwrite (pfile->out.base, 1, len, print.outf);
325 print.printed = true;
326 if (!CPP_OPTION (pfile, discard_comments))
327 account_for_newlines (pfile->out.base, len);
331 /* If the token read on logical line LINE needs to be output on a
332 different line to the current one, output the required newlines or
333 a line marker. If a line marker was emitted, return TRUE otherwise
334 return FALSE. */
336 static bool
337 maybe_print_line_1 (source_location src_loc, FILE *stream)
339 bool emitted_line_marker = false;
340 int src_line = LOCATION_LINE (src_loc);
341 const char *src_file = LOCATION_FILE (src_loc);
343 /* End the previous line of text. */
344 if (print.printed)
346 putc ('\n', stream);
347 print.src_line++;
348 print.printed = false;
351 if (!flag_no_line_commands
352 && src_line >= print.src_line
353 && src_line < print.src_line + 8
354 && strcmp (src_file, print.src_file) == 0)
356 while (src_line > print.src_line)
358 putc ('\n', stream);
359 print.src_line++;
362 else
363 emitted_line_marker = print_line_1 (src_loc, "", stream);
365 return emitted_line_marker;
368 /* If the token read on logical line LINE needs to be output on a
369 different line to the current one, output the required newlines or
370 a line marker. If a line marker was emitted, return TRUE otherwise
371 return FALSE. */
373 static bool
374 maybe_print_line (source_location src_loc)
376 if (cpp_get_options (parse_in)->debug)
377 linemap_dump_location (line_table, src_loc,
378 print.outf);
379 return maybe_print_line_1 (src_loc, print.outf);
382 /* Output a line marker for logical line LINE. Special flags are "1"
383 or "2" indicating entering or leaving a file. If the line marker
384 was effectively emitted, return TRUE otherwise return FALSE. */
386 static bool
387 print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
389 bool emitted_line_marker = false;
391 /* End any previous line of text. */
392 if (print.printed)
393 putc ('\n', stream);
394 print.printed = false;
396 if (!flag_no_line_commands)
398 const char *file_path = LOCATION_FILE (src_loc);
399 int sysp;
400 size_t to_file_len = strlen (file_path);
401 unsigned char *to_file_quoted =
402 (unsigned char *) alloca (to_file_len * 4 + 1);
403 unsigned char *p;
405 print.src_line = LOCATION_LINE (src_loc);
406 print.src_file = file_path;
408 /* cpp_quote_string does not nul-terminate, so we have to do it
409 ourselves. */
410 p = cpp_quote_string (to_file_quoted,
411 (const unsigned char *) file_path,
412 to_file_len);
413 *p = '\0';
414 fprintf (stream, "# %u \"%s\"%s",
415 print.src_line == 0 ? 1 : print.src_line,
416 to_file_quoted, special_flags);
418 sysp = in_system_header_at (src_loc);
419 if (sysp == 2)
420 fputs (" 3 4", stream);
421 else if (sysp == 1)
422 fputs (" 3", stream);
424 putc ('\n', stream);
425 emitted_line_marker = true;
428 return emitted_line_marker;
431 /* Output a line marker for logical line LINE. Special flags are "1"
432 or "2" indicating entering or leaving a file. Return TRUE if a
433 line marker was effectively emitted, FALSE otherwise. */
435 static bool
436 print_line (source_location src_loc, const char *special_flags)
438 if (cpp_get_options (parse_in)->debug)
439 linemap_dump_location (line_table, src_loc,
440 print.outf);
441 return print_line_1 (src_loc, special_flags, print.outf);
444 /* Helper function for cb_line_change and scan_translation_unit.
445 Return TRUE if a line marker is emitted, FALSE otherwise. */
446 static bool
447 do_line_change (cpp_reader *pfile, const cpp_token *token,
448 source_location src_loc, int parsing_args)
450 bool emitted_line_marker = false;
451 if (define_queue || undef_queue)
452 dump_queued_macros (pfile);
454 if (token->type == CPP_EOF || parsing_args)
455 return false;
457 emitted_line_marker = maybe_print_line (src_loc);
458 print.prev = 0;
459 print.source = 0;
461 /* Supply enough spaces to put this token in its original column,
462 one space per column greater than 2, since scan_translation_unit
463 will provide a space if PREV_WHITE. Don't bother trying to
464 reconstruct tabs; we can't get it right in general, and nothing
465 ought to care. Some things do care; the fault lies with them. */
466 if (!CPP_OPTION (pfile, traditional))
468 int spaces = LOCATION_COLUMN (src_loc) - 2;
469 print.printed = true;
471 while (-- spaces >= 0)
472 putc (' ', print.outf);
475 return emitted_line_marker;
478 /* Called when a line of output is started. TOKEN is the first token
479 of the line, and at end of file will be CPP_EOF. */
480 static void
481 cb_line_change (cpp_reader *pfile, const cpp_token *token,
482 int parsing_args)
484 do_line_change (pfile, token, token->src_loc, parsing_args);
487 static void
488 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
489 const cpp_string *str)
491 maybe_print_line (line);
492 fprintf (print.outf, "#ident %s\n", str->text);
493 print.src_line++;
496 static void
497 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
499 const line_map_ordinary *map;
501 maybe_print_line (line);
502 fputs ("#define ", print.outf);
504 /* 'D' is whole definition; 'N' is name only. */
505 if (flag_dump_macros == 'D')
506 fputs ((const char *) cpp_macro_definition (pfile, node),
507 print.outf);
508 else
509 fputs ((const char *) NODE_NAME (node), print.outf);
511 putc ('\n', print.outf);
512 print.printed = false;
513 linemap_resolve_location (line_table, line,
514 LRK_MACRO_DEFINITION_LOCATION,
515 &map);
516 if (LINEMAP_LINE (map) != 0)
517 print.src_line++;
520 static void
521 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
522 cpp_hashnode *node)
524 maybe_print_line (line);
525 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
526 print.src_line++;
529 static void
530 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
531 cpp_hashnode *node)
533 macro_queue *q;
534 if (node->flags & NODE_BUILTIN)
535 return;
536 q = XNEW (macro_queue);
537 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
538 q->next = define_queue;
539 define_queue = q;
542 static void
543 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
544 source_location line ATTRIBUTE_UNUSED,
545 cpp_hashnode *node)
547 macro_queue *q;
548 q = XNEW (macro_queue);
549 q->macro = xstrdup ((const char *) NODE_NAME (node));
550 q->next = undef_queue;
551 undef_queue = q;
554 static void
555 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
557 macro_queue *q;
559 /* End the previous line of text. */
560 if (print.printed)
562 putc ('\n', print.outf);
563 print.src_line++;
564 print.printed = false;
567 for (q = define_queue; q;)
569 macro_queue *oq;
570 fputs ("#define ", print.outf);
571 fputs (q->macro, print.outf);
572 putc ('\n', print.outf);
573 print.printed = false;
574 print.src_line++;
575 oq = q;
576 q = q->next;
577 free (oq->macro);
578 free (oq);
580 define_queue = NULL;
581 for (q = undef_queue; q;)
583 macro_queue *oq;
584 fprintf (print.outf, "#undef %s\n", q->macro);
585 print.src_line++;
586 oq = q;
587 q = q->next;
588 free (oq->macro);
589 free (oq);
591 undef_queue = NULL;
594 static void
595 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
596 const unsigned char *dir, const char *header, int angle_brackets,
597 const cpp_token **comments)
599 maybe_print_line (line);
600 if (angle_brackets)
601 fprintf (print.outf, "#%s <%s>", dir, header);
602 else
603 fprintf (print.outf, "#%s \"%s\"", dir, header);
605 if (comments != NULL)
607 while (*comments != NULL)
609 if ((*comments)->flags & PREV_WHITE)
610 putc (' ', print.outf);
611 cpp_output_token (*comments, print.outf);
612 ++comments;
616 putc ('\n', print.outf);
617 print.printed = false;
618 print.src_line++;
621 /* Callback called when -fworking-director and -E to emit working
622 directory in cpp output file. */
624 void
625 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
627 size_t to_file_len = strlen (dir);
628 unsigned char *to_file_quoted =
629 (unsigned char *) alloca (to_file_len * 4 + 1);
630 unsigned char *p;
632 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
633 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
634 *p = '\0';
635 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
638 /* The file name, line number or system header flags have changed, as
639 described in MAP. */
641 void
642 pp_file_change (const line_map_ordinary *map)
644 const char *flags = "";
646 if (flag_no_line_commands)
647 return;
649 if (map != NULL)
651 input_location = map->start_location;
652 if (print.first_time)
654 /* Avoid printing foo.i when the main file is foo.c. */
655 if (!cpp_get_options (parse_in)->preprocessed)
656 print_line (map->start_location, flags);
657 print.first_time = 0;
659 else
661 /* Bring current file to correct line when entering a new file. */
662 if (map->reason == LC_ENTER)
664 const line_map_ordinary *from = INCLUDED_FROM (line_table, map);
665 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
667 if (map->reason == LC_ENTER)
668 flags = " 1";
669 else if (map->reason == LC_LEAVE)
670 flags = " 2";
671 print_line (map->start_location, flags);
676 /* Copy a #pragma directive to the preprocessed output. */
677 static void
678 cb_def_pragma (cpp_reader *pfile, source_location line)
680 maybe_print_line (line);
681 fputs ("#pragma ", print.outf);
682 cpp_output_line (pfile, print.outf);
683 print.printed = false;
684 print.src_line++;
687 /* Dump out the hash table. */
688 static int
689 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
691 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
693 fputs ("#define ", print.outf);
694 fputs ((const char *) cpp_macro_definition (pfile, node),
695 print.outf);
696 putc ('\n', print.outf);
697 print.printed = false;
698 print.src_line++;
701 return 1;
704 /* Load in the PCH file NAME, open on FD. It was originally searched for
705 by ORIG_NAME. Also, print out a #include command so that the PCH
706 file can be loaded when the preprocessed output is compiled. */
708 static void
709 cb_read_pch (cpp_reader *pfile, const char *name,
710 int fd, const char *orig_name ATTRIBUTE_UNUSED)
712 c_common_read_pch (pfile, name, fd, orig_name);
714 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
715 print.src_line++;