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"
28 #include "c-common.h" /* For flags. */
29 #include "c-pragma.h" /* For parse_in. */
31 /* Encapsulates state used to convert a stream of tokens into a text
35 FILE *outf
; /* Stream to write to. */
36 const cpp_token
*prev
; /* Previous token. */
37 const cpp_token
*source
; /* Source token for spacing. */
38 int src_line
; /* Line number currently being written. */
39 unsigned char printed
; /* Nonzero if something output at line. */
40 bool first_time
; /* pp_file_change hasn't been called yet. */
41 const char *src_file
; /* Current source file. */
42 bool prev_was_system_token
; /* True if the previous token was a
46 /* Defined and undefined macros being queued for output with -dU at
48 typedef struct macro_queue
50 struct macro_queue
*next
; /* Next macro in the list. */
51 char *macro
; /* The name of the macro if not
52 defined, the full definition if
55 static macro_queue
*define_queue
, *undef_queue
;
57 /* General output routines. */
58 static void scan_translation_unit (cpp_reader
*);
59 static void print_lines_directives_only (int, const void *, size_t);
60 static void scan_translation_unit_directives_only (cpp_reader
*);
61 static void scan_translation_unit_trad (cpp_reader
*);
62 static void account_for_newlines (const unsigned char *, size_t);
63 static int dump_macro (cpp_reader
*, cpp_hashnode
*, void *);
64 static void dump_queued_macros (cpp_reader
*);
66 static bool print_line_1 (source_location
, const char*, FILE *);
67 static bool print_line (source_location
, const char *);
68 static bool maybe_print_line_1 (source_location
, FILE *);
69 static bool maybe_print_line (source_location
);
70 static bool do_line_change (cpp_reader
*, const cpp_token
*,
71 source_location
, int);
73 /* Callback routines for the parser. Most of these are active only
75 static void cb_line_change (cpp_reader
*, const cpp_token
*, int);
76 static void cb_define (cpp_reader
*, source_location
, cpp_hashnode
*);
77 static void cb_undef (cpp_reader
*, source_location
, cpp_hashnode
*);
78 static void cb_used_define (cpp_reader
*, source_location
, cpp_hashnode
*);
79 static void cb_used_undef (cpp_reader
*, source_location
, cpp_hashnode
*);
80 static void cb_include (cpp_reader
*, source_location
, const unsigned char *,
81 const char *, int, const cpp_token
**);
82 static void cb_ident (cpp_reader
*, source_location
, const cpp_string
*);
83 static void cb_def_pragma (cpp_reader
*, source_location
);
84 static void cb_read_pch (cpp_reader
*pfile
, const char *name
,
85 int fd
, const char *orig_name
);
87 /* Preprocess and output. */
89 preprocess_file (cpp_reader
*pfile
)
91 /* A successful cpp_read_main_file guarantees that we can call
92 cpp_scan_nooutput or cpp_get_token next. */
93 if (flag_no_output
&& pfile
->buffer
)
95 /* Scan -included buffers, then the main file. */
96 while (pfile
->buffer
->prev
)
97 cpp_scan_nooutput (pfile
);
98 cpp_scan_nooutput (pfile
);
100 else if (cpp_get_options (pfile
)->traditional
)
101 scan_translation_unit_trad (pfile
);
102 else if (cpp_get_options (pfile
)->directives_only
103 && !cpp_get_options (pfile
)->preprocessed
)
104 scan_translation_unit_directives_only (pfile
);
106 scan_translation_unit (pfile
);
108 /* -dM command line option. Should this be elsewhere? */
109 if (flag_dump_macros
== 'M')
110 cpp_forall_identifiers (pfile
, dump_macro
, NULL
);
112 /* Flush any pending output. */
114 putc ('\n', print
.outf
);
117 /* Set up the callbacks as appropriate. */
119 init_pp_output (FILE *out_stream
)
121 cpp_callbacks
*cb
= cpp_get_callbacks (parse_in
);
125 cb
->line_change
= cb_line_change
;
126 /* Don't emit #pragma or #ident directives if we are processing
127 assembly language; the assembler may choke on them. */
128 if (cpp_get_options (parse_in
)->lang
!= CLK_ASM
)
130 cb
->ident
= cb_ident
;
131 cb
->def_pragma
= cb_def_pragma
;
135 if (flag_dump_includes
)
136 cb
->include
= cb_include
;
138 if (flag_pch_preprocess
)
140 cb
->valid_pch
= c_common_valid_pch
;
141 cb
->read_pch
= cb_read_pch
;
144 if (flag_dump_macros
== 'N' || flag_dump_macros
== 'D')
146 cb
->define
= cb_define
;
147 cb
->undef
= cb_undef
;
150 if (flag_dump_macros
== 'U')
152 cb
->before_define
= dump_queued_macros
;
153 cb
->used_define
= cb_used_define
;
154 cb
->used_undef
= cb_used_undef
;
157 cb
->has_attribute
= c_common_has_attribute
;
159 /* Initialize the print structure. */
163 print
.outf
= out_stream
;
164 print
.first_time
= 1;
166 print
.prev_was_system_token
= false;
169 /* Writes out the preprocessed file, handling spacing and paste
172 scan_translation_unit (cpp_reader
*pfile
)
174 bool avoid_paste
= false;
175 bool do_line_adjustments
176 = cpp_get_options (parse_in
)->lang
!= CLK_ASM
177 && !flag_no_line_commands
;
178 bool in_pragma
= false;
179 bool line_marker_emitted
= false;
185 const cpp_token
*token
= cpp_get_token_with_location (pfile
, &loc
);
187 if (token
->type
== CPP_PADDING
)
190 if (print
.source
== NULL
191 || (!(print
.source
->flags
& PREV_WHITE
)
192 && token
->val
.source
== NULL
))
193 print
.source
= token
->val
.source
;
197 if (token
->type
== CPP_EOF
)
200 /* Subtle logic to output a space if and only if necessary. */
203 int src_line
= LOCATION_LINE (loc
);
205 if (print
.source
== NULL
)
206 print
.source
= token
;
208 if (src_line
!= print
.src_line
209 && do_line_adjustments
212 line_marker_emitted
= do_line_change (pfile
, token
, loc
, false);
213 putc (' ', print
.outf
);
215 else if (print
.source
->flags
& PREV_WHITE
217 && cpp_avoid_paste (pfile
, print
.prev
, token
))
218 || (print
.prev
== NULL
&& token
->type
== CPP_HASH
))
219 putc (' ', print
.outf
);
221 else if (token
->flags
& PREV_WHITE
)
223 int src_line
= LOCATION_LINE (loc
);
225 if (src_line
!= print
.src_line
226 && do_line_adjustments
228 line_marker_emitted
= do_line_change (pfile
, token
, loc
, false);
229 putc (' ', print
.outf
);
235 if (token
->type
== CPP_PRAGMA
)
240 line_marker_emitted
= maybe_print_line (token
->src_loc
);
241 fputs ("#pragma ", print
.outf
);
242 c_pp_lookup_pragma (token
->val
.pragma
, &space
, &name
);
244 fprintf (print
.outf
, "%s %s", space
, name
);
246 fprintf (print
.outf
, "%s", name
);
250 else if (token
->type
== CPP_PRAGMA_EOL
)
252 maybe_print_line (token
->src_loc
);
257 if (cpp_get_options (parse_in
)->debug
)
258 linemap_dump_location (line_table
, token
->src_loc
,
261 if (do_line_adjustments
263 && !line_marker_emitted
264 && print
.prev_was_system_token
!= !!in_system_header_at(loc
)
265 && !is_location_from_builtin_token (loc
))
266 /* The system-ness of this token is different from the one
267 of the previous token. Let's emit a line change to
268 mark the new system-ness before we emit the token. */
270 do_line_change (pfile
, token
, loc
, false);
271 print
.prev_was_system_token
= !!in_system_header_at(loc
);
273 cpp_output_token (token
, print
.outf
);
274 line_marker_emitted
= false;
277 /* CPP_COMMENT tokens and raw-string literal tokens can
278 have embedded new-line characters. Rather than enumerating
279 all the possible token types just check if token uses
280 val.str union member. */
281 if (cpp_token_val_index (token
) == CPP_TOKEN_FLD_STR
)
282 account_for_newlines (token
->val
.str
.text
, token
->val
.str
.len
);
287 print_lines_directives_only (int lines
, const void *buf
, size_t size
)
289 print
.src_line
+= lines
;
290 fwrite (buf
, 1, size
, print
.outf
);
293 /* Writes out the preprocessed file, handling spacing and paste
296 scan_translation_unit_directives_only (cpp_reader
*pfile
)
298 struct _cpp_dir_only_callbacks cb
;
300 cb
.print_lines
= print_lines_directives_only
;
301 cb
.maybe_print_line
= (void (*) (source_location
)) maybe_print_line
;
303 _cpp_preprocess_dir_only (pfile
, &cb
);
306 /* Adjust print.src_line for newlines embedded in output. */
308 account_for_newlines (const unsigned char *str
, size_t len
)
315 /* Writes out a traditionally preprocessed file. */
317 scan_translation_unit_trad (cpp_reader
*pfile
)
319 while (_cpp_read_logical_line_trad (pfile
))
321 size_t len
= pfile
->out
.cur
- pfile
->out
.base
;
322 maybe_print_line (pfile
->out
.first_line
);
323 fwrite (pfile
->out
.base
, 1, len
, print
.outf
);
325 if (!CPP_OPTION (pfile
, discard_comments
))
326 account_for_newlines (pfile
->out
.base
, len
);
330 /* If the token read on logical line LINE needs to be output on a
331 different line to the current one, output the required newlines or
332 a line marker. If a line marker was emitted, return TRUE otherwise
336 maybe_print_line_1 (source_location src_loc
, FILE *stream
)
338 bool emitted_line_marker
= false;
339 int src_line
= LOCATION_LINE (src_loc
);
340 const char *src_file
= LOCATION_FILE (src_loc
);
342 /* End the previous line of text. */
350 if (!flag_no_line_commands
351 && src_line
>= print
.src_line
352 && src_line
< print
.src_line
+ 8
353 && strcmp (src_file
, print
.src_file
) == 0)
355 while (src_line
> print
.src_line
)
362 emitted_line_marker
= print_line_1 (src_loc
, "", stream
);
364 return emitted_line_marker
;
367 /* If the token read on logical line LINE needs to be output on a
368 different line to the current one, output the required newlines or
369 a line marker. If a line marker was emitted, return TRUE otherwise
373 maybe_print_line (source_location src_loc
)
375 if (cpp_get_options (parse_in
)->debug
)
376 linemap_dump_location (line_table
, src_loc
,
378 return maybe_print_line_1 (src_loc
, print
.outf
);
381 /* Output a line marker for logical line LINE. Special flags are "1"
382 or "2" indicating entering or leaving a file. If the line marker
383 was effectively emitted, return TRUE otherwise return FALSE. */
386 print_line_1 (source_location src_loc
, const char *special_flags
, FILE *stream
)
388 bool emitted_line_marker
= false;
390 /* End any previous line of text. */
395 if (!flag_no_line_commands
)
397 const char *file_path
= LOCATION_FILE (src_loc
);
399 size_t to_file_len
= strlen (file_path
);
400 unsigned char *to_file_quoted
=
401 (unsigned char *) alloca (to_file_len
* 4 + 1);
404 print
.src_line
= LOCATION_LINE (src_loc
);
405 print
.src_file
= file_path
;
407 /* cpp_quote_string does not nul-terminate, so we have to do it
409 p
= cpp_quote_string (to_file_quoted
,
410 (const unsigned char *) file_path
,
413 fprintf (stream
, "# %u \"%s\"%s",
414 print
.src_line
== 0 ? 1 : print
.src_line
,
415 to_file_quoted
, special_flags
);
417 sysp
= in_system_header_at (src_loc
);
419 fputs (" 3 4", stream
);
421 fputs (" 3", stream
);
424 emitted_line_marker
= true;
427 return emitted_line_marker
;
430 /* Output a line marker for logical line LINE. Special flags are "1"
431 or "2" indicating entering or leaving a file. Return TRUE if a
432 line marker was effectively emitted, FALSE otherwise. */
435 print_line (source_location src_loc
, const char *special_flags
)
437 if (cpp_get_options (parse_in
)->debug
)
438 linemap_dump_location (line_table
, src_loc
,
440 return print_line_1 (src_loc
, special_flags
, print
.outf
);
443 /* Helper function for cb_line_change and scan_translation_unit.
444 Return TRUE if a line marker is emitted, FALSE otherwise. */
446 do_line_change (cpp_reader
*pfile
, const cpp_token
*token
,
447 source_location src_loc
, int parsing_args
)
449 bool emitted_line_marker
= false;
450 if (define_queue
|| undef_queue
)
451 dump_queued_macros (pfile
);
453 if (token
->type
== CPP_EOF
|| parsing_args
)
456 emitted_line_marker
= maybe_print_line (src_loc
);
460 /* Supply enough spaces to put this token in its original column,
461 one space per column greater than 2, since scan_translation_unit
462 will provide a space if PREV_WHITE. Don't bother trying to
463 reconstruct tabs; we can't get it right in general, and nothing
464 ought to care. Some things do care; the fault lies with them. */
465 if (!CPP_OPTION (pfile
, traditional
))
467 int spaces
= LOCATION_COLUMN (src_loc
) - 2;
470 while (-- spaces
>= 0)
471 putc (' ', print
.outf
);
474 return emitted_line_marker
;
477 /* Called when a line of output is started. TOKEN is the first token
478 of the line, and at end of file will be CPP_EOF. */
480 cb_line_change (cpp_reader
*pfile
, const cpp_token
*token
,
483 do_line_change (pfile
, token
, token
->src_loc
, parsing_args
);
487 cb_ident (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
488 const cpp_string
*str
)
490 maybe_print_line (line
);
491 fprintf (print
.outf
, "#ident %s\n", str
->text
);
496 cb_define (cpp_reader
*pfile
, source_location line
, cpp_hashnode
*node
)
498 const line_map_ordinary
*map
;
500 maybe_print_line (line
);
501 fputs ("#define ", print
.outf
);
503 /* 'D' is whole definition; 'N' is name only. */
504 if (flag_dump_macros
== 'D')
505 fputs ((const char *) cpp_macro_definition (pfile
, node
),
508 fputs ((const char *) NODE_NAME (node
), print
.outf
);
510 putc ('\n', print
.outf
);
511 linemap_resolve_location (line_table
, line
,
512 LRK_MACRO_DEFINITION_LOCATION
,
514 if (LINEMAP_LINE (map
) != 0)
519 cb_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
522 maybe_print_line (line
);
523 fprintf (print
.outf
, "#undef %s\n", NODE_NAME (node
));
528 cb_used_define (cpp_reader
*pfile
, source_location line ATTRIBUTE_UNUSED
,
532 if (node
->flags
& NODE_BUILTIN
)
534 q
= XNEW (macro_queue
);
535 q
->macro
= xstrdup ((const char *) cpp_macro_definition (pfile
, node
));
536 q
->next
= define_queue
;
541 cb_used_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
542 source_location line ATTRIBUTE_UNUSED
,
546 q
= XNEW (macro_queue
);
547 q
->macro
= xstrdup ((const char *) NODE_NAME (node
));
548 q
->next
= undef_queue
;
553 dump_queued_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
557 /* End the previous line of text. */
560 putc ('\n', print
.outf
);
565 for (q
= define_queue
; q
;)
568 fputs ("#define ", print
.outf
);
569 fputs (q
->macro
, print
.outf
);
570 putc ('\n', print
.outf
);
578 for (q
= undef_queue
; q
;)
581 fprintf (print
.outf
, "#undef %s\n", q
->macro
);
592 cb_include (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
593 const unsigned char *dir
, const char *header
, int angle_brackets
,
594 const cpp_token
**comments
)
596 maybe_print_line (line
);
598 fprintf (print
.outf
, "#%s <%s>", dir
, header
);
600 fprintf (print
.outf
, "#%s \"%s\"", dir
, header
);
602 if (comments
!= NULL
)
604 while (*comments
!= NULL
)
606 if ((*comments
)->flags
& PREV_WHITE
)
607 putc (' ', print
.outf
);
608 cpp_output_token (*comments
, print
.outf
);
613 putc ('\n', print
.outf
);
617 /* Callback called when -fworking-director and -E to emit working
618 directory in cpp output file. */
621 pp_dir_change (cpp_reader
*pfile ATTRIBUTE_UNUSED
, const char *dir
)
623 size_t to_file_len
= strlen (dir
);
624 unsigned char *to_file_quoted
=
625 (unsigned char *) alloca (to_file_len
* 4 + 1);
628 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
629 p
= cpp_quote_string (to_file_quoted
, (const unsigned char *) dir
, to_file_len
);
631 fprintf (print
.outf
, "# 1 \"%s//\"\n", to_file_quoted
);
634 /* The file name, line number or system header flags have changed, as
638 pp_file_change (const line_map_ordinary
*map
)
640 const char *flags
= "";
642 if (flag_no_line_commands
)
647 input_location
= map
->start_location
;
648 if (print
.first_time
)
650 /* Avoid printing foo.i when the main file is foo.c. */
651 if (!cpp_get_options (parse_in
)->preprocessed
)
652 print_line (map
->start_location
, flags
);
653 print
.first_time
= 0;
657 /* Bring current file to correct line when entering a new file. */
658 if (map
->reason
== LC_ENTER
)
660 const line_map_ordinary
*from
= INCLUDED_FROM (line_table
, map
);
661 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from
));
663 if (map
->reason
== LC_ENTER
)
665 else if (map
->reason
== LC_LEAVE
)
667 print_line (map
->start_location
, flags
);
672 /* Copy a #pragma directive to the preprocessed output. */
674 cb_def_pragma (cpp_reader
*pfile
, source_location line
)
676 maybe_print_line (line
);
677 fputs ("#pragma ", print
.outf
);
678 cpp_output_line (pfile
, print
.outf
);
682 /* Dump out the hash table. */
684 dump_macro (cpp_reader
*pfile
, cpp_hashnode
*node
, void *v ATTRIBUTE_UNUSED
)
686 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
688 fputs ("#define ", print
.outf
);
689 fputs ((const char *) cpp_macro_definition (pfile
, node
),
691 putc ('\n', print
.outf
);
698 /* Load in the PCH file NAME, open on FD. It was originally searched for
699 by ORIG_NAME. Also, print out a #include command so that the PCH
700 file can be loaded when the preprocessed output is compiled. */
703 cb_read_pch (cpp_reader
*pfile
, const char *name
,
704 int fd
, const char *orig_name ATTRIBUTE_UNUSED
)
706 c_common_read_pch (pfile
, name
, fd
, orig_name
);
708 fprintf (print
.outf
, "#pragma GCC pch_preprocess \"%s\"\n", name
);