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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [fortran/] [cpp.c] - Blame information for rev 826

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 285 jeremybenn
/* Copyright (C) 2008, 2009 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 378 julius
#include "mkdeps.h"
39 285 jeremybenn
 
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 ().  */
51
typedef struct
52
{
53
    enum opt_code code;
54
    const char *arg;
55
}
56
gfc_cpp_deferred_opt_t;
57
 
58
 
59
/* Defined and undefined macros being queued for output with -dU at
60
   the next newline.  */
61
typedef 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;
68
static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
69
 
70
struct gfc_cpp_option_data
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 */
88 378 julius
  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 */
94 285 jeremybenn
 
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
}
107
gfc_cpp_option;
108
 
109
/* Structures used with libcpp:  */
110
static cpp_options *cpp_option = NULL;
111
static cpp_reader *cpp_in = NULL;
112
 
113
/* Defined in toplev.c.  */
114
extern 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.  */
121
static 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.  */
132
static void scan_translation_unit (cpp_reader *);
133
static void scan_translation_unit_trad (cpp_reader *);
134
 
135
/* Callback routines for the parser. Most of these are active only
136
   in specific modes.  */
137
static void cb_file_change (cpp_reader *, const struct line_map *);
138
static void cb_line_change (cpp_reader *, const cpp_token *, int);
139
static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
140
static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
141
static void cb_def_pragma (cpp_reader *, source_location);
142
static void cb_include (cpp_reader *, source_location, const unsigned char *,
143
                        const char *, int, const cpp_token **);
144
static void cb_ident (cpp_reader *, source_location, const cpp_string *);
145
static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
146
static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
147
static bool cb_cpp_error (cpp_reader *, int, location_t, unsigned int,
148
                          const char *, va_list *)
149
     ATTRIBUTE_GCC_DIAG(5,0);
150
void pp_dir_change (cpp_reader *, const char *);
151
 
152
static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
153
static void dump_queued_macros (cpp_reader *);
154
 
155
 
156
static void
157
cpp_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)
180
    cpp_define (pfile, "_OPENMP=200805");
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
 
250
  /* FIXME: Pandora's Box
251
    Using the macros below results in multiple breakages:
252
     - mingw will fail to compile this file as dependent macros
253
       assume to be used in c-cppbuiltin.c only. Further, they use
254
       flags only valid/defined in C (same as noted above).
255
       [config/i386/mingw32.h, config/i386/cygming.h]
256
     - other platforms (not as popular) break similarly
257
       [grep for 'builtin_define_with_int_value' in gcc/config/]
258
 
259
  TARGET_CPU_CPP_BUILTINS ();
260
  TARGET_OS_CPP_BUILTINS ();
261
  TARGET_OBJFMT_CPP_BUILTINS (); */
262
 
263
#undef builtin_define
264
#undef builtin_define_std
265
#undef builtin_assert
266
}
267
 
268
bool
269
gfc_cpp_enabled (void)
270
{
271
  return gfc_cpp_option.temporary_filename != NULL;
272
}
273
 
274
bool
275
gfc_cpp_preprocess_only (void)
276
{
277
  return gfc_cpp_option.preprocess_only;
278
}
279
 
280 378 julius
bool
281
gfc_cpp_makedep (void)
282
{
283
  return gfc_cpp_option.deps;
284
}
285
 
286
void
287
gfc_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
 
293
void
294
gfc_cpp_add_target (const char *name)
295
{
296
  deps_add_target (cpp_get_deps (cpp_in), name, 0);
297
}
298
 
299
 
300 285 jeremybenn
const char *
301
gfc_cpp_temporary_file (void)
302
{
303
  return gfc_cpp_option.temporary_filename;
304
}
305
 
306
void
307
gfc_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;
329 378 julius
  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;
335 285 jeremybenn
 
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
 
344
int
345
gfc_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
 
385
    case OPT_idirafter:
386
      gfc_cpp_add_include_path_after (xstrdup(arg), true);
387
      break;
388
 
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
 
450 378 julius
    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
 
487 285 jeremybenn
    case OPT_P:
