]>
Commit | Line | Data |
---|---|---|
0578f103 | 1 | /* CPP Library - lexical analysis. |
e484a1cc | 2 | Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. |
0578f103 | 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" | |
805e22b2 | 24 | #include "coretypes.h" |
25 | #include "tm.h" | |
0578f103 | 26 | #include "cpplib.h" |
27 | #include "cpphash.h" | |
28 | ||
79bd622b | 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 | |
241e762e | 32 | { |
79bd622b | 33 | SPELL_OPERATOR = 0, |
34 | SPELL_CHAR, | |
35 | SPELL_IDENT, | |
8d27e472 | 36 | SPELL_NUMBER, |
79bd622b | 37 | SPELL_STRING, |
38 | SPELL_NONE | |
241e762e | 39 | }; |
40 | ||
79bd622b | 41 | struct token_spelling |
241e762e | 42 | { |
79bd622b | 43 | enum spell_type category; |
44 | const unsigned char *name; | |
241e762e | 45 | }; |
46 | ||
0ca849f9 | 47 | static const unsigned char *const digraph_spellings[] = |
48 | { U"%:", U"%:%:", U"<:", U":>", U"<%", U"%>" }; | |
79bd622b | 49 | |
50 | #define OP(e, s) { SPELL_OPERATOR, U s }, | |
51 | #define TK(e, s) { s, U STRINGX (e) }, | |
0ca849f9 | 52 | static const struct token_spelling token_spellings[N_TTYPES] = { TTYPE_TABLE }; |
79bd622b | 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) | |
e2f9a79f | 58 | |
a54e0bf8 | 59 | static void add_line_note PARAMS ((cpp_buffer *, const uchar *, unsigned int)); |
f669338a | 60 | static int skip_line_comment PARAMS ((cpp_reader *)); |
a54e0bf8 | 61 | static void skip_whitespace PARAMS ((cpp_reader *, cppchar_t)); |
2cbf1359 | 62 | static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *)); |
5bb46c08 | 63 | static void lex_number PARAMS ((cpp_reader *, cpp_string *)); |
2cbf1359 | 64 | static bool forms_identifier_p PARAMS ((cpp_reader *, int)); |
5bb46c08 | 65 | static void lex_string PARAMS ((cpp_reader *, cpp_token *)); |
b6d18b0a | 66 | static void save_comment PARAMS ((cpp_reader *, cpp_token *, const uchar *, |
d3f7919d | 67 | cppchar_t)); |
79bd622b | 68 | static int name_p PARAMS ((cpp_reader *, const cpp_string *)); |
2cbf1359 | 69 | static cppchar_t maybe_read_ucn PARAMS ((cpp_reader *, const uchar **)); |
83dcbb5c | 70 | static tokenrun *next_tokenrun PARAMS ((tokenrun *)); |
e916a356 | 71 | |
8330799c | 72 | static unsigned int hex_digit_value PARAMS ((unsigned int)); |
4b31a107 | 73 | static _cpp_buff *new_buff PARAMS ((size_t)); |
bce8e0c0 | 74 | |
e920deaf | 75 | |
f80e83a9 | 76 | /* Utility routine: |
2c63d6c8 | 77 | |
76faa4c0 | 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. */ | |
f80e83a9 | 80 | int |
76faa4c0 | 81 | cpp_ideq (token, string) |
82 | const cpp_token *token; | |
f80e83a9 | 83 | const char *string; |
84 | { | |
76faa4c0 | 85 | if (token->type != CPP_NAME) |
f80e83a9 | 86 | return 0; |
76faa4c0 | 87 | |
b6d18b0a | 88 | return !ustrcmp (NODE_NAME (token->val.node), (const uchar *) string); |
bce8e0c0 | 89 | } |
50fd6b48 | 90 | |
a54e0bf8 | 91 | /* Record a note TYPE at byte POS into the current cleaned logical |
92 | line. */ | |
1e0ef2fd | 93 | static void |
a54e0bf8 | 94 | add_line_note (buffer, pos, type) |
95 | cpp_buffer *buffer; | |
96 | const uchar *pos; | |
97 | unsigned int type; | |
338fa5f7 | 98 | { |
a54e0bf8 | 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 | } | |
338fa5f7 | 105 | |
a54e0bf8 | 106 | buffer->notes[buffer->notes_used].pos = pos; |
107 | buffer->notes[buffer->notes_used].type = type; | |
108 | buffer->notes_used++; | |
338fa5f7 | 109 | } |
110 | ||
a54e0bf8 | 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) | |
0578f103 | 115 | cpp_reader *pfile; |
0578f103 | 116 | { |
a54e0bf8 | 117 | cpp_buffer *buffer; |
118 | const uchar *s; | |
119 | uchar c, *d, *p; | |
1e0ef2fd | 120 | |
a54e0bf8 | 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; | |
1e0ef2fd | 126 | |
a54e0bf8 | 127 | if (!buffer->from_stage3) |
0578f103 | 128 | { |
a54e0bf8 | 129 | d = (uchar *) s; |
130 | ||
131 | for (;;) | |
4b912310 | 132 | { |
a54e0bf8 | 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 | } | |
4b912310 | 166 | } |
0578f103 | 167 | } |
a54e0bf8 | 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 | } | |
338fa5f7 | 179 | |
a54e0bf8 | 180 | *d = '\n'; |
181 | add_line_note (buffer, d + 1, NOTE_NEWLINE); | |
182 | buffer->next_line = s + 1; | |
0578f103 | 183 | } |
184 | ||
a54e0bf8 | 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) | |
c808d026 | 189 | cpp_reader *pfile; |
a54e0bf8 | 190 | int in_comment; |
0578f103 | 191 | { |
c808d026 | 192 | cpp_buffer *buffer = pfile->buffer; |
193 | ||
a54e0bf8 | 194 | for (;;) |
f80e83a9 | 195 | { |
a54e0bf8 | 196 | _cpp_line_note *note = &buffer->notes[buffer->cur_note]; |
197 | unsigned int col; | |
396ffa86 | 198 | |
a54e0bf8 | 199 | if (note->pos > buffer->cur) |
200 | break; | |
396ffa86 | 201 | |
a54e0bf8 | 202 | buffer->cur_note++; |
203 | col = CPP_BUF_COLUMN (buffer, note->pos + 1); | |
435fb09b | 204 | |
a54e0bf8 | 205 | switch (note->type) |
206 | { | |
207 | case NOTE_NEWLINE: | |
208 | /* This note is a kind of sentinel we should never reach. */ | |
209 | abort (); | |
f80e83a9 | 210 | |
a54e0bf8 | 211 | case NOTE_TRIGRAPH: |
212 | if (!in_comment && CPP_OPTION (pfile, warn_trigraphs)) | |
338fa5f7 | 213 | { |
a54e0bf8 | 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]); | |
338fa5f7 | 222 | } |
a54e0bf8 | 223 | break; |
0578f103 | 224 | |
a54e0bf8 | 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) | |
1e0ef2fd | 232 | { |
a54e0bf8 | 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; | |
1e0ef2fd | 237 | } |
a54e0bf8 | 238 | |
239 | buffer->line_base = note->pos; | |
240 | pfile->line++; | |
338fa5f7 | 241 | } |
f80e83a9 | 242 | } |
0578f103 | 243 | } |
244 | ||
338fa5f7 | 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 | |
edaf8cb5 | 247 | nonzero if comment terminated by EOF, zero otherwise. |
248 | ||
249 | Buffer->cur points to the initial asterisk of the comment. */ | |
a54e0bf8 | 250 | bool |
251 | _cpp_skip_block_comment (pfile) | |
0578f103 | 252 | cpp_reader *pfile; |
253 | { | |
f80e83a9 | 254 | cpp_buffer *buffer = pfile->buffer; |
a54e0bf8 | 255 | cppchar_t c; |
338fa5f7 | 256 | |
edaf8cb5 | 257 | buffer->cur++; |
a54e0bf8 | 258 | if (*buffer->cur == '/') |
259 | buffer->cur++; | |
338fa5f7 | 260 | |
a54e0bf8 | 261 | for (;;) |
262 | { | |
263 | c = *buffer->cur++; | |
f80e83a9 | 264 | |
338fa5f7 | 265 | /* People like decorating comments with '*', so check for '/' |
266 | instead for efficiency. */ | |
f80e83a9 | 267 | if (c == '/') |
0578f103 | 268 | { |
a54e0bf8 | 269 | if (buffer->cur[-2] == '*') |
338fa5f7 | 270 | break; |
f80e83a9 | 271 | |
338fa5f7 | 272 | /* Warn about potential nested comments, but not if the '/' |
3fb1e43b | 273 | comes immediately before the true comment delimiter. |
f80e83a9 | 274 | Don't bother to get it right across escaped newlines. */ |
338fa5f7 | 275 | if (CPP_OPTION (pfile, warn_comments) |
1e0ef2fd | 276 | && buffer->cur[0] == '*' && buffer->cur[1] != '/') |
73328dce | 277 | cpp_error_with_line (pfile, DL_WARNING, |
278 | pfile->line, CPP_BUF_COL (buffer), | |
279 | "\"/*\" within comment"); | |
0578f103 | 280 | } |
a54e0bf8 | 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 | } | |
0578f103 | 290 | } |
f80e83a9 | 291 | |
a54e0bf8 | 292 | return false; |
0578f103 | 293 | } |
294 | ||
1c124f85 | 295 | /* Skip a C++ line comment, leaving buffer->cur pointing to the |
d10cfa8d | 296 | terminating newline. Handles escaped newlines. Returns nonzero |
1c124f85 | 297 | if a multiline comment. */ |
f80e83a9 | 298 | static int |
f669338a | 299 | skip_line_comment (pfile) |
300 | cpp_reader *pfile; | |
0578f103 | 301 | { |
f669338a | 302 | cpp_buffer *buffer = pfile->buffer; |
1ea7ed21 | 303 | unsigned int orig_line = pfile->line; |
f80e83a9 | 304 | |
a54e0bf8 | 305 | while (*buffer->cur != '\n') |
306 | buffer->cur++; | |
1c124f85 | 307 | |
a54e0bf8 | 308 | _cpp_process_line_notes (pfile, true); |
1ea7ed21 | 309 | return orig_line != pfile->line; |
f80e83a9 | 310 | } |
0578f103 | 311 | |
a54e0bf8 | 312 | /* Skips whitespace, saving the next non-whitespace character. */ |
b86584f6 | 313 | static void |
338fa5f7 | 314 | skip_whitespace (pfile, c) |
f80e83a9 | 315 | cpp_reader *pfile; |
338fa5f7 | 316 | cppchar_t c; |
f80e83a9 | 317 | { |
318 | cpp_buffer *buffer = pfile->buffer; | |
fe9eb18b | 319 | bool saw_NUL = false; |
0578f103 | 320 | |
338fa5f7 | 321 | do |
f80e83a9 | 322 | { |
78719282 | 323 | /* Horizontal space always OK. */ |
a54e0bf8 | 324 | if (c == ' ' || c == '\t') |
338fa5f7 | 325 | ; |
338fa5f7 | 326 | /* Just \f \v or \0 left. */ |
78719282 | 327 | else if (c == '\0') |
fe9eb18b | 328 | saw_NUL = true; |
79bd622b | 329 | else if (pfile->state.in_directive && CPP_PEDANTIC (pfile)) |
73328dce | 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"); | |
338fa5f7 | 334 | |
338fa5f7 | 335 | c = *buffer->cur++; |
0578f103 | 336 | } |
2c0e001b | 337 | /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */ |
338fa5f7 | 338 | while (is_nvspace (c)); |
339 | ||
fe9eb18b | 340 | if (saw_NUL) |
341 | cpp_error (pfile, DL_WARNING, "null character(s) ignored"); | |
342 | ||
1c124f85 | 343 | buffer->cur--; |
f80e83a9 | 344 | } |
0578f103 | 345 | |
79bd622b | 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 | ||
b1a9ff83 | 359 | return 1; |
79bd622b | 360 | } |
361 | ||
5bb46c08 | 362 | /* Returns TRUE if the sequence starting at buffer->cur is invalid in |
2cbf1359 | 363 | an identifier. FIRST is TRUE if this starts an identifier. */ |
5bb46c08 | 364 | static bool |
2cbf1359 | 365 | forms_identifier_p (pfile, first) |
5bb46c08 | 366 | cpp_reader *pfile; |
2cbf1359 | 367 | int first; |
5bb46c08 | 368 | { |
2cbf1359 | 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 | } | |
5bb46c08 | 387 | |
2cbf1359 | 388 | /* Is this a syntactically valid UCN? */ |
389 | if (0 && *buffer->cur == '\\' | |
390 | && (buffer->cur[1] == 'u' || buffer->cur[1] == 'U')) | |
5bb46c08 | 391 | { |
2cbf1359 | 392 | buffer->cur += 2; |
393 | if (_cpp_valid_ucn (pfile, &buffer->cur, 1 + !first)) | |
394 | return true; | |
395 | buffer->cur -= 2; | |
5bb46c08 | 396 | } |
5bb46c08 | 397 | |
2cbf1359 | 398 | return false; |
5bb46c08 | 399 | } |
400 | ||
401 | /* Lex an identifier starting at BUFFER->CUR - 1. */ | |
338fa5f7 | 402 | static cpp_hashnode * |
2cbf1359 | 403 | lex_identifier (pfile, base) |
0578f103 | 404 | cpp_reader *pfile; |
2cbf1359 | 405 | const uchar *base; |
0578f103 | 406 | { |
79bd622b | 407 | cpp_hashnode *result; |
2cbf1359 | 408 | const uchar *cur; |
66a5287e | 409 | |
5bb46c08 | 410 | do |
78a11351 | 411 | { |
5bb46c08 | 412 | cur = pfile->buffer->cur; |
413 | ||
414 | /* N.B. ISIDNUM does not include $. */ | |
415 | while (ISIDNUM (*cur)) | |
416 | cur++; | |
78a11351 | 417 | |
78a11351 | 418 | pfile->buffer->cur = cur; |
66a5287e | 419 | } |
2cbf1359 | 420 | while (forms_identifier_p (pfile, false)); |
5bb46c08 | 421 | |
422 | result = (cpp_hashnode *) | |
423 | ht_lookup (pfile->hash_table, base, cur - base, HT_ALLOC); | |
66a5287e | 424 | |
5bb46c08 | 425 | /* Rarely, identifiers require diagnostics when lexed. */ |
66a5287e | 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) | |
73328dce | 431 | cpp_error (pfile, DL_ERROR, "attempt to use poisoned \"%s\"", |
66a5287e | 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) | |
73328dce | 438 | cpp_error (pfile, DL_PEDWARN, |
66a5287e | 439 | "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro"); |
440 | } | |
441 | ||
442 | return result; | |
443 | } | |
444 | ||
5bb46c08 | 445 | /* Lex a number to NUMBER starting at BUFFER->CUR - 1. */ |
0578f103 | 446 | static void |
5bb46c08 | 447 | lex_number (pfile, number) |
0578f103 | 448 | cpp_reader *pfile; |
338fa5f7 | 449 | cpp_string *number; |
0578f103 | 450 | { |
b6d18b0a | 451 | const uchar *cur; |
5bb46c08 | 452 | const uchar *base; |
453 | uchar *dest; | |
0578f103 | 454 | |
5bb46c08 | 455 | base = pfile->buffer->cur - 1; |
456 | do | |
f80e83a9 | 457 | { |
5bb46c08 | 458 | cur = pfile->buffer->cur; |
338fa5f7 | 459 | |
5bb46c08 | 460 | /* N.B. ISIDNUM does not include $. */ |
461 | while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1])) | |
462 | cur++; | |
0578f103 | 463 | |
78a11351 | 464 | pfile->buffer->cur = cur; |
0578f103 | 465 | } |
2cbf1359 | 466 | while (forms_identifier_p (pfile, false)); |
79bd622b | 467 | |
5bb46c08 | 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; | |
79bd622b | 473 | } |
474 | ||
5bb46c08 | 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. */ | |
f80e83a9 | 479 | static void |
5bb46c08 | 480 | lex_string (pfile, token) |
0578f103 | 481 | cpp_reader *pfile; |
f80e83a9 | 482 | cpp_token *token; |
0578f103 | 483 | { |
f80e83a9 | 484 | cpp_buffer *buffer = pfile->buffer; |
34cf330f | 485 | bool warned_nulls = false; |
5bb46c08 | 486 | const uchar *base; |
487 | uchar *dest; | |
488 | cppchar_t terminator; | |
338fa5f7 | 489 | |
5bb46c08 | 490 | base = buffer->cur; |
491 | terminator = base[-1]; | |
492 | if (terminator == '<') | |
493 | terminator = '>'; | |
79bd622b | 494 | |
338fa5f7 | 495 | for (;;) |
0578f103 | 496 | { |
5bb46c08 | 497 | cppchar_t c = *buffer->cur++; |
4b0c16ee | 498 | |
edaf8cb5 | 499 | /* In #include-style directives, terminators are not escapable. */ |
5bb46c08 | 500 | if (c == '\\' && !pfile->state.angled_headers && *buffer->cur != '\n') |
501 | buffer->cur++; | |
502 | else if (c == terminator || c == '\n') | |
503 | break; | |
435fb09b | 504 | else if (c == '\0') |
338fa5f7 | 505 | { |
435fb09b | 506 | if (!warned_nulls) |
507 | { | |
508 | warned_nulls = true; | |
73328dce | 509 | cpp_error (pfile, DL_WARNING, |
510 | "null character(s) preserved in literal"); | |
435fb09b | 511 | } |
0578f103 | 512 | } |
0578f103 | 513 | } |
514 | ||
5bb46c08 | 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; | |
0578f103 | 520 | |
5bb46c08 | 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 | } | |
338fa5f7 | 532 | } |
f80e83a9 | 533 | |
79bd622b | 534 | /* The stored comment includes the comment start and any terminator. */ |
2c63d6c8 | 535 | static void |
d3f7919d | 536 | save_comment (pfile, token, from, type) |
338fa5f7 | 537 | cpp_reader *pfile; |
f80e83a9 | 538 | cpp_token *token; |
539 | const unsigned char *from; | |
d3f7919d | 540 | cppchar_t type; |
2c63d6c8 | 541 | { |
f80e83a9 | 542 | unsigned char *buffer; |
d3f7919d | 543 | unsigned int len, clen; |
b1a9ff83 | 544 | |
f0495c2c | 545 | len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */ |
1c124f85 | 546 | |
a543b315 | 547 | /* C++ comments probably (not definitely) have moved past a new |
548 | line, which we don't want to save in the comment. */ | |
1c124f85 | 549 | if (is_vspace (pfile->buffer->cur[-1])) |
a543b315 | 550 | len--; |
d3f7919d | 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); | |
b1a9ff83 | 561 | |
f80e83a9 | 562 | token->type = CPP_COMMENT; |
d3f7919d | 563 | token->val.str.len = clen; |
338fa5f7 | 564 | token->val.str.text = buffer; |
0578f103 | 565 | |
f0495c2c | 566 | buffer[0] = '/'; |
567 | memcpy (buffer + 1, from, len - 1); | |
d3f7919d | 568 | |
a113df96 | 569 | /* Finish conversion to a C comment, if necessary. */ |
d3f7919d | 570 | if (pfile->state.in_directive && type == '/') |
571 | { | |
572 | buffer[1] = '*'; | |
573 | buffer[clen - 2] = '*'; | |
574 | buffer[clen - 1] = '/'; | |
575 | } | |
338fa5f7 | 576 | } |
0578f103 | 577 | |
83dcbb5c | 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); | |
fb5ab82c | 597 | run->next->prev = run; |
83dcbb5c | 598 | _cpp_init_tokenrun (run->next, 250); |
599 | } | |
600 | ||
601 | return run->next; | |
602 | } | |
603 | ||
f9b5f742 | 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 | ||
10b4496a | 627 | /* Lex a token into RESULT (external interface). Takes care of issues |
628 | like directive handling, token lookahead, multiple include | |
3fb1e43b | 629 | optimization and skipping. */ |
c00e481c | 630 | const cpp_token * |
631 | _cpp_lex_token (pfile) | |
0578f103 | 632 | cpp_reader *pfile; |
83dcbb5c | 633 | { |
fb5ab82c | 634 | cpp_token *result; |
83dcbb5c | 635 | |
fb5ab82c | 636 | for (;;) |
83dcbb5c | 637 | { |
fb5ab82c | 638 | if (pfile->cur_token == pfile->cur_run->limit) |
83dcbb5c | 639 | { |
fb5ab82c | 640 | pfile->cur_run = next_tokenrun (pfile->cur_run); |
641 | pfile->cur_token = pfile->cur_run->base; | |
83dcbb5c | 642 | } |
643 | ||
fb5ab82c | 644 | if (pfile->lookaheads) |
10b4496a | 645 | { |
646 | pfile->lookaheads--; | |
647 | result = pfile->cur_token++; | |
648 | } | |
fb5ab82c | 649 | else |
10b4496a | 650 | result = _cpp_lex_direct (pfile); |
fb5ab82c | 651 | |
652 | if (result->flags & BOL) | |
83dcbb5c | 653 | { |
fb5ab82c | 654 | /* Is this a directive. If _cpp_handle_directive returns |
655 | false, it is an assembler #. */ | |
656 | if (result->type == CPP_HASH | |
d6af0368 | 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 | |
fb5ab82c | 661 | && _cpp_handle_directive (pfile, result->flags & PREV_WHITE)) |
662 | continue; | |
5621a364 | 663 | if (pfile->cb.line_change && !pfile->state.skipping) |
664 | (*pfile->cb.line_change)(pfile, result, pfile->state.parsing_args); | |
83dcbb5c | 665 | } |
83dcbb5c | 666 | |
fb5ab82c | 667 | /* We don't skip tokens in directives. */ |
668 | if (pfile->state.in_directive) | |
669 | break; | |
83dcbb5c | 670 | |
fb5ab82c | 671 | /* Outside a directive, invalidate controlling macros. At file |
10b4496a | 672 | EOF, _cpp_lex_direct takes care of popping the buffer, so we never |
fb5ab82c | 673 | get here and MI optimisation works. */ |
83dcbb5c | 674 | pfile->mi_valid = false; |
fb5ab82c | 675 | |
676 | if (!pfile->state.skipping || result->type == CPP_EOF) | |
677 | break; | |
83dcbb5c | 678 | } |
679 | ||
c00e481c | 680 | return result; |
83dcbb5c | 681 | } |
682 | ||
a54e0bf8 | 683 | /* Returns true if a fresh line has been loaded. */ |
684 | bool | |
685 | _cpp_get_fresh_line (pfile) | |
0bb65704 | 686 | cpp_reader *pfile; |
687 | { | |
a54e0bf8 | 688 | /* We can't get a new line until we leave the current directive. */ |
689 | if (pfile->state.in_directive) | |
690 | return false; | |
b1a9ff83 | 691 | |
a54e0bf8 | 692 | for (;;) |
fb83e0d6 | 693 | { |
a54e0bf8 | 694 | cpp_buffer *buffer = pfile->buffer; |
fb83e0d6 | 695 | |
a54e0bf8 | 696 | if (!buffer->need_line) |
697 | return true; | |
698 | ||
699 | if (buffer->next_line < buffer->rlimit) | |
0bb65704 | 700 | { |
a54e0bf8 | 701 | _cpp_clean_line (pfile); |
702 | return true; | |
703 | } | |
0bb65704 | 704 | |
a54e0bf8 | 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; | |
0bb65704 | 725 | } |
0bb65704 | 726 | |
a54e0bf8 | 727 | if (!buffer->prev) |
728 | return false; | |
729 | ||
730 | _cpp_pop_buffer (pfile); | |
731 | } | |
0bb65704 | 732 | } |
733 | ||
edaf8cb5 | 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) | |
1c124f85 | 742 | |
10b4496a | 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) | |
83dcbb5c | 756 | cpp_reader *pfile; |
0578f103 | 757 | { |
338fa5f7 | 758 | cppchar_t c; |
230f0943 | 759 | cpp_buffer *buffer; |
338fa5f7 | 760 | const unsigned char *comment_start; |
10b4496a | 761 | cpp_token *result = pfile->cur_token++; |
0653b94e | 762 | |
83dcbb5c | 763 | fresh_line: |
a54e0bf8 | 764 | result->flags = 0; |
765 | if (pfile->buffer->need_line) | |
766 | { | |
767 | if (!_cpp_get_fresh_line (pfile)) | |
768 | { | |
769 | result->type = CPP_EOF; | |
911483bc | 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 | } | |
a54e0bf8 | 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 | } | |
230f0943 | 788 | buffer = pfile->buffer; |
83dcbb5c | 789 | update_tokens_line: |
36a0aa7c | 790 | result->line = pfile->line; |
f80e83a9 | 791 | |
83dcbb5c | 792 | skipped_white: |
a54e0bf8 | 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 | } | |
1c124f85 | 799 | c = *buffer->cur++; |
83dcbb5c | 800 | result->col = CPP_BUF_COLUMN (buffer, buffer->cur); |
83dcbb5c | 801 | |
338fa5f7 | 802 | switch (c) |
0578f103 | 803 | { |
435fb09b | 804 | case ' ': case '\t': case '\f': case '\v': case '\0': |
805 | result->flags |= PREV_WHITE; | |
a54e0bf8 | 806 | skip_whitespace (pfile, c); |
807 | goto skipped_white; | |
338fa5f7 | 808 | |
a54e0bf8 | 809 | case '\n': |
810 | pfile->line++; | |
811 | buffer->need_line = true; | |
812 | goto fresh_line; | |
732cb4c9 | 813 | |
338fa5f7 | 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; | |
5bb46c08 | 817 | lex_number (pfile, &result->val.str); |
338fa5f7 | 818 | break; |
732cb4c9 | 819 | |
78c551ad | 820 | case 'L': |
821 | /* 'L' may introduce wide characters or strings. */ | |
5bb46c08 | 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 | } | |
b1a9ff83 | 829 | /* Fall through. */ |
78c551ad | 830 | |
338fa5f7 | 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': | |
78c551ad | 838 | case 'G': case 'H': case 'I': case 'J': case 'K': |
338fa5f7 | 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; | |
2cbf1359 | 843 | result->val.node = lex_identifier (pfile, buffer->cur - 1); |
338fa5f7 | 844 | |
338fa5f7 | 845 | /* Convert named operators to their proper types. */ |
78c551ad | 846 | if (result->val.node->flags & NODE_OPERATOR) |
338fa5f7 | 847 | { |
848 | result->flags |= NAMED_OP; | |
805e22b2 | 849 | result->type = result->val.node->directive_index; |
338fa5f7 | 850 | } |
851 | break; | |
852 | ||
853 | case '\'': | |
854 | case '"': | |
855 | result->type = c == '"' ? CPP_STRING: CPP_CHAR; | |
5bb46c08 | 856 | lex_string (pfile, result); |
338fa5f7 | 857 | break; |
f80e83a9 | 858 | |
338fa5f7 | 859 | case '/': |
f0495c2c | 860 | /* A potential block or line comment. */ |
861 | comment_start = buffer->cur; | |
edaf8cb5 | 862 | c = *buffer->cur; |
863 | ||
f0495c2c | 864 | if (c == '*') |
865 | { | |
a54e0bf8 | 866 | if (_cpp_skip_block_comment (pfile)) |
73328dce | 867 | cpp_error (pfile, DL_ERROR, "unterminated comment"); |
338fa5f7 | 868 | } |
1c124f85 | 869 | else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments) |
870 | || CPP_IN_SYSTEM_HEADER (pfile))) | |
338fa5f7 | 871 | { |
5db5d057 | 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) | |
66914e49 | 875 | && ! buffer->warned_cplusplus_comments) |
f80e83a9 | 876 | { |
73328dce | 877 | cpp_error (pfile, DL_PEDWARN, |
ba059ac0 | 878 | "C++ style comments are not allowed in ISO C90"); |
73328dce | 879 | cpp_error (pfile, DL_PEDWARN, |
880 | "(this will be reported only once per input file)"); | |
f0495c2c | 881 | buffer->warned_cplusplus_comments = 1; |
882 | } | |
338fa5f7 | 883 | |
e1caf668 | 884 | if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments)) |
73328dce | 885 | cpp_error (pfile, DL_WARNING, "multi-line comment"); |
f0495c2c | 886 | } |
1c124f85 | 887 | else if (c == '=') |
888 | { | |
edaf8cb5 | 889 | buffer->cur++; |
1c124f85 | 890 | result->type = CPP_DIV_EQ; |
891 | break; | |
892 | } | |
893 | else | |
894 | { | |
1c124f85 | 895 | result->type = CPP_DIV; |
896 | break; | |
897 | } | |
338fa5f7 | 898 | |
f0495c2c | 899 | if (!pfile->state.save_comments) |
900 | { | |
901 | result->flags |= PREV_WHITE; | |
83dcbb5c | 902 | goto update_tokens_line; |
338fa5f7 | 903 | } |
f0495c2c | 904 | |
905 | /* Save the comment as a token in its own right. */ | |
d3f7919d | 906 | save_comment (pfile, result, comment_start, c); |
fb5ab82c | 907 | break; |
338fa5f7 | 908 | |
909 | case '<': | |
910 | if (pfile->state.angled_headers) | |
911 | { | |
912 | result->type = CPP_HEADER_NAME; | |
5bb46c08 | 913 | lex_string (pfile, result); |
1c124f85 | 914 | break; |
338fa5f7 | 915 | } |
0578f103 | 916 | |
edaf8cb5 | 917 | result->type = CPP_LESS; |
918 | if (*buffer->cur == '=') | |
919 | buffer->cur++, result->type = CPP_LESS_EQ; | |
920 | else if (*buffer->cur == '<') | |
338fa5f7 | 921 | { |
edaf8cb5 | 922 | buffer->cur++; |
923 | IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT); | |
338fa5f7 | 924 | } |
edaf8cb5 | 925 | else if (*buffer->cur == '?' && CPP_OPTION (pfile, cplusplus)) |
338fa5f7 | 926 | { |
edaf8cb5 | 927 | buffer->cur++; |
928 | IF_NEXT_IS ('=', CPP_MIN_EQ, CPP_MIN); | |
338fa5f7 | 929 | } |
edaf8cb5 | 930 | else if (CPP_OPTION (pfile, digraphs)) |
1c124f85 | 931 | { |
edaf8cb5 | 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 | } | |
1c124f85 | 944 | } |
338fa5f7 | 945 | break; |
946 | ||
947 | case '>': | |
edaf8cb5 | 948 | result->type = CPP_GREATER; |
949 | if (*buffer->cur == '=') | |
950 | buffer->cur++, result->type = CPP_GREATER_EQ; | |
951 | else if (*buffer->cur == '>') | |
338fa5f7 | 952 | { |
edaf8cb5 | 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); | |
338fa5f7 | 960 | } |
961 | break; | |
962 | ||
f669338a | 963 | case '%': |
edaf8cb5 | 964 | result->type = CPP_MOD; |
965 | if (*buffer->cur == '=') | |
966 | buffer->cur++, result->type = CPP_MOD_EQ; | |
967 | else if (CPP_OPTION (pfile, digraphs)) | |
1c124f85 | 968 | { |
edaf8cb5 | 969 | if (*buffer->cur == ':') |
1c124f85 | 970 | { |
edaf8cb5 | 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; | |
1c124f85 | 982 | } |
1c124f85 | 983 | } |
338fa5f7 | 984 | break; |
985 | ||
f669338a | 986 | case '.': |
1c124f85 | 987 | result->type = CPP_DOT; |
edaf8cb5 | 988 | if (ISDIGIT (*buffer->cur)) |
1c124f85 | 989 | { |
990 | result->type = CPP_NUMBER; | |
5bb46c08 | 991 | lex_number (pfile, &result->val.str); |
1c124f85 | 992 | } |
edaf8cb5 | 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; | |
338fa5f7 | 997 | break; |
0578f103 | 998 | |
338fa5f7 | 999 | case '+': |
edaf8cb5 | 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; | |
338fa5f7 | 1005 | break; |
ac0749c7 | 1006 | |
338fa5f7 | 1007 | case '-': |
edaf8cb5 | 1008 | result->type = CPP_MINUS; |
1009 | if (*buffer->cur == '>') | |
338fa5f7 | 1010 | { |
edaf8cb5 | 1011 | buffer->cur++; |
1c124f85 | 1012 | result->type = CPP_DEREF; |
edaf8cb5 | 1013 | if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus)) |
1014 | buffer->cur++, result->type = CPP_DEREF_STAR; | |
1c124f85 | 1015 | } |
edaf8cb5 | 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; | |
338fa5f7 | 1020 | break; |
0578f103 | 1021 | |
338fa5f7 | 1022 | case '&': |
edaf8cb5 | 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; | |
338fa5f7 | 1028 | break; |
b1a9ff83 | 1029 | |
338fa5f7 | 1030 | case '|': |
edaf8cb5 | 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; | |
338fa5f7 | 1036 | break; |
0578f103 | 1037 | |
338fa5f7 | 1038 | case ':': |
edaf8cb5 | 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)) | |
338fa5f7 | 1043 | { |
edaf8cb5 | 1044 | buffer->cur++; |
338fa5f7 | 1045 | result->flags |= DIGRAPH; |
1c124f85 | 1046 | result->type = CPP_CLOSE_SQUARE; |
1047 | } | |
338fa5f7 | 1048 | break; |
0578f103 | 1049 | |
1c124f85 | 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 | ||
a54e0bf8 | 1056 | case '?': result->type = CPP_QUERY; break; |
338fa5f7 | 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 | ||
7fd957fe | 1067 | /* @ is a punctuator in Objective-C. */ |
9ee99ac6 | 1068 | case '@': result->type = CPP_ATSIGN; break; |
338fa5f7 | 1069 | |
78c551ad | 1070 | case '$': |
2cbf1359 | 1071 | case '\\': |
1072 | { | |
1073 | const uchar *base = --buffer->cur; | |
78c551ad | 1074 | |
2cbf1359 | 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 | } | |
338fa5f7 | 1088 | } |
fb5ab82c | 1089 | |
1090 | return result; | |
338fa5f7 | 1091 | } |
1092 | ||
e484a1cc | 1093 | /* An upper bound on the number of bytes needed to spell TOKEN, |
79bd622b | 1094 | including preceding whitespace. */ |
1095 | unsigned int | |
1096 | cpp_token_len (token) | |
1097 | const cpp_token *token; | |
338fa5f7 | 1098 | { |
79bd622b | 1099 | unsigned int len; |
cfad5579 | 1100 | |
79bd622b | 1101 | switch (TOKEN_SPELL (token)) |
f80e83a9 | 1102 | { |
c86dbc5b | 1103 | default: len = 0; break; |
8d27e472 | 1104 | case SPELL_NUMBER: |
c86dbc5b | 1105 | case SPELL_STRING: len = token->val.str.len; break; |
1106 | case SPELL_IDENT: len = NODE_LEN (token->val.node); break; | |
f80e83a9 | 1107 | } |
8d27e472 | 1108 | /* 1 for whitespace, 4 for comment delimiters. */ |
79bd622b | 1109 | return len + 5; |
cfad5579 | 1110 | } |
1111 | ||
f80e83a9 | 1112 | /* Write the spelling of a token TOKEN to BUFFER. The buffer must |
c5ea33a8 | 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. */ | |
79bd622b | 1116 | unsigned char * |
1117 | cpp_spell_token (pfile, token, buffer) | |
f80e83a9 | 1118 | cpp_reader *pfile; /* Would be nice to be rid of this... */ |
1119 | const cpp_token *token; | |
1120 | unsigned char *buffer; | |
1121 | { | |
7e842f95 | 1122 | switch (TOKEN_SPELL (token)) |
f80e83a9 | 1123 | { |
1124 | case SPELL_OPERATOR: | |
1125 | { | |
1126 | const unsigned char *spelling; | |
1127 | unsigned char c; | |
ab12a39c | 1128 | |
f80e83a9 | 1129 | if (token->flags & DIGRAPH) |
ee6c4e4b | 1130 | spelling |
1131 | = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH]; | |
31674461 | 1132 | else if (token->flags & NAMED_OP) |
1133 | goto spell_ident; | |
f80e83a9 | 1134 | else |
7e842f95 | 1135 | spelling = TOKEN_NAME (token); |
b1a9ff83 | 1136 | |
f80e83a9 | 1137 | while ((c = *spelling++) != '\0') |
1138 | *buffer++ = c; | |
1139 | } | |
1140 | break; | |
ab12a39c | 1141 | |
8d27e472 | 1142 | case SPELL_CHAR: |
1143 | *buffer++ = token->val.c; | |
1144 | break; | |
1145 | ||
1146 | spell_ident: | |
f80e83a9 | 1147 | case SPELL_IDENT: |
c86dbc5b | 1148 | memcpy (buffer, NODE_NAME (token->val.node), NODE_LEN (token->val.node)); |
1149 | buffer += NODE_LEN (token->val.node); | |
f80e83a9 | 1150 | break; |
ab12a39c | 1151 | |
8d27e472 | 1152 | case SPELL_NUMBER: |
1153 | memcpy (buffer, token->val.str.text, token->val.str.len); | |
1154 | buffer += token->val.str.len; | |
1155 | break; | |
1156 | ||
f80e83a9 | 1157 | case SPELL_STRING: |
1158 | { | |
71aa9da4 | 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; | |
71aa9da4 | 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; | |
8d27e472 | 1167 | default: |
73328dce | 1168 | cpp_error (pfile, DL_ICE, "unknown string token %s\n", |
1169 | TOKEN_NAME (token)); | |
8d27e472 | 1170 | return buffer; |
71aa9da4 | 1171 | } |
1172 | if (tag) *buffer++ = tag; | |
8d27e472 | 1173 | *buffer++ = left; |
76faa4c0 | 1174 | memcpy (buffer, token->val.str.text, token->val.str.len); |
1175 | buffer += token->val.str.len; | |
8d27e472 | 1176 | *buffer++ = right; |
f80e83a9 | 1177 | } |
1178 | break; | |
ab12a39c | 1179 | |
f80e83a9 | 1180 | case SPELL_NONE: |
73328dce | 1181 | cpp_error (pfile, DL_ICE, "unspellable token %s", TOKEN_NAME (token)); |
f80e83a9 | 1182 | break; |
1183 | } | |
ab12a39c | 1184 | |
f80e83a9 | 1185 | return buffer; |
1186 | } | |
ab12a39c | 1187 | |
e484a1cc | 1188 | /* Returns TOKEN spelt as a null-terminated string. The string is |
1189 | freed when the reader is destroyed. Useful for diagnostics. */ | |
79bd622b | 1190 | unsigned char * |
1191 | cpp_token_as_text (pfile, token) | |
6060326b | 1192 | cpp_reader *pfile; |
f80e83a9 | 1193 | const cpp_token *token; |
6060326b | 1194 | { |
79bd622b | 1195 | unsigned int len = cpp_token_len (token); |
1fdf6039 | 1196 | unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end; |
6060326b | 1197 | |
79bd622b | 1198 | end = cpp_spell_token (pfile, token, start); |
1199 | end[0] = '\0'; | |
6060326b | 1200 | |
79bd622b | 1201 | return start; |
1202 | } | |
6060326b | 1203 | |
e484a1cc | 1204 | /* Used by C front ends, which really should move to using |
1205 | cpp_token_as_text. */ | |
79bd622b | 1206 | const char * |
1207 | cpp_type2name (type) | |
1208 | enum cpp_ttype type; | |
1209 | { | |
1210 | return (const char *) token_spellings[type].name; | |
1211 | } | |
6060326b | 1212 | |
f9b5f742 | 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. */ | |
79bd622b | 1216 | void |
1217 | cpp_output_token (token, fp) | |
1218 | const cpp_token *token; | |
1219 | FILE *fp; | |
1220 | { | |
79bd622b | 1221 | switch (TOKEN_SPELL (token)) |
6060326b | 1222 | { |
79bd622b | 1223 | case SPELL_OPERATOR: |
1224 | { | |
1225 | const unsigned char *spelling; | |
28874558 | 1226 | int c; |
6060326b | 1227 | |
79bd622b | 1228 | if (token->flags & DIGRAPH) |
ee6c4e4b | 1229 | spelling |
1230 | = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH]; | |
79bd622b | 1231 | else if (token->flags & NAMED_OP) |
1232 | goto spell_ident; | |
1233 | else | |
1234 | spelling = TOKEN_NAME (token); | |
f80e83a9 | 1235 | |
28874558 | 1236 | c = *spelling; |
1237 | do | |
1238 | putc (c, fp); | |
1239 | while ((c = *++spelling) != '\0'); | |
79bd622b | 1240 | } |
1241 | break; | |
f80e83a9 | 1242 | |
8d27e472 | 1243 | case SPELL_CHAR: |
1244 | putc (token->val.c, fp); | |
1245 | break; | |
1246 | ||
79bd622b | 1247 | spell_ident: |
1248 | case SPELL_IDENT: | |
28874558 | 1249 | fwrite (NODE_NAME (token->val.node), 1, NODE_LEN (token->val.node), fp); |
79bd622b | 1250 | break; |
f80e83a9 | 1251 | |
8d27e472 | 1252 | case SPELL_NUMBER: |
1253 | fwrite (token->val.str.text, 1, token->val.str.len, fp); | |
1254 | break; | |
1255 | ||
79bd622b | 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; | |
79bd622b | 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; | |
8d27e472 | 1266 | default: |
1267 | fprintf (stderr, "impossible STRING token %s\n", TOKEN_NAME (token)); | |
1268 | return; | |
79bd622b | 1269 | } |
1270 | if (tag) putc (tag, fp); | |
8d27e472 | 1271 | putc (left, fp); |
79bd622b | 1272 | fwrite (token->val.str.text, 1, token->val.str.len, fp); |
8d27e472 | 1273 | putc (right, fp); |
79bd622b | 1274 | } |
1275 | break; | |
6060326b | 1276 | |
79bd622b | 1277 | case SPELL_NONE: |
1278 | /* An error, most probably. */ | |
1279 | break; | |
f80e83a9 | 1280 | } |
6060326b | 1281 | } |
1282 | ||
79bd622b | 1283 | /* Compare two tokens. */ |
1284 | int | |
1285 | _cpp_equiv_tokens (a, b) | |
1286 | const cpp_token *a, *b; | |
6060326b | 1287 | { |
79bd622b | 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: | |
33344a1c | 1295 | return a->val.c == b->val.c; /* Character. */ |
79bd622b | 1296 | case SPELL_NONE: |
588d632b | 1297 | return (a->type != CPP_MACRO_ARG || a->val.arg_no == b->val.arg_no); |
79bd622b | 1298 | case SPELL_IDENT: |
1299 | return a->val.node == b->val.node; | |
8d27e472 | 1300 | case SPELL_NUMBER: |
79bd622b | 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 | } | |
6060326b | 1306 | |
f80e83a9 | 1307 | return 0; |
1308 | } | |
1309 | ||
79bd622b | 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". */ | |
79bd622b | 1314 | int |
1315 | cpp_avoid_paste (pfile, token1, token2) | |
6060326b | 1316 | cpp_reader *pfile; |
79bd622b | 1317 | const cpp_token *token1, *token2; |
6060326b | 1318 | { |
79bd622b | 1319 | enum cpp_ttype a = token1->type, b = token2->type; |
1320 | cppchar_t c; | |
6060326b | 1321 | |
79bd622b | 1322 | if (token1->flags & NAMED_OP) |
1323 | a = CPP_NAME; | |
1324 | if (token2->flags & NAMED_OP) | |
1325 | b = CPP_NAME; | |
6060326b | 1326 | |
79bd622b | 1327 | c = EOF; |
1328 | if (token2->flags & DIGRAPH) | |
ee6c4e4b | 1329 | c = digraph_spellings[(int) b - (int) CPP_FIRST_DIGRAPH][0]; |
79bd622b | 1330 | else if (token_spellings[b].category == SPELL_OPERATOR) |
1331 | c = token_spellings[b].name[0]; | |
6060326b | 1332 | |
79bd622b | 1333 | /* Quickly get everything that can paste with an '='. */ |
ee6c4e4b | 1334 | if ((int) a <= (int) CPP_LAST_EQ && c == '=') |
79bd622b | 1335 | return 1; |
6060326b | 1336 | |
79bd622b | 1337 | switch (a) |
6060326b | 1338 | { |
79bd622b | 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 == '*'; | |
efdcc728 | 1349 | case CPP_DOT: return c == '.' || c == '%' || b == CPP_NUMBER; |
79bd622b | 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 == '-'); | |
2cbf1359 | 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))); | |
79bd622b | 1362 | default: break; |
6060326b | 1363 | } |
6060326b | 1364 | |
deb356cf | 1365 | return 0; |
6060326b | 1366 | } |
1367 | ||
79bd622b | 1368 | /* Output all the remaining tokens on the current line, and a newline |
f9b5f742 | 1369 | character, to FP. Leading whitespace is removed. If there are |
1370 | macros, special token padding is not performed. */ | |
6060326b | 1371 | void |
79bd622b | 1372 | cpp_output_line (pfile, fp) |
6060326b | 1373 | cpp_reader *pfile; |
79bd622b | 1374 | FILE *fp; |
6060326b | 1375 | { |
f9b5f742 | 1376 | const cpp_token *token; |
7e842f95 | 1377 | |
f9b5f742 | 1378 | token = cpp_get_token (pfile); |
1379 | while (token->type != CPP_EOF) | |
7e842f95 | 1380 | { |
f9b5f742 | 1381 | cpp_output_token (token, fp); |
1382 | token = cpp_get_token (pfile); | |
1383 | if (token->flags & PREV_WHITE) | |
1384 | putc (' ', fp); | |
7e842f95 | 1385 | } |
1386 | ||
79bd622b | 1387 | putc ('\n', fp); |
f80e83a9 | 1388 | } |
6060326b | 1389 | |
8330799c | 1390 | /* Returns the value of a hexadecimal digit. */ |
1391 | static unsigned int | |
1392 | hex_digit_value (c) | |
1393 | unsigned int c; | |
1394 | { | |
768169bd | 1395 | if (hex_p (c)) |
1396 | return hex_value (c); | |
1397 | else | |
1398 | abort (); | |
8330799c | 1399 | } |
1400 | ||
2cbf1359 | 1401 | /* Read a possible universal character name starting at *PSTR. */ |
1402 | static cppchar_t | |
1403 | maybe_read_ucn (pfile, pstr) | |
8330799c | 1404 | cpp_reader *pfile; |
2cbf1359 | 1405 | const uchar **pstr; |
8330799c | 1406 | { |
2cbf1359 | 1407 | cppchar_t result, c = (*pstr)[-1]; |
c8342759 | 1408 | |
2cbf1359 | 1409 | result = _cpp_valid_ucn (pfile, pstr, false); |
1410 | if (result) | |
f73bab03 | 1411 | { |
2cbf1359 | 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)) | |
8330799c | 1418 | { |
2cbf1359 | 1419 | cpp_error (pfile, DL_ERROR, |
1420 | "universal character with an EBCDIC target"); | |
1421 | result = 0x3f; /* EBCDIC invalid character */ | |
8330799c | 1422 | } |
8330799c | 1423 | } |
1424 | ||
2cbf1359 | 1425 | return result; |
8330799c | 1426 | } |
1427 | ||
13c457e1 | 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) | |
8330799c | 1435 | cpp_reader *pfile; |
1436 | const unsigned char **pstr; | |
1437 | const unsigned char *limit; | |
13c457e1 | 1438 | int wide; |
8330799c | 1439 | { |
65a6d98c | 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 | ||
8330799c | 1444 | int unknown = 0; |
65a6d98c | 1445 | const unsigned char *str = *pstr, *charconsts; |
2cbf1359 | 1446 | cppchar_t c, ucn, mask; |
13c457e1 | 1447 | unsigned int width; |
1448 | ||
65a6d98c | 1449 | if (CPP_OPTION (pfile, EBCDIC)) |
1450 | charconsts = ebcdic; | |
1451 | else | |
1452 | charconsts = ascii; | |
1453 | ||
13c457e1 | 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; | |
8330799c | 1462 | |
13c457e1 | 1463 | c = *str++; |
8330799c | 1464 | switch (c) |
1465 | { | |
1466 | case '\\': case '\'': case '"': case '?': break; | |
65a6d98c | 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; | |
8330799c | 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)) | |
73328dce | 1482 | cpp_error (pfile, DL_WARNING, |
1483 | "the meaning of '\\a' is different in traditional C"); | |
65a6d98c | 1484 | c = charconsts[0]; |
8330799c | 1485 | break; |
1486 | ||
1487 | case 'e': case 'E': | |
1488 | if (CPP_PEDANTIC (pfile)) | |
73328dce | 1489 | cpp_error (pfile, DL_PEDWARN, |
40855b30 | 1490 | "non-ISO-standard escape sequence, '\\%c'", (int) c); |
65a6d98c | 1491 | c = charconsts[2]; |
8330799c | 1492 | break; |
b1a9ff83 | 1493 | |
8330799c | 1494 | case 'u': case 'U': |
2cbf1359 | 1495 | ucn = maybe_read_ucn (pfile, &str); |
1496 | if (ucn) | |
1497 | c = ucn; | |
1498 | else | |
1499 | unknown = true; | |
8330799c | 1500 | break; |
1501 | ||
1502 | case 'x': | |
1503 | if (CPP_WTRADITIONAL (pfile)) | |
73328dce | 1504 | cpp_error (pfile, DL_WARNING, |
1505 | "the meaning of '\\x' is different in traditional C"); | |
8330799c | 1506 | |
b1a9ff83 | 1507 | { |
1508 | cppchar_t i = 0, overflow = 0; | |
1509 | int digits_found = 0; | |
8330799c | 1510 | |
b1a9ff83 | 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 | } | |
8330799c | 1521 | |
b1a9ff83 | 1522 | if (!digits_found) |
1523 | cpp_error (pfile, DL_ERROR, | |
73328dce | 1524 | "\\x used with no following hex digits"); |
8330799c | 1525 | |
b1a9ff83 | 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 | } | |
8330799c | 1534 | break; |
1535 | ||
1536 | case '0': case '1': case '2': case '3': | |
1537 | case '4': case '5': case '6': case '7': | |
1538 | { | |
13c457e1 | 1539 | size_t count = 0; |
1540 | cppchar_t i = c - '0'; | |
8330799c | 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 | { | |
73328dce | 1553 | cpp_error (pfile, DL_PEDWARN, |
1554 | "octal escape sequence out of range"); | |
8330799c | 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)) | |
40855b30 | 1569 | cpp_error (pfile, DL_PEDWARN, |
1570 | "unknown escape sequence '\\%c'", (int) c); | |
8330799c | 1571 | else |
40855b30 | 1572 | cpp_error (pfile, DL_PEDWARN, |
1573 | "unknown escape sequence: '\\%03o'", (int) c); | |
8330799c | 1574 | } |
1575 | ||
c8342759 | 1576 | if (c > mask) |
13c457e1 | 1577 | { |
d507cd2f | 1578 | cpp_error (pfile, DL_PEDWARN, "escape sequence out of range for its type"); |
13c457e1 | 1579 | c &= mask; |
1580 | } | |
c8342759 | 1581 | |
8330799c | 1582 | *pstr = str; |
1583 | return c; | |
1584 | } | |
1585 | ||
8330799c | 1586 | /* Interpret a (possibly wide) character constant in TOKEN. |
13c457e1 | 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 | |
318fdd81 | 1592 | cpp_interpret_charconst (pfile, token, pchars_seen, unsignedp) |
8330799c | 1593 | cpp_reader *pfile; |
1594 | const cpp_token *token; | |
8330799c | 1595 | unsigned int *pchars_seen; |
13c457e1 | 1596 | int *unsignedp; |
8330799c | 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; | |
d507cd2f | 1601 | size_t width, max_chars; |
13c457e1 | 1602 | cppchar_t c, mask, result = 0; |
b3a9482f | 1603 | bool unsigned_p; |
8330799c | 1604 | |
8330799c | 1605 | /* Width in bits. */ |
1606 | if (token->type == CPP_CHAR) | |
b3a9482f | 1607 | { |
13c457e1 | 1608 | width = CPP_OPTION (pfile, char_precision); |
1893c11a | 1609 | max_chars = CPP_OPTION (pfile, int_precision) / width; |
d932f956 | 1610 | unsigned_p = CPP_OPTION (pfile, unsigned_char); |
b3a9482f | 1611 | } |
8330799c | 1612 | else |
b3a9482f | 1613 | { |
13c457e1 | 1614 | width = CPP_OPTION (pfile, wchar_precision); |
1893c11a | 1615 | max_chars = 1; |
d932f956 | 1616 | unsigned_p = CPP_OPTION (pfile, unsigned_wchar); |
b3a9482f | 1617 | } |
8330799c | 1618 | |
13c457e1 | 1619 | if (width < BITS_PER_CPPCHAR_T) |
1620 | mask = ((cppchar_t) 1 << width) - 1; | |
8330799c | 1621 | else |
1622 | mask = ~0; | |
8330799c | 1623 | |
1624 | while (str < limit) | |
1625 | { | |
8330799c | 1626 | c = *str++; |
8330799c | 1627 | |
1628 | if (c == '\\') | |
13c457e1 | 1629 | c = cpp_parse_escape (pfile, &str, limit, token->type == CPP_WCHAR); |
8330799c | 1630 | |
1631 | #ifdef MAP_CHARACTER | |
1632 | if (ISPRINT (c)) | |
1633 | c = MAP_CHARACTER (c); | |
1634 | #endif | |
b1a9ff83 | 1635 | |
d507cd2f | 1636 | chars_seen++; |
1637 | ||
318fdd81 | 1638 | /* Truncate the character, scale the result and merge the two. */ |
1639 | c &= mask; | |
d507cd2f | 1640 | if (width < BITS_PER_CPPCHAR_T) |
318fdd81 | 1641 | result = (result << width) | c; |
d507cd2f | 1642 | else |
1643 | result = c; | |
8330799c | 1644 | } |
1645 | ||
1646 | if (chars_seen == 0) | |
73328dce | 1647 | cpp_error (pfile, DL_ERROR, "empty character constant"); |
d507cd2f | 1648 | else if (chars_seen > 1) |
8330799c | 1649 | { |
d507cd2f | 1650 | /* Multichar charconsts are of type int and therefore signed. */ |
1651 | unsigned_p = 0; | |
318fdd81 | 1652 | |
d507cd2f | 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 | } | |
318fdd81 | 1659 | else if (CPP_OPTION (pfile, warn_multichar)) |
d507cd2f | 1660 | cpp_error (pfile, DL_WARNING, "multi-character character constant"); |
8330799c | 1661 | } |
1662 | ||
2798f07c | 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) | |
318fdd81 | 1669 | { |
2798f07c | 1670 | mask = ((cppchar_t) 1 << width) - 1; |
1671 | if (unsigned_p || !(result & (1 << (width - 1)))) | |
1672 | result &= mask; | |
1673 | else | |
1674 | result |= ~mask; | |
318fdd81 | 1675 | } |
1676 | ||
8330799c | 1677 | *pchars_seen = chars_seen; |
13c457e1 | 1678 | *unsignedp = unsigned_p; |
8330799c | 1679 | return result; |
1680 | } | |
1681 | ||
084163dc | 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 | |
1e0ef2fd | 1689 | #define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2) |
084163dc | 1690 | #define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \ |
1691 | (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2) | |
deb356cf | 1692 | |
1e0ef2fd | 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 | ||
1785b647 | 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. */ | |
06c92cbc | 1699 | static _cpp_buff * |
1700 | new_buff (len) | |
4b31a107 | 1701 | size_t len; |
06c92cbc | 1702 | { |
1703 | _cpp_buff *result; | |
1fdf6039 | 1704 | unsigned char *base; |
06c92cbc | 1705 | |
084163dc | 1706 | if (len < MIN_BUFF_SIZE) |
1707 | len = MIN_BUFF_SIZE; | |
198b48a0 | 1708 | len = CPP_ALIGN (len); |
06c92cbc | 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; | |
4b31a107 | 1737 | size_t min_size; |
06c92cbc | 1738 | { |
1739 | _cpp_buff *result, **p; | |
1740 | ||
1741 | for (p = &pfile->free_buffs;; p = &(*p)->next) | |
1742 | { | |
4b31a107 | 1743 | size_t size; |
084163dc | 1744 | |
1745 | if (*p == NULL) | |
06c92cbc | 1746 | return new_buff (min_size); |
084163dc | 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. */ | |
4085c149 | 1751 | if (size >= min_size && size <= BUFF_SIZE_UPPER_BOUND (min_size)) |
06c92cbc | 1752 | break; |
1753 | } | |
1754 | ||
1755 | *p = result->next; | |
1756 | result->next = NULL; | |
1757 | result->cur = result->base; | |
1758 | return result; | |
1759 | } | |
1760 | ||
20dd417a | 1761 | /* Creates a new buffer with enough space to hold the uncommitted |
e6a5f963 | 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. */ | |
06c92cbc | 1765 | _cpp_buff * |
e6a5f963 | 1766 | _cpp_append_extend_buff (pfile, buff, min_extra) |
06c92cbc | 1767 | cpp_reader *pfile; |
1768 | _cpp_buff *buff; | |
4b31a107 | 1769 | size_t min_extra; |
06c92cbc | 1770 | { |
4b31a107 | 1771 | size_t size = EXTENDED_BUFF_SIZE (buff, min_extra); |
e6a5f963 | 1772 | _cpp_buff *new_buff = _cpp_get_buff (pfile, size); |
06c92cbc | 1773 | |
e6a5f963 | 1774 | buff->next = new_buff; |
1775 | memcpy (new_buff->base, buff->cur, BUFF_ROOM (buff)); | |
1776 | return new_buff; | |
1777 | } | |
1778 | ||
20dd417a | 1779 | /* Creates a new buffer with enough space to hold the uncommitted |
e6a5f963 | 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; | |
06c92cbc | 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 | } | |
deb356cf | 1812 | |
1fdf6039 | 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 | ||
1e0ef2fd | 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. */ | |
79bd622b | 1844 | unsigned char * |
e6a5f963 | 1845 | _cpp_aligned_alloc (pfile, len) |
1846 | cpp_reader *pfile; | |
1847 | size_t len; | |
89b05ef6 | 1848 | { |
e6a5f963 | 1849 | _cpp_buff *buff = pfile->a_buff; |
1850 | unsigned char *result = buff->cur; | |
89b05ef6 | 1851 | |
e6a5f963 | 1852 | if (len > (size_t) (buff->limit - result)) |
89b05ef6 | 1853 | { |
e6a5f963 | 1854 | buff = _cpp_get_buff (pfile, len); |
1855 | buff->next = pfile->a_buff; | |
1856 | pfile->a_buff = buff; | |
1857 | result = buff->cur; | |
89b05ef6 | 1858 | } |
f80e83a9 | 1859 | |
e6a5f963 | 1860 | buff->cur = result + len; |
79bd622b | 1861 | return result; |
f80e83a9 | 1862 | } |