]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cppmacro.c
h8300.md (four define_peephole2's): Use h8300_regs_ok_for_stm().
[thirdparty/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,
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"
29#include "cpphash.h"
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;
12f9df4e 136 map = linemap_lookup (pfile->line_table, pfile->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:
12f9df4e 161 map = linemap_lookup (pfile->line_table, pfile->line);
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
NB
165 if (CPP_OPTION (pfile, traditional))
166 number = pfile->line;
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{
590e1987
NB
1141 if (CPP_OPTION (pfile, traditional))
1142 while (_cpp_read_logical_line_trad (pfile))
1143 ;
1144 else
1145 while (cpp_get_token (pfile)->type != CPP_EOF)
1146 ;
93c80368
NB
1147}
1148
bdcbe496
NB
1149/* Step back one (or more) tokens. Can only step mack more than 1 if
1150 they are from the lexer, and not from macro expansion. */
b528a07e 1151void
6cf87ca4 1152_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
93c80368 1153{
bdcbe496 1154 if (pfile->context->prev == NULL)
93c80368 1155 {
bdcbe496
NB
1156 pfile->lookaheads += count;
1157 while (count--)
1158 {
3293c3e3
NB
1159 pfile->cur_token--;
1160 if (pfile->cur_token == pfile->cur_run->base
1161 /* Possible with -fpreprocessed and no leading #line. */
1162 && pfile->cur_run->prev != NULL)
bdcbe496 1163 {
bdcbe496
NB
1164 pfile->cur_run = pfile->cur_run->prev;
1165 pfile->cur_token = pfile->cur_run->limit;
1166 }
1167 }
93c80368 1168 }
bdcbe496 1169 else
93c80368 1170 {
bdcbe496
NB
1171 if (count != 1)
1172 abort ();
4ed5bcfb 1173 if (pfile->context->direct_p)
82eda77e 1174 FIRST (pfile->context).token--;
4ed5bcfb 1175 else
82eda77e 1176 FIRST (pfile->context).ptoken--;
93c80368
NB
1177 }
1178}
711b8824 1179
93c80368
NB
1180/* #define directive parsing and handling. */
1181
da7d8304 1182/* Returns nonzero if a macro redefinition warning is required. */
cbc69f84 1183static bool
6cf87ca4
ZW
1184warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
1185 const cpp_macro *macro2)
93c80368
NB
1186{
1187 const cpp_macro *macro1;
1188 unsigned int i;
1189
618cdda7
NB
1190 /* Some redefinitions need to be warned about regardless. */
1191 if (node->flags & NODE_WARN)
cbc69f84 1192 return true;
93c80368 1193
618cdda7 1194 /* Redefinition of a macro is allowed if and only if the old and new
ec5c56db 1195 definitions are the same. (6.10.3 paragraph 2). */
93c80368
NB
1196 macro1 = node->value.macro;
1197
6618c5d4
NB
1198 /* Don't check count here as it can be different in valid
1199 traditional redefinitions with just whitespace differences. */
1200 if (macro1->paramc != macro2->paramc
93c80368 1201 || macro1->fun_like != macro2->fun_like
28e0f040 1202 || macro1->variadic != macro2->variadic)
cbc69f84 1203 return true;
93c80368
NB
1204
1205 /* Check parameter spellings. */
1206 for (i = 0; i < macro1->paramc; i++)
1207 if (macro1->params[i] != macro2->params[i])
cbc69f84 1208 return true;
93c80368 1209
cbc69f84
NB
1210 /* Check the replacement text or tokens. */
1211 if (CPP_OPTION (pfile, traditional))
6618c5d4 1212 return _cpp_expansions_different_trad (macro1, macro2);
cbc69f84 1213
a7f36da3
DD
1214 if (macro1->count != macro2->count)
1215 return true;
1216
1217 for (i = 0; i < macro1->count; i++)
1218 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1219 return true;
cbc69f84
NB
1220
1221 return false;
93c80368
NB
1222}
1223
1224/* Free the definition of hashnode H. */
711b8824 1225void
6cf87ca4 1226_cpp_free_definition (cpp_hashnode *h)
711b8824 1227{
93c80368
NB
1228 /* Macros and assertions no longer have anything to free. */
1229 h->type = NT_VOID;
1230 /* Clear builtin flag in case of redefinition. */
644eddaa 1231 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
93c80368
NB
1232}
1233
14baae01 1234/* Save parameter NODE to the parameter list of macro MACRO. Returns
da7d8304 1235 zero on success, nonzero if the parameter is a duplicate. */
c70f6ed3 1236bool
6cf87ca4 1237_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
93c80368 1238{
4977bab6 1239 unsigned int len;
93c80368 1240 /* Constraint 6.10.3.6 - duplicate parameter names. */
4977bab6 1241 if (node->flags & NODE_MACRO_ARG)
709e9e50 1242 {
0527bc4e 1243 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
ebef4e8c 1244 NODE_NAME (node));
23ff0223 1245 return true;
93c80368 1246 }
709e9e50 1247
8c3b2693
NB
1248 if (BUFF_ROOM (pfile->a_buff)
1249 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1250 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
709e9e50 1251
8c3b2693 1252 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
4977bab6
ZW
1253 node->flags |= NODE_MACRO_ARG;
1254 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1255 if (len > pfile->macro_buffer_len)
1256 {
703ad42b 1257 pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
4977bab6
ZW
1258 pfile->macro_buffer_len = len;
1259 }
1260 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1261 = node->value;
1262
1263 node->value.arg_index = macro->paramc;
23ff0223 1264 return false;
711b8824
ZW
1265}
1266
23ff0223
NB
1267/* Check the syntax of the parameters in a MACRO definition. Returns
1268 false if an error occurs. */
1269static bool
6cf87ca4 1270parse_params (cpp_reader *pfile, cpp_macro *macro)
711b8824 1271{
93c80368 1272 unsigned int prev_ident = 0;
711b8824 1273
93c80368 1274 for (;;)
711b8824 1275 {
345894b4 1276 const cpp_token *token = _cpp_lex_token (pfile);
711b8824 1277
345894b4 1278 switch (token->type)
711b8824 1279 {
93c80368 1280 default:
477cdac7
JT
1281 /* Allow/ignore comments in parameter lists if we are
1282 preserving comments in macro expansions. */
1283 if (token->type == CPP_COMMENT
1284 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1285 continue;
1286
0527bc4e 1287 cpp_error (pfile, CPP_DL_ERROR,
ebef4e8c 1288 "\"%s\" may not appear in macro parameter list",
345894b4 1289 cpp_token_as_text (pfile, token));
23ff0223 1290 return false;
93c80368 1291
711b8824 1292 case CPP_NAME:
93c80368
NB
1293 if (prev_ident)
1294 {
0527bc4e 1295 cpp_error (pfile, CPP_DL_ERROR,
ebef4e8c 1296 "macro parameters must be comma-separated");
23ff0223 1297 return false;
93c80368
NB
1298 }
1299 prev_ident = 1;
1300
c70f6ed3 1301 if (_cpp_save_parameter (pfile, macro, token->val.node))
23ff0223 1302 return false;
93c80368 1303 continue;
711b8824 1304
93c80368
NB
1305 case CPP_CLOSE_PAREN:
1306 if (prev_ident || macro->paramc == 0)
23ff0223 1307 return true;
711b8824 1308
93c80368
NB
1309 /* Fall through to pick up the error. */
1310 case CPP_COMMA:
1311 if (!prev_ident)
1312 {
0527bc4e 1313 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
23ff0223 1314 return false;
93c80368
NB
1315 }
1316 prev_ident = 0;
711b8824
ZW
1317 continue;
1318
93c80368 1319 case CPP_ELLIPSIS:
28e0f040 1320 macro->variadic = 1;
93c80368
NB
1321 if (!prev_ident)
1322 {
c70f6ed3
NB
1323 _cpp_save_parameter (pfile, macro,
1324 pfile->spec_nodes.n__VA_ARGS__);
93c80368
NB
1325 pfile->state.va_args_ok = 1;
1326 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
0527bc4e 1327 cpp_error (pfile, CPP_DL_PEDWARN,
ebef4e8c 1328 "anonymous variadic macros were introduced in C99");
93c80368
NB
1329 }
1330 else if (CPP_OPTION (pfile, pedantic))
0527bc4e 1331 cpp_error (pfile, CPP_DL_PEDWARN,
ebef4e8c 1332 "ISO C does not permit named variadic macros");
711b8824 1333
93c80368 1334 /* We're at the end, and just expect a closing parenthesis. */
345894b4
NB
1335 token = _cpp_lex_token (pfile);
1336 if (token->type == CPP_CLOSE_PAREN)
23ff0223 1337 return true;
93c80368 1338 /* Fall through. */
711b8824 1339
93c80368 1340 case CPP_EOF:
0527bc4e 1341 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
23ff0223 1342 return false;
711b8824 1343 }
711b8824 1344 }
93c80368
NB
1345}
1346
14baae01 1347/* Allocate room for a token from a macro's replacement list. */
93c80368 1348static cpp_token *
6cf87ca4 1349alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
93c80368 1350{
8c3b2693
NB
1351 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1352 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
711b8824 1353
8c3b2693 1354 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
14baae01
NB
1355}
1356
d15a58c0
NB
1357/* Lex a token from the expansion of MACRO, but mark parameters as we
1358 find them and warn of traditional stringification. */
14baae01 1359static cpp_token *
6cf87ca4 1360lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
14baae01
NB
1361{
1362 cpp_token *token;
1363
1364 pfile->cur_token = alloc_expansion_token (pfile, macro);
1365 token = _cpp_lex_direct (pfile);
93c80368 1366
d15a58c0 1367 /* Is this a parameter? */
4977bab6
ZW
1368 if (token->type == CPP_NAME
1369 && (token->val.node->flags & NODE_MACRO_ARG) != 0)
93c80368
NB
1370 {
1371 token->type = CPP_MACRO_ARG;
4977bab6 1372 token->val.arg_no = token->val.node->value.arg_index;
93c80368
NB
1373 }
1374 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1375 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1376 check_trad_stringification (pfile, macro, &token->val.str);
1377
1378 return token;
711b8824
ZW
1379}
1380
cbc69f84 1381static bool
6cf87ca4 1382create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
711b8824 1383{
cbc69f84 1384 cpp_token *token;
14baae01 1385 const cpp_token *ctoken;
93c80368
NB
1386
1387 /* Get the first token of the expansion (or the '(' of a
1388 function-like macro). */
14baae01
NB
1389 ctoken = _cpp_lex_token (pfile);
1390
1391 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
93c80368 1392 {
cbc69f84 1393 bool ok = parse_params (pfile, macro);
8c3b2693
NB
1394 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1395 if (!ok)
cbc69f84 1396 return false;
8c3b2693
NB
1397
1398 /* Success. Commit the parameter array. */
562a5c27 1399 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
93c80368 1400 macro->fun_like = 1;
93c80368 1401 }
14baae01 1402 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
0527bc4e 1403 cpp_error (pfile, CPP_DL_PEDWARN,
ebef4e8c 1404 "ISO C requires whitespace after the macro name");
711b8824 1405
14baae01
NB
1406 if (macro->fun_like)
1407 token = lex_expansion_token (pfile, macro);
1408 else
1409 {
1410 token = alloc_expansion_token (pfile, macro);
1411 *token = *ctoken;
1412 }
711b8824 1413
93c80368
NB
1414 for (;;)
1415 {
1416 /* Check the stringifying # constraint 6.10.3.2.1 of
1417 function-like macros when lexing the subsequent token. */
1418 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1419 {
1420 if (token->type == CPP_MACRO_ARG)
1421 {
1422 token->flags &= ~PREV_WHITE;
1423 token->flags |= STRINGIFY_ARG;
1424 token->flags |= token[-1].flags & PREV_WHITE;
1425 token[-1] = token[0];
1426 macro->count--;
1427 }
1428 /* Let assembler get away with murder. */
bdb05a7b 1429 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
93c80368 1430 {
0527bc4e 1431 cpp_error (pfile, CPP_DL_ERROR,
ebef4e8c 1432 "'#' is not followed by a macro parameter");
cbc69f84 1433 return false;
93c80368
NB
1434 }
1435 }
1436
1437 if (token->type == CPP_EOF)
1438 break;
1439
1440 /* Paste operator constraint 6.10.3.3.1. */
1441 if (token->type == CPP_PASTE)
1442 {
1443 /* Token-paste ##, can appear in both object-like and
1444 function-like macros, but not at the ends. */
1445 if (--macro->count > 0)
1446 token = lex_expansion_token (pfile, macro);
1447
1448 if (macro->count == 0 || token->type == CPP_EOF)
1449 {
0527bc4e 1450 cpp_error (pfile, CPP_DL_ERROR,
6cf87ca4 1451 "'##' cannot appear at either end of a macro expansion");
cbc69f84 1452 return false;
93c80368 1453 }
711b8824 1454
93c80368 1455 token[-1].flags |= PASTE_LEFT;
93c80368
NB
1456 }
1457
1458 token = lex_expansion_token (pfile, macro);
1459 }
1460
601328bb 1461 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
8c3b2693 1462
4c2b647d
NB
1463 /* Don't count the CPP_EOF. */
1464 macro->count--;
93c80368 1465
d15a58c0 1466 /* Clear whitespace on first token for warn_of_redefinition(). */
8c3b2693 1467 if (macro->count)
601328bb 1468 macro->exp.tokens[0].flags &= ~PREV_WHITE;
8c3b2693
NB
1469
1470 /* Commit the memory. */
601328bb 1471 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
8c3b2693 1472
cbc69f84
NB
1473 return true;
1474}
1475
da7d8304 1476/* Parse a macro and save its expansion. Returns nonzero on success. */
cbc69f84 1477bool
6cf87ca4 1478_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
cbc69f84
NB
1479{
1480 cpp_macro *macro;
1481 unsigned int i;
1482 bool ok;
44ed91a1 1483
cbc69f84
NB
1484 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1485 macro->line = pfile->directive_line;
1486 macro->params = 0;
1487 macro->paramc = 0;
1488 macro->variadic = 0;
23345bbb 1489 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
cbc69f84
NB
1490 macro->count = 0;
1491 macro->fun_like = 0;
7065e130 1492 /* To suppress some diagnostics. */
12f9df4e 1493 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
7065e130 1494
cbc69f84
NB
1495 if (CPP_OPTION (pfile, traditional))
1496 ok = _cpp_create_trad_definition (pfile, macro);
1497 else
1498 {
1499 cpp_token *saved_cur_token = pfile->cur_token;
1500
1501 ok = create_iso_definition (pfile, macro);
1502
1503 /* Restore lexer position because of games lex_expansion_token()
1504 plays lexing the macro. We set the type for SEEN_EOL() in
1505 cpplib.c.
1506
1507 Longer term we should lex the whole line before coming here,
1508 and just copy the expansion. */
1509 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1510 pfile->cur_token = saved_cur_token;
1511
1512 /* Stop the lexer accepting __VA_ARGS__. */
1513 pfile->state.va_args_ok = 0;
1514 }
1515
1516 /* Clear the fast argument lookup indices. */
1517 for (i = macro->paramc; i-- > 0; )
4977bab6
ZW
1518 {
1519 struct cpp_hashnode *node = macro->params[i];
1520 node->flags &= ~ NODE_MACRO_ARG;
1521 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1522 }
cbc69f84
NB
1523
1524 if (!ok)
1525 return ok;
1526
c2734e05 1527 if (node->type == NT_MACRO)
711b8824 1528 {
a69cbaac
NB
1529 if (CPP_OPTION (pfile, warn_unused_macros))
1530 _cpp_warn_if_unused_macro (pfile, node, NULL);
1531
cbc69f84 1532 if (warn_of_redefinition (pfile, node, macro))
711b8824 1533 {
0527bc4e 1534 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
ebef4e8c 1535 "\"%s\" redefined", NODE_NAME (node));
93c80368 1536
618cdda7 1537 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
0527bc4e 1538 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
cbc69f84
NB
1539 node->value.macro->line, 0,
1540 "this is the location of the previous definition");
711b8824 1541 }
711b8824
ZW
1542 }
1543
c2734e05
NB
1544 if (node->type != NT_VOID)
1545 _cpp_free_definition (node);
1546
711b8824 1547 /* Enter definition in hash table. */
93c80368
NB
1548 node->type = NT_MACRO;
1549 node->value.macro = macro;
a28c5035 1550 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
618cdda7 1551 node->flags |= NODE_WARN;
711b8824 1552
93c80368 1553 return ok;
711b8824
ZW
1554}
1555
d15a58c0
NB
1556/* Warn if a token in STRING matches one of a function-like MACRO's
1557 parameters. */
e1aa5140 1558static void
6cf87ca4
ZW
1559check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1560 const cpp_string *string)
e1aa5140 1561{
93c80368 1562 unsigned int i, len;
6338b358 1563 const uchar *p, *q, *limit;
df383483 1564
e1aa5140 1565 /* Loop over the string. */
6338b358
NB
1566 limit = string->text + string->len - 1;
1567 for (p = string->text + 1; p < limit; p = q)
e1aa5140 1568 {
e1aa5140 1569 /* Find the start of an identifier. */
61c16b10
GM
1570 while (p < limit && !is_idstart (*p))
1571 p++;
e1aa5140
KG
1572
1573 /* Find the end of the identifier. */
1574 q = p;
61c16b10
GM
1575 while (q < limit && is_idchar (*q))
1576 q++;
93c80368
NB
1577
1578 len = q - p;
1579
e1aa5140
KG
1580 /* Loop over the function macro arguments to see if the
1581 identifier inside the string matches one of them. */
93c80368
NB
1582 for (i = 0; i < macro->paramc; i++)
1583 {
1584 const cpp_hashnode *node = macro->params[i];
e1aa5140 1585
2a967f3d
NB
1586 if (NODE_LEN (node) == len
1587 && !memcmp (p, NODE_NAME (node), len))
e1aa5140 1588 {
0527bc4e 1589 cpp_error (pfile, CPP_DL_WARNING,
f458d1d5 1590 "macro argument \"%s\" would be stringified in traditional C",
ebef4e8c 1591 NODE_NAME (node));
e1aa5140
KG
1592 break;
1593 }
1594 }
1595 }
1596}
93c80368 1597
7096171b
NB
1598/* Returns the name, arguments and expansion of a macro, in a format
1599 suitable to be read back in again, and therefore also for DWARF 2
1600 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1601 Caller is expected to generate the "#define" bit if needed. The
93c80368 1602 returned text is temporary, and automatically freed later. */
93c80368 1603const unsigned char *
6cf87ca4 1604cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
93c80368
NB
1605{
1606 unsigned int i, len;
1607 const cpp_macro *macro = node->value.macro;
1608 unsigned char *buffer;
1609
1610 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1611 {
0527bc4e 1612 cpp_error (pfile, CPP_DL_ICE,
ebef4e8c 1613 "invalid hash type %d in cpp_macro_definition", node->type);
93c80368
NB
1614 return 0;
1615 }
1616
1617 /* Calculate length. */
8dc901de 1618 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
93c80368
NB
1619 if (macro->fun_like)
1620 {
64d08263
JB
1621 len += 4; /* "()" plus possible final ".." of named
1622 varargs (we have + 1 below). */
93c80368 1623 for (i = 0; i < macro->paramc; i++)
64d08263 1624 len += NODE_LEN (macro->params[i]) + 1; /* "," */
93c80368
NB
1625 }
1626
278c4662
NB
1627 if (CPP_OPTION (pfile, traditional))
1628 len += _cpp_replacement_text_len (macro);
1629 else
93c80368 1630 {
278c4662
NB
1631 for (i = 0; i < macro->count; i++)
1632 {
1633 cpp_token *token = &macro->exp.tokens[i];
93c80368 1634
278c4662
NB
1635 if (token->type == CPP_MACRO_ARG)
1636 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1637 else
59325650 1638 len += cpp_token_len (token) + 1; /* Includes room for ' '. */
278c4662
NB
1639 if (token->flags & STRINGIFY_ARG)
1640 len++; /* "#" */
1641 if (token->flags & PASTE_LEFT)
1642 len += 3; /* " ##" */
1643 }
93c80368
NB
1644 }
1645
1646 if (len > pfile->macro_buffer_len)
4b49c365 1647 {
703ad42b 1648 pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
4b49c365
AO
1649 pfile->macro_buffer_len = len;
1650 }
7096171b
NB
1651
1652 /* Fill in the buffer. Start with the macro name. */
93c80368 1653 buffer = pfile->macro_buffer;
7096171b
NB
1654 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1655 buffer += NODE_LEN (node);
93c80368
NB
1656
1657 /* Parameter names. */
1658 if (macro->fun_like)
1659 {
1660 *buffer++ = '(';
1661 for (i = 0; i < macro->paramc; i++)
1662 {
1663 cpp_hashnode *param = macro->params[i];
1664
1665 if (param != pfile->spec_nodes.n__VA_ARGS__)
1666 {
a28c5035
NB
1667 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1668 buffer += NODE_LEN (param);
93c80368
NB
1669 }
1670
1671 if (i + 1 < macro->paramc)
df383483
KH
1672 /* Don't emit a space after the comma here; we're trying
1673 to emit a Dwarf-friendly definition, and the Dwarf spec
1674 forbids spaces in the argument list. */
64d08263 1675 *buffer++ = ',';
28e0f040 1676 else if (macro->variadic)
93c80368
NB
1677 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1678 }
1679 *buffer++ = ')';
1680 }
1681
e37b38d7
JB
1682 /* The Dwarf spec requires a space after the macro name, even if the
1683 definition is the empty string. */
1684 *buffer++ = ' ';
1685
278c4662
NB
1686 if (CPP_OPTION (pfile, traditional))
1687 buffer = _cpp_copy_replacement_text (macro, buffer);
1688 else if (macro->count)
93c80368 1689 /* Expansion tokens. */
93c80368 1690 {
93c80368
NB
1691 for (i = 0; i < macro->count; i++)
1692 {
601328bb 1693 cpp_token *token = &macro->exp.tokens[i];
93c80368
NB
1694
1695 if (token->flags & PREV_WHITE)
1696 *buffer++ = ' ';
1697 if (token->flags & STRINGIFY_ARG)
1698 *buffer++ = '#';
1699
1700 if (token->type == CPP_MACRO_ARG)
1701 {
a28c5035
NB
1702 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1703 memcpy (buffer,
1704 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
93c80368
NB
1705 buffer += len;
1706 }
1707 else
1708 buffer = cpp_spell_token (pfile, token, buffer);
1709
1710 if (token->flags & PASTE_LEFT)
1711 {
1712 *buffer++ = ' ';
1713 *buffer++ = '#';
1714 *buffer++ = '#';
1715 /* Next has PREV_WHITE; see _cpp_create_definition. */
1716 }
1717 }
1718 }
1719
1720 *buffer = '\0';
1721 return pfile->macro_buffer;
1722}