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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [libcpp/] [directives.c] - Blame information for rev 300

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

Line No. Rev Author Line
1 270 jeremybenn
/* CPP Library. (Directive handling.)
2
   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3
   1999, 2000, 2001, 2002, 2003, 2004, 2005,
4
   2007, 2008, 2009 Free Software Foundation, Inc.
5
   Contributed by Per Bothner, 1994-95.
6
   Based on CCCP program by Paul Rubin, June 1986
7
   Adapted to ANSI C, Richard Stallman, Jan 1987
8
 
9
This program is free software; you can redistribute it and/or modify it
10
under the terms of the GNU General Public License as published by the
11
Free Software Foundation; either version 3, or (at your option) any
12
later version.
13
 
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
GNU General Public License for more details.
18
 
19
You should have received a copy of the GNU General Public License
20
along with this program; see the file COPYING3.  If not see
21
<http://www.gnu.org/licenses/>.  */
22
 
23
#include "config.h"
24
#include "system.h"
25
#include "cpplib.h"
26
#include "internal.h"
27
#include "mkdeps.h"
28
#include "obstack.h"
29
 
30
/* Stack of conditionals currently in progress
31
   (including both successful and failing conditionals).  */
32
struct if_stack
33
{
34
  struct if_stack *next;
35
  linenum_type line;            /* Line where condition started.  */
36
  const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
37
  bool skip_elses;              /* Can future #else / #elif be skipped?  */
38
  bool was_skipping;            /* If were skipping on entry.  */
39
  int type;                     /* Most recent conditional for diagnostics.  */
40
};
41
 
42
/* Contains a registered pragma or pragma namespace.  */
43
typedef void (*pragma_cb) (cpp_reader *);
44
struct pragma_entry
45
{
46
  struct pragma_entry *next;
47
  const cpp_hashnode *pragma;   /* Name and length.  */
48
  bool is_nspace;
49
  bool is_internal;
50
  bool is_deferred;
51
  bool allow_expansion;
52
  union {
53
    pragma_cb handler;
54
    struct pragma_entry *space;
55
    unsigned int ident;
56
  } u;
57
};
58
 
59
/* Values for the origin field of struct directive.  KANDR directives
60
   come from traditional (K&R) C.  STDC89 directives come from the
61
   1989 C standard.  EXTENSION directives are extensions.  */
62
#define KANDR           0
63
#define STDC89          1
64
#define EXTENSION       2
65
 
66
/* Values for the flags field of struct directive.  COND indicates a
67
   conditional; IF_COND an opening conditional.  INCL means to treat
68
   "..." and <...> as q-char and h-char sequences respectively.  IN_I
69
   means this directive should be handled even if -fpreprocessed is in
70
   effect (these are the directives with callback hooks).
71
 
72
   EXPAND is set on directives that are always macro-expanded.  */
73
#define COND            (1 << 0)
74
#define IF_COND         (1 << 1)
75
#define INCL            (1 << 2)
76
#define IN_I            (1 << 3)
77
#define EXPAND          (1 << 4)
78
#define DEPRECATED      (1 << 5)
79
 
80
/* Defines one #-directive, including how to handle it.  */
81
typedef void (*directive_handler) (cpp_reader *);
82
typedef struct directive directive;
83
struct directive
84
{
85
  directive_handler handler;    /* Function to handle directive.  */
86
  const uchar *name;            /* Name of directive.  */
87
  unsigned short length;        /* Length of name.  */
88
  unsigned char origin;         /* Origin of directive.  */
89
  unsigned char flags;          /* Flags describing this directive.  */
90
};
91
 
92
/* Forward declarations.  */
93
 
94
static void skip_rest_of_line (cpp_reader *);
95
static void check_eol (cpp_reader *, bool);
96
static void start_directive (cpp_reader *);
97
static void prepare_directive_trad (cpp_reader *);
98
static void end_directive (cpp_reader *, int);
99
static void directive_diagnostics (cpp_reader *, const directive *, int);
100
static void run_directive (cpp_reader *, int, const char *, size_t);
101
static char *glue_header_name (cpp_reader *);
102
static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
103
                                  source_location *);
104
static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
105
static unsigned int read_flag (cpp_reader *, unsigned int);
106
static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
107
static void do_diagnostic (cpp_reader *, int, int);
108
static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
109
static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
110
static void do_include_common (cpp_reader *, enum include_type);
111
static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
112
                                                 const cpp_hashnode *);
113
static int count_registered_pragmas (struct pragma_entry *);
114
static char ** save_registered_pragmas (struct pragma_entry *, char **);
115
static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
116
                                           char **);
117
static void do_pragma_once (cpp_reader *);
118
static void do_pragma_poison (cpp_reader *);
119
static void do_pragma_system_header (cpp_reader *);
120
static void do_pragma_dependency (cpp_reader *);
121
static void do_linemarker (cpp_reader *);
122
static const cpp_token *get_token_no_padding (cpp_reader *);
123
static const cpp_token *get__Pragma_string (cpp_reader *);
124
static void destringize_and_run (cpp_reader *, const cpp_string *);
125
static int parse_answer (cpp_reader *, struct answer **, int, source_location);
126
static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
127
static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
128
static void handle_assertion (cpp_reader *, const char *, int);
129
static void do_pragma_push_macro (cpp_reader *);
130
static void do_pragma_pop_macro (cpp_reader *);
131
 
132
/* This is the table of directive handlers.  It is ordered by
133
   frequency of occurrence; the numbers at the end are directive
134
   counts from all the source code I have lying around (egcs and libc
135
   CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
136
   pcmcia-cs-3.0.9).  This is no longer important as directive lookup
137
   is now O(1).  All extensions other than #warning, #include_next,
138
   and #import are deprecated.  The name is where the extension
139
   appears to have come from.  */
140
 
141
#define DIRECTIVE_TABLE                                                 \
142
D(define,       T_DEFINE = 0,    KANDR,     IN_I)           /* 270554 */ \
143
D(include,      T_INCLUDE,      KANDR,     INCL | EXPAND)  /*  52262 */ \
144
D(endif,        T_ENDIF,        KANDR,     COND)           /*  45855 */ \
145
D(ifdef,        T_IFDEF,        KANDR,     COND | IF_COND) /*  22000 */ \
146
D(if,           T_IF,           KANDR, COND | IF_COND | EXPAND) /*  18162 */ \
147
D(else,         T_ELSE,         KANDR,     COND)           /*   9863 */ \
148
D(ifndef,       T_IFNDEF,       KANDR,     COND | IF_COND) /*   9675 */ \
149
D(undef,        T_UNDEF,        KANDR,     IN_I)           /*   4837 */ \
150
D(line,         T_LINE,         KANDR,     EXPAND)         /*   2465 */ \
151
D(elif,         T_ELIF,         STDC89,    COND | EXPAND)  /*    610 */ \
152
D(error,        T_ERROR,        STDC89,    0)               /*    475 */ \
153
D(pragma,       T_PRAGMA,       STDC89,    IN_I)           /*    195 */ \
154
D(warning,      T_WARNING,      EXTENSION, 0)               /*     22 */ \
155
D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND)  /*     19 */ \
156
D(ident,        T_IDENT,        EXTENSION, IN_I)           /*     11 */ \
157
D(import,       T_IMPORT,       EXTENSION, INCL | EXPAND)  /* 0 ObjC */ \
158
D(assert,       T_ASSERT,       EXTENSION, DEPRECATED)     /* 0 SVR4 */ \
159
D(unassert,     T_UNASSERT,     EXTENSION, DEPRECATED)     /* 0 SVR4 */ \
160
D(sccs,         T_SCCS,         EXTENSION, IN_I)           /* 0 SVR4? */
161
 
162
/* #sccs is synonymous with #ident.  */
163
#define do_sccs do_ident
164
 
165
/* Use the table to generate a series of prototypes, an enum for the
166
   directive names, and an array of directive handlers.  */
167
 
168
#define D(name, t, o, f) static void do_##name (cpp_reader *);
169
DIRECTIVE_TABLE
170
#undef D
171
 
172
#define D(n, tag, o, f) tag,
173
enum
174
{
175
  DIRECTIVE_TABLE
176
  N_DIRECTIVES
177
};
178
#undef D
179
 
180
#define D(name, t, origin, flags) \
181
{ do_##name, (const uchar *) #name, \
182
  sizeof #name - 1, origin, flags },
183
static const directive dtable[] =
184
{
185
DIRECTIVE_TABLE
186
};
187
#undef D
188
#undef DIRECTIVE_TABLE
189
 
190
/* Wrapper struct directive for linemarkers.
191
   The origin is more or less true - the original K+R cpp
192
   did use this notation in its preprocessed output.  */
193
static const directive linemarker_dir =
194
{
195
  do_linemarker, UC"#", 1, KANDR, IN_I
196
};
197
 
198
#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
199
 
200
/* Skip any remaining tokens in a directive.  */
201
static void
202
skip_rest_of_line (cpp_reader *pfile)
203
{
204
  /* Discard all stacked contexts.  */
205
  while (pfile->context->prev)
206
    _cpp_pop_context (pfile);
207
 
208
  /* Sweep up all tokens remaining on the line.  */
209
  if (! SEEN_EOL ())
210
    while (_cpp_lex_token (pfile)->type != CPP_EOF)
211
      ;
212
}
213
 
214
/* Ensure there are no stray tokens at the end of a directive.  If
215
   EXPAND is true, tokens macro-expanding to nothing are allowed.  */
216
static void
217
check_eol (cpp_reader *pfile, bool expand)
218
{
219
  if (! SEEN_EOL () && (expand
220
                        ? cpp_get_token (pfile)
221
                        : _cpp_lex_token (pfile))->type != CPP_EOF)
222
    cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
223
               pfile->directive->name);
224
}
225
 
226
/* Ensure there are no stray tokens other than comments at the end of
227
   a directive, and gather the comments.  */
228
static const cpp_token **
229
check_eol_return_comments (cpp_reader *pfile)
230
{
231
  size_t c;
232
  size_t capacity = 8;
233
  const cpp_token **buf;
234
 
235
  buf = XNEWVEC (const cpp_token *, capacity);
236
  c = 0;
237
  if (! SEEN_EOL ())
238
    {
239
      while (1)
240
        {
241
          const cpp_token *tok;
242
 
243
          tok = _cpp_lex_token (pfile);
244
          if (tok->type == CPP_EOF)
245
            break;
246
          if (tok->type != CPP_COMMENT)
247
            cpp_error (pfile, CPP_DL_PEDWARN,
248
                       "extra tokens at end of #%s directive",
249
                       pfile->directive->name);
250
          else
251
            {
252
              if (c + 1 >= capacity)
253
                {
254
                  capacity *= 2;
255
                  buf = XRESIZEVEC (const cpp_token *, buf, capacity);
256
                }
257
              buf[c] = tok;
258
              ++c;
259
            }
260
        }
261
    }
262
  buf[c] = NULL;
263
  return buf;
264
}
265
 
