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 bool saw_NUL = false;
341 /* Horizontal space always OK. */
342 if (c == ' ' || c == '\t')
344 /* Just \f \v or \0 left. */
347 else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
348 cpp_error_with_line (pfile, DL_PEDWARN, pfile->line,
349 CPP_BUF_COL (buffer),
350 "%s in preprocessing directive",
351 c == '\f' ? "form feed" : "vertical tab");
355 /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
356 while (is_nvspace (c));
359 cpp_error (pfile, DL_WARNING, "null character(s) ignored");
364 /* See if the characters of a number token are valid in a name (no
367 name_p (pfile, string)
369 const cpp_string *string;
373 for (i = 0; i < string->len; i++)
374 if (!is_idchar (string->text[i]))
380 /* Parse an identifier, skipping embedded backslash-newlines. This is
381 a critical inner loop. The common case is an identifier which has
382 not been split by backslash-newline, does not contain a dollar
383 sign, and has already been scanned (roughly 10:1 ratio of
384 seen:unseen identifiers in normal code; the distribution is
385 Poisson-like). Second most common case is a new identifier, not
386 split and no dollar sign. The other possibilities are rare and
387 have been relegated to parse_slow. */
388 static cpp_hashnode *
389 parse_identifier (pfile)
392 cpp_hashnode *result;
393 const uchar *cur, *base;
395 /* Fast-path loop. Skim over a normal identifier.
396 N.B. ISIDNUM does not include $. */
397 cur = pfile->buffer->cur;
398 while (ISIDNUM (*cur))
401 /* Check for slow-path cases. */
406 base = parse_slow (pfile, cur, 0, &len);
407 result = (cpp_hashnode *)
408 ht_lookup (pfile->hash_table, base, len, HT_ALLOCED);
412 base = pfile->buffer->cur - 1;
413 pfile->buffer->cur = cur;
414 result = (cpp_hashnode *)
415 ht_lookup (pfile->hash_table, base, cur - base, HT_ALLOC);
418 /* Rarely, identifiers require diagnostics when lexed.
419 XXX Has to be forced out of the fast path. */
420 if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
421 && !pfile->state.skipping, 0))
423 /* It is allowed to poison the same identifier twice. */
424 if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
425 cpp_error (pfile, DL_ERROR, "attempt to use poisoned \"%s\"",
428 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
429 replacement list of a variadic macro. */
430 if (result == pfile->spec_nodes.n__VA_ARGS__
431 && !pfile->state.va_args_ok)
432 cpp_error (pfile, DL_PEDWARN,
433 "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
439 /* Slow path. This handles numbers and identifiers which have been
440 split, or contain dollar signs. The part of the token from
441 PFILE->buffer->cur-1 to CUR has already been scanned. NUMBER_P is
442 1 if it's a number, and 2 if it has a leading period. Returns a
443 pointer to the token's NUL-terminated spelling in permanent
444 storage, and sets PLEN to its length. */
446 parse_slow (pfile, cur, number_p, plen)
452 cpp_buffer *buffer = pfile->buffer;
453 const uchar *base = buffer->cur - 1;
454 struct obstack *stack = &pfile->hash_table->stack;
455 unsigned int c, prevc, saw_dollar = 0;
457 /* Place any leading period. */
459 obstack_1grow (stack, '.');
461 /* Copy the part of the token which is known to be okay. */
462 obstack_grow (stack, base, cur - base);
464 /* Now process the part which isn't. We are looking at one of
465 '$', '\\', or '?' on entry to this loop. */
471 /* Potential escaped newline? */
472 buffer->backup_to = buffer->cur - 1;
478 if (c != '.' && !VALID_SIGN (c, prevc))
482 /* Handle normal identifier characters in this loop. */
486 obstack_1grow (stack, c);
493 while (is_idchar (c));
496 /* Step back over the unwanted char. */
499 /* $ is not an identifier character in the standard, but is commonly
500 accepted as an extension. Don't warn about it in skipped
501 conditional blocks. */
502 if (saw_dollar && CPP_PEDANTIC (pfile) && ! pfile->state.skipping)
503 cpp_error (pfile, DL_PEDWARN, "'$' character(s) in identifier or number");
505 /* Identifiers and numbers are null-terminated. */
506 *plen = obstack_object_size (stack);
507 obstack_1grow (stack, '\0');
508 return obstack_finish (stack);
511 /* Parse a number, beginning with character C, skipping embedded
512 backslash-newlines. LEADING_PERIOD is nonzero if there was a "."
513 before C. Place the result in NUMBER. */
515 parse_number (pfile, number, leading_period)
522 /* Fast-path loop. Skim over a normal number.
523 N.B. ISIDNUM does not include $. */
524 cur = pfile->buffer->cur;
525 while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1]))
528 /* Check for slow-path cases. */
530 number->text = parse_slow (pfile, cur, 1 + leading_period, &number->len);
533 const uchar *base = pfile->buffer->cur - 1;
536 number->len = cur - base + leading_period;
537 dest = _cpp_unaligned_alloc (pfile, number->len + 1);
538 dest[number->len] = '\0';
543 memcpy (dest, base, cur - base);
544 pfile->buffer->cur = cur;
548 /* Subroutine of parse_string. */
550 unescaped_terminator_p (pfile, dest)
552 const unsigned char *dest;
554 const unsigned char *start, *temp;
556 /* In #include-style directives, terminators are not escapable. */
557 if (pfile->state.angled_headers)
560 start = BUFF_FRONT (pfile->u_buff);
562 /* An odd number of consecutive backslashes represents an escaped
564 for (temp = dest; temp > start && temp[-1] == '\\'; temp--)
567 return ((dest - temp) & 1) == 0;
570 /* Parses a string, character constant, or angle-bracketed header file
571 name. Handles embedded trigraphs and escaped newlines. The stored
572 string is guaranteed NUL-terminated, but it is not guaranteed that
573 this is the first NUL since embedded NULs are preserved.
575 When this function returns, buffer->cur points to the next
576 character to be processed. */
578 parse_string (pfile, token, terminator)
581 cppchar_t terminator;
583 cpp_buffer *buffer = pfile->buffer;
584 unsigned char *dest, *limit;
586 bool warned_nulls = false;
588 dest = BUFF_FRONT (pfile->u_buff);
589 limit = BUFF_LIMIT (pfile->u_buff);
593 /* We need room for another char, possibly the terminating NUL. */
594 if ((size_t) (limit - dest) < 1)
596 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
597 _cpp_extend_buff (pfile, &pfile->u_buff, 2);
598 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
599 limit = BUFF_LIMIT (pfile->u_buff);
606 if (unescaped_terminator_p (pfile, dest))
611 /* No string literal may extend over multiple lines. In
612 assembly language, suppress the error except for <>
613 includes. This is a kludge around not knowing where
615 if (CPP_OPTION (pfile, lang) != CLK_ASM || terminator == '>')
616 cpp_error (pfile, DL_ERROR, "missing terminating %c character",
626 cpp_error (pfile, DL_WARNING,
627 "null character(s) preserved in literal");
635 token->val.str.text = BUFF_FRONT (pfile->u_buff);
636 token->val.str.len = dest - BUFF_FRONT (pfile->u_buff);
637 BUFF_FRONT (pfile->u_buff) = dest + 1;
640 /* The stored comment includes the comment start and any terminator. */
642 save_comment (pfile, token, from, type)
645 const unsigned char *from;
648 unsigned char *buffer;
649 unsigned int len, clen;
651 len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */
653 /* C++ comments probably (not definitely) have moved past a new
654 line, which we don't want to save in the comment. */
655 if (is_vspace (pfile->buffer->cur[-1]))
658 /* If we are currently in a directive, then we need to store all
659 C++ comments as C comments internally, and so we need to
660 allocate a little extra space in that case.
662 Note that the only time we encounter a directive here is
663 when we are saving comments in a "#define". */
664 clen = (pfile->state.in_directive && type == '/') ? len + 2 : len;
666 buffer = _cpp_unaligned_alloc (pfile, clen);
668 token->type = CPP_COMMENT;
669 token->val.str.len = clen;
670 token->val.str.text = buffer;
673 memcpy (buffer + 1, from, len - 1);
675 /* Finish conversion to a C comment, if necessary. */
676 if (pfile->state.in_directive && type == '/')
679 buffer[clen - 2] = '*';
680 buffer[clen - 1] = '/';
684 /* Allocate COUNT tokens for RUN. */
686 _cpp_init_tokenrun (run, count)
690 run->base = xnewvec (cpp_token, count);
691 run->limit = run->base + count;
695 /* Returns the next tokenrun, or creates one if there is none. */
700 if (run->next == NULL)
702 run->next = xnew (tokenrun);
703 run->next->prev = run;
704 _cpp_init_tokenrun (run->next, 250);
710 /* Allocate a single token that is invalidated at the same time as the
711 rest of the tokens on the line. Has its line and col set to the
712 same as the last lexed token, so that diagnostics appear in the
715 _cpp_temp_token (pfile)
718 cpp_token *old, *result;
720 old = pfile->cur_token - 1;
721 if (pfile->cur_token == pfile->cur_run->limit)
723 pfile->cur_run = next_tokenrun (pfile->cur_run);
724 pfile->cur_token = pfile->cur_run->base;
727 result = pfile->cur_token++;
728 result->line = old->line;
729 result->col = old->col;
733 /* Lex a token into RESULT (external interface). Takes care of issues
734 like directive handling, token lookahead, multiple include
735 optimization and skipping. */
737 _cpp_lex_token (pfile)
744 if (pfile->cur_token == pfile->cur_run->limit)
746 pfile->cur_run = next_tokenrun (pfile->cur_run);
747 pfile->cur_token = pfile->cur_run->base;
750 if (pfile->lookaheads)
753 result = pfile->cur_token++;
756 result = _cpp_lex_direct (pfile);
758 if (result->flags & BOL)
760 /* Is this a directive. If _cpp_handle_directive returns
761 false, it is an assembler #. */
762 if (result->type == CPP_HASH
763 /* 6.10.3 p 11: Directives in a list of macro arguments
764 gives undefined behavior. This implementation
765 handles the directive as normal. */
766 && pfile->state.parsing_args != 1
767 && _cpp_handle_directive (pfile, result->flags & PREV_WHITE))
769 if (pfile->cb.line_change && !pfile->state.skipping)
770 (*pfile->cb.line_change)(pfile, result, pfile->state.parsing_args);
773 /* We don't skip tokens in directives. */
774 if (pfile->state.in_directive)
777 /* Outside a directive, invalidate controlling macros. At file
778 EOF, _cpp_lex_direct takes care of popping the buffer, so we never
779 get here and MI optimisation works. */
780 pfile->mi_valid = false;
782 if (!pfile->state.skipping || result->type == CPP_EOF)
789 /* Returns true if a fresh line has been loaded. */
791 _cpp_get_fresh_line (pfile)
794 /* We can't get a new line until we leave the current directive. */
795 if (pfile->state.in_directive)
800 cpp_buffer *buffer = pfile->buffer;
802 if (!buffer->need_line)
805 if (buffer->next_line < buffer->rlimit)
807 _cpp_clean_line (pfile);
811 /* First, get out of parsing arguments state. */
812 if (pfile->state.parsing_args)
815 /* End of buffer. Non-empty files should end in a newline. */
816 if (buffer->buf != buffer->rlimit
817 && buffer->next_line > buffer->rlimit
818 && !buffer->from_stage3)
820 /* Only warn once. */
821 buffer->next_line = buffer->rlimit;
822 cpp_error_with_line (pfile, DL_PEDWARN, pfile->line - 1,
823 CPP_BUF_COLUMN (buffer, buffer->cur),
824 "no newline at end of file");
827 if (buffer->return_at_eof)
829 buffer->return_at_eof = false;
836 _cpp_pop_buffer (pfile);
840 #define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
842 if (get_effective_char (pfile) == CHAR) \
843 result->type = THEN_TYPE; \
847 result->type = ELSE_TYPE; \
851 /* Lex a token into pfile->cur_token, which is also incremented, to
852 get diagnostics pointing to the correct location.
854 Does not handle issues such as token lookahead, multiple-include
855 optimisation, directives, skipping etc. This function is only
856 suitable for use by _cpp_lex_token, and in special cases like
857 lex_expansion_token which doesn't care for any of these issues.
859 When meeting a newline, returns CPP_EOF if parsing a directive,
860 otherwise returns to the start of the token buffer if permissible.
861 Returns the location of the lexed token. */
863 _cpp_lex_direct (pfile)
868 const unsigned char *comment_start;
869 cpp_token *result = pfile->cur_token++;
873 if (pfile->buffer->need_line)
875 if (!_cpp_get_fresh_line (pfile))
877 result->type = CPP_EOF;
880 if (!pfile->keep_tokens)
882 pfile->cur_run = &pfile->base_run;
883 result = pfile->base_run.base;
884 pfile->cur_token = result + 1;
887 if (pfile->state.parsing_args == 2)
888 result->flags |= PREV_WHITE;
890 buffer = pfile->buffer;
892 result->line = pfile->line;
895 if (buffer->cur >= buffer->notes[buffer->cur_note].pos
896 && !pfile->overlaid_buffer)
898 _cpp_process_line_notes (pfile, false);
899 result->line = pfile->line;
902 result->col = CPP_BUF_COLUMN (buffer, buffer->cur);
906 case ' ': case '\t': case '\f': case '\v': case '\0':
907 result->flags |= PREV_WHITE;
908 skip_whitespace (pfile, c);
913 buffer->need_line = true;
916 case '0': case '1': case '2': case '3': case '4':
917 case '5': case '6': case '7': case '8': case '9':
918 result->type = CPP_NUMBER;
919 parse_number (pfile, &result->val.str, 0);
923 /* 'L' may introduce wide characters or strings. */
925 const unsigned char *pos = buffer->cur;
927 c = get_effective_char (pfile);
928 if (c == '\'' || c == '"')
930 result->type = (c == '"' ? CPP_WSTRING: CPP_WCHAR);
931 parse_string (pfile, result, c);
940 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
941 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
942 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
943 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
945 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
946 case 'G': case 'H': case 'I': case 'J': case 'K':
947 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
948 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
950 result->type = CPP_NAME;
951 result->val.node = parse_identifier (pfile);
953 /* Convert named operators to their proper types. */
954 if (result->val.node->flags & NODE_OPERATOR)
956 result->flags |= NAMED_OP;
957 result->type = result->val.node->directive_index;
963 result->type = c == '"' ? CPP_STRING: CPP_CHAR;
964 parse_string (pfile, result, c);
968 /* A potential block or line comment. */
969 comment_start = buffer->cur;
970 c = get_effective_char (pfile);
974 if (_cpp_skip_block_comment (pfile))
975 cpp_error (pfile, DL_ERROR, "unterminated comment");
977 else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments)
978 || CPP_IN_SYSTEM_HEADER (pfile)))
980 /* Warn about comments only if pedantically GNUC89, and not
981 in system headers. */
982 if (CPP_OPTION (pfile, lang) == CLK_GNUC89 && CPP_PEDANTIC (pfile)
983 && ! buffer->warned_cplusplus_comments)
985 cpp_error (pfile, DL_PEDWARN,
986 "C++ style comments are not allowed in ISO C90");
987 cpp_error (pfile, DL_PEDWARN,
988 "(this will be reported only once per input file)");
989 buffer->warned_cplusplus_comments = 1;
992 if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments))
993 cpp_error (pfile, DL_WARNING, "multi-line comment");
997 result->type = CPP_DIV_EQ;
1003 result->type = CPP_DIV;
1007 if (!pfile->state.save_comments)
1009 result->flags |= PREV_WHITE;
1010 goto update_tokens_line;
1013 /* Save the comment as a token in its own right. */
1014 save_comment (pfile, result, comment_start, c);
1018 if (pfile->state.angled_headers)
1020 result->type = CPP_HEADER_NAME;
1021 parse_string (pfile, result, '>');
1025 c = get_effective_char (pfile);
1027 result->type = CPP_LESS_EQ;
1029 IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT);
1030 else if (c == '?' && CPP_OPTION (pfile, cplusplus))
1031 IF_NEXT_IS ('=', CPP_MIN_EQ, CPP_MIN);
1032 else if (c == ':' && CPP_OPTION (pfile, digraphs))
1034 result->type = CPP_OPEN_SQUARE;
1035 result->flags |= DIGRAPH;
1037 else if (c == '%' && CPP_OPTION (pfile, digraphs))
1039 result->type = CPP_OPEN_BRACE;
1040 result->flags |= DIGRAPH;
1045 result->type = CPP_LESS;
1050 c = get_effective_char (pfile);
1052 result->type = CPP_GREATER_EQ;
1054 IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT);
1055 else if (c == '?' && CPP_OPTION (pfile, cplusplus))
1056 IF_NEXT_IS ('=', CPP_MAX_EQ, CPP_MAX);
1060 result->type = CPP_GREATER;
1065 c = get_effective_char (pfile);
1067 result->type = CPP_MOD_EQ;
1068 else if (CPP_OPTION (pfile, digraphs) && c == ':')
1070 result->flags |= DIGRAPH;
1071 result->type = CPP_HASH;
1072 if (get_effective_char (pfile) == '%')
1074 const unsigned char *pos = buffer->cur;
1076 if (get_effective_char (pfile) == ':')
1077 result->type = CPP_PASTE;
1079 buffer->cur = pos - 1;
1084 else if (CPP_OPTION (pfile, digraphs) && c == '>')
1086 result->flags |= DIGRAPH;
1087 result->type = CPP_CLOSE_BRACE;
1092 result->type = CPP_MOD;
1097 result->type = CPP_DOT;
1098 c = get_effective_char (pfile);
1101 const unsigned char *pos = buffer->cur;
1103 if (get_effective_char (pfile) == '.')
1104 result->type = CPP_ELLIPSIS;
1106 buffer->cur = pos - 1;
1108 /* All known character sets have 0...9 contiguous. */
1109 else if (ISDIGIT (c))
1111 result->type = CPP_NUMBER;
1112 parse_number (pfile, &result->val.str, 1);
1114 else if (c == '*' && CPP_OPTION (pfile, cplusplus))
1115 result->type = CPP_DOT_STAR;
1121 c = get_effective_char (pfile);
1123 result->type = CPP_PLUS_PLUS;
1125 result->type = CPP_PLUS_EQ;
1129 result->type = CPP_PLUS;
1134 c = get_effective_char (pfile);
1137 result->type = CPP_DEREF;
1138 if (CPP_OPTION (pfile, cplusplus))
1140 if (get_effective_char (pfile) == '*')
1141 result->type = CPP_DEREF_STAR;
1147 result->type = CPP_MINUS_MINUS;
1149 result->type = CPP_MINUS_EQ;
1153 result->type = CPP_MINUS;
1158 c = get_effective_char (pfile);
1160 result->type = CPP_AND_AND;
1162 result->type = CPP_AND_EQ;
1166 result->type = CPP_AND;
1171 c = get_effective_char (pfile);
1173 result->type = CPP_OR_OR;
1175 result->type = CPP_OR_EQ;
1179 result->type = CPP_OR;
1184 c = get_effective_char (pfile);
1185 if (c == ':' && CPP_OPTION (pfile, cplusplus))
1186 result->type = CPP_SCOPE;
1187 else if (c == '>' && CPP_OPTION (pfile, digraphs))
1189 result->flags |= DIGRAPH;
1190 result->type = CPP_CLOSE_SQUARE;
1195 result->type = CPP_COLON;
1199 case '*': IF_NEXT_IS ('=', CPP_MULT_EQ, CPP_MULT); break;
1200 case '=': IF_NEXT_IS ('=', CPP_EQ_EQ, CPP_EQ); break;
1201 case '!': IF_NEXT_IS ('=', CPP_NOT_EQ, CPP_NOT); break;
1202 case '^': IF_NEXT_IS ('=', CPP_XOR_EQ, CPP_XOR); break;
1203 case '#': IF_NEXT_IS ('#', CPP_PASTE, CPP_HASH); break;
1205 case '?': result->type = CPP_QUERY; break;
1206 case '~': result->type = CPP_COMPL; break;
1207 case ',': result->type = CPP_COMMA; break;
1208 case '(': result->type = CPP_OPEN_PAREN; break;
1209 case ')': result->type = CPP_CLOSE_PAREN; break;
1210 case '[': result->type = CPP_OPEN_SQUARE; break;
1211 case ']': result->type = CPP_CLOSE_SQUARE; break;
1212 case '{': result->type = CPP_OPEN_BRACE; break;
1213 case '}': result->type = CPP_CLOSE_BRACE; break;
1214 case ';': result->type = CPP_SEMICOLON; break;
1216 /* @ is a punctuator in Objective-C. */
1217 case '@': result->type = CPP_ATSIGN; break;
1220 if (CPP_OPTION (pfile, dollars_in_ident))
1222 /* Fall through... */
1225 result->type = CPP_OTHER;
1233 /* An upper bound on the number of bytes needed to spell TOKEN,
1234 including preceding whitespace. */
1236 cpp_token_len (token)
1237 const cpp_token *token;
1241 switch (TOKEN_SPELL (token))
1243 default: len = 0; break;
1245 case SPELL_STRING: len = token->val.str.len; break;
1246 case SPELL_IDENT: len = NODE_LEN (token->val.node); break;
1248 /* 1 for whitespace, 4 for comment delimiters. */
1252 /* Write the spelling of a token TOKEN to BUFFER. The buffer must
1253 already contain the enough space to hold the token's spelling.
1254 Returns a pointer to the character after the last character
1257 cpp_spell_token (pfile, token, buffer)
1258 cpp_reader *pfile; /* Would be nice to be rid of this... */
1259 const cpp_token *token;
1260 unsigned char *buffer;
1262 switch (TOKEN_SPELL (token))
1264 case SPELL_OPERATOR:
1266 const unsigned char *spelling;
1269 if (token->flags & DIGRAPH)
1271 = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH];
1272 else if (token->flags & NAMED_OP)
1275 spelling = TOKEN_NAME (token);
1277 while ((c = *spelling++) != '\0')
1283 *buffer++ = token->val.c;
1288 memcpy (buffer, NODE_NAME (token->val.node), NODE_LEN (token->val.node));
1289 buffer += NODE_LEN (token->val.node);
1293 memcpy (buffer, token->val.str.text, token->val.str.len);
1294 buffer += token->val.str.len;
1299 int left, right, tag;
1300 switch (token->type)
1302 case CPP_STRING: left = '"'; right = '"'; tag = '\0'; break;
1303 case CPP_WSTRING: left = '"'; right = '"'; tag = 'L'; break;
1304 case CPP_CHAR: left = '\''; right = '\''; tag = '\0'; break;
1305 case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
1306 case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
1308 cpp_error (pfile, DL_ICE, "unknown string token %s\n",
1309 TOKEN_NAME (token));
1312 if (tag) *buffer++ = tag;
1314 memcpy (buffer, token->val.str.text, token->val.str.len);
1315 buffer += token->val.str.len;
1321 cpp_error (pfile, DL_ICE, "unspellable token %s", TOKEN_NAME (token));
1328 /* Returns TOKEN spelt as a null-terminated string. The string is
1329 freed when the reader is destroyed. Useful for diagnostics. */
1331 cpp_token_as_text (pfile, token)
1333 const cpp_token *token;
1335 unsigned int len = cpp_token_len (token);
1336 unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end;
1338 end = cpp_spell_token (pfile, token, start);
1344 /* Used by C front ends, which really should move to using
1345 cpp_token_as_text. */
1347 cpp_type2name (type)
1348 enum cpp_ttype type;
1350 return (const char *) token_spellings[type].name;
1353 /* Writes the spelling of token to FP, without any preceding space.
1354 Separated from cpp_spell_token for efficiency - to avoid stdio
1355 double-buffering. */
1357 cpp_output_token (token, fp)
1358 const cpp_token *token;
1361 switch (TOKEN_SPELL (token))
1363 case SPELL_OPERATOR:
1365 const unsigned char *spelling;
1368 if (token->flags & DIGRAPH)
1370 = digraph_spellings[(int) token->type - (int) CPP_FIRST_DIGRAPH];
1371 else if (token->flags & NAMED_OP)
1374 spelling = TOKEN_NAME (token);
1379 while ((c = *++spelling) != '\0');
1384 putc (token->val.c, fp);
1389 fwrite (NODE_NAME (token->val.node), 1, NODE_LEN (token->val.node), fp);
1393 fwrite (token->val.str.text, 1, token->val.str.len, fp);
1398 int left, right, tag;
1399 switch (token->type)
1401 case CPP_STRING: left = '"'; right = '"'; tag = '\0'; break;
1402 case CPP_WSTRING: left = '"'; right = '"'; tag = 'L'; break;
1403 case CPP_CHAR: left = '\''; right = '\''; tag = '\0'; break;
1404 case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
1405 case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
1407 fprintf (stderr, "impossible STRING token %s\n", TOKEN_NAME (token));
1410 if (tag) putc (tag, fp);
1412 fwrite (token->val.str.text, 1, token->val.str.len, fp);
1418 /* An error, most probably. */
1423 /* Compare two tokens. */
1425 _cpp_equiv_tokens (a, b)
1426 const cpp_token *a, *b;
1428 if (a->type == b->type && a->flags == b->flags)
1429 switch (TOKEN_SPELL (a))
1431 default: /* Keep compiler happy. */
1432 case SPELL_OPERATOR:
1435 return a->val.c == b->val.c; /* Character. */
1437 return (a->type != CPP_MACRO_ARG || a->val.arg_no == b->val.arg_no);
1439 return a->val.node == b->val.node;
1442 return (a->val.str.len == b->val.str.len
1443 && !memcmp (a->val.str.text, b->val.str.text,
1450 /* Returns nonzero if a space should be inserted to avoid an
1451 accidental token paste for output. For simplicity, it is
1452 conservative, and occasionally advises a space where one is not
1453 needed, e.g. "." and ".2". */
1455 cpp_avoid_paste (pfile, token1, token2)
1457 const cpp_token *token1, *token2;
1459 enum cpp_ttype a = token1->type, b = token2->type;
1462 if (token1->flags & NAMED_OP)
1464 if (token2->flags & NAMED_OP)
1468 if (token2->flags & DIGRAPH)
1469 c = digraph_spellings[(int) b - (int) CPP_FIRST_DIGRAPH][0];
1470 else if (token_spellings[b].category == SPELL_OPERATOR)
1471 c = token_spellings[b].name[0];
1473 /* Quickly get everything that can paste with an '='. */
1474 if ((int) a <= (int) CPP_LAST_EQ && c == '=')
1479 case CPP_GREATER: return c == '>' || c == '?';
1480 case CPP_LESS: return c == '<' || c == '?' || c == '%' || c == ':';
1481 case CPP_PLUS: return c == '+';
1482 case CPP_MINUS: return c == '-' || c == '>';
1483 case CPP_DIV: return c == '/' || c == '*'; /* Comments. */
1484 case CPP_MOD: return c == ':' || c == '>';
1485 case CPP_AND: return c == '&';
1486 case CPP_OR: return c == '|';
1487 case CPP_COLON: return c == ':' || c == '>';
1488 case CPP_DEREF: return c == '*';
1489 case CPP_DOT: return c == '.' || c == '%' || b == CPP_NUMBER;
1490 case CPP_HASH: return c == '#' || c == '%'; /* Digraph form. */
1491 case CPP_NAME: return ((b == CPP_NUMBER
1492 && name_p (pfile, &token2->val.str))
1494 || b == CPP_CHAR || b == CPP_STRING); /* L */
1495 case CPP_NUMBER: return (b == CPP_NUMBER || b == CPP_NAME
1496 || c == '.' || c == '+' || c == '-');
1497 case CPP_OTHER: return (CPP_OPTION (pfile, objc)
1498 && token1->val.c == '@'
1499 && (b == CPP_NAME || b == CPP_STRING));
1506 /* Output all the remaining tokens on the current line, and a newline
1507 character, to FP. Leading whitespace is removed. If there are
1508 macros, special token padding is not performed. */
1510 cpp_output_line (pfile, fp)
1514 const cpp_token *token;
1516 token = cpp_get_token (pfile);
1517 while (token->type != CPP_EOF)
1519 cpp_output_token (token, fp);
1520 token = cpp_get_token (pfile);
1521 if (token->flags & PREV_WHITE)
1528 /* Returns the value of a hexadecimal digit. */
1534 return hex_value (c);
1539 /* Parse a '\uNNNN' or '\UNNNNNNNN' sequence. Returns 1 to indicate
1540 failure if cpplib is not parsing C++ or C99. Such failure is
1541 silent, and no variables are updated. Otherwise returns 0, and
1542 warns if -Wtraditional.
1544 [lex.charset]: The character designated by the universal character
1545 name \UNNNNNNNN is that character whose character short name in
1546 ISO/IEC 10646 is NNNNNNNN; the character designated by the
1547 universal character name \uNNNN is that character whose character
1548 short name in ISO/IEC 10646 is 0000NNNN. If the hexadecimal value
1549 for a universal character name is less than 0x20 or in the range
1550 0x7F-0x9F (inclusive), or if the universal character name
1551 designates a character in the basic source character set, then the
1552 program is ill-formed.
1554 We assume that wchar_t is Unicode, so we don't need to do any
1555 mapping. Is this ever wrong?
1557 PC points to the 'u' or 'U', PSTR is points to the byte after PC,
1558 LIMIT is the end of the string or charconst. PSTR is updated to
1559 point after the UCS on return, and the UCS is written into PC. */
1562 maybe_read_ucs (pfile, pstr, limit, pc)
1564 const unsigned char **pstr;
1565 const unsigned char *limit;
1568 const unsigned char *p = *pstr;
1569 unsigned int code = 0;
1570 unsigned int c = *pc, length;
1572 /* Only attempt to interpret a UCS for C++ and C99. */
1573 if (! (CPP_OPTION (pfile, cplusplus) || CPP_OPTION (pfile, c99)))
1576 if (CPP_WTRADITIONAL (pfile))
1577 cpp_error (pfile, DL_WARNING,
1578 "the meaning of '\\%c' is different in traditional C", c);
1580 length = (c == 'u' ? 4: 8);
1582 if ((size_t) (limit - p) < length)
1584 cpp_error (pfile, DL_ERROR, "incomplete universal-character-name");
1585 /* Skip to the end to avoid more diagnostics. */
1590 for (; length; length--, p++)
1594 code = (code << 4) + hex_digit_value (c);
1597 cpp_error (pfile, DL_ERROR,
1598 "non-hex digit '%c' in universal-character-name", c);
1599 /* We shouldn't skip in case there are multibyte chars. */
1605 if (CPP_OPTION (pfile, EBCDIC))
1607 cpp_error (pfile, DL_ERROR, "universal-character-name on EBCDIC target");
1608 code = 0x3f; /* EBCDIC invalid character */
1610 /* True extended characters are OK. */
1611 else if (code >= 0xa0
1612 && !(code & 0x80000000)
1613 && !(code >= 0xD800 && code <= 0xDFFF))
1615 /* The standard permits $, @ and ` to be specified as UCNs. We use
1616 hex escapes so that this also works with EBCDIC hosts. */
1617 else if (code == 0x24 || code == 0x40 || code == 0x60)
1619 /* Don't give another error if one occurred above. */
1620 else if (length == 0)
1621 cpp_error (pfile, DL_ERROR, "universal-character-name out of range");
1628 /* Returns the value of an escape sequence, truncated to the correct
1629 target precision. PSTR points to the input pointer, which is just
1630 after the backslash. LIMIT is how much text we have. WIDE is true
1631 if the escape sequence is part of a wide character constant or
1632 string literal. Handles all relevant diagnostics. */
1634 cpp_parse_escape (pfile, pstr, limit, wide)
1636 const unsigned char **pstr;
1637 const unsigned char *limit;
1640 /* Values of \a \b \e \f \n \r \t \v respectively. */
1641 static const uchar ascii[] = { 7, 8, 27, 12, 10, 13, 9, 11 };
1642 static const uchar ebcdic[] = { 47, 22, 39, 12, 21, 13, 5, 11 };
1645 const unsigned char *str = *pstr, *charconsts;
1649 if (CPP_OPTION (pfile, EBCDIC))
1650 charconsts = ebcdic;
1655 width = CPP_OPTION (pfile, wchar_precision);
1657 width = CPP_OPTION (pfile, char_precision);
1658 if (width < BITS_PER_CPPCHAR_T)
1659 mask = ((cppchar_t) 1 << width) - 1;
1666 case '\\': case '\'': case '"': case '?': break;
1667 case 'b': c = charconsts[1]; break;
1668 case 'f': c = charconsts[3]; break;
1669 case 'n': c = charconsts[4]; break;
1670 case 'r': c = charconsts[5]; break;
1671 case 't': c = charconsts[6]; break;
1672 case 'v': c = charconsts[7]; break;
1674 case '(': case '{': case '[': case '%':
1675 /* '\(', etc, are used at beginning of line to avoid confusing Emacs.
1676 '\%' is used to prevent SCCS from getting confused. */
1677 unknown = CPP_PEDANTIC (pfile);
1681 if (CPP_WTRADITIONAL (pfile))
1682 cpp_error (pfile, DL_WARNING,
1683 "the meaning of '\\a' is different in traditional C");
1688 if (CPP_PEDANTIC (pfile))
1689 cpp_error (pfile, DL_PEDWARN,
1690 "non-ISO-standard escape sequence, '\\%c'", (int) c);
1695 unknown = maybe_read_ucs (pfile, &str, limit, &c);
1699 if (CPP_WTRADITIONAL (pfile))
1700 cpp_error (pfile, DL_WARNING,
1701 "the meaning of '\\x' is different in traditional C");
1704 cppchar_t i = 0, overflow = 0;
1705 int digits_found = 0;
1713 overflow |= i ^ (i << 4 >> 4);
1714 i = (i << 4) + hex_digit_value (c);
1719 cpp_error (pfile, DL_ERROR,
1720 "\\x used with no following hex digits");
1722 if (overflow | (i != (i & mask)))
1724 cpp_error (pfile, DL_PEDWARN,
1725 "hex escape sequence out of range");
1732 case '0': case '1': case '2': case '3':
1733 case '4': case '5': case '6': case '7':
1736 cppchar_t i = c - '0';
1738 while (str < limit && ++count < 3)
1741 if (c < '0' || c > '7')
1744 i = (i << 3) + c - '0';
1747 if (i != (i & mask))
1749 cpp_error (pfile, DL_PEDWARN,
1750 "octal escape sequence out of range");
1765 cpp_error (pfile, DL_PEDWARN,
1766 "unknown escape sequence '\\%c'", (int) c);
1768 cpp_error (pfile, DL_PEDWARN,
1769 "unknown escape sequence: '\\%03o'", (int) c);
1774 cpp_error (pfile, DL_PEDWARN, "escape sequence out of range for its type");
1782 /* Interpret a (possibly wide) character constant in TOKEN.
1783 WARN_MULTI warns about multi-character charconsts. PCHARS_SEEN
1784 points to a variable that is filled in with the number of
1785 characters seen, and UNSIGNEDP to a variable that indicates whether
1786 the result has signed type. */
1788 cpp_interpret_charconst (pfile, token, pchars_seen, unsignedp)
1790 const cpp_token *token;
1791 unsigned int *pchars_seen;
1794 const unsigned char *str = token->val.str.text;
1795 const unsigned char *limit = str + token->val.str.len;
1796 unsigned int chars_seen = 0;
1797 size_t width, max_chars;
1798 cppchar_t c, mask, result = 0;
1801 /* Width in bits. */
1802 if (token->type == CPP_CHAR)
1804 width = CPP_OPTION (pfile, char_precision);
1805 max_chars = CPP_OPTION (pfile, int_precision) / width;
1806 unsigned_p = CPP_OPTION (pfile, unsigned_char);
1810 width = CPP_OPTION (pfile, wchar_precision);
1812 unsigned_p = CPP_OPTION (pfile, unsigned_wchar);
1815 if (width < BITS_PER_CPPCHAR_T)
1816 mask = ((cppchar_t) 1 << width) - 1;
1825 c = cpp_parse_escape (pfile, &str, limit, token->type == CPP_WCHAR);
1827 #ifdef MAP_CHARACTER
1829 c = MAP_CHARACTER (c);
1834 /* Truncate the character, scale the result and merge the two. */
1836 if (width < BITS_PER_CPPCHAR_T)
1837 result = (result << width) | c;
1842 if (chars_seen == 0)
1843 cpp_error (pfile, DL_ERROR, "empty character constant");
1844 else if (chars_seen > 1)
1846 /* Multichar charconsts are of type int and therefore signed. */
1849 if (chars_seen > max_chars)
1851 chars_seen = max_chars;
1852 cpp_error (pfile, DL_WARNING,
1853 "character constant too long for its type");
1855 else if (CPP_OPTION (pfile, warn_multichar))
1856 cpp_error (pfile, DL_WARNING, "multi-character character constant");
1859 /* Sign-extend or truncate the constant to cppchar_t. The value is
1860 in WIDTH bits, but for multi-char charconsts it's value is the
1861 full target type's width. */
1864 if (width < BITS_PER_CPPCHAR_T)
1866 mask = ((cppchar_t) 1 << width) - 1;
1867 if (unsigned_p || !(result & (1 << (width - 1))))
1873 *pchars_seen = chars_seen;
1874 *unsignedp = unsigned_p;
1878 /* Memory buffers. Changing these three constants can have a dramatic
1879 effect on performance. The values here are reasonable defaults,
1880 but might be tuned. If you adjust them, be sure to test across a
1881 range of uses of cpplib, including heavy nested function-like macro
1882 expansion. Also check the change in peak memory usage (NJAMD is a
1883 good tool for this). */
1884 #define MIN_BUFF_SIZE 8000
1885 #define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2)
1886 #define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \
1887 (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2)
1889 #if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0)
1890 #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
1893 /* Create a new allocation buffer. Place the control block at the end
1894 of the buffer, so that buffer overflows will cause immediate chaos. */
1900 unsigned char *base;
1902 if (len < MIN_BUFF_SIZE)
1903 len = MIN_BUFF_SIZE;
1904 len = CPP_ALIGN (len);
1906 base = xmalloc (len + sizeof (_cpp_buff));
1907 result = (_cpp_buff *) (base + len);
1908 result->base = base;
1910 result->limit = base + len;
1911 result->next = NULL;
1915 /* Place a chain of unwanted allocation buffers on the free list. */
1917 _cpp_release_buff (pfile, buff)
1921 _cpp_buff *end = buff;
1925 end->next = pfile->free_buffs;
1926 pfile->free_buffs = buff;
1929 /* Return a free buffer of size at least MIN_SIZE. */
1931 _cpp_get_buff (pfile, min_size)
1935 _cpp_buff *result, **p;
1937 for (p = &pfile->free_buffs;; p = &(*p)->next)
1942 return new_buff (min_size);
1944 size = result->limit - result->base;
1945 /* Return a buffer that's big enough, but don't waste one that's
1947 if (size >= min_size && size <= BUFF_SIZE_UPPER_BOUND (min_size))
1952 result->next = NULL;
1953 result->cur = result->base;
1957 /* Creates a new buffer with enough space to hold the uncommitted
1958 remaining bytes of BUFF, and at least MIN_EXTRA more bytes. Copies
1959 the excess bytes to the new buffer. Chains the new buffer after
1960 BUFF, and returns the new buffer. */
1962 _cpp_append_extend_buff (pfile, buff, min_extra)
1967 size_t size = EXTENDED_BUFF_SIZE (buff, min_extra);
1968 _cpp_buff *new_buff = _cpp_get_buff (pfile, size);
1970 buff->next = new_buff;
1971 memcpy (new_buff->base, buff->cur, BUFF_ROOM (buff));
1975 /* Creates a new buffer with enough space to hold the uncommitted
1976 remaining bytes of the buffer pointed to by BUFF, and at least
1977 MIN_EXTRA more bytes. Copies the excess bytes to the new buffer.
1978 Chains the new buffer before the buffer pointed to by BUFF, and
1979 updates the pointer to point to the new buffer. */
1981 _cpp_extend_buff (pfile, pbuff, min_extra)
1986 _cpp_buff *new_buff, *old_buff = *pbuff;
1987 size_t size = EXTENDED_BUFF_SIZE (old_buff, min_extra);
1989 new_buff = _cpp_get_buff (pfile, size);
1990 memcpy (new_buff->base, old_buff->cur, BUFF_ROOM (old_buff));
1991 new_buff->next = old_buff;
1995 /* Free a chain of buffers starting at BUFF. */
1997 _cpp_free_buff (buff)
2002 for (; buff; buff = next)
2009 /* Allocate permanent, unaligned storage of length LEN. */
2011 _cpp_unaligned_alloc (pfile, len)
2015 _cpp_buff *buff = pfile->u_buff;
2016 unsigned char *result = buff->cur;
2018 if (len > (size_t) (buff->limit - result))
2020 buff = _cpp_get_buff (pfile, len);
2021 buff->next = pfile->u_buff;
2022 pfile->u_buff = buff;
2026 buff->cur = result + len;
2030 /* Allocate permanent, unaligned storage of length LEN from a_buff.
2031 That buffer is used for growing allocations when saving macro
2032 replacement lists in a #define, and when parsing an answer to an
2033 assertion in #assert, #unassert or #if (and therefore possibly
2034 whilst expanding macros). It therefore must not be used by any
2035 code that they might call: specifically the lexer and the guts of
2038 All existing other uses clearly fit this restriction: storing
2039 registered pragmas during initialization. */
2041 _cpp_aligned_alloc (pfile, len)
2045 _cpp_buff *buff = pfile->a_buff;
2046 unsigned char *result = buff->cur;
2048 if (len > (size_t) (buff->limit - result))
2050 buff = _cpp_get_buff (pfile, len);
2051 buff->next = pfile->a_buff;
2052 pfile->a_buff = buff;
2056 buff->cur = result + len;