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