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