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