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