OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [fortran/] [cpp.c] - Blame information for rev 729

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.