OSDN Git Service

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