488
      gfc_cpp_option.no_line_commands = 1;
489
      break;
490
  }
491
 
492
  return result;
493
}
494
 
495
 
496
void
497
gfc_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 ()
503 378 julius
          || 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))
510 285 jeremybenn
    gfc_fatal_error("To enable preprocessing, use -cpp");
511
 
512
  cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
513 378 julius
  if (!gfc_cpp_enabled ())
514 285 jeremybenn
    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;
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
 
536 378 julius
  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
 
547 285 jeremybenn
  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
 
552
  gfc_cpp_register_include_paths ();
553
}
554
 
555
 
556
void
557
gfc_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;
566
  cb->error = cb_cpp_error;
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",
608
                             gfc_cpp_option.output_filename, strerror(errno));
609
        }
610
      else
611
        print.outf = stdout;
612
    }
613
  else
614
    {
615
      print.outf = fopen (gfc_cpp_option.temporary_filename, "w");
616
      if (print.outf == NULL)
617
        gfc_fatal_error ("opening output file %s: %s",
618
                         gfc_cpp_option.temporary_filename, strerror(errno));
619
    }
620
 
621
  gcc_assert(cpp_in);
622
  if (!cpp_read_main_file (cpp_in, gfc_source_file))
623
    errorcount++;
624
}
625
 
626
void
627
gfc_cpp_init (void)
628
{
629
  int i;
630
 
631
  if (gfc_option.flag_preprocessed)
632
    return;
633
 
634
  cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
635
  if (!gfc_cpp_option.no_predefined)
636
    cpp_define_builtins (cpp_in);
637
 
638
  /* Handle deferred options from command-line.  */
639
  cpp_change_file (cpp_in, LC_RENAME, _("<command-line>"));
640
 
641
  for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++)
642
    {
643
      gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i];
644
 
645
      if (opt->code == OPT_D)
646
        cpp_define (cpp_in, opt->arg);
647
      else if (opt->code == OPT_U)
648
        cpp_undef (cpp_in, opt->arg);
649
      else if (opt->code == OPT_A)
650
        {
651
          if (opt->arg[0] == '-')
652
            cpp_unassert (cpp_in, opt->arg + 1);
653
          else
654
            cpp_assert (cpp_in, opt->arg);
655
        }
656 378 julius
      else if (opt->code == OPT_MT || opt->code == OPT_MQ)
657
        deps_add_target (cpp_get_deps (cpp_in),
658
                         opt->arg, opt->code == OPT_MQ);
659 285 jeremybenn
    }
660
 
661
  if (gfc_cpp_option.working_directory
662
      && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands)
663
    pp_dir_change (cpp_in, get_src_pwd ());
664
}
665
 
666
gfc_try
667
gfc_cpp_preprocess (const char *source_file)
668
{
669
  if (!gfc_cpp_enabled ())
670
    return FAILURE;
671
 
672
  cpp_change_file (cpp_in, LC_RENAME, source_file);
673
 
674
  if (cpp_option->traditional)
675
    scan_translation_unit_trad (cpp_in);
676
  else
677
    scan_translation_unit (cpp_in);
678
 
679
  /* -dM command line option.  */
680
  if (gfc_cpp_preprocess_only () &&
681
      gfc_cpp_option.dump_macros == 'M')
682
    {
683
      putc ('\n', print.outf);
684
      cpp_forall_identifiers (cpp_in, dump_macro, NULL);
685
    }
686
 
687
  putc ('\n', print.outf);
688
 
689
  if (!gfc_cpp_preprocess_only ()
690
      || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename))
691
    fclose (print.outf);
692
 
693
  return SUCCESS;
694
}
695
 
