2015-01-14 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / fortran / cpp.c
blobb30f90e05c50b961aff6aada840b318565ba94be
1 /* Copyright (C) 2008-2015 Free Software Foundation, Inc.
3 This file is part of GCC.
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.
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.
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/>. */
19 #include "config.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "tm.h"
23 #include "hash-set.h"
24 #include "machmode.h"
25 #include "vec.h"
26 #include "double-int.h"
27 #include "input.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "wide-int.h"
31 #include "inchash.h"
32 #include "tree.h"
33 #include "version.h"
34 #include "flags.h"
37 #include "options.h"
38 #include "gfortran.h"
39 #include "tm_p.h" /* Target prototypes. */
40 #include "target.h"
41 #include "toplev.h"
42 #include "diagnostic.h"
44 #include "../../libcpp/internal.h"
45 #include "cpp.h"
46 #include "incpath.h"
47 #include "cppbuiltin.h"
48 #include "mkdeps.h"
50 #ifndef TARGET_SYSTEM_ROOT
51 # define TARGET_SYSTEM_ROOT NULL
52 #endif
54 #ifndef TARGET_CPU_CPP_BUILTINS
55 # define TARGET_CPU_CPP_BUILTINS()
56 #endif
58 #ifndef TARGET_OS_CPP_BUILTINS
59 # define TARGET_OS_CPP_BUILTINS()
60 #endif
62 #ifndef TARGET_OBJFMT_CPP_BUILTINS
63 # define TARGET_OBJFMT_CPP_BUILTINS()
64 #endif
67 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
68 handling is deferred to gfc_cpp_init (). */
69 typedef struct
71 enum opt_code code;
72 const char *arg;
74 gfc_cpp_deferred_opt_t;
77 /* Defined and undefined macros being queued for output with -dU at
78 the next newline. */
79 typedef struct gfc_cpp_macro_queue
81 struct gfc_cpp_macro_queue *next; /* Next macro in the list. */
82 char *macro; /* The name of the macro if not
83 defined, the full definition if
84 defined. */
85 } gfc_cpp_macro_queue;
86 static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
88 struct gfc_cpp_option_data
90 /* Argument of -cpp, implied by SPEC;
91 if NULL, preprocessing disabled. */
92 const char *temporary_filename;
94 const char *output_filename; /* -o <arg> */
95 int preprocess_only; /* -E */
96 int discard_comments; /* -C */
97 int discard_comments_in_macro_exp; /* -CC */
98 int print_include_names; /* -H */
99 int no_line_commands; /* -P */
100 char dump_macros; /* -d[DMNU] */
101 int dump_includes; /* -dI */
102 int working_directory; /* -fworking-directory */
103 int no_predefined; /* -undef */
104 int standard_include_paths; /* -nostdinc */
105 int verbose; /* -v */
106 int deps; /* -M */
107 int deps_skip_system; /* -MM */
108 const char *deps_filename; /* -M[M]D */
109 const char *deps_filename_user; /* -MF <arg> */
110 int deps_missing_are_generated; /* -MG */
111 int deps_phony; /* -MP */
112 int warn_date_time; /* -Wdate-time */
114 const char *multilib; /* -imultilib <dir> */
115 const char *prefix; /* -iprefix <dir> */
116 const char *sysroot; /* -isysroot <dir> */
118 /* Options whose handling needs to be deferred until the
119 appropriate cpp-objects are created:
120 -A predicate=answer
121 -D <macro>[=<val>]
122 -U <macro> */
123 gfc_cpp_deferred_opt_t *deferred_opt;
124 int deferred_opt_count;
126 gfc_cpp_option;
128 /* Structures used with libcpp: */
129 static cpp_options *cpp_option = NULL;
130 static cpp_reader *cpp_in = NULL;
132 /* Encapsulates state used to convert a stream of cpp-tokens into
133 a text file. */
134 static struct
136 FILE *outf; /* Stream to write to. */
137 const cpp_token *prev; /* Previous token. */
138 const cpp_token *source; /* Source token for spacing. */
139 int src_line; /* Line number currently being written. */
140 unsigned char printed; /* Nonzero if something output at line. */
141 bool first_time; /* cb_file_change hasn't been called yet. */
142 } print;
144 /* General output routines. */
145 static void scan_translation_unit (cpp_reader *);
146 static void scan_translation_unit_trad (cpp_reader *);
148 /* Callback routines for the parser. Most of these are active only
149 in specific modes. */
150 static void cb_file_change (cpp_reader *, const struct line_map *);
151 static void cb_line_change (cpp_reader *, const cpp_token *, int);
152 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
153 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
154 static void cb_def_pragma (cpp_reader *, source_location);
155 static void cb_include (cpp_reader *, source_location, const unsigned char *,
156 const char *, int, const cpp_token **);
157 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
158 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
159 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
160 static bool cb_cpp_error (cpp_reader *, int, int, location_t, unsigned int,
161 const char *, va_list *)
162 ATTRIBUTE_GCC_DIAG(6,0);
163 void pp_dir_change (cpp_reader *, const char *);
165 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
166 static void dump_queued_macros (cpp_reader *);
169 static void
170 cpp_define_builtins (cpp_reader *pfile)
172 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
173 in C, defines __STDC_HOSTED__?! */
174 cpp_init_builtins (pfile, 0);
176 /* Initialize GFORTRAN specific builtins.
177 These are documented. */
178 define_language_independent_builtin_macros (pfile);
179 cpp_define (pfile, "__GFORTRAN__=1");
180 cpp_define (pfile, "_LANGUAGE_FORTRAN=1");
182 if (flag_openmp)
183 cpp_define (pfile, "_OPENMP=201307");
185 /* The defines below are necessary for the TARGET_* macros.
187 FIXME: Note that builtin_define_std() actually is a function
188 in c-cppbuiltin.c which uses flags undefined for Fortran.
189 Let's skip this for now. If needed, one needs to look into it
190 once more. */
192 # define builtin_define(TXT) cpp_define (pfile, TXT)
193 # define builtin_define_std(TXT)
194 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
196 /* FIXME: Pandora's Box
197 Using the macros below results in multiple breakages:
198 - mingw will fail to compile this file as dependent macros
199 assume to be used in c-cppbuiltin.c only. Further, they use
200 flags only valid/defined in C (same as noted above).
201 [config/i386/mingw32.h, config/i386/cygming.h]
202 - other platforms (not as popular) break similarly
203 [grep for 'builtin_define_with_int_value' in gcc/config/]
205 TARGET_CPU_CPP_BUILTINS ();
206 TARGET_OS_CPP_BUILTINS ();
207 TARGET_OBJFMT_CPP_BUILTINS (); */
209 #undef builtin_define
210 #undef builtin_define_std
211 #undef builtin_assert
214 bool
215 gfc_cpp_enabled (void)
217 return gfc_cpp_option.temporary_filename != NULL;
220 bool
221 gfc_cpp_preprocess_only (void)
223 return gfc_cpp_option.preprocess_only;
226 bool
227 gfc_cpp_makedep (void)
229 return gfc_cpp_option.deps;
232 void
233 gfc_cpp_add_dep (const char *name, bool system)
235 if (!gfc_cpp_option.deps_skip_system || !system)
236 deps_add_dep (cpp_get_deps (cpp_in), name);
239 void
240 gfc_cpp_add_target (const char *name)
242 deps_add_target (cpp_get_deps (cpp_in), name, 0);
246 const char *
247 gfc_cpp_temporary_file (void)
249 return gfc_cpp_option.temporary_filename;
252 void
253 gfc_cpp_init_options (unsigned int decoded_options_count,
254 struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
256 /* Do not create any objects from libcpp here. If no
257 preprocessing is requested, this would be wasted
258 time and effort.
260 See gfc_cpp_post_options() instead. */
262 gfc_cpp_option.temporary_filename = NULL;
263 gfc_cpp_option.output_filename = NULL;
264 gfc_cpp_option.preprocess_only = 0;
265 gfc_cpp_option.discard_comments = 1;
266 gfc_cpp_option.discard_comments_in_macro_exp = 1;
267 gfc_cpp_option.print_include_names = 0;
268 gfc_cpp_option.no_line_commands = 0;
269 gfc_cpp_option.dump_macros = '\0';
270 gfc_cpp_option.dump_includes = 0;
271 gfc_cpp_option.working_directory = -1;
272 gfc_cpp_option.no_predefined = 0;
273 gfc_cpp_option.standard_include_paths = 1;
274 gfc_cpp_option.verbose = 0;
275 gfc_cpp_option.warn_date_time = 0;
276 gfc_cpp_option.deps = 0;
277 gfc_cpp_option.deps_skip_system = 0;
278 gfc_cpp_option.deps_phony = 0;
279 gfc_cpp_option.deps_missing_are_generated = 0;
280 gfc_cpp_option.deps_filename = NULL;
281 gfc_cpp_option.deps_filename_user = NULL;
283 gfc_cpp_option.multilib = NULL;
284 gfc_cpp_option.prefix = NULL;
285 gfc_cpp_option.sysroot = TARGET_SYSTEM_ROOT;
287 gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t,
288 decoded_options_count);
289 gfc_cpp_option.deferred_opt_count = 0;
293 gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
295 int result = 1;
296 enum opt_code code = (enum opt_code) scode;
298 switch (code)
300 default:
301 result = 0;
302 break;
304 case OPT_cpp_:
305 gfc_cpp_option.temporary_filename = arg;
306 break;
308 case OPT_nocpp:
309 gfc_cpp_option.temporary_filename = 0L;
310 break;
312 case OPT_d:
313 for ( ; *arg; ++arg)
314 switch (*arg)
316 case 'D':
317 case 'M':
318 case 'N':
319 case 'U':
320 gfc_cpp_option.dump_macros = *arg;
321 break;
323 case 'I':
324 gfc_cpp_option.dump_includes = 1;
325 break;
327 break;
329 case OPT_fworking_directory:
330 gfc_cpp_option.working_directory = value;
331 break;
333 case OPT_idirafter:
334 gfc_cpp_add_include_path_after (xstrdup(arg), true);
335 break;
337 case OPT_imultilib:
338 gfc_cpp_option.multilib = arg;
339 break;
341 case OPT_iprefix:
342 gfc_cpp_option.prefix = arg;
343 break;
345 case OPT_isysroot:
346 gfc_cpp_option.sysroot = arg;
347 break;
349 case OPT_iquote:
350 case OPT_isystem:
351 gfc_cpp_add_include_path (xstrdup(arg), true);
352 break;
354 case OPT_nostdinc:
355 gfc_cpp_option.standard_include_paths = value;
356 break;
358 case OPT_o:
359 if (!gfc_cpp_option.output_filename)
360 gfc_cpp_option.output_filename = arg;
361 else
362 gfc_fatal_error ("output filename specified twice");
363 break;
365 case OPT_undef:
366 gfc_cpp_option.no_predefined = value;
367 break;
369 case OPT_v:
370 gfc_cpp_option.verbose = value;
371 break;
373 case OPT_Wdate_time:
374 gfc_cpp_option.warn_date_time = value;
375 break;
377 case OPT_A:
378 case OPT_D:
379 case OPT_U:
380 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
381 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
382 gfc_cpp_option.deferred_opt_count++;
383 break;
385 case OPT_C:
386 gfc_cpp_option.discard_comments = 0;
387 break;
389 case OPT_CC:
390 gfc_cpp_option.discard_comments = 0;
391 gfc_cpp_option.discard_comments_in_macro_exp = 0;
392 break;
394 case OPT_E:
395 gfc_cpp_option.preprocess_only = 1;
396 break;
398 case OPT_H:
399 gfc_cpp_option.print_include_names = 1;
400 break;
402 case OPT_MM:
403 gfc_cpp_option.deps_skip_system = 1;
404 /* fall through */
406 case OPT_M:
407 gfc_cpp_option.deps = 1;
408 break;
410 case OPT_MMD:
411 gfc_cpp_option.deps_skip_system = 1;
412 /* fall through */
414 case OPT_MD:
415 gfc_cpp_option.deps = 1;
416 gfc_cpp_option.deps_filename = arg;
417 break;
419 case OPT_MF:
420 /* If specified multiple times, last one wins. */
421 gfc_cpp_option.deps_filename_user = arg;
422 break;
424 case OPT_MG:
425 gfc_cpp_option.deps_missing_are_generated = 1;
426 break;
428 case OPT_MP:
429 gfc_cpp_option.deps_phony = 1;
430 break;
432 case OPT_MQ:
433 case OPT_MT:
434 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
435 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
436 gfc_cpp_option.deferred_opt_count++;
437 break;
439 case OPT_P:
440 gfc_cpp_option.no_line_commands = 1;
441 break;
444 return result;
448 void
449 gfc_cpp_post_options (void)
451 /* Any preprocessing-related option without '-cpp' is considered
452 an error. */
453 if (!gfc_cpp_enabled ()
454 && (gfc_cpp_preprocess_only ()
455 || gfc_cpp_makedep ()
456 || !gfc_cpp_option.discard_comments
457 || !gfc_cpp_option.discard_comments_in_macro_exp
458 || gfc_cpp_option.print_include_names
459 || gfc_cpp_option.no_line_commands
460 || gfc_cpp_option.dump_macros
461 || gfc_cpp_option.dump_includes))
462 gfc_fatal_error ("To enable preprocessing, use %<-cpp%>");
464 if (!gfc_cpp_enabled ())
465 return;
467 cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
468 gcc_assert (cpp_in);
470 /* The cpp_options-structure defines far more flags than those set here.
471 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
472 inter-option dependencies that may need to be enforced. */
473 cpp_option = cpp_get_options (cpp_in);
474 gcc_assert (cpp_option);
476 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
477 cpp_option->traditional = 1;
478 cpp_option->cplusplus_comments = 0;
480 cpp_option->cpp_pedantic = pedantic;
482 cpp_option->dollars_in_ident = flag_dollar_ok;
483 cpp_option->discard_comments = gfc_cpp_option.discard_comments;
484 cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
485 cpp_option->print_include_names = gfc_cpp_option.print_include_names;
486 cpp_option->preprocessed = gfc_option.flag_preprocessed;
487 cpp_option->warn_date_time = gfc_cpp_option.warn_date_time;
489 if (gfc_cpp_makedep ())
491 cpp_option->deps.style = DEPS_USER;
492 cpp_option->deps.phony_targets = gfc_cpp_option.deps_phony;
493 cpp_option->deps.missing_files = gfc_cpp_option.deps_missing_are_generated;
495 /* -MF <arg> overrides -M[M]D. */
496 if (gfc_cpp_option.deps_filename_user)
497 gfc_cpp_option.deps_filename = gfc_cpp_option.deps_filename_user;
500 if (gfc_cpp_option.working_directory == -1)
501 gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
503 cpp_post_options (cpp_in);
505 gfc_cpp_register_include_paths ();
509 void
510 gfc_cpp_init_0 (void)
512 struct cpp_callbacks *cb;
514 cb = cpp_get_callbacks (cpp_in);
515 cb->file_change = cb_file_change;
516 cb->line_change = cb_line_change;
517 cb->ident = cb_ident;
518 cb->def_pragma = cb_def_pragma;
519 cb->error = cb_cpp_error;
521 if (gfc_cpp_option.dump_includes)
522 cb->include = cb_include;
524 if ((gfc_cpp_option.dump_macros == 'D')
525 || (gfc_cpp_option.dump_macros == 'N'))
527 cb->define = cb_define;
528 cb->undef = cb_undef;
531 if (gfc_cpp_option.dump_macros == 'U')
533 cb->before_define = dump_queued_macros;
534 cb->used_define = cb_used_define;
535 cb->used_undef = cb_used_undef;
538 /* Initialize the print structure. Setting print.src_line to -1 here is
539 a trick to guarantee that the first token of the file will cause
540 a linemarker to be output by maybe_print_line. */
541 print.src_line = -1;
542 print.printed = 0;
543 print.prev = 0;
544 print.first_time = 1;
546 if (gfc_cpp_preprocess_only ())
548 if (gfc_cpp_option.output_filename)
550 /* This needs cheating: with "-E -o <file>", the user wants the
551 preprocessed output in <file>. However, if nothing is done
552 about it <file> is also used for assembler output. Hence, it
553 is necessary to redirect assembler output (actually nothing
554 as -E implies -fsyntax-only) to another file, otherwise the
555 output from preprocessing is lost. */
556 asm_file_name = gfc_cpp_option.temporary_filename;
558 print.outf = fopen (gfc_cpp_option.output_filename, "w");
559 if (print.outf == NULL)
560 gfc_fatal_error ("opening output file %qs: %s",
561 gfc_cpp_option.output_filename,
562 xstrerror (errno));
564 else
565 print.outf = stdout;
567 else
569 print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
570 if (print.outf == NULL)
571 gfc_fatal_error ("opening output file %qs: %s",
572 gfc_cpp_option.temporary_filename, xstrerror (errno));
575 gcc_assert(cpp_in);
576 if (!cpp_read_main_file (cpp_in, gfc_source_file))
577 errorcount++;
580 void
581 gfc_cpp_init (void)
583 int i;
585 if (gfc_option.flag_preprocessed)
586 return;
588 cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
589 if (!gfc_cpp_option.no_predefined)
591 /* Make sure all of the builtins about to be declared have
592 BUILTINS_LOCATION has their source_location. */
593 source_location builtins_loc = BUILTINS_LOCATION;
594 cpp_force_token_locations (cpp_in, &builtins_loc);
596 cpp_define_builtins (cpp_in);
598 cpp_stop_forcing_token_locations (cpp_in);
601 /* Handle deferred options from command-line. */
602 cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
604 for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
606 gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
608 if (opt->code == OPT_D)
609 cpp_define (cpp_in, opt->arg);
610 else if (opt->code == OPT_U)
611 cpp_undef (cpp_in, opt->arg);
612 else if (opt->code == OPT_A)
614 if (opt->arg[0] == '-')
615 cpp_unassert (cpp_in, opt->arg + 1);
616 else
617 cpp_assert (cpp_in, opt->arg);
619 else if (opt->code == OPT_MT || opt->code == OPT_MQ)
620 deps_add_target (cpp_get_deps (cpp_in),
621 opt->arg, opt->code == OPT_MQ);
624 if (gfc_cpp_option.working_directory
625 && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
626 pp_dir_change (cpp_in, get_src_pwd ());
629 bool
630 gfc_cpp_preprocess (const char *source_file)
632 if (!gfc_cpp_enabled ())
633 return false;
635 cpp_change_file (cpp_in, LC_RENAME, source_file);
637 if (cpp_option->traditional)
638 scan_translation_unit_trad (cpp_in);
639 else
640 scan_translation_unit (cpp_in);
642 /* -dM command line option. */
643 if (gfc_cpp_preprocess_only () &&
644 gfc_cpp_option.dump_macros == 'M')
646 putc ('\n', print.outf);
647 cpp_forall_identifiers (cpp_in, dump_macro, NULL);
650 putc ('\n', print.outf);
652 if (!gfc_cpp_preprocess_only ()
653 || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
654 fclose (print.outf);
656 return true;
659 void
660 gfc_cpp_done (void)
662 if (!gfc_cpp_enabled ())
663 return;
665 gcc_assert (cpp_in);
667 if (gfc_cpp_makedep ())
669 if (gfc_cpp_option.deps_filename)
671 FILE *f = fopen (gfc_cpp_option.deps_filename, "w");
672 if (f)
674 cpp_finish (cpp_in, f);
675 fclose (f);
677 else
678 gfc_fatal_error ("opening output file %qs: %s",
679 gfc_cpp_option.deps_filename,
680 xstrerror (errno));
682 else
683 cpp_finish (cpp_in, stdout);
686 cpp_undef_all (cpp_in);
687 cpp_clear_file_cache (cpp_in);
690 /* PATH must be malloc-ed and NULL-terminated. */
691 void
692 gfc_cpp_add_include_path (char *path, bool user_supplied)
694 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
695 include path. Fortran does not define any system include paths. */
696 int cxx_aware = 0;
698 add_path (path, BRACKET, cxx_aware, user_supplied);
701 void
702 gfc_cpp_add_include_path_after (char *path, bool user_supplied)
704 int cxx_aware = 0;
705 add_path (path, AFTER, cxx_aware, user_supplied);
708 void
709 gfc_cpp_register_include_paths (void)
711 int cxx_stdinc = 0;
712 register_include_chains (cpp_in, gfc_cpp_option.sysroot,
713 gfc_cpp_option.prefix, gfc_cpp_option.multilib,
714 gfc_cpp_option.standard_include_paths, cxx_stdinc,
715 gfc_cpp_option.verbose);
720 static void scan_translation_unit_trad (cpp_reader *);
721 static void account_for_newlines (const unsigned char *, size_t);
722 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
724 static void print_line (source_location, const char *);
725 static void maybe_print_line (source_location);
728 /* Writes out the preprocessed file, handling spacing and paste
729 avoidance issues. */
730 static void
731 scan_translation_unit (cpp_reader *pfile)
733 bool avoid_paste = false;
735 print.source = NULL;
736 for (;;)
738 const cpp_token *token = cpp_get_token (pfile);
740 if (token->type == CPP_PADDING)
742 avoid_paste = true;
743 if (print.source == NULL
744 || (!(print.source->flags & PREV_WHITE)
745 && token->val.source == NULL))
746 print.source = token->val.source;
747 continue;
750 if (token->type == CPP_EOF)
751 break;
753 /* Subtle logic to output a space if and only if necessary. */
754 if (avoid_paste)
756 if (print.source == NULL)
757 print.source = token;
758 if (print.source->flags & PREV_WHITE
759 || (print.prev
760 && cpp_avoid_paste (pfile, print.prev, token))
761 || (print.prev == NULL && token->type == CPP_HASH))
762 putc (' ', print.outf);
764 else if (token->flags & PREV_WHITE)
765 putc (' ', print.outf);
767 avoid_paste = false;
768 print.source = NULL;
769 print.prev = token;
770 cpp_output_token (token, print.outf);
772 if (token->type == CPP_COMMENT)
773 account_for_newlines (token->val.str.text, token->val.str.len);
777 /* Adjust print.src_line for newlines embedded in output. */
778 static void
779 account_for_newlines (const unsigned char *str, size_t len)
781 while (len--)
782 if (*str++ == '\n')
783 print.src_line++;
786 /* Writes out a traditionally preprocessed file. */
787 static void
788 scan_translation_unit_trad (cpp_reader *pfile)
790 while (_cpp_read_logical_line_trad (pfile))
792 size_t len = pfile->out.cur - pfile->out.base;
793 maybe_print_line (pfile->out.first_line);
794 fwrite (pfile->out.base, 1, len, print.outf);
795 print.printed = 1;
796 if (!CPP_OPTION (pfile, discard_comments))
797 account_for_newlines (pfile->out.base, len);
801 /* If the token read on logical line LINE needs to be output on a
802 different line to the current one, output the required newlines or
803 a line marker. */
804 static void
805 maybe_print_line (source_location src_loc)
807 const struct line_map *map = linemap_lookup (line_table, src_loc);
808 int src_line = SOURCE_LINE (map, src_loc);
810 /* End the previous line of text. */
811 if (print.printed)
813 putc ('\n', print.outf);
814 print.src_line++;
815 print.printed = 0;
818 if (src_line >= print.src_line && src_line < print.src_line + 8)
820 while (src_line > print.src_line)
822 putc ('\n', print.outf);
823 print.src_line++;
826 else
827 print_line (src_loc, "");
830 /* Output a line marker for logical line LINE. Special flags are "1"
831 or "2" indicating entering or leaving a file. */
832 static void
833 print_line (source_location src_loc, const char *special_flags)
835 /* End any previous line of text. */
836 if (print.printed)
837 putc ('\n', print.outf);
838 print.printed = 0;
840 if (!gfc_cpp_option.no_line_commands)
842 expanded_location loc;
843 size_t to_file_len;
844 unsigned char *to_file_quoted;
845 unsigned char *p;
846 int sysp;
848 loc = expand_location (src_loc);
849 to_file_len = strlen (loc.file);
850 to_file_quoted = (unsigned char *) alloca (to_file_len * 4 + 1);
852 print.src_line = loc.line;
854 /* cpp_quote_string does not nul-terminate, so we have to do it
855 ourselves. */
856 p = cpp_quote_string (to_file_quoted,
857 (const unsigned char *) loc.file, to_file_len);
858 *p = '\0';
859 fprintf (print.outf, "# %u \"%s\"%s",
860 print.src_line == 0 ? 1 : print.src_line,
861 to_file_quoted, special_flags);
863 sysp = in_system_header_at (src_loc);
864 if (sysp == 2)
865 fputs (" 3 4", print.outf);
866 else if (sysp == 1)
867 fputs (" 3", print.outf);
869 putc ('\n', print.outf);
873 static void
874 cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map)
876 const char *flags = "";
878 if (gfc_cpp_option.no_line_commands)
879 return;
881 if (!map)
882 return;
884 if (print.first_time)
886 /* Avoid printing foo.i when the main file is foo.c. */
887 if (!cpp_get_options (cpp_in)->preprocessed)
888 print_line (map->start_location, flags);
889 print.first_time = 0;
891 else
893 /* Bring current file to correct line when entering a new file. */
894 if (map->reason == LC_ENTER)
896 const struct line_map *from = INCLUDED_FROM (line_table, map);
897 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
899 if (map->reason == LC_ENTER)
900 flags = " 1";
901 else if (map->reason == LC_LEAVE)
902 flags = " 2";
903 print_line (map->start_location, flags);
908 /* Called when a line of output is started. TOKEN is the first token
909 of the line, and at end of file will be CPP_EOF. */
910 static void
911 cb_line_change (cpp_reader *pfile, const cpp_token *token,
912 int parsing_args)
914 source_location src_loc = token->src_loc;
916 if (token->type == CPP_EOF || parsing_args)
917 return;
919 maybe_print_line (src_loc);
920 print.prev = 0;
921 print.source = 0;
923 /* Supply enough spaces to put this token in its original column,
924 one space per column greater than 2, since scan_translation_unit
925 will provide a space if PREV_WHITE. Don't bother trying to
926 reconstruct tabs; we can't get it right in general, and nothing
927 ought to care. Some things do care; the fault lies with them. */
928 if (!CPP_OPTION (pfile, traditional))
930 const struct line_map *map = linemap_lookup (line_table, src_loc);
931 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
932 print.printed = 1;
934 while (-- spaces >= 0)
935 putc (' ', print.outf);
939 static void
940 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
941 const cpp_string *str)
943 maybe_print_line (line);
944 fprintf (print.outf, "#ident %s\n", str->text);
945 print.src_line++;
948 static void
949 cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
950 cpp_hashnode *node ATTRIBUTE_UNUSED)
952 maybe_print_line (line);
953 fputs ("#define ", print.outf);
955 /* 'D' is whole definition; 'N' is name only. */
956 if (gfc_cpp_option.dump_macros == 'D')
957 fputs ((const char *) cpp_macro_definition (pfile, node),
958 print.outf);
959 else
960 fputs ((const char *) NODE_NAME (node), print.outf);
962 putc ('\n', print.outf);
963 if (LOCATION_LINE (line) != 0)
964 print.src_line++;
967 static void
968 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
969 cpp_hashnode *node)
971 maybe_print_line (line);
972 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
973 print.src_line++;
976 static void
977 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
978 const unsigned char *dir, const char *header, int angle_brackets,
979 const cpp_token **comments)
981 maybe_print_line (line);
982 if (angle_brackets)
983 fprintf (print.outf, "#%s <%s>", dir, header);
984 else
985 fprintf (print.outf, "#%s \"%s\"", dir, header);
987 if (comments != NULL)
989 while (*comments != NULL)
991 if ((*comments)->flags & PREV_WHITE)
992 putc (' ', print.outf);
993 cpp_output_token (*comments, print.outf);
994 ++comments;
998 putc ('\n', print.outf);
999 print.src_line++;
1002 /* Dump out the hash table. */
1003 static int
1004 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
1006 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1008 fputs ("#define ", print.outf);
1009 fputs ((const char *) cpp_macro_definition (pfile, node),
1010 print.outf);
1011 putc ('\n', print.outf);
1012 print.src_line++;
1015 return 1;
1018 static void
1019 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
1020 cpp_hashnode *node)
1022 gfc_cpp_macro_queue *q;
1023 q = XNEW (gfc_cpp_macro_queue);
1024 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
1025 q->next = cpp_define_queue;
1026 cpp_define_queue = q;
1029 /* Callback from cpp_error for PFILE to print diagnostics from the
1030 preprocessor. The diagnostic is of type LEVEL, with REASON set
1031 to the reason code if LEVEL is represents a warning, at location
1032 LOCATION, with column number possibly overridden by COLUMN_OVERRIDE
1033 if not zero; MSG is the translated message and AP the arguments.
1034 Returns true if a diagnostic was emitted, false otherwise. */
1036 static bool
1037 cb_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
1038 location_t location, unsigned int column_override,
1039 const char *msg, va_list *ap)
1041 diagnostic_info diagnostic;
1042 diagnostic_t dlevel;
1043 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
1044 bool ret;
1046 switch (level)
1048 case CPP_DL_WARNING_SYSHDR:
1049 global_dc->dc_warn_system_headers = 1;
1050 /* Fall through. */
1051 case CPP_DL_WARNING:
1052 dlevel = DK_WARNING;
1053 break;
1054 case CPP_DL_PEDWARN:
1055 dlevel = DK_PEDWARN;
1056 break;
1057 case CPP_DL_ERROR:
1058 dlevel = DK_ERROR;
1059 break;
1060 case CPP_DL_ICE:
1061 dlevel = DK_ICE;
1062 break;
1063 case CPP_DL_NOTE:
1064 dlevel = DK_NOTE;
1065 break;
1066 case CPP_DL_FATAL:
1067 dlevel = DK_FATAL;
1068 break;
1069 default:
1070 gcc_unreachable ();
1072 diagnostic_set_info_translated (&diagnostic, msg, ap,
1073 location, dlevel);
1074 if (column_override)
1075 diagnostic_override_column (&diagnostic, column_override);
1076 if (reason == CPP_W_WARNING_DIRECTIVE)
1077 diagnostic_override_option_index (&diagnostic, OPT_Wcpp);
1078 ret = report_diagnostic (&diagnostic);
1079 if (level == CPP_DL_WARNING_SYSHDR)
1080 global_dc->dc_warn_system_headers = save_warn_system_headers;
1081 return ret;
1084 /* Callback called when -fworking-director and -E to emit working
1085 directory in cpp output file. */
1087 void
1088 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1090 size_t to_file_len = strlen (dir);
1091 unsigned char *to_file_quoted =
1092 (unsigned char *) alloca (to_file_len * 4 + 1);
1093 unsigned char *p;
1095 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
1096 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
1097 *p = '\0';
1098 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
1101 /* Copy a #pragma directive to the preprocessed output. */
1102 static void
1103 cb_def_pragma (cpp_reader *pfile, source_location line)
1105 maybe_print_line (line);
1106 fputs ("#pragma ", print.outf);
1107 cpp_output_line (pfile, print.outf);
1108 print.src_line++;
1111 static void
1112 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
1113 source_location line ATTRIBUTE_UNUSED,
1114 cpp_hashnode *node)
1116 gfc_cpp_macro_queue *q;
1117 q = XNEW (gfc_cpp_macro_queue);
1118 q->macro = xstrdup ((const char *) NODE_NAME (node));
1119 q->next = cpp_undefine_queue;
1120 cpp_undefine_queue = q;
1123 static void
1124 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
1126 gfc_cpp_macro_queue *q;
1128 /* End the previous line of text. */
1129 if (print.printed)
1131 putc ('\n', print.outf);
1132 print.src_line++;
1133 print.printed = 0;
1136 for (q = cpp_define_queue; q;)
1138 gfc_cpp_macro_queue *oq;
1139 fputs ("#define ", print.outf);
1140 fputs (q->macro, print.outf);
1141 putc ('\n', print.outf);
1142 print.src_line++;
1143 oq = q;
1144 q = q->next;
1145 free (oq->macro);
1146 free (oq);
1148 cpp_define_queue = NULL;
1149 for (q = cpp_undefine_queue; q;)
1151 gfc_cpp_macro_queue *oq;
1152 fprintf (print.outf, "#undef %s\n", q->macro);
1153 print.src_line++;
1154 oq = q;
1155 q = q->next;
1156 free (oq->macro);
1157 free (oq);
1159 cpp_undefine_queue = NULL;