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
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/>. */
21 #include "coretypes.h"
23 #include "../libcpp/internal.h"
27 #include "double-int.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
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 bool printed; /* True if something output at line. */
47 bool first_time; /* pp_file_change hasn't been called yet. */
48 bool prev_was_system_token; /* True if the previous token was a
50 const char *src_file; /* Current source file. */
53 /* Defined and undefined macros being queued for output with -dU at
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
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
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. */
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);
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. */
121 putc ('\n', print.outf);
124 /* Set up the callbacks as appropriate. */
126 init_pp_output (FILE *out_stream)
128 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
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. */
168 print.printed = false;
170 print.outf = out_stream;
171 print.first_time = 1;
173 print.prev_was_system_token = false;
176 /* Writes out the preprocessed file, handling spacing and paste
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;
192 const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
194 if (token->type == CPP_PADDING)
197 if (print.source == NULL
198 || (!(print.source->flags & PREV_WHITE)
199 && token->val.source == NULL))
200 print.source = token->val.source;
204 if (token->type == CPP_EOF)
207 /* Subtle logic to output a space if and only if necessary. */
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
219 line_marker_emitted = do_line_change (pfile, token, loc, false);
220 putc (' ', print.outf);
221 print.printed = true;
223 else if (print.source->flags & PREV_WHITE
225 && cpp_avoid_paste (pfile, print.prev, token))
226 || (print.prev == NULL && token->type == CPP_HASH))
228 putc (' ', print.outf);
229 print.printed = true;
232 else if (token->flags & PREV_WHITE)
234 int src_line = LOCATION_LINE (loc);
236 if (src_line != print.src_line
237 && do_line_adjustments
239 line_marker_emitted = do_line_change (pfile, token, loc, false);
240 putc (' ', print.outf);
241 print.printed = true;
247 if (token->type == CPP_PRAGMA)
252 line_marker_emitted = maybe_print_line (token->src_loc);
253 fputs ("#pragma ", print.outf);
254 c_pp_lookup_pragma (token->val.pragma, &space, &name);
256 fprintf (print.outf, "%s %s", space, name);
258 fprintf (print.outf, "%s", name);
259 print.printed = true;
262 else if (token->type == CPP_PRAGMA_EOL)
264 maybe_print_line (token->src_loc);
269 if (cpp_get_options (parse_in)->debug)
270 linemap_dump_location (line_table, token->src_loc, print.outf);
272 if (do_line_adjustments
274 && !line_marker_emitted
275 && print.prev_was_system_token != !!in_system_header_at (loc)
276 && !is_location_from_builtin_token (loc))
277 /* The system-ness of this token is different from the one
278 of the previous token. Let's emit a line change to
279 mark the new system-ness before we emit the token. */
281 do_line_change (pfile, token, loc, false);
282 print.prev_was_system_token = !!in_system_header_at (loc);
284 cpp_output_token (token, print.outf);
285 line_marker_emitted = false;
286 print.printed = true;
289 /* CPP_COMMENT tokens and raw-string literal tokens can
290 have embedded new-line characters. Rather than enumerating
291 all the possible token types just check if token uses
292 val.str union member. */
293 if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
294 account_for_newlines (token->val.str.text, token->val.str.len);
299 print_lines_directives_only (int lines, const void *buf, size_t size)
301 print.src_line += lines;
302 fwrite (buf, 1, size, print.outf);
305 /* Writes out the preprocessed file, handling spacing and paste
308 scan_translation_unit_directives_only (cpp_reader *pfile)
310 struct _cpp_dir_only_callbacks cb;
312 cb.print_lines = print_lines_directives_only;
313 cb.maybe_print_line = (void (*) (source_location)) maybe_print_line;
315 _cpp_preprocess_dir_only (pfile, &cb);
318 /* Adjust print.src_line for newlines embedded in output. */
320 account_for_newlines (const unsigned char *str, size_t len)
327 /* Writes out a traditionally preprocessed file. */
329 scan_translation_unit_trad (cpp_reader *pfile)
331 while (_cpp_read_logical_line_trad (pfile))
333 size_t len = pfile->out.cur - pfile->out.base;
334 maybe_print_line (pfile->out.first_line);
335 fwrite (pfile->out.base, 1, len, print.outf);
336 print.printed = true;
337 if (!CPP_OPTION (pfile, discard_comments))
338 account_for_newlines (pfile->out.base, len);
342 /* If the token read on logical line LINE needs to be output on a
343 different line to the current one, output the required newlines or
344 a line marker. If a line marker was emitted, return TRUE otherwise
348 maybe_print_line_1 (source_location src_loc, FILE *stream)
350 bool emitted_line_marker = false;
351 int src_line = LOCATION_LINE (src_loc);
352 const char *src_file = LOCATION_FILE (src_loc);
354 /* End the previous line of text. */
359 print.printed = false;
362 if (!flag_no_line_commands
363 && src_line >= print.src_line
364 && src_line < print.src_line + 8
365 && strcmp (src_file, print.src_file) == 0)
367 while (src_line > print.src_line)
374 emitted_line_marker = print_line_1 (src_loc, "", stream);
376 return emitted_line_marker;
379 /* If the token read on logical line LINE needs to be output on a
380 different line to the current one, output the required newlines or
381 a line marker. If a line marker was emitted, return TRUE otherwise
385 maybe_print_line (source_location src_loc)
387 if (cpp_get_options (parse_in)->debug)
388 linemap_dump_location (line_table, src_loc,
390 return maybe_print_line_1 (src_loc, print.outf);
393 /* Output a line marker for logical line LINE. Special flags are "1"
394 or "2" indicating entering or leaving a file. If the line marker
395 was effectively emitted, return TRUE otherwise return FALSE. */
398 print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
400 bool emitted_line_marker = false;
402 /* End any previous line of text. */
405 print.printed = false;
407 if (!flag_no_line_commands)
409 const char *file_path = LOCATION_FILE (src_loc);
411 size_t to_file_len = strlen (file_path);
412 unsigned char *to_file_quoted =
413 (unsigned char *) alloca (to_file_len * 4 + 1);
416 print.src_line = LOCATION_LINE (src_loc);
417 print.src_file = file_path;
419 /* cpp_quote_string does not nul-terminate, so we have to do it
421 p = cpp_quote_string (to_file_quoted,
422 (const unsigned char *) file_path,
425 fprintf (stream, "# %u \"%s\"%s",
426 print.src_line == 0 ? 1 : print.src_line,
427 to_file_quoted, special_flags);
429 sysp = in_system_header_at (src_loc);
431 fputs (" 3 4", stream);
433 fputs (" 3", stream);
436 emitted_line_marker = true;
439 return emitted_line_marker;
442 /* Output a line marker for logical line LINE. Special flags are "1"
443 or "2" indicating entering or leaving a file. Return TRUE if a
444 line marker was effectively emitted, FALSE otherwise. */
447 print_line (source_location src_loc, const char *special_flags)
449 if (cpp_get_options (parse_in)->debug)
450 linemap_dump_location (line_table, src_loc,
452 return print_line_1 (src_loc, special_flags, print.outf);
455 /* Helper function for cb_line_change and scan_translation_unit.
456 Return TRUE if a line marker is emitted, FALSE otherwise. */
458 do_line_change (cpp_reader *pfile, const cpp_token *token,
459 source_location src_loc, int parsing_args)
461 bool emitted_line_marker = false;
462 if (define_queue || undef_queue)
463 dump_queued_macros (pfile);
465 if (token->type == CPP_EOF || parsing_args)
468 emitted_line_marker = maybe_print_line (src_loc);
472 /* Supply enough spaces to put this token in its original column,
473 one space per column greater than 2, since scan_translation_unit
474 will provide a space if PREV_WHITE. Don't bother trying to
475 reconstruct tabs; we can't get it right in general, and nothing
476 ought to care. Some things do care; the fault lies with them. */
477 if (!CPP_OPTION (pfile, traditional))
479 int spaces = LOCATION_COLUMN (src_loc) - 2;
480 print.printed = true;
482 while (-- spaces >= 0)
483 putc (' ', print.outf);
486 return emitted_line_marker;
489 /* Called when a line of output is started. TOKEN is the first token
490 of the line, and at end of file will be CPP_EOF. */
492 cb_line_change (cpp_reader *pfile, const cpp_token *token,
495 do_line_change (pfile, token, token->src_loc, parsing_args);
499 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
500 const cpp_string *str)
502 maybe_print_line (line);
503 fprintf (print.outf, "#ident %s\n", str->text);
508 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
510 const struct line_map *map;
512 maybe_print_line (line);
513 fputs ("#define ", print.outf);
515 /* 'D' is whole definition; 'N' is name only. */
516 if (flag_dump_macros == 'D')
517 fputs ((const char *) cpp_macro_definition (pfile, node),
520 fputs ((const char *) NODE_NAME (node), print.outf);
522 putc ('\n', print.outf);
523 print.printed = false;
524 linemap_resolve_location (line_table, line,
525 LRK_MACRO_DEFINITION_LOCATION,
527 if (LINEMAP_LINE (map) != 0)
532 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
535 maybe_print_line (line);
536 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
541 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
545 if (node->flags & NODE_BUILTIN)
547 q = XNEW (macro_queue);
548 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
549 q->next = define_queue;
554 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
555 source_location line ATTRIBUTE_UNUSED,
559 q = XNEW (macro_queue);
560 q->macro = xstrdup ((const char *) NODE_NAME (node));
561 q->next = undef_queue;
566 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
570 /* End the previous line of text. */
573 putc ('\n', print.outf);
575 print.printed = false;
578 for (q = define_queue; q;)
581 fputs ("#define ", print.outf);
582 fputs (q->macro, print.outf);
583 putc ('\n', print.outf);
584 print.printed = false;
592 for (q = undef_queue; q;)
595 fprintf (print.outf, "#undef %s\n", q->macro);
606 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
607 const unsigned char *dir, const char *header, int angle_brackets,
608 const cpp_token **comments)
610 maybe_print_line (line);
612 fprintf (print.outf, "#%s <%s>", dir, header);
614 fprintf (print.outf, "#%s \"%s\"", dir, header);
616 if (comments != NULL)
618 while (*comments != NULL)
620 if ((*comments)->flags & PREV_WHITE)
621 putc (' ', print.outf);
622 cpp_output_token (*comments, print.outf);
627 putc ('\n', print.outf);
628 print.printed = false;
632 /* Callback called when -fworking-director and -E to emit working
633 directory in cpp output file. */
636 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
638 size_t to_file_len = strlen (dir);
639 unsigned char *to_file_quoted =
640 (unsigned char *) alloca (to_file_len * 4 + 1);
643 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
644 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
646 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
649 /* The file name, line number or system header flags have changed, as
653 pp_file_change (const struct line_map *map)
655 const char *flags = "";
657 if (flag_no_line_commands)
662 input_location = map->start_location;
663 if (print.first_time)
665 /* Avoid printing foo.i when the main file is foo.c. */
666 if (!cpp_get_options (parse_in)->preprocessed)
667 print_line (map->start_location, flags);
668 print.first_time = 0;
672 /* Bring current file to correct line when entering a new file. */
673 if (map->reason == LC_ENTER)
675 const struct line_map *from = INCLUDED_FROM (line_table, map);
676 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
678 if (map->reason == LC_ENTER)
680 else if (map->reason == LC_LEAVE)
682 print_line (map->start_location, flags);
687 /* Copy a #pragma directive to the preprocessed output. */
689 cb_def_pragma (cpp_reader *pfile, source_location line)
691 maybe_print_line (line);
692 fputs ("#pragma ", print.outf);
693 cpp_output_line (pfile, print.outf);
694 print.printed = false;
698 /* Dump out the hash table. */
700 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
702 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
704 fputs ("#define ", print.outf);
705 fputs ((const char *) cpp_macro_definition (pfile, node),
707 putc ('\n', print.outf);
708 print.printed = false;
715 /* Load in the PCH file NAME, open on FD. It was originally searched for
716 by ORIG_NAME. Also, print out a #include command so that the PCH
717 file can be loaded when the preprocessed output is compiled. */
720 cb_read_pch (cpp_reader *pfile, const char *name,
721 int fd, const char *orig_name ATTRIBUTE_UNUSED)
723 c_common_read_pch (pfile, name, fd, orig_name);
725 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);