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