266
/* Called when entering a directive, _Pragma or command-line directive.  */
267
static void
268
start_directive (cpp_reader *pfile)
269
{
270
  /* Setup in-directive state.  */
271
  pfile->state.in_directive = 1;
272
  pfile->state.save_comments = 0;
273
  pfile->directive_result.type = CPP_PADDING;
274
 
275
  /* Some handlers need the position of the # for diagnostics.  */
276
  pfile->directive_line = pfile->line_table->highest_line;
277
}
278
 
279
/* Called when leaving a directive, _Pragma or command-line directive.  */
280
static void
281
end_directive (cpp_reader *pfile, int skip_line)
282
{
283
  if (pfile->state.in_deferred_pragma)
284
    ;
285
  else if (CPP_OPTION (pfile, traditional))
286
    {
287
      /* Revert change of prepare_directive_trad.  */
288
      pfile->state.prevent_expansion--;
289
 
290
      if (pfile->directive != &dtable[T_DEFINE])
291
        _cpp_remove_overlay (pfile);
292
    }
293
  /* We don't skip for an assembler #.  */
294
  else if (skip_line)
295
    {
296
      skip_rest_of_line (pfile);
297
      if (!pfile->keep_tokens)
298
        {
299
          pfile->cur_run = &pfile->base_run;
300
          pfile->cur_token = pfile->base_run.base;
301
        }
302
    }
303
 
304
  /* Restore state.  */
305
  pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
306
  pfile->state.in_directive = 0;
307
  pfile->state.in_expression = 0;
308
  pfile->state.angled_headers = 0;
309
  pfile->directive = 0;
310
}
311
 
312
/* Prepare to handle the directive in pfile->directive.  */
313
static void
314
prepare_directive_trad (cpp_reader *pfile)
315
{
316
  if (pfile->directive != &dtable[T_DEFINE])
317
    {
318
      bool no_expand = (pfile->directive
319
                        && ! (pfile->directive->flags & EXPAND));
320
      bool was_skipping = pfile->state.skipping;
321
 
322
      pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
323
                                    || pfile->directive == &dtable[T_ELIF]);
324
      if (pfile->state.in_expression)
325
        pfile->state.skipping = false;
326
 
327
      if (no_expand)
328
        pfile->state.prevent_expansion++;
329
      _cpp_scan_out_logical_line (pfile, NULL);
330
      if (no_expand)
331
        pfile->state.prevent_expansion--;
332
 
333
      pfile->state.skipping = was_skipping;
334
      _cpp_overlay_buffer (pfile, pfile->out.base,
335
                           pfile->out.cur - pfile->out.base);
336
    }
337
 
338
  /* Stop ISO C from expanding anything.  */
339
  pfile->state.prevent_expansion++;
340
}
341
 
342
/* Output diagnostics for a directive DIR.  INDENTED is nonzero if
343
   the '#' was indented.  */
344
static void
345
directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
346
{
347
  /* Issue -pedantic or deprecated warnings for extensions.  We let
348
     -pedantic take precedence if both are applicable.  */
349
  if (! pfile->state.skipping)
350
    {
351
      if (dir->origin == EXTENSION
352
          && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
353
          && CPP_PEDANTIC (pfile))
354
        cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
355
      else if (((dir->flags & DEPRECATED) != 0
356
                || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
357
               && CPP_OPTION (pfile, warn_deprecated))
358
        cpp_error (pfile, CPP_DL_WARNING, "#%s is a deprecated GCC extension",
359
                   dir->name);
360
    }
361
 
362
  /* Traditionally, a directive is ignored unless its # is in
363
     column 1.  Therefore in code intended to work with K+R
364
     compilers, directives added by C89 must have their #
365
     indented, and directives present in traditional C must not.
366
     This is true even of directives in skipped conditional
367
     blocks.  #elif cannot be used at all.  */
368
  if (CPP_WTRADITIONAL (pfile))
369
    {
370
      if (dir == &dtable[T_ELIF])
371
        cpp_error (pfile, CPP_DL_WARNING,
372
                   "suggest not using #elif in traditional C");
373
      else if (indented && dir->origin == KANDR)
374
        cpp_error (pfile, CPP_DL_WARNING,
375
                   "traditional C ignores #%s with the # indented",
376
                   dir->name);
377
      else if (!indented && dir->origin != KANDR)
378
        cpp_error (pfile, CPP_DL_WARNING,
379
                   "suggest hiding #%s from traditional C with an indented #",
380
                   dir->name);
381
    }
382
}
383
 
384
/* Check if we have a known directive.  INDENTED is nonzero if the
385
   '#' of the directive was indented.  This function is in this file
386
   to save unnecessarily exporting dtable etc. to lex.c.  Returns
387
   nonzero if the line of tokens has been handled, zero if we should
388
   continue processing the line.  */
389
int
390
_cpp_handle_directive (cpp_reader *pfile, int indented)
391
{
392
  const directive *dir = 0;
393
  const cpp_token *dname;
394
  bool was_parsing_args = pfile->state.parsing_args;
395
  bool was_discarding_output = pfile->state.discarding_output;
396
  int skip = 1;
397
 
398
  if (was_discarding_output)
399
    pfile->state.prevent_expansion = 0;
400
 
401
  if (was_parsing_args)
402
    {
403
      if (CPP_OPTION (pfile, pedantic))
404
        cpp_error (pfile, CPP_DL_PEDWARN,
405
             "embedding a directive within macro arguments is not portable");
406
      pfile->state.parsing_args = 0;
407
      pfile->state.prevent_expansion = 0;
408
    }
409
  start_directive (pfile);
410
  dname = _cpp_lex_token (pfile);
411
 
412
  if (dname->type == CPP_NAME)
413
    {
414
      if (dname->val.node.node->is_directive)
415
        dir = &dtable[dname->val.node.node->directive_index];
416
    }
417
  /* We do not recognize the # followed by a number extension in
418
     assembler code.  */
419
  else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
420
    {
421
      dir = &linemarker_dir;
422
      if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
423
          && ! pfile->state.skipping)
424
        cpp_error (pfile, CPP_DL_PEDWARN,
425
                   "style of line directive is a GCC extension");
426
    }
427
 
428
  if (dir)
429
    {
430
      /* If we have a directive that is not an opening conditional,
431
         invalidate any control macro.  */
432
      if (! (dir->flags & IF_COND))
433
        pfile->mi_valid = false;
434
 
435
      /* Kluge alert.  In order to be sure that code like this
436
 
437
         #define HASH #
438
         HASH define foo bar
439
 
440
         does not cause '#define foo bar' to get executed when
441
         compiled with -save-temps, we recognize directives in
442
         -fpreprocessed mode only if the # is in column 1.  macro.c
443
         puts a space in front of any '#' at the start of a macro.
444
 
445
         We exclude the -fdirectives-only case because macro expansion
446
         has not been performed yet, and block comments can cause spaces
447
         to preceed the directive.  */
448
      if (CPP_OPTION (pfile, preprocessed)
449
          && !CPP_OPTION (pfile, directives_only)
450
          && (indented || !(dir->flags & IN_I)))
451
        {
452
          skip = 0;
453
          dir = 0;
454
        }
455
      else
456
        {
457
          /* In failed conditional groups, all non-conditional
458
             directives are ignored.  Before doing that, whether
459
             skipping or not, we should lex angle-bracketed headers
460
             correctly, and maybe output some diagnostics.  */
461
          pfile->state.angled_headers = dir->flags & INCL;
462
          pfile->state.directive_wants_padding = dir->flags & INCL;
463
          if (! CPP_OPTION (pfile, preprocessed))
464
            directive_diagnostics (pfile, dir, indented);
465
          if (pfile->state.skipping && !(dir->flags & COND))
466
            dir = 0;
467
        }
468
    }
469
  else if (dname->type == CPP_EOF)
470
    ;   /* CPP_EOF is the "null directive".  */
471
  else
472
    {
473
      /* An unknown directive.  Don't complain about it in assembly
474
         source: we don't know where the comments are, and # may
475
         introduce assembler pseudo-ops.  Don't complain about invalid
476
         directives in skipped conditional groups (6.10 p4).  */
477
      if (CPP_OPTION (pfile, lang) == CLK_ASM)
478
        skip = 0;
479
      else if (!pfile->state.skipping)
480
        cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
481
                   cpp_token_as_text (pfile, dname));
482
    }
483
 
484
  pfile->directive = dir;
485
  if (CPP_OPTION (pfile, traditional))
486
    prepare_directive_trad (pfile);
487
 
488
  if (dir)
489
    pfile->directive->handler (pfile);
490
  else if (skip == 0)
491
    _cpp_backup_tokens (pfile, 1);
492
 
493
  end_directive (pfile, skip);
494
  if (was_parsing_args && !pfile->state.in_deferred_pragma)
495
    {
496
      /* Restore state when within macro args.  */
497
      pfile->state.parsing_args = 2;
498
      pfile->state.prevent_expansion = 1;
499
    }
500
  if (was_discarding_output)
501
    pfile->state.prevent_expansion = 1;
502
  return skip;
503
}
504
 
505
/* Directive handler wrapper used by the command line option
506
   processor.  BUF is \n terminated.  */
507
static void
508
run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
509
{
510
  cpp_push_buffer (pfile, (const uchar *) buf, count,
511
                   /* from_stage3 */ true);
512
  start_directive (pfile);
513
 
514
  /* This is a short-term fix to prevent a leading '#' being
515
     interpreted as a directive.  */
516
  _cpp_clean_line (pfile);
517
 
518
  pfile->directive = &dtable[dir_no];
519
  if (CPP_OPTION (pfile, traditional))
520
    prepare_directive_trad (pfile);
521
  pfile->directive->handler (pfile);
522
  end_directive (pfile, 1);
523
  _cpp_pop_buffer (pfile);
524
}
525
 
526
/* Checks for validity the macro name in #define, #undef, #ifdef and
527
   #ifndef directives.  IS_DEF_OR_UNDEF is true if this call is
528
   processing a #define or #undefine directive, and false
529
   otherwise.  */
530
static cpp_hashnode *
531
lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
532
{
533
  const cpp_token *token = _cpp_lex_token (pfile);
534
 
535
  /* The token immediately after #define must be an identifier.  That
536
     identifier may not be "defined", per C99 6.10.8p4.
537
     In C++, it may not be any of the "named operators" either,
538
     per C++98 [lex.digraph], [lex.key].
539
     Finally, the identifier may not have been poisoned.  (In that case
540
     the lexer has issued the error message for us.)  */
541
 
542
  if (token->type == CPP_NAME)
543
    {
544
      cpp_hashnode *node = token->val.node.node;
545
 
546
      if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
547
        cpp_error (pfile, CPP_DL_ERROR,
548
                   "\"defined\" cannot be used as a macro name");
549
      else if (! (node->flags & NODE_POISONED))
550
        return node;
551
    }
552
  else if (token->flags & NAMED_OP)
553
    cpp_error (pfile, CPP_DL_ERROR,
554
       "\"%s\" cannot be used as a macro name as it is an operator in C++",
555
               NODE_NAME (token->val.node.node));
556
  else if (token->type == CPP_EOF)
557
    cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
558
               pfile->directive->name);
559
  else
560
    cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
561
 
