new tests
[official-gcc.git] / libcpp / macro.c
blobeba2349954a3e949bb29b592f0917e08cc0b7151
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, 2005,
4 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Written by Per Bothner, 1994.
6 Based on CCCP program by Paul Rubin, June 1986
7 Adapted to ANSI C, Richard Stallman, Jan 1987
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any
12 later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>.
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding! */
27 #include "config.h"
28 #include "system.h"
29 #include "cpplib.h"
30 #include "internal.h"
32 typedef struct macro_arg macro_arg;
33 struct macro_arg
35 const cpp_token **first; /* First token in unexpanded argument. */
36 const cpp_token **expanded; /* Macro-expanded argument. */
37 const cpp_token *stringified; /* Stringified argument. */
38 unsigned int count; /* # of tokens in argument. */
39 unsigned int expanded_count; /* # of tokens in expanded argument. */
42 /* Macro expansion. */
44 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
45 const cpp_token *);
46 static int builtin_macro (cpp_reader *, cpp_hashnode *);
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 _cpp_buff **);
51 static cpp_context *next_context (cpp_reader *);
52 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
53 static void expand_arg (cpp_reader *, macro_arg *);
54 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
55 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
56 static void paste_all_tokens (cpp_reader *, const cpp_token *);
57 static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
58 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
59 macro_arg *);
60 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
61 _cpp_buff **);
62 static bool create_iso_definition (cpp_reader *, cpp_macro *);
64 /* #define directive parsing and handling. */
66 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
67 static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
68 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
69 const cpp_macro *);
70 static bool parse_params (cpp_reader *, cpp_macro *);
71 static void check_trad_stringification (cpp_reader *, const cpp_macro *,
72 const cpp_string *);
74 /* Emits a warning if NODE is a macro defined in the main file that
75 has not been used. */
76 int
77 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
78 void *v ATTRIBUTE_UNUSED)
80 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
82 cpp_macro *macro = node->value.macro;
84 if (!macro->used
85 && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
86 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
87 "macro \"%s\" is not used", NODE_NAME (node));
90 return 1;
93 /* Allocates and returns a CPP_STRING token, containing TEXT of length
94 LEN, after null-terminating it. TEXT must be in permanent storage. */
95 static const cpp_token *
96 new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
98 cpp_token *token = _cpp_temp_token (pfile);
100 text[len] = '\0';
101 token->type = CPP_STRING;
102 token->val.str.len = len;
103 token->val.str.text = text;
104 token->flags = 0;
105 return token;
108 static const char * const monthnames[] =
110 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
111 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
114 /* Helper function for builtin_macro. Returns the text generated by
115 a builtin macro. */
116 const uchar *
117 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
119 const struct line_map *map;
120 const uchar *result = NULL;
121 linenum_type number = 1;
123 switch (node->value.builtin)
125 default:
126 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
127 NODE_NAME (node));
128 break;
130 case BT_TIMESTAMP:
132 cpp_buffer *pbuffer = cpp_get_buffer (pfile);
133 if (pbuffer->timestamp == NULL)
135 /* Initialize timestamp value of the assotiated file. */
136 struct _cpp_file *file = cpp_get_file (pbuffer);
137 if (file)
139 /* Generate __TIMESTAMP__ string, that represents
140 the date and time of the last modification
141 of the current source file. The string constant
142 looks like "Sun Sep 16 01:03:52 1973". */
143 struct tm *tb = NULL;
144 struct stat *st = _cpp_get_file_stat (file);
145 if (st)
146 tb = localtime (&st->st_mtime);
147 if (tb)
149 char *str = asctime (tb);
150 size_t len = strlen (str);
151 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
152 buf[0] = '"';
153 strcpy ((char *) buf + 1, str);
154 buf[len] = '"';
155 pbuffer->timestamp = buf;
157 else
159 cpp_errno (pfile, CPP_DL_WARNING,
160 "could not determine file timestamp");
161 pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
165 result = pbuffer->timestamp;
167 break;
168 case BT_FILE:
169 case BT_BASE_FILE:
171 unsigned int len;
172 const char *name;
173 uchar *buf;
174 map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
176 if (node->value.builtin == BT_BASE_FILE)
177 while (! MAIN_FILE_P (map))
178 map = INCLUDED_FROM (pfile->line_table, map);
180 name = map->to_file;
181 len = strlen (name);
182 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
183 result = buf;
184 *buf = '"';
185 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
186 *buf++ = '"';
187 *buf = '\0';
189 break;
191 case BT_INCLUDE_LEVEL:
192 /* The line map depth counts the primary source as level 1, but
193 historically __INCLUDE_DEPTH__ has called the primary source
194 level 0. */
195 number = pfile->line_table->depth - 1;
196 break;
198 case BT_SPECLINE:
199 map = &pfile->line_table->maps[pfile->line_table->used-1];
200 /* If __LINE__ is embedded in a macro, it must expand to the
201 line of the macro's invocation, not its definition.
202 Otherwise things like assert() will not work properly. */
203 number = SOURCE_LINE (map,
204 CPP_OPTION (pfile, traditional)
205 ? pfile->line_table->highest_line
206 : pfile->cur_token[-1].src_loc);
207 break;
209 /* __STDC__ has the value 1 under normal circumstances.
210 However, if (a) we are in a system header, (b) the option
211 stdc_0_in_system_headers is true (set by target config), and
212 (c) we are not in strictly conforming mode, then it has the
213 value 0. (b) and (c) are already checked in cpp_init_builtins. */
214 case BT_STDC:
215 if (cpp_in_system_header (pfile))
216 number = 0;
217 else
218 number = 1;
219 break;
221 case BT_DATE:
222 case BT_TIME:
223 if (pfile->date == NULL)
225 /* Allocate __DATE__ and __TIME__ strings from permanent
226 storage. We only do this once, and don't generate them
227 at init time, because time() and localtime() are very
228 slow on some systems. */
229 time_t tt;
230 struct tm *tb = NULL;
232 /* (time_t) -1 is a legitimate value for "number of seconds
233 since the Epoch", so we have to do a little dance to
234 distinguish that from a genuine error. */
235 errno = 0;
236 tt = time(NULL);
237 if (tt != (time_t)-1 || errno == 0)
238 tb = localtime (&tt);
240 if (tb)
242 pfile->date = _cpp_unaligned_alloc (pfile,
243 sizeof ("\"Oct 11 1347\""));
244 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
245 monthnames[tb->tm_mon], tb->tm_mday,
246 tb->tm_year + 1900);
248 pfile->time = _cpp_unaligned_alloc (pfile,
249 sizeof ("\"12:34:56\""));
250 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
251 tb->tm_hour, tb->tm_min, tb->tm_sec);
253 else
255 cpp_errno (pfile, CPP_DL_WARNING,
256 "could not determine date and time");
258 pfile->date = UC"\"??? ?? ????\"";
259 pfile->time = UC"\"??:??:??\"";
263 if (node->value.builtin == BT_DATE)
264 result = pfile->date;
265 else
266 result = pfile->time;
267 break;
269 case BT_COUNTER:
270 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
271 cpp_error (pfile, CPP_DL_ERROR,
272 "__COUNTER__ expanded inside directive with -fdirectives-only");
273 number = pfile->counter++;
274 break;
277 if (result == NULL)
279 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
280 result = _cpp_unaligned_alloc (pfile, 21);
281 sprintf ((char *) result, "%u", number);
284 return result;
287 /* Convert builtin macros like __FILE__ to a token and push it on the
288 context stack. Also handles _Pragma, for which a new token may not
289 be created. Returns 1 if it generates a new token context, 0 to
290 return the token to the caller. */
291 static int
292 builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
294 const uchar *buf;
295 size_t len;
296 char *nbuf;
298 if (node->value.builtin == BT_PRAGMA)
300 /* Don't interpret _Pragma within directives. The standard is
301 not clear on this, but to me this makes most sense. */
302 if (pfile->state.in_directive)
303 return 0;
305 return _cpp_do__Pragma (pfile);
308 buf = _cpp_builtin_macro_text (pfile, node);
309 len = ustrlen (buf);
310 nbuf = (char *) alloca (len + 1);
311 memcpy (nbuf, buf, len);
312 nbuf[len]='\n';
314 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
315 _cpp_clean_line (pfile);
317 /* Set pfile->cur_token as required by _cpp_lex_direct. */
318 pfile->cur_token = _cpp_temp_token (pfile);
319 _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
320 if (pfile->buffer->cur != pfile->buffer->rlimit)
321 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
322 NODE_NAME (node));
323 _cpp_pop_buffer (pfile);
325 return 1;
328 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
329 backslashes and double quotes. DEST must be of sufficient size.
330 Returns a pointer to the end of the string. */
331 uchar *
332 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
334 while (len--)
336 uchar c = *src++;
338 if (c == '\\' || c == '"')
340 *dest++ = '\\';
341 *dest++ = c;
343 else
344 *dest++ = c;
347 return dest;
350 /* Convert a token sequence ARG to a single string token according to
351 the rules of the ISO C #-operator. */
352 static const cpp_token *
353 stringify_arg (cpp_reader *pfile, macro_arg *arg)
355 unsigned char *dest;
356 unsigned int i, escape_it, backslash_count = 0;
357 const cpp_token *source = NULL;
358 size_t len;
360 if (BUFF_ROOM (pfile->u_buff) < 3)
361 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
362 dest = BUFF_FRONT (pfile->u_buff);
363 *dest++ = '"';
365 /* Loop, reading in the argument's tokens. */
366 for (i = 0; i < arg->count; i++)
368 const cpp_token *token = arg->first[i];
370 if (token->type == CPP_PADDING)
372 if (source == NULL
373 || (!(source->flags & PREV_WHITE)
374 && token->val.source == NULL))
375 source = token->val.source;
376 continue;
379 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
380 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
381 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
382 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
383 || token->type == CPP_UTF8STRING);
385 /* Room for each char being written in octal, initial space and
386 final quote and NUL. */
387 len = cpp_token_len (token);
388 if (escape_it)
389 len *= 4;
390 len += 3;
392 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
394 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
395 _cpp_extend_buff (pfile, &pfile->u_buff, len);
396 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
399 /* Leading white space? */
400 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
402 if (source == NULL)
403 source = token;
404 if (source->flags & PREV_WHITE)
405 *dest++ = ' ';
407 source = NULL;
409 if (escape_it)
411 _cpp_buff *buff = _cpp_get_buff (pfile, len);
412 unsigned char *buf = BUFF_FRONT (buff);
413 len = cpp_spell_token (pfile, token, buf, true) - buf;
414 dest = cpp_quote_string (dest, buf, len);
415 _cpp_release_buff (pfile, buff);
417 else
418 dest = cpp_spell_token (pfile, token, dest, true);
420 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
421 backslash_count++;
422 else
423 backslash_count = 0;
426 /* Ignore the final \ of invalid string literals. */
427 if (backslash_count & 1)
429 cpp_error (pfile, CPP_DL_WARNING,
430 "invalid string literal, ignoring final '\\'");
431 dest--;
434 /* Commit the memory, including NUL, and return the token. */
435 *dest++ = '"';
436 len = dest - BUFF_FRONT (pfile->u_buff);
437 BUFF_FRONT (pfile->u_buff) = dest + 1;
438 return new_string_token (pfile, dest - len, len);
441 /* Try to paste two tokens. On success, return nonzero. In any
442 case, PLHS is updated to point to the pasted token, which is
443 guaranteed to not have the PASTE_LEFT flag set. */
444 static bool
445 paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
447 unsigned char *buf, *end, *lhsend;
448 cpp_token *lhs;
449 unsigned int len;
451 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
452 buf = (unsigned char *) alloca (len);
453 end = lhsend = cpp_spell_token (pfile, *plhs, buf, false);
455 /* Avoid comment headers, since they are still processed in stage 3.
456 It is simpler to insert a space here, rather than modifying the
457 lexer to ignore comments in some circumstances. Simply returning
458 false doesn't work, since we want to clear the PASTE_LEFT flag. */
459 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
460 *end++ = ' ';
461 /* In one obscure case we might see padding here. */
462 if (rhs->type != CPP_PADDING)
463 end = cpp_spell_token (pfile, rhs, end, false);
464 *end = '\n';
466 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
467 _cpp_clean_line (pfile);
469 /* Set pfile->cur_token as required by _cpp_lex_direct. */
470 pfile->cur_token = _cpp_temp_token (pfile);
471 lhs = _cpp_lex_direct (pfile);
472 if (pfile->buffer->cur != pfile->buffer->rlimit)
474 source_location saved_loc = lhs->src_loc;
476 _cpp_pop_buffer (pfile);
477 _cpp_backup_tokens (pfile, 1);
478 *lhsend = '\0';
480 /* We have to remove the PASTE_LEFT flag from the old lhs, but
481 we want to keep the new location. */
482 *lhs = **plhs;
483 *plhs = lhs;
484 lhs->src_loc = saved_loc;
485 lhs->flags &= ~PASTE_LEFT;
487 /* Mandatory error for all apart from assembler. */
488 if (CPP_OPTION (pfile, lang) != CLK_ASM)
489 cpp_error (pfile, CPP_DL_ERROR,
490 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
491 buf, cpp_token_as_text (pfile, rhs));
492 return false;
495 *plhs = lhs;
496 _cpp_pop_buffer (pfile);
497 return true;
500 /* Handles an arbitrarily long sequence of ## operators, with initial
501 operand LHS. This implementation is left-associative,
502 non-recursive, and finishes a paste before handling succeeding
503 ones. If a paste fails, we back up to the RHS of the failing ##
504 operator before pushing the context containing the result of prior
505 successful pastes, with the effect that the RHS appears in the
506 output stream after the pasted LHS normally. */
507 static void
508 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
510 const cpp_token *rhs;
511 cpp_context *context = pfile->context;
515 /* Take the token directly from the current context. We can do
516 this, because we are in the replacement list of either an
517 object-like macro, or a function-like macro with arguments
518 inserted. In either case, the constraints to #define
519 guarantee we have at least one more token. */
520 if (context->direct_p)
521 rhs = FIRST (context).token++;
522 else
523 rhs = *FIRST (context).ptoken++;
525 if (rhs->type == CPP_PADDING)
527 if (rhs->flags & PASTE_LEFT)
528 abort ();
530 if (!paste_tokens (pfile, &lhs, rhs))
531 break;
533 while (rhs->flags & PASTE_LEFT);
535 /* Put the resulting token in its own context. */
536 _cpp_push_token_context (pfile, NULL, lhs, 1);
539 /* Returns TRUE if the number of arguments ARGC supplied in an
540 invocation of the MACRO referenced by NODE is valid. An empty
541 invocation to a macro with no parameters should pass ARGC as zero.
543 Note that MACRO cannot necessarily be deduced from NODE, in case
544 NODE was redefined whilst collecting arguments. */
545 bool
546 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
548 if (argc == macro->paramc)
549 return true;
551 if (argc < macro->paramc)
553 /* As an extension, a rest argument is allowed to not appear in
554 the invocation at all.
555 e.g. #define debug(format, args...) something
556 debug("string");
558 This is exactly the same as if there had been an empty rest
559 argument - debug("string", ). */
561 if (argc + 1 == macro->paramc && macro->variadic)
563 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
564 cpp_error (pfile, CPP_DL_PEDWARN,
565 "ISO C99 requires rest arguments to be used");
566 return true;
569 cpp_error (pfile, CPP_DL_ERROR,
570 "macro \"%s\" requires %u arguments, but only %u given",
571 NODE_NAME (node), macro->paramc, argc);
573 else
574 cpp_error (pfile, CPP_DL_ERROR,
575 "macro \"%s\" passed %u arguments, but takes just %u",
576 NODE_NAME (node), argc, macro->paramc);
578 return false;
581 /* Reads and returns the arguments to a function-like macro
582 invocation. Assumes the opening parenthesis has been processed.
583 If there is an error, emits an appropriate diagnostic and returns
584 NULL. Each argument is terminated by a CPP_EOF token, for the
585 future benefit of expand_arg(). If there are any deferred
586 #pragma directives among macro arguments, store pointers to the
587 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer. */
588 static _cpp_buff *
589 collect_args (cpp_reader *pfile, const cpp_hashnode *node,
590 _cpp_buff **pragma_buff)
592 _cpp_buff *buff, *base_buff;
593 cpp_macro *macro;
594 macro_arg *args, *arg;
595 const cpp_token *token;
596 unsigned int argc;
598 macro = node->value.macro;
599 if (macro->paramc)
600 argc = macro->paramc;
601 else
602 argc = 1;
603 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
604 + sizeof (macro_arg)));
605 base_buff = buff;
606 args = (macro_arg *) buff->base;
607 memset (args, 0, argc * sizeof (macro_arg));
608 buff->cur = (unsigned char *) &args[argc];
609 arg = args, argc = 0;
611 /* Collect the tokens making up each argument. We don't yet know
612 how many arguments have been supplied, whether too many or too
613 few. Hence the slightly bizarre usage of "argc" and "arg". */
616 unsigned int paren_depth = 0;
617 unsigned int ntokens = 0;
619 argc++;
620 arg->first = (const cpp_token **) buff->cur;
622 for (;;)
624 /* Require space for 2 new tokens (including a CPP_EOF). */
625 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
627 buff = _cpp_append_extend_buff (pfile, buff,
628 1000 * sizeof (cpp_token *));
629 arg->first = (const cpp_token **) buff->cur;
632 token = cpp_get_token (pfile);
634 if (token->type == CPP_PADDING)
636 /* Drop leading padding. */
637 if (ntokens == 0)
638 continue;
640 else if (token->type == CPP_OPEN_PAREN)
641 paren_depth++;
642 else if (token->type == CPP_CLOSE_PAREN)
644 if (paren_depth-- == 0)
645 break;
647 else if (token->type == CPP_COMMA)
649 /* A comma does not terminate an argument within
650 parentheses or as part of a variable argument. */
651 if (paren_depth == 0
652 && ! (macro->variadic && argc == macro->paramc))
653 break;
655 else if (token->type == CPP_EOF
656 || (token->type == CPP_HASH && token->flags & BOL))
657 break;
658 else if (token->type == CPP_PRAGMA)
660 cpp_token *newtok = _cpp_temp_token (pfile);
662 /* CPP_PRAGMA token lives in directive_result, which will
663 be overwritten on the next directive. */
664 *newtok = *token;
665 token = newtok;
668 if (*pragma_buff == NULL
669 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
671 _cpp_buff *next;
672 if (*pragma_buff == NULL)
673 *pragma_buff
674 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
675 else
677 next = *pragma_buff;
678 *pragma_buff
679 = _cpp_get_buff (pfile,
680 (BUFF_FRONT (*pragma_buff)
681 - (*pragma_buff)->base) * 2);
682 (*pragma_buff)->next = next;
685 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
686 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
687 if (token->type == CPP_PRAGMA_EOL)
688 break;
689 token = cpp_get_token (pfile);
691 while (token->type != CPP_EOF);
693 /* In deferred pragmas parsing_args and prevent_expansion
694 had been changed, reset it. */
695 pfile->state.parsing_args = 2;
696 pfile->state.prevent_expansion = 1;
698 if (token->type == CPP_EOF)
699 break;
700 else
701 continue;
704 arg->first[ntokens++] = token;
707 /* Drop trailing padding. */
708 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
709 ntokens--;
711 arg->count = ntokens;
712 arg->first[ntokens] = &pfile->eof;
714 /* Terminate the argument. Excess arguments loop back and
715 overwrite the final legitimate argument, before failing. */
716 if (argc <= macro->paramc)
718 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
719 if (argc != macro->paramc)
720 arg++;
723 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
725 if (token->type == CPP_EOF)
727 /* We still need the CPP_EOF to end directives, and to end
728 pre-expansion of a macro argument. Step back is not
729 unconditional, since we don't want to return a CPP_EOF to our
730 callers at the end of an -include-d file. */
731 if (pfile->context->prev || pfile->state.in_directive)
732 _cpp_backup_tokens (pfile, 1);
733 cpp_error (pfile, CPP_DL_ERROR,
734 "unterminated argument list invoking macro \"%s\"",
735 NODE_NAME (node));
737 else
739 /* A single empty argument is counted as no argument. */
740 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
741 argc = 0;
742 if (_cpp_arguments_ok (pfile, macro, node, argc))
744 /* GCC has special semantics for , ## b where b is a varargs
745 parameter: we remove the comma if b was omitted entirely.
746 If b was merely an empty argument, the comma is retained.
747 If the macro takes just one (varargs) parameter, then we
748 retain the comma only if we are standards conforming.
750 If FIRST is NULL replace_args () swallows the comma. */
751 if (macro->variadic && (argc < macro->paramc
752 || (argc == 1 && args[0].count == 0
753 && !CPP_OPTION (pfile, std))))
754 args[macro->paramc - 1].first = NULL;
755 return base_buff;
759 /* An error occurred. */
760 _cpp_release_buff (pfile, base_buff);
761 return NULL;
764 /* Search for an opening parenthesis to the macro of NODE, in such a
765 way that, if none is found, we don't lose the information in any
766 intervening padding tokens. If we find the parenthesis, collect
767 the arguments and return the buffer containing them. PRAGMA_BUFF
768 argument is the same as in collect_args. */
769 static _cpp_buff *
770 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
771 _cpp_buff **pragma_buff)
773 const cpp_token *token, *padding = NULL;
775 for (;;)
777 token = cpp_get_token (pfile);
778 if (token->type != CPP_PADDING)
779 break;
780 if (padding == NULL
781 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
782 padding = token;
785 if (token->type == CPP_OPEN_PAREN)
787 pfile->state.parsing_args = 2;
788 return collect_args (pfile, node, pragma_buff);
791 /* CPP_EOF can be the end of macro arguments, or the end of the
792 file. We mustn't back up over the latter. Ugh. */
793 if (token->type != CPP_EOF || token == &pfile->eof)
795 /* Back up. We may have skipped padding, in which case backing
796 up more than one token when expanding macros is in general
797 too difficult. We re-insert it in its own context. */
798 _cpp_backup_tokens (pfile, 1);
799 if (padding)
800 _cpp_push_token_context (pfile, NULL, padding, 1);
803 return NULL;
806 /* Return the real number of tokens in the expansion of MACRO. */
807 static inline unsigned int
808 macro_real_token_count (const cpp_macro *macro)
810 unsigned int i;
811 if (__builtin_expect (!macro->extra_tokens, true))
812 return macro->count;
813 for (i = 0; i < macro->count; i++)
814 if (macro->exp.tokens[i].type == CPP_PASTE)
815 return i;
816 abort ();
819 /* Push the context of a macro with hash entry NODE onto the context
820 stack. If we can successfully expand the macro, we push a context
821 containing its yet-to-be-rescanned replacement list and return one.
822 If there were additionally any unexpanded deferred #pragma directives
823 among macro arguments, push another context containing the
824 pragma tokens before the yet-to-be-rescanned replacement list
825 and return two. Otherwise, we don't push a context and return zero. */
826 static int
827 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
828 const cpp_token *result)
830 /* The presence of a macro invalidates a file's controlling macro. */
831 pfile->mi_valid = false;
833 pfile->state.angled_headers = false;
835 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
837 node->flags |= NODE_USED;
838 if ((!pfile->cb.user_builtin_macro
839 || !pfile->cb.user_builtin_macro (pfile, node))
840 && pfile->cb.used_define)
841 pfile->cb.used_define (pfile, pfile->directive_line, node);
844 /* Handle standard macros. */
845 if (! (node->flags & NODE_BUILTIN))
847 cpp_macro *macro = node->value.macro;
848 _cpp_buff *pragma_buff = NULL;
850 if (macro->fun_like)
852 _cpp_buff *buff;
854 pfile->state.prevent_expansion++;
855 pfile->keep_tokens++;
856 pfile->state.parsing_args = 1;
857 buff = funlike_invocation_p (pfile, node, &pragma_buff);
858 pfile->state.parsing_args = 0;
859 pfile->keep_tokens--;
860 pfile->state.prevent_expansion--;
862 if (buff == NULL)
864 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
865 cpp_warning (pfile, CPP_W_TRADITIONAL,
866 "function-like macro \"%s\" must be used with arguments in traditional C",
867 NODE_NAME (node));
869 if (pragma_buff)
870 _cpp_release_buff (pfile, pragma_buff);
872 return 0;
875 if (macro->paramc > 0)
876 replace_args (pfile, node, macro, (macro_arg *) buff->base);
877 _cpp_release_buff (pfile, buff);
880 /* Disable the macro within its expansion. */
881 node->flags |= NODE_DISABLED;
883 if (!(node->flags & NODE_USED))
885 node->flags |= NODE_USED;
886 if (pfile->cb.used_define)
887 pfile->cb.used_define (pfile, pfile->directive_line, node);
890 if (pfile->cb.used)
891 pfile->cb.used (pfile, result->src_loc, node);
893 macro->used = 1;
895 if (macro->paramc == 0)
896 _cpp_push_token_context (pfile, node, macro->exp.tokens,
897 macro_real_token_count (macro));
899 if (pragma_buff)
901 if (!pfile->state.in_directive)
902 _cpp_push_token_context (pfile, NULL,
903 padding_token (pfile, result), 1);
906 _cpp_buff *tail = pragma_buff->next;
907 pragma_buff->next = NULL;
908 push_ptoken_context (pfile, NULL, pragma_buff,
909 (const cpp_token **) pragma_buff->base,
910 ((const cpp_token **) BUFF_FRONT (pragma_buff)
911 - (const cpp_token **) pragma_buff->base));
912 pragma_buff = tail;
914 while (pragma_buff != NULL);
915 return 2;
918 return 1;
921 /* Handle built-in macros and the _Pragma operator. */
922 return builtin_macro (pfile, node);
925 /* Replace the parameters in a function-like macro of NODE with the
926 actual ARGS, and place the result in a newly pushed token context.
927 Expand each argument before replacing, unless it is operated upon
928 by the # or ## operators. */
929 static void
930 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
932 unsigned int i, total;
933 const cpp_token *src, *limit;
934 const cpp_token **dest, **first;
935 macro_arg *arg;
936 _cpp_buff *buff;
937 unsigned int count;
939 /* First, fully macro-expand arguments, calculating the number of
940 tokens in the final expansion as we go. The ordering of the if
941 statements below is subtle; we must handle stringification before
942 pasting. */
943 count = macro_real_token_count (macro);
944 total = count;
945 limit = macro->exp.tokens + count;
947 for (src = macro->exp.tokens; src < limit; src++)
948 if (src->type == CPP_MACRO_ARG)
950 /* Leading and trailing padding tokens. */
951 total += 2;
953 /* We have an argument. If it is not being stringified or
954 pasted it is macro-replaced before insertion. */
955 arg = &args[src->val.macro_arg.arg_no - 1];
957 if (src->flags & STRINGIFY_ARG)
959 if (!arg->stringified)
960 arg->stringified = stringify_arg (pfile, arg);
962 else if ((src->flags & PASTE_LEFT)
963 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
964 total += arg->count - 1;
965 else
967 if (!arg->expanded)
968 expand_arg (pfile, arg);
969 total += arg->expanded_count - 1;
973 /* Now allocate space for the expansion, copy the tokens and replace
974 the arguments. */
975 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
976 first = (const cpp_token **) buff->base;
977 dest = first;
979 for (src = macro->exp.tokens; src < limit; src++)
981 unsigned int count;
982 const cpp_token **from, **paste_flag;
984 if (src->type != CPP_MACRO_ARG)
986 *dest++ = src;
987 continue;
990 paste_flag = 0;
991 arg = &args[src->val.macro_arg.arg_no - 1];
992 if (src->flags & STRINGIFY_ARG)
993 count = 1, from = &arg->stringified;
994 else if (src->flags & PASTE_LEFT)
995 count = arg->count, from = arg->first;
996 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
998 count = arg->count, from = arg->first;
999 if (dest != first)
1001 if (dest[-1]->type == CPP_COMMA
1002 && macro->variadic
1003 && src->val.macro_arg.arg_no == macro->paramc)
1005 /* Swallow a pasted comma if from == NULL, otherwise
1006 drop the paste flag. */
1007 if (from == NULL)
1008 dest--;
1009 else
1010 paste_flag = dest - 1;
1012 /* Remove the paste flag if the RHS is a placemarker. */
1013 else if (count == 0)
1014 paste_flag = dest - 1;
1017 else
1018 count = arg->expanded_count, from = arg->expanded;
1020 /* Padding on the left of an argument (unless RHS of ##). */
1021 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
1022 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1023 *dest++ = padding_token (pfile, src);
1025 if (count)
1027 memcpy (dest, from, count * sizeof (cpp_token *));
1028 dest += count;
1030 /* With a non-empty argument on the LHS of ##, the last
1031 token should be flagged PASTE_LEFT. */
1032 if (src->flags & PASTE_LEFT)
1033 paste_flag = dest - 1;
1035 else if (CPP_PEDANTIC (pfile) && ! macro->syshdr
1036 && ! CPP_OPTION (pfile, c99)
1037 && ! cpp_in_system_header (pfile))
1039 cpp_error (pfile, CPP_DL_PEDWARN,
1040 "invoking macro %s argument %d: "
1041 "empty macro arguments are undefined"
1042 " in ISO C90 and ISO C++98",
1043 NODE_NAME (node),
1044 src->val.macro_arg.arg_no);
1047 /* Avoid paste on RHS (even case count == 0). */
1048 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
1049 *dest++ = &pfile->avoid_paste;
1051 /* Add a new paste flag, or remove an unwanted one. */
1052 if (paste_flag)
1054 cpp_token *token = _cpp_temp_token (pfile);
1055 token->type = (*paste_flag)->type;
1056 token->val = (*paste_flag)->val;
1057 if (src->flags & PASTE_LEFT)
1058 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1059 else
1060 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1061 *paste_flag = token;
1065 /* Free the expanded arguments. */
1066 for (i = 0; i < macro->paramc; i++)
1067 free (args[i].expanded);
1069 push_ptoken_context (pfile, node, buff, first, dest - first);
1072 /* Return a special padding token, with padding inherited from SOURCE. */
1073 static const cpp_token *
1074 padding_token (cpp_reader *pfile, const cpp_token *source)
1076 cpp_token *result = _cpp_temp_token (pfile);
1078 result->type = CPP_PADDING;
1080 /* Data in GCed data structures cannot be made const so far, so we
1081 need a cast here. */
1082 result->val.source = (cpp_token *) source;
1083 result->flags = 0;
1084 return result;
1087 /* Get a new uninitialized context. Create a new one if we cannot
1088 re-use an old one. */
1089 static cpp_context *
1090 next_context (cpp_reader *pfile)
1092 cpp_context *result = pfile->context->next;
1094 if (result == 0)
1096 result = XNEW (cpp_context);
1097 result->prev = pfile->context;
1098 result->next = 0;
1099 pfile->context->next = result;
1102 pfile->context = result;
1103 return result;
1106 /* Push a list of pointers to tokens. */
1107 static void
1108 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1109 const cpp_token **first, unsigned int count)
1111 cpp_context *context = next_context (pfile);
1113 context->direct_p = false;
1114 context->macro = macro;
1115 context->buff = buff;
1116 FIRST (context).ptoken = first;
1117 LAST (context).ptoken = first + count;
1120 /* Push a list of tokens. */
1121 void
1122 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1123 const cpp_token *first, unsigned int count)
1125 cpp_context *context = next_context (pfile);
1127 context->direct_p = true;
1128 context->macro = macro;
1129 context->buff = NULL;
1130 FIRST (context).token = first;
1131 LAST (context).token = first + count;
1134 /* Push a traditional macro's replacement text. */
1135 void
1136 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
1137 const uchar *start, size_t len)
1139 cpp_context *context = next_context (pfile);
1141 context->direct_p = true;
1142 context->macro = macro;
1143 context->buff = NULL;
1144 CUR (context) = start;
1145 RLIMIT (context) = start + len;
1146 macro->flags |= NODE_DISABLED;
1149 /* Expand an argument ARG before replacing parameters in a
1150 function-like macro. This works by pushing a context with the
1151 argument's tokens, and then expanding that into a temporary buffer
1152 as if it were a normal part of the token stream. collect_args()
1153 has terminated the argument's tokens with a CPP_EOF so that we know
1154 when we have fully expanded the argument. */
1155 static void
1156 expand_arg (cpp_reader *pfile, macro_arg *arg)
1158 unsigned int capacity;
1159 bool saved_warn_trad;
1161 if (arg->count == 0)
1162 return;
1164 /* Don't warn about funlike macros when pre-expanding. */
1165 saved_warn_trad = CPP_WTRADITIONAL (pfile);
1166 CPP_WTRADITIONAL (pfile) = 0;
1168 /* Loop, reading in the arguments. */
1169 capacity = 256;
1170 arg->expanded = XNEWVEC (const cpp_token *, capacity);
1172 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
1173 for (;;)
1175 const cpp_token *token;
1177 if (arg->expanded_count + 1 >= capacity)
1179 capacity *= 2;
1180 arg->expanded = XRESIZEVEC (const cpp_token *, arg->expanded,
1181 capacity);
1184 token = cpp_get_token (pfile);
1186 if (token->type == CPP_EOF)
1187 break;
1189 arg->expanded[arg->expanded_count++] = token;
1192 _cpp_pop_context (pfile);
1194 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
1197 /* Pop the current context off the stack, re-enabling the macro if the
1198 context represented a macro's replacement list. The context
1199 structure is not freed so that we can re-use it later. */
1200 void
1201 _cpp_pop_context (cpp_reader *pfile)
1203 cpp_context *context = pfile->context;
1205 if (context->macro)
1206 context->macro->flags &= ~NODE_DISABLED;
1208 if (context->buff)
1209 _cpp_release_buff (pfile, context->buff);
1211 pfile->context = context->prev;
1214 /* External routine to get a token. Also used nearly everywhere
1215 internally, except for places where we know we can safely call
1216 _cpp_lex_token directly, such as lexing a directive name.
1218 Macro expansions and directives are transparently handled,
1219 including entering included files. Thus tokens are post-macro
1220 expansion, and after any intervening directives. External callers
1221 see CPP_EOF only at EOF. Internal callers also see it when meeting
1222 a directive inside a macro call, when at the end of a directive and
1223 state.in_directive is still 1, and at the end of argument
1224 pre-expansion. */
1225 const cpp_token *
1226 cpp_get_token (cpp_reader *pfile)
1228 const cpp_token *result;
1229 bool can_set = pfile->set_invocation_location;
1230 pfile->set_invocation_location = false;
1232 for (;;)
1234 cpp_hashnode *node;
1235 cpp_context *context = pfile->context;
1237 /* Context->prev == 0 <=> base context. */
1238 if (!context->prev)
1239 result = _cpp_lex_token (pfile);
1240 else if (FIRST (context).token != LAST (context).token)
1242 if (context->direct_p)
1243 result = FIRST (context).token++;
1244 else
1245 result = *FIRST (context).ptoken++;
1247 if (result->flags & PASTE_LEFT)
1249 paste_all_tokens (pfile, result);
1250 if (pfile->state.in_directive)
1251 continue;
1252 return padding_token (pfile, result);
1255 else
1257 _cpp_pop_context (pfile);
1258 if (pfile->state.in_directive)
1259 continue;
1260 return &pfile->avoid_paste;
1263 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1264 continue;
1266 if (result->type != CPP_NAME)
1267 break;
1269 node = result->val.node.node;
1271 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1272 break;
1274 if (!(node->flags & NODE_DISABLED))
1276 int ret = 0;
1277 /* If not in a macro context, and we're going to start an
1278 expansion, record the location. */
1279 if (can_set && !context->macro)
1280 pfile->invocation_location = result->src_loc;
1281 if (pfile->state.prevent_expansion)
1282 break;
1284 /* Conditional macros require that a predicate be evaluated
1285 first. */
1286 if ((node->flags & NODE_CONDITIONAL) != 0)
1288 if (pfile->cb.macro_to_expand)
1290 bool whitespace_after;
1291 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
1293 whitespace_after = (peek_tok->type == CPP_PADDING
1294 || (peek_tok->flags & PREV_WHITE));
1295 node = pfile->cb.macro_to_expand (pfile, result);
1296 if (node)
1297 ret = enter_macro_context (pfile, node, result);
1298 else if (whitespace_after)
1300 /* If macro_to_expand hook returned NULL and it
1301 ate some tokens, see if we don't need to add
1302 a padding token in between this and the
1303 next token. */
1304 peek_tok = cpp_peek_token (pfile, 0);
1305 if (peek_tok->type != CPP_PADDING
1306 && (peek_tok->flags & PREV_WHITE) == 0)
1307 _cpp_push_token_context (pfile, NULL,
1308 padding_token (pfile,
1309 peek_tok), 1);
1313 else
1314 ret = enter_macro_context (pfile, node, result);
1315 if (ret)
1317 if (pfile->state.in_directive || ret == 2)
1318 continue;
1319 return padding_token (pfile, result);
1322 else
1324 /* Flag this token as always unexpandable. FIXME: move this
1325 to collect_args()?. */
1326 cpp_token *t = _cpp_temp_token (pfile);
1327 t->type = result->type;
1328 t->flags = result->flags | NO_EXPAND;
1329 t->val = result->val;
1330 result = t;
1333 break;
1336 return result;
1339 /* Like cpp_get_token, but also returns a location separate from the
1340 one provided by the returned token. LOC is an out parameter; *LOC
1341 is set to the location "as expected by the user". This matters
1342 when a token results from macro expansion -- the token's location
1343 will indicate where the macro is defined, but *LOC will be the
1344 location of the start of the expansion. */
1345 const cpp_token *
1346 cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
1348 const cpp_token *result;
1350 pfile->set_invocation_location = true;
1351 result = cpp_get_token (pfile);
1352 if (pfile->context->macro)
1353 *loc = pfile->invocation_location;
1354 else
1355 *loc = result->src_loc;
1357 return result;
1360 /* Returns true if we're expanding an object-like macro that was
1361 defined in a system header. Just checks the macro at the top of
1362 the stack. Used for diagnostic suppression. */
1364 cpp_sys_macro_p (cpp_reader *pfile)
1366 cpp_hashnode *node = pfile->context->macro;
1368 return node && node->value.macro && node->value.macro->syshdr;
1371 /* Read each token in, until end of the current file. Directives are
1372 transparently processed. */
1373 void
1374 cpp_scan_nooutput (cpp_reader *pfile)
1376 /* Request a CPP_EOF token at the end of this file, rather than
1377 transparently continuing with the including file. */
1378 pfile->buffer->return_at_eof = true;
1380 pfile->state.discarding_output++;
1381 pfile->state.prevent_expansion++;
1383 if (CPP_OPTION (pfile, traditional))
1384 while (_cpp_read_logical_line_trad (pfile))
1386 else
1387 while (cpp_get_token (pfile)->type != CPP_EOF)
1390 pfile->state.discarding_output--;
1391 pfile->state.prevent_expansion--;
1394 /* Step back one or more tokens obtained from the lexer. */
1395 void
1396 _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
1398 pfile->lookaheads += count;
1399 while (count--)
1401 pfile->cur_token--;
1402 if (pfile->cur_token == pfile->cur_run->base
1403 /* Possible with -fpreprocessed and no leading #line. */
1404 && pfile->cur_run->prev != NULL)
1406 pfile->cur_run = pfile->cur_run->prev;
1407 pfile->cur_token = pfile->cur_run->limit;
1412 /* Step back one (or more) tokens. Can only step back more than 1 if
1413 they are from the lexer, and not from macro expansion. */
1414 void
1415 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
1417 if (pfile->context->prev == NULL)
1418 _cpp_backup_tokens_direct (pfile, count);
1419 else
1421 if (count != 1)
1422 abort ();
1423 if (pfile->context->direct_p)
1424 FIRST (pfile->context).token--;
1425 else
1426 FIRST (pfile->context).ptoken--;
1430 /* #define directive parsing and handling. */
1432 /* Returns nonzero if a macro redefinition warning is required. */
1433 static bool
1434 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
1435 const cpp_macro *macro2)
1437 const cpp_macro *macro1;
1438 unsigned int i;
1440 /* Some redefinitions need to be warned about regardless. */
1441 if (node->flags & NODE_WARN)
1442 return true;
1444 /* Suppress warnings for builtins that lack the NODE_WARN flag. */
1445 if (node->flags & NODE_BUILTIN)
1447 if (!pfile->cb.user_builtin_macro
1448 || !pfile->cb.user_builtin_macro (pfile, node))
1449 return false;
1452 /* Redefinitions of conditional (context-sensitive) macros, on
1453 the other hand, must be allowed silently. */
1454 if (node->flags & NODE_CONDITIONAL)
1455 return false;
1457 /* Redefinition of a macro is allowed if and only if the old and new
1458 definitions are the same. (6.10.3 paragraph 2). */
1459 macro1 = node->value.macro;
1461 /* Don't check count here as it can be different in valid
1462 traditional redefinitions with just whitespace differences. */
1463 if (macro1->paramc != macro2->paramc
1464 || macro1->fun_like != macro2->fun_like
1465 || macro1->variadic != macro2->variadic)
1466 return true;
1468 /* Check parameter spellings. */
1469 for (i = 0; i < macro1->paramc; i++)
1470 if (macro1->params[i] != macro2->params[i])
1471 return true;
1473 /* Check the replacement text or tokens. */
1474 if (CPP_OPTION (pfile, traditional))
1475 return _cpp_expansions_different_trad (macro1, macro2);
1477 if (macro1->count != macro2->count)
1478 return true;
1480 for (i = 0; i < macro1->count; i++)
1481 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1482 return true;
1484 return false;
1487 /* Free the definition of hashnode H. */
1488 void
1489 _cpp_free_definition (cpp_hashnode *h)
1491 /* Macros and assertions no longer have anything to free. */
1492 h->type = NT_VOID;
1493 /* Clear builtin flag in case of redefinition. */
1494 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
1497 /* Save parameter NODE to the parameter list of macro MACRO. Returns
1498 zero on success, nonzero if the parameter is a duplicate. */
1499 bool
1500 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
1502 unsigned int len;
1503 /* Constraint 6.10.3.6 - duplicate parameter names. */
1504 if (node->flags & NODE_MACRO_ARG)
1506 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
1507 NODE_NAME (node));
1508 return true;
1511 if (BUFF_ROOM (pfile->a_buff)
1512 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1513 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1515 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1516 node->flags |= NODE_MACRO_ARG;
1517 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1518 if (len > pfile->macro_buffer_len)
1520 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
1521 len);
1522 pfile->macro_buffer_len = len;
1524 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1525 = node->value;
1527 node->value.arg_index = macro->paramc;
1528 return false;
1531 /* Check the syntax of the parameters in a MACRO definition. Returns
1532 false if an error occurs. */
1533 static bool
1534 parse_params (cpp_reader *pfile, cpp_macro *macro)
1536 unsigned int prev_ident = 0;
1538 for (;;)
1540 const cpp_token *token = _cpp_lex_token (pfile);
1542 switch (token->type)
1544 default:
1545 /* Allow/ignore comments in parameter lists if we are
1546 preserving comments in macro expansions. */
1547 if (token->type == CPP_COMMENT
1548 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1549 continue;
1551 cpp_error (pfile, CPP_DL_ERROR,
1552 "\"%s\" may not appear in macro parameter list",
1553 cpp_token_as_text (pfile, token));
1554 return false;
1556 case CPP_NAME:
1557 if (prev_ident)
1559 cpp_error (pfile, CPP_DL_ERROR,
1560 "macro parameters must be comma-separated");
1561 return false;
1563 prev_ident = 1;
1565 if (_cpp_save_parameter (pfile, macro, token->val.node.node))
1566 return false;
1567 continue;
1569 case CPP_CLOSE_PAREN:
1570 if (prev_ident || macro->paramc == 0)
1571 return true;
1573 /* Fall through to pick up the error. */
1574 case CPP_COMMA:
1575 if (!prev_ident)
1577 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
1578 return false;
1580 prev_ident = 0;
1581 continue;
1583 case CPP_ELLIPSIS:
1584 macro->variadic = 1;
1585 if (!prev_ident)
1587 _cpp_save_parameter (pfile, macro,
1588 pfile->spec_nodes.n__VA_ARGS__);
1589 pfile->state.va_args_ok = 1;
1590 if (! CPP_OPTION (pfile, c99)
1591 && CPP_OPTION (pfile, cpp_pedantic)
1592 && CPP_OPTION (pfile, warn_variadic_macros))
1593 cpp_pedwarning
1594 (pfile, CPP_W_VARIADIC_MACROS,
1595 "anonymous variadic macros were introduced in C99");
1597 else if (CPP_OPTION (pfile, cpp_pedantic)
1598 && CPP_OPTION (pfile, warn_variadic_macros))
1599 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
1600 "ISO C does not permit named variadic macros");
1602 /* We're at the end, and just expect a closing parenthesis. */
1603 token = _cpp_lex_token (pfile);
1604 if (token->type == CPP_CLOSE_PAREN)
1605 return true;
1606 /* Fall through. */
1608 case CPP_EOF:
1609 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
1610 return false;
1615 /* Allocate room for a token from a macro's replacement list. */
1616 static cpp_token *
1617 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1619 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1620 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1622 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1625 /* Lex a token from the expansion of MACRO, but mark parameters as we
1626 find them and warn of traditional stringification. */
1627 static cpp_token *
1628 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1630 cpp_token *token, *saved_cur_token;
1632 saved_cur_token = pfile->cur_token;
1633 pfile->cur_token = alloc_expansion_token (pfile, macro);
1634 token = _cpp_lex_direct (pfile);
1635 pfile->cur_token = saved_cur_token;
1637 /* Is this a parameter? */
1638 if (token->type == CPP_NAME
1639 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
1641 token->type = CPP_MACRO_ARG;
1642 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
1644 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1645 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1646 check_trad_stringification (pfile, macro, &token->val.str);
1648 return token;
1651 static bool
1652 create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
1654 cpp_token *token;
1655 const cpp_token *ctoken;
1656 bool following_paste_op = false;
1657 const char *paste_op_error_msg =
1658 N_("'##' cannot appear at either end of a macro expansion");
1659 unsigned int num_extra_tokens = 0;
1661 /* Get the first token of the expansion (or the '(' of a
1662 function-like macro). */
1663 ctoken = _cpp_lex_token (pfile);
1665 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1667 bool ok = parse_params (pfile, macro);
1668 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1669 if (!ok)
1670 return false;
1672 /* Success. Commit or allocate the parameter array. */
1673 if (pfile->hash_table->alloc_subobject)
1675 cpp_hashnode **params =
1676 (cpp_hashnode **) pfile->hash_table->alloc_subobject
1677 (sizeof (cpp_hashnode *) * macro->paramc);
1678 memcpy (params, macro->params,
1679 sizeof (cpp_hashnode *) * macro->paramc);
1680 macro->params = params;
1682 else
1683 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
1684 macro->fun_like = 1;
1686 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1688 /* While ISO C99 requires whitespace before replacement text
1689 in a macro definition, ISO C90 with TC1 allows there characters
1690 from the basic source character set. */
1691 if (CPP_OPTION (pfile, c99))
1692 cpp_error (pfile, CPP_DL_PEDWARN,
1693 "ISO C99 requires whitespace after the macro name");
1694 else
1696 int warntype = CPP_DL_WARNING;
1697 switch (ctoken->type)
1699 case CPP_ATSIGN:
1700 case CPP_AT_NAME:
1701 case CPP_OBJC_STRING:
1702 /* '@' is not in basic character set. */
1703 warntype = CPP_DL_PEDWARN;
1704 break;
1705 case CPP_OTHER:
1706 /* Basic character set sans letters, digits and _. */
1707 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
1708 ctoken->val.str.text[0]) == NULL)
1709 warntype = CPP_DL_PEDWARN;
1710 break;
1711 default:
1712 /* All other tokens start with a character from basic
1713 character set. */
1714 break;
1716 cpp_error (pfile, warntype,
1717 "missing whitespace after the macro name");
1721 if (macro->fun_like)
1722 token = lex_expansion_token (pfile, macro);
1723 else
1725 token = alloc_expansion_token (pfile, macro);
1726 *token = *ctoken;
1729 for (;;)
1731 /* Check the stringifying # constraint 6.10.3.2.1 of
1732 function-like macros when lexing the subsequent token. */
1733 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1735 if (token->type == CPP_MACRO_ARG)
1737 if (token->flags & PREV_WHITE)
1738 token->flags |= SP_PREV_WHITE;
1739 if (token[-1].flags & DIGRAPH)
1740 token->flags |= SP_DIGRAPH;
1741 token->flags &= ~PREV_WHITE;
1742 token->flags |= STRINGIFY_ARG;
1743 token->flags |= token[-1].flags & PREV_WHITE;
1744 token[-1] = token[0];
1745 macro->count--;
1747 /* Let assembler get away with murder. */
1748 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1750 cpp_error (pfile, CPP_DL_ERROR,
1751 "'#' is not followed by a macro parameter");
1752 return false;
1756 if (token->type == CPP_EOF)
1758 /* Paste operator constraint 6.10.3.3.1:
1759 Token-paste ##, can appear in both object-like and
1760 function-like macros, but not at the end. */
1761 if (following_paste_op)
1763 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
1764 return false;
1766 break;
1769 /* Paste operator constraint 6.10.3.3.1. */
1770 if (token->type == CPP_PASTE)
1772 /* Token-paste ##, can appear in both object-like and
1773 function-like macros, but not at the beginning. */
1774 if (macro->count == 1)
1776 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
1777 return false;
1780 if (token[-1].flags & PASTE_LEFT)
1782 macro->extra_tokens = 1;
1783 num_extra_tokens++;
1784 token->val.token_no = macro->count - 1;
1786 else
1788 --macro->count;
1789 token[-1].flags |= PASTE_LEFT;
1790 if (token->flags & DIGRAPH)
1791 token[-1].flags |= SP_DIGRAPH;
1792 if (token->flags & PREV_WHITE)
1793 token[-1].flags |= SP_PREV_WHITE;
1797 following_paste_op = (token->type == CPP_PASTE);
1798 token = lex_expansion_token (pfile, macro);
1801 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1802 macro->traditional = 0;
1804 /* Don't count the CPP_EOF. */
1805 macro->count--;
1807 /* Clear whitespace on first token for warn_of_redefinition(). */
1808 if (macro->count)
1809 macro->exp.tokens[0].flags &= ~PREV_WHITE;
1811 /* Commit or allocate the memory. */
1812 if (pfile->hash_table->alloc_subobject)
1814 cpp_token *tokns =
1815 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
1816 * macro->count);
1817 if (num_extra_tokens)
1819 /* Place second and subsequent ## or %:%: tokens in
1820 sequences of consecutive such tokens at the end of the
1821 list to preserve information about where they appear, how
1822 they are spelt and whether they are preceded by
1823 whitespace without otherwise interfering with macro
1824 expansion. */
1825 cpp_token *normal_dest = tokns;
1826 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
1827 unsigned int i;
1828 for (i = 0; i < macro->count; i++)
1830 if (macro->exp.tokens[i].type == CPP_PASTE)
1831 *extra_dest++ = macro->exp.tokens[i];
1832 else
1833 *normal_dest++ = macro->exp.tokens[i];
1836 else
1837 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
1838 macro->exp.tokens = tokns;
1840 else
1841 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
1843 return true;
1846 /* Parse a macro and save its expansion. Returns nonzero on success. */
1847 bool
1848 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
1850 cpp_macro *macro;
1851 unsigned int i;
1852 bool ok;
1854 if (pfile->hash_table->alloc_subobject)
1855 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
1856 (sizeof (cpp_macro));
1857 else
1858 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1859 macro->line = pfile->directive_line;
1860 macro->params = 0;
1861 macro->paramc = 0;
1862 macro->variadic = 0;
1863 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
1864 macro->count = 0;
1865 macro->fun_like = 0;
1866 macro->extra_tokens = 0;
1867 /* To suppress some diagnostics. */
1868 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
1870 if (CPP_OPTION (pfile, traditional))
1871 ok = _cpp_create_trad_definition (pfile, macro);
1872 else
1874 ok = create_iso_definition (pfile, macro);
1876 /* We set the type for SEEN_EOL() in directives.c.
1878 Longer term we should lex the whole line before coming here,
1879 and just copy the expansion. */
1881 /* Stop the lexer accepting __VA_ARGS__. */
1882 pfile->state.va_args_ok = 0;
1885 /* Clear the fast argument lookup indices. */
1886 for (i = macro->paramc; i-- > 0; )
1888 struct cpp_hashnode *node = macro->params[i];
1889 node->flags &= ~ NODE_MACRO_ARG;
1890 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1893 if (!ok)
1894 return ok;
1896 if (node->type == NT_MACRO)
1898 if (CPP_OPTION (pfile, warn_unused_macros))
1899 _cpp_warn_if_unused_macro (pfile, node, NULL);
1901 if (warn_of_redefinition (pfile, node, macro))
1903 const int reason = (node->flags & NODE_BUILTIN)
1904 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
1905 bool warned;
1907 warned = cpp_pedwarning_with_line (pfile, reason,
1908 pfile->directive_line, 0,
1909 "\"%s\" redefined",
1910 NODE_NAME (node));
1912 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1913 cpp_error_with_line (pfile, CPP_DL_NOTE,
1914 node->value.macro->line, 0,
1915 "this is the location of the previous definition");
1919 if (node->type != NT_VOID)
1920 _cpp_free_definition (node);
1922 /* Enter definition in hash table. */
1923 node->type = NT_MACRO;
1924 node->value.macro = macro;
1925 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
1926 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
1927 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
1928 in the C standard, as something that one must use in C++.
1929 However DR#593 indicates that these aren't actually mentioned
1930 in the C++ standard. We special-case them anyway. */
1931 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
1932 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
1933 node->flags |= NODE_WARN;
1935 /* If user defines one of the conditional macros, remove the
1936 conditional flag */
1937 node->flags &= ~NODE_CONDITIONAL;
1939 return ok;
1942 /* Warn if a token in STRING matches one of a function-like MACRO's
1943 parameters. */
1944 static void
1945 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1946 const cpp_string *string)
1948 unsigned int i, len;
1949 const uchar *p, *q, *limit;
1951 /* Loop over the string. */
1952 limit = string->text + string->len - 1;
1953 for (p = string->text + 1; p < limit; p = q)
1955 /* Find the start of an identifier. */
1956 while (p < limit && !is_idstart (*p))
1957 p++;
1959 /* Find the end of the identifier. */
1960 q = p;
1961 while (q < limit && is_idchar (*q))
1962 q++;
1964 len = q - p;
1966 /* Loop over the function macro arguments to see if the
1967 identifier inside the string matches one of them. */
1968 for (i = 0; i < macro->paramc; i++)
1970 const cpp_hashnode *node = macro->params[i];
1972 if (NODE_LEN (node) == len
1973 && !memcmp (p, NODE_NAME (node), len))
1975 cpp_error (pfile, CPP_DL_WARNING,
1976 "macro argument \"%s\" would be stringified in traditional C",
1977 NODE_NAME (node));
1978 break;
1984 /* Returns the name, arguments and expansion of a macro, in a format
1985 suitable to be read back in again, and therefore also for DWARF 2
1986 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1987 Caller is expected to generate the "#define" bit if needed. The
1988 returned text is temporary, and automatically freed later. */
1989 const unsigned char *
1990 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
1992 unsigned int i, len;
1993 const cpp_macro *macro;
1994 unsigned char *buffer;
1996 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1998 if (node->type != NT_MACRO
1999 || !pfile->cb.user_builtin_macro
2000 || !pfile->cb.user_builtin_macro (pfile, node))
2002 cpp_error (pfile, CPP_DL_ICE,
2003 "invalid hash type %d in cpp_macro_definition",
2004 node->type);
2005 return 0;
2009 macro = node->value.macro;
2010 /* Calculate length. */
2011 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
2012 if (macro->fun_like)
2014 len += 4; /* "()" plus possible final ".." of named
2015 varargs (we have + 1 below). */
2016 for (i = 0; i < macro->paramc; i++)
2017 len += NODE_LEN (macro->params[i]) + 1; /* "," */
2020 /* This should match below where we fill in the buffer. */
2021 if (CPP_OPTION (pfile, traditional))
2022 len += _cpp_replacement_text_len (macro);
2023 else
2025 unsigned int count = macro_real_token_count (macro);
2026 for (i = 0; i < count; i++)
2028 cpp_token *token = &macro->exp.tokens[i];
2030 if (token->type == CPP_MACRO_ARG)
2031 len += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
2032 else
2033 len += cpp_token_len (token);
2035 if (token->flags & STRINGIFY_ARG)
2036 len++; /* "#" */
2037 if (token->flags & PASTE_LEFT)
2038 len += 3; /* " ##" */
2039 if (token->flags & PREV_WHITE)
2040 len++; /* " " */
2044 if (len > pfile->macro_buffer_len)
2046 pfile->macro_buffer = XRESIZEVEC (unsigned char,
2047 pfile->macro_buffer, len);
2048 pfile->macro_buffer_len = len;
2051 /* Fill in the buffer. Start with the macro name. */
2052 buffer = pfile->macro_buffer;
2053 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
2054 buffer += NODE_LEN (node);
2056 /* Parameter names. */
2057 if (macro->fun_like)
2059 *buffer++ = '(';
2060 for (i = 0; i < macro->paramc; i++)
2062 cpp_hashnode *param = macro->params[i];
2064 if (param != pfile->spec_nodes.n__VA_ARGS__)
2066 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
2067 buffer += NODE_LEN (param);
2070 if (i + 1 < macro->paramc)
2071 /* Don't emit a space after the comma here; we're trying
2072 to emit a Dwarf-friendly definition, and the Dwarf spec
2073 forbids spaces in the argument list. */
2074 *buffer++ = ',';
2075 else if (macro->variadic)
2076 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
2078 *buffer++ = ')';
2081 /* The Dwarf spec requires a space after the macro name, even if the
2082 definition is the empty string. */
2083 *buffer++ = ' ';
2085 if (CPP_OPTION (pfile, traditional))
2086 buffer = _cpp_copy_replacement_text (macro, buffer);
2087 else if (macro->count)
2088 /* Expansion tokens. */
2090 unsigned int count = macro_real_token_count (macro);
2091 for (i = 0; i < count; i++)
2093 cpp_token *token = &macro->exp.tokens[i];
2095 if (token->flags & PREV_WHITE)
2096 *buffer++ = ' ';
2097 if (token->flags & STRINGIFY_ARG)
2098 *buffer++ = '#';
2100 if (token->type == CPP_MACRO_ARG)
2102 memcpy (buffer,
2103 NODE_NAME (macro->params[token->val.macro_arg.arg_no - 1]),
2104 NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]));
2105 buffer += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
2107 else
2108 buffer = cpp_spell_token (pfile, token, buffer, false);
2110 if (token->flags & PASTE_LEFT)
2112 *buffer++ = ' ';
2113 *buffer++ = '#';
2114 *buffer++ = '#';
2115 /* Next has PREV_WHITE; see _cpp_create_definition. */
2120 *buffer = '\0';
2121 return pfile->macro_buffer;