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