1 /* Copyright (C) 2008, 2009, 2010 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
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
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/>. */
21 #include "coretypes.h"
30 #include "tm_p.h" /* Target prototypes. */
33 #include "diagnostic.h"
35 #include "../../libcpp/internal.h"
38 #include "cppbuiltin.h"
41 #ifndef TARGET_CPU_CPP_BUILTINS
42 # define TARGET_CPU_CPP_BUILTINS()
45 #ifndef TARGET_OS_CPP_BUILTINS
46 # define TARGET_OS_CPP_BUILTINS()
49 #ifndef TARGET_OBJFMT_CPP_BUILTINS
50 # define TARGET_OBJFMT_CPP_BUILTINS()
54 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
55 handling is deferred to gfc_cpp_init (). */
61 gfc_cpp_deferred_opt_t;
64 /* Defined and undefined macros being queued for output with -dU at
66 typedef struct gfc_cpp_macro_queue
68 struct gfc_cpp_macro_queue *next; /* Next macro in the list. */
69 char *macro; /* The name of the macro if not
70 defined, the full definition if
72 } gfc_cpp_macro_queue;
73 static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
75 struct gfc_cpp_option_data
77 /* Argument of -cpp, implied by SPEC;
78 if NULL, preprocessing disabled. */
79 const char *temporary_filename;
81 const char *output_filename; /* -o <arg> */
82 int preprocess_only; /* -E */
83 int discard_comments; /* -C */
84 int discard_comments_in_macro_exp; /* -CC */
85 int print_include_names; /* -H */
86 int no_line_commands; /* -P */
87 char dump_macros; /* -d[DMNU] */
88 int dump_includes; /* -dI */
89 int working_directory; /* -fworking-directory */
90 int no_predefined; /* -undef */
91 int standard_include_paths; /* -nostdinc */
94 int deps_skip_system; /* -MM */
95 const char *deps_filename; /* -M[M]D */
96 const char *deps_filename_user; /* -MF <arg> */
97 int deps_missing_are_generated; /* -MG */
98 int deps_phony; /* -MP */
100 const char *multilib; /* -imultilib <dir> */
101 const char *prefix; /* -iprefix <dir> */
102 const char *sysroot; /* -isysroot <dir> */
104 /* Options whose handling needs to be deferred until the
105 appropriate cpp-objects are created:
109 gfc_cpp_deferred_opt_t *deferred_opt;
110 int deferred_opt_count;
114 /* Structures used with libcpp: */
115 static cpp_options *cpp_option = NULL;
116 static cpp_reader *cpp_in = NULL;
118 /* Encapsulates state used to convert a stream of cpp-tokens into
122 FILE *outf; /* Stream to write to. */
123 const cpp_token *prev; /* Previous token. */
124 const cpp_token *source; /* Source token for spacing. */
125 int src_line; /* Line number currently being written. */
126 unsigned char printed; /* Nonzero if something output at line. */
127 bool first_time; /* cb_file_change hasn't been called yet. */
130 /* General output routines. */
131 static void scan_translation_unit (cpp_reader *);
132 static void scan_translation_unit_trad (cpp_reader *);
134 /* Callback routines for the parser. Most of these are active only
135 in specific modes. */
136 static void cb_file_change (cpp_reader *, const struct line_map *);
137 static void cb_line_change (cpp_reader *, const cpp_token *, int);
138 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
139 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
140 static void cb_def_pragma (cpp_reader *, source_location);
141 static void cb_include (cpp_reader *, source_location, const unsigned char *,
142 const char *, int, const cpp_token **);
143 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
144 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
145 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
146 static bool cb_cpp_error (cpp_reader *, int, int, location_t, unsigned int,
147 const char *, va_list *)
148 ATTRIBUTE_GCC_DIAG(6,0);
149 void pp_dir_change (cpp_reader *, const char *);
151 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
152 static void dump_queued_macros (cpp_reader *);
156 cpp_define_builtins (cpp_reader *pfile)
158 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
159 in C, defines __STDC_HOSTED__?! */
160 cpp_init_builtins (pfile, 0);
162 /* Initialize GFORTRAN specific builtins.
163 These are documented. */
164 define_language_independent_builtin_macros (pfile);
165 cpp_define (pfile, "__GFORTRAN__=1");
166 cpp_define (pfile, "_LANGUAGE_FORTRAN=1");
168 if (gfc_option.gfc_flag_openmp)
169 cpp_define (pfile, "_OPENMP=200805");
171 /* The defines below are necessary for the TARGET_* macros.
173 FIXME: Note that builtin_define_std() actually is a function
174 in c-cppbuiltin.c which uses flags undefined for Fortran.
175 Let's skip this for now. If needed, one needs to look into it
178 # define builtin_define(TXT) cpp_define (pfile, TXT)
179 # define builtin_define_std(TXT)
180 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
182 /* FIXME: Pandora's Box
183 Using the macros below results in multiple breakages:
184 - mingw will fail to compile this file as dependent macros
185 assume to be used in c-cppbuiltin.c only. Further, they use
186 flags only valid/defined in C (same as noted above).
187 [config/i386/mingw32.h, config/i386/cygming.h]
188 - other platforms (not as popular) break similarly
189 [grep for 'builtin_define_with_int_value' in gcc/config/]
191 TARGET_CPU_CPP_BUILTINS ();
192 TARGET_OS_CPP_BUILTINS ();
193 TARGET_OBJFMT_CPP_BUILTINS (); */
195 #undef builtin_define
196 #undef builtin_define_std
197 #undef builtin_assert
201 gfc_cpp_enabled (void)
203 return gfc_cpp_option.temporary_filename != NULL;
207 gfc_cpp_preprocess_only (void)
209 return gfc_cpp_option.preprocess_only;
213 gfc_cpp_makedep (void)
215 return gfc_cpp_option.deps;
219 gfc_cpp_add_dep (const char *name, bool system)
221 if (!gfc_cpp_option.deps_skip_system || !system)
222 deps_add_dep (cpp_get_deps (cpp_in), name);
226 gfc_cpp_add_target (const char *name)
228 deps_add_target (cpp_get_deps (cpp_in), name, 0);
233 gfc_cpp_temporary_file (void)
235 return gfc_cpp_option.temporary_filename;
239 gfc_cpp_init_options (unsigned int decoded_options_count,
240 struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
242 /* Do not create any objects from libcpp here. If no
243 preprocessing is requested, this would be wasted
246 See gfc_cpp_post_options() instead. */
248 gfc_cpp_option.temporary_filename = NULL;
249 gfc_cpp_option.output_filename = NULL;
250 gfc_cpp_option.preprocess_only = 0;
251 gfc_cpp_option.discard_comments = 1;
252 gfc_cpp_option.discard_comments_in_macro_exp = 1;
253 gfc_cpp_option.print_include_names = 0;
254 gfc_cpp_option.no_line_commands = 0;
255 gfc_cpp_option.dump_macros = '\0';
256 gfc_cpp_option.dump_includes = 0;
257 gfc_cpp_option.working_directory = -1;
258 gfc_cpp_option.no_predefined = 0;
259 gfc_cpp_option.standard_include_paths = 1;
260 gfc_cpp_option.verbose = 0;
261 gfc_cpp_option.deps = 0;
262 gfc_cpp_option.deps_skip_system = 0;
263 gfc_cpp_option.deps_phony = 0;
264 gfc_cpp_option.deps_missing_are_generated = 0;
265 gfc_cpp_option.deps_filename = NULL;
266 gfc_cpp_option.deps_filename_user = NULL;
268 gfc_cpp_option.multilib = NULL;
269 gfc_cpp_option.prefix = NULL;
270 gfc_cpp_option.sysroot = NULL;
272 gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t,
273 decoded_options_count);
274 gfc_cpp_option.deferred_opt_count = 0;
278 gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
281 enum opt_code code = (enum opt_code) scode;
290 gfc_cpp_option.temporary_filename = arg;
294 gfc_cpp_option.temporary_filename = 0L;
305 gfc_cpp_option.dump_macros = *arg;
309 gfc_cpp_option.dump_includes = 1;
314 case OPT_fworking_directory:
315 gfc_cpp_option.working_directory = value;
319 gfc_cpp_add_include_path_after (xstrdup(arg), true);
323 gfc_cpp_option.multilib = arg;
327 gfc_cpp_option.prefix = arg;
331 gfc_cpp_option.sysroot = arg;
336 gfc_cpp_add_include_path (xstrdup(arg), true);
340 gfc_cpp_option.standard_include_paths = value;
344 if (!gfc_cpp_option.output_filename)
345 gfc_cpp_option.output_filename = arg;
347 gfc_fatal_error ("output filename specified twice");
351 gfc_cpp_option.no_predefined = value;
355 gfc_cpp_option.verbose = value;
361 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
362 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
363 gfc_cpp_option.deferred_opt_count++;
367 gfc_cpp_option.discard_comments = 0;
371 gfc_cpp_option.discard_comments = 0;
372 gfc_cpp_option.discard_comments_in_macro_exp = 0;
376 gfc_cpp_option.preprocess_only = 1;
380 gfc_cpp_option.print_include_names = 1;
384 gfc_cpp_option.deps_skip_system = 1;
388 gfc_cpp_option.deps = 1;
392 gfc_cpp_option.deps_skip_system = 1;
396 gfc_cpp_option.deps = 1;
397 gfc_cpp_option.deps_filename = arg;
401 /* If specified multiple times, last one wins. */
402 gfc_cpp_option.deps_filename_user = arg;
406 gfc_cpp_option.deps_missing_are_generated = 1;
410 gfc_cpp_option.deps_phony = 1;
415 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
416 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
417 gfc_cpp_option.deferred_opt_count++;
421 gfc_cpp_option.no_line_commands = 1;
430 gfc_cpp_post_options (void)
432 /* Any preprocessing-related option without '-cpp' is considered
434 if (!gfc_cpp_enabled ()
435 && (gfc_cpp_preprocess_only ()
436 || gfc_cpp_makedep ()
437 || !gfc_cpp_option.discard_comments
438 || !gfc_cpp_option.discard_comments_in_macro_exp
439 || gfc_cpp_option.print_include_names
440 || gfc_cpp_option.no_line_commands
441 || gfc_cpp_option.dump_macros
442 || gfc_cpp_option.dump_includes))
443 gfc_fatal_error("To enable preprocessing, use -cpp");
445 if (!gfc_cpp_enabled ())
448 cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
451 /* The cpp_options-structure defines far more flags than those set here.
452 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
453 inter-option dependencies that may need to be enforced. */
454 cpp_option = cpp_get_options (cpp_in);
455 gcc_assert (cpp_option);
457 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
458 cpp_option->traditional = 1;
459 cpp_option->cplusplus_comments = 0;
461 cpp_option->cpp_pedantic = pedantic;
463 cpp_option->dollars_in_ident = gfc_option.flag_dollar_ok;
464 cpp_option->discard_comments = gfc_cpp_option.discard_comments;
465 cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
466 cpp_option->print_include_names = gfc_cpp_option.print_include_names;
467 cpp_option->preprocessed = gfc_option.flag_preprocessed;
469 if (gfc_cpp_makedep ())
471 cpp_option->deps.style = DEPS_USER;
472 cpp_option->deps.phony_targets = gfc_cpp_option.deps_phony;
473 cpp_option->deps.missing_files = gfc_cpp_option.deps_missing_are_generated;
475 /* -MF <arg> overrides -M[M]D. */
476 if (gfc_cpp_option.deps_filename_user)
477 gfc_cpp_option.deps_filename = gfc_cpp_option.deps_filename_user;
480 if (gfc_cpp_option.working_directory == -1)
481 gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
483 cpp_post_options (cpp_in);
485 gfc_cpp_register_include_paths ();
490 gfc_cpp_init_0 (void)
492 struct cpp_callbacks *cb;
494 cb = cpp_get_callbacks (cpp_in);
495 cb->file_change = cb_file_change;
496 cb->line_change = cb_line_change;
497 cb->ident = cb_ident;
498 cb->def_pragma = cb_def_pragma;
499 cb->error = cb_cpp_error;
501 if (gfc_cpp_option.dump_includes)
502 cb->include = cb_include;
504 if ((gfc_cpp_option.dump_macros == 'D')
505 || (gfc_cpp_option.dump_macros == 'N'))
507 cb->define = cb_define;
508 cb->undef = cb_undef;
511 if (gfc_cpp_option.dump_macros == 'U')
513 cb->before_define = dump_queued_macros;
514 cb->used_define = cb_used_define;
515 cb->used_undef = cb_used_undef;
518 /* Initialize the print structure. Setting print.src_line to -1 here is
519 a trick to guarantee that the first token of the file will cause
520 a linemarker to be output by maybe_print_line. */
524 print.first_time = 1;
526 if (gfc_cpp_preprocess_only ())
528 if (gfc_cpp_option.output_filename)
530 /* This needs cheating: with "-E -o <file>", the user wants the
531 preprocessed output in <file>. However, if nothing is done
532 about it <file> is also used for assembler output. Hence, it
533 is necessary to redirect assembler output (actually nothing
534 as -E implies -fsyntax-only) to another file, otherwise the
535 output from preprocessing is lost. */
536 asm_file_name = gfc_cpp_option.temporary_filename;
538 print.outf = fopen (gfc_cpp_option.output_filename, "w");
539 if (print.outf == NULL)
540 gfc_fatal_error ("opening output file %s: %s",
541 gfc_cpp_option.output_filename,
549 print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
550 if (print.outf == NULL)
551 gfc_fatal_error ("opening output file %s: %s",
552 gfc_cpp_option.temporary_filename, xstrerror (errno));
556 if (!cpp_read_main_file (cpp_in, gfc_source_file))
565 if (gfc_option.flag_preprocessed)
568 cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
569 if (!gfc_cpp_option.no_predefined)
570 cpp_define_builtins (cpp_in);
572 /* Handle deferred options from command-line. */
573 cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
575 for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
577 gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
579 if (opt->code == OPT_D)
580 cpp_define (cpp_in, opt->arg);
581 else if (opt->code == OPT_U)
582 cpp_undef (cpp_in, opt->arg);
583 else if (opt->code == OPT_A)
585 if (opt->arg[0] == '-')
586 cpp_unassert (cpp_in, opt->arg + 1);
588 cpp_assert (cpp_in, opt->arg);
590 else if (opt->code == OPT_MT || opt->code == OPT_MQ)
591 deps_add_target (cpp_get_deps (cpp_in),
592 opt->arg, opt->code == OPT_MQ);
595 if (gfc_cpp_option.working_directory
596 && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
597 pp_dir_change (cpp_in, get_src_pwd ());
601 gfc_cpp_preprocess (const char *source_file)
603 if (!gfc_cpp_enabled ())
606 cpp_change_file (cpp_in, LC_RENAME, source_file);
608 if (cpp_option->traditional)
609 scan_translation_unit_trad (cpp_in);
611 scan_translation_unit (cpp_in);
613 /* -dM command line option. */
614 if (gfc_cpp_preprocess_only () &&
615 gfc_cpp_option.dump_macros == 'M')
617 putc ('\n', print.outf);
618 cpp_forall_identifiers (cpp_in, dump_macro, NULL);
621 putc ('\n', print.outf);
623 if (!gfc_cpp_preprocess_only ()
624 || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
633 if (!gfc_cpp_enabled ())
638 if (gfc_cpp_makedep ())
640 if (gfc_cpp_option.deps_filename)
642 FILE *f = fopen (gfc_cpp_option.deps_filename, "w");
645 cpp_finish (cpp_in, f);
649 gfc_fatal_error ("opening output file %s: %s",
650 gfc_cpp_option.deps_filename,
654 cpp_finish (cpp_in, stdout);
657 cpp_undef_all (cpp_in);
658 cpp_clear_file_cache (cpp_in);
661 /* PATH must be malloc-ed and NULL-terminated. */
663 gfc_cpp_add_include_path (char *path, bool user_supplied)
665 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
666 include path. Fortran does not define any system include paths. */
669 add_path (path, BRACKET, cxx_aware, user_supplied);
673 gfc_cpp_add_include_path_after (char *path, bool user_supplied)
676 add_path (path, AFTER, cxx_aware, user_supplied);
680 gfc_cpp_register_include_paths (void)
683 register_include_chains (cpp_in, gfc_cpp_option.sysroot,
684 gfc_cpp_option.prefix, gfc_cpp_option.multilib,
685 gfc_cpp_option.standard_include_paths, cxx_stdinc,
686 gfc_cpp_option.verbose);
691 static void scan_translation_unit_trad (cpp_reader *);
692 static void account_for_newlines (const unsigned char *, size_t);
693 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
695 static void print_line (source_location, const char *);
696 static void maybe_print_line (source_location);
699 /* Writes out the preprocessed file, handling spacing and paste
702 scan_translation_unit (cpp_reader *pfile)
704 bool avoid_paste = false;
709 const cpp_token *token = cpp_get_token (pfile);
711 if (token->type == CPP_PADDING)
714 if (print.source == NULL
715 || (!(print.source->flags & PREV_WHITE)
716 && token->val.source == NULL))
717 print.source = token->val.source;
721 if (token->type == CPP_EOF)
724 /* Subtle logic to output a space if and only if necessary. */
727 if (print.source == NULL)
728 print.source = token;
729 if (print.source->flags & PREV_WHITE
731 && cpp_avoid_paste (pfile, print.prev, token))
732 || (print.prev == NULL && token->type == CPP_HASH))
733 putc (' ', print.outf);
735 else if (token->flags & PREV_WHITE)
736 putc (' ', print.outf);
741 cpp_output_token (token, print.outf);
743 if (token->type == CPP_COMMENT)
744 account_for_newlines (token->val.str.text, token->val.str.len);
748 /* Adjust print.src_line for newlines embedded in output. */
750 account_for_newlines (const unsigned char *str, size_t len)
757 /* Writes out a traditionally preprocessed file. */
759 scan_translation_unit_trad (cpp_reader *pfile)
761 while (_cpp_read_logical_line_trad (pfile))
763 size_t len = pfile->out.cur - pfile->out.base;
764 maybe_print_line (pfile->out.first_line);
765 fwrite (pfile->out.base, 1, len, print.outf);
767 if (!CPP_OPTION (pfile, discard_comments))
768 account_for_newlines (pfile->out.base, len);
772 /* If the token read on logical line LINE needs to be output on a
773 different line to the current one, output the required newlines or
776 maybe_print_line (source_location src_loc)
778 const struct line_map *map = linemap_lookup (line_table, src_loc);
779 int src_line = SOURCE_LINE (map, src_loc);
781 /* End the previous line of text. */
784 putc ('\n', print.outf);
789 if (src_line >= print.src_line && src_line < print.src_line + 8)
791 while (src_line > print.src_line)
793 putc ('\n', print.outf);
798 print_line (src_loc, "");
801 /* Output a line marker for logical line LINE. Special flags are "1"
802 or "2" indicating entering or leaving a file. */
804 print_line (source_location src_loc, const char *special_flags)
806 /* End any previous line of text. */
808 putc ('\n', print.outf);
811 if (!gfc_cpp_option.no_line_commands)
813 const struct line_map *map = linemap_lookup (line_table, src_loc);
815 size_t to_file_len = strlen (map->to_file);
816 unsigned char *to_file_quoted =
817 (unsigned char *) alloca (to_file_len * 4 + 1);
820 print.src_line = SOURCE_LINE (map, src_loc);
822 /* cpp_quote_string does not nul-terminate, so we have to do it
824 p = cpp_quote_string (to_file_quoted,
825 (const unsigned char *) map->to_file, to_file_len);
827 fprintf (print.outf, "# %u \"%s\"%s",
828 print.src_line == 0 ? 1 : print.src_line,
829 to_file_quoted, special_flags);
832 fputs (" 3 4", print.outf);
833 else if (map->sysp == 1)
834 fputs (" 3", print.outf);
836 putc ('\n', print.outf);
841 cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map)
843 const char *flags = "";
845 if (gfc_cpp_option.no_line_commands)
851 if (print.first_time)
853 /* Avoid printing foo.i when the main file is foo.c. */
854 if (!cpp_get_options (cpp_in)->preprocessed)
855 print_line (map->start_location, flags);
856 print.first_time = 0;
860 /* Bring current file to correct line when entering a new file. */
861 if (map->reason == LC_ENTER)
863 const struct line_map *from = INCLUDED_FROM (line_table, map);
864 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
866 if (map->reason == LC_ENTER)
868 else if (map->reason == LC_LEAVE)
870 print_line (map->start_location, flags);
875 /* Called when a line of output is started. TOKEN is the first token
876 of the line, and at end of file will be CPP_EOF. */
878 cb_line_change (cpp_reader *pfile, const cpp_token *token,
881 source_location src_loc = token->src_loc;
883 if (token->type == CPP_EOF || parsing_args)
886 maybe_print_line (src_loc);
890 /* Supply enough spaces to put this token in its original column,
891 one space per column greater than 2, since scan_translation_unit
892 will provide a space if PREV_WHITE. Don't bother trying to
893 reconstruct tabs; we can't get it right in general, and nothing
894 ought to care. Some things do care; the fault lies with them. */
895 if (!CPP_OPTION (pfile, traditional))
897 const struct line_map *map = linemap_lookup (line_table, src_loc);
898 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
901 while (-- spaces >= 0)
902 putc (' ', print.outf);
907 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
908 const cpp_string *str)
910 maybe_print_line (line);
911 fprintf (print.outf, "#ident %s\n", str->text);
916 cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
917 cpp_hashnode *node ATTRIBUTE_UNUSED)
919 maybe_print_line (line);
920 fputs ("#define ", print.outf);
922 /* 'D' is whole definition; 'N' is name only. */
923 if (gfc_cpp_option.dump_macros == 'D')
924 fputs ((const char *) cpp_macro_definition (pfile, node),
927 fputs ((const char *) NODE_NAME (node), print.outf);
929 putc ('\n', print.outf);
930 if (linemap_lookup (line_table, line)->to_line != 0)
935 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
938 maybe_print_line (line);
939 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
944 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
945 const unsigned char *dir, const char *header, int angle_brackets,
946 const cpp_token **comments)
948 maybe_print_line (line);
950 fprintf (print.outf, "#%s <%s>", dir, header);
952 fprintf (print.outf, "#%s \"%s\"", dir, header);
954 if (comments != NULL)
956 while (*comments != NULL)
958 if ((*comments)->flags & PREV_WHITE)
959 putc (' ', print.outf);
960 cpp_output_token (*comments, print.outf);
965 putc ('\n', print.outf);
969 /* Dump out the hash table. */
971 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
973 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
975 fputs ("#define ", print.outf);
976 fputs ((const char *) cpp_macro_definition (pfile, node),
978 putc ('\n', print.outf);
986 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
989 gfc_cpp_macro_queue *q;
990 q = XNEW (gfc_cpp_macro_queue);
991 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
992 q->next = cpp_define_queue;
993 cpp_define_queue = q;
996 /* Callback from cpp_error for PFILE to print diagnostics from the
997 preprocessor. The diagnostic is of type LEVEL, with REASON set
998 to the reason code if LEVEL is represents a warning, at location
999 LOCATION, with column number possibly overridden by COLUMN_OVERRIDE
1000 if not zero; MSG is the translated message and AP the arguments.
1001 Returns true if a diagnostic was emitted, false otherwise. */
1004 cb_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
1005 location_t location, unsigned int column_override,
1006 const char *msg, va_list *ap)
1008 diagnostic_info diagnostic;
1009 diagnostic_t dlevel;
1010 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
1015 case CPP_DL_WARNING_SYSHDR:
1016 global_dc->dc_warn_system_headers = 1;
1018 case CPP_DL_WARNING:
1019 dlevel = DK_WARNING;
1021 case CPP_DL_PEDWARN:
1022 dlevel = DK_PEDWARN;
1039 diagnostic_set_info_translated (&diagnostic, msg, ap,
1041 if (column_override)
1042 diagnostic_override_column (&diagnostic, column_override);
1043 if (reason == CPP_W_WARNING_DIRECTIVE)
1044 diagnostic_override_option_index (&diagnostic, OPT_Wcpp);
1045 ret = report_diagnostic (&diagnostic);
1046 if (level == CPP_DL_WARNING_SYSHDR)
1047 global_dc->dc_warn_system_headers = save_warn_system_headers;
1051 /* Callback called when -fworking-director and -E to emit working
1052 directory in cpp output file. */
1055 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1057 size_t to_file_len = strlen (dir);
1058 unsigned char *to_file_quoted =
1059 (unsigned char *) alloca (to_file_len * 4 + 1);
1062 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
1063 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
1065 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
1068 /* Copy a #pragma directive to the preprocessed output. */
1070 cb_def_pragma (cpp_reader *pfile, source_location line)
1072 maybe_print_line (line);
1073 fputs ("#pragma ", print.outf);
1074 cpp_output_line (pfile, print.outf);
1079 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
1080 source_location line ATTRIBUTE_UNUSED,
1083 gfc_cpp_macro_queue *q;
1084 q = XNEW (gfc_cpp_macro_queue);
1085 q->macro = xstrdup ((const char *) NODE_NAME (node));
1086 q->next = cpp_undefine_queue;
1087 cpp_undefine_queue = q;
1091 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
1093 gfc_cpp_macro_queue *q;
1095 /* End the previous line of text. */
1098 putc ('\n', print.outf);
1103 for (q = cpp_define_queue; q;)
1105 gfc_cpp_macro_queue *oq;
1106 fputs ("#define ", print.outf);
1107 fputs (q->macro, print.outf);
1108 putc ('\n', print.outf);
1115 cpp_define_queue = NULL;
1116 for (q = cpp_undefine_queue; q;)
1118 gfc_cpp_macro_queue *oq;
1119 fprintf (print.outf, "#undef %s\n", q->macro);
1126 cpp_undefine_queue = NULL;