blob: b0b10ae92d755f3e4d624a742b6c444016ab2bde [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 Booth23345bb2003-03-14 21:47:50 +00003 1999, 2000, 2001, 2002, 2003 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"
Zack Weinberg4977bab2002-12-16 18:23:00 +000028#include "coretypes.h"
29#include "tm.h"
Zack Weinberg711b8822000-07-18 00:59:49 +000030#include "cpplib.h"
31#include "cpphash.h"
32
Neil Booth93c803682000-10-28 17:59:06 +000033typedef struct macro_arg macro_arg;
34struct macro_arg
35{
Neil Booth4ed5bcf2001-09-24 22:53:12 +000036 const cpp_token **first; /* First token in unexpanded argument. */
Kazu Hirata6d2f8882001-10-10 11:33:39 +000037 const cpp_token **expanded; /* Macro-expanded argument. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +000038 const cpp_token *stringified; /* Stringified argument. */
Neil Booth93c803682000-10-28 17:59:06 +000039 unsigned int count; /* # of tokens in argument. */
40 unsigned int expanded_count; /* # of tokens in expanded argument. */
41};
Zack Weinberg711b8822000-07-18 00:59:49 +000042
Neil Booth93c803682000-10-28 17:59:06 +000043/* Macro expansion. */
44
Neil Booth29b10742000-11-13 18:40:37 +000045static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
Neil Booth644edda2001-10-02 12:57:24 +000046static int builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
Neil Booth4ed5bcf2001-09-24 22:53:12 +000047static void push_token_context
Neil Booth644edda2001-10-02 12:57:24 +000048 PARAMS ((cpp_reader *, cpp_hashnode *, const cpp_token *, unsigned int));
Neil Booth4ed5bcf2001-09-24 22:53:12 +000049static void push_ptoken_context
Neil Booth644edda2001-10-02 12:57:24 +000050 PARAMS ((cpp_reader *, cpp_hashnode *, _cpp_buff *,
Neil Booth1e013d22001-09-26 21:44:35 +000051 const cpp_token **, unsigned int));
Neil Boothb8af0ca2001-09-26 17:52:50 +000052static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +000053static cpp_context *next_context PARAMS ((cpp_reader *));
Neil Booth4ed5bcf2001-09-24 22:53:12 +000054static const cpp_token *padding_token
55 PARAMS ((cpp_reader *, const cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +000056static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
Neil Booth562a5c22002-04-21 18:46:42 +000057static const cpp_token *new_string_token PARAMS ((cpp_reader *, uchar *,
Neil Booth4ed5bcf2001-09-24 22:53:12 +000058 unsigned int));
Neil Booth4ed5bcf2001-09-24 22:53:12 +000059static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
60static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
Neil Boothc9e7a602001-09-27 12:59:38 +000061static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
62 const cpp_token *));
Neil Boothe808ec92002-02-27 07:24:53 +000063static void replace_args PARAMS ((cpp_reader *, cpp_hashnode *, cpp_macro *,
64 macro_arg *));
Neil Boothd6da8362001-10-08 06:15:14 +000065static _cpp_buff *funlike_invocation_p PARAMS ((cpp_reader *, cpp_hashnode *));
Neil Boothcbc69f82002-06-05 20:27:12 +000066static bool create_iso_definition PARAMS ((cpp_reader *, cpp_macro *));
Neil Booth93c803682000-10-28 17:59:06 +000067
Neil Booth93c803682000-10-28 17:59:06 +000068/* #define directive parsing and handling. */
69
Neil Booth14baae02001-09-17 18:26:12 +000070static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
Neil Booth93c803682000-10-28 17:59:06 +000071static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
Neil Boothcbc69f82002-06-05 20:27:12 +000072static bool warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
73 const cpp_macro *));
Neil Booth23ff0222002-07-17 17:27:14 +000074static bool parse_params PARAMS ((cpp_reader *, cpp_macro *));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +000075static void check_trad_stringification PARAMS ((cpp_reader *,
Neil Booth93c803682000-10-28 17:59:06 +000076 const cpp_macro *,
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +000077 const cpp_string *));
Zack Weinberg711b8822000-07-18 00:59:49 +000078
Neil Bootha69cbaa2002-07-23 22:57:49 +000079/* Emits a warning if NODE is a macro defined in the main file that
80 has not been used. */
81int
82_cpp_warn_if_unused_macro (pfile, node, v)
83 cpp_reader *pfile;
84 cpp_hashnode *node;
85 void *v ATTRIBUTE_UNUSED;
86{
87 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
88 {
89 cpp_macro *macro = node->value.macro;
90
91 if (!macro->used
Neil Bootha69cbaa2002-07-23 22:57:49 +000092 && MAIN_FILE_P (lookup_line (&pfile->line_maps, macro->line)))
93 cpp_error_with_line (pfile, DL_WARNING, macro->line, 0,
94 "macro \"%s\" is not used", NODE_NAME (node));
95 }
96
97 return 1;
98}
99
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000100/* Allocates and returns a CPP_STRING token, containing TEXT of length
101 LEN, after null-terminating it. TEXT must be in permanent storage. */
102static const cpp_token *
103new_string_token (pfile, text, len)
104 cpp_reader *pfile;
105 unsigned char *text;
Neil Booth93c803682000-10-28 17:59:06 +0000106 unsigned int len;
Zack Weinberg711b8822000-07-18 00:59:49 +0000107{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000108 cpp_token *token = _cpp_temp_token (pfile);
Zack Weinberg711b8822000-07-18 00:59:49 +0000109
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000110 text[len] = '\0';
Neil Booth93c803682000-10-28 17:59:06 +0000111 token->type = CPP_STRING;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000112 token->val.str.len = len;
113 token->val.str.text = text;
Neil Booth93c803682000-10-28 17:59:06 +0000114 token->flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000115 return token;
Neil Booth93c803682000-10-28 17:59:06 +0000116}
117
Neil Booth93c803682000-10-28 17:59:06 +0000118static const char * const monthnames[] =
119{
120 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
121 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
122};
123
Neil Booth644edda2001-10-02 12:57:24 +0000124/* Handle builtin macros like __FILE__, and push the resulting token
125 on the context stack. Also handles _Pragma, for which no new token
Neil Boothd15a58c2002-01-03 18:32:55 +0000126 is created. Returns 1 if it generates a new token context, 0 to
127 return the token to the caller. */
Neil Booth278c4662002-06-19 05:40:08 +0000128const uchar *
129_cpp_builtin_macro_text (pfile, node)
Neil Booth93c803682000-10-28 17:59:06 +0000130 cpp_reader *pfile;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000131 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +0000132{
Neil Booth278c4662002-06-19 05:40:08 +0000133 const uchar *result = NULL;
134 unsigned int number = 1;
Neil Booth644edda2001-10-02 12:57:24 +0000135
Neil Booth93c803682000-10-28 17:59:06 +0000136 switch (node->value.builtin)
137 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000138 default:
Neil Boothebef4e82002-04-14 18:42:47 +0000139 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
140 NODE_NAME (node));
Neil Booth278c4662002-06-19 05:40:08 +0000141 break;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000142
Neil Booth93c803682000-10-28 17:59:06 +0000143 case BT_FILE:
144 case BT_BASE_FILE:
Zack Weinberg711b8822000-07-18 00:59:49 +0000145 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000146 unsigned int len;
Neil Booth0bda4762000-12-11 07:45:16 +0000147 const char *name;
Neil Booth562a5c22002-04-21 18:46:42 +0000148 uchar *buf;
Neil Boothbb74c962001-08-17 22:23:49 +0000149 const struct line_map *map = pfile->map;
Neil Booth93c803682000-10-28 17:59:06 +0000150
Neil Booth0bda4762000-12-11 07:45:16 +0000151 if (node->value.builtin == BT_BASE_FILE)
Neil Boothbb74c962001-08-17 22:23:49 +0000152 while (! MAIN_FILE_P (map))
153 map = INCLUDED_FROM (&pfile->line_maps, map);
Neil Booth93c803682000-10-28 17:59:06 +0000154
Neil Boothbb74c962001-08-17 22:23:49 +0000155 name = map->to_file;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000156 len = strlen (name);
Neil Booth278c4662002-06-19 05:40:08 +0000157 buf = _cpp_unaligned_alloc (pfile, len * 4 + 3);
158 result = buf;
159 *buf = '"';
160 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
161 *buf++ = '"';
162 *buf = '\0';
Zack Weinberg711b8822000-07-18 00:59:49 +0000163 }
Neil Booth644edda2001-10-02 12:57:24 +0000164 break;
165
Neil Booth93c803682000-10-28 17:59:06 +0000166 case BT_INCLUDE_LEVEL:
Neil Boothd8693c62001-08-21 23:05:12 +0000167 /* The line map depth counts the primary source as level 1, but
168 historically __INCLUDE_DEPTH__ has called the primary source
169 level 0. */
Neil Booth278c4662002-06-19 05:40:08 +0000170 number = pfile->line_maps.depth - 1;
Neil Booth644edda2001-10-02 12:57:24 +0000171 break;
Neil Booth93c803682000-10-28 17:59:06 +0000172
173 case BT_SPECLINE:
174 /* If __LINE__ is embedded in a macro, it must expand to the
175 line of the macro's invocation, not its definition.
176 Otherwise things like assert() will not work properly. */
Neil Booth278c4662002-06-19 05:40:08 +0000177 if (CPP_OPTION (pfile, traditional))
178 number = pfile->line;
179 else
180 number = pfile->cur_token[-1].line;
181 number = SOURCE_LINE (pfile->map, number);
Neil Booth644edda2001-10-02 12:57:24 +0000182 break;
Neil Booth93c803682000-10-28 17:59:06 +0000183
Zack Weinberg5279d732002-05-16 19:03:02 +0000184 /* __STDC__ has the value 1 under normal circumstances.
185 However, if (a) we are in a system header, (b) the option
Zack Weinberg2a1dc0d2002-05-21 21:55:37 +0000186 stdc_0_in_system_headers is true (set by target config), and
187 (c) we are not in strictly conforming mode, then it has the
188 value 0. */
Neil Booth93c803682000-10-28 17:59:06 +0000189 case BT_STDC:
190 {
Zack Weinberg5279d732002-05-16 19:03:02 +0000191 if (CPP_IN_SYSTEM_HEADER (pfile)
192 && CPP_OPTION (pfile, stdc_0_in_system_headers)
Neil Booth58551c22002-08-06 20:35:46 +0000193 && !CPP_OPTION (pfile,std))
Neil Booth278c4662002-06-19 05:40:08 +0000194 number = 0;
Zack Weinberg5279d732002-05-16 19:03:02 +0000195 else
Neil Booth278c4662002-06-19 05:40:08 +0000196 number = 1;
Neil Booth93c803682000-10-28 17:59:06 +0000197 }
Neil Booth644edda2001-10-02 12:57:24 +0000198 break;
Neil Booth93c803682000-10-28 17:59:06 +0000199
200 case BT_DATE:
201 case BT_TIME:
Neil Booth278c4662002-06-19 05:40:08 +0000202 if (pfile->date == NULL)
Neil Booth93c803682000-10-28 17:59:06 +0000203 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000204 /* Allocate __DATE__ and __TIME__ strings from permanent
205 storage. We only do this once, and don't generate them
206 at init time, because time() and localtime() are very
207 slow on some systems. */
Zack Weinberg56da7202002-08-02 04:18:16 +0000208 time_t tt;
209 struct tm *tb = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000210
Zack Weinberg56da7202002-08-02 04:18:16 +0000211 /* (time_t) -1 is a legitimate value for "number of seconds
212 since the Epoch", so we have to do a little dance to
213 distinguish that from a genuine error. */
214 errno = 0;
215 tt = time(NULL);
216 if (tt != (time_t)-1 || errno == 0)
217 tb = localtime (&tt);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000218
Zack Weinberg56da7202002-08-02 04:18:16 +0000219 if (tb)
220 {
221 pfile->date = _cpp_unaligned_alloc (pfile,
222 sizeof ("\"Oct 11 1347\""));
223 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
224 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
225
226 pfile->time = _cpp_unaligned_alloc (pfile,
227 sizeof ("\"12:34:56\""));
228 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
229 tb->tm_hour, tb->tm_min, tb->tm_sec);
230 }
231 else
232 {
233 cpp_errno (pfile, DL_WARNING,
234 "could not determine date and time");
235
236 pfile->date = U"\"??? ?? ????\"";
237 pfile->time = U"\"??:??:??\"";
238 }
Neil Booth93c803682000-10-28 17:59:06 +0000239 }
Neil Booth93c803682000-10-28 17:59:06 +0000240
Neil Booth644edda2001-10-02 12:57:24 +0000241 if (node->value.builtin == BT_DATE)
Neil Booth278c4662002-06-19 05:40:08 +0000242 result = pfile->date;
Neil Booth644edda2001-10-02 12:57:24 +0000243 else
Neil Booth278c4662002-06-19 05:40:08 +0000244 result = pfile->time;
Neil Booth644edda2001-10-02 12:57:24 +0000245 break;
Neil Booth278c4662002-06-19 05:40:08 +0000246 }
Neil Booth644edda2001-10-02 12:57:24 +0000247
Neil Booth278c4662002-06-19 05:40:08 +0000248 if (result == NULL)
249 {
250 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
251 result = _cpp_unaligned_alloc (pfile, 21);
252 sprintf ((char *) result, "%u", number);
253 }
254
255 return result;
256}
257
258/* Convert builtin macros like __FILE__ to a token and push it on the
259 context stack. Also handles _Pragma, for which no new token is
260 created. Returns 1 if it generates a new token context, 0 to
261 return the token to the caller. */
262static int
263builtin_macro (pfile, node)
264 cpp_reader *pfile;
265 cpp_hashnode *node;
266{
267 const uchar *buf;
Neil Booth26aea072003-04-19 00:22:51 +0000268 size_t len;
269 char *nbuf;
Neil Booth278c4662002-06-19 05:40:08 +0000270
271 if (node->value.builtin == BT_PRAGMA)
272 {
Neil Booth644edda2001-10-02 12:57:24 +0000273 /* Don't interpret _Pragma within directives. The standard is
274 not clear on this, but to me this makes most sense. */
275 if (pfile->state.in_directive)
276 return 0;
277
278 _cpp_do__Pragma (pfile);
279 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000280 }
Neil Booth644edda2001-10-02 12:57:24 +0000281
Neil Booth278c4662002-06-19 05:40:08 +0000282 buf = _cpp_builtin_macro_text (pfile, node);
Neil Booth26aea072003-04-19 00:22:51 +0000283 len = ustrlen (buf);
284 nbuf = alloca (len + 1);
285 memcpy (nbuf, buf, len);
286 nbuf[len]='\n';
Neil Booth278c4662002-06-19 05:40:08 +0000287
Neil Booth26aea072003-04-19 00:22:51 +0000288 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true, 1);
289 _cpp_clean_line (pfile);
Neil Booth278c4662002-06-19 05:40:08 +0000290
291 /* Set pfile->cur_token as required by _cpp_lex_direct. */
292 pfile->cur_token = _cpp_temp_token (pfile);
293 push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
294 if (pfile->buffer->cur != pfile->buffer->rlimit)
295 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
296 NODE_NAME (node));
297 _cpp_pop_buffer (pfile);
298
Neil Booth644edda2001-10-02 12:57:24 +0000299 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000300}
301
Neil Boothd15a58c2002-01-03 18:32:55 +0000302/* Copies SRC, of length LEN, to DEST, adding backslashes before all
303 backslashes and double quotes. Non-printable characters are
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000304 converted to octal. DEST must be of sufficient size. Returns
305 a pointer to the end of the string. */
Neil Booth562a5c22002-04-21 18:46:42 +0000306uchar *
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000307cpp_quote_string (dest, src, len)
Neil Booth562a5c22002-04-21 18:46:42 +0000308 uchar *dest;
309 const uchar *src;
Neil Booth93c803682000-10-28 17:59:06 +0000310 unsigned int len;
311{
312 while (len--)
313 {
Neil Booth562a5c22002-04-21 18:46:42 +0000314 uchar c = *src++;
Neil Booth93c803682000-10-28 17:59:06 +0000315
316 if (c == '\\' || c == '"')
317 {
318 *dest++ = '\\';
319 *dest++ = c;
320 }
321 else
322 {
323 if (ISPRINT (c))
324 *dest++ = c;
325 else
326 {
327 sprintf ((char *) dest, "\\%03o", c);
328 dest += 4;
329 }
330 }
331 }
332
333 return dest;
334}
335
Neil Boothd15a58c2002-01-03 18:32:55 +0000336/* Convert a token sequence ARG to a single string token according to
337 the rules of the ISO C #-operator. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000338static const cpp_token *
Neil Booth93c803682000-10-28 17:59:06 +0000339stringify_arg (pfile, arg)
340 cpp_reader *pfile;
341 macro_arg *arg;
342{
Neil Booth6338b352003-04-23 22:44:06 +0000343 unsigned char *dest;
Neil Boothece54d52001-09-28 09:40:22 +0000344 unsigned int i, escape_it, backslash_count = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000345 const cpp_token *source = NULL;
Neil Boothece54d52001-09-28 09:40:22 +0000346 size_t len;
Neil Booth93c803682000-10-28 17:59:06 +0000347
Neil Booth6338b352003-04-23 22:44:06 +0000348 if (BUFF_ROOM (pfile->u_buff) < 3)
349 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
350 dest = BUFF_FRONT (pfile->u_buff);
351 *dest++ = '"';
352
Neil Booth93c803682000-10-28 17:59:06 +0000353 /* Loop, reading in the argument's tokens. */
354 for (i = 0; i < arg->count; i++)
355 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000356 const cpp_token *token = arg->first[i];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000357
358 if (token->type == CPP_PADDING)
359 {
360 if (source == NULL)
361 source = token->val.source;
362 continue;
363 }
Neil Booth93c803682000-10-28 17:59:06 +0000364
365 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
Zack Weinbergcc937582001-03-07 01:32:01 +0000366 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
Neil Booth93c803682000-10-28 17:59:06 +0000367
Neil Boothece54d52001-09-28 09:40:22 +0000368 /* Room for each char being written in octal, initial space and
Neil Booth6338b352003-04-23 22:44:06 +0000369 final quote and NUL. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000370 len = cpp_token_len (token);
Neil Booth93c803682000-10-28 17:59:06 +0000371 if (escape_it)
Neil Booth93c803682000-10-28 17:59:06 +0000372 len *= 4;
Neil Booth6338b352003-04-23 22:44:06 +0000373 len += 3;
Neil Booth93c803682000-10-28 17:59:06 +0000374
Neil Boothece54d52001-09-28 09:40:22 +0000375 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth93c803682000-10-28 17:59:06 +0000376 {
Neil Boothece54d52001-09-28 09:40:22 +0000377 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +0000378 _cpp_extend_buff (pfile, &pfile->u_buff, len);
Neil Boothece54d52001-09-28 09:40:22 +0000379 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth93c803682000-10-28 17:59:06 +0000380 }
381
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000382 /* Leading white space? */
Neil Booth6338b352003-04-23 22:44:06 +0000383 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000384 {
385 if (source == NULL)
386 source = token;
387 if (source->flags & PREV_WHITE)
388 *dest++ = ' ';
389 }
390 source = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000391
392 if (escape_it)
393 {
Neil Boothece54d52001-09-28 09:40:22 +0000394 _cpp_buff *buff = _cpp_get_buff (pfile, len);
395 unsigned char *buf = BUFF_FRONT (buff);
Neil Booth93c803682000-10-28 17:59:06 +0000396 len = cpp_spell_token (pfile, token, buf) - buf;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000397 dest = cpp_quote_string (dest, buf, len);
Neil Boothece54d52001-09-28 09:40:22 +0000398 _cpp_release_buff (pfile, buff);
Neil Booth93c803682000-10-28 17:59:06 +0000399 }
400 else
401 dest = cpp_spell_token (pfile, token, dest);
Neil Booth93c803682000-10-28 17:59:06 +0000402
Neil Booth10676942003-04-22 19:28:00 +0000403 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
Neil Booth93c803682000-10-28 17:59:06 +0000404 backslash_count++;
405 else
406 backslash_count = 0;
407 }
408
409 /* Ignore the final \ of invalid string literals. */
410 if (backslash_count & 1)
411 {
Neil Boothebef4e82002-04-14 18:42:47 +0000412 cpp_error (pfile, DL_WARNING,
413 "invalid string literal, ignoring final '\\'");
Neil Boothece54d52001-09-28 09:40:22 +0000414 dest--;
Neil Booth93c803682000-10-28 17:59:06 +0000415 }
416
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000417 /* Commit the memory, including NUL, and return the token. */
Neil Booth6338b352003-04-23 22:44:06 +0000418 *dest++ = '"';
Neil Boothece54d52001-09-28 09:40:22 +0000419 len = dest - BUFF_FRONT (pfile->u_buff);
420 BUFF_FRONT (pfile->u_buff) = dest + 1;
421 return new_string_token (pfile, dest - len, len);
Neil Booth93c803682000-10-28 17:59:06 +0000422}
423
Kazu Hiratada7d8302002-09-22 02:03:17 +0000424/* Try to paste two tokens. On success, return nonzero. In any
Neil Boothc9e7a602001-09-27 12:59:38 +0000425 case, PLHS is updated to point to the pasted token, which is
426 guaranteed to not have the PASTE_LEFT flag set. */
427static bool
428paste_tokens (pfile, plhs, rhs)
Neil Boothd63eefb2000-11-20 23:59:26 +0000429 cpp_reader *pfile;
Neil Boothc9e7a602001-09-27 12:59:38 +0000430 const cpp_token **plhs, *rhs;
Neil Boothd63eefb2000-11-20 23:59:26 +0000431{
Neil Boothc9e7a602001-09-27 12:59:38 +0000432 unsigned char *buf, *end;
433 const cpp_token *lhs;
434 unsigned int len;
435 bool valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000436
Neil Boothc9e7a602001-09-27 12:59:38 +0000437 lhs = *plhs;
438 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
439 buf = (unsigned char *) alloca (len);
440 end = cpp_spell_token (pfile, lhs, buf);
Neil Boothd63eefb2000-11-20 23:59:26 +0000441
Neil Boothc9e7a602001-09-27 12:59:38 +0000442 /* Avoid comment headers, since they are still processed in stage 3.
443 It is simpler to insert a space here, rather than modifying the
444 lexer to ignore comments in some circumstances. Simply returning
445 false doesn't work, since we want to clear the PASTE_LEFT flag. */
Neil Booth5cc67322002-10-09 09:56:09 +0000446 if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
Neil Boothc9e7a602001-09-27 12:59:38 +0000447 *end++ = ' ';
448 end = cpp_spell_token (pfile, rhs, end);
Neil Booth26aea072003-04-19 00:22:51 +0000449 *end = '\n';
Neil Boothd63eefb2000-11-20 23:59:26 +0000450
Neil Boothc9e7a602001-09-27 12:59:38 +0000451 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
Neil Booth26aea072003-04-19 00:22:51 +0000452 _cpp_clean_line (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000453
Neil Boothc9e7a602001-09-27 12:59:38 +0000454 /* Set pfile->cur_token as required by _cpp_lex_direct. */
455 pfile->cur_token = _cpp_temp_token (pfile);
456 *plhs = _cpp_lex_direct (pfile);
Neil Booth480709c2001-10-21 14:04:42 +0000457 valid = pfile->buffer->cur == pfile->buffer->rlimit;
Neil Boothc9e7a602001-09-27 12:59:38 +0000458 _cpp_pop_buffer (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000459
Neil Boothc9e7a602001-09-27 12:59:38 +0000460 return valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000461}
462
Neil Boothd15a58c2002-01-03 18:32:55 +0000463/* Handles an arbitrarily long sequence of ## operators, with initial
464 operand LHS. This implementation is left-associative,
465 non-recursive, and finishes a paste before handling succeeding
466 ones. If a paste fails, we back up to the RHS of the failing ##
467 operator before pushing the context containing the result of prior
468 successful pastes, with the effect that the RHS appears in the
469 output stream after the pasted LHS normally. */
Neil Booth93c803682000-10-28 17:59:06 +0000470static void
471paste_all_tokens (pfile, lhs)
472 cpp_reader *pfile;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000473 const cpp_token *lhs;
Neil Booth93c803682000-10-28 17:59:06 +0000474{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000475 const cpp_token *rhs;
476 cpp_context *context = pfile->context;
477
Neil Booth93c803682000-10-28 17:59:06 +0000478 do
479 {
480 /* Take the token directly from the current context. We can do
481 this, because we are in the replacement list of either an
482 object-like macro, or a function-like macro with arguments
483 inserted. In either case, the constraints to #define
Neil Boothd63eefb2000-11-20 23:59:26 +0000484 guarantee we have at least one more token. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000485 if (context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +0000486 rhs = FIRST (context).token++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000487 else
Neil Booth82eda772002-06-04 13:07:06 +0000488 rhs = *FIRST (context).ptoken++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000489
490 if (rhs->type == CPP_PADDING)
491 abort ();
492
Neil Boothc9e7a602001-09-27 12:59:38 +0000493 if (!paste_tokens (pfile, &lhs, rhs))
Neil Booth93c803682000-10-28 17:59:06 +0000494 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000495 _cpp_backup_tokens (pfile, 1);
Neil Boothc9e7a602001-09-27 12:59:38 +0000496
Neil Boothc3bf3e62002-05-09 17:14:22 +0000497 /* Mandatory error for all apart from assembler. */
Neil Boothc9e7a602001-09-27 12:59:38 +0000498 if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Boothc3bf3e62002-05-09 17:14:22 +0000499 cpp_error (pfile, DL_ERROR,
Neil Boothc9e7a602001-09-27 12:59:38 +0000500 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
Neil Boothebef4e82002-04-14 18:42:47 +0000501 cpp_token_as_text (pfile, lhs),
502 cpp_token_as_text (pfile, rhs));
Neil Booth4c2b6472000-11-11 13:19:01 +0000503 break;
Neil Booth93c803682000-10-28 17:59:06 +0000504 }
Neil Booth93c803682000-10-28 17:59:06 +0000505 }
506 while (rhs->flags & PASTE_LEFT);
507
Neil Boothc9e7a602001-09-27 12:59:38 +0000508 /* Put the resulting token in its own context. */
509 push_token_context (pfile, NULL, lhs, 1);
Neil Booth93c803682000-10-28 17:59:06 +0000510}
511
Neil Booth1ce676a2002-06-09 20:04:17 +0000512/* Returns TRUE if the number of arguments ARGC supplied in an
513 invocation of the MACRO referenced by NODE is valid. An empty
514 invocation to a macro with no parameters should pass ARGC as zero.
515
516 Note that MACRO cannot necessarily be deduced from NODE, in case
517 NODE was redefined whilst collecting arguments. */
518bool
519_cpp_arguments_ok (pfile, macro, node, argc)
520 cpp_reader *pfile;
521 cpp_macro *macro;
522 const cpp_hashnode *node;
523 unsigned int argc;
524{
525 if (argc == macro->paramc)
526 return true;
527
528 if (argc < macro->paramc)
529 {
530 /* As an extension, a rest argument is allowed to not appear in
531 the invocation at all.
532 e.g. #define debug(format, args...) something
533 debug("string");
534
535 This is exactly the same as if there had been an empty rest
536 argument - debug("string", ). */
537
538 if (argc + 1 == macro->paramc && macro->variadic)
539 {
540 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
541 cpp_error (pfile, DL_PEDWARN,
542 "ISO C99 requires rest arguments to be used");
543 return true;
544 }
545
546 cpp_error (pfile, DL_ERROR,
547 "macro \"%s\" requires %u arguments, but only %u given",
548 NODE_NAME (node), macro->paramc, argc);
549 }
550 else
551 cpp_error (pfile, DL_ERROR,
552 "macro \"%s\" passed %u arguments, but takes just %u",
553 NODE_NAME (node), argc, macro->paramc);
554
555 return false;
556}
557
Neil Boothd15a58c2002-01-03 18:32:55 +0000558/* Reads and returns the arguments to a function-like macro
559 invocation. Assumes the opening parenthesis has been processed.
560 If there is an error, emits an appropriate diagnostic and returns
561 NULL. Each argument is terminated by a CPP_EOF token, for the
562 future benefit of expand_arg(). */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000563static _cpp_buff *
564collect_args (pfile, node)
Neil Booth93c803682000-10-28 17:59:06 +0000565 cpp_reader *pfile;
566 const cpp_hashnode *node;
567{
Neil Boothb8af0ca2001-09-26 17:52:50 +0000568 _cpp_buff *buff, *base_buff;
569 cpp_macro *macro;
570 macro_arg *args, *arg;
571 const cpp_token *token;
572 unsigned int argc;
Neil Booth93c803682000-10-28 17:59:06 +0000573
Neil Boothb8af0ca2001-09-26 17:52:50 +0000574 macro = node->value.macro;
575 if (macro->paramc)
576 argc = macro->paramc;
577 else
578 argc = 1;
579 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
580 + sizeof (macro_arg)));
581 base_buff = buff;
582 args = (macro_arg *) buff->base;
583 memset (args, 0, argc * sizeof (macro_arg));
Neil Boothece54d52001-09-28 09:40:22 +0000584 buff->cur = (unsigned char *) &args[argc];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000585 arg = args, argc = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000586
Neil Boothb8af0ca2001-09-26 17:52:50 +0000587 /* Collect the tokens making up each argument. We don't yet know
588 how many arguments have been supplied, whether too many or too
589 few. Hence the slightly bizarre usage of "argc" and "arg". */
590 do
Neil Booth93c803682000-10-28 17:59:06 +0000591 {
Neil Boothb8af0ca2001-09-26 17:52:50 +0000592 unsigned int paren_depth = 0;
593 unsigned int ntokens = 0;
594
Neil Booth93c803682000-10-28 17:59:06 +0000595 argc++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000596 arg->first = (const cpp_token **) buff->cur;
Neil Booth93c803682000-10-28 17:59:06 +0000597
Neil Boothb8af0ca2001-09-26 17:52:50 +0000598 for (;;)
599 {
600 /* Require space for 2 new tokens (including a CPP_EOF). */
Neil Boothece54d52001-09-28 09:40:22 +0000601 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000602 {
Neil Booth8c3b2692001-09-30 10:03:11 +0000603 buff = _cpp_append_extend_buff (pfile, buff,
604 1000 * sizeof (cpp_token *));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000605 arg->first = (const cpp_token **) buff->cur;
606 }
Neil Booth93c803682000-10-28 17:59:06 +0000607
Neil Boothb8af0ca2001-09-26 17:52:50 +0000608 token = cpp_get_token (pfile);
609
610 if (token->type == CPP_PADDING)
611 {
612 /* Drop leading padding. */
613 if (ntokens == 0)
614 continue;
615 }
616 else if (token->type == CPP_OPEN_PAREN)
617 paren_depth++;
618 else if (token->type == CPP_CLOSE_PAREN)
619 {
620 if (paren_depth-- == 0)
621 break;
622 }
623 else if (token->type == CPP_COMMA)
624 {
625 /* A comma does not terminate an argument within
626 parentheses or as part of a variable argument. */
627 if (paren_depth == 0
628 && ! (macro->variadic && argc == macro->paramc))
629 break;
630 }
631 else if (token->type == CPP_EOF
632 || (token->type == CPP_HASH && token->flags & BOL))
633 break;
634
635 arg->first[ntokens++] = token;
636 }
637
638 /* Drop trailing padding. */
639 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
640 ntokens--;
641
642 arg->count = ntokens;
643 arg->first[ntokens] = &pfile->eof;
644
645 /* Terminate the argument. Excess arguments loop back and
646 overwrite the final legitimate argument, before failing. */
647 if (argc <= macro->paramc)
648 {
Neil Boothece54d52001-09-28 09:40:22 +0000649 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000650 if (argc != macro->paramc)
651 arg++;
652 }
Neil Booth93c803682000-10-28 17:59:06 +0000653 }
Neil Boothe808ec92002-02-27 07:24:53 +0000654 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
Neil Booth93c803682000-10-28 17:59:06 +0000655
Neil Boothe808ec92002-02-27 07:24:53 +0000656 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000657 {
Neil Boothece54d52001-09-28 09:40:22 +0000658 /* We still need the CPP_EOF to end directives, and to end
659 pre-expansion of a macro argument. Step back is not
660 unconditional, since we don't want to return a CPP_EOF to our
661 callers at the end of an -include-d file. */
Neil Boothe808ec92002-02-27 07:24:53 +0000662 if (pfile->context->prev || pfile->state.in_directive)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000663 _cpp_backup_tokens (pfile, 1);
Neil Boothebef4e82002-04-14 18:42:47 +0000664 cpp_error (pfile, DL_ERROR,
665 "unterminated argument list invoking macro \"%s\"",
Neil Bootha28c50352001-05-16 22:02:09 +0000666 NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +0000667 }
Neil Booth1ce676a2002-06-09 20:04:17 +0000668 else
Neil Booth93c803682000-10-28 17:59:06 +0000669 {
Neil Booth1ce676a2002-06-09 20:04:17 +0000670 /* A single empty argument is counted as no argument. */
671 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
672 argc = 0;
673 if (_cpp_arguments_ok (pfile, macro, node, argc))
Neil Booth58551c22002-08-06 20:35:46 +0000674 {
675 /* GCC has special semantics for , ## b where b is a varargs
676 parameter: we remove the comma if b was omitted entirely.
677 If b was merely an empty argument, the comma is retained.
678 If the macro takes just one (varargs) parameter, then we
679 retain the comma only if we are standards conforming.
680
681 If FIRST is NULL replace_args () swallows the comma. */
682 if (macro->variadic && (argc < macro->paramc
683 || (argc == 1 && args[0].count == 0
684 && !CPP_OPTION (pfile, std))))
685 args[macro->paramc - 1].first = NULL;
686 return base_buff;
687 }
Neil Booth93c803682000-10-28 17:59:06 +0000688 }
689
Neil Booth1ce676a2002-06-09 20:04:17 +0000690 /* An error occurred. */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000691 _cpp_release_buff (pfile, base_buff);
692 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000693}
694
Neil Boothd6da8362001-10-08 06:15:14 +0000695/* Search for an opening parenthesis to the macro of NODE, in such a
696 way that, if none is found, we don't lose the information in any
697 intervening padding tokens. If we find the parenthesis, collect
698 the arguments and return the buffer containing them. */
699static _cpp_buff *
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000700funlike_invocation_p (pfile, node)
Neil Booth93c803682000-10-28 17:59:06 +0000701 cpp_reader *pfile;
Neil Booth644edda2001-10-02 12:57:24 +0000702 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +0000703{
Neil Boothd6da8362001-10-08 06:15:14 +0000704 const cpp_token *token, *padding = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000705
Neil Boothd6da8362001-10-08 06:15:14 +0000706 for (;;)
Neil Boothbdcbe492001-09-13 20:05:17 +0000707 {
Neil Boothd6da8362001-10-08 06:15:14 +0000708 token = cpp_get_token (pfile);
709 if (token->type != CPP_PADDING)
710 break;
711 if (padding == NULL
712 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
713 padding = token;
Neil Boothbdcbe492001-09-13 20:05:17 +0000714 }
Neil Booth93c803682000-10-28 17:59:06 +0000715
Neil Boothd6da8362001-10-08 06:15:14 +0000716 if (token->type == CPP_OPEN_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000717 {
Neil Boothd6da8362001-10-08 06:15:14 +0000718 pfile->state.parsing_args = 2;
719 return collect_args (pfile, node);
Neil Booth93c803682000-10-28 17:59:06 +0000720 }
721
Neil Booth9ac3b1b2002-04-21 16:17:55 +0000722 /* CPP_EOF can be the end of macro arguments, or the end of the
723 file. We mustn't back up over the latter. Ugh. */
724 if (token->type != CPP_EOF || token == &pfile->eof)
725 {
726 /* Back up. We may have skipped padding, in which case backing
727 up more than one token when expanding macros is in general
728 too difficult. We re-insert it in its own context. */
729 _cpp_backup_tokens (pfile, 1);
730 if (padding)
731 push_token_context (pfile, NULL, padding, 1);
732 }
Neil Boothd6da8362001-10-08 06:15:14 +0000733
734 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000735}
736
Neil Boothd15a58c2002-01-03 18:32:55 +0000737/* Push the context of a macro with hash entry NODE onto the context
738 stack. If we can successfully expand the macro, we push a context
739 containing its yet-to-be-rescanned replacement list and return one.
740 Otherwise, we don't push a context and return zero. */
Neil Booth93c803682000-10-28 17:59:06 +0000741static int
Neil Booth29b10742000-11-13 18:40:37 +0000742enter_macro_context (pfile, node)
Neil Booth93c803682000-10-28 17:59:06 +0000743 cpp_reader *pfile;
Neil Booth29b10742000-11-13 18:40:37 +0000744 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +0000745{
Neil Boothd15a58c2002-01-03 18:32:55 +0000746 /* The presence of a macro invalidates a file's controlling macro. */
Neil Booth644edda2001-10-02 12:57:24 +0000747 pfile->mi_valid = false;
748
Neil Booth36207112002-05-24 19:26:30 +0000749 pfile->state.angled_headers = false;
750
Neil Boothd15a58c2002-01-03 18:32:55 +0000751 /* Handle standard macros. */
Neil Booth644edda2001-10-02 12:57:24 +0000752 if (! (node->flags & NODE_BUILTIN))
Neil Booth93c803682000-10-28 17:59:06 +0000753 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000754 cpp_macro *macro = node->value.macro;
755
Neil Boothd6da8362001-10-08 06:15:14 +0000756 if (macro->fun_like)
757 {
758 _cpp_buff *buff;
759
760 pfile->state.prevent_expansion++;
761 pfile->keep_tokens++;
762 pfile->state.parsing_args = 1;
763 buff = funlike_invocation_p (pfile, node);
764 pfile->state.parsing_args = 0;
765 pfile->keep_tokens--;
766 pfile->state.prevent_expansion--;
767
768 if (buff == NULL)
769 {
770 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
Neil Boothebef4e82002-04-14 18:42:47 +0000771 cpp_error (pfile, DL_WARNING,
Neil Boothd6da8362001-10-08 06:15:14 +0000772 "function-like macro \"%s\" must be used with arguments in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +0000773 NODE_NAME (node));
Neil Boothd6da8362001-10-08 06:15:14 +0000774
775 return 0;
776 }
777
Neil Boothe808ec92002-02-27 07:24:53 +0000778 if (macro->paramc > 0)
779 replace_args (pfile, node, macro, (macro_arg *) buff->base);
Neil Boothd6da8362001-10-08 06:15:14 +0000780 _cpp_release_buff (pfile, buff);
781 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000782
783 /* Disable the macro within its expansion. */
Neil Booth644edda2001-10-02 12:57:24 +0000784 node->flags |= NODE_DISABLED;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000785
Neil Bootha69cbaa2002-07-23 22:57:49 +0000786 macro->used = 1;
787
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000788 if (macro->paramc == 0)
Neil Booth601328b2002-05-16 05:53:24 +0000789 push_token_context (pfile, node, macro->exp.tokens, macro->count);
Neil Booth644edda2001-10-02 12:57:24 +0000790
791 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000792 }
Neil Booth644edda2001-10-02 12:57:24 +0000793
Neil Boothd15a58c2002-01-03 18:32:55 +0000794 /* Handle built-in macros and the _Pragma operator. */
Neil Booth644edda2001-10-02 12:57:24 +0000795 return builtin_macro (pfile, node);
Zack Weinberg711b8822000-07-18 00:59:49 +0000796}
797
Neil Boothd15a58c2002-01-03 18:32:55 +0000798/* Replace the parameters in a function-like macro of NODE with the
799 actual ARGS, and place the result in a newly pushed token context.
800 Expand each argument before replacing, unless it is operated upon
801 by the # or ## operators. */
Neil Booth93c803682000-10-28 17:59:06 +0000802static void
Neil Boothe808ec92002-02-27 07:24:53 +0000803replace_args (pfile, node, macro, args)
Neil Booth93c803682000-10-28 17:59:06 +0000804 cpp_reader *pfile;
Neil Booth644edda2001-10-02 12:57:24 +0000805 cpp_hashnode *node;
Neil Boothe808ec92002-02-27 07:24:53 +0000806 cpp_macro *macro;
Neil Booth93c803682000-10-28 17:59:06 +0000807 macro_arg *args;
Neil Booth93c803682000-10-28 17:59:06 +0000808{
809 unsigned int i, total;
810 const cpp_token *src, *limit;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000811 const cpp_token **dest, **first;
Neil Booth93c803682000-10-28 17:59:06 +0000812 macro_arg *arg;
Neil Booth1e013d22001-09-26 21:44:35 +0000813 _cpp_buff *buff;
Neil Booth93c803682000-10-28 17:59:06 +0000814
Neil Booth93c803682000-10-28 17:59:06 +0000815 /* First, fully macro-expand arguments, calculating the number of
Neil Booth1e013d22001-09-26 21:44:35 +0000816 tokens in the final expansion as we go. The ordering of the if
817 statements below is subtle; we must handle stringification before
818 pasting. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000819 total = macro->count;
Neil Booth601328b2002-05-16 05:53:24 +0000820 limit = macro->exp.tokens + macro->count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000821
Neil Booth601328b2002-05-16 05:53:24 +0000822 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth93c803682000-10-28 17:59:06 +0000823 if (src->type == CPP_MACRO_ARG)
824 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000825 /* Leading and trailing padding tokens. */
826 total += 2;
827
Neil Booth93c803682000-10-28 17:59:06 +0000828 /* We have an argument. If it is not being stringified or
829 pasted it is macro-replaced before insertion. */
Neil Booth6c53ebf2000-11-06 18:43:32 +0000830 arg = &args[src->val.arg_no - 1];
Neil Booth4c2b6472000-11-11 13:19:01 +0000831
Neil Booth93c803682000-10-28 17:59:06 +0000832 if (src->flags & STRINGIFY_ARG)
833 {
834 if (!arg->stringified)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000835 arg->stringified = stringify_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000836 }
837 else if ((src->flags & PASTE_LEFT)
Neil Booth601328b2002-05-16 05:53:24 +0000838 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
Neil Booth93c803682000-10-28 17:59:06 +0000839 total += arg->count - 1;
840 else
841 {
842 if (!arg->expanded)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000843 expand_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000844 total += arg->expanded_count - 1;
845 }
846 }
847
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000848 /* Now allocate space for the expansion, copy the tokens and replace
849 the arguments. */
Neil Booth1e013d22001-09-26 21:44:35 +0000850 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
851 first = (const cpp_token **) buff->base;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000852 dest = first;
Neil Booth93c803682000-10-28 17:59:06 +0000853
Neil Booth601328b2002-05-16 05:53:24 +0000854 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000855 {
856 unsigned int count;
857 const cpp_token **from, **paste_flag;
Neil Booth93c803682000-10-28 17:59:06 +0000858
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000859 if (src->type != CPP_MACRO_ARG)
860 {
861 *dest++ = src;
862 continue;
863 }
864
865 paste_flag = 0;
866 arg = &args[src->val.arg_no - 1];
867 if (src->flags & STRINGIFY_ARG)
868 count = 1, from = &arg->stringified;
869 else if (src->flags & PASTE_LEFT)
870 count = arg->count, from = arg->first;
Neil Booth601328b2002-05-16 05:53:24 +0000871 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000872 {
Neil Booth93c803682000-10-28 17:59:06 +0000873 count = arg->count, from = arg->first;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000874 if (dest != first)
875 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000876 if (dest[-1]->type == CPP_COMMA
877 && macro->variadic
878 && src->val.arg_no == macro->paramc)
879 {
Neil Booth58551c22002-08-06 20:35:46 +0000880 /* Swallow a pasted comma if from == NULL, otherwise
881 drop the paste flag. */
882 if (from == NULL)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000883 dest--;
884 else
885 paste_flag = dest - 1;
886 }
887 /* Remove the paste flag if the RHS is a placemarker. */
888 else if (count == 0)
889 paste_flag = dest - 1;
890 }
891 }
892 else
893 count = arg->expanded_count, from = arg->expanded;
Neil Booth93c803682000-10-28 17:59:06 +0000894
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000895 /* Padding on the left of an argument (unless RHS of ##). */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000896 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
Neil Booth601328b2002-05-16 05:53:24 +0000897 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000898 *dest++ = padding_token (pfile, src);
Neil Booth93c803682000-10-28 17:59:06 +0000899
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000900 if (count)
901 {
902 memcpy (dest, from, count * sizeof (cpp_token *));
903 dest += count;
Neil Booth93c803682000-10-28 17:59:06 +0000904
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000905 /* With a non-empty argument on the LHS of ##, the last
906 token should be flagged PASTE_LEFT. */
907 if (src->flags & PASTE_LEFT)
908 paste_flag = dest - 1;
909 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000910
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000911 /* Avoid paste on RHS (even case count == 0). */
912 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
913 *dest++ = &pfile->avoid_paste;
Neil Booth26ec42e2001-01-28 11:22:23 +0000914
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000915 /* Add a new paste flag, or remove an unwanted one. */
916 if (paste_flag)
917 {
918 cpp_token *token = _cpp_temp_token (pfile);
919 token->type = (*paste_flag)->type;
920 token->val.str = (*paste_flag)->val.str;
921 if (src->flags & PASTE_LEFT)
922 token->flags = (*paste_flag)->flags | PASTE_LEFT;
923 else
924 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
925 *paste_flag = token;
926 }
927 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000928
Neil Booth93c803682000-10-28 17:59:06 +0000929 /* Free the expanded arguments. */
930 for (i = 0; i < macro->paramc; i++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000931 if (args[i].expanded)
932 free (args[i].expanded);
933
Neil Booth644edda2001-10-02 12:57:24 +0000934 push_ptoken_context (pfile, node, buff, first, dest - first);
Neil Booth93c803682000-10-28 17:59:06 +0000935}
936
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000937/* Return a special padding token, with padding inherited from SOURCE. */
938static const cpp_token *
939padding_token (pfile, source)
Neil Booth93c803682000-10-28 17:59:06 +0000940 cpp_reader *pfile;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000941 const cpp_token *source;
942{
943 cpp_token *result = _cpp_temp_token (pfile);
944
945 result->type = CPP_PADDING;
946 result->val.source = source;
947 result->flags = 0;
948 return result;
949}
950
Neil Boothd15a58c2002-01-03 18:32:55 +0000951/* Get a new uninitialized context. Create a new one if we cannot
952 re-use an old one. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000953static cpp_context *
954next_context (pfile)
955 cpp_reader *pfile;
956{
957 cpp_context *result = pfile->context->next;
958
959 if (result == 0)
960 {
961 result = xnew (cpp_context);
962 result->prev = pfile->context;
963 result->next = 0;
964 pfile->context->next = result;
965 }
966
967 pfile->context = result;
968 return result;
969}
970
971/* Push a list of pointers to tokens. */
972static void
Neil Booth1e013d22001-09-26 21:44:35 +0000973push_ptoken_context (pfile, macro, buff, first, count)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000974 cpp_reader *pfile;
Neil Booth644edda2001-10-02 12:57:24 +0000975 cpp_hashnode *macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000976 _cpp_buff *buff;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000977 const cpp_token **first;
978 unsigned int count;
Neil Booth93c803682000-10-28 17:59:06 +0000979{
980 cpp_context *context = next_context (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000981
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000982 context->direct_p = false;
983 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000984 context->buff = buff;
Neil Booth82eda772002-06-04 13:07:06 +0000985 FIRST (context).ptoken = first;
986 LAST (context).ptoken = first + count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000987}
988
989/* Push a list of tokens. */
990static void
991push_token_context (pfile, macro, first, count)
992 cpp_reader *pfile;
Neil Booth644edda2001-10-02 12:57:24 +0000993 cpp_hashnode *macro;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000994 const cpp_token *first;
995 unsigned int count;
996{
997 cpp_context *context = next_context (pfile);
998
999 context->direct_p = true;
1000 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +00001001 context->buff = NULL;
Neil Booth82eda772002-06-04 13:07:06 +00001002 FIRST (context).token = first;
1003 LAST (context).token = first + count;
Neil Booth93c803682000-10-28 17:59:06 +00001004}
1005
Neil Boothcbc69f82002-06-05 20:27:12 +00001006/* Push a traditional macro's replacement text. */
1007void
Neil Booth1ce676a2002-06-09 20:04:17 +00001008_cpp_push_text_context (pfile, macro, start, len)
Neil Boothcbc69f82002-06-05 20:27:12 +00001009 cpp_reader *pfile;
1010 cpp_hashnode *macro;
Neil Booth1ce676a2002-06-09 20:04:17 +00001011 const uchar *start;
1012 size_t len;
Neil Boothcbc69f82002-06-05 20:27:12 +00001013{
1014 cpp_context *context = next_context (pfile);
1015
1016 context->direct_p = true;
1017 context->macro = macro;
1018 context->buff = NULL;
1019 CUR (context) = start;
Neil Booth1ce676a2002-06-09 20:04:17 +00001020 RLIMIT (context) = start + len;
Neil Booth974c43f2002-06-13 06:25:28 +00001021 macro->flags |= NODE_DISABLED;
Neil Boothcbc69f82002-06-05 20:27:12 +00001022}
1023
Neil Boothd15a58c2002-01-03 18:32:55 +00001024/* Expand an argument ARG before replacing parameters in a
1025 function-like macro. This works by pushing a context with the
1026 argument's tokens, and then expanding that into a temporary buffer
1027 as if it were a normal part of the token stream. collect_args()
1028 has terminated the argument's tokens with a CPP_EOF so that we know
1029 when we have fully expanded the argument. */
Neil Booth93c803682000-10-28 17:59:06 +00001030static void
1031expand_arg (pfile, arg)
1032 cpp_reader *pfile;
1033 macro_arg *arg;
1034{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001035 unsigned int capacity;
Neil Booth56941bf2002-09-20 19:44:09 +00001036 bool saved_warn_trad;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001037
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001038 if (arg->count == 0)
1039 return;
Neil Booth93c803682000-10-28 17:59:06 +00001040
Neil Booth56941bf2002-09-20 19:44:09 +00001041 /* Don't warn about funlike macros when pre-expanding. */
1042 saved_warn_trad = CPP_WTRADITIONAL (pfile);
1043 CPP_WTRADITIONAL (pfile) = 0;
1044
Neil Booth93c803682000-10-28 17:59:06 +00001045 /* Loop, reading in the arguments. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001046 capacity = 256;
1047 arg->expanded = (const cpp_token **)
1048 xmalloc (capacity * sizeof (cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +00001049
Neil Booth1e013d22001-09-26 21:44:35 +00001050 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001051 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00001052 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001053 const cpp_token *token;
1054
1055 if (arg->expanded_count + 1 >= capacity)
Neil Booth93c803682000-10-28 17:59:06 +00001056 {
1057 capacity *= 2;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001058 arg->expanded = (const cpp_token **)
1059 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +00001060 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001061
1062 token = cpp_get_token (pfile);
1063
1064 if (token->type == CPP_EOF)
1065 break;
1066
1067 arg->expanded[arg->expanded_count++] = token;
Neil Booth93c803682000-10-28 17:59:06 +00001068 }
Neil Booth93c803682000-10-28 17:59:06 +00001069
Neil Booth1e013d22001-09-26 21:44:35 +00001070 _cpp_pop_context (pfile);
Neil Booth56941bf2002-09-20 19:44:09 +00001071
1072 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
Neil Booth93c803682000-10-28 17:59:06 +00001073}
1074
Neil Boothd15a58c2002-01-03 18:32:55 +00001075/* Pop the current context off the stack, re-enabling the macro if the
1076 context represented a macro's replacement list. The context
1077 structure is not freed so that we can re-use it later. */
Neil Booth93c803682000-10-28 17:59:06 +00001078void
1079_cpp_pop_context (pfile)
1080 cpp_reader *pfile;
1081{
Neil Booth1e013d22001-09-26 21:44:35 +00001082 cpp_context *context = pfile->context;
Neil Booth93c803682000-10-28 17:59:06 +00001083
Neil Booth1e013d22001-09-26 21:44:35 +00001084 if (context->macro)
Neil Booth644edda2001-10-02 12:57:24 +00001085 context->macro->flags &= ~NODE_DISABLED;
Neil Booth1e013d22001-09-26 21:44:35 +00001086
1087 if (context->buff)
1088 _cpp_release_buff (pfile, context->buff);
1089
1090 pfile->context = context->prev;
Neil Booth93c803682000-10-28 17:59:06 +00001091}
1092
Neil Booth7f2f1a62000-11-14 18:32:06 +00001093/* Eternal routine to get a token. Also used nearly everywhere
1094 internally, except for places where we know we can safely call
1095 the lexer directly, such as lexing a directive name.
1096
1097 Macro expansions and directives are transparently handled,
1098 including entering included files. Thus tokens are post-macro
1099 expansion, and after any intervening directives. External callers
1100 see CPP_EOF only at EOF. Internal callers also see it when meeting
1101 a directive inside a macro call, when at the end of a directive and
1102 state.in_directive is still 1, and at the end of argument
1103 pre-expansion. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001104const cpp_token *
1105cpp_get_token (pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001106 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001107{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001108 const cpp_token *result;
1109
Neil Booth29b10742000-11-13 18:40:37 +00001110 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00001111 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001112 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001113 cpp_context *context = pfile->context;
1114
Neil Booth93c803682000-10-28 17:59:06 +00001115 /* Context->prev == 0 <=> base context. */
Neil Boothbdcbe492001-09-13 20:05:17 +00001116 if (!context->prev)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001117 result = _cpp_lex_token (pfile);
Neil Booth82eda772002-06-04 13:07:06 +00001118 else if (FIRST (context).token != LAST (context).token)
Neil Booth29b10742000-11-13 18:40:37 +00001119 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001120 if (context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +00001121 result = FIRST (context).token++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001122 else
Neil Booth82eda772002-06-04 13:07:06 +00001123 result = *FIRST (context).ptoken++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001124
1125 if (result->flags & PASTE_LEFT)
Neil Boothec1a23e2001-01-31 07:48:54 +00001126 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001127 paste_all_tokens (pfile, result);
1128 if (pfile->state.in_directive)
1129 continue;
1130 return padding_token (pfile, result);
Neil Boothec1a23e2001-01-31 07:48:54 +00001131 }
Neil Booth29b10742000-11-13 18:40:37 +00001132 }
Neil Booth93c803682000-10-28 17:59:06 +00001133 else
1134 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001135 _cpp_pop_context (pfile);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001136 if (pfile->state.in_directive)
1137 continue;
1138 return &pfile->avoid_paste;
Neil Booth93c803682000-10-28 17:59:06 +00001139 }
Neil Booth93c803682000-10-28 17:59:06 +00001140
Jason Thorpe477cdac2002-04-07 03:12:23 +00001141 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1142 continue;
1143
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001144 if (result->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +00001145 break;
1146
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001147 node = result->val.node;
Neil Booth4c2b6472000-11-11 13:19:01 +00001148
Neil Booth644edda2001-10-02 12:57:24 +00001149 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1150 break;
Kazu Hiratadf383482002-05-22 22:02:16 +00001151
Neil Booth644edda2001-10-02 12:57:24 +00001152 if (!(node->flags & NODE_DISABLED))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001153 {
Neil Booth644edda2001-10-02 12:57:24 +00001154 if (!pfile->state.prevent_expansion
1155 && enter_macro_context (pfile, node))
Neil Boothbd969772001-02-01 19:13:53 +00001156 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001157 if (pfile->state.in_directive)
1158 continue;
1159 return padding_token (pfile, result);
Neil Boothbd969772001-02-01 19:13:53 +00001160 }
Neil Booth93c803682000-10-28 17:59:06 +00001161 }
Neil Booth644edda2001-10-02 12:57:24 +00001162 else
1163 {
Neil Boothd15a58c2002-01-03 18:32:55 +00001164 /* Flag this token as always unexpandable. FIXME: move this
1165 to collect_args()?. */
Neil Booth644edda2001-10-02 12:57:24 +00001166 cpp_token *t = _cpp_temp_token (pfile);
1167 t->type = result->type;
1168 t->flags = result->flags | NO_EXPAND;
1169 t->val.str = result->val.str;
1170 result = t;
1171 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001172
Neil Booth644edda2001-10-02 12:57:24 +00001173 break;
Neil Booth93c803682000-10-28 17:59:06 +00001174 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001175
1176 return result;
Neil Booth93c803682000-10-28 17:59:06 +00001177}
1178
Neil Booth7065e132001-02-14 07:38:20 +00001179/* Returns true if we're expanding an object-like macro that was
1180 defined in a system header. Just checks the macro at the top of
1181 the stack. Used for diagnostic suppression. */
1182int
Neil Boothc6911452001-03-03 11:32:32 +00001183cpp_sys_macro_p (pfile)
Neil Booth7065e132001-02-14 07:38:20 +00001184 cpp_reader *pfile;
1185{
Neil Booth644edda2001-10-02 12:57:24 +00001186 cpp_hashnode *node = pfile->context->macro;
Neil Booth7065e132001-02-14 07:38:20 +00001187
Neil Booth644edda2001-10-02 12:57:24 +00001188 return node && node->value.macro && node->value.macro->syshdr;
Neil Booth7065e132001-02-14 07:38:20 +00001189}
1190
Neil Boothaf0d16c2002-04-22 17:48:02 +00001191/* Read each token in, until end of the current file. Directives are
1192 transparently processed. */
Neil Booth93c803682000-10-28 17:59:06 +00001193void
Neil Boothef6e9582001-08-04 12:01:59 +00001194cpp_scan_nooutput (pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001195 cpp_reader *pfile;
1196{
Neil Boothaf0d16c2002-04-22 17:48:02 +00001197 /* Request a CPP_EOF token at the end of this file, rather than
1198 transparently continuing with the including file. */
1199 pfile->buffer->return_at_eof = true;
1200
Neil Booth590e1982002-07-01 12:47:54 +00001201 if (CPP_OPTION (pfile, traditional))
1202 while (_cpp_read_logical_line_trad (pfile))
1203 ;
1204 else
1205 while (cpp_get_token (pfile)->type != CPP_EOF)
1206 ;
Neil Booth93c803682000-10-28 17:59:06 +00001207}
1208
Neil Boothbdcbe492001-09-13 20:05:17 +00001209/* Step back one (or more) tokens. Can only step mack more than 1 if
1210 they are from the lexer, and not from macro expansion. */
Neil Boothb528a072000-11-12 11:46:21 +00001211void
Neil Boothbdcbe492001-09-13 20:05:17 +00001212_cpp_backup_tokens (pfile, count)
Neil Booth93c803682000-10-28 17:59:06 +00001213 cpp_reader *pfile;
Neil Boothbdcbe492001-09-13 20:05:17 +00001214 unsigned int count;
Neil Booth93c803682000-10-28 17:59:06 +00001215{
Neil Boothbdcbe492001-09-13 20:05:17 +00001216 if (pfile->context->prev == NULL)
1217 {
1218 pfile->lookaheads += count;
1219 while (count--)
1220 {
Neil Booth3293c3e2001-11-19 21:04:49 +00001221 pfile->cur_token--;
1222 if (pfile->cur_token == pfile->cur_run->base
1223 /* Possible with -fpreprocessed and no leading #line. */
1224 && pfile->cur_run->prev != NULL)
Neil Boothbdcbe492001-09-13 20:05:17 +00001225 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001226 pfile->cur_run = pfile->cur_run->prev;
1227 pfile->cur_token = pfile->cur_run->limit;
1228 }
1229 }
1230 }
Neil Booth93c803682000-10-28 17:59:06 +00001231 else
1232 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001233 if (count != 1)
1234 abort ();
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001235 if (pfile->context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +00001236 FIRST (pfile->context).token--;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001237 else
Neil Booth82eda772002-06-04 13:07:06 +00001238 FIRST (pfile->context).ptoken--;
Neil Booth93c803682000-10-28 17:59:06 +00001239 }
Neil Booth93c803682000-10-28 17:59:06 +00001240}
1241
1242/* #define directive parsing and handling. */
1243
Kazu Hiratada7d8302002-09-22 02:03:17 +00001244/* Returns nonzero if a macro redefinition warning is required. */
Neil Boothcbc69f82002-06-05 20:27:12 +00001245static bool
1246warn_of_redefinition (pfile, node, macro2)
1247 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001248 const cpp_hashnode *node;
1249 const cpp_macro *macro2;
1250{
1251 const cpp_macro *macro1;
1252 unsigned int i;
1253
Neil Booth618cdda2001-02-25 09:43:03 +00001254 /* Some redefinitions need to be warned about regardless. */
1255 if (node->flags & NODE_WARN)
Neil Boothcbc69f82002-06-05 20:27:12 +00001256 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001257
Neil Booth618cdda2001-02-25 09:43:03 +00001258 /* Redefinition of a macro is allowed if and only if the old and new
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001259 definitions are the same. (6.10.3 paragraph 2). */
Neil Booth93c803682000-10-28 17:59:06 +00001260 macro1 = node->value.macro;
1261
Neil Booth6618c5d2002-06-10 06:03:13 +00001262 /* Don't check count here as it can be different in valid
1263 traditional redefinitions with just whitespace differences. */
1264 if (macro1->paramc != macro2->paramc
Neil Booth93c803682000-10-28 17:59:06 +00001265 || macro1->fun_like != macro2->fun_like
Neil Booth28e0f042000-12-09 12:06:37 +00001266 || macro1->variadic != macro2->variadic)
Neil Boothcbc69f82002-06-05 20:27:12 +00001267 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001268
1269 /* Check parameter spellings. */
1270 for (i = 0; i < macro1->paramc; i++)
1271 if (macro1->params[i] != macro2->params[i])
Neil Boothcbc69f82002-06-05 20:27:12 +00001272 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001273
Neil Boothcbc69f82002-06-05 20:27:12 +00001274 /* Check the replacement text or tokens. */
1275 if (CPP_OPTION (pfile, traditional))
Neil Booth6618c5d2002-06-10 06:03:13 +00001276 return _cpp_expansions_different_trad (macro1, macro2);
Neil Boothcbc69f82002-06-05 20:27:12 +00001277
Neil Booth6618c5d2002-06-10 06:03:13 +00001278 if (macro1->count == macro2->count)
1279 for (i = 0; i < macro1->count; i++)
1280 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1281 return true;
Neil Boothcbc69f82002-06-05 20:27:12 +00001282
1283 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001284}
1285
Neil Booth93c803682000-10-28 17:59:06 +00001286/* Free the definition of hashnode H. */
Neil Booth93c803682000-10-28 17:59:06 +00001287void
1288_cpp_free_definition (h)
1289 cpp_hashnode *h;
Zack Weinberg711b8822000-07-18 00:59:49 +00001290{
Neil Booth93c803682000-10-28 17:59:06 +00001291 /* Macros and assertions no longer have anything to free. */
1292 h->type = NT_VOID;
1293 /* Clear builtin flag in case of redefinition. */
Neil Booth644edda2001-10-02 12:57:24 +00001294 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
Neil Booth93c803682000-10-28 17:59:06 +00001295}
Zack Weinberg711b8822000-07-18 00:59:49 +00001296
Neil Booth14baae02001-09-17 18:26:12 +00001297/* Save parameter NODE to the parameter list of macro MACRO. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001298 zero on success, nonzero if the parameter is a duplicate. */
Neil Boothc70f6ed2002-06-07 06:26:32 +00001299bool
1300_cpp_save_parameter (pfile, macro, node)
Neil Booth93c803682000-10-28 17:59:06 +00001301 cpp_reader *pfile;
1302 cpp_macro *macro;
1303 cpp_hashnode *node;
1304{
Zack Weinberg4977bab2002-12-16 18:23:00 +00001305 unsigned int len;
Neil Booth93c803682000-10-28 17:59:06 +00001306 /* Constraint 6.10.3.6 - duplicate parameter names. */
Zack Weinberg4977bab2002-12-16 18:23:00 +00001307 if (node->flags & NODE_MACRO_ARG)
Zack Weinberg711b8822000-07-18 00:59:49 +00001308 {
Neil Boothebef4e82002-04-14 18:42:47 +00001309 cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"",
1310 NODE_NAME (node));
Neil Booth23ff0222002-07-17 17:27:14 +00001311 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001312 }
1313
Neil Booth8c3b2692001-09-30 10:03:11 +00001314 if (BUFF_ROOM (pfile->a_buff)
1315 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1316 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +00001317
Neil Booth8c3b2692001-09-30 10:03:11 +00001318 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
Zack Weinberg4977bab2002-12-16 18:23:00 +00001319 node->flags |= NODE_MACRO_ARG;
1320 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1321 if (len > pfile->macro_buffer_len)
1322 {
1323 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
1324 pfile->macro_buffer_len = len;
1325 }
1326 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1327 = node->value;
1328
1329 node->value.arg_index = macro->paramc;
Neil Booth23ff0222002-07-17 17:27:14 +00001330 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001331}
1332
Neil Booth23ff0222002-07-17 17:27:14 +00001333/* Check the syntax of the parameters in a MACRO definition. Returns
1334 false if an error occurs. */
1335static bool
Neil Booth93c803682000-10-28 17:59:06 +00001336parse_params (pfile, macro)
1337 cpp_reader *pfile;
1338 cpp_macro *macro;
1339{
Neil Booth93c803682000-10-28 17:59:06 +00001340 unsigned int prev_ident = 0;
1341
Neil Booth93c803682000-10-28 17:59:06 +00001342 for (;;)
1343 {
Neil Booth345894b2001-09-16 13:44:29 +00001344 const cpp_token *token = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001345
Neil Booth345894b2001-09-16 13:44:29 +00001346 switch (token->type)
Zack Weinberg711b8822000-07-18 00:59:49 +00001347 {
1348 default:
Jason Thorpe477cdac2002-04-07 03:12:23 +00001349 /* Allow/ignore comments in parameter lists if we are
1350 preserving comments in macro expansions. */
1351 if (token->type == CPP_COMMENT
1352 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1353 continue;
1354
Neil Boothebef4e82002-04-14 18:42:47 +00001355 cpp_error (pfile, DL_ERROR,
1356 "\"%s\" may not appear in macro parameter list",
Neil Booth345894b2001-09-16 13:44:29 +00001357 cpp_token_as_text (pfile, token));
Neil Booth23ff0222002-07-17 17:27:14 +00001358 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001359
Zack Weinberg711b8822000-07-18 00:59:49 +00001360 case CPP_NAME:
1361 if (prev_ident)
1362 {
Neil Boothebef4e82002-04-14 18:42:47 +00001363 cpp_error (pfile, DL_ERROR,
1364 "macro parameters must be comma-separated");
Neil Booth23ff0222002-07-17 17:27:14 +00001365 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001366 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001367 prev_ident = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001368
Neil Boothc70f6ed2002-06-07 06:26:32 +00001369 if (_cpp_save_parameter (pfile, macro, token->val.node))
Neil Booth23ff0222002-07-17 17:27:14 +00001370 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001371 continue;
1372
1373 case CPP_CLOSE_PAREN:
Neil Booth93c803682000-10-28 17:59:06 +00001374 if (prev_ident || macro->paramc == 0)
Neil Booth23ff0222002-07-17 17:27:14 +00001375 return true;
Zack Weinberg711b8822000-07-18 00:59:49 +00001376
1377 /* Fall through to pick up the error. */
1378 case CPP_COMMA:
1379 if (!prev_ident)
1380 {
Neil Boothebef4e82002-04-14 18:42:47 +00001381 cpp_error (pfile, DL_ERROR, "parameter name missing");
Neil Booth23ff0222002-07-17 17:27:14 +00001382 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001383 }
1384 prev_ident = 0;
1385 continue;
1386
1387 case CPP_ELLIPSIS:
Neil Booth28e0f042000-12-09 12:06:37 +00001388 macro->variadic = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001389 if (!prev_ident)
1390 {
Neil Boothc70f6ed2002-06-07 06:26:32 +00001391 _cpp_save_parameter (pfile, macro,
1392 pfile->spec_nodes.n__VA_ARGS__);
Neil Booth93c803682000-10-28 17:59:06 +00001393 pfile->state.va_args_ok = 1;
1394 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
Neil Boothebef4e82002-04-14 18:42:47 +00001395 cpp_error (pfile, DL_PEDWARN,
1396 "anonymous variadic macros were introduced in C99");
Zack Weinberg711b8822000-07-18 00:59:49 +00001397 }
Neil Booth93c803682000-10-28 17:59:06 +00001398 else if (CPP_OPTION (pfile, pedantic))
Neil Boothebef4e82002-04-14 18:42:47 +00001399 cpp_error (pfile, DL_PEDWARN,
1400 "ISO C does not permit named variadic macros");
Zack Weinberg711b8822000-07-18 00:59:49 +00001401
Neil Booth93c803682000-10-28 17:59:06 +00001402 /* We're at the end, and just expect a closing parenthesis. */
Neil Booth345894b2001-09-16 13:44:29 +00001403 token = _cpp_lex_token (pfile);
1404 if (token->type == CPP_CLOSE_PAREN)
Neil Booth23ff0222002-07-17 17:27:14 +00001405 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001406 /* Fall through. */
1407
1408 case CPP_EOF:
Neil Boothebef4e82002-04-14 18:42:47 +00001409 cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list");
Neil Booth23ff0222002-07-17 17:27:14 +00001410 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001411 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001412 }
1413}
1414
Neil Booth14baae02001-09-17 18:26:12 +00001415/* Allocate room for a token from a macro's replacement list. */
Neil Booth93c803682000-10-28 17:59:06 +00001416static cpp_token *
Neil Booth14baae02001-09-17 18:26:12 +00001417alloc_expansion_token (pfile, macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001418 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001419 cpp_macro *macro;
Zack Weinberg711b8822000-07-18 00:59:49 +00001420{
Neil Booth8c3b2692001-09-30 10:03:11 +00001421 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1422 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
Zack Weinberg711b8822000-07-18 00:59:49 +00001423
Neil Booth8c3b2692001-09-30 10:03:11 +00001424 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
Neil Booth14baae02001-09-17 18:26:12 +00001425}
1426
Neil Boothd15a58c2002-01-03 18:32:55 +00001427/* Lex a token from the expansion of MACRO, but mark parameters as we
1428 find them and warn of traditional stringification. */
Neil Booth14baae02001-09-17 18:26:12 +00001429static cpp_token *
1430lex_expansion_token (pfile, macro)
1431 cpp_reader *pfile;
1432 cpp_macro *macro;
1433{
1434 cpp_token *token;
1435
1436 pfile->cur_token = alloc_expansion_token (pfile, macro);
1437 token = _cpp_lex_direct (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001438
Neil Boothd15a58c2002-01-03 18:32:55 +00001439 /* Is this a parameter? */
Zack Weinberg4977bab2002-12-16 18:23:00 +00001440 if (token->type == CPP_NAME
1441 && (token->val.node->flags & NODE_MACRO_ARG) != 0)
Zack Weinberg711b8822000-07-18 00:59:49 +00001442 {
Neil Booth93c803682000-10-28 17:59:06 +00001443 token->type = CPP_MACRO_ARG;
Zack Weinberg4977bab2002-12-16 18:23:00 +00001444 token->val.arg_no = token->val.node->value.arg_index;
Zack Weinberg711b8822000-07-18 00:59:49 +00001445 }
Neil Booth93c803682000-10-28 17:59:06 +00001446 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1447 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1448 check_trad_stringification (pfile, macro, &token->val.str);
Zack Weinberg711b8822000-07-18 00:59:49 +00001449
Neil Booth93c803682000-10-28 17:59:06 +00001450 return token;
Zack Weinberg711b8822000-07-18 00:59:49 +00001451}
1452
Neil Boothcbc69f82002-06-05 20:27:12 +00001453static bool
1454create_iso_definition (pfile, macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001455 cpp_reader *pfile;
Neil Boothcbc69f82002-06-05 20:27:12 +00001456 cpp_macro *macro;
Zack Weinberg711b8822000-07-18 00:59:49 +00001457{
Neil Boothcbc69f82002-06-05 20:27:12 +00001458 cpp_token *token;
Neil Booth14baae02001-09-17 18:26:12 +00001459 const cpp_token *ctoken;
Zack Weinberg711b8822000-07-18 00:59:49 +00001460
Neil Booth93c803682000-10-28 17:59:06 +00001461 /* Get the first token of the expansion (or the '(' of a
1462 function-like macro). */
Neil Booth14baae02001-09-17 18:26:12 +00001463 ctoken = _cpp_lex_token (pfile);
1464
1465 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
Zack Weinberg711b8822000-07-18 00:59:49 +00001466 {
Neil Boothcbc69f82002-06-05 20:27:12 +00001467 bool ok = parse_params (pfile, macro);
Neil Booth8c3b2692001-09-30 10:03:11 +00001468 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1469 if (!ok)
Neil Boothcbc69f82002-06-05 20:27:12 +00001470 return false;
Neil Booth8c3b2692001-09-30 10:03:11 +00001471
1472 /* Success. Commit the parameter array. */
Neil Booth562a5c22002-04-21 18:46:42 +00001473 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
Neil Booth93c803682000-10-28 17:59:06 +00001474 macro->fun_like = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001475 }
Neil Booth14baae02001-09-17 18:26:12 +00001476 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
Neil Boothebef4e82002-04-14 18:42:47 +00001477 cpp_error (pfile, DL_PEDWARN,
1478 "ISO C requires whitespace after the macro name");
Neil Booth93c803682000-10-28 17:59:06 +00001479
Neil Booth14baae02001-09-17 18:26:12 +00001480 if (macro->fun_like)
1481 token = lex_expansion_token (pfile, macro);
1482 else
1483 {
1484 token = alloc_expansion_token (pfile, macro);
1485 *token = *ctoken;
1486 }
Neil Booth93c803682000-10-28 17:59:06 +00001487
1488 for (;;)
1489 {
1490 /* Check the stringifying # constraint 6.10.3.2.1 of
1491 function-like macros when lexing the subsequent token. */
1492 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
Zack Weinberg711b8822000-07-18 00:59:49 +00001493 {
Neil Booth93c803682000-10-28 17:59:06 +00001494 if (token->type == CPP_MACRO_ARG)
1495 {
1496 token->flags &= ~PREV_WHITE;
1497 token->flags |= STRINGIFY_ARG;
1498 token->flags |= token[-1].flags & PREV_WHITE;
1499 token[-1] = token[0];
1500 macro->count--;
1501 }
1502 /* Let assembler get away with murder. */
Neil Boothbdb05a72000-11-26 17:31:13 +00001503 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +00001504 {
Neil Boothebef4e82002-04-14 18:42:47 +00001505 cpp_error (pfile, DL_ERROR,
1506 "'#' is not followed by a macro parameter");
Neil Boothcbc69f82002-06-05 20:27:12 +00001507 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001508 }
1509 }
1510
1511 if (token->type == CPP_EOF)
1512 break;
1513
1514 /* Paste operator constraint 6.10.3.3.1. */
1515 if (token->type == CPP_PASTE)
1516 {
1517 /* Token-paste ##, can appear in both object-like and
1518 function-like macros, but not at the ends. */
1519 if (--macro->count > 0)
1520 token = lex_expansion_token (pfile, macro);
1521
1522 if (macro->count == 0 || token->type == CPP_EOF)
1523 {
Neil Boothebef4e82002-04-14 18:42:47 +00001524 cpp_error (pfile, DL_ERROR,
Neil Booth93c803682000-10-28 17:59:06 +00001525 "'##' cannot appear at either end of a macro expansion");
Neil Boothcbc69f82002-06-05 20:27:12 +00001526 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001527 }
1528
1529 token[-1].flags |= PASTE_LEFT;
Neil Booth93c803682000-10-28 17:59:06 +00001530 }
1531
1532 token = lex_expansion_token (pfile, macro);
1533 }
1534
Neil Booth601328b2002-05-16 05:53:24 +00001535 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +00001536
Neil Booth4c2b6472000-11-11 13:19:01 +00001537 /* Don't count the CPP_EOF. */
1538 macro->count--;
Neil Booth93c803682000-10-28 17:59:06 +00001539
Neil Boothd15a58c2002-01-03 18:32:55 +00001540 /* Clear whitespace on first token for warn_of_redefinition(). */
Neil Booth8c3b2692001-09-30 10:03:11 +00001541 if (macro->count)
Neil Booth601328b2002-05-16 05:53:24 +00001542 macro->exp.tokens[0].flags &= ~PREV_WHITE;
Neil Booth8c3b2692001-09-30 10:03:11 +00001543
1544 /* Commit the memory. */
Neil Booth601328b2002-05-16 05:53:24 +00001545 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
Neil Booth8c3b2692001-09-30 10:03:11 +00001546
Neil Boothcbc69f82002-06-05 20:27:12 +00001547 return true;
1548}
Neil Booth44ed91a2000-10-29 11:37:18 +00001549
Kazu Hiratada7d8302002-09-22 02:03:17 +00001550/* Parse a macro and save its expansion. Returns nonzero on success. */
Neil Boothcbc69f82002-06-05 20:27:12 +00001551bool
1552_cpp_create_definition (pfile, node)
1553 cpp_reader *pfile;
1554 cpp_hashnode *node;
1555{
1556 cpp_macro *macro;
1557 unsigned int i;
1558 bool ok;
1559
1560 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1561 macro->line = pfile->directive_line;
1562 macro->params = 0;
1563 macro->paramc = 0;
1564 macro->variadic = 0;
Neil Booth23345bb2003-03-14 21:47:50 +00001565 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
Neil Boothcbc69f82002-06-05 20:27:12 +00001566 macro->count = 0;
1567 macro->fun_like = 0;
Neil Booth7065e132001-02-14 07:38:20 +00001568 /* To suppress some diagnostics. */
Neil Booth47d89cf2001-08-11 07:33:39 +00001569 macro->syshdr = pfile->map->sysp != 0;
Neil Booth7065e132001-02-14 07:38:20 +00001570
Neil Boothcbc69f82002-06-05 20:27:12 +00001571 if (CPP_OPTION (pfile, traditional))
1572 ok = _cpp_create_trad_definition (pfile, macro);
1573 else
1574 {
1575 cpp_token *saved_cur_token = pfile->cur_token;
1576
1577 ok = create_iso_definition (pfile, macro);
1578
1579 /* Restore lexer position because of games lex_expansion_token()
1580 plays lexing the macro. We set the type for SEEN_EOL() in
1581 cpplib.c.
1582
1583 Longer term we should lex the whole line before coming here,
1584 and just copy the expansion. */
1585 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1586 pfile->cur_token = saved_cur_token;
1587
1588 /* Stop the lexer accepting __VA_ARGS__. */
1589 pfile->state.va_args_ok = 0;
1590 }
1591
1592 /* Clear the fast argument lookup indices. */
1593 for (i = macro->paramc; i-- > 0; )
Zack Weinberg4977bab2002-12-16 18:23:00 +00001594 {
1595 struct cpp_hashnode *node = macro->params[i];
1596 node->flags &= ~ NODE_MACRO_ARG;
1597 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1598 }
Neil Boothcbc69f82002-06-05 20:27:12 +00001599
1600 if (!ok)
1601 return ok;
1602
Neil Boothc2734e02002-07-26 16:29:31 +00001603 if (node->type == NT_MACRO)
Neil Booth93c803682000-10-28 17:59:06 +00001604 {
Neil Bootha69cbaa2002-07-23 22:57:49 +00001605 if (CPP_OPTION (pfile, warn_unused_macros))
1606 _cpp_warn_if_unused_macro (pfile, node, NULL);
1607
Neil Boothcbc69f82002-06-05 20:27:12 +00001608 if (warn_of_redefinition (pfile, node, macro))
Neil Booth93c803682000-10-28 17:59:06 +00001609 {
Neil Boothebef4e82002-04-14 18:42:47 +00001610 cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0,
1611 "\"%s\" redefined", NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +00001612
Neil Booth618cdda2001-02-25 09:43:03 +00001613 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
Neil Boothcbc69f82002-06-05 20:27:12 +00001614 cpp_error_with_line (pfile, DL_PEDWARN,
1615 node->value.macro->line, 0,
1616 "this is the location of the previous definition");
Zack Weinberg711b8822000-07-18 00:59:49 +00001617 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001618 }
1619
Neil Boothc2734e02002-07-26 16:29:31 +00001620 if (node->type != NT_VOID)
1621 _cpp_free_definition (node);
1622
Zack Weinberg711b8822000-07-18 00:59:49 +00001623 /* Enter definition in hash table. */
Neil Booth93c803682000-10-28 17:59:06 +00001624 node->type = NT_MACRO;
1625 node->value.macro = macro;
Neil Bootha28c50352001-05-16 22:02:09 +00001626 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
Neil Booth618cdda2001-02-25 09:43:03 +00001627 node->flags |= NODE_WARN;
Zack Weinberg711b8822000-07-18 00:59:49 +00001628
Neil Booth93c803682000-10-28 17:59:06 +00001629 return ok;
Zack Weinberg711b8822000-07-18 00:59:49 +00001630}
1631
Neil Boothd15a58c2002-01-03 18:32:55 +00001632/* Warn if a token in STRING matches one of a function-like MACRO's
1633 parameters. */
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001634static void
Neil Booth93c803682000-10-28 17:59:06 +00001635check_trad_stringification (pfile, macro, string)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001636 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +00001637 const cpp_macro *macro;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001638 const cpp_string *string;
1639{
Neil Booth93c803682000-10-28 17:59:06 +00001640 unsigned int i, len;
Neil Booth6338b352003-04-23 22:44:06 +00001641 const uchar *p, *q, *limit;
Kazu Hiratadf383482002-05-22 22:02:16 +00001642
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001643 /* Loop over the string. */
Neil Booth6338b352003-04-23 22:44:06 +00001644 limit = string->text + string->len - 1;
1645 for (p = string->text + 1; p < limit; p = q)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001646 {
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001647 /* Find the start of an identifier. */
Greg McGary61c16b12000-09-15 21:25:02 +00001648 while (p < limit && !is_idstart (*p))
1649 p++;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001650
1651 /* Find the end of the identifier. */
1652 q = p;
Greg McGary61c16b12000-09-15 21:25:02 +00001653 while (q < limit && is_idchar (*q))
1654 q++;
Neil Booth93c803682000-10-28 17:59:06 +00001655
1656 len = q - p;
1657
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001658 /* Loop over the function macro arguments to see if the
1659 identifier inside the string matches one of them. */
Neil Booth93c803682000-10-28 17:59:06 +00001660 for (i = 0; i < macro->paramc; i++)
1661 {
1662 const cpp_hashnode *node = macro->params[i];
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001663
Neil Booth2a967f32001-05-20 06:26:45 +00001664 if (NODE_LEN (node) == len
1665 && !memcmp (p, NODE_NAME (node), len))
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001666 {
Neil Boothebef4e82002-04-14 18:42:47 +00001667 cpp_error (pfile, DL_WARNING,
Zack Weinbergf458d1d2002-02-27 18:48:07 +00001668 "macro argument \"%s\" would be stringified in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +00001669 NODE_NAME (node));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001670 break;
1671 }
1672 }
1673 }
1674}
Neil Booth93c803682000-10-28 17:59:06 +00001675
Neil Booth70961712001-06-23 11:34:41 +00001676/* Returns the name, arguments and expansion of a macro, in a format
1677 suitable to be read back in again, and therefore also for DWARF 2
1678 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1679 Caller is expected to generate the "#define" bit if needed. The
Neil Booth93c803682000-10-28 17:59:06 +00001680 returned text is temporary, and automatically freed later. */
Neil Booth93c803682000-10-28 17:59:06 +00001681const unsigned char *
1682cpp_macro_definition (pfile, node)
1683 cpp_reader *pfile;
1684 const cpp_hashnode *node;
1685{
1686 unsigned int i, len;
1687 const cpp_macro *macro = node->value.macro;
1688 unsigned char *buffer;
1689
1690 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1691 {
Neil Boothebef4e82002-04-14 18:42:47 +00001692 cpp_error (pfile, DL_ICE,
1693 "invalid hash type %d in cpp_macro_definition", node->type);
Neil Booth93c803682000-10-28 17:59:06 +00001694 return 0;
1695 }
1696
1697 /* Calculate length. */
Neil Booth8dc901d2002-05-29 19:30:07 +00001698 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
Neil Booth93c803682000-10-28 17:59:06 +00001699 if (macro->fun_like)
1700 {
Jim Blandy64d08262002-04-05 00:12:40 +00001701 len += 4; /* "()" plus possible final ".." of named
1702 varargs (we have + 1 below). */
Neil Booth93c803682000-10-28 17:59:06 +00001703 for (i = 0; i < macro->paramc; i++)
Jim Blandy64d08262002-04-05 00:12:40 +00001704 len += NODE_LEN (macro->params[i]) + 1; /* "," */
Neil Booth93c803682000-10-28 17:59:06 +00001705 }
1706
Neil Booth278c4662002-06-19 05:40:08 +00001707 if (CPP_OPTION (pfile, traditional))
1708 len += _cpp_replacement_text_len (macro);
1709 else
Neil Booth93c803682000-10-28 17:59:06 +00001710 {
Neil Booth278c4662002-06-19 05:40:08 +00001711 for (i = 0; i < macro->count; i++)
1712 {
1713 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00001714
Neil Booth278c4662002-06-19 05:40:08 +00001715 if (token->type == CPP_MACRO_ARG)
1716 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1717 else
1718 len += cpp_token_len (token); /* Includes room for ' '. */
1719 if (token->flags & STRINGIFY_ARG)
1720 len++; /* "#" */
1721 if (token->flags & PASTE_LEFT)
1722 len += 3; /* " ##" */
1723 }
Neil Booth93c803682000-10-28 17:59:06 +00001724 }
1725
1726 if (len > pfile->macro_buffer_len)
Alexandre Oliva4b49c362001-01-09 09:30:43 +00001727 {
Neil Booth562a5c22002-04-21 18:46:42 +00001728 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
Alexandre Oliva4b49c362001-01-09 09:30:43 +00001729 pfile->macro_buffer_len = len;
1730 }
Neil Booth70961712001-06-23 11:34:41 +00001731
1732 /* Fill in the buffer. Start with the macro name. */
Neil Booth93c803682000-10-28 17:59:06 +00001733 buffer = pfile->macro_buffer;
Neil Booth70961712001-06-23 11:34:41 +00001734 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1735 buffer += NODE_LEN (node);
Neil Booth93c803682000-10-28 17:59:06 +00001736
1737 /* Parameter names. */
1738 if (macro->fun_like)
1739 {
1740 *buffer++ = '(';
1741 for (i = 0; i < macro->paramc; i++)
1742 {
1743 cpp_hashnode *param = macro->params[i];
1744
1745 if (param != pfile->spec_nodes.n__VA_ARGS__)
1746 {
Neil Bootha28c50352001-05-16 22:02:09 +00001747 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1748 buffer += NODE_LEN (param);
Neil Booth93c803682000-10-28 17:59:06 +00001749 }
1750
1751 if (i + 1 < macro->paramc)
Kazu Hiratadf383482002-05-22 22:02:16 +00001752 /* Don't emit a space after the comma here; we're trying
1753 to emit a Dwarf-friendly definition, and the Dwarf spec
1754 forbids spaces in the argument list. */
Jim Blandy64d08262002-04-05 00:12:40 +00001755 *buffer++ = ',';
Neil Booth28e0f042000-12-09 12:06:37 +00001756 else if (macro->variadic)
Neil Booth93c803682000-10-28 17:59:06 +00001757 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1758 }
1759 *buffer++ = ')';
1760 }
1761
Jim Blandye37b38d2002-03-19 21:43:39 +00001762 /* The Dwarf spec requires a space after the macro name, even if the
1763 definition is the empty string. */
1764 *buffer++ = ' ';
1765
Neil Booth278c4662002-06-19 05:40:08 +00001766 if (CPP_OPTION (pfile, traditional))
1767 buffer = _cpp_copy_replacement_text (macro, buffer);
1768 else if (macro->count)
Neil Booth93c803682000-10-28 17:59:06 +00001769 /* Expansion tokens. */
Neil Booth93c803682000-10-28 17:59:06 +00001770 {
Neil Booth93c803682000-10-28 17:59:06 +00001771 for (i = 0; i < macro->count; i++)
1772 {
Neil Booth601328b2002-05-16 05:53:24 +00001773 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00001774
1775 if (token->flags & PREV_WHITE)
1776 *buffer++ = ' ';
1777 if (token->flags & STRINGIFY_ARG)
1778 *buffer++ = '#';
1779
1780 if (token->type == CPP_MACRO_ARG)
1781 {
Neil Bootha28c50352001-05-16 22:02:09 +00001782 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1783 memcpy (buffer,
1784 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
Neil Booth93c803682000-10-28 17:59:06 +00001785 buffer += len;
1786 }
1787 else
1788 buffer = cpp_spell_token (pfile, token, buffer);
1789
1790 if (token->flags & PASTE_LEFT)
1791 {
1792 *buffer++ = ' ';
1793 *buffer++ = '#';
1794 *buffer++ = '#';
1795 /* Next has PREV_WHITE; see _cpp_create_definition. */
1796 }
1797 }
1798 }
1799
1800 *buffer = '\0';
1801 return pfile->macro_buffer;
1802}