[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / gcc / c-family / c-ppoutput.c
blob085555459eb71048fc1e2db448e9dca6f01af783
1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995-2015 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 "cpplib.h"
23 #include "../libcpp/internal.h"
24 #include "alias.h"
25 #include "tree.h"
26 #include "options.h"
27 #include "c-common.h" /* For flags. */
28 #include "c-pragma.h" /* For parse_in. */
30 /* Encapsulates state used to convert a stream of tokens into a text
31 file. */
32 static struct
34 FILE *outf; /* Stream to write to. */
35 const cpp_token *prev; /* Previous token. */
36 const cpp_token *source; /* Source token for spacing. */
37 int src_line; /* Line number currently being written. */
38 unsigned char printed; /* Nonzero if something output at line. */
39 bool first_time; /* pp_file_change hasn't been called yet. */
40 const char *src_file; /* Current source file. */
41 bool prev_was_system_token; /* True if the previous token was a
42 system token.*/
43 } print;
45 /* Defined and undefined macros being queued for output with -dU at
46 the next newline. */
47 struct macro_queue
49 struct macro_queue *next; /* Next macro in the list. */
50 char *macro; /* The name of the macro if not
51 defined, the full definition if
52 defined. */
54 static macro_queue *define_queue, *undef_queue;
56 /* General output routines. */
57 static void scan_translation_unit (cpp_reader *);
58 static void print_lines_directives_only (int, const void *, size_t);
59 static void scan_translation_unit_directives_only (cpp_reader *);
60 static void scan_translation_unit_trad (cpp_reader *);
61 static void account_for_newlines (const unsigned char *, size_t);
62 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
63 static void dump_queued_macros (cpp_reader *);
65 static bool print_line_1 (source_location, const char*, FILE *);
66 static bool print_line (source_location, const char *);
67 static bool maybe_print_line_1 (source_location, FILE *);
68 static bool maybe_print_line (source_location);
69 static bool do_line_change (cpp_reader *, const cpp_token *,
70 source_location, int);
72 /* Callback routines for the parser. Most of these are active only
73 in specific modes. */
74 static void cb_line_change (cpp_reader *, const cpp_token *, int);
75 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
76 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
77 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
78 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
79 static void cb_include (cpp_reader *, source_location, const unsigned char *,
80 const char *, int, const cpp_token **);
81 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
82 static void cb_def_pragma (cpp_reader *, source_location);
83 static void cb_read_pch (cpp_reader *pfile, const char *name,
84 int fd, const char *orig_name);
86 /* Preprocess and output. */
87 void
88 preprocess_file (cpp_reader *pfile)
90 /* A successful cpp_read_main_file guarantees that we can call
91 cpp_scan_nooutput or cpp_get_token next. */
92 if (flag_no_output && pfile->buffer)
94 /* Scan -included buffers, then the main file. */
95 while (pfile->buffer->prev)
96 cpp_scan_nooutput (pfile);
97 cpp_scan_nooutput (pfile);
99 else if (cpp_get_options (pfile)->traditional)
100 scan_translation_unit_trad (pfile);
101 else if (cpp_get_options (pfile)->directives_only
102 && !cpp_get_options (pfile)->preprocessed)
103 scan_translation_unit_directives_only (pfile);
104 else
105 scan_translation_unit (pfile);
107 /* -dM command line option. Should this be elsewhere? */
108 if (flag_dump_macros == 'M')
109 cpp_forall_identifiers (pfile, dump_macro, NULL);
111 /* Flush any pending output. */
112 if (print.printed)
113 putc ('\n', print.outf);
116 /* Set up the callbacks as appropriate. */
117 void
118 init_pp_output (FILE *out_stream)
120 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
122 if (!flag_no_output)
124 cb->line_change = cb_line_change;
125 /* Don't emit #pragma or #ident directives if we are processing
126 assembly language; the assembler may choke on them. */
127 if (cpp_get_options (parse_in)->lang != CLK_ASM)
129 cb->ident = cb_ident;
130 cb->def_pragma = cb_def_pragma;
134 if (flag_dump_includes)
135 cb->include = cb_include;
137 if (flag_pch_preprocess)
139 cb->valid_pch = c_common_valid_pch;
140 cb->read_pch = cb_read_pch;
143 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
145 cb->define = cb_define;
146 cb->undef = cb_undef;
149 if (flag_dump_macros == 'U')
151 cb->before_define = dump_queued_macros;
152 cb->used_define = cb_used_define;
153 cb->used_undef = cb_used_undef;
156 cb->has_attribute = c_common_has_attribute;
158 /* Initialize the print structure. */
159 print.src_line = 1;
160 print.printed = 0;
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 /* Writes out the preprocessed file, handling spacing and paste
169 avoidance issues. */
170 static void
171 scan_translation_unit (cpp_reader *pfile)
173 bool avoid_paste = false;
174 bool do_line_adjustments
175 = cpp_get_options (parse_in)->lang != CLK_ASM
176 && !flag_no_line_commands;
177 bool in_pragma = false;
178 bool line_marker_emitted = false;
180 print.source = NULL;
181 for (;;)
183 source_location loc;
184 const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
186 if (token->type == CPP_PADDING)
188 avoid_paste = true;
189 if (print.source == NULL
190 || (!(print.source->flags & PREV_WHITE)
191 && token->val.source == NULL))
192 print.source = token->val.source;
193 continue;
196 if (token->type == CPP_EOF)
197 break;
199 /* Subtle logic to output a space if and only if necessary. */
200 if (avoid_paste)
202 int src_line = LOCATION_LINE (loc);
204 if (print.source == NULL)
205 print.source = token;
207 if (src_line != print.src_line
208 && do_line_adjustments
209 && !in_pragma)
211 line_marker_emitted = do_line_change (pfile, token, loc, false);
212 putc (' ', print.outf);
214 else if (print.source->flags & PREV_WHITE
215 || (print.prev
216 && cpp_avoid_paste (pfile, print.prev, token))
217 || (print.prev == NULL && token->type == CPP_HASH))
218 putc (' ', print.outf);
220 else if (token->flags & PREV_WHITE)
222 int src_line = LOCATION_LINE (loc);
224 if (src_line != print.src_line
225 && do_line_adjustments
226 && !in_pragma)
227 line_marker_emitted = do_line_change (pfile, token, loc, false);
228 putc (' ', print.outf);
231 avoid_paste = false;
232 print.source = NULL;
233 print.prev = token;
234 if (token->type == CPP_PRAGMA)
236 const char *space;
237 const char *name;
239 line_marker_emitted = maybe_print_line (token->src_loc);
240 fputs ("#pragma ", print.outf);
241 c_pp_lookup_pragma (token->val.pragma, &space, &name);
242 if (space)
243 fprintf (print.outf, "%s %s", space, name);
244 else
245 fprintf (print.outf, "%s", name);
246 print.printed = 1;
247 in_pragma = true;
249 else if (token->type == CPP_PRAGMA_EOL)
251 maybe_print_line (token->src_loc);
252 in_pragma = false;
254 else
256 if (cpp_get_options (parse_in)->debug)
257 linemap_dump_location (line_table, token->src_loc,
258 print.outf);
260 if (do_line_adjustments
261 && !in_pragma
262 && !line_marker_emitted
263 && print.prev_was_system_token != !!in_system_header_at(loc)
264 && !is_location_from_builtin_token (loc))
265 /* The system-ness of this token is different from the one
266 of the previous token. Let's emit a line change to
267 mark the new system-ness before we emit the token. */
269 do_line_change (pfile, token, loc, false);
270 print.prev_was_system_token = !!in_system_header_at(loc);
272 cpp_output_token (token, print.outf);
273 line_marker_emitted = false;
276 /* CPP_COMMENT tokens and raw-string literal tokens can
277 have embedded new-line characters. Rather than enumerating
278 all the possible token types just check if token uses
279 val.str union member. */
280 if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
281 account_for_newlines (token->val.str.text, token->val.str.len);
285 static void
286 print_lines_directives_only (int lines, const void *buf, size_t size)
288 print.src_line += lines;
289 fwrite (buf, 1, size, print.outf);
292 /* Writes out the preprocessed file, handling spacing and paste
293 avoidance issues. */
294 static void
295 scan_translation_unit_directives_only (cpp_reader *pfile)
297 struct _cpp_dir_only_callbacks cb;
299 cb.print_lines = print_lines_directives_only;
300 cb.maybe_print_line = (void (*) (source_location)) maybe_print_line;
302 _cpp_preprocess_dir_only (pfile, &cb);
305 /* Adjust print.src_line for newlines embedded in output. */
306 static void
307 account_for_newlines (const unsigned char *str, size_t len)
309 while (len--)
310 if (*str++ == '\n')
311 print.src_line++;
314 /* Writes out a traditionally preprocessed file. */
315 static void
316 scan_translation_unit_trad (cpp_reader *pfile)
318 while (_cpp_read_logical_line_trad (pfile))
320 size_t len = pfile->out.cur - pfile->out.base;
321 maybe_print_line (pfile->out.first_line);
322 fwrite (pfile->out.base, 1, len, print.outf);
323 print.printed = 1;
324 if (!CPP_OPTION (pfile, discard_comments))
325 account_for_newlines (pfile->out.base, len);
329 /* If the token read on logical line LINE needs to be output on a
330 different line to the current one, output the required newlines or
331 a line marker. If a line marker was emitted, return TRUE otherwise
332 return FALSE. */
334 static bool
335 maybe_print_line_1 (source_location src_loc, FILE *stream)
337 bool emitted_line_marker = false;
338 int src_line = LOCATION_LINE (src_loc);
339 const char *src_file = LOCATION_FILE (src_loc);
341 /* End the previous line of text. */
342 if (print.printed)
344 putc ('\n', stream);
345 print.src_line++;
346 print.printed = 0;
349 if (!flag_no_line_commands
350 && src_line >= print.src_line
351 && src_line < print.src_line + 8
352 && strcmp (src_file, print.src_file) == 0)
354 while (src_line > print.src_line)
356 putc ('\n', stream);
357 print.src_line++;
360 else
361 emitted_line_marker = print_line_1 (src_loc, "", stream);
363 return emitted_line_marker;
366 /* If the token read on logical line LINE needs to be output on a
367 different line to the current one, output the required newlines or
368 a line marker. If a line marker was emitted, return TRUE otherwise
369 return FALSE. */
371 static bool
372 maybe_print_line (source_location src_loc)
374 if (cpp_get_options (parse_in)->debug)
375 linemap_dump_location (line_table, src_loc,
376 print.outf);
377 return maybe_print_line_1 (src_loc, print.outf);
380 /* Output a line marker for logical line LINE. Special flags are "1"
381 or "2" indicating entering or leaving a file. If the line marker
382 was effectively emitted, return TRUE otherwise return FALSE. */
384 static bool
385 print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
387 bool emitted_line_marker = false;
389 /* End any previous line of text. */
390 if (print.printed)
391 putc ('\n', stream);
392 print.printed = 0;
394 if (!flag_no_line_commands)
396 const char *file_path = LOCATION_FILE (src_loc);
397 int sysp;
398 size_t to_file_len = strlen (file_path);
399 unsigned char *to_file_quoted =
400 (unsigned char *) alloca (to_file_len * 4 + 1);
401 unsigned char *p;
403 print.src_line = LOCATION_LINE (src_loc);
404 print.src_file = file_path;
406 /* cpp_quote_string does not nul-terminate, so we have to do it
407 ourselves. */
408 p = cpp_quote_string (to_file_quoted,
409 (const unsigned char *) file_path,
410 to_file_len);
411 *p = '\0';
412 fprintf (stream, "# %u \"%s\"%s",
413 print.src_line == 0 ? 1 : print.src_line,
414 to_file_quoted, special_flags);
416 sysp = in_system_header_at (src_loc);
417 if (sysp == 2)
418 fputs (" 3 4", stream);
419 else if (sysp == 1)
420 fputs (" 3", stream);
422 putc ('\n', stream);
423 emitted_line_marker = true;
426 return emitted_line_marker;
429 /* Output a line marker for logical line LINE. Special flags are "1"
430 or "2" indicating entering or leaving a file. Return TRUE if a
431 line marker was effectively emitted, FALSE otherwise. */
433 static bool
434 print_line (source_location src_loc, const char *special_flags)
436 if (cpp_get_options (parse_in)->debug)
437 linemap_dump_location (line_table, src_loc,
438 print.outf);
439 return print_line_1 (src_loc, special_flags, print.outf);
442 /* Helper function for cb_line_change and scan_translation_unit.
443 Return TRUE if a line marker is emitted, FALSE otherwise. */
444 static bool
445 do_line_change (cpp_reader *pfile, const cpp_token *token,
446 source_location src_loc, int parsing_args)
448 bool emitted_line_marker = false;
449 if (define_queue || undef_queue)
450 dump_queued_macros (pfile);
452 if (token->type == CPP_EOF || parsing_args)
453 return false;
455 emitted_line_marker = maybe_print_line (src_loc);
456 print.prev = 0;
457 print.source = 0;
459 /* Supply enough spaces to put this token in its original column,
460 one space per column greater than 2, since scan_translation_unit
461 will provide a space if PREV_WHITE. Don't bother trying to
462 reconstruct tabs; we can't get it right in general, and nothing
463 ought to care. Some things do care; the fault lies with them. */
464 if (!CPP_OPTION (pfile, traditional))
466 int spaces = LOCATION_COLUMN (src_loc) - 2;
467 print.printed = 1;
469 while (-- spaces >= 0)
470 putc (' ', print.outf);
473 return emitted_line_marker;
476 /* Called when a line of output is started. TOKEN is the first token
477 of the line, and at end of file will be CPP_EOF. */
478 static void
479 cb_line_change (cpp_reader *pfile, const cpp_token *token,
480 int parsing_args)
482 do_line_change (pfile, token, token->src_loc, parsing_args);
485 static void
486 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
487 const cpp_string *str)
489 maybe_print_line (line);
490 fprintf (print.outf, "#ident %s\n", str->text);
491 print.src_line++;
494 static void
495 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
497 const line_map_ordinary *map;
499 maybe_print_line (line);
500 fputs ("#define ", print.outf);
502 /* 'D' is whole definition; 'N' is name only. */
503 if (flag_dump_macros == 'D')
504 fputs ((const char *) cpp_macro_definition (pfile, node),
505 print.outf);
506 else
507 fputs ((const char *) NODE_NAME (node), print.outf);
509 putc ('\n', print.outf);
510 linemap_resolve_location (line_table, line,
511 LRK_MACRO_DEFINITION_LOCATION,
512 &map);
513 if (LINEMAP_LINE (map) != 0)
514 print.src_line++;
517 static void
518 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
519 cpp_hashnode *node)
521 maybe_print_line (line);
522 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
523 print.src_line++;
526 static void
527 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
528 cpp_hashnode *node)
530 macro_queue *q;
531 if (node->flags & NODE_BUILTIN)
532 return;
533 q = XNEW (macro_queue);
534 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
535 q->next = define_queue;
536 define_queue = q;
539 static void
540 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
541 source_location line ATTRIBUTE_UNUSED,
542 cpp_hashnode *node)
544 macro_queue *q;
545 q = XNEW (macro_queue);
546 q->macro = xstrdup ((const char *) NODE_NAME (node));
547 q->next = undef_queue;
548 undef_queue = q;
551 static void
552 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
554 macro_queue *q;
556 /* End the previous line of text. */
557 if (print.printed)
559 putc ('\n', print.outf);
560 print.src_line++;
561 print.printed = 0;
564 for (q = define_queue; q;)
566 macro_queue *oq;
567 fputs ("#define ", print.outf);
568 fputs (q->macro, print.outf);
569 putc ('\n', print.outf);
570 print.src_line++;
571 oq = q;
572 q = q->next;
573 free (oq->macro);
574 free (oq);
576 define_queue = NULL;
577 for (q = undef_queue; q;)
579 macro_queue *oq;
580 fprintf (print.outf, "#undef %s\n", q->macro);
581 print.src_line++;
582 oq = q;
583 q = q->next;
584 free (oq->macro);
585 free (oq);
587 undef_queue = NULL;
590 static void
591 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
592 const unsigned char *dir, const char *header, int angle_brackets,
593 const cpp_token **comments)
595 maybe_print_line (line);
596 if (angle_brackets)
597 fprintf (print.outf, "#%s <%s>", dir, header);
598 else
599 fprintf (print.outf, "#%s \"%s\"", dir, header);
601 if (comments != NULL)
603 while (*comments != NULL)
605 if ((*comments)->flags & PREV_WHITE)
606 putc (' ', print.outf);
607 cpp_output_token (*comments, print.outf);
608 ++comments;
612 putc ('\n', print.outf);
613 print.src_line++;
616 /* Callback called when -fworking-director and -E to emit working
617 directory in cpp output file. */
619 void
620 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
622 size_t to_file_len = strlen (dir);
623 unsigned char *to_file_quoted =
624 (unsigned char *) alloca (to_file_len * 4 + 1);
625 unsigned char *p;
627 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
628 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
629 *p = '\0';
630 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
633 /* The file name, line number or system header flags have changed, as
634 described in MAP. */
636 void
637 pp_file_change (const line_map_ordinary *map)
639 const char *flags = "";
641 if (flag_no_line_commands)
642 return;
644 if (map != NULL)
646 input_location = map->start_location;
647 if (print.first_time)
649 /* Avoid printing foo.i when the main file is foo.c. */
650 if (!cpp_get_options (parse_in)->preprocessed)
651 print_line (map->start_location, flags);
652 print.first_time = 0;
654 else
656 /* Bring current file to correct line when entering a new file. */
657 if (map->reason == LC_ENTER)
659 const line_map_ordinary *from = INCLUDED_FROM (line_table, map);
660 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
662 if (map->reason == LC_ENTER)
663 flags = " 1";
664 else if (map->reason == LC_LEAVE)
665 flags = " 2";
666 print_line (map->start_location, flags);
671 /* Copy a #pragma directive to the preprocessed output. */
672 static void
673 cb_def_pragma (cpp_reader *pfile, source_location line)
675 maybe_print_line (line);
676 fputs ("#pragma ", print.outf);
677 cpp_output_line (pfile, print.outf);
678 print.src_line++;
681 /* Dump out the hash table. */
682 static int
683 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
685 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
687 fputs ("#define ", print.outf);
688 fputs ((const char *) cpp_macro_definition (pfile, node),
689 print.outf);
690 putc ('\n', print.outf);
691 print.src_line++;
694 return 1;
697 /* Load in the PCH file NAME, open on FD. It was originally searched for
698 by ORIG_NAME. Also, print out a #include command so that the PCH
699 file can be loaded when the preprocessed output is compiled. */
701 static void
702 cb_read_pch (cpp_reader *pfile, const char *name,
703 int fd, const char *orig_name ATTRIBUTE_UNUSED)
705 c_common_read_pch (pfile, name, fd, orig_name);
707 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
708 print.src_line++;