1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995-2023 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"
22 #include "c-common.h" /* For flags. */
23 #include "../libcpp/internal.h"
24 #include "langhooks.h"
25 #include "c-pragma.h" /* For parse_in. */
26 #include "file-prefix-map.h" /* remap_macro_filename() */
30 /* Encapsulates state used to convert a stream of tokens into a text
34 FILE *outf
; /* Stream to write to. */
35 const cpp_token
*prev
; /* Previous token. */
36 const cpp_token
*source
; /* Source token for spacing. */
37 unsigned src_line
; /* Line number currently being written. */
38 bool printed
; /* True if something output at line. */
39 bool first_time
; /* pp_file_change hasn't been called yet. */
40 bool prev_was_system_token
; /* True if the previous token was a
42 const char *src_file
; /* Current source file. */
43 token_streamer
*streamer
; /* Instance of class token_streamer using this
47 /* Defined and undefined macros being queued for output with -dU at
51 struct macro_queue
*next
; /* Next macro in the list. */
52 char *macro
; /* The name of the macro if not
53 defined, the full definition if
56 static macro_queue
*define_queue
, *undef_queue
;
58 /* General output routines. */
59 static void scan_translation_unit (cpp_reader
*);
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 (location_t
, const char*, FILE *);
67 static bool print_line (location_t
, const char *);
68 static bool maybe_print_line_1 (location_t
, FILE *);
69 static bool maybe_print_line (location_t
);
70 static bool do_line_change (cpp_reader
*, const cpp_token
*,
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
*, location_t
, cpp_hashnode
*);
77 static void cb_undef (cpp_reader
*, location_t
, cpp_hashnode
*);
78 static void cb_used_define (cpp_reader
*, location_t
, cpp_hashnode
*);
79 static void cb_used_undef (cpp_reader
*, location_t
, cpp_hashnode
*);
80 static void cb_include (cpp_reader
*, location_t
, const unsigned char *,
81 const char *, int, const cpp_token
**);
82 static void cb_ident (cpp_reader
*, location_t
, const cpp_string
*);
83 static void cb_def_pragma (cpp_reader
*, location_t
);
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 /* Don't emit #pragma or #ident directives if we are processing
118 assembly language; the assembler may choke on them. */
120 should_output_pragmas ()
122 return cpp_get_options (parse_in
)->lang
!= CLK_ASM
;
125 /* Set up the callbacks as appropriate. */
127 init_pp_output (FILE *out_stream
)
129 cpp_callbacks
*cb
= cpp_get_callbacks (parse_in
);
133 cb
->line_change
= cb_line_change
;
134 if (should_output_pragmas ())
136 cb
->ident
= cb_ident
;
137 cb
->def_pragma
= cb_def_pragma
;
141 if (flag_dump_includes
)
142 cb
->include
= cb_include
;
144 if (flag_pch_preprocess
)
146 cb
->valid_pch
= c_common_valid_pch
;
147 cb
->read_pch
= cb_read_pch
;
150 if (flag_dump_macros
== 'N' || flag_dump_macros
== 'D')
152 cb
->define
= cb_define
;
153 cb
->undef
= cb_undef
;
156 if (flag_dump_macros
== 'U')
158 cb
->before_define
= dump_queued_macros
;
159 cb
->used_define
= cb_used_define
;
160 cb
->used_undef
= cb_used_undef
;
163 cb
->has_attribute
= c_common_has_attribute
;
164 cb
->has_builtin
= c_common_has_builtin
;
165 cb
->get_source_date_epoch
= cb_get_source_date_epoch
;
166 cb
->remap_filename
= remap_macro_filename
;
168 /* Initialize the print structure. */
170 print
.printed
= false;
172 print
.outf
= out_stream
;
173 print
.first_time
= 1;
175 print
.prev_was_system_token
= false;
176 print
.streamer
= nullptr;
179 // FIXME: Ideally we'd just turn the entirety of the print struct into
180 // an encapsulated streamer ...
185 bool do_line_adjustments
;
189 token_streamer (cpp_reader
*pfile
)
190 :avoid_paste (false),
191 do_line_adjustments (cpp_get_options (pfile
)->lang
!= CLK_ASM
192 && !flag_no_line_commands
),
195 gcc_assert (!print
.streamer
);
196 print
.streamer
= this;
204 void stream (cpp_reader
*pfile
, const cpp_token
*tok
, location_t
);
208 token_streamer::stream (cpp_reader
*pfile
, const cpp_token
*token
,
211 /* Keep input_location up to date, since it is needed for processing early
212 pragmas such as #pragma GCC diagnostic. */
213 input_location
= loc
;
215 if (token
->type
== CPP_PADDING
)
218 if (print
.source
== NULL
219 || (!(print
.source
->flags
& PREV_WHITE
)
220 && token
->val
.source
== NULL
))
221 print
.source
= token
->val
.source
;
225 if (token
->type
== CPP_EOF
)
228 /* Keep track when we move into and out of system locations. */
229 const bool is_system_token
= in_system_header_at (loc
);
230 const bool system_state_changed
231 = (is_system_token
!= print
.prev_was_system_token
);
232 print
.prev_was_system_token
= is_system_token
;
234 /* Subtle logic to output a space if and only if necessary. */
235 bool line_marker_emitted
= false;
238 unsigned src_line
= LOCATION_LINE (loc
);
240 if (print
.source
== NULL
)
241 print
.source
= token
;
243 if (src_line
!= print
.src_line
244 && do_line_adjustments
247 line_marker_emitted
= do_line_change (pfile
, token
, loc
, false);
248 putc (' ', print
.outf
);
249 print
.printed
= true;
251 else if (print
.source
->flags
& PREV_WHITE
253 && cpp_avoid_paste (pfile
, print
.prev
, token
))
254 || (print
.prev
== NULL
&& token
->type
== CPP_HASH
))
256 putc (' ', print
.outf
);
257 print
.printed
= true;
260 else if (token
->flags
& PREV_WHITE
&& token
->type
!= CPP_PRAGMA
)
262 unsigned src_line
= LOCATION_LINE (loc
);
264 if (src_line
!= print
.src_line
265 && do_line_adjustments
267 line_marker_emitted
= do_line_change (pfile
, token
, loc
, false);
268 putc (' ', print
.outf
);
269 print
.printed
= true;
275 if (token
->type
== CPP_PRAGMA
)
278 if (should_output_pragmas ())
283 line_marker_emitted
= maybe_print_line (token
->src_loc
);
284 fputs ("#pragma ", print
.outf
);
285 c_pp_lookup_pragma (token
->val
.pragma
, &space
, &name
);
287 fprintf (print
.outf
, "%s %s", space
, name
);
289 fprintf (print
.outf
, "%s", name
);
290 print
.printed
= true;
292 if (token
->val
.pragma
>= PRAGMA_FIRST_EXTERNAL
)
293 c_pp_invoke_early_pragma_handler (token
->val
.pragma
);
295 else if (token
->type
== CPP_PRAGMA_EOL
)
297 if (should_output_pragmas ())
298 maybe_print_line (UNKNOWN_LOCATION
);
303 if (cpp_get_options (parse_in
)->debug
)
304 linemap_dump_location (line_table
, token
->src_loc
, print
.outf
);
306 if (do_line_adjustments
308 && !line_marker_emitted
309 && system_state_changed
310 && !is_location_from_builtin_token (loc
))
311 /* The system-ness of this token is different from the one of
312 the previous token. Let's emit a line change to mark the
313 new system-ness before we emit the token. */
315 line_marker_emitted
= do_line_change (pfile
, token
, loc
, false);
317 if (!in_pragma
|| should_output_pragmas ())
319 cpp_output_token (token
, print
.outf
);
320 print
.printed
= true;
324 /* CPP_COMMENT tokens and raw-string literal tokens can have
325 embedded new-line characters. Rather than enumerating all the
326 possible token types just check if token uses val.str union
328 if (cpp_token_val_index (token
) == CPP_TOKEN_FLD_STR
)
329 account_for_newlines (token
->val
.str
.text
, token
->val
.str
.len
);
332 /* Writes out the preprocessed file, handling spacing and paste
336 scan_translation_unit (cpp_reader
*pfile
)
338 token_streamer
streamer (pfile
);
339 uintptr_t filter
= 0;
341 if (lang_hooks
.preprocess_token
)
342 filter
= lang_hooks
.preprocess_token (pfile
, NULL
, filter
);
347 location_t spelling_loc
;
348 const cpp_token
*token
349 = cpp_get_token_with_location (pfile
, &spelling_loc
);
351 streamer
.stream (pfile
, token
, spelling_loc
);
354 unsigned flags
= lang_hooks
.preprocess_token (pfile
, token
, filter
);
355 if (flags
& lang_hooks::PT_begin_pragma
)
356 streamer
.begin_pragma ();
358 if (token
->type
== CPP_EOF
)
363 lang_hooks
.preprocess_token (pfile
, NULL
, filter
);
366 class do_streamer
: public token_streamer
371 do_streamer (cpp_reader
*pfile
, uintptr_t filter
)
372 :token_streamer (pfile
), filter (filter
)
378 directives_only_cb (cpp_reader
*pfile
, CPP_DO_task task
, void *data_
, ...)
381 va_start (args
, data_
);
383 do_streamer
*streamer
= reinterpret_cast <do_streamer
*> (data_
);
391 print
.src_line
+= va_arg (args
, unsigned);
393 const void *buf
= va_arg (args
, const void *);
394 size_t size
= va_arg (args
, size_t);
395 fwrite (buf
, 1, size
, print
.outf
);
399 case CPP_DO_location
:
400 maybe_print_line (va_arg (args
, location_t
));
405 const cpp_token
*token
= va_arg (args
, const cpp_token
*);
406 location_t spelling_loc
= va_arg (args
, location_t
);
407 streamer
->stream (pfile
, token
, spelling_loc
);
408 if (streamer
->filter
)
410 unsigned flags
= lang_hooks
.preprocess_token
411 (pfile
, token
, streamer
->filter
);
412 if (flags
& lang_hooks::PT_begin_pragma
)
413 streamer
->begin_pragma ();
422 /* Writes out the preprocessed file, handling spacing and paste
425 scan_translation_unit_directives_only (cpp_reader
*pfile
)
427 uintptr_t filter
= 0;
428 if (lang_hooks
.preprocess_token
)
429 filter
= lang_hooks
.preprocess_token (pfile
, NULL
, filter
);
430 do_streamer
streamer (pfile
, filter
);
431 cpp_directive_only_process (pfile
, &streamer
, directives_only_cb
);
433 lang_hooks
.preprocess_token (pfile
, NULL
, streamer
.filter
);
436 /* Adjust print.src_line for newlines embedded in output. For example, if a raw
437 string literal contains newlines, then we need to increment our notion of the
438 current line to keep in sync and avoid outputting a line marker
439 unnecessarily. If a raw string literal containing newlines is the result of
440 macro expansion, then we have the opposite problem, where the token takes up
441 more lines in the output than it did in the input, and hence a line marker is
442 needed to restore the correct state for subsequent lines. In this case,
443 incrementing print.src_line still does the job, because it will cause us to
444 emit the line marker the next time a token is streamed. */
446 account_for_newlines (const unsigned char *str
, size_t len
)
453 /* Writes out a traditionally preprocessed file. */
455 scan_translation_unit_trad (cpp_reader
*pfile
)
457 while (_cpp_read_logical_line_trad (pfile
))
459 size_t len
= pfile
->out
.cur
- pfile
->out
.base
;
460 maybe_print_line (pfile
->out
.first_line
);
461 fwrite (pfile
->out
.base
, 1, len
, print
.outf
);
462 print
.printed
= true;
463 if (!CPP_OPTION (pfile
, discard_comments
))
464 account_for_newlines (pfile
->out
.base
, len
);
468 /* If the token read on logical line LINE needs to be output on a
469 different line to the current one, output the required newlines or
470 a line marker. If a line marker was emitted, return TRUE otherwise
474 maybe_print_line_1 (location_t src_loc
, FILE *stream
)
476 bool emitted_line_marker
= false;
477 unsigned src_line
= LOCATION_LINE (src_loc
);
478 const char *src_file
= LOCATION_FILE (src_loc
);
480 /* End the previous line of text. */
485 print
.printed
= false;
488 if (!flag_no_line_commands
489 && src_line
>= print
.src_line
490 && src_line
< print
.src_line
+ 8
491 && src_loc
!= UNKNOWN_LOCATION
492 && strcmp (src_file
, print
.src_file
) == 0)
494 while (src_line
> print
.src_line
)
501 emitted_line_marker
= print_line_1 (src_loc
, "", stream
);
503 return emitted_line_marker
;
506 /* If the token read on logical line LINE needs to be output on a
507 different line to the current one, output the required newlines or
508 a line marker. If a line marker was emitted, return TRUE otherwise
512 maybe_print_line (location_t src_loc
)
514 if (cpp_get_options (parse_in
)->debug
)
515 linemap_dump_location (line_table
, src_loc
,
517 return maybe_print_line_1 (src_loc
, print
.outf
);
520 /* Output a line marker for logical line LINE. Special flags are "1"
521 or "2" indicating entering or leaving a file. If the line marker
522 was effectively emitted, return TRUE otherwise return FALSE. */
525 print_line_1 (location_t src_loc
, const char *special_flags
, FILE *stream
)
527 bool emitted_line_marker
= false;
529 /* End any previous line of text. */
532 print
.printed
= false;
534 if (src_loc
!= UNKNOWN_LOCATION
&& !flag_no_line_commands
)
536 const char *file_path
= LOCATION_FILE (src_loc
);
537 size_t to_file_len
= strlen (file_path
);
538 unsigned char *to_file_quoted
=
539 (unsigned char *) alloca (to_file_len
* 4 + 1);
541 /* cpp_quote_string does not nul-terminate, so we have to do it
543 unsigned char *p
= cpp_quote_string (to_file_quoted
,
544 (const unsigned char *) file_path
,
548 print
.src_line
= LOCATION_LINE (src_loc
);
549 print
.src_file
= file_path
;
551 fprintf (stream
, "# %u \"%s\"%s",
552 print
.src_line
, to_file_quoted
, special_flags
);
554 int sysp
= in_system_header_at (src_loc
);
556 fputs (" 3 4", stream
);
558 fputs (" 3", stream
);
561 emitted_line_marker
= true;
564 return emitted_line_marker
;
567 /* Output a line marker for logical line LINE. Special flags are "1"
568 or "2" indicating entering or leaving a file. Return TRUE if a
569 line marker was effectively emitted, FALSE otherwise. */
572 print_line (location_t src_loc
, const char *special_flags
)
574 if (cpp_get_options (parse_in
)->debug
)
575 linemap_dump_location (line_table
, src_loc
,
577 return print_line_1 (src_loc
, special_flags
, print
.outf
);
580 /* Helper function for cb_line_change and scan_translation_unit.
581 Return TRUE if a line marker is emitted, FALSE otherwise. */
583 do_line_change (cpp_reader
*pfile
, const cpp_token
*token
,
584 location_t src_loc
, int parsing_args
)
586 bool emitted_line_marker
= false;
587 if (define_queue
|| undef_queue
)
588 dump_queued_macros (pfile
);
590 if (token
->type
== CPP_EOF
|| parsing_args
)
593 emitted_line_marker
= maybe_print_line (src_loc
);
597 /* Supply enough spaces to put this token in its original column,
598 one space per column greater than 2, since scan_translation_unit
599 will provide a space if PREV_WHITE. Don't bother trying to
600 reconstruct tabs; we can't get it right in general, and nothing
601 ought to care. Some things do care; the fault lies with them.
603 Also do not output the spaces if this is a CPP_PRAGMA token. In this
604 case, libcpp has provided the location of the first token after #pragma,
605 so we would start at the wrong column. */
606 if (!CPP_OPTION (pfile
, traditional
) && token
->type
!= CPP_PRAGMA
)
608 int spaces
= LOCATION_COLUMN (src_loc
) - 2;
609 print
.printed
= true;
611 while (-- spaces
>= 0)
612 putc (' ', print
.outf
);
615 return emitted_line_marker
;
618 /* Called when a line of output is started. TOKEN is the first token
619 of the line, and at end of file will be CPP_EOF. */
621 cb_line_change (cpp_reader
*pfile
, const cpp_token
*token
,
624 do_line_change (pfile
, token
, token
->src_loc
, parsing_args
);
628 cb_ident (cpp_reader
*pfile ATTRIBUTE_UNUSED
, location_t line
,
629 const cpp_string
*str
)
631 maybe_print_line (line
);
632 fprintf (print
.outf
, "#ident %s\n", str
->text
);
637 cb_define (cpp_reader
*pfile
, location_t line
, cpp_hashnode
*node
)
639 const line_map_ordinary
*map
;
641 maybe_print_line (line
);
642 fputs ("#define ", print
.outf
);
644 /* 'D' is whole definition; 'N' is name only. */
645 if (flag_dump_macros
== 'D')
646 fputs ((const char *) cpp_macro_definition (pfile
, node
),
649 fputs ((const char *) NODE_NAME (node
), print
.outf
);
651 putc ('\n', print
.outf
);
652 print
.printed
= false;
653 linemap_resolve_location (line_table
, line
,
654 LRK_MACRO_DEFINITION_LOCATION
,
660 cb_undef (cpp_reader
*pfile
, location_t line
, cpp_hashnode
*node
)
662 if (lang_hooks
.preprocess_undef
)
663 lang_hooks
.preprocess_undef (pfile
, line
, node
);
664 maybe_print_line (line
);
665 fprintf (print
.outf
, "#undef %s\n", NODE_NAME (node
));
670 cb_used_define (cpp_reader
*pfile
, location_t line ATTRIBUTE_UNUSED
,
673 if (cpp_user_macro_p (node
))
676 q
= XNEW (macro_queue
);
677 q
->macro
= xstrdup ((const char *) cpp_macro_definition (pfile
, node
));
678 q
->next
= define_queue
;
684 cb_used_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
685 location_t line ATTRIBUTE_UNUSED
,
689 q
= XNEW (macro_queue
);
690 q
->macro
= xstrdup ((const char *) NODE_NAME (node
));
691 q
->next
= undef_queue
;
696 dump_queued_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
700 /* End the previous line of text. */
703 putc ('\n', print
.outf
);
705 print
.printed
= false;
708 for (q
= define_queue
; q
;)
711 fputs ("#define ", print
.outf
);
712 fputs (q
->macro
, print
.outf
);
713 putc ('\n', print
.outf
);
714 print
.printed
= false;
722 for (q
= undef_queue
; q
;)
725 fprintf (print
.outf
, "#undef %s\n", q
->macro
);
736 cb_include (cpp_reader
*pfile ATTRIBUTE_UNUSED
, location_t line
,
737 const unsigned char *dir
, const char *header
, int angle_brackets
,
738 const cpp_token
**comments
)
740 maybe_print_line (line
);
742 fprintf (print
.outf
, "#%s <%s>", dir
, header
);
744 fprintf (print
.outf
, "#%s \"%s\"", dir
, header
);
746 if (comments
!= NULL
)
748 while (*comments
!= NULL
)
750 if ((*comments
)->flags
& PREV_WHITE
)
751 putc (' ', print
.outf
);
752 cpp_output_token (*comments
, print
.outf
);
757 putc ('\n', print
.outf
);
758 print
.printed
= false;
762 /* Callback called when -fworking-director and -E to emit working
763 directory in cpp output file. */
766 pp_dir_change (cpp_reader
*pfile ATTRIBUTE_UNUSED
, const char *dir
)
768 size_t to_file_len
= strlen (dir
);
769 unsigned char *to_file_quoted
=
770 (unsigned char *) alloca (to_file_len
* 4 + 1);
773 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
774 p
= cpp_quote_string (to_file_quoted
, (const unsigned char *) dir
, to_file_len
);
776 fprintf (print
.outf
, "# 1 \"%s//\"\n", to_file_quoted
);
779 /* The file name, line number or system header flags have changed, as
783 pp_file_change (const line_map_ordinary
*map
)
785 const char *flags
= "";
787 if (flag_no_line_commands
)
792 input_location
= map
->start_location
;
793 if (print
.first_time
)
795 /* Avoid printing foo.i when the main file is foo.c. */
796 if (!cpp_get_options (parse_in
)->preprocessed
)
797 print_line (map
->start_location
, flags
);
798 print
.first_time
= 0;
802 /* Bring current file to correct line when entering a new file. */
803 if (map
->reason
== LC_ENTER
)
805 maybe_print_line (linemap_included_from (map
));
808 else if (map
->reason
== LC_LEAVE
)
810 print_line (map
->start_location
, flags
);
815 /* Copy a #pragma directive to the preprocessed output. */
817 cb_def_pragma (cpp_reader
*pfile
, location_t line
)
819 maybe_print_line (line
);
820 fputs ("#pragma ", print
.outf
);
821 cpp_output_line (pfile
, print
.outf
);
822 print
.printed
= false;
826 /* Stream a token as if we had seen it directly ourselves; needed
827 in case a token was lexed externally, e.g. while processing a
830 c_pp_stream_token (cpp_reader
*pfile
, const cpp_token
*tok
, location_t loc
)
832 gcc_assert (print
.streamer
);
833 print
.streamer
->stream (pfile
, tok
, loc
);
836 /* Dump out the hash table. */
838 dump_macro (cpp_reader
*pfile
, cpp_hashnode
*node
, void *v ATTRIBUTE_UNUSED
)
840 if (cpp_user_macro_p (node
))
842 fputs ("#define ", print
.outf
);
843 fputs ((const char *) cpp_macro_definition (pfile
, node
),
845 putc ('\n', print
.outf
);
846 print
.printed
= false;
853 /* Load in the PCH file NAME, open on FD. It was originally searched for
854 by ORIG_NAME. Also, print out a #include command so that the PCH
855 file can be loaded when the preprocessed output is compiled. */
858 cb_read_pch (cpp_reader
*pfile
, const char *name
,
859 int fd
, const char *orig_name ATTRIBUTE_UNUSED
)
861 c_common_read_pch (pfile
, name
, fd
, orig_name
);
863 fprintf (print
.outf
, "#pragma GCC pch_preprocess \"%s\"\n", name
);