]> gcc.gnu.org Git - gcc.git/blame - gcc/cppmacro.c
cppmacro.c (paste_tokens): Rename from paste_payloads.
[gcc.git] / gcc / cppmacro.c
CommitLineData
93c80368 1/* Part of CPP library. (Macro and #define handling.)
711b8824
ZW
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
25
26#include "config.h"
27#include "system.h"
93c80368 28#include "intl.h" /* for _("<command line>") below. */
711b8824
ZW
29#include "cpplib.h"
30#include "cpphash.h"
31
510fbf86
NB
32#ifndef STDC_0_IN_SYSTEM_HEADERS
33#define STDC_0_IN_SYSTEM_HEADERS 0 /* Boolean macro. */
34#endif
35
93c80368 36struct cpp_macro
711b8824 37{
93c80368
NB
38 cpp_hashnode **params; /* Parameters, if any. */
39 cpp_token *expansion; /* First token of replacement list. */
40 const char *file; /* Defined in file name. */
41 unsigned int line; /* Starting line number. */
42 unsigned int count; /* Number of tokens in expansion. */
43 unsigned short paramc; /* Number of parameters. */
44 unsigned int fun_like : 1; /* If a function-like macro. */
45 unsigned int var_args : 1; /* If a variable-args macro. */
46 unsigned int disabled : 1; /* If macro is disabled. */
47};
48
49typedef struct macro_arg macro_arg;
50struct macro_arg
51{
52 cpp_token *first; /* First token in unexpanded argument. */
53 cpp_token *expanded; /* Macro-expanded argument. */
54 cpp_token *stringified; /* Stringified argument. */
55 unsigned int count; /* # of tokens in argument. */
56 unsigned int expanded_count; /* # of tokens in expanded argument. */
711b8824
ZW
57};
58
93c80368
NB
59/* Macro expansion. */
60
61static void lock_pools PARAMS ((cpp_reader *));
62static void unlock_pools PARAMS ((cpp_reader *));
29b10746 63static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
93c80368
NB
64static void builtin_macro PARAMS ((cpp_reader *, cpp_token *));
65static cpp_context *push_arg_context PARAMS ((cpp_reader *, macro_arg *));
66static enum cpp_ttype parse_arg PARAMS ((cpp_reader *, macro_arg *, int));
67static macro_arg *parse_args PARAMS ((cpp_reader *, const cpp_hashnode *));
68static cpp_context *next_context PARAMS ((cpp_reader *));
69static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
70static unsigned char *quote_string PARAMS ((unsigned char *,
71 const unsigned char *,
72 unsigned int));
73static void make_string_token PARAMS ((cpp_pool *, cpp_token *,
74 const U_CHAR *, unsigned int));
75static void make_number_token PARAMS ((cpp_reader *, cpp_token *, int));
76static void stringify_arg PARAMS ((cpp_reader *, macro_arg *));
77static void paste_all_tokens PARAMS ((cpp_reader *, cpp_token *));
d63eefbf 78static int paste_tokens PARAMS ((cpp_reader *, cpp_token *, cpp_token *));
93c80368
NB
79static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *,
80 struct toklist *));
81static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *,
82 struct toklist *));
83
84/* Lookaheads. */
85
86static void save_lookahead_token PARAMS ((cpp_reader *, const cpp_token *));
87static void take_lookahead_token PARAMS ((cpp_reader *, cpp_token *));
93c80368
NB
88static cpp_lookahead *alloc_lookahead PARAMS ((cpp_reader *));
89static void free_lookahead PARAMS ((cpp_lookahead *));
90
91/* #define directive parsing and handling. */
92
93static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
94static int check_macro_redefinition PARAMS ((cpp_reader *,
95 const cpp_hashnode *,
96 const cpp_macro *));
97static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
98static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
e1aa5140 99static void check_trad_stringification PARAMS ((cpp_reader *,
93c80368 100 const cpp_macro *,
e1aa5140 101 const cpp_string *));
711b8824 102
93c80368
NB
103/* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a
104 CPP_STRING token containing TEXT in quoted form. */
105static void
106make_string_token (pool, token, text, len)
107 cpp_pool *pool;
108 cpp_token *token;
109 const U_CHAR *text;
110 unsigned int len;
111{
112 U_CHAR *buf = _cpp_pool_alloc (pool, len * 4);
113
114 token->type = CPP_STRING;
115 token->val.str.text = buf;
116 token->val.str.len = quote_string (buf, text, len) - buf;
117 token->flags = 0;
118}
119
120/* Allocates and converts a temporary token to a CPP_NUMBER token,
121 evaluating to NUMBER. */
122static void
123make_number_token (pfile, token, number)
124 cpp_reader *pfile;
125 cpp_token *token;
126 int number;
127{
128 unsigned char *buf = _cpp_pool_alloc (pfile->string_pool, 20);
129
130 sprintf ((char *) buf, "%d", number);
131 token->type = CPP_NUMBER;
132 token->val.str.text = buf;
133 token->val.str.len = ustrlen (buf);
134 token->flags = 0;
135}
136
137static const char * const monthnames[] =
138{
139 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
140 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
141};
142
143/* Handle builtin macros like __FILE__. */
144static void
145builtin_macro (pfile, token)
146 cpp_reader *pfile;
147 cpp_token *token;
148{
149 unsigned char flags = token->flags & PREV_WHITE;
150 cpp_hashnode *node = token->val.node;
151 cpp_buffer *ip;
152
153 switch (node->value.builtin)
154 {
155 case BT_FILE:
156 case BT_BASE_FILE:
711b8824 157 {
93c80368
NB
158 const char *file;
159
160 ip = CPP_BUFFER (pfile);
161 if (ip == 0)
162 file = "";
163 else
164 {
165 if (node->value.builtin == BT_BASE_FILE)
166 while (CPP_PREV_BUFFER (ip) != NULL)
167 ip = CPP_PREV_BUFFER (ip);
168
169 file = ip->nominal_fname;
170 }
171 make_string_token (pfile->string_pool, token,
ce1cc601 172 (const U_CHAR *) file, strlen (file));
711b8824 173 }
93c80368
NB
174 break;
175
176 case BT_INCLUDE_LEVEL:
177 /* pfile->include_depth counts the primary source as level 1,
178 but historically __INCLUDE_DEPTH__ has called the primary
179 source level 0. */
180 make_number_token (pfile, token, pfile->include_depth - 1);
181 break;
182
183 case BT_SPECLINE:
184 /* If __LINE__ is embedded in a macro, it must expand to the
185 line of the macro's invocation, not its definition.
186 Otherwise things like assert() will not work properly. */
187 make_number_token (pfile, token, cpp_get_line (pfile)->line);
188 break;
189
190 case BT_STDC:
191 {
192 int stdc = 1;
193
510fbf86 194 if (STDC_0_IN_SYSTEM_HEADERS && CPP_IN_SYSTEM_HEADER (pfile)
8a47978d 195 && pfile->spec_nodes.n__STRICT_ANSI__->type == NT_VOID)
93c80368 196 stdc = 0;
93c80368
NB
197 make_number_token (pfile, token, stdc);
198 }
199 break;
711b8824 200
93c80368
NB
201 case BT_DATE:
202 case BT_TIME:
203 if (pfile->date.type == CPP_EOF)
204 {
205 /* Allocate __DATE__ and __TIME__ from permanent storage,
206 and save them in pfile so we don't have to do this again.
207 We don't generate these strings at init time because
208 time() and localtime() are very slow on some systems. */
209 time_t tt = time (NULL);
210 struct tm *tb = localtime (&tt);
211
212 make_string_token (&pfile->ident_pool, &pfile->date,
213 DSC("Oct 11 1347"));
214 make_string_token (&pfile->ident_pool, &pfile->time,
215 DSC("12:34:56"));
216
217 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
218 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
219 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
220 tb->tm_hour, tb->tm_min, tb->tm_sec);
221 }
222 *token = node->value.builtin == BT_DATE ? pfile->date: pfile->time;
223 break;
224
225 default:
226 cpp_ice (pfile, "invalid builtin macro \"%s\"", node->name);
227 break;
228 }
229
230 token->flags = flags;
711b8824
ZW
231}
232
93c80368
NB
233/* Used by cpperror.c to obtain the correct line and column to report
234 in a diagnostic. */
235const cpp_lexer_pos *
236cpp_get_line (pfile)
711b8824 237 cpp_reader *pfile;
711b8824 238{
93c80368
NB
239 /* Within a macro expansion, return the position of the outermost
240 invocation. */
241 if (pfile->context->prev)
242 return &pfile->macro_pos;
243 return &pfile->lexer_pos;
244}
711b8824 245
93c80368
NB
246static void
247lock_pools (pfile)
248 cpp_reader *pfile;
249{
250 _cpp_lock_pool (&pfile->temp_string_pool);
251 _cpp_lock_pool (&pfile->argument_pool);
711b8824
ZW
252}
253
93c80368
NB
254static void
255unlock_pools (pfile)
256 cpp_reader *pfile;
257{
258 _cpp_unlock_pool (&pfile->temp_string_pool);
259 _cpp_unlock_pool (&pfile->argument_pool);
260}
711b8824 261
93c80368
NB
262/* Adds backslashes before all backslashes and double quotes appearing
263 in strings. Non-printable characters are converted to octal. */
264static U_CHAR *
265quote_string (dest, src, len)
266 U_CHAR *dest;
267 const U_CHAR *src;
268 unsigned int len;
269{
270 while (len--)
271 {
272 U_CHAR c = *src++;
711b8824 273
93c80368
NB
274 if (c == '\\' || c == '"')
275 {
276 *dest++ = '\\';
277 *dest++ = c;
278 }
279 else
280 {
281 if (ISPRINT (c))
282 *dest++ = c;
283 else
711b8824 284 {
93c80368
NB
285 sprintf ((char *) dest, "\\%03o", c);
286 dest += 4;
711b8824 287 }
93c80368
NB
288 }
289 }
711b8824 290
93c80368
NB
291 return dest;
292}
711b8824 293
93c80368
NB
294/* Convert a token sequence to a single string token according to the
295 rules of the ISO C #-operator. */
296static void
297stringify_arg (pfile, arg)
298 cpp_reader *pfile;
299 macro_arg *arg;
300{
301 cpp_pool *pool = pfile->string_pool;
302 unsigned char *start = POOL_FRONT (pool);
303 unsigned int i, escape_it, total_len = 0, backslash_count = 0;
711b8824 304
93c80368
NB
305 /* Loop, reading in the argument's tokens. */
306 for (i = 0; i < arg->count; i++)
307 {
308 unsigned char *dest;
309 const cpp_token *token = &arg->first[i];
310 unsigned int len = cpp_token_len (token);
711b8824 311
93c80368
NB
312 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
313 || token->type == CPP_CHAR || token->type == CPP_WCHAR
314 || token->type == CPP_OSTRING);
711b8824 315
93c80368
NB
316 if (escape_it)
317 /* Worst case is each char is octal. */
318 len *= 4;
319 len++; /* Room for initial space. */
711b8824 320
93c80368
NB
321 dest = &start[total_len];
322 if (dest + len > POOL_LIMIT (pool))
323 {
324 _cpp_next_chunk (pool, len, (unsigned char **) &start);
325 dest = &start[total_len];
326 }
711b8824 327
93c80368 328 /* No leading white space. */
4c2b647d
NB
329 if (token->flags & PREV_WHITE && total_len > 0)
330 *dest++ = ' ';
711b8824 331
93c80368
NB
332 if (escape_it)
333 {
334 unsigned char *buf = (unsigned char *) xmalloc (len);
335
336 len = cpp_spell_token (pfile, token, buf) - buf;
337 dest = quote_string (dest, buf, len);
338 free (buf);
339 }
340 else
341 dest = cpp_spell_token (pfile, token, dest);
342 total_len = dest - start;
343
6c53ebff 344 if (token->type == CPP_OTHER && token->val.c == '\\')
93c80368
NB
345 backslash_count++;
346 else
347 backslash_count = 0;
348 }
349
350 /* Ignore the final \ of invalid string literals. */
351 if (backslash_count & 1)
352 {
353 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
354 total_len--;
355 }
356
357 POOL_COMMIT (pool, total_len);
358
359 arg->stringified = xnew (cpp_token);
360 arg->stringified->flags = 0;
361 arg->stringified->type = CPP_STRING;
362 arg->stringified->val.str.text = start;
363 arg->stringified->val.str.len = total_len;
364}
365
d63eefbf
NB
366/* Try to paste two tokens. On success, the LHS becomes the pasted
367 token, and 0 is returned. For failure, we update the flags of the
368 RHS appropriately and return non-zero. */
369static int
370paste_tokens (pfile, lhs, rhs)
371 cpp_reader *pfile;
372 cpp_token *lhs, *rhs;
373{
374 unsigned char flags;
375 int digraph = 0;
376 enum cpp_ttype type;
377
378 type = cpp_can_paste (pfile, lhs, rhs, &digraph);
379
380 if (type == CPP_EOF)
381 {
382 if (CPP_OPTION (pfile, warn_paste))
383 cpp_warning (pfile,
384 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
385 cpp_token_as_text (pfile, lhs),
386 cpp_token_as_text (pfile, rhs));
387
388 /* The standard states that behaviour is undefined. By the
389 principle of least surpise, we step back before the RHS, and
390 mark it to prevent macro expansion. Tests in the testsuite
391 rely on clearing PREV_WHITE here, though you could argue we
392 should actually set it. */
393 rhs->flags &= ~PREV_WHITE;
394 rhs->flags |= NO_EXPAND;
395 return 1;
396 }
397
398 flags = lhs->flags & ~DIGRAPH;
399 if (digraph)
400 flags |= DIGRAPH;
401
402 /* Identifiers and numbers need spellings to be pasted. */
403 if (type == CPP_NAME || type == CPP_NUMBER)
404 {
405 unsigned int total_len = cpp_token_len (lhs) + cpp_token_len (rhs);
406 unsigned char *result, *end;
407 cpp_pool *pool;
408
409 pool = type == CPP_NAME ? &pfile->ident_pool: pfile->string_pool;
410 result = _cpp_pool_alloc (pool, total_len + 1);
411
412 /* Paste the spellings and null terminate. */
413 end = cpp_spell_token (pfile, rhs, cpp_spell_token (pfile, lhs, result));
414 *end = '\0';
415 total_len = end - result;
416
417 if (type == CPP_NAME)
418 {
419 lhs->val.node = cpp_lookup (pfile, result, total_len);
420 if (lhs->val.node->flags & NODE_OPERATOR)
421 {
422 flags |= NAMED_OP;
423 lhs->type = lhs->val.node->value.operator;
424 }
425 }
426 else
427 {
428 lhs->val.str.text = result;
429 lhs->val.str.len = total_len;
430 }
431 }
432 else if (type == CPP_WCHAR || type == CPP_WSTRING)
433 lhs->val.str = rhs->val.str;
434
435 /* Set type and flags after pasting spellings. */
436 lhs->type = type;
437 lhs->flags = flags;
438
439 return 0;
440}
441
93c80368
NB
442/* Handles an arbitrarily long sequence of ## operators. This
443 implementation is left-associative, non-recursive, and finishes a
d63eefbf
NB
444 paste before handling succeeding ones. If the paste fails, we back
445 up a token to just after the ## operator, with the effect that it
446 appears in the output stream normally. */
93c80368
NB
447static void
448paste_all_tokens (pfile, lhs)
449 cpp_reader *pfile;
450 cpp_token *lhs;
451{
93c80368 452 cpp_token *rhs;
d63eefbf 453 unsigned char orig_flags = lhs->flags;
93c80368
NB
454
455 do
456 {
457 /* Take the token directly from the current context. We can do
458 this, because we are in the replacement list of either an
459 object-like macro, or a function-like macro with arguments
460 inserted. In either case, the constraints to #define
d63eefbf 461 guarantee we have at least one more token. */
93c80368 462 rhs = pfile->context->list.first++;
d63eefbf 463 if (paste_tokens (pfile, lhs, rhs))
93c80368 464 {
d63eefbf 465 /* We failed. Step back so we read the RHS in next. */
4c2b647d
NB
466 pfile->context->list.first--;
467 break;
93c80368 468 }
93c80368
NB
469 }
470 while (rhs->flags & PASTE_LEFT);
471
472 /* The pasted token has the PREV_WHITE flag of the LHS, is no longer
473 PASTE_LEFT, and is subject to macro expansion. */
474 lhs->flags &= ~(PREV_WHITE | PASTE_LEFT | NO_EXPAND);
475 lhs->flags |= orig_flags & PREV_WHITE;
476}
477
4c2b647d
NB
478/* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS
479 is non-zero if this is a variable argument. Returns the type of
480 the token that caused reading to finish. */
93c80368
NB
481static enum cpp_ttype
482parse_arg (pfile, arg, var_args)
483 cpp_reader *pfile;
484 struct macro_arg *arg;
485 int var_args;
486{
487 enum cpp_ttype result;
488 unsigned int paren = 0;
489
490 arg->first = (cpp_token *) POOL_FRONT (&pfile->argument_pool);
491 for (;; arg->count++)
492 {
493 cpp_token *token = &arg->first[arg->count];
494 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->argument_pool))
495 {
496 _cpp_next_chunk (&pfile->argument_pool, sizeof (cpp_token),
497 (unsigned char **) &arg->first);
498 token = &arg->first[arg->count];
499 }
500
7f2f1a66 501 cpp_get_token (pfile, token);
93c80368
NB
502 result = token->type;
503
504 if (result == CPP_OPEN_PAREN)
505 paren++;
506 else if (result == CPP_CLOSE_PAREN && paren-- == 0)
507 break;
508 /* Commas are not terminators within parantheses or var_args. */
509 else if (result == CPP_COMMA && paren == 0 && !var_args)
510 break;
511 else if (result == CPP_EOF)
512 break; /* Error reported by caller. */
93c80368 513 }
711b8824 514
93c80368
NB
515 /* Commit the memory used to store the arguments. */
516 POOL_COMMIT (&pfile->argument_pool, arg->count * sizeof (cpp_token));
517
518 return result;
711b8824
ZW
519}
520
93c80368
NB
521/* Parse the arguments making up a macro invocation. */
522static macro_arg *
523parse_args (pfile, node)
524 cpp_reader *pfile;
525 const cpp_hashnode *node;
526{
527 cpp_macro *macro = node->value.macro;
528 macro_arg *args, *cur;
529 enum cpp_ttype type;
530 int argc, error = 0;
531
532 /* Allocate room for at least one argument, and zero it out. */
533 argc = macro->paramc ? macro->paramc: 1;
534 args = xcnewvec (macro_arg, argc);
535
536 for (cur = args, argc = 0; ;)
537 {
538 argc++;
539
540 type = parse_arg (pfile, cur, argc == macro->paramc && macro->var_args);
541 if (type == CPP_CLOSE_PAREN || type == CPP_EOF)
542 break;
543
544 /* Re-use the last argument for excess arguments. */
545 if (argc < macro->paramc)
546 cur++;
547 }
548
549 if (type == CPP_EOF)
550 {
551 cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
552 node->name);
553 error = 1;
554 }
555 else if (argc < macro->paramc)
556 {
557 /* As an extension, a rest argument is allowed to not appear in
558 the invocation at all.
559 e.g. #define debug(format, args...) something
560 debug("string");
561
562 This is exactly the same as if there had been an empty rest
563 argument - debug("string", ). */
564
565 if (argc + 1 == macro->paramc && macro->var_args)
566 {
93c80368
NB
567 if (CPP_OPTION (pfile, c99) && CPP_PEDANTIC (pfile))
568 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
569 }
570 else
571 {
572 cpp_error (pfile,
573 "macro \"%s\" requires %u arguments, but only %u given",
574 node->name, macro->paramc, argc);
575 error = 1;
576 }
577 }
578 else if (argc > macro->paramc)
579 {
4c2b647d
NB
580 /* Empty argument to a macro taking no arguments is OK. */
581 if (argc != 1 || cur->count)
93c80368
NB
582 {
583 cpp_error (pfile,
584 "macro \"%s\" passed %u arguments, but takes just %u",
585 node->name, argc, macro->paramc);
586 error = 1;
587 }
588 }
589
590 if (error)
591 {
592 free (args);
593 args = 0;
594 }
595
596 return args;
597}
598
599static int
600funlike_invocation_p (pfile, node, list)
601 cpp_reader *pfile;
602 const cpp_hashnode *node;
603 struct toklist *list;
604{
605 cpp_context *orig_context;
606 cpp_token maybe_paren;
607 macro_arg *args = 0;
608
609 pfile->state.parsing_args = 1;
610 pfile->state.prevent_expansion++;
611 orig_context = pfile->context;
612
613 cpp_start_lookahead (pfile);
614 cpp_get_token (pfile, &maybe_paren);
615 cpp_stop_lookahead (pfile, maybe_paren.type == CPP_OPEN_PAREN);
616
617 if (maybe_paren.type == CPP_OPEN_PAREN)
618 args = parse_args (pfile, node);
619 else if (CPP_WTRADITIONAL (pfile))
620 cpp_warning (pfile,
621 "function-like macro \"%s\" must be used with arguments in traditional C",
622 node->name);
623
624 /* Restore original context. */
625 pfile->context = orig_context;
626 pfile->state.prevent_expansion--;
627 pfile->state.parsing_args = 0;
628
629 if (args)
630 {
631 if (node->value.macro->paramc > 0)
7f2f1a66
NB
632 {
633 /* Don't save tokens during pre-expansion. */
634 struct cpp_lookahead *la_saved = pfile->la_write;
635 pfile->la_write = 0;
636 replace_args (pfile, node->value.macro, args, list);
637 pfile->la_write = la_saved;
638 }
93c80368
NB
639 free (args);
640 }
641
642 return args != 0;
643}
644
645/* Push the context of a macro onto the context stack. TOKEN is the
646 macro name. If we can successfully start expanding the macro,
a5c3cccd
NB
647 TOKEN is replaced with the first token of the expansion, and we
648 return non-zero. */
711b8824 649static int
29b10746 650enter_macro_context (pfile, node)
711b8824 651 cpp_reader *pfile;
29b10746 652 cpp_hashnode *node;
711b8824 653{
93c80368 654 cpp_context *context;
29b10746 655 cpp_macro *macro = node->value.macro;
93c80368 656 struct toklist list;
711b8824 657
93c80368
NB
658 /* Save the position of the outermost macro invocation. */
659 if (!pfile->context->prev)
660 {
661 pfile->macro_pos = pfile->lexer_pos;
662 lock_pools (pfile);
663 }
711b8824 664
29b10746 665 if (macro->fun_like && !funlike_invocation_p (pfile, node, &list))
93c80368
NB
666 {
667 if (!pfile->context->prev)
668 unlock_pools (pfile);
a5c3cccd 669 return 0;
93c80368 670 }
711b8824 671
93c80368 672 if (macro->paramc == 0)
711b8824 673 {
4c2b647d
NB
674 list.first = macro->expansion;
675 list.limit = macro->expansion + macro->count;
711b8824 676 }
4c2b647d 677
29b10746
NB
678 if (list.first != list.limit)
679 {
680 /* Push its context. */
681 context = next_context (pfile);
682 context->list = list;
683 context->macro = macro;
93c80368 684
29b10746
NB
685 /* Disable the macro within its expansion. */
686 macro->disabled = 1;
687 }
93c80368 688
a5c3cccd 689 return 1;
93c80368
NB
690}
691
692/* Move to the next context. Create one if there is none. */
693static cpp_context *
694next_context (pfile)
695 cpp_reader *pfile;
696{
697 cpp_context *prev = pfile->context;
698 cpp_context *result = prev->next;
699
700 if (result == 0)
711b8824 701 {
93c80368
NB
702 result = xnew (cpp_context);
703 prev->next = result;
704 result->prev = prev;
705 result->next = 0;
711b8824
ZW
706 }
707
93c80368
NB
708 pfile->context = result;
709 return result;
710}
711
712static void
713replace_args (pfile, macro, args, list)
714 cpp_reader *pfile;
715 cpp_macro *macro;
716 macro_arg *args;
717 struct toklist *list;
718{
719 unsigned int i, total;
720 const cpp_token *src, *limit;
721 cpp_token *dest;
722 macro_arg *arg;
723
724 src = macro->expansion;
725 limit = src + macro->count;
726
727 /* First, fully macro-expand arguments, calculating the number of
728 tokens in the final expansion as we go. This ensures that the
729 possible recursive use of argument_pool is fine. */
730 total = limit - src;
731 for (; src < limit; src++)
732 if (src->type == CPP_MACRO_ARG)
733 {
734 /* We have an argument. If it is not being stringified or
735 pasted it is macro-replaced before insertion. */
6c53ebff 736 arg = &args[src->val.arg_no - 1];
4c2b647d 737
93c80368
NB
738 if (src->flags & STRINGIFY_ARG)
739 {
740 if (!arg->stringified)
741 stringify_arg (pfile, arg);
742 }
743 else if ((src->flags & PASTE_LEFT)
744 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
745 total += arg->count - 1;
746 else
747 {
748 if (!arg->expanded)
4c2b647d
NB
749 {
750 arg->expanded_count = 0;
751 if (arg->count)
752 expand_arg (pfile, arg);
753 }
93c80368
NB
754 total += arg->expanded_count - 1;
755 }
756 }
757
758 dest = (cpp_token *) _cpp_pool_alloc (&pfile->argument_pool,
759 total * sizeof (cpp_token));
760 list->first = dest;
93c80368
NB
761
762 for (src = macro->expansion; src < limit; src++)
763 if (src->type == CPP_MACRO_ARG)
764 {
765 unsigned int count;
766 const cpp_token *from;
767
6c53ebff 768 arg = &args[src->val.arg_no - 1];
93c80368
NB
769 if (src->flags & STRINGIFY_ARG)
770 from = arg->stringified, count = 1;
4c2b647d 771 else if (src->flags & PASTE_LEFT)
93c80368 772 count = arg->count, from = arg->first;
4c2b647d
NB
773 else if (src > macro->expansion && (src[-1].flags & PASTE_LEFT))
774 {
775 count = arg->count, from = arg->first;
776 if (dest != list->first)
777 {
778 /* GCC has special semantics for , ## b where b is a
779 varargs parameter: the comma disappears if b was
780 given no actual arguments (not merely if b is an
781 empty argument); otherwise pasting is turned off. */
782 if (dest[-1].type == CPP_COMMA
783 && macro->var_args
784 && src->val.arg_no == macro->paramc)
785 {
786 if (count == 0)
787 dest--;
788 else
789 dest[-1].flags &= ~PASTE_LEFT;
790 }
791 /* Count == 0 is the RHS a placemarker case. */
792 else if (count == 0)
793 dest[-1].flags &= ~PASTE_LEFT;
794 }
795 }
93c80368
NB
796 else
797 count = arg->expanded_count, from = arg->expanded;
93c80368 798
4c2b647d
NB
799 /* Count == 0 is the LHS a placemarker case. */
800 if (count)
801 {
802 memcpy (dest, from, count * sizeof (cpp_token));
803
804 /* The first token gets PREV_WHITE of the CPP_MACRO_ARG. */
805 dest->flags &= ~PREV_WHITE;
806 dest->flags |= src->flags & PREV_WHITE;
93c80368 807
4c2b647d
NB
808 /* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */
809 dest[count - 1].flags |= src->flags & PASTE_LEFT;
93c80368 810
4c2b647d
NB
811 dest += count;
812 }
93c80368
NB
813 }
814 else
815 *dest++ = *src;
816
4c2b647d
NB
817 list->limit = dest;
818
93c80368
NB
819 /* Free the expanded arguments. */
820 for (i = 0; i < macro->paramc; i++)
711b8824 821 {
93c80368
NB
822 if (args[i].expanded)
823 free (args[i].expanded);
824 if (args[i].stringified)
825 free (args[i].stringified);
826 }
827}
828
829/* Subroutine of expand_arg to put the unexpanded tokens on the
830 context stack. */
831static cpp_context *
832push_arg_context (pfile, arg)
833 cpp_reader *pfile;
834 macro_arg *arg;
835{
836 cpp_context *context = next_context (pfile);
837 context->macro = 0;
838 context->list.first = arg->first;
839 context->list.limit = arg->first + arg->count;
840
841 return context;
842}
843
844static void
845expand_arg (pfile, arg)
846 cpp_reader *pfile;
847 macro_arg *arg;
848{
849 cpp_token *token;
850 unsigned int capacity = 256;
851
852 /* Loop, reading in the arguments. */
853 arg->expanded = (cpp_token *) xmalloc (capacity * sizeof (cpp_token));
93c80368
NB
854
855 push_arg_context (pfile, arg);
856 do
857 {
858 if (arg->expanded_count >= capacity)
711b8824 859 {
93c80368
NB
860 capacity *= 2;
861 arg->expanded = (cpp_token *)
862 xrealloc (arg->expanded, capacity * sizeof (cpp_token));
863 }
864 token = &arg->expanded[arg->expanded_count++];
7f2f1a66 865 cpp_get_token (pfile, token);
93c80368
NB
866 }
867 while (token->type != CPP_EOF);
868
869 arg->expanded_count--;
870
871 /* Pop the context we pushed. */
872 pfile->context = pfile->context->prev;
873}
874
875void
876_cpp_pop_context (pfile)
877 cpp_reader *pfile;
878{
879 cpp_context *context = pfile->context;
880
881 pfile->context = context->prev;
882 /* Re-enable a macro and free resources when leaving its expansion. */
883 if (!pfile->state.parsing_args)
884 {
885 if (!pfile->context->prev)
886 unlock_pools (pfile);
887 context->macro->disabled = 0;
888 }
889}
890
7f2f1a66
NB
891/* Eternal routine to get a token. Also used nearly everywhere
892 internally, except for places where we know we can safely call
893 the lexer directly, such as lexing a directive name.
894
895 Macro expansions and directives are transparently handled,
896 including entering included files. Thus tokens are post-macro
897 expansion, and after any intervening directives. External callers
898 see CPP_EOF only at EOF. Internal callers also see it when meeting
899 a directive inside a macro call, when at the end of a directive and
900 state.in_directive is still 1, and at the end of argument
901 pre-expansion. */
93c80368 902void
7f2f1a66 903cpp_get_token (pfile, token)
93c80368
NB
904 cpp_reader *pfile;
905 cpp_token *token;
906{
29b10746
NB
907 unsigned char flags = 0;
908
909 for (;;)
93c80368
NB
910 {
911 cpp_context *context = pfile->context;
912
913 if (pfile->la_read)
914 take_lookahead_token (pfile, token);
915 /* Context->prev == 0 <=> base context. */
916 else if (!context->prev)
917 _cpp_lex_token (pfile, token);
918 else if (context->list.first != context->list.limit)
29b10746
NB
919 {
920 *token = *context->list.first++;
921 token->flags |= flags;
922 flags = 0;
7f2f1a66
NB
923 /* PASTE_LEFT tokens can only appear in macro expansions. */
924 if (token->flags & PASTE_LEFT && !pfile->skipping)
925 paste_all_tokens (pfile, token);
29b10746 926 }
93c80368
NB
927 else
928 {
929 if (context->macro)
711b8824 930 {
93c80368 931 _cpp_pop_context (pfile);
29b10746 932 continue;
711b8824 933 }
a949941c 934 /* End of argument pre-expansion. */
93c80368
NB
935 token->type = CPP_EOF;
936 token->flags = 0;
b528a07e 937 return;
93c80368 938 }
93c80368 939
29b10746
NB
940 /* Loop until we're not skipping. */
941 if (pfile->skipping)
942 continue;
943
a5c3cccd 944 if (token->type != CPP_NAME)
93c80368
NB
945 break;
946
a5c3cccd
NB
947 /* Handle macros and the _Pragma operator. */
948 if (token->val.node->type == NT_MACRO
949 && !pfile->state.prevent_expansion
950 && !(token->flags & NO_EXPAND))
93c80368 951 {
29b10746 952 cpp_hashnode *node = token->val.node;
4c2b647d 953
a5c3cccd
NB
954 /* Macros invalidate controlling macros. */
955 pfile->mi_state = MI_FAILED;
956
29b10746 957 if (node->flags & NODE_BUILTIN)
a5c3cccd
NB
958 {
959 builtin_macro (pfile, token);
960 break;
961 }
962
29b10746
NB
963 /* Merge PREV_WHITE of tokens. */
964 flags = token->flags & PREV_WHITE;
965
966 if (node->value.macro->disabled)
967 token->flags |= NO_EXPAND;
968 else if (enter_macro_context (pfile, node))
a5c3cccd 969 continue;
93c80368 970 }
a5c3cccd 971
fe6c2db9
NB
972 /* Don't interpret _Pragma within directives. The standard is
973 not clear on this, but to me this makes most sense. */
974 if (token->val.node != pfile->spec_nodes.n__Pragma
975 || pfile->state.in_directive)
93c80368 976 break;
a5c3cccd 977
7f2f1a66
NB
978 /* Handle it, and loop back for another token. MI is cleared
979 since this token came from either the lexer or a macro. */
a5c3cccd 980 _cpp_do__Pragma (pfile);
93c80368 981 }
93c80368
NB
982
983 if (pfile->la_write)
984 save_lookahead_token (pfile, token);
985}
986
987/* Read each token in, until EOF. Directives are transparently
988 processed. */
989void
990cpp_scan_buffer_nooutput (pfile)
991 cpp_reader *pfile;
992{
993 cpp_token token;
994
995 do
996 do
997 cpp_get_token (pfile, &token);
998 while (token.type != CPP_EOF);
999 while (cpp_pop_buffer (pfile) != 0);
1000}
1001
1002/* Lookahead handling. */
1003
1004static void
1005save_lookahead_token (pfile, token)
1006 cpp_reader *pfile;
1007 const cpp_token *token;
1008{
1009 if (token->type != CPP_EOF)
1010 {
1011 cpp_lookahead *la = pfile->la_write;
1012 cpp_token_with_pos *twp;
1013
1014 if (la->count == la->cap)
711b8824 1015 {
93c80368
NB
1016 la->cap += la->cap + 8;
1017 la->tokens = (cpp_token_with_pos *)
1018 xrealloc (la->tokens, la->cap * sizeof (cpp_token_with_pos));
711b8824 1019 }
93c80368
NB
1020
1021 twp = &la->tokens[la->count++];
1022 twp->token = *token;
1023 twp->pos = *cpp_get_line (pfile);
711b8824 1024 }
711b8824
ZW
1025}
1026
93c80368
NB
1027static void
1028take_lookahead_token (pfile, token)
711b8824 1029 cpp_reader *pfile;
93c80368 1030 cpp_token *token;
711b8824 1031{
93c80368
NB
1032 cpp_lookahead *la = pfile->la_read;
1033 cpp_token_with_pos *twp = &la->tokens[la->cur];
711b8824 1034
93c80368
NB
1035 *token = twp->token;
1036 pfile->lexer_pos = twp->pos;
711b8824 1037
93c80368 1038 if (++la->cur == la->count)
b528a07e 1039 _cpp_release_lookahead (pfile);
93c80368 1040}
711b8824 1041
93c80368 1042/* Moves the lookahead at the front of the read list to the free store. */
b528a07e
NB
1043void
1044_cpp_release_lookahead (pfile)
93c80368
NB
1045 cpp_reader *pfile;
1046{
1047 cpp_lookahead *la = pfile->la_read;
711b8824 1048
93c80368
NB
1049 pfile->la_read = la->next;
1050 la->next = pfile->la_unused;
1051 pfile->la_unused = la;
1052 unlock_pools (pfile);
1053}
711b8824 1054
93c80368
NB
1055/* Take a new lookahead from the free store, or allocate one if none. */
1056static cpp_lookahead *
1057alloc_lookahead (pfile)
1058 cpp_reader *pfile;
1059{
1060 cpp_lookahead *la = pfile->la_unused;
1061
1062 if (la)
1063 pfile->la_unused = la->next;
1064 else
1065 {
1066 la = xnew (cpp_lookahead);
1067 la->tokens = 0;
1068 la->cap = 0;
1069 }
1070
1071 la->cur = la->count = 0;
1072 return la;
711b8824
ZW
1073}
1074
93c80368
NB
1075/* Free memory associated with a lookahead list. */
1076static void
1077free_lookahead (la)
1078 cpp_lookahead *la;
711b8824 1079{
93c80368
NB
1080 if (la->tokens)
1081 free ((PTR) la->tokens);
1082 free ((PTR) la);
1083}
711b8824 1084
93c80368
NB
1085/* Free all the lookaheads of a cpp_reader. */
1086void
1087_cpp_free_lookaheads (pfile)
1088 cpp_reader *pfile;
1089{
1090 cpp_lookahead *la, *lan;
1091
1092 if (pfile->la_read)
1093 free_lookahead (pfile->la_read);
1094 if (pfile->la_write)
1095 free_lookahead (pfile->la_write);
1096
1097 for (la = pfile->la_unused; la; la = lan)
1098 {
1099 lan = la->next;
1100 free_lookahead (la);
1101 }
1102}
711b8824 1103
93c80368
NB
1104/* Allocate a lookahead and move it to the front of the write list. */
1105void
1106cpp_start_lookahead (pfile)
711b8824 1107 cpp_reader *pfile;
711b8824 1108{
93c80368 1109 cpp_lookahead *la = alloc_lookahead (pfile);
711b8824 1110
93c80368
NB
1111 la->next = pfile->la_write;
1112 pfile->la_write = la;
711b8824 1113
93c80368 1114 la->pos = *cpp_get_line (pfile);
711b8824 1115
93c80368
NB
1116 /* Don't allow memory pools to be re-used whilst we're reading ahead. */
1117 lock_pools (pfile);
1118}
711b8824 1119
93c80368
NB
1120/* Stop reading ahead - either step back, or drop the read ahead. */
1121void
1122cpp_stop_lookahead (pfile, drop)
1123 cpp_reader *pfile;
1124 int drop;
1125{
1126 cpp_lookahead *la = pfile->la_write;
711b8824 1127
93c80368
NB
1128 pfile->la_write = la->next;
1129 la->next = pfile->la_read;
1130 pfile->la_read = la;
711b8824 1131
93c80368 1132 if (drop || la->count == 0)
b528a07e 1133 _cpp_release_lookahead (pfile);
93c80368
NB
1134 else
1135 pfile->lexer_pos = la->pos;
711b8824
ZW
1136}
1137
93c80368
NB
1138/* Push a single token back to the front of the queue. Only to be
1139 used by cpplib, and only then when necessary. POS is the position
1140 to report for the preceding token. */
1141void
1142_cpp_push_token (pfile, token, pos)
1143 cpp_reader *pfile;
1144 const cpp_token *token;
1145 const cpp_lexer_pos *pos;
1146{
1147 cpp_start_lookahead (pfile);
1148 save_lookahead_token (pfile, token);
1149 cpp_stop_lookahead (pfile, 0);
1150 pfile->lexer_pos = *pos;
1151}
1152
1153/* #define directive parsing and handling. */
1154
1155/* Returns non-zero if a macro redefinition is trivial. */
1156static int
1157check_macro_redefinition (pfile, node, macro2)
1158 cpp_reader *pfile;
1159 const cpp_hashnode *node;
1160 const cpp_macro *macro2;
1161{
1162 const cpp_macro *macro1;
1163 unsigned int i;
1164
1165 if (node->type != NT_MACRO || node->flags & NODE_BUILTIN)
1166 return ! pfile->done_initializing;
1167
1168 macro1 = node->value.macro;
1169
1170 /* The quick failures. */
1171 if (macro1->count != macro2->count
1172 || macro1->paramc != macro2->paramc
1173 || macro1->fun_like != macro2->fun_like
1174 || macro1->var_args != macro2->var_args)
1175 return 0;
1176
1177 /* Check each token. */
1178 for (i = 0; i < macro1->count; i++)
1179 if (! _cpp_equiv_tokens (&macro1->expansion[i], &macro2->expansion[i]))
1180 return 0;
1181
1182 /* Check parameter spellings. */
1183 for (i = 0; i < macro1->paramc; i++)
1184 if (macro1->params[i] != macro2->params[i])
1185 return 0;
1186
1187 return 1;
1188}
1189
1190/* Free the definition of hashnode H. */
711b8824
ZW
1191
1192void
1193_cpp_free_definition (h)
1194 cpp_hashnode *h;
1195{
93c80368
NB
1196 /* Macros and assertions no longer have anything to free. */
1197 h->type = NT_VOID;
1198 /* Clear builtin flag in case of redefinition. */
1199 h->flags &= ~NODE_BUILTIN;
1200}
1201
1202static int
1203save_parameter (pfile, macro, node)
1204 cpp_reader *pfile;
1205 cpp_macro *macro;
1206 cpp_hashnode *node;
1207{
1208 cpp_hashnode **dest;
1209
1210 /* Constraint 6.10.3.6 - duplicate parameter names. */
1211 if (node->arg_index)
709e9e50 1212 {
93c80368
NB
1213 cpp_error (pfile, "duplicate macro parameter \"%s\"", node->name);
1214 return 1;
1215 }
709e9e50 1216
93c80368
NB
1217 dest = &macro->params[macro->paramc];
1218
1219 /* Check we have room for the parameters. */
1220 if ((unsigned char *) (dest + 1) >= POOL_LIMIT (&pfile->macro_pool))
1221 {
1222 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_hashnode *),
1223 (unsigned char **) &macro->params);
1224 dest = &macro->params[macro->paramc];
709e9e50
NB
1225 }
1226
93c80368
NB
1227 *dest = node;
1228 node->arg_index = ++macro->paramc;
1229 return 0;
711b8824
ZW
1230}
1231
93c80368
NB
1232static int
1233parse_params (pfile, macro)
711b8824 1234 cpp_reader *pfile;
93c80368 1235 cpp_macro *macro;
711b8824 1236{
93c80368
NB
1237 cpp_token token;
1238 unsigned int prev_ident = 0;
711b8824 1239
93c80368
NB
1240 macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool);
1241 for (;;)
711b8824 1242 {
93c80368 1243 _cpp_lex_token (pfile, &token);
711b8824 1244
93c80368 1245 switch (token.type)
711b8824 1246 {
93c80368
NB
1247 default:
1248 cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
1249 cpp_token_as_text (pfile, &token));
1250 return 0;
1251
711b8824 1252 case CPP_NAME:
93c80368
NB
1253 if (prev_ident)
1254 {
1255 cpp_error (pfile, "macro parameters must be comma-separated");
1256 return 0;
1257 }
1258 prev_ident = 1;
1259
1260 if (save_parameter (pfile, macro, token.val.node))
1261 return 0;
1262 continue;
711b8824 1263
93c80368
NB
1264 case CPP_CLOSE_PAREN:
1265 if (prev_ident || macro->paramc == 0)
711b8824 1266 break;
711b8824 1267
93c80368
NB
1268 /* Fall through to pick up the error. */
1269 case CPP_COMMA:
1270 if (!prev_ident)
1271 {
1272 cpp_error (pfile, "parameter name missing");
1273 return 0;
1274 }
1275 prev_ident = 0;
711b8824
ZW
1276 continue;
1277
93c80368
NB
1278 case CPP_ELLIPSIS:
1279 macro->var_args = 1;
1280 if (!prev_ident)
1281 {
1282 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1283 pfile->state.va_args_ok = 1;
1284 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1285 cpp_pedwarn (pfile,
1286 "C89 does not permit anonymous variable arguments");
1287 }
1288 else if (CPP_OPTION (pfile, pedantic))
1289 cpp_pedwarn (pfile,
1290 "ISO C does not permit named variable arguments");
711b8824 1291
93c80368
NB
1292 /* We're at the end, and just expect a closing parenthesis. */
1293 _cpp_lex_token (pfile, &token);
1294 if (token.type == CPP_CLOSE_PAREN)
1295 break;
1296 /* Fall through. */
711b8824 1297
93c80368
NB
1298 case CPP_EOF:
1299 cpp_error (pfile, "missing ')' in macro parameter list");
1300 return 0;
711b8824
ZW
1301 }
1302
93c80368
NB
1303 /* Success. Commit the parameter array. */
1304 POOL_COMMIT (&pfile->macro_pool,
1305 macro->paramc * sizeof (cpp_hashnode *));
1306 return 1;
711b8824 1307 }
93c80368
NB
1308}
1309
1310/* Lex a token from a macro's replacement list. Translate it to a
1311 CPP_MACRO_ARG if appropriate. */
1312static cpp_token *
1313lex_expansion_token (pfile, macro)
1314 cpp_reader *pfile;
1315 cpp_macro *macro;
1316{
1317 cpp_token *token = &macro->expansion[macro->count];
711b8824 1318
93c80368
NB
1319 /* Check we have room for the token. */
1320 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
711b8824 1321 {
93c80368
NB
1322 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1323 (unsigned char **) &macro->expansion);
1324 token = &macro->expansion[macro->count];
711b8824
ZW
1325 }
1326
93c80368
NB
1327 macro->count++;
1328 _cpp_lex_token (pfile, token);
1329
1330 /* Is this an argument? */
1331 if (token->type == CPP_NAME && token->val.node->arg_index)
1332 {
1333 token->type = CPP_MACRO_ARG;
6c53ebff 1334 token->val.arg_no = token->val.node->arg_index;
93c80368
NB
1335 }
1336 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1337 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1338 check_trad_stringification (pfile, macro, &token->val.str);
1339
1340 return token;
711b8824
ZW
1341}
1342
1343/* Parse a macro and save its expansion. Returns non-zero on success. */
1344int
93c80368 1345_cpp_create_definition (pfile, node)
711b8824 1346 cpp_reader *pfile;
93c80368 1347 cpp_hashnode *node;
711b8824 1348{
93c80368
NB
1349 cpp_macro *macro;
1350 cpp_token *token;
1351 unsigned int i, ok = 1;
1352
1353 macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool,
1354 sizeof (cpp_macro));
1355 macro->file = pfile->buffer->nominal_fname;
1356 macro->line = pfile->directive_pos.line;
1357 macro->params = 0;
1358 macro->paramc = 0;
1359 macro->fun_like = 0;
1360 macro->var_args = 0;
93c80368
NB
1361 macro->count = 0;
1362 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1363
1364 /* Get the first token of the expansion (or the '(' of a
1365 function-like macro). */
1366 token = lex_expansion_token (pfile, macro);
1367 if (token->type == CPP_OPEN_PAREN && !(token->flags & PREV_WHITE))
1368 {
1369 if (!(ok = parse_params (pfile, macro)))
1370 goto cleanup;
1371 macro->count = 0;
1372 macro->fun_like = 1;
1373 /* Some of the pool may have been used for the parameter store. */
1374 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1375 token = lex_expansion_token (pfile, macro);
1376 }
1377 else if (token->type != CPP_EOF && !(token->flags & PREV_WHITE))
1378 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
711b8824 1379
93c80368
NB
1380 /* Setting it here means we don't catch leading comments. */
1381 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
711b8824 1382
93c80368
NB
1383 for (;;)
1384 {
1385 /* Check the stringifying # constraint 6.10.3.2.1 of
1386 function-like macros when lexing the subsequent token. */
1387 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1388 {
1389 if (token->type == CPP_MACRO_ARG)
1390 {
1391 token->flags &= ~PREV_WHITE;
1392 token->flags |= STRINGIFY_ARG;
1393 token->flags |= token[-1].flags & PREV_WHITE;
1394 token[-1] = token[0];
1395 macro->count--;
1396 }
1397 /* Let assembler get away with murder. */
1398 else if (!CPP_OPTION (pfile, lang_asm))
1399 {
1400 ok = 0;
1401 cpp_error (pfile, "'#' is not followed by a macro parameter");
1402 goto cleanup;
1403 }
1404 }
1405
1406 if (token->type == CPP_EOF)
1407 break;
1408
1409 /* Paste operator constraint 6.10.3.3.1. */
1410 if (token->type == CPP_PASTE)
1411 {
1412 /* Token-paste ##, can appear in both object-like and
1413 function-like macros, but not at the ends. */
1414 if (--macro->count > 0)
1415 token = lex_expansion_token (pfile, macro);
1416
1417 if (macro->count == 0 || token->type == CPP_EOF)
1418 {
1419 ok = 0;
1420 cpp_error (pfile,
1421 "'##' cannot appear at either end of a macro expansion");
1422 goto cleanup;
1423 }
711b8824 1424
93c80368
NB
1425 token[-1].flags |= PASTE_LEFT;
1426 /* Give it a PREV_WHITE for -dM etc. */
1427 token->flags |= PREV_WHITE;
1428 }
1429
1430 token = lex_expansion_token (pfile, macro);
1431 }
1432
4c2b647d
NB
1433 /* Don't count the CPP_EOF. */
1434 macro->count--;
93c80368
NB
1435
1436 /* Clear the whitespace flag from the leading token. */
1437 macro->expansion[0].flags &= ~PREV_WHITE;
1438
44ed91a1
NB
1439 /* Implement the macro-defined-to-itself optimisation. */
1440 macro->disabled = (macro->count == 1 && !macro->fun_like
1441 && macro->expansion[0].type == CPP_NAME
1442 && macro->expansion[0].val.node == node);
1443
93c80368
NB
1444 /* Commit the memory. */
1445 POOL_COMMIT (&pfile->macro_pool, macro->count * sizeof (cpp_token));
1446
1447 /* Redefinition of a macro is allowed if and only if the old and new
1448 definitions are the same. (6.10.3 paragraph 2). */
1449 if (node->type != NT_VOID)
711b8824 1450 {
93c80368
NB
1451 if (CPP_PEDANTIC (pfile)
1452 && !check_macro_redefinition (pfile, node, macro))
711b8824 1453 {
93c80368
NB
1454 cpp_pedwarn_with_line (pfile, pfile->directive_pos.line,
1455 pfile->directive_pos.col,
1456 "\"%s\" redefined", node->name);
1457
1458 if (pfile->done_initializing && node->type == NT_MACRO
1459 && !(node->flags & NODE_BUILTIN))
711b8824 1460 cpp_pedwarn_with_file_and_line (pfile,
93c80368
NB
1461 node->value.macro->file,
1462 node->value.macro->line, 1,
711b8824
ZW
1463 "this is the location of the previous definition");
1464 }
93c80368 1465 _cpp_free_definition (node);
711b8824
ZW
1466 }
1467
1468 /* Enter definition in hash table. */
93c80368
NB
1469 node->type = NT_MACRO;
1470 node->value.macro = macro;
711b8824 1471
93c80368
NB
1472 cleanup:
1473
1474 /* Stop the lexer accepting __VA_ARGS__. */
1475 pfile->state.va_args_ok = 0;
1476
1477 /* Clear the fast argument lookup indices. */
1478 for (i = macro->paramc; i-- > 0; )
1479 macro->params[i]->arg_index = 0;
1480
1481 return ok;
711b8824
ZW
1482}
1483
e1aa5140
KG
1484/* Warn if a token in `string' matches one of the function macro
1485 arguments in `info'. This function assumes that the macro is a
1486 function macro and not an object macro. */
1487static void
93c80368 1488check_trad_stringification (pfile, macro, string)
e1aa5140 1489 cpp_reader *pfile;
93c80368 1490 const cpp_macro *macro;
e1aa5140
KG
1491 const cpp_string *string;
1492{
93c80368 1493 unsigned int i, len;
e1aa5140
KG
1494 const U_CHAR *p, *q, *limit = string->text + string->len;
1495
1496 /* Loop over the string. */
1497 for (p = string->text; p < limit; p = q)
1498 {
e1aa5140 1499 /* Find the start of an identifier. */
61c16b10
GM
1500 while (p < limit && !is_idstart (*p))
1501 p++;
e1aa5140
KG
1502
1503 /* Find the end of the identifier. */
1504 q = p;
61c16b10
GM
1505 while (q < limit && is_idchar (*q))
1506 q++;
93c80368
NB
1507
1508 len = q - p;
1509
e1aa5140
KG
1510 /* Loop over the function macro arguments to see if the
1511 identifier inside the string matches one of them. */
93c80368
NB
1512 for (i = 0; i < macro->paramc; i++)
1513 {
1514 const cpp_hashnode *node = macro->params[i];
e1aa5140 1515
93c80368 1516 if (node->length == len && !memcmp (p, node->name, len))
e1aa5140
KG
1517 {
1518 cpp_warning (pfile,
93c80368
NB
1519 "macro argument \"%s\" would be stringified with -traditional.",
1520 node->name);
e1aa5140
KG
1521 break;
1522 }
1523 }
1524 }
1525}
93c80368
NB
1526
1527/* Returns the expansion of a macro, in a format suitable to be read
1528 back in again, and therefore also for DWARF 2 debugging info.
1529 Caller is expected to generate the "#define NAME" bit. The
1530 returned text is temporary, and automatically freed later. */
1531
1532const unsigned char *
1533cpp_macro_definition (pfile, node)
1534 cpp_reader *pfile;
1535 const cpp_hashnode *node;
1536{
1537 unsigned int i, len;
1538 const cpp_macro *macro = node->value.macro;
1539 unsigned char *buffer;
1540
1541 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1542 {
1543 cpp_ice (pfile, "invalid hash type %d in dump_definition", node->type);
1544 return 0;
1545 }
1546
1547 /* Calculate length. */
1548 len = 1; /* ' ' */
1549 if (macro->fun_like)
1550 {
1551 len += 3; /* "()" plus possible final "." of ellipsis. */
1552 for (i = 0; i < macro->paramc; i++)
1553 len += macro->params[i]->length + 2; /* ", " */
1554 }
1555
4c2b647d 1556 for (i = 0; i < macro->count; i++)
93c80368 1557 {
4c2b647d 1558 cpp_token *token = &macro->expansion[i];
93c80368 1559
4c2b647d
NB
1560 if (token->type == CPP_MACRO_ARG)
1561 len += macro->params[token->val.arg_no - 1]->length;
1562 else
1563 len += cpp_token_len (token); /* Includes room for ' '. */
1564 if (token->flags & STRINGIFY_ARG)
1565 len++; /* "#" */
1566 if (token->flags & PASTE_LEFT)
1567 len += 3; /* " ##" */
93c80368
NB
1568 }
1569
1570 if (len > pfile->macro_buffer_len)
1571 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1572 buffer = pfile->macro_buffer;
1573
1574 /* Parameter names. */
1575 if (macro->fun_like)
1576 {
1577 *buffer++ = '(';
1578 for (i = 0; i < macro->paramc; i++)
1579 {
1580 cpp_hashnode *param = macro->params[i];
1581
1582 if (param != pfile->spec_nodes.n__VA_ARGS__)
1583 {
1584 memcpy (buffer, param->name, param->length);
1585 buffer += param->length;
1586 }
1587
1588 if (i + 1 < macro->paramc)
1589 *buffer++ = ',', *buffer++ = ' ';
1590 else if (macro->var_args)
1591 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1592 }
1593 *buffer++ = ')';
1594 }
1595
1596 /* Expansion tokens. */
4c2b647d 1597 if (macro->count)
93c80368
NB
1598 {
1599 *buffer++ = ' ';
1600 for (i = 0; i < macro->count; i++)
1601 {
1602 cpp_token *token = &macro->expansion[i];
1603
1604 if (token->flags & PREV_WHITE)
1605 *buffer++ = ' ';
1606 if (token->flags & STRINGIFY_ARG)
1607 *buffer++ = '#';
1608
1609 if (token->type == CPP_MACRO_ARG)
1610 {
6c53ebff
NB
1611 len = macro->params[token->val.arg_no - 1]->length;
1612 memcpy (buffer, macro->params[token->val.arg_no - 1]->name, len);
93c80368
NB
1613 buffer += len;
1614 }
1615 else
1616 buffer = cpp_spell_token (pfile, token, buffer);
1617
1618 if (token->flags & PASTE_LEFT)
1619 {
1620 *buffer++ = ' ';
1621 *buffer++ = '#';
1622 *buffer++ = '#';
1623 /* Next has PREV_WHITE; see _cpp_create_definition. */
1624 }
1625 }
1626 }
1627
1628 *buffer = '\0';
1629 return pfile->macro_buffer;
1630}
This page took 0.293513 seconds and 5 git commands to generate.