]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/cpp.c
Update copyright years.
[thirdparty/gcc.git] / gcc / fortran / cpp.c
1 /* Copyright (C) 2008-2019 Free Software Foundation, Inc.
2
3 This file is part of GCC.
4
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.
9
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.
14
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/>. */
18
19 #include "config.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "target.h"
23 #include "gfortran.h"
24 #include "diagnostic.h"
25
26
27 #include "toplev.h"
28
29 #include "../../libcpp/internal.h"
30 #include "cpp.h"
31 #include "incpath.h"
32 #include "cppbuiltin.h"
33 #include "mkdeps.h"
34
35 #ifndef TARGET_SYSTEM_ROOT
36 # define TARGET_SYSTEM_ROOT NULL
37 #endif
38
39 #ifndef TARGET_CPU_CPP_BUILTINS
40 # define TARGET_CPU_CPP_BUILTINS()
41 #endif
42
43 #ifndef TARGET_OS_CPP_BUILTINS
44 # define TARGET_OS_CPP_BUILTINS()
45 #endif
46
47 #ifndef TARGET_OBJFMT_CPP_BUILTINS
48 # define TARGET_OBJFMT_CPP_BUILTINS()
49 #endif
50
51
52 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
53 handling is deferred to gfc_cpp_init (). */
54 typedef struct
55 {
56 enum opt_code code;
57 const char *arg;
58 }
59 gfc_cpp_deferred_opt_t;
60
61
62 /* Defined and undefined macros being queued for output with -dU at
63 the next newline. */
64 typedef struct gfc_cpp_macro_queue
65 {
66 struct gfc_cpp_macro_queue *next; /* Next macro in the list. */
67 char *macro; /* The name of the macro if not
68 defined, the full definition if
69 defined. */
70 } gfc_cpp_macro_queue;
71 static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
72
73 struct gfc_cpp_option_data
74 {
75 /* Argument of -cpp, implied by SPEC;
76 if NULL, preprocessing disabled. */
77 const char *temporary_filename;
78
79 const char *output_filename; /* -o <arg> */
80 int preprocess_only; /* -E */
81 int discard_comments; /* -C */
82 int discard_comments_in_macro_exp; /* -CC */
83 int print_include_names; /* -H */
84 int no_line_commands; /* -P */
85 char dump_macros; /* -d[DMNU] */
86 int dump_includes; /* -dI */
87 int working_directory; /* -fworking-directory */
88 int no_predefined; /* -undef */
89 int standard_include_paths; /* -nostdinc */
90 int verbose; /* -v */
91 int deps; /* -M */
92 int deps_skip_system; /* -MM */
93 const char *deps_filename; /* -M[M]D */
94 const char *deps_filename_user; /* -MF <arg> */
95 int deps_missing_are_generated; /* -MG */
96 int deps_phony; /* -MP */
97 int warn_date_time; /* -Wdate-time */
98
99 const char *multilib; /* -imultilib <dir> */
100 const char *prefix; /* -iprefix <dir> */
101 const char *sysroot; /* -isysroot <dir> */
102
103 /* Options whose handling needs to be deferred until the
104 appropriate cpp-objects are created:
105 -A predicate=answer
106 -D <macro>[=<val>]
107 -U <macro> */
108 gfc_cpp_deferred_opt_t *deferred_opt;
109 int deferred_opt_count;
110 }
111 gfc_cpp_option;
112
113 /* Structures used with libcpp: */
114 static cpp_options *cpp_option = NULL;
115 static cpp_reader *cpp_in = NULL;
116
117 /* Encapsulates state used to convert a stream of cpp-tokens into
118 a text file. */
119 static struct
120 {
121 FILE *outf; /* Stream to write to. */
122 const cpp_token *prev; /* Previous token. */
123 const cpp_token *source; /* Source token for spacing. */
124 int src_line; /* Line number currently being written. */
125 unsigned char printed; /* Nonzero if something output at line. */
126 bool first_time; /* cb_file_change hasn't been called yet. */
127 } print;
128
129 /* General output routines. */
130 static void scan_translation_unit (cpp_reader *);
131 static void scan_translation_unit_trad (cpp_reader *);
132
133 /* Callback routines for the parser. Most of these are active only
134 in specific modes. */
135 static void cb_file_change (cpp_reader *, const line_map_ordinary *);
136 static void cb_line_change (cpp_reader *, const cpp_token *, int);
137 static void cb_define (cpp_reader *, location_t, cpp_hashnode *);
138 static void cb_undef (cpp_reader *, location_t, cpp_hashnode *);
139 static void cb_def_pragma (cpp_reader *, location_t);
140 static void cb_include (cpp_reader *, location_t, const unsigned char *,
141 const char *, int, const cpp_token **);
142 static void cb_ident (cpp_reader *, location_t, const cpp_string *);
143 static void cb_used_define (cpp_reader *, location_t, cpp_hashnode *);
144 static void cb_used_undef (cpp_reader *, location_t, cpp_hashnode *);
145 static bool cb_cpp_diagnostic (cpp_reader *, enum cpp_diagnostic_level,
146 enum cpp_warning_reason, rich_location *,
147 const char *, va_list *)
148 ATTRIBUTE_GCC_DIAG(5,0);
149 void pp_dir_change (cpp_reader *, const char *);
150
151 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
152 static void dump_queued_macros (cpp_reader *);
153
154
155 static void
156 cpp_define_builtins (cpp_reader *pfile)
157 {
158 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
159 in C, defines __STDC_HOSTED__?! */
160 cpp_init_builtins (pfile, 0);
161
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");
167
168 if (flag_openacc)
169 cpp_define (pfile, "_OPENACC=201306");
170
171 if (flag_openmp)
172 cpp_define (pfile, "_OPENMP=201511");
173
174 /* The defines below are necessary for the TARGET_* macros.
175
176 FIXME: Note that builtin_define_std() actually is a function
177 in c-cppbuiltin.c which uses flags undefined for Fortran.
178 Let's skip this for now. If needed, one needs to look into it
179 once more. */
180
181 # define builtin_define(TXT) cpp_define (pfile, TXT)
182 # define builtin_define_std(TXT)
183 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
184
185 /* FIXME: Pandora's Box
186 Using the macros below results in multiple breakages:
187 - mingw will fail to compile this file as dependent macros
188 assume to be used in c-cppbuiltin.c only. Further, they use
189 flags only valid/defined in C (same as noted above).
190 [config/i386/mingw32.h, config/i386/cygming.h]
191 - other platforms (not as popular) break similarly
192 [grep for 'builtin_define_with_int_value' in gcc/config/]
193
194 TARGET_CPU_CPP_BUILTINS ();
195 TARGET_OS_CPP_BUILTINS ();
196 TARGET_OBJFMT_CPP_BUILTINS (); */
197
198 #undef builtin_define
199 #undef builtin_define_std
200 #undef builtin_assert
201 }
202
203 bool
204 gfc_cpp_enabled (void)
205 {
206 return gfc_cpp_option.temporary_filename != NULL;
207 }
208
209 bool
210 gfc_cpp_preprocess_only (void)
211 {
212 return gfc_cpp_option.preprocess_only;
213 }
214
215 bool
216 gfc_cpp_makedep (void)
217 {
218 return gfc_cpp_option.deps;
219 }
220
221 void
222 gfc_cpp_add_dep (const char *name, bool system)
223 {
224 if (!gfc_cpp_option.deps_skip_system || !system)
225 deps_add_dep (cpp_get_deps (cpp_in), name);
226 }
227
228 void
229 gfc_cpp_add_target (const char *name)
230 {
231 deps_add_target (cpp_get_deps (cpp_in), name, 0);
232 }
233
234
235 const char *
236 gfc_cpp_temporary_file (void)
237 {
238 return gfc_cpp_option.temporary_filename;
239 }
240
241 void
242 gfc_cpp_init_options (unsigned int decoded_options_count,
243 struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
244 {
245 /* Do not create any objects from libcpp here. If no
246 preprocessing is requested, this would be wasted
247 time and effort.
248
249 See gfc_cpp_post_options() instead. */
250
251 gfc_cpp_option.temporary_filename = NULL;
252 gfc_cpp_option.output_filename = NULL;
253 gfc_cpp_option.preprocess_only = 0;
254 gfc_cpp_option.discard_comments = 1;
255 gfc_cpp_option.discard_comments_in_macro_exp = 1;
256 gfc_cpp_option.print_include_names = 0;
257 gfc_cpp_option.no_line_commands = 0;
258 gfc_cpp_option.dump_macros = '\0';
259 gfc_cpp_option.dump_includes = 0;
260 gfc_cpp_option.working_directory = -1;
261 gfc_cpp_option.no_predefined = 0;
262 gfc_cpp_option.standard_include_paths = 1;
263 gfc_cpp_option.verbose = 0;
264 gfc_cpp_option.warn_date_time = 0;
265 gfc_cpp_option.deps = 0;
266 gfc_cpp_option.deps_skip_system = 0;
267 gfc_cpp_option.deps_phony = 0;
268 gfc_cpp_option.deps_missing_are_generated = 0;
269 gfc_cpp_option.deps_filename = NULL;
270 gfc_cpp_option.deps_filename_user = NULL;
271
272 gfc_cpp_option.multilib = NULL;
273 gfc_cpp_option.prefix = NULL;
274 gfc_cpp_option.sysroot = TARGET_SYSTEM_ROOT;
275
276 gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t,
277 decoded_options_count);
278 gfc_cpp_option.deferred_opt_count = 0;
279 }
280
281 int
282 gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED)
283 {
284 int result = 1;
285 enum opt_code code = (enum opt_code) scode;
286
287 switch (code)
288 {
289 default:
290 result = 0;
291 break;
292
293 case OPT_cpp_:
294 gfc_cpp_option.temporary_filename = arg;
295 break;
296
297 case OPT_nocpp:
298 gfc_cpp_option.temporary_filename = 0L;
299 break;
300
301 case OPT_d:
302 for ( ; *arg; ++arg)
303 switch (*arg)
304 {
305 case 'D':
306 case 'M':
307 case 'N':
308 case 'U':
309 gfc_cpp_option.dump_macros = *arg;
310 break;
311
312 case 'I':
313 gfc_cpp_option.dump_includes = 1;
314 break;
315 }
316 break;
317
318 case OPT_fworking_directory:
319 gfc_cpp_option.working_directory = value;
320 break;
321
322 case OPT_idirafter:
323 gfc_cpp_add_include_path_after (xstrdup(arg), true);
324 break;
325
326 case OPT_imultilib:
327 gfc_cpp_option.multilib = arg;
328 break;
329
330 case OPT_iprefix:
331 gfc_cpp_option.prefix = arg;
332 break;
333
334 case OPT_isysroot:
335 gfc_cpp_option.sysroot = arg;
336 break;
337
338 case OPT_iquote:
339 case OPT_isystem:
340 gfc_cpp_add_include_path (xstrdup(arg), true);
341 break;
342
343 case OPT_nostdinc:
344 gfc_cpp_option.standard_include_paths = value;
345 break;
346
347 case OPT_o:
348 if (!gfc_cpp_option.output_filename)
349 gfc_cpp_option.output_filename = arg;
350 else
351 gfc_fatal_error ("output filename specified twice");
352 break;
353
354 case OPT_undef:
355 gfc_cpp_option.no_predefined = value;
356 break;
357
358 case OPT_v:
359 gfc_cpp_option.verbose = value;
360 break;
361
362 case OPT_Wdate_time:
363 gfc_cpp_option.warn_date_time = value;
364 break;
365
366 case OPT_A:
367 case OPT_D:
368 case OPT_U:
369 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
370 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
371 gfc_cpp_option.deferred_opt_count++;
372 break;
373
374 case OPT_C:
375 gfc_cpp_option.discard_comments = 0;
376 break;
377
378 case OPT_CC:
379 gfc_cpp_option.discard_comments = 0;
380 gfc_cpp_option.discard_comments_in_macro_exp = 0;
381 break;
382
383 case OPT_E:
384 gfc_cpp_option.preprocess_only = 1;
385 break;
386
387 case OPT_H:
388 gfc_cpp_option.print_include_names = 1;
389 break;
390
391 case OPT_MM:
392 gfc_cpp_option.deps_skip_system = 1;
393 /* fall through */
394
395 case OPT_M:
396 gfc_cpp_option.deps = 1;
397 break;
398
399 case OPT_MMD:
400 gfc_cpp_option.deps_skip_system = 1;
401 /* fall through */
402
403 case OPT_MD:
404 gfc_cpp_option.deps = 1;
405 gfc_cpp_option.deps_filename = arg;
406 break;
407
408 case OPT_MF:
409 /* If specified multiple times, last one wins. */
410 gfc_cpp_option.deps_filename_user = arg;
411 break;
412
413 case OPT_MG:
414 gfc_cpp_option.deps_missing_are_generated = 1;
415 break;
416
417 case OPT_MP:
418 gfc_cpp_option.deps_phony = 1;
419 break;
420
421 case OPT_MQ:
422 case OPT_MT:
423 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code;
424 gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg;
425 gfc_cpp_option.deferred_opt_count++;
426 break;
427
428 case OPT_P:
429 gfc_cpp_option.no_line_commands = 1;
430 break;
431 }
432
433 return result;
434 }
435
436
437 void
438 gfc_cpp_post_options (void)
439 {
440 /* Any preprocessing-related option without '-cpp' is considered
441 an error. */
442 if (!gfc_cpp_enabled ()
443 && (gfc_cpp_preprocess_only ()
444 || gfc_cpp_makedep ()
445 || !gfc_cpp_option.discard_comments
446 || !gfc_cpp_option.discard_comments_in_macro_exp
447 || gfc_cpp_option.print_include_names
448 || gfc_cpp_option.no_line_commands
449 || gfc_cpp_option.dump_macros
450 || gfc_cpp_option.dump_includes))
451 gfc_fatal_error ("To enable preprocessing, use %<-cpp%>");
452
453 if (!gfc_cpp_enabled ())
454 return;
455
456 cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
457 gcc_assert (cpp_in);
458
459 /* The cpp_options-structure defines far more flags than those set here.
460 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
461 inter-option dependencies that may need to be enforced. */
462 cpp_option = cpp_get_options (cpp_in);
463 gcc_assert (cpp_option);
464
465 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
466 cpp_option->traditional = 1;
467 cpp_option->cplusplus_comments = 0;
468
469 cpp_option->cpp_pedantic = pedantic;
470
471 cpp_option->dollars_in_ident = flag_dollar_ok;
472 cpp_option->discard_comments = gfc_cpp_option.discard_comments;
473 cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp;
474 cpp_option->print_include_names = gfc_cpp_option.print_include_names;
475 cpp_option->preprocessed = gfc_option.flag_preprocessed;
476 cpp_option->warn_date_time = gfc_cpp_option.warn_date_time;
477
478 if (gfc_cpp_makedep ())
479 {
480 cpp_option->deps.style = DEPS_USER;
481 cpp_option->deps.phony_targets = gfc_cpp_option.deps_phony;
482 cpp_option->deps.missing_files = gfc_cpp_option.deps_missing_are_generated;
483
484 /* -MF <arg> overrides -M[M]D. */
485 if (gfc_cpp_option.deps_filename_user)
486 gfc_cpp_option.deps_filename = gfc_cpp_option.deps_filename_user;
487 }
488
489 if (gfc_cpp_option.working_directory == -1)
490 gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE);
491
492 cpp_post_options (cpp_in);
493
494 gfc_cpp_register_include_paths ();
495 }
496
497
498 void
499 gfc_cpp_init_0 (void)
500 {
501 struct cpp_callbacks *cb;
502
503 cb = cpp_get_callbacks (cpp_in);
504 cb->file_change = cb_file_change;
505 cb->line_change = cb_line_change;
506 cb->ident = cb_ident;
507 cb->def_pragma = cb_def_pragma;
508 cb->diagnostic = cb_cpp_diagnostic;
509
510 if (gfc_cpp_option.dump_includes)
511 cb->include = cb_include;
512
513 if ((gfc_cpp_option.dump_macros == 'D')
514 || (gfc_cpp_option.dump_macros == 'N'))
515 {
516 cb->define = cb_define;
517 cb->undef = cb_undef;
518 }
519
520 if (gfc_cpp_option.dump_macros == 'U')
521 {
522 cb->before_define = dump_queued_macros;
523 cb->used_define = cb_used_define;
524 cb->used_undef = cb_used_undef;
525 }
526
527 /* Initialize the print structure. Setting print.src_line to -1 here is
528 a trick to guarantee that the first token of the file will cause
529 a linemarker to be output by maybe_print_line. */
530 print.src_line = -1;
531 print.printed = 0;
532 print.prev = 0;
533 print.first_time = 1;
534
535 if (gfc_cpp_preprocess_only ())
536 {
537 if (gfc_cpp_option.output_filename)
538 {
539 /* This needs cheating: with "-E -o <file>", the user wants the
540 preprocessed output in <file>. However, if nothing is done
541 about it <file> is also used for assembler output. Hence, it
542 is necessary to redirect assembler output (actually nothing
543 as -E implies -fsyntax-only) to another file, otherwise the
544 output from preprocessing is lost. */
545 asm_file_name = gfc_cpp_option.temporary_filename;
546
547 print.outf = fopen (gfc_cpp_option.output_filename, "w");
548 if (print.outf == NULL)
549 gfc_fatal_error ("opening output file %qs: %s",
550 gfc_cpp_option.output_filename,
551 xstrerror (errno));
552 }
553 else
554 print.outf = stdout;
555 }
556 else
557 {
558 print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
559 if (print.outf == NULL)
560 gfc_fatal_error ("opening output file %qs: %s",
561 gfc_cpp_option.temporary_filename, xstrerror (errno));
562 }
563
564 gcc_assert(cpp_in);
565 if (!cpp_read_main_file (cpp_in, gfc_source_file))
566 errorcount++;
567 }
568
569 void
570 gfc_cpp_init (void)
571 {
572 int i;
573
574 if (gfc_option.flag_preprocessed)
575 return;
576
577 cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
578 if (!gfc_cpp_option.no_predefined)
579 {
580 /* Make sure all of the builtins about to be declared have
581 BUILTINS_LOCATION has their location_t. */
582 cpp_force_token_locations (cpp_in, BUILTINS_LOCATION);
583
584 cpp_define_builtins (cpp_in);
585
586 cpp_stop_forcing_token_locations (cpp_in);
587 }
588
589 /* Handle deferred options from command-line. */
590 cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
591
592 for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
593 {
594 gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
595
596 if (opt->code == OPT_D)
597 cpp_define (cpp_in, opt->arg);
598 else if (opt->code == OPT_U)
599 cpp_undef (cpp_in, opt->arg);
600 else if (opt->code == OPT_A)
601 {
602 if (opt->arg[0] == '-')
603 cpp_unassert (cpp_in, opt->arg + 1);
604 else
605 cpp_assert (cpp_in, opt->arg);
606 }
607 else if (opt->code == OPT_MT || opt->code == OPT_MQ)
608 deps_add_target (cpp_get_deps (cpp_in),
609 opt->arg, opt->code == OPT_MQ);
610 }
611
612 /* Pre-defined macros for non-required INTEGER kind types. */
613 for (gfc_integer_info *itype = gfc_integer_kinds; itype->kind != 0; itype++)
614 {
615 if (itype->kind == 1)
616 cpp_define (cpp_in, "__GFC_INT_1__=1");
617 if (itype->kind == 2)
618 cpp_define (cpp_in, "__GFC_INT_2__=1");
619 if (itype->kind == 8)
620 cpp_define (cpp_in, "__GFC_INT_8__=1");
621 if (itype->kind == 16)
622 cpp_define (cpp_in, "__GFC_INT_16__=1");
623 }
624
625 /* Pre-defined macros for non-required REAL kind types. */
626 for (gfc_real_info *rtype = gfc_real_kinds; rtype->kind != 0; rtype++)
627 {
628 if (rtype->kind == 10)
629 cpp_define (cpp_in, "__GFC_REAL_10__=1");
630 if (rtype->kind == 16)
631 cpp_define (cpp_in, "__GFC_REAL_16__=1");
632 }
633
634 if (gfc_cpp_option.working_directory
635 && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
636 pp_dir_change (cpp_in, get_src_pwd ());
637 }
638
639 bool
640 gfc_cpp_preprocess (const char *source_file)
641 {
642 if (!gfc_cpp_enabled ())
643 return false;
644
645 cpp_change_file (cpp_in, LC_RENAME, source_file);
646
647 if (cpp_option->traditional)
648 scan_translation_unit_trad (cpp_in);
649 else
650 scan_translation_unit (cpp_in);
651
652 /* -dM command line option. */
653 if (gfc_cpp_preprocess_only () &&
654 gfc_cpp_option.dump_macros == 'M')
655 {
656 putc ('\n', print.outf);
657 cpp_forall_identifiers (cpp_in, dump_macro, NULL);
658 }
659
660 putc ('\n', print.outf);
661
662 if (!gfc_cpp_preprocess_only ()
663 || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
664 fclose (print.outf);
665
666 return true;
667 }
668
669 void
670 gfc_cpp_done (void)
671 {
672 if (!gfc_cpp_enabled ())
673 return;
674
675 gcc_assert (cpp_in);
676
677 if (gfc_cpp_makedep ())
678 {
679 if (gfc_cpp_option.deps_filename)
680 {
681 FILE *f = fopen (gfc_cpp_option.deps_filename, "w");
682 if (f)
683 {
684 cpp_finish (cpp_in, f);
685 fclose (f);
686 }
687 else
688 gfc_fatal_error ("opening output file %qs: %s",
689 gfc_cpp_option.deps_filename,
690 xstrerror (errno));
691 }
692 else
693 cpp_finish (cpp_in, stdout);
694 }
695
696 cpp_undef_all (cpp_in);
697 cpp_clear_file_cache (cpp_in);
698 }
699
700 /* PATH must be malloc-ed and NULL-terminated. */
701 void
702 gfc_cpp_add_include_path (char *path, bool user_supplied)
703 {
704 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
705 include path. Fortran does not define any system include paths. */
706 int cxx_aware = 0;
707
708 add_path (path, INC_BRACKET, cxx_aware, user_supplied);
709 }
710
711 void
712 gfc_cpp_add_include_path_after (char *path, bool user_supplied)
713 {
714 int cxx_aware = 0;
715 add_path (path, INC_AFTER, cxx_aware, user_supplied);
716 }
717
718 void
719 gfc_cpp_register_include_paths (void)
720 {
721 int cxx_stdinc = 0;
722 register_include_chains (cpp_in, gfc_cpp_option.sysroot,
723 gfc_cpp_option.prefix, gfc_cpp_option.multilib,
724 gfc_cpp_option.standard_include_paths, cxx_stdinc,
725 gfc_cpp_option.verbose);
726 }
727
728
729
730 static void scan_translation_unit_trad (cpp_reader *);
731 static void account_for_newlines (const unsigned char *, size_t);
732 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
733
734 static void print_line (location_t, const char *);
735 static void maybe_print_line (location_t);
736
737
738 /* Writes out the preprocessed file, handling spacing and paste
739 avoidance issues. */
740 static void
741 scan_translation_unit (cpp_reader *pfile)
742 {
743 bool avoid_paste = false;
744
745 print.source = NULL;
746 for (;;)
747 {
748 const cpp_token *token = cpp_get_token (pfile);
749
750 if (token->type == CPP_PADDING)
751 {
752 avoid_paste = true;
753 if (print.source == NULL
754 || (!(print.source->flags & PREV_WHITE)
755 && token->val.source == NULL))
756 print.source = token->val.source;
757 continue;
758 }
759
760 if (token->type == CPP_EOF)
761 break;
762
763 /* Subtle logic to output a space if and only if necessary. */
764 if (avoid_paste)
765 {
766 if (print.source == NULL)
767 print.source = token;
768 if (print.source->flags & PREV_WHITE
769 || (print.prev
770 && cpp_avoid_paste (pfile, print.prev, token))
771 || (print.prev == NULL && token->type == CPP_HASH))
772 putc (' ', print.outf);
773 }
774 else if (token->flags & PREV_WHITE)
775 putc (' ', print.outf);
776
777 avoid_paste = false;
778 print.source = NULL;
779 print.prev = token;
780 cpp_output_token (token, print.outf);
781
782 if (token->type == CPP_COMMENT)
783 account_for_newlines (token->val.str.text, token->val.str.len);
784 }
785 }
786
787 /* Adjust print.src_line for newlines embedded in output. */
788 static void
789 account_for_newlines (const unsigned char *str, size_t len)
790 {
791 while (len--)
792 if (*str++ == '\n')
793 print.src_line++;
794 }
795
796 /* Writes out a traditionally preprocessed file. */
797 static void
798 scan_translation_unit_trad (cpp_reader *pfile)
799 {
800 while (_cpp_read_logical_line_trad (pfile))
801 {
802 size_t len = pfile->out.cur - pfile->out.base;
803 maybe_print_line (pfile->out.first_line);
804 fwrite (pfile->out.base, 1, len, print.outf);
805 print.printed = 1;
806 if (!CPP_OPTION (pfile, discard_comments))
807 account_for_newlines (pfile->out.base, len);
808 }
809 }
810
811 /* If the token read on logical line LINE needs to be output on a
812 different line to the current one, output the required newlines or
813 a line marker. */
814 static void
815 maybe_print_line (location_t src_loc)
816 {
817 const line_map_ordinary *map
818 = linemap_check_ordinary (linemap_lookup (line_table, src_loc));
819 int src_line = SOURCE_LINE (map, src_loc);
820
821 /* End the previous line of text. */
822 if (print.printed)
823 {
824 putc ('\n', print.outf);
825 print.src_line++;
826 print.printed = 0;
827 }
828
829 if (src_line >= print.src_line && src_line < print.src_line + 8)
830 {
831 while (src_line > print.src_line)
832 {
833 putc ('\n', print.outf);
834 print.src_line++;
835 }
836 }
837 else
838 print_line (src_loc, "");
839 }
840
841 /* Output a line marker for logical line LINE. Special flags are "1"
842 or "2" indicating entering or leaving a file. */
843 static void
844 print_line (location_t src_loc, const char *special_flags)
845 {
846 /* End any previous line of text. */
847 if (print.printed)
848 putc ('\n', print.outf);
849 print.printed = 0;
850
851 if (!gfc_cpp_option.no_line_commands)
852 {
853 expanded_location loc;
854 size_t to_file_len;
855 unsigned char *to_file_quoted;
856 unsigned char *p;
857 int sysp;
858
859 loc = expand_location (src_loc);
860 to_file_len = strlen (loc.file);
861 to_file_quoted = (unsigned char *) alloca (to_file_len * 4 + 1);
862
863 print.src_line = loc.line;
864
865 /* cpp_quote_string does not nul-terminate, so we have to do it
866 ourselves. */
867 p = cpp_quote_string (to_file_quoted,
868 (const unsigned char *) loc.file, to_file_len);
869 *p = '\0';
870 fprintf (print.outf, "# %u \"%s\"%s",
871 print.src_line == 0 ? 1 : print.src_line,
872 to_file_quoted, special_flags);
873
874 sysp = in_system_header_at (src_loc);
875 if (sysp == 2)
876 fputs (" 3 4", print.outf);
877 else if (sysp == 1)
878 fputs (" 3", print.outf);
879
880 putc ('\n', print.outf);
881 }
882 }
883
884 static void
885 cb_file_change (cpp_reader * ARG_UNUSED (pfile), const line_map_ordinary *map)
886 {
887 const char *flags = "";
888
889 if (gfc_cpp_option.no_line_commands)
890 return;
891
892 if (!map)
893 return;
894
895 if (print.first_time)
896 {
897 /* Avoid printing foo.i when the main file is foo.c. */
898 if (!cpp_get_options (cpp_in)->preprocessed)
899 print_line (map->start_location, flags);
900 print.first_time = 0;
901 }
902 else
903 {
904 /* Bring current file to correct line when entering a new file. */
905 if (map->reason == LC_ENTER)
906 maybe_print_line (linemap_included_from (map));
907 if (map->reason == LC_ENTER)
908 flags = " 1";
909 else if (map->reason == LC_LEAVE)
910 flags = " 2";
911 print_line (map->start_location, flags);
912 }
913
914 }
915
916 /* Called when a line of output is started. TOKEN is the first token
917 of the line, and at end of file will be CPP_EOF. */
918 static void
919 cb_line_change (cpp_reader *pfile, const cpp_token *token,
920 int parsing_args)
921 {
922 location_t src_loc = token->src_loc;
923
924 if (token->type == CPP_EOF || parsing_args)
925 return;
926
927 maybe_print_line (src_loc);
928 print.prev = 0;
929 print.source = 0;
930
931 /* Supply enough spaces to put this token in its original column,
932 one space per column greater than 2, since scan_translation_unit
933 will provide a space if PREV_WHITE. Don't bother trying to
934 reconstruct tabs; we can't get it right in general, and nothing
935 ought to care. Some things do care; the fault lies with them. */
936 if (!CPP_OPTION (pfile, traditional))
937 {
938 const line_map_ordinary *map
939 = linemap_check_ordinary (linemap_lookup (line_table, src_loc));
940 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
941 print.printed = 1;
942
943 while (-- spaces >= 0)
944 putc (' ', print.outf);
945 }
946 }
947
948 static void
949 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
950 const cpp_string *str)
951 {
952 maybe_print_line (line);
953 fprintf (print.outf, "#ident %s\n", str->text);
954 print.src_line++;
955 }
956
957 static void
958 cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
959 cpp_hashnode *node ATTRIBUTE_UNUSED)
960 {
961 maybe_print_line (line);
962 fputs ("#define ", print.outf);
963
964 /* 'D' is whole definition; 'N' is name only. */
965 if (gfc_cpp_option.dump_macros == 'D')
966 fputs ((const char *) cpp_macro_definition (pfile, node),
967 print.outf);
968 else
969 fputs ((const char *) NODE_NAME (node), print.outf);
970
971 putc ('\n', print.outf);
972 if (LOCATION_LINE (line) != 0)
973 print.src_line++;
974 }
975
976 static void
977 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
978 cpp_hashnode *node)
979 {
980 maybe_print_line (line);
981 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
982 print.src_line++;
983 }
984
985 static void
986 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, location_t line,
987 const unsigned char *dir, const char *header, int angle_brackets,
988 const cpp_token **comments)
989 {
990 maybe_print_line (line);
991 if (angle_brackets)
992 fprintf (print.outf, "#%s <%s>", dir, header);
993 else
994 fprintf (print.outf, "#%s \"%s\"", dir, header);
995
996 if (comments != NULL)
997 {
998 while (*comments != NULL)
999 {
1000 if ((*comments)->flags & PREV_WHITE)
1001 putc (' ', print.outf);
1002 cpp_output_token (*comments, print.outf);
1003 ++comments;
1004 }
1005 }
1006
1007 putc ('\n', print.outf);
1008 print.src_line++;
1009 }
1010
1011 /* Dump out the hash table. */
1012 static int
1013 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
1014 {
1015 if (cpp_user_macro_p (node))
1016 {
1017 fputs ("#define ", print.outf);
1018 fputs ((const char *) cpp_macro_definition (pfile, node),
1019 print.outf);
1020 putc ('\n', print.outf);
1021 print.src_line++;
1022 }
1023
1024 return 1;
1025 }
1026
1027 static void
1028 cb_used_define (cpp_reader *pfile, location_t line ATTRIBUTE_UNUSED,
1029 cpp_hashnode *node)
1030 {
1031 gfc_cpp_macro_queue *q;
1032 q = XNEW (gfc_cpp_macro_queue);
1033 q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
1034 q->next = cpp_define_queue;
1035 cpp_define_queue = q;
1036 }
1037
1038 /* Callback from cpp_error for PFILE to print diagnostics from the
1039 preprocessor. The diagnostic is of type LEVEL, with REASON set
1040 to the reason code if LEVEL is represents a warning, at location
1041 RICHLOC; MSG is the translated message and AP the arguments.
1042 Returns true if a diagnostic was emitted, false otherwise. */
1043
1044 static bool
1045 cb_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
1046 enum cpp_diagnostic_level level,
1047 enum cpp_warning_reason reason,
1048 rich_location *richloc,
1049 const char *msg, va_list *ap)
1050 {
1051 diagnostic_info diagnostic;
1052 diagnostic_t dlevel;
1053 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
1054 bool ret;
1055
1056 switch (level)
1057 {
1058 case CPP_DL_WARNING_SYSHDR:
1059 global_dc->dc_warn_system_headers = 1;
1060 /* Fall through. */
1061 case CPP_DL_WARNING:
1062 dlevel = DK_WARNING;
1063 break;
1064 case CPP_DL_PEDWARN:
1065 dlevel = DK_PEDWARN;
1066 break;
1067 case CPP_DL_ERROR:
1068 dlevel = DK_ERROR;
1069 break;
1070 case CPP_DL_ICE:
1071 dlevel = DK_ICE;
1072 break;
1073 case CPP_DL_NOTE:
1074 dlevel = DK_NOTE;
1075 break;
1076 case CPP_DL_FATAL:
1077 dlevel = DK_FATAL;
1078 break;
1079 default:
1080 gcc_unreachable ();
1081 }
1082 diagnostic_set_info_translated (&diagnostic, msg, ap,
1083 richloc, dlevel);
1084 if (reason == CPP_W_WARNING_DIRECTIVE)
1085 diagnostic_override_option_index (&diagnostic, OPT_Wcpp);
1086 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
1087 if (level == CPP_DL_WARNING_SYSHDR)
1088 global_dc->dc_warn_system_headers = save_warn_system_headers;
1089 return ret;
1090 }
1091
1092 /* Callback called when -fworking-director and -E to emit working
1093 directory in cpp output file. */
1094
1095 void
1096 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1097 {
1098 size_t to_file_len = strlen (dir);
1099 unsigned char *to_file_quoted =
1100 (unsigned char *) alloca (to_file_len * 4 + 1);
1101 unsigned char *p;
1102
1103 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
1104 p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
1105 *p = '\0';
1106 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
1107 }
1108
1109 /* Copy a #pragma directive to the preprocessed output. */
1110 static void
1111 cb_def_pragma (cpp_reader *pfile, location_t line)
1112 {
1113 maybe_print_line (line);
1114 fputs ("#pragma ", print.outf);
1115 cpp_output_line (pfile, print.outf);
1116 print.src_line++;
1117 }
1118
1119 static void
1120 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
1121 location_t line ATTRIBUTE_UNUSED,
1122 cpp_hashnode *node)
1123 {
1124 gfc_cpp_macro_queue *q;
1125 q = XNEW (gfc_cpp_macro_queue);
1126 q->macro = xstrdup ((const char *) NODE_NAME (node));
1127 q->next = cpp_undefine_queue;
1128 cpp_undefine_queue = q;
1129 }
1130
1131 static void
1132 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
1133 {
1134 gfc_cpp_macro_queue *q;
1135
1136 /* End the previous line of text. */
1137 if (print.printed)
1138 {
1139 putc ('\n', print.outf);
1140 print.src_line++;
1141 print.printed = 0;
1142 }
1143
1144 for (q = cpp_define_queue; q;)
1145 {
1146 gfc_cpp_macro_queue *oq;
1147 fputs ("#define ", print.outf);
1148 fputs (q->macro, print.outf);
1149 putc ('\n', print.outf);
1150 print.src_line++;
1151 oq = q;
1152 q = q->next;
1153 free (oq->macro);
1154 free (oq);
1155 }
1156 cpp_define_queue = NULL;
1157 for (q = cpp_undefine_queue; q;)
1158 {
1159 gfc_cpp_macro_queue *oq;
1160 fprintf (print.outf, "#undef %s\n", q->macro);
1161 print.src_line++;
1162 oq = q;
1163 q = q->next;
1164 free (oq->macro);
1165 free (oq);
1166 }
1167 cpp_undefine_queue = NULL;
1168 }