* tree-ssa-loop-niter.c (refine_value_range_using_guard): New.
[official-gcc.git] / gcc / fortran / cpp.c
blobdaffc2069bf44d5a8f786810e72ec23446ae07a9
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 "alias.h"
24 #include "tree.h"
25 #include "version.h"
26 #include "flags.h"
29 #include "options.h"
30 #include "gfortran.h"
31 #include "tm_p.h" /* Target prototypes. */
32 #include "target.h"
33 #include "toplev.h"
34 #include "diagnostic.h"
36 #include "../../libcpp/internal.h"
37 #include "cpp.h"
38 #include "incpath.h"
39 #include "cppbuiltin.h"
40 #include "mkdeps.h"
42 #ifndef TARGET_SYSTEM_ROOT
43 # define TARGET_SYSTEM_ROOT NULL
44 #endif
46 #ifndef TARGET_CPU_CPP_BUILTINS
47 # define TARGET_CPU_CPP_BUILTINS()
48 #endif
50 #ifndef TARGET_OS_CPP_BUILTINS
51 # define TARGET_OS_CPP_BUILTINS()
52 #endif
54 #ifndef TARGET_OBJFMT_CPP_BUILTINS
55 # define TARGET_OBJFMT_CPP_BUILTINS()
56 #endif
59 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
60 handling is deferred to gfc_cpp_init (). */
61 typedef struct
63 enum opt_code code;
64 const char *arg;
66 gfc_cpp_deferred_opt_t;
69 /* Defined and undefined macros being queued for output with -dU at
70 the next newline. */
71 typedef struct gfc_cpp_macro_queue
73 struct gfc_cpp_macro_queue *next; /* Next macro in the list. */
74 char *macro; /* The name of the macro if not
75 defined, the full definition if
76 defined. */
77 } gfc_cpp_macro_queue;
78 static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
80 struct gfc_cpp_option_data
82 /* Argument of -cpp, implied by SPEC;
83 if NULL, preprocessing disabled. */
84 const char *temporary_filename;
86 const char *output_filename; /* -o <arg> */
87 int preprocess_only; /* -E */
88 int discard_comments; /* -C */
89 int discard_comments_in_macro_exp; /* -CC */
90 int print_include_names; /* -H */
91 int no_line_commands; /* -P */
92 char dump_macros; /* -d[DMNU] */
93 int dump_includes; /* -dI */
94 int working_directory; /* -fworking-directory */
95 int no_predefined; /* -undef */
96 int standard_include_paths; /* -nostdinc */
97 int verbose; /* -v */
98 int deps; /* -M */
99 int deps_skip_system; /* -MM */
100 const char *deps_filename; /* -M[M]D */
101 const char *deps_filename_user; /* -MF <arg> */
102 int deps_missing_are_generated; /* -MG */
103 int deps_phony; /* -MP */
104 int warn_date_time; /* -Wdate-time */
106 const char *multilib; /* -imultilib <dir> */
107 const char *prefix; /* -iprefix <dir> */
108 const char *sysroot; /* -isysroot <dir> */
110 /* Options whose handling needs to be deferred until the
111 appropriate cpp-objects are created:
112 -A predicate=answer
113 -D <macro>[=<val>]
114 -U <macro> */
115 gfc_cpp_deferred_opt_t *deferred_opt;
116 int deferred_opt_count;
118 gfc_cpp_option;
120 /* Structures used with libcpp: */
121 static cpp_options *cpp_option = NULL;
122 static cpp_reader *cpp_in = NULL;
124 /* Encapsulates state used to convert a stream of cpp-tokens into
125 a text file. */
126 static struct
128 FILE *outf; /* Stream to write to. */
129 const cpp_token *prev; /* Previous token. */
130 const cpp_token *source; /* Source token for spacing. */
131 int src_line; /* Line number currently being written. */
132 unsigned char printed; /* Nonzero if something output at line. */
133 bool first_time; /* cb_file_change hasn't been called yet. */
134 } print;
136 /* General output routines. */
137 static void scan_translation_unit (cpp_reader *);
138 static void scan_translation_unit_trad (cpp_reader *);
140 /* Callback routines for the parser. Most of these are active only
141 in specific modes. */
142 static void cb_file_change (cpp_reader *, const line_map_ordinary *);
143 static void cb_line_change (cpp_reader *, const cpp_token *, int);
144 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
145 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
146 static void cb_def_pragma (cpp_reader *, source_location);
147 static void cb_include (cpp_reader *, source_location, const unsigned char *,
148 const char *, int, const cpp_token **);
149 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
150 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
151 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
152 static bool cb_cpp_error (cpp_reader *, int, int, location_t, unsigned int,
153 const char *, va_list *)
154 ATTRIBUTE_GCC_DIAG(6,0);
155 void pp_dir_change (cpp_reader *, const char *);
157 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
158 static void dump_queued_macros (cpp_reader *);
161 static void
162 cpp_define_builtins (cpp_reader *pfile)
164 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
165 in C, defines __STDC_HOSTED__?! */
166 cpp_init_builtins (pfile, 0);
168 /* Initialize GFORTRAN specific builtins.
169 These are documented. */
170 define_language_independent_builtin_macros (pfile);
171 cpp_define (pfile, "__GFORTRAN__=1");
172 cpp_define (pfile, "_LANGUAGE_FORTRAN=1");
174 if (flag_openacc)
175 cpp_define (pfile, "_OPENACC=201306");
177 if (flag_openmp)
178 cpp_define (pfile, "_OPENMP=201307");
180 /* The defines below are necessary for the TARGET_* macros.
182 FIXME: Note that builtin_define_std() actually is a function
183 in c-cppbuiltin.c which uses flags undefined for Fortran.
184 Let's skip this for now. If needed, one needs to look into it
185 once more. */
187 # define builtin_define(TXT) cpp_define (pfile, TXT)
188 # define builtin_define_std(TXT)
189 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
191 /* FIXME: Pandora's Box
192 Using the macros below results in multiple breakages:
193 - mingw will fail to compile this file as dependent macros
194 assume to be used in c-cppbuiltin.c only. Further, they use
195 flags only valid/defined in C (same as noted above).
196 [config/i386/mingw32.h, config/i386/cygming.h]
197 - other platforms (not as popular) break similarly
198 [grep for 'builtin_define_with_int_value' in gcc/config/]
200 TARGET_CPU_CPP_BUILTINS ();
201 TARGET_OS_CPP_BUILTINS ();
202 TARGET_OBJFMT_CPP_BUILTINS (); */
204 #undef builtin_define
205 #undef builtin_define_std
206 #undef builtin_assert
209 bool
210 gfc_cpp_enabled (void)
212 return gfc_cpp_option.temporary_filename != NULL;
215 bool
216 gfc_cpp_preprocess_only (void)
218 return gfc_cpp_option.preprocess_only;
221 bool
222 gfc_cpp_makedep (void)
224 return gfc_cpp_option.deps;
227 void
228 gfc_cpp_add_dep (const char *name, bool system)
230 if (!gfc_cpp_option.deps_skip_system || !system)
231 deps_add_dep (cpp_get_deps (cpp_in), name);
234 void
235 gfc_cpp_add_target (const char *name)
237 deps_add_target (cpp_get_deps (cpp_in), name, 0);
241 const char *
242 gfc_cpp_temporary_file (void)
244 return gfc_cpp_option.temporary_filename;
247 void
248 gfc_cpp_init_options (unsigned int decoded_options_count,
249 struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
251 /* Do not create any objects from libcpp here. If no
252 preprocessing is requested, this would be wasted
253 time and effort.
255 See gfc_cpp_post_options() instead. */
257 gfc_cpp_option.temporary_filename = NULL;
258 gfc_cpp_option.output_filename = NULL;
259 gfc_cpp_option.preprocess_only = 0;
260 gfc_cpp_option.discard_comments = 1;
261 gfc_cpp_option.discard_comments_in_macro_exp = 1;
262 gfc_cpp_option.print_include_names = 0;
263 gfc_cpp_option.no_line_commands = 0;
264 gfc_cpp_option.dump_macros = '\0';
265 gfc_cpp_option.dump_includes = 0;
266 gfc_cpp_option.working_directory = -1;
267 gfc_cpp_option.no_predefined = 0;
268 gfc_cpp_option.standard_include_paths = 1;
269 gfc_cpp_option.verbose = 0;
270 gfc_cpp_option.warn_date_time = 0;
271 gfc_cpp_option.deps = 0;
272 gfc_cpp_option.deps_skip_system = 0;
273 gfc_cpp_option.deps_phony = 0;
274 gfc_cpp_option.deps_missing_are_generated = 0;
275 gfc_cpp_option.deps_filename = NULL;
276 gfc_cpp_option.deps_filename_user = NULL;
278 gfc_cpp_option.multilib = NULL;
279 gfc_cpp_option.prefix = NULL;
280 gfc_cpp_option.sysroot = TARGET_SYSTEM_ROOT;
282 gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t,
283 decoded_options_count);
284 gfc_cpp_option.deferred_opt_count = 0;
288 gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
290 int result = 1;
291 enum opt_code code = (enum opt_code) scode;
293 switch (code)
295 default:
296 result = 0;
297 break;
299 case OPT_cpp_:
300 gfc_cpp_option.temporary_filename = arg;
301 break;
303 case OPT_nocpp:
304 gfc_cpp_option.temporary_filename = 0L;
305 break;
307 case OPT_d:
308 for ( ; *arg; ++arg)
309 switch (*arg)
311 case 'D':
312 case 'M':
313 case 'N':
314 case 'U':
315 gfc_cpp_option.dump_macros = *arg;
316 break;
318 case 'I':
319 gfc_cpp_option.dump_includes = 1;
320 break;
322 break;
324 case OPT_fworking_directory:
325 gfc_cpp_option.working_directory = value;
326 break;
328 case OPT_idirafter:
329 gfc_cpp_add_include_path_after (xstrdup(arg), true);
330 break;
332 case OPT_imultilib:
333 gfc_cpp_option.multilib = arg;
334 break;
336 case OPT_iprefix:
337 gfc_cpp_option.prefix = arg;
338 break;
340 case OPT_isysroot:
341 gfc_cpp_option.sysroot = arg;
342 break;
344 case OPT_iquote:
345 case OPT_isystem:
346 gfc_cpp_add_include_path (xstrdup(arg), true);
347 break;
349 case OPT_nostdinc:
350 gfc_cpp_option.standard_include_paths = value;
351 break;
353 case OPT_o:
354 if (!gfc_cpp_option.output_filename)
355 gfc_cpp_option.output_filename = arg;
356 else
357 gfc_fatal_error ("output filename specified twice");
358 break;
360 case OPT_undef:
361 gfc_cpp_option.no_predefined = value;
362 break;
364 case OPT_v:
365 gfc_cpp_option.verbose = value;
366 break;
368 case OPT_Wdate_time:
369 gfc_cpp_option.warn_date_time = value;
370 break;
372 case OPT_A:
373 case OPT_D:
374 case OPT_U:
375 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
376 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
377 gfc_cpp_option.deferred_opt_count++;
378 break;
380 case OPT_C:
381 gfc_cpp_option.discard_comments = 0;
382 break;
384 case OPT_CC:
385 gfc_cpp_option.discard_comments = 0;
386 gfc_cpp_option.discard_comments_in_macro_exp = 0;
387 break;
389 case OPT_E:
390 gfc_cpp_option.preprocess_only = 1;
391 break;
393 case OPT_H:
394 gfc_cpp_option.print_include_names = 1;
395 break;
397 case OPT_MM:
398 gfc_cpp_option.deps_skip_system = 1;
399 /* fall through */
401 case OPT_M:
402 gfc_cpp_option.deps = 1;
403 break;
405 case OPT_MMD:
406 gfc_cpp_option.deps_skip_system = 1;
407 /* fall through */
409 case OPT_MD:
410 gfc_cpp_option.deps = 1;
411 gfc_cpp_option.deps_filename = arg;
412 break;
414 case OPT_MF:
415 /* If specified multiple times, last one wins. */
416 gfc_cpp_option.deps_filename_user = arg;
417 break;
419 case OPT_MG:
420 gfc_cpp_option.deps_missing_are_generated = 1;
421 break;
423 case OPT_MP:
424 gfc_cpp_option.deps_phony = 1;
425 break;
427 case OPT_MQ:
428 case OPT_MT:
429 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
430 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
431 gfc_cpp_option.deferred_opt_count++;
432 break;
434 case OPT_P:
435 gfc_cpp_option.no_line_commands = 1;
436 break;
439 return result;
443 void
444 gfc_cpp_post_options (void)
446 /* Any preprocessing-related option without '-cpp' is considered
447 an error. */
448 if (!gfc_cpp_enabled ()
449 && (gfc_cpp_preprocess_only ()
450 || gfc_cpp_makedep ()
451 || !gfc_cpp_option.discard_comments
452 || !gfc_cpp_option.discard_comments_in_macro_exp
453 || gfc_cpp_option.print_include_names
454 || gfc_cpp_option.no_line_commands
455 || gfc_cpp_option.dump_macros
456 || gfc_cpp_option.dump_includes))
457 gfc_fatal_error ("To enable preprocessing, use %<-cpp%>");
459 if (!gfc_cpp_enabled ())
460 return;
462 cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
463 gcc_assert (cpp_in);
465 /* The cpp_options-structure defines far more flags than those set here.
466 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
467 inter-option dependencies that may need to be enforced. */
468 cpp_option = cpp_get_options (cpp_in);
469 gcc_assert (cpp_option);
471 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
472 cpp_option->traditional = 1;
473 cpp_option->cplusplus_comments = 0;
475 cpp_option->cpp_pedantic = pedantic;
477 cpp_option->dollars_in_ident = flag_dollar_ok;
478 cpp_option->discard_comments = gfc_cpp_option.discard_comments;
479 cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
480 cpp_option->print_include_names = gfc_cpp_option.print_include_names;
481 cpp_option->preprocessed = gfc_option.flag_preprocessed;
482 cpp_option->warn_date_time = gfc_cpp_option.warn_date_time;
484 if (gfc_cpp_makedep ())
486 cpp_option->deps.style = DEPS_USER;
487 cpp_option->deps.phony_targets = gfc_cpp_option.deps_phony;
488 cpp_option->deps.missing_files = gfc_cpp_option.deps_missing_are_generated;
490 /* -MF <arg> overrides -M[M]D. */
491 if (gfc_cpp_option.deps_filename_user)
492 gfc_cpp_option.deps_filename = gfc_cpp_option.deps_filename_user;
495 if (gfc_cpp_option.working_directory == -1)
496 gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
498 cpp_post_options (cpp_in);
500 gfc_cpp_register_include_paths ();
504 void
505 gfc_cpp_init_0 (void)
507 struct cpp_callbacks *cb;
509 cb = cpp_get_callbacks (cpp_in);
510 cb->file_change = cb_file_change;
511 cb->line_change = cb_line_change;
512 cb->ident = cb_ident;
513 cb->def_pragma = cb_def_pragma;
514 cb->error = cb_cpp_error;
516 if (gfc_cpp_option.dump_includes)
517 cb->include = cb_include;
519 if ((gfc_cpp_option.dump_macros == 'D')
520 || (gfc_cpp_option.dump_macros == 'N'))
522 cb->define = cb_define;
523 cb->undef = cb_undef;
526 if (gfc_cpp_option.dump_macros == 'U')
528 cb->before_define = dump_queued_macros;
529 cb->used_define = cb_used_define;
530 cb->used_undef = cb_used_undef;
533 /* Initialize the print structure. Setting print.src_line to -1 here is
534 a trick to guarantee that the first token of the file will cause
535 a linemarker to be output by maybe_print_line. */
536 print.src_line = -1;
537 print.printed = 0;
538 print.prev = 0;
539 print.first_time = 1;
541 if (gfc_cpp_preprocess_only ())
543 if (gfc_cpp_option.output_filename)
545 /* This needs cheating: with "-E -o <file>", the user wants the
546 preprocessed output in <file>. However, if nothing is done
547 about it <file> is also used for assembler output. Hence, it
548 is necessary to redirect assembler output (actually nothing
549 as -E implies -fsyntax-only) to another file, otherwise the
550 output from preprocessing is lost. */
551 asm_file_name = gfc_cpp_option.temporary_filename;
553 print.outf = fopen (gfc_cpp_option.output_filename, "w");
554 if (print.outf == NULL)
555 gfc_fatal_error ("opening output file %qs: %s",
556 gfc_cpp_option.output_filename,
557 xstrerror (errno));
559 else
560 print.outf = stdout;
562 else
564 print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
565 if (print.outf == NULL)
566 gfc_fatal_error ("opening output file %qs: %s",
567 gfc_cpp_option.temporary_filename, xstrerror (errno));
570 gcc_assert(cpp_in);
571 if (!cpp_read_main_file (cpp_in, gfc_source_file))
572 errorcount++;
575 void
576 gfc_cpp_init (void)
578 int i;
580 if (gfc_option.flag_preprocessed)
581 return;
583 cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
584 if (!gfc_cpp_option.no_predefined)
586 /* Make sure all of the builtins about to be declared have
587 BUILTINS_LOCATION has their source_location. */
588 source_location builtins_loc = BUILTINS_LOCATION;
589 cpp_force_token_locations (cpp_in, &builtins_loc);
591 cpp_define_builtins (cpp_in);
593 cpp_stop_forcing_token_locations (cpp_in);
596 /* Handle deferred options from command-line. */
597 cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
599 for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
601 gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
603 if (opt->code == OPT_D)
604 cpp_define (cpp_in, opt->arg);
605 else if (opt->code == OPT_U)
606 cpp_undef (cpp_in, opt->arg);
607 else if (opt->code == OPT_A)
609 if (opt->arg[0] == '-')
610 cpp_unassert (cpp_in, opt->arg + 1);
611 else
612 cpp_assert (cpp_in, opt->arg);
614 else if (opt->code == OPT_MT || opt->code == OPT_MQ)
615 deps_add_target (cpp_get_deps (cpp_in),
616 opt->arg, opt->code == OPT_MQ);
619 if (gfc_cpp_option.working_directory
620 && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
621 pp_dir_change (cpp_in, get_src_pwd ());
624 bool
625 gfc_cpp_preprocess (const char *source_file)
627 if (!gfc_cpp_enabled ())
628 return false;
630 cpp_change_file (cpp_in, LC_RENAME, source_file);
632 if (cpp_option->traditional)
633 scan_translation_unit_trad (cpp_in);
634 else
635 scan_translation_unit (cpp_in);
637 /* -dM command line option. */
638 if (gfc_cpp_preprocess_only () &&
639 gfc_cpp_option.dump_macros == 'M')
641 putc ('\n', print.outf);
642 cpp_forall_identifiers (cpp_in, dump_macro, NULL);
645 putc ('\n', print.outf);
647 if (!gfc_cpp_preprocess_only ()
648 || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
649 fclose (print.outf);
651 return true;
654 void
655 gfc_cpp_done (void)
657 if (!gfc_cpp_enabled ())
658 return;
660 gcc_assert (cpp_in);
662 if (gfc_cpp_makedep ())
664 if (gfc_cpp_option.deps_filename)
666 FILE *f = fopen (gfc_cpp_option.deps_filename, "w");
667 if (f)
669 cpp_finish (cpp_in, f);
670 fclose (f);
672 else
673 gfc_fatal_error ("opening output file %qs: %s",
674 gfc_cpp_option.deps_filename,
675 xstrerror (errno));
677 else
678 cpp_finish (cpp_in, stdout);
681 cpp_undef_all (cpp_in);
682 cpp_clear_file_cache (cpp_in);
685 /* PATH must be malloc-ed and NULL-terminated. */
686 void
687 gfc_cpp_add_include_path (char *path, bool user_supplied)
689 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
690 include path. Fortran does not define any system include paths. */
691 int cxx_aware = 0;
693 add_path (path, BRACKET, cxx_aware, user_supplied);
696 void
697 gfc_cpp_add_include_path_after (char *path, bool user_supplied)
699 int cxx_aware = 0;
700 add_path (path, AFTER, cxx_aware, user_supplied);
703 void
704 gfc_cpp_register_include_paths (void)
706 int cxx_stdinc = 0;
707 register_include_chains (cpp_in, gfc_cpp_option.sysroot,
708 gfc_cpp_option.prefix, gfc_cpp_option.multilib,
709 gfc_cpp_option.standard_include_paths, cxx_stdinc,
710 gfc_cpp_option.verbose);
715 static void scan_translation_unit_trad (cpp_reader *);
716 static void account_for_newlines (const unsigned char *, size_t);
717 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
719 static void print_line (source_location, const char *);
720 static void maybe_print_line (source_location);
723 /* Writes out the preprocessed file, handling spacing and paste
724 avoidance issues. */
725 static void
726 scan_translation_unit (cpp_reader *pfile)
728 bool avoid_paste = false;
730 print.source = NULL;
731 for (;;)
733 const cpp_token *token = cpp_get_token (pfile);
735 if (token->type == CPP_PADDING)
737 avoid_paste = true;
738 if (print.source == NULL
739 || (!(print.source->flags & PREV_WHITE)
740 && token->val.source == NULL))
741 print.source = token->val.source;
742 continue;
745 if (token->type == CPP_EOF)
746 break;
748 /* Subtle logic to output a space if and only if necessary. */
749 if (avoid_paste)
751 if (print.source == NULL)
752 print.source = token;
753 if (print.source->flags & PREV_WHITE
754 || (print.prev
755 && cpp_avoid_paste (pfile, print.prev, token))
756 || (print.prev == NULL && token->type == CPP_HASH))
757 putc (' ', print.outf);
759 else if (token->flags & PREV_WHITE)
760 putc (' ', print.outf);
762 avoid_paste = false;
763 print.source = NULL;
764 print.prev = token;
765 cpp_output_token (token, print.outf);
767 if (token->type == CPP_COMMENT)
768 account_for_newlines (token->val.str.text, token->val.str.len);
772 /* Adjust print.src_line for newlines embedded in output. */
773 static void
774 account_for_newlines (const unsigned char *str, size_t len)
776 while (len--)
777 if (*str++ == '\n')
778 print.src_line++;
781 /* Writes out a traditionally preprocessed file. */
782 static void
783 scan_translation_unit_trad (cpp_reader *pfile)
785 while (_cpp_read_logical_line_trad (pfile))
787 size_t len = pfile->out.cur - pfile->out.base;
788 maybe_print_line (pfile->out.first_line);
789 fwrite (pfile->out.base, 1, len, print.outf);
790 print.printed = 1;
791 if (!CPP_OPTION (pfile, discard_comments))
792 account_for_newlines (pfile->out.base, len);
796 /* If the token read on logical line LINE needs to be output on a
797 different line to the current one, output the required newlines or
798 a line marker. */
799 static void
800 maybe_print_line (source_location src_loc)
802 const line_map_ordinary *map
803 = linemap_check_ordinary (linemap_lookup (line_table, src_loc));
804 int src_line = SOURCE_LINE (map, src_loc);
806 /* End the previous line of text. */
807 if (print.printed)
809 putc ('\n', print.outf);
810 print.src_line++;
811 print.printed = 0;
814 if (src_line >= print.src_line && src_line < print.src_line + 8)
816 while (src_line > print.src_line)
818 putc ('\n', print.outf);
819 print.src_line++;
822 else
823 print_line (src_loc, "");
826 /* Output a line marker for logical line LINE. Special flags are "1"
827 or "2" indicating entering or leaving a file. */
828 static void
829 print_line (source_location src_loc, const char *special_flags)
831 /* End any previous line of text. */
832 if (print.printed)
833 putc ('\n', print.outf);
834 print.printed = 0;
836 if (!gfc_cpp_option.no_line_commands)
838 expanded_location loc;
839 size_t to_file_len;
840 unsigned char *to_file_quoted;
841 unsigned char *p;
842 int sysp;
844 loc = expand_location (src_loc);
845 to_file_len = strlen (loc.file);
846 to_file_quoted = (unsigned char *) alloca (to_file_len * 4 + 1);
848 print.src_line = loc.line;
850 /* cpp_quote_string does not nul-terminate, so we have to do it
851 ourselves. */
852 p = cpp_quote_string (to_file_quoted,
853 (const unsigned char *) loc.file, to_file_len);
854 *p = '\0';
855 fprintf (print.outf, "# %u \"%s\"%s",
856 print.src_line == 0 ? 1 : print.src_line,
857 to_file_quoted, special_flags);
859 sysp = in_system_header_at (src_loc);
860 if (sysp == 2)
861 fputs (" 3 4", print.outf);
862 else if (sysp == 1)
863 fputs (" 3", print.outf);
865 putc ('\n', print.outf);
869 static void
870 cb_file_change (cpp_reader * ARG_UNUSED (pfile), const line_map_ordinary *map)
872 const char *flags = "";
874 if (gfc_cpp_option.no_line_commands)
875 return;
877 if (!map)
878 return;
880 if (print.first_time)
882 /* Avoid printing foo.i when the main file is foo.c. */
883 if (!cpp_get_options (cpp_in)->preprocessed)
884 print_line (map->start_location, flags);
885 print.first_time = 0;
887 else
889 /* Bring current file to correct line when entering a new file. */
890 if (map->reason == LC_ENTER)
892 const line_map_ordinary *from = INCLUDED_FROM (line_table, map);
893 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
895 if (map->reason == LC_ENTER)
896 flags = " 1";
897 else if (map->reason == LC_LEAVE)
898 flags = " 2";
899 print_line (map->start_location, flags);
904 /* Called when a line of output is started. TOKEN is the first token
905 of the line, and at end of file will be CPP_EOF. */
906 static void
907 cb_line_change (cpp_reader *pfile, const cpp_token *token,
908 int parsing_args)
910 source_location src_loc = token->src_loc;
912 if (token->type == CPP_EOF || parsing_args)
913 return;
915 maybe_print_line (src_loc);
916 print.prev = 0;
917 print.source = 0;
919 /* Supply enough spaces to put this token in its original column,
920 one space per column greater than 2, since scan_translation_unit
921 will provide a space if PREV_WHITE. Don't bother trying to
922 reconstruct tabs; we can't get it right in general, and nothing
923 ought to care. Some things do care; the fault lies with them. */
924 if (!CPP_OPTION (pfile, traditional))
926 const line_map_ordinary *map
927 = linemap_check_ordinary (linemap_lookup (line_table, src_loc));
928 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
929 print.printed = 1;
931 while (-- spaces >= 0)
932 putc (' ', print.outf);
936 static void
937 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
938 const cpp_string *str)
940 maybe_print_line (line);
941 fprintf (print.outf, "#ident %s\n", str->text);
942 print.src_line++;
945 static void
946 cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
947 cpp_hashnode *node ATTRIBUTE_UNUSED)
949 maybe_print_line (line);
950 fputs ("#define ", print.outf);
952 /* 'D' is whole definition; 'N' is name only. */
953 if (gfc_cpp_option.dump_macros == 'D')
954 fputs ((const char *) cpp_macro_definition (pfile, node),
955 print.outf);
956 else
957 fputs ((const char *) NODE_NAME (node), print.outf);
959 putc ('\n', print.outf);
960 if (LOCATION_LINE (line) != 0)
961 print.src_line++;
964 static void
965 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
966 cpp_hashnode *node)
968 maybe_print_line (line);
969 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
970 print.src_line++;
973 static void
974 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
975 const unsigned char *dir, const char *header, int angle_brackets,
976 const cpp_token **comments)
978 maybe_print_line (line);
979 if (angle_brackets)
980 fprintf (print.outf, "#%s <%s>", dir, header);
981 else
982 fprintf (print.outf, "#%s \"%s\"", dir, header);
984 if (comments != NULL)
986 while (*comments != NULL)
988 if ((*comments)->flags & PREV_WHITE)
989 putc (' ', print.outf);
990 cpp_output_token (*comments, print.outf);
991 ++comments;
995 putc ('\n', print.outf);
996 print.src_line++;
999 /* Dump out the hash table. */
1000 static int
1001 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
1003 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1005 fputs ("#define ", print.outf);
1006 fputs ((const char *) cpp_macro_definition (pfile, node),
1007 print.outf);
1008 putc ('\n', print.outf);
1009 print.src_line++;
1012 return 1;
1015 static void
1016 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
1017 cpp_hashnode *node)
1019 gfc_cpp_macro_queue *q;
1020 q = XNEW (gfc_cpp_macro_queue);
1021 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
1022 q->next = cpp_define_queue;
1023 cpp_define_queue = q;
1026 /* Callback from cpp_error for PFILE to print diagnostics from the
1027 preprocessor. The diagnostic is of type LEVEL, with REASON set
1028 to the reason code if LEVEL is represents a warning, at location
1029 LOCATION, with column number possibly overridden by COLUMN_OVERRIDE
1030 if not zero; MSG is the translated message and AP the arguments.
1031 Returns true if a diagnostic was emitted, false otherwise. */
1033 static bool
1034 cb_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
1035 location_t location, unsigned int column_override,
1036 const char *msg, va_list *ap)
1038 diagnostic_info diagnostic;
1039 diagnostic_t dlevel;
1040 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
1041 bool ret;
1043 switch (level)
1045 case CPP_DL_WARNING_SYSHDR:
1046 global_dc->dc_warn_system_headers = 1;
1047 /* Fall through. */
1048 case CPP_DL_WARNING:
1049 dlevel = DK_WARNING;
1050 break;
1051 case CPP_DL_PEDWARN:
1052 dlevel = DK_PEDWARN;
1053 break;
1054 case CPP_DL_ERROR:
1055 dlevel = DK_ERROR;
1056 break;
1057 case CPP_DL_ICE:
1058 dlevel = DK_ICE;
1059 break;
1060 case CPP_DL_NOTE:
1061 dlevel = DK_NOTE;
1062 break;
1063 case CPP_DL_FATAL:
1064 dlevel = DK_FATAL;
1065 break;
1066 default:
1067 gcc_unreachable ();
1069 diagnostic_set_info_translated (&diagnostic, msg, ap,
1070 location, dlevel);
1071 if (column_override)
1072 diagnostic_override_column (&diagnostic, column_override);
1073 if (reason == CPP_W_WARNING_DIRECTIVE)
1074 diagnostic_override_option_index (&diagnostic, OPT_Wcpp);
1075 ret = report_diagnostic (&diagnostic);
1076 if (level == CPP_DL_WARNING_SYSHDR)
1077 global_dc->dc_warn_system_headers = save_warn_system_headers;
1078 return ret;
1081 /* Callback called when -fworking-director and -E to emit working
1082 directory in cpp output file. */
1084 void
1085 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1087 size_t to_file_len = strlen (dir);
1088 unsigned char *to_file_quoted =
1089 (unsigned char *) alloca (to_file_len * 4 + 1);
1090 unsigned char *p;
1092 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
1093 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
1094 *p = '\0';
1095 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
1098 /* Copy a #pragma directive to the preprocessed output. */
1099 static void
1100 cb_def_pragma (cpp_reader *pfile, source_location line)
1102 maybe_print_line (line);
1103 fputs ("#pragma ", print.outf);
1104 cpp_output_line (pfile, print.outf);
1105 print.src_line++;
1108 static void
1109 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
1110 source_location line ATTRIBUTE_UNUSED,
1111 cpp_hashnode *node)
1113 gfc_cpp_macro_queue *q;
1114 q = XNEW (gfc_cpp_macro_queue);
1115 q->macro = xstrdup ((const char *) NODE_NAME (node));
1116 q->next = cpp_undefine_queue;
1117 cpp_undefine_queue = q;
1120 static void
1121 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
1123 gfc_cpp_macro_queue *q;
1125 /* End the previous line of text. */
1126 if (print.printed)
1128 putc ('\n', print.outf);
1129 print.src_line++;
1130 print.printed = 0;
1133 for (q = cpp_define_queue; q;)
1135 gfc_cpp_macro_queue *oq;
1136 fputs ("#define ", print.outf);
1137 fputs (q->macro, print.outf);
1138 putc ('\n', print.outf);
1139 print.src_line++;
1140 oq = q;
1141 q = q->next;
1142 free (oq->macro);
1143 free (oq);
1145 cpp_define_queue = NULL;
1146 for (q = cpp_undefine_queue; q;)
1148 gfc_cpp_macro_queue *oq;
1149 fprintf (print.outf, "#undef %s\n", q->macro);
1150 print.src_line++;
1151 oq = q;
1152 q = q->next;
1153 free (oq->macro);
1154 free (oq);
1156 cpp_undefine_queue = NULL;