OSDN Git Service

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