PR preprocessor/60723 - missing system-ness marks for macro tokens
[official-gcc.git] / gcc / c-family / c-ppoutput.c
blob618e1c94b6c42bb327788ce5c5cee482da369338
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 (!line_marker_emitted
257 && print.prev_was_system_token != !!in_system_header_at(loc))
258 /* The system-ness of this token is different from the one
259 of the previous token. Let's emit a line change to
260 mark the new system-ness before we emit the token. */
261 line_marker_emitted = do_line_change (pfile, token, loc, false);
262 cpp_output_token (token, print.outf);
263 line_marker_emitted = false;
266 print.prev_was_system_token = !!in_system_header_at(loc);
267 /* CPP_COMMENT tokens and raw-string literal tokens can
268 have embedded new-line characters. Rather than enumerating
269 all the possible token types just check if token uses
270 val.str union member. */
271 if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
272 account_for_newlines (token->val.str.text, token->val.str.len);
276 static void
277 print_lines_directives_only (int lines, const void *buf, size_t size)
279 print.src_line += lines;
280 fwrite (buf, 1, size, print.outf);
283 /* Writes out the preprocessed file, handling spacing and paste
284 avoidance issues. */
285 static void
286 scan_translation_unit_directives_only (cpp_reader *pfile)
288 struct _cpp_dir_only_callbacks cb;
290 cb.print_lines = print_lines_directives_only;
291 cb.maybe_print_line = (void (*) (source_location)) maybe_print_line;
293 _cpp_preprocess_dir_only (pfile, &cb);
296 /* Adjust print.src_line for newlines embedded in output. */
297 static void
298 account_for_newlines (const unsigned char *str, size_t len)
300 while (len--)
301 if (*str++ == '\n')
302 print.src_line++;
305 /* Writes out a traditionally preprocessed file. */
306 static void
307 scan_translation_unit_trad (cpp_reader *pfile)
309 while (_cpp_read_logical_line_trad (pfile))
311 size_t len = pfile->out.cur - pfile->out.base;
312 maybe_print_line (pfile->out.first_line);
313 fwrite (pfile->out.base, 1, len, print.outf);
314 print.printed = 1;
315 if (!CPP_OPTION (pfile, discard_comments))
316 account_for_newlines (pfile->out.base, len);
320 /* If the token read on logical line LINE needs to be output on a
321 different line to the current one, output the required newlines or
322 a line marker. If a line marker was emitted, return TRUE otherwise
323 return FALSE. */
325 static bool
326 maybe_print_line_1 (source_location src_loc, FILE *stream)
328 bool emitted_line_marker = false;
329 int src_line = LOCATION_LINE (src_loc);
330 const char *src_file = LOCATION_FILE (src_loc);
332 /* End the previous line of text. */
333 if (print.printed)
335 putc ('\n', stream);
336 print.src_line++;
337 print.printed = 0;
340 if (!flag_no_line_commands
341 && src_line >= print.src_line
342 && src_line < print.src_line + 8
343 && strcmp (src_file, print.src_file) == 0)
345 while (src_line > print.src_line)
347 putc ('\n', stream);
348 print.src_line++;
351 else
352 emitted_line_marker = print_line_1 (src_loc, "", stream);
354 return emitted_line_marker;
357 /* If the token read on logical line LINE needs to be output on a
358 different line to the current one, output the required newlines or
359 a line marker. If a line marker was emitted, return TRUE otherwise
360 return FALSE. */
362 static bool
363 maybe_print_line (source_location src_loc)
365 if (cpp_get_options (parse_in)->debug)
366 linemap_dump_location (line_table, src_loc,
367 print.outf);
368 return maybe_print_line_1 (src_loc, print.outf);
371 /* Output a line marker for logical line LINE. Special flags are "1"
372 or "2" indicating entering or leaving a file. If the line marker
373 was effectively emitted, return TRUE otherwise return FALSE. */
375 static bool
376 print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
378 bool emitted_line_marker = false;
380 /* End any previous line of text. */
381 if (print.printed)
382 putc ('\n', stream);
383 print.printed = 0;
385 if (!flag_no_line_commands)
387 const char *file_path = LOCATION_FILE (src_loc);
388 int sysp;
389 size_t to_file_len = strlen (file_path);
390 unsigned char *to_file_quoted =
391 (unsigned char *) alloca (to_file_len * 4 + 1);
392 unsigned char *p;
394 print.src_line = LOCATION_LINE (src_loc);
395 print.src_file = file_path;
397 /* cpp_quote_string does not nul-terminate, so we have to do it
398 ourselves. */
399 p = cpp_quote_string (to_file_quoted,
400 (const unsigned char *) file_path,
401 to_file_len);
402 *p = '\0';
403 fprintf (stream, "# %u \"%s\"%s",
404 print.src_line == 0 ? 1 : print.src_line,
405 to_file_quoted, special_flags);
407 sysp = in_system_header_at (src_loc);
408 if (sysp == 2)
409 fputs (" 3 4", stream);
410 else if (sysp == 1)
411 fputs (" 3", stream);
413 putc ('\n', stream);
414 emitted_line_marker = true;
417 return emitted_line_marker;
420 /* Output a line marker for logical line LINE. Special flags are "1"
421 or "2" indicating entering or leaving a file. Return TRUE if a
422 line marker was effectively emitted, FALSE otherwise. */
424 static bool
425 print_line (source_location src_loc, const char *special_flags)
427 if (cpp_get_options (parse_in)->debug)
428 linemap_dump_location (line_table, src_loc,
429 print.outf);
430 return print_line_1 (src_loc, special_flags, print.outf);
433 /* Helper function for cb_line_change and scan_translation_unit.
434 Return TRUE if a line marker is emitted, FALSE otherwise. */
435 static bool
436 do_line_change (cpp_reader *pfile, const cpp_token *token,
437 source_location src_loc, int parsing_args)
439 bool emitted_line_marker = false;
440 if (define_queue || undef_queue)
441 dump_queued_macros (pfile);
443 if (token->type == CPP_EOF || parsing_args)
444 return false;
446 emitted_line_marker = maybe_print_line (src_loc);
447 print.prev = 0;
448 print.source = 0;
450 /* Supply enough spaces to put this token in its original column,
451 one space per column greater than 2, since scan_translation_unit
452 will provide a space if PREV_WHITE. Don't bother trying to
453 reconstruct tabs; we can't get it right in general, and nothing
454 ought to care. Some things do care; the fault lies with them. */
455 if (!CPP_OPTION (pfile, traditional))
457 int spaces = LOCATION_COLUMN (src_loc) - 2;
458 print.printed = 1;
460 while (-- spaces >= 0)
461 putc (' ', print.outf);
464 return emitted_line_marker;
467 /* Called when a line of output is started. TOKEN is the first token
468 of the line, and at end of file will be CPP_EOF. */
469 static void
470 cb_line_change (cpp_reader *pfile, const cpp_token *token,
471 int parsing_args)
473 do_line_change (pfile, token, token->src_loc, parsing_args);
476 static void
477 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
478 const cpp_string *str)
480 maybe_print_line (line);
481 fprintf (print.outf, "#ident %s\n", str->text);
482 print.src_line++;
485 static void
486 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
488 const struct line_map *map;
490 maybe_print_line (line);
491 fputs ("#define ", print.outf);
493 /* 'D' is whole definition; 'N' is name only. */
494 if (flag_dump_macros == 'D')
495 fputs ((const char *) cpp_macro_definition (pfile, node),
496 print.outf);
497 else
498 fputs ((const char *) NODE_NAME (node), print.outf);
500 putc ('\n', print.outf);
501 linemap_resolve_location (line_table, line,
502 LRK_MACRO_DEFINITION_LOCATION,
503 &map);
504 if (LINEMAP_LINE (map) != 0)
505 print.src_line++;
508 static void
509 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
510 cpp_hashnode *node)
512 maybe_print_line (line);
513 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
514 print.src_line++;
517 static void
518 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
519 cpp_hashnode *node)
521 macro_queue *q;
522 if (node->flags & NODE_BUILTIN)
523 return;
524 q = XNEW (macro_queue);
525 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
526 q->next = define_queue;
527 define_queue = q;
530 static void
531 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
532 source_location line ATTRIBUTE_UNUSED,
533 cpp_hashnode *node)
535 macro_queue *q;
536 q = XNEW (macro_queue);
537 q->macro = xstrdup ((const char *) NODE_NAME (node));
538 q->next = undef_queue;
539 undef_queue = q;
542 static void
543 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
545 macro_queue *q;
547 /* End the previous line of text. */
548 if (print.printed)
550 putc ('\n', print.outf);
551 print.src_line++;
552 print.printed = 0;
555 for (q = define_queue; q;)
557 macro_queue *oq;
558 fputs ("#define ", print.outf);
559 fputs (q->macro, print.outf);
560 putc ('\n', print.outf);
561 print.src_line++;
562 oq = q;
563 q = q->next;
564 free (oq->macro);
565 free (oq);
567 define_queue = NULL;
568 for (q = undef_queue; q;)
570 macro_queue *oq;
571 fprintf (print.outf, "#undef %s\n", q->macro);
572 print.src_line++;
573 oq = q;
574 q = q->next;
575 free (oq->macro);
576 free (oq);
578 undef_queue = NULL;
581 static void
582 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
583 const unsigned char *dir, const char *header, int angle_brackets,
584 const cpp_token **comments)
586 maybe_print_line (line);
587 if (angle_brackets)
588 fprintf (print.outf, "#%s <%s>", dir, header);
589 else
590 fprintf (print.outf, "#%s \"%s\"", dir, header);
592 if (comments != NULL)
594 while (*comments != NULL)
596 if ((*comments)->flags & PREV_WHITE)
597 putc (' ', print.outf);
598 cpp_output_token (*comments, print.outf);
599 ++comments;
603 putc ('\n', print.outf);
604 print.src_line++;
607 /* Callback called when -fworking-director and -E to emit working
608 directory in cpp output file. */
610 void
611 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
613 size_t to_file_len = strlen (dir);
614 unsigned char *to_file_quoted =
615 (unsigned char *) alloca (to_file_len * 4 + 1);
616 unsigned char *p;
618 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
619 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
620 *p = '\0';
621 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
624 /* The file name, line number or system header flags have changed, as
625 described in MAP. */
627 void
628 pp_file_change (const struct line_map *map)
630 const char *flags = "";
632 if (flag_no_line_commands)
633 return;
635 if (map != NULL)
637 input_location = map->start_location;
638 if (print.first_time)
640 /* Avoid printing foo.i when the main file is foo.c. */
641 if (!cpp_get_options (parse_in)->preprocessed)
642 print_line (map->start_location, flags);
643 print.first_time = 0;
645 else
647 /* Bring current file to correct line when entering a new file. */
648 if (map->reason == LC_ENTER)
650 const struct line_map *from = INCLUDED_FROM (line_table, map);
651 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
653 if (map->reason == LC_ENTER)
654 flags = " 1";
655 else if (map->reason == LC_LEAVE)
656 flags = " 2";
657 print_line (map->start_location, flags);
662 /* Copy a #pragma directive to the preprocessed output. */
663 static void
664 cb_def_pragma (cpp_reader *pfile, source_location line)
666 maybe_print_line (line);
667 fputs ("#pragma ", print.outf);
668 cpp_output_line (pfile, print.outf);
669 print.src_line++;
672 /* Dump out the hash table. */
673 static int
674 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
676 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
678 fputs ("#define ", print.outf);
679 fputs ((const char *) cpp_macro_definition (pfile, node),
680 print.outf);
681 putc ('\n', print.outf);
682 print.src_line++;
685 return 1;
688 /* Load in the PCH file NAME, open on FD. It was originally searched for
689 by ORIG_NAME. Also, print out a #include command so that the PCH
690 file can be loaded when the preprocessed output is compiled. */
692 static void
693 cb_read_pch (cpp_reader *pfile, const char *name,
694 int fd, const char *orig_name ATTRIBUTE_UNUSED)
696 c_common_read_pch (pfile, name, fd, orig_name);
698 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
699 print.src_line++;