562
  return NULL;
563
}
564
 
565
/* Process a #define directive.  Most work is done in macro.c.  */
566
static void
567
do_define (cpp_reader *pfile)
568
{
569
  cpp_hashnode *node = lex_macro_node (pfile, true);
570
 
571
  if (node)
572
    {
573
      /* If we have been requested to expand comments into macros,
574
         then re-enable saving of comments.  */
575
      pfile->state.save_comments =
576
        ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
577
 
578
      if (pfile->cb.before_define)
579
        pfile->cb.before_define (pfile);
580
 
581
      if (_cpp_create_definition (pfile, node))
582
        if (pfile->cb.define)
583
          pfile->cb.define (pfile, pfile->directive_line, node);
584
 
585
      node->flags &= ~NODE_USED;
586
    }
587
}
588
 
589
/* Handle #undef.  Mark the identifier NT_VOID in the hash table.  */
590
static void
591
do_undef (cpp_reader *pfile)
592
{
593
  cpp_hashnode *node = lex_macro_node (pfile, true);
594
 
595
  if (node)
596
    {
597
      if (pfile->cb.before_define)
598
        pfile->cb.before_define (pfile);
599
 
600
      if (pfile->cb.undef)
601
        pfile->cb.undef (pfile, pfile->directive_line, node);
602
 
603
      /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
604
         identifier is not currently defined as a macro name.  */
605
      if (node->type == NT_MACRO)
606
        {
607
          if (node->flags & NODE_WARN)
608
            cpp_error (pfile, CPP_DL_WARNING,
609
                       "undefining \"%s\"", NODE_NAME (node));
610
 
611
          if (CPP_OPTION (pfile, warn_unused_macros))
612
            _cpp_warn_if_unused_macro (pfile, node, NULL);
613
 
614
          _cpp_free_definition (node);
615
        }
616
    }
617
 
618
  check_eol (pfile, false);
619
}
620
 
621
/* Undefine a single macro/assertion/whatever.  */
622
 
623
static int
624
undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
625
                 void *data_p ATTRIBUTE_UNUSED)
626
{
627
  /* Body of _cpp_free_definition inlined here for speed.
628
     Macros and assertions no longer have anything to free.  */
629
  h->type = NT_VOID;
630
  h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
631
  return 1;
632
}
633
 
634
/* Undefine all macros and assertions.  */
635
 
636
void
637
cpp_undef_all (cpp_reader *pfile)
638
{
639
  cpp_forall_identifiers (pfile, undefine_macros, NULL);
640
}
641
 
642
 
643
/* Helper routine used by parse_include.  Reinterpret the current line
644
   as an h-char-sequence (< ... >); we are looking at the first token
645
   after the <.  Returns a malloced filename.  */
646
static char *
647
glue_header_name (cpp_reader *pfile)
648
{
649
  const cpp_token *token;
650
  char *buffer;
651
  size_t len, total_len = 0, capacity = 1024;
652
 
653
  /* To avoid lexed tokens overwriting our glued name, we can only
654
     allocate from the string pool once we've lexed everything.  */
655
  buffer = XNEWVEC (char, capacity);
656
  for (;;)
657
    {
658
      token = get_token_no_padding (pfile);
659
 
660
      if (token->type == CPP_GREATER)
661
        break;
662
      if (token->type == CPP_EOF)
663
        {
664
          cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
665
          break;
666
        }
667
 
668
      len = cpp_token_len (token) + 2; /* Leading space, terminating \0.  */
669
      if (total_len + len > capacity)
670
        {
671
          capacity = (capacity + len) * 2;
672
          buffer = XRESIZEVEC (char, buffer, capacity);
673
        }
674
 
675
      if (token->flags & PREV_WHITE)
676
        buffer[total_len++] = ' ';
677
 
678
      total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
679
                                    true)
680
                   - (uchar *) buffer);
681
    }
682
 
683
  buffer[total_len] = '\0';
684
  return buffer;
685
}
686
 
687
/* Returns the file name of #include, #include_next, #import and
688
   #pragma dependency.  The string is malloced and the caller should
689
   free it.  Returns NULL on error.  LOCATION is the source location
690
   of the file name.  */
691
 
692
static const char *
693
parse_include (cpp_reader *pfile, int *pangle_brackets,
694
               const cpp_token ***buf, source_location *location)
695
{
696
  char *fname;
697
  const cpp_token *header;
698
 
699
  /* Allow macro expansion.  */
700
  header = get_token_no_padding (pfile);
701
  *location = header->src_loc;
702
  if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
703
      || header->type == CPP_HEADER_NAME)
704
    {
705
      fname = XNEWVEC (char, header->val.str.len - 1);
706
      memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
707
      fname[header->val.str.len - 2] = '\0';
708
      *pangle_brackets = header->type == CPP_HEADER_NAME;
709
    }
710
  else if (header->type == CPP_LESS)
711
    {
712
      fname = glue_header_name (pfile);
713
      *pangle_brackets = 1;
714
    }
715
  else
716
    {
717
      const unsigned char *dir;
718
 
719
      if (pfile->directive == &dtable[T_PRAGMA])
720
        dir = UC"pragma dependency";
721
      else
722
        dir = pfile->directive->name;
723
      cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
724
                 dir);
725
 
726
      return NULL;
727
    }
728
 
729
  if (pfile->directive == &dtable[T_PRAGMA])
730
    {
731
      /* This pragma allows extra tokens after the file name.  */
732
    }
733
  else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
734
    check_eol (pfile, true);
735
  else
736
    {
737
      /* If we are not discarding comments, then gather them while
738
         doing the eol check.  */
739
      *buf = check_eol_return_comments (pfile);
740
    }
741
 
742
  return fname;
743
}
744
 
745
/* Handle #include, #include_next and #import.  */
746
static void
747
do_include_common (cpp_reader *pfile, enum include_type type)
748
{
749
  const char *fname;
750
  int angle_brackets;
751
  const cpp_token **buf = NULL;
752
  source_location location;
753
 
754
  /* Re-enable saving of comments if requested, so that the include
755
     callback can dump comments which follow #include.  */
756
  pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
757
 
758
  fname = parse_include (pfile, &angle_brackets, &buf, &location);
759
  if (!fname)
760
    {
761
      if (buf)
762
        XDELETEVEC (buf);
763
      return;
764
    }
765
 
766
  if (!*fname)
767
  {
768
    cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
769
                         "empty filename in #%s",
770
                         pfile->directive->name);
771
    XDELETEVEC (fname);
772
    if (buf)
773
      XDELETEVEC (buf);
774
    return;
775
  }
776
 
777
  /* Prevent #include recursion.  */
778
  if (pfile->line_table->depth >= CPP_STACK_MAX)
779
    cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
780
  else
781
    {
782
      /* Get out of macro context, if we are.  */
783
      skip_rest_of_line (pfile);
784
 
785
      if (pfile->cb.include)
786
        pfile->cb.include (pfile, pfile->directive_line,
787
                           pfile->directive->name, fname, angle_brackets,
788
                           buf);
789
 
790
      _cpp_stack_include (pfile, fname, angle_brackets, type);
791
    }
792
 
793
  XDELETEVEC (fname);
794
  if (buf)
795
    XDELETEVEC (buf);
796
}
797
 
798
static void
799
do_include (cpp_reader *pfile)
800
{
801
  do_include_common (pfile, IT_INCLUDE);
802
}
803
 
804
static void
805
do_import (cpp_reader *pfile)
806
{
807
  do_include_common (pfile, IT_IMPORT);
808
}
809
 
810
static void
811
do_include_next (cpp_reader *pfile)
812
{
813
  enum include_type type = IT_INCLUDE_NEXT;
814
 
815
  /* If this is the primary source file, warn and use the normal
816
     search logic.  */
817
  if (cpp_in_primary_file (pfile))
818
    {
819
      cpp_error (pfile, CPP_DL_WARNING,
820
                 "#include_next in primary source file");
821
      type = IT_INCLUDE;
822
    }
823
  do_include_common (pfile, type);
824
}
825
 
826
/* Subroutine of do_linemarker.  Read possible flags after file name.
827
   LAST is the last flag seen; 0 if this is the first flag. Return the
828
   flag if it is valid, 0 at the end of the directive. Otherwise
829
   complain.  */
830
static unsigned int
831
read_flag (cpp_reader *pfile, unsigned int last)
832
{
833
  const cpp_token *token = _cpp_lex_token (pfile);
834
 
835
  if (token->type == CPP_NUMBER && token->val.str.len == 1)
836
    {
837
      unsigned int flag = token->val.str.text[0] - '0';
838
 
839
      if (flag > last && flag <= 4
840
          && (flag != 4 || last == 3)
841
          && (flag != 2 || last == 0))
842
        return flag;
843
    }
844
 
845
  if (token->type != CPP_EOF)
846
    cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
847
               cpp_token_as_text (pfile, token));
848
  return 0;
849
}
850
 
851
/* Subroutine of do_line and do_linemarker.  Convert a number in STR,
852
   of length LEN, to binary; store it in NUMP, and return false if the
853
   number was well-formed, true if not. WRAPPED is set to true if the
854
   number did not fit into 'unsigned long'.  */
855
static bool
856
strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
857
{
858
  linenum_type reg = 0;
859
  linenum_type reg_prev = 0;
860
 
861
  uchar c;
862
  *wrapped = false;
863
  while (len--)
864
    {
865
      c = *str++;
866
      if (!ISDIGIT (c))
867
        return true;
868
      reg *= 10;
869
      reg += c - '0';
870
      if (reg < reg_prev)
871
        *wrapped = true;
872
      reg_prev = reg;
873
    }
874
  *nump = reg;
875
  return false;
876
}
877
 
878
/* Interpret #line command.
879
   Note that the filename string (if any) is a true string constant
880
   (escapes are interpreted), unlike in #line.  */
881
static void
882
do_line (cpp_reader *pfile)
883
{
884
  const struct line_maps *line_table = pfile->line_table;
885
  const struct line_map *map = &line_table->maps[line_table->used - 1];
886
 
887
  /* skip_rest_of_line() may cause line table to be realloc()ed so note down
888
     sysp right now.  */
889
 
890
  unsigned char map_sysp = map->sysp;
891
  const cpp_token *token;
892
  const char *new_file = map->to_file;
893
  linenum_type new_lineno;
894
 
895
  /* C99 raised the minimum limit on #line numbers.  */
896
  linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
897
  bool wrapped;
898
 
899
  /* #line commands expand macros.  */
900
  token = cpp_get_token (pfile);
901
  if (token->type != CPP_NUMBER
902
      || strtolinenum (token->val.str.text, token->val.str.len,
903
                       &new_lineno, &wrapped))
904
    {
905
      if (token->type == CPP_EOF)
906
        cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
907
      else
908
        cpp_error (pfile, CPP_DL_ERROR,
909
                   "\"%s\" after #line is not a positive integer",
910
                   cpp_token_as_text (pfile, token));
911
      return;
912
    }
913
 
914
  if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
915
    cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
916
  else if (wrapped)
917
    cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
918
 
919
  token = cpp_get_token (pfile);
920
  if (token->type == CPP_STRING)
921
    {
922
      cpp_string s = { 0, 0 };
923
      if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
924
                                            &s, CPP_STRING))
