For obj-c stage-final re-use the checksum from the previous stage
[official-gcc.git] / libcpp / macro.c
blob4fc5f838919f41c2baad953d0c8274bded285647
1 /* Part of CPP library. (Macro and #define handling.)
2 Copyright (C) 1986-2021 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
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.
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.
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/>.
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! */
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "internal.h"
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
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. */
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
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
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
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;
93 static const char *vaopt_paste_error =
94 N_("'##' cannot appear at either end of __VA_OPT__");
96 static void expand_arg (cpp_reader *, macro_arg *);
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 {
103 public:
105 enum update_type
107 ERROR,
108 DROP,
109 INCLUDE,
110 BEGIN,
114 /* Initialize the state tracker. ANY_ARGS is true if variable
115 arguments were provided to the macro invocation. */
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)
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. */
131 update_type update (const cpp_token *token)
133 /* If the macro isn't variadic, just don't bother. */
134 if (!m_variadic)
135 return INCLUDE;
137 if (token->type == CPP_NAME
138 && token->val.node.node == m_pfile->spec_nodes.n__VA_OPT__)
140 if (m_state > 0)
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;
146 ++m_state;
147 m_location = token->src_loc;
148 return BEGIN;
150 else if (m_state == 1)
152 if (token->type != CPP_OPEN_PAREN)
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;
159 ++m_state;
160 if (m_update == ERROR)
162 if (m_arg == NULL)
163 m_update = INCLUDE;
164 else
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)
172 m_update = INCLUDE;
173 break;
177 return DROP;
179 else if (m_state >= 2)
181 if (m_state == 2 && token->type == CPP_PASTE)
183 cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
184 vaopt_paste_error);
185 return ERROR;
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;
193 bool was_paste = m_last_was_paste;
194 m_last_was_paste = false;
195 if (token->type == CPP_PASTE)
197 m_last_was_paste = true;
198 m_paste_location = token->src_loc;
200 else if (token->type == CPP_OPEN_PAREN)
201 ++m_state;
202 else if (token->type == CPP_CLOSE_PAREN)
204 --m_state;
205 if (m_state == 2)
207 /* Saw the final paren. */
208 m_state = 0;
210 if (was_paste)
212 cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
213 vaopt_paste_error);
214 return ERROR;
217 return END;
220 return m_update;
223 /* Nothing to do with __VA_OPT__. */
224 return INCLUDE;
227 /* Ensure that any __VA_OPT__ was completed. If ok, return true.
228 Otherwise, issue an error and return false. */
229 bool completed ()
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;
237 private:
239 /* The cpp_reader. */
240 cpp_reader *m_pfile;
242 /* The __VA_ARGS__ argument. */
243 macro_arg *m_arg;
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;
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;
258 /* The location of the paste token. */
259 location_t m_paste_location;
261 /* Location of the __VA_OPT__ token. */
262 location_t m_location;
264 /* If __VA_ARGS__ substitutes to no preprocessing tokens,
265 INCLUDE, otherwise DROP. ERROR when unknown yet. */
266 update_type m_update;
269 /* Macro expansion. */
271 static cpp_macro *get_deferred_or_lazy_macro (cpp_reader *, cpp_hashnode *,
272 location_t);
273 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
274 const cpp_token *, location_t);
275 static int builtin_macro (cpp_reader *, cpp_hashnode *,
276 location_t, location_t);
277 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
278 const cpp_token **, unsigned int);
279 static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
280 _cpp_buff *, location_t *,
281 const cpp_token **, unsigned int);
282 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
283 _cpp_buff **, unsigned *);
284 static cpp_context *next_context (cpp_reader *);
285 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
286 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
287 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
288 static void paste_all_tokens (cpp_reader *, const cpp_token *);
289 static bool paste_tokens (cpp_reader *, location_t,
290 const cpp_token **, const cpp_token *);
291 static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
292 static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
293 static void delete_macro_args (_cpp_buff*, unsigned num_args);
294 static void set_arg_token (macro_arg *, const cpp_token *,
295 location_t, size_t,
296 enum macro_arg_token_kind,
297 bool);
298 static const location_t *get_arg_token_location (const macro_arg *,
299 enum macro_arg_token_kind);
300 static const cpp_token **arg_token_ptr_at (const macro_arg *,
301 size_t,
302 enum macro_arg_token_kind,
303 location_t **virt_location);
305 static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
306 enum macro_arg_token_kind,
307 const macro_arg *,
308 const cpp_token **);
309 static const cpp_token *macro_arg_token_iter_get_token
310 (const macro_arg_token_iter *it);
311 static location_t macro_arg_token_iter_get_location
312 (const macro_arg_token_iter *);
313 static void macro_arg_token_iter_forward (macro_arg_token_iter *);
314 static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
315 location_t **);
316 static size_t tokens_buff_count (_cpp_buff *);
317 static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
318 static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
319 location_t *,
320 const cpp_token *,
321 location_t,
322 location_t,
323 const line_map_macro *,
324 unsigned int);
326 static const cpp_token **tokens_buff_add_token (_cpp_buff *,
327 location_t *,
328 const cpp_token *,
329 location_t,
330 location_t,
331 const line_map_macro *,
332 unsigned int);
333 static inline void tokens_buff_remove_last_token (_cpp_buff *);
334 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
335 macro_arg *, location_t);
336 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
337 _cpp_buff **, unsigned *);
338 static cpp_macro *create_iso_definition (cpp_reader *);
340 /* #define directive parsing and handling. */
342 static cpp_macro *lex_expansion_token (cpp_reader *, 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 *);
352 static cpp_hashnode* macro_of_context (cpp_context *context);
354 /* Statistical counter tracking the number of macros that got
355 expanded. */
356 unsigned num_expanded_macros_counter = 0;
357 /* Statistical counter tracking the total number tokens resulting
358 from macro expansion. */
359 unsigned num_macro_tokens_counter = 0;
361 /* Wrapper around cpp_get_token to skip CPP_PADDING tokens
362 and not consume CPP_EOF. */
363 static const cpp_token *
364 cpp_get_token_no_padding (cpp_reader *pfile)
366 for (;;)
368 const cpp_token *ret = cpp_peek_token (pfile, 0);
369 if (ret->type == CPP_EOF)
370 return ret;
371 ret = cpp_get_token (pfile);
372 if (ret->type != CPP_PADDING)
373 return ret;
377 /* Handle meeting "__has_include" builtin macro. */
379 static int
380 builtin_has_include (cpp_reader *pfile, cpp_hashnode *op, bool has_next)
382 int result = 0;
384 if (!pfile->state.in_directive)
385 cpp_error (pfile, CPP_DL_ERROR,
386 "\"%s\" used outside of preprocessing directive",
387 NODE_NAME (op));
389 pfile->state.angled_headers = true;
390 const cpp_token *token = cpp_get_token_no_padding (pfile);
391 bool paren = token->type == CPP_OPEN_PAREN;
392 if (paren)
393 token = cpp_get_token_no_padding (pfile);
394 else
395 cpp_error (pfile, CPP_DL_ERROR,
396 "missing '(' before \"%s\" operand", NODE_NAME (op));
397 pfile->state.angled_headers = false;
399 bool bracket = token->type != CPP_STRING;
400 char *fname = NULL;
401 if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
403 fname = XNEWVEC (char, token->val.str.len - 1);
404 memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
405 fname[token->val.str.len - 2] = '\0';
407 else if (token->type == CPP_LESS)
408 fname = _cpp_bracket_include (pfile);
409 else
410 cpp_error (pfile, CPP_DL_ERROR,
411 "operator \"%s\" requires a header-name", NODE_NAME (op));
413 if (fname)
415 /* Do not do the lookup if we're skipping, that's unnecessary
416 IO. */
417 if (!pfile->state.skip_eval
418 && _cpp_has_header (pfile, fname, bracket,
419 has_next ? IT_INCLUDE_NEXT : IT_INCLUDE))
420 result = 1;
422 XDELETEVEC (fname);
425 if (paren
426 && cpp_get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
427 cpp_error (pfile, CPP_DL_ERROR,
428 "missing ')' after \"%s\" operand", NODE_NAME (op));
430 return result;
433 /* Emits a warning if NODE is a macro defined in the main file that
434 has not been used. */
436 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
437 void *v ATTRIBUTE_UNUSED)
439 if (cpp_user_macro_p (node))
441 cpp_macro *macro = node->value.macro;
443 if (!macro->used
444 && MAIN_FILE_P (linemap_check_ordinary
445 (linemap_lookup (pfile->line_table,
446 macro->line))))
447 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
448 "macro \"%s\" is not used", NODE_NAME (node));
451 return 1;
454 /* Allocates and returns a CPP_STRING token, containing TEXT of length
455 LEN, after null-terminating it. TEXT must be in permanent storage. */
456 static const cpp_token *
457 new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
459 cpp_token *token = _cpp_temp_token (pfile);
461 text[len] = '\0';
462 token->type = CPP_STRING;
463 token->val.str.len = len;
464 token->val.str.text = text;
465 token->flags = 0;
466 return token;
469 static const char * const monthnames[] =
471 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
472 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
475 /* Helper function for builtin_macro. Returns the text generated by
476 a builtin macro. */
477 const uchar *
478 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
479 location_t loc)
481 const uchar *result = NULL;
482 linenum_type number = 1;
484 switch (node->value.builtin)
486 default:
487 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
488 NODE_NAME (node));
489 break;
491 case BT_TIMESTAMP:
493 if (CPP_OPTION (pfile, warn_date_time))
494 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
495 "reproducible builds", NODE_NAME (node));
497 cpp_buffer *pbuffer = cpp_get_buffer (pfile);
498 if (pbuffer->timestamp == NULL)
500 /* Initialize timestamp value of the assotiated file. */
501 struct _cpp_file *file = cpp_get_file (pbuffer);
502 if (file)
504 /* Generate __TIMESTAMP__ string, that represents
505 the date and time of the last modification
506 of the current source file. The string constant
507 looks like "Sun Sep 16 01:03:52 1973". */
508 struct tm *tb = NULL;
509 struct stat *st = _cpp_get_file_stat (file);
510 if (st)
511 tb = localtime (&st->st_mtime);
512 if (tb)
514 char *str = asctime (tb);
515 size_t len = strlen (str);
516 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
517 buf[0] = '"';
518 strcpy ((char *) buf + 1, str);
519 buf[len] = '"';
520 pbuffer->timestamp = buf;
522 else
524 cpp_errno (pfile, CPP_DL_WARNING,
525 "could not determine file timestamp");
526 pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
530 result = pbuffer->timestamp;
532 break;
533 case BT_FILE:
534 case BT_FILE_NAME:
535 case BT_BASE_FILE:
537 unsigned int len;
538 const char *name;
539 uchar *buf;
541 if (node->value.builtin == BT_FILE
542 || node->value.builtin == BT_FILE_NAME)
544 name = linemap_get_expansion_filename (pfile->line_table,
545 pfile->line_table->highest_line);
546 if ((node->value.builtin == BT_FILE_NAME) && name)
547 name = lbasename (name);
549 else
551 name = _cpp_get_file_name (pfile->main_file);
552 if (!name)
553 abort ();
555 if (pfile->cb.remap_filename)
556 name = pfile->cb.remap_filename (name);
557 len = strlen (name);
558 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
559 result = buf;
560 *buf = '"';
561 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
562 *buf++ = '"';
563 *buf = '\0';
565 break;
567 case BT_INCLUDE_LEVEL:
568 /* The line map depth counts the primary source as level 1, but
569 historically __INCLUDE_DEPTH__ has called the primary source
570 level 0. */
571 number = pfile->line_table->depth - 1;
572 break;
574 case BT_SPECLINE:
575 /* If __LINE__ is embedded in a macro, it must expand to the
576 line of the macro's invocation, not its definition.
577 Otherwise things like assert() will not work properly.
578 See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */
579 if (CPP_OPTION (pfile, traditional))
580 loc = pfile->line_table->highest_line;
581 else
582 loc = linemap_resolve_location (pfile->line_table, loc,
583 LRK_MACRO_EXPANSION_POINT, NULL);
584 number = linemap_get_expansion_line (pfile->line_table, loc);
585 break;
587 /* __STDC__ has the value 1 under normal circumstances.
588 However, if (a) we are in a system header, (b) the option
589 stdc_0_in_system_headers is true (set by target config), and
590 (c) we are not in strictly conforming mode, then it has the
591 value 0. (b) and (c) are already checked in cpp_init_builtins. */
592 case BT_STDC:
593 if (_cpp_in_system_header (pfile))
594 number = 0;
595 else
596 number = 1;
597 break;
599 case BT_DATE:
600 case BT_TIME:
601 if (CPP_OPTION (pfile, warn_date_time))
602 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
603 "reproducible builds", NODE_NAME (node));
604 if (pfile->date == NULL)
606 /* Allocate __DATE__ and __TIME__ strings from permanent
607 storage. We only do this once, and don't generate them
608 at init time, because time() and localtime() are very
609 slow on some systems. */
610 time_t tt;
611 auto kind = cpp_get_date (pfile, &tt);
613 if (kind == CPP_time_kind::UNKNOWN)
615 cpp_errno (pfile, CPP_DL_WARNING,
616 "could not determine date and time");
618 pfile->date = UC"\"??? ?? ????\"";
619 pfile->time = UC"\"??:??:??\"";
621 else
623 struct tm *tb = (kind == CPP_time_kind::FIXED
624 ? gmtime : localtime) (&tt);
626 pfile->date = _cpp_unaligned_alloc (pfile,
627 sizeof ("\"Oct 11 1347\""));
628 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
629 monthnames[tb->tm_mon], tb->tm_mday,
630 tb->tm_year + 1900);
632 pfile->time = _cpp_unaligned_alloc (pfile,
633 sizeof ("\"12:34:56\""));
634 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
635 tb->tm_hour, tb->tm_min, tb->tm_sec);
639 if (node->value.builtin == BT_DATE)
640 result = pfile->date;
641 else
642 result = pfile->time;
643 break;
645 case BT_COUNTER:
646 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
647 cpp_error (pfile, CPP_DL_ERROR,
648 "__COUNTER__ expanded inside directive with -fdirectives-only");
649 number = pfile->counter++;
650 break;
652 case BT_HAS_ATTRIBUTE:
653 number = pfile->cb.has_attribute (pfile, false);
654 break;
656 case BT_HAS_STD_ATTRIBUTE:
657 number = pfile->cb.has_attribute (pfile, true);
658 break;
660 case BT_HAS_BUILTIN:
661 number = pfile->cb.has_builtin (pfile);
662 break;
664 case BT_HAS_INCLUDE:
665 case BT_HAS_INCLUDE_NEXT:
666 number = builtin_has_include (pfile, node,
667 node->value.builtin == BT_HAS_INCLUDE_NEXT);
668 break;
671 if (result == NULL)
673 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
674 result = _cpp_unaligned_alloc (pfile, 21);
675 sprintf ((char *) result, "%u", number);
678 return result;
681 /* Get an idempotent date. Either the cached value, the value from
682 source epoch, or failing that, the value from time(2). Use this
683 during compilation so that every time stamp is the same. */
684 CPP_time_kind
685 cpp_get_date (cpp_reader *pfile, time_t *result)
687 if (!pfile->time_stamp_kind)
689 int kind = 0;
690 if (pfile->cb.get_source_date_epoch)
692 /* Try reading the fixed epoch. */
693 pfile->time_stamp = pfile->cb.get_source_date_epoch (pfile);
694 if (pfile->time_stamp != time_t (-1))
695 kind = int (CPP_time_kind::FIXED);
698 if (!kind)
700 /* Pedantically time_t (-1) is a legitimate value for
701 "number of seconds since the Epoch". It is a silly
702 time. */
703 errno = 0;
704 pfile->time_stamp = time (nullptr);
705 /* Annoyingly a library could legally set errno and return a
706 valid time! Bad library! */
707 if (pfile->time_stamp == time_t (-1) && errno)
708 kind = errno;
709 else
710 kind = int (CPP_time_kind::DYNAMIC);
713 pfile->time_stamp_kind = kind;
716 *result = pfile->time_stamp;
717 if (pfile->time_stamp_kind >= 0)
719 errno = pfile->time_stamp_kind;
720 return CPP_time_kind::UNKNOWN;
723 return CPP_time_kind (pfile->time_stamp_kind);
726 /* Convert builtin macros like __FILE__ to a token and push it on the
727 context stack. Also handles _Pragma, for which a new token may not
728 be created. Returns 1 if it generates a new token context, 0 to
729 return the token to the caller. LOC is the location of the expansion
730 point of the macro. */
731 static int
732 builtin_macro (cpp_reader *pfile, cpp_hashnode *node,
733 location_t loc, location_t expand_loc)
735 const uchar *buf;
736 size_t len;
737 char *nbuf;
739 if (node->value.builtin == BT_PRAGMA)
741 /* Don't interpret _Pragma within directives. The standard is
742 not clear on this, but to me this makes most sense. */
743 if (pfile->state.in_directive)
744 return 0;
746 return _cpp_do__Pragma (pfile, loc);
749 buf = _cpp_builtin_macro_text (pfile, node, expand_loc);
750 len = ustrlen (buf);
751 nbuf = (char *) alloca (len + 1);
752 memcpy (nbuf, buf, len);
753 nbuf[len]='\n';
755 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
756 _cpp_clean_line (pfile);
758 /* Set pfile->cur_token as required by _cpp_lex_direct. */
759 pfile->cur_token = _cpp_temp_token (pfile);
760 cpp_token *token = _cpp_lex_direct (pfile);
761 /* We should point to the expansion point of the builtin macro. */
762 token->src_loc = loc;
763 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
765 /* We are tracking tokens resulting from macro expansion.
766 Create a macro line map and generate a virtual location for
767 the token resulting from the expansion of the built-in
768 macro. */
769 location_t *virt_locs = NULL;
770 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
771 const line_map_macro * map =
772 linemap_enter_macro (pfile->line_table, node, loc, 1);
773 tokens_buff_add_token (token_buf, virt_locs, token,
774 pfile->line_table->builtin_location,
775 pfile->line_table->builtin_location,
776 map, /*macro_token_index=*/0);
777 push_extended_tokens_context (pfile, node, token_buf, virt_locs,
778 (const cpp_token **)token_buf->base,
781 else
782 _cpp_push_token_context (pfile, NULL, token, 1);
783 if (pfile->buffer->cur != pfile->buffer->rlimit)
784 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
785 NODE_NAME (node));
786 _cpp_pop_buffer (pfile);
788 return 1;
791 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
792 backslashes and double quotes. DEST must be of sufficient size.
793 Returns a pointer to the end of the string. */
794 uchar *
795 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
797 while (len--)
799 uchar c = *src++;
801 switch (c)
803 case '\n':
804 /* Naked LF can appear in raw string literals */
805 c = 'n';
806 /* FALLTHROUGH */
808 case '\\':
809 case '"':
810 *dest++ = '\\';
811 /* FALLTHROUGH */
813 default:
814 *dest++ = c;
818 return dest;
821 /* Convert a token sequence ARG to a single string token according to
822 the rules of the ISO C #-operator. */
823 static const cpp_token *
824 stringify_arg (cpp_reader *pfile, macro_arg *arg)
826 unsigned char *dest;
827 unsigned int i, escape_it, backslash_count = 0;
828 const cpp_token *source = NULL;
829 size_t len;
831 if (BUFF_ROOM (pfile->u_buff) < 3)
832 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
833 dest = BUFF_FRONT (pfile->u_buff);
834 *dest++ = '"';
836 /* Loop, reading in the argument's tokens. */
837 for (i = 0; i < arg->count; i++)
839 const cpp_token *token = arg->first[i];
841 if (token->type == CPP_PADDING)
843 if (source == NULL
844 || (!(source->flags & PREV_WHITE)
845 && token->val.source == NULL))
846 source = token->val.source;
847 continue;
850 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
851 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
852 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
853 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
854 || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR
855 || cpp_userdef_string_p (token->type)
856 || cpp_userdef_char_p (token->type));
858 /* Room for each char being written in octal, initial space and
859 final quote and NUL. */
860 len = cpp_token_len (token);
861 if (escape_it)
862 len *= 4;
863 len += 3;
865 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
867 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
868 _cpp_extend_buff (pfile, &pfile->u_buff, len);
869 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
872 /* Leading white space? */
873 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
875 if (source == NULL)
876 source = token;
877 if (source->flags & PREV_WHITE)
878 *dest++ = ' ';
880 source = NULL;
882 if (escape_it)
884 _cpp_buff *buff = _cpp_get_buff (pfile, len);
885 unsigned char *buf = BUFF_FRONT (buff);
886 len = cpp_spell_token (pfile, token, buf, true) - buf;
887 dest = cpp_quote_string (dest, buf, len);
888 _cpp_release_buff (pfile, buff);
890 else
891 dest = cpp_spell_token (pfile, token, dest, true);
893 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
894 backslash_count++;
895 else
896 backslash_count = 0;
899 /* Ignore the final \ of invalid string literals. */
900 if (backslash_count & 1)
902 cpp_error (pfile, CPP_DL_WARNING,
903 "invalid string literal, ignoring final '\\'");
904 dest--;
907 /* Commit the memory, including NUL, and return the token. */
908 *dest++ = '"';
909 len = dest - BUFF_FRONT (pfile->u_buff);
910 BUFF_FRONT (pfile->u_buff) = dest + 1;
911 return new_string_token (pfile, dest - len, len);
914 /* Try to paste two tokens. On success, return nonzero. In any
915 case, PLHS is updated to point to the pasted token, which is
916 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
917 the virtual location used for error reporting. */
918 static bool
919 paste_tokens (cpp_reader *pfile, location_t location,
920 const cpp_token **plhs, const cpp_token *rhs)
922 unsigned char *buf, *end, *lhsend;
923 cpp_token *lhs;
924 unsigned int len;
926 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
927 buf = (unsigned char *) alloca (len);
928 end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
930 /* Avoid comment headers, since they are still processed in stage 3.
931 It is simpler to insert a space here, rather than modifying the
932 lexer to ignore comments in some circumstances. Simply returning
933 false doesn't work, since we want to clear the PASTE_LEFT flag. */
934 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
935 *end++ = ' ';
936 /* In one obscure case we might see padding here. */
937 if (rhs->type != CPP_PADDING)
938 end = cpp_spell_token (pfile, rhs, end, true);
939 *end = '\n';
941 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
942 _cpp_clean_line (pfile);
944 /* Set pfile->cur_token as required by _cpp_lex_direct. */
945 pfile->cur_token = _cpp_temp_token (pfile);
946 lhs = _cpp_lex_direct (pfile);
947 if (pfile->buffer->cur != pfile->buffer->rlimit)
949 location_t saved_loc = lhs->src_loc;
951 _cpp_pop_buffer (pfile);
952 _cpp_backup_tokens (pfile, 1);
953 *lhsend = '\0';
955 /* We have to remove the PASTE_LEFT flag from the old lhs, but
956 we want to keep the new location. */
957 *lhs = **plhs;
958 *plhs = lhs;
959 lhs->src_loc = saved_loc;
960 lhs->flags &= ~PASTE_LEFT;
962 /* Mandatory error for all apart from assembler. */
963 if (CPP_OPTION (pfile, lang) != CLK_ASM)
964 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
965 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
966 buf, cpp_token_as_text (pfile, rhs));
967 return false;
970 *plhs = lhs;
971 _cpp_pop_buffer (pfile);
972 return true;
975 /* Handles an arbitrarily long sequence of ## operators, with initial
976 operand LHS. This implementation is left-associative,
977 non-recursive, and finishes a paste before handling succeeding
978 ones. If a paste fails, we back up to the RHS of the failing ##
979 operator before pushing the context containing the result of prior
980 successful pastes, with the effect that the RHS appears in the
981 output stream after the pasted LHS normally. */
982 static void
983 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
985 const cpp_token *rhs = NULL;
986 cpp_context *context = pfile->context;
987 location_t virt_loc = 0;
989 /* We are expanding a macro and we must have been called on a token
990 that appears at the left hand side of a ## operator. */
991 if (macro_of_context (pfile->context) == NULL
992 || (!(lhs->flags & PASTE_LEFT)))
993 abort ();
995 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
996 /* The caller must have called consume_next_token_from_context
997 right before calling us. That has incremented the pointer to
998 the current virtual location. So it now points to the location
999 of the token that comes right after *LHS. We want the
1000 resulting pasted token to have the location of the current
1001 *LHS, though. */
1002 virt_loc = context->c.mc->cur_virt_loc[-1];
1003 else
1004 /* We are not tracking macro expansion. So the best virtual
1005 location we can get here is the expansion point of the macro we
1006 are currently expanding. */
1007 virt_loc = pfile->invocation_location;
1011 /* Take the token directly from the current context. We can do
1012 this, because we are in the replacement list of either an
1013 object-like macro, or a function-like macro with arguments
1014 inserted. In either case, the constraints to #define
1015 guarantee we have at least one more token. */
1016 if (context->tokens_kind == TOKENS_KIND_DIRECT)
1017 rhs = FIRST (context).token++;
1018 else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
1019 rhs = *FIRST (context).ptoken++;
1020 else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
1022 /* So we are in presence of an extended token context, which
1023 means that each token in this context has a virtual
1024 location attached to it. So let's not forget to update
1025 the pointer to the current virtual location of the
1026 current token when we update the pointer to the current
1027 token */
1029 rhs = *FIRST (context).ptoken++;
1030 /* context->c.mc must be non-null, as if we were not in a
1031 macro context, context->tokens_kind could not be equal to
1032 TOKENS_KIND_EXTENDED. */
1033 context->c.mc->cur_virt_loc++;
1036 if (rhs->type == CPP_PADDING)
1038 if (rhs->flags & PASTE_LEFT)
1039 abort ();
1041 if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
1042 break;
1044 while (rhs->flags & PASTE_LEFT);
1046 /* Put the resulting token in its own context. */
1047 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
1049 location_t *virt_locs = NULL;
1050 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
1051 tokens_buff_add_token (token_buf, virt_locs, lhs,
1052 virt_loc, 0, NULL, 0);
1053 push_extended_tokens_context (pfile, context->c.mc->macro_node,
1054 token_buf, virt_locs,
1055 (const cpp_token **)token_buf->base, 1);
1057 else
1058 _cpp_push_token_context (pfile, NULL, lhs, 1);
1061 /* Returns TRUE if the number of arguments ARGC supplied in an
1062 invocation of the MACRO referenced by NODE is valid. An empty
1063 invocation to a macro with no parameters should pass ARGC as zero.
1065 Note that MACRO cannot necessarily be deduced from NODE, in case
1066 NODE was redefined whilst collecting arguments. */
1067 bool
1068 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
1070 if (argc == macro->paramc)
1071 return true;
1073 if (argc < macro->paramc)
1075 /* In C++20 (here the va_opt flag is used), and also as a GNU
1076 extension, variadic arguments are allowed to not appear in
1077 the invocation at all.
1078 e.g. #define debug(format, args...) something
1079 debug("string");
1081 This is exactly the same as if an empty variadic list had been
1082 supplied - debug("string", ). */
1084 if (argc + 1 == macro->paramc && macro->variadic)
1086 if (CPP_PEDANTIC (pfile) && ! macro->syshdr
1087 && ! CPP_OPTION (pfile, va_opt))
1089 if (CPP_OPTION (pfile, cplusplus))
1090 cpp_error (pfile, CPP_DL_PEDWARN,
1091 "ISO C++11 requires at least one argument "
1092 "for the \"...\" in a variadic macro");
1093 else
1094 cpp_error (pfile, CPP_DL_PEDWARN,
1095 "ISO C99 requires at least one argument "
1096 "for the \"...\" in a variadic macro");
1098 return true;
1101 cpp_error (pfile, CPP_DL_ERROR,
1102 "macro \"%s\" requires %u arguments, but only %u given",
1103 NODE_NAME (node), macro->paramc, argc);
1105 else
1106 cpp_error (pfile, CPP_DL_ERROR,
1107 "macro \"%s\" passed %u arguments, but takes just %u",
1108 NODE_NAME (node), argc, macro->paramc);
1110 if (macro->line > RESERVED_LOCATION_COUNT)
1111 cpp_error_at (pfile, CPP_DL_NOTE, macro->line, "macro \"%s\" defined here",
1112 NODE_NAME (node));
1114 return false;
1117 /* Reads and returns the arguments to a function-like macro
1118 invocation. Assumes the opening parenthesis has been processed.
1119 If there is an error, emits an appropriate diagnostic and returns
1120 NULL. Each argument is terminated by a CPP_EOF token, for the
1121 future benefit of expand_arg(). If there are any deferred
1122 #pragma directives among macro arguments, store pointers to the
1123 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
1125 What is returned is the buffer that contains the memory allocated
1126 to hold the macro arguments. NODE is the name of the macro this
1127 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
1128 set to the actual number of macro arguments allocated in the
1129 returned buffer. */
1130 static _cpp_buff *
1131 collect_args (cpp_reader *pfile, const cpp_hashnode *node,
1132 _cpp_buff **pragma_buff, unsigned *num_args)
1134 _cpp_buff *buff, *base_buff;
1135 cpp_macro *macro;
1136 macro_arg *args, *arg;
1137 const cpp_token *token;
1138 unsigned int argc;
1139 location_t virt_loc;
1140 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
1141 unsigned num_args_alloced = 0;
1143 macro = node->value.macro;
1144 if (macro->paramc)
1145 argc = macro->paramc;
1146 else
1147 argc = 1;
1149 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
1150 #define ARG_TOKENS_EXTENT 1000
1152 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
1153 * sizeof (cpp_token *)
1154 + sizeof (macro_arg)));
1155 base_buff = buff;
1156 args = (macro_arg *) buff->base;
1157 memset (args, 0, argc * sizeof (macro_arg));
1158 buff->cur = (unsigned char *) &args[argc];
1159 arg = args, argc = 0;
1161 /* Collect the tokens making up each argument. We don't yet know
1162 how many arguments have been supplied, whether too many or too
1163 few. Hence the slightly bizarre usage of "argc" and "arg". */
1166 unsigned int paren_depth = 0;
1167 unsigned int ntokens = 0;
1168 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
1169 num_args_alloced++;
1171 argc++;
1172 arg->first = (const cpp_token **) buff->cur;
1173 if (track_macro_expansion_p)
1175 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
1176 arg->virt_locs = XNEWVEC (location_t,
1177 virt_locs_capacity);
1180 for (;;)
1182 /* Require space for 2 new tokens (including a CPP_EOF). */
1183 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
1185 buff = _cpp_append_extend_buff (pfile, buff,
1186 ARG_TOKENS_EXTENT
1187 * sizeof (cpp_token *));
1188 arg->first = (const cpp_token **) buff->cur;
1190 if (track_macro_expansion_p
1191 && (ntokens + 2 > virt_locs_capacity))
1193 virt_locs_capacity += ARG_TOKENS_EXTENT;
1194 arg->virt_locs = XRESIZEVEC (location_t,
1195 arg->virt_locs,
1196 virt_locs_capacity);
1199 token = cpp_get_token_1 (pfile, &virt_loc);
1201 if (token->type == CPP_PADDING)
1203 /* Drop leading padding. */
1204 if (ntokens == 0)
1205 continue;
1207 else if (token->type == CPP_OPEN_PAREN)
1208 paren_depth++;
1209 else if (token->type == CPP_CLOSE_PAREN)
1211 if (paren_depth-- == 0)
1212 break;
1214 else if (token->type == CPP_COMMA)
1216 /* A comma does not terminate an argument within
1217 parentheses or as part of a variable argument. */
1218 if (paren_depth == 0
1219 && ! (macro->variadic && argc == macro->paramc))
1220 break;
1222 else if (token->type == CPP_EOF
1223 || (token->type == CPP_HASH && token->flags & BOL))
1224 break;
1225 else if (token->type == CPP_PRAGMA)
1227 cpp_token *newtok = _cpp_temp_token (pfile);
1229 /* CPP_PRAGMA token lives in directive_result, which will
1230 be overwritten on the next directive. */
1231 *newtok = *token;
1232 token = newtok;
1235 if (*pragma_buff == NULL
1236 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
1238 _cpp_buff *next;
1239 if (*pragma_buff == NULL)
1240 *pragma_buff
1241 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
1242 else
1244 next = *pragma_buff;
1245 *pragma_buff
1246 = _cpp_get_buff (pfile,
1247 (BUFF_FRONT (*pragma_buff)
1248 - (*pragma_buff)->base) * 2);
1249 (*pragma_buff)->next = next;
1252 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
1253 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
1254 if (token->type == CPP_PRAGMA_EOL)
1255 break;
1256 token = cpp_get_token_1 (pfile, &virt_loc);
1258 while (token->type != CPP_EOF);
1260 /* In deferred pragmas parsing_args and prevent_expansion
1261 had been changed, reset it. */
1262 pfile->state.parsing_args = 2;
1263 pfile->state.prevent_expansion = 1;
1265 if (token->type == CPP_EOF)
1266 break;
1267 else
1268 continue;
1270 set_arg_token (arg, token, virt_loc,
1271 ntokens, MACRO_ARG_TOKEN_NORMAL,
1272 CPP_OPTION (pfile, track_macro_expansion));
1273 ntokens++;
1276 /* Drop trailing padding. */
1277 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
1278 ntokens--;
1280 arg->count = ntokens;
1281 /* Append an EOF to mark end-of-argument. */
1282 set_arg_token (arg, &pfile->endarg, token->src_loc,
1283 ntokens, MACRO_ARG_TOKEN_NORMAL,
1284 CPP_OPTION (pfile, track_macro_expansion));
1286 /* Terminate the argument. Excess arguments loop back and
1287 overwrite the final legitimate argument, before failing. */
1288 if (argc <= macro->paramc)
1290 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
1291 if (argc != macro->paramc)
1292 arg++;
1295 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
1297 if (token->type == CPP_EOF)
1299 /* Unless the EOF is marking the end of an argument, it's a fake
1300 one from the end of a file that _cpp_clean_line will not have
1301 advanced past. */
1302 if (token == &pfile->endarg)
1303 _cpp_backup_tokens (pfile, 1);
1304 cpp_error (pfile, CPP_DL_ERROR,
1305 "unterminated argument list invoking macro \"%s\"",
1306 NODE_NAME (node));
1308 else
1310 /* A single empty argument is counted as no argument. */
1311 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
1312 argc = 0;
1313 if (_cpp_arguments_ok (pfile, macro, node, argc))
1315 /* GCC has special semantics for , ## b where b is a varargs
1316 parameter: we remove the comma if b was omitted entirely.
1317 If b was merely an empty argument, the comma is retained.
1318 If the macro takes just one (varargs) parameter, then we
1319 retain the comma only if we are standards conforming.
1321 If FIRST is NULL replace_args () swallows the comma. */
1322 if (macro->variadic && (argc < macro->paramc
1323 || (argc == 1 && args[0].count == 0
1324 && !CPP_OPTION (pfile, std))))
1325 args[macro->paramc - 1].first = NULL;
1326 if (num_args)
1327 *num_args = num_args_alloced;
1328 return base_buff;
1332 /* An error occurred. */
1333 _cpp_release_buff (pfile, base_buff);
1334 return NULL;
1337 /* Search for an opening parenthesis to the macro of NODE, in such a
1338 way that, if none is found, we don't lose the information in any
1339 intervening padding tokens. If we find the parenthesis, collect
1340 the arguments and return the buffer containing them. PRAGMA_BUFF
1341 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
1342 *NUM_ARGS is set to the number of arguments contained in the
1343 returned buffer. */
1344 static _cpp_buff *
1345 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
1346 _cpp_buff **pragma_buff, unsigned *num_args)
1348 const cpp_token *token, *padding = NULL;
1350 for (;;)
1352 token = cpp_get_token (pfile);
1353 if (token->type != CPP_PADDING)
1354 break;
1355 if (padding == NULL
1356 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
1357 padding = token;
1360 if (token->type == CPP_OPEN_PAREN)
1362 pfile->state.parsing_args = 2;
1363 return collect_args (pfile, node, pragma_buff, num_args);
1366 /* Back up. A CPP_EOF is either an EOF from an argument we're
1367 expanding, or a fake one from lex_direct. We want to backup the
1368 former, but not the latter. We may have skipped padding, in
1369 which case backing up more than one token when expanding macros
1370 is in general too difficult. We re-insert it in its own
1371 context. */
1372 if (token->type != CPP_EOF || token == &pfile->endarg)
1374 _cpp_backup_tokens (pfile, 1);
1375 if (padding)
1376 _cpp_push_token_context (pfile, NULL, padding, 1);
1379 return NULL;
1382 /* Return the real number of tokens in the expansion of MACRO. */
1383 static inline unsigned int
1384 macro_real_token_count (const cpp_macro *macro)
1386 if (__builtin_expect (!macro->extra_tokens, true))
1387 return macro->count;
1389 for (unsigned i = macro->count; i--;)
1390 if (macro->exp.tokens[i].type != CPP_PASTE)
1391 return i + 1;
1393 return 0;
1396 /* Push the context of a macro with hash entry NODE onto the context
1397 stack. If we can successfully expand the macro, we push a context
1398 containing its yet-to-be-rescanned replacement list and return one.
1399 If there were additionally any unexpanded deferred #pragma
1400 directives among macro arguments, push another context containing
1401 the pragma tokens before the yet-to-be-rescanned replacement list
1402 and return two. Otherwise, we don't push a context and return
1403 zero. LOCATION is the location of the expansion point of the
1404 macro. */
1405 static int
1406 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
1407 const cpp_token *result, location_t location)
1409 /* The presence of a macro invalidates a file's controlling macro. */
1410 pfile->mi_valid = false;
1412 pfile->state.angled_headers = false;
1414 /* From here to when we push the context for the macro later down
1415 this function, we need to flag the fact that we are about to
1416 expand a macro. This is useful when -ftrack-macro-expansion is
1417 turned off. In that case, we need to record the location of the
1418 expansion point of the top-most macro we are about to to expand,
1419 into pfile->invocation_location. But we must not record any such
1420 location once the process of expanding the macro starts; that is,
1421 we must not do that recording between now and later down this
1422 function where set this flag to FALSE. */
1423 pfile->about_to_expand_macro_p = true;
1425 if (cpp_user_macro_p (node))
1427 cpp_macro *macro = node->value.macro;
1428 _cpp_buff *pragma_buff = NULL;
1430 if (macro->fun_like)
1432 _cpp_buff *buff;
1433 unsigned num_args = 0;
1435 pfile->state.prevent_expansion++;
1436 pfile->keep_tokens++;
1437 pfile->state.parsing_args = 1;
1438 buff = funlike_invocation_p (pfile, node, &pragma_buff,
1439 &num_args);
1440 pfile->state.parsing_args = 0;
1441 pfile->keep_tokens--;
1442 pfile->state.prevent_expansion--;
1444 if (buff == NULL)
1446 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
1447 cpp_warning (pfile, CPP_W_TRADITIONAL,
1448 "function-like macro \"%s\" must be used with arguments in traditional C",
1449 NODE_NAME (node));
1451 if (pragma_buff)
1452 _cpp_release_buff (pfile, pragma_buff);
1454 pfile->about_to_expand_macro_p = false;
1455 return 0;
1458 if (macro->paramc > 0)
1459 replace_args (pfile, node, macro,
1460 (macro_arg *) buff->base,
1461 location);
1462 /* Free the memory used by the arguments of this
1463 function-like macro. This memory has been allocated by
1464 funlike_invocation_p and by replace_args. */
1465 delete_macro_args (buff, num_args);
1468 /* Disable the macro within its expansion. */
1469 node->flags |= NODE_DISABLED;
1471 /* Laziness can only affect the expansion tokens of the macro,
1472 not its fun-likeness or parameters. */
1473 _cpp_maybe_notify_macro_use (pfile, node, location);
1474 if (pfile->cb.used)
1475 pfile->cb.used (pfile, location, node);
1477 macro->used = 1;
1479 if (macro->paramc == 0)
1481 unsigned tokens_count = macro_real_token_count (macro);
1482 if (CPP_OPTION (pfile, track_macro_expansion))
1484 unsigned int i;
1485 const cpp_token *src = macro->exp.tokens;
1486 const line_map_macro *map;
1487 location_t *virt_locs = NULL;
1488 _cpp_buff *macro_tokens
1489 = tokens_buff_new (pfile, tokens_count, &virt_locs);
1491 /* Create a macro map to record the locations of the
1492 tokens that are involved in the expansion. LOCATION
1493 is the location of the macro expansion point. */
1494 map = linemap_enter_macro (pfile->line_table,
1495 node, location, tokens_count);
1496 for (i = 0; i < tokens_count; ++i)
1498 tokens_buff_add_token (macro_tokens, virt_locs,
1499 src, src->src_loc,
1500 src->src_loc, map, i);
1501 ++src;
1503 push_extended_tokens_context (pfile, node,
1504 macro_tokens,
1505 virt_locs,
1506 (const cpp_token **)
1507 macro_tokens->base,
1508 tokens_count);
1510 else
1511 _cpp_push_token_context (pfile, node, macro->exp.tokens,
1512 tokens_count);
1513 num_macro_tokens_counter += tokens_count;
1516 if (pragma_buff)
1518 if (!pfile->state.in_directive)
1519 _cpp_push_token_context (pfile, NULL,
1520 padding_token (pfile, result), 1);
1523 unsigned tokens_count;
1524 _cpp_buff *tail = pragma_buff->next;
1525 pragma_buff->next = NULL;
1526 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1527 - (const cpp_token **) pragma_buff->base);
1528 push_ptoken_context (pfile, NULL, pragma_buff,
1529 (const cpp_token **) pragma_buff->base,
1530 tokens_count);
1531 pragma_buff = tail;
1532 if (!CPP_OPTION (pfile, track_macro_expansion))
1533 num_macro_tokens_counter += tokens_count;
1536 while (pragma_buff != NULL);
1537 pfile->about_to_expand_macro_p = false;
1538 return 2;
1541 pfile->about_to_expand_macro_p = false;
1542 return 1;
1545 pfile->about_to_expand_macro_p = false;
1546 /* Handle built-in macros and the _Pragma operator. */
1548 location_t expand_loc;
1550 if (/* The top-level macro invocation that triggered the expansion
1551 we are looking at is with a function-like user macro ... */
1552 cpp_fun_like_macro_p (pfile->top_most_macro_node)
1553 /* ... and we are tracking the macro expansion. */
1554 && CPP_OPTION (pfile, track_macro_expansion))
1555 /* Then the location of the end of the macro invocation is the
1556 location of the expansion point of this macro. */
1557 expand_loc = location;
1558 else
1559 /* Otherwise, the location of the end of the macro invocation is
1560 the location of the expansion point of that top-level macro
1561 invocation. */
1562 expand_loc = pfile->invocation_location;
1564 return builtin_macro (pfile, node, location, expand_loc);
1568 /* De-allocate the memory used by BUFF which is an array of instances
1569 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1570 present in BUFF. */
1571 static void
1572 delete_macro_args (_cpp_buff *buff, unsigned num_args)
1574 macro_arg *macro_args;
1575 unsigned i;
1577 if (buff == NULL)
1578 return;
1580 macro_args = (macro_arg *) buff->base;
1582 /* Walk instances of macro_arg to free their expanded tokens as well
1583 as their macro_arg::virt_locs members. */
1584 for (i = 0; i < num_args; ++i)
1586 if (macro_args[i].expanded)
1588 free (macro_args[i].expanded);
1589 macro_args[i].expanded = NULL;
1591 if (macro_args[i].virt_locs)
1593 free (macro_args[i].virt_locs);
1594 macro_args[i].virt_locs = NULL;
1596 if (macro_args[i].expanded_virt_locs)
1598 free (macro_args[i].expanded_virt_locs);
1599 macro_args[i].expanded_virt_locs = NULL;
1602 _cpp_free_buff (buff);
1605 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1606 to set, LOCATION is its virtual location. "Virtual" location means
1607 the location that encodes loci across macro expansion. Otherwise
1608 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1609 argument ARG is supposed to contain. Note that ARG must be
1610 tailored so that it has enough room to contain INDEX + 1 numbers of
1611 tokens, at least. */
1612 static void
1613 set_arg_token (macro_arg *arg, const cpp_token *token,
1614 location_t location, size_t index,
1615 enum macro_arg_token_kind kind,
1616 bool track_macro_exp_p)
1618 const cpp_token **token_ptr;
1619 location_t *loc = NULL;
1621 token_ptr =
1622 arg_token_ptr_at (arg, index, kind,
1623 track_macro_exp_p ? &loc : NULL);
1624 *token_ptr = token;
1626 if (loc != NULL)
1628 /* We can't set the location of a stringified argument
1629 token and we can't set any location if we aren't tracking
1630 macro expansion locations. */
1631 gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
1632 && track_macro_exp_p);
1633 *loc = location;
1637 /* Get the pointer to the location of the argument token of the
1638 function-like macro argument ARG. This function must be called
1639 only when we -ftrack-macro-expansion is on. */
1640 static const location_t *
1641 get_arg_token_location (const macro_arg *arg,
1642 enum macro_arg_token_kind kind)
1644 const location_t *loc = NULL;
1645 const cpp_token **token_ptr =
1646 arg_token_ptr_at (arg, 0, kind, (location_t **) &loc);
1648 if (token_ptr == NULL)
1649 return NULL;
1651 return loc;
1654 /* Return the pointer to the INDEXth token of the macro argument ARG.
1655 KIND specifies the kind of token the macro argument ARG contains.
1656 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1657 of the virtual location of the returned token if the
1658 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1659 spelling location of the returned token. */
1660 static const cpp_token **
1661 arg_token_ptr_at (const macro_arg *arg, size_t index,
1662 enum macro_arg_token_kind kind,
1663 location_t **virt_location)
1665 const cpp_token **tokens_ptr = NULL;
1667 switch (kind)
1669 case MACRO_ARG_TOKEN_NORMAL:
1670 tokens_ptr = arg->first;
1671 break;
1672 case MACRO_ARG_TOKEN_STRINGIFIED:
1673 tokens_ptr = (const cpp_token **) &arg->stringified;
1674 break;
1675 case MACRO_ARG_TOKEN_EXPANDED:
1676 tokens_ptr = arg->expanded;
1677 break;
1680 if (tokens_ptr == NULL)
1681 /* This can happen for e.g, an empty token argument to a
1682 funtion-like macro. */
1683 return tokens_ptr;
1685 if (virt_location)
1687 if (kind == MACRO_ARG_TOKEN_NORMAL)
1688 *virt_location = &arg->virt_locs[index];
1689 else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1690 *virt_location = &arg->expanded_virt_locs[index];
1691 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1692 *virt_location =
1693 (location_t *) &tokens_ptr[index]->src_loc;
1695 return &tokens_ptr[index];
1698 /* Initialize an iterator so that it iterates over the tokens of a
1699 function-like macro argument. KIND is the kind of tokens we want
1700 ITER to iterate over. TOKEN_PTR points the first token ITER will
1701 iterate over. */
1702 static void
1703 macro_arg_token_iter_init (macro_arg_token_iter *iter,
1704 bool track_macro_exp_p,
1705 enum macro_arg_token_kind kind,
1706 const macro_arg *arg,
1707 const cpp_token **token_ptr)
1709 iter->track_macro_exp_p = track_macro_exp_p;
1710 iter->kind = kind;
1711 iter->token_ptr = token_ptr;
1712 /* Unconditionally initialize this so that the compiler doesn't warn
1713 about iter->location_ptr being possibly uninitialized later after
1714 this code has been inlined somewhere. */
1715 iter->location_ptr = NULL;
1716 if (track_macro_exp_p)
1717 iter->location_ptr = get_arg_token_location (arg, kind);
1718 #if CHECKING_P
1719 iter->num_forwards = 0;
1720 if (track_macro_exp_p
1721 && token_ptr != NULL
1722 && iter->location_ptr == NULL)
1723 abort ();
1724 #endif
1727 /* Move the iterator one token forward. Note that if IT was
1728 initialized on an argument that has a stringified token, moving it
1729 forward doesn't make sense as a stringified token is essentially one
1730 string. */
1731 static void
1732 macro_arg_token_iter_forward (macro_arg_token_iter *it)
1734 switch (it->kind)
1736 case MACRO_ARG_TOKEN_NORMAL:
1737 case MACRO_ARG_TOKEN_EXPANDED:
1738 it->token_ptr++;
1739 if (it->track_macro_exp_p)
1740 it->location_ptr++;
1741 break;
1742 case MACRO_ARG_TOKEN_STRINGIFIED:
1743 #if CHECKING_P
1744 if (it->num_forwards > 0)
1745 abort ();
1746 #endif
1747 break;
1750 #if CHECKING_P
1751 it->num_forwards++;
1752 #endif
1755 /* Return the token pointed to by the iterator. */
1756 static const cpp_token *
1757 macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1759 #if CHECKING_P
1760 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1761 && it->num_forwards > 0)
1762 abort ();
1763 #endif
1764 if (it->token_ptr == NULL)
1765 return NULL;
1766 return *it->token_ptr;
1769 /* Return the location of the token pointed to by the iterator.*/
1770 static location_t
1771 macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1773 #if CHECKING_P
1774 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1775 && it->num_forwards > 0)
1776 abort ();
1777 #endif
1778 if (it->track_macro_exp_p)
1779 return *it->location_ptr;
1780 else
1781 return (*it->token_ptr)->src_loc;
1784 /* Return the index of a token [resulting from macro expansion] inside
1785 the total list of tokens resulting from a given macro
1786 expansion. The index can be different depending on whether if we
1787 want each tokens resulting from function-like macro arguments
1788 expansion to have a different location or not.
1790 E.g, consider this function-like macro:
1792 #define M(x) x - 3
1794 Then consider us "calling" it (and thus expanding it) like:
1796 M(1+4)
1798 It will be expanded into:
1800 1+4-3
1802 Let's consider the case of the token '4'.
1804 Its index can be 2 (it's the third token of the set of tokens
1805 resulting from the expansion) or it can be 0 if we consider that
1806 all tokens resulting from the expansion of the argument "1+2" have
1807 the same index, which is 0. In this later case, the index of token
1808 '-' would then be 1 and the index of token '3' would be 2.
1810 The later case is useful to use less memory e.g, for the case of
1811 the user using the option -ftrack-macro-expansion=1.
1813 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1814 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1815 parameter (inside the macro replacement list) that corresponds to
1816 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1819 If we refer to the example above, for the '4' argument token,
1820 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1821 would be set to the token 'x', in the replacement list "x - 3" of
1822 macro M.
1824 This is a subroutine of replace_args. */
1825 inline static unsigned
1826 expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1827 const cpp_token *cur_replacement_token,
1828 unsigned absolute_token_index)
1830 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1831 return absolute_token_index;
1832 return cur_replacement_token - macro->exp.tokens;
1835 /* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG. */
1837 static void
1838 copy_paste_flag (cpp_reader *pfile, const cpp_token **paste_flag,
1839 const cpp_token *src)
1841 cpp_token *token = _cpp_temp_token (pfile);
1842 token->type = (*paste_flag)->type;
1843 token->val = (*paste_flag)->val;
1844 if (src->flags & PASTE_LEFT)
1845 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1846 else
1847 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1848 *paste_flag = token;
1851 /* True IFF the last token emitted into BUFF (if any) is PTR. */
1853 static bool
1854 last_token_is (_cpp_buff *buff, const cpp_token **ptr)
1856 return (ptr && tokens_buff_last_token_ptr (buff) == ptr);
1859 /* Replace the parameters in a function-like macro of NODE with the
1860 actual ARGS, and place the result in a newly pushed token context.
1861 Expand each argument before replacing, unless it is operated upon
1862 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1863 the expansion point of the macro. E.g, the location of the
1864 function-like macro invocation. */
1865 static void
1866 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1867 macro_arg *args, location_t expansion_point_loc)
1869 unsigned int i, total;
1870 const cpp_token *src, *limit;
1871 const cpp_token **first = NULL;
1872 macro_arg *arg;
1873 _cpp_buff *buff = NULL;
1874 location_t *virt_locs = NULL;
1875 unsigned int exp_count;
1876 const line_map_macro *map = NULL;
1877 int track_macro_exp;
1879 /* First, fully macro-expand arguments, calculating the number of
1880 tokens in the final expansion as we go. The ordering of the if
1881 statements below is subtle; we must handle stringification before
1882 pasting. */
1884 /* EXP_COUNT is the number of tokens in the macro replacement
1885 list. TOTAL is the number of tokens /after/ macro parameters
1886 have been replaced by their arguments. */
1887 exp_count = macro_real_token_count (macro);
1888 total = exp_count;
1889 limit = macro->exp.tokens + exp_count;
1891 for (src = macro->exp.tokens; src < limit; src++)
1892 if (src->type == CPP_MACRO_ARG)
1894 /* Leading and trailing padding tokens. */
1895 total += 2;
1896 /* Account for leading and padding tokens in exp_count too.
1897 This is going to be important later down this function,
1898 when we want to handle the case of (track_macro_exp <
1899 2). */
1900 exp_count += 2;
1902 /* We have an argument. If it is not being stringified or
1903 pasted it is macro-replaced before insertion. */
1904 arg = &args[src->val.macro_arg.arg_no - 1];
1906 if (src->flags & STRINGIFY_ARG)
1908 if (!arg->stringified)
1909 arg->stringified = stringify_arg (pfile, arg);
1911 else if ((src->flags & PASTE_LEFT)
1912 || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1913 total += arg->count - 1;
1914 else
1916 if (!arg->expanded)
1917 expand_arg (pfile, arg);
1918 total += arg->expanded_count - 1;
1922 /* When the compiler is called with the -ftrack-macro-expansion
1923 flag, we need to keep track of the location of each token that
1924 results from macro expansion.
1926 A token resulting from macro expansion is not a new token. It is
1927 simply the same token as the token coming from the macro
1928 definition. The new things that are allocated are the buffer
1929 that holds the tokens resulting from macro expansion and a new
1930 location that records many things like the locus of the expansion
1931 point as well as the original locus inside the definition of the
1932 macro. This location is called a virtual location.
1934 So the buffer BUFF holds a set of cpp_token*, and the buffer
1935 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1937 Both of these two buffers are going to be hung off of the macro
1938 context, when the latter is pushed. The memory allocated to
1939 store the tokens and their locations is going to be freed once
1940 the context of macro expansion is popped.
1942 As far as tokens are concerned, the memory overhead of
1943 -ftrack-macro-expansion is proportional to the number of
1944 macros that get expanded multiplied by sizeof (location_t).
1945 The good news is that extra memory gets freed when the macro
1946 context is freed, i.e shortly after the macro got expanded. */
1948 /* Is the -ftrack-macro-expansion flag in effect? */
1949 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1951 /* Now allocate memory space for tokens and locations resulting from
1952 the macro expansion, copy the tokens and replace the arguments.
1953 This memory must be freed when the context of the macro MACRO is
1954 popped. */
1955 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1957 first = (const cpp_token **) buff->base;
1959 /* Create a macro map to record the locations of the tokens that are
1960 involved in the expansion. Note that the expansion point is set
1961 to the location of the closing parenthesis. Otherwise, the
1962 subsequent map created for the first token that comes after the
1963 macro map might have a wrong line number. That would lead to
1964 tokens with wrong line numbers after the macro expansion. This
1965 adds up to the memory overhead of the -ftrack-macro-expansion
1966 flag; for every macro that is expanded, a "macro map" is
1967 created. */
1968 if (track_macro_exp)
1970 int num_macro_tokens = total;
1971 if (track_macro_exp < 2)
1972 /* Then the number of macro tokens won't take in account the
1973 fact that function-like macro arguments can expand to
1974 multiple tokens. This is to save memory at the expense of
1975 accuracy.
1977 Suppose we have #define SQUARE(A) A * A
1979 And then we do SQUARE(2+3)
1981 Then the tokens 2, +, 3, will have the same location,
1982 saying they come from the expansion of the argument A. */
1983 num_macro_tokens = exp_count;
1984 map = linemap_enter_macro (pfile->line_table, node,
1985 expansion_point_loc,
1986 num_macro_tokens);
1988 i = 0;
1989 vaopt_state vaopt_tracker (pfile, macro->variadic, &args[macro->paramc - 1]);
1990 const cpp_token **vaopt_start = NULL;
1991 for (src = macro->exp.tokens; src < limit; src++)
1993 unsigned int arg_tokens_count;
1994 macro_arg_token_iter from;
1995 const cpp_token **paste_flag = NULL;
1996 const cpp_token **tmp_token_ptr;
1998 /* __VA_OPT__ handling. */
1999 vaopt_state::update_type vostate = vaopt_tracker.update (src);
2000 if (vostate != vaopt_state::INCLUDE)
2002 if (vostate == vaopt_state::BEGIN)
2004 /* Padding on the left of __VA_OPT__ (unless RHS of ##). */
2005 if (src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
2007 const cpp_token *t = padding_token (pfile, src);
2008 unsigned index = expanded_token_index (pfile, macro, src, i);
2009 /* Allocate a virtual location for the padding token and
2010 append the token and its location to BUFF and
2011 VIRT_LOCS. */
2012 tokens_buff_add_token (buff, virt_locs, t,
2013 t->src_loc, t->src_loc,
2014 map, index);
2016 vaopt_start = tokens_buff_last_token_ptr (buff);
2018 else if (vostate == vaopt_state::END)
2020 const cpp_token **start = vaopt_start;
2021 vaopt_start = NULL;
2023 /* Remove any tail padding from inside the __VA_OPT__. */
2024 paste_flag = tokens_buff_last_token_ptr (buff);
2025 while (paste_flag && paste_flag != start
2026 && (*paste_flag)->type == CPP_PADDING)
2028 tokens_buff_remove_last_token (buff);
2029 paste_flag = tokens_buff_last_token_ptr (buff);
2032 if (src->flags & PASTE_LEFT)
2034 /* With a non-empty __VA_OPT__ on the LHS of ##, the last
2035 token should be flagged PASTE_LEFT. */
2036 if (paste_flag && (*paste_flag)->type != CPP_PADDING)
2037 copy_paste_flag (pfile, paste_flag, src);
2039 else
2041 /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or
2042 __VA_OPT__(c)__VA_OPT__(d). */
2043 const cpp_token *t = &pfile->avoid_paste;
2044 tokens_buff_add_token (buff, virt_locs,
2045 t, t->src_loc, t->src_loc,
2046 NULL, 0);
2049 continue;
2052 if (src->type != CPP_MACRO_ARG)
2054 /* Allocate a virtual location for token SRC, and add that
2055 token and its virtual location into the buffers BUFF and
2056 VIRT_LOCS. */
2057 unsigned index = expanded_token_index (pfile, macro, src, i);
2058 tokens_buff_add_token (buff, virt_locs, src,
2059 src->src_loc, src->src_loc,
2060 map, index);
2061 i += 1;
2062 continue;
2065 paste_flag = 0;
2066 arg = &args[src->val.macro_arg.arg_no - 1];
2067 /* SRC is a macro parameter that we need to replace with its
2068 corresponding argument. So at some point we'll need to
2069 iterate over the tokens of the macro argument and copy them
2070 into the "place" now holding the correspondig macro
2071 parameter. We are going to use the iterator type
2072 macro_argo_token_iter to handle that iterating. The 'if'
2073 below is to initialize the iterator depending on the type of
2074 tokens the macro argument has. It also does some adjustment
2075 related to padding tokens and some pasting corner cases. */
2076 if (src->flags & STRINGIFY_ARG)
2078 arg_tokens_count = 1;
2079 macro_arg_token_iter_init (&from,
2080 CPP_OPTION (pfile,
2081 track_macro_expansion),
2082 MACRO_ARG_TOKEN_STRINGIFIED,
2083 arg, &arg->stringified);
2085 else if (src->flags & PASTE_LEFT)
2087 arg_tokens_count = arg->count;
2088 macro_arg_token_iter_init (&from,
2089 CPP_OPTION (pfile,
2090 track_macro_expansion),
2091 MACRO_ARG_TOKEN_NORMAL,
2092 arg, arg->first);
2094 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
2096 int num_toks;
2097 arg_tokens_count = arg->count;
2098 macro_arg_token_iter_init (&from,
2099 CPP_OPTION (pfile,
2100 track_macro_expansion),
2101 MACRO_ARG_TOKEN_NORMAL,
2102 arg, arg->first);
2104 num_toks = tokens_buff_count (buff);
2106 if (num_toks != 0)
2108 /* So the current parameter token is pasted to the previous
2109 token in the replacement list. Let's look at what
2110 we have as previous and current arguments. */
2112 /* This is the previous argument's token ... */
2113 tmp_token_ptr = tokens_buff_last_token_ptr (buff);
2115 if ((*tmp_token_ptr)->type == CPP_COMMA
2116 && macro->variadic
2117 && src->val.macro_arg.arg_no == macro->paramc)
2119 /* ... which is a comma; and the current parameter
2120 is the last parameter of a variadic function-like
2121 macro. If the argument to the current last
2122 parameter is NULL, then swallow the comma,
2123 otherwise drop the paste flag. */
2124 if (macro_arg_token_iter_get_token (&from) == NULL)
2125 tokens_buff_remove_last_token (buff);
2126 else
2127 paste_flag = tmp_token_ptr;
2129 /* Remove the paste flag if the RHS is a placemarker, unless the
2130 previous emitted token is at the beginning of __VA_OPT__;
2131 placemarkers within __VA_OPT__ are ignored in that case. */
2132 else if (arg_tokens_count == 0
2133 && tmp_token_ptr != vaopt_start)
2134 paste_flag = tmp_token_ptr;
2137 else
2139 arg_tokens_count = arg->expanded_count;
2140 macro_arg_token_iter_init (&from,
2141 CPP_OPTION (pfile,
2142 track_macro_expansion),
2143 MACRO_ARG_TOKEN_EXPANDED,
2144 arg, arg->expanded);
2146 if (last_token_is (buff, vaopt_start))
2148 /* We're expanding an arg at the beginning of __VA_OPT__.
2149 Skip padding. */
2150 while (arg_tokens_count)
2152 const cpp_token *t = macro_arg_token_iter_get_token (&from);
2153 if (t->type != CPP_PADDING)
2154 break;
2155 macro_arg_token_iter_forward (&from);
2156 --arg_tokens_count;
2161 /* Padding on the left of an argument (unless RHS of ##). */
2162 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
2163 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)
2164 && !last_token_is (buff, vaopt_start))
2166 const cpp_token *t = padding_token (pfile, src);
2167 unsigned index = expanded_token_index (pfile, macro, src, i);
2168 /* Allocate a virtual location for the padding token and
2169 append the token and its location to BUFF and
2170 VIRT_LOCS. */
2171 tokens_buff_add_token (buff, virt_locs, t,
2172 t->src_loc, t->src_loc,
2173 map, index);
2176 if (arg_tokens_count)
2178 /* So now we've got the number of tokens that make up the
2179 argument that is going to replace the current parameter
2180 in the macro's replacement list. */
2181 unsigned int j;
2182 for (j = 0; j < arg_tokens_count; ++j)
2184 /* So if track_macro_exp is < 2, the user wants to
2185 save extra memory while tracking macro expansion
2186 locations. So in that case here is what we do:
2188 Suppose we have #define SQUARE(A) A * A
2190 And then we do SQUARE(2+3)
2192 Then the tokens 2, +, 3, will have the same location,
2193 saying they come from the expansion of the argument
2196 So that means we are going to ignore the COUNT tokens
2197 resulting from the expansion of the current macro
2198 argument. In other words all the ARG_TOKENS_COUNT tokens
2199 resulting from the expansion of the macro argument will
2200 have the index I. Normally, each of those tokens should
2201 have index I+J. */
2202 unsigned token_index = i;
2203 unsigned index;
2204 if (track_macro_exp > 1)
2205 token_index += j;
2207 index = expanded_token_index (pfile, macro, src, token_index);
2208 tokens_buff_add_token (buff, virt_locs,
2209 macro_arg_token_iter_get_token (&from),
2210 macro_arg_token_iter_get_location (&from),
2211 src->src_loc, map, index);
2212 macro_arg_token_iter_forward (&from);
2215 /* With a non-empty argument on the LHS of ##, the last
2216 token should be flagged PASTE_LEFT. */
2217 if (src->flags & PASTE_LEFT)
2218 paste_flag
2219 = (const cpp_token **) tokens_buff_last_token_ptr (buff);
2221 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
2222 && ! macro->syshdr && ! _cpp_in_system_header (pfile))
2224 if (CPP_OPTION (pfile, cplusplus))
2225 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2226 "invoking macro %s argument %d: "
2227 "empty macro arguments are undefined"
2228 " in ISO C++98",
2229 NODE_NAME (node), src->val.macro_arg.arg_no);
2230 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
2231 cpp_pedwarning (pfile,
2232 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2233 ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
2234 "invoking macro %s argument %d: "
2235 "empty macro arguments are undefined"
2236 " in ISO C90",
2237 NODE_NAME (node), src->val.macro_arg.arg_no);
2239 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
2240 && ! CPP_OPTION (pfile, cplusplus)
2241 && ! macro->syshdr && ! _cpp_in_system_header (pfile))
2242 cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
2243 "invoking macro %s argument %d: "
2244 "empty macro arguments are undefined"
2245 " in ISO C90",
2246 NODE_NAME (node), src->val.macro_arg.arg_no);
2248 /* Avoid paste on RHS (even case count == 0). */
2249 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)
2250 && !last_token_is (buff, vaopt_start))
2252 const cpp_token *t = &pfile->avoid_paste;
2253 tokens_buff_add_token (buff, virt_locs,
2254 t, t->src_loc, t->src_loc,
2255 NULL, 0);
2258 /* Add a new paste flag, or remove an unwanted one. */
2259 if (paste_flag)
2260 copy_paste_flag (pfile, paste_flag, src);
2262 i += arg_tokens_count;
2265 if (track_macro_exp)
2266 push_extended_tokens_context (pfile, node, buff, virt_locs, first,
2267 tokens_buff_count (buff));
2268 else
2269 push_ptoken_context (pfile, node, buff, first,
2270 tokens_buff_count (buff));
2272 num_macro_tokens_counter += tokens_buff_count (buff);
2275 /* Return a special padding token, with padding inherited from SOURCE. */
2276 static const cpp_token *
2277 padding_token (cpp_reader *pfile, const cpp_token *source)
2279 cpp_token *result = _cpp_temp_token (pfile);
2281 result->type = CPP_PADDING;
2283 /* Data in GCed data structures cannot be made const so far, so we
2284 need a cast here. */
2285 result->val.source = (cpp_token *) source;
2286 result->flags = 0;
2287 return result;
2290 /* Get a new uninitialized context. Create a new one if we cannot
2291 re-use an old one. */
2292 static cpp_context *
2293 next_context (cpp_reader *pfile)
2295 cpp_context *result = pfile->context->next;
2297 if (result == 0)
2299 result = XNEW (cpp_context);
2300 memset (result, 0, sizeof (cpp_context));
2301 result->prev = pfile->context;
2302 result->next = 0;
2303 pfile->context->next = result;
2306 pfile->context = result;
2307 return result;
2310 /* Push a list of pointers to tokens. */
2311 static void
2312 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
2313 const cpp_token **first, unsigned int count)
2315 cpp_context *context = next_context (pfile);
2317 context->tokens_kind = TOKENS_KIND_INDIRECT;
2318 context->c.macro = macro;
2319 context->buff = buff;
2320 FIRST (context).ptoken = first;
2321 LAST (context).ptoken = first + count;
2324 /* Push a list of tokens.
2326 A NULL macro means that we should continue the current macro
2327 expansion, in essence. That means that if we are currently in a
2328 macro expansion context, we'll make the new pfile->context refer to
2329 the current macro. */
2330 void
2331 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
2332 const cpp_token *first, unsigned int count)
2334 cpp_context *context;
2336 if (macro == NULL)
2337 macro = macro_of_context (pfile->context);
2339 context = next_context (pfile);
2340 context->tokens_kind = TOKENS_KIND_DIRECT;
2341 context->c.macro = macro;
2342 context->buff = NULL;
2343 FIRST (context).token = first;
2344 LAST (context).token = first + count;
2347 /* Build a context containing a list of tokens as well as their
2348 virtual locations and push it. TOKENS_BUFF is the buffer that
2349 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
2350 non-NULL, it means that the context owns it, meaning that
2351 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
2352 contains the virtual locations.
2354 A NULL macro means that we should continue the current macro
2355 expansion, in essence. That means that if we are currently in a
2356 macro expansion context, we'll make the new pfile->context refer to
2357 the current macro. */
2358 static void
2359 push_extended_tokens_context (cpp_reader *pfile,
2360 cpp_hashnode *macro,
2361 _cpp_buff *token_buff,
2362 location_t *virt_locs,
2363 const cpp_token **first,
2364 unsigned int count)
2366 cpp_context *context;
2367 macro_context *m;
2369 if (macro == NULL)
2370 macro = macro_of_context (pfile->context);
2372 context = next_context (pfile);
2373 context->tokens_kind = TOKENS_KIND_EXTENDED;
2374 context->buff = token_buff;
2376 m = XNEW (macro_context);
2377 m->macro_node = macro;
2378 m->virt_locs = virt_locs;
2379 m->cur_virt_loc = virt_locs;
2380 context->c.mc = m;
2381 FIRST (context).ptoken = first;
2382 LAST (context).ptoken = first + count;
2385 /* Push a traditional macro's replacement text. */
2386 void
2387 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
2388 const uchar *start, size_t len)
2390 cpp_context *context = next_context (pfile);
2392 context->tokens_kind = TOKENS_KIND_DIRECT;
2393 context->c.macro = macro;
2394 context->buff = NULL;
2395 CUR (context) = start;
2396 RLIMIT (context) = start + len;
2397 macro->flags |= NODE_DISABLED;
2400 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
2401 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
2402 non-null (which means that -ftrack-macro-expansion is on),
2403 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
2404 hold the virtual locations of the tokens resulting from macro
2405 expansion. */
2406 static _cpp_buff*
2407 tokens_buff_new (cpp_reader *pfile, size_t len,
2408 location_t **virt_locs)
2410 size_t tokens_size = len * sizeof (cpp_token *);
2411 size_t locs_size = len * sizeof (location_t);
2413 if (virt_locs != NULL)
2414 *virt_locs = XNEWVEC (location_t, locs_size);
2415 return _cpp_get_buff (pfile, tokens_size);
2418 /* Returns the number of tokens contained in a token buffer. The
2419 buffer holds a set of cpp_token*. */
2420 static size_t
2421 tokens_buff_count (_cpp_buff *buff)
2423 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
2426 /* Return a pointer to the last token contained in the token buffer
2427 BUFF. */
2428 static const cpp_token **
2429 tokens_buff_last_token_ptr (_cpp_buff *buff)
2431 if (BUFF_FRONT (buff) == buff->base)
2432 return NULL;
2433 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
2436 /* Remove the last token contained in the token buffer TOKENS_BUFF.
2437 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
2438 containing the virtual locations of the tokens in TOKENS_BUFF; in
2439 which case the function updates that buffer as well. */
2440 static inline void
2441 tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
2444 if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
2445 BUFF_FRONT (tokens_buff) =
2446 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
2449 /* Insert a token into the token buffer at the position pointed to by
2450 DEST. Note that the buffer is not enlarged so the previous token
2451 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
2452 means -ftrack-macro-expansion is effect; it then points to where to
2453 insert the virtual location of TOKEN. TOKEN is the token to
2454 insert. VIRT_LOC is the virtual location of the token, i.e, the
2455 location possibly encoding its locus across macro expansion. If
2456 TOKEN is an argument of a function-like macro (inside a macro
2457 replacement list), PARM_DEF_LOC is the spelling location of the
2458 macro parameter that TOKEN is replacing, in the replacement list of
2459 the macro. If TOKEN is not an argument of a function-like macro or
2460 if it doesn't come from a macro expansion, then VIRT_LOC can just
2461 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
2462 means TOKEN comes from a macro expansion and MAP is the macro map
2463 associated to the macro. MACRO_TOKEN_INDEX points to the index of
2464 the token in the macro map; it is not considered if MAP is NULL.
2466 Upon successful completion this function returns the a pointer to
2467 the position of the token coming right after the insertion
2468 point. */
2469 static inline const cpp_token **
2470 tokens_buff_put_token_to (const cpp_token **dest,
2471 location_t *virt_loc_dest,
2472 const cpp_token *token,
2473 location_t virt_loc,
2474 location_t parm_def_loc,
2475 const line_map_macro *map,
2476 unsigned int macro_token_index)
2478 location_t macro_loc = virt_loc;
2479 const cpp_token **result;
2481 if (virt_loc_dest)
2483 /* -ftrack-macro-expansion is on. */
2484 if (map)
2485 macro_loc = linemap_add_macro_token (map, macro_token_index,
2486 virt_loc, parm_def_loc);
2487 *virt_loc_dest = macro_loc;
2489 *dest = token;
2490 result = &dest[1];
2492 return result;
2495 /* Adds a token at the end of the tokens contained in BUFFER. Note
2496 that this function doesn't enlarge BUFFER when the number of tokens
2497 reaches BUFFER's size; it aborts in that situation.
2499 TOKEN is the token to append. VIRT_LOC is the virtual location of
2500 the token, i.e, the location possibly encoding its locus across
2501 macro expansion. If TOKEN is an argument of a function-like macro
2502 (inside a macro replacement list), PARM_DEF_LOC is the location of
2503 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2504 from a macro expansion, then VIRT_LOC can just be set to the same
2505 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2506 from a macro expansion and MAP is the macro map associated to the
2507 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2508 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2509 non-null, it means -ftrack-macro-expansion is on; in which case
2510 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2511 array, at the same index as the one of TOKEN in BUFFER. Upon
2512 successful completion this function returns the a pointer to the
2513 position of the token coming right after the insertion point. */
2514 static const cpp_token **
2515 tokens_buff_add_token (_cpp_buff *buffer,
2516 location_t *virt_locs,
2517 const cpp_token *token,
2518 location_t virt_loc,
2519 location_t parm_def_loc,
2520 const line_map_macro *map,
2521 unsigned int macro_token_index)
2523 const cpp_token **result;
2524 location_t *virt_loc_dest = NULL;
2525 unsigned token_index =
2526 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2528 /* Abort if we pass the end the buffer. */
2529 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2530 abort ();
2532 if (virt_locs != NULL)
2533 virt_loc_dest = &virt_locs[token_index];
2535 result =
2536 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2537 virt_loc_dest, token, virt_loc, parm_def_loc,
2538 map, macro_token_index);
2540 BUFF_FRONT (buffer) = (unsigned char *) result;
2541 return result;
2544 /* Allocate space for the function-like macro argument ARG to store
2545 the tokens resulting from the macro-expansion of the tokens that
2546 make up ARG itself. That space is allocated in ARG->expanded and
2547 needs to be freed using free. */
2548 static void
2549 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2551 gcc_checking_assert (arg->expanded == NULL
2552 && arg->expanded_virt_locs == NULL);
2554 arg->expanded = XNEWVEC (const cpp_token *, capacity);
2555 if (CPP_OPTION (pfile, track_macro_expansion))
2556 arg->expanded_virt_locs = XNEWVEC (location_t, capacity);
2560 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2561 tokens. */
2562 static void
2563 ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2564 size_t size, size_t *expanded_capacity)
2566 if (size <= *expanded_capacity)
2567 return;
2569 size *= 2;
2571 arg->expanded =
2572 XRESIZEVEC (const cpp_token *, arg->expanded, size);
2573 *expanded_capacity = size;
2575 if (CPP_OPTION (pfile, track_macro_expansion))
2577 if (arg->expanded_virt_locs == NULL)
2578 arg->expanded_virt_locs = XNEWVEC (location_t, size);
2579 else
2580 arg->expanded_virt_locs = XRESIZEVEC (location_t,
2581 arg->expanded_virt_locs,
2582 size);
2586 /* Expand an argument ARG before replacing parameters in a
2587 function-like macro. This works by pushing a context with the
2588 argument's tokens, and then expanding that into a temporary buffer
2589 as if it were a normal part of the token stream. collect_args()
2590 has terminated the argument's tokens with a CPP_EOF so that we know
2591 when we have fully expanded the argument. */
2592 static void
2593 expand_arg (cpp_reader *pfile, macro_arg *arg)
2595 size_t capacity;
2596 bool saved_warn_trad;
2597 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
2599 if (arg->count == 0
2600 || arg->expanded != NULL)
2601 return;
2603 /* Don't warn about funlike macros when pre-expanding. */
2604 saved_warn_trad = CPP_WTRADITIONAL (pfile);
2605 CPP_WTRADITIONAL (pfile) = 0;
2607 /* Loop, reading in the tokens of the argument. */
2608 capacity = 256;
2609 alloc_expanded_arg_mem (pfile, arg, capacity);
2611 if (track_macro_exp_p)
2612 push_extended_tokens_context (pfile, NULL, NULL,
2613 arg->virt_locs,
2614 arg->first,
2615 arg->count + 1);
2616 else
2617 push_ptoken_context (pfile, NULL, NULL,
2618 arg->first, arg->count + 1);
2620 for (;;)
2622 const cpp_token *token;
2623 location_t location;
2625 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2626 &capacity);
2628 token = cpp_get_token_1 (pfile, &location);
2630 if (token->type == CPP_EOF)
2631 break;
2633 set_arg_token (arg, token, location,
2634 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2635 CPP_OPTION (pfile, track_macro_expansion));
2636 arg->expanded_count++;
2639 _cpp_pop_context (pfile);
2641 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
2644 /* Returns the macro associated to the current context if we are in
2645 the context a macro expansion, NULL otherwise. */
2646 static cpp_hashnode*
2647 macro_of_context (cpp_context *context)
2649 if (context == NULL)
2650 return NULL;
2652 return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2653 ? context->c.mc->macro_node
2654 : context->c.macro;
2657 /* Return TRUE iff we are expanding a macro or are about to start
2658 expanding one. If we are effectively expanding a macro, the
2659 function macro_of_context returns a pointer to the macro being
2660 expanded. */
2661 static bool
2662 in_macro_expansion_p (cpp_reader *pfile)
2664 if (pfile == NULL)
2665 return false;
2667 return (pfile->about_to_expand_macro_p
2668 || macro_of_context (pfile->context));
2671 /* Pop the current context off the stack, re-enabling the macro if the
2672 context represented a macro's replacement list. Initially the
2673 context structure was not freed so that we can re-use it later, but
2674 now we do free it to reduce peak memory consumption. */
2675 void
2676 _cpp_pop_context (cpp_reader *pfile)
2678 cpp_context *context = pfile->context;
2680 /* We should not be popping the base context. */
2681 gcc_assert (context != &pfile->base_context);
2683 if (context->c.macro)
2685 cpp_hashnode *macro;
2686 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2688 macro_context *mc = context->c.mc;
2689 macro = mc->macro_node;
2690 /* If context->buff is set, it means the life time of tokens
2691 is bound to the life time of this context; so we must
2692 free the tokens; that means we must free the virtual
2693 locations of these tokens too. */
2694 if (context->buff && mc->virt_locs)
2696 free (mc->virt_locs);
2697 mc->virt_locs = NULL;
2699 free (mc);
2700 context->c.mc = NULL;
2702 else
2703 macro = context->c.macro;
2705 /* Beware that MACRO can be NULL in cases like when we are
2706 called from expand_arg. In those cases, a dummy context with
2707 tokens is pushed just for the purpose of walking them using
2708 cpp_get_token_1. In that case, no 'macro' field is set into
2709 the dummy context. */
2710 if (macro != NULL
2711 /* Several contiguous macro expansion contexts can be
2712 associated to the same macro; that means it's the same
2713 macro expansion that spans across all these (sub)
2714 contexts. So we should re-enable an expansion-disabled
2715 macro only when we are sure we are really out of that
2716 macro expansion. */
2717 && macro_of_context (context->prev) != macro)
2718 macro->flags &= ~NODE_DISABLED;
2720 if (macro == pfile->top_most_macro_node && context->prev == NULL)
2721 /* We are popping the context of the top-most macro node. */
2722 pfile->top_most_macro_node = NULL;
2725 if (context->buff)
2727 /* Decrease memory peak consumption by freeing the memory used
2728 by the context. */
2729 _cpp_free_buff (context->buff);
2732 pfile->context = context->prev;
2733 /* decrease peak memory consumption by feeing the context. */
2734 pfile->context->next = NULL;
2735 free (context);
2738 /* Return TRUE if we reached the end of the set of tokens stored in
2739 CONTEXT, FALSE otherwise. */
2740 static inline bool
2741 reached_end_of_context (cpp_context *context)
2743 if (context->tokens_kind == TOKENS_KIND_DIRECT)
2744 return FIRST (context).token == LAST (context).token;
2745 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2746 || context->tokens_kind == TOKENS_KIND_EXTENDED)
2747 return FIRST (context).ptoken == LAST (context).ptoken;
2748 else
2749 abort ();
2752 /* Consume the next token contained in the current context of PFILE,
2753 and return it in *TOKEN. It's "full location" is returned in
2754 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2755 means the location encoding the locus of the token across macro
2756 expansion; otherwise it's just is the "normal" location of the
2757 token which (*TOKEN)->src_loc. */
2758 static inline void
2759 consume_next_token_from_context (cpp_reader *pfile,
2760 const cpp_token ** token,
2761 location_t *location)
2763 cpp_context *c = pfile->context;
2765 if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2767 *token = FIRST (c).token;
2768 *location = (*token)->src_loc;
2769 FIRST (c).token++;
2771 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2773 *token = *FIRST (c).ptoken;
2774 *location = (*token)->src_loc;
2775 FIRST (c).ptoken++;
2777 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2779 macro_context *m = c->c.mc;
2780 *token = *FIRST (c).ptoken;
2781 if (m->virt_locs)
2783 *location = *m->cur_virt_loc;
2784 m->cur_virt_loc++;
2786 else
2787 *location = (*token)->src_loc;
2788 FIRST (c).ptoken++;
2790 else
2791 abort ();
2794 /* In the traditional mode of the preprocessor, if we are currently in
2795 a directive, the location of a token must be the location of the
2796 start of the directive line. This function returns the proper
2797 location if we are in the traditional mode, and just returns
2798 LOCATION otherwise. */
2800 static inline location_t
2801 maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, location_t location)
2803 if (CPP_OPTION (pfile, traditional))
2805 if (pfile->state.in_directive)
2806 return pfile->directive_line;
2808 return location;
2811 /* Routine to get a token as well as its location.
2813 Macro expansions and directives are transparently handled,
2814 including entering included files. Thus tokens are post-macro
2815 expansion, and after any intervening directives. External callers
2816 see CPP_EOF only at EOF. Internal callers also see it when meeting
2817 a directive inside a macro call, when at the end of a directive and
2818 state.in_directive is still 1, and at the end of argument
2819 pre-expansion.
2821 LOC is an out parameter; *LOC is set to the location "as expected
2822 by the user". Please read the comment of
2823 cpp_get_token_with_location to learn more about the meaning of this
2824 location. */
2825 static const cpp_token*
2826 cpp_get_token_1 (cpp_reader *pfile, location_t *location)
2828 const cpp_token *result;
2829 /* This token is a virtual token that either encodes a location
2830 related to macro expansion or a spelling location. */
2831 location_t virt_loc = 0;
2832 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2833 to functions that push macro contexts. So let's save it so that
2834 we can restore it when we are about to leave this routine. */
2835 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
2837 for (;;)
2839 cpp_hashnode *node;
2840 cpp_context *context = pfile->context;
2842 /* Context->prev == 0 <=> base context. */
2843 if (!context->prev)
2845 result = _cpp_lex_token (pfile);
2846 virt_loc = result->src_loc;
2848 else if (!reached_end_of_context (context))
2850 consume_next_token_from_context (pfile, &result,
2851 &virt_loc);
2852 if (result->flags & PASTE_LEFT)
2854 paste_all_tokens (pfile, result);
2855 if (pfile->state.in_directive)
2856 continue;
2857 result = padding_token (pfile, result);
2858 goto out;
2861 else
2863 if (pfile->context->c.macro)
2864 ++num_expanded_macros_counter;
2865 _cpp_pop_context (pfile);
2866 if (pfile->state.in_directive)
2867 continue;
2868 result = &pfile->avoid_paste;
2869 goto out;
2872 if (pfile->state.in_directive && result->type == CPP_COMMENT)
2873 continue;
2875 if (result->type != CPP_NAME)
2876 break;
2878 node = result->val.node.node;
2880 if (node->type == NT_VOID || (result->flags & NO_EXPAND))
2881 break;
2883 if (!(node->flags & NODE_USED)
2884 && node->type == NT_USER_MACRO
2885 && !node->value.macro
2886 && !cpp_get_deferred_macro (pfile, node, result->src_loc))
2887 break;
2889 if (!(node->flags & NODE_DISABLED))
2891 int ret = 0;
2892 /* If not in a macro context, and we're going to start an
2893 expansion, record the location and the top level macro
2894 about to be expanded. */
2895 if (!in_macro_expansion_p (pfile))
2897 pfile->invocation_location = result->src_loc;
2898 pfile->top_most_macro_node = node;
2900 if (pfile->state.prevent_expansion)
2901 break;
2903 /* Conditional macros require that a predicate be evaluated
2904 first. */
2905 if ((node->flags & NODE_CONDITIONAL) != 0)
2907 if (pfile->cb.macro_to_expand)
2909 bool whitespace_after;
2910 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2912 whitespace_after = (peek_tok->type == CPP_PADDING
2913 || (peek_tok->flags & PREV_WHITE));
2914 node = pfile->cb.macro_to_expand (pfile, result);
2915 if (node)
2916 ret = enter_macro_context (pfile, node, result, virt_loc);
2917 else if (whitespace_after)
2919 /* If macro_to_expand hook returned NULL and it
2920 ate some tokens, see if we don't need to add
2921 a padding token in between this and the
2922 next token. */
2923 peek_tok = cpp_peek_token (pfile, 0);
2924 if (peek_tok->type != CPP_PADDING
2925 && (peek_tok->flags & PREV_WHITE) == 0)
2926 _cpp_push_token_context (pfile, NULL,
2927 padding_token (pfile,
2928 peek_tok), 1);
2932 else
2933 ret = enter_macro_context (pfile, node, result, virt_loc);
2934 if (ret)
2936 if (pfile->state.in_directive || ret == 2)
2937 continue;
2938 result = padding_token (pfile, result);
2939 goto out;
2942 else
2944 /* Flag this token as always unexpandable. FIXME: move this
2945 to collect_args()?. */
2946 cpp_token *t = _cpp_temp_token (pfile);
2947 t->type = result->type;
2948 t->flags = result->flags | NO_EXPAND;
2949 t->val = result->val;
2950 result = t;
2953 break;
2956 out:
2957 if (location != NULL)
2959 if (virt_loc == 0)
2960 virt_loc = result->src_loc;
2961 *location = virt_loc;
2963 if (!CPP_OPTION (pfile, track_macro_expansion)
2964 && macro_of_context (pfile->context) != NULL)
2965 /* We are in a macro expansion context, are not tracking
2966 virtual location, but were asked to report the location
2967 of the expansion point of the macro being expanded. */
2968 *location = pfile->invocation_location;
2970 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2973 pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
2975 if (pfile->state.directive_file_token
2976 && !pfile->state.parsing_args
2977 && !(result->type == CPP_PADDING || result->type == CPP_COMMENT)
2978 && !(15 & --pfile->state.directive_file_token))
2980 /* Do header-name frobbery. Concatenate < ... > as approprate.
2981 Do header search if needed, and finally drop the outer <> or
2982 "". */
2983 pfile->state.angled_headers = false;
2985 /* Do angle-header reconstitution. Then do include searching.
2986 We'll always end up with a ""-quoted header-name in that
2987 case. If searching finds nothing, we emit a diagnostic and
2988 an empty string. */
2989 size_t len = 0;
2990 char *fname = NULL;
2992 cpp_token *tmp = _cpp_temp_token (pfile);
2993 *tmp = *result;
2995 tmp->type = CPP_HEADER_NAME;
2996 bool need_search = !pfile->state.directive_file_token;
2997 pfile->state.directive_file_token = 0;
2999 bool angle = result->type != CPP_STRING;
3000 if (result->type == CPP_HEADER_NAME
3001 || (result->type == CPP_STRING && result->val.str.text[0] != 'R'))
3003 len = result->val.str.len - 2;
3004 fname = XNEWVEC (char, len + 1);
3005 memcpy (fname, result->val.str.text + 1, len);
3006 fname[len] = 0;
3008 else if (result->type == CPP_LESS)
3009 fname = _cpp_bracket_include (pfile);
3011 if (fname)
3013 /* We have a header-name. Look it up. This will emit an
3014 unfound diagnostic. Canonicalize the found name. */
3015 const char *found = fname;
3017 if (need_search)
3019 found = _cpp_find_header_unit (pfile, fname, angle, tmp->src_loc);
3020 if (!found)
3021 found = "";
3022 len = strlen (found);
3024 /* Force a leading './' if it's not absolute. */
3025 bool dotme = (found[0] == '.' ? !IS_DIR_SEPARATOR (found[1])
3026 : found[0] && !IS_ABSOLUTE_PATH (found));
3028 if (BUFF_ROOM (pfile->u_buff) < len + 1 + dotme * 2)
3029 _cpp_extend_buff (pfile, &pfile->u_buff, len + 1 + dotme * 2);
3030 unsigned char *buf = BUFF_FRONT (pfile->u_buff);
3031 size_t pos = 0;
3033 if (dotme)
3035 buf[pos++] = '.';
3036 /* Apparently '/' is unconditional. */
3037 buf[pos++] = '/';
3039 memcpy (&buf[pos], found, len);
3040 pos += len;
3041 buf[pos] = 0;
3043 tmp->val.str.len = pos;
3044 tmp->val.str.text = buf;
3046 tmp->type = CPP_HEADER_NAME;
3047 XDELETEVEC (fname);
3049 result = tmp;
3053 return result;
3056 /* External routine to get a token. Also used nearly everywhere
3057 internally, except for places where we know we can safely call
3058 _cpp_lex_token directly, such as lexing a directive name.
3060 Macro expansions and directives are transparently handled,
3061 including entering included files. Thus tokens are post-macro
3062 expansion, and after any intervening directives. External callers
3063 see CPP_EOF only at EOF. Internal callers also see it when meeting
3064 a directive inside a macro call, when at the end of a directive and
3065 state.in_directive is still 1, and at the end of argument
3066 pre-expansion. */
3067 const cpp_token *
3068 cpp_get_token (cpp_reader *pfile)
3070 return cpp_get_token_1 (pfile, NULL);
3073 /* Like cpp_get_token, but also returns a virtual token location
3074 separate from the spelling location carried by the returned token.
3076 LOC is an out parameter; *LOC is set to the location "as expected
3077 by the user". This matters when a token results from macro
3078 expansion; in that case the token's spelling location indicates the
3079 locus of the token in the definition of the macro but *LOC
3080 virtually encodes all the other meaningful locuses associated to
3081 the token.
3083 What? virtual location? Yes, virtual location.
3085 If the token results from macro expansion and if macro expansion
3086 location tracking is enabled its virtual location encodes (at the
3087 same time):
3089 - the spelling location of the token
3091 - the locus of the macro expansion point
3093 - the locus of the point where the token got instantiated as part
3094 of the macro expansion process.
3096 You have to use the linemap API to get the locus you are interested
3097 in from a given virtual location.
3099 Note however that virtual locations are not necessarily ordered for
3100 relations '<' and '>'. One must use the function
3101 linemap_location_before_p instead of using the relational operator
3102 '<'.
3104 If macro expansion tracking is off and if the token results from
3105 macro expansion the virtual location is the expansion point of the
3106 macro that got expanded.
3108 When the token doesn't result from macro expansion, the virtual
3109 location is just the same thing as its spelling location. */
3111 const cpp_token *
3112 cpp_get_token_with_location (cpp_reader *pfile, location_t *loc)
3114 return cpp_get_token_1 (pfile, loc);
3117 /* Returns true if we're expanding an object-like macro that was
3118 defined in a system header. Just checks the macro at the top of
3119 the stack. Used for diagnostic suppression. */
3121 cpp_sys_macro_p (cpp_reader *pfile)
3123 cpp_hashnode *node = NULL;
3125 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
3126 node = pfile->context->c.mc->macro_node;
3127 else
3128 node = pfile->context->c.macro;
3130 return node && node->value.macro && node->value.macro->syshdr;
3133 /* Read each token in, until end of the current file. Directives are
3134 transparently processed. */
3135 void
3136 cpp_scan_nooutput (cpp_reader *pfile)
3138 /* Request a CPP_EOF token at the end of this file, rather than
3139 transparently continuing with the including file. */
3140 pfile->buffer->return_at_eof = true;
3142 pfile->state.discarding_output++;
3143 pfile->state.prevent_expansion++;
3145 if (CPP_OPTION (pfile, traditional))
3146 while (_cpp_read_logical_line_trad (pfile))
3148 else
3149 while (cpp_get_token (pfile)->type != CPP_EOF)
3152 pfile->state.discarding_output--;
3153 pfile->state.prevent_expansion--;
3156 /* Step back one or more tokens obtained from the lexer. */
3157 void
3158 _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
3160 pfile->lookaheads += count;
3161 while (count--)
3163 pfile->cur_token--;
3164 if (pfile->cur_token == pfile->cur_run->base
3165 /* Possible with -fpreprocessed and no leading #line. */
3166 && pfile->cur_run->prev != NULL)
3168 pfile->cur_run = pfile->cur_run->prev;
3169 pfile->cur_token = pfile->cur_run->limit;
3174 /* Step back one (or more) tokens. Can only step back more than 1 if
3175 they are from the lexer, and not from macro expansion. */
3176 void
3177 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
3179 if (pfile->context->prev == NULL)
3180 _cpp_backup_tokens_direct (pfile, count);
3181 else
3183 if (count != 1)
3184 abort ();
3185 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
3186 FIRST (pfile->context).token--;
3187 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
3188 FIRST (pfile->context).ptoken--;
3189 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
3191 FIRST (pfile->context).ptoken--;
3192 if (pfile->context->c.macro)
3194 macro_context *m = pfile->context->c.mc;
3195 m->cur_virt_loc--;
3196 gcc_checking_assert (m->cur_virt_loc >= m->virt_locs);
3198 else
3199 abort ();
3201 else
3202 abort ();
3206 /* #define directive parsing and handling. */
3208 /* Returns true if a macro redefinition warning is required. */
3209 static bool
3210 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
3211 const cpp_macro *macro2)
3213 /* Some redefinitions need to be warned about regardless. */
3214 if (node->flags & NODE_WARN)
3215 return true;
3217 /* Suppress warnings for builtins that lack the NODE_WARN flag,
3218 unless Wbuiltin-macro-redefined. */
3219 if (cpp_builtin_macro_p (node))
3220 return CPP_OPTION (pfile, warn_builtin_macro_redefined);
3222 /* Redefinitions of conditional (context-sensitive) macros, on
3223 the other hand, must be allowed silently. */
3224 if (node->flags & NODE_CONDITIONAL)
3225 return false;
3227 if (cpp_macro *macro1 = get_deferred_or_lazy_macro (pfile, node, macro2->line))
3228 return cpp_compare_macros (macro1, macro2);
3229 return false;
3232 /* Return TRUE if MACRO1 and MACRO2 differ. */
3234 bool
3235 cpp_compare_macros (const cpp_macro *macro1, const cpp_macro *macro2)
3237 /* Redefinition of a macro is allowed if and only if the old and new
3238 definitions are the same. (6.10.3 paragraph 2). */
3240 /* Don't check count here as it can be different in valid
3241 traditional redefinitions with just whitespace differences. */
3242 if (macro1->paramc != macro2->paramc
3243 || macro1->fun_like != macro2->fun_like
3244 || macro1->variadic != macro2->variadic)
3245 return true;
3247 /* Check parameter spellings. */
3248 for (unsigned i = macro1->paramc; i--; )
3249 if (macro1->parm.params[i] != macro2->parm.params[i])
3250 return true;
3252 /* Check the replacement text or tokens. */
3253 if (macro1->kind == cmk_traditional)
3254 return _cpp_expansions_different_trad (macro1, macro2);
3256 if (macro1->count != macro2->count)
3257 return true;
3259 for (unsigned i= macro1->count; i--; )
3260 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
3261 return true;
3263 return false;
3266 /* Free the definition of hashnode H. */
3267 void
3268 _cpp_free_definition (cpp_hashnode *h)
3270 /* Macros and assertions no longer have anything to free. */
3271 h->type = NT_VOID;
3272 h->value.answers = NULL;
3273 h->flags &= ~(NODE_DISABLED | NODE_USED);
3276 /* Save parameter NODE (spelling SPELLING) to the parameter list of
3277 macro MACRO. Returns true on success, false on failure. */
3278 bool
3279 _cpp_save_parameter (cpp_reader *pfile, unsigned n, cpp_hashnode *node,
3280 cpp_hashnode *spelling)
3282 /* Constraint 6.10.3.6 - duplicate parameter names. */
3283 if (node->type == NT_MACRO_ARG)
3285 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
3286 NODE_NAME (node));
3287 return false;
3290 unsigned len = (n + 1) * sizeof (struct macro_arg_saved_data);
3291 if (len > pfile->macro_buffer_len)
3293 pfile->macro_buffer
3294 = XRESIZEVEC (unsigned char, pfile->macro_buffer, len);
3295 pfile->macro_buffer_len = len;
3298 macro_arg_saved_data *saved = (macro_arg_saved_data *)pfile->macro_buffer;
3299 saved[n].canonical_node = node;
3300 saved[n].value = node->value;
3301 saved[n].type = node->type;
3303 void *base = _cpp_reserve_room (pfile, n * sizeof (cpp_hashnode *),
3304 sizeof (cpp_hashnode *));
3305 ((cpp_hashnode **)base)[n] = spelling;
3307 /* Morph into a macro arg. */
3308 node->type = NT_MACRO_ARG;
3309 /* Index is 1 based. */
3310 node->value.arg_index = n + 1;
3312 return true;
3315 /* Restore the parameters to their previous state. */
3316 void
3317 _cpp_unsave_parameters (cpp_reader *pfile, unsigned n)
3319 /* Clear the fast argument lookup indices. */
3320 while (n--)
3322 struct macro_arg_saved_data *save =
3323 &((struct macro_arg_saved_data *) pfile->macro_buffer)[n];
3325 struct cpp_hashnode *node = save->canonical_node;
3326 node->type = save->type;
3327 node->value = save->value;
3331 /* Check the syntax of the parameters in a MACRO definition. Return
3332 false on failure. Set *N_PTR and *VARADIC_PTR as appropriate.
3333 '(' ')'
3334 '(' parm-list ',' last-parm ')'
3335 '(' last-parm ')'
3336 parm-list: name
3337 | parm-list, name
3338 last-parm: name
3339 | name '...'
3340 | '...'
3343 static bool
3344 parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *varadic_ptr)
3346 unsigned nparms = 0;
3347 bool ok = false;
3349 for (bool prev_ident = false;;)
3351 const cpp_token *token = _cpp_lex_token (pfile);
3353 switch (token->type)
3355 case CPP_COMMENT:
3356 /* Allow/ignore comments in parameter lists if we are
3357 preserving comments in macro expansions. */
3358 if (!CPP_OPTION (pfile, discard_comments_in_macro_exp))
3359 break;
3361 /* FALLTHRU */
3362 default:
3363 bad:
3365 const char *const msgs[5] =
3367 N_("expected parameter name, found \"%s\""),
3368 N_("expected ',' or ')', found \"%s\""),
3369 N_("expected parameter name before end of line"),
3370 N_("expected ')' before end of line"),
3371 N_("expected ')' after \"...\"")
3373 unsigned ix = prev_ident;
3374 const unsigned char *as_text = NULL;
3375 if (*varadic_ptr)
3376 ix = 4;
3377 else if (token->type == CPP_EOF)
3378 ix += 2;
3379 else
3380 as_text = cpp_token_as_text (pfile, token);
3381 cpp_error (pfile, CPP_DL_ERROR, msgs[ix], as_text);
3383 goto out;
3385 case CPP_NAME:
3386 if (prev_ident || *varadic_ptr)
3387 goto bad;
3388 prev_ident = true;
3390 if (!_cpp_save_parameter (pfile, nparms, token->val.node.node,
3391 token->val.node.spelling))
3392 goto out;
3393 nparms++;
3394 break;
3396 case CPP_CLOSE_PAREN:
3397 if (prev_ident || !nparms || *varadic_ptr)
3399 ok = true;
3400 goto out;
3403 /* FALLTHRU */
3404 case CPP_COMMA:
3405 if (!prev_ident || *varadic_ptr)
3406 goto bad;
3407 prev_ident = false;
3408 break;
3410 case CPP_ELLIPSIS:
3411 if (*varadic_ptr)
3412 goto bad;
3413 *varadic_ptr = true;
3414 if (!prev_ident)
3416 /* An ISO bare ellipsis. */
3417 _cpp_save_parameter (pfile, nparms,
3418 pfile->spec_nodes.n__VA_ARGS__,
3419 pfile->spec_nodes.n__VA_ARGS__);
3420 nparms++;
3421 pfile->state.va_args_ok = 1;
3422 if (! CPP_OPTION (pfile, c99)
3423 && CPP_OPTION (pfile, cpp_pedantic)
3424 && CPP_OPTION (pfile, warn_variadic_macros))
3425 cpp_pedwarning
3426 (pfile, CPP_W_VARIADIC_MACROS,
3427 CPP_OPTION (pfile, cplusplus)
3428 ? N_("anonymous variadic macros were introduced in C++11")
3429 : N_("anonymous variadic macros were introduced in C99"));
3430 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
3431 && ! CPP_OPTION (pfile, cplusplus))
3432 cpp_error (pfile, CPP_DL_WARNING,
3433 "anonymous variadic macros were introduced in C99");
3435 else if (CPP_OPTION (pfile, cpp_pedantic)
3436 && CPP_OPTION (pfile, warn_variadic_macros))
3437 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
3438 CPP_OPTION (pfile, cplusplus)
3439 ? N_("ISO C++ does not permit named variadic macros")
3440 : N_("ISO C does not permit named variadic macros"));
3441 break;
3445 out:
3446 *n_ptr = nparms;
3448 return ok;
3451 /* Lex a token from the expansion of MACRO, but mark parameters as we
3452 find them and warn of traditional stringification. */
3453 static cpp_macro *
3454 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
3456 macro = (cpp_macro *)_cpp_reserve_room (pfile,
3457 sizeof (cpp_macro) - sizeof (cpp_token)
3458 + macro->count * sizeof (cpp_token),
3459 sizeof (cpp_token));
3460 cpp_token *saved_cur_token = pfile->cur_token;
3461 pfile->cur_token = &macro->exp.tokens[macro->count];
3462 cpp_token *token = _cpp_lex_direct (pfile);
3463 pfile->cur_token = saved_cur_token;
3465 /* Is this a parameter? */
3466 if (token->type == CPP_NAME && token->val.node.node->type == NT_MACRO_ARG)
3468 /* Morph into a parameter reference. */
3469 cpp_hashnode *spelling = token->val.node.spelling;
3470 token->type = CPP_MACRO_ARG;
3471 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
3472 token->val.macro_arg.spelling = spelling;
3474 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
3475 && (token->type == CPP_STRING || token->type == CPP_CHAR))
3476 check_trad_stringification (pfile, macro, &token->val.str);
3478 return macro;
3481 static cpp_macro *
3482 create_iso_definition (cpp_reader *pfile)
3484 bool following_paste_op = false;
3485 const char *paste_op_error_msg =
3486 N_("'##' cannot appear at either end of a macro expansion");
3487 unsigned int num_extra_tokens = 0;
3488 unsigned nparms = 0;
3489 cpp_hashnode **params = NULL;
3490 bool varadic = false;
3491 bool ok = false;
3492 cpp_macro *macro = NULL;
3494 /* Look at the first token, to see if this is a function-like
3495 macro. */
3496 cpp_token first;
3497 cpp_token *saved_cur_token = pfile->cur_token;
3498 pfile->cur_token = &first;
3499 cpp_token *token = _cpp_lex_direct (pfile);
3500 pfile->cur_token = saved_cur_token;
3502 if (token->flags & PREV_WHITE)
3503 /* Preceeded by space, must be part of expansion. */;
3504 else if (token->type == CPP_OPEN_PAREN)
3506 /* An open-paren, get a parameter list. */
3507 if (!parse_params (pfile, &nparms, &varadic))
3508 goto out;
3510 params = (cpp_hashnode **)_cpp_commit_buff
3511 (pfile, sizeof (cpp_hashnode *) * nparms);
3512 token = NULL;
3514 else if (token->type != CPP_EOF
3515 && !(token->type == CPP_COMMENT
3516 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)))
3518 /* While ISO C99 requires whitespace before replacement text
3519 in a macro definition, ISO C90 with TC1 allows characters
3520 from the basic source character set there. */
3521 if (CPP_OPTION (pfile, c99))
3522 cpp_error (pfile, CPP_DL_PEDWARN,
3523 CPP_OPTION (pfile, cplusplus)
3524 ? N_("ISO C++11 requires whitespace after the macro name")
3525 : N_("ISO C99 requires whitespace after the macro name"));
3526 else
3528 enum cpp_diagnostic_level warntype = CPP_DL_WARNING;
3529 switch (token->type)
3531 case CPP_ATSIGN:
3532 case CPP_AT_NAME:
3533 case CPP_OBJC_STRING:
3534 /* '@' is not in basic character set. */
3535 warntype = CPP_DL_PEDWARN;
3536 break;
3537 case CPP_OTHER:
3538 /* Basic character set sans letters, digits and _. */
3539 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
3540 token->val.str.text[0]) == NULL)
3541 warntype = CPP_DL_PEDWARN;
3542 break;
3543 default:
3544 /* All other tokens start with a character from basic
3545 character set. */
3546 break;
3548 cpp_error (pfile, warntype,
3549 "missing whitespace after the macro name");
3553 macro = _cpp_new_macro (pfile, cmk_macro,
3554 _cpp_reserve_room (pfile, 0, sizeof (cpp_macro)));
3556 if (!token)
3558 macro->variadic = varadic;
3559 macro->paramc = nparms;
3560 macro->parm.params = params;
3561 macro->fun_like = true;
3563 else
3565 /* Preserve the token we peeked, there is already a single slot for it. */
3566 macro->exp.tokens[0] = *token;
3567 token = &macro->exp.tokens[0];
3568 macro->count = 1;
3571 for (vaopt_state vaopt_tracker (pfile, macro->variadic, NULL);; token = NULL)
3573 if (!token)
3575 macro = lex_expansion_token (pfile, macro);
3576 token = &macro->exp.tokens[macro->count++];
3579 /* Check the stringifying # constraint 6.10.3.2.1 of
3580 function-like macros when lexing the subsequent token. */
3581 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
3583 if (token->type == CPP_MACRO_ARG)
3585 if (token->flags & PREV_WHITE)
3586 token->flags |= SP_PREV_WHITE;
3587 if (token[-1].flags & DIGRAPH)
3588 token->flags |= SP_DIGRAPH;
3589 token->flags &= ~PREV_WHITE;
3590 token->flags |= STRINGIFY_ARG;
3591 token->flags |= token[-1].flags & PREV_WHITE;
3592 token[-1] = token[0];
3593 macro->count--;
3595 /* Let assembler get away with murder. */
3596 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
3598 cpp_error (pfile, CPP_DL_ERROR,
3599 "'#' is not followed by a macro parameter");
3600 goto out;
3604 if (token->type == CPP_EOF)
3606 /* Paste operator constraint 6.10.3.3.1:
3607 Token-paste ##, can appear in both object-like and
3608 function-like macros, but not at the end. */
3609 if (following_paste_op)
3611 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3612 goto out;
3614 if (!vaopt_tracker.completed ())
3615 goto out;
3616 break;
3619 /* Paste operator constraint 6.10.3.3.1. */
3620 if (token->type == CPP_PASTE)
3622 /* Token-paste ##, can appear in both object-like and
3623 function-like macros, but not at the beginning. */
3624 if (macro->count == 1)
3626 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
3627 goto out;
3630 if (following_paste_op)
3632 /* Consecutive paste operators. This one will be moved
3633 to the end. */
3634 num_extra_tokens++;
3635 token->val.token_no = macro->count - 1;
3637 else
3639 /* Drop the paste operator. */
3640 --macro->count;
3641 token[-1].flags |= PASTE_LEFT;
3642 if (token->flags & DIGRAPH)
3643 token[-1].flags |= SP_DIGRAPH;
3644 if (token->flags & PREV_WHITE)
3645 token[-1].flags |= SP_PREV_WHITE;
3647 following_paste_op = true;
3649 else
3650 following_paste_op = false;
3652 if (vaopt_tracker.update (token) == vaopt_state::ERROR)
3653 goto out;
3656 /* We're committed to winning now. */
3657 ok = true;
3659 /* Don't count the CPP_EOF. */
3660 macro->count--;
3662 macro = (cpp_macro *)_cpp_commit_buff
3663 (pfile, sizeof (cpp_macro) - sizeof (cpp_token)
3664 + sizeof (cpp_token) * macro->count);
3666 /* Clear whitespace on first token. */
3667 if (macro->count)
3668 macro->exp.tokens[0].flags &= ~PREV_WHITE;
3670 if (num_extra_tokens)
3672 /* Place second and subsequent ## or %:%: tokens in sequences of
3673 consecutive such tokens at the end of the list to preserve
3674 information about where they appear, how they are spelt and
3675 whether they are preceded by whitespace without otherwise
3676 interfering with macro expansion. Remember, this is
3677 extremely rare, so efficiency is not a priority. */
3678 cpp_token *temp = (cpp_token *)_cpp_reserve_room
3679 (pfile, 0, num_extra_tokens * sizeof (cpp_token));
3680 unsigned extra_ix = 0, norm_ix = 0;
3681 cpp_token *exp = macro->exp.tokens;
3682 for (unsigned ix = 0; ix != macro->count; ix++)
3683 if (exp[ix].type == CPP_PASTE)
3684 temp[extra_ix++] = exp[ix];
3685 else
3686 exp[norm_ix++] = exp[ix];
3687 memcpy (&exp[norm_ix], temp, num_extra_tokens * sizeof (cpp_token));
3689 /* Record there are extra tokens. */
3690 macro->extra_tokens = 1;
3693 out:
3694 pfile->state.va_args_ok = 0;
3695 _cpp_unsave_parameters (pfile, nparms);
3697 return ok ? macro : NULL;
3700 cpp_macro *
3701 _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement)
3703 cpp_macro *macro = (cpp_macro *) placement;
3705 /* Zero init all the fields. This'll tell the compiler know all the
3706 following inits are writing a virgin object. */
3707 memset (macro, 0, offsetof (cpp_macro, exp));
3709 macro->line = pfile->directive_line;
3710 macro->parm.params = 0;
3711 macro->lazy = 0;
3712 macro->paramc = 0;
3713 macro->variadic = 0;
3714 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
3715 macro->count = 0;
3716 macro->fun_like = 0;
3717 macro->imported_p = false;
3718 macro->extra_tokens = 0;
3719 /* To suppress some diagnostics. */
3720 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
3722 macro->kind = kind;
3724 return macro;
3727 /* Parse a macro and save its expansion. Returns nonzero on success. */
3728 bool
3729 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3731 cpp_macro *macro;
3733 if (CPP_OPTION (pfile, traditional))
3734 macro = _cpp_create_trad_definition (pfile);
3735 else
3736 macro = create_iso_definition (pfile);
3738 if (!macro)
3739 return false;
3741 if (cpp_macro_p (node))
3743 if (CPP_OPTION (pfile, warn_unused_macros))
3744 _cpp_warn_if_unused_macro (pfile, node, NULL);
3746 if (warn_of_redefinition (pfile, node, macro))
3748 const enum cpp_warning_reason reason
3749 = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN))
3750 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3752 bool warned =
3753 cpp_pedwarning_with_line (pfile, reason,
3754 pfile->directive_line, 0,
3755 "\"%s\" redefined", NODE_NAME (node));
3757 if (warned && cpp_user_macro_p (node))
3758 cpp_error_with_line (pfile, CPP_DL_NOTE,
3759 node->value.macro->line, 0,
3760 "this is the location of the previous definition");
3762 _cpp_free_definition (node);
3765 /* Enter definition in hash table. */
3766 node->type = NT_USER_MACRO;
3767 node->value.macro = macro;
3768 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3769 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3770 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3771 in the C standard, as something that one must use in C++.
3772 However DR#593 and C++11 indicate that they play no role in C++.
3773 We special-case them anyway. */
3774 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3775 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
3776 node->flags |= NODE_WARN;
3778 /* If user defines one of the conditional macros, remove the
3779 conditional flag */
3780 node->flags &= ~NODE_CONDITIONAL;
3782 return true;
3785 extern void
3786 cpp_define_lazily (cpp_reader *pfile, cpp_hashnode *node, unsigned num)
3788 cpp_macro *macro = node->value.macro;
3790 gcc_checking_assert (pfile->cb.user_lazy_macro && macro && num < UCHAR_MAX);
3792 macro->lazy = num + 1;
3795 /* NODE is a deferred macro, resolve it, returning the definition
3796 (which may be NULL). */
3797 cpp_macro *
3798 cpp_get_deferred_macro (cpp_reader *pfile, cpp_hashnode *node,
3799 location_t loc)
3801 gcc_checking_assert (node->type == NT_USER_MACRO);
3803 node->value.macro = pfile->cb.user_deferred_macro (pfile, loc, node);
3805 if (!node->value.macro)
3806 node->type = NT_VOID;
3808 return node->value.macro;
3811 static cpp_macro *
3812 get_deferred_or_lazy_macro (cpp_reader *pfile, cpp_hashnode *node,
3813 location_t loc)
3815 cpp_macro *macro = node->value.macro;
3816 if (!macro)
3818 macro = cpp_get_deferred_macro (pfile, node, loc);
3819 gcc_checking_assert (!macro || !macro->lazy);
3821 else if (macro->lazy)
3823 pfile->cb.user_lazy_macro (pfile, macro, macro->lazy - 1);
3824 macro->lazy = 0;
3827 return macro;
3830 /* Notify the use of NODE in a macro-aware context (i.e. expanding it,
3831 or testing its existance). Also applies any lazy definition.
3832 Return FALSE if the macro isn't really there. */
3834 extern bool
3835 _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node,
3836 location_t loc)
3838 node->flags |= NODE_USED;
3839 switch (node->type)
3841 case NT_USER_MACRO:
3842 if (!get_deferred_or_lazy_macro (pfile, node, loc))
3843 return false;
3844 /* FALLTHROUGH. */
3846 case NT_BUILTIN_MACRO:
3847 if (pfile->cb.used_define)
3848 pfile->cb.used_define (pfile, loc, node);
3849 break;
3851 case NT_VOID:
3852 if (pfile->cb.used_undef)
3853 pfile->cb.used_undef (pfile, loc, node);
3854 break;
3856 default:
3857 abort ();
3860 return true;
3863 /* Warn if a token in STRING matches one of a function-like MACRO's
3864 parameters. */
3865 static void
3866 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3867 const cpp_string *string)
3869 unsigned int i, len;
3870 const uchar *p, *q, *limit;
3872 /* Loop over the string. */
3873 limit = string->text + string->len - 1;
3874 for (p = string->text + 1; p < limit; p = q)
3876 /* Find the start of an identifier. */
3877 while (p < limit && !is_idstart (*p))
3878 p++;
3880 /* Find the end of the identifier. */
3881 q = p;
3882 while (q < limit && is_idchar (*q))
3883 q++;
3885 len = q - p;
3887 /* Loop over the function macro arguments to see if the
3888 identifier inside the string matches one of them. */
3889 for (i = 0; i < macro->paramc; i++)
3891 const cpp_hashnode *node = macro->parm.params[i];
3893 if (NODE_LEN (node) == len
3894 && !memcmp (p, NODE_NAME (node), len))
3896 cpp_warning (pfile, CPP_W_TRADITIONAL,
3897 "macro argument \"%s\" would be stringified in traditional C",
3898 NODE_NAME (node));
3899 break;
3905 /* Returns the name, arguments and expansion of a macro, in a format
3906 suitable to be read back in again, and therefore also for DWARF 2
3907 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3908 Caller is expected to generate the "#define" bit if needed. The
3909 returned text is temporary, and automatically freed later. */
3910 const unsigned char *
3911 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3913 gcc_checking_assert (cpp_user_macro_p (node));
3915 if (const cpp_macro *macro = get_deferred_or_lazy_macro (pfile, node, 0))
3916 return cpp_macro_definition (pfile, node, macro);
3917 return NULL;
3920 const unsigned char *
3921 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node,
3922 const cpp_macro *macro)
3924 unsigned int i, len;
3925 unsigned char *buffer;
3927 /* Calculate length. */
3928 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
3929 if (macro->fun_like)
3931 len += 4; /* "()" plus possible final ".." of named
3932 varargs (we have + 1 below). */
3933 for (i = 0; i < macro->paramc; i++)
3934 len += NODE_LEN (macro->parm.params[i]) + 1; /* "," */
3937 /* This should match below where we fill in the buffer. */
3938 if (CPP_OPTION (pfile, traditional))
3939 len += _cpp_replacement_text_len (macro);
3940 else
3942 unsigned int count = macro_real_token_count (macro);
3943 for (i = 0; i < count; i++)
3945 const cpp_token *token = &macro->exp.tokens[i];
3947 if (token->type == CPP_MACRO_ARG)
3948 len += NODE_LEN (token->val.macro_arg.spelling);
3949 else
3950 len += cpp_token_len (token);
3952 if (token->flags & STRINGIFY_ARG)
3953 len++; /* "#" */
3954 if (token->flags & PASTE_LEFT)
3955 len += 3; /* " ##" */
3956 if (token->flags & PREV_WHITE)
3957 len++; /* " " */
3961 if (len > pfile->macro_buffer_len)
3963 pfile->macro_buffer = XRESIZEVEC (unsigned char,
3964 pfile->macro_buffer, len);
3965 pfile->macro_buffer_len = len;
3968 /* Fill in the buffer. Start with the macro name. */
3969 buffer = pfile->macro_buffer;
3970 buffer = _cpp_spell_ident_ucns (buffer, node);
3972 /* Parameter names. */
3973 if (macro->fun_like)
3975 *buffer++ = '(';
3976 for (i = 0; i < macro->paramc; i++)
3978 cpp_hashnode *param = macro->parm.params[i];
3980 if (param != pfile->spec_nodes.n__VA_ARGS__)
3982 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3983 buffer += NODE_LEN (param);
3986 if (i + 1 < macro->paramc)
3987 /* Don't emit a space after the comma here; we're trying
3988 to emit a Dwarf-friendly definition, and the Dwarf spec
3989 forbids spaces in the argument list. */
3990 *buffer++ = ',';
3991 else if (macro->variadic)
3992 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3994 *buffer++ = ')';
3997 /* The Dwarf spec requires a space after the macro name, even if the
3998 definition is the empty string. */
3999 *buffer++ = ' ';
4001 if (CPP_OPTION (pfile, traditional))
4002 buffer = _cpp_copy_replacement_text (macro, buffer);
4003 else if (macro->count)
4004 /* Expansion tokens. */
4006 unsigned int count = macro_real_token_count (macro);
4007 for (i = 0; i < count; i++)
4009 const cpp_token *token = &macro->exp.tokens[i];
4011 if (token->flags & PREV_WHITE)
4012 *buffer++ = ' ';
4013 if (token->flags & STRINGIFY_ARG)
4014 *buffer++ = '#';
4016 if (token->type == CPP_MACRO_ARG)
4018 memcpy (buffer,
4019 NODE_NAME (token->val.macro_arg.spelling),
4020 NODE_LEN (token->val.macro_arg.spelling));
4021 buffer += NODE_LEN (token->val.macro_arg.spelling);
4023 else
4024 buffer = cpp_spell_token (pfile, token, buffer, true);
4026 if (token->flags & PASTE_LEFT)
4028 *buffer++ = ' ';
4029 *buffer++ = '#';
4030 *buffer++ = '#';
4031 /* Next has PREV_WHITE; see _cpp_create_definition. */
4036 *buffer = '\0';
4037 return pfile->macro_buffer;