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_error_with_line (pfile
, CPP_DL_WARNING
, pfile
->line_table
->highest_line
, col
,
305 "trigraph ??%c converted to %c",
307 (int) _cpp_trigraph_map
[note
->type
]);
311 (pfile
, CPP_DL_WARNING
, pfile
->line_table
->highest_line
, col
,
312 "trigraph ??%c ignored, use -trigraphs to enable",
317 else if (note
->type
== 0)
318 /* Already processed in lex_raw_string. */;
324 /* Skip a C-style block comment. We find the end of the comment by
325 seeing if an asterisk is before every '/' we encounter. Returns
326 nonzero if comment terminated by EOF, zero otherwise.
328 Buffer->cur points to the initial asterisk of the comment. */
330 _cpp_skip_block_comment (cpp_reader
*pfile
)
332 cpp_buffer
*buffer
= pfile
->buffer
;
333 const uchar
*cur
= buffer
->cur
;
342 /* People like decorating comments with '*', so check for '/'
343 instead for efficiency. */
351 /* Warn about potential nested comments, but not if the '/'
352 comes immediately before the true comment delimiter.
353 Don't bother to get it right across escaped newlines. */
354 if (CPP_OPTION (pfile
, warn_comments
)
355 && cur
[0] == '*' && cur
[1] != '/')
358 cpp_error_with_line (pfile
, CPP_DL_WARNING
,
359 pfile
->line_table
->highest_line
, CPP_BUF_COL (buffer
),
360 "\"/*\" within comment");
366 buffer
->cur
= cur
- 1;
367 _cpp_process_line_notes (pfile
, true);
368 if (buffer
->next_line
>= buffer
->rlimit
)
370 _cpp_clean_line (pfile
);
372 cols
= buffer
->next_line
- buffer
->line_base
;
373 CPP_INCREMENT_LINE (pfile
, cols
);
380 _cpp_process_line_notes (pfile
, true);
384 /* Skip a C++ line comment, leaving buffer->cur pointing to the
385 terminating newline. Handles escaped newlines. Returns nonzero
386 if a multiline comment. */
388 skip_line_comment (cpp_reader
*pfile
)
390 cpp_buffer
*buffer
= pfile
->buffer
;
391 source_location orig_line
= pfile
->line_table
->highest_line
;
393 while (*buffer
->cur
!= '\n')
396 _cpp_process_line_notes (pfile
, true);
397 return orig_line
!= pfile
->line_table
->highest_line
;
400 /* Skips whitespace, saving the next non-whitespace character. */
402 skip_whitespace (cpp_reader
*pfile
, cppchar_t c
)
404 cpp_buffer
*buffer
= pfile
->buffer
;
405 bool saw_NUL
= false;
409 /* Horizontal space always OK. */
410 if (c
== ' ' || c
== '\t')
412 /* Just \f \v or \0 left. */
415 else if (pfile
->state
.in_directive
&& CPP_PEDANTIC (pfile
))
416 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, pfile
->line_table
->highest_line
,
417 CPP_BUF_COL (buffer
),
418 "%s in preprocessing directive",
419 c
== '\f' ? "form feed" : "vertical tab");
423 /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
424 while (is_nvspace (c
));
427 cpp_error (pfile
, CPP_DL_WARNING
, "null character(s) ignored");
432 /* See if the characters of a number token are valid in a name (no
435 name_p (cpp_reader
*pfile
, const cpp_string
*string
)
439 for (i
= 0; i
< string
->len
; i
++)
440 if (!is_idchar (string
->text
[i
]))
446 /* After parsing an identifier or other sequence, produce a warning about
447 sequences not in NFC/NFKC. */
449 warn_about_normalization (cpp_reader
*pfile
,
450 const cpp_token
*token
,
451 const struct normalize_state
*s
)
453 if (CPP_OPTION (pfile
, warn_normalize
) < NORMALIZE_STATE_RESULT (s
)
454 && !pfile
->state
.skipping
)
456 /* Make sure that the token is printed using UCNs, even
457 if we'd otherwise happily print UTF-8. */
458 unsigned char *buf
= XNEWVEC (unsigned char, cpp_token_len (token
));
461 sz
= cpp_spell_token (pfile
, token
, buf
, false) - buf
;
462 if (NORMALIZE_STATE_RESULT (s
) == normalized_C
)
463 cpp_error_with_line (pfile
, CPP_DL_WARNING
, token
->src_loc
, 0,
464 "`%.*s' is not in NFKC", (int) sz
, buf
);
466 cpp_error_with_line (pfile
, CPP_DL_WARNING
, token
->src_loc
, 0,
467 "`%.*s' is not in NFC", (int) sz
, buf
);
471 /* Returns TRUE if the sequence starting at buffer->cur is invalid in
472 an identifier. FIRST is TRUE if this starts an identifier. */
474 forms_identifier_p (cpp_reader
*pfile
, int first
,
475 struct normalize_state
*state
)
477 cpp_buffer
*buffer
= pfile
->buffer
;
479 if (*buffer
->cur
== '$')
481 if (!CPP_OPTION (pfile
, dollars_in_ident
))
485 if (CPP_OPTION (pfile
, warn_dollars
) && !pfile
->state
.skipping
)
487 CPP_OPTION (pfile
, warn_dollars
) = 0;
488 cpp_error (pfile
, CPP_DL_PEDWARN
, "'$' in identifier or number");
494 /* Is this a syntactically valid UCN? */
495 if (CPP_OPTION (pfile
, extended_identifiers
)
496 && *buffer
->cur
== '\\'
497 && (buffer
->cur
[1] == 'u' || buffer
->cur
[1] == 'U'))
500 if (_cpp_valid_ucn (pfile
, &buffer
->cur
, buffer
->rlimit
, 1 + !first
,
509 /* Helper function to get the cpp_hashnode of the identifier BASE. */
510 static cpp_hashnode
*
511 lex_identifier_intern (cpp_reader
*pfile
, const uchar
*base
)
513 cpp_hashnode
*result
;
516 unsigned int hash
= HT_HASHSTEP (0, *base
);
519 while (ISIDNUM (*cur
))
521 hash
= HT_HASHSTEP (hash
, *cur
);
525 hash
= HT_HASHFINISH (hash
, len
);
526 result
= CPP_HASHNODE (ht_lookup_with_hash (pfile
->hash_table
,
527 base
, len
, hash
, HT_ALLOC
));
529 /* Rarely, identifiers require diagnostics when lexed. */
530 if (__builtin_expect ((result
->flags
& NODE_DIAGNOSTIC
)
531 && !pfile
->state
.skipping
, 0))
533 /* It is allowed to poison the same identifier twice. */
534 if ((result
->flags
& NODE_POISONED
) && !pfile
->state
.poisoned_ok
)
535 cpp_error (pfile
, CPP_DL_ERROR
, "attempt to use poisoned \"%s\"",
538 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
539 replacement list of a variadic macro. */
540 if (result
== pfile
->spec_nodes
.n__VA_ARGS__
541 && !pfile
->state
.va_args_ok
)
542 cpp_error (pfile
, CPP_DL_PEDWARN
,
543 "__VA_ARGS__ can only appear in the expansion"
544 " of a C99 variadic macro");
546 /* For -Wc++-compat, warn about use of C++ named operators. */
547 if (result
->flags
& NODE_WARN_OPERATOR
)
548 cpp_error (pfile
, CPP_DL_WARNING
,
549 "identifier \"%s\" is a special operator name in C++",
556 /* Get the cpp_hashnode of an identifier specified by NAME in
557 the current cpp_reader object. If none is found, NULL is returned. */
559 _cpp_lex_identifier (cpp_reader
*pfile
, const char *name
)
561 cpp_hashnode
*result
;
562 result
= lex_identifier_intern (pfile
, (uchar
*) name
);
566 /* Lex an identifier starting at BUFFER->CUR - 1. */
567 static cpp_hashnode
*
568 lex_identifier (cpp_reader
*pfile
, const uchar
*base
, bool starts_ucn
,
569 struct normalize_state
*nst
)
571 cpp_hashnode
*result
;
574 unsigned int hash
= HT_HASHSTEP (0, *base
);
576 cur
= pfile
->buffer
->cur
;
578 while (ISIDNUM (*cur
))
580 hash
= HT_HASHSTEP (hash
, *cur
);
583 pfile
->buffer
->cur
= cur
;
584 if (starts_ucn
|| forms_identifier_p (pfile
, false, nst
))
586 /* Slower version for identifiers containing UCNs (or $). */
588 while (ISIDNUM (*pfile
->buffer
->cur
))
590 pfile
->buffer
->cur
++;
591 NORMALIZE_STATE_UPDATE_IDNUM (nst
);
593 } while (forms_identifier_p (pfile
, false, nst
));
594 result
= _cpp_interpret_identifier (pfile
, base
,
595 pfile
->buffer
->cur
- base
);
600 hash
= HT_HASHFINISH (hash
, len
);
602 result
= CPP_HASHNODE (ht_lookup_with_hash (pfile
->hash_table
,
603 base
, len
, hash
, HT_ALLOC
));
606 /* Rarely, identifiers require diagnostics when lexed. */
607 if (__builtin_expect ((result
->flags
& NODE_DIAGNOSTIC
)
608 && !pfile
->state
.skipping
, 0))
610 /* It is allowed to poison the same identifier twice. */
611 if ((result
->flags
& NODE_POISONED
) && !pfile
->state
.poisoned_ok
)
612 cpp_error (pfile
, CPP_DL_ERROR
, "attempt to use poisoned \"%s\"",
615 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
616 replacement list of a variadic macro. */
617 if (result
== pfile
->spec_nodes
.n__VA_ARGS__
618 && !pfile
->state
.va_args_ok
)
619 cpp_error (pfile
, CPP_DL_PEDWARN
,
620 "__VA_ARGS__ can only appear in the expansion"
621 " of a C99 variadic macro");
623 /* For -Wc++-compat, warn about use of C++ named operators. */
624 if (result
->flags
& NODE_WARN_OPERATOR
)
625 cpp_error (pfile
, CPP_DL_WARNING
,
626 "identifier \"%s\" is a special operator name in C++",
633 /* Lex a number to NUMBER starting at BUFFER->CUR - 1. */
635 lex_number (cpp_reader
*pfile
, cpp_string
*number
,
636 struct normalize_state
*nst
)
642 base
= pfile
->buffer
->cur
- 1;
645 cur
= pfile
->buffer
->cur
;
647 /* N.B. ISIDNUM does not include $. */
648 while (ISIDNUM (*cur
) || *cur
== '.' || VALID_SIGN (*cur
, cur
[-1]))
651 NORMALIZE_STATE_UPDATE_IDNUM (nst
);
654 pfile
->buffer
->cur
= cur
;
656 while (forms_identifier_p (pfile
, false, nst
));
658 number
->len
= cur
- base
;
659 dest
= _cpp_unaligned_alloc (pfile
, number
->len
+ 1);
660 memcpy (dest
, base
, number
->len
);
661 dest
[number
->len
] = '\0';
665 /* Create a token of type TYPE with a literal spelling. */
667 create_literal (cpp_reader
*pfile
, cpp_token
*token
, const uchar
*base
,
668 unsigned int len
, enum cpp_ttype type
)
670 uchar
*dest
= _cpp_unaligned_alloc (pfile
, len
+ 1);
672 memcpy (dest
, base
, len
);
675 token
->val
.str
.len
= len
;
676 token
->val
.str
.text
= dest
;
679 /* Subroutine of lex_raw_string: Append LEN chars from BASE to the buffer
680 sequence from *FIRST_BUFF_P to LAST_BUFF_P. */
683 bufring_append (cpp_reader
*pfile
, const uchar
*base
, size_t len
,
684 _cpp_buff
**first_buff_p
, _cpp_buff
**last_buff_p
)
686 _cpp_buff
*first_buff
= *first_buff_p
;
687 _cpp_buff
*last_buff
= *last_buff_p
;
689 if (first_buff
== NULL
)
690 first_buff
= last_buff
= _cpp_get_buff (pfile
, len
);
691 else if (len
> BUFF_ROOM (last_buff
))
693 size_t room
= BUFF_ROOM (last_buff
);
694 memcpy (BUFF_FRONT (last_buff
), base
, room
);
695 BUFF_FRONT (last_buff
) += room
;
698 last_buff
= _cpp_append_extend_buff (pfile
, last_buff
, len
);
701 memcpy (BUFF_FRONT (last_buff
), base
, len
);
702 BUFF_FRONT (last_buff
) += len
;
704 *first_buff_p
= first_buff
;
705 *last_buff_p
= last_buff
;
708 /* Lexes a raw string. The stored string contains the spelling, including
709 double quotes, delimiter string, '(' and ')', any leading
710 'L', 'u', 'U' or 'u8' and 'R' modifier. It returns the type of the
711 literal, or CPP_OTHER if it was not properly terminated.
713 The spelling is NUL-terminated, but it is not guaranteed that this
714 is the first NUL since embedded NULs are preserved. */
717 lex_raw_string (cpp_reader
*pfile
, cpp_token
*token
, const uchar
*base
,
720 source_location saw_NUL
= 0;
721 const uchar
*raw_prefix
;
722 unsigned int raw_prefix_len
= 0;
724 size_t total_len
= 0;
725 _cpp_buff
*first_buff
= NULL
, *last_buff
= NULL
;
726 _cpp_line_note
*note
= &pfile
->buffer
->notes
[pfile
->buffer
->cur_note
];
728 type
= (*base
== 'L' ? CPP_WSTRING
:
729 *base
== 'U' ? CPP_STRING32
:
730 *base
== 'u' ? (base
[1] == '8' ? CPP_UTF8STRING
: CPP_STRING16
)
733 raw_prefix
= cur
+ 1;
734 while (raw_prefix_len
< 16)
736 switch (raw_prefix
[raw_prefix_len
])
738 case ' ': case '(': case ')': case '\\': case '\t':
739 case '\v': case '\f': case '\n': default:
741 /* Basic source charset except the above chars. */
742 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
743 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
744 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
745 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
747 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
748 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
749 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
750 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
752 case '0': case '1': case '2': case '3': case '4': case '5':
753 case '6': case '7': case '8': case '9':
754 case '_': case '{': case '}': case '#': case '[': case ']':
755 case '<': case '>': case '%': case ':': case ';': case '.':
756 case '?': case '*': case '+': case '-': case '/': case '^':
757 case '&': case '|': case '~': case '!': case '=': case ',':
765 if (raw_prefix
[raw_prefix_len
] != '(')
767 int col
= CPP_BUF_COLUMN (pfile
->buffer
, raw_prefix
+ raw_prefix_len
)
769 if (raw_prefix_len
== 16)
770 cpp_error_with_line (pfile
, CPP_DL_ERROR
, token
->src_loc
, col
,
771 "raw string delimiter longer than 16 characters");
773 cpp_error_with_line (pfile
, CPP_DL_ERROR
, token
->src_loc
, col
,
774 "invalid character '%c' in raw string delimiter",
775 (int) raw_prefix
[raw_prefix_len
]);
776 pfile
->buffer
->cur
= raw_prefix
- 1;
777 create_literal (pfile
, token
, base
, raw_prefix
- 1 - base
, CPP_OTHER
);
781 cur
= raw_prefix
+ raw_prefix_len
+ 1;
784 #define BUF_APPEND(STR,LEN) \
786 bufring_append (pfile, (const uchar *)(STR), (LEN), \
787 &first_buff, &last_buff); \
788 total_len += (LEN); \
793 /* If we previously performed any trigraph or line splicing
794 transformations, undo them within the body of the raw string. */
795 while (note
->pos
< cur
)
797 for (; note
->pos
== cur
; ++note
)
803 /* Restore backslash followed by newline. */
804 BUF_APPEND (base
, cur
- base
);
806 BUF_APPEND ("\\", 1);
808 if (note
->type
== ' ')
810 /* GNU backslash whitespace newline extension. FIXME
811 could be any sequence of non-vertical space. When we
812 can properly restore any such sequence, we should mark
813 this note as handled so _cpp_process_line_notes
818 BUF_APPEND ("\n", 1);
822 /* Already handled. */
826 if (_cpp_trigraph_map
[note
->type
])
828 /* Don't warn about this trigraph in
829 _cpp_process_line_notes, since trigraphs show up as
830 trigraphs in raw strings. */
831 uchar type
= note
->type
;
834 if (!CPP_OPTION (pfile
, trigraphs
))
835 /* If we didn't convert the trigraph in the first
836 place, don't do anything now either. */
839 BUF_APPEND (base
, cur
- base
);
841 BUF_APPEND ("??", 2);
843 /* ??/ followed by newline gets two line notes, one for
844 the trigraph and one for the backslash/newline. */
845 if (type
== '/' && note
[1].pos
== cur
)
847 if (note
[1].type
!= '\\'
848 && note
[1].type
!= ' ')
852 goto after_backslash
;
854 /* The ) from ??) could be part of the suffix. */
856 && strncmp ((const char *) cur
+1,
857 (const char *) raw_prefix
,
859 && cur
[raw_prefix_len
+1] == '"')
861 cur
+= raw_prefix_len
+2;
862 goto break_outer_loop
;
866 /* Skip the replacement character. */
868 BUF_APPEND (&type
, 1);
879 && strncmp ((const char *) cur
, (const char *) raw_prefix
,
881 && cur
[raw_prefix_len
] == '"')
883 cur
+= raw_prefix_len
+ 1;
888 if (pfile
->state
.in_directive
889 || pfile
->state
.parsing_args
890 || pfile
->state
.in_deferred_pragma
)
894 cpp_error_with_line (pfile
, CPP_DL_ERROR
, token
->src_loc
, 0,
895 "unterminated raw string");
899 BUF_APPEND (base
, cur
- base
);
901 if (pfile
->buffer
->cur
< pfile
->buffer
->rlimit
)
902 CPP_INCREMENT_LINE (pfile
, 0);
903 pfile
->buffer
->need_line
= true;
905 pfile
->buffer
->cur
= cur
-1;
906 _cpp_process_line_notes (pfile
, false);
907 if (!_cpp_get_fresh_line (pfile
))
909 source_location src_loc
= token
->src_loc
;
910 token
->type
= CPP_EOF
;
911 /* Tell the compiler the line number of the EOF token. */
912 token
->src_loc
= pfile
->line_table
->highest_line
;
914 if (first_buff
!= NULL
)
915 _cpp_release_buff (pfile
, first_buff
);
916 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
917 "unterminated raw string");
921 cur
= base
= pfile
->buffer
->cur
;
922 note
= &pfile
->buffer
->notes
[pfile
->buffer
->cur_note
];
924 else if (c
== '\0' && !saw_NUL
)
925 LINEMAP_POSITION_FOR_COLUMN (saw_NUL
, pfile
->line_table
,
926 CPP_BUF_COLUMN (pfile
->buffer
, cur
));
930 if (saw_NUL
&& !pfile
->state
.skipping
)
931 cpp_error_with_line (pfile
, CPP_DL_WARNING
, saw_NUL
, 0,
932 "null character(s) preserved in literal");
934 pfile
->buffer
->cur
= cur
;
935 if (first_buff
== NULL
)
936 create_literal (pfile
, token
, base
, cur
- base
, type
);
939 uchar
*dest
= _cpp_unaligned_alloc (pfile
, total_len
+ (cur
- base
) + 1);
942 token
->val
.str
.len
= total_len
+ (cur
- base
);
943 token
->val
.str
.text
= dest
;
944 last_buff
= first_buff
;
945 while (last_buff
!= NULL
)
947 memcpy (dest
, last_buff
->base
,
948 BUFF_FRONT (last_buff
) - last_buff
->base
);
949 dest
+= BUFF_FRONT (last_buff
) - last_buff
->base
;
950 last_buff
= last_buff
->next
;
952 _cpp_release_buff (pfile
, first_buff
);
953 memcpy (dest
, base
, cur
- base
);
954 dest
[cur
- base
] = '\0';
958 /* Lexes a string, character constant, or angle-bracketed header file
959 name. The stored string contains the spelling, including opening
960 quote and any leading 'L', 'u', 'U' or 'u8' and optional
961 'R' modifier. It returns the type of the literal, or CPP_OTHER
962 if it was not properly terminated, or CPP_LESS for an unterminated
963 header name which must be relexed as normal tokens.
965 The spelling is NUL-terminated, but it is not guaranteed that this
966 is the first NUL since embedded NULs are preserved. */
968 lex_string (cpp_reader
*pfile
, cpp_token
*token
, const uchar
*base
)
970 bool saw_NUL
= false;
972 cppchar_t terminator
;
977 if (terminator
== 'L' || terminator
== 'U')
979 else if (terminator
== 'u')
982 if (terminator
== '8')
985 if (terminator
== 'R')
987 lex_raw_string (pfile
, token
, base
, cur
);
990 if (terminator
== '"')
991 type
= (*base
== 'L' ? CPP_WSTRING
:
992 *base
== 'U' ? CPP_STRING32
:
993 *base
== 'u' ? (base
[1] == '8' ? CPP_UTF8STRING
: CPP_STRING16
)
995 else if (terminator
== '\'')
996 type
= (*base
== 'L' ? CPP_WCHAR
:
997 *base
== 'U' ? CPP_CHAR32
:
998 *base
== 'u' ? CPP_CHAR16
: CPP_CHAR
);
1000 terminator
= '>', type
= CPP_HEADER_NAME
;
1004 cppchar_t c
= *cur
++;
1006 /* In #include-style directives, terminators are not escapable. */
1007 if (c
== '\\' && !pfile
->state
.angled_headers
&& *cur
!= '\n')
1009 else if (c
== terminator
)
1014 /* Unmatched quotes always yield undefined behavior, but
1015 greedy lexing means that what appears to be an unterminated
1016 header name may actually be a legitimate sequence of tokens. */
1017 if (terminator
== '>')
1019 token
->type
= CPP_LESS
;
1029 if (saw_NUL
&& !pfile
->state
.skipping
)
1030 cpp_error (pfile
, CPP_DL_WARNING
,
1031 "null character(s) preserved in literal");
1033 if (type
== CPP_OTHER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
1034 cpp_error (pfile
, CPP_DL_PEDWARN
, "missing terminating %c character",
1037 pfile
->buffer
->cur
= cur
;
1038 create_literal (pfile
, token
, base
, cur
- base
, type
);
1041 /* Return the comment table. The client may not make any assumption
1042 about the ordering of the table. */
1044 cpp_get_comments (cpp_reader
*pfile
)
1046 return &pfile
->comments
;
1049 /* Append a comment to the end of the comment table. */
1051 store_comment (cpp_reader
*pfile
, cpp_token
*token
)
1055 if (pfile
->comments
.allocated
== 0)
1057 pfile
->comments
.allocated
= 256;
1058 pfile
->comments
.entries
= (cpp_comment
*) xmalloc
1059 (pfile
->comments
.allocated
* sizeof (cpp_comment
));
1062 if (pfile
->comments
.count
== pfile
->comments
.allocated
)
1064 pfile
->comments
.allocated
*= 2;
1065 pfile
->comments
.entries
= (cpp_comment
*) xrealloc
1066 (pfile
->comments
.entries
,
1067 pfile
->comments
.allocated
* sizeof (cpp_comment
));
1070 len
= token
->val
.str
.len
;
1072 /* Copy comment. Note, token may not be NULL terminated. */
1073 pfile
->comments
.entries
[pfile
->comments
.count
].comment
=
1074 (char *) xmalloc (sizeof (char) * (len
+ 1));
1075 memcpy (pfile
->comments
.entries
[pfile
->comments
.count
].comment
,
1076 token
->val
.str
.text
, len
);
1077 pfile
->comments
.entries
[pfile
->comments
.count
].comment
[len
] = '\0';
1079 /* Set source location. */
1080 pfile
->comments
.entries
[pfile
->comments
.count
].sloc
= token
->src_loc
;
1082 /* Increment the count of entries in the comment table. */
1083 pfile
->comments
.count
++;
1086 /* The stored comment includes the comment start and any terminator. */
1088 save_comment (cpp_reader
*pfile
, cpp_token
*token
, const unsigned char *from
,
1091 unsigned char *buffer
;
1092 unsigned int len
, clen
;
1094 len
= pfile
->buffer
->cur
- from
+ 1; /* + 1 for the initial '/'. */
1096 /* C++ comments probably (not definitely) have moved past a new
1097 line, which we don't want to save in the comment. */
1098 if (is_vspace (pfile
->buffer
->cur
[-1]))
1101 /* If we are currently in a directive, then we need to store all
1102 C++ comments as C comments internally, and so we need to
1103 allocate a little extra space in that case.
1105 Note that the only time we encounter a directive here is
1106 when we are saving comments in a "#define". */
1107 clen
= (pfile
->state
.in_directive
&& type
== '/') ? len
+ 2 : len
;
1109 buffer
= _cpp_unaligned_alloc (pfile
, clen
);
1111 token
->type
= CPP_COMMENT
;
1112 token
->val
.str
.len
= clen
;
1113 token
->val
.str
.text
= buffer
;
1116 memcpy (buffer
+ 1, from
, len
- 1);
1118 /* Finish conversion to a C comment, if necessary. */
1119 if (pfile
->state
.in_directive
&& type
== '/')
1122 buffer
[clen
- 2] = '*';
1123 buffer
[clen
- 1] = '/';
1126 /* Finally store this comment for use by clients of libcpp. */
1127 store_comment (pfile
, token
);
1130 /* Allocate COUNT tokens for RUN. */
1132 _cpp_init_tokenrun (tokenrun
*run
, unsigned int count
)
1134 run
->base
= XNEWVEC (cpp_token
, count
);
1135 run
->limit
= run
->base
+ count
;
1139 /* Returns the next tokenrun, or creates one if there is none. */
1141 next_tokenrun (tokenrun
*run
)
1143 if (run
->next
== NULL
)
1145 run
->next
= XNEW (tokenrun
);
1146 run
->next
->prev
= run
;
1147 _cpp_init_tokenrun (run
->next
, 250);
1153 /* Look ahead in the input stream. */
1155 cpp_peek_token (cpp_reader
*pfile
, int index
)
1157 cpp_context
*context
= pfile
->context
;
1158 const cpp_token
*peektok
;
1161 /* First, scan through any pending cpp_context objects. */
1162 while (context
->prev
)
1164 ptrdiff_t sz
= (context
->direct_p
1165 ? LAST (context
).token
- FIRST (context
).token
1166 : LAST (context
).ptoken
- FIRST (context
).ptoken
);
1168 if (index
< (int) sz
)
1169 return (context
->direct_p
1170 ? FIRST (context
).token
+ index
1171 : *(FIRST (context
).ptoken
+ index
));
1174 context
= context
->prev
;
1177 /* We will have to read some new tokens after all (and do so
1178 without invalidating preceding tokens). */
1180 pfile
->keep_tokens
++;
1184 peektok
= _cpp_lex_token (pfile
);
1185 if (peektok
->type
== CPP_EOF
)
1190 _cpp_backup_tokens_direct (pfile
, count
+ 1);
1191 pfile
->keep_tokens
--;
1196 /* Allocate a single token that is invalidated at the same time as the
1197 rest of the tokens on the line. Has its line and col set to the
1198 same as the last lexed token, so that diagnostics appear in the
1201 _cpp_temp_token (cpp_reader
*pfile
)
1203 cpp_token
*old
, *result
;
1204 ptrdiff_t sz
= pfile
->cur_run
->limit
- pfile
->cur_token
;
1205 ptrdiff_t la
= (ptrdiff_t) pfile
->lookaheads
;
1207 old
= pfile
->cur_token
- 1;
1208 /* Any pre-existing lookaheads must not be clobbered. */
1213 tokenrun
*next
= next_tokenrun (pfile
->cur_run
);
1216 memmove (next
->base
+ 1, next
->base
,
1217 (la
- sz
) * sizeof (cpp_token
));
1219 next
->base
[0] = pfile
->cur_run
->limit
[-1];
1223 memmove (pfile
->cur_token
+ 1, pfile
->cur_token
,
1224 MIN (la
, sz
- 1) * sizeof (cpp_token
));
1227 if (!sz
&& pfile
->cur_token
== pfile
->cur_run
->limit
)
1229 pfile
->cur_run
= next_tokenrun (pfile
->cur_run
);
1230 pfile
->cur_token
= pfile
->cur_run
->base
;
1233 result
= pfile
->cur_token
++;
1234 result
->src_loc
= old
->src_loc
;
1238 /* Lex a token into RESULT (external interface). Takes care of issues
1239 like directive handling, token lookahead, multiple include
1240 optimization and skipping. */
1242 _cpp_lex_token (cpp_reader
*pfile
)
1248 if (pfile
->cur_token
== pfile
->cur_run
->limit
)
1250 pfile
->cur_run
= next_tokenrun (pfile
->cur_run
);
1251 pfile
->cur_token
= pfile
->cur_run
->base
;
1253 /* We assume that the current token is somewhere in the current
1255 if (pfile
->cur_token
< pfile
->cur_run
->base
1256 || pfile
->cur_token
>= pfile
->cur_run
->limit
)
1259 if (pfile
->lookaheads
)
1261 pfile
->lookaheads
--;
1262 result
= pfile
->cur_token
++;
1265 result
= _cpp_lex_direct (pfile
);
1267 if (result
->flags
& BOL
)
1269 /* Is this a directive. If _cpp_handle_directive returns
1270 false, it is an assembler #. */
1271 if (result
->type
== CPP_HASH
1272 /* 6.10.3 p 11: Directives in a list of macro arguments
1273 gives undefined behavior. This implementation
1274 handles the directive as normal. */
1275 && pfile
->state
.parsing_args
!= 1)
1277 if (_cpp_handle_directive (pfile
, result
->flags
& PREV_WHITE
))
1279 if (pfile
->directive_result
.type
== CPP_PADDING
)
1281 result
= &pfile
->directive_result
;
1284 else if (pfile
->state
.in_deferred_pragma
)
1285 result
= &pfile
->directive_result
;
1287 if (pfile
->cb
.line_change
&& !pfile
->state
.skipping
)
1288 pfile
->cb
.line_change (pfile
, result
, pfile
->state
.parsing_args
);
1291 /* We don't skip tokens in directives. */
1292 if (pfile
->state
.in_directive
|| pfile
->state
.in_deferred_pragma
)
1295 /* Outside a directive, invalidate controlling macros. At file
1296 EOF, _cpp_lex_direct takes care of popping the buffer, so we never
1297 get here and MI optimization works. */
1298 pfile
->mi_valid
= false;
1300 if (!pfile
->state
.skipping
|| result
->type
== CPP_EOF
)
1307 /* Returns true if a fresh line has been loaded. */
1309 _cpp_get_fresh_line (cpp_reader
*pfile
)
1313 /* We can't get a new line until we leave the current directive. */
1314 if (pfile
->state
.in_directive
)
1319 cpp_buffer
*buffer
= pfile
->buffer
;
1321 if (!buffer
->need_line
)
1324 if (buffer
->next_line
< buffer
->rlimit
)
1326 _cpp_clean_line (pfile
);
1330 /* First, get out of parsing arguments state. */
1331 if (pfile
->state
.parsing_args
)
1334 /* End of buffer. Non-empty files should end in a newline. */
1335 if (buffer
->buf
!= buffer
->rlimit
1336 && buffer
->next_line
> buffer
->rlimit
1337 && !buffer
->from_stage3
)
1339 /* Clip to buffer size. */
1340 buffer
->next_line
= buffer
->rlimit
;
1343 return_at_eof
= buffer
->return_at_eof
;
1344 _cpp_pop_buffer (pfile
);
1345 if (pfile
->buffer
== NULL
|| return_at_eof
)
1350 #define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
1353 result->type = ELSE_TYPE; \
1354 if (*buffer->cur == CHAR) \
1355 buffer->cur++, result->type = THEN_TYPE; \
1359 /* Lex a token into pfile->cur_token, which is also incremented, to
1360 get diagnostics pointing to the correct location.
1362 Does not handle issues such as token lookahead, multiple-include
1363 optimization, directives, skipping etc. This function is only
1364 suitable for use by _cpp_lex_token, and in special cases like
1365 lex_expansion_token which doesn't care for any of these issues.
1367 When meeting a newline, returns CPP_EOF if parsing a directive,
1368 otherwise returns to the start of the token buffer if permissible.
1369 Returns the location of the lexed token. */
1371 _cpp_lex_direct (cpp_reader
*pfile
)
1375 const unsigned char *comment_start
;
1376 cpp_token
*result
= pfile
->cur_token
++;
1380 buffer
= pfile
->buffer
;
1381 if (buffer
->need_line
)
1383 if (pfile
->state
.in_deferred_pragma
)
1385 result
->type
= CPP_PRAGMA_EOL
;
1386 pfile
->state
.in_deferred_pragma
= false;
1387 if (!pfile
->state
.pragma_allow_expansion
)
1388 pfile
->state
.prevent_expansion
--;
1391 if (!_cpp_get_fresh_line (pfile
))
1393 result
->type
= CPP_EOF
;
1394 if (!pfile
->state
.in_directive
)
1396 /* Tell the compiler the line number of the EOF token. */
1397 result
->src_loc
= pfile
->line_table
->highest_line
;
1398 result
->flags
= BOL
;
1402 if (!pfile
->keep_tokens
)
1404 pfile
->cur_run
= &pfile
->base_run
;
1405 result
= pfile
->base_run
.base
;
1406 pfile
->cur_token
= result
+ 1;
1408 result
->flags
= BOL
;
1409 if (pfile
->state
.parsing_args
== 2)
1410 result
->flags
|= PREV_WHITE
;
1412 buffer
= pfile
->buffer
;
1414 result
->src_loc
= pfile
->line_table
->highest_line
;
1417 if (buffer
->cur
>= buffer
->notes
[buffer
->cur_note
].pos
1418 && !pfile
->overlaid_buffer
)
1420 _cpp_process_line_notes (pfile
, false);
1421 result
->src_loc
= pfile
->line_table
->highest_line
;
1425 LINEMAP_POSITION_FOR_COLUMN (result
->src_loc
, pfile
->line_table
,
1426 CPP_BUF_COLUMN (buffer
, buffer
->cur
));
1430 case ' ': case '\t': case '\f': case '\v': case '\0':
1431 result
->flags
|= PREV_WHITE
;
1432 skip_whitespace (pfile
, c
);
1436 if (buffer
->cur
< buffer
->rlimit
)
1437 CPP_INCREMENT_LINE (pfile
, 0);
1438 buffer
->need_line
= true;
1441 case '0': case '1': case '2': case '3': case '4':
1442 case '5': case '6': case '7': case '8': case '9':
1444 struct normalize_state nst
= INITIAL_NORMALIZE_STATE
;
1445 result
->type
= CPP_NUMBER
;
1446 lex_number (pfile
, &result
->val
.str
, &nst
);
1447 warn_about_normalization (pfile
, result
, &nst
);
1455 /* 'L', 'u', 'U', 'u8' or 'R' may introduce wide characters,
1456 wide strings or raw strings. */
1457 if (c
== 'L' || CPP_OPTION (pfile
, uliterals
))
1459 if ((*buffer
->cur
== '\'' && c
!= 'R')
1460 || *buffer
->cur
== '"'
1461 || (*buffer
->cur
== 'R'
1463 && buffer
->cur
[1] == '"'
1464 && CPP_OPTION (pfile
, uliterals
))
1465 || (*buffer
->cur
== '8'
1467 && (buffer
->cur
[1] == '"'
1468 || (buffer
->cur
[1] == 'R' && buffer
->cur
[2] == '"'))))
1470 lex_string (pfile
, result
, buffer
->cur
- 1);
1477 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1478 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1479 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1480 case 's': case 't': case 'v': case 'w': case 'x':
1482 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1483 case 'G': case 'H': case 'I': case 'J': case 'K':
1484 case 'M': case 'N': case 'O': case 'P': case 'Q':
1485 case 'S': case 'T': case 'V': case 'W': case 'X':
1487 result
->type
= CPP_NAME
;
1489 struct normalize_state nst
= INITIAL_NORMALIZE_STATE
;
1490 result
->val
.node
.node
= lex_identifier (pfile
, buffer
->cur
- 1, false,
1492 warn_about_normalization (pfile
, result
, &nst
);
1495 /* Convert named operators to their proper types. */
1496 if (result
->val
.node
.node
->flags
& NODE_OPERATOR
)
1498 result
->flags
|= NAMED_OP
;
1499 result
->type
= (enum cpp_ttype
) result
->val
.node
.node
->directive_index
;
1505 lex_string (pfile
, result
, buffer
->cur
- 1);
1509 /* A potential block or line comment. */
1510 comment_start
= buffer
->cur
;
1515 if (_cpp_skip_block_comment (pfile
))
1516 cpp_error (pfile
, CPP_DL_ERROR
, "unterminated comment");
1518 else if (c
== '/' && (CPP_OPTION (pfile
, cplusplus_comments
)
1519 || cpp_in_system_header (pfile
)))
1521 /* Warn about comments only if pedantically GNUC89, and not
1522 in system headers. */
1523 if (CPP_OPTION (pfile
, lang
) == CLK_GNUC89
&& CPP_PEDANTIC (pfile
)
1524 && ! buffer
->warned_cplusplus_comments
)
1526 cpp_error (pfile
, CPP_DL_PEDWARN
,
1527 "C++ style comments are not allowed in ISO C90");
1528 cpp_error (pfile
, CPP_DL_PEDWARN
,
1529 "(this will be reported only once per input file)");
1530 buffer
->warned_cplusplus_comments
= 1;
1533 if (skip_line_comment (pfile
) && CPP_OPTION (pfile
, warn_comments
))
1534 cpp_error (pfile
, CPP_DL_WARNING
, "multi-line comment");
1539 result
->type
= CPP_DIV_EQ
;
1544 result
->type
= CPP_DIV
;
1548 if (!pfile
->state
.save_comments
)
1550 result
->flags
|= PREV_WHITE
;
1551 goto update_tokens_line
;
1554 /* Save the comment as a token in its own right. */
1555 save_comment (pfile
, result
, comment_start
, c
);
1559 if (pfile
->state
.angled_headers
)
1561 lex_string (pfile
, result
, buffer
->cur
- 1);
1562 if (result
->type
!= CPP_LESS
)
1566 result
->type
= CPP_LESS
;
1567 if (*buffer
->cur
== '=')
1568 buffer
->cur
++, result
->type
= CPP_LESS_EQ
;
1569 else if (*buffer
->cur
== '<')
1572 IF_NEXT_IS ('=', CPP_LSHIFT_EQ
, CPP_LSHIFT
);
1574 else if (CPP_OPTION (pfile
, digraphs
))
1576 if (*buffer
->cur
== ':')
1579 result
->flags
|= DIGRAPH
;
1580 result
->type
= CPP_OPEN_SQUARE
;
1582 else if (*buffer
->cur
== '%')
1585 result
->flags
|= DIGRAPH
;
1586 result
->type
= CPP_OPEN_BRACE
;
1592 result
->type
= CPP_GREATER
;
1593 if (*buffer
->cur
== '=')
1594 buffer
->cur
++, result
->type
= CPP_GREATER_EQ
;
1595 else if (*buffer
->cur
== '>')
1598 IF_NEXT_IS ('=', CPP_RSHIFT_EQ
, CPP_RSHIFT
);
1603 result
->type
= CPP_MOD
;
1604 if (*buffer
->cur
== '=')
1605 buffer
->cur
++, result
->type
= CPP_MOD_EQ
;
1606 else if (CPP_OPTION (pfile
, digraphs
))
1608 if (*buffer
->cur
== ':')
1611 result
->flags
|= DIGRAPH
;
1612 result
->type
= CPP_HASH
;
1613 if (*buffer
->cur
== '%' && buffer
->cur
[1] == ':')
1614 buffer
->cur
+= 2, result
->type
= CPP_PASTE
, result
->val
.token_no
= 0;
1616 else if (*buffer
->cur
== '>')
1619 result
->flags
|= DIGRAPH
;
1620 result
->type
= CPP_CLOSE_BRACE
;
1626 result
->type
= CPP_DOT
;
1627 if (ISDIGIT (*buffer
->cur
))
1629 struct normalize_state nst
= INITIAL_NORMALIZE_STATE
;
1630 result
->type
= CPP_NUMBER
;
1631 lex_number (pfile
, &result
->val
.str
, &nst
);
1632 warn_about_normalization (pfile
, result
, &nst
);
1634 else if (*buffer
->cur
== '.' && buffer
->cur
[1] == '.')
1635 buffer
->cur
+= 2, result
->type
= CPP_ELLIPSIS
;
1636 else if (*buffer
->cur
== '*' && CPP_OPTION (pfile
, cplusplus
))
1637 buffer
->cur
++, result
->type
= CPP_DOT_STAR
;
1641 result
->type
= CPP_PLUS
;
1642 if (*buffer
->cur
== '+')
1643 buffer
->cur
++, result
->type
= CPP_PLUS_PLUS
;
1644 else if (*buffer
->cur
== '=')
1645 buffer
->cur
++, result
->type
= CPP_PLUS_EQ
;
1649 result
->type
= CPP_MINUS
;
1650 if (*buffer
->cur
== '>')
1653 result
->type
= CPP_DEREF
;
1654 if (*buffer
->cur
== '*' && CPP_OPTION (pfile
, cplusplus
))
1655 buffer
->cur
++, result
->type
= CPP_DEREF_STAR
;
1657 else if (*buffer
->cur
== '-')
1658 buffer
->cur
++, result
->type
= CPP_MINUS_MINUS
;
1659 else if (*buffer
->cur
== '=')
1660 buffer
->cur
++, result
->type
= CPP_MINUS_EQ
;
1664 result
->type
= CPP_AND
;
1665 if (*buffer
->cur
== '&')
1666 buffer
->cur
++, result
->type
= CPP_AND_AND
;
1667 else if (*buffer
->cur
== '=')
1668 buffer
->cur
++, result
->type
= CPP_AND_EQ
;
1672 result
->type
= CPP_OR
;
1673 if (*buffer
->cur
== '|')
1674 buffer
->cur
++, result
->type
= CPP_OR_OR
;
1675 else if (*buffer
->cur
== '=')
1676 buffer
->cur
++, result
->type
= CPP_OR_EQ
;
1680 result
->type
= CPP_COLON
;
1681 if (*buffer
->cur
== ':' && CPP_OPTION (pfile
, cplusplus
))
1682 buffer
->cur
++, result
->type
= CPP_SCOPE
;
1683 else if (*buffer
->cur
== '>' && CPP_OPTION (pfile
, digraphs
))
1686 result
->flags
|= DIGRAPH
;
1687 result
->type
= CPP_CLOSE_SQUARE
;
1691 case '*': IF_NEXT_IS ('=', CPP_MULT_EQ
, CPP_MULT
); break;
1692 case '=': IF_NEXT_IS ('=', CPP_EQ_EQ
, CPP_EQ
); break;
1693 case '!': IF_NEXT_IS ('=', CPP_NOT_EQ
, CPP_NOT
); break;
1694 case '^': IF_NEXT_IS ('=', CPP_XOR_EQ
, CPP_XOR
); break;
1695 case '#': IF_NEXT_IS ('#', CPP_PASTE
, CPP_HASH
); result
->val
.token_no
= 0; break;
1697 case '?': result
->type
= CPP_QUERY
; break;
1698 case '~': result
->type
= CPP_COMPL
; break;
1699 case ',': result
->type
= CPP_COMMA
; break;
1700 case '(': result
->type
= CPP_OPEN_PAREN
; break;
1701 case ')': result
->type
= CPP_CLOSE_PAREN
; break;
1702 case '[': result
->type
= CPP_OPEN_SQUARE
; break;
1703 case ']': result
->type
= CPP_CLOSE_SQUARE
; break;
1704 case '{': result
->type
= CPP_OPEN_BRACE
; break;
1705 case '}': result
->type
= CPP_CLOSE_BRACE
; break;
1706 case ';': result
->type
= CPP_SEMICOLON
; break;
1708 /* @ is a punctuator in Objective-C. */
1709 case '@': result
->type
= CPP_ATSIGN
; break;
1714 const uchar
*base
= --buffer
->cur
;
1715 struct normalize_state nst
= INITIAL_NORMALIZE_STATE
;
1717 if (forms_identifier_p (pfile
, true, &nst
))
1719 result
->type
= CPP_NAME
;
1720 result
->val
.node
.node
= lex_identifier (pfile
, base
, true, &nst
);
1721 warn_about_normalization (pfile
, result
, &nst
);
1728 create_literal (pfile
, result
, buffer
->cur
- 1, 1, CPP_OTHER
);
1735 /* An upper bound on the number of bytes needed to spell TOKEN.
1736 Does not include preceding whitespace. */
1738 cpp_token_len (const cpp_token
*token
)
1742 switch (TOKEN_SPELL (token
))
1744 default: len
= 6; break;
1745 case SPELL_LITERAL
: len
= token
->val
.str
.len
; break;
1746 case SPELL_IDENT
: len
= NODE_LEN (token
->val
.node
.node
) * 10; break;
1752 /* Parse UTF-8 out of NAMEP and place a \U escape in BUFFER.
1753 Return the number of bytes read out of NAME. (There are always
1754 10 bytes written to BUFFER.) */
1757 utf8_to_ucn (unsigned char *buffer
, const unsigned char *name
)
1763 unsigned long utf32
;
1765 /* Compute the length of the UTF-8 sequence. */
1766 for (t
= *name
; t
& 0x80; t
<<= 1)
1769 utf32
= *name
& (0x7F >> ucn_len
);
1770 for (ucn_len_c
= 1; ucn_len_c
< ucn_len
; ucn_len_c
++)
1772 utf32
= (utf32
<< 6) | (*++name
& 0x3F);
1774 /* Ill-formed UTF-8. */
1775 if ((*name
& ~0x3F) != 0x80)
1781 for (j
= 7; j
>= 0; j
--)
1782 *buffer
++ = "0123456789abcdef"[(utf32
>> (4 * j
)) & 0xF];
1786 /* Given a token TYPE corresponding to a digraph, return a pointer to
1787 the spelling of the digraph. */
1788 static const unsigned char *
1789 cpp_digraph2name (enum cpp_ttype type
)
1791 return digraph_spellings
[(int) type
- (int) CPP_FIRST_DIGRAPH
];
1794 /* Write the spelling of a token TOKEN to BUFFER. The buffer must
1795 already contain the enough space to hold the token's spelling.
1796 Returns a pointer to the character after the last character written.
1797 FORSTRING is true if this is to be the spelling after translation
1798 phase 1 (this is different for UCNs).
1799 FIXME: Would be nice if we didn't need the PFILE argument. */
1801 cpp_spell_token (cpp_reader
*pfile
, const cpp_token
*token
,
1802 unsigned char *buffer
, bool forstring
)
1804 switch (TOKEN_SPELL (token
))
1806 case SPELL_OPERATOR
:
1808 const unsigned char *spelling
;
1811 if (token
->flags
& DIGRAPH
)
1812 spelling
= cpp_digraph2name (token
->type
);
1813 else if (token
->flags
& NAMED_OP
)
1816 spelling
= TOKEN_NAME (token
);
1818 while ((c
= *spelling
++) != '\0')
1827 memcpy (buffer
, NODE_NAME (token
->val
.node
.node
),
1828 NODE_LEN (token
->val
.node
.node
));
1829 buffer
+= NODE_LEN (token
->val
.node
.node
);
1834 const unsigned char * name
= NODE_NAME (token
->val
.node
.node
);
1836 for (i
= 0; i
< NODE_LEN (token
->val
.node
.node
); i
++)
1837 if (name
[i
] & ~0x7F)
1839 i
+= utf8_to_ucn (buffer
, name
+ i
) - 1;
1843 *buffer
++ = NODE_NAME (token
->val
.node
.node
)[i
];
1848 memcpy (buffer
, token
->val
.str
.text
, token
->val
.str
.len
);
1849 buffer
+= token
->val
.str
.len
;
1853 cpp_error (pfile
, CPP_DL_ICE
,
1854 "unspellable token %s", TOKEN_NAME (token
));
1861 /* Returns TOKEN spelt as a null-terminated string. The string is
1862 freed when the reader is destroyed. Useful for diagnostics. */
1864 cpp_token_as_text (cpp_reader
*pfile
, const cpp_token
*token
)
1866 unsigned int len
= cpp_token_len (token
) + 1;
1867 unsigned char *start
= _cpp_unaligned_alloc (pfile
, len
), *end
;
1869 end
= cpp_spell_token (pfile
, token
, start
, false);
1875 /* Returns a pointer to a string which spells the token defined by
1876 TYPE and FLAGS. Used by C front ends, which really should move to
1877 using cpp_token_as_text. */
1879 cpp_type2name (enum cpp_ttype type
, unsigned char flags
)
1881 if (flags
& DIGRAPH
)
1882 return (const char *) cpp_digraph2name (type
);
1883 else if (flags
& NAMED_OP
)
1884 return cpp_named_operator2name (type
);
1886 return (const char *) token_spellings
[type
].name
;
1889 /* Writes the spelling of token to FP, without any preceding space.
1890 Separated from cpp_spell_token for efficiency - to avoid stdio
1891 double-buffering. */
1893 cpp_output_token (const cpp_token
*token
, FILE *fp
)
1895 switch (TOKEN_SPELL (token
))
1897 case SPELL_OPERATOR
:
1899 const unsigned char *spelling
;
1902 if (token
->flags
& DIGRAPH
)
1903 spelling
= cpp_digraph2name (token
->type
);
1904 else if (token
->flags
& NAMED_OP
)
1907 spelling
= TOKEN_NAME (token
);
1912 while ((c
= *++spelling
) != '\0');
1920 const unsigned char * name
= NODE_NAME (token
->val
.node
.node
);
1922 for (i
= 0; i
< NODE_LEN (token
->val
.node
.node
); i
++)
1923 if (name
[i
] & ~0x7F)
1925 unsigned char buffer
[10];
1926 i
+= utf8_to_ucn (buffer
, name
+ i
) - 1;
1927 fwrite (buffer
, 1, 10, fp
);
1930 fputc (NODE_NAME (token
->val
.node
.node
)[i
], fp
);
1935 fwrite (token
->val
.str
.text
, 1, token
->val
.str
.len
, fp
);
1939 /* An error, most probably. */
1944 /* Compare two tokens. */
1946 _cpp_equiv_tokens (const cpp_token
*a
, const cpp_token
*b
)
1948 if (a
->type
== b
->type
&& a
->flags
== b
->flags
)
1949 switch (TOKEN_SPELL (a
))
1951 default: /* Keep compiler happy. */
1952 case SPELL_OPERATOR
:
1953 /* token_no is used to track where multiple consecutive ##
1954 tokens were originally located. */
1955 return (a
->type
!= CPP_PASTE
|| a
->val
.token_no
== b
->val
.token_no
);
1957 return (a
->type
!= CPP_MACRO_ARG
1958 || a
->val
.macro_arg
.arg_no
== b
->val
.macro_arg
.arg_no
);
1960 return a
->val
.node
.node
== b
->val
.node
.node
;
1962 return (a
->val
.str
.len
== b
->val
.str
.len
1963 && !memcmp (a
->val
.str
.text
, b
->val
.str
.text
,
1970 /* Returns nonzero if a space should be inserted to avoid an
1971 accidental token paste for output. For simplicity, it is
1972 conservative, and occasionally advises a space where one is not
1973 needed, e.g. "." and ".2". */
1975 cpp_avoid_paste (cpp_reader
*pfile
, const cpp_token
*token1
,
1976 const cpp_token
*token2
)
1978 enum cpp_ttype a
= token1
->type
, b
= token2
->type
;
1981 if (token1
->flags
& NAMED_OP
)
1983 if (token2
->flags
& NAMED_OP
)
1987 if (token2
->flags
& DIGRAPH
)
1988 c
= digraph_spellings
[(int) b
- (int) CPP_FIRST_DIGRAPH
][0];
1989 else if (token_spellings
[b
].category
== SPELL_OPERATOR
)
1990 c
= token_spellings
[b
].name
[0];
1992 /* Quickly get everything that can paste with an '='. */
1993 if ((int) a
<= (int) CPP_LAST_EQ
&& c
== '=')
1998 case CPP_GREATER
: return c
== '>';
1999 case CPP_LESS
: return c
== '<' || c
== '%' || c
== ':';
2000 case CPP_PLUS
: return c
== '+';
2001 case CPP_MINUS
: return c
== '-' || c
== '>';
2002 case CPP_DIV
: return c
== '/' || c
== '*'; /* Comments. */
2003 case CPP_MOD
: return c
== ':' || c
== '>';
2004 case CPP_AND
: return c
== '&';
2005 case CPP_OR
: return c
== '|';
2006 case CPP_COLON
: return c
== ':' || c
== '>';
2007 case CPP_DEREF
: return c
== '*';
2008 case CPP_DOT
: return c
== '.' || c
== '%' || b
== CPP_NUMBER
;
2009 case CPP_HASH
: return c
== '#' || c
== '%'; /* Digraph form. */
2010 case CPP_NAME
: return ((b
== CPP_NUMBER
2011 && name_p (pfile
, &token2
->val
.str
))
2013 || b
== CPP_CHAR
|| b
== CPP_STRING
); /* L */
2014 case CPP_NUMBER
: return (b
== CPP_NUMBER
|| b
== CPP_NAME
2015 || c
== '.' || c
== '+' || c
== '-');
2017 case CPP_OTHER
: return ((token1
->val
.str
.text
[0] == '\\'
2019 || (CPP_OPTION (pfile
, objc
)
2020 && token1
->val
.str
.text
[0] == '@'
2021 && (b
== CPP_NAME
|| b
== CPP_STRING
)));
2028 /* Output all the remaining tokens on the current line, and a newline
2029 character, to FP. Leading whitespace is removed. If there are
2030 macros, special token padding is not performed. */
2032 cpp_output_line (cpp_reader
*pfile
, FILE *fp
)
2034 const cpp_token
*token
;
2036 token
= cpp_get_token (pfile
);
2037 while (token
->type
!= CPP_EOF
)
2039 cpp_output_token (token
, fp
);
2040 token
= cpp_get_token (pfile
);
2041 if (token
->flags
& PREV_WHITE
)
2048 /* Return a string representation of all the remaining tokens on the
2049 current line. The result is allocated using xmalloc and must be
2050 freed by the caller. */
2052 cpp_output_line_to_string (cpp_reader
*pfile
, const unsigned char *dir_name
)
2054 const cpp_token
*token
;
2055 unsigned int out
= dir_name
? ustrlen (dir_name
) : 0;
2056 unsigned int alloced
= 120 + out
;
2057 unsigned char *result
= (unsigned char *) xmalloc (alloced
);
2059 /* If DIR_NAME is empty, there are no initial contents. */
2062 sprintf ((char *) result
, "#%s ", dir_name
);
2066 token
= cpp_get_token (pfile
);
2067 while (token
->type
!= CPP_EOF
)
2069 unsigned char *last
;
2070 /* Include room for a possible space and the terminating nul. */
2071 unsigned int len
= cpp_token_len (token
) + 2;
2073 if (out
+ len
> alloced
)
2076 if (out
+ len
> alloced
)
2077 alloced
= out
+ len
;
2078 result
= (unsigned char *) xrealloc (result
, alloced
);
2081 last
= cpp_spell_token (pfile
, token
, &result
[out
], 0);
2082 out
= last
- result
;
2084 token
= cpp_get_token (pfile
);
2085 if (token
->flags
& PREV_WHITE
)
2086 result
[out
++] = ' ';
2093 /* Memory buffers. Changing these three constants can have a dramatic
2094 effect on performance. The values here are reasonable defaults,
2095 but might be tuned. If you adjust them, be sure to test across a
2096 range of uses of cpplib, including heavy nested function-like macro
2097 expansion. Also check the change in peak memory usage (NJAMD is a
2098 good tool for this). */
2099 #define MIN_BUFF_SIZE 8000
2100 #define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2)
2101 #define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \
2102 (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2)
2104 #if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0)
2105 #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
2108 /* Create a new allocation buffer. Place the control block at the end
2109 of the buffer, so that buffer overflows will cause immediate chaos. */
2111 new_buff (size_t len
)
2114 unsigned char *base
;
2116 if (len
< MIN_BUFF_SIZE
)
2117 len
= MIN_BUFF_SIZE
;
2118 len
= CPP_ALIGN (len
);
2120 base
= XNEWVEC (unsigned char, len
+ sizeof (_cpp_buff
));
2121 result
= (_cpp_buff
*) (base
+ len
);
2122 result
->base
= base
;
2124 result
->limit
= base
+ len
;
2125 result
->next
= NULL
;
2129 /* Place a chain of unwanted allocation buffers on the free list. */
2131 _cpp_release_buff (cpp_reader
*pfile
, _cpp_buff
*buff
)
2133 _cpp_buff
*end
= buff
;
2137 end
->next
= pfile
->free_buffs
;
2138 pfile
->free_buffs
= buff
;
2141 /* Return a free buffer of size at least MIN_SIZE. */
2143 _cpp_get_buff (cpp_reader
*pfile
, size_t min_size
)
2145 _cpp_buff
*result
, **p
;
2147 for (p
= &pfile
->free_buffs
;; p
= &(*p
)->next
)
2152 return new_buff (min_size
);
2154 size
= result
->limit
- result
->base
;
2155 /* Return a buffer that's big enough, but don't waste one that's
2157 if (size
>= min_size
&& size
<= BUFF_SIZE_UPPER_BOUND (min_size
))
2162 result
->next
= NULL
;
2163 result
->cur
= result
->base
;
2167 /* Creates a new buffer with enough space to hold the uncommitted
2168 remaining bytes of BUFF, and at least MIN_EXTRA more bytes. Copies
2169 the excess bytes to the new buffer. Chains the new buffer after
2170 BUFF, and returns the new buffer. */
2172 _cpp_append_extend_buff (cpp_reader
*pfile
, _cpp_buff
*buff
, size_t min_extra
)
2174 size_t size
= EXTENDED_BUFF_SIZE (buff
, min_extra
);
2175 _cpp_buff
*new_buff
= _cpp_get_buff (pfile
, size
);
2177 buff
->next
= new_buff
;
2178 memcpy (new_buff
->base
, buff
->cur
, BUFF_ROOM (buff
));
2182 /* Creates a new buffer with enough space to hold the uncommitted
2183 remaining bytes of the buffer pointed to by BUFF, and at least
2184 MIN_EXTRA more bytes. Copies the excess bytes to the new buffer.
2185 Chains the new buffer before the buffer pointed to by BUFF, and
2186 updates the pointer to point to the new buffer. */
2188 _cpp_extend_buff (cpp_reader
*pfile
, _cpp_buff
**pbuff
, size_t min_extra
)
2190 _cpp_buff
*new_buff
, *old_buff
= *pbuff
;
2191 size_t size
= EXTENDED_BUFF_SIZE (old_buff
, min_extra
);
2193 new_buff
= _cpp_get_buff (pfile
, size
);
2194 memcpy (new_buff
->base
, old_buff
->cur
, BUFF_ROOM (old_buff
));
2195 new_buff
->next
= old_buff
;
2199 /* Free a chain of buffers starting at BUFF. */
2201 _cpp_free_buff (_cpp_buff
*buff
)
2205 for (; buff
; buff
= next
)
2212 /* Allocate permanent, unaligned storage of length LEN. */
2214 _cpp_unaligned_alloc (cpp_reader
*pfile
, size_t len
)
2216 _cpp_buff
*buff
= pfile
->u_buff
;
2217 unsigned char *result
= buff
->cur
;
2219 if (len
> (size_t) (buff
->limit
- result
))
2221 buff
= _cpp_get_buff (pfile
, len
);
2222 buff
->next
= pfile
->u_buff
;
2223 pfile
->u_buff
= buff
;
2227 buff
->cur
= result
+ len
;
2231 /* Allocate permanent, unaligned storage of length LEN from a_buff.
2232 That buffer is used for growing allocations when saving macro
2233 replacement lists in a #define, and when parsing an answer to an
2234 assertion in #assert, #unassert or #if (and therefore possibly
2235 whilst expanding macros). It therefore must not be used by any
2236 code that they might call: specifically the lexer and the guts of
2239 All existing other uses clearly fit this restriction: storing
2240 registered pragmas during initialization. */
2242 _cpp_aligned_alloc (cpp_reader
*pfile
, size_t len
)
2244 _cpp_buff
*buff
= pfile
->a_buff
;
2245 unsigned char *result
= buff
->cur
;
2247 if (len
> (size_t) (buff
->limit
- result
))
2249 buff
= _cpp_get_buff (pfile
, len
);
2250 buff
->next
= pfile
->a_buff
;
2251 pfile
->a_buff
= buff
;
2255 buff
->cur
= result
+ len
;
2259 /* Say which field of TOK is in use. */
2261 enum cpp_token_fld_kind
2262 cpp_token_val_index (cpp_token
*tok
)
2264 switch (TOKEN_SPELL (tok
))
2267 return CPP_TOKEN_FLD_NODE
;
2269 return CPP_TOKEN_FLD_STR
;
2270 case SPELL_OPERATOR
:
2271 if (tok
->type
== CPP_PASTE
)
2272 return CPP_TOKEN_FLD_TOKEN_NO
;
2274 return CPP_TOKEN_FLD_NONE
;
2276 if (tok
->type
== CPP_MACRO_ARG
)
2277 return CPP_TOKEN_FLD_ARG_NO
;
2278 else if (tok
->type
== CPP_PADDING
)
2279 return CPP_TOKEN_FLD_SOURCE
;
2280 else if (tok
->type
== CPP_PRAGMA
)
2281 return CPP_TOKEN_FLD_PRAGMA
;
2282 /* else fall through */
2284 return CPP_TOKEN_FLD_NONE
;