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