]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-ppoutput.c
genattrtab.c (write_header): Include hash-set.h...
[thirdparty/gcc.git] / gcc / c-family / c-ppoutput.c
CommitLineData
aaf93206 1/* Preprocess only, using cpplib.
5624e564 2 Copyright (C) 1995-2015 Free Software Foundation, Inc.
7f2935c7
PB
3 Written by Per Bothner, 1994-95.
4
9dcd6f09
NC
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
8 later version.
7f2935c7 9
9dcd6f09
NC
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.
7f2935c7 14
9dcd6f09
NC
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/>. */
7f2935c7 18
84ee6fd4 19#include "config.h"
b04cd507 20#include "system.h"
4977bab6 21#include "coretypes.h"
b04cd507 22#include "cpplib.h"
4f4e53dd 23#include "../libcpp/internal.h"
40e23961
MC
24#include "hash-set.h"
25#include "machmode.h"
26#include "vec.h"
27#include "double-int.h"
28#include "input.h"
29#include "alias.h"
30#include "symtab.h"
31#include "options.h"
32#include "wide-int.h"
33#include "inchash.h"
63973df3 34#include "tree.h"
9d10c9a9
NB
35#include "c-common.h" /* For flags. */
36#include "c-pragma.h" /* For parse_in. */
63973df3
NB
37
38/* Encapsulates state used to convert a stream of tokens into a text
39 file. */
40static struct
41{
42 FILE *outf; /* Stream to write to. */
63973df3
NB
43 const cpp_token *prev; /* Previous token. */
44 const cpp_token *source; /* Source token for spacing. */
12f9df4e 45 int src_line; /* Line number currently being written. */
63973df3 46 unsigned char printed; /* Nonzero if something output at line. */
a1105617 47 bool first_time; /* pp_file_change hasn't been called yet. */
22d17b1c 48 const char *src_file; /* Current source file. */
f41373b6
DS
49 bool prev_was_system_token; /* True if the previous token was a
50 system token.*/
63973df3 51} print;
7f2935c7 52
93d45d9e
JM
53/* Defined and undefined macros being queued for output with -dU at
54 the next newline. */
55typedef struct macro_queue
56{
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
60 defined. */
61} macro_queue;
62static macro_queue *define_queue, *undef_queue;
63
93c80368 64/* General output routines. */
2f6e4e97 65static void scan_translation_unit (cpp_reader *);
ccfc4c91
OW
66static void print_lines_directives_only (int, const void *, size_t);
67static void scan_translation_unit_directives_only (cpp_reader *);
2f6e4e97 68static void scan_translation_unit_trad (cpp_reader *);
43839642 69static void account_for_newlines (const unsigned char *, size_t);
2f6e4e97 70static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
93d45d9e 71static void dump_queued_macros (cpp_reader *);
93c80368 72
f41373b6
DS
73static bool print_line_1 (source_location, const char*, FILE *);
74static bool print_line (source_location, const char *);
75static bool maybe_print_line_1 (source_location, FILE *);
76static bool maybe_print_line (source_location);
77static bool do_line_change (cpp_reader *, const cpp_token *,
21ece9b2 78 source_location, int);
93c80368 79
58fea6af
ZW
80/* Callback routines for the parser. Most of these are active only
81 in specific modes. */
2f6e4e97 82static void cb_line_change (cpp_reader *, const cpp_token *, int);
a281759f
PB
83static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
84static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
93d45d9e
JM
85static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
86static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
a281759f 87static void cb_include (cpp_reader *, source_location, const unsigned char *,
cbc43ae0 88 const char *, int, const cpp_token **);
a281759f
PB
89static void cb_ident (cpp_reader *, source_location, const cpp_string *);
90static void cb_def_pragma (cpp_reader *, source_location);
c0d578e6
GK
91static void cb_read_pch (cpp_reader *pfile, const char *name,
92 int fd, const char *orig_name);
93c80368 93
aaf93206
NB
94/* Preprocess and output. */
95void
2f6e4e97 96preprocess_file (cpp_reader *pfile)
7f2935c7 97{
9d10c9a9
NB
98 /* A successful cpp_read_main_file guarantees that we can call
99 cpp_scan_nooutput or cpp_get_token next. */
6f3a2e23 100 if (flag_no_output && pfile->buffer)
6de8f7fc 101 {
9d10c9a9
NB
102 /* Scan -included buffers, then the main file. */
103 while (pfile->buffer->prev)
104 cpp_scan_nooutput (pfile);
105 cpp_scan_nooutput (pfile);
6de8f7fc 106 }
9d10c9a9
NB
107 else if (cpp_get_options (pfile)->traditional)
108 scan_translation_unit_trad (pfile);
ccfc4c91
OW
109 else if (cpp_get_options (pfile)->directives_only
110 && !cpp_get_options (pfile)->preprocessed)
111 scan_translation_unit_directives_only (pfile);
9d10c9a9
NB
112 else
113 scan_translation_unit (pfile);
114
115 /* -dM command line option. Should this be elsewhere? */
116 if (flag_dump_macros == 'M')
117 cpp_forall_identifiers (pfile, dump_macro, NULL);
6de8f7fc
NB
118
119 /* Flush any pending output. */
63973df3
NB
120 if (print.printed)
121 putc ('\n', print.outf);
6de8f7fc
NB
122}
123
620ef26c 124/* Set up the callbacks as appropriate. */
9d10c9a9 125void
2f6e4e97 126init_pp_output (FILE *out_stream)
cf44ea52 127{
9d10c9a9
NB
128 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
129
63973df3 130 if (!flag_no_output)
cf44ea52 131 {
f0581dc7 132 cb->line_change = cb_line_change;
c7544dd8
ZW
133 /* Don't emit #pragma or #ident directives if we are processing
134 assembly language; the assembler may choke on them. */
9d10c9a9 135 if (cpp_get_options (parse_in)->lang != CLK_ASM)
c7544dd8
ZW
136 {
137 cb->ident = cb_ident;
138 cb->def_pragma = cb_def_pragma;
139 }
cf44ea52
NB
140 }
141
63973df3 142 if (flag_dump_includes)
620ef26c 143 cb->include = cb_include;
cf44ea52 144
c0d578e6
GK
145 if (flag_pch_preprocess)
146 {
147 cb->valid_pch = c_common_valid_pch;
148 cb->read_pch = cb_read_pch;
149 }
150
63973df3 151 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
cf44ea52 152 {
620ef26c
NB
153 cb->define = cb_define;
154 cb->undef = cb_undef;
cf44ea52 155 }
9d10c9a9 156
93d45d9e
JM
157 if (flag_dump_macros == 'U')
158 {
159 cb->before_define = dump_queued_macros;
160 cb->used_define = cb_used_define;
161 cb->used_undef = cb_used_undef;
162 }
163
1f8d3e84
JJ
164 cb->has_attribute = c_common_has_attribute;
165
1a456b49
NS
166 /* Initialize the print structure. */
167 print.src_line = 1;
9d10c9a9
NB
168 print.printed = 0;
169 print.prev = 0;
9d10c9a9 170 print.outf = out_stream;
22234f56 171 print.first_time = 1;
22d17b1c 172 print.src_file = "";
f41373b6 173 print.prev_was_system_token = false;
cf44ea52
NB
174}
175
644eddaa
NB
176/* Writes out the preprocessed file, handling spacing and paste
177 avoidance issues. */
93c80368 178static void
2f6e4e97 179scan_translation_unit (cpp_reader *pfile)
93c80368 180{
4ed5bcfb 181 bool avoid_paste = false;
21ece9b2
JJ
182 bool do_line_adjustments
183 = cpp_get_options (parse_in)->lang != CLK_ASM
184 && !flag_no_line_commands;
185 bool in_pragma = false;
f41373b6 186 bool line_marker_emitted = false;
58fea6af 187
63973df3 188 print.source = NULL;
4ed5bcfb 189 for (;;)
93c80368 190 {
21ece9b2
JJ
191 source_location loc;
192 const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
4ed5bcfb
NB
193
194 if (token->type == CPP_PADDING)
195 {
196 avoid_paste = true;
63973df3
NB
197 if (print.source == NULL
198 || (!(print.source->flags & PREV_WHITE)
0c34509f 199 && token->val.source == NULL))
63973df3 200 print.source = token->val.source;
4ed5bcfb
NB
201 continue;
202 }
93c80368 203
ef6e958a
NB
204 if (token->type == CPP_EOF)
205 break;
93c80368 206
4ed5bcfb
NB
207 /* Subtle logic to output a space if and only if necessary. */
208 if (avoid_paste)
209 {
46427374 210 int src_line = LOCATION_LINE (loc);
21ece9b2 211
63973df3
NB
212 if (print.source == NULL)
213 print.source = token;
21ece9b2
JJ
214
215 if (src_line != print.src_line
216 && do_line_adjustments
217 && !in_pragma)
218 {
f41373b6 219 line_marker_emitted = do_line_change (pfile, token, loc, false);
21ece9b2
JJ
220 putc (' ', print.outf);
221 }
222 else if (print.source->flags & PREV_WHITE
223 || (print.prev
224 && cpp_avoid_paste (pfile, print.prev, token))
225 || (print.prev == NULL && token->type == CPP_HASH))
63973df3 226 putc (' ', print.outf);
4ed5bcfb
NB
227 }
228 else if (token->flags & PREV_WHITE)
21ece9b2 229 {
46427374 230 int src_line = LOCATION_LINE (loc);
21ece9b2
JJ
231
232 if (src_line != print.src_line
233 && do_line_adjustments
234 && !in_pragma)
f41373b6 235 line_marker_emitted = do_line_change (pfile, token, loc, false);
21ece9b2
JJ
236 putc (' ', print.outf);
237 }
ef6e958a 238
4ed5bcfb 239 avoid_paste = false;
63973df3
NB
240 print.source = NULL;
241 print.prev = token;
a25a8f3b
JJ
242 if (token->type == CPP_PRAGMA)
243 {
244 const char *space;
245 const char *name;
246
f41373b6 247 line_marker_emitted = maybe_print_line (token->src_loc);
a25a8f3b
JJ
248 fputs ("#pragma ", print.outf);
249 c_pp_lookup_pragma (token->val.pragma, &space, &name);
250 if (space)
251 fprintf (print.outf, "%s %s", space, name);
252 else
253 fprintf (print.outf, "%s", name);
254 print.printed = 1;
21ece9b2 255 in_pragma = true;
a25a8f3b
JJ
256 }
257 else if (token->type == CPP_PRAGMA_EOL)
21ece9b2
JJ
258 {
259 maybe_print_line (token->src_loc);
260 in_pragma = false;
261 }
a25a8f3b 262 else
847e697a
TT
263 {
264 if (cpp_get_options (parse_in)->debug)
265 linemap_dump_location (line_table, token->src_loc,
266 print.outf);
f41373b6
DS
267
268 if (do_line_adjustments
269 && !in_pragma
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. */
276 {
277 do_line_change (pfile, token, loc, false);
278 print.prev_was_system_token = !!in_system_header_at(loc);
279 }
847e697a 280 cpp_output_token (token, print.outf);
f41373b6 281 line_marker_emitted = false;
847e697a 282 }
4ed5bcfb 283
c26302d5
JJ
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)
63973df3 289 account_for_newlines (token->val.str.text, token->val.str.len);
93c80368 290 }
93c80368
NB
291}
292
ccfc4c91
OW
293static void
294print_lines_directives_only (int lines, const void *buf, size_t size)
295{
296 print.src_line += lines;
297 fwrite (buf, 1, size, print.outf);
298}
299
300/* Writes out the preprocessed file, handling spacing and paste
301 avoidance issues. */
302static void
303scan_translation_unit_directives_only (cpp_reader *pfile)
304{
305 struct _cpp_dir_only_callbacks cb;
306
307 cb.print_lines = print_lines_directives_only;
f41373b6 308 cb.maybe_print_line = (void (*) (source_location)) maybe_print_line;
ccfc4c91
OW
309
310 _cpp_preprocess_dir_only (pfile, &cb);
311}
312
12f9df4e 313/* Adjust print.src_line for newlines embedded in output. */
dfbf62ec 314static void
43839642 315account_for_newlines (const unsigned char *str, size_t len)
dfbf62ec 316{
b66377c1
NB
317 while (len--)
318 if (*str++ == '\n')
12f9df4e 319 print.src_line++;
dfbf62ec
NB
320}
321
1a76916c 322/* Writes out a traditionally preprocessed file. */
afb03408 323static void
2f6e4e97 324scan_translation_unit_trad (cpp_reader *pfile)
afb03408 325{
38800fe8 326 while (_cpp_read_logical_line_trad (pfile))
afb03408 327 {
38800fe8 328 size_t len = pfile->out.cur - pfile->out.base;
12f9df4e 329 maybe_print_line (pfile->out.first_line);
63973df3
NB
330 fwrite (pfile->out.base, 1, len, print.outf);
331 print.printed = 1;
b66377c1 332 if (!CPP_OPTION (pfile, discard_comments))
63973df3 333 account_for_newlines (pfile->out.base, len);
afb03408 334 }
afb03408
NB
335}
336
67821e3a
NB
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
f41373b6
DS
339 a line marker. If a line marker was emitted, return TRUE otherwise
340 return FALSE. */
847e697a 341
f41373b6 342static bool
847e697a 343maybe_print_line_1 (source_location src_loc, FILE *stream)
93c80368 344{
f41373b6 345 bool emitted_line_marker = false;
46427374
TT
346 int src_line = LOCATION_LINE (src_loc);
347 const char *src_file = LOCATION_FILE (src_loc);
348
67821e3a 349 /* End the previous line of text. */
63973df3 350 if (print.printed)
93c80368 351 {
847e697a 352 putc ('\n', stream);
12f9df4e 353 print.src_line++;
63973df3 354 print.printed = 0;
93c80368
NB
355 }
356
304dfbe3
RG
357 if (!flag_no_line_commands
358 && src_line >= print.src_line
22d17b1c 359 && src_line < print.src_line + 8
46427374 360 && strcmp (src_file, print.src_file) == 0)
3cab4133 361 {
12f9df4e 362 while (src_line > print.src_line)
93c80368 363 {
847e697a 364 putc ('\n', stream);
12f9df4e 365 print.src_line++;
93c80368
NB
366 }
367 }
368 else
f41373b6 369 emitted_line_marker = print_line_1 (src_loc, "", stream);
847e697a 370
f41373b6 371 return emitted_line_marker;
847e697a
TT
372}
373
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
f41373b6
DS
376 a line marker. If a line marker was emitted, return TRUE otherwise
377 return FALSE. */
847e697a 378
f41373b6 379static bool
847e697a
TT
380maybe_print_line (source_location src_loc)
381{
382 if (cpp_get_options (parse_in)->debug)
383 linemap_dump_location (line_table, src_loc,
384 print.outf);
f41373b6 385 return maybe_print_line_1 (src_loc, print.outf);
93c80368
NB
386}
387
47d89cf3 388/* Output a line marker for logical line LINE. Special flags are "1"
f41373b6
DS
389 or "2" indicating entering or leaving a file. If the line marker
390 was effectively emitted, return TRUE otherwise return FALSE. */
847e697a 391
f41373b6 392static bool
847e697a 393print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
93c80368 394{
f41373b6
DS
395 bool emitted_line_marker = false;
396
93c80368 397 /* End any previous line of text. */
63973df3 398 if (print.printed)
847e697a 399 putc ('\n', stream);
63973df3 400 print.printed = 0;
93c80368 401
63973df3 402 if (!flag_no_line_commands)
47d89cf3 403 {
46427374
TT
404 const char *file_path = LOCATION_FILE (src_loc);
405 int sysp;
406 size_t to_file_len = strlen (file_path);
cceb1885
GDR
407 unsigned char *to_file_quoted =
408 (unsigned char *) alloca (to_file_len * 4 + 1);
dcc229e5 409 unsigned char *p;
df383483 410
46427374
TT
411 print.src_line = LOCATION_LINE (src_loc);
412 print.src_file = file_path;
12f9df4e 413
dcc229e5
ZW
414 /* cpp_quote_string does not nul-terminate, so we have to do it
415 ourselves. */
416 p = cpp_quote_string (to_file_quoted,
46427374
TT
417 (const unsigned char *) file_path,
418 to_file_len);
dcc229e5 419 *p = '\0';
847e697a 420 fprintf (stream, "# %u \"%s\"%s",
d2c979ef 421 print.src_line == 0 ? 1 : print.src_line,
a3737481 422 to_file_quoted, special_flags);
47d89cf3 423
46427374
TT
424 sysp = in_system_header_at (src_loc);
425 if (sysp == 2)
847e697a 426 fputs (" 3 4", stream);
46427374 427 else if (sysp == 1)
847e697a 428 fputs (" 3", stream);
47d89cf3 429
847e697a 430 putc ('\n', stream);
f41373b6 431 emitted_line_marker = true;
47d89cf3 432 }
f41373b6
DS
433
434 return emitted_line_marker;
93c80368
NB
435}
436
847e697a 437/* Output a line marker for logical line LINE. Special flags are "1"
f41373b6
DS
438 or "2" indicating entering or leaving a file. Return TRUE if a
439 line marker was effectively emitted, FALSE otherwise. */
847e697a 440
f41373b6 441static bool
847e697a
TT
442print_line (source_location src_loc, const char *special_flags)
443{
444 if (cpp_get_options (parse_in)->debug)
445 linemap_dump_location (line_table, src_loc,
446 print.outf);
f41373b6 447 return print_line_1 (src_loc, special_flags, print.outf);
847e697a
TT
448}
449
f41373b6
DS
450/* Helper function for cb_line_change and scan_translation_unit.
451 Return TRUE if a line marker is emitted, FALSE otherwise. */
452static bool
21ece9b2
JJ
453do_line_change (cpp_reader *pfile, const cpp_token *token,
454 source_location src_loc, int parsing_args)
97293897 455{
f41373b6 456 bool emitted_line_marker = false;
93d45d9e
JM
457 if (define_queue || undef_queue)
458 dump_queued_macros (pfile);
459
7b9a5a66 460 if (token->type == CPP_EOF || parsing_args)
f41373b6 461 return false;
97293897 462
f41373b6 463 emitted_line_marker = maybe_print_line (src_loc);
63973df3
NB
464 print.prev = 0;
465 print.source = 0;
97293897
NB
466
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. */
278c4662 472 if (!CPP_OPTION (pfile, traditional))
97293897 473 {
46427374 474 int spaces = LOCATION_COLUMN (src_loc) - 2;
63973df3 475 print.printed = 1;
97293897 476
12f9df4e
PB
477 while (-- spaces >= 0)
478 putc (' ', print.outf);
97293897 479 }
f41373b6
DS
480
481 return emitted_line_marker;
97293897 482}
93c80368 483
21ece9b2
JJ
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. */
486static void
487cb_line_change (cpp_reader *pfile, const cpp_token *token,
488 int parsing_args)
489{
490 do_line_change (pfile, token, token->src_loc, parsing_args);
491}
492
93c80368 493static void
a281759f 494cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
2f6e4e97 495 const cpp_string *str)
58fea6af 496{
12f9df4e 497 maybe_print_line (line);
14788775 498 fprintf (print.outf, "#ident %s\n", str->text);
12f9df4e 499 print.src_line++;
58fea6af
ZW
500}
501
502static void
a281759f 503cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
58fea6af 504{
46427374
TT
505 const struct line_map *map;
506
12f9df4e 507 maybe_print_line (line);
63973df3 508 fputs ("#define ", print.outf);
93c80368 509
63973df3
NB
510 /* 'D' is whole definition; 'N' is name only. */
511 if (flag_dump_macros == 'D')
a3737481 512 fputs ((const char *) cpp_macro_definition (pfile, node),
63973df3 513 print.outf);
7096171b 514 else
63973df3 515 fputs ((const char *) NODE_NAME (node), print.outf);
93c80368 516
63973df3 517 putc ('\n', print.outf);
46427374
TT
518 linemap_resolve_location (line_table, line,
519 LRK_MACRO_DEFINITION_LOCATION,
520 &map);
521 if (LINEMAP_LINE (map) != 0)
112598f4 522 print.src_line++;
58fea6af
ZW
523}
524
525static void
12f9df4e 526cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
2f6e4e97 527 cpp_hashnode *node)
58fea6af 528{
12f9df4e 529 maybe_print_line (line);
63973df3 530 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
12f9df4e 531 print.src_line++;
58fea6af
ZW
532}
533
93d45d9e
JM
534static void
535cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
536 cpp_hashnode *node)
537{
538 macro_queue *q;
c9891b70
EB
539 if (node->flags & NODE_BUILTIN)
540 return;
93d45d9e
JM
541 q = XNEW (macro_queue);
542 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
543 q->next = define_queue;
544 define_queue = q;
545}
546
547static void
548cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
549 source_location line ATTRIBUTE_UNUSED,
550 cpp_hashnode *node)
551{
552 macro_queue *q;
553 q = XNEW (macro_queue);
554 q->macro = xstrdup ((const char *) NODE_NAME (node));
555 q->next = undef_queue;
556 undef_queue = q;
557}
558
559static void
560dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
561{
562 macro_queue *q;
563
564 /* End the previous line of text. */
565 if (print.printed)
566 {
567 putc ('\n', print.outf);
568 print.src_line++;
569 print.printed = 0;
570 }
571
572 for (q = define_queue; q;)
573 {
574 macro_queue *oq;
575 fputs ("#define ", print.outf);
576 fputs (q->macro, print.outf);
577 putc ('\n', print.outf);
578 print.src_line++;
579 oq = q;
580 q = q->next;
581 free (oq->macro);
582 free (oq);
583 }
584 define_queue = NULL;
585 for (q = undef_queue; q;)
586 {
587 macro_queue *oq;
588 fprintf (print.outf, "#undef %s\n", q->macro);
589 print.src_line++;
590 oq = q;
591 q = q->next;
592 free (oq->macro);
593 free (oq);
594 }
595 undef_queue = NULL;
596}
597
58fea6af 598static void
12f9df4e 599cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
cbc43ae0
ILT
600 const unsigned char *dir, const char *header, int angle_brackets,
601 const cpp_token **comments)
58fea6af 602{
12f9df4e 603 maybe_print_line (line);
74eb4b3e 604 if (angle_brackets)
cbc43ae0 605 fprintf (print.outf, "#%s <%s>", dir, header);
74eb4b3e 606 else
cbc43ae0
ILT
607 fprintf (print.outf, "#%s \"%s\"", dir, header);
608
609 if (comments != NULL)
610 {
611 while (*comments != NULL)
612 {
613 if ((*comments)->flags & PREV_WHITE)
614 putc (' ', print.outf);
615 cpp_output_token (*comments, print.outf);
616 ++comments;
617 }
618 }
619
620 putc ('\n', print.outf);
12f9df4e 621 print.src_line++;
58fea6af
ZW
622}
623
8e9ea4d7 624/* Callback called when -fworking-director and -E to emit working
fb0840fc 625 directory in cpp output file. */
8e9ea4d7
PB
626
627void
628pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
629{
630 size_t to_file_len = strlen (dir);
cceb1885
GDR
631 unsigned char *to_file_quoted =
632 (unsigned char *) alloca (to_file_len * 4 + 1);
8e9ea4d7
PB
633 unsigned char *p;
634
9ac97460 635 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
5f754896 636 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
8e9ea4d7
PB
637 *p = '\0';
638 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
639}
640
42dcba34 641/* The file name, line number or system header flags have changed, as
12f9df4e 642 described in MAP. */
42dcba34 643
23345bbb 644void
2f6e4e97 645pp_file_change (const struct line_map *map)
58fea6af 646{
d7bc7a98 647 const char *flags = "";
58fea6af 648
f9c65623 649 if (flag_no_line_commands)
23345bbb
NB
650 return;
651
f4b2bde7 652 if (map != NULL)
d7bc7a98 653 {
148e4216 654 input_location = map->start_location;
22234f56 655 if (print.first_time)
f4b2bde7
PB
656 {
657 /* Avoid printing foo.i when the main file is foo.c. */
658 if (!cpp_get_options (parse_in)->preprocessed)
12f9df4e 659 print_line (map->start_location, flags);
22234f56 660 print.first_time = 0;
f4b2bde7
PB
661 }
662 else
663 {
664 /* Bring current file to correct line when entering a new file. */
665 if (map->reason == LC_ENTER)
12f9df4e 666 {
5ffeb913 667 const struct line_map *from = INCLUDED_FROM (line_table, map);
12f9df4e
PB
668 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
669 }
f4b2bde7
PB
670 if (map->reason == LC_ENTER)
671 flags = " 1";
672 else if (map->reason == LC_LEAVE)
673 flags = " 2";
12f9df4e 674 print_line (map->start_location, flags);
f4b2bde7 675 }
ad2a084d 676 }
9ec7291f
ZW
677}
678
bb74c963 679/* Copy a #pragma directive to the preprocessed output. */
58fea6af 680static void
a281759f 681cb_def_pragma (cpp_reader *pfile, source_location line)
58fea6af 682{
12f9df4e 683 maybe_print_line (line);
63973df3
NB
684 fputs ("#pragma ", print.outf);
685 cpp_output_line (pfile, print.outf);
12f9df4e 686 print.src_line++;
58fea6af
ZW
687}
688
58fea6af
ZW
689/* Dump out the hash table. */
690static int
2f6e4e97 691dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
58fea6af 692{
93c80368 693 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
58fea6af 694 {
63973df3 695 fputs ("#define ", print.outf);
a3737481 696 fputs ((const char *) cpp_macro_definition (pfile, node),
63973df3
NB
697 print.outf);
698 putc ('\n', print.outf);
12f9df4e 699 print.src_line++;
58fea6af
ZW
700 }
701
702 return 1;
703}
c0d578e6
GK
704
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. */
708
709static void
710cb_read_pch (cpp_reader *pfile, const char *name,
711 int fd, const char *orig_name ATTRIBUTE_UNUSED)
712{
713 c_common_read_pch (pfile, name, fd, orig_name);
c22cacf3 714
c0d578e6
GK
715 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
716 print.src_line++;
717}