update email addresses
[official-gcc.git] / gcc / cppmacro.c
blobf77f6b36837430acfb303e60f3d49d09560262c1
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 "coretypes.h"
29 #include "tm.h"
30 #include "cpplib.h"
31 #include "cpphash.h"
33 typedef struct macro_arg macro_arg;
34 struct macro_arg
36 const cpp_token **first; /* First token in unexpanded argument. */
37 const cpp_token **expanded; /* Macro-expanded argument. */
38 const cpp_token *stringified; /* Stringified argument. */
39 unsigned int count; /* # of tokens in argument. */
40 unsigned int expanded_count; /* # of tokens in expanded argument. */
43 /* Macro expansion. */
45 static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
46 static int builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
47 static void push_token_context
48 PARAMS ((cpp_reader *, cpp_hashnode *, const cpp_token *, unsigned int));
49 static void push_ptoken_context
50 PARAMS ((cpp_reader *, cpp_hashnode *, _cpp_buff *,
51 const cpp_token **, unsigned int));
52 static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
53 static cpp_context *next_context PARAMS ((cpp_reader *));
54 static const cpp_token *padding_token
55 PARAMS ((cpp_reader *, const cpp_token *));
56 static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
57 static const cpp_token *new_string_token PARAMS ((cpp_reader *, uchar *,
58 unsigned int));
59 static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
60 static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
61 static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
62 const cpp_token *));
63 static void replace_args PARAMS ((cpp_reader *, cpp_hashnode *, cpp_macro *,
64 macro_arg *));
65 static _cpp_buff *funlike_invocation_p PARAMS ((cpp_reader *, cpp_hashnode *));
66 static bool create_iso_definition PARAMS ((cpp_reader *, cpp_macro *));
68 /* #define directive parsing and handling. */
70 static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
71 static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
72 static bool warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
73 const cpp_macro *));
74 static bool parse_params PARAMS ((cpp_reader *, cpp_macro *));
75 static void check_trad_stringification PARAMS ((cpp_reader *,
76 const cpp_macro *,
77 const cpp_string *));
79 /* Emits a warning if NODE is a macro defined in the main file that
80 has not been used. */
81 int
82 _cpp_warn_if_unused_macro (pfile, node, v)
83 cpp_reader *pfile;
84 cpp_hashnode *node;
85 void *v ATTRIBUTE_UNUSED;
87 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
89 cpp_macro *macro = node->value.macro;
91 if (!macro->used
92 && MAIN_FILE_P (lookup_line (&pfile->line_maps, macro->line)))
93 cpp_error_with_line (pfile, DL_WARNING, macro->line, 0,
94 "macro \"%s\" is not used", NODE_NAME (node));
97 return 1;
100 /* Allocates and returns a CPP_STRING token, containing TEXT of length
101 LEN, after null-terminating it. TEXT must be in permanent storage. */
102 static const cpp_token *
103 new_string_token (pfile, text, len)
104 cpp_reader *pfile;
105 unsigned char *text;
106 unsigned int len;
108 cpp_token *token = _cpp_temp_token (pfile);
110 text[len] = '\0';
111 token->type = CPP_STRING;
112 token->val.str.len = len;
113 token->val.str.text = text;
114 token->flags = 0;
115 return token;
118 static const char * const monthnames[] =
120 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
121 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
124 /* Handle builtin macros like __FILE__, and push the resulting token
125 on the context stack. Also handles _Pragma, for which no new token
126 is created. Returns 1 if it generates a new token context, 0 to
127 return the token to the caller. */
128 const uchar *
129 _cpp_builtin_macro_text (pfile, node)
130 cpp_reader *pfile;
131 cpp_hashnode *node;
133 const uchar *result = NULL;
134 unsigned int number = 1;
136 switch (node->value.builtin)
138 default:
139 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
140 NODE_NAME (node));
141 break;
143 case BT_FILE:
144 case BT_BASE_FILE:
146 unsigned int len;
147 const char *name;
148 uchar *buf;
149 const struct line_map *map = pfile->map;
151 if (node->value.builtin == BT_BASE_FILE)
152 while (! MAIN_FILE_P (map))
153 map = INCLUDED_FROM (&pfile->line_maps, map);
155 name = map->to_file;
156 len = strlen (name);
157 buf = _cpp_unaligned_alloc (pfile, len * 4 + 3);
158 result = buf;
159 *buf = '"';
160 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
161 *buf++ = '"';
162 *buf = '\0';
164 break;
166 case BT_INCLUDE_LEVEL:
167 /* The line map depth counts the primary source as level 1, but
168 historically __INCLUDE_DEPTH__ has called the primary source
169 level 0. */
170 number = pfile->line_maps.depth - 1;
171 break;
173 case BT_SPECLINE:
174 /* If __LINE__ is embedded in a macro, it must expand to the
175 line of the macro's invocation, not its definition.
176 Otherwise things like assert() will not work properly. */
177 if (CPP_OPTION (pfile, traditional))
178 number = pfile->line;
179 else
180 number = pfile->cur_token[-1].line;
181 number = SOURCE_LINE (pfile->map, number);
182 break;
184 /* __STDC__ has the value 1 under normal circumstances.
185 However, if (a) we are in a system header, (b) the option
186 stdc_0_in_system_headers is true (set by target config), and
187 (c) we are not in strictly conforming mode, then it has the
188 value 0. */
189 case BT_STDC:
191 if (CPP_IN_SYSTEM_HEADER (pfile)
192 && CPP_OPTION (pfile, stdc_0_in_system_headers)
193 && !CPP_OPTION (pfile,std))
194 number = 0;
195 else
196 number = 1;
198 break;
200 case BT_DATE:
201 case BT_TIME:
202 if (pfile->date == NULL)
204 /* Allocate __DATE__ and __TIME__ strings from permanent
205 storage. We only do this once, and don't generate them
206 at init time, because time() and localtime() are very
207 slow on some systems. */
208 time_t tt;
209 struct tm *tb = NULL;
211 /* (time_t) -1 is a legitimate value for "number of seconds
212 since the Epoch", so we have to do a little dance to
213 distinguish that from a genuine error. */
214 errno = 0;
215 tt = time(NULL);
216 if (tt != (time_t)-1 || errno == 0)
217 tb = localtime (&tt);
219 if (tb)
221 pfile->date = _cpp_unaligned_alloc (pfile,
222 sizeof ("\"Oct 11 1347\""));
223 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
224 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
226 pfile->time = _cpp_unaligned_alloc (pfile,
227 sizeof ("\"12:34:56\""));
228 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
229 tb->tm_hour, tb->tm_min, tb->tm_sec);
231 else
233 cpp_errno (pfile, DL_WARNING,
234 "could not determine date and time");
236 pfile->date = U"\"??? ?? ????\"";
237 pfile->time = U"\"??:??:??\"";
241 if (node->value.builtin == BT_DATE)
242 result = pfile->date;
243 else
244 result = pfile->time;
245 break;
248 if (result == NULL)
250 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
251 result = _cpp_unaligned_alloc (pfile, 21);
252 sprintf ((char *) result, "%u", number);
255 return result;
258 /* Convert builtin macros like __FILE__ to a token and push it on the
259 context stack. Also handles _Pragma, for which no new token is
260 created. Returns 1 if it generates a new token context, 0 to
261 return the token to the caller. */
262 static int
263 builtin_macro (pfile, node)
264 cpp_reader *pfile;
265 cpp_hashnode *node;
267 const uchar *buf;
268 size_t len;
269 char *nbuf;
271 if (node->value.builtin == BT_PRAGMA)
273 /* Don't interpret _Pragma within directives. The standard is
274 not clear on this, but to me this makes most sense. */
275 if (pfile->state.in_directive)
276 return 0;
278 _cpp_do__Pragma (pfile);
279 return 1;
282 buf = _cpp_builtin_macro_text (pfile, node);
283 len = ustrlen (buf);
284 nbuf = alloca (len + 1);
285 memcpy (nbuf, buf, len);
286 nbuf[len]='\n';
288 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true, 1);
289 _cpp_clean_line (pfile);
291 /* Set pfile->cur_token as required by _cpp_lex_direct. */
292 pfile->cur_token = _cpp_temp_token (pfile);
293 push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
294 if (pfile->buffer->cur != pfile->buffer->rlimit)
295 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
296 NODE_NAME (node));
297 _cpp_pop_buffer (pfile);
299 return 1;
302 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
303 backslashes and double quotes. Non-printable characters are
304 converted to octal. DEST must be of sufficient size. Returns
305 a pointer to the end of the string. */
306 uchar *
307 cpp_quote_string (dest, src, len)
308 uchar *dest;
309 const uchar *src;
310 unsigned int len;
312 while (len--)
314 uchar c = *src++;
316 if (c == '\\' || c == '"')
318 *dest++ = '\\';
319 *dest++ = c;
321 else
323 if (ISPRINT (c))
324 *dest++ = c;
325 else
327 sprintf ((char *) dest, "\\%03o", c);
328 dest += 4;
333 return dest;
336 /* Convert a token sequence ARG to a single string token according to
337 the rules of the ISO C #-operator. */
338 static const cpp_token *
339 stringify_arg (pfile, arg)
340 cpp_reader *pfile;
341 macro_arg *arg;
343 unsigned char *dest;
344 unsigned int i, escape_it, backslash_count = 0;
345 const cpp_token *source = NULL;
346 size_t len;
348 if (BUFF_ROOM (pfile->u_buff) < 3)
349 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
350 dest = BUFF_FRONT (pfile->u_buff);
351 *dest++ = '"';
353 /* Loop, reading in the argument's tokens. */
354 for (i = 0; i < arg->count; i++)
356 const cpp_token *token = arg->first[i];
358 if (token->type == CPP_PADDING)
360 if (source == NULL)
361 source = token->val.source;
362 continue;
365 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
366 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
368 /* Room for each char being written in octal, initial space and
369 final quote and NUL. */
370 len = cpp_token_len (token);
371 if (escape_it)
372 len *= 4;
373 len += 3;
375 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
377 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
378 _cpp_extend_buff (pfile, &pfile->u_buff, len);
379 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
382 /* Leading white space? */
383 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
385 if (source == NULL)
386 source = token;
387 if (source->flags & PREV_WHITE)
388 *dest++ = ' ';
390 source = NULL;
392 if (escape_it)
394 _cpp_buff *buff = _cpp_get_buff (pfile, len);
395 unsigned char *buf = BUFF_FRONT (buff);
396 len = cpp_spell_token (pfile, token, buf) - buf;
397 dest = cpp_quote_string (dest, buf, len);
398 _cpp_release_buff (pfile, buff);
400 else
401 dest = cpp_spell_token (pfile, token, dest);
403 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
404 backslash_count++;
405 else
406 backslash_count = 0;
409 /* Ignore the final \ of invalid string literals. */
410 if (backslash_count & 1)
412 cpp_error (pfile, DL_WARNING,
413 "invalid string literal, ignoring final '\\'");
414 dest--;
417 /* Commit the memory, including NUL, and return the token. */
418 *dest++ = '"';
419 len = dest - BUFF_FRONT (pfile->u_buff);
420 BUFF_FRONT (pfile->u_buff) = dest + 1;
421 return new_string_token (pfile, dest - len, len);
424 /* Try to paste two tokens. On success, return nonzero. In any
425 case, PLHS is updated to point to the pasted token, which is
426 guaranteed to not have the PASTE_LEFT flag set. */
427 static bool
428 paste_tokens (pfile, plhs, rhs)
429 cpp_reader *pfile;
430 const cpp_token **plhs, *rhs;
432 unsigned char *buf, *end;
433 const cpp_token *lhs;
434 unsigned int len;
435 bool valid;
437 lhs = *plhs;
438 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
439 buf = (unsigned char *) alloca (len);
440 end = cpp_spell_token (pfile, lhs, buf);
442 /* Avoid comment headers, since they are still processed in stage 3.
443 It is simpler to insert a space here, rather than modifying the
444 lexer to ignore comments in some circumstances. Simply returning
445 false doesn't work, since we want to clear the PASTE_LEFT flag. */
446 if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
447 *end++ = ' ';
448 end = cpp_spell_token (pfile, rhs, end);
449 *end = '\n';
451 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
452 _cpp_clean_line (pfile);
454 /* Set pfile->cur_token as required by _cpp_lex_direct. */
455 pfile->cur_token = _cpp_temp_token (pfile);
456 *plhs = _cpp_lex_direct (pfile);
457 valid = pfile->buffer->cur == pfile->buffer->rlimit;
458 _cpp_pop_buffer (pfile);
460 return valid;
463 /* Handles an arbitrarily long sequence of ## operators, with initial
464 operand LHS. This implementation is left-associative,
465 non-recursive, and finishes a paste before handling succeeding
466 ones. If a paste fails, we back up to the RHS of the failing ##
467 operator before pushing the context containing the result of prior
468 successful pastes, with the effect that the RHS appears in the
469 output stream after the pasted LHS normally. */
470 static void
471 paste_all_tokens (pfile, lhs)
472 cpp_reader *pfile;
473 const cpp_token *lhs;
475 const cpp_token *rhs;
476 cpp_context *context = pfile->context;
480 /* Take the token directly from the current context. We can do
481 this, because we are in the replacement list of either an
482 object-like macro, or a function-like macro with arguments
483 inserted. In either case, the constraints to #define
484 guarantee we have at least one more token. */
485 if (context->direct_p)
486 rhs = FIRST (context).token++;
487 else
488 rhs = *FIRST (context).ptoken++;
490 if (rhs->type == CPP_PADDING)
491 abort ();
493 if (!paste_tokens (pfile, &lhs, rhs))
495 _cpp_backup_tokens (pfile, 1);
497 /* Mandatory error for all apart from assembler. */
498 if (CPP_OPTION (pfile, lang) != CLK_ASM)
499 cpp_error (pfile, DL_ERROR,
500 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
501 cpp_token_as_text (pfile, lhs),
502 cpp_token_as_text (pfile, rhs));
503 break;
506 while (rhs->flags & PASTE_LEFT);
508 /* Put the resulting token in its own context. */
509 push_token_context (pfile, NULL, lhs, 1);
512 /* Returns TRUE if the number of arguments ARGC supplied in an
513 invocation of the MACRO referenced by NODE is valid. An empty
514 invocation to a macro with no parameters should pass ARGC as zero.
516 Note that MACRO cannot necessarily be deduced from NODE, in case
517 NODE was redefined whilst collecting arguments. */
518 bool
519 _cpp_arguments_ok (pfile, macro, node, argc)
520 cpp_reader *pfile;
521 cpp_macro *macro;
522 const cpp_hashnode *node;
523 unsigned int argc;
525 if (argc == macro->paramc)
526 return true;
528 if (argc < macro->paramc)
530 /* As an extension, a rest argument is allowed to not appear in
531 the invocation at all.
532 e.g. #define debug(format, args...) something
533 debug("string");
535 This is exactly the same as if there had been an empty rest
536 argument - debug("string", ). */
538 if (argc + 1 == macro->paramc && macro->variadic)
540 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
541 cpp_error (pfile, DL_PEDWARN,
542 "ISO C99 requires rest arguments to be used");
543 return true;
546 cpp_error (pfile, DL_ERROR,
547 "macro \"%s\" requires %u arguments, but only %u given",
548 NODE_NAME (node), macro->paramc, argc);
550 else
551 cpp_error (pfile, DL_ERROR,
552 "macro \"%s\" passed %u arguments, but takes just %u",
553 NODE_NAME (node), argc, macro->paramc);
555 return false;
558 /* Reads and returns the arguments to a function-like macro
559 invocation. Assumes the opening parenthesis has been processed.
560 If there is an error, emits an appropriate diagnostic and returns
561 NULL. Each argument is terminated by a CPP_EOF token, for the
562 future benefit of expand_arg(). */
563 static _cpp_buff *
564 collect_args (pfile, node)
565 cpp_reader *pfile;
566 const cpp_hashnode *node;
568 _cpp_buff *buff, *base_buff;
569 cpp_macro *macro;
570 macro_arg *args, *arg;
571 const cpp_token *token;
572 unsigned int argc;
574 macro = node->value.macro;
575 if (macro->paramc)
576 argc = macro->paramc;
577 else
578 argc = 1;
579 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
580 + sizeof (macro_arg)));
581 base_buff = buff;
582 args = (macro_arg *) buff->base;
583 memset (args, 0, argc * sizeof (macro_arg));
584 buff->cur = (unsigned char *) &args[argc];
585 arg = args, argc = 0;
587 /* Collect the tokens making up each argument. We don't yet know
588 how many arguments have been supplied, whether too many or too
589 few. Hence the slightly bizarre usage of "argc" and "arg". */
592 unsigned int paren_depth = 0;
593 unsigned int ntokens = 0;
595 argc++;
596 arg->first = (const cpp_token **) buff->cur;
598 for (;;)
600 /* Require space for 2 new tokens (including a CPP_EOF). */
601 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
603 buff = _cpp_append_extend_buff (pfile, buff,
604 1000 * sizeof (cpp_token *));
605 arg->first = (const cpp_token **) buff->cur;
608 token = cpp_get_token (pfile);
610 if (token->type == CPP_PADDING)
612 /* Drop leading padding. */
613 if (ntokens == 0)
614 continue;
616 else if (token->type == CPP_OPEN_PAREN)
617 paren_depth++;
618 else if (token->type == CPP_CLOSE_PAREN)
620 if (paren_depth-- == 0)
621 break;
623 else if (token->type == CPP_COMMA)
625 /* A comma does not terminate an argument within
626 parentheses or as part of a variable argument. */
627 if (paren_depth == 0
628 && ! (macro->variadic && argc == macro->paramc))
629 break;
631 else if (token->type == CPP_EOF
632 || (token->type == CPP_HASH && token->flags & BOL))
633 break;
635 arg->first[ntokens++] = token;
638 /* Drop trailing padding. */
639 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
640 ntokens--;
642 arg->count = ntokens;
643 arg->first[ntokens] = &pfile->eof;
645 /* Terminate the argument. Excess arguments loop back and
646 overwrite the final legitimate argument, before failing. */
647 if (argc <= macro->paramc)
649 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
650 if (argc != macro->paramc)
651 arg++;
654 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
656 if (token->type == CPP_EOF)
658 /* We still need the CPP_EOF to end directives, and to end
659 pre-expansion of a macro argument. Step back is not
660 unconditional, since we don't want to return a CPP_EOF to our
661 callers at the end of an -include-d file. */
662 if (pfile->context->prev || pfile->state.in_directive)
663 _cpp_backup_tokens (pfile, 1);
664 cpp_error (pfile, DL_ERROR,
665 "unterminated argument list invoking macro \"%s\"",
666 NODE_NAME (node));
668 else
670 /* A single empty argument is counted as no argument. */
671 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
672 argc = 0;
673 if (_cpp_arguments_ok (pfile, macro, node, argc))
675 /* GCC has special semantics for , ## b where b is a varargs
676 parameter: we remove the comma if b was omitted entirely.
677 If b was merely an empty argument, the comma is retained.
678 If the macro takes just one (varargs) parameter, then we
679 retain the comma only if we are standards conforming.
681 If FIRST is NULL replace_args () swallows the comma. */
682 if (macro->variadic && (argc < macro->paramc
683 || (argc == 1 && args[0].count == 0
684 && !CPP_OPTION (pfile, std))))
685 args[macro->paramc - 1].first = NULL;
686 return base_buff;
690 /* An error occurred. */
691 _cpp_release_buff (pfile, base_buff);
692 return NULL;
695 /* Search for an opening parenthesis to the macro of NODE, in such a
696 way that, if none is found, we don't lose the information in any
697 intervening padding tokens. If we find the parenthesis, collect
698 the arguments and return the buffer containing them. */
699 static _cpp_buff *
700 funlike_invocation_p (pfile, node)
701 cpp_reader *pfile;
702 cpp_hashnode *node;
704 const cpp_token *token, *padding = NULL;
706 for (;;)
708 token = cpp_get_token (pfile);
709 if (token->type != CPP_PADDING)
710 break;
711 if (padding == NULL
712 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
713 padding = token;
716 if (token->type == CPP_OPEN_PAREN)
718 pfile->state.parsing_args = 2;
719 return collect_args (pfile, node);
722 /* CPP_EOF can be the end of macro arguments, or the end of the
723 file. We mustn't back up over the latter. Ugh. */
724 if (token->type != CPP_EOF || token == &pfile->eof)
726 /* Back up. We may have skipped padding, in which case backing
727 up more than one token when expanding macros is in general
728 too difficult. We re-insert it in its own context. */
729 _cpp_backup_tokens (pfile, 1);
730 if (padding)
731 push_token_context (pfile, NULL, padding, 1);
734 return NULL;
737 /* Push the context of a macro with hash entry NODE onto the context
738 stack. If we can successfully expand the macro, we push a context
739 containing its yet-to-be-rescanned replacement list and return one.
740 Otherwise, we don't push a context and return zero. */
741 static int
742 enter_macro_context (pfile, node)
743 cpp_reader *pfile;
744 cpp_hashnode *node;
746 /* The presence of a macro invalidates a file's controlling macro. */
747 pfile->mi_valid = false;
749 pfile->state.angled_headers = false;
751 /* Handle standard macros. */
752 if (! (node->flags & NODE_BUILTIN))
754 cpp_macro *macro = node->value.macro;
756 if (macro->fun_like)
758 _cpp_buff *buff;
760 pfile->state.prevent_expansion++;
761 pfile->keep_tokens++;
762 pfile->state.parsing_args = 1;
763 buff = funlike_invocation_p (pfile, node);
764 pfile->state.parsing_args = 0;
765 pfile->keep_tokens--;
766 pfile->state.prevent_expansion--;
768 if (buff == NULL)
770 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
771 cpp_error (pfile, DL_WARNING,
772 "function-like macro \"%s\" must be used with arguments in traditional C",
773 NODE_NAME (node));
775 return 0;
778 if (macro->paramc > 0)
779 replace_args (pfile, node, macro, (macro_arg *) buff->base);
780 _cpp_release_buff (pfile, buff);
783 /* Disable the macro within its expansion. */
784 node->flags |= NODE_DISABLED;
786 macro->used = 1;
788 if (macro->paramc == 0)
789 push_token_context (pfile, node, macro->exp.tokens, macro->count);
791 return 1;
794 /* Handle built-in macros and the _Pragma operator. */
795 return builtin_macro (pfile, node);
798 /* Replace the parameters in a function-like macro of NODE with the
799 actual ARGS, and place the result in a newly pushed token context.
800 Expand each argument before replacing, unless it is operated upon
801 by the # or ## operators. */
802 static void
803 replace_args (pfile, node, macro, args)
804 cpp_reader *pfile;
805 cpp_hashnode *node;
806 cpp_macro *macro;
807 macro_arg *args;
809 unsigned int i, total;
810 const cpp_token *src, *limit;
811 const cpp_token **dest, **first;
812 macro_arg *arg;
813 _cpp_buff *buff;
815 /* First, fully macro-expand arguments, calculating the number of
816 tokens in the final expansion as we go. The ordering of the if
817 statements below is subtle; we must handle stringification before
818 pasting. */
819 total = macro->count;
820 limit = macro->exp.tokens + macro->count;
822 for (src = macro->exp.tokens; src < limit; src++)
823 if (src->type == CPP_MACRO_ARG)
825 /* Leading and trailing padding tokens. */
826 total += 2;
828 /* We have an argument. If it is not being stringified or
829 pasted it is macro-replaced before insertion. */
830 arg = &args[src->val.arg_no - 1];
832 if (src->flags & STRINGIFY_ARG)
834 if (!arg->stringified)
835 arg->stringified = stringify_arg (pfile, arg);
837 else if ((src->flags & PASTE_LEFT)
838 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
839 total += arg->count - 1;
840 else
842 if (!arg->expanded)
843 expand_arg (pfile, arg);
844 total += arg->expanded_count - 1;
848 /* Now allocate space for the expansion, copy the tokens and replace
849 the arguments. */
850 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
851 first = (const cpp_token **) buff->base;
852 dest = first;
854 for (src = macro->exp.tokens; src < limit; src++)
856 unsigned int count;
857 const cpp_token **from, **paste_flag;
859 if (src->type != CPP_MACRO_ARG)
861 *dest++ = src;
862 continue;
865 paste_flag = 0;
866 arg = &args[src->val.arg_no - 1];
867 if (src->flags & STRINGIFY_ARG)
868 count = 1, from = &arg->stringified;
869 else if (src->flags & PASTE_LEFT)
870 count = arg->count, from = arg->first;
871 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
873 count = arg->count, from = arg->first;
874 if (dest != first)
876 if (dest[-1]->type == CPP_COMMA
877 && macro->variadic
878 && src->val.arg_no == macro->paramc)
880 /* Swallow a pasted comma if from == NULL, otherwise
881 drop the paste flag. */
882 if (from == NULL)
883 dest--;
884 else
885 paste_flag = dest - 1;
887 /* Remove the paste flag if the RHS is a placemarker. */
888 else if (count == 0)
889 paste_flag = dest - 1;
892 else
893 count = arg->expanded_count, from = arg->expanded;
895 /* Padding on the left of an argument (unless RHS of ##). */
896 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
897 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
898 *dest++ = padding_token (pfile, src);
900 if (count)
902 memcpy (dest, from, count * sizeof (cpp_token *));
903 dest += count;
905 /* With a non-empty argument on the LHS of ##, the last
906 token should be flagged PASTE_LEFT. */
907 if (src->flags & PASTE_LEFT)
908 paste_flag = dest - 1;
911 /* Avoid paste on RHS (even case count == 0). */
912 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
913 *dest++ = &pfile->avoid_paste;
915 /* Add a new paste flag, or remove an unwanted one. */
916 if (paste_flag)
918 cpp_token *token = _cpp_temp_token (pfile);
919 token->type = (*paste_flag)->type;
920 token->val.str = (*paste_flag)->val.str;
921 if (src->flags & PASTE_LEFT)
922 token->flags = (*paste_flag)->flags | PASTE_LEFT;
923 else
924 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
925 *paste_flag = token;
929 /* Free the expanded arguments. */
930 for (i = 0; i < macro->paramc; i++)
931 if (args[i].expanded)
932 free (args[i].expanded);
934 push_ptoken_context (pfile, node, buff, first, dest - first);
937 /* Return a special padding token, with padding inherited from SOURCE. */
938 static const cpp_token *
939 padding_token (pfile, source)
940 cpp_reader *pfile;
941 const cpp_token *source;
943 cpp_token *result = _cpp_temp_token (pfile);
945 result->type = CPP_PADDING;
946 result->val.source = source;
947 result->flags = 0;
948 return result;
951 /* Get a new uninitialized context. Create a new one if we cannot
952 re-use an old one. */
953 static cpp_context *
954 next_context (pfile)
955 cpp_reader *pfile;
957 cpp_context *result = pfile->context->next;
959 if (result == 0)
961 result = xnew (cpp_context);
962 result->prev = pfile->context;
963 result->next = 0;
964 pfile->context->next = result;
967 pfile->context = result;
968 return result;
971 /* Push a list of pointers to tokens. */
972 static void
973 push_ptoken_context (pfile, macro, buff, first, count)
974 cpp_reader *pfile;
975 cpp_hashnode *macro;
976 _cpp_buff *buff;
977 const cpp_token **first;
978 unsigned int count;
980 cpp_context *context = next_context (pfile);
982 context->direct_p = false;
983 context->macro = macro;
984 context->buff = buff;
985 FIRST (context).ptoken = first;
986 LAST (context).ptoken = first + count;
989 /* Push a list of tokens. */
990 static void
991 push_token_context (pfile, macro, first, count)
992 cpp_reader *pfile;
993 cpp_hashnode *macro;
994 const cpp_token *first;
995 unsigned int count;
997 cpp_context *context = next_context (pfile);
999 context->direct_p = true;
1000 context->macro = macro;
1001 context->buff = NULL;
1002 FIRST (context).token = first;
1003 LAST (context).token = first + count;
1006 /* Push a traditional macro's replacement text. */
1007 void
1008 _cpp_push_text_context (pfile, macro, start, len)
1009 cpp_reader *pfile;
1010 cpp_hashnode *macro;
1011 const uchar *start;
1012 size_t len;
1014 cpp_context *context = next_context (pfile);
1016 context->direct_p = true;
1017 context->macro = macro;
1018 context->buff = NULL;
1019 CUR (context) = start;
1020 RLIMIT (context) = start + len;
1021 macro->flags |= NODE_DISABLED;
1024 /* Expand an argument ARG before replacing parameters in a
1025 function-like macro. This works by pushing a context with the
1026 argument's tokens, and then expanding that into a temporary buffer
1027 as if it were a normal part of the token stream. collect_args()
1028 has terminated the argument's tokens with a CPP_EOF so that we know
1029 when we have fully expanded the argument. */
1030 static void
1031 expand_arg (pfile, arg)
1032 cpp_reader *pfile;
1033 macro_arg *arg;
1035 unsigned int capacity;
1036 bool saved_warn_trad;
1038 if (arg->count == 0)
1039 return;
1041 /* Don't warn about funlike macros when pre-expanding. */
1042 saved_warn_trad = CPP_WTRADITIONAL (pfile);
1043 CPP_WTRADITIONAL (pfile) = 0;
1045 /* Loop, reading in the arguments. */
1046 capacity = 256;
1047 arg->expanded = (const cpp_token **)
1048 xmalloc (capacity * sizeof (cpp_token *));
1050 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
1051 for (;;)
1053 const cpp_token *token;
1055 if (arg->expanded_count + 1 >= capacity)
1057 capacity *= 2;
1058 arg->expanded = (const cpp_token **)
1059 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
1062 token = cpp_get_token (pfile);
1064 if (token->type == CPP_EOF)
1065 break;
1067 arg->expanded[arg->expanded_count++] = token;
1070 _cpp_pop_context (pfile);
1072 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
1075 /* Pop the current context off the stack, re-enabling the macro if the
1076 context represented a macro's replacement list. The context
1077 structure is not freed so that we can re-use it later. */
1078 void
1079 _cpp_pop_context (pfile)
1080 cpp_reader *pfile;
1082 cpp_context *context = pfile->context;
1084 if (context->macro)
1085 context->macro->flags &= ~NODE_DISABLED;
1087 if (context->buff)
1088 _cpp_release_buff (pfile, context->buff);
1090 pfile->context = context->prev;
1093 /* Eternal routine to get a token. Also used nearly everywhere
1094 internally, except for places where we know we can safely call
1095 the lexer directly, such as lexing a directive name.
1097 Macro expansions and directives are transparently handled,
1098 including entering included files. Thus tokens are post-macro
1099 expansion, and after any intervening directives. External callers
1100 see CPP_EOF only at EOF. Internal callers also see it when meeting
1101 a directive inside a macro call, when at the end of a directive and
1102 state.in_directive is still 1, and at the end of argument
1103 pre-expansion. */
1104 const cpp_token *
1105 cpp_get_token (pfile)
1106 cpp_reader *pfile;
1108 const cpp_token *result;
1110 for (;;)
1112 cpp_hashnode *node;
1113 cpp_context *context = pfile->context;
1115 /* Context->prev == 0 <=> base context. */
1116 if (!context->prev)
1117 result = _cpp_lex_token (pfile);
1118 else if (FIRST (context).token != LAST (context).token)
1120 if (context->direct_p)
1121 result = FIRST (context).token++;
1122 else
1123 result = *FIRST (context).ptoken++;
1125 if (result->flags & PASTE_LEFT)
1127 paste_all_tokens (pfile, result);
1128 if (pfile->state.in_directive)
1129 continue;
1130 return padding_token (pfile, result);
1133 else
1135 _cpp_pop_context (pfile);
1136 if (pfile->state.in_directive)
1137 continue;
1138 return &pfile->avoid_paste;
1141 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1142 continue;
1144 if (result->type != CPP_NAME)
1145 break;
1147 node = result->val.node;
1149 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1150 break;
1152 if (!(node->flags & NODE_DISABLED))
1154 if (!pfile->state.prevent_expansion
1155 && enter_macro_context (pfile, node))
1157 if (pfile->state.in_directive)
1158 continue;
1159 return padding_token (pfile, result);
1162 else
1164 /* Flag this token as always unexpandable. FIXME: move this
1165 to collect_args()?. */
1166 cpp_token *t = _cpp_temp_token (pfile);
1167 t->type = result->type;
1168 t->flags = result->flags | NO_EXPAND;
1169 t->val.str = result->val.str;
1170 result = t;
1173 break;
1176 return result;
1179 /* Returns true if we're expanding an object-like macro that was
1180 defined in a system header. Just checks the macro at the top of
1181 the stack. Used for diagnostic suppression. */
1183 cpp_sys_macro_p (pfile)
1184 cpp_reader *pfile;
1186 cpp_hashnode *node = pfile->context->macro;
1188 return node && node->value.macro && node->value.macro->syshdr;
1191 /* Read each token in, until end of the current file. Directives are
1192 transparently processed. */
1193 void
1194 cpp_scan_nooutput (pfile)
1195 cpp_reader *pfile;
1197 /* Request a CPP_EOF token at the end of this file, rather than
1198 transparently continuing with the including file. */
1199 pfile->buffer->return_at_eof = true;
1201 if (CPP_OPTION (pfile, traditional))
1202 while (_cpp_read_logical_line_trad (pfile))
1204 else
1205 while (cpp_get_token (pfile)->type != CPP_EOF)
1209 /* Step back one (or more) tokens. Can only step mack more than 1 if
1210 they are from the lexer, and not from macro expansion. */
1211 void
1212 _cpp_backup_tokens (pfile, count)
1213 cpp_reader *pfile;
1214 unsigned int count;
1216 if (pfile->context->prev == NULL)
1218 pfile->lookaheads += count;
1219 while (count--)
1221 pfile->cur_token--;
1222 if (pfile->cur_token == pfile->cur_run->base
1223 /* Possible with -fpreprocessed and no leading #line. */
1224 && pfile->cur_run->prev != NULL)
1226 pfile->cur_run = pfile->cur_run->prev;
1227 pfile->cur_token = pfile->cur_run->limit;
1231 else
1233 if (count != 1)
1234 abort ();
1235 if (pfile->context->direct_p)
1236 FIRST (pfile->context).token--;
1237 else
1238 FIRST (pfile->context).ptoken--;
1242 /* #define directive parsing and handling. */
1244 /* Returns nonzero if a macro redefinition warning is required. */
1245 static bool
1246 warn_of_redefinition (pfile, node, macro2)
1247 cpp_reader *pfile;
1248 const cpp_hashnode *node;
1249 const cpp_macro *macro2;
1251 const cpp_macro *macro1;
1252 unsigned int i;
1254 /* Some redefinitions need to be warned about regardless. */
1255 if (node->flags & NODE_WARN)
1256 return true;
1258 /* Redefinition of a macro is allowed if and only if the old and new
1259 definitions are the same. (6.10.3 paragraph 2). */
1260 macro1 = node->value.macro;
1262 /* Don't check count here as it can be different in valid
1263 traditional redefinitions with just whitespace differences. */
1264 if (macro1->paramc != macro2->paramc
1265 || macro1->fun_like != macro2->fun_like
1266 || macro1->variadic != macro2->variadic)
1267 return true;
1269 /* Check parameter spellings. */
1270 for (i = 0; i < macro1->paramc; i++)
1271 if (macro1->params[i] != macro2->params[i])
1272 return true;
1274 /* Check the replacement text or tokens. */
1275 if (CPP_OPTION (pfile, traditional))
1276 return _cpp_expansions_different_trad (macro1, macro2);
1278 if (macro1->count == macro2->count)
1279 for (i = 0; i < macro1->count; i++)
1280 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1281 return true;
1283 return false;
1286 /* Free the definition of hashnode H. */
1287 void
1288 _cpp_free_definition (h)
1289 cpp_hashnode *h;
1291 /* Macros and assertions no longer have anything to free. */
1292 h->type = NT_VOID;
1293 /* Clear builtin flag in case of redefinition. */
1294 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1297 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1298 zero on success, nonzero if the parameter is a duplicate. */
1299 bool
1300 _cpp_save_parameter (pfile, macro, node)
1301 cpp_reader *pfile;
1302 cpp_macro *macro;
1303 cpp_hashnode *node;
1305 unsigned int len;
1306 /* Constraint 6.10.3.6 - duplicate parameter names. */
1307 if (node->flags & NODE_MACRO_ARG)
1309 cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"",
1310 NODE_NAME (node));
1311 return true;
1314 if (BUFF_ROOM (pfile->a_buff)
1315 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1316 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1318 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1319 node->flags |= NODE_MACRO_ARG;
1320 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1321 if (len > pfile->macro_buffer_len)
1323 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
1324 pfile->macro_buffer_len = len;
1326 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1327 = node->value;
1329 node->value.arg_index = macro->paramc;
1330 return false;
1333 /* Check the syntax of the parameters in a MACRO definition. Returns
1334 false if an error occurs. */
1335 static bool
1336 parse_params (pfile, macro)
1337 cpp_reader *pfile;
1338 cpp_macro *macro;
1340 unsigned int prev_ident = 0;
1342 for (;;)
1344 const cpp_token *token = _cpp_lex_token (pfile);
1346 switch (token->type)
1348 default:
1349 /* Allow/ignore comments in parameter lists if we are
1350 preserving comments in macro expansions. */
1351 if (token->type == CPP_COMMENT
1352 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1353 continue;
1355 cpp_error (pfile, DL_ERROR,
1356 "\"%s\" may not appear in macro parameter list",
1357 cpp_token_as_text (pfile, token));
1358 return false;
1360 case CPP_NAME:
1361 if (prev_ident)
1363 cpp_error (pfile, DL_ERROR,
1364 "macro parameters must be comma-separated");
1365 return false;
1367 prev_ident = 1;
1369 if (_cpp_save_parameter (pfile, macro, token->val.node))
1370 return false;
1371 continue;
1373 case CPP_CLOSE_PAREN:
1374 if (prev_ident || macro->paramc == 0)
1375 return true;
1377 /* Fall through to pick up the error. */
1378 case CPP_COMMA:
1379 if (!prev_ident)
1381 cpp_error (pfile, DL_ERROR, "parameter name missing");
1382 return false;
1384 prev_ident = 0;
1385 continue;
1387 case CPP_ELLIPSIS:
1388 macro->variadic = 1;
1389 if (!prev_ident)
1391 _cpp_save_parameter (pfile, macro,
1392 pfile->spec_nodes.n__VA_ARGS__);
1393 pfile->state.va_args_ok = 1;
1394 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1395 cpp_error (pfile, DL_PEDWARN,
1396 "anonymous variadic macros were introduced in C99");
1398 else if (CPP_OPTION (pfile, pedantic))
1399 cpp_error (pfile, DL_PEDWARN,
1400 "ISO C does not permit named variadic macros");
1402 /* We're at the end, and just expect a closing parenthesis. */
1403 token = _cpp_lex_token (pfile);
1404 if (token->type == CPP_CLOSE_PAREN)
1405 return true;
1406 /* Fall through. */
1408 case CPP_EOF:
1409 cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list");
1410 return false;
1415 /* Allocate room for a token from a macro's replacement list. */
1416 static cpp_token *
1417 alloc_expansion_token (pfile, macro)
1418 cpp_reader *pfile;
1419 cpp_macro *macro;
1421 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1422 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1424 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1427 /* Lex a token from the expansion of MACRO, but mark parameters as we
1428 find them and warn of traditional stringification. */
1429 static cpp_token *
1430 lex_expansion_token (pfile, macro)
1431 cpp_reader *pfile;
1432 cpp_macro *macro;
1434 cpp_token *token;
1436 pfile->cur_token = alloc_expansion_token (pfile, macro);
1437 token = _cpp_lex_direct (pfile);
1439 /* Is this a parameter? */
1440 if (token->type == CPP_NAME
1441 && (token->val.node->flags & NODE_MACRO_ARG) != 0)
1443 token->type = CPP_MACRO_ARG;
1444 token->val.arg_no = token->val.node->value.arg_index;
1446 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1447 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1448 check_trad_stringification (pfile, macro, &token->val.str);
1450 return token;
1453 static bool
1454 create_iso_definition (pfile, macro)
1455 cpp_reader *pfile;
1456 cpp_macro *macro;
1458 cpp_token *token;
1459 const cpp_token *ctoken;
1461 /* Get the first token of the expansion (or the '(' of a
1462 function-like macro). */
1463 ctoken = _cpp_lex_token (pfile);
1465 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1467 bool ok = parse_params (pfile, macro);
1468 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1469 if (!ok)
1470 return false;
1472 /* Success. Commit the parameter array. */
1473 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
1474 macro->fun_like = 1;
1476 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1477 cpp_error (pfile, DL_PEDWARN,
1478 "ISO C requires whitespace after the macro name");
1480 if (macro->fun_like)
1481 token = lex_expansion_token (pfile, macro);
1482 else
1484 token = alloc_expansion_token (pfile, macro);
1485 *token = *ctoken;
1488 for (;;)
1490 /* Check the stringifying # constraint 6.10.3.2.1 of
1491 function-like macros when lexing the subsequent token. */
1492 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1494 if (token->type == CPP_MACRO_ARG)
1496 token->flags &= ~PREV_WHITE;
1497 token->flags |= STRINGIFY_ARG;
1498 token->flags |= token[-1].flags & PREV_WHITE;
1499 token[-1] = token[0];
1500 macro->count--;
1502 /* Let assembler get away with murder. */
1503 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1505 cpp_error (pfile, DL_ERROR,
1506 "'#' is not followed by a macro parameter");
1507 return false;
1511 if (token->type == CPP_EOF)
1512 break;
1514 /* Paste operator constraint 6.10.3.3.1. */
1515 if (token->type == CPP_PASTE)
1517 /* Token-paste ##, can appear in both object-like and
1518 function-like macros, but not at the ends. */
1519 if (--macro->count > 0)
1520 token = lex_expansion_token (pfile, macro);
1522 if (macro->count == 0 || token->type == CPP_EOF)
1524 cpp_error (pfile, DL_ERROR,
1525 "'##' cannot appear at either end of a macro expansion");
1526 return false;
1529 token[-1].flags |= PASTE_LEFT;
1532 token = lex_expansion_token (pfile, macro);
1535 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1537 /* Don't count the CPP_EOF. */
1538 macro->count--;
1540 /* Clear whitespace on first token for warn_of_redefinition(). */
1541 if (macro->count)
1542 macro->exp.tokens[0].flags &= ~PREV_WHITE;
1544 /* Commit the memory. */
1545 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
1547 return true;
1550 /* Parse a macro and save its expansion. Returns nonzero on success. */
1551 bool
1552 _cpp_create_definition (pfile, node)
1553 cpp_reader *pfile;
1554 cpp_hashnode *node;
1556 cpp_macro *macro;
1557 unsigned int i;
1558 bool ok;
1560 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1561 macro->line = pfile->directive_line;
1562 macro->params = 0;
1563 macro->paramc = 0;
1564 macro->variadic = 0;
1565 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
1566 macro->count = 0;
1567 macro->fun_like = 0;
1568 /* To suppress some diagnostics. */
1569 macro->syshdr = pfile->map->sysp != 0;
1571 if (CPP_OPTION (pfile, traditional))
1572 ok = _cpp_create_trad_definition (pfile, macro);
1573 else
1575 cpp_token *saved_cur_token = pfile->cur_token;
1577 ok = create_iso_definition (pfile, macro);
1579 /* Restore lexer position because of games lex_expansion_token()
1580 plays lexing the macro. We set the type for SEEN_EOL() in
1581 cpplib.c.
1583 Longer term we should lex the whole line before coming here,
1584 and just copy the expansion. */
1585 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1586 pfile->cur_token = saved_cur_token;
1588 /* Stop the lexer accepting __VA_ARGS__. */
1589 pfile->state.va_args_ok = 0;
1592 /* Clear the fast argument lookup indices. */
1593 for (i = macro->paramc; i-- > 0; )
1595 struct cpp_hashnode *node = macro->params[i];
1596 node->flags &= ~ NODE_MACRO_ARG;
1597 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1600 if (!ok)
1601 return ok;
1603 if (node->type == NT_MACRO)
1605 if (CPP_OPTION (pfile, warn_unused_macros))
1606 _cpp_warn_if_unused_macro (pfile, node, NULL);
1608 if (warn_of_redefinition (pfile, node, macro))
1610 cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0,
1611 "\"%s\" redefined", NODE_NAME (node));
1613 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1614 cpp_error_with_line (pfile, DL_PEDWARN,
1615 node->value.macro->line, 0,
1616 "this is the location of the previous definition");
1620 if (node->type != NT_VOID)
1621 _cpp_free_definition (node);
1623 /* Enter definition in hash table. */
1624 node->type = NT_MACRO;
1625 node->value.macro = macro;
1626 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1627 node->flags |= NODE_WARN;
1629 return ok;
1632 /* Warn if a token in STRING matches one of a function-like MACRO's
1633 parameters. */
1634 static void
1635 check_trad_stringification (pfile, macro, string)
1636 cpp_reader *pfile;
1637 const cpp_macro *macro;
1638 const cpp_string *string;
1640 unsigned int i, len;
1641 const uchar *p, *q, *limit;
1643 /* Loop over the string. */
1644 limit = string->text + string->len - 1;
1645 for (p = string->text + 1; p < limit; p = q)
1647 /* Find the start of an identifier. */
1648 while (p < limit && !is_idstart (*p))
1649 p++;
1651 /* Find the end of the identifier. */
1652 q = p;
1653 while (q < limit && is_idchar (*q))
1654 q++;
1656 len = q - p;
1658 /* Loop over the function macro arguments to see if the
1659 identifier inside the string matches one of them. */
1660 for (i = 0; i < macro->paramc; i++)
1662 const cpp_hashnode *node = macro->params[i];
1664 if (NODE_LEN (node) == len
1665 && !memcmp (p, NODE_NAME (node), len))
1667 cpp_error (pfile, DL_WARNING,
1668 "macro argument \"%s\" would be stringified in traditional C",
1669 NODE_NAME (node));
1670 break;
1676 /* Returns the name, arguments and expansion of a macro, in a format
1677 suitable to be read back in again, and therefore also for DWARF 2
1678 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1679 Caller is expected to generate the "#define" bit if needed. The
1680 returned text is temporary, and automatically freed later. */
1681 const unsigned char *
1682 cpp_macro_definition (pfile, node)
1683 cpp_reader *pfile;
1684 const cpp_hashnode *node;
1686 unsigned int i, len;
1687 const cpp_macro *macro = node->value.macro;
1688 unsigned char *buffer;
1690 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1692 cpp_error (pfile, DL_ICE,
1693 "invalid hash type %d in cpp_macro_definition", node->type);
1694 return 0;
1697 /* Calculate length. */
1698 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
1699 if (macro->fun_like)
1701 len += 4; /* "()" plus possible final ".." of named
1702 varargs (we have + 1 below). */
1703 for (i = 0; i < macro->paramc; i++)
1704 len += NODE_LEN (macro->params[i]) + 1; /* "," */
1707 if (CPP_OPTION (pfile, traditional))
1708 len += _cpp_replacement_text_len (macro);
1709 else
1711 for (i = 0; i < macro->count; i++)
1713 cpp_token *token = &macro->exp.tokens[i];
1715 if (token->type == CPP_MACRO_ARG)
1716 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1717 else
1718 len += cpp_token_len (token) + 1; /* Includes room for ' '. */
1719 if (token->flags & STRINGIFY_ARG)
1720 len++; /* "#" */
1721 if (token->flags & PASTE_LEFT)
1722 len += 3; /* " ##" */
1726 if (len > pfile->macro_buffer_len)
1728 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
1729 pfile->macro_buffer_len = len;
1732 /* Fill in the buffer. Start with the macro name. */
1733 buffer = pfile->macro_buffer;
1734 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1735 buffer += NODE_LEN (node);
1737 /* Parameter names. */
1738 if (macro->fun_like)
1740 *buffer++ = '(';
1741 for (i = 0; i < macro->paramc; i++)
1743 cpp_hashnode *param = macro->params[i];
1745 if (param != pfile->spec_nodes.n__VA_ARGS__)
1747 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1748 buffer += NODE_LEN (param);
1751 if (i + 1 < macro->paramc)
1752 /* Don't emit a space after the comma here; we're trying
1753 to emit a Dwarf-friendly definition, and the Dwarf spec
1754 forbids spaces in the argument list. */
1755 *buffer++ = ',';
1756 else if (macro->variadic)
1757 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1759 *buffer++ = ')';
1762 /* The Dwarf spec requires a space after the macro name, even if the
1763 definition is the empty string. */
1764 *buffer++ = ' ';
1766 if (CPP_OPTION (pfile, traditional))
1767 buffer = _cpp_copy_replacement_text (macro, buffer);
1768 else if (macro->count)
1769 /* Expansion tokens. */
1771 for (i = 0; i < macro->count; i++)
1773 cpp_token *token = &macro->exp.tokens[i];
1775 if (token->flags & PREV_WHITE)
1776 *buffer++ = ' ';
1777 if (token->flags & STRINGIFY_ARG)
1778 *buffer++ = '#';
1780 if (token->type == CPP_MACRO_ARG)
1782 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1783 memcpy (buffer,
1784 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1785 buffer += len;
1787 else
1788 buffer = cpp_spell_token (pfile, token, buffer);
1790 if (token->flags & PASTE_LEFT)
1792 *buffer++ = ' ';
1793 *buffer++ = '#';
1794 *buffer++ = '#';
1795 /* Next has PREV_WHITE; see _cpp_create_definition. */
1800 *buffer = '\0';
1801 return pfile->macro_buffer;