]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cpplex.c
* cpphash.h (_cpp_digraph_spellings, _cpp_process_directive,
[thirdparty/gcc.git] / gcc / cpplex.c
CommitLineData
0578f103 1/* CPP Library - lexical analysis.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6 Broken out to separate file, Zack Weinberg, Mar 2000
6060326b 7 Single-pass line tokenization by Neil Booth, April 2000
0578f103 8
9This program is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
11Free Software Foundation; either version 2, or (at your option) any
12later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
f80e83a9 23/*
24
25Cleanups to do:-
26
f80e83a9 27o Distinguish integers, floats, and 'other' pp-numbers.
28o Store ints and char constants as binary values.
29o New command-line assertion syntax.
f80e83a9 30o Comment all functions, and describe macro expansion algorithm.
31o Move as much out of header files as possible.
32o Remove single quote pairs `', and some '', from diagnostics.
33o Correct pastability test for CPP_NAME and CPP_NUMBER.
34
35*/
36
0578f103 37#include "config.h"
38#include "system.h"
39#include "intl.h"
40#include "cpplib.h"
41#include "cpphash.h"
f80e83a9 42#include "symcat.h"
0578f103 43
524f0c40 44const unsigned char *_cpp_digraph_spellings [] = {U"%:", U"%:%:", U"<:",
45 U":>", U"<%", U"%>"};
338fa5f7 46static const cpp_token placemarker_token = {0, 0, CPP_PLACEMARKER,
47 0 UNION_INIT_ZERO};
241e762e 48static const cpp_token eof_token = {0, 0, CPP_EOF, 0 UNION_INIT_ZERO};
49
50/* Flags for cpp_context. */
51#define CONTEXT_PASTEL (1 << 0) /* An argument context on LHS of ##. */
52#define CONTEXT_PASTER (1 << 1) /* An argument context on RHS of ##. */
53#define CONTEXT_RAW (1 << 2) /* If argument tokens already expanded. */
54#define CONTEXT_ARG (1 << 3) /* If an argument context. */
55
56typedef struct cpp_context cpp_context;
57struct cpp_context
58{
59 union
60 {
61 const cpp_toklist *list; /* Used for macro contexts only. */
62 const cpp_token **arg; /* Used for arg contexts only. */
63 } u;
64
65 /* Pushed token to be returned by next call to get_raw_token. */
66 const cpp_token *pushed_token;
67
fdf3a98f 68 struct macro_args *args; /* The arguments for a function-like
69 macro. NULL otherwise. */
241e762e 70 unsigned short posn; /* Current posn, index into u. */
71 unsigned short count; /* No. of tokens in u. */
72 unsigned short level;
73 unsigned char flags;
74};
75
76typedef struct macro_args macro_args;
77struct macro_args
78{
79 unsigned int *ends;
80 const cpp_token **tokens;
81 unsigned int capacity;
82 unsigned int used;
83 unsigned short level;
84};
85
86static const cpp_token *get_raw_token PARAMS ((cpp_reader *));
87static const cpp_token *parse_arg PARAMS ((cpp_reader *, int, unsigned int,
88 macro_args *, unsigned int *));
89static int parse_args PARAMS ((cpp_reader *, cpp_hashnode *, macro_args *));
90static void save_token PARAMS ((macro_args *, const cpp_token *));
91static int pop_context PARAMS ((cpp_reader *));
92static int push_macro_context PARAMS ((cpp_reader *, const cpp_token *));
93static void push_arg_context PARAMS ((cpp_reader *, const cpp_token *));
94static void free_macro_args PARAMS ((macro_args *));
e2f9a79f 95
338fa5f7 96static cppchar_t handle_newline PARAMS ((cpp_buffer *, cppchar_t));
97static cppchar_t skip_escaped_newlines PARAMS ((cpp_buffer *, cppchar_t));
98static cppchar_t get_effective_char PARAMS ((cpp_buffer *));
99
f80e83a9 100static int skip_block_comment PARAMS ((cpp_reader *));
f669338a 101static int skip_line_comment PARAMS ((cpp_reader *));
338fa5f7 102static void adjust_column PARAMS ((cpp_reader *));
103static void skip_whitespace PARAMS ((cpp_reader *, cppchar_t));
104static cpp_hashnode *parse_identifier PARAMS ((cpp_reader *, cppchar_t));
105static void parse_number PARAMS ((cpp_reader *, cpp_string *, cppchar_t));
106static void parse_string PARAMS ((cpp_reader *, cpp_token *, cppchar_t));
107static void unterminated PARAMS ((cpp_reader *, unsigned int, int));
108static int trigraph_ok PARAMS ((cpp_reader *, cppchar_t));
109static void save_comment PARAMS ((cpp_reader *, cpp_token *, const U_CHAR *));
f669338a 110static void lex_percent PARAMS ((cpp_buffer *, cpp_token *));
111static void lex_dot PARAMS ((cpp_reader *, cpp_token *));
f80e83a9 112static void lex_line PARAMS ((cpp_reader *, cpp_toklist *));
338fa5f7 113static void lex_token PARAMS ((cpp_reader *, cpp_token *));
f80e83a9 114static int lex_next PARAMS ((cpp_reader *, int));
338fa5f7 115
f80e83a9 116static int is_macro_disabled PARAMS ((cpp_reader *, const cpp_toklist *,
117 const cpp_token *));
6bc03ce3 118
f80e83a9 119static cpp_token *stringify_arg PARAMS ((cpp_reader *, const cpp_token *));
120static void expand_context_stack PARAMS ((cpp_reader *));
d2efb5ed 121static unsigned char * spell_token PARAMS ((cpp_reader *, const cpp_token *,
f80e83a9 122 unsigned char *));
6bc03ce3 123typedef unsigned int (* speller) PARAMS ((unsigned char *, cpp_toklist *,
124 cpp_token *));
f80e83a9 125static cpp_token *make_string_token PARAMS ((cpp_token *, const U_CHAR *,
126 unsigned int));
127static cpp_token *alloc_number_token PARAMS ((cpp_reader *, int number));
128static const cpp_token *special_symbol PARAMS ((cpp_reader *, cpp_hashnode *,
129 const cpp_token *));
130static cpp_token *duplicate_token PARAMS ((cpp_reader *, const cpp_token *));
131static const cpp_token *maybe_paste_with_next PARAMS ((cpp_reader *,
132 const cpp_token *));
f80e83a9 133static unsigned int prevent_macro_expansion PARAMS ((cpp_reader *));
134static void restore_macro_expansion PARAMS ((cpp_reader *, unsigned int));
135static cpp_token *get_temp_token PARAMS ((cpp_reader *));
136static void release_temp_tokens PARAMS ((cpp_reader *));
137static U_CHAR * quote_string PARAMS ((U_CHAR *, const U_CHAR *, unsigned int));
6bc03ce3 138
f80e83a9 139#define VALID_SIGN(c, prevc) \
140 (((c) == '+' || (c) == '-') && \
141 ((prevc) == 'e' || (prevc) == 'E' \
142 || (((prevc) == 'p' || (prevc) == 'P') && !CPP_OPTION (pfile, c89))))
143
e916a356 144/* An upper bound on the number of bytes needed to spell a token,
145 including preceding whitespace. */
6cae2504 146static inline size_t TOKEN_LEN PARAMS ((const cpp_token *));
147static inline size_t
148TOKEN_LEN (token)
149 const cpp_token *token;
150{
151 size_t len;
152
153 switch (TOKEN_SPELL (token))
154 {
155 default: len = 0; break;
156 case SPELL_STRING: len = token->val.str.len; break;
157 case SPELL_IDENT: len = token->val.node->length; break;
158 }
159 return len + 5;
160}
e916a356 161
241e762e 162#define IS_ARG_CONTEXT(c) ((c)->flags & CONTEXT_ARG)
163#define CURRENT_CONTEXT(pfile) ((pfile)->contexts + (pfile)->cur_context)
e0a859f1 164#define ON_REST_ARG(c) \
58fe658a 165 (((c)->u.list->flags & VAR_ARGS) \
166 && (c)->u.list->tokens[(c)->posn - 1].val.aux \
167 == (unsigned int) ((c)->u.list->paramc - 1))
241e762e 168
169#define ASSIGN_FLAGS_AND_POS(d, s) \
170 do {(d)->flags = (s)->flags & (PREV_WHITE | BOL | PASTE_LEFT); \
171 if ((d)->flags & BOL) {(d)->col = (s)->col; (d)->line = (s)->line;} \
172 } while (0)
173
174/* f is flags, just consisting of PREV_WHITE | BOL. */
175#define MODIFY_FLAGS_AND_POS(d, s, f) \
176 do {(d)->flags &= ~(PREV_WHITE | BOL); (d)->flags |= (f); \
177 if ((f) & BOL) {(d)->col = (s)->col; (d)->line = (s)->line;} \
178 } while (0)
179
7e842f95 180#define OP(e, s) { SPELL_OPERATOR, U s },
181#define TK(e, s) { s, U STRINGX (e) },
6bc03ce3 182
f80e83a9 183const struct token_spelling
7e842f95 184_cpp_token_spellings [N_TTYPES] = {TTYPE_TABLE };
f80e83a9 185
7e842f95 186#undef OP
187#undef TK
6bc03ce3 188
f80e83a9 189/* Helper routine used by parse_include, which can't see spell_token.
190 Reinterpret the current line as an h-char-sequence (< ... >); we are
191 looking at the first token after the <. */
192const cpp_token *
193_cpp_glue_header_name (pfile)
0578f103 194 cpp_reader *pfile;
195{
f80e83a9 196 const cpp_token *t;
197 cpp_token *hdr;
6cae2504 198 U_CHAR *buf, *p;
199 size_t len, avail;
200
201 avail = 40;
202 len = 0;
203 buf = xmalloc (avail);
f80e83a9 204
205 for (;;)
206 {
deb356cf 207 t = _cpp_get_token (pfile);
f80e83a9 208 if (t->type == CPP_GREATER || t->type == CPP_EOF)
209 break;
210
6cae2504 211 if (len + TOKEN_LEN (t) > avail)
212 {
213 avail = len + TOKEN_LEN (t) + 40;
214 buf = xrealloc (buf, avail);
215 }
216
f80e83a9 217 if (t->flags & PREV_WHITE)
6cae2504 218 buf[len++] = ' ';
219
220 p = spell_token (pfile, t, buf + len);
221 len = (size_t) (p - buf); /* p known >= buf */
f80e83a9 222 }
223
224 if (t->type == CPP_EOF)
225 cpp_error (pfile, "missing terminating > character");
0578f103 226
6cae2504 227 buf = xrealloc (buf, len);
f80e83a9 228
229 hdr = get_temp_token (pfile);
230 hdr->type = CPP_HEADER_NAME;
231 hdr->flags = 0;
76faa4c0 232 hdr->val.str.text = buf;
233 hdr->val.str.len = len;
f80e83a9 234 return hdr;
0578f103 235}
236
50fd6b48 237/* Token-buffer helper functions. */
238
d2efb5ed 239/* Expand a token list's string space. It is *vital* that
240 list->tokens_used is correct, to get pointer fix-up right. */
f80e83a9 241void
242_cpp_expand_name_space (list, len)
50fd6b48 243 cpp_toklist *list;
6060326b 244 unsigned int len;
245{
e916a356 246 const U_CHAR *old_namebuf;
e916a356 247
248 old_namebuf = list->namebuf;
6060326b 249 list->name_cap += len;
250 list->namebuf = (unsigned char *) xrealloc (list->namebuf, list->name_cap);
e916a356 251
252 /* Fix up token text pointers. */
6dafd80a 253 if (list->namebuf != old_namebuf)
e916a356 254 {
255 unsigned int i;
256
257 for (i = 0; i < list->tokens_used; i++)
7e842f95 258 if (TOKEN_SPELL (&list->tokens[i]) == SPELL_STRING)
76faa4c0 259 list->tokens[i].val.str.text += (list->namebuf - old_namebuf);
e916a356 260 }
50fd6b48 261}
262
f80e83a9 263/* If there is not enough room for LEN more characters, expand the
264 list by just enough to have room for LEN characters. */
265void
266_cpp_reserve_name_space (list, len)
267 cpp_toklist *list;
268 unsigned int len;
269{
270 unsigned int room = list->name_cap - list->name_used;
271
272 if (room < len)
273 _cpp_expand_name_space (list, len - room);
274}
275
50fd6b48 276/* Expand the number of tokens in a list. */
d2efb5ed 277void
278_cpp_expand_token_space (list, count)
50fd6b48 279 cpp_toklist *list;
d2efb5ed 280 unsigned int count;
50fd6b48 281{
d2efb5ed 282 list->tokens_cap += count;
50fd6b48 283 list->tokens = (cpp_token *)
f669338a 284 xrealloc (list->tokens, list->tokens_cap * sizeof (cpp_token));
50fd6b48 285}
286
f669338a 287/* Initialize a token list. If EMPTY is false, some token and name
288 space is provided. */
bce8e0c0 289void
f669338a 290_cpp_init_toklist (list, empty)
50fd6b48 291 cpp_toklist *list;
f669338a 292 int empty;
50fd6b48 293{
f669338a 294 if (empty)
d2efb5ed 295 {
296 list->tokens_cap = 0;
f80e83a9 297 list->tokens = 0;
d2efb5ed 298 list->name_cap = 0;
f80e83a9 299 list->namebuf = 0;
d2efb5ed 300 }
301 else
302 {
f669338a 303 /* Initialize token space. */
d2efb5ed 304 list->tokens_cap = 256; /* 4K's worth. */
305 list->tokens = (cpp_token *)
306 xmalloc ((list->tokens_cap + 1) * sizeof (cpp_token));
d2efb5ed 307
308 /* Initialize name space. */
309 list->name_cap = 1024;
f80e83a9 310 list->namebuf = (unsigned char *) xmalloc (list->name_cap);
d2efb5ed 311 }
bce8e0c0 312
bce8e0c0 313 _cpp_clear_toklist (list);
314}
50fd6b48 315
bce8e0c0 316/* Clear a token list. */
317void
318_cpp_clear_toklist (list)
319 cpp_toklist *list;
320{
6060326b 321 list->tokens_used = 0;
322 list->name_used = 0;
f80e83a9 323 list->directive = 0;
324 list->paramc = 0;
325 list->params_len = 0;
f669338a 326 list->flags = 0;
bce8e0c0 327}
328
329/* Free a token list. Does not free the list itself, which may be
330 embedded in a larger structure. */
331void
332_cpp_free_toklist (list)
f80e83a9 333 const cpp_toklist *list;
bce8e0c0 334{
f669338a 335 free (list->tokens);
bce8e0c0 336 free (list->namebuf);
50fd6b48 337}
338
bce8e0c0 339/* Compare two tokens. */
340int
341_cpp_equiv_tokens (a, b)
342 const cpp_token *a, *b;
343{
f80e83a9 344 if (a->type == b->type && a->flags == b->flags)
7e842f95 345 switch (TOKEN_SPELL (a))
f80e83a9 346 {
347 default: /* Keep compiler happy. */
348 case SPELL_OPERATOR:
349 return 1;
350 case SPELL_CHAR:
351 case SPELL_NONE:
352 return a->val.aux == b->val.aux; /* arg_no or character. */
353 case SPELL_IDENT:
76faa4c0 354 return a->val.node == b->val.node;
f80e83a9 355 case SPELL_STRING:
76faa4c0 356 return (a->val.str.len == b->val.str.len
357 && !memcmp (a->val.str.text, b->val.str.text,
358 a->val.str.len));
f80e83a9 359 }
bce8e0c0 360
f80e83a9 361 return 0;
bce8e0c0 362}
363
364/* Compare two token lists. */
365int
366_cpp_equiv_toklists (a, b)
367 const cpp_toklist *a, *b;
368{
369 unsigned int i;
370
f80e83a9 371 if (a->tokens_used != b->tokens_used
372 || a->flags != b->flags
373 || a->paramc != b->paramc)
bce8e0c0 374 return 0;
375
376 for (i = 0; i < a->tokens_used; i++)
377 if (! _cpp_equiv_tokens (&a->tokens[i], &b->tokens[i]))
378 return 0;
379 return 1;
380}
381
f80e83a9 382/* Utility routine:
2c63d6c8 383
76faa4c0 384 Compares, the token TOKEN to the NUL-terminated string STRING.
385 TOKEN must be a CPP_NAME. Returns 1 for equal, 0 for unequal. */
bce8e0c0 386
f80e83a9 387int
76faa4c0 388cpp_ideq (token, string)
389 const cpp_token *token;
f80e83a9 390 const char *string;
391{
76faa4c0 392 if (token->type != CPP_NAME)
f80e83a9 393 return 0;
76faa4c0 394
395 return !ustrcmp (token->val.node->name, (const U_CHAR *)string);
bce8e0c0 396}
50fd6b48 397
338fa5f7 398/* Call when meeting a newline. Returns the character after the newline
399 (or carriage-return newline combination), or EOF. */
400static cppchar_t
401handle_newline (buffer, newline_char)
402 cpp_buffer *buffer;
403 cppchar_t newline_char;
404{
405 cppchar_t next = EOF;
406
407 buffer->col_adjust = 0;
408 buffer->lineno++;
409 buffer->line_base = buffer->cur;
410
411 /* Handle CR-LF and LF-CR combinations, get the next character. */
412 if (buffer->cur < buffer->rlimit)
413 {
414 next = *buffer->cur++;
415 if (next + newline_char == '\r' + '\n')
416 {
417 buffer->line_base = buffer->cur;
418 if (buffer->cur < buffer->rlimit)
419 next = *buffer->cur++;
420 else
421 next = EOF;
422 }
423 }
424
425 buffer->read_ahead = next;
426 return next;
427}
428
429/* Subroutine of skip_escaped_newlines; called when a trigraph is
430 encountered. It warns if necessary, and returns true if the
431 trigraph should be honoured. FROM_CHAR is the third character of a
432 trigraph, and presumed to be the previous character for position
433 reporting. */
0578f103 434static int
338fa5f7 435trigraph_ok (pfile, from_char)
0578f103 436 cpp_reader *pfile;
338fa5f7 437 cppchar_t from_char;
0578f103 438{
f80e83a9 439 int accept = CPP_OPTION (pfile, trigraphs);
440
f669338a 441 /* Don't warn about trigraphs in comments. */
442 if (CPP_OPTION (pfile, warn_trigraphs) && !pfile->state.lexing_comment)
0578f103 443 {
338fa5f7 444 cpp_buffer *buffer = pfile->buffer;
f80e83a9 445 if (accept)
338fa5f7 446 cpp_warning_with_line (pfile, buffer->lineno, CPP_BUF_COL (buffer) - 2,
f80e83a9 447 "trigraph ??%c converted to %c",
338fa5f7 448 (int) from_char,
449 (int) _cpp_trigraph_map[from_char]);
0578f103 450 else
338fa5f7 451 cpp_warning_with_line (pfile, buffer->lineno, CPP_BUF_COL (buffer) - 2,
452 "trigraph ??%c ignored", (int) from_char);
0578f103 453 }
338fa5f7 454
f80e83a9 455 return accept;
0578f103 456}
457
338fa5f7 458/* Assumes local variables buffer and result. */
459#define ACCEPT_CHAR(t) \
460 do { result->type = t; buffer->read_ahead = EOF; } while (0)
461
462/* When we move to multibyte character sets, add to these something
463 that saves and restores the state of the multibyte conversion
464 library. This probably involves saving and restoring a "cookie".
465 In the case of glibc it is an 8-byte structure, so is not a high
466 overhead operation. In any case, it's out of the fast path. */
467#define SAVE_STATE() do { saved_cur = buffer->cur; } while (0)
468#define RESTORE_STATE() do { buffer->cur = saved_cur; } while (0)
469
470/* Skips any escaped newlines introduced by NEXT, which is either a
471 '?' or a '\\'. Returns the next character, which will also have
472 been placed in buffer->read_ahead. */
473static cppchar_t
474skip_escaped_newlines (buffer, next)
475 cpp_buffer *buffer;
476 cppchar_t next;
0578f103 477{
338fa5f7 478 cppchar_t next1;
479 const unsigned char *saved_cur;
480 int space;
f80e83a9 481
338fa5f7 482 do
f80e83a9 483 {
338fa5f7 484 if (buffer->cur == buffer->rlimit)
485 break;
486
487 SAVE_STATE ();
488 if (next == '?')
489 {
490 next1 = *buffer->cur++;
491 if (next1 != '?' || buffer->cur == buffer->rlimit)
492 {
493 RESTORE_STATE ();
494 break;
495 }
f80e83a9 496
338fa5f7 497 next1 = *buffer->cur++;
498 if (!_cpp_trigraph_map[next1] || !trigraph_ok (buffer->pfile, next1))
499 {
500 RESTORE_STATE ();
501 break;
502 }
0578f103 503
338fa5f7 504 /* We have a full trigraph here. */
505 next = _cpp_trigraph_map[next1];
506 if (next != '\\' || buffer->cur == buffer->rlimit)
507 break;
508 SAVE_STATE ();
509 }
510
511 /* We have a backslash, and room for at least one more character. */
512 space = 0;
513 do
514 {
515 next1 = *buffer->cur++;
516 if (!is_nvspace (next1))
517 break;
518 space = 1;
519 }
520 while (buffer->cur < buffer->rlimit);
521
522 if (!is_vspace (next1))
523 {
524 RESTORE_STATE ();
525 break;
526 }
0578f103 527
338fa5f7 528 if (space)
529 cpp_warning (buffer->pfile,
530 "backslash and newline separated by space");
531
532 next = handle_newline (buffer, next1);
533 if (next == EOF)
534 cpp_pedwarn (buffer->pfile, "backslash-newline at end of file");
f80e83a9 535 }
338fa5f7 536 while (next == '\\' || next == '?');
0578f103 537
338fa5f7 538 buffer->read_ahead = next;
539 return next;
0578f103 540}
541
338fa5f7 542/* Obtain the next character, after trigraph conversion and skipping
543 an arbitrary string of escaped newlines. The common case of no
544 trigraphs or escaped newlines falls through quickly. */
545static cppchar_t
546get_effective_char (buffer)
547 cpp_buffer *buffer;
852d1b04 548{
338fa5f7 549 cppchar_t next = EOF;
550
551 if (buffer->cur < buffer->rlimit)
552 {
553 next = *buffer->cur++;
554
555 /* '?' can introduce trigraphs (and therefore backslash); '\\'
556 can introduce escaped newlines, which we want to skip, or
557 UCNs, which, depending upon lexer state, we will handle in
558 the future. */
559 if (next == '?' || next == '\\')
560 next = skip_escaped_newlines (buffer, next);
561 }
562
563 buffer->read_ahead = next;
564 return next;
852d1b04 565}
566
338fa5f7 567/* Skip a C-style block comment. We find the end of the comment by
568 seeing if an asterisk is before every '/' we encounter. Returns
569 non-zero if comment terminated by EOF, zero otherwise. */
f80e83a9 570static int
571skip_block_comment (pfile)
0578f103 572 cpp_reader *pfile;
573{
f80e83a9 574 cpp_buffer *buffer = pfile->buffer;
338fa5f7 575 cppchar_t c = EOF, prevc;
576
f669338a 577 pfile->state.lexing_comment = 1;
338fa5f7 578 while (buffer->cur != buffer->rlimit)
0578f103 579 {
338fa5f7 580 prevc = c, c = *buffer->cur++;
581
582 next_char:
583 /* FIXME: For speed, create a new character class of characters
584 of no interest inside block comments. */
585 if (c == '?' || c == '\\')
586 c = skip_escaped_newlines (buffer, c);
f80e83a9 587
338fa5f7 588 /* People like decorating comments with '*', so check for '/'
589 instead for efficiency. */
f80e83a9 590 if (c == '/')
0578f103 591 {
338fa5f7 592 if (prevc == '*')
593 break;
f80e83a9 594
338fa5f7 595 /* Warn about potential nested comments, but not if the '/'
596 comes immediately before the true comment delimeter.
f80e83a9 597 Don't bother to get it right across escaped newlines. */
338fa5f7 598 if (CPP_OPTION (pfile, warn_comments)
599 && buffer->cur != buffer->rlimit)
0578f103 600 {
338fa5f7 601 prevc = c, c = *buffer->cur++;
602 if (c == '*' && buffer->cur != buffer->rlimit)
603 {
604 prevc = c, c = *buffer->cur++;
605 if (c != '/')
606 cpp_warning_with_line (pfile, CPP_BUF_LINE (buffer),
607 CPP_BUF_COL (buffer),
608 "\"/*\" within comment");
609 }
610 goto next_char;
0578f103 611 }
0578f103 612 }
78719282 613 else if (is_vspace (c))
0578f103 614 {
338fa5f7 615 prevc = c, c = handle_newline (buffer, c);
616 goto next_char;
0578f103 617 }
b86584f6 618 else if (c == '\t')
338fa5f7 619 adjust_column (pfile);
0578f103 620 }
f80e83a9 621
f669338a 622 pfile->state.lexing_comment = 0;
338fa5f7 623 buffer->read_ahead = EOF;
624 return c != '/' || prevc != '*';
0578f103 625}
626
241e762e 627/* Skip a C++ line comment. Handles escaped newlines. Returns
338fa5f7 628 non-zero if a multiline comment. The following new line, if any,
629 is left in buffer->read_ahead. */
f80e83a9 630static int
f669338a 631skip_line_comment (pfile)
632 cpp_reader *pfile;
0578f103 633{
f669338a 634 cpp_buffer *buffer = pfile->buffer;
338fa5f7 635 unsigned int orig_lineno = buffer->lineno;
636 cppchar_t c;
f80e83a9 637
f669338a 638 pfile->state.lexing_comment = 1;
338fa5f7 639 do
f80e83a9 640 {
338fa5f7 641 c = EOF;
642 if (buffer->cur == buffer->rlimit)
643 break;
f80e83a9 644
338fa5f7 645 c = *buffer->cur++;
646 if (c == '?' || c == '\\')
647 c = skip_escaped_newlines (buffer, c);
f80e83a9 648 }
338fa5f7 649 while (!is_vspace (c));
0578f103 650
f669338a 651 pfile->state.lexing_comment = 0;
338fa5f7 652 buffer->read_ahead = c; /* Leave any newline for caller. */
653 return orig_lineno != buffer->lineno;
f80e83a9 654}
0578f103 655
338fa5f7 656/* pfile->buffer->cur is one beyond the \t character. Update
657 col_adjust so we track the column correctly. */
b86584f6 658static void
338fa5f7 659adjust_column (pfile)
b86584f6 660 cpp_reader *pfile;
b86584f6 661{
338fa5f7 662 cpp_buffer *buffer = pfile->buffer;
663 unsigned int col = CPP_BUF_COL (buffer) - 1; /* Zero-based column. */
b86584f6 664
665 /* Round it up to multiple of the tabstop, but subtract 1 since the
666 tab itself occupies a character position. */
338fa5f7 667 buffer->col_adjust += (CPP_OPTION (pfile, tabstop)
668 - col % CPP_OPTION (pfile, tabstop)) - 1;
b86584f6 669}
670
338fa5f7 671/* Skips whitespace, saving the next non-whitespace character.
672 Adjusts pfile->col_adjust to account for tabs. Without this,
673 tokens might be assigned an incorrect column. */
f80e83a9 674static void
338fa5f7 675skip_whitespace (pfile, c)
f80e83a9 676 cpp_reader *pfile;
338fa5f7 677 cppchar_t c;
f80e83a9 678{
679 cpp_buffer *buffer = pfile->buffer;
338fa5f7 680 unsigned int warned = 0;
0578f103 681
338fa5f7 682 do
f80e83a9 683 {
78719282 684 /* Horizontal space always OK. */
685 if (c == ' ')
338fa5f7 686 ;
78719282 687 else if (c == '\t')
338fa5f7 688 adjust_column (pfile);
689 /* Just \f \v or \0 left. */
78719282 690 else if (c == '\0')
f80e83a9 691 {
78719282 692 if (!warned)
338fa5f7 693 {
694 cpp_warning (pfile, "null character(s) ignored");
695 warned = 1;
696 }
0578f103 697 }
338fa5f7 698 else if (IN_DIRECTIVE (pfile) && CPP_PEDANTIC (pfile))
78719282 699 cpp_pedwarn_with_line (pfile, CPP_BUF_LINE (buffer),
700 CPP_BUF_COL (buffer),
701 "%s in preprocessing directive",
702 c == '\f' ? "form feed" : "vertical tab");
338fa5f7 703
704 c = EOF;
705 if (buffer->cur == buffer->rlimit)
706 break;
707 c = *buffer->cur++;
0578f103 708 }
338fa5f7 709 /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
710 while (is_nvspace (c));
711
712 /* Remember the next character. */
713 buffer->read_ahead = c;
f80e83a9 714}
0578f103 715
338fa5f7 716/* Parse an identifier, skipping embedded backslash-newlines.
717 Calculate the hash value of the token while parsing, for improved
718 performance. The hashing algorithm *must* match cpp_lookup(). */
719
720static cpp_hashnode *
721parse_identifier (pfile, c)
0578f103 722 cpp_reader *pfile;
338fa5f7 723 cppchar_t c;
0578f103 724{
338fa5f7 725 cpp_buffer *buffer = pfile->buffer;
726 unsigned int r = 0, saw_dollar = 0;
727 unsigned int orig_used = pfile->token_list.name_used;
f80e83a9 728
338fa5f7 729 do
f80e83a9 730 {
338fa5f7 731 do
f80e83a9 732 {
338fa5f7 733 if (pfile->token_list.name_used == pfile->token_list.name_cap)
734 _cpp_expand_name_space (&pfile->token_list,
735 pfile->token_list.name_used + 256);
736 pfile->token_list.namebuf[pfile->token_list.name_used++] = c;
737 r = HASHSTEP (r, c);
0578f103 738
338fa5f7 739 if (c == '$')
740 saw_dollar++;
71aa9da4 741
338fa5f7 742 c = EOF;
743 if (buffer->cur == buffer->rlimit)
744 break;
71aa9da4 745
338fa5f7 746 c = *buffer->cur++;
747 }
748 while (is_idchar (c));
71aa9da4 749
338fa5f7 750 /* Potential escaped newline? */
751 if (c != '?' && c != '\\')
752 break;
753 c = skip_escaped_newlines (buffer, c);
f80e83a9 754 }
338fa5f7 755 while (is_idchar (c));
756
757 /* $ is not a identifier character in the standard, but is commonly
758 accepted as an extension. Don't warn about it in skipped
759 conditional blocks. */
760 if (saw_dollar && CPP_PEDANTIC (pfile) && ! pfile->skipping)
761 cpp_pedwarn (pfile, "'$' character(s) in identifier");
762
763 /* Remember the next character. */
764 buffer->read_ahead = c;
765 return _cpp_lookup_with_hash (pfile, &pfile->token_list.namebuf[orig_used],
766 pfile->token_list.name_used - orig_used, r);
0578f103 767}
768
338fa5f7 769/* Parse a number, skipping embedded backslash-newlines. */
0578f103 770static void
338fa5f7 771parse_number (pfile, number, c)
0578f103 772 cpp_reader *pfile;
338fa5f7 773 cpp_string *number;
774 cppchar_t c;
0578f103 775{
338fa5f7 776 cppchar_t prevc;
f80e83a9 777 cpp_buffer *buffer = pfile->buffer;
338fa5f7 778 unsigned int orig_used = pfile->token_list.name_used;
0578f103 779
f669338a 780 /* Reserve space for a leading period. */
781 if (pfile->state.seen_dot)
782 pfile->token_list.name_used++;
783
338fa5f7 784 do
f80e83a9 785 {
338fa5f7 786 do
787 {
f669338a 788 if (pfile->token_list.name_used >= pfile->token_list.name_cap)
338fa5f7 789 _cpp_expand_name_space (&pfile->token_list,
790 pfile->token_list.name_used + 256);
791 pfile->token_list.namebuf[pfile->token_list.name_used++] = c;
792
793 prevc = c;
794 c = EOF;
795 if (buffer->cur == buffer->rlimit)
796 break;
0578f103 797
338fa5f7 798 c = *buffer->cur++;
799 }
800 while (is_numchar (c) || c == '.' || VALID_SIGN (c, prevc));
0578f103 801
338fa5f7 802 /* Potential escaped newline? */
803 if (c != '?' && c != '\\')
804 break;
805 c = skip_escaped_newlines (buffer, c);
0578f103 806 }
338fa5f7 807 while (is_numchar (c) || c == '.' || VALID_SIGN (c, prevc));
808
f669338a 809 /* Put any leading period in place, now we have the room. */
810 if (pfile->state.seen_dot)
811 pfile->token_list.namebuf[orig_used] = '.';
812
338fa5f7 813 /* Remember the next character. */
814 buffer->read_ahead = c;
852d1b04 815
338fa5f7 816 number->text = &pfile->token_list.namebuf[orig_used];
817 number->len = pfile->token_list.name_used - orig_used;
818}
819
820/* Subroutine of parse_string. Emits error for unterminated strings. */
821static void
822unterminated (pfile, line, term)
823 cpp_reader *pfile;
824 unsigned int line;
825 int term;
826{
827 cpp_error (pfile, "missing terminating %c character", term);
828
829 if (term == '\"' && pfile->mls_line && pfile->mls_line != line)
f80e83a9 830 {
338fa5f7 831 cpp_error_with_line (pfile, pfile->mls_line, pfile->mls_column,
832 "possible start of unterminated string literal");
833 pfile->mls_line = 0;
f80e83a9 834 }
0578f103 835}
836
338fa5f7 837/* Parses a string, character constant, or angle-bracketed header file
838 name. Handles embedded trigraphs and escaped newlines.
0578f103 839
338fa5f7 840 Multi-line strings are allowed, but they are deprecated within
841 directives. */
f80e83a9 842static void
338fa5f7 843parse_string (pfile, token, terminator)
0578f103 844 cpp_reader *pfile;
f80e83a9 845 cpp_token *token;
338fa5f7 846 cppchar_t terminator;
0578f103 847{
f80e83a9 848 cpp_buffer *buffer = pfile->buffer;
338fa5f7 849 unsigned int orig_used = pfile->token_list.name_used;
850 cppchar_t c;
851 unsigned int nulls = 0;
852
853 for (;;)
0578f103 854 {
338fa5f7 855 if (buffer->cur == buffer->rlimit)
856 {
857 c = EOF;
858 unterminated (pfile, token->line, terminator);
859 break;
860 }
861 c = *buffer->cur++;
862
863 have_char:
864 /* Handle trigraphs, escaped newlines etc. */
865 if (c == '?' || c == '\\')
866 c = skip_escaped_newlines (buffer, c);
0578f103 867
338fa5f7 868 if (c == terminator)
0578f103 869 {
338fa5f7 870 unsigned int u = pfile->token_list.name_used;
01f93cf7 871
338fa5f7 872 /* An odd number of consecutive backslashes represents an
873 escaped terminator. */
874 while (u > orig_used && pfile->token_list.namebuf[u - 1] == '\\')
875 u--;
876
877 if ((pfile->token_list.name_used - u) % 2 == 0)
0578f103 878 {
338fa5f7 879 c = EOF;
880 break;
0578f103 881 }
338fa5f7 882 }
883 else if (is_vspace (c))
884 {
885 /* In assembly language, silently terminate string and
886 character literals at end of line. This is a kludge
887 around not knowing where comments are. */
888 if (CPP_OPTION (pfile, lang_asm) && terminator != '>')
889 break;
0578f103 890
338fa5f7 891 /* Character constants and header names may not extend over
892 multiple lines. In Standard C, neither may strings.
893 Unfortunately, we accept multiline strings as an
894 extension. (Deprecatedly even in directives - otherwise,
895 glibc's longlong.h breaks.) */
896 if (terminator != '"')
0578f103 897 {
338fa5f7 898 unterminated (pfile, token->line, terminator);
899 break;
0578f103 900 }
0578f103 901
338fa5f7 902 if (pfile->mls_line == 0)
903 {
904 pfile->mls_line = token->line;
905 pfile->mls_column = token->col;
906 if (CPP_PEDANTIC (pfile))
907 cpp_pedwarn (pfile, "multi-line string constant");
f80e83a9 908 }
338fa5f7 909
910 handle_newline (buffer, c); /* Stores to read_ahead. */
911 c = '\n';
912 }
913 else if (c == '\0')
914 {
915 if (nulls++ == 0)
916 cpp_warning (pfile, "null character(s) preserved in literal");
0578f103 917 }
0578f103 918
338fa5f7 919 if (pfile->token_list.name_used == pfile->token_list.name_cap)
920 _cpp_expand_name_space (&pfile->token_list,
921 pfile->token_list.name_used + 256);
9fb5b53d 922
338fa5f7 923 pfile->token_list.namebuf[pfile->token_list.name_used++] = c;
924 /* If we had a new line, the next character is in read_ahead. */
925 if (c != '\n')
926 continue;
927 c = buffer->read_ahead;
928 if (c != EOF)
929 goto have_char;
0578f103 930 }
931
338fa5f7 932 buffer->read_ahead = c;
0578f103 933
338fa5f7 934 token->val.str.text = &pfile->token_list.namebuf[orig_used];
935 token->val.str.len = pfile->token_list.name_used - orig_used;
936}
f80e83a9 937
338fa5f7 938/* For output routine simplicity, the stored comment includes the
939 comment start and any terminator. */
2c63d6c8 940static void
338fa5f7 941save_comment (pfile, token, from)
942 cpp_reader *pfile;
f80e83a9 943 cpp_token *token;
944 const unsigned char *from;
2c63d6c8 945{
f80e83a9 946 unsigned char *buffer;
338fa5f7 947 unsigned int len;
948 cpp_toklist *list = &pfile->token_list;
949
950#define COMMENT_START_LEN 2
951 len = pfile->buffer->cur - from + COMMENT_START_LEN;
952 _cpp_reserve_name_space (list, len);
953 buffer = list->namebuf + list->name_used;
954 list->name_used += len;
f80e83a9 955
f80e83a9 956 token->type = CPP_COMMENT;
76faa4c0 957 token->val.str.len = len;
338fa5f7 958 token->val.str.text = buffer;
0578f103 959
338fa5f7 960 /* from[-1] is '/' or '*' depending on the comment type. */
961 *buffer++ = '/';
962 *buffer++ = from[-1];
963 memcpy (buffer, from, len - COMMENT_START_LEN);
964}
0578f103 965
f669338a 966/* Subroutine of lex_token to handle '%'. A little tricky, since we
967 want to avoid stepping back when lexing %:%X. */
338fa5f7 968static void
f669338a 969lex_percent (buffer, result)
338fa5f7 970 cpp_buffer *buffer;
971 cpp_token *result;
338fa5f7 972{
f669338a 973 cppchar_t c;
974
975 result->type = CPP_MOD;
976 /* Parsing %:%X could leave an extra character. */
977 if (buffer->extra_char == EOF)
978 c = get_effective_char (buffer);
979 else
980 {
981 c = buffer->read_ahead = buffer->extra_char;
982 buffer->extra_char = EOF;
983 }
984
985 if (c == '=')
986 ACCEPT_CHAR (CPP_MOD_EQ);
987 else if (CPP_OPTION (buffer->pfile, digraphs))
988 {
989 if (c == ':')
990 {
991 result->flags |= DIGRAPH;
992 ACCEPT_CHAR (CPP_HASH);
993 if (get_effective_char (buffer) == '%')
994 {
995 buffer->extra_char = get_effective_char (buffer);
996 if (buffer->extra_char == ':')
997 {
998 buffer->extra_char = EOF;
999 ACCEPT_CHAR (CPP_PASTE);
1000 }
1001 else
1002 /* We'll catch the extra_char when we're called back. */
1003 buffer->read_ahead = '%';
1004 }
1005 }
1006 else if (c == '>')
1007 {
1008 result->flags |= DIGRAPH;
1009 ACCEPT_CHAR (CPP_CLOSE_BRACE);
1010 }
1011 }
1012}
1013
1014/* Subroutine of lex_token to handle '.'. This is tricky, since we
1015 want to avoid stepping back when lexing '...' or '.123'. In the
1016 latter case we should also set a flag for parse_number. */
1017static void
1018lex_dot (pfile, result)
1019 cpp_reader *pfile;
1020 cpp_token *result;
1021{
1022 cpp_buffer *buffer = pfile->buffer;
1023 cppchar_t c;
1024
1025 /* Parsing ..X could leave an extra character. */
1026 if (buffer->extra_char == EOF)
1027 c = get_effective_char (buffer);
1028 else
1029 {
1030 c = buffer->read_ahead = buffer->extra_char;
1031 buffer->extra_char = EOF;
1032 }
338fa5f7 1033
f669338a 1034 /* All known character sets have 0...9 contiguous. */
1035 if (c >= '0' && c <= '9')
1036 {
1037 result->type = CPP_NUMBER;
1038 buffer->pfile->state.seen_dot = 1;
1039 parse_number (pfile, &result->val.str, c);
1040 buffer->pfile->state.seen_dot = 0;
1041 }
f80e83a9 1042 else
c4357c92 1043 {
f669338a 1044 result->type = CPP_DOT;
1045 if (c == '.')
1046 {
1047 buffer->extra_char = get_effective_char (buffer);
1048 if (buffer->extra_char == '.')
1049 {
1050 buffer->extra_char = EOF;
1051 ACCEPT_CHAR (CPP_ELLIPSIS);
1052 }
1053 else
1054 /* We'll catch the extra_char when we're called back. */
1055 buffer->read_ahead = '.';
1056 }
1057 else if (c == '*' && CPP_OPTION (pfile, cplusplus))
1058 ACCEPT_CHAR (CPP_DOT_STAR);
c4357c92 1059 }
0578f103 1060}
1061
f80e83a9 1062static void
338fa5f7 1063lex_token (pfile, result)
0578f103 1064 cpp_reader *pfile;
338fa5f7 1065 cpp_token *result;
0578f103 1066{
338fa5f7 1067 cppchar_t c;
f80e83a9 1068 cpp_buffer *buffer = pfile->buffer;
338fa5f7 1069 const unsigned char *comment_start;
0653b94e 1070
338fa5f7 1071 result->flags = 0;
1072 next_char:
1073 result->line = CPP_BUF_LINE (buffer);
1074 next_char2:
1075 result->col = CPP_BUF_COLUMN (buffer, buffer->cur);
f80e83a9 1076
338fa5f7 1077 c = buffer->read_ahead;
1078 if (c == EOF && buffer->cur < buffer->rlimit)
1079 {
1080 c = *buffer->cur++;
1081 result->col++;
1082 }
0578f103 1083
338fa5f7 1084 do_switch:
1085 buffer->read_ahead = EOF;
1086 switch (c)
0578f103 1087 {
338fa5f7 1088 case EOF:
1089 /* Non-empty files should end in a newline. Testing
1090 skip_newlines ensures we only emit the warning once. */
1091 if (buffer->cur != buffer->line_base && buffer->cur != buffer->buf
1092 && pfile->state.skip_newlines)
1093 cpp_pedwarn_with_line (pfile, buffer->lineno, CPP_BUF_COL (buffer),
1094 "no newline at end of file");
1095 result->type = CPP_EOF;
1096 break;
0578f103 1097
338fa5f7 1098 case ' ': case '\t': case '\f': case '\v': case '\0':
1099 skip_whitespace (pfile, c);
1100 result->flags |= PREV_WHITE;
1101 goto next_char2;
1102
1103 case '\n': case '\r':
1104 result->type = CPP_EOF;
1105 handle_newline (buffer, c);
1106 /* Handling here will change significantly when moving to
1107 token-at-a-time. */
1108 if (pfile->state.skip_newlines)
0578f103 1109 {
338fa5f7 1110 result->flags &= ~PREV_WHITE; /* Clear any whitespace flag. */
1111 goto next_char;
0578f103 1112 }
338fa5f7 1113 break;
732cb4c9 1114
338fa5f7 1115 case '?':
1116 case '\\':
1117 /* These could start an escaped newline, or '?' a trigraph. Let
1118 skip_escaped_newlines do all the work. */
1119 {
1120 unsigned int lineno = buffer->lineno;
1121
1122 c = skip_escaped_newlines (buffer, c);
1123 if (lineno != buffer->lineno)
1124 /* We had at least one escaped newline of some sort, and the
1125 next character is in buffer->read_ahead. Update the
1126 token's line and column. */
1127 goto next_char;
1128
1129 /* We are either the original '?' or '\\', or a trigraph. */
1130 result->type = CPP_QUERY;
1131 buffer->read_ahead = EOF;
1132 if (c == '\\')
1133 result->type = CPP_BACKSLASH;
1134 else if (c != '?')
1135 goto do_switch;
1136 }
1137 break;
732cb4c9 1138
338fa5f7 1139 case '0': case '1': case '2': case '3': case '4':
1140 case '5': case '6': case '7': case '8': case '9':
1141 result->type = CPP_NUMBER;
1142 parse_number (pfile, &result->val.str, c);
1143 break;
732cb4c9 1144
338fa5f7 1145 case '$':
1146 if (!CPP_OPTION (pfile, dollars_in_ident))
1147 goto random_char;
1148 /* Fall through... */
1149
1150 case '_':
1151 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1152 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1153 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1154 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1155 case 'y': case 'z':
1156 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1157 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1158 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1159 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1160 case 'Y': case 'Z':
1161 result->type = CPP_NAME;
1162 result->val.node = parse_identifier (pfile, c);
1163
1164 /* 'L' may introduce wide characters or strings. */
1165 if (result->val.node == pfile->spec_nodes->n_L)
1166 {
1167 c = buffer->read_ahead; /* For make_string. */
1168 if (c == '\'' || c == '"')
71aa9da4 1169 {
338fa5f7 1170 ACCEPT_CHAR (c == '"' ? CPP_WSTRING: CPP_WCHAR);
1171 goto make_string;
71aa9da4 1172 }
338fa5f7 1173 }
1174 /* Convert named operators to their proper types. */
1175 else if (result->val.node->type == T_OPERATOR)
1176 {
1177 result->flags |= NAMED_OP;
1178 result->type = result->val.node->value.code;
1179 }
1180 break;
1181
1182 case '\'':
1183 case '"':
1184 result->type = c == '"' ? CPP_STRING: CPP_CHAR;
1185 make_string:
1186 parse_string (pfile, result, c);
1187 break;
f80e83a9 1188
338fa5f7 1189 case '/':
1190 result->type = CPP_DIV;
1191 c = get_effective_char (buffer);
1192 if (c == '=')
1193 ACCEPT_CHAR (CPP_DIV_EQ);
1194 else if (c == '*')
1195 {
1196 comment_start = buffer->cur;
0578f103 1197
338fa5f7 1198 /* Skip_block_comment updates buffer->read_ahead. */
1199 if (skip_block_comment (pfile))
1200 cpp_error_with_line (pfile, result->line, result->col,
1201 "unterminated comment");
1202 if (!pfile->state.save_comments)
31674461 1203 {
338fa5f7 1204 result->flags |= PREV_WHITE;
1205 goto next_char;
31674461 1206 }
1207
338fa5f7 1208 /* Save the comment as a token in its own right. */
1209 save_comment (pfile, result, comment_start);
1210 }
1211 else if (c == '/')
1212 {
1213 /* We silently allow C++ comments in system headers,
1214 irrespective of conformance mode, because lots of
1215 broken systems do that and trying to clean it up in
1216 fixincludes is a nightmare. */
1217 if (CPP_IN_SYSTEM_HEADER (pfile))
1218 goto do_line_comment;
1219 if (CPP_OPTION (pfile, cplusplus_comments))
f80e83a9 1220 {
338fa5f7 1221 if (CPP_OPTION (pfile, c89) && CPP_PEDANTIC (pfile)
1222 && ! buffer->warned_cplusplus_comments)
f80e83a9 1223 {
338fa5f7 1224 cpp_pedwarn (pfile,
1225 "C++ style comments are not allowed in ISO C89");
1226 cpp_pedwarn (pfile,
1227 "(this will be reported only once per input file)");
1228 buffer->warned_cplusplus_comments = 1;
f80e83a9 1229 }
f80e83a9 1230
338fa5f7 1231 do_line_comment:
1232 comment_start = buffer->cur;
1233
1234 /* Skip_line_comment updates buffer->read_ahead. */
f669338a 1235 if (skip_line_comment (pfile))
338fa5f7 1236 cpp_warning_with_line (pfile, result->line, result->col,
1237 "multi-line comment");
1238
1239 if (!pfile->state.save_comments)
f80e83a9 1240 {
338fa5f7 1241 result->flags |= PREV_WHITE;
1242 goto next_char;
f80e83a9 1243 }
338fa5f7 1244
1245 /* Save the comment as a token in its own right. */
1246 save_comment (pfile, result, comment_start);
0578f103 1247 }
338fa5f7 1248 }
1249 break;
1250
1251 case '<':
1252 if (pfile->state.angled_headers)
1253 {
1254 result->type = CPP_HEADER_NAME;
1255 c = '>'; /* terminator. */
1256 goto make_string;
1257 }
0578f103 1258
338fa5f7 1259 result->type = CPP_LESS;
1260 c = get_effective_char (buffer);
1261 if (c == '=')
1262 ACCEPT_CHAR (CPP_LESS_EQ);
1263 else if (c == '<')
1264 {
1265 ACCEPT_CHAR (CPP_LSHIFT);
1266 if (get_effective_char (buffer) == '=')
1267 ACCEPT_CHAR (CPP_LSHIFT_EQ);
1268 }
1269 else if (c == '?' && CPP_OPTION (pfile, cplusplus))
1270 {
1271 ACCEPT_CHAR (CPP_MIN);
1272 if (get_effective_char (buffer) == '=')
1273 ACCEPT_CHAR (CPP_MIN_EQ);
1274 }
1275 else if (c == ':' && CPP_OPTION (pfile, digraphs))
1276 {
1277 ACCEPT_CHAR (CPP_OPEN_SQUARE);
1278 result->flags |= DIGRAPH;
1279 }
1280 else if (c == '%' && CPP_OPTION (pfile, digraphs))
1281 {
1282 ACCEPT_CHAR (CPP_OPEN_BRACE);
1283 result->flags |= DIGRAPH;
1284 }
1285 break;
1286
1287 case '>':
1288 result->type = CPP_GREATER;
1289 c = get_effective_char (buffer);
1290 if (c == '=')
1291 ACCEPT_CHAR (CPP_GREATER_EQ);
1292 else if (c == '>')
1293 {
1294 ACCEPT_CHAR (CPP_RSHIFT);
1295 if (get_effective_char (buffer) == '=')
1296 ACCEPT_CHAR (CPP_RSHIFT_EQ);
1297 }
1298 else if (c == '?' && CPP_OPTION (pfile, cplusplus))
1299 {
1300 ACCEPT_CHAR (CPP_MAX);
1301 if (get_effective_char (buffer) == '=')
1302 ACCEPT_CHAR (CPP_MAX_EQ);
1303 }
1304 break;
1305
f669338a 1306 case '%':
1307 lex_percent (buffer, result);
338fa5f7 1308 break;
1309
f669338a 1310 case '.':
1311 lex_dot (pfile, result);
338fa5f7 1312 break;
0578f103 1313
338fa5f7 1314 case '+':
1315 result->type = CPP_PLUS;
1316 c = get_effective_char (buffer);
1317 if (c == '=')
1318 ACCEPT_CHAR (CPP_PLUS_EQ);
1319 else if (c == '+')
1320 ACCEPT_CHAR (CPP_PLUS_PLUS);
1321 break;
ac0749c7 1322
338fa5f7 1323 case '-':
1324 result->type = CPP_MINUS;
1325 c = get_effective_char (buffer);
1326 if (c == '>')
1327 {
1328 ACCEPT_CHAR (CPP_DEREF);
1329 if (CPP_OPTION (pfile, cplusplus)
1330 && get_effective_char (buffer) == '*')
1331 ACCEPT_CHAR (CPP_DEREF_STAR);
1332 }
1333 else if (c == '=')
1334 ACCEPT_CHAR (CPP_MINUS_EQ);
1335 else if (c == '-')
1336 ACCEPT_CHAR (CPP_MINUS_MINUS);
1337 break;
0578f103 1338
338fa5f7 1339 case '*':
1340 result->type = CPP_MULT;
1341 if (get_effective_char (buffer) == '=')
1342 ACCEPT_CHAR (CPP_MULT_EQ);
1343 break;
ac0749c7 1344
338fa5f7 1345 case '=':
1346 result->type = CPP_EQ;
1347 if (get_effective_char (buffer) == '=')
1348 ACCEPT_CHAR (CPP_EQ_EQ);
1349 break;
c4abf88d 1350
338fa5f7 1351 case '!':
1352 result->type = CPP_NOT;
1353 if (get_effective_char (buffer) == '=')
1354 ACCEPT_CHAR (CPP_NOT_EQ);
1355 break;
0578f103 1356
338fa5f7 1357 case '&':
1358 result->type = CPP_AND;
1359 c = get_effective_char (buffer);
1360 if (c == '=')
1361 ACCEPT_CHAR (CPP_AND_EQ);
1362 else if (c == '&')
1363 ACCEPT_CHAR (CPP_AND_AND);
1364 break;
1365
1366 case '#':
1367 result->type = CPP_HASH;
1368 if (get_effective_char (buffer) == '#')
1369 ACCEPT_CHAR (CPP_PASTE);
1370 break;
0578f103 1371
338fa5f7 1372 case '|':
1373 result->type = CPP_OR;
1374 c = get_effective_char (buffer);
1375 if (c == '=')
1376 ACCEPT_CHAR (CPP_OR_EQ);
1377 else if (c == '|')
1378 ACCEPT_CHAR (CPP_OR_OR);
1379 break;
0578f103 1380
338fa5f7 1381 case '^':
1382 result->type = CPP_XOR;
1383 if (get_effective_char (buffer) == '=')
1384 ACCEPT_CHAR (CPP_XOR_EQ);
1385 break;
0578f103 1386
338fa5f7 1387 case ':':
1388 result->type = CPP_COLON;
1389 c = get_effective_char (buffer);
1390 if (c == ':' && CPP_OPTION (pfile, cplusplus))
1391 ACCEPT_CHAR (CPP_SCOPE);
1392 else if (c == '>' && CPP_OPTION (pfile, digraphs))
1393 {
1394 result->flags |= DIGRAPH;
1395 ACCEPT_CHAR (CPP_CLOSE_SQUARE);
1396 }
1397 break;
0578f103 1398
338fa5f7 1399 case '~': result->type = CPP_COMPL; break;
1400 case ',': result->type = CPP_COMMA; break;
1401 case '(': result->type = CPP_OPEN_PAREN; break;
1402 case ')': result->type = CPP_CLOSE_PAREN; break;
1403 case '[': result->type = CPP_OPEN_SQUARE; break;
1404 case ']': result->type = CPP_CLOSE_SQUARE; break;
1405 case '{': result->type = CPP_OPEN_BRACE; break;
1406 case '}': result->type = CPP_CLOSE_BRACE; break;
1407 case ';': result->type = CPP_SEMICOLON; break;
1408
1409 case '@':
1410 if (CPP_OPTION (pfile, objc))
1411 {
1412 /* In Objective C, '@' may begin keywords or strings, like
1413 @keyword or @"string". It would be nice to call
1414 get_effective_char here and test the result. However, we
1415 would then need to pass 2 characters to parse_identifier,
1416 making it ugly and slowing down its main loop. Instead,
1417 we assume we have an identifier, and recover if not. */
1418 result->type = CPP_NAME;
1419 result->val.node = parse_identifier (pfile, c);
1420 if (result->val.node->length != 1)
1421 break;
ac0749c7 1422
338fa5f7 1423 /* OK, so it wasn't an identifier. Maybe a string? */
1424 if (buffer->read_ahead == '"')
f80e83a9 1425 {
338fa5f7 1426 c = '"';
1427 ACCEPT_CHAR (CPP_OSTRING);
1428 goto make_string;
f80e83a9 1429 }
338fa5f7 1430 }
1431 goto random_char;
1432
1433 random_char:
1434 default:
1435 result->type = CPP_OTHER;
1436 result->val.aux = c;
1437 break;
1438 }
1439}
1440
1441/*
1442 * The tokenizer's main loop. Returns a token list, representing a
1443 * logical line in the input file. On EOF after some tokens have
1444 * been processed, we return immediately. Then in next call, or if
1445 * EOF occurred at the beginning of a logical line, a single CPP_EOF
1446 * token is placed in the list.
1447 */
1448
1449static void
1450lex_line (pfile, list)
1451 cpp_reader *pfile;
1452 cpp_toklist *list;
1453{
1454 unsigned int first_token;
1455 cpp_token *cur_token, *first;
1456 cpp_buffer *buffer = pfile->buffer;
0578f103 1457
338fa5f7 1458 pfile->state.in_lex_line = 1;
1459 if (pfile->buffer->cur == pfile->buffer->buf)
1460 list->flags |= BEG_OF_FILE;
1461
1462 retry:
1463 pfile->state.in_directive = 0;
1464 pfile->state.angled_headers = 0;
1465 pfile->state.skip_newlines = 1;
1466 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
1467 first_token = list->tokens_used;
1468 list->file = buffer->nominal_fname;
1469
1470 do
1471 {
1472 if (list->tokens_used >= list->tokens_cap)
1473 _cpp_expand_token_space (list, 256);
1474
1475 cur_token = list->tokens + list->tokens_used;
1476 lex_token (pfile, cur_token);
1477
1478 if (pfile->state.skip_newlines)
1479 {
1480 pfile->state.skip_newlines = 0;
1481 list->line = buffer->lineno;
1482 if (cur_token->type == CPP_HASH)
f80e83a9 1483 {
338fa5f7 1484 pfile->state.in_directive = 1;
1485 pfile->state.save_comments = 0;
1486 pfile->state.indented = cur_token->flags & PREV_WHITE;
f80e83a9 1487 }
338fa5f7 1488 /* 6.10.3.10: Within the sequence of preprocessing tokens
1489 making up the invocation of a function-like macro, new
1490 line is considered a normal white-space character. */
1491 else if (first_token != 0)
1492 cur_token->flags |= PREV_WHITE;
1493 }
1494 else if (IN_DIRECTIVE (pfile) && list->tokens_used == first_token + 1)
1495 {
1496 if (cur_token->type == CPP_NUMBER)
1497 list->directive = _cpp_check_linemarker (pfile, cur_token);
f80e83a9 1498 else
338fa5f7 1499 list->directive = _cpp_check_directive (pfile, cur_token);
f80e83a9 1500 }
cfad5579 1501
338fa5f7 1502 /* _cpp_get_line assumes list->tokens_used refers to the current
1503 token being lexed. So do this after _cpp_check_directive to
1504 get the warnings therein correct. */
1505 list->tokens_used++;
f80e83a9 1506 }
338fa5f7 1507 while (cur_token->type != CPP_EOF);
cfad5579 1508
f80e83a9 1509 /* All tokens are allocated, so the memory location is fixed. */
1510 first = &list->tokens[first_token];
338fa5f7 1511 first->flags |= BOL;
1512 pfile->first_directive_token = first;
1513
f80e83a9 1514 /* Don't complain about the null directive, nor directives in
1515 assembly source: we don't know where the comments are, and # may
1516 introduce assembler pseudo-ops. Don't complain about invalid
1517 directives in skipped conditional groups (6.10 p4). */
338fa5f7 1518 if (IN_DIRECTIVE (pfile) && !KNOWN_DIRECTIVE (list) && !pfile->skipping
1519 && !CPP_OPTION (pfile, lang_asm))
f80e83a9 1520 {
338fa5f7 1521 if (cur_token > first + 1)
1522 {
1523 if (first[1].type == CPP_NAME)
1524 cpp_error_with_line (pfile, first->line, first->col,
1525 "invalid preprocessing directive #%s",
1526 first[1].val.node->name);
1527 else
1528 cpp_error_with_line (pfile, first->line, first->col,
1529 "invalid preprocessing directive");
1530 }
0653b94e 1531
1532 /* Discard this line to prevent further errors from cc1. */
1533 _cpp_clear_toklist (list);
1534 goto retry;
f80e83a9 1535 }
1536
be6e77fe 1537 /* Drop the EOF unless really at EOF or in a directive. */
1538 if (cur_token != first && !KNOWN_DIRECTIVE (list)
1539 && pfile->done_initializing)
1540 list->tokens_used--;
1541
338fa5f7 1542 pfile->state.in_lex_line = 0;
cfad5579 1543}
1544
f80e83a9 1545/* Write the spelling of a token TOKEN to BUFFER. The buffer must
c5ea33a8 1546 already contain the enough space to hold the token's spelling.
1547 Returns a pointer to the character after the last character
1548 written. */
ab12a39c 1549
f80e83a9 1550static unsigned char *
1551spell_token (pfile, token, buffer)
1552 cpp_reader *pfile; /* Would be nice to be rid of this... */
1553 const cpp_token *token;
1554 unsigned char *buffer;
1555{
7e842f95 1556 switch (TOKEN_SPELL (token))
f80e83a9 1557 {
1558 case SPELL_OPERATOR:
1559 {
1560 const unsigned char *spelling;
1561 unsigned char c;
ab12a39c 1562
f80e83a9 1563 if (token->flags & DIGRAPH)
524f0c40 1564 spelling = _cpp_digraph_spellings[token->type - CPP_FIRST_DIGRAPH];
31674461 1565 else if (token->flags & NAMED_OP)
1566 goto spell_ident;
f80e83a9 1567 else
7e842f95 1568 spelling = TOKEN_NAME (token);
f80e83a9 1569
1570 while ((c = *spelling++) != '\0')
1571 *buffer++ = c;
1572 }
1573 break;
ab12a39c 1574
f80e83a9 1575 case SPELL_IDENT:
31674461 1576 spell_ident:
76faa4c0 1577 memcpy (buffer, token->val.node->name, token->val.node->length);
1578 buffer += token->val.node->length;
f80e83a9 1579 break;
ab12a39c 1580
f80e83a9 1581 case SPELL_STRING:
1582 {
71aa9da4 1583 int left, right, tag;
1584 switch (token->type)
1585 {
1586 case CPP_STRING: left = '"'; right = '"'; tag = '\0'; break;
1587 case CPP_WSTRING: left = '"'; right = '"'; tag = 'L'; break;
1588 case CPP_OSTRING: left = '"'; right = '"'; tag = '@'; break;
1589 case CPP_CHAR: left = '\''; right = '\''; tag = '\0'; break;
1590 case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
1591 case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
1592 default: left = '\0'; right = '\0'; tag = '\0'; break;
1593 }
1594 if (tag) *buffer++ = tag;
1595 if (left) *buffer++ = left;
76faa4c0 1596 memcpy (buffer, token->val.str.text, token->val.str.len);
1597 buffer += token->val.str.len;
71aa9da4 1598 if (right) *buffer++ = right;
f80e83a9 1599 }
1600 break;
ab12a39c 1601
f80e83a9 1602 case SPELL_CHAR:
1603 *buffer++ = token->val.aux;
1604 break;
ab12a39c 1605
f80e83a9 1606 case SPELL_NONE:
7e842f95 1607 cpp_ice (pfile, "Unspellable token %s", TOKEN_NAME (token));
f80e83a9 1608 break;
1609 }
ab12a39c 1610
f80e83a9 1611 return buffer;
1612}
ab12a39c 1613
fdf3a98f 1614/* Macro expansion algorithm.
1615
1616Macro expansion is implemented by a single-pass algorithm; there are
1617no rescan passes involved. cpp_get_token expands just enough to be
1618able to return a token to the caller, a consequence is that when it
1619returns the preprocessor can be in a state of mid-expansion. The
1620algorithm does not work by fully expanding a macro invocation into
1621some kind of token list, and then returning them one by one.
1622
1623Our expansion state is recorded in a context stack. We start out with
1624a single context on the stack, let's call it base context. This
1625consists of the token list returned by lex_line that forms the next
1626logical line in the source file.
1627
1628The current level in the context stack is stored in the cur_context
1629member of the cpp_reader structure. The context it references keeps,
1630amongst other things, a count of how many tokens form that context and
1631our position within those tokens.
1632
1633Fundamentally, calling cpp_get_token will return the next token from
1634the current context. If we're at the end of the current context, that
1635context is popped from the stack first, unless it is the base context,
1636in which case the next logical line is lexed from the source file.
1637
1638However, before returning the token, if it is a CPP_NAME token
1639_cpp_get_token checks to see if it is a macro and if it is enabled.
1640Each time it encounters a macro name, it calls push_macro_context.
1641This function checks that the macro should be expanded (with
1642is_macro_enabled), and if so pushes a new macro context on the stack
1643which becomes the current context. It then loops back to read the
1644first token of the macro context.
1645
1646A macro context basically consists of the token list representing the
1647macro's replacement list, which was saved in the hash table by
1648save_macro_expansion when its #define statement was parsed. If the
1649macro is function-like, it also contains the tokens that form the
1650arguments to the macro. I say more about macro arguments below, but
1651for now just saying that each argument is a set of pointers to tokens
1652is enough.
1653
1654When taking tokens from a macro context, we may get a CPP_MACRO_ARG
1655token. This represents an argument passed to the macro, with the
1656argument number stored in the token's AUX field. The argument should
1657be substituted, this is achieved by pushing an "argument context". An
1658argument context is just refers to the tokens forming the argument,
1659which are obtained directly from the macro context. The STRINGIFY
1660flag on a CPP_MACRO_ARG token indicates that the argument should be
1661stringified.
1662
1663Here's a few simple rules the context stack obeys:-
1664
1665 1) The lex_line token list is always context zero.
1666
1667 2) Context 1, if it exists, must be a macro context.
1668
1669 3) An argument context can only appear above a macro context.
1670
1671 4) A macro context can appear above the base context, another macro
1672 context, or an argument context.
1673
1674 5) These imply that the minimal level of an argument context is 2.
1675
1676The only tricky thing left is ensuring that macros are enabled and
1677disabled correctly. The algorithm controls macro expansion by the
1678level of the context a token is taken from in the context stack. If a
1679token is taken from a level equal to no_expand_level (a member of
1680struct cpp_reader), no expansion is performed.
1681
1682When popping a context off the stack, if no_expand_level equals the
1683level of the popped context, it is reduced by one to match the new
1684context level, so that expansion is still disabled. It does not
1685increase if a context is pushed, though. It starts out life as
1686UINT_MAX, which has the effect that initially macro expansion is
1687enabled. I explain how this mechanism works below.
1688
1689The standard requires:-
1690
1691 1) Arguments to be fully expanded before substitution.
1692
1693 2) Stringified arguments to not be expanded, nor the tokens
1694 immediately surrounding a ## operator.
1695
1696 3) Continual rescanning until there are no more macros left to
1697 replace.
1698
1699 4) Once a macro has been expanded in stage 1) or 3), it cannot be
1700 expanded again during later rescans. This prevents infinite
1701 recursion.
1702
1703The first thing to observe is that stage 3) is mostly redundant.
1704Since a macro is disabled once it has been expanded, how can a rescan
1705find an unexpanded macro name? There are only two cases where this is
1706possible:-
1707
1708 a) If the macro name results from a token paste operation.
1709
1710 b) If the macro in question is a function-like macro that hasn't
1711 already been expanded because previously there was not the required
1712 '(' token immediately following it. This is only possible when an
1713 argument is substituted, and after substitution the last token of
1714 the argument can bind with a parenthesis appearing in the tokens
1715 following the substitution. Note that if the '(' appears within the
1716 argument, the ')' must too, as expanding macro arguments cannot
1717 "suck in" tokens outside the argument.
1718
1719So we tackle this as follows. When parsing the macro invocation for
1720arguments, we record the tokens forming each argument as a list of
1721pointers to those tokens. We do not expand any tokens that are "raw",
1722i.e. directly from the macro invocation, but other tokens that come
1723from (nested) argument substitution are fully expanded.
1724
1725This is achieved by setting the no_expand_level to that of the macro
1726invocation. A CPP_MACRO_ARG token never appears in the list of tokens
1727forming an argument, because parse_args (indirectly) calls
1728get_raw_token which automatically pushes argument contexts and traces
1729into them. Since these contexts are at a higher level than the
1730no_expand_level, they get fully macro expanded.
1731
1732"Raw" and non-raw tokens are separated in arguments by null pointers,
1733with the policy that the initial state of an argument is raw. If the
1734first token is not raw, it should be preceded by a null pointer. When
1735tracing through the tokens of an argument context, each time
1736get_raw_token encounters a null pointer, it toggles the flag
1737CONTEXT_RAW.
1738
1739This flag, when set, indicates to is_macro_disabled that we are
1740reading raw tokens which should be macro-expanded. Similarly, if
1741clear, is_macro_disabled suppresses re-expansion.
1742
1743It's probably time for an example.
1744
1745#define hash #
1746#define str(x) #x
1747#define xstr(y) str(y hash)
1748str(hash) // "hash"
1749xstr(hash) // "# hash"
1750
1751In the invocation of str, parse_args turns off macro expansion and so
1752parses the argument as <hash>. This is the only token (pointer)
1753passed as the argument to str. Since <hash> is raw there is no need
1754for an initial null pointer. stringify_arg is called from
1755get_raw_token when tracing through the expansion of str, since the
1756argument has the STRINGIFY flag set. stringify_arg turns off
1757macro_expansion by setting the no_expand_level to that of the argument
1758context. Thus it gets the token <hash> and stringifies it to "hash"
1759correctly.
1760
1761Similary xstr is passed <hash>. However, when parse_args is parsing
1762the invocation of str() in xstr's expansion, get_raw_token encounters
1763a CPP_MACRO_ARG token for y. Transparently to parse_args, it pushes
1764an argument context, and enters the tokens of the argument,
1765i.e. <hash>. This is at a higher context level than parse_args
1766disabled, and so is_macro_disabled permits expansion of it and a macro
1767context is pushed on top of the argument context. This contains the
1768<#> token, and the end result is that <hash> is macro expanded.
1769However, after popping off the argument context, the <hash> of xstr's
1770expansion does not get macro expanded because we're back at the
1771no_expand_level. The end result is that the argument passed to str is
1772<NULL> <#> <NULL> <hash>. Note the nulls - policy is we start off
1773raw, <#> is not raw, but then <hash> is.
1774
1775*/
ab12a39c 1776
f80e83a9 1777
1778/* Free the storage allocated for macro arguments. */
1779static void
1780free_macro_args (args)
1781 macro_args *args;
6060326b 1782{
f80e83a9 1783 if (args->tokens)
deb356cf 1784 free ((PTR) args->tokens);
f80e83a9 1785 free (args->ends);
1786 free (args);
6060326b 1787}
1788
f80e83a9 1789/* Determines if a macro has been already used (and is therefore
1790 disabled). */
6060326b 1791static int
f80e83a9 1792is_macro_disabled (pfile, expansion, token)
6060326b 1793 cpp_reader *pfile;
f80e83a9 1794 const cpp_toklist *expansion;
1795 const cpp_token *token;
6060326b 1796{
f80e83a9 1797 cpp_context *context = CURRENT_CONTEXT (pfile);
1798
1799 /* Arguments on either side of ## are inserted in place without
1800 macro expansion (6.10.3.3.2). Conceptually, any macro expansion
1801 occurs during a later rescan pass. The effect is that we expand
1802 iff we would as part of the macro's expansion list, so we should
1803 drop to the macro's context. */
1804 if (IS_ARG_CONTEXT (context))
6060326b 1805 {
f80e83a9 1806 if (token->flags & PASTED)
1807 context--;
1808 else if (!(context->flags & CONTEXT_RAW))
1809 return 1;
1810 else if (context->flags & (CONTEXT_PASTEL | CONTEXT_PASTER))
1811 context--;
6060326b 1812 }
6060326b 1813
f80e83a9 1814 /* Have we already used this macro? */
1815 while (context->level > 0)
6060326b 1816 {
f80e83a9 1817 if (!IS_ARG_CONTEXT (context) && context->u.list == expansion)
1818 return 1;
1819 /* Raw argument tokens are judged based on the token list they
1820 came from. */
1821 if (context->flags & CONTEXT_RAW)
1822 context = pfile->contexts + context->level;
1823 else
1824 context--;
1825 }
6060326b 1826
f80e83a9 1827 /* Function-like macros may be disabled if the '(' is not in the
1828 current context. We check this without disrupting the context
1829 stack. */
1830 if (expansion->paramc >= 0)
1831 {
1832 const cpp_token *next;
1833 unsigned int prev_nme;
6060326b 1834
f80e83a9 1835 context = CURRENT_CONTEXT (pfile);
1836 /* Drop down any contexts we're at the end of: the '(' may
1837 appear in lower macro expansions, or in the rest of the file. */
1838 while (context->posn == context->count && context > pfile->contexts)
1839 {
1840 context--;
1841 /* If we matched, we are disabled, as we appear in the
1842 expansion of each macro we meet. */
1843 if (!IS_ARG_CONTEXT (context) && context->u.list == expansion)
1844 return 1;
1845 }
6060326b 1846
f80e83a9 1847 prev_nme = pfile->no_expand_level;
1848 pfile->no_expand_level = context - pfile->contexts;
deb356cf 1849 next = _cpp_get_token (pfile);
f80e83a9 1850 restore_macro_expansion (pfile, prev_nme);
1851 if (next->type != CPP_OPEN_PAREN)
1852 {
1853 _cpp_push_token (pfile, next);
1449850f 1854 if (CPP_WTRADITIONAL (pfile))
f80e83a9 1855 cpp_warning (pfile,
31674461 1856 "function macro %s must be used with arguments in traditional C",
1857 token->val.node->name);
f80e83a9 1858 return 1;
1859 }
6060326b 1860 }
6060326b 1861
f80e83a9 1862 return 0;
6060326b 1863}
1864
f80e83a9 1865/* Add a token to the set of tokens forming the arguments to the macro
1866 being parsed in parse_args. */
1867static void
1868save_token (args, token)
1869 macro_args *args;
1870 const cpp_token *token;
6060326b 1871{
f80e83a9 1872 if (args->used == args->capacity)
1873 {
1874 args->capacity += args->capacity + 100;
1875 args->tokens = (const cpp_token **)
deb356cf 1876 xrealloc ((PTR) args->tokens,
1877 args->capacity * sizeof (const cpp_token *));
f80e83a9 1878 }
1879 args->tokens[args->used++] = token;
6060326b 1880}
1881
f80e83a9 1882/* Take and save raw tokens until we finish one argument. Empty
1883 arguments are saved as a single CPP_PLACEMARKER token. */
1884static const cpp_token *
1885parse_arg (pfile, var_args, paren_context, args, pcount)
6060326b 1886 cpp_reader *pfile;
f80e83a9 1887 int var_args;
1888 unsigned int paren_context;
1889 macro_args *args;
1890 unsigned int *pcount;
6060326b 1891{
f80e83a9 1892 const cpp_token *token;
1893 unsigned int paren = 0, count = 0;
1894 int raw, was_raw = 1;
6060326b 1895
f80e83a9 1896 for (count = 0;; count++)
6060326b 1897 {
deb356cf 1898 token = _cpp_get_token (pfile);
6060326b 1899
f80e83a9 1900 switch (token->type)
6060326b 1901 {
f80e83a9 1902 default:
1903 break;
6060326b 1904
f80e83a9 1905 case CPP_OPEN_PAREN:
1906 paren++;
1907 break;
1908
1909 case CPP_CLOSE_PAREN:
1910 if (paren-- != 0)
1911 break;
1912 goto out;
1913
1914 case CPP_COMMA:
1915 /* Commas are not terminators within parantheses or var_args. */
1916 if (paren || var_args)
1917 break;
1918 goto out;
1919
1920 case CPP_EOF: /* Error reported by caller. */
1921 goto out;
6060326b 1922 }
6060326b 1923
f80e83a9 1924 raw = pfile->cur_context <= paren_context;
1925 if (raw != was_raw)
1926 {
1927 was_raw = raw;
1928 save_token (args, 0);
1929 count++;
6060326b 1930 }
f80e83a9 1931 save_token (args, token);
6060326b 1932 }
6060326b 1933
1934 out:
f80e83a9 1935 if (count == 0)
1936 {
1937 /* Duplicate the placemarker. Then we can set its flags and
1938 position and safely be using more than one. */
1939 save_token (args, duplicate_token (pfile, &placemarker_token));
1940 count++;
1941 }
1942
1943 *pcount = count;
1944 return token;
6060326b 1945}
1946
f80e83a9 1947/* This macro returns true if the argument starting at offset O of arglist
1948 A is empty - that is, it's either a single PLACEMARKER token, or a null
1949 pointer followed by a PLACEMARKER. */
1950
1951#define empty_argument(A, O) \
1952 ((A)->tokens[O] ? (A)->tokens[O]->type == CPP_PLACEMARKER \
1953 : (A)->tokens[(O)+1]->type == CPP_PLACEMARKER)
1954
1955/* Parse the arguments making up a macro invocation. Nested arguments
1956 are automatically macro expanded, but immediate macros are not
1957 expanded; this enables e.g. operator # to work correctly. Returns
1958 non-zero on error. */
6060326b 1959static int
f80e83a9 1960parse_args (pfile, hp, args)
6060326b 1961 cpp_reader *pfile;
f80e83a9 1962 cpp_hashnode *hp;
1963 macro_args *args;
6060326b 1964{
f80e83a9 1965 const cpp_token *token;
1966 const cpp_toklist *macro;
1967 unsigned int total = 0;
1968 unsigned int paren_context = pfile->cur_context;
1969 int argc = 0;
1970
1971 macro = hp->value.expansion;
1972 do
6060326b 1973 {
f80e83a9 1974 unsigned int count;
6060326b 1975
f80e83a9 1976 token = parse_arg (pfile, (argc + 1 == macro->paramc
1977 && (macro->flags & VAR_ARGS)),
1978 paren_context, args, &count);
1979 if (argc < macro->paramc)
6060326b 1980 {
f80e83a9 1981 total += count;
1982 args->ends[argc] = total;
6060326b 1983 }
f80e83a9 1984 argc++;
6060326b 1985 }
f80e83a9 1986 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
6060326b 1987
f80e83a9 1988 if (token->type == CPP_EOF)
1989 {
31674461 1990 cpp_error(pfile, "unterminated argument list for macro \"%s\"", hp->name);
f80e83a9 1991 return 1;
1992 }
1993 else if (argc < macro->paramc)
1994 {
1995 /* A rest argument is allowed to not appear in the invocation at all.
1996 e.g. #define debug(format, args...) ...
1997 debug("string");
1998 This is exactly the same as if the rest argument had received no
57ba19ba 1999 tokens - debug("string",); This extension is deprecated. */
35677230 2000
2001 if (argc + 1 == macro->paramc && (macro->flags & VAR_ARGS))
f80e83a9 2002 {
2003 /* Duplicate the placemarker. Then we can set its flags and
2004 position and safely be using more than one. */
35677230 2005 cpp_token *pm = duplicate_token (pfile, &placemarker_token);
2006 pm->flags = VOID_REST;
2007 save_token (args, pm);
f80e83a9 2008 args->ends[argc] = total + 1;
35677230 2009
2010 if (CPP_OPTION (pfile, c99) && CPP_PEDANTIC (pfile))
2011 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
2012
f80e83a9 2013 return 0;
2014 }
2015 else
2016 {
31674461 2017 cpp_error (pfile, "not enough arguments for macro \"%s\"", hp->name);
f80e83a9 2018 return 1;
2019 }
2020 }
2021 /* An empty argument to an empty function-like macro is fine. */
2022 else if (argc > macro->paramc
2023 && !(macro->paramc == 0 && argc == 1 && empty_argument (args, 0)))
2024 {
31674461 2025 cpp_error (pfile, "too many arguments for macro \"%s\"", hp->name);
f80e83a9 2026 return 1;
2027 }
6060326b 2028
f80e83a9 2029 return 0;
2030}
2031
2032/* Adds backslashes before all backslashes and double quotes appearing
2033 in strings. Non-printable characters are converted to octal. */
2034static U_CHAR *
2035quote_string (dest, src, len)
2036 U_CHAR *dest;
2037 const U_CHAR *src;
2038 unsigned int len;
2039{
2040 while (len--)
6060326b 2041 {
f80e83a9 2042 U_CHAR c = *src++;
6060326b 2043
f80e83a9 2044 if (c == '\\' || c == '"')
3b304865 2045 {
f80e83a9 2046 *dest++ = '\\';
2047 *dest++ = c;
2048 }
2049 else
2050 {
2051 if (ISPRINT (c))
2052 *dest++ = c;
2053 else
2054 {
2055 sprintf ((char *) dest, "\\%03o", c);
2056 dest += 4;
2057 }
3b304865 2058 }
6060326b 2059 }
6060326b 2060
f80e83a9 2061 return dest;
6060326b 2062}
2063
f80e83a9 2064/* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a
2065 CPP_STRING token containing TEXT in quoted form. */
2066static cpp_token *
2067make_string_token (token, text, len)
2068 cpp_token *token;
2069 const U_CHAR *text;
2070 unsigned int len;
2071{
2072 U_CHAR *buf;
2073
2074 buf = (U_CHAR *) xmalloc (len * 4);
2075 token->type = CPP_STRING;
2076 token->flags = 0;
76faa4c0 2077 token->val.str.text = buf;
2078 token->val.str.len = quote_string (buf, text, len) - buf;
f80e83a9 2079 return token;
2080}
2081
2082/* Allocates and converts a temporary token to a CPP_NUMBER token,
2083 evaluating to NUMBER. */
2084static cpp_token *
2085alloc_number_token (pfile, number)
6060326b 2086 cpp_reader *pfile;
f80e83a9 2087 int number;
6060326b 2088{
f80e83a9 2089 cpp_token *result;
2090 char *buf;
2091
2092 result = get_temp_token (pfile);
2093 buf = xmalloc (20);
2094 sprintf (buf, "%d", number);
2095
2096 result->type = CPP_NUMBER;
2097 result->flags = 0;
76faa4c0 2098 result->val.str.text = (U_CHAR *) buf;
2099 result->val.str.len = strlen (buf);
f80e83a9 2100 return result;
2101}
6060326b 2102
f80e83a9 2103/* Returns a temporary token from the temporary token store of PFILE. */
2104static cpp_token *
2105get_temp_token (pfile)
2106 cpp_reader *pfile;
2107{
2108 if (pfile->temp_used == pfile->temp_alloced)
2109 {
2110 if (pfile->temp_used == pfile->temp_cap)
2111 {
2112 pfile->temp_cap += pfile->temp_cap + 20;
2113 pfile->temp_tokens = (cpp_token **) xrealloc
2114 (pfile->temp_tokens, pfile->temp_cap * sizeof (cpp_token *));
2115 }
2116 pfile->temp_tokens[pfile->temp_alloced++] = (cpp_token *) xmalloc
2117 (sizeof (cpp_token));
2118 }
6060326b 2119
f80e83a9 2120 return pfile->temp_tokens[pfile->temp_used++];
2121}
2122
2123/* Release (not free) for re-use the temporary tokens of PFILE. */
2124static void
2125release_temp_tokens (pfile)
2126 cpp_reader *pfile;
2127{
2128 while (pfile->temp_used)
6060326b 2129 {
f80e83a9 2130 cpp_token *token = pfile->temp_tokens[--pfile->temp_used];
6060326b 2131
7e842f95 2132 if (TOKEN_SPELL (token) == SPELL_STRING)
6060326b 2133 {
76faa4c0 2134 free ((char *) token->val.str.text);
2135 token->val.str.text = 0;
6060326b 2136 }
2137 }
f80e83a9 2138}
6060326b 2139
f80e83a9 2140/* Free all of PFILE's dynamically-allocated temporary tokens. */
2141void
2142_cpp_free_temp_tokens (pfile)
2143 cpp_reader *pfile;
2144{
2145 if (pfile->temp_tokens)
6060326b 2146 {
f80e83a9 2147 /* It is possible, though unlikely (looking for '(' of a funlike
2148 macro into EOF), that we haven't released the tokens yet. */
2149 release_temp_tokens (pfile);
2150 while (pfile->temp_alloced)
2151 free (pfile->temp_tokens[--pfile->temp_alloced]);
2152 free (pfile->temp_tokens);
6060326b 2153 }
2154
f80e83a9 2155 if (pfile->date)
2156 {
76faa4c0 2157 free ((char *) pfile->date->val.str.text);
f80e83a9 2158 free (pfile->date);
76faa4c0 2159 free ((char *) pfile->time->val.str.text);
f80e83a9 2160 free (pfile->time);
2161 }
6060326b 2162}
2163
f80e83a9 2164/* Copy TOKEN into a temporary token from PFILE's store. */
2165static cpp_token *
2166duplicate_token (pfile, token)
2167 cpp_reader *pfile;
2168 const cpp_token *token;
2169{
2170 cpp_token *result = get_temp_token (pfile);
6060326b 2171
f80e83a9 2172 *result = *token;
7e842f95 2173 if (TOKEN_SPELL (token) == SPELL_STRING)
f80e83a9 2174 {
76faa4c0 2175 U_CHAR *buff = (U_CHAR *) xmalloc (token->val.str.len);
2176 memcpy (buff, token->val.str.text, token->val.str.len);
2177 result->val.str.text = buff;
f80e83a9 2178 }
2179 return result;
2180}
6060326b 2181
f80e83a9 2182/* Determine whether two tokens can be pasted together, and if so,
2183 what the resulting token is. Returns CPP_EOF if the tokens cannot
2184 be pasted, or the appropriate type for the merged token if they
2185 can. */
524f0c40 2186enum cpp_ttype
2187_cpp_can_paste (pfile, token1, token2, digraph)
f80e83a9 2188 cpp_reader * pfile;
2189 const cpp_token *token1, *token2;
2190 int* digraph;
6060326b 2191{
f80e83a9 2192 enum cpp_ttype a = token1->type, b = token2->type;
2193 int cxx = CPP_OPTION (pfile, cplusplus);
6060326b 2194
31674461 2195 /* Treat named operators as if they were ordinary NAMEs. */
2196 if (token1->flags & NAMED_OP)
2197 a = CPP_NAME;
2198 if (token2->flags & NAMED_OP)
2199 b = CPP_NAME;
2200
f80e83a9 2201 if (a <= CPP_LAST_EQ && b == CPP_EQ)
2202 return a + (CPP_EQ_EQ - CPP_EQ);
6060326b 2203
f80e83a9 2204 switch (a)
6060326b 2205 {
f80e83a9 2206 case CPP_GREATER:
2207 if (b == a) return CPP_RSHIFT;
2208 if (b == CPP_QUERY && cxx) return CPP_MAX;
2209 if (b == CPP_GREATER_EQ) return CPP_RSHIFT_EQ;
2210 break;
2211 case CPP_LESS:
2212 if (b == a) return CPP_LSHIFT;
2213 if (b == CPP_QUERY && cxx) return CPP_MIN;
2214 if (b == CPP_LESS_EQ) return CPP_LSHIFT_EQ;
27fdc0b6 2215 if (CPP_OPTION (pfile, digraphs))
2216 {
2217 if (b == CPP_COLON)
2218 {*digraph = 1; return CPP_OPEN_SQUARE;} /* <: digraph */
2219 if (b == CPP_MOD)
2220 {*digraph = 1; return CPP_OPEN_BRACE;} /* <% digraph */
2221 }
f80e83a9 2222 break;
6060326b 2223
f80e83a9 2224 case CPP_PLUS: if (b == a) return CPP_PLUS_PLUS; break;
2225 case CPP_AND: if (b == a) return CPP_AND_AND; break;
2226 case CPP_OR: if (b == a) return CPP_OR_OR; break;
6060326b 2227
f80e83a9 2228 case CPP_MINUS:
2229 if (b == a) return CPP_MINUS_MINUS;
2230 if (b == CPP_GREATER) return CPP_DEREF;
2231 break;
2232 case CPP_COLON:
2233 if (b == a && cxx) return CPP_SCOPE;
27fdc0b6 2234 if (b == CPP_GREATER && CPP_OPTION (pfile, digraphs))
f80e83a9 2235 {*digraph = 1; return CPP_CLOSE_SQUARE;} /* :> digraph */
2236 break;
2237
2238 case CPP_MOD:
27fdc0b6 2239 if (CPP_OPTION (pfile, digraphs))
2240 {
2241 if (b == CPP_GREATER)
2242 {*digraph = 1; return CPP_CLOSE_BRACE;} /* %> digraph */
2243 if (b == CPP_COLON)
2244 {*digraph = 1; return CPP_HASH;} /* %: digraph */
2245 }
f80e83a9 2246 break;
2247 case CPP_DEREF:
2248 if (b == CPP_MULT && cxx) return CPP_DEREF_STAR;
2249 break;
2250 case CPP_DOT:
2251 if (b == CPP_MULT && cxx) return CPP_DOT_STAR;
2252 if (b == CPP_NUMBER) return CPP_NUMBER;
2253 break;
2254
2255 case CPP_HASH:
2256 if (b == a && (token1->flags & DIGRAPH) == (token2->flags & DIGRAPH))
2257 /* %:%: digraph */
2258 {*digraph = (token1->flags & DIGRAPH); return CPP_PASTE;}
2259 break;
2260
2261 case CPP_NAME:
2262 if (b == CPP_NAME) return CPP_NAME;
2263 if (b == CPP_NUMBER
76faa4c0 2264 && is_numstart(token2->val.str.text[0])) return CPP_NAME;
f80e83a9 2265 if (b == CPP_CHAR
76faa4c0 2266 && token1->val.node == pfile->spec_nodes->n_L) return CPP_WCHAR;
f80e83a9 2267 if (b == CPP_STRING
76faa4c0 2268 && token1->val.node == pfile->spec_nodes->n_L) return CPP_WSTRING;
f80e83a9 2269 break;
2270
2271 case CPP_NUMBER:
2272 if (b == CPP_NUMBER) return CPP_NUMBER;
2273 if (b == CPP_NAME) return CPP_NUMBER;
2274 if (b == CPP_DOT) return CPP_NUMBER;
2275 /* Numbers cannot have length zero, so this is safe. */
2276 if ((b == CPP_PLUS || b == CPP_MINUS)
76faa4c0 2277 && VALID_SIGN ('+', token1->val.str.text[token1->val.str.len - 1]))
f80e83a9 2278 return CPP_NUMBER;
2279 break;
2280
71aa9da4 2281 case CPP_OTHER:
2282 if (CPP_OPTION (pfile, objc) && token1->val.aux == '@')
2283 {
2284 if (b == CPP_NAME) return CPP_NAME;
2285 if (b == CPP_STRING) return CPP_OSTRING;
2286 }
2287
f80e83a9 2288 default:
2289 break;
6060326b 2290 }
2291
f80e83a9 2292 return CPP_EOF;
2293}
2294
2295/* Check if TOKEN is to be ##-pasted with the token after it. */
2296static const cpp_token *
2297maybe_paste_with_next (pfile, token)
2298 cpp_reader *pfile;
2299 const cpp_token *token;
2300{
2301 cpp_token *pasted;
2302 const cpp_token *second;
2303 cpp_context *context = CURRENT_CONTEXT (pfile);
2304
2305 /* Is this token on the LHS of ## ? */
f80e83a9 2306
deb356cf 2307 while ((token->flags & PASTE_LEFT)
2308 || ((context->flags & CONTEXT_PASTEL)
2309 && context->posn == context->count))
6060326b 2310 {
deb356cf 2311 /* Suppress macro expansion for next token, but don't conflict
2312 with the other method of suppression. If it is an argument,
2313 macro expansion within the argument will still occur. */
2314 pfile->paste_level = pfile->cur_context;
2315 second = _cpp_get_token (pfile);
2316 pfile->paste_level = 0;
2317
2318 /* Ignore placemarker argument tokens (cannot be from an empty
2319 macro since macros are not expanded). */
2320 if (token->type == CPP_PLACEMARKER)
2321 pasted = duplicate_token (pfile, second);
2322 else if (second->type == CPP_PLACEMARKER)
f80e83a9 2323 {
e0a859f1 2324 /* GCC has special extended semantics for , ## b where b is
2325 a varargs parameter: the comma disappears if b was given
2326 no actual arguments (not merely if b is an empty
2327 argument). */
2328 if (token->type == CPP_COMMA && second->flags & VOID_REST)
35677230 2329 pasted = duplicate_token (pfile, second);
deb356cf 2330 else
2331 pasted = duplicate_token (pfile, token);
f80e83a9 2332 }
2333 else
f80e83a9 2334 {
deb356cf 2335 int digraph = 0;
524f0c40 2336 enum cpp_ttype type = _cpp_can_paste (pfile, token, second, &digraph);
6060326b 2337
deb356cf 2338 if (type == CPP_EOF)
2339 {
2340 if (CPP_OPTION (pfile, warn_paste))
e0a859f1 2341 {
2342 /* Do not complain about , ## <whatever> if
2343 <whatever> came from a variable argument, because
2344 the author probably intended the ## to trigger
2345 the special extended semantics (see above). */
2346 if (token->type == CPP_COMMA
2347 && IS_ARG_CONTEXT (CURRENT_CONTEXT (pfile))
58fe658a 2348 && ON_REST_ARG (CURRENT_CONTEXT (pfile) - 1))
e0a859f1 2349 /* no warning */;
2350 else
2351 cpp_warning (pfile,
deb356cf 2352 "pasting would not give a valid preprocessing token");
e0a859f1 2353 }
deb356cf 2354 _cpp_push_token (pfile, second);
58fe658a 2355 /* A short term hack to safely clear the PASTE_LEFT flag. */
2356 pasted = duplicate_token (pfile, token);
2357 pasted->flags &= ~PASTE_LEFT;
2358 return pasted;
deb356cf 2359 }
6060326b 2360
deb356cf 2361 if (type == CPP_NAME || type == CPP_NUMBER)
2362 {
2363 /* Join spellings. */
2364 U_CHAR *buf, *end;
2365
2366 pasted = get_temp_token (pfile);
2367 buf = (U_CHAR *) alloca (TOKEN_LEN (token) + TOKEN_LEN (second));
2368 end = spell_token (pfile, token, buf);
2369 end = spell_token (pfile, second, end);
2370 *end = '\0';
f80e83a9 2371
deb356cf 2372 if (type == CPP_NAME)
2373 pasted->val.node = cpp_lookup (pfile, buf, end - buf);
2374 else
2375 {
2376 pasted->val.str.text = uxstrdup (buf);
2377 pasted->val.str.len = end - buf;
2378 }
2379 }
71aa9da4 2380 else if (type == CPP_WCHAR || type == CPP_WSTRING
2381 || type == CPP_OSTRING)
deb356cf 2382 pasted = duplicate_token (pfile, second);
76faa4c0 2383 else
2384 {
deb356cf 2385 pasted = get_temp_token (pfile);
2386 pasted->val.integer = 0;
76faa4c0 2387 }
deb356cf 2388
2389 pasted->type = type;
2390 pasted->flags = digraph ? DIGRAPH : 0;
31674461 2391
2392 if (type == CPP_NAME && pasted->val.node->type == T_OPERATOR)
2393 {
2394 pasted->type = pasted->val.node->value.code;
2395 pasted->flags |= NAMED_OP;
2396 }
f80e83a9 2397 }
2398
deb356cf 2399 /* The pasted token gets the whitespace flags and position of the
2400 first token, the PASTE_LEFT flag of the second token, plus the
2401 PASTED flag to indicate it is the result of a paste. However, we
2402 want to preserve the DIGRAPH flag. */
2403 pasted->flags &= ~(PREV_WHITE | BOL | PASTE_LEFT);
2404 pasted->flags |= ((token->flags & (PREV_WHITE | BOL))
2405 | (second->flags & PASTE_LEFT) | PASTED);
2406 pasted->col = token->col;
2407 pasted->line = token->line;
2408
2409 /* See if there is another token to be pasted onto the one we just
2410 constructed. */
2411 token = pasted;
2412 context = CURRENT_CONTEXT (pfile);
2413 /* and loop */
f80e83a9 2414 }
deb356cf 2415 return token;
f80e83a9 2416}
2417
2418/* Convert a token sequence to a single string token according to the
2419 rules of the ISO C #-operator. */
2420#define INIT_SIZE 200
2421static cpp_token *
2422stringify_arg (pfile, token)
6060326b 2423 cpp_reader *pfile;
f80e83a9 2424 const cpp_token *token;
6060326b 2425{
f80e83a9 2426 cpp_token *result;
2427 unsigned char *main_buf;
2428 unsigned int prev_value, backslash_count = 0;
2429 unsigned int buf_used = 0, whitespace = 0, buf_cap = INIT_SIZE;
6060326b 2430
deb356cf 2431 push_arg_context (pfile, token);
f80e83a9 2432 prev_value = prevent_macro_expansion (pfile);
2433 main_buf = (unsigned char *) xmalloc (buf_cap);
6060326b 2434
f80e83a9 2435 result = get_temp_token (pfile);
2436 ASSIGN_FLAGS_AND_POS (result, token);
2437
deb356cf 2438 for (; (token = _cpp_get_token (pfile))->type != CPP_EOF; )
6060326b 2439 {
f80e83a9 2440 int escape;
2441 unsigned char *buf;
2442 unsigned int len = TOKEN_LEN (token);
6060326b 2443
7e842f95 2444 if (token->type == CPP_PLACEMARKER)
2445 continue;
2446
f80e83a9 2447 escape = (token->type == CPP_STRING || token->type == CPP_WSTRING
2448 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
2449 if (escape)
2450 len *= 4 + 1;
2451
2452 if (buf_used + len > buf_cap)
6060326b 2453 {
f80e83a9 2454 buf_cap = buf_used + len + INIT_SIZE;
2455 main_buf = xrealloc (main_buf, buf_cap);
2456 }
6060326b 2457
f80e83a9 2458 if (whitespace && (token->flags & PREV_WHITE))
2459 main_buf[buf_used++] = ' ';
6060326b 2460
f80e83a9 2461 if (escape)
2462 buf = (unsigned char *) xmalloc (len);
2463 else
2464 buf = main_buf + buf_used;
2465
2466 len = spell_token (pfile, token, buf) - buf;
2467 if (escape)
2468 {
2469 buf_used = quote_string (&main_buf[buf_used], buf, len) - main_buf;
2470 free (buf);
2471 }
2472 else
2473 buf_used += len;
6060326b 2474
f80e83a9 2475 whitespace = 1;
2476 if (token->type == CPP_BACKSLASH)
2477 backslash_count++;
2478 else
2479 backslash_count = 0;
2480 }
6060326b 2481
f80e83a9 2482 /* Ignore the final \ of invalid string literals. */
2483 if (backslash_count & 1)
2484 {
2485 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
2486 buf_used--;
2487 }
6060326b 2488
f80e83a9 2489 result->type = CPP_STRING;
76faa4c0 2490 result->val.str.text = main_buf;
2491 result->val.str.len = buf_used;
f80e83a9 2492 restore_macro_expansion (pfile, prev_value);
2493 return result;
2494}
6060326b 2495
f80e83a9 2496/* Allocate more room on the context stack of PFILE. */
2497static void
2498expand_context_stack (pfile)
2499 cpp_reader *pfile;
2500{
2501 pfile->context_cap += pfile->context_cap + 20;
2502 pfile->contexts = (cpp_context *)
2503 xrealloc (pfile->contexts, pfile->context_cap * sizeof (cpp_context));
2504}
6060326b 2505
f80e83a9 2506/* Push the context of macro NODE onto the context stack. TOKEN is
2507 the CPP_NAME token invoking the macro. */
deb356cf 2508static int
2509push_macro_context (pfile, token)
f80e83a9 2510 cpp_reader *pfile;
f80e83a9 2511 const cpp_token *token;
2512{
2513 unsigned char orig_flags;
2514 macro_args *args;
2515 cpp_context *context;
deb356cf 2516 cpp_hashnode *node = token->val.node;
6060326b 2517
f80e83a9 2518 /* Token's flags may change when parsing args containing a nested
2519 invocation of this macro. */
2520 orig_flags = token->flags & (PREV_WHITE | BOL);
2521 args = 0;
2522 if (node->value.expansion->paramc >= 0)
6060326b 2523 {
f80e83a9 2524 unsigned int error, prev_nme;
2525
2526 /* Allocate room for the argument contexts, and parse them. */
2527 args = (macro_args *) xmalloc (sizeof (macro_args));
2528 args->ends = (unsigned int *)
2529 xmalloc (node->value.expansion->paramc * sizeof (unsigned int));
2530 args->tokens = 0;
2531 args->capacity = 0;
2532 args->used = 0;
f80e83a9 2533
2534 prev_nme = prevent_macro_expansion (pfile);
2535 pfile->args = args;
2536 error = parse_args (pfile, node, args);
2537 pfile->args = 0;
2538 restore_macro_expansion (pfile, prev_nme);
2539 if (error)
2540 {
2541 free_macro_args (args);
deb356cf 2542 return 1;
f80e83a9 2543 }
e2ab8477 2544 /* Set the level after the call to parse_args. */
2545 args->level = pfile->cur_context;
6060326b 2546 }
6060326b 2547
f80e83a9 2548 /* Now push its context. */
2549 pfile->cur_context++;
2550 if (pfile->cur_context == pfile->context_cap)
2551 expand_context_stack (pfile);
2552
2553 context = CURRENT_CONTEXT (pfile);
2554 context->u.list = node->value.expansion;
2555 context->args = args;
2556 context->posn = 0;
2557 context->count = context->u.list->tokens_used;
2558 context->level = pfile->cur_context;
2559 context->flags = 0;
2560 context->pushed_token = 0;
2561
2562 /* Set the flags of the first token. We know there must
2563 be one, empty macros are a single placemarker token. */
2564 MODIFY_FLAGS_AND_POS (&context->u.list->tokens[0], token, orig_flags);
2565
deb356cf 2566 return 0;
6060326b 2567}
2568
f80e83a9 2569/* Push an argument to the current macro onto the context stack.
2570 TOKEN is the MACRO_ARG token representing the argument expansion. */
deb356cf 2571static void
f80e83a9 2572push_arg_context (pfile, token)
2573 cpp_reader *pfile;
2574 const cpp_token *token;
6060326b 2575{
f80e83a9 2576 cpp_context *context;
2577 macro_args *args;
2578
2579 pfile->cur_context++;
2580 if (pfile->cur_context == pfile->context_cap)
2581 expand_context_stack (pfile);
2582
2583 context = CURRENT_CONTEXT (pfile);
2584 args = context[-1].args;
2585
2586 context->count = token->val.aux ? args->ends[token->val.aux - 1]: 0;
2587 context->u.arg = args->tokens + context->count;
2588 context->count = args->ends[token->val.aux] - context->count;
2589 context->args = 0;
2590 context->posn = 0;
2591 context->level = args->level;
2592 context->flags = CONTEXT_ARG | CONTEXT_RAW;
2593 context->pushed_token = 0;
2594
2595 /* Set the flags of the first token. There is one. */
2596 {
2597 const cpp_token *first = context->u.arg[0];
2598 if (!first)
2599 first = context->u.arg[1];
6060326b 2600
f80e83a9 2601 MODIFY_FLAGS_AND_POS ((cpp_token *) first, token,
2602 token->flags & (PREV_WHITE | BOL));
2603 }
6060326b 2604
f80e83a9 2605 if (token->flags & PASTE_LEFT)
2606 context->flags |= CONTEXT_PASTEL;
2607 if (pfile->paste_level)
2608 context->flags |= CONTEXT_PASTER;
6060326b 2609}
2610
f80e83a9 2611/* "Unget" a token. It is effectively inserted in the token queue and
2612 will be returned by the next call to get_raw_token. */
6060326b 2613void
f80e83a9 2614_cpp_push_token (pfile, token)
6060326b 2615 cpp_reader *pfile;
f80e83a9 2616 const cpp_token *token;
6060326b 2617{
f80e83a9 2618 cpp_context *context = CURRENT_CONTEXT (pfile);
7e842f95 2619
2620 if (context->posn > 0)
2621 {
2622 const cpp_token *prev;
2623 if (IS_ARG_CONTEXT (context))
2624 prev = context->u.arg[context->posn - 1];
2625 else
2626 prev = &context->u.list->tokens[context->posn - 1];
2627
2628 if (prev == token)
2629 {
2630 context->posn--;
2631 return;
2632 }
2633 }
2634
f80e83a9 2635 if (context->pushed_token)
2636 cpp_ice (pfile, "two tokens pushed in a row");
2637 if (token->type != CPP_EOF)
2638 context->pushed_token = token;
2639 /* Don't push back a directive's CPP_EOF, step back instead. */
2640 else if (pfile->cur_context == 0)
2641 pfile->contexts[0].posn--;
2642}
6060326b 2643
f80e83a9 2644/* Handle a preprocessing directive. TOKEN is the CPP_HASH token
2645 introducing the directive. */
524f0c40 2646void
2647_cpp_process_directive (pfile, token)
f80e83a9 2648 cpp_reader *pfile;
2649 const cpp_token *token;
2650{
2651 const struct directive *d = pfile->token_list.directive;
2652 int prev_nme = 0;
2653
2654 /* Skip over the directive name. */
2655 if (token[1].type == CPP_NAME)
2656 _cpp_get_raw_token (pfile);
2657 else if (token[1].type != CPP_NUMBER)
7e842f95 2658 cpp_ice (pfile, "directive begins with %s?!", TOKEN_NAME (token));
f80e83a9 2659
f80e83a9 2660 if (! (d->flags & EXPAND))
2661 prev_nme = prevent_macro_expansion (pfile);
2662 (void) (*d->handler) (pfile);
2663 if (! (d->flags & EXPAND))
2664 restore_macro_expansion (pfile, prev_nme);
2665 _cpp_skip_rest_of_line (pfile);
2666}
6060326b 2667
f80e83a9 2668/* The external interface to return the next token. All macro
2669 expansion and directive processing is handled internally, the
2670 caller only ever sees the output after preprocessing. */
2671const cpp_token *
2672cpp_get_token (pfile)
2673 cpp_reader *pfile;
2674{
2675 const cpp_token *token;
deb356cf 2676 /* Loop till we hit a non-directive, non-placemarker token. */
f80e83a9 2677 for (;;)
2678 {
deb356cf 2679 token = _cpp_get_token (pfile);
2680
2681 if (token->type == CPP_PLACEMARKER)
2682 continue;
2683
2684 if (token->type == CPP_HASH && token->flags & BOL
f80e83a9 2685 && pfile->token_list.directive)
6060326b 2686 {
524f0c40 2687 _cpp_process_directive (pfile, token);
f80e83a9 2688 continue;
6060326b 2689 }
2690
deb356cf 2691 return token;
2692 }
2693}
2694
2695/* The internal interface to return the next token. There are two
2696 differences between the internal and external interfaces: the
2697 internal interface may return a PLACEMARKER token, and it does not
2698 process directives. */
2699const cpp_token *
2700_cpp_get_token (pfile)
2701 cpp_reader *pfile;
2702{
c2cce424 2703 const cpp_token *token, *old_token;
deb356cf 2704 cpp_hashnode *node;
2705
2706 /* Loop until we hit a non-macro token. */
2707 for (;;)
2708 {
2709 token = get_raw_token (pfile);
2710
f80e83a9 2711 /* Short circuit EOF. */
2712 if (token->type == CPP_EOF)
2713 return token;
deb356cf 2714
2715 /* If we are skipping... */
2716 if (pfile->skipping)
6060326b 2717 {
deb356cf 2718 /* we still have to process directives, */
2719 if (pfile->token_list.directive)
2720 return token;
2721
2722 /* but everything else is ignored. */
f80e83a9 2723 _cpp_skip_rest_of_line (pfile);
2724 continue;
2725 }
6060326b 2726
deb356cf 2727 /* If there's a potential control macro and we get here, then that
2728 #ifndef didn't cover the entire file and its argument shouldn't
2729 be taken as a control macro. */
2730 pfile->potential_control_macro = 0;
6060326b 2731
db54da8f 2732 /* If we are rescanning preprocessed input, no macro expansion or
2733 token pasting may occur. */
2734 if (CPP_OPTION (pfile, preprocessed))
2735 return token;
2736
c2cce424 2737 old_token = token;
2738
deb356cf 2739 /* See if there's a token to paste with this one. */
2740 if (!pfile->paste_level)
2741 token = maybe_paste_with_next (pfile, token);
6060326b 2742
deb356cf 2743 /* If it isn't a macro, return it now. */
31674461 2744 if (token->type != CPP_NAME || token->val.node->type == T_VOID)
deb356cf 2745 return token;
6060326b 2746
31674461 2747 /* Is macro expansion disabled in general, or are we in the
c2cce424 2748 middle of a token paste, or was this token just pasted?
2749 (Note we don't check token->flags & PASTED, because that
2750 counts tokens that were pasted at some point in the past,
2751 we're only interested in tokens that were pasted by this call
2752 to maybe_paste_with_next.) */
2753 if (pfile->no_expand_level == pfile->cur_context
2754 || pfile->paste_level
2755 || (token != old_token
2756 && pfile->no_expand_level + 1 == pfile->cur_context))
deb356cf 2757 return token;
c2cce424 2758
deb356cf 2759 node = token->val.node;
2760 if (node->type != T_MACRO)
2761 return special_symbol (pfile, node, token);
6060326b 2762
f80e83a9 2763 if (is_macro_disabled (pfile, node->value.expansion, token))
2764 return token;
6060326b 2765
deb356cf 2766 if (push_macro_context (pfile, token))
2767 return token;
2768 /* else loop */
f80e83a9 2769 }
f80e83a9 2770}
6060326b 2771
f80e83a9 2772/* Returns the next raw token, i.e. without performing macro
2773 expansion. Argument contexts are automatically entered. */
2774static const cpp_token *
2775get_raw_token (pfile)
2776 cpp_reader *pfile;
2777{
2778 const cpp_token *result;
deb356cf 2779 cpp_context *context;
6060326b 2780
deb356cf 2781 for (;;)
f80e83a9 2782 {
deb356cf 2783 context = CURRENT_CONTEXT (pfile);
2784 if (context->pushed_token)
f80e83a9 2785 {
deb356cf 2786 result = context->pushed_token;
2787 context->pushed_token = 0;
35677230 2788 return result; /* Cannot be a CPP_MACRO_ARG */
deb356cf 2789 }
2790 else if (context->posn == context->count)
2791 {
2792 if (pop_context (pfile))
2793 return &eof_token;
2794 continue;
2795 }
35677230 2796 else if (IS_ARG_CONTEXT (context))
deb356cf 2797 {
35677230 2798 result = context->u.arg[context->posn++];
2799 if (result == 0)
6060326b 2800 {
35677230 2801 context->flags ^= CONTEXT_RAW;
f80e83a9 2802 result = context->u.arg[context->posn++];
6060326b 2803 }
35677230 2804 return result; /* Cannot be a CPP_MACRO_ARG */
f80e83a9 2805 }
6060326b 2806
35677230 2807 result = &context->u.list->tokens[context->posn++];
2808
deb356cf 2809 if (result->type != CPP_MACRO_ARG)
2810 return result;
2811
2812 if (result->flags & STRINGIFY_ARG)
2813 return stringify_arg (pfile, result);
2814
2815 push_arg_context (pfile, result);
2816 }
f80e83a9 2817}
6060326b 2818
f80e83a9 2819/* Internal interface to get the token without macro expanding. */
2820const cpp_token *
2821_cpp_get_raw_token (pfile)
2822 cpp_reader *pfile;
2823{
2824 int prev_nme = prevent_macro_expansion (pfile);
deb356cf 2825 const cpp_token *result = _cpp_get_token (pfile);
f80e83a9 2826 restore_macro_expansion (pfile, prev_nme);
2827 return result;
2828}
6060326b 2829
f80e83a9 2830/* A thin wrapper to lex_line. CLEAR is non-zero if the current token
2831 list should be overwritten, or zero if we need to append
2832 (typically, if we are within the arguments to a macro, or looking
2833 for the '(' to start a function-like macro invocation). */
2834static int
2835lex_next (pfile, clear)
2836 cpp_reader *pfile;
2837 int clear;
2838{
2839 cpp_toklist *list = &pfile->token_list;
2840 const cpp_token *old_list = list->tokens;
2841 unsigned int old_used = list->tokens_used;
6060326b 2842
f80e83a9 2843 if (clear)
6060326b 2844 {
f80e83a9 2845 /* Release all temporary tokens. */
2846 _cpp_clear_toklist (list);
2847 pfile->contexts[0].posn = 0;
2848 if (pfile->temp_used)
2849 release_temp_tokens (pfile);
6060326b 2850 }
f80e83a9 2851 lex_line (pfile, list);
2852 pfile->contexts[0].count = list->tokens_used;
6060326b 2853
f80e83a9 2854 if (!clear && pfile->args)
6060326b 2855 {
f80e83a9 2856 /* Fix up argument token pointers. */
2857 if (old_list != list->tokens)
2858 {
2859 unsigned int i;
2860
2861 for (i = 0; i < pfile->args->used; i++)
2862 {
2863 const cpp_token *token = pfile->args->tokens[i];
2864 if (token >= old_list && token < old_list + old_used)
2865 pfile->args->tokens[i] = (const cpp_token *)
2866 ((char *) token + ((char *) list->tokens - (char *) old_list));
2867 }
2868 }
2869
2870 /* 6.10.3 paragraph 11: If there are sequences of preprocessing
2871 tokens within the list of arguments that would otherwise act as
2872 preprocessing directives, the behavior is undefined.
2873
2874 This implementation will report a hard error and treat the
2875 'sequence of preprocessing tokens' as part of the macro argument,
2876 not a directive.
2877
2878 Note if pfile->args == 0, we're OK since we're only inside a
2879 macro argument after a '('. */
2880 if (list->directive)
2881 {
2882 cpp_error_with_line (pfile, list->tokens[old_used].line,
2883 list->tokens[old_used].col,
2884 "#%s may not be used inside a macro argument",
2885 list->directive->name);
78719282 2886 return 1;
f80e83a9 2887 }
6060326b 2888 }
2889
f80e83a9 2890 return 0;
6060326b 2891}
2892
deb356cf 2893/* Pops a context off the context stack. If we're at the bottom, lexes
2894 the next logical line. Returns EOF if we're at the end of the
e0a859f1 2895 argument list to the # operator, or we should not "overflow"
f80e83a9 2896 into the rest of the file (e.g. 6.10.3.1.1). */
2897static int
deb356cf 2898pop_context (pfile)
f80e83a9 2899 cpp_reader *pfile;
2900{
2901 cpp_context *context;
89b05ef6 2902
f80e83a9 2903 if (pfile->cur_context == 0)
deb356cf 2904 {
2905 /* If we are currently processing a directive, do not advance. 6.10
2906 paragraph 2: A new-line character ends the directive even if it
2907 occurs within what would otherwise be an invocation of a
2908 function-like macro. */
2909 if (pfile->token_list.directive)
2910 return 1;
2911
2912 return lex_next (pfile, pfile->no_expand_level == UINT_MAX);
2913 }
f80e83a9 2914
2915 /* Argument contexts, when parsing args or handling # operator
2916 return CPP_EOF at the end. */
2917 context = CURRENT_CONTEXT (pfile);
2918 if (IS_ARG_CONTEXT (context) && pfile->cur_context == pfile->no_expand_level)
2919 return 1;
2920
2921 /* Free resources when leaving macro contexts. */
2922 if (context->args)
2923 free_macro_args (context->args);
2924
2925 if (pfile->cur_context == pfile->no_expand_level)
2926 pfile->no_expand_level--;
2927 pfile->cur_context--;
2928
2929 return 0;
2930}
2931
f80e83a9 2932/* Turn off macro expansion at the current context level. */
2933static unsigned int
2934prevent_macro_expansion (pfile)
2935 cpp_reader *pfile;
2936{
2937 unsigned int prev_value = pfile->no_expand_level;
2938 pfile->no_expand_level = pfile->cur_context;
2939 return prev_value;
2940}
2941
2942/* Restore macro expansion to its previous state. */
2943static void
2944restore_macro_expansion (pfile, prev_value)
2945 cpp_reader *pfile;
2946 unsigned int prev_value;
2947{
2948 pfile->no_expand_level = prev_value;
2949}
2950
2951/* Used by cpperror.c to obtain the correct line and column to report
2952 in a diagnostic. */
2953unsigned int
2954_cpp_get_line (pfile, pcol)
2955 cpp_reader *pfile;
2956 unsigned int *pcol;
2957{
2958 unsigned int index;
2959 const cpp_token *cur_token;
2960
338fa5f7 2961 if (pfile->state.in_lex_line)
f80e83a9 2962 index = pfile->token_list.tokens_used;
2963 else
c2cce424 2964 {
338fa5f7 2965 index = pfile->contexts[0].posn;
2966
2967 if (index == 0)
2968 {
2969 if (pcol)
2970 *pcol = 0;
2971 return 0;
2972 }
2973 index--;
c2cce424 2974 }
2975
338fa5f7 2976 cur_token = &pfile->token_list.tokens[index];
f80e83a9 2977 if (pcol)
2978 *pcol = cur_token->col;
2979 return cur_token->line;
2980}
2981
2982#define DSC(str) (const U_CHAR *)str, sizeof str - 1
2983static const char * const monthnames[] =
2984{
2985 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2986 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2987};
2988
2989/* Handle builtin macros like __FILE__. */
2990static const cpp_token *
2991special_symbol (pfile, node, token)
2992 cpp_reader *pfile;
2993 cpp_hashnode *node;
d2efb5ed 2994 const cpp_token *token;
89b05ef6 2995{
f80e83a9 2996 cpp_token *result;
2997 cpp_buffer *ip;
89b05ef6 2998
f80e83a9 2999 switch (node->type)
89b05ef6 3000 {
f80e83a9 3001 case T_FILE:
3002 case T_BASE_FILE:
89b05ef6 3003 {
f80e83a9 3004 const char *file;
89b05ef6 3005
f80e83a9 3006 ip = CPP_BUFFER (pfile);
3007 if (ip == 0)
3008 file = "";
89b05ef6 3009 else
f80e83a9 3010 {
3011 if (node->type == T_BASE_FILE)
3012 while (CPP_PREV_BUFFER (ip) != NULL)
3013 ip = CPP_PREV_BUFFER (ip);
3014
3015 file = ip->nominal_fname;
3016 }
3017 result = make_string_token (get_temp_token (pfile), (U_CHAR *) file,
3018 strlen (file));
3019 }
3020 break;
89b05ef6 3021
f80e83a9 3022 case T_INCLUDE_LEVEL:
241e762e 3023 /* pfile->include_depth counts the primary source as level 1,
3024 but historically __INCLUDE_DEPTH__ has called the primary
3025 source level 0. */
3026 result = alloc_number_token (pfile, pfile->include_depth - 1);
89b05ef6 3027 break;
3028
f80e83a9 3029 case T_SPECLINE:
3030 /* If __LINE__ is embedded in a macro, it must expand to the
3031 line of the macro's invocation, not its definition.
3032 Otherwise things like assert() will not work properly. */
3033 result = alloc_number_token (pfile, _cpp_get_line (pfile, NULL));
de3d9756 3034 break;
3035
f80e83a9 3036 case T_STDC:
89b05ef6 3037 {
f80e83a9 3038 int stdc = 1;
89b05ef6 3039
f80e83a9 3040#ifdef STDC_0_IN_SYSTEM_HEADERS
3041 if (CPP_IN_SYSTEM_HEADER (pfile)
76faa4c0 3042 && pfile->spec_nodes->n__STRICT_ANSI__->type == T_VOID)
f80e83a9 3043 stdc = 0;
3044#endif
3045 result = alloc_number_token (pfile, stdc);
89b05ef6 3046 }
3047 break;
3048
f80e83a9 3049 case T_DATE:
3050 case T_TIME:
3051 if (pfile->date == 0)
3052 {
3053 /* Allocate __DATE__ and __TIME__ from permanent storage,
3054 and save them in pfile so we don't have to do this again.
3055 We don't generate these strings at init time because
3056 time() and localtime() are very slow on some systems. */
3057 time_t tt = time (NULL);
3058 struct tm *tb = localtime (&tt);
3059
3060 pfile->date = make_string_token
3061 ((cpp_token *) xmalloc (sizeof (cpp_token)), DSC("Oct 11 1347"));
3062 pfile->time = make_string_token
3063 ((cpp_token *) xmalloc (sizeof (cpp_token)), DSC("12:34:56"));
3064
76faa4c0 3065 sprintf ((char *) pfile->date->val.str.text, "%s %2d %4d",
f80e83a9 3066 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
76faa4c0 3067 sprintf ((char *) pfile->time->val.str.text, "%02d:%02d:%02d",
f80e83a9 3068 tb->tm_hour, tb->tm_min, tb->tm_sec);
3069 }
3070 result = node->type == T_DATE ? pfile->date: pfile->time;
89b05ef6 3071 break;
3072
f80e83a9 3073 case T_POISON:
76faa4c0 3074 cpp_error (pfile, "attempt to use poisoned \"%s\"", node->name);
f80e83a9 3075 return token;
3076
3077 default:
3078 cpp_ice (pfile, "invalid special hash type");
3079 return token;
89b05ef6 3080 }
3081
f80e83a9 3082 ASSIGN_FLAGS_AND_POS (result, token);
3083 return result;
3084}
3085#undef DSC
3086
b3954366 3087/* Allocate pfile->input_buffer, and initialize _cpp_trigraph_map[]
f80e83a9 3088 if it hasn't happened already. */
3089
3090void
3091_cpp_init_input_buffer (pfile)
3092 cpp_reader *pfile;
3093{
deb356cf 3094 cpp_context *base;
3095
f669338a 3096 _cpp_init_toklist (&pfile->token_list, 0);
deb356cf 3097 pfile->no_expand_level = UINT_MAX;
f80e83a9 3098 pfile->context_cap = 20;
f80e83a9 3099 pfile->cur_context = 0;
f80e83a9 3100
deb356cf 3101 pfile->contexts = (cpp_context *)
3102 xmalloc (pfile->context_cap * sizeof (cpp_context));
f80e83a9 3103
deb356cf 3104 /* Clear the base context. */
3105 base = &pfile->contexts[0];
3106 base->u.list = &pfile->token_list;
3107 base->posn = 0;
3108 base->count = 0;
3109 base->args = 0;
3110 base->level = 0;
3111 base->flags = 0;
3112 base->pushed_token = 0;
f80e83a9 3113}
3114
3115/* Moves to the end of the directive line, popping contexts as
3116 necessary. */
3117void
3118_cpp_skip_rest_of_line (pfile)
3119 cpp_reader *pfile;
3120{
deb356cf 3121 /* Discard all stacked contexts. */
3122 int i;
3123 for (i = pfile->cur_context; i > 0; i--)
3124 if (pfile->contexts[i].args)
3125 free_macro_args (pfile->contexts[i].args);
3126
3127 if (pfile->no_expand_level <= pfile->cur_context)
3128 pfile->no_expand_level = 0;
3129 pfile->cur_context = 0;
f80e83a9 3130
deb356cf 3131 /* Clear the base context, and clear the directive pointer so that
3132 get_raw_token will advance to the next line. */
3133 pfile->contexts[0].count = 0;
3134 pfile->contexts[0].posn = 0;
f80e83a9 3135 pfile->token_list.directive = 0;
6060326b 3136}
3137
f80e83a9 3138/* Directive handler wrapper used by the command line option
3139 processor. */
3140void
a336277c 3141_cpp_run_directive (pfile, dir, buf, count, name)
f80e83a9 3142 cpp_reader *pfile;
3143 const struct directive *dir;
3144 const char *buf;
3145 size_t count;
a336277c 3146 const char *name;
f80e83a9 3147{
3148 if (cpp_push_buffer (pfile, (const U_CHAR *)buf, count) != NULL)
3149 {
3150 unsigned int prev_lvl = 0;
deb356cf 3151
a336277c 3152 if (name)
3153 CPP_BUFFER (pfile)->nominal_fname = name;
3154 else
3155 CPP_BUFFER (pfile)->nominal_fname = _("<command line>");
3156 CPP_BUFFER (pfile)->lineno = (unsigned int)-1;
3157
deb356cf 3158 /* Scan the line now, else prevent_macro_expansion won't work. */
3159 lex_next (pfile, 1);
f80e83a9 3160 if (! (dir->flags & EXPAND))
3161 prev_lvl = prevent_macro_expansion (pfile);
3162
3163 (void) (*dir->handler) (pfile);
3164
3165 if (! (dir->flags & EXPAND))
3166 restore_macro_expansion (pfile, prev_lvl);
3167
3168 _cpp_skip_rest_of_line (pfile);
3169 cpp_pop_buffer (pfile);
3170 }
3171}