* aclocal.m4 (gcc_AC_CHECK_DECL): Before attempting the test,
[official-gcc.git] / gcc / cppmacro.c
blob7a65e9725e592a2b8cc71abe2f3dac46adc92f03
1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000, 2001 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
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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! */
26 #include "config.h"
27 #include "system.h"
28 #include "intl.h" /* for _("<command line>") below. */
29 #include "cpplib.h"
30 #include "cpphash.h"
32 #ifndef STDC_0_IN_SYSTEM_HEADERS
33 #define STDC_0_IN_SYSTEM_HEADERS 0 /* Boolean macro. */
34 #endif
36 struct cpp_macro
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 variadic : 1; /* If a variadic macro. */
46 unsigned int disabled : 1; /* If macro is disabled. */
47 unsigned int syshdr : 1; /* If macro defined in system header. */
50 typedef struct macro_arg macro_arg;
51 struct macro_arg
53 cpp_token *first; /* First token in unexpanded argument. */
54 cpp_token *expanded; /* Macro-expanded argument. */
55 cpp_token *stringified; /* Stringified argument. */
56 unsigned int count; /* # of tokens in argument. */
57 unsigned int expanded_count; /* # of tokens in expanded argument. */
60 /* Macro expansion. */
62 static void lock_pools PARAMS ((cpp_reader *));
63 static void unlock_pools PARAMS ((cpp_reader *));
64 static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
65 static void builtin_macro PARAMS ((cpp_reader *, cpp_token *));
66 static cpp_context *push_arg_context PARAMS ((cpp_reader *, macro_arg *));
67 static enum cpp_ttype parse_arg PARAMS ((cpp_reader *, macro_arg *, int));
68 static macro_arg *parse_args PARAMS ((cpp_reader *, const cpp_hashnode *));
69 static cpp_context *next_context PARAMS ((cpp_reader *));
70 static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
71 static unsigned char *quote_string PARAMS ((unsigned char *,
72 const unsigned char *,
73 unsigned int));
74 static void make_string_token PARAMS ((cpp_pool *, cpp_token *,
75 const U_CHAR *, unsigned int));
76 static void make_number_token PARAMS ((cpp_reader *, cpp_token *, int));
77 static void stringify_arg PARAMS ((cpp_reader *, macro_arg *));
78 static void paste_all_tokens PARAMS ((cpp_reader *, cpp_token *));
79 static int paste_tokens PARAMS ((cpp_reader *, cpp_token *, cpp_token *));
80 static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *,
81 struct toklist *));
82 static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *,
83 struct toklist *));
85 /* Lookaheads. */
87 static void save_lookahead_token PARAMS ((cpp_reader *, const cpp_token *));
88 static void take_lookahead_token PARAMS ((cpp_reader *, cpp_token *));
89 static cpp_lookahead *alloc_lookahead PARAMS ((cpp_reader *));
90 static void free_lookahead PARAMS ((cpp_lookahead *));
92 /* #define directive parsing and handling. */
94 static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
95 static int check_macro_redefinition PARAMS ((cpp_reader *,
96 const cpp_hashnode *,
97 const cpp_macro *));
98 static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
99 static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
100 static void check_trad_stringification PARAMS ((cpp_reader *,
101 const cpp_macro *,
102 const cpp_string *));
104 /* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a
105 CPP_STRING token containing TEXT in quoted form. */
106 static void
107 make_string_token (pool, token, text, len)
108 cpp_pool *pool;
109 cpp_token *token;
110 const U_CHAR *text;
111 unsigned int len;
113 U_CHAR *buf = _cpp_pool_alloc (pool, len * 4);
115 token->type = CPP_STRING;
116 token->val.str.text = buf;
117 token->val.str.len = quote_string (buf, text, len) - buf;
118 token->flags = 0;
121 /* Allocates and converts a temporary token to a CPP_NUMBER token,
122 evaluating to NUMBER. */
123 static void
124 make_number_token (pfile, token, number)
125 cpp_reader *pfile;
126 cpp_token *token;
127 int number;
129 unsigned char *buf = _cpp_pool_alloc (&pfile->ident_pool, 20);
131 sprintf ((char *) buf, "%d", number);
132 token->type = CPP_NUMBER;
133 token->val.str.text = buf;
134 token->val.str.len = ustrlen (buf);
135 token->flags = 0;
138 static const char * const monthnames[] =
140 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
141 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
144 /* Handle builtin macros like __FILE__. */
145 static void
146 builtin_macro (pfile, token)
147 cpp_reader *pfile;
148 cpp_token *token;
150 unsigned char flags = ((token->flags & PREV_WHITE) | AVOID_LPASTE);
151 cpp_hashnode *node = token->val.node;
153 switch (node->value.builtin)
155 case BT_FILE:
156 case BT_BASE_FILE:
158 const char *name;
159 cpp_buffer *buffer = pfile->buffer;
161 if (node->value.builtin == BT_BASE_FILE)
162 while (buffer->prev)
163 buffer = buffer->prev;
165 name = buffer->nominal_fname;
166 make_string_token (&pfile->ident_pool, token,
167 (const unsigned char *) name, strlen (name));
169 break;
171 case BT_INCLUDE_LEVEL:
172 /* pfile->include_depth counts the primary source as level 1,
173 but historically __INCLUDE_DEPTH__ has called the primary
174 source level 0. */
175 make_number_token (pfile, token, pfile->include_depth - 1);
176 break;
178 case BT_SPECLINE:
179 /* If __LINE__ is embedded in a macro, it must expand to the
180 line of the macro's invocation, not its definition.
181 Otherwise things like assert() will not work properly. */
182 make_number_token (pfile, token, cpp_get_line (pfile)->line);
183 break;
185 case BT_STDC:
187 int stdc = 1;
189 if (STDC_0_IN_SYSTEM_HEADERS && CPP_IN_SYSTEM_HEADER (pfile)
190 && pfile->spec_nodes.n__STRICT_ANSI__->type == NT_VOID)
191 stdc = 0;
192 make_number_token (pfile, token, stdc);
194 break;
196 case BT_DATE:
197 case BT_TIME:
198 if (pfile->date.type == CPP_EOF)
200 /* Allocate __DATE__ and __TIME__ from permanent storage,
201 and save them in pfile so we don't have to do this again.
202 We don't generate these strings at init time because
203 time() and localtime() are very slow on some systems. */
204 time_t tt = time (NULL);
205 struct tm *tb = localtime (&tt);
207 make_string_token (&pfile->ident_pool, &pfile->date,
208 DSC("Oct 11 1347"));
209 make_string_token (&pfile->ident_pool, &pfile->time,
210 DSC("12:34:56"));
212 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
213 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
214 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
215 tb->tm_hour, tb->tm_min, tb->tm_sec);
217 *token = node->value.builtin == BT_DATE ? pfile->date: pfile->time;
218 break;
220 case BT_WEAK:
221 make_number_token (pfile, token, SUPPORTS_ONE_ONLY);
222 break;
224 default:
225 cpp_ice (pfile, "invalid builtin macro \"%s\"", node->name);
226 break;
229 token->flags = flags;
232 /* Used by cpperror.c to obtain the correct line and column to report
233 in a diagnostic. */
234 const cpp_lexer_pos *
235 cpp_get_line (pfile)
236 cpp_reader *pfile;
238 return &pfile->lexer_pos;
241 static void
242 lock_pools (pfile)
243 cpp_reader *pfile;
245 _cpp_lock_pool (&pfile->argument_pool);
248 static void
249 unlock_pools (pfile)
250 cpp_reader *pfile;
252 _cpp_unlock_pool (&pfile->argument_pool);
255 /* Adds backslashes before all backslashes and double quotes appearing
256 in strings. Non-printable characters are converted to octal. */
257 static U_CHAR *
258 quote_string (dest, src, len)
259 U_CHAR *dest;
260 const U_CHAR *src;
261 unsigned int len;
263 while (len--)
265 U_CHAR c = *src++;
267 if (c == '\\' || c == '"')
269 *dest++ = '\\';
270 *dest++ = c;
272 else
274 if (ISPRINT (c))
275 *dest++ = c;
276 else
278 sprintf ((char *) dest, "\\%03o", c);
279 dest += 4;
284 return dest;
287 /* Convert a token sequence to a single string token according to the
288 rules of the ISO C #-operator. */
289 static void
290 stringify_arg (pfile, arg)
291 cpp_reader *pfile;
292 macro_arg *arg;
294 cpp_pool *pool = &pfile->ident_pool;
295 unsigned char *start = POOL_FRONT (pool);
296 unsigned int i, escape_it, total_len = 0, backslash_count = 0;
298 /* Loop, reading in the argument's tokens. */
299 for (i = 0; i < arg->count; i++)
301 unsigned char *dest;
302 const cpp_token *token = &arg->first[i];
303 unsigned int len = cpp_token_len (token);
305 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
306 || token->type == CPP_CHAR || token->type == CPP_WCHAR
307 || token->type == CPP_OSTRING);
309 if (escape_it)
310 /* Worst case is each char is octal. */
311 len *= 4;
312 len++; /* Room for initial space. */
314 dest = &start[total_len];
315 if (dest + len > POOL_LIMIT (pool))
317 _cpp_next_chunk (pool, len, (unsigned char **) &start);
318 dest = &start[total_len];
321 /* No leading white space. */
322 if (token->flags & PREV_WHITE && total_len > 0)
323 *dest++ = ' ';
325 if (escape_it)
327 unsigned char *buf = (unsigned char *) xmalloc (len);
329 len = cpp_spell_token (pfile, token, buf) - buf;
330 dest = quote_string (dest, buf, len);
331 free (buf);
333 else
334 dest = cpp_spell_token (pfile, token, dest);
335 total_len = dest - start;
337 if (token->type == CPP_OTHER && token->val.c == '\\')
338 backslash_count++;
339 else
340 backslash_count = 0;
343 /* Ignore the final \ of invalid string literals. */
344 if (backslash_count & 1)
346 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
347 total_len--;
350 POOL_COMMIT (pool, total_len);
352 arg->stringified = xnew (cpp_token);
353 arg->stringified->flags = 0;
354 arg->stringified->type = CPP_STRING;
355 arg->stringified->val.str.text = start;
356 arg->stringified->val.str.len = total_len;
359 /* Try to paste two tokens. On success, the LHS becomes the pasted
360 token, and 0 is returned. For failure, we update the flags of the
361 RHS appropriately and return non-zero. */
362 static int
363 paste_tokens (pfile, lhs, rhs)
364 cpp_reader *pfile;
365 cpp_token *lhs, *rhs;
367 unsigned char flags;
368 int digraph = 0;
369 enum cpp_ttype type;
371 type = cpp_can_paste (pfile, lhs, rhs, &digraph);
373 if (type == CPP_EOF)
375 /* Mandatory warning for all apart from assembler. */
376 if (CPP_OPTION (pfile, lang) != CLK_ASM)
377 cpp_warning (pfile,
378 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
379 cpp_token_as_text (pfile, lhs),
380 cpp_token_as_text (pfile, rhs));
382 /* The standard states that behaviour is undefined. By the
383 principle of least surpise, we step back before the RHS, and
384 mark it to prevent macro expansion. Tests in the testsuite
385 rely on clearing PREV_WHITE here, though you could argue we
386 should actually set it. Assembler can have '.' in labels and
387 so requires that we don't insert spaces there. Maybe we should
388 change this to put out a space unless it's assembler. */
389 rhs->flags &= ~PREV_WHITE;
390 rhs->flags |= NO_EXPAND;
391 return 1;
394 flags = lhs->flags & ~DIGRAPH;
395 if (digraph)
396 flags |= DIGRAPH;
398 /* Identifiers and numbers need spellings to be pasted. */
399 if (type == CPP_NAME || type == CPP_NUMBER)
401 unsigned int total_len = cpp_token_len (lhs) + cpp_token_len (rhs);
402 unsigned char *result, *end;
404 result = _cpp_pool_alloc (&pfile->ident_pool, total_len + 1);
406 /* Paste the spellings and null terminate. */
407 end = cpp_spell_token (pfile, rhs, cpp_spell_token (pfile, lhs, result));
408 *end = '\0';
409 total_len = end - result;
411 if (type == CPP_NAME)
413 lhs->val.node = cpp_lookup (pfile, result, total_len);
414 if (lhs->val.node->flags & NODE_OPERATOR)
416 flags |= NAMED_OP;
417 lhs->type = lhs->val.node->value.operator;
420 else
422 lhs->val.str.text = result;
423 lhs->val.str.len = total_len;
426 else if (type == CPP_WCHAR || type == CPP_WSTRING)
427 lhs->val.str = rhs->val.str;
429 /* Set type and flags after pasting spellings. */
430 lhs->type = type;
431 lhs->flags = flags;
433 return 0;
436 /* Handles an arbitrarily long sequence of ## operators. This
437 implementation is left-associative, non-recursive, and finishes a
438 paste before handling succeeding ones. If the paste fails, we back
439 up a token to just after the ## operator, with the effect that it
440 appears in the output stream normally. */
441 static void
442 paste_all_tokens (pfile, lhs)
443 cpp_reader *pfile;
444 cpp_token *lhs;
446 cpp_token *rhs;
447 unsigned char orig_flags = lhs->flags;
451 /* Take the token directly from the current context. We can do
452 this, because we are in the replacement list of either an
453 object-like macro, or a function-like macro with arguments
454 inserted. In either case, the constraints to #define
455 guarantee we have at least one more token. */
456 rhs = pfile->context->list.first++;
457 if (paste_tokens (pfile, lhs, rhs))
459 /* We failed. Step back so we read the RHS in next. */
460 pfile->context->list.first--;
461 break;
464 while (rhs->flags & PASTE_LEFT);
466 /* The pasted token has the PREV_WHITE flag of the LHS, is no longer
467 PASTE_LEFT, and is subject to macro expansion. */
468 lhs->flags &= ~(PREV_WHITE | PASTE_LEFT | NO_EXPAND);
469 lhs->flags |= orig_flags & (PREV_WHITE | AVOID_LPASTE);
472 /* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS
473 is non-zero if this is a variadic macro. Returns the type of the
474 token that caused reading to finish. */
475 static enum cpp_ttype
476 parse_arg (pfile, arg, variadic)
477 cpp_reader *pfile;
478 struct macro_arg *arg;
479 int variadic;
481 enum cpp_ttype result;
482 unsigned int paren = 0;
483 unsigned int line;
485 arg->first = (cpp_token *) POOL_FRONT (&pfile->argument_pool);
486 for (;; arg->count++)
488 cpp_token *token = &arg->first[arg->count];
489 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->argument_pool))
491 _cpp_next_chunk (&pfile->argument_pool, sizeof (cpp_token),
492 (unsigned char **) &arg->first);
493 token = &arg->first[arg->count];
496 /* Newlines in arguments are white space (6.10.3.10). */
497 line = pfile->lexer_pos.output_line;
498 cpp_get_token (pfile, token);
499 if (line != pfile->lexer_pos.output_line)
500 token->flags |= PREV_WHITE;
502 result = token->type;
503 if (result == CPP_OPEN_PAREN)
504 paren++;
505 else if (result == CPP_CLOSE_PAREN && paren-- == 0)
506 break;
507 /* Commas are not terminators within parantheses or variadic. */
508 else if (result == CPP_COMMA && paren == 0 && !variadic)
509 break;
510 else if (result == CPP_EOF)
511 break; /* Error reported by caller. */
514 /* Commit the memory used to store the arguments. */
515 POOL_COMMIT (&pfile->argument_pool, arg->count * sizeof (cpp_token));
517 return result;
520 /* Parse the arguments making up a macro invocation. */
521 static macro_arg *
522 parse_args (pfile, node)
523 cpp_reader *pfile;
524 const cpp_hashnode *node;
526 cpp_macro *macro = node->value.macro;
527 macro_arg *args, *cur;
528 enum cpp_ttype type;
529 int argc, error = 0;
531 /* Allocate room for at least one argument, and zero it out. */
532 argc = macro->paramc ? macro->paramc: 1;
533 args = xcnewvec (macro_arg, argc);
535 for (cur = args, argc = 0; ;)
537 argc++;
539 type = parse_arg (pfile, cur, argc == macro->paramc && macro->variadic);
540 if (type == CPP_CLOSE_PAREN || type == CPP_EOF)
541 break;
543 /* Re-use the last argument for excess arguments. */
544 if (argc < macro->paramc)
545 cur++;
548 if (type == CPP_EOF)
550 cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
551 node->name);
552 error = 1;
554 else if (argc < macro->paramc)
556 /* As an extension, a rest argument is allowed to not appear in
557 the invocation at all.
558 e.g. #define debug(format, args...) something
559 debug("string");
561 This is exactly the same as if there had been an empty rest
562 argument - debug("string", ). */
564 if (argc + 1 == macro->paramc && macro->variadic)
566 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
567 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
569 else
571 cpp_error (pfile,
572 "macro \"%s\" requires %u arguments, but only %u given",
573 node->name, macro->paramc, argc);
574 error = 1;
577 else if (argc > macro->paramc)
579 /* Empty argument to a macro taking no arguments is OK. */
580 if (argc != 1 || cur->count)
582 cpp_error (pfile,
583 "macro \"%s\" passed %u arguments, but takes just %u",
584 node->name, argc, macro->paramc);
585 error = 1;
589 if (error)
591 free (args);
592 args = 0;
595 return args;
598 static int
599 funlike_invocation_p (pfile, node, list)
600 cpp_reader *pfile;
601 const cpp_hashnode *node;
602 struct toklist *list;
604 cpp_context *orig;
605 cpp_token maybe_paren;
606 macro_arg *args = 0;
607 cpp_lexer_pos macro_pos;
609 macro_pos = pfile->lexer_pos;
610 pfile->state.parsing_args = 1;
611 pfile->state.prevent_expansion++;
612 orig = pfile->context;
614 cpp_start_lookahead (pfile);
615 cpp_get_token (pfile, &maybe_paren);
616 cpp_stop_lookahead (pfile, maybe_paren.type == CPP_OPEN_PAREN);
618 if (maybe_paren.type == CPP_OPEN_PAREN)
619 args = parse_args (pfile, node);
620 else if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
621 cpp_warning (pfile,
622 "function-like macro \"%s\" must be used with arguments in traditional C",
623 node->name);
625 /* Restore original context. */
626 pfile->context = orig;
627 pfile->state.prevent_expansion--;
628 pfile->state.parsing_args = 0;
630 if (args)
632 /* The macro's expansion appears where the name would have. */
633 pfile->lexer_pos = macro_pos;
635 if (node->value.macro->paramc > 0)
637 /* Don't save tokens during pre-expansion. */
638 struct cpp_lookahead *la_saved = pfile->la_write;
639 pfile->la_write = 0;
640 replace_args (pfile, node->value.macro, args, list);
641 pfile->la_write = la_saved;
643 free (args);
646 return args != 0;
649 /* Push the context of a macro onto the context stack. TOKEN is the
650 macro name. If we can successfully start expanding the macro,
651 TOKEN is replaced with the first token of the expansion, and we
652 return non-zero. */
653 static int
654 enter_macro_context (pfile, node)
655 cpp_reader *pfile;
656 cpp_hashnode *node;
658 cpp_context *context;
659 cpp_macro *macro = node->value.macro;
660 struct toklist list;
662 /* Save the position of the outermost macro invocation. */
663 if (!pfile->context->prev)
664 lock_pools (pfile);
666 if (macro->fun_like && !funlike_invocation_p (pfile, node, &list))
668 if (!pfile->context->prev)
669 unlock_pools (pfile);
670 return 0;
673 if (macro->paramc == 0)
675 list.first = macro->expansion;
676 list.limit = macro->expansion + macro->count;
679 /* Only push a macro context for non-empty replacement lists. */
680 if (list.first != list.limit)
682 context = next_context (pfile);
683 context->list = list;
684 context->macro = macro;
686 /* Disable the macro within its expansion. */
687 macro->disabled = 1;
690 return 1;
693 /* Move to the next context. Create one if there is none. */
694 static cpp_context *
695 next_context (pfile)
696 cpp_reader *pfile;
698 cpp_context *prev = pfile->context;
699 cpp_context *result = prev->next;
701 if (result == 0)
703 result = xnew (cpp_context);
704 prev->next = result;
705 result->prev = prev;
706 result->next = 0;
709 pfile->context = result;
710 return result;
713 static void
714 replace_args (pfile, macro, args, list)
715 cpp_reader *pfile;
716 cpp_macro *macro;
717 macro_arg *args;
718 struct toklist *list;
720 unsigned char flags = 0;
721 unsigned int i, total;
722 const cpp_token *src, *limit;
723 cpp_token *dest;
724 macro_arg *arg;
726 src = macro->expansion;
727 limit = src + macro->count;
729 /* First, fully macro-expand arguments, calculating the number of
730 tokens in the final expansion as we go. This ensures that the
731 possible recursive use of argument_pool is fine. */
732 total = limit - src;
733 for (; src < limit; src++)
734 if (src->type == CPP_MACRO_ARG)
736 /* We have an argument. If it is not being stringified or
737 pasted it is macro-replaced before insertion. */
738 arg = &args[src->val.arg_no - 1];
740 if (src->flags & STRINGIFY_ARG)
742 if (!arg->stringified)
743 stringify_arg (pfile, arg);
745 else if ((src->flags & PASTE_LEFT)
746 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
747 total += arg->count - 1;
748 else
750 if (!arg->expanded)
752 arg->expanded_count = 0;
753 if (arg->count)
754 expand_arg (pfile, arg);
756 total += arg->expanded_count - 1;
760 dest = (cpp_token *) _cpp_pool_alloc (&pfile->argument_pool,
761 total * sizeof (cpp_token));
762 list->first = dest;
764 for (src = macro->expansion; src < limit; src++)
765 if (src->type == CPP_MACRO_ARG)
767 unsigned int count;
768 const cpp_token *from;
770 arg = &args[src->val.arg_no - 1];
771 if (src->flags & STRINGIFY_ARG)
772 from = arg->stringified, count = 1;
773 else if (src->flags & PASTE_LEFT)
774 count = arg->count, from = arg->first;
775 else if (src > macro->expansion && (src[-1].flags & PASTE_LEFT))
777 count = arg->count, from = arg->first;
778 if (dest != list->first)
780 /* GCC has special semantics for , ## b where b is a
781 varargs parameter: the comma disappears if b was
782 given no actual arguments (not merely if b is an
783 empty argument); otherwise pasting is turned off. */
784 if (dest[-1].type == CPP_COMMA
785 && macro->variadic
786 && src->val.arg_no == macro->paramc)
788 if (count == 0)
789 dest--;
790 else
791 dest[-1].flags &= ~PASTE_LEFT;
793 /* Count == 0 is the RHS a placemarker case. */
794 else if (count == 0)
795 dest[-1].flags &= ~PASTE_LEFT;
798 else
799 count = arg->expanded_count, from = arg->expanded;
801 /* Count == 0 is the LHS a placemarker case. */
802 if (count)
804 memcpy (dest, from, count * sizeof (cpp_token));
806 /* The first token gets PREV_WHITE of the CPP_MACRO_ARG. */
807 dest->flags &= ~PREV_WHITE;
808 dest->flags |= src->flags & PREV_WHITE;
809 dest->flags |= AVOID_LPASTE;
811 /* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */
812 dest[count - 1].flags |= src->flags & PASTE_LEFT;
814 dest += count;
817 /* The token after the argument must avoid an accidental paste. */
818 flags = AVOID_LPASTE;
820 else
822 *dest = *src;
823 dest->flags |= flags;
824 dest++;
825 flags = 0;
828 list->limit = dest;
830 /* Free the expanded arguments. */
831 for (i = 0; i < macro->paramc; i++)
833 if (args[i].expanded)
834 free (args[i].expanded);
835 if (args[i].stringified)
836 free (args[i].stringified);
840 /* Subroutine of expand_arg to put the unexpanded tokens on the
841 context stack. */
842 static cpp_context *
843 push_arg_context (pfile, arg)
844 cpp_reader *pfile;
845 macro_arg *arg;
847 cpp_context *context = next_context (pfile);
848 context->macro = 0;
849 context->list.first = arg->first;
850 context->list.limit = arg->first + arg->count;
852 return context;
855 static void
856 expand_arg (pfile, arg)
857 cpp_reader *pfile;
858 macro_arg *arg;
860 cpp_token *token;
861 unsigned int capacity = 256;
863 /* Loop, reading in the arguments. */
864 arg->expanded = (cpp_token *) xmalloc (capacity * sizeof (cpp_token));
866 push_arg_context (pfile, arg);
869 if (arg->expanded_count >= capacity)
871 capacity *= 2;
872 arg->expanded = (cpp_token *)
873 xrealloc (arg->expanded, capacity * sizeof (cpp_token));
875 token = &arg->expanded[arg->expanded_count++];
876 cpp_get_token (pfile, token);
878 while (token->type != CPP_EOF);
880 arg->expanded_count--;
882 /* Pop the context we pushed. */
883 pfile->context = pfile->context->prev;
886 void
887 _cpp_pop_context (pfile)
888 cpp_reader *pfile;
890 cpp_context *context = pfile->context;
892 pfile->context = context->prev;
893 if (!pfile->context->prev && !pfile->state.parsing_args)
894 unlock_pools (pfile);
896 /* Re-enable a macro, temporarily if parsing_args, when leaving its
897 expansion. */
898 context->macro->disabled = 0;
901 /* Eternal routine to get a token. Also used nearly everywhere
902 internally, except for places where we know we can safely call
903 the lexer directly, such as lexing a directive name.
905 Macro expansions and directives are transparently handled,
906 including entering included files. Thus tokens are post-macro
907 expansion, and after any intervening directives. External callers
908 see CPP_EOF only at EOF. Internal callers also see it when meeting
909 a directive inside a macro call, when at the end of a directive and
910 state.in_directive is still 1, and at the end of argument
911 pre-expansion. */
912 void
913 cpp_get_token (pfile, token)
914 cpp_reader *pfile;
915 cpp_token *token;
917 for (;;)
919 cpp_context *context = pfile->context;
921 if (pfile->la_read)
922 take_lookahead_token (pfile, token);
923 /* Context->prev == 0 <=> base context. */
924 else if (!context->prev)
925 _cpp_lex_token (pfile, token);
926 else if (context->list.first != context->list.limit)
928 *token = *context->list.first++;
929 token->flags |= pfile->buffer->saved_flags;
930 pfile->buffer->saved_flags = 0;
931 /* PASTE_LEFT tokens can only appear in macro expansions. */
932 if (token->flags & PASTE_LEFT)
934 paste_all_tokens (pfile, token);
935 pfile->buffer->saved_flags = AVOID_LPASTE;
938 else
940 if (context->macro)
942 /* Avoid accidental paste at the end of a macro. */
943 pfile->buffer->saved_flags |= AVOID_LPASTE;
944 _cpp_pop_context (pfile);
945 continue;
947 /* End of argument pre-expansion. */
948 token->type = CPP_EOF;
949 token->flags = 0;
950 return;
953 if (token->type != CPP_NAME)
954 break;
956 /* Handle macros and the _Pragma operator. */
957 if (token->val.node->type == NT_MACRO
958 && !pfile->state.prevent_expansion
959 && !(token->flags & NO_EXPAND))
961 cpp_hashnode *node = token->val.node;
963 /* Macros invalidate controlling macros. */
964 pfile->mi_state = MI_FAILED;
966 if (node->flags & NODE_BUILTIN)
968 builtin_macro (pfile, token);
969 pfile->buffer->saved_flags = AVOID_LPASTE;
970 break;
973 if (node->value.macro->disabled)
974 token->flags |= NO_EXPAND;
975 else if (enter_macro_context (pfile, node))
977 /* Pass AVOID_LPASTE and our PREV_WHITE to next token. */
978 pfile->buffer->saved_flags = ((token->flags & PREV_WHITE)
979 | AVOID_LPASTE);
980 continue;
984 /* Don't interpret _Pragma within directives. The standard is
985 not clear on this, but to me this makes most sense. */
986 if (token->val.node != pfile->spec_nodes.n__Pragma
987 || pfile->state.in_directive)
988 break;
990 /* Handle it, and loop back for another token. MI is cleared
991 since this token came from either the lexer or a macro. */
992 _cpp_do__Pragma (pfile);
995 if (pfile->la_write)
996 save_lookahead_token (pfile, token);
999 /* Returns true if we're expanding an object-like macro that was
1000 defined in a system header. Just checks the macro at the top of
1001 the stack. Used for diagnostic suppression. */
1003 cpp_sys_objmacro_p (pfile)
1004 cpp_reader *pfile;
1006 cpp_macro *macro = pfile->context->macro;
1008 return macro && ! macro->fun_like && macro->syshdr;
1011 /* Read each token in, until EOF. Directives are transparently
1012 processed. */
1013 void
1014 cpp_scan_buffer_nooutput (pfile, all_buffers)
1015 cpp_reader *pfile;
1016 int all_buffers;
1018 cpp_token token;
1019 cpp_buffer *buffer = all_buffers ? 0: pfile->buffer->prev;
1023 cpp_get_token (pfile, &token);
1024 while (token.type != CPP_EOF);
1025 while (cpp_pop_buffer (pfile) != buffer);
1028 /* Lookahead handling. */
1030 static void
1031 save_lookahead_token (pfile, token)
1032 cpp_reader *pfile;
1033 const cpp_token *token;
1035 if (token->type != CPP_EOF)
1037 cpp_lookahead *la = pfile->la_write;
1038 cpp_token_with_pos *twp;
1040 if (la->count == la->cap)
1042 la->cap += la->cap + 8;
1043 la->tokens = (cpp_token_with_pos *)
1044 xrealloc (la->tokens, la->cap * sizeof (cpp_token_with_pos));
1047 twp = &la->tokens[la->count++];
1048 twp->token = *token;
1049 twp->pos = *cpp_get_line (pfile);
1053 static void
1054 take_lookahead_token (pfile, token)
1055 cpp_reader *pfile;
1056 cpp_token *token;
1058 cpp_lookahead *la = pfile->la_read;
1059 cpp_token_with_pos *twp = &la->tokens[la->cur];
1061 *token = twp->token;
1062 pfile->lexer_pos = twp->pos;
1064 if (++la->cur == la->count)
1065 _cpp_release_lookahead (pfile);
1068 /* Moves the lookahead at the front of the read list to the free store. */
1069 void
1070 _cpp_release_lookahead (pfile)
1071 cpp_reader *pfile;
1073 cpp_lookahead *la = pfile->la_read;
1075 pfile->la_read = la->next;
1076 la->next = pfile->la_unused;
1077 pfile->la_unused = la;
1078 unlock_pools (pfile);
1081 /* Take a new lookahead from the free store, or allocate one if none. */
1082 static cpp_lookahead *
1083 alloc_lookahead (pfile)
1084 cpp_reader *pfile;
1086 cpp_lookahead *la = pfile->la_unused;
1088 if (la)
1089 pfile->la_unused = la->next;
1090 else
1092 la = xnew (cpp_lookahead);
1093 la->tokens = 0;
1094 la->cap = 0;
1097 la->cur = la->count = 0;
1098 return la;
1101 /* Free memory associated with a lookahead list. */
1102 static void
1103 free_lookahead (la)
1104 cpp_lookahead *la;
1106 if (la->tokens)
1107 free ((PTR) la->tokens);
1108 free ((PTR) la);
1111 /* Free all the lookaheads of a cpp_reader. */
1112 void
1113 _cpp_free_lookaheads (pfile)
1114 cpp_reader *pfile;
1116 cpp_lookahead *la, *lan;
1118 if (pfile->la_read)
1119 free_lookahead (pfile->la_read);
1120 if (pfile->la_write)
1121 free_lookahead (pfile->la_write);
1123 for (la = pfile->la_unused; la; la = lan)
1125 lan = la->next;
1126 free_lookahead (la);
1130 /* Allocate a lookahead and move it to the front of the write list. */
1131 void
1132 cpp_start_lookahead (pfile)
1133 cpp_reader *pfile;
1135 cpp_lookahead *la = alloc_lookahead (pfile);
1137 la->next = pfile->la_write;
1138 pfile->la_write = la;
1140 la->pos = *cpp_get_line (pfile);
1142 /* Don't allow memory pools to be re-used whilst we're reading ahead. */
1143 lock_pools (pfile);
1146 /* Stop reading ahead - either step back, or drop the read ahead. */
1147 void
1148 cpp_stop_lookahead (pfile, drop)
1149 cpp_reader *pfile;
1150 int drop;
1152 cpp_lookahead *la = pfile->la_write;
1154 pfile->la_write = la->next;
1155 la->next = pfile->la_read;
1156 pfile->la_read = la;
1158 if (drop || la->count == 0)
1159 _cpp_release_lookahead (pfile);
1160 else
1161 pfile->lexer_pos = la->pos;
1164 /* Push a single token back to the front of the queue. Only to be
1165 used by cpplib, and only then when necessary. POS is the position
1166 to report for the preceding token. */
1167 void
1168 _cpp_push_token (pfile, token, pos)
1169 cpp_reader *pfile;
1170 const cpp_token *token;
1171 const cpp_lexer_pos *pos;
1173 cpp_start_lookahead (pfile);
1174 save_lookahead_token (pfile, token);
1175 cpp_stop_lookahead (pfile, 0);
1176 pfile->lexer_pos = *pos;
1179 /* #define directive parsing and handling. */
1181 /* Returns non-zero if a macro redefinition is trivial. */
1182 static int
1183 check_macro_redefinition (pfile, node, macro2)
1184 cpp_reader *pfile;
1185 const cpp_hashnode *node;
1186 const cpp_macro *macro2;
1188 const cpp_macro *macro1;
1189 unsigned int i;
1191 if (node->type != NT_MACRO || node->flags & NODE_BUILTIN)
1192 return ! pfile->done_initializing;
1194 macro1 = node->value.macro;
1196 /* The quick failures. */
1197 if (macro1->count != macro2->count
1198 || macro1->paramc != macro2->paramc
1199 || macro1->fun_like != macro2->fun_like
1200 || macro1->variadic != macro2->variadic)
1201 return 0;
1203 /* Check each token. */
1204 for (i = 0; i < macro1->count; i++)
1205 if (! _cpp_equiv_tokens (&macro1->expansion[i], &macro2->expansion[i]))
1206 return 0;
1208 /* Check parameter spellings. */
1209 for (i = 0; i < macro1->paramc; i++)
1210 if (macro1->params[i] != macro2->params[i])
1211 return 0;
1213 return 1;
1216 /* Free the definition of hashnode H. */
1218 void
1219 _cpp_free_definition (h)
1220 cpp_hashnode *h;
1222 /* Macros and assertions no longer have anything to free. */
1223 h->type = NT_VOID;
1224 /* Clear builtin flag in case of redefinition. */
1225 h->flags &= ~NODE_BUILTIN;
1228 static int
1229 save_parameter (pfile, macro, node)
1230 cpp_reader *pfile;
1231 cpp_macro *macro;
1232 cpp_hashnode *node;
1234 cpp_hashnode **dest;
1236 /* Constraint 6.10.3.6 - duplicate parameter names. */
1237 if (node->arg_index)
1239 cpp_error (pfile, "duplicate macro parameter \"%s\"", node->name);
1240 return 1;
1243 dest = &macro->params[macro->paramc];
1245 /* Check we have room for the parameters. */
1246 if ((unsigned char *) (dest + 1) >= POOL_LIMIT (&pfile->macro_pool))
1248 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_hashnode *),
1249 (unsigned char **) &macro->params);
1250 dest = &macro->params[macro->paramc];
1253 *dest = node;
1254 node->arg_index = ++macro->paramc;
1255 return 0;
1258 static int
1259 parse_params (pfile, macro)
1260 cpp_reader *pfile;
1261 cpp_macro *macro;
1263 cpp_token token;
1264 unsigned int prev_ident = 0;
1266 macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool);
1267 for (;;)
1269 _cpp_lex_token (pfile, &token);
1271 switch (token.type)
1273 default:
1274 cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
1275 cpp_token_as_text (pfile, &token));
1276 return 0;
1278 case CPP_NAME:
1279 if (prev_ident)
1281 cpp_error (pfile, "macro parameters must be comma-separated");
1282 return 0;
1284 prev_ident = 1;
1286 if (save_parameter (pfile, macro, token.val.node))
1287 return 0;
1288 continue;
1290 case CPP_CLOSE_PAREN:
1291 if (prev_ident || macro->paramc == 0)
1292 break;
1294 /* Fall through to pick up the error. */
1295 case CPP_COMMA:
1296 if (!prev_ident)
1298 cpp_error (pfile, "parameter name missing");
1299 return 0;
1301 prev_ident = 0;
1302 continue;
1304 case CPP_ELLIPSIS:
1305 macro->variadic = 1;
1306 if (!prev_ident)
1308 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1309 pfile->state.va_args_ok = 1;
1310 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1311 cpp_pedwarn (pfile,
1312 "anonymous variadic macros were introduced in C99");
1314 else if (CPP_OPTION (pfile, pedantic))
1315 cpp_pedwarn (pfile, "ISO C does not permit named variadic macros");
1317 /* We're at the end, and just expect a closing parenthesis. */
1318 _cpp_lex_token (pfile, &token);
1319 if (token.type == CPP_CLOSE_PAREN)
1320 break;
1321 /* Fall through. */
1323 case CPP_EOF:
1324 cpp_error (pfile, "missing ')' in macro parameter list");
1325 return 0;
1328 /* Success. Commit the parameter array. */
1329 POOL_COMMIT (&pfile->macro_pool,
1330 macro->paramc * sizeof (cpp_hashnode *));
1331 return 1;
1335 /* Lex a token from a macro's replacement list. Translate it to a
1336 CPP_MACRO_ARG if appropriate. */
1337 static cpp_token *
1338 lex_expansion_token (pfile, macro)
1339 cpp_reader *pfile;
1340 cpp_macro *macro;
1342 cpp_token *token = &macro->expansion[macro->count];
1344 /* Check we have room for the token. */
1345 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
1347 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1348 (unsigned char **) &macro->expansion);
1349 token = &macro->expansion[macro->count];
1352 macro->count++;
1353 _cpp_lex_token (pfile, token);
1355 /* Is this an argument? */
1356 if (token->type == CPP_NAME && token->val.node->arg_index)
1358 token->type = CPP_MACRO_ARG;
1359 token->val.arg_no = token->val.node->arg_index;
1361 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1362 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1363 check_trad_stringification (pfile, macro, &token->val.str);
1365 return token;
1368 /* Parse a macro and save its expansion. Returns non-zero on success. */
1370 _cpp_create_definition (pfile, node)
1371 cpp_reader *pfile;
1372 cpp_hashnode *node;
1374 cpp_macro *macro;
1375 cpp_token *token;
1376 unsigned int i, ok = 1;
1378 macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool,
1379 sizeof (cpp_macro));
1380 macro->file = pfile->buffer->nominal_fname;
1381 macro->line = pfile->directive_pos.line;
1382 macro->params = 0;
1383 macro->paramc = 0;
1384 macro->fun_like = 0;
1385 macro->variadic = 0;
1386 macro->count = 0;
1387 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1389 /* Get the first token of the expansion (or the '(' of a
1390 function-like macro). */
1391 token = lex_expansion_token (pfile, macro);
1392 if (token->type == CPP_OPEN_PAREN && !(token->flags & PREV_WHITE))
1394 if (!(ok = parse_params (pfile, macro)))
1395 goto cleanup;
1396 macro->count = 0;
1397 macro->fun_like = 1;
1398 /* Some of the pool may have been used for the parameter store. */
1399 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1400 token = lex_expansion_token (pfile, macro);
1402 else if (token->type != CPP_EOF && !(token->flags & PREV_WHITE))
1403 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
1405 /* Setting it here means we don't catch leading comments. */
1406 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
1408 for (;;)
1410 /* Check the stringifying # constraint 6.10.3.2.1 of
1411 function-like macros when lexing the subsequent token. */
1412 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1414 if (token->type == CPP_MACRO_ARG)
1416 token->flags &= ~PREV_WHITE;
1417 token->flags |= STRINGIFY_ARG;
1418 token->flags |= token[-1].flags & PREV_WHITE;
1419 token[-1] = token[0];
1420 macro->count--;
1422 /* Let assembler get away with murder. */
1423 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1425 ok = 0;
1426 cpp_error (pfile, "'#' is not followed by a macro parameter");
1427 goto cleanup;
1431 if (token->type == CPP_EOF)
1432 break;
1434 /* Paste operator constraint 6.10.3.3.1. */
1435 if (token->type == CPP_PASTE)
1437 /* Token-paste ##, can appear in both object-like and
1438 function-like macros, but not at the ends. */
1439 if (--macro->count > 0)
1440 token = lex_expansion_token (pfile, macro);
1442 if (macro->count == 0 || token->type == CPP_EOF)
1444 ok = 0;
1445 cpp_error (pfile,
1446 "'##' cannot appear at either end of a macro expansion");
1447 goto cleanup;
1450 token[-1].flags |= PASTE_LEFT;
1451 /* Give it a PREV_WHITE for -dM etc. */
1452 token->flags |= PREV_WHITE;
1455 token = lex_expansion_token (pfile, macro);
1458 /* Don't count the CPP_EOF. */
1459 macro->count--;
1461 /* Clear the whitespace flag from the leading token. */
1462 macro->expansion[0].flags &= ~PREV_WHITE;
1464 /* Implement the macro-defined-to-itself optimisation. */
1465 macro->disabled = (macro->count == 1 && !macro->fun_like
1466 && macro->expansion[0].type == CPP_NAME
1467 && macro->expansion[0].val.node == node);
1469 /* To suppress some diagnostics. */
1470 macro->syshdr = pfile->buffer->sysp != 0;
1472 /* Commit the memory. */
1473 POOL_COMMIT (&pfile->macro_pool, macro->count * sizeof (cpp_token));
1475 /* Redefinition of a macro is allowed if and only if the old and new
1476 definitions are the same. (6.10.3 paragraph 2). */
1477 if (node->type != NT_VOID)
1479 if (CPP_PEDANTIC (pfile)
1480 && !check_macro_redefinition (pfile, node, macro))
1482 cpp_pedwarn_with_line (pfile, pfile->directive_pos.line,
1483 pfile->directive_pos.col,
1484 "\"%s\" redefined", node->name);
1486 if (pfile->done_initializing && node->type == NT_MACRO
1487 && !(node->flags & NODE_BUILTIN))
1488 cpp_pedwarn_with_file_and_line (pfile,
1489 node->value.macro->file,
1490 node->value.macro->line, 1,
1491 "this is the location of the previous definition");
1493 _cpp_free_definition (node);
1496 /* Enter definition in hash table. */
1497 node->type = NT_MACRO;
1498 node->value.macro = macro;
1500 cleanup:
1502 /* Stop the lexer accepting __VA_ARGS__. */
1503 pfile->state.va_args_ok = 0;
1505 /* Clear the fast argument lookup indices. */
1506 for (i = macro->paramc; i-- > 0; )
1507 macro->params[i]->arg_index = 0;
1509 return ok;
1512 /* Warn if a token in `string' matches one of the function macro
1513 arguments in `info'. This function assumes that the macro is a
1514 function macro and not an object macro. */
1515 static void
1516 check_trad_stringification (pfile, macro, string)
1517 cpp_reader *pfile;
1518 const cpp_macro *macro;
1519 const cpp_string *string;
1521 unsigned int i, len;
1522 const U_CHAR *p, *q, *limit = string->text + string->len;
1524 /* Loop over the string. */
1525 for (p = string->text; p < limit; p = q)
1527 /* Find the start of an identifier. */
1528 while (p < limit && !is_idstart (*p))
1529 p++;
1531 /* Find the end of the identifier. */
1532 q = p;
1533 while (q < limit && is_idchar (*q))
1534 q++;
1536 len = q - p;
1538 /* Loop over the function macro arguments to see if the
1539 identifier inside the string matches one of them. */
1540 for (i = 0; i < macro->paramc; i++)
1542 const cpp_hashnode *node = macro->params[i];
1544 if (node->length == len && !memcmp (p, node->name, len))
1546 cpp_warning (pfile,
1547 "macro argument \"%s\" would be stringified with -traditional.",
1548 node->name);
1549 break;
1555 /* Returns the expansion of a macro, in a format suitable to be read
1556 back in again, and therefore also for DWARF 2 debugging info.
1557 Caller is expected to generate the "#define NAME" bit. The
1558 returned text is temporary, and automatically freed later. */
1560 const unsigned char *
1561 cpp_macro_definition (pfile, node)
1562 cpp_reader *pfile;
1563 const cpp_hashnode *node;
1565 unsigned int i, len;
1566 const cpp_macro *macro = node->value.macro;
1567 unsigned char *buffer;
1569 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1571 cpp_ice (pfile, "invalid hash type %d in dump_definition", node->type);
1572 return 0;
1575 /* Calculate length. */
1576 len = 1; /* ' ' */
1577 if (macro->fun_like)
1579 len += 3; /* "()" plus possible final "." of ellipsis. */
1580 for (i = 0; i < macro->paramc; i++)
1581 len += macro->params[i]->length + 2; /* ", " */
1584 for (i = 0; i < macro->count; i++)
1586 cpp_token *token = &macro->expansion[i];
1588 if (token->type == CPP_MACRO_ARG)
1589 len += macro->params[token->val.arg_no - 1]->length;
1590 else
1591 len += cpp_token_len (token); /* Includes room for ' '. */
1592 if (token->flags & STRINGIFY_ARG)
1593 len++; /* "#" */
1594 if (token->flags & PASTE_LEFT)
1595 len += 3; /* " ##" */
1598 if (len > pfile->macro_buffer_len)
1600 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1601 pfile->macro_buffer_len = len;
1603 buffer = pfile->macro_buffer;
1605 /* Parameter names. */
1606 if (macro->fun_like)
1608 *buffer++ = '(';
1609 for (i = 0; i < macro->paramc; i++)
1611 cpp_hashnode *param = macro->params[i];
1613 if (param != pfile->spec_nodes.n__VA_ARGS__)
1615 memcpy (buffer, param->name, param->length);
1616 buffer += param->length;
1619 if (i + 1 < macro->paramc)
1620 *buffer++ = ',', *buffer++ = ' ';
1621 else if (macro->variadic)
1622 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1624 *buffer++ = ')';
1627 /* Expansion tokens. */
1628 if (macro->count)
1630 *buffer++ = ' ';
1631 for (i = 0; i < macro->count; i++)
1633 cpp_token *token = &macro->expansion[i];
1635 if (token->flags & PREV_WHITE)
1636 *buffer++ = ' ';
1637 if (token->flags & STRINGIFY_ARG)
1638 *buffer++ = '#';
1640 if (token->type == CPP_MACRO_ARG)
1642 len = macro->params[token->val.arg_no - 1]->length;
1643 memcpy (buffer, macro->params[token->val.arg_no - 1]->name, len);
1644 buffer += len;
1646 else
1647 buffer = cpp_spell_token (pfile, token, buffer);
1649 if (token->flags & PASTE_LEFT)
1651 *buffer++ = ' ';
1652 *buffer++ = '#';
1653 *buffer++ = '#';
1654 /* Next has PREV_WHITE; see _cpp_create_definition. */
1659 *buffer = '\0';
1660 return pfile->macro_buffer;