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