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