1 /* CPP Library - lexical analysis.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
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
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
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.
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. */
24 #include "coretypes.h"
29 /* Tokens with SPELL_STRING store their spelling in the token list,
30 and it's length in the token->val.name.len. */
43 enum spell_type category;
44 const unsigned char *name;
47 static const unsigned char *const digraph_spellings[] =
48 { U"%:", U"%:%:", U"<:", U":>", U"<%", U"%>" };
50 #define OP(e, s) { SPELL_OPERATOR, U s },
51 #define TK(e, s) { s, U STRINGX (e) },
52 static const struct token_spelling token_spellings[N_TTYPES] = { TTYPE_TABLE };
56 #define TOKEN_SPELL(token) (token_spellings[(token)->type].category)
57 #define TOKEN_NAME(token) (token_spellings[(token)->type].name)
58 #define BACKUP() do {buffer->cur = buffer->backup_to;} while (0)
60 static void add_line_note PARAMS ((cpp_buffer *, const uchar *, unsigned int));
61 static cppchar_t get_effective_char PARAMS ((cpp_reader *));
63 static int skip_line_comment PARAMS ((cpp_reader *));
64 static void skip_whitespace PARAMS ((cpp_reader *, cppchar_t));
65 static cpp_hashnode *parse_identifier PARAMS ((cpp_reader *));
66 static uchar *parse_slow PARAMS ((cpp_reader *, const uchar *, int,
68 static void parse_number PARAMS ((cpp_reader *, cpp_string *, int));
69 static int unescaped_terminator_p PARAMS ((cpp_reader *, const uchar *));
70 static void parse_string PARAMS ((cpp_reader *, cpp_token *, cppchar_t));
71 static void save_comment PARAMS ((cpp_reader *, cpp_token *, const uchar *,
73 static int name_p PARAMS ((cpp_reader *, const cpp_string *));
74 static int maybe_read_ucs PARAMS ((cpp_reader *, const unsigned char **,
75 const unsigned char *, cppchar_t *));
76 static tokenrun *next_tokenrun PARAMS ((tokenrun *));
78 static unsigned int hex_digit_value PARAMS ((unsigned int));
79 static _cpp_buff *new_buff PARAMS ((size_t));
84 Compares, the token TOKEN to the NUL-terminated string STRING.
85 TOKEN must be a CPP_NAME. Returns 1 for equal, 0 for unequal. */
87 cpp_ideq (token, string)
88 const cpp_token *token;
91 if (token->type != CPP_NAME)
94 return !ustrcmp (NODE_NAME (token->val.node), (const uchar *) string);
97 /* Record a note TYPE at byte POS into the current cleaned logical
100 add_line_note (buffer, pos, type)
105 if (buffer->notes_used == buffer->notes_cap)
107 buffer->notes_cap = buffer->notes_cap * 2 + 200;
108 buffer->notes = (_cpp_line_note *)
109 xrealloc (buffer->notes, buffer->notes_cap * sizeof (_cpp_line_note));
112 buffer->notes[buffer->notes_used].pos = pos;
113 buffer->notes[buffer->notes_used].type = type;
114 buffer->notes_used++;
117 /* Returns with a logical line that contains no escaped newlines or
118 trigraphs. This is a time-critical inner loop. */
120 _cpp_clean_line (pfile)
127 buffer = pfile->buffer;
128 buffer->cur_note = buffer->notes_used = 0;
129 buffer->cur = buffer->line_base = buffer->next_line;
130 buffer->need_line = false;
131 s = buffer->next_line - 1;
133 if (!buffer->from_stage3)
142 if (c == '\n' || c == '\r')
144 /* Handle DOS line endings. */
145 if (c == '\r' && s != buffer->rlimit && s[1] == '\n')
147 if (s == buffer->rlimit)
152 while (p != buffer->next_line && is_nvspace (p[-1]))
154 if (p == buffer->next_line || p[-1] != '\\')
157 add_line_note (buffer, p - 1,
158 p != d ? NOTE_ESC_SPACE_NL: NOTE_ESC_NL);
160 buffer->next_line = p - 1;
162 else if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
164 /* Add a note regardless, for the benefit of -Wtrigraphs. */
165 add_line_note (buffer, d, NOTE_TRIGRAPH);
166 if (CPP_OPTION (pfile, trigraphs))
168 *d = _cpp_trigraph_map[s[2]];
178 while (*s != '\n' && *s != '\r');
181 /* Handle DOS line endings. */
182 if (*s == '\r' && s != buffer->rlimit && s[1] == '\n')
187 add_line_note (buffer, d + 1, NOTE_NEWLINE);
188 buffer->next_line = s + 1;
191 /* Process the notes created by add_line_note as far as the current
194 _cpp_process_line_notes (pfile, in_comment)
198 cpp_buffer *buffer = pfile->buffer;
202 _cpp_line_note *note = &buffer->notes[buffer->cur_note];
205 if (note->pos > buffer->cur)
209 col = CPP_BUF_COLUMN (buffer, note->pos + 1);
214 /* This note is a kind of sentinel we should never reach. */
218 if (!in_comment && CPP_OPTION (pfile, warn_trigraphs))
220 if (CPP_OPTION (pfile, trigraphs))
221 cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
222 "trigraph converted to %c",
225 cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
226 "trigraph ??%c ignored",
231 case NOTE_ESC_SPACE_NL:
233 cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
234 "backslash and newline separated by space");
235 /* Fall through... */
237 if (buffer->next_line > buffer->rlimit)
239 cpp_error_with_line (pfile, DL_PEDWARN, pfile->line, col,
240 "backslash-newline at end of file");
241 /* Prevent "no newline at end of file" warning. */
242 buffer->next_line = buffer->rlimit;
245 buffer->line_base = note->pos;
251 /* Obtain the next character, after trigraph conversion and skipping
252 an arbitrarily long string of escaped newlines. The common case of
253 no trigraphs or escaped newlines falls through quickly. On return,
254 buffer->backup_to points to where to return to if the character is
255 not to be processed. */
257 get_effective_char (pfile)
260 cpp_buffer *buffer = pfile->buffer;
262 buffer->backup_to = buffer->cur;
263 return *buffer->cur++;
266 /* Skip a C-style block comment. We find the end of the comment by
267 seeing if an asterisk is before every '/' we encounter. Returns
268 nonzero if comment terminated by EOF, zero otherwise. */
270 _cpp_skip_block_comment (pfile)
273 cpp_buffer *buffer = pfile->buffer;
276 if (*buffer->cur == '/')
283 /* People like decorating comments with '*', so check for '/'
284 instead for efficiency. */
287 if (buffer->cur[-2] == '*')
290 /* Warn about potential nested comments, but not if the '/'
291 comes immediately before the true comment delimiter.
292 Don't bother to get it right across escaped newlines. */
293 if (CPP_OPTION (pfile, warn_comments)
294 && buffer->cur[0] == '*' && buffer->cur[1] != '/')
295 cpp_error_with_line (pfile, DL_WARNING,
296 pfile->line, CPP_BUF_COL (buffer),
297 "\"/*\" within comment");
302 _cpp_process_line_notes (pfile, true);
303 if (buffer->next_line >= buffer->rlimit)
305 _cpp_clean_line (pfile);
313 /* Skip a C++ line comment, leaving buffer->cur pointing to the
314 terminating newline. Handles escaped newlines. Returns nonzero
315 if a multiline comment. */
317 skip_line_comment (pfile)
320 cpp_buffer *buffer = pfile->buffer;
321 unsigned int orig_line = pfile->line;
323 while (*buffer->cur != '\n')
326 _cpp_process_line_notes (pfile, true);
327 return orig_line != pfile->line;
330 /* Skips whitespace, saving the next non-whitespace character. */
332 skip_whitespace (pfile, c)
336 cpp_buffer *buffer = pfile->buffer;
337 unsigned int warned = 0;
341 /* Horizontal space always OK. */
342 if (c == ' ' || c == '\t')
344 /* Just \f \v or \0 left. */
349 cpp_error (pfile, DL_WARNING, "null character(s) ignored");
353 else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
354 cpp_error_with_line (pfile, DL_PEDWARN, pfile->line,
355 CPP_BUF_COL (buffer),
356 "%s in preprocessing directive",
357 c == '\f' ? "form feed" : "vertical tab");
361 /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
362 while (is_nvspace (c));
367 /* See if the characters of a number token are valid in a name (no
370 name_p (pfile, string)
372 const cpp_string *string;
376 for (i = 0; i < string->len; i++)
377 if (!is_idchar (string->text[i]))
383 /* Parse an identifier, skipping embedded backslash-newlines. This is
384 a critical inner loop. The common case is an identifier which has
385 not been split by backslash-newline, does not contain a dollar
386 sign, and has already been scanned (roughly 10:1 ratio of
387 seen:unseen identifiers in normal code; the distribution is
388 Poisson-like). Second most common case is a new identifier, not
389 split and no dollar sign. The other possibilities are rare and
390 have been relegated to parse_slow. */
391 static cpp_hashnode *
392 parse_identifier (pfile)
395 cpp_hashnode *result;
396 const uchar *cur, *base;
398 /* Fast-path loop. Skim over a normal identifier.
399 N.B. ISIDNUM does not include $. */
400 cur = pfile->buffer->cur;
401 while (ISIDNUM (*cur))
404 /* Check for slow-path cases. */
409 base = parse_slow (pfile, cur, 0, &len);
410 result = (cpp_hashnode *)
411 ht_lookup (pfile->hash_table, base, len, HT_ALLOCED);
415 base = pfile->buffer->cur - 1;
416 pfile->buffer->cur = cur;
417 result = (cpp_hashnode *)
418 ht_lookup (pfile->hash_table, base, cur - base, HT_ALLOC);
421 /* Rarely, identifiers require diagnostics when lexed.
422 XXX Has to be forced out of the fast path. */
423 if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
424 && !pfile->state.skipping, 0))
426 /* It is allowed to poison the same identifier twice. */
427 if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
428 cpp_error (pfile, DL_ERROR, "attempt to use poisoned \"%s\"",
431 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
432 replacement list of a variadic macro. */
433 if (result == pfile->spec_nodes.n__VA_ARGS__
434 && !pfile->state.va_args_ok)
435 cpp_error (pfile, DL_PEDWARN,
436 "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
442 /* Slow path. This handles numbers and identifiers which have been
443 split, or contain dollar signs. The part of the token from
444 PFILE->buffer->cur-1 to CUR has already been scanned. NUMBER_P is
445 1 if it's a number, and 2 if it has a leading period. Returns a
446 pointer to the token's NUL-terminated spelling in permanent
447 storage, and sets PLEN to its length. */
449 parse_slow (pfile, cur, number_p, plen)
455 cpp_buffer *buffer = pfile->buffer;
456 const uchar *base = buffer->cur - 1;
457 struct obstack *stack = &pfile->hash_table->stack;
458 unsigned int c, prevc, saw_dollar = 0;
460 /* Place any leading period. */
462 obstack_1grow (stack, '.');
464 /* Copy the part of the token which is known to be okay. */
465 obstack_grow (stack, base, cur - base);
467 /* Now process the part which isn't. We are looking at one of
468 '$', '\\', or '?' on entry to this loop. */
474 /* Potential escaped newline? */
475 buffer->backup_to = buffer->cur - 1;
481 if (c != '.' && !VALID_SIGN (c, prevc))
485 /* Handle normal identifier characters in this loop. */
489 obstack_1grow (stack, c);
496 while (is_idchar (c));
499 /* Step back over the unwanted char. */
502 /* $ is not an identifier character in the standard, but is commonly
503 accepted as an extension. Don't warn about it in skipped
504 conditional blocks. */
505 if (saw_dollar && CPP_PEDANTIC (pfile) && ! pfile->state.skipping)
506 cpp_error (pfile, DL_PEDWARN, "'$' character(s) in identifier or number");
508 /* Identifiers and numbers are null-terminated. */
509 *plen = obstack_object_size (stack);
510 obstack_1grow (stack, '\0');
511 return obstack_finish (stack);
514 /* Parse a number, beginning with character C, skipping embedded
515 backslash-newlines. LEADING_PERIOD is nonzero if there was a "."
516 before C. Place the result in NUMBER. */
518 parse_number (pfile, number, leading_period)
525 /* Fast-path loop. Skim over a normal number.
526 N.B. ISIDNUM does not include $. */
527 cur = pfile->buffer->cur;
528 while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1]))
531 /* Check for slow-path cases. */
533 number->text = parse_slow (pfile, cur, 1 + leading_period, &number->len);
536 const uchar *base = pfile->buffer->cur - 1;
539 number->len = cur - base + leading_period;
540 dest = _cpp_unaligned_alloc (pfile, number->len + 1);
541 dest[number->len] = '\0';
546 memcpy (dest, base, cur - base);
547 pfile->buffer->cur = cur;
551 /* Subroutine of parse_string. */
553 unescaped_terminator_p (pfile, dest)
555 const unsigned char *dest;
557 const unsigned char *start, *temp;
559 /* In #include-style directives, terminators are not escapable. */
560 if (pfile->state.angled_headers)
563 start = BUFF_FRONT (pfile->u_buff);
565 /* An odd number of consecutive backslashes represents an escaped
567 for (temp = dest; temp > start && temp[-1] == '\\'; temp--)
570 return ((dest - temp) & 1) == 0;
573 /* Parses a string, character constant, or angle-bracketed header file
574 name. Handles embedded trigraphs and escaped newlines. The stored
575 string is guaranteed NUL-terminated, but it is not guaranteed that
576 this is the first NUL since embedded NULs are preserved.
578 When this function returns, buffer->cur points to the next
579 character to be processed. */
581 parse_string (pfile, token, terminator)
584 cppchar_t terminator;
586 cpp_buffer *buffer = pfile->buffer;
587 unsigned char *dest, *limit;
589 bool warned_nulls = false;
591 dest = BUFF_FRONT (pfile->u_buff);
592 limit = BUFF_LIMIT (pfile->u_buff);
596 /* We need room for another char, possibly the terminating NUL. */
597 if ((size_t) (limit - dest) < 1)
599 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
600 _cpp_extend_buff (pfile, &pfile->u_buff, 2);
601 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
602 limit = BUFF_LIMIT (pfile->u_buff);
609 if (unescaped_terminator_p (pfile, dest))
614 /* No string literal may extend over multiple lines. In
615 assembly language, suppress the error except for <>
616 includes. This is a kludge around not knowing where
618 if (CPP_OPTION (pfile, lang) != CLK_ASM || terminator == '>')
619 cpp_error (pfile, DL_ERROR, "missing terminating %c character",
629 cpp_error (pfile, DL_WARNING,
630 "null character(s) preserved in literal");
638 token->val.str.text = BUFF_FRONT (pfile->u_buff);
639 token->val.str.len = dest - BUFF_FRONT (pfile->u_buff);
640 BUFF_FRONT (pfile->u_buff) = dest + 1;
643 /* The stored comment includes the comment start and any terminator. */
645 save_comment (pfile, token, from, type)
648 const unsigned char *from;
651 unsigned char *buffer;
652 unsigned int len, clen;
654 len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */
656 /* C++ comments probably (not definitely) have moved past a new
657 line, which we don't want to save in the comment. */
658 if (is_vspace (pfile->buffer->cur[-1]))
661 /* If we are currently in a directive, then we need to store all
662 C++ comments as C comments internally, and so we need to
663 allocate a little extra space in that case.
665 Note that the only time we encounter a directive here is
666 when we are saving comments in a "#define". */
667 clen = (pfile->state.in_directive && type == '/') ? len + 2 : len;
669 buffer = _cpp_unaligned_alloc (pfile, clen);
671 token->type = CPP_COMMENT;
672 token->val.str.len = clen;
673 token->val.str.text = buffer;
676 memcpy (buffer + 1, from, len - 1);
678 /* Finish conversion to a C comment, if necessary. */
679 if (pfile->state.in_directive && type == '/')
682 buffer[clen - 2] = '*';
683 buffer[clen - 1] = '/';
687 /* Allocate COUNT tokens for RUN. */
689 _cpp_init_tokenrun (run, count)
693 run->base = xnewvec (cpp_token, count);
694 run->limit = run->base + count;
698 /* Returns the next tokenrun, or creates one if there is none. */
703 if (run->next == NULL)
705 run->next = xnew (tokenrun);
706 run->next->prev = run;
707 _cpp_init_tokenrun (run->next, 250);
713 /* Allocate a single token that is invalidated at the same time as the
714 rest of the tokens on the line. Has its line and col set to the
715 same as the last lexed token, so that diagnostics appear in the
718 _cpp_temp_token (pfile)
721 cpp_token *old, *result;
723 old = pfile->cur_token - 1;
724 if (pfile->cur_token == pfile->cur_run->limit)
726 pfile->cur_run = next_tokenrun (pfile->cur_run);
727 pfile->cur_token = pfile->cur_run->base;
730 result = pfile->cur_token++;
731 result->line = old->line;
732 result->col = old->col;
736 /* Lex a token into RESULT (external interface). Takes care of issues
737 like directive handling, token lookahead, multiple include
738 optimization and skipping. */
740 _cpp_lex_token (pfile)
747 if (pfile->cur_token == pfile->cur_run->limit)
749 pfile->cur_run = next_tokenrun (pfile->cur_run);
750 pfile->cur_token = pfile->cur_run->base;
753 if (pfile->lookaheads)
756 result = pfile->cur_token++;
759 result = _cpp_lex_direct (pfile);
761 if (result->flags & BOL)
763 /* Is this a directive. If _cpp_handle_directive returns
764 false, it is an assembler #. */
765 if (result->type == CPP_HASH
766 /* 6.10.3 p 11: Directives in a list of macro arguments
767 gives undefined behavior. This implementation
768 handles the directive as normal. */
769 && pfile->state.parsing_args != 1
770 && _cpp_handle_directive (pfile, result->flags & PREV_WHITE))
772 if (pfile->cb.line_change && !pfile->state.skipping)
773 (*pfile->cb.line_change)(pfile, result, pfile->state.parsing_args);
776 /* We don't skip tokens in directives. */
777 if (pfile->state.in_directive)
780 /* Outside a directive, invalidate controlling macros. At file
781 EOF, _cpp_lex_direct takes care of popping the buffer, so we never
782 get here and MI optimisation works. */
783 pfile->mi_valid = false;
785 if (!pfile->state.skipping || result->type == CPP_EOF)
792 /* Returns true if a fresh line has been loaded. */
794 _cpp_get_fresh_line (pfile)
797 /* We can't get a new line until we leave the current directive. */
798 if (pfile->state.in_directive)
803 cpp_buffer *buffer = pfile->buffer;
805 if (!buffer->need_line)
808 if (buffer->next_line < buffer->rlimit)
810 _cpp_clean_line (pfile);
814 /* First, get out of parsing arguments state. */
815 if (pfile->state.parsing_args)
818 /* End of buffer. Non-empty files should end in a newline. */
819 if (buffer->buf != buffer->rlimit
820 && buffer->next_line > buffer->rlimit
821 && !buffer->from_stage3)
823 /* Only warn once. */
824 buffer->next_line = buffer->rlimit;
825 cpp_error_with_line (pfile, DL_PEDWARN, pfile->line - 1,
826 CPP_BUF_COLUMN (buffer, buffer->cur),
827 "no newline at end of file");
830 if (buffer->return_at_eof)
832 buffer->return_at_eof = false;
839 _cpp_pop_buffer (pfile);
843 #define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
845 if (get_effective_char (pfile) == CHAR) \
846 result->type = THEN_TYPE; \
850 result->type = ELSE_TYPE; \
854 /* Lex a token into pfile->cur_token, which is also incremented, to
855 get diagnostics pointing to the correct location.
857 Does not handle issues such as token lookahead, multiple-include
858 optimisation, directives, skipping etc. This function is only
859 suitable for use by _cpp_lex_token, and in special cases like
860 lex_expansion_token which doesn't care for any of these issues.
862 When meeting a newline, returns CPP_EOF if parsing a directive,
863 otherwise returns to the start of the token buffer if permissible.
864 Returns the location of the lexed token. */
866 _cpp_lex_direct (pfile)
871 const unsigned char *comment_start;
872 cpp_token *result = pfile->cur_token++;
876 if (pfile->buffer->need_line)
878 if (!_cpp_get_fresh_line (pfile))
880 result->type = CPP_EOF;
883 if (!pfile->keep_tokens)
885 pfile->cur_run = &pfile->base_run;
886 result = pfile->base_run.base;
887 pfile->cur_token = result + 1;
890 if (pfile->state.parsing_args == 2)
891 result->flags |= PREV_WHITE;
893 buffer = pfile->buffer;
895 result->line = pfile->line;
898 if (buffer->cur >= buffer->notes[buffer->cur_note].pos
899 && !pfile->overlaid_buffer)
901 _cpp_process_line_notes (pfile, false);
902 result->line = pfile->line;
905 result->col = CPP_BUF_COLUMN (buffer, buffer->cur);
909 case ' ': case '\t': case '\f': case '\v': case '\0':
910 result->flags |= PREV_WHITE;
911 skip_whitespace (pfile, c);
916 buffer->need_line = true;
919 case '0': case '1': case '2': case '3': case '4':
920 case '5': case '6': case '7': case '8': case '9':
921 result->type = CPP_NUMBER;
922 parse_number (pfile, &result->val.str, 0);
926 /* 'L' may introduce wide characters or strings. */
928 const unsigned char *pos = buffer->cur;
930 c = get_effective_char (pfile);
931 if (c == '\'' || c == '"')
933 result->type = (c == '"' ? CPP_WSTRING: CPP_WCHAR);
934 parse_string (pfile, result, c);
943 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
944 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
945 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
946 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
948 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
949 case 'G': case 'H': case 'I': case 'J': case 'K':
950 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
951 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
953 result->type = CPP_NAME;
954 result->val.node = parse_identifier (pfile);
956 /* Convert named operators to their proper types. */
957 if (result->val.node->flags & NODE_OPERATOR)
959 result->flags |= NAMED_OP;
960 result->type = result->val.node->directive_index;
966 result->type = c == '"' ? CPP_STRING: CPP_CHAR;
967 parse_string (pfile, result, c);
971 /* A potential block or line comment. */
972 comment_start = buffer->cur;
973 c = get_effective_char (pfile);
977 if (_cpp_skip_block_comment (pfile))
978 cpp_error (pfile, DL_ERROR, "unterminated comment");
980 else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments)
981 || CPP_IN_SYSTEM_HEADER (pfile)))
983 /* Warn about comments only if pedantically GNUC89, and not
984 in system headers. */
985 if (CPP_OPTION (pfile, lang) == CLK_GNUC89 && CPP_PEDANTIC (pfile)
986 && ! buffer->warned_cplusplus_comments)
988 cpp_error (pfile, DL_PEDWARN,
989 "C++ style comments are not allowed in ISO C90");
990 cpp_error (pfile, DL_PEDWARN,
991 "(this will be reported only once per input file)");
992 buffer->warned_cplusplus_comments = 1;
995 if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments))
996 cpp_error (pfile, DL_WARNING, "multi-line comment");
1000 result->type = CPP_DIV_EQ;
1006 result->type = CPP_DIV;
1010 if (!pfile->state.save_comments)
1012 result->flags |= PREV_WHITE;
1013 goto update_tokens_line;
1016 /* Save the comment as a token in its own right. */
1017 save_comment (pfile, result, comment_start, c);
1021 if (pfile->state.angled_headers)
1023 result->type = CPP_HEADER_NAME;
1024 parse_string (pfile, result, '>');
1028 c = get_effective_char (pfile);
1030 result->type = CPP_LESS_EQ;
1032 IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT);
1033 else if (c == '?' && CPP_OPTION (pfile, cplusplus))
1034 IF_NEXT_IS ('=', CPP_MIN_EQ, CPP_MIN);
1035 else if (c == ':' && CPP_OPTION (pfile, digraphs))
1037 result->type = CPP_OPEN_SQUARE;
1038 result->flags |= DIGRAPH;
1040 else if (c == '%' && CPP_OPTION (pfile, digraphs))
1042 result->type = CPP_OPEN_BRACE;
1043 result->flags |= DIGRAPH;
1048 result->type = CPP_LESS;
1053 c = get_effective_char (pfile);
1055 result->type = CPP_GREATER_EQ;
1057 IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT);
1058 else if (c == '?' && CPP_OPTION (pfile, cplusplus))
1059 IF_NEXT_IS ('=', CPP_MAX_EQ, CPP_MAX);
1063 result->type = CPP_GREATER;
1068 c = get_effective_char (pfile);
1070 result->type = CPP_MOD_EQ;
1071 else if (CPP_OPTION (pfile, digraphs) && c == ':')
1073 result->flags |= DIGRAPH;
1074 result->type = CPP_HASH;
1075 if (get_effective_char (pfile) == '%')
1077 const unsigned char *pos = buffer->cur;
1079 if (get_effective_char (pfile) == ':')
1080 result->type = CPP_PASTE;
1082 buffer->cur = pos - 1;
1087 else if (CPP_OPTION (pfile, digraphs) && c == '>')
1089 result->flags |= DIGRAPH;
1090 result->type = CPP_CLOSE_BRACE;
1095 result->type = CPP_MOD;
1100 result->type = CPP_DOT;
1101 c = get_effective_char (pfile);
1104 const unsigned char *pos = buffer->cur;
1106 if (get_effective_char (pfile) == '.')
1107 result->type = CPP_ELLIPSIS;
1109 buffer->cur = pos - 1;
1111 /* All known character sets have 0...9 contiguous. */
1112 else if (ISDIGIT (c))
1114 result->type = CPP_NUMBER;
1115 parse_number (pfile, &result->val.str, 1);
1117 else if (c == '*' && CPP_OPTION (pfile, cplusplus))
1118 result->type = CPP_DOT_STAR;
1124 c = get_effective_char (pfile);
1126 result->type = CPP_PLUS_PLUS;
1128 result->type = CPP_PLUS_EQ;
1132 result->type = CPP_PLUS;
1137 c = get_effective_char (pfile);
1140 result->type = CPP_DEREF;
1141 if (CPP_OPTION (pfile, cplusplus))
1143 if (get_effective_char (pfile) == '*')
1144 result->type = CPP_DEREF_STAR;
1150 result->type = CPP_MINUS_MINUS;
1152 result->type = CPP_MINUS_EQ;
1156 result->type = CPP_MINUS;
1161 c = get_effective_char (pfile);
1163 result->type = CPP_AND_AND;
1165 result->type = CPP_AND_EQ;
1169 result->type = CPP_AND;
1174 c = get_effective_char (pfile);
1176 result->type = CPP_OR_OR;
1178 result->type = CPP_OR_EQ;
1182 result->type = CPP_OR;
1187 c = get_effective_char (pfile);
1188 if (c == ':' && CPP_OPTION (pfile, cplusplus))
1189 result->type = CPP_SCOPE;
1190 else if (c == '>' && CPP_OPTION (pfile, digraphs))
1192 result->flags |= DIGRAPH;
1193 result->type = CPP_CLOSE_SQUARE;
1198 result->type = CPP_COLON;
1202 case '*': IF_NEXT_IS ('=', CPP_MULT_EQ, CPP_MULT); break;
1203 case '=': IF_NEXT_IS ('=', CPP_EQ_EQ, CPP_EQ); break;
1204 case '!': IF_NEXT_IS ('=', CPP_NOT_EQ, CPP_NOT); break;
1205 case '^': IF_NEXT_IS ('=', CPP_XOR_EQ, CPP_XOR); break;
1206 case '#': IF_NEXT_IS ('#', CPP_PASTE, CPP_HASH); break;
1208 case '?': result->type = CPP_QUERY; break;
1209 case '~': result->type = CPP_COMPL; break;
1210 case ',': result->type = CPP_COMMA; break;
1211 case '(': result->type = CPP_OPEN_PAREN; break;
1212 case ')': result->type = CPP_CLOSE_PAREN; break;
1213 case '[': result->type = CPP_OPEN_SQUARE; break;
1214 case ']': result->type = CPP_CLOSE_SQUARE; break;
1215 case '{': result->type = CPP_OPEN_BRACE; break;
1216 case '}': result->type = CPP_CLOSE_BRACE; break;
1217 case ';': result->type = CPP_SEMICOLON; break;
1219 /* @ is a punctuator in Objective-C. */
1220 case '@': result->type = CPP_ATSIGN; break;
1223 if (CPP_OPTION (pfile, dollars_in_ident))
1225 /* Fall through... */
1228 result->type = CPP_OTHER;
1236 /* An upper bound on the number of bytes needed to spell TOKEN,
1237 including preceding whitespace. */
1239 cpp_token_len (token)
1240 const cpp_token *token;
1244 switch (TOKEN_SPELL (token))
1246 default: len = 0; break;
1248 case SPELL_STRING: len = token->val.str.len; break;
1249 case SPELL_IDENT: len = NODE_LEN (token->val.node); break;
1251 /* 1 for whitespace, 4 for comment delimiters. */
1255 /* Write the spelling of a token TOKEN to BUFFER. The buffer must
1256 already contain the enough space to hold the token's spelling.
1257 Returns a pointer to the character after the last character
1260 cpp_spell_token (pfile, token, buffer)
1261 cpp_reader *pfile; /* Would be nice to be rid of this... */
1262 const cpp_token *token;
1263 unsigned char *buffer;
1265 switch (TOKEN_SPELL (token))
1267 case SPELL_OPERATOR:
1269 const unsigned char *spelling;
1272 if (token->flags & DIGRAPH)
1274 = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH];
1275 else if (token->flags & NAMED_OP)
1278 spelling = TOKEN_NAME (token);
1280 while ((c = *spelling++) != '\0')
1286 *buffer++ = token->val.c;
1291 memcpy (buffer, NODE_NAME (token->val.node), NODE_LEN (token->val.node));
1292 buffer += NODE_LEN (token->val.node);
1296 memcpy (buffer, token->val.str.text, token->val.str.len);
1297 buffer += token->val.str.len;
1302 int left, right, tag;
1303 switch (token->type)
1305 case CPP_STRING: left = '"'; right = '"'; tag = '\0'; break;
1306 case CPP_WSTRING: left = '"'; right = '"'; tag = 'L'; break;
1307 case CPP_CHAR: left = '\''; right = '\''; tag = '\0'; break;
1308 case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
1309 case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
1311 cpp_error (pfile, DL_ICE, "unknown string token %s\n",
1312 TOKEN_NAME (token));
1315 if (tag) *buffer++ = tag;
1317 memcpy (buffer, token->val.str.text, token->val.str.len);
1318 buffer += token->val.str.len;
1324 cpp_error (pfile, DL_ICE, "unspellable token %s", TOKEN_NAME (token));
1331 /* Returns TOKEN spelt as a null-terminated string. The string is
1332 freed when the reader is destroyed. Useful for diagnostics. */
1334 cpp_token_as_text (pfile, token)
1336 const cpp_token *token;
1338 unsigned int len = cpp_token_len (token);
1339 unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end;
1341 end = cpp_spell_token (pfile, token, start);
1347 /* Used by C front ends, which really should move to using
1348 cpp_token_as_text. */
1350 cpp_type2name (type)
1351 enum cpp_ttype type;
1353 return (const char *) token_spellings[type].name;
1356 /* Writes the spelling of token to FP, without any preceding space.
1357 Separated from cpp_spell_token for efficiency - to avoid stdio
1358 double-buffering. */
1360 cpp_output_token (token, fp)
1361 const cpp_token *token;
1364 switch (TOKEN_SPELL (token))
1366 case SPELL_OPERATOR:
1368 const unsigned char *spelling;
1371 if (token->flags & DIGRAPH)
1373 = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH];
1374 else if (token->flags & NAMED_OP)
1377 spelling = TOKEN_NAME (token);
1382 while ((c = *++spelling) != '\0');
1387 putc (token->val.c, fp);
1392 fwrite (NODE_NAME (token->val.node), 1, NODE_LEN (token->val.node), fp);
1396 fwrite (token->val.str.text, 1, token->val.str.len, fp);
1401 int left, right, tag;
1402 switch (token->type)
1404 case CPP_STRING: left = '"'; right = '"'; tag = '\0'; break;
1405 case CPP_WSTRING: left = '"'; right = '"'; tag = 'L'; break;
1406 case CPP_CHAR: left = '\''; right = '\''; tag = '\0'; break;
1407 case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
1408 case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
1410 fprintf (stderr, "impossible STRING token %s\n", TOKEN_NAME (token));
1413 if (tag) putc (tag, fp);
1415 fwrite (token->val.str.text, 1, token->val.str.len, fp);
1421 /* An error, most probably. */
1426 /* Compare two tokens. */
1428 _cpp_equiv_tokens (a, b)
1429 const cpp_token *a, *b;
1431 if (a->type == b->type && a->flags == b->flags)
1432 switch (TOKEN_SPELL (a))
1434 default: /* Keep compiler happy. */
1435 case SPELL_OPERATOR:
1438 return a->val.c == b->val.c; /* Character. */
1440 return (a->type != CPP_MACRO_ARG || a->val.arg_no == b->val.arg_no);
1442 return a->val.node == b->val.node;
1445 return (a->val.str.len == b->val.str.len
1446 && !memcmp (a->val.str.text, b->val.str.text,
1453 /* Returns nonzero if a space should be inserted to avoid an
1454 accidental token paste for output. For simplicity, it is
1455 conservative, and occasionally advises a space where one is not
1456 needed, e.g. "." and ".2". */
1458 cpp_avoid_paste (pfile, token1, token2)
1460 const cpp_token *token1, *token2;
1462 enum cpp_ttype a = token1->type, b = token2->type;
1465 if (token1->flags & NAMED_OP)
1467 if (token2->flags & NAMED_OP)
1471 if (token2->flags & DIGRAPH)
1472 c = digraph_spellings[(int) b - (int) CPP_FIRST_DIGRAPH][0];
1473 else if (token_spellings[b].category == SPELL_OPERATOR)
1474 c = token_spellings[b].name[0];
1476 /* Quickly get everything that can paste with an '='. */
1477 if ((int) a <= (int) CPP_LAST_EQ && c == '=')
1482 case CPP_GREATER: return c == '>' || c == '?';
1483 case CPP_LESS: return c == '<' || c == '?' || c == '%' || c == ':';
1484 case CPP_PLUS: return c == '+';
1485 case CPP_MINUS: return c == '-' || c == '>';
1486 case CPP_DIV: return c == '/' || c == '*'; /* Comments. */
1487 case CPP_MOD: return c == ':' || c == '>';
1488 case CPP_AND: return c == '&';
1489 case CPP_OR: return c == '|';
1490 case CPP_COLON: return c == ':' || c == '>';
1491 case CPP_DEREF: return c == '*';
1492 case CPP_DOT: return c == '.' || c == '%' || b == CPP_NUMBER;
1493 case CPP_HASH: return c == '#' || c == '%'; /* Digraph form. */
1494 case CPP_NAME: return ((b == CPP_NUMBER
1495 && name_p (pfile, &token2->val.str))
1497 || b == CPP_CHAR || b == CPP_STRING); /* L */
1498 case CPP_NUMBER: return (b == CPP_NUMBER || b == CPP_NAME
1499 || c == '.' || c == '+' || c == '-');
1500 case CPP_OTHER: return (CPP_OPTION (pfile, objc)
1501 && token1->val.c == '@'
1502 && (b == CPP_NAME || b == CPP_STRING));
1509 /* Output all the remaining tokens on the current line, and a newline
1510 character, to FP. Leading whitespace is removed. If there are
1511 macros, special token padding is not performed. */
1513 cpp_output_line (pfile, fp)
1517 const cpp_token *token;
1519 token = cpp_get_token (pfile);
1520 while (token->type != CPP_EOF)
1522 cpp_output_token (token, fp);
1523 token = cpp_get_token (pfile);
1524 if (token->flags & PREV_WHITE)
1531 /* Returns the value of a hexadecimal digit. */
1537 return hex_value (c);
1542 /* Parse a '\uNNNN' or '\UNNNNNNNN' sequence. Returns 1 to indicate
1543 failure if cpplib is not parsing C++ or C99. Such failure is
1544 silent, and no variables are updated. Otherwise returns 0, and
1545 warns if -Wtraditional.
1547 [lex.charset]: The character designated by the universal character
1548 name \UNNNNNNNN is that character whose character short name in
1549 ISO/IEC 10646 is NNNNNNNN; the character designated by the
1550 universal character name \uNNNN is that character whose character
1551 short name in ISO/IEC 10646 is 0000NNNN. If the hexadecimal value
1552 for a universal character name is less than 0x20 or in the range
1553 0x7F-0x9F (inclusive), or if the universal character name
1554 designates a character in the basic source character set, then the
1555 program is ill-formed.
1557 We assume that wchar_t is Unicode, so we don't need to do any
1558 mapping. Is this ever wrong?
1560 PC points to the 'u' or 'U', PSTR is points to the byte after PC,
1561 LIMIT is the end of the string or charconst. PSTR is updated to
1562 point after the UCS on return, and the UCS is written into PC. */
1565 maybe_read_ucs (pfile, pstr, limit, pc)
1567 const unsigned char **pstr;
1568 const unsigned char *limit;
1571 const unsigned char *p = *pstr;
1572 unsigned int code = 0;
1573 unsigned int c = *pc, length;
1575 /* Only attempt to interpret a UCS for C++ and C99. */
1576 if (! (CPP_OPTION (pfile, cplusplus) || CPP_OPTION (pfile, c99)))
1579 if (CPP_WTRADITIONAL (pfile))
1580 cpp_error (pfile, DL_WARNING,
1581 "the meaning of '\\%c' is different in traditional C", c);
1583 length = (c == 'u' ? 4: 8);
1585 if ((size_t) (limit - p) < length)
1587 cpp_error (pfile, DL_ERROR, "incomplete universal-character-name");
1588 /* Skip to the end to avoid more diagnostics. */
1593 for (; length; length--, p++)
1597 code = (code << 4) + hex_digit_value (c);
1600 cpp_error (pfile, DL_ERROR,
1601 "non-hex digit '%c' in universal-character-name", c);
1602 /* We shouldn't skip in case there are multibyte chars. */
1608 if (CPP_OPTION (pfile, EBCDIC))
1610 cpp_error (pfile, DL_ERROR, "universal-character-name on EBCDIC target");
1611 code = 0x3f; /* EBCDIC invalid character */
1613 /* True extended characters are OK. */
1614 else if (code >= 0xa0
1615 && !(code & 0x80000000)
1616 && !(code >= 0xD800 && code <= 0xDFFF))
1618 /* The standard permits $, @ and ` to be specified as UCNs. We use
1619 hex escapes so that this also works with EBCDIC hosts. */
1620 else if (code == 0x24 || code == 0x40 || code == 0x60)
1622 /* Don't give another error if one occurred above. */
1623 else if (length == 0)
1624 cpp_error (pfile, DL_ERROR, "universal-character-name out of range");
1631 /* Returns the value of an escape sequence, truncated to the correct
1632 target precision. PSTR points to the input pointer, which is just
1633 after the backslash. LIMIT is how much text we have. WIDE is true
1634 if the escape sequence is part of a wide character constant or
1635 string literal. Handles all relevant diagnostics. */
1637 cpp_parse_escape (pfile, pstr, limit, wide)
1639 const unsigned char **pstr;
1640 const unsigned char *limit;
1643 /* Values of \a \b \e \f \n \r \t \v respectively. */
1644 static const uchar ascii[] = { 7, 8, 27, 12, 10, 13, 9, 11 };
1645 static const uchar ebcdic[] = { 47, 22, 39, 12, 21, 13, 5, 11 };
1648 const unsigned char *str = *pstr, *charconsts;
1652 if (CPP_OPTION (pfile, EBCDIC))
1653 charconsts = ebcdic;
1658 width = CPP_OPTION (pfile, wchar_precision);
1660 width = CPP_OPTION (pfile, char_precision);
1661 if (width < BITS_PER_CPPCHAR_T)
1662 mask = ((cppchar_t) 1 << width) - 1;
1669 case '\\': case '\'': case '"': case '?': break;
1670 case 'b': c = charconsts[1]; break;
1671 case 'f': c = charconsts[3]; break;
1672 case 'n': c = charconsts[4]; break;
1673 case 'r': c = charconsts[5]; break;
1674 case 't': c = charconsts[6]; break;
1675 case 'v': c = charconsts[7]; break;
1677 case '(': case '{': case '[': case '%':
1678 /* '\(', etc, are used at beginning of line to avoid confusing Emacs.
1679 '\%' is used to prevent SCCS from getting confused. */
1680 unknown = CPP_PEDANTIC (pfile);
1684 if (CPP_WTRADITIONAL (pfile))
1685 cpp_error (pfile, DL_WARNING,
1686 "the meaning of '\\a' is different in traditional C");
1691 if (CPP_PEDANTIC (pfile))
1692 cpp_error (pfile, DL_PEDWARN,
1693 "non-ISO-standard escape sequence, '\\%c'", (int) c);
1698 unknown = maybe_read_ucs (pfile, &str, limit, &c);
1702 if (CPP_WTRADITIONAL (pfile))
1703 cpp_error (pfile, DL_WARNING,
1704 "the meaning of '\\x' is different in traditional C");
1707 cppchar_t i = 0, overflow = 0;
1708 int digits_found = 0;
1716 overflow |= i ^ (i << 4 >> 4);
1717 i = (i << 4) + hex_digit_value (c);
1722 cpp_error (pfile, DL_ERROR,
1723 "\\x used with no following hex digits");
1725 if (overflow | (i != (i & mask)))
1727 cpp_error (pfile, DL_PEDWARN,
1728 "hex escape sequence out of range");
1735 case '0': case '1': case '2': case '3':
1736 case '4': case '5': case '6': case '7':
1739 cppchar_t i = c - '0';
1741 while (str < limit && ++count < 3)
1744 if (c < '0' || c > '7')
1747 i = (i << 3) + c - '0';
1750 if (i != (i & mask))
1752 cpp_error (pfile, DL_PEDWARN,
1753 "octal escape sequence out of range");
1768 cpp_error (pfile, DL_PEDWARN,
1769 "unknown escape sequence '\\%c'", (int) c);
1771 cpp_error (pfile, DL_PEDWARN,
1772 "unknown escape sequence: '\\%03o'", (int) c);
1777 cpp_error (pfile, DL_PEDWARN, "escape sequence out of range for its type");
1785 /* Interpret a (possibly wide) character constant in TOKEN.
1786 WARN_MULTI warns about multi-character charconsts. PCHARS_SEEN
1787 points to a variable that is filled in with the number of
1788 characters seen, and UNSIGNEDP to a variable that indicates whether
1789 the result has signed type. */
1791 cpp_interpret_charconst (pfile, token, pchars_seen, unsignedp)
1793 const cpp_token *token;
1794 unsigned int *pchars_seen;
1797 const unsigned char *str = token->val.str.text;
1798 const unsigned char *limit = str + token->val.str.len;
1799 unsigned int chars_seen = 0;
1800 size_t width, max_chars;
1801 cppchar_t c, mask, result = 0;
1804 /* Width in bits. */
1805 if (token->type == CPP_CHAR)
1807 width = CPP_OPTION (pfile, char_precision);
1808 max_chars = CPP_OPTION (pfile, int_precision) / width;
1809 unsigned_p = CPP_OPTION (pfile, unsigned_char);
1813 width = CPP_OPTION (pfile, wchar_precision);
1815 unsigned_p = CPP_OPTION (pfile, unsigned_wchar);
1818 if (width < BITS_PER_CPPCHAR_T)
1819 mask = ((cppchar_t) 1 << width) - 1;
1828 c = cpp_parse_escape (pfile, &str, limit, token->type == CPP_WCHAR);
1830 #ifdef MAP_CHARACTER
1832 c = MAP_CHARACTER (c);
1837 /* Truncate the character, scale the result and merge the two. */
1839 if (width < BITS_PER_CPPCHAR_T)
1840 result = (result << width) | c;
1845 if (chars_seen == 0)
1846 cpp_error (pfile, DL_ERROR, "empty character constant");
1847 else if (chars_seen > 1)
1849 /* Multichar charconsts are of type int and therefore signed. */
1852 if (chars_seen > max_chars)
1854 chars_seen = max_chars;
1855 cpp_error (pfile, DL_WARNING,
1856 "character constant too long for its type");
1858 else if (CPP_OPTION (pfile, warn_multichar))
1859 cpp_error (pfile, DL_WARNING, "multi-character character constant");
1862 /* Sign-extend or truncate the constant to cppchar_t. The value is
1863 in WIDTH bits, but for multi-char charconsts it's value is the
1864 full target type's width. */
1867 if (width < BITS_PER_CPPCHAR_T)
1869 mask = ((cppchar_t) 1 << width) - 1;
1870 if (unsigned_p || !(result & (1 << (width - 1))))
1876 *pchars_seen = chars_seen;
1877 *unsignedp = unsigned_p;
1881 /* Memory buffers. Changing these three constants can have a dramatic
1882 effect on performance. The values here are reasonable defaults,
1883 but might be tuned. If you adjust them, be sure to test across a
1884 range of uses of cpplib, including heavy nested function-like macro
1885 expansion. Also check the change in peak memory usage (NJAMD is a
1886 good tool for this). */
1887 #define MIN_BUFF_SIZE 8000
1888 #define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2)
1889 #define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \
1890 (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2)
1892 #if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0)
1893 #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
1896 /* Create a new allocation buffer. Place the control block at the end
1897 of the buffer, so that buffer overflows will cause immediate chaos. */
1903 unsigned char *base;
1905 if (len < MIN_BUFF_SIZE)
1906 len = MIN_BUFF_SIZE;
1907 len = CPP_ALIGN (len);
1909 base = xmalloc (len + sizeof (_cpp_buff));
1910 result = (_cpp_buff *) (base + len);
1911 result->base = base;
1913 result->limit = base + len;
1914 result->next = NULL;
1918 /* Place a chain of unwanted allocation buffers on the free list. */
1920 _cpp_release_buff (pfile, buff)
1924 _cpp_buff *end = buff;
1928 end->next = pfile->free_buffs;
1929 pfile->free_buffs = buff;
1932 /* Return a free buffer of size at least MIN_SIZE. */
1934 _cpp_get_buff (pfile, min_size)
1938 _cpp_buff *result, **p;
1940 for (p = &pfile->free_buffs;; p = &(*p)->next)
1945 return new_buff (min_size);
1947 size = result->limit - result->base;
1948 /* Return a buffer that's big enough, but don't waste one that's
1950 if (size >= min_size && size <= BUFF_SIZE_UPPER_BOUND (min_size))
1955 result->next = NULL;
1956 result->cur = result->base;
1960 /* Creates a new buffer with enough space to hold the uncommitted
1961 remaining bytes of BUFF, and at least MIN_EXTRA more bytes. Copies
1962 the excess bytes to the new buffer. Chains the new buffer after
1963 BUFF, and returns the new buffer. */
1965 _cpp_append_extend_buff (pfile, buff, min_extra)
1970 size_t size = EXTENDED_BUFF_SIZE (buff, min_extra);
1971 _cpp_buff *new_buff = _cpp_get_buff (pfile, size);
1973 buff->next = new_buff;
1974 memcpy (new_buff->base, buff->cur, BUFF_ROOM (buff));
1978 /* Creates a new buffer with enough space to hold the uncommitted
1979 remaining bytes of the buffer pointed to by BUFF, and at least
1980 MIN_EXTRA more bytes. Copies the excess bytes to the new buffer.
1981 Chains the new buffer before the buffer pointed to by BUFF, and
1982 updates the pointer to point to the new buffer. */
1984 _cpp_extend_buff (pfile, pbuff, min_extra)
1989 _cpp_buff *new_buff, *old_buff = *pbuff;
1990 size_t size = EXTENDED_BUFF_SIZE (old_buff, min_extra);
1992 new_buff = _cpp_get_buff (pfile, size);
1993 memcpy (new_buff->base, old_buff->cur, BUFF_ROOM (old_buff));
1994 new_buff->next = old_buff;
1998 /* Free a chain of buffers starting at BUFF. */
2000 _cpp_free_buff (buff)
2005 for (; buff; buff = next)
2012 /* Allocate permanent, unaligned storage of length LEN. */
2014 _cpp_unaligned_alloc (pfile, len)
2018 _cpp_buff *buff = pfile->u_buff;
2019 unsigned char *result = buff->cur;
2021 if (len > (size_t) (buff->limit - result))
2023 buff = _cpp_get_buff (pfile, len);
2024 buff->next = pfile->u_buff;
2025 pfile->u_buff = buff;
2029 buff->cur = result + len;
2033 /* Allocate permanent, unaligned storage of length LEN from a_buff.
2034 That buffer is used for growing allocations when saving macro
2035 replacement lists in a #define, and when parsing an answer to an
2036 assertion in #assert, #unassert or #if (and therefore possibly
2037 whilst expanding macros). It therefore must not be used by any
2038 code that they might call: specifically the lexer and the guts of
2041 All existing other uses clearly fit this restriction: storing
2042 registered pragmas during initialization. */
2044 _cpp_aligned_alloc (pfile, len)
2048 _cpp_buff *buff = pfile->a_buff;
2049 unsigned char *result = buff->cur;
2051 if (len > (size_t) (buff->limit - result))
2053 buff = _cpp_get_buff (pfile, len);
2054 buff->next = pfile->a_buff;
2055 pfile->a_buff = buff;
2059 buff->cur = result + len;