OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.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)
71      cpp_reader *pfile;
72 {
73   options = cpp_get_options (pfile);
74
75   /* Let preprocessor know if it's only preprocessing.  It would be
76      nice to lose this somehow.  */
77   options->preprocess_only = 1;
78
79   /* Initialize the printer structure.  Setting print.line to -1 here
80      is a trick to guarantee that the first token of the file will
81      cause a linemarker to be output by maybe_print_line.  */
82   print.line = (unsigned int) -1;
83   print.printed = 0;
84   print.prev = 0;
85   print.map = 0;
86
87   /* Open the output now.  We must do so even if no_output is on,
88      because there may be other output than from the actual
89      preprocessing (e.g. from -dM).  */
90   if (options->out_fname[0] == '\0')
91     print.outf = stdout;
92   else
93     {
94       print.outf = fopen (options->out_fname, "w");
95       if (print.outf == NULL)
96         {
97           cpp_errno (pfile, DL_ERROR, options->out_fname);
98           return;
99         }
100     }
101
102   setup_callbacks (pfile);
103
104   if (cpp_read_main_file (pfile, options->in_fname, NULL))
105     {
106       cpp_finish_options (pfile);
107
108       /* A successful cpp_read_main_file guarantees that we can call
109          cpp_scan_nooutput or cpp_get_token next.  */
110       if (options->no_output)
111         cpp_scan_nooutput (pfile);
112       else if (options->traditional)
113         scan_translation_unit_trad (pfile);
114       else
115         scan_translation_unit (pfile);
116
117       /* -dM command line option.  Should this be in cpp_finish?  */
118       if (options->dump_macros == dump_only)
119         cpp_forall_identifiers (pfile, dump_macro, NULL);
120     }
121
122   /* Flush any pending output.  */
123   if (print.printed)
124     putc ('\n', print.outf);
125
126   /* Don't close stdout (dependencies have yet to be output).  */
127   if (print.outf != stdout)
128     {
129       if (ferror (print.outf) || fclose (print.outf))
130         cpp_errno (pfile, DL_ERROR, options->out_fname);
131     }
132 }
133
134 /* Set up the callbacks as appropriate.  */
135 static void
136 setup_callbacks (pfile)
137      cpp_reader *pfile;
138 {
139   cpp_callbacks *cb = cpp_get_callbacks (pfile);
140
141   if (! options->no_output)
142     {
143       cb->line_change = cb_line_change;
144       /* Don't emit #pragma or #ident directives if we are processing
145          assembly language; the assembler may choke on them.  */
146       if (options->lang != CLK_ASM)
147         {
148           cb->ident      = cb_ident;
149           cb->def_pragma = cb_def_pragma;
150         }
151       if (! options->no_line_commands)
152         cb->file_change = cb_file_change;
153     }
154
155   if (options->dump_includes)
156     cb->include  = cb_include;
157
158   if (options->dump_macros == dump_names
159       || options->dump_macros == dump_definitions)
160     {
161       cb->define = cb_define;
162       cb->undef  = cb_undef;
163     }
164 }
165
166 /* Writes out the preprocessed file, handling spacing and paste
167    avoidance issues.  */
168 static void
169 scan_translation_unit (pfile)
170      cpp_reader *pfile;
171 {
172   bool avoid_paste = false;
173
174   print.source = NULL;
175   for (;;)
176     {
177       const cpp_token *token = cpp_get_token (pfile);
178
179       if (token->type == CPP_PADDING)
180         {
181           avoid_paste = true;
182           if (print.source == NULL
183               || (!(print.source->flags & PREV_WHITE)
184                   && token->val.source == NULL))
185             print.source = token->val.source;
186           continue;
187         }
188
189       if (token->type == CPP_EOF)
190         break;
191
192       /* Subtle logic to output a space if and only if necessary.  */
193       if (avoid_paste)
194         {
195           if (print.source == NULL)
196             print.source = token;
197           if (print.source->flags & PREV_WHITE
198               || (print.prev && cpp_avoid_paste (pfile, print.prev, token))
199               || (print.prev == NULL && token->type == CPP_HASH))
200             putc (' ', print.outf);
201         }
202       else if (token->flags & PREV_WHITE)
203         putc (' ', print.outf);
204
205       avoid_paste = false;
206       print.source = NULL;
207       print.prev = token;
208       cpp_output_token (token, print.outf);
209
210       if (token->type == CPP_COMMENT)
211         account_for_newlines (token->val.str.text, token->val.str.len);
212     }
213 }
214
215 /* Adjust print.line for newlines embedded in output.  */
216 static void
217 account_for_newlines (str, len)
218      const uchar *str;
219      size_t len;
220 {
221   while (len--)
222     if (*str++ == '\n')
223       print.line++;
224 }
225
226 /* Writes out a traditionally preprocessed file.  */
227 static void
228 scan_translation_unit_trad (pfile)
229      cpp_reader *pfile;
230 {
231   for (;;)
232     {
233       size_t len;
234
235       if (!_cpp_read_logical_line_trad (pfile))
236         break;
237       len = pfile->out.cur - pfile->out.base;
238       maybe_print_line (print.map, pfile->out.first_line);
239       fwrite (pfile->out.base, 1, len, print.outf);
240       print.printed = 1;
241       if (!CPP_OPTION (pfile, discard_comments))
242         account_for_newlines (pfile->out.base, len);
243     }
244 }
245
246 /* If the token read on logical line LINE needs to be output on a
247    different line to the current one, output the required newlines or
248    a line marker, and return 1.  Otherwise return 0.  */
249 static void
250 maybe_print_line (map, line)
251      const struct line_map *map;
252      unsigned int line;
253 {
254   /* End the previous line of text.  */
255   if (print.printed)
256     {
257       putc ('\n', print.outf);
258       print.line++;
259       print.printed = 0;
260     }
261
262   if (line >= print.line && line < print.line + 8)
263     {
264       while (line > print.line)
265         {
266           putc ('\n', print.outf);
267           print.line++;
268         }
269     }
270   else
271     print_line (map, line, "");
272 }
273
274 /* Output a line marker for logical line LINE.  Special flags are "1"
275    or "2" indicating entering or leaving a file.  */
276 static void
277 print_line (map, line, special_flags)
278      const struct line_map *map;
279      unsigned int line;
280      const char *special_flags;
281 {
282   /* End any previous line of text.  */
283   if (print.printed)
284     putc ('\n', print.outf);
285   print.printed = 0;
286
287   print.line = line;
288   if (! options->no_line_commands)
289     {
290       size_t to_file_len = strlen (map->to_file);
291       unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
292       unsigned char *p;
293
294       /* cpp_quote_string does not nul-terminate, so we have to do it
295          ourselves.  */
296       p = cpp_quote_string (to_file_quoted,
297                             (unsigned char *)map->to_file, to_file_len);
298       *p = '\0';
299       fprintf (print.outf, "# %u \"%s\"%s",
300                SOURCE_LINE (map, print.line), to_file_quoted, special_flags);
301
302       if (map->sysp == 2)
303         fputs (" 3 4", print.outf);
304       else if (map->sysp == 1)
305         fputs (" 3", print.outf);
306
307       putc ('\n', print.outf);
308     }
309 }
310
311 /* Called when a line of output is started.  TOKEN is the first token
312    of the line, and at end of file will be CPP_EOF.  */
313 static void
314 cb_line_change (pfile, token, parsing_args)
315      cpp_reader *pfile ATTRIBUTE_UNUSED;
316      const cpp_token *token;
317      int parsing_args;
318 {
319   if (token->type == CPP_EOF || parsing_args)
320     return;
321
322   maybe_print_line (print.map, token->line);
323   print.prev = 0;
324   print.source = 0;
325
326   /* Supply enough spaces to put this token in its original column,
327      one space per column greater than 2, since scan_translation_unit
328      will provide a space if PREV_WHITE.  Don't bother trying to
329      reconstruct tabs; we can't get it right in general, and nothing
330      ought to care.  Some things do care; the fault lies with them.  */
331   if (!CPP_OPTION (pfile, traditional))
332     {
333       print.printed = 1;
334       if (token->col > 2)
335         {
336           unsigned int spaces = token->col - 2;
337
338           while (spaces--)
339             putc (' ', print.outf);
340         }
341     }
342 }
343
344 static void
345 cb_ident (pfile, line, str)
346      cpp_reader *pfile ATTRIBUTE_UNUSED;
347      unsigned int line;
348      const cpp_string * str;
349 {
350   maybe_print_line (print.map, line);
351   fprintf (print.outf, "#ident \"%s\"\n", str->text);
352   print.line++;
353 }
354
355 static void
356 cb_define (pfile, line, node)
357      cpp_reader *pfile;
358      unsigned int line;
359      cpp_hashnode *node;
360 {
361   maybe_print_line (print.map, line);
362   fputs ("#define ", print.outf);
363
364   /* -dD command line option.  */
365   if (options->dump_macros == dump_definitions)
366     fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
367   else
368     fputs ((const char *) NODE_NAME (node), print.outf);
369
370   putc ('\n', print.outf);
371   print.line++;
372 }
373
374 static void
375 cb_undef (pfile, line, node)
376      cpp_reader *pfile ATTRIBUTE_UNUSED;
377      unsigned int line;
378      cpp_hashnode *node;
379 {
380   maybe_print_line (print.map, line);
381   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
382   print.line++;
383 }
384
385 static void
386 cb_include (pfile, line, dir, header)
387      cpp_reader *pfile;
388      unsigned int line;
389      const unsigned char *dir;
390      const cpp_token *header;
391 {
392   maybe_print_line (print.map, line);
393   fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
394   print.line++;
395 }
396
397 /* The file name, line number or system header flags have changed, as
398    described in MAP.  From this point on, the old print.map might be
399    pointing to freed memory, and so must not be dereferenced.  */
400
401 static void
402 cb_file_change (pfile, map)
403      cpp_reader *pfile ATTRIBUTE_UNUSED;
404      const struct line_map *map;
405 {
406   const char *flags = "";
407
408   /* First time?  */
409   if (print.map == NULL)
410     {
411       /* Avoid printing foo.i when the main file is foo.c.  */
412       if (!options->preprocessed)
413         print_line (map, map->from_line, flags);
414     }
415   else
416     {
417       /* Bring current file to correct line when entering a new file.  */
418       if (map->reason == LC_ENTER)
419         maybe_print_line (map - 1, map->from_line - 1);
420
421       if (map->reason == LC_ENTER)
422         flags = " 1";
423       else if (map->reason == LC_LEAVE)
424         flags = " 2";
425       print_line (map, map->from_line, flags);
426     }
427
428   print.map = map;
429 }
430
431 /* Copy a #pragma directive to the preprocessed output.  */
432 static void
433 cb_def_pragma (pfile, line)
434      cpp_reader *pfile;
435      unsigned int line;
436 {
437   maybe_print_line (print.map, line);
438   fputs ("#pragma ", print.outf);
439   cpp_output_line (pfile, print.outf);
440   print.line++;
441 }
442
443 /* Dump out the hash table.  */
444 static int
445 dump_macro (pfile, node, v)
446      cpp_reader *pfile;
447      cpp_hashnode *node;
448      void *v ATTRIBUTE_UNUSED;
449 {
450   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
451     {
452       fputs ("#define ", print.outf);
453       fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
454       putc ('\n', print.outf);
455       print.line++;
456     }
457
458   return 1;
459 }