]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cppmacro.c
solaris_threads.c (MAX_ORIG_STACK_SIZE): Provide special Solaris 2/Intel definition.
[thirdparty/gcc.git] / gcc / cppmacro.c
CommitLineData
93c80368 1/* Part of CPP library. (Macro and #define handling.)
711b8824 2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
4b49c365 3 1999, 2000, 2001 Free Software Foundation, Inc.
711b8824
ZW
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
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
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! */
25
26#include "config.h"
27#include "system.h"
93c80368 28#include "intl.h" /* for _("<command line>") below. */
711b8824
ZW
29#include "cpplib.h"
30#include "cpphash.h"
31
93c80368 32struct cpp_macro
711b8824 33{
93c80368
NB
34 cpp_hashnode **params; /* Parameters, if any. */
35 cpp_token *expansion; /* First token of replacement list. */
93c80368
NB
36 unsigned int line; /* Starting line number. */
37 unsigned int count; /* Number of tokens in expansion. */
38 unsigned short paramc; /* Number of parameters. */
39 unsigned int fun_like : 1; /* If a function-like macro. */
28e0f040 40 unsigned int variadic : 1; /* If a variadic macro. */
93c80368 41 unsigned int disabled : 1; /* If macro is disabled. */
7065e130 42 unsigned int syshdr : 1; /* If macro defined in system header. */
93c80368
NB
43};
44
45typedef struct macro_arg macro_arg;
46struct macro_arg
47{
4ed5bcfb
NB
48 const cpp_token **first; /* First token in unexpanded argument. */
49 const cpp_token **expanded; /* Macro-expanded argument. */
50 const cpp_token *stringified; /* Stringified argument. */
93c80368
NB
51 unsigned int count; /* # of tokens in argument. */
52 unsigned int expanded_count; /* # of tokens in expanded argument. */
711b8824
ZW
53};
54
93c80368
NB
55/* Macro expansion. */
56
57static void lock_pools PARAMS ((cpp_reader *));
58static void unlock_pools PARAMS ((cpp_reader *));
29b10746 59static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
4ed5bcfb
NB
60static const cpp_token *builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
61static void push_token_context
62 PARAMS ((cpp_reader *, cpp_macro *, const cpp_token *, unsigned int));
63static void push_ptoken_context
64 PARAMS ((cpp_reader *, cpp_macro *, const cpp_token **, unsigned int));
93c80368
NB
65static enum cpp_ttype parse_arg PARAMS ((cpp_reader *, macro_arg *, int));
66static macro_arg *parse_args PARAMS ((cpp_reader *, const cpp_hashnode *));
67static cpp_context *next_context PARAMS ((cpp_reader *));
4ed5bcfb
NB
68static const cpp_token *padding_token
69 PARAMS ((cpp_reader *, const cpp_token *));
93c80368
NB
70static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
71static unsigned char *quote_string PARAMS ((unsigned char *,
72 const unsigned char *,
73 unsigned int));
4ed5bcfb
NB
74static const cpp_token *new_string_token PARAMS ((cpp_reader *, U_CHAR *,
75 unsigned int));
76static const cpp_token *new_number_token PARAMS ((cpp_reader *, int));
77static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
78static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
79static int paste_tokens PARAMS ((cpp_reader *, cpp_token *,
80 const cpp_token *));
81static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *));
82static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *));
93c80368 83
93c80368
NB
84/* #define directive parsing and handling. */
85
14baae01 86static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
93c80368 87static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
618cdda7
NB
88static int warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
89 const cpp_macro *));
93c80368
NB
90static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
91static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
e1aa5140 92static void check_trad_stringification PARAMS ((cpp_reader *,
93c80368 93 const cpp_macro *,
e1aa5140 94 const cpp_string *));
711b8824 95
4ed5bcfb
NB
96/* Allocates and returns a CPP_STRING token, containing TEXT of length
97 LEN, after null-terminating it. TEXT must be in permanent storage. */
98static const cpp_token *
99new_string_token (pfile, text, len)
100 cpp_reader *pfile;
101 unsigned char *text;
93c80368
NB
102 unsigned int len;
103{
4ed5bcfb 104 cpp_token *token = _cpp_temp_token (pfile);
93c80368 105
4ed5bcfb 106 text[len] = '\0';
93c80368 107 token->type = CPP_STRING;
4ed5bcfb
NB
108 token->val.str.len = len;
109 token->val.str.text = text;
93c80368 110 token->flags = 0;
4ed5bcfb 111 return token;
93c80368
NB
112}
113
4ed5bcfb
NB
114/* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */
115static const cpp_token *
116new_number_token (pfile, number)
93c80368 117 cpp_reader *pfile;
93c80368
NB
118 int number;
119{
4ed5bcfb 120 cpp_token *token = _cpp_temp_token (pfile);
49fe13f6 121 unsigned char *buf = _cpp_pool_alloc (&pfile->ident_pool, 20);
93c80368
NB
122
123 sprintf ((char *) buf, "%d", number);
124 token->type = CPP_NUMBER;
125 token->val.str.text = buf;
126 token->val.str.len = ustrlen (buf);
127 token->flags = 0;
4ed5bcfb 128 return token;
93c80368
NB
129}
130
131static const char * const monthnames[] =
132{
133 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
134 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
135};
136
137/* Handle builtin macros like __FILE__. */
4ed5bcfb
NB
138static const cpp_token *
139builtin_macro (pfile, node)
93c80368 140 cpp_reader *pfile;
4ed5bcfb 141 cpp_hashnode *node;
93c80368 142{
93c80368
NB
143 switch (node->value.builtin)
144 {
4ed5bcfb
NB
145 default:
146 cpp_ice (pfile, "invalid builtin macro \"%s\"", NODE_NAME (node));
147 return new_number_token (pfile, 1);
148
93c80368
NB
149 case BT_FILE:
150 case BT_BASE_FILE:
711b8824 151 {
4ed5bcfb 152 unsigned int len;
0bda4760 153 const char *name;
4ed5bcfb 154 U_CHAR *buf;
bb74c963 155 const struct line_map *map = pfile->map;
0bda4760
NB
156
157 if (node->value.builtin == BT_BASE_FILE)
bb74c963
NB
158 while (! MAIN_FILE_P (map))
159 map = INCLUDED_FROM (&pfile->line_maps, map);
0bda4760 160
bb74c963 161 name = map->to_file;
4ed5bcfb
NB
162 len = strlen (name);
163 buf = _cpp_pool_alloc (&pfile->ident_pool, len * 4 + 1);
164 len = quote_string (buf, (const unsigned char *) name, len) - buf;
165
166 return new_string_token (pfile, buf, len);
711b8824 167 }
93c80368
NB
168
169 case BT_INCLUDE_LEVEL:
d8693c6f
NB
170 /* The line map depth counts the primary source as level 1, but
171 historically __INCLUDE_DEPTH__ has called the primary source
172 level 0. */
4ed5bcfb 173 return new_number_token (pfile, pfile->line_maps.depth - 1);
93c80368
NB
174
175 case BT_SPECLINE:
176 /* If __LINE__ is embedded in a macro, it must expand to the
177 line of the macro's invocation, not its definition.
178 Otherwise things like assert() will not work properly. */
4ed5bcfb
NB
179 return new_number_token (pfile, SOURCE_LINE (pfile->map,
180 pfile->cur_token[-1].line));
93c80368
NB
181
182 case BT_STDC:
183 {
618cdda7
NB
184 int stdc = (!CPP_IN_SYSTEM_HEADER (pfile)
185 || pfile->spec_nodes.n__STRICT_ANSI__->type != NT_VOID);
4ed5bcfb 186 return new_number_token (pfile, stdc);
93c80368 187 }
711b8824 188
93c80368
NB
189 case BT_DATE:
190 case BT_TIME:
191 if (pfile->date.type == CPP_EOF)
192 {
4ed5bcfb
NB
193 /* Allocate __DATE__ and __TIME__ strings from permanent
194 storage. We only do this once, and don't generate them
195 at init time, because time() and localtime() are very
196 slow on some systems. */
93c80368
NB
197 time_t tt = time (NULL);
198 struct tm *tb = localtime (&tt);
199
4ed5bcfb
NB
200 pfile->date.val.str.text =
201 _cpp_pool_alloc (&pfile->ident_pool, sizeof ("Oct 11 1347"));
202 pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
203 pfile->date.type = CPP_STRING;
204 pfile->date.flags = 0;
93c80368
NB
205 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
206 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
4ed5bcfb
NB
207
208 pfile->time.val.str.text =
209 _cpp_pool_alloc (&pfile->ident_pool, sizeof ("12:34:56"));
210 pfile->time.val.str.len = sizeof ("12:34:56") - 1;
211 pfile->time.type = CPP_STRING;
212 pfile->time.flags = 0;
93c80368
NB
213 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
214 tb->tm_hour, tb->tm_min, tb->tm_sec);
215 }
93c80368 216
4ed5bcfb 217 return node->value.builtin == BT_DATE ? &pfile->date: &pfile->time;
93c80368 218 }
711b8824
ZW
219}
220
93c80368
NB
221static void
222lock_pools (pfile)
223 cpp_reader *pfile;
224{
93c80368 225 _cpp_lock_pool (&pfile->argument_pool);
711b8824
ZW
226}
227
93c80368
NB
228static void
229unlock_pools (pfile)
230 cpp_reader *pfile;
231{
93c80368
NB
232 _cpp_unlock_pool (&pfile->argument_pool);
233}
711b8824 234
93c80368
NB
235/* Adds backslashes before all backslashes and double quotes appearing
236 in strings. Non-printable characters are converted to octal. */
237static U_CHAR *
238quote_string (dest, src, len)
239 U_CHAR *dest;
240 const U_CHAR *src;
241 unsigned int len;
242{
243 while (len--)
244 {
245 U_CHAR c = *src++;
711b8824 246
93c80368
NB
247 if (c == '\\' || c == '"')
248 {
249 *dest++ = '\\';
250 *dest++ = c;
251 }
252 else
253 {
254 if (ISPRINT (c))
255 *dest++ = c;
256 else
711b8824 257 {
93c80368
NB
258 sprintf ((char *) dest, "\\%03o", c);
259 dest += 4;
711b8824 260 }
93c80368
NB
261 }
262 }
711b8824 263
93c80368
NB
264 return dest;
265}
711b8824 266
93c80368
NB
267/* Convert a token sequence to a single string token according to the
268 rules of the ISO C #-operator. */
4ed5bcfb 269static const cpp_token *
93c80368
NB
270stringify_arg (pfile, arg)
271 cpp_reader *pfile;
272 macro_arg *arg;
273{
49fe13f6 274 cpp_pool *pool = &pfile->ident_pool;
93c80368
NB
275 unsigned char *start = POOL_FRONT (pool);
276 unsigned int i, escape_it, total_len = 0, backslash_count = 0;
4ed5bcfb 277 const cpp_token *source = NULL;
711b8824 278
93c80368
NB
279 /* Loop, reading in the argument's tokens. */
280 for (i = 0; i < arg->count; i++)
281 {
282 unsigned char *dest;
4ed5bcfb
NB
283 const cpp_token *token = arg->first[i];
284 unsigned int len;
285
286 if (token->type == CPP_PADDING)
287 {
288 if (source == NULL)
289 source = token->val.source;
290 continue;
291 }
711b8824 292
93c80368 293 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
cc937581 294 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
711b8824 295
4ed5bcfb 296 len = cpp_token_len (token);
93c80368
NB
297 if (escape_it)
298 /* Worst case is each char is octal. */
299 len *= 4;
b044e9d5 300 len += 2; /* Room for initial space and final NUL. */
711b8824 301
93c80368
NB
302 dest = &start[total_len];
303 if (dest + len > POOL_LIMIT (pool))
304 {
305 _cpp_next_chunk (pool, len, (unsigned char **) &start);
306 dest = &start[total_len];
307 }
711b8824 308
4ed5bcfb
NB
309 /* Leading white space? */
310 if (total_len)
311 {
312 if (source == NULL)
313 source = token;
314 if (source->flags & PREV_WHITE)
315 *dest++ = ' ';
316 }
317 source = NULL;
711b8824 318
93c80368
NB
319 if (escape_it)
320 {
321 unsigned char *buf = (unsigned char *) xmalloc (len);
322
323 len = cpp_spell_token (pfile, token, buf) - buf;
324 dest = quote_string (dest, buf, len);
325 free (buf);
326 }
327 else
328 dest = cpp_spell_token (pfile, token, dest);
329 total_len = dest - start;
330
6c53ebff 331 if (token->type == CPP_OTHER && token->val.c == '\\')
93c80368
NB
332 backslash_count++;
333 else
334 backslash_count = 0;
335 }
336
337 /* Ignore the final \ of invalid string literals. */
338 if (backslash_count & 1)
339 {
340 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
341 total_len--;
342 }
343
4ed5bcfb 344 /* Commit the memory, including NUL, and return the token. */
b044e9d5 345 POOL_COMMIT (pool, total_len + 1);
4ed5bcfb 346 return new_string_token (pfile, start, total_len);
93c80368
NB
347}
348
d63eefbf
NB
349/* Try to paste two tokens. On success, the LHS becomes the pasted
350 token, and 0 is returned. For failure, we update the flags of the
351 RHS appropriately and return non-zero. */
352static int
353paste_tokens (pfile, lhs, rhs)
354 cpp_reader *pfile;
4ed5bcfb
NB
355 cpp_token *lhs;
356 const cpp_token *rhs;
d63eefbf 357{
4ed5bcfb 358 unsigned char flags = 0;
d63eefbf
NB
359 int digraph = 0;
360 enum cpp_ttype type;
361
362 type = cpp_can_paste (pfile, lhs, rhs, &digraph);
363
364 if (type == CPP_EOF)
365 {
6a6b1628
NB
366 /* Mandatory warning for all apart from assembler. */
367 if (CPP_OPTION (pfile, lang) != CLK_ASM)
d63eefbf
NB
368 cpp_warning (pfile,
369 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
370 cpp_token_as_text (pfile, lhs),
371 cpp_token_as_text (pfile, rhs));
d63eefbf
NB
372 return 1;
373 }
374
d63eefbf
NB
375 if (digraph)
376 flags |= DIGRAPH;
377
378 /* Identifiers and numbers need spellings to be pasted. */
379 if (type == CPP_NAME || type == CPP_NUMBER)
380 {
381 unsigned int total_len = cpp_token_len (lhs) + cpp_token_len (rhs);
382 unsigned char *result, *end;
d63eefbf 383
49fe13f6 384 result = _cpp_pool_alloc (&pfile->ident_pool, total_len + 1);
d63eefbf
NB
385
386 /* Paste the spellings and null terminate. */
387 end = cpp_spell_token (pfile, rhs, cpp_spell_token (pfile, lhs, result));
388 *end = '\0';
389 total_len = end - result;
390
391 if (type == CPP_NAME)
392 {
393 lhs->val.node = cpp_lookup (pfile, result, total_len);
394 if (lhs->val.node->flags & NODE_OPERATOR)
395 {
396 flags |= NAMED_OP;
397 lhs->type = lhs->val.node->value.operator;
398 }
399 }
400 else
401 {
402 lhs->val.str.text = result;
403 lhs->val.str.len = total_len;
404 }
405 }
406 else if (type == CPP_WCHAR || type == CPP_WSTRING)
407 lhs->val.str = rhs->val.str;
408
409 /* Set type and flags after pasting spellings. */
410 lhs->type = type;
411 lhs->flags = flags;
412
413 return 0;
414}
415
93c80368
NB
416/* Handles an arbitrarily long sequence of ## operators. This
417 implementation is left-associative, non-recursive, and finishes a
d63eefbf
NB
418 paste before handling succeeding ones. If the paste fails, we back
419 up a token to just after the ## operator, with the effect that it
420 appears in the output stream normally. */
93c80368
NB
421static void
422paste_all_tokens (pfile, lhs)
423 cpp_reader *pfile;
4ed5bcfb 424 const cpp_token *lhs;
93c80368 425{
4ed5bcfb
NB
426 cpp_token *pasted;
427 const cpp_token *rhs;
428 cpp_context *context = pfile->context;
429
430 /* Copy lhs to pasted, but preserve original line and column. */
431 pasted = _cpp_temp_token (pfile);
432 pasted->type = lhs->type;
433 pasted->flags = lhs->flags;
434 pasted->val.str = lhs->val.str;
93c80368
NB
435
436 do
437 {
438 /* Take the token directly from the current context. We can do
439 this, because we are in the replacement list of either an
440 object-like macro, or a function-like macro with arguments
441 inserted. In either case, the constraints to #define
d63eefbf 442 guarantee we have at least one more token. */
4ed5bcfb
NB
443 if (context->direct_p)
444 rhs = context->first.token++;
445 else
446 rhs = *context->first.ptoken++;
447
448 if (rhs->type == CPP_PADDING)
449 abort ();
450
451 if (paste_tokens (pfile, pasted, rhs))
93c80368 452 {
4ed5bcfb 453 _cpp_backup_tokens (pfile, 1);
4c2b647d 454 break;
93c80368 455 }
93c80368
NB
456 }
457 while (rhs->flags & PASTE_LEFT);
458
4ed5bcfb
NB
459 /* Clear PASTE_LEFT flag, put the token in its own context. */
460 pasted->flags &= ~PASTE_LEFT;
461 push_token_context (pfile, NULL, pasted, 1);
93c80368
NB
462}
463
4c2b647d 464/* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS
28e0f040
NB
465 is non-zero if this is a variadic macro. Returns the type of the
466 token that caused reading to finish. */
93c80368 467static enum cpp_ttype
28e0f040 468parse_arg (pfile, arg, variadic)
93c80368
NB
469 cpp_reader *pfile;
470 struct macro_arg *arg;
28e0f040 471 int variadic;
93c80368
NB
472{
473 enum cpp_ttype result;
474 unsigned int paren = 0;
475
4ed5bcfb 476 arg->first = (const cpp_token **) POOL_FRONT (&pfile->argument_pool);
93c80368
NB
477 for (;; arg->count++)
478 {
4ed5bcfb
NB
479 const cpp_token *token;
480 const cpp_token **ptoken = &arg->first[arg->count];
481 if ((unsigned char *) (ptoken + 2) >= POOL_LIMIT (&pfile->argument_pool))
93c80368 482 {
4ed5bcfb 483 _cpp_next_chunk (&pfile->argument_pool, 2 * sizeof (cpp_token *),
93c80368 484 (unsigned char **) &arg->first);
4ed5bcfb 485 ptoken = &arg->first[arg->count];
93c80368
NB
486 }
487
4ed5bcfb
NB
488 /* Drop leading padding. */
489 do
490 token = cpp_get_token (pfile);
491 while (arg->count == 0 && token->type == CPP_PADDING);
492 *ptoken++ = token;
8d9e9a08 493 result = token->type;
bdcbe496 494
93c80368
NB
495 if (result == CPP_OPEN_PAREN)
496 paren++;
497 else if (result == CPP_CLOSE_PAREN && paren-- == 0)
498 break;
28e0f040
NB
499 /* Commas are not terminators within parantheses or variadic. */
500 else if (result == CPP_COMMA && paren == 0 && !variadic)
93c80368
NB
501 break;
502 else if (result == CPP_EOF)
bdcbe496
NB
503 {
504 /* We still need the EOF (added below) to end pre-expansion
505 and directives. */
506 if (pfile->context->prev || pfile->state.in_directive)
507 _cpp_backup_tokens (pfile, 1);
508 /* Error reported by caller. */
509 break;
510 }
511 else if (result == CPP_HASH && token->flags & BOL)
512 {
513 /* 6.10.3 paragraph 11: If there are sequences of
514 preprocessing tokens within the list of arguments that
515 would otherwise act as preprocessing directives, the
516 behavior is undefined.
517
518 This implementation will report a hard error, terminate
519 the macro invocation, and proceed to process the
520 directive. */
521 cpp_error (pfile,
522 "directives may not be used inside a macro argument");
523 _cpp_backup_tokens (pfile, 1);
524 result = CPP_EOF;
525 break;
526 }
93c80368 527 }
711b8824 528
4ed5bcfb
NB
529 /* Drop trailing padding. */
530 while (arg->count > 0 && arg->first[arg->count - 1]->type == CPP_PADDING)
531 arg->count--;
532
bdcbe496
NB
533 /* Commit the memory used to store the arguments. We make the last
534 argument a CPP_EOF, so that it terminates macro pre-expansion,
535 but it is not included in arg->count. */
4ed5bcfb
NB
536 arg->first[arg->count] = &pfile->eof;
537 POOL_COMMIT (&pfile->argument_pool, (arg->count + 1) * sizeof (cpp_token *));
93c80368 538 return result;
711b8824
ZW
539}
540
93c80368
NB
541/* Parse the arguments making up a macro invocation. */
542static macro_arg *
543parse_args (pfile, node)
544 cpp_reader *pfile;
545 const cpp_hashnode *node;
546{
547 cpp_macro *macro = node->value.macro;
548 macro_arg *args, *cur;
549 enum cpp_ttype type;
550 int argc, error = 0;
551
552 /* Allocate room for at least one argument, and zero it out. */
553 argc = macro->paramc ? macro->paramc: 1;
554 args = xcnewvec (macro_arg, argc);
555
556 for (cur = args, argc = 0; ;)
557 {
558 argc++;
559
28e0f040 560 type = parse_arg (pfile, cur, argc == macro->paramc && macro->variadic);
93c80368
NB
561 if (type == CPP_CLOSE_PAREN || type == CPP_EOF)
562 break;
563
564 /* Re-use the last argument for excess arguments. */
565 if (argc < macro->paramc)
566 cur++;
567 }
568
569 if (type == CPP_EOF)
570 {
571 cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
a28c5035 572 NODE_NAME (node));
93c80368
NB
573 error = 1;
574 }
575 else if (argc < macro->paramc)
576 {
577 /* As an extension, a rest argument is allowed to not appear in
578 the invocation at all.
579 e.g. #define debug(format, args...) something
580 debug("string");
581
582 This is exactly the same as if there had been an empty rest
583 argument - debug("string", ). */
584
28e0f040 585 if (argc + 1 == macro->paramc && macro->variadic)
93c80368 586 {
7065e130 587 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
93c80368
NB
588 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
589 }
590 else
591 {
592 cpp_error (pfile,
593 "macro \"%s\" requires %u arguments, but only %u given",
a28c5035 594 NODE_NAME (node), macro->paramc, argc);
93c80368
NB
595 error = 1;
596 }
597 }
598 else if (argc > macro->paramc)
599 {
4c2b647d
NB
600 /* Empty argument to a macro taking no arguments is OK. */
601 if (argc != 1 || cur->count)
93c80368
NB
602 {
603 cpp_error (pfile,
604 "macro \"%s\" passed %u arguments, but takes just %u",
a28c5035 605 NODE_NAME (node), argc, macro->paramc);
93c80368
NB
606 error = 1;
607 }
608 }
609
610 if (error)
611 {
612 free (args);
613 args = 0;
614 }
615
616 return args;
617}
618
619static int
4ed5bcfb 620funlike_invocation_p (pfile, node)
93c80368
NB
621 cpp_reader *pfile;
622 const cpp_hashnode *node;
93c80368 623{
4ed5bcfb 624 const cpp_token *maybe_paren;
93c80368
NB
625 macro_arg *args = 0;
626
93c80368 627 pfile->state.prevent_expansion++;
5fddcffc 628 pfile->keep_tokens++;
4ed5bcfb
NB
629
630 pfile->state.parsing_args = 1;
631 do
632 maybe_paren = cpp_get_token (pfile);
633 while (maybe_paren->type == CPP_PADDING);
e8408f25 634 pfile->state.parsing_args = 2;
93c80368 635
4ed5bcfb 636 if (maybe_paren->type == CPP_OPEN_PAREN)
93c80368 637 args = parse_args (pfile, node);
bdcbe496
NB
638 else
639 {
640 _cpp_backup_tokens (pfile, 1);
641 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
642 cpp_warning (pfile,
643 "function-like macro \"%s\" must be used with arguments in traditional C",
644 NODE_NAME (node));
645 }
93c80368 646
93c80368 647 pfile->state.parsing_args = 0;
5fddcffc 648 pfile->keep_tokens--;
4ed5bcfb 649 pfile->state.prevent_expansion--;
93c80368
NB
650
651 if (args)
652 {
653 if (node->value.macro->paramc > 0)
4ed5bcfb 654 replace_args (pfile, node->value.macro, args);
93c80368
NB
655 free (args);
656 }
657
658 return args != 0;
659}
660
661/* Push the context of a macro onto the context stack. TOKEN is the
662 macro name. If we can successfully start expanding the macro,
a5c3cccd
NB
663 TOKEN is replaced with the first token of the expansion, and we
664 return non-zero. */
711b8824 665static int
29b10746 666enter_macro_context (pfile, node)
711b8824 667 cpp_reader *pfile;
29b10746 668 cpp_hashnode *node;
711b8824 669{
4ed5bcfb
NB
670 if (node->flags & NODE_BUILTIN)
671 push_token_context (pfile, NULL, builtin_macro (pfile, node), 1);
672 else
711b8824 673 {
4ed5bcfb 674 cpp_macro *macro = node->value.macro;
4c2b647d 675
4ed5bcfb
NB
676 if (!pfile->context->prev)
677 lock_pools (pfile);
93c80368 678
4ed5bcfb
NB
679 if (macro->fun_like && !funlike_invocation_p (pfile, node))
680 {
681 if (!pfile->context->prev)
682 unlock_pools (pfile);
683 return 0;
684 }
93c80368 685
4ed5bcfb
NB
686 /* Disable the macro within its expansion. */
687 macro->disabled = 1;
93c80368 688
4ed5bcfb
NB
689 if (macro->paramc == 0)
690 push_token_context (pfile, macro, macro->expansion, macro->count);
711b8824 691 }
4ed5bcfb
NB
692
693 return 1;
93c80368
NB
694}
695
4ed5bcfb
NB
696/* Take the expansion of a function-like MACRO, replacing parameters
697 with the actual arguments. Each instance is first macro-expanded,
698 unless that paramter is operated upon by the # or ## operators. */
93c80368 699static void
4ed5bcfb 700replace_args (pfile, macro, args)
93c80368
NB
701 cpp_reader *pfile;
702 cpp_macro *macro;
703 macro_arg *args;
93c80368
NB
704{
705 unsigned int i, total;
706 const cpp_token *src, *limit;
4ed5bcfb 707 const cpp_token **dest, **first;
93c80368
NB
708 macro_arg *arg;
709
93c80368
NB
710 /* First, fully macro-expand arguments, calculating the number of
711 tokens in the final expansion as we go. This ensures that the
4ed5bcfb
NB
712 possible recursive use of argument_pool is fine. The ordering of
713 the if statements below is subtle; we must handle stringification
714 before pasting. */
715 total = macro->count;
716 limit = macro->expansion + macro->count;
717
718 for (src = macro->expansion; src < limit; src++)
93c80368
NB
719 if (src->type == CPP_MACRO_ARG)
720 {
4ed5bcfb
NB
721 /* Leading and trailing padding tokens. */
722 total += 2;
723
93c80368
NB
724 /* We have an argument. If it is not being stringified or
725 pasted it is macro-replaced before insertion. */
6c53ebff 726 arg = &args[src->val.arg_no - 1];
4c2b647d 727
93c80368
NB
728 if (src->flags & STRINGIFY_ARG)
729 {
730 if (!arg->stringified)
4ed5bcfb 731 arg->stringified = stringify_arg (pfile, arg);
93c80368
NB
732 }
733 else if ((src->flags & PASTE_LEFT)
734 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
735 total += arg->count - 1;
736 else
737 {
738 if (!arg->expanded)
4ed5bcfb 739 expand_arg (pfile, arg);
93c80368
NB
740 total += arg->expanded_count - 1;
741 }
742 }
743
4ed5bcfb
NB
744 /* Now allocate space for the expansion, copy the tokens and replace
745 the arguments. */
746 first = (const cpp_token **) _cpp_pool_alloc (&pfile->argument_pool,
747 total * sizeof (cpp_token *));
748 dest = first;
93c80368
NB
749
750 for (src = macro->expansion; src < limit; src++)
4ed5bcfb
NB
751 {
752 unsigned int count;
753 const cpp_token **from, **paste_flag;
93c80368 754
4ed5bcfb
NB
755 if (src->type != CPP_MACRO_ARG)
756 {
757 *dest++ = src;
758 continue;
759 }
93c80368 760
4ed5bcfb
NB
761 paste_flag = 0;
762 arg = &args[src->val.arg_no - 1];
763 if (src->flags & STRINGIFY_ARG)
764 count = 1, from = &arg->stringified;
765 else if (src->flags & PASTE_LEFT)
766 count = arg->count, from = arg->first;
767 else if (src != macro->expansion && (src[-1].flags & PASTE_LEFT))
768 {
769 count = arg->count, from = arg->first;
770 if (dest != first)
771 {
772 /* GCC has special semantics for , ## b where b is a
773 varargs parameter: the comma disappears if b was
774 given no actual arguments (not merely if b is an
775 empty argument); otherwise the paste flag is removed. */
776 if (dest[-1]->type == CPP_COMMA
777 && macro->variadic
778 && src->val.arg_no == macro->paramc)
779 {
780 if (count == 0)
781 dest--;
782 else
783 paste_flag = dest - 1;
784 }
785 /* Remove the paste flag if the RHS is a placemarker. */
786 else if (count == 0)
787 paste_flag = dest - 1;
788 }
789 }
790 else
791 count = arg->expanded_count, from = arg->expanded;
4c2b647d 792
4ed5bcfb
NB
793 /* Padding on the left of an argument (unless RHS of ##). */
794 if (!pfile->state.in_directive
795 && src != macro->expansion && !(src[-1].flags & PASTE_LEFT))
796 *dest++ = padding_token (pfile, src);
93c80368 797
4ed5bcfb
NB
798 if (count)
799 {
800 memcpy (dest, from, count * sizeof (cpp_token *));
801 dest += count;
93c80368 802
4ed5bcfb
NB
803 /* With a non-empty argument on the LHS of ##, the last
804 token should be flagged PASTE_LEFT. */
805 if (src->flags & PASTE_LEFT)
806 paste_flag = dest - 1;
807 }
26ec42ee 808
4ed5bcfb
NB
809 /* Avoid paste on RHS (even case count == 0). */
810 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
811 *dest++ = &pfile->avoid_paste;
93c80368 812
4ed5bcfb
NB
813 /* Add a new paste flag, or remove an unwanted one. */
814 if (paste_flag)
815 {
816 cpp_token *token = _cpp_temp_token (pfile);
817 token->type = (*paste_flag)->type;
818 token->val.str = (*paste_flag)->val.str;
819 if (src->flags & PASTE_LEFT)
820 token->flags = (*paste_flag)->flags | PASTE_LEFT;
821 else
822 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
823 *paste_flag = token;
824 }
825 }
4c2b647d 826
93c80368
NB
827 /* Free the expanded arguments. */
828 for (i = 0; i < macro->paramc; i++)
4ed5bcfb
NB
829 if (args[i].expanded)
830 free (args[i].expanded);
831
832 push_ptoken_context (pfile, macro, first, dest - first);
833}
834
835/* Return a special padding token, with padding inherited from SOURCE. */
836static const cpp_token *
837padding_token (pfile, source)
838 cpp_reader *pfile;
839 const cpp_token *source;
840{
841 cpp_token *result = _cpp_temp_token (pfile);
842
843 result->type = CPP_PADDING;
844 result->val.source = source;
845 result->flags = 0;
846 return result;
847}
848
849/* Move to the next context. Create one if there is none. */
850static cpp_context *
851next_context (pfile)
852 cpp_reader *pfile;
853{
854 cpp_context *result = pfile->context->next;
855
856 if (result == 0)
711b8824 857 {
4ed5bcfb
NB
858 result = xnew (cpp_context);
859 result->prev = pfile->context;
860 result->next = 0;
861 pfile->context->next = result;
93c80368 862 }
4ed5bcfb
NB
863
864 pfile->context = result;
865 return result;
93c80368
NB
866}
867
4ed5bcfb
NB
868/* Push a list of pointers to tokens. */
869static void
870push_ptoken_context (pfile, macro, first, count)
93c80368 871 cpp_reader *pfile;
4ed5bcfb
NB
872 cpp_macro *macro;
873 const cpp_token **first;
874 unsigned int count;
93c80368
NB
875{
876 cpp_context *context = next_context (pfile);
93c80368 877
4ed5bcfb
NB
878 context->direct_p = false;
879 context->macro = macro;
880 context->first.ptoken = first;
881 context->last.ptoken = first + count;
882}
883
884/* Push a list of tokens. */
885static void
886push_token_context (pfile, macro, first, count)
887 cpp_reader *pfile;
888 cpp_macro *macro;
889 const cpp_token *first;
890 unsigned int count;
891{
892 cpp_context *context = next_context (pfile);
893
894 context->direct_p = true;
895 context->macro = macro;
896 context->first.token = first;
897 context->last.token = first + count;
93c80368
NB
898}
899
900static void
901expand_arg (pfile, arg)
902 cpp_reader *pfile;
903 macro_arg *arg;
904{
4ed5bcfb
NB
905 unsigned int capacity;
906
907 arg->expanded_count = 0;
908 if (arg->count == 0)
909 return;
93c80368
NB
910
911 /* Loop, reading in the arguments. */
4ed5bcfb
NB
912 capacity = 256;
913 arg->expanded = (const cpp_token **)
914 xmalloc (capacity * sizeof (cpp_token *));
93c80368 915
4ed5bcfb
NB
916 push_ptoken_context (pfile, NULL, arg->first, arg->count + 1);
917 for (;;)
93c80368 918 {
4ed5bcfb
NB
919 const cpp_token *token;
920
921 if (arg->expanded_count + 1 >= capacity)
711b8824 922 {
93c80368 923 capacity *= 2;
4ed5bcfb
NB
924 arg->expanded = (const cpp_token **)
925 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
93c80368 926 }
93c80368 927
4ed5bcfb 928 token = cpp_get_token (pfile);
93c80368 929
4ed5bcfb
NB
930 if (token->type == CPP_EOF)
931 break;
932
933 arg->expanded[arg->expanded_count++] = token;
934 }
935
936 /* Avoid the unlock_pools test of _cpp_pop_context. Change this to
937 call _cpp_pop_context once we remove pool locking. */
93c80368
NB
938 pfile->context = pfile->context->prev;
939}
940
941void
942_cpp_pop_context (pfile)
943 cpp_reader *pfile;
944{
4ed5bcfb
NB
945 /* Re-enable a macro when leaving its expansion. */
946 if (pfile->context->macro)
947 pfile->context->macro->disabled = 0;
93c80368 948
4ed5bcfb 949 pfile->context = pfile->context->prev;
8aaef6e0
NB
950 if (!pfile->context->prev && !pfile->state.parsing_args)
951 unlock_pools (pfile);
93c80368
NB
952}
953
7f2f1a66
NB
954/* Eternal routine to get a token. Also used nearly everywhere
955 internally, except for places where we know we can safely call
956 the lexer directly, such as lexing a directive name.
957
958 Macro expansions and directives are transparently handled,
959 including entering included files. Thus tokens are post-macro
960 expansion, and after any intervening directives. External callers
961 see CPP_EOF only at EOF. Internal callers also see it when meeting
962 a directive inside a macro call, when at the end of a directive and
963 state.in_directive is still 1, and at the end of argument
964 pre-expansion. */
4ed5bcfb
NB
965const cpp_token *
966cpp_get_token (pfile)
93c80368 967 cpp_reader *pfile;
93c80368 968{
4ed5bcfb
NB
969 const cpp_token *result;
970
29b10746 971 for (;;)
93c80368 972 {
4ed5bcfb 973 cpp_hashnode *node;
93c80368
NB
974 cpp_context *context = pfile->context;
975
93c80368 976 /* Context->prev == 0 <=> base context. */
bdcbe496 977 if (!context->prev)
4ed5bcfb
NB
978 result = _cpp_lex_token (pfile);
979 else if (context->first.token != context->last.token)
29b10746 980 {
4ed5bcfb
NB
981 if (context->direct_p)
982 result = context->first.token++;
983 else
984 result = *context->first.ptoken++;
985
986 if (result->flags & PASTE_LEFT)
ec1a23e6 987 {
4ed5bcfb
NB
988 paste_all_tokens (pfile, result);
989 if (pfile->state.in_directive)
990 continue;
991 return padding_token (pfile, result);
ec1a23e6 992 }
29b10746 993 }
93c80368
NB
994 else
995 {
bdcbe496 996 _cpp_pop_context (pfile);
4ed5bcfb
NB
997 if (pfile->state.in_directive)
998 continue;
999 return &pfile->avoid_paste;
93c80368 1000 }
93c80368 1001
4ed5bcfb 1002 if (result->type != CPP_NAME)
93c80368
NB
1003 break;
1004
4ed5bcfb
NB
1005 node = result->val.node;
1006
a5c3cccd 1007 /* Handle macros and the _Pragma operator. */
4ed5bcfb 1008 if (node->type == NT_MACRO && !(result->flags & NO_EXPAND))
93c80368 1009 {
a5c3cccd 1010 /* Macros invalidate controlling macros. */
6d18adbc 1011 pfile->mi_valid = false;
a5c3cccd 1012
4ed5bcfb 1013 if (!(node->flags & NODE_BUILTIN) && node->value.macro->disabled)
a5c3cccd 1014 {
4ed5bcfb
NB
1015 /* Flag this token as always unexpandable. */
1016 cpp_token *t = _cpp_temp_token (pfile);
1017 t->type = result->type;
1018 t->flags = result->flags | NO_EXPAND;
1019 t->val.str = result->val.str;
1020 result = t;
a5c3cccd 1021 }
4ed5bcfb
NB
1022 else if (!pfile->state.prevent_expansion
1023 && enter_macro_context (pfile, node))
bd969772 1024 {
4ed5bcfb
NB
1025 if (pfile->state.in_directive)
1026 continue;
1027 return padding_token (pfile, result);
bd969772 1028 }
93c80368 1029 }
a5c3cccd 1030
fe6c2db9
NB
1031 /* Don't interpret _Pragma within directives. The standard is
1032 not clear on this, but to me this makes most sense. */
4ed5bcfb 1033 if (node != pfile->spec_nodes.n__Pragma
fe6c2db9 1034 || pfile->state.in_directive)
93c80368 1035 break;
a5c3cccd 1036
7f2f1a66
NB
1037 /* Handle it, and loop back for another token. MI is cleared
1038 since this token came from either the lexer or a macro. */
a5c3cccd 1039 _cpp_do__Pragma (pfile);
93c80368 1040 }
4ed5bcfb
NB
1041
1042 return result;
93c80368
NB
1043}
1044
7065e130
NB
1045/* Returns true if we're expanding an object-like macro that was
1046 defined in a system header. Just checks the macro at the top of
1047 the stack. Used for diagnostic suppression. */
1048int
c691145a 1049cpp_sys_macro_p (pfile)
7065e130
NB
1050 cpp_reader *pfile;
1051{
1052 cpp_macro *macro = pfile->context->macro;
1053
c691145a 1054 return macro && macro->syshdr;
7065e130
NB
1055}
1056
93c80368
NB
1057/* Read each token in, until EOF. Directives are transparently
1058 processed. */
1059void
ef6e958a 1060cpp_scan_nooutput (pfile)
93c80368
NB
1061 cpp_reader *pfile;
1062{
4ed5bcfb
NB
1063 while (cpp_get_token (pfile)->type != CPP_EOF)
1064 ;
93c80368
NB
1065}
1066
bdcbe496
NB
1067/* Step back one (or more) tokens. Can only step mack more than 1 if
1068 they are from the lexer, and not from macro expansion. */
b528a07e 1069void
bdcbe496 1070_cpp_backup_tokens (pfile, count)
93c80368 1071 cpp_reader *pfile;
bdcbe496 1072 unsigned int count;
93c80368 1073{
bdcbe496 1074 if (pfile->context->prev == NULL)
93c80368 1075 {
bdcbe496
NB
1076 pfile->lookaheads += count;
1077 while (count--)
1078 {
1079 pfile->cur_token--;
1080 if (pfile->cur_token == pfile->cur_run->base)
1081 {
bdcbe496
NB
1082 pfile->cur_run = pfile->cur_run->prev;
1083 pfile->cur_token = pfile->cur_run->limit;
1084 }
1085 }
93c80368 1086 }
bdcbe496 1087 else
93c80368 1088 {
bdcbe496
NB
1089 if (count != 1)
1090 abort ();
4ed5bcfb
NB
1091 if (pfile->context->direct_p)
1092 pfile->context->first.token--;
1093 else
1094 pfile->context->first.ptoken--;
93c80368
NB
1095 }
1096}
711b8824 1097
93c80368
NB
1098/* #define directive parsing and handling. */
1099
618cdda7 1100/* Returns non-zero if a macro redefinition warning is required. */
93c80368 1101static int
618cdda7 1102warn_of_redefinition (pfile, node, macro2)
93c80368
NB
1103 cpp_reader *pfile;
1104 const cpp_hashnode *node;
1105 const cpp_macro *macro2;
1106{
1107 const cpp_macro *macro1;
1108 unsigned int i;
1109
618cdda7
NB
1110 /* Some redefinitions need to be warned about regardless. */
1111 if (node->flags & NODE_WARN)
1112 return 1;
93c80368 1113
618cdda7
NB
1114 if (! CPP_PEDANTIC (pfile))
1115 return 0;
1116
1117 /* Redefinition of a macro is allowed if and only if the old and new
ec5c56db 1118 definitions are the same. (6.10.3 paragraph 2). */
93c80368
NB
1119 macro1 = node->value.macro;
1120
1121 /* The quick failures. */
1122 if (macro1->count != macro2->count
1123 || macro1->paramc != macro2->paramc
1124 || macro1->fun_like != macro2->fun_like
28e0f040 1125 || macro1->variadic != macro2->variadic)
618cdda7 1126 return 1;
93c80368
NB
1127
1128 /* Check each token. */
1129 for (i = 0; i < macro1->count; i++)
1130 if (! _cpp_equiv_tokens (&macro1->expansion[i], &macro2->expansion[i]))
618cdda7 1131 return 1;
93c80368
NB
1132
1133 /* Check parameter spellings. */
1134 for (i = 0; i < macro1->paramc; i++)
1135 if (macro1->params[i] != macro2->params[i])
618cdda7 1136 return 1;
93c80368 1137
618cdda7 1138 return 0;
93c80368
NB
1139}
1140
1141/* Free the definition of hashnode H. */
711b8824
ZW
1142
1143void
1144_cpp_free_definition (h)
1145 cpp_hashnode *h;
1146{
93c80368
NB
1147 /* Macros and assertions no longer have anything to free. */
1148 h->type = NT_VOID;
1149 /* Clear builtin flag in case of redefinition. */
1150 h->flags &= ~NODE_BUILTIN;
1151}
1152
14baae01
NB
1153/* Save parameter NODE to the parameter list of macro MACRO. Returns
1154 zero on success, non-zero if the paramter is a duplicate. */
93c80368
NB
1155static int
1156save_parameter (pfile, macro, node)
1157 cpp_reader *pfile;
1158 cpp_macro *macro;
1159 cpp_hashnode *node;
1160{
1161 cpp_hashnode **dest;
1162
1163 /* Constraint 6.10.3.6 - duplicate parameter names. */
1164 if (node->arg_index)
709e9e50 1165 {
a28c5035 1166 cpp_error (pfile, "duplicate macro parameter \"%s\"", NODE_NAME (node));
93c80368
NB
1167 return 1;
1168 }
709e9e50 1169
93c80368
NB
1170 dest = &macro->params[macro->paramc];
1171
1172 /* Check we have room for the parameters. */
1173 if ((unsigned char *) (dest + 1) >= POOL_LIMIT (&pfile->macro_pool))
1174 {
1175 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_hashnode *),
1176 (unsigned char **) &macro->params);
1177 dest = &macro->params[macro->paramc];
709e9e50
NB
1178 }
1179
93c80368
NB
1180 *dest = node;
1181 node->arg_index = ++macro->paramc;
1182 return 0;
711b8824
ZW
1183}
1184
14baae01 1185/* Check the syntax of the paramters in a MACRO definition. */
93c80368
NB
1186static int
1187parse_params (pfile, macro)
711b8824 1188 cpp_reader *pfile;
93c80368 1189 cpp_macro *macro;
711b8824 1190{
93c80368 1191 unsigned int prev_ident = 0;
711b8824 1192
93c80368
NB
1193 macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool);
1194 for (;;)
711b8824 1195 {
345894b4 1196 const cpp_token *token = _cpp_lex_token (pfile);
711b8824 1197
345894b4 1198 switch (token->type)
711b8824 1199 {
93c80368
NB
1200 default:
1201 cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
345894b4 1202 cpp_token_as_text (pfile, token));
93c80368
NB
1203 return 0;
1204
711b8824 1205 case CPP_NAME:
93c80368
NB
1206 if (prev_ident)
1207 {
1208 cpp_error (pfile, "macro parameters must be comma-separated");
1209 return 0;
1210 }
1211 prev_ident = 1;
1212
345894b4 1213 if (save_parameter (pfile, macro, token->val.node))
93c80368
NB
1214 return 0;
1215 continue;
711b8824 1216
93c80368
NB
1217 case CPP_CLOSE_PAREN:
1218 if (prev_ident || macro->paramc == 0)
711b8824 1219 break;
711b8824 1220
93c80368
NB
1221 /* Fall through to pick up the error. */
1222 case CPP_COMMA:
1223 if (!prev_ident)
1224 {
1225 cpp_error (pfile, "parameter name missing");
1226 return 0;
1227 }
1228 prev_ident = 0;
711b8824
ZW
1229 continue;
1230
93c80368 1231 case CPP_ELLIPSIS:
28e0f040 1232 macro->variadic = 1;
93c80368
NB
1233 if (!prev_ident)
1234 {
1235 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1236 pfile->state.va_args_ok = 1;
1237 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1238 cpp_pedwarn (pfile,
28e0f040 1239 "anonymous variadic macros were introduced in C99");
93c80368
NB
1240 }
1241 else if (CPP_OPTION (pfile, pedantic))
28e0f040 1242 cpp_pedwarn (pfile, "ISO C does not permit named variadic macros");
711b8824 1243
93c80368 1244 /* We're at the end, and just expect a closing parenthesis. */
345894b4
NB
1245 token = _cpp_lex_token (pfile);
1246 if (token->type == CPP_CLOSE_PAREN)
93c80368
NB
1247 break;
1248 /* Fall through. */
711b8824 1249
93c80368
NB
1250 case CPP_EOF:
1251 cpp_error (pfile, "missing ')' in macro parameter list");
1252 return 0;
711b8824
ZW
1253 }
1254
93c80368
NB
1255 /* Success. Commit the parameter array. */
1256 POOL_COMMIT (&pfile->macro_pool,
1257 macro->paramc * sizeof (cpp_hashnode *));
1258 return 1;
711b8824 1259 }
93c80368
NB
1260}
1261
14baae01 1262/* Allocate room for a token from a macro's replacement list. */
93c80368 1263static cpp_token *
14baae01 1264alloc_expansion_token (pfile, macro)
93c80368
NB
1265 cpp_reader *pfile;
1266 cpp_macro *macro;
1267{
1268 cpp_token *token = &macro->expansion[macro->count];
711b8824 1269
93c80368
NB
1270 /* Check we have room for the token. */
1271 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
711b8824 1272 {
93c80368
NB
1273 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1274 (unsigned char **) &macro->expansion);
1275 token = &macro->expansion[macro->count];
711b8824
ZW
1276 }
1277
93c80368 1278 macro->count++;
14baae01
NB
1279 return token;
1280}
1281
1282static cpp_token *
1283lex_expansion_token (pfile, macro)
1284 cpp_reader *pfile;
1285 cpp_macro *macro;
1286{
1287 cpp_token *token;
1288
1289 pfile->cur_token = alloc_expansion_token (pfile, macro);
1290 token = _cpp_lex_direct (pfile);
93c80368
NB
1291
1292 /* Is this an argument? */
1293 if (token->type == CPP_NAME && token->val.node->arg_index)
1294 {
1295 token->type = CPP_MACRO_ARG;
6c53ebff 1296 token->val.arg_no = token->val.node->arg_index;
93c80368
NB
1297 }
1298 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1299 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1300 check_trad_stringification (pfile, macro, &token->val.str);
1301
1302 return token;
711b8824
ZW
1303}
1304
1305/* Parse a macro and save its expansion. Returns non-zero on success. */
1306int
93c80368 1307_cpp_create_definition (pfile, node)
711b8824 1308 cpp_reader *pfile;
93c80368 1309 cpp_hashnode *node;
711b8824 1310{
93c80368 1311 cpp_macro *macro;
14baae01
NB
1312 cpp_token *token, *saved_cur_token;
1313 const cpp_token *ctoken;
93c80368
NB
1314 unsigned int i, ok = 1;
1315
1316 macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool,
1317 sizeof (cpp_macro));
50410426 1318 macro->line = pfile->directive_line;
93c80368
NB
1319 macro->params = 0;
1320 macro->paramc = 0;
28e0f040 1321 macro->variadic = 0;
93c80368 1322 macro->count = 0;
14baae01 1323 macro->fun_like = 0;
93c80368
NB
1324
1325 /* Get the first token of the expansion (or the '(' of a
1326 function-like macro). */
14baae01
NB
1327 ctoken = _cpp_lex_token (pfile);
1328
1329 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
93c80368
NB
1330 {
1331 if (!(ok = parse_params (pfile, macro)))
14baae01 1332 goto cleanup2;
93c80368 1333 macro->fun_like = 1;
93c80368 1334 }
14baae01 1335 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
93c80368 1336 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
711b8824 1337
93c80368 1338 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
14baae01
NB
1339 saved_cur_token = pfile->cur_token;
1340 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1341
1342 if (macro->fun_like)
1343 token = lex_expansion_token (pfile, macro);
1344 else
1345 {
1346 token = alloc_expansion_token (pfile, macro);
1347 *token = *ctoken;
1348 }
711b8824 1349
93c80368
NB
1350 for (;;)
1351 {
1352 /* Check the stringifying # constraint 6.10.3.2.1 of
1353 function-like macros when lexing the subsequent token. */
1354 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1355 {
1356 if (token->type == CPP_MACRO_ARG)
1357 {
1358 token->flags &= ~PREV_WHITE;
1359 token->flags |= STRINGIFY_ARG;
1360 token->flags |= token[-1].flags & PREV_WHITE;
1361 token[-1] = token[0];
1362 macro->count--;
1363 }
1364 /* Let assembler get away with murder. */
bdb05a7b 1365 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
93c80368
NB
1366 {
1367 ok = 0;
1368 cpp_error (pfile, "'#' is not followed by a macro parameter");
14baae01 1369 goto cleanup1;
93c80368
NB
1370 }
1371 }
1372
1373 if (token->type == CPP_EOF)
1374 break;
1375
1376 /* Paste operator constraint 6.10.3.3.1. */
1377 if (token->type == CPP_PASTE)
1378 {
1379 /* Token-paste ##, can appear in both object-like and
1380 function-like macros, but not at the ends. */
1381 if (--macro->count > 0)
1382 token = lex_expansion_token (pfile, macro);
1383
1384 if (macro->count == 0 || token->type == CPP_EOF)
1385 {
1386 ok = 0;
1387 cpp_error (pfile,
1388 "'##' cannot appear at either end of a macro expansion");
14baae01 1389 goto cleanup1;
93c80368 1390 }
711b8824 1391
93c80368 1392 token[-1].flags |= PASTE_LEFT;
93c80368
NB
1393 }
1394
1395 token = lex_expansion_token (pfile, macro);
1396 }
1397
4c2b647d
NB
1398 /* Don't count the CPP_EOF. */
1399 macro->count--;
93c80368 1400
44ed91a1
NB
1401 /* Implement the macro-defined-to-itself optimisation. */
1402 macro->disabled = (macro->count == 1 && !macro->fun_like
1403 && macro->expansion[0].type == CPP_NAME
1404 && macro->expansion[0].val.node == node);
1405
7065e130 1406 /* To suppress some diagnostics. */
47d89cf3 1407 macro->syshdr = pfile->map->sysp != 0;
7065e130 1408
93c80368
NB
1409 /* Commit the memory. */
1410 POOL_COMMIT (&pfile->macro_pool, macro->count * sizeof (cpp_token));
1411
93c80368 1412 if (node->type != NT_VOID)
711b8824 1413 {
618cdda7 1414 if (warn_of_redefinition (pfile, node, macro))
711b8824 1415 {
50410426 1416 cpp_pedwarn_with_line (pfile, pfile->directive_line, 0,
a28c5035 1417 "\"%s\" redefined", NODE_NAME (node));
93c80368 1418
618cdda7 1419 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
14baae01 1420 cpp_pedwarn_with_line (pfile, node->value.macro->line, 0,
711b8824
ZW
1421 "this is the location of the previous definition");
1422 }
93c80368 1423 _cpp_free_definition (node);
711b8824
ZW
1424 }
1425
1426 /* Enter definition in hash table. */
93c80368
NB
1427 node->type = NT_MACRO;
1428 node->value.macro = macro;
a28c5035 1429 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
618cdda7 1430 node->flags |= NODE_WARN;
711b8824 1431
14baae01
NB
1432 cleanup1:
1433
1434 /* Set type for SEEN_EOL() in cpplib.c, restore the lexer position. */
1435 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1436 pfile->cur_token = saved_cur_token;
1437
1438 cleanup2:
93c80368
NB
1439
1440 /* Stop the lexer accepting __VA_ARGS__. */
1441 pfile->state.va_args_ok = 0;
1442
1443 /* Clear the fast argument lookup indices. */
1444 for (i = macro->paramc; i-- > 0; )
1445 macro->params[i]->arg_index = 0;
1446
1447 return ok;
711b8824
ZW
1448}
1449
e1aa5140
KG
1450/* Warn if a token in `string' matches one of the function macro
1451 arguments in `info'. This function assumes that the macro is a
1452 function macro and not an object macro. */
1453static void
93c80368 1454check_trad_stringification (pfile, macro, string)
e1aa5140 1455 cpp_reader *pfile;
93c80368 1456 const cpp_macro *macro;
e1aa5140
KG
1457 const cpp_string *string;
1458{
93c80368 1459 unsigned int i, len;
e1aa5140
KG
1460 const U_CHAR *p, *q, *limit = string->text + string->len;
1461
1462 /* Loop over the string. */
1463 for (p = string->text; p < limit; p = q)
1464 {
e1aa5140 1465 /* Find the start of an identifier. */
61c16b10
GM
1466 while (p < limit && !is_idstart (*p))
1467 p++;
e1aa5140
KG
1468
1469 /* Find the end of the identifier. */
1470 q = p;
61c16b10
GM
1471 while (q < limit && is_idchar (*q))
1472 q++;
93c80368
NB
1473
1474 len = q - p;
1475
e1aa5140
KG
1476 /* Loop over the function macro arguments to see if the
1477 identifier inside the string matches one of them. */
93c80368
NB
1478 for (i = 0; i < macro->paramc; i++)
1479 {
1480 const cpp_hashnode *node = macro->params[i];
e1aa5140 1481
2a967f3d
NB
1482 if (NODE_LEN (node) == len
1483 && !memcmp (p, NODE_NAME (node), len))
e1aa5140
KG
1484 {
1485 cpp_warning (pfile,
93c80368 1486 "macro argument \"%s\" would be stringified with -traditional.",
a28c5035 1487 NODE_NAME (node));
e1aa5140
KG
1488 break;
1489 }
1490 }
1491 }
1492}
93c80368 1493
7096171b
NB
1494/* Returns the name, arguments and expansion of a macro, in a format
1495 suitable to be read back in again, and therefore also for DWARF 2
1496 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1497 Caller is expected to generate the "#define" bit if needed. The
93c80368
NB
1498 returned text is temporary, and automatically freed later. */
1499
1500const unsigned char *
1501cpp_macro_definition (pfile, node)
1502 cpp_reader *pfile;
1503 const cpp_hashnode *node;
1504{
1505 unsigned int i, len;
1506 const cpp_macro *macro = node->value.macro;
1507 unsigned char *buffer;
1508
1509 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1510 {
7096171b 1511 cpp_ice (pfile, "invalid hash type %d in cpp_macro_definition", node->type);
93c80368
NB
1512 return 0;
1513 }
1514
1515 /* Calculate length. */
7096171b 1516 len = NODE_LEN (node) + 1; /* ' ' */
93c80368
NB
1517 if (macro->fun_like)
1518 {
7096171b
NB
1519 len += 3; /* "()" plus possible final "." of named
1520 varargs (we have + 2 below). */
93c80368 1521 for (i = 0; i < macro->paramc; i++)
a28c5035 1522 len += NODE_LEN (macro->params[i]) + 2; /* ", " */
93c80368
NB
1523 }
1524
4c2b647d 1525 for (i = 0; i < macro->count; i++)
93c80368 1526 {
4c2b647d 1527 cpp_token *token = &macro->expansion[i];
93c80368 1528
4c2b647d 1529 if (token->type == CPP_MACRO_ARG)
a28c5035 1530 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
4c2b647d
NB
1531 else
1532 len += cpp_token_len (token); /* Includes room for ' '. */
1533 if (token->flags & STRINGIFY_ARG)
1534 len++; /* "#" */
1535 if (token->flags & PASTE_LEFT)
1536 len += 3; /* " ##" */
93c80368
NB
1537 }
1538
1539 if (len > pfile->macro_buffer_len)
4b49c365
AO
1540 {
1541 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1542 pfile->macro_buffer_len = len;
1543 }
7096171b
NB
1544
1545 /* Fill in the buffer. Start with the macro name. */
93c80368 1546 buffer = pfile->macro_buffer;
7096171b
NB
1547 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1548 buffer += NODE_LEN (node);
93c80368
NB
1549
1550 /* Parameter names. */
1551 if (macro->fun_like)
1552 {
1553 *buffer++ = '(';
1554 for (i = 0; i < macro->paramc; i++)
1555 {
1556 cpp_hashnode *param = macro->params[i];
1557
1558 if (param != pfile->spec_nodes.n__VA_ARGS__)
1559 {
a28c5035
NB
1560 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1561 buffer += NODE_LEN (param);
93c80368
NB
1562 }
1563
1564 if (i + 1 < macro->paramc)
1565 *buffer++ = ',', *buffer++ = ' ';
28e0f040 1566 else if (macro->variadic)
93c80368
NB
1567 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1568 }
1569 *buffer++ = ')';
1570 }
1571
1572 /* Expansion tokens. */
4c2b647d 1573 if (macro->count)
93c80368
NB
1574 {
1575 *buffer++ = ' ';
1576 for (i = 0; i < macro->count; i++)
1577 {
1578 cpp_token *token = &macro->expansion[i];
1579
1580 if (token->flags & PREV_WHITE)
1581 *buffer++ = ' ';
1582 if (token->flags & STRINGIFY_ARG)
1583 *buffer++ = '#';
1584
1585 if (token->type == CPP_MACRO_ARG)
1586 {
a28c5035
NB
1587 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1588 memcpy (buffer,
1589 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
93c80368
NB
1590 buffer += len;
1591 }
1592 else
1593 buffer = cpp_spell_token (pfile, token, buffer);
1594
1595 if (token->flags & PASTE_LEFT)
1596 {
1597 *buffer++ = ' ';
1598 *buffer++ = '#';
1599 *buffer++ = '#';
1600 /* Next has PREV_WHITE; see _cpp_create_definition. */
1601 }
1602 }
1603 }
1604
1605 *buffer = '\0';
1606 return pfile->macro_buffer;
1607}