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