925
        new_file = (const char *)s.text;
926
      check_eol (pfile, true);
927
    }
928
  else if (token->type != CPP_EOF)
929
    {
930
      cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
931
                 cpp_token_as_text (pfile, token));
932
      return;
933
    }
934
 
935
  skip_rest_of_line (pfile);
936
  _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
937
                       map_sysp);
938
}
939
 
940
/* Interpret the # 44 "file" [flags] notation, which has slightly
941
   different syntax and semantics from #line:  Flags are allowed,
942
   and we never complain about the line number being too big.  */
943
static void
944
do_linemarker (cpp_reader *pfile)
945
{
946
  const struct line_maps *line_table = pfile->line_table;
947
  const struct line_map *map = &line_table->maps[line_table->used - 1];
948
  const cpp_token *token;
949
  const char *new_file = map->to_file;
950
  linenum_type new_lineno;
951
  unsigned int new_sysp = map->sysp;
952
  enum lc_reason reason = LC_RENAME_VERBATIM;
953
  int flag;
954
  bool wrapped;
955
 
956
  /* Back up so we can get the number again.  Putting this in
957
     _cpp_handle_directive risks two calls to _cpp_backup_tokens in
958
     some circumstances, which can segfault.  */
959
  _cpp_backup_tokens (pfile, 1);
960
 
961
  /* #line commands expand macros.  */
962
  token = cpp_get_token (pfile);
963
  if (token->type != CPP_NUMBER
964
      || strtolinenum (token->val.str.text, token->val.str.len,
965
                       &new_lineno, &wrapped))
966
    {
967
      /* Unlike #line, there does not seem to be a way to get an EOF
968
         here.  So, it should be safe to always spell the token.  */
969
      cpp_error (pfile, CPP_DL_ERROR,
970
                 "\"%s\" after # is not a positive integer",
971
                 cpp_token_as_text (pfile, token));
972
      return;
973
    }
974
 
975
  token = cpp_get_token (pfile);
976
  if (token->type == CPP_STRING)
977
    {
978
      cpp_string s = { 0, 0 };
979
      if (cpp_interpret_string_notranslate (pfile, &token->val.str,
980
                                            1, &s, CPP_STRING))
981
        new_file = (const char *)s.text;
982
 
983
      new_sysp = 0;
984
      flag = read_flag (pfile, 0);
985
      if (flag == 1)
986
        {
987
          reason = LC_ENTER;
988
          /* Fake an include for cpp_included ().  */
989
          _cpp_fake_include (pfile, new_file);
990
          flag = read_flag (pfile, flag);
991
        }
992
      else if (flag == 2)
993
        {
994
          reason = LC_LEAVE;
995
          flag = read_flag (pfile, flag);
996
        }
997
      if (flag == 3)
998
        {
999
          new_sysp = 1;
1000
          flag = read_flag (pfile, flag);
1001
          if (flag == 4)
1002
            new_sysp = 2;
1003
        }
1004
      pfile->buffer->sysp = new_sysp;
1005
 
1006
      check_eol (pfile, false);
1007
    }
1008
  else if (token->type != CPP_EOF)
1009
    {
1010
      cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
1011
                 cpp_token_as_text (pfile, token));
1012
      return;
1013
    }
1014
 
1015
  skip_rest_of_line (pfile);
1016
 
1017
  /* Compensate for the increment in linemap_add that occurs in
1018
     _cpp_do_file_change.  We're currently at the start of the line
1019
     *following* the #line directive.  A separate source_location for this
1020
     location makes no sense (until we do the LC_LEAVE), and
1021
     complicates LAST_SOURCE_LINE_LOCATION.  */
1022
  pfile->line_table->highest_location--;
1023
 
1024
  _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
1025
}
1026
 
1027
/* Arrange the file_change callback.  pfile->line has changed to
1028
   FILE_LINE of TO_FILE, for reason REASON.  SYSP is 1 for a system
1029
   header, 2 for a system header that needs to be extern "C" protected,
1030
   and zero otherwise.  */
1031
void
1032
_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
1033
                     const char *to_file, linenum_type file_line,
1034
                     unsigned int sysp)
1035
{
1036
  const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1037
                                            to_file, file_line);
1038
  if (map != NULL)
1039
    linemap_line_start (pfile->line_table, map->to_line, 127);
1040
 
1041
  if (pfile->cb.file_change)
1042
    pfile->cb.file_change (pfile, map);
1043
}
1044
 
1045
/* Report a warning or error detected by the program we are
1046
   processing.  Use the directive's tokens in the error message.  */
1047
static void
1048
do_diagnostic (cpp_reader *pfile, int code, int print_dir)
1049
{
1050
  const unsigned char *dir_name;
1051
  unsigned char *line;
1052
  source_location src_loc = pfile->cur_token[-1].src_loc;
1053
 
1054
  if (print_dir)
1055
    dir_name = pfile->directive->name;
1056
  else
1057
    dir_name = NULL;
1058
  pfile->state.prevent_expansion++;
1059
  line = cpp_output_line_to_string (pfile, dir_name);
1060
  pfile->state.prevent_expansion--;
1061
 
1062
  cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
1063
  free (line);
1064
}
1065
 
1066
static void
1067
do_error (cpp_reader *pfile)
1068
{
1069
  do_diagnostic (pfile, CPP_DL_ERROR, 1);
1070
}
1071
 
1072
static void
1073
do_warning (cpp_reader *pfile)
1074
{
1075
  /* We want #warning diagnostics to be emitted in system headers too.  */
1076
  do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
1077
}
1078
 
1079
/* Report program identification.  */
1080
static void
1081
do_ident (cpp_reader *pfile)
1082
{
1083
  const cpp_token *str = cpp_get_token (pfile);
1084
 
1085
  if (str->type != CPP_STRING)
1086
    cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1087
               pfile->directive->name);
1088
  else if (pfile->cb.ident)
1089
    pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1090
 
1091
  check_eol (pfile, false);
1092
}
1093
 
1094
/* Lookup a PRAGMA name in a singly-linked CHAIN.  Returns the
1095
   matching entry, or NULL if none is found.  The returned entry could
1096
   be the start of a namespace chain, or a pragma.  */
1097
static struct pragma_entry *
1098
lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1099
{
1100
  while (chain && chain->pragma != pragma)
1101
    chain = chain->next;
1102
 
1103
  return chain;
1104
}
1105
 
1106
/* Create and insert a blank pragma entry at the beginning of a
1107
   singly-linked CHAIN.  */
1108
static struct pragma_entry *
1109
new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1110
{
1111
  struct pragma_entry *new_entry;
1112
 
1113
  new_entry = (struct pragma_entry *)
1114
    _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1115
 
1116
  memset (new_entry, 0, sizeof (struct pragma_entry));
1117
  new_entry->next = *chain;
1118
 
1119
  *chain = new_entry;
1120
  return new_entry;
1121
}
1122
 
1123
/* Register a pragma NAME in namespace SPACE.  If SPACE is null, it
1124
   goes in the global namespace.  */
1125
static struct pragma_entry *
1126
register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1127
                   bool allow_name_expansion)
1128
{
1129
  struct pragma_entry **chain = &pfile->pragmas;
1130
  struct pragma_entry *entry;
1131
  const cpp_hashnode *node;
1132
 
1133
  if (space)
1134
    {
1135
      node = cpp_lookup (pfile, UC space, strlen (space));
1136
      entry = lookup_pragma_entry (*chain, node);
1137
      if (!entry)
1138
        {
1139
          entry = new_pragma_entry (pfile, chain);
1140
          entry->pragma = node;
1141
          entry->is_nspace = true;
1142
          entry->allow_expansion = allow_name_expansion;
1143
        }
1144
      else if (!entry->is_nspace)
1145
        goto clash;
1146
      else if (entry->allow_expansion != allow_name_expansion)
1147
        {
1148
          cpp_error (pfile, CPP_DL_ICE,
1149
                     "registering pragmas in namespace \"%s\" with mismatched "
1150
                     "name expansion", space);
1151
          return NULL;
1152
        }
1153
      chain = &entry->u.space;
1154
    }
1155
  else if (allow_name_expansion)
1156
    {
1157
      cpp_error (pfile, CPP_DL_ICE,
1158
                 "registering pragma \"%s\" with name expansion "
1159
                 "and no namespace", name);
1160
      return NULL;
1161
    }
1162
 
1163
  /* Check for duplicates.  */
1164
  node = cpp_lookup (pfile, UC name, strlen (name));
1165
  entry = lookup_pragma_entry (*chain, node);
1166
  if (entry == NULL)
1167
    {
1168
      entry = new_pragma_entry (pfile, chain);
1169
      entry->pragma = node;
1170
      return entry;
1171
    }
1172
 
1173
  if (entry->is_nspace)
1174
    clash:
1175
    cpp_error (pfile, CPP_DL_ICE,
1176
               "registering \"%s\" as both a pragma and a pragma namespace",
1177
               NODE_NAME (node));
1178
  else if (space)
1179
    cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1180
               space, name);
1181
  else
1182
    cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1183
 
1184
  return NULL;
1185
}
1186
 
1187
/* Register a cpplib internal pragma SPACE NAME with HANDLER.  */
1188
static void
1189
register_pragma_internal (cpp_reader *pfile, const char *space,
1190
                          const char *name, pragma_cb handler)
1191
{
1192
  struct pragma_entry *entry;
1193
 
1194
  entry = register_pragma_1 (pfile, space, name, false);
1195
  entry->is_internal = true;
1196
  entry->u.handler = handler;
1197
}
1198
 
1199
/* Register a pragma NAME in namespace SPACE.  If SPACE is null, it
1200
   goes in the global namespace.  HANDLER is the handler it will call,
1201
   which must be non-NULL.  If ALLOW_EXPANSION is set, allow macro
1202
   expansion while parsing pragma NAME.  This function is exported
1203
   from libcpp. */
1204
void
1205
cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1206
                     pragma_cb handler, bool allow_expansion)
1207
{
1208
  struct pragma_entry *entry;
1209
 
1210
  if (!handler)
1211
    {
1212
      cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1213
      return;
1214
    }
1215
 
1216
  entry = register_pragma_1 (pfile, space, name, false);
1217
  if (entry)
1218
    {
1219
      entry->allow_expansion = allow_expansion;
1220
      entry->u.handler = handler;
1221
    }
1222
}
1223
 
1224
/* Similarly, but create mark the pragma for deferred processing.
1225
   When found, a CPP_PRAGMA token will be insertted into the stream
1226
   with IDENT in the token->u.pragma slot.  */
1227
void
1228
cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1229
                              const char *name, unsigned int ident,
1230
                              bool allow_expansion, bool allow_name_expansion)
1231
{
1232
  struct pragma_entry *entry;
1233
 
1234
  entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1235
  if (entry)
1236
    {
1237
      entry->is_deferred = true;
1238
      entry->allow_expansion = allow_expansion;
1239
      entry->u.ident = ident;
1240
    }
1241
}
1242
 
