OSDN Git Service

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