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