OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / c-ppoutput.c
1 /* Preprocess only, using cpplib.
2    Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007,
3    2008 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 3, 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; see the file COPYING3.  If not see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "cpplib.h"
25 #include "../libcpp/internal.h"
26 #include "tree.h"
27 #include "c-common.h"           /* For flags.  */
28 #include "c-pragma.h"           /* For parse_in.  */
29
30 /* Encapsulates state used to convert a stream of tokens into a text
31    file.  */
32 static struct
33 {
34   FILE *outf;                   /* Stream to write to.  */
35   const cpp_token *prev;        /* Previous token.  */
36   const cpp_token *source;      /* Source token for spacing.  */
37   int src_line;                 /* Line number currently being written.  */
38   unsigned char printed;        /* Nonzero if something output at line.  */
39   bool first_time;              /* pp_file_change hasn't been called yet.  */
40 } print;
41
42 /* General output routines.  */
43 static void scan_translation_unit (cpp_reader *);
44 static void print_lines_directives_only (int, const void *, size_t);
45 static void scan_translation_unit_directives_only (cpp_reader *);
46 static void scan_translation_unit_trad (cpp_reader *);
47 static void account_for_newlines (const unsigned char *, size_t);
48 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
49
50 static void print_line (source_location, const char *);
51 static void maybe_print_line (source_location);
52
53 /* Callback routines for the parser.   Most of these are active only
54    in specific modes.  */
55 static void cb_line_change (cpp_reader *, const cpp_token *, int);
56 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
57 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
58 static void cb_include (cpp_reader *, source_location, const unsigned char *,
59                         const char *, int, const cpp_token **);
60 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
61 static void cb_def_pragma (cpp_reader *, source_location);
62 static void cb_read_pch (cpp_reader *pfile, const char *name,
63                          int fd, const char *orig_name);
64
65 /* Preprocess and output.  */
66 void
67 preprocess_file (cpp_reader *pfile)
68 {
69   /* A successful cpp_read_main_file guarantees that we can call
70      cpp_scan_nooutput or cpp_get_token next.  */
71   if (flag_no_output)
72     {
73       /* Scan -included buffers, then the main file.  */
74       while (pfile->buffer->prev)
75         cpp_scan_nooutput (pfile);
76       cpp_scan_nooutput (pfile);
77     }
78   else if (cpp_get_options (pfile)->traditional)
79     scan_translation_unit_trad (pfile);
80   else if (cpp_get_options (pfile)->directives_only
81            && !cpp_get_options (pfile)->preprocessed)
82     scan_translation_unit_directives_only (pfile);
83   else
84     scan_translation_unit (pfile);
85
86   /* -dM command line option.  Should this be elsewhere?  */
87   if (flag_dump_macros == 'M')
88     cpp_forall_identifiers (pfile, dump_macro, NULL);
89
90   /* Flush any pending output.  */
91   if (print.printed)
92     putc ('\n', print.outf);
93 }
94
95 /* Set up the callbacks as appropriate.  */
96 void
97 init_pp_output (FILE *out_stream)
98 {
99   cpp_callbacks *cb = cpp_get_callbacks (parse_in);
100
101   if (!flag_no_output)
102     {
103       cb->line_change = cb_line_change;
104       /* Don't emit #pragma or #ident directives if we are processing
105          assembly language; the assembler may choke on them.  */
106       if (cpp_get_options (parse_in)->lang != CLK_ASM)
107         {
108           cb->ident      = cb_ident;
109           cb->def_pragma = cb_def_pragma;
110         }
111     }
112
113   if (flag_dump_includes)
114     cb->include  = cb_include;
115
116   if (flag_pch_preprocess)
117     {
118       cb->valid_pch = c_common_valid_pch;
119       cb->read_pch = cb_read_pch;
120     }
121
122   if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
123     {
124       cb->define = cb_define;
125       cb->undef  = cb_undef;
126     }
127
128   /* Initialize the print structure.  Setting print.src_line to -1 here is
129      a trick to guarantee that the first token of the file will cause
130      a linemarker to be output by maybe_print_line.  */
131   print.src_line = -1;
132   print.printed = 0;
133   print.prev = 0;
134   print.outf = out_stream;
135   print.first_time = 1;
136 }
137
138 /* Writes out the preprocessed file, handling spacing and paste
139    avoidance issues.  */
140 static void
141 scan_translation_unit (cpp_reader *pfile)
142 {
143   bool avoid_paste = false;
144
145   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 (print.source == NULL
154               || (!(print.source->flags & PREV_WHITE)
155                   && token->val.source == NULL))
156             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 (print.source == NULL)
167             print.source = token;
168           if (print.source->flags & PREV_WHITE
169               || (print.prev
170                   && cpp_avoid_paste (pfile, print.prev, token))
171               || (print.prev == NULL && token->type == CPP_HASH))
172             putc (' ', print.outf);
173         }
174       else if (token->flags & PREV_WHITE)
175         putc (' ', print.outf);
176
177       avoid_paste = false;
178       print.source = NULL;
179       print.prev = token;
180       if (token->type == CPP_PRAGMA)
181         {
182           const char *space;
183           const char *name;
184
185           maybe_print_line (token->src_loc);
186           fputs ("#pragma ", print.outf);
187           c_pp_lookup_pragma (token->val.pragma, &space, &name);
188           if (space)
189             fprintf (print.outf, "%s %s", space, name);
190           else
191             fprintf (print.outf, "%s", name);
192           print.printed = 1;
193         }
194       else if (token->type == CPP_PRAGMA_EOL)
195         maybe_print_line (token->src_loc);
196       else
197         cpp_output_token (token, print.outf);
198
199       if (token->type == CPP_COMMENT)
200         account_for_newlines (token->val.str.text, token->val.str.len);
201     }
202 }
203
204 static void
205 print_lines_directives_only (int lines, const void *buf, size_t size)
206 {
207   print.src_line += lines;
208   fwrite (buf, 1, size, print.outf);
209 }
210
211 /* Writes out the preprocessed file, handling spacing and paste
212    avoidance issues.  */
213 static void
214 scan_translation_unit_directives_only (cpp_reader *pfile)
215 {
216   struct _cpp_dir_only_callbacks cb;
217
218   cb.print_lines = print_lines_directives_only;
219   cb.maybe_print_line = maybe_print_line;
220
221   _cpp_preprocess_dir_only (pfile, &cb);
222 }
223
224 /* Adjust print.src_line for newlines embedded in output.  */
225 static void
226 account_for_newlines (const unsigned char *str, size_t len)
227 {
228   while (len--)
229     if (*str++ == '\n')
230       print.src_line++;
231 }
232
233 /* Writes out a traditionally preprocessed file.  */
234 static void
235 scan_translation_unit_trad (cpp_reader *pfile)
236 {
237   while (_cpp_read_logical_line_trad (pfile))
238     {
239       size_t len = pfile->out.cur - pfile->out.base;
240       maybe_print_line (pfile->out.first_line);
241       fwrite (pfile->out.base, 1, len, print.outf);
242       print.printed = 1;
243       if (!CPP_OPTION (pfile, discard_comments))
244         account_for_newlines (pfile->out.base, len);
245     }
246 }
247
248 /* If the token read on logical line LINE needs to be output on a
249    different line to the current one, output the required newlines or
250    a line marker, and return 1.  Otherwise return 0.  */
251 static void
252 maybe_print_line (source_location src_loc)
253 {
254   const struct line_map *map = linemap_lookup (line_table, src_loc);
255   int src_line = SOURCE_LINE (map, src_loc);
256   /* End the previous line of text.  */
257   if (print.printed)
258     {
259       putc ('\n', print.outf);
260       print.src_line++;
261       print.printed = 0;
262     }
263
264   if (src_line >= print.src_line && src_line < print.src_line + 8)
265     {
266       while (src_line > print.src_line)
267         {
268           putc ('\n', print.outf);
269           print.src_line++;
270         }
271     }
272   else
273     print_line (src_loc, "");
274 }
275
276 /* Output a line marker for logical line LINE.  Special flags are "1"
277    or "2" indicating entering or leaving a file.  */
278 static void
279 print_line (source_location src_loc, const char *special_flags)
280 {
281   /* End any previous line of text.  */
282   if (print.printed)
283     putc ('\n', print.outf);
284   print.printed = 0;
285
286   if (!flag_no_line_commands)
287     {
288       const struct line_map *map = linemap_lookup (line_table, src_loc);
289
290       size_t to_file_len = strlen (map->to_file);
291       unsigned char *to_file_quoted =
292          (unsigned char *) alloca (to_file_len * 4 + 1);
293       unsigned char *p;
294
295       print.src_line = SOURCE_LINE (map, src_loc);
296
297       /* cpp_quote_string does not nul-terminate, so we have to do it
298          ourselves.  */
299       p = cpp_quote_string (to_file_quoted,
300                             (const unsigned char *) map->to_file, to_file_len);
301       *p = '\0';
302       fprintf (print.outf, "# %u \"%s\"%s",
303                print.src_line == 0 ? 1 : print.src_line,
304                to_file_quoted, special_flags);
305
306       if (map->sysp == 2)
307         fputs (" 3 4", print.outf);
308       else if (map->sysp == 1)
309         fputs (" 3", print.outf);
310
311       putc ('\n', print.outf);
312     }
313 }
314
315 /* Called when a line of output is started.  TOKEN is the first token
316    of the line, and at end of file will be CPP_EOF.  */
317 static void
318 cb_line_change (cpp_reader *pfile, const cpp_token *token,
319                 int parsing_args)
320 {
321   source_location src_loc = token->src_loc;
322
323   if (token->type == CPP_EOF || parsing_args)
324     return;
325
326   maybe_print_line (src_loc);
327   print.prev = 0;
328   print.source = 0;
329
330   /* Supply enough spaces to put this token in its original column,
331      one space per column greater than 2, since scan_translation_unit
332      will provide a space if PREV_WHITE.  Don't bother trying to
333      reconstruct tabs; we can't get it right in general, and nothing
334      ought to care.  Some things do care; the fault lies with them.  */
335   if (!CPP_OPTION (pfile, traditional))
336     {
337       const struct line_map *map = linemap_lookup (line_table, src_loc);
338       int spaces = SOURCE_COLUMN (map, src_loc) - 2;
339       print.printed = 1;
340
341       while (-- spaces >= 0)
342         putc (' ', print.outf);
343     }
344 }
345
346 static void
347 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
348           const cpp_string *str)
349 {
350   maybe_print_line (line);
351   fprintf (print.outf, "#ident %s\n", str->text);
352   print.src_line++;
353 }
354
355 static void
356 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
357 {
358   maybe_print_line (line);
359   fputs ("#define ", print.outf);
360
361   /* 'D' is whole definition; 'N' is name only.  */
362   if (flag_dump_macros == 'D')
363     fputs ((const char *) cpp_macro_definition (pfile, node),
364            print.outf);
365   else
366     fputs ((const char *) NODE_NAME (node), print.outf);
367
368   putc ('\n', print.outf);
369   if (linemap_lookup (line_table, line)->to_line != 0)
370     print.src_line++;
371 }
372
373 static void
374 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
375           cpp_hashnode *node)
376 {
377   maybe_print_line (line);
378   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
379   print.src_line++;
380 }
381
382 static void
383 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
384             const unsigned char *dir, const char *header, int angle_brackets,
385             const cpp_token **comments)
386 {
387   maybe_print_line (line);
388   if (angle_brackets)
389     fprintf (print.outf, "#%s <%s>", dir, header);
390   else
391     fprintf (print.outf, "#%s \"%s\"", dir, header);
392
393   if (comments != NULL)
394     {
395       while (*comments != NULL)
396         {
397           if ((*comments)->flags & PREV_WHITE)
398             putc (' ', print.outf);
399           cpp_output_token (*comments, print.outf);
400           ++comments;
401         }
402     }
403
404   putc ('\n', print.outf);
405   print.src_line++;
406 }
407
408 /* Callback called when -fworking-director and -E to emit working
409    directory in cpp output file.  */
410
411 void
412 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
413 {
414   size_t to_file_len = strlen (dir);
415   unsigned char *to_file_quoted =
416      (unsigned char *) alloca (to_file_len * 4 + 1);
417   unsigned char *p;
418
419   /* cpp_quote_string does not nul-terminate, so we have to do it ourselves.  */
420   p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
421   *p = '\0';
422   fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
423 }
424
425 /* The file name, line number or system header flags have changed, as
426    described in MAP.  */
427
428 void
429 pp_file_change (const struct line_map *map)
430 {
431   const char *flags = "";
432
433   if (flag_no_line_commands)
434     return;
435
436   if (map != NULL)
437     {
438       if (print.first_time)
439         {
440           /* Avoid printing foo.i when the main file is foo.c.  */
441           if (!cpp_get_options (parse_in)->preprocessed)
442             print_line (map->start_location, flags);
443           print.first_time = 0;
444         }
445       else
446         {
447           /* Bring current file to correct line when entering a new file.  */
448           if (map->reason == LC_ENTER)
449             {
450               const struct line_map *from = INCLUDED_FROM (line_table, map);
451               maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
452             }
453           if (map->reason == LC_ENTER)
454             flags = " 1";
455           else if (map->reason == LC_LEAVE)
456             flags = " 2";
457           print_line (map->start_location, flags);
458         }
459     }
460 }
461
462 /* Copy a #pragma directive to the preprocessed output.  */
463 static void
464 cb_def_pragma (cpp_reader *pfile, source_location line)
465 {
466   maybe_print_line (line);
467   fputs ("#pragma ", print.outf);
468   cpp_output_line (pfile, print.outf);
469   print.src_line++;
470 }
471
472 /* Dump out the hash table.  */
473 static int
474 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
475 {
476   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
477     {
478       fputs ("#define ", print.outf);
479       fputs ((const char *) cpp_macro_definition (pfile, node),
480              print.outf);
481       putc ('\n', print.outf);
482       print.src_line++;
483     }
484
485   return 1;
486 }
487
488 /* Load in the PCH file NAME, open on FD.  It was originally searched for
489    by ORIG_NAME.  Also, print out a #include command so that the PCH
490    file can be loaded when the preprocessed output is compiled.  */
491
492 static void
493 cb_read_pch (cpp_reader *pfile, const char *name,
494              int fd, const char *orig_name ATTRIBUTE_UNUSED)
495 {
496   c_common_read_pch (pfile, name, fd, orig_name);
497
498   fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
499   print.src_line++;
500 }