OSDN Git Service

* g++.dg/init/new1.C, g++.dg/template/alignof1.C,
[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 "coretypes.h"
27 #include "tm.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30
31 static void setup_callbacks PARAMS ((cpp_reader *));
32
33 /* General output routines.  */
34 static void scan_translation_unit PARAMS ((cpp_reader *));
35 static void scan_translation_unit_trad PARAMS ((cpp_reader *));
36 static void account_for_newlines PARAMS ((cpp_reader *, const uchar *,
37                                           size_t));
38 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
39
40 static void print_line PARAMS ((cpp_reader *, const struct line_map *,
41                                 unsigned int, const char *));
42 static void maybe_print_line PARAMS ((cpp_reader *, const struct line_map *,
43                                       unsigned int));
44
45 /* Callback routines for the parser.   Most of these are active only
46    in specific modes.  */
47 static void cb_line_change PARAMS ((cpp_reader *, const cpp_token *, int));
48 static void cb_define   PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
49 static void cb_undef    PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
50 static void cb_include  PARAMS ((cpp_reader *, unsigned int,
51                                  const unsigned char *, const cpp_token *));
52 static void cb_ident      PARAMS ((cpp_reader *, unsigned int,
53                                    const cpp_string *));
54 static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
55 static void cb_def_pragma PARAMS ((cpp_reader *, unsigned int));
56
57 /* Preprocess and output.  */
58 void
59 cpp_preprocess_file (pfile, in_fname, out_stream)
60      cpp_reader *pfile;
61      const char *in_fname;
62      FILE *out_stream;
63 {
64   /* Initialize the printer structure.  Setting print.line to -1 here
65      is a trick to guarantee that the first token of the file will
66      cause a linemarker to be output by maybe_print_line.  */
67   pfile->print.line = (unsigned int) -1;
68   pfile->print.printed = 0;
69   pfile->print.prev = 0;
70   pfile->print.map = 0;
71   pfile->print.outf = out_stream;
72
73   setup_callbacks (pfile);
74
75   if (cpp_read_main_file (pfile, in_fname, NULL))
76     {
77       cpp_options *options = &pfile->opts;
78       cpp_finish_options (pfile);
79
80       /* A successful cpp_read_main_file guarantees that we can call
81          cpp_scan_nooutput or cpp_get_token next.  */
82       if (options->no_output)
83         {
84           /* Scan -included buffers, then the main file.  */
85           while (pfile->buffer->prev)
86             cpp_scan_nooutput (pfile);
87           cpp_scan_nooutput (pfile);
88         }
89       else if (options->traditional)
90         scan_translation_unit_trad (pfile);
91       else
92         scan_translation_unit (pfile);
93
94       /* -dM command line option.  Should this be in cpp_finish?  */
95       if (options->dump_macros == dump_only)
96         cpp_forall_identifiers (pfile, dump_macro, NULL);
97     }
98
99   /* Flush any pending output.  */
100   if (pfile->print.printed)
101     putc ('\n', pfile->print.outf);
102 }
103
104 /* Set up the callbacks as appropriate.  */
105 static void
106 setup_callbacks (pfile)
107      cpp_reader *pfile;
108 {
109   cpp_options *options = &pfile->opts;
110   cpp_callbacks *cb = cpp_get_callbacks (pfile);
111
112   if (! options->no_output)
113     {
114       cb->line_change = cb_line_change;
115       /* Don't emit #pragma or #ident directives if we are processing
116          assembly language; the assembler may choke on them.  */
117       if (options->lang != CLK_ASM)
118         {
119           cb->ident      = cb_ident;
120           cb->def_pragma = cb_def_pragma;
121         }
122       if (! options->no_line_commands)
123         cb->file_change = cb_file_change;
124     }
125
126   if (options->dump_includes)
127     cb->include  = cb_include;
128
129   if (options->dump_macros == dump_names
130       || options->dump_macros == dump_definitions)
131     {
132       cb->define = cb_define;
133       cb->undef  = cb_undef;
134     }
135 }
136
137 /* Writes out the preprocessed file, handling spacing and paste
138    avoidance issues.  */
139 static void
140 scan_translation_unit (pfile)
141      cpp_reader *pfile;
142 {
143   bool avoid_paste = false;
144
145   pfile->print.source = NULL;
146   for (;;)
147     {
148       const cpp_token *token = cpp_get_token (pfile);
149
150       if (token->type == CPP_PADDING)
151         {
152           avoid_paste = true;
153           if (pfile->print.source == NULL
154               || (!(pfile->print.source->flags & PREV_WHITE)
155                   && token->val.source == NULL))
156             pfile->print.source = token->val.source;
157           continue;
158         }
159
160       if (token->type == CPP_EOF)
161         break;
162
163       /* Subtle logic to output a space if and only if necessary.  */
164       if (avoid_paste)
165         {
166           if (pfile->print.source == NULL)
167             pfile->print.source = token;
168           if (pfile->print.source->flags & PREV_WHITE
169               || (pfile->print.prev
170                   && cpp_avoid_paste (pfile, pfile->print.prev, token))
171               || (pfile->print.prev == NULL && token->type == CPP_HASH))
172             putc (' ', pfile->print.outf);
173         }
174       else if (token->flags & PREV_WHITE)
175         putc (' ', pfile->print.outf);
176
177       avoid_paste = false;
178       pfile->print.source = NULL;
179       pfile->print.prev = token;
180       cpp_output_token (token, pfile->print.outf);
181
182       if (token->type == CPP_COMMENT)
183         account_for_newlines (pfile, token->val.str.text, token->val.str.len);
184     }
185 }
186
187 /* Adjust pfile->print.line for newlines embedded in output.  */
188 static void
189 account_for_newlines (pfile, str, len)
190      cpp_reader *pfile;
191      const uchar *str;
192      size_t len;
193 {
194   while (len--)
195     if (*str++ == '\n')
196       pfile->print.line++;
197 }
198
199 /* Writes out a traditionally preprocessed file.  */
200 static void
201 scan_translation_unit_trad (pfile)
202      cpp_reader *pfile;
203 {
204   while (_cpp_read_logical_line_trad (pfile))
205     {
206       size_t len = pfile->out.cur - pfile->out.base;
207       maybe_print_line (pfile, pfile->print.map, pfile->out.first_line);
208       fwrite (pfile->out.base, 1, len, pfile->print.outf);
209       pfile->print.printed = 1;
210       if (!CPP_OPTION (pfile, discard_comments))
211         account_for_newlines (pfile, pfile->out.base, len);
212     }
213 }
214
215 /* If the token read on logical line LINE needs to be output on a
216    different line to the current one, output the required newlines or
217    a line marker, and return 1.  Otherwise return 0.  */
218 static void
219 maybe_print_line (pfile, map, line)
220      cpp_reader *pfile;
221      const struct line_map *map;
222      unsigned int line;
223 {
224   /* End the previous line of text.  */
225   if (pfile->print.printed)
226     {
227       putc ('\n', pfile->print.outf);
228       pfile->print.line++;
229       pfile->print.printed = 0;
230     }
231
232   if (line >= pfile->print.line && line < pfile->print.line + 8)
233     {
234       while (line > pfile->print.line)
235         {
236           putc ('\n', pfile->print.outf);
237           pfile->print.line++;
238         }
239     }
240   else
241     print_line (pfile, map, line, "");
242 }
243
244 /* Output a line marker for logical line LINE.  Special flags are "1"
245    or "2" indicating entering or leaving a file.  */
246 static void
247 print_line (pfile, map, line, special_flags)
248      cpp_reader *pfile;
249      const struct line_map *map;
250      unsigned int line;
251      const char *special_flags;
252 {
253   /* End any previous line of text.  */
254   if (pfile->print.printed)
255     putc ('\n', pfile->print.outf);
256   pfile->print.printed = 0;
257
258   pfile->print.line = line;
259   if (! CPP_OPTION (pfile, no_line_commands))
260     {
261       size_t to_file_len = strlen (map->to_file);
262       unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
263       unsigned char *p;
264
265       /* cpp_quote_string does not nul-terminate, so we have to do it
266          ourselves.  */
267       p = cpp_quote_string (to_file_quoted,
268                             (unsigned char *)map->to_file, to_file_len);
269       *p = '\0';
270       fprintf (pfile->print.outf, "# %u \"%s\"%s",
271                SOURCE_LINE (map, pfile->print.line),
272                to_file_quoted, special_flags);
273
274       if (map->sysp == 2)
275         fputs (" 3 4", pfile->print.outf);
276       else if (map->sysp == 1)
277         fputs (" 3", pfile->print.outf);
278
279       putc ('\n', pfile->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;
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 (pfile, pfile->print.map, token->line);
295   pfile->print.prev = 0;
296   pfile->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       pfile->print.printed = 1;
306       if (token->col > 2)
307         {
308           unsigned int spaces = token->col - 2;
309
310           while (spaces--)
311             putc (' ', pfile->print.outf);
312         }
313     }
314 }
315
316 static void
317 cb_ident (pfile, line, str)
318      cpp_reader *pfile;
319      unsigned int line;
320      const cpp_string * str;
321 {
322   maybe_print_line (pfile, pfile->print.map, line);
323   fprintf (pfile->print.outf, "#ident \"%s\"\n", str->text);
324   pfile->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 (pfile, pfile->print.map, line);
334   fputs ("#define ", pfile->print.outf);
335
336   /* -dD command line option.  */
337   if (CPP_OPTION (pfile, dump_macros) == dump_definitions)
338     fputs ((const char *) cpp_macro_definition (pfile, node),
339            pfile->print.outf);
340   else
341     fputs ((const char *) NODE_NAME (node), pfile->print.outf);
342
343   putc ('\n', pfile->print.outf);
344   pfile->print.line++;
345 }
346
347 static void
348 cb_undef (pfile, line, node)
349      cpp_reader *pfile;
350      unsigned int line;
351      cpp_hashnode *node;
352 {
353   maybe_print_line (pfile, pfile->print.map, line);
354   fprintf (pfile->print.outf, "#undef %s\n", NODE_NAME (node));
355   pfile->print.line++;
356 }
357
358 static void
359 cb_include (pfile, line, dir, header)
360      cpp_reader *pfile;
361      unsigned int line;
362      const unsigned char *dir;
363      const cpp_token *header;
364 {
365   maybe_print_line (pfile, pfile->print.map, line);
366   fprintf (pfile->print.outf, "#%s %s\n", dir,
367            cpp_token_as_text (pfile, header));
368   pfile->print.line++;
369 }
370
371 /* The file name, line number or system header flags have changed, as
372    described in MAP.  From this point on, the old pfile->print.map might be
373    pointing to freed memory, and so must not be dereferenced.  */
374
375 static void
376 cb_file_change (pfile, map)
377      cpp_reader *pfile;
378      const struct line_map *map;
379 {
380   const char *flags = "";
381
382   /* First time?  */
383   if (pfile->print.map == NULL)
384     {
385       /* Avoid printing foo.i when the main file is foo.c.  */
386       if (!CPP_OPTION (pfile, preprocessed))
387         print_line (pfile, map, map->from_line, flags);
388     }
389   else
390     {
391       /* Bring current file to correct line when entering a new file.  */
392       if (map->reason == LC_ENTER)
393         maybe_print_line (pfile, map - 1, map->from_line - 1);
394
395       if (map->reason == LC_ENTER)
396         flags = " 1";
397       else if (map->reason == LC_LEAVE)
398         flags = " 2";
399       print_line (pfile, map, map->from_line, flags);
400     }
401
402   pfile->print.map = map;
403 }
404
405 /* Copy a #pragma directive to the preprocessed output.  */
406 static void
407 cb_def_pragma (pfile, line)
408      cpp_reader *pfile;
409      unsigned int line;
410 {
411   maybe_print_line (pfile, pfile->print.map, line);
412   fputs ("#pragma ", pfile->print.outf);
413   cpp_output_line (pfile, pfile->print.outf);
414   pfile->print.line++;
415 }
416
417 /* Dump out the hash table.  */
418 static int
419 dump_macro (pfile, node, v)
420      cpp_reader *pfile;
421      cpp_hashnode *node;
422      void *v ATTRIBUTE_UNUSED;
423 {
424   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
425     {
426       fputs ("#define ", pfile->print.outf);
427       fputs ((const char *) cpp_macro_definition (pfile, node),
428              pfile->print.outf);
429       putc ('\n', pfile->print.outf);
430       pfile->print.line++;
431     }
432
433   return 1;
434 }