696
void
697
gfc_cpp_done (void)
698
{
699
  if (!gfc_cpp_enabled ())
700
    return;
701
 
702 378 julius
  gcc_assert (cpp_in);
703 285 jeremybenn
 
704 378 julius
  if (gfc_cpp_makedep ())
705
    {
706
      if (gfc_cpp_option.deps_filename)
707
        {
708
          FILE *f = fopen (gfc_cpp_option.deps_filename, "w");
709
          if (f)
710
            {
711
              cpp_finish (cpp_in, f);
712
              fclose (f);
713
            }
714
          else
715
            gfc_fatal_error ("opening output file %s: %s",
716
                             gfc_cpp_option.deps_filename,
717
                             xstrerror (errno));
718
        }
719
      else
720
        cpp_finish (cpp_in, stdout);
721
    }
722 285 jeremybenn
 
723
  cpp_undef_all (cpp_in);
724
  cpp_clear_file_cache (cpp_in);
725
}
726
 
727
/* PATH must be malloc-ed and NULL-terminated.  */
728
void
729
gfc_cpp_add_include_path (char *path, bool user_supplied)
730
{
731
  /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
732
     include path. Fortran does not define any system include paths.  */
733
  int cxx_aware = 0;
734
 
735
  add_path (path, BRACKET, cxx_aware, user_supplied);
736
}
737
 
738
void
739
gfc_cpp_add_include_path_after (char *path, bool user_supplied)
740
{
741
  int cxx_aware = 0;
742
  add_path (path, AFTER, cxx_aware, user_supplied);
743
}
744
 
745
void
746
gfc_cpp_register_include_paths (void)
747
{
748
  int cxx_stdinc = 0;
749
  register_include_chains (cpp_in, gfc_cpp_option.sysroot,
750
                           gfc_cpp_option.prefix, gfc_cpp_option.multilib,
751
                           gfc_cpp_option.standard_include_paths, cxx_stdinc,
752
                           gfc_cpp_option.verbose);
753
}
754
 
755
 
756
 
757
static void scan_translation_unit_trad (cpp_reader *);
758
static void account_for_newlines (const unsigned char *, size_t);
759
static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
760
 
761
static void print_line (source_location, const char *);
762
static void maybe_print_line (source_location);
763
 
764
 
765
/* Writes out the preprocessed file, handling spacing and paste
766
   avoidance issues.  */
767
static void
768
scan_translation_unit (cpp_reader *pfile)
769
{
770
  bool avoid_paste = false;
771
 
772
  print.source = NULL;
773
  for (;;)
774
    {
775
      const cpp_token *token = cpp_get_token (pfile);
776
 
777
      if (token->type == CPP_PADDING)
778
        {
779
          avoid_paste = true;
780
          if (print.source == NULL
781
              || (!(print.source->flags & PREV_WHITE)
782
                  && token->val.source == NULL))
783
            print.source = token->val.source;
784
          continue;
785
        }
786
 
787
      if (token->type == CPP_EOF)
788
        break;
789
 
790
      /* Subtle logic to output a space if and only if necessary.  */
791
      if (avoid_paste)
792
        {
793
          if (print.source == NULL)
794
            print.source = token;
795
          if (print.source->flags & PREV_WHITE
796
              || (print.prev
797
                  && cpp_avoid_paste (pfile, print.prev, token))
798
              || (print.prev == NULL && token->type == CPP_HASH))
799
            putc (' ', print.outf);
800
        }
801
      else if (token->flags & PREV_WHITE)
802
        putc (' ', print.outf);
803
 
804
      avoid_paste = false;
805
      print.source = NULL;
806
      print.prev = token;
807
      cpp_output_token (token, print.outf);
808
 
809
      if (token->type == CPP_COMMENT)
810
        account_for_newlines (token->val.str.text, token->val.str.len);
811
    }
812
}
813
 
814
/* Adjust print.src_line for newlines embedded in output.  */
815
static void
816
account_for_newlines (const unsigned char *str, size_t len)
817
{
818
  while (len--)
819
    if (*str++ == '\n')
820
      print.src_line++;
821
}
822
 
823
/* Writes out a traditionally preprocessed file.  */
824
static void
825
scan_translation_unit_trad (cpp_reader *pfile)
826
{
827
  while (_cpp_read_logical_line_trad (pfile))
828
    {
829
      size_t len = pfile->out.cur - pfile->out.base;
830
      maybe_print_line (pfile->out.first_line);
831
      fwrite (pfile->out.base, 1, len, print.outf);
832
      print.printed = 1;
833
      if (!CPP_OPTION (pfile, discard_comments))
834
        account_for_newlines (pfile->out.base, len);
835
    }
836
}
837
 
