* config/frv/frv-protos.h: Guard ifcvt functions declarations with
[official-gcc.git] / libcpp / macro.c
blob77183343d9e15576c614d706dd4e6c89f7421735
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, 2004 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 "internal.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 (linemap_lookup (pfile->line_table, macro->line)))
84 cpp_error_with_line (pfile, CPP_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 /* Helper function for builtin_macro. Returns the text generated by
113 a builtin macro. */
114 const uchar *
115 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
117 const struct line_map *map;
118 const uchar *result = NULL;
119 unsigned int number = 1;
121 switch (node->value.builtin)
123 default:
124 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
125 NODE_NAME (node));
126 break;
128 case BT_FILE:
129 case BT_BASE_FILE:
131 unsigned int len;
132 const char *name;
133 uchar *buf;
134 map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
136 if (node->value.builtin == BT_BASE_FILE)
137 while (! MAIN_FILE_P (map))
138 map = INCLUDED_FROM (pfile->line_table, map);
140 name = map->to_file;
141 len = strlen (name);
142 buf = _cpp_unaligned_alloc (pfile, len * 4 + 3);
143 result = buf;
144 *buf = '"';
145 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
146 *buf++ = '"';
147 *buf = '\0';
149 break;
151 case BT_INCLUDE_LEVEL:
152 /* The line map depth counts the primary source as level 1, but
153 historically __INCLUDE_DEPTH__ has called the primary source
154 level 0. */
155 number = pfile->line_table->depth - 1;
156 break;
158 case BT_SPECLINE:
159 map = &pfile->line_table->maps[pfile->line_table->used-1];
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_table->highest_line;
165 else
166 number = pfile->cur_token[-1].src_loc;
167 number = SOURCE_LINE (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, CPP_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 a new token may not
247 be 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 if (pfile->directive_result.type == CPP_PRAGMA)
266 cpp_token *tok = _cpp_temp_token (pfile);
267 *tok = pfile->directive_result;
268 push_token_context (pfile, NULL, tok, 1);
271 return 1;
274 buf = _cpp_builtin_macro_text (pfile, node);
275 len = ustrlen (buf);
276 nbuf = alloca (len + 1);
277 memcpy (nbuf, buf, len);
278 nbuf[len]='\n';
280 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
281 _cpp_clean_line (pfile);
283 /* Set pfile->cur_token as required by _cpp_lex_direct. */
284 pfile->cur_token = _cpp_temp_token (pfile);
285 push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
286 if (pfile->buffer->cur != pfile->buffer->rlimit)
287 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
288 NODE_NAME (node));
289 _cpp_pop_buffer (pfile);
291 return 1;
294 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
295 backslashes and double quotes. Non-printable characters are
296 converted to octal. DEST must be of sufficient size. Returns
297 a pointer to the end of the string. */
298 uchar *
299 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
301 while (len--)
303 uchar c = *src++;
305 if (c == '\\' || c == '"')
307 *dest++ = '\\';
308 *dest++ = c;
310 else
312 if (ISPRINT (c))
313 *dest++ = c;
314 else
316 sprintf ((char *) dest, "\\%03o", c);
317 dest += 4;
322 return dest;
325 /* Convert a token sequence ARG to a single string token according to
326 the rules of the ISO C #-operator. */
327 static const cpp_token *
328 stringify_arg (cpp_reader *pfile, macro_arg *arg)
330 unsigned char *dest;
331 unsigned int i, escape_it, backslash_count = 0;
332 const cpp_token *source = NULL;
333 size_t len;
335 if (BUFF_ROOM (pfile->u_buff) < 3)
336 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
337 dest = BUFF_FRONT (pfile->u_buff);
338 *dest++ = '"';
340 /* Loop, reading in the argument's tokens. */
341 for (i = 0; i < arg->count; i++)
343 const cpp_token *token = arg->first[i];
345 if (token->type == CPP_PADDING)
347 if (source == NULL)
348 source = token->val.source;
349 continue;
352 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
353 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
355 /* Room for each char being written in octal, initial space and
356 final quote and NUL. */
357 len = cpp_token_len (token);
358 if (escape_it)
359 len *= 4;
360 len += 3;
362 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
364 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
365 _cpp_extend_buff (pfile, &pfile->u_buff, len);
366 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
369 /* Leading white space? */
370 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
372 if (source == NULL)
373 source = token;
374 if (source->flags & PREV_WHITE)
375 *dest++ = ' ';
377 source = NULL;
379 if (escape_it)
381 _cpp_buff *buff = _cpp_get_buff (pfile, len);
382 unsigned char *buf = BUFF_FRONT (buff);
383 len = cpp_spell_token (pfile, token, buf) - buf;
384 dest = cpp_quote_string (dest, buf, len);
385 _cpp_release_buff (pfile, buff);
387 else
388 dest = cpp_spell_token (pfile, token, dest);
390 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
391 backslash_count++;
392 else
393 backslash_count = 0;
396 /* Ignore the final \ of invalid string literals. */
397 if (backslash_count & 1)
399 cpp_error (pfile, CPP_DL_WARNING,
400 "invalid string literal, ignoring final '\\'");
401 dest--;
404 /* Commit the memory, including NUL, and return the token. */
405 *dest++ = '"';
406 len = dest - BUFF_FRONT (pfile->u_buff);
407 BUFF_FRONT (pfile->u_buff) = dest + 1;
408 return new_string_token (pfile, dest - len, len);
411 /* Try to paste two tokens. On success, return nonzero. In any
412 case, PLHS is updated to point to the pasted token, which is
413 guaranteed to not have the PASTE_LEFT flag set. */
414 static bool
415 paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
417 unsigned char *buf, *end;
418 const cpp_token *lhs;
419 unsigned int len;
420 bool valid;
422 lhs = *plhs;
423 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
424 buf = alloca (len);
425 end = cpp_spell_token (pfile, lhs, buf);
427 /* Avoid comment headers, since they are still processed in stage 3.
428 It is simpler to insert a space here, rather than modifying the
429 lexer to ignore comments in some circumstances. Simply returning
430 false doesn't work, since we want to clear the PASTE_LEFT flag. */
431 if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
432 *end++ = ' ';
433 end = cpp_spell_token (pfile, rhs, end);
434 *end = '\n';
436 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
437 _cpp_clean_line (pfile);
439 /* Set pfile->cur_token as required by _cpp_lex_direct. */
440 pfile->cur_token = _cpp_temp_token (pfile);
441 *plhs = _cpp_lex_direct (pfile);
442 valid = pfile->buffer->cur == pfile->buffer->rlimit;
443 _cpp_pop_buffer (pfile);
445 return valid;
448 /* Handles an arbitrarily long sequence of ## operators, with initial
449 operand LHS. This implementation is left-associative,
450 non-recursive, and finishes a paste before handling succeeding
451 ones. If a paste fails, we back up to the RHS of the failing ##
452 operator before pushing the context containing the result of prior
453 successful pastes, with the effect that the RHS appears in the
454 output stream after the pasted LHS normally. */
455 static void
456 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
458 const cpp_token *rhs;
459 cpp_context *context = pfile->context;
463 /* Take the token directly from the current context. We can do
464 this, because we are in the replacement list of either an
465 object-like macro, or a function-like macro with arguments
466 inserted. In either case, the constraints to #define
467 guarantee we have at least one more token. */
468 if (context->direct_p)
469 rhs = FIRST (context).token++;
470 else
471 rhs = *FIRST (context).ptoken++;
473 if (rhs->type == CPP_PADDING)
474 abort ();
476 if (!paste_tokens (pfile, &lhs, rhs))
478 _cpp_backup_tokens (pfile, 1);
480 /* Mandatory error for all apart from assembler. */
481 if (CPP_OPTION (pfile, lang) != CLK_ASM)
482 cpp_error (pfile, CPP_DL_ERROR,
483 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
484 cpp_token_as_text (pfile, lhs),
485 cpp_token_as_text (pfile, rhs));
486 break;
489 while (rhs->flags & PASTE_LEFT);
491 /* Put the resulting token in its own context. */
492 push_token_context (pfile, NULL, lhs, 1);
495 /* Returns TRUE if the number of arguments ARGC supplied in an
496 invocation of the MACRO referenced by NODE is valid. An empty
497 invocation to a macro with no parameters should pass ARGC as zero.
499 Note that MACRO cannot necessarily be deduced from NODE, in case
500 NODE was redefined whilst collecting arguments. */
501 bool
502 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
504 if (argc == macro->paramc)
505 return true;
507 if (argc < macro->paramc)
509 /* As an extension, a rest argument is allowed to not appear in
510 the invocation at all.
511 e.g. #define debug(format, args...) something
512 debug("string");
514 This is exactly the same as if there had been an empty rest
515 argument - debug("string", ). */
517 if (argc + 1 == macro->paramc && macro->variadic)
519 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
520 cpp_error (pfile, CPP_DL_PEDWARN,
521 "ISO C99 requires rest arguments to be used");
522 return true;
525 cpp_error (pfile, CPP_DL_ERROR,
526 "macro \"%s\" requires %u arguments, but only %u given",
527 NODE_NAME (node), macro->paramc, argc);
529 else
530 cpp_error (pfile, CPP_DL_ERROR,
531 "macro \"%s\" passed %u arguments, but takes just %u",
532 NODE_NAME (node), argc, macro->paramc);
534 return false;
537 /* Reads and returns the arguments to a function-like macro
538 invocation. Assumes the opening parenthesis has been processed.
539 If there is an error, emits an appropriate diagnostic and returns
540 NULL. Each argument is terminated by a CPP_EOF token, for the
541 future benefit of expand_arg(). */
542 static _cpp_buff *
543 collect_args (cpp_reader *pfile, const cpp_hashnode *node)
545 _cpp_buff *buff, *base_buff;
546 cpp_macro *macro;
547 macro_arg *args, *arg;
548 const cpp_token *token;
549 unsigned int argc;
551 macro = node->value.macro;
552 if (macro->paramc)
553 argc = macro->paramc;
554 else
555 argc = 1;
556 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
557 + sizeof (macro_arg)));
558 base_buff = buff;
559 args = (macro_arg *) buff->base;
560 memset (args, 0, argc * sizeof (macro_arg));
561 buff->cur = (unsigned char *) &args[argc];
562 arg = args, argc = 0;
564 /* Collect the tokens making up each argument. We don't yet know
565 how many arguments have been supplied, whether too many or too
566 few. Hence the slightly bizarre usage of "argc" and "arg". */
569 unsigned int paren_depth = 0;
570 unsigned int ntokens = 0;
572 argc++;
573 arg->first = (const cpp_token **) buff->cur;
575 for (;;)
577 /* Require space for 2 new tokens (including a CPP_EOF). */
578 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
580 buff = _cpp_append_extend_buff (pfile, buff,
581 1000 * sizeof (cpp_token *));
582 arg->first = (const cpp_token **) buff->cur;
585 token = cpp_get_token (pfile);
587 if (token->type == CPP_PADDING)
589 /* Drop leading padding. */
590 if (ntokens == 0)
591 continue;
593 else if (token->type == CPP_OPEN_PAREN)
594 paren_depth++;
595 else if (token->type == CPP_CLOSE_PAREN)
597 if (paren_depth-- == 0)
598 break;
600 else if (token->type == CPP_COMMA)
602 /* A comma does not terminate an argument within
603 parentheses or as part of a variable argument. */
604 if (paren_depth == 0
605 && ! (macro->variadic && argc == macro->paramc))
606 break;
608 else if (token->type == CPP_EOF
609 || (token->type == CPP_HASH && token->flags & BOL))
610 break;
612 arg->first[ntokens++] = token;
615 /* Drop trailing padding. */
616 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
617 ntokens--;
619 arg->count = ntokens;
620 arg->first[ntokens] = &pfile->eof;
622 /* Terminate the argument. Excess arguments loop back and
623 overwrite the final legitimate argument, before failing. */
624 if (argc <= macro->paramc)
626 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
627 if (argc != macro->paramc)
628 arg++;
631 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
633 if (token->type == CPP_EOF)
635 /* We still need the CPP_EOF to end directives, and to end
636 pre-expansion of a macro argument. Step back is not
637 unconditional, since we don't want to return a CPP_EOF to our
638 callers at the end of an -include-d file. */
639 if (pfile->context->prev || pfile->state.in_directive)
640 _cpp_backup_tokens (pfile, 1);
641 cpp_error (pfile, CPP_DL_ERROR,
642 "unterminated argument list invoking macro \"%s\"",
643 NODE_NAME (node));
645 else
647 /* A single empty argument is counted as no argument. */
648 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
649 argc = 0;
650 if (_cpp_arguments_ok (pfile, macro, node, argc))
652 /* GCC has special semantics for , ## b where b is a varargs
653 parameter: we remove the comma if b was omitted entirely.
654 If b was merely an empty argument, the comma is retained.
655 If the macro takes just one (varargs) parameter, then we
656 retain the comma only if we are standards conforming.
658 If FIRST is NULL replace_args () swallows the comma. */
659 if (macro->variadic && (argc < macro->paramc
660 || (argc == 1 && args[0].count == 0
661 && !CPP_OPTION (pfile, std))))
662 args[macro->paramc - 1].first = NULL;
663 return base_buff;
667 /* An error occurred. */
668 _cpp_release_buff (pfile, base_buff);
669 return NULL;
672 /* Search for an opening parenthesis to the macro of NODE, in such a
673 way that, if none is found, we don't lose the information in any
674 intervening padding tokens. If we find the parenthesis, collect
675 the arguments and return the buffer containing them. */
676 static _cpp_buff *
677 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
679 const cpp_token *token, *padding = NULL;
681 for (;;)
683 token = cpp_get_token (pfile);
684 if (token->type != CPP_PADDING)
685 break;
686 if (padding == NULL
687 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
688 padding = token;
691 if (token->type == CPP_OPEN_PAREN)
693 pfile->state.parsing_args = 2;
694 return collect_args (pfile, node);
697 /* CPP_EOF can be the end of macro arguments, or the end of the
698 file. We mustn't back up over the latter. Ugh. */
699 if (token->type != CPP_EOF || token == &pfile->eof)
701 /* Back up. We may have skipped padding, in which case backing
702 up more than one token when expanding macros is in general
703 too difficult. We re-insert it in its own context. */
704 _cpp_backup_tokens (pfile, 1);
705 if (padding)
706 push_token_context (pfile, NULL, padding, 1);
709 return NULL;
712 /* Push the context of a macro with hash entry NODE onto the context
713 stack. If we can successfully expand the macro, we push a context
714 containing its yet-to-be-rescanned replacement list and return one.
715 Otherwise, we don't push a context and return zero. */
716 static int
717 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
719 /* The presence of a macro invalidates a file's controlling macro. */
720 pfile->mi_valid = false;
722 pfile->state.angled_headers = false;
724 /* Handle standard macros. */
725 if (! (node->flags & NODE_BUILTIN))
727 cpp_macro *macro = node->value.macro;
729 if (macro->fun_like)
731 _cpp_buff *buff;
733 pfile->state.prevent_expansion++;
734 pfile->keep_tokens++;
735 pfile->state.parsing_args = 1;
736 buff = funlike_invocation_p (pfile, node);
737 pfile->state.parsing_args = 0;
738 pfile->keep_tokens--;
739 pfile->state.prevent_expansion--;
741 if (buff == NULL)
743 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
744 cpp_error (pfile, CPP_DL_WARNING,
745 "function-like macro \"%s\" must be used with arguments in traditional C",
746 NODE_NAME (node));
748 return 0;
751 if (macro->paramc > 0)
752 replace_args (pfile, node, macro, (macro_arg *) buff->base);
753 _cpp_release_buff (pfile, buff);
756 /* Disable the macro within its expansion. */
757 node->flags |= NODE_DISABLED;
759 macro->used = 1;
761 if (macro->paramc == 0)
762 push_token_context (pfile, node, macro->exp.tokens, macro->count);
764 return 1;
767 /* Handle built-in macros and the _Pragma operator. */
768 return builtin_macro (pfile, node);
771 /* Replace the parameters in a function-like macro of NODE with the
772 actual ARGS, and place the result in a newly pushed token context.
773 Expand each argument before replacing, unless it is operated upon
774 by the # or ## operators. */
775 static void
776 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
778 unsigned int i, total;
779 const cpp_token *src, *limit;
780 const cpp_token **dest, **first;
781 macro_arg *arg;
782 _cpp_buff *buff;
784 /* First, fully macro-expand arguments, calculating the number of
785 tokens in the final expansion as we go. The ordering of the if
786 statements below is subtle; we must handle stringification before
787 pasting. */
788 total = macro->count;
789 limit = macro->exp.tokens + macro->count;
791 for (src = macro->exp.tokens; src < limit; src++)
792 if (src->type == CPP_MACRO_ARG)
794 /* Leading and trailing padding tokens. */
795 total += 2;
797 /* We have an argument. If it is not being stringified or
798 pasted it is macro-replaced before insertion. */
799 arg = &args[src->val.arg_no - 1];
801 if (src->flags & STRINGIFY_ARG)
803 if (!arg->stringified)
804 arg->stringified = stringify_arg (pfile, arg);
806 else if ((src->flags & PASTE_LEFT)
807 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
808 total += arg->count - 1;
809 else
811 if (!arg->expanded)
812 expand_arg (pfile, arg);
813 total += arg->expanded_count - 1;
817 /* Now allocate space for the expansion, copy the tokens and replace
818 the arguments. */
819 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
820 first = (const cpp_token **) buff->base;
821 dest = first;
823 for (src = macro->exp.tokens; src < limit; src++)
825 unsigned int count;
826 const cpp_token **from, **paste_flag;
828 if (src->type != CPP_MACRO_ARG)
830 *dest++ = src;
831 continue;
834 paste_flag = 0;
835 arg = &args[src->val.arg_no - 1];
836 if (src->flags & STRINGIFY_ARG)
837 count = 1, from = &arg->stringified;
838 else if (src->flags & PASTE_LEFT)
839 count = arg->count, from = arg->first;
840 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
842 count = arg->count, from = arg->first;
843 if (dest != first)
845 if (dest[-1]->type == CPP_COMMA
846 && macro->variadic
847 && src->val.arg_no == macro->paramc)
849 /* Swallow a pasted comma if from == NULL, otherwise
850 drop the paste flag. */
851 if (from == NULL)
852 dest--;
853 else
854 paste_flag = dest - 1;
856 /* Remove the paste flag if the RHS is a placemarker. */
857 else if (count == 0)
858 paste_flag = dest - 1;
861 else
862 count = arg->expanded_count, from = arg->expanded;
864 /* Padding on the left of an argument (unless RHS of ##). */
865 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
866 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
867 *dest++ = padding_token (pfile, src);
869 if (count)
871 memcpy (dest, from, count * sizeof (cpp_token *));
872 dest += count;
874 /* With a non-empty argument on the LHS of ##, the last
875 token should be flagged PASTE_LEFT. */
876 if (src->flags & PASTE_LEFT)
877 paste_flag = dest - 1;
880 /* Avoid paste on RHS (even case count == 0). */
881 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
882 *dest++ = &pfile->avoid_paste;
884 /* Add a new paste flag, or remove an unwanted one. */
885 if (paste_flag)
887 cpp_token *token = _cpp_temp_token (pfile);
888 token->type = (*paste_flag)->type;
889 token->val.str = (*paste_flag)->val.str;
890 if (src->flags & PASTE_LEFT)
891 token->flags = (*paste_flag)->flags | PASTE_LEFT;
892 else
893 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
894 *paste_flag = token;
898 /* Free the expanded arguments. */
899 for (i = 0; i < macro->paramc; i++)
900 if (args[i].expanded)
901 free (args[i].expanded);
903 push_ptoken_context (pfile, node, buff, first, dest - first);
906 /* Return a special padding token, with padding inherited from SOURCE. */
907 static const cpp_token *
908 padding_token (cpp_reader *pfile, const cpp_token *source)
910 cpp_token *result = _cpp_temp_token (pfile);
912 result->type = CPP_PADDING;
913 result->val.source = source;
914 result->flags = 0;
915 return result;
918 /* Get a new uninitialized context. Create a new one if we cannot
919 re-use an old one. */
920 static cpp_context *
921 next_context (cpp_reader *pfile)
923 cpp_context *result = pfile->context->next;
925 if (result == 0)
927 result = XNEW (cpp_context);
928 result->prev = pfile->context;
929 result->next = 0;
930 pfile->context->next = result;
933 pfile->context = result;
934 return result;
937 /* Push a list of pointers to tokens. */
938 static void
939 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
940 const cpp_token **first, unsigned int count)
942 cpp_context *context = next_context (pfile);
944 context->direct_p = false;
945 context->macro = macro;
946 context->buff = buff;
947 FIRST (context).ptoken = first;
948 LAST (context).ptoken = first + count;
951 /* Push a list of tokens. */
952 static void
953 push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
954 const cpp_token *first, unsigned int count)
956 cpp_context *context = next_context (pfile);
958 context->direct_p = true;
959 context->macro = macro;
960 context->buff = NULL;
961 FIRST (context).token = first;
962 LAST (context).token = first + count;
965 /* Push a traditional macro's replacement text. */
966 void
967 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
968 const uchar *start, size_t len)
970 cpp_context *context = next_context (pfile);
972 context->direct_p = true;
973 context->macro = macro;
974 context->buff = NULL;
975 CUR (context) = start;
976 RLIMIT (context) = start + len;
977 macro->flags |= NODE_DISABLED;
980 /* Expand an argument ARG before replacing parameters in a
981 function-like macro. This works by pushing a context with the
982 argument's tokens, and then expanding that into a temporary buffer
983 as if it were a normal part of the token stream. collect_args()
984 has terminated the argument's tokens with a CPP_EOF so that we know
985 when we have fully expanded the argument. */
986 static void
987 expand_arg (cpp_reader *pfile, macro_arg *arg)
989 unsigned int capacity;
990 bool saved_warn_trad;
992 if (arg->count == 0)
993 return;
995 /* Don't warn about funlike macros when pre-expanding. */
996 saved_warn_trad = CPP_WTRADITIONAL (pfile);
997 CPP_WTRADITIONAL (pfile) = 0;
999 /* Loop, reading in the arguments. */
1000 capacity = 256;
1001 arg->expanded = xmalloc (capacity * sizeof (cpp_token *));
1003 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
1004 for (;;)
1006 const cpp_token *token;
1008 if (arg->expanded_count + 1 >= capacity)
1010 capacity *= 2;
1011 arg->expanded = xrealloc (arg->expanded,
1012 capacity * sizeof (cpp_token *));
1015 token = cpp_get_token (pfile);
1017 if (token->type == CPP_EOF)
1018 break;
1020 arg->expanded[arg->expanded_count++] = token;
1023 _cpp_pop_context (pfile);
1025 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
1028 /* Pop the current context off the stack, re-enabling the macro if the
1029 context represented a macro's replacement list. The context
1030 structure is not freed so that we can re-use it later. */
1031 void
1032 _cpp_pop_context (cpp_reader *pfile)
1034 cpp_context *context = pfile->context;
1036 if (context->macro)
1037 context->macro->flags &= ~NODE_DISABLED;
1039 if (context->buff)
1040 _cpp_release_buff (pfile, context->buff);
1042 pfile->context = context->prev;
1045 /* External routine to get a token. Also used nearly everywhere
1046 internally, except for places where we know we can safely call
1047 _cpp_lex_token directly, such as lexing a directive name.
1049 Macro expansions and directives are transparently handled,
1050 including entering included files. Thus tokens are post-macro
1051 expansion, and after any intervening directives. External callers
1052 see CPP_EOF only at EOF. Internal callers also see it when meeting
1053 a directive inside a macro call, when at the end of a directive and
1054 state.in_directive is still 1, and at the end of argument
1055 pre-expansion. */
1056 const cpp_token *
1057 cpp_get_token (cpp_reader *pfile)
1059 const cpp_token *result;
1061 for (;;)
1063 cpp_hashnode *node;
1064 cpp_context *context = pfile->context;
1066 /* Context->prev == 0 <=> base context. */
1067 if (!context->prev)
1068 result = _cpp_lex_token (pfile);
1069 else if (FIRST (context).token != LAST (context).token)
1071 if (context->direct_p)
1072 result = FIRST (context).token++;
1073 else
1074 result = *FIRST (context).ptoken++;
1076 if (result->flags & PASTE_LEFT)
1078 paste_all_tokens (pfile, result);
1079 if (pfile->state.in_directive)
1080 continue;
1081 return padding_token (pfile, result);
1084 else
1086 _cpp_pop_context (pfile);
1087 if (pfile->state.in_directive)
1088 continue;
1089 return &pfile->avoid_paste;
1092 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1093 continue;
1095 if (result->type != CPP_NAME)
1096 break;
1098 node = result->val.node;
1100 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1101 break;
1103 if (!(node->flags & NODE_DISABLED))
1105 if (!pfile->state.prevent_expansion
1106 && enter_macro_context (pfile, node))
1108 if (pfile->state.in_directive)
1109 continue;
1110 return padding_token (pfile, result);
1113 else
1115 /* Flag this token as always unexpandable. FIXME: move this
1116 to collect_args()?. */
1117 cpp_token *t = _cpp_temp_token (pfile);
1118 t->type = result->type;
1119 t->flags = result->flags | NO_EXPAND;
1120 t->val.str = result->val.str;
1121 result = t;
1124 break;
1127 return result;
1130 /* Returns true if we're expanding an object-like macro that was
1131 defined in a system header. Just checks the macro at the top of
1132 the stack. Used for diagnostic suppression. */
1134 cpp_sys_macro_p (cpp_reader *pfile)
1136 cpp_hashnode *node = pfile->context->macro;
1138 return node && node->value.macro && node->value.macro->syshdr;
1141 /* Read each token in, until end of the current file. Directives are
1142 transparently processed. */
1143 void
1144 cpp_scan_nooutput (cpp_reader *pfile)
1146 /* Request a CPP_EOF token at the end of this file, rather than
1147 transparently continuing with the including file. */
1148 pfile->buffer->return_at_eof = true;
1150 pfile->state.discarding_output++;
1151 pfile->state.prevent_expansion++;
1153 if (CPP_OPTION (pfile, traditional))
1154 while (_cpp_read_logical_line_trad (pfile))
1156 else
1157 while (cpp_get_token (pfile)->type != CPP_EOF)
1160 pfile->state.discarding_output--;
1161 pfile->state.prevent_expansion--;
1164 /* Step back one (or more) tokens. Can only step mack more than 1 if
1165 they are from the lexer, and not from macro expansion. */
1166 void
1167 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
1169 if (pfile->context->prev == NULL)
1171 pfile->lookaheads += count;
1172 while (count--)
1174 pfile->cur_token--;
1175 if (pfile->cur_token == pfile->cur_run->base
1176 /* Possible with -fpreprocessed and no leading #line. */
1177 && pfile->cur_run->prev != NULL)
1179 pfile->cur_run = pfile->cur_run->prev;
1180 pfile->cur_token = pfile->cur_run->limit;
1184 else
1186 if (count != 1)
1187 abort ();
1188 if (pfile->context->direct_p)
1189 FIRST (pfile->context).token--;
1190 else
1191 FIRST (pfile->context).ptoken--;
1195 /* #define directive parsing and handling. */
1197 /* Returns nonzero if a macro redefinition warning is required. */
1198 static bool
1199 warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
1200 const cpp_macro *macro2)
1202 const cpp_macro *macro1;
1203 unsigned int i;
1205 /* Some redefinitions need to be warned about regardless. */
1206 if (node->flags & NODE_WARN)
1207 return true;
1209 /* Redefinition of a macro is allowed if and only if the old and new
1210 definitions are the same. (6.10.3 paragraph 2). */
1211 macro1 = node->value.macro;
1213 /* Don't check count here as it can be different in valid
1214 traditional redefinitions with just whitespace differences. */
1215 if (macro1->paramc != macro2->paramc
1216 || macro1->fun_like != macro2->fun_like
1217 || macro1->variadic != macro2->variadic)
1218 return true;
1220 /* Check parameter spellings. */
1221 for (i = 0; i < macro1->paramc; i++)
1222 if (macro1->params[i] != macro2->params[i])
1223 return true;
1225 /* Check the replacement text or tokens. */
1226 if (CPP_OPTION (pfile, traditional))
1227 return _cpp_expansions_different_trad (macro1, macro2);
1229 if (macro1->count != macro2->count)
1230 return true;
1232 for (i = 0; i < macro1->count; i++)
1233 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1234 return true;
1236 return false;
1239 /* Free the definition of hashnode H. */
1240 void
1241 _cpp_free_definition (cpp_hashnode *h)
1243 /* Macros and assertions no longer have anything to free. */
1244 h->type = NT_VOID;
1245 /* Clear builtin flag in case of redefinition. */
1246 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1249 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1250 zero on success, nonzero if the parameter is a duplicate. */
1251 bool
1252 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
1254 unsigned int len;
1255 /* Constraint 6.10.3.6 - duplicate parameter names. */
1256 if (node->flags & NODE_MACRO_ARG)
1258 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
1259 NODE_NAME (node));
1260 return true;
1263 if (BUFF_ROOM (pfile->a_buff)
1264 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1265 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1267 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1268 node->flags |= NODE_MACRO_ARG;
1269 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1270 if (len > pfile->macro_buffer_len)
1272 pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
1273 pfile->macro_buffer_len = len;
1275 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1276 = node->value;
1278 node->value.arg_index = macro->paramc;
1279 return false;
1282 /* Check the syntax of the parameters in a MACRO definition. Returns
1283 false if an error occurs. */
1284 static bool
1285 parse_params (cpp_reader *pfile, cpp_macro *macro)
1287 unsigned int prev_ident = 0;
1289 for (;;)
1291 const cpp_token *token = _cpp_lex_token (pfile);
1293 switch (token->type)
1295 default:
1296 /* Allow/ignore comments in parameter lists if we are
1297 preserving comments in macro expansions. */
1298 if (token->type == CPP_COMMENT
1299 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1300 continue;
1302 cpp_error (pfile, CPP_DL_ERROR,
1303 "\"%s\" may not appear in macro parameter list",
1304 cpp_token_as_text (pfile, token));
1305 return false;
1307 case CPP_NAME:
1308 if (prev_ident)
1310 cpp_error (pfile, CPP_DL_ERROR,
1311 "macro parameters must be comma-separated");
1312 return false;
1314 prev_ident = 1;
1316 if (_cpp_save_parameter (pfile, macro, token->val.node))
1317 return false;
1318 continue;
1320 case CPP_CLOSE_PAREN:
1321 if (prev_ident || macro->paramc == 0)
1322 return true;
1324 /* Fall through to pick up the error. */
1325 case CPP_COMMA:
1326 if (!prev_ident)
1328 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
1329 return false;
1331 prev_ident = 0;
1332 continue;
1334 case CPP_ELLIPSIS:
1335 macro->variadic = 1;
1336 if (!prev_ident)
1338 _cpp_save_parameter (pfile, macro,
1339 pfile->spec_nodes.n__VA_ARGS__);
1340 pfile->state.va_args_ok = 1;
1341 if (! CPP_OPTION (pfile, c99)
1342 && CPP_OPTION (pfile, pedantic)
1343 && CPP_OPTION (pfile, warn_variadic_macros))
1344 cpp_error (pfile, CPP_DL_PEDWARN,
1345 "anonymous variadic macros were introduced in C99");
1347 else if (CPP_OPTION (pfile, pedantic)
1348 && CPP_OPTION (pfile, warn_variadic_macros))
1349 cpp_error (pfile, CPP_DL_PEDWARN,
1350 "ISO C does not permit named variadic macros");
1352 /* We're at the end, and just expect a closing parenthesis. */
1353 token = _cpp_lex_token (pfile);
1354 if (token->type == CPP_CLOSE_PAREN)
1355 return true;
1356 /* Fall through. */
1358 case CPP_EOF:
1359 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
1360 return false;
1365 /* Allocate room for a token from a macro's replacement list. */
1366 static cpp_token *
1367 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1369 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1370 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1372 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1375 /* Lex a token from the expansion of MACRO, but mark parameters as we
1376 find them and warn of traditional stringification. */
1377 static cpp_token *
1378 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1380 cpp_token *token;
1382 pfile->cur_token = alloc_expansion_token (pfile, macro);
1383 token = _cpp_lex_direct (pfile);
1385 /* Is this a parameter? */
1386 if (token->type == CPP_NAME
1387 && (token->val.node->flags & NODE_MACRO_ARG) != 0)
1389 token->type = CPP_MACRO_ARG;
1390 token->val.arg_no = token->val.node->value.arg_index;
1392 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1393 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1394 check_trad_stringification (pfile, macro, &token->val.str);
1396 return token;
1399 static bool
1400 create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
1402 cpp_token *token;
1403 const cpp_token *ctoken;
1405 /* Get the first token of the expansion (or the '(' of a
1406 function-like macro). */
1407 ctoken = _cpp_lex_token (pfile);
1409 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1411 bool ok = parse_params (pfile, macro);
1412 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1413 if (!ok)
1414 return false;
1416 /* Success. Commit or allocate the parameter array. */
1417 if (pfile->hash_table->alloc_subobject)
1419 cpp_token *tokns = pfile->hash_table->alloc_subobject
1420 (sizeof (cpp_token) * macro->paramc);
1421 memcpy (tokns, macro->params, sizeof (cpp_token) * macro->paramc);
1422 macro->params = tokns;
1424 else
1425 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
1426 macro->fun_like = 1;
1428 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1429 cpp_error (pfile, CPP_DL_PEDWARN,
1430 "ISO C requires whitespace after the macro name");
1432 if (macro->fun_like)
1433 token = lex_expansion_token (pfile, macro);
1434 else
1436 token = alloc_expansion_token (pfile, macro);
1437 *token = *ctoken;
1440 for (;;)
1442 /* Check the stringifying # constraint 6.10.3.2.1 of
1443 function-like macros when lexing the subsequent token. */
1444 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1446 if (token->type == CPP_MACRO_ARG)
1448 token->flags &= ~PREV_WHITE;
1449 token->flags |= STRINGIFY_ARG;
1450 token->flags |= token[-1].flags & PREV_WHITE;
1451 token[-1] = token[0];
1452 macro->count--;
1454 /* Let assembler get away with murder. */
1455 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1457 cpp_error (pfile, CPP_DL_ERROR,
1458 "'#' is not followed by a macro parameter");
1459 return false;
1463 if (token->type == CPP_EOF)
1464 break;
1466 /* Paste operator constraint 6.10.3.3.1. */
1467 if (token->type == CPP_PASTE)
1469 /* Token-paste ##, can appear in both object-like and
1470 function-like macros, but not at the ends. */
1471 if (--macro->count > 0)
1472 token = lex_expansion_token (pfile, macro);
1474 if (macro->count == 0 || token->type == CPP_EOF)
1476 cpp_error (pfile, CPP_DL_ERROR,
1477 "'##' cannot appear at either end of a macro expansion");
1478 return false;
1481 token[-1].flags |= PASTE_LEFT;
1484 token = lex_expansion_token (pfile, macro);
1487 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1488 macro->traditional = 0;
1490 /* Don't count the CPP_EOF. */
1491 macro->count--;
1493 /* Clear whitespace on first token for warn_of_redefinition(). */
1494 if (macro->count)
1495 macro->exp.tokens[0].flags &= ~PREV_WHITE;
1497 /* Commit or allocate the memory. */
1498 if (pfile->hash_table->alloc_subobject)
1500 cpp_token *tokns = pfile->hash_table->alloc_subobject (sizeof (cpp_token)
1501 * macro->count);
1502 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
1503 macro->exp.tokens = tokns;
1505 else
1506 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
1508 return true;
1511 /* Parse a macro and save its expansion. Returns nonzero on success. */
1512 bool
1513 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
1515 cpp_macro *macro;
1516 unsigned int i;
1517 bool ok;
1519 if (pfile->hash_table->alloc_subobject)
1520 macro = pfile->hash_table->alloc_subobject (sizeof (cpp_macro));
1521 else
1522 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1523 macro->line = pfile->directive_line;
1524 macro->params = 0;
1525 macro->paramc = 0;
1526 macro->variadic = 0;
1527 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
1528 macro->count = 0;
1529 macro->fun_like = 0;
1530 /* To suppress some diagnostics. */
1531 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
1533 if (CPP_OPTION (pfile, traditional))
1534 ok = _cpp_create_trad_definition (pfile, macro);
1535 else
1537 cpp_token *saved_cur_token = pfile->cur_token;
1539 ok = create_iso_definition (pfile, macro);
1541 /* Restore lexer position because of games lex_expansion_token()
1542 plays lexing the macro. We set the type for SEEN_EOL() in
1543 cpplib.c.
1545 Longer term we should lex the whole line before coming here,
1546 and just copy the expansion. */
1547 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1548 pfile->cur_token = saved_cur_token;
1550 /* Stop the lexer accepting __VA_ARGS__. */
1551 pfile->state.va_args_ok = 0;
1554 /* Clear the fast argument lookup indices. */
1555 for (i = macro->paramc; i-- > 0; )
1557 struct cpp_hashnode *node = macro->params[i];
1558 node->flags &= ~ NODE_MACRO_ARG;
1559 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1562 if (!ok)
1563 return ok;
1565 if (node->type == NT_MACRO)
1567 if (CPP_OPTION (pfile, warn_unused_macros))
1568 _cpp_warn_if_unused_macro (pfile, node, NULL);
1570 if (warn_of_redefinition (pfile, node, macro))
1572 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
1573 "\"%s\" redefined", NODE_NAME (node));
1575 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1576 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1577 node->value.macro->line, 0,
1578 "this is the location of the previous definition");
1582 if (node->type != NT_VOID)
1583 _cpp_free_definition (node);
1585 /* Enter definition in hash table. */
1586 node->type = NT_MACRO;
1587 node->value.macro = macro;
1588 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1589 node->flags |= NODE_WARN;
1591 return ok;
1594 /* Warn if a token in STRING matches one of a function-like MACRO's
1595 parameters. */
1596 static void
1597 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1598 const cpp_string *string)
1600 unsigned int i, len;
1601 const uchar *p, *q, *limit;
1603 /* Loop over the string. */
1604 limit = string->text + string->len - 1;
1605 for (p = string->text + 1; p < limit; p = q)
1607 /* Find the start of an identifier. */
1608 while (p < limit && !is_idstart (*p))
1609 p++;
1611 /* Find the end of the identifier. */
1612 q = p;
1613 while (q < limit && is_idchar (*q))
1614 q++;
1616 len = q - p;
1618 /* Loop over the function macro arguments to see if the
1619 identifier inside the string matches one of them. */
1620 for (i = 0; i < macro->paramc; i++)
1622 const cpp_hashnode *node = macro->params[i];
1624 if (NODE_LEN (node) == len
1625 && !memcmp (p, NODE_NAME (node), len))
1627 cpp_error (pfile, CPP_DL_WARNING,
1628 "macro argument \"%s\" would be stringified in traditional C",
1629 NODE_NAME (node));
1630 break;
1636 /* Returns the name, arguments and expansion of a macro, in a format
1637 suitable to be read back in again, and therefore also for DWARF 2
1638 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1639 Caller is expected to generate the "#define" bit if needed. The
1640 returned text is temporary, and automatically freed later. */
1641 const unsigned char *
1642 cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
1644 unsigned int i, len;
1645 const cpp_macro *macro = node->value.macro;
1646 unsigned char *buffer;
1648 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1650 cpp_error (pfile, CPP_DL_ICE,
1651 "invalid hash type %d in cpp_macro_definition", node->type);
1652 return 0;
1655 /* Calculate length. */
1656 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
1657 if (macro->fun_like)
1659 len += 4; /* "()" plus possible final ".." of named
1660 varargs (we have + 1 below). */
1661 for (i = 0; i < macro->paramc; i++)
1662 len += NODE_LEN (macro->params[i]) + 1; /* "," */
1665 if (CPP_OPTION (pfile, traditional))
1666 len += _cpp_replacement_text_len (macro);
1667 else
1669 for (i = 0; i < macro->count; i++)
1671 cpp_token *token = &macro->exp.tokens[i];
1673 if (token->type == CPP_MACRO_ARG)
1674 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1675 else
1676 len += cpp_token_len (token) + 1; /* Includes room for ' '. */
1677 if (token->flags & STRINGIFY_ARG)
1678 len++; /* "#" */
1679 if (token->flags & PASTE_LEFT)
1680 len += 3; /* " ##" */
1684 if (len > pfile->macro_buffer_len)
1686 pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
1687 pfile->macro_buffer_len = len;
1690 /* Fill in the buffer. Start with the macro name. */
1691 buffer = pfile->macro_buffer;
1692 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1693 buffer += NODE_LEN (node);
1695 /* Parameter names. */
1696 if (macro->fun_like)
1698 *buffer++ = '(';
1699 for (i = 0; i < macro->paramc; i++)
1701 cpp_hashnode *param = macro->params[i];
1703 if (param != pfile->spec_nodes.n__VA_ARGS__)
1705 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1706 buffer += NODE_LEN (param);
1709 if (i + 1 < macro->paramc)
1710 /* Don't emit a space after the comma here; we're trying
1711 to emit a Dwarf-friendly definition, and the Dwarf spec
1712 forbids spaces in the argument list. */
1713 *buffer++ = ',';
1714 else if (macro->variadic)
1715 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1717 *buffer++ = ')';
1720 /* The Dwarf spec requires a space after the macro name, even if the
1721 definition is the empty string. */
1722 *buffer++ = ' ';
1724 if (CPP_OPTION (pfile, traditional))
1725 buffer = _cpp_copy_replacement_text (macro, buffer);
1726 else if (macro->count)
1727 /* Expansion tokens. */
1729 for (i = 0; i < macro->count; i++)
1731 cpp_token *token = &macro->exp.tokens[i];
1733 if (token->flags & PREV_WHITE)
1734 *buffer++ = ' ';
1735 if (token->flags & STRINGIFY_ARG)
1736 *buffer++ = '#';
1738 if (token->type == CPP_MACRO_ARG)
1740 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1741 memcpy (buffer,
1742 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1743 buffer += len;
1745 else
1746 buffer = cpp_spell_token (pfile, token, buffer);
1748 if (token->flags & PASTE_LEFT)
1750 *buffer++ = ' ';
1751 *buffer++ = '#';
1752 *buffer++ = '#';
1753 /* Next has PREV_WHITE; see _cpp_create_definition. */
1758 *buffer = '\0';
1759 return pfile->macro_buffer;