* error.c (dump_expr): Handle EMPTY_CLASS_EXPR.
[official-gcc.git] / gcc / cppmacro.c
blob87ad5927754eb7f55fd090773d5b6bf0ccfce8af
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, 2002, 2003 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 "cpplib.h"
29 #include "cpphash.h"
31 typedef struct macro_arg macro_arg;
32 struct macro_arg
34 const cpp_token **first; /* First token in unexpanded argument. */
35 const cpp_token **expanded; /* Macro-expanded argument. */
36 const cpp_token *stringified; /* Stringified argument. */
37 unsigned int count; /* # of tokens in argument. */
38 unsigned int expanded_count; /* # of tokens in expanded argument. */
41 /* Macro expansion. */
43 static int enter_macro_context (cpp_reader *, cpp_hashnode *);
44 static int builtin_macro (cpp_reader *, cpp_hashnode *);
45 static void push_token_context (cpp_reader *, cpp_hashnode *,
46 const cpp_token *, unsigned int);
47 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
48 const cpp_token **, unsigned int);
49 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *);
50 static cpp_context *next_context (cpp_reader *);
51 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
52 static void expand_arg (cpp_reader *, macro_arg *);
53 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
54 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
55 static void paste_all_tokens (cpp_reader *, const cpp_token *);
56 static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
57 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
58 macro_arg *);
59 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *);
60 static bool create_iso_definition (cpp_reader *, cpp_macro *);
62 /* #define directive parsing and handling. */
64 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
65 static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
66 static bool warn_of_redefinition (cpp_reader *, const cpp_hashnode *,
67 const cpp_macro *);
68 static bool parse_params (cpp_reader *, cpp_macro *);
69 static void check_trad_stringification (cpp_reader *, const cpp_macro *,
70 const cpp_string *);
72 /* Emits a warning if NODE is a macro defined in the main file that
73 has not been used. */
74 int
75 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
76 void *v ATTRIBUTE_UNUSED)
78 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
80 cpp_macro *macro = node->value.macro;
82 if (!macro->used
83 && MAIN_FILE_P (lookup_line (&pfile->line_maps, macro->line)))
84 cpp_error_with_line (pfile, DL_WARNING, macro->line, 0,
85 "macro \"%s\" is not used", NODE_NAME (node));
88 return 1;
91 /* Allocates and returns a CPP_STRING token, containing TEXT of length
92 LEN, after null-terminating it. TEXT must be in permanent storage. */
93 static const cpp_token *
94 new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
96 cpp_token *token = _cpp_temp_token (pfile);
98 text[len] = '\0';
99 token->type = CPP_STRING;
100 token->val.str.len = len;
101 token->val.str.text = text;
102 token->flags = 0;
103 return token;
106 static const char * const monthnames[] =
108 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
109 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
112 /* Handle builtin macros like __FILE__, and push the resulting token
113 on the context stack. Also handles _Pragma, for which no new token
114 is created. Returns 1 if it generates a new token context, 0 to
115 return the token to the caller. */
116 const uchar *
117 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
119 const uchar *result = NULL;
120 unsigned int number = 1;
122 switch (node->value.builtin)
124 default:
125 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
126 NODE_NAME (node));
127 break;
129 case BT_FILE:
130 case BT_BASE_FILE:
132 unsigned int len;
133 const char *name;
134 uchar *buf;
135 const struct line_map *map = pfile->map;
137 if (node->value.builtin == BT_BASE_FILE)
138 while (! MAIN_FILE_P (map))
139 map = INCLUDED_FROM (&pfile->line_maps, map);
141 name = map->to_file;
142 len = strlen (name);
143 buf = _cpp_unaligned_alloc (pfile, len * 4 + 3);
144 result = buf;
145 *buf = '"';
146 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
147 *buf++ = '"';
148 *buf = '\0';
150 break;
152 case BT_INCLUDE_LEVEL:
153 /* The line map depth counts the primary source as level 1, but
154 historically __INCLUDE_DEPTH__ has called the primary source
155 level 0. */
156 number = pfile->line_maps.depth - 1;
157 break;
159 case BT_SPECLINE:
160 /* If __LINE__ is embedded in a macro, it must expand to the
161 line of the macro's invocation, not its definition.
162 Otherwise things like assert() will not work properly. */
163 if (CPP_OPTION (pfile, traditional))
164 number = pfile->line;
165 else
166 number = pfile->cur_token[-1].line;
167 number = SOURCE_LINE (pfile->map, number);
168 break;
170 /* __STDC__ has the value 1 under normal circumstances.
171 However, if (a) we are in a system header, (b) the option
172 stdc_0_in_system_headers is true (set by target config), and
173 (c) we are not in strictly conforming mode, then it has the
174 value 0. */
175 case BT_STDC:
177 if (CPP_IN_SYSTEM_HEADER (pfile)
178 && CPP_OPTION (pfile, stdc_0_in_system_headers)
179 && !CPP_OPTION (pfile,std))
180 number = 0;
181 else
182 number = 1;
184 break;
186 case BT_DATE:
187 case BT_TIME:
188 if (pfile->date == NULL)
190 /* Allocate __DATE__ and __TIME__ strings from permanent
191 storage. We only do this once, and don't generate them
192 at init time, because time() and localtime() are very
193 slow on some systems. */
194 time_t tt;
195 struct tm *tb = NULL;
197 /* (time_t) -1 is a legitimate value for "number of seconds
198 since the Epoch", so we have to do a little dance to
199 distinguish that from a genuine error. */
200 errno = 0;
201 tt = time(NULL);
202 if (tt != (time_t)-1 || errno == 0)
203 tb = localtime (&tt);
205 if (tb)
207 pfile->date = _cpp_unaligned_alloc (pfile,
208 sizeof ("\"Oct 11 1347\""));
209 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
210 monthnames[tb->tm_mon], tb->tm_mday,
211 tb->tm_year + 1900);
213 pfile->time = _cpp_unaligned_alloc (pfile,
214 sizeof ("\"12:34:56\""));
215 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
216 tb->tm_hour, tb->tm_min, tb->tm_sec);
218 else
220 cpp_errno (pfile, DL_WARNING,
221 "could not determine date and time");
223 pfile->date = U"\"??? ?? ????\"";
224 pfile->time = U"\"??:??:??\"";
228 if (node->value.builtin == BT_DATE)
229 result = pfile->date;
230 else
231 result = pfile->time;
232 break;
235 if (result == NULL)
237 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
238 result = _cpp_unaligned_alloc (pfile, 21);
239 sprintf ((char *) result, "%u", number);
242 return result;
245 /* Convert builtin macros like __FILE__ to a token and push it on the
246 context stack. Also handles _Pragma, for which no new token is
247 created. Returns 1 if it generates a new token context, 0 to
248 return the token to the caller. */
249 static int
250 builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
252 const uchar *buf;
253 size_t len;
254 char *nbuf;
256 if (node->value.builtin == BT_PRAGMA)
258 /* Don't interpret _Pragma within directives. The standard is
259 not clear on this, but to me this makes most sense. */
260 if (pfile->state.in_directive)
261 return 0;
263 _cpp_do__Pragma (pfile);
264 return 1;
267 buf = _cpp_builtin_macro_text (pfile, node);
268 len = ustrlen (buf);
269 nbuf = alloca (len + 1);
270 memcpy (nbuf, buf, len);
271 nbuf[len]='\n';
273 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true, 1);
274 _cpp_clean_line (pfile);
276 /* Set pfile->cur_token as required by _cpp_lex_direct. */
277 pfile->cur_token = _cpp_temp_token (pfile);
278 push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
279 if (pfile->buffer->cur != pfile->buffer->rlimit)
280 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
281 NODE_NAME (node));
282 _cpp_pop_buffer (pfile);
284 return 1;
287 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
288 backslashes and double quotes. Non-printable characters are
289 converted to octal. DEST must be of sufficient size. Returns
290 a pointer to the end of the string. */
291 uchar *
292 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
294 while (len--)
296 uchar c = *src++;
298 if (c == '\\' || c == '"')
300 *dest++ = '\\';
301 *dest++ = c;
303 else
305 if (ISPRINT (c))
306 *dest++ = c;
307 else
309 sprintf ((char *) dest, "\\%03o", c);
310 dest += 4;
315 return dest;
318 /* Convert a token sequence ARG to a single string token according to
319 the rules of the ISO C #-operator. */
320 static const cpp_token *
321 stringify_arg (cpp_reader *pfile, macro_arg *arg)
323 unsigned char *dest;
324 unsigned int i, escape_it, backslash_count = 0;
325 const cpp_token *source = NULL;
326 size_t len;
328 if (BUFF_ROOM (pfile->u_buff) < 3)
329 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
330 dest = BUFF_FRONT (pfile->u_buff);
331 *dest++ = '"';
333 /* Loop, reading in the argument's tokens. */
334 for (i = 0; i < arg->count; i++)
336 const cpp_token *token = arg->first[i];
338 if (token->type == CPP_PADDING)
340 if (source == NULL)
341 source = token->val.source;
342 continue;
345 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
346 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
348 /* Room for each char being written in octal, initial space and
349 final quote and NUL. */
350 len = cpp_token_len (token);
351 if (escape_it)
352 len *= 4;
353 len += 3;
355 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
357 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
358 _cpp_extend_buff (pfile, &pfile->u_buff, len);
359 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
362 /* Leading white space? */
363 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
365 if (source == NULL)
366 source = token;
367 if (source->flags & PREV_WHITE)
368 *dest++ = ' ';
370 source = NULL;
372 if (escape_it)
374 _cpp_buff *buff = _cpp_get_buff (pfile, len);
375 unsigned char *buf = BUFF_FRONT (buff);
376 len = cpp_spell_token (pfile, token, buf) - buf;
377 dest = cpp_quote_string (dest, buf, len);
378 _cpp_release_buff (pfile, buff);
380 else
381 dest = cpp_spell_token (pfile, token, dest);
383 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
384 backslash_count++;
385 else
386 backslash_count = 0;
389 /* Ignore the final \ of invalid string literals. */
390 if (backslash_count & 1)
392 cpp_error (pfile, DL_WARNING,
393 "invalid string literal, ignoring final '\\'");
394 dest--;
397 /* Commit the memory, including NUL, and return the token. */
398 *dest++ = '"';
399 len = dest - BUFF_FRONT (pfile->u_buff);
400 BUFF_FRONT (pfile->u_buff) = dest + 1;
401 return new_string_token (pfile, dest - len, len);
404 /* Try to paste two tokens. On success, return nonzero. In any
405 case, PLHS is updated to point to the pasted token, which is
406 guaranteed to not have the PASTE_LEFT flag set. */
407 static bool
408 paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
410 unsigned char *buf, *end;
411 const cpp_token *lhs;
412 unsigned int len;
413 bool valid;
415 lhs = *plhs;
416 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
417 buf = (unsigned char *) alloca (len);
418 end = cpp_spell_token (pfile, lhs, buf);
420 /* Avoid comment headers, since they are still processed in stage 3.
421 It is simpler to insert a space here, rather than modifying the
422 lexer to ignore comments in some circumstances. Simply returning
423 false doesn't work, since we want to clear the PASTE_LEFT flag. */
424 if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
425 *end++ = ' ';
426 end = cpp_spell_token (pfile, rhs, end);
427 *end = '\n';
429 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
430 _cpp_clean_line (pfile);
432 /* Set pfile->cur_token as required by _cpp_lex_direct. */
433 pfile->cur_token = _cpp_temp_token (pfile);
434 *plhs = _cpp_lex_direct (pfile);
435 valid = pfile->buffer->cur == pfile->buffer->rlimit;
436 _cpp_pop_buffer (pfile);
438 return valid;
441 /* Handles an arbitrarily long sequence of ## operators, with initial
442 operand LHS. This implementation is left-associative,
443 non-recursive, and finishes a paste before handling succeeding
444 ones. If a paste fails, we back up to the RHS of the failing ##
445 operator before pushing the context containing the result of prior
446 successful pastes, with the effect that the RHS appears in the
447 output stream after the pasted LHS normally. */
448 static void
449 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
451 const cpp_token *rhs;
452 cpp_context *context = pfile->context;
456 /* Take the token directly from the current context. We can do
457 this, because we are in the replacement list of either an
458 object-like macro, or a function-like macro with arguments
459 inserted. In either case, the constraints to #define
460 guarantee we have at least one more token. */
461 if (context->direct_p)
462 rhs = FIRST (context).token++;
463 else
464 rhs = *FIRST (context).ptoken++;
466 if (rhs->type == CPP_PADDING)
467 abort ();
469 if (!paste_tokens (pfile, &lhs, rhs))
471 _cpp_backup_tokens (pfile, 1);
473 /* Mandatory error for all apart from assembler. */
474 if (CPP_OPTION (pfile, lang) != CLK_ASM)
475 cpp_error (pfile, DL_ERROR,
476 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
477 cpp_token_as_text (pfile, lhs),
478 cpp_token_as_text (pfile, rhs));
479 break;
482 while (rhs->flags & PASTE_LEFT);
484 /* Put the resulting token in its own context. */
485 push_token_context (pfile, NULL, lhs, 1);
488 /* Returns TRUE if the number of arguments ARGC supplied in an
489 invocation of the MACRO referenced by NODE is valid. An empty
490 invocation to a macro with no parameters should pass ARGC as zero.
492 Note that MACRO cannot necessarily be deduced from NODE, in case
493 NODE was redefined whilst collecting arguments. */
494 bool
495 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
497 if (argc == macro->paramc)
498 return true;
500 if (argc < macro->paramc)
502 /* As an extension, a rest argument is allowed to not appear in
503 the invocation at all.
504 e.g. #define debug(format, args...) something
505 debug("string");
507 This is exactly the same as if there had been an empty rest
508 argument - debug("string", ). */
510 if (argc + 1 == macro->paramc && macro->variadic)
512 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
513 cpp_error (pfile, DL_PEDWARN,
514 "ISO C99 requires rest arguments to be used");
515 return true;
518 cpp_error (pfile, DL_ERROR,
519 "macro \"%s\" requires %u arguments, but only %u given",
520 NODE_NAME (node), macro->paramc, argc);
522 else
523 cpp_error (pfile, DL_ERROR,
524 "macro \"%s\" passed %u arguments, but takes just %u",
525 NODE_NAME (node), argc, macro->paramc);
527 return false;
530 /* Reads and returns the arguments to a function-like macro
531 invocation. Assumes the opening parenthesis has been processed.
532 If there is an error, emits an appropriate diagnostic and returns
533 NULL. Each argument is terminated by a CPP_EOF token, for the
534 future benefit of expand_arg(). */
535 static _cpp_buff *
536 collect_args (cpp_reader *pfile, const cpp_hashnode *node)
538 _cpp_buff *buff, *base_buff;
539 cpp_macro *macro;
540 macro_arg *args, *arg;
541 const cpp_token *token;
542 unsigned int argc;
544 macro = node->value.macro;
545 if (macro->paramc)
546 argc = macro->paramc;
547 else
548 argc = 1;
549 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
550 + sizeof (macro_arg)));
551 base_buff = buff;
552 args = (macro_arg *) buff->base;
553 memset (args, 0, argc * sizeof (macro_arg));
554 buff->cur = (unsigned char *) &args[argc];
555 arg = args, argc = 0;
557 /* Collect the tokens making up each argument. We don't yet know
558 how many arguments have been supplied, whether too many or too
559 few. Hence the slightly bizarre usage of "argc" and "arg". */
562 unsigned int paren_depth = 0;
563 unsigned int ntokens = 0;
565 argc++;
566 arg->first = (const cpp_token **) buff->cur;
568 for (;;)
570 /* Require space for 2 new tokens (including a CPP_EOF). */
571 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
573 buff = _cpp_append_extend_buff (pfile, buff,
574 1000 * sizeof (cpp_token *));
575 arg->first = (const cpp_token **) buff->cur;
578 token = cpp_get_token (pfile);
580 if (token->type == CPP_PADDING)
582 /* Drop leading padding. */
583 if (ntokens == 0)
584 continue;
586 else if (token->type == CPP_OPEN_PAREN)
587 paren_depth++;
588 else if (token->type == CPP_CLOSE_PAREN)
590 if (paren_depth-- == 0)
591 break;
593 else if (token->type == CPP_COMMA)
595 /* A comma does not terminate an argument within
596 parentheses or as part of a variable argument. */
597 if (paren_depth == 0
598 && ! (macro->variadic && argc == macro->paramc))
599 break;
601 else if (token->type == CPP_EOF
602 || (token->type == CPP_HASH && token->flags & BOL))
603 break;
605 arg->first[ntokens++] = token;
608 /* Drop trailing padding. */
609 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
610 ntokens--;
612 arg->count = ntokens;
613 arg->first[ntokens] = &pfile->eof;
615 /* Terminate the argument. Excess arguments loop back and
616 overwrite the final legitimate argument, before failing. */
617 if (argc <= macro->paramc)
619 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
620 if (argc != macro->paramc)
621 arg++;
624 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
626 if (token->type == CPP_EOF)
628 /* We still need the CPP_EOF to end directives, and to end
629 pre-expansion of a macro argument. Step back is not
630 unconditional, since we don't want to return a CPP_EOF to our
631 callers at the end of an -include-d file. */
632 if (pfile->context->prev || pfile->state.in_directive)
633 _cpp_backup_tokens (pfile, 1);
634 cpp_error (pfile, DL_ERROR,
635 "unterminated argument list invoking macro \"%s\"",
636 NODE_NAME (node));
638 else
640 /* A single empty argument is counted as no argument. */
641 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
642 argc = 0;
643 if (_cpp_arguments_ok (pfile, macro, node, argc))
645 /* GCC has special semantics for , ## b where b is a varargs
646 parameter: we remove the comma if b was omitted entirely.
647 If b was merely an empty argument, the comma is retained.
648 If the macro takes just one (varargs) parameter, then we
649 retain the comma only if we are standards conforming.
651 If FIRST is NULL replace_args () swallows the comma. */
652 if (macro->variadic && (argc < macro->paramc
653 || (argc == 1 && args[0].count == 0
654 && !CPP_OPTION (pfile, std))))
655 args[macro->paramc - 1].first = NULL;
656 return base_buff;
660 /* An error occurred. */
661 _cpp_release_buff (pfile, base_buff);
662 return NULL;
665 /* Search for an opening parenthesis to the macro of NODE, in such a
666 way that, if none is found, we don't lose the information in any
667 intervening padding tokens. If we find the parenthesis, collect
668 the arguments and return the buffer containing them. */
669 static _cpp_buff *
670 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
672 const cpp_token *token, *padding = NULL;
674 for (;;)
676 token = cpp_get_token (pfile);
677 if (token->type != CPP_PADDING)
678 break;
679 if (padding == NULL
680 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
681 padding = token;
684 if (token->type == CPP_OPEN_PAREN)
686 pfile->state.parsing_args = 2;
687 return collect_args (pfile, node);
690 /* CPP_EOF can be the end of macro arguments, or the end of the
691 file. We mustn't back up over the latter. Ugh. */
692 if (token->type != CPP_EOF || token == &pfile->eof)
694 /* Back up. We may have skipped padding, in which case backing
695 up more than one token when expanding macros is in general
696 too difficult. We re-insert it in its own context. */
697 _cpp_backup_tokens (pfile, 1);
698 if (padding)
699 push_token_context (pfile, NULL, padding, 1);
702 return NULL;
705 /* Push the context of a macro with hash entry NODE onto the context
706 stack. If we can successfully expand the macro, we push a context
707 containing its yet-to-be-rescanned replacement list and return one.
708 Otherwise, we don't push a context and return zero. */
709 static int
710 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
712 /* The presence of a macro invalidates a file's controlling macro. */
713 pfile->mi_valid = false;
715 pfile->state.angled_headers = false;
717 /* Handle standard macros. */
718 if (! (node->flags & NODE_BUILTIN))
720 cpp_macro *macro = node->value.macro;
722 if (macro->fun_like)
724 _cpp_buff *buff;
726 pfile->state.prevent_expansion++;
727 pfile->keep_tokens++;
728 pfile->state.parsing_args = 1;
729 buff = funlike_invocation_p (pfile, node);
730 pfile->state.parsing_args = 0;
731 pfile->keep_tokens--;
732 pfile->state.prevent_expansion--;
734 if (buff == NULL)
736 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
737 cpp_error (pfile, DL_WARNING,
738 "function-like macro \"%s\" must be used with arguments in traditional C",
739 NODE_NAME (node));
741 return 0;
744 if (macro->paramc > 0)
745 replace_args (pfile, node, macro, (macro_arg *) buff->base);
746 _cpp_release_buff (pfile, buff);
749 /* Disable the macro within its expansion. */
750 node->flags |= NODE_DISABLED;
752 macro->used = 1;
754 if (macro->paramc == 0)
755 push_token_context (pfile, node, macro->exp.tokens, macro->count);
757 return 1;
760 /* Handle built-in macros and the _Pragma operator. */
761 return builtin_macro (pfile, node);
764 /* Replace the parameters in a function-like macro of NODE with the
765 actual ARGS, and place the result in a newly pushed token context.
766 Expand each argument before replacing, unless it is operated upon
767 by the # or ## operators. */
768 static void
769 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
771 unsigned int i, total;
772 const cpp_token *src, *limit;
773 const cpp_token **dest, **first;
774 macro_arg *arg;
775 _cpp_buff *buff;
777 /* First, fully macro-expand arguments, calculating the number of
778 tokens in the final expansion as we go. The ordering of the if
779 statements below is subtle; we must handle stringification before
780 pasting. */
781 total = macro->count;
782 limit = macro->exp.tokens + macro->count;
784 for (src = macro->exp.tokens; src < limit; src++)
785 if (src->type == CPP_MACRO_ARG)
787 /* Leading and trailing padding tokens. */
788 total += 2;
790 /* We have an argument. If it is not being stringified or
791 pasted it is macro-replaced before insertion. */
792 arg = &args[src->val.arg_no - 1];
794 if (src->flags & STRINGIFY_ARG)
796 if (!arg->stringified)
797 arg->stringified = stringify_arg (pfile, arg);
799 else if ((src->flags & PASTE_LEFT)
800 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
801 total += arg->count - 1;
802 else
804 if (!arg->expanded)
805 expand_arg (pfile, arg);
806 total += arg->expanded_count - 1;
810 /* Now allocate space for the expansion, copy the tokens and replace
811 the arguments. */
812 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
813 first = (const cpp_token **) buff->base;
814 dest = first;
816 for (src = macro->exp.tokens; src < limit; src++)
818 unsigned int count;
819 const cpp_token **from, **paste_flag;
821 if (src->type != CPP_MACRO_ARG)
823 *dest++ = src;
824 continue;
827 paste_flag = 0;
828 arg = &args[src->val.arg_no - 1];
829 if (src->flags & STRINGIFY_ARG)
830 count = 1, from = &arg->stringified;
831 else if (src->flags & PASTE_LEFT)
832 count = arg->count, from = arg->first;
833 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
835 count = arg->count, from = arg->first;
836 if (dest != first)
838 if (dest[-1]->type == CPP_COMMA
839 && macro->variadic
840 && src->val.arg_no == macro->paramc)
842 /* Swallow a pasted comma if from == NULL, otherwise
843 drop the paste flag. */
844 if (from == NULL)
845 dest--;
846 else
847 paste_flag = dest - 1;
849 /* Remove the paste flag if the RHS is a placemarker. */
850 else if (count == 0)
851 paste_flag = dest - 1;
854 else
855 count = arg->expanded_count, from = arg->expanded;
857 /* Padding on the left of an argument (unless RHS of ##). */
858 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
859 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
860 *dest++ = padding_token (pfile, src);
862 if (count)
864 memcpy (dest, from, count * sizeof (cpp_token *));
865 dest += count;
867 /* With a non-empty argument on the LHS of ##, the last
868 token should be flagged PASTE_LEFT. */
869 if (src->flags & PASTE_LEFT)
870 paste_flag = dest - 1;
873 /* Avoid paste on RHS (even case count == 0). */
874 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
875 *dest++ = &pfile->avoid_paste;
877 /* Add a new paste flag, or remove an unwanted one. */
878 if (paste_flag)
880 cpp_token *token = _cpp_temp_token (pfile);
881 token->type = (*paste_flag)->type;
882 token->val.str = (*paste_flag)->val.str;
883 if (src->flags & PASTE_LEFT)
884 token->flags = (*paste_flag)->flags | PASTE_LEFT;
885 else
886 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
887 *paste_flag = token;
891 /* Free the expanded arguments. */
892 for (i = 0; i < macro->paramc; i++)
893 if (args[i].expanded)
894 free (args[i].expanded);
896 push_ptoken_context (pfile, node, buff, first, dest - first);
899 /* Return a special padding token, with padding inherited from SOURCE. */
900 static const cpp_token *
901 padding_token (cpp_reader *pfile, const cpp_token *source)
903 cpp_token *result = _cpp_temp_token (pfile);
905 result->type = CPP_PADDING;
906 result->val.source = source;
907 result->flags = 0;
908 return result;
911 /* Get a new uninitialized context. Create a new one if we cannot
912 re-use an old one. */
913 static cpp_context *
914 next_context (cpp_reader *pfile)
916 cpp_context *result = pfile->context->next;
918 if (result == 0)
920 result = xnew (cpp_context);
921 result->prev = pfile->context;
922 result->next = 0;
923 pfile->context->next = result;
926 pfile->context = result;
927 return result;
930 /* Push a list of pointers to tokens. */
931 static void
932 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
933 const cpp_token **first, unsigned int count)
935 cpp_context *context = next_context (pfile);
937 context->direct_p = false;
938 context->macro = macro;
939 context->buff = buff;
940 FIRST (context).ptoken = first;
941 LAST (context).ptoken = first + count;
944 /* Push a list of tokens. */
945 static void
946 push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
947 const cpp_token *first, unsigned int count)
949 cpp_context *context = next_context (pfile);
951 context->direct_p = true;
952 context->macro = macro;
953 context->buff = NULL;
954 FIRST (context).token = first;
955 LAST (context).token = first + count;
958 /* Push a traditional macro's replacement text. */
959 void
960 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
961 const uchar *start, size_t len)
963 cpp_context *context = next_context (pfile);
965 context->direct_p = true;
966 context->macro = macro;
967 context->buff = NULL;
968 CUR (context) = start;
969 RLIMIT (context) = start + len;
970 macro->flags |= NODE_DISABLED;
973 /* Expand an argument ARG before replacing parameters in a
974 function-like macro. This works by pushing a context with the
975 argument's tokens, and then expanding that into a temporary buffer
976 as if it were a normal part of the token stream. collect_args()
977 has terminated the argument's tokens with a CPP_EOF so that we know
978 when we have fully expanded the argument. */
979 static void
980 expand_arg (cpp_reader *pfile, macro_arg *arg)
982 unsigned int capacity;
983 bool saved_warn_trad;
985 if (arg->count == 0)
986 return;
988 /* Don't warn about funlike macros when pre-expanding. */
989 saved_warn_trad = CPP_WTRADITIONAL (pfile);
990 CPP_WTRADITIONAL (pfile) = 0;
992 /* Loop, reading in the arguments. */
993 capacity = 256;
994 arg->expanded = (const cpp_token **)
995 xmalloc (capacity * sizeof (cpp_token *));
997 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
998 for (;;)
1000 const cpp_token *token;
1002 if (arg->expanded_count + 1 >= capacity)
1004 capacity *= 2;
1005 arg->expanded = (const cpp_token **)
1006 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
1009 token = cpp_get_token (pfile);
1011 if (token->type == CPP_EOF)
1012 break;
1014 arg->expanded[arg->expanded_count++] = token;
1017 _cpp_pop_context (pfile);
1019 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
1022 /* Pop the current context off the stack, re-enabling the macro if the
1023 context represented a macro's replacement list. The context
1024 structure is not freed so that we can re-use it later. */
1025 void
1026 _cpp_pop_context (cpp_reader *pfile)
1028 cpp_context *context = pfile->context;
1030 if (context->macro)
1031 context->macro->flags &= ~NODE_DISABLED;
1033 if (context->buff)
1034 _cpp_release_buff (pfile, context->buff);
1036 pfile->context = context->prev;
1039 /* External routine to get a token. Also used nearly everywhere
1040 internally, except for places where we know we can safely call
1041 _cpp_lex_token directly, such as lexing a directive name.
1043 Macro expansions and directives are transparently handled,
1044 including entering included files. Thus tokens are post-macro
1045 expansion, and after any intervening directives. External callers
1046 see CPP_EOF only at EOF. Internal callers also see it when meeting
1047 a directive inside a macro call, when at the end of a directive and
1048 state.in_directive is still 1, and at the end of argument
1049 pre-expansion. */
1050 const cpp_token *
1051 cpp_get_token (cpp_reader *pfile)
1053 const cpp_token *result;
1055 for (;;)
1057 cpp_hashnode *node;
1058 cpp_context *context = pfile->context;
1060 /* Context->prev == 0 <=> base context. */
1061 if (!context->prev)
1062 result = _cpp_lex_token (pfile);
1063 else if (FIRST (context).token != LAST (context).token)
1065 if (context->direct_p)
1066 result = FIRST (context).token++;
1067 else
1068 result = *FIRST (context).ptoken++;
1070 if (result->flags & PASTE_LEFT)
1072 paste_all_tokens (pfile, result);
1073 if (pfile->state.in_directive)
1074 continue;
1075 return padding_token (pfile, result);
1078 else
1080 _cpp_pop_context (pfile);
1081 if (pfile->state.in_directive)
1082 continue;
1083 return &pfile->avoid_paste;
1086 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1087 continue;
1089 if (result->type != CPP_NAME)
1090 break;
1092 node = result->val.node;
1094 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1095 break;
1097 if (!(node->flags & NODE_DISABLED))
1099 if (!pfile->state.prevent_expansion
1100 && enter_macro_context (pfile, node))
1102 if (pfile->state.in_directive)
1103 continue;
1104 return padding_token (pfile, result);
1107 else
1109 /* Flag this token as always unexpandable. FIXME: move this
1110 to collect_args()?. */
1111 cpp_token *t = _cpp_temp_token (pfile);
1112 t->type = result->type;
1113 t->flags = result->flags | NO_EXPAND;
1114 t->val.str = result->val.str;
1115 result = t;
1118 break;
1121 return result;
1124 /* Returns true if we're expanding an object-like macro that was
1125 defined in a system header. Just checks the macro at the top of
1126 the stack. Used for diagnostic suppression. */
1128 cpp_sys_macro_p (cpp_reader *pfile)
1130 cpp_hashnode *node = pfile->context->macro;
1132 return node && node->value.macro && node->value.macro->syshdr;
1135 /* Read each token in, until end of the current file. Directives are
1136 transparently processed. */
1137 void
1138 cpp_scan_nooutput (cpp_reader *pfile)
1140 /* Request a CPP_EOF token at the end of this file, rather than
1141 transparently continuing with the including file. */
1142 pfile->buffer->return_at_eof = true;
1144 if (CPP_OPTION (pfile, traditional))
1145 while (_cpp_read_logical_line_trad (pfile))
1147 else
1148 while (cpp_get_token (pfile)->type != CPP_EOF)
1152 /* Step back one (or more) tokens. Can only step mack more than 1 if
1153 they are from the lexer, and not from macro expansion. */
1154 void
1155 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
1157 if (pfile->context->prev == NULL)
1159 pfile->lookaheads += count;
1160 while (count--)
1162 pfile->cur_token--;
1163 if (pfile->cur_token == pfile->cur_run->base
1164 /* Possible with -fpreprocessed and no leading #line. */
1165 && pfile->cur_run->prev != NULL)
1167 pfile->cur_run = pfile->cur_run->prev;
1168 pfile->cur_token = pfile->cur_run->limit;
1172 else
1174 if (count != 1)
1175 abort ();
1176 if (pfile->context->direct_p)
1177 FIRST (pfile->context).token--;
1178 else
1179 FIRST (pfile->context).ptoken--;
1183 /* #define directive parsing and handling. */
1185 /* Returns nonzero if a macro redefinition warning is required. */
1186 static bool
1187 warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
1188 const cpp_macro *macro2)
1190 const cpp_macro *macro1;
1191 unsigned int i;
1193 /* Some redefinitions need to be warned about regardless. */
1194 if (node->flags & NODE_WARN)
1195 return true;
1197 /* Redefinition of a macro is allowed if and only if the old and new
1198 definitions are the same. (6.10.3 paragraph 2). */
1199 macro1 = node->value.macro;
1201 /* Don't check count here as it can be different in valid
1202 traditional redefinitions with just whitespace differences. */
1203 if (macro1->paramc != macro2->paramc
1204 || macro1->fun_like != macro2->fun_like
1205 || macro1->variadic != macro2->variadic)
1206 return true;
1208 /* Check parameter spellings. */
1209 for (i = 0; i < macro1->paramc; i++)
1210 if (macro1->params[i] != macro2->params[i])
1211 return true;
1213 /* Check the replacement text or tokens. */
1214 if (CPP_OPTION (pfile, traditional))
1215 return _cpp_expansions_different_trad (macro1, macro2);
1217 if (macro1->count != macro2->count)
1218 return true;
1220 for (i = 0; i < macro1->count; i++)
1221 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1222 return true;
1224 return false;
1227 /* Free the definition of hashnode H. */
1228 void
1229 _cpp_free_definition (cpp_hashnode *h)
1231 /* Macros and assertions no longer have anything to free. */
1232 h->type = NT_VOID;
1233 /* Clear builtin flag in case of redefinition. */
1234 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1237 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1238 zero on success, nonzero if the parameter is a duplicate. */
1239 bool
1240 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
1242 unsigned int len;
1243 /* Constraint 6.10.3.6 - duplicate parameter names. */
1244 if (node->flags & NODE_MACRO_ARG)
1246 cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"",
1247 NODE_NAME (node));
1248 return true;
1251 if (BUFF_ROOM (pfile->a_buff)
1252 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1253 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1255 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1256 node->flags |= NODE_MACRO_ARG;
1257 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1258 if (len > pfile->macro_buffer_len)
1260 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
1261 pfile->macro_buffer_len = len;
1263 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1264 = node->value;
1266 node->value.arg_index = macro->paramc;
1267 return false;
1270 /* Check the syntax of the parameters in a MACRO definition. Returns
1271 false if an error occurs. */
1272 static bool
1273 parse_params (cpp_reader *pfile, cpp_macro *macro)
1275 unsigned int prev_ident = 0;
1277 for (;;)
1279 const cpp_token *token = _cpp_lex_token (pfile);
1281 switch (token->type)
1283 default:
1284 /* Allow/ignore comments in parameter lists if we are
1285 preserving comments in macro expansions. */
1286 if (token->type == CPP_COMMENT
1287 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1288 continue;
1290 cpp_error (pfile, DL_ERROR,
1291 "\"%s\" may not appear in macro parameter list",
1292 cpp_token_as_text (pfile, token));
1293 return false;
1295 case CPP_NAME:
1296 if (prev_ident)
1298 cpp_error (pfile, DL_ERROR,
1299 "macro parameters must be comma-separated");
1300 return false;
1302 prev_ident = 1;
1304 if (_cpp_save_parameter (pfile, macro, token->val.node))
1305 return false;
1306 continue;
1308 case CPP_CLOSE_PAREN:
1309 if (prev_ident || macro->paramc == 0)
1310 return true;
1312 /* Fall through to pick up the error. */
1313 case CPP_COMMA:
1314 if (!prev_ident)
1316 cpp_error (pfile, DL_ERROR, "parameter name missing");
1317 return false;
1319 prev_ident = 0;
1320 continue;
1322 case CPP_ELLIPSIS:
1323 macro->variadic = 1;
1324 if (!prev_ident)
1326 _cpp_save_parameter (pfile, macro,
1327 pfile->spec_nodes.n__VA_ARGS__);
1328 pfile->state.va_args_ok = 1;
1329 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1330 cpp_error (pfile, DL_PEDWARN,
1331 "anonymous variadic macros were introduced in C99");
1333 else if (CPP_OPTION (pfile, pedantic))
1334 cpp_error (pfile, DL_PEDWARN,
1335 "ISO C does not permit named variadic macros");
1337 /* We're at the end, and just expect a closing parenthesis. */
1338 token = _cpp_lex_token (pfile);
1339 if (token->type == CPP_CLOSE_PAREN)
1340 return true;
1341 /* Fall through. */
1343 case CPP_EOF:
1344 cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list");
1345 return false;
1350 /* Allocate room for a token from a macro's replacement list. */
1351 static cpp_token *
1352 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1354 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1355 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1357 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1360 /* Lex a token from the expansion of MACRO, but mark parameters as we
1361 find them and warn of traditional stringification. */
1362 static cpp_token *
1363 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1365 cpp_token *token;
1367 pfile->cur_token = alloc_expansion_token (pfile, macro);
1368 token = _cpp_lex_direct (pfile);
1370 /* Is this a parameter? */
1371 if (token->type == CPP_NAME
1372 && (token->val.node->flags & NODE_MACRO_ARG) != 0)
1374 token->type = CPP_MACRO_ARG;
1375 token->val.arg_no = token->val.node->value.arg_index;
1377 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1378 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1379 check_trad_stringification (pfile, macro, &token->val.str);
1381 return token;
1384 static bool
1385 create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
1387 cpp_token *token;
1388 const cpp_token *ctoken;
1390 /* Get the first token of the expansion (or the '(' of a
1391 function-like macro). */
1392 ctoken = _cpp_lex_token (pfile);
1394 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1396 bool ok = parse_params (pfile, macro);
1397 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1398 if (!ok)
1399 return false;
1401 /* Success. Commit the parameter array. */
1402 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
1403 macro->fun_like = 1;
1405 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1406 cpp_error (pfile, DL_PEDWARN,
1407 "ISO C requires whitespace after the macro name");
1409 if (macro->fun_like)
1410 token = lex_expansion_token (pfile, macro);
1411 else
1413 token = alloc_expansion_token (pfile, macro);
1414 *token = *ctoken;
1417 for (;;)
1419 /* Check the stringifying # constraint 6.10.3.2.1 of
1420 function-like macros when lexing the subsequent token. */
1421 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1423 if (token->type == CPP_MACRO_ARG)
1425 token->flags &= ~PREV_WHITE;
1426 token->flags |= STRINGIFY_ARG;
1427 token->flags |= token[-1].flags & PREV_WHITE;
1428 token[-1] = token[0];
1429 macro->count--;
1431 /* Let assembler get away with murder. */
1432 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1434 cpp_error (pfile, DL_ERROR,
1435 "'#' is not followed by a macro parameter");
1436 return false;
1440 if (token->type == CPP_EOF)
1441 break;
1443 /* Paste operator constraint 6.10.3.3.1. */
1444 if (token->type == CPP_PASTE)
1446 /* Token-paste ##, can appear in both object-like and
1447 function-like macros, but not at the ends. */
1448 if (--macro->count > 0)
1449 token = lex_expansion_token (pfile, macro);
1451 if (macro->count == 0 || token->type == CPP_EOF)
1453 cpp_error (pfile, DL_ERROR,
1454 "'##' cannot appear at either end of a macro expansion");
1455 return false;
1458 token[-1].flags |= PASTE_LEFT;
1461 token = lex_expansion_token (pfile, macro);
1464 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1466 /* Don't count the CPP_EOF. */
1467 macro->count--;
1469 /* Clear whitespace on first token for warn_of_redefinition(). */
1470 if (macro->count)
1471 macro->exp.tokens[0].flags &= ~PREV_WHITE;
1473 /* Commit the memory. */
1474 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
1476 return true;
1479 /* Parse a macro and save its expansion. Returns nonzero on success. */
1480 bool
1481 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
1483 cpp_macro *macro;
1484 unsigned int i;
1485 bool ok;
1487 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1488 macro->line = pfile->directive_line;
1489 macro->params = 0;
1490 macro->paramc = 0;
1491 macro->variadic = 0;
1492 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
1493 macro->count = 0;
1494 macro->fun_like = 0;
1495 /* To suppress some diagnostics. */
1496 macro->syshdr = pfile->map->sysp != 0;
1498 if (CPP_OPTION (pfile, traditional))
1499 ok = _cpp_create_trad_definition (pfile, macro);
1500 else
1502 cpp_token *saved_cur_token = pfile->cur_token;
1504 ok = create_iso_definition (pfile, macro);
1506 /* Restore lexer position because of games lex_expansion_token()
1507 plays lexing the macro. We set the type for SEEN_EOL() in
1508 cpplib.c.
1510 Longer term we should lex the whole line before coming here,
1511 and just copy the expansion. */
1512 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1513 pfile->cur_token = saved_cur_token;
1515 /* Stop the lexer accepting __VA_ARGS__. */
1516 pfile->state.va_args_ok = 0;
1519 /* Clear the fast argument lookup indices. */
1520 for (i = macro->paramc; i-- > 0; )
1522 struct cpp_hashnode *node = macro->params[i];
1523 node->flags &= ~ NODE_MACRO_ARG;
1524 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1527 if (!ok)
1528 return ok;
1530 if (node->type == NT_MACRO)
1532 if (CPP_OPTION (pfile, warn_unused_macros))
1533 _cpp_warn_if_unused_macro (pfile, node, NULL);
1535 if (warn_of_redefinition (pfile, node, macro))
1537 cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0,
1538 "\"%s\" redefined", NODE_NAME (node));
1540 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1541 cpp_error_with_line (pfile, DL_PEDWARN,
1542 node->value.macro->line, 0,
1543 "this is the location of the previous definition");
1547 if (node->type != NT_VOID)
1548 _cpp_free_definition (node);
1550 /* Enter definition in hash table. */
1551 node->type = NT_MACRO;
1552 node->value.macro = macro;
1553 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1554 node->flags |= NODE_WARN;
1556 return ok;
1559 /* Warn if a token in STRING matches one of a function-like MACRO's
1560 parameters. */
1561 static void
1562 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1563 const cpp_string *string)
1565 unsigned int i, len;
1566 const uchar *p, *q, *limit;
1568 /* Loop over the string. */
1569 limit = string->text + string->len - 1;
1570 for (p = string->text + 1; p < limit; p = q)
1572 /* Find the start of an identifier. */
1573 while (p < limit && !is_idstart (*p))
1574 p++;
1576 /* Find the end of the identifier. */
1577 q = p;
1578 while (q < limit && is_idchar (*q))
1579 q++;
1581 len = q - p;
1583 /* Loop over the function macro arguments to see if the
1584 identifier inside the string matches one of them. */
1585 for (i = 0; i < macro->paramc; i++)
1587 const cpp_hashnode *node = macro->params[i];
1589 if (NODE_LEN (node) == len
1590 && !memcmp (p, NODE_NAME (node), len))
1592 cpp_error (pfile, DL_WARNING,
1593 "macro argument \"%s\" would be stringified in traditional C",
1594 NODE_NAME (node));
1595 break;
1601 /* Returns the name, arguments and expansion of a macro, in a format
1602 suitable to be read back in again, and therefore also for DWARF 2
1603 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1604 Caller is expected to generate the "#define" bit if needed. The
1605 returned text is temporary, and automatically freed later. */
1606 const unsigned char *
1607 cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
1609 unsigned int i, len;
1610 const cpp_macro *macro = node->value.macro;
1611 unsigned char *buffer;
1613 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1615 cpp_error (pfile, DL_ICE,
1616 "invalid hash type %d in cpp_macro_definition", node->type);
1617 return 0;
1620 /* Calculate length. */
1621 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
1622 if (macro->fun_like)
1624 len += 4; /* "()" plus possible final ".." of named
1625 varargs (we have + 1 below). */
1626 for (i = 0; i < macro->paramc; i++)
1627 len += NODE_LEN (macro->params[i]) + 1; /* "," */
1630 if (CPP_OPTION (pfile, traditional))
1631 len += _cpp_replacement_text_len (macro);
1632 else
1634 for (i = 0; i < macro->count; i++)
1636 cpp_token *token = &macro->exp.tokens[i];
1638 if (token->type == CPP_MACRO_ARG)
1639 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1640 else
1641 len += cpp_token_len (token) + 1; /* Includes room for ' '. */
1642 if (token->flags & STRINGIFY_ARG)
1643 len++; /* "#" */
1644 if (token->flags & PASTE_LEFT)
1645 len += 3; /* " ##" */
1649 if (len > pfile->macro_buffer_len)
1651 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
1652 pfile->macro_buffer_len = len;
1655 /* Fill in the buffer. Start with the macro name. */
1656 buffer = pfile->macro_buffer;
1657 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1658 buffer += NODE_LEN (node);
1660 /* Parameter names. */
1661 if (macro->fun_like)
1663 *buffer++ = '(';
1664 for (i = 0; i < macro->paramc; i++)
1666 cpp_hashnode *param = macro->params[i];
1668 if (param != pfile->spec_nodes.n__VA_ARGS__)
1670 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1671 buffer += NODE_LEN (param);
1674 if (i + 1 < macro->paramc)
1675 /* Don't emit a space after the comma here; we're trying
1676 to emit a Dwarf-friendly definition, and the Dwarf spec
1677 forbids spaces in the argument list. */
1678 *buffer++ = ',';
1679 else if (macro->variadic)
1680 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1682 *buffer++ = ')';
1685 /* The Dwarf spec requires a space after the macro name, even if the
1686 definition is the empty string. */
1687 *buffer++ = ' ';
1689 if (CPP_OPTION (pfile, traditional))
1690 buffer = _cpp_copy_replacement_text (macro, buffer);
1691 else if (macro->count)
1692 /* Expansion tokens. */
1694 for (i = 0; i < macro->count; i++)
1696 cpp_token *token = &macro->exp.tokens[i];
1698 if (token->flags & PREV_WHITE)
1699 *buffer++ = ' ';
1700 if (token->flags & STRINGIFY_ARG)
1701 *buffer++ = '#';
1703 if (token->type == CPP_MACRO_ARG)
1705 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1706 memcpy (buffer,
1707 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1708 buffer += len;
1710 else
1711 buffer = cpp_spell_token (pfile, token, buffer);
1713 if (token->flags & PASTE_LEFT)
1715 *buffer++ = ' ';
1716 *buffer++ = '#';
1717 *buffer++ = '#';
1718 /* Next has PREV_WHITE; see _cpp_create_definition. */
1723 *buffer = '\0';
1724 return pfile->macro_buffer;