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