]>
Commit | Line | Data |
---|---|---|
45b966db | 1 | /* CPP Library - lexical analysis. |
748086b7 JJ |
2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 |
3 | Free Software Foundation, Inc. | |
45b966db ZW |
4 | Contributed by Per Bothner, 1994-95. |
5 | Based on CCCP program by Paul Rubin, June 1986 | |
6 | Adapted to ANSI C, Richard Stallman, Jan 1987 | |
7 | Broken out to separate file, Zack Weinberg, Mar 2000 | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by the | |
748086b7 | 11 | Free Software Foundation; either version 3, or (at your option) any |
45b966db ZW |
12 | later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
748086b7 JJ |
20 | along with this program; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
45b966db ZW |
22 | |
23 | #include "config.h" | |
24 | #include "system.h" | |
45b966db | 25 | #include "cpplib.h" |
4f4e53dd | 26 | #include "internal.h" |
45b966db | 27 | |
93c80368 | 28 | enum spell_type |
f9a0e96c | 29 | { |
93c80368 | 30 | SPELL_OPERATOR = 0, |
93c80368 | 31 | SPELL_IDENT, |
6338b358 | 32 | SPELL_LITERAL, |
93c80368 | 33 | SPELL_NONE |
f9a0e96c ZW |
34 | }; |
35 | ||
93c80368 | 36 | struct token_spelling |
f9a0e96c | 37 | { |
93c80368 NB |
38 | enum spell_type category; |
39 | const unsigned char *name; | |
f9a0e96c ZW |
40 | }; |
41 | ||
8206c799 | 42 | static const unsigned char *const digraph_spellings[] = |
b6baa67d | 43 | { UC"%:", UC"%:%:", UC"<:", UC":>", UC"<%", UC"%>" }; |
93c80368 | 44 | |
b6baa67d KVH |
45 | #define OP(e, s) { SPELL_OPERATOR, UC s }, |
46 | #define TK(e, s) { SPELL_ ## s, UC #e }, | |
8206c799 | 47 | static const struct token_spelling token_spellings[N_TTYPES] = { TTYPE_TABLE }; |
93c80368 NB |
48 | #undef OP |
49 | #undef TK | |
50 | ||
51 | #define TOKEN_SPELL(token) (token_spellings[(token)->type].category) | |
52 | #define TOKEN_NAME(token) (token_spellings[(token)->type].name) | |
f2d5f0cc | 53 | |
6cf87ca4 ZW |
54 | static void add_line_note (cpp_buffer *, const uchar *, unsigned int); |
55 | static int skip_line_comment (cpp_reader *); | |
56 | static void skip_whitespace (cpp_reader *, cppchar_t); | |
6cf87ca4 ZW |
57 | static void lex_string (cpp_reader *, cpp_token *, const uchar *); |
58 | static void save_comment (cpp_reader *, cpp_token *, const uchar *, cppchar_t); | |
631d0d36 | 59 | static void store_comment (cpp_reader *, cpp_token *); |
6cf87ca4 ZW |
60 | static void create_literal (cpp_reader *, cpp_token *, const uchar *, |
61 | unsigned int, enum cpp_ttype); | |
62 | static bool warn_in_comment (cpp_reader *, _cpp_line_note *); | |
63 | static int name_p (cpp_reader *, const cpp_string *); | |
6cf87ca4 ZW |
64 | static tokenrun *next_tokenrun (tokenrun *); |
65 | ||
6cf87ca4 | 66 | static _cpp_buff *new_buff (size_t); |
15dad1d9 | 67 | |
9d10c9a9 | 68 | |
041c3194 | 69 | /* Utility routine: |
9e62c811 | 70 | |
bfb9dc7f ZW |
71 | Compares, the token TOKEN to the NUL-terminated string STRING. |
72 | TOKEN must be a CPP_NAME. Returns 1 for equal, 0 for unequal. */ | |
041c3194 | 73 | int |
6cf87ca4 | 74 | cpp_ideq (const cpp_token *token, const char *string) |
041c3194 | 75 | { |
bfb9dc7f | 76 | if (token->type != CPP_NAME) |
041c3194 | 77 | return 0; |
bfb9dc7f | 78 | |
9a0c6187 | 79 | return !ustrcmp (NODE_NAME (token->val.node.node), (const uchar *) string); |
15dad1d9 | 80 | } |
1368ee70 | 81 | |
26aea073 NB |
82 | /* Record a note TYPE at byte POS into the current cleaned logical |
83 | line. */ | |
87062813 | 84 | static void |
6cf87ca4 | 85 | add_line_note (cpp_buffer *buffer, const uchar *pos, unsigned int type) |
0d9f234d | 86 | { |
26aea073 NB |
87 | if (buffer->notes_used == buffer->notes_cap) |
88 | { | |
89 | buffer->notes_cap = buffer->notes_cap * 2 + 200; | |
c3f829c1 GDR |
90 | buffer->notes = XRESIZEVEC (_cpp_line_note, buffer->notes, |
91 | buffer->notes_cap); | |
26aea073 | 92 | } |
0d9f234d | 93 | |
26aea073 NB |
94 | buffer->notes[buffer->notes_used].pos = pos; |
95 | buffer->notes[buffer->notes_used].type = type; | |
96 | buffer->notes_used++; | |
0d9f234d NB |
97 | } |
98 | ||
26aea073 NB |
99 | /* Returns with a logical line that contains no escaped newlines or |
100 | trigraphs. This is a time-critical inner loop. */ | |
101 | void | |
6cf87ca4 | 102 | _cpp_clean_line (cpp_reader *pfile) |
45b966db | 103 | { |
26aea073 NB |
104 | cpp_buffer *buffer; |
105 | const uchar *s; | |
106 | uchar c, *d, *p; | |
87062813 | 107 | |
26aea073 NB |
108 | buffer = pfile->buffer; |
109 | buffer->cur_note = buffer->notes_used = 0; | |
110 | buffer->cur = buffer->line_base = buffer->next_line; | |
111 | buffer->need_line = false; | |
112 | s = buffer->next_line - 1; | |
87062813 | 113 | |
26aea073 | 114 | if (!buffer->from_stage3) |
45b966db | 115 | { |
7af45bd4 ILT |
116 | const uchar *pbackslash = NULL; |
117 | ||
d08dcf87 ZW |
118 | /* Short circuit for the common case of an un-escaped line with |
119 | no trigraphs. The primary win here is by not writing any | |
120 | data back to memory until we have to. */ | |
121 | for (;;) | |
122 | { | |
123 | c = *++s; | |
7af45bd4 ILT |
124 | if (__builtin_expect (c == '\n', false) |
125 | || __builtin_expect (c == '\r', false)) | |
d08dcf87 ZW |
126 | { |
127 | d = (uchar *) s; | |
128 | ||
7af45bd4 | 129 | if (__builtin_expect (s == buffer->rlimit, false)) |
d08dcf87 ZW |
130 | goto done; |
131 | ||
132 | /* DOS line ending? */ | |
7af45bd4 ILT |
133 | if (__builtin_expect (c == '\r', false) |
134 | && s[1] == '\n') | |
135 | { | |
136 | s++; | |
137 | if (s == buffer->rlimit) | |
138 | goto done; | |
139 | } | |
d08dcf87 | 140 | |
7af45bd4 | 141 | if (__builtin_expect (pbackslash == NULL, true)) |
d08dcf87 ZW |
142 | goto done; |
143 | ||
7af45bd4 | 144 | /* Check for escaped newline. */ |
d08dcf87 | 145 | p = d; |
7af45bd4 | 146 | while (is_nvspace (p[-1])) |
d08dcf87 | 147 | p--; |
7af45bd4 | 148 | if (p - 1 != pbackslash) |
d08dcf87 ZW |
149 | goto done; |
150 | ||
151 | /* Have an escaped newline; process it and proceed to | |
152 | the slow path. */ | |
153 | add_line_note (buffer, p - 1, p != d ? ' ' : '\\'); | |
154 | d = p - 2; | |
155 | buffer->next_line = p - 1; | |
156 | break; | |
157 | } | |
7af45bd4 ILT |
158 | if (__builtin_expect (c == '\\', false)) |
159 | pbackslash = s; | |
160 | else if (__builtin_expect (c == '?', false) | |
161 | && __builtin_expect (s[1] == '?', false) | |
162 | && _cpp_trigraph_map[s[2]]) | |
d08dcf87 ZW |
163 | { |
164 | /* Have a trigraph. We may or may not have to convert | |
165 | it. Add a line note regardless, for -Wtrigraphs. */ | |
166 | add_line_note (buffer, s, s[2]); | |
167 | if (CPP_OPTION (pfile, trigraphs)) | |
168 | { | |
169 | /* We do, and that means we have to switch to the | |
170 | slow path. */ | |
171 | d = (uchar *) s; | |
172 | *d = _cpp_trigraph_map[s[2]]; | |
173 | s += 2; | |
174 | break; | |
175 | } | |
176 | } | |
177 | } | |
178 | ||
26aea073 NB |
179 | |
180 | for (;;) | |
4a5b68a2 | 181 | { |
26aea073 NB |
182 | c = *++s; |
183 | *++d = c; | |
184 | ||
185 | if (c == '\n' || c == '\r') | |
186 | { | |
187 | /* Handle DOS line endings. */ | |
188 | if (c == '\r' && s != buffer->rlimit && s[1] == '\n') | |
189 | s++; | |
190 | if (s == buffer->rlimit) | |
191 | break; | |
192 | ||
193 | /* Escaped? */ | |
194 | p = d; | |
195 | while (p != buffer->next_line && is_nvspace (p[-1])) | |
196 | p--; | |
197 | if (p == buffer->next_line || p[-1] != '\\') | |
198 | break; | |
199 | ||
41c32c98 | 200 | add_line_note (buffer, p - 1, p != d ? ' ': '\\'); |
26aea073 NB |
201 | d = p - 2; |
202 | buffer->next_line = p - 1; | |
203 | } | |
204 | else if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]]) | |
205 | { | |
206 | /* Add a note regardless, for the benefit of -Wtrigraphs. */ | |
41c32c98 | 207 | add_line_note (buffer, d, s[2]); |
26aea073 NB |
208 | if (CPP_OPTION (pfile, trigraphs)) |
209 | { | |
210 | *d = _cpp_trigraph_map[s[2]]; | |
211 | s += 2; | |
212 | } | |
213 | } | |
4a5b68a2 | 214 | } |
45b966db | 215 | } |
26aea073 NB |
216 | else |
217 | { | |
218 | do | |
219 | s++; | |
220 | while (*s != '\n' && *s != '\r'); | |
221 | d = (uchar *) s; | |
222 | ||
223 | /* Handle DOS line endings. */ | |
224 | if (*s == '\r' && s != buffer->rlimit && s[1] == '\n') | |
225 | s++; | |
226 | } | |
0d9f234d | 227 | |
d08dcf87 | 228 | done: |
26aea073 | 229 | *d = '\n'; |
41c32c98 NB |
230 | /* A sentinel note that should never be processed. */ |
231 | add_line_note (buffer, d + 1, '\n'); | |
26aea073 | 232 | buffer->next_line = s + 1; |
45b966db ZW |
233 | } |
234 | ||
a8eb6044 NB |
235 | /* Return true if the trigraph indicated by NOTE should be warned |
236 | about in a comment. */ | |
237 | static bool | |
6cf87ca4 | 238 | warn_in_comment (cpp_reader *pfile, _cpp_line_note *note) |
a8eb6044 NB |
239 | { |
240 | const uchar *p; | |
241 | ||
242 | /* Within comments we don't warn about trigraphs, unless the | |
243 | trigraph forms an escaped newline, as that may change | |
6356f892 | 244 | behavior. */ |
a8eb6044 NB |
245 | if (note->type != '/') |
246 | return false; | |
247 | ||
248 | /* If -trigraphs, then this was an escaped newline iff the next note | |
249 | is coincident. */ | |
250 | if (CPP_OPTION (pfile, trigraphs)) | |
251 | return note[1].pos == note->pos; | |
252 | ||
253 | /* Otherwise, see if this forms an escaped newline. */ | |
254 | p = note->pos + 3; | |
255 | while (is_nvspace (*p)) | |
256 | p++; | |
257 | ||
258 | /* There might have been escaped newlines between the trigraph and the | |
259 | newline we found. Hence the position test. */ | |
260 | return (*p == '\n' && p < note[1].pos); | |
261 | } | |
262 | ||
26aea073 NB |
263 | /* Process the notes created by add_line_note as far as the current |
264 | location. */ | |
265 | void | |
6cf87ca4 | 266 | _cpp_process_line_notes (cpp_reader *pfile, int in_comment) |
45b966db | 267 | { |
29401c30 NB |
268 | cpp_buffer *buffer = pfile->buffer; |
269 | ||
26aea073 | 270 | for (;;) |
041c3194 | 271 | { |
26aea073 NB |
272 | _cpp_line_note *note = &buffer->notes[buffer->cur_note]; |
273 | unsigned int col; | |
a5c3cccd | 274 | |
26aea073 NB |
275 | if (note->pos > buffer->cur) |
276 | break; | |
a5c3cccd | 277 | |
26aea073 NB |
278 | buffer->cur_note++; |
279 | col = CPP_BUF_COLUMN (buffer, note->pos + 1); | |
4d6baafa | 280 | |
41c32c98 | 281 | if (note->type == '\\' || note->type == ' ') |
26aea073 | 282 | { |
41c32c98 | 283 | if (note->type == ' ' && !in_comment) |
500bee0a | 284 | cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col, |
26aea073 | 285 | "backslash and newline separated by space"); |
41c32c98 | 286 | |
26aea073 | 287 | if (buffer->next_line > buffer->rlimit) |
87062813 | 288 | { |
500bee0a | 289 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, col, |
26aea073 NB |
290 | "backslash-newline at end of file"); |
291 | /* Prevent "no newline at end of file" warning. */ | |
292 | buffer->next_line = buffer->rlimit; | |
87062813 | 293 | } |
26aea073 NB |
294 | |
295 | buffer->line_base = note->pos; | |
12f9df4e | 296 | CPP_INCREMENT_LINE (pfile, 0); |
0d9f234d | 297 | } |
41c32c98 NB |
298 | else if (_cpp_trigraph_map[note->type]) |
299 | { | |
a8eb6044 NB |
300 | if (CPP_OPTION (pfile, warn_trigraphs) |
301 | && (!in_comment || warn_in_comment (pfile, note))) | |
41c32c98 NB |
302 | { |
303 | if (CPP_OPTION (pfile, trigraphs)) | |
87cf0651 SB |
304 | cpp_warning_with_line (pfile, CPP_W_TRIGRAPHS, |
305 | pfile->line_table->highest_line, col, | |
306 | "trigraph ??%c converted to %c", | |
307 | note->type, | |
308 | (int) _cpp_trigraph_map[note->type]); | |
41c32c98 | 309 | else |
905bd7b5 | 310 | { |
87cf0651 SB |
311 | cpp_warning_with_line |
312 | (pfile, CPP_W_TRIGRAPHS, | |
313 | pfile->line_table->highest_line, col, | |
905bd7b5 GK |
314 | "trigraph ??%c ignored, use -trigraphs to enable", |
315 | note->type); | |
316 | } | |
41c32c98 NB |
317 | } |
318 | } | |
00a81b8b JM |
319 | else if (note->type == 0) |
320 | /* Already processed in lex_raw_string. */; | |
41c32c98 NB |
321 | else |
322 | abort (); | |
041c3194 | 323 | } |
45b966db ZW |
324 | } |
325 | ||
0d9f234d NB |
326 | /* Skip a C-style block comment. We find the end of the comment by |
327 | seeing if an asterisk is before every '/' we encounter. Returns | |
6f572ac2 NB |
328 | nonzero if comment terminated by EOF, zero otherwise. |
329 | ||
330 | Buffer->cur points to the initial asterisk of the comment. */ | |
26aea073 | 331 | bool |
6cf87ca4 | 332 | _cpp_skip_block_comment (cpp_reader *pfile) |
45b966db | 333 | { |
041c3194 | 334 | cpp_buffer *buffer = pfile->buffer; |
d08dcf87 ZW |
335 | const uchar *cur = buffer->cur; |
336 | uchar c; | |
0d9f234d | 337 | |
d08dcf87 ZW |
338 | cur++; |
339 | if (*cur == '/') | |
340 | cur++; | |
0d9f234d | 341 | |
26aea073 NB |
342 | for (;;) |
343 | { | |
0d9f234d NB |
344 | /* People like decorating comments with '*', so check for '/' |
345 | instead for efficiency. */ | |
d08dcf87 ZW |
346 | c = *cur++; |
347 | ||
041c3194 | 348 | if (c == '/') |
45b966db | 349 | { |
d08dcf87 | 350 | if (cur[-2] == '*') |
0d9f234d | 351 | break; |
041c3194 | 352 | |
0d9f234d | 353 | /* Warn about potential nested comments, but not if the '/' |
a1f300c0 | 354 | comes immediately before the true comment delimiter. |
041c3194 | 355 | Don't bother to get it right across escaped newlines. */ |
0d9f234d | 356 | if (CPP_OPTION (pfile, warn_comments) |
d08dcf87 ZW |
357 | && cur[0] == '*' && cur[1] != '/') |
358 | { | |
359 | buffer->cur = cur; | |
87cf0651 SB |
360 | cpp_warning_with_line (pfile, CPP_W_COMMENTS, |
361 | pfile->line_table->highest_line, | |
362 | CPP_BUF_COL (buffer), | |
363 | "\"/*\" within comment"); | |
d08dcf87 | 364 | } |
45b966db | 365 | } |
26aea073 NB |
366 | else if (c == '\n') |
367 | { | |
12f9df4e | 368 | unsigned int cols; |
d08dcf87 | 369 | buffer->cur = cur - 1; |
26aea073 NB |
370 | _cpp_process_line_notes (pfile, true); |
371 | if (buffer->next_line >= buffer->rlimit) | |
372 | return true; | |
373 | _cpp_clean_line (pfile); | |
12f9df4e PB |
374 | |
375 | cols = buffer->next_line - buffer->line_base; | |
376 | CPP_INCREMENT_LINE (pfile, cols); | |
377 | ||
d08dcf87 | 378 | cur = buffer->cur; |
26aea073 | 379 | } |
45b966db | 380 | } |
041c3194 | 381 | |
d08dcf87 | 382 | buffer->cur = cur; |
a8eb6044 | 383 | _cpp_process_line_notes (pfile, true); |
26aea073 | 384 | return false; |
45b966db ZW |
385 | } |
386 | ||
480709cc | 387 | /* Skip a C++ line comment, leaving buffer->cur pointing to the |
da7d8304 | 388 | terminating newline. Handles escaped newlines. Returns nonzero |
480709cc | 389 | if a multiline comment. */ |
041c3194 | 390 | static int |
6cf87ca4 | 391 | skip_line_comment (cpp_reader *pfile) |
45b966db | 392 | { |
cbcff6df | 393 | cpp_buffer *buffer = pfile->buffer; |
1bb64668 | 394 | source_location orig_line = pfile->line_table->highest_line; |
041c3194 | 395 | |
26aea073 NB |
396 | while (*buffer->cur != '\n') |
397 | buffer->cur++; | |
480709cc | 398 | |
26aea073 | 399 | _cpp_process_line_notes (pfile, true); |
500bee0a | 400 | return orig_line != pfile->line_table->highest_line; |
041c3194 | 401 | } |
45b966db | 402 | |
26aea073 | 403 | /* Skips whitespace, saving the next non-whitespace character. */ |
52fadca8 | 404 | static void |
6cf87ca4 | 405 | skip_whitespace (cpp_reader *pfile, cppchar_t c) |
041c3194 ZW |
406 | { |
407 | cpp_buffer *buffer = pfile->buffer; | |
f7d151fb | 408 | bool saw_NUL = false; |
45b966db | 409 | |
0d9f234d | 410 | do |
041c3194 | 411 | { |
91fcd158 | 412 | /* Horizontal space always OK. */ |
26aea073 | 413 | if (c == ' ' || c == '\t') |
0d9f234d | 414 | ; |
0d9f234d | 415 | /* Just \f \v or \0 left. */ |
91fcd158 | 416 | else if (c == '\0') |
f7d151fb | 417 | saw_NUL = true; |
93c80368 | 418 | else if (pfile->state.in_directive && CPP_PEDANTIC (pfile)) |
500bee0a | 419 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, |
ebef4e8c NB |
420 | CPP_BUF_COL (buffer), |
421 | "%s in preprocessing directive", | |
422 | c == '\f' ? "form feed" : "vertical tab"); | |
0d9f234d | 423 | |
0d9f234d | 424 | c = *buffer->cur++; |
45b966db | 425 | } |
ec5c56db | 426 | /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */ |
0d9f234d NB |
427 | while (is_nvspace (c)); |
428 | ||
f7d151fb | 429 | if (saw_NUL) |
0527bc4e | 430 | cpp_error (pfile, CPP_DL_WARNING, "null character(s) ignored"); |
f7d151fb | 431 | |
480709cc | 432 | buffer->cur--; |
041c3194 | 433 | } |
45b966db | 434 | |
93c80368 NB |
435 | /* See if the characters of a number token are valid in a name (no |
436 | '.', '+' or '-'). */ | |
437 | static int | |
6cf87ca4 | 438 | name_p (cpp_reader *pfile, const cpp_string *string) |
93c80368 NB |
439 | { |
440 | unsigned int i; | |
441 | ||
442 | for (i = 0; i < string->len; i++) | |
443 | if (!is_idchar (string->text[i])) | |
444 | return 0; | |
445 | ||
df383483 | 446 | return 1; |
93c80368 NB |
447 | } |
448 | ||
50668cf6 GK |
449 | /* After parsing an identifier or other sequence, produce a warning about |
450 | sequences not in NFC/NFKC. */ | |
451 | static void | |
452 | warn_about_normalization (cpp_reader *pfile, | |
453 | const cpp_token *token, | |
454 | const struct normalize_state *s) | |
455 | { | |
456 | if (CPP_OPTION (pfile, warn_normalize) < NORMALIZE_STATE_RESULT (s) | |
457 | && !pfile->state.skipping) | |
458 | { | |
459 | /* Make sure that the token is printed using UCNs, even | |
460 | if we'd otherwise happily print UTF-8. */ | |
c3f829c1 | 461 | unsigned char *buf = XNEWVEC (unsigned char, cpp_token_len (token)); |
50668cf6 GK |
462 | size_t sz; |
463 | ||
464 | sz = cpp_spell_token (pfile, token, buf, false) - buf; | |
465 | if (NORMALIZE_STATE_RESULT (s) == normalized_C) | |
87cf0651 SB |
466 | cpp_warning_with_line (pfile, CPP_W_NORMALIZE, token->src_loc, 0, |
467 | "`%.*s' is not in NFKC", (int) sz, buf); | |
50668cf6 | 468 | else |
87cf0651 SB |
469 | cpp_warning_with_line (pfile, CPP_W_NORMALIZE, token->src_loc, 0, |
470 | "`%.*s' is not in NFC", (int) sz, buf); | |
50668cf6 GK |
471 | } |
472 | } | |
473 | ||
bced6edf | 474 | /* Returns TRUE if the sequence starting at buffer->cur is invalid in |
1613e52b | 475 | an identifier. FIRST is TRUE if this starts an identifier. */ |
bced6edf | 476 | static bool |
50668cf6 GK |
477 | forms_identifier_p (cpp_reader *pfile, int first, |
478 | struct normalize_state *state) | |
bced6edf | 479 | { |
1613e52b NB |
480 | cpp_buffer *buffer = pfile->buffer; |
481 | ||
482 | if (*buffer->cur == '$') | |
483 | { | |
484 | if (!CPP_OPTION (pfile, dollars_in_ident)) | |
485 | return false; | |
486 | ||
487 | buffer->cur++; | |
78b8811a | 488 | if (CPP_OPTION (pfile, warn_dollars) && !pfile->state.skipping) |
1613e52b | 489 | { |
78b8811a | 490 | CPP_OPTION (pfile, warn_dollars) = 0; |
0527bc4e | 491 | cpp_error (pfile, CPP_DL_PEDWARN, "'$' in identifier or number"); |
1613e52b NB |
492 | } |
493 | ||
494 | return true; | |
495 | } | |
bced6edf | 496 | |
1613e52b | 497 | /* Is this a syntactically valid UCN? */ |
af15a2fe | 498 | if (CPP_OPTION (pfile, extended_identifiers) |
6baba9bb | 499 | && *buffer->cur == '\\' |
1613e52b | 500 | && (buffer->cur[1] == 'u' || buffer->cur[1] == 'U')) |
bced6edf | 501 | { |
1613e52b | 502 | buffer->cur += 2; |
50668cf6 GK |
503 | if (_cpp_valid_ucn (pfile, &buffer->cur, buffer->rlimit, 1 + !first, |
504 | state)) | |
1613e52b NB |
505 | return true; |
506 | buffer->cur -= 2; | |
bced6edf | 507 | } |
bced6edf | 508 | |
1613e52b | 509 | return false; |
bced6edf NB |
510 | } |
511 | ||
17e7cb85 KT |
512 | /* Helper function to get the cpp_hashnode of the identifier BASE. */ |
513 | static cpp_hashnode * | |
514 | lex_identifier_intern (cpp_reader *pfile, const uchar *base) | |
515 | { | |
516 | cpp_hashnode *result; | |
517 | const uchar *cur; | |
518 | unsigned int len; | |
519 | unsigned int hash = HT_HASHSTEP (0, *base); | |
520 | ||
521 | cur = base + 1; | |
522 | while (ISIDNUM (*cur)) | |
523 | { | |
524 | hash = HT_HASHSTEP (hash, *cur); | |
525 | cur++; | |
526 | } | |
527 | len = cur - base; | |
528 | hash = HT_HASHFINISH (hash, len); | |
529 | result = CPP_HASHNODE (ht_lookup_with_hash (pfile->hash_table, | |
530 | base, len, hash, HT_ALLOC)); | |
531 | ||
532 | /* Rarely, identifiers require diagnostics when lexed. */ | |
533 | if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC) | |
534 | && !pfile->state.skipping, 0)) | |
535 | { | |
536 | /* It is allowed to poison the same identifier twice. */ | |
537 | if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok) | |
538 | cpp_error (pfile, CPP_DL_ERROR, "attempt to use poisoned \"%s\"", | |
539 | NODE_NAME (result)); | |
540 | ||
541 | /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the | |
542 | replacement list of a variadic macro. */ | |
543 | if (result == pfile->spec_nodes.n__VA_ARGS__ | |
544 | && !pfile->state.va_args_ok) | |
545 | cpp_error (pfile, CPP_DL_PEDWARN, | |
546 | "__VA_ARGS__ can only appear in the expansion" | |
547 | " of a C99 variadic macro"); | |
548 | ||
549 | /* For -Wc++-compat, warn about use of C++ named operators. */ | |
550 | if (result->flags & NODE_WARN_OPERATOR) | |
87cf0651 SB |
551 | cpp_warning (pfile, CPP_W_CXX_OPERATOR_NAMES, |
552 | "identifier \"%s\" is a special operator name in C++", | |
553 | NODE_NAME (result)); | |
17e7cb85 KT |
554 | } |
555 | ||
556 | return result; | |
557 | } | |
558 | ||
559 | /* Get the cpp_hashnode of an identifier specified by NAME in | |
560 | the current cpp_reader object. If none is found, NULL is returned. */ | |
561 | cpp_hashnode * | |
562 | _cpp_lex_identifier (cpp_reader *pfile, const char *name) | |
563 | { | |
564 | cpp_hashnode *result; | |
565 | result = lex_identifier_intern (pfile, (uchar *) name); | |
566 | return result; | |
567 | } | |
568 | ||
bced6edf | 569 | /* Lex an identifier starting at BUFFER->CUR - 1. */ |
0d9f234d | 570 | static cpp_hashnode * |
50668cf6 GK |
571 | lex_identifier (cpp_reader *pfile, const uchar *base, bool starts_ucn, |
572 | struct normalize_state *nst) | |
45b966db | 573 | { |
93c80368 | 574 | cpp_hashnode *result; |
47e20491 | 575 | const uchar *cur; |
c6e83800 ZW |
576 | unsigned int len; |
577 | unsigned int hash = HT_HASHSTEP (0, *base); | |
2c3fcba6 | 578 | |
c6e83800 | 579 | cur = pfile->buffer->cur; |
47e20491 GK |
580 | if (! starts_ucn) |
581 | while (ISIDNUM (*cur)) | |
582 | { | |
583 | hash = HT_HASHSTEP (hash, *cur); | |
584 | cur++; | |
585 | } | |
586 | pfile->buffer->cur = cur; | |
50668cf6 | 587 | if (starts_ucn || forms_identifier_p (pfile, false, nst)) |
10cf9bde | 588 | { |
47e20491 GK |
589 | /* Slower version for identifiers containing UCNs (or $). */ |
590 | do { | |
591 | while (ISIDNUM (*pfile->buffer->cur)) | |
50668cf6 GK |
592 | { |
593 | pfile->buffer->cur++; | |
594 | NORMALIZE_STATE_UPDATE_IDNUM (nst); | |
595 | } | |
596 | } while (forms_identifier_p (pfile, false, nst)); | |
47e20491 GK |
597 | result = _cpp_interpret_identifier (pfile, base, |
598 | pfile->buffer->cur - base); | |
2c3fcba6 | 599 | } |
47e20491 GK |
600 | else |
601 | { | |
602 | len = cur - base; | |
603 | hash = HT_HASHFINISH (hash, len); | |
bced6edf | 604 | |
2bf41bf0 TT |
605 | result = CPP_HASHNODE (ht_lookup_with_hash (pfile->hash_table, |
606 | base, len, hash, HT_ALLOC)); | |
47e20491 | 607 | } |
2c3fcba6 | 608 | |
bced6edf | 609 | /* Rarely, identifiers require diagnostics when lexed. */ |
2c3fcba6 ZW |
610 | if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC) |
611 | && !pfile->state.skipping, 0)) | |
612 | { | |
613 | /* It is allowed to poison the same identifier twice. */ | |
614 | if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok) | |
0527bc4e | 615 | cpp_error (pfile, CPP_DL_ERROR, "attempt to use poisoned \"%s\"", |
2c3fcba6 ZW |
616 | NODE_NAME (result)); |
617 | ||
618 | /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the | |
619 | replacement list of a variadic macro. */ | |
620 | if (result == pfile->spec_nodes.n__VA_ARGS__ | |
621 | && !pfile->state.va_args_ok) | |
0527bc4e | 622 | cpp_error (pfile, CPP_DL_PEDWARN, |
6cf87ca4 ZW |
623 | "__VA_ARGS__ can only appear in the expansion" |
624 | " of a C99 variadic macro"); | |
3d8b2a98 ILT |
625 | |
626 | /* For -Wc++-compat, warn about use of C++ named operators. */ | |
627 | if (result->flags & NODE_WARN_OPERATOR) | |
87cf0651 SB |
628 | cpp_warning (pfile, CPP_W_CXX_OPERATOR_NAMES, |
629 | "identifier \"%s\" is a special operator name in C++", | |
630 | NODE_NAME (result)); | |
2c3fcba6 ZW |
631 | } |
632 | ||
633 | return result; | |
634 | } | |
635 | ||
bced6edf | 636 | /* Lex a number to NUMBER starting at BUFFER->CUR - 1. */ |
45b966db | 637 | static void |
50668cf6 GK |
638 | lex_number (cpp_reader *pfile, cpp_string *number, |
639 | struct normalize_state *nst) | |
45b966db | 640 | { |
562a5c27 | 641 | const uchar *cur; |
bced6edf NB |
642 | const uchar *base; |
643 | uchar *dest; | |
45b966db | 644 | |
bced6edf NB |
645 | base = pfile->buffer->cur - 1; |
646 | do | |
041c3194 | 647 | { |
bced6edf | 648 | cur = pfile->buffer->cur; |
0d9f234d | 649 | |
bced6edf NB |
650 | /* N.B. ISIDNUM does not include $. */ |
651 | while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1])) | |
50668cf6 GK |
652 | { |
653 | cur++; | |
654 | NORMALIZE_STATE_UPDATE_IDNUM (nst); | |
655 | } | |
45b966db | 656 | |
10cf9bde | 657 | pfile->buffer->cur = cur; |
45b966db | 658 | } |
50668cf6 | 659 | while (forms_identifier_p (pfile, false, nst)); |
93c80368 | 660 | |
bced6edf NB |
661 | number->len = cur - base; |
662 | dest = _cpp_unaligned_alloc (pfile, number->len + 1); | |
663 | memcpy (dest, base, number->len); | |
664 | dest[number->len] = '\0'; | |
665 | number->text = dest; | |
93c80368 NB |
666 | } |
667 | ||
6338b358 NB |
668 | /* Create a token of type TYPE with a literal spelling. */ |
669 | static void | |
6cf87ca4 ZW |
670 | create_literal (cpp_reader *pfile, cpp_token *token, const uchar *base, |
671 | unsigned int len, enum cpp_ttype type) | |
6338b358 NB |
672 | { |
673 | uchar *dest = _cpp_unaligned_alloc (pfile, len + 1); | |
674 | ||
675 | memcpy (dest, base, len); | |
676 | dest[len] = '\0'; | |
677 | token->type = type; | |
678 | token->val.str.len = len; | |
679 | token->val.str.text = dest; | |
680 | } | |
681 | ||
00a81b8b JM |
682 | /* Subroutine of lex_raw_string: Append LEN chars from BASE to the buffer |
683 | sequence from *FIRST_BUFF_P to LAST_BUFF_P. */ | |
684 | ||
685 | static void | |
686 | bufring_append (cpp_reader *pfile, const uchar *base, size_t len, | |
687 | _cpp_buff **first_buff_p, _cpp_buff **last_buff_p) | |
688 | { | |
689 | _cpp_buff *first_buff = *first_buff_p; | |
690 | _cpp_buff *last_buff = *last_buff_p; | |
691 | ||
692 | if (first_buff == NULL) | |
693 | first_buff = last_buff = _cpp_get_buff (pfile, len); | |
694 | else if (len > BUFF_ROOM (last_buff)) | |
695 | { | |
696 | size_t room = BUFF_ROOM (last_buff); | |
697 | memcpy (BUFF_FRONT (last_buff), base, room); | |
698 | BUFF_FRONT (last_buff) += room; | |
699 | base += room; | |
700 | len -= room; | |
701 | last_buff = _cpp_append_extend_buff (pfile, last_buff, len); | |
702 | } | |
703 | ||
704 | memcpy (BUFF_FRONT (last_buff), base, len); | |
705 | BUFF_FRONT (last_buff) += len; | |
706 | ||
707 | *first_buff_p = first_buff; | |
708 | *last_buff_p = last_buff; | |
709 | } | |
710 | ||
2c6e3f55 | 711 | /* Lexes a raw string. The stored string contains the spelling, including |
00a81b8b | 712 | double quotes, delimiter string, '(' and ')', any leading |
2c6e3f55 JJ |
713 | 'L', 'u', 'U' or 'u8' and 'R' modifier. It returns the type of the |
714 | literal, or CPP_OTHER if it was not properly terminated. | |
715 | ||
716 | The spelling is NUL-terminated, but it is not guaranteed that this | |
717 | is the first NUL since embedded NULs are preserved. */ | |
718 | ||
719 | static void | |
720 | lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base, | |
721 | const uchar *cur) | |
722 | { | |
723 | source_location saw_NUL = 0; | |
724 | const uchar *raw_prefix; | |
725 | unsigned int raw_prefix_len = 0; | |
726 | enum cpp_ttype type; | |
727 | size_t total_len = 0; | |
728 | _cpp_buff *first_buff = NULL, *last_buff = NULL; | |
00a81b8b | 729 | _cpp_line_note *note = &pfile->buffer->notes[pfile->buffer->cur_note]; |
2c6e3f55 JJ |
730 | |
731 | type = (*base == 'L' ? CPP_WSTRING : | |
732 | *base == 'U' ? CPP_STRING32 : | |
733 | *base == 'u' ? (base[1] == '8' ? CPP_UTF8STRING : CPP_STRING16) | |
734 | : CPP_STRING); | |
735 | ||
736 | raw_prefix = cur + 1; | |
737 | while (raw_prefix_len < 16) | |
738 | { | |
739 | switch (raw_prefix[raw_prefix_len]) | |
740 | { | |
52150625 | 741 | case ' ': case '(': case ')': case '\\': case '\t': |
2c6e3f55 JJ |
742 | case '\v': case '\f': case '\n': default: |
743 | break; | |
744 | /* Basic source charset except the above chars. */ | |
745 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | |
746 | case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': | |
747 | case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': | |
748 | case 's': case 't': case 'u': case 'v': case 'w': case 'x': | |
749 | case 'y': case 'z': | |
750 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | |
751 | case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': | |
752 | case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': | |
753 | case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': | |
754 | case 'Y': case 'Z': | |
755 | case '0': case '1': case '2': case '3': case '4': case '5': | |
756 | case '6': case '7': case '8': case '9': | |
52150625 | 757 | case '_': case '{': case '}': case '#': case '[': case ']': |
2c6e3f55 JJ |
758 | case '<': case '>': case '%': case ':': case ';': case '.': |
759 | case '?': case '*': case '+': case '-': case '/': case '^': | |
760 | case '&': case '|': case '~': case '!': case '=': case ',': | |
52150625 | 761 | case '"': case '\'': |
2c6e3f55 JJ |
762 | raw_prefix_len++; |
763 | continue; | |
764 | } | |
765 | break; | |
766 | } | |
767 | ||
52150625 | 768 | if (raw_prefix[raw_prefix_len] != '(') |
2c6e3f55 JJ |
769 | { |
770 | int col = CPP_BUF_COLUMN (pfile->buffer, raw_prefix + raw_prefix_len) | |
771 | + 1; | |
772 | if (raw_prefix_len == 16) | |
773 | cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, col, | |
774 | "raw string delimiter longer than 16 characters"); | |
775 | else | |
776 | cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, col, | |
777 | "invalid character '%c' in raw string delimiter", | |
778 | (int) raw_prefix[raw_prefix_len]); | |
779 | pfile->buffer->cur = raw_prefix - 1; | |
780 | create_literal (pfile, token, base, raw_prefix - 1 - base, CPP_OTHER); | |
781 | return; | |
782 | } | |
783 | ||
784 | cur = raw_prefix + raw_prefix_len + 1; | |
785 | for (;;) | |
786 | { | |
00a81b8b JM |
787 | #define BUF_APPEND(STR,LEN) \ |
788 | do { \ | |
789 | bufring_append (pfile, (const uchar *)(STR), (LEN), \ | |
790 | &first_buff, &last_buff); \ | |
791 | total_len += (LEN); \ | |
792 | } while (0); | |
793 | ||
794 | cppchar_t c; | |
795 | ||
796 | /* If we previously performed any trigraph or line splicing | |
797 | transformations, undo them within the body of the raw string. */ | |
798 | while (note->pos < cur) | |
799 | ++note; | |
800 | for (; note->pos == cur; ++note) | |
801 | { | |
802 | switch (note->type) | |
803 | { | |
804 | case '\\': | |
805 | case ' ': | |
806 | /* Restore backslash followed by newline. */ | |
807 | BUF_APPEND (base, cur - base); | |
808 | base = cur; | |
809 | BUF_APPEND ("\\", 1); | |
810 | after_backslash: | |
811 | if (note->type == ' ') | |
812 | { | |
813 | /* GNU backslash whitespace newline extension. FIXME | |
814 | could be any sequence of non-vertical space. When we | |
815 | can properly restore any such sequence, we should mark | |
816 | this note as handled so _cpp_process_line_notes | |
817 | doesn't warn. */ | |
818 | BUF_APPEND (" ", 1); | |
819 | } | |
820 | ||
821 | BUF_APPEND ("\n", 1); | |
822 | break; | |
823 | ||
824 | case 0: | |
825 | /* Already handled. */ | |
826 | break; | |
827 | ||
828 | default: | |
829 | if (_cpp_trigraph_map[note->type]) | |
830 | { | |
831 | /* Don't warn about this trigraph in | |
832 | _cpp_process_line_notes, since trigraphs show up as | |
833 | trigraphs in raw strings. */ | |
d947ada0 | 834 | uchar type = note->type; |
00a81b8b JM |
835 | note->type = 0; |
836 | ||
837 | if (!CPP_OPTION (pfile, trigraphs)) | |
838 | /* If we didn't convert the trigraph in the first | |
839 | place, don't do anything now either. */ | |
840 | break; | |
841 | ||
842 | BUF_APPEND (base, cur - base); | |
843 | base = cur; | |
844 | BUF_APPEND ("??", 2); | |
845 | ||
846 | /* ??/ followed by newline gets two line notes, one for | |
847 | the trigraph and one for the backslash/newline. */ | |
848 | if (type == '/' && note[1].pos == cur) | |
849 | { | |
850 | if (note[1].type != '\\' | |
851 | && note[1].type != ' ') | |
852 | abort (); | |
853 | BUF_APPEND ("/", 1); | |
854 | ++note; | |
855 | goto after_backslash; | |
856 | } | |
857 | /* The ) from ??) could be part of the suffix. */ | |
858 | else if (type == ')' | |
859 | && strncmp ((const char *) cur+1, | |
860 | (const char *) raw_prefix, | |
861 | raw_prefix_len) == 0 | |
862 | && cur[raw_prefix_len+1] == '"') | |
863 | { | |
864 | cur += raw_prefix_len+2; | |
865 | goto break_outer_loop; | |
866 | } | |
867 | else | |
868 | { | |
869 | /* Skip the replacement character. */ | |
870 | base = ++cur; | |
871 | BUF_APPEND (&type, 1); | |
872 | } | |
873 | } | |
874 | else | |
875 | abort (); | |
876 | break; | |
877 | } | |
878 | } | |
879 | c = *cur++; | |
2c6e3f55 | 880 | |
52150625 | 881 | if (c == ')' |
2c6e3f55 JJ |
882 | && strncmp ((const char *) cur, (const char *) raw_prefix, |
883 | raw_prefix_len) == 0 | |
884 | && cur[raw_prefix_len] == '"') | |
885 | { | |
886 | cur += raw_prefix_len + 1; | |
887 | break; | |
888 | } | |
889 | else if (c == '\n') | |
890 | { | |
891 | if (pfile->state.in_directive | |
892 | || pfile->state.parsing_args | |
893 | || pfile->state.in_deferred_pragma) | |
894 | { | |
895 | cur--; | |
896 | type = CPP_OTHER; | |
897 | cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, 0, | |
898 | "unterminated raw string"); | |
899 | break; | |
900 | } | |
901 | ||
00a81b8b | 902 | BUF_APPEND (base, cur - base); |
2c6e3f55 JJ |
903 | |
904 | if (pfile->buffer->cur < pfile->buffer->rlimit) | |
905 | CPP_INCREMENT_LINE (pfile, 0); | |
906 | pfile->buffer->need_line = true; | |
907 | ||
00a81b8b JM |
908 | pfile->buffer->cur = cur-1; |
909 | _cpp_process_line_notes (pfile, false); | |
2c6e3f55 JJ |
910 | if (!_cpp_get_fresh_line (pfile)) |
911 | { | |
912 | source_location src_loc = token->src_loc; | |
913 | token->type = CPP_EOF; | |
914 | /* Tell the compiler the line number of the EOF token. */ | |
915 | token->src_loc = pfile->line_table->highest_line; | |
916 | token->flags = BOL; | |
917 | if (first_buff != NULL) | |
918 | _cpp_release_buff (pfile, first_buff); | |
919 | cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0, | |
920 | "unterminated raw string"); | |
921 | return; | |
922 | } | |
923 | ||
924 | cur = base = pfile->buffer->cur; | |
00a81b8b | 925 | note = &pfile->buffer->notes[pfile->buffer->cur_note]; |
2c6e3f55 JJ |
926 | } |
927 | else if (c == '\0' && !saw_NUL) | |
928 | LINEMAP_POSITION_FOR_COLUMN (saw_NUL, pfile->line_table, | |
929 | CPP_BUF_COLUMN (pfile->buffer, cur)); | |
930 | } | |
00a81b8b | 931 | break_outer_loop: |
2c6e3f55 JJ |
932 | |
933 | if (saw_NUL && !pfile->state.skipping) | |
934 | cpp_error_with_line (pfile, CPP_DL_WARNING, saw_NUL, 0, | |
935 | "null character(s) preserved in literal"); | |
936 | ||
937 | pfile->buffer->cur = cur; | |
938 | if (first_buff == NULL) | |
939 | create_literal (pfile, token, base, cur - base, type); | |
940 | else | |
941 | { | |
942 | uchar *dest = _cpp_unaligned_alloc (pfile, total_len + (cur - base) + 1); | |
943 | ||
944 | token->type = type; | |
945 | token->val.str.len = total_len + (cur - base); | |
946 | token->val.str.text = dest; | |
947 | last_buff = first_buff; | |
948 | while (last_buff != NULL) | |
949 | { | |
950 | memcpy (dest, last_buff->base, | |
951 | BUFF_FRONT (last_buff) - last_buff->base); | |
952 | dest += BUFF_FRONT (last_buff) - last_buff->base; | |
953 | last_buff = last_buff->next; | |
954 | } | |
955 | _cpp_release_buff (pfile, first_buff); | |
956 | memcpy (dest, base, cur - base); | |
957 | dest[cur - base] = '\0'; | |
958 | } | |
959 | } | |
960 | ||
bced6edf | 961 | /* Lexes a string, character constant, or angle-bracketed header file |
6338b358 | 962 | name. The stored string contains the spelling, including opening |
2c6e3f55 JJ |
963 | quote and any leading 'L', 'u', 'U' or 'u8' and optional |
964 | 'R' modifier. It returns the type of the literal, or CPP_OTHER | |
965 | if it was not properly terminated, or CPP_LESS for an unterminated | |
966 | header name which must be relexed as normal tokens. | |
6338b358 NB |
967 | |
968 | The spelling is NUL-terminated, but it is not guaranteed that this | |
969 | is the first NUL since embedded NULs are preserved. */ | |
041c3194 | 970 | static void |
6cf87ca4 | 971 | lex_string (cpp_reader *pfile, cpp_token *token, const uchar *base) |
45b966db | 972 | { |
6338b358 NB |
973 | bool saw_NUL = false; |
974 | const uchar *cur; | |
bced6edf | 975 | cppchar_t terminator; |
6338b358 NB |
976 | enum cpp_ttype type; |
977 | ||
978 | cur = base; | |
979 | terminator = *cur++; | |
2c6e3f55 | 980 | if (terminator == 'L' || terminator == 'U') |
6338b358 | 981 | terminator = *cur++; |
2c6e3f55 JJ |
982 | else if (terminator == 'u') |
983 | { | |
984 | terminator = *cur++; | |
985 | if (terminator == '8') | |
986 | terminator = *cur++; | |
987 | } | |
988 | if (terminator == 'R') | |
989 | { | |
990 | lex_raw_string (pfile, token, base, cur); | |
991 | return; | |
992 | } | |
993 | if (terminator == '"') | |
b6baa67d KVH |
994 | type = (*base == 'L' ? CPP_WSTRING : |
995 | *base == 'U' ? CPP_STRING32 : | |
2c6e3f55 JJ |
996 | *base == 'u' ? (base[1] == '8' ? CPP_UTF8STRING : CPP_STRING16) |
997 | : CPP_STRING); | |
6338b358 | 998 | else if (terminator == '\'') |
b6baa67d KVH |
999 | type = (*base == 'L' ? CPP_WCHAR : |
1000 | *base == 'U' ? CPP_CHAR32 : | |
1001 | *base == 'u' ? CPP_CHAR16 : CPP_CHAR); | |
6338b358 NB |
1002 | else |
1003 | terminator = '>', type = CPP_HEADER_NAME; | |
93c80368 | 1004 | |
0d9f234d | 1005 | for (;;) |
45b966db | 1006 | { |
6338b358 | 1007 | cppchar_t c = *cur++; |
7868b4a2 | 1008 | |
6f572ac2 | 1009 | /* In #include-style directives, terminators are not escapable. */ |
6338b358 NB |
1010 | if (c == '\\' && !pfile->state.angled_headers && *cur != '\n') |
1011 | cur++; | |
1012 | else if (c == terminator) | |
bced6edf | 1013 | break; |
6338b358 | 1014 | else if (c == '\n') |
0d9f234d | 1015 | { |
6338b358 | 1016 | cur--; |
4bb09c26 JM |
1017 | /* Unmatched quotes always yield undefined behavior, but |
1018 | greedy lexing means that what appears to be an unterminated | |
1019 | header name may actually be a legitimate sequence of tokens. */ | |
1020 | if (terminator == '>') | |
1021 | { | |
1022 | token->type = CPP_LESS; | |
1023 | return; | |
1024 | } | |
6338b358 NB |
1025 | type = CPP_OTHER; |
1026 | break; | |
45b966db | 1027 | } |
6338b358 NB |
1028 | else if (c == '\0') |
1029 | saw_NUL = true; | |
45b966db ZW |
1030 | } |
1031 | ||
6338b358 | 1032 | if (saw_NUL && !pfile->state.skipping) |
0527bc4e JDA |
1033 | cpp_error (pfile, CPP_DL_WARNING, |
1034 | "null character(s) preserved in literal"); | |
45b966db | 1035 | |
c663e301 JM |
1036 | if (type == CPP_OTHER && CPP_OPTION (pfile, lang) != CLK_ASM) |
1037 | cpp_error (pfile, CPP_DL_PEDWARN, "missing terminating %c character", | |
1038 | (int) terminator); | |
1039 | ||
6338b358 NB |
1040 | pfile->buffer->cur = cur; |
1041 | create_literal (pfile, token, base, cur - base, type); | |
0d9f234d | 1042 | } |
041c3194 | 1043 | |
631d0d36 MG |
1044 | /* Return the comment table. The client may not make any assumption |
1045 | about the ordering of the table. */ | |
1046 | cpp_comment_table * | |
1047 | cpp_get_comments (cpp_reader *pfile) | |
1048 | { | |
1049 | return &pfile->comments; | |
1050 | } | |
1051 | ||
1052 | /* Append a comment to the end of the comment table. */ | |
1053 | static void | |
1054 | store_comment (cpp_reader *pfile, cpp_token *token) | |
1055 | { | |
1056 | int len; | |
1057 | ||
1058 | if (pfile->comments.allocated == 0) | |
1059 | { | |
1060 | pfile->comments.allocated = 256; | |
1061 | pfile->comments.entries = (cpp_comment *) xmalloc | |
1062 | (pfile->comments.allocated * sizeof (cpp_comment)); | |
1063 | } | |
1064 | ||
1065 | if (pfile->comments.count == pfile->comments.allocated) | |
1066 | { | |
1067 | pfile->comments.allocated *= 2; | |
1068 | pfile->comments.entries = (cpp_comment *) xrealloc | |
1069 | (pfile->comments.entries, | |
1070 | pfile->comments.allocated * sizeof (cpp_comment)); | |
1071 | } | |
1072 | ||
1073 | len = token->val.str.len; | |
1074 | ||
1075 | /* Copy comment. Note, token may not be NULL terminated. */ | |
1076 | pfile->comments.entries[pfile->comments.count].comment = | |
1077 | (char *) xmalloc (sizeof (char) * (len + 1)); | |
1078 | memcpy (pfile->comments.entries[pfile->comments.count].comment, | |
1079 | token->val.str.text, len); | |
1080 | pfile->comments.entries[pfile->comments.count].comment[len] = '\0'; | |
1081 | ||
1082 | /* Set source location. */ | |
1083 | pfile->comments.entries[pfile->comments.count].sloc = token->src_loc; | |
1084 | ||
1085 | /* Increment the count of entries in the comment table. */ | |
1086 | pfile->comments.count++; | |
1087 | } | |
1088 | ||
93c80368 | 1089 | /* The stored comment includes the comment start and any terminator. */ |
9e62c811 | 1090 | static void |
6cf87ca4 ZW |
1091 | save_comment (cpp_reader *pfile, cpp_token *token, const unsigned char *from, |
1092 | cppchar_t type) | |
9e62c811 | 1093 | { |
041c3194 | 1094 | unsigned char *buffer; |
477cdac7 | 1095 | unsigned int len, clen; |
df383483 | 1096 | |
1c6d33ef | 1097 | len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */ |
480709cc | 1098 | |
3542203b NB |
1099 | /* C++ comments probably (not definitely) have moved past a new |
1100 | line, which we don't want to save in the comment. */ | |
480709cc | 1101 | if (is_vspace (pfile->buffer->cur[-1])) |
3542203b | 1102 | len--; |
477cdac7 JT |
1103 | |
1104 | /* If we are currently in a directive, then we need to store all | |
1105 | C++ comments as C comments internally, and so we need to | |
1106 | allocate a little extra space in that case. | |
1107 | ||
1108 | Note that the only time we encounter a directive here is | |
1109 | when we are saving comments in a "#define". */ | |
1110 | clen = (pfile->state.in_directive && type == '/') ? len + 2 : len; | |
1111 | ||
1112 | buffer = _cpp_unaligned_alloc (pfile, clen); | |
df383483 | 1113 | |
041c3194 | 1114 | token->type = CPP_COMMENT; |
477cdac7 | 1115 | token->val.str.len = clen; |
0d9f234d | 1116 | token->val.str.text = buffer; |
45b966db | 1117 | |
1c6d33ef NB |
1118 | buffer[0] = '/'; |
1119 | memcpy (buffer + 1, from, len - 1); | |
477cdac7 | 1120 | |
1eeeb6a4 | 1121 | /* Finish conversion to a C comment, if necessary. */ |
477cdac7 JT |
1122 | if (pfile->state.in_directive && type == '/') |
1123 | { | |
1124 | buffer[1] = '*'; | |
1125 | buffer[clen - 2] = '*'; | |
1126 | buffer[clen - 1] = '/'; | |
1127 | } | |
631d0d36 MG |
1128 | |
1129 | /* Finally store this comment for use by clients of libcpp. */ | |
1130 | store_comment (pfile, token); | |
0d9f234d | 1131 | } |
45b966db | 1132 | |
5fddcffc NB |
1133 | /* Allocate COUNT tokens for RUN. */ |
1134 | void | |
6cf87ca4 | 1135 | _cpp_init_tokenrun (tokenrun *run, unsigned int count) |
5fddcffc | 1136 | { |
72bb2c39 | 1137 | run->base = XNEWVEC (cpp_token, count); |
5fddcffc NB |
1138 | run->limit = run->base + count; |
1139 | run->next = NULL; | |
1140 | } | |
1141 | ||
1142 | /* Returns the next tokenrun, or creates one if there is none. */ | |
1143 | static tokenrun * | |
6cf87ca4 | 1144 | next_tokenrun (tokenrun *run) |
5fddcffc NB |
1145 | { |
1146 | if (run->next == NULL) | |
1147 | { | |
72bb2c39 | 1148 | run->next = XNEW (tokenrun); |
bdcbe496 | 1149 | run->next->prev = run; |
5fddcffc NB |
1150 | _cpp_init_tokenrun (run->next, 250); |
1151 | } | |
1152 | ||
1153 | return run->next; | |
1154 | } | |
1155 | ||
5950c3c9 BE |
1156 | /* Look ahead in the input stream. */ |
1157 | const cpp_token * | |
1158 | cpp_peek_token (cpp_reader *pfile, int index) | |
1159 | { | |
1160 | cpp_context *context = pfile->context; | |
1161 | const cpp_token *peektok; | |
1162 | int count; | |
1163 | ||
1164 | /* First, scan through any pending cpp_context objects. */ | |
1165 | while (context->prev) | |
1166 | { | |
1167 | ptrdiff_t sz = (context->direct_p | |
1168 | ? LAST (context).token - FIRST (context).token | |
1169 | : LAST (context).ptoken - FIRST (context).ptoken); | |
1170 | ||
1171 | if (index < (int) sz) | |
1172 | return (context->direct_p | |
1173 | ? FIRST (context).token + index | |
1174 | : *(FIRST (context).ptoken + index)); | |
1175 | ||
1176 | index -= (int) sz; | |
1177 | context = context->prev; | |
1178 | } | |
1179 | ||
1180 | /* We will have to read some new tokens after all (and do so | |
1181 | without invalidating preceding tokens). */ | |
1182 | count = index; | |
1183 | pfile->keep_tokens++; | |
1184 | ||
1185 | do | |
1186 | { | |
1187 | peektok = _cpp_lex_token (pfile); | |
1188 | if (peektok->type == CPP_EOF) | |
1189 | return peektok; | |
1190 | } | |
1191 | while (index--); | |
1192 | ||
1193 | _cpp_backup_tokens_direct (pfile, count + 1); | |
1194 | pfile->keep_tokens--; | |
1195 | ||
1196 | return peektok; | |
1197 | } | |
1198 | ||
4ed5bcfb NB |
1199 | /* Allocate a single token that is invalidated at the same time as the |
1200 | rest of the tokens on the line. Has its line and col set to the | |
1201 | same as the last lexed token, so that diagnostics appear in the | |
1202 | right place. */ | |
1203 | cpp_token * | |
6cf87ca4 | 1204 | _cpp_temp_token (cpp_reader *pfile) |
4ed5bcfb NB |
1205 | { |
1206 | cpp_token *old, *result; | |
5950c3c9 BE |
1207 | ptrdiff_t sz = pfile->cur_run->limit - pfile->cur_token; |
1208 | ptrdiff_t la = (ptrdiff_t) pfile->lookaheads; | |
4ed5bcfb NB |
1209 | |
1210 | old = pfile->cur_token - 1; | |
5950c3c9 BE |
1211 | /* Any pre-existing lookaheads must not be clobbered. */ |
1212 | if (la) | |
1213 | { | |
1214 | if (sz <= la) | |
1215 | { | |
1216 | tokenrun *next = next_tokenrun (pfile->cur_run); | |
1217 | ||
1218 | if (sz < la) | |
1219 | memmove (next->base + 1, next->base, | |
1220 | (la - sz) * sizeof (cpp_token)); | |
1221 | ||
1222 | next->base[0] = pfile->cur_run->limit[-1]; | |
1223 | } | |
1224 | ||
1225 | if (sz > 1) | |
1226 | memmove (pfile->cur_token + 1, pfile->cur_token, | |
1227 | MIN (la, sz - 1) * sizeof (cpp_token)); | |
1228 | } | |
1229 | ||
1230 | if (!sz && pfile->cur_token == pfile->cur_run->limit) | |
4ed5bcfb NB |
1231 | { |
1232 | pfile->cur_run = next_tokenrun (pfile->cur_run); | |
1233 | pfile->cur_token = pfile->cur_run->base; | |
1234 | } | |
1235 | ||
1236 | result = pfile->cur_token++; | |
12f9df4e | 1237 | result->src_loc = old->src_loc; |
4ed5bcfb NB |
1238 | return result; |
1239 | } | |
1240 | ||
14baae01 NB |
1241 | /* Lex a token into RESULT (external interface). Takes care of issues |
1242 | like directive handling, token lookahead, multiple include | |
a1f300c0 | 1243 | optimization and skipping. */ |
345894b4 | 1244 | const cpp_token * |
6cf87ca4 | 1245 | _cpp_lex_token (cpp_reader *pfile) |
5fddcffc | 1246 | { |
bdcbe496 | 1247 | cpp_token *result; |
5fddcffc | 1248 | |
bdcbe496 | 1249 | for (;;) |
5fddcffc | 1250 | { |
bdcbe496 | 1251 | if (pfile->cur_token == pfile->cur_run->limit) |
5fddcffc | 1252 | { |
bdcbe496 NB |
1253 | pfile->cur_run = next_tokenrun (pfile->cur_run); |
1254 | pfile->cur_token = pfile->cur_run->base; | |
5fddcffc | 1255 | } |
ee380365 TT |
1256 | /* We assume that the current token is somewhere in the current |
1257 | run. */ | |
1258 | if (pfile->cur_token < pfile->cur_run->base | |
1259 | || pfile->cur_token >= pfile->cur_run->limit) | |
1260 | abort (); | |
5fddcffc | 1261 | |
bdcbe496 | 1262 | if (pfile->lookaheads) |
14baae01 NB |
1263 | { |
1264 | pfile->lookaheads--; | |
1265 | result = pfile->cur_token++; | |
1266 | } | |
bdcbe496 | 1267 | else |
14baae01 | 1268 | result = _cpp_lex_direct (pfile); |
bdcbe496 NB |
1269 | |
1270 | if (result->flags & BOL) | |
5fddcffc | 1271 | { |
bdcbe496 NB |
1272 | /* Is this a directive. If _cpp_handle_directive returns |
1273 | false, it is an assembler #. */ | |
1274 | if (result->type == CPP_HASH | |
e808ec9c NB |
1275 | /* 6.10.3 p 11: Directives in a list of macro arguments |
1276 | gives undefined behavior. This implementation | |
1277 | handles the directive as normal. */ | |
bc4071dd | 1278 | && pfile->state.parsing_args != 1) |
21b11495 | 1279 | { |
bc4071dd | 1280 | if (_cpp_handle_directive (pfile, result->flags & PREV_WHITE)) |
21b11495 | 1281 | { |
bc4071dd RH |
1282 | if (pfile->directive_result.type == CPP_PADDING) |
1283 | continue; | |
21b11495 | 1284 | result = &pfile->directive_result; |
21b11495 ZW |
1285 | } |
1286 | } | |
bc4071dd RH |
1287 | else if (pfile->state.in_deferred_pragma) |
1288 | result = &pfile->directive_result; | |
21b11495 | 1289 | |
97293897 | 1290 | if (pfile->cb.line_change && !pfile->state.skipping) |
6cf87ca4 | 1291 | pfile->cb.line_change (pfile, result, pfile->state.parsing_args); |
5fddcffc | 1292 | } |
5fddcffc | 1293 | |
bdcbe496 | 1294 | /* We don't skip tokens in directives. */ |
bc4071dd | 1295 | if (pfile->state.in_directive || pfile->state.in_deferred_pragma) |
bdcbe496 | 1296 | break; |
5fddcffc | 1297 | |
bdcbe496 | 1298 | /* Outside a directive, invalidate controlling macros. At file |
14baae01 | 1299 | EOF, _cpp_lex_direct takes care of popping the buffer, so we never |
6356f892 | 1300 | get here and MI optimization works. */ |
5fddcffc | 1301 | pfile->mi_valid = false; |
bdcbe496 NB |
1302 | |
1303 | if (!pfile->state.skipping || result->type == CPP_EOF) | |
1304 | break; | |
5fddcffc NB |
1305 | } |
1306 | ||
345894b4 | 1307 | return result; |
5fddcffc NB |
1308 | } |
1309 | ||
26aea073 NB |
1310 | /* Returns true if a fresh line has been loaded. */ |
1311 | bool | |
6cf87ca4 | 1312 | _cpp_get_fresh_line (cpp_reader *pfile) |
004cb263 | 1313 | { |
22234f56 PB |
1314 | int return_at_eof; |
1315 | ||
26aea073 NB |
1316 | /* We can't get a new line until we leave the current directive. */ |
1317 | if (pfile->state.in_directive) | |
1318 | return false; | |
df383483 | 1319 | |
26aea073 | 1320 | for (;;) |
1a76916c | 1321 | { |
26aea073 | 1322 | cpp_buffer *buffer = pfile->buffer; |
1a76916c | 1323 | |
26aea073 NB |
1324 | if (!buffer->need_line) |
1325 | return true; | |
1326 | ||
1327 | if (buffer->next_line < buffer->rlimit) | |
004cb263 | 1328 | { |
26aea073 NB |
1329 | _cpp_clean_line (pfile); |
1330 | return true; | |
1331 | } | |
004cb263 | 1332 | |
26aea073 NB |
1333 | /* First, get out of parsing arguments state. */ |
1334 | if (pfile->state.parsing_args) | |
1335 | return false; | |
1336 | ||
1337 | /* End of buffer. Non-empty files should end in a newline. */ | |
1338 | if (buffer->buf != buffer->rlimit | |
1339 | && buffer->next_line > buffer->rlimit | |
1340 | && !buffer->from_stage3) | |
1341 | { | |
ed0e74e0 | 1342 | /* Clip to buffer size. */ |
26aea073 | 1343 | buffer->next_line = buffer->rlimit; |
26aea073 | 1344 | } |
22234f56 PB |
1345 | |
1346 | return_at_eof = buffer->return_at_eof; | |
26aea073 | 1347 | _cpp_pop_buffer (pfile); |
22234f56 | 1348 | if (pfile->buffer == NULL || return_at_eof) |
a506c55c | 1349 | return false; |
26aea073 | 1350 | } |
004cb263 NB |
1351 | } |
1352 | ||
6f572ac2 NB |
1353 | #define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \ |
1354 | do \ | |
1355 | { \ | |
1356 | result->type = ELSE_TYPE; \ | |
1357 | if (*buffer->cur == CHAR) \ | |
1358 | buffer->cur++, result->type = THEN_TYPE; \ | |
1359 | } \ | |
1360 | while (0) | |
480709cc | 1361 | |
14baae01 NB |
1362 | /* Lex a token into pfile->cur_token, which is also incremented, to |
1363 | get diagnostics pointing to the correct location. | |
1364 | ||
1365 | Does not handle issues such as token lookahead, multiple-include | |
f1ba665b | 1366 | optimization, directives, skipping etc. This function is only |
14baae01 NB |
1367 | suitable for use by _cpp_lex_token, and in special cases like |
1368 | lex_expansion_token which doesn't care for any of these issues. | |
1369 | ||
1370 | When meeting a newline, returns CPP_EOF if parsing a directive, | |
1371 | otherwise returns to the start of the token buffer if permissible. | |
1372 | Returns the location of the lexed token. */ | |
1373 | cpp_token * | |
6cf87ca4 | 1374 | _cpp_lex_direct (cpp_reader *pfile) |
45b966db | 1375 | { |
0d9f234d | 1376 | cppchar_t c; |
adb84b42 | 1377 | cpp_buffer *buffer; |
0d9f234d | 1378 | const unsigned char *comment_start; |
14baae01 | 1379 | cpp_token *result = pfile->cur_token++; |
9ec7291f | 1380 | |
5fddcffc | 1381 | fresh_line: |
26aea073 | 1382 | result->flags = 0; |
2be570f9 | 1383 | buffer = pfile->buffer; |
a506c55c | 1384 | if (buffer->need_line) |
26aea073 | 1385 | { |
bc4071dd RH |
1386 | if (pfile->state.in_deferred_pragma) |
1387 | { | |
1388 | result->type = CPP_PRAGMA_EOL; | |
1389 | pfile->state.in_deferred_pragma = false; | |
1390 | if (!pfile->state.pragma_allow_expansion) | |
1391 | pfile->state.prevent_expansion--; | |
1392 | return result; | |
1393 | } | |
26aea073 NB |
1394 | if (!_cpp_get_fresh_line (pfile)) |
1395 | { | |
1396 | result->type = CPP_EOF; | |
9ff7868d NB |
1397 | if (!pfile->state.in_directive) |
1398 | { | |
1399 | /* Tell the compiler the line number of the EOF token. */ | |
500bee0a | 1400 | result->src_loc = pfile->line_table->highest_line; |
9ff7868d NB |
1401 | result->flags = BOL; |
1402 | } | |
26aea073 NB |
1403 | return result; |
1404 | } | |
1405 | if (!pfile->keep_tokens) | |
1406 | { | |
1407 | pfile->cur_run = &pfile->base_run; | |
1408 | result = pfile->base_run.base; | |
1409 | pfile->cur_token = result + 1; | |
1410 | } | |
1411 | result->flags = BOL; | |
1412 | if (pfile->state.parsing_args == 2) | |
1413 | result->flags |= PREV_WHITE; | |
1414 | } | |
a506c55c | 1415 | buffer = pfile->buffer; |
5fddcffc | 1416 | update_tokens_line: |
500bee0a | 1417 | result->src_loc = pfile->line_table->highest_line; |
041c3194 | 1418 | |
5fddcffc | 1419 | skipped_white: |
26aea073 NB |
1420 | if (buffer->cur >= buffer->notes[buffer->cur_note].pos |
1421 | && !pfile->overlaid_buffer) | |
1422 | { | |
1423 | _cpp_process_line_notes (pfile, false); | |
500bee0a | 1424 | result->src_loc = pfile->line_table->highest_line; |
26aea073 | 1425 | } |
480709cc | 1426 | c = *buffer->cur++; |
12f9df4e | 1427 | |
500bee0a PB |
1428 | LINEMAP_POSITION_FOR_COLUMN (result->src_loc, pfile->line_table, |
1429 | CPP_BUF_COLUMN (buffer, buffer->cur)); | |
5fddcffc | 1430 | |
0d9f234d | 1431 | switch (c) |
45b966db | 1432 | { |
4d6baafa NB |
1433 | case ' ': case '\t': case '\f': case '\v': case '\0': |
1434 | result->flags |= PREV_WHITE; | |
26aea073 NB |
1435 | skip_whitespace (pfile, c); |
1436 | goto skipped_white; | |
0d9f234d | 1437 | |
26aea073 | 1438 | case '\n': |
12f9df4e PB |
1439 | if (buffer->cur < buffer->rlimit) |
1440 | CPP_INCREMENT_LINE (pfile, 0); | |
26aea073 NB |
1441 | buffer->need_line = true; |
1442 | goto fresh_line; | |
46d07497 | 1443 | |
0d9f234d NB |
1444 | case '0': case '1': case '2': case '3': case '4': |
1445 | case '5': case '6': case '7': case '8': case '9': | |
50668cf6 GK |
1446 | { |
1447 | struct normalize_state nst = INITIAL_NORMALIZE_STATE; | |
1448 | result->type = CPP_NUMBER; | |
1449 | lex_number (pfile, &result->val.str, &nst); | |
1450 | warn_about_normalization (pfile, result, &nst); | |
1451 | break; | |
1452 | } | |
46d07497 | 1453 | |
0abc6a6a | 1454 | case 'L': |
b6baa67d KVH |
1455 | case 'u': |
1456 | case 'U': | |
2c6e3f55 JJ |
1457 | case 'R': |
1458 | /* 'L', 'u', 'U', 'u8' or 'R' may introduce wide characters, | |
1459 | wide strings or raw strings. */ | |
b6baa67d | 1460 | if (c == 'L' || CPP_OPTION (pfile, uliterals)) |
bced6edf | 1461 | { |
2c6e3f55 JJ |
1462 | if ((*buffer->cur == '\'' && c != 'R') |
1463 | || *buffer->cur == '"' | |
1464 | || (*buffer->cur == 'R' | |
1465 | && c != 'R' | |
1466 | && buffer->cur[1] == '"' | |
1467 | && CPP_OPTION (pfile, uliterals)) | |
1468 | || (*buffer->cur == '8' | |
1469 | && c == 'u' | |
1470 | && (buffer->cur[1] == '"' | |
1471 | || (buffer->cur[1] == 'R' && buffer->cur[2] == '"')))) | |
b6baa67d KVH |
1472 | { |
1473 | lex_string (pfile, result, buffer->cur - 1); | |
1474 | break; | |
1475 | } | |
bced6edf | 1476 | } |
df383483 | 1477 | /* Fall through. */ |
0abc6a6a | 1478 | |
0d9f234d NB |
1479 | case '_': |
1480 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | |
1481 | case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': | |
1482 | case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': | |
b6baa67d | 1483 | case 's': case 't': case 'v': case 'w': case 'x': |
0d9f234d NB |
1484 | case 'y': case 'z': |
1485 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | |
0abc6a6a | 1486 | case 'G': case 'H': case 'I': case 'J': case 'K': |
2c6e3f55 | 1487 | case 'M': case 'N': case 'O': case 'P': case 'Q': |
b6baa67d | 1488 | case 'S': case 'T': case 'V': case 'W': case 'X': |
0d9f234d NB |
1489 | case 'Y': case 'Z': |
1490 | result->type = CPP_NAME; | |
50668cf6 GK |
1491 | { |
1492 | struct normalize_state nst = INITIAL_NORMALIZE_STATE; | |
9a0c6187 JM |
1493 | result->val.node.node = lex_identifier (pfile, buffer->cur - 1, false, |
1494 | &nst); | |
50668cf6 GK |
1495 | warn_about_normalization (pfile, result, &nst); |
1496 | } | |
0d9f234d | 1497 | |
0d9f234d | 1498 | /* Convert named operators to their proper types. */ |
9a0c6187 | 1499 | if (result->val.node.node->flags & NODE_OPERATOR) |
0d9f234d NB |
1500 | { |
1501 | result->flags |= NAMED_OP; | |
9a0c6187 | 1502 | result->type = (enum cpp_ttype) result->val.node.node->directive_index; |
0d9f234d NB |
1503 | } |
1504 | break; | |
1505 | ||
1506 | case '\'': | |
1507 | case '"': | |
6338b358 | 1508 | lex_string (pfile, result, buffer->cur - 1); |
0d9f234d | 1509 | break; |
041c3194 | 1510 | |
0d9f234d | 1511 | case '/': |
1c6d33ef NB |
1512 | /* A potential block or line comment. */ |
1513 | comment_start = buffer->cur; | |
6f572ac2 NB |
1514 | c = *buffer->cur; |
1515 | ||
1c6d33ef NB |
1516 | if (c == '*') |
1517 | { | |
26aea073 | 1518 | if (_cpp_skip_block_comment (pfile)) |
0527bc4e | 1519 | cpp_error (pfile, CPP_DL_ERROR, "unterminated comment"); |
0d9f234d | 1520 | } |
480709cc | 1521 | else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments) |
12f9df4e | 1522 | || cpp_in_system_header (pfile))) |
0d9f234d | 1523 | { |
bdb05a7b NB |
1524 | /* Warn about comments only if pedantically GNUC89, and not |
1525 | in system headers. */ | |
1526 | if (CPP_OPTION (pfile, lang) == CLK_GNUC89 && CPP_PEDANTIC (pfile) | |
a94c1199 | 1527 | && ! buffer->warned_cplusplus_comments) |
041c3194 | 1528 | { |
0527bc4e | 1529 | cpp_error (pfile, CPP_DL_PEDWARN, |
56508306 | 1530 | "C++ style comments are not allowed in ISO C90"); |
0527bc4e | 1531 | cpp_error (pfile, CPP_DL_PEDWARN, |
ebef4e8c | 1532 | "(this will be reported only once per input file)"); |
1c6d33ef NB |
1533 | buffer->warned_cplusplus_comments = 1; |
1534 | } | |
0d9f234d | 1535 | |
01ef6563 | 1536 | if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments)) |
87cf0651 | 1537 | cpp_warning (pfile, CPP_W_COMMENTS, "multi-line comment"); |
1c6d33ef | 1538 | } |
480709cc NB |
1539 | else if (c == '=') |
1540 | { | |
6f572ac2 | 1541 | buffer->cur++; |
480709cc NB |
1542 | result->type = CPP_DIV_EQ; |
1543 | break; | |
1544 | } | |
1545 | else | |
1546 | { | |
480709cc NB |
1547 | result->type = CPP_DIV; |
1548 | break; | |
1549 | } | |
0d9f234d | 1550 | |
1c6d33ef NB |
1551 | if (!pfile->state.save_comments) |
1552 | { | |
1553 | result->flags |= PREV_WHITE; | |
5fddcffc | 1554 | goto update_tokens_line; |
0d9f234d | 1555 | } |
1c6d33ef NB |
1556 | |
1557 | /* Save the comment as a token in its own right. */ | |
477cdac7 | 1558 | save_comment (pfile, result, comment_start, c); |
bdcbe496 | 1559 | break; |
0d9f234d NB |
1560 | |
1561 | case '<': | |
1562 | if (pfile->state.angled_headers) | |
1563 | { | |
6338b358 | 1564 | lex_string (pfile, result, buffer->cur - 1); |
4bb09c26 JM |
1565 | if (result->type != CPP_LESS) |
1566 | break; | |
0d9f234d | 1567 | } |
45b966db | 1568 | |
6f572ac2 NB |
1569 | result->type = CPP_LESS; |
1570 | if (*buffer->cur == '=') | |
1571 | buffer->cur++, result->type = CPP_LESS_EQ; | |
1572 | else if (*buffer->cur == '<') | |
0d9f234d | 1573 | { |
6f572ac2 NB |
1574 | buffer->cur++; |
1575 | IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT); | |
0d9f234d | 1576 | } |
6f572ac2 | 1577 | else if (CPP_OPTION (pfile, digraphs)) |
480709cc | 1578 | { |
6f572ac2 NB |
1579 | if (*buffer->cur == ':') |
1580 | { | |
1581 | buffer->cur++; | |
1582 | result->flags |= DIGRAPH; | |
1583 | result->type = CPP_OPEN_SQUARE; | |
1584 | } | |
1585 | else if (*buffer->cur == '%') | |
1586 | { | |
1587 | buffer->cur++; | |
1588 | result->flags |= DIGRAPH; | |
1589 | result->type = CPP_OPEN_BRACE; | |
1590 | } | |
480709cc | 1591 | } |
0d9f234d NB |
1592 | break; |
1593 | ||
1594 | case '>': | |
6f572ac2 NB |
1595 | result->type = CPP_GREATER; |
1596 | if (*buffer->cur == '=') | |
1597 | buffer->cur++, result->type = CPP_GREATER_EQ; | |
1598 | else if (*buffer->cur == '>') | |
0d9f234d | 1599 | { |
6f572ac2 NB |
1600 | buffer->cur++; |
1601 | IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT); | |
1602 | } | |
0d9f234d NB |
1603 | break; |
1604 | ||
cbcff6df | 1605 | case '%': |
6f572ac2 NB |
1606 | result->type = CPP_MOD; |
1607 | if (*buffer->cur == '=') | |
1608 | buffer->cur++, result->type = CPP_MOD_EQ; | |
1609 | else if (CPP_OPTION (pfile, digraphs)) | |
480709cc | 1610 | { |
6f572ac2 | 1611 | if (*buffer->cur == ':') |
480709cc | 1612 | { |
6f572ac2 NB |
1613 | buffer->cur++; |
1614 | result->flags |= DIGRAPH; | |
1615 | result->type = CPP_HASH; | |
1616 | if (*buffer->cur == '%' && buffer->cur[1] == ':') | |
9a0c6187 | 1617 | buffer->cur += 2, result->type = CPP_PASTE, result->val.token_no = 0; |
6f572ac2 NB |
1618 | } |
1619 | else if (*buffer->cur == '>') | |
1620 | { | |
1621 | buffer->cur++; | |
1622 | result->flags |= DIGRAPH; | |
1623 | result->type = CPP_CLOSE_BRACE; | |
480709cc | 1624 | } |
480709cc | 1625 | } |
0d9f234d NB |
1626 | break; |
1627 | ||
cbcff6df | 1628 | case '.': |
480709cc | 1629 | result->type = CPP_DOT; |
6f572ac2 | 1630 | if (ISDIGIT (*buffer->cur)) |
480709cc | 1631 | { |
50668cf6 | 1632 | struct normalize_state nst = INITIAL_NORMALIZE_STATE; |
480709cc | 1633 | result->type = CPP_NUMBER; |
50668cf6 GK |
1634 | lex_number (pfile, &result->val.str, &nst); |
1635 | warn_about_normalization (pfile, result, &nst); | |
480709cc | 1636 | } |
6f572ac2 NB |
1637 | else if (*buffer->cur == '.' && buffer->cur[1] == '.') |
1638 | buffer->cur += 2, result->type = CPP_ELLIPSIS; | |
1639 | else if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus)) | |
1640 | buffer->cur++, result->type = CPP_DOT_STAR; | |
0d9f234d | 1641 | break; |
45b966db | 1642 | |
0d9f234d | 1643 | case '+': |
6f572ac2 NB |
1644 | result->type = CPP_PLUS; |
1645 | if (*buffer->cur == '+') | |
1646 | buffer->cur++, result->type = CPP_PLUS_PLUS; | |
1647 | else if (*buffer->cur == '=') | |
1648 | buffer->cur++, result->type = CPP_PLUS_EQ; | |
0d9f234d | 1649 | break; |
04e3ec78 | 1650 | |
0d9f234d | 1651 | case '-': |
6f572ac2 NB |
1652 | result->type = CPP_MINUS; |
1653 | if (*buffer->cur == '>') | |
0d9f234d | 1654 | { |
6f572ac2 | 1655 | buffer->cur++; |
480709cc | 1656 | result->type = CPP_DEREF; |
6f572ac2 NB |
1657 | if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus)) |
1658 | buffer->cur++, result->type = CPP_DEREF_STAR; | |
480709cc | 1659 | } |
6f572ac2 NB |
1660 | else if (*buffer->cur == '-') |
1661 | buffer->cur++, result->type = CPP_MINUS_MINUS; | |
1662 | else if (*buffer->cur == '=') | |
1663 | buffer->cur++, result->type = CPP_MINUS_EQ; | |
0d9f234d | 1664 | break; |
45b966db | 1665 | |
0d9f234d | 1666 | case '&': |
6f572ac2 NB |
1667 | result->type = CPP_AND; |
1668 | if (*buffer->cur == '&') | |
1669 | buffer->cur++, result->type = CPP_AND_AND; | |
1670 | else if (*buffer->cur == '=') | |
1671 | buffer->cur++, result->type = CPP_AND_EQ; | |
0d9f234d | 1672 | break; |
df383483 | 1673 | |
0d9f234d | 1674 | case '|': |
6f572ac2 NB |
1675 | result->type = CPP_OR; |
1676 | if (*buffer->cur == '|') | |
1677 | buffer->cur++, result->type = CPP_OR_OR; | |
1678 | else if (*buffer->cur == '=') | |
1679 | buffer->cur++, result->type = CPP_OR_EQ; | |
0d9f234d | 1680 | break; |
45b966db | 1681 | |
0d9f234d | 1682 | case ':': |
6f572ac2 NB |
1683 | result->type = CPP_COLON; |
1684 | if (*buffer->cur == ':' && CPP_OPTION (pfile, cplusplus)) | |
1685 | buffer->cur++, result->type = CPP_SCOPE; | |
1686 | else if (*buffer->cur == '>' && CPP_OPTION (pfile, digraphs)) | |
0d9f234d | 1687 | { |
6f572ac2 | 1688 | buffer->cur++; |
0d9f234d | 1689 | result->flags |= DIGRAPH; |
480709cc NB |
1690 | result->type = CPP_CLOSE_SQUARE; |
1691 | } | |
0d9f234d | 1692 | break; |
45b966db | 1693 | |
480709cc NB |
1694 | case '*': IF_NEXT_IS ('=', CPP_MULT_EQ, CPP_MULT); break; |
1695 | case '=': IF_NEXT_IS ('=', CPP_EQ_EQ, CPP_EQ); break; | |
1696 | case '!': IF_NEXT_IS ('=', CPP_NOT_EQ, CPP_NOT); break; | |
1697 | case '^': IF_NEXT_IS ('=', CPP_XOR_EQ, CPP_XOR); break; | |
9a0c6187 | 1698 | case '#': IF_NEXT_IS ('#', CPP_PASTE, CPP_HASH); result->val.token_no = 0; break; |
480709cc | 1699 | |
26aea073 | 1700 | case '?': result->type = CPP_QUERY; break; |
0d9f234d NB |
1701 | case '~': result->type = CPP_COMPL; break; |
1702 | case ',': result->type = CPP_COMMA; break; | |
1703 | case '(': result->type = CPP_OPEN_PAREN; break; | |
1704 | case ')': result->type = CPP_CLOSE_PAREN; break; | |
1705 | case '[': result->type = CPP_OPEN_SQUARE; break; | |
1706 | case ']': result->type = CPP_CLOSE_SQUARE; break; | |
1707 | case '{': result->type = CPP_OPEN_BRACE; break; | |
1708 | case '}': result->type = CPP_CLOSE_BRACE; break; | |
1709 | case ';': result->type = CPP_SEMICOLON; break; | |
1710 | ||
40f03658 | 1711 | /* @ is a punctuator in Objective-C. */ |
cc937581 | 1712 | case '@': result->type = CPP_ATSIGN; break; |
0d9f234d | 1713 | |
0abc6a6a | 1714 | case '$': |
1613e52b NB |
1715 | case '\\': |
1716 | { | |
1717 | const uchar *base = --buffer->cur; | |
50668cf6 | 1718 | struct normalize_state nst = INITIAL_NORMALIZE_STATE; |
0abc6a6a | 1719 | |
50668cf6 | 1720 | if (forms_identifier_p (pfile, true, &nst)) |
1613e52b NB |
1721 | { |
1722 | result->type = CPP_NAME; | |
9a0c6187 | 1723 | result->val.node.node = lex_identifier (pfile, base, true, &nst); |
50668cf6 | 1724 | warn_about_normalization (pfile, result, &nst); |
1613e52b NB |
1725 | break; |
1726 | } | |
1727 | buffer->cur++; | |
1067694a | 1728 | } |
1613e52b | 1729 | |
1067694a | 1730 | default: |
6338b358 NB |
1731 | create_literal (pfile, result, buffer->cur - 1, 1, CPP_OTHER); |
1732 | break; | |
0d9f234d | 1733 | } |
bdcbe496 NB |
1734 | |
1735 | return result; | |
0d9f234d NB |
1736 | } |
1737 | ||
59325650 NB |
1738 | /* An upper bound on the number of bytes needed to spell TOKEN. |
1739 | Does not include preceding whitespace. */ | |
93c80368 | 1740 | unsigned int |
6cf87ca4 | 1741 | cpp_token_len (const cpp_token *token) |
0d9f234d | 1742 | { |
93c80368 | 1743 | unsigned int len; |
6d2c2047 | 1744 | |
93c80368 | 1745 | switch (TOKEN_SPELL (token)) |
041c3194 | 1746 | { |
cc955282 | 1747 | default: len = 6; break; |
6338b358 | 1748 | case SPELL_LITERAL: len = token->val.str.len; break; |
9a0c6187 | 1749 | case SPELL_IDENT: len = NODE_LEN (token->val.node.node) * 10; break; |
041c3194 | 1750 | } |
59325650 NB |
1751 | |
1752 | return len; | |
6d2c2047 ZW |
1753 | } |
1754 | ||
47e20491 GK |
1755 | /* Parse UTF-8 out of NAMEP and place a \U escape in BUFFER. |
1756 | Return the number of bytes read out of NAME. (There are always | |
1757 | 10 bytes written to BUFFER.) */ | |
1758 | ||
1759 | static size_t | |
1760 | utf8_to_ucn (unsigned char *buffer, const unsigned char *name) | |
1761 | { | |
1762 | int j; | |
1763 | int ucn_len = 0; | |
1764 | int ucn_len_c; | |
1765 | unsigned t; | |
1766 | unsigned long utf32; | |
1767 | ||
1768 | /* Compute the length of the UTF-8 sequence. */ | |
1769 | for (t = *name; t & 0x80; t <<= 1) | |
1770 | ucn_len++; | |
1771 | ||
1772 | utf32 = *name & (0x7F >> ucn_len); | |
1773 | for (ucn_len_c = 1; ucn_len_c < ucn_len; ucn_len_c++) | |
1774 | { | |
1775 | utf32 = (utf32 << 6) | (*++name & 0x3F); | |
1776 | ||
1777 | /* Ill-formed UTF-8. */ | |
1778 | if ((*name & ~0x3F) != 0x80) | |
1779 | abort (); | |
1780 | } | |
1781 | ||
1782 | *buffer++ = '\\'; | |
1783 | *buffer++ = 'U'; | |
1784 | for (j = 7; j >= 0; j--) | |
1785 | *buffer++ = "0123456789abcdef"[(utf32 >> (4 * j)) & 0xF]; | |
1786 | return ucn_len; | |
1787 | } | |
1788 | ||
cfc93532 MLI |
1789 | /* Given a token TYPE corresponding to a digraph, return a pointer to |
1790 | the spelling of the digraph. */ | |
1791 | static const unsigned char * | |
1792 | cpp_digraph2name (enum cpp_ttype type) | |
1793 | { | |
1794 | return digraph_spellings[(int) type - (int) CPP_FIRST_DIGRAPH]; | |
1795 | } | |
47e20491 | 1796 | |
041c3194 | 1797 | /* Write the spelling of a token TOKEN to BUFFER. The buffer must |
cf00a885 | 1798 | already contain the enough space to hold the token's spelling. |
6cf87ca4 | 1799 | Returns a pointer to the character after the last character written. |
47e20491 GK |
1800 | FORSTRING is true if this is to be the spelling after translation |
1801 | phase 1 (this is different for UCNs). | |
6cf87ca4 | 1802 | FIXME: Would be nice if we didn't need the PFILE argument. */ |
93c80368 | 1803 | unsigned char * |
6cf87ca4 | 1804 | cpp_spell_token (cpp_reader *pfile, const cpp_token *token, |
47e20491 | 1805 | unsigned char *buffer, bool forstring) |
041c3194 | 1806 | { |
96be6998 | 1807 | switch (TOKEN_SPELL (token)) |
041c3194 ZW |
1808 | { |
1809 | case SPELL_OPERATOR: | |
1810 | { | |
1811 | const unsigned char *spelling; | |
1812 | unsigned char c; | |
d6d5f795 | 1813 | |
041c3194 | 1814 | if (token->flags & DIGRAPH) |
cfc93532 | 1815 | spelling = cpp_digraph2name (token->type); |
92936ecf ZW |
1816 | else if (token->flags & NAMED_OP) |
1817 | goto spell_ident; | |
041c3194 | 1818 | else |
96be6998 | 1819 | spelling = TOKEN_NAME (token); |
df383483 | 1820 | |
041c3194 ZW |
1821 | while ((c = *spelling++) != '\0') |
1822 | *buffer++ = c; | |
1823 | } | |
1824 | break; | |
d6d5f795 | 1825 | |
47ad4138 | 1826 | spell_ident: |
041c3194 | 1827 | case SPELL_IDENT: |
47e20491 GK |
1828 | if (forstring) |
1829 | { | |
9a0c6187 JM |
1830 | memcpy (buffer, NODE_NAME (token->val.node.node), |
1831 | NODE_LEN (token->val.node.node)); | |
1832 | buffer += NODE_LEN (token->val.node.node); | |
47e20491 GK |
1833 | } |
1834 | else | |
1835 | { | |
1836 | size_t i; | |
9a0c6187 | 1837 | const unsigned char * name = NODE_NAME (token->val.node.node); |
47e20491 | 1838 | |
9a0c6187 | 1839 | for (i = 0; i < NODE_LEN (token->val.node.node); i++) |
47e20491 GK |
1840 | if (name[i] & ~0x7F) |
1841 | { | |
1842 | i += utf8_to_ucn (buffer, name + i) - 1; | |
1843 | buffer += 10; | |
1844 | } | |
1845 | else | |
9a0c6187 | 1846 | *buffer++ = NODE_NAME (token->val.node.node)[i]; |
47e20491 | 1847 | } |
041c3194 | 1848 | break; |
d6d5f795 | 1849 | |
6338b358 | 1850 | case SPELL_LITERAL: |
47ad4138 ZW |
1851 | memcpy (buffer, token->val.str.text, token->val.str.len); |
1852 | buffer += token->val.str.len; | |
1853 | break; | |
1854 | ||
041c3194 | 1855 | case SPELL_NONE: |
0527bc4e JDA |
1856 | cpp_error (pfile, CPP_DL_ICE, |
1857 | "unspellable token %s", TOKEN_NAME (token)); | |
041c3194 ZW |
1858 | break; |
1859 | } | |
d6d5f795 | 1860 | |
041c3194 ZW |
1861 | return buffer; |
1862 | } | |
d6d5f795 | 1863 | |
5d8ebbd8 NB |
1864 | /* Returns TOKEN spelt as a null-terminated string. The string is |
1865 | freed when the reader is destroyed. Useful for diagnostics. */ | |
93c80368 | 1866 | unsigned char * |
6cf87ca4 | 1867 | cpp_token_as_text (cpp_reader *pfile, const cpp_token *token) |
59325650 NB |
1868 | { |
1869 | unsigned int len = cpp_token_len (token) + 1; | |
ece54d54 | 1870 | unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end; |
c5a04734 | 1871 | |
47e20491 | 1872 | end = cpp_spell_token (pfile, token, start, false); |
93c80368 | 1873 | end[0] = '\0'; |
c5a04734 | 1874 | |
93c80368 NB |
1875 | return start; |
1876 | } | |
c5a04734 | 1877 | |
cfc93532 MLI |
1878 | /* Returns a pointer to a string which spells the token defined by |
1879 | TYPE and FLAGS. Used by C front ends, which really should move to | |
1880 | using cpp_token_as_text. */ | |
93c80368 | 1881 | const char * |
cfc93532 | 1882 | cpp_type2name (enum cpp_ttype type, unsigned char flags) |
93c80368 | 1883 | { |
cfc93532 MLI |
1884 | if (flags & DIGRAPH) |
1885 | return (const char *) cpp_digraph2name (type); | |
1886 | else if (flags & NAMED_OP) | |
1887 | return cpp_named_operator2name (type); | |
1888 | ||
93c80368 NB |
1889 | return (const char *) token_spellings[type].name; |
1890 | } | |
c5a04734 | 1891 | |
4ed5bcfb NB |
1892 | /* Writes the spelling of token to FP, without any preceding space. |
1893 | Separated from cpp_spell_token for efficiency - to avoid stdio | |
1894 | double-buffering. */ | |
93c80368 | 1895 | void |
6cf87ca4 | 1896 | cpp_output_token (const cpp_token *token, FILE *fp) |
93c80368 | 1897 | { |
93c80368 | 1898 | switch (TOKEN_SPELL (token)) |
c5a04734 | 1899 | { |
93c80368 NB |
1900 | case SPELL_OPERATOR: |
1901 | { | |
1902 | const unsigned char *spelling; | |
3b681e9d | 1903 | int c; |
c5a04734 | 1904 | |
93c80368 | 1905 | if (token->flags & DIGRAPH) |
cfc93532 | 1906 | spelling = cpp_digraph2name (token->type); |
93c80368 NB |
1907 | else if (token->flags & NAMED_OP) |
1908 | goto spell_ident; | |
1909 | else | |
1910 | spelling = TOKEN_NAME (token); | |
041c3194 | 1911 | |
3b681e9d ZW |
1912 | c = *spelling; |
1913 | do | |
1914 | putc (c, fp); | |
1915 | while ((c = *++spelling) != '\0'); | |
93c80368 NB |
1916 | } |
1917 | break; | |
041c3194 | 1918 | |
93c80368 NB |
1919 | spell_ident: |
1920 | case SPELL_IDENT: | |
47e20491 GK |
1921 | { |
1922 | size_t i; | |
9a0c6187 | 1923 | const unsigned char * name = NODE_NAME (token->val.node.node); |
47e20491 | 1924 | |
9a0c6187 | 1925 | for (i = 0; i < NODE_LEN (token->val.node.node); i++) |
47e20491 GK |
1926 | if (name[i] & ~0x7F) |
1927 | { | |
1928 | unsigned char buffer[10]; | |
1929 | i += utf8_to_ucn (buffer, name + i) - 1; | |
1930 | fwrite (buffer, 1, 10, fp); | |
1931 | } | |
1932 | else | |
9a0c6187 | 1933 | fputc (NODE_NAME (token->val.node.node)[i], fp); |
47e20491 GK |
1934 | } |
1935 | break; | |
041c3194 | 1936 | |
6338b358 | 1937 | case SPELL_LITERAL: |
47ad4138 ZW |
1938 | fwrite (token->val.str.text, 1, token->val.str.len, fp); |
1939 | break; | |
1940 | ||
93c80368 NB |
1941 | case SPELL_NONE: |
1942 | /* An error, most probably. */ | |
1943 | break; | |
041c3194 | 1944 | } |
c5a04734 ZW |
1945 | } |
1946 | ||
93c80368 NB |
1947 | /* Compare two tokens. */ |
1948 | int | |
6cf87ca4 | 1949 | _cpp_equiv_tokens (const cpp_token *a, const cpp_token *b) |
c5a04734 | 1950 | { |
93c80368 NB |
1951 | if (a->type == b->type && a->flags == b->flags) |
1952 | switch (TOKEN_SPELL (a)) | |
1953 | { | |
1954 | default: /* Keep compiler happy. */ | |
1955 | case SPELL_OPERATOR: | |
9a0c6187 | 1956 | /* token_no is used to track where multiple consecutive ## |
aa508502 | 1957 | tokens were originally located. */ |
9a0c6187 | 1958 | return (a->type != CPP_PASTE || a->val.token_no == b->val.token_no); |
93c80368 | 1959 | case SPELL_NONE: |
9a0c6187 JM |
1960 | return (a->type != CPP_MACRO_ARG |
1961 | || a->val.macro_arg.arg_no == b->val.macro_arg.arg_no); | |
93c80368 | 1962 | case SPELL_IDENT: |
9a0c6187 | 1963 | return a->val.node.node == b->val.node.node; |
6338b358 | 1964 | case SPELL_LITERAL: |
93c80368 NB |
1965 | return (a->val.str.len == b->val.str.len |
1966 | && !memcmp (a->val.str.text, b->val.str.text, | |
1967 | a->val.str.len)); | |
1968 | } | |
c5a04734 | 1969 | |
041c3194 ZW |
1970 | return 0; |
1971 | } | |
1972 | ||
93c80368 NB |
1973 | /* Returns nonzero if a space should be inserted to avoid an |
1974 | accidental token paste for output. For simplicity, it is | |
1975 | conservative, and occasionally advises a space where one is not | |
1976 | needed, e.g. "." and ".2". */ | |
93c80368 | 1977 | int |
6cf87ca4 ZW |
1978 | cpp_avoid_paste (cpp_reader *pfile, const cpp_token *token1, |
1979 | const cpp_token *token2) | |
c5a04734 | 1980 | { |
93c80368 NB |
1981 | enum cpp_ttype a = token1->type, b = token2->type; |
1982 | cppchar_t c; | |
c5a04734 | 1983 | |
93c80368 NB |
1984 | if (token1->flags & NAMED_OP) |
1985 | a = CPP_NAME; | |
1986 | if (token2->flags & NAMED_OP) | |
1987 | b = CPP_NAME; | |
c5a04734 | 1988 | |
93c80368 NB |
1989 | c = EOF; |
1990 | if (token2->flags & DIGRAPH) | |
37b8524c | 1991 | c = digraph_spellings[(int) b - (int) CPP_FIRST_DIGRAPH][0]; |
93c80368 NB |
1992 | else if (token_spellings[b].category == SPELL_OPERATOR) |
1993 | c = token_spellings[b].name[0]; | |
c5a04734 | 1994 | |
93c80368 | 1995 | /* Quickly get everything that can paste with an '='. */ |
37b8524c | 1996 | if ((int) a <= (int) CPP_LAST_EQ && c == '=') |
93c80368 | 1997 | return 1; |
c5a04734 | 1998 | |
93c80368 | 1999 | switch (a) |
c5a04734 | 2000 | { |
b52dbbf8 SE |
2001 | case CPP_GREATER: return c == '>'; |
2002 | case CPP_LESS: return c == '<' || c == '%' || c == ':'; | |
93c80368 NB |
2003 | case CPP_PLUS: return c == '+'; |
2004 | case CPP_MINUS: return c == '-' || c == '>'; | |
2005 | case CPP_DIV: return c == '/' || c == '*'; /* Comments. */ | |
2006 | case CPP_MOD: return c == ':' || c == '>'; | |
2007 | case CPP_AND: return c == '&'; | |
2008 | case CPP_OR: return c == '|'; | |
2009 | case CPP_COLON: return c == ':' || c == '>'; | |
2010 | case CPP_DEREF: return c == '*'; | |
26ec42ee | 2011 | case CPP_DOT: return c == '.' || c == '%' || b == CPP_NUMBER; |
93c80368 NB |
2012 | case CPP_HASH: return c == '#' || c == '%'; /* Digraph form. */ |
2013 | case CPP_NAME: return ((b == CPP_NUMBER | |
2014 | && name_p (pfile, &token2->val.str)) | |
2015 | || b == CPP_NAME | |
2016 | || b == CPP_CHAR || b == CPP_STRING); /* L */ | |
2017 | case CPP_NUMBER: return (b == CPP_NUMBER || b == CPP_NAME | |
2018 | || c == '.' || c == '+' || c == '-'); | |
1613e52b | 2019 | /* UCNs */ |
1067694a NB |
2020 | case CPP_OTHER: return ((token1->val.str.text[0] == '\\' |
2021 | && b == CPP_NAME) | |
1613e52b | 2022 | || (CPP_OPTION (pfile, objc) |
1067694a | 2023 | && token1->val.str.text[0] == '@' |
1613e52b | 2024 | && (b == CPP_NAME || b == CPP_STRING))); |
93c80368 | 2025 | default: break; |
c5a04734 | 2026 | } |
c5a04734 | 2027 | |
417f3e3a | 2028 | return 0; |
c5a04734 ZW |
2029 | } |
2030 | ||
93c80368 | 2031 | /* Output all the remaining tokens on the current line, and a newline |
4ed5bcfb NB |
2032 | character, to FP. Leading whitespace is removed. If there are |
2033 | macros, special token padding is not performed. */ | |
c5a04734 | 2034 | void |
6cf87ca4 | 2035 | cpp_output_line (cpp_reader *pfile, FILE *fp) |
c5a04734 | 2036 | { |
4ed5bcfb | 2037 | const cpp_token *token; |
96be6998 | 2038 | |
4ed5bcfb NB |
2039 | token = cpp_get_token (pfile); |
2040 | while (token->type != CPP_EOF) | |
96be6998 | 2041 | { |
4ed5bcfb NB |
2042 | cpp_output_token (token, fp); |
2043 | token = cpp_get_token (pfile); | |
2044 | if (token->flags & PREV_WHITE) | |
2045 | putc (' ', fp); | |
96be6998 ZW |
2046 | } |
2047 | ||
93c80368 | 2048 | putc ('\n', fp); |
041c3194 | 2049 | } |
c5a04734 | 2050 | |
5d6342eb TT |
2051 | /* Return a string representation of all the remaining tokens on the |
2052 | current line. The result is allocated using xmalloc and must be | |
2053 | freed by the caller. */ | |
2054 | unsigned char * | |
2055 | cpp_output_line_to_string (cpp_reader *pfile, const unsigned char *dir_name) | |
2056 | { | |
2057 | const cpp_token *token; | |
2058 | unsigned int out = dir_name ? ustrlen (dir_name) : 0; | |
2059 | unsigned int alloced = 120 + out; | |
2060 | unsigned char *result = (unsigned char *) xmalloc (alloced); | |
2061 | ||
2062 | /* If DIR_NAME is empty, there are no initial contents. */ | |
2063 | if (dir_name) | |
2064 | { | |
2065 | sprintf ((char *) result, "#%s ", dir_name); | |
2066 | out += 2; | |
2067 | } | |
2068 | ||
2069 | token = cpp_get_token (pfile); | |
2070 | while (token->type != CPP_EOF) | |
2071 | { | |
2072 | unsigned char *last; | |
2073 | /* Include room for a possible space and the terminating nul. */ | |
2074 | unsigned int len = cpp_token_len (token) + 2; | |
2075 | ||
2076 | if (out + len > alloced) | |
2077 | { | |
2078 | alloced *= 2; | |
2079 | if (out + len > alloced) | |
2080 | alloced = out + len; | |
2081 | result = (unsigned char *) xrealloc (result, alloced); | |
2082 | } | |
2083 | ||
2084 | last = cpp_spell_token (pfile, token, &result[out], 0); | |
2085 | out = last - result; | |
2086 | ||
2087 | token = cpp_get_token (pfile); | |
2088 | if (token->flags & PREV_WHITE) | |
2089 | result[out++] = ' '; | |
2090 | } | |
2091 | ||
2092 | result[out] = '\0'; | |
2093 | return result; | |
2094 | } | |
2095 | ||
1e013d2e NB |
2096 | /* Memory buffers. Changing these three constants can have a dramatic |
2097 | effect on performance. The values here are reasonable defaults, | |
2098 | but might be tuned. If you adjust them, be sure to test across a | |
2099 | range of uses of cpplib, including heavy nested function-like macro | |
2100 | expansion. Also check the change in peak memory usage (NJAMD is a | |
2101 | good tool for this). */ | |
2102 | #define MIN_BUFF_SIZE 8000 | |
87062813 | 2103 | #define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2) |
1e013d2e NB |
2104 | #define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \ |
2105 | (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2) | |
417f3e3a | 2106 | |
87062813 NB |
2107 | #if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0) |
2108 | #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE! | |
2109 | #endif | |
2110 | ||
c9e7a609 NB |
2111 | /* Create a new allocation buffer. Place the control block at the end |
2112 | of the buffer, so that buffer overflows will cause immediate chaos. */ | |
b8af0ca5 | 2113 | static _cpp_buff * |
6cf87ca4 | 2114 | new_buff (size_t len) |
b8af0ca5 NB |
2115 | { |
2116 | _cpp_buff *result; | |
ece54d54 | 2117 | unsigned char *base; |
b8af0ca5 | 2118 | |
1e013d2e NB |
2119 | if (len < MIN_BUFF_SIZE) |
2120 | len = MIN_BUFF_SIZE; | |
c70f6ed3 | 2121 | len = CPP_ALIGN (len); |
b8af0ca5 | 2122 | |
c3f829c1 | 2123 | base = XNEWVEC (unsigned char, len + sizeof (_cpp_buff)); |
b8af0ca5 NB |
2124 | result = (_cpp_buff *) (base + len); |
2125 | result->base = base; | |
2126 | result->cur = base; | |
2127 | result->limit = base + len; | |
2128 | result->next = NULL; | |
2129 | return result; | |
2130 | } | |
2131 | ||
2132 | /* Place a chain of unwanted allocation buffers on the free list. */ | |
2133 | void | |
6cf87ca4 | 2134 | _cpp_release_buff (cpp_reader *pfile, _cpp_buff *buff) |
b8af0ca5 NB |
2135 | { |
2136 | _cpp_buff *end = buff; | |
2137 | ||
2138 | while (end->next) | |
2139 | end = end->next; | |
2140 | end->next = pfile->free_buffs; | |
2141 | pfile->free_buffs = buff; | |
2142 | } | |
2143 | ||
2144 | /* Return a free buffer of size at least MIN_SIZE. */ | |
2145 | _cpp_buff * | |
6cf87ca4 | 2146 | _cpp_get_buff (cpp_reader *pfile, size_t min_size) |
b8af0ca5 NB |
2147 | { |
2148 | _cpp_buff *result, **p; | |
2149 | ||
2150 | for (p = &pfile->free_buffs;; p = &(*p)->next) | |
2151 | { | |
6142088c | 2152 | size_t size; |
1e013d2e NB |
2153 | |
2154 | if (*p == NULL) | |
b8af0ca5 | 2155 | return new_buff (min_size); |
1e013d2e NB |
2156 | result = *p; |
2157 | size = result->limit - result->base; | |
2158 | /* Return a buffer that's big enough, but don't waste one that's | |
2159 | way too big. */ | |
34f5271d | 2160 | if (size >= min_size && size <= BUFF_SIZE_UPPER_BOUND (min_size)) |
b8af0ca5 NB |
2161 | break; |
2162 | } | |
2163 | ||
2164 | *p = result->next; | |
2165 | result->next = NULL; | |
2166 | result->cur = result->base; | |
2167 | return result; | |
2168 | } | |
2169 | ||
4fe9b91c | 2170 | /* Creates a new buffer with enough space to hold the uncommitted |
8c3b2693 NB |
2171 | remaining bytes of BUFF, and at least MIN_EXTRA more bytes. Copies |
2172 | the excess bytes to the new buffer. Chains the new buffer after | |
2173 | BUFF, and returns the new buffer. */ | |
b8af0ca5 | 2174 | _cpp_buff * |
6cf87ca4 | 2175 | _cpp_append_extend_buff (cpp_reader *pfile, _cpp_buff *buff, size_t min_extra) |
b8af0ca5 | 2176 | { |
6142088c | 2177 | size_t size = EXTENDED_BUFF_SIZE (buff, min_extra); |
8c3b2693 | 2178 | _cpp_buff *new_buff = _cpp_get_buff (pfile, size); |
b8af0ca5 | 2179 | |
8c3b2693 NB |
2180 | buff->next = new_buff; |
2181 | memcpy (new_buff->base, buff->cur, BUFF_ROOM (buff)); | |
2182 | return new_buff; | |
2183 | } | |
2184 | ||
4fe9b91c | 2185 | /* Creates a new buffer with enough space to hold the uncommitted |
8c3b2693 NB |
2186 | remaining bytes of the buffer pointed to by BUFF, and at least |
2187 | MIN_EXTRA more bytes. Copies the excess bytes to the new buffer. | |
2188 | Chains the new buffer before the buffer pointed to by BUFF, and | |
2189 | updates the pointer to point to the new buffer. */ | |
2190 | void | |
6cf87ca4 | 2191 | _cpp_extend_buff (cpp_reader *pfile, _cpp_buff **pbuff, size_t min_extra) |
8c3b2693 NB |
2192 | { |
2193 | _cpp_buff *new_buff, *old_buff = *pbuff; | |
2194 | size_t size = EXTENDED_BUFF_SIZE (old_buff, min_extra); | |
2195 | ||
2196 | new_buff = _cpp_get_buff (pfile, size); | |
2197 | memcpy (new_buff->base, old_buff->cur, BUFF_ROOM (old_buff)); | |
2198 | new_buff->next = old_buff; | |
2199 | *pbuff = new_buff; | |
b8af0ca5 NB |
2200 | } |
2201 | ||
2202 | /* Free a chain of buffers starting at BUFF. */ | |
2203 | void | |
5671bf27 | 2204 | _cpp_free_buff (_cpp_buff *buff) |
b8af0ca5 NB |
2205 | { |
2206 | _cpp_buff *next; | |
2207 | ||
2208 | for (; buff; buff = next) | |
2209 | { | |
2210 | next = buff->next; | |
2211 | free (buff->base); | |
2212 | } | |
2213 | } | |
417f3e3a | 2214 | |
ece54d54 NB |
2215 | /* Allocate permanent, unaligned storage of length LEN. */ |
2216 | unsigned char * | |
6cf87ca4 | 2217 | _cpp_unaligned_alloc (cpp_reader *pfile, size_t len) |
ece54d54 NB |
2218 | { |
2219 | _cpp_buff *buff = pfile->u_buff; | |
2220 | unsigned char *result = buff->cur; | |
2221 | ||
2222 | if (len > (size_t) (buff->limit - result)) | |
2223 | { | |
2224 | buff = _cpp_get_buff (pfile, len); | |
2225 | buff->next = pfile->u_buff; | |
2226 | pfile->u_buff = buff; | |
2227 | result = buff->cur; | |
2228 | } | |
2229 | ||
2230 | buff->cur = result + len; | |
2231 | return result; | |
2232 | } | |
2233 | ||
87062813 NB |
2234 | /* Allocate permanent, unaligned storage of length LEN from a_buff. |
2235 | That buffer is used for growing allocations when saving macro | |
2236 | replacement lists in a #define, and when parsing an answer to an | |
2237 | assertion in #assert, #unassert or #if (and therefore possibly | |
2238 | whilst expanding macros). It therefore must not be used by any | |
2239 | code that they might call: specifically the lexer and the guts of | |
2240 | the macro expander. | |
2241 | ||
2242 | All existing other uses clearly fit this restriction: storing | |
2243 | registered pragmas during initialization. */ | |
93c80368 | 2244 | unsigned char * |
6cf87ca4 | 2245 | _cpp_aligned_alloc (cpp_reader *pfile, size_t len) |
3fef5b2b | 2246 | { |
8c3b2693 NB |
2247 | _cpp_buff *buff = pfile->a_buff; |
2248 | unsigned char *result = buff->cur; | |
3fef5b2b | 2249 | |
8c3b2693 | 2250 | if (len > (size_t) (buff->limit - result)) |
3fef5b2b | 2251 | { |
8c3b2693 NB |
2252 | buff = _cpp_get_buff (pfile, len); |
2253 | buff->next = pfile->a_buff; | |
2254 | pfile->a_buff = buff; | |
2255 | result = buff->cur; | |
3fef5b2b | 2256 | } |
041c3194 | 2257 | |
8c3b2693 | 2258 | buff->cur = result + len; |
93c80368 | 2259 | return result; |
041c3194 | 2260 | } |
d8044160 GK |
2261 | |
2262 | /* Say which field of TOK is in use. */ | |
2263 | ||
2264 | enum cpp_token_fld_kind | |
2265 | cpp_token_val_index (cpp_token *tok) | |
2266 | { | |
2267 | switch (TOKEN_SPELL (tok)) | |
2268 | { | |
2269 | case SPELL_IDENT: | |
2270 | return CPP_TOKEN_FLD_NODE; | |
2271 | case SPELL_LITERAL: | |
2272 | return CPP_TOKEN_FLD_STR; | |
aa508502 JM |
2273 | case SPELL_OPERATOR: |
2274 | if (tok->type == CPP_PASTE) | |
9a0c6187 | 2275 | return CPP_TOKEN_FLD_TOKEN_NO; |
aa508502 JM |
2276 | else |
2277 | return CPP_TOKEN_FLD_NONE; | |
d8044160 GK |
2278 | case SPELL_NONE: |
2279 | if (tok->type == CPP_MACRO_ARG) | |
2280 | return CPP_TOKEN_FLD_ARG_NO; | |
2281 | else if (tok->type == CPP_PADDING) | |
2282 | return CPP_TOKEN_FLD_SOURCE; | |
21b11495 | 2283 | else if (tok->type == CPP_PRAGMA) |
bc4071dd | 2284 | return CPP_TOKEN_FLD_PRAGMA; |
d8044160 GK |
2285 | /* else fall through */ |
2286 | default: | |
2287 | return CPP_TOKEN_FLD_NONE; | |
2288 | } | |
2289 | } |