svn merge -r 218679:218997 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / fortran / cpp.c
blobb8ec52b64c90ec5413106961fc153286f794de08
1 /* Copyright (C) 2008-2014 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 "tree.h"
24 #include "version.h"
25 #include "flags.h"
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"
35 #include "../../libcpp/internal.h"
36 #include "cpp.h"
37 #include "incpath.h"
38 #include "cppbuiltin.h"
39 #include "mkdeps.h"
41 #ifndef TARGET_SYSTEM_ROOT
42 # define TARGET_SYSTEM_ROOT NULL
43 #endif
45 #ifndef TARGET_CPU_CPP_BUILTINS
46 # define TARGET_CPU_CPP_BUILTINS()
47 #endif
49 #ifndef TARGET_OS_CPP_BUILTINS
50 # define TARGET_OS_CPP_BUILTINS()
51 #endif
53 #ifndef TARGET_OBJFMT_CPP_BUILTINS
54 # define TARGET_OBJFMT_CPP_BUILTINS()
55 #endif
58 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
59 handling is deferred to gfc_cpp_init (). */
60 typedef struct
62 enum opt_code code;
63 const char *arg;
65 gfc_cpp_deferred_opt_t;
68 /* Defined and undefined macros being queued for output with -dU at
69 the next newline. */
70 typedef struct gfc_cpp_macro_queue
72 struct gfc_cpp_macro_queue *next; /* Next macro in the list. */
73 char *macro; /* The name of the macro if not
74 defined, the full definition if
75 defined. */
76 } gfc_cpp_macro_queue;
77 static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
79 struct gfc_cpp_option_data
81 /* Argument of -cpp, implied by SPEC;
82 if NULL, preprocessing disabled. */
83 const char *temporary_filename;
85 const char *output_filename; /* -o <arg> */
86 int preprocess_only; /* -E */
87 int discard_comments; /* -C */
88 int discard_comments_in_macro_exp; /* -CC */
89 int print_include_names; /* -H */
90 int no_line_commands; /* -P */
91 char dump_macros; /* -d[DMNU] */
92 int dump_includes; /* -dI */
93 int working_directory; /* -fworking-directory */
94 int no_predefined; /* -undef */
95 int standard_include_paths; /* -nostdinc */
96 int verbose; /* -v */
97 int deps; /* -M */
98 int deps_skip_system; /* -MM */
99 const char *deps_filename; /* -M[M]D */
100 const char *deps_filename_user; /* -MF <arg> */
101 int deps_missing_are_generated; /* -MG */
102 int deps_phony; /* -MP */
103 int warn_date_time; /* -Wdate-time */
105 const char *multilib; /* -imultilib <dir> */
106 const char *prefix; /* -iprefix <dir> */
107 const char *sysroot; /* -isysroot <dir> */
109 /* Options whose handling needs to be deferred until the
110 appropriate cpp-objects are created:
111 -A predicate=answer
112 -D <macro>[=<val>]
113 -U <macro> */
114 gfc_cpp_deferred_opt_t *deferred_opt;
115 int deferred_opt_count;
117 gfc_cpp_option;
119 /* Structures used with libcpp: */
120 static cpp_options *cpp_option = NULL;
121 static cpp_reader *cpp_in = NULL;
123 /* Encapsulates state used to convert a stream of cpp-tokens into
124 a text file. */
125 static struct
127 FILE *outf; /* Stream to write to. */
128 const cpp_token *prev; /* Previous token. */
129 const cpp_token *source; /* Source token for spacing. */
130 int src_line; /* Line number currently being written. */
131 unsigned char printed; /* Nonzero if something output at line. */
132 bool first_time; /* cb_file_change hasn't been called yet. */
133 } print;
135 /* General output routines. */
136 static void scan_translation_unit (cpp_reader *);
137 static void scan_translation_unit_trad (cpp_reader *);
139 /* Callback routines for the parser. Most of these are active only
140 in specific modes. */
141 static void cb_file_change (cpp_reader *, const struct line_map *);
142 static void cb_line_change (cpp_reader *, const cpp_token *, int);
143 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
144 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
145 static void cb_def_pragma (cpp_reader *, source_location);
146 static void cb_include (cpp_reader *, source_location, const unsigned char *,
147 const char *, int, const cpp_token **);
148 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
149 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
150 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
151 static bool cb_cpp_error (cpp_reader *, int, int, location_t, unsigned int,
152 const char *, va_list *)
153 ATTRIBUTE_GCC_DIAG(6,0);
154 void pp_dir_change (cpp_reader *, const char *);
156 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
157 static void dump_queued_macros (cpp_reader *);
160 static void
161 cpp_define_builtins (cpp_reader *pfile)
163 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
164 in C, defines __STDC_HOSTED__?! */
165 cpp_init_builtins (pfile, 0);
167 /* Initialize GFORTRAN specific builtins.
168 These are documented. */
169 define_language_independent_builtin_macros (pfile);
170 cpp_define (pfile, "__GFORTRAN__=1");
171 cpp_define (pfile, "_LANGUAGE_FORTRAN=1");
173 if (flag_openacc)
174 cpp_define (pfile, "_OPENACC=201306");
176 if (flag_openmp)
177 cpp_define (pfile, "_OPENMP=201307");
179 /* The defines below are necessary for the TARGET_* macros.
181 FIXME: Note that builtin_define_std() actually is a function
182 in c-cppbuiltin.c which uses flags undefined for Fortran.
183 Let's skip this for now. If needed, one needs to look into it
184 once more. */
186 # define builtin_define(TXT) cpp_define (pfile, TXT)
187 # define builtin_define_std(TXT)
188 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
190 /* FIXME: Pandora's Box
191 Using the macros below results in multiple breakages:
192 - mingw will fail to compile this file as dependent macros
193 assume to be used in c-cppbuiltin.c only. Further, they use
194 flags only valid/defined in C (same as noted above).
195 [config/i386/mingw32.h, config/i386/cygming.h]
196 - other platforms (not as popular) break similarly
197 [grep for 'builtin_define_with_int_value' in gcc/config/]
199 TARGET_CPU_CPP_BUILTINS ();
200 TARGET_OS_CPP_BUILTINS ();
201 TARGET_OBJFMT_CPP_BUILTINS (); */
203 #undef builtin_define
204 #undef builtin_define_std
205 #undef builtin_assert
208 bool
209 gfc_cpp_enabled (void)
211 return gfc_cpp_option.temporary_filename != NULL;
214 bool
215 gfc_cpp_preprocess_only (void)
217 return gfc_cpp_option.preprocess_only;
220 bool
221 gfc_cpp_makedep (void)
223 return gfc_cpp_option.deps;
226 void
227 gfc_cpp_add_dep (const char *name, bool system)
229 if (!gfc_cpp_option.deps_skip_system || !system)
230 deps_add_dep (cpp_get_deps (cpp_in), name);
233 void
234 gfc_cpp_add_target (const char *name)
236 deps_add_target (cpp_get_deps (cpp_in), name, 0);
240 const char *
241 gfc_cpp_temporary_file (void)
243 return gfc_cpp_option.temporary_filename;
246 void
247 gfc_cpp_init_options (unsigned int decoded_options_count,
248 struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
250 /* Do not create any objects from libcpp here. If no
251 preprocessing is requested, this would be wasted
252 time and effort.
254 See gfc_cpp_post_options() instead. */
256 gfc_cpp_option.temporary_filename = NULL;
257 gfc_cpp_option.output_filename = NULL;
258 gfc_cpp_option.preprocess_only = 0;
259 gfc_cpp_option.discard_comments = 1;
260 gfc_cpp_option.discard_comments_in_macro_exp = 1;
261 gfc_cpp_option.print_include_names = 0;
262 gfc_cpp_option.no_line_commands = 0;
263 gfc_cpp_option.dump_macros = '\0';
264 gfc_cpp_option.dump_includes = 0;
265 gfc_cpp_option.working_directory = -1;
266 gfc_cpp_option.no_predefined = 0;
267 gfc_cpp_option.standard_include_paths = 1;
268 gfc_cpp_option.verbose = 0;
269 gfc_cpp_option.warn_date_time = 0;
270 gfc_cpp_option.deps = 0;
271 gfc_cpp_option.deps_skip_system = 0;
272 gfc_cpp_option.deps_phony = 0;
273 gfc_cpp_option.deps_missing_are_generated = 0;
274 gfc_cpp_option.deps_filename = NULL;
275 gfc_cpp_option.deps_filename_user = NULL;
277 gfc_cpp_option.multilib = NULL;
278 gfc_cpp_option.prefix = NULL;
279 gfc_cpp_option.sysroot = TARGET_SYSTEM_ROOT;
281 gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t,
282 decoded_options_count);
283 gfc_cpp_option.deferred_opt_count = 0;
287 gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
289 int result = 1;
290 enum opt_code code = (enum opt_code) scode;
292 switch (code)
294 default:
295 result = 0;
296 break;
298 case OPT_cpp_:
299 gfc_cpp_option.temporary_filename = arg;
300 break;
302 case OPT_nocpp:
303 gfc_cpp_option.temporary_filename = 0L;
304 break;
306 case OPT_d:
307 for ( ; *arg; ++arg)
308 switch (*arg)
310 case 'D':
311 case 'M':
312 case 'N':
313 case 'U':
314 gfc_cpp_option.dump_macros = *arg;
315 break;
317 case 'I':
318 gfc_cpp_option.dump_includes = 1;
319 break;
321 break;
323 case OPT_fworking_directory:
324 gfc_cpp_option.working_directory = value;
325 break;
327 case OPT_idirafter:
328 gfc_cpp_add_include_path_after (xstrdup(arg), true);
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_Wdate_time:
368 gfc_cpp_option.warn_date_time = value;
369 break;
371 case OPT_A:
372 case OPT_D:
373 case OPT_U:
374 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
375 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
376 gfc_cpp_option.deferred_opt_count++;
377 break;
379 case OPT_C:
380 gfc_cpp_option.discard_comments = 0;
381 break;
383 case OPT_CC:
384 gfc_cpp_option.discard_comments = 0;
385 gfc_cpp_option.discard_comments_in_macro_exp = 0;
386 break;
388 case OPT_E:
389 gfc_cpp_option.preprocess_only = 1;
390 break;
392 case OPT_H:
393 gfc_cpp_option.print_include_names = 1;
394 break;
396 case OPT_MM:
397 gfc_cpp_option.deps_skip_system = 1;
398 /* fall through */
400 case OPT_M:
401 gfc_cpp_option.deps = 1;
402 break;
404 case OPT_MMD:
405 gfc_cpp_option.deps_skip_system = 1;
406 /* fall through */
408 case OPT_MD:
409 gfc_cpp_option.deps = 1;
410 gfc_cpp_option.deps_filename = arg;
411 break;
413 case OPT_MF:
414 /* If specified multiple times, last one wins. */
415 gfc_cpp_option.deps_filename_user = arg;
416 break;
418 case OPT_MG:
419 gfc_cpp_option.deps_missing_are_generated = 1;
420 break;
422 case OPT_MP:
423 gfc_cpp_option.deps_phony = 1;
424 break;
426 case OPT_MQ:
427 case OPT_MT:
428 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
429 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
430 gfc_cpp_option.deferred_opt_count++;
431 break;
433 case OPT_P:
434 gfc_cpp_option.no_line_commands = 1;
435 break;
438 return result;
442 void
443 gfc_cpp_post_options (void)
445 /* Any preprocessing-related option without '-cpp' is considered
446 an error. */
447 if (!gfc_cpp_enabled ()
448 && (gfc_cpp_preprocess_only ()
449 || gfc_cpp_makedep ()
450 || !gfc_cpp_option.discard_comments
451 || !gfc_cpp_option.discard_comments_in_macro_exp
452 || gfc_cpp_option.print_include_names
453 || gfc_cpp_option.no_line_commands
454 || gfc_cpp_option.dump_macros
455 || gfc_cpp_option.dump_includes))
456 gfc_fatal_error ("To enable preprocessing, use %<-cpp%>");
458 if (!gfc_cpp_enabled ())
459 return;
461 cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
462 gcc_assert (cpp_in);
464 /* The cpp_options-structure defines far more flags than those set here.
465 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
466 inter-option dependencies that may need to be enforced. */
467 cpp_option = cpp_get_options (cpp_in);
468 gcc_assert (cpp_option);
470 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
471 cpp_option->traditional = 1;
472 cpp_option->cplusplus_comments = 0;
474 cpp_option->cpp_pedantic = pedantic;
476 cpp_option->dollars_in_ident = flag_dollar_ok;
477 cpp_option->discard_comments = gfc_cpp_option.discard_comments;
478 cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
479 cpp_option->print_include_names = gfc_cpp_option.print_include_names;
480 cpp_option->preprocessed = gfc_option.flag_preprocessed;
481 cpp_option->warn_date_time = gfc_cpp_option.warn_date_time;
483 if (gfc_cpp_makedep ())
485 cpp_option->deps.style = DEPS_USER;
486 cpp_option->deps.phony_targets = gfc_cpp_option.deps_phony;
487 cpp_option->deps.missing_files = gfc_cpp_option.deps_missing_are_generated;
489 /* -MF <arg> overrides -M[M]D. */
490 if (gfc_cpp_option.deps_filename_user)
491 gfc_cpp_option.deps_filename = gfc_cpp_option.deps_filename_user;
494 if (gfc_cpp_option.working_directory == -1)
495 gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
497 cpp_post_options (cpp_in);
499 gfc_cpp_register_include_paths ();
503 void
504 gfc_cpp_init_0 (void)
506 struct cpp_callbacks *cb;
508 cb = cpp_get_callbacks (cpp_in);
509 cb->file_change = cb_file_change;
510 cb->line_change = cb_line_change;
511 cb->ident = cb_ident;
512 cb->def_pragma = cb_def_pragma;
513 cb->error = cb_cpp_error;
515 if (gfc_cpp_option.dump_includes)
516 cb->include = cb_include;
518 if ((gfc_cpp_option.dump_macros == 'D')
519 || (gfc_cpp_option.dump_macros == 'N'))
521 cb->define = cb_define;
522 cb->undef = cb_undef;
525 if (gfc_cpp_option.dump_macros == 'U')
527 cb->before_define = dump_queued_macros;
528 cb->used_define = cb_used_define;
529 cb->used_undef = cb_used_undef;
532 /* Initialize the print structure. Setting print.src_line to -1 here is
533 a trick to guarantee that the first token of the file will cause
534 a linemarker to be output by maybe_print_line. */
535 print.src_line = -1;
536 print.printed = 0;
537 print.prev = 0;
538 print.first_time = 1;
540 if (gfc_cpp_preprocess_only ())
542 if (gfc_cpp_option.output_filename)
544 /* This needs cheating: with "-E -o <file>", the user wants the
545 preprocessed output in <file>. However, if nothing is done
546 about it <file> is also used for assembler output. Hence, it
547 is necessary to redirect assembler output (actually nothing
548 as -E implies -fsyntax-only) to another file, otherwise the
549 output from preprocessing is lost. */
550 asm_file_name = gfc_cpp_option.temporary_filename;
552 print.outf = fopen (gfc_cpp_option.output_filename, "w");
553 if (print.outf == NULL)
554 gfc_fatal_error ("opening output file %qs: %s",
555 gfc_cpp_option.output_filename,
556 xstrerror (errno));
558 else
559 print.outf = stdout;
561 else
563 print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
564 if (print.outf == NULL)
565 gfc_fatal_error ("opening output file %qs: %s",
566 gfc_cpp_option.temporary_filename, xstrerror (errno));
569 gcc_assert(cpp_in);
570 if (!cpp_read_main_file (cpp_in, gfc_source_file))
571 errorcount++;
574 void
575 gfc_cpp_init (void)
577 int i;
579 if (gfc_option.flag_preprocessed)
580 return;
582 cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
583 if (!gfc_cpp_option.no_predefined)
585 /* Make sure all of the builtins about to be declared have
586 BUILTINS_LOCATION has their source_location. */
587 source_location builtins_loc = BUILTINS_LOCATION;
588 cpp_force_token_locations (cpp_in, &builtins_loc);
590 cpp_define_builtins (cpp_in);
592 cpp_stop_forcing_token_locations (cpp_in);
595 /* Handle deferred options from command-line. */
596 cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
598 for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
600 gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
602 if (opt->code == OPT_D)
603 cpp_define (cpp_in, opt->arg);
604 else if (opt->code == OPT_U)
605 cpp_undef (cpp_in, opt->arg);
606 else if (opt->code == OPT_A)
608 if (opt->arg[0] == '-')
609 cpp_unassert (cpp_in, opt->arg + 1);
610 else
611 cpp_assert (cpp_in, opt->arg);
613 else if (opt->code == OPT_MT || opt->code == OPT_MQ)
614 deps_add_target (cpp_get_deps (cpp_in),
615 opt->arg, opt->code == OPT_MQ);
618 if (gfc_cpp_option.working_directory
619 && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
620 pp_dir_change (cpp_in, get_src_pwd ());
623 bool
624 gfc_cpp_preprocess (const char *source_file)
626 if (!gfc_cpp_enabled ())
627 return false;
629 cpp_change_file (cpp_in, LC_RENAME, source_file);
631 if (cpp_option->traditional)
632 scan_translation_unit_trad (cpp_in);
633 else
634 scan_translation_unit (cpp_in);
636 /* -dM command line option. */
637 if (gfc_cpp_preprocess_only () &&
638 gfc_cpp_option.dump_macros == 'M')
640 putc ('\n', print.outf);
641 cpp_forall_identifiers (cpp_in, dump_macro, NULL);
644 putc ('\n', print.outf);
646 if (!gfc_cpp_preprocess_only ()
647 || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
648 fclose (print.outf);
650 return true;
653 void
654 gfc_cpp_done (void)
656 if (!gfc_cpp_enabled ())
657 return;
659 gcc_assert (cpp_in);
661 if (gfc_cpp_makedep ())
663 if (gfc_cpp_option.deps_filename)
665 FILE *f = fopen (gfc_cpp_option.deps_filename, "w");
666 if (f)
668 cpp_finish (cpp_in, f);
669 fclose (f);
671 else
672 gfc_fatal_error ("opening output file %qs: %s",
673 gfc_cpp_option.deps_filename,
674 xstrerror (errno));
676 else
677 cpp_finish (cpp_in, stdout);
680 cpp_undef_all (cpp_in);
681 cpp_clear_file_cache (cpp_in);
684 /* PATH must be malloc-ed and NULL-terminated. */
685 void
686 gfc_cpp_add_include_path (char *path, bool user_supplied)
688 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
689 include path. Fortran does not define any system include paths. */
690 int cxx_aware = 0;
692 add_path (path, BRACKET, cxx_aware, user_supplied);
695 void
696 gfc_cpp_add_include_path_after (char *path, bool user_supplied)
698 int cxx_aware = 0;
699 add_path (path, AFTER, cxx_aware, user_supplied);
702 void
703 gfc_cpp_register_include_paths (void)
705 int cxx_stdinc = 0;
706 register_include_chains (cpp_in, gfc_cpp_option.sysroot,
707 gfc_cpp_option.prefix, gfc_cpp_option.multilib,
708 gfc_cpp_option.standard_include_paths, cxx_stdinc,
709 gfc_cpp_option.verbose);
714 static void scan_translation_unit_trad (cpp_reader *);
715 static void account_for_newlines (const unsigned char *, size_t);
716 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
718 static void print_line (source_location, const char *);
719 static void maybe_print_line (source_location);
722 /* Writes out the preprocessed file, handling spacing and paste
723 avoidance issues. */
724 static void
725 scan_translation_unit (cpp_reader *pfile)
727 bool avoid_paste = false;
729 print.source = NULL;
730 for (;;)
732 const cpp_token *token = cpp_get_token (pfile);
734 if (token->type == CPP_PADDING)
736 avoid_paste = true;
737 if (print.source == NULL
738 || (!(print.source->flags & PREV_WHITE)
739 && token->val.source == NULL))
740 print.source = token->val.source;
741 continue;
744 if (token->type == CPP_EOF)
745 break;
747 /* Subtle logic to output a space if and only if necessary. */
748 if (avoid_paste)
750 if (print.source == NULL)
751 print.source = token;
752 if (print.source->flags & PREV_WHITE
753 || (print.prev
754 && cpp_avoid_paste (pfile, print.prev, token))
755 || (print.prev == NULL && token->type == CPP_HASH))
756 putc (' ', print.outf);
758 else if (token->flags & PREV_WHITE)
759 putc (' ', print.outf);
761 avoid_paste = false;
762 print.source = NULL;
763 print.prev = token;
764 cpp_output_token (token, print.outf);
766 if (token->type == CPP_COMMENT)
767 account_for_newlines (token->val.str.text, token->val.str.len);
771 /* Adjust print.src_line for newlines embedded in output. */
772 static void
773 account_for_newlines (const unsigned char *str, size_t len)
775 while (len--)
776 if (*str++ == '\n')
777 print.src_line++;
780 /* Writes out a traditionally preprocessed file. */
781 static void
782 scan_translation_unit_trad (cpp_reader *pfile)
784 while (_cpp_read_logical_line_trad (pfile))
786 size_t len = pfile->out.cur - pfile->out.base;
787 maybe_print_line (pfile->out.first_line);
788 fwrite (pfile->out.base, 1, len, print.outf);
789 print.printed = 1;
790 if (!CPP_OPTION (pfile, discard_comments))
791 account_for_newlines (pfile->out.base, len);
795 /* If the token read on logical line LINE needs to be output on a
796 different line to the current one, output the required newlines or
797 a line marker. */
798 static void
799 maybe_print_line (source_location src_loc)
801 const struct line_map *map = linemap_lookup (line_table, src_loc);
802 int src_line = SOURCE_LINE (map, src_loc);
804 /* End the previous line of text. */
805 if (print.printed)
807 putc ('\n', print.outf);
808 print.src_line++;
809 print.printed = 0;
812 if (src_line >= print.src_line && src_line < print.src_line + 8)
814 while (src_line > print.src_line)
816 putc ('\n', print.outf);
817 print.src_line++;
820 else
821 print_line (src_loc, "");
824 /* Output a line marker for logical line LINE. Special flags are "1"
825 or "2" indicating entering or leaving a file. */
826 static void
827 print_line (source_location src_loc, const char *special_flags)
829 /* End any previous line of text. */
830 if (print.printed)
831 putc ('\n', print.outf);
832 print.printed = 0;
834 if (!gfc_cpp_option.no_line_commands)
836 expanded_location loc;
837 size_t to_file_len;
838 unsigned char *to_file_quoted;
839 unsigned char *p;
840 int sysp;
842 loc = expand_location (src_loc);
843 to_file_len = strlen (loc.file);
844 to_file_quoted = (unsigned char *) alloca (to_file_len * 4 + 1);
846 print.src_line = loc.line;
848 /* cpp_quote_string does not nul-terminate, so we have to do it
849 ourselves. */
850 p = cpp_quote_string (to_file_quoted,
851 (const unsigned char *) loc.file, to_file_len);
852 *p = '\0';
853 fprintf (print.outf, "# %u \"%s\"%s",
854 print.src_line == 0 ? 1 : print.src_line,
855 to_file_quoted, special_flags);
857 sysp = in_system_header_at (src_loc);
858 if (sysp == 2)
859 fputs (" 3 4", print.outf);
860 else if (sysp == 1)
861 fputs (" 3", print.outf);
863 putc ('\n', print.outf);
867 static void
868 cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map)
870 const char *flags = "";
872 if (gfc_cpp_option.no_line_commands)
873 return;
875 if (!map)
876 return;
878 if (print.first_time)
880 /* Avoid printing foo.i when the main file is foo.c. */
881 if (!cpp_get_options (cpp_in)->preprocessed)
882 print_line (map->start_location, flags);
883 print.first_time = 0;
885 else
887 /* Bring current file to correct line when entering a new file. */
888 if (map->reason == LC_ENTER)
890 const struct line_map *from = INCLUDED_FROM (line_table, map);
891 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
893 if (map->reason == LC_ENTER)
894 flags = " 1";
895 else if (map->reason == LC_LEAVE)
896 flags = " 2";
897 print_line (map->start_location, flags);
902 /* Called when a line of output is started. TOKEN is the first token
903 of the line, and at end of file will be CPP_EOF. */
904 static void
905 cb_line_change (cpp_reader *pfile, const cpp_token *token,
906 int parsing_args)
908 source_location src_loc = token->src_loc;
910 if (token->type == CPP_EOF || parsing_args)
911 return;
913 maybe_print_line (src_loc);
914 print.prev = 0;
915 print.source = 0;
917 /* Supply enough spaces to put this token in its original column,
918 one space per column greater than 2, since scan_translation_unit
919 will provide a space if PREV_WHITE. Don't bother trying to
920 reconstruct tabs; we can't get it right in general, and nothing
921 ought to care. Some things do care; the fault lies with them. */
922 if (!CPP_OPTION (pfile, traditional))
924 const struct line_map *map = linemap_lookup (line_table, src_loc);
925 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
926 print.printed = 1;
928 while (-- spaces >= 0)
929 putc (' ', print.outf);
933 static void
934 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
935 const cpp_string *str)
937 maybe_print_line (line);
938 fprintf (print.outf, "#ident %s\n", str->text);
939 print.src_line++;
942 static void
943 cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
944 cpp_hashnode *node ATTRIBUTE_UNUSED)
946 maybe_print_line (line);
947 fputs ("#define ", print.outf);
949 /* 'D' is whole definition; 'N' is name only. */
950 if (gfc_cpp_option.dump_macros == 'D')
951 fputs ((const char *) cpp_macro_definition (pfile, node),
952 print.outf);
953 else
954 fputs ((const char *) NODE_NAME (node), print.outf);
956 putc ('\n', print.outf);
957 if (LOCATION_LINE (line) != 0)
958 print.src_line++;
961 static void
962 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
963 cpp_hashnode *node)
965 maybe_print_line (line);
966 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
967 print.src_line++;
970 static void
971 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
972 const unsigned char *dir, const char *header, int angle_brackets,
973 const cpp_token **comments)
975 maybe_print_line (line);
976 if (angle_brackets)
977 fprintf (print.outf, "#%s <%s>", dir, header);
978 else
979 fprintf (print.outf, "#%s \"%s\"", dir, header);
981 if (comments != NULL)
983 while (*comments != NULL)
985 if ((*comments)->flags & PREV_WHITE)
986 putc (' ', print.outf);
987 cpp_output_token (*comments, print.outf);
988 ++comments;
992 putc ('\n', print.outf);
993 print.src_line++;
996 /* Dump out the hash table. */
997 static int
998 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
1000 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1002 fputs ("#define ", print.outf);
1003 fputs ((const char *) cpp_macro_definition (pfile, node),
1004 print.outf);
1005 putc ('\n', print.outf);
1006 print.src_line++;
1009 return 1;
1012 static void
1013 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
1014 cpp_hashnode *node)
1016 gfc_cpp_macro_queue *q;
1017 q = XNEW (gfc_cpp_macro_queue);
1018 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
1019 q->next = cpp_define_queue;
1020 cpp_define_queue = q;
1023 /* Callback from cpp_error for PFILE to print diagnostics from the
1024 preprocessor. The diagnostic is of type LEVEL, with REASON set
1025 to the reason code if LEVEL is represents a warning, at location
1026 LOCATION, with column number possibly overridden by COLUMN_OVERRIDE
1027 if not zero; MSG is the translated message and AP the arguments.
1028 Returns true if a diagnostic was emitted, false otherwise. */
1030 static bool
1031 cb_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
1032 location_t location, unsigned int column_override,
1033 const char *msg, va_list *ap)
1035 diagnostic_info diagnostic;
1036 diagnostic_t dlevel;
1037 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
1038 bool ret;
1040 switch (level)
1042 case CPP_DL_WARNING_SYSHDR:
1043 global_dc->dc_warn_system_headers = 1;
1044 /* Fall through. */
1045 case CPP_DL_WARNING:
1046 dlevel = DK_WARNING;
1047 break;
1048 case CPP_DL_PEDWARN:
1049 dlevel = DK_PEDWARN;
1050 break;
1051 case CPP_DL_ERROR:
1052 dlevel = DK_ERROR;
1053 break;
1054 case CPP_DL_ICE:
1055 dlevel = DK_ICE;
1056 break;
1057 case CPP_DL_NOTE:
1058 dlevel = DK_NOTE;
1059 break;
1060 case CPP_DL_FATAL:
1061 dlevel = DK_FATAL;
1062 break;
1063 default:
1064 gcc_unreachable ();
1066 diagnostic_set_info_translated (&diagnostic, msg, ap,
1067 location, dlevel);
1068 if (column_override)
1069 diagnostic_override_column (&diagnostic, column_override);
1070 if (reason == CPP_W_WARNING_DIRECTIVE)
1071 diagnostic_override_option_index (&diagnostic, OPT_Wcpp);
1072 ret = report_diagnostic (&diagnostic);
1073 if (level == CPP_DL_WARNING_SYSHDR)
1074 global_dc->dc_warn_system_headers = save_warn_system_headers;
1075 return ret;
1078 /* Callback called when -fworking-director and -E to emit working
1079 directory in cpp output file. */
1081 void
1082 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1084 size_t to_file_len = strlen (dir);
1085 unsigned char *to_file_quoted =
1086 (unsigned char *) alloca (to_file_len * 4 + 1);
1087 unsigned char *p;
1089 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
1090 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
1091 *p = '\0';
1092 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
1095 /* Copy a #pragma directive to the preprocessed output. */
1096 static void
1097 cb_def_pragma (cpp_reader *pfile, source_location line)
1099 maybe_print_line (line);
1100 fputs ("#pragma ", print.outf);
1101 cpp_output_line (pfile, print.outf);
1102 print.src_line++;
1105 static void
1106 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
1107 source_location line ATTRIBUTE_UNUSED,
1108 cpp_hashnode *node)
1110 gfc_cpp_macro_queue *q;
1111 q = XNEW (gfc_cpp_macro_queue);
1112 q->macro = xstrdup ((const char *) NODE_NAME (node));
1113 q->next = cpp_undefine_queue;
1114 cpp_undefine_queue = q;
1117 static void
1118 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
1120 gfc_cpp_macro_queue *q;
1122 /* End the previous line of text. */
1123 if (print.printed)
1125 putc ('\n', print.outf);
1126 print.src_line++;
1127 print.printed = 0;
1130 for (q = cpp_define_queue; q;)
1132 gfc_cpp_macro_queue *oq;
1133 fputs ("#define ", print.outf);
1134 fputs (q->macro, print.outf);
1135 putc ('\n', print.outf);
1136 print.src_line++;
1137 oq = q;
1138 q = q->next;
1139 free (oq->macro);
1140 free (oq);
1142 cpp_define_queue = NULL;
1143 for (q = cpp_undefine_queue; q;)
1145 gfc_cpp_macro_queue *oq;
1146 fprintf (print.outf, "#undef %s\n", q->macro);
1147 print.src_line++;
1148 oq = q;
1149 q = q->next;
1150 free (oq->macro);
1151 free (oq);
1153 cpp_undefine_queue = NULL;