OSDN Git Service

* doc/gcov.texi (Invoking Gcov): Describe output name mangling
[pf3gnuchains/gcc-fork.git] / gcc / cpplib.c
1 /* CPP Library. (Directive handling.)
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4    Contributed by Per Bothner, 1994-95.
5    Based on CCCP program by Paul Rubin, June 1986
6    Adapted to ANSI C, Richard Stallman, Jan 1987
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "obstack.h"
27
28 /* Chained list of answers to an assertion.  */
29 struct answer
30 {
31   struct answer *next;
32   unsigned int count;
33   cpp_token first[1];
34 };
35
36 /* Stack of conditionals currently in progress
37    (including both successful and failing conditionals).  */
38 struct if_stack
39 {
40   struct if_stack *next;
41   unsigned int line;            /* Line where condition started.  */
42   const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
43   bool skip_elses;              /* Can future #else / #elif be skipped?  */
44   bool was_skipping;            /* If were skipping on entry.  */
45   int type;                     /* Most recent conditional for diagnostics.  */
46 };
47
48 /* Contains a registered pragma or pragma namespace.  */
49 typedef void (*pragma_cb) (cpp_reader *);
50 struct pragma_entry
51 {
52   struct pragma_entry *next;
53   const cpp_hashnode *pragma;   /* Name and length.  */
54   int is_nspace;
55   union {
56     pragma_cb handler;
57     struct pragma_entry *space;
58   } u;
59 };
60
61 /* Values for the origin field of struct directive.  KANDR directives
62    come from traditional (K&R) C.  STDC89 directives come from the
63    1989 C standard.  EXTENSION directives are extensions.  */
64 #define KANDR           0
65 #define STDC89          1
66 #define EXTENSION       2
67
68 /* Values for the flags field of struct directive.  COND indicates a
69    conditional; IF_COND an opening conditional.  INCL means to treat
70    "..." and <...> as q-char and h-char sequences respectively.  IN_I
71    means this directive should be handled even if -fpreprocessed is in
72    effect (these are the directives with callback hooks).
73
74    EXPAND is set on directives that are always macro-expanded.  */
75 #define COND            (1 << 0)
76 #define IF_COND         (1 << 1)
77 #define INCL            (1 << 2)
78 #define IN_I            (1 << 3)
79 #define EXPAND          (1 << 4)
80
81 /* Defines one #-directive, including how to handle it.  */
82 typedef void (*directive_handler) (cpp_reader *);
83 typedef struct directive directive;
84 struct directive
85 {
86   directive_handler handler;    /* Function to handle directive.  */
87   const uchar *name;            /* Name of directive.  */
88   unsigned short length;        /* Length of name.  */
89   unsigned char origin;         /* Origin of directive.  */
90   unsigned char flags;          /* Flags describing this directive.  */
91 };
92
93 /* Forward declarations.  */
94
95 static void skip_rest_of_line (cpp_reader *);
96 static void check_eol (cpp_reader *);
97 static void start_directive (cpp_reader *);
98 static void prepare_directive_trad (cpp_reader *);
99 static void end_directive (cpp_reader *, int);
100 static void directive_diagnostics (cpp_reader *, const directive *, int);
101 static void run_directive (cpp_reader *, int, const char *, size_t);
102 static char *glue_header_name (cpp_reader *);
103 static const char *parse_include (cpp_reader *, int *);
104 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
105 static unsigned int read_flag (cpp_reader *, unsigned int);
106 static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
107 static void do_diagnostic (cpp_reader *, int, int);
108 static cpp_hashnode *lex_macro_node (cpp_reader *);
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 struct pragma_entry *insert_pragma_entry (cpp_reader *,
114                                                  struct pragma_entry **,
115                                                  const cpp_hashnode *,
116                                                  pragma_cb);
117 static int count_registered_pragmas (struct pragma_entry *);
118 static char ** save_registered_pragmas (struct pragma_entry *, char **);
119 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
120                                            char **);
121 static void do_pragma_once (cpp_reader *);
122 static void do_pragma_poison (cpp_reader *);
123 static void do_pragma_system_header (cpp_reader *);
124 static void do_pragma_dependency (cpp_reader *);
125 static void do_linemarker (cpp_reader *);
126 static const cpp_token *get_token_no_padding (cpp_reader *);
127 static const cpp_token *get__Pragma_string (cpp_reader *);
128 static void destringize_and_run (cpp_reader *, const cpp_string *);
129 static int parse_answer (cpp_reader *, struct answer **, int);
130 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
131 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
132 static void handle_assertion (cpp_reader *, const char *, int);
133
134 /* This is the table of directive handlers.  It is ordered by
135    frequency of occurrence; the numbers at the end are directive
136    counts from all the source code I have lying around (egcs and libc
137    CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
138    pcmcia-cs-3.0.9).  This is no longer important as directive lookup
139    is now O(1).  All extensions other than #warning and #include_next
140    are deprecated.  The name is where the extension appears to have
141    come from.  */
142
143 #define DIRECTIVE_TABLE                                                 \
144 D(define,       T_DEFINE = 0,   KANDR,     IN_I)           /* 270554 */ \
145 D(include,      T_INCLUDE,      KANDR,     INCL | EXPAND)  /*  52262 */ \
146 D(endif,        T_ENDIF,        KANDR,     COND)           /*  45855 */ \
147 D(ifdef,        T_IFDEF,        KANDR,     COND | IF_COND) /*  22000 */ \
148 D(if,           T_IF,           KANDR, COND | IF_COND | EXPAND) /*  18162 */ \
149 D(else,         T_ELSE,         KANDR,     COND)           /*   9863 */ \
150 D(ifndef,       T_IFNDEF,       KANDR,     COND | IF_COND) /*   9675 */ \
151 D(undef,        T_UNDEF,        KANDR,     IN_I)           /*   4837 */ \
152 D(line,         T_LINE,         KANDR,     EXPAND)         /*   2465 */ \
153 D(elif,         T_ELIF,         STDC89,    COND | EXPAND)  /*    610 */ \
154 D(error,        T_ERROR,        STDC89,    0)              /*    475 */ \
155 D(pragma,       T_PRAGMA,       STDC89,    IN_I)           /*    195 */ \
156 D(warning,      T_WARNING,      EXTENSION, 0)              /*     22 */ \
157 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND)  /*     19 */ \
158 D(ident,        T_IDENT,        EXTENSION, IN_I)           /*     11 */ \
159 D(import,       T_IMPORT,       EXTENSION, INCL | EXPAND)  /* 0 ObjC */ \
160 D(assert,       T_ASSERT,       EXTENSION, 0)              /* 0 SVR4 */ \
161 D(unassert,     T_UNASSERT,     EXTENSION, 0)              /* 0 SVR4 */ \
162 D(sccs,         T_SCCS,         EXTENSION, 0)              /* 0 SVR4? */
163
164 /* Use the table to generate a series of prototypes, an enum for the
165    directive names, and an array of directive handlers.  */
166
167 #define D(name, t, o, f) static void do_##name (cpp_reader *);
168 DIRECTIVE_TABLE
169 #undef D
170
171 #define D(n, tag, o, f) tag,
172 enum
173 {
174   DIRECTIVE_TABLE
175   N_DIRECTIVES
176 };
177 #undef D
178
179 #define D(name, t, origin, flags) \
180 { do_##name, (const uchar *) #name, \
181   sizeof #name - 1, origin, flags },
182 static const directive dtable[] =
183 {
184 DIRECTIVE_TABLE
185 };
186 #undef D
187 #undef DIRECTIVE_TABLE
188
189 /* Wrapper struct directive for linemarkers.
190    The origin is more or less true - the original K+R cpp
191    did use this notation in its preprocessed output.  */
192 static const directive linemarker_dir =
193 {
194   do_linemarker, U"#", 1, KANDR, IN_I
195 };
196
197 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
198
199 /* Skip any remaining tokens in a directive.  */
200 static void
201 skip_rest_of_line (cpp_reader *pfile)
202 {
203   /* Discard all stacked contexts.  */
204   while (pfile->context->prev)
205     _cpp_pop_context (pfile);
206
207   /* Sweep up all tokens remaining on the line.  */
208   if (! SEEN_EOL ())
209     while (_cpp_lex_token (pfile)->type != CPP_EOF)
210       ;
211 }
212
213 /* Ensure there are no stray tokens at the end of a directive.  */
214 static void
215 check_eol (cpp_reader *pfile)
216 {
217   if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
218     cpp_error (pfile, DL_PEDWARN, "extra tokens at end of #%s directive",
219                pfile->directive->name);
220 }
221
222 /* Called when entering a directive, _Pragma or command-line directive.  */
223 static void
224 start_directive (cpp_reader *pfile)
225 {
226   /* Setup in-directive state.  */
227   pfile->state.in_directive = 1;
228   pfile->state.save_comments = 0;
229
230   /* Some handlers need the position of the # for diagnostics.  */
231   pfile->directive_line = pfile->line;
232 }
233
234 /* Called when leaving a directive, _Pragma or command-line directive.  */
235 static void
236 end_directive (cpp_reader *pfile, int skip_line)
237 {
238   if (CPP_OPTION (pfile, traditional))
239     {
240       /* Revert change of prepare_directive_trad.  */
241       pfile->state.prevent_expansion--;
242
243       if (pfile->directive != &dtable[T_DEFINE])
244         _cpp_remove_overlay (pfile);
245     }
246   /* We don't skip for an assembler #.  */
247   else if (skip_line)
248     {
249       skip_rest_of_line (pfile);
250       if (!pfile->keep_tokens)
251         {
252           pfile->cur_run = &pfile->base_run;
253           pfile->cur_token = pfile->base_run.base;
254         }
255     }
256
257   /* Restore state.  */
258   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
259   pfile->state.in_directive = 0;
260   pfile->state.in_expression = 0;
261   pfile->state.angled_headers = 0;
262   pfile->directive = 0;
263 }
264
265 /* Prepare to handle the directive in pfile->directive.  */
266 static void
267 prepare_directive_trad (cpp_reader *pfile)
268 {
269   if (pfile->directive != &dtable[T_DEFINE])
270     {
271       bool no_expand = (pfile->directive
272                         && ! (pfile->directive->flags & EXPAND));
273       bool was_skipping = pfile->state.skipping;
274
275       pfile->state.skipping = false;
276       pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
277                                     || pfile->directive == &dtable[T_ELIF]);
278       if (no_expand)
279         pfile->state.prevent_expansion++;
280       _cpp_scan_out_logical_line (pfile, NULL);
281       if (no_expand)
282         pfile->state.prevent_expansion--;
283       pfile->state.skipping = was_skipping;
284       _cpp_overlay_buffer (pfile, pfile->out.base,
285                            pfile->out.cur - pfile->out.base);
286     }
287
288   /* Stop ISO C from expanding anything.  */
289   pfile->state.prevent_expansion++;
290 }
291
292 /* Output diagnostics for a directive DIR.  INDENTED is nonzero if
293    the '#' was indented.  */
294 static void
295 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
296 {
297   /* Issue -pedantic warnings for extensions.  */
298   if (CPP_PEDANTIC (pfile)
299       && ! pfile->state.skipping
300       && dir->origin == EXTENSION)
301     cpp_error (pfile, DL_PEDWARN, "#%s is a GCC extension", dir->name);
302
303   /* Traditionally, a directive is ignored unless its # is in
304      column 1.  Therefore in code intended to work with K+R
305      compilers, directives added by C89 must have their #
306      indented, and directives present in traditional C must not.
307      This is true even of directives in skipped conditional
308      blocks.  #elif cannot be used at all.  */
309   if (CPP_WTRADITIONAL (pfile))
310     {
311       if (dir == &dtable[T_ELIF])
312         cpp_error (pfile, DL_WARNING,
313                    "suggest not using #elif in traditional C");
314       else if (indented && dir->origin == KANDR)
315         cpp_error (pfile, DL_WARNING,
316                    "traditional C ignores #%s with the # indented",
317                    dir->name);
318       else if (!indented && dir->origin != KANDR)
319         cpp_error (pfile, DL_WARNING,
320                    "suggest hiding #%s from traditional C with an indented #",
321                    dir->name);
322     }
323 }
324
325 /* Check if we have a known directive.  INDENTED is nonzero if the
326    '#' of the directive was indented.  This function is in this file
327    to save unnecessarily exporting dtable etc. to cpplex.c.  Returns
328    nonzero if the line of tokens has been handled, zero if we should
329    continue processing the line.  */
330 int
331 _cpp_handle_directive (cpp_reader *pfile, int indented)
332 {
333   const directive *dir = 0;
334   const cpp_token *dname;
335   bool was_parsing_args = pfile->state.parsing_args;
336   int skip = 1;
337
338   if (was_parsing_args)
339     {
340       if (CPP_OPTION (pfile, pedantic))
341         cpp_error (pfile, DL_PEDWARN,
342              "embedding a directive within macro arguments is not portable");
343       pfile->state.parsing_args = 0;
344       pfile->state.prevent_expansion = 0;
345     }
346   start_directive (pfile);
347   dname = _cpp_lex_token (pfile);
348
349   if (dname->type == CPP_NAME)
350     {
351       if (dname->val.node->is_directive)
352         dir = &dtable[dname->val.node->directive_index];
353     }
354   /* We do not recognize the # followed by a number extension in
355      assembler code.  */
356   else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
357     {
358       dir = &linemarker_dir;
359       if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
360           && ! pfile->state.skipping)
361         cpp_error (pfile, DL_PEDWARN,
362                    "style of line directive is a GCC extension");
363     }
364
365   if (dir)
366     {
367       /* If we have a directive that is not an opening conditional,
368          invalidate any control macro.  */
369       if (! (dir->flags & IF_COND))
370         pfile->mi_valid = false;
371
372       /* Kluge alert.  In order to be sure that code like this
373
374          #define HASH #
375          HASH define foo bar
376
377          does not cause '#define foo bar' to get executed when
378          compiled with -save-temps, we recognize directives in
379          -fpreprocessed mode only if the # is in column 1.  cppmacro.c
380          puts a space in front of any '#' at the start of a macro.  */
381       if (CPP_OPTION (pfile, preprocessed)
382           && (indented || !(dir->flags & IN_I)))
383         {
384           skip = 0;
385           dir = 0;
386         }
387       else
388         {
389           /* In failed conditional groups, all non-conditional
390              directives are ignored.  Before doing that, whether
391              skipping or not, we should lex angle-bracketed headers
392              correctly, and maybe output some diagnostics.  */
393           pfile->state.angled_headers = dir->flags & INCL;
394           pfile->state.directive_wants_padding = dir->flags & INCL;
395           if (! CPP_OPTION (pfile, preprocessed))
396             directive_diagnostics (pfile, dir, indented);
397           if (pfile->state.skipping && !(dir->flags & COND))
398             dir = 0;
399         }
400     }
401   else if (dname->type == CPP_EOF)
402     ;   /* CPP_EOF is the "null directive".  */
403   else
404     {
405       /* An unknown directive.  Don't complain about it in assembly
406          source: we don't know where the comments are, and # may
407          introduce assembler pseudo-ops.  Don't complain about invalid
408          directives in skipped conditional groups (6.10 p4).  */
409       if (CPP_OPTION (pfile, lang) == CLK_ASM)
410         skip = 0;
411       else if (!pfile->state.skipping)
412         cpp_error (pfile, DL_ERROR, "invalid preprocessing directive #%s",
413                    cpp_token_as_text (pfile, dname));
414     }
415
416   pfile->directive = dir;
417   if (CPP_OPTION (pfile, traditional))
418     prepare_directive_trad (pfile);
419
420   if (dir)
421     pfile->directive->handler (pfile);
422   else if (skip == 0)
423     _cpp_backup_tokens (pfile, 1);
424
425   end_directive (pfile, skip);
426   if (was_parsing_args)
427     {
428       /* Restore state when within macro args.  */
429       pfile->state.parsing_args = 2;
430       pfile->state.prevent_expansion = 1;
431     }
432   return skip;
433 }
434
435 /* Directive handler wrapper used by the command line option
436    processor.  BUF is \n terminated.  */
437 static void
438 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
439 {
440   cpp_push_buffer (pfile, (const uchar *) buf, count,
441                    /* from_stage3 */ true, 1);
442   /* Disgusting hack.  */
443   if (dir_no == T_PRAGMA)
444     pfile->buffer->file = pfile->buffer->prev->file;
445   start_directive (pfile);
446
447   /* This is a short-term fix to prevent a leading '#' being
448      interpreted as a directive.  */
449   _cpp_clean_line (pfile);
450
451   pfile->directive = &dtable[dir_no];
452   if (CPP_OPTION (pfile, traditional))
453     prepare_directive_trad (pfile);
454   pfile->directive->handler (pfile);
455   end_directive (pfile, 1);
456   if (dir_no == T_PRAGMA)
457     pfile->buffer->file = NULL;
458   _cpp_pop_buffer (pfile);
459 }
460
461 /* Checks for validity the macro name in #define, #undef, #ifdef and
462    #ifndef directives.  */
463 static cpp_hashnode *
464 lex_macro_node (cpp_reader *pfile)
465 {
466   const cpp_token *token = _cpp_lex_token (pfile);
467
468   /* The token immediately after #define must be an identifier.  That
469      identifier may not be "defined", per C99 6.10.8p4.
470      In C++, it may not be any of the "named operators" either,
471      per C++98 [lex.digraph], [lex.key].
472      Finally, the identifier may not have been poisoned.  (In that case
473      the lexer has issued the error message for us.)  */
474
475   if (token->type == CPP_NAME)
476     {
477       cpp_hashnode *node = token->val.node;
478
479       if (node == pfile->spec_nodes.n_defined)
480         cpp_error (pfile, DL_ERROR,
481                    "\"defined\" cannot be used as a macro name");
482       else if (! (node->flags & NODE_POISONED))
483         return node;
484     }
485   else if (token->flags & NAMED_OP)
486     cpp_error (pfile, DL_ERROR,
487        "\"%s\" cannot be used as a macro name as it is an operator in C++",
488                NODE_NAME (token->val.node));
489   else if (token->type == CPP_EOF)
490     cpp_error (pfile, DL_ERROR, "no macro name given in #%s directive",
491                pfile->directive->name);
492   else
493     cpp_error (pfile, DL_ERROR, "macro names must be identifiers");
494
495   return NULL;
496 }
497
498 /* Process a #define directive.  Most work is done in cppmacro.c.  */
499 static void
500 do_define (cpp_reader *pfile)
501 {
502   cpp_hashnode *node = lex_macro_node (pfile);
503
504   if (node)
505     {
506       /* If we have been requested to expand comments into macros,
507          then re-enable saving of comments.  */
508       pfile->state.save_comments =
509         ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
510
511       if (_cpp_create_definition (pfile, node))
512         if (pfile->cb.define)
513           pfile->cb.define (pfile, pfile->directive_line, node);
514     }
515 }
516
517 /* Handle #undef.  Mark the identifier NT_VOID in the hash table.  */
518 static void
519 do_undef (cpp_reader *pfile)
520 {
521   cpp_hashnode *node = lex_macro_node (pfile);
522
523   /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
524      is not currently defined as a macro name.  */
525   if (node && node->type == NT_MACRO)
526     {
527       if (pfile->cb.undef)
528         pfile->cb.undef (pfile, pfile->directive_line, node);
529
530       if (node->flags & NODE_WARN)
531         cpp_error (pfile, DL_WARNING, "undefining \"%s\"", NODE_NAME (node));
532
533       if (CPP_OPTION (pfile, warn_unused_macros))
534         _cpp_warn_if_unused_macro (pfile, node, NULL);
535
536       _cpp_free_definition (node);
537     }
538   check_eol (pfile);
539 }
540
541 /* Undefine a single macro/assertion/whatever.  */
542
543 static int
544 undefine_macros (cpp_reader *pfile, cpp_hashnode *h, 
545                  void *data_p ATTRIBUTE_UNUSED)
546 {
547   switch (h->type)
548     {
549     case NT_VOID:
550       break;
551       
552     case NT_MACRO:
553       if (pfile->cb.undef)
554         (*pfile->cb.undef) (pfile, pfile->directive_line, h);
555
556       if (CPP_OPTION (pfile, warn_unused_macros))
557         _cpp_warn_if_unused_macro (pfile, h, NULL);
558
559       /* And fall through....  */
560     case NT_ASSERTION:
561       _cpp_free_definition (h);
562       break;
563
564     default:
565       abort ();
566     }
567   h->flags &= ~NODE_POISONED;
568   return 1;
569 }
570
571 /* Undefine all macros and assertions.  */
572
573 void
574 cpp_undef_all (cpp_reader *pfile)
575 {
576   cpp_forall_identifiers (pfile, undefine_macros, NULL);
577 }
578
579
580 /* Helper routine used by parse_include.  Reinterpret the current line
581    as an h-char-sequence (< ... >); we are looking at the first token
582    after the <.  Returns a malloced filename.  */
583 static char *
584 glue_header_name (cpp_reader *pfile)
585 {
586   const cpp_token *token;
587   char *buffer;
588   size_t len, total_len = 0, capacity = 1024;
589
590   /* To avoid lexed tokens overwriting our glued name, we can only
591      allocate from the string pool once we've lexed everything.  */
592   buffer = xmalloc (capacity);
593   for (;;)
594     {
595       token = get_token_no_padding (pfile);
596
597       if (token->type == CPP_GREATER)
598         break;
599       if (token->type == CPP_EOF)
600         {
601           cpp_error (pfile, DL_ERROR, "missing terminating > character");
602           break;
603         }
604
605       len = cpp_token_len (token) + 2; /* Leading space, terminating \0.  */
606       if (total_len + len > capacity)
607         {
608           capacity = (capacity + len) * 2;
609           buffer = xrealloc (buffer, capacity);
610         }
611
612       if (token->flags & PREV_WHITE)
613         buffer[total_len++] = ' ';
614
615       total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len])
616                    - (uchar *) buffer);
617     }
618
619   buffer[total_len] = '\0';
620   return buffer;
621 }
622
623 /* Returns the file name of #include, #include_next, #import and
624    #pragma dependency.  The string is malloced and the caller should
625    free it.  Returns NULL on error.  */
626 static const char *
627 parse_include (cpp_reader *pfile, int *pangle_brackets)
628 {
629   char *fname;
630   const cpp_token *header;
631
632   /* Allow macro expansion.  */
633   header = get_token_no_padding (pfile);
634   if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
635     {
636       fname = xmalloc (header->val.str.len - 1);
637       memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
638       fname[header->val.str.len - 2] = '\0';
639       *pangle_brackets = header->type == CPP_HEADER_NAME;
640     }
641   else if (header->type == CPP_LESS)
642     {
643       fname = glue_header_name (pfile);
644       *pangle_brackets = 1;
645     }
646   else
647     {
648       const unsigned char *dir;
649
650       if (pfile->directive == &dtable[T_PRAGMA])
651         dir = U"pragma dependency";
652       else
653         dir = pfile->directive->name;
654       cpp_error (pfile, DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
655                  dir);
656
657       return NULL;
658     }
659
660   check_eol (pfile);
661   return fname;
662 }
663
664 /* Handle #include, #include_next and #import.  */
665 static void
666 do_include_common (cpp_reader *pfile, enum include_type type)
667 {
668   const char *fname;
669   int angle_brackets;
670
671   fname = parse_include (pfile, &angle_brackets);
672   if (!fname)
673     return;
674
675   /* Prevent #include recursion.  */
676   if (pfile->line_maps.depth >= CPP_STACK_MAX)
677     cpp_error (pfile, DL_ERROR, "#include nested too deeply");
678   else
679     {
680       /* Get out of macro context, if we are.  */
681       skip_rest_of_line (pfile);
682
683       if (pfile->cb.include)
684         pfile->cb.include (pfile, pfile->directive_line,
685                            pfile->directive->name, fname, angle_brackets);
686
687       _cpp_stack_include (pfile, fname, angle_brackets, type);
688     }
689
690   free ((void *) fname);
691 }
692
693 static void
694 do_include (cpp_reader *pfile)
695 {
696   do_include_common (pfile, IT_INCLUDE);
697 }
698
699 static void
700 do_import (cpp_reader *pfile)
701 {
702   do_include_common (pfile, IT_IMPORT);
703 }
704
705 static void
706 do_include_next (cpp_reader *pfile)
707 {
708   enum include_type type = IT_INCLUDE_NEXT;
709
710   /* If this is the primary source file, warn and use the normal
711      search logic.  */
712   if (! pfile->buffer->prev)
713     {
714       cpp_error (pfile, DL_WARNING,
715                  "#include_next in primary source file");
716       type = IT_INCLUDE;
717     }
718   do_include_common (pfile, type);
719 }
720
721 /* Subroutine of do_linemarker.  Read possible flags after file name.
722    LAST is the last flag seen; 0 if this is the first flag. Return the
723    flag if it is valid, 0 at the end of the directive. Otherwise
724    complain.  */
725 static unsigned int
726 read_flag (cpp_reader *pfile, unsigned int last)
727 {
728   const cpp_token *token = _cpp_lex_token (pfile);
729
730   if (token->type == CPP_NUMBER && token->val.str.len == 1)
731     {
732       unsigned int flag = token->val.str.text[0] - '0';
733
734       if (flag > last && flag <= 4
735           && (flag != 4 || last == 3)
736           && (flag != 2 || last == 0))
737         return flag;
738     }
739
740   if (token->type != CPP_EOF)
741     cpp_error (pfile, DL_ERROR, "invalid flag \"%s\" in line directive",
742                cpp_token_as_text (pfile, token));
743   return 0;
744 }
745
746 /* Subroutine of do_line and do_linemarker.  Convert a number in STR,
747    of length LEN, to binary; store it in NUMP, and return 0 if the
748    number was well-formed, 1 if not.  Temporary, hopefully.  */
749 static int
750 strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
751 {
752   unsigned long reg = 0;
753   uchar c;
754   while (len--)
755     {
756       c = *str++;
757       if (!ISDIGIT (c))
758         return 1;
759       reg *= 10;
760       reg += c - '0';
761     }
762   *nump = reg;
763   return 0;
764 }
765
766 /* Interpret #line command.
767    Note that the filename string (if any) is a true string constant
768    (escapes are interpreted), unlike in #line.  */
769 static void
770 do_line (cpp_reader *pfile)
771 {
772   const cpp_token *token;
773   const char *new_file = pfile->map->to_file;
774   unsigned long new_lineno;
775
776   /* C99 raised the minimum limit on #line numbers.  */
777   unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
778
779   /* #line commands expand macros.  */
780   token = cpp_get_token (pfile);
781   if (token->type != CPP_NUMBER
782       || strtoul_for_line (token->val.str.text, token->val.str.len,
783                            &new_lineno))
784     {
785       cpp_error (pfile, DL_ERROR,
786                  "\"%s\" after #line is not a positive integer",
787                  cpp_token_as_text (pfile, token));
788       return;
789     }
790
791   if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
792     cpp_error (pfile, DL_PEDWARN, "line number out of range");
793
794   token = cpp_get_token (pfile);
795   if (token->type == CPP_STRING)
796     {
797       cpp_string s = { 0, 0 };
798       if (_cpp_interpret_string_notranslate (pfile, &token->val.str, &s))
799         new_file = (const char *)s.text;
800       check_eol (pfile);
801     }
802   else if (token->type != CPP_EOF)
803     {
804       cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
805                  cpp_token_as_text (pfile, token));
806       return;
807     }
808
809   skip_rest_of_line (pfile);
810   _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
811                        pfile->map->sysp);
812 }
813
814 /* Interpret the # 44 "file" [flags] notation, which has slightly
815    different syntax and semantics from #line:  Flags are allowed,
816    and we never complain about the line number being too big.  */
817 static void
818 do_linemarker (cpp_reader *pfile)
819 {
820   const cpp_token *token;
821   const char *new_file = pfile->map->to_file;
822   unsigned long new_lineno;
823   unsigned int new_sysp = pfile->map->sysp;
824   enum lc_reason reason = LC_RENAME;
825   int flag;
826
827   /* Back up so we can get the number again.  Putting this in
828      _cpp_handle_directive risks two calls to _cpp_backup_tokens in
829      some circumstances, which can segfault.  */
830   _cpp_backup_tokens (pfile, 1);
831
832   /* #line commands expand macros.  */
833   token = cpp_get_token (pfile);
834   if (token->type != CPP_NUMBER
835       || strtoul_for_line (token->val.str.text, token->val.str.len,
836                            &new_lineno))
837     {
838       cpp_error (pfile, DL_ERROR, "\"%s\" after # is not a positive integer",
839                  cpp_token_as_text (pfile, token));
840       return;
841     }
842
843   token = cpp_get_token (pfile);
844   if (token->type == CPP_STRING)
845     {
846       cpp_string s = { 0, 0 };
847       if (_cpp_interpret_string_notranslate (pfile, &token->val.str, &s))
848         new_file = (const char *)s.text;
849       
850       new_sysp = 0;
851       flag = read_flag (pfile, 0);
852       if (flag == 1)
853         {
854           reason = LC_ENTER;
855           /* Fake an include for cpp_included ().  */
856           _cpp_fake_include (pfile, new_file);
857           flag = read_flag (pfile, flag);
858         }
859       else if (flag == 2)
860         {
861           reason = LC_LEAVE;
862           flag = read_flag (pfile, flag);
863         }
864       if (flag == 3)
865         {
866           new_sysp = 1;
867           flag = read_flag (pfile, flag);
868           if (flag == 4)
869             new_sysp = 2;
870         }
871
872       check_eol (pfile);
873     }
874   else if (token->type != CPP_EOF)
875     {
876       cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
877                  cpp_token_as_text (pfile, token));
878       return;
879     }
880
881   skip_rest_of_line (pfile);
882   _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
883 }
884
885 /* Arrange the file_change callback.  pfile->line has changed to
886    FILE_LINE of TO_FILE, for reason REASON.  SYSP is 1 for a system
887    header, 2 for a system header that needs to be extern "C" protected,
888    and zero otherwise.  */
889 void
890 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
891                      const char *to_file, unsigned int file_line,
892                      unsigned int sysp)
893 {
894   pfile->map = linemap_add (&pfile->line_maps, reason, sysp,
895                             pfile->line, to_file, file_line);
896
897   if (pfile->cb.file_change)
898     pfile->cb.file_change (pfile, pfile->map);
899 }
900
901 /* Report a warning or error detected by the program we are
902    processing.  Use the directive's tokens in the error message.  */
903 static void
904 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
905 {
906   if (_cpp_begin_message (pfile, code,
907                           pfile->cur_token[-1].line,
908                           pfile->cur_token[-1].col))
909     {
910       if (print_dir)
911         fprintf (stderr, "#%s ", pfile->directive->name);
912       pfile->state.prevent_expansion++;
913       cpp_output_line (pfile, stderr);
914       pfile->state.prevent_expansion--;
915     }
916 }
917
918 static void
919 do_error (cpp_reader *pfile)
920 {
921   do_diagnostic (pfile, DL_ERROR, 1);
922 }
923
924 static void
925 do_warning (cpp_reader *pfile)
926 {
927   /* We want #warning diagnostics to be emitted in system headers too.  */
928   do_diagnostic (pfile, DL_WARNING_SYSHDR, 1);
929 }
930
931 /* Report program identification.  */
932 static void
933 do_ident (cpp_reader *pfile)
934 {
935   const cpp_token *str = cpp_get_token (pfile);
936
937   if (str->type != CPP_STRING)
938     cpp_error (pfile, DL_ERROR, "invalid #ident directive");
939   else if (pfile->cb.ident)
940     pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
941
942   check_eol (pfile);
943 }
944
945 /* Lookup a PRAGMA name in a singly-linked CHAIN.  Returns the
946    matching entry, or NULL if none is found.  The returned entry could
947    be the start of a namespace chain, or a pragma.  */
948 static struct pragma_entry *
949 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
950 {
951   while (chain && chain->pragma != pragma)
952     chain = chain->next;
953
954   return chain;
955 }
956
957 /* Create and insert a pragma entry for NAME at the beginning of a
958    singly-linked CHAIN.  If handler is NULL, it is a namespace,
959    otherwise it is a pragma and its handler.  */
960 static struct pragma_entry *
961 insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
962                      const cpp_hashnode *pragma, pragma_cb handler)
963 {
964   struct pragma_entry *new;
965
966   new = (struct pragma_entry *)
967     _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
968   new->pragma = pragma;
969   if (handler)
970     {
971       new->is_nspace = 0;
972       new->u.handler = handler;
973     }
974   else
975     {
976       new->is_nspace = 1;
977       new->u.space = NULL;
978     }
979
980   new->next = *chain;
981   *chain = new;
982   return new;
983 }
984
985 /* Register a pragma NAME in namespace SPACE.  If SPACE is null, it
986    goes in the global namespace.  HANDLER is the handler it will call,
987    which must be non-NULL.  */
988 void
989 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
990                      pragma_cb handler)
991 {
992   struct pragma_entry **chain = &pfile->pragmas;
993   struct pragma_entry *entry;
994   const cpp_hashnode *node;
995
996   if (!handler)
997     abort ();
998
999   if (space)
1000     {
1001       node = cpp_lookup (pfile, U space, strlen (space));
1002       entry = lookup_pragma_entry (*chain, node);
1003       if (!entry)
1004         entry = insert_pragma_entry (pfile, chain, node, NULL);
1005       else if (!entry->is_nspace)
1006         goto clash;
1007       chain = &entry->u.space;
1008     }
1009
1010   /* Check for duplicates.  */
1011   node = cpp_lookup (pfile, U name, strlen (name));
1012   entry = lookup_pragma_entry (*chain, node);
1013   if (entry)
1014     {
1015       if (entry->is_nspace)
1016         clash:
1017         cpp_error (pfile, DL_ICE,
1018                  "registering \"%s\" as both a pragma and a pragma namespace",
1019                  NODE_NAME (node));
1020       else if (space)
1021         cpp_error (pfile, DL_ICE, "#pragma %s %s is already registered",
1022                    space, name);
1023       else
1024         cpp_error (pfile, DL_ICE, "#pragma %s is already registered", name);
1025     }
1026   else
1027     insert_pragma_entry (pfile, chain, node, handler);
1028 }
1029
1030 /* Register the pragmas the preprocessor itself handles.  */
1031 void
1032 _cpp_init_internal_pragmas (cpp_reader *pfile)
1033 {
1034   /* Pragmas in the global namespace.  */
1035   cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1036
1037   /* New GCC-specific pragmas should be put in the GCC namespace.  */
1038   cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1039   cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1040   cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
1041 }
1042
1043 /* Return the number of registered pragmas in PE.  */
1044
1045 static int
1046 count_registered_pragmas (struct pragma_entry *pe)
1047 {
1048   int ct = 0;
1049   for (; pe != NULL; pe = pe->next)
1050     {
1051       if (pe->is_nspace)
1052         ct += count_registered_pragmas (pe->u.space);
1053       ct++;
1054     }
1055   return ct;
1056 }
1057
1058 /* Save into SD the names of the registered pragmas referenced by PE,
1059    and return a pointer to the next free space in SD.  */
1060
1061 static char **
1062 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1063 {
1064   for (; pe != NULL; pe = pe->next)
1065     {
1066       if (pe->is_nspace)
1067         sd = save_registered_pragmas (pe->u.space, sd);
1068       *sd++ = xmemdup (HT_STR (&pe->pragma->ident),
1069                        HT_LEN (&pe->pragma->ident),
1070                        HT_LEN (&pe->pragma->ident) + 1);
1071     }
1072   return sd;
1073 }
1074
1075 /* Return a newly-allocated array which saves the names of the
1076    registered pragmas.  */
1077
1078 char **
1079 _cpp_save_pragma_names (cpp_reader *pfile)
1080 {
1081   int ct = count_registered_pragmas (pfile->pragmas);
1082   char **result = xnewvec (char *, ct);
1083   (void) save_registered_pragmas (pfile->pragmas, result);
1084   return result;
1085 }
1086
1087 /* Restore from SD the names of the registered pragmas referenced by PE,
1088    and return a pointer to the next unused name in SD.  */
1089
1090 static char **
1091 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1092                             char **sd)
1093 {
1094   for (; pe != NULL; pe = pe->next)
1095     {
1096       if (pe->is_nspace)
1097         sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1098       pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1099       free (*sd);
1100       sd++;
1101     }
1102   return sd;
1103 }
1104
1105 /* Restore the names of the registered pragmas from SAVED.  */
1106
1107 void
1108 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1109 {
1110   (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1111   free (saved);
1112 }
1113
1114 /* Pragmata handling.  We handle some, and pass the rest on to the
1115    front end.  C99 defines three pragmas and says that no macro
1116    expansion is to be performed on them; whether or not macro
1117    expansion happens for other pragmas is implementation defined.
1118    This implementation never macro-expands the text after #pragma.  */
1119 static void
1120 do_pragma (cpp_reader *pfile)
1121 {
1122   const struct pragma_entry *p = NULL;
1123   const cpp_token *token;
1124   unsigned int count = 1;
1125
1126   pfile->state.prevent_expansion++;
1127
1128   token = cpp_get_token (pfile);
1129   if (token->type == CPP_NAME)
1130     {
1131       p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1132       if (p && p->is_nspace)
1133         {
1134           count = 2;
1135           token = cpp_get_token (pfile);
1136           if (token->type == CPP_NAME)
1137             p = lookup_pragma_entry (p->u.space, token->val.node);
1138           else
1139             p = NULL;
1140         }
1141     }
1142
1143   /* FIXME.  This is an awful kludge to get the front ends to update
1144      their notion of line number for diagnostic purposes.  The line
1145      number should be passed to the handler and they should do it
1146      themselves.  Stand-alone CPP must ignore us, otherwise it will
1147      prefix the directive with spaces, hence the 1.  Ugh.  */
1148   if (pfile->cb.line_change)
1149     pfile->cb.line_change (pfile, token, 1);
1150
1151   if (p)
1152     p->u.handler (pfile);
1153   else if (pfile->cb.def_pragma)
1154     {
1155       _cpp_backup_tokens (pfile, count);
1156       pfile->cb.def_pragma (pfile, pfile->directive_line);
1157     }
1158
1159   pfile->state.prevent_expansion--;
1160 }
1161
1162 /* Handle #pragma once.  */
1163 static void
1164 do_pragma_once (cpp_reader *pfile)
1165 {
1166   if (pfile->buffer->prev == NULL)
1167     cpp_error (pfile, DL_WARNING, "#pragma once in main file");
1168
1169   check_eol (pfile);
1170   _cpp_mark_file_once_only (pfile, pfile->buffer->file, false);
1171 }
1172
1173 /* Handle #pragma GCC poison, to poison one or more identifiers so
1174    that the lexer produces a hard error for each subsequent usage.  */
1175 static void
1176 do_pragma_poison (cpp_reader *pfile)
1177 {
1178   const cpp_token *tok;
1179   cpp_hashnode *hp;
1180
1181   pfile->state.poisoned_ok = 1;
1182   for (;;)
1183     {
1184       tok = _cpp_lex_token (pfile);
1185       if (tok->type == CPP_EOF)
1186         break;
1187       if (tok->type != CPP_NAME)
1188         {
1189           cpp_error (pfile, DL_ERROR, "invalid #pragma GCC poison directive");
1190           break;
1191         }
1192
1193       hp = tok->val.node;
1194       if (hp->flags & NODE_POISONED)
1195         continue;
1196
1197       if (hp->type == NT_MACRO)
1198         cpp_error (pfile, DL_WARNING, "poisoning existing macro \"%s\"",
1199                    NODE_NAME (hp));
1200       _cpp_free_definition (hp);
1201       hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1202     }
1203   pfile->state.poisoned_ok = 0;
1204 }
1205
1206 /* Mark the current header as a system header.  This will suppress
1207    some categories of warnings (notably those from -pedantic).  It is
1208    intended for use in system libraries that cannot be implemented in
1209    conforming C, but cannot be certain that their headers appear in a
1210    system include directory.  To prevent abuse, it is rejected in the
1211    primary source file.  */
1212 static void
1213 do_pragma_system_header (cpp_reader *pfile)
1214 {
1215   cpp_buffer *buffer = pfile->buffer;
1216
1217   if (buffer->prev == 0)
1218     cpp_error (pfile, DL_WARNING,
1219                "#pragma system_header ignored outside include file");
1220   else
1221     {
1222       check_eol (pfile);
1223       skip_rest_of_line (pfile);
1224       cpp_make_system_header (pfile, 1, 0);
1225     }
1226 }
1227
1228 /* Check the modified date of the current include file against a specified
1229    file. Issue a diagnostic, if the specified file is newer. We use this to
1230    determine if a fixed header should be refixed.  */
1231 static void
1232 do_pragma_dependency (cpp_reader *pfile)
1233 {
1234   const char *fname;
1235   int angle_brackets, ordering;
1236
1237   fname = parse_include (pfile, &angle_brackets);
1238   if (!fname)
1239     return;
1240
1241   ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1242   if (ordering < 0)
1243     cpp_error (pfile, DL_WARNING, "cannot find source file %s", fname);
1244   else if (ordering > 0)
1245     {
1246       cpp_error (pfile, DL_WARNING, "current file is older than %s", fname);
1247       if (cpp_get_token (pfile)->type != CPP_EOF)
1248         {
1249           _cpp_backup_tokens (pfile, 1);
1250           do_diagnostic (pfile, DL_WARNING, 0);
1251         }
1252     }
1253
1254   free ((void *) fname);
1255 }
1256
1257 /* Get a token but skip padding.  */
1258 static const cpp_token *
1259 get_token_no_padding (cpp_reader *pfile)
1260 {
1261   for (;;)
1262     {
1263       const cpp_token *result = cpp_get_token (pfile);
1264       if (result->type != CPP_PADDING)
1265         return result;
1266     }
1267 }
1268
1269 /* Check syntax is "(string-literal)".  Returns the string on success,
1270    or NULL on failure.  */
1271 static const cpp_token *
1272 get__Pragma_string (cpp_reader *pfile)
1273 {
1274   const cpp_token *string;
1275
1276   if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1277     return NULL;
1278
1279   string = get_token_no_padding (pfile);
1280   if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1281     return NULL;
1282
1283   if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1284     return NULL;
1285
1286   return string;
1287 }
1288
1289 /* Destringize IN into a temporary buffer, by removing the first \ of
1290    \" and \\ sequences, and process the result as a #pragma directive.  */
1291 static void
1292 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1293 {
1294   const unsigned char *src, *limit;
1295   char *dest, *result;
1296
1297   dest = result = alloca (in->len - 1);
1298   src = in->text + 1 + (in->text[0] == 'L');
1299   limit = in->text + in->len - 1;
1300   while (src < limit)
1301     {
1302       /* We know there is a character following the backslash.  */
1303       if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1304         src++;
1305       *dest++ = *src++;
1306     }
1307   *dest = '\n';
1308
1309   /* Ugh; an awful kludge.  We are really not set up to be lexing
1310      tokens when in the middle of a macro expansion.  Use a new
1311      context to force cpp_get_token to lex, and so skip_rest_of_line
1312      doesn't go beyond the end of the text.  Also, remember the
1313      current lexing position so we can return to it later.
1314
1315      Something like line-at-a-time lexing should remove the need for
1316      this.  */
1317   {
1318     cpp_context *saved_context = pfile->context;
1319     cpp_token *saved_cur_token = pfile->cur_token;
1320     tokenrun *saved_cur_run = pfile->cur_run;
1321
1322     pfile->context = xnew (cpp_context);
1323     pfile->context->macro = 0;
1324     pfile->context->prev = 0;
1325     run_directive (pfile, T_PRAGMA, result, dest - result);
1326     free (pfile->context);
1327     pfile->context = saved_context;
1328     pfile->cur_token = saved_cur_token;
1329     pfile->cur_run = saved_cur_run;
1330     pfile->line--;
1331   }
1332
1333   /* See above comment.  For the moment, we'd like
1334
1335      token1 _Pragma ("foo") token2
1336
1337      to be output as
1338
1339                 token1
1340                 # 7 "file.c"
1341                 #pragma foo
1342                 # 7 "file.c"
1343                                token2
1344
1345       Getting the line markers is a little tricky.  */
1346   if (pfile->cb.line_change)
1347     pfile->cb.line_change (pfile, pfile->cur_token, false);
1348 }
1349
1350 /* Handle the _Pragma operator.  */
1351 void
1352 _cpp_do__Pragma (cpp_reader *pfile)
1353 {
1354   const cpp_token *string = get__Pragma_string (pfile);
1355
1356   if (string)
1357     destringize_and_run (pfile, &string->val.str);
1358   else
1359     cpp_error (pfile, DL_ERROR,
1360                "_Pragma takes a parenthesized string literal");
1361 }
1362
1363 /* Ignore #sccs on all systems.  */
1364 static void
1365 do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED)
1366 {
1367 }
1368
1369 /* Handle #ifdef.  */
1370 static void
1371 do_ifdef (cpp_reader *pfile)
1372 {
1373   int skip = 1;
1374
1375   if (! pfile->state.skipping)
1376     {
1377       const cpp_hashnode *node = lex_macro_node (pfile);
1378
1379       if (node)
1380         {
1381           skip = node->type != NT_MACRO;
1382           _cpp_mark_macro_used (node);
1383           check_eol (pfile);
1384         }
1385     }
1386
1387   push_conditional (pfile, skip, T_IFDEF, 0);
1388 }
1389
1390 /* Handle #ifndef.  */
1391 static void
1392 do_ifndef (cpp_reader *pfile)
1393 {
1394   int skip = 1;
1395   const cpp_hashnode *node = 0;
1396
1397   if (! pfile->state.skipping)
1398     {
1399       node = lex_macro_node (pfile);
1400
1401       if (node)
1402         {
1403           skip = node->type == NT_MACRO;
1404           _cpp_mark_macro_used (node);
1405           check_eol (pfile);
1406         }
1407     }
1408
1409   push_conditional (pfile, skip, T_IFNDEF, node);
1410 }
1411
1412 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1413    pfile->mi_ind_cmacro so we can handle multiple-include
1414    optimizations.  If macro expansion occurs in the expression, we
1415    cannot treat it as a controlling conditional, since the expansion
1416    could change in the future.  That is handled by cpp_get_token.  */
1417 static void
1418 do_if (cpp_reader *pfile)
1419 {
1420   int skip = 1;
1421
1422   if (! pfile->state.skipping)
1423     skip = _cpp_parse_expr (pfile) == false;
1424
1425   push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1426 }
1427
1428 /* Flip skipping state if appropriate and continue without changing
1429    if_stack; this is so that the error message for missing #endif's
1430    etc. will point to the original #if.  */
1431 static void
1432 do_else (cpp_reader *pfile)
1433 {
1434   cpp_buffer *buffer = pfile->buffer;
1435   struct if_stack *ifs = buffer->if_stack;
1436
1437   if (ifs == NULL)
1438     cpp_error (pfile, DL_ERROR, "#else without #if");
1439   else
1440     {
1441       if (ifs->type == T_ELSE)
1442         {
1443           cpp_error (pfile, DL_ERROR, "#else after #else");
1444           cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1445                                "the conditional began here");
1446         }
1447       ifs->type = T_ELSE;
1448
1449       /* Skip any future (erroneous) #elses or #elifs.  */
1450       pfile->state.skipping = ifs->skip_elses;
1451       ifs->skip_elses = true;
1452
1453       /* Invalidate any controlling macro.  */
1454       ifs->mi_cmacro = 0;
1455
1456       /* Only check EOL if was not originally skipping.  */
1457       if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1458         check_eol (pfile);
1459     }
1460 }
1461
1462 /* Handle a #elif directive by not changing if_stack either.  See the
1463    comment above do_else.  */
1464 static void
1465 do_elif (cpp_reader *pfile)
1466 {
1467   cpp_buffer *buffer = pfile->buffer;
1468   struct if_stack *ifs = buffer->if_stack;
1469
1470   if (ifs == NULL)
1471     cpp_error (pfile, DL_ERROR, "#elif without #if");
1472   else
1473     {
1474       if (ifs->type == T_ELSE)
1475         {
1476           cpp_error (pfile, DL_ERROR, "#elif after #else");
1477           cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1478                                "the conditional began here");
1479         }
1480       ifs->type = T_ELIF;
1481
1482       /* Only evaluate this if we aren't skipping elses.  During
1483          evaluation, set skipping to false to get lexer warnings.  */
1484       if (ifs->skip_elses)
1485         pfile->state.skipping = 1;
1486       else
1487         {
1488           pfile->state.skipping = 0;
1489           pfile->state.skipping = ! _cpp_parse_expr (pfile);
1490           ifs->skip_elses = ! pfile->state.skipping;
1491         }
1492
1493       /* Invalidate any controlling macro.  */
1494       ifs->mi_cmacro = 0;
1495     }
1496 }
1497
1498 /* #endif pops the if stack and resets pfile->state.skipping.  */
1499 static void
1500 do_endif (cpp_reader *pfile)
1501 {
1502   cpp_buffer *buffer = pfile->buffer;
1503   struct if_stack *ifs = buffer->if_stack;
1504
1505   if (ifs == NULL)
1506     cpp_error (pfile, DL_ERROR, "#endif without #if");
1507   else
1508     {
1509       /* Only check EOL if was not originally skipping.  */
1510       if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1511         check_eol (pfile);
1512
1513       /* If potential control macro, we go back outside again.  */
1514       if (ifs->next == 0 && ifs->mi_cmacro)
1515         {
1516           pfile->mi_valid = true;
1517           pfile->mi_cmacro = ifs->mi_cmacro;
1518         }
1519
1520       buffer->if_stack = ifs->next;
1521       pfile->state.skipping = ifs->was_skipping;
1522       obstack_free (&pfile->buffer_ob, ifs);
1523     }
1524 }
1525
1526 /* Push an if_stack entry for a preprocessor conditional, and set
1527    pfile->state.skipping to SKIP.  If TYPE indicates the conditional
1528    is #if or #ifndef, CMACRO is a potentially controlling macro, and
1529    we need to check here that we are at the top of the file.  */
1530 static void
1531 push_conditional (cpp_reader *pfile, int skip, int type,
1532                   const cpp_hashnode *cmacro)
1533 {
1534   struct if_stack *ifs;
1535   cpp_buffer *buffer = pfile->buffer;
1536
1537   ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1538   ifs->line = pfile->directive_line;
1539   ifs->next = buffer->if_stack;
1540   ifs->skip_elses = pfile->state.skipping || !skip;
1541   ifs->was_skipping = pfile->state.skipping;
1542   ifs->type = type;
1543   /* This condition is effectively a test for top-of-file.  */
1544   if (pfile->mi_valid && pfile->mi_cmacro == 0)
1545     ifs->mi_cmacro = cmacro;
1546   else
1547     ifs->mi_cmacro = 0;
1548
1549   pfile->state.skipping = skip;
1550   buffer->if_stack = ifs;
1551 }
1552
1553 /* Read the tokens of the answer into the macro pool, in a directive
1554    of type TYPE.  Only commit the memory if we intend it as permanent
1555    storage, i.e. the #assert case.  Returns 0 on success, and sets
1556    ANSWERP to point to the answer.  */
1557 static int
1558 parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1559 {
1560   const cpp_token *paren;
1561   struct answer *answer;
1562   unsigned int acount;
1563
1564   /* In a conditional, it is legal to not have an open paren.  We
1565      should save the following token in this case.  */
1566   paren = cpp_get_token (pfile);
1567
1568   /* If not a paren, see if we're OK.  */
1569   if (paren->type != CPP_OPEN_PAREN)
1570     {
1571       /* In a conditional no answer is a test for any answer.  It
1572          could be followed by any token.  */
1573       if (type == T_IF)
1574         {
1575           _cpp_backup_tokens (pfile, 1);
1576           return 0;
1577         }
1578
1579       /* #unassert with no answer is valid - it removes all answers.  */
1580       if (type == T_UNASSERT && paren->type == CPP_EOF)
1581         return 0;
1582
1583       cpp_error (pfile, DL_ERROR, "missing '(' after predicate");
1584       return 1;
1585     }
1586
1587   for (acount = 0;; acount++)
1588     {
1589       size_t room_needed;
1590       const cpp_token *token = cpp_get_token (pfile);
1591       cpp_token *dest;
1592
1593       if (token->type == CPP_CLOSE_PAREN)
1594         break;
1595
1596       if (token->type == CPP_EOF)
1597         {
1598           cpp_error (pfile, DL_ERROR, "missing ')' to complete answer");
1599           return 1;
1600         }
1601
1602       /* struct answer includes the space for one token.  */
1603       room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1604
1605       if (BUFF_ROOM (pfile->a_buff) < room_needed)
1606         _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1607
1608       dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1609       *dest = *token;
1610
1611       /* Drop whitespace at start, for answer equivalence purposes.  */
1612       if (acount == 0)
1613         dest->flags &= ~PREV_WHITE;
1614     }
1615
1616   if (acount == 0)
1617     {
1618       cpp_error (pfile, DL_ERROR, "predicate's answer is empty");
1619       return 1;
1620     }
1621
1622   answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1623   answer->count = acount;
1624   answer->next = NULL;
1625   *answerp = answer;
1626
1627   return 0;
1628 }
1629
1630 /* Parses an assertion directive of type TYPE, returning a pointer to
1631    the hash node of the predicate, or 0 on error.  If an answer was
1632    supplied, it is placed in ANSWERP, otherwise it is set to 0.  */
1633 static cpp_hashnode *
1634 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1635 {
1636   cpp_hashnode *result = 0;
1637   const cpp_token *predicate;
1638
1639   /* We don't expand predicates or answers.  */
1640   pfile->state.prevent_expansion++;
1641
1642   *answerp = 0;
1643   predicate = cpp_get_token (pfile);
1644   if (predicate->type == CPP_EOF)
1645     cpp_error (pfile, DL_ERROR, "assertion without predicate");
1646   else if (predicate->type != CPP_NAME)
1647     cpp_error (pfile, DL_ERROR, "predicate must be an identifier");
1648   else if (parse_answer (pfile, answerp, type) == 0)
1649     {
1650       unsigned int len = NODE_LEN (predicate->val.node);
1651       unsigned char *sym = alloca (len + 1);
1652
1653       /* Prefix '#' to get it out of macro namespace.  */
1654       sym[0] = '#';
1655       memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1656       result = cpp_lookup (pfile, sym, len + 1);
1657     }
1658
1659   pfile->state.prevent_expansion--;
1660   return result;
1661 }
1662
1663 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1664    or a pointer to NULL if the answer is not in the chain.  */
1665 static struct answer **
1666 find_answer (cpp_hashnode *node, const struct answer *candidate)
1667 {
1668   unsigned int i;
1669   struct answer **result;
1670
1671   for (result = &node->value.answers; *result; result = &(*result)->next)
1672     {
1673       struct answer *answer = *result;
1674
1675       if (answer->count == candidate->count)
1676         {
1677           for (i = 0; i < answer->count; i++)
1678             if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1679               break;
1680
1681           if (i == answer->count)
1682             break;
1683         }
1684     }
1685
1686   return result;
1687 }
1688
1689 /* Test an assertion within a preprocessor conditional.  Returns
1690    nonzero on failure, zero on success.  On success, the result of
1691    the test is written into VALUE, otherwise the value 0.  */
1692 int
1693 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
1694 {
1695   struct answer *answer;
1696   cpp_hashnode *node;
1697
1698   node = parse_assertion (pfile, &answer, T_IF);
1699
1700   /* For recovery, an erroneous assertion expression is handled as a
1701      failing assertion.  */
1702   *value = 0;
1703
1704   if (node)
1705     *value = (node->type == NT_ASSERTION &&
1706               (answer == 0 || *find_answer (node, answer) != 0));
1707   else if (pfile->cur_token[-1].type == CPP_EOF)
1708     _cpp_backup_tokens (pfile, 1);
1709
1710   /* We don't commit the memory for the answer - it's temporary only.  */
1711   return node == 0;
1712 }
1713
1714 /* Handle #assert.  */
1715 static void
1716 do_assert (cpp_reader *pfile)
1717 {
1718   struct answer *new_answer;
1719   cpp_hashnode *node;
1720
1721   node = parse_assertion (pfile, &new_answer, T_ASSERT);
1722   if (node)
1723     {
1724       /* Place the new answer in the answer list.  First check there
1725          is not a duplicate.  */
1726       new_answer->next = 0;
1727       if (node->type == NT_ASSERTION)
1728         {
1729           if (*find_answer (node, new_answer))
1730             {
1731               cpp_error (pfile, DL_WARNING, "\"%s\" re-asserted",
1732                          NODE_NAME (node) + 1);
1733               return;
1734             }
1735           new_answer->next = node->value.answers;
1736         }
1737
1738       node->type = NT_ASSERTION;
1739       node->value.answers = new_answer;
1740       BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1741                                      + (new_answer->count - 1)
1742                                      * sizeof (cpp_token));
1743       check_eol (pfile);
1744     }
1745 }
1746
1747 /* Handle #unassert.  */
1748 static void
1749 do_unassert (cpp_reader *pfile)
1750 {
1751   cpp_hashnode *node;
1752   struct answer *answer;
1753
1754   node = parse_assertion (pfile, &answer, T_UNASSERT);
1755   /* It isn't an error to #unassert something that isn't asserted.  */
1756   if (node && node->type == NT_ASSERTION)
1757     {
1758       if (answer)
1759         {
1760           struct answer **p = find_answer (node, answer), *temp;
1761
1762           /* Remove the answer from the list.  */
1763           temp = *p;
1764           if (temp)
1765             *p = temp->next;
1766
1767           /* Did we free the last answer?  */
1768           if (node->value.answers == 0)
1769             node->type = NT_VOID;
1770
1771           check_eol (pfile);
1772         }
1773       else
1774         _cpp_free_definition (node);
1775     }
1776
1777   /* We don't commit the memory for the answer - it's temporary only.  */
1778 }
1779
1780 /* These are for -D, -U, -A.  */
1781
1782 /* Process the string STR as if it appeared as the body of a #define.
1783    If STR is just an identifier, define it with value 1.
1784    If STR has anything after the identifier, then it should
1785    be identifier=definition.  */
1786 void
1787 cpp_define (cpp_reader *pfile, const char *str)
1788 {
1789   char *buf, *p;
1790   size_t count;
1791
1792   /* Copy the entire option so we can modify it.
1793      Change the first "=" in the string to a space.  If there is none,
1794      tack " 1" on the end.  */
1795
1796   count = strlen (str);
1797   buf = alloca (count + 3);
1798   memcpy (buf, str, count);
1799
1800   p = strchr (str, '=');
1801   if (p)
1802     buf[p - str] = ' ';
1803   else
1804     {
1805       buf[count++] = ' ';
1806       buf[count++] = '1';
1807     }
1808   buf[count] = '\n';
1809
1810   run_directive (pfile, T_DEFINE, buf, count);
1811 }
1812
1813 /* Slight variant of the above for use by initialize_builtins.  */
1814 void
1815 _cpp_define_builtin (cpp_reader *pfile, const char *str)
1816 {
1817   size_t len = strlen (str);
1818   char *buf = alloca (len + 1);
1819   memcpy (buf, str, len);
1820   buf[len] = '\n';
1821   run_directive (pfile, T_DEFINE, buf, len);
1822 }
1823
1824 /* Process MACRO as if it appeared as the body of an #undef.  */
1825 void
1826 cpp_undef (cpp_reader *pfile, const char *macro)
1827 {
1828   size_t len = strlen (macro);
1829   char *buf = alloca (len + 1);
1830   memcpy (buf, macro, len);
1831   buf[len] = '\n';
1832   run_directive (pfile, T_UNDEF, buf, len);
1833 }
1834
1835 /* Process the string STR as if it appeared as the body of a #assert.  */
1836 void
1837 cpp_assert (cpp_reader *pfile, const char *str)
1838 {
1839   handle_assertion (pfile, str, T_ASSERT);
1840 }
1841
1842 /* Process STR as if it appeared as the body of an #unassert.  */
1843 void
1844 cpp_unassert (cpp_reader *pfile, const char *str)
1845 {
1846   handle_assertion (pfile, str, T_UNASSERT);
1847 }
1848
1849 /* Common code for cpp_assert (-A) and cpp_unassert (-A-).  */
1850 static void
1851 handle_assertion (cpp_reader *pfile, const char *str, int type)
1852 {
1853   size_t count = strlen (str);
1854   const char *p = strchr (str, '=');
1855
1856   /* Copy the entire option so we can modify it.  Change the first
1857      "=" in the string to a '(', and tack a ')' on the end.  */
1858   char *buf = alloca (count + 2);
1859
1860   memcpy (buf, str, count);
1861   if (p)
1862     {
1863       buf[p - str] = '(';
1864       buf[count++] = ')';
1865     }
1866   buf[count] = '\n';
1867   str = buf;
1868
1869   run_directive (pfile, type, str, count);
1870 }
1871
1872 /* The number of errors for a given reader.  */
1873 unsigned int
1874 cpp_errors (cpp_reader *pfile)
1875 {
1876   return pfile->errors;
1877 }
1878
1879 /* The options structure.  */
1880 cpp_options *
1881 cpp_get_options (cpp_reader *pfile)
1882 {
1883   return &pfile->opts;
1884 }
1885
1886 /* The callbacks structure.  */
1887 cpp_callbacks *
1888 cpp_get_callbacks (cpp_reader *pfile)
1889 {
1890   return &pfile->cb;
1891 }
1892
1893 /* The line map set.  */
1894 const struct line_maps *
1895 cpp_get_line_maps (cpp_reader *pfile)
1896 {
1897   return &pfile->line_maps;
1898 }
1899
1900 /* Copy the given callbacks structure to our own.  */
1901 void
1902 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
1903 {
1904   pfile->cb = *cb;
1905 }
1906
1907 /* Push a new buffer on the buffer stack.  Returns the new buffer; it
1908    doesn't fail.  It does not generate a file change call back; that
1909    is the responsibility of the caller.  */
1910 cpp_buffer *
1911 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
1912                  int from_stage3, int return_at_eof)
1913 {
1914   cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1915
1916   /* Clears, amongst other things, if_stack and mi_cmacro.  */
1917   memset (new, 0, sizeof (cpp_buffer));
1918
1919   new->next_line = new->buf = buffer;
1920   new->rlimit = buffer + len;
1921   new->from_stage3 = from_stage3;
1922   new->prev = pfile->buffer;
1923   new->return_at_eof = return_at_eof;
1924   new->need_line = true;
1925
1926   pfile->buffer = new;
1927   return new;
1928 }
1929
1930 /* Pops a single buffer, with a file change call-back if appropriate.
1931    Then pushes the next -include file, if any remain.  */
1932 void
1933 _cpp_pop_buffer (cpp_reader *pfile)
1934 {
1935   cpp_buffer *buffer = pfile->buffer;
1936   struct _cpp_file *inc = buffer->file;
1937   struct if_stack *ifs;
1938
1939   /* Walk back up the conditional stack till we reach its level at
1940      entry to this file, issuing error messages.  */
1941   for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1942     cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1943                          "unterminated #%s", dtable[ifs->type].name);
1944
1945   /* In case of a missing #endif.  */
1946   pfile->state.skipping = 0;
1947
1948   /* _cpp_do_file_change expects pfile->buffer to be the new one.  */
1949   pfile->buffer = buffer->prev;
1950
1951   free (buffer->notes);
1952
1953   /* Free the buffer object now; we may want to push a new buffer
1954      in _cpp_push_next_include_file.  */
1955   obstack_free (&pfile->buffer_ob, buffer);
1956
1957   if (inc)
1958     {
1959       _cpp_pop_file_buffer (pfile, inc);
1960
1961       /* Don't generate a callback for popping the main file.  */
1962       if (pfile->buffer)
1963         _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
1964     }
1965 }
1966
1967 /* Enter all recognized directives in the hash table.  */
1968 void
1969 _cpp_init_directives (cpp_reader *pfile)
1970 {
1971   unsigned int i;
1972   cpp_hashnode *node;
1973
1974   for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1975     {
1976       node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1977       node->is_directive = 1;
1978       node->directive_index = i;
1979     }
1980 }