2015-01-23 Bernd Edlinger <bernd.edlinger@hotmail.de>
[official-gcc.git] / gcc / c-family / c-ppoutput.c
blob9b9cba06b393d1d2ad8f8f8a4488557398ab70d2
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 "hash-set.h"
25 #include "machmode.h"
26 #include "vec.h"
27 #include "double-int.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "options.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "c-common.h" /* For flags. */
36 #include "c-pragma.h" /* For parse_in. */
38 /* Encapsulates state used to convert a stream of tokens into a text
39 file. */
40 static struct
42 FILE *outf; /* Stream to write to. */
43 const cpp_token *prev; /* Previous token. */
44 const cpp_token *source; /* Source token for spacing. */
45 int src_line; /* Line number currently being written. */
46 unsigned char printed; /* Nonzero if something output at line. */
47 bool first_time; /* pp_file_change hasn't been called yet. */
48 const char *src_file; /* Current source file. */
49 bool prev_was_system_token; /* True if the previous token was a
50 system token.*/
51 } print;
53 /* Defined and undefined macros being queued for output with -dU at
54 the next newline. */
55 typedef struct macro_queue
57 struct macro_queue *next; /* Next macro in the list. */
58 char *macro; /* The name of the macro if not
59 defined, the full definition if
60 defined. */
61 } macro_queue;
62 static macro_queue *define_queue, *undef_queue;
64 /* General output routines. */
65 static void scan_translation_unit (cpp_reader *);
66 static void print_lines_directives_only (int, const void *, size_t);
67 static void scan_translation_unit_directives_only (cpp_reader *);
68 static void scan_translation_unit_trad (cpp_reader *);
69 static void account_for_newlines (const unsigned char *, size_t);
70 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
71 static void dump_queued_macros (cpp_reader *);
73 static bool print_line_1 (source_location, const char*, FILE *);
74 static bool print_line (source_location, const char *);
75 static bool maybe_print_line_1 (source_location, FILE *);
76 static bool maybe_print_line (source_location);
77 static bool do_line_change (cpp_reader *, const cpp_token *,
78 source_location, int);
80 /* Callback routines for the parser. Most of these are active only
81 in specific modes. */
82 static void cb_line_change (cpp_reader *, const cpp_token *, int);
83 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
84 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
85 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
86 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
87 static void cb_include (cpp_reader *, source_location, const unsigned char *,
88 const char *, int, const cpp_token **);
89 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
90 static void cb_def_pragma (cpp_reader *, source_location);
91 static void cb_read_pch (cpp_reader *pfile, const char *name,
92 int fd, const char *orig_name);
94 /* Preprocess and output. */
95 void
96 preprocess_file (cpp_reader *pfile)
98 /* A successful cpp_read_main_file guarantees that we can call
99 cpp_scan_nooutput or cpp_get_token next. */
100 if (flag_no_output && pfile->buffer)
102 /* Scan -included buffers, then the main file. */
103 while (pfile->buffer->prev)
104 cpp_scan_nooutput (pfile);
105 cpp_scan_nooutput (pfile);
107 else if (cpp_get_options (pfile)->traditional)
108 scan_translation_unit_trad (pfile);
109 else if (cpp_get_options (pfile)->directives_only
110 && !cpp_get_options (pfile)->preprocessed)
111 scan_translation_unit_directives_only (pfile);
112 else
113 scan_translation_unit (pfile);
115 /* -dM command line option. Should this be elsewhere? */
116 if (flag_dump_macros == 'M')
117 cpp_forall_identifiers (pfile, dump_macro, NULL);
119 /* Flush any pending output. */
120 if (print.printed)
121 putc ('\n', print.outf);
124 /* Set up the callbacks as appropriate. */
125 void
126 init_pp_output (FILE *out_stream)
128 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
130 if (!flag_no_output)
132 cb->line_change = cb_line_change;
133 /* Don't emit #pragma or #ident directives if we are processing
134 assembly language; the assembler may choke on them. */
135 if (cpp_get_options (parse_in)->lang != CLK_ASM)
137 cb->ident = cb_ident;
138 cb->def_pragma = cb_def_pragma;
142 if (flag_dump_includes)
143 cb->include = cb_include;
145 if (flag_pch_preprocess)
147 cb->valid_pch = c_common_valid_pch;
148 cb->read_pch = cb_read_pch;
151 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
153 cb->define = cb_define;
154 cb->undef = cb_undef;
157 if (flag_dump_macros == 'U')
159 cb->before_define = dump_queued_macros;
160 cb->used_define = cb_used_define;
161 cb->used_undef = cb_used_undef;
164 cb->has_attribute = c_common_has_attribute;
166 /* Initialize the print structure. */
167 print.src_line = 1;
168 print.printed = 0;
169 print.prev = 0;
170 print.outf = out_stream;
171 print.first_time = 1;
172 print.src_file = "";
173 print.prev_was_system_token = false;
176 /* Writes out the preprocessed file, handling spacing and paste
177 avoidance issues. */
178 static void
179 scan_translation_unit (cpp_reader *pfile)
181 bool avoid_paste = false;
182 bool do_line_adjustments
183 = cpp_get_options (parse_in)->lang != CLK_ASM
184 && !flag_no_line_commands;
185 bool in_pragma = false;
186 bool line_marker_emitted = false;
188 print.source = NULL;
189 for (;;)
191 source_location loc;
192 const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
194 if (token->type == CPP_PADDING)
196 avoid_paste = true;
197 if (print.source == NULL
198 || (!(print.source->flags & PREV_WHITE)
199 && token->val.source == NULL))
200 print.source = token->val.source;
201 continue;
204 if (token->type == CPP_EOF)
205 break;
207 /* Subtle logic to output a space if and only if necessary. */
208 if (avoid_paste)
210 int src_line = LOCATION_LINE (loc);
212 if (print.source == NULL)
213 print.source = token;
215 if (src_line != print.src_line
216 && do_line_adjustments
217 && !in_pragma)
219 line_marker_emitted = do_line_change (pfile, token, loc, false);
220 putc (' ', print.outf);
222 else if (print.source->flags & PREV_WHITE
223 || (print.prev
224 && cpp_avoid_paste (pfile, print.prev, token))
225 || (print.prev == NULL && token->type == CPP_HASH))
226 putc (' ', print.outf);
228 else if (token->flags & PREV_WHITE)
230 int src_line = LOCATION_LINE (loc);
232 if (src_line != print.src_line
233 && do_line_adjustments
234 && !in_pragma)
235 line_marker_emitted = do_line_change (pfile, token, loc, false);
236 putc (' ', print.outf);
239 avoid_paste = false;
240 print.source = NULL;
241 print.prev = token;
242 if (token->type == CPP_PRAGMA)
244 const char *space;
245 const char *name;
247 line_marker_emitted = maybe_print_line (token->src_loc);
248 fputs ("#pragma ", print.outf);
249 c_pp_lookup_pragma (token->val.pragma, &space, &name);
250 if (space)
251 fprintf (print.outf, "%s %s", space, name);
252 else
253 fprintf (print.outf, "%s", name);
254 print.printed = 1;
255 in_pragma = true;
257 else if (token->type == CPP_PRAGMA_EOL)
259 maybe_print_line (token->src_loc);
260 in_pragma = false;
262 else
264 if (cpp_get_options (parse_in)->debug)
265 linemap_dump_location (line_table, token->src_loc,
266 print.outf);
268 if (do_line_adjustments
269 && !in_pragma
270 && !line_marker_emitted
271 && print.prev_was_system_token != !!in_system_header_at(loc)
272 && !is_location_from_builtin_token (loc))
273 /* The system-ness of this token is different from the one
274 of the previous token. Let's emit a line change to
275 mark the new system-ness before we emit the token. */
277 do_line_change (pfile, token, loc, false);
278 print.prev_was_system_token = !!in_system_header_at(loc);
280 cpp_output_token (token, print.outf);
281 line_marker_emitted = false;
284 /* CPP_COMMENT tokens and raw-string literal tokens can
285 have embedded new-line characters. Rather than enumerating
286 all the possible token types just check if token uses
287 val.str union member. */
288 if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
289 account_for_newlines (token->val.str.text, token->val.str.len);
293 static void
294 print_lines_directives_only (int lines, const void *buf, size_t size)
296 print.src_line += lines;
297 fwrite (buf, 1, size, print.outf);
300 /* Writes out the preprocessed file, handling spacing and paste
301 avoidance issues. */
302 static void
303 scan_translation_unit_directives_only (cpp_reader *pfile)
305 struct _cpp_dir_only_callbacks cb;
307 cb.print_lines = print_lines_directives_only;
308 cb.maybe_print_line = (void (*) (source_location)) maybe_print_line;
310 _cpp_preprocess_dir_only (pfile, &cb);
313 /* Adjust print.src_line for newlines embedded in output. */
314 static void
315 account_for_newlines (const unsigned char *str, size_t len)
317 while (len--)
318 if (*str++ == '\n')
319 print.src_line++;
322 /* Writes out a traditionally preprocessed file. */
323 static void
324 scan_translation_unit_trad (cpp_reader *pfile)
326 while (_cpp_read_logical_line_trad (pfile))
328 size_t len = pfile->out.cur - pfile->out.base;
329 maybe_print_line (pfile->out.first_line);
330 fwrite (pfile->out.base, 1, len, print.outf);
331 print.printed = 1;
332 if (!CPP_OPTION (pfile, discard_comments))
333 account_for_newlines (pfile->out.base, len);
337 /* If the token read on logical line LINE needs to be output on a
338 different line to the current one, output the required newlines or
339 a line marker. If a line marker was emitted, return TRUE otherwise
340 return FALSE. */
342 static bool
343 maybe_print_line_1 (source_location src_loc, FILE *stream)
345 bool emitted_line_marker = false;
346 int src_line = LOCATION_LINE (src_loc);
347 const char *src_file = LOCATION_FILE (src_loc);
349 /* End the previous line of text. */
350 if (print.printed)
352 putc ('\n', stream);
353 print.src_line++;
354 print.printed = 0;
357 if (!flag_no_line_commands
358 && src_line >= print.src_line
359 && src_line < print.src_line + 8
360 && strcmp (src_file, print.src_file) == 0)
362 while (src_line > print.src_line)
364 putc ('\n', stream);
365 print.src_line++;
368 else
369 emitted_line_marker = print_line_1 (src_loc, "", stream);
371 return emitted_line_marker;
374 /* If the token read on logical line LINE needs to be output on a
375 different line to the current one, output the required newlines or
376 a line marker. If a line marker was emitted, return TRUE otherwise
377 return FALSE. */
379 static bool
380 maybe_print_line (source_location src_loc)
382 if (cpp_get_options (parse_in)->debug)
383 linemap_dump_location (line_table, src_loc,
384 print.outf);
385 return maybe_print_line_1 (src_loc, print.outf);
388 /* Output a line marker for logical line LINE. Special flags are "1"
389 or "2" indicating entering or leaving a file. If the line marker
390 was effectively emitted, return TRUE otherwise return FALSE. */
392 static bool
393 print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
395 bool emitted_line_marker = false;
397 /* End any previous line of text. */
398 if (print.printed)
399 putc ('\n', stream);
400 print.printed = 0;
402 if (!flag_no_line_commands)
404 const char *file_path = LOCATION_FILE (src_loc);
405 int sysp;
406 size_t to_file_len = strlen (file_path);
407 unsigned char *to_file_quoted =
408 (unsigned char *) alloca (to_file_len * 4 + 1);
409 unsigned char *p;
411 print.src_line = LOCATION_LINE (src_loc);
412 print.src_file = file_path;
414 /* cpp_quote_string does not nul-terminate, so we have to do it
415 ourselves. */
416 p = cpp_quote_string (to_file_quoted,
417 (const unsigned char *) file_path,
418 to_file_len);
419 *p = '\0';
420 fprintf (stream, "# %u \"%s\"%s",
421 print.src_line == 0 ? 1 : print.src_line,
422 to_file_quoted, special_flags);
424 sysp = in_system_header_at (src_loc);
425 if (sysp == 2)
426 fputs (" 3 4", stream);
427 else if (sysp == 1)
428 fputs (" 3", stream);
430 putc ('\n', stream);
431 emitted_line_marker = true;
434 return emitted_line_marker;
437 /* Output a line marker for logical line LINE. Special flags are "1"
438 or "2" indicating entering or leaving a file. Return TRUE if a
439 line marker was effectively emitted, FALSE otherwise. */
441 static bool
442 print_line (source_location src_loc, const char *special_flags)
444 if (cpp_get_options (parse_in)->debug)
445 linemap_dump_location (line_table, src_loc,
446 print.outf);
447 return print_line_1 (src_loc, special_flags, print.outf);
450 /* Helper function for cb_line_change and scan_translation_unit.
451 Return TRUE if a line marker is emitted, FALSE otherwise. */
452 static bool
453 do_line_change (cpp_reader *pfile, const cpp_token *token,
454 source_location src_loc, int parsing_args)
456 bool emitted_line_marker = false;
457 if (define_queue || undef_queue)
458 dump_queued_macros (pfile);
460 if (token->type == CPP_EOF || parsing_args)
461 return false;
463 emitted_line_marker = maybe_print_line (src_loc);
464 print.prev = 0;
465 print.source = 0;
467 /* Supply enough spaces to put this token in its original column,
468 one space per column greater than 2, since scan_translation_unit
469 will provide a space if PREV_WHITE. Don't bother trying to
470 reconstruct tabs; we can't get it right in general, and nothing
471 ought to care. Some things do care; the fault lies with them. */
472 if (!CPP_OPTION (pfile, traditional))
474 int spaces = LOCATION_COLUMN (src_loc) - 2;
475 print.printed = 1;
477 while (-- spaces >= 0)
478 putc (' ', print.outf);
481 return emitted_line_marker;
484 /* Called when a line of output is started. TOKEN is the first token
485 of the line, and at end of file will be CPP_EOF. */
486 static void
487 cb_line_change (cpp_reader *pfile, const cpp_token *token,
488 int parsing_args)
490 do_line_change (pfile, token, token->src_loc, parsing_args);
493 static void
494 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
495 const cpp_string *str)
497 maybe_print_line (line);
498 fprintf (print.outf, "#ident %s\n", str->text);
499 print.src_line++;
502 static void
503 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
505 const struct line_map *map;
507 maybe_print_line (line);
508 fputs ("#define ", print.outf);
510 /* 'D' is whole definition; 'N' is name only. */
511 if (flag_dump_macros == 'D')
512 fputs ((const char *) cpp_macro_definition (pfile, node),
513 print.outf);
514 else
515 fputs ((const char *) NODE_NAME (node), print.outf);
517 putc ('\n', print.outf);
518 linemap_resolve_location (line_table, line,
519 LRK_MACRO_DEFINITION_LOCATION,
520 &map);
521 if (LINEMAP_LINE (map) != 0)
522 print.src_line++;
525 static void
526 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
527 cpp_hashnode *node)
529 maybe_print_line (line);
530 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
531 print.src_line++;
534 static void
535 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
536 cpp_hashnode *node)
538 macro_queue *q;
539 if (node->flags & NODE_BUILTIN)
540 return;
541 q = XNEW (macro_queue);
542 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
543 q->next = define_queue;
544 define_queue = q;
547 static void
548 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
549 source_location line ATTRIBUTE_UNUSED,
550 cpp_hashnode *node)
552 macro_queue *q;
553 q = XNEW (macro_queue);
554 q->macro = xstrdup ((const char *) NODE_NAME (node));
555 q->next = undef_queue;
556 undef_queue = q;
559 static void
560 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
562 macro_queue *q;
564 /* End the previous line of text. */
565 if (print.printed)
567 putc ('\n', print.outf);
568 print.src_line++;
569 print.printed = 0;
572 for (q = define_queue; q;)
574 macro_queue *oq;
575 fputs ("#define ", print.outf);
576 fputs (q->macro, print.outf);
577 putc ('\n', print.outf);
578 print.src_line++;
579 oq = q;
580 q = q->next;
581 free (oq->macro);
582 free (oq);
584 define_queue = NULL;
585 for (q = undef_queue; q;)
587 macro_queue *oq;
588 fprintf (print.outf, "#undef %s\n", q->macro);
589 print.src_line++;
590 oq = q;
591 q = q->next;
592 free (oq->macro);
593 free (oq);
595 undef_queue = NULL;
598 static void
599 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
600 const unsigned char *dir, const char *header, int angle_brackets,
601 const cpp_token **comments)
603 maybe_print_line (line);
604 if (angle_brackets)
605 fprintf (print.outf, "#%s <%s>", dir, header);
606 else
607 fprintf (print.outf, "#%s \"%s\"", dir, header);
609 if (comments != NULL)
611 while (*comments != NULL)
613 if ((*comments)->flags & PREV_WHITE)
614 putc (' ', print.outf);
615 cpp_output_token (*comments, print.outf);
616 ++comments;
620 putc ('\n', print.outf);
621 print.src_line++;
624 /* Callback called when -fworking-director and -E to emit working
625 directory in cpp output file. */
627 void
628 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
630 size_t to_file_len = strlen (dir);
631 unsigned char *to_file_quoted =
632 (unsigned char *) alloca (to_file_len * 4 + 1);
633 unsigned char *p;
635 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
636 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
637 *p = '\0';
638 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
641 /* The file name, line number or system header flags have changed, as
642 described in MAP. */
644 void
645 pp_file_change (const struct line_map *map)
647 const char *flags = "";
649 if (flag_no_line_commands)
650 return;
652 if (map != NULL)
654 input_location = map->start_location;
655 if (print.first_time)
657 /* Avoid printing foo.i when the main file is foo.c. */
658 if (!cpp_get_options (parse_in)->preprocessed)
659 print_line (map->start_location, flags);
660 print.first_time = 0;
662 else
664 /* Bring current file to correct line when entering a new file. */
665 if (map->reason == LC_ENTER)
667 const struct line_map *from = INCLUDED_FROM (line_table, map);
668 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
670 if (map->reason == LC_ENTER)
671 flags = " 1";
672 else if (map->reason == LC_LEAVE)
673 flags = " 2";
674 print_line (map->start_location, flags);
679 /* Copy a #pragma directive to the preprocessed output. */
680 static void
681 cb_def_pragma (cpp_reader *pfile, source_location line)
683 maybe_print_line (line);
684 fputs ("#pragma ", print.outf);
685 cpp_output_line (pfile, print.outf);
686 print.src_line++;
689 /* Dump out the hash table. */
690 static int
691 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
693 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
695 fputs ("#define ", print.outf);
696 fputs ((const char *) cpp_macro_definition (pfile, node),
697 print.outf);
698 putc ('\n', print.outf);
699 print.src_line++;
702 return 1;
705 /* Load in the PCH file NAME, open on FD. It was originally searched for
706 by ORIG_NAME. Also, print out a #include command so that the PCH
707 file can be loaded when the preprocessed output is compiled. */
709 static void
710 cb_read_pch (cpp_reader *pfile, const char *name,
711 int fd, const char *orig_name ATTRIBUTE_UNUSED)
713 c_common_read_pch (pfile, name, fd, orig_name);
715 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
716 print.src_line++;