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