blob: daa2bd34a0e9ce0643ca02af01aa1343cfd30f5c [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 Hirata31c3e632005-02-14 14:43:56 +00003 1999, 2000, 2001, 2002, 2003, 2004, 2005 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"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000029#include "internal.h"
Zack Weinberg711b8822000-07-18 00:59:49 +000030
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
Zack Weinberg21b11492004-09-09 19:16:56 +0000112/* Helper function for builtin_macro. Returns the text generated by
113 a builtin macro. */
Neil Booth278c4662002-06-19 05:40:08 +0000114const uchar *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000115_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000116{
Per Bothner12f9df42004-02-11 07:29:30 -0800117 const struct line_map *map;
Neil Booth278c4662002-06-19 05:40:08 +0000118 const uchar *result = NULL;
119 unsigned int number = 1;
Neil Booth644edda2001-10-02 12:57:24 +0000120
Neil Booth93c803682000-10-28 17:59:06 +0000121 switch (node->value.builtin)
122 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000123 default:
John David Anglin0527bc42003-11-01 22:56:54 +0000124 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +0000125 NODE_NAME (node));
Neil Booth278c4662002-06-19 05:40:08 +0000126 break;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000127
Neil Booth93c803682000-10-28 17:59:06 +0000128 case BT_FILE:
129 case BT_BASE_FILE:
Zack Weinberg711b8822000-07-18 00:59:49 +0000130 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000131 unsigned int len;
Neil Booth0bda4762000-12-11 07:45:16 +0000132 const char *name;
Neil Booth562a5c22002-04-21 18:46:42 +0000133 uchar *buf;
Per Bothner500bee02004-04-22 19:22:27 -0700134 map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
Neil Booth93c803682000-10-28 17:59:06 +0000135
Neil Booth0bda4762000-12-11 07:45:16 +0000136 if (node->value.builtin == BT_BASE_FILE)
Neil Boothbb74c962001-08-17 22:23:49 +0000137 while (! MAIN_FILE_P (map))
Per Bothner50f59cd2004-01-19 21:30:18 -0800138 map = INCLUDED_FROM (pfile->line_table, map);
Neil Booth93c803682000-10-28 17:59:06 +0000139
Neil Boothbb74c962001-08-17 22:23:49 +0000140 name = map->to_file;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000141 len = strlen (name);
Neil Booth278c4662002-06-19 05:40:08 +0000142 buf = _cpp_unaligned_alloc (pfile, len * 4 + 3);
143 result = buf;
144 *buf = '"';
145 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
146 *buf++ = '"';
147 *buf = '\0';
Zack Weinberg711b8822000-07-18 00:59:49 +0000148 }
Neil Booth644edda2001-10-02 12:57:24 +0000149 break;
150
Neil Booth93c803682000-10-28 17:59:06 +0000151 case BT_INCLUDE_LEVEL:
Neil Boothd8693c62001-08-21 23:05:12 +0000152 /* The line map depth counts the primary source as level 1, but
153 historically __INCLUDE_DEPTH__ has called the primary source
154 level 0. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800155 number = pfile->line_table->depth - 1;
Neil Booth644edda2001-10-02 12:57:24 +0000156 break;
Neil Booth93c803682000-10-28 17:59:06 +0000157
158 case BT_SPECLINE:
Per Bothner500bee02004-04-22 19:22:27 -0700159 map = &pfile->line_table->maps[pfile->line_table->used-1];
Neil Booth93c803682000-10-28 17:59:06 +0000160 /* If __LINE__ is embedded in a macro, it must expand to the
161 line of the macro's invocation, not its definition.
162 Otherwise things like assert() will not work properly. */
Neil Booth278c4662002-06-19 05:40:08 +0000163 if (CPP_OPTION (pfile, traditional))
Per Bothner500bee02004-04-22 19:22:27 -0700164 number = pfile->line_table->highest_line;
Neil Booth278c4662002-06-19 05:40:08 +0000165 else
Per Bothner12f9df42004-02-11 07:29:30 -0800166 number = pfile->cur_token[-1].src_loc;
167 number = SOURCE_LINE (map, number);
Neil Booth644edda2001-10-02 12:57:24 +0000168 break;
Neil Booth93c803682000-10-28 17:59:06 +0000169
Zack Weinberg5279d732002-05-16 19:03:02 +0000170 /* __STDC__ has the value 1 under normal circumstances.
171 However, if (a) we are in a system header, (b) the option
Zack Weinberg2a1dc0d2002-05-21 21:55:37 +0000172 stdc_0_in_system_headers is true (set by target config), and
173 (c) we are not in strictly conforming mode, then it has the
174 value 0. */
Neil Booth93c803682000-10-28 17:59:06 +0000175 case BT_STDC:
176 {
Per Bothner12f9df42004-02-11 07:29:30 -0800177 if (cpp_in_system_header (pfile)
Zack Weinberg5279d732002-05-16 19:03:02 +0000178 && CPP_OPTION (pfile, stdc_0_in_system_headers)
Neil Booth58551c22002-08-06 20:35:46 +0000179 && !CPP_OPTION (pfile,std))
Neil Booth278c4662002-06-19 05:40:08 +0000180 number = 0;
Zack Weinberg5279d732002-05-16 19:03:02 +0000181 else
Neil Booth278c4662002-06-19 05:40:08 +0000182 number = 1;
Neil Booth93c803682000-10-28 17:59:06 +0000183 }
Neil Booth644edda2001-10-02 12:57:24 +0000184 break;
Neil Booth93c803682000-10-28 17:59:06 +0000185
186 case BT_DATE:
187 case BT_TIME:
Neil Booth278c4662002-06-19 05:40:08 +0000188 if (pfile->date == NULL)
Neil Booth93c803682000-10-28 17:59:06 +0000189 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000190 /* Allocate __DATE__ and __TIME__ strings from permanent
191 storage. We only do this once, and don't generate them
192 at init time, because time() and localtime() are very
193 slow on some systems. */
Zack Weinberg56da7202002-08-02 04:18:16 +0000194 time_t tt;
195 struct tm *tb = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000196
Zack Weinberg56da7202002-08-02 04:18:16 +0000197 /* (time_t) -1 is a legitimate value for "number of seconds
198 since the Epoch", so we have to do a little dance to
199 distinguish that from a genuine error. */
200 errno = 0;
201 tt = time(NULL);
202 if (tt != (time_t)-1 || errno == 0)
203 tb = localtime (&tt);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000204
Zack Weinberg56da7202002-08-02 04:18:16 +0000205 if (tb)
206 {
207 pfile->date = _cpp_unaligned_alloc (pfile,
208 sizeof ("\"Oct 11 1347\""));
209 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000210 monthnames[tb->tm_mon], tb->tm_mday,
211 tb->tm_year + 1900);
Zack Weinberg56da7202002-08-02 04:18:16 +0000212
213 pfile->time = _cpp_unaligned_alloc (pfile,
214 sizeof ("\"12:34:56\""));
215 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
216 tb->tm_hour, tb->tm_min, tb->tm_sec);
217 }
218 else
219 {
John David Anglin0527bc42003-11-01 22:56:54 +0000220 cpp_errno (pfile, CPP_DL_WARNING,
Zack Weinberg56da7202002-08-02 04:18:16 +0000221 "could not determine date and time");
222
223 pfile->date = U"\"??? ?? ????\"";
224 pfile->time = U"\"??:??:??\"";
225 }
Neil Booth93c803682000-10-28 17:59:06 +0000226 }
Neil Booth93c803682000-10-28 17:59:06 +0000227
Neil Booth644edda2001-10-02 12:57:24 +0000228 if (node->value.builtin == BT_DATE)
Neil Booth278c4662002-06-19 05:40:08 +0000229 result = pfile->date;
Neil Booth644edda2001-10-02 12:57:24 +0000230 else
Neil Booth278c4662002-06-19 05:40:08 +0000231 result = pfile->time;
Neil Booth644edda2001-10-02 12:57:24 +0000232 break;
Neil Booth278c4662002-06-19 05:40:08 +0000233 }
Neil Booth644edda2001-10-02 12:57:24 +0000234
Neil Booth278c4662002-06-19 05:40:08 +0000235 if (result == NULL)
236 {
237 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
238 result = _cpp_unaligned_alloc (pfile, 21);
239 sprintf ((char *) result, "%u", number);
240 }
241
242 return result;
243}
244
245/* Convert builtin macros like __FILE__ to a token and push it on the
Zack Weinberg21b11492004-09-09 19:16:56 +0000246 context stack. Also handles _Pragma, for which a new token may not
247 be created. Returns 1 if it generates a new token context, 0 to
Neil Booth278c4662002-06-19 05:40:08 +0000248 return the token to the caller. */
249static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000250builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth278c4662002-06-19 05:40:08 +0000251{
252 const uchar *buf;
Neil Booth26aea072003-04-19 00:22:51 +0000253 size_t len;
254 char *nbuf;
Neil Booth278c4662002-06-19 05:40:08 +0000255
256 if (node->value.builtin == BT_PRAGMA)
257 {
Neil Booth644edda2001-10-02 12:57:24 +0000258 /* Don't interpret _Pragma within directives. The standard is
259 not clear on this, but to me this makes most sense. */
260 if (pfile->state.in_directive)
261 return 0;
262
263 _cpp_do__Pragma (pfile);
Zack Weinberg21b11492004-09-09 19:16:56 +0000264 if (pfile->directive_result.type == CPP_PRAGMA)
265 {
266 cpp_token *tok = _cpp_temp_token (pfile);
267 *tok = pfile->directive_result;
268 push_token_context (pfile, NULL, tok, 1);
269 }
270
Neil Booth644edda2001-10-02 12:57:24 +0000271 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000272 }
Neil Booth644edda2001-10-02 12:57:24 +0000273
Neil Booth278c4662002-06-19 05:40:08 +0000274 buf = _cpp_builtin_macro_text (pfile, node);
Neil Booth26aea072003-04-19 00:22:51 +0000275 len = ustrlen (buf);
276 nbuf = alloca (len + 1);
277 memcpy (nbuf, buf, len);
278 nbuf[len]='\n';
Neil Booth278c4662002-06-19 05:40:08 +0000279
Per Bothner40de9f72003-10-02 07:30:34 +0000280 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
Neil Booth26aea072003-04-19 00:22:51 +0000281 _cpp_clean_line (pfile);
Neil Booth278c4662002-06-19 05:40:08 +0000282
283 /* Set pfile->cur_token as required by _cpp_lex_direct. */
284 pfile->cur_token = _cpp_temp_token (pfile);
285 push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
286 if (pfile->buffer->cur != pfile->buffer->rlimit)
John David Anglin0527bc42003-11-01 22:56:54 +0000287 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
Neil Booth278c4662002-06-19 05:40:08 +0000288 NODE_NAME (node));
289 _cpp_pop_buffer (pfile);
290
Neil Booth644edda2001-10-02 12:57:24 +0000291 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000292}
293
Neil Boothd15a58c2002-01-03 18:32:55 +0000294/* Copies SRC, of length LEN, to DEST, adding backslashes before all
295 backslashes and double quotes. Non-printable characters are
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000296 converted to octal. DEST must be of sufficient size. Returns
297 a pointer to the end of the string. */
Neil Booth562a5c22002-04-21 18:46:42 +0000298uchar *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000299cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
Neil Booth93c803682000-10-28 17:59:06 +0000300{
301 while (len--)
302 {
Neil Booth562a5c22002-04-21 18:46:42 +0000303 uchar c = *src++;
Neil Booth93c803682000-10-28 17:59:06 +0000304
305 if (c == '\\' || c == '"')
306 {
307 *dest++ = '\\';
308 *dest++ = c;
309 }
310 else
311 {
312 if (ISPRINT (c))
313 *dest++ = c;
314 else
315 {
316 sprintf ((char *) dest, "\\%03o", c);
317 dest += 4;
318 }
319 }
320 }
321
322 return dest;
323}
324
Neil Boothd15a58c2002-01-03 18:32:55 +0000325/* Convert a token sequence ARG to a single string token according to
326 the rules of the ISO C #-operator. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000327static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000328stringify_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +0000329{
Neil Booth6338b352003-04-23 22:44:06 +0000330 unsigned char *dest;
Neil Boothece54d52001-09-28 09:40:22 +0000331 unsigned int i, escape_it, backslash_count = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000332 const cpp_token *source = NULL;
Neil Boothece54d52001-09-28 09:40:22 +0000333 size_t len;
Neil Booth93c803682000-10-28 17:59:06 +0000334
Neil Booth6338b352003-04-23 22:44:06 +0000335 if (BUFF_ROOM (pfile->u_buff) < 3)
336 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
337 dest = BUFF_FRONT (pfile->u_buff);
338 *dest++ = '"';
339
Neil Booth93c803682000-10-28 17:59:06 +0000340 /* Loop, reading in the argument's tokens. */
341 for (i = 0; i < arg->count; i++)
342 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000343 const cpp_token *token = arg->first[i];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000344
345 if (token->type == CPP_PADDING)
346 {
347 if (source == NULL)
348 source = token->val.source;
349 continue;
350 }
Neil Booth93c803682000-10-28 17:59:06 +0000351
352 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
Zack Weinbergcc937582001-03-07 01:32:01 +0000353 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
Neil Booth93c803682000-10-28 17:59:06 +0000354
Neil Boothece54d52001-09-28 09:40:22 +0000355 /* Room for each char being written in octal, initial space and
Neil Booth6338b352003-04-23 22:44:06 +0000356 final quote and NUL. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000357 len = cpp_token_len (token);
Neil Booth93c803682000-10-28 17:59:06 +0000358 if (escape_it)
Neil Booth93c803682000-10-28 17:59:06 +0000359 len *= 4;
Neil Booth6338b352003-04-23 22:44:06 +0000360 len += 3;
Neil Booth93c803682000-10-28 17:59:06 +0000361
Neil Boothece54d52001-09-28 09:40:22 +0000362 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth93c803682000-10-28 17:59:06 +0000363 {
Neil Boothece54d52001-09-28 09:40:22 +0000364 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +0000365 _cpp_extend_buff (pfile, &pfile->u_buff, len);
Neil Boothece54d52001-09-28 09:40:22 +0000366 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth93c803682000-10-28 17:59:06 +0000367 }
368
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000369 /* Leading white space? */
Neil Booth6338b352003-04-23 22:44:06 +0000370 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000371 {
372 if (source == NULL)
373 source = token;
374 if (source->flags & PREV_WHITE)
375 *dest++ = ' ';
376 }
377 source = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000378
379 if (escape_it)
380 {
Neil Boothece54d52001-09-28 09:40:22 +0000381 _cpp_buff *buff = _cpp_get_buff (pfile, len);
382 unsigned char *buf = BUFF_FRONT (buff);
Geoffrey Keating47e20492005-03-12 10:44:06 +0000383 len = cpp_spell_token (pfile, token, buf, true) - buf;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000384 dest = cpp_quote_string (dest, buf, len);
Neil Boothece54d52001-09-28 09:40:22 +0000385 _cpp_release_buff (pfile, buff);
Neil Booth93c803682000-10-28 17:59:06 +0000386 }
387 else
Geoffrey Keating47e20492005-03-12 10:44:06 +0000388 dest = cpp_spell_token (pfile, token, dest, true);
Neil Booth93c803682000-10-28 17:59:06 +0000389
Neil Booth10676942003-04-22 19:28:00 +0000390 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
Neil Booth93c803682000-10-28 17:59:06 +0000391 backslash_count++;
392 else
393 backslash_count = 0;
394 }
395
396 /* Ignore the final \ of invalid string literals. */
397 if (backslash_count & 1)
398 {
John David Anglin0527bc42003-11-01 22:56:54 +0000399 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000400 "invalid string literal, ignoring final '\\'");
Neil Boothece54d52001-09-28 09:40:22 +0000401 dest--;
Neil Booth93c803682000-10-28 17:59:06 +0000402 }
403
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000404 /* Commit the memory, including NUL, and return the token. */
Neil Booth6338b352003-04-23 22:44:06 +0000405 *dest++ = '"';
Neil Boothece54d52001-09-28 09:40:22 +0000406 len = dest - BUFF_FRONT (pfile->u_buff);
407 BUFF_FRONT (pfile->u_buff) = dest + 1;
408 return new_string_token (pfile, dest - len, len);
Neil Booth93c803682000-10-28 17:59:06 +0000409}
410
Kazu Hiratada7d8302002-09-22 02:03:17 +0000411/* Try to paste two tokens. On success, return nonzero. In any
Neil Boothc9e7a602001-09-27 12:59:38 +0000412 case, PLHS is updated to point to the pasted token, which is
413 guaranteed to not have the PASTE_LEFT flag set. */
414static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000415paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
Neil Boothd63eefb2000-11-20 23:59:26 +0000416{
Neil Boothc9e7a602001-09-27 12:59:38 +0000417 unsigned char *buf, *end;
418 const cpp_token *lhs;
419 unsigned int len;
420 bool valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000421
Neil Boothc9e7a602001-09-27 12:59:38 +0000422 lhs = *plhs;
423 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +0000424 buf = alloca (len);
Geoffrey Keating47e20492005-03-12 10:44:06 +0000425 end = cpp_spell_token (pfile, lhs, buf, false);
Neil Boothd63eefb2000-11-20 23:59:26 +0000426
Neil Boothc9e7a602001-09-27 12:59:38 +0000427 /* Avoid comment headers, since they are still processed in stage 3.
428 It is simpler to insert a space here, rather than modifying the
429 lexer to ignore comments in some circumstances. Simply returning
430 false doesn't work, since we want to clear the PASTE_LEFT flag. */
Neil Booth5cc67322002-10-09 09:56:09 +0000431 if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
Neil Boothc9e7a602001-09-27 12:59:38 +0000432 *end++ = ' ';
Geoffrey Keating47e20492005-03-12 10:44:06 +0000433 end = cpp_spell_token (pfile, rhs, end, false);
Neil Booth26aea072003-04-19 00:22:51 +0000434 *end = '\n';
Neil Boothd63eefb2000-11-20 23:59:26 +0000435
Per Bothner40de9f72003-10-02 07:30:34 +0000436 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
Neil Booth26aea072003-04-19 00:22:51 +0000437 _cpp_clean_line (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000438
Neil Boothc9e7a602001-09-27 12:59:38 +0000439 /* Set pfile->cur_token as required by _cpp_lex_direct. */
440 pfile->cur_token = _cpp_temp_token (pfile);
441 *plhs = _cpp_lex_direct (pfile);
Neil Booth480709c2001-10-21 14:04:42 +0000442 valid = pfile->buffer->cur == pfile->buffer->rlimit;
Neil Boothc9e7a602001-09-27 12:59:38 +0000443 _cpp_pop_buffer (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000444
Neil Boothc9e7a602001-09-27 12:59:38 +0000445 return valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000446}
447
Neil Boothd15a58c2002-01-03 18:32:55 +0000448/* Handles an arbitrarily long sequence of ## operators, with initial
449 operand LHS. This implementation is left-associative,
450 non-recursive, and finishes a paste before handling succeeding
451 ones. If a paste fails, we back up to the RHS of the failing ##
452 operator before pushing the context containing the result of prior
453 successful pastes, with the effect that the RHS appears in the
454 output stream after the pasted LHS normally. */
Neil Booth93c803682000-10-28 17:59:06 +0000455static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000456paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
Neil Booth93c803682000-10-28 17:59:06 +0000457{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000458 const cpp_token *rhs;
459 cpp_context *context = pfile->context;
460
Neil Booth93c803682000-10-28 17:59:06 +0000461 do
462 {
463 /* Take the token directly from the current context. We can do
464 this, because we are in the replacement list of either an
465 object-like macro, or a function-like macro with arguments
466 inserted. In either case, the constraints to #define
Neil Boothd63eefb2000-11-20 23:59:26 +0000467 guarantee we have at least one more token. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000468 if (context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +0000469 rhs = FIRST (context).token++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000470 else
Neil Booth82eda772002-06-04 13:07:06 +0000471 rhs = *FIRST (context).ptoken++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000472
473 if (rhs->type == CPP_PADDING)
474 abort ();
475
Neil Boothc9e7a602001-09-27 12:59:38 +0000476 if (!paste_tokens (pfile, &lhs, rhs))
Neil Booth93c803682000-10-28 17:59:06 +0000477 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000478 _cpp_backup_tokens (pfile, 1);
Neil Boothc9e7a602001-09-27 12:59:38 +0000479
Neil Boothc3bf3e62002-05-09 17:14:22 +0000480 /* Mandatory error for all apart from assembler. */
Neil Boothc9e7a602001-09-27 12:59:38 +0000481 if (CPP_OPTION (pfile, lang) != CLK_ASM)
John David Anglin0527bc42003-11-01 22:56:54 +0000482 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothc9e7a602001-09-27 12:59:38 +0000483 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
Neil Boothebef4e82002-04-14 18:42:47 +0000484 cpp_token_as_text (pfile, lhs),
485 cpp_token_as_text (pfile, rhs));
Neil Booth4c2b6472000-11-11 13:19:01 +0000486 break;
Neil Booth93c803682000-10-28 17:59:06 +0000487 }
Neil Booth93c803682000-10-28 17:59:06 +0000488 }
489 while (rhs->flags & PASTE_LEFT);
490
Neil Boothc9e7a602001-09-27 12:59:38 +0000491 /* Put the resulting token in its own context. */
492 push_token_context (pfile, NULL, lhs, 1);
Neil Booth93c803682000-10-28 17:59:06 +0000493}
494
Neil Booth1ce676a2002-06-09 20:04:17 +0000495/* Returns TRUE if the number of arguments ARGC supplied in an
496 invocation of the MACRO referenced by NODE is valid. An empty
497 invocation to a macro with no parameters should pass ARGC as zero.
498
499 Note that MACRO cannot necessarily be deduced from NODE, in case
500 NODE was redefined whilst collecting arguments. */
501bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000502_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
Neil Booth1ce676a2002-06-09 20:04:17 +0000503{
504 if (argc == macro->paramc)
505 return true;
506
507 if (argc < macro->paramc)
508 {
509 /* As an extension, a rest argument is allowed to not appear in
510 the invocation at all.
511 e.g. #define debug(format, args...) something
512 debug("string");
513
514 This is exactly the same as if there had been an empty rest
515 argument - debug("string", ). */
516
517 if (argc + 1 == macro->paramc && macro->variadic)
518 {
519 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
John David Anglin0527bc42003-11-01 22:56:54 +0000520 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth1ce676a2002-06-09 20:04:17 +0000521 "ISO C99 requires rest arguments to be used");
522 return true;
523 }
524
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\" requires %u arguments, but only %u given",
527 NODE_NAME (node), macro->paramc, argc);
528 }
529 else
John David Anglin0527bc42003-11-01 22:56:54 +0000530 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1ce676a2002-06-09 20:04:17 +0000531 "macro \"%s\" passed %u arguments, but takes just %u",
532 NODE_NAME (node), argc, macro->paramc);
533
534 return false;
535}
536
Neil Boothd15a58c2002-01-03 18:32:55 +0000537/* Reads and returns the arguments to a function-like macro
538 invocation. Assumes the opening parenthesis has been processed.
539 If there is an error, emits an appropriate diagnostic and returns
540 NULL. Each argument is terminated by a CPP_EOF token, for the
541 future benefit of expand_arg(). */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000542static _cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000543collect_args (cpp_reader *pfile, const cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000544{
Neil Boothb8af0ca2001-09-26 17:52:50 +0000545 _cpp_buff *buff, *base_buff;
546 cpp_macro *macro;
547 macro_arg *args, *arg;
548 const cpp_token *token;
549 unsigned int argc;
Neil Booth93c803682000-10-28 17:59:06 +0000550
Neil Boothb8af0ca2001-09-26 17:52:50 +0000551 macro = node->value.macro;
552 if (macro->paramc)
553 argc = macro->paramc;
554 else
555 argc = 1;
556 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
557 + sizeof (macro_arg)));
558 base_buff = buff;
559 args = (macro_arg *) buff->base;
560 memset (args, 0, argc * sizeof (macro_arg));
Neil Boothece54d52001-09-28 09:40:22 +0000561 buff->cur = (unsigned char *) &args[argc];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000562 arg = args, argc = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000563
Neil Boothb8af0ca2001-09-26 17:52:50 +0000564 /* Collect the tokens making up each argument. We don't yet know
565 how many arguments have been supplied, whether too many or too
566 few. Hence the slightly bizarre usage of "argc" and "arg". */
567 do
Neil Booth93c803682000-10-28 17:59:06 +0000568 {
Neil Boothb8af0ca2001-09-26 17:52:50 +0000569 unsigned int paren_depth = 0;
570 unsigned int ntokens = 0;
571
Neil Booth93c803682000-10-28 17:59:06 +0000572 argc++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000573 arg->first = (const cpp_token **) buff->cur;
Neil Booth93c803682000-10-28 17:59:06 +0000574
Neil Boothb8af0ca2001-09-26 17:52:50 +0000575 for (;;)
576 {
577 /* Require space for 2 new tokens (including a CPP_EOF). */
Neil Boothece54d52001-09-28 09:40:22 +0000578 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000579 {
Neil Booth8c3b2692001-09-30 10:03:11 +0000580 buff = _cpp_append_extend_buff (pfile, buff,
581 1000 * sizeof (cpp_token *));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000582 arg->first = (const cpp_token **) buff->cur;
583 }
Neil Booth93c803682000-10-28 17:59:06 +0000584
Neil Boothb8af0ca2001-09-26 17:52:50 +0000585 token = cpp_get_token (pfile);
586
587 if (token->type == CPP_PADDING)
588 {
589 /* Drop leading padding. */
590 if (ntokens == 0)
591 continue;
592 }
593 else if (token->type == CPP_OPEN_PAREN)
594 paren_depth++;
595 else if (token->type == CPP_CLOSE_PAREN)
596 {
597 if (paren_depth-- == 0)
598 break;
599 }
600 else if (token->type == CPP_COMMA)
601 {
602 /* A comma does not terminate an argument within
603 parentheses or as part of a variable argument. */
604 if (paren_depth == 0
605 && ! (macro->variadic && argc == macro->paramc))
606 break;
607 }
608 else if (token->type == CPP_EOF
609 || (token->type == CPP_HASH && token->flags & BOL))
610 break;
611
612 arg->first[ntokens++] = token;
613 }
614
615 /* Drop trailing padding. */
616 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
617 ntokens--;
618
619 arg->count = ntokens;
620 arg->first[ntokens] = &pfile->eof;
621
622 /* Terminate the argument. Excess arguments loop back and
623 overwrite the final legitimate argument, before failing. */
624 if (argc <= macro->paramc)
625 {
Neil Boothece54d52001-09-28 09:40:22 +0000626 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000627 if (argc != macro->paramc)
628 arg++;
629 }
Neil Booth93c803682000-10-28 17:59:06 +0000630 }
Neil Boothe808ec92002-02-27 07:24:53 +0000631 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
Neil Booth93c803682000-10-28 17:59:06 +0000632
Neil Boothe808ec92002-02-27 07:24:53 +0000633 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000634 {
Neil Boothece54d52001-09-28 09:40:22 +0000635 /* We still need the CPP_EOF to end directives, and to end
636 pre-expansion of a macro argument. Step back is not
637 unconditional, since we don't want to return a CPP_EOF to our
638 callers at the end of an -include-d file. */
Neil Boothe808ec92002-02-27 07:24:53 +0000639 if (pfile->context->prev || pfile->state.in_directive)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000640 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +0000641 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000642 "unterminated argument list invoking macro \"%s\"",
Neil Bootha28c50352001-05-16 22:02:09 +0000643 NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +0000644 }
Neil Booth1ce676a2002-06-09 20:04:17 +0000645 else
Neil Booth93c803682000-10-28 17:59:06 +0000646 {
Neil Booth1ce676a2002-06-09 20:04:17 +0000647 /* A single empty argument is counted as no argument. */
648 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
649 argc = 0;
650 if (_cpp_arguments_ok (pfile, macro, node, argc))
Neil Booth58551c22002-08-06 20:35:46 +0000651 {
652 /* GCC has special semantics for , ## b where b is a varargs
653 parameter: we remove the comma if b was omitted entirely.
654 If b was merely an empty argument, the comma is retained.
655 If the macro takes just one (varargs) parameter, then we
656 retain the comma only if we are standards conforming.
657
658 If FIRST is NULL replace_args () swallows the comma. */
659 if (macro->variadic && (argc < macro->paramc
660 || (argc == 1 && args[0].count == 0
661 && !CPP_OPTION (pfile, std))))
662 args[macro->paramc - 1].first = NULL;
663 return base_buff;
664 }
Neil Booth93c803682000-10-28 17:59:06 +0000665 }
666
Neil Booth1ce676a2002-06-09 20:04:17 +0000667 /* An error occurred. */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000668 _cpp_release_buff (pfile, base_buff);
669 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000670}
671
Neil Boothd6da8362001-10-08 06:15:14 +0000672/* Search for an opening parenthesis to the macro of NODE, in such a
673 way that, if none is found, we don't lose the information in any
674 intervening padding tokens. If we find the parenthesis, collect
675 the arguments and return the buffer containing them. */
676static _cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000677funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000678{
Neil Boothd6da8362001-10-08 06:15:14 +0000679 const cpp_token *token, *padding = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000680
Neil Boothd6da8362001-10-08 06:15:14 +0000681 for (;;)
Neil Boothbdcbe492001-09-13 20:05:17 +0000682 {
Neil Boothd6da8362001-10-08 06:15:14 +0000683 token = cpp_get_token (pfile);
684 if (token->type != CPP_PADDING)
685 break;
686 if (padding == NULL
687 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
688 padding = token;
Neil Boothbdcbe492001-09-13 20:05:17 +0000689 }
Neil Booth93c803682000-10-28 17:59:06 +0000690
Neil Boothd6da8362001-10-08 06:15:14 +0000691 if (token->type == CPP_OPEN_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000692 {
Neil Boothd6da8362001-10-08 06:15:14 +0000693 pfile->state.parsing_args = 2;
694 return collect_args (pfile, node);
Neil Booth93c803682000-10-28 17:59:06 +0000695 }
696
Neil Booth9ac3b1b2002-04-21 16:17:55 +0000697 /* CPP_EOF can be the end of macro arguments, or the end of the
698 file. We mustn't back up over the latter. Ugh. */
699 if (token->type != CPP_EOF || token == &pfile->eof)
700 {
701 /* Back up. We may have skipped padding, in which case backing
702 up more than one token when expanding macros is in general
703 too difficult. We re-insert it in its own context. */
704 _cpp_backup_tokens (pfile, 1);
705 if (padding)
706 push_token_context (pfile, NULL, padding, 1);
707 }
Neil Boothd6da8362001-10-08 06:15:14 +0000708
709 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000710}
711
Neil Boothd15a58c2002-01-03 18:32:55 +0000712/* Push the context of a macro with hash entry NODE onto the context
713 stack. If we can successfully expand the macro, we push a context
714 containing its yet-to-be-rescanned replacement list and return one.
715 Otherwise, we don't push a context and return zero. */
Neil Booth93c803682000-10-28 17:59:06 +0000716static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000717enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000718{
Neil Boothd15a58c2002-01-03 18:32:55 +0000719 /* The presence of a macro invalidates a file's controlling macro. */
Neil Booth644edda2001-10-02 12:57:24 +0000720 pfile->mi_valid = false;
721
Neil Booth36207112002-05-24 19:26:30 +0000722 pfile->state.angled_headers = false;
723
Neil Boothd15a58c2002-01-03 18:32:55 +0000724 /* Handle standard macros. */
Neil Booth644edda2001-10-02 12:57:24 +0000725 if (! (node->flags & NODE_BUILTIN))
Neil Booth93c803682000-10-28 17:59:06 +0000726 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000727 cpp_macro *macro = node->value.macro;
728
Neil Boothd6da8362001-10-08 06:15:14 +0000729 if (macro->fun_like)
730 {
731 _cpp_buff *buff;
732
733 pfile->state.prevent_expansion++;
734 pfile->keep_tokens++;
735 pfile->state.parsing_args = 1;
736 buff = funlike_invocation_p (pfile, node);
737 pfile->state.parsing_args = 0;
738 pfile->keep_tokens--;
739 pfile->state.prevent_expansion--;
740
741 if (buff == NULL)
742 {
743 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
John David Anglin0527bc42003-11-01 22:56:54 +0000744 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothd6da8362001-10-08 06:15:14 +0000745 "function-like macro \"%s\" must be used with arguments in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +0000746 NODE_NAME (node));
Neil Boothd6da8362001-10-08 06:15:14 +0000747
748 return 0;
749 }
750
Neil Boothe808ec92002-02-27 07:24:53 +0000751 if (macro->paramc > 0)
752 replace_args (pfile, node, macro, (macro_arg *) buff->base);
Neil Boothd6da8362001-10-08 06:15:14 +0000753 _cpp_release_buff (pfile, buff);
754 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000755
756 /* Disable the macro within its expansion. */
Neil Booth644edda2001-10-02 12:57:24 +0000757 node->flags |= NODE_DISABLED;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000758
Neil Bootha69cbaa2002-07-23 22:57:49 +0000759 macro->used = 1;
760
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000761 if (macro->paramc == 0)
Neil Booth601328b2002-05-16 05:53:24 +0000762 push_token_context (pfile, node, macro->exp.tokens, macro->count);
Neil Booth644edda2001-10-02 12:57:24 +0000763
764 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000765 }
Neil Booth644edda2001-10-02 12:57:24 +0000766
Neil Boothd15a58c2002-01-03 18:32:55 +0000767 /* Handle built-in macros and the _Pragma operator. */
Neil Booth644edda2001-10-02 12:57:24 +0000768 return builtin_macro (pfile, node);
Zack Weinberg711b8822000-07-18 00:59:49 +0000769}
770
Neil Boothd15a58c2002-01-03 18:32:55 +0000771/* Replace the parameters in a function-like macro of NODE with the
772 actual ARGS, and place the result in a newly pushed token context.
773 Expand each argument before replacing, unless it is operated upon
774 by the # or ## operators. */
Neil Booth93c803682000-10-28 17:59:06 +0000775static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000776replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
Neil Booth93c803682000-10-28 17:59:06 +0000777{
778 unsigned int i, total;
779 const cpp_token *src, *limit;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000780 const cpp_token **dest, **first;
Neil Booth93c803682000-10-28 17:59:06 +0000781 macro_arg *arg;
Neil Booth1e013d22001-09-26 21:44:35 +0000782 _cpp_buff *buff;
Neil Booth93c803682000-10-28 17:59:06 +0000783
Neil Booth93c803682000-10-28 17:59:06 +0000784 /* First, fully macro-expand arguments, calculating the number of
Neil Booth1e013d22001-09-26 21:44:35 +0000785 tokens in the final expansion as we go. The ordering of the if
786 statements below is subtle; we must handle stringification before
787 pasting. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000788 total = macro->count;
Neil Booth601328b2002-05-16 05:53:24 +0000789 limit = macro->exp.tokens + macro->count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000790
Neil Booth601328b2002-05-16 05:53:24 +0000791 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth93c803682000-10-28 17:59:06 +0000792 if (src->type == CPP_MACRO_ARG)
793 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000794 /* Leading and trailing padding tokens. */
795 total += 2;
796
Neil Booth93c803682000-10-28 17:59:06 +0000797 /* We have an argument. If it is not being stringified or
798 pasted it is macro-replaced before insertion. */
Neil Booth6c53ebf2000-11-06 18:43:32 +0000799 arg = &args[src->val.arg_no - 1];
Neil Booth4c2b6472000-11-11 13:19:01 +0000800
Neil Booth93c803682000-10-28 17:59:06 +0000801 if (src->flags & STRINGIFY_ARG)
802 {
803 if (!arg->stringified)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000804 arg->stringified = stringify_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000805 }
806 else if ((src->flags & PASTE_LEFT)
Neil Booth601328b2002-05-16 05:53:24 +0000807 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
Neil Booth93c803682000-10-28 17:59:06 +0000808 total += arg->count - 1;
809 else
810 {
811 if (!arg->expanded)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000812 expand_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000813 total += arg->expanded_count - 1;
814 }
815 }
816
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000817 /* Now allocate space for the expansion, copy the tokens and replace
818 the arguments. */
Neil Booth1e013d22001-09-26 21:44:35 +0000819 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
820 first = (const cpp_token **) buff->base;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000821 dest = first;
Neil Booth93c803682000-10-28 17:59:06 +0000822
Neil Booth601328b2002-05-16 05:53:24 +0000823 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000824 {
825 unsigned int count;
826 const cpp_token **from, **paste_flag;
Neil Booth93c803682000-10-28 17:59:06 +0000827
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000828 if (src->type != CPP_MACRO_ARG)
829 {
830 *dest++ = src;
831 continue;
832 }
833
834 paste_flag = 0;
835 arg = &args[src->val.arg_no - 1];
836 if (src->flags & STRINGIFY_ARG)
837 count = 1, from = &arg->stringified;
838 else if (src->flags & PASTE_LEFT)
839 count = arg->count, from = arg->first;
Neil Booth601328b2002-05-16 05:53:24 +0000840 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000841 {
Neil Booth93c803682000-10-28 17:59:06 +0000842 count = arg->count, from = arg->first;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000843 if (dest != first)
844 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000845 if (dest[-1]->type == CPP_COMMA
846 && macro->variadic
847 && src->val.arg_no == macro->paramc)
848 {
Neil Booth58551c22002-08-06 20:35:46 +0000849 /* Swallow a pasted comma if from == NULL, otherwise
850 drop the paste flag. */
851 if (from == NULL)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000852 dest--;
853 else
854 paste_flag = dest - 1;
855 }
856 /* Remove the paste flag if the RHS is a placemarker. */
857 else if (count == 0)
858 paste_flag = dest - 1;
859 }
860 }
861 else
862 count = arg->expanded_count, from = arg->expanded;
Neil Booth93c803682000-10-28 17:59:06 +0000863
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000864 /* Padding on the left of an argument (unless RHS of ##). */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000865 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
Neil Booth601328b2002-05-16 05:53:24 +0000866 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000867 *dest++ = padding_token (pfile, src);
Neil Booth93c803682000-10-28 17:59:06 +0000868
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000869 if (count)
870 {
871 memcpy (dest, from, count * sizeof (cpp_token *));
872 dest += count;
Neil Booth93c803682000-10-28 17:59:06 +0000873
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000874 /* With a non-empty argument on the LHS of ##, the last
875 token should be flagged PASTE_LEFT. */
876 if (src->flags & PASTE_LEFT)
877 paste_flag = dest - 1;
878 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000879
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000880 /* Avoid paste on RHS (even case count == 0). */
881 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
882 *dest++ = &pfile->avoid_paste;
Neil Booth26ec42e2001-01-28 11:22:23 +0000883
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000884 /* Add a new paste flag, or remove an unwanted one. */
885 if (paste_flag)
886 {
887 cpp_token *token = _cpp_temp_token (pfile);
888 token->type = (*paste_flag)->type;
Jakub Jelinek73096712005-03-04 16:33:23 +0100889 token->val = (*paste_flag)->val;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000890 if (src->flags & PASTE_LEFT)
891 token->flags = (*paste_flag)->flags | PASTE_LEFT;
892 else
893 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
894 *paste_flag = token;
895 }
896 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000897
Neil Booth93c803682000-10-28 17:59:06 +0000898 /* Free the expanded arguments. */
899 for (i = 0; i < macro->paramc; i++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000900 if (args[i].expanded)
901 free (args[i].expanded);
902
Neil Booth644edda2001-10-02 12:57:24 +0000903 push_ptoken_context (pfile, node, buff, first, dest - first);
Neil Booth93c803682000-10-28 17:59:06 +0000904}
905
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000906/* Return a special padding token, with padding inherited from SOURCE. */
907static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000908padding_token (cpp_reader *pfile, const cpp_token *source)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000909{
910 cpp_token *result = _cpp_temp_token (pfile);
911
912 result->type = CPP_PADDING;
Paolo Bonzinibe0f1e52005-02-14 08:52:24 +0000913
914 /* Data in GCed data structures cannot be made const so far, so we
915 need a cast here. */
916 result->val.source = (cpp_token *) source;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000917 result->flags = 0;
918 return result;
919}
920
Neil Boothd15a58c2002-01-03 18:32:55 +0000921/* Get a new uninitialized context. Create a new one if we cannot
922 re-use an old one. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000923static cpp_context *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000924next_context (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000925{
926 cpp_context *result = pfile->context->next;
927
928 if (result == 0)
929 {
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +0200930 result = XNEW (cpp_context);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000931 result->prev = pfile->context;
932 result->next = 0;
933 pfile->context->next = result;
934 }
935
936 pfile->context = result;
937 return result;
938}
939
940/* Push a list of pointers to tokens. */
941static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000942push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
943 const cpp_token **first, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +0000944{
945 cpp_context *context = next_context (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000946
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000947 context->direct_p = false;
948 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000949 context->buff = buff;
Neil Booth82eda772002-06-04 13:07:06 +0000950 FIRST (context).ptoken = first;
951 LAST (context).ptoken = first + count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000952}
953
954/* Push a list of tokens. */
955static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000956push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
957 const cpp_token *first, unsigned int count)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000958{
959 cpp_context *context = next_context (pfile);
960
961 context->direct_p = true;
962 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000963 context->buff = NULL;
Neil Booth82eda772002-06-04 13:07:06 +0000964 FIRST (context).token = first;
965 LAST (context).token = first + count;
Neil Booth93c803682000-10-28 17:59:06 +0000966}
967
Neil Boothcbc69f82002-06-05 20:27:12 +0000968/* Push a traditional macro's replacement text. */
969void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000970_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
971 const uchar *start, size_t len)
Neil Boothcbc69f82002-06-05 20:27:12 +0000972{
973 cpp_context *context = next_context (pfile);
974
975 context->direct_p = true;
976 context->macro = macro;
977 context->buff = NULL;
978 CUR (context) = start;
Neil Booth1ce676a2002-06-09 20:04:17 +0000979 RLIMIT (context) = start + len;
Neil Booth974c43f2002-06-13 06:25:28 +0000980 macro->flags |= NODE_DISABLED;
Neil Boothcbc69f82002-06-05 20:27:12 +0000981}
982
Neil Boothd15a58c2002-01-03 18:32:55 +0000983/* Expand an argument ARG before replacing parameters in a
984 function-like macro. This works by pushing a context with the
985 argument's tokens, and then expanding that into a temporary buffer
986 as if it were a normal part of the token stream. collect_args()
987 has terminated the argument's tokens with a CPP_EOF so that we know
988 when we have fully expanded the argument. */
Neil Booth93c803682000-10-28 17:59:06 +0000989static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000990expand_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +0000991{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000992 unsigned int capacity;
Neil Booth56941bf2002-09-20 19:44:09 +0000993 bool saved_warn_trad;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000994
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000995 if (arg->count == 0)
996 return;
Neil Booth93c803682000-10-28 17:59:06 +0000997
Neil Booth56941bf2002-09-20 19:44:09 +0000998 /* Don't warn about funlike macros when pre-expanding. */
999 saved_warn_trad = CPP_WTRADITIONAL (pfile);
1000 CPP_WTRADITIONAL (pfile) = 0;
1001
Neil Booth93c803682000-10-28 17:59:06 +00001002 /* Loop, reading in the arguments. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001003 capacity = 256;
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001004 arg->expanded = xmalloc (capacity * sizeof (cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +00001005
Neil Booth1e013d22001-09-26 21:44:35 +00001006 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001007 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00001008 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001009 const cpp_token *token;
1010
1011 if (arg->expanded_count + 1 >= capacity)
Neil Booth93c803682000-10-28 17:59:06 +00001012 {
1013 capacity *= 2;
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001014 arg->expanded = xrealloc (arg->expanded,
1015 capacity * sizeof (cpp_token *));
Neil Booth93c803682000-10-28 17:59:06 +00001016 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001017
1018 token = cpp_get_token (pfile);
1019
1020 if (token->type == CPP_EOF)
1021 break;
1022
1023 arg->expanded[arg->expanded_count++] = token;
Neil Booth93c803682000-10-28 17:59:06 +00001024 }
Neil Booth93c803682000-10-28 17:59:06 +00001025
Neil Booth1e013d22001-09-26 21:44:35 +00001026 _cpp_pop_context (pfile);
Neil Booth56941bf2002-09-20 19:44:09 +00001027
1028 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
Neil Booth93c803682000-10-28 17:59:06 +00001029}
1030
Neil Boothd15a58c2002-01-03 18:32:55 +00001031/* Pop the current context off the stack, re-enabling the macro if the
1032 context represented a macro's replacement list. The context
1033 structure is not freed so that we can re-use it later. */
Neil Booth93c803682000-10-28 17:59:06 +00001034void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001035_cpp_pop_context (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001036{
Neil Booth1e013d22001-09-26 21:44:35 +00001037 cpp_context *context = pfile->context;
Neil Booth93c803682000-10-28 17:59:06 +00001038
Neil Booth1e013d22001-09-26 21:44:35 +00001039 if (context->macro)
Neil Booth644edda2001-10-02 12:57:24 +00001040 context->macro->flags &= ~NODE_DISABLED;
Neil Booth1e013d22001-09-26 21:44:35 +00001041
1042 if (context->buff)
1043 _cpp_release_buff (pfile, context->buff);
1044
1045 pfile->context = context->prev;
Neil Booth93c803682000-10-28 17:59:06 +00001046}
1047
Martin Schaffner48c47212003-06-25 23:01:10 +02001048/* External routine to get a token. Also used nearly everywhere
Neil Booth7f2f1a62000-11-14 18:32:06 +00001049 internally, except for places where we know we can safely call
Martin Schaffner48c47212003-06-25 23:01:10 +02001050 _cpp_lex_token directly, such as lexing a directive name.
Neil Booth7f2f1a62000-11-14 18:32:06 +00001051
1052 Macro expansions and directives are transparently handled,
1053 including entering included files. Thus tokens are post-macro
1054 expansion, and after any intervening directives. External callers
1055 see CPP_EOF only at EOF. Internal callers also see it when meeting
1056 a directive inside a macro call, when at the end of a directive and
1057 state.in_directive is still 1, and at the end of argument
1058 pre-expansion. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001059const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001060cpp_get_token (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001061{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001062 const cpp_token *result;
1063
Neil Booth29b10742000-11-13 18:40:37 +00001064 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00001065 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001066 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001067 cpp_context *context = pfile->context;
1068
Neil Booth93c803682000-10-28 17:59:06 +00001069 /* Context->prev == 0 <=> base context. */
Neil Boothbdcbe492001-09-13 20:05:17 +00001070 if (!context->prev)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001071 result = _cpp_lex_token (pfile);
Neil Booth82eda772002-06-04 13:07:06 +00001072 else if (FIRST (context).token != LAST (context).token)
Neil Booth29b10742000-11-13 18:40:37 +00001073 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001074 if (context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +00001075 result = FIRST (context).token++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001076 else
Neil Booth82eda772002-06-04 13:07:06 +00001077 result = *FIRST (context).ptoken++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001078
1079 if (result->flags & PASTE_LEFT)
Neil Boothec1a23e2001-01-31 07:48:54 +00001080 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001081 paste_all_tokens (pfile, result);
1082 if (pfile->state.in_directive)
1083 continue;
1084 return padding_token (pfile, result);
Neil Boothec1a23e2001-01-31 07:48:54 +00001085 }
Neil Booth29b10742000-11-13 18:40:37 +00001086 }
Neil Booth93c803682000-10-28 17:59:06 +00001087 else
1088 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001089 _cpp_pop_context (pfile);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001090 if (pfile->state.in_directive)
1091 continue;
1092 return &pfile->avoid_paste;
Neil Booth93c803682000-10-28 17:59:06 +00001093 }
Neil Booth93c803682000-10-28 17:59:06 +00001094
Jason Thorpe477cdac2002-04-07 03:12:23 +00001095 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1096 continue;
1097
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001098 if (result->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +00001099 break;
1100
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001101 node = result->val.node;
Neil Booth4c2b6472000-11-11 13:19:01 +00001102
Neil Booth644edda2001-10-02 12:57:24 +00001103 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1104 break;
Kazu Hiratadf383482002-05-22 22:02:16 +00001105
Neil Booth644edda2001-10-02 12:57:24 +00001106 if (!(node->flags & NODE_DISABLED))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001107 {
Neil Booth644edda2001-10-02 12:57:24 +00001108 if (!pfile->state.prevent_expansion
1109 && enter_macro_context (pfile, node))
Neil Boothbd969772001-02-01 19:13:53 +00001110 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001111 if (pfile->state.in_directive)
1112 continue;
1113 return padding_token (pfile, result);
Neil Boothbd969772001-02-01 19:13:53 +00001114 }
Neil Booth93c803682000-10-28 17:59:06 +00001115 }
Neil Booth644edda2001-10-02 12:57:24 +00001116 else
1117 {
Neil Boothd15a58c2002-01-03 18:32:55 +00001118 /* Flag this token as always unexpandable. FIXME: move this
1119 to collect_args()?. */
Neil Booth644edda2001-10-02 12:57:24 +00001120 cpp_token *t = _cpp_temp_token (pfile);
1121 t->type = result->type;
1122 t->flags = result->flags | NO_EXPAND;
Jakub Jelinek73096712005-03-04 16:33:23 +01001123 t->val = result->val;
Neil Booth644edda2001-10-02 12:57:24 +00001124 result = t;
1125 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001126
Neil Booth644edda2001-10-02 12:57:24 +00001127 break;
Neil Booth93c803682000-10-28 17:59:06 +00001128 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001129
1130 return result;
Neil Booth93c803682000-10-28 17:59:06 +00001131}
1132
Neil Booth7065e132001-02-14 07:38:20 +00001133/* Returns true if we're expanding an object-like macro that was
1134 defined in a system header. Just checks the macro at the top of
1135 the stack. Used for diagnostic suppression. */
1136int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001137cpp_sys_macro_p (cpp_reader *pfile)
Neil Booth7065e132001-02-14 07:38:20 +00001138{
Neil Booth644edda2001-10-02 12:57:24 +00001139 cpp_hashnode *node = pfile->context->macro;
Neil Booth7065e132001-02-14 07:38:20 +00001140
Neil Booth644edda2001-10-02 12:57:24 +00001141 return node && node->value.macro && node->value.macro->syshdr;
Neil Booth7065e132001-02-14 07:38:20 +00001142}
1143
Neil Boothaf0d16c2002-04-22 17:48:02 +00001144/* Read each token in, until end of the current file. Directives are
1145 transparently processed. */
Neil Booth93c803682000-10-28 17:59:06 +00001146void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001147cpp_scan_nooutput (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001148{
Per Bothner22234f52004-02-18 14:02:39 -08001149 /* Request a CPP_EOF token at the end of this file, rather than
1150 transparently continuing with the including file. */
1151 pfile->buffer->return_at_eof = true;
1152
Zack Weinbergc6e83802004-06-05 20:58:06 +00001153 pfile->state.discarding_output++;
1154 pfile->state.prevent_expansion++;
1155
Neil Booth590e1982002-07-01 12:47:54 +00001156 if (CPP_OPTION (pfile, traditional))
1157 while (_cpp_read_logical_line_trad (pfile))
1158 ;
1159 else
1160 while (cpp_get_token (pfile)->type != CPP_EOF)
1161 ;
Zack Weinbergc6e83802004-06-05 20:58:06 +00001162
1163 pfile->state.discarding_output--;
1164 pfile->state.prevent_expansion--;
Neil Booth93c803682000-10-28 17:59:06 +00001165}
1166
Neil Boothbdcbe492001-09-13 20:05:17 +00001167/* Step back one (or more) tokens. Can only step mack more than 1 if
1168 they are from the lexer, and not from macro expansion. */
Neil Boothb528a072000-11-12 11:46:21 +00001169void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001170_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +00001171{
Neil Boothbdcbe492001-09-13 20:05:17 +00001172 if (pfile->context->prev == NULL)
1173 {
1174 pfile->lookaheads += count;
1175 while (count--)
1176 {
Neil Booth3293c3e2001-11-19 21:04:49 +00001177 pfile->cur_token--;
1178 if (pfile->cur_token == pfile->cur_run->base
1179 /* Possible with -fpreprocessed and no leading #line. */
1180 && pfile->cur_run->prev != NULL)
Neil Boothbdcbe492001-09-13 20:05:17 +00001181 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001182 pfile->cur_run = pfile->cur_run->prev;
1183 pfile->cur_token = pfile->cur_run->limit;
1184 }
1185 }
1186 }
Neil Booth93c803682000-10-28 17:59:06 +00001187 else
1188 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001189 if (count != 1)
1190 abort ();
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001191 if (pfile->context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +00001192 FIRST (pfile->context).token--;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001193 else
Neil Booth82eda772002-06-04 13:07:06 +00001194 FIRST (pfile->context).ptoken--;
Neil Booth93c803682000-10-28 17:59:06 +00001195 }
Neil Booth93c803682000-10-28 17:59:06 +00001196}
1197
1198/* #define directive parsing and handling. */
1199
Kazu Hiratada7d8302002-09-22 02:03:17 +00001200/* Returns nonzero if a macro redefinition warning is required. */
Neil Boothcbc69f82002-06-05 20:27:12 +00001201static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001202warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
1203 const cpp_macro *macro2)
Neil Booth93c803682000-10-28 17:59:06 +00001204{
1205 const cpp_macro *macro1;
1206 unsigned int i;
1207
Neil Booth618cdda2001-02-25 09:43:03 +00001208 /* Some redefinitions need to be warned about regardless. */
1209 if (node->flags & NODE_WARN)
Neil Boothcbc69f82002-06-05 20:27:12 +00001210 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001211
Neil Booth618cdda2001-02-25 09:43:03 +00001212 /* Redefinition of a macro is allowed if and only if the old and new
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001213 definitions are the same. (6.10.3 paragraph 2). */
Neil Booth93c803682000-10-28 17:59:06 +00001214 macro1 = node->value.macro;
1215
Neil Booth6618c5d2002-06-10 06:03:13 +00001216 /* Don't check count here as it can be different in valid
1217 traditional redefinitions with just whitespace differences. */
1218 if (macro1->paramc != macro2->paramc
Neil Booth93c803682000-10-28 17:59:06 +00001219 || macro1->fun_like != macro2->fun_like
Neil Booth28e0f042000-12-09 12:06:37 +00001220 || macro1->variadic != macro2->variadic)
Neil Boothcbc69f82002-06-05 20:27:12 +00001221 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001222
1223 /* Check parameter spellings. */
1224 for (i = 0; i < macro1->paramc; i++)
1225 if (macro1->params[i] != macro2->params[i])
Neil Boothcbc69f82002-06-05 20:27:12 +00001226 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001227
Neil Boothcbc69f82002-06-05 20:27:12 +00001228 /* Check the replacement text or tokens. */
1229 if (CPP_OPTION (pfile, traditional))
Neil Booth6618c5d2002-06-10 06:03:13 +00001230 return _cpp_expansions_different_trad (macro1, macro2);
Neil Boothcbc69f82002-06-05 20:27:12 +00001231
DJ Deloriea7f36da2003-06-01 14:55:15 -04001232 if (macro1->count != macro2->count)
1233 return true;
1234
1235 for (i = 0; i < macro1->count; i++)
1236 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1237 return true;
Neil Boothcbc69f82002-06-05 20:27:12 +00001238
1239 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001240}
1241
Neil Booth93c803682000-10-28 17:59:06 +00001242/* Free the definition of hashnode H. */
Neil Booth93c803682000-10-28 17:59:06 +00001243void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001244_cpp_free_definition (cpp_hashnode *h)
Zack Weinberg711b8822000-07-18 00:59:49 +00001245{
Neil Booth93c803682000-10-28 17:59:06 +00001246 /* Macros and assertions no longer have anything to free. */
1247 h->type = NT_VOID;
1248 /* Clear builtin flag in case of redefinition. */
Neil Booth644edda2001-10-02 12:57:24 +00001249 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
Neil Booth93c803682000-10-28 17:59:06 +00001250}
Zack Weinberg711b8822000-07-18 00:59:49 +00001251
Neil Booth14baae02001-09-17 18:26:12 +00001252/* Save parameter NODE to the parameter list of macro MACRO. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001253 zero on success, nonzero if the parameter is a duplicate. */
Neil Boothc70f6ed2002-06-07 06:26:32 +00001254bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001255_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00001256{
Zack Weinberg4977bab2002-12-16 18:23:00 +00001257 unsigned int len;
Neil Booth93c803682000-10-28 17:59:06 +00001258 /* Constraint 6.10.3.6 - duplicate parameter names. */
Zack Weinberg4977bab2002-12-16 18:23:00 +00001259 if (node->flags & NODE_MACRO_ARG)
Zack Weinberg711b8822000-07-18 00:59:49 +00001260 {
John David Anglin0527bc42003-11-01 22:56:54 +00001261 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001262 NODE_NAME (node));
Neil Booth23ff0222002-07-17 17:27:14 +00001263 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001264 }
1265
Neil Booth8c3b2692001-09-30 10:03:11 +00001266 if (BUFF_ROOM (pfile->a_buff)
1267 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1268 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +00001269
Neil Booth8c3b2692001-09-30 10:03:11 +00001270 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
Zack Weinberg4977bab2002-12-16 18:23:00 +00001271 node->flags |= NODE_MACRO_ARG;
1272 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1273 if (len > pfile->macro_buffer_len)
1274 {
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001275 pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
Zack Weinberg4977bab2002-12-16 18:23:00 +00001276 pfile->macro_buffer_len = len;
1277 }
1278 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1279 = node->value;
1280
1281 node->value.arg_index = macro->paramc;
Neil Booth23ff0222002-07-17 17:27:14 +00001282 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001283}
1284
Neil Booth23ff0222002-07-17 17:27:14 +00001285/* Check the syntax of the parameters in a MACRO definition. Returns
1286 false if an error occurs. */
1287static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001288parse_params (cpp_reader *pfile, cpp_macro *macro)
Neil Booth93c803682000-10-28 17:59:06 +00001289{
Neil Booth93c803682000-10-28 17:59:06 +00001290 unsigned int prev_ident = 0;
1291
Neil Booth93c803682000-10-28 17:59:06 +00001292 for (;;)
1293 {
Neil Booth345894b2001-09-16 13:44:29 +00001294 const cpp_token *token = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001295
Neil Booth345894b2001-09-16 13:44:29 +00001296 switch (token->type)
Zack Weinberg711b8822000-07-18 00:59:49 +00001297 {
1298 default:
Jason Thorpe477cdac2002-04-07 03:12:23 +00001299 /* Allow/ignore comments in parameter lists if we are
1300 preserving comments in macro expansions. */
1301 if (token->type == CPP_COMMENT
1302 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1303 continue;
1304
John David Anglin0527bc42003-11-01 22:56:54 +00001305 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001306 "\"%s\" may not appear in macro parameter list",
Neil Booth345894b2001-09-16 13:44:29 +00001307 cpp_token_as_text (pfile, token));
Neil Booth23ff0222002-07-17 17:27:14 +00001308 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001309
Zack Weinberg711b8822000-07-18 00:59:49 +00001310 case CPP_NAME:
1311 if (prev_ident)
1312 {
John David Anglin0527bc42003-11-01 22:56:54 +00001313 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001314 "macro parameters must be comma-separated");
Neil Booth23ff0222002-07-17 17:27:14 +00001315 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001316 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001317 prev_ident = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001318
Neil Boothc70f6ed2002-06-07 06:26:32 +00001319 if (_cpp_save_parameter (pfile, macro, token->val.node))
Neil Booth23ff0222002-07-17 17:27:14 +00001320 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001321 continue;
1322
1323 case CPP_CLOSE_PAREN:
Neil Booth93c803682000-10-28 17:59:06 +00001324 if (prev_ident || macro->paramc == 0)
Neil Booth23ff0222002-07-17 17:27:14 +00001325 return true;
Zack Weinberg711b8822000-07-18 00:59:49 +00001326
1327 /* Fall through to pick up the error. */
1328 case CPP_COMMA:
1329 if (!prev_ident)
1330 {
John David Anglin0527bc42003-11-01 22:56:54 +00001331 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
Neil Booth23ff0222002-07-17 17:27:14 +00001332 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001333 }
1334 prev_ident = 0;
1335 continue;
1336
1337 case CPP_ELLIPSIS:
Neil Booth28e0f042000-12-09 12:06:37 +00001338 macro->variadic = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001339 if (!prev_ident)
1340 {
Neil Boothc70f6ed2002-06-07 06:26:32 +00001341 _cpp_save_parameter (pfile, macro,
1342 pfile->spec_nodes.n__VA_ARGS__);
Neil Booth93c803682000-10-28 17:59:06 +00001343 pfile->state.va_args_ok = 1;
Richard Hendersone5b79212004-02-19 14:18:50 -08001344 if (! CPP_OPTION (pfile, c99)
1345 && CPP_OPTION (pfile, pedantic)
1346 && CPP_OPTION (pfile, warn_variadic_macros))
John David Anglin0527bc42003-11-01 22:56:54 +00001347 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +00001348 "anonymous variadic macros were introduced in C99");
Zack Weinberg711b8822000-07-18 00:59:49 +00001349 }
Richard Hendersone5b79212004-02-19 14:18:50 -08001350 else if (CPP_OPTION (pfile, pedantic)
1351 && CPP_OPTION (pfile, warn_variadic_macros))
John David Anglin0527bc42003-11-01 22:56:54 +00001352 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +00001353 "ISO C does not permit named variadic macros");
Zack Weinberg711b8822000-07-18 00:59:49 +00001354
Neil Booth93c803682000-10-28 17:59:06 +00001355 /* We're at the end, and just expect a closing parenthesis. */
Neil Booth345894b2001-09-16 13:44:29 +00001356 token = _cpp_lex_token (pfile);
1357 if (token->type == CPP_CLOSE_PAREN)
Neil Booth23ff0222002-07-17 17:27:14 +00001358 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001359 /* Fall through. */
1360
1361 case CPP_EOF:
John David Anglin0527bc42003-11-01 22:56:54 +00001362 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
Neil Booth23ff0222002-07-17 17:27:14 +00001363 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001364 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001365 }
1366}
1367
Neil Booth14baae02001-09-17 18:26:12 +00001368/* Allocate room for a token from a macro's replacement list. */
Neil Booth93c803682000-10-28 17:59:06 +00001369static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001370alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001371{
Neil Booth8c3b2692001-09-30 10:03:11 +00001372 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1373 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
Zack Weinberg711b8822000-07-18 00:59:49 +00001374
Neil Booth8c3b2692001-09-30 10:03:11 +00001375 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
Neil Booth14baae02001-09-17 18:26:12 +00001376}
1377
Neil Boothd15a58c2002-01-03 18:32:55 +00001378/* Lex a token from the expansion of MACRO, but mark parameters as we
1379 find them and warn of traditional stringification. */
Neil Booth14baae02001-09-17 18:26:12 +00001380static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001381lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Neil Booth14baae02001-09-17 18:26:12 +00001382{
1383 cpp_token *token;
1384
1385 pfile->cur_token = alloc_expansion_token (pfile, macro);
1386 token = _cpp_lex_direct (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001387
Neil Boothd15a58c2002-01-03 18:32:55 +00001388 /* Is this a parameter? */
Zack Weinberg4977bab2002-12-16 18:23:00 +00001389 if (token->type == CPP_NAME
1390 && (token->val.node->flags & NODE_MACRO_ARG) != 0)
Zack Weinberg711b8822000-07-18 00:59:49 +00001391 {
Neil Booth93c803682000-10-28 17:59:06 +00001392 token->type = CPP_MACRO_ARG;
Zack Weinberg4977bab2002-12-16 18:23:00 +00001393 token->val.arg_no = token->val.node->value.arg_index;
Zack Weinberg711b8822000-07-18 00:59:49 +00001394 }
Neil Booth93c803682000-10-28 17:59:06 +00001395 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1396 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1397 check_trad_stringification (pfile, macro, &token->val.str);
Zack Weinberg711b8822000-07-18 00:59:49 +00001398
Neil Booth93c803682000-10-28 17:59:06 +00001399 return token;
Zack Weinberg711b8822000-07-18 00:59:49 +00001400}
1401
Neil Boothcbc69f82002-06-05 20:27:12 +00001402static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001403create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001404{
Neil Boothcbc69f82002-06-05 20:27:12 +00001405 cpp_token *token;
Neil Booth14baae02001-09-17 18:26:12 +00001406 const cpp_token *ctoken;
Zack Weinberg711b8822000-07-18 00:59:49 +00001407
Neil Booth93c803682000-10-28 17:59:06 +00001408 /* Get the first token of the expansion (or the '(' of a
1409 function-like macro). */
Neil Booth14baae02001-09-17 18:26:12 +00001410 ctoken = _cpp_lex_token (pfile);
1411
1412 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
Zack Weinberg711b8822000-07-18 00:59:49 +00001413 {
Neil Boothcbc69f82002-06-05 20:27:12 +00001414 bool ok = parse_params (pfile, macro);
Neil Booth8c3b2692001-09-30 10:03:11 +00001415 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1416 if (!ok)
Neil Boothcbc69f82002-06-05 20:27:12 +00001417 return false;
Neil Booth8c3b2692001-09-30 10:03:11 +00001418
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001419 /* Success. Commit or allocate the parameter array. */
1420 if (pfile->hash_table->alloc_subobject)
1421 {
Paolo Bonzinibe0f1e52005-02-14 08:52:24 +00001422 cpp_hashnode **params = pfile->hash_table->alloc_subobject
1423 (sizeof (cpp_hashnode *) * macro->paramc);
1424 memcpy (params, macro->params,
1425 sizeof (cpp_hashnode *) * macro->paramc);
1426 macro->params = params;
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001427 }
1428 else
1429 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
Neil Booth93c803682000-10-28 17:59:06 +00001430 macro->fun_like = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001431 }
Neil Booth14baae02001-09-17 18:26:12 +00001432 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
Jakub Jelinekcae064e2005-04-05 22:07:06 +02001433 {
1434 /* While ISO C99 requires whitespace before replacement text
1435 in a macro definition, ISO C90 with TC1 allows there characters
1436 from the basic source character set. */
1437 if (CPP_OPTION (pfile, c99))
1438 cpp_error (pfile, CPP_DL_PEDWARN,
1439 "ISO C99 requires whitespace after the macro name");
1440 else
1441 {
1442 int warntype = CPP_DL_WARNING;
1443 switch (ctoken->type)
1444 {
1445 case CPP_ATSIGN:
1446 case CPP_AT_NAME:
1447 case CPP_OBJC_STRING:
1448 /* '@' is not in basic character set. */
1449 warntype = CPP_DL_PEDWARN;
1450 break;
1451 case CPP_OTHER:
1452 /* Basic character set sans letters, digits and _. */
1453 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
1454 ctoken->val.str.text[0]) == NULL)
1455 warntype = CPP_DL_PEDWARN;
1456 break;
1457 default:
1458 /* All other tokens start with a character from basic
1459 character set. */
1460 break;
1461 }
1462 cpp_error (pfile, warntype,
1463 "missing whitespace after the macro name");
1464 }
1465 }
Neil Booth93c803682000-10-28 17:59:06 +00001466
Neil Booth14baae02001-09-17 18:26:12 +00001467 if (macro->fun_like)
1468 token = lex_expansion_token (pfile, macro);
1469 else
1470 {
1471 token = alloc_expansion_token (pfile, macro);
1472 *token = *ctoken;
1473 }
Neil Booth93c803682000-10-28 17:59:06 +00001474
1475 for (;;)
1476 {
1477 /* Check the stringifying # constraint 6.10.3.2.1 of
1478 function-like macros when lexing the subsequent token. */
1479 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
Zack Weinberg711b8822000-07-18 00:59:49 +00001480 {
Neil Booth93c803682000-10-28 17:59:06 +00001481 if (token->type == CPP_MACRO_ARG)
1482 {
1483 token->flags &= ~PREV_WHITE;
1484 token->flags |= STRINGIFY_ARG;
1485 token->flags |= token[-1].flags & PREV_WHITE;
1486 token[-1] = token[0];
1487 macro->count--;
1488 }
1489 /* Let assembler get away with murder. */
Neil Boothbdb05a72000-11-26 17:31:13 +00001490 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +00001491 {
John David Anglin0527bc42003-11-01 22:56:54 +00001492 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001493 "'#' is not followed by a macro parameter");
Neil Boothcbc69f82002-06-05 20:27:12 +00001494 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001495 }
1496 }
1497
1498 if (token->type == CPP_EOF)
1499 break;
1500
1501 /* Paste operator constraint 6.10.3.3.1. */
1502 if (token->type == CPP_PASTE)
1503 {
1504 /* Token-paste ##, can appear in both object-like and
1505 function-like macros, but not at the ends. */
1506 if (--macro->count > 0)
1507 token = lex_expansion_token (pfile, macro);
1508
1509 if (macro->count == 0 || token->type == CPP_EOF)
1510 {
John David Anglin0527bc42003-11-01 22:56:54 +00001511 cpp_error (pfile, CPP_DL_ERROR,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001512 "'##' cannot appear at either end of a macro expansion");
Neil Boothcbc69f82002-06-05 20:27:12 +00001513 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001514 }
1515
1516 token[-1].flags |= PASTE_LEFT;
Neil Booth93c803682000-10-28 17:59:06 +00001517 }
1518
1519 token = lex_expansion_token (pfile, macro);
1520 }
1521
Neil Booth601328b2002-05-16 05:53:24 +00001522 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001523 macro->traditional = 0;
Neil Booth8c3b2692001-09-30 10:03:11 +00001524
Neil Booth4c2b6472000-11-11 13:19:01 +00001525 /* Don't count the CPP_EOF. */
1526 macro->count--;
Neil Booth93c803682000-10-28 17:59:06 +00001527
Neil Boothd15a58c2002-01-03 18:32:55 +00001528 /* Clear whitespace on first token for warn_of_redefinition(). */
Neil Booth8c3b2692001-09-30 10:03:11 +00001529 if (macro->count)
Neil Booth601328b2002-05-16 05:53:24 +00001530 macro->exp.tokens[0].flags &= ~PREV_WHITE;
Neil Booth8c3b2692001-09-30 10:03:11 +00001531
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001532 /* Commit or allocate the memory. */
1533 if (pfile->hash_table->alloc_subobject)
1534 {
1535 cpp_token *tokns = pfile->hash_table->alloc_subobject (sizeof (cpp_token)
1536 * macro->count);
1537 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
1538 macro->exp.tokens = tokns;
1539 }
1540 else
1541 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
Neil Booth8c3b2692001-09-30 10:03:11 +00001542
Neil Boothcbc69f82002-06-05 20:27:12 +00001543 return true;
1544}
Neil Booth44ed91a2000-10-29 11:37:18 +00001545
Kazu Hiratada7d8302002-09-22 02:03:17 +00001546/* Parse a macro and save its expansion. Returns nonzero on success. */
Neil Boothcbc69f82002-06-05 20:27:12 +00001547bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001548_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
Neil Boothcbc69f82002-06-05 20:27:12 +00001549{
1550 cpp_macro *macro;
1551 unsigned int i;
1552 bool ok;
1553
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001554 if (pfile->hash_table->alloc_subobject)
1555 macro = pfile->hash_table->alloc_subobject (sizeof (cpp_macro));
1556 else
1557 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
Neil Boothcbc69f82002-06-05 20:27:12 +00001558 macro->line = pfile->directive_line;
1559 macro->params = 0;
1560 macro->paramc = 0;
1561 macro->variadic = 0;
Neil Booth23345bb2003-03-14 21:47:50 +00001562 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
Neil Boothcbc69f82002-06-05 20:27:12 +00001563 macro->count = 0;
1564 macro->fun_like = 0;
Neil Booth7065e132001-02-14 07:38:20 +00001565 /* To suppress some diagnostics. */
Per Bothner12f9df42004-02-11 07:29:30 -08001566 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
Neil Booth7065e132001-02-14 07:38:20 +00001567
Neil Boothcbc69f82002-06-05 20:27:12 +00001568 if (CPP_OPTION (pfile, traditional))
1569 ok = _cpp_create_trad_definition (pfile, macro);
1570 else
1571 {
1572 cpp_token *saved_cur_token = pfile->cur_token;
1573
1574 ok = create_iso_definition (pfile, macro);
1575
1576 /* Restore lexer position because of games lex_expansion_token()
1577 plays lexing the macro. We set the type for SEEN_EOL() in
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +00001578 directives.c.
Neil Boothcbc69f82002-06-05 20:27:12 +00001579
1580 Longer term we should lex the whole line before coming here,
1581 and just copy the expansion. */
1582 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1583 pfile->cur_token = saved_cur_token;
1584
1585 /* Stop the lexer accepting __VA_ARGS__. */
1586 pfile->state.va_args_ok = 0;
1587 }
1588
1589 /* Clear the fast argument lookup indices. */
1590 for (i = macro->paramc; i-- > 0; )
Zack Weinberg4977bab2002-12-16 18:23:00 +00001591 {
1592 struct cpp_hashnode *node = macro->params[i];
1593 node->flags &= ~ NODE_MACRO_ARG;
1594 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1595 }
Neil Boothcbc69f82002-06-05 20:27:12 +00001596
1597 if (!ok)
1598 return ok;
1599
Neil Boothc2734e02002-07-26 16:29:31 +00001600 if (node->type == NT_MACRO)
Neil Booth93c803682000-10-28 17:59:06 +00001601 {
Neil Bootha69cbaa2002-07-23 22:57:49 +00001602 if (CPP_OPTION (pfile, warn_unused_macros))
1603 _cpp_warn_if_unused_macro (pfile, node, NULL);
1604
Neil Boothcbc69f82002-06-05 20:27:12 +00001605 if (warn_of_redefinition (pfile, node, macro))
Neil Booth93c803682000-10-28 17:59:06 +00001606 {
John David Anglin0527bc42003-11-01 22:56:54 +00001607 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
Neil Boothebef4e82002-04-14 18:42:47 +00001608 "\"%s\" redefined", NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +00001609
Neil Booth618cdda2001-02-25 09:43:03 +00001610 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
John David Anglin0527bc42003-11-01 22:56:54 +00001611 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
Neil Boothcbc69f82002-06-05 20:27:12 +00001612 node->value.macro->line, 0,
1613 "this is the location of the previous definition");
Zack Weinberg711b8822000-07-18 00:59:49 +00001614 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001615 }
1616
Neil Boothc2734e02002-07-26 16:29:31 +00001617 if (node->type != NT_VOID)
1618 _cpp_free_definition (node);
1619
Zack Weinberg711b8822000-07-18 00:59:49 +00001620 /* Enter definition in hash table. */
Neil Booth93c803682000-10-28 17:59:06 +00001621 node->type = NT_MACRO;
1622 node->value.macro = macro;
Neil Bootha28c50352001-05-16 22:02:09 +00001623 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
Neil Booth618cdda2001-02-25 09:43:03 +00001624 node->flags |= NODE_WARN;
Zack Weinberg711b8822000-07-18 00:59:49 +00001625
Neil Booth93c803682000-10-28 17:59:06 +00001626 return ok;
Zack Weinberg711b8822000-07-18 00:59:49 +00001627}
1628
Neil Boothd15a58c2002-01-03 18:32:55 +00001629/* Warn if a token in STRING matches one of a function-like MACRO's
1630 parameters. */
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001631static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001632check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1633 const cpp_string *string)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001634{
Neil Booth93c803682000-10-28 17:59:06 +00001635 unsigned int i, len;
Neil Booth6338b352003-04-23 22:44:06 +00001636 const uchar *p, *q, *limit;
Kazu Hiratadf383482002-05-22 22:02:16 +00001637
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001638 /* Loop over the string. */
Neil Booth6338b352003-04-23 22:44:06 +00001639 limit = string->text + string->len - 1;
1640 for (p = string->text + 1; p < limit; p = q)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001641 {
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001642 /* Find the start of an identifier. */
Greg McGary61c16b12000-09-15 21:25:02 +00001643 while (p < limit && !is_idstart (*p))
1644 p++;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001645
1646 /* Find the end of the identifier. */
1647 q = p;
Greg McGary61c16b12000-09-15 21:25:02 +00001648 while (q < limit && is_idchar (*q))
1649 q++;
Neil Booth93c803682000-10-28 17:59:06 +00001650
1651 len = q - p;
1652
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001653 /* Loop over the function macro arguments to see if the
1654 identifier inside the string matches one of them. */
Neil Booth93c803682000-10-28 17:59:06 +00001655 for (i = 0; i < macro->paramc; i++)
1656 {
1657 const cpp_hashnode *node = macro->params[i];
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001658
Neil Booth2a967f32001-05-20 06:26:45 +00001659 if (NODE_LEN (node) == len
1660 && !memcmp (p, NODE_NAME (node), len))
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001661 {
John David Anglin0527bc42003-11-01 22:56:54 +00001662 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinbergf458d1d2002-02-27 18:48:07 +00001663 "macro argument \"%s\" would be stringified in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +00001664 NODE_NAME (node));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001665 break;
1666 }
1667 }
1668 }
1669}
Neil Booth93c803682000-10-28 17:59:06 +00001670
Neil Booth70961712001-06-23 11:34:41 +00001671/* Returns the name, arguments and expansion of a macro, in a format
1672 suitable to be read back in again, and therefore also for DWARF 2
1673 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1674 Caller is expected to generate the "#define" bit if needed. The
Neil Booth93c803682000-10-28 17:59:06 +00001675 returned text is temporary, and automatically freed later. */
Neil Booth93c803682000-10-28 17:59:06 +00001676const unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001677cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00001678{
1679 unsigned int i, len;
1680 const cpp_macro *macro = node->value.macro;
1681 unsigned char *buffer;
1682
1683 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1684 {
John David Anglin0527bc42003-11-01 22:56:54 +00001685 cpp_error (pfile, CPP_DL_ICE,
Neil Boothebef4e82002-04-14 18:42:47 +00001686 "invalid hash type %d in cpp_macro_definition", node->type);
Neil Booth93c803682000-10-28 17:59:06 +00001687 return 0;
1688 }
1689
1690 /* Calculate length. */
Neil Booth8dc901d2002-05-29 19:30:07 +00001691 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
Neil Booth93c803682000-10-28 17:59:06 +00001692 if (macro->fun_like)
1693 {
Jim Blandy64d08262002-04-05 00:12:40 +00001694 len += 4; /* "()" plus possible final ".." of named
1695 varargs (we have + 1 below). */
Neil Booth93c803682000-10-28 17:59:06 +00001696 for (i = 0; i < macro->paramc; i++)
Jim Blandy64d08262002-04-05 00:12:40 +00001697 len += NODE_LEN (macro->params[i]) + 1; /* "," */
Neil Booth93c803682000-10-28 17:59:06 +00001698 }
1699
Eric Christopher6da55c02005-02-15 23:18:04 +00001700 /* This should match below where we fill in the buffer. */
Neil Booth278c4662002-06-19 05:40:08 +00001701 if (CPP_OPTION (pfile, traditional))
1702 len += _cpp_replacement_text_len (macro);
1703 else
Neil Booth93c803682000-10-28 17:59:06 +00001704 {
Neil Booth278c4662002-06-19 05:40:08 +00001705 for (i = 0; i < macro->count; i++)
1706 {
1707 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00001708
Neil Booth278c4662002-06-19 05:40:08 +00001709 if (token->type == CPP_MACRO_ARG)
1710 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1711 else
Eric Christopher6da55c02005-02-15 23:18:04 +00001712 len += cpp_token_len (token);
1713
Neil Booth278c4662002-06-19 05:40:08 +00001714 if (token->flags & STRINGIFY_ARG)
1715 len++; /* "#" */
1716 if (token->flags & PASTE_LEFT)
1717 len += 3; /* " ##" */
Eric Christopher6da55c02005-02-15 23:18:04 +00001718 if (token->flags & PREV_WHITE)
1719 len++; /* " " */
Neil Booth278c4662002-06-19 05:40:08 +00001720 }
Neil Booth93c803682000-10-28 17:59:06 +00001721 }
1722
1723 if (len > pfile->macro_buffer_len)
Alexandre Oliva4b49c362001-01-09 09:30:43 +00001724 {
Kaveh R. Ghazi703ad42b2003-07-19 14:47:15 +00001725 pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
Alexandre Oliva4b49c362001-01-09 09:30:43 +00001726 pfile->macro_buffer_len = len;
1727 }
Neil Booth70961712001-06-23 11:34:41 +00001728
1729 /* Fill in the buffer. Start with the macro name. */
Neil Booth93c803682000-10-28 17:59:06 +00001730 buffer = pfile->macro_buffer;
Neil Booth70961712001-06-23 11:34:41 +00001731 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1732 buffer += NODE_LEN (node);
Neil Booth93c803682000-10-28 17:59:06 +00001733
1734 /* Parameter names. */
1735 if (macro->fun_like)
1736 {
1737 *buffer++ = '(';
1738 for (i = 0; i < macro->paramc; i++)
1739 {
1740 cpp_hashnode *param = macro->params[i];
1741
1742 if (param != pfile->spec_nodes.n__VA_ARGS__)
1743 {
Neil Bootha28c50352001-05-16 22:02:09 +00001744 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1745 buffer += NODE_LEN (param);
Neil Booth93c803682000-10-28 17:59:06 +00001746 }
1747
1748 if (i + 1 < macro->paramc)
Kazu Hiratadf383482002-05-22 22:02:16 +00001749 /* Don't emit a space after the comma here; we're trying
1750 to emit a Dwarf-friendly definition, and the Dwarf spec
1751 forbids spaces in the argument list. */
Jim Blandy64d08262002-04-05 00:12:40 +00001752 *buffer++ = ',';
Neil Booth28e0f042000-12-09 12:06:37 +00001753 else if (macro->variadic)
Neil Booth93c803682000-10-28 17:59:06 +00001754 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1755 }
1756 *buffer++ = ')';
1757 }
1758
Jim Blandye37b38d2002-03-19 21:43:39 +00001759 /* The Dwarf spec requires a space after the macro name, even if the
1760 definition is the empty string. */
1761 *buffer++ = ' ';
1762
Neil Booth278c4662002-06-19 05:40:08 +00001763 if (CPP_OPTION (pfile, traditional))
1764 buffer = _cpp_copy_replacement_text (macro, buffer);
1765 else if (macro->count)
Neil Booth93c803682000-10-28 17:59:06 +00001766 /* Expansion tokens. */
Neil Booth93c803682000-10-28 17:59:06 +00001767 {
Neil Booth93c803682000-10-28 17:59:06 +00001768 for (i = 0; i < macro->count; i++)
1769 {
Neil Booth601328b2002-05-16 05:53:24 +00001770 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00001771
1772 if (token->flags & PREV_WHITE)
1773 *buffer++ = ' ';
1774 if (token->flags & STRINGIFY_ARG)
1775 *buffer++ = '#';
1776
1777 if (token->type == CPP_MACRO_ARG)
1778 {
Neil Bootha28c50352001-05-16 22:02:09 +00001779 memcpy (buffer,
Eric Christopher6da55c02005-02-15 23:18:04 +00001780 NODE_NAME (macro->params[token->val.arg_no - 1]),
1781 NODE_LEN (macro->params[token->val.arg_no - 1]));
1782 buffer += NODE_LEN (macro->params[token->val.arg_no - 1]);
Neil Booth93c803682000-10-28 17:59:06 +00001783 }
1784 else
Geoffrey Keating47e20492005-03-12 10:44:06 +00001785 buffer = cpp_spell_token (pfile, token, buffer, false);
Neil Booth93c803682000-10-28 17:59:06 +00001786
1787 if (token->flags & PASTE_LEFT)
1788 {
1789 *buffer++ = ' ';
1790 *buffer++ = '#';
1791 *buffer++ = '#';
1792 /* Next has PREV_WHITE; see _cpp_create_definition. */
1793 }
1794 }
1795 }
1796
1797 *buffer = '\0';
1798 return pfile->macro_buffer;
1799}