OSDN Git Service

86e0335dc9fd22d4c0b3a9e7fb682e3ee1e7f137
[pf3gnuchains/gcc-fork.git] / gcc / fortran / cpp.c
1 /* Copyright (C) 2008 Free Software Foundation, Inc.
2
3 This file is part of GCC.
4
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
8 version.
9
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GCC; see the file COPYING3.  If not see
17 <http://www.gnu.org/licenses/>.  */
18
19 #include "config.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "tm.h"
23 #include "tree.h"
24 #include "version.h"
25 #include "flags.h"
26
27
28 #include "options.h"
29 #include "gfortran.h"
30 #include "tm_p.h"               /* Target prototypes.  */
31 #include "target.h"
32 #include "toplev.h"
33 #include "diagnostic.h"
34
35 #include "../../libcpp/internal.h"
36 #include "cpp.h"
37 #include "incpath.h"
38
39 #ifndef TARGET_OS_CPP_BUILTINS
40 # define TARGET_OS_CPP_BUILTINS()
41 #endif
42
43 #ifndef TARGET_OBJFMT_CPP_BUILTINS
44 # define TARGET_OBJFMT_CPP_BUILTINS()
45 #endif
46
47
48 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
49    handling is deferred to gfc_cpp_init ().  */
50 typedef struct
51 {
52     enum opt_code code;
53     const char *arg;
54 }
55 gfc_cpp_deferred_opt_t;
56
57
58 /* Defined and undefined macros being queued for output with -dU at
59    the next newline.  */
60 typedef struct gfc_cpp_macro_queue
61 {
62   struct gfc_cpp_macro_queue *next;     /* Next macro in the list.  */
63   char *macro;                          /* The name of the macro if not
64                                            defined, the full definition if
65                                            defined.  */
66 } gfc_cpp_macro_queue;
67 static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
68
69 struct
70 {
71   /* Argument of -cpp, implied by SPEC;
72      if NULL, preprocessing disabled.  */
73   const char *temporary_filename;
74
75   const char *output_filename;          /* -o <arg>  */
76   int preprocess_only;                  /* -E  */
77   int discard_comments;                 /* -C  */
78   int discard_comments_in_macro_exp;    /* -CC  */
79   int print_include_names;              /* -H  */
80   int no_line_commands;                 /* -P  */
81   char dump_macros;                     /* -d[DMNU]  */
82   int dump_includes;                    /* -dI  */
83   int working_directory;                /* -fworking-directory  */
84   int no_predefined;                    /* -undef */
85   int standard_include_paths;           /* -nostdinc */
86   int verbose;                          /* -v */
87
88   const char *multilib;                 /* -imultilib <dir>  */
89   const char *prefix;                   /* -iprefix <dir>  */
90   const char *sysroot;                  /* -isysroot <dir>  */
91
92   /* Options whose handling needs to be deferred until the
93      appropriate cpp-objects are created:
94       -A predicate=answer
95       -D <macro>[=<val>]
96       -U <macro>  */
97   gfc_cpp_deferred_opt_t *deferred_opt;
98   int deferred_opt_count;
99 }
100 gfc_cpp_option;
101
102 /* Structures used with libcpp:  */
103 static cpp_options *cpp_option = NULL;
104 static cpp_reader *cpp_in = NULL;
105
106 /* Defined in toplev.c.  */
107 extern const char *asm_file_name;
108
109
110
111
112 /* Encapsulates state used to convert a stream of cpp-tokens into
113    a text file.  */
114 static struct
115 {
116   FILE *outf;                   /* Stream to write to.  */
117   const cpp_token *prev;        /* Previous token.  */
118   const cpp_token *source;      /* Source token for spacing.  */
119   int src_line;                 /* Line number currently being written.  */
120   unsigned char printed;        /* Nonzero if something output at line.  */
121   bool first_time;              /* cb_file_change hasn't been called yet.  */
122 } print;
123
124 /* General output routines.  */
125 static void scan_translation_unit (cpp_reader *);
126 static void scan_translation_unit_trad (cpp_reader *);
127
128 /* Callback routines for the parser. Most of these are active only
129    in specific modes.  */
130 static void cb_file_change (cpp_reader *, const struct line_map *);
131 static void cb_line_change (cpp_reader *, const cpp_token *, int);
132 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
133 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
134 static void cb_def_pragma (cpp_reader *, source_location);
135 static void cb_include (cpp_reader *, source_location, const unsigned char *,
136                         const char *, int, const cpp_token **);
137 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
138 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
139 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
140 void pp_dir_change (cpp_reader *, const char *);
141
142 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
143 static void dump_queued_macros (cpp_reader *);
144
145
146 static void
147 cpp_define_builtins (cpp_reader *pfile)
148 {
149   int major, minor, patchlevel;
150
151   /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
152      in C, defines __STDC_HOSTED__?!  */
153   cpp_init_builtins (pfile, 0);
154
155   /* Initialize GFORTRAN specific builtins.
156      These are documented.  */
157   if (sscanf (BASEVER, "%d.%d.%d", &major, &minor, &patchlevel) != 3)
158     {
159       sscanf (BASEVER, "%d.%d", &major, &minor);
160       patchlevel = 0;
161     }
162   cpp_define_formatted (pfile, "__GNUC__=%d", major);
163   cpp_define_formatted (pfile, "__GNUC_MINOR__=%d", minor);
164   cpp_define_formatted (pfile, "__GNUC_PATCHLEVEL__=%d", patchlevel);
165
166   cpp_define (pfile, "__GFORTRAN__=1");
167   cpp_define (pfile, "_LANGUAGE_FORTRAN=1");
168
169   if (gfc_option.flag_openmp)
170     cpp_define (pfile, "_OPENMP=200805");
171
172
173   /* More builtins that might be useful, but are not documented
174      (in no particular order).  */
175   cpp_define_formatted (pfile, "__VERSION__=\"%s\"", version_string);
176
177   if (flag_pic)
178     {
179       cpp_define_formatted (pfile, "__pic__=%d", flag_pic);
180       cpp_define_formatted (pfile, "__PIC__=%d", flag_pic);
181     }
182   if (flag_pie)
183     {
184       cpp_define_formatted (pfile, "__pie__=%d", flag_pie);
185       cpp_define_formatted (pfile, "__PIE__=%d", flag_pie);
186     }
187
188   if (optimize_size)
189     cpp_define (pfile, "__OPTIMIZE_SIZE__");
190   if (optimize)
191     cpp_define (pfile, "__OPTIMIZE__");
192
193   if (fast_math_flags_set_p ())
194     cpp_define (pfile, "__FAST_MATH__");
195   if (flag_signaling_nans)
196     cpp_define (pfile, "__SUPPORT_SNAN__");
197
198   cpp_define_formatted (pfile, "__FINITE_MATH_ONLY__=%d", flag_finite_math_only);
199
200   /* Definitions for LP64 model. */
201   if (TYPE_PRECISION (long_integer_type_node) == 64
202       && POINTER_SIZE == 64
203       && TYPE_PRECISION (integer_type_node) == 32)
204     {
205       cpp_define (pfile, "_LP64");
206       cpp_define (pfile, "__LP64__");
207     }
208
209   /* Define NAME with value TYPE size_unit.
210      The C-side also defines __SIZEOF_WCHAR_T__, __SIZEOF_WINT_T__
211      __SIZEOF_PTRDIFF_T__, however, fortran seems to lack the
212      appropriate type nodes.  */
213
214 #define define_type_sizeof(NAME, TYPE)                             \
215     cpp_define_formatted (pfile, NAME"="HOST_WIDE_INT_PRINT_DEC,   \
216                           tree_low_cst (TYPE_SIZE_UNIT (TYPE), 1))
217
218   define_type_sizeof ("__SIZEOF_INT__", integer_type_node);
219   define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node);
220   define_type_sizeof ("__SIZEOF_LONG_LONG__", long_long_integer_type_node);
221   define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node);
222   define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node);
223   define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node);
224   define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node);
225   define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node);
226
227 #undef define_type_sizeof
228
229   /* The defines below are necessary for the TARGET_* macros.
230
231      FIXME:  Note that builtin_define_std() actually is a function
232      in c-cppbuiltin.c which uses flags undefined for Fortran.
233      Let's skip this for now. If needed, one needs to look into it
234      once more.  */
235
236 # define builtin_define(TXT) cpp_define (pfile, TXT)
237 # define builtin_define_std(TXT)
238 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
239
240   /* FIXME: Pandora's Box
241     Using the macros below results in multiple breakages:
242      - mingw will fail to compile this file as dependent macros
243        assume to be used in c-cppbuiltin.c only. Further, they use
244        flags only valid/defined in C (same as noted above).
245        [config/i386/mingw32.h, config/i386/cygming.h]
246      - other platforms (not as popular) break similarly
247        [grep for 'builtin_define_with_int_value' in gcc/config/]
248
249   TARGET_CPU_CPP_BUILTINS ();
250   TARGET_OS_CPP_BUILTINS ();
251   TARGET_OBJFMT_CPP_BUILTINS (); */
252
253 #undef builtin_define
254 #undef builtin_define_std
255 #undef builtin_assert
256 }
257
258 bool
259 gfc_cpp_enabled (void)
260 {
261   return gfc_cpp_option.temporary_filename != NULL;
262 }
263
264 bool
265 gfc_cpp_preprocess_only (void)
266 {
267   return gfc_cpp_option.preprocess_only;
268 }
269
270 const char *
271 gfc_cpp_temporary_file (void)
272 {
273   return gfc_cpp_option.temporary_filename;
274 }
275
276 void
277 gfc_cpp_init_options (unsigned int argc,
278                       const char **argv ATTRIBUTE_UNUSED)
279 {
280   /* Do not create any objects from libcpp here. If no
281      preprocessing is requested, this would be wasted
282      time and effort.
283
284      See gfc_cpp_post_options() instead.  */
285
286   gfc_cpp_option.temporary_filename = NULL;
287   gfc_cpp_option.output_filename = NULL;
288   gfc_cpp_option.preprocess_only = 0;
289   gfc_cpp_option.discard_comments = 1;
290   gfc_cpp_option.discard_comments_in_macro_exp = 1;
291   gfc_cpp_option.print_include_names = 0;
292   gfc_cpp_option.no_line_commands = 0;
293   gfc_cpp_option.dump_macros = '\0';
294   gfc_cpp_option.dump_includes = 0;
295   gfc_cpp_option.working_directory = -1;
296   gfc_cpp_option.no_predefined = 0;
297   gfc_cpp_option.standard_include_paths = 1;
298   gfc_cpp_option.verbose = 0;
299
300   gfc_cpp_option.multilib = NULL;
301   gfc_cpp_option.prefix = NULL;
302   gfc_cpp_option.sysroot = NULL;
303
304   gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t, argc);
305   gfc_cpp_option.deferred_opt_count = 0;
306 }
307
308 int
309 gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
310 {
311   int result = 1;
312   enum opt_code code = (enum opt_code) scode;
313
314   switch (code)
315   {
316     default:
317       result = 0;
318       break;
319
320     case OPT_cpp:
321       gfc_cpp_option.temporary_filename = arg;
322       break;
323
324     case OPT_nocpp:
325       gfc_cpp_option.temporary_filename = 0L;
326       break;
327
328     case OPT_d:
329       for ( ; *arg; ++arg)
330         switch (*arg)
331         {
332           case 'D':
333           case 'M':
334           case 'N':
335           case 'U':
336             gfc_cpp_option.dump_macros = *arg;
337             break;
338
339           case 'I':
340             gfc_cpp_option.dump_includes = 1;
341             break;
342         }
343       break;
344
345     case OPT_fworking_directory:
346       gfc_cpp_option.working_directory = value;
347       break;
348
349     case OPT_imultilib:
350       gfc_cpp_option.multilib = arg;
351       break;
352
353     case OPT_iprefix:
354       gfc_cpp_option.prefix = arg;
355       break;
356
357     case OPT_isysroot:
358       gfc_cpp_option.sysroot = arg;
359       break;
360
361     case OPT_iquote:
362     case OPT_isystem:
363       gfc_cpp_add_include_path (xstrdup(arg), true);
364       break;
365
366     case OPT_nostdinc:
367       gfc_cpp_option.standard_include_paths = value;
368       break;
369
370     case OPT_o:
371       if (!gfc_cpp_option.output_filename)
372         gfc_cpp_option.output_filename = arg;
373       else
374         gfc_fatal_error ("output filename specified twice");
375       break;
376
377     case OPT_undef:
378       gfc_cpp_option.no_predefined = value;
379       break;
380
381     case OPT_v:
382       gfc_cpp_option.verbose = value;
383       break;
384
385     case OPT_A:
386     case OPT_D:
387     case OPT_U:
388       gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
389       gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
390       gfc_cpp_option.deferred_opt_count++;
391       break;
392
393     case OPT_C:
394       gfc_cpp_option.discard_comments = 0;
395       break;
396
397     case OPT_CC:
398       gfc_cpp_option.discard_comments = 0;
399       gfc_cpp_option.discard_comments_in_macro_exp = 0;
400       break;
401
402     case OPT_E:
403       gfc_cpp_option.preprocess_only = 1;
404       break;
405
406     case OPT_H:
407       gfc_cpp_option.print_include_names = 1;
408       break;
409
410     case OPT_P:
411       gfc_cpp_option.no_line_commands = 1;
412       break;
413   }
414
415   return result;
416 }
417
418
419 void
420 gfc_cpp_post_options (void)
421 {
422   /* Any preprocessing-related option without '-cpp' is considered
423      an error.  */
424   if (!gfc_cpp_enabled ()
425       && (gfc_cpp_preprocess_only ()
426           || !gfc_cpp_option.discard_comments
427           || !gfc_cpp_option.discard_comments_in_macro_exp
428           || gfc_cpp_option.print_include_names
429           || gfc_cpp_option.no_line_commands
430           || gfc_cpp_option.dump_macros
431           || gfc_cpp_option.dump_includes))
432     gfc_fatal_error("To enable preprocessing, use -cpp");
433
434   cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
435   if (!gfc_cpp_enabled())
436     return;
437
438   gcc_assert (cpp_in);
439
440   /* The cpp_options-structure defines far more flags than those set here.
441      If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
442      inter-option dependencies that may need to be enforced.  */
443   cpp_option = cpp_get_options (cpp_in);
444   gcc_assert (cpp_option);
445
446   /* TODO: allow non-traditional modes, e.g. by -cpp-std=...?  */
447   cpp_option->traditional = 1;
448   cpp_option->cplusplus_comments = 0;
449
450   cpp_option->pedantic = pedantic;
451   cpp_option->inhibit_warnings = inhibit_warnings;
452
453   cpp_option->dollars_in_ident = gfc_option.flag_dollar_ok;
454   cpp_option->discard_comments = gfc_cpp_option.discard_comments;
455   cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
456   cpp_option->print_include_names = gfc_cpp_option.print_include_names;
457   cpp_option->preprocessed = gfc_option.flag_preprocessed;
458
459   if (gfc_cpp_option.working_directory == -1)
460     gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
461
462   cpp_post_options (cpp_in);
463
464   /* If an error has occurred in cpplib, note it so we fail immediately.  */
465   errorcount += cpp_errors (cpp_in);
466
467   gfc_cpp_register_include_paths ();
468 }
469
470
471 void
472 gfc_cpp_init_0 (void)
473 {
474   struct cpp_callbacks *cb;
475
476   cb = cpp_get_callbacks (cpp_in);
477   cb->file_change = cb_file_change;
478   cb->line_change = cb_line_change;
479   cb->ident = cb_ident;
480   cb->def_pragma = cb_def_pragma;
481
482   if (gfc_cpp_option.dump_includes)
483     cb->include = cb_include;
484
485   if ((gfc_cpp_option.dump_macros == 'D')
486       || (gfc_cpp_option.dump_macros == 'N'))
487     {
488       cb->define = cb_define;
489       cb->undef  = cb_undef;
490     }
491
492   if (gfc_cpp_option.dump_macros == 'U')
493     {
494       cb->before_define = dump_queued_macros;
495       cb->used_define = cb_used_define;
496       cb->used_undef = cb_used_undef;
497     }
498
499   /* Initialize the print structure.  Setting print.src_line to -1 here is
500      a trick to guarantee that the first token of the file will cause
501      a linemarker to be output by maybe_print_line.  */
502   print.src_line = -1;
503   print.printed = 0;
504   print.prev = 0;
505   print.first_time = 1;
506
507   if (gfc_cpp_preprocess_only ())
508     {
509       if (gfc_cpp_option.output_filename)
510         {
511           /* This needs cheating: with "-E -o <file>", the user wants the
512              preprocessed output in <file>. However, if nothing is done
513              about it <file> is also used for assembler output. Hence, it
514              is necessary to redirect assembler output (actually nothing
515              as -E implies -fsyntax-only) to another file, otherwise the
516              output from preprocessing is lost.  */
517           asm_file_name = gfc_cpp_option.temporary_filename;
518
519           print.outf = fopen (gfc_cpp_option.output_filename, "w");
520           if (print.outf == NULL)
521             gfc_fatal_error ("opening output file %s: %s",
522                              gfc_cpp_option.output_filename, strerror(errno));
523         }
524       else
525         print.outf = stdout;
526     }
527   else
528     {
529       print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
530       if (print.outf == NULL)
531         gfc_fatal_error ("opening output file %s: %s",
532                          gfc_cpp_option.temporary_filename, strerror(errno));
533     }
534
535   gcc_assert(cpp_in);
536   if (!cpp_read_main_file (cpp_in, gfc_source_file))
537     errorcount++;
538 }
539
540 void
541 gfc_cpp_init (void)
542 {
543   int i;
544
545   if (gfc_option.flag_preprocessed)
546     return;
547
548   cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
549   if (!gfc_cpp_option.no_predefined)
550     cpp_define_builtins (cpp_in);
551
552   /* Handle deferred options from command-line.  */
553   cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
554
555   for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
556     {
557       gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
558
559       if (opt->code == OPT_D)
560         cpp_define (cpp_in, opt->arg);
561       else if (opt->code == OPT_U)
562         cpp_undef (cpp_in, opt->arg);
563       else if (opt->code == OPT_A)
564         {
565           if (opt->arg[0] == '-')
566             cpp_unassert (cpp_in, opt->arg + 1);
567           else
568             cpp_assert (cpp_in, opt->arg);
569         }
570     }
571
572   if (gfc_cpp_option.working_directory
573       && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
574     pp_dir_change (cpp_in, get_src_pwd ());
575 }
576
577 gfc_try
578 gfc_cpp_preprocess (const char *source_file)
579 {
580   if (!gfc_cpp_enabled ())
581     return FAILURE;
582
583   cpp_change_file (cpp_in, LC_RENAME, source_file);
584
585   if (cpp_option->traditional)
586     scan_translation_unit_trad (cpp_in);
587   else
588     scan_translation_unit (cpp_in);
589
590   /* -dM command line option.  */
591   if (gfc_cpp_preprocess_only () &&
592       gfc_cpp_option.dump_macros == 'M')
593     {
594       putc ('\n', print.outf);
595       cpp_forall_identifiers (cpp_in, dump_macro, NULL);
596     }
597
598   putc ('\n', print.outf);
599
600   if (!gfc_cpp_preprocess_only ()
601       || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
602     fclose (print.outf);
603
604   return SUCCESS;
605 }
606
607 void
608 gfc_cpp_done (void)
609 {
610   if (!gfc_cpp_enabled ())
611     return;
612
613   /* TODO: if dependency tracking was enabled, call
614      cpp_finish() here to write dependencies.
615
616      Use cpp_get_deps() to access the current source's
617      dependencies during parsing. Add dependencies using
618      the mkdeps-interface (defined in libcpp).  */
619
620   gcc_assert (cpp_in);
621   cpp_undef_all (cpp_in);
622   cpp_clear_file_cache (cpp_in);
623 }
624
625 /* PATH must be malloc-ed and NULL-terminated.  */
626 void
627 gfc_cpp_add_include_path (char *path, bool user_supplied)
628 {
629   /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
630      include path. Fortran does not define any system include paths.  */
631   int chain = 0;
632   int cxx_aware = 0;
633
634   add_path (path, chain, cxx_aware, user_supplied);
635 }
636
637 void
638 gfc_cpp_register_include_paths (void)
639 {
640   int cxx_stdinc = 0;
641   register_include_chains (cpp_in, gfc_cpp_option.sysroot,
642                            gfc_cpp_option.prefix, gfc_cpp_option.multilib,
643                            gfc_cpp_option.standard_include_paths, cxx_stdinc,
644                            gfc_cpp_option.verbose);
645 }
646
647
648
649 static void scan_translation_unit_trad (cpp_reader *);
650 static void account_for_newlines (const unsigned char *, size_t);
651 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
652
653 static void print_line (source_location, const char *);
654 static void maybe_print_line (source_location);
655
656
657 /* Writes out the preprocessed file, handling spacing and paste
658    avoidance issues.  */
659 static void
660 scan_translation_unit (cpp_reader *pfile)
661 {
662   bool avoid_paste = false;
663
664   print.source = NULL;
665   for (;;)
666     {
667       const cpp_token *token = cpp_get_token (pfile);
668
669       if (token->type == CPP_PADDING)
670         {
671           avoid_paste = true;
672           if (print.source == NULL
673               || (!(print.source->flags & PREV_WHITE)
674                   && token->val.source == NULL))
675             print.source = token->val.source;
676           continue;
677         }
678
679       if (token->type == CPP_EOF)
680         break;
681
682       /* Subtle logic to output a space if and only if necessary.  */
683       if (avoid_paste)
684         {
685           if (print.source == NULL)
686             print.source = token;
687           if (print.source->flags & PREV_WHITE
688               || (print.prev
689                   && cpp_avoid_paste (pfile, print.prev, token))
690               || (print.prev == NULL && token->type == CPP_HASH))
691             putc (' ', print.outf);
692         }
693       else if (token->flags & PREV_WHITE)
694         putc (' ', print.outf);
695
696       avoid_paste = false;
697       print.source = NULL;
698       print.prev = token;
699       cpp_output_token (token, print.outf);
700
701       if (token->type == CPP_COMMENT)
702         account_for_newlines (token->val.str.text, token->val.str.len);
703     }
704 }
705
706 /* Adjust print.src_line for newlines embedded in output.  */
707 static void
708 account_for_newlines (const unsigned char *str, size_t len)
709 {
710   while (len--)
711     if (*str++ == '\n')
712       print.src_line++;
713 }
714
715 /* Writes out a traditionally preprocessed file.  */
716 static void
717 scan_translation_unit_trad (cpp_reader *pfile)
718 {
719   while (_cpp_read_logical_line_trad (pfile))
720     {
721       size_t len = pfile->out.cur - pfile->out.base;
722       maybe_print_line (pfile->out.first_line);
723       fwrite (pfile->out.base, 1, len, print.outf);
724       print.printed = 1;
725       if (!CPP_OPTION (pfile, discard_comments))
726         account_for_newlines (pfile->out.base, len);
727     }
728 }
729
730 /* If the token read on logical line LINE needs to be output on a
731    different line to the current one, output the required newlines or
732    a line marker.  */
733 static void
734 maybe_print_line (source_location src_loc)
735 {
736   const struct line_map *map = linemap_lookup (line_table, src_loc);
737   int src_line = SOURCE_LINE (map, src_loc);
738
739   /* End the previous line of text.  */
740   if (print.printed)
741     {
742       putc ('\n', print.outf);
743       print.src_line++;
744       print.printed = 0;
745     }
746
747   if (src_line >= print.src_line && src_line < print.src_line + 8)
748     {
749       while (src_line > print.src_line)
750         {
751           putc ('\n', print.outf);
752           print.src_line++;
753         }
754     }
755   else
756     print_line (src_loc, "");
757 }
758
759 /* Output a line marker for logical line LINE.  Special flags are "1"
760    or "2" indicating entering or leaving a file.  */
761 static void
762 print_line (source_location src_loc, const char *special_flags)
763 {
764   /* End any previous line of text.  */
765   if (print.printed)
766     putc ('\n', print.outf);
767   print.printed = 0;
768
769   if (!gfc_cpp_option.no_line_commands)
770     {
771       const struct line_map *map = linemap_lookup (line_table, src_loc);
772
773       size_t to_file_len = strlen (map->to_file);
774       unsigned char *to_file_quoted =
775          (unsigned char *) alloca (to_file_len * 4 + 1);
776       unsigned char *p;
777
778       print.src_line = SOURCE_LINE (map, src_loc);
779
780       /* cpp_quote_string does not nul-terminate, so we have to do it
781          ourselves.  */
782       p = cpp_quote_string (to_file_quoted,
783                             (const unsigned char *) map->to_file, to_file_len);
784       *p = '\0';
785       fprintf (print.outf, "# %u \"%s\"%s",
786                print.src_line == 0 ? 1 : print.src_line,
787                to_file_quoted, special_flags);
788
789       if (map->sysp == 2)
790         fputs (" 3 4", print.outf);
791       else if (map->sysp == 1)
792         fputs (" 3", print.outf);
793
794       putc ('\n', print.outf);
795     }
796 }
797
798 static void
799 cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map)
800 {
801   const char *flags = "";
802
803   if (gfc_cpp_option.no_line_commands)
804     return;
805
806   if (!map)
807     return;
808
809       if (print.first_time)
810         {
811           /* Avoid printing foo.i when the main file is foo.c.  */
812           if (!cpp_get_options (cpp_in)->preprocessed)
813             print_line (map->start_location, flags);
814           print.first_time = 0;
815         }
816       else
817         {
818           /* Bring current file to correct line when entering a new file.  */
819           if (map->reason == LC_ENTER)
820             {
821               const struct line_map *from = INCLUDED_FROM (line_table, map);
822               maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
823             }
824           if (map->reason == LC_ENTER)
825             flags = " 1";
826           else if (map->reason == LC_LEAVE)
827             flags = " 2";
828           print_line (map->start_location, flags);
829         }
830
831 }
832
833 /* Called when a line of output is started.  TOKEN is the first token
834    of the line, and at end of file will be CPP_EOF.  */
835 static void
836 cb_line_change (cpp_reader *pfile, const cpp_token *token,
837                 int parsing_args)
838 {
839   source_location src_loc = token->src_loc;
840
841   if (token->type == CPP_EOF || parsing_args)
842     return;
843
844   maybe_print_line (src_loc);
845   print.prev = 0;
846   print.source = 0;
847
848   /* Supply enough spaces to put this token in its original column,
849      one space per column greater than 2, since scan_translation_unit
850      will provide a space if PREV_WHITE.  Don't bother trying to
851      reconstruct tabs; we can't get it right in general, and nothing
852      ought to care.  Some things do care; the fault lies with them.  */
853   if (!CPP_OPTION (pfile, traditional))
854     {
855       const struct line_map *map = linemap_lookup (line_table, src_loc);
856       int spaces = SOURCE_COLUMN (map, src_loc) - 2;
857       print.printed = 1;
858
859       while (-- spaces >= 0)
860         putc (' ', print.outf);
861     }
862 }
863
864 static void
865 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
866           const cpp_string *str)
867 {
868   maybe_print_line (line);
869   fprintf (print.outf, "#ident %s\n", str->text);
870   print.src_line++;
871 }
872
873 static void
874 cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
875            cpp_hashnode *node ATTRIBUTE_UNUSED)
876 {
877   maybe_print_line (line);
878   fputs ("#define ", print.outf);
879
880   /* 'D' is whole definition; 'N' is name only.  */
881   if (gfc_cpp_option.dump_macros == 'D')
882     fputs ((const char *) cpp_macro_definition (pfile, node),
883            print.outf);
884   else
885     fputs ((const char *) NODE_NAME (node), print.outf);
886
887   putc ('\n', print.outf);
888   if (linemap_lookup (line_table, line)->to_line != 0)
889     print.src_line++;
890 }
891
892 static void
893 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
894           cpp_hashnode *node)
895 {
896   maybe_print_line (line);
897   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
898   print.src_line++;
899 }
900
901 static void
902 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
903             const unsigned char *dir, const char *header, int angle_brackets,
904             const cpp_token **comments)
905 {
906   maybe_print_line (line);
907   if (angle_brackets)
908     fprintf (print.outf, "#%s <%s>", dir, header);
909   else
910     fprintf (print.outf, "#%s \"%s\"", dir, header);
911
912   if (comments != NULL)
913     {
914       while (*comments != NULL)
915         {
916           if ((*comments)->flags & PREV_WHITE)
917             putc (' ', print.outf);
918           cpp_output_token (*comments, print.outf);
919           ++comments;
920         }
921     }
922
923   putc ('\n', print.outf);
924   print.src_line++;
925 }
926
927 /* Dump out the hash table.  */
928 static int
929 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
930 {
931   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
932     {
933       fputs ("#define ", print.outf);
934       fputs ((const char *) cpp_macro_definition (pfile, node),
935              print.outf);
936       putc ('\n', print.outf);
937       print.src_line++;
938     }
939
940   return 1;
941 }
942
943 static void
944 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
945                 cpp_hashnode *node)
946 {
947   gfc_cpp_macro_queue *q;
948   q = XNEW (gfc_cpp_macro_queue);
949   q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
950   q->next = cpp_define_queue;
951   cpp_define_queue = q;
952 }
953
954
955 /* Callback called when -fworking-director and -E to emit working
956    directory in cpp output file.  */
957
958 void
959 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
960 {
961   size_t to_file_len = strlen (dir);
962   unsigned char *to_file_quoted =
963      (unsigned char *) alloca (to_file_len * 4 + 1);
964   unsigned char *p;
965
966   /* cpp_quote_string does not nul-terminate, so we have to do it ourselves.  */
967   p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
968   *p = '\0';
969   fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
970 }
971
972 /* Copy a #pragma directive to the preprocessed output.  */
973 static void
974 cb_def_pragma (cpp_reader *pfile, source_location line)
975 {
976   maybe_print_line (line);
977   fputs ("#pragma ", print.outf);
978   cpp_output_line (pfile, print.outf);
979   print.src_line++;
980 }
981
982 static void
983 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
984                source_location line ATTRIBUTE_UNUSED,
985                cpp_hashnode *node)
986 {
987   gfc_cpp_macro_queue *q;
988   q = XNEW (gfc_cpp_macro_queue);
989   q->macro = xstrdup ((const char *) NODE_NAME (node));
990   q->next = cpp_undefine_queue;
991   cpp_undefine_queue = q;
992 }
993
994 static void
995 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
996 {
997   gfc_cpp_macro_queue *q;
998
999   /* End the previous line of text.  */
1000   if (print.printed)
1001     {
1002       putc ('\n', print.outf);
1003       print.src_line++;
1004       print.printed = 0;
1005     }
1006
1007   for (q = cpp_define_queue; q;)
1008     {
1009       gfc_cpp_macro_queue *oq;
1010       fputs ("#define ", print.outf);
1011       fputs (q->macro, print.outf);
1012       putc ('\n', print.outf);
1013       print.src_line++;
1014       oq = q;
1015       q = q->next;
1016       gfc_free (oq->macro);
1017       gfc_free (oq);
1018     }
1019   cpp_define_queue = NULL;
1020   for (q = cpp_undefine_queue; q;)
1021     {
1022       gfc_cpp_macro_queue *oq;
1023       fprintf (print.outf, "#undef %s\n", q->macro);
1024       print.src_line++;
1025       oq = q;
1026       q = q->next;
1027       gfc_free (oq->macro);
1028       gfc_free (oq);
1029     }
1030   cpp_undefine_queue = NULL;
1031 }
1032
1033