xref: /openbsd-src/gnu/gcc/libcpp/lex.c (revision dd6081ec6b253c97e7202ea58b8ccc493dc34c6c)
1*404b540aSrobert /* CPP Library - lexical analysis.
2*404b540aSrobert    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3*404b540aSrobert    Contributed by Per Bothner, 1994-95.
4*404b540aSrobert    Based on CCCP program by Paul Rubin, June 1986
5*404b540aSrobert    Adapted to ANSI C, Richard Stallman, Jan 1987
6*404b540aSrobert    Broken out to separate file, Zack Weinberg, Mar 2000
7*404b540aSrobert 
8*404b540aSrobert This program is free software; you can redistribute it and/or modify it
9*404b540aSrobert under the terms of the GNU General Public License as published by the
10*404b540aSrobert Free Software Foundation; either version 2, or (at your option) any
11*404b540aSrobert later version.
12*404b540aSrobert 
13*404b540aSrobert This program is distributed in the hope that it will be useful,
14*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
15*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*404b540aSrobert GNU General Public License for more details.
17*404b540aSrobert 
18*404b540aSrobert You should have received a copy of the GNU General Public License
19*404b540aSrobert along with this program; if not, write to the Free Software
20*404b540aSrobert Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
21*404b540aSrobert 
22*404b540aSrobert #include "config.h"
23*404b540aSrobert #include "system.h"
24*404b540aSrobert #include "cpplib.h"
25*404b540aSrobert #include "internal.h"
26*404b540aSrobert 
27*404b540aSrobert enum spell_type
28*404b540aSrobert {
29*404b540aSrobert   SPELL_OPERATOR = 0,
30*404b540aSrobert   SPELL_IDENT,
31*404b540aSrobert   SPELL_LITERAL,
32*404b540aSrobert   SPELL_NONE
33*404b540aSrobert };
34*404b540aSrobert 
35*404b540aSrobert struct token_spelling
36*404b540aSrobert {
37*404b540aSrobert   enum spell_type category;
38*404b540aSrobert   const unsigned char *name;
39*404b540aSrobert };
40*404b540aSrobert 
41*404b540aSrobert static const unsigned char *const digraph_spellings[] =
42*404b540aSrobert { U"%:", U"%:%:", U"<:", U":>", U"<%", U"%>" };
43*404b540aSrobert 
44*404b540aSrobert #define OP(e, s) { SPELL_OPERATOR, U s  },
45*404b540aSrobert #define TK(e, s) { SPELL_ ## s,    U #e },
46*404b540aSrobert static const struct token_spelling token_spellings[N_TTYPES] = { TTYPE_TABLE };
47*404b540aSrobert #undef OP
48*404b540aSrobert #undef TK
49*404b540aSrobert 
50*404b540aSrobert #define TOKEN_SPELL(token) (token_spellings[(token)->type].category)
51*404b540aSrobert #define TOKEN_NAME(token) (token_spellings[(token)->type].name)
52*404b540aSrobert 
53*404b540aSrobert static void add_line_note (cpp_buffer *, const uchar *, unsigned int);
54*404b540aSrobert static int skip_line_comment (cpp_reader *);
55*404b540aSrobert static void skip_whitespace (cpp_reader *, cppchar_t);
56*404b540aSrobert static void lex_string (cpp_reader *, cpp_token *, const uchar *);
57*404b540aSrobert static void save_comment (cpp_reader *, cpp_token *, const uchar *, cppchar_t);
58*404b540aSrobert static void create_literal (cpp_reader *, cpp_token *, const uchar *,
59*404b540aSrobert 			    unsigned int, enum cpp_ttype);
60*404b540aSrobert static bool warn_in_comment (cpp_reader *, _cpp_line_note *);
61*404b540aSrobert static int name_p (cpp_reader *, const cpp_string *);
62*404b540aSrobert static tokenrun *next_tokenrun (tokenrun *);
63*404b540aSrobert 
64*404b540aSrobert static _cpp_buff *new_buff (size_t);
65*404b540aSrobert 
66*404b540aSrobert 
67*404b540aSrobert /* Utility routine:
68*404b540aSrobert 
69*404b540aSrobert    Compares, the token TOKEN to the NUL-terminated string STRING.
70*404b540aSrobert    TOKEN must be a CPP_NAME.  Returns 1 for equal, 0 for unequal.  */
71*404b540aSrobert int
cpp_ideq(const cpp_token * token,const char * string)72*404b540aSrobert cpp_ideq (const cpp_token *token, const char *string)
73*404b540aSrobert {
74*404b540aSrobert   if (token->type != CPP_NAME)
75*404b540aSrobert     return 0;
76*404b540aSrobert 
77*404b540aSrobert   return !ustrcmp (NODE_NAME (token->val.node), (const uchar *) string);
78*404b540aSrobert }
79*404b540aSrobert 
80*404b540aSrobert /* Record a note TYPE at byte POS into the current cleaned logical
81*404b540aSrobert    line.  */
82*404b540aSrobert static void
add_line_note(cpp_buffer * buffer,const uchar * pos,unsigned int type)83*404b540aSrobert add_line_note (cpp_buffer *buffer, const uchar *pos, unsigned int type)
84*404b540aSrobert {
85*404b540aSrobert   if (buffer->notes_used == buffer->notes_cap)
86*404b540aSrobert     {
87*404b540aSrobert       buffer->notes_cap = buffer->notes_cap * 2 + 200;
88*404b540aSrobert       buffer->notes = XRESIZEVEC (_cpp_line_note, buffer->notes,
89*404b540aSrobert                                   buffer->notes_cap);
90*404b540aSrobert     }
91*404b540aSrobert 
92*404b540aSrobert   buffer->notes[buffer->notes_used].pos = pos;
93*404b540aSrobert   buffer->notes[buffer->notes_used].type = type;
94*404b540aSrobert   buffer->notes_used++;
95*404b540aSrobert }
96*404b540aSrobert 
97*404b540aSrobert /* Returns with a logical line that contains no escaped newlines or
98*404b540aSrobert    trigraphs.  This is a time-critical inner loop.  */
99*404b540aSrobert void
_cpp_clean_line(cpp_reader * pfile)100*404b540aSrobert _cpp_clean_line (cpp_reader *pfile)
101*404b540aSrobert {
102*404b540aSrobert   cpp_buffer *buffer;
103*404b540aSrobert   const uchar *s;
104*404b540aSrobert   uchar c, *d, *p;
105*404b540aSrobert 
106*404b540aSrobert   buffer = pfile->buffer;
107*404b540aSrobert   buffer->cur_note = buffer->notes_used = 0;
108*404b540aSrobert   buffer->cur = buffer->line_base = buffer->next_line;
109*404b540aSrobert   buffer->need_line = false;
110*404b540aSrobert   s = buffer->next_line - 1;
111*404b540aSrobert 
112*404b540aSrobert   if (!buffer->from_stage3)
113*404b540aSrobert     {
114*404b540aSrobert       /* Short circuit for the common case of an un-escaped line with
115*404b540aSrobert 	 no trigraphs.  The primary win here is by not writing any
116*404b540aSrobert 	 data back to memory until we have to.  */
117*404b540aSrobert       for (;;)
118*404b540aSrobert 	{
119*404b540aSrobert 	  c = *++s;
120*404b540aSrobert 	  if (c == '\n' || c == '\r')
121*404b540aSrobert 	    {
122*404b540aSrobert 	      d = (uchar *) s;
123*404b540aSrobert 
124*404b540aSrobert 	      if (s == buffer->rlimit)
125*404b540aSrobert 		goto done;
126*404b540aSrobert 
127*404b540aSrobert 	      /* DOS line ending? */
128*404b540aSrobert 	      if (c == '\r' && s[1] == '\n')
129*404b540aSrobert 		s++;
130*404b540aSrobert 
131*404b540aSrobert 	      if (s == buffer->rlimit)
132*404b540aSrobert 		goto done;
133*404b540aSrobert 
134*404b540aSrobert 	      /* check for escaped newline */
135*404b540aSrobert 	      p = d;
136*404b540aSrobert 	      while (p != buffer->next_line && is_nvspace (p[-1]))
137*404b540aSrobert 		p--;
138*404b540aSrobert 	      if (p == buffer->next_line || p[-1] != '\\')
139*404b540aSrobert 		goto done;
140*404b540aSrobert 
141*404b540aSrobert 	      /* Have an escaped newline; process it and proceed to
142*404b540aSrobert 		 the slow path.  */
143*404b540aSrobert 	      add_line_note (buffer, p - 1, p != d ? ' ' : '\\');
144*404b540aSrobert 	      d = p - 2;
145*404b540aSrobert 	      buffer->next_line = p - 1;
146*404b540aSrobert 	      break;
147*404b540aSrobert 	    }
148*404b540aSrobert 	  if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
149*404b540aSrobert 	    {
150*404b540aSrobert 	      /* Have a trigraph.  We may or may not have to convert
151*404b540aSrobert 		 it.  Add a line note regardless, for -Wtrigraphs.  */
152*404b540aSrobert 	      add_line_note (buffer, s, s[2]);
153*404b540aSrobert 	      if (CPP_OPTION (pfile, trigraphs))
154*404b540aSrobert 		{
155*404b540aSrobert 		  /* We do, and that means we have to switch to the
156*404b540aSrobert 		     slow path.  */
157*404b540aSrobert 		  d = (uchar *) s;
158*404b540aSrobert 		  *d = _cpp_trigraph_map[s[2]];
159*404b540aSrobert 		  s += 2;
160*404b540aSrobert 		  break;
161*404b540aSrobert 		}
162*404b540aSrobert 	    }
163*404b540aSrobert 	}
164*404b540aSrobert 
165*404b540aSrobert 
166*404b540aSrobert       for (;;)
167*404b540aSrobert 	{
168*404b540aSrobert 	  c = *++s;
169*404b540aSrobert 	  *++d = c;
170*404b540aSrobert 
171*404b540aSrobert 	  if (c == '\n' || c == '\r')
172*404b540aSrobert 	    {
173*404b540aSrobert 		  /* Handle DOS line endings.  */
174*404b540aSrobert 	      if (c == '\r' && s != buffer->rlimit && s[1] == '\n')
175*404b540aSrobert 		s++;
176*404b540aSrobert 	      if (s == buffer->rlimit)
177*404b540aSrobert 		break;
178*404b540aSrobert 
179*404b540aSrobert 	      /* Escaped?  */
180*404b540aSrobert 	      p = d;
181*404b540aSrobert 	      while (p != buffer->next_line && is_nvspace (p[-1]))
182*404b540aSrobert 		p--;
183*404b540aSrobert 	      if (p == buffer->next_line || p[-1] != '\\')
184*404b540aSrobert 		break;
185*404b540aSrobert 
186*404b540aSrobert 	      add_line_note (buffer, p - 1, p != d ? ' ': '\\');
187*404b540aSrobert 	      d = p - 2;
188*404b540aSrobert 	      buffer->next_line = p - 1;
189*404b540aSrobert 	    }
190*404b540aSrobert 	  else if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
191*404b540aSrobert 	    {
192*404b540aSrobert 	      /* Add a note regardless, for the benefit of -Wtrigraphs.  */
193*404b540aSrobert 	      add_line_note (buffer, d, s[2]);
194*404b540aSrobert 	      if (CPP_OPTION (pfile, trigraphs))
195*404b540aSrobert 		{
196*404b540aSrobert 		  *d = _cpp_trigraph_map[s[2]];
197*404b540aSrobert 		  s += 2;
198*404b540aSrobert 		}
199*404b540aSrobert 	    }
200*404b540aSrobert 	}
201*404b540aSrobert     }
202*404b540aSrobert   else
203*404b540aSrobert     {
204*404b540aSrobert       do
205*404b540aSrobert 	s++;
206*404b540aSrobert       while (*s != '\n' && *s != '\r');
207*404b540aSrobert       d = (uchar *) s;
208*404b540aSrobert 
209*404b540aSrobert       /* Handle DOS line endings.  */
210*404b540aSrobert       if (*s == '\r' && s != buffer->rlimit && s[1] == '\n')
211*404b540aSrobert 	s++;
212*404b540aSrobert     }
213*404b540aSrobert 
214*404b540aSrobert  done:
215*404b540aSrobert   *d = '\n';
216*404b540aSrobert   /* A sentinel note that should never be processed.  */
217*404b540aSrobert   add_line_note (buffer, d + 1, '\n');
218*404b540aSrobert   buffer->next_line = s + 1;
219*404b540aSrobert }
220*404b540aSrobert 
221*404b540aSrobert /* Return true if the trigraph indicated by NOTE should be warned
222*404b540aSrobert    about in a comment.  */
223*404b540aSrobert static bool
warn_in_comment(cpp_reader * pfile,_cpp_line_note * note)224*404b540aSrobert warn_in_comment (cpp_reader *pfile, _cpp_line_note *note)
225*404b540aSrobert {
226*404b540aSrobert   const uchar *p;
227*404b540aSrobert 
228*404b540aSrobert   /* Within comments we don't warn about trigraphs, unless the
229*404b540aSrobert      trigraph forms an escaped newline, as that may change
230*404b540aSrobert      behavior.  */
231*404b540aSrobert   if (note->type != '/')
232*404b540aSrobert     return false;
233*404b540aSrobert 
234*404b540aSrobert   /* If -trigraphs, then this was an escaped newline iff the next note
235*404b540aSrobert      is coincident.  */
236*404b540aSrobert   if (CPP_OPTION (pfile, trigraphs))
237*404b540aSrobert     return note[1].pos == note->pos;
238*404b540aSrobert 
239*404b540aSrobert   /* Otherwise, see if this forms an escaped newline.  */
240*404b540aSrobert   p = note->pos + 3;
241*404b540aSrobert   while (is_nvspace (*p))
242*404b540aSrobert     p++;
243*404b540aSrobert 
244*404b540aSrobert   /* There might have been escaped newlines between the trigraph and the
245*404b540aSrobert      newline we found.  Hence the position test.  */
246*404b540aSrobert   return (*p == '\n' && p < note[1].pos);
247*404b540aSrobert }
248*404b540aSrobert 
249*404b540aSrobert /* Process the notes created by add_line_note as far as the current
250*404b540aSrobert    location.  */
251*404b540aSrobert void
_cpp_process_line_notes(cpp_reader * pfile,int in_comment)252*404b540aSrobert _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
253*404b540aSrobert {
254*404b540aSrobert   cpp_buffer *buffer = pfile->buffer;
255*404b540aSrobert 
256*404b540aSrobert   for (;;)
257*404b540aSrobert     {
258*404b540aSrobert       _cpp_line_note *note = &buffer->notes[buffer->cur_note];
259*404b540aSrobert       unsigned int col;
260*404b540aSrobert 
261*404b540aSrobert       if (note->pos > buffer->cur)
262*404b540aSrobert 	break;
263*404b540aSrobert 
264*404b540aSrobert       buffer->cur_note++;
265*404b540aSrobert       col = CPP_BUF_COLUMN (buffer, note->pos + 1);
266*404b540aSrobert 
267*404b540aSrobert       if (note->type == '\\' || note->type == ' ')
268*404b540aSrobert 	{
269*404b540aSrobert 	  if (note->type == ' ' && !in_comment)
270*404b540aSrobert 	    cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
271*404b540aSrobert 				 "backslash and newline separated by space");
272*404b540aSrobert 
273*404b540aSrobert 	  if (buffer->next_line > buffer->rlimit)
274*404b540aSrobert 	    {
275*404b540aSrobert 	      cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, col,
276*404b540aSrobert 				   "backslash-newline at end of file");
277*404b540aSrobert 	      /* Prevent "no newline at end of file" warning.  */
278*404b540aSrobert 	      buffer->next_line = buffer->rlimit;
279*404b540aSrobert 	    }
280*404b540aSrobert 
281*404b540aSrobert 	  buffer->line_base = note->pos;
282*404b540aSrobert 	  CPP_INCREMENT_LINE (pfile, 0);
283*404b540aSrobert 	}
284*404b540aSrobert       else if (_cpp_trigraph_map[note->type])
285*404b540aSrobert 	{
286*404b540aSrobert 	  if (CPP_OPTION (pfile, warn_trigraphs)
287*404b540aSrobert 	      && (!in_comment || warn_in_comment (pfile, note)))
288*404b540aSrobert 	    {
289*404b540aSrobert 	      if (CPP_OPTION (pfile, trigraphs))
290*404b540aSrobert 		cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
291*404b540aSrobert 				     "trigraph ??%c converted to %c",
292*404b540aSrobert 				     note->type,
293*404b540aSrobert 				     (int) _cpp_trigraph_map[note->type]);
294*404b540aSrobert 	      else
295*404b540aSrobert 		{
296*404b540aSrobert 		  cpp_error_with_line
297*404b540aSrobert 		    (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
298*404b540aSrobert 		     "trigraph ??%c ignored, use -trigraphs to enable",
299*404b540aSrobert 		     note->type);
300*404b540aSrobert 		}
301*404b540aSrobert 	    }
302*404b540aSrobert 	}
303*404b540aSrobert       else
304*404b540aSrobert 	abort ();
305*404b540aSrobert     }
306*404b540aSrobert }
307*404b540aSrobert 
308*404b540aSrobert /* Skip a C-style block comment.  We find the end of the comment by
309*404b540aSrobert    seeing if an asterisk is before every '/' we encounter.  Returns
310*404b540aSrobert    nonzero if comment terminated by EOF, zero otherwise.
311*404b540aSrobert 
312*404b540aSrobert    Buffer->cur points to the initial asterisk of the comment.  */
313*404b540aSrobert bool
_cpp_skip_block_comment(cpp_reader * pfile)314*404b540aSrobert _cpp_skip_block_comment (cpp_reader *pfile)
315*404b540aSrobert {
316*404b540aSrobert   cpp_buffer *buffer = pfile->buffer;
317*404b540aSrobert   const uchar *cur = buffer->cur;
318*404b540aSrobert   uchar c;
319*404b540aSrobert 
320*404b540aSrobert   cur++;
321*404b540aSrobert   if (*cur == '/')
322*404b540aSrobert     cur++;
323*404b540aSrobert 
324*404b540aSrobert   for (;;)
325*404b540aSrobert     {
326*404b540aSrobert       /* People like decorating comments with '*', so check for '/'
327*404b540aSrobert 	 instead for efficiency.  */
328*404b540aSrobert       c = *cur++;
329*404b540aSrobert 
330*404b540aSrobert       if (c == '/')
331*404b540aSrobert 	{
332*404b540aSrobert 	  if (cur[-2] == '*')
333*404b540aSrobert 	    break;
334*404b540aSrobert 
335*404b540aSrobert 	  /* Warn about potential nested comments, but not if the '/'
336*404b540aSrobert 	     comes immediately before the true comment delimiter.
337*404b540aSrobert 	     Don't bother to get it right across escaped newlines.  */
338*404b540aSrobert 	  if (CPP_OPTION (pfile, warn_comments)
339*404b540aSrobert 	      && cur[0] == '*' && cur[1] != '/')
340*404b540aSrobert 	    {
341*404b540aSrobert 	      buffer->cur = cur;
342*404b540aSrobert 	      cpp_error_with_line (pfile, CPP_DL_WARNING,
343*404b540aSrobert 				   pfile->line_table->highest_line, CPP_BUF_COL (buffer),
344*404b540aSrobert 				   "\"/*\" within comment");
345*404b540aSrobert 	    }
346*404b540aSrobert 	}
347*404b540aSrobert       else if (c == '\n')
348*404b540aSrobert 	{
349*404b540aSrobert 	  unsigned int cols;
350*404b540aSrobert 	  buffer->cur = cur - 1;
351*404b540aSrobert 	  _cpp_process_line_notes (pfile, true);
352*404b540aSrobert 	  if (buffer->next_line >= buffer->rlimit)
353*404b540aSrobert 	    return true;
354*404b540aSrobert 	  _cpp_clean_line (pfile);
355*404b540aSrobert 
356*404b540aSrobert 	  cols = buffer->next_line - buffer->line_base;
357*404b540aSrobert 	  CPP_INCREMENT_LINE (pfile, cols);
358*404b540aSrobert 
359*404b540aSrobert 	  cur = buffer->cur;
360*404b540aSrobert 	}
361*404b540aSrobert     }
362*404b540aSrobert 
363*404b540aSrobert   buffer->cur = cur;
364*404b540aSrobert   _cpp_process_line_notes (pfile, true);
365*404b540aSrobert   return false;
366*404b540aSrobert }
367*404b540aSrobert 
368*404b540aSrobert /* Skip a C++ line comment, leaving buffer->cur pointing to the
369*404b540aSrobert    terminating newline.  Handles escaped newlines.  Returns nonzero
370*404b540aSrobert    if a multiline comment.  */
371*404b540aSrobert static int
skip_line_comment(cpp_reader * pfile)372*404b540aSrobert skip_line_comment (cpp_reader *pfile)
373*404b540aSrobert {
374*404b540aSrobert   cpp_buffer *buffer = pfile->buffer;
375*404b540aSrobert   unsigned int orig_line = pfile->line_table->highest_line;
376*404b540aSrobert 
377*404b540aSrobert   while (*buffer->cur != '\n')
378*404b540aSrobert     buffer->cur++;
379*404b540aSrobert 
380*404b540aSrobert   _cpp_process_line_notes (pfile, true);
381*404b540aSrobert   return orig_line != pfile->line_table->highest_line;
382*404b540aSrobert }
383*404b540aSrobert 
384*404b540aSrobert /* Skips whitespace, saving the next non-whitespace character.  */
385*404b540aSrobert static void
skip_whitespace(cpp_reader * pfile,cppchar_t c)386*404b540aSrobert skip_whitespace (cpp_reader *pfile, cppchar_t c)
387*404b540aSrobert {
388*404b540aSrobert   cpp_buffer *buffer = pfile->buffer;
389*404b540aSrobert   bool saw_NUL = false;
390*404b540aSrobert 
391*404b540aSrobert   do
392*404b540aSrobert     {
393*404b540aSrobert       /* Horizontal space always OK.  */
394*404b540aSrobert       if (c == ' ' || c == '\t')
395*404b540aSrobert 	;
396*404b540aSrobert       /* Just \f \v or \0 left.  */
397*404b540aSrobert       else if (c == '\0')
398*404b540aSrobert 	saw_NUL = true;
399*404b540aSrobert       else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
400*404b540aSrobert 	cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line,
401*404b540aSrobert 			     CPP_BUF_COL (buffer),
402*404b540aSrobert 			     "%s in preprocessing directive",
403*404b540aSrobert 			     c == '\f' ? "form feed" : "vertical tab");
404*404b540aSrobert 
405*404b540aSrobert       c = *buffer->cur++;
406*404b540aSrobert     }
407*404b540aSrobert   /* We only want non-vertical space, i.e. ' ' \t \f \v \0.  */
408*404b540aSrobert   while (is_nvspace (c));
409*404b540aSrobert 
410*404b540aSrobert   if (saw_NUL)
411*404b540aSrobert     cpp_error (pfile, CPP_DL_WARNING, "null character(s) ignored");
412*404b540aSrobert 
413*404b540aSrobert   buffer->cur--;
414*404b540aSrobert }
415*404b540aSrobert 
416*404b540aSrobert /* See if the characters of a number token are valid in a name (no
417*404b540aSrobert    '.', '+' or '-').  */
418*404b540aSrobert static int
name_p(cpp_reader * pfile,const cpp_string * string)419*404b540aSrobert name_p (cpp_reader *pfile, const cpp_string *string)
420*404b540aSrobert {
421*404b540aSrobert   unsigned int i;
422*404b540aSrobert 
423*404b540aSrobert   for (i = 0; i < string->len; i++)
424*404b540aSrobert     if (!is_idchar (string->text[i]))
425*404b540aSrobert       return 0;
426*404b540aSrobert 
427*404b540aSrobert   return 1;
428*404b540aSrobert }
429*404b540aSrobert 
430*404b540aSrobert /* After parsing an identifier or other sequence, produce a warning about
431*404b540aSrobert    sequences not in NFC/NFKC.  */
432*404b540aSrobert static void
warn_about_normalization(cpp_reader * pfile,const cpp_token * token,const struct normalize_state * s)433*404b540aSrobert warn_about_normalization (cpp_reader *pfile,
434*404b540aSrobert 			  const cpp_token *token,
435*404b540aSrobert 			  const struct normalize_state *s)
436*404b540aSrobert {
437*404b540aSrobert   if (CPP_OPTION (pfile, warn_normalize) < NORMALIZE_STATE_RESULT (s)
438*404b540aSrobert       && !pfile->state.skipping)
439*404b540aSrobert     {
440*404b540aSrobert       /* Make sure that the token is printed using UCNs, even
441*404b540aSrobert 	 if we'd otherwise happily print UTF-8.  */
442*404b540aSrobert       unsigned char *buf = XNEWVEC (unsigned char, cpp_token_len (token));
443*404b540aSrobert       size_t sz;
444*404b540aSrobert 
445*404b540aSrobert       sz = cpp_spell_token (pfile, token, buf, false) - buf;
446*404b540aSrobert       if (NORMALIZE_STATE_RESULT (s) == normalized_C)
447*404b540aSrobert 	cpp_error_with_line (pfile, CPP_DL_WARNING, token->src_loc, 0,
448*404b540aSrobert 			     "`%.*s' is not in NFKC", (int) sz, buf);
449*404b540aSrobert       else
450*404b540aSrobert 	cpp_error_with_line (pfile, CPP_DL_WARNING, token->src_loc, 0,
451*404b540aSrobert 			     "`%.*s' is not in NFC", (int) sz, buf);
452*404b540aSrobert     }
453*404b540aSrobert }
454*404b540aSrobert 
455*404b540aSrobert /* Returns TRUE if the sequence starting at buffer->cur is invalid in
456*404b540aSrobert    an identifier.  FIRST is TRUE if this starts an identifier.  */
457*404b540aSrobert static bool
forms_identifier_p(cpp_reader * pfile,int first,struct normalize_state * state)458*404b540aSrobert forms_identifier_p (cpp_reader *pfile, int first,
459*404b540aSrobert 		    struct normalize_state *state)
460*404b540aSrobert {
461*404b540aSrobert   cpp_buffer *buffer = pfile->buffer;
462*404b540aSrobert 
463*404b540aSrobert   if (*buffer->cur == '$')
464*404b540aSrobert     {
465*404b540aSrobert       if (!CPP_OPTION (pfile, dollars_in_ident))
466*404b540aSrobert 	return false;
467*404b540aSrobert 
468*404b540aSrobert       buffer->cur++;
469*404b540aSrobert       if (CPP_OPTION (pfile, warn_dollars) && !pfile->state.skipping)
470*404b540aSrobert 	{
471*404b540aSrobert 	  CPP_OPTION (pfile, warn_dollars) = 0;
472*404b540aSrobert 	  cpp_error (pfile, CPP_DL_PEDWARN, "'$' in identifier or number");
473*404b540aSrobert 	}
474*404b540aSrobert 
475*404b540aSrobert       return true;
476*404b540aSrobert     }
477*404b540aSrobert 
478*404b540aSrobert   /* Is this a syntactically valid UCN?  */
479*404b540aSrobert   if (CPP_OPTION (pfile, extended_identifiers)
480*404b540aSrobert       && *buffer->cur == '\\'
481*404b540aSrobert       && (buffer->cur[1] == 'u' || buffer->cur[1] == 'U'))
482*404b540aSrobert     {
483*404b540aSrobert       buffer->cur += 2;
484*404b540aSrobert       if (_cpp_valid_ucn (pfile, &buffer->cur, buffer->rlimit, 1 + !first,
485*404b540aSrobert 			  state))
486*404b540aSrobert 	return true;
487*404b540aSrobert       buffer->cur -= 2;
488*404b540aSrobert     }
489*404b540aSrobert 
490*404b540aSrobert   return false;
491*404b540aSrobert }
492*404b540aSrobert 
493*404b540aSrobert /* Lex an identifier starting at BUFFER->CUR - 1.  */
494*404b540aSrobert static cpp_hashnode *
lex_identifier(cpp_reader * pfile,const uchar * base,bool starts_ucn,struct normalize_state * nst)495*404b540aSrobert lex_identifier (cpp_reader *pfile, const uchar *base, bool starts_ucn,
496*404b540aSrobert 		struct normalize_state *nst)
497*404b540aSrobert {
498*404b540aSrobert   cpp_hashnode *result;
499*404b540aSrobert   const uchar *cur;
500*404b540aSrobert   unsigned int len;
501*404b540aSrobert   unsigned int hash = HT_HASHSTEP (0, *base);
502*404b540aSrobert 
503*404b540aSrobert   cur = pfile->buffer->cur;
504*404b540aSrobert   if (! starts_ucn)
505*404b540aSrobert     while (ISIDNUM (*cur))
506*404b540aSrobert       {
507*404b540aSrobert 	hash = HT_HASHSTEP (hash, *cur);
508*404b540aSrobert 	cur++;
509*404b540aSrobert       }
510*404b540aSrobert   pfile->buffer->cur = cur;
511*404b540aSrobert   if (starts_ucn || forms_identifier_p (pfile, false, nst))
512*404b540aSrobert     {
513*404b540aSrobert       /* Slower version for identifiers containing UCNs (or $).  */
514*404b540aSrobert       do {
515*404b540aSrobert 	while (ISIDNUM (*pfile->buffer->cur))
516*404b540aSrobert 	  {
517*404b540aSrobert 	    pfile->buffer->cur++;
518*404b540aSrobert 	    NORMALIZE_STATE_UPDATE_IDNUM (nst);
519*404b540aSrobert 	  }
520*404b540aSrobert       } while (forms_identifier_p (pfile, false, nst));
521*404b540aSrobert       result = _cpp_interpret_identifier (pfile, base,
522*404b540aSrobert 					  pfile->buffer->cur - base);
523*404b540aSrobert     }
524*404b540aSrobert   else
525*404b540aSrobert     {
526*404b540aSrobert       len = cur - base;
527*404b540aSrobert       hash = HT_HASHFINISH (hash, len);
528*404b540aSrobert 
529*404b540aSrobert       result = (cpp_hashnode *)
530*404b540aSrobert 	ht_lookup_with_hash (pfile->hash_table, base, len, hash, HT_ALLOC);
531*404b540aSrobert     }
532*404b540aSrobert 
533*404b540aSrobert   /* Rarely, identifiers require diagnostics when lexed.  */
534*404b540aSrobert   if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
535*404b540aSrobert 			&& !pfile->state.skipping, 0))
536*404b540aSrobert     {
537*404b540aSrobert       /* It is allowed to poison the same identifier twice.  */
538*404b540aSrobert       if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
539*404b540aSrobert 	cpp_error (pfile, CPP_DL_ERROR, "attempt to use poisoned \"%s\"",
540*404b540aSrobert 		   NODE_NAME (result));
541*404b540aSrobert 
542*404b540aSrobert       /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
543*404b540aSrobert 	 replacement list of a variadic macro.  */
544*404b540aSrobert       if (result == pfile->spec_nodes.n__VA_ARGS__
545*404b540aSrobert 	  && !pfile->state.va_args_ok)
546*404b540aSrobert 	cpp_error (pfile, CPP_DL_PEDWARN,
547*404b540aSrobert 		   "__VA_ARGS__ can only appear in the expansion"
548*404b540aSrobert 		   " of a C99 variadic macro");
549*404b540aSrobert     }
550*404b540aSrobert 
551*404b540aSrobert   return result;
552*404b540aSrobert }
553*404b540aSrobert 
554*404b540aSrobert /* Lex a number to NUMBER starting at BUFFER->CUR - 1.  */
555*404b540aSrobert static void
lex_number(cpp_reader * pfile,cpp_string * number,struct normalize_state * nst)556*404b540aSrobert lex_number (cpp_reader *pfile, cpp_string *number,
557*404b540aSrobert 	    struct normalize_state *nst)
558*404b540aSrobert {
559*404b540aSrobert   const uchar *cur;
560*404b540aSrobert   const uchar *base;
561*404b540aSrobert   uchar *dest;
562*404b540aSrobert 
563*404b540aSrobert   base = pfile->buffer->cur - 1;
564*404b540aSrobert   do
565*404b540aSrobert     {
566*404b540aSrobert       cur = pfile->buffer->cur;
567*404b540aSrobert 
568*404b540aSrobert       /* N.B. ISIDNUM does not include $.  */
569*404b540aSrobert       while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1]))
570*404b540aSrobert 	{
571*404b540aSrobert 	  cur++;
572*404b540aSrobert 	  NORMALIZE_STATE_UPDATE_IDNUM (nst);
573*404b540aSrobert 	}
574*404b540aSrobert 
575*404b540aSrobert       pfile->buffer->cur = cur;
576*404b540aSrobert     }
577*404b540aSrobert   while (forms_identifier_p (pfile, false, nst));
578*404b540aSrobert 
579*404b540aSrobert   number->len = cur - base;
580*404b540aSrobert   dest = _cpp_unaligned_alloc (pfile, number->len + 1);
581*404b540aSrobert   memcpy (dest, base, number->len);
582*404b540aSrobert   dest[number->len] = '\0';
583*404b540aSrobert   number->text = dest;
584*404b540aSrobert }
585*404b540aSrobert 
586*404b540aSrobert /* Create a token of type TYPE with a literal spelling.  */
587*404b540aSrobert static void
create_literal(cpp_reader * pfile,cpp_token * token,const uchar * base,unsigned int len,enum cpp_ttype type)588*404b540aSrobert create_literal (cpp_reader *pfile, cpp_token *token, const uchar *base,
589*404b540aSrobert 		unsigned int len, enum cpp_ttype type)
590*404b540aSrobert {
591*404b540aSrobert   uchar *dest = _cpp_unaligned_alloc (pfile, len + 1);
592*404b540aSrobert 
593*404b540aSrobert   memcpy (dest, base, len);
594*404b540aSrobert   dest[len] = '\0';
595*404b540aSrobert   token->type = type;
596*404b540aSrobert   token->val.str.len = len;
597*404b540aSrobert   token->val.str.text = dest;
598*404b540aSrobert }
599*404b540aSrobert 
600*404b540aSrobert /* Lexes a string, character constant, or angle-bracketed header file
601*404b540aSrobert    name.  The stored string contains the spelling, including opening
602*404b540aSrobert    quote and leading any leading 'L'.  It returns the type of the
603*404b540aSrobert    literal, or CPP_OTHER if it was not properly terminated.
604*404b540aSrobert 
605*404b540aSrobert    The spelling is NUL-terminated, but it is not guaranteed that this
606*404b540aSrobert    is the first NUL since embedded NULs are preserved.  */
607*404b540aSrobert static void
lex_string(cpp_reader * pfile,cpp_token * token,const uchar * base)608*404b540aSrobert lex_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
609*404b540aSrobert {
610*404b540aSrobert   bool saw_NUL = false;
611*404b540aSrobert   const uchar *cur;
612*404b540aSrobert   cppchar_t terminator;
613*404b540aSrobert   enum cpp_ttype type;
614*404b540aSrobert 
615*404b540aSrobert   cur = base;
616*404b540aSrobert   terminator = *cur++;
617*404b540aSrobert   if (terminator == 'L')
618*404b540aSrobert     terminator = *cur++;
619*404b540aSrobert   if (terminator == '\"')
620*404b540aSrobert     type = *base == 'L' ? CPP_WSTRING: CPP_STRING;
621*404b540aSrobert   else if (terminator == '\'')
622*404b540aSrobert     type = *base == 'L' ? CPP_WCHAR: CPP_CHAR;
623*404b540aSrobert   else
624*404b540aSrobert     terminator = '>', type = CPP_HEADER_NAME;
625*404b540aSrobert 
626*404b540aSrobert   for (;;)
627*404b540aSrobert     {
628*404b540aSrobert       cppchar_t c = *cur++;
629*404b540aSrobert 
630*404b540aSrobert       /* In #include-style directives, terminators are not escapable.  */
631*404b540aSrobert       if (c == '\\' && !pfile->state.angled_headers && *cur != '\n')
632*404b540aSrobert 	cur++;
633*404b540aSrobert       else if (c == terminator)
634*404b540aSrobert 	break;
635*404b540aSrobert       else if (c == '\n')
636*404b540aSrobert 	{
637*404b540aSrobert 	  cur--;
638*404b540aSrobert 	  type = CPP_OTHER;
639*404b540aSrobert 	  break;
640*404b540aSrobert 	}
641*404b540aSrobert       else if (c == '\0')
642*404b540aSrobert 	saw_NUL = true;
643*404b540aSrobert     }
644*404b540aSrobert 
645*404b540aSrobert   if (saw_NUL && !pfile->state.skipping)
646*404b540aSrobert     cpp_error (pfile, CPP_DL_WARNING,
647*404b540aSrobert 	       "null character(s) preserved in literal");
648*404b540aSrobert 
649*404b540aSrobert   if (type == CPP_OTHER && CPP_OPTION (pfile, lang) != CLK_ASM)
650*404b540aSrobert     cpp_error (pfile, CPP_DL_PEDWARN, "missing terminating %c character",
651*404b540aSrobert 	       (int) terminator);
652*404b540aSrobert 
653*404b540aSrobert   pfile->buffer->cur = cur;
654*404b540aSrobert   create_literal (pfile, token, base, cur - base, type);
655*404b540aSrobert }
656*404b540aSrobert 
657*404b540aSrobert /* The stored comment includes the comment start and any terminator.  */
658*404b540aSrobert static void
save_comment(cpp_reader * pfile,cpp_token * token,const unsigned char * from,cppchar_t type)659*404b540aSrobert save_comment (cpp_reader *pfile, cpp_token *token, const unsigned char *from,
660*404b540aSrobert 	      cppchar_t type)
661*404b540aSrobert {
662*404b540aSrobert   unsigned char *buffer;
663*404b540aSrobert   unsigned int len, clen;
664*404b540aSrobert 
665*404b540aSrobert   len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'.  */
666*404b540aSrobert 
667*404b540aSrobert   /* C++ comments probably (not definitely) have moved past a new
668*404b540aSrobert      line, which we don't want to save in the comment.  */
669*404b540aSrobert   if (is_vspace (pfile->buffer->cur[-1]))
670*404b540aSrobert     len--;
671*404b540aSrobert 
672*404b540aSrobert   /* If we are currently in a directive, then we need to store all
673*404b540aSrobert      C++ comments as C comments internally, and so we need to
674*404b540aSrobert      allocate a little extra space in that case.
675*404b540aSrobert 
676*404b540aSrobert      Note that the only time we encounter a directive here is
677*404b540aSrobert      when we are saving comments in a "#define".  */
678*404b540aSrobert   clen = (pfile->state.in_directive && type == '/') ? len + 2 : len;
679*404b540aSrobert 
680*404b540aSrobert   buffer = _cpp_unaligned_alloc (pfile, clen);
681*404b540aSrobert 
682*404b540aSrobert   token->type = CPP_COMMENT;
683*404b540aSrobert   token->val.str.len = clen;
684*404b540aSrobert   token->val.str.text = buffer;
685*404b540aSrobert 
686*404b540aSrobert   buffer[0] = '/';
687*404b540aSrobert   memcpy (buffer + 1, from, len - 1);
688*404b540aSrobert 
689*404b540aSrobert   /* Finish conversion to a C comment, if necessary.  */
690*404b540aSrobert   if (pfile->state.in_directive && type == '/')
691*404b540aSrobert     {
692*404b540aSrobert       buffer[1] = '*';
693*404b540aSrobert       buffer[clen - 2] = '*';
694*404b540aSrobert       buffer[clen - 1] = '/';
695*404b540aSrobert     }
696*404b540aSrobert }
697*404b540aSrobert 
698*404b540aSrobert /* Allocate COUNT tokens for RUN.  */
699*404b540aSrobert void
_cpp_init_tokenrun(tokenrun * run,unsigned int count)700*404b540aSrobert _cpp_init_tokenrun (tokenrun *run, unsigned int count)
701*404b540aSrobert {
702*404b540aSrobert   run->base = XNEWVEC (cpp_token, count);
703*404b540aSrobert   run->limit = run->base + count;
704*404b540aSrobert   run->next = NULL;
705*404b540aSrobert }
706*404b540aSrobert 
707*404b540aSrobert /* Returns the next tokenrun, or creates one if there is none.  */
708*404b540aSrobert static tokenrun *
next_tokenrun(tokenrun * run)709*404b540aSrobert next_tokenrun (tokenrun *run)
710*404b540aSrobert {
711*404b540aSrobert   if (run->next == NULL)
712*404b540aSrobert     {
713*404b540aSrobert       run->next = XNEW (tokenrun);
714*404b540aSrobert       run->next->prev = run;
715*404b540aSrobert       _cpp_init_tokenrun (run->next, 250);
716*404b540aSrobert     }
717*404b540aSrobert 
718*404b540aSrobert   return run->next;
719*404b540aSrobert }
720*404b540aSrobert 
721*404b540aSrobert /* Allocate a single token that is invalidated at the same time as the
722*404b540aSrobert    rest of the tokens on the line.  Has its line and col set to the
723*404b540aSrobert    same as the last lexed token, so that diagnostics appear in the
724*404b540aSrobert    right place.  */
725*404b540aSrobert cpp_token *
_cpp_temp_token(cpp_reader * pfile)726*404b540aSrobert _cpp_temp_token (cpp_reader *pfile)
727*404b540aSrobert {
728*404b540aSrobert   cpp_token *old, *result;
729*404b540aSrobert 
730*404b540aSrobert   old = pfile->cur_token - 1;
731*404b540aSrobert   if (pfile->cur_token == pfile->cur_run->limit)
732*404b540aSrobert     {
733*404b540aSrobert       pfile->cur_run = next_tokenrun (pfile->cur_run);
734*404b540aSrobert       pfile->cur_token = pfile->cur_run->base;
735*404b540aSrobert     }
736*404b540aSrobert 
737*404b540aSrobert   result = pfile->cur_token++;
738*404b540aSrobert   result->src_loc = old->src_loc;
739*404b540aSrobert   return result;
740*404b540aSrobert }
741*404b540aSrobert 
742*404b540aSrobert /* Lex a token into RESULT (external interface).  Takes care of issues
743*404b540aSrobert    like directive handling, token lookahead, multiple include
744*404b540aSrobert    optimization and skipping.  */
745*404b540aSrobert const cpp_token *
_cpp_lex_token(cpp_reader * pfile)746*404b540aSrobert _cpp_lex_token (cpp_reader *pfile)
747*404b540aSrobert {
748*404b540aSrobert   cpp_token *result;
749*404b540aSrobert 
750*404b540aSrobert   for (;;)
751*404b540aSrobert     {
752*404b540aSrobert       if (pfile->cur_token == pfile->cur_run->limit)
753*404b540aSrobert 	{
754*404b540aSrobert 	  pfile->cur_run = next_tokenrun (pfile->cur_run);
755*404b540aSrobert 	  pfile->cur_token = pfile->cur_run->base;
756*404b540aSrobert 	}
757*404b540aSrobert 
758*404b540aSrobert       if (pfile->lookaheads)
759*404b540aSrobert 	{
760*404b540aSrobert 	  pfile->lookaheads--;
761*404b540aSrobert 	  result = pfile->cur_token++;
762*404b540aSrobert 	}
763*404b540aSrobert       else
764*404b540aSrobert 	result = _cpp_lex_direct (pfile);
765*404b540aSrobert 
766*404b540aSrobert       if (result->flags & BOL)
767*404b540aSrobert 	{
768*404b540aSrobert 	  /* Is this a directive.  If _cpp_handle_directive returns
769*404b540aSrobert 	     false, it is an assembler #.  */
770*404b540aSrobert 	  if (result->type == CPP_HASH
771*404b540aSrobert 	      /* 6.10.3 p 11: Directives in a list of macro arguments
772*404b540aSrobert 		 gives undefined behavior.  This implementation
773*404b540aSrobert 		 handles the directive as normal.  */
774*404b540aSrobert 	      && pfile->state.parsing_args != 1)
775*404b540aSrobert 	    {
776*404b540aSrobert 	      if (_cpp_handle_directive (pfile, result->flags & PREV_WHITE))
777*404b540aSrobert 		{
778*404b540aSrobert 		  if (pfile->directive_result.type == CPP_PADDING)
779*404b540aSrobert 		    continue;
780*404b540aSrobert 		  result = &pfile->directive_result;
781*404b540aSrobert 		}
782*404b540aSrobert 	    }
783*404b540aSrobert 	  else if (pfile->state.in_deferred_pragma)
784*404b540aSrobert 	    result = &pfile->directive_result;
785*404b540aSrobert 
786*404b540aSrobert 	  if (pfile->cb.line_change && !pfile->state.skipping)
787*404b540aSrobert 	    pfile->cb.line_change (pfile, result, pfile->state.parsing_args);
788*404b540aSrobert 	}
789*404b540aSrobert 
790*404b540aSrobert       /* We don't skip tokens in directives.  */
791*404b540aSrobert       if (pfile->state.in_directive || pfile->state.in_deferred_pragma)
792*404b540aSrobert 	break;
793*404b540aSrobert 
794*404b540aSrobert       /* Outside a directive, invalidate controlling macros.  At file
795*404b540aSrobert 	 EOF, _cpp_lex_direct takes care of popping the buffer, so we never
796*404b540aSrobert 	 get here and MI optimization works.  */
797*404b540aSrobert       pfile->mi_valid = false;
798*404b540aSrobert 
799*404b540aSrobert       if (!pfile->state.skipping || result->type == CPP_EOF)
800*404b540aSrobert 	break;
801*404b540aSrobert     }
802*404b540aSrobert 
803*404b540aSrobert   return result;
804*404b540aSrobert }
805*404b540aSrobert 
806*404b540aSrobert /* Returns true if a fresh line has been loaded.  */
807*404b540aSrobert bool
_cpp_get_fresh_line(cpp_reader * pfile)808*404b540aSrobert _cpp_get_fresh_line (cpp_reader *pfile)
809*404b540aSrobert {
810*404b540aSrobert   int return_at_eof;
811*404b540aSrobert 
812*404b540aSrobert   /* We can't get a new line until we leave the current directive.  */
813*404b540aSrobert   if (pfile->state.in_directive)
814*404b540aSrobert     return false;
815*404b540aSrobert 
816*404b540aSrobert   for (;;)
817*404b540aSrobert     {
818*404b540aSrobert       cpp_buffer *buffer = pfile->buffer;
819*404b540aSrobert 
820*404b540aSrobert       if (!buffer->need_line)
821*404b540aSrobert 	return true;
822*404b540aSrobert 
823*404b540aSrobert       if (buffer->next_line < buffer->rlimit)
824*404b540aSrobert 	{
825*404b540aSrobert 	  _cpp_clean_line (pfile);
826*404b540aSrobert 	  return true;
827*404b540aSrobert 	}
828*404b540aSrobert 
829*404b540aSrobert       /* First, get out of parsing arguments state.  */
830*404b540aSrobert       if (pfile->state.parsing_args)
831*404b540aSrobert 	return false;
832*404b540aSrobert 
833*404b540aSrobert       /* End of buffer.  Non-empty files should end in a newline.  */
834*404b540aSrobert       if (buffer->buf != buffer->rlimit
835*404b540aSrobert 	  && buffer->next_line > buffer->rlimit
836*404b540aSrobert 	  && !buffer->from_stage3)
837*404b540aSrobert 	{
838*404b540aSrobert 	  /* Only warn once.  */
839*404b540aSrobert 	  buffer->next_line = buffer->rlimit;
840*404b540aSrobert 	}
841*404b540aSrobert 
842*404b540aSrobert       return_at_eof = buffer->return_at_eof;
843*404b540aSrobert       _cpp_pop_buffer (pfile);
844*404b540aSrobert       if (pfile->buffer == NULL || return_at_eof)
845*404b540aSrobert 	return false;
846*404b540aSrobert     }
847*404b540aSrobert }
848*404b540aSrobert 
849*404b540aSrobert #define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE)		\
850*404b540aSrobert   do							\
851*404b540aSrobert     {							\
852*404b540aSrobert       result->type = ELSE_TYPE;				\
853*404b540aSrobert       if (*buffer->cur == CHAR)				\
854*404b540aSrobert 	buffer->cur++, result->type = THEN_TYPE;	\
855*404b540aSrobert     }							\
856*404b540aSrobert   while (0)
857*404b540aSrobert 
858*404b540aSrobert /* Lex a token into pfile->cur_token, which is also incremented, to
859*404b540aSrobert    get diagnostics pointing to the correct location.
860*404b540aSrobert 
861*404b540aSrobert    Does not handle issues such as token lookahead, multiple-include
862*404b540aSrobert    optimization, directives, skipping etc.  This function is only
863*404b540aSrobert    suitable for use by _cpp_lex_token, and in special cases like
864*404b540aSrobert    lex_expansion_token which doesn't care for any of these issues.
865*404b540aSrobert 
866*404b540aSrobert    When meeting a newline, returns CPP_EOF if parsing a directive,
867*404b540aSrobert    otherwise returns to the start of the token buffer if permissible.
868*404b540aSrobert    Returns the location of the lexed token.  */
869*404b540aSrobert cpp_token *
_cpp_lex_direct(cpp_reader * pfile)870*404b540aSrobert _cpp_lex_direct (cpp_reader *pfile)
871*404b540aSrobert {
872*404b540aSrobert   cppchar_t c;
873*404b540aSrobert   cpp_buffer *buffer;
874*404b540aSrobert   const unsigned char *comment_start;
875*404b540aSrobert   cpp_token *result = pfile->cur_token++;
876*404b540aSrobert 
877*404b540aSrobert  fresh_line:
878*404b540aSrobert   result->flags = 0;
879*404b540aSrobert   buffer = pfile->buffer;
880*404b540aSrobert   if (buffer->need_line)
881*404b540aSrobert     {
882*404b540aSrobert       if (pfile->state.in_deferred_pragma)
883*404b540aSrobert 	{
884*404b540aSrobert 	  result->type = CPP_PRAGMA_EOL;
885*404b540aSrobert 	  pfile->state.in_deferred_pragma = false;
886*404b540aSrobert 	  if (!pfile->state.pragma_allow_expansion)
887*404b540aSrobert 	    pfile->state.prevent_expansion--;
888*404b540aSrobert 	  return result;
889*404b540aSrobert 	}
890*404b540aSrobert       if (!_cpp_get_fresh_line (pfile))
891*404b540aSrobert 	{
892*404b540aSrobert 	  result->type = CPP_EOF;
893*404b540aSrobert 	  if (!pfile->state.in_directive)
894*404b540aSrobert 	    {
895*404b540aSrobert 	      /* Tell the compiler the line number of the EOF token.  */
896*404b540aSrobert 	      result->src_loc = pfile->line_table->highest_line;
897*404b540aSrobert 	      result->flags = BOL;
898*404b540aSrobert 	    }
899*404b540aSrobert 	  return result;
900*404b540aSrobert 	}
901*404b540aSrobert       if (!pfile->keep_tokens)
902*404b540aSrobert 	{
903*404b540aSrobert 	  pfile->cur_run = &pfile->base_run;
904*404b540aSrobert 	  result = pfile->base_run.base;
905*404b540aSrobert 	  pfile->cur_token = result + 1;
906*404b540aSrobert 	}
907*404b540aSrobert       result->flags = BOL;
908*404b540aSrobert       if (pfile->state.parsing_args == 2)
909*404b540aSrobert 	result->flags |= PREV_WHITE;
910*404b540aSrobert     }
911*404b540aSrobert   buffer = pfile->buffer;
912*404b540aSrobert  update_tokens_line:
913*404b540aSrobert   result->src_loc = pfile->line_table->highest_line;
914*404b540aSrobert 
915*404b540aSrobert  skipped_white:
916*404b540aSrobert   if (buffer->cur >= buffer->notes[buffer->cur_note].pos
917*404b540aSrobert       && !pfile->overlaid_buffer)
918*404b540aSrobert     {
919*404b540aSrobert       _cpp_process_line_notes (pfile, false);
920*404b540aSrobert       result->src_loc = pfile->line_table->highest_line;
921*404b540aSrobert     }
922*404b540aSrobert   c = *buffer->cur++;
923*404b540aSrobert 
924*404b540aSrobert   LINEMAP_POSITION_FOR_COLUMN (result->src_loc, pfile->line_table,
925*404b540aSrobert 			       CPP_BUF_COLUMN (buffer, buffer->cur));
926*404b540aSrobert 
927*404b540aSrobert   switch (c)
928*404b540aSrobert     {
929*404b540aSrobert     case ' ': case '\t': case '\f': case '\v': case '\0':
930*404b540aSrobert       result->flags |= PREV_WHITE;
931*404b540aSrobert       skip_whitespace (pfile, c);
932*404b540aSrobert       goto skipped_white;
933*404b540aSrobert 
934*404b540aSrobert     case '\n':
935*404b540aSrobert       if (buffer->cur < buffer->rlimit)
936*404b540aSrobert 	CPP_INCREMENT_LINE (pfile, 0);
937*404b540aSrobert       buffer->need_line = true;
938*404b540aSrobert       goto fresh_line;
939*404b540aSrobert 
940*404b540aSrobert     case '0': case '1': case '2': case '3': case '4':
941*404b540aSrobert     case '5': case '6': case '7': case '8': case '9':
942*404b540aSrobert       {
943*404b540aSrobert 	struct normalize_state nst = INITIAL_NORMALIZE_STATE;
944*404b540aSrobert 	result->type = CPP_NUMBER;
945*404b540aSrobert 	lex_number (pfile, &result->val.str, &nst);
946*404b540aSrobert 	warn_about_normalization (pfile, result, &nst);
947*404b540aSrobert 	break;
948*404b540aSrobert       }
949*404b540aSrobert 
950*404b540aSrobert     case 'L':
951*404b540aSrobert       /* 'L' may introduce wide characters or strings.  */
952*404b540aSrobert       if (*buffer->cur == '\'' || *buffer->cur == '"')
953*404b540aSrobert 	{
954*404b540aSrobert 	  lex_string (pfile, result, buffer->cur - 1);
955*404b540aSrobert 	  break;
956*404b540aSrobert 	}
957*404b540aSrobert       /* Fall through.  */
958*404b540aSrobert 
959*404b540aSrobert     case '_':
960*404b540aSrobert     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
961*404b540aSrobert     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
962*404b540aSrobert     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
963*404b540aSrobert     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
964*404b540aSrobert     case 'y': case 'z':
965*404b540aSrobert     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
966*404b540aSrobert     case 'G': case 'H': case 'I': case 'J': case 'K':
967*404b540aSrobert     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
968*404b540aSrobert     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
969*404b540aSrobert     case 'Y': case 'Z':
970*404b540aSrobert       result->type = CPP_NAME;
971*404b540aSrobert       {
972*404b540aSrobert 	struct normalize_state nst = INITIAL_NORMALIZE_STATE;
973*404b540aSrobert 	result->val.node = lex_identifier (pfile, buffer->cur - 1, false,
974*404b540aSrobert 					   &nst);
975*404b540aSrobert 	warn_about_normalization (pfile, result, &nst);
976*404b540aSrobert       }
977*404b540aSrobert 
978*404b540aSrobert       /* Convert named operators to their proper types.  */
979*404b540aSrobert       if (result->val.node->flags & NODE_OPERATOR)
980*404b540aSrobert 	{
981*404b540aSrobert 	  result->flags |= NAMED_OP;
982*404b540aSrobert 	  result->type = (enum cpp_ttype) result->val.node->directive_index;
983*404b540aSrobert 	}
984*404b540aSrobert       break;
985*404b540aSrobert 
986*404b540aSrobert     case '\'':
987*404b540aSrobert     case '"':
988*404b540aSrobert       lex_string (pfile, result, buffer->cur - 1);
989*404b540aSrobert       break;
990*404b540aSrobert 
991*404b540aSrobert     case '/':
992*404b540aSrobert       /* A potential block or line comment.  */
993*404b540aSrobert       comment_start = buffer->cur;
994*404b540aSrobert       c = *buffer->cur;
995*404b540aSrobert 
996*404b540aSrobert       if (c == '*')
997*404b540aSrobert 	{
998*404b540aSrobert 	  if (_cpp_skip_block_comment (pfile))
999*404b540aSrobert 	    cpp_error (pfile, CPP_DL_ERROR, "unterminated comment");
1000*404b540aSrobert 	}
1001*404b540aSrobert       else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments)
1002*404b540aSrobert 			    || cpp_in_system_header (pfile)))
1003*404b540aSrobert 	{
1004*404b540aSrobert 	  /* Warn about comments only if pedantically GNUC89, and not
1005*404b540aSrobert 	     in system headers.  */
1006*404b540aSrobert 	  if (CPP_OPTION (pfile, lang) == CLK_GNUC89 && CPP_PEDANTIC (pfile)
1007*404b540aSrobert 	      && ! buffer->warned_cplusplus_comments)
1008*404b540aSrobert 	    {
1009*404b540aSrobert 	      cpp_error (pfile, CPP_DL_PEDWARN,
1010*404b540aSrobert 			 "C++ style comments are not allowed in ISO C90");
1011*404b540aSrobert 	      cpp_error (pfile, CPP_DL_PEDWARN,
1012*404b540aSrobert 			 "(this will be reported only once per input file)");
1013*404b540aSrobert 	      buffer->warned_cplusplus_comments = 1;
1014*404b540aSrobert 	    }
1015*404b540aSrobert 
1016*404b540aSrobert 	  if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments))
1017*404b540aSrobert 	    cpp_error (pfile, CPP_DL_WARNING, "multi-line comment");
1018*404b540aSrobert 	}
1019*404b540aSrobert       else if (c == '=')
1020*404b540aSrobert 	{
1021*404b540aSrobert 	  buffer->cur++;
1022*404b540aSrobert 	  result->type = CPP_DIV_EQ;
1023*404b540aSrobert 	  break;
1024*404b540aSrobert 	}
1025*404b540aSrobert       else
1026*404b540aSrobert 	{
1027*404b540aSrobert 	  result->type = CPP_DIV;
1028*404b540aSrobert 	  break;
1029*404b540aSrobert 	}
1030*404b540aSrobert 
1031*404b540aSrobert       if (!pfile->state.save_comments)
1032*404b540aSrobert 	{
1033*404b540aSrobert 	  result->flags |= PREV_WHITE;
1034*404b540aSrobert 	  goto update_tokens_line;
1035*404b540aSrobert 	}
1036*404b540aSrobert 
1037*404b540aSrobert       /* Save the comment as a token in its own right.  */
1038*404b540aSrobert       save_comment (pfile, result, comment_start, c);
1039*404b540aSrobert       break;
1040*404b540aSrobert 
1041*404b540aSrobert     case '<':
1042*404b540aSrobert       if (pfile->state.angled_headers)
1043*404b540aSrobert 	{
1044*404b540aSrobert 	  lex_string (pfile, result, buffer->cur - 1);
1045*404b540aSrobert 	  break;
1046*404b540aSrobert 	}
1047*404b540aSrobert 
1048*404b540aSrobert       result->type = CPP_LESS;
1049*404b540aSrobert       if (*buffer->cur == '=')
1050*404b540aSrobert 	buffer->cur++, result->type = CPP_LESS_EQ;
1051*404b540aSrobert       else if (*buffer->cur == '<')
1052*404b540aSrobert 	{
1053*404b540aSrobert 	  buffer->cur++;
1054*404b540aSrobert 	  IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT);
1055*404b540aSrobert 	}
1056*404b540aSrobert       else if (CPP_OPTION (pfile, digraphs))
1057*404b540aSrobert 	{
1058*404b540aSrobert 	  if (*buffer->cur == ':')
1059*404b540aSrobert 	    {
1060*404b540aSrobert 	      buffer->cur++;
1061*404b540aSrobert 	      result->flags |= DIGRAPH;
1062*404b540aSrobert 	      result->type = CPP_OPEN_SQUARE;
1063*404b540aSrobert 	    }
1064*404b540aSrobert 	  else if (*buffer->cur == '%')
1065*404b540aSrobert 	    {
1066*404b540aSrobert 	      buffer->cur++;
1067*404b540aSrobert 	      result->flags |= DIGRAPH;
1068*404b540aSrobert 	      result->type = CPP_OPEN_BRACE;
1069*404b540aSrobert 	    }
1070*404b540aSrobert 	}
1071*404b540aSrobert       break;
1072*404b540aSrobert 
1073*404b540aSrobert     case '>':
1074*404b540aSrobert       result->type = CPP_GREATER;
1075*404b540aSrobert       if (*buffer->cur == '=')
1076*404b540aSrobert 	buffer->cur++, result->type = CPP_GREATER_EQ;
1077*404b540aSrobert       else if (*buffer->cur == '>')
1078*404b540aSrobert 	{
1079*404b540aSrobert 	  buffer->cur++;
1080*404b540aSrobert 	  IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT);
1081*404b540aSrobert 	}
1082*404b540aSrobert       break;
1083*404b540aSrobert 
1084*404b540aSrobert     case '%':
1085*404b540aSrobert       result->type = CPP_MOD;
1086*404b540aSrobert       if (*buffer->cur == '=')
1087*404b540aSrobert 	buffer->cur++, result->type = CPP_MOD_EQ;
1088*404b540aSrobert       else if (CPP_OPTION (pfile, digraphs))
1089*404b540aSrobert 	{
1090*404b540aSrobert 	  if (*buffer->cur == ':')
1091*404b540aSrobert 	    {
1092*404b540aSrobert 	      buffer->cur++;
1093*404b540aSrobert 	      result->flags |= DIGRAPH;
1094*404b540aSrobert 	      result->type = CPP_HASH;
1095*404b540aSrobert 	      if (*buffer->cur == '%' && buffer->cur[1] == ':')
1096*404b540aSrobert 		buffer->cur += 2, result->type = CPP_PASTE;
1097*404b540aSrobert 	    }
1098*404b540aSrobert 	  else if (*buffer->cur == '>')
1099*404b540aSrobert 	    {
1100*404b540aSrobert 	      buffer->cur++;
1101*404b540aSrobert 	      result->flags |= DIGRAPH;
1102*404b540aSrobert 	      result->type = CPP_CLOSE_BRACE;
1103*404b540aSrobert 	    }
1104*404b540aSrobert 	}
1105*404b540aSrobert       break;
1106*404b540aSrobert 
1107*404b540aSrobert     case '.':
1108*404b540aSrobert       result->type = CPP_DOT;
1109*404b540aSrobert       if (ISDIGIT (*buffer->cur))
1110*404b540aSrobert 	{
1111*404b540aSrobert 	  struct normalize_state nst = INITIAL_NORMALIZE_STATE;
1112*404b540aSrobert 	  result->type = CPP_NUMBER;
1113*404b540aSrobert 	  lex_number (pfile, &result->val.str, &nst);
1114*404b540aSrobert 	  warn_about_normalization (pfile, result, &nst);
1115*404b540aSrobert 	}
1116*404b540aSrobert       else if (*buffer->cur == '.' && buffer->cur[1] == '.')
1117*404b540aSrobert 	buffer->cur += 2, result->type = CPP_ELLIPSIS;
1118*404b540aSrobert       else if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
1119*404b540aSrobert 	buffer->cur++, result->type = CPP_DOT_STAR;
1120*404b540aSrobert       break;
1121*404b540aSrobert 
1122*404b540aSrobert     case '+':
1123*404b540aSrobert       result->type = CPP_PLUS;
1124*404b540aSrobert       if (*buffer->cur == '+')
1125*404b540aSrobert 	buffer->cur++, result->type = CPP_PLUS_PLUS;
1126*404b540aSrobert       else if (*buffer->cur == '=')
1127*404b540aSrobert 	buffer->cur++, result->type = CPP_PLUS_EQ;
1128*404b540aSrobert       break;
1129*404b540aSrobert 
1130*404b540aSrobert     case '-':
1131*404b540aSrobert       result->type = CPP_MINUS;
1132*404b540aSrobert       if (*buffer->cur == '>')
1133*404b540aSrobert 	{
1134*404b540aSrobert 	  buffer->cur++;
1135*404b540aSrobert 	  result->type = CPP_DEREF;
1136*404b540aSrobert 	  if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
1137*404b540aSrobert 	    buffer->cur++, result->type = CPP_DEREF_STAR;
1138*404b540aSrobert 	}
1139*404b540aSrobert       else if (*buffer->cur == '-')
1140*404b540aSrobert 	buffer->cur++, result->type = CPP_MINUS_MINUS;
1141*404b540aSrobert       else if (*buffer->cur == '=')
1142*404b540aSrobert 	buffer->cur++, result->type = CPP_MINUS_EQ;
1143*404b540aSrobert       break;
1144*404b540aSrobert 
1145*404b540aSrobert     case '&':
1146*404b540aSrobert       result->type = CPP_AND;
1147*404b540aSrobert       if (*buffer->cur == '&')
1148*404b540aSrobert 	buffer->cur++, result->type = CPP_AND_AND;
1149*404b540aSrobert       else if (*buffer->cur == '=')
1150*404b540aSrobert 	buffer->cur++, result->type = CPP_AND_EQ;
1151*404b540aSrobert       break;
1152*404b540aSrobert 
1153*404b540aSrobert     case '|':
1154*404b540aSrobert       result->type = CPP_OR;
1155*404b540aSrobert       if (*buffer->cur == '|')
1156*404b540aSrobert 	buffer->cur++, result->type = CPP_OR_OR;
1157*404b540aSrobert       else if (*buffer->cur == '=')
1158*404b540aSrobert 	buffer->cur++, result->type = CPP_OR_EQ;
1159*404b540aSrobert       break;
1160*404b540aSrobert 
1161*404b540aSrobert     case ':':
1162*404b540aSrobert       result->type = CPP_COLON;
1163*404b540aSrobert       if (*buffer->cur == ':' && CPP_OPTION (pfile, cplusplus))
1164*404b540aSrobert 	buffer->cur++, result->type = CPP_SCOPE;
1165*404b540aSrobert       else if (*buffer->cur == '>' && CPP_OPTION (pfile, digraphs))
1166*404b540aSrobert 	{
1167*404b540aSrobert 	  buffer->cur++;
1168*404b540aSrobert 	  result->flags |= DIGRAPH;
1169*404b540aSrobert 	  result->type = CPP_CLOSE_SQUARE;
1170*404b540aSrobert 	}
1171*404b540aSrobert       break;
1172*404b540aSrobert 
1173*404b540aSrobert     case '*': IF_NEXT_IS ('=', CPP_MULT_EQ, CPP_MULT); break;
1174*404b540aSrobert     case '=': IF_NEXT_IS ('=', CPP_EQ_EQ, CPP_EQ); break;
1175*404b540aSrobert     case '!': IF_NEXT_IS ('=', CPP_NOT_EQ, CPP_NOT); break;
1176*404b540aSrobert     case '^': IF_NEXT_IS ('=', CPP_XOR_EQ, CPP_XOR); break;
1177*404b540aSrobert     case '#': IF_NEXT_IS ('#', CPP_PASTE, CPP_HASH); break;
1178*404b540aSrobert 
1179*404b540aSrobert     case '?': result->type = CPP_QUERY; break;
1180*404b540aSrobert     case '~': result->type = CPP_COMPL; break;
1181*404b540aSrobert     case ',': result->type = CPP_COMMA; break;
1182*404b540aSrobert     case '(': result->type = CPP_OPEN_PAREN; break;
1183*404b540aSrobert     case ')': result->type = CPP_CLOSE_PAREN; break;
1184*404b540aSrobert     case '[': result->type = CPP_OPEN_SQUARE; break;
1185*404b540aSrobert     case ']': result->type = CPP_CLOSE_SQUARE; break;
1186*404b540aSrobert     case '{': result->type = CPP_OPEN_BRACE; break;
1187*404b540aSrobert     case '}': result->type = CPP_CLOSE_BRACE; break;
1188*404b540aSrobert     case ';': result->type = CPP_SEMICOLON; break;
1189*404b540aSrobert 
1190*404b540aSrobert       /* @ is a punctuator in Objective-C.  */
1191*404b540aSrobert     case '@': result->type = CPP_ATSIGN; break;
1192*404b540aSrobert 
1193*404b540aSrobert     case '$':
1194*404b540aSrobert     case '\\':
1195*404b540aSrobert       {
1196*404b540aSrobert 	const uchar *base = --buffer->cur;
1197*404b540aSrobert 	struct normalize_state nst = INITIAL_NORMALIZE_STATE;
1198*404b540aSrobert 
1199*404b540aSrobert 	if (forms_identifier_p (pfile, true, &nst))
1200*404b540aSrobert 	  {
1201*404b540aSrobert 	    result->type = CPP_NAME;
1202*404b540aSrobert 	    result->val.node = lex_identifier (pfile, base, true, &nst);
1203*404b540aSrobert 	    warn_about_normalization (pfile, result, &nst);
1204*404b540aSrobert 	    break;
1205*404b540aSrobert 	  }
1206*404b540aSrobert 	buffer->cur++;
1207*404b540aSrobert       }
1208*404b540aSrobert 
1209*404b540aSrobert     default:
1210*404b540aSrobert       create_literal (pfile, result, buffer->cur - 1, 1, CPP_OTHER);
1211*404b540aSrobert       break;
1212*404b540aSrobert     }
1213*404b540aSrobert 
1214*404b540aSrobert   return result;
1215*404b540aSrobert }
1216*404b540aSrobert 
1217*404b540aSrobert /* An upper bound on the number of bytes needed to spell TOKEN.
1218*404b540aSrobert    Does not include preceding whitespace.  */
1219*404b540aSrobert unsigned int
cpp_token_len(const cpp_token * token)1220*404b540aSrobert cpp_token_len (const cpp_token *token)
1221*404b540aSrobert {
1222*404b540aSrobert   unsigned int len;
1223*404b540aSrobert 
1224*404b540aSrobert   switch (TOKEN_SPELL (token))
1225*404b540aSrobert     {
1226*404b540aSrobert     default:		len = 4;				break;
1227*404b540aSrobert     case SPELL_LITERAL:	len = token->val.str.len;		break;
1228*404b540aSrobert     case SPELL_IDENT:	len = NODE_LEN (token->val.node) * 10;	break;
1229*404b540aSrobert     }
1230*404b540aSrobert 
1231*404b540aSrobert   return len;
1232*404b540aSrobert }
1233*404b540aSrobert 
1234*404b540aSrobert /* Parse UTF-8 out of NAMEP and place a \U escape in BUFFER.
1235*404b540aSrobert    Return the number of bytes read out of NAME.  (There are always
1236*404b540aSrobert    10 bytes written to BUFFER.)  */
1237*404b540aSrobert 
1238*404b540aSrobert static size_t
utf8_to_ucn(unsigned char * buffer,const unsigned char * name)1239*404b540aSrobert utf8_to_ucn (unsigned char *buffer, const unsigned char *name)
1240*404b540aSrobert {
1241*404b540aSrobert   int j;
1242*404b540aSrobert   int ucn_len = 0;
1243*404b540aSrobert   int ucn_len_c;
1244*404b540aSrobert   unsigned t;
1245*404b540aSrobert   unsigned long utf32;
1246*404b540aSrobert 
1247*404b540aSrobert   /* Compute the length of the UTF-8 sequence.  */
1248*404b540aSrobert   for (t = *name; t & 0x80; t <<= 1)
1249*404b540aSrobert     ucn_len++;
1250*404b540aSrobert 
1251*404b540aSrobert   utf32 = *name & (0x7F >> ucn_len);
1252*404b540aSrobert   for (ucn_len_c = 1; ucn_len_c < ucn_len; ucn_len_c++)
1253*404b540aSrobert     {
1254*404b540aSrobert       utf32 = (utf32 << 6) | (*++name & 0x3F);
1255*404b540aSrobert 
1256*404b540aSrobert       /* Ill-formed UTF-8.  */
1257*404b540aSrobert       if ((*name & ~0x3F) != 0x80)
1258*404b540aSrobert 	abort ();
1259*404b540aSrobert     }
1260*404b540aSrobert 
1261*404b540aSrobert   *buffer++ = '\\';
1262*404b540aSrobert   *buffer++ = 'U';
1263*404b540aSrobert   for (j = 7; j >= 0; j--)
1264*404b540aSrobert     *buffer++ = "0123456789abcdef"[(utf32 >> (4 * j)) & 0xF];
1265*404b540aSrobert   return ucn_len;
1266*404b540aSrobert }
1267*404b540aSrobert 
1268*404b540aSrobert 
1269*404b540aSrobert /* Write the spelling of a token TOKEN to BUFFER.  The buffer must
1270*404b540aSrobert    already contain the enough space to hold the token's spelling.
1271*404b540aSrobert    Returns a pointer to the character after the last character written.
1272*404b540aSrobert    FORSTRING is true if this is to be the spelling after translation
1273*404b540aSrobert    phase 1 (this is different for UCNs).
1274*404b540aSrobert    FIXME: Would be nice if we didn't need the PFILE argument.  */
1275*404b540aSrobert unsigned char *
cpp_spell_token(cpp_reader * pfile,const cpp_token * token,unsigned char * buffer,bool forstring)1276*404b540aSrobert cpp_spell_token (cpp_reader *pfile, const cpp_token *token,
1277*404b540aSrobert 		 unsigned char *buffer, bool forstring)
1278*404b540aSrobert {
1279*404b540aSrobert   switch (TOKEN_SPELL (token))
1280*404b540aSrobert     {
1281*404b540aSrobert     case SPELL_OPERATOR:
1282*404b540aSrobert       {
1283*404b540aSrobert 	const unsigned char *spelling;
1284*404b540aSrobert 	unsigned char c;
1285*404b540aSrobert 
1286*404b540aSrobert 	if (token->flags & DIGRAPH)
1287*404b540aSrobert 	  spelling
1288*404b540aSrobert 	    = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH];
1289*404b540aSrobert 	else if (token->flags & NAMED_OP)
1290*404b540aSrobert 	  goto spell_ident;
1291*404b540aSrobert 	else
1292*404b540aSrobert 	  spelling = TOKEN_NAME (token);
1293*404b540aSrobert 
1294*404b540aSrobert 	while ((c = *spelling++) != '\0')
1295*404b540aSrobert 	  *buffer++ = c;
1296*404b540aSrobert       }
1297*404b540aSrobert       break;
1298*404b540aSrobert 
1299*404b540aSrobert     spell_ident:
1300*404b540aSrobert     case SPELL_IDENT:
1301*404b540aSrobert       if (forstring)
1302*404b540aSrobert 	{
1303*404b540aSrobert 	  memcpy (buffer, NODE_NAME (token->val.node),
1304*404b540aSrobert 		  NODE_LEN (token->val.node));
1305*404b540aSrobert 	  buffer += NODE_LEN (token->val.node);
1306*404b540aSrobert 	}
1307*404b540aSrobert       else
1308*404b540aSrobert 	{
1309*404b540aSrobert 	  size_t i;
1310*404b540aSrobert 	  const unsigned char * name = NODE_NAME (token->val.node);
1311*404b540aSrobert 
1312*404b540aSrobert 	  for (i = 0; i < NODE_LEN (token->val.node); i++)
1313*404b540aSrobert 	    if (name[i] & ~0x7F)
1314*404b540aSrobert 	      {
1315*404b540aSrobert 		i += utf8_to_ucn (buffer, name + i) - 1;
1316*404b540aSrobert 		buffer += 10;
1317*404b540aSrobert 	      }
1318*404b540aSrobert 	    else
1319*404b540aSrobert 	      *buffer++ = NODE_NAME (token->val.node)[i];
1320*404b540aSrobert 	}
1321*404b540aSrobert       break;
1322*404b540aSrobert 
1323*404b540aSrobert     case SPELL_LITERAL:
1324*404b540aSrobert       memcpy (buffer, token->val.str.text, token->val.str.len);
1325*404b540aSrobert       buffer += token->val.str.len;
1326*404b540aSrobert       break;
1327*404b540aSrobert 
1328*404b540aSrobert     case SPELL_NONE:
1329*404b540aSrobert       cpp_error (pfile, CPP_DL_ICE,
1330*404b540aSrobert 		 "unspellable token %s", TOKEN_NAME (token));
1331*404b540aSrobert       break;
1332*404b540aSrobert     }
1333*404b540aSrobert 
1334*404b540aSrobert   return buffer;
1335*404b540aSrobert }
1336*404b540aSrobert 
1337*404b540aSrobert /* Returns TOKEN spelt as a null-terminated string.  The string is
1338*404b540aSrobert    freed when the reader is destroyed.  Useful for diagnostics.  */
1339*404b540aSrobert unsigned char *
cpp_token_as_text(cpp_reader * pfile,const cpp_token * token)1340*404b540aSrobert cpp_token_as_text (cpp_reader *pfile, const cpp_token *token)
1341*404b540aSrobert {
1342*404b540aSrobert   unsigned int len = cpp_token_len (token) + 1;
1343*404b540aSrobert   unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end;
1344*404b540aSrobert 
1345*404b540aSrobert   end = cpp_spell_token (pfile, token, start, false);
1346*404b540aSrobert   end[0] = '\0';
1347*404b540aSrobert 
1348*404b540aSrobert   return start;
1349*404b540aSrobert }
1350*404b540aSrobert 
1351*404b540aSrobert /* Used by C front ends, which really should move to using
1352*404b540aSrobert    cpp_token_as_text.  */
1353*404b540aSrobert const char *
cpp_type2name(enum cpp_ttype type)1354*404b540aSrobert cpp_type2name (enum cpp_ttype type)
1355*404b540aSrobert {
1356*404b540aSrobert   return (const char *) token_spellings[type].name;
1357*404b540aSrobert }
1358*404b540aSrobert 
1359*404b540aSrobert /* Writes the spelling of token to FP, without any preceding space.
1360*404b540aSrobert    Separated from cpp_spell_token for efficiency - to avoid stdio
1361*404b540aSrobert    double-buffering.  */
1362*404b540aSrobert void
cpp_output_token(const cpp_token * token,FILE * fp)1363*404b540aSrobert cpp_output_token (const cpp_token *token, FILE *fp)
1364*404b540aSrobert {
1365*404b540aSrobert   switch (TOKEN_SPELL (token))
1366*404b540aSrobert     {
1367*404b540aSrobert     case SPELL_OPERATOR:
1368*404b540aSrobert       {
1369*404b540aSrobert 	const unsigned char *spelling;
1370*404b540aSrobert 	int c;
1371*404b540aSrobert 
1372*404b540aSrobert 	if (token->flags & DIGRAPH)
1373*404b540aSrobert 	  spelling
1374*404b540aSrobert 	    = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH];
1375*404b540aSrobert 	else if (token->flags & NAMED_OP)
1376*404b540aSrobert 	  goto spell_ident;
1377*404b540aSrobert 	else
1378*404b540aSrobert 	  spelling = TOKEN_NAME (token);
1379*404b540aSrobert 
1380*404b540aSrobert 	c = *spelling;
1381*404b540aSrobert 	do
1382*404b540aSrobert 	  putc (c, fp);
1383*404b540aSrobert 	while ((c = *++spelling) != '\0');
1384*404b540aSrobert       }
1385*404b540aSrobert       break;
1386*404b540aSrobert 
1387*404b540aSrobert     spell_ident:
1388*404b540aSrobert     case SPELL_IDENT:
1389*404b540aSrobert       {
1390*404b540aSrobert 	size_t i;
1391*404b540aSrobert 	const unsigned char * name = NODE_NAME (token->val.node);
1392*404b540aSrobert 
1393*404b540aSrobert 	for (i = 0; i < NODE_LEN (token->val.node); i++)
1394*404b540aSrobert 	  if (name[i] & ~0x7F)
1395*404b540aSrobert 	    {
1396*404b540aSrobert 	      unsigned char buffer[10];
1397*404b540aSrobert 	      i += utf8_to_ucn (buffer, name + i) - 1;
1398*404b540aSrobert 	      fwrite (buffer, 1, 10, fp);
1399*404b540aSrobert 	    }
1400*404b540aSrobert 	  else
1401*404b540aSrobert 	    fputc (NODE_NAME (token->val.node)[i], fp);
1402*404b540aSrobert       }
1403*404b540aSrobert       break;
1404*404b540aSrobert 
1405*404b540aSrobert     case SPELL_LITERAL:
1406*404b540aSrobert       fwrite (token->val.str.text, 1, token->val.str.len, fp);
1407*404b540aSrobert       break;
1408*404b540aSrobert 
1409*404b540aSrobert     case SPELL_NONE:
1410*404b540aSrobert       /* An error, most probably.  */
1411*404b540aSrobert       break;
1412*404b540aSrobert     }
1413*404b540aSrobert }
1414*404b540aSrobert 
1415*404b540aSrobert /* Compare two tokens.  */
1416*404b540aSrobert int
_cpp_equiv_tokens(const cpp_token * a,const cpp_token * b)1417*404b540aSrobert _cpp_equiv_tokens (const cpp_token *a, const cpp_token *b)
1418*404b540aSrobert {
1419*404b540aSrobert   if (a->type == b->type && a->flags == b->flags)
1420*404b540aSrobert     switch (TOKEN_SPELL (a))
1421*404b540aSrobert       {
1422*404b540aSrobert       default:			/* Keep compiler happy.  */
1423*404b540aSrobert       case SPELL_OPERATOR:
1424*404b540aSrobert 	return 1;
1425*404b540aSrobert       case SPELL_NONE:
1426*404b540aSrobert 	return (a->type != CPP_MACRO_ARG || a->val.arg_no == b->val.arg_no);
1427*404b540aSrobert       case SPELL_IDENT:
1428*404b540aSrobert 	return a->val.node == b->val.node;
1429*404b540aSrobert       case SPELL_LITERAL:
1430*404b540aSrobert 	return (a->val.str.len == b->val.str.len
1431*404b540aSrobert 		&& !memcmp (a->val.str.text, b->val.str.text,
1432*404b540aSrobert 			    a->val.str.len));
1433*404b540aSrobert       }
1434*404b540aSrobert 
1435*404b540aSrobert   return 0;
1436*404b540aSrobert }
1437*404b540aSrobert 
1438*404b540aSrobert /* Returns nonzero if a space should be inserted to avoid an
1439*404b540aSrobert    accidental token paste for output.  For simplicity, it is
1440*404b540aSrobert    conservative, and occasionally advises a space where one is not
1441*404b540aSrobert    needed, e.g. "." and ".2".  */
1442*404b540aSrobert int
cpp_avoid_paste(cpp_reader * pfile,const cpp_token * token1,const cpp_token * token2)1443*404b540aSrobert cpp_avoid_paste (cpp_reader *pfile, const cpp_token *token1,
1444*404b540aSrobert 		 const cpp_token *token2)
1445*404b540aSrobert {
1446*404b540aSrobert   enum cpp_ttype a = token1->type, b = token2->type;
1447*404b540aSrobert   cppchar_t c;
1448*404b540aSrobert 
1449*404b540aSrobert   if (token1->flags & NAMED_OP)
1450*404b540aSrobert     a = CPP_NAME;
1451*404b540aSrobert   if (token2->flags & NAMED_OP)
1452*404b540aSrobert     b = CPP_NAME;
1453*404b540aSrobert 
1454*404b540aSrobert   c = EOF;
1455*404b540aSrobert   if (token2->flags & DIGRAPH)
1456*404b540aSrobert     c = digraph_spellings[(int) b - (int) CPP_FIRST_DIGRAPH][0];
1457*404b540aSrobert   else if (token_spellings[b].category == SPELL_OPERATOR)
1458*404b540aSrobert     c = token_spellings[b].name[0];
1459*404b540aSrobert 
1460*404b540aSrobert   /* Quickly get everything that can paste with an '='.  */
1461*404b540aSrobert   if ((int) a <= (int) CPP_LAST_EQ && c == '=')
1462*404b540aSrobert     return 1;
1463*404b540aSrobert 
1464*404b540aSrobert   switch (a)
1465*404b540aSrobert     {
1466*404b540aSrobert     case CPP_GREATER:	return c == '>';
1467*404b540aSrobert     case CPP_LESS:	return c == '<' || c == '%' || c == ':';
1468*404b540aSrobert     case CPP_PLUS:	return c == '+';
1469*404b540aSrobert     case CPP_MINUS:	return c == '-' || c == '>';
1470*404b540aSrobert     case CPP_DIV:	return c == '/' || c == '*'; /* Comments.  */
1471*404b540aSrobert     case CPP_MOD:	return c == ':' || c == '>';
1472*404b540aSrobert     case CPP_AND:	return c == '&';
1473*404b540aSrobert     case CPP_OR:	return c == '|';
1474*404b540aSrobert     case CPP_COLON:	return c == ':' || c == '>';
1475*404b540aSrobert     case CPP_DEREF:	return c == '*';
1476*404b540aSrobert     case CPP_DOT:	return c == '.' || c == '%' || b == CPP_NUMBER;
1477*404b540aSrobert     case CPP_HASH:	return c == '#' || c == '%'; /* Digraph form.  */
1478*404b540aSrobert     case CPP_NAME:	return ((b == CPP_NUMBER
1479*404b540aSrobert 				 && name_p (pfile, &token2->val.str))
1480*404b540aSrobert 				|| b == CPP_NAME
1481*404b540aSrobert 				|| b == CPP_CHAR || b == CPP_STRING); /* L */
1482*404b540aSrobert     case CPP_NUMBER:	return (b == CPP_NUMBER || b == CPP_NAME
1483*404b540aSrobert 				|| c == '.' || c == '+' || c == '-');
1484*404b540aSrobert 				      /* UCNs */
1485*404b540aSrobert     case CPP_OTHER:	return ((token1->val.str.text[0] == '\\'
1486*404b540aSrobert 				 && b == CPP_NAME)
1487*404b540aSrobert 				|| (CPP_OPTION (pfile, objc)
1488*404b540aSrobert 				    && token1->val.str.text[0] == '@'
1489*404b540aSrobert 				    && (b == CPP_NAME || b == CPP_STRING)));
1490*404b540aSrobert     default:		break;
1491*404b540aSrobert     }
1492*404b540aSrobert 
1493*404b540aSrobert   return 0;
1494*404b540aSrobert }
1495*404b540aSrobert 
1496*404b540aSrobert /* Output all the remaining tokens on the current line, and a newline
1497*404b540aSrobert    character, to FP.  Leading whitespace is removed.  If there are
1498*404b540aSrobert    macros, special token padding is not performed.  */
1499*404b540aSrobert void
cpp_output_line(cpp_reader * pfile,FILE * fp)1500*404b540aSrobert cpp_output_line (cpp_reader *pfile, FILE *fp)
1501*404b540aSrobert {
1502*404b540aSrobert   const cpp_token *token;
1503*404b540aSrobert 
1504*404b540aSrobert   token = cpp_get_token (pfile);
1505*404b540aSrobert   while (token->type != CPP_EOF)
1506*404b540aSrobert     {
1507*404b540aSrobert       cpp_output_token (token, fp);
1508*404b540aSrobert       token = cpp_get_token (pfile);
1509*404b540aSrobert       if (token->flags & PREV_WHITE)
1510*404b540aSrobert 	putc (' ', fp);
1511*404b540aSrobert     }
1512*404b540aSrobert 
1513*404b540aSrobert   putc ('\n', fp);
1514*404b540aSrobert }
1515*404b540aSrobert 
1516*404b540aSrobert /* Memory buffers.  Changing these three constants can have a dramatic
1517*404b540aSrobert    effect on performance.  The values here are reasonable defaults,
1518*404b540aSrobert    but might be tuned.  If you adjust them, be sure to test across a
1519*404b540aSrobert    range of uses of cpplib, including heavy nested function-like macro
1520*404b540aSrobert    expansion.  Also check the change in peak memory usage (NJAMD is a
1521*404b540aSrobert    good tool for this).  */
1522*404b540aSrobert #define MIN_BUFF_SIZE 8000
1523*404b540aSrobert #define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2)
1524*404b540aSrobert #define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \
1525*404b540aSrobert 	(MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2)
1526*404b540aSrobert 
1527*404b540aSrobert #if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0)
1528*404b540aSrobert   #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
1529*404b540aSrobert #endif
1530*404b540aSrobert 
1531*404b540aSrobert /* Create a new allocation buffer.  Place the control block at the end
1532*404b540aSrobert    of the buffer, so that buffer overflows will cause immediate chaos.  */
1533*404b540aSrobert static _cpp_buff *
new_buff(size_t len)1534*404b540aSrobert new_buff (size_t len)
1535*404b540aSrobert {
1536*404b540aSrobert   _cpp_buff *result;
1537*404b540aSrobert   unsigned char *base;
1538*404b540aSrobert 
1539*404b540aSrobert   if (len < MIN_BUFF_SIZE)
1540*404b540aSrobert     len = MIN_BUFF_SIZE;
1541*404b540aSrobert   len = CPP_ALIGN (len);
1542*404b540aSrobert 
1543*404b540aSrobert   base = XNEWVEC (unsigned char, len + sizeof (_cpp_buff));
1544*404b540aSrobert   result = (_cpp_buff *) (base + len);
1545*404b540aSrobert   result->base = base;
1546*404b540aSrobert   result->cur = base;
1547*404b540aSrobert   result->limit = base + len;
1548*404b540aSrobert   result->next = NULL;
1549*404b540aSrobert   return result;
1550*404b540aSrobert }
1551*404b540aSrobert 
1552*404b540aSrobert /* Place a chain of unwanted allocation buffers on the free list.  */
1553*404b540aSrobert void
_cpp_release_buff(cpp_reader * pfile,_cpp_buff * buff)1554*404b540aSrobert _cpp_release_buff (cpp_reader *pfile, _cpp_buff *buff)
1555*404b540aSrobert {
1556*404b540aSrobert   _cpp_buff *end = buff;
1557*404b540aSrobert 
1558*404b540aSrobert   while (end->next)
1559*404b540aSrobert     end = end->next;
1560*404b540aSrobert   end->next = pfile->free_buffs;
1561*404b540aSrobert   pfile->free_buffs = buff;
1562*404b540aSrobert }
1563*404b540aSrobert 
1564*404b540aSrobert /* Return a free buffer of size at least MIN_SIZE.  */
1565*404b540aSrobert _cpp_buff *
_cpp_get_buff(cpp_reader * pfile,size_t min_size)1566*404b540aSrobert _cpp_get_buff (cpp_reader *pfile, size_t min_size)
1567*404b540aSrobert {
1568*404b540aSrobert   _cpp_buff *result, **p;
1569*404b540aSrobert 
1570*404b540aSrobert   for (p = &pfile->free_buffs;; p = &(*p)->next)
1571*404b540aSrobert     {
1572*404b540aSrobert       size_t size;
1573*404b540aSrobert 
1574*404b540aSrobert       if (*p == NULL)
1575*404b540aSrobert 	return new_buff (min_size);
1576*404b540aSrobert       result = *p;
1577*404b540aSrobert       size = result->limit - result->base;
1578*404b540aSrobert       /* Return a buffer that's big enough, but don't waste one that's
1579*404b540aSrobert          way too big.  */
1580*404b540aSrobert       if (size >= min_size && size <= BUFF_SIZE_UPPER_BOUND (min_size))
1581*404b540aSrobert 	break;
1582*404b540aSrobert     }
1583*404b540aSrobert 
1584*404b540aSrobert   *p = result->next;
1585*404b540aSrobert   result->next = NULL;
1586*404b540aSrobert   result->cur = result->base;
1587*404b540aSrobert   return result;
1588*404b540aSrobert }
1589*404b540aSrobert 
1590*404b540aSrobert /* Creates a new buffer with enough space to hold the uncommitted
1591*404b540aSrobert    remaining bytes of BUFF, and at least MIN_EXTRA more bytes.  Copies
1592*404b540aSrobert    the excess bytes to the new buffer.  Chains the new buffer after
1593*404b540aSrobert    BUFF, and returns the new buffer.  */
1594*404b540aSrobert _cpp_buff *
_cpp_append_extend_buff(cpp_reader * pfile,_cpp_buff * buff,size_t min_extra)1595*404b540aSrobert _cpp_append_extend_buff (cpp_reader *pfile, _cpp_buff *buff, size_t min_extra)
1596*404b540aSrobert {
1597*404b540aSrobert   size_t size = EXTENDED_BUFF_SIZE (buff, min_extra);
1598*404b540aSrobert   _cpp_buff *new_buff = _cpp_get_buff (pfile, size);
1599*404b540aSrobert 
1600*404b540aSrobert   buff->next = new_buff;
1601*404b540aSrobert   memcpy (new_buff->base, buff->cur, BUFF_ROOM (buff));
1602*404b540aSrobert   return new_buff;
1603*404b540aSrobert }
1604*404b540aSrobert 
1605*404b540aSrobert /* Creates a new buffer with enough space to hold the uncommitted
1606*404b540aSrobert    remaining bytes of the buffer pointed to by BUFF, and at least
1607*404b540aSrobert    MIN_EXTRA more bytes.  Copies the excess bytes to the new buffer.
1608*404b540aSrobert    Chains the new buffer before the buffer pointed to by BUFF, and
1609*404b540aSrobert    updates the pointer to point to the new buffer.  */
1610*404b540aSrobert void
_cpp_extend_buff(cpp_reader * pfile,_cpp_buff ** pbuff,size_t min_extra)1611*404b540aSrobert _cpp_extend_buff (cpp_reader *pfile, _cpp_buff **pbuff, size_t min_extra)
1612*404b540aSrobert {
1613*404b540aSrobert   _cpp_buff *new_buff, *old_buff = *pbuff;
1614*404b540aSrobert   size_t size = EXTENDED_BUFF_SIZE (old_buff, min_extra);
1615*404b540aSrobert 
1616*404b540aSrobert   new_buff = _cpp_get_buff (pfile, size);
1617*404b540aSrobert   memcpy (new_buff->base, old_buff->cur, BUFF_ROOM (old_buff));
1618*404b540aSrobert   new_buff->next = old_buff;
1619*404b540aSrobert   *pbuff = new_buff;
1620*404b540aSrobert }
1621*404b540aSrobert 
1622*404b540aSrobert /* Free a chain of buffers starting at BUFF.  */
1623*404b540aSrobert void
_cpp_free_buff(_cpp_buff * buff)1624*404b540aSrobert _cpp_free_buff (_cpp_buff *buff)
1625*404b540aSrobert {
1626*404b540aSrobert   _cpp_buff *next;
1627*404b540aSrobert 
1628*404b540aSrobert   for (; buff; buff = next)
1629*404b540aSrobert     {
1630*404b540aSrobert       next = buff->next;
1631*404b540aSrobert       free (buff->base);
1632*404b540aSrobert     }
1633*404b540aSrobert }
1634*404b540aSrobert 
1635*404b540aSrobert /* Allocate permanent, unaligned storage of length LEN.  */
1636*404b540aSrobert unsigned char *
_cpp_unaligned_alloc(cpp_reader * pfile,size_t len)1637*404b540aSrobert _cpp_unaligned_alloc (cpp_reader *pfile, size_t len)
1638*404b540aSrobert {
1639*404b540aSrobert   _cpp_buff *buff = pfile->u_buff;
1640*404b540aSrobert   unsigned char *result = buff->cur;
1641*404b540aSrobert 
1642*404b540aSrobert   if (len > (size_t) (buff->limit - result))
1643*404b540aSrobert     {
1644*404b540aSrobert       buff = _cpp_get_buff (pfile, len);
1645*404b540aSrobert       buff->next = pfile->u_buff;
1646*404b540aSrobert       pfile->u_buff = buff;
1647*404b540aSrobert       result = buff->cur;
1648*404b540aSrobert     }
1649*404b540aSrobert 
1650*404b540aSrobert   buff->cur = result + len;
1651*404b540aSrobert   return result;
1652*404b540aSrobert }
1653*404b540aSrobert 
1654*404b540aSrobert /* Allocate permanent, unaligned storage of length LEN from a_buff.
1655*404b540aSrobert    That buffer is used for growing allocations when saving macro
1656*404b540aSrobert    replacement lists in a #define, and when parsing an answer to an
1657*404b540aSrobert    assertion in #assert, #unassert or #if (and therefore possibly
1658*404b540aSrobert    whilst expanding macros).  It therefore must not be used by any
1659*404b540aSrobert    code that they might call: specifically the lexer and the guts of
1660*404b540aSrobert    the macro expander.
1661*404b540aSrobert 
1662*404b540aSrobert    All existing other uses clearly fit this restriction: storing
1663*404b540aSrobert    registered pragmas during initialization.  */
1664*404b540aSrobert unsigned char *
_cpp_aligned_alloc(cpp_reader * pfile,size_t len)1665*404b540aSrobert _cpp_aligned_alloc (cpp_reader *pfile, size_t len)
1666*404b540aSrobert {
1667*404b540aSrobert   _cpp_buff *buff = pfile->a_buff;
1668*404b540aSrobert   unsigned char *result = buff->cur;
1669*404b540aSrobert 
1670*404b540aSrobert   if (len > (size_t) (buff->limit - result))
1671*404b540aSrobert     {
1672*404b540aSrobert       buff = _cpp_get_buff (pfile, len);
1673*404b540aSrobert       buff->next = pfile->a_buff;
1674*404b540aSrobert       pfile->a_buff = buff;
1675*404b540aSrobert       result = buff->cur;
1676*404b540aSrobert     }
1677*404b540aSrobert 
1678*404b540aSrobert   buff->cur = result + len;
1679*404b540aSrobert   return result;
1680*404b540aSrobert }
1681*404b540aSrobert 
1682*404b540aSrobert /* Say which field of TOK is in use.  */
1683*404b540aSrobert 
1684*404b540aSrobert enum cpp_token_fld_kind
cpp_token_val_index(cpp_token * tok)1685*404b540aSrobert cpp_token_val_index (cpp_token *tok)
1686*404b540aSrobert {
1687*404b540aSrobert   switch (TOKEN_SPELL (tok))
1688*404b540aSrobert     {
1689*404b540aSrobert     case SPELL_IDENT:
1690*404b540aSrobert       return CPP_TOKEN_FLD_NODE;
1691*404b540aSrobert     case SPELL_LITERAL:
1692*404b540aSrobert       return CPP_TOKEN_FLD_STR;
1693*404b540aSrobert     case SPELL_NONE:
1694*404b540aSrobert       if (tok->type == CPP_MACRO_ARG)
1695*404b540aSrobert 	return CPP_TOKEN_FLD_ARG_NO;
1696*404b540aSrobert       else if (tok->type == CPP_PADDING)
1697*404b540aSrobert 	return CPP_TOKEN_FLD_SOURCE;
1698*404b540aSrobert       else if (tok->type == CPP_PRAGMA)
1699*404b540aSrobert 	return CPP_TOKEN_FLD_PRAGMA;
1700*404b540aSrobert       /* else fall through */
1701*404b540aSrobert     default:
1702*404b540aSrobert       return CPP_TOKEN_FLD_NONE;
1703*404b540aSrobert     }
1704*404b540aSrobert }
1705