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