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