langhooks-def.h (lhd_init_options, [...]): New.
[official-gcc.git] / gcc / fortran / cpp.c
blobb98d83f1b483803d16605c75c8c3c0d55c67b51a
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
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 "mkdeps.h"
40 #ifndef TARGET_OS_CPP_BUILTINS
41 # define TARGET_OS_CPP_BUILTINS()
42 #endif
44 #ifndef TARGET_OBJFMT_CPP_BUILTINS
45 # define TARGET_OBJFMT_CPP_BUILTINS()
46 #endif
49 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
50 handling is deferred to gfc_cpp_init (). */
51 typedef struct
53 enum opt_code code;
54 const char *arg;
56 gfc_cpp_deferred_opt_t;
59 /* Defined and undefined macros being queued for output with -dU at
60 the next newline. */
61 typedef struct gfc_cpp_macro_queue
63 struct gfc_cpp_macro_queue *next; /* Next macro in the list. */
64 char *macro; /* The name of the macro if not
65 defined, the full definition if
66 defined. */
67 } gfc_cpp_macro_queue;
68 static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
70 struct gfc_cpp_option_data
72 /* Argument of -cpp, implied by SPEC;
73 if NULL, preprocessing disabled. */
74 const char *temporary_filename;
76 const char *output_filename; /* -o <arg> */
77 int preprocess_only; /* -E */
78 int discard_comments; /* -C */
79 int discard_comments_in_macro_exp; /* -CC */
80 int print_include_names; /* -H */
81 int no_line_commands; /* -P */
82 char dump_macros; /* -d[DMNU] */
83 int dump_includes; /* -dI */
84 int working_directory; /* -fworking-directory */
85 int no_predefined; /* -undef */
86 int standard_include_paths; /* -nostdinc */
87 int verbose; /* -v */
88 int deps; /* -M */
89 int deps_skip_system; /* -MM */
90 const char *deps_filename; /* -M[M]D */
91 const char *deps_filename_user; /* -MF <arg> */
92 int deps_missing_are_generated; /* -MG */
93 int deps_phony; /* -MP */
95 const char *multilib; /* -imultilib <dir> */
96 const char *prefix; /* -iprefix <dir> */
97 const char *sysroot; /* -isysroot <dir> */
99 /* Options whose handling needs to be deferred until the
100 appropriate cpp-objects are created:
101 -A predicate=answer
102 -D <macro>[=<val>]
103 -U <macro> */
104 gfc_cpp_deferred_opt_t *deferred_opt;
105 int deferred_opt_count;
107 gfc_cpp_option;
109 /* Structures used with libcpp: */
110 static cpp_options *cpp_option = NULL;
111 static cpp_reader *cpp_in = NULL;
113 /* Defined in toplev.c. */
114 extern const char *asm_file_name;
119 /* Encapsulates state used to convert a stream of cpp-tokens into
120 a text file. */
121 static struct
123 FILE *outf; /* Stream to write to. */
124 const cpp_token *prev; /* Previous token. */
125 const cpp_token *source; /* Source token for spacing. */
126 int src_line; /* Line number currently being written. */
127 unsigned char printed; /* Nonzero if something output at line. */
128 bool first_time; /* cb_file_change hasn't been called yet. */
129 } print;
131 /* General output routines. */
132 static void scan_translation_unit (cpp_reader *);
133 static void scan_translation_unit_trad (cpp_reader *);
135 /* Callback routines for the parser. Most of these are active only
136 in specific modes. */
137 static void cb_file_change (cpp_reader *, const struct line_map *);
138 static void cb_line_change (cpp_reader *, const cpp_token *, int);
139 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
140 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
141 static void cb_def_pragma (cpp_reader *, source_location);
142 static void cb_include (cpp_reader *, source_location, const unsigned char *,
143 const char *, int, const cpp_token **);
144 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
145 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
146 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
147 static bool cb_cpp_error (cpp_reader *, int, int, location_t, unsigned int,
148 const char *, va_list *)
149 ATTRIBUTE_GCC_DIAG(6,0);
150 void pp_dir_change (cpp_reader *, const char *);
152 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
153 static void dump_queued_macros (cpp_reader *);
156 static void
157 cpp_define_builtins (cpp_reader *pfile)
159 int major, minor, patchlevel;
161 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
162 in C, defines __STDC_HOSTED__?! */
163 cpp_init_builtins (pfile, 0);
165 /* Initialize GFORTRAN specific builtins.
166 These are documented. */
167 if (sscanf (BASEVER, "%d.%d.%d", &major, &minor, &patchlevel) != 3)
169 sscanf (BASEVER, "%d.%d", &major, &minor);
170 patchlevel = 0;
172 cpp_define_formatted (pfile, "__GNUC__=%d", major);
173 cpp_define_formatted (pfile, "__GNUC_MINOR__=%d", minor);
174 cpp_define_formatted (pfile, "__GNUC_PATCHLEVEL__=%d", patchlevel);
176 cpp_define (pfile, "__GFORTRAN__=1");
177 cpp_define (pfile, "_LANGUAGE_FORTRAN=1");
179 if (gfc_option.flag_openmp)
180 cpp_define (pfile, "_OPENMP=200805");
183 /* More builtins that might be useful, but are not documented
184 (in no particular order). */
185 cpp_define_formatted (pfile, "__VERSION__=\"%s\"", version_string);
187 if (flag_pic)
189 cpp_define_formatted (pfile, "__pic__=%d", flag_pic);
190 cpp_define_formatted (pfile, "__PIC__=%d", flag_pic);
192 if (flag_pie)
194 cpp_define_formatted (pfile, "__pie__=%d", flag_pie);
195 cpp_define_formatted (pfile, "__PIE__=%d", flag_pie);
198 if (optimize_size)
199 cpp_define (pfile, "__OPTIMIZE_SIZE__");
200 if (optimize)
201 cpp_define (pfile, "__OPTIMIZE__");
203 if (fast_math_flags_set_p ())
204 cpp_define (pfile, "__FAST_MATH__");
205 if (flag_signaling_nans)
206 cpp_define (pfile, "__SUPPORT_SNAN__");
208 cpp_define_formatted (pfile, "__FINITE_MATH_ONLY__=%d", flag_finite_math_only);
210 /* Definitions for LP64 model. */
211 if (TYPE_PRECISION (long_integer_type_node) == 64
212 && POINTER_SIZE == 64
213 && TYPE_PRECISION (integer_type_node) == 32)
215 cpp_define (pfile, "_LP64");
216 cpp_define (pfile, "__LP64__");
219 /* Define NAME with value TYPE size_unit.
220 The C-side also defines __SIZEOF_WCHAR_T__, __SIZEOF_WINT_T__
221 __SIZEOF_PTRDIFF_T__, however, fortran seems to lack the
222 appropriate type nodes. */
224 #define define_type_sizeof(NAME, TYPE) \
225 cpp_define_formatted (pfile, NAME"="HOST_WIDE_INT_PRINT_DEC, \
226 tree_low_cst (TYPE_SIZE_UNIT (TYPE), 1))
228 define_type_sizeof ("__SIZEOF_INT__", integer_type_node);
229 define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node);
230 define_type_sizeof ("__SIZEOF_LONG_LONG__", long_long_integer_type_node);
231 define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node);
232 define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node);
233 define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node);
234 define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node);
235 define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node);
237 #undef define_type_sizeof
239 /* The defines below are necessary for the TARGET_* macros.
241 FIXME: Note that builtin_define_std() actually is a function
242 in c-cppbuiltin.c which uses flags undefined for Fortran.
243 Let's skip this for now. If needed, one needs to look into it
244 once more. */
246 # define builtin_define(TXT) cpp_define (pfile, TXT)
247 # define builtin_define_std(TXT)
248 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
250 /* FIXME: Pandora's Box
251 Using the macros below results in multiple breakages:
252 - mingw will fail to compile this file as dependent macros
253 assume to be used in c-cppbuiltin.c only. Further, they use
254 flags only valid/defined in C (same as noted above).
255 [config/i386/mingw32.h, config/i386/cygming.h]
256 - other platforms (not as popular) break similarly
257 [grep for 'builtin_define_with_int_value' in gcc/config/]
259 TARGET_CPU_CPP_BUILTINS ();
260 TARGET_OS_CPP_BUILTINS ();
261 TARGET_OBJFMT_CPP_BUILTINS (); */
263 #undef builtin_define
264 #undef builtin_define_std
265 #undef builtin_assert
268 bool
269 gfc_cpp_enabled (void)
271 return gfc_cpp_option.temporary_filename != NULL;
274 bool
275 gfc_cpp_preprocess_only (void)
277 return gfc_cpp_option.preprocess_only;
280 bool
281 gfc_cpp_makedep (void)
283 return gfc_cpp_option.deps;
286 void
287 gfc_cpp_add_dep (const char *name, bool system)
289 if (!gfc_cpp_option.deps_skip_system || !system)
290 deps_add_dep (cpp_get_deps (cpp_in), name);
293 void
294 gfc_cpp_add_target (const char *name)
296 deps_add_target (cpp_get_deps (cpp_in), name, 0);
300 const char *
301 gfc_cpp_temporary_file (void)
303 return gfc_cpp_option.temporary_filename;
306 void
307 gfc_cpp_init_options (unsigned int decoded_options_count,
308 struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
310 /* Do not create any objects from libcpp here. If no
311 preprocessing is requested, this would be wasted
312 time and effort.
314 See gfc_cpp_post_options() instead. */
316 gfc_cpp_option.temporary_filename = NULL;
317 gfc_cpp_option.output_filename = NULL;
318 gfc_cpp_option.preprocess_only = 0;
319 gfc_cpp_option.discard_comments = 1;
320 gfc_cpp_option.discard_comments_in_macro_exp = 1;
321 gfc_cpp_option.print_include_names = 0;
322 gfc_cpp_option.no_line_commands = 0;
323 gfc_cpp_option.dump_macros = '\0';
324 gfc_cpp_option.dump_includes = 0;
325 gfc_cpp_option.working_directory = -1;
326 gfc_cpp_option.no_predefined = 0;
327 gfc_cpp_option.standard_include_paths = 1;
328 gfc_cpp_option.verbose = 0;
329 gfc_cpp_option.deps = 0;
330 gfc_cpp_option.deps_skip_system = 0;
331 gfc_cpp_option.deps_phony = 0;
332 gfc_cpp_option.deps_missing_are_generated = 0;
333 gfc_cpp_option.deps_filename = NULL;
334 gfc_cpp_option.deps_filename_user = NULL;
336 gfc_cpp_option.multilib = NULL;
337 gfc_cpp_option.prefix = NULL;
338 gfc_cpp_option.sysroot = NULL;
340 gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t,
341 decoded_options_count);
342 gfc_cpp_option.deferred_opt_count = 0;
346 gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
348 int result = 1;
349 enum opt_code code = (enum opt_code) scode;
351 switch (code)
353 default:
354 result = 0;
355 break;
357 case OPT_cpp:
358 gfc_cpp_option.temporary_filename = arg;
359 break;
361 case OPT_nocpp:
362 gfc_cpp_option.temporary_filename = 0L;
363 break;
365 case OPT_d:
366 for ( ; *arg; ++arg)
367 switch (*arg)
369 case 'D':
370 case 'M':
371 case 'N':
372 case 'U':
373 gfc_cpp_option.dump_macros = *arg;
374 break;
376 case 'I':
377 gfc_cpp_option.dump_includes = 1;
378 break;
380 break;
382 case OPT_fworking_directory:
383 gfc_cpp_option.working_directory = value;
384 break;
386 case OPT_idirafter:
387 gfc_cpp_add_include_path_after (xstrdup(arg), true);
388 break;
390 case OPT_imultilib:
391 gfc_cpp_option.multilib = arg;
392 break;
394 case OPT_iprefix:
395 gfc_cpp_option.prefix = arg;
396 break;
398 case OPT_isysroot:
399 gfc_cpp_option.sysroot = arg;
400 break;
402 case OPT_iquote:
403 case OPT_isystem:
404 gfc_cpp_add_include_path (xstrdup(arg), true);
405 break;
407 case OPT_nostdinc:
408 gfc_cpp_option.standard_include_paths = value;
409 break;
411 case OPT_o:
412 if (!gfc_cpp_option.output_filename)
413 gfc_cpp_option.output_filename = arg;
414 else
415 gfc_fatal_error ("output filename specified twice");
416 break;
418 case OPT_undef:
419 gfc_cpp_option.no_predefined = value;
420 break;
422 case OPT_v:
423 gfc_cpp_option.verbose = value;
424 break;
426 case OPT_A:
427 case OPT_D:
428 case OPT_U:
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_C:
435 gfc_cpp_option.discard_comments = 0;
436 break;
438 case OPT_CC:
439 gfc_cpp_option.discard_comments = 0;
440 gfc_cpp_option.discard_comments_in_macro_exp = 0;
441 break;
443 case OPT_E:
444 gfc_cpp_option.preprocess_only = 1;
445 break;
447 case OPT_H:
448 gfc_cpp_option.print_include_names = 1;
449 break;
451 case OPT_MM:
452 gfc_cpp_option.deps_skip_system = 1;
453 /* fall through */
455 case OPT_M:
456 gfc_cpp_option.deps = 1;
457 break;
459 case OPT_MMD:
460 gfc_cpp_option.deps_skip_system = 1;
461 /* fall through */
463 case OPT_MD:
464 gfc_cpp_option.deps = 1;
465 gfc_cpp_option.deps_filename = arg;
466 break;
468 case OPT_MF:
469 /* If specified multiple times, last one wins. */
470 gfc_cpp_option.deps_filename_user = arg;
471 break;
473 case OPT_MG:
474 gfc_cpp_option.deps_missing_are_generated = 1;
475 break;
477 case OPT_MP:
478 gfc_cpp_option.deps_phony = 1;
479 break;
481 case OPT_MQ:
482 case OPT_MT:
483 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
484 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
485 gfc_cpp_option.deferred_opt_count++;
486 break;
488 case OPT_P:
489 gfc_cpp_option.no_line_commands = 1;
490 break;
493 return result;
497 void
498 gfc_cpp_post_options (void)
500 /* Any preprocessing-related option without '-cpp' is considered
501 an error. */
502 if (!gfc_cpp_enabled ()
503 && (gfc_cpp_preprocess_only ()
504 || gfc_cpp_makedep ()
505 || !gfc_cpp_option.discard_comments
506 || !gfc_cpp_option.discard_comments_in_macro_exp
507 || gfc_cpp_option.print_include_names
508 || gfc_cpp_option.no_line_commands
509 || gfc_cpp_option.dump_macros
510 || gfc_cpp_option.dump_includes))
511 gfc_fatal_error("To enable preprocessing, use -cpp");
513 cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
514 if (!gfc_cpp_enabled ())
515 return;
517 gcc_assert (cpp_in);
519 /* The cpp_options-structure defines far more flags than those set here.
520 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
521 inter-option dependencies that may need to be enforced. */
522 cpp_option = cpp_get_options (cpp_in);
523 gcc_assert (cpp_option);
525 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
526 cpp_option->traditional = 1;
527 cpp_option->cplusplus_comments = 0;
529 cpp_option->pedantic = pedantic;
531 cpp_option->dollars_in_ident = gfc_option.flag_dollar_ok;
532 cpp_option->discard_comments = gfc_cpp_option.discard_comments;
533 cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
534 cpp_option->print_include_names = gfc_cpp_option.print_include_names;
535 cpp_option->preprocessed = gfc_option.flag_preprocessed;
537 if (gfc_cpp_makedep ())
539 cpp_option->deps.style = DEPS_USER;
540 cpp_option->deps.phony_targets = gfc_cpp_option.deps_phony;
541 cpp_option->deps.missing_files = gfc_cpp_option.deps_missing_are_generated;
543 /* -MF <arg> overrides -M[M]D. */
544 if (gfc_cpp_option.deps_filename_user)
545 gfc_cpp_option.deps_filename = gfc_cpp_option.deps_filename_user;
548 if (gfc_cpp_option.working_directory == -1)
549 gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
551 cpp_post_options (cpp_in);
553 gfc_cpp_register_include_paths ();
557 void
558 gfc_cpp_init_0 (void)
560 struct cpp_callbacks *cb;
562 cb = cpp_get_callbacks (cpp_in);
563 cb->file_change = cb_file_change;
564 cb->line_change = cb_line_change;
565 cb->ident = cb_ident;
566 cb->def_pragma = cb_def_pragma;
567 cb->error = cb_cpp_error;
569 if (gfc_cpp_option.dump_includes)
570 cb->include = cb_include;
572 if ((gfc_cpp_option.dump_macros == 'D')
573 || (gfc_cpp_option.dump_macros == 'N'))
575 cb->define = cb_define;
576 cb->undef = cb_undef;
579 if (gfc_cpp_option.dump_macros == 'U')
581 cb->before_define = dump_queued_macros;
582 cb->used_define = cb_used_define;
583 cb->used_undef = cb_used_undef;
586 /* Initialize the print structure. Setting print.src_line to -1 here is
587 a trick to guarantee that the first token of the file will cause
588 a linemarker to be output by maybe_print_line. */
589 print.src_line = -1;
590 print.printed = 0;
591 print.prev = 0;
592 print.first_time = 1;
594 if (gfc_cpp_preprocess_only ())
596 if (gfc_cpp_option.output_filename)
598 /* This needs cheating: with "-E -o <file>", the user wants the
599 preprocessed output in <file>. However, if nothing is done
600 about it <file> is also used for assembler output. Hence, it
601 is necessary to redirect assembler output (actually nothing
602 as -E implies -fsyntax-only) to another file, otherwise the
603 output from preprocessing is lost. */
604 asm_file_name = gfc_cpp_option.temporary_filename;
606 print.outf = fopen (gfc_cpp_option.output_filename, "w");
607 if (print.outf == NULL)
608 gfc_fatal_error ("opening output file %s: %s",
609 gfc_cpp_option.output_filename,
610 xstrerror (errno));
612 else
613 print.outf = stdout;
615 else
617 print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
618 if (print.outf == NULL)
619 gfc_fatal_error ("opening output file %s: %s",
620 gfc_cpp_option.temporary_filename, xstrerror (errno));
623 gcc_assert(cpp_in);
624 if (!cpp_read_main_file (cpp_in, gfc_source_file))
625 errorcount++;
628 void
629 gfc_cpp_init (void)
631 int i;
633 if (gfc_option.flag_preprocessed)
634 return;
636 cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
637 if (!gfc_cpp_option.no_predefined)
638 cpp_define_builtins (cpp_in);
640 /* Handle deferred options from command-line. */
641 cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
643 for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
645 gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
647 if (opt->code == OPT_D)
648 cpp_define (cpp_in, opt->arg);
649 else if (opt->code == OPT_U)
650 cpp_undef (cpp_in, opt->arg);
651 else if (opt->code == OPT_A)
653 if (opt->arg[0] == '-')
654 cpp_unassert (cpp_in, opt->arg + 1);
655 else
656 cpp_assert (cpp_in, opt->arg);
658 else if (opt->code == OPT_MT || opt->code == OPT_MQ)
659 deps_add_target (cpp_get_deps (cpp_in),
660 opt->arg, opt->code == OPT_MQ);
663 if (gfc_cpp_option.working_directory
664 && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
665 pp_dir_change (cpp_in, get_src_pwd ());
668 gfc_try
669 gfc_cpp_preprocess (const char *source_file)
671 if (!gfc_cpp_enabled ())
672 return FAILURE;
674 cpp_change_file (cpp_in, LC_RENAME, source_file);
676 if (cpp_option->traditional)
677 scan_translation_unit_trad (cpp_in);
678 else
679 scan_translation_unit (cpp_in);
681 /* -dM command line option. */
682 if (gfc_cpp_preprocess_only () &&
683 gfc_cpp_option.dump_macros == 'M')
685 putc ('\n', print.outf);
686 cpp_forall_identifiers (cpp_in, dump_macro, NULL);
689 putc ('\n', print.outf);
691 if (!gfc_cpp_preprocess_only ()
692 || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
693 fclose (print.outf);
695 return SUCCESS;
698 void
699 gfc_cpp_done (void)
701 if (!gfc_cpp_enabled ())
702 return;
704 gcc_assert (cpp_in);
706 if (gfc_cpp_makedep ())
708 if (gfc_cpp_option.deps_filename)
710 FILE *f = fopen (gfc_cpp_option.deps_filename, "w");
711 if (f)
713 cpp_finish (cpp_in, f);
714 fclose (f);
716 else
717 gfc_fatal_error ("opening output file %s: %s",
718 gfc_cpp_option.deps_filename,
719 xstrerror (errno));
721 else
722 cpp_finish (cpp_in, stdout);
725 cpp_undef_all (cpp_in);
726 cpp_clear_file_cache (cpp_in);
729 /* PATH must be malloc-ed and NULL-terminated. */
730 void
731 gfc_cpp_add_include_path (char *path, bool user_supplied)
733 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
734 include path. Fortran does not define any system include paths. */
735 int cxx_aware = 0;
737 add_path (path, BRACKET, cxx_aware, user_supplied);
740 void
741 gfc_cpp_add_include_path_after (char *path, bool user_supplied)
743 int cxx_aware = 0;
744 add_path (path, AFTER, cxx_aware, user_supplied);
747 void
748 gfc_cpp_register_include_paths (void)
750 int cxx_stdinc = 0;
751 register_include_chains (cpp_in, gfc_cpp_option.sysroot,
752 gfc_cpp_option.prefix, gfc_cpp_option.multilib,
753 gfc_cpp_option.standard_include_paths, cxx_stdinc,
754 gfc_cpp_option.verbose);
759 static void scan_translation_unit_trad (cpp_reader *);
760 static void account_for_newlines (const unsigned char *, size_t);
761 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
763 static void print_line (source_location, const char *);
764 static void maybe_print_line (source_location);
767 /* Writes out the preprocessed file, handling spacing and paste
768 avoidance issues. */
769 static void
770 scan_translation_unit (cpp_reader *pfile)
772 bool avoid_paste = false;
774 print.source = NULL;
775 for (;;)
777 const cpp_token *token = cpp_get_token (pfile);
779 if (token->type == CPP_PADDING)
781 avoid_paste = true;
782 if (print.source == NULL
783 || (!(print.source->flags & PREV_WHITE)
784 && token->val.source == NULL))
785 print.source = token->val.source;
786 continue;
789 if (token->type == CPP_EOF)
790 break;
792 /* Subtle logic to output a space if and only if necessary. */
793 if (avoid_paste)
795 if (print.source == NULL)
796 print.source = token;
797 if (print.source->flags & PREV_WHITE
798 || (print.prev
799 && cpp_avoid_paste (pfile, print.prev, token))
800 || (print.prev == NULL && token->type == CPP_HASH))
801 putc (' ', print.outf);
803 else if (token->flags & PREV_WHITE)
804 putc (' ', print.outf);
806 avoid_paste = false;
807 print.source = NULL;
808 print.prev = token;
809 cpp_output_token (token, print.outf);
811 if (token->type == CPP_COMMENT)
812 account_for_newlines (token->val.str.text, token->val.str.len);
816 /* Adjust print.src_line for newlines embedded in output. */
817 static void
818 account_for_newlines (const unsigned char *str, size_t len)
820 while (len--)
821 if (*str++ == '\n')
822 print.src_line++;
825 /* Writes out a traditionally preprocessed file. */
826 static void
827 scan_translation_unit_trad (cpp_reader *pfile)
829 while (_cpp_read_logical_line_trad (pfile))
831 size_t len = pfile->out.cur - pfile->out.base;
832 maybe_print_line (pfile->out.first_line);
833 fwrite (pfile->out.base, 1, len, print.outf);
834 print.printed = 1;
835 if (!CPP_OPTION (pfile, discard_comments))
836 account_for_newlines (pfile->out.base, len);
840 /* If the token read on logical line LINE needs to be output on a
841 different line to the current one, output the required newlines or
842 a line marker. */
843 static void
844 maybe_print_line (source_location src_loc)
846 const struct line_map *map = linemap_lookup (line_table, src_loc);
847 int src_line = SOURCE_LINE (map, src_loc);
849 /* End the previous line of text. */
850 if (print.printed)
852 putc ('\n', print.outf);
853 print.src_line++;
854 print.printed = 0;
857 if (src_line >= print.src_line && src_line < print.src_line + 8)
859 while (src_line > print.src_line)
861 putc ('\n', print.outf);
862 print.src_line++;
865 else
866 print_line (src_loc, "");
869 /* Output a line marker for logical line LINE. Special flags are "1"
870 or "2" indicating entering or leaving a file. */
871 static void
872 print_line (source_location src_loc, const char *special_flags)
874 /* End any previous line of text. */
875 if (print.printed)
876 putc ('\n', print.outf);
877 print.printed = 0;
879 if (!gfc_cpp_option.no_line_commands)
881 const struct line_map *map = linemap_lookup (line_table, src_loc);
883 size_t to_file_len = strlen (map->to_file);
884 unsigned char *to_file_quoted =
885 (unsigned char *) alloca (to_file_len * 4 + 1);
886 unsigned char *p;
888 print.src_line = SOURCE_LINE (map, src_loc);
890 /* cpp_quote_string does not nul-terminate, so we have to do it
891 ourselves. */
892 p = cpp_quote_string (to_file_quoted,
893 (const unsigned char *) map->to_file, to_file_len);
894 *p = '\0';
895 fprintf (print.outf, "# %u \"%s\"%s",
896 print.src_line == 0 ? 1 : print.src_line,
897 to_file_quoted, special_flags);
899 if (map->sysp == 2)
900 fputs (" 3 4", print.outf);
901 else if (map->sysp == 1)
902 fputs (" 3", print.outf);
904 putc ('\n', print.outf);
908 static void
909 cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map)
911 const char *flags = "";
913 if (gfc_cpp_option.no_line_commands)
914 return;
916 if (!map)
917 return;
919 if (print.first_time)
921 /* Avoid printing foo.i when the main file is foo.c. */
922 if (!cpp_get_options (cpp_in)->preprocessed)
923 print_line (map->start_location, flags);
924 print.first_time = 0;
926 else
928 /* Bring current file to correct line when entering a new file. */
929 if (map->reason == LC_ENTER)
931 const struct line_map *from = INCLUDED_FROM (line_table, map);
932 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
934 if (map->reason == LC_ENTER)
935 flags = " 1";
936 else if (map->reason == LC_LEAVE)
937 flags = " 2";
938 print_line (map->start_location, flags);
943 /* Called when a line of output is started. TOKEN is the first token
944 of the line, and at end of file will be CPP_EOF. */
945 static void
946 cb_line_change (cpp_reader *pfile, const cpp_token *token,
947 int parsing_args)
949 source_location src_loc = token->src_loc;
951 if (token->type == CPP_EOF || parsing_args)
952 return;
954 maybe_print_line (src_loc);
955 print.prev = 0;
956 print.source = 0;
958 /* Supply enough spaces to put this token in its original column,
959 one space per column greater than 2, since scan_translation_unit
960 will provide a space if PREV_WHITE. Don't bother trying to
961 reconstruct tabs; we can't get it right in general, and nothing
962 ought to care. Some things do care; the fault lies with them. */
963 if (!CPP_OPTION (pfile, traditional))
965 const struct line_map *map = linemap_lookup (line_table, src_loc);
966 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
967 print.printed = 1;
969 while (-- spaces >= 0)
970 putc (' ', print.outf);
974 static void
975 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
976 const cpp_string *str)
978 maybe_print_line (line);
979 fprintf (print.outf, "#ident %s\n", str->text);
980 print.src_line++;
983 static void
984 cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
985 cpp_hashnode *node ATTRIBUTE_UNUSED)
987 maybe_print_line (line);
988 fputs ("#define ", print.outf);
990 /* 'D' is whole definition; 'N' is name only. */
991 if (gfc_cpp_option.dump_macros == 'D')
992 fputs ((const char *) cpp_macro_definition (pfile, node),
993 print.outf);
994 else
995 fputs ((const char *) NODE_NAME (node), print.outf);
997 putc ('\n', print.outf);
998 if (linemap_lookup (line_table, line)->to_line != 0)
999 print.src_line++;
1002 static void
1003 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
1004 cpp_hashnode *node)
1006 maybe_print_line (line);
1007 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
1008 print.src_line++;
1011 static void
1012 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
1013 const unsigned char *dir, const char *header, int angle_brackets,
1014 const cpp_token **comments)
1016 maybe_print_line (line);
1017 if (angle_brackets)
1018 fprintf (print.outf, "#%s <%s>", dir, header);
1019 else
1020 fprintf (print.outf, "#%s \"%s\"", dir, header);
1022 if (comments != NULL)
1024 while (*comments != NULL)
1026 if ((*comments)->flags & PREV_WHITE)
1027 putc (' ', print.outf);
1028 cpp_output_token (*comments, print.outf);
1029 ++comments;
1033 putc ('\n', print.outf);
1034 print.src_line++;
1037 /* Dump out the hash table. */
1038 static int
1039 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
1041 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1043 fputs ("#define ", print.outf);
1044 fputs ((const char *) cpp_macro_definition (pfile, node),
1045 print.outf);
1046 putc ('\n', print.outf);
1047 print.src_line++;
1050 return 1;
1053 static void
1054 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
1055 cpp_hashnode *node)
1057 gfc_cpp_macro_queue *q;
1058 q = XNEW (gfc_cpp_macro_queue);
1059 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
1060 q->next = cpp_define_queue;
1061 cpp_define_queue = q;
1064 /* Callback from cpp_error for PFILE to print diagnostics from the
1065 preprocessor. The diagnostic is of type LEVEL, with REASON set
1066 to the reason code if LEVEL is represents a warning, at location
1067 LOCATION, with column number possibly overridden by COLUMN_OVERRIDE
1068 if not zero; MSG is the translated message and AP the arguments.
1069 Returns true if a diagnostic was emitted, false otherwise. */
1071 static bool
1072 cb_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
1073 location_t location, unsigned int column_override,
1074 const char *msg, va_list *ap)
1076 diagnostic_info diagnostic;
1077 diagnostic_t dlevel;
1078 bool save_warn_system_headers = global_dc->warn_system_headers;
1079 bool ret;
1081 switch (level)
1083 case CPP_DL_WARNING_SYSHDR:
1084 global_dc->warn_system_headers = 1;
1085 /* Fall through. */
1086 case CPP_DL_WARNING:
1087 dlevel = DK_WARNING;
1088 break;
1089 case CPP_DL_PEDWARN:
1090 dlevel = DK_PEDWARN;
1091 break;
1092 case CPP_DL_ERROR:
1093 dlevel = DK_ERROR;
1094 break;
1095 case CPP_DL_ICE:
1096 dlevel = DK_ICE;
1097 break;
1098 case CPP_DL_NOTE:
1099 dlevel = DK_NOTE;
1100 break;
1101 case CPP_DL_FATAL:
1102 dlevel = DK_FATAL;
1103 break;
1104 default:
1105 gcc_unreachable ();
1107 diagnostic_set_info_translated (&diagnostic, msg, ap,
1108 location, dlevel);
1109 if (column_override)
1110 diagnostic_override_column (&diagnostic, column_override);
1111 if (reason == CPP_W_WARNING_DIRECTIVE)
1112 diagnostic_override_option_index (&diagnostic, OPT_Wcpp);
1113 ret = report_diagnostic (&diagnostic);
1114 if (level == CPP_DL_WARNING_SYSHDR)
1115 global_dc->warn_system_headers = save_warn_system_headers;
1116 return ret;
1119 /* Callback called when -fworking-director and -E to emit working
1120 directory in cpp output file. */
1122 void
1123 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1125 size_t to_file_len = strlen (dir);
1126 unsigned char *to_file_quoted =
1127 (unsigned char *) alloca (to_file_len * 4 + 1);
1128 unsigned char *p;
1130 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
1131 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
1132 *p = '\0';
1133 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
1136 /* Copy a #pragma directive to the preprocessed output. */
1137 static void
1138 cb_def_pragma (cpp_reader *pfile, source_location line)
1140 maybe_print_line (line);
1141 fputs ("#pragma ", print.outf);
1142 cpp_output_line (pfile, print.outf);
1143 print.src_line++;
1146 static void
1147 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
1148 source_location line ATTRIBUTE_UNUSED,
1149 cpp_hashnode *node)
1151 gfc_cpp_macro_queue *q;
1152 q = XNEW (gfc_cpp_macro_queue);
1153 q->macro = xstrdup ((const char *) NODE_NAME (node));
1154 q->next = cpp_undefine_queue;
1155 cpp_undefine_queue = q;
1158 static void
1159 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
1161 gfc_cpp_macro_queue *q;
1163 /* End the previous line of text. */
1164 if (print.printed)
1166 putc ('\n', print.outf);
1167 print.src_line++;
1168 print.printed = 0;
1171 for (q = cpp_define_queue; q;)
1173 gfc_cpp_macro_queue *oq;
1174 fputs ("#define ", print.outf);
1175 fputs (q->macro, print.outf);
1176 putc ('\n', print.outf);
1177 print.src_line++;
1178 oq = q;
1179 q = q->next;
1180 gfc_free (oq->macro);
1181 gfc_free (oq);
1183 cpp_define_queue = NULL;
1184 for (q = cpp_undefine_queue; q;)
1186 gfc_cpp_macro_queue *oq;
1187 fprintf (print.outf, "#undef %s\n", q->macro);
1188 print.src_line++;
1189 oq = q;
1190 q = q->next;
1191 gfc_free (oq->macro);
1192 gfc_free (oq);
1194 cpp_undefine_queue = NULL;