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