838
/* If the token read on logical line LINE needs to be output on a
839
   different line to the current one, output the required newlines or
840
   a line marker.  */
841
static void
842
maybe_print_line (source_location src_loc)
843
{
844
  const struct line_map *map = linemap_lookup (line_table, src_loc);
845
  int src_line = SOURCE_LINE (map, src_loc);
846
 
847
  /* End the previous line of text.  */
848
  if (print.printed)
849
    {
850
      putc ('\n', print.outf);
851
      print.src_line++;
852
      print.printed = 0;
853
    }
854
 
855
  if (src_line >= print.src_line && src_line < print.src_line + 8)
856
    {
857
      while (src_line > print.src_line)
858
        {
859
          putc ('\n', print.outf);
860
          print.src_line++;
861
        }
862
    }
863
  else
864
    print_line (src_loc, "");
865
}
866
 
867
/* Output a line marker for logical line LINE.  Special flags are "1"
868
   or "2" indicating entering or leaving a file.  */
869
static void
870
print_line (source_location src_loc, const char *special_flags)
871
{
872
  /* End any previous line of text.  */
873
  if (print.printed)
874
    putc ('\n', print.outf);
875
  print.printed = 0;
876
 
877
  if (!gfc_cpp_option.no_line_commands)
878
    {
879
      const struct line_map *map = linemap_lookup (line_table, src_loc);
880
 
881
      size_t to_file_len = strlen (map->to_file);
882
      unsigned char *to_file_quoted =
883
         (unsigned char *) alloca (to_file_len * 4 + 1);
884
      unsigned char *p;
885
 
886
      print.src_line = SOURCE_LINE (map, src_loc);
887
 
888
      /* cpp_quote_string does not nul-terminate, so we have to do it
889
         ourselves.  */
890
      p = cpp_quote_string (to_file_quoted,
891
                            (const unsigned char *) map->to_file, to_file_len);
892
      *p = '\0';
893
      fprintf (print.outf, "# %u \"%s\"%s",
894
               print.src_line == 0 ? 1 : print.src_line,
895
               to_file_quoted, special_flags);
896
 
897
      if (map->sysp == 2)
898
        fputs (" 3 4", print.outf);
899
      else if (map->sysp == 1)
900
        fputs (" 3", print.outf);
901
 
902
      putc ('\n', print.outf);
903
    }
904
}
905
 
906
static void
907
cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map)
908
{
909
  const char *flags = "";
910
 
911
  if (gfc_cpp_option.no_line_commands)
912
    return;
913
 
914
  if (!map)
915
    return;
916
 
917
      if (print.first_time)
918
        {
919
          /* Avoid printing foo.i when the main file is foo.c.  */
920
          if (!cpp_get_options (cpp_in)->preprocessed)
921
            print_line (map->start_location, flags);
922
          print.first_time = 0;
923
        }
924
      else
925
        {
926
          /* Bring current file to correct line when entering a new file.  */
927
          if (map->reason == LC_ENTER)
928
            {
929
              const struct line_map *from = INCLUDED_FROM (line_table, map);
930
              maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
931
            }
932
          if (map->reason == LC_ENTER)
933
            flags = " 1";
934
          else if (map->reason == LC_LEAVE)
935
            flags = " 2";
936
          print_line (map->start_location, flags);
937
        }
938
 
939
}
940
 
941
/* Called when a line of output is started.  TOKEN is the first token
942
   of the line, and at end of file will be CPP_EOF.  */
943
static void
944
cb_line_change (cpp_reader *pfile, const cpp_token *token,
945
                int parsing_args)