1243
/* Register the pragmas the preprocessor itself handles.  */
1244
void
1245
_cpp_init_internal_pragmas (cpp_reader *pfile)
1246
{
1247
  /* Pragmas in the global namespace.  */
1248
  register_pragma_internal (pfile, 0, "once", do_pragma_once);
1249
  register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro);
1250
  register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro);
1251
 
1252
  /* New GCC-specific pragmas should be put in the GCC namespace.  */
1253
  register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1254
  register_pragma_internal (pfile, "GCC", "system_header",
1255
                            do_pragma_system_header);
1256
  register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1257
}
1258
 
1259
/* Return the number of registered pragmas in PE.  */
1260
 
1261
static int
1262
count_registered_pragmas (struct pragma_entry *pe)
1263
{
1264
  int ct = 0;
1265
  for (; pe != NULL; pe = pe->next)
1266
    {
1267
      if (pe->is_nspace)
1268
        ct += count_registered_pragmas (pe->u.space);
1269
      ct++;
1270
    }
1271
  return ct;
1272
}
1273
 
1274
/* Save into SD the names of the registered pragmas referenced by PE,
1275
   and return a pointer to the next free space in SD.  */
1276
 
1277
static char **
1278
save_registered_pragmas (struct pragma_entry *pe, char **sd)
1279
{
1280
  for (; pe != NULL; pe = pe->next)
1281
    {
1282
      if (pe->is_nspace)
1283
        sd = save_registered_pragmas (pe->u.space, sd);
1284
      *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1285
                                HT_LEN (&pe->pragma->ident),
1286
                                HT_LEN (&pe->pragma->ident) + 1);
1287
    }
1288
  return sd;
1289
}
1290
 
1291
/* Return a newly-allocated array which saves the names of the
1292
   registered pragmas.  */
1293
 
1294
char **
1295
_cpp_save_pragma_names (cpp_reader *pfile)
1296
{
1297
  int ct = count_registered_pragmas (pfile->pragmas);
1298
  char **result = XNEWVEC (char *, ct);
1299
  (void) save_registered_pragmas (pfile->pragmas, result);
1300
  return result;
1301
}
1302
 
1303
/* Restore from SD the names of the registered pragmas referenced by PE,
1304
   and return a pointer to the next unused name in SD.  */
1305
 
1306
static char **
1307
restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1308
                            char **sd)
1309
{
1310
  for (; pe != NULL; pe = pe->next)
1311
    {
1312
      if (pe->is_nspace)
1313
        sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1314
      pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
1315
      free (*sd);
1316
      sd++;
1317
    }
1318
  return sd;
1319
}
1320
 
1321
/* Restore the names of the registered pragmas from SAVED.  */
1322
 
1323
void
1324
_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1325
{
1326
  (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1327
  free (saved);
1328
}
1329
 
1330
/* Pragmata handling.  We handle some, and pass the rest on to the
1331
   front end.  C99 defines three pragmas and says that no macro
1332
   expansion is to be performed on them; whether or not macro
1333
   expansion happens for other pragmas is implementation defined.
1334
   This implementation allows for a mix of both, since GCC did not
1335
   traditionally macro expand its (few) pragmas, whereas OpenMP
1336
   specifies that macro expansion should happen.  */
1337
static void
1338
do_pragma (cpp_reader *pfile)
1339
{
1340
  const struct pragma_entry *p = NULL;
1341
  const cpp_token *token, *pragma_token = pfile->cur_token;
1342
  cpp_token ns_token;
1343
  unsigned int count = 1;
1344
 
1345
  pfile->state.prevent_expansion++;
1346
 
1347
  token = cpp_get_token (pfile);
1348
  ns_token = *token;
1349
  if (token->type == CPP_NAME)
1350
    {
1351
      p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
1352
      if (p && p->is_nspace)
1353
        {
1354
          bool allow_name_expansion = p->allow_expansion;
1355
          if (allow_name_expansion)
1356
            pfile->state.prevent_expansion--;
1357
          token = cpp_get_token (pfile);
1358
          if (token->type == CPP_NAME)
1359
            p = lookup_pragma_entry (p->u.space, token->val.node.node);
1360
          else
1361
            p = NULL;
1362
          if (allow_name_expansion)
1363
            pfile->state.prevent_expansion++;
1364
          count = 2;
1365
        }
1366
    }
1367
 
1368
  if (p)
1369
    {
1370
      if (p->is_deferred)
1371
        {
1372
          pfile->directive_result.src_loc = pragma_token->src_loc;
1373
          pfile->directive_result.type = CPP_PRAGMA;
1374
          pfile->directive_result.flags = pragma_token->flags;
1375
          pfile->directive_result.val.pragma = p->u.ident;
1376
          pfile->state.in_deferred_pragma = true;
1377
          pfile->state.pragma_allow_expansion = p->allow_expansion;
1378
          if (!p->allow_expansion)
1379
            pfile->state.prevent_expansion++;
1380
        }
1381
      else
1382
        {
1383
          /* Since the handler below doesn't get the line number, that
1384
             it might need for diagnostics, make sure it has the right
1385
             numbers in place.  */
1386
          if (pfile->cb.line_change)
1387
            (*pfile->cb.line_change) (pfile, pragma_token, false);
1388
          if (p->allow_expansion)
1389
            pfile->state.prevent_expansion--;
1390
          (*p->u.handler) (pfile);
1391
          if (p->allow_expansion)
1392
            pfile->state.prevent_expansion++;
1393
        }
1394
    }
1395
  else if (pfile->cb.def_pragma)
1396
    {
1397
      if (count == 1 || pfile->context->prev == NULL)
1398
        _cpp_backup_tokens (pfile, count);
1399
      else
1400
        {
1401
          /* Invalid name comes from macro expansion, _cpp_backup_tokens
1402
             won't allow backing 2 tokens.  */
1403
          /* ??? The token buffer is leaked.  Perhaps if def_pragma hook
1404
             reads both tokens, we could perhaps free it, but if it doesn't,
1405
             we don't know the exact lifespan.  */
1406
          cpp_token *toks = XNEWVEC (cpp_token, 2);
1407
          toks[0] = ns_token;
1408
          toks[0].flags |= NO_EXPAND;
1409
          toks[1] = *token;
1410
          toks[1].flags |= NO_EXPAND;
1411
          _cpp_push_token_context (pfile, NULL, toks, 2);
1412
        }
1413
      pfile->cb.def_pragma (pfile, pfile->directive_line);
1414
    }
1415
 
1416
  pfile->state.prevent_expansion--;
1417
}
1418
 
1419
/* Handle #pragma once.  */
1420
static void
1421
do_pragma_once (cpp_reader *pfile)
1422
{
1423
  if (cpp_in_primary_file (pfile))
1424
    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1425
 
1426
  check_eol (pfile, false);
1427
  _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1428
}
1429
 
1430
/* Handle #pragma push_macro(STRING).  */
1431
static void
1432
do_pragma_push_macro (cpp_reader *pfile)
1433
{
1434
  char *macroname, *dest;
1435
  const char *limit, *src;
1436
  const cpp_token *txt;
1437
  struct def_pragma_macro *c;
1438
 
1439
  txt = get__Pragma_string (pfile);
1440
  if (!txt)
1441
    {
1442
      source_location src_loc = pfile->cur_token[-1].src_loc;
1443
      cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1444
                 "invalid #pragma push_macro directive");
1445
      check_eol (pfile, false);
1446
      skip_rest_of_line (pfile);
1447
      return;
1448
    }
1449
  dest = macroname = (char *) alloca (txt->val.str.len + 2);
1450
  src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1451
  limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1452
  while (src < limit)
1453
    {
1454
      /* We know there is a character following the backslash.  */
1455
      if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1456
        src++;
1457
      *dest++ = *src++;
1458
    }
1459
  *dest = 0;
1460
  check_eol (pfile, false);
1461
  skip_rest_of_line (pfile);
1462
  c = XNEW (struct def_pragma_macro);
1463
  c->name = XNEWVAR (char, strlen (macroname) + 1);
1464
  strcpy (c->name, macroname);
1465
  c->next = pfile->pushed_macros;
1466
  c->value = cpp_push_definition (pfile, c->name);
1467
  pfile->pushed_macros = c;
1468
}
1469
 
1470
/* Handle #pragma pop_macro(STRING).  */
1471
static void
1472
do_pragma_pop_macro (cpp_reader *pfile)
1473
{
1474
  char *macroname, *dest;
1475
  const char *limit, *src;
1476
  const cpp_token *txt;
1477
  struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros;
1478
  txt = get__Pragma_string (pfile);
1479
  if (!txt)
1480
    {
1481
      source_location src_loc = pfile->cur_token[-1].src_loc;
1482
      cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1483
                 "invalid #pragma pop_macro directive");
1484
      check_eol (pfile, false);
1485
      skip_rest_of_line (pfile);
1486
      return;
1487
    }
1488
  dest = macroname = (char *) alloca (txt->val.str.len + 2);
1489
  src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1490
  limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1491
  while (src < limit)
1492
    {
1493
      /* We know there is a character following the backslash.  */
1494
      if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1495
        src++;
1496
      *dest++ = *src++;
1497
    }
1498
  *dest = 0;
1499
  check_eol (pfile, false);
1500
  skip_rest_of_line (pfile);
1501
 
1502
  while (c != NULL)
1503
    {
1504
      if (!strcmp (c->name, macroname))
1505
        {
1506
          if (!l)
1507
            pfile->pushed_macros = c->next;
1508
          else
1509
            l->next = c->next;
1510
          cpp_pop_definition (pfile, c->name, c->value);
1511
          free (c->name);
1512
          free (c);
1513
          break;
1514
        }
1515
      l = c;
1516
      c = c->next;
1517
    }
1518
}
1519
 
1520
/* Handle #pragma GCC poison, to poison one or more identifiers so
1521
   that the lexer produces a hard error for each subsequent usage.  */
1522
static void
1523
do_pragma_poison (cpp_reader *pfile)
1524
{
1525
  const cpp_token *tok;
1526
  cpp_hashnode *hp;
1527
 
1528
  pfile->state.poisoned_ok = 1;
1529
  for (;;)
1530
    {
1531
      tok = _cpp_lex_token (pfile);
1532
      if (tok->type == CPP_EOF)
1533
        break;
1534
      if (tok->type != CPP_NAME)
1535
        {
1536
          cpp_error (pfile, CPP_DL_ERROR,
1537
                     "invalid #pragma GCC poison directive");
1538
          break;
1539
        }
1540
 
1541
      hp = tok->val.node.node;
1542
      if (hp->flags & NODE_POISONED)
1543
        continue;
1544
 
1545
      if (hp->type == NT_MACRO)
1546
        cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1547
                   NODE_NAME (hp));
1548
      _cpp_free_definition (hp);
1549
      hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1550
    }
1551
  pfile->state.poisoned_ok = 0;
1552
}
1553
 
