OSDN Git Service

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