PR preprocessor/60723 - missing system-ness marks for macro tokens
[official-gcc.git] / gcc / c-family / c-ppoutput.c
blob0292d1620ed0e9e4f92e495e4c8d926194fa3c62
1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995-2014 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 "tree.h"
25 #include "c-common.h" /* For flags. */
26 #include "c-pragma.h" /* For parse_in. */
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 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;
43 /* Defined and undefined macros being queued for output with -dU at
44 the next newline. */
45 typedef 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. */
51 } macro_queue;
52 static macro_queue *define_queue, *undef_queue;
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 *);
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);
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);
84 /* Preprocess and output. */
85 void
86 preprocess_file (cpp_reader *pfile)
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)
92 /* Scan -included buffers, then the main file. */
93 while (pfile->buffer->prev)
94 cpp_scan_nooutput (pfile);
95 cpp_scan_nooutput (pfile);
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);
105 /* -dM command line option. Should this be elsewhere? */
106 if (flag_dump_macros == 'M')
107 cpp_forall_identifiers (pfile, dump_macro, NULL);
109 /* Flush any pending output. */
110 if (print.printed)
111 putc ('\n', print.outf);
114 /* Set up the callbacks as appropriate. */
115 void
116 init_pp_output (FILE *out_stream)
118 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
120 if (!flag_no_output)
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)
127 cb->ident = cb_ident;
128 cb->def_pragma = cb_def_pragma;
132 if (flag_dump_includes)
133 cb->include = cb_include;
135 if (flag_pch_preprocess)
137 cb->valid_pch = c_common_valid_pch;
138 cb->read_pch = cb_read_pch;
141 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
143 cb->define = cb_define;
144 cb->undef = cb_undef;
147 if (flag_dump_macros == 'U')
149 cb->before_define = dump_queued_macros;
150 cb->used_define = cb_used_define;
151 cb->used_undef = cb_used_undef;
154 /* Initialize the print structure. */
155 print.src_line = 1;
156 print.printed = 0;
157 print.prev = 0;
158 print.outf = out_stream;
159 print.first_time = 1;
160 print.src_file = "";
161 print.prev_was_system_token = false;
164 /* Writes out the preprocessed file, handling spacing and paste
165 avoidance issues. */
166 static void
167 scan_translation_unit (cpp_reader *pfile)
169 bool avoid_paste = false;
170 bool do_line_adjustments
171 = cpp_get_options (parse_in)->lang != CLK_ASM
172 && !flag_no_line_commands;
173 bool in_pragma = false;
174 bool line_marker_emitted = false;
176 print.source = NULL;
177 for (;;)
179 source_location loc;
180 const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
182 if (token->type == CPP_PADDING)
184 avoid_paste = true;
185 if (print.source == NULL
186 || (!(print.source->flags & PREV_WHITE)
187 && token->val.source == NULL))
188 print.source = token->val.source;
189 continue;
192 if (token->type == CPP_EOF)
193 break;
195 /* Subtle logic to output a space if and only if necessary. */
196 if (avoid_paste)
198 int src_line = LOCATION_LINE (loc);
200 if (print.source == NULL)
201 print.source = token;
203 if (src_line != print.src_line
204 && do_line_adjustments
205 && !in_pragma)
207 line_marker_emitted = do_line_change (pfile, token, loc, false);
208 putc (' ', print.outf);
210 else if (print.source->flags & PREV_WHITE
211 || (print.prev
212 && cpp_avoid_paste (pfile, print.prev, token))
213 || (print.prev == NULL && token->type == CPP_HASH))
214 putc (' ', print.outf);
216 else if (token->flags & PREV_WHITE)
218 int src_line = LOCATION_LINE (loc);
220 if (src_line != print.src_line
221 && do_line_adjustments
222 && !in_pragma)
223 line_marker_emitted = do_line_change (pfile, token, loc, false);
224 putc (' ', print.outf);
227 avoid_paste = false;
228 print.source = NULL;
229 print.prev = token;
230 if (token->type == CPP_PRAGMA)
232 const char *space;
233 const char *name;
235 line_marker_emitted = maybe_print_line (token->src_loc);
236 fputs ("#pragma ", print.outf);
237 c_pp_lookup_pragma (token->val.pragma, &space, &name);
238 if (space)
239 fprintf (print.outf, "%s %s", space, name);
240 else
241 fprintf (print.outf, "%s", name);
242 print.printed = 1;
243 in_pragma = true;
245 else if (token->type == CPP_PRAGMA_EOL)
247 maybe_print_line (token->src_loc);
248 in_pragma = false;
250 else
252 if (cpp_get_options (parse_in)->debug)
253 linemap_dump_location (line_table, token->src_loc,
254 print.outf);
256 if (do_line_adjustments
257 && !in_pragma
258 && !line_marker_emitted
259 && print.prev_was_system_token != !!in_system_header_at(loc)
260 && !is_location_from_builtin_token (loc))
261 /* The system-ness of this token is different from the one
262 of the previous token. Let's emit a line change to
263 mark the new system-ness before we emit the token. */
265 do_line_change (pfile, token, loc, false);
266 print.prev_was_system_token = !!in_system_header_at(loc);
268 cpp_output_token (token, print.outf);
269 line_marker_emitted = false;
272 /* CPP_COMMENT tokens and raw-string literal tokens can
273 have embedded new-line characters. Rather than enumerating
274 all the possible token types just check if token uses
275 val.str union member. */
276 if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
277 account_for_newlines (token->val.str.text, token->val.str.len);
281 static void
282 print_lines_directives_only (int lines, const void *buf, size_t size)
284 print.src_line += lines;
285 fwrite (buf, 1, size, print.outf);
288 /* Writes out the preprocessed file, handling spacing and paste
289 avoidance issues. */
290 static void
291 scan_translation_unit_directives_only (cpp_reader *pfile)
293 struct _cpp_dir_only_callbacks cb;
295 cb.print_lines = print_lines_directives_only;
296 cb.maybe_print_line = (void (*) (source_location)) maybe_print_line;
298 _cpp_preprocess_dir_only (pfile, &cb);
301 /* Adjust print.src_line for newlines embedded in output. */
302 static void
303 account_for_newlines (const unsigned char *str, size_t len)
305 while (len--)
306 if (*str++ == '\n')
307 print.src_line++;
310 /* Writes out a traditionally preprocessed file. */
311 static void
312 scan_translation_unit_trad (cpp_reader *pfile)
314 while (_cpp_read_logical_line_trad (pfile))
316 size_t len = pfile->out.cur - pfile->out.base;
317 maybe_print_line (pfile->out.first_line);
318 fwrite (pfile->out.base, 1, len, print.outf);
319 print.printed = 1;
320 if (!CPP_OPTION (pfile, discard_comments))
321 account_for_newlines (pfile->out.base, len);
325 /* If the token read on logical line LINE needs to be output on a
326 different line to the current one, output the required newlines or
327 a line marker. If a line marker was emitted, return TRUE otherwise
328 return FALSE. */
330 static bool
331 maybe_print_line_1 (source_location src_loc, FILE *stream)
333 bool emitted_line_marker = false;
334 int src_line = LOCATION_LINE (src_loc);
335 const char *src_file = LOCATION_FILE (src_loc);
337 /* End the previous line of text. */
338 if (print.printed)
340 putc ('\n', stream);
341 print.src_line++;
342 print.printed = 0;
345 if (!flag_no_line_commands
346 && src_line >= print.src_line
347 && src_line < print.src_line + 8
348 && strcmp (src_file, print.src_file) == 0)
350 while (src_line > print.src_line)
352 putc ('\n', stream);
353 print.src_line++;
356 else
357 emitted_line_marker = print_line_1 (src_loc, "", stream);
359 return emitted_line_marker;
362 /* If the token read on logical line LINE needs to be output on a
363 different line to the current one, output the required newlines or
364 a line marker. If a line marker was emitted, return TRUE otherwise
365 return FALSE. */
367 static bool
368 maybe_print_line (source_location src_loc)
370 if (cpp_get_options (parse_in)->debug)
371 linemap_dump_location (line_table, src_loc,
372 print.outf);
373 return maybe_print_line_1 (src_loc, print.outf);
376 /* Output a line marker for logical line LINE. Special flags are "1"
377 or "2" indicating entering or leaving a file. If the line marker
378 was effectively emitted, return TRUE otherwise return FALSE. */
380 static bool
381 print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
383 bool emitted_line_marker = false;
385 /* End any previous line of text. */
386 if (print.printed)
387 putc ('\n', stream);
388 print.printed = 0;
390 if (!flag_no_line_commands)
392 const char *file_path = LOCATION_FILE (src_loc);
393 int sysp;
394 size_t to_file_len = strlen (file_path);
395 unsigned char *to_file_quoted =
396 (unsigned char *) alloca (to_file_len * 4 + 1);
397 unsigned char *p;
399 print.src_line = LOCATION_LINE (src_loc);
400 print.src_file = file_path;
402 /* cpp_quote_string does not nul-terminate, so we have to do it
403 ourselves. */
404 p = cpp_quote_string (to_file_quoted,
405 (const unsigned char *) file_path,
406 to_file_len);
407 *p = '\0';
408 fprintf (stream, "# %u \"%s\"%s",
409 print.src_line == 0 ? 1 : print.src_line,
410 to_file_quoted, special_flags);
412 sysp = in_system_header_at (src_loc);
413 if (sysp == 2)
414 fputs (" 3 4", stream);
415 else if (sysp == 1)
416 fputs (" 3", stream);
418 putc ('\n', stream);
419 emitted_line_marker = true;
422 return emitted_line_marker;
425 /* Output a line marker for logical line LINE. Special flags are "1"
426 or "2" indicating entering or leaving a file. Return TRUE if a
427 line marker was effectively emitted, FALSE otherwise. */
429 static bool
430 print_line (source_location src_loc, const char *special_flags)
432 if (cpp_get_options (parse_in)->debug)
433 linemap_dump_location (line_table, src_loc,
434 print.outf);
435 return print_line_1 (src_loc, special_flags, print.outf);
438 /* Helper function for cb_line_change and scan_translation_unit.
439 Return TRUE if a line marker is emitted, FALSE otherwise. */
440 static bool
441 do_line_change (cpp_reader *pfile, const cpp_token *token,
442 source_location src_loc, int parsing_args)
444 bool emitted_line_marker = false;
445 if (define_queue || undef_queue)
446 dump_queued_macros (pfile);
448 if (token->type == CPP_EOF || parsing_args)
449 return false;
451 emitted_line_marker = maybe_print_line (src_loc);
452 print.prev = 0;
453 print.source = 0;
455 /* Supply enough spaces to put this token in its original column,
456 one space per column greater than 2, since scan_translation_unit
457 will provide a space if PREV_WHITE. Don't bother trying to
458 reconstruct tabs; we can't get it right in general, and nothing
459 ought to care. Some things do care; the fault lies with them. */
460 if (!CPP_OPTION (pfile, traditional))
462 int spaces = LOCATION_COLUMN (src_loc) - 2;
463 print.printed = 1;
465 while (-- spaces >= 0)
466 putc (' ', print.outf);
469 return emitted_line_marker;
472 /* Called when a line of output is started. TOKEN is the first token
473 of the line, and at end of file will be CPP_EOF. */
474 static void
475 cb_line_change (cpp_reader *pfile, const cpp_token *token,
476 int parsing_args)
478 do_line_change (pfile, token, token->src_loc, parsing_args);
481 static void
482 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
483 const cpp_string *str)
485 maybe_print_line (line);
486 fprintf (print.outf, "#ident %s\n", str->text);
487 print.src_line++;
490 static void
491 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
493 const struct line_map *map;
495 maybe_print_line (line);
496 fputs ("#define ", print.outf);
498 /* 'D' is whole definition; 'N' is name only. */
499 if (flag_dump_macros == 'D')
500 fputs ((const char *) cpp_macro_definition (pfile, node),
501 print.outf);
502 else
503 fputs ((const char *) NODE_NAME (node), print.outf);
505 putc ('\n', print.outf);
506 linemap_resolve_location (line_table, line,
507 LRK_MACRO_DEFINITION_LOCATION,
508 &map);
509 if (LINEMAP_LINE (map) != 0)
510 print.src_line++;
513 static void
514 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
515 cpp_hashnode *node)
517 maybe_print_line (line);
518 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
519 print.src_line++;
522 static void
523 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
524 cpp_hashnode *node)
526 macro_queue *q;
527 if (node->flags & NODE_BUILTIN)
528 return;
529 q = XNEW (macro_queue);
530 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
531 q->next = define_queue;
532 define_queue = q;
535 static void
536 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
537 source_location line ATTRIBUTE_UNUSED,
538 cpp_hashnode *node)
540 macro_queue *q;
541 q = XNEW (macro_queue);
542 q->macro = xstrdup ((const char *) NODE_NAME (node));
543 q->next = undef_queue;
544 undef_queue = q;
547 static void
548 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
550 macro_queue *q;
552 /* End the previous line of text. */
553 if (print.printed)
555 putc ('\n', print.outf);
556 print.src_line++;
557 print.printed = 0;
560 for (q = define_queue; q;)
562 macro_queue *oq;
563 fputs ("#define ", print.outf);
564 fputs (q->macro, print.outf);
565 putc ('\n', print.outf);
566 print.src_line++;
567 oq = q;
568 q = q->next;
569 free (oq->macro);
570 free (oq);
572 define_queue = NULL;
573 for (q = undef_queue; q;)
575 macro_queue *oq;
576 fprintf (print.outf, "#undef %s\n", q->macro);
577 print.src_line++;
578 oq = q;
579 q = q->next;
580 free (oq->macro);
581 free (oq);
583 undef_queue = NULL;
586 static void
587 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
588 const unsigned char *dir, const char *header, int angle_brackets,
589 const cpp_token **comments)
591 maybe_print_line (line);
592 if (angle_brackets)
593 fprintf (print.outf, "#%s <%s>", dir, header);
594 else
595 fprintf (print.outf, "#%s \"%s\"", dir, header);
597 if (comments != NULL)
599 while (*comments != NULL)
601 if ((*comments)->flags & PREV_WHITE)
602 putc (' ', print.outf);
603 cpp_output_token (*comments, print.outf);
604 ++comments;
608 putc ('\n', print.outf);
609 print.src_line++;
612 /* Callback called when -fworking-director and -E to emit working
613 directory in cpp output file. */
615 void
616 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
618 size_t to_file_len = strlen (dir);
619 unsigned char *to_file_quoted =
620 (unsigned char *) alloca (to_file_len * 4 + 1);
621 unsigned char *p;
623 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
624 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
625 *p = '\0';
626 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
629 /* The file name, line number or system header flags have changed, as
630 described in MAP. */
632 void
633 pp_file_change (const struct line_map *map)
635 const char *flags = "";
637 if (flag_no_line_commands)
638 return;
640 if (map != NULL)
642 input_location = map->start_location;
643 if (print.first_time)
645 /* Avoid printing foo.i when the main file is foo.c. */
646 if (!cpp_get_options (parse_in)->preprocessed)
647 print_line (map->start_location, flags);
648 print.first_time = 0;
650 else
652 /* Bring current file to correct line when entering a new file. */
653 if (map->reason == LC_ENTER)
655 const struct line_map *from = INCLUDED_FROM (line_table, map);
656 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
658 if (map->reason == LC_ENTER)
659 flags = " 1";
660 else if (map->reason == LC_LEAVE)
661 flags = " 2";
662 print_line (map->start_location, flags);
667 /* Copy a #pragma directive to the preprocessed output. */
668 static void
669 cb_def_pragma (cpp_reader *pfile, source_location line)
671 maybe_print_line (line);
672 fputs ("#pragma ", print.outf);
673 cpp_output_line (pfile, print.outf);
674 print.src_line++;
677 /* Dump out the hash table. */
678 static int
679 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
681 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
683 fputs ("#define ", print.outf);
684 fputs ((const char *) cpp_macro_definition (pfile, node),
685 print.outf);
686 putc ('\n', print.outf);
687 print.src_line++;
690 return 1;
693 /* Load in the PCH file NAME, open on FD. It was originally searched for
694 by ORIG_NAME. Also, print out a #include command so that the PCH
695 file can be loaded when the preprocessed output is compiled. */
697 static void
698 cb_read_pch (cpp_reader *pfile, const char *name,
699 int fd, const char *orig_name ATTRIBUTE_UNUSED)
701 c_common_read_pch (pfile, name, fd, orig_name);
703 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
704 print.src_line++;