1 /* CPP Library - lexical analysis.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7 Broken out to separate file, Zack Weinberg, Mar 2000
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
38 enum spell_type category
;
39 const unsigned char *name
;
42 static const unsigned char *const digraph_spellings
[] =
43 { UC
"%:", UC
"%:%:", UC
"<:", UC
":>", UC
"<%", UC
"%>" };
45 #define OP(e, s) { SPELL_OPERATOR, UC s },
46 #define TK(e, s) { SPELL_ ## s, UC #e },
47 static const struct token_spelling token_spellings
[N_TTYPES
] = { TTYPE_TABLE
};
51 #define TOKEN_SPELL(token) (token_spellings[(token)->type].category)
52 #define TOKEN_NAME(token) (token_spellings[(token)->type].name)
54 static void add_line_note (cpp_buffer
*, const uchar
*, unsigned int);
55 static int skip_line_comment (cpp_reader
*);
56 static void skip_whitespace (cpp_reader
*, cppchar_t
);
57 static void lex_string (cpp_reader
*, cpp_token
*, const uchar
*);
58 static void save_comment (cpp_reader
*, cpp_token
*, const uchar
*, cppchar_t
);
59 static void store_comment (cpp_reader
*, cpp_token
*);
60 static void create_literal (cpp_reader
*, cpp_token
*, const uchar
*,
61 unsigned int, enum cpp_ttype
);
62 static bool warn_in_comment (cpp_reader
*, _cpp_line_note
*);
63 static int name_p (cpp_reader
*, const cpp_string
*);
64 static tokenrun
*next_tokenrun (tokenrun
*);
66 static _cpp_buff
*new_buff (size_t);
71 Compares, the token TOKEN to the NUL-terminated string STRING.
72 TOKEN must be a CPP_NAME. Returns 1 for equal, 0 for unequal. */
74 cpp_ideq (const cpp_token
*token
, const char *string
)
76 if (token
->type
!= CPP_NAME
)
79 return !ustrcmp (NODE_NAME (token
->val
.node
.node
), (const uchar
*) string
);
82 /* Record a note TYPE at byte POS into the current cleaned logical
85 add_line_note (cpp_buffer
*buffer
, const uchar
*pos
, unsigned int type
)
87 if (buffer
->notes_used
== buffer
->notes_cap
)
89 buffer
->notes_cap
= buffer
->notes_cap
* 2 + 200;
90 buffer
->notes
= XRESIZEVEC (_cpp_line_note
, buffer
->notes
,
94 buffer
->notes
[buffer
->notes_used
].pos
= pos
;
95 buffer
->notes
[buffer
->notes_used
].type
= type
;
99 /* Returns with a logical line that contains no escaped newlines or
100 trigraphs. This is a time-critical inner loop. */
102 _cpp_clean_line (cpp_reader
*pfile
)
108 buffer
= pfile
->buffer
;
109 buffer
->cur_note
= buffer
->notes_used
= 0;
110 buffer
->cur
= buffer
->line_base
= buffer
->next_line
;
111 buffer
->need_line
= false;
112 s
= buffer
->next_line
- 1;
114 if (!buffer
->from_stage3
)
116 const uchar
*pbackslash
= NULL
;
118 /* Short circuit for the common case of an un-escaped line with
119 no trigraphs. The primary win here is by not writing any
120 data back to memory until we have to. */
124 if (__builtin_expect (c
== '\n', false)
125 || __builtin_expect (c
== '\r', false))
129 if (__builtin_expect (s
== buffer
->rlimit
, false))
132 /* DOS line ending? */
133 if (__builtin_expect (c
== '\r', false)
137 if (s
== buffer
->rlimit
)
141 if (__builtin_expect (pbackslash
== NULL
, true))
144 /* Check for escaped newline. */
146 while (is_nvspace (p
[-1]))
148 if (p
- 1 != pbackslash
)
151 /* Have an escaped newline; process it and proceed to
153 add_line_note (buffer
, p
- 1, p
!= d
? ' ' : '\\');
155 buffer
->next_line
= p
- 1;
158 if (__builtin_expect (c
== '\\', false))
160 else if (__builtin_expect (c
== '?', false)
161 && __builtin_expect (s
[1] == '?', false)
162 && _cpp_trigraph_map
[s
[2]])
164 /* Have a trigraph. We may or may not have to convert
165 it. Add a line note regardless, for -Wtrigraphs. */
166 add_line_note (buffer
, s
, s
[2]);
167 if (CPP_OPTION (pfile
, trigraphs
))
169 /* We do, and that means we have to switch to the
172 *d
= _cpp_trigraph_map
[s
[2]];
185 if (c
== '\n' || c
== '\r')
187 /* Handle DOS line endings. */
188 if (c
== '\r' && s
!= buffer
->rlimit
&& s
[1] == '\n')
190 if (s
== buffer
->rlimit
)
195 while (p
!= buffer
->next_line
&& is_nvspace (p
[-1]))
197 if (p
== buffer
->next_line
|| p
[-1] != '\\')
200 add_line_note (buffer
, p
- 1, p
!= d
? ' ': '\\');
202 buffer
->next_line
= p
- 1;
204 else if (c
== '?' && s
[1] == '?' && _cpp_trigraph_map
[s
[2]])
206 /* Add a note regardless, for the benefit of -Wtrigraphs. */
207 add_line_note (buffer
, d
, s
[2]);
208 if (CPP_OPTION (pfile
, trigraphs
))
210 *d
= _cpp_trigraph_map
[s
[2]];
220 while (*s
!= '\n' && *s
!= '\r');
223 /* Handle DOS line endings. */
224 if (*s
== '\r' && s
!= buffer
->rlimit
&& s
[1] == '\n')
230 /* A sentinel note that should never be processed. */
231 add_line_note (buffer
, d
+ 1, '\n');
232 buffer
->next_line
= s
+ 1;
235 /* Return true if the trigraph indicated by NOTE should be warned
236 about in a comment. */
238 warn_in_comment (cpp_reader
*pfile
, _cpp_line_note
*note
)
242 /* Within comments we don't warn about trigraphs, unless the
243 trigraph forms an escaped newline, as that may change
245 if (note
->type
!= '/')
248 /* If -trigraphs, then this was an escaped newline iff the next note
250 if (CPP_OPTION (pfile
, trigraphs
))
251 return note
[1].pos
== note
->pos
;
253 /* Otherwise, see if this forms an escaped newline. */
255 while (is_nvspace (*p
))
258 /* There might have been escaped newlines between the trigraph and the
259 newline we found. Hence the position test. */
260 return (*p
== '\n' && p
< note
[1].pos
);
263 /* Process the notes created by add_line_note as far as the current
266 _cpp_process_line_notes (cpp_reader
*pfile
, int in_comment
)
268 cpp_buffer
*buffer
= pfile
->buffer
;
272 _cpp_line_note
*note
= &buffer
->notes
[buffer
->cur_note
];
275 if (note
->pos
> buffer
->cur
)
279 col
= CPP_BUF_COLUMN (buffer
, note
->pos
+ 1);
281 if (note
->type
== '\\' || note
->type
== ' ')
283 if (note
->type
== ' ' && !in_comment
)
284 cpp_error_with_line (pfile
, CPP_DL_WARNING
, pfile
->line_table
->highest_line
, col
,
285 "backslash and newline separated by space");
287 if (buffer
->next_line
> buffer
->rlimit
)
289 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, pfile
->line_table
->highest_line
, col
,
290 "backslash-newline at end of file");
291 /* Prevent "no newline at end of file" warning. */
292 buffer
->next_line
= buffer
->rlimit
;
295 buffer
->line_base
= note
->pos
;
296 CPP_INCREMENT_LINE (pfile
, 0);
298 else if (_cpp_trigraph_map
[note
->type
])
300 if (CPP_OPTION (pfile
, warn_trigraphs
)
301 && (!in_comment
|| warn_in_comment (pfile
, note
)))
303 if (CPP_OPTION (pfile
, trigraphs
))
304 cpp_warning_with_line (pfile
, CPP_W_TRIGRAPHS
,
305 pfile
->line_table
->highest_line
, col
,
306 "trigraph ??%c converted to %c",
308 (int) _cpp_trigraph_map
[note
->type
]);
311 cpp_warning_with_line
312 (pfile
, CPP_W_TRIGRAPHS
,
313 pfile
->line_table
->highest_line
, col
,
314 "trigraph ??%c ignored, use -trigraphs to enable",
319 else if (note
->type
== 0)
320 /* Already processed in lex_raw_string. */;
326 /* Skip a C-style block comment. We find the end of the comment by
327 seeing if an asterisk is before every '/' we encounter. Returns
328 nonzero if comment terminated by EOF, zero otherwise.
330 Buffer->cur points to the initial asterisk of the comment. */
332 _cpp_skip_block_comment (cpp_reader
*pfile
)
334 cpp_buffer
*buffer
= pfile
->buffer
;
335 const uchar
*cur
= buffer
->cur
;
344 /* People like decorating comments with '*', so check for '/'
345 instead for efficiency. */
353 /* Warn about potential nested comments, but not if the '/'
354 comes immediately before the true comment delimiter.
355 Don't bother to get it right across escaped newlines. */
356 if (CPP_OPTION (pfile
, warn_comments
)
357 && cur
[0] == '*' && cur
[1] != '/')
360 cpp_warning_with_line (pfile
, CPP_W_COMMENTS
,
361 pfile
->line_table
->highest_line
,
362 CPP_BUF_COL (buffer
),
363 "\"/*\" within comment");
369 buffer
->cur
= cur
- 1;
370 _cpp_process_line_notes (pfile
, true);
371 if (buffer
->next_line
>= buffer
->rlimit
)
373 _cpp_clean_line (pfile
);
375 cols
= buffer
->next_line
- buffer
->line_base
;
376 CPP_INCREMENT_LINE (pfile
, cols
);
383 _cpp_process_line_notes (pfile
, true);
387 /* Skip a C++ line comment, leaving buffer->cur pointing to the
388 terminating newline. Handles escaped newlines. Returns nonzero
389 if a multiline comment. */
391 skip_line_comment (cpp_reader
*pfile
)
393 cpp_buffer
*buffer
= pfile
->buffer
;
394 source_location orig_line
= pfile
->line_table
->highest_line
;
396 while (*buffer
->cur
!= '\n')
399 _cpp_process_line_notes (pfile
, true);
400 return orig_line
!= pfile
->line_table
->highest_line
;
403 /* Skips whitespace, saving the next non-whitespace character. */
405 skip_whitespace (cpp_reader
*pfile
, cppchar_t c
)
407 cpp_buffer
*buffer
= pfile
->buffer
;
408 bool saw_NUL
= false;
412 /* Horizontal space always OK. */
413 if (c
== ' ' || c
== '\t')
415 /* Just \f \v or \0 left. */
418 else if (pfile
->state
.in_directive
&& CPP_PEDANTIC (pfile
))
419 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, pfile
->line_table
->highest_line
,
420 CPP_BUF_COL (buffer
),
421 "%s in preprocessing directive",
422 c
== '\f' ? "form feed" : "vertical tab");
426 /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
427 while (is_nvspace (c
));
430 cpp_error (pfile
, CPP_DL_WARNING
, "null character(s) ignored");
435 /* See if the characters of a number token are valid in a name (no
438 name_p (cpp_reader
*pfile
, const cpp_string
*string
)
442 for (i
= 0; i
< string
->len
; i
++)
443 if (!is_idchar (string
->text
[i
]))
449 /* After parsing an identifier or other sequence, produce a warning about
450 sequences not in NFC/NFKC. */
452 warn_about_normalization (cpp_reader
*pfile
,
453 const cpp_token
*token
,
454 const struct normalize_state
*s
)
456 if (CPP_OPTION (pfile
, warn_normalize
) < NORMALIZE_STATE_RESULT (s
)
457 && !pfile
->state
.skipping
)
459 /* Make sure that the token is printed using UCNs, even
460 if we'd otherwise happily print UTF-8. */
461 unsigned char *buf
= XNEWVEC (unsigned char, cpp_token_len (token
));
464 sz
= cpp_spell_token (pfile
, token
, buf
, false) - buf
;
465 if (NORMALIZE_STATE_RESULT (s
) == normalized_C
)
466 cpp_warning_with_line (pfile
, CPP_W_NORMALIZE
, token
->src_loc
, 0,
467 "`%.*s' is not in NFKC", (int) sz
, buf
);
469 cpp_warning_with_line (pfile
, CPP_W_NORMALIZE
, token
->src_loc
, 0,
470 "`%.*s' is not in NFC", (int) sz
, buf
);
474 /* Returns TRUE if the sequence starting at buffer->cur is invalid in
475 an identifier. FIRST is TRUE if this starts an identifier. */
477 forms_identifier_p (cpp_reader
*pfile
, int first
,
478 struct normalize_state
*state
)
480 cpp_buffer
*buffer
= pfile
->buffer
;
482 if (*buffer
->cur
== '$')
484 if (!CPP_OPTION (pfile
, dollars_in_ident
))
488 if (CPP_OPTION (pfile
, warn_dollars
) && !pfile
->state
.skipping
)
490 CPP_OPTION (pfile
, warn_dollars
) = 0;
491 cpp_error (pfile
, CPP_DL_PEDWARN
, "'$' in identifier or number");
497 /* Is this a syntactically valid UCN? */
498 if (CPP_OPTION (pfile
, extended_identifiers
)
499 && *buffer
->cur
== '\\'
500 && (buffer
->cur
[1] == 'u' || buffer
->cur
[1] == 'U'))
503 if (_cpp_valid_ucn (pfile
, &buffer
->cur
, buffer
->rlimit
, 1 + !first
,
512 /* Helper function to get the cpp_hashnode of the identifier BASE. */
513 static cpp_hashnode
*
514 lex_identifier_intern (cpp_reader
*pfile
, const uchar
*base
)
516 cpp_hashnode
*result
;
519 unsigned int hash
= HT_HASHSTEP (0, *base
);
522 while (ISIDNUM (*cur
))
524 hash
= HT_HASHSTEP (hash
, *cur
);
528 hash
= HT_HASHFINISH (hash
, len
);
529 result
= CPP_HASHNODE (ht_lookup_with_hash (pfile
->hash_table
,
530 base
, len
, hash
, HT_ALLOC
));
532 /* Rarely, identifiers require diagnostics when lexed. */
533 if (__builtin_expect ((result
->flags
& NODE_DIAGNOSTIC
)
534 && !pfile
->state
.skipping
, 0))
536 /* It is allowed to poison the same identifier twice. */
537 if ((result
->flags
& NODE_POISONED
) && !pfile
->state
.poisoned_ok
)
538 cpp_error (pfile
, CPP_DL_ERROR
, "attempt to use poisoned \"%s\"",
541 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
542 replacement list of a variadic macro. */
543 if (result
== pfile
->spec_nodes
.n__VA_ARGS__
544 && !pfile
->state
.va_args_ok
)
545 cpp_error (pfile
, CPP_DL_PEDWARN
,
546 "__VA_ARGS__ can only appear in the expansion"
547 " of a C99 variadic macro");
549 /* For -Wc++-compat, warn about use of C++ named operators. */
550 if (result
->flags
& NODE_WARN_OPERATOR
)
551 cpp_warning (pfile
, CPP_W_CXX_OPERATOR_NAMES
,
552 "identifier \"%s\" is a special operator name in C++",
559 /* Get the cpp_hashnode of an identifier specified by NAME in
560 the current cpp_reader object. If none is found, NULL is returned. */
562 _cpp_lex_identifier (cpp_reader
*pfile
, const char *name
)
564 cpp_hashnode
*result
;
565 result
= lex_identifier_intern (pfile
, (uchar
*) name
);
569 /* Lex an identifier starting at BUFFER->CUR - 1. */
570 static cpp_hashnode
*
571 lex_identifier (cpp_reader
*pfile
, const uchar
*base
, bool starts_ucn
,
572 struct normalize_state
*nst
)
574 cpp_hashnode
*result
;
577 unsigned int hash
= HT_HASHSTEP (0, *base
);
579 cur
= pfile
->buffer
->cur
;
581 while (ISIDNUM (*cur
))
583 hash
= HT_HASHSTEP (hash
, *cur
);
586 pfile
->buffer
->cur
= cur
;
587 if (starts_ucn
|| forms_identifier_p (pfile
, false, nst
))
589 /* Slower version for identifiers containing UCNs (or $). */
591 while (ISIDNUM (*pfile
->buffer
->cur
))
593 pfile
->buffer
->cur
++;
594 NORMALIZE_STATE_UPDATE_IDNUM (nst
);
596 } while (forms_identifier_p (pfile
, false, nst
));
597 result
= _cpp_interpret_identifier (pfile
, base
,
598 pfile
->buffer
->cur
- base
);
603 hash
= HT_HASHFINISH (hash
, len
);
605 result
= CPP_HASHNODE (ht_lookup_with_hash (pfile
->hash_table
,
606 base
, len
, hash
, HT_ALLOC
));
609 /* Rarely, identifiers require diagnostics when lexed. */
610 if (__builtin_expect ((result
->flags
& NODE_DIAGNOSTIC
)
611 && !pfile
->state
.skipping
, 0))
613 /* It is allowed to poison the same identifier twice. */
614 if ((result
->flags
& NODE_POISONED
) && !pfile
->state
.poisoned_ok
)
615 cpp_error (pfile
, CPP_DL_ERROR
, "attempt to use poisoned \"%s\"",
618 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
619 replacement list of a variadic macro. */
620 if (result
== pfile
->spec_nodes
.n__VA_ARGS__
621 && !pfile
->state
.va_args_ok
)
622 cpp_error (pfile
, CPP_DL_PEDWARN
,
623 "__VA_ARGS__ can only appear in the expansion"
624 " of a C99 variadic macro");
626 /* For -Wc++-compat, warn about use of C++ named operators. */
627 if (result
->flags
& NODE_WARN_OPERATOR
)
628 cpp_warning (pfile
, CPP_W_CXX_OPERATOR_NAMES
,
629 "identifier \"%s\" is a special operator name in C++",
636 /* Lex a number to NUMBER starting at BUFFER->CUR - 1. */
638 lex_number (cpp_reader
*pfile
, cpp_string
*number
,
639 struct normalize_state
*nst
)
645 base
= pfile
->buffer
->cur
- 1;
648 cur
= pfile
->buffer
->cur
;
650 /* N.B. ISIDNUM does not include $. */
651 while (ISIDNUM (*cur
) || *cur
== '.' || VALID_SIGN (*cur
, cur
[-1]))
654 NORMALIZE_STATE_UPDATE_IDNUM (nst
);
657 pfile
->buffer
->cur
= cur
;
659 while (forms_identifier_p (pfile
, false, nst
));
661 number
->len
= cur
- base
;
662 dest
= _cpp_unaligned_alloc (pfile
, number
->len
+ 1);
663 memcpy (dest
, base
, number
->len
);
664 dest
[number
->len
] = '\0';
668 /* Create a token of type TYPE with a literal spelling. */
670 create_literal (cpp_reader
*pfile
, cpp_token
*token
, const uchar
*base
,
671 unsigned int len
, enum cpp_ttype type
)
673 uchar
*dest
= _cpp_unaligned_alloc (pfile
, len
+ 1);
675 memcpy (dest
, base
, len
);
678 token
->val
.str
.len
= len
;
679 token
->val
.str
.text
= dest
;
682 /* Subroutine of lex_raw_string: Append LEN chars from BASE to the buffer
683 sequence from *FIRST_BUFF_P to LAST_BUFF_P. */
686 bufring_append (cpp_reader
*pfile
, const uchar
*base
, size_t len
,
687 _cpp_buff
**first_buff_p
, _cpp_buff
**last_buff_p
)
689 _cpp_buff
*first_buff
= *first_buff_p
;
690 _cpp_buff
*last_buff
= *last_buff_p
;
692 if (first_buff
== NULL
)
693 first_buff
= last_buff
= _cpp_get_buff (pfile
, len
);
694 else if (len
> BUFF_ROOM (last_buff
))
696 size_t room
= BUFF_ROOM (last_buff
);
697 memcpy (BUFF_FRONT (last_buff
), base
, room
);
698 BUFF_FRONT (last_buff
) += room
;
701 last_buff
= _cpp_append_extend_buff (pfile
, last_buff
, len
);
704 memcpy (BUFF_FRONT (last_buff
), base
, len
);
705 BUFF_FRONT (last_buff
) += len
;
707 *first_buff_p
= first_buff
;
708 *last_buff_p
= last_buff
;
711 /* Lexes a raw string. The stored string contains the spelling, including
712 double quotes, delimiter string, '(' and ')', any leading
713 'L', 'u', 'U' or 'u8' and 'R' modifier. It returns the type of the
714 literal, or CPP_OTHER if it was not properly terminated.
716 The spelling is NUL-terminated, but it is not guaranteed that this
717 is the first NUL since embedded NULs are preserved. */
720 lex_raw_string (cpp_reader
*pfile
, cpp_token
*token
, const uchar
*base
,
723 source_location saw_NUL
= 0;
724 const uchar
*raw_prefix
;
725 unsigned int raw_prefix_len
= 0;
727 size_t total_len
= 0;
728 _cpp_buff
*first_buff
= NULL
, *last_buff
= NULL
;
729 _cpp_line_note
*note
= &pfile
->buffer
->notes
[pfile
->buffer
->cur_note
];
731 type
= (*base
== 'L' ? CPP_WSTRING
:
732 *base
== 'U' ? CPP_STRING32
:
733 *base
== 'u' ? (base
[1] == '8' ? CPP_UTF8STRING
: CPP_STRING16
)
736 raw_prefix
= cur
+ 1;
737 while (raw_prefix_len
< 16)
739 switch (raw_prefix
[raw_prefix_len
])
741 case ' ': case '(': case ')': case '\\': case '\t':
742 case '\v': case '\f': case '\n': default:
744 /* Basic source charset except the above chars. */
745 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
746 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
747 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
748 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
750 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
751 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
752 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
753 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
755 case '0': case '1': case '2': case '3': case '4': case '5':
756 case '6': case '7': case '8': case '9':
757 case '_': case '{': case '}': case '#': case '[': case ']':
758 case '<': case '>': case '%': case ':': case ';': case '.':
759 case '?': case '*': case '+': case '-': case '/': case '^':
760 case '&': case '|': case '~': case '!': case '=': case ',':
768 if (raw_prefix
[raw_prefix_len
] != '(')
770 int col
= CPP_BUF_COLUMN (pfile
->buffer
, raw_prefix
+ raw_prefix_len
)
772 if (raw_prefix_len
== 16)
773 cpp_error_with_line (pfile
, CPP_DL_ERROR
, token
->src_loc
, col
,
774 "raw string delimiter longer than 16 characters");
776 cpp_error_with_line (pfile
, CPP_DL_ERROR
, token
->src_loc
, col
,
777 "invalid character '%c' in raw string delimiter",
778 (int) raw_prefix
[raw_prefix_len
]);
779 pfile
->buffer
->cur
= raw_prefix
- 1;
780 create_literal (pfile
, token
, base
, raw_prefix
- 1 - base
, CPP_OTHER
);
784 cur
= raw_prefix
+ raw_prefix_len
+ 1;
787 #define BUF_APPEND(STR,LEN) \
789 bufring_append (pfile, (const uchar *)(STR), (LEN), \
790 &first_buff, &last_buff); \
791 total_len += (LEN); \
796 /* If we previously performed any trigraph or line splicing
797 transformations, undo them within the body of the raw string. */
798 while (note
->pos
< cur
)
800 for (; note
->pos
== cur
; ++note
)
806 /* Restore backslash followed by newline. */
807 BUF_APPEND (base
, cur
- base
);
809 BUF_APPEND ("\\", 1);
811 if (note
->type
== ' ')
813 /* GNU backslash whitespace newline extension. FIXME
814 could be any sequence of non-vertical space. When we
815 can properly restore any such sequence, we should mark
816 this note as handled so _cpp_process_line_notes
821 BUF_APPEND ("\n", 1);
825 /* Already handled. */
829 if (_cpp_trigraph_map
[note
->type
])
831 /* Don't warn about this trigraph in
832 _cpp_process_line_notes, since trigraphs show up as
833 trigraphs in raw strings. */
834 uchar type
= note
->type
;
837 if (!CPP_OPTION (pfile
, trigraphs
))
838 /* If we didn't convert the trigraph in the first
839 place, don't do anything now either. */
842 BUF_APPEND (base
, cur
- base
);
844 BUF_APPEND ("??", 2);
846 /* ??/ followed by newline gets two line notes, one for
847 the trigraph and one for the backslash/newline. */
848 if (type
== '/' && note
[1].pos
== cur
)
850 if (note
[1].type
!= '\\'
851 && note
[1].type
!= ' ')
855 goto after_backslash
;
857 /* The ) from ??) could be part of the suffix. */
859 && strncmp ((const char *) cur
+1,
860 (const char *) raw_prefix
,
862 && cur
[raw_prefix_len
+1] == '"')
864 cur
+= raw_prefix_len
+2;
865 goto break_outer_loop
;
869 /* Skip the replacement character. */
871 BUF_APPEND (&type
, 1);
882 && strncmp ((const char *) cur
, (const char *) raw_prefix
,
884 && cur
[raw_prefix_len
] == '"')
886 cur
+= raw_prefix_len
+ 1;
891 if (pfile
->state
.in_directive
892 || pfile
->state
.parsing_args
893 || pfile
->state
.in_deferred_pragma
)
897 cpp_error_with_line (pfile
, CPP_DL_ERROR
, token
->src_loc
, 0,
898 "unterminated raw string");
902 BUF_APPEND (base
, cur
- base
);
904 if (pfile
->buffer
->cur
< pfile
->buffer
->rlimit
)
905 CPP_INCREMENT_LINE (pfile
, 0);
906 pfile
->buffer
->need_line
= true;
908 pfile
->buffer
->cur
= cur
-1;
909 _cpp_process_line_notes (pfile
, false);
910 if (!_cpp_get_fresh_line (pfile
))
912 source_location src_loc
= token
->src_loc
;
913 token
->type
= CPP_EOF
;
914 /* Tell the compiler the line number of the EOF token. */
915 token
->src_loc
= pfile
->line_table
->highest_line
;
917 if (first_buff
!= NULL
)
918 _cpp_release_buff (pfile
, first_buff
);
919 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
920 "unterminated raw string");
924 cur
= base
= pfile
->buffer
->cur
;
925 note
= &pfile
->buffer
->notes
[pfile
->buffer
->cur_note
];
927 else if (c
== '\0' && !saw_NUL
)
928 LINEMAP_POSITION_FOR_COLUMN (saw_NUL
, pfile
->line_table
,
929 CPP_BUF_COLUMN (pfile
->buffer
, cur
));
933 if (saw_NUL
&& !pfile
->state
.skipping
)
934 cpp_error_with_line (pfile
, CPP_DL_WARNING
, saw_NUL
, 0,
935 "null character(s) preserved in literal");
937 pfile
->buffer
->cur
= cur
;
938 if (first_buff
== NULL
)
939 create_literal (pfile
, token
, base
, cur
- base
, type
);
942 uchar
*dest
= _cpp_unaligned_alloc (pfile
, total_len
+ (cur
- base
) + 1);
945 token
->val
.str
.len
= total_len
+ (cur
- base
);
946 token
->val
.str
.text
= dest
;
947 last_buff
= first_buff
;
948 while (last_buff
!= NULL
)
950 memcpy (dest
, last_buff
->base
,
951 BUFF_FRONT (last_buff
) - last_buff
->base
);
952 dest
+= BUFF_FRONT (last_buff
) - last_buff
->base
;
953 last_buff
= last_buff
->next
;
955 _cpp_release_buff (pfile
, first_buff
);
956 memcpy (dest
, base
, cur
- base
);
957 dest
[cur
- base
] = '\0';
961 /* Lexes a string, character constant, or angle-bracketed header file
962 name. The stored string contains the spelling, including opening
963 quote and any leading 'L', 'u', 'U' or 'u8' and optional
964 'R' modifier. It returns the type of the literal, or CPP_OTHER
965 if it was not properly terminated, or CPP_LESS for an unterminated
966 header name which must be relexed as normal tokens.
968 The spelling is NUL-terminated, but it is not guaranteed that this
969 is the first NUL since embedded NULs are preserved. */
971 lex_string (cpp_reader
*pfile
, cpp_token
*token
, const uchar
*base
)
973 bool saw_NUL
= false;
975 cppchar_t terminator
;
980 if (terminator
== 'L' || terminator
== 'U')
982 else if (terminator
== 'u')
985 if (terminator
== '8')
988 if (terminator
== 'R')
990 lex_raw_string (pfile
, token
, base
, cur
);
993 if (terminator
== '"')
994 type
= (*base
== 'L' ? CPP_WSTRING
:
995 *base
== 'U' ? CPP_STRING32
:
996 *base
== 'u' ? (base
[1] == '8' ? CPP_UTF8STRING
: CPP_STRING16
)
998 else if (terminator
== '\'')
999 type
= (*base
== 'L' ? CPP_WCHAR
:
1000 *base
== 'U' ? CPP_CHAR32
:
1001 *base
== 'u' ? CPP_CHAR16
: CPP_CHAR
);
1003 terminator
= '>', type
= CPP_HEADER_NAME
;
1007 cppchar_t c
= *cur
++;
1009 /* In #include-style directives, terminators are not escapable. */
1010 if (c
== '\\' && !pfile
->state
.angled_headers
&& *cur
!= '\n')
1012 else if (c
== terminator
)
1017 /* Unmatched quotes always yield undefined behavior, but
1018 greedy lexing means that what appears to be an unterminated
1019 header name may actually be a legitimate sequence of tokens. */
1020 if (terminator
== '>')
1022 token
->type
= CPP_LESS
;
1032 if (saw_NUL
&& !pfile
->state
.skipping
)
1033 cpp_error (pfile
, CPP_DL_WARNING
,
1034 "null character(s) preserved in literal");
1036 if (type
== CPP_OTHER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
1037 cpp_error (pfile
, CPP_DL_PEDWARN
, "missing terminating %c character",
1040 pfile
->buffer
->cur
= cur
;
1041 create_literal (pfile
, token
, base
, cur
- base
, type
);
1044 /* Return the comment table. The client may not make any assumption
1045 about the ordering of the table. */
1047 cpp_get_comments (cpp_reader
*pfile
)
1049 return &pfile
->comments
;
1052 /* Append a comment to the end of the comment table. */
1054 store_comment (cpp_reader
*pfile
, cpp_token
*token
)
1058 if (pfile
->comments
.allocated
== 0)
1060 pfile
->comments
.allocated
= 256;
1061 pfile
->comments
.entries
= (cpp_comment
*) xmalloc
1062 (pfile
->comments
.allocated
* sizeof (cpp_comment
));
1065 if (pfile
->comments
.count
== pfile
->comments
.allocated
)
1067 pfile
->comments
.allocated
*= 2;
1068 pfile
->comments
.entries
= (cpp_comment
*) xrealloc
1069 (pfile
->comments
.entries
,
1070 pfile
->comments
.allocated
* sizeof (cpp_comment
));
1073 len
= token
->val
.str
.len
;
1075 /* Copy comment. Note, token may not be NULL terminated. */
1076 pfile
->comments
.entries
[pfile
->comments
.count
].comment
=
1077 (char *) xmalloc (sizeof (char) * (len
+ 1));
1078 memcpy (pfile
->comments
.entries
[pfile
->comments
.count
].comment
,
1079 token
->val
.str
.text
, len
);
1080 pfile
->comments
.entries
[pfile
->comments
.count
].comment
[len
] = '\0';
1082 /* Set source location. */
1083 pfile
->comments
.entries
[pfile
->comments
.count
].sloc
= token
->src_loc
;
1085 /* Increment the count of entries in the comment table. */
1086 pfile
->comments
.count
++;
1089 /* The stored comment includes the comment start and any terminator. */
1091 save_comment (cpp_reader
*pfile
, cpp_token
*token
, const unsigned char *from
,
1094 unsigned char *buffer
;
1095 unsigned int len
, clen
;
1097 len
= pfile
->buffer
->cur
- from
+ 1; /* + 1 for the initial '/'. */
1099 /* C++ comments probably (not definitely) have moved past a new
1100 line, which we don't want to save in the comment. */
1101 if (is_vspace (pfile
->buffer
->cur
[-1]))
1104 /* If we are currently in a directive, then we need to store all
1105 C++ comments as C comments internally, and so we need to
1106 allocate a little extra space in that case.
1108 Note that the only time we encounter a directive here is
1109 when we are saving comments in a "#define". */
1110 clen
= (pfile
->state
.in_directive
&& type
== '/') ? len
+ 2 : len
;
1112 buffer
= _cpp_unaligned_alloc (pfile
, clen
);
1114 token
->type
= CPP_COMMENT
;
1115 token
->val
.str
.len
= clen
;
1116 token
->val
.str
.text
= buffer
;
1119 memcpy (buffer
+ 1, from
, len
- 1);
1121 /* Finish conversion to a C comment, if necessary. */
1122 if (pfile
->state
.in_directive
&& type
== '/')
1125 buffer
[clen
- 2] = '*';
1126 buffer
[clen
- 1] = '/';
1129 /* Finally store this comment for use by clients of libcpp. */
1130 store_comment (pfile
, token
);
1133 /* Allocate COUNT tokens for RUN. */
1135 _cpp_init_tokenrun (tokenrun
*run
, unsigned int count
)
1137 run
->base
= XNEWVEC (cpp_token
, count
);
1138 run
->limit
= run
->base
+ count
;
1142 /* Returns the next tokenrun, or creates one if there is none. */
1144 next_tokenrun (tokenrun
*run
)
1146 if (run
->next
== NULL
)
1148 run
->next
= XNEW (tokenrun
);
1149 run
->next
->prev
= run
;
1150 _cpp_init_tokenrun (run
->next
, 250);
1156 /* Look ahead in the input stream. */
1158 cpp_peek_token (cpp_reader
*pfile
, int index
)
1160 cpp_context
*context
= pfile
->context
;
1161 const cpp_token
*peektok
;
1164 /* First, scan through any pending cpp_context objects. */
1165 while (context
->prev
)
1167 ptrdiff_t sz
= (context
->direct_p
1168 ? LAST (context
).token
- FIRST (context
).token
1169 : LAST (context
).ptoken
- FIRST (context
).ptoken
);
1171 if (index
< (int) sz
)
1172 return (context
->direct_p
1173 ? FIRST (context
).token
+ index
1174 : *(FIRST (context
).ptoken
+ index
));
1177 context
= context
->prev
;
1180 /* We will have to read some new tokens after all (and do so
1181 without invalidating preceding tokens). */
1183 pfile
->keep_tokens
++;
1187 peektok
= _cpp_lex_token (pfile
);
1188 if (peektok
->type
== CPP_EOF
)
1193 _cpp_backup_tokens_direct (pfile
, count
+ 1);
1194 pfile
->keep_tokens
--;
1199 /* Allocate a single token that is invalidated at the same time as the
1200 rest of the tokens on the line. Has its line and col set to the
1201 same as the last lexed token, so that diagnostics appear in the
1204 _cpp_temp_token (cpp_reader
*pfile
)
1206 cpp_token
*old
, *result
;
1207 ptrdiff_t sz
= pfile
->cur_run
->limit
- pfile
->cur_token
;
1208 ptrdiff_t la
= (ptrdiff_t) pfile
->lookaheads
;
1210 old
= pfile
->cur_token
- 1;
1211 /* Any pre-existing lookaheads must not be clobbered. */
1216 tokenrun
*next
= next_tokenrun (pfile
->cur_run
);
1219 memmove (next
->base
+ 1, next
->base
,
1220 (la
- sz
) * sizeof (cpp_token
));
1222 next
->base
[0] = pfile
->cur_run
->limit
[-1];
1226 memmove (pfile
->cur_token
+ 1, pfile
->cur_token
,
1227 MIN (la
, sz
- 1) * sizeof (cpp_token
));
1230 if (!sz
&& pfile
->cur_token
== pfile
->cur_run
->limit
)
1232 pfile
->cur_run
= next_tokenrun (pfile
->cur_run
);
1233 pfile
->cur_token
= pfile
->cur_run
->base
;
1236 result
= pfile
->cur_token
++;
1237 result
->src_loc
= old
->src_loc
;
1241 /* Lex a token into RESULT (external interface). Takes care of issues
1242 like directive handling, token lookahead, multiple include
1243 optimization and skipping. */
1245 _cpp_lex_token (cpp_reader
*pfile
)
1251 if (pfile
->cur_token
== pfile
->cur_run
->limit
)
1253 pfile
->cur_run
= next_tokenrun (pfile
->cur_run
);
1254 pfile
->cur_token
= pfile
->cur_run
->base
;
1256 /* We assume that the current token is somewhere in the current
1258 if (pfile
->cur_token
< pfile
->cur_run
->base
1259 || pfile
->cur_token
>= pfile
->cur_run
->limit
)
1262 if (pfile
->lookaheads
)
1264 pfile
->lookaheads
--;
1265 result
= pfile
->cur_token
++;
1268 result
= _cpp_lex_direct (pfile
);
1270 if (result
->flags
& BOL
)
1272 /* Is this a directive. If _cpp_handle_directive returns
1273 false, it is an assembler #. */
1274 if (result
->type
== CPP_HASH
1275 /* 6.10.3 p 11: Directives in a list of macro arguments
1276 gives undefined behavior. This implementation
1277 handles the directive as normal. */
1278 && pfile
->state
.parsing_args
!= 1)
1280 if (_cpp_handle_directive (pfile
, result
->flags
& PREV_WHITE
))
1282 if (pfile
->directive_result
.type
== CPP_PADDING
)
1284 result
= &pfile
->directive_result
;
1287 else if (pfile
->state
.in_deferred_pragma
)
1288 result
= &pfile
->directive_result
;
1290 if (pfile
->cb
.line_change
&& !pfile
->state
.skipping
)
1291 pfile
->cb
.line_change (pfile
, result
, pfile
->state
.parsing_args
);
1294 /* We don't skip tokens in directives. */
1295 if (pfile
->state
.in_directive
|| pfile
->state
.in_deferred_pragma
)
1298 /* Outside a directive, invalidate controlling macros. At file
1299 EOF, _cpp_lex_direct takes care of popping the buffer, so we never
1300 get here and MI optimization works. */
1301 pfile
->mi_valid
= false;
1303 if (!pfile
->state
.skipping
|| result
->type
== CPP_EOF
)
1310 /* Returns true if a fresh line has been loaded. */
1312 _cpp_get_fresh_line (cpp_reader
*pfile
)
1316 /* We can't get a new line until we leave the current directive. */
1317 if (pfile
->state
.in_directive
)
1322 cpp_buffer
*buffer
= pfile
->buffer
;
1324 if (!buffer
->need_line
)
1327 if (buffer
->next_line
< buffer
->rlimit
)
1329 _cpp_clean_line (pfile
);
1333 /* First, get out of parsing arguments state. */
1334 if (pfile
->state
.parsing_args
)
1337 /* End of buffer. Non-empty files should end in a newline. */
1338 if (buffer
->buf
!= buffer
->rlimit
1339 && buffer
->next_line
> buffer
->rlimit
1340 && !buffer
->from_stage3
)
1342 /* Clip to buffer size. */
1343 buffer
->next_line
= buffer
->rlimit
;
1346 return_at_eof
= buffer
->return_at_eof
;
1347 _cpp_pop_buffer (pfile
);
1348 if (pfile
->buffer
== NULL
|| return_at_eof
)
1353 #define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
1356 result->type = ELSE_TYPE; \
1357 if (*buffer->cur == CHAR) \
1358 buffer->cur++, result->type = THEN_TYPE; \
1362 /* Lex a token into pfile->cur_token, which is also incremented, to
1363 get diagnostics pointing to the correct location.
1365 Does not handle issues such as token lookahead, multiple-include
1366 optimization, directives, skipping etc. This function is only
1367 suitable for use by _cpp_lex_token, and in special cases like
1368 lex_expansion_token which doesn't care for any of these issues.
1370 When meeting a newline, returns CPP_EOF if parsing a directive,
1371 otherwise returns to the start of the token buffer if permissible.
1372 Returns the location of the lexed token. */
1374 _cpp_lex_direct (cpp_reader
*pfile
)
1378 const unsigned char *comment_start
;
1379 cpp_token
*result
= pfile
->cur_token
++;
1383 buffer
= pfile
->buffer
;
1384 if (buffer
->need_line
)
1386 if (pfile
->state
.in_deferred_pragma
)
1388 result
->type
= CPP_PRAGMA_EOL
;
1389 pfile
->state
.in_deferred_pragma
= false;
1390 if (!pfile
->state
.pragma_allow_expansion
)
1391 pfile
->state
.prevent_expansion
--;
1394 if (!_cpp_get_fresh_line (pfile
))
1396 result
->type
= CPP_EOF
;
1397 if (!pfile
->state
.in_directive
)
1399 /* Tell the compiler the line number of the EOF token. */
1400 result
->src_loc
= pfile
->line_table
->highest_line
;
1401 result
->flags
= BOL
;
1405 if (!pfile
->keep_tokens
)
1407 pfile
->cur_run
= &pfile
->base_run
;
1408 result
= pfile
->base_run
.base
;
1409 pfile
->cur_token
= result
+ 1;
1411 result
->flags
= BOL
;
1412 if (pfile
->state
.parsing_args
== 2)
1413 result
->flags
|= PREV_WHITE
;
1415 buffer
= pfile
->buffer
;
1417 result
->src_loc
= pfile
->line_table
->highest_line
;
1420 if (buffer
->cur
>= buffer
->notes
[buffer
->cur_note
].pos
1421 && !pfile
->overlaid_buffer
)
1423 _cpp_process_line_notes (pfile
, false);
1424 result
->src_loc
= pfile
->line_table
->highest_line
;
1428 LINEMAP_POSITION_FOR_COLUMN (result
->src_loc
, pfile
->line_table
,
1429 CPP_BUF_COLUMN (buffer
, buffer
->cur
));
1433 case ' ': case '\t': case '\f': case '\v': case '\0':
1434 result
->flags
|= PREV_WHITE
;
1435 skip_whitespace (pfile
, c
);
1439 if (buffer
->cur
< buffer
->rlimit
)
1440 CPP_INCREMENT_LINE (pfile
, 0);
1441 buffer
->need_line
= true;
1444 case '0': case '1': case '2': case '3': case '4':
1445 case '5': case '6': case '7': case '8': case '9':
1447 struct normalize_state nst
= INITIAL_NORMALIZE_STATE
;
1448 result
->type
= CPP_NUMBER
;
1449 lex_number (pfile
, &result
->val
.str
, &nst
);
1450 warn_about_normalization (pfile
, result
, &nst
);
1458 /* 'L', 'u', 'U', 'u8' or 'R' may introduce wide characters,
1459 wide strings or raw strings. */
1460 if (c
== 'L' || CPP_OPTION (pfile
, uliterals
))
1462 if ((*buffer
->cur
== '\'' && c
!= 'R')
1463 || *buffer
->cur
== '"'
1464 || (*buffer
->cur
== 'R'
1466 && buffer
->cur
[1] == '"'
1467 && CPP_OPTION (pfile
, uliterals
))
1468 || (*buffer
->cur
== '8'
1470 && (buffer
->cur
[1] == '"'
1471 || (buffer
->cur
[1] == 'R' && buffer
->cur
[2] == '"'))))
1473 lex_string (pfile
, result
, buffer
->cur
- 1);
1480 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1481 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1482 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1483 case 's': case 't': case 'v': case 'w': case 'x':
1485 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1486 case 'G': case 'H': case 'I': case 'J': case 'K':
1487 case 'M': case 'N': case 'O': case 'P': case 'Q':
1488 case 'S': case 'T': case 'V': case 'W': case 'X':
1490 result
->type
= CPP_NAME
;
1492 struct normalize_state nst
= INITIAL_NORMALIZE_STATE
;
1493 result
->val
.node
.node
= lex_identifier (pfile
, buffer
->cur
- 1, false,
1495 warn_about_normalization (pfile
, result
, &nst
);
1498 /* Convert named operators to their proper types. */
1499 if (result
->val
.node
.node
->flags
& NODE_OPERATOR
)
1501 result
->flags
|= NAMED_OP
;
1502 result
->type
= (enum cpp_ttype
) result
->val
.node
.node
->directive_index
;
1508 lex_string (pfile
, result
, buffer
->cur
- 1);
1512 /* A potential block or line comment. */
1513 comment_start
= buffer
->cur
;
1518 if (_cpp_skip_block_comment (pfile
))
1519 cpp_error (pfile
, CPP_DL_ERROR
, "unterminated comment");
1521 else if (c
== '/' && (CPP_OPTION (pfile
, cplusplus_comments
)
1522 || cpp_in_system_header (pfile
)))
1524 /* Warn about comments only if pedantically GNUC89, and not
1525 in system headers. */
1526 if (CPP_OPTION (pfile
, lang
) == CLK_GNUC89
&& CPP_PEDANTIC (pfile
)
1527 && ! buffer
->warned_cplusplus_comments
)
1529 cpp_error (pfile
, CPP_DL_PEDWARN
,
1530 "C++ style comments are not allowed in ISO C90");
1531 cpp_error (pfile
, CPP_DL_PEDWARN
,
1532 "(this will be reported only once per input file)");
1533 buffer
->warned_cplusplus_comments
= 1;
1536 if (skip_line_comment (pfile
) && CPP_OPTION (pfile
, warn_comments
))
1537 cpp_warning (pfile
, CPP_W_COMMENTS
, "multi-line comment");
1542 result
->type
= CPP_DIV_EQ
;
1547 result
->type
= CPP_DIV
;
1551 if (!pfile
->state
.save_comments
)
1553 result
->flags
|= PREV_WHITE
;
1554 goto update_tokens_line
;
1557 /* Save the comment as a token in its own right. */
1558 save_comment (pfile
, result
, comment_start
, c
);
1562 if (pfile
->state
.angled_headers
)
1564 lex_string (pfile
, result
, buffer
->cur
- 1);
1565 if (result
->type
!= CPP_LESS
)
1569 result
->type
= CPP_LESS
;
1570 if (*buffer
->cur
== '=')
1571 buffer
->cur
++, result
->type
= CPP_LESS_EQ
;
1572 else if (*buffer
->cur
== '<')
1575 IF_NEXT_IS ('=', CPP_LSHIFT_EQ
, CPP_LSHIFT
);
1577 else if (CPP_OPTION (pfile
, digraphs
))
1579 if (*buffer
->cur
== ':')
1582 result
->flags
|= DIGRAPH
;
1583 result
->type
= CPP_OPEN_SQUARE
;
1585 else if (*buffer
->cur
== '%')
1588 result
->flags
|= DIGRAPH
;
1589 result
->type
= CPP_OPEN_BRACE
;
1595 result
->type
= CPP_GREATER
;
1596 if (*buffer
->cur
== '=')
1597 buffer
->cur
++, result
->type
= CPP_GREATER_EQ
;
1598 else if (*buffer
->cur
== '>')
1601 IF_NEXT_IS ('=', CPP_RSHIFT_EQ
, CPP_RSHIFT
);
1606 result
->type
= CPP_MOD
;
1607 if (*buffer
->cur
== '=')
1608 buffer
->cur
++, result
->type
= CPP_MOD_EQ
;
1609 else if (CPP_OPTION (pfile
, digraphs
))
1611 if (*buffer
->cur
== ':')
1614 result
->flags
|= DIGRAPH
;
1615 result
->type
= CPP_HASH
;
1616 if (*buffer
->cur
== '%' && buffer
->cur
[1] == ':')
1617 buffer
->cur
+= 2, result
->type
= CPP_PASTE
, result
->val
.token_no
= 0;
1619 else if (*buffer
->cur
== '>')
1622 result
->flags
|= DIGRAPH
;
1623 result
->type
= CPP_CLOSE_BRACE
;
1629 result
->type
= CPP_DOT
;
1630 if (ISDIGIT (*buffer
->cur
))
1632 struct normalize_state nst
= INITIAL_NORMALIZE_STATE
;
1633 result
->type
= CPP_NUMBER
;
1634 lex_number (pfile
, &result
->val
.str
, &nst
);
1635 warn_about_normalization (pfile
, result
, &nst
);
1637 else if (*buffer
->cur
== '.' && buffer
->cur
[1] == '.')
1638 buffer
->cur
+= 2, result
->type
= CPP_ELLIPSIS
;
1639 else if (*buffer
->cur
== '*' && CPP_OPTION (pfile
, cplusplus
))
1640 buffer
->cur
++, result
->type
= CPP_DOT_STAR
;
1644 result
->type
= CPP_PLUS
;
1645 if (*buffer
->cur
== '+')
1646 buffer
->cur
++, result
->type
= CPP_PLUS_PLUS
;
1647 else if (*buffer
->cur
== '=')
1648 buffer
->cur
++, result
->type
= CPP_PLUS_EQ
;
1652 result
->type
= CPP_MINUS
;
1653 if (*buffer
->cur
== '>')
1656 result
->type
= CPP_DEREF
;
1657 if (*buffer
->cur
== '*' && CPP_OPTION (pfile
, cplusplus
))
1658 buffer
->cur
++, result
->type
= CPP_DEREF_STAR
;
1660 else if (*buffer
->cur
== '-')
1661 buffer
->cur
++, result
->type
= CPP_MINUS_MINUS
;
1662 else if (*buffer
->cur
== '=')
1663 buffer
->cur
++, result
->type
= CPP_MINUS_EQ
;
1667 result
->type
= CPP_AND
;
1668 if (*buffer
->cur
== '&')
1669 buffer
->cur
++, result
->type
= CPP_AND_AND
;
1670 else if (*buffer
->cur
== '=')
1671 buffer
->cur
++, result
->type
= CPP_AND_EQ
;
1675 result
->type
= CPP_OR
;
1676 if (*buffer
->cur
== '|')
1677 buffer
->cur
++, result
->type
= CPP_OR_OR
;
1678 else if (*buffer
->cur
== '=')
1679 buffer
->cur
++, result
->type
= CPP_OR_EQ
;
1683 result
->type
= CPP_COLON
;
1684 if (*buffer
->cur
== ':' && CPP_OPTION (pfile
, cplusplus
))
1685 buffer
->cur
++, result
->type
= CPP_SCOPE
;
1686 else if (*buffer
->cur
== '>' && CPP_OPTION (pfile
, digraphs
))
1689 result
->flags
|= DIGRAPH
;
1690 result
->type
= CPP_CLOSE_SQUARE
;
1694 case '*': IF_NEXT_IS ('=', CPP_MULT_EQ
, CPP_MULT
); break;
1695 case '=': IF_NEXT_IS ('=', CPP_EQ_EQ
, CPP_EQ
); break;
1696 case '!': IF_NEXT_IS ('=', CPP_NOT_EQ
, CPP_NOT
); break;
1697 case '^': IF_NEXT_IS ('=', CPP_XOR_EQ
, CPP_XOR
); break;
1698 case '#': IF_NEXT_IS ('#', CPP_PASTE
, CPP_HASH
); result
->val
.token_no
= 0; break;
1700 case '?': result
->type
= CPP_QUERY
; break;
1701 case '~': result
->type
= CPP_COMPL
; break;
1702 case ',': result
->type
= CPP_COMMA
; break;
1703 case '(': result
->type
= CPP_OPEN_PAREN
; break;
1704 case ')': result
->type
= CPP_CLOSE_PAREN
; break;
1705 case '[': result
->type
= CPP_OPEN_SQUARE
; break;
1706 case ']': result
->type
= CPP_CLOSE_SQUARE
; break;
1707 case '{': result
->type
= CPP_OPEN_BRACE
; break;
1708 case '}': result
->type
= CPP_CLOSE_BRACE
; break;
1709 case ';': result
->type
= CPP_SEMICOLON
; break;
1711 /* @ is a punctuator in Objective-C. */
1712 case '@': result
->type
= CPP_ATSIGN
; break;
1717 const uchar
*base
= --buffer
->cur
;
1718 struct normalize_state nst
= INITIAL_NORMALIZE_STATE
;
1720 if (forms_identifier_p (pfile
, true, &nst
))
1722 result
->type
= CPP_NAME
;
1723 result
->val
.node
.node
= lex_identifier (pfile
, base
, true, &nst
);
1724 warn_about_normalization (pfile
, result
, &nst
);
1731 create_literal (pfile
, result
, buffer
->cur
- 1, 1, CPP_OTHER
);
1738 /* An upper bound on the number of bytes needed to spell TOKEN.
1739 Does not include preceding whitespace. */
1741 cpp_token_len (const cpp_token
*token
)
1745 switch (TOKEN_SPELL (token
))
1747 default: len
= 6; break;
1748 case SPELL_LITERAL
: len
= token
->val
.str
.len
; break;
1749 case SPELL_IDENT
: len
= NODE_LEN (token
->val
.node
.node
) * 10; break;
1755 /* Parse UTF-8 out of NAMEP and place a \U escape in BUFFER.
1756 Return the number of bytes read out of NAME. (There are always
1757 10 bytes written to BUFFER.) */
1760 utf8_to_ucn (unsigned char *buffer
, const unsigned char *name
)
1766 unsigned long utf32
;
1768 /* Compute the length of the UTF-8 sequence. */
1769 for (t
= *name
; t
& 0x80; t
<<= 1)
1772 utf32
= *name
& (0x7F >> ucn_len
);
1773 for (ucn_len_c
= 1; ucn_len_c
< ucn_len
; ucn_len_c
++)
1775 utf32
= (utf32
<< 6) | (*++name
& 0x3F);
1777 /* Ill-formed UTF-8. */
1778 if ((*name
& ~0x3F) != 0x80)
1784 for (j
= 7; j
>= 0; j
--)
1785 *buffer
++ = "0123456789abcdef"[(utf32
>> (4 * j
)) & 0xF];
1789 /* Given a token TYPE corresponding to a digraph, return a pointer to
1790 the spelling of the digraph. */
1791 static const unsigned char *
1792 cpp_digraph2name (enum cpp_ttype type
)
1794 return digraph_spellings
[(int) type
- (int) CPP_FIRST_DIGRAPH
];
1797 /* Write the spelling of a token TOKEN to BUFFER. The buffer must
1798 already contain the enough space to hold the token's spelling.
1799 Returns a pointer to the character after the last character written.
1800 FORSTRING is true if this is to be the spelling after translation
1801 phase 1 (this is different for UCNs).
1802 FIXME: Would be nice if we didn't need the PFILE argument. */
1804 cpp_spell_token (cpp_reader
*pfile
, const cpp_token
*token
,
1805 unsigned char *buffer
, bool forstring
)
1807 switch (TOKEN_SPELL (token
))
1809 case SPELL_OPERATOR
:
1811 const unsigned char *spelling
;
1814 if (token
->flags
& DIGRAPH
)
1815 spelling
= cpp_digraph2name (token
->type
);
1816 else if (token
->flags
& NAMED_OP
)
1819 spelling
= TOKEN_NAME (token
);
1821 while ((c
= *spelling
++) != '\0')
1830 memcpy (buffer
, NODE_NAME (token
->val
.node
.node
),
1831 NODE_LEN (token
->val
.node
.node
));
1832 buffer
+= NODE_LEN (token
->val
.node
.node
);
1837 const unsigned char * name
= NODE_NAME (token
->val
.node
.node
);
1839 for (i
= 0; i
< NODE_LEN (token
->val
.node
.node
); i
++)
1840 if (name
[i
] & ~0x7F)
1842 i
+= utf8_to_ucn (buffer
, name
+ i
) - 1;
1846 *buffer
++ = NODE_NAME (token
->val
.node
.node
)[i
];
1851 memcpy (buffer
, token
->val
.str
.text
, token
->val
.str
.len
);
1852 buffer
+= token
->val
.str
.len
;
1856 cpp_error (pfile
, CPP_DL_ICE
,
1857 "unspellable token %s", TOKEN_NAME (token
));
1864 /* Returns TOKEN spelt as a null-terminated string. The string is
1865 freed when the reader is destroyed. Useful for diagnostics. */
1867 cpp_token_as_text (cpp_reader
*pfile
, const cpp_token
*token
)
1869 unsigned int len
= cpp_token_len (token
) + 1;
1870 unsigned char *start
= _cpp_unaligned_alloc (pfile
, len
), *end
;
1872 end
= cpp_spell_token (pfile
, token
, start
, false);
1878 /* Returns a pointer to a string which spells the token defined by
1879 TYPE and FLAGS. Used by C front ends, which really should move to
1880 using cpp_token_as_text. */
1882 cpp_type2name (enum cpp_ttype type
, unsigned char flags
)
1884 if (flags
& DIGRAPH
)
1885 return (const char *) cpp_digraph2name (type
);
1886 else if (flags
& NAMED_OP
)
1887 return cpp_named_operator2name (type
);
1889 return (const char *) token_spellings
[type
].name
;
1892 /* Writes the spelling of token to FP, without any preceding space.
1893 Separated from cpp_spell_token for efficiency - to avoid stdio
1894 double-buffering. */
1896 cpp_output_token (const cpp_token
*token
, FILE *fp
)
1898 switch (TOKEN_SPELL (token
))
1900 case SPELL_OPERATOR
:
1902 const unsigned char *spelling
;
1905 if (token
->flags
& DIGRAPH
)
1906 spelling
= cpp_digraph2name (token
->type
);
1907 else if (token
->flags
& NAMED_OP
)
1910 spelling
= TOKEN_NAME (token
);
1915 while ((c
= *++spelling
) != '\0');
1923 const unsigned char * name
= NODE_NAME (token
->val
.node
.node
);
1925 for (i
= 0; i
< NODE_LEN (token
->val
.node
.node
); i
++)
1926 if (name
[i
] & ~0x7F)
1928 unsigned char buffer
[10];
1929 i
+= utf8_to_ucn (buffer
, name
+ i
) - 1;
1930 fwrite (buffer
, 1, 10, fp
);
1933 fputc (NODE_NAME (token
->val
.node
.node
)[i
], fp
);
1938 fwrite (token
->val
.str
.text
, 1, token
->val
.str
.len
, fp
);
1942 /* An error, most probably. */
1947 /* Compare two tokens. */
1949 _cpp_equiv_tokens (const cpp_token
*a
, const cpp_token
*b
)
1951 if (a
->type
== b
->type
&& a
->flags
== b
->flags
)
1952 switch (TOKEN_SPELL (a
))
1954 default: /* Keep compiler happy. */
1955 case SPELL_OPERATOR
:
1956 /* token_no is used to track where multiple consecutive ##
1957 tokens were originally located. */
1958 return (a
->type
!= CPP_PASTE
|| a
->val
.token_no
== b
->val
.token_no
);
1960 return (a
->type
!= CPP_MACRO_ARG
1961 || a
->val
.macro_arg
.arg_no
== b
->val
.macro_arg
.arg_no
);
1963 return a
->val
.node
.node
== b
->val
.node
.node
;
1965 return (a
->val
.str
.len
== b
->val
.str
.len
1966 && !memcmp (a
->val
.str
.text
, b
->val
.str
.text
,
1973 /* Returns nonzero if a space should be inserted to avoid an
1974 accidental token paste for output. For simplicity, it is
1975 conservative, and occasionally advises a space where one is not
1976 needed, e.g. "." and ".2". */
1978 cpp_avoid_paste (cpp_reader
*pfile
, const cpp_token
*token1
,
1979 const cpp_token
*token2
)
1981 enum cpp_ttype a
= token1
->type
, b
= token2
->type
;
1984 if (token1
->flags
& NAMED_OP
)
1986 if (token2
->flags
& NAMED_OP
)
1990 if (token2
->flags
& DIGRAPH
)
1991 c
= digraph_spellings
[(int) b
- (int) CPP_FIRST_DIGRAPH
][0];
1992 else if (token_spellings
[b
].category
== SPELL_OPERATOR
)
1993 c
= token_spellings
[b
].name
[0];
1995 /* Quickly get everything that can paste with an '='. */
1996 if ((int) a
<= (int) CPP_LAST_EQ
&& c
== '=')
2001 case CPP_GREATER
: return c
== '>';
2002 case CPP_LESS
: return c
== '<' || c
== '%' || c
== ':';
2003 case CPP_PLUS
: return c
== '+';
2004 case CPP_MINUS
: return c
== '-' || c
== '>';
2005 case CPP_DIV
: return c
== '/' || c
== '*'; /* Comments. */
2006 case CPP_MOD
: return c
== ':' || c
== '>';
2007 case CPP_AND
: return c
== '&';
2008 case CPP_OR
: return c
== '|';
2009 case CPP_COLON
: return c
== ':' || c
== '>';
2010 case CPP_DEREF
: return c
== '*';
2011 case CPP_DOT
: return c
== '.' || c
== '%' || b
== CPP_NUMBER
;
2012 case CPP_HASH
: return c
== '#' || c
== '%'; /* Digraph form. */
2013 case CPP_NAME
: return ((b
== CPP_NUMBER
2014 && name_p (pfile
, &token2
->val
.str
))
2016 || b
== CPP_CHAR
|| b
== CPP_STRING
); /* L */
2017 case CPP_NUMBER
: return (b
== CPP_NUMBER
|| b
== CPP_NAME
2018 || c
== '.' || c
== '+' || c
== '-');
2020 case CPP_OTHER
: return ((token1
->val
.str
.text
[0] == '\\'
2022 || (CPP_OPTION (pfile
, objc
)
2023 && token1
->val
.str
.text
[0] == '@'
2024 && (b
== CPP_NAME
|| b
== CPP_STRING
)));
2031 /* Output all the remaining tokens on the current line, and a newline
2032 character, to FP. Leading whitespace is removed. If there are
2033 macros, special token padding is not performed. */
2035 cpp_output_line (cpp_reader
*pfile
, FILE *fp
)
2037 const cpp_token
*token
;
2039 token
= cpp_get_token (pfile
);
2040 while (token
->type
!= CPP_EOF
)
2042 cpp_output_token (token
, fp
);
2043 token
= cpp_get_token (pfile
);
2044 if (token
->flags
& PREV_WHITE
)
2051 /* Return a string representation of all the remaining tokens on the
2052 current line. The result is allocated using xmalloc and must be
2053 freed by the caller. */
2055 cpp_output_line_to_string (cpp_reader
*pfile
, const unsigned char *dir_name
)
2057 const cpp_token
*token
;
2058 unsigned int out
= dir_name
? ustrlen (dir_name
) : 0;
2059 unsigned int alloced
= 120 + out
;
2060 unsigned char *result
= (unsigned char *) xmalloc (alloced
);
2062 /* If DIR_NAME is empty, there are no initial contents. */
2065 sprintf ((char *) result
, "#%s ", dir_name
);
2069 token
= cpp_get_token (pfile
);
2070 while (token
->type
!= CPP_EOF
)
2072 unsigned char *last
;
2073 /* Include room for a possible space and the terminating nul. */
2074 unsigned int len
= cpp_token_len (token
) + 2;
2076 if (out
+ len
> alloced
)
2079 if (out
+ len
> alloced
)
2080 alloced
= out
+ len
;
2081 result
= (unsigned char *) xrealloc (result
, alloced
);
2084 last
= cpp_spell_token (pfile
, token
, &result
[out
], 0);
2085 out
= last
- result
;
2087 token
= cpp_get_token (pfile
);
2088 if (token
->flags
& PREV_WHITE
)
2089 result
[out
++] = ' ';
2096 /* Memory buffers. Changing these three constants can have a dramatic
2097 effect on performance. The values here are reasonable defaults,
2098 but might be tuned. If you adjust them, be sure to test across a
2099 range of uses of cpplib, including heavy nested function-like macro
2100 expansion. Also check the change in peak memory usage (NJAMD is a
2101 good tool for this). */
2102 #define MIN_BUFF_SIZE 8000
2103 #define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2)
2104 #define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \
2105 (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2)
2107 #if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0)
2108 #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
2111 /* Create a new allocation buffer. Place the control block at the end
2112 of the buffer, so that buffer overflows will cause immediate chaos. */
2114 new_buff (size_t len
)
2117 unsigned char *base
;
2119 if (len
< MIN_BUFF_SIZE
)
2120 len
= MIN_BUFF_SIZE
;
2121 len
= CPP_ALIGN (len
);
2123 base
= XNEWVEC (unsigned char, len
+ sizeof (_cpp_buff
));
2124 result
= (_cpp_buff
*) (base
+ len
);
2125 result
->base
= base
;
2127 result
->limit
= base
+ len
;
2128 result
->next
= NULL
;
2132 /* Place a chain of unwanted allocation buffers on the free list. */
2134 _cpp_release_buff (cpp_reader
*pfile
, _cpp_buff
*buff
)
2136 _cpp_buff
*end
= buff
;
2140 end
->next
= pfile
->free_buffs
;
2141 pfile
->free_buffs
= buff
;
2144 /* Return a free buffer of size at least MIN_SIZE. */
2146 _cpp_get_buff (cpp_reader
*pfile
, size_t min_size
)
2148 _cpp_buff
*result
, **p
;
2150 for (p
= &pfile
->free_buffs
;; p
= &(*p
)->next
)
2155 return new_buff (min_size
);
2157 size
= result
->limit
- result
->base
;
2158 /* Return a buffer that's big enough, but don't waste one that's
2160 if (size
>= min_size
&& size
<= BUFF_SIZE_UPPER_BOUND (min_size
))
2165 result
->next
= NULL
;
2166 result
->cur
= result
->base
;
2170 /* Creates a new buffer with enough space to hold the uncommitted
2171 remaining bytes of BUFF, and at least MIN_EXTRA more bytes. Copies
2172 the excess bytes to the new buffer. Chains the new buffer after
2173 BUFF, and returns the new buffer. */
2175 _cpp_append_extend_buff (cpp_reader
*pfile
, _cpp_buff
*buff
, size_t min_extra
)
2177 size_t size
= EXTENDED_BUFF_SIZE (buff
, min_extra
);
2178 _cpp_buff
*new_buff
= _cpp_get_buff (pfile
, size
);
2180 buff
->next
= new_buff
;
2181 memcpy (new_buff
->base
, buff
->cur
, BUFF_ROOM (buff
));
2185 /* Creates a new buffer with enough space to hold the uncommitted
2186 remaining bytes of the buffer pointed to by BUFF, and at least
2187 MIN_EXTRA more bytes. Copies the excess bytes to the new buffer.
2188 Chains the new buffer before the buffer pointed to by BUFF, and
2189 updates the pointer to point to the new buffer. */
2191 _cpp_extend_buff (cpp_reader
*pfile
, _cpp_buff
**pbuff
, size_t min_extra
)
2193 _cpp_buff
*new_buff
, *old_buff
= *pbuff
;
2194 size_t size
= EXTENDED_BUFF_SIZE (old_buff
, min_extra
);
2196 new_buff
= _cpp_get_buff (pfile
, size
);
2197 memcpy (new_buff
->base
, old_buff
->cur
, BUFF_ROOM (old_buff
));
2198 new_buff
->next
= old_buff
;
2202 /* Free a chain of buffers starting at BUFF. */
2204 _cpp_free_buff (_cpp_buff
*buff
)
2208 for (; buff
; buff
= next
)
2215 /* Allocate permanent, unaligned storage of length LEN. */
2217 _cpp_unaligned_alloc (cpp_reader
*pfile
, size_t len
)
2219 _cpp_buff
*buff
= pfile
->u_buff
;
2220 unsigned char *result
= buff
->cur
;
2222 if (len
> (size_t) (buff
->limit
- result
))
2224 buff
= _cpp_get_buff (pfile
, len
);
2225 buff
->next
= pfile
->u_buff
;
2226 pfile
->u_buff
= buff
;
2230 buff
->cur
= result
+ len
;
2234 /* Allocate permanent, unaligned storage of length LEN from a_buff.
2235 That buffer is used for growing allocations when saving macro
2236 replacement lists in a #define, and when parsing an answer to an
2237 assertion in #assert, #unassert or #if (and therefore possibly
2238 whilst expanding macros). It therefore must not be used by any
2239 code that they might call: specifically the lexer and the guts of
2242 All existing other uses clearly fit this restriction: storing
2243 registered pragmas during initialization. */
2245 _cpp_aligned_alloc (cpp_reader
*pfile
, size_t len
)
2247 _cpp_buff
*buff
= pfile
->a_buff
;
2248 unsigned char *result
= buff
->cur
;
2250 if (len
> (size_t) (buff
->limit
- result
))
2252 buff
= _cpp_get_buff (pfile
, len
);
2253 buff
->next
= pfile
->a_buff
;
2254 pfile
->a_buff
= buff
;
2258 buff
->cur
= result
+ len
;
2262 /* Say which field of TOK is in use. */
2264 enum cpp_token_fld_kind
2265 cpp_token_val_index (cpp_token
*tok
)
2267 switch (TOKEN_SPELL (tok
))
2270 return CPP_TOKEN_FLD_NODE
;
2272 return CPP_TOKEN_FLD_STR
;
2273 case SPELL_OPERATOR
:
2274 if (tok
->type
== CPP_PASTE
)
2275 return CPP_TOKEN_FLD_TOKEN_NO
;
2277 return CPP_TOKEN_FLD_NONE
;
2279 if (tok
->type
== CPP_MACRO_ARG
)
2280 return CPP_TOKEN_FLD_ARG_NO
;
2281 else if (tok
->type
== CPP_PADDING
)
2282 return CPP_TOKEN_FLD_SOURCE
;
2283 else if (tok
->type
== CPP_PRAGMA
)
2284 return CPP_TOKEN_FLD_PRAGMA
;
2285 /* else fall through */
2287 return CPP_TOKEN_FLD_NONE
;