Debian Codegraph @

gcc/fortran/cpp.cc

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