blob: 62a28f81b8781379ea929c30866cdd6f9b039a08 [file] [log] [blame]
Zack Weinberg45b966d2000-03-13 22:01:08 +00001/* CPP Library - lexical analysis.
Kazu Hirataaa335b72004-02-18 15:05:17 +00002 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
Zack Weinberg45b966d2000-03-13 22:01:08 +00003 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6 Broken out to separate file, Zack Weinberg, Mar 2000
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22#include "config.h"
23#include "system.h"
Zack Weinberg45b966d2000-03-13 22:01:08 +000024#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000025#include "internal.h"
Zack Weinberg45b966d2000-03-13 22:01:08 +000026
Neil Booth93c803682000-10-28 17:59:06 +000027enum spell_type
Zack Weinbergf9a0e962000-07-13 02:32:41 +000028{
Neil Booth93c803682000-10-28 17:59:06 +000029 SPELL_OPERATOR = 0,
Neil Booth93c803682000-10-28 17:59:06 +000030 SPELL_IDENT,
Neil Booth6338b352003-04-23 22:44:06 +000031 SPELL_LITERAL,
Neil Booth93c803682000-10-28 17:59:06 +000032 SPELL_NONE
Zack Weinbergf9a0e962000-07-13 02:32:41 +000033};
34
Neil Booth93c803682000-10-28 17:59:06 +000035struct token_spelling
Zack Weinbergf9a0e962000-07-13 02:32:41 +000036{
Neil Booth93c803682000-10-28 17:59:06 +000037 enum spell_type category;
38 const unsigned char *name;
Zack Weinbergf9a0e962000-07-13 02:32:41 +000039};
40
Zack Weinberg8206c792001-10-11 21:21:57 +000041static const unsigned char *const digraph_spellings[] =
42{ U"%:", U"%:%:", U"<:", U":>", U"<%", U"%>" };
Neil Booth93c803682000-10-28 17:59:06 +000043
Zack Weinberg21b11492004-09-09 19:16:56 +000044#define OP(e, s) { SPELL_OPERATOR, U s },
45#define TK(e, s) { SPELL_ ## s, U #e },
Zack Weinberg8206c792001-10-11 21:21:57 +000046static const struct token_spelling token_spellings[N_TTYPES] = { TTYPE_TABLE };
Neil Booth93c803682000-10-28 17:59:06 +000047#undef OP
48#undef TK
49
50#define TOKEN_SPELL(token) (token_spellings[(token)->type].category)
51#define TOKEN_NAME(token) (token_spellings[(token)->type].name)
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +000052
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000053static void add_line_note (cpp_buffer *, const uchar *, unsigned int);
54static int skip_line_comment (cpp_reader *);
55static void skip_whitespace (cpp_reader *, cppchar_t);
56static cpp_hashnode *lex_identifier (cpp_reader *, const uchar *);
57static void lex_number (cpp_reader *, cpp_string *);
58static bool forms_identifier_p (cpp_reader *, int);
59static void lex_string (cpp_reader *, cpp_token *, const uchar *);
60static void save_comment (cpp_reader *, cpp_token *, const uchar *, cppchar_t);
61static void create_literal (cpp_reader *, cpp_token *, const uchar *,
62 unsigned int, enum cpp_ttype);
63static bool warn_in_comment (cpp_reader *, _cpp_line_note *);
64static int name_p (cpp_reader *, const cpp_string *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000065static tokenrun *next_tokenrun (tokenrun *);
Neil Booth0d9f2342000-09-18 18:43:05 +000066
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000067static _cpp_buff *new_buff (size_t);
Zack Weinberg15dad1d2000-05-18 15:55:46 +000068
Neil Booth9d10c9a2003-03-06 23:12:30 +000069
Zack Weinberg6d2c2042000-04-30 17:30:25 +000070/* Utility routine:
Zack Weinberg6d2c2042000-04-30 17:30:25 +000071
Zack Weinbergbfb9dc72000-07-08 19:00:39 +000072 Compares, the token TOKEN to the NUL-terminated string STRING.
73 TOKEN must be a CPP_NAME. Returns 1 for equal, 0 for unequal. */
Zack Weinberg6d2c2042000-04-30 17:30:25 +000074int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000075cpp_ideq (const cpp_token *token, const char *string)
Zack Weinberg6d2c2042000-04-30 17:30:25 +000076{
Zack Weinbergbfb9dc72000-07-08 19:00:39 +000077 if (token->type != CPP_NAME)
Zack Weinberg6d2c2042000-04-30 17:30:25 +000078 return 0;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +000079
Neil Booth562a5c22002-04-21 18:46:42 +000080 return !ustrcmp (NODE_NAME (token->val.node), (const uchar *) string);
Zack Weinberg6d2c2042000-04-30 17:30:25 +000081}
82
Neil Booth26aea072003-04-19 00:22:51 +000083/* Record a note TYPE at byte POS into the current cleaned logical
84 line. */
Neil Booth87062812001-10-20 09:00:53 +000085static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000086add_line_note (cpp_buffer *buffer, const uchar *pos, unsigned int type)
Neil Booth0d9f2342000-09-18 18:43:05 +000087{
Neil Booth26aea072003-04-19 00:22:51 +000088 if (buffer->notes_used == buffer->notes_cap)
Zack Weinbergc5a04732000-04-25 19:32:36 +000089 {
Neil Booth26aea072003-04-19 00:22:51 +000090 buffer->notes_cap = buffer->notes_cap * 2 + 200;
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +000091 buffer->notes = xrealloc (buffer->notes,
92 buffer->notes_cap * sizeof (_cpp_line_note));
Zack Weinbergc5a04732000-04-25 19:32:36 +000093 }
Neil Booth0d9f2342000-09-18 18:43:05 +000094
Neil Booth26aea072003-04-19 00:22:51 +000095 buffer->notes[buffer->notes_used].pos = pos;
96 buffer->notes[buffer->notes_used].type = type;
97 buffer->notes_used++;
Zack Weinbergc5a04732000-04-25 19:32:36 +000098}
99
Neil Booth26aea072003-04-19 00:22:51 +0000100/* Returns with a logical line that contains no escaped newlines or
101 trigraphs. This is a time-critical inner loop. */
102void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000103_cpp_clean_line (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000104{
Neil Booth26aea072003-04-19 00:22:51 +0000105 cpp_buffer *buffer;
106 const uchar *s;
107 uchar c, *d, *p;
Neil Booth29401c32001-08-22 20:37:20 +0000108
Neil Booth26aea072003-04-19 00:22:51 +0000109 buffer = pfile->buffer;
110 buffer->cur_note = buffer->notes_used = 0;
111 buffer->cur = buffer->line_base = buffer->next_line;
112 buffer->need_line = false;
113 s = buffer->next_line - 1;
114
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000115 if (!buffer->from_stage3)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000116 {
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000117 /* Short circuit for the common case of an un-escaped line with
118 no trigraphs. The primary win here is by not writing any
119 data back to memory until we have to. */
120 for (;;)
121 {
122 c = *++s;
123 if (c == '\n' || c == '\r')
124 {
125 d = (uchar *) s;
126
127 if (s == buffer->rlimit)
128 goto done;
129
130 /* DOS line ending? */
131 if (c == '\r' && s[1] == '\n')
132 s++;
133
134 if (s == buffer->rlimit)
135 goto done;
136
137 /* check for escaped newline */
138 p = d;
139 while (p != buffer->next_line && is_nvspace (p[-1]))
140 p--;
141 if (p == buffer->next_line || p[-1] != '\\')
142 goto done;
143
144 /* Have an escaped newline; process it and proceed to
145 the slow path. */
146 add_line_note (buffer, p - 1, p != d ? ' ' : '\\');
147 d = p - 2;
148 buffer->next_line = p - 1;
149 break;
150 }
151 if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
152 {
153 /* Have a trigraph. We may or may not have to convert
154 it. Add a line note regardless, for -Wtrigraphs. */
155 add_line_note (buffer, s, s[2]);
156 if (CPP_OPTION (pfile, trigraphs))
157 {
158 /* We do, and that means we have to switch to the
159 slow path. */
160 d = (uchar *) s;
161 *d = _cpp_trigraph_map[s[2]];
162 s += 2;
163 break;
164 }
165 }
166 }
167
Zack Weinbergc5a04732000-04-25 19:32:36 +0000168
Neil Booth26aea072003-04-19 00:22:51 +0000169 for (;;)
Neil Booth0d9f2342000-09-18 18:43:05 +0000170 {
Neil Booth26aea072003-04-19 00:22:51 +0000171 c = *++s;
172 *++d = c;
173
174 if (c == '\n' || c == '\r')
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000175 {
Neil Booth26aea072003-04-19 00:22:51 +0000176 /* Handle DOS line endings. */
177 if (c == '\r' && s != buffer->rlimit && s[1] == '\n')
178 s++;
179 if (s == buffer->rlimit)
Neil Booth87062812001-10-20 09:00:53 +0000180 break;
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000181
Neil Booth26aea072003-04-19 00:22:51 +0000182 /* Escaped? */
183 p = d;
184 while (p != buffer->next_line && is_nvspace (p[-1]))
185 p--;
186 if (p == buffer->next_line || p[-1] != '\\')
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000187 break;
Neil Booth26aea072003-04-19 00:22:51 +0000188
Neil Booth41c32c92003-04-20 19:02:53 +0000189 add_line_note (buffer, p - 1, p != d ? ' ': '\\');
Neil Booth26aea072003-04-19 00:22:51 +0000190 d = p - 2;
191 buffer->next_line = p - 1;
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000192 }
Neil Booth26aea072003-04-19 00:22:51 +0000193 else if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000194 {
Neil Booth26aea072003-04-19 00:22:51 +0000195 /* Add a note regardless, for the benefit of -Wtrigraphs. */
Neil Booth41c32c92003-04-20 19:02:53 +0000196 add_line_note (buffer, d, s[2]);
Neil Booth26aea072003-04-19 00:22:51 +0000197 if (CPP_OPTION (pfile, trigraphs))
198 {
199 *d = _cpp_trigraph_map[s[2]];
200 s += 2;
201 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000202 }
Neil Booth0d9f2342000-09-18 18:43:05 +0000203 }
Neil Booth26aea072003-04-19 00:22:51 +0000204 }
205 else
206 {
207 do
208 s++;
209 while (*s != '\n' && *s != '\r');
210 d = (uchar *) s;
211
212 /* Handle DOS line endings. */
213 if (*s == '\r' && s != buffer->rlimit && s[1] == '\n')
214 s++;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000215 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000216
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000217 done:
Neil Booth26aea072003-04-19 00:22:51 +0000218 *d = '\n';
Neil Booth41c32c92003-04-20 19:02:53 +0000219 /* A sentinel note that should never be processed. */
220 add_line_note (buffer, d + 1, '\n');
Neil Booth26aea072003-04-19 00:22:51 +0000221 buffer->next_line = s + 1;
222}
223
Neil Bootha8eb6042003-05-04 20:03:55 +0000224/* Return true if the trigraph indicated by NOTE should be warned
225 about in a comment. */
226static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000227warn_in_comment (cpp_reader *pfile, _cpp_line_note *note)
Neil Bootha8eb6042003-05-04 20:03:55 +0000228{
229 const uchar *p;
230
231 /* Within comments we don't warn about trigraphs, unless the
232 trigraph forms an escaped newline, as that may change
Kazu Hirata6356f892003-06-12 19:01:08 +0000233 behavior. */
Neil Bootha8eb6042003-05-04 20:03:55 +0000234 if (note->type != '/')
235 return false;
236
237 /* If -trigraphs, then this was an escaped newline iff the next note
238 is coincident. */
239 if (CPP_OPTION (pfile, trigraphs))
240 return note[1].pos == note->pos;
241
242 /* Otherwise, see if this forms an escaped newline. */
243 p = note->pos + 3;
244 while (is_nvspace (*p))
245 p++;
246
247 /* There might have been escaped newlines between the trigraph and the
248 newline we found. Hence the position test. */
249 return (*p == '\n' && p < note[1].pos);
250}
251
Neil Booth26aea072003-04-19 00:22:51 +0000252/* Process the notes created by add_line_note as far as the current
253 location. */
254void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000255_cpp_process_line_notes (cpp_reader *pfile, int in_comment)
Neil Booth26aea072003-04-19 00:22:51 +0000256{
257 cpp_buffer *buffer = pfile->buffer;
258
259 for (;;)
260 {
261 _cpp_line_note *note = &buffer->notes[buffer->cur_note];
262 unsigned int col;
263
264 if (note->pos > buffer->cur)
265 break;
266
267 buffer->cur_note++;
268 col = CPP_BUF_COLUMN (buffer, note->pos + 1);
269
Neil Booth41c32c92003-04-20 19:02:53 +0000270 if (note->type == '\\' || note->type == ' ')
Neil Booth26aea072003-04-19 00:22:51 +0000271 {
Neil Booth41c32c92003-04-20 19:02:53 +0000272 if (note->type == ' ' && !in_comment)
Per Bothner500bee02004-04-22 19:22:27 -0700273 cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
Neil Booth26aea072003-04-19 00:22:51 +0000274 "backslash and newline separated by space");
Neil Booth41c32c92003-04-20 19:02:53 +0000275
Neil Booth26aea072003-04-19 00:22:51 +0000276 if (buffer->next_line > buffer->rlimit)
277 {
Per Bothner500bee02004-04-22 19:22:27 -0700278 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, col,
Neil Booth26aea072003-04-19 00:22:51 +0000279 "backslash-newline at end of file");
280 /* Prevent "no newline at end of file" warning. */
281 buffer->next_line = buffer->rlimit;
282 }
283
284 buffer->line_base = note->pos;
Per Bothner12f9df42004-02-11 07:29:30 -0800285 CPP_INCREMENT_LINE (pfile, 0);
Neil Booth26aea072003-04-19 00:22:51 +0000286 }
Neil Booth41c32c92003-04-20 19:02:53 +0000287 else if (_cpp_trigraph_map[note->type])
288 {
Neil Bootha8eb6042003-05-04 20:03:55 +0000289 if (CPP_OPTION (pfile, warn_trigraphs)
290 && (!in_comment || warn_in_comment (pfile, note)))
Neil Booth41c32c92003-04-20 19:02:53 +0000291 {
292 if (CPP_OPTION (pfile, trigraphs))
Per Bothner500bee02004-04-22 19:22:27 -0700293 cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
Neil Booth41c32c92003-04-20 19:02:53 +0000294 "trigraph ??%c converted to %c",
295 note->type,
296 (int) _cpp_trigraph_map[note->type]);
297 else
Geoffrey Keating905bd7b2003-07-22 02:21:16 +0000298 {
299 cpp_error_with_line
Per Bothner500bee02004-04-22 19:22:27 -0700300 (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
Geoffrey Keating905bd7b2003-07-22 02:21:16 +0000301 "trigraph ??%c ignored, use -trigraphs to enable",
302 note->type);
303 }
Neil Booth41c32c92003-04-20 19:02:53 +0000304 }
305 }
306 else
307 abort ();
Neil Booth26aea072003-04-19 00:22:51 +0000308 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000309}
310
Neil Booth0d9f2342000-09-18 18:43:05 +0000311/* Skip a C-style block comment. We find the end of the comment by
312 seeing if an asterisk is before every '/' we encounter. Returns
Neil Booth6f572ac2003-04-19 16:34:33 +0000313 nonzero if comment terminated by EOF, zero otherwise.
314
315 Buffer->cur points to the initial asterisk of the comment. */
Neil Booth26aea072003-04-19 00:22:51 +0000316bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000317_cpp_skip_block_comment (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000318{
319 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000320 const uchar *cur = buffer->cur;
321 uchar c;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000322
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000323 cur++;
324 if (*cur == '/')
325 cur++;
Neil Booth26aea072003-04-19 00:22:51 +0000326
327 for (;;)
Neil Booth0d9f2342000-09-18 18:43:05 +0000328 {
Neil Booth0d9f2342000-09-18 18:43:05 +0000329 /* People like decorating comments with '*', so check for '/'
330 instead for efficiency. */
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000331 c = *cur++;
332
Zack Weinbergc5a04732000-04-25 19:32:36 +0000333 if (c == '/')
334 {
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000335 if (cur[-2] == '*')
Neil Booth0d9f2342000-09-18 18:43:05 +0000336 break;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000337
Neil Booth0d9f2342000-09-18 18:43:05 +0000338 /* Warn about potential nested comments, but not if the '/'
Joseph Myersa1f300c2001-11-23 02:05:19 +0000339 comes immediately before the true comment delimiter.
Zack Weinbergc5a04732000-04-25 19:32:36 +0000340 Don't bother to get it right across escaped newlines. */
Neil Booth0d9f2342000-09-18 18:43:05 +0000341 if (CPP_OPTION (pfile, warn_comments)
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000342 && cur[0] == '*' && cur[1] != '/')
343 {
344 buffer->cur = cur;
John David Anglin0527bc42003-11-01 22:56:54 +0000345 cpp_error_with_line (pfile, CPP_DL_WARNING,
Per Bothner500bee02004-04-22 19:22:27 -0700346 pfile->line_table->highest_line, CPP_BUF_COL (buffer),
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000347 "\"/*\" within comment");
348 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000349 }
Neil Booth26aea072003-04-19 00:22:51 +0000350 else if (c == '\n')
351 {
Per Bothner12f9df42004-02-11 07:29:30 -0800352 unsigned int cols;
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000353 buffer->cur = cur - 1;
Neil Booth26aea072003-04-19 00:22:51 +0000354 _cpp_process_line_notes (pfile, true);
355 if (buffer->next_line >= buffer->rlimit)
356 return true;
357 _cpp_clean_line (pfile);
Per Bothner12f9df42004-02-11 07:29:30 -0800358
359 cols = buffer->next_line - buffer->line_base;
360 CPP_INCREMENT_LINE (pfile, cols);
361
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000362 cur = buffer->cur;
Neil Booth26aea072003-04-19 00:22:51 +0000363 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000364 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000365
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000366 buffer->cur = cur;
Neil Bootha8eb6042003-05-04 20:03:55 +0000367 _cpp_process_line_notes (pfile, true);
Neil Booth26aea072003-04-19 00:22:51 +0000368 return false;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000369}
370
Neil Booth480709c2001-10-21 14:04:42 +0000371/* Skip a C++ line comment, leaving buffer->cur pointing to the
Kazu Hiratada7d8302002-09-22 02:03:17 +0000372 terminating newline. Handles escaped newlines. Returns nonzero
Neil Booth480709c2001-10-21 14:04:42 +0000373 if a multiline comment. */
Zack Weinbergc5a04732000-04-25 19:32:36 +0000374static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000375skip_line_comment (cpp_reader *pfile)
Neil Booth0d9f2342000-09-18 18:43:05 +0000376{
Neil Boothcbcff6d2000-09-23 21:41:41 +0000377 cpp_buffer *buffer = pfile->buffer;
Per Bothner500bee02004-04-22 19:22:27 -0700378 unsigned int orig_line = pfile->line_table->highest_line;
Neil Booth0d9f2342000-09-18 18:43:05 +0000379
Neil Booth26aea072003-04-19 00:22:51 +0000380 while (*buffer->cur != '\n')
381 buffer->cur++;
Neil Booth0d9f2342000-09-18 18:43:05 +0000382
Neil Booth26aea072003-04-19 00:22:51 +0000383 _cpp_process_line_notes (pfile, true);
Per Bothner500bee02004-04-22 19:22:27 -0700384 return orig_line != pfile->line_table->highest_line;
Neil Booth0d9f2342000-09-18 18:43:05 +0000385}
386
Neil Booth26aea072003-04-19 00:22:51 +0000387/* Skips whitespace, saving the next non-whitespace character. */
Neil Booth0d9f2342000-09-18 18:43:05 +0000388static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000389skip_whitespace (cpp_reader *pfile, cppchar_t c)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000390{
391 cpp_buffer *buffer = pfile->buffer;
Neil Boothf7d151f2003-04-19 07:41:15 +0000392 bool saw_NUL = false;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000393
Neil Booth0d9f2342000-09-18 18:43:05 +0000394 do
Zack Weinbergc5a04732000-04-25 19:32:36 +0000395 {
Neil Booth91fcd152000-07-09 09:19:44 +0000396 /* Horizontal space always OK. */
Neil Booth26aea072003-04-19 00:22:51 +0000397 if (c == ' ' || c == '\t')
Neil Booth0d9f2342000-09-18 18:43:05 +0000398 ;
Neil Booth0d9f2342000-09-18 18:43:05 +0000399 /* Just \f \v or \0 left. */
Neil Booth91fcd152000-07-09 09:19:44 +0000400 else if (c == '\0')
Neil Boothf7d151f2003-04-19 07:41:15 +0000401 saw_NUL = true;
Neil Booth93c803682000-10-28 17:59:06 +0000402 else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
Per Bothner500bee02004-04-22 19:22:27 -0700403 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line,
Neil Boothebef4e82002-04-14 18:42:47 +0000404 CPP_BUF_COL (buffer),
405 "%s in preprocessing directive",
406 c == '\f' ? "form feed" : "vertical tab");
Zack Weinbergc5a04732000-04-25 19:32:36 +0000407
Neil Booth0d9f2342000-09-18 18:43:05 +0000408 c = *buffer->cur++;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000409 }
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000410 /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
Neil Booth0d9f2342000-09-18 18:43:05 +0000411 while (is_nvspace (c));
Zack Weinbergc5a04732000-04-25 19:32:36 +0000412
Neil Boothf7d151f2003-04-19 07:41:15 +0000413 if (saw_NUL)
John David Anglin0527bc42003-11-01 22:56:54 +0000414 cpp_error (pfile, CPP_DL_WARNING, "null character(s) ignored");
Neil Boothf7d151f2003-04-19 07:41:15 +0000415
Neil Booth480709c2001-10-21 14:04:42 +0000416 buffer->cur--;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000417}
418
Neil Booth93c803682000-10-28 17:59:06 +0000419/* See if the characters of a number token are valid in a name (no
420 '.', '+' or '-'). */
421static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000422name_p (cpp_reader *pfile, const cpp_string *string)
Neil Booth93c803682000-10-28 17:59:06 +0000423{
424 unsigned int i;
425
426 for (i = 0; i < string->len; i++)
427 if (!is_idchar (string->text[i]))
428 return 0;
429
Kazu Hiratadf383482002-05-22 22:02:16 +0000430 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000431}
432
Neil Boothbced6ed2003-04-19 11:59:44 +0000433/* Returns TRUE if the sequence starting at buffer->cur is invalid in
Neil Booth1613e522003-04-20 07:29:23 +0000434 an identifier. FIRST is TRUE if this starts an identifier. */
Neil Boothbced6ed2003-04-19 11:59:44 +0000435static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000436forms_identifier_p (cpp_reader *pfile, int first)
Neil Boothbced6ed2003-04-19 11:59:44 +0000437{
Neil Booth1613e522003-04-20 07:29:23 +0000438 cpp_buffer *buffer = pfile->buffer;
Neil Boothbced6ed2003-04-19 11:59:44 +0000439
Neil Booth1613e522003-04-20 07:29:23 +0000440 if (*buffer->cur == '$')
Neil Boothbced6ed2003-04-19 11:59:44 +0000441 {
Neil Booth1613e522003-04-20 07:29:23 +0000442 if (!CPP_OPTION (pfile, dollars_in_ident))
443 return false;
Neil Boothbced6ed2003-04-19 11:59:44 +0000444
Neil Booth1613e522003-04-20 07:29:23 +0000445 buffer->cur++;
Hans-Peter Nilsson78b88112003-06-12 06:09:15 +0000446 if (CPP_OPTION (pfile, warn_dollars) && !pfile->state.skipping)
Neil Booth1613e522003-04-20 07:29:23 +0000447 {
Hans-Peter Nilsson78b88112003-06-12 06:09:15 +0000448 CPP_OPTION (pfile, warn_dollars) = 0;
John David Anglin0527bc42003-11-01 22:56:54 +0000449 cpp_error (pfile, CPP_DL_PEDWARN, "'$' in identifier or number");
Neil Booth1613e522003-04-20 07:29:23 +0000450 }
451
452 return true;
453 }
454
455 /* Is this a syntactically valid UCN? */
456 if (0 && *buffer->cur == '\\'
457 && (buffer->cur[1] == 'u' || buffer->cur[1] == 'U'))
458 {
459 buffer->cur += 2;
Zack Weinberge6cc3a22003-07-05 00:24:00 +0000460 if (_cpp_valid_ucn (pfile, &buffer->cur, buffer->rlimit, 1 + !first))
Neil Booth1613e522003-04-20 07:29:23 +0000461 return true;
462 buffer->cur -= 2;
463 }
464
465 return false;
Neil Boothbced6ed2003-04-19 11:59:44 +0000466}
467
468/* Lex an identifier starting at BUFFER->CUR - 1. */
Neil Booth0d9f2342000-09-18 18:43:05 +0000469static cpp_hashnode *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000470lex_identifier (cpp_reader *pfile, const uchar *base)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000471{
Neil Booth93c803682000-10-28 17:59:06 +0000472 cpp_hashnode *result;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000473 const uchar *cur, *limit;
474 unsigned int len;
475 unsigned int hash = HT_HASHSTEP (0, *base);
Zack Weinbergc5a04732000-04-25 19:32:36 +0000476
Zack Weinbergc6e83802004-06-05 20:58:06 +0000477 cur = pfile->buffer->cur;
478 for (;;)
Neil Booth10cf9bd2002-03-22 07:23:21 +0000479 {
Neil Boothbced6ed2003-04-19 11:59:44 +0000480 /* N.B. ISIDNUM does not include $. */
481 while (ISIDNUM (*cur))
Zack Weinbergc6e83802004-06-05 20:58:06 +0000482 {
483 hash = HT_HASHSTEP (hash, *cur);
484 cur++;
485 }
Neil Boothbced6ed2003-04-19 11:59:44 +0000486
Neil Booth10cf9bd2002-03-22 07:23:21 +0000487 pfile->buffer->cur = cur;
Zack Weinbergc6e83802004-06-05 20:58:06 +0000488 if (!forms_identifier_p (pfile, false))
489 break;
490
491 limit = pfile->buffer->cur;
492 while (cur < limit)
493 {
494 hash = HT_HASHSTEP (hash, *cur);
495 cur++;
496 }
Zack Weinberg2c3fcba2001-09-10 22:34:03 +0000497 }
Zack Weinbergc6e83802004-06-05 20:58:06 +0000498 len = cur - base;
499 hash = HT_HASHFINISH (hash, len);
Zack Weinberg2c3fcba2001-09-10 22:34:03 +0000500
Neil Boothbced6ed2003-04-19 11:59:44 +0000501 result = (cpp_hashnode *)
Zack Weinbergc6e83802004-06-05 20:58:06 +0000502 ht_lookup_with_hash (pfile->hash_table, base, len, hash, HT_ALLOC);
Neil Boothbced6ed2003-04-19 11:59:44 +0000503
504 /* Rarely, identifiers require diagnostics when lexed. */
Zack Weinberg2c3fcba2001-09-10 22:34:03 +0000505 if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
506 && !pfile->state.skipping, 0))
507 {
508 /* It is allowed to poison the same identifier twice. */
509 if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
John David Anglin0527bc42003-11-01 22:56:54 +0000510 cpp_error (pfile, CPP_DL_ERROR, "attempt to use poisoned \"%s\"",
Zack Weinberg2c3fcba2001-09-10 22:34:03 +0000511 NODE_NAME (result));
512
513 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
514 replacement list of a variadic macro. */
515 if (result == pfile->spec_nodes.n__VA_ARGS__
516 && !pfile->state.va_args_ok)
John David Anglin0527bc42003-11-01 22:56:54 +0000517 cpp_error (pfile, CPP_DL_PEDWARN,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000518 "__VA_ARGS__ can only appear in the expansion"
519 " of a C99 variadic macro");
Zack Weinberg2c3fcba2001-09-10 22:34:03 +0000520 }
521
522 return result;
523}
524
Neil Boothbced6ed2003-04-19 11:59:44 +0000525/* Lex a number to NUMBER starting at BUFFER->CUR - 1. */
Zack Weinbergc5a04732000-04-25 19:32:36 +0000526static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000527lex_number (cpp_reader *pfile, cpp_string *number)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000528{
Neil Booth562a5c22002-04-21 18:46:42 +0000529 const uchar *cur;
Neil Boothbced6ed2003-04-19 11:59:44 +0000530 const uchar *base;
531 uchar *dest;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000532
Neil Boothbced6ed2003-04-19 11:59:44 +0000533 base = pfile->buffer->cur - 1;
534 do
Neil Booth93c803682000-10-28 17:59:06 +0000535 {
Neil Boothbced6ed2003-04-19 11:59:44 +0000536 cur = pfile->buffer->cur;
Neil Booth10cf9bd2002-03-22 07:23:21 +0000537
Neil Boothbced6ed2003-04-19 11:59:44 +0000538 /* N.B. ISIDNUM does not include $. */
539 while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1]))
540 cur++;
Neil Booth10cf9bd2002-03-22 07:23:21 +0000541
Neil Booth10cf9bd2002-03-22 07:23:21 +0000542 pfile->buffer->cur = cur;
Neil Booth93c803682000-10-28 17:59:06 +0000543 }
Neil Booth1613e522003-04-20 07:29:23 +0000544 while (forms_identifier_p (pfile, false));
Neil Boothbced6ed2003-04-19 11:59:44 +0000545
546 number->len = cur - base;
547 dest = _cpp_unaligned_alloc (pfile, number->len + 1);
548 memcpy (dest, base, number->len);
549 dest[number->len] = '\0';
550 number->text = dest;
Neil Booth0d9f2342000-09-18 18:43:05 +0000551}
552
Neil Booth6338b352003-04-23 22:44:06 +0000553/* Create a token of type TYPE with a literal spelling. */
Zack Weinbergc5a04732000-04-25 19:32:36 +0000554static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000555create_literal (cpp_reader *pfile, cpp_token *token, const uchar *base,
556 unsigned int len, enum cpp_ttype type)
Neil Booth0d9f2342000-09-18 18:43:05 +0000557{
Neil Booth6338b352003-04-23 22:44:06 +0000558 uchar *dest = _cpp_unaligned_alloc (pfile, len + 1);
Neil Booth0d9f2342000-09-18 18:43:05 +0000559
Neil Booth6338b352003-04-23 22:44:06 +0000560 memcpy (dest, base, len);
561 dest[len] = '\0';
562 token->type = type;
563 token->val.str.len = len;
564 token->val.str.text = dest;
565}
566
567/* Lexes a string, character constant, or angle-bracketed header file
568 name. The stored string contains the spelling, including opening
569 quote and leading any leading 'L'. It returns the type of the
570 literal, or CPP_OTHER if it was not properly terminated.
571
572 The spelling is NUL-terminated, but it is not guaranteed that this
573 is the first NUL since embedded NULs are preserved. */
574static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000575lex_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
Neil Booth6338b352003-04-23 22:44:06 +0000576{
577 bool saw_NUL = false;
578 const uchar *cur;
579 cppchar_t terminator;
580 enum cpp_ttype type;
581
582 cur = base;
583 terminator = *cur++;
584 if (terminator == 'L')
585 terminator = *cur++;
586 if (terminator == '\"')
587 type = *base == 'L' ? CPP_WSTRING: CPP_STRING;
588 else if (terminator == '\'')
589 type = *base == 'L' ? CPP_WCHAR: CPP_CHAR;
590 else
591 terminator = '>', type = CPP_HEADER_NAME;
Neil Booth93c803682000-10-28 17:59:06 +0000592
Neil Booth0d9f2342000-09-18 18:43:05 +0000593 for (;;)
594 {
Neil Booth6338b352003-04-23 22:44:06 +0000595 cppchar_t c = *cur++;
Neil Booth7868b4a2001-03-04 12:02:02 +0000596
Neil Booth6f572ac2003-04-19 16:34:33 +0000597 /* In #include-style directives, terminators are not escapable. */
Neil Booth6338b352003-04-23 22:44:06 +0000598 if (c == '\\' && !pfile->state.angled_headers && *cur != '\n')
599 cur++;
600 else if (c == terminator)
Neil Boothbced6ed2003-04-19 11:59:44 +0000601 break;
Neil Booth6338b352003-04-23 22:44:06 +0000602 else if (c == '\n')
Neil Booth0d9f2342000-09-18 18:43:05 +0000603 {
Neil Booth6338b352003-04-23 22:44:06 +0000604 cur--;
605 type = CPP_OTHER;
606 break;
Neil Booth0d9f2342000-09-18 18:43:05 +0000607 }
Neil Booth6338b352003-04-23 22:44:06 +0000608 else if (c == '\0')
609 saw_NUL = true;
Neil Booth0d9f2342000-09-18 18:43:05 +0000610 }
611
Neil Booth6338b352003-04-23 22:44:06 +0000612 if (saw_NUL && !pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +0000613 cpp_error (pfile, CPP_DL_WARNING,
614 "null character(s) preserved in literal");
Neil Booth0d9f2342000-09-18 18:43:05 +0000615
Neil Booth6338b352003-04-23 22:44:06 +0000616 pfile->buffer->cur = cur;
617 create_literal (pfile, token, base, cur - base, type);
Neil Booth0d9f2342000-09-18 18:43:05 +0000618}
619
Neil Booth93c803682000-10-28 17:59:06 +0000620/* The stored comment includes the comment start and any terminator. */
Neil Booth0d9f2342000-09-18 18:43:05 +0000621static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000622save_comment (cpp_reader *pfile, cpp_token *token, const unsigned char *from,
623 cppchar_t type)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000624{
Neil Booth5d7ee2f2000-05-10 09:39:18 +0000625 unsigned char *buffer;
Jason Thorpe477cdac2002-04-07 03:12:23 +0000626 unsigned int len, clen;
Kazu Hiratadf383482002-05-22 22:02:16 +0000627
Neil Booth1c6d33e2000-09-25 22:39:51 +0000628 len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */
Neil Booth480709c2001-10-21 14:04:42 +0000629
Neil Booth35422032000-10-29 09:56:00 +0000630 /* C++ comments probably (not definitely) have moved past a new
631 line, which we don't want to save in the comment. */
Neil Booth480709c2001-10-21 14:04:42 +0000632 if (is_vspace (pfile->buffer->cur[-1]))
Neil Booth35422032000-10-29 09:56:00 +0000633 len--;
Jason Thorpe477cdac2002-04-07 03:12:23 +0000634
635 /* If we are currently in a directive, then we need to store all
636 C++ comments as C comments internally, and so we need to
637 allocate a little extra space in that case.
638
639 Note that the only time we encounter a directive here is
640 when we are saving comments in a "#define". */
641 clen = (pfile->state.in_directive && type == '/') ? len + 2 : len;
642
643 buffer = _cpp_unaligned_alloc (pfile, clen);
Kazu Hiratadf383482002-05-22 22:02:16 +0000644
Neil Booth0d9f2342000-09-18 18:43:05 +0000645 token->type = CPP_COMMENT;
Jason Thorpe477cdac2002-04-07 03:12:23 +0000646 token->val.str.len = clen;
Neil Booth0d9f2342000-09-18 18:43:05 +0000647 token->val.str.text = buffer;
Neil Boothd1d9a6b2000-05-27 23:19:56 +0000648
Neil Booth1c6d33e2000-09-25 22:39:51 +0000649 buffer[0] = '/';
650 memcpy (buffer + 1, from, len - 1);
Jason Thorpe477cdac2002-04-07 03:12:23 +0000651
Kazu Hirata1eeeb6a2002-04-30 20:48:55 +0000652 /* Finish conversion to a C comment, if necessary. */
Jason Thorpe477cdac2002-04-07 03:12:23 +0000653 if (pfile->state.in_directive && type == '/')
654 {
655 buffer[1] = '*';
656 buffer[clen - 2] = '*';
657 buffer[clen - 1] = '/';
658 }
Neil Booth0d9f2342000-09-18 18:43:05 +0000659}
660
Neil Booth5fddcff2001-09-11 07:00:12 +0000661/* Allocate COUNT tokens for RUN. */
662void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000663_cpp_init_tokenrun (tokenrun *run, unsigned int count)
Neil Booth5fddcff2001-09-11 07:00:12 +0000664{
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +0200665 run->base = XNEWVEC (cpp_token, count);
Neil Booth5fddcff2001-09-11 07:00:12 +0000666 run->limit = run->base + count;
667 run->next = NULL;
668}
669
670/* Returns the next tokenrun, or creates one if there is none. */
671static tokenrun *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000672next_tokenrun (tokenrun *run)
Neil Booth5fddcff2001-09-11 07:00:12 +0000673{
674 if (run->next == NULL)
675 {
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +0200676 run->next = XNEW (tokenrun);
Neil Boothbdcbe492001-09-13 20:05:17 +0000677 run->next->prev = run;
Neil Booth5fddcff2001-09-11 07:00:12 +0000678 _cpp_init_tokenrun (run->next, 250);
679 }
680
681 return run->next;
682}
683
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000684/* Allocate a single token that is invalidated at the same time as the
685 rest of the tokens on the line. Has its line and col set to the
686 same as the last lexed token, so that diagnostics appear in the
687 right place. */
688cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000689_cpp_temp_token (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000690{
691 cpp_token *old, *result;
692
693 old = pfile->cur_token - 1;
694 if (pfile->cur_token == pfile->cur_run->limit)
695 {
696 pfile->cur_run = next_tokenrun (pfile->cur_run);
697 pfile->cur_token = pfile->cur_run->base;
698 }
699
700 result = pfile->cur_token++;
Per Bothner12f9df42004-02-11 07:29:30 -0800701 result->src_loc = old->src_loc;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000702 return result;
703}
704
Neil Booth14baae02001-09-17 18:26:12 +0000705/* Lex a token into RESULT (external interface). Takes care of issues
706 like directive handling, token lookahead, multiple include
Joseph Myersa1f300c2001-11-23 02:05:19 +0000707 optimization and skipping. */
Neil Booth345894b2001-09-16 13:44:29 +0000708const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000709_cpp_lex_token (cpp_reader *pfile)
Neil Booth0d9f2342000-09-18 18:43:05 +0000710{
Neil Boothbdcbe492001-09-13 20:05:17 +0000711 cpp_token *result;
Neil Booth5fddcff2001-09-11 07:00:12 +0000712
Neil Boothbdcbe492001-09-13 20:05:17 +0000713 for (;;)
Neil Booth5fddcff2001-09-11 07:00:12 +0000714 {
Neil Boothbdcbe492001-09-13 20:05:17 +0000715 if (pfile->cur_token == pfile->cur_run->limit)
Neil Booth5fddcff2001-09-11 07:00:12 +0000716 {
Neil Boothbdcbe492001-09-13 20:05:17 +0000717 pfile->cur_run = next_tokenrun (pfile->cur_run);
718 pfile->cur_token = pfile->cur_run->base;
719 }
Neil Boothbdcbe492001-09-13 20:05:17 +0000720
721 if (pfile->lookaheads)
Neil Booth14baae02001-09-17 18:26:12 +0000722 {
723 pfile->lookaheads--;
724 result = pfile->cur_token++;
725 }
Neil Boothbdcbe492001-09-13 20:05:17 +0000726 else
Neil Booth14baae02001-09-17 18:26:12 +0000727 result = _cpp_lex_direct (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +0000728
729 if (result->flags & BOL)
730 {
Neil Boothbdcbe492001-09-13 20:05:17 +0000731 /* Is this a directive. If _cpp_handle_directive returns
732 false, it is an assembler #. */
733 if (result->type == CPP_HASH
Neil Boothe808ec92002-02-27 07:24:53 +0000734 /* 6.10.3 p 11: Directives in a list of macro arguments
735 gives undefined behavior. This implementation
736 handles the directive as normal. */
737 && pfile->state.parsing_args != 1
Neil Boothbdcbe492001-09-13 20:05:17 +0000738 && _cpp_handle_directive (pfile, result->flags & PREV_WHITE))
Zack Weinberg21b11492004-09-09 19:16:56 +0000739 {
740 if (pfile->directive_result.type == CPP_PADDING)
741 continue;
742 else
743 {
744 result = &pfile->directive_result;
745 break;
746 }
747 }
748
Neil Booth97293892001-09-14 22:04:46 +0000749 if (pfile->cb.line_change && !pfile->state.skipping)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000750 pfile->cb.line_change (pfile, result, pfile->state.parsing_args);
Neil Booth5fddcff2001-09-11 07:00:12 +0000751 }
752
Neil Boothbdcbe492001-09-13 20:05:17 +0000753 /* We don't skip tokens in directives. */
754 if (pfile->state.in_directive)
755 break;
Neil Booth5fddcff2001-09-11 07:00:12 +0000756
Neil Boothbdcbe492001-09-13 20:05:17 +0000757 /* Outside a directive, invalidate controlling macros. At file
Neil Booth14baae02001-09-17 18:26:12 +0000758 EOF, _cpp_lex_direct takes care of popping the buffer, so we never
Kazu Hirata6356f892003-06-12 19:01:08 +0000759 get here and MI optimization works. */
Neil Booth5fddcff2001-09-11 07:00:12 +0000760 pfile->mi_valid = false;
Neil Boothbdcbe492001-09-13 20:05:17 +0000761
762 if (!pfile->state.skipping || result->type == CPP_EOF)
763 break;
Neil Booth5fddcff2001-09-11 07:00:12 +0000764 }
765
Neil Booth345894b2001-09-16 13:44:29 +0000766 return result;
Neil Booth5fddcff2001-09-11 07:00:12 +0000767}
768
Neil Booth26aea072003-04-19 00:22:51 +0000769/* Returns true if a fresh line has been loaded. */
770bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000771_cpp_get_fresh_line (cpp_reader *pfile)
Neil Booth004cb262002-05-17 20:16:48 +0000772{
Per Bothner22234f52004-02-18 14:02:39 -0800773 int return_at_eof;
774
Neil Booth26aea072003-04-19 00:22:51 +0000775 /* We can't get a new line until we leave the current directive. */
776 if (pfile->state.in_directive)
777 return false;
Kazu Hiratadf383482002-05-22 22:02:16 +0000778
Neil Booth26aea072003-04-19 00:22:51 +0000779 for (;;)
Neil Booth1a769162002-06-11 05:36:17 +0000780 {
Neil Booth26aea072003-04-19 00:22:51 +0000781 cpp_buffer *buffer = pfile->buffer;
782
783 if (!buffer->need_line)
784 return true;
785
786 if (buffer->next_line < buffer->rlimit)
787 {
788 _cpp_clean_line (pfile);
789 return true;
790 }
791
792 /* First, get out of parsing arguments state. */
793 if (pfile->state.parsing_args)
Neil Booth1a769162002-06-11 05:36:17 +0000794 return false;
795
Neil Booth26aea072003-04-19 00:22:51 +0000796 /* End of buffer. Non-empty files should end in a newline. */
797 if (buffer->buf != buffer->rlimit
798 && buffer->next_line > buffer->rlimit
799 && !buffer->from_stage3)
Neil Booth004cb262002-05-17 20:16:48 +0000800 {
Neil Booth26aea072003-04-19 00:22:51 +0000801 /* Only warn once. */
802 buffer->next_line = buffer->rlimit;
Per Bothner500bee02004-04-22 19:22:27 -0700803 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line,
Neil Booth26aea072003-04-19 00:22:51 +0000804 CPP_BUF_COLUMN (buffer, buffer->cur),
805 "no newline at end of file");
Neil Booth004cb262002-05-17 20:16:48 +0000806 }
Per Bothner22234f52004-02-18 14:02:39 -0800807
808 return_at_eof = buffer->return_at_eof;
Neil Booth26aea072003-04-19 00:22:51 +0000809 _cpp_pop_buffer (pfile);
Per Bothner22234f52004-02-18 14:02:39 -0800810 if (pfile->buffer == NULL || return_at_eof)
Per Bothnera506c552003-10-02 07:20:38 +0000811 return false;
Neil Booth26aea072003-04-19 00:22:51 +0000812 }
Neil Booth004cb262002-05-17 20:16:48 +0000813}
814
Neil Booth6f572ac2003-04-19 16:34:33 +0000815#define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
816 do \
817 { \
818 result->type = ELSE_TYPE; \
819 if (*buffer->cur == CHAR) \
820 buffer->cur++, result->type = THEN_TYPE; \
821 } \
822 while (0)
Neil Booth480709c2001-10-21 14:04:42 +0000823
Neil Booth14baae02001-09-17 18:26:12 +0000824/* Lex a token into pfile->cur_token, which is also incremented, to
825 get diagnostics pointing to the correct location.
826
827 Does not handle issues such as token lookahead, multiple-include
Kazu Hirataf1ba6652003-06-28 19:43:01 +0000828 optimization, directives, skipping etc. This function is only
Neil Booth14baae02001-09-17 18:26:12 +0000829 suitable for use by _cpp_lex_token, and in special cases like
830 lex_expansion_token which doesn't care for any of these issues.
831
832 When meeting a newline, returns CPP_EOF if parsing a directive,
833 otherwise returns to the start of the token buffer if permissible.
834 Returns the location of the lexed token. */
835cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000836_cpp_lex_direct (cpp_reader *pfile)
Neil Booth5fddcff2001-09-11 07:00:12 +0000837{
Neil Booth0d9f2342000-09-18 18:43:05 +0000838 cppchar_t c;
Neil Boothadb84b42000-11-08 23:08:07 +0000839 cpp_buffer *buffer;
Neil Booth0d9f2342000-09-18 18:43:05 +0000840 const unsigned char *comment_start;
Neil Booth14baae02001-09-17 18:26:12 +0000841 cpp_token *result = pfile->cur_token++;
Neil Booth0d9f2342000-09-18 18:43:05 +0000842
Neil Booth5fddcff2001-09-11 07:00:12 +0000843 fresh_line:
Neil Booth26aea072003-04-19 00:22:51 +0000844 result->flags = 0;
Per Bothner2be570f2003-08-28 18:07:42 -0700845 buffer = pfile->buffer;
Per Bothnera506c552003-10-02 07:20:38 +0000846 if (buffer->need_line)
Neil Booth26aea072003-04-19 00:22:51 +0000847 {
848 if (!_cpp_get_fresh_line (pfile))
849 {
850 result->type = CPP_EOF;
Neil Booth9ff78682003-04-26 21:03:51 +0000851 if (!pfile->state.in_directive)
852 {
853 /* Tell the compiler the line number of the EOF token. */
Per Bothner500bee02004-04-22 19:22:27 -0700854 result->src_loc = pfile->line_table->highest_line;
Neil Booth9ff78682003-04-26 21:03:51 +0000855 result->flags = BOL;
856 }
Neil Booth26aea072003-04-19 00:22:51 +0000857 return result;
858 }
859 if (!pfile->keep_tokens)
860 {
861 pfile->cur_run = &pfile->base_run;
862 result = pfile->base_run.base;
863 pfile->cur_token = result + 1;
864 }
865 result->flags = BOL;
866 if (pfile->state.parsing_args == 2)
867 result->flags |= PREV_WHITE;
868 }
Per Bothnera506c552003-10-02 07:20:38 +0000869 buffer = pfile->buffer;
Neil Booth5fddcff2001-09-11 07:00:12 +0000870 update_tokens_line:
Per Bothner500bee02004-04-22 19:22:27 -0700871 result->src_loc = pfile->line_table->highest_line;
Neil Booth0d9f2342000-09-18 18:43:05 +0000872
Neil Booth5fddcff2001-09-11 07:00:12 +0000873 skipped_white:
Neil Booth26aea072003-04-19 00:22:51 +0000874 if (buffer->cur >= buffer->notes[buffer->cur_note].pos
875 && !pfile->overlaid_buffer)
876 {
877 _cpp_process_line_notes (pfile, false);
Per Bothner500bee02004-04-22 19:22:27 -0700878 result->src_loc = pfile->line_table->highest_line;
Neil Booth26aea072003-04-19 00:22:51 +0000879 }
Neil Booth480709c2001-10-21 14:04:42 +0000880 c = *buffer->cur++;
Per Bothner12f9df42004-02-11 07:29:30 -0800881
Per Bothner500bee02004-04-22 19:22:27 -0700882 LINEMAP_POSITION_FOR_COLUMN (result->src_loc, pfile->line_table,
883 CPP_BUF_COLUMN (buffer, buffer->cur));
Neil Booth5fddcff2001-09-11 07:00:12 +0000884
Neil Booth0d9f2342000-09-18 18:43:05 +0000885 switch (c)
886 {
Neil Booth4d6baaf2001-11-26 23:44:54 +0000887 case ' ': case '\t': case '\f': case '\v': case '\0':
888 result->flags |= PREV_WHITE;
Neil Booth26aea072003-04-19 00:22:51 +0000889 skip_whitespace (pfile, c);
890 goto skipped_white;
Neil Booth4d6baaf2001-11-26 23:44:54 +0000891
Neil Booth26aea072003-04-19 00:22:51 +0000892 case '\n':
Per Bothner12f9df42004-02-11 07:29:30 -0800893 if (buffer->cur < buffer->rlimit)
894 CPP_INCREMENT_LINE (pfile, 0);
Neil Booth26aea072003-04-19 00:22:51 +0000895 buffer->need_line = true;
896 goto fresh_line;
Neil Booth0d9f2342000-09-18 18:43:05 +0000897
Neil Booth0d9f2342000-09-18 18:43:05 +0000898 case '0': case '1': case '2': case '3': case '4':
899 case '5': case '6': case '7': case '8': case '9':
900 result->type = CPP_NUMBER;
Neil Boothbced6ed2003-04-19 11:59:44 +0000901 lex_number (pfile, &result->val.str);
Neil Booth0d9f2342000-09-18 18:43:05 +0000902 break;
903
Neil Booth0abc6a62001-11-27 22:31:34 +0000904 case 'L':
905 /* 'L' may introduce wide characters or strings. */
Neil Boothbced6ed2003-04-19 11:59:44 +0000906 if (*buffer->cur == '\'' || *buffer->cur == '"')
907 {
Neil Booth6338b352003-04-23 22:44:06 +0000908 lex_string (pfile, result, buffer->cur - 1);
Neil Boothbced6ed2003-04-19 11:59:44 +0000909 break;
910 }
Kazu Hiratadf383482002-05-22 22:02:16 +0000911 /* Fall through. */
Neil Booth0abc6a62001-11-27 22:31:34 +0000912
Neil Booth0d9f2342000-09-18 18:43:05 +0000913 case '_':
914 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
915 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
916 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
917 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
918 case 'y': case 'z':
919 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
Neil Booth0abc6a62001-11-27 22:31:34 +0000920 case 'G': case 'H': case 'I': case 'J': case 'K':
Neil Booth0d9f2342000-09-18 18:43:05 +0000921 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
922 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
923 case 'Y': case 'Z':
924 result->type = CPP_NAME;
Neil Booth1613e522003-04-20 07:29:23 +0000925 result->val.node = lex_identifier (pfile, buffer->cur - 1);
Neil Booth0d9f2342000-09-18 18:43:05 +0000926
Neil Booth0d9f2342000-09-18 18:43:05 +0000927 /* Convert named operators to their proper types. */
Neil Booth0abc6a62001-11-27 22:31:34 +0000928 if (result->val.node->flags & NODE_OPERATOR)
Neil Booth0d9f2342000-09-18 18:43:05 +0000929 {
930 result->flags |= NAMED_OP;
Zack Weinberg4977bab2002-12-16 18:23:00 +0000931 result->type = result->val.node->directive_index;
Neil Booth0d9f2342000-09-18 18:43:05 +0000932 }
933 break;
934
935 case '\'':
936 case '"':
Neil Booth6338b352003-04-23 22:44:06 +0000937 lex_string (pfile, result, buffer->cur - 1);
Neil Booth0d9f2342000-09-18 18:43:05 +0000938 break;
939
940 case '/':
Neil Booth1c6d33e2000-09-25 22:39:51 +0000941 /* A potential block or line comment. */
942 comment_start = buffer->cur;
Neil Booth6f572ac2003-04-19 16:34:33 +0000943 c = *buffer->cur;
944
Neil Booth1c6d33e2000-09-25 22:39:51 +0000945 if (c == '*')
946 {
Neil Booth26aea072003-04-19 00:22:51 +0000947 if (_cpp_skip_block_comment (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000948 cpp_error (pfile, CPP_DL_ERROR, "unterminated comment");
Neil Booth0d9f2342000-09-18 18:43:05 +0000949 }
Neil Booth480709c2001-10-21 14:04:42 +0000950 else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments)
Per Bothner12f9df42004-02-11 07:29:30 -0800951 || cpp_in_system_header (pfile)))
Neil Booth0d9f2342000-09-18 18:43:05 +0000952 {
Neil Boothbdb05a72000-11-26 17:31:13 +0000953 /* Warn about comments only if pedantically GNUC89, and not
954 in system headers. */
955 if (CPP_OPTION (pfile, lang) == CLK_GNUC89 && CPP_PEDANTIC (pfile)
Neil Bootha94c1192000-09-25 23:35:10 +0000956 && ! buffer->warned_cplusplus_comments)
Neil Booth0d9f2342000-09-18 18:43:05 +0000957 {
John David Anglin0527bc42003-11-01 22:56:54 +0000958 cpp_error (pfile, CPP_DL_PEDWARN,
Gabriel Dos Reis56508302002-07-21 21:35:17 +0000959 "C++ style comments are not allowed in ISO C90");
John David Anglin0527bc42003-11-01 22:56:54 +0000960 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +0000961 "(this will be reported only once per input file)");
Neil Booth1c6d33e2000-09-25 22:39:51 +0000962 buffer->warned_cplusplus_comments = 1;
Neil Booth0d9f2342000-09-18 18:43:05 +0000963 }
Neil Booth1c6d33e2000-09-25 22:39:51 +0000964
Jakub Jelinek01ef6562001-04-11 11:43:10 +0200965 if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments))
John David Anglin0527bc42003-11-01 22:56:54 +0000966 cpp_error (pfile, CPP_DL_WARNING, "multi-line comment");
Neil Booth0d9f2342000-09-18 18:43:05 +0000967 }
Neil Booth480709c2001-10-21 14:04:42 +0000968 else if (c == '=')
969 {
Neil Booth6f572ac2003-04-19 16:34:33 +0000970 buffer->cur++;
Neil Booth480709c2001-10-21 14:04:42 +0000971 result->type = CPP_DIV_EQ;
972 break;
973 }
974 else
975 {
Neil Booth480709c2001-10-21 14:04:42 +0000976 result->type = CPP_DIV;
977 break;
978 }
Neil Booth1c6d33e2000-09-25 22:39:51 +0000979
Neil Booth1c6d33e2000-09-25 22:39:51 +0000980 if (!pfile->state.save_comments)
981 {
982 result->flags |= PREV_WHITE;
Neil Booth5fddcff2001-09-11 07:00:12 +0000983 goto update_tokens_line;
Neil Booth1c6d33e2000-09-25 22:39:51 +0000984 }
985
986 /* Save the comment as a token in its own right. */
Jason Thorpe477cdac2002-04-07 03:12:23 +0000987 save_comment (pfile, result, comment_start, c);
Neil Boothbdcbe492001-09-13 20:05:17 +0000988 break;
Neil Booth0d9f2342000-09-18 18:43:05 +0000989
990 case '<':
991 if (pfile->state.angled_headers)
992 {
Neil Booth6338b352003-04-23 22:44:06 +0000993 lex_string (pfile, result, buffer->cur - 1);
Neil Booth480709c2001-10-21 14:04:42 +0000994 break;
Neil Booth0d9f2342000-09-18 18:43:05 +0000995 }
996
Neil Booth6f572ac2003-04-19 16:34:33 +0000997 result->type = CPP_LESS;
998 if (*buffer->cur == '=')
999 buffer->cur++, result->type = CPP_LESS_EQ;
1000 else if (*buffer->cur == '<')
Neil Booth0d9f2342000-09-18 18:43:05 +00001001 {
Neil Booth6f572ac2003-04-19 16:34:33 +00001002 buffer->cur++;
1003 IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT);
Neil Booth0d9f2342000-09-18 18:43:05 +00001004 }
Neil Booth6f572ac2003-04-19 16:34:33 +00001005 else if (*buffer->cur == '?' && CPP_OPTION (pfile, cplusplus))
Neil Booth0d9f2342000-09-18 18:43:05 +00001006 {
Neil Booth6f572ac2003-04-19 16:34:33 +00001007 buffer->cur++;
1008 IF_NEXT_IS ('=', CPP_MIN_EQ, CPP_MIN);
Neil Booth0d9f2342000-09-18 18:43:05 +00001009 }
Neil Booth6f572ac2003-04-19 16:34:33 +00001010 else if (CPP_OPTION (pfile, digraphs))
Neil Booth480709c2001-10-21 14:04:42 +00001011 {
Neil Booth6f572ac2003-04-19 16:34:33 +00001012 if (*buffer->cur == ':')
1013 {
1014 buffer->cur++;
1015 result->flags |= DIGRAPH;
1016 result->type = CPP_OPEN_SQUARE;
1017 }
1018 else if (*buffer->cur == '%')
1019 {
1020 buffer->cur++;
1021 result->flags |= DIGRAPH;
1022 result->type = CPP_OPEN_BRACE;
1023 }
Neil Booth480709c2001-10-21 14:04:42 +00001024 }
Neil Booth0d9f2342000-09-18 18:43:05 +00001025 break;
1026
1027 case '>':
Neil Booth6f572ac2003-04-19 16:34:33 +00001028 result->type = CPP_GREATER;
1029 if (*buffer->cur == '=')
1030 buffer->cur++, result->type = CPP_GREATER_EQ;
1031 else if (*buffer->cur == '>')
Neil Booth0d9f2342000-09-18 18:43:05 +00001032 {
Neil Booth6f572ac2003-04-19 16:34:33 +00001033 buffer->cur++;
1034 IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT);
1035 }
1036 else if (*buffer->cur == '?' && CPP_OPTION (pfile, cplusplus))
1037 {
1038 buffer->cur++;
1039 IF_NEXT_IS ('=', CPP_MAX_EQ, CPP_MAX);
Neil Booth0d9f2342000-09-18 18:43:05 +00001040 }
1041 break;
1042
Neil Boothcbcff6d2000-09-23 21:41:41 +00001043 case '%':
Neil Booth6f572ac2003-04-19 16:34:33 +00001044 result->type = CPP_MOD;
1045 if (*buffer->cur == '=')
1046 buffer->cur++, result->type = CPP_MOD_EQ;
1047 else if (CPP_OPTION (pfile, digraphs))
Neil Booth480709c2001-10-21 14:04:42 +00001048 {
Neil Booth6f572ac2003-04-19 16:34:33 +00001049 if (*buffer->cur == ':')
Neil Booth480709c2001-10-21 14:04:42 +00001050 {
Neil Booth6f572ac2003-04-19 16:34:33 +00001051 buffer->cur++;
1052 result->flags |= DIGRAPH;
1053 result->type = CPP_HASH;
1054 if (*buffer->cur == '%' && buffer->cur[1] == ':')
1055 buffer->cur += 2, result->type = CPP_PASTE;
Neil Booth480709c2001-10-21 14:04:42 +00001056 }
Neil Booth6f572ac2003-04-19 16:34:33 +00001057 else if (*buffer->cur == '>')
1058 {
1059 buffer->cur++;
1060 result->flags |= DIGRAPH;
1061 result->type = CPP_CLOSE_BRACE;
1062 }
Neil Booth480709c2001-10-21 14:04:42 +00001063 }
Neil Booth0d9f2342000-09-18 18:43:05 +00001064 break;
1065
Neil Boothcbcff6d2000-09-23 21:41:41 +00001066 case '.':
Neil Booth480709c2001-10-21 14:04:42 +00001067 result->type = CPP_DOT;
Neil Booth6f572ac2003-04-19 16:34:33 +00001068 if (ISDIGIT (*buffer->cur))
Neil Booth480709c2001-10-21 14:04:42 +00001069 {
Neil Booth480709c2001-10-21 14:04:42 +00001070 result->type = CPP_NUMBER;
Neil Boothbced6ed2003-04-19 11:59:44 +00001071 lex_number (pfile, &result->val.str);
Neil Booth480709c2001-10-21 14:04:42 +00001072 }
Neil Booth6f572ac2003-04-19 16:34:33 +00001073 else if (*buffer->cur == '.' && buffer->cur[1] == '.')
1074 buffer->cur += 2, result->type = CPP_ELLIPSIS;
1075 else if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
1076 buffer->cur++, result->type = CPP_DOT_STAR;
Neil Booth0d9f2342000-09-18 18:43:05 +00001077 break;
1078
1079 case '+':
Neil Booth6f572ac2003-04-19 16:34:33 +00001080 result->type = CPP_PLUS;
1081 if (*buffer->cur == '+')
1082 buffer->cur++, result->type = CPP_PLUS_PLUS;
1083 else if (*buffer->cur == '=')
1084 buffer->cur++, result->type = CPP_PLUS_EQ;
Neil Booth0d9f2342000-09-18 18:43:05 +00001085 break;
1086
1087 case '-':
Neil Booth6f572ac2003-04-19 16:34:33 +00001088 result->type = CPP_MINUS;
1089 if (*buffer->cur == '>')
Neil Booth0d9f2342000-09-18 18:43:05 +00001090 {
Neil Booth6f572ac2003-04-19 16:34:33 +00001091 buffer->cur++;
Neil Booth480709c2001-10-21 14:04:42 +00001092 result->type = CPP_DEREF;
Neil Booth6f572ac2003-04-19 16:34:33 +00001093 if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
1094 buffer->cur++, result->type = CPP_DEREF_STAR;
Neil Booth0d9f2342000-09-18 18:43:05 +00001095 }
Neil Booth6f572ac2003-04-19 16:34:33 +00001096 else if (*buffer->cur == '-')
1097 buffer->cur++, result->type = CPP_MINUS_MINUS;
1098 else if (*buffer->cur == '=')
1099 buffer->cur++, result->type = CPP_MINUS_EQ;
Neil Booth0d9f2342000-09-18 18:43:05 +00001100 break;
1101
1102 case '&':
Neil Booth6f572ac2003-04-19 16:34:33 +00001103 result->type = CPP_AND;
1104 if (*buffer->cur == '&')
1105 buffer->cur++, result->type = CPP_AND_AND;
1106 else if (*buffer->cur == '=')
1107 buffer->cur++, result->type = CPP_AND_EQ;
Neil Booth0d9f2342000-09-18 18:43:05 +00001108 break;
Kazu Hiratadf383482002-05-22 22:02:16 +00001109
Neil Booth0d9f2342000-09-18 18:43:05 +00001110 case '|':
Neil Booth6f572ac2003-04-19 16:34:33 +00001111 result->type = CPP_OR;
1112 if (*buffer->cur == '|')
1113 buffer->cur++, result->type = CPP_OR_OR;
1114 else if (*buffer->cur == '=')
1115 buffer->cur++, result->type = CPP_OR_EQ;
Neil Booth0d9f2342000-09-18 18:43:05 +00001116 break;
1117
1118 case ':':
Neil Booth6f572ac2003-04-19 16:34:33 +00001119 result->type = CPP_COLON;
1120 if (*buffer->cur == ':' && CPP_OPTION (pfile, cplusplus))
1121 buffer->cur++, result->type = CPP_SCOPE;
1122 else if (*buffer->cur == '>' && CPP_OPTION (pfile, digraphs))
Neil Booth0d9f2342000-09-18 18:43:05 +00001123 {
Neil Booth6f572ac2003-04-19 16:34:33 +00001124 buffer->cur++;
Neil Booth0d9f2342000-09-18 18:43:05 +00001125 result->flags |= DIGRAPH;
Neil Booth480709c2001-10-21 14:04:42 +00001126 result->type = CPP_CLOSE_SQUARE;
1127 }
Neil Booth0d9f2342000-09-18 18:43:05 +00001128 break;
1129
Neil Booth480709c2001-10-21 14:04:42 +00001130 case '*': IF_NEXT_IS ('=', CPP_MULT_EQ, CPP_MULT); break;
1131 case '=': IF_NEXT_IS ('=', CPP_EQ_EQ, CPP_EQ); break;
1132 case '!': IF_NEXT_IS ('=', CPP_NOT_EQ, CPP_NOT); break;
1133 case '^': IF_NEXT_IS ('=', CPP_XOR_EQ, CPP_XOR); break;
1134 case '#': IF_NEXT_IS ('#', CPP_PASTE, CPP_HASH); break;
1135
Neil Booth26aea072003-04-19 00:22:51 +00001136 case '?': result->type = CPP_QUERY; break;
Neil Booth0d9f2342000-09-18 18:43:05 +00001137 case '~': result->type = CPP_COMPL; break;
1138 case ',': result->type = CPP_COMMA; break;
1139 case '(': result->type = CPP_OPEN_PAREN; break;
1140 case ')': result->type = CPP_CLOSE_PAREN; break;
1141 case '[': result->type = CPP_OPEN_SQUARE; break;
1142 case ']': result->type = CPP_CLOSE_SQUARE; break;
1143 case '{': result->type = CPP_OPEN_BRACE; break;
1144 case '}': result->type = CPP_CLOSE_BRACE; break;
1145 case ';': result->type = CPP_SEMICOLON; break;
1146
Kazu Hirata40f03652002-09-26 22:25:14 +00001147 /* @ is a punctuator in Objective-C. */
Zack Weinbergcc937582001-03-07 01:32:01 +00001148 case '@': result->type = CPP_ATSIGN; break;
Neil Booth0d9f2342000-09-18 18:43:05 +00001149
Neil Booth0abc6a62001-11-27 22:31:34 +00001150 case '$':
Neil Booth1613e522003-04-20 07:29:23 +00001151 case '\\':
1152 {
1153 const uchar *base = --buffer->cur;
Neil Booth0abc6a62001-11-27 22:31:34 +00001154
Neil Booth1613e522003-04-20 07:29:23 +00001155 if (forms_identifier_p (pfile, true))
1156 {
1157 result->type = CPP_NAME;
1158 result->val.node = lex_identifier (pfile, base);
1159 break;
1160 }
1161 buffer->cur++;
Neil Booth10676942003-04-22 19:28:00 +00001162 }
Neil Booth1613e522003-04-20 07:29:23 +00001163
Neil Booth10676942003-04-22 19:28:00 +00001164 default:
Neil Booth6338b352003-04-23 22:44:06 +00001165 create_literal (pfile, result, buffer->cur - 1, 1, CPP_OTHER);
1166 break;
Neil Booth0d9f2342000-09-18 18:43:05 +00001167 }
Neil Boothbdcbe492001-09-13 20:05:17 +00001168
1169 return result;
Zack Weinbergc5a04732000-04-25 19:32:36 +00001170}
1171
Neil Booth59325652003-04-24 20:03:57 +00001172/* An upper bound on the number of bytes needed to spell TOKEN.
1173 Does not include preceding whitespace. */
Neil Booth93c803682000-10-28 17:59:06 +00001174unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001175cpp_token_len (const cpp_token *token)
Zack Weinbergc5a04732000-04-25 19:32:36 +00001176{
Neil Booth93c803682000-10-28 17:59:06 +00001177 unsigned int len;
Zack Weinbergc5a04732000-04-25 19:32:36 +00001178
Neil Booth93c803682000-10-28 17:59:06 +00001179 switch (TOKEN_SPELL (token))
Zack Weinbergc5a04732000-04-25 19:32:36 +00001180 {
Neil Booth59325652003-04-24 20:03:57 +00001181 default: len = 4; break;
Neil Booth6338b352003-04-23 22:44:06 +00001182 case SPELL_LITERAL: len = token->val.str.len; break;
Neil Bootha28c50352001-05-16 22:02:09 +00001183 case SPELL_IDENT: len = NODE_LEN (token->val.node); break;
Zack Weinbergc5a04732000-04-25 19:32:36 +00001184 }
Neil Booth59325652003-04-24 20:03:57 +00001185
1186 return len;
Zack Weinberg041c3192000-07-04 01:58:21 +00001187}
1188
Neil Booth3fef5b22000-05-08 22:22:49 +00001189/* Write the spelling of a token TOKEN to BUFFER. The buffer must
Zack Weinbergcf00a882000-07-08 02:33:00 +00001190 already contain the enough space to hold the token's spelling.
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001191 Returns a pointer to the character after the last character written.
1192 FIXME: Would be nice if we didn't need the PFILE argument. */
Neil Booth93c803682000-10-28 17:59:06 +00001193unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001194cpp_spell_token (cpp_reader *pfile, const cpp_token *token,
1195 unsigned char *buffer)
Neil Booth3fef5b22000-05-08 22:22:49 +00001196{
Zack Weinberg96be6992000-07-18 23:25:06 +00001197 switch (TOKEN_SPELL (token))
Neil Booth3fef5b22000-05-08 22:22:49 +00001198 {
Neil Booth5d7ee2f2000-05-10 09:39:18 +00001199 case SPELL_OPERATOR:
Neil Booth3fef5b22000-05-08 22:22:49 +00001200 {
1201 const unsigned char *spelling;
1202 unsigned char c;
1203
1204 if (token->flags & DIGRAPH)
John David Anglin37b85242001-03-02 01:11:50 +00001205 spelling
1206 = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH];
Zack Weinberg92936ec2000-07-19 20:18:08 +00001207 else if (token->flags & NAMED_OP)
1208 goto spell_ident;
Neil Booth3fef5b22000-05-08 22:22:49 +00001209 else
Zack Weinberg96be6992000-07-18 23:25:06 +00001210 spelling = TOKEN_NAME (token);
Kazu Hiratadf383482002-05-22 22:02:16 +00001211
Neil Booth3fef5b22000-05-08 22:22:49 +00001212 while ((c = *spelling++) != '\0')
1213 *buffer++ = c;
1214 }
1215 break;
1216
Zack Weinberg47ad4132001-10-06 23:11:27 +00001217 spell_ident:
Neil Booth5d7ee2f2000-05-10 09:39:18 +00001218 case SPELL_IDENT:
Neil Bootha28c50352001-05-16 22:02:09 +00001219 memcpy (buffer, NODE_NAME (token->val.node), NODE_LEN (token->val.node));
1220 buffer += NODE_LEN (token->val.node);
Neil Booth5d7ee2f2000-05-10 09:39:18 +00001221 break;
Neil Booth3fef5b22000-05-08 22:22:49 +00001222
Neil Booth6338b352003-04-23 22:44:06 +00001223 case SPELL_LITERAL:
Zack Weinberg47ad4132001-10-06 23:11:27 +00001224 memcpy (buffer, token->val.str.text, token->val.str.len);
1225 buffer += token->val.str.len;
1226 break;
1227
Neil Booth3fef5b22000-05-08 22:22:49 +00001228 case SPELL_NONE:
John David Anglin0527bc42003-11-01 22:56:54 +00001229 cpp_error (pfile, CPP_DL_ICE,
1230 "unspellable token %s", TOKEN_NAME (token));
Neil Booth3fef5b22000-05-08 22:22:49 +00001231 break;
1232 }
1233
1234 return buffer;
1235}
1236
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001237/* Returns TOKEN spelt as a null-terminated string. The string is
1238 freed when the reader is destroyed. Useful for diagnostics. */
Neil Booth93c803682000-10-28 17:59:06 +00001239unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001240cpp_token_as_text (cpp_reader *pfile, const cpp_token *token)
Neil Booth59325652003-04-24 20:03:57 +00001241{
1242 unsigned int len = cpp_token_len (token) + 1;
Neil Boothece54d52001-09-28 09:40:22 +00001243 unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end;
Zack Weinberg041c3192000-07-04 01:58:21 +00001244
Neil Booth93c803682000-10-28 17:59:06 +00001245 end = cpp_spell_token (pfile, token, start);
1246 end[0] = '\0';
Zack Weinberg041c3192000-07-04 01:58:21 +00001247
Neil Booth93c803682000-10-28 17:59:06 +00001248 return start;
Zack Weinberg041c3192000-07-04 01:58:21 +00001249}
1250
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001251/* Used by C front ends, which really should move to using
1252 cpp_token_as_text. */
Neil Booth93c803682000-10-28 17:59:06 +00001253const char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001254cpp_type2name (enum cpp_ttype type)
Zack Weinberg041c3192000-07-04 01:58:21 +00001255{
Neil Booth93c803682000-10-28 17:59:06 +00001256 return (const char *) token_spellings[type].name;
Zack Weinberg041c3192000-07-04 01:58:21 +00001257}
1258
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001259/* Writes the spelling of token to FP, without any preceding space.
1260 Separated from cpp_spell_token for efficiency - to avoid stdio
1261 double-buffering. */
Zack Weinberg041c3192000-07-04 01:58:21 +00001262void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001263cpp_output_token (const cpp_token *token, FILE *fp)
Zack Weinberg041c3192000-07-04 01:58:21 +00001264{
Neil Booth93c803682000-10-28 17:59:06 +00001265 switch (TOKEN_SPELL (token))
Zack Weinberg041c3192000-07-04 01:58:21 +00001266 {
Neil Booth93c803682000-10-28 17:59:06 +00001267 case SPELL_OPERATOR:
1268 {
1269 const unsigned char *spelling;
Zack Weinberg3b681e92001-09-28 07:00:27 +00001270 int c;
Neil Booth93c803682000-10-28 17:59:06 +00001271
1272 if (token->flags & DIGRAPH)
John David Anglin37b85242001-03-02 01:11:50 +00001273 spelling
1274 = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH];
Neil Booth93c803682000-10-28 17:59:06 +00001275 else if (token->flags & NAMED_OP)
1276 goto spell_ident;
1277 else
1278 spelling = TOKEN_NAME (token);
1279
Zack Weinberg3b681e92001-09-28 07:00:27 +00001280 c = *spelling;
1281 do
1282 putc (c, fp);
1283 while ((c = *++spelling) != '\0');
Neil Booth93c803682000-10-28 17:59:06 +00001284 }
1285 break;
1286
1287 spell_ident:
1288 case SPELL_IDENT:
Zack Weinberg3b681e92001-09-28 07:00:27 +00001289 fwrite (NODE_NAME (token->val.node), 1, NODE_LEN (token->val.node), fp);
Neil Booth93c803682000-10-28 17:59:06 +00001290 break;
1291
Neil Booth6338b352003-04-23 22:44:06 +00001292 case SPELL_LITERAL:
Zack Weinberg47ad4132001-10-06 23:11:27 +00001293 fwrite (token->val.str.text, 1, token->val.str.len, fp);
1294 break;
1295
Neil Booth93c803682000-10-28 17:59:06 +00001296 case SPELL_NONE:
1297 /* An error, most probably. */
1298 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00001299 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001300}
1301
Neil Booth93c803682000-10-28 17:59:06 +00001302/* Compare two tokens. */
1303int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001304_cpp_equiv_tokens (const cpp_token *a, const cpp_token *b)
Neil Booth93c803682000-10-28 17:59:06 +00001305{
1306 if (a->type == b->type && a->flags == b->flags)
1307 switch (TOKEN_SPELL (a))
1308 {
1309 default: /* Keep compiler happy. */
1310 case SPELL_OPERATOR:
1311 return 1;
Neil Booth93c803682000-10-28 17:59:06 +00001312 case SPELL_NONE:
Neil Booth56051c02000-11-06 18:47:21 +00001313 return (a->type != CPP_MACRO_ARG || a->val.arg_no == b->val.arg_no);
Neil Booth93c803682000-10-28 17:59:06 +00001314 case SPELL_IDENT:
1315 return a->val.node == b->val.node;
Neil Booth6338b352003-04-23 22:44:06 +00001316 case SPELL_LITERAL:
Neil Booth93c803682000-10-28 17:59:06 +00001317 return (a->val.str.len == b->val.str.len
1318 && !memcmp (a->val.str.text, b->val.str.text,
1319 a->val.str.len));
1320 }
1321
1322 return 0;
1323}
1324
Neil Booth93c803682000-10-28 17:59:06 +00001325/* Returns nonzero if a space should be inserted to avoid an
1326 accidental token paste for output. For simplicity, it is
1327 conservative, and occasionally advises a space where one is not
1328 needed, e.g. "." and ".2". */
Neil Booth93c803682000-10-28 17:59:06 +00001329int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001330cpp_avoid_paste (cpp_reader *pfile, const cpp_token *token1,
1331 const cpp_token *token2)
Zack Weinberg041c3192000-07-04 01:58:21 +00001332{
Neil Booth93c803682000-10-28 17:59:06 +00001333 enum cpp_ttype a = token1->type, b = token2->type;
1334 cppchar_t c;
Zack Weinberg041c3192000-07-04 01:58:21 +00001335
Neil Booth93c803682000-10-28 17:59:06 +00001336 if (token1->flags & NAMED_OP)
1337 a = CPP_NAME;
1338 if (token2->flags & NAMED_OP)
1339 b = CPP_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +00001340
Neil Booth93c803682000-10-28 17:59:06 +00001341 c = EOF;
1342 if (token2->flags & DIGRAPH)
John David Anglin37b85242001-03-02 01:11:50 +00001343 c = digraph_spellings[(int) b - (int) CPP_FIRST_DIGRAPH][0];
Neil Booth93c803682000-10-28 17:59:06 +00001344 else if (token_spellings[b].category == SPELL_OPERATOR)
1345 c = token_spellings[b].name[0];
Zack Weinberg417f3e32000-07-11 23:20:53 +00001346
Neil Booth93c803682000-10-28 17:59:06 +00001347 /* Quickly get everything that can paste with an '='. */
John David Anglin37b85242001-03-02 01:11:50 +00001348 if ((int) a <= (int) CPP_LAST_EQ && c == '=')
Zack Weinberg041c3192000-07-04 01:58:21 +00001349 return 1;
1350
Neil Booth93c803682000-10-28 17:59:06 +00001351 switch (a)
1352 {
1353 case CPP_GREATER: return c == '>' || c == '?';
1354 case CPP_LESS: return c == '<' || c == '?' || c == '%' || c == ':';
1355 case CPP_PLUS: return c == '+';
1356 case CPP_MINUS: return c == '-' || c == '>';
1357 case CPP_DIV: return c == '/' || c == '*'; /* Comments. */
1358 case CPP_MOD: return c == ':' || c == '>';
1359 case CPP_AND: return c == '&';
1360 case CPP_OR: return c == '|';
1361 case CPP_COLON: return c == ':' || c == '>';
1362 case CPP_DEREF: return c == '*';
Neil Booth26ec42e2001-01-28 11:22:23 +00001363 case CPP_DOT: return c == '.' || c == '%' || b == CPP_NUMBER;
Neil Booth93c803682000-10-28 17:59:06 +00001364 case CPP_HASH: return c == '#' || c == '%'; /* Digraph form. */
1365 case CPP_NAME: return ((b == CPP_NUMBER
1366 && name_p (pfile, &token2->val.str))
1367 || b == CPP_NAME
1368 || b == CPP_CHAR || b == CPP_STRING); /* L */
1369 case CPP_NUMBER: return (b == CPP_NUMBER || b == CPP_NAME
1370 || c == '.' || c == '+' || c == '-');
Neil Booth1613e522003-04-20 07:29:23 +00001371 /* UCNs */
Neil Booth10676942003-04-22 19:28:00 +00001372 case CPP_OTHER: return ((token1->val.str.text[0] == '\\'
1373 && b == CPP_NAME)
Neil Booth1613e522003-04-20 07:29:23 +00001374 || (CPP_OPTION (pfile, objc)
Neil Booth10676942003-04-22 19:28:00 +00001375 && token1->val.str.text[0] == '@'
Neil Booth1613e522003-04-20 07:29:23 +00001376 && (b == CPP_NAME || b == CPP_STRING)));
Neil Booth93c803682000-10-28 17:59:06 +00001377 default: break;
1378 }
Zack Weinberg041c3192000-07-04 01:58:21 +00001379
1380 return 0;
1381}
1382
Neil Booth93c803682000-10-28 17:59:06 +00001383/* Output all the remaining tokens on the current line, and a newline
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001384 character, to FP. Leading whitespace is removed. If there are
1385 macros, special token padding is not performed. */
Neil Booth93c803682000-10-28 17:59:06 +00001386void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001387cpp_output_line (cpp_reader *pfile, FILE *fp)
Zack Weinberg041c3192000-07-04 01:58:21 +00001388{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001389 const cpp_token *token;
Zack Weinberg041c3192000-07-04 01:58:21 +00001390
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001391 token = cpp_get_token (pfile);
1392 while (token->type != CPP_EOF)
Zack Weinberg6ead1e92000-07-31 23:47:19 +00001393 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001394 cpp_output_token (token, fp);
1395 token = cpp_get_token (pfile);
1396 if (token->flags & PREV_WHITE)
1397 putc (' ', fp);
Zack Weinberg6ead1e92000-07-31 23:47:19 +00001398 }
1399
Neil Booth93c803682000-10-28 17:59:06 +00001400 putc ('\n', fp);
Zack Weinberg041c3192000-07-04 01:58:21 +00001401}
1402
Neil Booth1e013d22001-09-26 21:44:35 +00001403/* Memory buffers. Changing these three constants can have a dramatic
1404 effect on performance. The values here are reasonable defaults,
1405 but might be tuned. If you adjust them, be sure to test across a
1406 range of uses of cpplib, including heavy nested function-like macro
1407 expansion. Also check the change in peak memory usage (NJAMD is a
1408 good tool for this). */
1409#define MIN_BUFF_SIZE 8000
Neil Booth87062812001-10-20 09:00:53 +00001410#define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2)
Neil Booth1e013d22001-09-26 21:44:35 +00001411#define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \
1412 (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2)
Neil Booth93c803682000-10-28 17:59:06 +00001413
Neil Booth87062812001-10-20 09:00:53 +00001414#if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0)
1415 #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
1416#endif
1417
Neil Boothc9e7a602001-09-27 12:59:38 +00001418/* Create a new allocation buffer. Place the control block at the end
1419 of the buffer, so that buffer overflows will cause immediate chaos. */
Neil Boothb8af0ca2001-09-26 17:52:50 +00001420static _cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001421new_buff (size_t len)
Neil Boothb8af0ca2001-09-26 17:52:50 +00001422{
1423 _cpp_buff *result;
Neil Boothece54d52001-09-28 09:40:22 +00001424 unsigned char *base;
Neil Boothb8af0ca2001-09-26 17:52:50 +00001425
Neil Booth1e013d22001-09-26 21:44:35 +00001426 if (len < MIN_BUFF_SIZE)
1427 len = MIN_BUFF_SIZE;
Neil Boothc70f6ed2002-06-07 06:26:32 +00001428 len = CPP_ALIGN (len);
Neil Boothb8af0ca2001-09-26 17:52:50 +00001429
1430 base = xmalloc (len + sizeof (_cpp_buff));
1431 result = (_cpp_buff *) (base + len);
1432 result->base = base;
1433 result->cur = base;
1434 result->limit = base + len;
1435 result->next = NULL;
1436 return result;
1437}
1438
1439/* Place a chain of unwanted allocation buffers on the free list. */
1440void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001441_cpp_release_buff (cpp_reader *pfile, _cpp_buff *buff)
Neil Boothb8af0ca2001-09-26 17:52:50 +00001442{
1443 _cpp_buff *end = buff;
1444
1445 while (end->next)
1446 end = end->next;
1447 end->next = pfile->free_buffs;
1448 pfile->free_buffs = buff;
1449}
1450
1451/* Return a free buffer of size at least MIN_SIZE. */
1452_cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001453_cpp_get_buff (cpp_reader *pfile, size_t min_size)
Neil Boothb8af0ca2001-09-26 17:52:50 +00001454{
1455 _cpp_buff *result, **p;
1456
1457 for (p = &pfile->free_buffs;; p = &(*p)->next)
1458 {
Neil Booth61420882001-09-28 13:25:38 +00001459 size_t size;
Neil Booth1e013d22001-09-26 21:44:35 +00001460
1461 if (*p == NULL)
Neil Boothb8af0ca2001-09-26 17:52:50 +00001462 return new_buff (min_size);
Neil Booth1e013d22001-09-26 21:44:35 +00001463 result = *p;
1464 size = result->limit - result->base;
1465 /* Return a buffer that's big enough, but don't waste one that's
1466 way too big. */
Richard Earnshaw34f52712001-10-17 16:20:04 +00001467 if (size >= min_size && size <= BUFF_SIZE_UPPER_BOUND (min_size))
Neil Boothb8af0ca2001-09-26 17:52:50 +00001468 break;
1469 }
1470
1471 *p = result->next;
1472 result->next = NULL;
1473 result->cur = result->base;
1474 return result;
1475}
1476
Kazu Hirata4fe9b912001-10-09 06:03:16 +00001477/* Creates a new buffer with enough space to hold the uncommitted
Neil Booth8c3b2692001-09-30 10:03:11 +00001478 remaining bytes of BUFF, and at least MIN_EXTRA more bytes. Copies
1479 the excess bytes to the new buffer. Chains the new buffer after
1480 BUFF, and returns the new buffer. */
Neil Boothb8af0ca2001-09-26 17:52:50 +00001481_cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001482_cpp_append_extend_buff (cpp_reader *pfile, _cpp_buff *buff, size_t min_extra)
Neil Boothb8af0ca2001-09-26 17:52:50 +00001483{
Neil Booth61420882001-09-28 13:25:38 +00001484 size_t size = EXTENDED_BUFF_SIZE (buff, min_extra);
Neil Booth8c3b2692001-09-30 10:03:11 +00001485 _cpp_buff *new_buff = _cpp_get_buff (pfile, size);
Neil Boothb8af0ca2001-09-26 17:52:50 +00001486
Neil Booth8c3b2692001-09-30 10:03:11 +00001487 buff->next = new_buff;
1488 memcpy (new_buff->base, buff->cur, BUFF_ROOM (buff));
1489 return new_buff;
1490}
1491
Kazu Hirata4fe9b912001-10-09 06:03:16 +00001492/* Creates a new buffer with enough space to hold the uncommitted
Neil Booth8c3b2692001-09-30 10:03:11 +00001493 remaining bytes of the buffer pointed to by BUFF, and at least
1494 MIN_EXTRA more bytes. Copies the excess bytes to the new buffer.
1495 Chains the new buffer before the buffer pointed to by BUFF, and
1496 updates the pointer to point to the new buffer. */
1497void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001498_cpp_extend_buff (cpp_reader *pfile, _cpp_buff **pbuff, size_t min_extra)
Neil Booth8c3b2692001-09-30 10:03:11 +00001499{
1500 _cpp_buff *new_buff, *old_buff = *pbuff;
1501 size_t size = EXTENDED_BUFF_SIZE (old_buff, min_extra);
1502
1503 new_buff = _cpp_get_buff (pfile, size);
1504 memcpy (new_buff->base, old_buff->cur, BUFF_ROOM (old_buff));
1505 new_buff->next = old_buff;
1506 *pbuff = new_buff;
Neil Boothb8af0ca2001-09-26 17:52:50 +00001507}
1508
1509/* Free a chain of buffers starting at BUFF. */
1510void
Andreas Jaeger5671bf22003-07-07 21:11:59 +02001511_cpp_free_buff (_cpp_buff *buff)
Neil Boothb8af0ca2001-09-26 17:52:50 +00001512{
1513 _cpp_buff *next;
1514
1515 for (; buff; buff = next)
1516 {
1517 next = buff->next;
1518 free (buff->base);
1519 }
1520}
Neil Booth93c803682000-10-28 17:59:06 +00001521
Neil Boothece54d52001-09-28 09:40:22 +00001522/* Allocate permanent, unaligned storage of length LEN. */
1523unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001524_cpp_unaligned_alloc (cpp_reader *pfile, size_t len)
Neil Boothece54d52001-09-28 09:40:22 +00001525{
1526 _cpp_buff *buff = pfile->u_buff;
1527 unsigned char *result = buff->cur;
1528
1529 if (len > (size_t) (buff->limit - result))
1530 {
1531 buff = _cpp_get_buff (pfile, len);
1532 buff->next = pfile->u_buff;
1533 pfile->u_buff = buff;
1534 result = buff->cur;
1535 }
1536
1537 buff->cur = result + len;
1538 return result;
1539}
1540
Neil Booth87062812001-10-20 09:00:53 +00001541/* Allocate permanent, unaligned storage of length LEN from a_buff.
1542 That buffer is used for growing allocations when saving macro
1543 replacement lists in a #define, and when parsing an answer to an
1544 assertion in #assert, #unassert or #if (and therefore possibly
1545 whilst expanding macros). It therefore must not be used by any
1546 code that they might call: specifically the lexer and the guts of
1547 the macro expander.
1548
1549 All existing other uses clearly fit this restriction: storing
1550 registered pragmas during initialization. */
Neil Booth93c803682000-10-28 17:59:06 +00001551unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001552_cpp_aligned_alloc (cpp_reader *pfile, size_t len)
Neil Booth93c803682000-10-28 17:59:06 +00001553{
Neil Booth8c3b2692001-09-30 10:03:11 +00001554 _cpp_buff *buff = pfile->a_buff;
1555 unsigned char *result = buff->cur;
Neil Booth93c803682000-10-28 17:59:06 +00001556
Neil Booth8c3b2692001-09-30 10:03:11 +00001557 if (len > (size_t) (buff->limit - result))
Zack Weinberg041c3192000-07-04 01:58:21 +00001558 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001559 buff = _cpp_get_buff (pfile, len);
1560 buff->next = pfile->a_buff;
1561 pfile->a_buff = buff;
1562 result = buff->cur;
Zack Weinberg041c3192000-07-04 01:58:21 +00001563 }
1564
Neil Booth8c3b2692001-09-30 10:03:11 +00001565 buff->cur = result + len;
Neil Booth93c803682000-10-28 17:59:06 +00001566 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00001567}
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001568
1569/* Say which field of TOK is in use. */
1570
1571enum cpp_token_fld_kind
1572cpp_token_val_index (cpp_token *tok)
1573{
1574 switch (TOKEN_SPELL (tok))
1575 {
1576 case SPELL_IDENT:
1577 return CPP_TOKEN_FLD_NODE;
1578 case SPELL_LITERAL:
1579 return CPP_TOKEN_FLD_STR;
1580 case SPELL_NONE:
1581 if (tok->type == CPP_MACRO_ARG)
1582 return CPP_TOKEN_FLD_ARG_NO;
1583 else if (tok->type == CPP_PADDING)
1584 return CPP_TOKEN_FLD_SOURCE;
Zack Weinberg21b11492004-09-09 19:16:56 +00001585 else if (tok->type == CPP_PRAGMA)
1586 return CPP_TOKEN_FLD_STR;
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001587 /* else fall through */
1588 default:
1589 return CPP_TOKEN_FLD_NONE;
1590 }
1591}