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