946
{
947
  source_location src_loc = token->src_loc;
948
 
949
  if (token->type == CPP_EOF || parsing_args)
950
    return;
951
 
952
  maybe_print_line (src_loc);
953
  print.prev = 0;
954
  print.source = 0;
955
 
956
  /* Supply enough spaces to put this token in its original column,
957
     one space per column greater than 2, since scan_translation_unit
958
     will provide a space if PREV_WHITE.  Don't bother trying to
959
     reconstruct tabs; we can't get it right in general, and nothing
960
     ought to care.  Some things do care; the fault lies with them.  */
961
  if (!CPP_OPTION (pfile, traditional))
962
    {
963
      const struct line_map *map = linemap_lookup (line_table, src_loc);
964
      int spaces = SOURCE_COLUMN (map, src_loc) - 2;
965
      print.printed = 1;
966
 
967
      while (-- spaces >= 0)
968
        putc (' ', print.outf);
969
    }
970
}
971
 
972
static void
973
cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
974
          const cpp_string *str)
975
{
976
  maybe_print_line (line);
977
  fprintf (print.outf, "#ident %s\n", str->text);
978
  print.src_line++;
979
}
980
 
981
static void
982
cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
983
           cpp_hashnode *node ATTRIBUTE_UNUSED)
984
{
985
  maybe_print_line (line);
986
  fputs ("#define ", print.outf);
987
 
988
  /* 'D' is whole definition; 'N' is name only.  */
989
  if (gfc_cpp_option.dump_macros == 'D')
990
    fputs ((const char *) cpp_macro_definition (pfile, node),
991
           print.outf);
992
  else
993
    fputs ((const char *) NODE_NAME (node), print.outf);
994
 
995
  putc ('\n', print.outf);
996
  if (linemap_lookup (line_table, line)->to_line != 0)
997
    print.src_line++;
998
}
999
 
1000
static void
1001
cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
1002
          cpp_hashnode *node)
1003
{
1004
  maybe_print_line (line);
1005
  fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
1006
  print.src_line++;
1007
}
1008
 
1009
static void
1010
cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
1011
            const unsigned char *dir, const char *header, int angle_brackets,
1012
            const cpp_token **comments)
1013
{
1014
  maybe_print_line (line);
1015
  if (angle_brackets)
1016
    fprintf (print.outf, "#%s <%s>", dir, header);
1017
  else
1018
    fprintf (print.outf, "#%s \"%s\"", dir, header);
1019
 
1020
  if (comments != NULL)
1021
    {
1022
      while (*comments != NULL)
1023
        {
1024
          if ((*comments)->flags & PREV_WHITE)
1025
            putc (' ', print.outf);
1026
          cpp_output_token (*comments, print.outf);
1027
          ++comments;
1028
        }
1029
    }
1030
 
1031
  putc ('\n', print.outf);
1032
  print.src_line++;
1033
}
1034
 
1035
/* Dump out the hash table.  */
1036
static int
1037
dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
1038
{
1039
  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1040
    {
1041
      fputs ("#define ", print.outf);
1042
      fputs ((const char *) cpp_macro_definition (pfile, node),
1043
             print.outf);
1044
      putc ('\n', print.outf);
1045
      print.src_line++;
1046
    }
1047
 
1048
  return 1;
1049
}
1050
 
1051
static void
1052
cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
1053
                cpp_hashnode *node)
1054
{
1055
  gfc_cpp_macro_queue *q;
1056
  q = XNEW (gfc_cpp_macro_queue);
1057
  q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
1058
  q->next = cpp_define_queue;
1059
  cpp_define_queue = q;
1060
}
1061
 
1062
/* Callback from cpp_error for PFILE to print diagnostics from the
1063
   preprocessor.  The diagnostic is of type LEVEL, at location
1064
   LOCATION, with column number possibly overridden by COLUMN_OVERRIDE
1065
   if not zero; MSG is the translated message and AP the arguments.
1066
   Returns true if a diagnostic was emitted, false otherwise.  */
1067
 
1068
static bool
1069
cb_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level,
1070
              location_t location, unsigned int column_override,
1071
              const char *msg, va_list *ap)
