blob: 065c3972016656fbebd8a481b145350486e11abc [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,
Kazu Hiratad9221e012004-01-21 20:40:04 +00003 1999, 2000, 2001, 2002, 2003, 2004 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 +000031typedef struct macro_arg macro_arg;
32struct macro_arg
33{
Neil Booth4ed5bcf2001-09-24 22:53:12 +000034 const cpp_token **first; /* First token in unexpanded argument. */
Kazu Hirata6d2f8882001-10-10 11:33:39 +000035 const cpp_token **expanded; /* Macro-expanded argument. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +000036 const cpp_token *stringified; /* Stringified argument. */
Neil Booth93c803682000-10-28 17:59:06 +000037 unsigned int count; /* # of tokens in argument. */
38 unsigned int expanded_count; /* # of tokens in expanded argument. */
39};
Zack Weinberg711b8822000-07-18 00:59:49 +000040
Neil Booth93c803682000-10-28 17:59:06 +000041/* Macro expansion. */
42
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000043static int enter_macro_context (cpp_reader *, cpp_hashnode *);
44static int builtin_macro (cpp_reader *, cpp_hashnode *);
45static void push_token_context (cpp_reader *, cpp_hashnode *,
46 const cpp_token *, unsigned int);
47static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
48 const cpp_token **, unsigned int);
49static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *);
50static cpp_context *next_context (cpp_reader *);
51static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
52static void expand_arg (cpp_reader *, macro_arg *);
53static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
54static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
55static void paste_all_tokens (cpp_reader *, const cpp_token *);
56static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
57static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
58 macro_arg *);
59static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *);
60static bool create_iso_definition (cpp_reader *, cpp_macro *);
Neil Booth93c803682000-10-28 17:59:06 +000061
Neil Booth93c803682000-10-28 17:59:06 +000062/* #define directive parsing and handling. */
63
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000064static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
65static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
66static bool warn_of_redefinition (cpp_reader *, const cpp_hashnode *,
67 const cpp_macro *);
68static bool parse_params (cpp_reader *, cpp_macro *);
69static void check_trad_stringification (cpp_reader *, const cpp_macro *,
70 const cpp_string *);
Zack Weinberg711b8822000-07-18 00:59:49 +000071
Neil Bootha69cbaa2002-07-23 22:57:49 +000072/* Emits a warning if NODE is a macro defined in the main file that
73 has not been used. */
74int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000075_cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
76 void *v ATTRIBUTE_UNUSED)
Neil Bootha69cbaa2002-07-23 22:57:49 +000077{
78 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
79 {
80 cpp_macro *macro = node->value.macro;
81
82 if (!macro->used
Per Bothner50f59cd2004-01-19 21:30:18 -080083 && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
John David Anglin0527bc42003-11-01 22:56:54 +000084 cpp_error_with_line (pfile, CPP_DL_WARNING, macro->line, 0,
Neil Bootha69cbaa2002-07-23 22:57:49 +000085 "macro \"%s\" is not used", NODE_NAME (node));
86 }
87
88 return 1;
89}
90
Neil Booth4ed5bcf2001-09-24 22:53:12 +000091/* Allocates and returns a CPP_STRING token, containing TEXT of length
92 LEN, after null-terminating it. TEXT must be in permanent storage. */
93static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000094new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
Zack Weinberg711b8822000-07-18 00:59:49 +000095{
Neil Booth4ed5bcf2001-09-24 22:53:12 +000096 cpp_token *token = _cpp_temp_token (pfile);
Zack Weinberg711b8822000-07-18 00:59:49 +000097
Neil Booth4ed5bcf2001-09-24 22:53:12 +000098 text[len] = '\0';
Neil Booth93c803682000-10-28 17:59:06 +000099 token->type = CPP_STRING;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000100 token->val.str.len = len;
101 token->val.str.text = text;
Neil Booth93c803682000-10-28 17:59:06 +0000102 token->flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000103 return token;
Neil Booth93c803682000-10-28 17:59:06 +0000104}
105
Neil Booth93c803682000-10-28 17:59:06 +0000106static const char * const monthnames[] =
107{
108 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
109 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
110};
111
Neil Booth644edda2001-10-02 12:57:24 +0000112/* Handle builtin macros like __FILE__, and push the resulting token
113 on the context stack. Also handles _Pragma, for which no new token
Neil Boothd15a58c2002-01-03 18:32:55 +0000114 is created. Returns 1 if it generates a new token context, 0 to
115 return the token to the caller. */
Neil Booth278c4662002-06-19 05:40:08 +0000116const uchar *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000117_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000118{
Per Bothner12f9df42004-02-11 07:29:30 -0800119 const struct line_map *map;
Neil Booth278c4662002-06-19 05:40:08 +0000120 const uchar *result = NULL;
121 unsigned int number = 1;
Neil Booth644edda2001-10-02 12:57:24 +0000122
Neil Booth93c803682000-10-28 17:59:06 +0000123 switch (node->value.builtin)
124 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000125 default:
John David Anglin0527bc42003-11-01 22:56:54 +0000126 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +0000127 NODE_NAME (node));
Neil Booth278c4662002-06-19 05:40:08 +0000128 break;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000129
Neil Booth93c803682000-10-28 17:59:06 +0000130 case BT_FILE:
131 case BT_BASE_FILE:
Zack Weinberg711b8822000-07-18 00:59:49 +0000132 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000133 unsigned int len;
Neil Booth0bda4762000-12-11 07:45:16 +0000134 const char *name;
Neil Booth562a5c22002-04-21 18:46:42 +0000135 uchar *buf;
Per Bothner12f9df42004-02-11 07:29:30 -0800136 map = linemap_lookup (pfile->line_table, pfile->line);
Neil Booth93c803682000-10-28 17:59:06 +0000137
Neil Booth0bda4762000-12-11 07:45:16 +0000138 if (node->value.builtin == BT_BASE_FILE)
Neil Boothbb74c962001-08-17 22:23:49 +0000139 while (! MAIN_FILE_P (map))
Per Bothner50f59cd2004-01-19 21:30:18 -0800140 map = INCLUDED_FROM (pfile->line_table, map);
Neil Booth93c803682000-10-28 17:59:06 +0000141
Neil Boothbb74c962001-08-17 22:23:49 +0000142 name = map->to_file;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000143 len = strlen (name);
Neil Booth278c4662002-06-19 05:40:08 +0000144 buf = _cpp_unaligned_alloc (pfile, len * 4 + 3);
145 result = buf;
146 *buf = '"';
147 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
148 *buf++ = '"';
149 *buf = '\0';
Zack Weinberg711b8822000-07-18 00:59:49 +0000150 }
Neil Booth644edda2001-10-02 12:57:24 +0000151 break;
152
Neil Booth93c803682000-10-28 17:59:06 +0000153 case BT_INCLUDE_LEVEL:
Neil Boothd8693c62001-08-21 23:05:12 +0000154 /* The line map depth counts the primary source as level 1, but
155 historically __INCLUDE_DEPTH__ has called the primary source
156 level 0. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800157 number = pfile->line_table->depth - 1;
Neil Booth644edda2001-10-02 12:57:24 +0000158 break;
Neil Booth93c803682000-10-28 17:59:06 +0000159
160 case BT_SPECLINE:
Per Bothner12f9df42004-02-11 07:29:30 -0800161 map = linemap_lookup (pfile->line_table, pfile->line);
Neil Booth93c803682000-10-28 17:59:06 +0000162 /* If __LINE__ is embedded in a macro, it must expand to the
163 line of the macro's invocation, not its definition.
164 Otherwise things like assert() will not work properly. */
Neil Booth278c4662002-06-19 05:40:08 +0000165 if (CPP_OPTION (pfile, traditional))
166 number = pfile->line;
167 else
Per Bothner12f9df42004-02-11 07:29:30 -0800168 number = pfile->cur_token[-1].src_loc;
169 number = SOURCE_LINE (map, number);
Neil Booth644edda2001-10-02 12:57:24 +0000170 break;
Neil Booth93c803682000-10-28 17:59:06 +0000171
Zack Weinberg5279d732002-05-16 19:03:02 +0000172 /* __STDC__ has the value 1 under normal circumstances.
173 However, if (a) we are in a system header, (b) the option
Zack Weinberg2a1dc0d2002-05-21 21:55:37 +0000174 stdc_0_in_system_headers is true (set by target config), and
175 (c) we are not in strictly conforming mode, then it has the
176 value 0. */
Neil Booth93c803682000-10-28 17:59:06 +0000177 case BT_STDC:
178 {
Per Bothner12f9df42004-02-11 07:29:30 -0800179 if (cpp_in_system_header (pfile)
Zack Weinberg5279d732002-05-16 19:03:02 +0000180 && CPP_OPTION (pfile, stdc_0_in_system_headers)
Neil Booth58551c22002-08-06 20:35:46 +0000181 && !CPP_OPTION (pfile,std))
Neil Booth278c4662002-06-19 05:40:08 +0000182 number = 0;
Zack Weinberg5279d732002-05-16 19:03:02 +0000183 else
Neil Booth278c4662002-06-19 05:40:08 +0000184 number = 1;
Neil Booth93c803682000-10-28 17:59:06 +0000185 }
Neil Booth644edda2001-10-02 12:57:24 +0000186 break;
Neil Booth93c803682000-10-28 17:59:06 +0000187
188 case BT_DATE:
189 case BT_TIME:
Neil Booth278c4662002-06-19 05:40:08 +0000190 if (pfile->date == NULL)
Neil Booth93c803682000-10-28 17:59:06 +0000191 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000192 /* Allocate __DATE__ and __TIME__ strings from permanent
193 storage. We only do this once, and don't generate them
194 at init time, because time() and localtime() are very
195 slow on some systems. */
Zack Weinberg56da7202002-08-02 04:18:16 +0000196 time_t tt;
197 struct tm *tb = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000198
Zack Weinberg56da7202002-08-02 04:18:16 +0000199 /* (time_t) -1 is a legitimate value for "number of seconds
200 since the Epoch", so we have to do a little dance to
201 distinguish that from a genuine error. */
202 errno = 0;
203 tt = time(NULL);
204 if (tt != (time_t)-1 || errno == 0)
205 tb = localtime (&tt);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000206
Zack Weinberg56da7202002-08-02 04:18:16 +0000207 if (tb)
208 {
209 pfile->date = _cpp_unaligned_alloc (pfile,
210 sizeof ("\"Oct 11 1347\""));
211 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000212 monthnames[tb->tm_mon], tb->tm_mday,
213 tb->tm_year + 1900);
Zack Weinberg56da7202002-08-02 04:18:16 +0000214
215 pfile->time = _cpp_unaligned_alloc (pfile,
216 sizeof ("\"12:34:56\""));
217 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
218 tb->tm_hour, tb->tm_min, tb->tm_sec);
219 }
220 else
221 {
John David Anglin0527bc42003-11-01 22:56:54 +0000222 cpp_errno (pfile, CPP_DL_WARNING,
Zack Weinberg56da7202002-08-02 04:18:16 +0000223 "could not determine date and time");
224
225 pfile->date = U"\"??? ?? ????\"";
226 pfile->time = U"\"??:??:??\"";
227 }
Neil Booth93c803682000-10-28 17:59:06 +0000228 }
Neil Booth93c803682000-10-28 17:59:06 +0000229
Neil Booth644edda2001-10-02 12:57:24 +0000230 if (node->value.builtin == BT_DATE)
Neil Booth278c4662002-06-19 05:40:08 +0000231 result = pfile->date;
Neil Booth644edda2001-10-02 12:57:24 +0000232 else
Neil Booth278c4662002-06-19 05:40:08 +0000233 result = pfile->time;
Neil Booth644edda2001-10-02 12:57:24 +0000234 break;
Neil Booth278c4662002-06-19 05:40:08 +0000235 }
Neil Booth644edda2001-10-02 12:57:24 +0000236
Neil Booth278c4662002-06-19 05:40:08 +0000237 if (result == NULL)
238 {
239 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
240 result = _cpp_unaligned_alloc (pfile, 21);
241 sprintf ((char *) result, "%u", number);
242 }
243
244 return result;
245}
246
247/* Convert builtin macros like __FILE__ to a token and push it on the
248 context stack. Also handles _Pragma, for which no new token is
249 created. Returns 1 if it generates a new token context, 0 to
250 return the token to the caller. */
251static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000252builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth278c4662002-06-19 05:40:08 +0000253{
254 const uchar *buf;
Neil Booth26aea072003-04-19 00:22:51 +0000255 size_t len;
256 char *nbuf;
Neil Booth278c4662002-06-19 05:40:08 +0000257
258 if (node->value.builtin == BT_PRAGMA)
259 {
Neil Booth644edda2001-10-02 12:57:24 +0000260 /* Don't interpret _Pragma within directives. The standard is
261 not clear on this, but to me this makes most sense. */
262 if (pfile->state.in_directive)
263 return 0;
264
265 _cpp_do__Pragma (pfile);
266 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000267 }
Neil Booth644edda2001-10-02 12:57:24 +0000268
Neil Booth278c4662002-06-19 05:40:08 +0000269 buf = _cpp_builtin_macro_text (pfile, node);
Neil Booth26aea072003-04-19 00:22:51 +0000270 len = ustrlen (buf);
271 nbuf = alloca (len + 1);
272 memcpy (nbuf, buf, len);
273 nbuf[len]='\n';
Neil Booth278c4662002-06-19 05:40:08 +0000274
Per Bothner40de9f72003-10-02 07:30:34 +0000275 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
Neil Booth26aea072003-04-19 00:22:51 +0000276 _cpp_clean_line (pfile);
Neil Booth278c4662002-06-19 05:40:08 +0000277
278 /* Set pfile->cur_token as required by _cpp_lex_direct. */
279 pfile->cur_token = _cpp_temp_token (pfile);
280 push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
281 if (pfile->buffer->cur != pfile->buffer->rlimit)
John David Anglin0527bc42003-11-01 22:56:54 +0000282 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
Neil Booth278c4662002-06-19 05:40:08 +0000283 NODE_NAME (node));
284 _cpp_pop_buffer (pfile);
285
Neil Booth644edda2001-10-02 12:57:24 +0000286 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000287}
288
Neil Boothd15a58c2002-01-03 18:32:55 +0000289/* Copies SRC, of length LEN, to DEST, adding backslashes before all
290 backslashes and double quotes. Non-printable characters are
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000291 converted to octal. DEST must be of sufficient size. Returns
292 a pointer to the end of the string. */
Neil Booth562a5c22002-04-21 18:46:42 +0000293uchar *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000294cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
Neil Booth93c803682000-10-28 17:59:06 +0000295{
296 while (len--)
297 {
Neil Booth562a5c22002-04-21 18:46:42 +0000298 uchar c = *src++;
Neil Booth93c803682000-10-28 17:59:06 +0000299
300 if (c == '\\' || c == '"')
301 {
302 *dest++ = '\\';
303 *dest++ = c;
304 }
305 else
306 {
307 if (ISPRINT (c))
308 *dest++ = c;
309 else
310 {
311 sprintf ((char *) dest, "\\%03o", c);
312 dest += 4;
313 }
314 }
315 }
316
317 return dest;
318}
319
Neil Boothd15a58c2002-01-03 18:32:55 +0000320/* Convert a token sequence ARG to a single string token according to
321 the rules of the ISO C #-operator. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000322static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000323stringify_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +0000324{
Neil Booth6338b352003-04-23 22:44:06 +0000325 unsigned char *dest;
Neil Boothece54d52001-09-28 09:40:22 +0000326 unsigned int i, escape_it, backslash_count = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000327 const cpp_token *source = NULL;
Neil Boothece54d52001-09-28 09:40:22 +0000328 size_t len;
Neil Booth93c803682000-10-28 17:59:06 +0000329
Neil Booth6338b352003-04-23 22:44:06 +0000330 if (BUFF_ROOM (pfile->u_buff) < 3)
331 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
332 dest = BUFF_FRONT (pfile->u_buff);
333 *dest++ = '"';
334
Neil Booth93c803682000-10-28 17:59:06 +0000335 /* Loop, reading in the argument's tokens. */
336 for (i = 0; i < arg->count; i++)
337 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000338 const cpp_token *token = arg->first[i];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000339
340 if (token->type == CPP_PADDING)
341 {
342 if (source == NULL)
343 source = token->val.source;
344 continue;
345 }
Neil Booth93c803682000-10-28 17:59:06 +0000346
347 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
Zack Weinbergcc937582001-03-07 01:32:01 +0000348 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
Neil Booth93c803682000-10-28 17:59:06 +0000349
Neil Boothece54d52001-09-28 09:40:22 +0000350 /* Room for each char being written in octal, initial space and
Neil Booth6338b352003-04-23 22:44:06 +0000351 final quote and NUL. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000352 len = cpp_token_len (token);
Neil Booth93c803682000-10-28 17:59:06 +0000353 if (escape_it)
Neil Booth93c803682000-10-28 17:59:06 +0000354 len *= 4;
Neil Booth6338b352003-04-23 22:44:06 +0000355 len += 3;
Neil Booth93c803682000-10-28 17:59:06 +0000356
Neil Boothece54d52001-09-28 09:40:22 +0000357 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth93c803682000-10-28 17:59:06 +0000358 {
Neil Boothece54d52001-09-28 09:40:22 +0000359 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +0000360 _cpp_extend_buff (pfile, &pfile->u_buff, len);
Neil Boothece54d52001-09-28 09:40:22 +0000361 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth93c803682000-10-28 17:59:06 +0000362 }
363
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000364 /* Leading white space? */
Neil Booth6338b352003-04-23 22:44:06 +0000365 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000366 {
367 if (source == NULL)
368 source = token;
369 if (source->flags & PREV_WHITE)
370 *dest++ = ' ';
371 }
372 source = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000373
374 if (escape_it)
375 {
Neil Boothece54d52001-09-28 09:40:22 +0000376 _cpp_buff *buff = _cpp_get_buff (pfile, len);
377 unsigned char *buf = BUFF_FRONT (buff);
Neil Booth93c803682000-10-28 17:59:06 +0000378 len = cpp_spell_token (pfile, token, buf) - buf;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000379 dest = cpp_quote_string (dest, buf, len);
Neil Boothece54d52001-09-28 09:40:22 +0000380 _cpp_release_buff (pfile, buff);
Neil Booth93c803682000-10-28 17:59:06 +0000381 }
382 else
383 dest = cpp_spell_token (pfile, token, dest);
Neil Booth93c803682000-10-28 17:59:06 +0000384
Neil Booth10676942003-04-22 19:28:00 +0000385 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
Neil Booth93c803682000-10-28 17:59:06 +0000386 backslash_count++;
387 else
388 backslash_count = 0;
389 }
390
391 /* Ignore the final \ of invalid string literals. */
392 if (backslash_count & 1)
393 {
John David Anglin0527bc42003-11-01 22:56:54 +0000394 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000395 "invalid string literal, ignoring final '\\'");
Neil Boothece54d52001-09-28 09:40:22 +0000396 dest--;
Neil Booth93c803682000-10-28 17:59:06 +0000397 }
398
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000399 /* Commit the memory, including NUL, and return the token. */
Neil Booth6338b352003-04-23 22:44:06 +0000400 *dest++ = '"';
Neil Boothece54d52001-09-28 09:40:22 +0000401 len = dest - BUFF_FRONT (pfile->u_buff);
402 BUFF_FRONT (pfile->u_buff) = dest + 1;
403 return new_string_token (pfile, dest - len, len);
Neil Booth93c803682000-10-28 17:59:06 +0000404}
405
Kazu Hiratada7d8302002-09-22 02:03:17 +0000406/* Try to paste two tokens. On success, return nonzero. In any
Neil Boothc9e7a602001-09-27 12:59:38 +0000407 case, PLHS is updated to point to the pasted token, which is
408 guaranteed to not have the PASTE_LEFT flag set. */
409static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000410paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
Neil Boothd63eefb2000-11-20 23:59:26 +0000411{
Neil Boothc9e7a602001-09-27 12:59:38 +0000412 unsigned char *buf, *end;
413 const cpp_token *lhs;
414 unsigned int len;
415 bool valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000416
Neil Boothc9e7a602001-09-27 12:59:38 +0000417 lhs = *plhs;
418 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +0000419 buf = alloca (len);
Neil Boothc9e7a602001-09-27 12:59:38 +0000420 end = cpp_spell_token (pfile, lhs, buf);
Neil Boothd63eefb2000-11-20 23:59:26 +0000421
Neil Boothc9e7a602001-09-27 12:59:38 +0000422 /* Avoid comment headers, since they are still processed in stage 3.
423 It is simpler to insert a space here, rather than modifying the
424 lexer to ignore comments in some circumstances. Simply returning
425 false doesn't work, since we want to clear the PASTE_LEFT flag. */
Neil Booth5cc67322002-10-09 09:56:09 +0000426 if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
Neil Boothc9e7a602001-09-27 12:59:38 +0000427 *end++ = ' ';
428 end = cpp_spell_token (pfile, rhs, end);
Neil Booth26aea072003-04-19 00:22:51 +0000429 *end = '\n';
Neil Boothd63eefb2000-11-20 23:59:26 +0000430
Per Bothner40de9f72003-10-02 07:30:34 +0000431 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
Neil Booth26aea072003-04-19 00:22:51 +0000432 _cpp_clean_line (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000433
Neil Boothc9e7a602001-09-27 12:59:38 +0000434 /* Set pfile->cur_token as required by _cpp_lex_direct. */
435 pfile->cur_token = _cpp_temp_token (pfile);
436 *plhs = _cpp_lex_direct (pfile);
Neil Booth480709c2001-10-21 14:04:42 +0000437 valid = pfile->buffer->cur == pfile->buffer->rlimit;
Neil Boothc9e7a602001-09-27 12:59:38 +0000438 _cpp_pop_buffer (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000439
Neil Boothc9e7a602001-09-27 12:59:38 +0000440 return valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000441}
442
Neil Boothd15a58c2002-01-03 18:32:55 +0000443/* Handles an arbitrarily long sequence of ## operators, with initial
444 operand LHS. This implementation is left-associative,
445 non-recursive, and finishes a paste before handling succeeding
446 ones. If a paste fails, we back up to the RHS of the failing ##
447 operator before pushing the context containing the result of prior
448 successful pastes, with the effect that the RHS appears in the
449 output stream after the pasted LHS normally. */
Neil Booth93c803682000-10-28 17:59:06 +0000450static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000451paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
Neil Booth93c803682000-10-28 17:59:06 +0000452{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000453 const cpp_token *rhs;
454 cpp_context *context = pfile->context;
455
Neil Booth93c803682000-10-28 17:59:06 +0000456 do
457 {
458 /* Take the token directly from the current context. We can do
459 this, because we are in the replacement list of either an
460 object-like macro, or a function-like macro with arguments
461 inserted. In either case, the constraints to #define
Neil Boothd63eefb2000-11-20 23:59:26 +0000462 guarantee we have at least one more token. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000463 if (context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +0000464 rhs = FIRST (context).token++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000465 else
Neil Booth82eda772002-06-04 13:07:06 +0000466 rhs = *FIRST (context).ptoken++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000467
468 if (rhs->type == CPP_PADDING)
469 abort ();
470
Neil Boothc9e7a602001-09-27 12:59:38 +0000471 if (!paste_tokens (pfile, &lhs, rhs))
Neil Booth93c803682000-10-28 17:59:06 +0000472 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000473 _cpp_backup_tokens (pfile, 1);
Neil Boothc9e7a602001-09-27 12:59:38 +0000474
Neil Boothc3bf3e62002-05-09 17:14:22 +0000475 /* Mandatory error for all apart from assembler. */
Neil Boothc9e7a602001-09-27 12:59:38 +0000476 if (CPP_OPTION (pfile, lang) != CLK_ASM)
John David Anglin0527bc42003-11-01 22:56:54 +0000477 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothc9e7a602001-09-27 12:59:38 +0000478 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
Neil Boothebef4e82002-04-14 18:42:47 +0000479 cpp_token_as_text (pfile, lhs),
480 cpp_token_as_text (pfile, rhs));
Neil Booth4c2b6472000-11-11 13:19:01 +0000481 break;
Neil Booth93c803682000-10-28 17:59:06 +0000482 }
Neil Booth93c803682000-10-28 17:59:06 +0000483 }
484 while (rhs->flags & PASTE_LEFT);
485
Neil Boothc9e7a602001-09-27 12:59:38 +0000486 /* Put the resulting token in its own context. */
487 push_token_context (pfile, NULL, lhs, 1);
Neil Booth93c803682000-10-28 17:59:06 +0000488}
489
Neil Booth1ce676a2002-06-09 20:04:17 +0000490/* Returns TRUE if the number of arguments ARGC supplied in an
491 invocation of the MACRO referenced by NODE is valid. An empty
492 invocation to a macro with no parameters should pass ARGC as zero.
493
494 Note that MACRO cannot necessarily be deduced from NODE, in case
495 NODE was redefined whilst collecting arguments. */
496bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000497_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
Neil Booth1ce676a2002-06-09 20:04:17 +0000498{
499 if (argc == macro->paramc)
500 return true;
501
502 if (argc < macro->paramc)
503 {
504 /* As an extension, a rest argument is allowed to not appear in
505 the invocation at all.
506 e.g. #define debug(format, args...) something
507 debug("string");
508
509 This is exactly the same as if there had been an empty rest
510 argument - debug("string", ). */
511
512 if (argc + 1 == macro->paramc && macro->variadic)
513 {
514 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
John David Anglin0527bc42003-11-01 22:56:54 +0000515 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth1ce676a2002-06-09 20:04:17 +0000516 "ISO C99 requires rest arguments to be used");
517 return true;
518 }
519
John David Anglin0527bc42003-11-01 22:56:54 +0000520 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1ce676a2002-06-09 20:04:17 +0000521 "macro \"%s\" requires %u arguments, but only %u given",
522 NODE_NAME (node), macro->paramc, argc);
523 }
524 else
John David Anglin0527bc42003-11-01 22:56:54 +0000525 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1ce676a2002-06-09 20:04:17 +0000526 "macro \"%s\" passed %u arguments, but takes just %u",
527 NODE_NAME (node), argc, macro->paramc);
528
529 return false;
530}
531
Neil Boothd15a58c2002-01-03 18:32:55 +0000532/* Reads and returns the arguments to a function-like macro
533 invocation. Assumes the opening parenthesis has been processed.
534 If there is an error, emits an appropriate diagnostic and returns
535 NULL. Each argument is terminated by a CPP_EOF token, for the
536 future benefit of expand_arg(). */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000537static _cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000538collect_args (cpp_reader *pfile, const cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000539{
Neil Boothb8af0ca2001-09-26 17:52:50 +0000540 _cpp_buff *buff, *base_buff;
541 cpp_macro *macro;
542 macro_arg *args, *arg;
543 const cpp_token *token;
544 unsigned int argc;
Neil Booth93c803682000-10-28 17:59:06 +0000545
Neil Boothb8af0ca2001-09-26 17:52:50 +0000546 macro = node->value.macro;
547 if (macro->paramc)
548 argc = macro->paramc;
549 else
550 argc = 1;
551 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
552 + sizeof (macro_arg)));
553 base_buff = buff;
554 args = (macro_arg *) buff->base;
555 memset (args, 0, argc * sizeof (macro_arg));
Neil Boothece54d52001-09-28 09:40:22 +0000556 buff->cur = (unsigned char *) &args[argc];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000557 arg = args, argc = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000558
Neil Boothb8af0ca2001-09-26 17:52:50 +0000559 /* Collect the tokens making up each argument. We don't yet know
560 how many arguments have been supplied, whether too many or too
561 few. Hence the slightly bizarre usage of "argc" and "arg". */
562 do
Neil Booth93c803682000-10-28 17:59:06 +0000563 {
Neil Boothb8af0ca2001-09-26 17:52:50 +0000564 unsigned int paren_depth = 0;
565 unsigned int ntokens = 0;
566
Neil Booth93c803682000-10-28 17:59:06 +0000567 argc++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000568 arg->first = (const cpp_token **) buff->cur;
Neil Booth93c803682000-10-28 17:59:06 +0000569
Neil Boothb8af0ca2001-09-26 17:52:50 +0000570 for (;;)
571 {
572 /* Require space for 2 new tokens (including a CPP_EOF). */
Neil Boothece54d52001-09-28 09:40:22 +0000573 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000574 {
Neil Booth8c3b2692001-09-30 10:03:11 +0000575 buff = _cpp_append_extend_buff (pfile, buff,
576 1000 * sizeof (cpp_token *));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000577 arg->first = (const cpp_token **) buff->cur;
578 }
Neil Booth93c803682000-10-28 17:59:06 +0000579
Neil Boothb8af0ca2001-09-26 17:52:50 +0000580 token = cpp_get_token (pfile);
581
582 if (token->type == CPP_PADDING)
583 {
584 /* Drop leading padding. */
585 if (ntokens == 0)
586 continue;
587 }
588 else if (token->type == CPP_OPEN_PAREN)
589 paren_depth++;
590 else if (token->type == CPP_CLOSE_PAREN)
591 {
592 if (paren_depth-- == 0)
593 break;
594 }
595 else if (token->type == CPP_COMMA)
596 {
597 /* A comma does not terminate an argument within
598 parentheses or as part of a variable argument. */
599 if (paren_depth == 0
600 && ! (macro->variadic && argc == macro->paramc))
601 break;
602 }
603 else if (token->type == CPP_EOF
604 || (token->type == CPP_HASH && token->flags & BOL))
605 break;
606
607 arg->first[ntokens++] = token;
608 }
609
610 /* Drop trailing padding. */
611 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
612 ntokens--;
613
614 arg->count = ntokens;
615 arg->first[ntokens] = &pfile->eof;
616
617 /* Terminate the argument. Excess arguments loop back and
618 overwrite the final legitimate argument, before failing. */
619 if (argc <= macro->paramc)
620 {
Neil Boothece54d52001-09-28 09:40:22 +0000621 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000622 if (argc != macro->paramc)
623 arg++;
624 }
Neil Booth93c803682000-10-28 17:59:06 +0000625 }
Neil Boothe808ec92002-02-27 07:24:53 +0000626 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
Neil Booth93c803682000-10-28 17:59:06 +0000627
Neil Boothe808ec92002-02-27 07:24:53 +0000628 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000629 {
Neil Boothece54d52001-09-28 09:40:22 +0000630 /* We still need the CPP_EOF to end directives, and to end
631 pre-expansion of a macro argument. Step back is not
632 unconditional, since we don't want to return a CPP_EOF to our
633 callers at the end of an -include-d file. */
Neil Boothe808ec92002-02-27 07:24:53 +0000634 if (pfile->context->prev || pfile->state.in_directive)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000635 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +0000636 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000637 "unterminated argument list invoking macro \"%s\"",
Neil Bootha28c50352001-05-16 22:02:09 +0000638 NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +0000639 }
Neil Booth1ce676a2002-06-09 20:04:17 +0000640 else
Neil Booth93c803682000-10-28 17:59:06 +0000641 {
Neil Booth1ce676a2002-06-09 20:04:17 +0000642 /* A single empty argument is counted as no argument. */
643 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
644 argc = 0;
645 if (_cpp_arguments_ok (pfile, macro, node, argc))
Neil Booth58551c22002-08-06 20:35:46 +0000646 {
647 /* GCC has special semantics for , ## b where b is a varargs
648 parameter: we remove the comma if b was omitted entirely.
649 If b was merely an empty argument, the comma is retained.
650 If the macro takes just one (varargs) parameter, then we
651 retain the comma only if we are standards conforming.
652
653 If FIRST is NULL replace_args () swallows the comma. */
654 if (macro->variadic && (argc < macro->paramc
655 || (argc == 1 && args[0].count == 0
656 && !CPP_OPTION (pfile, std))))
657 args[macro->paramc - 1].first = NULL;
658 return base_buff;
659 }
Neil Booth93c803682000-10-28 17:59:06 +0000660 }
661
Neil Booth1ce676a2002-06-09 20:04:17 +0000662 /* An error occurred. */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000663 _cpp_release_buff (pfile, base_buff);
664 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000665}
666
Neil Boothd6da8362001-10-08 06:15:14 +0000667/* Search for an opening parenthesis to the macro of NODE, in such a
668 way that, if none is found, we don't lose the information in any
669 intervening padding tokens. If we find the parenthesis, collect
670 the arguments and return the buffer containing them. */
671static _cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000672funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000673{
Neil Boothd6da8362001-10-08 06:15:14 +0000674 const cpp_token *token, *padding = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000675
Neil Boothd6da8362001-10-08 06:15:14 +0000676 for (;;)
Neil Boothbdcbe492001-09-13 20:05:17 +0000677 {
Neil Boothd6da8362001-10-08 06:15:14 +0000678 token = cpp_get_token (pfile);
679 if (token->type != CPP_PADDING)
680 break;
681 if (padding == NULL
682 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
683 padding = token;
Neil Boothbdcbe492001-09-13 20:05:17 +0000684 }
Neil Booth93c803682000-10-28 17:59:06 +0000685
Neil Boothd6da8362001-10-08 06:15:14 +0000686 if (token->type == CPP_OPEN_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000687 {
Neil Boothd6da8362001-10-08 06:15:14 +0000688 pfile->state.parsing_args = 2;
689 return collect_args (pfile, node);
Neil Booth93c803682000-10-28 17:59:06 +0000690 }
691
Neil Booth9ac3b1b2002-04-21 16:17:55 +0000692 /* CPP_EOF can be the end of macro arguments, or the end of the
693 file. We mustn't back up over the latter. Ugh. */
694 if (token->type != CPP_EOF || token == &pfile->eof)
695 {
696 /* Back up. We may have skipped padding, in which case backing
697 up more than one token when expanding macros is in general
698 too difficult. We re-insert it in its own context. */
699 _cpp_backup_tokens (pfile, 1);
700 if (padding)
701 push_token_context (pfile, NULL, padding, 1);
702 }
Neil Boothd6da8362001-10-08 06:15:14 +0000703
704 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000705}
706
Neil Boothd15a58c2002-01-03 18:32:55 +0000707/* Push the context of a macro with hash entry NODE onto the context
708 stack. If we can successfully expand the macro, we push a context
709 containing its yet-to-be-rescanned replacement list and return one.
710 Otherwise, we don't push a context and return zero. */
Neil Booth93c803682000-10-28 17:59:06 +0000711static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000712enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000713{
Neil Boothd15a58c2002-01-03 18:32:55 +0000714 /* The presence of a macro invalidates a file's controlling macro. */
Neil Booth644edda2001-10-02 12:57:24 +0000715 pfile->mi_valid = false;
716
Neil Booth36207112002-05-24 19:26:30 +0000717 pfile->state.angled_headers = false;
718
Neil Boothd15a58c2002-01-03 18:32:55 +0000719 /* Handle standard macros. */
Neil Booth644edda2001-10-02 12:57:24 +0000720 if (! (node->flags & NODE_BUILTIN))
Neil Booth93c803682000-10-28 17:59:06 +0000721 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000722 cpp_macro *macro = node->value.macro;
723
Neil Boothd6da8362001-10-08 06:15:14 +0000724 if (macro->fun_like)
725 {
726 _cpp_buff *buff;
727
728 pfile->state.prevent_expansion++;
729 pfile->keep_tokens++;
730 pfile->state.parsing_args = 1;
731 buff = funlike_invocation_p (pfile, node);
732 pfile->state.parsing_args = 0;
733 pfile->keep_tokens--;
734 pfile->state.prevent_expansion--;
735
736 if (buff == NULL)
737 {
738 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
John David Anglin0527bc42003-11-01 22:56:54 +0000739 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothd6da8362001-10-08 06:15:14 +0000740 "function-like macro \"%s\" must be used with arguments in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +0000741 NODE_NAME (node));
Neil Boothd6da8362001-10-08 06:15:14 +0000742
743 return 0;
744 }
745
Neil Boothe808ec92002-02-27 07:24:53 +0000746 if (macro->paramc > 0)
747 replace_args (pfile, node, macro, (macro_arg *) buff->base);
Neil Boothd6da8362001-10-08 06:15:14 +0000748 _cpp_release_buff (pfile, buff);
749 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000750
751 /* Disable the macro within its expansion. */
Neil Booth644edda2001-10-02 12:57:24 +0000752 node->flags |= NODE_DISABLED;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000753
Neil Bootha69cbaa2002-07-23 22:57:49 +0000754 macro->used = 1;
755
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000756 if (macro->paramc == 0)
Neil Booth601328b2002-05-16 05:53:24 +0000757 push_token_context (pfile, node, macro->exp.tokens, macro->count);
Neil Booth644edda2001-10-02 12:57:24 +0000758
759 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000760 }
Neil Booth644edda2001-10-02 12:57:24 +0000761
Neil Boothd15a58c2002-01-03 18:32:55 +0000762 /* Handle built-in macros and the _Pragma operator. */
Neil Booth644edda2001-10-02 12:57:24 +0000763 return builtin_macro (pfile, node);
Zack Weinberg711b8822000-07-18 00:59:49 +0000764}
765
Neil Boothd15a58c2002-01-03 18:32:55 +0000766/* Replace the parameters in a function-like macro of NODE with the
767 actual ARGS, and place the result in a newly pushed token context.
768 Expand each argument before replacing, unless it is operated upon
769 by the # or ## operators. */
Neil Booth93c803682000-10-28 17:59:06 +0000770static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000771replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
Neil Booth93c803682000-10-28 17:59:06 +0000772{
773 unsigned int i, total;
774 const cpp_token *src, *limit;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000775 const cpp_token **dest, **first;
Neil Booth93c803682000-10-28 17:59:06 +0000776 macro_arg *arg;
Neil Booth1e013d22001-09-26 21:44:35 +0000777 _cpp_buff *buff;
Neil Booth93c803682000-10-28 17:59:06 +0000778
Neil Booth93c803682000-10-28 17:59:06 +0000779 /* First, fully macro-expand arguments, calculating the number of
Neil Booth1e013d22001-09-26 21:44:35 +0000780 tokens in the final expansion as we go. The ordering of the if
781 statements below is subtle; we must handle stringification before
782 pasting. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000783 total = macro->count;
Neil Booth601328b2002-05-16 05:53:24 +0000784 limit = macro->exp.tokens + macro->count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000785
Neil Booth601328b2002-05-16 05:53:24 +0000786 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth93c803682000-10-28 17:59:06 +0000787 if (src->type == CPP_MACRO_ARG)
788 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000789 /* Leading and trailing padding tokens. */
790 total += 2;
791
Neil Booth93c803682000-10-28 17:59:06 +0000792 /* We have an argument. If it is not being stringified or
793 pasted it is macro-replaced before insertion. */
Neil Booth6c53ebf2000-11-06 18:43:32 +0000794 arg = &args[src->val.arg_no - 1];
Neil Booth4c2b6472000-11-11 13:19:01 +0000795
Neil Booth93c803682000-10-28 17:59:06 +0000796 if (src->flags & STRINGIFY_ARG)
797 {
798 if (!arg->stringified)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000799 arg->stringified = stringify_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000800 }
801 else if ((src->flags & PASTE_LEFT)
Neil Booth601328b2002-05-16 05:53:24 +0000802 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
Neil Booth93c803682000-10-28 17:59:06 +0000803 total += arg->count - 1;
804 else
805 {
806 if (!arg->expanded)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000807 expand_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000808 total += arg->expanded_count - 1;
809 }
810 }
811
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000812 /* Now allocate space for the expansion, copy the tokens and replace
813 the arguments. */
Neil Booth1e013d22001-09-26 21:44:35 +0000814 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
815 first = (const cpp_token **) buff->base;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000816 dest = first;
Neil Booth93c803682000-10-28 17:59:06 +0000817
Neil Booth601328b2002-05-16 05:53:24 +0000818 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000819 {
820 unsigned int count;
821 const cpp_token **from, **paste_flag;
Neil Booth93c803682000-10-28 17:59:06 +0000822
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000823 if (src->type != CPP_MACRO_ARG)
824 {
825 *dest++ = src;
826 continue;
827 }
828
829 paste_flag = 0;
830 arg = &args[src->val.arg_no - 1];
831 if (src->flags & STRINGIFY_ARG)
832 count = 1, from = &arg->stringified;
833 else if (src->flags & PASTE_LEFT)
834 count = arg->count, from = arg->first;
Neil Booth601328b2002-05-16 05:53:24 +0000835 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000836 {
Neil Booth93c803682000-10-28 17:59:06 +0000837 count = arg->count, from = arg->first;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000838 if (dest != first)
839 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000840 if (dest[-1]->type == CPP_COMMA
841 && macro->variadic
842 && src->val.arg_no == macro->paramc)
843 {
Neil Booth58551c22002-08-06 20:35:46 +0000844 /* Swallow a pasted comma if from == NULL, otherwise
845 drop the paste flag. */
846 if (from == NULL)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000847 dest--;
848 else
849 paste_flag = dest - 1;
850 }
851 /* Remove the paste flag if the RHS is a placemarker. */
852 else if (count == 0)
853 paste_flag = dest - 1;
854 }
855 }
856 else
857 count = arg->expanded_count, from = arg->expanded;
Neil Booth93c803682000-10-28 17:59:06 +0000858
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000859 /* Padding on the left of an argument (unless RHS of ##). */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000860 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
Neil Booth601328b2002-05-16 05:53:24 +0000861 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000862 *dest++ = padding_token (pfile, src);
Neil Booth93c803682000-10-28 17:59:06 +0000863
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000864 if (count)
865 {
866 memcpy (dest, from, count * sizeof (cpp_token *));
867 dest += count;
Neil Booth93c803682000-10-28 17:59:06 +0000868
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000869 /* With a non-empty argument on the LHS of ##, the last
870 token should be flagged PASTE_LEFT. */
871 if (src->flags & PASTE_LEFT)
872 paste_flag = dest - 1;
873 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000874
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000875 /* Avoid paste on RHS (even case count == 0). */
876 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
877 *dest++ = &pfile->avoid_paste;
Neil Booth26ec42e2001-01-28 11:22:23 +0000878
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000879 /* Add a new paste flag, or remove an unwanted one. */
880 if (paste_flag)
881 {
882 cpp_token *token = _cpp_temp_token (pfile);
883 token->type = (*paste_flag)->type;
884 token->val.str = (*paste_flag)->val.str;
885 if (src->flags & PASTE_LEFT)
886 token->flags = (*paste_flag)->flags | PASTE_LEFT;
887 else
888 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
889 *paste_flag = token;
890 }
891 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000892
Neil Booth93c803682000-10-28 17:59:06 +0000893 /* Free the expanded arguments. */
894 for (i = 0; i < macro->paramc; i++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000895 if (args[i].expanded)
896 free (args[i].expanded);
897
Neil Booth644edda2001-10-02 12:57:24 +0000898 push_ptoken_context (pfile, node, buff, first, dest - first);
Neil Booth93c803682000-10-28 17:59:06 +0000899}
900
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000901/* Return a special padding token, with padding inherited from SOURCE. */
902static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000903padding_token (cpp_reader *pfile, const cpp_token *source)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000904{
905 cpp_token *result = _cpp_temp_token (pfile);
906
907 result->type = CPP_PADDING;
908 result->val.source = source;
909 result->flags = 0;
910 return result;
911}
912
Neil Boothd15a58c2002-01-03 18:32:55 +0000913/* Get a new uninitialized context. Create a new one if we cannot
914 re-use an old one. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000915static cpp_context *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000916next_context (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000917{
918 cpp_context *result = pfile->context->next;
919
920 if (result == 0)
921 {
922 result = xnew (cpp_context);
923 result->prev = pfile->context;
924 result->next = 0;
925 pfile->context->next = result;
926 }
927
928 pfile->context = result;
929 return result;
930}
931
932/* Push a list of pointers to tokens. */
933static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000934push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
935 const cpp_token **first, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +0000936{
937 cpp_context *context = next_context (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000938
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000939 context->direct_p = false;
940 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000941 context->buff = buff;
Neil Booth82eda772002-06-04 13:07:06 +0000942 FIRST (context).ptoken = first;
943 LAST (context).ptoken = first + count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000944}
945
946/* Push a list of tokens. */
947static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000948push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
949 const cpp_token *first, unsigned int count)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000950{
951 cpp_context *context = next_context (pfile);
952
953 context->direct_p = true;
954 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000955 context->buff = NULL;
Neil Booth82eda772002-06-04 13:07:06 +0000956 FIRST (context).token = first;
957 LAST (context).token = first + count;
Neil Booth93c803682000-10-28 17:59:06 +0000958}
959
Neil Boothcbc69f82002-06-05 20:27:12 +0000960/* Push a traditional macro's replacement text. */
961void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000962_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
963 const uchar *start, size_t len)
Neil Boothcbc69f82002-06-05 20:27:12 +0000964{
965 cpp_context *context = next_context (pfile);
966
967 context->direct_p = true;
968 context->macro = macro;
969 context->buff = NULL;
970 CUR (context) = start;
Neil Booth1ce676a2002-06-09 20:04:17 +0000971 RLIMIT (context) = start + len;
Neil Booth974c43f2002-06-13 06:25:28 +0000972 macro->flags |= NODE_DISABLED;
Neil Boothcbc69f82002-06-05 20:27:12 +0000973}
974
Neil Boothd15a58c2002-01-03 18:32:55 +0000975/* Expand an argument ARG before replacing parameters in a
976 function-like macro. This works by pushing a context with the
977 argument's tokens, and then expanding that into a temporary buffer
978 as if it were a normal part of the token stream. collect_args()
979 has terminated the argument's tokens with a CPP_EOF so that we know
980 when we have fully expanded the argument. */
Neil Booth93c803682000-10-28 17:59:06 +0000981static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000982expand_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +0000983{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000984 unsigned int capacity;
Neil Booth56941bf2002-09-20 19:44:09 +0000985 bool saved_warn_trad;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000986
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000987 if (arg->count == 0)
988 return;
Neil Booth93c803682000-10-28 17:59:06 +0000989
Neil Booth56941bf2002-09-20 19:44:09 +0000990 /* Don't warn about funlike macros when pre-expanding. */
991 saved_warn_trad = CPP_WTRADITIONAL (pfile);
992 CPP_WTRADITIONAL (pfile) = 0;
993
Neil Booth93c803682000-10-28 17:59:06 +0000994 /* Loop, reading in the arguments. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000995 capacity = 256;
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +0000996 arg->expanded = xmalloc (capacity * sizeof (cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +0000997
Neil Booth1e013d22001-09-26 21:44:35 +0000998 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000999 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00001000 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001001 const cpp_token *token;
1002
1003 if (arg->expanded_count + 1 >= capacity)
Neil Booth93c803682000-10-28 17:59:06 +00001004 {
1005 capacity *= 2;
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001006 arg->expanded = xrealloc (arg->expanded,
1007 capacity * sizeof (cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +00001008 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001009
1010 token = cpp_get_token (pfile);
1011
1012 if (token->type == CPP_EOF)
1013 break;
1014
1015 arg->expanded[arg->expanded_count++] = token;
Neil Booth93c803682000-10-28 17:59:06 +00001016 }
Neil Booth93c803682000-10-28 17:59:06 +00001017
Neil Booth1e013d22001-09-26 21:44:35 +00001018 _cpp_pop_context (pfile);
Neil Booth56941bf2002-09-20 19:44:09 +00001019
1020 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
Neil Booth93c803682000-10-28 17:59:06 +00001021}
1022
Neil Boothd15a58c2002-01-03 18:32:55 +00001023/* Pop the current context off the stack, re-enabling the macro if the
1024 context represented a macro's replacement list. The context
1025 structure is not freed so that we can re-use it later. */
Neil Booth93c803682000-10-28 17:59:06 +00001026void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001027_cpp_pop_context (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001028{
Neil Booth1e013d22001-09-26 21:44:35 +00001029 cpp_context *context = pfile->context;
Neil Booth93c803682000-10-28 17:59:06 +00001030
Neil Booth1e013d22001-09-26 21:44:35 +00001031 if (context->macro)
Neil Booth644edda2001-10-02 12:57:24 +00001032 context->macro->flags &= ~NODE_DISABLED;
Neil Booth1e013d22001-09-26 21:44:35 +00001033
1034 if (context->buff)
1035 _cpp_release_buff (pfile, context->buff);
1036
1037 pfile->context = context->prev;
Neil Booth93c803682000-10-28 17:59:06 +00001038}
1039
Martin Schaffner48c47212003-06-25 23:01:10 +02001040/* External routine to get a token. Also used nearly everywhere
Neil Booth7f2f1a62000-11-14 18:32:06 +00001041 internally, except for places where we know we can safely call
Martin Schaffner48c47212003-06-25 23:01:10 +02001042 _cpp_lex_token directly, such as lexing a directive name.
Neil Booth7f2f1a62000-11-14 18:32:06 +00001043
1044 Macro expansions and directives are transparently handled,
1045 including entering included files. Thus tokens are post-macro
1046 expansion, and after any intervening directives. External callers
1047 see CPP_EOF only at EOF. Internal callers also see it when meeting
1048 a directive inside a macro call, when at the end of a directive and
1049 state.in_directive is still 1, and at the end of argument
1050 pre-expansion. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001051const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001052cpp_get_token (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001053{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001054 const cpp_token *result;
1055
Neil Booth29b10742000-11-13 18:40:37 +00001056 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00001057 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001058 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001059 cpp_context *context = pfile->context;
1060
Neil Booth93c803682000-10-28 17:59:06 +00001061 /* Context->prev == 0 <=> base context. */
Neil Boothbdcbe492001-09-13 20:05:17 +00001062 if (!context->prev)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001063 result = _cpp_lex_token (pfile);
Neil Booth82eda772002-06-04 13:07:06 +00001064 else if (FIRST (context).token != LAST (context).token)
Neil Booth29b10742000-11-13 18:40:37 +00001065 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001066 if (context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +00001067 result = FIRST (context).token++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001068 else
Neil Booth82eda772002-06-04 13:07:06 +00001069 result = *FIRST (context).ptoken++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001070
1071 if (result->flags & PASTE_LEFT)
Neil Boothec1a23e2001-01-31 07:48:54 +00001072 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001073 paste_all_tokens (pfile, result);
1074 if (pfile->state.in_directive)
1075 continue;
1076 return padding_token (pfile, result);
Neil Boothec1a23e2001-01-31 07:48:54 +00001077 }
Neil Booth29b10742000-11-13 18:40:37 +00001078 }
Neil Booth93c803682000-10-28 17:59:06 +00001079 else
1080 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001081 _cpp_pop_context (pfile);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001082 if (pfile->state.in_directive)
1083 continue;
1084 return &pfile->avoid_paste;
Neil Booth93c803682000-10-28 17:59:06 +00001085 }
Neil Booth93c803682000-10-28 17:59:06 +00001086
Jason Thorpe477cdac2002-04-07 03:12:23 +00001087 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1088 continue;
1089
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001090 if (result->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +00001091 break;
1092
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001093 node = result->val.node;
Neil Booth4c2b6472000-11-11 13:19:01 +00001094
Neil Booth644edda2001-10-02 12:57:24 +00001095 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1096 break;
Kazu Hiratadf383482002-05-22 22:02:16 +00001097
Neil Booth644edda2001-10-02 12:57:24 +00001098 if (!(node->flags & NODE_DISABLED))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001099 {
Neil Booth644edda2001-10-02 12:57:24 +00001100 if (!pfile->state.prevent_expansion
1101 && enter_macro_context (pfile, node))
Neil Boothbd969772001-02-01 19:13:53 +00001102 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001103 if (pfile->state.in_directive)
1104 continue;
1105 return padding_token (pfile, result);
Neil Boothbd969772001-02-01 19:13:53 +00001106 }
Neil Booth93c803682000-10-28 17:59:06 +00001107 }
Neil Booth644edda2001-10-02 12:57:24 +00001108 else
1109 {
Neil Boothd15a58c2002-01-03 18:32:55 +00001110 /* Flag this token as always unexpandable. FIXME: move this
1111 to collect_args()?. */
Neil Booth644edda2001-10-02 12:57:24 +00001112 cpp_token *t = _cpp_temp_token (pfile);
1113 t->type = result->type;
1114 t->flags = result->flags | NO_EXPAND;
1115 t->val.str = result->val.str;
1116 result = t;
1117 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001118
Neil Booth644edda2001-10-02 12:57:24 +00001119 break;
Neil Booth93c803682000-10-28 17:59:06 +00001120 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001121
1122 return result;
Neil Booth93c803682000-10-28 17:59:06 +00001123}
1124
Neil Booth7065e132001-02-14 07:38:20 +00001125/* Returns true if we're expanding an object-like macro that was
1126 defined in a system header. Just checks the macro at the top of
1127 the stack. Used for diagnostic suppression. */
1128int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001129cpp_sys_macro_p (cpp_reader *pfile)
Neil Booth7065e132001-02-14 07:38:20 +00001130{
Neil Booth644edda2001-10-02 12:57:24 +00001131 cpp_hashnode *node = pfile->context->macro;
Neil Booth7065e132001-02-14 07:38:20 +00001132
Neil Booth644edda2001-10-02 12:57:24 +00001133 return node && node->value.macro && node->value.macro->syshdr;
Neil Booth7065e132001-02-14 07:38:20 +00001134}
1135
Neil Boothaf0d16c2002-04-22 17:48:02 +00001136/* Read each token in, until end of the current file. Directives are
1137 transparently processed. */
Neil Booth93c803682000-10-28 17:59:06 +00001138void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001139cpp_scan_nooutput (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001140{
Per Bothner22234f52004-02-18 14:02:39 -08001141 /* Request a CPP_EOF token at the end of this file, rather than
1142 transparently continuing with the including file. */
1143 pfile->buffer->return_at_eof = true;
1144
Neil Booth590e1982002-07-01 12:47:54 +00001145 if (CPP_OPTION (pfile, traditional))
1146 while (_cpp_read_logical_line_trad (pfile))
1147 ;
1148 else
1149 while (cpp_get_token (pfile)->type != CPP_EOF)
1150 ;
Neil Booth93c803682000-10-28 17:59:06 +00001151}
1152
Neil Boothbdcbe492001-09-13 20:05:17 +00001153/* Step back one (or more) tokens. Can only step mack more than 1 if
1154 they are from the lexer, and not from macro expansion. */
Neil Boothb528a072000-11-12 11:46:21 +00001155void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001156_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +00001157{
Neil Boothbdcbe492001-09-13 20:05:17 +00001158 if (pfile->context->prev == NULL)
1159 {
1160 pfile->lookaheads += count;
1161 while (count--)
1162 {
Neil Booth3293c3e2001-11-19 21:04:49 +00001163 pfile->cur_token--;
1164 if (pfile->cur_token == pfile->cur_run->base
1165 /* Possible with -fpreprocessed and no leading #line. */
1166 && pfile->cur_run->prev != NULL)
Neil Boothbdcbe492001-09-13 20:05:17 +00001167 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001168 pfile->cur_run = pfile->cur_run->prev;
1169 pfile->cur_token = pfile->cur_run->limit;
1170 }
1171 }
1172 }
Neil Booth93c803682000-10-28 17:59:06 +00001173 else
1174 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001175 if (count != 1)
1176 abort ();
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001177 if (pfile->context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +00001178 FIRST (pfile->context).token--;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001179 else
Neil Booth82eda772002-06-04 13:07:06 +00001180 FIRST (pfile->context).ptoken--;
Neil Booth93c803682000-10-28 17:59:06 +00001181 }
Neil Booth93c803682000-10-28 17:59:06 +00001182}
1183
1184/* #define directive parsing and handling. */
1185
Kazu Hiratada7d8302002-09-22 02:03:17 +00001186/* Returns nonzero if a macro redefinition warning is required. */
Neil Boothcbc69f82002-06-05 20:27:12 +00001187static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001188warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
1189 const cpp_macro *macro2)
Neil Booth93c803682000-10-28 17:59:06 +00001190{
1191 const cpp_macro *macro1;
1192 unsigned int i;
1193
Neil Booth618cdda2001-02-25 09:43:03 +00001194 /* Some redefinitions need to be warned about regardless. */
1195 if (node->flags & NODE_WARN)
Neil Boothcbc69f82002-06-05 20:27:12 +00001196 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001197
Neil Booth618cdda2001-02-25 09:43:03 +00001198 /* Redefinition of a macro is allowed if and only if the old and new
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001199 definitions are the same. (6.10.3 paragraph 2). */
Neil Booth93c803682000-10-28 17:59:06 +00001200 macro1 = node->value.macro;
1201
Neil Booth6618c5d2002-06-10 06:03:13 +00001202 /* Don't check count here as it can be different in valid
1203 traditional redefinitions with just whitespace differences. */
1204 if (macro1->paramc != macro2->paramc
Neil Booth93c803682000-10-28 17:59:06 +00001205 || macro1->fun_like != macro2->fun_like
Neil Booth28e0f042000-12-09 12:06:37 +00001206 || macro1->variadic != macro2->variadic)
Neil Boothcbc69f82002-06-05 20:27:12 +00001207 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001208
1209 /* Check parameter spellings. */
1210 for (i = 0; i < macro1->paramc; i++)
1211 if (macro1->params[i] != macro2->params[i])
Neil Boothcbc69f82002-06-05 20:27:12 +00001212 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001213
Neil Boothcbc69f82002-06-05 20:27:12 +00001214 /* Check the replacement text or tokens. */
1215 if (CPP_OPTION (pfile, traditional))
Neil Booth6618c5d2002-06-10 06:03:13 +00001216 return _cpp_expansions_different_trad (macro1, macro2);
Neil Boothcbc69f82002-06-05 20:27:12 +00001217
DJ Deloriea7f36da2003-06-01 14:55:15 -04001218 if (macro1->count != macro2->count)
1219 return true;
1220
1221 for (i = 0; i < macro1->count; i++)
1222 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1223 return true;
Neil Boothcbc69f82002-06-05 20:27:12 +00001224
1225 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001226}
1227
Neil Booth93c803682000-10-28 17:59:06 +00001228/* Free the definition of hashnode H. */
Neil Booth93c803682000-10-28 17:59:06 +00001229void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001230_cpp_free_definition (cpp_hashnode *h)
Zack Weinberg711b8822000-07-18 00:59:49 +00001231{
Neil Booth93c803682000-10-28 17:59:06 +00001232 /* Macros and assertions no longer have anything to free. */
1233 h->type = NT_VOID;
1234 /* Clear builtin flag in case of redefinition. */
Neil Booth644edda2001-10-02 12:57:24 +00001235 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
Neil Booth93c803682000-10-28 17:59:06 +00001236}
Zack Weinberg711b8822000-07-18 00:59:49 +00001237
Neil Booth14baae02001-09-17 18:26:12 +00001238/* Save parameter NODE to the parameter list of macro MACRO. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001239 zero on success, nonzero if the parameter is a duplicate. */
Neil Boothc70f6ed2002-06-07 06:26:32 +00001240bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001241_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00001242{
Zack Weinberg4977bab2002-12-16 18:23:00 +00001243 unsigned int len;
Neil Booth93c803682000-10-28 17:59:06 +00001244 /* Constraint 6.10.3.6 - duplicate parameter names. */
Zack Weinberg4977bab2002-12-16 18:23:00 +00001245 if (node->flags & NODE_MACRO_ARG)
Zack Weinberg711b8822000-07-18 00:59:49 +00001246 {
John David Anglin0527bc42003-11-01 22:56:54 +00001247 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001248 NODE_NAME (node));
Neil Booth23ff0222002-07-17 17:27:14 +00001249 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001250 }
1251
Neil Booth8c3b2692001-09-30 10:03:11 +00001252 if (BUFF_ROOM (pfile->a_buff)
1253 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1254 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +00001255
Neil Booth8c3b2692001-09-30 10:03:11 +00001256 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
Zack Weinberg4977bab2002-12-16 18:23:00 +00001257 node->flags |= NODE_MACRO_ARG;
1258 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1259 if (len > pfile->macro_buffer_len)
1260 {
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001261 pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
Zack Weinberg4977bab2002-12-16 18:23:00 +00001262 pfile->macro_buffer_len = len;
1263 }
1264 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1265 = node->value;
1266
1267 node->value.arg_index = macro->paramc;
Neil Booth23ff0222002-07-17 17:27:14 +00001268 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001269}
1270
Neil Booth23ff0222002-07-17 17:27:14 +00001271/* Check the syntax of the parameters in a MACRO definition. Returns
1272 false if an error occurs. */
1273static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001274parse_params (cpp_reader *pfile, cpp_macro *macro)
Neil Booth93c803682000-10-28 17:59:06 +00001275{
Neil Booth93c803682000-10-28 17:59:06 +00001276 unsigned int prev_ident = 0;
1277
Neil Booth93c803682000-10-28 17:59:06 +00001278 for (;;)
1279 {
Neil Booth345894b2001-09-16 13:44:29 +00001280 const cpp_token *token = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001281
Neil Booth345894b2001-09-16 13:44:29 +00001282 switch (token->type)
Zack Weinberg711b8822000-07-18 00:59:49 +00001283 {
1284 default:
Jason Thorpe477cdac2002-04-07 03:12:23 +00001285 /* Allow/ignore comments in parameter lists if we are
1286 preserving comments in macro expansions. */
1287 if (token->type == CPP_COMMENT
1288 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1289 continue;
1290
John David Anglin0527bc42003-11-01 22:56:54 +00001291 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001292 "\"%s\" may not appear in macro parameter list",
Neil Booth345894b2001-09-16 13:44:29 +00001293 cpp_token_as_text (pfile, token));
Neil Booth23ff0222002-07-17 17:27:14 +00001294 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001295
Zack Weinberg711b8822000-07-18 00:59:49 +00001296 case CPP_NAME:
1297 if (prev_ident)
1298 {
John David Anglin0527bc42003-11-01 22:56:54 +00001299 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001300 "macro parameters must be comma-separated");
Neil Booth23ff0222002-07-17 17:27:14 +00001301 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001302 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001303 prev_ident = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001304
Neil Boothc70f6ed2002-06-07 06:26:32 +00001305 if (_cpp_save_parameter (pfile, macro, token->val.node))
Neil Booth23ff0222002-07-17 17:27:14 +00001306 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001307 continue;
1308
1309 case CPP_CLOSE_PAREN:
Neil Booth93c803682000-10-28 17:59:06 +00001310 if (prev_ident || macro->paramc == 0)
Neil Booth23ff0222002-07-17 17:27:14 +00001311 return true;
Zack Weinberg711b8822000-07-18 00:59:49 +00001312
1313 /* Fall through to pick up the error. */
1314 case CPP_COMMA:
1315 if (!prev_ident)
1316 {
John David Anglin0527bc42003-11-01 22:56:54 +00001317 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
Neil Booth23ff0222002-07-17 17:27:14 +00001318 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001319 }
1320 prev_ident = 0;
1321 continue;
1322
1323 case CPP_ELLIPSIS:
Neil Booth28e0f042000-12-09 12:06:37 +00001324 macro->variadic = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001325 if (!prev_ident)
1326 {
Neil Boothc70f6ed2002-06-07 06:26:32 +00001327 _cpp_save_parameter (pfile, macro,
1328 pfile->spec_nodes.n__VA_ARGS__);
Neil Booth93c803682000-10-28 17:59:06 +00001329 pfile->state.va_args_ok = 1;
Richard Hendersone5b79212004-02-19 14:18:50 -08001330 if (! CPP_OPTION (pfile, c99)
1331 && CPP_OPTION (pfile, pedantic)
1332 && CPP_OPTION (pfile, warn_variadic_macros))
John David Anglin0527bc42003-11-01 22:56:54 +00001333 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +00001334 "anonymous variadic macros were introduced in C99");
Zack Weinberg711b8822000-07-18 00:59:49 +00001335 }
Richard Hendersone5b79212004-02-19 14:18:50 -08001336 else if (CPP_OPTION (pfile, pedantic)
1337 && CPP_OPTION (pfile, warn_variadic_macros))
John David Anglin0527bc42003-11-01 22:56:54 +00001338 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +00001339 "ISO C does not permit named variadic macros");
Zack Weinberg711b8822000-07-18 00:59:49 +00001340
Neil Booth93c803682000-10-28 17:59:06 +00001341 /* We're at the end, and just expect a closing parenthesis. */
Neil Booth345894b2001-09-16 13:44:29 +00001342 token = _cpp_lex_token (pfile);
1343 if (token->type == CPP_CLOSE_PAREN)
Neil Booth23ff0222002-07-17 17:27:14 +00001344 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001345 /* Fall through. */
1346
1347 case CPP_EOF:
John David Anglin0527bc42003-11-01 22:56:54 +00001348 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
Neil Booth23ff0222002-07-17 17:27:14 +00001349 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001350 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001351 }
1352}
1353
Neil Booth14baae02001-09-17 18:26:12 +00001354/* Allocate room for a token from a macro's replacement list. */
Neil Booth93c803682000-10-28 17:59:06 +00001355static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001356alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001357{
Neil Booth8c3b2692001-09-30 10:03:11 +00001358 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1359 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
Zack Weinberg711b8822000-07-18 00:59:49 +00001360
Neil Booth8c3b2692001-09-30 10:03:11 +00001361 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
Neil Booth14baae02001-09-17 18:26:12 +00001362}
1363
Neil Boothd15a58c2002-01-03 18:32:55 +00001364/* Lex a token from the expansion of MACRO, but mark parameters as we
1365 find them and warn of traditional stringification. */
Neil Booth14baae02001-09-17 18:26:12 +00001366static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001367lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Neil Booth14baae02001-09-17 18:26:12 +00001368{
1369 cpp_token *token;
1370
1371 pfile->cur_token = alloc_expansion_token (pfile, macro);
1372 token = _cpp_lex_direct (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001373
Neil Boothd15a58c2002-01-03 18:32:55 +00001374 /* Is this a parameter? */
Zack Weinberg4977bab2002-12-16 18:23:00 +00001375 if (token->type == CPP_NAME
1376 && (token->val.node->flags & NODE_MACRO_ARG) != 0)
Zack Weinberg711b8822000-07-18 00:59:49 +00001377 {
Neil Booth93c803682000-10-28 17:59:06 +00001378 token->type = CPP_MACRO_ARG;
Zack Weinberg4977bab2002-12-16 18:23:00 +00001379 token->val.arg_no = token->val.node->value.arg_index;
Zack Weinberg711b8822000-07-18 00:59:49 +00001380 }
Neil Booth93c803682000-10-28 17:59:06 +00001381 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1382 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1383 check_trad_stringification (pfile, macro, &token->val.str);
Zack Weinberg711b8822000-07-18 00:59:49 +00001384
Neil Booth93c803682000-10-28 17:59:06 +00001385 return token;
Zack Weinberg711b8822000-07-18 00:59:49 +00001386}
1387
Neil Boothcbc69f82002-06-05 20:27:12 +00001388static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001389create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001390{
Neil Boothcbc69f82002-06-05 20:27:12 +00001391 cpp_token *token;
Neil Booth14baae02001-09-17 18:26:12 +00001392 const cpp_token *ctoken;
Zack Weinberg711b8822000-07-18 00:59:49 +00001393
Neil Booth93c803682000-10-28 17:59:06 +00001394 /* Get the first token of the expansion (or the '(' of a
1395 function-like macro). */
Neil Booth14baae02001-09-17 18:26:12 +00001396 ctoken = _cpp_lex_token (pfile);
1397
1398 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
Zack Weinberg711b8822000-07-18 00:59:49 +00001399 {
Neil Boothcbc69f82002-06-05 20:27:12 +00001400 bool ok = parse_params (pfile, macro);
Neil Booth8c3b2692001-09-30 10:03:11 +00001401 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1402 if (!ok)
Neil Boothcbc69f82002-06-05 20:27:12 +00001403 return false;
Neil Booth8c3b2692001-09-30 10:03:11 +00001404
1405 /* Success. Commit the parameter array. */
Neil Booth562a5c22002-04-21 18:46:42 +00001406 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
Neil Booth93c803682000-10-28 17:59:06 +00001407 macro->fun_like = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001408 }
Neil Booth14baae02001-09-17 18:26:12 +00001409 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
John David Anglin0527bc42003-11-01 22:56:54 +00001410 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +00001411 "ISO C requires whitespace after the macro name");
Neil Booth93c803682000-10-28 17:59:06 +00001412
Neil Booth14baae02001-09-17 18:26:12 +00001413 if (macro->fun_like)
1414 token = lex_expansion_token (pfile, macro);
1415 else
1416 {
1417 token = alloc_expansion_token (pfile, macro);
1418 *token = *ctoken;
1419 }
Neil Booth93c803682000-10-28 17:59:06 +00001420
1421 for (;;)
1422 {
1423 /* Check the stringifying # constraint 6.10.3.2.1 of
1424 function-like macros when lexing the subsequent token. */
1425 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
Zack Weinberg711b8822000-07-18 00:59:49 +00001426 {
Neil Booth93c803682000-10-28 17:59:06 +00001427 if (token->type == CPP_MACRO_ARG)
1428 {
1429 token->flags &= ~PREV_WHITE;
1430 token->flags |= STRINGIFY_ARG;
1431 token->flags |= token[-1].flags & PREV_WHITE;
1432 token[-1] = token[0];
1433 macro->count--;
1434 }
1435 /* Let assembler get away with murder. */
Neil Boothbdb05a72000-11-26 17:31:13 +00001436 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +00001437 {
John David Anglin0527bc42003-11-01 22:56:54 +00001438 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001439 "'#' is not followed by a macro parameter");
Neil Boothcbc69f82002-06-05 20:27:12 +00001440 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001441 }
1442 }
1443
1444 if (token->type == CPP_EOF)
1445 break;
1446
1447 /* Paste operator constraint 6.10.3.3.1. */
1448 if (token->type == CPP_PASTE)
1449 {
1450 /* Token-paste ##, can appear in both object-like and
1451 function-like macros, but not at the ends. */
1452 if (--macro->count > 0)
1453 token = lex_expansion_token (pfile, macro);
1454
1455 if (macro->count == 0 || token->type == CPP_EOF)
1456 {
John David Anglin0527bc42003-11-01 22:56:54 +00001457 cpp_error (pfile, CPP_DL_ERROR,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001458 "'##' cannot appear at either end of a macro expansion");
Neil Boothcbc69f82002-06-05 20:27:12 +00001459 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001460 }
1461
1462 token[-1].flags |= PASTE_LEFT;
Neil Booth93c803682000-10-28 17:59:06 +00001463 }
1464
1465 token = lex_expansion_token (pfile, macro);
1466 }
1467
Neil Booth601328b2002-05-16 05:53:24 +00001468 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +00001469
Neil Booth4c2b6472000-11-11 13:19:01 +00001470 /* Don't count the CPP_EOF. */
1471 macro->count--;
Neil Booth93c803682000-10-28 17:59:06 +00001472
Neil Boothd15a58c2002-01-03 18:32:55 +00001473 /* Clear whitespace on first token for warn_of_redefinition(). */
Neil Booth8c3b2692001-09-30 10:03:11 +00001474 if (macro->count)
Neil Booth601328b2002-05-16 05:53:24 +00001475 macro->exp.tokens[0].flags &= ~PREV_WHITE;
Neil Booth8c3b2692001-09-30 10:03:11 +00001476
1477 /* Commit the memory. */
Neil Booth601328b2002-05-16 05:53:24 +00001478 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
Neil Booth8c3b2692001-09-30 10:03:11 +00001479
Neil Boothcbc69f82002-06-05 20:27:12 +00001480 return true;
1481}
Neil Booth44ed91a2000-10-29 11:37:18 +00001482
Kazu Hiratada7d8302002-09-22 02:03:17 +00001483/* Parse a macro and save its expansion. Returns nonzero on success. */
Neil Boothcbc69f82002-06-05 20:27:12 +00001484bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001485_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
Neil Boothcbc69f82002-06-05 20:27:12 +00001486{
1487 cpp_macro *macro;
1488 unsigned int i;
1489 bool ok;
1490
1491 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1492 macro->line = pfile->directive_line;
1493 macro->params = 0;
1494 macro->paramc = 0;
1495 macro->variadic = 0;
Neil Booth23345bb2003-03-14 21:47:50 +00001496 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
Neil Boothcbc69f82002-06-05 20:27:12 +00001497 macro->count = 0;
1498 macro->fun_like = 0;
Neil Booth7065e132001-02-14 07:38:20 +00001499 /* To suppress some diagnostics. */
Per Bothner12f9df42004-02-11 07:29:30 -08001500 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
Neil Booth7065e132001-02-14 07:38:20 +00001501
Neil Boothcbc69f82002-06-05 20:27:12 +00001502 if (CPP_OPTION (pfile, traditional))
1503 ok = _cpp_create_trad_definition (pfile, macro);
1504 else
1505 {
1506 cpp_token *saved_cur_token = pfile->cur_token;
1507
1508 ok = create_iso_definition (pfile, macro);
1509
1510 /* Restore lexer position because of games lex_expansion_token()
1511 plays lexing the macro. We set the type for SEEN_EOL() in
1512 cpplib.c.
1513
1514 Longer term we should lex the whole line before coming here,
1515 and just copy the expansion. */
1516 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1517 pfile->cur_token = saved_cur_token;
1518
1519 /* Stop the lexer accepting __VA_ARGS__. */
1520 pfile->state.va_args_ok = 0;
1521 }
1522
1523 /* Clear the fast argument lookup indices. */
1524 for (i = macro->paramc; i-- > 0; )
Zack Weinberg4977bab2002-12-16 18:23:00 +00001525 {
1526 struct cpp_hashnode *node = macro->params[i];
1527 node->flags &= ~ NODE_MACRO_ARG;
1528 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1529 }
Neil Boothcbc69f82002-06-05 20:27:12 +00001530
1531 if (!ok)
1532 return ok;
1533
Neil Boothc2734e02002-07-26 16:29:31 +00001534 if (node->type == NT_MACRO)
Neil Booth93c803682000-10-28 17:59:06 +00001535 {
Neil Bootha69cbaa2002-07-23 22:57:49 +00001536 if (CPP_OPTION (pfile, warn_unused_macros))
1537 _cpp_warn_if_unused_macro (pfile, node, NULL);
1538
Neil Boothcbc69f82002-06-05 20:27:12 +00001539 if (warn_of_redefinition (pfile, node, macro))
Neil Booth93c803682000-10-28 17:59:06 +00001540 {
John David Anglin0527bc42003-11-01 22:56:54 +00001541 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
Neil Boothebef4e82002-04-14 18:42:47 +00001542 "\"%s\" redefined", NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +00001543
Neil Booth618cdda2001-02-25 09:43:03 +00001544 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
John David Anglin0527bc42003-11-01 22:56:54 +00001545 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
Neil Boothcbc69f82002-06-05 20:27:12 +00001546 node->value.macro->line, 0,
1547 "this is the location of the previous definition");
Zack Weinberg711b8822000-07-18 00:59:49 +00001548 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001549 }
1550
Neil Boothc2734e02002-07-26 16:29:31 +00001551 if (node->type != NT_VOID)
1552 _cpp_free_definition (node);
1553
Zack Weinberg711b8822000-07-18 00:59:49 +00001554 /* Enter definition in hash table. */
Neil Booth93c803682000-10-28 17:59:06 +00001555 node->type = NT_MACRO;
1556 node->value.macro = macro;
Neil Bootha28c50352001-05-16 22:02:09 +00001557 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
Neil Booth618cdda2001-02-25 09:43:03 +00001558 node->flags |= NODE_WARN;
Zack Weinberg711b8822000-07-18 00:59:49 +00001559
Neil Booth93c803682000-10-28 17:59:06 +00001560 return ok;
Zack Weinberg711b8822000-07-18 00:59:49 +00001561}
1562
Neil Boothd15a58c2002-01-03 18:32:55 +00001563/* Warn if a token in STRING matches one of a function-like MACRO's
1564 parameters. */
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001565static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001566check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1567 const cpp_string *string)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001568{
Neil Booth93c803682000-10-28 17:59:06 +00001569 unsigned int i, len;
Neil Booth6338b352003-04-23 22:44:06 +00001570 const uchar *p, *q, *limit;
Kazu Hiratadf383482002-05-22 22:02:16 +00001571
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001572 /* Loop over the string. */
Neil Booth6338b352003-04-23 22:44:06 +00001573 limit = string->text + string->len - 1;
1574 for (p = string->text + 1; p < limit; p = q)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001575 {
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001576 /* Find the start of an identifier. */
Greg McGary61c16b12000-09-15 21:25:02 +00001577 while (p < limit && !is_idstart (*p))
1578 p++;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001579
1580 /* Find the end of the identifier. */
1581 q = p;
Greg McGary61c16b12000-09-15 21:25:02 +00001582 while (q < limit && is_idchar (*q))
1583 q++;
Neil Booth93c803682000-10-28 17:59:06 +00001584
1585 len = q - p;
1586
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001587 /* Loop over the function macro arguments to see if the
1588 identifier inside the string matches one of them. */
Neil Booth93c803682000-10-28 17:59:06 +00001589 for (i = 0; i < macro->paramc; i++)
1590 {
1591 const cpp_hashnode *node = macro->params[i];
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001592
Neil Booth2a967f32001-05-20 06:26:45 +00001593 if (NODE_LEN (node) == len
1594 && !memcmp (p, NODE_NAME (node), len))
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001595 {
John David Anglin0527bc42003-11-01 22:56:54 +00001596 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinbergf458d1d2002-02-27 18:48:07 +00001597 "macro argument \"%s\" would be stringified in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +00001598 NODE_NAME (node));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001599 break;
1600 }
1601 }
1602 }
1603}
Neil Booth93c803682000-10-28 17:59:06 +00001604
Neil Booth70961712001-06-23 11:34:41 +00001605/* Returns the name, arguments and expansion of a macro, in a format
1606 suitable to be read back in again, and therefore also for DWARF 2
1607 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1608 Caller is expected to generate the "#define" bit if needed. The
Neil Booth93c803682000-10-28 17:59:06 +00001609 returned text is temporary, and automatically freed later. */
Neil Booth93c803682000-10-28 17:59:06 +00001610const unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001611cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00001612{
1613 unsigned int i, len;
1614 const cpp_macro *macro = node->value.macro;
1615 unsigned char *buffer;
1616
1617 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1618 {
John David Anglin0527bc42003-11-01 22:56:54 +00001619 cpp_error (pfile, CPP_DL_ICE,
Neil Boothebef4e82002-04-14 18:42:47 +00001620 "invalid hash type %d in cpp_macro_definition", node->type);
Neil Booth93c803682000-10-28 17:59:06 +00001621 return 0;
1622 }
1623
1624 /* Calculate length. */
Neil Booth8dc901d2002-05-29 19:30:07 +00001625 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
Neil Booth93c803682000-10-28 17:59:06 +00001626 if (macro->fun_like)
1627 {
Jim Blandy64d08262002-04-05 00:12:40 +00001628 len += 4; /* "()" plus possible final ".." of named
1629 varargs (we have + 1 below). */
Neil Booth93c803682000-10-28 17:59:06 +00001630 for (i = 0; i < macro->paramc; i++)
Jim Blandy64d08262002-04-05 00:12:40 +00001631 len += NODE_LEN (macro->params[i]) + 1; /* "," */
Neil Booth93c803682000-10-28 17:59:06 +00001632 }
1633
Neil Booth278c4662002-06-19 05:40:08 +00001634 if (CPP_OPTION (pfile, traditional))
1635 len += _cpp_replacement_text_len (macro);
1636 else
Neil Booth93c803682000-10-28 17:59:06 +00001637 {
Neil Booth278c4662002-06-19 05:40:08 +00001638 for (i = 0; i < macro->count; i++)
1639 {
1640 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00001641
Neil Booth278c4662002-06-19 05:40:08 +00001642 if (token->type == CPP_MACRO_ARG)
1643 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1644 else
Neil Booth59325652003-04-24 20:03:57 +00001645 len += cpp_token_len (token) + 1; /* Includes room for ' '. */
Neil Booth278c4662002-06-19 05:40:08 +00001646 if (token->flags & STRINGIFY_ARG)
1647 len++; /* "#" */
1648 if (token->flags & PASTE_LEFT)
1649 len += 3; /* " ##" */
1650 }
Neil Booth93c803682000-10-28 17:59:06 +00001651 }
1652
1653 if (len > pfile->macro_buffer_len)
Alexandre Oliva4b49c362001-01-09 09:30:43 +00001654 {
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001655 pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
Alexandre Oliva4b49c362001-01-09 09:30:43 +00001656 pfile->macro_buffer_len = len;
1657 }
Neil Booth70961712001-06-23 11:34:41 +00001658
1659 /* Fill in the buffer. Start with the macro name. */
Neil Booth93c803682000-10-28 17:59:06 +00001660 buffer = pfile->macro_buffer;
Neil Booth70961712001-06-23 11:34:41 +00001661 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1662 buffer += NODE_LEN (node);
Neil Booth93c803682000-10-28 17:59:06 +00001663
1664 /* Parameter names. */
1665 if (macro->fun_like)
1666 {
1667 *buffer++ = '(';
1668 for (i = 0; i < macro->paramc; i++)
1669 {
1670 cpp_hashnode *param = macro->params[i];
1671
1672 if (param != pfile->spec_nodes.n__VA_ARGS__)
1673 {
Neil Bootha28c50352001-05-16 22:02:09 +00001674 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1675 buffer += NODE_LEN (param);
Neil Booth93c803682000-10-28 17:59:06 +00001676 }
1677
1678 if (i + 1 < macro->paramc)
Kazu Hiratadf383482002-05-22 22:02:16 +00001679 /* Don't emit a space after the comma here; we're trying
1680 to emit a Dwarf-friendly definition, and the Dwarf spec
1681 forbids spaces in the argument list. */
Jim Blandy64d08262002-04-05 00:12:40 +00001682 *buffer++ = ',';
Neil Booth28e0f042000-12-09 12:06:37 +00001683 else if (macro->variadic)
Neil Booth93c803682000-10-28 17:59:06 +00001684 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1685 }
1686 *buffer++ = ')';
1687 }
1688
Jim Blandye37b38d2002-03-19 21:43:39 +00001689 /* The Dwarf spec requires a space after the macro name, even if the
1690 definition is the empty string. */
1691 *buffer++ = ' ';
1692
Neil Booth278c4662002-06-19 05:40:08 +00001693 if (CPP_OPTION (pfile, traditional))
1694 buffer = _cpp_copy_replacement_text (macro, buffer);
1695 else if (macro->count)
Neil Booth93c803682000-10-28 17:59:06 +00001696 /* Expansion tokens. */
Neil Booth93c803682000-10-28 17:59:06 +00001697 {
Neil Booth93c803682000-10-28 17:59:06 +00001698 for (i = 0; i < macro->count; i++)
1699 {
Neil Booth601328b2002-05-16 05:53:24 +00001700 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00001701
1702 if (token->flags & PREV_WHITE)
1703 *buffer++ = ' ';
1704 if (token->flags & STRINGIFY_ARG)
1705 *buffer++ = '#';
1706
1707 if (token->type == CPP_MACRO_ARG)
1708 {
Neil Bootha28c50352001-05-16 22:02:09 +00001709 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1710 memcpy (buffer,
1711 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
Neil Booth93c803682000-10-28 17:59:06 +00001712 buffer += len;
1713 }
1714 else
1715 buffer = cpp_spell_token (pfile, token, buffer);
1716
1717 if (token->flags & PASTE_LEFT)
1718 {
1719 *buffer++ = ' ';
1720 *buffer++ = '#';
1721 *buffer++ = '#';
1722 /* Next has PREV_WHITE; see _cpp_create_definition. */
1723 }
1724 }
1725 }
1726
1727 *buffer = '\0';
1728 return pfile->macro_buffer;
1729}