Daily bump.
[official-gcc.git] / gcc / c-family / c-ppoutput.c
blobae33c1ff40f76cfe4094d43607995461ffdeab7d
1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995-2021 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 "langhooks.h"
25 #include "c-pragma.h" /* For parse_in. */
26 #include "file-prefix-map.h" /* remap_macro_filename() */
28 /* Encapsulates state used to convert a stream of tokens into a text
29 file. */
30 static struct
32 FILE *outf; /* Stream to write to. */
33 const cpp_token *prev; /* Previous token. */
34 const cpp_token *source; /* Source token for spacing. */
35 unsigned src_line; /* Line number currently being written. */
36 bool printed; /* True if something output at line. */
37 bool first_time; /* pp_file_change hasn't been called yet. */
38 bool prev_was_system_token; /* True if the previous token was a
39 system token.*/
40 const char *src_file; /* Current source file. */
41 } print;
43 /* Defined and undefined macros being queued for output with -dU at
44 the next newline. */
45 struct macro_queue
47 struct macro_queue *next; /* Next macro in the list. */
48 char *macro; /* The name of the macro if not
49 defined, the full definition if
50 defined. */
52 static macro_queue *define_queue, *undef_queue;
54 /* General output routines. */
55 static void scan_translation_unit (cpp_reader *);
56 static void scan_translation_unit_directives_only (cpp_reader *);
57 static void scan_translation_unit_trad (cpp_reader *);
58 static void account_for_newlines (const unsigned char *, size_t);
59 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
60 static void dump_queued_macros (cpp_reader *);
62 static bool print_line_1 (location_t, const char*, FILE *);
63 static bool print_line (location_t, const char *);
64 static bool maybe_print_line_1 (location_t, FILE *);
65 static bool maybe_print_line (location_t);
66 static bool do_line_change (cpp_reader *, const cpp_token *,
67 location_t, int);
69 /* Callback routines for the parser. Most of these are active only
70 in specific modes. */
71 static void cb_line_change (cpp_reader *, const cpp_token *, int);
72 static void cb_define (cpp_reader *, location_t, cpp_hashnode *);
73 static void cb_undef (cpp_reader *, location_t, cpp_hashnode *);
74 static void cb_used_define (cpp_reader *, location_t, cpp_hashnode *);
75 static void cb_used_undef (cpp_reader *, location_t, cpp_hashnode *);
76 static void cb_include (cpp_reader *, location_t, const unsigned char *,
77 const char *, int, const cpp_token **);
78 static void cb_ident (cpp_reader *, location_t, const cpp_string *);
79 static void cb_def_pragma (cpp_reader *, location_t);
80 static void cb_read_pch (cpp_reader *pfile, const char *name,
81 int fd, const char *orig_name);
83 /* Preprocess and output. */
84 void
85 preprocess_file (cpp_reader *pfile)
87 /* A successful cpp_read_main_file guarantees that we can call
88 cpp_scan_nooutput or cpp_get_token next. */
89 if (flag_no_output && pfile->buffer)
91 /* Scan -included buffers, then the main file. */
92 while (pfile->buffer->prev)
93 cpp_scan_nooutput (pfile);
94 cpp_scan_nooutput (pfile);
96 else if (cpp_get_options (pfile)->traditional)
97 scan_translation_unit_trad (pfile);
98 else if (cpp_get_options (pfile)->directives_only
99 && !cpp_get_options (pfile)->preprocessed)
100 scan_translation_unit_directives_only (pfile);
101 else
102 scan_translation_unit (pfile);
104 /* -dM command line option. Should this be elsewhere? */
105 if (flag_dump_macros == 'M')
106 cpp_forall_identifiers (pfile, dump_macro, NULL);
108 /* Flush any pending output. */
109 if (print.printed)
110 putc ('\n', print.outf);
113 /* Set up the callbacks as appropriate. */
114 void
115 init_pp_output (FILE *out_stream)
117 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
119 if (!flag_no_output)
121 cb->line_change = cb_line_change;
122 /* Don't emit #pragma or #ident directives if we are processing
123 assembly language; the assembler may choke on them. */
124 if (cpp_get_options (parse_in)->lang != CLK_ASM)
126 cb->ident = cb_ident;
127 cb->def_pragma = cb_def_pragma;
131 if (flag_dump_includes)
132 cb->include = cb_include;
134 if (flag_pch_preprocess)
136 cb->valid_pch = c_common_valid_pch;
137 cb->read_pch = cb_read_pch;
140 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
142 cb->define = cb_define;
143 cb->undef = cb_undef;
146 if (flag_dump_macros == 'U')
148 cb->before_define = dump_queued_macros;
149 cb->used_define = cb_used_define;
150 cb->used_undef = cb_used_undef;
153 cb->has_attribute = c_common_has_attribute;
154 cb->has_builtin = c_common_has_builtin;
155 cb->get_source_date_epoch = cb_get_source_date_epoch;
156 cb->remap_filename = remap_macro_filename;
158 /* Initialize the print structure. */
159 print.src_line = 1;
160 print.printed = false;
161 print.prev = 0;
162 print.outf = out_stream;
163 print.first_time = 1;
164 print.src_file = "";
165 print.prev_was_system_token = false;
168 // FIXME: Ideally we'd just turn the entirety of the print struct into
169 // an encapsulated streamer ...
171 class token_streamer
173 bool avoid_paste;
174 bool do_line_adjustments;
175 bool in_pragma;
176 bool line_marker_emitted;
178 public:
179 token_streamer (cpp_reader *pfile)
180 :avoid_paste (false),
181 do_line_adjustments (cpp_get_options (pfile)->lang != CLK_ASM
182 && !flag_no_line_commands),
183 in_pragma (false),
184 line_marker_emitted (false)
188 void begin_pragma ()
190 in_pragma = true;
193 void stream (cpp_reader *pfile, const cpp_token *tok, location_t);
196 void
197 token_streamer::stream (cpp_reader *pfile, const cpp_token *token,
198 location_t loc)
200 if (token->type == CPP_PADDING)
202 avoid_paste = true;
203 if (print.source == NULL
204 || (!(print.source->flags & PREV_WHITE)
205 && token->val.source == NULL))
206 print.source = token->val.source;
207 return;
210 if (token->type == CPP_EOF)
211 return;
213 /* Subtle logic to output a space if and only if necessary. */
214 if (avoid_paste)
216 unsigned src_line = LOCATION_LINE (loc);
218 if (print.source == NULL)
219 print.source = token;
221 if (src_line != print.src_line
222 && do_line_adjustments
223 && !in_pragma)
225 line_marker_emitted = do_line_change (pfile, token, loc, false);
226 putc (' ', print.outf);
227 print.printed = true;
229 else if (print.source->flags & PREV_WHITE
230 || (print.prev
231 && cpp_avoid_paste (pfile, print.prev, token))
232 || (print.prev == NULL && token->type == CPP_HASH))
234 putc (' ', print.outf);
235 print.printed = true;
238 else if (token->flags & PREV_WHITE)
240 unsigned src_line = LOCATION_LINE (loc);
242 if (src_line != print.src_line
243 && do_line_adjustments
244 && !in_pragma)
245 line_marker_emitted = do_line_change (pfile, token, loc, false);
246 putc (' ', print.outf);
247 print.printed = true;
250 avoid_paste = false;
251 print.source = NULL;
252 print.prev = token;
253 if (token->type == CPP_PRAGMA)
255 const char *space;
256 const char *name;
258 line_marker_emitted = maybe_print_line (token->src_loc);
259 fputs ("#pragma ", print.outf);
260 c_pp_lookup_pragma (token->val.pragma, &space, &name);
261 if (space)
262 fprintf (print.outf, "%s %s", space, name);
263 else
264 fprintf (print.outf, "%s", name);
265 print.printed = true;
266 in_pragma = true;
268 else if (token->type == CPP_PRAGMA_EOL)
270 maybe_print_line (UNKNOWN_LOCATION);
271 in_pragma = false;
273 else
275 if (cpp_get_options (parse_in)->debug)
276 linemap_dump_location (line_table, token->src_loc, print.outf);
278 if (do_line_adjustments
279 && !in_pragma
280 && !line_marker_emitted
281 && print.prev_was_system_token != !!in_system_header_at (loc)
282 && !is_location_from_builtin_token (loc))
283 /* The system-ness of this token is different from the one of
284 the previous token. Let's emit a line change to mark the
285 new system-ness before we emit the token. */
287 do_line_change (pfile, token, loc, false);
288 print.prev_was_system_token = !!in_system_header_at (loc);
290 cpp_output_token (token, print.outf);
291 line_marker_emitted = false;
292 print.printed = true;
295 /* CPP_COMMENT tokens and raw-string literal tokens can have
296 embedded new-line characters. Rather than enumerating all the
297 possible token types just check if token uses val.str union
298 member. */
299 if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
300 account_for_newlines (token->val.str.text, token->val.str.len);
303 /* Writes out the preprocessed file, handling spacing and paste
304 avoidance issues. */
306 static void
307 scan_translation_unit (cpp_reader *pfile)
309 token_streamer streamer (pfile);
310 uintptr_t filter = 0;
312 if (lang_hooks.preprocess_token)
313 filter = lang_hooks.preprocess_token (pfile, NULL, filter);
315 print.source = NULL;
316 for (;;)
318 location_t spelling_loc;
319 const cpp_token *token
320 = cpp_get_token_with_location (pfile, &spelling_loc);
322 streamer.stream (pfile, token, spelling_loc);
323 if (filter)
325 unsigned flags = lang_hooks.preprocess_token (pfile, token, filter);
326 if (flags & lang_hooks::PT_begin_pragma)
327 streamer.begin_pragma ();
329 if (token->type == CPP_EOF)
330 break;
333 if (filter)
334 lang_hooks.preprocess_token (pfile, NULL, filter);
337 class do_streamer : public token_streamer
339 public:
340 uintptr_t filter;
342 do_streamer (cpp_reader *pfile, uintptr_t filter)
343 :token_streamer (pfile), filter (filter)
348 static void
349 directives_only_cb (cpp_reader *pfile, CPP_DO_task task, void *data_, ...)
351 va_list args;
352 va_start (args, data_);
354 do_streamer *streamer = reinterpret_cast <do_streamer *> (data_);
355 switch (task)
357 default:
358 gcc_unreachable ();
360 case CPP_DO_print:
362 print.src_line += va_arg (args, unsigned);
364 const void *buf = va_arg (args, const void *);
365 size_t size = va_arg (args, size_t);
366 fwrite (buf, 1, size, print.outf);
368 break;
370 case CPP_DO_location:
371 maybe_print_line (va_arg (args, location_t));
372 break;
374 case CPP_DO_token:
376 const cpp_token *token = va_arg (args, const cpp_token *);
377 location_t spelling_loc = va_arg (args, location_t);
378 streamer->stream (pfile, token, spelling_loc);
379 if (streamer->filter)
381 unsigned flags = lang_hooks.preprocess_token
382 (pfile, token, streamer->filter);
383 if (flags & lang_hooks::PT_begin_pragma)
384 streamer->begin_pragma ();
387 break;
390 va_end (args);
393 /* Writes out the preprocessed file, handling spacing and paste
394 avoidance issues. */
395 static void
396 scan_translation_unit_directives_only (cpp_reader *pfile)
398 uintptr_t filter = 0;
399 if (lang_hooks.preprocess_token)
400 filter = lang_hooks.preprocess_token (pfile, NULL, filter);
401 do_streamer streamer (pfile, filter);
402 cpp_directive_only_process (pfile, &streamer, directives_only_cb);
403 if (streamer.filter)
404 lang_hooks.preprocess_token (pfile, NULL, streamer.filter);
407 /* Adjust print.src_line for newlines embedded in output. */
408 static void
409 account_for_newlines (const unsigned char *str, size_t len)
411 while (len--)
412 if (*str++ == '\n')
413 print.src_line++;
416 /* Writes out a traditionally preprocessed file. */
417 static void
418 scan_translation_unit_trad (cpp_reader *pfile)
420 while (_cpp_read_logical_line_trad (pfile))
422 size_t len = pfile->out.cur - pfile->out.base;
423 maybe_print_line (pfile->out.first_line);
424 fwrite (pfile->out.base, 1, len, print.outf);
425 print.printed = true;
426 if (!CPP_OPTION (pfile, discard_comments))
427 account_for_newlines (pfile->out.base, len);
431 /* If the token read on logical line LINE needs to be output on a
432 different line to the current one, output the required newlines or
433 a line marker. If a line marker was emitted, return TRUE otherwise
434 return FALSE. */
436 static bool
437 maybe_print_line_1 (location_t src_loc, FILE *stream)
439 bool emitted_line_marker = false;
440 unsigned src_line = LOCATION_LINE (src_loc);
441 const char *src_file = LOCATION_FILE (src_loc);
443 /* End the previous line of text. */
444 if (print.printed)
446 putc ('\n', stream);
447 print.src_line++;
448 print.printed = false;
451 if (!flag_no_line_commands
452 && src_line >= print.src_line
453 && src_line < print.src_line + 8
454 && src_loc != UNKNOWN_LOCATION
455 && strcmp (src_file, print.src_file) == 0)
457 while (src_line > print.src_line)
459 putc ('\n', stream);
460 print.src_line++;
463 else
464 emitted_line_marker = print_line_1 (src_loc, "", stream);
466 return emitted_line_marker;
469 /* If the token read on logical line LINE needs to be output on a
470 different line to the current one, output the required newlines or
471 a line marker. If a line marker was emitted, return TRUE otherwise
472 return FALSE. */
474 static bool
475 maybe_print_line (location_t src_loc)
477 if (cpp_get_options (parse_in)->debug)
478 linemap_dump_location (line_table, src_loc,
479 print.outf);
480 return maybe_print_line_1 (src_loc, print.outf);
483 /* Output a line marker for logical line LINE. Special flags are "1"
484 or "2" indicating entering or leaving a file. If the line marker
485 was effectively emitted, return TRUE otherwise return FALSE. */
487 static bool
488 print_line_1 (location_t src_loc, const char *special_flags, FILE *stream)
490 bool emitted_line_marker = false;
492 /* End any previous line of text. */
493 if (print.printed)
494 putc ('\n', stream);
495 print.printed = false;
497 if (src_loc != UNKNOWN_LOCATION && !flag_no_line_commands)
499 const char *file_path = LOCATION_FILE (src_loc);
500 size_t to_file_len = strlen (file_path);
501 unsigned char *to_file_quoted =
502 (unsigned char *) alloca (to_file_len * 4 + 1);
504 /* cpp_quote_string does not nul-terminate, so we have to do it
505 ourselves. */
506 unsigned char *p = cpp_quote_string (to_file_quoted,
507 (const unsigned char *) file_path,
508 to_file_len);
509 *p = '\0';
511 print.src_line = LOCATION_LINE (src_loc);
512 print.src_file = file_path;
514 fprintf (stream, "# %u \"%s\"%s",
515 print.src_line, to_file_quoted, special_flags);
517 int sysp = in_system_header_at (src_loc);
518 if (sysp == 2)
519 fputs (" 3 4", stream);
520 else if (sysp == 1)
521 fputs (" 3", stream);
523 putc ('\n', stream);
524 emitted_line_marker = true;
527 return emitted_line_marker;
530 /* Output a line marker for logical line LINE. Special flags are "1"
531 or "2" indicating entering or leaving a file. Return TRUE if a
532 line marker was effectively emitted, FALSE otherwise. */
534 static bool
535 print_line (location_t src_loc, const char *special_flags)
537 if (cpp_get_options (parse_in)->debug)
538 linemap_dump_location (line_table, src_loc,
539 print.outf);
540 return print_line_1 (src_loc, special_flags, print.outf);
543 /* Helper function for cb_line_change and scan_translation_unit.
544 Return TRUE if a line marker is emitted, FALSE otherwise. */
545 static bool
546 do_line_change (cpp_reader *pfile, const cpp_token *token,
547 location_t src_loc, int parsing_args)
549 bool emitted_line_marker = false;
550 if (define_queue || undef_queue)
551 dump_queued_macros (pfile);
553 if (token->type == CPP_EOF || parsing_args)
554 return false;
556 emitted_line_marker = maybe_print_line (src_loc);
557 print.prev = 0;
558 print.source = 0;
560 /* Supply enough spaces to put this token in its original column,
561 one space per column greater than 2, since scan_translation_unit
562 will provide a space if PREV_WHITE. Don't bother trying to
563 reconstruct tabs; we can't get it right in general, and nothing
564 ought to care. Some things do care; the fault lies with them. */
565 if (!CPP_OPTION (pfile, traditional))
567 int spaces = LOCATION_COLUMN (src_loc) - 2;
568 print.printed = true;
570 while (-- spaces >= 0)
571 putc (' ', print.outf);
574 return emitted_line_marker;
577 /* Called when a line of output is started. TOKEN is the first token
578 of the line, and at end of file will be CPP_EOF. */
579 static void
580 cb_line_change (cpp_reader *pfile, const cpp_token *token,
581 int parsing_args)
583 do_line_change (pfile, token, token->src_loc, parsing_args);
586 static void
587 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
588 const cpp_string *str)
590 maybe_print_line (line);
591 fprintf (print.outf, "#ident %s\n", str->text);
592 print.src_line++;
595 static void
596 cb_define (cpp_reader *pfile, location_t line, cpp_hashnode *node)
598 const line_map_ordinary *map;
600 maybe_print_line (line);
601 fputs ("#define ", print.outf);
603 /* 'D' is whole definition; 'N' is name only. */
604 if (flag_dump_macros == 'D')
605 fputs ((const char *) cpp_macro_definition (pfile, node),
606 print.outf);
607 else
608 fputs ((const char *) NODE_NAME (node), print.outf);
610 putc ('\n', print.outf);
611 print.printed = false;
612 linemap_resolve_location (line_table, line,
613 LRK_MACRO_DEFINITION_LOCATION,
614 &map);
615 print.src_line++;
618 static void
619 cb_undef (cpp_reader *pfile, location_t line, cpp_hashnode *node)
621 if (lang_hooks.preprocess_undef)
622 lang_hooks.preprocess_undef (pfile, line, node);
623 maybe_print_line (line);
624 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
625 print.src_line++;
628 static void
629 cb_used_define (cpp_reader *pfile, location_t line ATTRIBUTE_UNUSED,
630 cpp_hashnode *node)
632 if (cpp_user_macro_p (node))
634 macro_queue *q;
635 q = XNEW (macro_queue);
636 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
637 q->next = define_queue;
638 define_queue = q;
642 static void
643 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
644 location_t line ATTRIBUTE_UNUSED,
645 cpp_hashnode *node)
647 macro_queue *q;
648 q = XNEW (macro_queue);
649 q->macro = xstrdup ((const char *) NODE_NAME (node));
650 q->next = undef_queue;
651 undef_queue = q;
654 static void
655 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
657 macro_queue *q;
659 /* End the previous line of text. */
660 if (print.printed)
662 putc ('\n', print.outf);
663 print.src_line++;
664 print.printed = false;
667 for (q = define_queue; q;)
669 macro_queue *oq;
670 fputs ("#define ", print.outf);
671 fputs (q->macro, print.outf);
672 putc ('\n', print.outf);
673 print.printed = false;
674 print.src_line++;
675 oq = q;
676 q = q->next;
677 free (oq->macro);
678 free (oq);
680 define_queue = NULL;
681 for (q = undef_queue; q;)
683 macro_queue *oq;
684 fprintf (print.outf, "#undef %s\n", q->macro);
685 print.src_line++;
686 oq = q;
687 q = q->next;
688 free (oq->macro);
689 free (oq);
691 undef_queue = NULL;
694 static void
695 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
696 const unsigned char *dir, const char *header, int angle_brackets,
697 const cpp_token **comments)
699 maybe_print_line (line);
700 if (angle_brackets)
701 fprintf (print.outf, "#%s <%s>", dir, header);
702 else
703 fprintf (print.outf, "#%s \"%s\"", dir, header);
705 if (comments != NULL)
707 while (*comments != NULL)
709 if ((*comments)->flags & PREV_WHITE)
710 putc (' ', print.outf);
711 cpp_output_token (*comments, print.outf);
712 ++comments;
716 putc ('\n', print.outf);
717 print.printed = false;
718 print.src_line++;
721 /* Callback called when -fworking-director and -E to emit working
722 directory in cpp output file. */
724 void
725 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
727 size_t to_file_len = strlen (dir);
728 unsigned char *to_file_quoted =
729 (unsigned char *) alloca (to_file_len * 4 + 1);
730 unsigned char *p;
732 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
733 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
734 *p = '\0';
735 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
738 /* The file name, line number or system header flags have changed, as
739 described in MAP. */
741 void
742 pp_file_change (const line_map_ordinary *map)
744 const char *flags = "";
746 if (flag_no_line_commands)
747 return;
749 if (map != NULL)
751 input_location = map->start_location;
752 if (print.first_time)
754 /* Avoid printing foo.i when the main file is foo.c. */
755 if (!cpp_get_options (parse_in)->preprocessed)
756 print_line (map->start_location, flags);
757 print.first_time = 0;
759 else
761 /* Bring current file to correct line when entering a new file. */
762 if (map->reason == LC_ENTER)
764 maybe_print_line (linemap_included_from (map));
765 flags = " 1";
767 else if (map->reason == LC_LEAVE)
768 flags = " 2";
769 print_line (map->start_location, flags);
774 /* Copy a #pragma directive to the preprocessed output. */
775 static void
776 cb_def_pragma (cpp_reader *pfile, location_t line)
778 maybe_print_line (line);
779 fputs ("#pragma ", print.outf);
780 cpp_output_line (pfile, print.outf);
781 print.printed = false;
782 print.src_line++;
785 /* Dump out the hash table. */
786 static int
787 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
789 if (cpp_user_macro_p (node))
791 fputs ("#define ", print.outf);
792 fputs ((const char *) cpp_macro_definition (pfile, node),
793 print.outf);
794 putc ('\n', print.outf);
795 print.printed = false;
796 print.src_line++;
799 return 1;
802 /* Load in the PCH file NAME, open on FD. It was originally searched for
803 by ORIG_NAME. Also, print out a #include command so that the PCH
804 file can be loaded when the preprocessed output is compiled. */
806 static void
807 cb_read_pch (cpp_reader *pfile, const char *name,
808 int fd, const char *orig_name ATTRIBUTE_UNUSED)
810 c_common_read_pch (pfile, name, fd, orig_name);
812 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
813 print.src_line++;