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