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