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 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
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. */
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
);
222 else if (print
.source
->flags
& PREV_WHITE
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
235 line_marker_emitted
= do_line_change (pfile
, token
, loc
, false);
236 putc (' ', print
.outf
);
242 if (token
->type
== CPP_PRAGMA
)
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
);
251 fprintf (print
.outf
, "%s %s", space
, name
);
253 fprintf (print
.outf
, "%s", name
);
257 else if (token
->type
== CPP_PRAGMA_EOL
)
259 maybe_print_line (token
->src_loc
);
264 if (cpp_get_options (parse_in
)->debug
)
265 linemap_dump_location (line_table
, token
->src_loc
,
268 if (do_line_adjustments
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
);
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
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. */
315 account_for_newlines (const unsigned char *str
, size_t len
)
322 /* Writes out a traditionally preprocessed file. */
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
);
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
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. */
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
)
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
380 maybe_print_line (source_location src_loc
)
382 if (cpp_get_options (parse_in
)->debug
)
383 linemap_dump_location (line_table
, src_loc
,
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. */
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. */
402 if (!flag_no_line_commands
)
404 const char *file_path
= LOCATION_FILE (src_loc
);
406 size_t to_file_len
= strlen (file_path
);
407 unsigned char *to_file_quoted
=
408 (unsigned char *) alloca (to_file_len
* 4 + 1);
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
416 p
= cpp_quote_string (to_file_quoted
,
417 (const unsigned char *) file_path
,
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
);
426 fputs (" 3 4", stream
);
428 fputs (" 3", 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. */
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
,
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. */
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
)
463 emitted_line_marker
= maybe_print_line (src_loc
);
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;
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. */
487 cb_line_change (cpp_reader
*pfile
, const cpp_token
*token
,
490 do_line_change (pfile
, token
, token
->src_loc
, parsing_args
);
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
);
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
),
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
,
521 if (LINEMAP_LINE (map
) != 0)
526 cb_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
529 maybe_print_line (line
);
530 fprintf (print
.outf
, "#undef %s\n", NODE_NAME (node
));
535 cb_used_define (cpp_reader
*pfile
, source_location line ATTRIBUTE_UNUSED
,
539 if (node
->flags
& NODE_BUILTIN
)
541 q
= XNEW (macro_queue
);
542 q
->macro
= xstrdup ((const char *) cpp_macro_definition (pfile
, node
));
543 q
->next
= define_queue
;
548 cb_used_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
549 source_location line ATTRIBUTE_UNUSED
,
553 q
= XNEW (macro_queue
);
554 q
->macro
= xstrdup ((const char *) NODE_NAME (node
));
555 q
->next
= undef_queue
;
560 dump_queued_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
564 /* End the previous line of text. */
567 putc ('\n', print
.outf
);
572 for (q
= define_queue
; q
;)
575 fputs ("#define ", print
.outf
);
576 fputs (q
->macro
, print
.outf
);
577 putc ('\n', print
.outf
);
585 for (q
= undef_queue
; q
;)
588 fprintf (print
.outf
, "#undef %s\n", q
->macro
);
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
);
605 fprintf (print
.outf
, "#%s <%s>", dir
, header
);
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
);
620 putc ('\n', print
.outf
);
624 /* Callback called when -fworking-director and -E to emit working
625 directory in cpp output file. */
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);
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
);
638 fprintf (print
.outf
, "# 1 \"%s//\"\n", to_file_quoted
);
641 /* The file name, line number or system header flags have changed, as
645 pp_file_change (const struct line_map
*map
)
647 const char *flags
= "";
649 if (flag_no_line_commands
)
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;
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
)
672 else if (map
->reason
== LC_LEAVE
)
674 print_line (map
->start_location
, flags
);
679 /* Copy a #pragma directive to the preprocessed output. */
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
);
689 /* Dump out the hash table. */
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
),
698 putc ('\n', print
.outf
);
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. */
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
);