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