blob: 43a9010f141a63a40bb6b4d5f72d58e6591620cd [file] [log] [blame]
Neil Booth93c803682000-10-28 17:59:06 +00001/* Part of CPP library. (Macro and #define handling.)
Zack Weinberg711b8822000-07-18 00:59:49 +00002 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"
Neil Booth93c803682000-10-28 17:59:06 +000028#include "intl.h" /* for _("<command line>") below. */
Zack Weinberg711b8822000-07-18 00:59:49 +000029#include "cpplib.h"
30#include "cpphash.h"
31
Neil Booth510fbf82000-10-31 19:59:44 +000032#ifndef STDC_0_IN_SYSTEM_HEADERS
33#define STDC_0_IN_SYSTEM_HEADERS 0 /* Boolean macro. */
34#endif
35
Neil Booth93c803682000-10-28 17:59:06 +000036struct cpp_macro
Zack Weinberg711b8822000-07-18 00:59:49 +000037{
Neil Booth93c803682000-10-28 17:59:06 +000038 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. */
Zack Weinberg711b8822000-07-18 00:59:49 +000047};
48
Neil Booth93c803682000-10-28 17:59:06 +000049typedef 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. */
57};
Zack Weinberg711b8822000-07-18 00:59:49 +000058
Neil Booth93c803682000-10-28 17:59:06 +000059/* Macro expansion. */
60
61static void lock_pools PARAMS ((cpp_reader *));
62static void unlock_pools PARAMS ((cpp_reader *));
63static int enter_macro_context PARAMS ((cpp_reader *, cpp_token *));
64static void builtin_macro PARAMS ((cpp_reader *, cpp_token *));
65static cpp_context *push_arg_context PARAMS ((cpp_reader *, macro_arg *));
66static enum cpp_ttype parse_arg PARAMS ((cpp_reader *, macro_arg *, int));
67static macro_arg *parse_args PARAMS ((cpp_reader *, const cpp_hashnode *));
68static cpp_context *next_context PARAMS ((cpp_reader *));
69static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
70static unsigned char *quote_string PARAMS ((unsigned char *,
71 const unsigned char *,
72 unsigned int));
73static void make_string_token PARAMS ((cpp_pool *, cpp_token *,
74 const U_CHAR *, unsigned int));
75static void make_number_token PARAMS ((cpp_reader *, cpp_token *, int));
76static void stringify_arg PARAMS ((cpp_reader *, macro_arg *));
77static void paste_all_tokens PARAMS ((cpp_reader *, cpp_token *));
78static void paste_payloads PARAMS ((cpp_reader *, cpp_token *,
79 const cpp_token *));
80static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *,
81 struct toklist *));
82static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *,
83 struct toklist *));
84
85/* Lookaheads. */
86
87static void save_lookahead_token PARAMS ((cpp_reader *, const cpp_token *));
88static void take_lookahead_token PARAMS ((cpp_reader *, cpp_token *));
89static void release_lookahead PARAMS ((cpp_reader *));
90static cpp_lookahead *alloc_lookahead PARAMS ((cpp_reader *));
91static void free_lookahead PARAMS ((cpp_lookahead *));
92
93/* #define directive parsing and handling. */
94
95static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
96static int check_macro_redefinition PARAMS ((cpp_reader *,
97 const cpp_hashnode *,
98 const cpp_macro *));
99static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
100static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +0000101static void check_trad_stringification PARAMS ((cpp_reader *,
Neil Booth93c803682000-10-28 17:59:06 +0000102 const cpp_macro *,
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +0000103 const cpp_string *));
Zack Weinberg711b8822000-07-18 00:59:49 +0000104
Neil Booth93c803682000-10-28 17:59:06 +0000105/* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a
106 CPP_STRING token containing TEXT in quoted form. */
107static void
108make_string_token (pool, token, text, len)
109 cpp_pool *pool;
110 cpp_token *token;
111 const U_CHAR *text;
112 unsigned int len;
Zack Weinberg711b8822000-07-18 00:59:49 +0000113{
Neil Booth93c803682000-10-28 17:59:06 +0000114 U_CHAR *buf = _cpp_pool_alloc (pool, len * 4);
Zack Weinberg711b8822000-07-18 00:59:49 +0000115
Neil Booth93c803682000-10-28 17:59:06 +0000116 token->type = CPP_STRING;
117 token->val.str.text = buf;
118 token->val.str.len = quote_string (buf, text, len) - buf;
119 token->flags = 0;
120}
121
122/* Allocates and converts a temporary token to a CPP_NUMBER token,
123 evaluating to NUMBER. */
124static void
125make_number_token (pfile, token, number)
126 cpp_reader *pfile;
127 cpp_token *token;
128 int number;
129{
130 unsigned char *buf = _cpp_pool_alloc (pfile->string_pool, 20);
131
132 sprintf ((char *) buf, "%d", number);
133 token->type = CPP_NUMBER;
134 token->val.str.text = buf;
135 token->val.str.len = ustrlen (buf);
136 token->flags = 0;
137}
138
139static const char * const monthnames[] =
140{
141 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
142 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
143};
144
145/* Handle builtin macros like __FILE__. */
146static void
147builtin_macro (pfile, token)
148 cpp_reader *pfile;
149 cpp_token *token;
150{
151 unsigned char flags = token->flags & PREV_WHITE;
152 cpp_hashnode *node = token->val.node;
153 cpp_buffer *ip;
154
155 switch (node->value.builtin)
156 {
157 case BT_FILE:
158 case BT_BASE_FILE:
Zack Weinberg711b8822000-07-18 00:59:49 +0000159 {
Neil Booth93c803682000-10-28 17:59:06 +0000160 const char *file;
161
162 ip = CPP_BUFFER (pfile);
163 if (ip == 0)
164 file = "";
165 else
166 {
167 if (node->value.builtin == BT_BASE_FILE)
168 while (CPP_PREV_BUFFER (ip) != NULL)
169 ip = CPP_PREV_BUFFER (ip);
170
171 file = ip->nominal_fname;
172 }
173 make_string_token (pfile->string_pool, token,
174 (U_CHAR *) file, strlen (file));
Zack Weinberg711b8822000-07-18 00:59:49 +0000175 }
Neil Booth93c803682000-10-28 17:59:06 +0000176 break;
177
178 case BT_INCLUDE_LEVEL:
179 /* pfile->include_depth counts the primary source as level 1,
180 but historically __INCLUDE_DEPTH__ has called the primary
181 source level 0. */
182 make_number_token (pfile, token, pfile->include_depth - 1);
183 break;
184
185 case BT_SPECLINE:
186 /* If __LINE__ is embedded in a macro, it must expand to the
187 line of the macro's invocation, not its definition.
188 Otherwise things like assert() will not work properly. */
189 make_number_token (pfile, token, cpp_get_line (pfile)->line);
190 break;
191
192 case BT_STDC:
193 {
194 int stdc = 1;
195
Neil Booth510fbf82000-10-31 19:59:44 +0000196 if (STDC_0_IN_SYSTEM_HEADERS && CPP_IN_SYSTEM_HEADER (pfile)
Mark Mitchell8a479782000-10-30 08:41:23 +0000197 && pfile->spec_nodes.n__STRICT_ANSI__->type == NT_VOID)
Neil Booth93c803682000-10-28 17:59:06 +0000198 stdc = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000199 make_number_token (pfile, token, stdc);
200 }
201 break;
202
203 case BT_DATE:
204 case BT_TIME:
205 if (pfile->date.type == CPP_EOF)
206 {
207 /* Allocate __DATE__ and __TIME__ from permanent storage,
208 and save them in pfile so we don't have to do this again.
209 We don't generate these strings at init time because
210 time() and localtime() are very slow on some systems. */
211 time_t tt = time (NULL);
212 struct tm *tb = localtime (&tt);
213
214 make_string_token (&pfile->ident_pool, &pfile->date,
215 DSC("Oct 11 1347"));
216 make_string_token (&pfile->ident_pool, &pfile->time,
217 DSC("12:34:56"));
218
219 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
220 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
221 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
222 tb->tm_hour, tb->tm_min, tb->tm_sec);
223 }
224 *token = node->value.builtin == BT_DATE ? pfile->date: pfile->time;
225 break;
226
227 default:
228 cpp_ice (pfile, "invalid builtin macro \"%s\"", node->name);
229 break;
230 }
231
232 token->flags = flags;
233}
234
235/* Used by cpperror.c to obtain the correct line and column to report
236 in a diagnostic. */
237const cpp_lexer_pos *
238cpp_get_line (pfile)
239 cpp_reader *pfile;
240{
241 /* Within a macro expansion, return the position of the outermost
242 invocation. */
243 if (pfile->context->prev)
244 return &pfile->macro_pos;
245 return &pfile->lexer_pos;
246}
247
248static void
249lock_pools (pfile)
250 cpp_reader *pfile;
251{
252 _cpp_lock_pool (&pfile->temp_string_pool);
253 _cpp_lock_pool (&pfile->argument_pool);
254}
255
256static void
257unlock_pools (pfile)
258 cpp_reader *pfile;
259{
260 _cpp_unlock_pool (&pfile->temp_string_pool);
261 _cpp_unlock_pool (&pfile->argument_pool);
262}
263
264static void
265paste_payloads (pfile, lhs, rhs)
266 cpp_reader *pfile;
267 cpp_token *lhs;
268 const cpp_token *rhs;
269{
270 unsigned int total_len = cpp_token_len (lhs) + cpp_token_len (rhs);
271 unsigned char *result, *end;
272 cpp_pool *pool;
273
274 pool = lhs->type == CPP_NAME ? &pfile->ident_pool: pfile->string_pool;
275 result = _cpp_pool_alloc (pool, total_len + 1);
276
277 /* Paste the spellings and null terminate. */
278 end = cpp_spell_token (pfile, rhs, cpp_spell_token (pfile, lhs, result));
279 *end = '\0';
280 total_len = end - result;
281
282 if (lhs->type == CPP_NAME)
283 {
284 lhs->val.node = cpp_lookup (pfile, result, total_len);
285 if (lhs->val.node->flags & NODE_OPERATOR)
286 {
287 lhs->flags |= NAMED_OP;
288 lhs->type = lhs->val.node->value.operator;
289 }
290 }
291 else
292 {
293 lhs->val.str.text = result;
294 lhs->val.str.len = total_len;
295 }
296}
297
298/* Adds backslashes before all backslashes and double quotes appearing
299 in strings. Non-printable characters are converted to octal. */
300static U_CHAR *
301quote_string (dest, src, len)
302 U_CHAR *dest;
303 const U_CHAR *src;
304 unsigned int len;
305{
306 while (len--)
307 {
308 U_CHAR c = *src++;
309
310 if (c == '\\' || c == '"')
311 {
312 *dest++ = '\\';
313 *dest++ = c;
314 }
315 else
316 {
317 if (ISPRINT (c))
318 *dest++ = c;
319 else
320 {
321 sprintf ((char *) dest, "\\%03o", c);
322 dest += 4;
323 }
324 }
325 }
326
327 return dest;
328}
329
330/* Convert a token sequence to a single string token according to the
331 rules of the ISO C #-operator. */
332static void
333stringify_arg (pfile, arg)
334 cpp_reader *pfile;
335 macro_arg *arg;
336{
337 cpp_pool *pool = pfile->string_pool;
338 unsigned char *start = POOL_FRONT (pool);
339 unsigned int i, escape_it, total_len = 0, backslash_count = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000340
341 /* Loop, reading in the argument's tokens. */
342 for (i = 0; i < arg->count; i++)
343 {
344 unsigned char *dest;
345 const cpp_token *token = &arg->first[i];
346 unsigned int len = cpp_token_len (token);
347
348 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
349 || token->type == CPP_CHAR || token->type == CPP_WCHAR
350 || token->type == CPP_OSTRING);
351
352 if (escape_it)
353 /* Worst case is each char is octal. */
354 len *= 4;
355 len++; /* Room for initial space. */
356
357 dest = &start[total_len];
358 if (dest + len > POOL_LIMIT (pool))
359 {
360 _cpp_next_chunk (pool, len, (unsigned char **) &start);
361 dest = &start[total_len];
362 }
363
Neil Booth93c803682000-10-28 17:59:06 +0000364 /* No leading white space. */
Neil Booth4c2b6472000-11-11 13:19:01 +0000365 if (token->flags & PREV_WHITE && total_len > 0)
366 *dest++ = ' ';
Neil Booth93c803682000-10-28 17:59:06 +0000367
368 if (escape_it)
369 {
370 unsigned char *buf = (unsigned char *) xmalloc (len);
371
372 len = cpp_spell_token (pfile, token, buf) - buf;
373 dest = quote_string (dest, buf, len);
374 free (buf);
375 }
376 else
377 dest = cpp_spell_token (pfile, token, dest);
378 total_len = dest - start;
379
Neil Booth6c53ebf2000-11-06 18:43:32 +0000380 if (token->type == CPP_OTHER && token->val.c == '\\')
Neil Booth93c803682000-10-28 17:59:06 +0000381 backslash_count++;
382 else
383 backslash_count = 0;
384 }
385
386 /* Ignore the final \ of invalid string literals. */
387 if (backslash_count & 1)
388 {
389 cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
390 total_len--;
391 }
392
393 POOL_COMMIT (pool, total_len);
394
395 arg->stringified = xnew (cpp_token);
396 arg->stringified->flags = 0;
397 arg->stringified->type = CPP_STRING;
398 arg->stringified->val.str.text = start;
399 arg->stringified->val.str.len = total_len;
400}
401
402/* Handles an arbitrarily long sequence of ## operators. This
403 implementation is left-associative, non-recursive, and finishes a
404 paste before handling succeeding ones. If the paste fails, the
405 right hand side of the ## operator is placed in the then-current
406 context's lookahead buffer, with the effect that it appears in the
407 output stream normally. */
408static void
409paste_all_tokens (pfile, lhs)
410 cpp_reader *pfile;
411 cpp_token *lhs;
412{
413 unsigned char orig_flags = lhs->flags;
414 cpp_token *rhs;
415
416 do
417 {
Neil Booth4c2b6472000-11-11 13:19:01 +0000418 int digraph = 0;
419 enum cpp_ttype type;
420
Neil Booth93c803682000-10-28 17:59:06 +0000421 /* Take the token directly from the current context. We can do
422 this, because we are in the replacement list of either an
423 object-like macro, or a function-like macro with arguments
424 inserted. In either case, the constraints to #define
425 guarantee we have at least one more token (empty arguments
426 become placemarkers). */
427 rhs = pfile->context->list.first++;
428
Neil Booth4c2b6472000-11-11 13:19:01 +0000429 type = cpp_can_paste (pfile, lhs, rhs, &digraph);
430 if (type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000431 {
Neil Booth4c2b6472000-11-11 13:19:01 +0000432 if (CPP_OPTION (pfile, warn_paste))
433 cpp_warning (pfile,
434 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
435 cpp_token_as_text (pfile, lhs),
436 cpp_token_as_text (pfile, rhs));
437
438 /* The standard states that behaviour is undefined. By the
439 principle of least surpise, we step back before the RHS,
440 and mark it to prevent macro expansion. Tests in the
441 testsuite rely on clearing PREV_WHITE here, though you
442 could argue we should actually set it. */
443 rhs->flags &= ~PREV_WHITE;
444 rhs->flags |= NO_EXPAND;
445
446 /* Step back so we read the RHS in next. */
447 pfile->context->list.first--;
448 break;
Neil Booth93c803682000-10-28 17:59:06 +0000449 }
Neil Booth93c803682000-10-28 17:59:06 +0000450
Neil Booth4c2b6472000-11-11 13:19:01 +0000451 lhs->type = type;
452 lhs->flags &= ~DIGRAPH;
453 if (digraph)
454 lhs->flags |= DIGRAPH;
Neil Booth93c803682000-10-28 17:59:06 +0000455
Neil Booth4c2b6472000-11-11 13:19:01 +0000456 if (type == CPP_NAME || type == CPP_NUMBER)
457 paste_payloads (pfile, lhs, rhs);
458 else if (type == CPP_WCHAR || type == CPP_WSTRING)
459 lhs->val.str = rhs->val.str;
Neil Booth93c803682000-10-28 17:59:06 +0000460 }
461 while (rhs->flags & PASTE_LEFT);
462
463 /* The pasted token has the PREV_WHITE flag of the LHS, is no longer
464 PASTE_LEFT, and is subject to macro expansion. */
465 lhs->flags &= ~(PREV_WHITE | PASTE_LEFT | NO_EXPAND);
466 lhs->flags |= orig_flags & PREV_WHITE;
467}
468
Neil Booth4c2b6472000-11-11 13:19:01 +0000469/* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS
470 is non-zero if this is a variable argument. Returns the type of
471 the token that caused reading to finish. */
Neil Booth93c803682000-10-28 17:59:06 +0000472static enum cpp_ttype
473parse_arg (pfile, arg, var_args)
474 cpp_reader *pfile;
475 struct macro_arg *arg;
476 int var_args;
477{
478 enum cpp_ttype result;
479 unsigned int paren = 0;
480
481 arg->first = (cpp_token *) POOL_FRONT (&pfile->argument_pool);
482 for (;; arg->count++)
483 {
484 cpp_token *token = &arg->first[arg->count];
485 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->argument_pool))
486 {
487 _cpp_next_chunk (&pfile->argument_pool, sizeof (cpp_token),
488 (unsigned char **) &arg->first);
489 token = &arg->first[arg->count];
490 }
491
492 _cpp_get_token (pfile, token);
493 result = token->type;
494
495 if (result == CPP_OPEN_PAREN)
496 paren++;
497 else if (result == CPP_CLOSE_PAREN && paren-- == 0)
498 break;
499 /* Commas are not terminators within parantheses or var_args. */
500 else if (result == CPP_COMMA && paren == 0 && !var_args)
501 break;
502 else if (result == CPP_EOF)
503 break; /* Error reported by caller. */
Neil Booth93c803682000-10-28 17:59:06 +0000504 }
505
Neil Booth93c803682000-10-28 17:59:06 +0000506 /* Commit the memory used to store the arguments. */
507 POOL_COMMIT (&pfile->argument_pool, arg->count * sizeof (cpp_token));
508
509 return result;
510}
511
512/* Parse the arguments making up a macro invocation. */
513static macro_arg *
514parse_args (pfile, node)
515 cpp_reader *pfile;
516 const cpp_hashnode *node;
517{
518 cpp_macro *macro = node->value.macro;
519 macro_arg *args, *cur;
520 enum cpp_ttype type;
521 int argc, error = 0;
522
523 /* Allocate room for at least one argument, and zero it out. */
524 argc = macro->paramc ? macro->paramc: 1;
525 args = xcnewvec (macro_arg, argc);
526
527 for (cur = args, argc = 0; ;)
528 {
529 argc++;
530
531 type = parse_arg (pfile, cur, argc == macro->paramc && macro->var_args);
532 if (type == CPP_CLOSE_PAREN || type == CPP_EOF)
533 break;
534
535 /* Re-use the last argument for excess arguments. */
536 if (argc < macro->paramc)
537 cur++;
538 }
539
540 if (type == CPP_EOF)
541 {
542 cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
543 node->name);
544 error = 1;
545 }
546 else if (argc < macro->paramc)
547 {
548 /* As an extension, a rest argument is allowed to not appear in
549 the invocation at all.
550 e.g. #define debug(format, args...) something
551 debug("string");
552
553 This is exactly the same as if there had been an empty rest
554 argument - debug("string", ). */
555
556 if (argc + 1 == macro->paramc && macro->var_args)
557 {
Neil Booth93c803682000-10-28 17:59:06 +0000558 if (CPP_OPTION (pfile, c99) && CPP_PEDANTIC (pfile))
559 cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
560 }
561 else
562 {
563 cpp_error (pfile,
564 "macro \"%s\" requires %u arguments, but only %u given",
565 node->name, macro->paramc, argc);
566 error = 1;
567 }
568 }
569 else if (argc > macro->paramc)
570 {
Neil Booth4c2b6472000-11-11 13:19:01 +0000571 /* Empty argument to a macro taking no arguments is OK. */
572 if (argc != 1 || cur->count)
Neil Booth93c803682000-10-28 17:59:06 +0000573 {
574 cpp_error (pfile,
575 "macro \"%s\" passed %u arguments, but takes just %u",
576 node->name, argc, macro->paramc);
577 error = 1;
578 }
579 }
580
581 if (error)
582 {
583 free (args);
584 args = 0;
585 }
586
587 return args;
588}
589
590static int
591funlike_invocation_p (pfile, node, list)
592 cpp_reader *pfile;
593 const cpp_hashnode *node;
594 struct toklist *list;
595{
596 cpp_context *orig_context;
597 cpp_token maybe_paren;
598 macro_arg *args = 0;
599
600 pfile->state.parsing_args = 1;
601 pfile->state.prevent_expansion++;
602 orig_context = pfile->context;
603
604 cpp_start_lookahead (pfile);
605 cpp_get_token (pfile, &maybe_paren);
606 cpp_stop_lookahead (pfile, maybe_paren.type == CPP_OPEN_PAREN);
607
608 if (maybe_paren.type == CPP_OPEN_PAREN)
609 args = parse_args (pfile, node);
610 else if (CPP_WTRADITIONAL (pfile))
611 cpp_warning (pfile,
612 "function-like macro \"%s\" must be used with arguments in traditional C",
613 node->name);
614
615 /* Restore original context. */
616 pfile->context = orig_context;
617 pfile->state.prevent_expansion--;
618 pfile->state.parsing_args = 0;
619
620 if (args)
621 {
622 if (node->value.macro->paramc > 0)
623 replace_args (pfile, node->value.macro, args, list);
624 free (args);
625 }
626
627 return args != 0;
628}
629
630/* Push the context of a macro onto the context stack. TOKEN is the
631 macro name. If we can successfully start expanding the macro,
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000632 TOKEN is replaced with the first token of the expansion, and we
633 return non-zero. */
Neil Booth93c803682000-10-28 17:59:06 +0000634static int
635enter_macro_context (pfile, token)
636 cpp_reader *pfile;
637 cpp_token *token;
638{
639 cpp_context *context;
640 cpp_macro *macro;
641 unsigned char flags;
642 struct toklist list;
643
644 macro = token->val.node->value.macro;
645 if (macro->disabled)
646 {
647 token->flags |= NO_EXPAND;
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000648 return 0;
Neil Booth93c803682000-10-28 17:59:06 +0000649 }
650
651 /* Save the position of the outermost macro invocation. */
652 if (!pfile->context->prev)
653 {
654 pfile->macro_pos = pfile->lexer_pos;
655 lock_pools (pfile);
656 }
657
658 if (macro->fun_like && !funlike_invocation_p (pfile, token->val.node, &list))
659 {
660 if (!pfile->context->prev)
661 unlock_pools (pfile);
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000662 return 0;
Neil Booth93c803682000-10-28 17:59:06 +0000663 }
664
Neil Booth93c803682000-10-28 17:59:06 +0000665 if (macro->paramc == 0)
666 {
Neil Booth4c2b6472000-11-11 13:19:01 +0000667 list.first = macro->expansion;
668 list.limit = macro->expansion + macro->count;
Neil Booth93c803682000-10-28 17:59:06 +0000669 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000670
671 /* Temporary kludge. */
672 if (list.first == list.limit)
673 return 2;
674
675 /* Now push its context. */
676 context = next_context (pfile);
677 context->list = list;
Neil Booth93c803682000-10-28 17:59:06 +0000678 context->macro = macro;
679
680 /* The first expansion token inherits the PREV_WHITE of TOKEN. */
681 flags = token->flags & PREV_WHITE;
682 *token = *context->list.first++;
683 token->flags |= flags;
684
685 /* Disable the macro within its expansion. */
686 macro->disabled = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +0000687
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000688 return 1;
Zack Weinberg711b8822000-07-18 00:59:49 +0000689}
690
Neil Booth93c803682000-10-28 17:59:06 +0000691/* Move to the next context. Create one if there is none. */
692static cpp_context *
693next_context (pfile)
694 cpp_reader *pfile;
695{
696 cpp_context *prev = pfile->context;
697 cpp_context *result = prev->next;
698
699 if (result == 0)
700 {
701 result = xnew (cpp_context);
702 prev->next = result;
703 result->prev = prev;
704 result->next = 0;
705 }
706
707 pfile->context = result;
708 return result;
709}
710
711static void
712replace_args (pfile, macro, args, list)
713 cpp_reader *pfile;
714 cpp_macro *macro;
715 macro_arg *args;
716 struct toklist *list;
717{
718 unsigned int i, total;
719 const cpp_token *src, *limit;
720 cpp_token *dest;
721 macro_arg *arg;
722
723 src = macro->expansion;
724 limit = src + macro->count;
725
726 /* First, fully macro-expand arguments, calculating the number of
727 tokens in the final expansion as we go. This ensures that the
728 possible recursive use of argument_pool is fine. */
729 total = limit - src;
730 for (; src < limit; src++)
731 if (src->type == CPP_MACRO_ARG)
732 {
733 /* We have an argument. If it is not being stringified or
734 pasted it is macro-replaced before insertion. */
Neil Booth6c53ebf2000-11-06 18:43:32 +0000735 arg = &args[src->val.arg_no - 1];
Neil Booth4c2b6472000-11-11 13:19:01 +0000736
Neil Booth93c803682000-10-28 17:59:06 +0000737 if (src->flags & STRINGIFY_ARG)
738 {
739 if (!arg->stringified)
740 stringify_arg (pfile, arg);
741 }
742 else if ((src->flags & PASTE_LEFT)
743 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
744 total += arg->count - 1;
745 else
746 {
747 if (!arg->expanded)
Neil Booth4c2b6472000-11-11 13:19:01 +0000748 {
749 arg->expanded_count = 0;
750 if (arg->count)
751 expand_arg (pfile, arg);
752 }
Neil Booth93c803682000-10-28 17:59:06 +0000753 total += arg->expanded_count - 1;
754 }
755 }
756
757 dest = (cpp_token *) _cpp_pool_alloc (&pfile->argument_pool,
758 total * sizeof (cpp_token));
759 list->first = dest;
Neil Booth93c803682000-10-28 17:59:06 +0000760
761 for (src = macro->expansion; src < limit; src++)
762 if (src->type == CPP_MACRO_ARG)
763 {
764 unsigned int count;
765 const cpp_token *from;
766
Neil Booth6c53ebf2000-11-06 18:43:32 +0000767 arg = &args[src->val.arg_no - 1];
Neil Booth93c803682000-10-28 17:59:06 +0000768 if (src->flags & STRINGIFY_ARG)
769 from = arg->stringified, count = 1;
Neil Booth4c2b6472000-11-11 13:19:01 +0000770 else if (src->flags & PASTE_LEFT)
Neil Booth93c803682000-10-28 17:59:06 +0000771 count = arg->count, from = arg->first;
Neil Booth4c2b6472000-11-11 13:19:01 +0000772 else if (src > macro->expansion && (src[-1].flags & PASTE_LEFT))
773 {
774 count = arg->count, from = arg->first;
775 if (dest != list->first)
776 {
777 /* GCC has special semantics for , ## b where b is a
778 varargs parameter: the comma disappears if b was
779 given no actual arguments (not merely if b is an
780 empty argument); otherwise pasting is turned off. */
781 if (dest[-1].type == CPP_COMMA
782 && macro->var_args
783 && src->val.arg_no == macro->paramc)
784 {
785 if (count == 0)
786 dest--;
787 else
788 dest[-1].flags &= ~PASTE_LEFT;
789 }
790 /* Count == 0 is the RHS a placemarker case. */
791 else if (count == 0)
792 dest[-1].flags &= ~PASTE_LEFT;
793 }
794 }
Neil Booth93c803682000-10-28 17:59:06 +0000795 else
796 count = arg->expanded_count, from = arg->expanded;
Neil Booth93c803682000-10-28 17:59:06 +0000797
Neil Booth4c2b6472000-11-11 13:19:01 +0000798 /* Count == 0 is the LHS a placemarker case. */
799 if (count)
800 {
801 memcpy (dest, from, count * sizeof (cpp_token));
Neil Booth93c803682000-10-28 17:59:06 +0000802
Neil Booth4c2b6472000-11-11 13:19:01 +0000803 /* The first token gets PREV_WHITE of the CPP_MACRO_ARG. */
804 dest->flags &= ~PREV_WHITE;
805 dest->flags |= src->flags & PREV_WHITE;
Neil Booth93c803682000-10-28 17:59:06 +0000806
Neil Booth4c2b6472000-11-11 13:19:01 +0000807 /* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */
808 dest[count - 1].flags |= src->flags & PASTE_LEFT;
809
810 dest += count;
811 }
Neil Booth93c803682000-10-28 17:59:06 +0000812 }
813 else
814 *dest++ = *src;
815
Neil Booth4c2b6472000-11-11 13:19:01 +0000816 list->limit = dest;
817
Neil Booth93c803682000-10-28 17:59:06 +0000818 /* Free the expanded arguments. */
819 for (i = 0; i < macro->paramc; i++)
820 {
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));
Neil Booth93c803682000-10-28 17:59:06 +0000853
854 push_arg_context (pfile, arg);
855 do
856 {
857 if (arg->expanded_count >= capacity)
858 {
859 capacity *= 2;
860 arg->expanded = (cpp_token *)
861 xrealloc (arg->expanded, capacity * sizeof (cpp_token));
862 }
863 token = &arg->expanded[arg->expanded_count++];
864 _cpp_get_token (pfile, token);
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;
881 /* Re-enable a macro and free resources when leaving its expansion. */
882 if (!pfile->state.parsing_args)
883 {
884 if (!pfile->context->prev)
885 unlock_pools (pfile);
886 context->macro->disabled = 0;
887 }
888}
889
890/* Internal routine to return a token, either from an in-progress
Neil Bootha9499412000-11-09 21:18:15 +0000891 macro expansion, or from the source file as appropriate.
892 Transparently enters included files. Handles macros, so tokens
Neil Booth4c2b6472000-11-11 13:19:01 +0000893 returned are post-expansion. Returns CPP_EOF at EOL and EOF. */
Neil Booth93c803682000-10-28 17:59:06 +0000894void
895_cpp_get_token (pfile, token)
896 cpp_reader *pfile;
897 cpp_token *token;
898{
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000899 next_token:
Neil Booth93c803682000-10-28 17:59:06 +0000900 for (;;)
901 {
902 cpp_context *context = pfile->context;
903
904 if (pfile->la_read)
905 take_lookahead_token (pfile, token);
906 /* Context->prev == 0 <=> base context. */
907 else if (!context->prev)
908 _cpp_lex_token (pfile, token);
909 else if (context->list.first != context->list.limit)
910 *token = *context->list.first++;
911 else
912 {
913 if (context->macro)
914 {
915 _cpp_pop_context (pfile);
916 continue;
917 }
Neil Bootha9499412000-11-09 21:18:15 +0000918 /* End of argument pre-expansion. */
Neil Booth93c803682000-10-28 17:59:06 +0000919 token->type = CPP_EOF;
920 token->flags = 0;
921 }
922 break;
923 }
924
925 /* Only perform macro expansion (and therefore pasting) when not
926 skipping, or when skipping but in a directive. The only
927 directive where this could be true is #elif. A possible later
928 optimisation: get do_elif to clear skipping so we don't need the
929 directive test here. */
930 if (pfile->skipping && !pfile->state.in_directive)
931 return;
932
933 for (;;)
934 {
935 if (token->flags & PASTE_LEFT)
936 paste_all_tokens (pfile, token);
937
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000938 if (token->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +0000939 break;
940
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000941 /* Handle macros and the _Pragma operator. */
942 if (token->val.node->type == NT_MACRO
943 && !pfile->state.prevent_expansion
944 && !(token->flags & NO_EXPAND))
Neil Booth93c803682000-10-28 17:59:06 +0000945 {
Neil Booth4c2b6472000-11-11 13:19:01 +0000946 int m;
947
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000948 /* Macros invalidate controlling macros. */
949 pfile->mi_state = MI_FAILED;
950
951 if (token->val.node->flags & NODE_BUILTIN)
952 {
953 builtin_macro (pfile, token);
954 break;
955 }
956
Neil Booth4c2b6472000-11-11 13:19:01 +0000957 m = enter_macro_context (pfile, token);
958 if (m == 1)
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000959 continue;
Neil Booth4c2b6472000-11-11 13:19:01 +0000960 if (m == 2)
961 goto next_token;
Neil Booth93c803682000-10-28 17:59:06 +0000962 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000963
964 if (token->val.node != pfile->spec_nodes.n__Pragma)
Neil Booth93c803682000-10-28 17:59:06 +0000965 break;
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000966
967 /* Invalidate controlling macros. */
968 pfile->mi_state = MI_FAILED;
969 _cpp_do__Pragma (pfile);
970 goto next_token;
Neil Booth93c803682000-10-28 17:59:06 +0000971 }
972}
973
974/* External interface to get a token. Tokens are returned after macro
975 expansion and directives have been handled, as a continuous stream.
Neil Bootha9499412000-11-09 21:18:15 +0000976 Compared to the function above, CPP_EOF means EOF, and placemarker
977 tokens are filtered out. Also, it skips tokens if we're skipping,
978 and saves tokens to lookahead.
Neil Booth93c803682000-10-28 17:59:06 +0000979
Neil Bootha9499412000-11-09 21:18:15 +0000980 CPP_EOF indicates end of original source file. For the benefit of
981 #pragma callbacks which may want to get the pragma's tokens,
982 returns CPP_EOF to indicate end-of-directive in this case. */
Neil Booth93c803682000-10-28 17:59:06 +0000983void
984cpp_get_token (pfile, token)
985 cpp_reader *pfile;
986 cpp_token *token;
987{
988 for (;;)
989 {
990 _cpp_get_token (pfile, token);
991
992 if (token->type == CPP_EOF)
Neil Bootha9499412000-11-09 21:18:15 +0000993 break;
Neil Booth4c2b6472000-11-11 13:19:01 +0000994 else if (pfile->skipping)
Neil Booth93c803682000-10-28 17:59:06 +0000995 continue;
996
997 /* Non-comment tokens invalidate any controlling macros. */
998 if (token->type != CPP_COMMENT)
999 pfile->mi_state = MI_FAILED;
1000
1001 break;
1002 }
1003
1004 if (pfile->la_write)
1005 save_lookahead_token (pfile, token);
1006}
1007
1008/* Read each token in, until EOF. Directives are transparently
1009 processed. */
1010void
1011cpp_scan_buffer_nooutput (pfile)
1012 cpp_reader *pfile;
1013{
1014 cpp_token token;
1015
1016 do
1017 do
1018 cpp_get_token (pfile, &token);
1019 while (token.type != CPP_EOF);
1020 while (cpp_pop_buffer (pfile) != 0);
1021}
1022
1023/* Lookahead handling. */
1024
1025static void
1026save_lookahead_token (pfile, token)
Zack Weinberg711b8822000-07-18 00:59:49 +00001027 cpp_reader *pfile;
1028 const cpp_token *token;
1029{
Neil Booth93c803682000-10-28 17:59:06 +00001030 if (token->type != CPP_EOF)
1031 {
1032 cpp_lookahead *la = pfile->la_write;
1033 cpp_token_with_pos *twp;
1034
1035 if (la->count == la->cap)
1036 {
1037 la->cap += la->cap + 8;
1038 la->tokens = (cpp_token_with_pos *)
1039 xrealloc (la->tokens, la->cap * sizeof (cpp_token_with_pos));
1040 }
1041
1042 twp = &la->tokens[la->count++];
1043 twp->token = *token;
1044 twp->pos = *cpp_get_line (pfile);
1045 }
1046}
1047
1048static void
1049take_lookahead_token (pfile, token)
1050 cpp_reader *pfile;
1051 cpp_token *token;
1052{
1053 cpp_lookahead *la = pfile->la_read;
1054 cpp_token_with_pos *twp = &la->tokens[la->cur];
1055
1056 *token = twp->token;
1057 pfile->lexer_pos = twp->pos;
1058
1059 if (++la->cur == la->count)
1060 release_lookahead (pfile);
1061}
1062
1063/* Moves the lookahead at the front of the read list to the free store. */
1064static void
1065release_lookahead (pfile)
1066 cpp_reader *pfile;
1067{
1068 cpp_lookahead *la = pfile->la_read;
1069
1070 pfile->la_read = la->next;
1071 la->next = pfile->la_unused;
1072 pfile->la_unused = la;
1073 unlock_pools (pfile);
1074}
1075
1076/* Take a new lookahead from the free store, or allocate one if none. */
1077static cpp_lookahead *
1078alloc_lookahead (pfile)
1079 cpp_reader *pfile;
1080{
1081 cpp_lookahead *la = pfile->la_unused;
1082
1083 if (la)
1084 pfile->la_unused = la->next;
1085 else
1086 {
1087 la = xnew (cpp_lookahead);
1088 la->tokens = 0;
1089 la->cap = 0;
1090 }
1091
1092 la->cur = la->count = 0;
1093 return la;
1094}
1095
1096/* Free memory associated with a lookahead list. */
1097static void
1098free_lookahead (la)
1099 cpp_lookahead *la;
1100{
1101 if (la->tokens)
1102 free ((PTR) la->tokens);
1103 free ((PTR) la);
1104}
1105
1106/* Free all the lookaheads of a cpp_reader. */
1107void
1108_cpp_free_lookaheads (pfile)
1109 cpp_reader *pfile;
1110{
1111 cpp_lookahead *la, *lan;
1112
1113 if (pfile->la_read)
1114 free_lookahead (pfile->la_read);
1115 if (pfile->la_write)
1116 free_lookahead (pfile->la_write);
1117
1118 for (la = pfile->la_unused; la; la = lan)
1119 {
1120 lan = la->next;
1121 free_lookahead (la);
1122 }
1123}
1124
1125/* Allocate a lookahead and move it to the front of the write list. */
1126void
1127cpp_start_lookahead (pfile)
1128 cpp_reader *pfile;
1129{
1130 cpp_lookahead *la = alloc_lookahead (pfile);
1131
1132 la->next = pfile->la_write;
1133 pfile->la_write = la;
1134
1135 la->pos = *cpp_get_line (pfile);
1136
1137 /* Don't allow memory pools to be re-used whilst we're reading ahead. */
1138 lock_pools (pfile);
1139}
1140
1141/* Stop reading ahead - either step back, or drop the read ahead. */
1142void
1143cpp_stop_lookahead (pfile, drop)
1144 cpp_reader *pfile;
1145 int drop;
1146{
1147 cpp_lookahead *la = pfile->la_write;
1148
1149 pfile->la_write = la->next;
1150 la->next = pfile->la_read;
1151 pfile->la_read = la;
1152
1153 if (drop || la->count == 0)
1154 release_lookahead (pfile);
1155 else
1156 pfile->lexer_pos = la->pos;
1157}
1158
1159/* Push a single token back to the front of the queue. Only to be
1160 used by cpplib, and only then when necessary. POS is the position
1161 to report for the preceding token. */
1162void
1163_cpp_push_token (pfile, token, pos)
1164 cpp_reader *pfile;
1165 const cpp_token *token;
1166 const cpp_lexer_pos *pos;
1167{
1168 cpp_start_lookahead (pfile);
1169 save_lookahead_token (pfile, token);
1170 cpp_stop_lookahead (pfile, 0);
1171 pfile->lexer_pos = *pos;
1172}
1173
1174/* #define directive parsing and handling. */
1175
1176/* Returns non-zero if a macro redefinition is trivial. */
1177static int
1178check_macro_redefinition (pfile, node, macro2)
1179 cpp_reader *pfile;
1180 const cpp_hashnode *node;
1181 const cpp_macro *macro2;
1182{
1183 const cpp_macro *macro1;
1184 unsigned int i;
1185
1186 if (node->type != NT_MACRO || node->flags & NODE_BUILTIN)
1187 return ! pfile->done_initializing;
1188
1189 macro1 = node->value.macro;
1190
1191 /* The quick failures. */
1192 if (macro1->count != macro2->count
1193 || macro1->paramc != macro2->paramc
1194 || macro1->fun_like != macro2->fun_like
1195 || macro1->var_args != macro2->var_args)
Zack Weinberg711b8822000-07-18 00:59:49 +00001196 return 0;
1197
Neil Booth93c803682000-10-28 17:59:06 +00001198 /* Check each token. */
1199 for (i = 0; i < macro1->count; i++)
1200 if (! _cpp_equiv_tokens (&macro1->expansion[i], &macro2->expansion[i]))
1201 return 0;
1202
1203 /* Check parameter spellings. */
1204 for (i = 0; i < macro1->paramc; i++)
1205 if (macro1->params[i] != macro2->params[i])
1206 return 0;
1207
Zack Weinberg711b8822000-07-18 00:59:49 +00001208 return 1;
1209}
1210
Neil Booth93c803682000-10-28 17:59:06 +00001211/* Free the definition of hashnode H. */
Zack Weinberg711b8822000-07-18 00:59:49 +00001212
Neil Booth93c803682000-10-28 17:59:06 +00001213void
1214_cpp_free_definition (h)
1215 cpp_hashnode *h;
Zack Weinberg711b8822000-07-18 00:59:49 +00001216{
Neil Booth93c803682000-10-28 17:59:06 +00001217 /* Macros and assertions no longer have anything to free. */
1218 h->type = NT_VOID;
1219 /* Clear builtin flag in case of redefinition. */
1220 h->flags &= ~NODE_BUILTIN;
1221}
Zack Weinberg711b8822000-07-18 00:59:49 +00001222
Neil Booth93c803682000-10-28 17:59:06 +00001223static int
1224save_parameter (pfile, macro, node)
1225 cpp_reader *pfile;
1226 cpp_macro *macro;
1227 cpp_hashnode *node;
1228{
1229 cpp_hashnode **dest;
Zack Weinberg711b8822000-07-18 00:59:49 +00001230
Neil Booth93c803682000-10-28 17:59:06 +00001231 /* Constraint 6.10.3.6 - duplicate parameter names. */
1232 if (node->arg_index)
Zack Weinberg711b8822000-07-18 00:59:49 +00001233 {
Neil Booth93c803682000-10-28 17:59:06 +00001234 cpp_error (pfile, "duplicate macro parameter \"%s\"", node->name);
1235 return 1;
1236 }
1237
1238 dest = &macro->params[macro->paramc];
1239
1240 /* Check we have room for the parameters. */
1241 if ((unsigned char *) (dest + 1) >= POOL_LIMIT (&pfile->macro_pool))
1242 {
1243 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_hashnode *),
1244 (unsigned char **) &macro->params);
1245 dest = &macro->params[macro->paramc];
1246 }
1247
1248 *dest = node;
1249 node->arg_index = ++macro->paramc;
1250 return 0;
1251}
1252
1253static int
1254parse_params (pfile, macro)
1255 cpp_reader *pfile;
1256 cpp_macro *macro;
1257{
1258 cpp_token token;
1259 unsigned int prev_ident = 0;
1260
1261 macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool);
1262 for (;;)
1263 {
1264 _cpp_lex_token (pfile, &token);
1265
1266 switch (token.type)
Zack Weinberg711b8822000-07-18 00:59:49 +00001267 {
1268 default:
Neil Booth93c803682000-10-28 17:59:06 +00001269 cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
1270 cpp_token_as_text (pfile, &token));
1271 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001272
Zack Weinberg711b8822000-07-18 00:59:49 +00001273 case CPP_NAME:
1274 if (prev_ident)
1275 {
Neil Booth93c803682000-10-28 17:59:06 +00001276 cpp_error (pfile, "macro parameters must be comma-separated");
1277 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001278 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001279 prev_ident = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001280
1281 if (save_parameter (pfile, macro, token.val.node))
1282 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001283 continue;
1284
1285 case CPP_CLOSE_PAREN:
Neil Booth93c803682000-10-28 17:59:06 +00001286 if (prev_ident || macro->paramc == 0)
Zack Weinberg711b8822000-07-18 00:59:49 +00001287 break;
1288
1289 /* Fall through to pick up the error. */
1290 case CPP_COMMA:
1291 if (!prev_ident)
1292 {
Neil Booth93c803682000-10-28 17:59:06 +00001293 cpp_error (pfile, "parameter name missing");
1294 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001295 }
1296 prev_ident = 0;
1297 continue;
1298
1299 case CPP_ELLIPSIS:
Neil Booth93c803682000-10-28 17:59:06 +00001300 macro->var_args = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001301 if (!prev_ident)
1302 {
Neil Booth93c803682000-10-28 17:59:06 +00001303 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1304 pfile->state.va_args_ok = 1;
1305 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
Zack Weinberg711b8822000-07-18 00:59:49 +00001306 cpp_pedwarn (pfile,
Neil Booth93c803682000-10-28 17:59:06 +00001307 "C89 does not permit anonymous variable arguments");
Zack Weinberg711b8822000-07-18 00:59:49 +00001308 }
Neil Booth93c803682000-10-28 17:59:06 +00001309 else if (CPP_OPTION (pfile, pedantic))
1310 cpp_pedwarn (pfile,
1311 "ISO C does not permit named variable arguments");
Zack Weinberg711b8822000-07-18 00:59:49 +00001312
Neil Booth93c803682000-10-28 17:59:06 +00001313 /* We're at the end, and just expect a closing parenthesis. */
1314 _cpp_lex_token (pfile, &token);
1315 if (token.type == CPP_CLOSE_PAREN)
Zack Weinberg711b8822000-07-18 00:59:49 +00001316 break;
Neil Booth93c803682000-10-28 17:59:06 +00001317 /* Fall through. */
1318
1319 case CPP_EOF:
1320 cpp_error (pfile, "missing ')' in macro parameter list");
1321 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001322 }
1323
Neil Booth93c803682000-10-28 17:59:06 +00001324 /* Success. Commit the parameter array. */
1325 POOL_COMMIT (&pfile->macro_pool,
1326 macro->paramc * sizeof (cpp_hashnode *));
1327 return 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001328 }
1329}
1330
Neil Booth93c803682000-10-28 17:59:06 +00001331/* Lex a token from a macro's replacement list. Translate it to a
1332 CPP_MACRO_ARG if appropriate. */
1333static cpp_token *
1334lex_expansion_token (pfile, macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001335 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001336 cpp_macro *macro;
Zack Weinberg711b8822000-07-18 00:59:49 +00001337{
Neil Booth93c803682000-10-28 17:59:06 +00001338 cpp_token *token = &macro->expansion[macro->count];
Zack Weinberg711b8822000-07-18 00:59:49 +00001339
Neil Booth93c803682000-10-28 17:59:06 +00001340 /* Check we have room for the token. */
1341 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
Zack Weinberg711b8822000-07-18 00:59:49 +00001342 {
Neil Booth93c803682000-10-28 17:59:06 +00001343 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1344 (unsigned char **) &macro->expansion);
1345 token = &macro->expansion[macro->count];
Zack Weinberg711b8822000-07-18 00:59:49 +00001346 }
1347
Neil Booth93c803682000-10-28 17:59:06 +00001348 macro->count++;
1349 _cpp_lex_token (pfile, token);
1350
1351 /* Is this an argument? */
1352 if (token->type == CPP_NAME && token->val.node->arg_index)
Zack Weinberg711b8822000-07-18 00:59:49 +00001353 {
Neil Booth93c803682000-10-28 17:59:06 +00001354 token->type = CPP_MACRO_ARG;
Neil Booth6c53ebf2000-11-06 18:43:32 +00001355 token->val.arg_no = token->val.node->arg_index;
Zack Weinberg711b8822000-07-18 00:59:49 +00001356 }
Neil Booth93c803682000-10-28 17:59:06 +00001357 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1358 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1359 check_trad_stringification (pfile, macro, &token->val.str);
Zack Weinberg711b8822000-07-18 00:59:49 +00001360
Neil Booth93c803682000-10-28 17:59:06 +00001361 return token;
Zack Weinberg711b8822000-07-18 00:59:49 +00001362}
1363
1364/* Parse a macro and save its expansion. Returns non-zero on success. */
1365int
Neil Booth93c803682000-10-28 17:59:06 +00001366_cpp_create_definition (pfile, node)
Zack Weinberg711b8822000-07-18 00:59:49 +00001367 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001368 cpp_hashnode *node;
Zack Weinberg711b8822000-07-18 00:59:49 +00001369{
Neil Booth93c803682000-10-28 17:59:06 +00001370 cpp_macro *macro;
1371 cpp_token *token;
1372 unsigned int i, ok = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001373
Neil Booth93c803682000-10-28 17:59:06 +00001374 macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool,
1375 sizeof (cpp_macro));
1376 macro->file = pfile->buffer->nominal_fname;
1377 macro->line = pfile->directive_pos.line;
1378 macro->params = 0;
1379 macro->paramc = 0;
1380 macro->fun_like = 0;
1381 macro->var_args = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001382 macro->count = 0;
1383 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
Zack Weinberg711b8822000-07-18 00:59:49 +00001384
Neil Booth93c803682000-10-28 17:59:06 +00001385 /* Get the first token of the expansion (or the '(' of a
1386 function-like macro). */
1387 token = lex_expansion_token (pfile, macro);
1388 if (token->type == CPP_OPEN_PAREN && !(token->flags & PREV_WHITE))
Zack Weinberg711b8822000-07-18 00:59:49 +00001389 {
Neil Booth93c803682000-10-28 17:59:06 +00001390 if (!(ok = parse_params (pfile, macro)))
1391 goto cleanup;
1392 macro->count = 0;
1393 macro->fun_like = 1;
1394 /* Some of the pool may have been used for the parameter store. */
1395 macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool);
1396 token = lex_expansion_token (pfile, macro);
1397 }
1398 else if (token->type != CPP_EOF && !(token->flags & PREV_WHITE))
1399 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
1400
1401 /* Setting it here means we don't catch leading comments. */
1402 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
1403
1404 for (;;)
1405 {
1406 /* Check the stringifying # constraint 6.10.3.2.1 of
1407 function-like macros when lexing the subsequent token. */
1408 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
Zack Weinberg711b8822000-07-18 00:59:49 +00001409 {
Neil Booth93c803682000-10-28 17:59:06 +00001410 if (token->type == CPP_MACRO_ARG)
1411 {
1412 token->flags &= ~PREV_WHITE;
1413 token->flags |= STRINGIFY_ARG;
1414 token->flags |= token[-1].flags & PREV_WHITE;
1415 token[-1] = token[0];
1416 macro->count--;
1417 }
1418 /* Let assembler get away with murder. */
1419 else if (!CPP_OPTION (pfile, lang_asm))
1420 {
1421 ok = 0;
1422 cpp_error (pfile, "'#' is not followed by a macro parameter");
1423 goto cleanup;
1424 }
1425 }
1426
1427 if (token->type == CPP_EOF)
1428 break;
1429
1430 /* Paste operator constraint 6.10.3.3.1. */
1431 if (token->type == CPP_PASTE)
1432 {
1433 /* Token-paste ##, can appear in both object-like and
1434 function-like macros, but not at the ends. */
1435 if (--macro->count > 0)
1436 token = lex_expansion_token (pfile, macro);
1437
1438 if (macro->count == 0 || token->type == CPP_EOF)
1439 {
1440 ok = 0;
1441 cpp_error (pfile,
1442 "'##' cannot appear at either end of a macro expansion");
1443 goto cleanup;
1444 }
1445
1446 token[-1].flags |= PASTE_LEFT;
1447 /* Give it a PREV_WHITE for -dM etc. */
1448 token->flags |= PREV_WHITE;
1449 }
1450
1451 token = lex_expansion_token (pfile, macro);
1452 }
1453
Neil Booth4c2b6472000-11-11 13:19:01 +00001454 /* Don't count the CPP_EOF. */
1455 macro->count--;
Neil Booth93c803682000-10-28 17:59:06 +00001456
1457 /* Clear the whitespace flag from the leading token. */
1458 macro->expansion[0].flags &= ~PREV_WHITE;
1459
Neil Booth44ed91a2000-10-29 11:37:18 +00001460 /* Implement the macro-defined-to-itself optimisation. */
1461 macro->disabled = (macro->count == 1 && !macro->fun_like
1462 && macro->expansion[0].type == CPP_NAME
1463 && macro->expansion[0].val.node == node);
1464
Neil Booth93c803682000-10-28 17:59:06 +00001465 /* Commit the memory. */
1466 POOL_COMMIT (&pfile->macro_pool, macro->count * sizeof (cpp_token));
1467
1468 /* Redefinition of a macro is allowed if and only if the old and new
1469 definitions are the same. (6.10.3 paragraph 2). */
1470 if (node->type != NT_VOID)
1471 {
1472 if (CPP_PEDANTIC (pfile)
1473 && !check_macro_redefinition (pfile, node, macro))
1474 {
1475 cpp_pedwarn_with_line (pfile, pfile->directive_pos.line,
1476 pfile->directive_pos.col,
1477 "\"%s\" redefined", node->name);
1478
1479 if (pfile->done_initializing && node->type == NT_MACRO
1480 && !(node->flags & NODE_BUILTIN))
Zack Weinberg711b8822000-07-18 00:59:49 +00001481 cpp_pedwarn_with_file_and_line (pfile,
Neil Booth93c803682000-10-28 17:59:06 +00001482 node->value.macro->file,
1483 node->value.macro->line, 1,
Zack Weinberg711b8822000-07-18 00:59:49 +00001484 "this is the location of the previous definition");
1485 }
Neil Booth93c803682000-10-28 17:59:06 +00001486 _cpp_free_definition (node);
Zack Weinberg711b8822000-07-18 00:59:49 +00001487 }
1488
1489 /* Enter definition in hash table. */
Neil Booth93c803682000-10-28 17:59:06 +00001490 node->type = NT_MACRO;
1491 node->value.macro = macro;
Zack Weinberg711b8822000-07-18 00:59:49 +00001492
Neil Booth93c803682000-10-28 17:59:06 +00001493 cleanup:
1494
1495 /* Stop the lexer accepting __VA_ARGS__. */
1496 pfile->state.va_args_ok = 0;
1497
1498 /* Clear the fast argument lookup indices. */
1499 for (i = macro->paramc; i-- > 0; )
1500 macro->params[i]->arg_index = 0;
1501
1502 return ok;
Zack Weinberg711b8822000-07-18 00:59:49 +00001503}
1504
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001505/* Warn if a token in `string' matches one of the function macro
1506 arguments in `info'. This function assumes that the macro is a
1507 function macro and not an object macro. */
1508static void
Neil Booth93c803682000-10-28 17:59:06 +00001509check_trad_stringification (pfile, macro, string)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001510 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001511 const cpp_macro *macro;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001512 const cpp_string *string;
1513{
Neil Booth93c803682000-10-28 17:59:06 +00001514 unsigned int i, len;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001515 const U_CHAR *p, *q, *limit = string->text + string->len;
1516
1517 /* Loop over the string. */
1518 for (p = string->text; p < limit; p = q)
1519 {
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001520 /* Find the start of an identifier. */
Greg McGary61c16b12000-09-15 21:25:02 +00001521 while (p < limit && !is_idstart (*p))
1522 p++;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001523
1524 /* Find the end of the identifier. */
1525 q = p;
Greg McGary61c16b12000-09-15 21:25:02 +00001526 while (q < limit && is_idchar (*q))
1527 q++;
Neil Booth93c803682000-10-28 17:59:06 +00001528
1529 len = q - p;
1530
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001531 /* Loop over the function macro arguments to see if the
1532 identifier inside the string matches one of them. */
Neil Booth93c803682000-10-28 17:59:06 +00001533 for (i = 0; i < macro->paramc; i++)
1534 {
1535 const cpp_hashnode *node = macro->params[i];
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001536
Neil Booth93c803682000-10-28 17:59:06 +00001537 if (node->length == len && !memcmp (p, node->name, len))
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001538 {
1539 cpp_warning (pfile,
Neil Booth93c803682000-10-28 17:59:06 +00001540 "macro argument \"%s\" would be stringified with -traditional.",
1541 node->name);
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001542 break;
1543 }
1544 }
1545 }
1546}
Neil Booth93c803682000-10-28 17:59:06 +00001547
1548/* Returns the expansion of a macro, in a format suitable to be read
1549 back in again, and therefore also for DWARF 2 debugging info.
1550 Caller is expected to generate the "#define NAME" bit. The
1551 returned text is temporary, and automatically freed later. */
1552
1553const unsigned char *
1554cpp_macro_definition (pfile, node)
1555 cpp_reader *pfile;
1556 const cpp_hashnode *node;
1557{
1558 unsigned int i, len;
1559 const cpp_macro *macro = node->value.macro;
1560 unsigned char *buffer;
1561
1562 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1563 {
1564 cpp_ice (pfile, "invalid hash type %d in dump_definition", node->type);
1565 return 0;
1566 }
1567
1568 /* Calculate length. */
1569 len = 1; /* ' ' */
1570 if (macro->fun_like)
1571 {
1572 len += 3; /* "()" plus possible final "." of ellipsis. */
1573 for (i = 0; i < macro->paramc; i++)
1574 len += macro->params[i]->length + 2; /* ", " */
1575 }
1576
Neil Booth4c2b6472000-11-11 13:19:01 +00001577 for (i = 0; i < macro->count; i++)
Neil Booth93c803682000-10-28 17:59:06 +00001578 {
Neil Booth4c2b6472000-11-11 13:19:01 +00001579 cpp_token *token = &macro->expansion[i];
Neil Booth93c803682000-10-28 17:59:06 +00001580
Neil Booth4c2b6472000-11-11 13:19:01 +00001581 if (token->type == CPP_MACRO_ARG)
1582 len += macro->params[token->val.arg_no - 1]->length;
1583 else
1584 len += cpp_token_len (token); /* Includes room for ' '. */
1585 if (token->flags & STRINGIFY_ARG)
1586 len++; /* "#" */
1587 if (token->flags & PASTE_LEFT)
1588 len += 3; /* " ##" */
Neil Booth93c803682000-10-28 17:59:06 +00001589 }
1590
1591 if (len > pfile->macro_buffer_len)
1592 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1593 buffer = pfile->macro_buffer;
1594
1595 /* Parameter names. */
1596 if (macro->fun_like)
1597 {
1598 *buffer++ = '(';
1599 for (i = 0; i < macro->paramc; i++)
1600 {
1601 cpp_hashnode *param = macro->params[i];
1602
1603 if (param != pfile->spec_nodes.n__VA_ARGS__)
1604 {
1605 memcpy (buffer, param->name, param->length);
1606 buffer += param->length;
1607 }
1608
1609 if (i + 1 < macro->paramc)
1610 *buffer++ = ',', *buffer++ = ' ';
1611 else if (macro->var_args)
1612 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1613 }
1614 *buffer++ = ')';
1615 }
1616
1617 /* Expansion tokens. */
Neil Booth4c2b6472000-11-11 13:19:01 +00001618 if (macro->count)
Neil Booth93c803682000-10-28 17:59:06 +00001619 {
1620 *buffer++ = ' ';
1621 for (i = 0; i < macro->count; i++)
1622 {
1623 cpp_token *token = &macro->expansion[i];
1624
1625 if (token->flags & PREV_WHITE)
1626 *buffer++ = ' ';
1627 if (token->flags & STRINGIFY_ARG)
1628 *buffer++ = '#';
1629
1630 if (token->type == CPP_MACRO_ARG)
1631 {
Neil Booth6c53ebf2000-11-06 18:43:32 +00001632 len = macro->params[token->val.arg_no - 1]->length;
1633 memcpy (buffer, macro->params[token->val.arg_no - 1]->name, len);
Neil Booth93c803682000-10-28 17:59:06 +00001634 buffer += len;
1635 }
1636 else
1637 buffer = cpp_spell_token (pfile, token, buffer);
1638
1639 if (token->flags & PASTE_LEFT)
1640 {
1641 *buffer++ = ' ';
1642 *buffer++ = '#';
1643 *buffer++ = '#';
1644 /* Next has PREV_WHITE; see _cpp_create_definition. */
1645 }
1646 }
1647 }
1648
1649 *buffer = '\0';
1650 return pfile->macro_buffer;
1651}