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