1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994-95.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include "../libcpp/internal.h"
26 /* Encapsulates state used to convert a stream of tokens into a text
30 FILE *outf; /* Stream to write to. */
31 const cpp_token *prev; /* Previous token. */
32 const cpp_token *source; /* Source token for spacing. */
33 int src_line; /* Line number currently being written. */
34 unsigned char printed; /* Nonzero if something output at line. */
35 bool first_time; /* pp_file_change hasn't been called yet. */
38 /* General output routines. */
39 static void scan_translation_unit (cpp_reader *);
40 static void scan_translation_unit_trad (cpp_reader *);
41 static void account_for_newlines (const unsigned char *, size_t);
42 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
44 static void print_line (source_location, const char *);
45 static void maybe_print_line (source_location);
47 /* Callback routines for the parser. Most of these are active only
49 static void cb_line_change (cpp_reader *, const cpp_token *, int);
50 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
51 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
52 static void cb_include (cpp_reader *, source_location, const unsigned char *,
53 const char *, int, const cpp_token **);
54 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
55 static void cb_def_pragma (cpp_reader *, source_location);
57 static void cb_read_pch (cpp_reader *pfile, const char *name,
58 int fd, const char *orig_name);
61 /* Preprocess and output. */
63 preprocess_file (cpp_reader *pfile)
65 /* A successful cpp_read_main_file guarantees that we can call
66 cpp_scan_nooutput or cpp_get_token next. */
69 /* Scan -included buffers, then the main file. */
70 while (pfile->buffer->prev)
71 cpp_scan_nooutput (pfile);
72 cpp_scan_nooutput (pfile);
74 else if (cpp_get_options (pfile)->traditional)
75 scan_translation_unit_trad (pfile);
77 scan_translation_unit (pfile);
79 /* -dM command line option. Should this be elsewhere? */
80 if (flag_dump_macros == 'M')
81 cpp_forall_identifiers (pfile, dump_macro, NULL);
83 /* Flush any pending output. */
85 putc ('\n', print.outf);
88 /* Set up the callbacks as appropriate. */
90 init_pp_output (FILE *out_stream)
92 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
96 cb->line_change = cb_line_change;
97 /* Don't emit #pragma or #ident directives if we are processing
98 assembly language; the assembler may choke on them. */
99 if (cpp_get_options (parse_in)->lang != CLK_ASM)
101 cb->ident = cb_ident;
102 cb->def_pragma = cb_def_pragma;
106 if (flag_dump_includes)
107 cb->include = cb_include;
110 if (flag_pch_preprocess)
112 cb->valid_pch = c_common_valid_pch;
113 cb->read_pch = cb_read_pch;
117 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
119 cb->define = cb_define;
120 cb->undef = cb_undef;
123 /* Initialize the print structure. Setting print.src_line to -1 here is
124 a trick to guarantee that the first token of the file will cause
125 a linemarker to be output by maybe_print_line. */
129 print.outf = out_stream;
130 print.first_time = 1;
133 /* Writes out the preprocessed file, handling spacing and paste
136 scan_translation_unit (cpp_reader *pfile)
138 bool avoid_paste = false;
143 const cpp_token *token = cpp_get_token (pfile);
145 if (token->type == CPP_PADDING)
148 if (print.source == NULL
149 || (!(print.source->flags & PREV_WHITE)
150 && token->val.source == NULL))
151 print.source = token->val.source;
155 if (token->type == CPP_EOF)
158 /* Subtle logic to output a space if and only if necessary. */
161 if (print.source == NULL)
162 print.source = token;
163 if (print.source->flags & PREV_WHITE
165 && cpp_avoid_paste (pfile, print.prev, token))
166 || (print.prev == NULL && token->type == CPP_HASH))
167 putc (' ', print.outf);
169 else if (token->flags & PREV_WHITE)
170 putc (' ', print.outf);
175 cpp_output_token (token, print.outf);
177 if (token->type == CPP_COMMENT)
178 account_for_newlines (token->val.str.text, token->val.str.len);
182 /* Adjust print.src_line for newlines embedded in output. */
184 account_for_newlines (const unsigned char *str, size_t len)
191 /* Writes out a traditionally preprocessed file. */
193 scan_translation_unit_trad (cpp_reader *pfile)
195 while (_cpp_read_logical_line_trad (pfile))
198 size_t len = pfile->out.cur - pfile->out.base;
199 maybe_print_line (pfile->out.first_line);
200 res = fwrite (pfile->out.base, 1, len, print.outf);
203 if (!CPP_OPTION (pfile, discard_comments))
204 account_for_newlines (pfile->out.base, len);
208 /* If the token read on logical line LINE needs to be output on a
209 different line to the current one, output the required newlines or
210 a line marker, and return 1. Otherwise return 0. */
212 maybe_print_line (source_location src_loc)
214 const struct line_map *map = linemap_lookup (&line_table, src_loc);
215 int src_line = SOURCE_LINE (map, src_loc);
216 /* End the previous line of text. */
219 putc ('\n', print.outf);
224 if (src_line >= print.src_line && src_line < print.src_line + 8)
226 while (src_line > print.src_line)
228 putc ('\n', print.outf);
233 print_line (src_loc, "");
236 /* Output a line marker for logical line LINE. Special flags are "1"
237 or "2" indicating entering or leaving a file. */
239 print_line (source_location src_loc, const char *special_flags)
241 /* End any previous line of text. */
243 putc ('\n', print.outf);
246 if (!flag_no_line_commands)
248 const struct line_map *map = linemap_lookup (&line_table, src_loc);
250 size_t to_file_len = strlen (map->to_file);
251 unsigned char *to_file_quoted =
252 (unsigned char *) alloca (to_file_len * 4 + 1);
255 print.src_line = SOURCE_LINE (map, src_loc);
257 /* cpp_quote_string does not nul-terminate, so we have to do it
259 p = cpp_quote_string (to_file_quoted,
260 (unsigned char *) map->to_file, to_file_len);
262 fprintf (print.outf, "# %u \"%s\"%s",
263 print.src_line == 0 ? 1 : print.src_line,
264 to_file_quoted, special_flags);
267 fputs (" 3 4", print.outf);
268 else if (map->sysp == 1)
269 fputs (" 3", print.outf);
271 putc ('\n', print.outf);
275 /* Called when a line of output is started. TOKEN is the first token
276 of the line, and at end of file will be CPP_EOF. */
278 cb_line_change (cpp_reader *pfile, const cpp_token *token,
281 source_location src_loc = token->src_loc;
283 if (token->type == CPP_EOF || parsing_args)
286 maybe_print_line (src_loc);
290 /* Supply enough spaces to put this token in its original column,
291 one space per column greater than 2, since scan_translation_unit
292 will provide a space if PREV_WHITE. Don't bother trying to
293 reconstruct tabs; we can't get it right in general, and nothing
294 ought to care. Some things do care; the fault lies with them. */
295 if (!CPP_OPTION (pfile, traditional))
297 const struct line_map *map = linemap_lookup (&line_table, src_loc);
298 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
301 while (-- spaces >= 0)
302 putc (' ', print.outf);
307 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
308 const cpp_string *str)
310 maybe_print_line (line);
311 fprintf (print.outf, "#ident %s\n", str->text);
316 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
318 maybe_print_line (line);
319 fputs ("#define ", print.outf);
321 /* 'D' is whole definition; 'N' is name only. */
322 if (flag_dump_macros == 'D')
323 fputs ((const char *) cpp_macro_definition (pfile, node),
326 fputs ((const char *) NODE_NAME (node), print.outf);
328 putc ('\n', print.outf);
329 if (linemap_lookup (&line_table, line)->to_line != 0)
334 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
337 maybe_print_line (line);
338 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
343 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
344 const unsigned char *dir, const char *header, int angle_brackets,
345 const cpp_token **comments)
347 maybe_print_line (line);
349 fprintf (print.outf, "#%s <%s>", dir, header);
351 fprintf (print.outf, "#%s \"%s\"", dir, header);
353 if (comments != NULL)
355 while (*comments != NULL)
357 if ((*comments)->flags & PREV_WHITE)
358 putc (' ', print.outf);
359 cpp_output_token (*comments, print.outf);
364 putc ('\n', print.outf);
368 /* Callback called when -fworking-director and -E to emit working
369 directory in cpp output file. */
372 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
374 size_t to_file_len = strlen (dir);
375 unsigned char *to_file_quoted =
376 (unsigned char *) alloca (to_file_len * 4 + 1);
379 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
380 p = cpp_quote_string (to_file_quoted, (unsigned char *) dir, to_file_len);
382 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
385 /* The file name, line number or system header flags have changed, as
389 pp_file_change (const struct line_map *map)
391 const char *flags = "";
393 if (flag_no_line_commands)
398 if (print.first_time)
400 /* Avoid printing foo.i when the main file is foo.c. */
401 if (!cpp_get_options (parse_in)->preprocessed)
402 print_line (map->start_location, flags);
403 print.first_time = 0;
407 /* Bring current file to correct line when entering a new file. */
408 if (map->reason == LC_ENTER)
410 const struct line_map *from = INCLUDED_FROM (&line_table, map);
411 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
413 if (map->reason == LC_ENTER)
415 else if (map->reason == LC_LEAVE)
417 print_line (map->start_location, flags);
422 /* Copy a #pragma directive to the preprocessed output. */
424 cb_def_pragma (cpp_reader *pfile, source_location line)
426 maybe_print_line (line);
427 fputs ("#pragma ", print.outf);
428 cpp_output_line (pfile, print.outf);
432 /* Dump out the hash table. */
434 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
436 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
438 fputs ("#define ", print.outf);
439 fputs ((const char *) cpp_macro_definition (pfile, node),
441 putc ('\n', print.outf);
449 /* Load in the PCH file NAME, open on FD. It was originally searched for
450 by ORIG_NAME. Also, print out a #include command so that the PCH
451 file can be loaded when the preprocessed output is compiled. */
454 cb_read_pch (cpp_reader *pfile, const char *name,
455 int fd, const char *orig_name ATTRIBUTE_UNUSED)
457 c_common_read_pch (pfile, name, fd, orig_name);
459 fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);