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