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