]> gcc.gnu.org Git - gcc.git/blob - gcc/c-family/c-ppoutput.c
preprocessor: Better line info for <builtin> & <command-line>
[gcc.git] / gcc / c-family / c-ppoutput.c
1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995-2020 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 "c-common.h" /* For flags. */
23 #include "../libcpp/internal.h"
24 #include "c-pragma.h" /* For parse_in. */
25 #include "file-prefix-map.h" /* remap_macro_filename() */
26
27 /* Encapsulates state used to convert a stream of tokens into a text
28 file. */
29 static struct
30 {
31 FILE *outf; /* Stream to write to. */
32 const cpp_token *prev; /* Previous token. */
33 const cpp_token *source; /* Source token for spacing. */
34 int src_line; /* Line number currently being written. */
35 bool printed; /* True if something output at line. */
36 bool first_time; /* pp_file_change hasn't been called yet. */
37 bool prev_was_system_token; /* True if the previous token was a
38 system token.*/
39 const char *src_file; /* Current source file. */
40 } print;
41
42 /* Defined and undefined macros being queued for output with -dU at
43 the next newline. */
44 struct macro_queue
45 {
46 struct macro_queue *next; /* Next macro in the list. */
47 char *macro; /* The name of the macro if not
48 defined, the full definition if
49 defined. */
50 };
51 static macro_queue *define_queue, *undef_queue;
52
53 /* General output routines. */
54 static void scan_translation_unit (cpp_reader *);
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 *);
60
61 static bool print_line_1 (location_t, const char*, FILE *);
62 static bool print_line (location_t, const char *);
63 static bool maybe_print_line_1 (location_t, FILE *);
64 static bool maybe_print_line (location_t);
65 static bool do_line_change (cpp_reader *, const cpp_token *,
66 location_t, int);
67
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 *, location_t, cpp_hashnode *);
72 static void cb_undef (cpp_reader *, location_t, cpp_hashnode *);
73 static void cb_used_define (cpp_reader *, location_t, cpp_hashnode *);
74 static void cb_used_undef (cpp_reader *, location_t, cpp_hashnode *);
75 static void cb_include (cpp_reader *, location_t, const unsigned char *,
76 const char *, int, const cpp_token **);
77 static void cb_ident (cpp_reader *, location_t, const cpp_string *);
78 static void cb_def_pragma (cpp_reader *, location_t);
79 static void cb_read_pch (cpp_reader *pfile, const char *name,
80 int fd, const char *orig_name);
81
82 /* Preprocess and output. */
83 void
84 preprocess_file (cpp_reader *pfile)
85 {
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)
89 {
90 /* Scan -included buffers, then the main file. */
91 while (pfile->buffer->prev)
92 cpp_scan_nooutput (pfile);
93 cpp_scan_nooutput (pfile);
94 }
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);
102
103 /* -dM command line option. Should this be elsewhere? */
104 if (flag_dump_macros == 'M')
105 cpp_forall_identifiers (pfile, dump_macro, NULL);
106
107 /* Flush any pending output. */
108 if (print.printed)
109 putc ('\n', print.outf);
110 }
111
112 /* Set up the callbacks as appropriate. */
113 void
114 init_pp_output (FILE *out_stream)
115 {
116 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
117
118 if (!flag_no_output)
119 {
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)
124 {
125 cb->ident = cb_ident;
126 cb->def_pragma = cb_def_pragma;
127 }
128 }
129
130 if (flag_dump_includes)
131 cb->include = cb_include;
132
133 if (flag_pch_preprocess)
134 {
135 cb->valid_pch = c_common_valid_pch;
136 cb->read_pch = cb_read_pch;
137 }
138
139 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
140 {
141 cb->define = cb_define;
142 cb->undef = cb_undef;
143 }
144
145 if (flag_dump_macros == 'U')
146 {
147 cb->before_define = dump_queued_macros;
148 cb->used_define = cb_used_define;
149 cb->used_undef = cb_used_undef;
150 }
151
152 cb->has_attribute = c_common_has_attribute;
153 cb->has_builtin = c_common_has_builtin;
154 cb->get_source_date_epoch = cb_get_source_date_epoch;
155 cb->remap_filename = remap_macro_filename;
156
157 /* Initialize the print structure. */
158 print.src_line = 1;
159 print.printed = false;
160 print.prev = 0;
161 print.outf = out_stream;
162 print.first_time = 1;
163 print.src_file = "";
164 print.prev_was_system_token = false;
165 }
166
167 // FIXME: Ideally we'd just turn the entirety of the print struct into
168 // an encapsulated streamer ...
169
170 class token_streamer
171 {
172 bool avoid_paste;
173 bool do_line_adjustments;
174 bool in_pragma;
175 bool line_marker_emitted;
176
177 public:
178 token_streamer (cpp_reader *pfile)
179 :avoid_paste (false),
180 do_line_adjustments (cpp_get_options (pfile)->lang != CLK_ASM
181 && !flag_no_line_commands),
182 in_pragma (false),
183 line_marker_emitted (false)
184 {
185 }
186
187 void begin_pragma ()
188 {
189 in_pragma = true;
190 }
191
192 void stream (cpp_reader *pfile, const cpp_token *tok, location_t);
193 };
194
195 void
196 token_streamer::stream (cpp_reader *pfile, const cpp_token *token,
197 location_t loc)
198 {
199 if (token->type == CPP_PADDING)
200 {
201 avoid_paste = true;
202 if (print.source == NULL
203 || (!(print.source->flags & PREV_WHITE)
204 && token->val.source == NULL))
205 print.source = token->val.source;
206 return;
207 }
208
209 if (token->type == CPP_EOF)
210 return;
211
212 /* Subtle logic to output a space if and only if necessary. */
213 if (avoid_paste)
214 {
215 int src_line = LOCATION_LINE (loc);
216
217 if (print.source == NULL)
218 print.source = token;
219
220 if (src_line != print.src_line
221 && do_line_adjustments
222 && !in_pragma)
223 {
224 line_marker_emitted = do_line_change (pfile, token, loc, false);
225 putc (' ', print.outf);
226 print.printed = true;
227 }
228 else if (print.source->flags & PREV_WHITE
229 || (print.prev
230 && cpp_avoid_paste (pfile, print.prev, token))
231 || (print.prev == NULL && token->type == CPP_HASH))
232 {
233 putc (' ', print.outf);
234 print.printed = true;
235 }
236 }
237 else if (token->flags & PREV_WHITE)
238 {
239 int src_line = LOCATION_LINE (loc);
240
241 if (src_line != print.src_line
242 && do_line_adjustments
243 && !in_pragma)
244 line_marker_emitted = do_line_change (pfile, token, loc, false);
245 putc (' ', print.outf);
246 print.printed = true;
247 }
248
249 avoid_paste = false;
250 print.source = NULL;
251 print.prev = token;
252 if (token->type == CPP_PRAGMA)
253 {
254 const char *space;
255 const char *name;
256
257 line_marker_emitted = maybe_print_line (token->src_loc);
258 fputs ("#pragma ", print.outf);
259 c_pp_lookup_pragma (token->val.pragma, &space, &name);
260 if (space)
261 fprintf (print.outf, "%s %s", space, name);
262 else
263 fprintf (print.outf, "%s", name);
264 print.printed = true;
265 in_pragma = true;
266 }
267 else if (token->type == CPP_PRAGMA_EOL)
268 {
269 maybe_print_line (UNKNOWN_LOCATION);
270 in_pragma = false;
271 }
272 else
273 {
274 if (cpp_get_options (parse_in)->debug)
275 linemap_dump_location (line_table, token->src_loc, print.outf);
276
277 if (do_line_adjustments
278 && !in_pragma
279 && !line_marker_emitted
280 && print.prev_was_system_token != !!in_system_header_at (loc)
281 && !is_location_from_builtin_token (loc))
282 /* The system-ness of this token is different from the one of
283 the previous token. Let's emit a line change to mark the
284 new system-ness before we emit the token. */
285 {
286 do_line_change (pfile, token, loc, false);
287 print.prev_was_system_token = !!in_system_header_at (loc);
288 }
289 cpp_output_token (token, print.outf);
290 line_marker_emitted = false;
291 print.printed = true;
292 }
293
294 /* CPP_COMMENT tokens and raw-string literal tokens can have
295 embedded new-line characters. Rather than enumerating all the
296 possible token types just check if token uses val.str union
297 member. */
298 if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
299 account_for_newlines (token->val.str.text, token->val.str.len);
300 }
301
302 /* Writes out the preprocessed file, handling spacing and paste
303 avoidance issues. */
304 static void
305 scan_translation_unit (cpp_reader *pfile)
306 {
307 bool avoid_paste = false;
308 bool do_line_adjustments
309 = cpp_get_options (parse_in)->lang != CLK_ASM
310 && !flag_no_line_commands;
311 bool in_pragma = false;
312 bool line_marker_emitted = false;
313
314 print.source = NULL;
315 for (;;)
316 {
317 location_t loc;
318 const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
319
320 if (token->type == CPP_PADDING)
321 {
322 avoid_paste = true;
323 if (print.source == NULL
324 || (!(print.source->flags & PREV_WHITE)
325 && token->val.source == NULL))
326 print.source = token->val.source;
327 continue;
328 }
329
330 if (token->type == CPP_EOF)
331 break;
332
333 /* Subtle logic to output a space if and only if necessary. */
334 if (avoid_paste)
335 {
336 int src_line = LOCATION_LINE (loc);
337
338 if (print.source == NULL)
339 print.source = token;
340
341 if (src_line != print.src_line
342 && do_line_adjustments
343 && !in_pragma)
344 {
345 line_marker_emitted = do_line_change (pfile, token, loc, false);
346 putc (' ', print.outf);
347 print.printed = true;
348 }
349 else if (print.source->flags & PREV_WHITE
350 || (print.prev
351 && cpp_avoid_paste (pfile, print.prev, token))
352 || (print.prev == NULL && token->type == CPP_HASH))
353 {
354 putc (' ', print.outf);
355 print.printed = true;
356 }
357 }
358 else if (token->flags & PREV_WHITE)
359 {
360 int src_line = LOCATION_LINE (loc);
361
362 if (src_line != print.src_line
363 && do_line_adjustments
364 && !in_pragma)
365 line_marker_emitted = do_line_change (pfile, token, loc, false);
366 putc (' ', print.outf);
367 print.printed = true;
368 }
369
370 avoid_paste = false;
371 print.source = NULL;
372 print.prev = token;
373 if (token->type == CPP_PRAGMA)
374 {
375 const char *space;
376 const char *name;
377
378 line_marker_emitted = maybe_print_line (token->src_loc);
379 fputs ("#pragma ", print.outf);
380 c_pp_lookup_pragma (token->val.pragma, &space, &name);
381 if (space)
382 fprintf (print.outf, "%s %s", space, name);
383 else
384 fprintf (print.outf, "%s", name);
385 print.printed = true;
386 in_pragma = true;
387 }
388 else if (token->type == CPP_PRAGMA_EOL)
389 {
390 maybe_print_line (token->src_loc);
391 in_pragma = false;
392 }
393 else
394 {
395 if (cpp_get_options (parse_in)->debug)
396 linemap_dump_location (line_table, token->src_loc, print.outf);
397
398 if (do_line_adjustments
399 && !in_pragma
400 && !line_marker_emitted
401 && print.prev_was_system_token != !!in_system_header_at (loc)
402 && !is_location_from_builtin_token (loc))
403 /* The system-ness of this token is different from the one
404 of the previous token. Let's emit a line change to
405 mark the new system-ness before we emit the token. */
406 {
407 do_line_change (pfile, token, loc, false);
408 print.prev_was_system_token = !!in_system_header_at (loc);
409 }
410 cpp_output_token (token, print.outf);
411 line_marker_emitted = false;
412 print.printed = true;
413 }
414
415 /* CPP_COMMENT tokens and raw-string literal tokens can
416 have embedded new-line characters. Rather than enumerating
417 all the possible token types just check if token uses
418 val.str union member. */
419 if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
420 account_for_newlines (token->val.str.text, token->val.str.len);
421 }
422 }
423
424 static void
425 directives_only_cb (cpp_reader *pfile, CPP_DO_task task, void *data_, ...)
426 {
427 va_list args;
428 va_start (args, data_);
429
430 token_streamer *streamer = reinterpret_cast <token_streamer *> (data_);
431 switch (task)
432 {
433 default:
434 gcc_unreachable ();
435
436 case CPP_DO_print:
437 {
438 print.src_line += va_arg (args, unsigned);
439
440 const void *buf = va_arg (args, const void *);
441 size_t size = va_arg (args, size_t);
442 fwrite (buf, 1, size, print.outf);
443 }
444 break;
445
446 case CPP_DO_location:
447 maybe_print_line (va_arg (args, location_t));
448 break;
449
450 case CPP_DO_token:
451 {
452 const cpp_token *token = va_arg (args, const cpp_token *);
453 location_t spelling_loc = va_arg (args, location_t);
454 streamer->stream (pfile, token, spelling_loc);
455 }
456 break;
457 }
458
459 va_end (args);
460 }
461
462 /* Writes out the preprocessed file, handling spacing and paste
463 avoidance issues. */
464 static void
465 scan_translation_unit_directives_only (cpp_reader *pfile)
466 {
467 token_streamer streamer (pfile);
468 cpp_directive_only_process (pfile, &streamer, directives_only_cb);
469 }
470
471 /* Adjust print.src_line for newlines embedded in output. */
472 static void
473 account_for_newlines (const unsigned char *str, size_t len)
474 {
475 while (len--)
476 if (*str++ == '\n')
477 print.src_line++;
478 }
479
480 /* Writes out a traditionally preprocessed file. */
481 static void
482 scan_translation_unit_trad (cpp_reader *pfile)
483 {
484 while (_cpp_read_logical_line_trad (pfile))
485 {
486 size_t len = pfile->out.cur - pfile->out.base;
487 maybe_print_line (pfile->out.first_line);
488 fwrite (pfile->out.base, 1, len, print.outf);
489 print.printed = true;
490 if (!CPP_OPTION (pfile, discard_comments))
491 account_for_newlines (pfile->out.base, len);
492 }
493 }
494
495 /* If the token read on logical line LINE needs to be output on a
496 different line to the current one, output the required newlines or
497 a line marker. If a line marker was emitted, return TRUE otherwise
498 return FALSE. */
499
500 static bool
501 maybe_print_line_1 (location_t src_loc, FILE *stream)
502 {
503 bool emitted_line_marker = false;
504 int src_line = LOCATION_LINE (src_loc);
505 const char *src_file = LOCATION_FILE (src_loc);
506
507 /* End the previous line of text. */
508 if (print.printed)
509 {
510 putc ('\n', stream);
511 print.src_line++;
512 print.printed = false;
513 }
514
515 if (!flag_no_line_commands
516 && src_line >= print.src_line
517 && src_line < print.src_line + 8
518 && strcmp (src_file, print.src_file) == 0)
519 {
520 while (src_line > print.src_line)
521 {
522 putc ('\n', stream);
523 print.src_line++;
524 }
525 }
526 else
527 emitted_line_marker = print_line_1 (src_loc, "", stream);
528
529 return emitted_line_marker;
530 }
531
532 /* If the token read on logical line LINE needs to be output on a
533 different line to the current one, output the required newlines or
534 a line marker. If a line marker was emitted, return TRUE otherwise
535 return FALSE. */
536
537 static bool
538 maybe_print_line (location_t src_loc)
539 {
540 if (cpp_get_options (parse_in)->debug)
541 linemap_dump_location (line_table, src_loc,
542 print.outf);
543 return maybe_print_line_1 (src_loc, print.outf);
544 }
545
546 /* Output a line marker for logical line LINE. Special flags are "1"
547 or "2" indicating entering or leaving a file. If the line marker
548 was effectively emitted, return TRUE otherwise return FALSE. */
549
550 static bool
551 print_line_1 (location_t src_loc, const char *special_flags, FILE *stream)
552 {
553 bool emitted_line_marker = false;
554
555 /* End any previous line of text. */
556 if (print.printed)
557 putc ('\n', stream);
558 print.printed = false;
559
560 if (src_loc != UNKNOWN_LOCATION && !flag_no_line_commands)
561 {
562 const char *file_path = LOCATION_FILE (src_loc);
563 size_t to_file_len = strlen (file_path);
564 unsigned char *to_file_quoted =
565 (unsigned char *) alloca (to_file_len * 4 + 1);
566
567 print.src_line = LOCATION_LINE (src_loc);
568 print.src_file = file_path;
569
570 /* cpp_quote_string does not nul-terminate, so we have to do it
571 ourselves. */
572 unsigned char *p = cpp_quote_string (to_file_quoted,
573 (const unsigned char *) file_path,
574 to_file_len);
575 *p = '\0';
576 fprintf (stream, "# %u \"%s\"%s",
577 print.src_line, to_file_quoted, special_flags);
578
579 int sysp = in_system_header_at (src_loc);
580 if (sysp == 2)
581 fputs (" 3 4", stream);
582 else if (sysp == 1)
583 fputs (" 3", stream);
584
585 putc ('\n', stream);
586 emitted_line_marker = true;
587 }
588
589 return emitted_line_marker;
590 }
591
592 /* Output a line marker for logical line LINE. Special flags are "1"
593 or "2" indicating entering or leaving a file. Return TRUE if a
594 line marker was effectively emitted, FALSE otherwise. */
595
596 static bool
597 print_line (location_t src_loc, const char *special_flags)
598 {
599 if (cpp_get_options (parse_in)->debug)
600 linemap_dump_location (line_table, src_loc,
601 print.outf);
602 return print_line_1 (src_loc, special_flags, print.outf);
603 }
604
605 /* Helper function for cb_line_change and scan_translation_unit.
606 Return TRUE if a line marker is emitted, FALSE otherwise. */
607 static bool
608 do_line_change (cpp_reader *pfile, const cpp_token *token,
609 location_t src_loc, int parsing_args)
610 {
611 bool emitted_line_marker = false;
612 if (define_queue || undef_queue)
613 dump_queued_macros (pfile);
614
615 if (token->type == CPP_EOF || parsing_args)
616 return false;
617
618 emitted_line_marker = maybe_print_line (src_loc);
619 print.prev = 0;
620 print.source = 0;
621
622 /* Supply enough spaces to put this token in its original column,
623 one space per column greater than 2, since scan_translation_unit
624 will provide a space if PREV_WHITE. Don't bother trying to
625 reconstruct tabs; we can't get it right in general, and nothing
626 ought to care. Some things do care; the fault lies with them. */
627 if (!CPP_OPTION (pfile, traditional))
628 {
629 int spaces = LOCATION_COLUMN (src_loc) - 2;
630 print.printed = true;
631
632 while (-- spaces >= 0)
633 putc (' ', print.outf);
634 }
635
636 return emitted_line_marker;
637 }
638
639 /* Called when a line of output is started. TOKEN is the first token
640 of the line, and at end of file will be CPP_EOF. */
641 static void
642 cb_line_change (cpp_reader *pfile, const cpp_token *token,
643 int parsing_args)
644 {
645 do_line_change (pfile, token, token->src_loc, parsing_args);
646 }
647
648 static void
649 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
650 const cpp_string *str)
651 {
652 maybe_print_line (line);
653 fprintf (print.outf, "#ident %s\n", str->text);
654 print.src_line++;
655 }
656
657 static void
658 cb_define (cpp_reader *pfile, location_t line, cpp_hashnode *node)
659 {
660 const line_map_ordinary *map;
661
662 maybe_print_line (line);
663 fputs ("#define ", print.outf);
664
665 /* 'D' is whole definition; 'N' is name only. */
666 if (flag_dump_macros == 'D')
667 fputs ((const char *) cpp_macro_definition (pfile, node),
668 print.outf);
669 else
670 fputs ((const char *) NODE_NAME (node), print.outf);
671
672 putc ('\n', print.outf);
673 print.printed = false;
674 linemap_resolve_location (line_table, line,
675 LRK_MACRO_DEFINITION_LOCATION,
676 &map);
677 print.src_line++;
678 }
679
680 static void
681 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
682 cpp_hashnode *node)
683 {
684 maybe_print_line (line);
685 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
686 print.src_line++;
687 }
688
689 static void
690 cb_used_define (cpp_reader *pfile, location_t line ATTRIBUTE_UNUSED,
691 cpp_hashnode *node)
692 {
693 if (cpp_user_macro_p (node))
694 {
695 macro_queue *q;
696 q = XNEW (macro_queue);
697 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
698 q->next = define_queue;
699 define_queue = q;
700 }
701 }
702
703 static void
704 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
705 location_t line ATTRIBUTE_UNUSED,
706 cpp_hashnode *node)
707 {
708 macro_queue *q;
709 q = XNEW (macro_queue);
710 q->macro = xstrdup ((const char *) NODE_NAME (node));
711 q->next = undef_queue;
712 undef_queue = q;
713 }
714
715 static void
716 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
717 {
718 macro_queue *q;
719
720 /* End the previous line of text. */
721 if (print.printed)
722 {
723 putc ('\n', print.outf);
724 print.src_line++;
725 print.printed = false;
726 }
727
728 for (q = define_queue; q;)
729 {
730 macro_queue *oq;
731 fputs ("#define ", print.outf);
732 fputs (q->macro, print.outf);
733 putc ('\n', print.outf);
734 print.printed = false;
735 print.src_line++;
736 oq = q;
737 q = q->next;
738 free (oq->macro);
739 free (oq);
740 }
741 define_queue = NULL;
742 for (q = undef_queue; q;)
743 {
744 macro_queue *oq;
745 fprintf (print.outf, "#undef %s\n", q->macro);
746 print.src_line++;
747 oq = q;
748 q = q->next;
749 free (oq->macro);
750 free (oq);
751 }
752 undef_queue = NULL;
753 }
754
755 static void
756 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
757 const unsigned char *dir, const char *header, int angle_brackets,
758 const cpp_token **comments)
759 {
760 maybe_print_line (line);
761 if (angle_brackets)
762 fprintf (print.outf, "#%s <%s>", dir, header);
763 else
764 fprintf (print.outf, "#%s \"%s\"", dir, header);
765
766 if (comments != NULL)
767 {
768 while (*comments != NULL)
769 {
770 if ((*comments)->flags & PREV_WHITE)
771 putc (' ', print.outf);
772 cpp_output_token (*comments, print.outf);
773 ++comments;
774 }
775 }
776
777 putc ('\n', print.outf);
778 print.printed = false;
779 print.src_line++;
780 }
781
782 /* Callback called when -fworking-director and -E to emit working
783 directory in cpp output file. */
784
785 void
786 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
787 {
788 size_t to_file_len = strlen (dir);
789 unsigned char *to_file_quoted =
790 (unsigned char *) alloca (to_file_len * 4 + 1);
791 unsigned char *p;
792
793 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
794 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
795 *p = '\0';
796 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
797 }
798
799 /* The file name, line number or system header flags have changed, as
800 described in MAP. */
801
802 void
803 pp_file_change (const line_map_ordinary *map)
804 {
805 const char *flags = "";
806
807 if (flag_no_line_commands)
808 return;
809
810 if (map != NULL)
811 {
812 input_location = map->start_location;
813 if (print.first_time)
814 {
815 /* Avoid printing foo.i when the main file is foo.c. */
816 if (!cpp_get_options (parse_in)->preprocessed)
817 print_line (map->start_location, flags);
818 print.first_time = 0;
819 }
820 else
821 {
822 /* Bring current file to correct line when entering a new file. */
823 if (map->reason == LC_ENTER)
824 {
825 maybe_print_line (linemap_included_from (map));
826 flags = " 1";
827 }
828 else if (map->reason == LC_LEAVE)
829 flags = " 2";
830 print_line (map->start_location, flags);
831 }
832 }
833 }
834
835 /* Copy a #pragma directive to the preprocessed output. */
836 static void
837 cb_def_pragma (cpp_reader *pfile, location_t line)
838 {
839 maybe_print_line (line);
840 fputs ("#pragma ", print.outf);
841 cpp_output_line (pfile, print.outf);
842 print.printed = false;
843 print.src_line++;
844 }
845
846 /* Dump out the hash table. */
847 static int
848 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
849 {
850 if (cpp_user_macro_p (node))
851 {
852 fputs ("#define ", print.outf);
853 fputs ((const char *) cpp_macro_definition (pfile, node),
854 print.outf);
855 putc ('\n', print.outf);
856 print.printed = false;
857 print.src_line++;
858 }
859
860 return 1;
861 }
862
863 /* Load in the PCH file NAME, open on FD. It was originally searched for
864 by ORIG_NAME. Also, print out a #include command so that the PCH
865 file can be loaded when the preprocessed output is compiled. */
866
867 static void
868 cb_read_pch (cpp_reader *pfile, const char *name,
869 int fd, const char *orig_name ATTRIBUTE_UNUSED)
870 {
871 c_common_read_pch (pfile, name, fd, orig_name);
872
873 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
874 print.src_line++;
875 }
This page took 0.070751 seconds and 5 git commands to generate.