1554
/* Mark the current header as a system header.  This will suppress
1555
   some categories of warnings (notably those from -pedantic).  It is
1556
   intended for use in system libraries that cannot be implemented in
1557
   conforming C, but cannot be certain that their headers appear in a
1558
   system include directory.  To prevent abuse, it is rejected in the
1559
   primary source file.  */
1560
static void
1561
do_pragma_system_header (cpp_reader *pfile)
1562
{
1563
  if (cpp_in_primary_file (pfile))
1564
    cpp_error (pfile, CPP_DL_WARNING,
1565
               "#pragma system_header ignored outside include file");
1566
  else
1567
    {
1568
      check_eol (pfile, false);
1569
      skip_rest_of_line (pfile);
1570
      cpp_make_system_header (pfile, 1, 0);
1571
    }
1572
}
1573
 
1574
/* Check the modified date of the current include file against a specified
1575
   file. Issue a diagnostic, if the specified file is newer. We use this to
1576
   determine if a fixed header should be refixed.  */
1577
static void
1578
do_pragma_dependency (cpp_reader *pfile)
1579
{
1580
  const char *fname;
1581
  int angle_brackets, ordering;
1582
  source_location location;
1583
 
1584
  fname = parse_include (pfile, &angle_brackets, NULL, &location);
1585
  if (!fname)
1586
    return;
1587
 
1588
  ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1589
  if (ordering < 0)
1590
    cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1591
  else if (ordering > 0)
1592
    {
1593
      cpp_error (pfile, CPP_DL_WARNING,
1594
                 "current file is older than %s", fname);
1595
      if (cpp_get_token (pfile)->type != CPP_EOF)
1596
        {
1597
          _cpp_backup_tokens (pfile, 1);
1598
          do_diagnostic (pfile, CPP_DL_WARNING, 0);
1599
        }
1600
    }
1601
 
1602
  free ((void *) fname);
1603
}
1604
 
1605
/* Get a token but skip padding.  */
1606
static const cpp_token *
1607
get_token_no_padding (cpp_reader *pfile)
1608
{
1609
  for (;;)
1610
    {
1611
      const cpp_token *result = cpp_get_token (pfile);
1612
      if (result->type != CPP_PADDING)
1613
        return result;
1614
    }
1615
}
1616
 
1617
/* Check syntax is "(string-literal)".  Returns the string on success,
1618
   or NULL on failure.  */
1619
static const cpp_token *
1620
get__Pragma_string (cpp_reader *pfile)
1621
{
1622
  const cpp_token *string;
1623
  const cpp_token *paren;
1624
 
1625
  paren = get_token_no_padding (pfile);
1626
  if (paren->type == CPP_EOF)
1627
    _cpp_backup_tokens (pfile, 1);
1628
  if (paren->type != CPP_OPEN_PAREN)
1629
    return NULL;
1630
 
1631
  string = get_token_no_padding (pfile);
1632
  if (string->type == CPP_EOF)
1633
    _cpp_backup_tokens (pfile, 1);
1634
  if (string->type != CPP_STRING && string->type != CPP_WSTRING
1635
      && string->type != CPP_STRING32 && string->type != CPP_STRING16
1636
      && string->type != CPP_UTF8STRING)
1637
    return NULL;
1638
 
1639
  paren = get_token_no_padding (pfile);
1640
  if (paren->type == CPP_EOF)
1641
    _cpp_backup_tokens (pfile, 1);
1642
  if (paren->type != CPP_CLOSE_PAREN)
1643
    return NULL;
1644
 
1645
  return string;
1646
}
1647
 
1648
/* Destringize IN into a temporary buffer, by removing the first \ of
1649
   \" and \\ sequences, and process the result as a #pragma directive.  */
1650
static void
1651
destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1652
{
1653
  const unsigned char *src, *limit;
1654
  char *dest, *result;
1655
  cpp_context *saved_context;
1656
  cpp_token *saved_cur_token;
1657
  tokenrun *saved_cur_run;
1658
  cpp_token *toks;
1659
  int count;
1660
  const struct directive *save_directive;
1661
 
1662
  dest = result = (char *) alloca (in->len - 1);
1663
  src = in->text + 1 + (in->text[0] == 'L');
1664
  limit = in->text + in->len - 1;
1665
  while (src < limit)
1666
    {
1667
      /* We know there is a character following the backslash.  */
1668
      if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1669
        src++;
1670
      *dest++ = *src++;
1671
    }
1672
  *dest = '\n';
1673
 
1674
  /* Ugh; an awful kludge.  We are really not set up to be lexing
1675
     tokens when in the middle of a macro expansion.  Use a new
1676
     context to force cpp_get_token to lex, and so skip_rest_of_line
1677
     doesn't go beyond the end of the text.  Also, remember the
1678
     current lexing position so we can return to it later.
1679
 
1680
     Something like line-at-a-time lexing should remove the need for
1681
     this.  */
1682
  saved_context = pfile->context;
1683
  saved_cur_token = pfile->cur_token;
1684
  saved_cur_run = pfile->cur_run;
1685
 
1686
  pfile->context = XNEW (cpp_context);
1687
  pfile->context->macro = 0;
1688
  pfile->context->prev = 0;
1689
  pfile->context->next = 0;
1690
 
1691
  /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1692
     until we've read all of the tokens that we want.  */
1693
  cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1694
                   /* from_stage3 */ true);
1695
  /* ??? Antique Disgusting Hack.  What does this do?  */
1696
  if (pfile->buffer->prev)
1697
    pfile->buffer->file = pfile->buffer->prev->file;
1698
 
1699
  start_directive (pfile);
1700
  _cpp_clean_line (pfile);
1701
  save_directive = pfile->directive;
1702
  pfile->directive = &dtable[T_PRAGMA];
1703
  do_pragma (pfile);
1704
  end_directive (pfile, 1);
1705
  pfile->directive = save_directive;
1706
 
1707
  /* We always insert at least one token, the directive result.  It'll
1708
     either be a CPP_PADDING or a CPP_PRAGMA.  In the later case, we
1709
     need to insert *all* of the tokens, including the CPP_PRAGMA_EOL.  */
1710
 
1711
  /* If we're not handling the pragma internally, read all of the tokens from
1712
     the string buffer now, while the string buffer is still installed.  */
1713
  /* ??? Note that the token buffer allocated here is leaked.  It's not clear
1714
     to me what the true lifespan of the tokens are.  It would appear that
1715
     the lifespan is the entire parse of the main input stream, in which case
1716
     this may not be wrong.  */
1717
  if (pfile->directive_result.type == CPP_PRAGMA)
1718
    {
1719
      int maxcount;
1720
 
1721
      count = 1;
1722
      maxcount = 50;
1723
      toks = XNEWVEC (cpp_token, maxcount);
1724
      toks[0] = pfile->directive_result;
1725
 
1726
      do
1727
        {
1728
          if (count == maxcount)
1729
            {
1730
              maxcount = maxcount * 3 / 2;
1731
              toks = XRESIZEVEC (cpp_token, toks, maxcount);
1732
            }
1733
          toks[count] = *cpp_get_token (pfile);
1734
          /* Macros have been already expanded by cpp_get_token
1735
             if the pragma allowed expansion.  */
1736
          toks[count++].flags |= NO_EXPAND;
1737
        }
1738
      while (toks[count-1].type != CPP_PRAGMA_EOL);
1739
    }
1740
  else
1741
    {
1742
      count = 1;
1743
      toks = XNEW (cpp_token);
1744
      toks[0] = pfile->directive_result;
1745
 
1746
      /* If we handled the entire pragma internally, make sure we get the
1747
         line number correct for the next token.  */
1748
      if (pfile->cb.line_change)
1749
        pfile->cb.line_change (pfile, pfile->cur_token, false);
1750
    }
1751
 
1752
  /* Finish inlining run_directive.  */
1753
  pfile->buffer->file = NULL;
1754
  _cpp_pop_buffer (pfile);
1755
 
1756
  /* Reset the old macro state before ...  */
1757
  XDELETE (pfile->context);
1758
  pfile->context = saved_context;
1759
  pfile->cur_token = saved_cur_token;
1760
  pfile->cur_run = saved_cur_run;
1761
 
1762
  /* ... inserting the new tokens we collected.  */
1763
  _cpp_push_token_context (pfile, NULL, toks, count);
1764
}
1765
 
1766
/* Handle the _Pragma operator.  Return 0 on error, 1 if ok.  */
1767
int
1768
_cpp_do__Pragma (cpp_reader *pfile)
1769
{
1770
  const cpp_token *string = get__Pragma_string (pfile);
1771
  pfile->directive_result.type = CPP_PADDING;
1772
 
1773
  if (string)
1774
    {
1775
      destringize_and_run (pfile, &string->val.str);
1776
      return 1;
1777
    }
1778
  cpp_error (pfile, CPP_DL_ERROR,
1779
             "_Pragma takes a parenthesized string literal");
1780
  return 0;
1781
}
1782
 
1783
/* Handle #ifdef.  */
1784
static void
1785
do_ifdef (cpp_reader *pfile)
1786
{
1787
  int skip = 1;
1788
 
1789
  if (! pfile->state.skipping)
1790
    {
1791
      cpp_hashnode *node = lex_macro_node (pfile, false);
1792
 
1793
      if (node)
1794
        {
1795
          skip = node->type != NT_MACRO;
1796
          _cpp_mark_macro_used (node);
1797
          if (!(node->flags & NODE_USED))
1798
            {
1799
              node->flags |= NODE_USED;
1800
              if (node->type == NT_MACRO)
1801
                {
1802
                  if (pfile->cb.used_define)
1803
                    pfile->cb.used_define (pfile, pfile->directive_line, node);
1804
                }
1805
              else
1806
                {
1807
                  if (pfile->cb.used_undef)
1808
                    pfile->cb.used_undef (pfile, pfile->directive_line, node);
1809
                }
1810
            }
1811
          if (pfile->cb.used)
1812
            pfile->cb.used (pfile, pfile->directive_line, node);
1813
          check_eol (pfile, false);
1814
        }
1815
    }
1816
 
1817
  push_conditional (pfile, skip, T_IFDEF, 0);
1818
}
1819
 
1820
/* Handle #ifndef.  */
1821
static void
1822
do_ifndef (cpp_reader *pfile)
1823
{
1824
  int skip = 1;
1825
  cpp_hashnode *node = 0;
1826
 
1827
  if (! pfile->state.skipping)
1828
    {
1829
      node = lex_macro_node (pfile, false);
1830
 
1831
      if (node)
1832
        {
1833
          skip = node->type == NT_MACRO;
1834
          _cpp_mark_macro_used (node);
1835
          if (!(node->flags & NODE_USED))
1836
            {
1837
              node->flags |= NODE_USED;
1838
              if (node->type == NT_MACRO)
1839
                {
1840
                  if (pfile->cb.used_define)
1841
                    pfile->cb.used_define (pfile, pfile->directive_line, node);
1842
                }
1843
              else
1844
                {
1845
                  if (pfile->cb.used_undef)
1846
                    pfile->cb.used_undef (pfile, pfile->directive_line, node);
1847
                }
1848
            }
1849
          if (pfile->cb.used)
1850
            pfile->cb.used (pfile, pfile->directive_line, node);
1851
          check_eol (pfile, false);
1852
        }
1853
    }
1854
 
1855
  push_conditional (pfile, skip, T_IFNDEF, node);
1856
}
1857
 
