c-common.c (c_common_init): Call preprocess_file instead.
[gcc.git] / gcc / cppmain.c
1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994-95.
5
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
9 later version.
10
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.
15
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding! */
23
24 #include "config.h"
25 #include "system.h"
26 #include "cpplib.h"
27 #include "cpphash.h"
28
29 /* Encapsulates state used to convert the stream of tokens coming from
30 cpp_get_token back into a text file. */
31 struct printer
32 {
33 FILE *outf; /* Stream to write to. */
34 const struct line_map *map; /* Logical to physical line mappings. */
35 const cpp_token *prev; /* Previous token. */
36 const cpp_token *source; /* Source token for spacing. */
37 unsigned int line; /* Line currently being written. */
38 unsigned char printed; /* Nonzero if something output at line. */
39 };
40
41 static void setup_callbacks PARAMS ((cpp_reader *));
42
43 /* General output routines. */
44 static void scan_translation_unit PARAMS ((cpp_reader *));
45 static void scan_translation_unit_trad PARAMS ((cpp_reader *));
46 static void account_for_newlines PARAMS ((const uchar *, size_t));
47 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
48
49 static void print_line PARAMS ((const struct line_map *, unsigned int,
50 const char *));
51 static void maybe_print_line PARAMS ((const struct line_map *, unsigned int));
52
53 /* Callback routines for the parser. Most of these are active only
54 in specific modes. */
55 static void cb_line_change PARAMS ((cpp_reader *, const cpp_token *, int));
56 static void cb_define PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
57 static void cb_undef PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
58 static void cb_include PARAMS ((cpp_reader *, unsigned int,
59 const unsigned char *, const cpp_token *));
60 static void cb_ident PARAMS ((cpp_reader *, unsigned int,
61 const cpp_string *));
62 static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
63 static void cb_def_pragma PARAMS ((cpp_reader *, unsigned int));
64
65 static cpp_options *options; /* Options of pfile. */
66 static struct printer print;
67
68 /* Preprocess and output. */
69 void
70 cpp_preprocess_file (pfile, out_stream)
71 cpp_reader *pfile;
72 FILE *out_stream;
73 {
74 options = cpp_get_options (pfile);
75
76 /* Initialize the printer structure. Setting print.line to -1 here
77 is a trick to guarantee that the first token of the file will
78 cause a linemarker to be output by maybe_print_line. */
79 print.line = (unsigned int) -1;
80 print.printed = 0;
81 print.prev = 0;
82 print.map = 0;
83 print.outf = out_stream;
84
85 setup_callbacks (pfile);
86
87 if (cpp_read_main_file (pfile, options->in_fname, NULL))
88 {
89 cpp_finish_options (pfile);
90
91 /* A successful cpp_read_main_file guarantees that we can call
92 cpp_scan_nooutput or cpp_get_token next. */
93 if (options->no_output)
94 cpp_scan_nooutput (pfile);
95 else if (options->traditional)
96 scan_translation_unit_trad (pfile);
97 else
98 scan_translation_unit (pfile);
99
100 /* -dM command line option. Should this be in cpp_finish? */
101 if (options->dump_macros == dump_only)
102 cpp_forall_identifiers (pfile, dump_macro, NULL);
103 }
104
105 /* Flush any pending output. */
106 if (print.printed)
107 putc ('\n', print.outf);
108 }
109
110 /* Set up the callbacks as appropriate. */
111 static void
112 setup_callbacks (pfile)
113 cpp_reader *pfile;
114 {
115 cpp_callbacks *cb = cpp_get_callbacks (pfile);
116
117 if (! options->no_output)
118 {
119 cb->line_change = cb_line_change;
120 /* Don't emit #pragma or #ident directives if we are processing
121 assembly language; the assembler may choke on them. */
122 if (options->lang != CLK_ASM)
123 {
124 cb->ident = cb_ident;
125 cb->def_pragma = cb_def_pragma;
126 }
127 if (! options->no_line_commands)
128 cb->file_change = cb_file_change;
129 }
130
131 if (options->dump_includes)
132 cb->include = cb_include;
133
134 if (options->dump_macros == dump_names
135 || options->dump_macros == dump_definitions)
136 {
137 cb->define = cb_define;
138 cb->undef = cb_undef;
139 }
140 }
141
142 /* Writes out the preprocessed file, handling spacing and paste
143 avoidance issues. */
144 static void
145 scan_translation_unit (pfile)
146 cpp_reader *pfile;
147 {
148 bool avoid_paste = false;
149
150 print.source = NULL;
151 for (;;)
152 {
153 const cpp_token *token = cpp_get_token (pfile);
154
155 if (token->type == CPP_PADDING)
156 {
157 avoid_paste = true;
158 if (print.source == NULL
159 || (!(print.source->flags & PREV_WHITE)
160 && token->val.source == NULL))
161 print.source = token->val.source;
162 continue;
163 }
164
165 if (token->type == CPP_EOF)
166 break;
167
168 /* Subtle logic to output a space if and only if necessary. */
169 if (avoid_paste)
170 {
171 if (print.source == NULL)
172 print.source = token;
173 if (print.source->flags & PREV_WHITE
174 || (print.prev && cpp_avoid_paste (pfile, print.prev, token))
175 || (print.prev == NULL && token->type == CPP_HASH))
176 putc (' ', print.outf);
177 }
178 else if (token->flags & PREV_WHITE)
179 putc (' ', print.outf);
180
181 avoid_paste = false;
182 print.source = NULL;
183 print.prev = token;
184 cpp_output_token (token, print.outf);
185
186 if (token->type == CPP_COMMENT)
187 account_for_newlines (token->val.str.text, token->val.str.len);
188 }
189 }
190
191 /* Adjust print.line for newlines embedded in output. */
192 static void
193 account_for_newlines (str, len)
194 const uchar *str;
195 size_t len;
196 {
197 while (len--)
198 if (*str++ == '\n')
199 print.line++;
200 }
201
202 /* Writes out a traditionally preprocessed file. */
203 static void
204 scan_translation_unit_trad (pfile)
205 cpp_reader *pfile;
206 {
207 while (_cpp_read_logical_line_trad (pfile))
208 {
209 size_t len = pfile->out.cur - pfile->out.base;
210 maybe_print_line (print.map, pfile->out.first_line);
211 fwrite (pfile->out.base, 1, len, print.outf);
212 print.printed = 1;
213 if (!CPP_OPTION (pfile, discard_comments))
214 account_for_newlines (pfile->out.base, len);
215 }
216 }
217
218 /* If the token read on logical line LINE needs to be output on a
219 different line to the current one, output the required newlines or
220 a line marker, and return 1. Otherwise return 0. */
221 static void
222 maybe_print_line (map, line)
223 const struct line_map *map;
224 unsigned int line;
225 {
226 /* End the previous line of text. */
227 if (print.printed)
228 {
229 putc ('\n', print.outf);
230 print.line++;
231 print.printed = 0;
232 }
233
234 if (line >= print.line && line < print.line + 8)
235 {
236 while (line > print.line)
237 {
238 putc ('\n', print.outf);
239 print.line++;
240 }
241 }
242 else
243 print_line (map, line, "");
244 }
245
246 /* Output a line marker for logical line LINE. Special flags are "1"
247 or "2" indicating entering or leaving a file. */
248 static void
249 print_line (map, line, special_flags)
250 const struct line_map *map;
251 unsigned int line;
252 const char *special_flags;
253 {
254 /* End any previous line of text. */
255 if (print.printed)
256 putc ('\n', print.outf);
257 print.printed = 0;
258
259 print.line = line;
260 if (! options->no_line_commands)
261 {
262 size_t to_file_len = strlen (map->to_file);
263 unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
264 unsigned char *p;
265
266 /* cpp_quote_string does not nul-terminate, so we have to do it
267 ourselves. */
268 p = cpp_quote_string (to_file_quoted,
269 (unsigned char *)map->to_file, to_file_len);
270 *p = '\0';
271 fprintf (print.outf, "# %u \"%s\"%s",
272 SOURCE_LINE (map, print.line), to_file_quoted, special_flags);
273
274 if (map->sysp == 2)
275 fputs (" 3 4", print.outf);
276 else if (map->sysp == 1)
277 fputs (" 3", print.outf);
278
279 putc ('\n', print.outf);
280 }
281 }
282
283 /* Called when a line of output is started. TOKEN is the first token
284 of the line, and at end of file will be CPP_EOF. */
285 static void
286 cb_line_change (pfile, token, parsing_args)
287 cpp_reader *pfile ATTRIBUTE_UNUSED;
288 const cpp_token *token;
289 int parsing_args;
290 {
291 if (token->type == CPP_EOF || parsing_args)
292 return;
293
294 maybe_print_line (print.map, token->line);
295 print.prev = 0;
296 print.source = 0;
297
298 /* Supply enough spaces to put this token in its original column,
299 one space per column greater than 2, since scan_translation_unit
300 will provide a space if PREV_WHITE. Don't bother trying to
301 reconstruct tabs; we can't get it right in general, and nothing
302 ought to care. Some things do care; the fault lies with them. */
303 if (!CPP_OPTION (pfile, traditional))
304 {
305 print.printed = 1;
306 if (token->col > 2)
307 {
308 unsigned int spaces = token->col - 2;
309
310 while (spaces--)
311 putc (' ', print.outf);
312 }
313 }
314 }
315
316 static void
317 cb_ident (pfile, line, str)
318 cpp_reader *pfile ATTRIBUTE_UNUSED;
319 unsigned int line;
320 const cpp_string * str;
321 {
322 maybe_print_line (print.map, line);
323 fprintf (print.outf, "#ident \"%s\"\n", str->text);
324 print.line++;
325 }
326
327 static void
328 cb_define (pfile, line, node)
329 cpp_reader *pfile;
330 unsigned int line;
331 cpp_hashnode *node;
332 {
333 maybe_print_line (print.map, line);
334 fputs ("#define ", print.outf);
335
336 /* -dD command line option. */
337 if (options->dump_macros == dump_definitions)
338 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
339 else
340 fputs ((const char *) NODE_NAME (node), print.outf);
341
342 putc ('\n', print.outf);
343 print.line++;
344 }
345
346 static void
347 cb_undef (pfile, line, node)
348 cpp_reader *pfile ATTRIBUTE_UNUSED;
349 unsigned int line;
350 cpp_hashnode *node;
351 {
352 maybe_print_line (print.map, line);
353 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
354 print.line++;
355 }
356
357 static void
358 cb_include (pfile, line, dir, header)
359 cpp_reader *pfile;
360 unsigned int line;
361 const unsigned char *dir;
362 const cpp_token *header;
363 {
364 maybe_print_line (print.map, line);
365 fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
366 print.line++;
367 }
368
369 /* The file name, line number or system header flags have changed, as
370 described in MAP. From this point on, the old print.map might be
371 pointing to freed memory, and so must not be dereferenced. */
372
373 static void
374 cb_file_change (pfile, map)
375 cpp_reader *pfile ATTRIBUTE_UNUSED;
376 const struct line_map *map;
377 {
378 const char *flags = "";
379
380 /* First time? */
381 if (print.map == NULL)
382 {
383 /* Avoid printing foo.i when the main file is foo.c. */
384 if (!options->preprocessed)
385 print_line (map, map->from_line, flags);
386 }
387 else
388 {
389 /* Bring current file to correct line when entering a new file. */
390 if (map->reason == LC_ENTER)
391 maybe_print_line (map - 1, map->from_line - 1);
392
393 if (map->reason == LC_ENTER)
394 flags = " 1";
395 else if (map->reason == LC_LEAVE)
396 flags = " 2";
397 print_line (map, map->from_line, flags);
398 }
399
400 print.map = map;
401 }
402
403 /* Copy a #pragma directive to the preprocessed output. */
404 static void
405 cb_def_pragma (pfile, line)
406 cpp_reader *pfile;
407 unsigned int line;
408 {
409 maybe_print_line (print.map, line);
410 fputs ("#pragma ", print.outf);
411 cpp_output_line (pfile, print.outf);
412 print.line++;
413 }
414
415 /* Dump out the hash table. */
416 static int
417 dump_macro (pfile, node, v)
418 cpp_reader *pfile;
419 cpp_hashnode *node;
420 void *v ATTRIBUTE_UNUSED;
421 {
422 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
423 {
424 fputs ("#define ", print.outf);
425 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
426 putc ('\n', print.outf);
427 print.line++;
428 }
429
430 return 1;
431 }