1072
{
1073
  diagnostic_info diagnostic;
1074
  diagnostic_t dlevel;
1075
  int save_warn_system_headers = warn_system_headers;
1076
  bool ret;
1077
 
1078
  switch (level)
1079
    {
1080
    case CPP_DL_WARNING_SYSHDR:
1081
      warn_system_headers = 1;
1082
      /* Fall through.  */
1083
    case CPP_DL_WARNING:
1084
      dlevel = DK_WARNING;
1085
      break;
1086
    case CPP_DL_PEDWARN:
1087
      dlevel = DK_PEDWARN;
1088
      break;
1089
    case CPP_DL_ERROR:
1090
      dlevel = DK_ERROR;
1091
      break;
1092
    case CPP_DL_ICE:
1093
      dlevel = DK_ICE;
1094
      break;
1095
    case CPP_DL_NOTE:
1096
      dlevel = DK_NOTE;
1097
      break;
1098
    case CPP_DL_FATAL:
1099
      dlevel = DK_FATAL;
1100
      break;
1101
    default:
1102
      gcc_unreachable ();
1103
    }
1104
  diagnostic_set_info_translated (&diagnostic, msg, ap,
1105
                                  location, dlevel);
1106
  if (column_override)
1107
    diagnostic_override_column (&diagnostic, column_override);
1108
  ret = report_diagnostic (&diagnostic);
1109
  if (level == CPP_DL_WARNING_SYSHDR)
1110
    warn_system_headers = save_warn_system_headers;
1111
  return ret;
1112
}
1113
 
1114
/* Callback called when -fworking-director and -E to emit working
1115
   directory in cpp output file.  */
1116
 
1117
void
1118
pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1119
{
1120
  size_t to_file_len = strlen (dir);
1121
  unsigned char *to_file_quoted =
1122
     (unsigned char *) alloca (to_file_len * 4 + 1);
1123
  unsigned char *p;
1124
 
1125
  /* cpp_quote_string does not nul-terminate, so we have to do it ourselves.  */
1126
  p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
1127
  *p = '\0';
1128
  fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
1129
}
1130
 
1131
/* Copy a #pragma directive to the preprocessed output.  */
1132
static void
1133
cb_def_pragma (cpp_reader *pfile, source_location line)
1134
{
1135
  maybe_print_line (line);
1136
  fputs ("#pragma ", print.outf);
1137
  cpp_output_line (pfile, print.outf);
1138
  print.src_line++;
1139
}
1140
 
1141
static void
1142
cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
1143
               source_location line ATTRIBUTE_UNUSED,
1144
               cpp_hashnode *node)
1145
{
1146
  gfc_cpp_macro_queue *q;
1147
  q = XNEW (gfc_cpp_macro_queue);
1148
  q->macro = xstrdup ((const char *) NODE_NAME (node));
1149
  q->next = cpp_undefine_queue;
1150
  cpp_undefine_queue = q;
1151
}
1152
 
1153
static void
1154
dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
1155
{
1156
  gfc_cpp_macro_queue *q;
1157
 
1158
  /* End the previous line of text.  */
1159
  if (print.printed)
1160
    {
1161
      putc ('\n', print.outf);
1162
      print.src_line++;
1163
      print.printed = 0;
1164
    }
1165
 
1166
  for (q = cpp_define_queue; q;)
1167
    {
1168
      gfc_cpp_macro_queue *oq;
1169
      fputs ("#define ", print.outf);
1170
      fputs (q->macro, print.outf);
1171
      putc ('\n', print.outf);
1172
      print.src_line++;
1173
      oq = q;
1174
      q = q->next;
1175
      gfc_free (oq->macro);
1176
      gfc_free (oq);
1177
    }
1178
  cpp_define_queue = NULL;
1179
  for (q = cpp_undefine_queue; q;)
1180
    {
1181
      gfc_cpp_macro_queue *oq;
1182
      fprintf (print.outf, "#undef %s\n", q->macro);
1183
      print.src_line++;
1184
      oq = q;
1185
      q = q->next;
1186
      gfc_free (oq->macro);
1187
      gfc_free (oq);
1188
    }
1189
  cpp_undefine_queue = NULL;
1190
}
1191
 
1192
 

powered by: WebSVN 2.1.0

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