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