]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/macro.c
2015-10-21 Steven G. Kargl <kargl@gcc.gnu.org>
[thirdparty/gcc.git] / libcpp / macro.c
CommitLineData
79bd622b 1/* Part of CPP library. (Macro and #define handling.)
d353bf18 2 Copyright (C) 1986-2015 Free Software Foundation, Inc.
69461e0d 3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6
7This program is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
6bc9506f 9Free Software Foundation; either version 3, or (at your option) any
69461e0d 10later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
6bc9506f 18along with this program; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>.
69461e0d 20
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
24
25#include "config.h"
26#include "system.h"
27#include "cpplib.h"
d856c8a6 28#include "internal.h"
69461e0d 29
79bd622b 30typedef struct macro_arg macro_arg;
ce70f433 31/* This structure represents the tokens of a macro argument. These
32 tokens can be macro themselves, in which case they can be either
33 expanded or unexpanded. When they are expanded, this data
34 structure keeps both the expanded and unexpanded forms. */
79bd622b 35struct macro_arg
36{
f9b5f742 37 const cpp_token **first; /* First token in unexpanded argument. */
1e625a2e 38 const cpp_token **expanded; /* Macro-expanded argument. */
f9b5f742 39 const cpp_token *stringified; /* Stringified argument. */
79bd622b 40 unsigned int count; /* # of tokens in argument. */
41 unsigned int expanded_count; /* # of tokens in expanded argument. */
ce70f433 42 source_location *virt_locs; /* Where virtual locations for
43 unexpanded tokens are stored. */
44 source_location *expanded_virt_locs; /* Where virtual locations for
45 expanded tokens are
46 stored. */
47};
48
49/* The kind of macro tokens which the instance of
50 macro_arg_token_iter is supposed to iterate over. */
51enum macro_arg_token_kind {
52 MACRO_ARG_TOKEN_NORMAL,
53 /* This is a macro argument token that got transformed into a string
54 litteral, e.g. #foo. */
55 MACRO_ARG_TOKEN_STRINGIFIED,
56 /* This is a token resulting from the expansion of a macro
57 argument that was itself a macro. */
58 MACRO_ARG_TOKEN_EXPANDED
59};
60
61/* An iterator over tokens coming from a function-like macro
62 argument. */
63typedef struct macro_arg_token_iter macro_arg_token_iter;
64struct macro_arg_token_iter
65{
66 /* Whether or not -ftrack-macro-expansion is used. */
67 bool track_macro_exp_p;
68 /* The kind of token over which we are supposed to iterate. */
69 enum macro_arg_token_kind kind;
70 /* A pointer to the current token pointed to by the iterator. */
71 const cpp_token **token_ptr;
72 /* A pointer to the "full" location of the current token. If
e8aa7eaf 73 -ftrack-macro-expansion is used this location tracks loci across
ce70f433 74 macro expansion. */
75 const source_location *location_ptr;
76#ifdef ENABLE_CHECKING
77 /* The number of times the iterator went forward. This useful only
78 when checking is enabled. */
79 size_t num_forwards;
80#endif
69461e0d 81};
82
109ca87a 83/* Saved data about an identifier being used as a macro argument
84 name. */
85struct macro_arg_saved_data {
86 /* The canonical (UTF-8) spelling of this identifier. */
87 cpp_hashnode *canonical_node;
88 /* The previous value of this identifier. */
89 union _cpp_hashnode_value value;
90};
91
79bd622b 92/* Macro expansion. */
93
45f9f140 94static int enter_macro_context (cpp_reader *, cpp_hashnode *,
ce70f433 95 const cpp_token *, source_location);
2e82cf2c 96static int builtin_macro (cpp_reader *, cpp_hashnode *, source_location);
f7fdd7a1 97static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
98 const cpp_token **, unsigned int);
ce70f433 99static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
100 _cpp_buff *, source_location *,
101 const cpp_token **, unsigned int);
45f9f140 102static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
ce70f433 103 _cpp_buff **, unsigned *);
f7fdd7a1 104static cpp_context *next_context (cpp_reader *);
105static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
106static void expand_arg (cpp_reader *, macro_arg *);
107static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
108static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
109static void paste_all_tokens (cpp_reader *, const cpp_token *);
8c6425eb 110static bool paste_tokens (cpp_reader *, source_location,
111 const cpp_token **, const cpp_token *);
ce70f433 112static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
113static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
114static void delete_macro_args (_cpp_buff*, unsigned num_args);
115static void set_arg_token (macro_arg *, const cpp_token *,
116 source_location, size_t,
117 enum macro_arg_token_kind,
118 bool);
119static const source_location *get_arg_token_location (const macro_arg *,
120 enum macro_arg_token_kind);
121static const cpp_token **arg_token_ptr_at (const macro_arg *,
122 size_t,
123 enum macro_arg_token_kind,
124 source_location **virt_location);
125
126static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
127 enum macro_arg_token_kind,
128 const macro_arg *,
129 const cpp_token **);
130static const cpp_token *macro_arg_token_iter_get_token
131(const macro_arg_token_iter *it);
132static source_location macro_arg_token_iter_get_location
133(const macro_arg_token_iter *);
134static void macro_arg_token_iter_forward (macro_arg_token_iter *);
135static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
136 source_location **);
137static size_t tokens_buff_count (_cpp_buff *);
138static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
80e34234 139static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
140 source_location *,
141 const cpp_token *,
142 source_location,
143 source_location,
551e34da 144 const line_map_macro *,
80e34234 145 unsigned int);
ce70f433 146
147static const cpp_token **tokens_buff_add_token (_cpp_buff *,
148 source_location *,
149 const cpp_token *,
150 source_location,
151 source_location,
551e34da 152 const line_map_macro *,
ce70f433 153 unsigned int);
80e34234 154static inline void tokens_buff_remove_last_token (_cpp_buff *);
f7fdd7a1 155static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
ce70f433 156 macro_arg *, source_location);
45f9f140 157static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
ce70f433 158 _cpp_buff **, unsigned *);
f7fdd7a1 159static bool create_iso_definition (cpp_reader *, cpp_macro *);
79bd622b 160
79bd622b 161/* #define directive parsing and handling. */
162
f7fdd7a1 163static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
164static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
25692381 165static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
f7fdd7a1 166 const cpp_macro *);
167static bool parse_params (cpp_reader *, cpp_macro *);
168static void check_trad_stringification (cpp_reader *, const cpp_macro *,
169 const cpp_string *);
ce70f433 170static bool reached_end_of_context (cpp_context *);
171static void consume_next_token_from_context (cpp_reader *pfile,
172 const cpp_token **,
173 source_location *);
174static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
69461e0d 175
00abfdc0 176static cpp_hashnode* macro_of_context (cpp_context *context);
177
8c6425eb 178static bool in_macro_expansion_p (cpp_reader *pfile);
179
e77b8253 180/* Statistical counter tracking the number of macros that got
181 expanded. */
182unsigned num_expanded_macros_counter = 0;
183/* Statistical counter tracking the total number tokens resulting
184 from macro expansion. */
185unsigned num_macro_tokens_counter = 0;
186
71a7c282 187/* Emits a warning if NODE is a macro defined in the main file that
188 has not been used. */
189int
f7fdd7a1 190_cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
191 void *v ATTRIBUTE_UNUSED)
71a7c282 192{
193 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
194 {
195 cpp_macro *macro = node->value.macro;
196
197 if (!macro->used
551e34da 198 && MAIN_FILE_P (linemap_check_ordinary
199 (linemap_lookup (pfile->line_table,
200 macro->line))))
3a79f5da 201 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
202 "macro \"%s\" is not used", NODE_NAME (node));
71a7c282 203 }
204
205 return 1;
206}
207
f9b5f742 208/* Allocates and returns a CPP_STRING token, containing TEXT of length
209 LEN, after null-terminating it. TEXT must be in permanent storage. */
210static const cpp_token *
f7fdd7a1 211new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
79bd622b 212{
f9b5f742 213 cpp_token *token = _cpp_temp_token (pfile);
79bd622b 214
f9b5f742 215 text[len] = '\0';
79bd622b 216 token->type = CPP_STRING;
f9b5f742 217 token->val.str.len = len;
218 token->val.str.text = text;
79bd622b 219 token->flags = 0;
f9b5f742 220 return token;
79bd622b 221}
222
79bd622b 223static const char * const monthnames[] =
224{
225 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
226 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
227};
228
d6d3c909 229/* Helper function for builtin_macro. Returns the text generated by
230 a builtin macro. */
9c343313 231const uchar *
f7fdd7a1 232_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
79bd622b 233{
9c343313 234 const uchar *result = NULL;
4999c35b 235 linenum_type number = 1;
c7e5d924 236
79bd622b 237 switch (node->value.builtin)
238 {
f9b5f742 239 default:
d80d2074 240 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
73328dce 241 NODE_NAME (node));
9c343313 242 break;
f9b5f742 243
cca5dddc 244 case BT_TIMESTAMP:
245 {
9148bda3 246 if (CPP_OPTION (pfile, warn_date_time))
5b1a0622 247 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
248 "reproducible builds", NODE_NAME (node));
9148bda3 249
cca5dddc 250 cpp_buffer *pbuffer = cpp_get_buffer (pfile);
251 if (pbuffer->timestamp == NULL)
252 {
253 /* Initialize timestamp value of the assotiated file. */
254 struct _cpp_file *file = cpp_get_file (pbuffer);
255 if (file)
256 {
257 /* Generate __TIMESTAMP__ string, that represents
258 the date and time of the last modification
259 of the current source file. The string constant
260 looks like "Sun Sep 16 01:03:52 1973". */
261 struct tm *tb = NULL;
262 struct stat *st = _cpp_get_file_stat (file);
263 if (st)
264 tb = localtime (&st->st_mtime);
265 if (tb)
266 {
267 char *str = asctime (tb);
268 size_t len = strlen (str);
269 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
270 buf[0] = '"';
271 strcpy ((char *) buf + 1, str);
272 buf[len] = '"';
273 pbuffer->timestamp = buf;
274 }
275 else
276 {
277 cpp_errno (pfile, CPP_DL_WARNING,
278 "could not determine file timestamp");
924bbf02 279 pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
cca5dddc 280 }
281 }
282 }
283 result = pbuffer->timestamp;
284 }
285 break;
79bd622b 286 case BT_FILE:
287 case BT_BASE_FILE:
69461e0d 288 {
f9b5f742 289 unsigned int len;
6cee4464 290 const char *name;
b6d18b0a 291 uchar *buf;
97bfb9ef 292
293 if (node->value.builtin == BT_FILE)
294 name = linemap_get_expansion_filename (pfile->line_table,
295 pfile->line_table->highest_line);
296 else
297 {
5e791406 298 name = _cpp_get_file_name (pfile->main_file);
299 if (!name)
300 abort ();
97bfb9ef 301 }
f9b5f742 302 len = strlen (name);
cc9012f7 303 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
9c343313 304 result = buf;
305 *buf = '"';
306 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
307 *buf++ = '"';
308 *buf = '\0';
69461e0d 309 }
c7e5d924 310 break;
311
79bd622b 312 case BT_INCLUDE_LEVEL:
4087823d 313 /* The line map depth counts the primary source as level 1, but
314 historically __INCLUDE_DEPTH__ has called the primary source
315 level 0. */
ceec9c13 316 number = pfile->line_table->depth - 1;
c7e5d924 317 break;
79bd622b 318
319 case BT_SPECLINE:
320 /* If __LINE__ is embedded in a macro, it must expand to the
321 line of the macro's invocation, not its definition.
322 Otherwise things like assert() will not work properly. */
97bfb9ef 323 number = linemap_get_expansion_line (pfile->line_table,
324 CPP_OPTION (pfile, traditional)
325 ? pfile->line_table->highest_line
326 : pfile->cur_token[-1].src_loc);
c7e5d924 327 break;
79bd622b 328
63994318 329 /* __STDC__ has the value 1 under normal circumstances.
330 However, if (a) we are in a system header, (b) the option
965e9876 331 stdc_0_in_system_headers is true (set by target config), and
332 (c) we are not in strictly conforming mode, then it has the
31614f7c 333 value 0. (b) and (c) are already checked in cpp_init_builtins. */
79bd622b 334 case BT_STDC:
31614f7c 335 if (cpp_in_system_header (pfile))
336 number = 0;
337 else
338 number = 1;
c7e5d924 339 break;
69461e0d 340
79bd622b 341 case BT_DATE:
342 case BT_TIME:
9148bda3 343 if (CPP_OPTION (pfile, warn_date_time))
5b1a0622 344 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
345 "reproducible builds", NODE_NAME (node));
9c343313 346 if (pfile->date == NULL)
79bd622b 347 {
f9b5f742 348 /* Allocate __DATE__ and __TIME__ strings from permanent
349 storage. We only do this once, and don't generate them
350 at init time, because time() and localtime() are very
351 slow on some systems. */
3385506f 352 time_t tt;
353 struct tm *tb = NULL;
354
355 /* (time_t) -1 is a legitimate value for "number of seconds
356 since the Epoch", so we have to do a little dance to
357 distinguish that from a genuine error. */
358 errno = 0;
359 tt = time(NULL);
360 if (tt != (time_t)-1 || errno == 0)
361 tb = localtime (&tt);
362
363 if (tb)
364 {
365 pfile->date = _cpp_unaligned_alloc (pfile,
366 sizeof ("\"Oct 11 1347\""));
367 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
f7fdd7a1 368 monthnames[tb->tm_mon], tb->tm_mday,
369 tb->tm_year + 1900);
3385506f 370
371 pfile->time = _cpp_unaligned_alloc (pfile,
372 sizeof ("\"12:34:56\""));
373 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
374 tb->tm_hour, tb->tm_min, tb->tm_sec);
375 }
376 else
377 {
d80d2074 378 cpp_errno (pfile, CPP_DL_WARNING,
3385506f 379 "could not determine date and time");
380
924bbf02 381 pfile->date = UC"\"??? ?? ????\"";
382 pfile->time = UC"\"??:??:??\"";
3385506f 383 }
79bd622b 384 }
79bd622b 385
c7e5d924 386 if (node->value.builtin == BT_DATE)
9c343313 387 result = pfile->date;
c7e5d924 388 else
9c343313 389 result = pfile->time;
c7e5d924 390 break;
ce079f70 391
392 case BT_COUNTER:
fcde64dc 393 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
394 cpp_error (pfile, CPP_DL_ERROR,
395 "__COUNTER__ expanded inside directive with -fdirectives-only");
ce079f70 396 number = pfile->counter++;
397 break;
33058239 398
399 case BT_HAS_ATTRIBUTE:
400 number = pfile->cb.has_attribute (pfile);
401 break;
9c343313 402 }
403
404 if (result == NULL)
405 {
406 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
407 result = _cpp_unaligned_alloc (pfile, 21);
408 sprintf ((char *) result, "%u", number);
409 }
410
411 return result;
412}
413
414/* Convert builtin macros like __FILE__ to a token and push it on the
d6d3c909 415 context stack. Also handles _Pragma, for which a new token may not
416 be created. Returns 1 if it generates a new token context, 0 to
2e82cf2c 417 return the token to the caller. LOC is the location of the expansion
418 point of the macro. */
9c343313 419static int
2e82cf2c 420builtin_macro (cpp_reader *pfile, cpp_hashnode *node, source_location loc)
9c343313 421{
422 const uchar *buf;
a54e0bf8 423 size_t len;
424 char *nbuf;
c7e5d924 425
9c343313 426 if (node->value.builtin == BT_PRAGMA)
427 {
c7e5d924 428 /* Don't interpret _Pragma within directives. The standard is
429 not clear on this, but to me this makes most sense. */
430 if (pfile->state.in_directive)
431 return 0;
432
2d507e67 433 return _cpp_do__Pragma (pfile);
79bd622b 434 }
c7e5d924 435
9c343313 436 buf = _cpp_builtin_macro_text (pfile, node);
a54e0bf8 437 len = ustrlen (buf);
720aca92 438 nbuf = (char *) alloca (len + 1);
a54e0bf8 439 memcpy (nbuf, buf, len);
440 nbuf[len]='\n';
9c343313 441
9eb74666 442 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
a54e0bf8 443 _cpp_clean_line (pfile);
9c343313 444
445 /* Set pfile->cur_token as required by _cpp_lex_direct. */
446 pfile->cur_token = _cpp_temp_token (pfile);
a4cfdfed 447 cpp_token *token = _cpp_lex_direct (pfile);
2e82cf2c 448 /* We should point to the expansion point of the builtin macro. */
449 token->src_loc = loc;
a4cfdfed 450 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
451 {
452 /* We are tracking tokens resulting from macro expansion.
453 Create a macro line map and generate a virtual location for
454 the token resulting from the expansion of the built-in
455 macro. */
456 source_location *virt_locs = NULL;
457 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
551e34da 458 const line_map_macro * map =
a4cfdfed 459 linemap_enter_macro (pfile->line_table, node,
460 token->src_loc, 1);
461 tokens_buff_add_token (token_buf, virt_locs, token,
462 pfile->line_table->builtin_location,
463 pfile->line_table->builtin_location,
464 map, /*macro_token_index=*/0);
465 push_extended_tokens_context (pfile, node, token_buf, virt_locs,
466 (const cpp_token **)token_buf->base,
467 1);
468 }
469 else
470 _cpp_push_token_context (pfile, NULL, token, 1);
9c343313 471 if (pfile->buffer->cur != pfile->buffer->rlimit)
d80d2074 472 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
9c343313 473 NODE_NAME (node));
474 _cpp_pop_buffer (pfile);
475
c7e5d924 476 return 1;
69461e0d 477}
478
88cf66fa 479/* Copies SRC, of length LEN, to DEST, adding backslashes before all
cc9012f7 480 backslashes and double quotes. DEST must be of sufficient size.
481 Returns a pointer to the end of the string. */
b6d18b0a 482uchar *
f7fdd7a1 483cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
79bd622b 484{
485 while (len--)
486 {
b6d18b0a 487 uchar c = *src++;
69461e0d 488
79bd622b 489 if (c == '\\' || c == '"')
490 {
491 *dest++ = '\\';
492 *dest++ = c;
493 }
494 else
cc9012f7 495 *dest++ = c;
79bd622b 496 }
69461e0d 497
79bd622b 498 return dest;
499}
69461e0d 500
88cf66fa 501/* Convert a token sequence ARG to a single string token according to
502 the rules of the ISO C #-operator. */
f9b5f742 503static const cpp_token *
f7fdd7a1 504stringify_arg (cpp_reader *pfile, macro_arg *arg)
79bd622b 505{
4970d4c2 506 unsigned char *dest;
1fdf6039 507 unsigned int i, escape_it, backslash_count = 0;
f9b5f742 508 const cpp_token *source = NULL;
1fdf6039 509 size_t len;
69461e0d 510
4970d4c2 511 if (BUFF_ROOM (pfile->u_buff) < 3)
512 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
513 dest = BUFF_FRONT (pfile->u_buff);
514 *dest++ = '"';
515
79bd622b 516 /* Loop, reading in the argument's tokens. */
517 for (i = 0; i < arg->count; i++)
518 {
f9b5f742 519 const cpp_token *token = arg->first[i];
f9b5f742 520
521 if (token->type == CPP_PADDING)
522 {
e21164ef 523 if (source == NULL
524 || (!(source->flags & PREV_WHITE)
525 && token->val.source == NULL))
f9b5f742 526 source = token->val.source;
527 continue;
528 }
69461e0d 529
924bbf02 530 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
2a50bfcf 531 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
924bbf02 532 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
538ba11a 533 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
30b1ba42 534 || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR
1608bb4b 535 || cpp_userdef_string_p (token->type)
536 || cpp_userdef_char_p (token->type));
df271348 537
1fdf6039 538 /* Room for each char being written in octal, initial space and
4970d4c2 539 final quote and NUL. */
f9b5f742 540 len = cpp_token_len (token);
79bd622b 541 if (escape_it)
79bd622b 542 len *= 4;
4970d4c2 543 len += 3;
69461e0d 544
1fdf6039 545 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
79bd622b 546 {
1fdf6039 547 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
e6a5f963 548 _cpp_extend_buff (pfile, &pfile->u_buff, len);
1fdf6039 549 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
79bd622b 550 }
69461e0d 551
f9b5f742 552 /* Leading white space? */
4970d4c2 553 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
f9b5f742 554 {
555 if (source == NULL)
556 source = token;
557 if (source->flags & PREV_WHITE)
558 *dest++ = ' ';
559 }
560 source = NULL;
69461e0d 561
79bd622b 562 if (escape_it)
563 {
1fdf6039 564 _cpp_buff *buff = _cpp_get_buff (pfile, len);
565 unsigned char *buf = BUFF_FRONT (buff);
bb1fa6bb 566 len = cpp_spell_token (pfile, token, buf, true) - buf;
25266990 567 dest = cpp_quote_string (dest, buf, len);
1fdf6039 568 _cpp_release_buff (pfile, buff);
79bd622b 569 }
570 else
bb1fa6bb 571 dest = cpp_spell_token (pfile, token, dest, true);
79bd622b 572
bc205914 573 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
79bd622b 574 backslash_count++;
575 else
576 backslash_count = 0;
577 }
578
579 /* Ignore the final \ of invalid string literals. */
580 if (backslash_count & 1)
581 {
d80d2074 582 cpp_error (pfile, CPP_DL_WARNING,
73328dce 583 "invalid string literal, ignoring final '\\'");
1fdf6039 584 dest--;
79bd622b 585 }
586
f9b5f742 587 /* Commit the memory, including NUL, and return the token. */
4970d4c2 588 *dest++ = '"';
1fdf6039 589 len = dest - BUFF_FRONT (pfile->u_buff);
590 BUFF_FRONT (pfile->u_buff) = dest + 1;
591 return new_string_token (pfile, dest - len, len);
79bd622b 592}
593
d10cfa8d 594/* Try to paste two tokens. On success, return nonzero. In any
1785b647 595 case, PLHS is updated to point to the pasted token, which is
8c6425eb 596 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
597 the virtual location used for error reporting. */
1785b647 598static bool
8c6425eb 599paste_tokens (cpp_reader *pfile, source_location location,
600 const cpp_token **plhs, const cpp_token *rhs)
08138c65 601{
46139d3f 602 unsigned char *buf, *end, *lhsend;
3127c357 603 cpp_token *lhs;
1785b647 604 unsigned int len;
1785b647 605
3127c357 606 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
720aca92 607 buf = (unsigned char *) alloca (len);
109ca87a 608 end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
1785b647 609
610 /* Avoid comment headers, since they are still processed in stage 3.
611 It is simpler to insert a space here, rather than modifying the
612 lexer to ignore comments in some circumstances. Simply returning
613 false doesn't work, since we want to clear the PASTE_LEFT flag. */
3127c357 614 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
1785b647 615 *end++ = ' ';
1991d8eb 616 /* In one obscure case we might see padding here. */
617 if (rhs->type != CPP_PADDING)
109ca87a 618 end = cpp_spell_token (pfile, rhs, end, true);
a54e0bf8 619 *end = '\n';
1785b647 620
9eb74666 621 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
a54e0bf8 622 _cpp_clean_line (pfile);
1785b647 623
624 /* Set pfile->cur_token as required by _cpp_lex_direct. */
625 pfile->cur_token = _cpp_temp_token (pfile);
3127c357 626 lhs = _cpp_lex_direct (pfile);
46139d3f 627 if (pfile->buffer->cur != pfile->buffer->rlimit)
628 {
3127c357 629 source_location saved_loc = lhs->src_loc;
630
46139d3f 631 _cpp_pop_buffer (pfile);
632 _cpp_backup_tokens (pfile, 1);
633 *lhsend = '\0';
634
3127c357 635 /* We have to remove the PASTE_LEFT flag from the old lhs, but
636 we want to keep the new location. */
637 *lhs = **plhs;
638 *plhs = lhs;
639 lhs->src_loc = saved_loc;
640 lhs->flags &= ~PASTE_LEFT;
641
46139d3f 642 /* Mandatory error for all apart from assembler. */
643 if (CPP_OPTION (pfile, lang) != CLK_ASM)
8c6425eb 644 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
46139d3f 645 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
646 buf, cpp_token_as_text (pfile, rhs));
647 return false;
648 }
1785b647 649
3127c357 650 *plhs = lhs;
46139d3f 651 _cpp_pop_buffer (pfile);
652 return true;
08138c65 653}
654
88cf66fa 655/* Handles an arbitrarily long sequence of ## operators, with initial
656 operand LHS. This implementation is left-associative,
657 non-recursive, and finishes a paste before handling succeeding
658 ones. If a paste fails, we back up to the RHS of the failing ##
659 operator before pushing the context containing the result of prior
660 successful pastes, with the effect that the RHS appears in the
661 output stream after the pasted LHS normally. */
79bd622b 662static void
f7fdd7a1 663paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
79bd622b 664{
ce70f433 665 const cpp_token *rhs = NULL;
f9b5f742 666 cpp_context *context = pfile->context;
c55700de 667 source_location virt_loc = 0;
668
8c6425eb 669 /* We are expanding a macro and we must have been called on a token
670 that appears at the left hand side of a ## operator. */
671 if (macro_of_context (pfile->context) == NULL
672 || (!(lhs->flags & PASTE_LEFT)))
c55700de 673 abort ();
674
675 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
676 /* The caller must have called consume_next_token_from_context
677 right before calling us. That has incremented the pointer to
678 the current virtual location. So it now points to the location
679 of the token that comes right after *LHS. We want the
680 resulting pasted token to have the location of the current
681 *LHS, though. */
682 virt_loc = context->c.mc->cur_virt_loc[-1];
8c6425eb 683 else
684 /* We are not tracking macro expansion. So the best virtual
685 location we can get here is the expansion point of the macro we
686 are currently expanding. */
687 virt_loc = pfile->invocation_location;
f9b5f742 688
79bd622b 689 do
690 {
691 /* Take the token directly from the current context. We can do
692 this, because we are in the replacement list of either an
693 object-like macro, or a function-like macro with arguments
694 inserted. In either case, the constraints to #define
08138c65 695 guarantee we have at least one more token. */
ce70f433 696 if (context->tokens_kind == TOKENS_KIND_DIRECT)
a854276a 697 rhs = FIRST (context).token++;
ce70f433 698 else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
a854276a 699 rhs = *FIRST (context).ptoken++;
ce70f433 700 else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
701 {
702 /* So we are in presence of an extended token context, which
703 means that each token in this context has a virtual
704 location attached to it. So let's not forget to update
705 the pointer to the current virtual location of the
706 current token when we update the pointer to the current
707 token */
708
709 rhs = *FIRST (context).ptoken++;
710 /* context->c.mc must be non-null, as if we were not in a
711 macro context, context->tokens_kind could not be equal to
712 TOKENS_KIND_EXTENDED. */
713 context->c.mc->cur_virt_loc++;
714 }
f9b5f742 715
716 if (rhs->type == CPP_PADDING)
1991d8eb 717 {
718 if (rhs->flags & PASTE_LEFT)
719 abort ();
720 }
8c6425eb 721 if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
46139d3f 722 break;
79bd622b 723 }
724 while (rhs->flags & PASTE_LEFT);
725
1785b647 726 /* Put the resulting token in its own context. */
c55700de 727 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
728 {
729 source_location *virt_locs = NULL;
730 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
731 tokens_buff_add_token (token_buf, virt_locs, lhs,
732 virt_loc, 0, NULL, 0);
733 push_extended_tokens_context (pfile, context->c.mc->macro_node,
734 token_buf, virt_locs,
735 (const cpp_token **)token_buf->base, 1);
736 }
737 else
738 _cpp_push_token_context (pfile, NULL, lhs, 1);
79bd622b 739}
740
06025647 741/* Returns TRUE if the number of arguments ARGC supplied in an
742 invocation of the MACRO referenced by NODE is valid. An empty
743 invocation to a macro with no parameters should pass ARGC as zero.
744
745 Note that MACRO cannot necessarily be deduced from NODE, in case
746 NODE was redefined whilst collecting arguments. */
747bool
f7fdd7a1 748_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
06025647 749{
750 if (argc == macro->paramc)
751 return true;
752
753 if (argc < macro->paramc)
754 {
49f161b6 755 /* As an extension, variadic arguments are allowed to not appear in
06025647 756 the invocation at all.
757 e.g. #define debug(format, args...) something
758 debug("string");
759
49f161b6 760 This is exactly the same as if an empty variadic list had been
761 supplied - debug("string", ). */
06025647 762
763 if (argc + 1 == macro->paramc && macro->variadic)
764 {
765 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
49f161b6 766 {
767 if (CPP_OPTION (pfile, cplusplus))
768 cpp_error (pfile, CPP_DL_PEDWARN,
769 "ISO C++11 requires at least one argument "
770 "for the \"...\" in a variadic macro");
771 else
772 cpp_error (pfile, CPP_DL_PEDWARN,
773 "ISO C99 requires at least one argument "
774 "for the \"...\" in a variadic macro");
775 }
06025647 776 return true;
777 }
778
d80d2074 779 cpp_error (pfile, CPP_DL_ERROR,
06025647 780 "macro \"%s\" requires %u arguments, but only %u given",
781 NODE_NAME (node), macro->paramc, argc);
782 }
783 else
d80d2074 784 cpp_error (pfile, CPP_DL_ERROR,
06025647 785 "macro \"%s\" passed %u arguments, but takes just %u",
786 NODE_NAME (node), argc, macro->paramc);
787
788 return false;
789}
790
88cf66fa 791/* Reads and returns the arguments to a function-like macro
792 invocation. Assumes the opening parenthesis has been processed.
793 If there is an error, emits an appropriate diagnostic and returns
794 NULL. Each argument is terminated by a CPP_EOF token, for the
45f9f140 795 future benefit of expand_arg(). If there are any deferred
796 #pragma directives among macro arguments, store pointers to the
ce70f433 797 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
798
799 What is returned is the buffer that contains the memory allocated
800 to hold the macro arguments. NODE is the name of the macro this
801 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
802 set to the actual number of macro arguments allocated in the
803 returned buffer. */
06c92cbc 804static _cpp_buff *
45f9f140 805collect_args (cpp_reader *pfile, const cpp_hashnode *node,
ce70f433 806 _cpp_buff **pragma_buff, unsigned *num_args)
79bd622b 807{
06c92cbc 808 _cpp_buff *buff, *base_buff;
809 cpp_macro *macro;
810 macro_arg *args, *arg;
811 const cpp_token *token;
812 unsigned int argc;
ce70f433 813 source_location virt_loc;
814 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
815 unsigned num_args_alloced = 0;
06c92cbc 816
817 macro = node->value.macro;
818 if (macro->paramc)
819 argc = macro->paramc;
820 else
821 argc = 1;
ce70f433 822
823#define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
824#define ARG_TOKENS_EXTENT 1000
825
826 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
827 * sizeof (cpp_token *)
06c92cbc 828 + sizeof (macro_arg)));
829 base_buff = buff;
830 args = (macro_arg *) buff->base;
831 memset (args, 0, argc * sizeof (macro_arg));
1fdf6039 832 buff->cur = (unsigned char *) &args[argc];
06c92cbc 833 arg = args, argc = 0;
834
835 /* Collect the tokens making up each argument. We don't yet know
836 how many arguments have been supplied, whether too many or too
837 few. Hence the slightly bizarre usage of "argc" and "arg". */
838 do
79bd622b 839 {
06c92cbc 840 unsigned int paren_depth = 0;
841 unsigned int ntokens = 0;
ce70f433 842 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
843 num_args_alloced++;
79bd622b 844
06c92cbc 845 argc++;
846 arg->first = (const cpp_token **) buff->cur;
ce70f433 847 if (track_macro_expansion_p)
848 {
849 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
850 arg->virt_locs = XNEWVEC (source_location,
851 virt_locs_capacity);
852 }
69461e0d 853
06c92cbc 854 for (;;)
855 {
856 /* Require space for 2 new tokens (including a CPP_EOF). */
1fdf6039 857 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
06c92cbc 858 {
e6a5f963 859 buff = _cpp_append_extend_buff (pfile, buff,
ce70f433 860 ARG_TOKENS_EXTENT
861 * sizeof (cpp_token *));
06c92cbc 862 arg->first = (const cpp_token **) buff->cur;
863 }
ce70f433 864 if (track_macro_expansion_p
865 && (ntokens + 2 > virt_locs_capacity))
866 {
867 virt_locs_capacity += ARG_TOKENS_EXTENT;
868 arg->virt_locs = XRESIZEVEC (source_location,
869 arg->virt_locs,
870 virt_locs_capacity);
871 }
f9b5f742 872
ce70f433 873 token = cpp_get_token_1 (pfile, &virt_loc);
69461e0d 874
06c92cbc 875 if (token->type == CPP_PADDING)
876 {
877 /* Drop leading padding. */
878 if (ntokens == 0)
879 continue;
880 }
881 else if (token->type == CPP_OPEN_PAREN)
882 paren_depth++;
883 else if (token->type == CPP_CLOSE_PAREN)
884 {
885 if (paren_depth-- == 0)
886 break;
887 }
888 else if (token->type == CPP_COMMA)
889 {
890 /* A comma does not terminate an argument within
891 parentheses or as part of a variable argument. */
892 if (paren_depth == 0
893 && ! (macro->variadic && argc == macro->paramc))
894 break;
895 }
896 else if (token->type == CPP_EOF
897 || (token->type == CPP_HASH && token->flags & BOL))
898 break;
45f9f140 899 else if (token->type == CPP_PRAGMA)
900 {
901 cpp_token *newtok = _cpp_temp_token (pfile);
902
903 /* CPP_PRAGMA token lives in directive_result, which will
904 be overwritten on the next directive. */
905 *newtok = *token;
906 token = newtok;
907 do
908 {
909 if (*pragma_buff == NULL
910 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
911 {
912 _cpp_buff *next;
913 if (*pragma_buff == NULL)
914 *pragma_buff
915 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
916 else
917 {
918 next = *pragma_buff;
919 *pragma_buff
920 = _cpp_get_buff (pfile,
921 (BUFF_FRONT (*pragma_buff)
922 - (*pragma_buff)->base) * 2);
923 (*pragma_buff)->next = next;
924 }
925 }
926 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
927 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
928 if (token->type == CPP_PRAGMA_EOL)
929 break;
ce70f433 930 token = cpp_get_token_1 (pfile, &virt_loc);
45f9f140 931 }
932 while (token->type != CPP_EOF);
933
934 /* In deferred pragmas parsing_args and prevent_expansion
935 had been changed, reset it. */
936 pfile->state.parsing_args = 2;
937 pfile->state.prevent_expansion = 1;
938
939 if (token->type == CPP_EOF)
940 break;
941 else
942 continue;
943 }
ce70f433 944 set_arg_token (arg, token, virt_loc,
945 ntokens, MACRO_ARG_TOKEN_NORMAL,
946 CPP_OPTION (pfile, track_macro_expansion));
947 ntokens++;
06c92cbc 948 }
79bd622b 949
06c92cbc 950 /* Drop trailing padding. */
951 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
952 ntokens--;
79bd622b 953
06c92cbc 954 arg->count = ntokens;
ce70f433 955 set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
956 ntokens, MACRO_ARG_TOKEN_NORMAL,
957 CPP_OPTION (pfile, track_macro_expansion));
79bd622b 958
06c92cbc 959 /* Terminate the argument. Excess arguments loop back and
960 overwrite the final legitimate argument, before failing. */
961 if (argc <= macro->paramc)
962 {
1fdf6039 963 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
06c92cbc 964 if (argc != macro->paramc)
965 arg++;
966 }
79bd622b 967 }
d6af0368 968 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
79bd622b 969
d6af0368 970 if (token->type == CPP_EOF)
79bd622b 971 {
1fdf6039 972 /* We still need the CPP_EOF to end directives, and to end
973 pre-expansion of a macro argument. Step back is not
974 unconditional, since we don't want to return a CPP_EOF to our
975 callers at the end of an -include-d file. */
d6af0368 976 if (pfile->context->prev || pfile->state.in_directive)
06c92cbc 977 _cpp_backup_tokens (pfile, 1);
d80d2074 978 cpp_error (pfile, CPP_DL_ERROR,
73328dce 979 "unterminated argument list invoking macro \"%s\"",
c86dbc5b 980 NODE_NAME (node));
79bd622b 981 }
06025647 982 else
79bd622b 983 {
06025647 984 /* A single empty argument is counted as no argument. */
985 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
986 argc = 0;
987 if (_cpp_arguments_ok (pfile, macro, node, argc))
ed909a09 988 {
989 /* GCC has special semantics for , ## b where b is a varargs
990 parameter: we remove the comma if b was omitted entirely.
991 If b was merely an empty argument, the comma is retained.
992 If the macro takes just one (varargs) parameter, then we
993 retain the comma only if we are standards conforming.
994
995 If FIRST is NULL replace_args () swallows the comma. */
996 if (macro->variadic && (argc < macro->paramc
997 || (argc == 1 && args[0].count == 0
998 && !CPP_OPTION (pfile, std))))
999 args[macro->paramc - 1].first = NULL;
ce70f433 1000 if (num_args)
1001 *num_args = num_args_alloced;
ed909a09 1002 return base_buff;
1003 }
79bd622b 1004 }
1005
06025647 1006 /* An error occurred. */
06c92cbc 1007 _cpp_release_buff (pfile, base_buff);
1008 return NULL;
79bd622b 1009}
1010
2b0e25fe 1011/* Search for an opening parenthesis to the macro of NODE, in such a
1012 way that, if none is found, we don't lose the information in any
1013 intervening padding tokens. If we find the parenthesis, collect
45f9f140 1014 the arguments and return the buffer containing them. PRAGMA_BUFF
ce70f433 1015 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1016 *NUM_ARGS is set to the number of arguments contained in the
1017 returned buffer. */
2b0e25fe 1018static _cpp_buff *
45f9f140 1019funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
ce70f433 1020 _cpp_buff **pragma_buff, unsigned *num_args)
79bd622b 1021{
2b0e25fe 1022 const cpp_token *token, *padding = NULL;
f9b5f742 1023
2b0e25fe 1024 for (;;)
fb5ab82c 1025 {
2b0e25fe 1026 token = cpp_get_token (pfile);
1027 if (token->type != CPP_PADDING)
1028 break;
1029 if (padding == NULL
1030 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
1031 padding = token;
fb5ab82c 1032 }
79bd622b 1033
2b0e25fe 1034 if (token->type == CPP_OPEN_PAREN)
79bd622b 1035 {
2b0e25fe 1036 pfile->state.parsing_args = 2;
ce70f433 1037 return collect_args (pfile, node, pragma_buff, num_args);
79bd622b 1038 }
1039
7e2fc40e 1040 /* CPP_EOF can be the end of macro arguments, or the end of the
1041 file. We mustn't back up over the latter. Ugh. */
1042 if (token->type != CPP_EOF || token == &pfile->eof)
1043 {
1044 /* Back up. We may have skipped padding, in which case backing
1045 up more than one token when expanding macros is in general
1046 too difficult. We re-insert it in its own context. */
1047 _cpp_backup_tokens (pfile, 1);
1048 if (padding)
b75b98aa 1049 _cpp_push_token_context (pfile, NULL, padding, 1);
7e2fc40e 1050 }
2b0e25fe 1051
1052 return NULL;
79bd622b 1053}
1054
941f2388 1055/* Return the real number of tokens in the expansion of MACRO. */
1056static inline unsigned int
1057macro_real_token_count (const cpp_macro *macro)
1058{
1059 unsigned int i;
1060 if (__builtin_expect (!macro->extra_tokens, true))
1061 return macro->count;
1062 for (i = 0; i < macro->count; i++)
1063 if (macro->exp.tokens[i].type == CPP_PASTE)
1064 return i;
1065 abort ();
1066}
1067
88cf66fa 1068/* Push the context of a macro with hash entry NODE onto the context
1069 stack. If we can successfully expand the macro, we push a context
1070 containing its yet-to-be-rescanned replacement list and return one.
ce70f433 1071 If there were additionally any unexpanded deferred #pragma
1072 directives among macro arguments, push another context containing
1073 the pragma tokens before the yet-to-be-rescanned replacement list
1074 and return two. Otherwise, we don't push a context and return
1075 zero. LOCATION is the location of the expansion point of the
1076 macro. */
69461e0d 1077static int
45f9f140 1078enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
ce70f433 1079 const cpp_token *result, source_location location)
69461e0d 1080{
88cf66fa 1081 /* The presence of a macro invalidates a file's controlling macro. */
c7e5d924 1082 pfile->mi_valid = false;
1083
606942e3 1084 pfile->state.angled_headers = false;
1085
8c6425eb 1086 /* From here to when we push the context for the macro later down
1087 this function, we need to flag the fact that we are about to
1088 expand a macro. This is useful when -ftrack-macro-expansion is
1089 turned off. In that case, we need to record the location of the
1090 expansion point of the top-most macro we are about to to expand,
1091 into pfile->invocation_location. But we must not record any such
1092 location once the process of expanding the macro starts; that is,
1093 we must not do that recording between now and later down this
1094 function where set this flag to FALSE. */
1095 pfile->about_to_expand_macro_p = true;
1096
34c3de48 1097 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
1098 {
1099 node->flags |= NODE_USED;
25692381 1100 if ((!pfile->cb.user_builtin_macro
1101 || !pfile->cb.user_builtin_macro (pfile, node))
1102 && pfile->cb.used_define)
34c3de48 1103 pfile->cb.used_define (pfile, pfile->directive_line, node);
1104 }
1105
88cf66fa 1106 /* Handle standard macros. */
c7e5d924 1107 if (! (node->flags & NODE_BUILTIN))
69461e0d 1108 {
f9b5f742 1109 cpp_macro *macro = node->value.macro;
45f9f140 1110 _cpp_buff *pragma_buff = NULL;
3c7df4d3 1111
2b0e25fe 1112 if (macro->fun_like)
1113 {
1114 _cpp_buff *buff;
ce70f433 1115 unsigned num_args = 0;
2b0e25fe 1116
1117 pfile->state.prevent_expansion++;
1118 pfile->keep_tokens++;
1119 pfile->state.parsing_args = 1;
ce70f433 1120 buff = funlike_invocation_p (pfile, node, &pragma_buff,
1121 &num_args);
2b0e25fe 1122 pfile->state.parsing_args = 0;
1123 pfile->keep_tokens--;
1124 pfile->state.prevent_expansion--;
1125
1126 if (buff == NULL)
1127 {
1128 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
3a79f5da 1129 cpp_warning (pfile, CPP_W_TRADITIONAL,
2b0e25fe 1130 "function-like macro \"%s\" must be used with arguments in traditional C",
3a79f5da 1131 NODE_NAME (node));
2b0e25fe 1132
45f9f140 1133 if (pragma_buff)
1134 _cpp_release_buff (pfile, pragma_buff);
1135
8c6425eb 1136 pfile->about_to_expand_macro_p = false;
2b0e25fe 1137 return 0;
1138 }
1139
d6af0368 1140 if (macro->paramc > 0)
ce70f433 1141 replace_args (pfile, node, macro,
1142 (macro_arg *) buff->base,
1143 location);
1144 /* Free the memory used by the arguments of this
1145 function-like macro. This memory has been allocated by
1146 funlike_invocation_p and by replace_args. */
1147 delete_macro_args (buff, num_args);
2b0e25fe 1148 }
79bd622b 1149
f9b5f742 1150 /* Disable the macro within its expansion. */
c7e5d924 1151 node->flags |= NODE_DISABLED;
79bd622b 1152
34c3de48 1153 if (!(node->flags & NODE_USED))
1154 {
1155 node->flags |= NODE_USED;
1156 if (pfile->cb.used_define)
1157 pfile->cb.used_define (pfile, pfile->directive_line, node);
1158 }
1159
116f0a5f 1160 if (pfile->cb.used)
ce70f433 1161 pfile->cb.used (pfile, location, node);
116f0a5f 1162
71a7c282 1163 macro->used = 1;
1164
f9b5f742 1165 if (macro->paramc == 0)
ce70f433 1166 {
9b48364f 1167 unsigned tokens_count = macro_real_token_count (macro);
ce70f433 1168 if (CPP_OPTION (pfile, track_macro_expansion))
1169 {
9b48364f 1170 unsigned int i;
ce70f433 1171 const cpp_token *src = macro->exp.tokens;
551e34da 1172 const line_map_macro *map;
ce70f433 1173 source_location *virt_locs = NULL;
9b48364f 1174 _cpp_buff *macro_tokens
1175 = tokens_buff_new (pfile, tokens_count, &virt_locs);
ce70f433 1176
1177 /* Create a macro map to record the locations of the
1178 tokens that are involved in the expansion. LOCATION
1179 is the location of the macro expansion point. */
9b48364f 1180 map = linemap_enter_macro (pfile->line_table,
1181 node, location, tokens_count);
1182 for (i = 0; i < tokens_count; ++i)
ce70f433 1183 {
1184 tokens_buff_add_token (macro_tokens, virt_locs,
1185 src, src->src_loc,
1186 src->src_loc, map, i);
1187 ++src;
1188 }
1189 push_extended_tokens_context (pfile, node,
1190 macro_tokens,
1191 virt_locs,
1192 (const cpp_token **)
1193 macro_tokens->base,
9b48364f 1194 tokens_count);
ce70f433 1195 }
1196 else
9b48364f 1197 _cpp_push_token_context (pfile, node, macro->exp.tokens,
1198 tokens_count);
1199 num_macro_tokens_counter += tokens_count;
ce70f433 1200 }
c7e5d924 1201
45f9f140 1202 if (pragma_buff)
1203 {
1204 if (!pfile->state.in_directive)
1205 _cpp_push_token_context (pfile, NULL,
1206 padding_token (pfile, result), 1);
1207 do
1208 {
e77b8253 1209 unsigned tokens_count;
45f9f140 1210 _cpp_buff *tail = pragma_buff->next;
1211 pragma_buff->next = NULL;
e77b8253 1212 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1213 - (const cpp_token **) pragma_buff->base);
45f9f140 1214 push_ptoken_context (pfile, NULL, pragma_buff,
1215 (const cpp_token **) pragma_buff->base,
e77b8253 1216 tokens_count);
45f9f140 1217 pragma_buff = tail;
e77b8253 1218 if (!CPP_OPTION (pfile, track_macro_expansion))
1219 num_macro_tokens_counter += tokens_count;
1220
45f9f140 1221 }
1222 while (pragma_buff != NULL);
8c6425eb 1223 pfile->about_to_expand_macro_p = false;
45f9f140 1224 return 2;
1225 }
1226
8c6425eb 1227 pfile->about_to_expand_macro_p = false;
c7e5d924 1228 return 1;
69461e0d 1229 }
c7e5d924 1230
8c6425eb 1231 pfile->about_to_expand_macro_p = false;
88cf66fa 1232 /* Handle built-in macros and the _Pragma operator. */
825751e2 1233 {
1234 source_location loc;
1235 if (/* The top-level macro invocation that triggered the expansion
1236 we are looking at is with a standard macro ...*/
1237 !(pfile->top_most_macro_node->flags & NODE_BUILTIN)
1238 /* ... and it's a function-like macro invocation. */
1239 && pfile->top_most_macro_node->value.macro->fun_like)
1240 /* Then the location of the end of the macro invocation is the
1241 location of the closing parenthesis. */
1242 loc = pfile->cur_token[-1].src_loc;
1243 else
1244 /* Otherwise, the location of the end of the macro invocation is
1245 the location of the expansion point of that top-level macro
1246 invocation. */
1247 loc = location;
1248
1249 return builtin_macro (pfile, node, loc);
1250 }
79bd622b 1251}
1252
ce70f433 1253/* De-allocate the memory used by BUFF which is an array of instances
1254 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1255 present in BUFF. */
1256static void
1257delete_macro_args (_cpp_buff *buff, unsigned num_args)
1258{
1259 macro_arg *macro_args;
1260 unsigned i;
1261
1262 if (buff == NULL)
1263 return;
1264
1265 macro_args = (macro_arg *) buff->base;
1266
1267 /* Walk instances of macro_arg to free their expanded tokens as well
1268 as their macro_arg::virt_locs members. */
1269 for (i = 0; i < num_args; ++i)
1270 {
1271 if (macro_args[i].expanded)
1272 {
1273 free (macro_args[i].expanded);
1274 macro_args[i].expanded = NULL;
1275 }
1276 if (macro_args[i].virt_locs)
1277 {
1278 free (macro_args[i].virt_locs);
1279 macro_args[i].virt_locs = NULL;
1280 }
1281 if (macro_args[i].expanded_virt_locs)
1282 {
1283 free (macro_args[i].expanded_virt_locs);
1284 macro_args[i].expanded_virt_locs = NULL;
1285 }
1286 }
1287 _cpp_free_buff (buff);
1288}
1289
1290/* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1291 to set, LOCATION is its virtual location. "Virtual" location means
e8aa7eaf 1292 the location that encodes loci across macro expansion. Otherwise
ce70f433 1293 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1294 argument ARG is supposed to contain. Note that ARG must be
1295 tailored so that it has enough room to contain INDEX + 1 numbers of
1296 tokens, at least. */
1297static void
1298set_arg_token (macro_arg *arg, const cpp_token *token,
1299 source_location location, size_t index,
1300 enum macro_arg_token_kind kind,
1301 bool track_macro_exp_p)
1302{
1303 const cpp_token **token_ptr;
1304 source_location *loc = NULL;
1305
1306 token_ptr =
1307 arg_token_ptr_at (arg, index, kind,
1308 track_macro_exp_p ? &loc : NULL);
1309 *token_ptr = token;
1310
1311 if (loc != NULL)
1312 {
1313#ifdef ENABLE_CHECKING
1314 if (kind == MACRO_ARG_TOKEN_STRINGIFIED
1315 || !track_macro_exp_p)
1316 /* We can't set the location of a stringified argument
1317 token and we can't set any location if we aren't tracking
1318 macro expansion locations. */
1319 abort ();
1320#endif
1321 *loc = location;
1322 }
1323}
1324
1325/* Get the pointer to the location of the argument token of the
1326 function-like macro argument ARG. This function must be called
1327 only when we -ftrack-macro-expansion is on. */
1328static const source_location *
1329get_arg_token_location (const macro_arg *arg,
1330 enum macro_arg_token_kind kind)
1331{
1332 const source_location *loc = NULL;
1333 const cpp_token **token_ptr =
1334 arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1335
1336 if (token_ptr == NULL)
1337 return NULL;
1338
1339 return loc;
1340}
1341
1342/* Return the pointer to the INDEXth token of the macro argument ARG.
1343 KIND specifies the kind of token the macro argument ARG contains.
1344 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1345 of the virtual location of the returned token if the
1346 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1347 spelling location of the returned token. */
1348static const cpp_token **
1349arg_token_ptr_at (const macro_arg *arg, size_t index,
1350 enum macro_arg_token_kind kind,
1351 source_location **virt_location)
1352{
1353 const cpp_token **tokens_ptr = NULL;
1354
1355 switch (kind)
1356 {
1357 case MACRO_ARG_TOKEN_NORMAL:
1358 tokens_ptr = arg->first;
1359 break;
1360 case MACRO_ARG_TOKEN_STRINGIFIED:
1361 tokens_ptr = (const cpp_token **) &arg->stringified;
1362 break;
1363 case MACRO_ARG_TOKEN_EXPANDED:
1364 tokens_ptr = arg->expanded;
1365 break;
1366 }
1367
1368 if (tokens_ptr == NULL)
1369 /* This can happen for e.g, an empty token argument to a
1370 funtion-like macro. */
1371 return tokens_ptr;
1372
1373 if (virt_location)
1374 {
1375 if (kind == MACRO_ARG_TOKEN_NORMAL)
1376 *virt_location = &arg->virt_locs[index];
1377 else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1378 *virt_location = &arg->expanded_virt_locs[index];
1379 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1380 *virt_location =
1381 (source_location *) &tokens_ptr[index]->src_loc;
1382 }
1383 return &tokens_ptr[index];
1384}
1385
1386/* Initialize an iterator so that it iterates over the tokens of a
1387 function-like macro argument. KIND is the kind of tokens we want
1388 ITER to iterate over. TOKEN_PTR points the first token ITER will
1389 iterate over. */
1390static void
1391macro_arg_token_iter_init (macro_arg_token_iter *iter,
1392 bool track_macro_exp_p,
1393 enum macro_arg_token_kind kind,
1394 const macro_arg *arg,
1395 const cpp_token **token_ptr)
1396{
1397 iter->track_macro_exp_p = track_macro_exp_p;
1398 iter->kind = kind;
1399 iter->token_ptr = token_ptr;
2a688977 1400 /* Unconditionally initialize this so that the compiler doesn't warn
1401 about iter->location_ptr being possibly uninitialized later after
1402 this code has been inlined somewhere. */
1403 iter->location_ptr = NULL;
ce70f433 1404 if (track_macro_exp_p)
1405 iter->location_ptr = get_arg_token_location (arg, kind);
1406#ifdef ENABLE_CHECKING
1407 iter->num_forwards = 0;
1408 if (track_macro_exp_p
1409 && token_ptr != NULL
1410 && iter->location_ptr == NULL)
1411 abort ();
1412#endif
1413}
1414
1415/* Move the iterator one token forward. Note that if IT was
1416 initialized on an argument that has a stringified token, moving it
e8aa7eaf 1417 forward doesn't make sense as a stringified token is essentially one
ce70f433 1418 string. */
1419static void
1420macro_arg_token_iter_forward (macro_arg_token_iter *it)
1421{
1422 switch (it->kind)
1423 {
1424 case MACRO_ARG_TOKEN_NORMAL:
1425 case MACRO_ARG_TOKEN_EXPANDED:
1426 it->token_ptr++;
1427 if (it->track_macro_exp_p)
1428 it->location_ptr++;
1429 break;
1430 case MACRO_ARG_TOKEN_STRINGIFIED:
1431#ifdef ENABLE_CHECKING
1432 if (it->num_forwards > 0)
1433 abort ();
1434#endif
1435 break;
1436 }
1437
1438#ifdef ENABLE_CHECKING
1439 it->num_forwards++;
1440#endif
1441}
1442
1443/* Return the token pointed to by the iterator. */
1444static const cpp_token *
1445macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1446{
1447#ifdef ENABLE_CHECKING
1448 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1449 && it->num_forwards > 0)
1450 abort ();
1451#endif
1452 if (it->token_ptr == NULL)
1453 return NULL;
1454 return *it->token_ptr;
1455}
1456
1457/* Return the location of the token pointed to by the iterator.*/
1458static source_location
1459macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1460{
1461#ifdef ENABLE_CHECKING
1462 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1463 && it->num_forwards > 0)
1464 abort ();
1465#endif
1466 if (it->track_macro_exp_p)
1467 return *it->location_ptr;
1468 else
1469 return (*it->token_ptr)->src_loc;
1470}
1471
1472/* Return the index of a token [resulting from macro expansion] inside
1473 the total list of tokens resulting from a given macro
1474 expansion. The index can be different depending on whether if we
1475 want each tokens resulting from function-like macro arguments
1476 expansion to have a different location or not.
1477
1478 E.g, consider this function-like macro:
1479
1480 #define M(x) x - 3
1481
1482 Then consider us "calling" it (and thus expanding it) like:
1483
1484 M(1+4)
1485
1486 It will be expanded into:
1487
1488 1+4-3
1489
1490 Let's consider the case of the token '4'.
1491
1492 Its index can be 2 (it's the third token of the set of tokens
1493 resulting from the expansion) or it can be 0 if we consider that
1494 all tokens resulting from the expansion of the argument "1+2" have
1495 the same index, which is 0. In this later case, the index of token
1496 '-' would then be 1 and the index of token '3' would be 2.
1497
1498 The later case is useful to use less memory e.g, for the case of
1499 the user using the option -ftrack-macro-expansion=1.
1500
1501 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1502 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1503 parameter (inside the macro replacement list) that corresponds to
1504 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1505 of.
1506
1507 If we refer to the example above, for the '4' argument token,
1508 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1509 would be set to the token 'x', in the replacement list "x - 3" of
1510 macro M.
1511
1512 This is a subroutine of replace_args. */
1513inline static unsigned
1514expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1515 const cpp_token *cur_replacement_token,
1516 unsigned absolute_token_index)
1517{
1518 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1519 return absolute_token_index;
1520 return cur_replacement_token - macro->exp.tokens;
1521}
1522
88cf66fa 1523/* Replace the parameters in a function-like macro of NODE with the
1524 actual ARGS, and place the result in a newly pushed token context.
1525 Expand each argument before replacing, unless it is operated upon
ce70f433 1526 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1527 the expansion point of the macro. E.g, the location of the
1528 function-like macro invocation. */
79bd622b 1529static void
ce70f433 1530replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1531 macro_arg *args, source_location expansion_point_loc)
79bd622b 1532{
1533 unsigned int i, total;
1534 const cpp_token *src, *limit;
ce70f433 1535 const cpp_token **first = NULL;
79bd622b 1536 macro_arg *arg;
ce70f433 1537 _cpp_buff *buff = NULL;
1538 source_location *virt_locs = NULL;
1539 unsigned int exp_count;
551e34da 1540 const line_map_macro *map = NULL;
ce70f433 1541 int track_macro_exp;
79bd622b 1542
79bd622b 1543 /* First, fully macro-expand arguments, calculating the number of
084163dc 1544 tokens in the final expansion as we go. The ordering of the if
1545 statements below is subtle; we must handle stringification before
1546 pasting. */
ce70f433 1547
1548 /* EXP_COUNT is the number of tokens in the macro replacement
1549 list. TOTAL is the number of tokens /after/ macro parameters
1550 have been replaced by their arguments. */
1551 exp_count = macro_real_token_count (macro);
1552 total = exp_count;
1553 limit = macro->exp.tokens + exp_count;
f9b5f742 1554
672f38da 1555 for (src = macro->exp.tokens; src < limit; src++)
79bd622b 1556 if (src->type == CPP_MACRO_ARG)
1557 {
f9b5f742 1558 /* Leading and trailing padding tokens. */
1559 total += 2;
ce70f433 1560 /* Account for leading and padding tokens in exp_count too.
1561 This is going to be important later down this function,
1562 when we want to handle the case of (track_macro_exp <
1563 2). */
1564 exp_count += 2;
f9b5f742 1565
79bd622b 1566 /* We have an argument. If it is not being stringified or
1567 pasted it is macro-replaced before insertion. */
2ee04baa 1568 arg = &args[src->val.macro_arg.arg_no - 1];
3c7df4d3 1569
79bd622b 1570 if (src->flags & STRINGIFY_ARG)
1571 {
1572 if (!arg->stringified)
f9b5f742 1573 arg->stringified = stringify_arg (pfile, arg);
79bd622b 1574 }
1575 else if ((src->flags & PASTE_LEFT)
672f38da 1576 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
79bd622b 1577 total += arg->count - 1;
1578 else
1579 {
1580 if (!arg->expanded)
f9b5f742 1581 expand_arg (pfile, arg);
79bd622b 1582 total += arg->expanded_count - 1;
1583 }
1584 }
1585
ce70f433 1586 /* When the compiler is called with the -ftrack-macro-expansion
1587 flag, we need to keep track of the location of each token that
1588 results from macro expansion.
1589
1590 A token resulting from macro expansion is not a new token. It is
1591 simply the same token as the token coming from the macro
1592 definition. The new things that are allocated are the buffer
1593 that holds the tokens resulting from macro expansion and a new
1594 location that records many things like the locus of the expansion
1595 point as well as the original locus inside the definition of the
1596 macro. This location is called a virtual location.
1597
1598 So the buffer BUFF holds a set of cpp_token*, and the buffer
1599 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1600
1601 Both of these two buffers are going to be hung off of the macro
1602 context, when the latter is pushed. The memory allocated to
1603 store the tokens and their locations is going to be freed once
1604 the context of macro expansion is popped.
1605
1606 As far as tokens are concerned, the memory overhead of
1607 -ftrack-macro-expansion is proportional to the number of
1608 macros that get expanded multiplied by sizeof (source_location).
1609 The good news is that extra memory gets freed when the macro
1610 context is freed, i.e shortly after the macro got expanded. */
1611
1612 /* Is the -ftrack-macro-expansion flag in effect? */
1613 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1614
1615 /* Now allocate memory space for tokens and locations resulting from
1616 the macro expansion, copy the tokens and replace the arguments.
1617 This memory must be freed when the context of the macro MACRO is
1618 popped. */
1619 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1620
084163dc 1621 first = (const cpp_token **) buff->base;
79bd622b 1622
ce70f433 1623 /* Create a macro map to record the locations of the tokens that are
1624 involved in the expansion. Note that the expansion point is set
1625 to the location of the closing parenthesis. Otherwise, the
1626 subsequent map created for the first token that comes after the
1627 macro map might have a wrong line number. That would lead to
1628 tokens with wrong line numbers after the macro expansion. This
1629 adds up to the memory overhead of the -ftrack-macro-expansion
1630 flag; for every macro that is expanded, a "macro map" is
1631 created. */
1632 if (track_macro_exp)
1633 {
1634 int num_macro_tokens = total;
1635 if (track_macro_exp < 2)
1636 /* Then the number of macro tokens won't take in account the
1637 fact that function-like macro arguments can expand to
1638 multiple tokens. This is to save memory at the expense of
1639 accuracy.
1640
1641 Suppose we have #define SQARE(A) A * A
1642
1643 And then we do SQARE(2+3)
1644
1645 Then the tokens 2, +, 3, will have the same location,
1646 saying they come from the expansion of the argument A. */
1647 num_macro_tokens = exp_count;
1648 map = linemap_enter_macro (pfile->line_table, node,
1649 expansion_point_loc,
1650 num_macro_tokens);
1651 }
1652 i = 0;
672f38da 1653 for (src = macro->exp.tokens; src < limit; src++)
f9b5f742 1654 {
ce70f433 1655 unsigned int arg_tokens_count;
1656 macro_arg_token_iter from;
1657 const cpp_token **paste_flag = NULL;
1658 const cpp_token **tmp_token_ptr;
79bd622b 1659
f9b5f742 1660 if (src->type != CPP_MACRO_ARG)
1661 {
ce70f433 1662 /* Allocate a virtual location for token SRC, and add that
1663 token and its virtual location into the buffers BUFF and
1664 VIRT_LOCS. */
1665 unsigned index = expanded_token_index (pfile, macro, src, i);
1666 tokens_buff_add_token (buff, virt_locs, src,
1667 src->src_loc, src->src_loc,
1668 map, index);
1669 i += 1;
f9b5f742 1670 continue;
1671 }
79bd622b 1672
f9b5f742 1673 paste_flag = 0;
2ee04baa 1674 arg = &args[src->val.macro_arg.arg_no - 1];
ce70f433 1675 /* SRC is a macro parameter that we need to replace with its
1676 corresponding argument. So at some point we'll need to
1677 iterate over the tokens of the macro argument and copy them
1678 into the "place" now holding the correspondig macro
1679 parameter. We are going to use the iterator type
1680 macro_argo_token_iter to handle that iterating. The 'if'
1681 below is to initialize the iterator depending on the type of
1682 tokens the macro argument has. It also does some adjustment
1683 related to padding tokens and some pasting corner cases. */
f9b5f742 1684 if (src->flags & STRINGIFY_ARG)
ce70f433 1685 {
1686 arg_tokens_count = 1;
1687 macro_arg_token_iter_init (&from,
1688 CPP_OPTION (pfile,
1689 track_macro_expansion),
1690 MACRO_ARG_TOKEN_STRINGIFIED,
1691 arg, &arg->stringified);
1692 }
f9b5f742 1693 else if (src->flags & PASTE_LEFT)
ce70f433 1694 {
1695 arg_tokens_count = arg->count;
1696 macro_arg_token_iter_init (&from,
1697 CPP_OPTION (pfile,
1698 track_macro_expansion),
1699 MACRO_ARG_TOKEN_NORMAL,
1700 arg, arg->first);
1701 }
672f38da 1702 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
f9b5f742 1703 {
ce70f433 1704 int num_toks;
1705 arg_tokens_count = arg->count;
1706 macro_arg_token_iter_init (&from,
1707 CPP_OPTION (pfile,
1708 track_macro_expansion),
1709 MACRO_ARG_TOKEN_NORMAL,
1710 arg, arg->first);
1711
1712 num_toks = tokens_buff_count (buff);
1713
1714 if (num_toks != 0)
f9b5f742 1715 {
ce70f433 1716 /* So the current parameter token is pasted to the previous
1717 token in the replacement list. Let's look at what
1718 we have as previous and current arguments. */
1719
1720 /* This is the previous argument's token ... */
1721 tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1722
1723 if ((*tmp_token_ptr)->type == CPP_COMMA
f9b5f742 1724 && macro->variadic
2ee04baa 1725 && src->val.macro_arg.arg_no == macro->paramc)
f9b5f742 1726 {
ce70f433 1727 /* ... which is a comma; and the current parameter
1728 is the last parameter of a variadic function-like
1729 macro. If the argument to the current last
1730 parameter is NULL, then swallow the comma,
1731 otherwise drop the paste flag. */
1732 if (macro_arg_token_iter_get_token (&from) == NULL)
1733 tokens_buff_remove_last_token (buff);
f9b5f742 1734 else
ce70f433 1735 paste_flag = tmp_token_ptr;
f9b5f742 1736 }
1737 /* Remove the paste flag if the RHS is a placemarker. */
ce70f433 1738 else if (arg_tokens_count == 0)
1739 paste_flag = tmp_token_ptr;
f9b5f742 1740 }
1741 }
1742 else
ce70f433 1743 {
1744 arg_tokens_count = arg->expanded_count;
1745 macro_arg_token_iter_init (&from,
1746 CPP_OPTION (pfile,
1747 track_macro_expansion),
1748 MACRO_ARG_TOKEN_EXPANDED,
1749 arg, arg->expanded);
1750 }
3c7df4d3 1751
f9b5f742 1752 /* Padding on the left of an argument (unless RHS of ##). */
7825551f 1753 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
672f38da 1754 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
ce70f433 1755 {
1756 const cpp_token *t = padding_token (pfile, src);
1757 unsigned index = expanded_token_index (pfile, macro, src, i);
1758 /* Allocate a virtual location for the padding token and
1759 append the token and its location to BUFF and
1760 VIRT_LOCS. */
1761 tokens_buff_add_token (buff, virt_locs, t,
1762 t->src_loc, t->src_loc,
1763 map, index);
1764 }
79bd622b 1765
ce70f433 1766 if (arg_tokens_count)
f9b5f742 1767 {
ce70f433 1768 /* So now we've got the number of tokens that make up the
1769 argument that is going to replace the current parameter
1770 in the macro's replacement list. */
1771 unsigned int j;
1772 for (j = 0; j < arg_tokens_count; ++j)
1773 {
1774 /* So if track_macro_exp is < 2, the user wants to
1775 save extra memory while tracking macro expansion
1776 locations. So in that case here is what we do:
1777
1778 Suppose we have #define SQARE(A) A * A
1779
1780 And then we do SQARE(2+3)
1781
1782 Then the tokens 2, +, 3, will have the same location,
1783 saying they come from the expansion of the argument
1784 A.
1785
1786 So that means we are going to ignore the COUNT tokens
1787 resulting from the expansion of the current macro
1788 arugment. In other words all the ARG_TOKENS_COUNT tokens
1789 resulting from the expansion of the macro argument will
1790 have the index I. Normally, each of those token should
1791 have index I+J. */
1792 unsigned token_index = i;
1793 unsigned index;
1794 if (track_macro_exp > 1)
1795 token_index += j;
1796
1797 index = expanded_token_index (pfile, macro, src, token_index);
1798 tokens_buff_add_token (buff, virt_locs,
1799 macro_arg_token_iter_get_token (&from),
1800 macro_arg_token_iter_get_location (&from),
1801 src->src_loc, map, index);
1802 macro_arg_token_iter_forward (&from);
1803 }
79bd622b 1804
f9b5f742 1805 /* With a non-empty argument on the LHS of ##, the last
1806 token should be flagged PASTE_LEFT. */
1807 if (src->flags & PASTE_LEFT)
ce70f433 1808 paste_flag =
1809 (const cpp_token **) tokens_buff_last_token_ptr (buff);
f9b5f742 1810 }
04afd878 1811 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
1812 && ! macro->syshdr && ! cpp_in_system_header (pfile))
403c2b12 1813 {
49f161b6 1814 if (CPP_OPTION (pfile, cplusplus))
04afd878 1815 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1816 "invoking macro %s argument %d: "
1817 "empty macro arguments are undefined"
1818 " in ISO C++98",
1819 NODE_NAME (node), src->val.macro_arg.arg_no);
806fe15e 1820 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
04afd878 1821 cpp_pedwarning (pfile,
1822 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1823 ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
1824 "invoking macro %s argument %d: "
1825 "empty macro arguments are undefined"
1826 " in ISO C90",
1827 NODE_NAME (node), src->val.macro_arg.arg_no);
403c2b12 1828 }
806fe15e 1829 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
04afd878 1830 && ! CPP_OPTION (pfile, cplusplus)
1831 && ! macro->syshdr && ! cpp_in_system_header (pfile))
1832 cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
1833 "invoking macro %s argument %d: "
1834 "empty macro arguments are undefined"
1835 " in ISO C90",
1836 NODE_NAME (node), src->val.macro_arg.arg_no);
efdcc728 1837
f9b5f742 1838 /* Avoid paste on RHS (even case count == 0). */
1839 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
ce70f433 1840 {
1841 const cpp_token *t = &pfile->avoid_paste;
1842 tokens_buff_add_token (buff, virt_locs,
1843 t, t->src_loc, t->src_loc,
1844 NULL, 0);
1845 }
79bd622b 1846
f9b5f742 1847 /* Add a new paste flag, or remove an unwanted one. */
1848 if (paste_flag)
1849 {
1850 cpp_token *token = _cpp_temp_token (pfile);
1851 token->type = (*paste_flag)->type;
960391da 1852 token->val = (*paste_flag)->val;
f9b5f742 1853 if (src->flags & PASTE_LEFT)
1854 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1855 else
1856 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1857 *paste_flag = token;
1858 }
3c7df4d3 1859
ce70f433 1860 i += arg_tokens_count;
1861 }
f9b5f742 1862
ce70f433 1863 if (track_macro_exp)
1864 push_extended_tokens_context (pfile, node, buff, virt_locs, first,
1865 tokens_buff_count (buff));
1866 else
1867 push_ptoken_context (pfile, node, buff, first,
1868 tokens_buff_count (buff));
e77b8253 1869
1870 num_macro_tokens_counter += tokens_buff_count (buff);
f9b5f742 1871}
1872
1873/* Return a special padding token, with padding inherited from SOURCE. */
1874static const cpp_token *
f7fdd7a1 1875padding_token (cpp_reader *pfile, const cpp_token *source)
f9b5f742 1876{
1877 cpp_token *result = _cpp_temp_token (pfile);
1878
1879 result->type = CPP_PADDING;
bb30d1f4 1880
1881 /* Data in GCed data structures cannot be made const so far, so we
1882 need a cast here. */
1883 result->val.source = (cpp_token *) source;
f9b5f742 1884 result->flags = 0;
1885 return result;
1886}
1887
88cf66fa 1888/* Get a new uninitialized context. Create a new one if we cannot
1889 re-use an old one. */
f9b5f742 1890static cpp_context *
f7fdd7a1 1891next_context (cpp_reader *pfile)
f9b5f742 1892{
1893 cpp_context *result = pfile->context->next;
1894
1895 if (result == 0)
69461e0d 1896 {
3b298764 1897 result = XNEW (cpp_context);
ce70f433 1898 memset (result, 0, sizeof (cpp_context));
f9b5f742 1899 result->prev = pfile->context;
1900 result->next = 0;
1901 pfile->context->next = result;
79bd622b 1902 }
f9b5f742 1903
1904 pfile->context = result;
1905 return result;
79bd622b 1906}
1907
f9b5f742 1908/* Push a list of pointers to tokens. */
1909static void
f7fdd7a1 1910push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1911 const cpp_token **first, unsigned int count)
79bd622b 1912{
1913 cpp_context *context = next_context (pfile);
79bd622b 1914
ce70f433 1915 context->tokens_kind = TOKENS_KIND_INDIRECT;
1916 context->c.macro = macro;
084163dc 1917 context->buff = buff;
a854276a 1918 FIRST (context).ptoken = first;
1919 LAST (context).ptoken = first + count;
f9b5f742 1920}
1921
00abfdc0 1922/* Push a list of tokens.
1923
1924 A NULL macro means that we should continue the current macro
1925 expansion, in essence. That means that if we are currently in a
1926 macro expansion context, we'll make the new pfile->context refer to
1927 the current macro. */
b75b98aa 1928void
1929_cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1930 const cpp_token *first, unsigned int count)
f9b5f742 1931{
00abfdc0 1932 cpp_context *context;
1933
1934 if (macro == NULL)
1935 macro = macro_of_context (pfile->context);
1936
1937 context = next_context (pfile);
ce70f433 1938 context->tokens_kind = TOKENS_KIND_DIRECT;
1939 context->c.macro = macro;
1940 context->buff = NULL;
00abfdc0 1941 FIRST (context).token = first;
1942 LAST (context).token = first + count;
79bd622b 1943}
1944
ce70f433 1945/* Build a context containing a list of tokens as well as their
1946 virtual locations and push it. TOKENS_BUFF is the buffer that
1947 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1948 non-NULL, it means that the context owns it, meaning that
1949 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
00abfdc0 1950 contains the virtual locations.
1951
1952 A NULL macro means that we should continue the current macro
1953 expansion, in essence. That means that if we are currently in a
1954 macro expansion context, we'll make the new pfile->context refer to
1955 the current macro. */
ce70f433 1956static void
1957push_extended_tokens_context (cpp_reader *pfile,
1958 cpp_hashnode *macro,
1959 _cpp_buff *token_buff,
1960 source_location *virt_locs,
1961 const cpp_token **first,
1962 unsigned int count)
1963{
00abfdc0 1964 cpp_context *context;
ce70f433 1965 macro_context *m;
1966
00abfdc0 1967 if (macro == NULL)
1968 macro = macro_of_context (pfile->context);
1969
1970 context = next_context (pfile);
ce70f433 1971 context->tokens_kind = TOKENS_KIND_EXTENDED;
1972 context->buff = token_buff;
1973
1974 m = XNEW (macro_context);
1975 m->macro_node = macro;
1976 m->virt_locs = virt_locs;
1977 m->cur_virt_loc = virt_locs;
1978 context->c.mc = m;
1979 FIRST (context).ptoken = first;
1980 LAST (context).ptoken = first + count;
1981}
1982
f15f6c8d 1983/* Push a traditional macro's replacement text. */
1984void
f7fdd7a1 1985_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
1986 const uchar *start, size_t len)
f15f6c8d 1987{
1988 cpp_context *context = next_context (pfile);
1989
ce70f433 1990 context->tokens_kind = TOKENS_KIND_DIRECT;
1991 context->c.macro = macro;
f15f6c8d 1992 context->buff = NULL;
1993 CUR (context) = start;
06025647 1994 RLIMIT (context) = start + len;
01628c3c 1995 macro->flags |= NODE_DISABLED;
f15f6c8d 1996}
1997
ce70f433 1998/* Creates a buffer that holds tokens a.k.a "token buffer", usually
1999 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2000 non-null (which means that -ftrack-macro-expansion is on),
2001 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2002 hold the virtual locations of the tokens resulting from macro
2003 expansion. */
2004static _cpp_buff*
2005tokens_buff_new (cpp_reader *pfile, size_t len,
2006 source_location **virt_locs)
2007{
2008 size_t tokens_size = len * sizeof (cpp_token *);
2009 size_t locs_size = len * sizeof (source_location);
2010
2011 if (virt_locs != NULL)
2012 *virt_locs = XNEWVEC (source_location, locs_size);
2013 return _cpp_get_buff (pfile, tokens_size);
2014}
2015
2016/* Returns the number of tokens contained in a token buffer. The
2017 buffer holds a set of cpp_token*. */
2018static size_t
2019tokens_buff_count (_cpp_buff *buff)
2020{
2021 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
2022}
2023
2024/* Return a pointer to the last token contained in the token buffer
2025 BUFF. */
2026static const cpp_token **
2027tokens_buff_last_token_ptr (_cpp_buff *buff)
2028{
2029 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
2030}
2031
2032/* Remove the last token contained in the token buffer TOKENS_BUFF.
2033 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2034 containing the virtual locations of the tokens in TOKENS_BUFF; in
2035 which case the function updates that buffer as well. */
2036static inline void
2037tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
2038
2039{
2040 if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
2041 BUFF_FRONT (tokens_buff) =
2042 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
2043}
2044
2045/* Insert a token into the token buffer at the position pointed to by
2046 DEST. Note that the buffer is not enlarged so the previous token
2047 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2048 means -ftrack-macro-expansion is effect; it then points to where to
2049 insert the virtual location of TOKEN. TOKEN is the token to
2050 insert. VIRT_LOC is the virtual location of the token, i.e, the
e8aa7eaf 2051 location possibly encoding its locus across macro expansion. If
ce70f433 2052 TOKEN is an argument of a function-like macro (inside a macro
2053 replacement list), PARM_DEF_LOC is the spelling location of the
2054 macro parameter that TOKEN is replacing, in the replacement list of
2055 the macro. If TOKEN is not an argument of a function-like macro or
2056 if it doesn't come from a macro expansion, then VIRT_LOC can just
2057 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2058 means TOKEN comes from a macro expansion and MAP is the macro map
2059 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2060 the token in the macro map; it is not considered if MAP is NULL.
2061
2062 Upon successful completion this function returns the a pointer to
2063 the position of the token coming right after the insertion
2064 point. */
2065static inline const cpp_token **
2066tokens_buff_put_token_to (const cpp_token **dest,
2067 source_location *virt_loc_dest,
2068 const cpp_token *token,
2069 source_location virt_loc,
2070 source_location parm_def_loc,
551e34da 2071 const line_map_macro *map,
ce70f433 2072 unsigned int macro_token_index)
2073{
2074 source_location macro_loc = virt_loc;
2075 const cpp_token **result;
2076
2077 if (virt_loc_dest)
2078 {
2079 /* -ftrack-macro-expansion is on. */
2080 if (map)
2081 macro_loc = linemap_add_macro_token (map, macro_token_index,
2082 virt_loc, parm_def_loc);
2083 *virt_loc_dest = macro_loc;
2084 }
2085 *dest = token;
2086 result = &dest[1];
2087
2088 return result;
2089}
2090
2091/* Adds a token at the end of the tokens contained in BUFFER. Note
2092 that this function doesn't enlarge BUFFER when the number of tokens
2093 reaches BUFFER's size; it aborts in that situation.
2094
2095 TOKEN is the token to append. VIRT_LOC is the virtual location of
e8aa7eaf 2096 the token, i.e, the location possibly encoding its locus across
ce70f433 2097 macro expansion. If TOKEN is an argument of a function-like macro
2098 (inside a macro replacement list), PARM_DEF_LOC is the location of
2099 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2100 from a macro expansion, then VIRT_LOC can just be set to the same
2101 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2102 from a macro expansion and MAP is the macro map associated to the
2103 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2104 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2105 non-null, it means -ftrack-macro-expansion is on; in which case
2106 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2107 array, at the same index as the one of TOKEN in BUFFER. Upon
2108 successful completion this function returns the a pointer to the
2109 position of the token coming right after the insertion point. */
2110static const cpp_token **
2111tokens_buff_add_token (_cpp_buff *buffer,
2112 source_location *virt_locs,
2113 const cpp_token *token,
2114 source_location virt_loc,
2115 source_location parm_def_loc,
551e34da 2116 const line_map_macro *map,
ce70f433 2117 unsigned int macro_token_index)
2118{
2119 const cpp_token **result;
2120 source_location *virt_loc_dest = NULL;
2121 unsigned token_index =
2122 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2123
2124 /* Abort if we pass the end the buffer. */
2125 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2126 abort ();
2127
2128 if (virt_locs != NULL)
2129 virt_loc_dest = &virt_locs[token_index];
2130
2131 result =
2132 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2133 virt_loc_dest, token, virt_loc, parm_def_loc,
2134 map, macro_token_index);
2135
2136 BUFF_FRONT (buffer) = (unsigned char *) result;
2137 return result;
2138}
2139
2140/* Allocate space for the function-like macro argument ARG to store
2141 the tokens resulting from the macro-expansion of the tokens that
2142 make up ARG itself. That space is allocated in ARG->expanded and
2143 needs to be freed using free. */
2144static void
2145alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2146{
2147#ifdef ENABLE_CHECKING
2148 if (arg->expanded != NULL
2149 || arg->expanded_virt_locs != NULL)
2150 abort ();
2151#endif
2152 arg->expanded = XNEWVEC (const cpp_token *, capacity);
2153 if (CPP_OPTION (pfile, track_macro_expansion))
2154 arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2155
2156}
2157
2158/* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2159 tokens. */
2160static void
2161ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2162 size_t size, size_t *expanded_capacity)
2163{
2164 if (size <= *expanded_capacity)
2165 return;
2166
2167 size *= 2;
2168
2169 arg->expanded =
2170 XRESIZEVEC (const cpp_token *, arg->expanded, size);
2171 *expanded_capacity = size;
2172
2173 if (CPP_OPTION (pfile, track_macro_expansion))
2174 {
2175 if (arg->expanded_virt_locs == NULL)
2176 arg->expanded_virt_locs = XNEWVEC (source_location, size);
2177 else
2178 arg->expanded_virt_locs = XRESIZEVEC (source_location,
2179 arg->expanded_virt_locs,
2180 size);
2181 }
2182}
2183
88cf66fa 2184/* Expand an argument ARG before replacing parameters in a
2185 function-like macro. This works by pushing a context with the
2186 argument's tokens, and then expanding that into a temporary buffer
2187 as if it were a normal part of the token stream. collect_args()
2188 has terminated the argument's tokens with a CPP_EOF so that we know
2189 when we have fully expanded the argument. */
79bd622b 2190static void
f7fdd7a1 2191expand_arg (cpp_reader *pfile, macro_arg *arg)
79bd622b 2192{
ce70f433 2193 size_t capacity;
dbb7d6f7 2194 bool saved_warn_trad;
ce70f433 2195 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
f9b5f742 2196
ce70f433 2197 if (arg->count == 0
2198 || arg->expanded != NULL)
f9b5f742 2199 return;
79bd622b 2200
dbb7d6f7 2201 /* Don't warn about funlike macros when pre-expanding. */
2202 saved_warn_trad = CPP_WTRADITIONAL (pfile);
2203 CPP_WTRADITIONAL (pfile) = 0;
2204
ce70f433 2205 /* Loop, reading in the tokens of the argument. */
f9b5f742 2206 capacity = 256;
ce70f433 2207 alloc_expanded_arg_mem (pfile, arg, capacity);
2208
2209 if (track_macro_exp_p)
2210 push_extended_tokens_context (pfile, NULL, NULL,
2211 arg->virt_locs,
2212 arg->first,
2213 arg->count + 1);
2214 else
2215 push_ptoken_context (pfile, NULL, NULL,
2216 arg->first, arg->count + 1);
79bd622b 2217
f9b5f742 2218 for (;;)
79bd622b 2219 {
f9b5f742 2220 const cpp_token *token;
ce70f433 2221 source_location location;
f9b5f742 2222
ce70f433 2223 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2224 &capacity);
79bd622b 2225
ce70f433 2226 token = cpp_get_token_1 (pfile, &location);
79bd622b 2227
f9b5f742 2228 if (token->type == CPP_EOF)
2229 break;
2230
ce70f433 2231 set_arg_token (arg, token, location,
2232 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2233 CPP_OPTION (pfile, track_macro_expansion));
2234 arg->expanded_count++;
f9b5f742 2235 }
2236
084163dc 2237 _cpp_pop_context (pfile);
dbb7d6f7 2238
2239 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
79bd622b 2240}
2241
00abfdc0 2242/* Returns the macro associated to the current context if we are in
2243 the context a macro expansion, NULL otherwise. */
2244static cpp_hashnode*
2245macro_of_context (cpp_context *context)
2246{
2247 if (context == NULL)
2248 return NULL;
2249
2250 return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2251 ? context->c.mc->macro_node
2252 : context->c.macro;
2253}
2254
8c6425eb 2255/* Return TRUE iff we are expanding a macro or are about to start
2256 expanding one. If we are effectively expanding a macro, the
2257 function macro_of_context returns a pointer to the macro being
2258 expanded. */
2259static bool
2260in_macro_expansion_p (cpp_reader *pfile)
2261{
2262 if (pfile == NULL)
2263 return false;
2264
2265 return (pfile->about_to_expand_macro_p
2266 || macro_of_context (pfile->context));
2267}
2268
88cf66fa 2269/* Pop the current context off the stack, re-enabling the macro if the
ce70f433 2270 context represented a macro's replacement list. Initially the
2271 context structure was not freed so that we can re-use it later, but
2272 now we do free it to reduce peak memory consumption. */
79bd622b 2273void
f7fdd7a1 2274_cpp_pop_context (cpp_reader *pfile)
79bd622b 2275{
084163dc 2276 cpp_context *context = pfile->context;
2277
6cfa7465 2278 /* We should not be popping the base context. */
2279 if (context == &pfile->base_context)
2280 abort ();
2281
ce70f433 2282 if (context->c.macro)
2283 {
2284 cpp_hashnode *macro;
2285 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2286 {
2287 macro_context *mc = context->c.mc;
2288 macro = mc->macro_node;
2289 /* If context->buff is set, it means the life time of tokens
2290 is bound to the life time of this context; so we must
2291 free the tokens; that means we must free the virtual
2292 locations of these tokens too. */
2293 if (context->buff && mc->virt_locs)
2294 {
2295 free (mc->virt_locs);
2296 mc->virt_locs = NULL;
2297 }
2298 free (mc);
2299 context->c.mc = NULL;
2300 }
2301 else
2302 macro = context->c.macro;
2303
2304 /* Beware that MACRO can be NULL in cases like when we are
2305 called from expand_arg. In those cases, a dummy context with
2306 tokens is pushed just for the purpose of walking them using
2307 cpp_get_token_1. In that case, no 'macro' field is set into
2308 the dummy context. */
00abfdc0 2309 if (macro != NULL
2310 /* Several contiguous macro expansion contexts can be
2311 associated to the same macro; that means it's the same
e8aa7eaf 2312 macro expansion that spans across all these (sub)
00abfdc0 2313 contexts. So we should re-enable an expansion-disabled
2314 macro only when we are sure we are really out of that
2315 macro expansion. */
2316 && macro_of_context (context->prev) != macro)
ce70f433 2317 macro->flags &= ~NODE_DISABLED;
825751e2 2318
2319 if (macro == pfile->top_most_macro_node && context->prev == NULL)
2320 /* We are popping the context of the top-most macro node. */
2321 pfile->top_most_macro_node = NULL;
ce70f433 2322 }
084163dc 2323
2324 if (context->buff)
ce70f433 2325 {
2326 /* Decrease memory peak consumption by freeing the memory used
2327 by the context. */
2328 _cpp_free_buff (context->buff);
2329 }
79bd622b 2330
084163dc 2331 pfile->context = context->prev;
ce70f433 2332 /* decrease peak memory consumption by feeing the context. */
2333 pfile->context->next = NULL;
2334 free (context);
79bd622b 2335}
2336
ce70f433 2337/* Return TRUE if we reached the end of the set of tokens stored in
2338 CONTEXT, FALSE otherwise. */
2339static inline bool
2340reached_end_of_context (cpp_context *context)
2341{
2342 if (context->tokens_kind == TOKENS_KIND_DIRECT)
2343 return FIRST (context).token == LAST (context).token;
2344 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2345 || context->tokens_kind == TOKENS_KIND_EXTENDED)
2346 return FIRST (context).ptoken == LAST (context).ptoken;
2347 else
2348 abort ();
2349}
2350
2351/* Consume the next token contained in the current context of PFILE,
2352 and return it in *TOKEN. It's "full location" is returned in
2353 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
e8aa7eaf 2354 means the location encoding the locus of the token across macro
ce70f433 2355 expansion; otherwise it's just is the "normal" location of the
2356 token which (*TOKEN)->src_loc. */
2357static inline void
2358consume_next_token_from_context (cpp_reader *pfile,
2359 const cpp_token ** token,
2360 source_location *location)
2361{
2362 cpp_context *c = pfile->context;
2363
2364 if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2365 {
2366 *token = FIRST (c).token;
2367 *location = (*token)->src_loc;
2368 FIRST (c).token++;
2369 }
2370 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2371 {
2372 *token = *FIRST (c).ptoken;
2373 *location = (*token)->src_loc;
2374 FIRST (c).ptoken++;
2375 }
2376 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2377 {
2378 macro_context *m = c->c.mc;
2379 *token = *FIRST (c).ptoken;
2380 if (m->virt_locs)
2381 {
2382 *location = *m->cur_virt_loc;
2383 m->cur_virt_loc++;
2384 }
2385 else
2386 *location = (*token)->src_loc;
2387 FIRST (c).ptoken++;
2388 }
2389 else
2390 abort ();
2391}
2392
2393/* In the traditional mode of the preprocessor, if we are currently in
2394 a directive, the location of a token must be the location of the
2395 start of the directive line. This function returns the proper
2396 location if we are in the traditional mode, and just returns
2397 LOCATION otherwise. */
2398
2399static inline source_location
2400maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2401{
2402 if (CPP_OPTION (pfile, traditional))
2403 {
2404 if (pfile->state.in_directive)
2405 return pfile->directive_line;
2406 }
2407 return location;
2408}
2409
2410/* Routine to get a token as well as its location.
343fd982 2411
2412 Macro expansions and directives are transparently handled,
2413 including entering included files. Thus tokens are post-macro
2414 expansion, and after any intervening directives. External callers
2415 see CPP_EOF only at EOF. Internal callers also see it when meeting
2416 a directive inside a macro call, when at the end of a directive and
2417 state.in_directive is still 1, and at the end of argument
ce70f433 2418 pre-expansion.
2419
2420 LOC is an out parameter; *LOC is set to the location "as expected
2421 by the user". Please read the comment of
2422 cpp_get_token_with_location to learn more about the meaning of this
2423 location. */
2424static const cpp_token*
2425cpp_get_token_1 (cpp_reader *pfile, source_location *location)
79bd622b 2426{
f9b5f742 2427 const cpp_token *result;
ce70f433 2428 /* This token is a virtual token that either encodes a location
2429 related to macro expansion or a spelling location. */
2430 source_location virt_loc = 0;
8c6425eb 2431 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2432 to functions that push macro contexts. So let's save it so that
2433 we can restore it when we are about to leave this routine. */
2434 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
f9b5f742 2435
def71b06 2436 for (;;)
79bd622b 2437 {
f9b5f742 2438 cpp_hashnode *node;
79bd622b 2439 cpp_context *context = pfile->context;
2440
79bd622b 2441 /* Context->prev == 0 <=> base context. */
fb5ab82c 2442 if (!context->prev)
def71b06 2443 {
ce70f433 2444 result = _cpp_lex_token (pfile);
2445 virt_loc = result->src_loc;
2446 }
2447 else if (!reached_end_of_context (context))
2448 {
2449 consume_next_token_from_context (pfile, &result,
2450 &virt_loc);
f9b5f742 2451 if (result->flags & PASTE_LEFT)
e55de93e 2452 {
f9b5f742 2453 paste_all_tokens (pfile, result);
2454 if (pfile->state.in_directive)
2455 continue;
ce70f433 2456 result = padding_token (pfile, result);
2457 goto out;
e55de93e 2458 }
def71b06 2459 }
79bd622b 2460 else
2461 {
e77b8253 2462 if (pfile->context->c.macro)
2463 ++num_expanded_macros_counter;
fb5ab82c 2464 _cpp_pop_context (pfile);
f9b5f742 2465 if (pfile->state.in_directive)
2466 continue;
ce70f433 2467 result = &pfile->avoid_paste;
2468 goto out;
79bd622b 2469 }
79bd622b 2470
d3f7919d 2471 if (pfile->state.in_directive && result->type == CPP_COMMENT)
2472 continue;
2473
f9b5f742 2474 if (result->type != CPP_NAME)
79bd622b 2475 break;
2476
2ee04baa 2477 node = result->val.node.node;
f9b5f742 2478
c7e5d924 2479 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2480 break;
b1a9ff83 2481
c7e5d924 2482 if (!(node->flags & NODE_DISABLED))
79bd622b 2483 {
89768577 2484 int ret = 0;
931b0a0f 2485 /* If not in a macro context, and we're going to start an
825751e2 2486 expansion, record the location and the top level macro
2487 about to be expanded. */
8c6425eb 2488 if (!in_macro_expansion_p (pfile))
825751e2 2489 {
2490 pfile->invocation_location = result->src_loc;
2491 pfile->top_most_macro_node = node;
2492 }
45f9f140 2493 if (pfile->state.prevent_expansion)
2494 break;
89768577 2495
2496 /* Conditional macros require that a predicate be evaluated
2497 first. */
4514949a 2498 if ((node->flags & NODE_CONDITIONAL) != 0)
2499 {
2500 if (pfile->cb.macro_to_expand)
2501 {
2502 bool whitespace_after;
2503 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2504
2505 whitespace_after = (peek_tok->type == CPP_PADDING
2506 || (peek_tok->flags & PREV_WHITE));
2507 node = pfile->cb.macro_to_expand (pfile, result);
2508 if (node)
ce70f433 2509 ret = enter_macro_context (pfile, node, result,
2510 virt_loc);
4514949a 2511 else if (whitespace_after)
2512 {
2513 /* If macro_to_expand hook returned NULL and it
2514 ate some tokens, see if we don't need to add
2515 a padding token in between this and the
2516 next token. */
2517 peek_tok = cpp_peek_token (pfile, 0);
2518 if (peek_tok->type != CPP_PADDING
2519 && (peek_tok->flags & PREV_WHITE) == 0)
2520 _cpp_push_token_context (pfile, NULL,
2521 padding_token (pfile,
2522 peek_tok), 1);
2523 }
2524 }
2525 }
2526 else
ce70f433 2527 ret = enter_macro_context (pfile, node, result,
2528 virt_loc);
4514949a 2529 if (ret)
89768577 2530 {
45f9f140 2531 if (pfile->state.in_directive || ret == 2)
f9b5f742 2532 continue;
ce70f433 2533 result = padding_token (pfile, result);
2534 goto out;
8c2e2fc5 2535 }
79bd622b 2536 }
c7e5d924 2537 else
2538 {
88cf66fa 2539 /* Flag this token as always unexpandable. FIXME: move this
2540 to collect_args()?. */
c7e5d924 2541 cpp_token *t = _cpp_temp_token (pfile);
2542 t->type = result->type;
2543 t->flags = result->flags | NO_EXPAND;
960391da 2544 t->val = result->val;
c7e5d924 2545 result = t;
2546 }
396ffa86 2547
c7e5d924 2548 break;
79bd622b 2549 }
f9b5f742 2550
ce70f433 2551 out:
2552 if (location != NULL)
2553 {
2554 if (virt_loc == 0)
2555 virt_loc = result->src_loc;
2556 *location = virt_loc;
2557
2558 if (!CPP_OPTION (pfile, track_macro_expansion)
8c6425eb 2559 && macro_of_context (pfile->context) != NULL)
ce70f433 2560 /* We are in a macro expansion context, are not tracking
2561 virtual location, but were asked to report the location
2562 of the expansion point of the macro being expanded. */
2563 *location = pfile->invocation_location;
2564
2565 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2566 }
8c6425eb 2567
2568 pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
f9b5f742 2569 return result;
79bd622b 2570}
2571
ce70f433 2572/* External routine to get a token. Also used nearly everywhere
2573 internally, except for places where we know we can safely call
2574 _cpp_lex_token directly, such as lexing a directive name.
2575
2576 Macro expansions and directives are transparently handled,
2577 including entering included files. Thus tokens are post-macro
2578 expansion, and after any intervening directives. External callers
2579 see CPP_EOF only at EOF. Internal callers also see it when meeting
2580 a directive inside a macro call, when at the end of a directive and
2581 state.in_directive is still 1, and at the end of argument
2582 pre-expansion. */
2583const cpp_token *
2584cpp_get_token (cpp_reader *pfile)
2585{
2586 return cpp_get_token_1 (pfile, NULL);
2587}
2588
2589/* Like cpp_get_token, but also returns a virtual token location
2590 separate from the spelling location carried by the returned token.
2591
2592 LOC is an out parameter; *LOC is set to the location "as expected
2593 by the user". This matters when a token results from macro
2594 expansion; in that case the token's spelling location indicates the
2595 locus of the token in the definition of the macro but *LOC
2596 virtually encodes all the other meaningful locuses associated to
2597 the token.
2598
2599 What? virtual location? Yes, virtual location.
2600
2601 If the token results from macro expansion and if macro expansion
2602 location tracking is enabled its virtual location encodes (at the
2603 same time):
2604
2605 - the spelling location of the token
2606
2607 - the locus of the macro expansion point
2608
2609 - the locus of the point where the token got instantiated as part
2610 of the macro expansion process.
2611
2612 You have to use the linemap API to get the locus you are interested
2613 in from a given virtual location.
2614
2615 Note however that virtual locations are not necessarily ordered for
2616 relations '<' and '>'. One must use the function
2617 linemap_location_before_p instead of using the relational operator
2618 '<'.
2619
2620 If macro expansion tracking is off and if the token results from
2621 macro expansion the virtual location is the expansion point of the
2622 macro that got expanded.
2623
2624 When the token doesn't result from macro expansion, the virtual
2625 location is just the same thing as its spelling location. */
2626
931b0a0f 2627const cpp_token *
2628cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2629{
8c6425eb 2630 return cpp_get_token_1 (pfile, loc);
931b0a0f 2631}
2632
ae2348f6 2633/* Returns true if we're expanding an object-like macro that was
2634 defined in a system header. Just checks the macro at the top of
2635 the stack. Used for diagnostic suppression. */
2636int
f7fdd7a1 2637cpp_sys_macro_p (cpp_reader *pfile)
ae2348f6 2638{
d16f4f47 2639 cpp_hashnode *node = NULL;
2640
2641 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2642 node = pfile->context->c.mc->macro_node;
2643 else
2644 node = pfile->context->c.macro;
ae2348f6 2645
c7e5d924 2646 return node && node->value.macro && node->value.macro->syshdr;
ae2348f6 2647}
2648
02516fb9 2649/* Read each token in, until end of the current file. Directives are
2650 transparently processed. */
79bd622b 2651void
f7fdd7a1 2652cpp_scan_nooutput (cpp_reader *pfile)
79bd622b 2653{
6e04daf1 2654 /* Request a CPP_EOF token at the end of this file, rather than
2655 transparently continuing with the including file. */
2656 pfile->buffer->return_at_eof = true;
2657
3eb3f293 2658 pfile->state.discarding_output++;
2659 pfile->state.prevent_expansion++;
2660
7951771a 2661 if (CPP_OPTION (pfile, traditional))
2662 while (_cpp_read_logical_line_trad (pfile))
2663 ;
2664 else
2665 while (cpp_get_token (pfile)->type != CPP_EOF)
2666 ;
3eb3f293 2667
2668 pfile->state.discarding_output--;
2669 pfile->state.prevent_expansion--;
79bd622b 2670}
2671
89768577 2672/* Step back one or more tokens obtained from the lexer. */
2673void
2674_cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2675{
2676 pfile->lookaheads += count;
2677 while (count--)
2678 {
2679 pfile->cur_token--;
2680 if (pfile->cur_token == pfile->cur_run->base
2681 /* Possible with -fpreprocessed and no leading #line. */
2682 && pfile->cur_run->prev != NULL)
2683 {
2684 pfile->cur_run = pfile->cur_run->prev;
2685 pfile->cur_token = pfile->cur_run->limit;
2686 }
2687 }
2688}
2689
add258d7 2690/* Step back one (or more) tokens. Can only step back more than 1 if
fb5ab82c 2691 they are from the lexer, and not from macro expansion. */
920b5d41 2692void
f7fdd7a1 2693_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
79bd622b 2694{
fb5ab82c 2695 if (pfile->context->prev == NULL)
89768577 2696 _cpp_backup_tokens_direct (pfile, count);
fb5ab82c 2697 else
79bd622b 2698 {
fb5ab82c 2699 if (count != 1)
2700 abort ();
ce70f433 2701 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
a854276a 2702 FIRST (pfile->context).token--;
ce70f433 2703 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
a854276a 2704 FIRST (pfile->context).ptoken--;
ce70f433 2705 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2706 {
2707 FIRST (pfile->context).ptoken--;
2708 if (pfile->context->c.macro)
2709 {
2710 macro_context *m = pfile->context->c.mc;
2711 m->cur_virt_loc--;
2712#ifdef ENABLE_CHECKING
2713 if (m->cur_virt_loc < m->virt_locs)
2714 abort ();
2715#endif
2716 }
2717 else
2718 abort ();
2719 }
2720 else
2721 abort ();
79bd622b 2722 }
2723}
69461e0d 2724
79bd622b 2725/* #define directive parsing and handling. */
2726
d10cfa8d 2727/* Returns nonzero if a macro redefinition warning is required. */
f15f6c8d 2728static bool
25692381 2729warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
f7fdd7a1 2730 const cpp_macro *macro2)
79bd622b 2731{
2732 const cpp_macro *macro1;
2733 unsigned int i;
2734
31ca26b1 2735 /* Some redefinitions need to be warned about regardless. */
2736 if (node->flags & NODE_WARN)
f15f6c8d 2737 return true;
79bd622b 2738
a7d2e480 2739 /* Suppress warnings for builtins that lack the NODE_WARN flag,
2740 unless Wbuiltin-macro-redefined. */
2741 if (node->flags & NODE_BUILTIN
2742 && (!pfile->cb.user_builtin_macro
2743 || !pfile->cb.user_builtin_macro (pfile, node)))
2744 return CPP_OPTION (pfile, warn_builtin_macro_redefined);
2ad0b097 2745
89768577 2746 /* Redefinitions of conditional (context-sensitive) macros, on
2747 the other hand, must be allowed silently. */
2748 if (node->flags & NODE_CONDITIONAL)
2749 return false;
2750
31ca26b1 2751 /* Redefinition of a macro is allowed if and only if the old and new
2c0e001b 2752 definitions are the same. (6.10.3 paragraph 2). */
79bd622b 2753 macro1 = node->value.macro;
2754
3396dc80 2755 /* Don't check count here as it can be different in valid
2756 traditional redefinitions with just whitespace differences. */
2757 if (macro1->paramc != macro2->paramc
79bd622b 2758 || macro1->fun_like != macro2->fun_like
53c052ca 2759 || macro1->variadic != macro2->variadic)
f15f6c8d 2760 return true;
79bd622b 2761
2762 /* Check parameter spellings. */
2763 for (i = 0; i < macro1->paramc; i++)
2764 if (macro1->params[i] != macro2->params[i])
f15f6c8d 2765 return true;
79bd622b 2766
f15f6c8d 2767 /* Check the replacement text or tokens. */
2768 if (CPP_OPTION (pfile, traditional))
3396dc80 2769 return _cpp_expansions_different_trad (macro1, macro2);
f15f6c8d 2770
9c7ff5cc 2771 if (macro1->count != macro2->count)
2772 return true;
2773
2774 for (i = 0; i < macro1->count; i++)
2775 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2776 return true;
f15f6c8d 2777
2778 return false;
79bd622b 2779}
2780
2781/* Free the definition of hashnode H. */
69461e0d 2782void
f7fdd7a1 2783_cpp_free_definition (cpp_hashnode *h)
69461e0d 2784{
79bd622b 2785 /* Macros and assertions no longer have anything to free. */
2786 h->type = NT_VOID;
2787 /* Clear builtin flag in case of redefinition. */
34c3de48 2788 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
79bd622b 2789}
2790
109ca87a 2791/* Save parameter NODE (spelling SPELLING) to the parameter list of
2792 macro MACRO. Returns zero on success, nonzero if the parameter is
2793 a duplicate. */
198b48a0 2794bool
109ca87a 2795_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node,
2796 cpp_hashnode *spelling)
79bd622b 2797{
805e22b2 2798 unsigned int len;
79bd622b 2799 /* Constraint 6.10.3.6 - duplicate parameter names. */
805e22b2 2800 if (node->flags & NODE_MACRO_ARG)
11d10d3f 2801 {
d80d2074 2802 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
73328dce 2803 NODE_NAME (node));
1ae6ed16 2804 return true;
79bd622b 2805 }
11d10d3f 2806
e6a5f963 2807 if (BUFF_ROOM (pfile->a_buff)
2808 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2809 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
11d10d3f 2810
109ca87a 2811 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
805e22b2 2812 node->flags |= NODE_MACRO_ARG;
109ca87a 2813 len = macro->paramc * sizeof (struct macro_arg_saved_data);
805e22b2 2814 if (len > pfile->macro_buffer_len)
2815 {
720aca92 2816 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
2817 len);
805e22b2 2818 pfile->macro_buffer_len = len;
2819 }
109ca87a 2820 struct macro_arg_saved_data save;
2821 save.value = node->value;
2822 save.canonical_node = node;
2823 ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
2824 = save;
805e22b2 2825
2826 node->value.arg_index = macro->paramc;
1ae6ed16 2827 return false;
69461e0d 2828}
2829
1ae6ed16 2830/* Check the syntax of the parameters in a MACRO definition. Returns
2831 false if an error occurs. */
2832static bool
f7fdd7a1 2833parse_params (cpp_reader *pfile, cpp_macro *macro)
69461e0d 2834{
79bd622b 2835 unsigned int prev_ident = 0;
69461e0d 2836
79bd622b 2837 for (;;)
69461e0d 2838 {
c00e481c 2839 const cpp_token *token = _cpp_lex_token (pfile);
69461e0d 2840
c00e481c 2841 switch (token->type)
69461e0d 2842 {
79bd622b 2843 default:
d3f7919d 2844 /* Allow/ignore comments in parameter lists if we are
2845 preserving comments in macro expansions. */
2846 if (token->type == CPP_COMMENT
2847 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
2848 continue;
2849
d80d2074 2850 cpp_error (pfile, CPP_DL_ERROR,
73328dce 2851 "\"%s\" may not appear in macro parameter list",
c00e481c 2852 cpp_token_as_text (pfile, token));
1ae6ed16 2853 return false;
79bd622b 2854
69461e0d 2855 case CPP_NAME:
79bd622b 2856 if (prev_ident)
2857 {
d80d2074 2858 cpp_error (pfile, CPP_DL_ERROR,
73328dce 2859 "macro parameters must be comma-separated");
1ae6ed16 2860 return false;
79bd622b 2861 }
2862 prev_ident = 1;
2863
109ca87a 2864 if (_cpp_save_parameter (pfile, macro, token->val.node.node,
2865 token->val.node.spelling))
1ae6ed16 2866 return false;
79bd622b 2867 continue;
69461e0d 2868
79bd622b 2869 case CPP_CLOSE_PAREN:
2870 if (prev_ident || macro->paramc == 0)
1ae6ed16 2871 return true;
69461e0d 2872
79bd622b 2873 /* Fall through to pick up the error. */
2874 case CPP_COMMA:
2875 if (!prev_ident)
2876 {
d80d2074 2877 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
1ae6ed16 2878 return false;
79bd622b 2879 }
2880 prev_ident = 0;
69461e0d 2881 continue;
2882
79bd622b 2883 case CPP_ELLIPSIS:
53c052ca 2884 macro->variadic = 1;
79bd622b 2885 if (!prev_ident)
2886 {
198b48a0 2887 _cpp_save_parameter (pfile, macro,
109ca87a 2888 pfile->spec_nodes.n__VA_ARGS__,
198b48a0 2889 pfile->spec_nodes.n__VA_ARGS__);
79bd622b 2890 pfile->state.va_args_ok = 1;
05d51900 2891 if (! CPP_OPTION (pfile, c99)
5ae82d58 2892 && CPP_OPTION (pfile, cpp_pedantic)
05d51900 2893 && CPP_OPTION (pfile, warn_variadic_macros))
49f161b6 2894 {
2895 if (CPP_OPTION (pfile, cplusplus))
2896 cpp_pedwarning
2897 (pfile, CPP_W_VARIADIC_MACROS,
2898 "anonymous variadic macros were introduced in C++11");
2899 else
2900 cpp_pedwarning
2901 (pfile, CPP_W_VARIADIC_MACROS,
2902 "anonymous variadic macros were introduced in C99");
2903 }
806fe15e 2904 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
890c2e2f 2905 && ! CPP_OPTION (pfile, cplusplus))
2906 cpp_error (pfile, CPP_DL_WARNING,
2907 "anonymous variadic macros were introduced in C99");
79bd622b 2908 }
5ae82d58 2909 else if (CPP_OPTION (pfile, cpp_pedantic)
05d51900 2910 && CPP_OPTION (pfile, warn_variadic_macros))
49f161b6 2911 {
2912 if (CPP_OPTION (pfile, cplusplus))
2913 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2914 "ISO C++ does not permit named variadic macros");
2915 else
2916 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
3a79f5da 2917 "ISO C does not permit named variadic macros");
49f161b6 2918 }
69461e0d 2919
79bd622b 2920 /* We're at the end, and just expect a closing parenthesis. */
c00e481c 2921 token = _cpp_lex_token (pfile);
2922 if (token->type == CPP_CLOSE_PAREN)
1ae6ed16 2923 return true;
79bd622b 2924 /* Fall through. */
69461e0d 2925
79bd622b 2926 case CPP_EOF:
d80d2074 2927 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
1ae6ed16 2928 return false;
69461e0d 2929 }
69461e0d 2930 }
79bd622b 2931}
2932
10b4496a 2933/* Allocate room for a token from a macro's replacement list. */
79bd622b 2934static cpp_token *
f7fdd7a1 2935alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
79bd622b 2936{
e6a5f963 2937 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2938 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
69461e0d 2939
e6a5f963 2940 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
10b4496a 2941}
2942
88cf66fa 2943/* Lex a token from the expansion of MACRO, but mark parameters as we
2944 find them and warn of traditional stringification. */
10b4496a 2945static cpp_token *
f7fdd7a1 2946lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
10b4496a 2947{
e0ff7935 2948 cpp_token *token, *saved_cur_token;
10b4496a 2949
e0ff7935 2950 saved_cur_token = pfile->cur_token;
10b4496a 2951 pfile->cur_token = alloc_expansion_token (pfile, macro);
2952 token = _cpp_lex_direct (pfile);
e0ff7935 2953 pfile->cur_token = saved_cur_token;
79bd622b 2954
88cf66fa 2955 /* Is this a parameter? */
805e22b2 2956 if (token->type == CPP_NAME
2ee04baa 2957 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
79bd622b 2958 {
109ca87a 2959 cpp_hashnode *spelling = token->val.node.spelling;
79bd622b 2960 token->type = CPP_MACRO_ARG;
2ee04baa 2961 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
109ca87a 2962 token->val.macro_arg.spelling = spelling;
79bd622b 2963 }
2964 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2965 && (token->type == CPP_STRING || token->type == CPP_CHAR))
2966 check_trad_stringification (pfile, macro, &token->val.str);
2967
2968 return token;
69461e0d 2969}
2970
f15f6c8d 2971static bool
f7fdd7a1 2972create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
69461e0d 2973{
f15f6c8d 2974 cpp_token *token;
10b4496a 2975 const cpp_token *ctoken;
3b6c638e 2976 bool following_paste_op = false;
2977 const char *paste_op_error_msg =
2978 N_("'##' cannot appear at either end of a macro expansion");
941f2388 2979 unsigned int num_extra_tokens = 0;
79bd622b 2980
2981 /* Get the first token of the expansion (or the '(' of a
2982 function-like macro). */
10b4496a 2983 ctoken = _cpp_lex_token (pfile);
2984
2985 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
79bd622b 2986 {
f15f6c8d 2987 bool ok = parse_params (pfile, macro);
e6a5f963 2988 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
2989 if (!ok)
f15f6c8d 2990 return false;
e6a5f963 2991
c39ed964 2992 /* Success. Commit or allocate the parameter array. */
2993 if (pfile->hash_table->alloc_subobject)
2994 {
720aca92 2995 cpp_hashnode **params =
2996 (cpp_hashnode **) pfile->hash_table->alloc_subobject
2997 (sizeof (cpp_hashnode *) * macro->paramc);
bb30d1f4 2998 memcpy (params, macro->params,
2999 sizeof (cpp_hashnode *) * macro->paramc);
3000 macro->params = params;
c39ed964 3001 }
3002 else
3003 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
79bd622b 3004 macro->fun_like = 1;
79bd622b 3005 }
10b4496a 3006 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
99617355 3007 {
3008 /* While ISO C99 requires whitespace before replacement text
49f161b6 3009 in a macro definition, ISO C90 with TC1 allows characters
3010 from the basic source character set there. */
99617355 3011 if (CPP_OPTION (pfile, c99))
49f161b6 3012 {
3013 if (CPP_OPTION (pfile, cplusplus))
3014 cpp_error (pfile, CPP_DL_PEDWARN,
3015 "ISO C++11 requires whitespace after the macro name");
3016 else
3017 cpp_error (pfile, CPP_DL_PEDWARN,
3018 "ISO C99 requires whitespace after the macro name");
3019 }
99617355 3020 else
3021 {
3022 int warntype = CPP_DL_WARNING;
3023 switch (ctoken->type)
3024 {
3025 case CPP_ATSIGN:
3026 case CPP_AT_NAME:
3027 case CPP_OBJC_STRING:
3028 /* '@' is not in basic character set. */
3029 warntype = CPP_DL_PEDWARN;
3030 break;
3031 case CPP_OTHER:
3032 /* Basic character set sans letters, digits and _. */
3033 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3034 ctoken->val.str.text[0]) == NULL)
3035 warntype = CPP_DL_PEDWARN;
3036 break;
3037 default:
3038 /* All other tokens start with a character from basic
3039 character set. */
3040 break;
3041 }
3042 cpp_error (pfile, warntype,
3043 "missing whitespace after the macro name");
3044 }
3045 }
69461e0d 3046
10b4496a 3047 if (macro->fun_like)
3048 token = lex_expansion_token (pfile, macro);
3049 else
3050 {
3051 token = alloc_expansion_token (pfile, macro);
3052 *token = *ctoken;
3053 }
69461e0d 3054
79bd622b 3055 for (;;)
3056 {
3057 /* Check the stringifying # constraint 6.10.3.2.1 of
3058 function-like macros when lexing the subsequent token. */
3059 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
3060 {
3061 if (token->type == CPP_MACRO_ARG)
3062 {
941f2388 3063 if (token->flags & PREV_WHITE)
3064 token->flags |= SP_PREV_WHITE;
3065 if (token[-1].flags & DIGRAPH)
3066 token->flags |= SP_DIGRAPH;
79bd622b 3067 token->flags &= ~PREV_WHITE;
3068 token->flags |= STRINGIFY_ARG;
3069 token->flags |= token[-1].flags & PREV_WHITE;
3070 token[-1] = token[0];
3071 macro->count--;
3072 }
3073 /* Let assembler get away with murder. */
5db5d057 3074 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
79bd622b 3075 {
d80d2074 3076 cpp_error (pfile, CPP_DL_ERROR,
73328dce 3077 "'#' is not followed by a macro parameter");
f15f6c8d 3078 return false;
79bd622b 3079 }
3080 }
3081
3082 if (token->type == CPP_EOF)
3b6c638e 3083 {
3084 /* Paste operator constraint 6.10.3.3.1:
3085 Token-paste ##, can appear in both object-like and
3086 function-like macros, but not at the end. */
3087 if (following_paste_op)
3088 {
3089 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3090 return false;
3091 }
3092 break;
3093 }
79bd622b 3094
3095 /* Paste operator constraint 6.10.3.3.1. */
3096 if (token->type == CPP_PASTE)
3097 {
3098 /* Token-paste ##, can appear in both object-like and
3b6c638e 3099 function-like macros, but not at the beginning. */
3100 if (macro->count == 1)
79bd622b 3101 {
3b6c638e 3102 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
f15f6c8d 3103 return false;
79bd622b 3104 }
69461e0d 3105
941f2388 3106 if (token[-1].flags & PASTE_LEFT)
3107 {
3108 macro->extra_tokens = 1;
3109 num_extra_tokens++;
2ee04baa 3110 token->val.token_no = macro->count - 1;
941f2388 3111 }
3112 else
3113 {
3114 --macro->count;
3115 token[-1].flags |= PASTE_LEFT;
3116 if (token->flags & DIGRAPH)
3117 token[-1].flags |= SP_DIGRAPH;
3118 if (token->flags & PREV_WHITE)
3119 token[-1].flags |= SP_PREV_WHITE;
3120 }
79bd622b 3121 }
3122
3b6c638e 3123 following_paste_op = (token->type == CPP_PASTE);
79bd622b 3124 token = lex_expansion_token (pfile, macro);
3125 }
3126
672f38da 3127 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
c39ed964 3128 macro->traditional = 0;
e6a5f963 3129
3c7df4d3 3130 /* Don't count the CPP_EOF. */
3131 macro->count--;
79bd622b 3132
88cf66fa 3133 /* Clear whitespace on first token for warn_of_redefinition(). */
e6a5f963 3134 if (macro->count)
672f38da 3135 macro->exp.tokens[0].flags &= ~PREV_WHITE;
e6a5f963 3136
c39ed964 3137 /* Commit or allocate the memory. */
3138 if (pfile->hash_table->alloc_subobject)
3139 {
720aca92 3140 cpp_token *tokns =
3141 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
3142 * macro->count);
941f2388 3143 if (num_extra_tokens)
3144 {
3145 /* Place second and subsequent ## or %:%: tokens in
3146 sequences of consecutive such tokens at the end of the
3147 list to preserve information about where they appear, how
3148 they are spelt and whether they are preceded by
3149 whitespace without otherwise interfering with macro
3150 expansion. */
3151 cpp_token *normal_dest = tokns;
3152 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
3153 unsigned int i;
3154 for (i = 0; i < macro->count; i++)
3155 {
3156 if (macro->exp.tokens[i].type == CPP_PASTE)
3157 *extra_dest++ = macro->exp.tokens[i];
3158 else
3159 *normal_dest++ = macro->exp.tokens[i];
3160 }
3161 }
3162 else
3163 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
c39ed964 3164 macro->exp.tokens = tokns;
3165 }
3166 else
3167 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
e6a5f963 3168
f15f6c8d 3169 return true;
3170}
3171
d10cfa8d 3172/* Parse a macro and save its expansion. Returns nonzero on success. */
f15f6c8d 3173bool
f7fdd7a1 3174_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
f15f6c8d 3175{
3176 cpp_macro *macro;
3177 unsigned int i;
3178 bool ok;
4dab9f9f 3179
c39ed964 3180 if (pfile->hash_table->alloc_subobject)
720aca92 3181 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
3182 (sizeof (cpp_macro));
c39ed964 3183 else
3184 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
f15f6c8d 3185 macro->line = pfile->directive_line;
3186 macro->params = 0;
3187 macro->paramc = 0;
3188 macro->variadic = 0;
b717e161 3189 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
f15f6c8d 3190 macro->count = 0;
3191 macro->fun_like = 0;
941f2388 3192 macro->extra_tokens = 0;
ae2348f6 3193 /* To suppress some diagnostics. */
610625e3 3194 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
ae2348f6 3195
f15f6c8d 3196 if (CPP_OPTION (pfile, traditional))
3197 ok = _cpp_create_trad_definition (pfile, macro);
3198 else
3199 {
f15f6c8d 3200 ok = create_iso_definition (pfile, macro);
3201
e0ff7935 3202 /* We set the type for SEEN_EOL() in directives.c.
f15f6c8d 3203
3204 Longer term we should lex the whole line before coming here,
3205 and just copy the expansion. */
f15f6c8d 3206
3207 /* Stop the lexer accepting __VA_ARGS__. */
3208 pfile->state.va_args_ok = 0;
3209 }
3210
3211 /* Clear the fast argument lookup indices. */
3212 for (i = macro->paramc; i-- > 0; )
805e22b2 3213 {
109ca87a 3214 struct macro_arg_saved_data *save =
3215 &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
3216 struct cpp_hashnode *node = save->canonical_node;
805e22b2 3217 node->flags &= ~ NODE_MACRO_ARG;
109ca87a 3218 node->value = save->value;
805e22b2 3219 }
f15f6c8d 3220
3221 if (!ok)
3222 return ok;
3223
70145d81 3224 if (node->type == NT_MACRO)
69461e0d 3225 {
71a7c282 3226 if (CPP_OPTION (pfile, warn_unused_macros))
3227 _cpp_warn_if_unused_macro (pfile, node, NULL);
3228
f15f6c8d 3229 if (warn_of_redefinition (pfile, node, macro))
69461e0d 3230 {
a7d2e480 3231 const int reason = ((node->flags & NODE_BUILTIN)
3232 && !(node->flags & NODE_WARN))
3a79f5da 3233 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3a79f5da 3234
a7d2e480 3235 bool warned =
3236 cpp_pedwarning_with_line (pfile, reason,
3237 pfile->directive_line, 0,
3238 "\"%s\" redefined", NODE_NAME (node));
79bd622b 3239
7f5f3953 3240 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3241 cpp_error_with_line (pfile, CPP_DL_NOTE,
f15f6c8d 3242 node->value.macro->line, 0,
3243 "this is the location of the previous definition");
69461e0d 3244 }
69461e0d 3245 }
3246
70145d81 3247 if (node->type != NT_VOID)
3248 _cpp_free_definition (node);
3249
69461e0d 3250 /* Enter definition in hash table. */
79bd622b 3251 node->type = NT_MACRO;
3252 node->value.macro = macro;
5eb3761c 3253 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
a56d0856 3254 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3255 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3256 in the C standard, as something that one must use in C++.
49f161b6 3257 However DR#593 and C++11 indicate that they play no role in C++.
3258 We special-case them anyway. */
a56d0856 3259 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3260 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
31ca26b1 3261 node->flags |= NODE_WARN;
69461e0d 3262
89768577 3263 /* If user defines one of the conditional macros, remove the
3264 conditional flag */
3265 node->flags &= ~NODE_CONDITIONAL;
3266
79bd622b 3267 return ok;
69461e0d 3268}
3269
88cf66fa 3270/* Warn if a token in STRING matches one of a function-like MACRO's
3271 parameters. */
bceb9193 3272static void
f7fdd7a1 3273check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3274 const cpp_string *string)
bceb9193 3275{
79bd622b 3276 unsigned int i, len;
4970d4c2 3277 const uchar *p, *q, *limit;
b1a9ff83 3278
bceb9193 3279 /* Loop over the string. */
4970d4c2 3280 limit = string->text + string->len - 1;
3281 for (p = string->text + 1; p < limit; p = q)
bceb9193 3282 {
bceb9193 3283 /* Find the start of an identifier. */
7e118154 3284 while (p < limit && !is_idstart (*p))
3285 p++;
bceb9193 3286
3287 /* Find the end of the identifier. */
3288 q = p;
7e118154 3289 while (q < limit && is_idchar (*q))
3290 q++;
79bd622b 3291
3292 len = q - p;
3293
bceb9193 3294 /* Loop over the function macro arguments to see if the
3295 identifier inside the string matches one of them. */
79bd622b 3296 for (i = 0; i < macro->paramc; i++)
3297 {
3298 const cpp_hashnode *node = macro->params[i];
bceb9193 3299
0d086e18 3300 if (NODE_LEN (node) == len
3301 && !memcmp (p, NODE_NAME (node), len))
bceb9193 3302 {
d80d2074 3303 cpp_error (pfile, CPP_DL_WARNING,
455730ef 3304 "macro argument \"%s\" would be stringified in traditional C",
73328dce 3305 NODE_NAME (node));
bceb9193 3306 break;
3307 }
3308 }
3309 }
3310}
79bd622b 3311
4d9e9a28 3312/* Returns the name, arguments and expansion of a macro, in a format
3313 suitable to be read back in again, and therefore also for DWARF 2
3314 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3315 Caller is expected to generate the "#define" bit if needed. The
79bd622b 3316 returned text is temporary, and automatically freed later. */
79bd622b 3317const unsigned char *
25692381 3318cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
79bd622b 3319{
3320 unsigned int i, len;
25692381 3321 const cpp_macro *macro;
79bd622b 3322 unsigned char *buffer;
3323
3324 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3325 {
25692381 3326 if (node->type != NT_MACRO
3327 || !pfile->cb.user_builtin_macro
3328 || !pfile->cb.user_builtin_macro (pfile, node))
3329 {
3330 cpp_error (pfile, CPP_DL_ICE,
3331 "invalid hash type %d in cpp_macro_definition",
3332 node->type);
3333 return 0;
3334 }
79bd622b 3335 }
3336
25692381 3337 macro = node->value.macro;
79bd622b 3338 /* Calculate length. */
109ca87a 3339 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
79bd622b 3340 if (macro->fun_like)
3341 {
3a68fd94 3342 len += 4; /* "()" plus possible final ".." of named
3343 varargs (we have + 1 below). */
79bd622b 3344 for (i = 0; i < macro->paramc; i++)
3a68fd94 3345 len += NODE_LEN (macro->params[i]) + 1; /* "," */
79bd622b 3346 }
3347
9936e07d 3348 /* This should match below where we fill in the buffer. */
9c343313 3349 if (CPP_OPTION (pfile, traditional))
3350 len += _cpp_replacement_text_len (macro);
3351 else
79bd622b 3352 {
941f2388 3353 unsigned int count = macro_real_token_count (macro);
3354 for (i = 0; i < count; i++)
9c343313 3355 {
3356 cpp_token *token = &macro->exp.tokens[i];
79bd622b 3357
9c343313 3358 if (token->type == CPP_MACRO_ARG)
109ca87a 3359 len += NODE_LEN (token->val.macro_arg.spelling);
9c343313 3360 else
9936e07d 3361 len += cpp_token_len (token);
3362
9c343313 3363 if (token->flags & STRINGIFY_ARG)
3364 len++; /* "#" */
3365 if (token->flags & PASTE_LEFT)
3366 len += 3; /* " ##" */
9936e07d 3367 if (token->flags & PREV_WHITE)
3368 len++; /* " " */
9c343313 3369 }
79bd622b 3370 }
3371
3372 if (len > pfile->macro_buffer_len)
8744fb7e 3373 {
720aca92 3374 pfile->macro_buffer = XRESIZEVEC (unsigned char,
3375 pfile->macro_buffer, len);
8744fb7e 3376 pfile->macro_buffer_len = len;
3377 }
4d9e9a28 3378
3379 /* Fill in the buffer. Start with the macro name. */
79bd622b 3380 buffer = pfile->macro_buffer;
109ca87a 3381 buffer = _cpp_spell_ident_ucns (buffer, node);
79bd622b 3382
3383 /* Parameter names. */
3384 if (macro->fun_like)
3385 {
3386 *buffer++ = '(';
3387 for (i = 0; i < macro->paramc; i++)
3388 {
3389 cpp_hashnode *param = macro->params[i];
3390
3391 if (param != pfile->spec_nodes.n__VA_ARGS__)
3392 {
c86dbc5b 3393 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3394 buffer += NODE_LEN (param);
79bd622b 3395 }
3396
3397 if (i + 1 < macro->paramc)
b1a9ff83 3398 /* Don't emit a space after the comma here; we're trying
3399 to emit a Dwarf-friendly definition, and the Dwarf spec
3400 forbids spaces in the argument list. */
3a68fd94 3401 *buffer++ = ',';
53c052ca 3402 else if (macro->variadic)
79bd622b 3403 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3404 }
3405 *buffer++ = ')';
3406 }
3407
920e5093 3408 /* The Dwarf spec requires a space after the macro name, even if the
3409 definition is the empty string. */
3410 *buffer++ = ' ';
3411
9c343313 3412 if (CPP_OPTION (pfile, traditional))
3413 buffer = _cpp_copy_replacement_text (macro, buffer);
3414 else if (macro->count)
79bd622b 3415 /* Expansion tokens. */
79bd622b 3416 {
941f2388 3417 unsigned int count = macro_real_token_count (macro);
3418 for (i = 0; i < count; i++)
79bd622b 3419 {
672f38da 3420 cpp_token *token = &macro->exp.tokens[i];
79bd622b 3421
3422 if (token->flags & PREV_WHITE)
3423 *buffer++ = ' ';
3424 if (token->flags & STRINGIFY_ARG)
3425 *buffer++ = '#';
3426
3427 if (token->type == CPP_MACRO_ARG)
3428 {
c86dbc5b 3429 memcpy (buffer,
109ca87a 3430 NODE_NAME (token->val.macro_arg.spelling),
3431 NODE_LEN (token->val.macro_arg.spelling));
3432 buffer += NODE_LEN (token->val.macro_arg.spelling);
79bd622b 3433 }
3434 else
109ca87a 3435 buffer = cpp_spell_token (pfile, token, buffer, true);
79bd622b 3436
3437 if (token->flags & PASTE_LEFT)
3438 {
3439 *buffer++ = ' ';
3440 *buffer++ = '#';
3441 *buffer++ = '#';
3442 /* Next has PREV_WHITE; see _cpp_create_definition. */
3443 }
3444 }
3445 }
3446
3447 *buffer = '\0';
3448 return pfile->macro_buffer;
3449}