1 /* Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3 This file is part of GCC.
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License
16 along with GCC; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
21 #include "coretypes.h"
30 #include "tm_p.h" /* Target prototypes. */
33 #include "diagnostic.h"
35 #include "../../libcpp/internal.h"
40 #ifndef TARGET_OS_CPP_BUILTINS
41 # define TARGET_OS_CPP_BUILTINS()
44 #ifndef TARGET_OBJFMT_CPP_BUILTINS
45 # define TARGET_OBJFMT_CPP_BUILTINS()
49 /* Holds switches parsed by gfc_cpp_handle_option (), but whose
50 handling is deferred to gfc_cpp_init (). */
56 gfc_cpp_deferred_opt_t
;
59 /* Defined and undefined macros being queued for output with -dU at
61 typedef struct gfc_cpp_macro_queue
63 struct gfc_cpp_macro_queue
*next
; /* Next macro in the list. */
64 char *macro
; /* The name of the macro if not
65 defined, the full definition if
67 } gfc_cpp_macro_queue
;
68 static gfc_cpp_macro_queue
*cpp_define_queue
, *cpp_undefine_queue
;
70 struct gfc_cpp_option_data
72 /* Argument of -cpp, implied by SPEC;
73 if NULL, preprocessing disabled. */
74 const char *temporary_filename
;
76 const char *output_filename
; /* -o <arg> */
77 int preprocess_only
; /* -E */
78 int discard_comments
; /* -C */
79 int discard_comments_in_macro_exp
; /* -CC */
80 int print_include_names
; /* -H */
81 int no_line_commands
; /* -P */
82 char dump_macros
; /* -d[DMNU] */
83 int dump_includes
; /* -dI */
84 int working_directory
; /* -fworking-directory */
85 int no_predefined
; /* -undef */
86 int standard_include_paths
; /* -nostdinc */
89 int deps_skip_system
; /* -MM */
90 const char *deps_filename
; /* -M[M]D */
91 const char *deps_filename_user
; /* -MF <arg> */
92 int deps_missing_are_generated
; /* -MG */
93 int deps_phony
; /* -MP */
95 const char *multilib
; /* -imultilib <dir> */
96 const char *prefix
; /* -iprefix <dir> */
97 const char *sysroot
; /* -isysroot <dir> */
99 /* Options whose handling needs to be deferred until the
100 appropriate cpp-objects are created:
104 gfc_cpp_deferred_opt_t
*deferred_opt
;
105 int deferred_opt_count
;
109 /* Structures used with libcpp: */
110 static cpp_options
*cpp_option
= NULL
;
111 static cpp_reader
*cpp_in
= NULL
;
113 /* Defined in toplev.c. */
114 extern const char *asm_file_name
;
119 /* Encapsulates state used to convert a stream of cpp-tokens into
123 FILE *outf
; /* Stream to write to. */
124 const cpp_token
*prev
; /* Previous token. */
125 const cpp_token
*source
; /* Source token for spacing. */
126 int src_line
; /* Line number currently being written. */
127 unsigned char printed
; /* Nonzero if something output at line. */
128 bool first_time
; /* cb_file_change hasn't been called yet. */
131 /* General output routines. */
132 static void scan_translation_unit (cpp_reader
*);
133 static void scan_translation_unit_trad (cpp_reader
*);
135 /* Callback routines for the parser. Most of these are active only
136 in specific modes. */
137 static void cb_file_change (cpp_reader
*, const struct line_map
*);
138 static void cb_line_change (cpp_reader
*, const cpp_token
*, int);
139 static void cb_define (cpp_reader
*, source_location
, cpp_hashnode
*);
140 static void cb_undef (cpp_reader
*, source_location
, cpp_hashnode
*);
141 static void cb_def_pragma (cpp_reader
*, source_location
);
142 static void cb_include (cpp_reader
*, source_location
, const unsigned char *,
143 const char *, int, const cpp_token
**);
144 static void cb_ident (cpp_reader
*, source_location
, const cpp_string
*);
145 static void cb_used_define (cpp_reader
*, source_location
, cpp_hashnode
*);
146 static void cb_used_undef (cpp_reader
*, source_location
, cpp_hashnode
*);
147 static bool cb_cpp_error (cpp_reader
*, int, int, location_t
, unsigned int,
148 const char *, va_list *)
149 ATTRIBUTE_GCC_DIAG(6,0);
150 void pp_dir_change (cpp_reader
*, const char *);
152 static int dump_macro (cpp_reader
*, cpp_hashnode
*, void *);
153 static void dump_queued_macros (cpp_reader
*);
157 cpp_define_builtins (cpp_reader
*pfile
)
159 int major
, minor
, patchlevel
;
161 /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted'
162 in C, defines __STDC_HOSTED__?! */
163 cpp_init_builtins (pfile
, 0);
165 /* Initialize GFORTRAN specific builtins.
166 These are documented. */
167 if (sscanf (BASEVER
, "%d.%d.%d", &major
, &minor
, &patchlevel
) != 3)
169 sscanf (BASEVER
, "%d.%d", &major
, &minor
);
172 cpp_define_formatted (pfile
, "__GNUC__=%d", major
);
173 cpp_define_formatted (pfile
, "__GNUC_MINOR__=%d", minor
);
174 cpp_define_formatted (pfile
, "__GNUC_PATCHLEVEL__=%d", patchlevel
);
176 cpp_define (pfile
, "__GFORTRAN__=1");
177 cpp_define (pfile
, "_LANGUAGE_FORTRAN=1");
179 if (gfc_option
.flag_openmp
)
180 cpp_define (pfile
, "_OPENMP=200805");
183 /* More builtins that might be useful, but are not documented
184 (in no particular order). */
185 cpp_define_formatted (pfile
, "__VERSION__=\"%s\"", version_string
);
189 cpp_define_formatted (pfile
, "__pic__=%d", flag_pic
);
190 cpp_define_formatted (pfile
, "__PIC__=%d", flag_pic
);
194 cpp_define_formatted (pfile
, "__pie__=%d", flag_pie
);
195 cpp_define_formatted (pfile
, "__PIE__=%d", flag_pie
);
199 cpp_define (pfile
, "__OPTIMIZE_SIZE__");
201 cpp_define (pfile
, "__OPTIMIZE__");
203 if (fast_math_flags_set_p ())
204 cpp_define (pfile
, "__FAST_MATH__");
205 if (flag_signaling_nans
)
206 cpp_define (pfile
, "__SUPPORT_SNAN__");
208 cpp_define_formatted (pfile
, "__FINITE_MATH_ONLY__=%d", flag_finite_math_only
);
210 /* Definitions for LP64 model. */
211 if (TYPE_PRECISION (long_integer_type_node
) == 64
212 && POINTER_SIZE
== 64
213 && TYPE_PRECISION (integer_type_node
) == 32)
215 cpp_define (pfile
, "_LP64");
216 cpp_define (pfile
, "__LP64__");
219 /* Define NAME with value TYPE size_unit.
220 The C-side also defines __SIZEOF_WCHAR_T__, __SIZEOF_WINT_T__
221 __SIZEOF_PTRDIFF_T__, however, fortran seems to lack the
222 appropriate type nodes. */
224 #define define_type_sizeof(NAME, TYPE) \
225 cpp_define_formatted (pfile, NAME"="HOST_WIDE_INT_PRINT_DEC, \
226 tree_low_cst (TYPE_SIZE_UNIT (TYPE), 1))
228 define_type_sizeof ("__SIZEOF_INT__", integer_type_node
);
229 define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node
);
230 define_type_sizeof ("__SIZEOF_LONG_LONG__", long_long_integer_type_node
);
231 define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node
);
232 define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node
);
233 define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node
);
234 define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node
);
235 define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node
);
237 #undef define_type_sizeof
239 /* The defines below are necessary for the TARGET_* macros.
241 FIXME: Note that builtin_define_std() actually is a function
242 in c-cppbuiltin.c which uses flags undefined for Fortran.
243 Let's skip this for now. If needed, one needs to look into it
246 # define builtin_define(TXT) cpp_define (pfile, TXT)
247 # define builtin_define_std(TXT)
248 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
250 /* FIXME: Pandora's Box
251 Using the macros below results in multiple breakages:
252 - mingw will fail to compile this file as dependent macros
253 assume to be used in c-cppbuiltin.c only. Further, they use
254 flags only valid/defined in C (same as noted above).
255 [config/i386/mingw32.h, config/i386/cygming.h]
256 - other platforms (not as popular) break similarly
257 [grep for 'builtin_define_with_int_value' in gcc/config/]
259 TARGET_CPU_CPP_BUILTINS ();
260 TARGET_OS_CPP_BUILTINS ();
261 TARGET_OBJFMT_CPP_BUILTINS (); */
263 #undef builtin_define
264 #undef builtin_define_std
265 #undef builtin_assert
269 gfc_cpp_enabled (void)
271 return gfc_cpp_option
.temporary_filename
!= NULL
;
275 gfc_cpp_preprocess_only (void)
277 return gfc_cpp_option
.preprocess_only
;
281 gfc_cpp_makedep (void)
283 return gfc_cpp_option
.deps
;
287 gfc_cpp_add_dep (const char *name
, bool system
)
289 if (!gfc_cpp_option
.deps_skip_system
|| !system
)
290 deps_add_dep (cpp_get_deps (cpp_in
), name
);
294 gfc_cpp_add_target (const char *name
)
296 deps_add_target (cpp_get_deps (cpp_in
), name
, 0);
301 gfc_cpp_temporary_file (void)
303 return gfc_cpp_option
.temporary_filename
;
307 gfc_cpp_init_options (unsigned int decoded_options_count
,
308 struct cl_decoded_option
*decoded_options ATTRIBUTE_UNUSED
)
310 /* Do not create any objects from libcpp here. If no
311 preprocessing is requested, this would be wasted
314 See gfc_cpp_post_options() instead. */
316 gfc_cpp_option
.temporary_filename
= NULL
;
317 gfc_cpp_option
.output_filename
= NULL
;
318 gfc_cpp_option
.preprocess_only
= 0;
319 gfc_cpp_option
.discard_comments
= 1;
320 gfc_cpp_option
.discard_comments_in_macro_exp
= 1;
321 gfc_cpp_option
.print_include_names
= 0;
322 gfc_cpp_option
.no_line_commands
= 0;
323 gfc_cpp_option
.dump_macros
= '\0';
324 gfc_cpp_option
.dump_includes
= 0;
325 gfc_cpp_option
.working_directory
= -1;
326 gfc_cpp_option
.no_predefined
= 0;
327 gfc_cpp_option
.standard_include_paths
= 1;
328 gfc_cpp_option
.verbose
= 0;
329 gfc_cpp_option
.deps
= 0;
330 gfc_cpp_option
.deps_skip_system
= 0;
331 gfc_cpp_option
.deps_phony
= 0;
332 gfc_cpp_option
.deps_missing_are_generated
= 0;
333 gfc_cpp_option
.deps_filename
= NULL
;
334 gfc_cpp_option
.deps_filename_user
= NULL
;
336 gfc_cpp_option
.multilib
= NULL
;
337 gfc_cpp_option
.prefix
= NULL
;
338 gfc_cpp_option
.sysroot
= NULL
;
340 gfc_cpp_option
.deferred_opt
= XNEWVEC (gfc_cpp_deferred_opt_t
,
341 decoded_options_count
);
342 gfc_cpp_option
.deferred_opt_count
= 0;
346 gfc_cpp_handle_option (size_t scode
, const char *arg
, int value ATTRIBUTE_UNUSED
)
349 enum opt_code code
= (enum opt_code
) scode
;
358 gfc_cpp_option
.temporary_filename
= arg
;
362 gfc_cpp_option
.temporary_filename
= 0L;
373 gfc_cpp_option
.dump_macros
= *arg
;
377 gfc_cpp_option
.dump_includes
= 1;
382 case OPT_fworking_directory
:
383 gfc_cpp_option
.working_directory
= value
;
387 gfc_cpp_add_include_path_after (xstrdup(arg
), true);
391 gfc_cpp_option
.multilib
= arg
;
395 gfc_cpp_option
.prefix
= arg
;
399 gfc_cpp_option
.sysroot
= arg
;
404 gfc_cpp_add_include_path (xstrdup(arg
), true);
408 gfc_cpp_option
.standard_include_paths
= value
;
412 if (!gfc_cpp_option
.output_filename
)
413 gfc_cpp_option
.output_filename
= arg
;
415 gfc_fatal_error ("output filename specified twice");
419 gfc_cpp_option
.no_predefined
= value
;
423 gfc_cpp_option
.verbose
= value
;
429 gfc_cpp_option
.deferred_opt
[gfc_cpp_option
.deferred_opt_count
].code
= code
;
430 gfc_cpp_option
.deferred_opt
[gfc_cpp_option
.deferred_opt_count
].arg
= arg
;
431 gfc_cpp_option
.deferred_opt_count
++;
435 gfc_cpp_option
.discard_comments
= 0;
439 gfc_cpp_option
.discard_comments
= 0;
440 gfc_cpp_option
.discard_comments_in_macro_exp
= 0;
444 gfc_cpp_option
.preprocess_only
= 1;
448 gfc_cpp_option
.print_include_names
= 1;
452 gfc_cpp_option
.deps_skip_system
= 1;
456 gfc_cpp_option
.deps
= 1;
460 gfc_cpp_option
.deps_skip_system
= 1;
464 gfc_cpp_option
.deps
= 1;
465 gfc_cpp_option
.deps_filename
= arg
;
469 /* If specified multiple times, last one wins. */
470 gfc_cpp_option
.deps_filename_user
= arg
;
474 gfc_cpp_option
.deps_missing_are_generated
= 1;
478 gfc_cpp_option
.deps_phony
= 1;
483 gfc_cpp_option
.deferred_opt
[gfc_cpp_option
.deferred_opt_count
].code
= code
;
484 gfc_cpp_option
.deferred_opt
[gfc_cpp_option
.deferred_opt_count
].arg
= arg
;
485 gfc_cpp_option
.deferred_opt_count
++;
489 gfc_cpp_option
.no_line_commands
= 1;
498 gfc_cpp_post_options (void)
500 /* Any preprocessing-related option without '-cpp' is considered
502 if (!gfc_cpp_enabled ()
503 && (gfc_cpp_preprocess_only ()
504 || gfc_cpp_makedep ()
505 || !gfc_cpp_option
.discard_comments
506 || !gfc_cpp_option
.discard_comments_in_macro_exp
507 || gfc_cpp_option
.print_include_names
508 || gfc_cpp_option
.no_line_commands
509 || gfc_cpp_option
.dump_macros
510 || gfc_cpp_option
.dump_includes
))
511 gfc_fatal_error("To enable preprocessing, use -cpp");
513 cpp_in
= cpp_create_reader (CLK_GNUC89
, NULL
, line_table
);
514 if (!gfc_cpp_enabled ())
519 /* The cpp_options-structure defines far more flags than those set here.
520 If any other is implemented, see c-opt.c (sanitize_cpp_opts) for
521 inter-option dependencies that may need to be enforced. */
522 cpp_option
= cpp_get_options (cpp_in
);
523 gcc_assert (cpp_option
);
525 /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */
526 cpp_option
->traditional
= 1;
527 cpp_option
->cplusplus_comments
= 0;
529 cpp_option
->pedantic
= pedantic
;
531 cpp_option
->dollars_in_ident
= gfc_option
.flag_dollar_ok
;
532 cpp_option
->discard_comments
= gfc_cpp_option
.discard_comments
;
533 cpp_option
->discard_comments_in_macro_exp
= gfc_cpp_option
.discard_comments_in_macro_exp
;
534 cpp_option
->print_include_names
= gfc_cpp_option
.print_include_names
;
535 cpp_option
->preprocessed
= gfc_option
.flag_preprocessed
;
537 if (gfc_cpp_makedep ())
539 cpp_option
->deps
.style
= DEPS_USER
;
540 cpp_option
->deps
.phony_targets
= gfc_cpp_option
.deps_phony
;
541 cpp_option
->deps
.missing_files
= gfc_cpp_option
.deps_missing_are_generated
;
543 /* -MF <arg> overrides -M[M]D. */
544 if (gfc_cpp_option
.deps_filename_user
)
545 gfc_cpp_option
.deps_filename
= gfc_cpp_option
.deps_filename_user
;
548 if (gfc_cpp_option
.working_directory
== -1)
549 gfc_cpp_option
.working_directory
= (debug_info_level
!= DINFO_LEVEL_NONE
);
551 cpp_post_options (cpp_in
);
553 gfc_cpp_register_include_paths ();
558 gfc_cpp_init_0 (void)
560 struct cpp_callbacks
*cb
;
562 cb
= cpp_get_callbacks (cpp_in
);
563 cb
->file_change
= cb_file_change
;
564 cb
->line_change
= cb_line_change
;
565 cb
->ident
= cb_ident
;
566 cb
->def_pragma
= cb_def_pragma
;
567 cb
->error
= cb_cpp_error
;
569 if (gfc_cpp_option
.dump_includes
)
570 cb
->include
= cb_include
;
572 if ((gfc_cpp_option
.dump_macros
== 'D')
573 || (gfc_cpp_option
.dump_macros
== 'N'))
575 cb
->define
= cb_define
;
576 cb
->undef
= cb_undef
;
579 if (gfc_cpp_option
.dump_macros
== 'U')
581 cb
->before_define
= dump_queued_macros
;
582 cb
->used_define
= cb_used_define
;
583 cb
->used_undef
= cb_used_undef
;
586 /* Initialize the print structure. Setting print.src_line to -1 here is
587 a trick to guarantee that the first token of the file will cause
588 a linemarker to be output by maybe_print_line. */
592 print
.first_time
= 1;
594 if (gfc_cpp_preprocess_only ())
596 if (gfc_cpp_option
.output_filename
)
598 /* This needs cheating: with "-E -o <file>", the user wants the
599 preprocessed output in <file>. However, if nothing is done
600 about it <file> is also used for assembler output. Hence, it
601 is necessary to redirect assembler output (actually nothing
602 as -E implies -fsyntax-only) to another file, otherwise the
603 output from preprocessing is lost. */
604 asm_file_name
= gfc_cpp_option
.temporary_filename
;
606 print
.outf
= fopen (gfc_cpp_option
.output_filename
, "w");
607 if (print
.outf
== NULL
)
608 gfc_fatal_error ("opening output file %s: %s",
609 gfc_cpp_option
.output_filename
,
617 print
.outf
= fopen (gfc_cpp_option
.temporary_filename
, "w");
618 if (print
.outf
== NULL
)
619 gfc_fatal_error ("opening output file %s: %s",
620 gfc_cpp_option
.temporary_filename
, xstrerror (errno
));
624 if (!cpp_read_main_file (cpp_in
, gfc_source_file
))
633 if (gfc_option
.flag_preprocessed
)
636 cpp_change_file (cpp_in
, LC_RENAME
, _("<built-in>"));
637 if (!gfc_cpp_option
.no_predefined
)
638 cpp_define_builtins (cpp_in
);
640 /* Handle deferred options from command-line. */
641 cpp_change_file (cpp_in
, LC_RENAME
, _("<command-line>"));
643 for (i
= 0; i
< gfc_cpp_option
.deferred_opt_count
; i
++)
645 gfc_cpp_deferred_opt_t
*opt
= &gfc_cpp_option
.deferred_opt
[i
];
647 if (opt
->code
== OPT_D
)
648 cpp_define (cpp_in
, opt
->arg
);
649 else if (opt
->code
== OPT_U
)
650 cpp_undef (cpp_in
, opt
->arg
);
651 else if (opt
->code
== OPT_A
)
653 if (opt
->arg
[0] == '-')
654 cpp_unassert (cpp_in
, opt
->arg
+ 1);
656 cpp_assert (cpp_in
, opt
->arg
);
658 else if (opt
->code
== OPT_MT
|| opt
->code
== OPT_MQ
)
659 deps_add_target (cpp_get_deps (cpp_in
),
660 opt
->arg
, opt
->code
== OPT_MQ
);
663 if (gfc_cpp_option
.working_directory
664 && gfc_cpp_option
.preprocess_only
&& !gfc_cpp_option
.no_line_commands
)
665 pp_dir_change (cpp_in
, get_src_pwd ());
669 gfc_cpp_preprocess (const char *source_file
)
671 if (!gfc_cpp_enabled ())
674 cpp_change_file (cpp_in
, LC_RENAME
, source_file
);
676 if (cpp_option
->traditional
)
677 scan_translation_unit_trad (cpp_in
);
679 scan_translation_unit (cpp_in
);
681 /* -dM command line option. */
682 if (gfc_cpp_preprocess_only () &&
683 gfc_cpp_option
.dump_macros
== 'M')
685 putc ('\n', print
.outf
);
686 cpp_forall_identifiers (cpp_in
, dump_macro
, NULL
);
689 putc ('\n', print
.outf
);
691 if (!gfc_cpp_preprocess_only ()
692 || (gfc_cpp_preprocess_only () && gfc_cpp_option
.output_filename
))
701 if (!gfc_cpp_enabled ())
706 if (gfc_cpp_makedep ())
708 if (gfc_cpp_option
.deps_filename
)
710 FILE *f
= fopen (gfc_cpp_option
.deps_filename
, "w");
713 cpp_finish (cpp_in
, f
);
717 gfc_fatal_error ("opening output file %s: %s",
718 gfc_cpp_option
.deps_filename
,
722 cpp_finish (cpp_in
, stdout
);
725 cpp_undef_all (cpp_in
);
726 cpp_clear_file_cache (cpp_in
);
729 /* PATH must be malloc-ed and NULL-terminated. */
731 gfc_cpp_add_include_path (char *path
, bool user_supplied
)
733 /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
734 include path. Fortran does not define any system include paths. */
737 add_path (path
, BRACKET
, cxx_aware
, user_supplied
);
741 gfc_cpp_add_include_path_after (char *path
, bool user_supplied
)
744 add_path (path
, AFTER
, cxx_aware
, user_supplied
);
748 gfc_cpp_register_include_paths (void)
751 register_include_chains (cpp_in
, gfc_cpp_option
.sysroot
,
752 gfc_cpp_option
.prefix
, gfc_cpp_option
.multilib
,
753 gfc_cpp_option
.standard_include_paths
, cxx_stdinc
,
754 gfc_cpp_option
.verbose
);
759 static void scan_translation_unit_trad (cpp_reader
*);
760 static void account_for_newlines (const unsigned char *, size_t);
761 static int dump_macro (cpp_reader
*, cpp_hashnode
*, void *);
763 static void print_line (source_location
, const char *);
764 static void maybe_print_line (source_location
);
767 /* Writes out the preprocessed file, handling spacing and paste
770 scan_translation_unit (cpp_reader
*pfile
)
772 bool avoid_paste
= false;
777 const cpp_token
*token
= cpp_get_token (pfile
);
779 if (token
->type
== CPP_PADDING
)
782 if (print
.source
== NULL
783 || (!(print
.source
->flags
& PREV_WHITE
)
784 && token
->val
.source
== NULL
))
785 print
.source
= token
->val
.source
;
789 if (token
->type
== CPP_EOF
)
792 /* Subtle logic to output a space if and only if necessary. */
795 if (print
.source
== NULL
)
796 print
.source
= token
;
797 if (print
.source
->flags
& PREV_WHITE
799 && cpp_avoid_paste (pfile
, print
.prev
, token
))
800 || (print
.prev
== NULL
&& token
->type
== CPP_HASH
))
801 putc (' ', print
.outf
);
803 else if (token
->flags
& PREV_WHITE
)
804 putc (' ', print
.outf
);
809 cpp_output_token (token
, print
.outf
);
811 if (token
->type
== CPP_COMMENT
)
812 account_for_newlines (token
->val
.str
.text
, token
->val
.str
.len
);
816 /* Adjust print.src_line for newlines embedded in output. */
818 account_for_newlines (const unsigned char *str
, size_t len
)
825 /* Writes out a traditionally preprocessed file. */
827 scan_translation_unit_trad (cpp_reader
*pfile
)
829 while (_cpp_read_logical_line_trad (pfile
))
831 size_t len
= pfile
->out
.cur
- pfile
->out
.base
;
832 maybe_print_line (pfile
->out
.first_line
);
833 fwrite (pfile
->out
.base
, 1, len
, print
.outf
);
835 if (!CPP_OPTION (pfile
, discard_comments
))
836 account_for_newlines (pfile
->out
.base
, len
);
840 /* If the token read on logical line LINE needs to be output on a
841 different line to the current one, output the required newlines or
844 maybe_print_line (source_location src_loc
)
846 const struct line_map
*map
= linemap_lookup (line_table
, src_loc
);
847 int src_line
= SOURCE_LINE (map
, src_loc
);
849 /* End the previous line of text. */
852 putc ('\n', print
.outf
);
857 if (src_line
>= print
.src_line
&& src_line
< print
.src_line
+ 8)
859 while (src_line
> print
.src_line
)
861 putc ('\n', print
.outf
);
866 print_line (src_loc
, "");
869 /* Output a line marker for logical line LINE. Special flags are "1"
870 or "2" indicating entering or leaving a file. */
872 print_line (source_location src_loc
, const char *special_flags
)
874 /* End any previous line of text. */
876 putc ('\n', print
.outf
);
879 if (!gfc_cpp_option
.no_line_commands
)
881 const struct line_map
*map
= linemap_lookup (line_table
, src_loc
);
883 size_t to_file_len
= strlen (map
->to_file
);
884 unsigned char *to_file_quoted
=
885 (unsigned char *) alloca (to_file_len
* 4 + 1);
888 print
.src_line
= SOURCE_LINE (map
, src_loc
);
890 /* cpp_quote_string does not nul-terminate, so we have to do it
892 p
= cpp_quote_string (to_file_quoted
,
893 (const unsigned char *) map
->to_file
, to_file_len
);
895 fprintf (print
.outf
, "# %u \"%s\"%s",
896 print
.src_line
== 0 ? 1 : print
.src_line
,
897 to_file_quoted
, special_flags
);
900 fputs (" 3 4", print
.outf
);
901 else if (map
->sysp
== 1)
902 fputs (" 3", print
.outf
);
904 putc ('\n', print
.outf
);
909 cb_file_change (cpp_reader
* ARG_UNUSED (pfile
), const struct line_map
*map
)
911 const char *flags
= "";
913 if (gfc_cpp_option
.no_line_commands
)
919 if (print
.first_time
)
921 /* Avoid printing foo.i when the main file is foo.c. */
922 if (!cpp_get_options (cpp_in
)->preprocessed
)
923 print_line (map
->start_location
, flags
);
924 print
.first_time
= 0;
928 /* Bring current file to correct line when entering a new file. */
929 if (map
->reason
== LC_ENTER
)
931 const struct line_map
*from
= INCLUDED_FROM (line_table
, map
);
932 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from
));
934 if (map
->reason
== LC_ENTER
)
936 else if (map
->reason
== LC_LEAVE
)
938 print_line (map
->start_location
, flags
);
943 /* Called when a line of output is started. TOKEN is the first token
944 of the line, and at end of file will be CPP_EOF. */
946 cb_line_change (cpp_reader
*pfile
, const cpp_token
*token
,
949 source_location src_loc
= token
->src_loc
;
951 if (token
->type
== CPP_EOF
|| parsing_args
)
954 maybe_print_line (src_loc
);
958 /* Supply enough spaces to put this token in its original column,
959 one space per column greater than 2, since scan_translation_unit
960 will provide a space if PREV_WHITE. Don't bother trying to
961 reconstruct tabs; we can't get it right in general, and nothing
962 ought to care. Some things do care; the fault lies with them. */
963 if (!CPP_OPTION (pfile
, traditional
))
965 const struct line_map
*map
= linemap_lookup (line_table
, src_loc
);
966 int spaces
= SOURCE_COLUMN (map
, src_loc
) - 2;
969 while (-- spaces
>= 0)
970 putc (' ', print
.outf
);
975 cb_ident (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
976 const cpp_string
*str
)
978 maybe_print_line (line
);
979 fprintf (print
.outf
, "#ident %s\n", str
->text
);
984 cb_define (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
985 cpp_hashnode
*node ATTRIBUTE_UNUSED
)
987 maybe_print_line (line
);
988 fputs ("#define ", print
.outf
);
990 /* 'D' is whole definition; 'N' is name only. */
991 if (gfc_cpp_option
.dump_macros
== 'D')
992 fputs ((const char *) cpp_macro_definition (pfile
, node
),
995 fputs ((const char *) NODE_NAME (node
), print
.outf
);
997 putc ('\n', print
.outf
);
998 if (linemap_lookup (line_table
, line
)->to_line
!= 0)
1003 cb_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
1006 maybe_print_line (line
);
1007 fprintf (print
.outf
, "#undef %s\n", NODE_NAME (node
));
1012 cb_include (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
1013 const unsigned char *dir
, const char *header
, int angle_brackets
,
1014 const cpp_token
**comments
)
1016 maybe_print_line (line
);
1018 fprintf (print
.outf
, "#%s <%s>", dir
, header
);
1020 fprintf (print
.outf
, "#%s \"%s\"", dir
, header
);
1022 if (comments
!= NULL
)
1024 while (*comments
!= NULL
)
1026 if ((*comments
)->flags
& PREV_WHITE
)
1027 putc (' ', print
.outf
);
1028 cpp_output_token (*comments
, print
.outf
);
1033 putc ('\n', print
.outf
);
1037 /* Dump out the hash table. */
1039 dump_macro (cpp_reader
*pfile
, cpp_hashnode
*node
, void *v ATTRIBUTE_UNUSED
)
1041 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
1043 fputs ("#define ", print
.outf
);
1044 fputs ((const char *) cpp_macro_definition (pfile
, node
),
1046 putc ('\n', print
.outf
);
1054 cb_used_define (cpp_reader
*pfile
, source_location line ATTRIBUTE_UNUSED
,
1057 gfc_cpp_macro_queue
*q
;
1058 q
= XNEW (gfc_cpp_macro_queue
);
1059 q
->macro
= xstrdup ((const char *) cpp_macro_definition (pfile
, node
));
1060 q
->next
= cpp_define_queue
;
1061 cpp_define_queue
= q
;
1064 /* Callback from cpp_error for PFILE to print diagnostics from the
1065 preprocessor. The diagnostic is of type LEVEL, with REASON set
1066 to the reason code if LEVEL is represents a warning, at location
1067 LOCATION, with column number possibly overridden by COLUMN_OVERRIDE
1068 if not zero; MSG is the translated message and AP the arguments.
1069 Returns true if a diagnostic was emitted, false otherwise. */
1072 cb_cpp_error (cpp_reader
*pfile ATTRIBUTE_UNUSED
, int level
, int reason
,
1073 location_t location
, unsigned int column_override
,
1074 const char *msg
, va_list *ap
)
1076 diagnostic_info diagnostic
;
1077 diagnostic_t dlevel
;
1078 bool save_warn_system_headers
= global_dc
->warn_system_headers
;
1083 case CPP_DL_WARNING_SYSHDR
:
1084 global_dc
->warn_system_headers
= 1;
1086 case CPP_DL_WARNING
:
1087 dlevel
= DK_WARNING
;
1089 case CPP_DL_PEDWARN
:
1090 dlevel
= DK_PEDWARN
;
1107 diagnostic_set_info_translated (&diagnostic
, msg
, ap
,
1109 if (column_override
)
1110 diagnostic_override_column (&diagnostic
, column_override
);
1111 if (reason
== CPP_W_WARNING_DIRECTIVE
)
1112 diagnostic_override_option_index (&diagnostic
, OPT_Wcpp
);
1113 ret
= report_diagnostic (&diagnostic
);
1114 if (level
== CPP_DL_WARNING_SYSHDR
)
1115 global_dc
->warn_system_headers
= save_warn_system_headers
;
1119 /* Callback called when -fworking-director and -E to emit working
1120 directory in cpp output file. */
1123 pp_dir_change (cpp_reader
*pfile ATTRIBUTE_UNUSED
, const char *dir
)
1125 size_t to_file_len
= strlen (dir
);
1126 unsigned char *to_file_quoted
=
1127 (unsigned char *) alloca (to_file_len
* 4 + 1);
1130 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
1131 p
= cpp_quote_string (to_file_quoted
, (const unsigned char *) dir
, to_file_len
);
1133 fprintf (print
.outf
, "# 1 \"%s//\"\n", to_file_quoted
);
1136 /* Copy a #pragma directive to the preprocessed output. */
1138 cb_def_pragma (cpp_reader
*pfile
, source_location line
)
1140 maybe_print_line (line
);
1141 fputs ("#pragma ", print
.outf
);
1142 cpp_output_line (pfile
, print
.outf
);
1147 cb_used_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1148 source_location line ATTRIBUTE_UNUSED
,
1151 gfc_cpp_macro_queue
*q
;
1152 q
= XNEW (gfc_cpp_macro_queue
);
1153 q
->macro
= xstrdup ((const char *) NODE_NAME (node
));
1154 q
->next
= cpp_undefine_queue
;
1155 cpp_undefine_queue
= q
;
1159 dump_queued_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
1161 gfc_cpp_macro_queue
*q
;
1163 /* End the previous line of text. */
1166 putc ('\n', print
.outf
);
1171 for (q
= cpp_define_queue
; q
;)
1173 gfc_cpp_macro_queue
*oq
;
1174 fputs ("#define ", print
.outf
);
1175 fputs (q
->macro
, print
.outf
);
1176 putc ('\n', print
.outf
);
1180 gfc_free (oq
->macro
);
1183 cpp_define_queue
= NULL
;
1184 for (q
= cpp_undefine_queue
; q
;)
1186 gfc_cpp_macro_queue
*oq
;
1187 fprintf (print
.outf
, "#undef %s\n", q
->macro
);
1191 gfc_free (oq
->macro
);
1194 cpp_undefine_queue
= NULL
;