directives.c revision 169695
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
426169695Skan	 puts a space in front of any '#' at the start of a macro.  */
427169695Skan      if (CPP_OPTION (pfile, preprocessed)
428169695Skan	  && (indented || !(dir->flags & IN_I)))
429169695Skan	{
430169695Skan	  skip = 0;
431169695Skan	  dir = 0;
432169695Skan	}
433169695Skan      else
434169695Skan	{
435169695Skan	  /* In failed conditional groups, all non-conditional
436169695Skan	     directives are ignored.  Before doing that, whether
437169695Skan	     skipping or not, we should lex angle-bracketed headers
438169695Skan	     correctly, and maybe output some diagnostics.  */
439169695Skan	  pfile->state.angled_headers = dir->flags & INCL;
440169695Skan	  pfile->state.directive_wants_padding = dir->flags & INCL;
441169695Skan	  if (! CPP_OPTION (pfile, preprocessed))
442169695Skan	    directive_diagnostics (pfile, dir, indented);
443169695Skan	  if (pfile->state.skipping && !(dir->flags & COND))
444169695Skan	    dir = 0;
445169695Skan	}
446169695Skan    }
447169695Skan  else if (dname->type == CPP_EOF)
448169695Skan    ;	/* CPP_EOF is the "null directive".  */
449169695Skan  else
450169695Skan    {
451169695Skan      /* An unknown directive.  Don't complain about it in assembly
452169695Skan	 source: we don't know where the comments are, and # may
453169695Skan	 introduce assembler pseudo-ops.  Don't complain about invalid
454169695Skan	 directives in skipped conditional groups (6.10 p4).  */
455169695Skan      if (CPP_OPTION (pfile, lang) == CLK_ASM)
456169695Skan	skip = 0;
457169695Skan      else if (!pfile->state.skipping)
458169695Skan	cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
459169695Skan		   cpp_token_as_text (pfile, dname));
460169695Skan    }
461169695Skan
462169695Skan  pfile->directive = dir;
463169695Skan  if (CPP_OPTION (pfile, traditional))
464169695Skan    prepare_directive_trad (pfile);
465169695Skan
466169695Skan  if (dir)
467169695Skan    pfile->directive->handler (pfile);
468169695Skan  else if (skip == 0)
469169695Skan    _cpp_backup_tokens (pfile, 1);
470169695Skan
471169695Skan  end_directive (pfile, skip);
472169695Skan  if (was_parsing_args)
473169695Skan    {
474169695Skan      /* Restore state when within macro args.  */
475169695Skan      pfile->state.parsing_args = 2;
476169695Skan      pfile->state.prevent_expansion = 1;
477169695Skan    }
478169695Skan  if (was_discarding_output)
479169695Skan    pfile->state.prevent_expansion = 1;
480169695Skan  return skip;
481169695Skan}
482169695Skan
483169695Skan/* Directive handler wrapper used by the command line option
484169695Skan   processor.  BUF is \n terminated.  */
485169695Skanstatic void
486169695Skanrun_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
487169695Skan{
488169695Skan  cpp_push_buffer (pfile, (const uchar *) buf, count,
489169695Skan		   /* from_stage3 */ true);
490169695Skan  start_directive (pfile);
491169695Skan
492169695Skan  /* This is a short-term fix to prevent a leading '#' being
493169695Skan     interpreted as a directive.  */
494169695Skan  _cpp_clean_line (pfile);
495169695Skan
496169695Skan  pfile->directive = &dtable[dir_no];
497169695Skan  if (CPP_OPTION (pfile, traditional))
498169695Skan    prepare_directive_trad (pfile);
499169695Skan  pfile->directive->handler (pfile);
500169695Skan  end_directive (pfile, 1);
501169695Skan  _cpp_pop_buffer (pfile);
502169695Skan}
503169695Skan
504169695Skan/* Checks for validity the macro name in #define, #undef, #ifdef and
505169695Skan   #ifndef directives.  */
506169695Skanstatic cpp_hashnode *
507169695Skanlex_macro_node (cpp_reader *pfile)
508169695Skan{
509169695Skan  const cpp_token *token = _cpp_lex_token (pfile);
510169695Skan
511169695Skan  /* The token immediately after #define must be an identifier.  That
512169695Skan     identifier may not be "defined", per C99 6.10.8p4.
513169695Skan     In C++, it may not be any of the "named operators" either,
514169695Skan     per C++98 [lex.digraph], [lex.key].
515169695Skan     Finally, the identifier may not have been poisoned.  (In that case
516169695Skan     the lexer has issued the error message for us.)  */
517169695Skan
518169695Skan  if (token->type == CPP_NAME)
519169695Skan    {
520169695Skan      cpp_hashnode *node = token->val.node;
521169695Skan
522169695Skan      if (node == pfile->spec_nodes.n_defined)
523169695Skan	cpp_error (pfile, CPP_DL_ERROR,
524169695Skan		   "\"defined\" cannot be used as a macro name");
525169695Skan      else if (! (node->flags & NODE_POISONED))
526169695Skan	return node;
527169695Skan    }
528169695Skan  else if (token->flags & NAMED_OP)
529169695Skan    cpp_error (pfile, CPP_DL_ERROR,
530169695Skan       "\"%s\" cannot be used as a macro name as it is an operator in C++",
531169695Skan	       NODE_NAME (token->val.node));
532169695Skan  else if (token->type == CPP_EOF)
533169695Skan    cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
534169695Skan	       pfile->directive->name);
535169695Skan  else
536169695Skan    cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
537169695Skan
538169695Skan  return NULL;
539169695Skan}
540169695Skan
541169695Skan/* Process a #define directive.  Most work is done in macro.c.  */
542169695Skanstatic void
543169695Skando_define (cpp_reader *pfile)
544169695Skan{
545169695Skan  cpp_hashnode *node = lex_macro_node (pfile);
546169695Skan
547169695Skan  if (node)
548169695Skan    {
549169695Skan      /* If we have been requested to expand comments into macros,
550169695Skan	 then re-enable saving of comments.  */
551169695Skan      pfile->state.save_comments =
552169695Skan	! CPP_OPTION (pfile, discard_comments_in_macro_exp);
553169695Skan
554169695Skan      if (_cpp_create_definition (pfile, node))
555169695Skan	if (pfile->cb.define)
556169695Skan	  pfile->cb.define (pfile, pfile->directive_line, node);
557169695Skan    }
558169695Skan}
559169695Skan
560169695Skan/* Handle #undef.  Mark the identifier NT_VOID in the hash table.  */
561169695Skanstatic void
562169695Skando_undef (cpp_reader *pfile)
563169695Skan{
564169695Skan  cpp_hashnode *node = lex_macro_node (pfile);
565169695Skan
566169695Skan  if (node)
567169695Skan    {
568169695Skan      if (pfile->cb.undef)
569169695Skan	pfile->cb.undef (pfile, pfile->directive_line, node);
570169695Skan
571169695Skan      /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
572169695Skan	 identifier is not currently defined as a macro name.  */
573169695Skan      if (node->type == NT_MACRO)
574169695Skan	{
575169695Skan	  if (node->flags & NODE_WARN)
576169695Skan	    cpp_error (pfile, CPP_DL_WARNING,
577169695Skan		       "undefining \"%s\"", NODE_NAME (node));
578169695Skan
579169695Skan	  if (CPP_OPTION (pfile, warn_unused_macros))
580169695Skan	    _cpp_warn_if_unused_macro (pfile, node, NULL);
581169695Skan
582169695Skan	  _cpp_free_definition (node);
583169695Skan	}
584169695Skan    }
585169695Skan
586169695Skan  check_eol (pfile);
587169695Skan}
588169695Skan
589169695Skan/* Undefine a single macro/assertion/whatever.  */
590169695Skan
591169695Skanstatic int
592169695Skanundefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
593169695Skan		 void *data_p ATTRIBUTE_UNUSED)
594169695Skan{
595169695Skan  /* Body of _cpp_free_definition inlined here for speed.
596169695Skan     Macros and assertions no longer have anything to free.  */
597169695Skan  h->type = NT_VOID;
598169695Skan  h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
599169695Skan  return 1;
600169695Skan}
601169695Skan
602169695Skan/* Undefine all macros and assertions.  */
603169695Skan
604169695Skanvoid
605169695Skancpp_undef_all (cpp_reader *pfile)
606169695Skan{
607169695Skan  cpp_forall_identifiers (pfile, undefine_macros, NULL);
608169695Skan}
609169695Skan
610169695Skan
611169695Skan/* Helper routine used by parse_include.  Reinterpret the current line
612169695Skan   as an h-char-sequence (< ... >); we are looking at the first token
613169695Skan   after the <.  Returns a malloced filename.  */
614169695Skanstatic char *
615169695Skanglue_header_name (cpp_reader *pfile)
616169695Skan{
617169695Skan  const cpp_token *token;
618169695Skan  char *buffer;
619169695Skan  size_t len, total_len = 0, capacity = 1024;
620169695Skan
621169695Skan  /* To avoid lexed tokens overwriting our glued name, we can only
622169695Skan     allocate from the string pool once we've lexed everything.  */
623169695Skan  buffer = XNEWVEC (char, capacity);
624169695Skan  for (;;)
625169695Skan    {
626169695Skan      token = get_token_no_padding (pfile);
627169695Skan
628169695Skan      if (token->type == CPP_GREATER)
629169695Skan	break;
630169695Skan      if (token->type == CPP_EOF)
631169695Skan	{
632169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
633169695Skan	  break;
634169695Skan	}
635169695Skan
636169695Skan      len = cpp_token_len (token) + 2; /* Leading space, terminating \0.  */
637169695Skan      if (total_len + len > capacity)
638169695Skan	{
639169695Skan	  capacity = (capacity + len) * 2;
640169695Skan	  buffer = XRESIZEVEC (char, buffer, capacity);
641169695Skan	}
642169695Skan
643169695Skan      if (token->flags & PREV_WHITE)
644169695Skan	buffer[total_len++] = ' ';
645169695Skan
646169695Skan      total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
647169695Skan				    true)
648169695Skan		   - (uchar *) buffer);
649169695Skan    }
650169695Skan
651169695Skan  buffer[total_len] = '\0';
652169695Skan  return buffer;
653169695Skan}
654169695Skan
655169695Skan/* Returns the file name of #include, #include_next, #import and
656169695Skan   #pragma dependency.  The string is malloced and the caller should
657169695Skan   free it.  Returns NULL on error.  */
658169695Skanstatic const char *
659169695Skanparse_include (cpp_reader *pfile, int *pangle_brackets,
660169695Skan	       const cpp_token ***buf)
661169695Skan{
662169695Skan  char *fname;
663169695Skan  const cpp_token *header;
664169695Skan
665169695Skan  /* Allow macro expansion.  */
666169695Skan  header = get_token_no_padding (pfile);
667169695Skan  if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
668169695Skan    {
669169695Skan      fname = XNEWVEC (char, header->val.str.len - 1);
670169695Skan      memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
671169695Skan      fname[header->val.str.len - 2] = '\0';
672169695Skan      *pangle_brackets = header->type == CPP_HEADER_NAME;
673169695Skan    }
674169695Skan  else if (header->type == CPP_LESS)
675169695Skan    {
676169695Skan      fname = glue_header_name (pfile);
677169695Skan      *pangle_brackets = 1;
678169695Skan    }
679169695Skan  else
680169695Skan    {
681169695Skan      const unsigned char *dir;
682169695Skan
683169695Skan      if (pfile->directive == &dtable[T_PRAGMA])
684169695Skan	dir = U"pragma dependency";
685169695Skan      else
686169695Skan	dir = pfile->directive->name;
687169695Skan      cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
688169695Skan		 dir);
689169695Skan
690169695Skan      return NULL;
691169695Skan    }
692169695Skan
693169695Skan  if (buf == NULL || CPP_OPTION (pfile, discard_comments))
694169695Skan    check_eol (pfile);
695169695Skan  else
696169695Skan    {
697169695Skan      /* If we are not discarding comments, then gather them while
698169695Skan	 doing the eol check.  */
699169695Skan      *buf = check_eol_return_comments (pfile);
700169695Skan    }
701169695Skan
702169695Skan  return fname;
703169695Skan}
704169695Skan
705169695Skan/* Handle #include, #include_next and #import.  */
706169695Skanstatic void
707169695Skando_include_common (cpp_reader *pfile, enum include_type type)
708169695Skan{
709169695Skan  const char *fname;
710169695Skan  int angle_brackets;
711169695Skan  const cpp_token **buf = NULL;
712169695Skan
713169695Skan  /* Re-enable saving of comments if requested, so that the include
714169695Skan     callback can dump comments which follow #include.  */
715169695Skan  pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
716169695Skan
717169695Skan  fname = parse_include (pfile, &angle_brackets, &buf);
718169695Skan  if (!fname)
719169695Skan    {
720169695Skan      if (buf)
721169695Skan	XDELETEVEC (buf);
722169695Skan      return;
723169695Skan    }
724169695Skan
725169695Skan  if (!*fname)
726169695Skan  {
727169695Skan    cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
728169695Skan               pfile->directive->name);
729169695Skan    XDELETEVEC (fname);
730169695Skan    if (buf)
731169695Skan      XDELETEVEC (buf);
732169695Skan    return;
733169695Skan  }
734169695Skan
735169695Skan  /* Prevent #include recursion.  */
736169695Skan  if (pfile->line_table->depth >= CPP_STACK_MAX)
737169695Skan    cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
738169695Skan  else
739169695Skan    {
740169695Skan      /* Get out of macro context, if we are.  */
741169695Skan      skip_rest_of_line (pfile);
742169695Skan
743169695Skan      if (pfile->cb.include)
744169695Skan	pfile->cb.include (pfile, pfile->directive_line,
745169695Skan			   pfile->directive->name, fname, angle_brackets,
746169695Skan			   buf);
747169695Skan
748169695Skan      _cpp_stack_include (pfile, fname, angle_brackets, type);
749169695Skan    }
750169695Skan
751169695Skan  XDELETEVEC (fname);
752169695Skan  if (buf)
753169695Skan    XDELETEVEC (buf);
754169695Skan}
755169695Skan
756169695Skanstatic void
757169695Skando_include (cpp_reader *pfile)
758169695Skan{
759169695Skan  do_include_common (pfile, IT_INCLUDE);
760169695Skan}
761169695Skan
762169695Skanstatic void
763169695Skando_import (cpp_reader *pfile)
764169695Skan{
765169695Skan  do_include_common (pfile, IT_IMPORT);
766169695Skan}
767169695Skan
768169695Skanstatic void
769169695Skando_include_next (cpp_reader *pfile)
770169695Skan{
771169695Skan  enum include_type type = IT_INCLUDE_NEXT;
772169695Skan
773169695Skan  /* If this is the primary source file, warn and use the normal
774169695Skan     search logic.  */
775169695Skan  if (! pfile->buffer->prev)
776169695Skan    {
777169695Skan      cpp_error (pfile, CPP_DL_WARNING,
778169695Skan		 "#include_next in primary source file");
779169695Skan      type = IT_INCLUDE;
780169695Skan    }
781169695Skan  do_include_common (pfile, type);
782169695Skan}
783169695Skan
784169695Skan/* Subroutine of do_linemarker.  Read possible flags after file name.
785169695Skan   LAST is the last flag seen; 0 if this is the first flag. Return the
786169695Skan   flag if it is valid, 0 at the end of the directive. Otherwise
787169695Skan   complain.  */
788169695Skanstatic unsigned int
789169695Skanread_flag (cpp_reader *pfile, unsigned int last)
790169695Skan{
791169695Skan  const cpp_token *token = _cpp_lex_token (pfile);
792169695Skan
793169695Skan  if (token->type == CPP_NUMBER && token->val.str.len == 1)
794169695Skan    {
795169695Skan      unsigned int flag = token->val.str.text[0] - '0';
796169695Skan
797169695Skan      if (flag > last && flag <= 4
798169695Skan	  && (flag != 4 || last == 3)
799169695Skan	  && (flag != 2 || last == 0))
800169695Skan	return flag;
801169695Skan    }
802169695Skan
803169695Skan  if (token->type != CPP_EOF)
804169695Skan    cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
805169695Skan	       cpp_token_as_text (pfile, token));
806169695Skan  return 0;
807169695Skan}
808169695Skan
809169695Skan/* Subroutine of do_line and do_linemarker.  Convert a number in STR,
810169695Skan   of length LEN, to binary; store it in NUMP, and return 0 if the
811169695Skan   number was well-formed, 1 if not.  Temporary, hopefully.  */
812169695Skanstatic int
813169695Skanstrtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
814169695Skan{
815169695Skan  unsigned long reg = 0;
816169695Skan  uchar c;
817169695Skan  while (len--)
818169695Skan    {
819169695Skan      c = *str++;
820169695Skan      if (!ISDIGIT (c))
821169695Skan	return 1;
822169695Skan      reg *= 10;
823169695Skan      reg += c - '0';
824169695Skan    }
825169695Skan  *nump = reg;
826169695Skan  return 0;
827169695Skan}
828169695Skan
829169695Skan/* Interpret #line command.
830169695Skan   Note that the filename string (if any) is a true string constant
831169695Skan   (escapes are interpreted), unlike in #line.  */
832169695Skanstatic void
833169695Skando_line (cpp_reader *pfile)
834169695Skan{
835169695Skan  const struct line_maps *line_table = pfile->line_table;
836169695Skan  const struct line_map *map = &line_table->maps[line_table->used - 1];
837169695Skan
838169695Skan  /* skip_rest_of_line() may cause line table to be realloc()ed so note down
839169695Skan     sysp right now.  */
840169695Skan
841169695Skan  unsigned char map_sysp = map->sysp;
842169695Skan  const cpp_token *token;
843169695Skan  const char *new_file = map->to_file;
844169695Skan  unsigned long new_lineno;
845169695Skan
846169695Skan  /* C99 raised the minimum limit on #line numbers.  */
847169695Skan  unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
848169695Skan
849169695Skan  /* #line commands expand macros.  */
850169695Skan  token = cpp_get_token (pfile);
851169695Skan  if (token->type != CPP_NUMBER
852169695Skan      || strtoul_for_line (token->val.str.text, token->val.str.len,
853169695Skan			   &new_lineno))
854169695Skan    {
855169695Skan      cpp_error (pfile, CPP_DL_ERROR,
856169695Skan		 "\"%s\" after #line is not a positive integer",
857169695Skan		 cpp_token_as_text (pfile, token));
858169695Skan      return;
859169695Skan    }
860169695Skan
861169695Skan  if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
862169695Skan    cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
863169695Skan
864169695Skan  token = cpp_get_token (pfile);
865169695Skan  if (token->type == CPP_STRING)
866169695Skan    {
867169695Skan      cpp_string s = { 0, 0 };
868169695Skan      if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
869169695Skan					    &s, false))
870169695Skan	new_file = (const char *)s.text;
871169695Skan      check_eol (pfile);
872169695Skan    }
873169695Skan  else if (token->type != CPP_EOF)
874169695Skan    {
875169695Skan      cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
876169695Skan		 cpp_token_as_text (pfile, token));
877169695Skan      return;
878169695Skan    }
879169695Skan
880169695Skan  skip_rest_of_line (pfile);
881169695Skan  _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
882169695Skan		       map_sysp);
883169695Skan}
884169695Skan
885169695Skan/* Interpret the # 44 "file" [flags] notation, which has slightly
886169695Skan   different syntax and semantics from #line:  Flags are allowed,
887169695Skan   and we never complain about the line number being too big.  */
888169695Skanstatic void
889169695Skando_linemarker (cpp_reader *pfile)
890169695Skan{
891169695Skan  const struct line_maps *line_table = pfile->line_table;
892169695Skan  const struct line_map *map = &line_table->maps[line_table->used - 1];
893169695Skan  const cpp_token *token;
894169695Skan  const char *new_file = map->to_file;
895169695Skan  unsigned long new_lineno;
896169695Skan  unsigned int new_sysp = map->sysp;
897169695Skan  enum lc_reason reason = LC_RENAME;
898169695Skan  int flag;
899169695Skan
900169695Skan  /* Back up so we can get the number again.  Putting this in
901169695Skan     _cpp_handle_directive risks two calls to _cpp_backup_tokens in
902169695Skan     some circumstances, which can segfault.  */
903169695Skan  _cpp_backup_tokens (pfile, 1);
904169695Skan
905169695Skan  /* #line commands expand macros.  */
906169695Skan  token = cpp_get_token (pfile);
907169695Skan  if (token->type != CPP_NUMBER
908169695Skan      || strtoul_for_line (token->val.str.text, token->val.str.len,
909169695Skan			   &new_lineno))
910169695Skan    {
911169695Skan      cpp_error (pfile, CPP_DL_ERROR,
912169695Skan		 "\"%s\" after # is not a positive integer",
913169695Skan		 cpp_token_as_text (pfile, token));
914169695Skan      return;
915169695Skan    }
916169695Skan
917169695Skan  token = cpp_get_token (pfile);
918169695Skan  if (token->type == CPP_STRING)
919169695Skan    {
920169695Skan      cpp_string s = { 0, 0 };
921169695Skan      if (cpp_interpret_string_notranslate (pfile, &token->val.str,
922169695Skan					    1, &s, false))
923169695Skan	new_file = (const char *)s.text;
924169695Skan
925169695Skan      new_sysp = 0;
926169695Skan      flag = read_flag (pfile, 0);
927169695Skan      if (flag == 1)
928169695Skan	{
929169695Skan	  reason = LC_ENTER;
930169695Skan	  /* Fake an include for cpp_included ().  */
931169695Skan	  _cpp_fake_include (pfile, new_file);
932169695Skan	  flag = read_flag (pfile, flag);
933169695Skan	}
934169695Skan      else if (flag == 2)
935169695Skan	{
936169695Skan	  reason = LC_LEAVE;
937169695Skan	  flag = read_flag (pfile, flag);
938169695Skan	}
939169695Skan      if (flag == 3)
940169695Skan	{
941169695Skan	  new_sysp = 1;
942169695Skan	  flag = read_flag (pfile, flag);
943169695Skan	  if (flag == 4)
944169695Skan	    new_sysp = 2;
945169695Skan	}
946169695Skan      pfile->buffer->sysp = new_sysp;
947169695Skan
948169695Skan      check_eol (pfile);
949169695Skan    }
950169695Skan  else if (token->type != CPP_EOF)
951169695Skan    {
952169695Skan      cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
953169695Skan		 cpp_token_as_text (pfile, token));
954169695Skan      return;
955169695Skan    }
956169695Skan
957169695Skan  skip_rest_of_line (pfile);
958169695Skan  _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
959169695Skan}
960169695Skan
961169695Skan/* Arrange the file_change callback.  pfile->line has changed to
962169695Skan   FILE_LINE of TO_FILE, for reason REASON.  SYSP is 1 for a system
963169695Skan   header, 2 for a system header that needs to be extern "C" protected,
964169695Skan   and zero otherwise.  */
965169695Skanvoid
966169695Skan_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
967169695Skan		     const char *to_file, unsigned int file_line,
968169695Skan		     unsigned int sysp)
969169695Skan{
970169695Skan  const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
971169695Skan					    to_file, file_line);
972169695Skan  if (map != NULL)
973169695Skan    linemap_line_start (pfile->line_table, map->to_line, 127);
974169695Skan
975169695Skan  if (pfile->cb.file_change)
976169695Skan    pfile->cb.file_change (pfile, map);
977169695Skan}
978169695Skan
979169695Skan/* Report a warning or error detected by the program we are
980169695Skan   processing.  Use the directive's tokens in the error message.  */
981169695Skanstatic void
982169695Skando_diagnostic (cpp_reader *pfile, int code, int print_dir)
983169695Skan{
984169695Skan  if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
985169695Skan    {
986169695Skan      if (print_dir)
987169695Skan	fprintf (stderr, "#%s ", pfile->directive->name);
988169695Skan      pfile->state.prevent_expansion++;
989169695Skan      cpp_output_line (pfile, stderr);
990169695Skan      pfile->state.prevent_expansion--;
991169695Skan    }
992169695Skan}
993169695Skan
994169695Skanstatic void
995169695Skando_error (cpp_reader *pfile)
996169695Skan{
997169695Skan  do_diagnostic (pfile, CPP_DL_ERROR, 1);
998169695Skan}
999169695Skan
1000169695Skanstatic void
1001169695Skando_warning (cpp_reader *pfile)
1002169695Skan{
1003169695Skan  /* We want #warning diagnostics to be emitted in system headers too.  */
1004169695Skan  do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
1005169695Skan}
1006169695Skan
1007169695Skan/* Report program identification.  */
1008169695Skanstatic void
1009169695Skando_ident (cpp_reader *pfile)
1010169695Skan{
1011169695Skan  const cpp_token *str = cpp_get_token (pfile);
1012169695Skan
1013169695Skan  if (str->type != CPP_STRING)
1014169695Skan    cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1015169695Skan	       pfile->directive->name);
1016169695Skan  else if (pfile->cb.ident)
1017169695Skan    pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1018169695Skan
1019169695Skan  check_eol (pfile);
1020169695Skan}
1021169695Skan
1022169695Skan/* Lookup a PRAGMA name in a singly-linked CHAIN.  Returns the
1023169695Skan   matching entry, or NULL if none is found.  The returned entry could
1024169695Skan   be the start of a namespace chain, or a pragma.  */
1025169695Skanstatic struct pragma_entry *
1026169695Skanlookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1027169695Skan{
1028169695Skan  while (chain && chain->pragma != pragma)
1029169695Skan    chain = chain->next;
1030169695Skan
1031169695Skan  return chain;
1032169695Skan}
1033169695Skan
1034169695Skan/* Create and insert a blank pragma entry at the beginning of a
1035169695Skan   singly-linked CHAIN.  */
1036169695Skanstatic struct pragma_entry *
1037169695Skannew_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1038169695Skan{
1039169695Skan  struct pragma_entry *new_entry;
1040169695Skan
1041169695Skan  new_entry = (struct pragma_entry *)
1042169695Skan    _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1043169695Skan
1044169695Skan  memset (new_entry, 0, sizeof (struct pragma_entry));
1045169695Skan  new_entry->next = *chain;
1046169695Skan
1047169695Skan  *chain = new_entry;
1048169695Skan  return new_entry;
1049169695Skan}
1050169695Skan
1051169695Skan/* Register a pragma NAME in namespace SPACE.  If SPACE is null, it
1052169695Skan   goes in the global namespace.  */
1053169695Skanstatic struct pragma_entry *
1054169695Skanregister_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1055169695Skan		   bool allow_name_expansion)
1056169695Skan{
1057169695Skan  struct pragma_entry **chain = &pfile->pragmas;
1058169695Skan  struct pragma_entry *entry;
1059169695Skan  const cpp_hashnode *node;
1060169695Skan
1061169695Skan  if (space)
1062169695Skan    {
1063169695Skan      node = cpp_lookup (pfile, U space, strlen (space));
1064169695Skan      entry = lookup_pragma_entry (*chain, node);
1065169695Skan      if (!entry)
1066169695Skan	{
1067169695Skan	  entry = new_pragma_entry (pfile, chain);
1068169695Skan	  entry->pragma = node;
1069169695Skan	  entry->is_nspace = true;
1070169695Skan	  entry->allow_expansion = allow_name_expansion;
1071169695Skan	}
1072169695Skan      else if (!entry->is_nspace)
1073169695Skan	goto clash;
1074169695Skan      else if (entry->allow_expansion != allow_name_expansion)
1075169695Skan	{
1076169695Skan	  cpp_error (pfile, CPP_DL_ICE,
1077169695Skan		     "registering pragmas in namespace \"%s\" with mismatched "
1078169695Skan		     "name expansion", space);
1079169695Skan	  return NULL;
1080169695Skan	}
1081169695Skan      chain = &entry->u.space;
1082169695Skan    }
1083169695Skan  else if (allow_name_expansion)
1084169695Skan    {
1085169695Skan      cpp_error (pfile, CPP_DL_ICE,
1086169695Skan		 "registering pragma \"%s\" with name expansion "
1087169695Skan		 "and no namespace", name);
1088169695Skan      return NULL;
1089169695Skan    }
1090169695Skan
1091169695Skan  /* Check for duplicates.  */
1092169695Skan  node = cpp_lookup (pfile, U name, strlen (name));
1093169695Skan  entry = lookup_pragma_entry (*chain, node);
1094169695Skan  if (entry == NULL)
1095169695Skan    {
1096169695Skan      entry = new_pragma_entry (pfile, chain);
1097169695Skan      entry->pragma = node;
1098169695Skan      return entry;
1099169695Skan    }
1100169695Skan
1101169695Skan  if (entry->is_nspace)
1102169695Skan    clash:
1103169695Skan    cpp_error (pfile, CPP_DL_ICE,
1104169695Skan	       "registering \"%s\" as both a pragma and a pragma namespace",
1105169695Skan	       NODE_NAME (node));
1106169695Skan  else if (space)
1107169695Skan    cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1108169695Skan	       space, name);
1109169695Skan  else
1110169695Skan    cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1111169695Skan
1112169695Skan  return NULL;
1113169695Skan}
1114169695Skan
1115169695Skan/* Register a cpplib internal pragma SPACE NAME with HANDLER.  */
1116169695Skanstatic void
1117169695Skanregister_pragma_internal (cpp_reader *pfile, const char *space,
1118169695Skan			  const char *name, pragma_cb handler)
1119169695Skan{
1120169695Skan  struct pragma_entry *entry;
1121169695Skan
1122169695Skan  entry = register_pragma_1 (pfile, space, name, false);
1123169695Skan  entry->is_internal = true;
1124169695Skan  entry->u.handler = handler;
1125169695Skan}
1126169695Skan
1127169695Skan/* Register a pragma NAME in namespace SPACE.  If SPACE is null, it
1128169695Skan   goes in the global namespace.  HANDLER is the handler it will call,
1129169695Skan   which must be non-NULL.  If ALLOW_EXPANSION is set, allow macro
1130169695Skan   expansion while parsing pragma NAME.  This function is exported
1131169695Skan   from libcpp. */
1132169695Skanvoid
1133169695Skancpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1134169695Skan		     pragma_cb handler, bool allow_expansion)
1135169695Skan{
1136169695Skan  struct pragma_entry *entry;
1137169695Skan
1138169695Skan  if (!handler)
1139169695Skan    {
1140169695Skan      cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1141169695Skan      return;
1142169695Skan    }
1143169695Skan
1144169695Skan  entry = register_pragma_1 (pfile, space, name, false);
1145169695Skan  if (entry)
1146169695Skan    {
1147169695Skan      entry->allow_expansion = allow_expansion;
1148169695Skan      entry->u.handler = handler;
1149169695Skan    }
1150169695Skan}
1151169695Skan
1152169695Skan/* Similarly, but create mark the pragma for deferred processing.
1153169695Skan   When found, a CPP_PRAGMA token will be insertted into the stream
1154169695Skan   with IDENT in the token->u.pragma slot.  */
1155169695Skanvoid
1156169695Skancpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1157169695Skan			      const char *name, unsigned int ident,
1158169695Skan			      bool allow_expansion, bool allow_name_expansion)
1159169695Skan{
1160169695Skan  struct pragma_entry *entry;
1161169695Skan
1162169695Skan  entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1163169695Skan  if (entry)
1164169695Skan    {
1165169695Skan      entry->is_deferred = true;
1166169695Skan      entry->allow_expansion = allow_expansion;
1167169695Skan      entry->u.ident = ident;
1168169695Skan    }
1169169695Skan}
1170169695Skan
1171169695Skan/* Register the pragmas the preprocessor itself handles.  */
1172169695Skanvoid
1173169695Skan_cpp_init_internal_pragmas (cpp_reader *pfile)
1174169695Skan{
1175169695Skan  /* Pragmas in the global namespace.  */
1176169695Skan  register_pragma_internal (pfile, 0, "once", do_pragma_once);
1177169695Skan
1178169695Skan  /* New GCC-specific pragmas should be put in the GCC namespace.  */
1179169695Skan  register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1180169695Skan  register_pragma_internal (pfile, "GCC", "system_header",
1181169695Skan			    do_pragma_system_header);
1182169695Skan  register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1183169695Skan}
1184169695Skan
1185169695Skan/* Return the number of registered pragmas in PE.  */
1186169695Skan
1187169695Skanstatic int
1188169695Skancount_registered_pragmas (struct pragma_entry *pe)
1189169695Skan{
1190169695Skan  int ct = 0;
1191169695Skan  for (; pe != NULL; pe = pe->next)
1192169695Skan    {
1193169695Skan      if (pe->is_nspace)
1194169695Skan	ct += count_registered_pragmas (pe->u.space);
1195169695Skan      ct++;
1196169695Skan    }
1197169695Skan  return ct;
1198169695Skan}
1199169695Skan
1200169695Skan/* Save into SD the names of the registered pragmas referenced by PE,
1201169695Skan   and return a pointer to the next free space in SD.  */
1202169695Skan
1203169695Skanstatic char **
1204169695Skansave_registered_pragmas (struct pragma_entry *pe, char **sd)
1205169695Skan{
1206169695Skan  for (; pe != NULL; pe = pe->next)
1207169695Skan    {
1208169695Skan      if (pe->is_nspace)
1209169695Skan	sd = save_registered_pragmas (pe->u.space, sd);
1210169695Skan      *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1211169695Skan                                HT_LEN (&pe->pragma->ident),
1212169695Skan                                HT_LEN (&pe->pragma->ident) + 1);
1213169695Skan    }
1214169695Skan  return sd;
1215169695Skan}
1216169695Skan
1217169695Skan/* Return a newly-allocated array which saves the names of the
1218169695Skan   registered pragmas.  */
1219169695Skan
1220169695Skanchar **
1221169695Skan_cpp_save_pragma_names (cpp_reader *pfile)
1222169695Skan{
1223169695Skan  int ct = count_registered_pragmas (pfile->pragmas);
1224169695Skan  char **result = XNEWVEC (char *, ct);
1225169695Skan  (void) save_registered_pragmas (pfile->pragmas, result);
1226169695Skan  return result;
1227169695Skan}
1228169695Skan
1229169695Skan/* Restore from SD the names of the registered pragmas referenced by PE,
1230169695Skan   and return a pointer to the next unused name in SD.  */
1231169695Skan
1232169695Skanstatic char **
1233169695Skanrestore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1234169695Skan			    char **sd)
1235169695Skan{
1236169695Skan  for (; pe != NULL; pe = pe->next)
1237169695Skan    {
1238169695Skan      if (pe->is_nspace)
1239169695Skan	sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1240169695Skan      pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1241169695Skan      free (*sd);
1242169695Skan      sd++;
1243169695Skan    }
1244169695Skan  return sd;
1245169695Skan}
1246169695Skan
1247169695Skan/* Restore the names of the registered pragmas from SAVED.  */
1248169695Skan
1249169695Skanvoid
1250169695Skan_cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1251169695Skan{
1252169695Skan  (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1253169695Skan  free (saved);
1254169695Skan}
1255169695Skan
1256169695Skan/* Pragmata handling.  We handle some, and pass the rest on to the
1257169695Skan   front end.  C99 defines three pragmas and says that no macro
1258169695Skan   expansion is to be performed on them; whether or not macro
1259169695Skan   expansion happens for other pragmas is implementation defined.
1260169695Skan   This implementation allows for a mix of both, since GCC did not
1261169695Skan   traditionally macro expand its (few) pragmas, whereas OpenMP
1262169695Skan   specifies that macro expansion should happen.  */
1263169695Skanstatic void
1264169695Skando_pragma (cpp_reader *pfile)
1265169695Skan{
1266169695Skan  const struct pragma_entry *p = NULL;
1267169695Skan  const cpp_token *token, *pragma_token = pfile->cur_token;
1268169695Skan  cpp_token ns_token;
1269169695Skan  unsigned int count = 1;
1270169695Skan
1271169695Skan  pfile->state.prevent_expansion++;
1272169695Skan
1273169695Skan  token = cpp_get_token (pfile);
1274169695Skan  ns_token = *token;
1275169695Skan  if (token->type == CPP_NAME)
1276169695Skan    {
1277169695Skan      p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1278169695Skan      if (p && p->is_nspace)
1279169695Skan	{
1280169695Skan	  bool allow_name_expansion = p->allow_expansion;
1281169695Skan	  if (allow_name_expansion)
1282169695Skan	    pfile->state.prevent_expansion--;
1283169695Skan	  token = cpp_get_token (pfile);
1284169695Skan	  if (token->type == CPP_NAME)
1285169695Skan	    p = lookup_pragma_entry (p->u.space, token->val.node);
1286169695Skan	  else
1287169695Skan	    p = NULL;
1288169695Skan	  if (allow_name_expansion)
1289169695Skan	    pfile->state.prevent_expansion++;
1290169695Skan	  count = 2;
1291169695Skan	}
1292169695Skan    }
1293169695Skan
1294169695Skan  if (p)
1295169695Skan    {
1296169695Skan      if (p->is_deferred)
1297169695Skan	{
1298169695Skan	  pfile->directive_result.src_loc = pragma_token->src_loc;
1299169695Skan	  pfile->directive_result.type = CPP_PRAGMA;
1300169695Skan	  pfile->directive_result.flags = pragma_token->flags;
1301169695Skan	  pfile->directive_result.val.pragma = p->u.ident;
1302169695Skan	  pfile->state.in_deferred_pragma = true;
1303169695Skan	  pfile->state.pragma_allow_expansion = p->allow_expansion;
1304169695Skan	  if (!p->allow_expansion)
1305169695Skan	    pfile->state.prevent_expansion++;
1306169695Skan	}
1307169695Skan      else
1308169695Skan	{
1309169695Skan	  /* Since the handler below doesn't get the line number, that
1310169695Skan	     it might need for diagnostics, make sure it has the right
1311169695Skan	     numbers in place.  */
1312169695Skan	  if (pfile->cb.line_change)
1313169695Skan	    (*pfile->cb.line_change) (pfile, pragma_token, false);
1314169695Skan	  if (p->allow_expansion)
1315169695Skan	    pfile->state.prevent_expansion--;
1316169695Skan	  (*p->u.handler) (pfile);
1317169695Skan	  if (p->allow_expansion)
1318169695Skan	    pfile->state.prevent_expansion++;
1319169695Skan	}
1320169695Skan    }
1321169695Skan  else if (pfile->cb.def_pragma)
1322169695Skan    {
1323169695Skan      if (count == 1 || pfile->context->prev == NULL)
1324169695Skan	_cpp_backup_tokens (pfile, count);
1325169695Skan      else
1326169695Skan	{
1327169695Skan	  /* Invalid name comes from macro expansion, _cpp_backup_tokens
1328169695Skan	     won't allow backing 2 tokens.  */
1329169695Skan	  /* ??? The token buffer is leaked.  Perhaps if def_pragma hook
1330169695Skan	     reads both tokens, we could perhaps free it, but if it doesn't,
1331169695Skan	     we don't know the exact lifespan.  */
1332169695Skan	  cpp_token *toks = XNEWVEC (cpp_token, 2);
1333169695Skan	  toks[0] = ns_token;
1334169695Skan	  toks[0].flags |= NO_EXPAND;
1335169695Skan	  toks[1] = *token;
1336169695Skan	  toks[1].flags |= NO_EXPAND;
1337169695Skan	  _cpp_push_token_context (pfile, NULL, toks, 2);
1338169695Skan	}
1339169695Skan      pfile->cb.def_pragma (pfile, pfile->directive_line);
1340169695Skan    }
1341169695Skan
1342169695Skan  pfile->state.prevent_expansion--;
1343169695Skan}
1344169695Skan
1345169695Skan/* Handle #pragma once.  */
1346169695Skanstatic void
1347169695Skando_pragma_once (cpp_reader *pfile)
1348169695Skan{
1349169695Skan  if (pfile->buffer->prev == NULL)
1350169695Skan    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1351169695Skan
1352169695Skan  check_eol (pfile);
1353169695Skan  _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1354169695Skan}
1355169695Skan
1356169695Skan/* Handle #pragma GCC poison, to poison one or more identifiers so
1357169695Skan   that the lexer produces a hard error for each subsequent usage.  */
1358169695Skanstatic void
1359169695Skando_pragma_poison (cpp_reader *pfile)
1360169695Skan{
1361169695Skan  const cpp_token *tok;
1362169695Skan  cpp_hashnode *hp;
1363169695Skan
1364169695Skan  pfile->state.poisoned_ok = 1;
1365169695Skan  for (;;)
1366169695Skan    {
1367169695Skan      tok = _cpp_lex_token (pfile);
1368169695Skan      if (tok->type == CPP_EOF)
1369169695Skan	break;
1370169695Skan      if (tok->type != CPP_NAME)
1371169695Skan	{
1372169695Skan	  cpp_error (pfile, CPP_DL_ERROR,
1373169695Skan		     "invalid #pragma GCC poison directive");
1374169695Skan	  break;
1375169695Skan	}
1376169695Skan
1377169695Skan      hp = tok->val.node;
1378169695Skan      if (hp->flags & NODE_POISONED)
1379169695Skan	continue;
1380169695Skan
1381169695Skan      if (hp->type == NT_MACRO)
1382169695Skan	cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1383169695Skan		   NODE_NAME (hp));
1384169695Skan      _cpp_free_definition (hp);
1385169695Skan      hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1386169695Skan    }
1387169695Skan  pfile->state.poisoned_ok = 0;
1388169695Skan}
1389169695Skan
1390169695Skan/* Mark the current header as a system header.  This will suppress
1391169695Skan   some categories of warnings (notably those from -pedantic).  It is
1392169695Skan   intended for use in system libraries that cannot be implemented in
1393169695Skan   conforming C, but cannot be certain that their headers appear in a
1394169695Skan   system include directory.  To prevent abuse, it is rejected in the
1395169695Skan   primary source file.  */
1396169695Skanstatic void
1397169695Skando_pragma_system_header (cpp_reader *pfile)
1398169695Skan{
1399169695Skan  cpp_buffer *buffer = pfile->buffer;
1400169695Skan
1401169695Skan  if (buffer->prev == 0)
1402169695Skan    cpp_error (pfile, CPP_DL_WARNING,
1403169695Skan	       "#pragma system_header ignored outside include file");
1404169695Skan  else
1405169695Skan    {
1406169695Skan      check_eol (pfile);
1407169695Skan      skip_rest_of_line (pfile);
1408169695Skan      cpp_make_system_header (pfile, 1, 0);
1409169695Skan    }
1410169695Skan}
1411169695Skan
1412169695Skan/* Check the modified date of the current include file against a specified
1413169695Skan   file. Issue a diagnostic, if the specified file is newer. We use this to
1414169695Skan   determine if a fixed header should be refixed.  */
1415169695Skanstatic void
1416169695Skando_pragma_dependency (cpp_reader *pfile)
1417169695Skan{
1418169695Skan  const char *fname;
1419169695Skan  int angle_brackets, ordering;
1420169695Skan
1421169695Skan  fname = parse_include (pfile, &angle_brackets, NULL);
1422169695Skan  if (!fname)
1423169695Skan    return;
1424169695Skan
1425169695Skan  ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1426169695Skan  if (ordering < 0)
1427169695Skan    cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1428169695Skan  else if (ordering > 0)
1429169695Skan    {
1430169695Skan      cpp_error (pfile, CPP_DL_WARNING,
1431169695Skan		 "current file is older than %s", fname);
1432169695Skan      if (cpp_get_token (pfile)->type != CPP_EOF)
1433169695Skan	{
1434169695Skan	  _cpp_backup_tokens (pfile, 1);
1435169695Skan	  do_diagnostic (pfile, CPP_DL_WARNING, 0);
1436169695Skan	}
1437169695Skan    }
1438169695Skan
1439169695Skan  free ((void *) fname);
1440169695Skan}
1441169695Skan
1442169695Skan/* Get a token but skip padding.  */
1443169695Skanstatic const cpp_token *
1444169695Skanget_token_no_padding (cpp_reader *pfile)
1445169695Skan{
1446169695Skan  for (;;)
1447169695Skan    {
1448169695Skan      const cpp_token *result = cpp_get_token (pfile);
1449169695Skan      if (result->type != CPP_PADDING)
1450169695Skan	return result;
1451169695Skan    }
1452169695Skan}
1453169695Skan
1454169695Skan/* Check syntax is "(string-literal)".  Returns the string on success,
1455169695Skan   or NULL on failure.  */
1456169695Skanstatic const cpp_token *
1457169695Skanget__Pragma_string (cpp_reader *pfile)
1458169695Skan{
1459169695Skan  const cpp_token *string;
1460169695Skan
1461169695Skan  if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1462169695Skan    return NULL;
1463169695Skan
1464169695Skan  string = get_token_no_padding (pfile);
1465169695Skan  if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1466169695Skan    return NULL;
1467169695Skan
1468169695Skan  if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1469169695Skan    return NULL;
1470169695Skan
1471169695Skan  return string;
1472169695Skan}
1473169695Skan
1474169695Skan/* Destringize IN into a temporary buffer, by removing the first \ of
1475169695Skan   \" and \\ sequences, and process the result as a #pragma directive.  */
1476169695Skanstatic void
1477169695Skandestringize_and_run (cpp_reader *pfile, const cpp_string *in)
1478169695Skan{
1479169695Skan  const unsigned char *src, *limit;
1480169695Skan  char *dest, *result;
1481169695Skan  cpp_context *saved_context;
1482169695Skan  cpp_token *saved_cur_token;
1483169695Skan  tokenrun *saved_cur_run;
1484169695Skan  cpp_token *toks;
1485169695Skan  int count;
1486169695Skan
1487169695Skan  dest = result = (char *) alloca (in->len - 1);
1488169695Skan  src = in->text + 1 + (in->text[0] == 'L');
1489169695Skan  limit = in->text + in->len - 1;
1490169695Skan  while (src < limit)
1491169695Skan    {
1492169695Skan      /* We know there is a character following the backslash.  */
1493169695Skan      if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1494169695Skan	src++;
1495169695Skan      *dest++ = *src++;
1496169695Skan    }
1497169695Skan  *dest = '\n';
1498169695Skan
1499169695Skan  /* Ugh; an awful kludge.  We are really not set up to be lexing
1500169695Skan     tokens when in the middle of a macro expansion.  Use a new
1501169695Skan     context to force cpp_get_token to lex, and so skip_rest_of_line
1502169695Skan     doesn't go beyond the end of the text.  Also, remember the
1503169695Skan     current lexing position so we can return to it later.
1504169695Skan
1505169695Skan     Something like line-at-a-time lexing should remove the need for
1506169695Skan     this.  */
1507169695Skan  saved_context = pfile->context;
1508169695Skan  saved_cur_token = pfile->cur_token;
1509169695Skan  saved_cur_run = pfile->cur_run;
1510169695Skan
1511169695Skan  pfile->context = XNEW (cpp_context);
1512169695Skan  pfile->context->macro = 0;
1513169695Skan  pfile->context->prev = 0;
1514169695Skan  pfile->context->next = 0;
1515169695Skan
1516169695Skan  /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1517169695Skan     until we've read all of the tokens that we want.  */
1518169695Skan  cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1519169695Skan		   /* from_stage3 */ true);
1520169695Skan  /* ??? Antique Disgusting Hack.  What does this do?  */
1521169695Skan  if (pfile->buffer->prev)
1522169695Skan    pfile->buffer->file = pfile->buffer->prev->file;
1523169695Skan
1524169695Skan  start_directive (pfile);
1525169695Skan  _cpp_clean_line (pfile);
1526169695Skan  do_pragma (pfile);
1527169695Skan  end_directive (pfile, 1);
1528169695Skan
1529169695Skan  /* We always insert at least one token, the directive result.  It'll
1530169695Skan     either be a CPP_PADDING or a CPP_PRAGMA.  In the later case, we
1531169695Skan     need to insert *all* of the tokens, including the CPP_PRAGMA_EOL.  */
1532169695Skan
1533169695Skan  /* If we're not handling the pragma internally, read all of the tokens from
1534169695Skan     the string buffer now, while the string buffer is still installed.  */
1535169695Skan  /* ??? Note that the token buffer allocated here is leaked.  It's not clear
1536169695Skan     to me what the true lifespan of the tokens are.  It would appear that
1537169695Skan     the lifespan is the entire parse of the main input stream, in which case
1538169695Skan     this may not be wrong.  */
1539169695Skan  if (pfile->directive_result.type == CPP_PRAGMA)
1540169695Skan    {
1541169695Skan      int maxcount;
1542169695Skan
1543169695Skan      count = 1;
1544169695Skan      maxcount = 50;
1545169695Skan      toks = XNEWVEC (cpp_token, maxcount);
1546169695Skan      toks[0] = pfile->directive_result;
1547169695Skan
1548169695Skan      do
1549169695Skan	{
1550169695Skan	  if (count == maxcount)
1551169695Skan	    {
1552169695Skan	      maxcount = maxcount * 3 / 2;
1553169695Skan	      toks = XRESIZEVEC (cpp_token, toks, maxcount);
1554169695Skan	    }
1555169695Skan	  toks[count] = *cpp_get_token (pfile);
1556169695Skan	  /* Macros have been already expanded by cpp_get_token
1557169695Skan	     if the pragma allowed expansion.  */
1558169695Skan	  toks[count++].flags |= NO_EXPAND;
1559169695Skan	}
1560169695Skan      while (toks[count-1].type != CPP_PRAGMA_EOL);
1561169695Skan    }
1562169695Skan  else
1563169695Skan    {
1564169695Skan      count = 1;
1565169695Skan      toks = XNEW (cpp_token);
1566169695Skan      toks[0] = pfile->directive_result;
1567169695Skan
1568169695Skan      /* If we handled the entire pragma internally, make sure we get the
1569169695Skan	 line number correct for the next token.  */
1570169695Skan      if (pfile->cb.line_change)
1571169695Skan	pfile->cb.line_change (pfile, pfile->cur_token, false);
1572169695Skan    }
1573169695Skan
1574169695Skan  /* Finish inlining run_directive.  */
1575169695Skan  pfile->buffer->file = NULL;
1576169695Skan  _cpp_pop_buffer (pfile);
1577169695Skan
1578169695Skan  /* Reset the old macro state before ...  */
1579169695Skan  XDELETE (pfile->context);
1580169695Skan  pfile->context = saved_context;
1581169695Skan  pfile->cur_token = saved_cur_token;
1582169695Skan  pfile->cur_run = saved_cur_run;
1583169695Skan
1584169695Skan  /* ... inserting the new tokens we collected.  */
1585169695Skan  _cpp_push_token_context (pfile, NULL, toks, count);
1586169695Skan}
1587169695Skan
1588169695Skan/* Handle the _Pragma operator.  */
1589169695Skanvoid
1590169695Skan_cpp_do__Pragma (cpp_reader *pfile)
1591169695Skan{
1592169695Skan  const cpp_token *string = get__Pragma_string (pfile);
1593169695Skan  pfile->directive_result.type = CPP_PADDING;
1594169695Skan
1595169695Skan  if (string)
1596169695Skan    destringize_and_run (pfile, &string->val.str);
1597169695Skan  else
1598169695Skan    cpp_error (pfile, CPP_DL_ERROR,
1599169695Skan	       "_Pragma takes a parenthesized string literal");
1600169695Skan}
1601169695Skan
1602169695Skan/* Handle #ifdef.  */
1603169695Skanstatic void
1604169695Skando_ifdef (cpp_reader *pfile)
1605169695Skan{
1606169695Skan  int skip = 1;
1607169695Skan
1608169695Skan  if (! pfile->state.skipping)
1609169695Skan    {
1610169695Skan      const cpp_hashnode *node = lex_macro_node (pfile);
1611169695Skan
1612169695Skan      if (node)
1613169695Skan	{
1614169695Skan	  skip = node->type != NT_MACRO;
1615169695Skan	  _cpp_mark_macro_used (node);
1616169695Skan	  check_eol (pfile);
1617169695Skan	}
1618169695Skan    }
1619169695Skan
1620169695Skan  push_conditional (pfile, skip, T_IFDEF, 0);
1621169695Skan}
1622169695Skan
1623169695Skan/* Handle #ifndef.  */
1624169695Skanstatic void
1625169695Skando_ifndef (cpp_reader *pfile)
1626169695Skan{
1627169695Skan  int skip = 1;
1628169695Skan  const cpp_hashnode *node = 0;
1629169695Skan
1630169695Skan  if (! pfile->state.skipping)
1631169695Skan    {
1632169695Skan      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_IFNDEF, node);
1643169695Skan}
1644169695Skan
1645169695Skan/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1646169695Skan   pfile->mi_ind_cmacro so we can handle multiple-include
1647169695Skan   optimizations.  If macro expansion occurs in the expression, we
1648169695Skan   cannot treat it as a controlling conditional, since the expansion
1649169695Skan   could change in the future.  That is handled by cpp_get_token.  */
1650169695Skanstatic void
1651169695Skando_if (cpp_reader *pfile)
1652169695Skan{
1653169695Skan  int skip = 1;
1654169695Skan
1655169695Skan  if (! pfile->state.skipping)
1656169695Skan    skip = _cpp_parse_expr (pfile) == false;
1657169695Skan
1658169695Skan  push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1659169695Skan}
1660169695Skan
1661169695Skan/* Flip skipping state if appropriate and continue without changing
1662169695Skan   if_stack; this is so that the error message for missing #endif's
1663169695Skan   etc. will point to the original #if.  */
1664169695Skanstatic void
1665169695Skando_else (cpp_reader *pfile)
1666169695Skan{
1667169695Skan  cpp_buffer *buffer = pfile->buffer;
1668169695Skan  struct if_stack *ifs = buffer->if_stack;
1669169695Skan
1670169695Skan  if (ifs == NULL)
1671169695Skan    cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1672169695Skan  else
1673169695Skan    {
1674169695Skan      if (ifs->type == T_ELSE)
1675169695Skan	{
1676169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1677169695Skan	  cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1678169695Skan			       "the conditional began here");
1679169695Skan	}
1680169695Skan      ifs->type = T_ELSE;
1681169695Skan
1682169695Skan      /* Skip any future (erroneous) #elses or #elifs.  */
1683169695Skan      pfile->state.skipping = ifs->skip_elses;
1684169695Skan      ifs->skip_elses = true;
1685169695Skan
1686169695Skan      /* Invalidate any controlling macro.  */
1687169695Skan      ifs->mi_cmacro = 0;
1688169695Skan
1689169695Skan      /* Only check EOL if was not originally skipping.  */
1690169695Skan      if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1691169695Skan	check_eol (pfile);
1692169695Skan    }
1693169695Skan}
1694169695Skan
1695169695Skan/* Handle a #elif directive by not changing if_stack either.  See the
1696169695Skan   comment above do_else.  */
1697169695Skanstatic void
1698169695Skando_elif (cpp_reader *pfile)
1699169695Skan{
1700169695Skan  cpp_buffer *buffer = pfile->buffer;
1701169695Skan  struct if_stack *ifs = buffer->if_stack;
1702169695Skan
1703169695Skan  if (ifs == NULL)
1704169695Skan    cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1705169695Skan  else
1706169695Skan    {
1707169695Skan      if (ifs->type == T_ELSE)
1708169695Skan	{
1709169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1710169695Skan	  cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1711169695Skan			       "the conditional began here");
1712169695Skan	}
1713169695Skan      ifs->type = T_ELIF;
1714169695Skan
1715169695Skan      /* Only evaluate this if we aren't skipping elses.  During
1716169695Skan	 evaluation, set skipping to false to get lexer warnings.  */
1717169695Skan      if (ifs->skip_elses)
1718169695Skan	pfile->state.skipping = 1;
1719169695Skan      else
1720169695Skan	{
1721169695Skan	  pfile->state.skipping = 0;
1722169695Skan	  pfile->state.skipping = ! _cpp_parse_expr (pfile);
1723169695Skan	  ifs->skip_elses = ! pfile->state.skipping;
1724169695Skan	}
1725169695Skan
1726169695Skan      /* Invalidate any controlling macro.  */
1727169695Skan      ifs->mi_cmacro = 0;
1728169695Skan    }
1729169695Skan}
1730169695Skan
1731169695Skan/* #endif pops the if stack and resets pfile->state.skipping.  */
1732169695Skanstatic void
1733169695Skando_endif (cpp_reader *pfile)
1734169695Skan{
1735169695Skan  cpp_buffer *buffer = pfile->buffer;
1736169695Skan  struct if_stack *ifs = buffer->if_stack;
1737169695Skan
1738169695Skan  if (ifs == NULL)
1739169695Skan    cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1740169695Skan  else
1741169695Skan    {
1742169695Skan      /* Only check EOL if was not originally skipping.  */
1743169695Skan      if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1744169695Skan	check_eol (pfile);
1745169695Skan
1746169695Skan      /* If potential control macro, we go back outside again.  */
1747169695Skan      if (ifs->next == 0 && ifs->mi_cmacro)
1748169695Skan	{
1749169695Skan	  pfile->mi_valid = true;
1750169695Skan	  pfile->mi_cmacro = ifs->mi_cmacro;
1751169695Skan	}
1752169695Skan
1753169695Skan      buffer->if_stack = ifs->next;
1754169695Skan      pfile->state.skipping = ifs->was_skipping;
1755169695Skan      obstack_free (&pfile->buffer_ob, ifs);
1756169695Skan    }
1757169695Skan}
1758169695Skan
1759169695Skan/* Push an if_stack entry for a preprocessor conditional, and set
1760169695Skan   pfile->state.skipping to SKIP.  If TYPE indicates the conditional
1761169695Skan   is #if or #ifndef, CMACRO is a potentially controlling macro, and
1762169695Skan   we need to check here that we are at the top of the file.  */
1763169695Skanstatic void
1764169695Skanpush_conditional (cpp_reader *pfile, int skip, int type,
1765169695Skan		  const cpp_hashnode *cmacro)
1766169695Skan{
1767169695Skan  struct if_stack *ifs;
1768169695Skan  cpp_buffer *buffer = pfile->buffer;
1769169695Skan
1770169695Skan  ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
1771169695Skan  ifs->line = pfile->directive_line;
1772169695Skan  ifs->next = buffer->if_stack;
1773169695Skan  ifs->skip_elses = pfile->state.skipping || !skip;
1774169695Skan  ifs->was_skipping = pfile->state.skipping;
1775169695Skan  ifs->type = type;
1776169695Skan  /* This condition is effectively a test for top-of-file.  */
1777169695Skan  if (pfile->mi_valid && pfile->mi_cmacro == 0)
1778169695Skan    ifs->mi_cmacro = cmacro;
1779169695Skan  else
1780169695Skan    ifs->mi_cmacro = 0;
1781169695Skan
1782169695Skan  pfile->state.skipping = skip;
1783169695Skan  buffer->if_stack = ifs;
1784169695Skan}
1785169695Skan
1786169695Skan/* Read the tokens of the answer into the macro pool, in a directive
1787169695Skan   of type TYPE.  Only commit the memory if we intend it as permanent
1788169695Skan   storage, i.e. the #assert case.  Returns 0 on success, and sets
1789169695Skan   ANSWERP to point to the answer.  */
1790169695Skanstatic int
1791169695Skanparse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1792169695Skan{
1793169695Skan  const cpp_token *paren;
1794169695Skan  struct answer *answer;
1795169695Skan  unsigned int acount;
1796169695Skan
1797169695Skan  /* In a conditional, it is legal to not have an open paren.  We
1798169695Skan     should save the following token in this case.  */
1799169695Skan  paren = cpp_get_token (pfile);
1800169695Skan
1801169695Skan  /* If not a paren, see if we're OK.  */
1802169695Skan  if (paren->type != CPP_OPEN_PAREN)
1803169695Skan    {
1804169695Skan      /* In a conditional no answer is a test for any answer.  It
1805169695Skan         could be followed by any token.  */
1806169695Skan      if (type == T_IF)
1807169695Skan	{
1808169695Skan	  _cpp_backup_tokens (pfile, 1);
1809169695Skan	  return 0;
1810169695Skan	}
1811169695Skan
1812169695Skan      /* #unassert with no answer is valid - it removes all answers.  */
1813169695Skan      if (type == T_UNASSERT && paren->type == CPP_EOF)
1814169695Skan	return 0;
1815169695Skan
1816169695Skan      cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
1817169695Skan      return 1;
1818169695Skan    }
1819169695Skan
1820169695Skan  for (acount = 0;; acount++)
1821169695Skan    {
1822169695Skan      size_t room_needed;
1823169695Skan      const cpp_token *token = cpp_get_token (pfile);
1824169695Skan      cpp_token *dest;
1825169695Skan
1826169695Skan      if (token->type == CPP_CLOSE_PAREN)
1827169695Skan	break;
1828169695Skan
1829169695Skan      if (token->type == CPP_EOF)
1830169695Skan	{
1831169695Skan	  cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
1832169695Skan	  return 1;
1833169695Skan	}
1834169695Skan
1835169695Skan      /* struct answer includes the space for one token.  */
1836169695Skan      room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1837169695Skan
1838169695Skan      if (BUFF_ROOM (pfile->a_buff) < room_needed)
1839169695Skan	_cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1840169695Skan
1841169695Skan      dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1842169695Skan      *dest = *token;
1843169695Skan
1844169695Skan      /* Drop whitespace at start, for answer equivalence purposes.  */
1845169695Skan      if (acount == 0)
1846169695Skan	dest->flags &= ~PREV_WHITE;
1847169695Skan    }
1848169695Skan
1849169695Skan  if (acount == 0)
1850169695Skan    {
1851169695Skan      cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
1852169695Skan      return 1;
1853169695Skan    }
1854169695Skan
1855169695Skan  answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1856169695Skan  answer->count = acount;
1857169695Skan  answer->next = NULL;
1858169695Skan  *answerp = answer;
1859169695Skan
1860169695Skan  return 0;
1861169695Skan}
1862169695Skan
1863169695Skan/* Parses an assertion directive of type TYPE, returning a pointer to
1864169695Skan   the hash node of the predicate, or 0 on error.  If an answer was
1865169695Skan   supplied, it is placed in ANSWERP, otherwise it is set to 0.  */
1866169695Skanstatic cpp_hashnode *
1867169695Skanparse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1868169695Skan{
1869169695Skan  cpp_hashnode *result = 0;
1870169695Skan  const cpp_token *predicate;
1871169695Skan
1872169695Skan  /* We don't expand predicates or answers.  */
1873169695Skan  pfile->state.prevent_expansion++;
1874169695Skan
1875169695Skan  *answerp = 0;
1876169695Skan  predicate = cpp_get_token (pfile);
1877169695Skan  if (predicate->type == CPP_EOF)
1878169695Skan    cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
1879169695Skan  else if (predicate->type != CPP_NAME)
1880169695Skan    cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
1881169695Skan  else if (parse_answer (pfile, answerp, type) == 0)
1882169695Skan    {
1883169695Skan      unsigned int len = NODE_LEN (predicate->val.node);
1884169695Skan      unsigned char *sym = (unsigned char *) alloca (len + 1);
1885169695Skan
1886169695Skan      /* Prefix '#' to get it out of macro namespace.  */
1887169695Skan      sym[0] = '#';
1888169695Skan      memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1889169695Skan      result = cpp_lookup (pfile, sym, len + 1);
1890169695Skan    }
1891169695Skan
1892169695Skan  pfile->state.prevent_expansion--;
1893169695Skan  return result;
1894169695Skan}
1895169695Skan
1896169695Skan/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1897169695Skan   or a pointer to NULL if the answer is not in the chain.  */
1898169695Skanstatic struct answer **
1899169695Skanfind_answer (cpp_hashnode *node, const struct answer *candidate)
1900169695Skan{
1901169695Skan  unsigned int i;
1902169695Skan  struct answer **result;
1903169695Skan
1904169695Skan  for (result = &node->value.answers; *result; result = &(*result)->next)
1905169695Skan    {
1906169695Skan      struct answer *answer = *result;
1907169695Skan
1908169695Skan      if (answer->count == candidate->count)
1909169695Skan	{
1910169695Skan	  for (i = 0; i < answer->count; i++)
1911169695Skan	    if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1912169695Skan	      break;
1913169695Skan
1914169695Skan	  if (i == answer->count)
1915169695Skan	    break;
1916169695Skan	}
1917169695Skan    }
1918169695Skan
1919169695Skan  return result;
1920169695Skan}
1921169695Skan
1922169695Skan/* Test an assertion within a preprocessor conditional.  Returns
1923169695Skan   nonzero on failure, zero on success.  On success, the result of
1924169695Skan   the test is written into VALUE, otherwise the value 0.  */
1925169695Skanint
1926169695Skan_cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
1927169695Skan{
1928169695Skan  struct answer *answer;
1929169695Skan  cpp_hashnode *node;
1930169695Skan
1931169695Skan  node = parse_assertion (pfile, &answer, T_IF);
1932169695Skan
1933169695Skan  /* For recovery, an erroneous assertion expression is handled as a
1934169695Skan     failing assertion.  */
1935169695Skan  *value = 0;
1936169695Skan
1937169695Skan  if (node)
1938169695Skan    *value = (node->type == NT_ASSERTION &&
1939169695Skan	      (answer == 0 || *find_answer (node, answer) != 0));
1940169695Skan  else if (pfile->cur_token[-1].type == CPP_EOF)
1941169695Skan    _cpp_backup_tokens (pfile, 1);
1942169695Skan
1943169695Skan  /* We don't commit the memory for the answer - it's temporary only.  */
1944169695Skan  return node == 0;
1945169695Skan}
1946169695Skan
1947169695Skan/* Handle #assert.  */
1948169695Skanstatic void
1949169695Skando_assert (cpp_reader *pfile)
1950169695Skan{
1951169695Skan  struct answer *new_answer;
1952169695Skan  cpp_hashnode *node;
1953169695Skan
1954169695Skan  node = parse_assertion (pfile, &new_answer, T_ASSERT);
1955169695Skan  if (node)
1956169695Skan    {
1957169695Skan      size_t answer_size;
1958169695Skan
1959169695Skan      /* Place the new answer in the answer list.  First check there
1960169695Skan         is not a duplicate.  */
1961169695Skan      new_answer->next = 0;
1962169695Skan      if (node->type == NT_ASSERTION)
1963169695Skan	{
1964169695Skan	  if (*find_answer (node, new_answer))
1965169695Skan	    {
1966169695Skan	      cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
1967169695Skan			 NODE_NAME (node) + 1);
1968169695Skan	      return;
1969169695Skan	    }
1970169695Skan	  new_answer->next = node->value.answers;
1971169695Skan	}
1972169695Skan
1973169695Skan      answer_size = sizeof (struct answer) + ((new_answer->count - 1)
1974169695Skan					      * sizeof (cpp_token));
1975169695Skan      /* Commit or allocate storage for the object.  */
1976169695Skan      if (pfile->hash_table->alloc_subobject)
1977169695Skan	{
1978169695Skan	  struct answer *temp_answer = new_answer;
1979169695Skan	  new_answer = (struct answer *) pfile->hash_table->alloc_subobject
1980169695Skan            (answer_size);
1981169695Skan	  memcpy (new_answer, temp_answer, answer_size);
1982169695Skan	}
1983169695Skan      else
1984169695Skan	BUFF_FRONT (pfile->a_buff) += answer_size;
1985169695Skan
1986169695Skan      node->type = NT_ASSERTION;
1987169695Skan      node->value.answers = new_answer;
1988169695Skan      check_eol (pfile);
1989169695Skan    }
1990169695Skan}
1991169695Skan
1992169695Skan/* Handle #unassert.  */
1993169695Skanstatic void
1994169695Skando_unassert (cpp_reader *pfile)
1995169695Skan{
1996169695Skan  cpp_hashnode *node;
1997169695Skan  struct answer *answer;
1998169695Skan
1999169695Skan  node = parse_assertion (pfile, &answer, T_UNASSERT);
2000169695Skan  /* It isn't an error to #unassert something that isn't asserted.  */
2001169695Skan  if (node && node->type == NT_ASSERTION)
2002169695Skan    {
2003169695Skan      if (answer)
2004169695Skan	{
2005169695Skan	  struct answer **p = find_answer (node, answer), *temp;
2006169695Skan
2007169695Skan	  /* Remove the answer from the list.  */
2008169695Skan	  temp = *p;
2009169695Skan	  if (temp)
2010169695Skan	    *p = temp->next;
2011169695Skan
2012169695Skan	  /* Did we free the last answer?  */
2013169695Skan	  if (node->value.answers == 0)
2014169695Skan	    node->type = NT_VOID;
2015169695Skan
2016169695Skan	  check_eol (pfile);
2017169695Skan	}
2018169695Skan      else
2019169695Skan	_cpp_free_definition (node);
2020169695Skan    }
2021169695Skan
2022169695Skan  /* We don't commit the memory for the answer - it's temporary only.  */
2023169695Skan}
2024169695Skan
2025169695Skan/* These are for -D, -U, -A.  */
2026169695Skan
2027169695Skan/* Process the string STR as if it appeared as the body of a #define.
2028169695Skan   If STR is just an identifier, define it with value 1.
2029169695Skan   If STR has anything after the identifier, then it should
2030169695Skan   be identifier=definition.  */
2031169695Skanvoid
2032169695Skancpp_define (cpp_reader *pfile, const char *str)
2033169695Skan{
2034169695Skan  char *buf, *p;
2035169695Skan  size_t count;
2036169695Skan
2037169695Skan  /* Copy the entire option so we can modify it.
2038169695Skan     Change the first "=" in the string to a space.  If there is none,
2039169695Skan     tack " 1" on the end.  */
2040169695Skan
2041169695Skan  count = strlen (str);
2042169695Skan  buf = (char *) alloca (count + 3);
2043169695Skan  memcpy (buf, str, count);
2044169695Skan
2045169695Skan  p = strchr (str, '=');
2046169695Skan  if (p)
2047169695Skan    buf[p - str] = ' ';
2048169695Skan  else
2049169695Skan    {
2050169695Skan      buf[count++] = ' ';
2051169695Skan      buf[count++] = '1';
2052169695Skan    }
2053169695Skan  buf[count] = '\n';
2054169695Skan
2055169695Skan  run_directive (pfile, T_DEFINE, buf, count);
2056169695Skan}
2057169695Skan
2058169695Skan/* Slight variant of the above for use by initialize_builtins.  */
2059169695Skanvoid
2060169695Skan_cpp_define_builtin (cpp_reader *pfile, const char *str)
2061169695Skan{
2062169695Skan  size_t len = strlen (str);
2063169695Skan  char *buf = (char *) alloca (len + 1);
2064169695Skan  memcpy (buf, str, len);
2065169695Skan  buf[len] = '\n';
2066169695Skan  run_directive (pfile, T_DEFINE, buf, len);
2067169695Skan}
2068169695Skan
2069169695Skan/* Process MACRO as if it appeared as the body of an #undef.  */
2070169695Skanvoid
2071169695Skancpp_undef (cpp_reader *pfile, const char *macro)
2072169695Skan{
2073169695Skan  size_t len = strlen (macro);
2074169695Skan  char *buf = (char *) alloca (len + 1);
2075169695Skan  memcpy (buf, macro, len);
2076169695Skan  buf[len] = '\n';
2077169695Skan  run_directive (pfile, T_UNDEF, buf, len);
2078169695Skan}
2079169695Skan
2080169695Skan/* Process the string STR as if it appeared as the body of a #assert.  */
2081169695Skanvoid
2082169695Skancpp_assert (cpp_reader *pfile, const char *str)
2083169695Skan{
2084169695Skan  handle_assertion (pfile, str, T_ASSERT);
2085169695Skan}
2086169695Skan
2087169695Skan/* Process STR as if it appeared as the body of an #unassert.  */
2088169695Skanvoid
2089169695Skancpp_unassert (cpp_reader *pfile, const char *str)
2090169695Skan{
2091169695Skan  handle_assertion (pfile, str, T_UNASSERT);
2092169695Skan}
2093169695Skan
2094169695Skan/* Common code for cpp_assert (-A) and cpp_unassert (-A-).  */
2095169695Skanstatic void
2096169695Skanhandle_assertion (cpp_reader *pfile, const char *str, int type)
2097169695Skan{
2098169695Skan  size_t count = strlen (str);
2099169695Skan  const char *p = strchr (str, '=');
2100169695Skan
2101169695Skan  /* Copy the entire option so we can modify it.  Change the first
2102169695Skan     "=" in the string to a '(', and tack a ')' on the end.  */
2103169695Skan  char *buf = (char *) alloca (count + 2);
2104169695Skan
2105169695Skan  memcpy (buf, str, count);
2106169695Skan  if (p)
2107169695Skan    {
2108169695Skan      buf[p - str] = '(';
2109169695Skan      buf[count++] = ')';
2110169695Skan    }
2111169695Skan  buf[count] = '\n';
2112169695Skan  str = buf;
2113169695Skan
2114169695Skan  run_directive (pfile, type, str, count);
2115169695Skan}
2116169695Skan
2117169695Skan/* The number of errors for a given reader.  */
2118169695Skanunsigned int
2119169695Skancpp_errors (cpp_reader *pfile)
2120169695Skan{
2121169695Skan  return pfile->errors;
2122169695Skan}
2123169695Skan
2124169695Skan/* The options structure.  */
2125169695Skancpp_options *
2126169695Skancpp_get_options (cpp_reader *pfile)
2127169695Skan{
2128169695Skan  return &pfile->opts;
2129169695Skan}
2130169695Skan
2131169695Skan/* The callbacks structure.  */
2132169695Skancpp_callbacks *
2133169695Skancpp_get_callbacks (cpp_reader *pfile)
2134169695Skan{
2135169695Skan  return &pfile->cb;
2136169695Skan}
2137169695Skan
2138169695Skan/* Copy the given callbacks structure to our own.  */
2139169695Skanvoid
2140169695Skancpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2141169695Skan{
2142169695Skan  pfile->cb = *cb;
2143169695Skan}
2144169695Skan
2145169695Skan/* The dependencies structure.  (Creates one if it hasn't already been.)  */
2146169695Skanstruct deps *
2147169695Skancpp_get_deps (cpp_reader *pfile)
2148169695Skan{
2149169695Skan  if (!pfile->deps)
2150169695Skan    pfile->deps = deps_init ();
2151169695Skan  return pfile->deps;
2152169695Skan}
2153169695Skan
2154169695Skan/* Push a new buffer on the buffer stack.  Returns the new buffer; it
2155169695Skan   doesn't fail.  It does not generate a file change call back; that
2156169695Skan   is the responsibility of the caller.  */
2157169695Skancpp_buffer *
2158169695Skancpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2159169695Skan		 int from_stage3)
2160169695Skan{
2161169695Skan  cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2162169695Skan
2163169695Skan  /* Clears, amongst other things, if_stack and mi_cmacro.  */
2164169695Skan  memset (new_buffer, 0, sizeof (cpp_buffer));
2165169695Skan
2166169695Skan  new_buffer->next_line = new_buffer->buf = buffer;
2167169695Skan  new_buffer->rlimit = buffer + len;
2168169695Skan  new_buffer->from_stage3 = from_stage3;
2169169695Skan  new_buffer->prev = pfile->buffer;
2170169695Skan  new_buffer->need_line = true;
2171169695Skan
2172169695Skan  pfile->buffer = new_buffer;
2173169695Skan
2174169695Skan  return new_buffer;
2175169695Skan}
2176169695Skan
2177169695Skan/* Pops a single buffer, with a file change call-back if appropriate.
2178169695Skan   Then pushes the next -include file, if any remain.  */
2179169695Skanvoid
2180169695Skan_cpp_pop_buffer (cpp_reader *pfile)
2181169695Skan{
2182169695Skan  cpp_buffer *buffer = pfile->buffer;
2183169695Skan  struct _cpp_file *inc = buffer->file;
2184169695Skan  struct if_stack *ifs;
2185169695Skan
2186169695Skan  /* Walk back up the conditional stack till we reach its level at
2187169695Skan     entry to this file, issuing error messages.  */
2188169695Skan  for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2189169695Skan    cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2190169695Skan			 "unterminated #%s", dtable[ifs->type].name);
2191169695Skan
2192169695Skan  /* In case of a missing #endif.  */
2193169695Skan  pfile->state.skipping = 0;
2194169695Skan
2195169695Skan  /* _cpp_do_file_change expects pfile->buffer to be the new one.  */
2196169695Skan  pfile->buffer = buffer->prev;
2197169695Skan
2198169695Skan  free (buffer->notes);
2199169695Skan
2200169695Skan  /* Free the buffer object now; we may want to push a new buffer
2201169695Skan     in _cpp_push_next_include_file.  */
2202169695Skan  obstack_free (&pfile->buffer_ob, buffer);
2203169695Skan
2204169695Skan  if (inc)
2205169695Skan    {
2206169695Skan      _cpp_pop_file_buffer (pfile, inc);
2207169695Skan
2208169695Skan      _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2209169695Skan    }
2210169695Skan}
2211169695Skan
2212169695Skan/* Enter all recognized directives in the hash table.  */
2213169695Skanvoid
2214169695Skan_cpp_init_directives (cpp_reader *pfile)
2215169695Skan{
2216169695Skan  unsigned int i;
2217169695Skan  cpp_hashnode *node;
2218169695Skan
2219169695Skan  for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2220169695Skan    {
2221169695Skan      node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2222169695Skan      node->is_directive = 1;
2223169695Skan      node->directive_index = i;
2224169695Skan    }
2225169695Skan}
2226