1*404b540aSrobert /* Part of CPP library. (Macro and #define handling.)
2*404b540aSrobert Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3*404b540aSrobert 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert Written by Per Bothner, 1994.
5*404b540aSrobert Based on CCCP program by Paul Rubin, June 1986
6*404b540aSrobert Adapted to ANSI C, Richard Stallman, Jan 1987
7*404b540aSrobert
8*404b540aSrobert This program is free software; you can redistribute it and/or modify it
9*404b540aSrobert under the terms of the GNU General Public License as published by the
10*404b540aSrobert Free Software Foundation; either version 2, or (at your option) any
11*404b540aSrobert later version.
12*404b540aSrobert
13*404b540aSrobert This program is distributed in the hope that it will be useful,
14*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
15*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*404b540aSrobert GNU General Public License for more details.
17*404b540aSrobert
18*404b540aSrobert You should have received a copy of the GNU General Public License
19*404b540aSrobert along with this program; if not, write to the Free Software
20*404b540aSrobert Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21*404b540aSrobert
22*404b540aSrobert In other words, you are welcome to use, share and improve this program.
23*404b540aSrobert You are forbidden to forbid anyone else to use, share and improve
24*404b540aSrobert what you give them. Help stamp out software-hoarding! */
25*404b540aSrobert
26*404b540aSrobert #include "config.h"
27*404b540aSrobert #include "system.h"
28*404b540aSrobert #include "cpplib.h"
29*404b540aSrobert #include "internal.h"
30*404b540aSrobert
31*404b540aSrobert typedef struct macro_arg macro_arg;
32*404b540aSrobert struct macro_arg
33*404b540aSrobert {
34*404b540aSrobert const cpp_token **first; /* First token in unexpanded argument. */
35*404b540aSrobert const cpp_token **expanded; /* Macro-expanded argument. */
36*404b540aSrobert const cpp_token *stringified; /* Stringified argument. */
37*404b540aSrobert unsigned int count; /* # of tokens in argument. */
38*404b540aSrobert unsigned int expanded_count; /* # of tokens in expanded argument. */
39*404b540aSrobert };
40*404b540aSrobert
41*404b540aSrobert /* Macro expansion. */
42*404b540aSrobert
43*404b540aSrobert static int enter_macro_context (cpp_reader *, cpp_hashnode *);
44*404b540aSrobert static int builtin_macro (cpp_reader *, cpp_hashnode *);
45*404b540aSrobert static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
46*404b540aSrobert const cpp_token **, unsigned int);
47*404b540aSrobert static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *);
48*404b540aSrobert static cpp_context *next_context (cpp_reader *);
49*404b540aSrobert static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
50*404b540aSrobert static void expand_arg (cpp_reader *, macro_arg *);
51*404b540aSrobert static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
52*404b540aSrobert static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
53*404b540aSrobert static void paste_all_tokens (cpp_reader *, const cpp_token *);
54*404b540aSrobert static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
55*404b540aSrobert static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
56*404b540aSrobert macro_arg *);
57*404b540aSrobert static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *);
58*404b540aSrobert static bool create_iso_definition (cpp_reader *, cpp_macro *);
59*404b540aSrobert
60*404b540aSrobert /* #define directive parsing and handling. */
61*404b540aSrobert
62*404b540aSrobert static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
63*404b540aSrobert static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
64*404b540aSrobert static bool warn_of_redefinition (cpp_reader *, const cpp_hashnode *,
65*404b540aSrobert const cpp_macro *);
66*404b540aSrobert static bool parse_params (cpp_reader *, cpp_macro *);
67*404b540aSrobert static void check_trad_stringification (cpp_reader *, const cpp_macro *,
68*404b540aSrobert const cpp_string *);
69*404b540aSrobert
70*404b540aSrobert /* Emits a warning if NODE is a macro defined in the main file that
71*404b540aSrobert has not been used. */
72*404b540aSrobert int
_cpp_warn_if_unused_macro(cpp_reader * pfile,cpp_hashnode * node,void * v ATTRIBUTE_UNUSED)73*404b540aSrobert _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
74*404b540aSrobert void *v ATTRIBUTE_UNUSED)
75*404b540aSrobert {
76*404b540aSrobert if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
77*404b540aSrobert {
78*404b540aSrobert cpp_macro *macro = node->value.macro;
79*404b540aSrobert
80*404b540aSrobert if (!macro->used
81*404b540aSrobert && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
82*404b540aSrobert cpp_error_with_line (pfile, CPP_DL_WARNING, macro->line, 0,
83*404b540aSrobert "macro \"%s\" is not used", NODE_NAME (node));
84*404b540aSrobert }
85*404b540aSrobert
86*404b540aSrobert return 1;
87*404b540aSrobert }
88*404b540aSrobert
89*404b540aSrobert /* Allocates and returns a CPP_STRING token, containing TEXT of length
90*404b540aSrobert LEN, after null-terminating it. TEXT must be in permanent storage. */
91*404b540aSrobert static const cpp_token *
new_string_token(cpp_reader * pfile,unsigned char * text,unsigned int len)92*404b540aSrobert new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
93*404b540aSrobert {
94*404b540aSrobert cpp_token *token = _cpp_temp_token (pfile);
95*404b540aSrobert
96*404b540aSrobert text[len] = '\0';
97*404b540aSrobert token->type = CPP_STRING;
98*404b540aSrobert token->val.str.len = len;
99*404b540aSrobert token->val.str.text = text;
100*404b540aSrobert token->flags = 0;
101*404b540aSrobert return token;
102*404b540aSrobert }
103*404b540aSrobert
104*404b540aSrobert static const char * const monthnames[] =
105*404b540aSrobert {
106*404b540aSrobert "Jan", "Feb", "Mar", "Apr", "May", "Jun",
107*404b540aSrobert "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
108*404b540aSrobert };
109*404b540aSrobert
110*404b540aSrobert /* Helper function for builtin_macro. Returns the text generated by
111*404b540aSrobert a builtin macro. */
112*404b540aSrobert const uchar *
_cpp_builtin_macro_text(cpp_reader * pfile,cpp_hashnode * node)113*404b540aSrobert _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
114*404b540aSrobert {
115*404b540aSrobert const struct line_map *map;
116*404b540aSrobert const uchar *result = NULL;
117*404b540aSrobert unsigned int number = 1;
118*404b540aSrobert
119*404b540aSrobert switch (node->value.builtin)
120*404b540aSrobert {
121*404b540aSrobert default:
122*404b540aSrobert cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
123*404b540aSrobert NODE_NAME (node));
124*404b540aSrobert break;
125*404b540aSrobert
126*404b540aSrobert case BT_TIMESTAMP:
127*404b540aSrobert {
128*404b540aSrobert cpp_buffer *pbuffer = cpp_get_buffer (pfile);
129*404b540aSrobert if (pbuffer->timestamp == NULL)
130*404b540aSrobert {
131*404b540aSrobert /* Initialize timestamp value of the assotiated file. */
132*404b540aSrobert struct _cpp_file *file = cpp_get_file (pbuffer);
133*404b540aSrobert if (file)
134*404b540aSrobert {
135*404b540aSrobert /* Generate __TIMESTAMP__ string, that represents
136*404b540aSrobert the date and time of the last modification
137*404b540aSrobert of the current source file. The string constant
138*404b540aSrobert looks like "Sun Sep 16 01:03:52 1973". */
139*404b540aSrobert struct tm *tb = NULL;
140*404b540aSrobert struct stat *st = _cpp_get_file_stat (file);
141*404b540aSrobert if (st)
142*404b540aSrobert tb = localtime (&st->st_mtime);
143*404b540aSrobert if (tb)
144*404b540aSrobert {
145*404b540aSrobert char *str = asctime (tb);
146*404b540aSrobert size_t len = strlen (str);
147*404b540aSrobert unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
148*404b540aSrobert buf[0] = '"';
149*404b540aSrobert strcpy ((char *) buf + 1, str);
150*404b540aSrobert buf[len] = '"';
151*404b540aSrobert pbuffer->timestamp = buf;
152*404b540aSrobert }
153*404b540aSrobert else
154*404b540aSrobert {
155*404b540aSrobert cpp_errno (pfile, CPP_DL_WARNING,
156*404b540aSrobert "could not determine file timestamp");
157*404b540aSrobert pbuffer->timestamp = U"\"??? ??? ?? ??:??:?? ????\"";
158*404b540aSrobert }
159*404b540aSrobert }
160*404b540aSrobert }
161*404b540aSrobert result = pbuffer->timestamp;
162*404b540aSrobert }
163*404b540aSrobert break;
164*404b540aSrobert case BT_FILE:
165*404b540aSrobert case BT_BASE_FILE:
166*404b540aSrobert {
167*404b540aSrobert unsigned int len;
168*404b540aSrobert const char *name;
169*404b540aSrobert uchar *buf;
170*404b540aSrobert map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
171*404b540aSrobert
172*404b540aSrobert if (node->value.builtin == BT_BASE_FILE)
173*404b540aSrobert while (! MAIN_FILE_P (map))
174*404b540aSrobert map = INCLUDED_FROM (pfile->line_table, map);
175*404b540aSrobert
176*404b540aSrobert name = map->to_file;
177*404b540aSrobert len = strlen (name);
178*404b540aSrobert buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
179*404b540aSrobert result = buf;
180*404b540aSrobert *buf = '"';
181*404b540aSrobert buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
182*404b540aSrobert *buf++ = '"';
183*404b540aSrobert *buf = '\0';
184*404b540aSrobert }
185*404b540aSrobert break;
186*404b540aSrobert
187*404b540aSrobert case BT_INCLUDE_LEVEL:
188*404b540aSrobert /* The line map depth counts the primary source as level 1, but
189*404b540aSrobert historically __INCLUDE_DEPTH__ has called the primary source
190*404b540aSrobert level 0. */
191*404b540aSrobert number = pfile->line_table->depth - 1;
192*404b540aSrobert break;
193*404b540aSrobert
194*404b540aSrobert case BT_SPECLINE:
195*404b540aSrobert map = &pfile->line_table->maps[pfile->line_table->used-1];
196*404b540aSrobert /* If __LINE__ is embedded in a macro, it must expand to the
197*404b540aSrobert line of the macro's invocation, not its definition.
198*404b540aSrobert Otherwise things like assert() will not work properly. */
199*404b540aSrobert if (CPP_OPTION (pfile, traditional))
200*404b540aSrobert number = pfile->line_table->highest_line;
201*404b540aSrobert else
202*404b540aSrobert number = pfile->cur_token[-1].src_loc;
203*404b540aSrobert number = SOURCE_LINE (map, number);
204*404b540aSrobert break;
205*404b540aSrobert
206*404b540aSrobert /* __STDC__ has the value 1 under normal circumstances.
207*404b540aSrobert However, if (a) we are in a system header, (b) the option
208*404b540aSrobert stdc_0_in_system_headers is true (set by target config), and
209*404b540aSrobert (c) we are not in strictly conforming mode, then it has the
210*404b540aSrobert value 0. (b) and (c) are already checked in cpp_init_builtins. */
211*404b540aSrobert case BT_STDC:
212*404b540aSrobert if (cpp_in_system_header (pfile))
213*404b540aSrobert number = 0;
214*404b540aSrobert else
215*404b540aSrobert number = 1;
216*404b540aSrobert break;
217*404b540aSrobert
218*404b540aSrobert case BT_DATE:
219*404b540aSrobert case BT_TIME:
220*404b540aSrobert if (pfile->date == NULL)
221*404b540aSrobert {
222*404b540aSrobert /* Allocate __DATE__ and __TIME__ strings from permanent
223*404b540aSrobert storage. We only do this once, and don't generate them
224*404b540aSrobert at init time, because time() and localtime() are very
225*404b540aSrobert slow on some systems. */
226*404b540aSrobert time_t tt;
227*404b540aSrobert struct tm *tb = NULL;
228*404b540aSrobert
229*404b540aSrobert /* (time_t) -1 is a legitimate value for "number of seconds
230*404b540aSrobert since the Epoch", so we have to do a little dance to
231*404b540aSrobert distinguish that from a genuine error. */
232*404b540aSrobert errno = 0;
233*404b540aSrobert tt = time(NULL);
234*404b540aSrobert if (tt != (time_t)-1 || errno == 0)
235*404b540aSrobert tb = localtime (&tt);
236*404b540aSrobert
237*404b540aSrobert if (tb)
238*404b540aSrobert {
239*404b540aSrobert pfile->date = _cpp_unaligned_alloc (pfile,
240*404b540aSrobert sizeof ("\"Oct 11 1347\""));
241*404b540aSrobert sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
242*404b540aSrobert monthnames[tb->tm_mon], tb->tm_mday,
243*404b540aSrobert tb->tm_year + 1900);
244*404b540aSrobert
245*404b540aSrobert pfile->time = _cpp_unaligned_alloc (pfile,
246*404b540aSrobert sizeof ("\"12:34:56\""));
247*404b540aSrobert sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
248*404b540aSrobert tb->tm_hour, tb->tm_min, tb->tm_sec);
249*404b540aSrobert }
250*404b540aSrobert else
251*404b540aSrobert {
252*404b540aSrobert cpp_errno (pfile, CPP_DL_WARNING,
253*404b540aSrobert "could not determine date and time");
254*404b540aSrobert
255*404b540aSrobert pfile->date = U"\"??? ?? ????\"";
256*404b540aSrobert pfile->time = U"\"??:??:??\"";
257*404b540aSrobert }
258*404b540aSrobert }
259*404b540aSrobert
260*404b540aSrobert if (node->value.builtin == BT_DATE)
261*404b540aSrobert result = pfile->date;
262*404b540aSrobert else
263*404b540aSrobert result = pfile->time;
264*404b540aSrobert break;
265*404b540aSrobert }
266*404b540aSrobert
267*404b540aSrobert if (result == NULL)
268*404b540aSrobert {
269*404b540aSrobert /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
270*404b540aSrobert result = _cpp_unaligned_alloc (pfile, 21);
271*404b540aSrobert sprintf ((char *) result, "%u", number);
272*404b540aSrobert }
273*404b540aSrobert
274*404b540aSrobert return result;
275*404b540aSrobert }
276*404b540aSrobert
277*404b540aSrobert /* Convert builtin macros like __FILE__ to a token and push it on the
278*404b540aSrobert context stack. Also handles _Pragma, for which a new token may not
279*404b540aSrobert be created. Returns 1 if it generates a new token context, 0 to
280*404b540aSrobert return the token to the caller. */
281*404b540aSrobert static int
builtin_macro(cpp_reader * pfile,cpp_hashnode * node)282*404b540aSrobert builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
283*404b540aSrobert {
284*404b540aSrobert const uchar *buf;
285*404b540aSrobert size_t len;
286*404b540aSrobert char *nbuf;
287*404b540aSrobert
288*404b540aSrobert if (node->value.builtin == BT_PRAGMA)
289*404b540aSrobert {
290*404b540aSrobert /* Don't interpret _Pragma within directives. The standard is
291*404b540aSrobert not clear on this, but to me this makes most sense. */
292*404b540aSrobert if (pfile->state.in_directive)
293*404b540aSrobert return 0;
294*404b540aSrobert
295*404b540aSrobert _cpp_do__Pragma (pfile);
296*404b540aSrobert return 1;
297*404b540aSrobert }
298*404b540aSrobert
299*404b540aSrobert buf = _cpp_builtin_macro_text (pfile, node);
300*404b540aSrobert len = ustrlen (buf);
301*404b540aSrobert nbuf = (char *) alloca (len + 1);
302*404b540aSrobert memcpy (nbuf, buf, len);
303*404b540aSrobert nbuf[len]='\n';
304*404b540aSrobert
305*404b540aSrobert cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
306*404b540aSrobert _cpp_clean_line (pfile);
307*404b540aSrobert
308*404b540aSrobert /* Set pfile->cur_token as required by _cpp_lex_direct. */
309*404b540aSrobert pfile->cur_token = _cpp_temp_token (pfile);
310*404b540aSrobert _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
311*404b540aSrobert if (pfile->buffer->cur != pfile->buffer->rlimit)
312*404b540aSrobert cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
313*404b540aSrobert NODE_NAME (node));
314*404b540aSrobert _cpp_pop_buffer (pfile);
315*404b540aSrobert
316*404b540aSrobert return 1;
317*404b540aSrobert }
318*404b540aSrobert
319*404b540aSrobert /* Copies SRC, of length LEN, to DEST, adding backslashes before all
320*404b540aSrobert backslashes and double quotes. DEST must be of sufficient size.
321*404b540aSrobert Returns a pointer to the end of the string. */
322*404b540aSrobert uchar *
cpp_quote_string(uchar * dest,const uchar * src,unsigned int len)323*404b540aSrobert cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
324*404b540aSrobert {
325*404b540aSrobert while (len--)
326*404b540aSrobert {
327*404b540aSrobert uchar c = *src++;
328*404b540aSrobert
329*404b540aSrobert if (c == '\\' || c == '"')
330*404b540aSrobert {
331*404b540aSrobert *dest++ = '\\';
332*404b540aSrobert *dest++ = c;
333*404b540aSrobert }
334*404b540aSrobert else
335*404b540aSrobert *dest++ = c;
336*404b540aSrobert }
337*404b540aSrobert
338*404b540aSrobert return dest;
339*404b540aSrobert }
340*404b540aSrobert
341*404b540aSrobert /* Convert a token sequence ARG to a single string token according to
342*404b540aSrobert the rules of the ISO C #-operator. */
343*404b540aSrobert static const cpp_token *
stringify_arg(cpp_reader * pfile,macro_arg * arg)344*404b540aSrobert stringify_arg (cpp_reader *pfile, macro_arg *arg)
345*404b540aSrobert {
346*404b540aSrobert unsigned char *dest;
347*404b540aSrobert unsigned int i, escape_it, backslash_count = 0;
348*404b540aSrobert const cpp_token *source = NULL;
349*404b540aSrobert size_t len;
350*404b540aSrobert
351*404b540aSrobert if (BUFF_ROOM (pfile->u_buff) < 3)
352*404b540aSrobert _cpp_extend_buff (pfile, &pfile->u_buff, 3);
353*404b540aSrobert dest = BUFF_FRONT (pfile->u_buff);
354*404b540aSrobert *dest++ = '"';
355*404b540aSrobert
356*404b540aSrobert /* Loop, reading in the argument's tokens. */
357*404b540aSrobert for (i = 0; i < arg->count; i++)
358*404b540aSrobert {
359*404b540aSrobert const cpp_token *token = arg->first[i];
360*404b540aSrobert
361*404b540aSrobert if (token->type == CPP_PADDING)
362*404b540aSrobert {
363*404b540aSrobert if (source == NULL)
364*404b540aSrobert source = token->val.source;
365*404b540aSrobert continue;
366*404b540aSrobert }
367*404b540aSrobert
368*404b540aSrobert escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
369*404b540aSrobert || token->type == CPP_CHAR || token->type == CPP_WCHAR);
370*404b540aSrobert
371*404b540aSrobert /* Room for each char being written in octal, initial space and
372*404b540aSrobert final quote and NUL. */
373*404b540aSrobert len = cpp_token_len (token);
374*404b540aSrobert if (escape_it)
375*404b540aSrobert len *= 4;
376*404b540aSrobert len += 3;
377*404b540aSrobert
378*404b540aSrobert if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
379*404b540aSrobert {
380*404b540aSrobert size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
381*404b540aSrobert _cpp_extend_buff (pfile, &pfile->u_buff, len);
382*404b540aSrobert dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
383*404b540aSrobert }
384*404b540aSrobert
385*404b540aSrobert /* Leading white space? */
386*404b540aSrobert if (dest - 1 != BUFF_FRONT (pfile->u_buff))
387*404b540aSrobert {
388*404b540aSrobert if (source == NULL)
389*404b540aSrobert source = token;
390*404b540aSrobert if (source->flags & PREV_WHITE)
391*404b540aSrobert *dest++ = ' ';
392*404b540aSrobert }
393*404b540aSrobert source = NULL;
394*404b540aSrobert
395*404b540aSrobert if (escape_it)
396*404b540aSrobert {
397*404b540aSrobert _cpp_buff *buff = _cpp_get_buff (pfile, len);
398*404b540aSrobert unsigned char *buf = BUFF_FRONT (buff);
399*404b540aSrobert len = cpp_spell_token (pfile, token, buf, true) - buf;
400*404b540aSrobert dest = cpp_quote_string (dest, buf, len);
401*404b540aSrobert _cpp_release_buff (pfile, buff);
402*404b540aSrobert }
403*404b540aSrobert else
404*404b540aSrobert dest = cpp_spell_token (pfile, token, dest, true);
405*404b540aSrobert
406*404b540aSrobert if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
407*404b540aSrobert backslash_count++;
408*404b540aSrobert else
409*404b540aSrobert backslash_count = 0;
410*404b540aSrobert }
411*404b540aSrobert
412*404b540aSrobert /* Ignore the final \ of invalid string literals. */
413*404b540aSrobert if (backslash_count & 1)
414*404b540aSrobert {
415*404b540aSrobert cpp_error (pfile, CPP_DL_WARNING,
416*404b540aSrobert "invalid string literal, ignoring final '\\'");
417*404b540aSrobert dest--;
418*404b540aSrobert }
419*404b540aSrobert
420*404b540aSrobert /* Commit the memory, including NUL, and return the token. */
421*404b540aSrobert *dest++ = '"';
422*404b540aSrobert len = dest - BUFF_FRONT (pfile->u_buff);
423*404b540aSrobert BUFF_FRONT (pfile->u_buff) = dest + 1;
424*404b540aSrobert return new_string_token (pfile, dest - len, len);
425*404b540aSrobert }
426*404b540aSrobert
427*404b540aSrobert /* Try to paste two tokens. On success, return nonzero. In any
428*404b540aSrobert case, PLHS is updated to point to the pasted token, which is
429*404b540aSrobert guaranteed to not have the PASTE_LEFT flag set. */
430*404b540aSrobert static bool
paste_tokens(cpp_reader * pfile,const cpp_token ** plhs,const cpp_token * rhs)431*404b540aSrobert paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
432*404b540aSrobert {
433*404b540aSrobert unsigned char *buf, *end, *lhsend;
434*404b540aSrobert const cpp_token *lhs;
435*404b540aSrobert unsigned int len;
436*404b540aSrobert
437*404b540aSrobert lhs = *plhs;
438*404b540aSrobert len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
439*404b540aSrobert buf = (unsigned char *) alloca (len);
440*404b540aSrobert end = lhsend = cpp_spell_token (pfile, lhs, buf, false);
441*404b540aSrobert
442*404b540aSrobert /* Avoid comment headers, since they are still processed in stage 3.
443*404b540aSrobert It is simpler to insert a space here, rather than modifying the
444*404b540aSrobert lexer to ignore comments in some circumstances. Simply returning
445*404b540aSrobert false doesn't work, since we want to clear the PASTE_LEFT flag. */
446*404b540aSrobert if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
447*404b540aSrobert *end++ = ' ';
448*404b540aSrobert end = cpp_spell_token (pfile, rhs, end, false);
449*404b540aSrobert *end = '\n';
450*404b540aSrobert
451*404b540aSrobert cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
452*404b540aSrobert _cpp_clean_line (pfile);
453*404b540aSrobert
454*404b540aSrobert /* Set pfile->cur_token as required by _cpp_lex_direct. */
455*404b540aSrobert pfile->cur_token = _cpp_temp_token (pfile);
456*404b540aSrobert *plhs = _cpp_lex_direct (pfile);
457*404b540aSrobert if (pfile->buffer->cur != pfile->buffer->rlimit)
458*404b540aSrobert {
459*404b540aSrobert _cpp_pop_buffer (pfile);
460*404b540aSrobert _cpp_backup_tokens (pfile, 1);
461*404b540aSrobert *lhsend = '\0';
462*404b540aSrobert
463*404b540aSrobert /* Mandatory error for all apart from assembler. */
464*404b540aSrobert if (CPP_OPTION (pfile, lang) != CLK_ASM)
465*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
466*404b540aSrobert "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
467*404b540aSrobert buf, cpp_token_as_text (pfile, rhs));
468*404b540aSrobert return false;
469*404b540aSrobert }
470*404b540aSrobert
471*404b540aSrobert _cpp_pop_buffer (pfile);
472*404b540aSrobert return true;
473*404b540aSrobert }
474*404b540aSrobert
475*404b540aSrobert /* Handles an arbitrarily long sequence of ## operators, with initial
476*404b540aSrobert operand LHS. This implementation is left-associative,
477*404b540aSrobert non-recursive, and finishes a paste before handling succeeding
478*404b540aSrobert ones. If a paste fails, we back up to the RHS of the failing ##
479*404b540aSrobert operator before pushing the context containing the result of prior
480*404b540aSrobert successful pastes, with the effect that the RHS appears in the
481*404b540aSrobert output stream after the pasted LHS normally. */
482*404b540aSrobert static void
paste_all_tokens(cpp_reader * pfile,const cpp_token * lhs)483*404b540aSrobert paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
484*404b540aSrobert {
485*404b540aSrobert const cpp_token *rhs;
486*404b540aSrobert cpp_context *context = pfile->context;
487*404b540aSrobert
488*404b540aSrobert do
489*404b540aSrobert {
490*404b540aSrobert /* Take the token directly from the current context. We can do
491*404b540aSrobert this, because we are in the replacement list of either an
492*404b540aSrobert object-like macro, or a function-like macro with arguments
493*404b540aSrobert inserted. In either case, the constraints to #define
494*404b540aSrobert guarantee we have at least one more token. */
495*404b540aSrobert if (context->direct_p)
496*404b540aSrobert rhs = FIRST (context).token++;
497*404b540aSrobert else
498*404b540aSrobert rhs = *FIRST (context).ptoken++;
499*404b540aSrobert
500*404b540aSrobert if (rhs->type == CPP_PADDING)
501*404b540aSrobert abort ();
502*404b540aSrobert
503*404b540aSrobert if (!paste_tokens (pfile, &lhs, rhs))
504*404b540aSrobert break;
505*404b540aSrobert }
506*404b540aSrobert while (rhs->flags & PASTE_LEFT);
507*404b540aSrobert
508*404b540aSrobert /* Put the resulting token in its own context. */
509*404b540aSrobert _cpp_push_token_context (pfile, NULL, lhs, 1);
510*404b540aSrobert }
511*404b540aSrobert
512*404b540aSrobert /* Returns TRUE if the number of arguments ARGC supplied in an
513*404b540aSrobert invocation of the MACRO referenced by NODE is valid. An empty
514*404b540aSrobert invocation to a macro with no parameters should pass ARGC as zero.
515*404b540aSrobert
516*404b540aSrobert Note that MACRO cannot necessarily be deduced from NODE, in case
517*404b540aSrobert NODE was redefined whilst collecting arguments. */
518*404b540aSrobert bool
_cpp_arguments_ok(cpp_reader * pfile,cpp_macro * macro,const cpp_hashnode * node,unsigned int argc)519*404b540aSrobert _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
520*404b540aSrobert {
521*404b540aSrobert if (argc == macro->paramc)
522*404b540aSrobert return true;
523*404b540aSrobert
524*404b540aSrobert if (argc < macro->paramc)
525*404b540aSrobert {
526*404b540aSrobert /* As an extension, a rest argument is allowed to not appear in
527*404b540aSrobert the invocation at all.
528*404b540aSrobert e.g. #define debug(format, args...) something
529*404b540aSrobert debug("string");
530*404b540aSrobert
531*404b540aSrobert This is exactly the same as if there had been an empty rest
532*404b540aSrobert argument - debug("string", ). */
533*404b540aSrobert
534*404b540aSrobert if (argc + 1 == macro->paramc && macro->variadic)
535*404b540aSrobert {
536*404b540aSrobert if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
537*404b540aSrobert cpp_error (pfile, CPP_DL_PEDWARN,
538*404b540aSrobert "ISO C99 requires rest arguments to be used");
539*404b540aSrobert return true;
540*404b540aSrobert }
541*404b540aSrobert
542*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
543*404b540aSrobert "macro \"%s\" requires %u arguments, but only %u given",
544*404b540aSrobert NODE_NAME (node), macro->paramc, argc);
545*404b540aSrobert }
546*404b540aSrobert else
547*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
548*404b540aSrobert "macro \"%s\" passed %u arguments, but takes just %u",
549*404b540aSrobert NODE_NAME (node), argc, macro->paramc);
550*404b540aSrobert
551*404b540aSrobert return false;
552*404b540aSrobert }
553*404b540aSrobert
554*404b540aSrobert /* Reads and returns the arguments to a function-like macro
555*404b540aSrobert invocation. Assumes the opening parenthesis has been processed.
556*404b540aSrobert If there is an error, emits an appropriate diagnostic and returns
557*404b540aSrobert NULL. Each argument is terminated by a CPP_EOF token, for the
558*404b540aSrobert future benefit of expand_arg(). */
559*404b540aSrobert static _cpp_buff *
collect_args(cpp_reader * pfile,const cpp_hashnode * node)560*404b540aSrobert collect_args (cpp_reader *pfile, const cpp_hashnode *node)
561*404b540aSrobert {
562*404b540aSrobert _cpp_buff *buff, *base_buff;
563*404b540aSrobert cpp_macro *macro;
564*404b540aSrobert macro_arg *args, *arg;
565*404b540aSrobert const cpp_token *token;
566*404b540aSrobert unsigned int argc;
567*404b540aSrobert
568*404b540aSrobert macro = node->value.macro;
569*404b540aSrobert if (macro->paramc)
570*404b540aSrobert argc = macro->paramc;
571*404b540aSrobert else
572*404b540aSrobert argc = 1;
573*404b540aSrobert buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
574*404b540aSrobert + sizeof (macro_arg)));
575*404b540aSrobert base_buff = buff;
576*404b540aSrobert args = (macro_arg *) buff->base;
577*404b540aSrobert memset (args, 0, argc * sizeof (macro_arg));
578*404b540aSrobert buff->cur = (unsigned char *) &args[argc];
579*404b540aSrobert arg = args, argc = 0;
580*404b540aSrobert
581*404b540aSrobert /* Collect the tokens making up each argument. We don't yet know
582*404b540aSrobert how many arguments have been supplied, whether too many or too
583*404b540aSrobert few. Hence the slightly bizarre usage of "argc" and "arg". */
584*404b540aSrobert do
585*404b540aSrobert {
586*404b540aSrobert unsigned int paren_depth = 0;
587*404b540aSrobert unsigned int ntokens = 0;
588*404b540aSrobert
589*404b540aSrobert argc++;
590*404b540aSrobert arg->first = (const cpp_token **) buff->cur;
591*404b540aSrobert
592*404b540aSrobert for (;;)
593*404b540aSrobert {
594*404b540aSrobert /* Require space for 2 new tokens (including a CPP_EOF). */
595*404b540aSrobert if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
596*404b540aSrobert {
597*404b540aSrobert buff = _cpp_append_extend_buff (pfile, buff,
598*404b540aSrobert 1000 * sizeof (cpp_token *));
599*404b540aSrobert arg->first = (const cpp_token **) buff->cur;
600*404b540aSrobert }
601*404b540aSrobert
602*404b540aSrobert token = cpp_get_token (pfile);
603*404b540aSrobert
604*404b540aSrobert if (token->type == CPP_PADDING)
605*404b540aSrobert {
606*404b540aSrobert /* Drop leading padding. */
607*404b540aSrobert if (ntokens == 0)
608*404b540aSrobert continue;
609*404b540aSrobert }
610*404b540aSrobert else if (token->type == CPP_OPEN_PAREN)
611*404b540aSrobert paren_depth++;
612*404b540aSrobert else if (token->type == CPP_CLOSE_PAREN)
613*404b540aSrobert {
614*404b540aSrobert if (paren_depth-- == 0)
615*404b540aSrobert break;
616*404b540aSrobert }
617*404b540aSrobert else if (token->type == CPP_COMMA)
618*404b540aSrobert {
619*404b540aSrobert /* A comma does not terminate an argument within
620*404b540aSrobert parentheses or as part of a variable argument. */
621*404b540aSrobert if (paren_depth == 0
622*404b540aSrobert && ! (macro->variadic && argc == macro->paramc))
623*404b540aSrobert break;
624*404b540aSrobert }
625*404b540aSrobert else if (token->type == CPP_EOF
626*404b540aSrobert || (token->type == CPP_HASH && token->flags & BOL))
627*404b540aSrobert break;
628*404b540aSrobert
629*404b540aSrobert arg->first[ntokens++] = token;
630*404b540aSrobert }
631*404b540aSrobert
632*404b540aSrobert /* Drop trailing padding. */
633*404b540aSrobert while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
634*404b540aSrobert ntokens--;
635*404b540aSrobert
636*404b540aSrobert arg->count = ntokens;
637*404b540aSrobert arg->first[ntokens] = &pfile->eof;
638*404b540aSrobert
639*404b540aSrobert /* Terminate the argument. Excess arguments loop back and
640*404b540aSrobert overwrite the final legitimate argument, before failing. */
641*404b540aSrobert if (argc <= macro->paramc)
642*404b540aSrobert {
643*404b540aSrobert buff->cur = (unsigned char *) &arg->first[ntokens + 1];
644*404b540aSrobert if (argc != macro->paramc)
645*404b540aSrobert arg++;
646*404b540aSrobert }
647*404b540aSrobert }
648*404b540aSrobert while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
649*404b540aSrobert
650*404b540aSrobert if (token->type == CPP_EOF)
651*404b540aSrobert {
652*404b540aSrobert /* We still need the CPP_EOF to end directives, and to end
653*404b540aSrobert pre-expansion of a macro argument. Step back is not
654*404b540aSrobert unconditional, since we don't want to return a CPP_EOF to our
655*404b540aSrobert callers at the end of an -include-d file. */
656*404b540aSrobert if (pfile->context->prev || pfile->state.in_directive)
657*404b540aSrobert _cpp_backup_tokens (pfile, 1);
658*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
659*404b540aSrobert "unterminated argument list invoking macro \"%s\"",
660*404b540aSrobert NODE_NAME (node));
661*404b540aSrobert }
662*404b540aSrobert else
663*404b540aSrobert {
664*404b540aSrobert /* A single empty argument is counted as no argument. */
665*404b540aSrobert if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
666*404b540aSrobert argc = 0;
667*404b540aSrobert if (_cpp_arguments_ok (pfile, macro, node, argc))
668*404b540aSrobert {
669*404b540aSrobert /* GCC has special semantics for , ## b where b is a varargs
670*404b540aSrobert parameter: we remove the comma if b was omitted entirely.
671*404b540aSrobert If b was merely an empty argument, the comma is retained.
672*404b540aSrobert If the macro takes just one (varargs) parameter, then we
673*404b540aSrobert retain the comma only if we are standards conforming.
674*404b540aSrobert
675*404b540aSrobert If FIRST is NULL replace_args () swallows the comma. */
676*404b540aSrobert if (macro->variadic && (argc < macro->paramc
677*404b540aSrobert || (argc == 1 && args[0].count == 0
678*404b540aSrobert && !CPP_OPTION (pfile, std))))
679*404b540aSrobert args[macro->paramc - 1].first = NULL;
680*404b540aSrobert return base_buff;
681*404b540aSrobert }
682*404b540aSrobert }
683*404b540aSrobert
684*404b540aSrobert /* An error occurred. */
685*404b540aSrobert _cpp_release_buff (pfile, base_buff);
686*404b540aSrobert return NULL;
687*404b540aSrobert }
688*404b540aSrobert
689*404b540aSrobert /* Search for an opening parenthesis to the macro of NODE, in such a
690*404b540aSrobert way that, if none is found, we don't lose the information in any
691*404b540aSrobert intervening padding tokens. If we find the parenthesis, collect
692*404b540aSrobert the arguments and return the buffer containing them. */
693*404b540aSrobert static _cpp_buff *
funlike_invocation_p(cpp_reader * pfile,cpp_hashnode * node)694*404b540aSrobert funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
695*404b540aSrobert {
696*404b540aSrobert const cpp_token *token, *padding = NULL;
697*404b540aSrobert
698*404b540aSrobert for (;;)
699*404b540aSrobert {
700*404b540aSrobert token = cpp_get_token (pfile);
701*404b540aSrobert if (token->type != CPP_PADDING)
702*404b540aSrobert break;
703*404b540aSrobert if (padding == NULL
704*404b540aSrobert || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
705*404b540aSrobert padding = token;
706*404b540aSrobert }
707*404b540aSrobert
708*404b540aSrobert if (token->type == CPP_OPEN_PAREN)
709*404b540aSrobert {
710*404b540aSrobert pfile->state.parsing_args = 2;
711*404b540aSrobert return collect_args (pfile, node);
712*404b540aSrobert }
713*404b540aSrobert
714*404b540aSrobert /* CPP_EOF can be the end of macro arguments, or the end of the
715*404b540aSrobert file. We mustn't back up over the latter. Ugh. */
716*404b540aSrobert if (token->type != CPP_EOF || token == &pfile->eof)
717*404b540aSrobert {
718*404b540aSrobert /* Back up. We may have skipped padding, in which case backing
719*404b540aSrobert up more than one token when expanding macros is in general
720*404b540aSrobert too difficult. We re-insert it in its own context. */
721*404b540aSrobert _cpp_backup_tokens (pfile, 1);
722*404b540aSrobert if (padding)
723*404b540aSrobert _cpp_push_token_context (pfile, NULL, padding, 1);
724*404b540aSrobert }
725*404b540aSrobert
726*404b540aSrobert return NULL;
727*404b540aSrobert }
728*404b540aSrobert
729*404b540aSrobert /* Push the context of a macro with hash entry NODE onto the context
730*404b540aSrobert stack. If we can successfully expand the macro, we push a context
731*404b540aSrobert containing its yet-to-be-rescanned replacement list and return one.
732*404b540aSrobert Otherwise, we don't push a context and return zero. */
733*404b540aSrobert static int
enter_macro_context(cpp_reader * pfile,cpp_hashnode * node)734*404b540aSrobert enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
735*404b540aSrobert {
736*404b540aSrobert /* The presence of a macro invalidates a file's controlling macro. */
737*404b540aSrobert pfile->mi_valid = false;
738*404b540aSrobert
739*404b540aSrobert pfile->state.angled_headers = false;
740*404b540aSrobert
741*404b540aSrobert /* Handle standard macros. */
742*404b540aSrobert if (! (node->flags & NODE_BUILTIN))
743*404b540aSrobert {
744*404b540aSrobert cpp_macro *macro = node->value.macro;
745*404b540aSrobert
746*404b540aSrobert if (macro->fun_like)
747*404b540aSrobert {
748*404b540aSrobert _cpp_buff *buff;
749*404b540aSrobert
750*404b540aSrobert pfile->state.prevent_expansion++;
751*404b540aSrobert pfile->keep_tokens++;
752*404b540aSrobert pfile->state.parsing_args = 1;
753*404b540aSrobert buff = funlike_invocation_p (pfile, node);
754*404b540aSrobert pfile->state.parsing_args = 0;
755*404b540aSrobert pfile->keep_tokens--;
756*404b540aSrobert pfile->state.prevent_expansion--;
757*404b540aSrobert
758*404b540aSrobert if (buff == NULL)
759*404b540aSrobert {
760*404b540aSrobert if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
761*404b540aSrobert cpp_error (pfile, CPP_DL_WARNING,
762*404b540aSrobert "function-like macro \"%s\" must be used with arguments in traditional C",
763*404b540aSrobert NODE_NAME (node));
764*404b540aSrobert
765*404b540aSrobert return 0;
766*404b540aSrobert }
767*404b540aSrobert
768*404b540aSrobert if (macro->paramc > 0)
769*404b540aSrobert replace_args (pfile, node, macro, (macro_arg *) buff->base);
770*404b540aSrobert _cpp_release_buff (pfile, buff);
771*404b540aSrobert }
772*404b540aSrobert
773*404b540aSrobert /* Disable the macro within its expansion. */
774*404b540aSrobert node->flags |= NODE_DISABLED;
775*404b540aSrobert
776*404b540aSrobert macro->used = 1;
777*404b540aSrobert
778*404b540aSrobert if (macro->paramc == 0)
779*404b540aSrobert _cpp_push_token_context (pfile, node, macro->exp.tokens, macro->count);
780*404b540aSrobert
781*404b540aSrobert return 1;
782*404b540aSrobert }
783*404b540aSrobert
784*404b540aSrobert /* Handle built-in macros and the _Pragma operator. */
785*404b540aSrobert return builtin_macro (pfile, node);
786*404b540aSrobert }
787*404b540aSrobert
788*404b540aSrobert /* Replace the parameters in a function-like macro of NODE with the
789*404b540aSrobert actual ARGS, and place the result in a newly pushed token context.
790*404b540aSrobert Expand each argument before replacing, unless it is operated upon
791*404b540aSrobert by the # or ## operators. */
792*404b540aSrobert static void
replace_args(cpp_reader * pfile,cpp_hashnode * node,cpp_macro * macro,macro_arg * args)793*404b540aSrobert replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
794*404b540aSrobert {
795*404b540aSrobert unsigned int i, total;
796*404b540aSrobert const cpp_token *src, *limit;
797*404b540aSrobert const cpp_token **dest, **first;
798*404b540aSrobert macro_arg *arg;
799*404b540aSrobert _cpp_buff *buff;
800*404b540aSrobert
801*404b540aSrobert /* First, fully macro-expand arguments, calculating the number of
802*404b540aSrobert tokens in the final expansion as we go. The ordering of the if
803*404b540aSrobert statements below is subtle; we must handle stringification before
804*404b540aSrobert pasting. */
805*404b540aSrobert total = macro->count;
806*404b540aSrobert limit = macro->exp.tokens + macro->count;
807*404b540aSrobert
808*404b540aSrobert for (src = macro->exp.tokens; src < limit; src++)
809*404b540aSrobert if (src->type == CPP_MACRO_ARG)
810*404b540aSrobert {
811*404b540aSrobert /* Leading and trailing padding tokens. */
812*404b540aSrobert total += 2;
813*404b540aSrobert
814*404b540aSrobert /* We have an argument. If it is not being stringified or
815*404b540aSrobert pasted it is macro-replaced before insertion. */
816*404b540aSrobert arg = &args[src->val.arg_no - 1];
817*404b540aSrobert
818*404b540aSrobert if (src->flags & STRINGIFY_ARG)
819*404b540aSrobert {
820*404b540aSrobert if (!arg->stringified)
821*404b540aSrobert arg->stringified = stringify_arg (pfile, arg);
822*404b540aSrobert }
823*404b540aSrobert else if ((src->flags & PASTE_LEFT)
824*404b540aSrobert || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
825*404b540aSrobert total += arg->count - 1;
826*404b540aSrobert else
827*404b540aSrobert {
828*404b540aSrobert if (!arg->expanded)
829*404b540aSrobert expand_arg (pfile, arg);
830*404b540aSrobert total += arg->expanded_count - 1;
831*404b540aSrobert }
832*404b540aSrobert }
833*404b540aSrobert
834*404b540aSrobert /* Now allocate space for the expansion, copy the tokens and replace
835*404b540aSrobert the arguments. */
836*404b540aSrobert buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
837*404b540aSrobert first = (const cpp_token **) buff->base;
838*404b540aSrobert dest = first;
839*404b540aSrobert
840*404b540aSrobert for (src = macro->exp.tokens; src < limit; src++)
841*404b540aSrobert {
842*404b540aSrobert unsigned int count;
843*404b540aSrobert const cpp_token **from, **paste_flag;
844*404b540aSrobert
845*404b540aSrobert if (src->type != CPP_MACRO_ARG)
846*404b540aSrobert {
847*404b540aSrobert *dest++ = src;
848*404b540aSrobert continue;
849*404b540aSrobert }
850*404b540aSrobert
851*404b540aSrobert paste_flag = 0;
852*404b540aSrobert arg = &args[src->val.arg_no - 1];
853*404b540aSrobert if (src->flags & STRINGIFY_ARG)
854*404b540aSrobert count = 1, from = &arg->stringified;
855*404b540aSrobert else if (src->flags & PASTE_LEFT)
856*404b540aSrobert count = arg->count, from = arg->first;
857*404b540aSrobert else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
858*404b540aSrobert {
859*404b540aSrobert count = arg->count, from = arg->first;
860*404b540aSrobert if (dest != first)
861*404b540aSrobert {
862*404b540aSrobert if (dest[-1]->type == CPP_COMMA
863*404b540aSrobert && macro->variadic
864*404b540aSrobert && src->val.arg_no == macro->paramc)
865*404b540aSrobert {
866*404b540aSrobert /* Swallow a pasted comma if from == NULL, otherwise
867*404b540aSrobert drop the paste flag. */
868*404b540aSrobert if (from == NULL)
869*404b540aSrobert dest--;
870*404b540aSrobert else
871*404b540aSrobert paste_flag = dest - 1;
872*404b540aSrobert }
873*404b540aSrobert /* Remove the paste flag if the RHS is a placemarker. */
874*404b540aSrobert else if (count == 0)
875*404b540aSrobert paste_flag = dest - 1;
876*404b540aSrobert }
877*404b540aSrobert }
878*404b540aSrobert else
879*404b540aSrobert count = arg->expanded_count, from = arg->expanded;
880*404b540aSrobert
881*404b540aSrobert /* Padding on the left of an argument (unless RHS of ##). */
882*404b540aSrobert if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
883*404b540aSrobert && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
884*404b540aSrobert *dest++ = padding_token (pfile, src);
885*404b540aSrobert
886*404b540aSrobert if (count)
887*404b540aSrobert {
888*404b540aSrobert memcpy (dest, from, count * sizeof (cpp_token *));
889*404b540aSrobert dest += count;
890*404b540aSrobert
891*404b540aSrobert /* With a non-empty argument on the LHS of ##, the last
892*404b540aSrobert token should be flagged PASTE_LEFT. */
893*404b540aSrobert if (src->flags & PASTE_LEFT)
894*404b540aSrobert paste_flag = dest - 1;
895*404b540aSrobert }
896*404b540aSrobert
897*404b540aSrobert /* Avoid paste on RHS (even case count == 0). */
898*404b540aSrobert if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
899*404b540aSrobert *dest++ = &pfile->avoid_paste;
900*404b540aSrobert
901*404b540aSrobert /* Add a new paste flag, or remove an unwanted one. */
902*404b540aSrobert if (paste_flag)
903*404b540aSrobert {
904*404b540aSrobert cpp_token *token = _cpp_temp_token (pfile);
905*404b540aSrobert token->type = (*paste_flag)->type;
906*404b540aSrobert token->val = (*paste_flag)->val;
907*404b540aSrobert if (src->flags & PASTE_LEFT)
908*404b540aSrobert token->flags = (*paste_flag)->flags | PASTE_LEFT;
909*404b540aSrobert else
910*404b540aSrobert token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
911*404b540aSrobert *paste_flag = token;
912*404b540aSrobert }
913*404b540aSrobert }
914*404b540aSrobert
915*404b540aSrobert /* Free the expanded arguments. */
916*404b540aSrobert for (i = 0; i < macro->paramc; i++)
917*404b540aSrobert if (args[i].expanded)
918*404b540aSrobert free (args[i].expanded);
919*404b540aSrobert
920*404b540aSrobert push_ptoken_context (pfile, node, buff, first, dest - first);
921*404b540aSrobert }
922*404b540aSrobert
923*404b540aSrobert /* Return a special padding token, with padding inherited from SOURCE. */
924*404b540aSrobert static const cpp_token *
padding_token(cpp_reader * pfile,const cpp_token * source)925*404b540aSrobert padding_token (cpp_reader *pfile, const cpp_token *source)
926*404b540aSrobert {
927*404b540aSrobert cpp_token *result = _cpp_temp_token (pfile);
928*404b540aSrobert
929*404b540aSrobert result->type = CPP_PADDING;
930*404b540aSrobert
931*404b540aSrobert /* Data in GCed data structures cannot be made const so far, so we
932*404b540aSrobert need a cast here. */
933*404b540aSrobert result->val.source = (cpp_token *) source;
934*404b540aSrobert result->flags = 0;
935*404b540aSrobert return result;
936*404b540aSrobert }
937*404b540aSrobert
938*404b540aSrobert /* Get a new uninitialized context. Create a new one if we cannot
939*404b540aSrobert re-use an old one. */
940*404b540aSrobert static cpp_context *
next_context(cpp_reader * pfile)941*404b540aSrobert next_context (cpp_reader *pfile)
942*404b540aSrobert {
943*404b540aSrobert cpp_context *result = pfile->context->next;
944*404b540aSrobert
945*404b540aSrobert if (result == 0)
946*404b540aSrobert {
947*404b540aSrobert result = XNEW (cpp_context);
948*404b540aSrobert result->prev = pfile->context;
949*404b540aSrobert result->next = 0;
950*404b540aSrobert pfile->context->next = result;
951*404b540aSrobert }
952*404b540aSrobert
953*404b540aSrobert pfile->context = result;
954*404b540aSrobert return result;
955*404b540aSrobert }
956*404b540aSrobert
957*404b540aSrobert /* Push a list of pointers to tokens. */
958*404b540aSrobert static void
push_ptoken_context(cpp_reader * pfile,cpp_hashnode * macro,_cpp_buff * buff,const cpp_token ** first,unsigned int count)959*404b540aSrobert push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
960*404b540aSrobert const cpp_token **first, unsigned int count)
961*404b540aSrobert {
962*404b540aSrobert cpp_context *context = next_context (pfile);
963*404b540aSrobert
964*404b540aSrobert context->direct_p = false;
965*404b540aSrobert context->macro = macro;
966*404b540aSrobert context->buff = buff;
967*404b540aSrobert FIRST (context).ptoken = first;
968*404b540aSrobert LAST (context).ptoken = first + count;
969*404b540aSrobert }
970*404b540aSrobert
971*404b540aSrobert /* Push a list of tokens. */
972*404b540aSrobert void
_cpp_push_token_context(cpp_reader * pfile,cpp_hashnode * macro,const cpp_token * first,unsigned int count)973*404b540aSrobert _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
974*404b540aSrobert const cpp_token *first, unsigned int count)
975*404b540aSrobert {
976*404b540aSrobert cpp_context *context = next_context (pfile);
977*404b540aSrobert
978*404b540aSrobert context->direct_p = true;
979*404b540aSrobert context->macro = macro;
980*404b540aSrobert context->buff = NULL;
981*404b540aSrobert FIRST (context).token = first;
982*404b540aSrobert LAST (context).token = first + count;
983*404b540aSrobert }
984*404b540aSrobert
985*404b540aSrobert /* Push a traditional macro's replacement text. */
986*404b540aSrobert void
_cpp_push_text_context(cpp_reader * pfile,cpp_hashnode * macro,const uchar * start,size_t len)987*404b540aSrobert _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
988*404b540aSrobert const uchar *start, size_t len)
989*404b540aSrobert {
990*404b540aSrobert cpp_context *context = next_context (pfile);
991*404b540aSrobert
992*404b540aSrobert context->direct_p = true;
993*404b540aSrobert context->macro = macro;
994*404b540aSrobert context->buff = NULL;
995*404b540aSrobert CUR (context) = start;
996*404b540aSrobert RLIMIT (context) = start + len;
997*404b540aSrobert macro->flags |= NODE_DISABLED;
998*404b540aSrobert }
999*404b540aSrobert
1000*404b540aSrobert /* Expand an argument ARG before replacing parameters in a
1001*404b540aSrobert function-like macro. This works by pushing a context with the
1002*404b540aSrobert argument's tokens, and then expanding that into a temporary buffer
1003*404b540aSrobert as if it were a normal part of the token stream. collect_args()
1004*404b540aSrobert has terminated the argument's tokens with a CPP_EOF so that we know
1005*404b540aSrobert when we have fully expanded the argument. */
1006*404b540aSrobert static void
expand_arg(cpp_reader * pfile,macro_arg * arg)1007*404b540aSrobert expand_arg (cpp_reader *pfile, macro_arg *arg)
1008*404b540aSrobert {
1009*404b540aSrobert unsigned int capacity;
1010*404b540aSrobert bool saved_warn_trad;
1011*404b540aSrobert
1012*404b540aSrobert if (arg->count == 0)
1013*404b540aSrobert return;
1014*404b540aSrobert
1015*404b540aSrobert /* Don't warn about funlike macros when pre-expanding. */
1016*404b540aSrobert saved_warn_trad = CPP_WTRADITIONAL (pfile);
1017*404b540aSrobert CPP_WTRADITIONAL (pfile) = 0;
1018*404b540aSrobert
1019*404b540aSrobert /* Loop, reading in the arguments. */
1020*404b540aSrobert capacity = 256;
1021*404b540aSrobert arg->expanded = XNEWVEC (const cpp_token *, capacity);
1022*404b540aSrobert
1023*404b540aSrobert push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
1024*404b540aSrobert for (;;)
1025*404b540aSrobert {
1026*404b540aSrobert const cpp_token *token;
1027*404b540aSrobert
1028*404b540aSrobert if (arg->expanded_count + 1 >= capacity)
1029*404b540aSrobert {
1030*404b540aSrobert capacity *= 2;
1031*404b540aSrobert arg->expanded = XRESIZEVEC (const cpp_token *, arg->expanded,
1032*404b540aSrobert capacity);
1033*404b540aSrobert }
1034*404b540aSrobert
1035*404b540aSrobert token = cpp_get_token (pfile);
1036*404b540aSrobert
1037*404b540aSrobert if (token->type == CPP_EOF)
1038*404b540aSrobert break;
1039*404b540aSrobert
1040*404b540aSrobert arg->expanded[arg->expanded_count++] = token;
1041*404b540aSrobert }
1042*404b540aSrobert
1043*404b540aSrobert _cpp_pop_context (pfile);
1044*404b540aSrobert
1045*404b540aSrobert CPP_WTRADITIONAL (pfile) = saved_warn_trad;
1046*404b540aSrobert }
1047*404b540aSrobert
1048*404b540aSrobert /* Pop the current context off the stack, re-enabling the macro if the
1049*404b540aSrobert context represented a macro's replacement list. The context
1050*404b540aSrobert structure is not freed so that we can re-use it later. */
1051*404b540aSrobert void
_cpp_pop_context(cpp_reader * pfile)1052*404b540aSrobert _cpp_pop_context (cpp_reader *pfile)
1053*404b540aSrobert {
1054*404b540aSrobert cpp_context *context = pfile->context;
1055*404b540aSrobert
1056*404b540aSrobert if (context->macro)
1057*404b540aSrobert context->macro->flags &= ~NODE_DISABLED;
1058*404b540aSrobert
1059*404b540aSrobert if (context->buff)
1060*404b540aSrobert _cpp_release_buff (pfile, context->buff);
1061*404b540aSrobert
1062*404b540aSrobert pfile->context = context->prev;
1063*404b540aSrobert }
1064*404b540aSrobert
1065*404b540aSrobert /* External routine to get a token. Also used nearly everywhere
1066*404b540aSrobert internally, except for places where we know we can safely call
1067*404b540aSrobert _cpp_lex_token directly, such as lexing a directive name.
1068*404b540aSrobert
1069*404b540aSrobert Macro expansions and directives are transparently handled,
1070*404b540aSrobert including entering included files. Thus tokens are post-macro
1071*404b540aSrobert expansion, and after any intervening directives. External callers
1072*404b540aSrobert see CPP_EOF only at EOF. Internal callers also see it when meeting
1073*404b540aSrobert a directive inside a macro call, when at the end of a directive and
1074*404b540aSrobert state.in_directive is still 1, and at the end of argument
1075*404b540aSrobert pre-expansion. */
1076*404b540aSrobert const cpp_token *
cpp_get_token(cpp_reader * pfile)1077*404b540aSrobert cpp_get_token (cpp_reader *pfile)
1078*404b540aSrobert {
1079*404b540aSrobert const cpp_token *result;
1080*404b540aSrobert
1081*404b540aSrobert for (;;)
1082*404b540aSrobert {
1083*404b540aSrobert cpp_hashnode *node;
1084*404b540aSrobert cpp_context *context = pfile->context;
1085*404b540aSrobert
1086*404b540aSrobert /* Context->prev == 0 <=> base context. */
1087*404b540aSrobert if (!context->prev)
1088*404b540aSrobert result = _cpp_lex_token (pfile);
1089*404b540aSrobert else if (FIRST (context).token != LAST (context).token)
1090*404b540aSrobert {
1091*404b540aSrobert if (context->direct_p)
1092*404b540aSrobert result = FIRST (context).token++;
1093*404b540aSrobert else
1094*404b540aSrobert result = *FIRST (context).ptoken++;
1095*404b540aSrobert
1096*404b540aSrobert if (result->flags & PASTE_LEFT)
1097*404b540aSrobert {
1098*404b540aSrobert paste_all_tokens (pfile, result);
1099*404b540aSrobert if (pfile->state.in_directive)
1100*404b540aSrobert continue;
1101*404b540aSrobert return padding_token (pfile, result);
1102*404b540aSrobert }
1103*404b540aSrobert }
1104*404b540aSrobert else
1105*404b540aSrobert {
1106*404b540aSrobert _cpp_pop_context (pfile);
1107*404b540aSrobert if (pfile->state.in_directive)
1108*404b540aSrobert continue;
1109*404b540aSrobert return &pfile->avoid_paste;
1110*404b540aSrobert }
1111*404b540aSrobert
1112*404b540aSrobert if (pfile->state.in_directive && result->type == CPP_COMMENT)
1113*404b540aSrobert continue;
1114*404b540aSrobert
1115*404b540aSrobert if (result->type != CPP_NAME)
1116*404b540aSrobert break;
1117*404b540aSrobert
1118*404b540aSrobert node = result->val.node;
1119*404b540aSrobert
1120*404b540aSrobert if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1121*404b540aSrobert break;
1122*404b540aSrobert
1123*404b540aSrobert if (!(node->flags & NODE_DISABLED))
1124*404b540aSrobert {
1125*404b540aSrobert if (!pfile->state.prevent_expansion
1126*404b540aSrobert && enter_macro_context (pfile, node))
1127*404b540aSrobert {
1128*404b540aSrobert if (pfile->state.in_directive)
1129*404b540aSrobert continue;
1130*404b540aSrobert return padding_token (pfile, result);
1131*404b540aSrobert }
1132*404b540aSrobert }
1133*404b540aSrobert else
1134*404b540aSrobert {
1135*404b540aSrobert /* Flag this token as always unexpandable. FIXME: move this
1136*404b540aSrobert to collect_args()?. */
1137*404b540aSrobert cpp_token *t = _cpp_temp_token (pfile);
1138*404b540aSrobert t->type = result->type;
1139*404b540aSrobert t->flags = result->flags | NO_EXPAND;
1140*404b540aSrobert t->val = result->val;
1141*404b540aSrobert result = t;
1142*404b540aSrobert }
1143*404b540aSrobert
1144*404b540aSrobert break;
1145*404b540aSrobert }
1146*404b540aSrobert
1147*404b540aSrobert return result;
1148*404b540aSrobert }
1149*404b540aSrobert
1150*404b540aSrobert /* Returns true if we're expanding an object-like macro that was
1151*404b540aSrobert defined in a system header. Just checks the macro at the top of
1152*404b540aSrobert the stack. Used for diagnostic suppression. */
1153*404b540aSrobert int
cpp_sys_macro_p(cpp_reader * pfile)1154*404b540aSrobert cpp_sys_macro_p (cpp_reader *pfile)
1155*404b540aSrobert {
1156*404b540aSrobert cpp_hashnode *node = pfile->context->macro;
1157*404b540aSrobert
1158*404b540aSrobert return node && node->value.macro && node->value.macro->syshdr;
1159*404b540aSrobert }
1160*404b540aSrobert
1161*404b540aSrobert /* Read each token in, until end of the current file. Directives are
1162*404b540aSrobert transparently processed. */
1163*404b540aSrobert void
cpp_scan_nooutput(cpp_reader * pfile)1164*404b540aSrobert cpp_scan_nooutput (cpp_reader *pfile)
1165*404b540aSrobert {
1166*404b540aSrobert /* Request a CPP_EOF token at the end of this file, rather than
1167*404b540aSrobert transparently continuing with the including file. */
1168*404b540aSrobert pfile->buffer->return_at_eof = true;
1169*404b540aSrobert
1170*404b540aSrobert pfile->state.discarding_output++;
1171*404b540aSrobert pfile->state.prevent_expansion++;
1172*404b540aSrobert
1173*404b540aSrobert if (CPP_OPTION (pfile, traditional))
1174*404b540aSrobert while (_cpp_read_logical_line_trad (pfile))
1175*404b540aSrobert ;
1176*404b540aSrobert else
1177*404b540aSrobert while (cpp_get_token (pfile)->type != CPP_EOF)
1178*404b540aSrobert ;
1179*404b540aSrobert
1180*404b540aSrobert pfile->state.discarding_output--;
1181*404b540aSrobert pfile->state.prevent_expansion--;
1182*404b540aSrobert }
1183*404b540aSrobert
1184*404b540aSrobert /* Step back one (or more) tokens. Can only step back more than 1 if
1185*404b540aSrobert they are from the lexer, and not from macro expansion. */
1186*404b540aSrobert void
_cpp_backup_tokens(cpp_reader * pfile,unsigned int count)1187*404b540aSrobert _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
1188*404b540aSrobert {
1189*404b540aSrobert if (pfile->context->prev == NULL)
1190*404b540aSrobert {
1191*404b540aSrobert pfile->lookaheads += count;
1192*404b540aSrobert while (count--)
1193*404b540aSrobert {
1194*404b540aSrobert pfile->cur_token--;
1195*404b540aSrobert if (pfile->cur_token == pfile->cur_run->base
1196*404b540aSrobert /* Possible with -fpreprocessed and no leading #line. */
1197*404b540aSrobert && pfile->cur_run->prev != NULL)
1198*404b540aSrobert {
1199*404b540aSrobert pfile->cur_run = pfile->cur_run->prev;
1200*404b540aSrobert pfile->cur_token = pfile->cur_run->limit;
1201*404b540aSrobert }
1202*404b540aSrobert }
1203*404b540aSrobert }
1204*404b540aSrobert else
1205*404b540aSrobert {
1206*404b540aSrobert if (count != 1)
1207*404b540aSrobert abort ();
1208*404b540aSrobert if (pfile->context->direct_p)
1209*404b540aSrobert FIRST (pfile->context).token--;
1210*404b540aSrobert else
1211*404b540aSrobert FIRST (pfile->context).ptoken--;
1212*404b540aSrobert }
1213*404b540aSrobert }
1214*404b540aSrobert
1215*404b540aSrobert /* #define directive parsing and handling. */
1216*404b540aSrobert
1217*404b540aSrobert /* Returns nonzero if a macro redefinition warning is required. */
1218*404b540aSrobert static bool
warn_of_redefinition(cpp_reader * pfile,const cpp_hashnode * node,const cpp_macro * macro2)1219*404b540aSrobert warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
1220*404b540aSrobert const cpp_macro *macro2)
1221*404b540aSrobert {
1222*404b540aSrobert const cpp_macro *macro1;
1223*404b540aSrobert unsigned int i;
1224*404b540aSrobert
1225*404b540aSrobert /* Some redefinitions need to be warned about regardless. */
1226*404b540aSrobert if (node->flags & NODE_WARN)
1227*404b540aSrobert return true;
1228*404b540aSrobert
1229*404b540aSrobert /* Redefinition of a macro is allowed if and only if the old and new
1230*404b540aSrobert definitions are the same. (6.10.3 paragraph 2). */
1231*404b540aSrobert macro1 = node->value.macro;
1232*404b540aSrobert
1233*404b540aSrobert /* Don't check count here as it can be different in valid
1234*404b540aSrobert traditional redefinitions with just whitespace differences. */
1235*404b540aSrobert if (macro1->paramc != macro2->paramc
1236*404b540aSrobert || macro1->fun_like != macro2->fun_like
1237*404b540aSrobert || macro1->variadic != macro2->variadic)
1238*404b540aSrobert return true;
1239*404b540aSrobert
1240*404b540aSrobert /* Check parameter spellings. */
1241*404b540aSrobert for (i = 0; i < macro1->paramc; i++)
1242*404b540aSrobert if (macro1->params[i] != macro2->params[i])
1243*404b540aSrobert return true;
1244*404b540aSrobert
1245*404b540aSrobert /* Check the replacement text or tokens. */
1246*404b540aSrobert if (CPP_OPTION (pfile, traditional))
1247*404b540aSrobert return _cpp_expansions_different_trad (macro1, macro2);
1248*404b540aSrobert
1249*404b540aSrobert if (macro1->count != macro2->count)
1250*404b540aSrobert return true;
1251*404b540aSrobert
1252*404b540aSrobert for (i = 0; i < macro1->count; i++)
1253*404b540aSrobert if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i]))
1254*404b540aSrobert return true;
1255*404b540aSrobert
1256*404b540aSrobert return false;
1257*404b540aSrobert }
1258*404b540aSrobert
1259*404b540aSrobert /* Free the definition of hashnode H. */
1260*404b540aSrobert void
_cpp_free_definition(cpp_hashnode * h)1261*404b540aSrobert _cpp_free_definition (cpp_hashnode *h)
1262*404b540aSrobert {
1263*404b540aSrobert /* Macros and assertions no longer have anything to free. */
1264*404b540aSrobert h->type = NT_VOID;
1265*404b540aSrobert /* Clear builtin flag in case of redefinition. */
1266*404b540aSrobert h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1267*404b540aSrobert }
1268*404b540aSrobert
1269*404b540aSrobert /* Save parameter NODE to the parameter list of macro MACRO. Returns
1270*404b540aSrobert zero on success, nonzero if the parameter is a duplicate. */
1271*404b540aSrobert bool
_cpp_save_parameter(cpp_reader * pfile,cpp_macro * macro,cpp_hashnode * node)1272*404b540aSrobert _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
1273*404b540aSrobert {
1274*404b540aSrobert unsigned int len;
1275*404b540aSrobert /* Constraint 6.10.3.6 - duplicate parameter names. */
1276*404b540aSrobert if (node->flags & NODE_MACRO_ARG)
1277*404b540aSrobert {
1278*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
1279*404b540aSrobert NODE_NAME (node));
1280*404b540aSrobert return true;
1281*404b540aSrobert }
1282*404b540aSrobert
1283*404b540aSrobert if (BUFF_ROOM (pfile->a_buff)
1284*404b540aSrobert < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1285*404b540aSrobert _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1286*404b540aSrobert
1287*404b540aSrobert ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1288*404b540aSrobert node->flags |= NODE_MACRO_ARG;
1289*404b540aSrobert len = macro->paramc * sizeof (union _cpp_hashnode_value);
1290*404b540aSrobert if (len > pfile->macro_buffer_len)
1291*404b540aSrobert {
1292*404b540aSrobert pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
1293*404b540aSrobert len);
1294*404b540aSrobert pfile->macro_buffer_len = len;
1295*404b540aSrobert }
1296*404b540aSrobert ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1297*404b540aSrobert = node->value;
1298*404b540aSrobert
1299*404b540aSrobert node->value.arg_index = macro->paramc;
1300*404b540aSrobert return false;
1301*404b540aSrobert }
1302*404b540aSrobert
1303*404b540aSrobert /* Check the syntax of the parameters in a MACRO definition. Returns
1304*404b540aSrobert false if an error occurs. */
1305*404b540aSrobert static bool
parse_params(cpp_reader * pfile,cpp_macro * macro)1306*404b540aSrobert parse_params (cpp_reader *pfile, cpp_macro *macro)
1307*404b540aSrobert {
1308*404b540aSrobert unsigned int prev_ident = 0;
1309*404b540aSrobert
1310*404b540aSrobert for (;;)
1311*404b540aSrobert {
1312*404b540aSrobert const cpp_token *token = _cpp_lex_token (pfile);
1313*404b540aSrobert
1314*404b540aSrobert switch (token->type)
1315*404b540aSrobert {
1316*404b540aSrobert default:
1317*404b540aSrobert /* Allow/ignore comments in parameter lists if we are
1318*404b540aSrobert preserving comments in macro expansions. */
1319*404b540aSrobert if (token->type == CPP_COMMENT
1320*404b540aSrobert && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1321*404b540aSrobert continue;
1322*404b540aSrobert
1323*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
1324*404b540aSrobert "\"%s\" may not appear in macro parameter list",
1325*404b540aSrobert cpp_token_as_text (pfile, token));
1326*404b540aSrobert return false;
1327*404b540aSrobert
1328*404b540aSrobert case CPP_NAME:
1329*404b540aSrobert if (prev_ident)
1330*404b540aSrobert {
1331*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
1332*404b540aSrobert "macro parameters must be comma-separated");
1333*404b540aSrobert return false;
1334*404b540aSrobert }
1335*404b540aSrobert prev_ident = 1;
1336*404b540aSrobert
1337*404b540aSrobert if (_cpp_save_parameter (pfile, macro, token->val.node))
1338*404b540aSrobert return false;
1339*404b540aSrobert continue;
1340*404b540aSrobert
1341*404b540aSrobert case CPP_CLOSE_PAREN:
1342*404b540aSrobert if (prev_ident || macro->paramc == 0)
1343*404b540aSrobert return true;
1344*404b540aSrobert
1345*404b540aSrobert /* Fall through to pick up the error. */
1346*404b540aSrobert case CPP_COMMA:
1347*404b540aSrobert if (!prev_ident)
1348*404b540aSrobert {
1349*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
1350*404b540aSrobert return false;
1351*404b540aSrobert }
1352*404b540aSrobert prev_ident = 0;
1353*404b540aSrobert continue;
1354*404b540aSrobert
1355*404b540aSrobert case CPP_ELLIPSIS:
1356*404b540aSrobert macro->variadic = 1;
1357*404b540aSrobert if (!prev_ident)
1358*404b540aSrobert {
1359*404b540aSrobert _cpp_save_parameter (pfile, macro,
1360*404b540aSrobert pfile->spec_nodes.n__VA_ARGS__);
1361*404b540aSrobert pfile->state.va_args_ok = 1;
1362*404b540aSrobert if (! CPP_OPTION (pfile, c99)
1363*404b540aSrobert && CPP_OPTION (pfile, pedantic)
1364*404b540aSrobert && CPP_OPTION (pfile, warn_variadic_macros))
1365*404b540aSrobert cpp_error (pfile, CPP_DL_PEDWARN,
1366*404b540aSrobert "anonymous variadic macros were introduced in C99");
1367*404b540aSrobert }
1368*404b540aSrobert else if (CPP_OPTION (pfile, pedantic)
1369*404b540aSrobert && CPP_OPTION (pfile, warn_variadic_macros))
1370*404b540aSrobert cpp_error (pfile, CPP_DL_PEDWARN,
1371*404b540aSrobert "ISO C does not permit named variadic macros");
1372*404b540aSrobert
1373*404b540aSrobert /* We're at the end, and just expect a closing parenthesis. */
1374*404b540aSrobert token = _cpp_lex_token (pfile);
1375*404b540aSrobert if (token->type == CPP_CLOSE_PAREN)
1376*404b540aSrobert return true;
1377*404b540aSrobert /* Fall through. */
1378*404b540aSrobert
1379*404b540aSrobert case CPP_EOF:
1380*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
1381*404b540aSrobert return false;
1382*404b540aSrobert }
1383*404b540aSrobert }
1384*404b540aSrobert }
1385*404b540aSrobert
1386*404b540aSrobert /* Allocate room for a token from a macro's replacement list. */
1387*404b540aSrobert static cpp_token *
alloc_expansion_token(cpp_reader * pfile,cpp_macro * macro)1388*404b540aSrobert alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1389*404b540aSrobert {
1390*404b540aSrobert if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1391*404b540aSrobert _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1392*404b540aSrobert
1393*404b540aSrobert return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1394*404b540aSrobert }
1395*404b540aSrobert
1396*404b540aSrobert /* Lex a token from the expansion of MACRO, but mark parameters as we
1397*404b540aSrobert find them and warn of traditional stringification. */
1398*404b540aSrobert static cpp_token *
lex_expansion_token(cpp_reader * pfile,cpp_macro * macro)1399*404b540aSrobert lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1400*404b540aSrobert {
1401*404b540aSrobert cpp_token *token;
1402*404b540aSrobert
1403*404b540aSrobert pfile->cur_token = alloc_expansion_token (pfile, macro);
1404*404b540aSrobert token = _cpp_lex_direct (pfile);
1405*404b540aSrobert
1406*404b540aSrobert /* Is this a parameter? */
1407*404b540aSrobert if (token->type == CPP_NAME
1408*404b540aSrobert && (token->val.node->flags & NODE_MACRO_ARG) != 0)
1409*404b540aSrobert {
1410*404b540aSrobert token->type = CPP_MACRO_ARG;
1411*404b540aSrobert token->val.arg_no = token->val.node->value.arg_index;
1412*404b540aSrobert }
1413*404b540aSrobert else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1414*404b540aSrobert && (token->type == CPP_STRING || token->type == CPP_CHAR))
1415*404b540aSrobert check_trad_stringification (pfile, macro, &token->val.str);
1416*404b540aSrobert
1417*404b540aSrobert return token;
1418*404b540aSrobert }
1419*404b540aSrobert
1420*404b540aSrobert static bool
create_iso_definition(cpp_reader * pfile,cpp_macro * macro)1421*404b540aSrobert create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
1422*404b540aSrobert {
1423*404b540aSrobert cpp_token *token;
1424*404b540aSrobert const cpp_token *ctoken;
1425*404b540aSrobert
1426*404b540aSrobert /* Get the first token of the expansion (or the '(' of a
1427*404b540aSrobert function-like macro). */
1428*404b540aSrobert ctoken = _cpp_lex_token (pfile);
1429*404b540aSrobert
1430*404b540aSrobert if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1431*404b540aSrobert {
1432*404b540aSrobert bool ok = parse_params (pfile, macro);
1433*404b540aSrobert macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1434*404b540aSrobert if (!ok)
1435*404b540aSrobert return false;
1436*404b540aSrobert
1437*404b540aSrobert /* Success. Commit or allocate the parameter array. */
1438*404b540aSrobert if (pfile->hash_table->alloc_subobject)
1439*404b540aSrobert {
1440*404b540aSrobert cpp_hashnode **params =
1441*404b540aSrobert (cpp_hashnode **) pfile->hash_table->alloc_subobject
1442*404b540aSrobert (sizeof (cpp_hashnode *) * macro->paramc);
1443*404b540aSrobert memcpy (params, macro->params,
1444*404b540aSrobert sizeof (cpp_hashnode *) * macro->paramc);
1445*404b540aSrobert macro->params = params;
1446*404b540aSrobert }
1447*404b540aSrobert else
1448*404b540aSrobert BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc];
1449*404b540aSrobert macro->fun_like = 1;
1450*404b540aSrobert }
1451*404b540aSrobert else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1452*404b540aSrobert {
1453*404b540aSrobert /* While ISO C99 requires whitespace before replacement text
1454*404b540aSrobert in a macro definition, ISO C90 with TC1 allows there characters
1455*404b540aSrobert from the basic source character set. */
1456*404b540aSrobert if (CPP_OPTION (pfile, c99))
1457*404b540aSrobert cpp_error (pfile, CPP_DL_PEDWARN,
1458*404b540aSrobert "ISO C99 requires whitespace after the macro name");
1459*404b540aSrobert else
1460*404b540aSrobert {
1461*404b540aSrobert int warntype = CPP_DL_WARNING;
1462*404b540aSrobert switch (ctoken->type)
1463*404b540aSrobert {
1464*404b540aSrobert case CPP_ATSIGN:
1465*404b540aSrobert case CPP_AT_NAME:
1466*404b540aSrobert case CPP_OBJC_STRING:
1467*404b540aSrobert /* '@' is not in basic character set. */
1468*404b540aSrobert warntype = CPP_DL_PEDWARN;
1469*404b540aSrobert break;
1470*404b540aSrobert case CPP_OTHER:
1471*404b540aSrobert /* Basic character set sans letters, digits and _. */
1472*404b540aSrobert if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
1473*404b540aSrobert ctoken->val.str.text[0]) == NULL)
1474*404b540aSrobert warntype = CPP_DL_PEDWARN;
1475*404b540aSrobert break;
1476*404b540aSrobert default:
1477*404b540aSrobert /* All other tokens start with a character from basic
1478*404b540aSrobert character set. */
1479*404b540aSrobert break;
1480*404b540aSrobert }
1481*404b540aSrobert cpp_error (pfile, warntype,
1482*404b540aSrobert "missing whitespace after the macro name");
1483*404b540aSrobert }
1484*404b540aSrobert }
1485*404b540aSrobert
1486*404b540aSrobert if (macro->fun_like)
1487*404b540aSrobert token = lex_expansion_token (pfile, macro);
1488*404b540aSrobert else
1489*404b540aSrobert {
1490*404b540aSrobert token = alloc_expansion_token (pfile, macro);
1491*404b540aSrobert *token = *ctoken;
1492*404b540aSrobert }
1493*404b540aSrobert
1494*404b540aSrobert for (;;)
1495*404b540aSrobert {
1496*404b540aSrobert /* Check the stringifying # constraint 6.10.3.2.1 of
1497*404b540aSrobert function-like macros when lexing the subsequent token. */
1498*404b540aSrobert if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1499*404b540aSrobert {
1500*404b540aSrobert if (token->type == CPP_MACRO_ARG)
1501*404b540aSrobert {
1502*404b540aSrobert token->flags &= ~PREV_WHITE;
1503*404b540aSrobert token->flags |= STRINGIFY_ARG;
1504*404b540aSrobert token->flags |= token[-1].flags & PREV_WHITE;
1505*404b540aSrobert token[-1] = token[0];
1506*404b540aSrobert macro->count--;
1507*404b540aSrobert }
1508*404b540aSrobert /* Let assembler get away with murder. */
1509*404b540aSrobert else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1510*404b540aSrobert {
1511*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
1512*404b540aSrobert "'#' is not followed by a macro parameter");
1513*404b540aSrobert return false;
1514*404b540aSrobert }
1515*404b540aSrobert }
1516*404b540aSrobert
1517*404b540aSrobert if (token->type == CPP_EOF)
1518*404b540aSrobert break;
1519*404b540aSrobert
1520*404b540aSrobert /* Paste operator constraint 6.10.3.3.1. */
1521*404b540aSrobert if (token->type == CPP_PASTE)
1522*404b540aSrobert {
1523*404b540aSrobert /* Token-paste ##, can appear in both object-like and
1524*404b540aSrobert function-like macros, but not at the ends. */
1525*404b540aSrobert if (--macro->count > 0)
1526*404b540aSrobert token = lex_expansion_token (pfile, macro);
1527*404b540aSrobert
1528*404b540aSrobert if (macro->count == 0 || token->type == CPP_EOF)
1529*404b540aSrobert {
1530*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
1531*404b540aSrobert "'##' cannot appear at either end of a macro expansion");
1532*404b540aSrobert return false;
1533*404b540aSrobert }
1534*404b540aSrobert
1535*404b540aSrobert token[-1].flags |= PASTE_LEFT;
1536*404b540aSrobert }
1537*404b540aSrobert
1538*404b540aSrobert token = lex_expansion_token (pfile, macro);
1539*404b540aSrobert }
1540*404b540aSrobert
1541*404b540aSrobert macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1542*404b540aSrobert macro->traditional = 0;
1543*404b540aSrobert
1544*404b540aSrobert /* Don't count the CPP_EOF. */
1545*404b540aSrobert macro->count--;
1546*404b540aSrobert
1547*404b540aSrobert /* Clear whitespace on first token for warn_of_redefinition(). */
1548*404b540aSrobert if (macro->count)
1549*404b540aSrobert macro->exp.tokens[0].flags &= ~PREV_WHITE;
1550*404b540aSrobert
1551*404b540aSrobert /* Commit or allocate the memory. */
1552*404b540aSrobert if (pfile->hash_table->alloc_subobject)
1553*404b540aSrobert {
1554*404b540aSrobert cpp_token *tokns =
1555*404b540aSrobert (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
1556*404b540aSrobert * macro->count);
1557*404b540aSrobert memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
1558*404b540aSrobert macro->exp.tokens = tokns;
1559*404b540aSrobert }
1560*404b540aSrobert else
1561*404b540aSrobert BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count];
1562*404b540aSrobert
1563*404b540aSrobert return true;
1564*404b540aSrobert }
1565*404b540aSrobert
1566*404b540aSrobert /* Parse a macro and save its expansion. Returns nonzero on success. */
1567*404b540aSrobert bool
_cpp_create_definition(cpp_reader * pfile,cpp_hashnode * node)1568*404b540aSrobert _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
1569*404b540aSrobert {
1570*404b540aSrobert cpp_macro *macro;
1571*404b540aSrobert unsigned int i;
1572*404b540aSrobert bool ok;
1573*404b540aSrobert
1574*404b540aSrobert if (pfile->hash_table->alloc_subobject)
1575*404b540aSrobert macro = (cpp_macro *) pfile->hash_table->alloc_subobject
1576*404b540aSrobert (sizeof (cpp_macro));
1577*404b540aSrobert else
1578*404b540aSrobert macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1579*404b540aSrobert macro->line = pfile->directive_line;
1580*404b540aSrobert macro->params = 0;
1581*404b540aSrobert macro->paramc = 0;
1582*404b540aSrobert macro->variadic = 0;
1583*404b540aSrobert macro->used = !CPP_OPTION (pfile, warn_unused_macros);
1584*404b540aSrobert macro->count = 0;
1585*404b540aSrobert macro->fun_like = 0;
1586*404b540aSrobert /* To suppress some diagnostics. */
1587*404b540aSrobert macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
1588*404b540aSrobert
1589*404b540aSrobert if (CPP_OPTION (pfile, traditional))
1590*404b540aSrobert ok = _cpp_create_trad_definition (pfile, macro);
1591*404b540aSrobert else
1592*404b540aSrobert {
1593*404b540aSrobert cpp_token *saved_cur_token = pfile->cur_token;
1594*404b540aSrobert
1595*404b540aSrobert ok = create_iso_definition (pfile, macro);
1596*404b540aSrobert
1597*404b540aSrobert /* Restore lexer position because of games lex_expansion_token()
1598*404b540aSrobert plays lexing the macro. We set the type for SEEN_EOL() in
1599*404b540aSrobert directives.c.
1600*404b540aSrobert
1601*404b540aSrobert Longer term we should lex the whole line before coming here,
1602*404b540aSrobert and just copy the expansion. */
1603*404b540aSrobert saved_cur_token[-1].type = pfile->cur_token[-1].type;
1604*404b540aSrobert pfile->cur_token = saved_cur_token;
1605*404b540aSrobert
1606*404b540aSrobert /* Stop the lexer accepting __VA_ARGS__. */
1607*404b540aSrobert pfile->state.va_args_ok = 0;
1608*404b540aSrobert }
1609*404b540aSrobert
1610*404b540aSrobert /* Clear the fast argument lookup indices. */
1611*404b540aSrobert for (i = macro->paramc; i-- > 0; )
1612*404b540aSrobert {
1613*404b540aSrobert struct cpp_hashnode *node = macro->params[i];
1614*404b540aSrobert node->flags &= ~ NODE_MACRO_ARG;
1615*404b540aSrobert node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1616*404b540aSrobert }
1617*404b540aSrobert
1618*404b540aSrobert if (!ok)
1619*404b540aSrobert return ok;
1620*404b540aSrobert
1621*404b540aSrobert if (node->type == NT_MACRO)
1622*404b540aSrobert {
1623*404b540aSrobert if (CPP_OPTION (pfile, warn_unused_macros))
1624*404b540aSrobert _cpp_warn_if_unused_macro (pfile, node, NULL);
1625*404b540aSrobert
1626*404b540aSrobert if (warn_of_redefinition (pfile, node, macro))
1627*404b540aSrobert {
1628*404b540aSrobert cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
1629*404b540aSrobert "\"%s\" redefined", NODE_NAME (node));
1630*404b540aSrobert
1631*404b540aSrobert if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1632*404b540aSrobert cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1633*404b540aSrobert node->value.macro->line, 0,
1634*404b540aSrobert "this is the location of the previous definition");
1635*404b540aSrobert }
1636*404b540aSrobert }
1637*404b540aSrobert
1638*404b540aSrobert if (node->type != NT_VOID)
1639*404b540aSrobert _cpp_free_definition (node);
1640*404b540aSrobert
1641*404b540aSrobert /* Enter definition in hash table. */
1642*404b540aSrobert node->type = NT_MACRO;
1643*404b540aSrobert node->value.macro = macro;
1644*404b540aSrobert if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1645*404b540aSrobert node->flags |= NODE_WARN;
1646*404b540aSrobert
1647*404b540aSrobert return ok;
1648*404b540aSrobert }
1649*404b540aSrobert
1650*404b540aSrobert /* Warn if a token in STRING matches one of a function-like MACRO's
1651*404b540aSrobert parameters. */
1652*404b540aSrobert static void
check_trad_stringification(cpp_reader * pfile,const cpp_macro * macro,const cpp_string * string)1653*404b540aSrobert check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1654*404b540aSrobert const cpp_string *string)
1655*404b540aSrobert {
1656*404b540aSrobert unsigned int i, len;
1657*404b540aSrobert const uchar *p, *q, *limit;
1658*404b540aSrobert
1659*404b540aSrobert /* Loop over the string. */
1660*404b540aSrobert limit = string->text + string->len - 1;
1661*404b540aSrobert for (p = string->text + 1; p < limit; p = q)
1662*404b540aSrobert {
1663*404b540aSrobert /* Find the start of an identifier. */
1664*404b540aSrobert while (p < limit && !is_idstart (*p))
1665*404b540aSrobert p++;
1666*404b540aSrobert
1667*404b540aSrobert /* Find the end of the identifier. */
1668*404b540aSrobert q = p;
1669*404b540aSrobert while (q < limit && is_idchar (*q))
1670*404b540aSrobert q++;
1671*404b540aSrobert
1672*404b540aSrobert len = q - p;
1673*404b540aSrobert
1674*404b540aSrobert /* Loop over the function macro arguments to see if the
1675*404b540aSrobert identifier inside the string matches one of them. */
1676*404b540aSrobert for (i = 0; i < macro->paramc; i++)
1677*404b540aSrobert {
1678*404b540aSrobert const cpp_hashnode *node = macro->params[i];
1679*404b540aSrobert
1680*404b540aSrobert if (NODE_LEN (node) == len
1681*404b540aSrobert && !memcmp (p, NODE_NAME (node), len))
1682*404b540aSrobert {
1683*404b540aSrobert cpp_error (pfile, CPP_DL_WARNING,
1684*404b540aSrobert "macro argument \"%s\" would be stringified in traditional C",
1685*404b540aSrobert NODE_NAME (node));
1686*404b540aSrobert break;
1687*404b540aSrobert }
1688*404b540aSrobert }
1689*404b540aSrobert }
1690*404b540aSrobert }
1691*404b540aSrobert
1692*404b540aSrobert /* Returns the name, arguments and expansion of a macro, in a format
1693*404b540aSrobert suitable to be read back in again, and therefore also for DWARF 2
1694*404b540aSrobert debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1695*404b540aSrobert Caller is expected to generate the "#define" bit if needed. The
1696*404b540aSrobert returned text is temporary, and automatically freed later. */
1697*404b540aSrobert const unsigned char *
cpp_macro_definition(cpp_reader * pfile,const cpp_hashnode * node)1698*404b540aSrobert cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
1699*404b540aSrobert {
1700*404b540aSrobert unsigned int i, len;
1701*404b540aSrobert const cpp_macro *macro = node->value.macro;
1702*404b540aSrobert unsigned char *buffer;
1703*404b540aSrobert
1704*404b540aSrobert if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1705*404b540aSrobert {
1706*404b540aSrobert cpp_error (pfile, CPP_DL_ICE,
1707*404b540aSrobert "invalid hash type %d in cpp_macro_definition", node->type);
1708*404b540aSrobert return 0;
1709*404b540aSrobert }
1710*404b540aSrobert
1711*404b540aSrobert /* Calculate length. */
1712*404b540aSrobert len = NODE_LEN (node) + 2; /* ' ' and NUL. */
1713*404b540aSrobert if (macro->fun_like)
1714*404b540aSrobert {
1715*404b540aSrobert len += 4; /* "()" plus possible final ".." of named
1716*404b540aSrobert varargs (we have + 1 below). */
1717*404b540aSrobert for (i = 0; i < macro->paramc; i++)
1718*404b540aSrobert len += NODE_LEN (macro->params[i]) + 1; /* "," */
1719*404b540aSrobert }
1720*404b540aSrobert
1721*404b540aSrobert /* This should match below where we fill in the buffer. */
1722*404b540aSrobert if (CPP_OPTION (pfile, traditional))
1723*404b540aSrobert len += _cpp_replacement_text_len (macro);
1724*404b540aSrobert else
1725*404b540aSrobert {
1726*404b540aSrobert for (i = 0; i < macro->count; i++)
1727*404b540aSrobert {
1728*404b540aSrobert cpp_token *token = ¯o->exp.tokens[i];
1729*404b540aSrobert
1730*404b540aSrobert if (token->type == CPP_MACRO_ARG)
1731*404b540aSrobert len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1732*404b540aSrobert else
1733*404b540aSrobert len += cpp_token_len (token);
1734*404b540aSrobert
1735*404b540aSrobert if (token->flags & STRINGIFY_ARG)
1736*404b540aSrobert len++; /* "#" */
1737*404b540aSrobert if (token->flags & PASTE_LEFT)
1738*404b540aSrobert len += 3; /* " ##" */
1739*404b540aSrobert if (token->flags & PREV_WHITE)
1740*404b540aSrobert len++; /* " " */
1741*404b540aSrobert }
1742*404b540aSrobert }
1743*404b540aSrobert
1744*404b540aSrobert if (len > pfile->macro_buffer_len)
1745*404b540aSrobert {
1746*404b540aSrobert pfile->macro_buffer = XRESIZEVEC (unsigned char,
1747*404b540aSrobert pfile->macro_buffer, len);
1748*404b540aSrobert pfile->macro_buffer_len = len;
1749*404b540aSrobert }
1750*404b540aSrobert
1751*404b540aSrobert /* Fill in the buffer. Start with the macro name. */
1752*404b540aSrobert buffer = pfile->macro_buffer;
1753*404b540aSrobert memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1754*404b540aSrobert buffer += NODE_LEN (node);
1755*404b540aSrobert
1756*404b540aSrobert /* Parameter names. */
1757*404b540aSrobert if (macro->fun_like)
1758*404b540aSrobert {
1759*404b540aSrobert *buffer++ = '(';
1760*404b540aSrobert for (i = 0; i < macro->paramc; i++)
1761*404b540aSrobert {
1762*404b540aSrobert cpp_hashnode *param = macro->params[i];
1763*404b540aSrobert
1764*404b540aSrobert if (param != pfile->spec_nodes.n__VA_ARGS__)
1765*404b540aSrobert {
1766*404b540aSrobert memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1767*404b540aSrobert buffer += NODE_LEN (param);
1768*404b540aSrobert }
1769*404b540aSrobert
1770*404b540aSrobert if (i + 1 < macro->paramc)
1771*404b540aSrobert /* Don't emit a space after the comma here; we're trying
1772*404b540aSrobert to emit a Dwarf-friendly definition, and the Dwarf spec
1773*404b540aSrobert forbids spaces in the argument list. */
1774*404b540aSrobert *buffer++ = ',';
1775*404b540aSrobert else if (macro->variadic)
1776*404b540aSrobert *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1777*404b540aSrobert }
1778*404b540aSrobert *buffer++ = ')';
1779*404b540aSrobert }
1780*404b540aSrobert
1781*404b540aSrobert /* The Dwarf spec requires a space after the macro name, even if the
1782*404b540aSrobert definition is the empty string. */
1783*404b540aSrobert *buffer++ = ' ';
1784*404b540aSrobert
1785*404b540aSrobert if (CPP_OPTION (pfile, traditional))
1786*404b540aSrobert buffer = _cpp_copy_replacement_text (macro, buffer);
1787*404b540aSrobert else if (macro->count)
1788*404b540aSrobert /* Expansion tokens. */
1789*404b540aSrobert {
1790*404b540aSrobert for (i = 0; i < macro->count; i++)
1791*404b540aSrobert {
1792*404b540aSrobert cpp_token *token = ¯o->exp.tokens[i];
1793*404b540aSrobert
1794*404b540aSrobert if (token->flags & PREV_WHITE)
1795*404b540aSrobert *buffer++ = ' ';
1796*404b540aSrobert if (token->flags & STRINGIFY_ARG)
1797*404b540aSrobert *buffer++ = '#';
1798*404b540aSrobert
1799*404b540aSrobert if (token->type == CPP_MACRO_ARG)
1800*404b540aSrobert {
1801*404b540aSrobert memcpy (buffer,
1802*404b540aSrobert NODE_NAME (macro->params[token->val.arg_no - 1]),
1803*404b540aSrobert NODE_LEN (macro->params[token->val.arg_no - 1]));
1804*404b540aSrobert buffer += NODE_LEN (macro->params[token->val.arg_no - 1]);
1805*404b540aSrobert }
1806*404b540aSrobert else
1807*404b540aSrobert buffer = cpp_spell_token (pfile, token, buffer, false);
1808*404b540aSrobert
1809*404b540aSrobert if (token->flags & PASTE_LEFT)
1810*404b540aSrobert {
1811*404b540aSrobert *buffer++ = ' ';
1812*404b540aSrobert *buffer++ = '#';
1813*404b540aSrobert *buffer++ = '#';
1814*404b540aSrobert /* Next has PREV_WHITE; see _cpp_create_definition. */
1815*404b540aSrobert }
1816*404b540aSrobert }
1817*404b540aSrobert }
1818*404b540aSrobert
1819*404b540aSrobert *buffer = '\0';
1820*404b540aSrobert return pfile->macro_buffer;
1821*404b540aSrobert }
1822