* ChangeLog-2013: Correct an old entry.
[official-gcc.git] / gcc / fortran / cpp.c
blob169599003db400ff2e36d094db324b4fb0bc5ca5
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 (gfc_option.gfc_flag_openmp)
174 cpp_define (pfile, "_OPENMP=201107");
176 /* The defines below are necessary for the TARGET_* macros.
178 FIXME: Note that builtin_define_std() actually is a function
179 in c-cppbuiltin.c which uses flags undefined for Fortran.
180 Let's skip this for now. If needed, one needs to look into it
181 once more. */
183 # define builtin_define(TXT) cpp_define (pfile, TXT)
184 # define builtin_define_std(TXT)
185 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
187 /* FIXME: Pandora's Box
188 Using the macros below results in multiple breakages:
189 - mingw will fail to compile this file as dependent macros
190 assume to be used in c-cppbuiltin.c only. Further, they use
191 flags only valid/defined in C (same as noted above).
192 [config/i386/mingw32.h, config/i386/cygming.h]
193 - other platforms (not as popular) break similarly
194 [grep for 'builtin_define_with_int_value' in gcc/config/]
196 TARGET_CPU_CPP_BUILTINS ();
197 TARGET_OS_CPP_BUILTINS ();
198 TARGET_OBJFMT_CPP_BUILTINS (); */
200 #undef builtin_define
201 #undef builtin_define_std
202 #undef builtin_assert
205 bool
206 gfc_cpp_enabled (void)
208 return gfc_cpp_option.temporary_filename != NULL;
211 bool
212 gfc_cpp_preprocess_only (void)
214 return gfc_cpp_option.preprocess_only;
217 bool
218 gfc_cpp_makedep (void)
220 return gfc_cpp_option.deps;
223 void
224 gfc_cpp_add_dep (const char *name, bool system)
226 if (!gfc_cpp_option.deps_skip_system || !system)
227 deps_add_dep (cpp_get_deps (cpp_in), name);
230 void
231 gfc_cpp_add_target (const char *name)
233 deps_add_target (cpp_get_deps (cpp_in), name, 0);
237 const char *
238 gfc_cpp_temporary_file (void)
240 return gfc_cpp_option.temporary_filename;
243 void
244 gfc_cpp_init_options (unsigned int decoded_options_count,
245 struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
247 /* Do not create any objects from libcpp here. If no
248 preprocessing is requested, this would be wasted
249 time and effort.
251 See gfc_cpp_post_options() instead. */
253 gfc_cpp_option.temporary_filename = NULL;
254 gfc_cpp_option.output_filename = NULL;
255 gfc_cpp_option.preprocess_only = 0;
256 gfc_cpp_option.discard_comments = 1;
257 gfc_cpp_option.discard_comments_in_macro_exp = 1;
258 gfc_cpp_option.print_include_names = 0;
259 gfc_cpp_option.no_line_commands = 0;
260 gfc_cpp_option.dump_macros = '\0';
261 gfc_cpp_option.dump_includes = 0;
262 gfc_cpp_option.working_directory = -1;
263 gfc_cpp_option.no_predefined = 0;
264 gfc_cpp_option.standard_include_paths = 1;
265 gfc_cpp_option.verbose = 0;
266 gfc_cpp_option.warn_date_time = 0;
267 gfc_cpp_option.deps = 0;
268 gfc_cpp_option.deps_skip_system = 0;
269 gfc_cpp_option.deps_phony = 0;
270 gfc_cpp_option.deps_missing_are_generated = 0;
271 gfc_cpp_option.deps_filename = NULL;
272 gfc_cpp_option.deps_filename_user = NULL;
274 gfc_cpp_option.multilib = NULL;
275 gfc_cpp_option.prefix = NULL;
276 gfc_cpp_option.sysroot = TARGET_SYSTEM_ROOT;
278 gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t,
279 decoded_options_count);
280 gfc_cpp_option.deferred_opt_count = 0;
284 gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
286 int result = 1;
287 enum opt_code code = (enum opt_code) scode;
289 switch (code)
291 default:
292 result = 0;
293 break;
295 case OPT_cpp_:
296 gfc_cpp_option.temporary_filename = arg;
297 break;
299 case OPT_nocpp:
300 gfc_cpp_option.temporary_filename = 0L;
301 break;
303 case OPT_d:
304 for ( ; *arg; ++arg)
305 switch (*arg)
307 case 'D':
308 case 'M':
309 case 'N':
310 case 'U':
311 gfc_cpp_option.dump_macros = *arg;
312 break;
314 case 'I':
315 gfc_cpp_option.dump_includes = 1;
316 break;
318 break;
320 case OPT_fworking_directory:
321 gfc_cpp_option.working_directory = value;
322 break;
324 case OPT_idirafter:
325 gfc_cpp_add_include_path_after (xstrdup(arg), true);
326 break;
328 case OPT_imultilib:
329 gfc_cpp_option.multilib = arg;
330 break;
332 case OPT_iprefix:
333 gfc_cpp_option.prefix = arg;
334 break;
336 case OPT_isysroot:
337 gfc_cpp_option.sysroot = arg;
338 break;
340 case OPT_iquote:
341 case OPT_isystem:
342 gfc_cpp_add_include_path (xstrdup(arg), true);
343 break;
345 case OPT_nostdinc:
346 gfc_cpp_option.standard_include_paths = value;
347 break;
349 case OPT_o:
350 if (!gfc_cpp_option.output_filename)
351 gfc_cpp_option.output_filename = arg;
352 else
353 gfc_fatal_error ("output filename specified twice");
354 break;
356 case OPT_undef:
357 gfc_cpp_option.no_predefined = value;
358 break;
360 case OPT_v:
361 gfc_cpp_option.verbose = value;
362 break;
364 case OPT_Wdate_time:
365 gfc_cpp_option.warn_date_time = value;
366 break;
368 case OPT_A:
369 case OPT_D:
370 case OPT_U:
371 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
372 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
373 gfc_cpp_option.deferred_opt_count++;
374 break;
376 case OPT_C:
377 gfc_cpp_option.discard_comments = 0;
378 break;
380 case OPT_CC:
381 gfc_cpp_option.discard_comments = 0;
382 gfc_cpp_option.discard_comments_in_macro_exp = 0;
383 break;
385 case OPT_E:
386 gfc_cpp_option.preprocess_only = 1;
387 break;
389 case OPT_H:
390 gfc_cpp_option.print_include_names = 1;
391 break;
393 case OPT_MM:
394 gfc_cpp_option.deps_skip_system = 1;
395 /* fall through */
397 case OPT_M:
398 gfc_cpp_option.deps = 1;
399 break;
401 case OPT_MMD:
402 gfc_cpp_option.deps_skip_system = 1;
403 /* fall through */
405 case OPT_MD:
406 gfc_cpp_option.deps = 1;
407 gfc_cpp_option.deps_filename = arg;
408 break;
410 case OPT_MF:
411 /* If specified multiple times, last one wins. */
412 gfc_cpp_option.deps_filename_user = arg;
413 break;
415 case OPT_MG:
416 gfc_cpp_option.deps_missing_are_generated = 1;
417 break;
419 case OPT_MP:
420 gfc_cpp_option.deps_phony = 1;
421 break;
423 case OPT_MQ:
424 case OPT_MT:
425 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
426 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
427 gfc_cpp_option.deferred_opt_count++;
428 break;
430 case OPT_P:
431 gfc_cpp_option.no_line_commands = 1;
432 break;
435 return result;
439 void
440 gfc_cpp_post_options (void)
442 /* Any preprocessing-related option without '-cpp' is considered
443 an error. */
444 if (!gfc_cpp_enabled ()
445 && (gfc_cpp_preprocess_only ()
446 || gfc_cpp_makedep ()
447 || !gfc_cpp_option.discard_comments
448 || !gfc_cpp_option.discard_comments_in_macro_exp
449 || gfc_cpp_option.print_include_names
450 || gfc_cpp_option.no_line_commands
451 || gfc_cpp_option.dump_macros
452 || gfc_cpp_option.dump_includes))
453 gfc_fatal_error("To enable preprocessing, use -cpp");
455 if (!gfc_cpp_enabled ())
456 return;
458 cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
459 gcc_assert (cpp_in);
461 /* The cpp_options-structure defines far more flags than those set here.
462 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
463 inter-option dependencies that may need to be enforced. */
464 cpp_option = cpp_get_options (cpp_in);
465 gcc_assert (cpp_option);
467 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
468 cpp_option->traditional = 1;
469 cpp_option->cplusplus_comments = 0;
471 cpp_option->cpp_pedantic = pedantic;
473 cpp_option->dollars_in_ident = gfc_option.flag_dollar_ok;
474 cpp_option->discard_comments = gfc_cpp_option.discard_comments;
475 cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
476 cpp_option->print_include_names = gfc_cpp_option.print_include_names;
477 cpp_option->preprocessed = gfc_option.flag_preprocessed;
478 cpp_option->warn_date_time = gfc_cpp_option.warn_date_time;
480 if (gfc_cpp_makedep ())
482 cpp_option->deps.style = DEPS_USER;
483 cpp_option->deps.phony_targets = gfc_cpp_option.deps_phony;
484 cpp_option->deps.missing_files = gfc_cpp_option.deps_missing_are_generated;
486 /* -MF <arg> overrides -M[M]D. */
487 if (gfc_cpp_option.deps_filename_user)
488 gfc_cpp_option.deps_filename = gfc_cpp_option.deps_filename_user;
491 if (gfc_cpp_option.working_directory == -1)
492 gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
494 cpp_post_options (cpp_in);
496 gfc_cpp_register_include_paths ();
500 void
501 gfc_cpp_init_0 (void)
503 struct cpp_callbacks *cb;
505 cb = cpp_get_callbacks (cpp_in);
506 cb->file_change = cb_file_change;
507 cb->line_change = cb_line_change;
508 cb->ident = cb_ident;
509 cb->def_pragma = cb_def_pragma;
510 cb->error = cb_cpp_error;
512 if (gfc_cpp_option.dump_includes)
513 cb->include = cb_include;
515 if ((gfc_cpp_option.dump_macros == 'D')
516 || (gfc_cpp_option.dump_macros == 'N'))
518 cb->define = cb_define;
519 cb->undef = cb_undef;
522 if (gfc_cpp_option.dump_macros == 'U')
524 cb->before_define = dump_queued_macros;
525 cb->used_define = cb_used_define;
526 cb->used_undef = cb_used_undef;
529 /* Initialize the print structure. Setting print.src_line to -1 here is
530 a trick to guarantee that the first token of the file will cause
531 a linemarker to be output by maybe_print_line. */
532 print.src_line = -1;
533 print.printed = 0;
534 print.prev = 0;
535 print.first_time = 1;
537 if (gfc_cpp_preprocess_only ())
539 if (gfc_cpp_option.output_filename)
541 /* This needs cheating: with "-E -o <file>", the user wants the
542 preprocessed output in <file>. However, if nothing is done
543 about it <file> is also used for assembler output. Hence, it
544 is necessary to redirect assembler output (actually nothing
545 as -E implies -fsyntax-only) to another file, otherwise the
546 output from preprocessing is lost. */
547 asm_file_name = gfc_cpp_option.temporary_filename;
549 print.outf = fopen (gfc_cpp_option.output_filename, "w");
550 if (print.outf == NULL)
551 gfc_fatal_error ("opening output file %s: %s",
552 gfc_cpp_option.output_filename,
553 xstrerror (errno));
555 else
556 print.outf = stdout;
558 else
560 print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
561 if (print.outf == NULL)
562 gfc_fatal_error ("opening output file %s: %s",
563 gfc_cpp_option.temporary_filename, xstrerror (errno));
566 gcc_assert(cpp_in);
567 if (!cpp_read_main_file (cpp_in, gfc_source_file))
568 errorcount++;
571 void
572 gfc_cpp_init (void)
574 int i;
576 if (gfc_option.flag_preprocessed)
577 return;
579 cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
580 if (!gfc_cpp_option.no_predefined)
582 /* Make sure all of the builtins about to be declared have
583 BUILTINS_LOCATION has their source_location. */
584 source_location builtins_loc = BUILTINS_LOCATION;
585 cpp_force_token_locations (cpp_in, &builtins_loc);
587 cpp_define_builtins (cpp_in);
589 cpp_stop_forcing_token_locations (cpp_in);
592 /* Handle deferred options from command-line. */
593 cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
595 for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
597 gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
599 if (opt->code == OPT_D)
600 cpp_define (cpp_in, opt->arg);
601 else if (opt->code == OPT_U)
602 cpp_undef (cpp_in, opt->arg);
603 else if (opt->code == OPT_A)
605 if (opt->arg[0] == '-')
606 cpp_unassert (cpp_in, opt->arg + 1);
607 else
608 cpp_assert (cpp_in, opt->arg);
610 else if (opt->code == OPT_MT || opt->code == OPT_MQ)
611 deps_add_target (cpp_get_deps (cpp_in),
612 opt->arg, opt->code == OPT_MQ);
615 if (gfc_cpp_option.working_directory
616 && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
617 pp_dir_change (cpp_in, get_src_pwd ());
620 bool
621 gfc_cpp_preprocess (const char *source_file)
623 if (!gfc_cpp_enabled ())
624 return false;
626 cpp_change_file (cpp_in, LC_RENAME, source_file);
628 if (cpp_option->traditional)
629 scan_translation_unit_trad (cpp_in);
630 else
631 scan_translation_unit (cpp_in);
633 /* -dM command line option. */
634 if (gfc_cpp_preprocess_only () &&
635 gfc_cpp_option.dump_macros == 'M')
637 putc ('\n', print.outf);
638 cpp_forall_identifiers (cpp_in, dump_macro, NULL);
641 putc ('\n', print.outf);
643 if (!gfc_cpp_preprocess_only ()
644 || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
645 fclose (print.outf);
647 return true;
650 void
651 gfc_cpp_done (void)
653 if (!gfc_cpp_enabled ())
654 return;
656 gcc_assert (cpp_in);
658 if (gfc_cpp_makedep ())
660 if (gfc_cpp_option.deps_filename)
662 FILE *f = fopen (gfc_cpp_option.deps_filename, "w");
663 if (f)
665 cpp_finish (cpp_in, f);
666 fclose (f);
668 else
669 gfc_fatal_error ("opening output file %s: %s",
670 gfc_cpp_option.deps_filename,
671 xstrerror (errno));
673 else
674 cpp_finish (cpp_in, stdout);
677 cpp_undef_all (cpp_in);
678 cpp_clear_file_cache (cpp_in);
681 /* PATH must be malloc-ed and NULL-terminated. */
682 void
683 gfc_cpp_add_include_path (char *path, bool user_supplied)
685 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
686 include path. Fortran does not define any system include paths. */
687 int cxx_aware = 0;
689 add_path (path, BRACKET, cxx_aware, user_supplied);
692 void
693 gfc_cpp_add_include_path_after (char *path, bool user_supplied)
695 int cxx_aware = 0;
696 add_path (path, AFTER, cxx_aware, user_supplied);
699 void
700 gfc_cpp_register_include_paths (void)
702 int cxx_stdinc = 0;
703 register_include_chains (cpp_in, gfc_cpp_option.sysroot,
704 gfc_cpp_option.prefix, gfc_cpp_option.multilib,
705 gfc_cpp_option.standard_include_paths, cxx_stdinc,
706 gfc_cpp_option.verbose);
711 static void scan_translation_unit_trad (cpp_reader *);
712 static void account_for_newlines (const unsigned char *, size_t);
713 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
715 static void print_line (source_location, const char *);
716 static void maybe_print_line (source_location);
719 /* Writes out the preprocessed file, handling spacing and paste
720 avoidance issues. */
721 static void
722 scan_translation_unit (cpp_reader *pfile)
724 bool avoid_paste = false;
726 print.source = NULL;
727 for (;;)
729 const cpp_token *token = cpp_get_token (pfile);
731 if (token->type == CPP_PADDING)
733 avoid_paste = true;
734 if (print.source == NULL
735 || (!(print.source->flags & PREV_WHITE)
736 && token->val.source == NULL))
737 print.source = token->val.source;
738 continue;
741 if (token->type == CPP_EOF)
742 break;
744 /* Subtle logic to output a space if and only if necessary. */
745 if (avoid_paste)
747 if (print.source == NULL)
748 print.source = token;
749 if (print.source->flags & PREV_WHITE
750 || (print.prev
751 && cpp_avoid_paste (pfile, print.prev, token))
752 || (print.prev == NULL && token->type == CPP_HASH))
753 putc (' ', print.outf);
755 else if (token->flags & PREV_WHITE)
756 putc (' ', print.outf);
758 avoid_paste = false;
759 print.source = NULL;
760 print.prev = token;
761 cpp_output_token (token, print.outf);
763 if (token->type == CPP_COMMENT)
764 account_for_newlines (token->val.str.text, token->val.str.len);
768 /* Adjust print.src_line for newlines embedded in output. */
769 static void
770 account_for_newlines (const unsigned char *str, size_t len)
772 while (len--)
773 if (*str++ == '\n')
774 print.src_line++;
777 /* Writes out a traditionally preprocessed file. */
778 static void
779 scan_translation_unit_trad (cpp_reader *pfile)
781 while (_cpp_read_logical_line_trad (pfile))
783 size_t len = pfile->out.cur - pfile->out.base;
784 maybe_print_line (pfile->out.first_line);
785 fwrite (pfile->out.base, 1, len, print.outf);
786 print.printed = 1;
787 if (!CPP_OPTION (pfile, discard_comments))
788 account_for_newlines (pfile->out.base, len);
792 /* If the token read on logical line LINE needs to be output on a
793 different line to the current one, output the required newlines or
794 a line marker. */
795 static void
796 maybe_print_line (source_location src_loc)
798 const struct line_map *map = linemap_lookup (line_table, src_loc);
799 int src_line = SOURCE_LINE (map, src_loc);
801 /* End the previous line of text. */
802 if (print.printed)
804 putc ('\n', print.outf);
805 print.src_line++;
806 print.printed = 0;
809 if (src_line >= print.src_line && src_line < print.src_line + 8)
811 while (src_line > print.src_line)
813 putc ('\n', print.outf);
814 print.src_line++;
817 else
818 print_line (src_loc, "");
821 /* Output a line marker for logical line LINE. Special flags are "1"
822 or "2" indicating entering or leaving a file. */
823 static void
824 print_line (source_location src_loc, const char *special_flags)
826 /* End any previous line of text. */
827 if (print.printed)
828 putc ('\n', print.outf);
829 print.printed = 0;
831 if (!gfc_cpp_option.no_line_commands)
833 expanded_location loc;
834 size_t to_file_len;
835 unsigned char *to_file_quoted;
836 unsigned char *p;
837 int sysp;
839 loc = expand_location (src_loc);
840 to_file_len = strlen (loc.file);
841 to_file_quoted = (unsigned char *) alloca (to_file_len * 4 + 1);
843 print.src_line = loc.line;
845 /* cpp_quote_string does not nul-terminate, so we have to do it
846 ourselves. */
847 p = cpp_quote_string (to_file_quoted,
848 (const unsigned char *) loc.file, to_file_len);
849 *p = '\0';
850 fprintf (print.outf, "# %u \"%s\"%s",
851 print.src_line == 0 ? 1 : print.src_line,
852 to_file_quoted, special_flags);
854 sysp = in_system_header_at (src_loc);
855 if (sysp == 2)
856 fputs (" 3 4", print.outf);
857 else if (sysp == 1)
858 fputs (" 3", print.outf);
860 putc ('\n', print.outf);
864 static void
865 cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map)
867 const char *flags = "";
869 if (gfc_cpp_option.no_line_commands)
870 return;
872 if (!map)
873 return;
875 if (print.first_time)
877 /* Avoid printing foo.i when the main file is foo.c. */
878 if (!cpp_get_options (cpp_in)->preprocessed)
879 print_line (map->start_location, flags);
880 print.first_time = 0;
882 else
884 /* Bring current file to correct line when entering a new file. */
885 if (map->reason == LC_ENTER)
887 const struct line_map *from = INCLUDED_FROM (line_table, map);
888 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
890 if (map->reason == LC_ENTER)
891 flags = " 1";
892 else if (map->reason == LC_LEAVE)
893 flags = " 2";
894 print_line (map->start_location, flags);
899 /* Called when a line of output is started. TOKEN is the first token
900 of the line, and at end of file will be CPP_EOF. */
901 static void
902 cb_line_change (cpp_reader *pfile, const cpp_token *token,
903 int parsing_args)
905 source_location src_loc = token->src_loc;
907 if (token->type == CPP_EOF || parsing_args)
908 return;
910 maybe_print_line (src_loc);
911 print.prev = 0;
912 print.source = 0;
914 /* Supply enough spaces to put this token in its original column,
915 one space per column greater than 2, since scan_translation_unit
916 will provide a space if PREV_WHITE. Don't bother trying to
917 reconstruct tabs; we can't get it right in general, and nothing
918 ought to care. Some things do care; the fault lies with them. */
919 if (!CPP_OPTION (pfile, traditional))
921 const struct line_map *map = linemap_lookup (line_table, src_loc);
922 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
923 print.printed = 1;
925 while (-- spaces >= 0)
926 putc (' ', print.outf);
930 static void
931 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
932 const cpp_string *str)
934 maybe_print_line (line);
935 fprintf (print.outf, "#ident %s\n", str->text);
936 print.src_line++;
939 static void
940 cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
941 cpp_hashnode *node ATTRIBUTE_UNUSED)
943 maybe_print_line (line);
944 fputs ("#define ", print.outf);
946 /* 'D' is whole definition; 'N' is name only. */
947 if (gfc_cpp_option.dump_macros == 'D')
948 fputs ((const char *) cpp_macro_definition (pfile, node),
949 print.outf);
950 else
951 fputs ((const char *) NODE_NAME (node), print.outf);
953 putc ('\n', print.outf);
954 if (LOCATION_LINE (line) != 0)
955 print.src_line++;
958 static void
959 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
960 cpp_hashnode *node)
962 maybe_print_line (line);
963 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
964 print.src_line++;
967 static void
968 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
969 const unsigned char *dir, const char *header, int angle_brackets,
970 const cpp_token **comments)
972 maybe_print_line (line);
973 if (angle_brackets)
974 fprintf (print.outf, "#%s <%s>", dir, header);
975 else
976 fprintf (print.outf, "#%s \"%s\"", dir, header);
978 if (comments != NULL)
980 while (*comments != NULL)
982 if ((*comments)->flags & PREV_WHITE)
983 putc (' ', print.outf);
984 cpp_output_token (*comments, print.outf);
985 ++comments;
989 putc ('\n', print.outf);
990 print.src_line++;
993 /* Dump out the hash table. */
994 static int
995 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
997 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
999 fputs ("#define ", print.outf);
1000 fputs ((const char *) cpp_macro_definition (pfile, node),
1001 print.outf);
1002 putc ('\n', print.outf);
1003 print.src_line++;
1006 return 1;
1009 static void
1010 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
1011 cpp_hashnode *node)
1013 gfc_cpp_macro_queue *q;
1014 q = XNEW (gfc_cpp_macro_queue);
1015 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
1016 q->next = cpp_define_queue;
1017 cpp_define_queue = q;
1020 /* Callback from cpp_error for PFILE to print diagnostics from the
1021 preprocessor. The diagnostic is of type LEVEL, with REASON set
1022 to the reason code if LEVEL is represents a warning, at location
1023 LOCATION, with column number possibly overridden by COLUMN_OVERRIDE
1024 if not zero; MSG is the translated message and AP the arguments.
1025 Returns true if a diagnostic was emitted, false otherwise. */
1027 static bool
1028 cb_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
1029 location_t location, unsigned int column_override,
1030 const char *msg, va_list *ap)
1032 diagnostic_info diagnostic;
1033 diagnostic_t dlevel;
1034 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
1035 bool ret;
1037 switch (level)
1039 case CPP_DL_WARNING_SYSHDR:
1040 global_dc->dc_warn_system_headers = 1;
1041 /* Fall through. */
1042 case CPP_DL_WARNING:
1043 dlevel = DK_WARNING;
1044 break;
1045 case CPP_DL_PEDWARN:
1046 dlevel = DK_PEDWARN;
1047 break;
1048 case CPP_DL_ERROR:
1049 dlevel = DK_ERROR;
1050 break;
1051 case CPP_DL_ICE:
1052 dlevel = DK_ICE;
1053 break;
1054 case CPP_DL_NOTE:
1055 dlevel = DK_NOTE;
1056 break;
1057 case CPP_DL_FATAL:
1058 dlevel = DK_FATAL;
1059 break;
1060 default:
1061 gcc_unreachable ();
1063 diagnostic_set_info_translated (&diagnostic, msg, ap,
1064 location, dlevel);
1065 if (column_override)
1066 diagnostic_override_column (&diagnostic, column_override);
1067 if (reason == CPP_W_WARNING_DIRECTIVE)
1068 diagnostic_override_option_index (&diagnostic, OPT_Wcpp);
1069 ret = report_diagnostic (&diagnostic);
1070 if (level == CPP_DL_WARNING_SYSHDR)
1071 global_dc->dc_warn_system_headers = save_warn_system_headers;
1072 return ret;
1075 /* Callback called when -fworking-director and -E to emit working
1076 directory in cpp output file. */
1078 void
1079 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1081 size_t to_file_len = strlen (dir);
1082 unsigned char *to_file_quoted =
1083 (unsigned char *) alloca (to_file_len * 4 + 1);
1084 unsigned char *p;
1086 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
1087 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
1088 *p = '\0';
1089 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
1092 /* Copy a #pragma directive to the preprocessed output. */
1093 static void
1094 cb_def_pragma (cpp_reader *pfile, source_location line)
1096 maybe_print_line (line);
1097 fputs ("#pragma ", print.outf);
1098 cpp_output_line (pfile, print.outf);
1099 print.src_line++;
1102 static void
1103 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
1104 source_location line ATTRIBUTE_UNUSED,
1105 cpp_hashnode *node)
1107 gfc_cpp_macro_queue *q;
1108 q = XNEW (gfc_cpp_macro_queue);
1109 q->macro = xstrdup ((const char *) NODE_NAME (node));
1110 q->next = cpp_undefine_queue;
1111 cpp_undefine_queue = q;
1114 static void
1115 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
1117 gfc_cpp_macro_queue *q;
1119 /* End the previous line of text. */
1120 if (print.printed)
1122 putc ('\n', print.outf);
1123 print.src_line++;
1124 print.printed = 0;
1127 for (q = cpp_define_queue; q;)
1129 gfc_cpp_macro_queue *oq;
1130 fputs ("#define ", print.outf);
1131 fputs (q->macro, print.outf);
1132 putc ('\n', print.outf);
1133 print.src_line++;
1134 oq = q;
1135 q = q->next;
1136 free (oq->macro);
1137 free (oq);
1139 cpp_define_queue = NULL;
1140 for (q = cpp_undefine_queue; q;)
1142 gfc_cpp_macro_queue *oq;
1143 fprintf (print.outf, "#undef %s\n", q->macro);
1144 print.src_line++;
1145 oq = q;
1146 q = q->next;
1147 free (oq->macro);
1148 free (oq);
1150 cpp_undefine_queue = NULL;