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 /* Defined and undefined macros being queued for output with -dU at
43    the next newline.  */
44 typedef struct macro_queue
45 {
46   struct macro_queue *next;     /* Next macro in the list.  */
47   char *macro;                  /* The name of the macro if not
48                                    defined, the full definition if
49                                    defined.  */
50 } macro_queue;
51 static macro_queue *define_queue, *undef_queue;
52
53 /* General output routines.  */
54 static void scan_translation_unit (cpp_reader *);
55 static void print_lines_directives_only (int, const void *, size_t);
56 static void scan_translation_unit_directives_only (cpp_reader *);
57 static void scan_translation_unit_trad (cpp_reader *);
58 static void account_for_newlines (const unsigned char *, size_t);
59 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
60 static void dump_queued_macros (cpp_reader *);
61
62 static void print_line (source_location, const char *);
63 static void maybe_print_line (source_location);
64
65 /* Callback routines for the parser.   Most of these are active only
66    in specific modes.  */
67 static void cb_line_change (cpp_reader *, const cpp_token *, int);
68 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
69 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
70 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
71 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
72 static void cb_include (cpp_reader *, source_location, const unsigned char *,
73                         const char *, int, const cpp_token **);
74 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
75 static void cb_def_pragma (cpp_reader *, source_location);
76 static void cb_read_pch (cpp_reader *pfile, const char *name,
77                          int fd, const char *orig_name);
78
79 /* Preprocess and output.  */
80 void
81 preprocess_file (cpp_reader *pfile)
82 {
83   /* A successful cpp_read_main_file guarantees that we can call
84      cpp_scan_nooutput or cpp_get_token next.  */
85   if (flag_no_output)
86     {
87       /* Scan -included buffers, then the main file.  */
88       while (pfile->buffer->prev)
89         cpp_scan_nooutput (pfile);
90       cpp_scan_nooutput (pfile);
91     }
92   else if (cpp_get_options (pfile)->traditional)
93     scan_translation_unit_trad (pfile);
94   else if (cpp_get_options (pfile)->directives_only
95            && !cpp_get_options (pfile)->preprocessed)
96     scan_translation_unit_directives_only (pfile);
97   else
98     scan_translation_unit (pfile);
99
100   /* -dM command line option.  Should this be elsewhere?  */
101   if (flag_dump_macros == 'M')
102     cpp_forall_identifiers (pfile, dump_macro, NULL);
103
104   /* Flush any pending output.  */
105   if (print.printed)
106     putc ('\n', print.outf);
107 }
108
109 /* Set up the callbacks as appropriate.  */
110 void
111 init_pp_output (FILE *out_stream)
112 {
113   cpp_callbacks *cb = cpp_get_callbacks (parse_in);
114
115   if (!flag_no_output)
116     {
117       cb->line_change = cb_line_change;
118       /* Don't emit #pragma or #ident directives if we are processing
119          assembly language; the assembler may choke on them.  */
120       if (cpp_get_options (parse_in)->lang != CLK_ASM)
121         {
122           cb->ident      = cb_ident;
123           cb->def_pragma = cb_def_pragma;
124         }
125     }
126
127   if (flag_dump_includes)
128     cb->include  = cb_include;
129
130   if (flag_pch_preprocess)
131     {
132       cb->valid_pch = c_common_valid_pch;
133       cb->read_pch = cb_read_pch;
134     }
135
136   if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
137     {
138       cb->define = cb_define;
139       cb->undef  = cb_undef;
140     }
141
142   if (flag_dump_macros == 'U')
143     {
144       cb->before_define = dump_queued_macros;
145       cb->used_define = cb_used_define;
146       cb->used_undef = cb_used_undef;
147     }
148
149   /* Initialize the print structure.  Setting print.src_line to -1 here is
150      a trick to guarantee that the first token of the file will cause
151      a linemarker to be output by maybe_print_line.  */
152   print.src_line = -1;
153   print.printed = 0;
154   print.prev = 0;
155   print.outf = out_stream;
156   print.first_time = 1;
157 }
158
159 /* Writes out the preprocessed file, handling spacing and paste
160    avoidance issues.  */
161 static void
162 scan_translation_unit (cpp_reader *pfile)
163 {
164   bool avoid_paste = false;
165
166   print.source = NULL;
167   for (;;)
168     {
169       const cpp_token *token = cpp_get_token (pfile);
170
171       if (token->type == CPP_PADDING)
172         {
173           avoid_paste = true;
174           if (print.source == NULL
175               || (!(print.source->flags & PREV_WHITE)
176                   && token->val.source == NULL))
177             print.source = token->val.source;
178           continue;
179         }
180
181       if (token->type == CPP_EOF)
182         break;
183
184       /* Subtle logic to output a space if and only if necessary.  */
185       if (avoid_paste)
186         {
187           if (print.source == NULL)
188             print.source = token;
189           if (print.source->flags & PREV_WHITE
190               || (print.prev
191                   && 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       if (token->type == CPP_PRAGMA)
202         {
203           const char *space;
204           const char *name;
205
206           maybe_print_line (token->src_loc);
207           fputs ("#pragma ", print.outf);
208           c_pp_lookup_pragma (token->val.pragma, &space, &name);
209           if (space)
210             fprintf (print.outf, "%s %s", space, name);
211           else
212             fprintf (print.outf, "%s", name);
213           print.printed = 1;
214         }
215       else if (token->type == CPP_PRAGMA_EOL)
216         maybe_print_line (token->src_loc);
217       else
218         cpp_output_token (token, print.outf);
219
220       if (token->type == CPP_COMMENT)
221         account_for_newlines (token->val.str.text, token->val.str.len);
222     }
223 }
224
225 static void
226 print_lines_directives_only (int lines, const void *buf, size_t size)
227 {
228   print.src_line += lines;
229   fwrite (buf, 1, size, print.outf);
230 }
231
232 /* Writes out the preprocessed file, handling spacing and paste
233    avoidance issues.  */
234 static void
235 scan_translation_unit_directives_only (cpp_reader *pfile)
236 {
237   struct _cpp_dir_only_callbacks cb;
238
239   cb.print_lines = print_lines_directives_only;
240   cb.maybe_print_line = maybe_print_line;
241
242   _cpp_preprocess_dir_only (pfile, &cb);
243 }
244
245 /* Adjust print.src_line for newlines embedded in output.  */
246 static void
247 account_for_newlines (const unsigned char *str, size_t len)
248 {
249   while (len--)
250     if (*str++ == '\n')
251       print.src_line++;
252 }
253
254 /* Writes out a traditionally preprocessed file.  */
255 static void
256 scan_translation_unit_trad (cpp_reader *pfile)
257 {
258   while (_cpp_read_logical_line_trad (pfile))
259     {
260       size_t len = pfile->out.cur - pfile->out.base;
261       maybe_print_line (pfile->out.first_line);
262       fwrite (pfile->out.base, 1, len, print.outf);
263       print.printed = 1;
264       if (!CPP_OPTION (pfile, discard_comments))
265         account_for_newlines (pfile->out.base, len);
266     }
267 }
268
269 /* If the token read on logical line LINE needs to be output on a
270    different line to the current one, output the required newlines or
271    a line marker, and return 1.  Otherwise return 0.  */
272 static void
273 maybe_print_line (source_location src_loc)
274 {
275   const struct line_map *map = linemap_lookup (line_table, src_loc);
276   int src_line = SOURCE_LINE (map, src_loc);
277   /* End the previous line of text.  */
278   if (print.printed)
279     {
280       putc ('\n', print.outf);
281       print.src_line++;
282       print.printed = 0;
283     }
284
285   if (src_line >= print.src_line && src_line < print.src_line + 8)
286     {
287       while (src_line > print.src_line)
288         {
289           putc ('\n', print.outf);
290           print.src_line++;
291         }
292     }
293   else
294     print_line (src_loc, "");
295 }
296
297 /* Output a line marker for logical line LINE.  Special flags are "1"
298    or "2" indicating entering or leaving a file.  */
299 static void
300 print_line (source_location src_loc, const char *special_flags)
301 {
302   /* End any previous line of text.  */
303   if (print.printed)
304     putc ('\n', print.outf);
305   print.printed = 0;
306
307   if (!flag_no_line_commands)
308     {
309       const struct line_map *map = linemap_lookup (line_table, src_loc);
310
311       size_t to_file_len = strlen (map->to_file);
312       unsigned char *to_file_quoted =
313          (unsigned char *) alloca (to_file_len * 4 + 1);
314       unsigned char *p;
315
316       print.src_line = SOURCE_LINE (map, src_loc);
317
318       /* cpp_quote_string does not nul-terminate, so we have to do it
319          ourselves.  */
320       p = cpp_quote_string (to_file_quoted,
321                             (const unsigned char *) map->to_file, to_file_len);
322       *p = '\0';
323       fprintf (print.outf, "# %u \"%s\"%s",
324                print.src_line == 0 ? 1 : print.src_line,
325                to_file_quoted, special_flags);
326
327       if (map->sysp == 2)
328         fputs (" 3 4", print.outf);
329       else if (map->sysp == 1)
330         fputs (" 3", print.outf);
331
332       putc ('\n', print.outf);
333     }
334 }
335
336 /* Called when a line of output is started.  TOKEN is the first token
337    of the line, and at end of file will be CPP_EOF.  */
338 static void
339 cb_line_change (cpp_reader *pfile, const cpp_token *token,
340                 int parsing_args)
341 {
342   source_location src_loc = token->src_loc;
343
344   if (define_queue || undef_queue)
345     dump_queued_macros (pfile);
346
347   if (token->type == CPP_EOF || parsing_args)
348     return;
349
350   maybe_print_line (src_loc);
351   print.prev = 0;
352   print.source = 0;
353
354   /* Supply enough spaces to put this token in its original column,
355      one space per column greater than 2, since scan_translation_unit
356      will provide a space if PREV_WHITE.  Don't bother trying to
357      reconstruct tabs; we can't get it right in general, and nothing
358      ought to care.  Some things do care; the fault lies with them.  */
359   if (!CPP_OPTION (pfile, traditional))
360     {
361       const struct line_map *map = linemap_lookup (line_table, src_loc);
362       int spaces = SOURCE_COLUMN (map, src_loc) - 2;
363       print.printed = 1;
364
365       while (-- spaces >= 0)
366         putc (' ', print.outf);
367     }
368 }
369
370 static void
371 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
372           const cpp_string *str)
373 {
374   maybe_print_line (line);
375   fprintf (print.outf, "#ident %s\n", str->text);
376   print.src_line++;
377 }
378
379 static void
380 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
381 {
382   maybe_print_line (line);
383   fputs ("#define ", print.outf);
384
385   /* 'D' is whole definition; 'N' is name only.  */
386   if (flag_dump_macros == 'D')
387     fputs ((const char *) cpp_macro_definition (pfile, node),
388            print.outf);
389   else
390     fputs ((const char *) NODE_NAME (node), print.outf);
391
392   putc ('\n', print.outf);
393   if (linemap_lookup (line_table, line)->to_line != 0)
394     print.src_line++;
395 }
396
397 static void
398 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
399           cpp_hashnode *node)
400 {
401   maybe_print_line (line);
402   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
403   print.src_line++;
404 }
405
406 static void
407 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
408                 cpp_hashnode *node)
409 {
410   macro_queue *q;
411   q = XNEW (macro_queue);
412   q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
413   q->next = define_queue;
414   define_queue = q;
415 }
416
417 static void
418 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
419                source_location line ATTRIBUTE_UNUSED,
420                cpp_hashnode *node)
421 {
422   macro_queue *q;
423   q = XNEW (macro_queue);
424   q->macro = xstrdup ((const char *) NODE_NAME (node));
425   q->next = undef_queue;
426   undef_queue = q;
427 }
428
429 static void
430 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
431 {
432   macro_queue *q;
433
434   /* End the previous line of text.  */
435   if (print.printed)
436     {
437       putc ('\n', print.outf);
438       print.src_line++;
439       print.printed = 0;
440     }
441
442   for (q = define_queue; q;)
443     {
444       macro_queue *oq;
445       fputs ("#define ", print.outf);
446       fputs (q->macro, print.outf);
447       putc ('\n', print.outf);
448       print.src_line++;
449       oq = q;
450       q = q->next;
451       free (oq->macro);
452       free (oq);
453     }
454   define_queue = NULL;
455   for (q = undef_queue; q;)
456     {
457       macro_queue *oq;
458       fprintf (print.outf, "#undef %s\n", q->macro);
459       print.src_line++;
460       oq = q;
461       q = q->next;
462       free (oq->macro);
463       free (oq);
464     }
465   undef_queue = NULL;
466 }
467
468 static void
469 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
470             const unsigned char *dir, const char *header, int angle_brackets,
471             const cpp_token **comments)
472 {
473   maybe_print_line (line);
474   if (angle_brackets)
475     fprintf (print.outf, "#%s <%s>", dir, header);
476   else
477     fprintf (print.outf, "#%s \"%s\"", dir, header);
478
479   if (comments != NULL)
480     {
481       while (*comments != NULL)
482         {
483           if ((*comments)->flags & PREV_WHITE)
484             putc (' ', print.outf);
485           cpp_output_token (*comments, print.outf);
486           ++comments;
487         }
488     }
489
490   putc ('\n', print.outf);
491   print.src_line++;
492 }
493
494 /* Callback called when -fworking-director and -E to emit working
495    directory in cpp output file.  */
496
497 void
498 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
499 {
500   size_t to_file_len = strlen (dir);
501   unsigned char *to_file_quoted =
502      (unsigned char *) alloca (to_file_len * 4 + 1);
503   unsigned char *p;
504
505   /* cpp_quote_string does not nul-terminate, so we have to do it ourselves.  */
506   p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
507   *p = '\0';
508   fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
509 }
510
511 /* The file name, line number or system header flags have changed, as
512    described in MAP.  */
513
514 void
515 pp_file_change (const struct line_map *map)
516 {
517   const char *flags = "";
518
519   if (flag_no_line_commands)
520     return;
521
522   if (map != NULL)
523     {
524       if (print.first_time)
525         {
526           /* Avoid printing foo.i when the main file is foo.c.  */
527           if (!cpp_get_options (parse_in)->preprocessed)
528             print_line (map->start_location, flags);
529           print.first_time = 0;
530         }
531       else
532         {
533           /* Bring current file to correct line when entering a new file.  */
534           if (map->reason == LC_ENTER)
535             {
536               const struct line_map *from = INCLUDED_FROM (line_table, map);
537               maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
538             }
539           if (map->reason == LC_ENTER)
540             flags = " 1";
541           else if (map->reason == LC_LEAVE)
542             flags = " 2";
543           print_line (map->start_location, flags);
544         }
545     }
546 }
547
548 /* Copy a #pragma directive to the preprocessed output.  */
549 static void
550 cb_def_pragma (cpp_reader *pfile, source_location line)
551 {
552   maybe_print_line (line);
553   fputs ("#pragma ", print.outf);
554   cpp_output_line (pfile, print.outf);
555   print.src_line++;
556 }
557
558 /* Dump out the hash table.  */
559 static int
560 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
561 {
562   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
563     {
564       fputs ("#define ", print.outf);
565       fputs ((const char *) cpp_macro_definition (pfile, node),
566              print.outf);
567       putc ('\n', print.outf);
568       print.src_line++;
569     }
570
571   return 1;
572 }
573
574 /* Load in the PCH file NAME, open on FD.  It was originally searched for
575    by ORIG_NAME.  Also, print out a #include command so that the PCH
576    file can be loaded when the preprocessed output is compiled.  */
577
578 static void
579 cb_read_pch (cpp_reader *pfile, const char *name,
580              int fd, const char *orig_name ATTRIBUTE_UNUSED)
581 {
582   c_common_read_pch (pfile, name, fd, orig_name);
583
584   fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
585   print.src_line++;
586 }