OSDN Git Service

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