1858
/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1859
   pfile->mi_ind_cmacro so we can handle multiple-include
1860
   optimizations.  If macro expansion occurs in the expression, we
1861
   cannot treat it as a controlling conditional, since the expansion
1862
   could change in the future.  That is handled by cpp_get_token.  */
1863
static void
1864
do_if (cpp_reader *pfile)
1865
{
1866
  int skip = 1;
1867
 
1868
  if (! pfile->state.skipping)
1869
    skip = _cpp_parse_expr (pfile, true) == false;
1870
 
1871
  push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1872
}
1873
 
1874
/* Flip skipping state if appropriate and continue without changing
1875
   if_stack; this is so that the error message for missing #endif's
1876
   etc. will point to the original #if.  */
1877
static void
1878
do_else (cpp_reader *pfile)
1879
{
1880
  cpp_buffer *buffer = pfile->buffer;
1881
  struct if_stack *ifs = buffer->if_stack;
1882
 
1883
  if (ifs == NULL)
1884
    cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1885
  else
1886
    {
1887
      if (ifs->type == T_ELSE)
1888
        {
1889
          cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1890
          cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1891
                               "the conditional began here");
1892
        }
1893
      ifs->type = T_ELSE;
1894
 
1895
      /* Skip any future (erroneous) #elses or #elifs.  */
1896
      pfile->state.skipping = ifs->skip_elses;
1897
      ifs->skip_elses = true;
1898
 
1899
      /* Invalidate any controlling macro.  */
1900
      ifs->mi_cmacro = 0;
1901
 
1902
      /* Only check EOL if was not originally skipping.  */
1903
      if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1904
        check_eol (pfile, false);
1905
    }
1906
}
1907
 
1908
/* Handle a #elif directive by not changing if_stack either.  See the
1909
   comment above do_else.  */
1910
static void
1911
do_elif (cpp_reader *pfile)
1912
{
1913
  cpp_buffer *buffer = pfile->buffer;
1914
  struct if_stack *ifs = buffer->if_stack;
1915
 
1916
  if (ifs == NULL)
1917
    cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1918
  else
1919
    {
1920
      if (ifs->type == T_ELSE)
1921
        {
1922
          cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1923
          cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1924
                               "the conditional began here");
1925
        }
1926
      ifs->type = T_ELIF;
1927
 
1928
      if (! ifs->was_skipping)
1929
        {
1930
          bool value;
1931
          /* The standard mandates that the expression be parsed even
1932
             if we are skipping elses at this point -- the lexical
1933
             restrictions on #elif only apply to skipped groups, but
1934
             this group is not being skipped.  Temporarily set
1935
             skipping to false to get lexer warnings.  */
1936
          pfile->state.skipping = 0;
1937
          value = _cpp_parse_expr (pfile, false);
1938
          if (ifs->skip_elses)
1939
            pfile->state.skipping = 1;
1940
          else
1941
            {
1942
              pfile->state.skipping = ! value;
1943
              ifs->skip_elses = value;
1944
            }
1945
        }
1946
 
1947
      /* Invalidate any controlling macro.  */
1948
      ifs->mi_cmacro = 0;
1949
    }
1950
}
1951
 
1952
/* #endif pops the if stack and resets pfile->state.skipping.  */
1953
static void
1954
do_endif (cpp_reader *pfile)
1955
{
1956
  cpp_buffer *buffer = pfile->buffer;
1957
  struct if_stack *ifs = buffer->if_stack;
1958
 
1959
  if (ifs == NULL)
1960
    cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1961
  else
1962
    {
1963
      /* Only check EOL if was not originally skipping.  */
1964
      if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1965
        check_eol (pfile, false);
1966
 
1967
      /* If potential control macro, we go back outside again.  */
1968
      if (ifs->next == 0 && ifs->mi_cmacro)
1969
        {
1970
          pfile->mi_valid = true;
1971
          pfile->mi_cmacro = ifs->mi_cmacro;
1972
        }
1973
 
1974
      buffer->if_stack = ifs->next;
1975
      pfile->state.skipping = ifs->was_skipping;
1976
      obstack_free (&pfile->buffer_ob, ifs);
1977
    }
1978
}
1979
 
1980
/* Push an if_stack entry for a preprocessor conditional, and set
1981
   pfile->state.skipping to SKIP.  If TYPE indicates the conditional
1982
   is #if or #ifndef, CMACRO is a potentially controlling macro, and
1983
   we need to check here that we are at the top of the file.  */
1984
static void
1985
push_conditional (cpp_reader *pfile, int skip, int type,
1986
                  const cpp_hashnode *cmacro)
1987
{
1988
  struct if_stack *ifs;
1989
  cpp_buffer *buffer = pfile->buffer;
1990
 
1991
  ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
1992
  ifs->line = pfile->directive_line;
1993
  ifs->next = buffer->if_stack;
1994
  ifs->skip_elses = pfile->state.skipping || !skip;
1995
  ifs->was_skipping = pfile->state.skipping;
1996
  ifs->type = type;
1997
  /* This condition is effectively a test for top-of-file.  */
1998
  if (pfile->mi_valid && pfile->mi_cmacro == 0)
1999
    ifs->mi_cmacro = cmacro;
2000
  else
2001
    ifs->mi_cmacro = 0;
2002
 
2003
  pfile->state.skipping = skip;
2004
  buffer->if_stack = ifs;
2005
}
2006
 
2007
/* Read the tokens of the answer into the macro pool, in a directive
2008
   of type TYPE.  Only commit the memory if we intend it as permanent
2009
   storage, i.e. the #assert case.  Returns 0 on success, and sets
2010
   ANSWERP to point to the answer.  PRED_LOC is the location of the
2011
   predicate.  */
2012
static int
2013
parse_answer (cpp_reader *pfile, struct answer **answerp, int type,
2014
              source_location pred_loc)
2015
{
2016
  const cpp_token *paren;
2017
  struct answer *answer;
2018
  unsigned int acount;
2019
 
2020
  /* In a conditional, it is legal to not have an open paren.  We
2021
     should save the following token in this case.  */
2022
  paren = cpp_get_token (pfile);
2023
 
2024
  /* If not a paren, see if we're OK.  */
2025
  if (paren->type != CPP_OPEN_PAREN)
2026
    {
2027
      /* In a conditional no answer is a test for any answer.  It
2028
         could be followed by any token.  */
2029
      if (type == T_IF)
2030
        {
2031
          _cpp_backup_tokens (pfile, 1);
2032
          return 0;
2033
        }
2034
 
2035
      /* #unassert with no answer is valid - it removes all answers.  */
2036
      if (type == T_UNASSERT && paren->type == CPP_EOF)
2037
        return 0;
2038
 
2039
      cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
2040
                           "missing '(' after predicate");
2041
      return 1;
2042
    }
2043
 
2044
  for (acount = 0;; acount++)
2045
    {
2046
      size_t room_needed;
2047
      const cpp_token *token = cpp_get_token (pfile);
2048
      cpp_token *dest;
2049
 
2050
      if (token->type == CPP_CLOSE_PAREN)
2051
        break;
2052
 
2053
      if (token->type == CPP_EOF)
2054
        {
2055
          cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
2056
          return 1;
2057
        }
2058
 
2059
      /* struct answer includes the space for one token.  */
2060
      room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
2061
 
2062
      if (BUFF_ROOM (pfile->a_buff) < room_needed)
2063
        _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
2064
 
2065
      dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
2066
      *dest = *token;
2067
 
2068
      /* Drop whitespace at start, for answer equivalence purposes.  */
2069
      if (acount == 0)
2070
        dest->flags &= ~PREV_WHITE;
2071
    }
2072
 
2073
  if (acount == 0)
2074
    {
2075
      cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
2076
      return 1;
2077
    }
2078
 
2079
  answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
2080
  answer->count = acount;
2081
  answer->next = NULL;
2082
  *answerp = answer;
2083
 
2084
  return 0;
2085
}
2086
 
2087
/* Parses an assertion directive of type TYPE, returning a pointer to
2088
   the hash node of the predicate, or 0 on error.  If an answer was
2089
   supplied, it is placed in ANSWERP, otherwise it is set to 0.  */
2090
static cpp_hashnode *
2091
parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
2092
{
2093
  cpp_hashnode *result = 0;
2094
  const cpp_token *predicate;
2095
 
2096
  /* We don't expand predicates or answers.  */
2097
  pfile->state.prevent_expansion++;
2098
 
2099
  *answerp = 0;
2100
  predicate = cpp_get_token (pfile);
2101
  if (predicate->type == CPP_EOF)
2102
    cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
2103
  else if (predicate->type != CPP_NAME)
2104
    cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
2105
                         "predicate must be an identifier");
2106
  else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0)
2107
    {
2108
      unsigned int len = NODE_LEN (predicate->val.node.node);
2109
      unsigned char *sym = (unsigned char *) alloca (len + 1);
2110
 
2111
      /* Prefix '#' to get it out of macro namespace.  */
2112
      sym[0] = '#';
2113
      memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
2114
      result = cpp_lookup (pfile, sym, len + 1);
2115
    }
2116
 
2117
  pfile->state.prevent_expansion--;
2118
  return result;
2119
}
2120
 
2121
/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2122
   or a pointer to NULL if the answer is not in the chain.  */
2123
static struct answer **
2124
find_answer (cpp_hashnode *node, const struct answer *candidate)
2125
{
2126
  unsigned int i;
2127
  struct answer **result;
2128
 
2129
  for (result = &node->value.answers; *result; result = &(*result)->next)
2130
    {
2131
      struct answer *answer = *result;
2132
 
2133
      if (answer->count == candidate->count)
2134
        {
2135
          for (i = 0; i < answer->count; i++)
2136
            if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
2137
              break;
2138
 
2139
          if (i == answer->count)
2140
            break;
2141
        }
2142
    }
2143
 
2144
  return result;
2145
}
2146
 
2147
/* Test an assertion within a preprocessor conditional.  Returns
2148
   nonzero on failure, zero on success.  On success, the result of
2149
   the test is written into VALUE, otherwise the value 0.  */
2150
int
2151
_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
2152
{
2153
  struct answer *answer;
2154
  cpp_hashnode *node;
2155
 
2156
  node = parse_assertion (pfile, &answer, T_IF);
2157
 
2158
  /* For recovery, an erroneous assertion expression is handled as a
2159
     failing assertion.  */
2160
  *value = 0;
2161
 
2162
  if (node)
2163
    *value = (node->type == NT_ASSERTION &&
2164
              (answer == 0 || *find_answer (node, answer) != 0));
2165
  else if (pfile->cur_token[-1].type == CPP_EOF)
2166
    _cpp_backup_tokens (pfile, 1);
2167
 
2168
  /* We don't commit the memory for the answer - it's temporary only.  */
2169
  return node == 0;
2170
}
2171
 
