1 /* Copyright (C) 2008-2023 Free Software Foundation, Inc.
3 This file is part of GCC.
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License
16 along with GCC; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
21 #include "coretypes.h"
23 #define GCC_C_COMMON_C
24 #include "options.h" /* For cpp_reason_option_codes. */
29 #include "diagnostic.h"
33 #include "../../libcpp/internal.h"
36 #include "cppbuiltin.h"
39 #ifndef TARGET_SYSTEM_ROOT
40 # define TARGET_SYSTEM_ROOT NULL
43 #ifndef TARGET_CPU_CPP_BUILTINS
44 # define TARGET_CPU_CPP_BUILTINS()
47 #ifndef TARGET_OS_CPP_BUILTINS
48 # define TARGET_OS_CPP_BUILTINS()
51 #ifndef TARGET_OBJFMT_CPP_BUILTINS
52 # define TARGET_OBJFMT_CPP_BUILTINS()
56 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
57 handling is deferred to gfc_cpp_init (). */
63 gfc_cpp_deferred_opt_t
;
66 /* Defined and undefined macros being queued for output with -dU at
68 typedef struct gfc_cpp_macro_queue
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
74 } gfc_cpp_macro_queue
;
75 static gfc_cpp_macro_queue
*cpp_define_queue
, *cpp_undefine_queue
;
77 struct gfc_cpp_option_data
79 /* Argument of -cpp, implied by SPEC;
80 if NULL, preprocessing disabled. */
81 const char *temporary_filename
;
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 */
96 int deps_skip_system
; /* -MM */
97 const char *deps_filename
; /* -M[M]D */
98 const char *deps_filename_user
; /* -MF <arg> */
99 int deps_missing_are_generated
; /* -MG */
100 int deps_phony
; /* -MP */
101 int warn_date_time
; /* -Wdate-time */
103 const char *multilib
; /* -imultilib <dir> */
104 const char *prefix
; /* -iprefix <dir> */
105 const char *sysroot
; /* -isysroot <dir> */
107 /* Options whose handling needs to be deferred until the
108 appropriate cpp-objects are created:
112 gfc_cpp_deferred_opt_t
*deferred_opt
;
113 int deferred_opt_count
;
117 /* Structures used with libcpp: */
118 static cpp_options
*cpp_option
= NULL
;
119 static cpp_reader
*cpp_in
= NULL
;
121 /* Encapsulates state used to convert a stream of cpp-tokens into
125 FILE *outf
; /* Stream to write to. */
126 const cpp_token
*prev
; /* Previous token. */
127 const cpp_token
*source
; /* Source token for spacing. */
128 int src_line
; /* Line number currently being written. */
129 unsigned char printed
; /* Nonzero if something output at line. */
130 bool first_time
; /* cb_file_change hasn't been called yet. */
133 /* General output routines. */
134 static void scan_translation_unit (cpp_reader
*);
135 static void scan_translation_unit_trad (cpp_reader
*);
137 /* Callback routines for the parser. Most of these are active only
138 in specific modes. */
139 static void cb_file_change (cpp_reader
*, const line_map_ordinary
*);
140 static void cb_line_change (cpp_reader
*, const cpp_token
*, int);
141 static void cb_define (cpp_reader
*, location_t
, cpp_hashnode
*);
142 static void cb_undef (cpp_reader
*, location_t
, cpp_hashnode
*);
143 static void cb_def_pragma (cpp_reader
*, location_t
);
144 static void cb_include (cpp_reader
*, location_t
, const unsigned char *,
145 const char *, int, const cpp_token
**);
146 static void cb_ident (cpp_reader
*, location_t
, const cpp_string
*);
147 static void cb_used_define (cpp_reader
*, location_t
, cpp_hashnode
*);
148 static void cb_used_undef (cpp_reader
*, location_t
, cpp_hashnode
*);
149 static bool cb_cpp_diagnostic (cpp_reader
*, enum cpp_diagnostic_level
,
150 enum cpp_warning_reason
, rich_location
*,
151 const char *, va_list *)
152 ATTRIBUTE_GCC_DIAG(5,0);
153 void pp_dir_change (cpp_reader
*, const char *);
155 static int dump_macro (cpp_reader
*, cpp_hashnode
*, void *);
156 static void dump_queued_macros (cpp_reader
*);
160 cpp_define_builtins (cpp_reader
*pfile
)
162 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
163 in C, defines __STDC_HOSTED__?! */
164 cpp_init_builtins (pfile
, 0);
166 /* Initialize GFORTRAN specific builtins.
167 These are documented. */
168 define_language_independent_builtin_macros (pfile
);
169 cpp_define (pfile
, "__GFORTRAN__=1");
170 cpp_define (pfile
, "_LANGUAGE_FORTRAN=1");
173 cpp_define (pfile
, "_OPENACC=201711");
176 cpp_define (pfile
, "_OPENMP=201511");
178 /* The defines below are necessary for the TARGET_* macros.
180 FIXME: Note that builtin_define_std() actually is a function
181 in c-cppbuiltin.cc which uses flags undefined for Fortran.
182 Let's skip this for now. If needed, one needs to look into it
185 # define builtin_define(TXT) cpp_define (pfile, TXT)
186 # define builtin_define_std(TXT)
187 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
189 /* FIXME: Pandora's Box
190 Using the macros below results in multiple breakages:
191 - mingw will fail to compile this file as dependent macros
192 assume to be used in c-cppbuiltin.cc only. Further, they use
193 flags only valid/defined in C (same as noted above).
194 [config/i386/mingw32.h, config/i386/cygming.h]
195 - other platforms (not as popular) break similarly
196 [grep for 'builtin_define_with_int_value' in gcc/config/]
198 TARGET_CPU_CPP_BUILTINS ();
199 TARGET_OS_CPP_BUILTINS ();
200 TARGET_OBJFMT_CPP_BUILTINS (); */
202 #undef builtin_define
203 #undef builtin_define_std
204 #undef builtin_assert
208 gfc_cpp_enabled (void)
210 return gfc_cpp_option
.temporary_filename
!= NULL
;
214 gfc_cpp_preprocess_only (void)
216 return gfc_cpp_option
.preprocess_only
;
220 gfc_cpp_makedep (void)
222 return gfc_cpp_option
.deps
;
226 gfc_cpp_add_dep (const char *name
, bool system
)
228 if (!gfc_cpp_option
.deps_skip_system
|| !system
)
229 if (mkdeps
*deps
= cpp_get_deps (cpp_in
))
230 deps_add_dep (deps
, name
);
234 gfc_cpp_add_target (const char *name
)
236 if (mkdeps
*deps
= cpp_get_deps (cpp_in
))
237 deps_add_target (deps
, name
, 0);
242 gfc_cpp_temporary_file (void)
244 return gfc_cpp_option
.temporary_filename
;
248 gfc_cpp_register_include_paths (bool verbose_missing_dir_warn
)
251 cpp_get_options (cpp_in
)->warn_missing_include_dirs
252 = (global_options
.x_cpp_warn_missing_include_dirs
253 && verbose_missing_dir_warn
);
254 register_include_chains (cpp_in
, gfc_cpp_option
.sysroot
,
255 gfc_cpp_option
.prefix
, gfc_cpp_option
.multilib
,
256 gfc_cpp_option
.standard_include_paths
, cxx_stdinc
,
257 gfc_cpp_option
.verbose
);
261 gfc_cpp_init_options (unsigned int decoded_options_count
,
262 struct cl_decoded_option
*decoded_options ATTRIBUTE_UNUSED
)
264 /* Do not create any objects from libcpp here. If no
265 preprocessing is requested, this would be wasted
268 See gfc_cpp_post_options() instead. */
270 gfc_cpp_option
.temporary_filename
= NULL
;
271 gfc_cpp_option
.output_filename
= NULL
;
272 gfc_cpp_option
.preprocess_only
= 0;
273 gfc_cpp_option
.discard_comments
= 1;
274 gfc_cpp_option
.discard_comments_in_macro_exp
= 1;
275 gfc_cpp_option
.print_include_names
= 0;
276 gfc_cpp_option
.no_line_commands
= 0;
277 gfc_cpp_option
.dump_macros
= '\0';
278 gfc_cpp_option
.dump_includes
= 0;
279 gfc_cpp_option
.working_directory
= -1;
280 gfc_cpp_option
.no_predefined
= 0;
281 gfc_cpp_option
.standard_include_paths
= 1;
282 gfc_cpp_option
.verbose
= 0;
283 gfc_cpp_option
.warn_date_time
= 0;
284 gfc_cpp_option
.deps
= 0;
285 gfc_cpp_option
.deps_skip_system
= 0;
286 gfc_cpp_option
.deps_phony
= 0;
287 gfc_cpp_option
.deps_missing_are_generated
= 0;
288 gfc_cpp_option
.deps_filename
= NULL
;
289 gfc_cpp_option
.deps_filename_user
= NULL
;
291 gfc_cpp_option
.multilib
= NULL
;
292 gfc_cpp_option
.prefix
= NULL
;
293 gfc_cpp_option
.sysroot
= TARGET_SYSTEM_ROOT
;
295 gfc_cpp_option
.deferred_opt
= XNEWVEC (gfc_cpp_deferred_opt_t
,
296 decoded_options_count
);
297 gfc_cpp_option
.deferred_opt_count
= 0;
301 gfc_cpp_handle_option (size_t scode
, const char *arg
, int value ATTRIBUTE_UNUSED
)
304 enum opt_code code
= (enum opt_code
) scode
;
313 gfc_cpp_option
.temporary_filename
= arg
;
317 gfc_cpp_option
.temporary_filename
= 0L;
328 gfc_cpp_option
.dump_macros
= *arg
;
332 gfc_cpp_option
.dump_includes
= 1;
337 case OPT_fworking_directory
:
338 gfc_cpp_option
.working_directory
= value
;
342 gfc_cpp_add_include_path_after (xstrdup(arg
), true);
346 gfc_cpp_option
.multilib
= arg
;
350 gfc_cpp_option
.prefix
= arg
;
354 gfc_cpp_option
.sysroot
= arg
;
359 gfc_cpp_add_include_path (xstrdup(arg
), true);
363 gfc_cpp_option
.standard_include_paths
= value
;
367 if (!gfc_cpp_option
.output_filename
)
368 gfc_cpp_option
.output_filename
= arg
;
370 gfc_fatal_error ("output filename specified twice");
374 gfc_cpp_option
.no_predefined
= value
;
378 gfc_cpp_option
.verbose
= value
;
382 gfc_cpp_option
.warn_date_time
= value
;
388 gfc_cpp_option
.deferred_opt
[gfc_cpp_option
.deferred_opt_count
].code
= code
;
389 gfc_cpp_option
.deferred_opt
[gfc_cpp_option
.deferred_opt_count
].arg
= arg
;
390 gfc_cpp_option
.deferred_opt_count
++;
394 gfc_cpp_option
.discard_comments
= 0;
398 gfc_cpp_option
.discard_comments
= 0;
399 gfc_cpp_option
.discard_comments_in_macro_exp
= 0;
403 gfc_cpp_option
.preprocess_only
= 1;
407 gfc_cpp_option
.print_include_names
= 1;
411 gfc_cpp_option
.deps_skip_system
= 1;
415 gfc_cpp_option
.deps
= 1;
419 gfc_cpp_option
.deps_skip_system
= 1;
423 gfc_cpp_option
.deps
= 1;
424 gfc_cpp_option
.deps_filename
= arg
;
428 /* If specified multiple times, last one wins. */
429 gfc_cpp_option
.deps_filename_user
= arg
;
433 gfc_cpp_option
.deps_missing_are_generated
= 1;
437 gfc_cpp_option
.deps_phony
= 1;
442 gfc_cpp_option
.deferred_opt
[gfc_cpp_option
.deferred_opt_count
].code
= code
;
443 gfc_cpp_option
.deferred_opt
[gfc_cpp_option
.deferred_opt_count
].arg
= arg
;
444 gfc_cpp_option
.deferred_opt_count
++;
448 gfc_cpp_option
.no_line_commands
= 1;
455 /* This function needs to be called before gfc_cpp_register_include_paths
456 as the latter may diagnose missing include directories. */
458 gfc_cpp_init_cb (void)
460 struct cpp_callbacks
*cb
;
462 cb
= cpp_get_callbacks (cpp_in
);
463 cb
->file_change
= cb_file_change
;
464 cb
->line_change
= cb_line_change
;
465 cb
->ident
= cb_ident
;
466 cb
->def_pragma
= cb_def_pragma
;
467 cb
->diagnostic
= cb_cpp_diagnostic
;
469 if (gfc_cpp_option
.dump_includes
)
470 cb
->include
= cb_include
;
472 if ((gfc_cpp_option
.dump_macros
== 'D')
473 || (gfc_cpp_option
.dump_macros
== 'N'))
475 cb
->define
= cb_define
;
476 cb
->undef
= cb_undef
;
479 if (gfc_cpp_option
.dump_macros
== 'U')
481 cb
->before_define
= dump_queued_macros
;
482 cb
->used_define
= cb_used_define
;
483 cb
->used_undef
= cb_used_undef
;
488 gfc_cpp_post_options (bool verbose_missing_dir_warn
)
490 /* Any preprocessing-related option without '-cpp' is considered
492 if (!gfc_cpp_enabled ()
493 && (gfc_cpp_preprocess_only ()
494 || gfc_cpp_makedep ()
495 || !gfc_cpp_option
.discard_comments
496 || !gfc_cpp_option
.discard_comments_in_macro_exp
497 || gfc_cpp_option
.print_include_names
498 || gfc_cpp_option
.no_line_commands
499 || gfc_cpp_option
.dump_macros
500 || gfc_cpp_option
.dump_includes
))
501 gfc_fatal_error ("To enable preprocessing, use %<-cpp%>");
503 if (!gfc_cpp_enabled ())
506 cpp_in
= cpp_create_reader (CLK_GNUC89
, NULL
, line_table
);
509 /* The cpp_options-structure defines far more flags than those set here.
510 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
511 inter-option dependencies that may need to be enforced. */
512 cpp_option
= cpp_get_options (cpp_in
);
513 gcc_assert (cpp_option
);
515 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
516 cpp_option
->traditional
= 1;
517 cpp_option
->cplusplus_comments
= 0;
519 cpp_option
->cpp_pedantic
= pedantic
;
521 cpp_option
->dollars_in_ident
= flag_dollar_ok
;
522 cpp_option
->discard_comments
= gfc_cpp_option
.discard_comments
;
523 cpp_option
->discard_comments_in_macro_exp
= gfc_cpp_option
.discard_comments_in_macro_exp
;
524 cpp_option
->print_include_names
= gfc_cpp_option
.print_include_names
;
525 cpp_option
->preprocessed
= gfc_option
.flag_preprocessed
;
526 cpp_option
->warn_date_time
= gfc_cpp_option
.warn_date_time
;
528 if (gfc_cpp_makedep ())
530 cpp_option
->deps
.style
= DEPS_USER
;
531 cpp_option
->deps
.phony_targets
= gfc_cpp_option
.deps_phony
;
532 cpp_option
->deps
.missing_files
= gfc_cpp_option
.deps_missing_are_generated
;
534 /* -MF <arg> overrides -M[M]D. */
535 if (gfc_cpp_option
.deps_filename_user
)
536 gfc_cpp_option
.deps_filename
= gfc_cpp_option
.deps_filename_user
;
539 if (gfc_cpp_option
.working_directory
== -1)
540 gfc_cpp_option
.working_directory
= (debug_info_level
!= DINFO_LEVEL_NONE
);
542 cpp_post_options (cpp_in
);
545 /* Let diagnostics infrastructure know how to convert input files the same
546 way libcpp will do it, namely, with no charset conversion but with
547 skipping of a UTF-8 BOM if present. */
548 diagnostic_initialize_input_context (global_dc
, nullptr, true);
551 gfc_cpp_register_include_paths (verbose_missing_dir_warn
);
556 gfc_cpp_init_0 (void)
558 /* Initialize the print structure. Setting print.src_line to -1 here is
559 a trick to guarantee that the first token of the file will cause
560 a linemarker to be output by maybe_print_line. */
564 print
.first_time
= 1;
566 if (gfc_cpp_preprocess_only ())
568 if (gfc_cpp_option
.output_filename
)
570 /* This needs cheating: with "-E -o <file>", the user wants the
571 preprocessed output in <file>. However, if nothing is done
572 about it <file> is also used for assembler output. Hence, it
573 is necessary to redirect assembler output (actually nothing
574 as -E implies -fsyntax-only) to another file, otherwise the
575 output from preprocessing is lost. */
576 asm_file_name
= gfc_cpp_option
.temporary_filename
;
578 print
.outf
= fopen (gfc_cpp_option
.output_filename
, "w");
579 if (print
.outf
== NULL
)
580 gfc_fatal_error ("opening output file %qs: %s",
581 gfc_cpp_option
.output_filename
,
589 print
.outf
= fopen (gfc_cpp_option
.temporary_filename
, "w");
590 if (print
.outf
== NULL
)
591 gfc_fatal_error ("opening output file %qs: %s",
592 gfc_cpp_option
.temporary_filename
, xstrerror (errno
));
596 if (!cpp_read_main_file (cpp_in
, gfc_source_file
))
605 if (gfc_option
.flag_preprocessed
)
608 cpp_change_file (cpp_in
, LC_RENAME
, special_fname_builtin ());
609 if (!gfc_cpp_option
.no_predefined
)
611 /* Make sure all of the builtins about to be declared have
612 BUILTINS_LOCATION has their location_t. */
613 cpp_force_token_locations (cpp_in
, BUILTINS_LOCATION
);
615 cpp_define_builtins (cpp_in
);
617 cpp_stop_forcing_token_locations (cpp_in
);
620 /* Handle deferred options from command-line. */
621 cpp_change_file (cpp_in
, LC_RENAME
, _("<command-line>"));
623 for (i
= 0; i
< gfc_cpp_option
.deferred_opt_count
; i
++)
625 gfc_cpp_deferred_opt_t
*opt
= &gfc_cpp_option
.deferred_opt
[i
];
627 if (opt
->code
== OPT_D
)
628 cpp_define (cpp_in
, opt
->arg
);
629 else if (opt
->code
== OPT_U
)
630 cpp_undef (cpp_in
, opt
->arg
);
631 else if (opt
->code
== OPT_A
)
633 if (opt
->arg
[0] == '-')
634 cpp_unassert (cpp_in
, opt
->arg
+ 1);
636 cpp_assert (cpp_in
, opt
->arg
);
638 else if (opt
->code
== OPT_MT
|| opt
->code
== OPT_MQ
)
639 if (mkdeps
*deps
= cpp_get_deps (cpp_in
))
640 deps_add_target (deps
, opt
->arg
, opt
->code
== OPT_MQ
);
643 /* Pre-defined macros for non-required INTEGER kind types. */
644 for (gfc_integer_info
*itype
= gfc_integer_kinds
; itype
->kind
!= 0; itype
++)
646 if (itype
->kind
== 1)
647 cpp_define (cpp_in
, "__GFC_INT_1__=1");
648 if (itype
->kind
== 2)
649 cpp_define (cpp_in
, "__GFC_INT_2__=1");
650 if (itype
->kind
== 8)
651 cpp_define (cpp_in
, "__GFC_INT_8__=1");
652 if (itype
->kind
== 16)
653 cpp_define (cpp_in
, "__GFC_INT_16__=1");
656 /* Pre-defined macros for non-required REAL kind types. */
657 for (gfc_real_info
*rtype
= gfc_real_kinds
; rtype
->kind
!= 0; rtype
++)
659 if (rtype
->kind
== 10)
660 cpp_define (cpp_in
, "__GFC_REAL_10__=1");
661 if (rtype
->kind
== 16)
662 cpp_define (cpp_in
, "__GFC_REAL_16__=1");
665 if (gfc_cpp_option
.working_directory
666 && gfc_cpp_option
.preprocess_only
&& !gfc_cpp_option
.no_line_commands
)
667 pp_dir_change (cpp_in
, get_src_pwd ());
671 gfc_cpp_preprocess (const char *source_file
)
673 if (!gfc_cpp_enabled ())
676 cpp_change_file (cpp_in
, LC_RENAME
, source_file
);
678 if (cpp_option
->traditional
)
679 scan_translation_unit_trad (cpp_in
);
681 scan_translation_unit (cpp_in
);
683 /* -dM command line option. */
684 if (gfc_cpp_preprocess_only () &&
685 gfc_cpp_option
.dump_macros
== 'M')
687 putc ('\n', print
.outf
);
688 cpp_forall_identifiers (cpp_in
, dump_macro
, NULL
);
691 putc ('\n', print
.outf
);
693 if (!gfc_cpp_preprocess_only ()
694 || (gfc_cpp_preprocess_only () && gfc_cpp_option
.output_filename
))
703 if (!gfc_cpp_enabled ())
708 if (gfc_cpp_makedep ())
710 if (gfc_cpp_option
.deps_filename
)
712 FILE *f
= fopen (gfc_cpp_option
.deps_filename
, "w");
715 cpp_finish (cpp_in
, f
);
719 gfc_fatal_error ("opening output file %qs: %s",
720 gfc_cpp_option
.deps_filename
,
724 cpp_finish (cpp_in
, stdout
);
727 cpp_undef_all (cpp_in
);
728 cpp_clear_file_cache (cpp_in
);
731 /* PATH must be malloc-ed and NULL-terminated. */
733 gfc_cpp_add_include_path (char *path
, bool user_supplied
)
735 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
736 include path. Fortran does not define any system include paths. */
739 add_path (path
, INC_BRACKET
, cxx_aware
, user_supplied
);
743 gfc_cpp_add_include_path_after (char *path
, bool user_supplied
)
746 add_path (path
, INC_AFTER
, cxx_aware
, user_supplied
);
750 static void scan_translation_unit_trad (cpp_reader
*);
751 static void account_for_newlines (const unsigned char *, size_t);
753 static void print_line (location_t
, const char *);
754 static void maybe_print_line (location_t
);
757 /* Writes out the preprocessed file, handling spacing and paste
760 scan_translation_unit (cpp_reader
*pfile
)
762 bool avoid_paste
= false;
767 const cpp_token
*token
= cpp_get_token (pfile
);
769 if (token
->type
== CPP_PADDING
)
772 if (print
.source
== NULL
773 || (!(print
.source
->flags
& PREV_WHITE
)
774 && token
->val
.source
== NULL
))
775 print
.source
= token
->val
.source
;
779 if (token
->type
== CPP_EOF
)
782 /* Subtle logic to output a space if and only if necessary. */
785 if (print
.source
== NULL
)
786 print
.source
= token
;
787 if (print
.source
->flags
& PREV_WHITE
789 && cpp_avoid_paste (pfile
, print
.prev
, token
))
790 || (print
.prev
== NULL
&& token
->type
== CPP_HASH
))
791 putc (' ', print
.outf
);
793 else if (token
->flags
& PREV_WHITE
)
794 putc (' ', print
.outf
);
799 cpp_output_token (token
, print
.outf
);
801 if (token
->type
== CPP_COMMENT
)
802 account_for_newlines (token
->val
.str
.text
, token
->val
.str
.len
);
806 /* Adjust print.src_line for newlines embedded in output. */
808 account_for_newlines (const unsigned char *str
, size_t len
)
815 /* Writes out a traditionally preprocessed file. */
817 scan_translation_unit_trad (cpp_reader
*pfile
)
819 while (_cpp_read_logical_line_trad (pfile
))
821 size_t len
= pfile
->out
.cur
- pfile
->out
.base
;
822 maybe_print_line (pfile
->out
.first_line
);
823 fwrite (pfile
->out
.base
, 1, len
, print
.outf
);
825 if (!CPP_OPTION (pfile
, discard_comments
))
826 account_for_newlines (pfile
->out
.base
, len
);
830 /* If the token read on logical line LINE needs to be output on a
831 different line to the current one, output the required newlines or
834 maybe_print_line (location_t src_loc
)
836 const line_map_ordinary
*map
837 = linemap_check_ordinary (linemap_lookup (line_table
, src_loc
));
838 int src_line
= SOURCE_LINE (map
, src_loc
);
840 /* End the previous line of text. */
843 putc ('\n', print
.outf
);
848 if (src_line
>= print
.src_line
&& src_line
< print
.src_line
+ 8)
850 while (src_line
> print
.src_line
)
852 putc ('\n', print
.outf
);
857 print_line (src_loc
, "");
860 /* Output a line marker for logical line LINE. Special flags are "1"
861 or "2" indicating entering or leaving a file. */
863 print_line (location_t src_loc
, const char *special_flags
)
865 /* End any previous line of text. */
867 putc ('\n', print
.outf
);
870 if (!gfc_cpp_option
.no_line_commands
)
872 expanded_location loc
;
874 unsigned char *to_file_quoted
;
878 loc
= expand_location (src_loc
);
879 to_file_len
= strlen (loc
.file
);
880 to_file_quoted
= (unsigned char *) alloca (to_file_len
* 4 + 1);
882 print
.src_line
= loc
.line
;
884 /* cpp_quote_string does not nul-terminate, so we have to do it
886 p
= cpp_quote_string (to_file_quoted
,
887 (const unsigned char *) loc
.file
, to_file_len
);
889 fprintf (print
.outf
, "# %u \"%s\"%s",
890 print
.src_line
== 0 ? 1 : print
.src_line
,
891 to_file_quoted
, special_flags
);
893 sysp
= in_system_header_at (src_loc
);
895 fputs (" 3 4", print
.outf
);
897 fputs (" 3", print
.outf
);
899 putc ('\n', print
.outf
);
904 cb_file_change (cpp_reader
* ARG_UNUSED (pfile
), const line_map_ordinary
*map
)
906 const char *flags
= "";
908 if (gfc_cpp_option
.no_line_commands
)
914 if (print
.first_time
)
916 /* Avoid printing foo.i when the main file is foo.c. */
917 if (!cpp_get_options (cpp_in
)->preprocessed
)
918 print_line (map
->start_location
, flags
);
919 print
.first_time
= 0;
923 /* Bring current file to correct line when entering a new file. */
924 if (map
->reason
== LC_ENTER
)
925 maybe_print_line (linemap_included_from (map
));
926 if (map
->reason
== LC_ENTER
)
928 else if (map
->reason
== LC_LEAVE
)
930 print_line (map
->start_location
, flags
);
935 /* Called when a line of output is started. TOKEN is the first token
936 of the line, and at end of file will be CPP_EOF. */
938 cb_line_change (cpp_reader
*pfile
, const cpp_token
*token
,
941 location_t src_loc
= token
->src_loc
;
943 if (token
->type
== CPP_EOF
|| parsing_args
)
946 maybe_print_line (src_loc
);
950 /* Supply enough spaces to put this token in its original column,
951 one space per column greater than 2, since scan_translation_unit
952 will provide a space if PREV_WHITE. Don't bother trying to
953 reconstruct tabs; we can't get it right in general, and nothing
954 ought to care. Some things do care; the fault lies with them. */
955 if (!CPP_OPTION (pfile
, traditional
))
957 const line_map_ordinary
*map
958 = linemap_check_ordinary (linemap_lookup (line_table
, src_loc
));
959 int spaces
= SOURCE_COLUMN (map
, src_loc
) - 2;
962 while (-- spaces
>= 0)
963 putc (' ', print
.outf
);
968 cb_ident (cpp_reader
*pfile ATTRIBUTE_UNUSED
, location_t line
,
969 const cpp_string
*str
)
971 maybe_print_line (line
);
972 fprintf (print
.outf
, "#ident %s\n", str
->text
);
977 cb_define (cpp_reader
*pfile ATTRIBUTE_UNUSED
, location_t line
,
978 cpp_hashnode
*node ATTRIBUTE_UNUSED
)
980 maybe_print_line (line
);
981 fputs ("#define ", print
.outf
);
983 /* 'D' is whole definition; 'N' is name only. */
984 if (gfc_cpp_option
.dump_macros
== 'D')
985 fputs ((const char *) cpp_macro_definition (pfile
, node
),
988 fputs ((const char *) NODE_NAME (node
), print
.outf
);
990 putc ('\n', print
.outf
);
991 if (LOCATION_LINE (line
) != 0)
996 cb_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
, location_t line
,
999 maybe_print_line (line
);
1000 fprintf (print
.outf
, "#undef %s\n", NODE_NAME (node
));
1005 cb_include (cpp_reader
*pfile ATTRIBUTE_UNUSED
, location_t line
,
1006 const unsigned char *dir
, const char *header
, int angle_brackets
,
1007 const cpp_token
**comments
)
1009 maybe_print_line (line
);
1011 fprintf (print
.outf
, "#%s <%s>", dir
, header
);
1013 fprintf (print
.outf
, "#%s \"%s\"", dir
, header
);
1015 if (comments
!= NULL
)
1017 while (*comments
!= NULL
)
1019 if ((*comments
)->flags
& PREV_WHITE
)
1020 putc (' ', print
.outf
);
1021 cpp_output_token (*comments
, print
.outf
);
1026 putc ('\n', print
.outf
);
1030 /* Dump out the hash table. */
1032 dump_macro (cpp_reader
*pfile
, cpp_hashnode
*node
, void *v ATTRIBUTE_UNUSED
)
1034 if (cpp_user_macro_p (node
))
1036 fputs ("#define ", print
.outf
);
1037 fputs ((const char *) cpp_macro_definition (pfile
, node
),
1039 putc ('\n', print
.outf
);
1047 cb_used_define (cpp_reader
*pfile
, location_t line ATTRIBUTE_UNUSED
,
1050 gfc_cpp_macro_queue
*q
;
1051 q
= XNEW (gfc_cpp_macro_queue
);
1052 q
->macro
= xstrdup ((const char *) cpp_macro_definition (pfile
, node
));
1053 q
->next
= cpp_define_queue
;
1054 cpp_define_queue
= q
;
1057 /* Return the gcc option code associated with the reason for a cpp
1058 message, or 0 if none. */
1061 cb_cpp_diagnostic_cpp_option (enum cpp_warning_reason reason
)
1063 const struct cpp_reason_option_codes_t
*entry
;
1065 for (entry
= cpp_reason_option_codes
; entry
->reason
!= CPP_W_NONE
; entry
++)
1066 if (entry
->reason
== reason
)
1067 return entry
->option_code
;
1072 /* Callback from cpp_error for PFILE to print diagnostics from the
1073 preprocessor. The diagnostic is of type LEVEL, with REASON set
1074 to the reason code if LEVEL is represents a warning, at location
1075 RICHLOC; MSG is the translated message and AP the arguments.
1076 Returns true if a diagnostic was emitted, false otherwise. */
1079 cb_cpp_diagnostic (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1080 enum cpp_diagnostic_level level
,
1081 enum cpp_warning_reason reason
,
1082 rich_location
*richloc
,
1083 const char *msg
, va_list *ap
)
1085 diagnostic_info diagnostic
;
1086 diagnostic_t dlevel
;
1087 bool save_warn_system_headers
= global_dc
->m_warn_system_headers
;
1092 case CPP_DL_WARNING_SYSHDR
:
1093 global_dc
->m_warn_system_headers
= 1;
1095 case CPP_DL_WARNING
:
1096 dlevel
= DK_WARNING
;
1098 case CPP_DL_PEDWARN
:
1099 dlevel
= DK_PEDWARN
;
1116 diagnostic_set_info_translated (&diagnostic
, msg
, ap
,
1118 diagnostic_override_option_index (&diagnostic
,
1119 cb_cpp_diagnostic_cpp_option (reason
));
1120 ret
= diagnostic_report_diagnostic (global_dc
, &diagnostic
);
1121 if (level
== CPP_DL_WARNING_SYSHDR
)
1122 global_dc
->m_warn_system_headers
= save_warn_system_headers
;
1126 /* Callback called when -fworking-director and -E to emit working
1127 directory in cpp output file. */
1130 pp_dir_change (cpp_reader
*pfile ATTRIBUTE_UNUSED
, const char *dir
)
1132 size_t to_file_len
= strlen (dir
);
1133 unsigned char *to_file_quoted
=
1134 (unsigned char *) alloca (to_file_len
* 4 + 1);
1137 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
1138 p
= cpp_quote_string (to_file_quoted
, (const unsigned char *) dir
, to_file_len
);
1140 fprintf (print
.outf
, "# 1 \"%s//\"\n", to_file_quoted
);
1143 /* Copy a #pragma directive to the preprocessed output. */
1145 cb_def_pragma (cpp_reader
*pfile
, location_t line
)
1147 maybe_print_line (line
);
1148 fputs ("#pragma ", print
.outf
);
1149 cpp_output_line (pfile
, print
.outf
);
1154 cb_used_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1155 location_t line ATTRIBUTE_UNUSED
,
1158 gfc_cpp_macro_queue
*q
;
1159 q
= XNEW (gfc_cpp_macro_queue
);
1160 q
->macro
= xstrdup ((const char *) NODE_NAME (node
));
1161 q
->next
= cpp_undefine_queue
;
1162 cpp_undefine_queue
= q
;
1166 dump_queued_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
1168 gfc_cpp_macro_queue
*q
;
1170 /* End the previous line of text. */
1173 putc ('\n', print
.outf
);
1178 for (q
= cpp_define_queue
; q
;)
1180 gfc_cpp_macro_queue
*oq
;
1181 fputs ("#define ", print
.outf
);
1182 fputs (q
->macro
, print
.outf
);
1183 putc ('\n', print
.outf
);
1190 cpp_define_queue
= NULL
;
1191 for (q
= cpp_undefine_queue
; q
;)
1193 gfc_cpp_macro_queue
*oq
;
1194 fprintf (print
.outf
, "#undef %s\n", q
->macro
);
1201 cpp_undefine_queue
= NULL
;