blob: a0aa93ea994acbd5cccc23f02c862bb4ed4bb0a0 [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
Kelley Cook200031d2005-06-29 02:34:39 +000020Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Zack Weinberg711b8822000-07-18 00:59:49 +000021
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);
Andrew Pinski651ed942005-11-04 00:23:01 +0000142 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
Neil Booth278c4662002-06-19 05:40:08 +0000143 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);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000276 nbuf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +0000277 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
Andrew Pinski651ed942005-11-04 00:23:01 +0000295 backslashes and double quotes. DEST must be of sufficient size.
296 Returns a pointer to the end of the string. */
Neil Booth562a5c22002-04-21 18:46:42 +0000297uchar *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000298cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
Neil Booth93c803682000-10-28 17:59:06 +0000299{
300 while (len--)
301 {
Neil Booth562a5c22002-04-21 18:46:42 +0000302 uchar c = *src++;
Neil Booth93c803682000-10-28 17:59:06 +0000303
304 if (c == '\\' || c == '"')
305 {
306 *dest++ = '\\';
307 *dest++ = c;
308 }
309 else
Andrew Pinski651ed942005-11-04 00:23:01 +0000310 *dest++ = c;
Neil Booth93c803682000-10-28 17:59:06 +0000311 }
312
313 return dest;
314}
315
Neil Boothd15a58c2002-01-03 18:32:55 +0000316/* Convert a token sequence ARG to a single string token according to
317 the rules of the ISO C #-operator. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000318static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000319stringify_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +0000320{
Neil Booth6338b352003-04-23 22:44:06 +0000321 unsigned char *dest;
Neil Boothece54d52001-09-28 09:40:22 +0000322 unsigned int i, escape_it, backslash_count = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000323 const cpp_token *source = NULL;
Neil Boothece54d52001-09-28 09:40:22 +0000324 size_t len;
Neil Booth93c803682000-10-28 17:59:06 +0000325
Neil Booth6338b352003-04-23 22:44:06 +0000326 if (BUFF_ROOM (pfile->u_buff) < 3)
327 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
328 dest = BUFF_FRONT (pfile->u_buff);
329 *dest++ = '"';
330
Neil Booth93c803682000-10-28 17:59:06 +0000331 /* Loop, reading in the argument's tokens. */
332 for (i = 0; i < arg->count; i++)
333 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000334 const cpp_token *token = arg->first[i];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000335
336 if (token->type == CPP_PADDING)
337 {
338 if (source == NULL)
339 source = token->val.source;
340 continue;
341 }
Neil Booth93c803682000-10-28 17:59:06 +0000342
343 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
Zack Weinbergcc937582001-03-07 01:32:01 +0000344 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
Neil Booth93c803682000-10-28 17:59:06 +0000345
Neil Boothece54d52001-09-28 09:40:22 +0000346 /* Room for each char being written in octal, initial space and
Neil Booth6338b352003-04-23 22:44:06 +0000347 final quote and NUL. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000348 len = cpp_token_len (token);
Neil Booth93c803682000-10-28 17:59:06 +0000349 if (escape_it)
Neil Booth93c803682000-10-28 17:59:06 +0000350 len *= 4;
Neil Booth6338b352003-04-23 22:44:06 +0000351 len += 3;
Neil Booth93c803682000-10-28 17:59:06 +0000352
Neil Boothece54d52001-09-28 09:40:22 +0000353 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth93c803682000-10-28 17:59:06 +0000354 {
Neil Boothece54d52001-09-28 09:40:22 +0000355 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +0000356 _cpp_extend_buff (pfile, &pfile->u_buff, len);
Neil Boothece54d52001-09-28 09:40:22 +0000357 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth93c803682000-10-28 17:59:06 +0000358 }
359
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000360 /* Leading white space? */
Neil Booth6338b352003-04-23 22:44:06 +0000361 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000362 {
363 if (source == NULL)
364 source = token;
365 if (source->flags & PREV_WHITE)
366 *dest++ = ' ';
367 }
368 source = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000369
370 if (escape_it)
371 {
Neil Boothece54d52001-09-28 09:40:22 +0000372 _cpp_buff *buff = _cpp_get_buff (pfile, len);
373 unsigned char *buf = BUFF_FRONT (buff);
Geoffrey Keating47e20492005-03-12 10:44:06 +0000374 len = cpp_spell_token (pfile, token, buf, true) - buf;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000375 dest = cpp_quote_string (dest, buf, len);
Neil Boothece54d52001-09-28 09:40:22 +0000376 _cpp_release_buff (pfile, buff);
Neil Booth93c803682000-10-28 17:59:06 +0000377 }
378 else
Geoffrey Keating47e20492005-03-12 10:44:06 +0000379 dest = cpp_spell_token (pfile, token, dest, true);
Neil Booth93c803682000-10-28 17:59:06 +0000380
Neil Booth10676942003-04-22 19:28:00 +0000381 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
Neil Booth93c803682000-10-28 17:59:06 +0000382 backslash_count++;
383 else
384 backslash_count = 0;
385 }
386
387 /* Ignore the final \ of invalid string literals. */
388 if (backslash_count & 1)
389 {
John David Anglin0527bc42003-11-01 22:56:54 +0000390 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000391 "invalid string literal, ignoring final '\\'");
Neil Boothece54d52001-09-28 09:40:22 +0000392 dest--;
Neil Booth93c803682000-10-28 17:59:06 +0000393 }
394
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000395 /* Commit the memory, including NUL, and return the token. */
Neil Booth6338b352003-04-23 22:44:06 +0000396 *dest++ = '"';
Neil Boothece54d52001-09-28 09:40:22 +0000397 len = dest - BUFF_FRONT (pfile->u_buff);
398 BUFF_FRONT (pfile->u_buff) = dest + 1;
399 return new_string_token (pfile, dest - len, len);
Neil Booth93c803682000-10-28 17:59:06 +0000400}
401
Kazu Hiratada7d8302002-09-22 02:03:17 +0000402/* Try to paste two tokens. On success, return nonzero. In any
Neil Boothc9e7a602001-09-27 12:59:38 +0000403 case, PLHS is updated to point to the pasted token, which is
404 guaranteed to not have the PASTE_LEFT flag set. */
405static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000406paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
Neil Boothd63eefb2000-11-20 23:59:26 +0000407{
Neil Boothc9e7a602001-09-27 12:59:38 +0000408 unsigned char *buf, *end;
409 const cpp_token *lhs;
410 unsigned int len;
411 bool valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000412
Neil Boothc9e7a602001-09-27 12:59:38 +0000413 lhs = *plhs;
414 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000415 buf = (unsigned char *) alloca (len);
Geoffrey Keating47e20492005-03-12 10:44:06 +0000416 end = cpp_spell_token (pfile, lhs, buf, false);
Neil Boothd63eefb2000-11-20 23:59:26 +0000417
Neil Boothc9e7a602001-09-27 12:59:38 +0000418 /* Avoid comment headers, since they are still processed in stage 3.
419 It is simpler to insert a space here, rather than modifying the
420 lexer to ignore comments in some circumstances. Simply returning
421 false doesn't work, since we want to clear the PASTE_LEFT flag. */
Neil Booth5cc67322002-10-09 09:56:09 +0000422 if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
Neil Boothc9e7a602001-09-27 12:59:38 +0000423 *end++ = ' ';
Geoffrey Keating47e20492005-03-12 10:44:06 +0000424 end = cpp_spell_token (pfile, rhs, end, false);
Neil Booth26aea072003-04-19 00:22:51 +0000425 *end = '\n';
Neil Boothd63eefb2000-11-20 23:59:26 +0000426
Per Bothner40de9f72003-10-02 07:30:34 +0000427 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
Neil Booth26aea072003-04-19 00:22:51 +0000428 _cpp_clean_line (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000429
Neil Boothc9e7a602001-09-27 12:59:38 +0000430 /* Set pfile->cur_token as required by _cpp_lex_direct. */
431 pfile->cur_token = _cpp_temp_token (pfile);
432 *plhs = _cpp_lex_direct (pfile);
Neil Booth480709c2001-10-21 14:04:42 +0000433 valid = pfile->buffer->cur == pfile->buffer->rlimit;
Neil Boothc9e7a602001-09-27 12:59:38 +0000434 _cpp_pop_buffer (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000435
Neil Boothc9e7a602001-09-27 12:59:38 +0000436 return valid;
Neil Boothd63eefb2000-11-20 23:59:26 +0000437}
438
Neil Boothd15a58c2002-01-03 18:32:55 +0000439/* Handles an arbitrarily long sequence of ## operators, with initial
440 operand LHS. This implementation is left-associative,
441 non-recursive, and finishes a paste before handling succeeding
442 ones. If a paste fails, we back up to the RHS of the failing ##
443 operator before pushing the context containing the result of prior
444 successful pastes, with the effect that the RHS appears in the
445 output stream after the pasted LHS normally. */
Neil Booth93c803682000-10-28 17:59:06 +0000446static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000447paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
Neil Booth93c803682000-10-28 17:59:06 +0000448{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000449 const cpp_token *rhs;
450 cpp_context *context = pfile->context;
451
Neil Booth93c803682000-10-28 17:59:06 +0000452 do
453 {
454 /* Take the token directly from the current context. We can do
455 this, because we are in the replacement list of either an
456 object-like macro, or a function-like macro with arguments
457 inserted. In either case, the constraints to #define
Neil Boothd63eefb2000-11-20 23:59:26 +0000458 guarantee we have at least one more token. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000459 if (context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +0000460 rhs = FIRST (context).token++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000461 else
Neil Booth82eda772002-06-04 13:07:06 +0000462 rhs = *FIRST (context).ptoken++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000463
464 if (rhs->type == CPP_PADDING)
465 abort ();
466
Neil Boothc9e7a602001-09-27 12:59:38 +0000467 if (!paste_tokens (pfile, &lhs, rhs))
Neil Booth93c803682000-10-28 17:59:06 +0000468 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000469 _cpp_backup_tokens (pfile, 1);
Neil Boothc9e7a602001-09-27 12:59:38 +0000470
Neil Boothc3bf3e62002-05-09 17:14:22 +0000471 /* Mandatory error for all apart from assembler. */
Neil Boothc9e7a602001-09-27 12:59:38 +0000472 if (CPP_OPTION (pfile, lang) != CLK_ASM)
John David Anglin0527bc42003-11-01 22:56:54 +0000473 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothc9e7a602001-09-27 12:59:38 +0000474 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
Neil Boothebef4e82002-04-14 18:42:47 +0000475 cpp_token_as_text (pfile, lhs),
476 cpp_token_as_text (pfile, rhs));
Neil Booth4c2b6472000-11-11 13:19:01 +0000477 break;
Neil Booth93c803682000-10-28 17:59:06 +0000478 }
Neil Booth93c803682000-10-28 17:59:06 +0000479 }
480 while (rhs->flags & PASTE_LEFT);
481
Neil Boothc9e7a602001-09-27 12:59:38 +0000482 /* Put the resulting token in its own context. */
483 push_token_context (pfile, NULL, lhs, 1);
Neil Booth93c803682000-10-28 17:59:06 +0000484}
485
Neil Booth1ce676a2002-06-09 20:04:17 +0000486/* Returns TRUE if the number of arguments ARGC supplied in an
487 invocation of the MACRO referenced by NODE is valid. An empty
488 invocation to a macro with no parameters should pass ARGC as zero.
489
490 Note that MACRO cannot necessarily be deduced from NODE, in case
491 NODE was redefined whilst collecting arguments. */
492bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000493_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
Neil Booth1ce676a2002-06-09 20:04:17 +0000494{
495 if (argc == macro->paramc)
496 return true;
497
498 if (argc < macro->paramc)
499 {
500 /* As an extension, a rest argument is allowed to not appear in
501 the invocation at all.
502 e.g. #define debug(format, args...) something
503 debug("string");
504
505 This is exactly the same as if there had been an empty rest
506 argument - debug("string", ). */
507
508 if (argc + 1 == macro->paramc && macro->variadic)
509 {
510 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
John David Anglin0527bc42003-11-01 22:56:54 +0000511 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth1ce676a2002-06-09 20:04:17 +0000512 "ISO C99 requires rest arguments to be used");
513 return true;
514 }
515
John David Anglin0527bc42003-11-01 22:56:54 +0000516 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1ce676a2002-06-09 20:04:17 +0000517 "macro \"%s\" requires %u arguments, but only %u given",
518 NODE_NAME (node), macro->paramc, argc);
519 }
520 else
John David Anglin0527bc42003-11-01 22:56:54 +0000521 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1ce676a2002-06-09 20:04:17 +0000522 "macro \"%s\" passed %u arguments, but takes just %u",
523 NODE_NAME (node), argc, macro->paramc);
524
525 return false;
526}
527
Neil Boothd15a58c2002-01-03 18:32:55 +0000528/* Reads and returns the arguments to a function-like macro
529 invocation. Assumes the opening parenthesis has been processed.
530 If there is an error, emits an appropriate diagnostic and returns
531 NULL. Each argument is terminated by a CPP_EOF token, for the
532 future benefit of expand_arg(). */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000533static _cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000534collect_args (cpp_reader *pfile, const cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000535{
Neil Boothb8af0ca2001-09-26 17:52:50 +0000536 _cpp_buff *buff, *base_buff;
537 cpp_macro *macro;
538 macro_arg *args, *arg;
539 const cpp_token *token;
540 unsigned int argc;
Neil Booth93c803682000-10-28 17:59:06 +0000541
Neil Boothb8af0ca2001-09-26 17:52:50 +0000542 macro = node->value.macro;
543 if (macro->paramc)
544 argc = macro->paramc;
545 else
546 argc = 1;
547 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
548 + sizeof (macro_arg)));
549 base_buff = buff;
550 args = (macro_arg *) buff->base;
551 memset (args, 0, argc * sizeof (macro_arg));
Neil Boothece54d52001-09-28 09:40:22 +0000552 buff->cur = (unsigned char *) &args[argc];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000553 arg = args, argc = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000554
Neil Boothb8af0ca2001-09-26 17:52:50 +0000555 /* Collect the tokens making up each argument. We don't yet know
556 how many arguments have been supplied, whether too many or too
557 few. Hence the slightly bizarre usage of "argc" and "arg". */
558 do
Neil Booth93c803682000-10-28 17:59:06 +0000559 {
Neil Boothb8af0ca2001-09-26 17:52:50 +0000560 unsigned int paren_depth = 0;
561 unsigned int ntokens = 0;
562
Neil Booth93c803682000-10-28 17:59:06 +0000563 argc++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000564 arg->first = (const cpp_token **) buff->cur;
Neil Booth93c803682000-10-28 17:59:06 +0000565
Neil Boothb8af0ca2001-09-26 17:52:50 +0000566 for (;;)
567 {
568 /* Require space for 2 new tokens (including a CPP_EOF). */
Neil Boothece54d52001-09-28 09:40:22 +0000569 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000570 {
Neil Booth8c3b2692001-09-30 10:03:11 +0000571 buff = _cpp_append_extend_buff (pfile, buff,
572 1000 * sizeof (cpp_token *));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000573 arg->first = (const cpp_token **) buff->cur;
574 }
Neil Booth93c803682000-10-28 17:59:06 +0000575
Neil Boothb8af0ca2001-09-26 17:52:50 +0000576 token = cpp_get_token (pfile);
577
578 if (token->type == CPP_PADDING)
579 {
580 /* Drop leading padding. */
581 if (ntokens == 0)
582 continue;
583 }
584 else if (token->type == CPP_OPEN_PAREN)
585 paren_depth++;
586 else if (token->type == CPP_CLOSE_PAREN)
587 {
588 if (paren_depth-- == 0)
589 break;
590 }
591 else if (token->type == CPP_COMMA)
592 {
593 /* A comma does not terminate an argument within
594 parentheses or as part of a variable argument. */
595 if (paren_depth == 0
596 && ! (macro->variadic && argc == macro->paramc))
597 break;
598 }
599 else if (token->type == CPP_EOF
600 || (token->type == CPP_HASH && token->flags & BOL))
601 break;
602
603 arg->first[ntokens++] = token;
604 }
605
606 /* Drop trailing padding. */
607 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
608 ntokens--;
609
610 arg->count = ntokens;
611 arg->first[ntokens] = &pfile->eof;
612
613 /* Terminate the argument. Excess arguments loop back and
614 overwrite the final legitimate argument, before failing. */
615 if (argc <= macro->paramc)
616 {
Neil Boothece54d52001-09-28 09:40:22 +0000617 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000618 if (argc != macro->paramc)
619 arg++;
620 }
Neil Booth93c803682000-10-28 17:59:06 +0000621 }
Neil Boothe808ec92002-02-27 07:24:53 +0000622 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
Neil Booth93c803682000-10-28 17:59:06 +0000623
Neil Boothe808ec92002-02-27 07:24:53 +0000624 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000625 {
Neil Boothece54d52001-09-28 09:40:22 +0000626 /* We still need the CPP_EOF to end directives, and to end
627 pre-expansion of a macro argument. Step back is not
628 unconditional, since we don't want to return a CPP_EOF to our
629 callers at the end of an -include-d file. */
Neil Boothe808ec92002-02-27 07:24:53 +0000630 if (pfile->context->prev || pfile->state.in_directive)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000631 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +0000632 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000633 "unterminated argument list invoking macro \"%s\"",
Neil Bootha28c50352001-05-16 22:02:09 +0000634 NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +0000635 }
Neil Booth1ce676a2002-06-09 20:04:17 +0000636 else
Neil Booth93c803682000-10-28 17:59:06 +0000637 {
Neil Booth1ce676a2002-06-09 20:04:17 +0000638 /* A single empty argument is counted as no argument. */
639 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
640 argc = 0;
641 if (_cpp_arguments_ok (pfile, macro, node, argc))
Neil Booth58551c22002-08-06 20:35:46 +0000642 {
643 /* GCC has special semantics for , ## b where b is a varargs
644 parameter: we remove the comma if b was omitted entirely.
645 If b was merely an empty argument, the comma is retained.
646 If the macro takes just one (varargs) parameter, then we
647 retain the comma only if we are standards conforming.
648
649 If FIRST is NULL replace_args () swallows the comma. */
650 if (macro->variadic && (argc < macro->paramc
651 || (argc == 1 && args[0].count == 0
652 && !CPP_OPTION (pfile, std))))
653 args[macro->paramc - 1].first = NULL;
654 return base_buff;
655 }
Neil Booth93c803682000-10-28 17:59:06 +0000656 }
657
Neil Booth1ce676a2002-06-09 20:04:17 +0000658 /* An error occurred. */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000659 _cpp_release_buff (pfile, base_buff);
660 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000661}
662
Neil Boothd6da8362001-10-08 06:15:14 +0000663/* Search for an opening parenthesis to the macro of NODE, in such a
664 way that, if none is found, we don't lose the information in any
665 intervening padding tokens. If we find the parenthesis, collect
666 the arguments and return the buffer containing them. */
667static _cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000668funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000669{
Neil Boothd6da8362001-10-08 06:15:14 +0000670 const cpp_token *token, *padding = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000671
Neil Boothd6da8362001-10-08 06:15:14 +0000672 for (;;)
Neil Boothbdcbe492001-09-13 20:05:17 +0000673 {
Neil Boothd6da8362001-10-08 06:15:14 +0000674 token = cpp_get_token (pfile);
675 if (token->type != CPP_PADDING)
676 break;
677 if (padding == NULL
678 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
679 padding = token;
Neil Boothbdcbe492001-09-13 20:05:17 +0000680 }
Neil Booth93c803682000-10-28 17:59:06 +0000681
Neil Boothd6da8362001-10-08 06:15:14 +0000682 if (token->type == CPP_OPEN_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000683 {
Neil Boothd6da8362001-10-08 06:15:14 +0000684 pfile->state.parsing_args = 2;
685 return collect_args (pfile, node);
Neil Booth93c803682000-10-28 17:59:06 +0000686 }
687
Neil Booth9ac3b1b2002-04-21 16:17:55 +0000688 /* CPP_EOF can be the end of macro arguments, or the end of the
689 file. We mustn't back up over the latter. Ugh. */
690 if (token->type != CPP_EOF || token == &pfile->eof)
691 {
692 /* Back up. We may have skipped padding, in which case backing
693 up more than one token when expanding macros is in general
694 too difficult. We re-insert it in its own context. */
695 _cpp_backup_tokens (pfile, 1);
696 if (padding)
697 push_token_context (pfile, NULL, padding, 1);
698 }
Neil Boothd6da8362001-10-08 06:15:14 +0000699
700 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000701}
702
Neil Boothd15a58c2002-01-03 18:32:55 +0000703/* Push the context of a macro with hash entry NODE onto the context
704 stack. If we can successfully expand the macro, we push a context
705 containing its yet-to-be-rescanned replacement list and return one.
706 Otherwise, we don't push a context and return zero. */
Neil Booth93c803682000-10-28 17:59:06 +0000707static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000708enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000709{
Neil Boothd15a58c2002-01-03 18:32:55 +0000710 /* The presence of a macro invalidates a file's controlling macro. */
Neil Booth644edda2001-10-02 12:57:24 +0000711 pfile->mi_valid = false;
712
Neil Booth36207112002-05-24 19:26:30 +0000713 pfile->state.angled_headers = false;
714
Neil Boothd15a58c2002-01-03 18:32:55 +0000715 /* Handle standard macros. */
Neil Booth644edda2001-10-02 12:57:24 +0000716 if (! (node->flags & NODE_BUILTIN))
Neil Booth93c803682000-10-28 17:59:06 +0000717 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000718 cpp_macro *macro = node->value.macro;
719
Neil Boothd6da8362001-10-08 06:15:14 +0000720 if (macro->fun_like)
721 {
722 _cpp_buff *buff;
723
724 pfile->state.prevent_expansion++;
725 pfile->keep_tokens++;
726 pfile->state.parsing_args = 1;
727 buff = funlike_invocation_p (pfile, node);
728 pfile->state.parsing_args = 0;
729 pfile->keep_tokens--;
730 pfile->state.prevent_expansion--;
731
732 if (buff == NULL)
733 {
734 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
John David Anglin0527bc42003-11-01 22:56:54 +0000735 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothd6da8362001-10-08 06:15:14 +0000736 "function-like macro \"%s\" must be used with arguments in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +0000737 NODE_NAME (node));
Neil Boothd6da8362001-10-08 06:15:14 +0000738
739 return 0;
740 }
741
Neil Boothe808ec92002-02-27 07:24:53 +0000742 if (macro->paramc > 0)
743 replace_args (pfile, node, macro, (macro_arg *) buff->base);
Neil Boothd6da8362001-10-08 06:15:14 +0000744 _cpp_release_buff (pfile, buff);
745 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000746
747 /* Disable the macro within its expansion. */
Neil Booth644edda2001-10-02 12:57:24 +0000748 node->flags |= NODE_DISABLED;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000749
Neil Bootha69cbaa2002-07-23 22:57:49 +0000750 macro->used = 1;
751
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000752 if (macro->paramc == 0)
Neil Booth601328b2002-05-16 05:53:24 +0000753 push_token_context (pfile, node, macro->exp.tokens, macro->count);
Neil Booth644edda2001-10-02 12:57:24 +0000754
755 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000756 }
Neil Booth644edda2001-10-02 12:57:24 +0000757
Neil Boothd15a58c2002-01-03 18:32:55 +0000758 /* Handle built-in macros and the _Pragma operator. */
Neil Booth644edda2001-10-02 12:57:24 +0000759 return builtin_macro (pfile, node);
Zack Weinberg711b8822000-07-18 00:59:49 +0000760}
761
Neil Boothd15a58c2002-01-03 18:32:55 +0000762/* Replace the parameters in a function-like macro of NODE with the
763 actual ARGS, and place the result in a newly pushed token context.
764 Expand each argument before replacing, unless it is operated upon
765 by the # or ## operators. */
Neil Booth93c803682000-10-28 17:59:06 +0000766static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000767replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
Neil Booth93c803682000-10-28 17:59:06 +0000768{
769 unsigned int i, total;
770 const cpp_token *src, *limit;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000771 const cpp_token **dest, **first;
Neil Booth93c803682000-10-28 17:59:06 +0000772 macro_arg *arg;
Neil Booth1e013d22001-09-26 21:44:35 +0000773 _cpp_buff *buff;
Neil Booth93c803682000-10-28 17:59:06 +0000774
Neil Booth93c803682000-10-28 17:59:06 +0000775 /* First, fully macro-expand arguments, calculating the number of
Neil Booth1e013d22001-09-26 21:44:35 +0000776 tokens in the final expansion as we go. The ordering of the if
777 statements below is subtle; we must handle stringification before
778 pasting. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000779 total = macro->count;
Neil Booth601328b2002-05-16 05:53:24 +0000780 limit = macro->exp.tokens + macro->count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000781
Neil Booth601328b2002-05-16 05:53:24 +0000782 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth93c803682000-10-28 17:59:06 +0000783 if (src->type == CPP_MACRO_ARG)
784 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000785 /* Leading and trailing padding tokens. */
786 total += 2;
787
Neil Booth93c803682000-10-28 17:59:06 +0000788 /* We have an argument. If it is not being stringified or
789 pasted it is macro-replaced before insertion. */
Neil Booth6c53ebf2000-11-06 18:43:32 +0000790 arg = &args[src->val.arg_no - 1];
Neil Booth4c2b6472000-11-11 13:19:01 +0000791
Neil Booth93c803682000-10-28 17:59:06 +0000792 if (src->flags & STRINGIFY_ARG)
793 {
794 if (!arg->stringified)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000795 arg->stringified = stringify_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000796 }
797 else if ((src->flags & PASTE_LEFT)
Neil Booth601328b2002-05-16 05:53:24 +0000798 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
Neil Booth93c803682000-10-28 17:59:06 +0000799 total += arg->count - 1;
800 else
801 {
802 if (!arg->expanded)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000803 expand_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +0000804 total += arg->expanded_count - 1;
805 }
806 }
807
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000808 /* Now allocate space for the expansion, copy the tokens and replace
809 the arguments. */
Neil Booth1e013d22001-09-26 21:44:35 +0000810 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
811 first = (const cpp_token **) buff->base;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000812 dest = first;
Neil Booth93c803682000-10-28 17:59:06 +0000813
Neil Booth601328b2002-05-16 05:53:24 +0000814 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000815 {
816 unsigned int count;
817 const cpp_token **from, **paste_flag;
Neil Booth93c803682000-10-28 17:59:06 +0000818
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000819 if (src->type != CPP_MACRO_ARG)
820 {
821 *dest++ = src;
822 continue;
823 }
824
825 paste_flag = 0;
826 arg = &args[src->val.arg_no - 1];
827 if (src->flags & STRINGIFY_ARG)
828 count = 1, from = &arg->stringified;
829 else if (src->flags & PASTE_LEFT)
830 count = arg->count, from = arg->first;
Neil Booth601328b2002-05-16 05:53:24 +0000831 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000832 {
Neil Booth93c803682000-10-28 17:59:06 +0000833 count = arg->count, from = arg->first;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000834 if (dest != first)
835 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000836 if (dest[-1]->type == CPP_COMMA
837 && macro->variadic
838 && src->val.arg_no == macro->paramc)
839 {
Neil Booth58551c22002-08-06 20:35:46 +0000840 /* Swallow a pasted comma if from == NULL, otherwise
841 drop the paste flag. */
842 if (from == NULL)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000843 dest--;
844 else
845 paste_flag = dest - 1;
846 }
847 /* Remove the paste flag if the RHS is a placemarker. */
848 else if (count == 0)
849 paste_flag = dest - 1;
850 }
851 }
852 else
853 count = arg->expanded_count, from = arg->expanded;
Neil Booth93c803682000-10-28 17:59:06 +0000854
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000855 /* Padding on the left of an argument (unless RHS of ##). */
Zack Weinberga8d0dda2003-02-21 18:06:30 +0000856 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
Neil Booth601328b2002-05-16 05:53:24 +0000857 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000858 *dest++ = padding_token (pfile, src);
Neil Booth93c803682000-10-28 17:59:06 +0000859
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000860 if (count)
861 {
862 memcpy (dest, from, count * sizeof (cpp_token *));
863 dest += count;
Neil Booth93c803682000-10-28 17:59:06 +0000864
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000865 /* With a non-empty argument on the LHS of ##, the last
866 token should be flagged PASTE_LEFT. */
867 if (src->flags & PASTE_LEFT)
868 paste_flag = dest - 1;
869 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000870
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000871 /* Avoid paste on RHS (even case count == 0). */
872 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
873 *dest++ = &pfile->avoid_paste;
Neil Booth26ec42e2001-01-28 11:22:23 +0000874
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000875 /* Add a new paste flag, or remove an unwanted one. */
876 if (paste_flag)
877 {
878 cpp_token *token = _cpp_temp_token (pfile);
879 token->type = (*paste_flag)->type;
Jakub Jelinek73096712005-03-04 16:33:23 +0100880 token->val = (*paste_flag)->val;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000881 if (src->flags & PASTE_LEFT)
882 token->flags = (*paste_flag)->flags | PASTE_LEFT;
883 else
884 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
885 *paste_flag = token;
886 }
887 }
Neil Booth4c2b6472000-11-11 13:19:01 +0000888
Neil Booth93c803682000-10-28 17:59:06 +0000889 /* Free the expanded arguments. */
890 for (i = 0; i < macro->paramc; i++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000891 if (args[i].expanded)
892 free (args[i].expanded);
893
Neil Booth644edda2001-10-02 12:57:24 +0000894 push_ptoken_context (pfile, node, buff, first, dest - first);
Neil Booth93c803682000-10-28 17:59:06 +0000895}
896
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000897/* Return a special padding token, with padding inherited from SOURCE. */
898static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000899padding_token (cpp_reader *pfile, const cpp_token *source)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000900{
901 cpp_token *result = _cpp_temp_token (pfile);
902
903 result->type = CPP_PADDING;
Paolo Bonzinibe0f1e52005-02-14 08:52:24 +0000904
905 /* Data in GCed data structures cannot be made const so far, so we
906 need a cast here. */
907 result->val.source = (cpp_token *) source;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000908 result->flags = 0;
909 return result;
910}
911
Neil Boothd15a58c2002-01-03 18:32:55 +0000912/* Get a new uninitialized context. Create a new one if we cannot
913 re-use an old one. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000914static cpp_context *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000915next_context (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000916{
917 cpp_context *result = pfile->context->next;
918
919 if (result == 0)
920 {
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +0200921 result = XNEW (cpp_context);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000922 result->prev = pfile->context;
923 result->next = 0;
924 pfile->context->next = result;
925 }
926
927 pfile->context = result;
928 return result;
929}
930
931/* Push a list of pointers to tokens. */
932static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000933push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
934 const cpp_token **first, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +0000935{
936 cpp_context *context = next_context (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000937
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000938 context->direct_p = false;
939 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000940 context->buff = buff;
Neil Booth82eda772002-06-04 13:07:06 +0000941 FIRST (context).ptoken = first;
942 LAST (context).ptoken = first + count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000943}
944
945/* Push a list of tokens. */
946static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000947push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
948 const cpp_token *first, unsigned int count)
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000949{
950 cpp_context *context = next_context (pfile);
951
952 context->direct_p = true;
953 context->macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +0000954 context->buff = NULL;
Neil Booth82eda772002-06-04 13:07:06 +0000955 FIRST (context).token = first;
956 LAST (context).token = first + count;
Neil Booth93c803682000-10-28 17:59:06 +0000957}
958
Neil Boothcbc69f82002-06-05 20:27:12 +0000959/* Push a traditional macro's replacement text. */
960void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000961_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
962 const uchar *start, size_t len)
Neil Boothcbc69f82002-06-05 20:27:12 +0000963{
964 cpp_context *context = next_context (pfile);
965
966 context->direct_p = true;
967 context->macro = macro;
968 context->buff = NULL;
969 CUR (context) = start;
Neil Booth1ce676a2002-06-09 20:04:17 +0000970 RLIMIT (context) = start + len;
Neil Booth974c43f2002-06-13 06:25:28 +0000971 macro->flags |= NODE_DISABLED;
Neil Boothcbc69f82002-06-05 20:27:12 +0000972}
973
Neil Boothd15a58c2002-01-03 18:32:55 +0000974/* Expand an argument ARG before replacing parameters in a
975 function-like macro. This works by pushing a context with the
976 argument's tokens, and then expanding that into a temporary buffer
977 as if it were a normal part of the token stream. collect_args()
978 has terminated the argument's tokens with a CPP_EOF so that we know
979 when we have fully expanded the argument. */
Neil Booth93c803682000-10-28 17:59:06 +0000980static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000981expand_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +0000982{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000983 unsigned int capacity;
Neil Booth56941bf2002-09-20 19:44:09 +0000984 bool saved_warn_trad;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000985
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000986 if (arg->count == 0)
987 return;
Neil Booth93c803682000-10-28 17:59:06 +0000988
Neil Booth56941bf2002-09-20 19:44:09 +0000989 /* Don't warn about funlike macros when pre-expanding. */
990 saved_warn_trad = CPP_WTRADITIONAL (pfile);
991 CPP_WTRADITIONAL (pfile) = 0;
992
Neil Booth93c803682000-10-28 17:59:06 +0000993 /* Loop, reading in the arguments. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000994 capacity = 256;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000995 arg->expanded = XNEWVEC (const cpp_token *, capacity);
Neil Booth93c803682000-10-28 17:59:06 +0000996
Neil Booth1e013d22001-09-26 21:44:35 +0000997 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000998 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +0000999 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001000 const cpp_token *token;
1001
1002 if (arg->expanded_count + 1 >= capacity)
Neil Booth93c803682000-10-28 17:59:06 +00001003 {
1004 capacity *= 2;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001005 arg->expanded = XRESIZEVEC (const cpp_token *, arg->expanded,
1006 capacity);
Neil Booth93c803682000-10-28 17:59:06 +00001007 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001008
1009 token = cpp_get_token (pfile);
1010
1011 if (token->type == CPP_EOF)
1012 break;
1013
1014 arg->expanded[arg->expanded_count++] = token;
Neil Booth93c803682000-10-28 17:59:06 +00001015 }
Neil Booth93c803682000-10-28 17:59:06 +00001016
Neil Booth1e013d22001-09-26 21:44:35 +00001017 _cpp_pop_context (pfile);
Neil Booth56941bf2002-09-20 19:44:09 +00001018
1019 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
Neil Booth93c803682000-10-28 17:59:06 +00001020}
1021
Neil Boothd15a58c2002-01-03 18:32:55 +00001022/* Pop the current context off the stack, re-enabling the macro if the
1023 context represented a macro's replacement list. The context
1024 structure is not freed so that we can re-use it later. */
Neil Booth93c803682000-10-28 17:59:06 +00001025void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001026_cpp_pop_context (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001027{
Neil Booth1e013d22001-09-26 21:44:35 +00001028 cpp_context *context = pfile->context;
Neil Booth93c803682000-10-28 17:59:06 +00001029
Neil Booth1e013d22001-09-26 21:44:35 +00001030 if (context->macro)
Neil Booth644edda2001-10-02 12:57:24 +00001031 context->macro->flags &= ~NODE_DISABLED;
Neil Booth1e013d22001-09-26 21:44:35 +00001032
1033 if (context->buff)
1034 _cpp_release_buff (pfile, context->buff);
1035
1036 pfile->context = context->prev;
Neil Booth93c803682000-10-28 17:59:06 +00001037}
1038
Martin Schaffner48c47212003-06-25 23:01:10 +02001039/* External routine to get a token. Also used nearly everywhere
Neil Booth7f2f1a62000-11-14 18:32:06 +00001040 internally, except for places where we know we can safely call
Martin Schaffner48c47212003-06-25 23:01:10 +02001041 _cpp_lex_token directly, such as lexing a directive name.
Neil Booth7f2f1a62000-11-14 18:32:06 +00001042
1043 Macro expansions and directives are transparently handled,
1044 including entering included files. Thus tokens are post-macro
1045 expansion, and after any intervening directives. External callers
1046 see CPP_EOF only at EOF. Internal callers also see it when meeting
1047 a directive inside a macro call, when at the end of a directive and
1048 state.in_directive is still 1, and at the end of argument
1049 pre-expansion. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001050const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001051cpp_get_token (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001052{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001053 const cpp_token *result;
1054
Neil Booth29b10742000-11-13 18:40:37 +00001055 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00001056 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001057 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00001058 cpp_context *context = pfile->context;
1059
Neil Booth93c803682000-10-28 17:59:06 +00001060 /* Context->prev == 0 <=> base context. */
Neil Boothbdcbe492001-09-13 20:05:17 +00001061 if (!context->prev)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001062 result = _cpp_lex_token (pfile);
Neil Booth82eda772002-06-04 13:07:06 +00001063 else if (FIRST (context).token != LAST (context).token)
Neil Booth29b10742000-11-13 18:40:37 +00001064 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001065 if (context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +00001066 result = FIRST (context).token++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001067 else
Neil Booth82eda772002-06-04 13:07:06 +00001068 result = *FIRST (context).ptoken++;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001069
1070 if (result->flags & PASTE_LEFT)
Neil Boothec1a23e2001-01-31 07:48:54 +00001071 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001072 paste_all_tokens (pfile, result);
1073 if (pfile->state.in_directive)
1074 continue;
1075 return padding_token (pfile, result);
Neil Boothec1a23e2001-01-31 07:48:54 +00001076 }
Neil Booth29b10742000-11-13 18:40:37 +00001077 }
Neil Booth93c803682000-10-28 17:59:06 +00001078 else
1079 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001080 _cpp_pop_context (pfile);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001081 if (pfile->state.in_directive)
1082 continue;
1083 return &pfile->avoid_paste;
Neil Booth93c803682000-10-28 17:59:06 +00001084 }
Neil Booth93c803682000-10-28 17:59:06 +00001085
Jason Thorpe477cdac2002-04-07 03:12:23 +00001086 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1087 continue;
1088
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001089 if (result->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +00001090 break;
1091
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001092 node = result->val.node;
Neil Booth4c2b6472000-11-11 13:19:01 +00001093
Neil Booth644edda2001-10-02 12:57:24 +00001094 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1095 break;
Kazu Hiratadf383482002-05-22 22:02:16 +00001096
Neil Booth644edda2001-10-02 12:57:24 +00001097 if (!(node->flags & NODE_DISABLED))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001098 {
Neil Booth644edda2001-10-02 12:57:24 +00001099 if (!pfile->state.prevent_expansion
1100 && enter_macro_context (pfile, node))
Neil Boothbd969772001-02-01 19:13:53 +00001101 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001102 if (pfile->state.in_directive)
1103 continue;
1104 return padding_token (pfile, result);
Neil Boothbd969772001-02-01 19:13:53 +00001105 }
Neil Booth93c803682000-10-28 17:59:06 +00001106 }
Neil Booth644edda2001-10-02 12:57:24 +00001107 else
1108 {
Neil Boothd15a58c2002-01-03 18:32:55 +00001109 /* Flag this token as always unexpandable. FIXME: move this
1110 to collect_args()?. */
Neil Booth644edda2001-10-02 12:57:24 +00001111 cpp_token *t = _cpp_temp_token (pfile);
1112 t->type = result->type;
1113 t->flags = result->flags | NO_EXPAND;
Jakub Jelinek73096712005-03-04 16:33:23 +01001114 t->val = result->val;
Neil Booth644edda2001-10-02 12:57:24 +00001115 result = t;
1116 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00001117
Neil Booth644edda2001-10-02 12:57:24 +00001118 break;
Neil Booth93c803682000-10-28 17:59:06 +00001119 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001120
1121 return result;
Neil Booth93c803682000-10-28 17:59:06 +00001122}
1123
Neil Booth7065e132001-02-14 07:38:20 +00001124/* Returns true if we're expanding an object-like macro that was
1125 defined in a system header. Just checks the macro at the top of
1126 the stack. Used for diagnostic suppression. */
1127int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001128cpp_sys_macro_p (cpp_reader *pfile)
Neil Booth7065e132001-02-14 07:38:20 +00001129{
Neil Booth644edda2001-10-02 12:57:24 +00001130 cpp_hashnode *node = pfile->context->macro;
Neil Booth7065e132001-02-14 07:38:20 +00001131
Neil Booth644edda2001-10-02 12:57:24 +00001132 return node && node->value.macro && node->value.macro->syshdr;
Neil Booth7065e132001-02-14 07:38:20 +00001133}
1134
Neil Boothaf0d16c2002-04-22 17:48:02 +00001135/* Read each token in, until end of the current file. Directives are
1136 transparently processed. */
Neil Booth93c803682000-10-28 17:59:06 +00001137void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001138cpp_scan_nooutput (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00001139{
Per Bothner22234f52004-02-18 14:02:39 -08001140 /* Request a CPP_EOF token at the end of this file, rather than
1141 transparently continuing with the including file. */
1142 pfile->buffer->return_at_eof = true;
1143
Zack Weinbergc6e83802004-06-05 20:58:06 +00001144 pfile->state.discarding_output++;
1145 pfile->state.prevent_expansion++;
1146
Neil Booth590e1982002-07-01 12:47:54 +00001147 if (CPP_OPTION (pfile, traditional))
1148 while (_cpp_read_logical_line_trad (pfile))
1149 ;
1150 else
1151 while (cpp_get_token (pfile)->type != CPP_EOF)
1152 ;
Zack Weinbergc6e83802004-06-05 20:58:06 +00001153
1154 pfile->state.discarding_output--;
1155 pfile->state.prevent_expansion--;
Neil Booth93c803682000-10-28 17:59:06 +00001156}
1157
Neil Boothbdcbe492001-09-13 20:05:17 +00001158/* Step back one (or more) tokens. Can only step mack more than 1 if
1159 they are from the lexer, and not from macro expansion. */
Neil Boothb528a072000-11-12 11:46:21 +00001160void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001161_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +00001162{
Neil Boothbdcbe492001-09-13 20:05:17 +00001163 if (pfile->context->prev == NULL)
1164 {
1165 pfile->lookaheads += count;
1166 while (count--)
1167 {
Neil Booth3293c3e2001-11-19 21:04:49 +00001168 pfile->cur_token--;
1169 if (pfile->cur_token == pfile->cur_run->base
1170 /* Possible with -fpreprocessed and no leading #line. */
1171 && pfile->cur_run->prev != NULL)
Neil Boothbdcbe492001-09-13 20:05:17 +00001172 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001173 pfile->cur_run = pfile->cur_run->prev;
1174 pfile->cur_token = pfile->cur_run->limit;
1175 }
1176 }
1177 }
Neil Booth93c803682000-10-28 17:59:06 +00001178 else
1179 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001180 if (count != 1)
1181 abort ();
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001182 if (pfile->context->direct_p)
Neil Booth82eda772002-06-04 13:07:06 +00001183 FIRST (pfile->context).token--;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001184 else
Neil Booth82eda772002-06-04 13:07:06 +00001185 FIRST (pfile->context).ptoken--;
Neil Booth93c803682000-10-28 17:59:06 +00001186 }
Neil Booth93c803682000-10-28 17:59:06 +00001187}
1188
1189/* #define directive parsing and handling. */
1190
Kazu Hiratada7d8302002-09-22 02:03:17 +00001191/* Returns nonzero if a macro redefinition warning is required. */
Neil Boothcbc69f82002-06-05 20:27:12 +00001192static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001193warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
1194 const cpp_macro *macro2)
Neil Booth93c803682000-10-28 17:59:06 +00001195{
1196 const cpp_macro *macro1;
1197 unsigned int i;
1198
Neil Booth618cdda2001-02-25 09:43:03 +00001199 /* Some redefinitions need to be warned about regardless. */
1200 if (node->flags & NODE_WARN)
Neil Boothcbc69f82002-06-05 20:27:12 +00001201 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001202
Neil Booth618cdda2001-02-25 09:43:03 +00001203 /* Redefinition of a macro is allowed if and only if the old and new
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001204 definitions are the same. (6.10.3 paragraph 2). */
Neil Booth93c803682000-10-28 17:59:06 +00001205 macro1 = node->value.macro;
1206
Neil Booth6618c5d2002-06-10 06:03:13 +00001207 /* Don't check count here as it can be different in valid
1208 traditional redefinitions with just whitespace differences. */
1209 if (macro1->paramc != macro2->paramc
Neil Booth93c803682000-10-28 17:59:06 +00001210 || macro1->fun_like != macro2->fun_like
Neil Booth28e0f042000-12-09 12:06:37 +00001211 || macro1->variadic != macro2->variadic)
Neil Boothcbc69f82002-06-05 20:27:12 +00001212 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001213
1214 /* Check parameter spellings. */
1215 for (i = 0; i < macro1->paramc; i++)
1216 if (macro1->params[i] != macro2->params[i])
Neil Boothcbc69f82002-06-05 20:27:12 +00001217 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001218
Neil Boothcbc69f82002-06-05 20:27:12 +00001219 /* Check the replacement text or tokens. */
1220 if (CPP_OPTION (pfile, traditional))
Neil Booth6618c5d2002-06-10 06:03:13 +00001221 return _cpp_expansions_different_trad (macro1, macro2);
Neil Boothcbc69f82002-06-05 20:27:12 +00001222
DJ Deloriea7f36da2003-06-01 14:55:15 -04001223 if (macro1->count != macro2->count)
1224 return true;
1225
1226 for (i = 0; i < macro1->count; i++)
1227 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1228 return true;
Neil Boothcbc69f82002-06-05 20:27:12 +00001229
1230 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001231}
1232
Neil Booth93c803682000-10-28 17:59:06 +00001233/* Free the definition of hashnode H. */
Neil Booth93c803682000-10-28 17:59:06 +00001234void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001235_cpp_free_definition (cpp_hashnode *h)
Zack Weinberg711b8822000-07-18 00:59:49 +00001236{
Neil Booth93c803682000-10-28 17:59:06 +00001237 /* Macros and assertions no longer have anything to free. */
1238 h->type = NT_VOID;
1239 /* Clear builtin flag in case of redefinition. */
Neil Booth644edda2001-10-02 12:57:24 +00001240 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
Neil Booth93c803682000-10-28 17:59:06 +00001241}
Zack Weinberg711b8822000-07-18 00:59:49 +00001242
Neil Booth14baae02001-09-17 18:26:12 +00001243/* Save parameter NODE to the parameter list of macro MACRO. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00001244 zero on success, nonzero if the parameter is a duplicate. */
Neil Boothc70f6ed2002-06-07 06:26:32 +00001245bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001246_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00001247{
Zack Weinberg4977bab2002-12-16 18:23:00 +00001248 unsigned int len;
Neil Booth93c803682000-10-28 17:59:06 +00001249 /* Constraint 6.10.3.6 - duplicate parameter names. */
Zack Weinberg4977bab2002-12-16 18:23:00 +00001250 if (node->flags & NODE_MACRO_ARG)
Zack Weinberg711b8822000-07-18 00:59:49 +00001251 {
John David Anglin0527bc42003-11-01 22:56:54 +00001252 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00001253 NODE_NAME (node));
Neil Booth23ff0222002-07-17 17:27:14 +00001254 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001255 }
1256
Neil Booth8c3b2692001-09-30 10:03:11 +00001257 if (BUFF_ROOM (pfile->a_buff)
1258 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1259 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +00001260
Neil Booth8c3b2692001-09-30 10:03:11 +00001261 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
Zack Weinberg4977bab2002-12-16 18:23:00 +00001262 node->flags |= NODE_MACRO_ARG;
1263 len = macro->paramc * sizeof (union _cpp_hashnode_value);
1264 if (len > pfile->macro_buffer_len)
1265 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001266 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
1267 len);
Zack Weinberg4977bab2002-12-16 18:23:00 +00001268 pfile->macro_buffer_len = len;
1269 }
1270 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1271 = node->value;
1272
1273 node->value.arg_index = macro->paramc;
Neil Booth23ff0222002-07-17 17:27:14 +00001274 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001275}
1276
Neil Booth23ff0222002-07-17 17:27:14 +00001277/* Check the syntax of the parameters in a MACRO definition. Returns
1278 false if an error occurs. */
1279static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001280parse_params (cpp_reader *pfile, cpp_macro *macro)
Neil Booth93c803682000-10-28 17:59:06 +00001281{
Neil Booth93c803682000-10-28 17:59:06 +00001282 unsigned int prev_ident = 0;
1283
Neil Booth93c803682000-10-28 17:59:06 +00001284 for (;;)
1285 {
Neil Booth345894b2001-09-16 13:44:29 +00001286 const cpp_token *token = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001287
Neil Booth345894b2001-09-16 13:44:29 +00001288 switch (token->type)
Zack Weinberg711b8822000-07-18 00:59:49 +00001289 {
1290 default:
Jason Thorpe477cdac2002-04-07 03:12:23 +00001291 /* Allow/ignore comments in parameter lists if we are
1292 preserving comments in macro expansions. */
1293 if (token->type == CPP_COMMENT
1294 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1295 continue;
1296
John David Anglin0527bc42003-11-01 22:56:54 +00001297 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001298 "\"%s\" may not appear in macro parameter list",
Neil Booth345894b2001-09-16 13:44:29 +00001299 cpp_token_as_text (pfile, token));
Neil Booth23ff0222002-07-17 17:27:14 +00001300 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001301
Zack Weinberg711b8822000-07-18 00:59:49 +00001302 case CPP_NAME:
1303 if (prev_ident)
1304 {
John David Anglin0527bc42003-11-01 22:56:54 +00001305 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001306 "macro parameters must be comma-separated");
Neil Booth23ff0222002-07-17 17:27:14 +00001307 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001308 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001309 prev_ident = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001310
Neil Boothc70f6ed2002-06-07 06:26:32 +00001311 if (_cpp_save_parameter (pfile, macro, token->val.node))
Neil Booth23ff0222002-07-17 17:27:14 +00001312 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001313 continue;
1314
1315 case CPP_CLOSE_PAREN:
Neil Booth93c803682000-10-28 17:59:06 +00001316 if (prev_ident || macro->paramc == 0)
Neil Booth23ff0222002-07-17 17:27:14 +00001317 return true;
Zack Weinberg711b8822000-07-18 00:59:49 +00001318
1319 /* Fall through to pick up the error. */
1320 case CPP_COMMA:
1321 if (!prev_ident)
1322 {
John David Anglin0527bc42003-11-01 22:56:54 +00001323 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
Neil Booth23ff0222002-07-17 17:27:14 +00001324 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001325 }
1326 prev_ident = 0;
1327 continue;
1328
1329 case CPP_ELLIPSIS:
Neil Booth28e0f042000-12-09 12:06:37 +00001330 macro->variadic = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00001331 if (!prev_ident)
1332 {
Neil Boothc70f6ed2002-06-07 06:26:32 +00001333 _cpp_save_parameter (pfile, macro,
1334 pfile->spec_nodes.n__VA_ARGS__);
Neil Booth93c803682000-10-28 17:59:06 +00001335 pfile->state.va_args_ok = 1;
Richard Hendersone5b79212004-02-19 14:18:50 -08001336 if (! CPP_OPTION (pfile, c99)
1337 && CPP_OPTION (pfile, pedantic)
1338 && CPP_OPTION (pfile, warn_variadic_macros))
John David Anglin0527bc42003-11-01 22:56:54 +00001339 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +00001340 "anonymous variadic macros were introduced in C99");
Zack Weinberg711b8822000-07-18 00:59:49 +00001341 }
Richard Hendersone5b79212004-02-19 14:18:50 -08001342 else if (CPP_OPTION (pfile, pedantic)
1343 && CPP_OPTION (pfile, warn_variadic_macros))
John David Anglin0527bc42003-11-01 22:56:54 +00001344 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +00001345 "ISO C does not permit named variadic macros");
Zack Weinberg711b8822000-07-18 00:59:49 +00001346
Neil Booth93c803682000-10-28 17:59:06 +00001347 /* We're at the end, and just expect a closing parenthesis. */
Neil Booth345894b2001-09-16 13:44:29 +00001348 token = _cpp_lex_token (pfile);
1349 if (token->type == CPP_CLOSE_PAREN)
Neil Booth23ff0222002-07-17 17:27:14 +00001350 return true;
Neil Booth93c803682000-10-28 17:59:06 +00001351 /* Fall through. */
1352
1353 case CPP_EOF:
John David Anglin0527bc42003-11-01 22:56:54 +00001354 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
Neil Booth23ff0222002-07-17 17:27:14 +00001355 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00001356 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001357 }
1358}
1359
Neil Booth14baae02001-09-17 18:26:12 +00001360/* Allocate room for a token from a macro's replacement list. */
Neil Booth93c803682000-10-28 17:59:06 +00001361static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001362alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001363{
Neil Booth8c3b2692001-09-30 10:03:11 +00001364 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1365 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
Zack Weinberg711b8822000-07-18 00:59:49 +00001366
Neil Booth8c3b2692001-09-30 10:03:11 +00001367 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
Neil Booth14baae02001-09-17 18:26:12 +00001368}
1369
Neil Boothd15a58c2002-01-03 18:32:55 +00001370/* Lex a token from the expansion of MACRO, but mark parameters as we
1371 find them and warn of traditional stringification. */
Neil Booth14baae02001-09-17 18:26:12 +00001372static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001373lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Neil Booth14baae02001-09-17 18:26:12 +00001374{
1375 cpp_token *token;
1376
1377 pfile->cur_token = alloc_expansion_token (pfile, macro);
1378 token = _cpp_lex_direct (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001379
Neil Boothd15a58c2002-01-03 18:32:55 +00001380 /* Is this a parameter? */
Zack Weinberg4977bab2002-12-16 18:23:00 +00001381 if (token->type == CPP_NAME
1382 && (token->val.node->flags & NODE_MACRO_ARG) != 0)
Zack Weinberg711b8822000-07-18 00:59:49 +00001383 {
Neil Booth93c803682000-10-28 17:59:06 +00001384 token->type = CPP_MACRO_ARG;
Zack Weinberg4977bab2002-12-16 18:23:00 +00001385 token->val.arg_no = token->val.node->value.arg_index;
Zack Weinberg711b8822000-07-18 00:59:49 +00001386 }
Neil Booth93c803682000-10-28 17:59:06 +00001387 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1388 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1389 check_trad_stringification (pfile, macro, &token->val.str);
Zack Weinberg711b8822000-07-18 00:59:49 +00001390
Neil Booth93c803682000-10-28 17:59:06 +00001391 return token;
Zack Weinberg711b8822000-07-18 00:59:49 +00001392}
1393
Neil Boothcbc69f82002-06-05 20:27:12 +00001394static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001395create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00001396{
Neil Boothcbc69f82002-06-05 20:27:12 +00001397 cpp_token *token;
Neil Booth14baae02001-09-17 18:26:12 +00001398 const cpp_token *ctoken;
Zack Weinberg711b8822000-07-18 00:59:49 +00001399
Neil Booth93c803682000-10-28 17:59:06 +00001400 /* Get the first token of the expansion (or the '(' of a
1401 function-like macro). */
Neil Booth14baae02001-09-17 18:26:12 +00001402 ctoken = _cpp_lex_token (pfile);
1403
1404 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
Zack Weinberg711b8822000-07-18 00:59:49 +00001405 {
Neil Boothcbc69f82002-06-05 20:27:12 +00001406 bool ok = parse_params (pfile, macro);
Neil Booth8c3b2692001-09-30 10:03:11 +00001407 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1408 if (!ok)
Neil Boothcbc69f82002-06-05 20:27:12 +00001409 return false;
Neil Booth8c3b2692001-09-30 10:03:11 +00001410
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001411 /* Success. Commit or allocate the parameter array. */
1412 if (pfile->hash_table->alloc_subobject)
1413 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001414 cpp_hashnode **params =
1415 (cpp_hashnode **) pfile->hash_table->alloc_subobject
1416 (sizeof (cpp_hashnode *) * macro->paramc);
Paolo Bonzinibe0f1e52005-02-14 08:52:24 +00001417 memcpy (params, macro->params,
1418 sizeof (cpp_hashnode *) * macro->paramc);
1419 macro->params = params;
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001420 }
1421 else
1422 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
Neil Booth93c803682000-10-28 17:59:06 +00001423 macro->fun_like = 1;
Neil Booth93c803682000-10-28 17:59:06 +00001424 }
Neil Booth14baae02001-09-17 18:26:12 +00001425 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
Jakub Jelinekcae064e2005-04-05 22:07:06 +02001426 {
1427 /* While ISO C99 requires whitespace before replacement text
1428 in a macro definition, ISO C90 with TC1 allows there characters
1429 from the basic source character set. */
1430 if (CPP_OPTION (pfile, c99))
1431 cpp_error (pfile, CPP_DL_PEDWARN,
1432 "ISO C99 requires whitespace after the macro name");
1433 else
1434 {
1435 int warntype = CPP_DL_WARNING;
1436 switch (ctoken->type)
1437 {
1438 case CPP_ATSIGN:
1439 case CPP_AT_NAME:
1440 case CPP_OBJC_STRING:
1441 /* '@' is not in basic character set. */
1442 warntype = CPP_DL_PEDWARN;
1443 break;
1444 case CPP_OTHER:
1445 /* Basic character set sans letters, digits and _. */
1446 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
1447 ctoken->val.str.text[0]) == NULL)
1448 warntype = CPP_DL_PEDWARN;
1449 break;
1450 default:
1451 /* All other tokens start with a character from basic
1452 character set. */
1453 break;
1454 }
1455 cpp_error (pfile, warntype,
1456 "missing whitespace after the macro name");
1457 }
1458 }
Neil Booth93c803682000-10-28 17:59:06 +00001459
Neil Booth14baae02001-09-17 18:26:12 +00001460 if (macro->fun_like)
1461 token = lex_expansion_token (pfile, macro);
1462 else
1463 {
1464 token = alloc_expansion_token (pfile, macro);
1465 *token = *ctoken;
1466 }
Neil Booth93c803682000-10-28 17:59:06 +00001467
1468 for (;;)
1469 {
1470 /* Check the stringifying # constraint 6.10.3.2.1 of
1471 function-like macros when lexing the subsequent token. */
1472 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
Zack Weinberg711b8822000-07-18 00:59:49 +00001473 {
Neil Booth93c803682000-10-28 17:59:06 +00001474 if (token->type == CPP_MACRO_ARG)
1475 {
1476 token->flags &= ~PREV_WHITE;
1477 token->flags |= STRINGIFY_ARG;
1478 token->flags |= token[-1].flags & PREV_WHITE;
1479 token[-1] = token[0];
1480 macro->count--;
1481 }
1482 /* Let assembler get away with murder. */
Neil Boothbdb05a72000-11-26 17:31:13 +00001483 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +00001484 {
John David Anglin0527bc42003-11-01 22:56:54 +00001485 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00001486 "'#' is not followed by a macro parameter");
Neil Boothcbc69f82002-06-05 20:27:12 +00001487 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001488 }
1489 }
1490
1491 if (token->type == CPP_EOF)
1492 break;
1493
1494 /* Paste operator constraint 6.10.3.3.1. */
1495 if (token->type == CPP_PASTE)
1496 {
1497 /* Token-paste ##, can appear in both object-like and
1498 function-like macros, but not at the ends. */
1499 if (--macro->count > 0)
1500 token = lex_expansion_token (pfile, macro);
1501
1502 if (macro->count == 0 || token->type == CPP_EOF)
1503 {
John David Anglin0527bc42003-11-01 22:56:54 +00001504 cpp_error (pfile, CPP_DL_ERROR,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001505 "'##' cannot appear at either end of a macro expansion");
Neil Boothcbc69f82002-06-05 20:27:12 +00001506 return false;
Neil Booth93c803682000-10-28 17:59:06 +00001507 }
1508
1509 token[-1].flags |= PASTE_LEFT;
Neil Booth93c803682000-10-28 17:59:06 +00001510 }
1511
1512 token = lex_expansion_token (pfile, macro);
1513 }
1514
Neil Booth601328b2002-05-16 05:53:24 +00001515 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001516 macro->traditional = 0;
Neil Booth8c3b2692001-09-30 10:03:11 +00001517
Neil Booth4c2b6472000-11-11 13:19:01 +00001518 /* Don't count the CPP_EOF. */
1519 macro->count--;
Neil Booth93c803682000-10-28 17:59:06 +00001520
Neil Boothd15a58c2002-01-03 18:32:55 +00001521 /* Clear whitespace on first token for warn_of_redefinition(). */
Neil Booth8c3b2692001-09-30 10:03:11 +00001522 if (macro->count)
Neil Booth601328b2002-05-16 05:53:24 +00001523 macro->exp.tokens[0].flags &= ~PREV_WHITE;
Neil Booth8c3b2692001-09-30 10:03:11 +00001524
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001525 /* Commit or allocate the memory. */
1526 if (pfile->hash_table->alloc_subobject)
1527 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001528 cpp_token *tokns =
1529 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
1530 * macro->count);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001531 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
1532 macro->exp.tokens = tokns;
1533 }
1534 else
1535 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
Neil Booth8c3b2692001-09-30 10:03:11 +00001536
Neil Boothcbc69f82002-06-05 20:27:12 +00001537 return true;
1538}
Neil Booth44ed91a2000-10-29 11:37:18 +00001539
Kazu Hiratada7d8302002-09-22 02:03:17 +00001540/* Parse a macro and save its expansion. Returns nonzero on success. */
Neil Boothcbc69f82002-06-05 20:27:12 +00001541bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001542_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
Neil Boothcbc69f82002-06-05 20:27:12 +00001543{
1544 cpp_macro *macro;
1545 unsigned int i;
1546 bool ok;
1547
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001548 if (pfile->hash_table->alloc_subobject)
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001549 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
1550 (sizeof (cpp_macro));
Geoffrey Keatingd8044162004-06-09 20:10:13 +00001551 else
1552 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
Neil Boothcbc69f82002-06-05 20:27:12 +00001553 macro->line = pfile->directive_line;
1554 macro->params = 0;
1555 macro->paramc = 0;
1556 macro->variadic = 0;
Neil Booth23345bb2003-03-14 21:47:50 +00001557 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
Neil Boothcbc69f82002-06-05 20:27:12 +00001558 macro->count = 0;
1559 macro->fun_like = 0;
Neil Booth7065e132001-02-14 07:38:20 +00001560 /* To suppress some diagnostics. */
Per Bothner12f9df42004-02-11 07:29:30 -08001561 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
Neil Booth7065e132001-02-14 07:38:20 +00001562
Neil Boothcbc69f82002-06-05 20:27:12 +00001563 if (CPP_OPTION (pfile, traditional))
1564 ok = _cpp_create_trad_definition (pfile, macro);
1565 else
1566 {
1567 cpp_token *saved_cur_token = pfile->cur_token;
1568
1569 ok = create_iso_definition (pfile, macro);
1570
1571 /* Restore lexer position because of games lex_expansion_token()
1572 plays lexing the macro. We set the type for SEEN_EOL() in
Gabriel Dos Reisa2566ae2005-01-02 01:32:21 +00001573 directives.c.
Neil Boothcbc69f82002-06-05 20:27:12 +00001574
1575 Longer term we should lex the whole line before coming here,
1576 and just copy the expansion. */
1577 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1578 pfile->cur_token = saved_cur_token;
1579
1580 /* Stop the lexer accepting __VA_ARGS__. */
1581 pfile->state.va_args_ok = 0;
1582 }
1583
1584 /* Clear the fast argument lookup indices. */
1585 for (i = macro->paramc; i-- > 0; )
Zack Weinberg4977bab2002-12-16 18:23:00 +00001586 {
1587 struct cpp_hashnode *node = macro->params[i];
1588 node->flags &= ~ NODE_MACRO_ARG;
1589 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1590 }
Neil Boothcbc69f82002-06-05 20:27:12 +00001591
1592 if (!ok)
1593 return ok;
1594
Neil Boothc2734e02002-07-26 16:29:31 +00001595 if (node->type == NT_MACRO)
Neil Booth93c803682000-10-28 17:59:06 +00001596 {
Neil Bootha69cbaa2002-07-23 22:57:49 +00001597 if (CPP_OPTION (pfile, warn_unused_macros))
1598 _cpp_warn_if_unused_macro (pfile, node, NULL);
1599
Neil Boothcbc69f82002-06-05 20:27:12 +00001600 if (warn_of_redefinition (pfile, node, macro))
Neil Booth93c803682000-10-28 17:59:06 +00001601 {
John David Anglin0527bc42003-11-01 22:56:54 +00001602 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
Neil Boothebef4e82002-04-14 18:42:47 +00001603 "\"%s\" redefined", NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +00001604
Neil Booth618cdda2001-02-25 09:43:03 +00001605 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
John David Anglin0527bc42003-11-01 22:56:54 +00001606 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
Neil Boothcbc69f82002-06-05 20:27:12 +00001607 node->value.macro->line, 0,
1608 "this is the location of the previous definition");
Zack Weinberg711b8822000-07-18 00:59:49 +00001609 }
Zack Weinberg711b8822000-07-18 00:59:49 +00001610 }
1611
Neil Boothc2734e02002-07-26 16:29:31 +00001612 if (node->type != NT_VOID)
1613 _cpp_free_definition (node);
1614
Zack Weinberg711b8822000-07-18 00:59:49 +00001615 /* Enter definition in hash table. */
Neil Booth93c803682000-10-28 17:59:06 +00001616 node->type = NT_MACRO;
1617 node->value.macro = macro;
Neil Bootha28c50352001-05-16 22:02:09 +00001618 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
Neil Booth618cdda2001-02-25 09:43:03 +00001619 node->flags |= NODE_WARN;
Zack Weinberg711b8822000-07-18 00:59:49 +00001620
Neil Booth93c803682000-10-28 17:59:06 +00001621 return ok;
Zack Weinberg711b8822000-07-18 00:59:49 +00001622}
1623
Neil Boothd15a58c2002-01-03 18:32:55 +00001624/* Warn if a token in STRING matches one of a function-like MACRO's
1625 parameters. */
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001626static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001627check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1628 const cpp_string *string)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001629{
Neil Booth93c803682000-10-28 17:59:06 +00001630 unsigned int i, len;
Neil Booth6338b352003-04-23 22:44:06 +00001631 const uchar *p, *q, *limit;
Kazu Hiratadf383482002-05-22 22:02:16 +00001632
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001633 /* Loop over the string. */
Neil Booth6338b352003-04-23 22:44:06 +00001634 limit = string->text + string->len - 1;
1635 for (p = string->text + 1; p < limit; p = q)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001636 {
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001637 /* Find the start of an identifier. */
Greg McGary61c16b12000-09-15 21:25:02 +00001638 while (p < limit && !is_idstart (*p))
1639 p++;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001640
1641 /* Find the end of the identifier. */
1642 q = p;
Greg McGary61c16b12000-09-15 21:25:02 +00001643 while (q < limit && is_idchar (*q))
1644 q++;
Neil Booth93c803682000-10-28 17:59:06 +00001645
1646 len = q - p;
1647
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001648 /* Loop over the function macro arguments to see if the
1649 identifier inside the string matches one of them. */
Neil Booth93c803682000-10-28 17:59:06 +00001650 for (i = 0; i < macro->paramc; i++)
1651 {
1652 const cpp_hashnode *node = macro->params[i];
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001653
Neil Booth2a967f32001-05-20 06:26:45 +00001654 if (NODE_LEN (node) == len
1655 && !memcmp (p, NODE_NAME (node), len))
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001656 {
John David Anglin0527bc42003-11-01 22:56:54 +00001657 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinbergf458d1d2002-02-27 18:48:07 +00001658 "macro argument \"%s\" would be stringified in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +00001659 NODE_NAME (node));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00001660 break;
1661 }
1662 }
1663 }
1664}
Neil Booth93c803682000-10-28 17:59:06 +00001665
Neil Booth70961712001-06-23 11:34:41 +00001666/* Returns the name, arguments and expansion of a macro, in a format
1667 suitable to be read back in again, and therefore also for DWARF 2
1668 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1669 Caller is expected to generate the "#define" bit if needed. The
Neil Booth93c803682000-10-28 17:59:06 +00001670 returned text is temporary, and automatically freed later. */
Neil Booth93c803682000-10-28 17:59:06 +00001671const unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001672cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00001673{
1674 unsigned int i, len;
1675 const cpp_macro *macro = node->value.macro;
1676 unsigned char *buffer;
1677
1678 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1679 {
John David Anglin0527bc42003-11-01 22:56:54 +00001680 cpp_error (pfile, CPP_DL_ICE,
Neil Boothebef4e82002-04-14 18:42:47 +00001681 "invalid hash type %d in cpp_macro_definition", node->type);
Neil Booth93c803682000-10-28 17:59:06 +00001682 return 0;
1683 }
1684
1685 /* Calculate length. */
Neil Booth8dc901d2002-05-29 19:30:07 +00001686 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
Neil Booth93c803682000-10-28 17:59:06 +00001687 if (macro->fun_like)
1688 {
Jim Blandy64d08262002-04-05 00:12:40 +00001689 len += 4; /* "()" plus possible final ".." of named
1690 varargs (we have + 1 below). */
Neil Booth93c803682000-10-28 17:59:06 +00001691 for (i = 0; i < macro->paramc; i++)
Jim Blandy64d08262002-04-05 00:12:40 +00001692 len += NODE_LEN (macro->params[i]) + 1; /* "," */
Neil Booth93c803682000-10-28 17:59:06 +00001693 }
1694
Eric Christopher6da55c02005-02-15 23:18:04 +00001695 /* This should match below where we fill in the buffer. */
Neil Booth278c4662002-06-19 05:40:08 +00001696 if (CPP_OPTION (pfile, traditional))
1697 len += _cpp_replacement_text_len (macro);
1698 else
Neil Booth93c803682000-10-28 17:59:06 +00001699 {
Neil Booth278c4662002-06-19 05:40:08 +00001700 for (i = 0; i < macro->count; i++)
1701 {
1702 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00001703
Neil Booth278c4662002-06-19 05:40:08 +00001704 if (token->type == CPP_MACRO_ARG)
1705 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1706 else
Eric Christopher6da55c02005-02-15 23:18:04 +00001707 len += cpp_token_len (token);
1708
Neil Booth278c4662002-06-19 05:40:08 +00001709 if (token->flags & STRINGIFY_ARG)
1710 len++; /* "#" */
1711 if (token->flags & PASTE_LEFT)
1712 len += 3; /* " ##" */
Eric Christopher6da55c02005-02-15 23:18:04 +00001713 if (token->flags & PREV_WHITE)
1714 len++; /* " " */
Neil Booth278c4662002-06-19 05:40:08 +00001715 }
Neil Booth93c803682000-10-28 17:59:06 +00001716 }
1717
1718 if (len > pfile->macro_buffer_len)
Alexandre Oliva4b49c362001-01-09 09:30:43 +00001719 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001720 pfile->macro_buffer = XRESIZEVEC (unsigned char,
1721 pfile->macro_buffer, len);
Alexandre Oliva4b49c362001-01-09 09:30:43 +00001722 pfile->macro_buffer_len = len;
1723 }
Neil Booth70961712001-06-23 11:34:41 +00001724
1725 /* Fill in the buffer. Start with the macro name. */
Neil Booth93c803682000-10-28 17:59:06 +00001726 buffer = pfile->macro_buffer;
Neil Booth70961712001-06-23 11:34:41 +00001727 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1728 buffer += NODE_LEN (node);
Neil Booth93c803682000-10-28 17:59:06 +00001729
1730 /* Parameter names. */
1731 if (macro->fun_like)
1732 {
1733 *buffer++ = '(';
1734 for (i = 0; i < macro->paramc; i++)
1735 {
1736 cpp_hashnode *param = macro->params[i];
1737
1738 if (param != pfile->spec_nodes.n__VA_ARGS__)
1739 {
Neil Bootha28c50352001-05-16 22:02:09 +00001740 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1741 buffer += NODE_LEN (param);
Neil Booth93c803682000-10-28 17:59:06 +00001742 }
1743
1744 if (i + 1 < macro->paramc)
Kazu Hiratadf383482002-05-22 22:02:16 +00001745 /* Don't emit a space after the comma here; we're trying
1746 to emit a Dwarf-friendly definition, and the Dwarf spec
1747 forbids spaces in the argument list. */
Jim Blandy64d08262002-04-05 00:12:40 +00001748 *buffer++ = ',';
Neil Booth28e0f042000-12-09 12:06:37 +00001749 else if (macro->variadic)
Neil Booth93c803682000-10-28 17:59:06 +00001750 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1751 }
1752 *buffer++ = ')';
1753 }
1754
Jim Blandye37b38d2002-03-19 21:43:39 +00001755 /* The Dwarf spec requires a space after the macro name, even if the
1756 definition is the empty string. */
1757 *buffer++ = ' ';
1758
Neil Booth278c4662002-06-19 05:40:08 +00001759 if (CPP_OPTION (pfile, traditional))
1760 buffer = _cpp_copy_replacement_text (macro, buffer);
1761 else if (macro->count)
Neil Booth93c803682000-10-28 17:59:06 +00001762 /* Expansion tokens. */
Neil Booth93c803682000-10-28 17:59:06 +00001763 {
Neil Booth93c803682000-10-28 17:59:06 +00001764 for (i = 0; i < macro->count; i++)
1765 {
Neil Booth601328b2002-05-16 05:53:24 +00001766 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00001767
1768 if (token->flags & PREV_WHITE)
1769 *buffer++ = ' ';
1770 if (token->flags & STRINGIFY_ARG)
1771 *buffer++ = '#';
1772
1773 if (token->type == CPP_MACRO_ARG)
1774 {
Neil Bootha28c50352001-05-16 22:02:09 +00001775 memcpy (buffer,
Eric Christopher6da55c02005-02-15 23:18:04 +00001776 NODE_NAME (macro->params[token->val.arg_no - 1]),
1777 NODE_LEN (macro->params[token->val.arg_no - 1]));
1778 buffer += NODE_LEN (macro->params[token->val.arg_no - 1]);
Neil Booth93c803682000-10-28 17:59:06 +00001779 }
1780 else
Geoffrey Keating47e20492005-03-12 10:44:06 +00001781 buffer = cpp_spell_token (pfile, token, buffer, false);
Neil Booth93c803682000-10-28 17:59:06 +00001782
1783 if (token->flags & PASTE_LEFT)
1784 {
1785 *buffer++ = ' ';
1786 *buffer++ = '#';
1787 *buffer++ = '#';
1788 /* Next has PREV_WHITE; see _cpp_create_definition. */
1789 }
1790 }
1791 }
1792
1793 *buffer = '\0';
1794 return pfile->macro_buffer;
1795}