blob: 207085175ea4fedbf030194622e322fceab9dd45 [file] [log] [blame]
Neil Booth93c803682000-10-28 17:59:06 +00001/* Part of CPP library. (Macro and #define handling.)
Zack Weinberg711b8822000-07-18 00:59:49 +00002 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
Neil Booth5d8ebbd2002-01-03 21:43:09 +00003 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
Zack Weinberg711b8822000-07-18 00:59:49 +00004 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
25
26#include "config.h"
27#include "system.h"
28#include "cpplib.h"
29#include "cpphash.h"
30
Neil Booth93c803682000-10-28 17:59:06 +000031struct cpp_macro
Zack Weinberg711b8822000-07-18 00:59:49 +000032{
Neil Booth93c803682000-10-28 17:59:06 +000033 cpp_hashnode **params; /* Parameters, if any. */
Kazu Hirata6d2f8882001-10-10 11:33:39 +000034 cpp_token *expansion; /* First token of replacement list. */
Neil Booth93c803682000-10-28 17:59:06 +000035 unsigned int line; /* Starting line number. */
36 unsigned int count; /* Number of tokens in expansion. */
37 unsigned short paramc; /* Number of parameters. */
38 unsigned int fun_like : 1; /* If a function-like macro. */
Neil Booth28e0f042000-12-09 12:06:37 +000039 unsigned int variadic : 1; /* If a variadic macro. */
Neil Booth7065e132001-02-14 07:38:20 +000040 unsigned int syshdr : 1; /* If macro defined in system header. */
Zack Weinberg711b8822000-07-18 00:59:49 +000041};
42
Neil Booth93c803682000-10-28 17:59:06 +000043typedef struct macro_arg macro_arg;
44struct macro_arg
45{
Neil Booth4ed5bcf2001-09-24 22:53:12 +000046 const cpp_token **first; /* First token in unexpanded argument. */
Kazu Hirata6d2f8882001-10-10 11:33:39 +000047 const cpp_token **expanded; /* Macro-expanded argument. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +000048 const cpp_token *stringified; /* Stringified argument. */
Neil Booth93c803682000-10-28 17:59:06 +000049 unsigned int count; /* # of tokens in argument. */
50 unsigned int expanded_count; /* # of tokens in expanded argument. */
51};
Zack Weinberg711b8822000-07-18 00:59:49 +000052
Neil Booth93c803682000-10-28 17:59:06 +000053/* Macro expansion. */
54
Neil Booth29b10742000-11-13 18:40:37 +000055static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
Neil Booth644edda2001-10-02 12:57:24 +000056static int builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
Neil Booth4ed5bcf2001-09-24 22:53:12 +000057static void push_token_context
Neil Booth644edda2001-10-02 12:57:24 +000058 PARAMS ((cpp_reader *, cpp_hashnode *, const cpp_token *, unsigned int));
Neil Booth4ed5bcf2001-09-24 22:53:12 +000059static void push_ptoken_context
Neil Booth644edda2001-10-02 12:57:24 +000060 PARAMS ((cpp_reader *, cpp_hashnode *, _cpp_buff *,
Neil Booth1e013d22001-09-26 21:44:35 +000061 const cpp_token **, unsigned int));
Neil Boothb8af0ca2001-09-26 17:52:50 +000062static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +000063static cpp_context *next_context PARAMS ((cpp_reader *));
Neil Booth4ed5bcf2001-09-24 22:53:12 +000064static const cpp_token *padding_token
65 PARAMS ((cpp_reader *, const cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +000066static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
Neil Booth4ed5bcf2001-09-24 22:53:12 +000067static const cpp_token *new_string_token PARAMS ((cpp_reader *, U_CHAR *,
68 unsigned int));
Neil Boothd15a58c2002-01-03 18:32:55 +000069static const cpp_token *new_number_token PARAMS ((cpp_reader *, unsigned int));
Neil Booth4ed5bcf2001-09-24 22:53:12 +000070static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
71static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
Neil Boothc9e7a602001-09-27 12:59:38 +000072static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
73 const cpp_token *));
Neil Boothe808ec92002-02-27 07:24:53 +000074static void replace_args PARAMS ((cpp_reader *, cpp_hashnode *, cpp_macro *,
75 macro_arg *));
Neil Boothd6da8362001-10-08 06:15:14 +000076static _cpp_buff *funlike_invocation_p PARAMS ((cpp_reader *, cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +000077
Neil Booth93c803682000-10-28 17:59:06 +000078/* #define directive parsing and handling. */
79
Neil Booth14baae02001-09-17 18:26:12 +000080static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
Neil Booth93c803682000-10-28 17:59:06 +000081static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
Neil Booth4d6baaf2001-11-26 23:44:54 +000082static int warn_of_redefinition PARAMS ((const cpp_hashnode *,
Neil Booth618cdda2001-02-25 09:43:03 +000083 const cpp_macro *));
Neil Booth93c803682000-10-28 17:59:06 +000084static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
85static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +000086static void check_trad_stringification PARAMS ((cpp_reader *,
Neil Booth93c803682000-10-28 17:59:06 +000087 const cpp_macro *,
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +000088 const cpp_string *));
Zack Weinberg711b8822000-07-18 00:59:49 +000089
Neil Booth4ed5bcf2001-09-24 22:53:12 +000090/* Allocates and returns a CPP_STRING token, containing TEXT of length
91 LEN, after null-terminating it. TEXT must be in permanent storage. */
92static const cpp_token *
93new_string_token (pfile, text, len)
94 cpp_reader *pfile;
95 unsigned char *text;
Neil Booth93c803682000-10-28 17:59:06 +000096 unsigned int len;
Zack Weinberg711b8822000-07-18 00:59:49 +000097{
Neil Booth4ed5bcf2001-09-24 22:53:12 +000098 cpp_token *token = _cpp_temp_token (pfile);
Zack Weinberg711b8822000-07-18 00:59:49 +000099
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000100 text[len] = '\0';
Neil Booth93c803682000-10-28 17:59:06 +0000101 token->type = CPP_STRING;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000102 token->val.str.len = len;
103 token->val.str.text = text;
Neil Booth93c803682000-10-28 17:59:06 +0000104 token->flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000105 return token;
Neil Booth93c803682000-10-28 17:59:06 +0000106}
107
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000108/* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */
109static const cpp_token *
110new_number_token (pfile, number)
Neil Booth93c803682000-10-28 17:59:06 +0000111 cpp_reader *pfile;
Neil Boothd15a58c2002-01-03 18:32:55 +0000112 unsigned int number;
Neil Booth93c803682000-10-28 17:59:06 +0000113{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000114 cpp_token *token = _cpp_temp_token (pfile);
Neil Boothd15a58c2002-01-03 18:32:55 +0000115 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
116 unsigned char *buf = _cpp_unaligned_alloc (pfile, 21);
Neil Booth93c803682000-10-28 17:59:06 +0000117
Neil Boothd15a58c2002-01-03 18:32:55 +0000118 sprintf ((char *) buf, "%u", number);
Neil Booth93c803682000-10-28 17:59:06 +0000119 token->type = CPP_NUMBER;
120 token->val.str.text = buf;
121 token->val.str.len = ustrlen (buf);
122 token->flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000123 return token;
Neil Booth93c803682000-10-28 17:59:06 +0000124}
125
126static const char * const monthnames[] =
127{
128 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
129 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
130};
131
Neil Booth644edda2001-10-02 12:57:24 +0000132/* Handle builtin macros like __FILE__, and push the resulting token
133 on the context stack. Also handles _Pragma, for which no new token
Neil Boothd15a58c2002-01-03 18:32:55 +0000134 is created. Returns 1 if it generates a new token context, 0 to
135 return the token to the caller. */
Neil Booth644edda2001-10-02 12:57:24 +0000136static int
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000137builtin_macro (pfile, node)
Neil Booth93c803682000-10-28 17:59:06 +0000138 cpp_reader *pfile;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000139 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +0000140{
Neil Booth644edda2001-10-02 12:57:24 +0000141 const cpp_token *result;
142
Neil Booth93c803682000-10-28 17:59:06 +0000143 switch (node->value.builtin)
144 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000145 default:
Neil Boothebef4e82002-04-14 18:42:47 +0000146 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
147 NODE_NAME (node));
Neil Booth644edda2001-10-02 12:57:24 +0000148 return 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000149
Neil Booth93c803682000-10-28 17:59:06 +0000150 case BT_FILE:
151 case BT_BASE_FILE:
Zack Weinberg711b8822000-07-18 00:59:49 +0000152 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000153 unsigned int len;
Neil Booth0bda4762000-12-11 07:45:16 +0000154 const char *name;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000155 U_CHAR *buf;
Neil Boothbb74c962001-08-17 22:23:49 +0000156 const struct line_map *map = pfile->map;
Neil Booth93c803682000-10-28 17:59:06 +0000157
Neil Booth0bda4762000-12-11 07:45:16 +0000158 if (node->value.builtin == BT_BASE_FILE)
Neil Boothbb74c962001-08-17 22:23:49 +0000159 while (! MAIN_FILE_P (map))
160 map = INCLUDED_FROM (&pfile->line_maps, map);
Neil Booth93c803682000-10-28 17:59:06 +0000161
Neil Boothbb74c962001-08-17 22:23:49 +0000162 name = map->to_file;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000163 len = strlen (name);
Neil Boothece54d52001-09-28 09:40:22 +0000164 buf = _cpp_unaligned_alloc (pfile, len * 4 + 1);
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000165 len = cpp_quote_string (buf, (const unsigned char *) name, len) - buf;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000166
Neil Booth644edda2001-10-02 12:57:24 +0000167 result = new_string_token (pfile, buf, len);
Zack Weinberg711b8822000-07-18 00:59:49 +0000168 }
Neil Booth644edda2001-10-02 12:57:24 +0000169 break;
170
Neil Booth93c803682000-10-28 17:59:06 +0000171 case BT_INCLUDE_LEVEL:
Neil Boothd8693c62001-08-21 23:05:12 +0000172 /* The line map depth counts the primary source as level 1, but
173 historically __INCLUDE_DEPTH__ has called the primary source
174 level 0. */
Neil Booth644edda2001-10-02 12:57:24 +0000175 result = new_number_token (pfile, pfile->line_maps.depth - 1);
176 break;
Neil Booth93c803682000-10-28 17:59:06 +0000177
178 case BT_SPECLINE:
179 /* If __LINE__ is embedded in a macro, it must expand to the
180 line of the macro's invocation, not its definition.
181 Otherwise things like assert() will not work properly. */
Neil Booth644edda2001-10-02 12:57:24 +0000182 result = new_number_token (pfile,
183 SOURCE_LINE (pfile->map,
184 pfile->cur_token[-1].line));
185 break;
Neil Booth93c803682000-10-28 17:59:06 +0000186
187 case BT_STDC:
188 {
Neil Booth618cdda2001-02-25 09:43:03 +0000189 int stdc = (!CPP_IN_SYSTEM_HEADER (pfile)
190 || pfile->spec_nodes.n__STRICT_ANSI__->type != NT_VOID);
Neil Booth644edda2001-10-02 12:57:24 +0000191 result = new_number_token (pfile, stdc);
Neil Booth93c803682000-10-28 17:59:06 +0000192 }
Neil Booth644edda2001-10-02 12:57:24 +0000193 break;
Neil Booth93c803682000-10-28 17:59:06 +0000194
195 case BT_DATE:
196 case BT_TIME:
197 if (pfile->date.type == CPP_EOF)
198 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000199 /* Allocate __DATE__ and __TIME__ strings from permanent
200 storage. We only do this once, and don't generate them
201 at init time, because time() and localtime() are very
202 slow on some systems. */
Neil Booth93c803682000-10-28 17:59:06 +0000203 time_t tt = time (NULL);
204 struct tm *tb = localtime (&tt);
205
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000206 pfile->date.val.str.text =
Neil Boothece54d52001-09-28 09:40:22 +0000207 _cpp_unaligned_alloc (pfile, sizeof ("Oct 11 1347"));
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000208 pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
209 pfile->date.type = CPP_STRING;
210 pfile->date.flags = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000211 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
212 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000213
214 pfile->time.val.str.text =
Neil Boothece54d52001-09-28 09:40:22 +0000215 _cpp_unaligned_alloc (pfile, sizeof ("12:34:56"));
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000216 pfile->time.val.str.len = sizeof ("12:34:56") - 1;
217 pfile->time.type = CPP_STRING;
218 pfile->time.flags = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000219 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
220 tb->tm_hour, tb->tm_min, tb->tm_sec);
221 }
Neil Booth93c803682000-10-28 17:59:06 +0000222
Neil Booth644edda2001-10-02 12:57:24 +0000223 if (node->value.builtin == BT_DATE)
224 result = &pfile->date;
225 else
226 result = &pfile->time;
227 break;
228
229 case BT_PRAGMA:
230 /* Don't interpret _Pragma within directives. The standard is
231 not clear on this, but to me this makes most sense. */
232 if (pfile->state.in_directive)
233 return 0;
234
235 _cpp_do__Pragma (pfile);
236 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000237 }
Neil Booth644edda2001-10-02 12:57:24 +0000238
239 push_token_context (pfile, NULL, result, 1);
240 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000241}
242
Neil Boothd15a58c2002-01-03 18:32:55 +0000243/* Copies SRC, of length LEN, to DEST, adding backslashes before all
244 backslashes and double quotes. Non-printable characters are
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000245 converted to octal. DEST must be of sufficient size. Returns
246 a pointer to the end of the string. */
247U_CHAR *
248cpp_quote_string (dest, src, len)
Neil Booth93c803682000-10-28 17:59:06 +0000249 U_CHAR *dest;
250 const U_CHAR *src;
251 unsigned int len;
252{
253 while (len--)
254 {
255 U_CHAR c = *src++;
256
257 if (c == '\\' || c == '"')
258 {
259 *dest++ = '\\';
260 *dest++ = c;
261 }
262 else
263 {
264 if (ISPRINT (c))
265 *dest++ = c;
266 else
267 {
268 sprintf ((char *) dest, "\\%03o", c);
269 dest += 4;
270 }
271 }
272 }
273
274 return dest;
275}
276
Neil Boothd15a58c2002-01-03 18:32:55 +0000277/* Convert a token sequence ARG to a single string token according to
278 the rules of the ISO C #-operator. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000279static const cpp_token *
Neil Booth93c803682000-10-28 17:59:06 +0000280stringify_arg (pfile, arg)
281 cpp_reader *pfile;
282 macro_arg *arg;
283{
Neil Boothece54d52001-09-28 09:40:22 +0000284 unsigned char *dest = BUFF_FRONT (pfile->u_buff);
285 unsigned int i, escape_it, backslash_count = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000286 const cpp_token *source = NULL;
Neil Boothece54d52001-09-28 09:40:22 +0000287 size_t len;
Neil Booth93c803682000-10-28 17:59:06 +0000288
289 /* Loop, reading in the argument's tokens. */
290 for (i = 0; i < arg->count; i++)
291 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000292 const cpp_token *token = arg->first[i];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000293
294 if (token->type == CPP_PADDING)
295 {
296 if (source == NULL)
297 source = token->val.source;
298 continue;
299 }
Neil Booth93c803682000-10-28 17:59:06 +0000300
301 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
Zack Weinbergcc937582001-03-07 01:32:01 +0000302 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
Neil Booth93c803682000-10-28 17:59:06 +0000303
Neil Boothece54d52001-09-28 09:40:22 +0000304 /* Room for each char being written in octal, initial space and
305 final NUL. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000306 len = cpp_token_len (token);
Neil Booth93c803682000-10-28 17:59:06 +0000307 if (escape_it)
Neil Booth93c803682000-10-28 17:59:06 +0000308 len *= 4;
Neil Boothece54d52001-09-28 09:40:22 +0000309 len += 2;
Neil Booth93c803682000-10-28 17:59:06 +0000310
Neil Boothece54d52001-09-28 09:40:22 +0000311 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth93c803682000-10-28 17:59:06 +0000312 {
Neil Boothece54d52001-09-28 09:40:22 +0000313 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +0000314 _cpp_extend_buff (pfile, &pfile->u_buff, len);
Neil Boothece54d52001-09-28 09:40:22 +0000315 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth93c803682000-10-28 17:59:06 +0000316 }
317
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000318 /* Leading white space? */
Neil Boothece54d52001-09-28 09:40:22 +0000319 if (dest != BUFF_FRONT (pfile->u_buff))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000320 {
321 if (source == NULL)
322 source = token;
323 if (source->flags & PREV_WHITE)
324 *dest++ = ' ';
325 }
326 source = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000327
328 if (escape_it)
329 {
Neil Boothece54d52001-09-28 09:40:22 +0000330 _cpp_buff *buff = _cpp_get_buff (pfile, len);
331 unsigned char *buf = BUFF_FRONT (buff);
Neil Booth93c803682000-10-28 17:59:06 +0000332 len = cpp_spell_token (pfile, token, buf) - buf;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000333 dest = cpp_quote_string (dest, buf, len);
Neil Boothece54d52001-09-28 09:40:22 +0000334 _cpp_release_buff (pfile, buff);
Neil Booth93c803682000-10-28 17:59:06 +0000335 }
336 else
337 dest = cpp_spell_token (pfile, token, dest);
Neil Booth93c803682000-10-28 17:59:06 +0000338
Neil Booth6c53ebf2000-11-06 18:43:32 +0000339 if (token->type == CPP_OTHER && token->val.c == '\\')
Neil Booth93c803682000-10-28 17:59:06 +0000340 backslash_count++;
341 else
342 backslash_count = 0;
343 }
344
345 /* Ignore the final \ of invalid string literals. */
346 if (backslash_count & 1)
347 {
Neil Boothebef4e82002-04-14 18:42:47 +0000348 cpp_error (pfile, DL_WARNING,
349 "invalid string literal, ignoring final '\\'");
Neil Boothece54d52001-09-28 09:40:22 +0000350 dest--;
Neil Booth93c803682000-10-28 17:59:06 +0000351 }
352
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000353 /* Commit the memory, including NUL, and return the token. */
Neil Boothece54d52001-09-28 09:40:22 +0000354 len = dest - BUFF_FRONT (pfile->u_buff);
355 BUFF_FRONT (pfile->u_buff) = dest + 1;
356 return new_string_token (pfile, dest - len, len);
Neil Booth93c803682000-10-28 17:59:06 +0000357}
358
Neil Boothc9e7a602001-09-27 12:59:38 +0000359/* Try to paste two tokens. On success, return non-zero. In any
360 case, PLHS is updated to point to the pasted token, which is
361 guaranteed to not have the PASTE_LEFT flag set. */
362static bool
363paste_tokens (pfile, plhs, rhs)
Neil Boothd63eefb2000-11-20 23:59:26 +0000364 cpp_reader *pfile;
Neil Boothc9e7a602001-09-27 12:59:38 +0000365 const cpp_token **plhs, *rhs;
Neil Boothd63eefb2000-11-20 23:59:26 +0000366{
Neil Boothc9e7a602001-09-27 12:59:38 +0000367 unsigned char *buf, *end;
368 const cpp_token *lhs;
369 unsigned int len;
370 bool valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000371
Neil Boothc9e7a602001-09-27 12:59:38 +0000372 lhs = *plhs;
373 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
374 buf = (unsigned char *) alloca (len);
375 end = cpp_spell_token (pfile, lhs, buf);
Neil Boothd63eefb2000-11-20 23:59:26 +0000376
Neil Boothc9e7a602001-09-27 12:59:38 +0000377 /* Avoid comment headers, since they are still processed in stage 3.
378 It is simpler to insert a space here, rather than modifying the
379 lexer to ignore comments in some circumstances. Simply returning
380 false doesn't work, since we want to clear the PASTE_LEFT flag. */
381 if (lhs->type == CPP_DIV
382 && (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
383 *end++ = ' ';
384 end = cpp_spell_token (pfile, rhs, end);
Neil Booth4d6baaf2001-11-26 23:44:54 +0000385 *end = '\0';
Neil Boothd63eefb2000-11-20 23:59:26 +0000386
Neil Boothc9e7a602001-09-27 12:59:38 +0000387 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
Neil Boothd63eefb2000-11-20 23:59:26 +0000388
Neil Boothc9e7a602001-09-27 12:59:38 +0000389 /* Tweak the column number the lexer will report. */
390 pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
Neil Boothd63eefb2000-11-20 23:59:26 +0000391
Neil Boothc9e7a602001-09-27 12:59:38 +0000392 /* We don't want a leading # to be interpreted as a directive. */
393 pfile->buffer->saved_flags = 0;
Neil Boothd63eefb2000-11-20 23:59:26 +0000394
Neil Boothc9e7a602001-09-27 12:59:38 +0000395 /* Set pfile->cur_token as required by _cpp_lex_direct. */
396 pfile->cur_token = _cpp_temp_token (pfile);
397 *plhs = _cpp_lex_direct (pfile);
Neil Booth480709c2001-10-21 14:04:42 +0000398 valid = pfile->buffer->cur == pfile->buffer->rlimit;
Neil Boothc9e7a602001-09-27 12:59:38 +0000399 _cpp_pop_buffer (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000400
Neil Boothc9e7a602001-09-27 12:59:38 +0000401 return valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000402}
403
Neil Boothd15a58c2002-01-03 18:32:55 +0000404/* Handles an arbitrarily long sequence of ## operators, with initial
405 operand LHS. This implementation is left-associative,
406 non-recursive, and finishes a paste before handling succeeding
407 ones. If a paste fails, we back up to the RHS of the failing ##
408 operator before pushing the context containing the result of prior
409 successful pastes, with the effect that the RHS appears in the
410 output stream after the pasted LHS normally. */
Neil Booth93c803682000-10-28 17:59:06 +0000411static void
412paste_all_tokens (pfile, lhs)
413 cpp_reader *pfile;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000414 const cpp_token *lhs;
Neil Booth93c803682000-10-28 17:59:06 +0000415{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000416 const cpp_token *rhs;
417 cpp_context *context = pfile->context;
418
Neil Booth93c803682000-10-28 17:59:06 +0000419 do
420 {
421 /* Take the token directly from the current context. We can do
422 this, because we are in the replacement list of either an
423 object-like macro, or a function-like macro with arguments
424 inserted. In either case, the constraints to #define
Neil Boothd63eefb2000-11-20 23:59:26 +0000425 guarantee we have at least one more token. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000426 if (context->direct_p)
427 rhs = context->first.token++;
428 else
429 rhs = *context->first.ptoken++;
430
431 if (rhs->type == CPP_PADDING)
432 abort ();
433
Neil Boothc9e7a602001-09-27 12:59:38 +0000434 if (!paste_tokens (pfile, &lhs, rhs))
Neil Booth93c803682000-10-28 17:59:06 +0000435 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000436 _cpp_backup_tokens (pfile, 1);
Neil Boothc9e7a602001-09-27 12:59:38 +0000437
438 /* Mandatory warning for all apart from assembler. */
439 if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Boothebef4e82002-04-14 18:42:47 +0000440 cpp_error (pfile, DL_WARNING,
Neil Boothc9e7a602001-09-27 12:59:38 +0000441 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
Neil Boothebef4e82002-04-14 18:42:47 +0000442 cpp_token_as_text (pfile, lhs),
443 cpp_token_as_text (pfile, rhs));
Neil Booth4c2b6472000-11-11 13:19:01 +0000444 break;
Neil Booth93c803682000-10-28 17:59:06 +0000445 }
Neil Booth93c803682000-10-28 17:59:06 +0000446 }
447 while (rhs->flags & PASTE_LEFT);
448
Neil Boothc9e7a602001-09-27 12:59:38 +0000449 /* Put the resulting token in its own context. */
450 push_token_context (pfile, NULL, lhs, 1);
Neil Booth93c803682000-10-28 17:59:06 +0000451}
452
Neil Boothd15a58c2002-01-03 18:32:55 +0000453/* Reads and returns the arguments to a function-like macro
454 invocation. Assumes the opening parenthesis has been processed.
455 If there is an error, emits an appropriate diagnostic and returns
456 NULL. Each argument is terminated by a CPP_EOF token, for the
457 future benefit of expand_arg(). */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000458static _cpp_buff *
459collect_args (pfile, node)
Neil Booth93c803682000-10-28 17:59:06 +0000460 cpp_reader *pfile;
461 const cpp_hashnode *node;
462{
Neil Boothb8af0ca2001-09-26 17:52:50 +0000463 _cpp_buff *buff, *base_buff;
464 cpp_macro *macro;
465 macro_arg *args, *arg;
466 const cpp_token *token;
467 unsigned int argc;
468 bool error = false;
Neil Booth93c803682000-10-28 17:59:06 +0000469
Neil Boothb8af0ca2001-09-26 17:52:50 +0000470 macro = node->value.macro;
471 if (macro->paramc)
472 argc = macro->paramc;
473 else
474 argc = 1;
475 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
476 + sizeof (macro_arg)));
477 base_buff = buff;
478 args = (macro_arg *) buff->base;
479 memset (args, 0, argc * sizeof (macro_arg));
Neil Boothece54d52001-09-28 09:40:22 +0000480 buff->cur = (unsigned char *) &args[argc];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000481 arg = args, argc = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000482
Neil Boothb8af0ca2001-09-26 17:52:50 +0000483 /* Collect the tokens making up each argument. We don't yet know
484 how many arguments have been supplied, whether too many or too
485 few. Hence the slightly bizarre usage of "argc" and "arg". */
486 do
Neil Booth93c803682000-10-28 17:59:06 +0000487 {
Neil Boothb8af0ca2001-09-26 17:52:50 +0000488 unsigned int paren_depth = 0;
489 unsigned int ntokens = 0;
490
Neil Booth93c803682000-10-28 17:59:06 +0000491 argc++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000492 arg->first = (const cpp_token **) buff->cur;
Neil Booth93c803682000-10-28 17:59:06 +0000493
Neil Boothb8af0ca2001-09-26 17:52:50 +0000494 for (;;)
495 {
496 /* Require space for 2 new tokens (including a CPP_EOF). */
Neil Boothece54d52001-09-28 09:40:22 +0000497 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000498 {
Neil Booth8c3b2692001-09-30 10:03:11 +0000499 buff = _cpp_append_extend_buff (pfile, buff,
500 1000 * sizeof (cpp_token *));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000501 arg->first = (const cpp_token **) buff->cur;
502 }
Neil Booth93c803682000-10-28 17:59:06 +0000503
Neil Boothb8af0ca2001-09-26 17:52:50 +0000504 token = cpp_get_token (pfile);
505
506 if (token->type == CPP_PADDING)
507 {
508 /* Drop leading padding. */
509 if (ntokens == 0)
510 continue;
511 }
512 else if (token->type == CPP_OPEN_PAREN)
513 paren_depth++;
514 else if (token->type == CPP_CLOSE_PAREN)
515 {
516 if (paren_depth-- == 0)
517 break;
518 }
519 else if (token->type == CPP_COMMA)
520 {
521 /* A comma does not terminate an argument within
522 parentheses or as part of a variable argument. */
523 if (paren_depth == 0
524 && ! (macro->variadic && argc == macro->paramc))
525 break;
526 }
527 else if (token->type == CPP_EOF
528 || (token->type == CPP_HASH && token->flags & BOL))
529 break;
530
531 arg->first[ntokens++] = token;
532 }
533
534 /* Drop trailing padding. */
535 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
536 ntokens--;
537
538 arg->count = ntokens;
539 arg->first[ntokens] = &pfile->eof;
540
541 /* Terminate the argument. Excess arguments loop back and
542 overwrite the final legitimate argument, before failing. */
543 if (argc <= macro->paramc)
544 {
Neil Boothece54d52001-09-28 09:40:22 +0000545 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000546 if (argc != macro->paramc)
547 arg++;
548 }
Neil Booth93c803682000-10-28 17:59:06 +0000549 }
Neil Boothe808ec92002-02-27 07:24:53 +0000550 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
Neil Booth93c803682000-10-28 17:59:06 +0000551
Neil Boothe808ec92002-02-27 07:24:53 +0000552 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000553 {
Neil Boothece54d52001-09-28 09:40:22 +0000554 /* We still need the CPP_EOF to end directives, and to end
555 pre-expansion of a macro argument. Step back is not
556 unconditional, since we don't want to return a CPP_EOF to our
557 callers at the end of an -include-d file. */
Neil Boothe808ec92002-02-27 07:24:53 +0000558 if (pfile->context->prev || pfile->state.in_directive)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000559 _cpp_backup_tokens (pfile, 1);
Neil Boothebef4e82002-04-14 18:42:47 +0000560 cpp_error (pfile, DL_ERROR,
561 "unterminated argument list invoking macro \"%s\"",
Neil Bootha28c50352001-05-16 22:02:09 +0000562 NODE_NAME (node));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000563 error = true;
Neil Booth93c803682000-10-28 17:59:06 +0000564 }
565 else if (argc < macro->paramc)
566 {
567 /* As an extension, a rest argument is allowed to not appear in
568 the invocation at all.
569 e.g. #define debug(format, args...) something
570 debug("string");
571
572 This is exactly the same as if there had been an empty rest
573 argument - debug("string", ). */
574
Neil Booth28e0f042000-12-09 12:06:37 +0000575 if (argc + 1 == macro->paramc && macro->variadic)
Neil Booth93c803682000-10-28 17:59:06 +0000576 {
Neil Booth7065e132001-02-14 07:38:20 +0000577 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
Neil Boothebef4e82002-04-14 18:42:47 +0000578 cpp_error (pfile, DL_PEDWARN,
579 "ISO C99 requires rest arguments to be used");
Neil Booth93c803682000-10-28 17:59:06 +0000580 }
581 else
582 {
Neil Boothebef4e82002-04-14 18:42:47 +0000583 cpp_error (pfile, DL_ERROR,
Neil Booth93c803682000-10-28 17:59:06 +0000584 "macro \"%s\" requires %u arguments, but only %u given",
Neil Bootha28c50352001-05-16 22:02:09 +0000585 NODE_NAME (node), macro->paramc, argc);
Neil Boothb8af0ca2001-09-26 17:52:50 +0000586 error = true;
Neil Booth93c803682000-10-28 17:59:06 +0000587 }
588 }
589 else if (argc > macro->paramc)
590 {
Neil Booth4c2b6472000-11-11 13:19:01 +0000591 /* Empty argument to a macro taking no arguments is OK. */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000592 if (argc != 1 || arg->count)
Neil Booth93c803682000-10-28 17:59:06 +0000593 {
Neil Boothebef4e82002-04-14 18:42:47 +0000594 cpp_error (pfile, DL_ERROR,
Neil Booth93c803682000-10-28 17:59:06 +0000595 "macro \"%s\" passed %u arguments, but takes just %u",
Neil Bootha28c50352001-05-16 22:02:09 +0000596 NODE_NAME (node), argc, macro->paramc);
Neil Boothb8af0ca2001-09-26 17:52:50 +0000597 error = true;
Neil Booth93c803682000-10-28 17:59:06 +0000598 }
599 }
600
Neil Boothb8af0ca2001-09-26 17:52:50 +0000601 if (!error)
602 return base_buff;
Neil Booth93c803682000-10-28 17:59:06 +0000603
Neil Boothb8af0ca2001-09-26 17:52:50 +0000604 _cpp_release_buff (pfile, base_buff);
605 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000606}
607
Neil Boothd6da8362001-10-08 06:15:14 +0000608/* Search for an opening parenthesis to the macro of NODE, in such a
609 way that, if none is found, we don't lose the information in any
610 intervening padding tokens. If we find the parenthesis, collect
611 the arguments and return the buffer containing them. */
612static _cpp_buff *
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000613funlike_invocation_p (pfile, node)
Neil Booth93c803682000-10-28 17:59:06 +0000614 cpp_reader *pfile;
Neil Booth644edda2001-10-02 12:57:24 +0000615 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +0000616{
Neil Boothd6da8362001-10-08 06:15:14 +0000617 const cpp_token *token, *padding = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000618
Neil Boothd6da8362001-10-08 06:15:14 +0000619 for (;;)
Neil Boothbdcbe492001-09-13 20:05:17 +0000620 {
Neil Boothd6da8362001-10-08 06:15:14 +0000621 token = cpp_get_token (pfile);
622 if (token->type != CPP_PADDING)
623 break;
624 if (padding == NULL
625 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
626 padding = token;
Neil Boothbdcbe492001-09-13 20:05:17 +0000627 }
Neil Booth93c803682000-10-28 17:59:06 +0000628
Neil Boothd6da8362001-10-08 06:15:14 +0000629 if (token->type == CPP_OPEN_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000630 {
Neil Boothd6da8362001-10-08 06:15:14 +0000631 pfile->state.parsing_args = 2;
632 return collect_args (pfile, node);
Neil Booth93c803682000-10-28 17:59:06 +0000633 }
634
Neil Boothd6da8362001-10-08 06:15:14 +0000635 /* Back up. We may have skipped padding, in which case backing up
636 more than one token when expanding macros is in general too
637 difficult. We re-insert it in its own context. */
638 _cpp_backup_tokens (pfile, 1);
639 if (padding)
640 push_token_context (pfile, NULL, padding, 1);
641
642 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000643}
644
Neil Boothd15a58c2002-01-03 18:32:55 +0000645/* Push the context of a macro with hash entry NODE onto the context
646 stack. If we can successfully expand the macro, we push a context
647 containing its yet-to-be-rescanned replacement list and return one.
648 Otherwise, we don't push a context and return zero. */
Neil Booth93c803682000-10-28 17:59:06 +0000649static int
Neil Booth29b10742000-11-13 18:40:37 +0000650enter_macro_context (pfile, node)
Neil Booth93c803682000-10-28 17:59:06 +0000651 cpp_reader *pfile;
Neil Booth29b10742000-11-13 18:40:37 +0000652 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +0000653{
Neil Boothd15a58c2002-01-03 18:32:55 +0000654 /* The presence of a macro invalidates a file's controlling macro. */
Neil Booth644edda2001-10-02 12:57:24 +0000655 pfile->mi_valid = false;
656
Neil Boothd15a58c2002-01-03 18:32:55 +0000657 /* Handle standard macros. */
Neil Booth644edda2001-10-02 12:57:24 +0000658 if (! (node->flags & NODE_BUILTIN))
Neil Booth93c803682000-10-28 17:59:06 +0000659 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000660 cpp_macro *macro = node->value.macro;
661
Neil Boothd6da8362001-10-08 06:15:14 +0000662 if (macro->fun_like)
663 {
664 _cpp_buff *buff;
665
666 pfile->state.prevent_expansion++;
667 pfile->keep_tokens++;
668 pfile->state.parsing_args = 1;
669 buff = funlike_invocation_p (pfile, node);
670 pfile->state.parsing_args = 0;
671 pfile->keep_tokens--;
672 pfile->state.prevent_expansion--;
673
674 if (buff == NULL)
675 {
676 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
Neil Boothebef4e82002-04-14 18:42:47 +0000677 cpp_error (pfile, DL_WARNING,
Neil Boothd6da8362001-10-08 06:15:14 +0000678 "function-like macro \"%s\" must be used with arguments in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +0000679 NODE_NAME (node));
Neil Boothd6da8362001-10-08 06:15:14 +0000680
681 return 0;
682 }
683
Neil Boothe808ec92002-02-27 07:24:53 +0000684 if (macro->paramc > 0)
685 replace_args (pfile, node, macro, (macro_arg *) buff->base);
Neil Boothd6da8362001-10-08 06:15:14 +0000686 _cpp_release_buff (pfile, buff);
687 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000688
689 /* Disable the macro within its expansion. */
Neil Booth644edda2001-10-02 12:57:24 +0000690 node->flags |= NODE_DISABLED;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000691
692 if (macro->paramc == 0)
Neil Booth644edda2001-10-02 12:57:24 +0000693 push_token_context (pfile, node, macro->expansion, macro->count);
694
695 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000696 }
Neil Booth644edda2001-10-02 12:57:24 +0000697
Neil Boothd15a58c2002-01-03 18:32:55 +0000698 /* Handle built-in macros and the _Pragma operator. */
Neil Booth644edda2001-10-02 12:57:24 +0000699 return builtin_macro (pfile, node);
Zack Weinberg711b8822000-07-18 00:59:49 +0000700}
701
Neil Boothd15a58c2002-01-03 18:32:55 +0000702/* Replace the parameters in a function-like macro of NODE with the
703 actual ARGS, and place the result in a newly pushed token context.
704 Expand each argument before replacing, unless it is operated upon
705 by the # or ## operators. */
Neil Booth93c803682000-10-28 17:59:06 +0000706static void
Neil Boothe808ec92002-02-27 07:24:53 +0000707replace_args (pfile, node, macro, args)
Neil Booth93c803682000-10-28 17:59:06 +0000708 cpp_reader *pfile;
Neil Booth644edda2001-10-02 12:57:24 +0000709 cpp_hashnode *node;
Neil Boothe808ec92002-02-27 07:24:53 +0000710 cpp_macro *macro;
Neil Booth93c803682000-10-28 17:59:06 +0000711 macro_arg *args;
Neil Booth93c803682000-10-28 17:59:06 +0000712{
713 unsigned int i, total;
714 const cpp_token *src, *limit;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000715 const cpp_token **dest, **first;
Neil Booth93c803682000-10-28 17:59:06 +0000716 macro_arg *arg;
Neil Booth1e013d22001-09-26 21:44:35 +0000717 _cpp_buff *buff;
Neil Booth93c803682000-10-28 17:59:06 +0000718
Neil Booth93c803682000-10-28 17:59:06 +0000719 /* First, fully macro-expand arguments, calculating the number of
Neil Booth1e013d22001-09-26 21:44:35 +0000720 tokens in the final expansion as we go. The ordering of the if
721 statements below is subtle; we must handle stringification before
722 pasting. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000723 total = macro->count;
724 limit = macro->expansion + macro->count;
725
726 for (src = macro->expansion; src < limit; src++)
Neil Booth93c803682000-10-28 17:59:06 +0000727 if (src->type == CPP_MACRO_ARG)
728 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000729 /* Leading and trailing padding tokens. */
730 total += 2;
731
Neil Booth93c803682000-10-28 17:59:06 +0000732 /* We have an argument. If it is not being stringified or
733 pasted it is macro-replaced before insertion. */
Neil Booth6c53ebf2000-11-06 18:43:32 +0000734 arg = &args[src->val.arg_no - 1];
Neil Booth4c2b6472000-11-11 13:19:01 +0000735
Neil Booth93c803682000-10-28 17:59:06 +0000736 if (src->flags & STRINGIFY_ARG)
737 {
738 if (!arg->stringified)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000739 arg->stringified = stringify_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000740 }
741 else if ((src->flags & PASTE_LEFT)
742 || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
743 total += arg->count - 1;
744 else
745 {
746 if (!arg->expanded)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000747 expand_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000748 total += arg->expanded_count - 1;
749 }
750 }
751
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000752 /* Now allocate space for the expansion, copy the tokens and replace
753 the arguments. */
Neil Booth1e013d22001-09-26 21:44:35 +0000754 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
755 first = (const cpp_token **) buff->base;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000756 dest = first;
Neil Booth93c803682000-10-28 17:59:06 +0000757
758 for (src = macro->expansion; src < limit; src++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000759 {
760 unsigned int count;
761 const cpp_token **from, **paste_flag;
Neil Booth93c803682000-10-28 17:59:06 +0000762
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000763 if (src->type != CPP_MACRO_ARG)
764 {
765 *dest++ = src;
766 continue;
767 }
768
769 paste_flag = 0;
770 arg = &args[src->val.arg_no - 1];
771 if (src->flags & STRINGIFY_ARG)
772 count = 1, from = &arg->stringified;
773 else if (src->flags & PASTE_LEFT)
774 count = arg->count, from = arg->first;
775 else if (src != macro->expansion && (src[-1].flags & PASTE_LEFT))
776 {
Neil Booth93c803682000-10-28 17:59:06 +0000777 count = arg->count, from = arg->first;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000778 if (dest != first)
779 {
780 /* GCC has special semantics for , ## b where b is a
781 varargs parameter: the comma disappears if b was
782 given no actual arguments (not merely if b is an
783 empty argument); otherwise the paste flag is removed. */
784 if (dest[-1]->type == CPP_COMMA
785 && macro->variadic
786 && src->val.arg_no == macro->paramc)
787 {
788 if (count == 0)
789 dest--;
790 else
791 paste_flag = dest - 1;
792 }
793 /* Remove the paste flag if the RHS is a placemarker. */
794 else if (count == 0)
795 paste_flag = dest - 1;
796 }
797 }
798 else
799 count = arg->expanded_count, from = arg->expanded;
Neil Booth93c803682000-10-28 17:59:06 +0000800
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000801 /* Padding on the left of an argument (unless RHS of ##). */
802 if (!pfile->state.in_directive
803 && src != macro->expansion && !(src[-1].flags & PASTE_LEFT))
804 *dest++ = padding_token (pfile, src);
Neil Booth93c803682000-10-28 17:59:06 +0000805
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000806 if (count)
807 {
808 memcpy (dest, from, count * sizeof (cpp_token *));
809 dest += count;
Neil Booth93c803682000-10-28 17:59:06 +0000810
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000811 /* With a non-empty argument on the LHS of ##, the last
812 token should be flagged PASTE_LEFT. */
813 if (src->flags & PASTE_LEFT)
814 paste_flag = dest - 1;
815 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000816
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000817 /* Avoid paste on RHS (even case count == 0). */
818 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
819 *dest++ = &pfile->avoid_paste;
Neil Booth26ec42e2001-01-28 11:22:23 +0000820
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000821 /* Add a new paste flag, or remove an unwanted one. */
822 if (paste_flag)
823 {
824 cpp_token *token = _cpp_temp_token (pfile);
825 token->type = (*paste_flag)->type;
826 token->val.str = (*paste_flag)->val.str;
827 if (src->flags & PASTE_LEFT)
828 token->flags = (*paste_flag)->flags | PASTE_LEFT;
829 else
830 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
831 *paste_flag = token;
832 }
833 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000834
Neil Booth93c803682000-10-28 17:59:06 +0000835 /* Free the expanded arguments. */
836 for (i = 0; i < macro->paramc; i++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000837 if (args[i].expanded)
838 free (args[i].expanded);
839
Neil Booth644edda2001-10-02 12:57:24 +0000840 push_ptoken_context (pfile, node, buff, first, dest - first);
Neil Booth93c803682000-10-28 17:59:06 +0000841}
842
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000843/* Return a special padding token, with padding inherited from SOURCE. */
844static const cpp_token *
845padding_token (pfile, source)
Neil Booth93c803682000-10-28 17:59:06 +0000846 cpp_reader *pfile;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000847 const cpp_token *source;
848{
849 cpp_token *result = _cpp_temp_token (pfile);
850
851 result->type = CPP_PADDING;
852 result->val.source = source;
853 result->flags = 0;
854 return result;
855}
856
Neil Boothd15a58c2002-01-03 18:32:55 +0000857/* Get a new uninitialized context. Create a new one if we cannot
858 re-use an old one. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000859static cpp_context *
860next_context (pfile)
861 cpp_reader *pfile;
862{
863 cpp_context *result = pfile->context->next;
864
865 if (result == 0)
866 {
867 result = xnew (cpp_context);
868 result->prev = pfile->context;
869 result->next = 0;
870 pfile->context->next = result;
871 }
872
873 pfile->context = result;
874 return result;
875}
876
877/* Push a list of pointers to tokens. */
878static void
Neil Booth1e013d22001-09-26 21:44:35 +0000879push_ptoken_context (pfile, macro, buff, first, count)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000880 cpp_reader *pfile;
Neil Booth644edda2001-10-02 12:57:24 +0000881 cpp_hashnode *macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000882 _cpp_buff *buff;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000883 const cpp_token **first;
884 unsigned int count;
Neil Booth93c803682000-10-28 17:59:06 +0000885{
886 cpp_context *context = next_context (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000887
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000888 context->direct_p = false;
889 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000890 context->buff = buff;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000891 context->first.ptoken = first;
892 context->last.ptoken = first + count;
893}
894
895/* Push a list of tokens. */
896static void
897push_token_context (pfile, macro, first, count)
898 cpp_reader *pfile;
Neil Booth644edda2001-10-02 12:57:24 +0000899 cpp_hashnode *macro;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000900 const cpp_token *first;
901 unsigned int count;
902{
903 cpp_context *context = next_context (pfile);
904
905 context->direct_p = true;
906 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000907 context->buff = NULL;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000908 context->first.token = first;
909 context->last.token = first + count;
Neil Booth93c803682000-10-28 17:59:06 +0000910}
911
Neil Boothd15a58c2002-01-03 18:32:55 +0000912/* Expand an argument ARG before replacing parameters in a
913 function-like macro. This works by pushing a context with the
914 argument's tokens, and then expanding that into a temporary buffer
915 as if it were a normal part of the token stream. collect_args()
916 has terminated the argument's tokens with a CPP_EOF so that we know
917 when we have fully expanded the argument. */
Neil Booth93c803682000-10-28 17:59:06 +0000918static void
919expand_arg (pfile, arg)
920 cpp_reader *pfile;
921 macro_arg *arg;
922{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000923 unsigned int capacity;
924
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000925 if (arg->count == 0)
926 return;
Neil Booth93c803682000-10-28 17:59:06 +0000927
928 /* Loop, reading in the arguments. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000929 capacity = 256;
930 arg->expanded = (const cpp_token **)
931 xmalloc (capacity * sizeof (cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +0000932
Neil Booth1e013d22001-09-26 21:44:35 +0000933 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000934 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +0000935 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000936 const cpp_token *token;
937
938 if (arg->expanded_count + 1 >= capacity)
Neil Booth93c803682000-10-28 17:59:06 +0000939 {
940 capacity *= 2;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000941 arg->expanded = (const cpp_token **)
942 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +0000943 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000944
945 token = cpp_get_token (pfile);
946
947 if (token->type == CPP_EOF)
948 break;
949
950 arg->expanded[arg->expanded_count++] = token;
Neil Booth93c803682000-10-28 17:59:06 +0000951 }
Neil Booth93c803682000-10-28 17:59:06 +0000952
Neil Booth1e013d22001-09-26 21:44:35 +0000953 _cpp_pop_context (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000954}
955
Neil Boothd15a58c2002-01-03 18:32:55 +0000956/* Pop the current context off the stack, re-enabling the macro if the
957 context represented a macro's replacement list. The context
958 structure is not freed so that we can re-use it later. */
Neil Booth93c803682000-10-28 17:59:06 +0000959void
960_cpp_pop_context (pfile)
961 cpp_reader *pfile;
962{
Neil Booth1e013d22001-09-26 21:44:35 +0000963 cpp_context *context = pfile->context;
Neil Booth93c803682000-10-28 17:59:06 +0000964
Neil Booth1e013d22001-09-26 21:44:35 +0000965 if (context->macro)
Neil Booth644edda2001-10-02 12:57:24 +0000966 context->macro->flags &= ~NODE_DISABLED;
Neil Booth1e013d22001-09-26 21:44:35 +0000967
968 if (context->buff)
969 _cpp_release_buff (pfile, context->buff);
970
971 pfile->context = context->prev;
Neil Booth93c803682000-10-28 17:59:06 +0000972}
973
Neil Booth7f2f1a62000-11-14 18:32:06 +0000974/* Eternal routine to get a token. Also used nearly everywhere
975 internally, except for places where we know we can safely call
976 the lexer directly, such as lexing a directive name.
977
978 Macro expansions and directives are transparently handled,
979 including entering included files. Thus tokens are post-macro
980 expansion, and after any intervening directives. External callers
981 see CPP_EOF only at EOF. Internal callers also see it when meeting
982 a directive inside a macro call, when at the end of a directive and
983 state.in_directive is still 1, and at the end of argument
984 pre-expansion. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000985const cpp_token *
986cpp_get_token (pfile)
Neil Booth93c803682000-10-28 17:59:06 +0000987 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000988{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000989 const cpp_token *result;
990
Neil Booth29b10742000-11-13 18:40:37 +0000991 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +0000992 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000993 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +0000994 cpp_context *context = pfile->context;
995
Neil Booth93c803682000-10-28 17:59:06 +0000996 /* Context->prev == 0 <=> base context. */
Neil Boothbdcbe492001-09-13 20:05:17 +0000997 if (!context->prev)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000998 result = _cpp_lex_token (pfile);
999 else if (context->first.token != context->last.token)
Neil Booth29b10742000-11-13 18:40:37 +00001000 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001001 if (context->direct_p)
1002 result = context->first.token++;
1003 else
1004 result = *context->first.ptoken++;
1005
1006 if (result->flags & PASTE_LEFT)
Neil Boothec1a23e2001-01-31 07:48:54 +00001007 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001008 paste_all_tokens (pfile, result);
1009 if (pfile->state.in_directive)
1010 continue;
1011 return padding_token (pfile, result);
Neil Boothec1a23e2001-01-31 07:48:54 +00001012 }
Neil Booth29b10742000-11-13 18:40:37 +00001013 }
Neil Booth93c803682000-10-28 17:59:06 +00001014 else
1015 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001016 _cpp_pop_context (pfile);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001017 if (pfile->state.in_directive)
1018 continue;
1019 return &pfile->avoid_paste;
Neil Booth93c803682000-10-28 17:59:06 +00001020 }
Neil Booth93c803682000-10-28 17:59:06 +00001021
Jason Thorpe477cdac2002-04-07 03:12:23 +00001022 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1023 continue;
1024
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001025 if (result->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +00001026 break;
1027
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001028 node = result->val.node;
Neil Booth4c2b6472000-11-11 13:19:01 +00001029
Neil Booth644edda2001-10-02 12:57:24 +00001030 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1031 break;
1032
1033 if (!(node->flags & NODE_DISABLED))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001034 {
Neil Booth644edda2001-10-02 12:57:24 +00001035 if (!pfile->state.prevent_expansion
1036 && enter_macro_context (pfile, node))
Neil Boothbd969772001-02-01 19:13:53 +00001037 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001038 if (pfile->state.in_directive)
1039 continue;
1040 return padding_token (pfile, result);
Neil Boothbd969772001-02-01 19:13:53 +00001041 }
Neil Booth93c803682000-10-28 17:59:06 +00001042 }
Neil Booth644edda2001-10-02 12:57:24 +00001043 else
1044 {
Neil Boothd15a58c2002-01-03 18:32:55 +00001045 /* Flag this token as always unexpandable. FIXME: move this
1046 to collect_args()?. */
Neil Booth644edda2001-10-02 12:57:24 +00001047 cpp_token *t = _cpp_temp_token (pfile);
1048 t->type = result->type;
1049 t->flags = result->flags | NO_EXPAND;
1050 t->val.str = result->val.str;
1051 result = t;
1052 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001053
Neil Booth644edda2001-10-02 12:57:24 +00001054 break;
Neil Booth93c803682000-10-28 17:59:06 +00001055 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001056
1057 return result;
Neil Booth93c803682000-10-28 17:59:06 +00001058}
1059
Neil Booth7065e132001-02-14 07:38:20 +00001060/* Returns true if we're expanding an object-like macro that was
1061 defined in a system header. Just checks the macro at the top of
1062 the stack. Used for diagnostic suppression. */
1063int
Neil Boothc6911452001-03-03 11:32:32 +00001064cpp_sys_macro_p (pfile)
Neil Booth7065e132001-02-14 07:38:20 +00001065 cpp_reader *pfile;
1066{
Neil Booth644edda2001-10-02 12:57:24 +00001067 cpp_hashnode *node = pfile->context->macro;
Neil Booth7065e132001-02-14 07:38:20 +00001068
Neil Booth644edda2001-10-02 12:57:24 +00001069 return node && node->value.macro && node->value.macro->syshdr;
Neil Booth7065e132001-02-14 07:38:20 +00001070}
1071
Neil Booth93c803682000-10-28 17:59:06 +00001072/* Read each token in, until EOF. Directives are transparently
1073 processed. */
1074void
Neil Boothef6e9582001-08-04 12:01:59 +00001075cpp_scan_nooutput (pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001076 cpp_reader *pfile;
1077{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001078 while (cpp_get_token (pfile)->type != CPP_EOF)
1079 ;
Neil Booth93c803682000-10-28 17:59:06 +00001080}
1081
Neil Boothbdcbe492001-09-13 20:05:17 +00001082/* Step back one (or more) tokens. Can only step mack more than 1 if
1083 they are from the lexer, and not from macro expansion. */
Neil Boothb528a072000-11-12 11:46:21 +00001084void
Neil Boothbdcbe492001-09-13 20:05:17 +00001085_cpp_backup_tokens (pfile, count)
Neil Booth93c803682000-10-28 17:59:06 +00001086 cpp_reader *pfile;
Neil Boothbdcbe492001-09-13 20:05:17 +00001087 unsigned int count;
Neil Booth93c803682000-10-28 17:59:06 +00001088{
Neil Boothbdcbe492001-09-13 20:05:17 +00001089 if (pfile->context->prev == NULL)
1090 {
1091 pfile->lookaheads += count;
1092 while (count--)
1093 {
Neil Booth3293c3e2001-11-19 21:04:49 +00001094 pfile->cur_token--;
1095 if (pfile->cur_token == pfile->cur_run->base
1096 /* Possible with -fpreprocessed and no leading #line. */
1097 && pfile->cur_run->prev != NULL)
Neil Boothbdcbe492001-09-13 20:05:17 +00001098 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001099 pfile->cur_run = pfile->cur_run->prev;
1100 pfile->cur_token = pfile->cur_run->limit;
1101 }
1102 }
1103 }
Neil Booth93c803682000-10-28 17:59:06 +00001104 else
1105 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001106 if (count != 1)
1107 abort ();
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001108 if (pfile->context->direct_p)
1109 pfile->context->first.token--;
1110 else
1111 pfile->context->first.ptoken--;
Neil Booth93c803682000-10-28 17:59:06 +00001112 }
Neil Booth93c803682000-10-28 17:59:06 +00001113}
1114
1115/* #define directive parsing and handling. */
1116
Neil Booth618cdda2001-02-25 09:43:03 +00001117/* Returns non-zero if a macro redefinition warning is required. */
Neil Booth93c803682000-10-28 17:59:06 +00001118static int
Neil Booth4d6baaf2001-11-26 23:44:54 +00001119warn_of_redefinition (node, macro2)
Neil Booth93c803682000-10-28 17:59:06 +00001120 const cpp_hashnode *node;
1121 const cpp_macro *macro2;
1122{
1123 const cpp_macro *macro1;
1124 unsigned int i;
1125
Neil Booth618cdda2001-02-25 09:43:03 +00001126 /* Some redefinitions need to be warned about regardless. */
1127 if (node->flags & NODE_WARN)
1128 return 1;
Neil Booth93c803682000-10-28 17:59:06 +00001129
Neil Booth618cdda2001-02-25 09:43:03 +00001130 /* Redefinition of a macro is allowed if and only if the old and new
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001131 definitions are the same. (6.10.3 paragraph 2). */
Neil Booth93c803682000-10-28 17:59:06 +00001132 macro1 = node->value.macro;
1133
1134 /* The quick failures. */
1135 if (macro1->count != macro2->count
1136 || macro1->paramc != macro2->paramc
1137 || macro1->fun_like != macro2->fun_like
Neil Booth28e0f042000-12-09 12:06:37 +00001138 || macro1->variadic != macro2->variadic)
Neil Booth618cdda2001-02-25 09:43:03 +00001139 return 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001140
Neil Booth93c803682000-10-28 17:59:06 +00001141 /* Check each token. */
1142 for (i = 0; i < macro1->count; i++)
1143 if (! _cpp_equiv_tokens (&macro1->expansion[i], &macro2->expansion[i]))
Neil Booth618cdda2001-02-25 09:43:03 +00001144 return 1;
Neil Booth93c803682000-10-28 17:59:06 +00001145
1146 /* Check parameter spellings. */
1147 for (i = 0; i < macro1->paramc; i++)
1148 if (macro1->params[i] != macro2->params[i])
Neil Booth618cdda2001-02-25 09:43:03 +00001149 return 1;
Neil Booth93c803682000-10-28 17:59:06 +00001150
Neil Booth618cdda2001-02-25 09:43:03 +00001151 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001152}
1153
Neil Booth93c803682000-10-28 17:59:06 +00001154/* Free the definition of hashnode H. */
Neil Booth93c803682000-10-28 17:59:06 +00001155void
1156_cpp_free_definition (h)
1157 cpp_hashnode *h;
Zack Weinberg711b8822000-07-18 00:59:49 +00001158{
Neil Booth93c803682000-10-28 17:59:06 +00001159 /* Macros and assertions no longer have anything to free. */
1160 h->type = NT_VOID;
1161 /* Clear builtin flag in case of redefinition. */
Neil Booth644edda2001-10-02 12:57:24 +00001162 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
Neil Booth93c803682000-10-28 17:59:06 +00001163}
Zack Weinberg711b8822000-07-18 00:59:49 +00001164
Neil Booth14baae02001-09-17 18:26:12 +00001165/* Save parameter NODE to the parameter list of macro MACRO. Returns
Joseph Myersf5143c42001-11-04 02:51:28 +00001166 zero on success, non-zero if the parameter is a duplicate. */
Neil Booth93c803682000-10-28 17:59:06 +00001167static int
1168save_parameter (pfile, macro, node)
1169 cpp_reader *pfile;
1170 cpp_macro *macro;
1171 cpp_hashnode *node;
1172{
Neil Booth93c803682000-10-28 17:59:06 +00001173 /* Constraint 6.10.3.6 - duplicate parameter names. */
1174 if (node->arg_index)
Zack Weinberg711b8822000-07-18 00:59:49 +00001175 {
Neil Boothebef4e82002-04-14 18:42:47 +00001176 cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"",
1177 NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +00001178 return 1;
1179 }
1180
Neil Booth8c3b2692001-09-30 10:03:11 +00001181 if (BUFF_ROOM (pfile->a_buff)
1182 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1183 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +00001184
Neil Booth8c3b2692001-09-30 10:03:11 +00001185 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1186 node->arg_index = macro->paramc;
Neil Booth93c803682000-10-28 17:59:06 +00001187 return 0;
1188}
1189
Neil Booth8c3b2692001-09-30 10:03:11 +00001190/* Check the syntax of the parameters in a MACRO definition. */
Neil Booth93c803682000-10-28 17:59:06 +00001191static int
1192parse_params (pfile, macro)
1193 cpp_reader *pfile;
1194 cpp_macro *macro;
1195{
Neil Booth93c803682000-10-28 17:59:06 +00001196 unsigned int prev_ident = 0;
1197
Neil Booth93c803682000-10-28 17:59:06 +00001198 for (;;)
1199 {
Neil Booth345894b2001-09-16 13:44:29 +00001200 const cpp_token *token = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001201
Neil Booth345894b2001-09-16 13:44:29 +00001202 switch (token->type)
Zack Weinberg711b8822000-07-18 00:59:49 +00001203 {
1204 default:
Jason Thorpe477cdac2002-04-07 03:12:23 +00001205 /* Allow/ignore comments in parameter lists if we are
1206 preserving comments in macro expansions. */
1207 if (token->type == CPP_COMMENT
1208 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1209 continue;
1210
Neil Boothebef4e82002-04-14 18:42:47 +00001211 cpp_error (pfile, DL_ERROR,
1212 "\"%s\" may not appear in macro parameter list",
Neil Booth345894b2001-09-16 13:44:29 +00001213 cpp_token_as_text (pfile, token));
Neil Booth93c803682000-10-28 17:59:06 +00001214 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001215
Zack Weinberg711b8822000-07-18 00:59:49 +00001216 case CPP_NAME:
1217 if (prev_ident)
1218 {
Neil Boothebef4e82002-04-14 18:42:47 +00001219 cpp_error (pfile, DL_ERROR,
1220 "macro parameters must be comma-separated");
Neil Booth93c803682000-10-28 17:59:06 +00001221 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001222 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001223 prev_ident = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001224
Neil Booth345894b2001-09-16 13:44:29 +00001225 if (save_parameter (pfile, macro, token->val.node))
Neil Booth93c803682000-10-28 17:59:06 +00001226 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001227 continue;
1228
1229 case CPP_CLOSE_PAREN:
Neil Booth93c803682000-10-28 17:59:06 +00001230 if (prev_ident || macro->paramc == 0)
Neil Booth8c3b2692001-09-30 10:03:11 +00001231 return 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001232
1233 /* Fall through to pick up the error. */
1234 case CPP_COMMA:
1235 if (!prev_ident)
1236 {
Neil Boothebef4e82002-04-14 18:42:47 +00001237 cpp_error (pfile, DL_ERROR, "parameter name missing");
Neil Booth93c803682000-10-28 17:59:06 +00001238 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001239 }
1240 prev_ident = 0;
1241 continue;
1242
1243 case CPP_ELLIPSIS:
Neil Booth28e0f042000-12-09 12:06:37 +00001244 macro->variadic = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001245 if (!prev_ident)
1246 {
Neil Booth93c803682000-10-28 17:59:06 +00001247 save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1248 pfile->state.va_args_ok = 1;
1249 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
Neil Boothebef4e82002-04-14 18:42:47 +00001250 cpp_error (pfile, DL_PEDWARN,
1251 "anonymous variadic macros were introduced in C99");
Zack Weinberg711b8822000-07-18 00:59:49 +00001252 }
Neil Booth93c803682000-10-28 17:59:06 +00001253 else if (CPP_OPTION (pfile, pedantic))
Neil Boothebef4e82002-04-14 18:42:47 +00001254 cpp_error (pfile, DL_PEDWARN,
1255 "ISO C does not permit named variadic macros");
Zack Weinberg711b8822000-07-18 00:59:49 +00001256
Neil Booth93c803682000-10-28 17:59:06 +00001257 /* We're at the end, and just expect a closing parenthesis. */
Neil Booth345894b2001-09-16 13:44:29 +00001258 token = _cpp_lex_token (pfile);
1259 if (token->type == CPP_CLOSE_PAREN)
Neil Booth8c3b2692001-09-30 10:03:11 +00001260 return 1;
Neil Booth93c803682000-10-28 17:59:06 +00001261 /* Fall through. */
1262
1263 case CPP_EOF:
Neil Boothebef4e82002-04-14 18:42:47 +00001264 cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list");
Neil Booth93c803682000-10-28 17:59:06 +00001265 return 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001266 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001267 }
1268}
1269
Neil Booth14baae02001-09-17 18:26:12 +00001270/* Allocate room for a token from a macro's replacement list. */
Neil Booth93c803682000-10-28 17:59:06 +00001271static cpp_token *
Neil Booth14baae02001-09-17 18:26:12 +00001272alloc_expansion_token (pfile, macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001273 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001274 cpp_macro *macro;
Zack Weinberg711b8822000-07-18 00:59:49 +00001275{
Neil Booth8c3b2692001-09-30 10:03:11 +00001276 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1277 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
Zack Weinberg711b8822000-07-18 00:59:49 +00001278
Neil Booth8c3b2692001-09-30 10:03:11 +00001279 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
Neil Booth14baae02001-09-17 18:26:12 +00001280}
1281
Neil Boothd15a58c2002-01-03 18:32:55 +00001282/* Lex a token from the expansion of MACRO, but mark parameters as we
1283 find them and warn of traditional stringification. */
Neil Booth14baae02001-09-17 18:26:12 +00001284static cpp_token *
1285lex_expansion_token (pfile, macro)
1286 cpp_reader *pfile;
1287 cpp_macro *macro;
1288{
1289 cpp_token *token;
1290
1291 pfile->cur_token = alloc_expansion_token (pfile, macro);
1292 token = _cpp_lex_direct (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001293
Neil Boothd15a58c2002-01-03 18:32:55 +00001294 /* Is this a parameter? */
Neil Booth93c803682000-10-28 17:59:06 +00001295 if (token->type == CPP_NAME && token->val.node->arg_index)
Zack Weinberg711b8822000-07-18 00:59:49 +00001296 {
Neil Booth93c803682000-10-28 17:59:06 +00001297 token->type = CPP_MACRO_ARG;
Neil Booth6c53ebf2000-11-06 18:43:32 +00001298 token->val.arg_no = token->val.node->arg_index;
Zack Weinberg711b8822000-07-18 00:59:49 +00001299 }
Neil Booth93c803682000-10-28 17:59:06 +00001300 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1301 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1302 check_trad_stringification (pfile, macro, &token->val.str);
Zack Weinberg711b8822000-07-18 00:59:49 +00001303
Neil Booth93c803682000-10-28 17:59:06 +00001304 return token;
Zack Weinberg711b8822000-07-18 00:59:49 +00001305}
1306
1307/* Parse a macro and save its expansion. Returns non-zero on success. */
1308int
Neil Booth93c803682000-10-28 17:59:06 +00001309_cpp_create_definition (pfile, node)
Zack Weinberg711b8822000-07-18 00:59:49 +00001310 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001311 cpp_hashnode *node;
Zack Weinberg711b8822000-07-18 00:59:49 +00001312{
Neil Booth93c803682000-10-28 17:59:06 +00001313 cpp_macro *macro;
Neil Booth14baae02001-09-17 18:26:12 +00001314 cpp_token *token, *saved_cur_token;
1315 const cpp_token *ctoken;
Neil Booth93c803682000-10-28 17:59:06 +00001316 unsigned int i, ok = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001317
Neil Booth8c3b2692001-09-30 10:03:11 +00001318 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
Neil Booth50410422001-09-15 10:18:03 +00001319 macro->line = pfile->directive_line;
Neil Booth93c803682000-10-28 17:59:06 +00001320 macro->params = 0;
1321 macro->paramc = 0;
Neil Booth28e0f042000-12-09 12:06:37 +00001322 macro->variadic = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001323 macro->count = 0;
Neil Booth14baae02001-09-17 18:26:12 +00001324 macro->fun_like = 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00001325
Neil Booth93c803682000-10-28 17:59:06 +00001326 /* Get the first token of the expansion (or the '(' of a
1327 function-like macro). */
Neil Booth14baae02001-09-17 18:26:12 +00001328 ctoken = _cpp_lex_token (pfile);
1329
1330 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
Zack Weinberg711b8822000-07-18 00:59:49 +00001331 {
Neil Booth8c3b2692001-09-30 10:03:11 +00001332 ok = parse_params (pfile, macro);
1333 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1334 if (!ok)
Neil Booth14baae02001-09-17 18:26:12 +00001335 goto cleanup2;
Neil Booth8c3b2692001-09-30 10:03:11 +00001336
1337 /* Success. Commit the parameter array. */
1338 BUFF_FRONT (pfile->a_buff) = (U_CHAR *) &macro->params[macro->paramc];
Neil Booth93c803682000-10-28 17:59:06 +00001339 macro->fun_like = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001340 }
Neil Booth14baae02001-09-17 18:26:12 +00001341 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
Neil Boothebef4e82002-04-14 18:42:47 +00001342 cpp_error (pfile, DL_PEDWARN,
1343 "ISO C requires whitespace after the macro name");
Neil Booth93c803682000-10-28 17:59:06 +00001344
Neil Booth14baae02001-09-17 18:26:12 +00001345 saved_cur_token = pfile->cur_token;
Neil Booth14baae02001-09-17 18:26:12 +00001346
1347 if (macro->fun_like)
1348 token = lex_expansion_token (pfile, macro);
1349 else
1350 {
1351 token = alloc_expansion_token (pfile, macro);
1352 *token = *ctoken;
1353 }
Neil Booth93c803682000-10-28 17:59:06 +00001354
1355 for (;;)
1356 {
1357 /* Check the stringifying # constraint 6.10.3.2.1 of
1358 function-like macros when lexing the subsequent token. */
1359 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
Zack Weinberg711b8822000-07-18 00:59:49 +00001360 {
Neil Booth93c803682000-10-28 17:59:06 +00001361 if (token->type == CPP_MACRO_ARG)
1362 {
1363 token->flags &= ~PREV_WHITE;
1364 token->flags |= STRINGIFY_ARG;
1365 token->flags |= token[-1].flags & PREV_WHITE;
1366 token[-1] = token[0];
1367 macro->count--;
1368 }
1369 /* Let assembler get away with murder. */
Neil Boothbdb05a72000-11-26 17:31:13 +00001370 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +00001371 {
1372 ok = 0;
Neil Boothebef4e82002-04-14 18:42:47 +00001373 cpp_error (pfile, DL_ERROR,
1374 "'#' is not followed by a macro parameter");
Neil Booth14baae02001-09-17 18:26:12 +00001375 goto cleanup1;
Neil Booth93c803682000-10-28 17:59:06 +00001376 }
1377 }
1378
1379 if (token->type == CPP_EOF)
1380 break;
1381
1382 /* Paste operator constraint 6.10.3.3.1. */
1383 if (token->type == CPP_PASTE)
1384 {
1385 /* Token-paste ##, can appear in both object-like and
1386 function-like macros, but not at the ends. */
1387 if (--macro->count > 0)
1388 token = lex_expansion_token (pfile, macro);
1389
1390 if (macro->count == 0 || token->type == CPP_EOF)
1391 {
1392 ok = 0;
Neil Boothebef4e82002-04-14 18:42:47 +00001393 cpp_error (pfile, DL_ERROR,
Neil Booth93c803682000-10-28 17:59:06 +00001394 "'##' cannot appear at either end of a macro expansion");
Neil Booth14baae02001-09-17 18:26:12 +00001395 goto cleanup1;
Neil Booth93c803682000-10-28 17:59:06 +00001396 }
1397
1398 token[-1].flags |= PASTE_LEFT;
Neil Booth93c803682000-10-28 17:59:06 +00001399 }
1400
1401 token = lex_expansion_token (pfile, macro);
1402 }
1403
Neil Booth8c3b2692001-09-30 10:03:11 +00001404 macro->expansion = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1405
Neil Booth4c2b6472000-11-11 13:19:01 +00001406 /* Don't count the CPP_EOF. */
1407 macro->count--;
Neil Booth93c803682000-10-28 17:59:06 +00001408
Neil Boothd15a58c2002-01-03 18:32:55 +00001409 /* Clear whitespace on first token for warn_of_redefinition(). */
Neil Booth8c3b2692001-09-30 10:03:11 +00001410 if (macro->count)
1411 macro->expansion[0].flags &= ~PREV_WHITE;
1412
1413 /* Commit the memory. */
1414 BUFF_FRONT (pfile->a_buff) = (U_CHAR *) &macro->expansion[macro->count];
1415
Neil Booth44ed91a2000-10-29 11:37:18 +00001416 /* Implement the macro-defined-to-itself optimisation. */
Neil Booth644edda2001-10-02 12:57:24 +00001417 if (macro->count == 1 && !macro->fun_like
1418 && macro->expansion[0].type == CPP_NAME
1419 && macro->expansion[0].val.node == node)
1420 node->flags |= NODE_DISABLED;
Neil Booth44ed91a2000-10-29 11:37:18 +00001421
Neil Booth7065e132001-02-14 07:38:20 +00001422 /* To suppress some diagnostics. */
Neil Booth47d89cf2001-08-11 07:33:39 +00001423 macro->syshdr = pfile->map->sysp != 0;
Neil Booth7065e132001-02-14 07:38:20 +00001424
Neil Booth93c803682000-10-28 17:59:06 +00001425 if (node->type != NT_VOID)
1426 {
Neil Booth4d6baaf2001-11-26 23:44:54 +00001427 if (warn_of_redefinition (node, macro))
Neil Booth93c803682000-10-28 17:59:06 +00001428 {
Neil Boothebef4e82002-04-14 18:42:47 +00001429 cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0,
1430 "\"%s\" redefined", NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +00001431
Neil Booth618cdda2001-02-25 09:43:03 +00001432 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
Neil Boothebef4e82002-04-14 18:42:47 +00001433 cpp_error_with_line (pfile, DL_PEDWARN, node->value.macro->line, 0,
Zack Weinberg711b8822000-07-18 00:59:49 +00001434 "this is the location of the previous definition");
1435 }
Neil Booth93c803682000-10-28 17:59:06 +00001436 _cpp_free_definition (node);
Zack Weinberg711b8822000-07-18 00:59:49 +00001437 }
1438
1439 /* Enter definition in hash table. */
Neil Booth93c803682000-10-28 17:59:06 +00001440 node->type = NT_MACRO;
1441 node->value.macro = macro;
Neil Bootha28c50352001-05-16 22:02:09 +00001442 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
Neil Booth618cdda2001-02-25 09:43:03 +00001443 node->flags |= NODE_WARN;
Zack Weinberg711b8822000-07-18 00:59:49 +00001444
Neil Booth14baae02001-09-17 18:26:12 +00001445 cleanup1:
1446
1447 /* Set type for SEEN_EOL() in cpplib.c, restore the lexer position. */
1448 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1449 pfile->cur_token = saved_cur_token;
1450
1451 cleanup2:
Neil Booth93c803682000-10-28 17:59:06 +00001452
1453 /* Stop the lexer accepting __VA_ARGS__. */
1454 pfile->state.va_args_ok = 0;
1455
1456 /* Clear the fast argument lookup indices. */
1457 for (i = macro->paramc; i-- > 0; )
1458 macro->params[i]->arg_index = 0;
1459
1460 return ok;
Zack Weinberg711b8822000-07-18 00:59:49 +00001461}
1462
Neil Boothd15a58c2002-01-03 18:32:55 +00001463/* Warn if a token in STRING matches one of a function-like MACRO's
1464 parameters. */
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001465static void
Neil Booth93c803682000-10-28 17:59:06 +00001466check_trad_stringification (pfile, macro, string)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001467 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001468 const cpp_macro *macro;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001469 const cpp_string *string;
1470{
Neil Booth93c803682000-10-28 17:59:06 +00001471 unsigned int i, len;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001472 const U_CHAR *p, *q, *limit = string->text + string->len;
1473
1474 /* Loop over the string. */
1475 for (p = string->text; p < limit; p = q)
1476 {
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001477 /* Find the start of an identifier. */
Greg McGary61c16b12000-09-15 21:25:02 +00001478 while (p < limit && !is_idstart (*p))
1479 p++;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001480
1481 /* Find the end of the identifier. */
1482 q = p;
Greg McGary61c16b12000-09-15 21:25:02 +00001483 while (q < limit && is_idchar (*q))
1484 q++;
Neil Booth93c803682000-10-28 17:59:06 +00001485
1486 len = q - p;
1487
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001488 /* Loop over the function macro arguments to see if the
1489 identifier inside the string matches one of them. */
Neil Booth93c803682000-10-28 17:59:06 +00001490 for (i = 0; i < macro->paramc; i++)
1491 {
1492 const cpp_hashnode *node = macro->params[i];
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001493
Neil Booth2a967f32001-05-20 06:26:45 +00001494 if (NODE_LEN (node) == len
1495 && !memcmp (p, NODE_NAME (node), len))
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001496 {
Neil Boothebef4e82002-04-14 18:42:47 +00001497 cpp_error (pfile, DL_WARNING,
Zack Weinbergf458d1d2002-02-27 18:48:07 +00001498 "macro argument \"%s\" would be stringified in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +00001499 NODE_NAME (node));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001500 break;
1501 }
1502 }
1503 }
1504}
Neil Booth93c803682000-10-28 17:59:06 +00001505
Neil Booth70961712001-06-23 11:34:41 +00001506/* Returns the name, arguments and expansion of a macro, in a format
1507 suitable to be read back in again, and therefore also for DWARF 2
1508 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1509 Caller is expected to generate the "#define" bit if needed. The
Neil Booth93c803682000-10-28 17:59:06 +00001510 returned text is temporary, and automatically freed later. */
Neil Booth93c803682000-10-28 17:59:06 +00001511const unsigned char *
1512cpp_macro_definition (pfile, node)
1513 cpp_reader *pfile;
1514 const cpp_hashnode *node;
1515{
1516 unsigned int i, len;
1517 const cpp_macro *macro = node->value.macro;
1518 unsigned char *buffer;
1519
1520 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1521 {
Neil Boothebef4e82002-04-14 18:42:47 +00001522 cpp_error (pfile, DL_ICE,
1523 "invalid hash type %d in cpp_macro_definition", node->type);
Neil Booth93c803682000-10-28 17:59:06 +00001524 return 0;
1525 }
1526
1527 /* Calculate length. */
Neil Booth70961712001-06-23 11:34:41 +00001528 len = NODE_LEN (node) + 1; /* ' ' */
Neil Booth93c803682000-10-28 17:59:06 +00001529 if (macro->fun_like)
1530 {
Jim Blandy64d08262002-04-05 00:12:40 +00001531 len += 4; /* "()" plus possible final ".." of named
1532 varargs (we have + 1 below). */
Neil Booth93c803682000-10-28 17:59:06 +00001533 for (i = 0; i < macro->paramc; i++)
Jim Blandy64d08262002-04-05 00:12:40 +00001534 len += NODE_LEN (macro->params[i]) + 1; /* "," */
Neil Booth93c803682000-10-28 17:59:06 +00001535 }
1536
Neil Booth4c2b6472000-11-11 13:19:01 +00001537 for (i = 0; i < macro->count; i++)
Neil Booth93c803682000-10-28 17:59:06 +00001538 {
Neil Booth4c2b6472000-11-11 13:19:01 +00001539 cpp_token *token = &macro->expansion[i];
Neil Booth93c803682000-10-28 17:59:06 +00001540
Neil Booth4c2b6472000-11-11 13:19:01 +00001541 if (token->type == CPP_MACRO_ARG)
Neil Bootha28c50352001-05-16 22:02:09 +00001542 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
Neil Booth4c2b6472000-11-11 13:19:01 +00001543 else
1544 len += cpp_token_len (token); /* Includes room for ' '. */
1545 if (token->flags & STRINGIFY_ARG)
1546 len++; /* "#" */
1547 if (token->flags & PASTE_LEFT)
1548 len += 3; /* " ##" */
Neil Booth93c803682000-10-28 17:59:06 +00001549 }
1550
1551 if (len > pfile->macro_buffer_len)
Alexandre Oliva4b49c362001-01-09 09:30:43 +00001552 {
1553 pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1554 pfile->macro_buffer_len = len;
1555 }
Neil Booth70961712001-06-23 11:34:41 +00001556
1557 /* Fill in the buffer. Start with the macro name. */
Neil Booth93c803682000-10-28 17:59:06 +00001558 buffer = pfile->macro_buffer;
Neil Booth70961712001-06-23 11:34:41 +00001559 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1560 buffer += NODE_LEN (node);
Neil Booth93c803682000-10-28 17:59:06 +00001561
1562 /* Parameter names. */
1563 if (macro->fun_like)
1564 {
1565 *buffer++ = '(';
1566 for (i = 0; i < macro->paramc; i++)
1567 {
1568 cpp_hashnode *param = macro->params[i];
1569
1570 if (param != pfile->spec_nodes.n__VA_ARGS__)
1571 {
Neil Bootha28c50352001-05-16 22:02:09 +00001572 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1573 buffer += NODE_LEN (param);
Neil Booth93c803682000-10-28 17:59:06 +00001574 }
1575
1576 if (i + 1 < macro->paramc)
Jim Blandy64d08262002-04-05 00:12:40 +00001577 /* Don't emit a space after the comma here; we're trying
1578 to emit a Dwarf-friendly definition, and the Dwarf spec
1579 forbids spaces in the argument list. */
1580 *buffer++ = ',';
Neil Booth28e0f042000-12-09 12:06:37 +00001581 else if (macro->variadic)
Neil Booth93c803682000-10-28 17:59:06 +00001582 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1583 }
1584 *buffer++ = ')';
1585 }
1586
Jim Blandye37b38d2002-03-19 21:43:39 +00001587 /* The Dwarf spec requires a space after the macro name, even if the
1588 definition is the empty string. */
1589 *buffer++ = ' ';
1590
Neil Booth93c803682000-10-28 17:59:06 +00001591 /* Expansion tokens. */
Neil Booth4c2b6472000-11-11 13:19:01 +00001592 if (macro->count)
Neil Booth93c803682000-10-28 17:59:06 +00001593 {
Neil Booth93c803682000-10-28 17:59:06 +00001594 for (i = 0; i < macro->count; i++)
1595 {
1596 cpp_token *token = &macro->expansion[i];
1597
1598 if (token->flags & PREV_WHITE)
1599 *buffer++ = ' ';
1600 if (token->flags & STRINGIFY_ARG)
1601 *buffer++ = '#';
1602
1603 if (token->type == CPP_MACRO_ARG)
1604 {
Neil Bootha28c50352001-05-16 22:02:09 +00001605 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1606 memcpy (buffer,
1607 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
Neil Booth93c803682000-10-28 17:59:06 +00001608 buffer += len;
1609 }
1610 else
1611 buffer = cpp_spell_token (pfile, token, buffer);
1612
1613 if (token->flags & PASTE_LEFT)
1614 {
1615 *buffer++ = ' ';
1616 *buffer++ = '#';
1617 *buffer++ = '#';
1618 /* Next has PREV_WHITE; see _cpp_create_definition. */
1619 }
1620 }
1621 }
1622
1623 *buffer = '\0';
1624 return pfile->macro_buffer;
1625}