1*38fd1498Szrj /* Preprocess only, using cpplib.
2*38fd1498Szrj Copyright (C) 1995-2018 Free Software Foundation, Inc.
3*38fd1498Szrj Written by Per Bothner, 1994-95.
4*38fd1498Szrj
5*38fd1498Szrj This program is free software; you can redistribute it and/or modify it
6*38fd1498Szrj under the terms of the GNU General Public License as published by the
7*38fd1498Szrj Free Software Foundation; either version 3, or (at your option) any
8*38fd1498Szrj later version.
9*38fd1498Szrj
10*38fd1498Szrj This program is distributed in the hope that it will be useful,
11*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
12*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*38fd1498Szrj GNU General Public License for more details.
14*38fd1498Szrj
15*38fd1498Szrj You should have received a copy of the GNU General Public License
16*38fd1498Szrj along with this program; see the file COPYING3. If not see
17*38fd1498Szrj <http://www.gnu.org/licenses/>. */
18*38fd1498Szrj
19*38fd1498Szrj #include "config.h"
20*38fd1498Szrj #include "system.h"
21*38fd1498Szrj #include "coretypes.h"
22*38fd1498Szrj #include "c-common.h" /* For flags. */
23*38fd1498Szrj #include "../libcpp/internal.h"
24*38fd1498Szrj #include "c-pragma.h" /* For parse_in. */
25*38fd1498Szrj #include "file-prefix-map.h" /* remap_macro_filename() */
26*38fd1498Szrj
27*38fd1498Szrj /* Encapsulates state used to convert a stream of tokens into a text
28*38fd1498Szrj file. */
29*38fd1498Szrj static struct
30*38fd1498Szrj {
31*38fd1498Szrj FILE *outf; /* Stream to write to. */
32*38fd1498Szrj const cpp_token *prev; /* Previous token. */
33*38fd1498Szrj const cpp_token *source; /* Source token for spacing. */
34*38fd1498Szrj int src_line; /* Line number currently being written. */
35*38fd1498Szrj bool printed; /* True if something output at line. */
36*38fd1498Szrj bool first_time; /* pp_file_change hasn't been called yet. */
37*38fd1498Szrj bool prev_was_system_token; /* True if the previous token was a
38*38fd1498Szrj system token.*/
39*38fd1498Szrj const char *src_file; /* Current source file. */
40*38fd1498Szrj } print;
41*38fd1498Szrj
42*38fd1498Szrj /* Defined and undefined macros being queued for output with -dU at
43*38fd1498Szrj the next newline. */
44*38fd1498Szrj struct macro_queue
45*38fd1498Szrj {
46*38fd1498Szrj struct macro_queue *next; /* Next macro in the list. */
47*38fd1498Szrj char *macro; /* The name of the macro if not
48*38fd1498Szrj defined, the full definition if
49*38fd1498Szrj defined. */
50*38fd1498Szrj };
51*38fd1498Szrj static macro_queue *define_queue, *undef_queue;
52*38fd1498Szrj
53*38fd1498Szrj /* General output routines. */
54*38fd1498Szrj static void scan_translation_unit (cpp_reader *);
55*38fd1498Szrj static void print_lines_directives_only (int, const void *, size_t);
56*38fd1498Szrj static void scan_translation_unit_directives_only (cpp_reader *);
57*38fd1498Szrj static void scan_translation_unit_trad (cpp_reader *);
58*38fd1498Szrj static void account_for_newlines (const unsigned char *, size_t);
59*38fd1498Szrj static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
60*38fd1498Szrj static void dump_queued_macros (cpp_reader *);
61*38fd1498Szrj
62*38fd1498Szrj static bool print_line_1 (source_location, const char*, FILE *);
63*38fd1498Szrj static bool print_line (source_location, const char *);
64*38fd1498Szrj static bool maybe_print_line_1 (source_location, FILE *);
65*38fd1498Szrj static bool maybe_print_line (source_location);
66*38fd1498Szrj static bool do_line_change (cpp_reader *, const cpp_token *,
67*38fd1498Szrj source_location, int);
68*38fd1498Szrj
69*38fd1498Szrj /* Callback routines for the parser. Most of these are active only
70*38fd1498Szrj in specific modes. */
71*38fd1498Szrj static void cb_line_change (cpp_reader *, const cpp_token *, int);
72*38fd1498Szrj static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
73*38fd1498Szrj static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
74*38fd1498Szrj static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
75*38fd1498Szrj static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
76*38fd1498Szrj static void cb_include (cpp_reader *, source_location, const unsigned char *,
77*38fd1498Szrj const char *, int, const cpp_token **);
78*38fd1498Szrj static void cb_ident (cpp_reader *, source_location, const cpp_string *);
79*38fd1498Szrj static void cb_def_pragma (cpp_reader *, source_location);
80*38fd1498Szrj static void cb_read_pch (cpp_reader *pfile, const char *name,
81*38fd1498Szrj int fd, const char *orig_name);
82*38fd1498Szrj
83*38fd1498Szrj /* Preprocess and output. */
84*38fd1498Szrj void
preprocess_file(cpp_reader * pfile)85*38fd1498Szrj preprocess_file (cpp_reader *pfile)
86*38fd1498Szrj {
87*38fd1498Szrj /* A successful cpp_read_main_file guarantees that we can call
88*38fd1498Szrj cpp_scan_nooutput or cpp_get_token next. */
89*38fd1498Szrj if (flag_no_output && pfile->buffer)
90*38fd1498Szrj {
91*38fd1498Szrj /* Scan -included buffers, then the main file. */
92*38fd1498Szrj while (pfile->buffer->prev)
93*38fd1498Szrj cpp_scan_nooutput (pfile);
94*38fd1498Szrj cpp_scan_nooutput (pfile);
95*38fd1498Szrj }
96*38fd1498Szrj else if (cpp_get_options (pfile)->traditional)
97*38fd1498Szrj scan_translation_unit_trad (pfile);
98*38fd1498Szrj else if (cpp_get_options (pfile)->directives_only
99*38fd1498Szrj && !cpp_get_options (pfile)->preprocessed)
100*38fd1498Szrj scan_translation_unit_directives_only (pfile);
101*38fd1498Szrj else
102*38fd1498Szrj scan_translation_unit (pfile);
103*38fd1498Szrj
104*38fd1498Szrj /* -dM command line option. Should this be elsewhere? */
105*38fd1498Szrj if (flag_dump_macros == 'M')
106*38fd1498Szrj cpp_forall_identifiers (pfile, dump_macro, NULL);
107*38fd1498Szrj
108*38fd1498Szrj /* Flush any pending output. */
109*38fd1498Szrj if (print.printed)
110*38fd1498Szrj putc ('\n', print.outf);
111*38fd1498Szrj }
112*38fd1498Szrj
113*38fd1498Szrj /* Set up the callbacks as appropriate. */
114*38fd1498Szrj void
init_pp_output(FILE * out_stream)115*38fd1498Szrj init_pp_output (FILE *out_stream)
116*38fd1498Szrj {
117*38fd1498Szrj cpp_callbacks *cb = cpp_get_callbacks (parse_in);
118*38fd1498Szrj
119*38fd1498Szrj if (!flag_no_output)
120*38fd1498Szrj {
121*38fd1498Szrj cb->line_change = cb_line_change;
122*38fd1498Szrj /* Don't emit #pragma or #ident directives if we are processing
123*38fd1498Szrj assembly language; the assembler may choke on them. */
124*38fd1498Szrj if (cpp_get_options (parse_in)->lang != CLK_ASM)
125*38fd1498Szrj {
126*38fd1498Szrj cb->ident = cb_ident;
127*38fd1498Szrj cb->def_pragma = cb_def_pragma;
128*38fd1498Szrj }
129*38fd1498Szrj }
130*38fd1498Szrj
131*38fd1498Szrj if (flag_dump_includes)
132*38fd1498Szrj cb->include = cb_include;
133*38fd1498Szrj
134*38fd1498Szrj if (flag_pch_preprocess)
135*38fd1498Szrj {
136*38fd1498Szrj cb->valid_pch = c_common_valid_pch;
137*38fd1498Szrj cb->read_pch = cb_read_pch;
138*38fd1498Szrj }
139*38fd1498Szrj
140*38fd1498Szrj if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
141*38fd1498Szrj {
142*38fd1498Szrj cb->define = cb_define;
143*38fd1498Szrj cb->undef = cb_undef;
144*38fd1498Szrj }
145*38fd1498Szrj
146*38fd1498Szrj if (flag_dump_macros == 'U')
147*38fd1498Szrj {
148*38fd1498Szrj cb->before_define = dump_queued_macros;
149*38fd1498Szrj cb->used_define = cb_used_define;
150*38fd1498Szrj cb->used_undef = cb_used_undef;
151*38fd1498Szrj }
152*38fd1498Szrj
153*38fd1498Szrj cb->has_attribute = c_common_has_attribute;
154*38fd1498Szrj cb->get_source_date_epoch = cb_get_source_date_epoch;
155*38fd1498Szrj cb->remap_filename = remap_macro_filename;
156*38fd1498Szrj
157*38fd1498Szrj /* Initialize the print structure. */
158*38fd1498Szrj print.src_line = 1;
159*38fd1498Szrj print.printed = false;
160*38fd1498Szrj print.prev = 0;
161*38fd1498Szrj print.outf = out_stream;
162*38fd1498Szrj print.first_time = 1;
163*38fd1498Szrj print.src_file = "";
164*38fd1498Szrj print.prev_was_system_token = false;
165*38fd1498Szrj }
166*38fd1498Szrj
167*38fd1498Szrj /* Writes out the preprocessed file, handling spacing and paste
168*38fd1498Szrj avoidance issues. */
169*38fd1498Szrj static void
scan_translation_unit(cpp_reader * pfile)170*38fd1498Szrj scan_translation_unit (cpp_reader *pfile)
171*38fd1498Szrj {
172*38fd1498Szrj bool avoid_paste = false;
173*38fd1498Szrj bool do_line_adjustments
174*38fd1498Szrj = cpp_get_options (parse_in)->lang != CLK_ASM
175*38fd1498Szrj && !flag_no_line_commands;
176*38fd1498Szrj bool in_pragma = false;
177*38fd1498Szrj bool line_marker_emitted = false;
178*38fd1498Szrj
179*38fd1498Szrj print.source = NULL;
180*38fd1498Szrj for (;;)
181*38fd1498Szrj {
182*38fd1498Szrj source_location loc;
183*38fd1498Szrj const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
184*38fd1498Szrj
185*38fd1498Szrj if (token->type == CPP_PADDING)
186*38fd1498Szrj {
187*38fd1498Szrj avoid_paste = true;
188*38fd1498Szrj if (print.source == NULL
189*38fd1498Szrj || (!(print.source->flags & PREV_WHITE)
190*38fd1498Szrj && token->val.source == NULL))
191*38fd1498Szrj print.source = token->val.source;
192*38fd1498Szrj continue;
193*38fd1498Szrj }
194*38fd1498Szrj
195*38fd1498Szrj if (token->type == CPP_EOF)
196*38fd1498Szrj break;
197*38fd1498Szrj
198*38fd1498Szrj /* Subtle logic to output a space if and only if necessary. */
199*38fd1498Szrj if (avoid_paste)
200*38fd1498Szrj {
201*38fd1498Szrj int src_line = LOCATION_LINE (loc);
202*38fd1498Szrj
203*38fd1498Szrj if (print.source == NULL)
204*38fd1498Szrj print.source = token;
205*38fd1498Szrj
206*38fd1498Szrj if (src_line != print.src_line
207*38fd1498Szrj && do_line_adjustments
208*38fd1498Szrj && !in_pragma)
209*38fd1498Szrj {
210*38fd1498Szrj line_marker_emitted = do_line_change (pfile, token, loc, false);
211*38fd1498Szrj putc (' ', print.outf);
212*38fd1498Szrj print.printed = true;
213*38fd1498Szrj }
214*38fd1498Szrj else if (print.source->flags & PREV_WHITE
215*38fd1498Szrj || (print.prev
216*38fd1498Szrj && cpp_avoid_paste (pfile, print.prev, token))
217*38fd1498Szrj || (print.prev == NULL && token->type == CPP_HASH))
218*38fd1498Szrj {
219*38fd1498Szrj putc (' ', print.outf);
220*38fd1498Szrj print.printed = true;
221*38fd1498Szrj }
222*38fd1498Szrj }
223*38fd1498Szrj else if (token->flags & PREV_WHITE)
224*38fd1498Szrj {
225*38fd1498Szrj int src_line = LOCATION_LINE (loc);
226*38fd1498Szrj
227*38fd1498Szrj if (src_line != print.src_line
228*38fd1498Szrj && do_line_adjustments
229*38fd1498Szrj && !in_pragma)
230*38fd1498Szrj line_marker_emitted = do_line_change (pfile, token, loc, false);
231*38fd1498Szrj putc (' ', print.outf);
232*38fd1498Szrj print.printed = true;
233*38fd1498Szrj }
234*38fd1498Szrj
235*38fd1498Szrj avoid_paste = false;
236*38fd1498Szrj print.source = NULL;
237*38fd1498Szrj print.prev = token;
238*38fd1498Szrj if (token->type == CPP_PRAGMA)
239*38fd1498Szrj {
240*38fd1498Szrj const char *space;
241*38fd1498Szrj const char *name;
242*38fd1498Szrj
243*38fd1498Szrj line_marker_emitted = maybe_print_line (token->src_loc);
244*38fd1498Szrj fputs ("#pragma ", print.outf);
245*38fd1498Szrj c_pp_lookup_pragma (token->val.pragma, &space, &name);
246*38fd1498Szrj if (space)
247*38fd1498Szrj fprintf (print.outf, "%s %s", space, name);
248*38fd1498Szrj else
249*38fd1498Szrj fprintf (print.outf, "%s", name);
250*38fd1498Szrj print.printed = true;
251*38fd1498Szrj in_pragma = true;
252*38fd1498Szrj }
253*38fd1498Szrj else if (token->type == CPP_PRAGMA_EOL)
254*38fd1498Szrj {
255*38fd1498Szrj maybe_print_line (token->src_loc);
256*38fd1498Szrj in_pragma = false;
257*38fd1498Szrj }
258*38fd1498Szrj else
259*38fd1498Szrj {
260*38fd1498Szrj if (cpp_get_options (parse_in)->debug)
261*38fd1498Szrj linemap_dump_location (line_table, token->src_loc, print.outf);
262*38fd1498Szrj
263*38fd1498Szrj if (do_line_adjustments
264*38fd1498Szrj && !in_pragma
265*38fd1498Szrj && !line_marker_emitted
266*38fd1498Szrj && print.prev_was_system_token != !!in_system_header_at (loc)
267*38fd1498Szrj && !is_location_from_builtin_token (loc))
268*38fd1498Szrj /* The system-ness of this token is different from the one
269*38fd1498Szrj of the previous token. Let's emit a line change to
270*38fd1498Szrj mark the new system-ness before we emit the token. */
271*38fd1498Szrj {
272*38fd1498Szrj do_line_change (pfile, token, loc, false);
273*38fd1498Szrj print.prev_was_system_token = !!in_system_header_at (loc);
274*38fd1498Szrj }
275*38fd1498Szrj cpp_output_token (token, print.outf);
276*38fd1498Szrj line_marker_emitted = false;
277*38fd1498Szrj print.printed = true;
278*38fd1498Szrj }
279*38fd1498Szrj
280*38fd1498Szrj /* CPP_COMMENT tokens and raw-string literal tokens can
281*38fd1498Szrj have embedded new-line characters. Rather than enumerating
282*38fd1498Szrj all the possible token types just check if token uses
283*38fd1498Szrj val.str union member. */
284*38fd1498Szrj if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
285*38fd1498Szrj account_for_newlines (token->val.str.text, token->val.str.len);
286*38fd1498Szrj }
287*38fd1498Szrj }
288*38fd1498Szrj
289*38fd1498Szrj static void
print_lines_directives_only(int lines,const void * buf,size_t size)290*38fd1498Szrj print_lines_directives_only (int lines, const void *buf, size_t size)
291*38fd1498Szrj {
292*38fd1498Szrj print.src_line += lines;
293*38fd1498Szrj fwrite (buf, 1, size, print.outf);
294*38fd1498Szrj }
295*38fd1498Szrj
296*38fd1498Szrj /* Writes out the preprocessed file, handling spacing and paste
297*38fd1498Szrj avoidance issues. */
298*38fd1498Szrj static void
scan_translation_unit_directives_only(cpp_reader * pfile)299*38fd1498Szrj scan_translation_unit_directives_only (cpp_reader *pfile)
300*38fd1498Szrj {
301*38fd1498Szrj struct _cpp_dir_only_callbacks cb;
302*38fd1498Szrj
303*38fd1498Szrj cb.print_lines = print_lines_directives_only;
304*38fd1498Szrj cb.maybe_print_line = maybe_print_line;
305*38fd1498Szrj
306*38fd1498Szrj _cpp_preprocess_dir_only (pfile, &cb);
307*38fd1498Szrj }
308*38fd1498Szrj
309*38fd1498Szrj /* Adjust print.src_line for newlines embedded in output. */
310*38fd1498Szrj static void
account_for_newlines(const unsigned char * str,size_t len)311*38fd1498Szrj account_for_newlines (const unsigned char *str, size_t len)
312*38fd1498Szrj {
313*38fd1498Szrj while (len--)
314*38fd1498Szrj if (*str++ == '\n')
315*38fd1498Szrj print.src_line++;
316*38fd1498Szrj }
317*38fd1498Szrj
318*38fd1498Szrj /* Writes out a traditionally preprocessed file. */
319*38fd1498Szrj static void
scan_translation_unit_trad(cpp_reader * pfile)320*38fd1498Szrj scan_translation_unit_trad (cpp_reader *pfile)
321*38fd1498Szrj {
322*38fd1498Szrj while (_cpp_read_logical_line_trad (pfile))
323*38fd1498Szrj {
324*38fd1498Szrj size_t len = pfile->out.cur - pfile->out.base;
325*38fd1498Szrj maybe_print_line (pfile->out.first_line);
326*38fd1498Szrj fwrite (pfile->out.base, 1, len, print.outf);
327*38fd1498Szrj print.printed = true;
328*38fd1498Szrj if (!CPP_OPTION (pfile, discard_comments))
329*38fd1498Szrj account_for_newlines (pfile->out.base, len);
330*38fd1498Szrj }
331*38fd1498Szrj }
332*38fd1498Szrj
333*38fd1498Szrj /* If the token read on logical line LINE needs to be output on a
334*38fd1498Szrj different line to the current one, output the required newlines or
335*38fd1498Szrj a line marker. If a line marker was emitted, return TRUE otherwise
336*38fd1498Szrj return FALSE. */
337*38fd1498Szrj
338*38fd1498Szrj static bool
maybe_print_line_1(source_location src_loc,FILE * stream)339*38fd1498Szrj maybe_print_line_1 (source_location src_loc, FILE *stream)
340*38fd1498Szrj {
341*38fd1498Szrj bool emitted_line_marker = false;
342*38fd1498Szrj int src_line = LOCATION_LINE (src_loc);
343*38fd1498Szrj const char *src_file = LOCATION_FILE (src_loc);
344*38fd1498Szrj
345*38fd1498Szrj /* End the previous line of text. */
346*38fd1498Szrj if (print.printed)
347*38fd1498Szrj {
348*38fd1498Szrj putc ('\n', stream);
349*38fd1498Szrj print.src_line++;
350*38fd1498Szrj print.printed = false;
351*38fd1498Szrj }
352*38fd1498Szrj
353*38fd1498Szrj if (!flag_no_line_commands
354*38fd1498Szrj && src_line >= print.src_line
355*38fd1498Szrj && src_line < print.src_line + 8
356*38fd1498Szrj && strcmp (src_file, print.src_file) == 0)
357*38fd1498Szrj {
358*38fd1498Szrj while (src_line > print.src_line)
359*38fd1498Szrj {
360*38fd1498Szrj putc ('\n', stream);
361*38fd1498Szrj print.src_line++;
362*38fd1498Szrj }
363*38fd1498Szrj }
364*38fd1498Szrj else
365*38fd1498Szrj emitted_line_marker = print_line_1 (src_loc, "", stream);
366*38fd1498Szrj
367*38fd1498Szrj return emitted_line_marker;
368*38fd1498Szrj }
369*38fd1498Szrj
370*38fd1498Szrj /* If the token read on logical line LINE needs to be output on a
371*38fd1498Szrj different line to the current one, output the required newlines or
372*38fd1498Szrj a line marker. If a line marker was emitted, return TRUE otherwise
373*38fd1498Szrj return FALSE. */
374*38fd1498Szrj
375*38fd1498Szrj static bool
maybe_print_line(source_location src_loc)376*38fd1498Szrj maybe_print_line (source_location src_loc)
377*38fd1498Szrj {
378*38fd1498Szrj if (cpp_get_options (parse_in)->debug)
379*38fd1498Szrj linemap_dump_location (line_table, src_loc,
380*38fd1498Szrj print.outf);
381*38fd1498Szrj return maybe_print_line_1 (src_loc, print.outf);
382*38fd1498Szrj }
383*38fd1498Szrj
384*38fd1498Szrj /* Output a line marker for logical line LINE. Special flags are "1"
385*38fd1498Szrj or "2" indicating entering or leaving a file. If the line marker
386*38fd1498Szrj was effectively emitted, return TRUE otherwise return FALSE. */
387*38fd1498Szrj
388*38fd1498Szrj static bool
print_line_1(source_location src_loc,const char * special_flags,FILE * stream)389*38fd1498Szrj print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
390*38fd1498Szrj {
391*38fd1498Szrj bool emitted_line_marker = false;
392*38fd1498Szrj
393*38fd1498Szrj /* End any previous line of text. */
394*38fd1498Szrj if (print.printed)
395*38fd1498Szrj putc ('\n', stream);
396*38fd1498Szrj print.printed = false;
397*38fd1498Szrj
398*38fd1498Szrj if (!flag_no_line_commands)
399*38fd1498Szrj {
400*38fd1498Szrj const char *file_path = LOCATION_FILE (src_loc);
401*38fd1498Szrj int sysp;
402*38fd1498Szrj size_t to_file_len = strlen (file_path);
403*38fd1498Szrj unsigned char *to_file_quoted =
404*38fd1498Szrj (unsigned char *) alloca (to_file_len * 4 + 1);
405*38fd1498Szrj unsigned char *p;
406*38fd1498Szrj
407*38fd1498Szrj print.src_line = LOCATION_LINE (src_loc);
408*38fd1498Szrj print.src_file = file_path;
409*38fd1498Szrj
410*38fd1498Szrj /* cpp_quote_string does not nul-terminate, so we have to do it
411*38fd1498Szrj ourselves. */
412*38fd1498Szrj p = cpp_quote_string (to_file_quoted,
413*38fd1498Szrj (const unsigned char *) file_path,
414*38fd1498Szrj to_file_len);
415*38fd1498Szrj *p = '\0';
416*38fd1498Szrj fprintf (stream, "# %u \"%s\"%s",
417*38fd1498Szrj print.src_line == 0 ? 1 : print.src_line,
418*38fd1498Szrj to_file_quoted, special_flags);
419*38fd1498Szrj
420*38fd1498Szrj sysp = in_system_header_at (src_loc);
421*38fd1498Szrj if (sysp == 2)
422*38fd1498Szrj fputs (" 3 4", stream);
423*38fd1498Szrj else if (sysp == 1)
424*38fd1498Szrj fputs (" 3", stream);
425*38fd1498Szrj
426*38fd1498Szrj putc ('\n', stream);
427*38fd1498Szrj emitted_line_marker = true;
428*38fd1498Szrj }
429*38fd1498Szrj
430*38fd1498Szrj return emitted_line_marker;
431*38fd1498Szrj }
432*38fd1498Szrj
433*38fd1498Szrj /* Output a line marker for logical line LINE. Special flags are "1"
434*38fd1498Szrj or "2" indicating entering or leaving a file. Return TRUE if a
435*38fd1498Szrj line marker was effectively emitted, FALSE otherwise. */
436*38fd1498Szrj
437*38fd1498Szrj static bool
print_line(source_location src_loc,const char * special_flags)438*38fd1498Szrj print_line (source_location src_loc, const char *special_flags)
439*38fd1498Szrj {
440*38fd1498Szrj if (cpp_get_options (parse_in)->debug)
441*38fd1498Szrj linemap_dump_location (line_table, src_loc,
442*38fd1498Szrj print.outf);
443*38fd1498Szrj return print_line_1 (src_loc, special_flags, print.outf);
444*38fd1498Szrj }
445*38fd1498Szrj
446*38fd1498Szrj /* Helper function for cb_line_change and scan_translation_unit.
447*38fd1498Szrj Return TRUE if a line marker is emitted, FALSE otherwise. */
448*38fd1498Szrj static bool
do_line_change(cpp_reader * pfile,const cpp_token * token,source_location src_loc,int parsing_args)449*38fd1498Szrj do_line_change (cpp_reader *pfile, const cpp_token *token,
450*38fd1498Szrj source_location src_loc, int parsing_args)
451*38fd1498Szrj {
452*38fd1498Szrj bool emitted_line_marker = false;
453*38fd1498Szrj if (define_queue || undef_queue)
454*38fd1498Szrj dump_queued_macros (pfile);
455*38fd1498Szrj
456*38fd1498Szrj if (token->type == CPP_EOF || parsing_args)
457*38fd1498Szrj return false;
458*38fd1498Szrj
459*38fd1498Szrj emitted_line_marker = maybe_print_line (src_loc);
460*38fd1498Szrj print.prev = 0;
461*38fd1498Szrj print.source = 0;
462*38fd1498Szrj
463*38fd1498Szrj /* Supply enough spaces to put this token in its original column,
464*38fd1498Szrj one space per column greater than 2, since scan_translation_unit
465*38fd1498Szrj will provide a space if PREV_WHITE. Don't bother trying to
466*38fd1498Szrj reconstruct tabs; we can't get it right in general, and nothing
467*38fd1498Szrj ought to care. Some things do care; the fault lies with them. */
468*38fd1498Szrj if (!CPP_OPTION (pfile, traditional))
469*38fd1498Szrj {
470*38fd1498Szrj int spaces = LOCATION_COLUMN (src_loc) - 2;
471*38fd1498Szrj print.printed = true;
472*38fd1498Szrj
473*38fd1498Szrj while (-- spaces >= 0)
474*38fd1498Szrj putc (' ', print.outf);
475*38fd1498Szrj }
476*38fd1498Szrj
477*38fd1498Szrj return emitted_line_marker;
478*38fd1498Szrj }
479*38fd1498Szrj
480*38fd1498Szrj /* Called when a line of output is started. TOKEN is the first token
481*38fd1498Szrj of the line, and at end of file will be CPP_EOF. */
482*38fd1498Szrj static void
cb_line_change(cpp_reader * pfile,const cpp_token * token,int parsing_args)483*38fd1498Szrj cb_line_change (cpp_reader *pfile, const cpp_token *token,
484*38fd1498Szrj int parsing_args)
485*38fd1498Szrj {
486*38fd1498Szrj do_line_change (pfile, token, token->src_loc, parsing_args);
487*38fd1498Szrj }
488*38fd1498Szrj
489*38fd1498Szrj static void
cb_ident(cpp_reader * pfile ATTRIBUTE_UNUSED,source_location line,const cpp_string * str)490*38fd1498Szrj cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
491*38fd1498Szrj const cpp_string *str)
492*38fd1498Szrj {
493*38fd1498Szrj maybe_print_line (line);
494*38fd1498Szrj fprintf (print.outf, "#ident %s\n", str->text);
495*38fd1498Szrj print.src_line++;
496*38fd1498Szrj }
497*38fd1498Szrj
498*38fd1498Szrj static void
cb_define(cpp_reader * pfile,source_location line,cpp_hashnode * node)499*38fd1498Szrj cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
500*38fd1498Szrj {
501*38fd1498Szrj const line_map_ordinary *map;
502*38fd1498Szrj
503*38fd1498Szrj maybe_print_line (line);
504*38fd1498Szrj fputs ("#define ", print.outf);
505*38fd1498Szrj
506*38fd1498Szrj /* 'D' is whole definition; 'N' is name only. */
507*38fd1498Szrj if (flag_dump_macros == 'D')
508*38fd1498Szrj fputs ((const char *) cpp_macro_definition (pfile, node),
509*38fd1498Szrj print.outf);
510*38fd1498Szrj else
511*38fd1498Szrj fputs ((const char *) NODE_NAME (node), print.outf);
512*38fd1498Szrj
513*38fd1498Szrj putc ('\n', print.outf);
514*38fd1498Szrj print.printed = false;
515*38fd1498Szrj linemap_resolve_location (line_table, line,
516*38fd1498Szrj LRK_MACRO_DEFINITION_LOCATION,
517*38fd1498Szrj &map);
518*38fd1498Szrj if (LINEMAP_LINE (map) != 0)
519*38fd1498Szrj print.src_line++;
520*38fd1498Szrj }
521*38fd1498Szrj
522*38fd1498Szrj static void
cb_undef(cpp_reader * pfile ATTRIBUTE_UNUSED,source_location line,cpp_hashnode * node)523*38fd1498Szrj cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
524*38fd1498Szrj cpp_hashnode *node)
525*38fd1498Szrj {
526*38fd1498Szrj maybe_print_line (line);
527*38fd1498Szrj fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
528*38fd1498Szrj print.src_line++;
529*38fd1498Szrj }
530*38fd1498Szrj
531*38fd1498Szrj static void
cb_used_define(cpp_reader * pfile,source_location line ATTRIBUTE_UNUSED,cpp_hashnode * node)532*38fd1498Szrj cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
533*38fd1498Szrj cpp_hashnode *node)
534*38fd1498Szrj {
535*38fd1498Szrj macro_queue *q;
536*38fd1498Szrj if (node->flags & NODE_BUILTIN)
537*38fd1498Szrj return;
538*38fd1498Szrj q = XNEW (macro_queue);
539*38fd1498Szrj q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
540*38fd1498Szrj q->next = define_queue;
541*38fd1498Szrj define_queue = q;
542*38fd1498Szrj }
543*38fd1498Szrj
544*38fd1498Szrj static void
cb_used_undef(cpp_reader * pfile ATTRIBUTE_UNUSED,source_location line ATTRIBUTE_UNUSED,cpp_hashnode * node)545*38fd1498Szrj cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
546*38fd1498Szrj source_location line ATTRIBUTE_UNUSED,
547*38fd1498Szrj cpp_hashnode *node)
548*38fd1498Szrj {
549*38fd1498Szrj macro_queue *q;
550*38fd1498Szrj q = XNEW (macro_queue);
551*38fd1498Szrj q->macro = xstrdup ((const char *) NODE_NAME (node));
552*38fd1498Szrj q->next = undef_queue;
553*38fd1498Szrj undef_queue = q;
554*38fd1498Szrj }
555*38fd1498Szrj
556*38fd1498Szrj static void
dump_queued_macros(cpp_reader * pfile ATTRIBUTE_UNUSED)557*38fd1498Szrj dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
558*38fd1498Szrj {
559*38fd1498Szrj macro_queue *q;
560*38fd1498Szrj
561*38fd1498Szrj /* End the previous line of text. */
562*38fd1498Szrj if (print.printed)
563*38fd1498Szrj {
564*38fd1498Szrj putc ('\n', print.outf);
565*38fd1498Szrj print.src_line++;
566*38fd1498Szrj print.printed = false;
567*38fd1498Szrj }
568*38fd1498Szrj
569*38fd1498Szrj for (q = define_queue; q;)
570*38fd1498Szrj {
571*38fd1498Szrj macro_queue *oq;
572*38fd1498Szrj fputs ("#define ", print.outf);
573*38fd1498Szrj fputs (q->macro, print.outf);
574*38fd1498Szrj putc ('\n', print.outf);
575*38fd1498Szrj print.printed = false;
576*38fd1498Szrj print.src_line++;
577*38fd1498Szrj oq = q;
578*38fd1498Szrj q = q->next;
579*38fd1498Szrj free (oq->macro);
580*38fd1498Szrj free (oq);
581*38fd1498Szrj }
582*38fd1498Szrj define_queue = NULL;
583*38fd1498Szrj for (q = undef_queue; q;)
584*38fd1498Szrj {
585*38fd1498Szrj macro_queue *oq;
586*38fd1498Szrj fprintf (print.outf, "#undef %s\n", q->macro);
587*38fd1498Szrj print.src_line++;
588*38fd1498Szrj oq = q;
589*38fd1498Szrj q = q->next;
590*38fd1498Szrj free (oq->macro);
591*38fd1498Szrj free (oq);
592*38fd1498Szrj }
593*38fd1498Szrj undef_queue = NULL;
594*38fd1498Szrj }
595*38fd1498Szrj
596*38fd1498Szrj static void
cb_include(cpp_reader * pfile ATTRIBUTE_UNUSED,source_location line,const unsigned char * dir,const char * header,int angle_brackets,const cpp_token ** comments)597*38fd1498Szrj cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
598*38fd1498Szrj const unsigned char *dir, const char *header, int angle_brackets,
599*38fd1498Szrj const cpp_token **comments)
600*38fd1498Szrj {
601*38fd1498Szrj maybe_print_line (line);
602*38fd1498Szrj if (angle_brackets)
603*38fd1498Szrj fprintf (print.outf, "#%s <%s>", dir, header);
604*38fd1498Szrj else
605*38fd1498Szrj fprintf (print.outf, "#%s \"%s\"", dir, header);
606*38fd1498Szrj
607*38fd1498Szrj if (comments != NULL)
608*38fd1498Szrj {
609*38fd1498Szrj while (*comments != NULL)
610*38fd1498Szrj {
611*38fd1498Szrj if ((*comments)->flags & PREV_WHITE)
612*38fd1498Szrj putc (' ', print.outf);
613*38fd1498Szrj cpp_output_token (*comments, print.outf);
614*38fd1498Szrj ++comments;
615*38fd1498Szrj }
616*38fd1498Szrj }
617*38fd1498Szrj
618*38fd1498Szrj putc ('\n', print.outf);
619*38fd1498Szrj print.printed = false;
620*38fd1498Szrj print.src_line++;
621*38fd1498Szrj }
622*38fd1498Szrj
623*38fd1498Szrj /* Callback called when -fworking-director and -E to emit working
624*38fd1498Szrj directory in cpp output file. */
625*38fd1498Szrj
626*38fd1498Szrj void
pp_dir_change(cpp_reader * pfile ATTRIBUTE_UNUSED,const char * dir)627*38fd1498Szrj pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
628*38fd1498Szrj {
629*38fd1498Szrj size_t to_file_len = strlen (dir);
630*38fd1498Szrj unsigned char *to_file_quoted =
631*38fd1498Szrj (unsigned char *) alloca (to_file_len * 4 + 1);
632*38fd1498Szrj unsigned char *p;
633*38fd1498Szrj
634*38fd1498Szrj /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
635*38fd1498Szrj p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
636*38fd1498Szrj *p = '\0';
637*38fd1498Szrj fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
638*38fd1498Szrj }
639*38fd1498Szrj
640*38fd1498Szrj /* The file name, line number or system header flags have changed, as
641*38fd1498Szrj described in MAP. */
642*38fd1498Szrj
643*38fd1498Szrj void
pp_file_change(const line_map_ordinary * map)644*38fd1498Szrj pp_file_change (const line_map_ordinary *map)
645*38fd1498Szrj {
646*38fd1498Szrj const char *flags = "";
647*38fd1498Szrj
648*38fd1498Szrj if (flag_no_line_commands)
649*38fd1498Szrj return;
650*38fd1498Szrj
651*38fd1498Szrj if (map != NULL)
652*38fd1498Szrj {
653*38fd1498Szrj input_location = map->start_location;
654*38fd1498Szrj if (print.first_time)
655*38fd1498Szrj {
656*38fd1498Szrj /* Avoid printing foo.i when the main file is foo.c. */
657*38fd1498Szrj if (!cpp_get_options (parse_in)->preprocessed)
658*38fd1498Szrj print_line (map->start_location, flags);
659*38fd1498Szrj print.first_time = 0;
660*38fd1498Szrj }
661*38fd1498Szrj else
662*38fd1498Szrj {
663*38fd1498Szrj /* Bring current file to correct line when entering a new file. */
664*38fd1498Szrj if (map->reason == LC_ENTER)
665*38fd1498Szrj {
666*38fd1498Szrj const line_map_ordinary *from = INCLUDED_FROM (line_table, map);
667*38fd1498Szrj maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
668*38fd1498Szrj }
669*38fd1498Szrj if (map->reason == LC_ENTER)
670*38fd1498Szrj flags = " 1";
671*38fd1498Szrj else if (map->reason == LC_LEAVE)
672*38fd1498Szrj flags = " 2";
673*38fd1498Szrj print_line (map->start_location, flags);
674*38fd1498Szrj }
675*38fd1498Szrj }
676*38fd1498Szrj }
677*38fd1498Szrj
678*38fd1498Szrj /* Copy a #pragma directive to the preprocessed output. */
679*38fd1498Szrj static void
cb_def_pragma(cpp_reader * pfile,source_location line)680*38fd1498Szrj cb_def_pragma (cpp_reader *pfile, source_location line)
681*38fd1498Szrj {
682*38fd1498Szrj maybe_print_line (line);
683*38fd1498Szrj fputs ("#pragma ", print.outf);
684*38fd1498Szrj cpp_output_line (pfile, print.outf);
685*38fd1498Szrj print.printed = false;
686*38fd1498Szrj print.src_line++;
687*38fd1498Szrj }
688*38fd1498Szrj
689*38fd1498Szrj /* Dump out the hash table. */
690*38fd1498Szrj static int
dump_macro(cpp_reader * pfile,cpp_hashnode * node,void * v ATTRIBUTE_UNUSED)691*38fd1498Szrj dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
692*38fd1498Szrj {
693*38fd1498Szrj if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
694*38fd1498Szrj {
695*38fd1498Szrj fputs ("#define ", print.outf);
696*38fd1498Szrj fputs ((const char *) cpp_macro_definition (pfile, node),
697*38fd1498Szrj print.outf);
698*38fd1498Szrj putc ('\n', print.outf);
699*38fd1498Szrj print.printed = false;
700*38fd1498Szrj print.src_line++;
701*38fd1498Szrj }
702*38fd1498Szrj
703*38fd1498Szrj return 1;
704*38fd1498Szrj }
705*38fd1498Szrj
706*38fd1498Szrj /* Load in the PCH file NAME, open on FD. It was originally searched for
707*38fd1498Szrj by ORIG_NAME. Also, print out a #include command so that the PCH
708*38fd1498Szrj file can be loaded when the preprocessed output is compiled. */
709*38fd1498Szrj
710*38fd1498Szrj static void
cb_read_pch(cpp_reader * pfile,const char * name,int fd,const char * orig_name ATTRIBUTE_UNUSED)711*38fd1498Szrj cb_read_pch (cpp_reader *pfile, const char *name,
712*38fd1498Szrj int fd, const char *orig_name ATTRIBUTE_UNUSED)
713*38fd1498Szrj {
714*38fd1498Szrj c_common_read_pch (pfile, name, fd, orig_name);
715*38fd1498Szrj
716*38fd1498Szrj fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
717*38fd1498Szrj print.src_line++;
718*38fd1498Szrj }
719