1169695Skan/* CPP Library. (Directive handling.)
2169695Skan   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3169695Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4169695Skan   Contributed by Per Bothner, 1994-95.
5169695Skan   Based on CCCP program by Paul Rubin, June 1986
6169695Skan   Adapted to ANSI C, Richard Stallman, Jan 1987
7169695Skan
8169695SkanThis program is free software; you can redistribute it and/or modify it
9169695Skanunder the terms of the GNU General Public License as published by the
10169695SkanFree Software Foundation; either version 2, or (at your option) any
11169695Skanlater version.
12169695Skan
13169695SkanThis program is distributed in the hope that it will be useful,
14169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
15169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16169695SkanGNU General Public License for more details.
17169695Skan
18169695SkanYou should have received a copy of the GNU General Public License
19169695Skanalong with this program; if not, write to the Free Software
20169695SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
21169695Skan
22169695Skan#include "config.h"
23169695Skan#include "system.h"
24169695Skan#include "cpplib.h"
25169695Skan#include "internal.h"
26169695Skan#include "mkdeps.h"
27169695Skan#include "obstack.h"
28169695Skan
29169695Skan/* Stack of conditionals currently in progress
30169695Skan   (including both successful and failing conditionals).  */
31169695Skanstruct if_stack
32169695Skan{
33169695Skan  struct if_stack *next;
34169695Skan  unsigned int line;		/* Line where condition started.  */
35169695Skan  const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
36169695Skan  bool skip_elses;		/* Can future #else / #elif be skipped?  */
37169695Skan  bool was_skipping;		/* If were skipping on entry.  */
38169695Skan  int type;			/* Most recent conditional for diagnostics.  */
39169695Skan};
40169695Skan
41169695Skan/* Contains a registered pragma or pragma namespace.  */
42169695Skantypedef void (*pragma_cb) (cpp_reader *);
43169695Skanstruct pragma_entry
44169695Skan{
45169695Skan  struct pragma_entry *next;
46169695Skan  const cpp_hashnode *pragma;	/* Name and length.  */
47169695Skan  bool is_nspace;
48169695Skan  bool is_internal;
49169695Skan  bool is_deferred;
50169695Skan  bool allow_expansion;
51169695Skan  union {
52169695Skan    pragma_cb handler;
53169695Skan    struct pragma_entry *space;
54169695Skan    unsigned int ident;
55169695Skan  } u;
56169695Skan};
57169695Skan
58169695Skan/* Values for the origin field of struct directive.  KANDR directives
59169695Skan   come from traditional (K&R) C.  STDC89 directives come from the
60169695Skan   1989 C standard.  EXTENSION directives are extensions.  */
61169695Skan#define KANDR		0
62169695Skan#define STDC89		1
63169695Skan#define EXTENSION	2
64169695Skan
65169695Skan/* Values for the flags field of struct directive.  COND indicates a
66169695Skan   conditional; IF_COND an opening conditional.  INCL means to treat
67169695Skan   "..." and <...> as q-char and h-char sequences respectively.  IN_I
68169695Skan   means this directive should be handled even if -fpreprocessed is in
69169695Skan   effect (these are the directives with callback hooks).
70169695Skan
71169695Skan   EXPAND is set on directives that are always macro-expanded.  */
72169695Skan#define COND		(1 << 0)
73169695Skan#define IF_COND		(1 << 1)
74169695Skan#define INCL		(1 << 2)
75169695Skan#define IN_I		(1 << 3)
76169695Skan#define EXPAND		(1 << 4)
77169695Skan
78169695Skan/* Defines one #-directive, including how to handle it.  */
79169695Skantypedef void (*directive_handler) (cpp_reader *);
80169695Skantypedef struct directive directive;
81169695Skanstruct directive
82169695Skan{
83169695Skan  directive_handler handler;	/* Function to handle directive.  */
84169695Skan  const uchar *name;		/* Name of directive.  */
85169695Skan  unsigned short length;	/* Length of name.  */
86169695Skan  unsigned char origin;		/* Origin of directive.  */
87169695Skan  unsigned char flags;	        /* Flags describing this directive.  */
88169695Skan};
89169695Skan
90169695Skan/* Forward declarations.  */
91169695Skan
92169695Skanstatic void skip_rest_of_line (cpp_reader *);
93169695Skanstatic void check_eol (cpp_reader *);
94169695Skanstatic void start_directive (cpp_reader *);
95169695Skanstatic void prepare_directive_trad (cpp_reader *);
96169695Skanstatic void end_directive (cpp_reader *, int);
97169695Skanstatic void directive_diagnostics (cpp_reader *, const directive *, int);
98169695Skanstatic void run_directive (cpp_reader *, int, const char *, size_t);
99169695Skanstatic char *glue_header_name (cpp_reader *);
100169695Skanstatic const char *parse_include (cpp_reader *, int *, const cpp_token ***);
101169695Skanstatic void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
102169695Skanstatic unsigned int read_flag (cpp_reader *, unsigned int);
103169695Skanstatic int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
104169695Skanstatic void do_diagnostic (cpp_reader *, int, int);
105169695Skanstatic cpp_hashnode *lex_macro_node (cpp_reader *);
106169695Skanstatic int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
107169695Skanstatic void do_include_common (cpp_reader *, enum include_type);
108169695Skanstatic struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
109169695Skan                                                 const cpp_hashnode *);
110169695Skanstatic int count_registered_pragmas (struct pragma_entry *);
111169695Skanstatic char ** save_registered_pragmas (struct pragma_entry *, char **);
112169695Skanstatic char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
113169695Skan                                           char **);
114169695Skanstatic void do_pragma_once (cpp_reader *);
115169695Skanstatic void do_pragma_poison (cpp_reader *);
116169695Skanstatic void do_pragma_system_header (cpp_reader *);
117169695Skanstatic void do_pragma_dependency (cpp_reader *);
118169695Skanstatic void do_linemarker (cpp_reader *);
119169695Skanstatic const cpp_token *get_token_no_padding (cpp_reader *);
120169695Skanstatic const cpp_token *get__Pragma_string (cpp_reader *);
121169695Skanstatic void destringize_and_run (cpp_reader *, const cpp_string *);
122169695Skanstatic int parse_answer (cpp_reader *, struct answer **, int);
123169695Skanstatic cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
124169695Skanstatic struct answer ** find_answer (cpp_hashnode *, const struct answer *);
125169695Skanstatic void handle_assertion (cpp_reader *, const char *, int);
126169695Skan
127169695Skan/* This is the table of directive handlers.  It is ordered by
128169695Skan   frequency of occurrence; the numbers at the end are directive
129169695Skan   counts from all the source code I have lying around (egcs and libc
130169695Skan   CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
131169695Skan   pcmcia-cs-3.0.9).  This is no longer important as directive lookup
132169695Skan   is now O(1).  All extensions other than #warning and #include_next
133169695Skan   are deprecated.  The name is where the extension appears to have
134169695Skan   come from.  */
135169695Skan
136169695Skan#define DIRECTIVE_TABLE							\
137169695SkanD(define,	T_DEFINE = 0,	KANDR,     IN_I)	   /* 270554 */ \
138169695SkanD(include,	T_INCLUDE,	KANDR,     INCL | EXPAND)  /*  52262 */ \
139169695SkanD(endif,	T_ENDIF,	KANDR,     COND)	   /*  45855 */ \
140169695SkanD(ifdef,	T_IFDEF,	KANDR,     COND | IF_COND) /*  22000 */ \
141169695SkanD(if,		T_IF,		KANDR, COND | IF_COND | EXPAND) /*  18162 */ \
142169695SkanD(else,		T_ELSE,		KANDR,     COND)	   /*   9863 */ \
143169695SkanD(ifndef,	T_IFNDEF,	KANDR,     COND | IF_COND) /*   9675 */ \
144169695SkanD(undef,	T_UNDEF,	KANDR,     IN_I)	   /*   4837 */ \
145169695SkanD(line,		T_LINE,		KANDR,     EXPAND)	   /*   2465 */ \
146169695SkanD(elif,		T_ELIF,		STDC89,    COND | EXPAND)  /*    610 */ \
147169695SkanD(error,	T_ERROR,	STDC89,    0)		   /*    475 */ \
148169695SkanD(pragma,	T_PRAGMA,	STDC89,    IN_I)	   /*    195 */ \
149169695SkanD(warning,	T_WARNING,	EXTENSION, 0)		   /*     22 */ \
150169695SkanD(include_next,	T_INCLUDE_NEXT,	EXTENSION, INCL | EXPAND)  /*     19 */ \
151169695SkanD(ident,	T_IDENT,	EXTENSION, IN_I)	   /*     11 */ \
152169695SkanD(import,	T_IMPORT,	EXTENSION, INCL | EXPAND)  /* 0 ObjC */	\
153169695SkanD(assert,	T_ASSERT,	EXTENSION, 0)		   /* 0 SVR4 */	\
154169695SkanD(unassert,	T_UNASSERT,	EXTENSION, 0)		   /* 0 SVR4 */	\
155169695SkanD(sccs,		T_SCCS,		EXTENSION, IN_I)	   /* 0 SVR4? */
156169695Skan
157169695Skan/* #sccs is synonymous with #ident.  */
158169695Skan#define do_sccs do_ident
159169695Skan
160169695Skan/* Use the table to generate a series of prototypes, an enum for the
161169695Skan   directive names, and an array of directive handlers.  */
162169695Skan
163169695Skan#define D(name, t, o, f) static void do_##name (cpp_reader *);
164169695SkanDIRECTIVE_TABLE
165169695Skan#undef D
166169695Skan
167169695Skan#define D(n, tag, o, f) tag,
168169695Skanenum
169169695Skan{
170169695Skan  DIRECTIVE_TABLE
171169695Skan  N_DIRECTIVES
172169695Skan};
173169695Skan#undef D
174169695Skan
175169695Skan#define D(name, t, origin, flags) \
176169695Skan{ do_##name, (const uchar *) #name, \
177169695Skan  sizeof #name - 1, origin, flags },
178169695Skanstatic const directive dtable[] =
179169695Skan{
180169695SkanDIRECTIVE_TABLE
181169695Skan};
182169695Skan#undef D
183169695Skan#undef DIRECTIVE_TABLE
184169695Skan
185169695Skan/* Wrapper struct directive for linemarkers.
186169695Skan   The origin is more or less true - the original K+R cpp
187169695Skan   did use this notation in its preprocessed output.  */
188169695Skanstatic const directive linemarker_dir =
189169695Skan{
190169695Skan  do_linemarker, U"#", 1, KANDR, IN_I
191169695Skan};
192169695Skan
193169695Skan#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
194169695Skan
195169695Skan/* Skip any remaining tokens in a directive.  */
196169695Skanstatic void
197169695Skanskip_rest_of_line (cpp_reader *pfile)
198169695Skan{
199169695Skan  /* Discard all stacked contexts.  */
200169695Skan  while (pfile->context->prev)
201169695Skan    _cpp_pop_context (pfile);
202169695Skan
203169695Skan  /* Sweep up all tokens remaining on the line.  */
204169695Skan  if (! SEEN_EOL ())
205169695Skan    while (_cpp_lex_token (pfile)->type != CPP_EOF)
206169695Skan      ;
207169695Skan}
208169695Skan
209169695Skan/* Ensure there are no stray tokens at the end of a directive.  */
210169695Skanstatic void
211169695Skancheck_eol (cpp_reader *pfile)
212169695Skan{
213169695Skan  if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
214169695Skan    cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
215169695Skan	       pfile->directive->name);
216169695Skan}
217169695Skan
218169695Skan/* Ensure there are no stray tokens other than comments at the end of
219169695Skan   a directive, and gather the comments.  */
220169695Skanstatic const cpp_token **
221169695Skancheck_eol_return_comments (cpp_reader *pfile)
222169695Skan{
223169695Skan  size_t c;
224169695Skan  size_t capacity = 8;
225169695Skan  const cpp_token **buf;
226169695Skan
227169695Skan  buf = XNEWVEC (const cpp_token *, capacity);
228169695Skan  c = 0;
229169695Skan  if (! SEEN_EOL ())
230169695Skan    {
231169695Skan      while (1)
232169695Skan	{
233169695Skan	  const cpp_token *tok;
234169695Skan
235169695Skan	  tok = _cpp_lex_token (pfile);
236169695Skan	  if (tok->type == CPP_EOF)
237169695Skan	    break;
238169695Skan	  if (tok->type != CPP_COMMENT)
239169695Skan	    cpp_error (pfile, CPP_DL_PEDWARN,
240169695Skan		       "extra tokens at end of #%s directive",
241169695Skan		       pfile->directive->name);
242169695Skan	  else
243169695Skan	    {
244169695Skan	      if (c + 1 >= capacity)
245169695Skan		{
246169695Skan		  capacity *= 2;
247169695Skan		  buf = XRESIZEVEC (const cpp_token *, buf, capacity);
248169695Skan		}
249169695Skan	      buf[c] = tok;
250169695Skan	      ++c;
251169695Skan	    }
252169695Skan	}
253169695Skan    }
254169695Skan  buf[c] = NULL;
255169695Skan  return buf;
256169695Skan}
257169695Skan
258169695Skan/* Called when entering a directive, _Pragma or command-line directive.  */
259169695Skanstatic void
260169695Skanstart_directive (cpp_reader *pfile)
261169695Skan{
262169695Skan  /* Setup in-directive state.  */
263169695Skan  pfile->state.in_directive = 1;
264169695Skan  pfile->state.save_comments = 0;
265169695Skan  pfile->directive_result.type = CPP_PADDING;
266169695Skan
267169695Skan  /* Some handlers need the position of the # for diagnostics.  */
268169695Skan  pfile->directive_line = pfile->line_table->highest_line;
269169695Skan}
270169695Skan
271169695Skan/* Called when leaving a directive, _Pragma or command-line directive.  */
272169695Skanstatic void
273169695Skanend_directive (cpp_reader *pfile, int skip_line)
274169695Skan{
275169695Skan  if (pfile->state.in_deferred_pragma)
276169695Skan    ;
277169695Skan  else if (CPP_OPTION (pfile, traditional))
278169695Skan    {
279169695Skan      /* Revert change of prepare_directive_trad.  */
280169695Skan      pfile->state.prevent_expansion--;
281169695Skan
282169695Skan      if (pfile->directive != &dtable[T_DEFINE])
283169695Skan	_cpp_remove_overlay (pfile);
284169695Skan    }
285169695Skan  /* We don't skip for an assembler #.  */
286169695Skan  else if (skip_line)
287169695Skan    {
288169695Skan      skip_rest_of_line (pfile);
289169695Skan      if (!pfile->keep_tokens)
290169695Skan	{
291169695Skan	  pfile->cur_run = &pfile->base_run;
292169695Skan	  pfile->cur_token = pfile->base_run.base;
293169695Skan	}
294169695Skan    }
295169695Skan
296169695Skan  /* Restore state.  */
297169695Skan  pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
298169695Skan  pfile->state.in_directive = 0;
299169695Skan  pfile->state.in_expression = 0;
300169695Skan  pfile->state.angled_headers = 0;
301169695Skan  pfile->directive = 0;
302169695Skan}
303169695Skan
304169695Skan/* Prepare to handle the directive in pfile->directive.  */
305169695Skanstatic void
306169695Skanprepare_directive_trad (cpp_reader *pfile)
307169695Skan{
308169695Skan  if (pfile->directive != &dtable[T_DEFINE])
309169695Skan    {
310169695Skan      bool no_expand = (pfile->directive
311169695Skan			&& ! (pfile->directive->flags & EXPAND));
312169695Skan      bool was_skipping = pfile->state.skipping;
313169695Skan
314169695Skan      pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
315169695Skan				    || pfile->directive == &dtable[T_ELIF]);
316169695Skan      if (pfile->state.in_expression)
317169695Skan	pfile->state.skipping = false;
318169695Skan
319169695Skan      if (no_expand)
320169695Skan	pfile->state.prevent_expansion++;
321169695Skan      _cpp_scan_out_logical_line (pfile, NULL);
322169695Skan      if (no_expand)
323169695Skan	pfile->state.prevent_expansion--;
324169695Skan
325169695Skan      pfile->state.skipping = was_skipping;
326169695Skan      _cpp_overlay_buffer (pfile, pfile->out.base,
327169695Skan			   pfile->out.cur - pfile->out.base);
328169695Skan    }
329169695Skan
330169695Skan  /* Stop ISO C from expanding anything.  */
331169695Skan  pfile->state.prevent_expansion++;
332169695Skan}
333169695Skan
334169695Skan/* Output diagnostics for a directive DIR.  INDENTED is nonzero if
335169695Skan   the '#' was indented.  */
336169695Skanstatic void
337169695Skandirective_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
338169695Skan{
339169695Skan  /* Issue -pedantic warnings for extensions.  */
340169695Skan  if (CPP_PEDANTIC (pfile)
341169695Skan      && ! pfile->state.skipping
342169695Skan      && dir->origin == EXTENSION)
343169695Skan    cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
344169695Skan
345169695Skan  /* Traditionally, a directive is ignored unless its # is in
346169695Skan     column 1.  Therefore in code intended to work with K+R
347169695Skan     compilers, directives added by C89 must have their #
348169695Skan     indented, and directives present in traditional C must not.
349169695Skan     This is true even of directives in skipped conditional
350169695Skan     blocks.  #elif cannot be used at all.  */
351169695Skan  if (CPP_WTRADITIONAL (pfile))
352169695Skan    {
353169695Skan      if (dir == &dtable[T_ELIF])
354169695Skan	cpp_error (pfile, CPP_DL_WARNING,
355169695Skan		   "suggest not using #elif in traditional C");
356169695Skan      else if (indented && dir->origin == KANDR)
357169695Skan	cpp_error (pfile, CPP_DL_WARNING,
358169695Skan		   "traditional C ignores #%s with the # indented",
359169695Skan		   dir->name);
360169695Skan      else if (!indented && dir->origin != KANDR)
361169695Skan	cpp_error (pfile, CPP_DL_WARNING,
362169695Skan		   "suggest hiding #%s from traditional C with an indented #",
363169695Skan		   dir->name);
364169695Skan    }
365169695Skan}
366169695Skan
367169695Skan/* Check if we have a known directive.  INDENTED is nonzero if the
368169695Skan   '#' of the directive was indented.  This function is in this file
369169695Skan   to save unnecessarily exporting dtable etc. to lex.c.  Returns
370169695Skan   nonzero if the line of tokens has been handled, zero if we should
371169695Skan   continue processing the line.  */
372169695Skanint
373169695Skan_cpp_handle_directive (cpp_reader *pfile, int indented)
374169695Skan{
375169695Skan  const directive *dir = 0;
376169695Skan  const cpp_token *dname;
377169695Skan  bool was_parsing_args = pfile->state.parsing_args;
378169695Skan  bool was_discarding_output = pfile->state.discarding_output;
379169695Skan  int skip = 1;
380169695Skan
381169695Skan  if (was_discarding_output)
382169695Skan    pfile->state.prevent_expansion = 0;
383169695Skan
384169695Skan  if (was_parsing_args)
385169695Skan    {
386169695Skan      if (CPP_OPTION (pfile, pedantic))
387169695Skan	cpp_error (pfile, CPP_DL_PEDWARN,
388169695Skan	     "embedding a directive within macro arguments is not portable");
389169695Skan      pfile->state.parsing_args = 0;
390169695Skan      pfile->state.prevent_expansion = 0;
391169695Skan    }
392169695Skan  start_directive (pfile);
393169695Skan  dname = _cpp_lex_token (pfile);
394169695Skan
395169695Skan  if (dname->type == CPP_NAME)
396169695Skan    {
397169695Skan      if (dname->val.node->is_directive)
398169695Skan	dir = &dtable[dname->val.node->directive_index];
399169695Skan    }
400169695Skan  /* We do not recognize the # followed by a number extension in
401169695Skan     assembler code.  */
402169695Skan  else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
403169695Skan    {
404169695Skan      dir = &linemarker_dir;
405169695Skan      if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
406169695Skan	  && ! pfile->state.skipping)
407169695Skan	cpp_error (pfile, CPP_DL_PEDWARN,
408169695Skan		   "style of line directive is a GCC extension");
409169695Skan    }
410169695Skan
411169695Skan  if (dir)
412169695Skan    {
413169695Skan      /* If we have a directive that is not an opening conditional,
414169695Skan	 invalidate any control macro.  */
415169695Skan      if (! (dir->flags & IF_COND))
416169695Skan	pfile->mi_valid = false;
417169695Skan
418169695Skan      /* Kluge alert.  In order to be sure that code like this
419169695Skan
420169695Skan	 #define HASH #
421169695Skan	 HASH define foo bar
422169695Skan
423169695Skan	 does not cause '#define foo bar' to get executed when
424169695Skan	 compiled with -save-temps, we recognize directives in
425169695Skan	 -fpreprocessed mode only if the # is in column 1.  macro.c
426258501Spfg	 puts a space in front of any '#' at the start of a macro.
427258501Spfg
428258501Spfg	 We exclude the -fdirectives-only case because macro expansion
429258501Spfg	 has not been performed yet, and block comments can cause spaces
430258501Spfg	 to preceed the directive.  */
431169695Skan      if (CPP_OPTION (pfile, preprocessed)
432258501Spfg	  && !CPP_OPTION (pfile, directives_only)
433169695Skan	  && (indented || !(dir->flags & IN_I)))
434169695Skan	{
435169695Skan	  skip = 0;
436169695Skan	  dir = 0;
437169695Skan	}
438169695Skan      else
439169695Skan	{
440169695Skan	  /* In failed conditional groups, all non-conditional
441169695Skan	     directives are ignored.  Before doing that, whether
442169695Skan	     skipping or not, we should lex angle-bracketed headers
443169695Skan	     correctly, and maybe output some diagnostics.  */
444169695Skan	  pfile->state.angled_headers = dir->flags & INCL;
445169695Skan	  pfile->state.directive_wants_padding = dir->flags & INCL;
446169695Skan	  if (! CPP_OPTION (pfile, preprocessed))
447169695Skan	    directive_diagnostics (pfile, dir, indented);
448169695Skan	  if (pfile->state.skipping && !(dir->flags & COND))
449169695Skan	    dir = 0;
450169695Skan	}
451169695Skan    }
452169695Skan  else if (dname->type == CPP_EOF)
453169695Skan    ;	/* CPP_EOF is the "null directive".  */
454169695Skan  else
455169695Skan    {
456169695Skan      /* An unknown directive.  Don't complain about it in assembly
457169695Skan	 source: we don't know where the comments are, and # may
458169695Skan	 introduce assembler pseudo-ops.  Don't complain about invalid
459169695Skan	 directives in skipped conditional groups (6.10 p4).  */
460169695Skan      if (CPP_OPTION (pfile, lang) == CLK_ASM)
461169695Skan	skip = 0;
462169695Skan      else if (!pfile->state.skipping)
463169695Skan	cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
464169695Skan		   cpp_token_as_text (pfile, dname));
465169695Skan    }
466169695Skan
467169695Skan  pfile->directive = dir;
468169695Skan  if (CPP_OPTION (pfile, traditional))
469169695Skan    prepare_directive_trad (pfile);
470169695Skan
471169695Skan  if (dir)
472169695Skan    pfile->directive->handler (pfile);
473169695Skan  else if (skip == 0)
474169695Skan    _cpp_backup_tokens (pfile, 1);
475169695Skan
476169695Skan  end_directive (pfile, skip);
477169695Skan  if (was_parsing_args)
478169695Skan    {
479169695Skan      /* Restore state when within macro args.  */
480169695Skan      pfile->state.parsing_args = 2;
481169695Skan      pfile->state.prevent_expansion = 1;
482169695Skan    }
483169695Skan  if (was_discarding_output)
484169695Skan    pfile->state.prevent_expansion = 1;
485169695Skan  return skip;
486169695Skan}
487169695Skan
488169695Skan/* Directive handler wrapper used by the command line option
489169695Skan   processor.  BUF is \n terminated.  */
490169695Skanstatic void
491169695Skanrun_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
492169695Skan{
493169695Skan  cpp_push_buffer (pfile, (const uchar *) buf, count,
494169695Skan		   /* from_stage3 */ true);
495169695Skan  start_directive (pfile);
496169695Skan
497169695Skan  /* This is a short-term fix to prevent a leading '#' being
498169695Skan     interpreted as a directive.  */
499169695Skan  _cpp_clean_line (pfile);
500169695Skan
501169695Skan  pfile->directive = &dtable[dir_no];
502169695Skan  if (CPP_OPTION (pfile, traditional))
503169695Skan    prepare_directive_trad (pfile);
504169695Skan  pfile->directive->handler (pfile);
505169695Skan  end_directive (pfile, 1);
506169695Skan  _cpp_pop_buffer (pfile);
507169695Skan}
508169695Skan
509169695Skan/* Checks for validity the macro name in #define, #undef, #ifdef and
510169695Skan   #ifndef directives.  */
511169695Skanstatic cpp_hashnode *
512169695Skanlex_macro_node (cpp_reader *pfile)
513169695Skan{
514169695Skan  const cpp_token *token = _cpp_lex_token (pfile);
515169695Skan
516169695Skan  /* The token immediately after #define must be an identifier.  That
517169695Skan     identifier may not be "defined", per C99 6.10.8p4.
518169695Skan     In C++, it may not be any of the "named operators" either,
519169695Skan     per C++98 [lex.digraph], [lex.key].
520169695Skan     Finally, the identifier may not have been poisoned.  (In that case
521169695Skan     the lexer has issued the error message for us.)  */
522169695Skan
523169695Skan  if (token->type == CPP_NAME)
524169695Skan    {
525169695Skan      cpp_hashnode *node = token->val.node;
526169695Skan
527169695Skan      if (node == pfile->spec_nodes.n_defined)
528169695Skan	cpp_error (pfile, CPP_DL_ERROR,
529169695Skan		   "\"defined\" cannot be used as a macro name");
530169695Skan      else if (! (node->flags & NODE_POISONED))
531169695Skan	return node;
532169695Skan    }
533169695Skan  else if (token->flags & NAMED_OP)
534169695Skan    cpp_error (pfile, CPP_DL_ERROR,
535169695Skan       "\"%s\" cannot be used as a macro name as it is an operator in C++",
536169695Skan	       NODE_NAME (token->val.node));
537169695Skan  else if (token->type == CPP_EOF)
538169695Skan    cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
539169695Skan	       pfile->directive->name);
540169695Skan  else
541169695Skan    cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
542169695Skan
543169695Skan  return NULL;
544169695Skan}
545169695Skan
546169695Skan/* Process a #define directive.  Most work is done in macro.c.  */
547169695Skanstatic void
548169695Skando_define (cpp_reader *pfile)
549169695Skan{
550169695Skan  cpp_hashnode *node = lex_macro_node (pfile);
551169695Skan
552169695Skan  if (node)
553169695Skan    {
554169695Skan      /* If we have been requested to expand comments into macros,
555169695Skan	 then re-enable saving of comments.  */
556169695Skan      pfile->state.save_comments =
557169695Skan	! CPP_OPTION (pfile, discard_comments_in_macro_exp);
558169695Skan
559169695Skan      if (_cpp_create_definition (pfile, node))
560169695Skan	if (pfile->cb.define)
561169695Skan	  pfile->cb.define (pfile, pfile->directive_line, node);
562169695Skan    }
563169695Skan}
564169695Skan
565169695Skan/* Handle #undef.  Mark the identifier NT_VOID in the hash table.  */
566169695Skanstatic void
567169695Skando_undef (cpp_reader *pfile)
568169695Skan{
569169695Skan  cpp_hashnode *node = lex_macro_node (pfile);
570169695Skan
571169695Skan  if (node)
572169695Skan    {
573169695Skan      if (pfile->cb.undef)
574169695Skan	pfile->cb.undef (pfile, pfile->directive_line, node);
575169695Skan
576169695Skan      /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
577169695Skan	 identifier is not currently defined as a macro name.  */
578169695Skan      if (node->type == NT_MACRO)
579169695Skan	{
580169695Skan	  if (node->flags & NODE_WARN)
581169695Skan	    cpp_error (pfile, CPP_DL_WARNING,
582169695Skan		       "undefining \"%s\"", NODE_NAME (node));
583169695Skan
584169695Skan	  if (CPP_OPTION (pfile, warn_unused_macros))
585169695Skan	    _cpp_warn_if_unused_macro (pfile, node, NULL);
586169695Skan
587169695Skan	  _cpp_free_definition (node);
588169695Skan	}
589169695Skan    }
590169695Skan
591169695Skan  check_eol (pfile);
592169695Skan}
593169695Skan
594169695Skan/* Undefine a single macro/assertion/whatever.  */
595169695Skan
596169695Skanstatic int
597169695Skanundefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
598169695Skan		 void *data_p ATTRIBUTE_UNUSED)
599169695Skan{
600169695Skan  /* Body of _cpp_free_definition inlined here for speed.
601169695Skan     Macros and assertions no longer have anything to free.  */
602169695Skan  h->type = NT_VOID;
603169695Skan  h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
604169695Skan  return 1;
605169695Skan}
606169695Skan
607169695Skan/* Undefine all macros and assertions.  */
608169695Skan
609169695Skanvoid
610169695Skancpp_undef_all (cpp_reader *pfile)
611169695Skan{
612169695Skan  cpp_forall_identifiers (pfile, undefine_macros, NULL);
613169695Skan}
614169695Skan
615169695Skan
616169695Skan/* Helper routine used by parse_include.  Reinterpret the current line
617169695Skan   as an h-char-sequence (< ... >); we are looking at the first token
618169695Skan   after the <.  Returns a malloced filename.  */
619169695Skanstatic char *
620169695Skanglue_header_name (cpp_reader *pfile)
621169695Skan{
622169695Skan  const cpp_token *token;
623169695Skan  char *buffer;
624169695Skan  size_t len, total_len = 0, capacity = 1024;
625169695Skan
626169695Skan  /* To avoid lexed tokens overwriting our glued name, we can only
627169695Skan     allocate from the string pool once we've lexed everything.  */
628169695Skan  buffer = XNEWVEC (char, capacity);
629169695Skan  for (;;)
630169695Skan    {
631169695Skan      token = get_token_no_padding (pfile);
632169695Skan
633169695Skan      if (token->type == CPP_GREATER)
634169695Skan	break;
635169695Skan      if (token->type == CPP_EOF)
636169695Skan	{
637169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
638169695Skan	  break;
639169695Skan	}
640169695Skan
641169695Skan      len = cpp_token_len (token) + 2; /* Leading space, terminating \0.  */
642169695Skan      if (total_len + len > capacity)
643169695Skan	{
644169695Skan	  capacity = (capacity + len) * 2;
645169695Skan	  buffer = XRESIZEVEC (char, buffer, capacity);
646169695Skan	}
647169695Skan
648169695Skan      if (token->flags & PREV_WHITE)
649169695Skan	buffer[total_len++] = ' ';
650169695Skan
651169695Skan      total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
652169695Skan				    true)
653169695Skan		   - (uchar *) buffer);
654169695Skan    }
655169695Skan
656169695Skan  buffer[total_len] = '\0';
657169695Skan  return buffer;
658169695Skan}
659169695Skan
660169695Skan/* Returns the file name of #include, #include_next, #import and
661169695Skan   #pragma dependency.  The string is malloced and the caller should
662169695Skan   free it.  Returns NULL on error.  */
663169695Skanstatic const char *
664169695Skanparse_include (cpp_reader *pfile, int *pangle_brackets,
665169695Skan	       const cpp_token ***buf)
666169695Skan{
667169695Skan  char *fname;
668169695Skan  const cpp_token *header;
669169695Skan
670169695Skan  /* Allow macro expansion.  */
671169695Skan  header = get_token_no_padding (pfile);
672169695Skan  if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
673169695Skan    {
674169695Skan      fname = XNEWVEC (char, header->val.str.len - 1);
675169695Skan      memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
676169695Skan      fname[header->val.str.len - 2] = '\0';
677169695Skan      *pangle_brackets = header->type == CPP_HEADER_NAME;
678169695Skan    }
679169695Skan  else if (header->type == CPP_LESS)
680169695Skan    {
681169695Skan      fname = glue_header_name (pfile);
682169695Skan      *pangle_brackets = 1;
683169695Skan    }
684169695Skan  else
685169695Skan    {
686169695Skan      const unsigned char *dir;
687169695Skan
688169695Skan      if (pfile->directive == &dtable[T_PRAGMA])
689169695Skan	dir = U"pragma dependency";
690169695Skan      else
691169695Skan	dir = pfile->directive->name;
692169695Skan      cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
693169695Skan		 dir);
694169695Skan
695169695Skan      return NULL;
696169695Skan    }
697169695Skan
698169695Skan  if (buf == NULL || CPP_OPTION (pfile, discard_comments))
699169695Skan    check_eol (pfile);
700169695Skan  else
701169695Skan    {
702169695Skan      /* If we are not discarding comments, then gather them while
703169695Skan	 doing the eol check.  */
704169695Skan      *buf = check_eol_return_comments (pfile);
705169695Skan    }
706169695Skan
707169695Skan  return fname;
708169695Skan}
709169695Skan
710169695Skan/* Handle #include, #include_next and #import.  */
711169695Skanstatic void
712169695Skando_include_common (cpp_reader *pfile, enum include_type type)
713169695Skan{
714169695Skan  const char *fname;
715169695Skan  int angle_brackets;
716169695Skan  const cpp_token **buf = NULL;
717169695Skan
718169695Skan  /* Re-enable saving of comments if requested, so that the include
719169695Skan     callback can dump comments which follow #include.  */
720169695Skan  pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
721169695Skan
722169695Skan  fname = parse_include (pfile, &angle_brackets, &buf);
723169695Skan  if (!fname)
724169695Skan    {
725169695Skan      if (buf)
726169695Skan	XDELETEVEC (buf);
727169695Skan      return;
728169695Skan    }
729169695Skan
730169695Skan  if (!*fname)
731169695Skan  {
732169695Skan    cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
733169695Skan               pfile->directive->name);
734169695Skan    XDELETEVEC (fname);
735169695Skan    if (buf)
736169695Skan      XDELETEVEC (buf);
737169695Skan    return;
738169695Skan  }
739169695Skan
740169695Skan  /* Prevent #include recursion.  */
741169695Skan  if (pfile->line_table->depth >= CPP_STACK_MAX)
742169695Skan    cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
743169695Skan  else
744169695Skan    {
745169695Skan      /* Get out of macro context, if we are.  */
746169695Skan      skip_rest_of_line (pfile);
747169695Skan
748169695Skan      if (pfile->cb.include)
749169695Skan	pfile->cb.include (pfile, pfile->directive_line,
750169695Skan			   pfile->directive->name, fname, angle_brackets,
751169695Skan			   buf);
752169695Skan
753169695Skan      _cpp_stack_include (pfile, fname, angle_brackets, type);
754169695Skan    }
755169695Skan
756169695Skan  XDELETEVEC (fname);
757169695Skan  if (buf)
758169695Skan    XDELETEVEC (buf);
759169695Skan}
760169695Skan
761169695Skanstatic void
762169695Skando_include (cpp_reader *pfile)
763169695Skan{
764169695Skan  do_include_common (pfile, IT_INCLUDE);
765169695Skan}
766169695Skan
767169695Skanstatic void
768169695Skando_import (cpp_reader *pfile)
769169695Skan{
770169695Skan  do_include_common (pfile, IT_IMPORT);
771169695Skan}
772169695Skan
773169695Skanstatic void
774169695Skando_include_next (cpp_reader *pfile)
775169695Skan{
776169695Skan  enum include_type type = IT_INCLUDE_NEXT;
777169695Skan
778169695Skan  /* If this is the primary source file, warn and use the normal
779169695Skan     search logic.  */
780169695Skan  if (! pfile->buffer->prev)
781169695Skan    {
782169695Skan      cpp_error (pfile, CPP_DL_WARNING,
783169695Skan		 "#include_next in primary source file");
784169695Skan      type = IT_INCLUDE;
785169695Skan    }
786169695Skan  do_include_common (pfile, type);
787169695Skan}
788169695Skan
789169695Skan/* Subroutine of do_linemarker.  Read possible flags after file name.
790169695Skan   LAST is the last flag seen; 0 if this is the first flag. Return the
791169695Skan   flag if it is valid, 0 at the end of the directive. Otherwise
792169695Skan   complain.  */
793169695Skanstatic unsigned int
794169695Skanread_flag (cpp_reader *pfile, unsigned int last)
795169695Skan{
796169695Skan  const cpp_token *token = _cpp_lex_token (pfile);
797169695Skan
798169695Skan  if (token->type == CPP_NUMBER && token->val.str.len == 1)
799169695Skan    {
800169695Skan      unsigned int flag = token->val.str.text[0] - '0';
801169695Skan
802169695Skan      if (flag > last && flag <= 4
803169695Skan	  && (flag != 4 || last == 3)
804169695Skan	  && (flag != 2 || last == 0))
805169695Skan	return flag;
806169695Skan    }
807169695Skan
808169695Skan  if (token->type != CPP_EOF)
809169695Skan    cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
810169695Skan	       cpp_token_as_text (pfile, token));
811169695Skan  return 0;
812169695Skan}
813169695Skan
814169695Skan/* Subroutine of do_line and do_linemarker.  Convert a number in STR,
815169695Skan   of length LEN, to binary; store it in NUMP, and return 0 if the
816169695Skan   number was well-formed, 1 if not.  Temporary, hopefully.  */
817169695Skanstatic int
818169695Skanstrtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
819169695Skan{
820169695Skan  unsigned long reg = 0;
821169695Skan  uchar c;
822169695Skan  while (len--)
823169695Skan    {
824169695Skan      c = *str++;
825169695Skan      if (!ISDIGIT (c))
826169695Skan	return 1;
827169695Skan      reg *= 10;
828169695Skan      reg += c - '0';
829169695Skan    }
830169695Skan  *nump = reg;
831169695Skan  return 0;
832169695Skan}
833169695Skan
834169695Skan/* Interpret #line command.
835169695Skan   Note that the filename string (if any) is a true string constant
836169695Skan   (escapes are interpreted), unlike in #line.  */
837169695Skanstatic void
838169695Skando_line (cpp_reader *pfile)
839169695Skan{
840169695Skan  const struct line_maps *line_table = pfile->line_table;
841169695Skan  const struct line_map *map = &line_table->maps[line_table->used - 1];
842169695Skan
843169695Skan  /* skip_rest_of_line() may cause line table to be realloc()ed so note down
844169695Skan     sysp right now.  */
845169695Skan
846169695Skan  unsigned char map_sysp = map->sysp;
847169695Skan  const cpp_token *token;
848169695Skan  const char *new_file = map->to_file;
849169695Skan  unsigned long new_lineno;
850169695Skan
851169695Skan  /* C99 raised the minimum limit on #line numbers.  */
852169695Skan  unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
853169695Skan
854169695Skan  /* #line commands expand macros.  */
855169695Skan  token = cpp_get_token (pfile);
856169695Skan  if (token->type != CPP_NUMBER
857169695Skan      || strtoul_for_line (token->val.str.text, token->val.str.len,
858169695Skan			   &new_lineno))
859169695Skan    {
860169695Skan      cpp_error (pfile, CPP_DL_ERROR,
861169695Skan		 "\"%s\" after #line is not a positive integer",
862169695Skan		 cpp_token_as_text (pfile, token));
863169695Skan      return;
864169695Skan    }
865169695Skan
866169695Skan  if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
867169695Skan    cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
868169695Skan
869169695Skan  token = cpp_get_token (pfile);
870169695Skan  if (token->type == CPP_STRING)
871169695Skan    {
872169695Skan      cpp_string s = { 0, 0 };
873169695Skan      if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
874169695Skan					    &s, false))
875169695Skan	new_file = (const char *)s.text;
876169695Skan      check_eol (pfile);
877169695Skan    }
878169695Skan  else if (token->type != CPP_EOF)
879169695Skan    {
880169695Skan      cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
881169695Skan		 cpp_token_as_text (pfile, token));
882169695Skan      return;
883169695Skan    }
884169695Skan
885169695Skan  skip_rest_of_line (pfile);
886169695Skan  _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
887169695Skan		       map_sysp);
888169695Skan}
889169695Skan
890169695Skan/* Interpret the # 44 "file" [flags] notation, which has slightly
891169695Skan   different syntax and semantics from #line:  Flags are allowed,
892169695Skan   and we never complain about the line number being too big.  */
893169695Skanstatic void
894169695Skando_linemarker (cpp_reader *pfile)
895169695Skan{
896169695Skan  const struct line_maps *line_table = pfile->line_table;
897169695Skan  const struct line_map *map = &line_table->maps[line_table->used - 1];
898169695Skan  const cpp_token *token;
899169695Skan  const char *new_file = map->to_file;
900169695Skan  unsigned long new_lineno;
901169695Skan  unsigned int new_sysp = map->sysp;
902169695Skan  enum lc_reason reason = LC_RENAME;
903169695Skan  int flag;
904169695Skan
905169695Skan  /* Back up so we can get the number again.  Putting this in
906169695Skan     _cpp_handle_directive risks two calls to _cpp_backup_tokens in
907169695Skan     some circumstances, which can segfault.  */
908169695Skan  _cpp_backup_tokens (pfile, 1);
909169695Skan
910169695Skan  /* #line commands expand macros.  */
911169695Skan  token = cpp_get_token (pfile);
912169695Skan  if (token->type != CPP_NUMBER
913169695Skan      || strtoul_for_line (token->val.str.text, token->val.str.len,
914169695Skan			   &new_lineno))
915169695Skan    {
916169695Skan      cpp_error (pfile, CPP_DL_ERROR,
917169695Skan		 "\"%s\" after # is not a positive integer",
918169695Skan		 cpp_token_as_text (pfile, token));
919169695Skan      return;
920169695Skan    }
921169695Skan
922169695Skan  token = cpp_get_token (pfile);
923169695Skan  if (token->type == CPP_STRING)
924169695Skan    {
925169695Skan      cpp_string s = { 0, 0 };
926169695Skan      if (cpp_interpret_string_notranslate (pfile, &token->val.str,
927169695Skan					    1, &s, false))
928169695Skan	new_file = (const char *)s.text;
929169695Skan
930169695Skan      new_sysp = 0;
931169695Skan      flag = read_flag (pfile, 0);
932169695Skan      if (flag == 1)
933169695Skan	{
934169695Skan	  reason = LC_ENTER;
935169695Skan	  /* Fake an include for cpp_included ().  */
936169695Skan	  _cpp_fake_include (pfile, new_file);
937169695Skan	  flag = read_flag (pfile, flag);
938169695Skan	}
939169695Skan      else if (flag == 2)
940169695Skan	{
941169695Skan	  reason = LC_LEAVE;
942169695Skan	  flag = read_flag (pfile, flag);
943169695Skan	}
944169695Skan      if (flag == 3)
945169695Skan	{
946169695Skan	  new_sysp = 1;
947169695Skan	  flag = read_flag (pfile, flag);
948169695Skan	  if (flag == 4)
949169695Skan	    new_sysp = 2;
950169695Skan	}
951169695Skan      pfile->buffer->sysp = new_sysp;
952169695Skan
953169695Skan      check_eol (pfile);
954169695Skan    }
955169695Skan  else if (token->type != CPP_EOF)
956169695Skan    {
957169695Skan      cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
958169695Skan		 cpp_token_as_text (pfile, token));
959169695Skan      return;
960169695Skan    }
961169695Skan
962169695Skan  skip_rest_of_line (pfile);
963169695Skan  _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
964169695Skan}
965169695Skan
966169695Skan/* Arrange the file_change callback.  pfile->line has changed to
967169695Skan   FILE_LINE of TO_FILE, for reason REASON.  SYSP is 1 for a system
968169695Skan   header, 2 for a system header that needs to be extern "C" protected,
969169695Skan   and zero otherwise.  */
970169695Skanvoid
971169695Skan_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
972169695Skan		     const char *to_file, unsigned int file_line,
973169695Skan		     unsigned int sysp)
974169695Skan{
975169695Skan  const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
976169695Skan					    to_file, file_line);
977169695Skan  if (map != NULL)
978169695Skan    linemap_line_start (pfile->line_table, map->to_line, 127);
979169695Skan
980169695Skan  if (pfile->cb.file_change)
981169695Skan    pfile->cb.file_change (pfile, map);
982169695Skan}
983169695Skan
984169695Skan/* Report a warning or error detected by the program we are
985169695Skan   processing.  Use the directive's tokens in the error message.  */
986169695Skanstatic void
987169695Skando_diagnostic (cpp_reader *pfile, int code, int print_dir)
988169695Skan{
989169695Skan  if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
990169695Skan    {
991169695Skan      if (print_dir)
992169695Skan	fprintf (stderr, "#%s ", pfile->directive->name);
993169695Skan      pfile->state.prevent_expansion++;
994260310Spfg      /* APPLE LOCAL #error with unmatched quotes 5607574 */
995260310Spfg      pfile->state.in_diagnostic++;
996169695Skan      cpp_output_line (pfile, stderr);
997260310Spfg      /* APPLE LOCAL #error with unmatched quotes 5607574 */
998260310Spfg      pfile->state.in_diagnostic--;
999169695Skan      pfile->state.prevent_expansion--;
1000169695Skan    }
1001169695Skan}
1002169695Skan
1003169695Skanstatic void
1004169695Skando_error (cpp_reader *pfile)
1005169695Skan{
1006169695Skan  do_diagnostic (pfile, CPP_DL_ERROR, 1);
1007169695Skan}
1008169695Skan
1009169695Skanstatic void
1010169695Skando_warning (cpp_reader *pfile)
1011169695Skan{
1012169695Skan  /* We want #warning diagnostics to be emitted in system headers too.  */
1013169695Skan  do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
1014169695Skan}
1015169695Skan
1016169695Skan/* Report program identification.  */
1017169695Skanstatic void
1018169695Skando_ident (cpp_reader *pfile)
1019169695Skan{
1020169695Skan  const cpp_token *str = cpp_get_token (pfile);
1021169695Skan
1022169695Skan  if (str->type != CPP_STRING)
1023169695Skan    cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1024169695Skan	       pfile->directive->name);
1025169695Skan  else if (pfile->cb.ident)
1026169695Skan    pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1027169695Skan
1028169695Skan  check_eol (pfile);
1029169695Skan}
1030169695Skan
1031169695Skan/* Lookup a PRAGMA name in a singly-linked CHAIN.  Returns the
1032169695Skan   matching entry, or NULL if none is found.  The returned entry could
1033169695Skan   be the start of a namespace chain, or a pragma.  */
1034169695Skanstatic struct pragma_entry *
1035169695Skanlookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1036169695Skan{
1037169695Skan  while (chain && chain->pragma != pragma)
1038169695Skan    chain = chain->next;
1039169695Skan
1040169695Skan  return chain;
1041169695Skan}
1042169695Skan
1043169695Skan/* Create and insert a blank pragma entry at the beginning of a
1044169695Skan   singly-linked CHAIN.  */
1045169695Skanstatic struct pragma_entry *
1046169695Skannew_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1047169695Skan{
1048169695Skan  struct pragma_entry *new_entry;
1049169695Skan
1050169695Skan  new_entry = (struct pragma_entry *)
1051169695Skan    _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1052169695Skan
1053169695Skan  memset (new_entry, 0, sizeof (struct pragma_entry));
1054169695Skan  new_entry->next = *chain;
1055169695Skan
1056169695Skan  *chain = new_entry;
1057169695Skan  return new_entry;
1058169695Skan}
1059169695Skan
1060169695Skan/* Register a pragma NAME in namespace SPACE.  If SPACE is null, it
1061169695Skan   goes in the global namespace.  */
1062169695Skanstatic struct pragma_entry *
1063169695Skanregister_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1064169695Skan		   bool allow_name_expansion)
1065169695Skan{
1066169695Skan  struct pragma_entry **chain = &pfile->pragmas;
1067169695Skan  struct pragma_entry *entry;
1068169695Skan  const cpp_hashnode *node;
1069169695Skan
1070169695Skan  if (space)
1071169695Skan    {
1072169695Skan      node = cpp_lookup (pfile, U space, strlen (space));
1073169695Skan      entry = lookup_pragma_entry (*chain, node);
1074169695Skan      if (!entry)
1075169695Skan	{
1076169695Skan	  entry = new_pragma_entry (pfile, chain);
1077169695Skan	  entry->pragma = node;
1078169695Skan	  entry->is_nspace = true;
1079169695Skan	  entry->allow_expansion = allow_name_expansion;
1080169695Skan	}
1081169695Skan      else if (!entry->is_nspace)
1082169695Skan	goto clash;
1083169695Skan      else if (entry->allow_expansion != allow_name_expansion)
1084169695Skan	{
1085169695Skan	  cpp_error (pfile, CPP_DL_ICE,
1086169695Skan		     "registering pragmas in namespace \"%s\" with mismatched "
1087169695Skan		     "name expansion", space);
1088169695Skan	  return NULL;
1089169695Skan	}
1090169695Skan      chain = &entry->u.space;
1091169695Skan    }
1092169695Skan  else if (allow_name_expansion)
1093169695Skan    {
1094169695Skan      cpp_error (pfile, CPP_DL_ICE,
1095169695Skan		 "registering pragma \"%s\" with name expansion "
1096169695Skan		 "and no namespace", name);
1097169695Skan      return NULL;
1098169695Skan    }
1099169695Skan
1100169695Skan  /* Check for duplicates.  */
1101169695Skan  node = cpp_lookup (pfile, U name, strlen (name));
1102169695Skan  entry = lookup_pragma_entry (*chain, node);
1103169695Skan  if (entry == NULL)
1104169695Skan    {
1105169695Skan      entry = new_pragma_entry (pfile, chain);
1106169695Skan      entry->pragma = node;
1107169695Skan      return entry;
1108169695Skan    }
1109169695Skan
1110169695Skan  if (entry->is_nspace)
1111169695Skan    clash:
1112169695Skan    cpp_error (pfile, CPP_DL_ICE,
1113169695Skan	       "registering \"%s\" as both a pragma and a pragma namespace",
1114169695Skan	       NODE_NAME (node));
1115169695Skan  else if (space)
1116169695Skan    cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1117169695Skan	       space, name);
1118169695Skan  else
1119169695Skan    cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1120169695Skan
1121169695Skan  return NULL;
1122169695Skan}
1123169695Skan
1124169695Skan/* Register a cpplib internal pragma SPACE NAME with HANDLER.  */
1125169695Skanstatic void
1126169695Skanregister_pragma_internal (cpp_reader *pfile, const char *space,
1127169695Skan			  const char *name, pragma_cb handler)
1128169695Skan{
1129169695Skan  struct pragma_entry *entry;
1130169695Skan
1131169695Skan  entry = register_pragma_1 (pfile, space, name, false);
1132169695Skan  entry->is_internal = true;
1133169695Skan  entry->u.handler = handler;
1134169695Skan}
1135169695Skan
1136169695Skan/* Register a pragma NAME in namespace SPACE.  If SPACE is null, it
1137169695Skan   goes in the global namespace.  HANDLER is the handler it will call,
1138169695Skan   which must be non-NULL.  If ALLOW_EXPANSION is set, allow macro
1139169695Skan   expansion while parsing pragma NAME.  This function is exported
1140169695Skan   from libcpp. */
1141169695Skanvoid
1142169695Skancpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1143169695Skan		     pragma_cb handler, bool allow_expansion)
1144169695Skan{
1145169695Skan  struct pragma_entry *entry;
1146169695Skan
1147169695Skan  if (!handler)
1148169695Skan    {
1149169695Skan      cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1150169695Skan      return;
1151169695Skan    }
1152169695Skan
1153169695Skan  entry = register_pragma_1 (pfile, space, name, false);
1154169695Skan  if (entry)
1155169695Skan    {
1156169695Skan      entry->allow_expansion = allow_expansion;
1157169695Skan      entry->u.handler = handler;
1158169695Skan    }
1159169695Skan}
1160169695Skan
1161169695Skan/* Similarly, but create mark the pragma for deferred processing.
1162169695Skan   When found, a CPP_PRAGMA token will be insertted into the stream
1163169695Skan   with IDENT in the token->u.pragma slot.  */
1164169695Skanvoid
1165169695Skancpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1166169695Skan			      const char *name, unsigned int ident,
1167169695Skan			      bool allow_expansion, bool allow_name_expansion)
1168169695Skan{
1169169695Skan  struct pragma_entry *entry;
1170169695Skan
1171169695Skan  entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1172169695Skan  if (entry)
1173169695Skan    {
1174169695Skan      entry->is_deferred = true;
1175169695Skan      entry->allow_expansion = allow_expansion;
1176169695Skan      entry->u.ident = ident;
1177169695Skan    }
1178169695Skan}
1179169695Skan
1180260310Spfg/* APPLE LOCAL begin pragma mark 5614511 */
1181260310Spfg/* Handle #pragma mark.  */
1182260310Spfgstatic void
1183260310Spfgdo_pragma_mark (cpp_reader *pfile)
1184260310Spfg{
1185260310Spfg  ++pfile->state.skipping;
1186260310Spfg  skip_rest_of_line (pfile);
1187260310Spfg  --pfile->state.skipping;
1188260310Spfg}
1189260310Spfg/* APPLE LOCAL end pragma mark 5614511 */
1190260310Spfg
1191169695Skan/* Register the pragmas the preprocessor itself handles.  */
1192169695Skanvoid
1193169695Skan_cpp_init_internal_pragmas (cpp_reader *pfile)
1194169695Skan{
1195169695Skan  /* Pragmas in the global namespace.  */
1196169695Skan  register_pragma_internal (pfile, 0, "once", do_pragma_once);
1197260310Spfg  /* APPLE LOCAL pragma mark 5614511 */
1198260310Spfg  register_pragma_internal (pfile, 0, "mark", do_pragma_mark);
1199169695Skan
1200169695Skan  /* New GCC-specific pragmas should be put in the GCC namespace.  */
1201169695Skan  register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1202169695Skan  register_pragma_internal (pfile, "GCC", "system_header",
1203169695Skan			    do_pragma_system_header);
1204169695Skan  register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1205169695Skan}
1206169695Skan
1207169695Skan/* Return the number of registered pragmas in PE.  */
1208169695Skan
1209169695Skanstatic int
1210169695Skancount_registered_pragmas (struct pragma_entry *pe)
1211169695Skan{
1212169695Skan  int ct = 0;
1213169695Skan  for (; pe != NULL; pe = pe->next)
1214169695Skan    {
1215169695Skan      if (pe->is_nspace)
1216169695Skan	ct += count_registered_pragmas (pe->u.space);
1217169695Skan      ct++;
1218169695Skan    }
1219169695Skan  return ct;
1220169695Skan}
1221169695Skan
1222169695Skan/* Save into SD the names of the registered pragmas referenced by PE,
1223169695Skan   and return a pointer to the next free space in SD.  */
1224169695Skan
1225169695Skanstatic char **
1226169695Skansave_registered_pragmas (struct pragma_entry *pe, char **sd)
1227169695Skan{
1228169695Skan  for (; pe != NULL; pe = pe->next)
1229169695Skan    {
1230169695Skan      if (pe->is_nspace)
1231169695Skan	sd = save_registered_pragmas (pe->u.space, sd);
1232169695Skan      *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1233169695Skan                                HT_LEN (&pe->pragma->ident),
1234169695Skan                                HT_LEN (&pe->pragma->ident) + 1);
1235169695Skan    }
1236169695Skan  return sd;
1237169695Skan}
1238169695Skan
1239169695Skan/* Return a newly-allocated array which saves the names of the
1240169695Skan   registered pragmas.  */
1241169695Skan
1242169695Skanchar **
1243169695Skan_cpp_save_pragma_names (cpp_reader *pfile)
1244169695Skan{
1245169695Skan  int ct = count_registered_pragmas (pfile->pragmas);
1246169695Skan  char **result = XNEWVEC (char *, ct);
1247169695Skan  (void) save_registered_pragmas (pfile->pragmas, result);
1248169695Skan  return result;
1249169695Skan}
1250169695Skan
1251169695Skan/* Restore from SD the names of the registered pragmas referenced by PE,
1252169695Skan   and return a pointer to the next unused name in SD.  */
1253169695Skan
1254169695Skanstatic char **
1255169695Skanrestore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1256169695Skan			    char **sd)
1257169695Skan{
1258169695Skan  for (; pe != NULL; pe = pe->next)
1259169695Skan    {
1260169695Skan      if (pe->is_nspace)
1261169695Skan	sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1262169695Skan      pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1263169695Skan      free (*sd);
1264169695Skan      sd++;
1265169695Skan    }
1266169695Skan  return sd;
1267169695Skan}
1268169695Skan
1269169695Skan/* Restore the names of the registered pragmas from SAVED.  */
1270169695Skan
1271169695Skanvoid
1272169695Skan_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1273169695Skan{
1274169695Skan  (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1275169695Skan  free (saved);
1276169695Skan}
1277169695Skan
1278169695Skan/* Pragmata handling.  We handle some, and pass the rest on to the
1279169695Skan   front end.  C99 defines three pragmas and says that no macro
1280169695Skan   expansion is to be performed on them; whether or not macro
1281169695Skan   expansion happens for other pragmas is implementation defined.
1282169695Skan   This implementation allows for a mix of both, since GCC did not
1283169695Skan   traditionally macro expand its (few) pragmas, whereas OpenMP
1284169695Skan   specifies that macro expansion should happen.  */
1285169695Skanstatic void
1286169695Skando_pragma (cpp_reader *pfile)
1287169695Skan{
1288169695Skan  const struct pragma_entry *p = NULL;
1289169695Skan  const cpp_token *token, *pragma_token = pfile->cur_token;
1290169695Skan  cpp_token ns_token;
1291169695Skan  unsigned int count = 1;
1292169695Skan
1293169695Skan  pfile->state.prevent_expansion++;
1294169695Skan
1295169695Skan  token = cpp_get_token (pfile);
1296169695Skan  ns_token = *token;
1297169695Skan  if (token->type == CPP_NAME)
1298169695Skan    {
1299169695Skan      p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1300169695Skan      if (p && p->is_nspace)
1301169695Skan	{
1302169695Skan	  bool allow_name_expansion = p->allow_expansion;
1303169695Skan	  if (allow_name_expansion)
1304169695Skan	    pfile->state.prevent_expansion--;
1305169695Skan	  token = cpp_get_token (pfile);
1306169695Skan	  if (token->type == CPP_NAME)
1307169695Skan	    p = lookup_pragma_entry (p->u.space, token->val.node);
1308169695Skan	  else
1309169695Skan	    p = NULL;
1310169695Skan	  if (allow_name_expansion)
1311169695Skan	    pfile->state.prevent_expansion++;
1312169695Skan	  count = 2;
1313169695Skan	}
1314169695Skan    }
1315169695Skan
1316169695Skan  if (p)
1317169695Skan    {
1318169695Skan      if (p->is_deferred)
1319169695Skan	{
1320169695Skan	  pfile->directive_result.src_loc = pragma_token->src_loc;
1321169695Skan	  pfile->directive_result.type = CPP_PRAGMA;
1322169695Skan	  pfile->directive_result.flags = pragma_token->flags;
1323169695Skan	  pfile->directive_result.val.pragma = p->u.ident;
1324169695Skan	  pfile->state.in_deferred_pragma = true;
1325169695Skan	  pfile->state.pragma_allow_expansion = p->allow_expansion;
1326169695Skan	  if (!p->allow_expansion)
1327169695Skan	    pfile->state.prevent_expansion++;
1328169695Skan	}
1329169695Skan      else
1330169695Skan	{
1331169695Skan	  /* Since the handler below doesn't get the line number, that
1332169695Skan	     it might need for diagnostics, make sure it has the right
1333169695Skan	     numbers in place.  */
1334169695Skan	  if (pfile->cb.line_change)
1335169695Skan	    (*pfile->cb.line_change) (pfile, pragma_token, false);
1336169695Skan	  if (p->allow_expansion)
1337169695Skan	    pfile->state.prevent_expansion--;
1338169695Skan	  (*p->u.handler) (pfile);
1339169695Skan	  if (p->allow_expansion)
1340169695Skan	    pfile->state.prevent_expansion++;
1341169695Skan	}
1342169695Skan    }
1343169695Skan  else if (pfile->cb.def_pragma)
1344169695Skan    {
1345169695Skan      if (count == 1 || pfile->context->prev == NULL)
1346169695Skan	_cpp_backup_tokens (pfile, count);
1347169695Skan      else
1348169695Skan	{
1349169695Skan	  /* Invalid name comes from macro expansion, _cpp_backup_tokens
1350169695Skan	     won't allow backing 2 tokens.  */
1351169695Skan	  /* ??? The token buffer is leaked.  Perhaps if def_pragma hook
1352169695Skan	     reads both tokens, we could perhaps free it, but if it doesn't,
1353169695Skan	     we don't know the exact lifespan.  */
1354169695Skan	  cpp_token *toks = XNEWVEC (cpp_token, 2);
1355169695Skan	  toks[0] = ns_token;
1356169695Skan	  toks[0].flags |= NO_EXPAND;
1357169695Skan	  toks[1] = *token;
1358169695Skan	  toks[1].flags |= NO_EXPAND;
1359169695Skan	  _cpp_push_token_context (pfile, NULL, toks, 2);
1360169695Skan	}
1361169695Skan      pfile->cb.def_pragma (pfile, pfile->directive_line);
1362169695Skan    }
1363169695Skan
1364169695Skan  pfile->state.prevent_expansion--;
1365169695Skan}
1366169695Skan
1367169695Skan/* Handle #pragma once.  */
1368169695Skanstatic void
1369169695Skando_pragma_once (cpp_reader *pfile)
1370169695Skan{
1371169695Skan  if (pfile->buffer->prev == NULL)
1372169695Skan    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1373169695Skan
1374169695Skan  check_eol (pfile);
1375169695Skan  _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1376169695Skan}
1377169695Skan
1378169695Skan/* Handle #pragma GCC poison, to poison one or more identifiers so
1379169695Skan   that the lexer produces a hard error for each subsequent usage.  */
1380169695Skanstatic void
1381169695Skando_pragma_poison (cpp_reader *pfile)
1382169695Skan{
1383169695Skan  const cpp_token *tok;
1384169695Skan  cpp_hashnode *hp;
1385169695Skan
1386169695Skan  pfile->state.poisoned_ok = 1;
1387169695Skan  for (;;)
1388169695Skan    {
1389169695Skan      tok = _cpp_lex_token (pfile);
1390169695Skan      if (tok->type == CPP_EOF)
1391169695Skan	break;
1392169695Skan      if (tok->type != CPP_NAME)
1393169695Skan	{
1394169695Skan	  cpp_error (pfile, CPP_DL_ERROR,
1395169695Skan		     "invalid #pragma GCC poison directive");
1396169695Skan	  break;
1397169695Skan	}
1398169695Skan
1399169695Skan      hp = tok->val.node;
1400169695Skan      if (hp->flags & NODE_POISONED)
1401169695Skan	continue;
1402169695Skan
1403169695Skan      if (hp->type == NT_MACRO)
1404169695Skan	cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1405169695Skan		   NODE_NAME (hp));
1406169695Skan      _cpp_free_definition (hp);
1407169695Skan      hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1408169695Skan    }
1409169695Skan  pfile->state.poisoned_ok = 0;
1410169695Skan}
1411169695Skan
1412169695Skan/* Mark the current header as a system header.  This will suppress
1413169695Skan   some categories of warnings (notably those from -pedantic).  It is
1414169695Skan   intended for use in system libraries that cannot be implemented in
1415169695Skan   conforming C, but cannot be certain that their headers appear in a
1416169695Skan   system include directory.  To prevent abuse, it is rejected in the
1417169695Skan   primary source file.  */
1418169695Skanstatic void
1419169695Skando_pragma_system_header (cpp_reader *pfile)
1420169695Skan{
1421169695Skan  cpp_buffer *buffer = pfile->buffer;
1422169695Skan
1423169695Skan  if (buffer->prev == 0)
1424169695Skan    cpp_error (pfile, CPP_DL_WARNING,
1425169695Skan	       "#pragma system_header ignored outside include file");
1426169695Skan  else
1427169695Skan    {
1428169695Skan      check_eol (pfile);
1429169695Skan      skip_rest_of_line (pfile);
1430169695Skan      cpp_make_system_header (pfile, 1, 0);
1431169695Skan    }
1432169695Skan}
1433169695Skan
1434169695Skan/* Check the modified date of the current include file against a specified
1435169695Skan   file. Issue a diagnostic, if the specified file is newer. We use this to
1436169695Skan   determine if a fixed header should be refixed.  */
1437169695Skanstatic void
1438169695Skando_pragma_dependency (cpp_reader *pfile)
1439169695Skan{
1440169695Skan  const char *fname;
1441169695Skan  int angle_brackets, ordering;
1442169695Skan
1443169695Skan  fname = parse_include (pfile, &angle_brackets, NULL);
1444169695Skan  if (!fname)
1445169695Skan    return;
1446169695Skan
1447169695Skan  ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1448169695Skan  if (ordering < 0)
1449169695Skan    cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1450169695Skan  else if (ordering > 0)
1451169695Skan    {
1452169695Skan      cpp_error (pfile, CPP_DL_WARNING,
1453169695Skan		 "current file is older than %s", fname);
1454169695Skan      if (cpp_get_token (pfile)->type != CPP_EOF)
1455169695Skan	{
1456169695Skan	  _cpp_backup_tokens (pfile, 1);
1457169695Skan	  do_diagnostic (pfile, CPP_DL_WARNING, 0);
1458169695Skan	}
1459169695Skan    }
1460169695Skan
1461169695Skan  free ((void *) fname);
1462169695Skan}
1463169695Skan
1464169695Skan/* Get a token but skip padding.  */
1465169695Skanstatic const cpp_token *
1466169695Skanget_token_no_padding (cpp_reader *pfile)
1467169695Skan{
1468169695Skan  for (;;)
1469169695Skan    {
1470169695Skan      const cpp_token *result = cpp_get_token (pfile);
1471169695Skan      if (result->type != CPP_PADDING)
1472169695Skan	return result;
1473169695Skan    }
1474169695Skan}
1475169695Skan
1476169695Skan/* Check syntax is "(string-literal)".  Returns the string on success,
1477169695Skan   or NULL on failure.  */
1478169695Skanstatic const cpp_token *
1479169695Skanget__Pragma_string (cpp_reader *pfile)
1480169695Skan{
1481169695Skan  const cpp_token *string;
1482169695Skan
1483169695Skan  if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1484169695Skan    return NULL;
1485169695Skan
1486169695Skan  string = get_token_no_padding (pfile);
1487169695Skan  if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1488169695Skan    return NULL;
1489169695Skan
1490169695Skan  if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1491169695Skan    return NULL;
1492169695Skan
1493169695Skan  return string;
1494169695Skan}
1495169695Skan
1496169695Skan/* Destringize IN into a temporary buffer, by removing the first \ of
1497169695Skan   \" and \\ sequences, and process the result as a #pragma directive.  */
1498169695Skanstatic void
1499169695Skandestringize_and_run (cpp_reader *pfile, const cpp_string *in)
1500169695Skan{
1501169695Skan  const unsigned char *src, *limit;
1502169695Skan  char *dest, *result;
1503169695Skan  cpp_context *saved_context;
1504169695Skan  cpp_token *saved_cur_token;
1505169695Skan  tokenrun *saved_cur_run;
1506169695Skan  cpp_token *toks;
1507169695Skan  int count;
1508169695Skan
1509169695Skan  dest = result = (char *) alloca (in->len - 1);
1510169695Skan  src = in->text + 1 + (in->text[0] == 'L');
1511169695Skan  limit = in->text + in->len - 1;
1512169695Skan  while (src < limit)
1513169695Skan    {
1514169695Skan      /* We know there is a character following the backslash.  */
1515169695Skan      if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1516169695Skan	src++;
1517169695Skan      *dest++ = *src++;
1518169695Skan    }
1519169695Skan  *dest = '\n';
1520169695Skan
1521169695Skan  /* Ugh; an awful kludge.  We are really not set up to be lexing
1522169695Skan     tokens when in the middle of a macro expansion.  Use a new
1523169695Skan     context to force cpp_get_token to lex, and so skip_rest_of_line
1524169695Skan     doesn't go beyond the end of the text.  Also, remember the
1525169695Skan     current lexing position so we can return to it later.
1526169695Skan
1527169695Skan     Something like line-at-a-time lexing should remove the need for
1528169695Skan     this.  */
1529169695Skan  saved_context = pfile->context;
1530169695Skan  saved_cur_token = pfile->cur_token;
1531169695Skan  saved_cur_run = pfile->cur_run;
1532169695Skan
1533169695Skan  pfile->context = XNEW (cpp_context);
1534169695Skan  pfile->context->macro = 0;
1535169695Skan  pfile->context->prev = 0;
1536169695Skan  pfile->context->next = 0;
1537169695Skan
1538169695Skan  /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1539169695Skan     until we've read all of the tokens that we want.  */
1540169695Skan  cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1541169695Skan		   /* from_stage3 */ true);
1542169695Skan  /* ??? Antique Disgusting Hack.  What does this do?  */
1543169695Skan  if (pfile->buffer->prev)
1544169695Skan    pfile->buffer->file = pfile->buffer->prev->file;
1545169695Skan
1546169695Skan  start_directive (pfile);
1547169695Skan  _cpp_clean_line (pfile);
1548169695Skan  do_pragma (pfile);
1549169695Skan  end_directive (pfile, 1);
1550169695Skan
1551169695Skan  /* We always insert at least one token, the directive result.  It'll
1552169695Skan     either be a CPP_PADDING or a CPP_PRAGMA.  In the later case, we
1553169695Skan     need to insert *all* of the tokens, including the CPP_PRAGMA_EOL.  */
1554169695Skan
1555169695Skan  /* If we're not handling the pragma internally, read all of the tokens from
1556169695Skan     the string buffer now, while the string buffer is still installed.  */
1557169695Skan  /* ??? Note that the token buffer allocated here is leaked.  It's not clear
1558169695Skan     to me what the true lifespan of the tokens are.  It would appear that
1559169695Skan     the lifespan is the entire parse of the main input stream, in which case
1560169695Skan     this may not be wrong.  */
1561169695Skan  if (pfile->directive_result.type == CPP_PRAGMA)
1562169695Skan    {
1563169695Skan      int maxcount;
1564169695Skan
1565169695Skan      count = 1;
1566169695Skan      maxcount = 50;
1567169695Skan      toks = XNEWVEC (cpp_token, maxcount);
1568169695Skan      toks[0] = pfile->directive_result;
1569169695Skan
1570169695Skan      do
1571169695Skan	{
1572169695Skan	  if (count == maxcount)
1573169695Skan	    {
1574169695Skan	      maxcount = maxcount * 3 / 2;
1575169695Skan	      toks = XRESIZEVEC (cpp_token, toks, maxcount);
1576169695Skan	    }
1577169695Skan	  toks[count] = *cpp_get_token (pfile);
1578169695Skan	  /* Macros have been already expanded by cpp_get_token
1579169695Skan	     if the pragma allowed expansion.  */
1580169695Skan	  toks[count++].flags |= NO_EXPAND;
1581169695Skan	}
1582169695Skan      while (toks[count-1].type != CPP_PRAGMA_EOL);
1583169695Skan    }
1584169695Skan  else
1585169695Skan    {
1586169695Skan      count = 1;
1587169695Skan      toks = XNEW (cpp_token);
1588169695Skan      toks[0] = pfile->directive_result;
1589169695Skan
1590169695Skan      /* If we handled the entire pragma internally, make sure we get the
1591169695Skan	 line number correct for the next token.  */
1592169695Skan      if (pfile->cb.line_change)
1593169695Skan	pfile->cb.line_change (pfile, pfile->cur_token, false);
1594169695Skan    }
1595169695Skan
1596169695Skan  /* Finish inlining run_directive.  */
1597169695Skan  pfile->buffer->file = NULL;
1598169695Skan  _cpp_pop_buffer (pfile);
1599169695Skan
1600169695Skan  /* Reset the old macro state before ...  */
1601169695Skan  XDELETE (pfile->context);
1602169695Skan  pfile->context = saved_context;
1603169695Skan  pfile->cur_token = saved_cur_token;
1604169695Skan  pfile->cur_run = saved_cur_run;
1605169695Skan
1606169695Skan  /* ... inserting the new tokens we collected.  */
1607169695Skan  _cpp_push_token_context (pfile, NULL, toks, count);
1608169695Skan}
1609169695Skan
1610169695Skan/* Handle the _Pragma operator.  */
1611169695Skanvoid
1612169695Skan_cpp_do__Pragma (cpp_reader *pfile)
1613169695Skan{
1614169695Skan  const cpp_token *string = get__Pragma_string (pfile);
1615169695Skan  pfile->directive_result.type = CPP_PADDING;
1616169695Skan
1617169695Skan  if (string)
1618169695Skan    destringize_and_run (pfile, &string->val.str);
1619169695Skan  else
1620169695Skan    cpp_error (pfile, CPP_DL_ERROR,
1621169695Skan	       "_Pragma takes a parenthesized string literal");
1622169695Skan}
1623169695Skan
1624169695Skan/* Handle #ifdef.  */
1625169695Skanstatic void
1626169695Skando_ifdef (cpp_reader *pfile)
1627169695Skan{
1628169695Skan  int skip = 1;
1629169695Skan
1630169695Skan  if (! pfile->state.skipping)
1631169695Skan    {
1632169695Skan      const cpp_hashnode *node = lex_macro_node (pfile);
1633169695Skan
1634169695Skan      if (node)
1635169695Skan	{
1636169695Skan	  skip = node->type != NT_MACRO;
1637169695Skan	  _cpp_mark_macro_used (node);
1638169695Skan	  check_eol (pfile);
1639169695Skan	}
1640169695Skan    }
1641169695Skan
1642169695Skan  push_conditional (pfile, skip, T_IFDEF, 0);
1643169695Skan}
1644169695Skan
1645169695Skan/* Handle #ifndef.  */
1646169695Skanstatic void
1647169695Skando_ifndef (cpp_reader *pfile)
1648169695Skan{
1649169695Skan  int skip = 1;
1650169695Skan  const cpp_hashnode *node = 0;
1651169695Skan
1652169695Skan  if (! pfile->state.skipping)
1653169695Skan    {
1654169695Skan      node = lex_macro_node (pfile);
1655169695Skan
1656169695Skan      if (node)
1657169695Skan	{
1658169695Skan	  skip = node->type == NT_MACRO;
1659169695Skan	  _cpp_mark_macro_used (node);
1660169695Skan	  check_eol (pfile);
1661169695Skan	}
1662169695Skan    }
1663169695Skan
1664169695Skan  push_conditional (pfile, skip, T_IFNDEF, node);
1665169695Skan}
1666169695Skan
1667169695Skan/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1668169695Skan   pfile->mi_ind_cmacro so we can handle multiple-include
1669169695Skan   optimizations.  If macro expansion occurs in the expression, we
1670169695Skan   cannot treat it as a controlling conditional, since the expansion
1671169695Skan   could change in the future.  That is handled by cpp_get_token.  */
1672169695Skanstatic void
1673169695Skando_if (cpp_reader *pfile)
1674169695Skan{
1675169695Skan  int skip = 1;
1676169695Skan
1677169695Skan  if (! pfile->state.skipping)
1678169695Skan    skip = _cpp_parse_expr (pfile) == false;
1679169695Skan
1680169695Skan  push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1681169695Skan}
1682169695Skan
1683169695Skan/* Flip skipping state if appropriate and continue without changing
1684169695Skan   if_stack; this is so that the error message for missing #endif's
1685169695Skan   etc. will point to the original #if.  */
1686169695Skanstatic void
1687169695Skando_else (cpp_reader *pfile)
1688169695Skan{
1689169695Skan  cpp_buffer *buffer = pfile->buffer;
1690169695Skan  struct if_stack *ifs = buffer->if_stack;
1691169695Skan
1692169695Skan  if (ifs == NULL)
1693169695Skan    cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1694169695Skan  else
1695169695Skan    {
1696169695Skan      if (ifs->type == T_ELSE)
1697169695Skan	{
1698169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1699169695Skan	  cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1700169695Skan			       "the conditional began here");
1701169695Skan	}
1702169695Skan      ifs->type = T_ELSE;
1703169695Skan
1704169695Skan      /* Skip any future (erroneous) #elses or #elifs.  */
1705169695Skan      pfile->state.skipping = ifs->skip_elses;
1706169695Skan      ifs->skip_elses = true;
1707169695Skan
1708169695Skan      /* Invalidate any controlling macro.  */
1709169695Skan      ifs->mi_cmacro = 0;
1710169695Skan
1711169695Skan      /* Only check EOL if was not originally skipping.  */
1712169695Skan      if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1713169695Skan	check_eol (pfile);
1714169695Skan    }
1715169695Skan}
1716169695Skan
1717169695Skan/* Handle a #elif directive by not changing if_stack either.  See the
1718169695Skan   comment above do_else.  */
1719169695Skanstatic void
1720169695Skando_elif (cpp_reader *pfile)
1721169695Skan{
1722169695Skan  cpp_buffer *buffer = pfile->buffer;
1723169695Skan  struct if_stack *ifs = buffer->if_stack;
1724169695Skan
1725169695Skan  if (ifs == NULL)
1726169695Skan    cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1727169695Skan  else
1728169695Skan    {
1729169695Skan      if (ifs->type == T_ELSE)
1730169695Skan	{
1731169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1732169695Skan	  cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1733169695Skan			       "the conditional began here");
1734169695Skan	}
1735169695Skan      ifs->type = T_ELIF;
1736169695Skan
1737169695Skan      /* Only evaluate this if we aren't skipping elses.  During
1738169695Skan	 evaluation, set skipping to false to get lexer warnings.  */
1739169695Skan      if (ifs->skip_elses)
1740169695Skan	pfile->state.skipping = 1;
1741169695Skan      else
1742169695Skan	{
1743169695Skan	  pfile->state.skipping = 0;
1744169695Skan	  pfile->state.skipping = ! _cpp_parse_expr (pfile);
1745169695Skan	  ifs->skip_elses = ! pfile->state.skipping;
1746169695Skan	}
1747169695Skan
1748169695Skan      /* Invalidate any controlling macro.  */
1749169695Skan      ifs->mi_cmacro = 0;
1750169695Skan    }
1751169695Skan}
1752169695Skan
1753169695Skan/* #endif pops the if stack and resets pfile->state.skipping.  */
1754169695Skanstatic void
1755169695Skando_endif (cpp_reader *pfile)
1756169695Skan{
1757169695Skan  cpp_buffer *buffer = pfile->buffer;
1758169695Skan  struct if_stack *ifs = buffer->if_stack;
1759169695Skan
1760169695Skan  if (ifs == NULL)
1761169695Skan    cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1762169695Skan  else
1763169695Skan    {
1764169695Skan      /* Only check EOL if was not originally skipping.  */
1765169695Skan      if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1766169695Skan	check_eol (pfile);
1767169695Skan
1768169695Skan      /* If potential control macro, we go back outside again.  */
1769169695Skan      if (ifs->next == 0 && ifs->mi_cmacro)
1770169695Skan	{
1771169695Skan	  pfile->mi_valid = true;
1772169695Skan	  pfile->mi_cmacro = ifs->mi_cmacro;
1773169695Skan	}
1774169695Skan
1775169695Skan      buffer->if_stack = ifs->next;
1776169695Skan      pfile->state.skipping = ifs->was_skipping;
1777169695Skan      obstack_free (&pfile->buffer_ob, ifs);
1778169695Skan    }
1779169695Skan}
1780169695Skan
1781169695Skan/* Push an if_stack entry for a preprocessor conditional, and set
1782169695Skan   pfile->state.skipping to SKIP.  If TYPE indicates the conditional
1783169695Skan   is #if or #ifndef, CMACRO is a potentially controlling macro, and
1784169695Skan   we need to check here that we are at the top of the file.  */
1785169695Skanstatic void
1786169695Skanpush_conditional (cpp_reader *pfile, int skip, int type,
1787169695Skan		  const cpp_hashnode *cmacro)
1788169695Skan{
1789169695Skan  struct if_stack *ifs;
1790169695Skan  cpp_buffer *buffer = pfile->buffer;
1791169695Skan
1792169695Skan  ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
1793169695Skan  ifs->line = pfile->directive_line;
1794169695Skan  ifs->next = buffer->if_stack;
1795169695Skan  ifs->skip_elses = pfile->state.skipping || !skip;
1796169695Skan  ifs->was_skipping = pfile->state.skipping;
1797169695Skan  ifs->type = type;
1798169695Skan  /* This condition is effectively a test for top-of-file.  */
1799169695Skan  if (pfile->mi_valid && pfile->mi_cmacro == 0)
1800169695Skan    ifs->mi_cmacro = cmacro;
1801169695Skan  else
1802169695Skan    ifs->mi_cmacro = 0;
1803169695Skan
1804169695Skan  pfile->state.skipping = skip;
1805169695Skan  buffer->if_stack = ifs;
1806169695Skan}
1807169695Skan
1808169695Skan/* Read the tokens of the answer into the macro pool, in a directive
1809169695Skan   of type TYPE.  Only commit the memory if we intend it as permanent
1810169695Skan   storage, i.e. the #assert case.  Returns 0 on success, and sets
1811169695Skan   ANSWERP to point to the answer.  */
1812169695Skanstatic int
1813169695Skanparse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1814169695Skan{
1815169695Skan  const cpp_token *paren;
1816169695Skan  struct answer *answer;
1817169695Skan  unsigned int acount;
1818169695Skan
1819169695Skan  /* In a conditional, it is legal to not have an open paren.  We
1820169695Skan     should save the following token in this case.  */
1821169695Skan  paren = cpp_get_token (pfile);
1822169695Skan
1823169695Skan  /* If not a paren, see if we're OK.  */
1824169695Skan  if (paren->type != CPP_OPEN_PAREN)
1825169695Skan    {
1826169695Skan      /* In a conditional no answer is a test for any answer.  It
1827169695Skan         could be followed by any token.  */
1828169695Skan      if (type == T_IF)
1829169695Skan	{
1830169695Skan	  _cpp_backup_tokens (pfile, 1);
1831169695Skan	  return 0;
1832169695Skan	}
1833169695Skan
1834169695Skan      /* #unassert with no answer is valid - it removes all answers.  */
1835169695Skan      if (type == T_UNASSERT && paren->type == CPP_EOF)
1836169695Skan	return 0;
1837169695Skan
1838169695Skan      cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
1839169695Skan      return 1;
1840169695Skan    }
1841169695Skan
1842169695Skan  for (acount = 0;; acount++)
1843169695Skan    {
1844169695Skan      size_t room_needed;
1845169695Skan      const cpp_token *token = cpp_get_token (pfile);
1846169695Skan      cpp_token *dest;
1847169695Skan
1848169695Skan      if (token->type == CPP_CLOSE_PAREN)
1849169695Skan	break;
1850169695Skan
1851169695Skan      if (token->type == CPP_EOF)
1852169695Skan	{
1853169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
1854169695Skan	  return 1;
1855169695Skan	}
1856169695Skan
1857169695Skan      /* struct answer includes the space for one token.  */
1858169695Skan      room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1859169695Skan
1860169695Skan      if (BUFF_ROOM (pfile->a_buff) < room_needed)
1861169695Skan	_cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1862169695Skan
1863169695Skan      dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1864169695Skan      *dest = *token;
1865169695Skan
1866169695Skan      /* Drop whitespace at start, for answer equivalence purposes.  */
1867169695Skan      if (acount == 0)
1868169695Skan	dest->flags &= ~PREV_WHITE;
1869169695Skan    }
1870169695Skan
1871169695Skan  if (acount == 0)
1872169695Skan    {
1873169695Skan      cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
1874169695Skan      return 1;
1875169695Skan    }
1876169695Skan
1877169695Skan  answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1878169695Skan  answer->count = acount;
1879169695Skan  answer->next = NULL;
1880169695Skan  *answerp = answer;
1881169695Skan
1882169695Skan  return 0;
1883169695Skan}
1884169695Skan
1885169695Skan/* Parses an assertion directive of type TYPE, returning a pointer to
1886169695Skan   the hash node of the predicate, or 0 on error.  If an answer was
1887169695Skan   supplied, it is placed in ANSWERP, otherwise it is set to 0.  */
1888169695Skanstatic cpp_hashnode *
1889169695Skanparse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1890169695Skan{
1891169695Skan  cpp_hashnode *result = 0;
1892169695Skan  const cpp_token *predicate;
1893169695Skan
1894169695Skan  /* We don't expand predicates or answers.  */
1895169695Skan  pfile->state.prevent_expansion++;
1896169695Skan
1897169695Skan  *answerp = 0;
1898169695Skan  predicate = cpp_get_token (pfile);
1899169695Skan  if (predicate->type == CPP_EOF)
1900169695Skan    cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
1901169695Skan  else if (predicate->type != CPP_NAME)
1902169695Skan    cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
1903169695Skan  else if (parse_answer (pfile, answerp, type) == 0)
1904169695Skan    {
1905169695Skan      unsigned int len = NODE_LEN (predicate->val.node);
1906169695Skan      unsigned char *sym = (unsigned char *) alloca (len + 1);
1907169695Skan
1908169695Skan      /* Prefix '#' to get it out of macro namespace.  */
1909169695Skan      sym[0] = '#';
1910169695Skan      memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1911169695Skan      result = cpp_lookup (pfile, sym, len + 1);
1912169695Skan    }
1913169695Skan
1914169695Skan  pfile->state.prevent_expansion--;
1915169695Skan  return result;
1916169695Skan}
1917169695Skan
1918169695Skan/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1919169695Skan   or a pointer to NULL if the answer is not in the chain.  */
1920169695Skanstatic struct answer **
1921169695Skanfind_answer (cpp_hashnode *node, const struct answer *candidate)
1922169695Skan{
1923169695Skan  unsigned int i;
1924169695Skan  struct answer **result;
1925169695Skan
1926169695Skan  for (result = &node->value.answers; *result; result = &(*result)->next)
1927169695Skan    {
1928169695Skan      struct answer *answer = *result;
1929169695Skan
1930169695Skan      if (answer->count == candidate->count)
1931169695Skan	{
1932169695Skan	  for (i = 0; i < answer->count; i++)
1933169695Skan	    if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1934169695Skan	      break;
1935169695Skan
1936169695Skan	  if (i == answer->count)
1937169695Skan	    break;
1938169695Skan	}
1939169695Skan    }
1940169695Skan
1941169695Skan  return result;
1942169695Skan}
1943169695Skan
1944169695Skan/* Test an assertion within a preprocessor conditional.  Returns
1945169695Skan   nonzero on failure, zero on success.  On success, the result of
1946169695Skan   the test is written into VALUE, otherwise the value 0.  */
1947169695Skanint
1948169695Skan_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
1949169695Skan{
1950169695Skan  struct answer *answer;
1951169695Skan  cpp_hashnode *node;
1952169695Skan
1953169695Skan  node = parse_assertion (pfile, &answer, T_IF);
1954169695Skan
1955169695Skan  /* For recovery, an erroneous assertion expression is handled as a
1956169695Skan     failing assertion.  */
1957169695Skan  *value = 0;
1958169695Skan
1959169695Skan  if (node)
1960169695Skan    *value = (node->type == NT_ASSERTION &&
1961169695Skan	      (answer == 0 || *find_answer (node, answer) != 0));
1962169695Skan  else if (pfile->cur_token[-1].type == CPP_EOF)
1963169695Skan    _cpp_backup_tokens (pfile, 1);
1964169695Skan
1965169695Skan  /* We don't commit the memory for the answer - it's temporary only.  */
1966169695Skan  return node == 0;
1967169695Skan}
1968169695Skan
1969169695Skan/* Handle #assert.  */
1970169695Skanstatic void
1971169695Skando_assert (cpp_reader *pfile)
1972169695Skan{
1973169695Skan  struct answer *new_answer;
1974169695Skan  cpp_hashnode *node;
1975169695Skan
1976169695Skan  node = parse_assertion (pfile, &new_answer, T_ASSERT);
1977169695Skan  if (node)
1978169695Skan    {
1979169695Skan      size_t answer_size;
1980169695Skan
1981169695Skan      /* Place the new answer in the answer list.  First check there
1982169695Skan         is not a duplicate.  */
1983169695Skan      new_answer->next = 0;
1984169695Skan      if (node->type == NT_ASSERTION)
1985169695Skan	{
1986169695Skan	  if (*find_answer (node, new_answer))
1987169695Skan	    {
1988169695Skan	      cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
1989169695Skan			 NODE_NAME (node) + 1);
1990169695Skan	      return;
1991169695Skan	    }
1992169695Skan	  new_answer->next = node->value.answers;
1993169695Skan	}
1994169695Skan
1995169695Skan      answer_size = sizeof (struct answer) + ((new_answer->count - 1)
1996169695Skan					      * sizeof (cpp_token));
1997169695Skan      /* Commit or allocate storage for the object.  */
1998169695Skan      if (pfile->hash_table->alloc_subobject)
1999169695Skan	{
2000169695Skan	  struct answer *temp_answer = new_answer;
2001169695Skan	  new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2002169695Skan            (answer_size);
2003169695Skan	  memcpy (new_answer, temp_answer, answer_size);
2004169695Skan	}
2005169695Skan      else
2006169695Skan	BUFF_FRONT (pfile->a_buff) += answer_size;
2007169695Skan
2008169695Skan      node->type = NT_ASSERTION;
2009169695Skan      node->value.answers = new_answer;
2010169695Skan      check_eol (pfile);
2011169695Skan    }
2012169695Skan}
2013169695Skan
2014169695Skan/* Handle #unassert.  */
2015169695Skanstatic void
2016169695Skando_unassert (cpp_reader *pfile)
2017169695Skan{
2018169695Skan  cpp_hashnode *node;
2019169695Skan  struct answer *answer;
2020169695Skan
2021169695Skan  node = parse_assertion (pfile, &answer, T_UNASSERT);
2022169695Skan  /* It isn't an error to #unassert something that isn't asserted.  */
2023169695Skan  if (node && node->type == NT_ASSERTION)
2024169695Skan    {
2025169695Skan      if (answer)
2026169695Skan	{
2027169695Skan	  struct answer **p = find_answer (node, answer), *temp;
2028169695Skan
2029169695Skan	  /* Remove the answer from the list.  */
2030169695Skan	  temp = *p;
2031169695Skan	  if (temp)
2032169695Skan	    *p = temp->next;
2033169695Skan
2034169695Skan	  /* Did we free the last answer?  */
2035169695Skan	  if (node->value.answers == 0)
2036169695Skan	    node->type = NT_VOID;
2037169695Skan
2038169695Skan	  check_eol (pfile);
2039169695Skan	}
2040169695Skan      else
2041169695Skan	_cpp_free_definition (node);
2042169695Skan    }
2043169695Skan
2044169695Skan  /* We don't commit the memory for the answer - it's temporary only.  */
2045169695Skan}
2046169695Skan
2047169695Skan/* These are for -D, -U, -A.  */
2048169695Skan
2049169695Skan/* Process the string STR as if it appeared as the body of a #define.
2050169695Skan   If STR is just an identifier, define it with value 1.
2051169695Skan   If STR has anything after the identifier, then it should
2052169695Skan   be identifier=definition.  */
2053169695Skanvoid
2054169695Skancpp_define (cpp_reader *pfile, const char *str)
2055169695Skan{
2056169695Skan  char *buf, *p;
2057169695Skan  size_t count;
2058169695Skan
2059169695Skan  /* Copy the entire option so we can modify it.
2060169695Skan     Change the first "=" in the string to a space.  If there is none,
2061169695Skan     tack " 1" on the end.  */
2062169695Skan
2063169695Skan  count = strlen (str);
2064169695Skan  buf = (char *) alloca (count + 3);
2065169695Skan  memcpy (buf, str, count);
2066169695Skan
2067169695Skan  p = strchr (str, '=');
2068169695Skan  if (p)
2069169695Skan    buf[p - str] = ' ';
2070169695Skan  else
2071169695Skan    {
2072169695Skan      buf[count++] = ' ';
2073169695Skan      buf[count++] = '1';
2074169695Skan    }
2075169695Skan  buf[count] = '\n';
2076169695Skan
2077169695Skan  run_directive (pfile, T_DEFINE, buf, count);
2078169695Skan}
2079169695Skan
2080169695Skan/* Slight variant of the above for use by initialize_builtins.  */
2081169695Skanvoid
2082169695Skan_cpp_define_builtin (cpp_reader *pfile, const char *str)
2083169695Skan{
2084169695Skan  size_t len = strlen (str);
2085169695Skan  char *buf = (char *) alloca (len + 1);
2086169695Skan  memcpy (buf, str, len);
2087169695Skan  buf[len] = '\n';
2088169695Skan  run_directive (pfile, T_DEFINE, buf, len);
2089169695Skan}
2090169695Skan
2091169695Skan/* Process MACRO as if it appeared as the body of an #undef.  */
2092169695Skanvoid
2093169695Skancpp_undef (cpp_reader *pfile, const char *macro)
2094169695Skan{
2095169695Skan  size_t len = strlen (macro);
2096169695Skan  char *buf = (char *) alloca (len + 1);
2097169695Skan  memcpy (buf, macro, len);
2098169695Skan  buf[len] = '\n';
2099169695Skan  run_directive (pfile, T_UNDEF, buf, len);
2100169695Skan}
2101169695Skan
2102169695Skan/* Process the string STR as if it appeared as the body of a #assert.  */
2103169695Skanvoid
2104169695Skancpp_assert (cpp_reader *pfile, const char *str)
2105169695Skan{
2106169695Skan  handle_assertion (pfile, str, T_ASSERT);
2107169695Skan}
2108169695Skan
2109169695Skan/* Process STR as if it appeared as the body of an #unassert.  */
2110169695Skanvoid
2111169695Skancpp_unassert (cpp_reader *pfile, const char *str)
2112169695Skan{
2113169695Skan  handle_assertion (pfile, str, T_UNASSERT);
2114169695Skan}
2115169695Skan
2116169695Skan/* Common code for cpp_assert (-A) and cpp_unassert (-A-).  */
2117169695Skanstatic void
2118169695Skanhandle_assertion (cpp_reader *pfile, const char *str, int type)
2119169695Skan{
2120169695Skan  size_t count = strlen (str);
2121169695Skan  const char *p = strchr (str, '=');
2122169695Skan
2123169695Skan  /* Copy the entire option so we can modify it.  Change the first
2124169695Skan     "=" in the string to a '(', and tack a ')' on the end.  */
2125169695Skan  char *buf = (char *) alloca (count + 2);
2126169695Skan
2127169695Skan  memcpy (buf, str, count);
2128169695Skan  if (p)
2129169695Skan    {
2130169695Skan      buf[p - str] = '(';
2131169695Skan      buf[count++] = ')';
2132169695Skan    }
2133169695Skan  buf[count] = '\n';
2134169695Skan  str = buf;
2135169695Skan
2136169695Skan  run_directive (pfile, type, str, count);
2137169695Skan}
2138169695Skan
2139169695Skan/* The number of errors for a given reader.  */
2140169695Skanunsigned int
2141169695Skancpp_errors (cpp_reader *pfile)
2142169695Skan{
2143169695Skan  return pfile->errors;
2144169695Skan}
2145169695Skan
2146169695Skan/* The options structure.  */
2147169695Skancpp_options *
2148169695Skancpp_get_options (cpp_reader *pfile)
2149169695Skan{
2150169695Skan  return &pfile->opts;
2151169695Skan}
2152169695Skan
2153169695Skan/* The callbacks structure.  */
2154169695Skancpp_callbacks *
2155169695Skancpp_get_callbacks (cpp_reader *pfile)
2156169695Skan{
2157169695Skan  return &pfile->cb;
2158169695Skan}
2159169695Skan
2160169695Skan/* Copy the given callbacks structure to our own.  */
2161169695Skanvoid
2162169695Skancpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2163169695Skan{
2164169695Skan  pfile->cb = *cb;
2165169695Skan}
2166169695Skan
2167169695Skan/* The dependencies structure.  (Creates one if it hasn't already been.)  */
2168169695Skanstruct deps *
2169169695Skancpp_get_deps (cpp_reader *pfile)
2170169695Skan{
2171169695Skan  if (!pfile->deps)
2172169695Skan    pfile->deps = deps_init ();
2173169695Skan  return pfile->deps;
2174169695Skan}
2175169695Skan
2176169695Skan/* Push a new buffer on the buffer stack.  Returns the new buffer; it
2177169695Skan   doesn't fail.  It does not generate a file change call back; that
2178169695Skan   is the responsibility of the caller.  */
2179169695Skancpp_buffer *
2180169695Skancpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2181169695Skan		 int from_stage3)
2182169695Skan{
2183169695Skan  cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2184169695Skan
2185169695Skan  /* Clears, amongst other things, if_stack and mi_cmacro.  */
2186169695Skan  memset (new_buffer, 0, sizeof (cpp_buffer));
2187169695Skan
2188169695Skan  new_buffer->next_line = new_buffer->buf = buffer;
2189169695Skan  new_buffer->rlimit = buffer + len;
2190169695Skan  new_buffer->from_stage3 = from_stage3;
2191169695Skan  new_buffer->prev = pfile->buffer;
2192169695Skan  new_buffer->need_line = true;
2193169695Skan
2194169695Skan  pfile->buffer = new_buffer;
2195169695Skan
2196169695Skan  return new_buffer;
2197169695Skan}
2198169695Skan
2199169695Skan/* Pops a single buffer, with a file change call-back if appropriate.
2200169695Skan   Then pushes the next -include file, if any remain.  */
2201169695Skanvoid
2202169695Skan_cpp_pop_buffer (cpp_reader *pfile)
2203169695Skan{
2204169695Skan  cpp_buffer *buffer = pfile->buffer;
2205169695Skan  struct _cpp_file *inc = buffer->file;
2206169695Skan  struct if_stack *ifs;
2207169695Skan
2208169695Skan  /* Walk back up the conditional stack till we reach its level at
2209169695Skan     entry to this file, issuing error messages.  */
2210169695Skan  for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2211169695Skan    cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2212169695Skan			 "unterminated #%s", dtable[ifs->type].name);
2213169695Skan
2214169695Skan  /* In case of a missing #endif.  */
2215169695Skan  pfile->state.skipping = 0;
2216169695Skan
2217169695Skan  /* _cpp_do_file_change expects pfile->buffer to be the new one.  */
2218169695Skan  pfile->buffer = buffer->prev;
2219169695Skan
2220169695Skan  free (buffer->notes);
2221169695Skan
2222169695Skan  /* Free the buffer object now; we may want to push a new buffer
2223169695Skan     in _cpp_push_next_include_file.  */
2224169695Skan  obstack_free (&pfile->buffer_ob, buffer);
2225169695Skan
2226169695Skan  if (inc)
2227169695Skan    {
2228169695Skan      _cpp_pop_file_buffer (pfile, inc);
2229169695Skan
2230169695Skan      _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2231169695Skan    }
2232169695Skan}
2233169695Skan
2234169695Skan/* Enter all recognized directives in the hash table.  */
2235169695Skanvoid
2236169695Skan_cpp_init_directives (cpp_reader *pfile)
2237169695Skan{
2238169695Skan  unsigned int i;
2239169695Skan  cpp_hashnode *node;
2240169695Skan
2241169695Skan  for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2242169695Skan    {
2243169695Skan      node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2244169695Skan      node->is_directive = 1;
2245169695Skan      node->directive_index = i;
2246169695Skan    }
2247169695Skan}
2248