2001-01-23 Alexandre Petit-Bianco <apbianco@cygnus.com>
[official-gcc.git] / gcc / cppmacro.c
bloba7d71a096440ebbd395c1c67963f30a87c00c96a
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. */
49 typedef struct macro_arg macro_arg;
50 struct macro_arg
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. */
59 /* Macro expansion. */
61 static void lock_pools PARAMS ((cpp_reader *));
62 static void unlock_pools PARAMS ((cpp_reader *));
63 static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
64 static void builtin_macro PARAMS ((cpp_reader *, cpp_token *));
65 static cpp_context *push_arg_context PARAMS ((cpp_reader *, macro_arg *));
66 static enum cpp_ttype parse_arg PARAMS ((cpp_reader *, macro_arg *, int));
67 static macro_arg *parse_args PARAMS ((cpp_reader *, const cpp_hashnode *));
68 static cpp_context *next_context PARAMS ((cpp_reader *));
69 static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
70 static unsigned char *quote_string PARAMS ((unsigned char *,
71 const unsigned char *,
72 unsigned int));
73 static void make_string_token PARAMS ((cpp_pool *, cpp_token *,
74 const U_CHAR *, unsigned int));
75 static void make_number_token PARAMS ((cpp_reader *, cpp_token *, int));
76 static void stringify_arg PARAMS ((cpp_reader *, macro_arg *));
77 static void paste_all_tokens PARAMS ((cpp_reader *, cpp_token *));
78 static int paste_tokens PARAMS ((cpp_reader *, cpp_token *, cpp_token *));
79 static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *,
80 struct toklist *));
81 static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *,
82 struct toklist *));
84 /* Lookaheads. */
86 static void save_lookahead_token PARAMS ((cpp_reader *, const cpp_token *));
87 static void take_lookahead_token PARAMS ((cpp_reader *, cpp_token *));
88 static cpp_lookahead *alloc_lookahead PARAMS ((cpp_reader *));
89 static void free_lookahead PARAMS ((cpp_lookahead *));
91 /* #define directive parsing and handling. */
93 static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
94 static int check_macro_redefinition PARAMS ((cpp_reader *,
95 const cpp_hashnode *,
96 const cpp_macro *));
97 static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
98 static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
99 static void check_trad_stringification PARAMS ((cpp_reader *,
100 const cpp_macro *,
101 const cpp_string *));
103 /* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a
104 CPP_STRING token containing TEXT in quoted form. */
105 static void
106 make_string_token (pool, token, text, len)
107 cpp_pool *pool;
108 cpp_token *token;
109 const U_CHAR *text;
110 unsigned int len;
112 U_CHAR *buf = _cpp_pool_alloc (pool, len * 4);
114 token->type = CPP_STRING;
115 token->val.str.text = buf;
116 token->val.str.len = quote_string (buf, text, len) - buf;
117 token->flags = 0;
120 /* Allocates and converts a temporary token to a CPP_NUMBER token,
121 evaluating to NUMBER. */
122 static void
123 make_number_token (pfile, token, number)
124 cpp_reader *pfile;
125 cpp_token *token;
126 int number;
128 unsigned char *buf = _cpp_pool_alloc (&pfile->ident_pool, 20);
130 sprintf ((char *) buf, "%d", number);
131 token->type = CPP_NUMBER;
132 token->val.str.text = buf;
133 token->val.str.len = ustrlen (buf);
134 token->flags = 0;
137 static const char * const monthnames[] =
139 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
140 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
143 /* Handle builtin macros like __FILE__. */
144 static void
145 builtin_macro (pfile, token)
146 cpp_reader *pfile;
147 cpp_token *token;
149 unsigned char flags = token->flags & PREV_WHITE;
150 cpp_hashnode *node = token->val.node;
152 switch (node->value.builtin)
154 case BT_FILE:
155 case BT_BASE_FILE:
157 const char *name;
158 cpp_buffer *buffer = pfile->buffer;
160 if (node->value.builtin == BT_BASE_FILE)
161 while (buffer->prev)
162 buffer = buffer->prev;
164 name = buffer->nominal_fname;
165 make_string_token (&pfile->ident_pool, token,
166 (const unsigned char *) name, strlen (name));
168 break;
170 case BT_INCLUDE_LEVEL:
171 /* pfile->include_depth counts the primary source as level 1,
172 but historically __INCLUDE_DEPTH__ has called the primary
173 source level 0. */
174 make_number_token (pfile, token, pfile->include_depth - 1);
175 break;
177 case BT_SPECLINE:
178 /* If __LINE__ is embedded in a macro, it must expand to the
179 line of the macro's invocation, not its definition.
180 Otherwise things like assert() will not work properly. */
181 make_number_token (pfile, token, cpp_get_line (pfile)->line);
182 break;
184 case BT_STDC:
186 int stdc = 1;
188 if (STDC_0_IN_SYSTEM_HEADERS && CPP_IN_SYSTEM_HEADER (pfile)
189 && pfile->spec_nodes.n__STRICT_ANSI__->type == NT_VOID)
190 stdc = 0;
191 make_number_token (pfile, token, stdc);
193 break;
195 case BT_DATE:
196 case BT_TIME:
197 if (pfile->date.type == CPP_EOF)
199 /* Allocate __DATE__ and __TIME__ from permanent storage,
200 and save them in pfile so we don't have to do this again.
201 We don't generate these strings at init time because
202 time() and localtime() are very slow on some systems. */
203 time_t tt = time (NULL);
204 struct tm *tb = localtime (&tt);
206 make_string_token (&pfile->ident_pool, &pfile->date,
207 DSC("Oct 11 1347"));
208 make_string_token (&pfile->ident_pool, &pfile->time,
209 DSC("12:34:56"));
211 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
212 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
213 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
214 tb->tm_hour, tb->tm_min, tb->tm_sec);
216 *token = node->value.builtin == BT_DATE ? pfile->date: pfile->time;
217 break;
219 default:
220 cpp_ice (pfile, "invalid builtin macro \"%s\"", node->name);
221 break;
224 token->flags = flags;
227 /* Used by cpperror.c to obtain the correct line and column to report
228 in a diagnostic. */
229 const cpp_lexer_pos *
230 cpp_get_line (pfile)
231 cpp_reader *pfile;
233 return &pfile->lexer_pos;
236 static void
237 lock_pools (pfile)
238 cpp_reader *pfile;
240 _cpp_lock_pool (&pfile->argument_pool);
243 static void
244 unlock_pools (pfile)
245 cpp_reader *pfile;
247 _cpp_unlock_pool (&pfile->argument_pool);
250 /* Adds backslashes before all backslashes and double quotes appearing
251 in strings. Non-printable characters are converted to octal. */
252 static U_CHAR *
253 quote_string (dest, src, len)
254 U_CHAR *dest;
255 const U_CHAR *src;
256 unsigned int len;
258 while (len--)
260 U_CHAR c = *src++;
262 if (c == '\\' || c == '"')
264 *dest++ = '\\';
265 *dest++ = c;
267 else
269 if (ISPRINT (c))
270 *dest++ = c;
271 else
273 sprintf ((char *) dest, "\\%03o", c);
274 dest += 4;
279 return dest;
282 /* Convert a token sequence to a single string token according to the
283 rules of the ISO C #-operator. */
284 static void
285 stringify_arg (pfile, arg)
286 cpp_reader *pfile;
287 macro_arg *arg;
289 cpp_pool *pool = &pfile->ident_pool;
290 unsigned char *start = POOL_FRONT (pool);
291 unsigned int i, escape_it, total_len = 0, backslash_count = 0;
293 /* Loop, reading in the argument's tokens. */
294 for (i = 0; i < arg->count; i++)
296 unsigned char *dest;
297 const cpp_token *token = &arg->first[i];
298 unsigned int len = cpp_token_len (token);
300 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
301 || token->type == CPP_CHAR || token->type == CPP_WCHAR
302 || token->type == CPP_OSTRING);
304 if (escape_it)
305 /* Worst case is each char is octal. */
306 len *= 4;
307 len++; /* Room for initial space. */
309 dest = &start[total_len];
310 if (dest + len > POOL_LIMIT (pool))
312 _cpp_next_chunk (pool, len, (unsigned char **) &start);
313 dest = &start[total_len];
316 /* No leading white space. */
317 if (token->flags & PREV_WHITE && total_len > 0)
318 *dest++ = ' ';
320 if (escape_it)
322 unsigned char *buf = (unsigned char *) xmalloc (len);
324 len = cpp_spell_token (pfile, token, buf) - buf;
325 dest = quote_string (dest, buf, len);
326 free (buf);
328 else
329 dest = cpp_spell_token (pfile, token, dest);
330 total_len = dest - start;
332 if (token->type == CPP_OTHER && token->val.c == '\\')
333 backslash_count++;
334 else
335 backslash_count = 0;
338 /* Ignore the final \ of invalid string literals. */
339 if (backslash_count & 1)
341 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
342 total_len--;
345 POOL_COMMIT (pool, total_len);
347 arg->stringified = xnew (cpp_token);
348 arg->stringified->flags = 0;
349 arg->stringified->type = CPP_STRING;
350 arg->stringified->val.str.text = start;
351 arg->stringified->val.str.len = total_len;
354 /* Try to paste two tokens. On success, the LHS becomes the pasted
355 token, and 0 is returned. For failure, we update the flags of the
356 RHS appropriately and return non-zero. */
357 static int
358 paste_tokens (pfile, lhs, rhs)
359 cpp_reader *pfile;
360 cpp_token *lhs, *rhs;
362 unsigned char flags;
363 int digraph = 0;
364 enum cpp_ttype type;
366 type = cpp_can_paste (pfile, lhs, rhs, &digraph);
368 if (type == CPP_EOF)
370 /* Mandatory warning for all apart from assembler. */
371 if (CPP_OPTION (pfile, lang) != CLK_ASM)
372 cpp_warning (pfile,
373 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
374 cpp_token_as_text (pfile, lhs),
375 cpp_token_as_text (pfile, rhs));
377 /* The standard states that behaviour is undefined. By the
378 principle of least surpise, we step back before the RHS, and
379 mark it to prevent macro expansion. Tests in the testsuite
380 rely on clearing PREV_WHITE here, though you could argue we
381 should actually set it. Assembler can have '.' in labels and
382 so requires that we don't insert spaces there. Maybe we should
383 change this to put out a space unless it's assembler. */
384 rhs->flags &= ~PREV_WHITE;
385 rhs->flags |= NO_EXPAND;
386 return 1;
389 flags = lhs->flags & ~DIGRAPH;
390 if (digraph)
391 flags |= DIGRAPH;
393 /* Identifiers and numbers need spellings to be pasted. */
394 if (type == CPP_NAME || type == CPP_NUMBER)
396 unsigned int total_len = cpp_token_len (lhs) + cpp_token_len (rhs);
397 unsigned char *result, *end;
399 result = _cpp_pool_alloc (&pfile->ident_pool, total_len + 1);
401 /* Paste the spellings and null terminate. */
402 end = cpp_spell_token (pfile, rhs, cpp_spell_token (pfile, lhs, result));
403 *end = '\0';
404 total_len = end - result;
406 if (type == CPP_NAME)
408 lhs->val.node = cpp_lookup (pfile, result, total_len);
409 if (lhs->val.node->flags & NODE_OPERATOR)
411 flags |= NAMED_OP;
412 lhs->type = lhs->val.node->value.operator;
415 else
417 lhs->val.str.text = result;
418 lhs->val.str.len = total_len;
421 else if (type == CPP_WCHAR || type == CPP_WSTRING)
422 lhs->val.str = rhs->val.str;
424 /* Set type and flags after pasting spellings. */
425 lhs->type = type;
426 lhs->flags = flags;
428 return 0;
431 /* Handles an arbitrarily long sequence of ## operators. This
432 implementation is left-associative, non-recursive, and finishes a
433 paste before handling succeeding ones. If the paste fails, we back
434 up a token to just after the ## operator, with the effect that it
435 appears in the output stream normally. */
436 static void
437 paste_all_tokens (pfile, lhs)
438 cpp_reader *pfile;
439 cpp_token *lhs;
441 cpp_token *rhs;
442 unsigned char orig_flags = lhs->flags;
446 /* Take the token directly from the current context. We can do
447 this, because we are in the replacement list of either an
448 object-like macro, or a function-like macro with arguments
449 inserted. In either case, the constraints to #define
450 guarantee we have at least one more token. */
451 rhs = pfile->context->list.first++;
452 if (paste_tokens (pfile, lhs, rhs))
454 /* We failed. Step back so we read the RHS in next. */
455 pfile->context->list.first--;
456 break;
459 while (rhs->flags & PASTE_LEFT);
461 /* The pasted token has the PREV_WHITE flag of the LHS, is no longer
462 PASTE_LEFT, and is subject to macro expansion. */
463 lhs->flags &= ~(PREV_WHITE | PASTE_LEFT | NO_EXPAND);
464 lhs->flags |= orig_flags & PREV_WHITE;
467 /* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS
468 is non-zero if this is a variadic macro. Returns the type of the
469 token that caused reading to finish. */
470 static enum cpp_ttype
471 parse_arg (pfile, arg, variadic)
472 cpp_reader *pfile;
473 struct macro_arg *arg;
474 int variadic;
476 enum cpp_ttype result;
477 unsigned int paren = 0;
478 unsigned int line;
480 arg->first = (cpp_token *) POOL_FRONT (&pfile->argument_pool);
481 for (;; arg->count++)
483 cpp_token *token = &arg->first[arg->count];
484 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->argument_pool))
486 _cpp_next_chunk (&pfile->argument_pool, sizeof (cpp_token),
487 (unsigned char **) &arg->first);
488 token = &arg->first[arg->count];
491 /* Newlines in arguments are white space (6.10.3.10). */
492 line = pfile->lexer_pos.output_line;
493 cpp_get_token (pfile, token);
494 if (line != pfile->lexer_pos.output_line)
495 token->flags |= PREV_WHITE;
497 result = token->type;
498 if (result == CPP_OPEN_PAREN)
499 paren++;
500 else if (result == CPP_CLOSE_PAREN && paren-- == 0)
501 break;
502 /* Commas are not terminators within parantheses or variadic. */
503 else if (result == CPP_COMMA && paren == 0 && !variadic)
504 break;
505 else if (result == CPP_EOF)
506 break; /* Error reported by caller. */
509 /* Commit the memory used to store the arguments. */
510 POOL_COMMIT (&pfile->argument_pool, arg->count * sizeof (cpp_token));
512 return result;
515 /* Parse the arguments making up a macro invocation. */
516 static macro_arg *
517 parse_args (pfile, node)
518 cpp_reader *pfile;
519 const cpp_hashnode *node;
521 cpp_macro *macro = node->value.macro;
522 macro_arg *args, *cur;
523 enum cpp_ttype type;
524 int argc, error = 0;
526 /* Allocate room for at least one argument, and zero it out. */
527 argc = macro->paramc ? macro->paramc: 1;
528 args = xcnewvec (macro_arg, argc);
530 for (cur = args, argc = 0; ;)
532 argc++;
534 type = parse_arg (pfile, cur, argc == macro->paramc && macro->variadic);
535 if (type == CPP_CLOSE_PAREN || type == CPP_EOF)
536 break;
538 /* Re-use the last argument for excess arguments. */
539 if (argc < macro->paramc)
540 cur++;
543 if (type == CPP_EOF)
545 cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
546 node->name);
547 error = 1;
549 else if (argc < macro->paramc)
551 /* As an extension, a rest argument is allowed to not appear in
552 the invocation at all.
553 e.g. #define debug(format, args...) something
554 debug("string");
556 This is exactly the same as if there had been an empty rest
557 argument - debug("string", ). */
559 if (argc + 1 == macro->paramc && macro->variadic)
561 if (CPP_PEDANTIC (pfile))
562 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
564 else
566 cpp_error (pfile,
567 "macro \"%s\" requires %u arguments, but only %u given",
568 node->name, macro->paramc, argc);
569 error = 1;
572 else if (argc > macro->paramc)
574 /* Empty argument to a macro taking no arguments is OK. */
575 if (argc != 1 || cur->count)
577 cpp_error (pfile,
578 "macro \"%s\" passed %u arguments, but takes just %u",
579 node->name, argc, macro->paramc);
580 error = 1;
584 if (error)
586 free (args);
587 args = 0;
590 return args;
593 static int
594 funlike_invocation_p (pfile, node, list)
595 cpp_reader *pfile;
596 const cpp_hashnode *node;
597 struct toklist *list;
599 cpp_context *orig;
600 cpp_token maybe_paren;
601 macro_arg *args = 0;
602 cpp_lexer_pos macro_pos;
604 macro_pos = pfile->lexer_pos;
605 pfile->state.parsing_args = 1;
606 pfile->state.prevent_expansion++;
607 orig = pfile->context;
609 cpp_start_lookahead (pfile);
610 cpp_get_token (pfile, &maybe_paren);
611 cpp_stop_lookahead (pfile, maybe_paren.type == CPP_OPEN_PAREN);
613 if (maybe_paren.type == CPP_OPEN_PAREN)
614 args = parse_args (pfile, node);
615 else if (CPP_WTRADITIONAL (pfile))
616 cpp_warning (pfile,
617 "function-like macro \"%s\" must be used with arguments in traditional C",
618 node->name);
620 /* Restore original context. */
621 pfile->context = orig;
622 pfile->state.prevent_expansion--;
623 pfile->state.parsing_args = 0;
625 if (args)
627 /* The macro's expansion appears where the name would have. */
628 pfile->lexer_pos = macro_pos;
630 if (node->value.macro->paramc > 0)
632 /* Don't save tokens during pre-expansion. */
633 struct cpp_lookahead *la_saved = pfile->la_write;
634 pfile->la_write = 0;
635 replace_args (pfile, node->value.macro, args, list);
636 pfile->la_write = la_saved;
638 free (args);
641 return args != 0;
644 /* Push the context of a macro onto the context stack. TOKEN is the
645 macro name. If we can successfully start expanding the macro,
646 TOKEN is replaced with the first token of the expansion, and we
647 return non-zero. */
648 static int
649 enter_macro_context (pfile, node)
650 cpp_reader *pfile;
651 cpp_hashnode *node;
653 cpp_context *context;
654 cpp_macro *macro = node->value.macro;
655 struct toklist list;
657 /* Save the position of the outermost macro invocation. */
658 if (!pfile->context->prev)
659 lock_pools (pfile);
661 if (macro->fun_like && !funlike_invocation_p (pfile, node, &list))
663 if (!pfile->context->prev)
664 unlock_pools (pfile);
665 return 0;
668 if (macro->paramc == 0)
670 list.first = macro->expansion;
671 list.limit = macro->expansion + macro->count;
674 if (list.first != list.limit)
676 /* Push its context. */
677 context = next_context (pfile);
678 context->list = list;
679 context->macro = macro;
681 /* Disable the macro within its expansion. */
682 macro->disabled = 1;
685 return 1;
688 /* Move to the next context. Create one if there is none. */
689 static cpp_context *
690 next_context (pfile)
691 cpp_reader *pfile;
693 cpp_context *prev = pfile->context;
694 cpp_context *result = prev->next;
696 if (result == 0)
698 result = xnew (cpp_context);
699 prev->next = result;
700 result->prev = prev;
701 result->next = 0;
704 pfile->context = result;
705 return result;
708 static void
709 replace_args (pfile, macro, args, list)
710 cpp_reader *pfile;
711 cpp_macro *macro;
712 macro_arg *args;
713 struct toklist *list;
715 unsigned int i, total;
716 const cpp_token *src, *limit;
717 cpp_token *dest;
718 macro_arg *arg;
720 src = macro->expansion;
721 limit = src + macro->count;
723 /* First, fully macro-expand arguments, calculating the number of
724 tokens in the final expansion as we go. This ensures that the
725 possible recursive use of argument_pool is fine. */
726 total = limit - src;
727 for (; src < limit; src++)
728 if (src->type == CPP_MACRO_ARG)
730 /* We have an argument. If it is not being stringified or
731 pasted it is macro-replaced before insertion. */
732 arg = &args[src->val.arg_no - 1];
734 if (src->flags & STRINGIFY_ARG)
736 if (!arg->stringified)
737 stringify_arg (pfile, arg);
739 else if ((src->flags & PASTE_LEFT)
740 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
741 total += arg->count - 1;
742 else
744 if (!arg->expanded)
746 arg->expanded_count = 0;
747 if (arg->count)
748 expand_arg (pfile, arg);
750 total += arg->expanded_count - 1;
754 dest = (cpp_token *) _cpp_pool_alloc (&pfile->argument_pool,
755 total * sizeof (cpp_token));
756 list->first = dest;
758 for (src = macro->expansion; src < limit; src++)
759 if (src->type == CPP_MACRO_ARG)
761 unsigned int count;
762 const cpp_token *from;
764 arg = &args[src->val.arg_no - 1];
765 if (src->flags & STRINGIFY_ARG)
766 from = arg->stringified, count = 1;
767 else if (src->flags & PASTE_LEFT)
768 count = arg->count, from = arg->first;
769 else if (src > macro->expansion && (src[-1].flags & PASTE_LEFT))
771 count = arg->count, from = arg->first;
772 if (dest != list->first)
774 /* GCC has special semantics for , ## b where b is a
775 varargs parameter: the comma disappears if b was
776 given no actual arguments (not merely if b is an
777 empty argument); otherwise pasting is turned off. */
778 if (dest[-1].type == CPP_COMMA
779 && macro->variadic
780 && src->val.arg_no == macro->paramc)
782 if (count == 0)
783 dest--;
784 else
785 dest[-1].flags &= ~PASTE_LEFT;
787 /* Count == 0 is the RHS a placemarker case. */
788 else if (count == 0)
789 dest[-1].flags &= ~PASTE_LEFT;
792 else
793 count = arg->expanded_count, from = arg->expanded;
795 /* Count == 0 is the LHS a placemarker case. */
796 if (count)
798 memcpy (dest, from, count * sizeof (cpp_token));
800 /* The first token gets PREV_WHITE of the CPP_MACRO_ARG. */
801 dest->flags &= ~PREV_WHITE;
802 dest->flags |= src->flags & PREV_WHITE;
804 /* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */
805 dest[count - 1].flags |= src->flags & PASTE_LEFT;
807 dest += count;
810 else
811 *dest++ = *src;
813 list->limit = dest;
815 /* Free the expanded arguments. */
816 for (i = 0; i < macro->paramc; i++)
818 if (args[i].expanded)
819 free (args[i].expanded);
820 if (args[i].stringified)
821 free (args[i].stringified);
825 /* Subroutine of expand_arg to put the unexpanded tokens on the
826 context stack. */
827 static cpp_context *
828 push_arg_context (pfile, arg)
829 cpp_reader *pfile;
830 macro_arg *arg;
832 cpp_context *context = next_context (pfile);
833 context->macro = 0;
834 context->list.first = arg->first;
835 context->list.limit = arg->first + arg->count;
837 return context;
840 static void
841 expand_arg (pfile, arg)
842 cpp_reader *pfile;
843 macro_arg *arg;
845 cpp_token *token;
846 unsigned int capacity = 256;
848 /* Loop, reading in the arguments. */
849 arg->expanded = (cpp_token *) xmalloc (capacity * sizeof (cpp_token));
851 push_arg_context (pfile, arg);
854 if (arg->expanded_count >= capacity)
856 capacity *= 2;
857 arg->expanded = (cpp_token *)
858 xrealloc (arg->expanded, capacity * sizeof (cpp_token));
860 token = &arg->expanded[arg->expanded_count++];
861 cpp_get_token (pfile, token);
863 while (token->type != CPP_EOF);
865 arg->expanded_count--;
867 /* Pop the context we pushed. */
868 pfile->context = pfile->context->prev;
871 void
872 _cpp_pop_context (pfile)
873 cpp_reader *pfile;
875 cpp_context *context = pfile->context;
877 pfile->context = context->prev;
878 if (!pfile->context->prev && !pfile->state.parsing_args)
879 unlock_pools (pfile);
881 /* Re-enable a macro, temporarily if parsing_args, when leaving its
882 expansion. */
883 context->macro->disabled = 0;
886 /* Eternal routine to get a token. Also used nearly everywhere
887 internally, except for places where we know we can safely call
888 the lexer directly, such as lexing a directive name.
890 Macro expansions and directives are transparently handled,
891 including entering included files. Thus tokens are post-macro
892 expansion, and after any intervening directives. External callers
893 see CPP_EOF only at EOF. Internal callers also see it when meeting
894 a directive inside a macro call, when at the end of a directive and
895 state.in_directive is still 1, and at the end of argument
896 pre-expansion. */
897 void
898 cpp_get_token (pfile, token)
899 cpp_reader *pfile;
900 cpp_token *token;
902 unsigned char flags = 0;
904 for (;;)
906 cpp_context *context = pfile->context;
908 if (pfile->la_read)
909 take_lookahead_token (pfile, token);
910 /* Context->prev == 0 <=> base context. */
911 else if (!context->prev)
912 _cpp_lex_token (pfile, token);
913 else if (context->list.first != context->list.limit)
915 *token = *context->list.first++;
916 token->flags |= flags;
917 flags = 0;
918 /* PASTE_LEFT tokens can only appear in macro expansions. */
919 if (token->flags & PASTE_LEFT)
920 paste_all_tokens (pfile, token);
922 else
924 if (context->macro)
926 _cpp_pop_context (pfile);
927 continue;
929 /* End of argument pre-expansion. */
930 token->type = CPP_EOF;
931 token->flags = 0;
932 return;
935 if (token->type != CPP_NAME)
936 break;
938 /* Handle macros and the _Pragma operator. */
939 if (token->val.node->type == NT_MACRO
940 && !pfile->state.prevent_expansion
941 && !(token->flags & NO_EXPAND))
943 cpp_hashnode *node = token->val.node;
945 /* Macros invalidate controlling macros. */
946 pfile->mi_state = MI_FAILED;
948 if (node->flags & NODE_BUILTIN)
950 builtin_macro (pfile, token);
951 break;
954 /* Merge PREV_WHITE of tokens. */
955 flags = token->flags & PREV_WHITE;
957 if (node->value.macro->disabled)
958 token->flags |= NO_EXPAND;
959 else if (enter_macro_context (pfile, node))
960 continue;
963 /* Don't interpret _Pragma within directives. The standard is
964 not clear on this, but to me this makes most sense. */
965 if (token->val.node != pfile->spec_nodes.n__Pragma
966 || pfile->state.in_directive)
967 break;
969 /* Handle it, and loop back for another token. MI is cleared
970 since this token came from either the lexer or a macro. */
971 _cpp_do__Pragma (pfile);
974 if (pfile->la_write)
975 save_lookahead_token (pfile, token);
978 /* Read each token in, until EOF. Directives are transparently
979 processed. */
980 void
981 cpp_scan_buffer_nooutput (pfile, all_buffers)
982 cpp_reader *pfile;
983 int all_buffers;
985 cpp_token token;
986 cpp_buffer *buffer = all_buffers ? 0: pfile->buffer->prev;
990 cpp_get_token (pfile, &token);
991 while (token.type != CPP_EOF);
992 while (cpp_pop_buffer (pfile) != buffer);
995 /* Lookahead handling. */
997 static void
998 save_lookahead_token (pfile, token)
999 cpp_reader *pfile;
1000 const cpp_token *token;
1002 if (token->type != CPP_EOF)
1004 cpp_lookahead *la = pfile->la_write;
1005 cpp_token_with_pos *twp;
1007 if (la->count == la->cap)
1009 la->cap += la->cap + 8;
1010 la->tokens = (cpp_token_with_pos *)
1011 xrealloc (la->tokens, la->cap * sizeof (cpp_token_with_pos));
1014 twp = &la->tokens[la->count++];
1015 twp->token = *token;
1016 twp->pos = *cpp_get_line (pfile);
1020 static void
1021 take_lookahead_token (pfile, token)
1022 cpp_reader *pfile;
1023 cpp_token *token;
1025 cpp_lookahead *la = pfile->la_read;
1026 cpp_token_with_pos *twp = &la->tokens[la->cur];
1028 *token = twp->token;
1029 pfile->lexer_pos = twp->pos;
1031 if (++la->cur == la->count)
1032 _cpp_release_lookahead (pfile);
1035 /* Moves the lookahead at the front of the read list to the free store. */
1036 void
1037 _cpp_release_lookahead (pfile)
1038 cpp_reader *pfile;
1040 cpp_lookahead *la = pfile->la_read;
1042 pfile->la_read = la->next;
1043 la->next = pfile->la_unused;
1044 pfile->la_unused = la;
1045 unlock_pools (pfile);
1048 /* Take a new lookahead from the free store, or allocate one if none. */
1049 static cpp_lookahead *
1050 alloc_lookahead (pfile)
1051 cpp_reader *pfile;
1053 cpp_lookahead *la = pfile->la_unused;
1055 if (la)
1056 pfile->la_unused = la->next;
1057 else
1059 la = xnew (cpp_lookahead);
1060 la->tokens = 0;
1061 la->cap = 0;
1064 la->cur = la->count = 0;
1065 return la;
1068 /* Free memory associated with a lookahead list. */
1069 static void
1070 free_lookahead (la)
1071 cpp_lookahead *la;
1073 if (la->tokens)
1074 free ((PTR) la->tokens);
1075 free ((PTR) la);
1078 /* Free all the lookaheads of a cpp_reader. */
1079 void
1080 _cpp_free_lookaheads (pfile)
1081 cpp_reader *pfile;
1083 cpp_lookahead *la, *lan;
1085 if (pfile->la_read)
1086 free_lookahead (pfile->la_read);
1087 if (pfile->la_write)
1088 free_lookahead (pfile->la_write);
1090 for (la = pfile->la_unused; la; la = lan)
1092 lan = la->next;
1093 free_lookahead (la);
1097 /* Allocate a lookahead and move it to the front of the write list. */
1098 void
1099 cpp_start_lookahead (pfile)
1100 cpp_reader *pfile;
1102 cpp_lookahead *la = alloc_lookahead (pfile);
1104 la->next = pfile->la_write;
1105 pfile->la_write = la;
1107 la->pos = *cpp_get_line (pfile);
1109 /* Don't allow memory pools to be re-used whilst we're reading ahead. */
1110 lock_pools (pfile);
1113 /* Stop reading ahead - either step back, or drop the read ahead. */
1114 void
1115 cpp_stop_lookahead (pfile, drop)
1116 cpp_reader *pfile;
1117 int drop;
1119 cpp_lookahead *la = pfile->la_write;
1121 pfile->la_write = la->next;
1122 la->next = pfile->la_read;
1123 pfile->la_read = la;
1125 if (drop || la->count == 0)
1126 _cpp_release_lookahead (pfile);
1127 else
1128 pfile->lexer_pos = la->pos;
1131 /* Push a single token back to the front of the queue. Only to be
1132 used by cpplib, and only then when necessary. POS is the position
1133 to report for the preceding token. */
1134 void
1135 _cpp_push_token (pfile, token, pos)
1136 cpp_reader *pfile;
1137 const cpp_token *token;
1138 const cpp_lexer_pos *pos;
1140 cpp_start_lookahead (pfile);
1141 save_lookahead_token (pfile, token);
1142 cpp_stop_lookahead (pfile, 0);
1143 pfile->lexer_pos = *pos;
1146 /* #define directive parsing and handling. */
1148 /* Returns non-zero if a macro redefinition is trivial. */
1149 static int
1150 check_macro_redefinition (pfile, node, macro2)
1151 cpp_reader *pfile;
1152 const cpp_hashnode *node;
1153 const cpp_macro *macro2;
1155 const cpp_macro *macro1;
1156 unsigned int i;
1158 if (node->type != NT_MACRO || node->flags & NODE_BUILTIN)
1159 return ! pfile->done_initializing;
1161 macro1 = node->value.macro;
1163 /* The quick failures. */
1164 if (macro1->count != macro2->count
1165 || macro1->paramc != macro2->paramc
1166 || macro1->fun_like != macro2->fun_like
1167 || macro1->variadic != macro2->variadic)
1168 return 0;
1170 /* Check each token. */
1171 for (i = 0; i < macro1->count; i++)
1172 if (! _cpp_equiv_tokens (&macro1->expansion[i], &macro2->expansion[i]))
1173 return 0;
1175 /* Check parameter spellings. */
1176 for (i = 0; i < macro1->paramc; i++)
1177 if (macro1->params[i] != macro2->params[i])
1178 return 0;
1180 return 1;
1183 /* Free the definition of hashnode H. */
1185 void
1186 _cpp_free_definition (h)
1187 cpp_hashnode *h;
1189 /* Macros and assertions no longer have anything to free. */
1190 h->type = NT_VOID;
1191 /* Clear builtin flag in case of redefinition. */
1192 h->flags &= ~NODE_BUILTIN;
1195 static int
1196 save_parameter (pfile, macro, node)
1197 cpp_reader *pfile;
1198 cpp_macro *macro;
1199 cpp_hashnode *node;
1201 cpp_hashnode **dest;
1203 /* Constraint 6.10.3.6 - duplicate parameter names. */
1204 if (node->arg_index)
1206 cpp_error (pfile, "duplicate macro parameter \"%s\"", node->name);
1207 return 1;
1210 dest = &macro->params[macro->paramc];
1212 /* Check we have room for the parameters. */
1213 if ((unsigned char *) (dest + 1) >= POOL_LIMIT (&pfile->macro_pool))
1215 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_hashnode *),
1216 (unsigned char **) &macro->params);
1217 dest = &macro->params[macro->paramc];
1220 *dest = node;
1221 node->arg_index = ++macro->paramc;
1222 return 0;
1225 static int
1226 parse_params (pfile, macro)
1227 cpp_reader *pfile;
1228 cpp_macro *macro;
1230 cpp_token token;
1231 unsigned int prev_ident = 0;
1233 macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool);
1234 for (;;)
1236 _cpp_lex_token (pfile, &token);
1238 switch (token.type)
1240 default:
1241 cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
1242 cpp_token_as_text (pfile, &token));
1243 return 0;
1245 case CPP_NAME:
1246 if (prev_ident)
1248 cpp_error (pfile, "macro parameters must be comma-separated");
1249 return 0;
1251 prev_ident = 1;
1253 if (save_parameter (pfile, macro, token.val.node))
1254 return 0;
1255 continue;
1257 case CPP_CLOSE_PAREN:
1258 if (prev_ident || macro->paramc == 0)
1259 break;
1261 /* Fall through to pick up the error. */
1262 case CPP_COMMA:
1263 if (!prev_ident)
1265 cpp_error (pfile, "parameter name missing");
1266 return 0;
1268 prev_ident = 0;
1269 continue;
1271 case CPP_ELLIPSIS:
1272 macro->variadic = 1;
1273 if (!prev_ident)
1275 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1276 pfile->state.va_args_ok = 1;
1277 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1278 cpp_pedwarn (pfile,
1279 "anonymous variadic macros were introduced in C99");
1281 else if (CPP_OPTION (pfile, pedantic))
1282 cpp_pedwarn (pfile, "ISO C does not permit named variadic macros");
1284 /* We're at the end, and just expect a closing parenthesis. */
1285 _cpp_lex_token (pfile, &token);
1286 if (token.type == CPP_CLOSE_PAREN)
1287 break;
1288 /* Fall through. */
1290 case CPP_EOF:
1291 cpp_error (pfile, "missing ')' in macro parameter list");
1292 return 0;
1295 /* Success. Commit the parameter array. */
1296 POOL_COMMIT (&pfile->macro_pool,
1297 macro->paramc * sizeof (cpp_hashnode *));
1298 return 1;
1302 /* Lex a token from a macro's replacement list. Translate it to a
1303 CPP_MACRO_ARG if appropriate. */
1304 static cpp_token *
1305 lex_expansion_token (pfile, macro)
1306 cpp_reader *pfile;
1307 cpp_macro *macro;
1309 cpp_token *token = &macro->expansion[macro->count];
1311 /* Check we have room for the token. */
1312 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
1314 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1315 (unsigned char **) &macro->expansion);
1316 token = &macro->expansion[macro->count];
1319 macro->count++;
1320 _cpp_lex_token (pfile, token);
1322 /* Is this an argument? */
1323 if (token->type == CPP_NAME && token->val.node->arg_index)
1325 token->type = CPP_MACRO_ARG;
1326 token->val.arg_no = token->val.node->arg_index;
1328 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1329 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1330 check_trad_stringification (pfile, macro, &token->val.str);
1332 return token;
1335 /* Parse a macro and save its expansion. Returns non-zero on success. */
1337 _cpp_create_definition (pfile, node)
1338 cpp_reader *pfile;
1339 cpp_hashnode *node;
1341 cpp_macro *macro;
1342 cpp_token *token;
1343 unsigned int i, ok = 1;
1345 macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool,
1346 sizeof (cpp_macro));
1347 macro->file = pfile->buffer->nominal_fname;
1348 macro->line = pfile->directive_pos.line;
1349 macro->params = 0;
1350 macro->paramc = 0;
1351 macro->fun_like = 0;
1352 macro->variadic = 0;
1353 macro->count = 0;
1354 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1356 /* Get the first token of the expansion (or the '(' of a
1357 function-like macro). */
1358 token = lex_expansion_token (pfile, macro);
1359 if (token->type == CPP_OPEN_PAREN && !(token->flags & PREV_WHITE))
1361 if (!(ok = parse_params (pfile, macro)))
1362 goto cleanup;
1363 macro->count = 0;
1364 macro->fun_like = 1;
1365 /* Some of the pool may have been used for the parameter store. */
1366 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1367 token = lex_expansion_token (pfile, macro);
1369 else if (token->type != CPP_EOF && !(token->flags & PREV_WHITE))
1370 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
1372 /* Setting it here means we don't catch leading comments. */
1373 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
1375 for (;;)
1377 /* Check the stringifying # constraint 6.10.3.2.1 of
1378 function-like macros when lexing the subsequent token. */
1379 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1381 if (token->type == CPP_MACRO_ARG)
1383 token->flags &= ~PREV_WHITE;
1384 token->flags |= STRINGIFY_ARG;
1385 token->flags |= token[-1].flags & PREV_WHITE;
1386 token[-1] = token[0];
1387 macro->count--;
1389 /* Let assembler get away with murder. */
1390 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1392 ok = 0;
1393 cpp_error (pfile, "'#' is not followed by a macro parameter");
1394 goto cleanup;
1398 if (token->type == CPP_EOF)
1399 break;
1401 /* Paste operator constraint 6.10.3.3.1. */
1402 if (token->type == CPP_PASTE)
1404 /* Token-paste ##, can appear in both object-like and
1405 function-like macros, but not at the ends. */
1406 if (--macro->count > 0)
1407 token = lex_expansion_token (pfile, macro);
1409 if (macro->count == 0 || token->type == CPP_EOF)
1411 ok = 0;
1412 cpp_error (pfile,
1413 "'##' cannot appear at either end of a macro expansion");
1414 goto cleanup;
1417 token[-1].flags |= PASTE_LEFT;
1418 /* Give it a PREV_WHITE for -dM etc. */
1419 token->flags |= PREV_WHITE;
1422 token = lex_expansion_token (pfile, macro);
1425 /* Don't count the CPP_EOF. */
1426 macro->count--;
1428 /* Clear the whitespace flag from the leading token. */
1429 macro->expansion[0].flags &= ~PREV_WHITE;
1431 /* Implement the macro-defined-to-itself optimisation. */
1432 macro->disabled = (macro->count == 1 && !macro->fun_like
1433 && macro->expansion[0].type == CPP_NAME
1434 && macro->expansion[0].val.node == node);
1436 /* Commit the memory. */
1437 POOL_COMMIT (&pfile->macro_pool, macro->count * sizeof (cpp_token));
1439 /* Redefinition of a macro is allowed if and only if the old and new
1440 definitions are the same. (6.10.3 paragraph 2). */
1441 if (node->type != NT_VOID)
1443 if (CPP_PEDANTIC (pfile)
1444 && !check_macro_redefinition (pfile, node, macro))
1446 cpp_pedwarn_with_line (pfile, pfile->directive_pos.line,
1447 pfile->directive_pos.col,
1448 "\"%s\" redefined", node->name);
1450 if (pfile->done_initializing && node->type == NT_MACRO
1451 && !(node->flags & NODE_BUILTIN))
1452 cpp_pedwarn_with_file_and_line (pfile,
1453 node->value.macro->file,
1454 node->value.macro->line, 1,
1455 "this is the location of the previous definition");
1457 _cpp_free_definition (node);
1460 /* Enter definition in hash table. */
1461 node->type = NT_MACRO;
1462 node->value.macro = macro;
1464 cleanup:
1466 /* Stop the lexer accepting __VA_ARGS__. */
1467 pfile->state.va_args_ok = 0;
1469 /* Clear the fast argument lookup indices. */
1470 for (i = macro->paramc; i-- > 0; )
1471 macro->params[i]->arg_index = 0;
1473 return ok;
1476 /* Warn if a token in `string' matches one of the function macro
1477 arguments in `info'. This function assumes that the macro is a
1478 function macro and not an object macro. */
1479 static void
1480 check_trad_stringification (pfile, macro, string)
1481 cpp_reader *pfile;
1482 const cpp_macro *macro;
1483 const cpp_string *string;
1485 unsigned int i, len;
1486 const U_CHAR *p, *q, *limit = string->text + string->len;
1488 /* Loop over the string. */
1489 for (p = string->text; p < limit; p = q)
1491 /* Find the start of an identifier. */
1492 while (p < limit && !is_idstart (*p))
1493 p++;
1495 /* Find the end of the identifier. */
1496 q = p;
1497 while (q < limit && is_idchar (*q))
1498 q++;
1500 len = q - p;
1502 /* Loop over the function macro arguments to see if the
1503 identifier inside the string matches one of them. */
1504 for (i = 0; i < macro->paramc; i++)
1506 const cpp_hashnode *node = macro->params[i];
1508 if (node->length == len && !memcmp (p, node->name, len))
1510 cpp_warning (pfile,
1511 "macro argument \"%s\" would be stringified with -traditional.",
1512 node->name);
1513 break;
1519 /* Returns the expansion of a macro, in a format suitable to be read
1520 back in again, and therefore also for DWARF 2 debugging info.
1521 Caller is expected to generate the "#define NAME" bit. The
1522 returned text is temporary, and automatically freed later. */
1524 const unsigned char *
1525 cpp_macro_definition (pfile, node)
1526 cpp_reader *pfile;
1527 const cpp_hashnode *node;
1529 unsigned int i, len;
1530 const cpp_macro *macro = node->value.macro;
1531 unsigned char *buffer;
1533 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1535 cpp_ice (pfile, "invalid hash type %d in dump_definition", node->type);
1536 return 0;
1539 /* Calculate length. */
1540 len = 1; /* ' ' */
1541 if (macro->fun_like)
1543 len += 3; /* "()" plus possible final "." of ellipsis. */
1544 for (i = 0; i < macro->paramc; i++)
1545 len += macro->params[i]->length + 2; /* ", " */
1548 for (i = 0; i < macro->count; i++)
1550 cpp_token *token = &macro->expansion[i];
1552 if (token->type == CPP_MACRO_ARG)
1553 len += macro->params[token->val.arg_no - 1]->length;
1554 else
1555 len += cpp_token_len (token); /* Includes room for ' '. */
1556 if (token->flags & STRINGIFY_ARG)
1557 len++; /* "#" */
1558 if (token->flags & PASTE_LEFT)
1559 len += 3; /* " ##" */
1562 if (len > pfile->macro_buffer_len)
1564 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1565 pfile->macro_buffer_len = len;
1567 buffer = pfile->macro_buffer;
1569 /* Parameter names. */
1570 if (macro->fun_like)
1572 *buffer++ = '(';
1573 for (i = 0; i < macro->paramc; i++)
1575 cpp_hashnode *param = macro->params[i];
1577 if (param != pfile->spec_nodes.n__VA_ARGS__)
1579 memcpy (buffer, param->name, param->length);
1580 buffer += param->length;
1583 if (i + 1 < macro->paramc)
1584 *buffer++ = ',', *buffer++ = ' ';
1585 else if (macro->variadic)
1586 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1588 *buffer++ = ')';
1591 /* Expansion tokens. */
1592 if (macro->count)
1594 *buffer++ = ' ';
1595 for (i = 0; i < macro->count; i++)
1597 cpp_token *token = &macro->expansion[i];
1599 if (token->flags & PREV_WHITE)
1600 *buffer++ = ' ';
1601 if (token->flags & STRINGIFY_ARG)
1602 *buffer++ = '#';
1604 if (token->type == CPP_MACRO_ARG)
1606 len = macro->params[token->val.arg_no - 1]->length;
1607 memcpy (buffer, macro->params[token->val.arg_no - 1]->name, len);
1608 buffer += len;
1610 else
1611 buffer = cpp_spell_token (pfile, token, buffer);
1613 if (token->flags & PASTE_LEFT)
1615 *buffer++ = ' ';
1616 *buffer++ = '#';
1617 *buffer++ = '#';
1618 /* Next has PREV_WHITE; see _cpp_create_definition. */
1623 *buffer = '\0';
1624 return pfile->macro_buffer;