Fix ChangeLog
[official-gcc.git] / gcc / fortran / cpp.c
blob170f6cdcd63391d90d57fdc94efc00067772de4e
1 #include "config.h"
2 #include "system.h"
3 #include "coretypes.h"
4 #include "tm.h"
5 #include "tree.h"
6 #include "version.h"
7 #include "flags.h"
10 #include "options.h"
11 #include "gfortran.h"
12 #include "tm_p.h" /* Target prototypes. */
13 #include "target.h"
14 #include "toplev.h"
15 #include "diagnostic.h"
17 #include "../../libcpp/internal.h"
18 #include "cpp.h"
19 #include "incpath.h"
21 #ifndef TARGET_OS_CPP_BUILTINS
22 # define TARGET_OS_CPP_BUILTINS()
23 #endif
25 #ifndef TARGET_OBJFMT_CPP_BUILTINS
26 # define TARGET_OBJFMT_CPP_BUILTINS()
27 #endif
30 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
31 handling is deferred to gfc_cpp_init (). */
32 typedef struct
34 enum opt_code code;
35 const char *arg;
37 gfc_cpp_deferred_opt_t;
40 /* Defined and undefined macros being queued for output with -dU at
41 the next newline. */
42 typedef struct gfc_cpp_macro_queue
44 struct gfc_cpp_macro_queue *next; /* Next macro in the list. */
45 char *macro; /* The name of the macro if not
46 defined, the full definition if
47 defined. */
48 } gfc_cpp_macro_queue;
49 static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
51 struct
53 /* Argument of -cpp, implied by SPEC;
54 if NULL, preprocessing disabled. */
55 const char *temporary_filename;
57 const char *output_filename; /* -o <arg> */
58 int preprocess_only; /* -E */
59 int discard_comments; /* -C */
60 int discard_comments_in_macro_exp; /* -CC */
61 int print_include_names; /* -H */
62 int no_line_commands; /* -P */
63 char dump_macros; /* -d[DMNU] */
64 int dump_includes; /* -dI */
65 int working_directory; /* -fworking-directory */
66 int no_predefined; /* -undef */
67 int standard_include_paths; /* -nostdinc */
68 int verbose; /* -v */
70 const char *multilib; /* -imultilib <dir> */
71 const char *prefix; /* -iprefix <dir> */
72 const char *sysroot; /* -isysroot <dir> */
74 /* Options whose handling needs to be deferred until the
75 appropriate cpp-objects are created:
76 -A predicate=answer
77 -D <macro>[=<val>]
78 -U <macro> */
79 gfc_cpp_deferred_opt_t *deferred_opt;
80 int deferred_opt_count;
82 gfc_cpp_option;
84 /* Structures used with libcpp: */
85 static cpp_options *cpp_option = NULL;
86 static cpp_reader *cpp_in = NULL;
88 /* Defined in toplev.c. */
89 extern const char *asm_file_name;
94 /* Encapsulates state used to convert a stream of cpp-tokens into
95 a text file. */
96 static struct
98 FILE *outf; /* Stream to write to. */
99 const cpp_token *prev; /* Previous token. */
100 const cpp_token *source; /* Source token for spacing. */
101 int src_line; /* Line number currently being written. */
102 unsigned char printed; /* Nonzero if something output at line. */
103 bool first_time; /* cb_file_change hasn't been called yet. */
104 } print;
106 /* General output routines. */
107 static void scan_translation_unit (cpp_reader *);
108 static void scan_translation_unit_trad (cpp_reader *);
110 /* Callback routines for the parser. Most of these are active only
111 in specific modes. */
112 static void cb_file_change (cpp_reader *, const struct line_map *);
113 static void cb_line_change (cpp_reader *, const cpp_token *, int);
114 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
115 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
116 static void cb_def_pragma (cpp_reader *, source_location);
117 static void cb_include (cpp_reader *, source_location, const unsigned char *,
118 const char *, int, const cpp_token **);
119 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
120 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
121 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
122 void pp_dir_change (cpp_reader *, const char *);
124 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
125 static void dump_queued_macros (cpp_reader *);
128 static void
129 cpp_define_builtins (cpp_reader *pfile)
131 int major, minor, patchlevel;
133 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
134 in C, defines __STDC_HOSTED__?! */
135 cpp_init_builtins (pfile, 0);
137 /* Initialize GFORTRAN specific builtins.
138 These are documented. */
139 if (sscanf (BASEVER, "%d.%d.%d", &major, &minor, &patchlevel) != 3)
141 sscanf (BASEVER, "%d.%d", &major, &minor);
142 patchlevel = 0;
144 cpp_define_formatted (pfile, "__GNUC__=%d", major);
145 cpp_define_formatted (pfile, "__GNUC_MINOR__=%d", minor);
146 cpp_define_formatted (pfile, "__GNUC_PATCHLEVEL__=%d", patchlevel);
148 cpp_define (pfile, "__GFORTRAN__=1");
149 cpp_define (pfile, "_LANGUAGE_FORTRAN=1");
151 if (gfc_option.flag_openmp)
152 cpp_define (pfile, "_OPENMP=200505");
155 /* More builtins that might be useful, but are not documented
156 (in no particular order). */
157 cpp_define_formatted (pfile, "__VERSION__=\"%s\"", version_string);
159 if (flag_pic)
161 cpp_define_formatted (pfile, "__pic__=%d", flag_pic);
162 cpp_define_formatted (pfile, "__PIC__=%d", flag_pic);
164 if (flag_pie)
166 cpp_define_formatted (pfile, "__pie__=%d", flag_pie);
167 cpp_define_formatted (pfile, "__PIE__=%d", flag_pie);
170 if (optimize_size)
171 cpp_define (pfile, "__OPTIMIZE_SIZE__");
172 if (optimize)
173 cpp_define (pfile, "__OPTIMIZE__");
175 if (fast_math_flags_set_p ())
176 cpp_define (pfile, "__FAST_MATH__");
177 if (flag_signaling_nans)
178 cpp_define (pfile, "__SUPPORT_SNAN__");
180 cpp_define_formatted (pfile, "__FINITE_MATH_ONLY__=%d", flag_finite_math_only);
182 /* Definitions for LP64 model. */
183 if (TYPE_PRECISION (long_integer_type_node) == 64
184 && POINTER_SIZE == 64
185 && TYPE_PRECISION (integer_type_node) == 32)
187 cpp_define (pfile, "_LP64");
188 cpp_define (pfile, "__LP64__");
191 /* Define NAME with value TYPE size_unit.
192 The C-side also defines __SIZEOF_WCHAR_T__, __SIZEOF_WINT_T__
193 __SIZEOF_PTRDIFF_T__, however, fortran seems to lack the
194 appropriate type nodes. */
196 #define define_type_sizeof(NAME, TYPE) \
197 cpp_define_formatted (pfile, NAME"="HOST_WIDE_INT_PRINT_DEC, \
198 tree_low_cst (TYPE_SIZE_UNIT (TYPE), 1))
200 define_type_sizeof ("__SIZEOF_INT__", integer_type_node);
201 define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node);
202 define_type_sizeof ("__SIZEOF_LONG_LONG__", long_long_integer_type_node);
203 define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node);
204 define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node);
205 define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node);
206 define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node);
207 define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node);
209 #undef define_type_sizeof
211 /* The defines below are necessary for the TARGET_* macros.
213 FIXME: Note that builtin_define_std() actually is a function
214 in c-cppbuiltin.c which uses flags undefined for Fortran.
215 Let's skip this for now. If needed, one needs to look into it
216 once more. */
218 # define builtin_define(TXT) cpp_define (pfile, TXT)
219 # define builtin_define_std(TXT)
220 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
222 /* FIXME: Pandora's Box
223 Using the macros below results in multiple breakages:
224 - mingw will fail to compile this file as dependent macros
225 assume to be used in c-cppbuiltin.c only. Further, they use
226 flags only valid/defined in C (same as noted above).
227 [config/i386/mingw32.h, config/i386/cygming.h]
228 - other platforms (not as popular) break similarly
229 [grep for 'builtin_define_with_int_value' in gcc/config/]
231 TARGET_CPU_CPP_BUILTINS ();
232 TARGET_OS_CPP_BUILTINS ();
233 TARGET_OBJFMT_CPP_BUILTINS (); */
235 #undef builtin_define
236 #undef builtin_define_std
237 #undef builtin_assert
240 bool
241 gfc_cpp_enabled (void)
243 return gfc_cpp_option.temporary_filename != NULL;
246 bool
247 gfc_cpp_preprocess_only (void)
249 return gfc_cpp_option.preprocess_only;
252 const char *
253 gfc_cpp_temporary_file (void)
255 return gfc_cpp_option.temporary_filename;
258 void
259 gfc_cpp_init_options (unsigned int argc,
260 const char **argv ATTRIBUTE_UNUSED)
262 /* Do not create any objects from libcpp here. If no
263 preprocessing is requested, this would be wasted
264 time and effort.
266 See gfc_cpp_post_options() instead. */
268 gfc_cpp_option.temporary_filename = NULL;
269 gfc_cpp_option.output_filename = NULL;
270 gfc_cpp_option.preprocess_only = 0;
271 gfc_cpp_option.discard_comments = 1;
272 gfc_cpp_option.discard_comments_in_macro_exp = 1;
273 gfc_cpp_option.print_include_names = 0;
274 gfc_cpp_option.no_line_commands = 0;
275 gfc_cpp_option.dump_macros = '\0';
276 gfc_cpp_option.dump_includes = 0;
277 gfc_cpp_option.working_directory = -1;
278 gfc_cpp_option.no_predefined = 0;
279 gfc_cpp_option.standard_include_paths = 1;
280 gfc_cpp_option.verbose = 0;
282 gfc_cpp_option.multilib = NULL;
283 gfc_cpp_option.prefix = NULL;
284 gfc_cpp_option.sysroot = NULL;
286 gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t, argc);
287 gfc_cpp_option.deferred_opt_count = 0;
291 gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
293 int result = 1;
294 enum opt_code code = (enum opt_code) scode;
296 switch (code)
298 default:
299 result = 0;
300 break;
302 case OPT_cpp:
303 gfc_cpp_option.temporary_filename = arg;
304 break;
306 case OPT_nocpp:
307 gfc_cpp_option.temporary_filename = 0L;
308 break;
310 case OPT_d:
311 for ( ; *arg; ++arg)
312 switch (*arg)
314 case 'D':
315 case 'M':
316 case 'N':
317 case 'U':
318 gfc_cpp_option.dump_macros = *arg;
319 break;
321 case 'I':
322 gfc_cpp_option.dump_includes = 1;
323 break;
325 break;
327 case OPT_fworking_directory:
328 gfc_cpp_option.working_directory = value;
329 break;
331 case OPT_imultilib:
332 gfc_cpp_option.multilib = arg;
333 break;
335 case OPT_iprefix:
336 gfc_cpp_option.prefix = arg;
337 break;
339 case OPT_isysroot:
340 gfc_cpp_option.sysroot = arg;
341 break;
343 case OPT_iquote:
344 case OPT_isystem:
345 gfc_cpp_add_include_path (xstrdup(arg), true);
346 break;
348 case OPT_nostdinc:
349 gfc_cpp_option.standard_include_paths = value;
350 break;
352 case OPT_o:
353 if (!gfc_cpp_option.output_filename)
354 gfc_cpp_option.output_filename = arg;
355 else
356 gfc_fatal_error ("output filename specified twice");
357 break;
359 case OPT_undef:
360 gfc_cpp_option.no_predefined = value;
361 break;
363 case OPT_v:
364 gfc_cpp_option.verbose = value;
365 break;
367 case OPT_A:
368 case OPT_D:
369 case OPT_U:
370 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
371 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
372 gfc_cpp_option.deferred_opt_count++;
373 break;
375 case OPT_C:
376 gfc_cpp_option.discard_comments = 0;
377 break;
379 case OPT_CC:
380 gfc_cpp_option.discard_comments = 0;
381 gfc_cpp_option.discard_comments_in_macro_exp = 0;
382 break;
384 case OPT_E:
385 gfc_cpp_option.preprocess_only = 1;
386 break;
388 case OPT_H:
389 gfc_cpp_option.print_include_names = 1;
390 break;
392 case OPT_P:
393 gfc_cpp_option.no_line_commands = 1;
394 break;
397 return result;
401 void
402 gfc_cpp_post_options (void)
404 /* Any preprocessing-related option without '-cpp' is considered
405 an error. */
406 if (!gfc_cpp_enabled ()
407 && (gfc_cpp_preprocess_only ()
408 || !gfc_cpp_option.discard_comments
409 || !gfc_cpp_option.discard_comments_in_macro_exp
410 || gfc_cpp_option.print_include_names
411 || gfc_cpp_option.no_line_commands
412 || gfc_cpp_option.dump_macros
413 || gfc_cpp_option.dump_includes))
414 gfc_fatal_error("To enable preprocessing, use -cpp");
416 cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
417 if (!gfc_cpp_enabled())
418 return;
420 gcc_assert (cpp_in);
422 /* The cpp_options-structure defines far more flags than those set here.
423 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
424 inter-option dependencies that may need to be enforced. */
425 cpp_option = cpp_get_options (cpp_in);
426 gcc_assert (cpp_option);
428 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
429 cpp_option->traditional = 1;
430 cpp_option->cplusplus_comments = 0;
432 cpp_option->pedantic = pedantic;
433 cpp_option->inhibit_warnings = inhibit_warnings;
435 cpp_option->dollars_in_ident = gfc_option.flag_dollar_ok;
436 cpp_option->discard_comments = gfc_cpp_option.discard_comments;
437 cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
438 cpp_option->print_include_names = gfc_cpp_option.print_include_names;
439 cpp_option->preprocessed = gfc_option.flag_preprocessed;
441 if (gfc_cpp_option.working_directory == -1)
442 gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
444 cpp_post_options (cpp_in);
446 /* If an error has occurred in cpplib, note it so we fail immediately. */
447 errorcount += cpp_errors (cpp_in);
449 gfc_cpp_register_include_paths ();
453 void
454 gfc_cpp_init_0 (void)
456 struct cpp_callbacks *cb;
458 cb = cpp_get_callbacks (cpp_in);
459 cb->file_change = cb_file_change;
460 cb->line_change = cb_line_change;
461 cb->ident = cb_ident;
462 cb->def_pragma = cb_def_pragma;
464 if (gfc_cpp_option.dump_includes)
465 cb->include = cb_include;
467 if ((gfc_cpp_option.dump_macros == 'D')
468 || (gfc_cpp_option.dump_macros == 'N'))
470 cb->define = cb_define;
471 cb->undef = cb_undef;
474 if (gfc_cpp_option.dump_macros == 'U')
476 cb->before_define = dump_queued_macros;
477 cb->used_define = cb_used_define;
478 cb->used_undef = cb_used_undef;
481 /* Initialize the print structure. Setting print.src_line to -1 here is
482 a trick to guarantee that the first token of the file will cause
483 a linemarker to be output by maybe_print_line. */
484 print.src_line = -1;
485 print.printed = 0;
486 print.prev = 0;
487 print.first_time = 1;
489 if (gfc_cpp_preprocess_only ())
491 if (gfc_cpp_option.output_filename)
493 /* This needs cheating: with "-E -o <file>", the user wants the
494 preprocessed output in <file>. However, if nothing is done
495 about it <file> is also used for assembler output. Hence, it
496 is necessary to redirect assembler output (actually nothing
497 as -E implies -fsyntax-only) to another file, otherwise the
498 output from preprocessing is lost. */
499 asm_file_name = gfc_cpp_option.temporary_filename;
501 print.outf = fopen (gfc_cpp_option.output_filename, "w");
502 if (print.outf == NULL)
503 gfc_fatal_error ("opening output file %s: %s",
504 gfc_cpp_option.output_filename, strerror(errno));
506 else
507 print.outf = stdout;
509 else
511 print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
512 if (print.outf == NULL)
513 gfc_fatal_error ("opening output file %s: %s",
514 gfc_cpp_option.temporary_filename, strerror(errno));
517 gcc_assert(cpp_in);
518 if (!cpp_read_main_file (cpp_in, gfc_source_file))
519 errorcount++;
522 void
523 gfc_cpp_init (void)
525 int i;
527 if (gfc_option.flag_preprocessed)
528 return;
530 cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
531 if (!gfc_cpp_option.no_predefined)
532 cpp_define_builtins (cpp_in);
534 /* Handle deferred options from command-line. */
535 cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
537 for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
539 gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
541 if (opt->code == OPT_D)
542 cpp_define (cpp_in, opt->arg);
543 else if (opt->code == OPT_U)
544 cpp_undef (cpp_in, opt->arg);
545 else if (opt->code == OPT_A)
547 if (opt->arg[0] == '-')
548 cpp_unassert (cpp_in, opt->arg + 1);
549 else
550 cpp_assert (cpp_in, opt->arg);
554 if (gfc_cpp_option.working_directory
555 && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
556 pp_dir_change (cpp_in, get_src_pwd ());
560 gfc_cpp_preprocess (const char *source_file)
562 if (!gfc_cpp_enabled ())
563 return FAILURE;
565 cpp_change_file (cpp_in, LC_RENAME, source_file);
567 if (cpp_option->traditional)
568 scan_translation_unit_trad (cpp_in);
569 else
570 scan_translation_unit (cpp_in);
572 /* -dM command line option. */
573 if (gfc_cpp_preprocess_only () &&
574 gfc_cpp_option.dump_macros == 'M')
576 putc ('\n', print.outf);
577 cpp_forall_identifiers (cpp_in, dump_macro, NULL);
580 putc ('\n', print.outf);
582 if (!gfc_cpp_preprocess_only ()
583 || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
584 fclose (print.outf);
586 return SUCCESS;
589 void
590 gfc_cpp_done (void)
592 if (!gfc_cpp_enabled ())
593 return;
595 /* TODO: if dependency tracking was enabled, call
596 cpp_finish() here to write dependencies.
598 Use cpp_get_deps() to access the current source's
599 dependencies during parsing. Add dependencies using
600 the mkdeps-interface (defined in libcpp). */
602 gcc_assert (cpp_in);
603 cpp_undef_all (cpp_in);
604 cpp_clear_file_cache (cpp_in);
607 /* PATH must be malloc-ed and NULL-terminated. */
608 void
609 gfc_cpp_add_include_path (char *path, bool user_supplied)
611 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
612 include path. Fortran does not define any system include paths. */
613 int chain = 0;
614 int cxx_aware = 0;
616 add_path (path, chain, cxx_aware, user_supplied);
619 void
620 gfc_cpp_register_include_paths (void)
622 int cxx_stdinc = 0;
623 register_include_chains (cpp_in, gfc_cpp_option.sysroot,
624 gfc_cpp_option.prefix, gfc_cpp_option.multilib,
625 gfc_cpp_option.standard_include_paths, cxx_stdinc,
626 gfc_cpp_option.verbose);
631 static void scan_translation_unit_trad (cpp_reader *);
632 static void account_for_newlines (const unsigned char *, size_t);
633 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
635 static void print_line (source_location, const char *);
636 static void maybe_print_line (source_location);
639 /* Writes out the preprocessed file, handling spacing and paste
640 avoidance issues. */
641 static void
642 scan_translation_unit (cpp_reader *pfile)
644 bool avoid_paste = false;
646 print.source = NULL;
647 for (;;)
649 const cpp_token *token = cpp_get_token (pfile);
651 if (token->type == CPP_PADDING)
653 avoid_paste = true;
654 if (print.source == NULL
655 || (!(print.source->flags & PREV_WHITE)
656 && token->val.source == NULL))
657 print.source = token->val.source;
658 continue;
661 if (token->type == CPP_EOF)
662 break;
664 /* Subtle logic to output a space if and only if necessary. */
665 if (avoid_paste)
667 if (print.source == NULL)
668 print.source = token;
669 if (print.source->flags & PREV_WHITE
670 || (print.prev
671 && cpp_avoid_paste (pfile, print.prev, token))
672 || (print.prev == NULL && token->type == CPP_HASH))
673 putc (' ', print.outf);
675 else if (token->flags & PREV_WHITE)
676 putc (' ', print.outf);
678 avoid_paste = false;
679 print.source = NULL;
680 print.prev = token;
681 cpp_output_token (token, print.outf);
683 if (token->type == CPP_COMMENT)
684 account_for_newlines (token->val.str.text, token->val.str.len);
688 /* Adjust print.src_line for newlines embedded in output. */
689 static void
690 account_for_newlines (const unsigned char *str, size_t len)
692 while (len--)
693 if (*str++ == '\n')
694 print.src_line++;
697 /* Writes out a traditionally preprocessed file. */
698 static void
699 scan_translation_unit_trad (cpp_reader *pfile)
701 while (_cpp_read_logical_line_trad (pfile))
703 size_t len = pfile->out.cur - pfile->out.base;
704 maybe_print_line (pfile->out.first_line);
705 fwrite (pfile->out.base, 1, len, print.outf);
706 print.printed = 1;
707 if (!CPP_OPTION (pfile, discard_comments))
708 account_for_newlines (pfile->out.base, len);
712 /* If the token read on logical line LINE needs to be output on a
713 different line to the current one, output the required newlines or
714 a line marker. */
715 static void
716 maybe_print_line (source_location src_loc)
718 const struct line_map *map = linemap_lookup (line_table, src_loc);
719 int src_line = SOURCE_LINE (map, src_loc);
721 /* End the previous line of text. */
722 if (print.printed)
724 putc ('\n', print.outf);
725 print.src_line++;
726 print.printed = 0;
729 if (src_line >= print.src_line && src_line < print.src_line + 8)
731 while (src_line > print.src_line)
733 putc ('\n', print.outf);
734 print.src_line++;
737 else
738 print_line (src_loc, "");
741 /* Output a line marker for logical line LINE. Special flags are "1"
742 or "2" indicating entering or leaving a file. */
743 static void
744 print_line (source_location src_loc, const char *special_flags)
746 /* End any previous line of text. */
747 if (print.printed)
748 putc ('\n', print.outf);
749 print.printed = 0;
751 if (!gfc_cpp_option.no_line_commands)
753 const struct line_map *map = linemap_lookup (line_table, src_loc);
755 size_t to_file_len = strlen (map->to_file);
756 unsigned char *to_file_quoted =
757 (unsigned char *) alloca (to_file_len * 4 + 1);
758 unsigned char *p;
760 print.src_line = SOURCE_LINE (map, src_loc);
762 /* cpp_quote_string does not nul-terminate, so we have to do it
763 ourselves. */
764 p = cpp_quote_string (to_file_quoted,
765 (const unsigned char *) map->to_file, to_file_len);
766 *p = '\0';
767 fprintf (print.outf, "# %u \"%s\"%s",
768 print.src_line == 0 ? 1 : print.src_line,
769 to_file_quoted, special_flags);
771 if (map->sysp == 2)
772 fputs (" 3 4", print.outf);
773 else if (map->sysp == 1)
774 fputs (" 3", print.outf);
776 putc ('\n', print.outf);
780 static void
781 cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map)
783 const char *flags = "";
785 if (gfc_cpp_option.no_line_commands)
786 return;
788 if (!map)
789 return;
791 if (print.first_time)
793 /* Avoid printing foo.i when the main file is foo.c. */
794 if (!cpp_get_options (cpp_in)->preprocessed)
795 print_line (map->start_location, flags);
796 print.first_time = 0;
798 else
800 /* Bring current file to correct line when entering a new file. */
801 if (map->reason == LC_ENTER)
803 const struct line_map *from = INCLUDED_FROM (line_table, map);
804 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
806 if (map->reason == LC_ENTER)
807 flags = " 1";
808 else if (map->reason == LC_LEAVE)
809 flags = " 2";
810 print_line (map->start_location, flags);
815 /* Called when a line of output is started. TOKEN is the first token
816 of the line, and at end of file will be CPP_EOF. */
817 static void
818 cb_line_change (cpp_reader *pfile, const cpp_token *token,
819 int parsing_args)
821 source_location src_loc = token->src_loc;
823 if (token->type == CPP_EOF || parsing_args)
824 return;
826 maybe_print_line (src_loc);
827 print.prev = 0;
828 print.source = 0;
830 /* Supply enough spaces to put this token in its original column,
831 one space per column greater than 2, since scan_translation_unit
832 will provide a space if PREV_WHITE. Don't bother trying to
833 reconstruct tabs; we can't get it right in general, and nothing
834 ought to care. Some things do care; the fault lies with them. */
835 if (!CPP_OPTION (pfile, traditional))
837 const struct line_map *map = linemap_lookup (line_table, src_loc);
838 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
839 print.printed = 1;
841 while (-- spaces >= 0)
842 putc (' ', print.outf);
846 static void
847 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
848 const cpp_string *str)
850 maybe_print_line (line);
851 fprintf (print.outf, "#ident %s\n", str->text);
852 print.src_line++;
855 static void
856 cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
857 cpp_hashnode *node ATTRIBUTE_UNUSED)
859 maybe_print_line (line);
860 fputs ("#define ", print.outf);
862 /* 'D' is whole definition; 'N' is name only. */
863 if (gfc_cpp_option.dump_macros == 'D')
864 fputs ((const char *) cpp_macro_definition (pfile, node),
865 print.outf);
866 else
867 fputs ((const char *) NODE_NAME (node), print.outf);
869 putc ('\n', print.outf);
870 if (linemap_lookup (line_table, line)->to_line != 0)
871 print.src_line++;
874 static void
875 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
876 cpp_hashnode *node)
878 maybe_print_line (line);
879 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
880 print.src_line++;
883 static void
884 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
885 const unsigned char *dir, const char *header, int angle_brackets,
886 const cpp_token **comments)
888 maybe_print_line (line);
889 if (angle_brackets)
890 fprintf (print.outf, "#%s <%s>", dir, header);
891 else
892 fprintf (print.outf, "#%s \"%s\"", dir, header);
894 if (comments != NULL)
896 while (*comments != NULL)
898 if ((*comments)->flags & PREV_WHITE)
899 putc (' ', print.outf);
900 cpp_output_token (*comments, print.outf);
901 ++comments;
905 putc ('\n', print.outf);
906 print.src_line++;
909 /* Dump out the hash table. */
910 static int
911 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
913 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
915 fputs ("#define ", print.outf);
916 fputs ((const char *) cpp_macro_definition (pfile, node),
917 print.outf);
918 putc ('\n', print.outf);
919 print.src_line++;
922 return 1;
925 static void
926 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
927 cpp_hashnode *node)
929 gfc_cpp_macro_queue *q;
930 q = XNEW (gfc_cpp_macro_queue);
931 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
932 q->next = cpp_define_queue;
933 cpp_define_queue = q;
937 /* Callback called when -fworking-director and -E to emit working
938 directory in cpp output file. */
940 void
941 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
943 size_t to_file_len = strlen (dir);
944 unsigned char *to_file_quoted =
945 (unsigned char *) alloca (to_file_len * 4 + 1);
946 unsigned char *p;
948 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
949 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
950 *p = '\0';
951 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
954 /* Copy a #pragma directive to the preprocessed output. */
955 static void
956 cb_def_pragma (cpp_reader *pfile, source_location line)
958 maybe_print_line (line);
959 fputs ("#pragma ", print.outf);
960 cpp_output_line (pfile, print.outf);
961 print.src_line++;
964 static void
965 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
966 source_location line ATTRIBUTE_UNUSED,
967 cpp_hashnode *node)
969 gfc_cpp_macro_queue *q;
970 q = XNEW (gfc_cpp_macro_queue);
971 q->macro = xstrdup ((const char *) NODE_NAME (node));
972 q->next = cpp_undefine_queue;
973 cpp_undefine_queue = q;
976 static void
977 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
979 gfc_cpp_macro_queue *q;
981 /* End the previous line of text. */
982 if (print.printed)
984 putc ('\n', print.outf);
985 print.src_line++;
986 print.printed = 0;
989 for (q = cpp_define_queue; q;)
991 gfc_cpp_macro_queue *oq;
992 fputs ("#define ", print.outf);
993 fputs (q->macro, print.outf);
994 putc ('\n', print.outf);
995 print.src_line++;
996 oq = q;
997 q = q->next;
998 gfc_free (oq->macro);
999 gfc_free (oq);
1001 cpp_define_queue = NULL;
1002 for (q = cpp_undefine_queue; q;)
1004 gfc_cpp_macro_queue *oq;
1005 fprintf (print.outf, "#undef %s\n", q->macro);
1006 print.src_line++;
1007 oq = q;
1008 q = q->next;
1009 gfc_free (oq->macro);
1010 gfc_free (oq);
1012 cpp_undefine_queue = NULL;