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