2172
/* Handle #assert.  */
2173
static void
2174
do_assert (cpp_reader *pfile)
2175
{
2176
  struct answer *new_answer;
2177
  cpp_hashnode *node;
2178
 
2179
  node = parse_assertion (pfile, &new_answer, T_ASSERT);
2180
  if (node)
2181
    {
2182
      size_t answer_size;
2183
 
2184
      /* Place the new answer in the answer list.  First check there
2185
         is not a duplicate.  */
2186
      new_answer->next = 0;
2187
      if (node->type == NT_ASSERTION)
2188
        {
2189
          if (*find_answer (node, new_answer))
2190
            {
2191
              cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
2192
                         NODE_NAME (node) + 1);
2193
              return;
2194
            }
2195
          new_answer->next = node->value.answers;
2196
        }
2197
 
2198
      answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2199
                                              * sizeof (cpp_token));
2200
      /* Commit or allocate storage for the object.  */
2201
      if (pfile->hash_table->alloc_subobject)
2202
        {
2203
          struct answer *temp_answer = new_answer;
2204
          new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2205
            (answer_size);
2206
          memcpy (new_answer, temp_answer, answer_size);
2207
        }
2208
      else
2209
        BUFF_FRONT (pfile->a_buff) += answer_size;
2210
 
2211
      node->type = NT_ASSERTION;
2212
      node->value.answers = new_answer;
2213
      check_eol (pfile, false);
2214
    }
2215
}
2216
 
2217
/* Handle #unassert.  */
2218
static void
2219
do_unassert (cpp_reader *pfile)
2220
{
2221
  cpp_hashnode *node;
2222
  struct answer *answer;
2223
 
2224
  node = parse_assertion (pfile, &answer, T_UNASSERT);
2225
  /* It isn't an error to #unassert something that isn't asserted.  */
2226
  if (node && node->type == NT_ASSERTION)
2227
    {
2228
      if (answer)
2229
        {
2230
          struct answer **p = find_answer (node, answer), *temp;
2231
 
2232
          /* Remove the answer from the list.  */
2233
          temp = *p;
2234
          if (temp)
2235
            *p = temp->next;
2236
 
2237
          /* Did we free the last answer?  */
2238
          if (node->value.answers == 0)
2239
            node->type = NT_VOID;
2240
 
2241
          check_eol (pfile, false);
2242
        }
2243
      else
2244
        _cpp_free_definition (node);
2245
    }
2246
 
2247
  /* We don't commit the memory for the answer - it's temporary only.  */
2248
}
2249
 
2250
/* These are for -D, -U, -A.  */
2251
 
2252
/* Process the string STR as if it appeared as the body of a #define.
2253
   If STR is just an identifier, define it with value 1.
2254
   If STR has anything after the identifier, then it should
2255
   be identifier=definition.  */
2256
void
2257
cpp_define (cpp_reader *pfile, const char *str)
2258
{
2259
  char *buf;
2260
  const char *p;
2261
  size_t count;
2262
 
2263
  /* Copy the entire option so we can modify it.
2264
     Change the first "=" in the string to a space.  If there is none,
2265
     tack " 1" on the end.  */
2266
 
2267
  count = strlen (str);
2268
  buf = (char *) alloca (count + 3);
2269
  memcpy (buf, str, count);
2270
 
2271
  p = strchr (str, '=');
2272
  if (p)
2273
    buf[p - str] = ' ';
2274
  else
2275
    {
2276
      buf[count++] = ' ';
2277
      buf[count++] = '1';
2278
    }
2279
  buf[count] = '\n';
2280
 
2281
  run_directive (pfile, T_DEFINE, buf, count);
2282
}
2283
 
2284
 
2285
/* Use to build macros to be run through cpp_define() as
2286
   described above.
2287
   Example: cpp_define_formatted (pfile, "MACRO=%d", value);  */
2288
 
2289
void
2290
cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2291
{
2292
  char *ptr = NULL;
2293
 
2294
  va_list ap;
2295
  va_start (ap, fmt);
2296
  vasprintf (&ptr, fmt, ap);
2297
  va_end (ap);
2298
 
2299
  cpp_define (pfile, ptr);
2300
  free (ptr);
2301
}
2302
 
2303
 
2304
/* Slight variant of the above for use by initialize_builtins.  */
2305
void
2306
_cpp_define_builtin (cpp_reader *pfile, const char *str)
2307
{
2308
  size_t len = strlen (str);
2309
  char *buf = (char *) alloca (len + 1);
2310
  memcpy (buf, str, len);
2311
  buf[len] = '\n';
2312
  run_directive (pfile, T_DEFINE, buf, len);
2313
}
2314
 
2315
/* Process MACRO as if it appeared as the body of an #undef.  */
2316
void
2317
cpp_undef (cpp_reader *pfile, const char *macro)
2318
{
2319
  size_t len = strlen (macro);
2320
  char *buf = (char *) alloca (len + 1);
2321
  memcpy (buf, macro, len);
2322
  buf[len] = '\n';
2323
  run_directive (pfile, T_UNDEF, buf, len);
2324
}
2325
 
2326
/* If STR is a defined macro, return its definition node, else return NULL.  */
2327
cpp_macro *
2328
cpp_push_definition (cpp_reader *pfile, const char *str)
2329
{
2330
  cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
2331
  if (node && node->type == NT_MACRO)
2332
    return node->value.macro;
2333
  else
2334
    return NULL;
2335
}
2336
 
2337
/* Replace a previous definition DFN of the macro STR.  If DFN is NULL,
2338
   then the macro should be undefined.  */
2339
void
2340
cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
2341
{
2342
  cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
2343
  if (node == NULL)
2344
    return;
2345
 
2346
  if (pfile->cb.before_define)
2347
    pfile->cb.before_define (pfile);
2348
 
2349
  if (node->type == NT_MACRO)
2350
    {
2351
      if (pfile->cb.undef)
2352
        pfile->cb.undef (pfile, pfile->directive_line, node);
2353
      if (CPP_OPTION (pfile, warn_unused_macros))
2354
        _cpp_warn_if_unused_macro (pfile, node, NULL);
2355
    }
2356
  if (node->type != NT_VOID)
2357
    _cpp_free_definition (node);
2358
 
2359
  if (dfn)
2360
    {
2361
      node->type = NT_MACRO;
2362
      node->value.macro = dfn;
2363
      if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
2364
        node->flags |= NODE_WARN;
2365
 
2366
      if (pfile->cb.define)
2367
        pfile->cb.define (pfile, pfile->directive_line, node);
2368
    }
2369
}
2370
 
2371
/* Process the string STR as if it appeared as the body of a #assert.  */
2372
void
2373
cpp_assert (cpp_reader *pfile, const char *str)
2374
{
2375
  handle_assertion (pfile, str, T_ASSERT);
2376
}
2377
 
2378
/* Process STR as if it appeared as the body of an #unassert.  */
2379
void
2380
cpp_unassert (cpp_reader *pfile, const char *str)
2381
{
2382
  handle_assertion (pfile, str, T_UNASSERT);
2383
}
2384
 
2385
/* Common code for cpp_assert (-A) and cpp_unassert (-A-).  */
2386
static void
2387
handle_assertion (cpp_reader *pfile, const char *str, int type)
2388
{
2389
  size_t count = strlen (str);
2390
  const char *p = strchr (str, '=');
2391
 
2392
  /* Copy the entire option so we can modify it.  Change the first
2393
     "=" in the string to a '(', and tack a ')' on the end.  */
2394
  char *buf = (char *) alloca (count + 2);
2395
 
2396
  memcpy (buf, str, count);
2397
  if (p)
2398
    {
2399
      buf[p - str] = '(';
2400
      buf[count++] = ')';
2401
    }
2402
  buf[count] = '\n';
2403
  str = buf;
2404
 
2405
  run_directive (pfile, type, str, count);
2406
}
2407
 
2408
/* The options structure.  */
2409
cpp_options *
2410
cpp_get_options (cpp_reader *pfile)
2411
{
2412
  return &pfile->opts;
2413
}
2414
 
2415
/* The callbacks structure.  */
2416
cpp_callbacks *
2417
cpp_get_callbacks (cpp_reader *pfile)
2418
{
2419
  return &pfile->cb;
2420
}
2421
 
2422
/* Copy the given callbacks structure to our own.  */
2423
void
2424
cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2425
{
2426
  pfile->cb = *cb;
2427
}
2428
 
2429
/* The dependencies structure.  (Creates one if it hasn't already been.)  */
2430
struct deps *
2431
cpp_get_deps (cpp_reader *pfile)
2432
{
2433
  if (!pfile->deps)
2434
    pfile->deps = deps_init ();
2435
  return pfile->deps;
2436
}
2437
 
2438
/* Push a new buffer on the buffer stack.  Returns the new buffer; it
2439
   doesn't fail.  It does not generate a file change call back; that
2440
   is the responsibility of the caller.  */
2441
cpp_buffer *
2442
cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2443
                 int from_stage3)
2444
{
2445
  cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2446
 
2447
  /* Clears, amongst other things, if_stack and mi_cmacro.  */
2448
  memset (new_buffer, 0, sizeof (cpp_buffer));
2449
 
2450
  new_buffer->next_line = new_buffer->buf = buffer;
2451
  new_buffer->rlimit = buffer + len;
2452
  new_buffer->from_stage3 = from_stage3;
2453
  new_buffer->prev = pfile->buffer;
2454
  new_buffer->need_line = true;
2455
 
2456
  pfile->buffer = new_buffer;
2457
 
2458
  return new_buffer;
2459
}
2460
 
2461
/* Pops a single buffer, with a file change call-back if appropriate.
2462
   Then pushes the next -include file, if any remain.  */
2463
void
2464
_cpp_pop_buffer (cpp_reader *pfile)
2465
{
2466
  cpp_buffer *buffer = pfile->buffer;
2467
  struct _cpp_file *inc = buffer->file;
2468
  struct if_stack *ifs;
2469
 
2470
  /* Walk back up the conditional stack till we reach its level at
2471
     entry to this file, issuing error messages.  */
2472
  for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2473
    cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2474
                         "unterminated #%s", dtable[ifs->type].name);
2475
 
2476
  /* In case of a missing #endif.  */
2477
  pfile->state.skipping = 0;
2478
 
2479
  /* _cpp_do_file_change expects pfile->buffer to be the new one.  */
2480
  pfile->buffer = buffer->prev;
2481
 
2482
  free (buffer->notes);
2483
 
2484
  /* Free the buffer object now; we may want to push a new buffer
2485
     in _cpp_push_next_include_file.  */
2486
  obstack_free (&pfile->buffer_ob, buffer);
2487
 
2488
  if (inc)
2489
    {
2490
      _cpp_pop_file_buffer (pfile, inc);
2491
 
2492
      _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2493
    }
2494
}
2495
 
2496
/* Enter all recognized directives in the hash table.  */
2497
void
2498
_cpp_init_directives (cpp_reader *pfile)
2499
{
2500
  unsigned int i;
2501
  cpp_hashnode *node;
2502
 
2503
  for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2504
    {
2505
      node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2506
      node->is_directive = 1;
2507
      node->directive_index = i;
2508
    }
2509
}

powered by: WebSVN 2.1.0

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