blob: d96b2631a86d033ecc5526e41c81cdf132ad3cac [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,
Tom Tromeyee380362007-01-30 15:46:01 +00003 1999, 2000, 2001, 2002, 2003, 2004, 2005,
Tom Tromey92582b72011-10-17 09:59:12 +00004 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
Zack Weinberg711b8822000-07-18 00:59:49 +00005 Written by Per Bothner, 1994.
6 Based on CCCP program by Paul Rubin, June 1986
7 Adapted to ANSI C, Richard Stallman, Jan 1987
8
9This program is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
Jakub Jelinek748086b2009-04-09 17:00:19 +020011Free Software Foundation; either version 3, or (at your option) any
Zack Weinberg711b8822000-07-18 00:59:49 +000012later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
Jakub Jelinek748086b2009-04-09 17:00:19 +020020along with this program; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>.
Zack Weinberg711b8822000-07-18 00:59:49 +000022
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding! */
26
27#include "config.h"
28#include "system.h"
29#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000030#include "internal.h"
Zack Weinberg711b8822000-07-18 00:59:49 +000031
Neil Booth93c803682000-10-28 17:59:06 +000032typedef struct macro_arg macro_arg;
Tom Tromey92582b72011-10-17 09:59:12 +000033/* This structure represents the tokens of a macro argument. These
34 tokens can be macro themselves, in which case they can be either
35 expanded or unexpanded. When they are expanded, this data
36 structure keeps both the expanded and unexpanded forms. */
Neil Booth93c803682000-10-28 17:59:06 +000037struct macro_arg
38{
Neil Booth4ed5bcf2001-09-24 22:53:12 +000039 const cpp_token **first; /* First token in unexpanded argument. */
Kazu Hirata6d2f8882001-10-10 11:33:39 +000040 const cpp_token **expanded; /* Macro-expanded argument. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +000041 const cpp_token *stringified; /* Stringified argument. */
Neil Booth93c803682000-10-28 17:59:06 +000042 unsigned int count; /* # of tokens in argument. */
43 unsigned int expanded_count; /* # of tokens in expanded argument. */
Tom Tromey92582b72011-10-17 09:59:12 +000044 source_location *virt_locs; /* Where virtual locations for
45 unexpanded tokens are stored. */
46 source_location *expanded_virt_locs; /* Where virtual locations for
47 expanded tokens are
48 stored. */
49};
50
51/* The kind of macro tokens which the instance of
52 macro_arg_token_iter is supposed to iterate over. */
53enum macro_arg_token_kind {
54 MACRO_ARG_TOKEN_NORMAL,
55 /* This is a macro argument token that got transformed into a string
56 litteral, e.g. #foo. */
57 MACRO_ARG_TOKEN_STRINGIFIED,
58 /* This is a token resulting from the expansion of a macro
59 argument that was itself a macro. */
60 MACRO_ARG_TOKEN_EXPANDED
61};
62
63/* An iterator over tokens coming from a function-like macro
64 argument. */
65typedef struct macro_arg_token_iter macro_arg_token_iter;
66struct macro_arg_token_iter
67{
68 /* Whether or not -ftrack-macro-expansion is used. */
69 bool track_macro_exp_p;
70 /* The kind of token over which we are supposed to iterate. */
71 enum macro_arg_token_kind kind;
72 /* A pointer to the current token pointed to by the iterator. */
73 const cpp_token **token_ptr;
74 /* A pointer to the "full" location of the current token. If
75 -ftrack-macro-expansion is used this location tracks loci accross
76 macro expansion. */
77 const source_location *location_ptr;
78#ifdef ENABLE_CHECKING
79 /* The number of times the iterator went forward. This useful only
80 when checking is enabled. */
81 size_t num_forwards;
82#endif
Neil Booth93c803682000-10-28 17:59:06 +000083};
Zack Weinberg711b8822000-07-18 00:59:49 +000084
Neil Booth93c803682000-10-28 17:59:06 +000085/* Macro expansion. */
86
Jakub Jelinek765d6002008-01-25 10:01:27 +010087static int enter_macro_context (cpp_reader *, cpp_hashnode *,
Tom Tromey92582b72011-10-17 09:59:12 +000088 const cpp_token *, source_location);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000089static int builtin_macro (cpp_reader *, cpp_hashnode *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000090static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
91 const cpp_token **, unsigned int);
Tom Tromey92582b72011-10-17 09:59:12 +000092static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
93 _cpp_buff *, source_location *,
94 const cpp_token **, unsigned int);
Jakub Jelinek765d6002008-01-25 10:01:27 +010095static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
Tom Tromey92582b72011-10-17 09:59:12 +000096 _cpp_buff **, unsigned *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000097static cpp_context *next_context (cpp_reader *);
98static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
99static void expand_arg (cpp_reader *, macro_arg *);
100static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
101static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
102static void paste_all_tokens (cpp_reader *, const cpp_token *);
103static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
Tom Tromey92582b72011-10-17 09:59:12 +0000104static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
105static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
106static void delete_macro_args (_cpp_buff*, unsigned num_args);
107static void set_arg_token (macro_arg *, const cpp_token *,
108 source_location, size_t,
109 enum macro_arg_token_kind,
110 bool);
111static const source_location *get_arg_token_location (const macro_arg *,
112 enum macro_arg_token_kind);
113static const cpp_token **arg_token_ptr_at (const macro_arg *,
114 size_t,
115 enum macro_arg_token_kind,
116 source_location **virt_location);
117
118static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
119 enum macro_arg_token_kind,
120 const macro_arg *,
121 const cpp_token **);
122static const cpp_token *macro_arg_token_iter_get_token
123(const macro_arg_token_iter *it);
124static source_location macro_arg_token_iter_get_location
125(const macro_arg_token_iter *);
126static void macro_arg_token_iter_forward (macro_arg_token_iter *);
127static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
128 source_location **);
129static size_t tokens_buff_count (_cpp_buff *);
130static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
Dodji Seketeli9b554be2011-12-05 09:20:59 +0000131static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
132 source_location *,
133 const cpp_token *,
134 source_location,
135 source_location,
136 const struct line_map *,
137 unsigned int);
Tom Tromey92582b72011-10-17 09:59:12 +0000138
139static const cpp_token **tokens_buff_add_token (_cpp_buff *,
140 source_location *,
141 const cpp_token *,
142 source_location,
143 source_location,
144 const struct line_map *,
145 unsigned int);
Dodji Seketeli9b554be2011-12-05 09:20:59 +0000146static inline void tokens_buff_remove_last_token (_cpp_buff *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000147static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
Tom Tromey92582b72011-10-17 09:59:12 +0000148 macro_arg *, source_location);
Jakub Jelinek765d6002008-01-25 10:01:27 +0100149static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
Tom Tromey92582b72011-10-17 09:59:12 +0000150 _cpp_buff **, unsigned *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000151static bool create_iso_definition (cpp_reader *, cpp_macro *);
Neil Booth93c803682000-10-28 17:59:06 +0000152
Neil Booth93c803682000-10-28 17:59:06 +0000153/* #define directive parsing and handling. */
154
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000155static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
156static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
Jakub Jelinek8e680db2010-06-11 20:37:34 +0200157static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000158 const cpp_macro *);
159static bool parse_params (cpp_reader *, cpp_macro *);
160static void check_trad_stringification (cpp_reader *, const cpp_macro *,
161 const cpp_string *);
Tom Tromey92582b72011-10-17 09:59:12 +0000162static bool reached_end_of_context (cpp_context *);
163static void consume_next_token_from_context (cpp_reader *pfile,
164 const cpp_token **,
165 source_location *);
166static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
Zack Weinberg711b8822000-07-18 00:59:49 +0000167
Tom Tromey64a1a422011-10-17 09:59:52 +0000168/* Statistical counter tracking the number of macros that got
169 expanded. */
170unsigned num_expanded_macros_counter = 0;
171/* Statistical counter tracking the total number tokens resulting
172 from macro expansion. */
173unsigned num_macro_tokens_counter = 0;
174
Neil Bootha69cbaa2002-07-23 22:57:49 +0000175/* Emits a warning if NODE is a macro defined in the main file that
176 has not been used. */
177int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000178_cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
179 void *v ATTRIBUTE_UNUSED)
Neil Bootha69cbaa2002-07-23 22:57:49 +0000180{
181 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
182 {
183 cpp_macro *macro = node->value.macro;
184
185 if (!macro->used
Per Bothner50f59cd2004-01-19 21:30:18 -0800186 && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000187 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
188 "macro \"%s\" is not used", NODE_NAME (node));
Neil Bootha69cbaa2002-07-23 22:57:49 +0000189 }
190
191 return 1;
192}
193
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000194/* Allocates and returns a CPP_STRING token, containing TEXT of length
195 LEN, after null-terminating it. TEXT must be in permanent storage. */
196static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000197new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
Zack Weinberg711b8822000-07-18 00:59:49 +0000198{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000199 cpp_token *token = _cpp_temp_token (pfile);
Zack Weinberg711b8822000-07-18 00:59:49 +0000200
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000201 text[len] = '\0';
Neil Booth93c803682000-10-28 17:59:06 +0000202 token->type = CPP_STRING;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000203 token->val.str.len = len;
204 token->val.str.text = text;
Neil Booth93c803682000-10-28 17:59:06 +0000205 token->flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000206 return token;
Neil Booth93c803682000-10-28 17:59:06 +0000207}
208
Neil Booth93c803682000-10-28 17:59:06 +0000209static const char * const monthnames[] =
210{
211 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
212 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
213};
214
Zack Weinberg21b11492004-09-09 19:16:56 +0000215/* Helper function for builtin_macro. Returns the text generated by
216 a builtin macro. */
Neil Booth278c4662002-06-19 05:40:08 +0000217const uchar *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000218_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000219{
Per Bothner12f9df42004-02-11 07:29:30 -0800220 const struct line_map *map;
Neil Booth278c4662002-06-19 05:40:08 +0000221 const uchar *result = NULL;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000222 linenum_type number = 1;
Neil Booth644edda2001-10-02 12:57:24 +0000223
Neil Booth93c803682000-10-28 17:59:06 +0000224 switch (node->value.builtin)
225 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000226 default:
John David Anglin0527bc42003-11-01 22:56:54 +0000227 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +0000228 NODE_NAME (node));
Neil Booth278c4662002-06-19 05:40:08 +0000229 break;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000230
Grigory Zagorodnevbe8ac3e2006-02-18 09:25:31 +0000231 case BT_TIMESTAMP:
232 {
233 cpp_buffer *pbuffer = cpp_get_buffer (pfile);
234 if (pbuffer->timestamp == NULL)
235 {
236 /* Initialize timestamp value of the assotiated file. */
237 struct _cpp_file *file = cpp_get_file (pbuffer);
238 if (file)
239 {
240 /* Generate __TIMESTAMP__ string, that represents
241 the date and time of the last modification
242 of the current source file. The string constant
243 looks like "Sun Sep 16 01:03:52 1973". */
244 struct tm *tb = NULL;
245 struct stat *st = _cpp_get_file_stat (file);
246 if (st)
247 tb = localtime (&st->st_mtime);
248 if (tb)
249 {
250 char *str = asctime (tb);
251 size_t len = strlen (str);
252 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
253 buf[0] = '"';
254 strcpy ((char *) buf + 1, str);
255 buf[len] = '"';
256 pbuffer->timestamp = buf;
257 }
258 else
259 {
260 cpp_errno (pfile, CPP_DL_WARNING,
261 "could not determine file timestamp");
Kris Van Heesb6baa672008-04-18 13:58:08 +0000262 pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
Grigory Zagorodnevbe8ac3e2006-02-18 09:25:31 +0000263 }
264 }
265 }
266 result = pbuffer->timestamp;
267 }
268 break;
Neil Booth93c803682000-10-28 17:59:06 +0000269 case BT_FILE:
270 case BT_BASE_FILE:
Zack Weinberg711b8822000-07-18 00:59:49 +0000271 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000272 unsigned int len;
Neil Booth0bda4762000-12-11 07:45:16 +0000273 const char *name;
Neil Booth562a5c22002-04-21 18:46:42 +0000274 uchar *buf;
Tom Tromey46427372011-10-17 11:58:56 +0200275
276 if (node->value.builtin == BT_FILE)
277 name = linemap_get_expansion_filename (pfile->line_table,
278 pfile->line_table->highest_line);
279 else
280 {
281 map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
282 while (! MAIN_FILE_P (map))
283 map = INCLUDED_FROM (pfile->line_table, map);
284 name = ORDINARY_MAP_FILE_NAME (map);
285 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000286 len = strlen (name);
Andrew Pinski651ed942005-11-04 00:23:01 +0000287 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
Neil Booth278c4662002-06-19 05:40:08 +0000288 result = buf;
289 *buf = '"';
290 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
291 *buf++ = '"';
292 *buf = '\0';
Zack Weinberg711b8822000-07-18 00:59:49 +0000293 }
Neil Booth644edda2001-10-02 12:57:24 +0000294 break;
295
Neil Booth93c803682000-10-28 17:59:06 +0000296 case BT_INCLUDE_LEVEL:
Neil Boothd8693c62001-08-21 23:05:12 +0000297 /* The line map depth counts the primary source as level 1, but
298 historically __INCLUDE_DEPTH__ has called the primary source
299 level 0. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800300 number = pfile->line_table->depth - 1;
Neil Booth644edda2001-10-02 12:57:24 +0000301 break;
Neil Booth93c803682000-10-28 17:59:06 +0000302
303 case BT_SPECLINE:
Tom Tromey46427372011-10-17 11:58:56 +0200304 map = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table);
Neil Booth93c803682000-10-28 17:59:06 +0000305 /* If __LINE__ is embedded in a macro, it must expand to the
306 line of the macro's invocation, not its definition.
307 Otherwise things like assert() will not work properly. */
Tom Tromey46427372011-10-17 11:58:56 +0200308 number = linemap_get_expansion_line (pfile->line_table,
309 CPP_OPTION (pfile, traditional)
310 ? pfile->line_table->highest_line
311 : pfile->cur_token[-1].src_loc);
Neil Booth644edda2001-10-02 12:57:24 +0000312 break;
Neil Booth93c803682000-10-28 17:59:06 +0000313
Zack Weinberg5279d732002-05-16 19:03:02 +0000314 /* __STDC__ has the value 1 under normal circumstances.
315 However, if (a) we are in a system header, (b) the option
Zack Weinberg2a1dc0d2002-05-21 21:55:37 +0000316 stdc_0_in_system_headers is true (set by target config), and
317 (c) we are not in strictly conforming mode, then it has the
Jakub Jelinek83900992006-01-23 22:50:15 +0100318 value 0. (b) and (c) are already checked in cpp_init_builtins. */
Neil Booth93c803682000-10-28 17:59:06 +0000319 case BT_STDC:
Jakub Jelinek83900992006-01-23 22:50:15 +0100320 if (cpp_in_system_header (pfile))
321 number = 0;
322 else
323 number = 1;
Neil Booth644edda2001-10-02 12:57:24 +0000324 break;
Neil Booth93c803682000-10-28 17:59:06 +0000325
326 case BT_DATE:
327 case BT_TIME:
Neil Booth278c4662002-06-19 05:40:08 +0000328 if (pfile->date == NULL)
Neil Booth93c803682000-10-28 17:59:06 +0000329 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000330 /* Allocate __DATE__ and __TIME__ strings from permanent
331 storage. We only do this once, and don't generate them
332 at init time, because time() and localtime() are very
333 slow on some systems. */
Zack Weinberg56da7202002-08-02 04:18:16 +0000334 time_t tt;
335 struct tm *tb = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000336
Zack Weinberg56da7202002-08-02 04:18:16 +0000337 /* (time_t) -1 is a legitimate value for "number of seconds
338 since the Epoch", so we have to do a little dance to
339 distinguish that from a genuine error. */
340 errno = 0;
341 tt = time(NULL);
342 if (tt != (time_t)-1 || errno == 0)
343 tb = localtime (&tt);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000344
Zack Weinberg56da7202002-08-02 04:18:16 +0000345 if (tb)
346 {
347 pfile->date = _cpp_unaligned_alloc (pfile,
348 sizeof ("\"Oct 11 1347\""));
349 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000350 monthnames[tb->tm_mon], tb->tm_mday,
351 tb->tm_year + 1900);
Zack Weinberg56da7202002-08-02 04:18:16 +0000352
353 pfile->time = _cpp_unaligned_alloc (pfile,
354 sizeof ("\"12:34:56\""));
355 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
356 tb->tm_hour, tb->tm_min, tb->tm_sec);
357 }
358 else
359 {
John David Anglin0527bc42003-11-01 22:56:54 +0000360 cpp_errno (pfile, CPP_DL_WARNING,
Zack Weinberg56da7202002-08-02 04:18:16 +0000361 "could not determine date and time");
362
Kris Van Heesb6baa672008-04-18 13:58:08 +0000363 pfile->date = UC"\"??? ?? ????\"";
364 pfile->time = UC"\"??:??:??\"";
Zack Weinberg56da7202002-08-02 04:18:16 +0000365 }
Neil Booth93c803682000-10-28 17:59:06 +0000366 }
Neil Booth93c803682000-10-28 17:59:06 +0000367
Neil Booth644edda2001-10-02 12:57:24 +0000368 if (node->value.builtin == BT_DATE)
Neil Booth278c4662002-06-19 05:40:08 +0000369 result = pfile->date;
Neil Booth644edda2001-10-02 12:57:24 +0000370 else
Neil Booth278c4662002-06-19 05:40:08 +0000371 result = pfile->time;
Neil Booth644edda2001-10-02 12:57:24 +0000372 break;
Ollie Wilda7020452007-05-24 20:55:36 +0000373
374 case BT_COUNTER:
Ollie Wildccfc4c92007-07-30 18:29:20 +0000375 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
376 cpp_error (pfile, CPP_DL_ERROR,
377 "__COUNTER__ expanded inside directive with -fdirectives-only");
Ollie Wilda7020452007-05-24 20:55:36 +0000378 number = pfile->counter++;
379 break;
Neil Booth278c4662002-06-19 05:40:08 +0000380 }
Neil Booth644edda2001-10-02 12:57:24 +0000381
Neil Booth278c4662002-06-19 05:40:08 +0000382 if (result == NULL)
383 {
384 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
385 result = _cpp_unaligned_alloc (pfile, 21);
386 sprintf ((char *) result, "%u", number);
387 }
388
389 return result;
390}
391
392/* Convert builtin macros like __FILE__ to a token and push it on the
Zack Weinberg21b11492004-09-09 19:16:56 +0000393 context stack. Also handles _Pragma, for which a new token may not
394 be created. Returns 1 if it generates a new token context, 0 to
Neil Booth278c4662002-06-19 05:40:08 +0000395 return the token to the caller. */
396static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000397builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth278c4662002-06-19 05:40:08 +0000398{
399 const uchar *buf;
Neil Booth26aea072003-04-19 00:22:51 +0000400 size_t len;
401 char *nbuf;
Neil Booth278c4662002-06-19 05:40:08 +0000402
403 if (node->value.builtin == BT_PRAGMA)
404 {
Neil Booth644edda2001-10-02 12:57:24 +0000405 /* Don't interpret _Pragma within directives. The standard is
406 not clear on this, but to me this makes most sense. */
407 if (pfile->state.in_directive)
408 return 0;
409
Tom Tromey5b9a40d2007-10-31 14:50:13 +0000410 return _cpp_do__Pragma (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000411 }
Neil Booth644edda2001-10-02 12:57:24 +0000412
Neil Booth278c4662002-06-19 05:40:08 +0000413 buf = _cpp_builtin_macro_text (pfile, node);
Neil Booth26aea072003-04-19 00:22:51 +0000414 len = ustrlen (buf);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000415 nbuf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +0000416 memcpy (nbuf, buf, len);
417 nbuf[len]='\n';
Neil Booth278c4662002-06-19 05:40:08 +0000418
Per Bothner40de9f72003-10-02 07:30:34 +0000419 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
Neil Booth26aea072003-04-19 00:22:51 +0000420 _cpp_clean_line (pfile);
Neil Booth278c4662002-06-19 05:40:08 +0000421
422 /* Set pfile->cur_token as required by _cpp_lex_direct. */
423 pfile->cur_token = _cpp_temp_token (pfile);
Richard Hendersonbc4071d2006-01-04 08:33:38 -0800424 _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
Neil Booth278c4662002-06-19 05:40:08 +0000425 if (pfile->buffer->cur != pfile->buffer->rlimit)
John David Anglin0527bc42003-11-01 22:56:54 +0000426 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
Neil Booth278c4662002-06-19 05:40:08 +0000427 NODE_NAME (node));
428 _cpp_pop_buffer (pfile);
429
Neil Booth644edda2001-10-02 12:57:24 +0000430 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000431}
432
Neil Boothd15a58c2002-01-03 18:32:55 +0000433/* Copies SRC, of length LEN, to DEST, adding backslashes before all
Andrew Pinski651ed942005-11-04 00:23:01 +0000434 backslashes and double quotes. DEST must be of sufficient size.
435 Returns a pointer to the end of the string. */
Neil Booth562a5c22002-04-21 18:46:42 +0000436uchar *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000437cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
Neil Booth93c803682000-10-28 17:59:06 +0000438{
439 while (len--)
440 {
Neil Booth562a5c22002-04-21 18:46:42 +0000441 uchar c = *src++;
Neil Booth93c803682000-10-28 17:59:06 +0000442
443 if (c == '\\' || c == '"')
444 {
445 *dest++ = '\\';
446 *dest++ = c;
447 }
448 else
Andrew Pinski651ed942005-11-04 00:23:01 +0000449 *dest++ = c;
Neil Booth93c803682000-10-28 17:59:06 +0000450 }
451
452 return dest;
453}
454
Neil Boothd15a58c2002-01-03 18:32:55 +0000455/* Convert a token sequence ARG to a single string token according to
456 the rules of the ISO C #-operator. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000457static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000458stringify_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +0000459{
Neil Booth6338b352003-04-23 22:44:06 +0000460 unsigned char *dest;
Neil Boothece54d52001-09-28 09:40:22 +0000461 unsigned int i, escape_it, backslash_count = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000462 const cpp_token *source = NULL;
Neil Boothece54d52001-09-28 09:40:22 +0000463 size_t len;
Neil Booth93c803682000-10-28 17:59:06 +0000464
Neil Booth6338b352003-04-23 22:44:06 +0000465 if (BUFF_ROOM (pfile->u_buff) < 3)
466 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
467 dest = BUFF_FRONT (pfile->u_buff);
468 *dest++ = '"';
469
Neil Booth93c803682000-10-28 17:59:06 +0000470 /* Loop, reading in the argument's tokens. */
471 for (i = 0; i < arg->count; i++)
472 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000473 const cpp_token *token = arg->first[i];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000474
475 if (token->type == CPP_PADDING)
476 {
Joseph Myers18f41a12009-04-12 23:20:02 +0100477 if (source == NULL
478 || (!(source->flags & PREV_WHITE)
479 && token->val.source == NULL))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000480 source = token->val.source;
481 continue;
482 }
Neil Booth93c803682000-10-28 17:59:06 +0000483
Kris Van Heesb6baa672008-04-18 13:58:08 +0000484 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
Ian Lance Taylorfd2ab212009-09-02 17:35:30 +0000485 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
Kris Van Heesb6baa672008-04-18 13:58:08 +0000486 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +0200487 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
488 || token->type == CPP_UTF8STRING);
Neil Booth93c803682000-10-28 17:59:06 +0000489
Neil Boothece54d52001-09-28 09:40:22 +0000490 /* Room for each char being written in octal, initial space and
Neil Booth6338b352003-04-23 22:44:06 +0000491 final quote and NUL. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000492 len = cpp_token_len (token);
Neil Booth93c803682000-10-28 17:59:06 +0000493 if (escape_it)
Neil Booth93c803682000-10-28 17:59:06 +0000494 len *= 4;
Neil Booth6338b352003-04-23 22:44:06 +0000495 len += 3;
Neil Booth93c803682000-10-28 17:59:06 +0000496
Neil Boothece54d52001-09-28 09:40:22 +0000497 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth93c803682000-10-28 17:59:06 +0000498 {
Neil Boothece54d52001-09-28 09:40:22 +0000499 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +0000500 _cpp_extend_buff (pfile, &pfile->u_buff, len);
Neil Boothece54d52001-09-28 09:40:22 +0000501 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth93c803682000-10-28 17:59:06 +0000502 }
503
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000504 /* Leading white space? */
Neil Booth6338b352003-04-23 22:44:06 +0000505 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000506 {
507 if (source == NULL)
508 source = token;
509 if (source->flags & PREV_WHITE)
510 *dest++ = ' ';
511 }
512 source = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000513
514 if (escape_it)
515 {
Neil Boothece54d52001-09-28 09:40:22 +0000516 _cpp_buff *buff = _cpp_get_buff (pfile, len);
517 unsigned char *buf = BUFF_FRONT (buff);
Geoffrey Keating47e20492005-03-12 10:44:06 +0000518 len = cpp_spell_token (pfile, token, buf, true) - buf;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000519 dest = cpp_quote_string (dest, buf, len);
Neil Boothece54d52001-09-28 09:40:22 +0000520 _cpp_release_buff (pfile, buff);
Neil Booth93c803682000-10-28 17:59:06 +0000521 }
522 else
Geoffrey Keating47e20492005-03-12 10:44:06 +0000523 dest = cpp_spell_token (pfile, token, dest, true);
Neil Booth93c803682000-10-28 17:59:06 +0000524
Neil Booth10676942003-04-22 19:28:00 +0000525 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
Neil Booth93c803682000-10-28 17:59:06 +0000526 backslash_count++;
527 else
528 backslash_count = 0;
529 }
530
531 /* Ignore the final \ of invalid string literals. */
532 if (backslash_count & 1)
533 {
John David Anglin0527bc42003-11-01 22:56:54 +0000534 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000535 "invalid string literal, ignoring final '\\'");
Neil Boothece54d52001-09-28 09:40:22 +0000536 dest--;
Neil Booth93c803682000-10-28 17:59:06 +0000537 }
538
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000539 /* Commit the memory, including NUL, and return the token. */
Neil Booth6338b352003-04-23 22:44:06 +0000540 *dest++ = '"';
Neil Boothece54d52001-09-28 09:40:22 +0000541 len = dest - BUFF_FRONT (pfile->u_buff);
542 BUFF_FRONT (pfile->u_buff) = dest + 1;
543 return new_string_token (pfile, dest - len, len);
Neil Booth93c803682000-10-28 17:59:06 +0000544}
545
Kazu Hiratada7d8302002-09-22 02:03:17 +0000546/* Try to paste two tokens. On success, return nonzero. In any
Neil Boothc9e7a602001-09-27 12:59:38 +0000547 case, PLHS is updated to point to the pasted token, which is
548 guaranteed to not have the PASTE_LEFT flag set. */
549static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000550paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
Neil Boothd63eefb2000-11-20 23:59:26 +0000551{
Jakub Jelinekde000d22006-10-12 11:25:59 +0200552 unsigned char *buf, *end, *lhsend;
Tom Tromeyfca35e12007-05-02 19:33:44 +0000553 cpp_token *lhs;
Neil Boothc9e7a602001-09-27 12:59:38 +0000554 unsigned int len;
Neil Boothd63eefb2000-11-20 23:59:26 +0000555
Tom Tromeyfca35e12007-05-02 19:33:44 +0000556 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000557 buf = (unsigned char *) alloca (len);
Tom Tromeyfca35e12007-05-02 19:33:44 +0000558 end = lhsend = cpp_spell_token (pfile, *plhs, buf, false);
Neil Boothd63eefb2000-11-20 23:59:26 +0000559
Neil Boothc9e7a602001-09-27 12:59:38 +0000560 /* Avoid comment headers, since they are still processed in stage 3.
561 It is simpler to insert a space here, rather than modifying the
562 lexer to ignore comments in some circumstances. Simply returning
563 false doesn't work, since we want to clear the PASTE_LEFT flag. */
Tom Tromeyfca35e12007-05-02 19:33:44 +0000564 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
Neil Boothc9e7a602001-09-27 12:59:38 +0000565 *end++ = ' ';
Tom Tromeyf373b442007-11-01 18:20:48 +0000566 /* In one obscure case we might see padding here. */
567 if (rhs->type != CPP_PADDING)
568 end = cpp_spell_token (pfile, rhs, end, false);
Neil Booth26aea072003-04-19 00:22:51 +0000569 *end = '\n';
Neil Boothd63eefb2000-11-20 23:59:26 +0000570
Per Bothner40de9f72003-10-02 07:30:34 +0000571 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
Neil Booth26aea072003-04-19 00:22:51 +0000572 _cpp_clean_line (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000573
Neil Boothc9e7a602001-09-27 12:59:38 +0000574 /* Set pfile->cur_token as required by _cpp_lex_direct. */
575 pfile->cur_token = _cpp_temp_token (pfile);
Tom Tromeyfca35e12007-05-02 19:33:44 +0000576 lhs = _cpp_lex_direct (pfile);
Jakub Jelinekde000d22006-10-12 11:25:59 +0200577 if (pfile->buffer->cur != pfile->buffer->rlimit)
578 {
Tom Tromeyfca35e12007-05-02 19:33:44 +0000579 source_location saved_loc = lhs->src_loc;
580
Jakub Jelinekde000d22006-10-12 11:25:59 +0200581 _cpp_pop_buffer (pfile);
582 _cpp_backup_tokens (pfile, 1);
583 *lhsend = '\0';
Neil Boothd63eefb2000-11-20 23:59:26 +0000584
Tom Tromeyfca35e12007-05-02 19:33:44 +0000585 /* We have to remove the PASTE_LEFT flag from the old lhs, but
586 we want to keep the new location. */
587 *lhs = **plhs;
588 *plhs = lhs;
589 lhs->src_loc = saved_loc;
590 lhs->flags &= ~PASTE_LEFT;
591
Jakub Jelinekde000d22006-10-12 11:25:59 +0200592 /* Mandatory error for all apart from assembler. */
593 if (CPP_OPTION (pfile, lang) != CLK_ASM)
594 cpp_error (pfile, CPP_DL_ERROR,
595 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
596 buf, cpp_token_as_text (pfile, rhs));
597 return false;
598 }
599
Tom Tromeyfca35e12007-05-02 19:33:44 +0000600 *plhs = lhs;
Jakub Jelinekde000d22006-10-12 11:25:59 +0200601 _cpp_pop_buffer (pfile);
602 return true;
Neil Boothd63eefb2000-11-20 23:59:26 +0000603}
604
Neil Boothd15a58c2002-01-03 18:32:55 +0000605/* Handles an arbitrarily long sequence of ## operators, with initial
606 operand LHS. This implementation is left-associative,
607 non-recursive, and finishes a paste before handling succeeding
608 ones. If a paste fails, we back up to the RHS of the failing ##
609 operator before pushing the context containing the result of prior
610 successful pastes, with the effect that the RHS appears in the
611 output stream after the pasted LHS normally. */
Neil Booth93c803682000-10-28 17:59:06 +0000612static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000613paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
Neil Booth93c803682000-10-28 17:59:06 +0000614{
Tom Tromey92582b72011-10-17 09:59:12 +0000615 const cpp_token *rhs = NULL;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000616 cpp_context *context = pfile->context;
617
Neil Booth93c803682000-10-28 17:59:06 +0000618 do
619 {
620 /* Take the token directly from the current context. We can do
621 this, because we are in the replacement list of either an
622 object-like macro, or a function-like macro with arguments
623 inserted. In either case, the constraints to #define
Neil Boothd63eefb2000-11-20 23:59:26 +0000624 guarantee we have at least one more token. */
Tom Tromey92582b72011-10-17 09:59:12 +0000625 if (context->tokens_kind == TOKENS_KIND_DIRECT)
Neil Booth82eda772002-06-04 13:07:06 +0000626 rhs = FIRST (context).token++;
Tom Tromey92582b72011-10-17 09:59:12 +0000627 else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
Neil Booth82eda772002-06-04 13:07:06 +0000628 rhs = *FIRST (context).ptoken++;
Tom Tromey92582b72011-10-17 09:59:12 +0000629 else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
630 {
631 /* So we are in presence of an extended token context, which
632 means that each token in this context has a virtual
633 location attached to it. So let's not forget to update
634 the pointer to the current virtual location of the
635 current token when we update the pointer to the current
636 token */
637
638 rhs = *FIRST (context).ptoken++;
639 /* context->c.mc must be non-null, as if we were not in a
640 macro context, context->tokens_kind could not be equal to
641 TOKENS_KIND_EXTENDED. */
642 context->c.mc->cur_virt_loc++;
643 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000644
645 if (rhs->type == CPP_PADDING)
Tom Tromeyf373b442007-11-01 18:20:48 +0000646 {
647 if (rhs->flags & PASTE_LEFT)
648 abort ();
649 }
Neil Boothc9e7a602001-09-27 12:59:38 +0000650 if (!paste_tokens (pfile, &lhs, rhs))
Jakub Jelinekde000d22006-10-12 11:25:59 +0200651 break;
Neil Booth93c803682000-10-28 17:59:06 +0000652 }
653 while (rhs->flags & PASTE_LEFT);
654
Neil Boothc9e7a602001-09-27 12:59:38 +0000655 /* Put the resulting token in its own context. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -0800656 _cpp_push_token_context (pfile, NULL, lhs, 1);
Neil Booth93c803682000-10-28 17:59:06 +0000657}
658
Neil Booth1ce676a2002-06-09 20:04:17 +0000659/* Returns TRUE if the number of arguments ARGC supplied in an
660 invocation of the MACRO referenced by NODE is valid. An empty
661 invocation to a macro with no parameters should pass ARGC as zero.
662
663 Note that MACRO cannot necessarily be deduced from NODE, in case
664 NODE was redefined whilst collecting arguments. */
665bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000666_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
Neil Booth1ce676a2002-06-09 20:04:17 +0000667{
668 if (argc == macro->paramc)
669 return true;
670
671 if (argc < macro->paramc)
672 {
673 /* As an extension, a rest argument is allowed to not appear in
674 the invocation at all.
675 e.g. #define debug(format, args...) something
676 debug("string");
677
678 This is exactly the same as if there had been an empty rest
679 argument - debug("string", ). */
680
681 if (argc + 1 == macro->paramc && macro->variadic)
682 {
683 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
John David Anglin0527bc42003-11-01 22:56:54 +0000684 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth1ce676a2002-06-09 20:04:17 +0000685 "ISO C99 requires rest arguments to be used");
686 return true;
687 }
688
John David Anglin0527bc42003-11-01 22:56:54 +0000689 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1ce676a2002-06-09 20:04:17 +0000690 "macro \"%s\" requires %u arguments, but only %u given",
691 NODE_NAME (node), macro->paramc, argc);
692 }
693 else
John David Anglin0527bc42003-11-01 22:56:54 +0000694 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1ce676a2002-06-09 20:04:17 +0000695 "macro \"%s\" passed %u arguments, but takes just %u",
696 NODE_NAME (node), argc, macro->paramc);
697
698 return false;
699}
700
Neil Boothd15a58c2002-01-03 18:32:55 +0000701/* Reads and returns the arguments to a function-like macro
702 invocation. Assumes the opening parenthesis has been processed.
703 If there is an error, emits an appropriate diagnostic and returns
704 NULL. Each argument is terminated by a CPP_EOF token, for the
Jakub Jelinek765d6002008-01-25 10:01:27 +0100705 future benefit of expand_arg(). If there are any deferred
706 #pragma directives among macro arguments, store pointers to the
Tom Tromey92582b72011-10-17 09:59:12 +0000707 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
708
709 What is returned is the buffer that contains the memory allocated
710 to hold the macro arguments. NODE is the name of the macro this
711 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
712 set to the actual number of macro arguments allocated in the
713 returned buffer. */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000714static _cpp_buff *
Jakub Jelinek765d6002008-01-25 10:01:27 +0100715collect_args (cpp_reader *pfile, const cpp_hashnode *node,
Tom Tromey92582b72011-10-17 09:59:12 +0000716 _cpp_buff **pragma_buff, unsigned *num_args)
Neil Booth93c803682000-10-28 17:59:06 +0000717{
Neil Boothb8af0ca2001-09-26 17:52:50 +0000718 _cpp_buff *buff, *base_buff;
719 cpp_macro *macro;
720 macro_arg *args, *arg;
721 const cpp_token *token;
722 unsigned int argc;
Tom Tromey92582b72011-10-17 09:59:12 +0000723 source_location virt_loc;
724 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
725 unsigned num_args_alloced = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000726
Neil Boothb8af0ca2001-09-26 17:52:50 +0000727 macro = node->value.macro;
728 if (macro->paramc)
729 argc = macro->paramc;
730 else
731 argc = 1;
Tom Tromey92582b72011-10-17 09:59:12 +0000732
733#define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
734#define ARG_TOKENS_EXTENT 1000
735
736 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
737 * sizeof (cpp_token *)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000738 + sizeof (macro_arg)));
739 base_buff = buff;
740 args = (macro_arg *) buff->base;
741 memset (args, 0, argc * sizeof (macro_arg));
Neil Boothece54d52001-09-28 09:40:22 +0000742 buff->cur = (unsigned char *) &args[argc];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000743 arg = args, argc = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000744
Neil Boothb8af0ca2001-09-26 17:52:50 +0000745 /* Collect the tokens making up each argument. We don't yet know
746 how many arguments have been supplied, whether too many or too
747 few. Hence the slightly bizarre usage of "argc" and "arg". */
748 do
Neil Booth93c803682000-10-28 17:59:06 +0000749 {
Neil Boothb8af0ca2001-09-26 17:52:50 +0000750 unsigned int paren_depth = 0;
751 unsigned int ntokens = 0;
Tom Tromey92582b72011-10-17 09:59:12 +0000752 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
753 num_args_alloced++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000754
Neil Booth93c803682000-10-28 17:59:06 +0000755 argc++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000756 arg->first = (const cpp_token **) buff->cur;
Tom Tromey92582b72011-10-17 09:59:12 +0000757 if (track_macro_expansion_p)
758 {
759 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
760 arg->virt_locs = XNEWVEC (source_location,
761 virt_locs_capacity);
762 }
Neil Booth93c803682000-10-28 17:59:06 +0000763
Neil Boothb8af0ca2001-09-26 17:52:50 +0000764 for (;;)
765 {
766 /* Require space for 2 new tokens (including a CPP_EOF). */
Neil Boothece54d52001-09-28 09:40:22 +0000767 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000768 {
Neil Booth8c3b2692001-09-30 10:03:11 +0000769 buff = _cpp_append_extend_buff (pfile, buff,
Tom Tromey92582b72011-10-17 09:59:12 +0000770 ARG_TOKENS_EXTENT
771 * sizeof (cpp_token *));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000772 arg->first = (const cpp_token **) buff->cur;
773 }
Tom Tromey92582b72011-10-17 09:59:12 +0000774 if (track_macro_expansion_p
775 && (ntokens + 2 > virt_locs_capacity))
776 {
777 virt_locs_capacity += ARG_TOKENS_EXTENT;
778 arg->virt_locs = XRESIZEVEC (source_location,
779 arg->virt_locs,
780 virt_locs_capacity);
781 }
Neil Booth93c803682000-10-28 17:59:06 +0000782
Tom Tromey92582b72011-10-17 09:59:12 +0000783 token = cpp_get_token_1 (pfile, &virt_loc);
Neil Boothb8af0ca2001-09-26 17:52:50 +0000784
785 if (token->type == CPP_PADDING)
786 {
787 /* Drop leading padding. */
788 if (ntokens == 0)
789 continue;
790 }
791 else if (token->type == CPP_OPEN_PAREN)
792 paren_depth++;
793 else if (token->type == CPP_CLOSE_PAREN)
794 {
795 if (paren_depth-- == 0)
796 break;
797 }
798 else if (token->type == CPP_COMMA)
799 {
800 /* A comma does not terminate an argument within
801 parentheses or as part of a variable argument. */
802 if (paren_depth == 0
803 && ! (macro->variadic && argc == macro->paramc))
804 break;
805 }
806 else if (token->type == CPP_EOF
807 || (token->type == CPP_HASH && token->flags & BOL))
808 break;
Jakub Jelinek765d6002008-01-25 10:01:27 +0100809 else if (token->type == CPP_PRAGMA)
810 {
811 cpp_token *newtok = _cpp_temp_token (pfile);
812
813 /* CPP_PRAGMA token lives in directive_result, which will
814 be overwritten on the next directive. */
815 *newtok = *token;
816 token = newtok;
817 do
818 {
819 if (*pragma_buff == NULL
820 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
821 {
822 _cpp_buff *next;
823 if (*pragma_buff == NULL)
824 *pragma_buff
825 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
826 else
827 {
828 next = *pragma_buff;
829 *pragma_buff
830 = _cpp_get_buff (pfile,
831 (BUFF_FRONT (*pragma_buff)
832 - (*pragma_buff)->base) * 2);
833 (*pragma_buff)->next = next;
834 }
835 }
836 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
837 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
838 if (token->type == CPP_PRAGMA_EOL)
839 break;
Tom Tromey92582b72011-10-17 09:59:12 +0000840 token = cpp_get_token_1 (pfile, &virt_loc);
Jakub Jelinek765d6002008-01-25 10:01:27 +0100841 }
842 while (token->type != CPP_EOF);
843
844 /* In deferred pragmas parsing_args and prevent_expansion
845 had been changed, reset it. */
846 pfile->state.parsing_args = 2;
847 pfile->state.prevent_expansion = 1;
848
849 if (token->type == CPP_EOF)
850 break;
851 else
852 continue;
853 }
Tom Tromey92582b72011-10-17 09:59:12 +0000854 set_arg_token (arg, token, virt_loc,
855 ntokens, MACRO_ARG_TOKEN_NORMAL,
856 CPP_OPTION (pfile, track_macro_expansion));
857 ntokens++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000858 }
859
860 /* Drop trailing padding. */
861 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
862 ntokens--;
863
864 arg->count = ntokens;
Tom Tromey92582b72011-10-17 09:59:12 +0000865 set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
866 ntokens, MACRO_ARG_TOKEN_NORMAL,
867 CPP_OPTION (pfile, track_macro_expansion));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000868
869 /* Terminate the argument. Excess arguments loop back and
870 overwrite the final legitimate argument, before failing. */
871 if (argc <= macro->paramc)
872 {
Neil Boothece54d52001-09-28 09:40:22 +0000873 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000874 if (argc != macro->paramc)
875 arg++;
876 }
Neil Booth93c803682000-10-28 17:59:06 +0000877 }
Neil Boothe808ec92002-02-27 07:24:53 +0000878 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
Neil Booth93c803682000-10-28 17:59:06 +0000879
Neil Boothe808ec92002-02-27 07:24:53 +0000880 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000881 {
Neil Boothece54d52001-09-28 09:40:22 +0000882 /* We still need the CPP_EOF to end directives, and to end
883 pre-expansion of a macro argument. Step back is not
884 unconditional, since we don't want to return a CPP_EOF to our
885 callers at the end of an -include-d file. */
Neil Boothe808ec92002-02-27 07:24:53 +0000886 if (pfile->context->prev || pfile->state.in_directive)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000887 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +0000888 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000889 "unterminated argument list invoking macro \"%s\"",
Neil Bootha28c50352001-05-16 22:02:09 +0000890 NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +0000891 }
Neil Booth1ce676a2002-06-09 20:04:17 +0000892 else
Neil Booth93c803682000-10-28 17:59:06 +0000893 {
Neil Booth1ce676a2002-06-09 20:04:17 +0000894 /* A single empty argument is counted as no argument. */
895 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
896 argc = 0;
897 if (_cpp_arguments_ok (pfile, macro, node, argc))
Neil Booth58551c22002-08-06 20:35:46 +0000898 {
899 /* GCC has special semantics for , ## b where b is a varargs
900 parameter: we remove the comma if b was omitted entirely.
901 If b was merely an empty argument, the comma is retained.
902 If the macro takes just one (varargs) parameter, then we
903 retain the comma only if we are standards conforming.
904
905 If FIRST is NULL replace_args () swallows the comma. */
906 if (macro->variadic && (argc < macro->paramc
907 || (argc == 1 && args[0].count == 0
908 && !CPP_OPTION (pfile, std))))
909 args[macro->paramc - 1].first = NULL;
Tom Tromey92582b72011-10-17 09:59:12 +0000910 if (num_args)
911 *num_args = num_args_alloced;
Neil Booth58551c22002-08-06 20:35:46 +0000912 return base_buff;
913 }
Neil Booth93c803682000-10-28 17:59:06 +0000914 }
915
Neil Booth1ce676a2002-06-09 20:04:17 +0000916 /* An error occurred. */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000917 _cpp_release_buff (pfile, base_buff);
918 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000919}
920
Neil Boothd6da8362001-10-08 06:15:14 +0000921/* Search for an opening parenthesis to the macro of NODE, in such a
922 way that, if none is found, we don't lose the information in any
923 intervening padding tokens. If we find the parenthesis, collect
Jakub Jelinek765d6002008-01-25 10:01:27 +0100924 the arguments and return the buffer containing them. PRAGMA_BUFF
Tom Tromey92582b72011-10-17 09:59:12 +0000925 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
926 *NUM_ARGS is set to the number of arguments contained in the
927 returned buffer. */
Neil Boothd6da8362001-10-08 06:15:14 +0000928static _cpp_buff *
Jakub Jelinek765d6002008-01-25 10:01:27 +0100929funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
Tom Tromey92582b72011-10-17 09:59:12 +0000930 _cpp_buff **pragma_buff, unsigned *num_args)
Neil Booth93c803682000-10-28 17:59:06 +0000931{
Neil Boothd6da8362001-10-08 06:15:14 +0000932 const cpp_token *token, *padding = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000933
Neil Boothd6da8362001-10-08 06:15:14 +0000934 for (;;)
Neil Boothbdcbe492001-09-13 20:05:17 +0000935 {
Neil Boothd6da8362001-10-08 06:15:14 +0000936 token = cpp_get_token (pfile);
937 if (token->type != CPP_PADDING)
938 break;
939 if (padding == NULL
940 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
941 padding = token;
Neil Boothbdcbe492001-09-13 20:05:17 +0000942 }
Neil Booth93c803682000-10-28 17:59:06 +0000943
Neil Boothd6da8362001-10-08 06:15:14 +0000944 if (token->type == CPP_OPEN_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000945 {
Neil Boothd6da8362001-10-08 06:15:14 +0000946 pfile->state.parsing_args = 2;
Tom Tromey92582b72011-10-17 09:59:12 +0000947 return collect_args (pfile, node, pragma_buff, num_args);
Neil Booth93c803682000-10-28 17:59:06 +0000948 }
949
Neil Booth9ac3b1b2002-04-21 16:17:55 +0000950 /* CPP_EOF can be the end of macro arguments, or the end of the
951 file. We mustn't back up over the latter. Ugh. */
952 if (token->type != CPP_EOF || token == &pfile->eof)
953 {
954 /* Back up. We may have skipped padding, in which case backing
955 up more than one token when expanding macros is in general
956 too difficult. We re-insert it in its own context. */
957 _cpp_backup_tokens (pfile, 1);
958 if (padding)
Richard Hendersonbc4071d2006-01-04 08:33:38 -0800959 _cpp_push_token_context (pfile, NULL, padding, 1);
Neil Booth9ac3b1b2002-04-21 16:17:55 +0000960 }
Neil Boothd6da8362001-10-08 06:15:14 +0000961
962 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000963}
964
Joseph Myersaa508502009-04-19 18:10:56 +0100965/* Return the real number of tokens in the expansion of MACRO. */
966static inline unsigned int
967macro_real_token_count (const cpp_macro *macro)
968{
969 unsigned int i;
970 if (__builtin_expect (!macro->extra_tokens, true))
971 return macro->count;
972 for (i = 0; i < macro->count; i++)
973 if (macro->exp.tokens[i].type == CPP_PASTE)
974 return i;
975 abort ();
976}
977
Neil Boothd15a58c2002-01-03 18:32:55 +0000978/* Push the context of a macro with hash entry NODE onto the context
979 stack. If we can successfully expand the macro, we push a context
980 containing its yet-to-be-rescanned replacement list and return one.
Tom Tromey92582b72011-10-17 09:59:12 +0000981 If there were additionally any unexpanded deferred #pragma
982 directives among macro arguments, push another context containing
983 the pragma tokens before the yet-to-be-rescanned replacement list
984 and return two. Otherwise, we don't push a context and return
985 zero. LOCATION is the location of the expansion point of the
986 macro. */
Neil Booth93c803682000-10-28 17:59:06 +0000987static int
Jakub Jelinek765d6002008-01-25 10:01:27 +0100988enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
Tom Tromey92582b72011-10-17 09:59:12 +0000989 const cpp_token *result, source_location location)
Neil Booth93c803682000-10-28 17:59:06 +0000990{
Neil Boothd15a58c2002-01-03 18:32:55 +0000991 /* The presence of a macro invalidates a file's controlling macro. */
Neil Booth644edda2001-10-02 12:57:24 +0000992 pfile->mi_valid = false;
993
Neil Booth36207112002-05-24 19:26:30 +0000994 pfile->state.angled_headers = false;
995
Joseph Myers93d45d92008-04-02 20:42:53 +0100996 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
997 {
998 node->flags |= NODE_USED;
Jakub Jelinek8e680db2010-06-11 20:37:34 +0200999 if ((!pfile->cb.user_builtin_macro
1000 || !pfile->cb.user_builtin_macro (pfile, node))
1001 && pfile->cb.used_define)
Joseph Myers93d45d92008-04-02 20:42:53 +01001002 pfile->cb.used_define (pfile, pfile->directive_line, node);
1003 }
1004
Neil Boothd15a58c2002-01-03 18:32:55 +00001005 /* Handle standard macros. */
Neil Booth644edda2001-10-02 12:57:24 +00001006 if (! (node->flags & NODE_BUILTIN))
Neil Booth93c803682000-10-28 17:59:06 +00001007 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001008 cpp_macro *macro = node->value.macro;
Jakub Jelinek765d6002008-01-25 10:01:27 +01001009 _cpp_buff *pragma_buff = NULL;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001010
Neil Boothd6da8362001-10-08 06:15:14 +00001011 if (macro->fun_like)
1012 {
1013 _cpp_buff *buff;
Tom Tromey92582b72011-10-17 09:59:12 +00001014 unsigned num_args = 0;
Neil Boothd6da8362001-10-08 06:15:14 +00001015
1016 pfile->state.prevent_expansion++;
1017 pfile->keep_tokens++;
1018 pfile->state.parsing_args = 1;
Tom Tromey92582b72011-10-17 09:59:12 +00001019 buff = funlike_invocation_p (pfile, node, &pragma_buff,
1020 &num_args);
Neil Boothd6da8362001-10-08 06:15:14 +00001021 pfile->state.parsing_args = 0;
1022 pfile->keep_tokens--;
1023 pfile->state.prevent_expansion--;
1024
1025 if (buff == NULL)
1026 {
1027 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
Simon Baldwin87cf0652010-04-07 17:18:10 +00001028 cpp_warning (pfile, CPP_W_TRADITIONAL,
Neil Boothd6da8362001-10-08 06:15:14 +00001029 "function-like macro \"%s\" must be used with arguments in traditional C",
Simon Baldwin87cf0652010-04-07 17:18:10 +00001030 NODE_NAME (node));
Neil Boothd6da8362001-10-08 06:15:14 +00001031
Jakub Jelinek765d6002008-01-25 10:01:27 +01001032 if (pragma_buff)
1033 _cpp_release_buff (pfile, pragma_buff);
1034
Neil Boothd6da8362001-10-08 06:15:14 +00001035 return 0;
1036 }
1037
Neil Boothe808ec92002-02-27 07:24:53 +00001038 if (macro->paramc > 0)
Tom Tromey92582b72011-10-17 09:59:12 +00001039 replace_args (pfile, node, macro,
1040 (macro_arg *) buff->base,
1041 location);
1042 /* Free the memory used by the arguments of this
1043 function-like macro. This memory has been allocated by
1044 funlike_invocation_p and by replace_args. */
1045 delete_macro_args (buff, num_args);
Neil Boothd6da8362001-10-08 06:15:14 +00001046 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001047
1048 /* Disable the macro within its expansion. */
Neil Booth644edda2001-10-02 12:57:24 +00001049 node->flags |= NODE_DISABLED;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001050
Joseph Myers93d45d92008-04-02 20:42:53 +01001051 if (!(node->flags & NODE_USED))
1052 {
1053 node->flags |= NODE_USED;
1054 if (pfile->cb.used_define)
1055 pfile->cb.used_define (pfile, pfile->directive_line, node);
1056 }
1057
Arnaud Charlet3de8a542009-11-20 08:18:16 +00001058 if (pfile->cb.used)
Tom Tromey92582b72011-10-17 09:59:12 +00001059 pfile->cb.used (pfile, location, node);
Arnaud Charlet3de8a542009-11-20 08:18:16 +00001060
Neil Bootha69cbaa2002-07-23 22:57:49 +00001061 macro->used = 1;
1062
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001063 if (macro->paramc == 0)
Tom Tromey92582b72011-10-17 09:59:12 +00001064 {
1065 if (CPP_OPTION (pfile, track_macro_expansion))
1066 {
1067 unsigned int i, count = macro->count;
1068 const cpp_token *src = macro->exp.tokens;
1069 const struct line_map *map;
1070 source_location *virt_locs = NULL;
1071 _cpp_buff *macro_tokens =
1072 tokens_buff_new (pfile, count, &virt_locs);
1073
1074 /* Create a macro map to record the locations of the
1075 tokens that are involved in the expansion. LOCATION
1076 is the location of the macro expansion point. */
1077 map = linemap_enter_macro (pfile->line_table,
1078 node, location, count);
1079 for (i = 0; i < count; ++i)
1080 {
1081 tokens_buff_add_token (macro_tokens, virt_locs,
1082 src, src->src_loc,
1083 src->src_loc, map, i);
1084 ++src;
1085 }
1086 push_extended_tokens_context (pfile, node,
1087 macro_tokens,
1088 virt_locs,
1089 (const cpp_token **)
1090 macro_tokens->base,
1091 count);
Tom Tromey64a1a422011-10-17 09:59:52 +00001092 num_macro_tokens_counter += count;
Tom Tromey92582b72011-10-17 09:59:12 +00001093 }
1094 else
Tom Tromey64a1a422011-10-17 09:59:52 +00001095 {
1096 unsigned tokens_count = macro_real_token_count (macro);
1097 _cpp_push_token_context (pfile, node, macro->exp.tokens,
1098 tokens_count);
1099 num_macro_tokens_counter += tokens_count;
1100 }
Tom Tromey92582b72011-10-17 09:59:12 +00001101 }
Neil Booth644edda2001-10-02 12:57:24 +00001102
Jakub Jelinek765d6002008-01-25 10:01:27 +01001103 if (pragma_buff)
1104 {
1105 if (!pfile->state.in_directive)
1106 _cpp_push_token_context (pfile, NULL,
1107 padding_token (pfile, result), 1);
1108 do
1109 {
Tom Tromey64a1a422011-10-17 09:59:52 +00001110 unsigned tokens_count;
Jakub Jelinek765d6002008-01-25 10:01:27 +01001111 _cpp_buff *tail = pragma_buff->next;
1112 pragma_buff->next = NULL;
Tom Tromey64a1a422011-10-17 09:59:52 +00001113 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1114 - (const cpp_token **) pragma_buff->base);
Jakub Jelinek765d6002008-01-25 10:01:27 +01001115 push_ptoken_context (pfile, NULL, pragma_buff,
1116 (const cpp_token **) pragma_buff->base,
Tom Tromey64a1a422011-10-17 09:59:52 +00001117 tokens_count);
Jakub Jelinek765d6002008-01-25 10:01:27 +01001118 pragma_buff = tail;
Tom Tromey64a1a422011-10-17 09:59:52 +00001119 if (!CPP_OPTION (pfile, track_macro_expansion))
1120 num_macro_tokens_counter += tokens_count;
1121
Jakub Jelinek765d6002008-01-25 10:01:27 +01001122 }
1123 while (pragma_buff != NULL);
1124 return 2;
1125 }
1126
Neil Booth644edda2001-10-02 12:57:24 +00001127 return 1;
Neil Booth93c803682000-10-28 17:59:06 +00001128 }
Neil Booth644edda2001-10-02 12:57:24 +00001129
Neil Boothd15a58c2002-01-03 18:32:55 +00001130 /* Handle built-in macros and the _Pragma operator. */
Neil Booth644edda2001-10-02 12:57:24 +00001131 return builtin_macro (pfile, node);
Zack Weinberg711b8822000-07-18 00:59:49 +00001132}
1133
Tom Tromey92582b72011-10-17 09:59:12 +00001134/* De-allocate the memory used by BUFF which is an array of instances
1135 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1136 present in BUFF. */
1137static void
1138delete_macro_args (_cpp_buff *buff, unsigned num_args)
1139{
1140 macro_arg *macro_args;
1141 unsigned i;
1142
1143 if (buff == NULL)
1144 return;
1145
1146 macro_args = (macro_arg *) buff->base;
1147
1148 /* Walk instances of macro_arg to free their expanded tokens as well
1149 as their macro_arg::virt_locs members. */
1150 for (i = 0; i < num_args; ++i)
1151 {
1152 if (macro_args[i].expanded)
1153 {
1154 free (macro_args[i].expanded);
1155 macro_args[i].expanded = NULL;
1156 }
1157 if (macro_args[i].virt_locs)
1158 {
1159 free (macro_args[i].virt_locs);
1160 macro_args[i].virt_locs = NULL;
1161 }
1162 if (macro_args[i].expanded_virt_locs)
1163 {
1164 free (macro_args[i].expanded_virt_locs);
1165 macro_args[i].expanded_virt_locs = NULL;
1166 }
1167 }
1168 _cpp_free_buff (buff);
1169}
1170
1171/* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1172 to set, LOCATION is its virtual location. "Virtual" location means
1173 the location that encodes loci accross macro expansion. Otherwise
1174 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1175 argument ARG is supposed to contain. Note that ARG must be
1176 tailored so that it has enough room to contain INDEX + 1 numbers of
1177 tokens, at least. */
1178static void
1179set_arg_token (macro_arg *arg, const cpp_token *token,
1180 source_location location, size_t index,
1181 enum macro_arg_token_kind kind,
1182 bool track_macro_exp_p)
1183{
1184 const cpp_token **token_ptr;
1185 source_location *loc = NULL;
1186
1187 token_ptr =
1188 arg_token_ptr_at (arg, index, kind,
1189 track_macro_exp_p ? &loc : NULL);
1190 *token_ptr = token;
1191
1192 if (loc != NULL)
1193 {
1194#ifdef ENABLE_CHECKING
1195 if (kind == MACRO_ARG_TOKEN_STRINGIFIED
1196 || !track_macro_exp_p)
1197 /* We can't set the location of a stringified argument
1198 token and we can't set any location if we aren't tracking
1199 macro expansion locations. */
1200 abort ();
1201#endif
1202 *loc = location;
1203 }
1204}
1205
1206/* Get the pointer to the location of the argument token of the
1207 function-like macro argument ARG. This function must be called
1208 only when we -ftrack-macro-expansion is on. */
1209static const source_location *
1210get_arg_token_location (const macro_arg *arg,
1211 enum macro_arg_token_kind kind)
1212{
1213 const source_location *loc = NULL;
1214 const cpp_token **token_ptr =
1215 arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1216
1217 if (token_ptr == NULL)
1218 return NULL;
1219
1220 return loc;
1221}
1222
1223/* Return the pointer to the INDEXth token of the macro argument ARG.
1224 KIND specifies the kind of token the macro argument ARG contains.
1225 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1226 of the virtual location of the returned token if the
1227 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1228 spelling location of the returned token. */
1229static const cpp_token **
1230arg_token_ptr_at (const macro_arg *arg, size_t index,
1231 enum macro_arg_token_kind kind,
1232 source_location **virt_location)
1233{
1234 const cpp_token **tokens_ptr = NULL;
1235
1236 switch (kind)
1237 {
1238 case MACRO_ARG_TOKEN_NORMAL:
1239 tokens_ptr = arg->first;
1240 break;
1241 case MACRO_ARG_TOKEN_STRINGIFIED:
1242 tokens_ptr = (const cpp_token **) &arg->stringified;
1243 break;
1244 case MACRO_ARG_TOKEN_EXPANDED:
1245 tokens_ptr = arg->expanded;
1246 break;
1247 }
1248
1249 if (tokens_ptr == NULL)
1250 /* This can happen for e.g, an empty token argument to a
1251 funtion-like macro. */
1252 return tokens_ptr;
1253
1254 if (virt_location)
1255 {
1256 if (kind == MACRO_ARG_TOKEN_NORMAL)
1257 *virt_location = &arg->virt_locs[index];
1258 else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1259 *virt_location = &arg->expanded_virt_locs[index];
1260 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1261 *virt_location =
1262 (source_location *) &tokens_ptr[index]->src_loc;
1263 }
1264 return &tokens_ptr[index];
1265}
1266
1267/* Initialize an iterator so that it iterates over the tokens of a
1268 function-like macro argument. KIND is the kind of tokens we want
1269 ITER to iterate over. TOKEN_PTR points the first token ITER will
1270 iterate over. */
1271static void
1272macro_arg_token_iter_init (macro_arg_token_iter *iter,
1273 bool track_macro_exp_p,
1274 enum macro_arg_token_kind kind,
1275 const macro_arg *arg,
1276 const cpp_token **token_ptr)
1277{
1278 iter->track_macro_exp_p = track_macro_exp_p;
1279 iter->kind = kind;
1280 iter->token_ptr = token_ptr;
Dodji Seketelid17687f2011-10-18 08:44:49 +00001281 /* Unconditionally initialize this so that the compiler doesn't warn
1282 about iter->location_ptr being possibly uninitialized later after
1283 this code has been inlined somewhere. */
1284 iter->location_ptr = NULL;
Tom Tromey92582b72011-10-17 09:59:12 +00001285 if (track_macro_exp_p)
1286 iter->location_ptr = get_arg_token_location (arg, kind);
1287#ifdef ENABLE_CHECKING
1288 iter->num_forwards = 0;
1289 if (track_macro_exp_p
1290 && token_ptr != NULL
1291 && iter->location_ptr == NULL)
1292 abort ();
1293#endif
1294}
1295
1296/* Move the iterator one token forward. Note that if IT was
1297 initialized on an argument that has a stringified token, moving it
1298 foward doesn't make sense as a stringified token is essentially one
1299 string. */
1300static void
1301macro_arg_token_iter_forward (macro_arg_token_iter *it)
1302{
1303 switch (it->kind)
1304 {
1305 case MACRO_ARG_TOKEN_NORMAL:
1306 case MACRO_ARG_TOKEN_EXPANDED:
1307 it->token_ptr++;
1308 if (it->track_macro_exp_p)
1309 it->location_ptr++;
1310 break;
1311 case MACRO_ARG_TOKEN_STRINGIFIED:
1312#ifdef ENABLE_CHECKING
1313 if (it->num_forwards > 0)
1314 abort ();
1315#endif
1316 break;
1317 }
1318
1319#ifdef ENABLE_CHECKING
1320 it->num_forwards++;
1321#endif
1322}
1323
1324/* Return the token pointed to by the iterator. */
1325static const cpp_token *
1326macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1327{
1328#ifdef ENABLE_CHECKING
1329 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1330 && it->num_forwards > 0)
1331 abort ();
1332#endif
1333 if (it->token_ptr == NULL)
1334 return NULL;
1335 return *it->token_ptr;
1336}
1337
1338/* Return the location of the token pointed to by the iterator.*/
1339static source_location
1340macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1341{
1342#ifdef ENABLE_CHECKING
1343 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1344 && it->num_forwards > 0)
1345 abort ();
1346#endif
1347 if (it->track_macro_exp_p)
1348 return *it->location_ptr;
1349 else
1350 return (*it->token_ptr)->src_loc;
1351}
1352
1353/* Return the index of a token [resulting from macro expansion] inside
1354 the total list of tokens resulting from a given macro
1355 expansion. The index can be different depending on whether if we
1356 want each tokens resulting from function-like macro arguments
1357 expansion to have a different location or not.
1358
1359 E.g, consider this function-like macro:
1360
1361 #define M(x) x - 3
1362
1363 Then consider us "calling" it (and thus expanding it) like:
1364
1365 M(1+4)
1366
1367 It will be expanded into:
1368
1369 1+4-3
1370
1371 Let's consider the case of the token '4'.
1372
1373 Its index can be 2 (it's the third token of the set of tokens
1374 resulting from the expansion) or it can be 0 if we consider that
1375 all tokens resulting from the expansion of the argument "1+2" have
1376 the same index, which is 0. In this later case, the index of token
1377 '-' would then be 1 and the index of token '3' would be 2.
1378
1379 The later case is useful to use less memory e.g, for the case of
1380 the user using the option -ftrack-macro-expansion=1.
1381
1382 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1383 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1384 parameter (inside the macro replacement list) that corresponds to
1385 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1386 of.
1387
1388 If we refer to the example above, for the '4' argument token,
1389 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1390 would be set to the token 'x', in the replacement list "x - 3" of
1391 macro M.
1392
1393 This is a subroutine of replace_args. */
1394inline static unsigned
1395expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1396 const cpp_token *cur_replacement_token,
1397 unsigned absolute_token_index)
1398{
1399 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1400 return absolute_token_index;
1401 return cur_replacement_token - macro->exp.tokens;
1402}
1403
Neil Boothd15a58c2002-01-03 18:32:55 +00001404/* Replace the parameters in a function-like macro of NODE with the
1405 actual ARGS, and place the result in a newly pushed token context.
1406 Expand each argument before replacing, unless it is operated upon
Tom Tromey92582b72011-10-17 09:59:12 +00001407 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1408 the expansion point of the macro. E.g, the location of the
1409 function-like macro invocation. */
Neil Booth93c803682000-10-28 17:59:06 +00001410static void
Tom Tromey92582b72011-10-17 09:59:12 +00001411replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1412 macro_arg *args, source_location expansion_point_loc)
Neil Booth93c803682000-10-28 17:59:06 +00001413{
1414 unsigned int i, total;
1415 const cpp_token *src, *limit;
Tom Tromey92582b72011-10-17 09:59:12 +00001416 const cpp_token **first = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001417 macro_arg *arg;
Tom Tromey92582b72011-10-17 09:59:12 +00001418 _cpp_buff *buff = NULL;
1419 source_location *virt_locs = NULL;
1420 unsigned int exp_count;
1421 const struct line_map *map = NULL;
1422 int track_macro_exp;
Neil Booth93c803682000-10-28 17:59:06 +00001423
Neil Booth93c803682000-10-28 17:59:06 +00001424 /* First, fully macro-expand arguments, calculating the number of
Neil Booth1e013d22001-09-26 21:44:35 +00001425 tokens in the final expansion as we go. The ordering of the if
1426 statements below is subtle; we must handle stringification before
1427 pasting. */
Tom Tromey92582b72011-10-17 09:59:12 +00001428
1429 /* EXP_COUNT is the number of tokens in the macro replacement
1430 list. TOTAL is the number of tokens /after/ macro parameters
1431 have been replaced by their arguments. */
1432 exp_count = macro_real_token_count (macro);
1433 total = exp_count;
1434 limit = macro->exp.tokens + exp_count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001435
Neil Booth601328b2002-05-16 05:53:24 +00001436 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth93c803682000-10-28 17:59:06 +00001437 if (src->type == CPP_MACRO_ARG)
1438 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001439 /* Leading and trailing padding tokens. */
1440 total += 2;
Tom Tromey92582b72011-10-17 09:59:12 +00001441 /* Account for leading and padding tokens in exp_count too.
1442 This is going to be important later down this function,
1443 when we want to handle the case of (track_macro_exp <
1444 2). */
1445 exp_count += 2;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001446
Neil Booth93c803682000-10-28 17:59:06 +00001447 /* We have an argument. If it is not being stringified or
1448 pasted it is macro-replaced before insertion. */
Joseph Myers9a0c6182009-05-10 15:27:32 +01001449 arg = &args[src->val.macro_arg.arg_no - 1];
Neil Booth4c2b6472000-11-11 13:19:01 +00001450
Neil Booth93c803682000-10-28 17:59:06 +00001451 if (src->flags & STRINGIFY_ARG)
1452 {
1453 if (!arg->stringified)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001454 arg->stringified = stringify_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +00001455 }
1456 else if ((src->flags & PASTE_LEFT)
Neil Booth601328b2002-05-16 05:53:24 +00001457 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
Neil Booth93c803682000-10-28 17:59:06 +00001458 total += arg->count - 1;
1459 else
1460 {
1461 if (!arg->expanded)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001462 expand_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +00001463 total += arg->expanded_count - 1;
1464 }
1465 }
1466
Tom Tromey92582b72011-10-17 09:59:12 +00001467 /* When the compiler is called with the -ftrack-macro-expansion
1468 flag, we need to keep track of the location of each token that
1469 results from macro expansion.
Neil Booth93c803682000-10-28 17:59:06 +00001470
Tom Tromey92582b72011-10-17 09:59:12 +00001471 A token resulting from macro expansion is not a new token. It is
1472 simply the same token as the token coming from the macro
1473 definition. The new things that are allocated are the buffer
1474 that holds the tokens resulting from macro expansion and a new
1475 location that records many things like the locus of the expansion
1476 point as well as the original locus inside the definition of the
1477 macro. This location is called a virtual location.
1478
1479 So the buffer BUFF holds a set of cpp_token*, and the buffer
1480 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1481
1482 Both of these two buffers are going to be hung off of the macro
1483 context, when the latter is pushed. The memory allocated to
1484 store the tokens and their locations is going to be freed once
1485 the context of macro expansion is popped.
1486
1487 As far as tokens are concerned, the memory overhead of
1488 -ftrack-macro-expansion is proportional to the number of
1489 macros that get expanded multiplied by sizeof (source_location).
1490 The good news is that extra memory gets freed when the macro
1491 context is freed, i.e shortly after the macro got expanded. */
1492
1493 /* Is the -ftrack-macro-expansion flag in effect? */
1494 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1495
1496 /* Now allocate memory space for tokens and locations resulting from
1497 the macro expansion, copy the tokens and replace the arguments.
1498 This memory must be freed when the context of the macro MACRO is
1499 popped. */
1500 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1501
1502 first = (const cpp_token **) buff->base;
1503
1504 /* Create a macro map to record the locations of the tokens that are
1505 involved in the expansion. Note that the expansion point is set
1506 to the location of the closing parenthesis. Otherwise, the
1507 subsequent map created for the first token that comes after the
1508 macro map might have a wrong line number. That would lead to
1509 tokens with wrong line numbers after the macro expansion. This
1510 adds up to the memory overhead of the -ftrack-macro-expansion
1511 flag; for every macro that is expanded, a "macro map" is
1512 created. */
1513 if (track_macro_exp)
1514 {
1515 int num_macro_tokens = total;
1516 if (track_macro_exp < 2)
1517 /* Then the number of macro tokens won't take in account the
1518 fact that function-like macro arguments can expand to
1519 multiple tokens. This is to save memory at the expense of
1520 accuracy.
1521
1522 Suppose we have #define SQARE(A) A * A
1523
1524 And then we do SQARE(2+3)
1525
1526 Then the tokens 2, +, 3, will have the same location,
1527 saying they come from the expansion of the argument A. */
1528 num_macro_tokens = exp_count;
1529 map = linemap_enter_macro (pfile->line_table, node,
1530 expansion_point_loc,
1531 num_macro_tokens);
1532 }
1533 i = 0;
Neil Booth601328b2002-05-16 05:53:24 +00001534 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001535 {
Tom Tromey92582b72011-10-17 09:59:12 +00001536 unsigned int arg_tokens_count;
1537 macro_arg_token_iter from;
1538 const cpp_token **paste_flag = NULL;
1539 const cpp_token **tmp_token_ptr;
Neil Booth93c803682000-10-28 17:59:06 +00001540
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001541 if (src->type != CPP_MACRO_ARG)
1542 {
Tom Tromey92582b72011-10-17 09:59:12 +00001543 /* Allocate a virtual location for token SRC, and add that
1544 token and its virtual location into the buffers BUFF and
1545 VIRT_LOCS. */
1546 unsigned index = expanded_token_index (pfile, macro, src, i);
1547 tokens_buff_add_token (buff, virt_locs, src,
1548 src->src_loc, src->src_loc,
1549 map, index);
1550 i += 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001551 continue;
1552 }
1553
1554 paste_flag = 0;
Joseph Myers9a0c6182009-05-10 15:27:32 +01001555 arg = &args[src->val.macro_arg.arg_no - 1];
Tom Tromey92582b72011-10-17 09:59:12 +00001556 /* SRC is a macro parameter that we need to replace with its
1557 corresponding argument. So at some point we'll need to
1558 iterate over the tokens of the macro argument and copy them
1559 into the "place" now holding the correspondig macro
1560 parameter. We are going to use the iterator type
1561 macro_argo_token_iter to handle that iterating. The 'if'
1562 below is to initialize the iterator depending on the type of
1563 tokens the macro argument has. It also does some adjustment
1564 related to padding tokens and some pasting corner cases. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001565 if (src->flags & STRINGIFY_ARG)
Tom Tromey92582b72011-10-17 09:59:12 +00001566 {
1567 arg_tokens_count = 1;
1568 macro_arg_token_iter_init (&from,
1569 CPP_OPTION (pfile,
1570 track_macro_expansion),
1571 MACRO_ARG_TOKEN_STRINGIFIED,
1572 arg, &arg->stringified);
1573 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001574 else if (src->flags & PASTE_LEFT)
Tom Tromey92582b72011-10-17 09:59:12 +00001575 {
1576 arg_tokens_count = arg->count;
1577 macro_arg_token_iter_init (&from,
1578 CPP_OPTION (pfile,
1579 track_macro_expansion),
1580 MACRO_ARG_TOKEN_NORMAL,
1581 arg, arg->first);
1582 }
Neil Booth601328b2002-05-16 05:53:24 +00001583 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001584 {
Tom Tromey92582b72011-10-17 09:59:12 +00001585 int num_toks;
1586 arg_tokens_count = arg->count;
1587 macro_arg_token_iter_init (&from,
1588 CPP_OPTION (pfile,
1589 track_macro_expansion),
1590 MACRO_ARG_TOKEN_NORMAL,
1591 arg, arg->first);
1592
1593 num_toks = tokens_buff_count (buff);
1594
1595 if (num_toks != 0)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001596 {
Tom Tromey92582b72011-10-17 09:59:12 +00001597 /* So the current parameter token is pasted to the previous
1598 token in the replacement list. Let's look at what
1599 we have as previous and current arguments. */
1600
1601 /* This is the previous argument's token ... */
1602 tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1603
1604 if ((*tmp_token_ptr)->type == CPP_COMMA
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001605 && macro->variadic
Joseph Myers9a0c6182009-05-10 15:27:32 +01001606 && src->val.macro_arg.arg_no == macro->paramc)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001607 {
Tom Tromey92582b72011-10-17 09:59:12 +00001608 /* ... which is a comma; and the current parameter
1609 is the last parameter of a variadic function-like
1610 macro. If the argument to the current last
1611 parameter is NULL, then swallow the comma,
1612 otherwise drop the paste flag. */
1613 if (macro_arg_token_iter_get_token (&from) == NULL)
1614 tokens_buff_remove_last_token (buff);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001615 else
Tom Tromey92582b72011-10-17 09:59:12 +00001616 paste_flag = tmp_token_ptr;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001617 }
1618 /* Remove the paste flag if the RHS is a placemarker. */
Tom Tromey92582b72011-10-17 09:59:12 +00001619 else if (arg_tokens_count == 0)
1620 paste_flag = tmp_token_ptr;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001621 }
1622 }
1623 else
Tom Tromey92582b72011-10-17 09:59:12 +00001624 {
1625 arg_tokens_count = arg->expanded_count;
1626 macro_arg_token_iter_init (&from,
1627 CPP_OPTION (pfile,
1628 track_macro_expansion),
1629 MACRO_ARG_TOKEN_EXPANDED,
1630 arg, arg->expanded);
1631 }
Neil Booth93c803682000-10-28 17:59:06 +00001632
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001633 /* Padding on the left of an argument (unless RHS of ##). */
Zack Weinberga8d0dda2003-02-21 18:06:30 +00001634 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
Neil Booth601328b2002-05-16 05:53:24 +00001635 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001636 {
Tom Tromey92582b72011-10-17 09:59:12 +00001637 const cpp_token *t = padding_token (pfile, src);
1638 unsigned index = expanded_token_index (pfile, macro, src, i);
1639 /* Allocate a virtual location for the padding token and
1640 append the token and its location to BUFF and
1641 VIRT_LOCS. */
1642 tokens_buff_add_token (buff, virt_locs, t,
1643 t->src_loc, t->src_loc,
1644 map, index);
1645 }
1646
1647 if (arg_tokens_count)
1648 {
1649 /* So now we've got the number of tokens that make up the
1650 argument that is going to replace the current parameter
1651 in the macro's replacement list. */
1652 unsigned int j;
1653 for (j = 0; j < arg_tokens_count; ++j)
1654 {
1655 /* So if track_macro_exp is < 2, the user wants to
1656 save extra memory while tracking macro expansion
1657 locations. So in that case here is what we do:
1658
1659 Suppose we have #define SQARE(A) A * A
1660
1661 And then we do SQARE(2+3)
1662
1663 Then the tokens 2, +, 3, will have the same location,
1664 saying they come from the expansion of the argument
1665 A.
1666
1667 So that means we are going to ignore the COUNT tokens
1668 resulting from the expansion of the current macro
1669 arugment. In other words all the ARG_TOKENS_COUNT tokens
1670 resulting from the expansion of the macro argument will
1671 have the index I. Normally, each of those token should
1672 have index I+J. */
1673 unsigned token_index = i;
1674 unsigned index;
1675 if (track_macro_exp > 1)
1676 token_index += j;
1677
1678 index = expanded_token_index (pfile, macro, src, token_index);
1679 tokens_buff_add_token (buff, virt_locs,
1680 macro_arg_token_iter_get_token (&from),
1681 macro_arg_token_iter_get_location (&from),
1682 src->src_loc, map, index);
1683 macro_arg_token_iter_forward (&from);
1684 }
Neil Booth93c803682000-10-28 17:59:06 +00001685
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001686 /* With a non-empty argument on the LHS of ##, the last
1687 token should be flagged PASTE_LEFT. */
1688 if (src->flags & PASTE_LEFT)
Tom Tromey92582b72011-10-17 09:59:12 +00001689 paste_flag =
1690 (const cpp_token **) tokens_buff_last_token_ptr (buff);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001691 }
Andrew Haleye85edc92008-07-03 10:31:50 +00001692 else if (CPP_PEDANTIC (pfile) && ! macro->syshdr
1693 && ! CPP_OPTION (pfile, c99)
1694 && ! cpp_in_system_header (pfile))
1695 {
1696 cpp_error (pfile, CPP_DL_PEDWARN,
1697 "invoking macro %s argument %d: "
1698 "empty macro arguments are undefined"
1699 " in ISO C90 and ISO C++98",
1700 NODE_NAME (node),
Joseph Myers9a0c6182009-05-10 15:27:32 +01001701 src->val.macro_arg.arg_no);
Andrew Haleye85edc92008-07-03 10:31:50 +00001702 }
Neil Booth4c2b6472000-11-11 13:19:01 +00001703
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001704 /* Avoid paste on RHS (even case count == 0). */
1705 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
Tom Tromey92582b72011-10-17 09:59:12 +00001706 {
1707 const cpp_token *t = &pfile->avoid_paste;
1708 tokens_buff_add_token (buff, virt_locs,
1709 t, t->src_loc, t->src_loc,
1710 NULL, 0);
1711 }
Neil Booth26ec42e2001-01-28 11:22:23 +00001712
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001713 /* Add a new paste flag, or remove an unwanted one. */
1714 if (paste_flag)
1715 {
1716 cpp_token *token = _cpp_temp_token (pfile);
1717 token->type = (*paste_flag)->type;
Jakub Jelinek73096712005-03-04 16:33:23 +01001718 token->val = (*paste_flag)->val;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001719 if (src->flags & PASTE_LEFT)
1720 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1721 else
1722 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1723 *paste_flag = token;
1724 }
Tom Tromey92582b72011-10-17 09:59:12 +00001725
1726 i += arg_tokens_count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001727 }
Neil Booth4c2b6472000-11-11 13:19:01 +00001728
Tom Tromey92582b72011-10-17 09:59:12 +00001729 if (track_macro_exp)
1730 push_extended_tokens_context (pfile, node, buff, virt_locs, first,
1731 tokens_buff_count (buff));
1732 else
1733 push_ptoken_context (pfile, node, buff, first,
1734 tokens_buff_count (buff));
Tom Tromey64a1a422011-10-17 09:59:52 +00001735
1736 num_macro_tokens_counter += tokens_buff_count (buff);
Neil Booth93c803682000-10-28 17:59:06 +00001737}
1738
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001739/* Return a special padding token, with padding inherited from SOURCE. */
1740static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001741padding_token (cpp_reader *pfile, const cpp_token *source)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001742{
1743 cpp_token *result = _cpp_temp_token (pfile);
1744
1745 result->type = CPP_PADDING;
Paolo Bonzinibe0f1e52005-02-14 08:52:24 +00001746
1747 /* Data in GCed data structures cannot be made const so far, so we
1748 need a cast here. */
1749 result->val.source = (cpp_token *) source;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001750 result->flags = 0;
1751 return result;
1752}
1753
Neil Boothd15a58c2002-01-03 18:32:55 +00001754/* Get a new uninitialized context. Create a new one if we cannot
1755 re-use an old one. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001756static cpp_context *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001757next_context (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001758{
1759 cpp_context *result = pfile->context->next;
1760
1761 if (result == 0)
1762 {
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001763 result = XNEW (cpp_context);
Tom Tromey92582b72011-10-17 09:59:12 +00001764 memset (result, 0, sizeof (cpp_context));
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001765 result->prev = pfile->context;
1766 result->next = 0;
1767 pfile->context->next = result;
1768 }
1769
1770 pfile->context = result;
1771 return result;
1772}
1773
1774/* Push a list of pointers to tokens. */
1775static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001776push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1777 const cpp_token **first, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +00001778{
1779 cpp_context *context = next_context (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001780
Tom Tromey92582b72011-10-17 09:59:12 +00001781 context->tokens_kind = TOKENS_KIND_INDIRECT;
1782 context->c.macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +00001783 context->buff = buff;
Neil Booth82eda772002-06-04 13:07:06 +00001784 FIRST (context).ptoken = first;
1785 LAST (context).ptoken = first + count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001786}
1787
1788/* Push a list of tokens. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001789void
1790_cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1791 const cpp_token *first, unsigned int count)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001792{
Tom Tromey92582b72011-10-17 09:59:12 +00001793 cpp_context *context = next_context (pfile);
1794
1795 context->tokens_kind = TOKENS_KIND_DIRECT;
1796 context->c.macro = macro;
1797 context->buff = NULL;
Neil Booth82eda772002-06-04 13:07:06 +00001798 FIRST (context).token = first;
1799 LAST (context).token = first + count;
Neil Booth93c803682000-10-28 17:59:06 +00001800}
1801
Tom Tromey92582b72011-10-17 09:59:12 +00001802/* Build a context containing a list of tokens as well as their
1803 virtual locations and push it. TOKENS_BUFF is the buffer that
1804 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1805 non-NULL, it means that the context owns it, meaning that
1806 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1807 contains the virtual locations. */
1808static void
1809push_extended_tokens_context (cpp_reader *pfile,
1810 cpp_hashnode *macro,
1811 _cpp_buff *token_buff,
1812 source_location *virt_locs,
1813 const cpp_token **first,
1814 unsigned int count)
1815{
1816 cpp_context *context = next_context (pfile);
1817 macro_context *m;
1818
1819 context->tokens_kind = TOKENS_KIND_EXTENDED;
1820 context->buff = token_buff;
1821
1822 m = XNEW (macro_context);
1823 m->macro_node = macro;
1824 m->virt_locs = virt_locs;
1825 m->cur_virt_loc = virt_locs;
1826 context->c.mc = m;
1827 FIRST (context).ptoken = first;
1828 LAST (context).ptoken = first + count;
1829}
1830
Neil Boothcbc69f82002-06-05 20:27:12 +00001831/* Push a traditional macro's replacement text. */
1832void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001833_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
1834 const uchar *start, size_t len)
Neil Boothcbc69f82002-06-05 20:27:12 +00001835{
1836 cpp_context *context = next_context (pfile);
1837
Tom Tromey92582b72011-10-17 09:59:12 +00001838 context->tokens_kind = TOKENS_KIND_DIRECT;
1839 context->c.macro = macro;
Neil Boothcbc69f82002-06-05 20:27:12 +00001840 context->buff = NULL;
1841 CUR (context) = start;
Neil Booth1ce676a2002-06-09 20:04:17 +00001842 RLIMIT (context) = start + len;
Neil Booth974c43f2002-06-13 06:25:28 +00001843 macro->flags |= NODE_DISABLED;
Neil Boothcbc69f82002-06-05 20:27:12 +00001844}
1845
Tom Tromey92582b72011-10-17 09:59:12 +00001846/* Creates a buffer that holds tokens a.k.a "token buffer", usually
1847 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
1848 non-null (which means that -ftrack-macro-expansion is on),
1849 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
1850 hold the virtual locations of the tokens resulting from macro
1851 expansion. */
1852static _cpp_buff*
1853tokens_buff_new (cpp_reader *pfile, size_t len,
1854 source_location **virt_locs)
1855{
1856 size_t tokens_size = len * sizeof (cpp_token *);
1857 size_t locs_size = len * sizeof (source_location);
1858
1859 if (virt_locs != NULL)
1860 *virt_locs = XNEWVEC (source_location, locs_size);
1861 return _cpp_get_buff (pfile, tokens_size);
1862}
1863
1864/* Returns the number of tokens contained in a token buffer. The
1865 buffer holds a set of cpp_token*. */
1866static size_t
1867tokens_buff_count (_cpp_buff *buff)
1868{
1869 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
1870}
1871
1872/* Return a pointer to the last token contained in the token buffer
1873 BUFF. */
1874static const cpp_token **
1875tokens_buff_last_token_ptr (_cpp_buff *buff)
1876{
1877 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
1878}
1879
1880/* Remove the last token contained in the token buffer TOKENS_BUFF.
1881 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
1882 containing the virtual locations of the tokens in TOKENS_BUFF; in
1883 which case the function updates that buffer as well. */
1884static inline void
1885tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
1886
1887{
1888 if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
1889 BUFF_FRONT (tokens_buff) =
1890 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
1891}
1892
1893/* Insert a token into the token buffer at the position pointed to by
1894 DEST. Note that the buffer is not enlarged so the previous token
1895 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
1896 means -ftrack-macro-expansion is effect; it then points to where to
1897 insert the virtual location of TOKEN. TOKEN is the token to
1898 insert. VIRT_LOC is the virtual location of the token, i.e, the
1899 location possibly encoding its locus accross macro expansion. If
1900 TOKEN is an argument of a function-like macro (inside a macro
1901 replacement list), PARM_DEF_LOC is the spelling location of the
1902 macro parameter that TOKEN is replacing, in the replacement list of
1903 the macro. If TOKEN is not an argument of a function-like macro or
1904 if it doesn't come from a macro expansion, then VIRT_LOC can just
1905 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
1906 means TOKEN comes from a macro expansion and MAP is the macro map
1907 associated to the macro. MACRO_TOKEN_INDEX points to the index of
1908 the token in the macro map; it is not considered if MAP is NULL.
1909
1910 Upon successful completion this function returns the a pointer to
1911 the position of the token coming right after the insertion
1912 point. */
1913static inline const cpp_token **
1914tokens_buff_put_token_to (const cpp_token **dest,
1915 source_location *virt_loc_dest,
1916 const cpp_token *token,
1917 source_location virt_loc,
1918 source_location parm_def_loc,
1919 const struct line_map *map,
1920 unsigned int macro_token_index)
1921{
1922 source_location macro_loc = virt_loc;
1923 const cpp_token **result;
1924
1925 if (virt_loc_dest)
1926 {
1927 /* -ftrack-macro-expansion is on. */
1928 if (map)
1929 macro_loc = linemap_add_macro_token (map, macro_token_index,
1930 virt_loc, parm_def_loc);
1931 *virt_loc_dest = macro_loc;
1932 }
1933 *dest = token;
1934 result = &dest[1];
1935
1936 return result;
1937}
1938
1939/* Adds a token at the end of the tokens contained in BUFFER. Note
1940 that this function doesn't enlarge BUFFER when the number of tokens
1941 reaches BUFFER's size; it aborts in that situation.
1942
1943 TOKEN is the token to append. VIRT_LOC is the virtual location of
1944 the token, i.e, the location possibly encoding its locus accross
1945 macro expansion. If TOKEN is an argument of a function-like macro
1946 (inside a macro replacement list), PARM_DEF_LOC is the location of
1947 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
1948 from a macro expansion, then VIRT_LOC can just be set to the same
1949 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
1950 from a macro expansion and MAP is the macro map associated to the
1951 macro. MACRO_TOKEN_INDEX points to the index of the token in the
1952 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
1953 non-null, it means -ftrack-macro-expansion is on; in which case
1954 this function adds the virtual location DEF_LOC to the VIRT_LOCS
1955 array, at the same index as the one of TOKEN in BUFFER. Upon
1956 successful completion this function returns the a pointer to the
1957 position of the token coming right after the insertion point. */
1958static const cpp_token **
1959tokens_buff_add_token (_cpp_buff *buffer,
1960 source_location *virt_locs,
1961 const cpp_token *token,
1962 source_location virt_loc,
1963 source_location parm_def_loc,
1964 const struct line_map *map,
1965 unsigned int macro_token_index)
1966{
1967 const cpp_token **result;
1968 source_location *virt_loc_dest = NULL;
1969 unsigned token_index =
1970 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
1971
1972 /* Abort if we pass the end the buffer. */
1973 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
1974 abort ();
1975
1976 if (virt_locs != NULL)
1977 virt_loc_dest = &virt_locs[token_index];
1978
1979 result =
1980 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
1981 virt_loc_dest, token, virt_loc, parm_def_loc,
1982 map, macro_token_index);
1983
1984 BUFF_FRONT (buffer) = (unsigned char *) result;
1985 return result;
1986}
1987
1988/* Allocate space for the function-like macro argument ARG to store
1989 the tokens resulting from the macro-expansion of the tokens that
1990 make up ARG itself. That space is allocated in ARG->expanded and
1991 needs to be freed using free. */
1992static void
1993alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
1994{
1995#ifdef ENABLE_CHECKING
1996 if (arg->expanded != NULL
1997 || arg->expanded_virt_locs != NULL)
1998 abort ();
1999#endif
2000 arg->expanded = XNEWVEC (const cpp_token *, capacity);
2001 if (CPP_OPTION (pfile, track_macro_expansion))
2002 arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2003
2004}
2005
2006/* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2007 tokens. */
2008static void
2009ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2010 size_t size, size_t *expanded_capacity)
2011{
2012 if (size <= *expanded_capacity)
2013 return;
2014
2015 size *= 2;
2016
2017 arg->expanded =
2018 XRESIZEVEC (const cpp_token *, arg->expanded, size);
2019 *expanded_capacity = size;
2020
2021 if (CPP_OPTION (pfile, track_macro_expansion))
2022 {
2023 if (arg->expanded_virt_locs == NULL)
2024 arg->expanded_virt_locs = XNEWVEC (source_location, size);
2025 else
2026 arg->expanded_virt_locs = XRESIZEVEC (source_location,
2027 arg->expanded_virt_locs,
2028 size);
2029 }
2030}
2031
Neil Boothd15a58c2002-01-03 18:32:55 +00002032/* Expand an argument ARG before replacing parameters in a
2033 function-like macro. This works by pushing a context with the
2034 argument's tokens, and then expanding that into a temporary buffer
2035 as if it were a normal part of the token stream. collect_args()
2036 has terminated the argument's tokens with a CPP_EOF so that we know
2037 when we have fully expanded the argument. */
Neil Booth93c803682000-10-28 17:59:06 +00002038static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002039expand_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +00002040{
Tom Tromey92582b72011-10-17 09:59:12 +00002041 size_t capacity;
Neil Booth56941bf2002-09-20 19:44:09 +00002042 bool saved_warn_trad;
Tom Tromey92582b72011-10-17 09:59:12 +00002043 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002044
Tom Tromey92582b72011-10-17 09:59:12 +00002045 if (arg->count == 0
2046 || arg->expanded != NULL)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002047 return;
Neil Booth93c803682000-10-28 17:59:06 +00002048
Neil Booth56941bf2002-09-20 19:44:09 +00002049 /* Don't warn about funlike macros when pre-expanding. */
2050 saved_warn_trad = CPP_WTRADITIONAL (pfile);
2051 CPP_WTRADITIONAL (pfile) = 0;
2052
Tom Tromey92582b72011-10-17 09:59:12 +00002053 /* Loop, reading in the tokens of the argument. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002054 capacity = 256;
Tom Tromey92582b72011-10-17 09:59:12 +00002055 alloc_expanded_arg_mem (pfile, arg, capacity);
Neil Booth93c803682000-10-28 17:59:06 +00002056
Tom Tromey92582b72011-10-17 09:59:12 +00002057 if (track_macro_exp_p)
2058 push_extended_tokens_context (pfile, NULL, NULL,
2059 arg->virt_locs,
2060 arg->first,
2061 arg->count + 1);
2062 else
2063 push_ptoken_context (pfile, NULL, NULL,
2064 arg->first, arg->count + 1);
2065
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002066 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00002067 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002068 const cpp_token *token;
Tom Tromey92582b72011-10-17 09:59:12 +00002069 source_location location;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002070
Tom Tromey92582b72011-10-17 09:59:12 +00002071 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2072 &capacity);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002073
Tom Tromey92582b72011-10-17 09:59:12 +00002074 token = cpp_get_token_1 (pfile, &location);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002075
2076 if (token->type == CPP_EOF)
2077 break;
2078
Tom Tromey92582b72011-10-17 09:59:12 +00002079 set_arg_token (arg, token, location,
2080 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2081 CPP_OPTION (pfile, track_macro_expansion));
2082 arg->expanded_count++;
Neil Booth93c803682000-10-28 17:59:06 +00002083 }
Neil Booth93c803682000-10-28 17:59:06 +00002084
Neil Booth1e013d22001-09-26 21:44:35 +00002085 _cpp_pop_context (pfile);
Neil Booth56941bf2002-09-20 19:44:09 +00002086
2087 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
Neil Booth93c803682000-10-28 17:59:06 +00002088}
2089
Neil Boothd15a58c2002-01-03 18:32:55 +00002090/* Pop the current context off the stack, re-enabling the macro if the
Tom Tromey92582b72011-10-17 09:59:12 +00002091 context represented a macro's replacement list. Initially the
2092 context structure was not freed so that we can re-use it later, but
2093 now we do free it to reduce peak memory consumption. */
Neil Booth93c803682000-10-28 17:59:06 +00002094void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002095_cpp_pop_context (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00002096{
Neil Booth1e013d22001-09-26 21:44:35 +00002097 cpp_context *context = pfile->context;
Neil Booth93c803682000-10-28 17:59:06 +00002098
Tom Tromey92582b72011-10-17 09:59:12 +00002099 if (context->c.macro)
2100 {
2101 cpp_hashnode *macro;
2102 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2103 {
2104 macro_context *mc = context->c.mc;
2105 macro = mc->macro_node;
2106 /* If context->buff is set, it means the life time of tokens
2107 is bound to the life time of this context; so we must
2108 free the tokens; that means we must free the virtual
2109 locations of these tokens too. */
2110 if (context->buff && mc->virt_locs)
2111 {
2112 free (mc->virt_locs);
2113 mc->virt_locs = NULL;
2114 }
2115 free (mc);
2116 context->c.mc = NULL;
2117 }
2118 else
2119 macro = context->c.macro;
2120
2121 /* Beware that MACRO can be NULL in cases like when we are
2122 called from expand_arg. In those cases, a dummy context with
2123 tokens is pushed just for the purpose of walking them using
2124 cpp_get_token_1. In that case, no 'macro' field is set into
2125 the dummy context. */
2126 if (macro != NULL)
2127 macro->flags &= ~NODE_DISABLED;
2128 }
Neil Booth1e013d22001-09-26 21:44:35 +00002129
2130 if (context->buff)
Tom Tromey92582b72011-10-17 09:59:12 +00002131 {
2132 /* Decrease memory peak consumption by freeing the memory used
2133 by the context. */
2134 _cpp_free_buff (context->buff);
2135 }
Neil Booth1e013d22001-09-26 21:44:35 +00002136
2137 pfile->context = context->prev;
Tom Tromey92582b72011-10-17 09:59:12 +00002138 /* decrease peak memory consumption by feeing the context. */
2139 pfile->context->next = NULL;
2140 free (context);
Neil Booth93c803682000-10-28 17:59:06 +00002141}
2142
Tom Tromey92582b72011-10-17 09:59:12 +00002143/* Return TRUE if we reached the end of the set of tokens stored in
2144 CONTEXT, FALSE otherwise. */
2145static inline bool
2146reached_end_of_context (cpp_context *context)
2147{
2148 if (context->tokens_kind == TOKENS_KIND_DIRECT)
2149 return FIRST (context).token == LAST (context).token;
2150 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2151 || context->tokens_kind == TOKENS_KIND_EXTENDED)
2152 return FIRST (context).ptoken == LAST (context).ptoken;
2153 else
2154 abort ();
2155}
2156
2157/* Consume the next token contained in the current context of PFILE,
2158 and return it in *TOKEN. It's "full location" is returned in
2159 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2160 means the location encoding the locus of the token accross macro
2161 expansion; otherwise it's just is the "normal" location of the
2162 token which (*TOKEN)->src_loc. */
2163static inline void
2164consume_next_token_from_context (cpp_reader *pfile,
2165 const cpp_token ** token,
2166 source_location *location)
2167{
2168 cpp_context *c = pfile->context;
2169
2170 if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2171 {
2172 *token = FIRST (c).token;
2173 *location = (*token)->src_loc;
2174 FIRST (c).token++;
2175 }
2176 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2177 {
2178 *token = *FIRST (c).ptoken;
2179 *location = (*token)->src_loc;
2180 FIRST (c).ptoken++;
2181 }
2182 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2183 {
2184 macro_context *m = c->c.mc;
2185 *token = *FIRST (c).ptoken;
2186 if (m->virt_locs)
2187 {
2188 *location = *m->cur_virt_loc;
2189 m->cur_virt_loc++;
2190 }
2191 else
2192 *location = (*token)->src_loc;
2193 FIRST (c).ptoken++;
2194 }
2195 else
2196 abort ();
2197}
2198
2199/* In the traditional mode of the preprocessor, if we are currently in
2200 a directive, the location of a token must be the location of the
2201 start of the directive line. This function returns the proper
2202 location if we are in the traditional mode, and just returns
2203 LOCATION otherwise. */
2204
2205static inline source_location
2206maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2207{
2208 if (CPP_OPTION (pfile, traditional))
2209 {
2210 if (pfile->state.in_directive)
2211 return pfile->directive_line;
2212 }
2213 return location;
2214}
2215
2216/* Routine to get a token as well as its location.
Neil Booth7f2f1a62000-11-14 18:32:06 +00002217
2218 Macro expansions and directives are transparently handled,
2219 including entering included files. Thus tokens are post-macro
2220 expansion, and after any intervening directives. External callers
2221 see CPP_EOF only at EOF. Internal callers also see it when meeting
2222 a directive inside a macro call, when at the end of a directive and
2223 state.in_directive is still 1, and at the end of argument
Tom Tromey92582b72011-10-17 09:59:12 +00002224 pre-expansion.
2225
2226 LOC is an out parameter; *LOC is set to the location "as expected
2227 by the user". Please read the comment of
2228 cpp_get_token_with_location to learn more about the meaning of this
2229 location. */
2230static const cpp_token*
2231cpp_get_token_1 (cpp_reader *pfile, source_location *location)
Neil Booth93c803682000-10-28 17:59:06 +00002232{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002233 const cpp_token *result;
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002234 bool can_set = pfile->set_invocation_location;
Tom Tromey92582b72011-10-17 09:59:12 +00002235 /* This token is a virtual token that either encodes a location
2236 related to macro expansion or a spelling location. */
2237 source_location virt_loc = 0;
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002238 pfile->set_invocation_location = false;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002239
Neil Booth29b10742000-11-13 18:40:37 +00002240 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00002241 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002242 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00002243 cpp_context *context = pfile->context;
2244
Neil Booth93c803682000-10-28 17:59:06 +00002245 /* Context->prev == 0 <=> base context. */
Neil Boothbdcbe492001-09-13 20:05:17 +00002246 if (!context->prev)
Neil Booth29b10742000-11-13 18:40:37 +00002247 {
Tom Tromey92582b72011-10-17 09:59:12 +00002248 result = _cpp_lex_token (pfile);
2249 virt_loc = result->src_loc;
2250 }
2251 else if (!reached_end_of_context (context))
2252 {
2253 consume_next_token_from_context (pfile, &result,
2254 &virt_loc);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002255 if (result->flags & PASTE_LEFT)
Neil Boothec1a23e2001-01-31 07:48:54 +00002256 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002257 paste_all_tokens (pfile, result);
2258 if (pfile->state.in_directive)
2259 continue;
Tom Tromey92582b72011-10-17 09:59:12 +00002260 result = padding_token (pfile, result);
2261 goto out;
Neil Boothec1a23e2001-01-31 07:48:54 +00002262 }
Neil Booth29b10742000-11-13 18:40:37 +00002263 }
Neil Booth93c803682000-10-28 17:59:06 +00002264 else
2265 {
Tom Tromey64a1a422011-10-17 09:59:52 +00002266 if (pfile->context->c.macro)
2267 ++num_expanded_macros_counter;
Neil Boothbdcbe492001-09-13 20:05:17 +00002268 _cpp_pop_context (pfile);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002269 if (pfile->state.in_directive)
2270 continue;
Tom Tromey92582b72011-10-17 09:59:12 +00002271 result = &pfile->avoid_paste;
2272 goto out;
Neil Booth93c803682000-10-28 17:59:06 +00002273 }
Neil Booth93c803682000-10-28 17:59:06 +00002274
Jason Thorpe477cdac2002-04-07 03:12:23 +00002275 if (pfile->state.in_directive && result->type == CPP_COMMENT)
2276 continue;
2277
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002278 if (result->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +00002279 break;
2280
Joseph Myers9a0c6182009-05-10 15:27:32 +01002281 node = result->val.node.node;
Neil Booth4c2b6472000-11-11 13:19:01 +00002282
Neil Booth644edda2001-10-02 12:57:24 +00002283 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2284 break;
Kazu Hiratadf383482002-05-22 22:02:16 +00002285
Neil Booth644edda2001-10-02 12:57:24 +00002286 if (!(node->flags & NODE_DISABLED))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002287 {
Ben Elliston5950c3c2008-07-14 05:09:48 +00002288 int ret = 0;
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002289 /* If not in a macro context, and we're going to start an
2290 expansion, record the location. */
Tom Tromey92582b72011-10-17 09:59:12 +00002291 if (can_set && !context->c.macro)
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002292 pfile->invocation_location = result->src_loc;
Jakub Jelinek765d6002008-01-25 10:01:27 +01002293 if (pfile->state.prevent_expansion)
2294 break;
Ben Elliston5950c3c2008-07-14 05:09:48 +00002295
2296 /* Conditional macros require that a predicate be evaluated
2297 first. */
Jakub Jelineka37a7b82009-03-30 17:00:52 +02002298 if ((node->flags & NODE_CONDITIONAL) != 0)
2299 {
2300 if (pfile->cb.macro_to_expand)
2301 {
2302 bool whitespace_after;
2303 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2304
2305 whitespace_after = (peek_tok->type == CPP_PADDING
2306 || (peek_tok->flags & PREV_WHITE));
2307 node = pfile->cb.macro_to_expand (pfile, result);
2308 if (node)
Tom Tromey92582b72011-10-17 09:59:12 +00002309 ret = enter_macro_context (pfile, node, result,
2310 virt_loc);
Jakub Jelineka37a7b82009-03-30 17:00:52 +02002311 else if (whitespace_after)
2312 {
2313 /* If macro_to_expand hook returned NULL and it
2314 ate some tokens, see if we don't need to add
2315 a padding token in between this and the
2316 next token. */
2317 peek_tok = cpp_peek_token (pfile, 0);
2318 if (peek_tok->type != CPP_PADDING
2319 && (peek_tok->flags & PREV_WHITE) == 0)
2320 _cpp_push_token_context (pfile, NULL,
2321 padding_token (pfile,
2322 peek_tok), 1);
2323 }
2324 }
2325 }
2326 else
Tom Tromey92582b72011-10-17 09:59:12 +00002327 ret = enter_macro_context (pfile, node, result,
2328 virt_loc);
Jakub Jelineka37a7b82009-03-30 17:00:52 +02002329 if (ret)
Ben Elliston5950c3c2008-07-14 05:09:48 +00002330 {
Jakub Jelinek765d6002008-01-25 10:01:27 +01002331 if (pfile->state.in_directive || ret == 2)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002332 continue;
Tom Tromey92582b72011-10-17 09:59:12 +00002333 result = padding_token (pfile, result);
2334 goto out;
Neil Boothbd969772001-02-01 19:13:53 +00002335 }
Neil Booth93c803682000-10-28 17:59:06 +00002336 }
Neil Booth644edda2001-10-02 12:57:24 +00002337 else
2338 {
Neil Boothd15a58c2002-01-03 18:32:55 +00002339 /* Flag this token as always unexpandable. FIXME: move this
2340 to collect_args()?. */
Neil Booth644edda2001-10-02 12:57:24 +00002341 cpp_token *t = _cpp_temp_token (pfile);
2342 t->type = result->type;
2343 t->flags = result->flags | NO_EXPAND;
Jakub Jelinek73096712005-03-04 16:33:23 +01002344 t->val = result->val;
Neil Booth644edda2001-10-02 12:57:24 +00002345 result = t;
2346 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00002347
Neil Booth644edda2001-10-02 12:57:24 +00002348 break;
Neil Booth93c803682000-10-28 17:59:06 +00002349 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002350
Tom Tromey92582b72011-10-17 09:59:12 +00002351 out:
2352 if (location != NULL)
2353 {
2354 if (virt_loc == 0)
2355 virt_loc = result->src_loc;
2356 *location = virt_loc;
2357
2358 if (!CPP_OPTION (pfile, track_macro_expansion)
2359 && can_set
2360 && pfile->context->c.macro != NULL)
2361 /* We are in a macro expansion context, are not tracking
2362 virtual location, but were asked to report the location
2363 of the expansion point of the macro being expanded. */
2364 *location = pfile->invocation_location;
2365
2366 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2367 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002368 return result;
Neil Booth93c803682000-10-28 17:59:06 +00002369}
2370
Tom Tromey92582b72011-10-17 09:59:12 +00002371/* External routine to get a token. Also used nearly everywhere
2372 internally, except for places where we know we can safely call
2373 _cpp_lex_token directly, such as lexing a directive name.
2374
2375 Macro expansions and directives are transparently handled,
2376 including entering included files. Thus tokens are post-macro
2377 expansion, and after any intervening directives. External callers
2378 see CPP_EOF only at EOF. Internal callers also see it when meeting
2379 a directive inside a macro call, when at the end of a directive and
2380 state.in_directive is still 1, and at the end of argument
2381 pre-expansion. */
2382const cpp_token *
2383cpp_get_token (cpp_reader *pfile)
2384{
2385 return cpp_get_token_1 (pfile, NULL);
2386}
2387
2388/* Like cpp_get_token, but also returns a virtual token location
2389 separate from the spelling location carried by the returned token.
2390
2391 LOC is an out parameter; *LOC is set to the location "as expected
2392 by the user". This matters when a token results from macro
2393 expansion; in that case the token's spelling location indicates the
2394 locus of the token in the definition of the macro but *LOC
2395 virtually encodes all the other meaningful locuses associated to
2396 the token.
2397
2398 What? virtual location? Yes, virtual location.
2399
2400 If the token results from macro expansion and if macro expansion
2401 location tracking is enabled its virtual location encodes (at the
2402 same time):
2403
2404 - the spelling location of the token
2405
2406 - the locus of the macro expansion point
2407
2408 - the locus of the point where the token got instantiated as part
2409 of the macro expansion process.
2410
2411 You have to use the linemap API to get the locus you are interested
2412 in from a given virtual location.
2413
2414 Note however that virtual locations are not necessarily ordered for
2415 relations '<' and '>'. One must use the function
2416 linemap_location_before_p instead of using the relational operator
2417 '<'.
2418
2419 If macro expansion tracking is off and if the token results from
2420 macro expansion the virtual location is the expansion point of the
2421 macro that got expanded.
2422
2423 When the token doesn't result from macro expansion, the virtual
2424 location is just the same thing as its spelling location. */
2425
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002426const cpp_token *
2427cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2428{
2429 const cpp_token *result;
2430
2431 pfile->set_invocation_location = true;
Tom Tromey92582b72011-10-17 09:59:12 +00002432 result = cpp_get_token_1 (pfile, loc);
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002433 return result;
2434}
2435
Neil Booth7065e132001-02-14 07:38:20 +00002436/* Returns true if we're expanding an object-like macro that was
2437 defined in a system header. Just checks the macro at the top of
2438 the stack. Used for diagnostic suppression. */
2439int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002440cpp_sys_macro_p (cpp_reader *pfile)
Neil Booth7065e132001-02-14 07:38:20 +00002441{
Tom Tromey92582b72011-10-17 09:59:12 +00002442 cpp_hashnode *node = pfile->context->c.macro;
Neil Booth7065e132001-02-14 07:38:20 +00002443
Neil Booth644edda2001-10-02 12:57:24 +00002444 return node && node->value.macro && node->value.macro->syshdr;
Neil Booth7065e132001-02-14 07:38:20 +00002445}
2446
Neil Boothaf0d16c2002-04-22 17:48:02 +00002447/* Read each token in, until end of the current file. Directives are
2448 transparently processed. */
Neil Booth93c803682000-10-28 17:59:06 +00002449void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002450cpp_scan_nooutput (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00002451{
Per Bothner22234f52004-02-18 14:02:39 -08002452 /* Request a CPP_EOF token at the end of this file, rather than
2453 transparently continuing with the including file. */
2454 pfile->buffer->return_at_eof = true;
2455
Zack Weinbergc6e83802004-06-05 20:58:06 +00002456 pfile->state.discarding_output++;
2457 pfile->state.prevent_expansion++;
2458
Neil Booth590e1982002-07-01 12:47:54 +00002459 if (CPP_OPTION (pfile, traditional))
2460 while (_cpp_read_logical_line_trad (pfile))
2461 ;
2462 else
2463 while (cpp_get_token (pfile)->type != CPP_EOF)
2464 ;
Zack Weinbergc6e83802004-06-05 20:58:06 +00002465
2466 pfile->state.discarding_output--;
2467 pfile->state.prevent_expansion--;
Neil Booth93c803682000-10-28 17:59:06 +00002468}
2469
Ben Elliston5950c3c2008-07-14 05:09:48 +00002470/* Step back one or more tokens obtained from the lexer. */
2471void
2472_cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2473{
2474 pfile->lookaheads += count;
2475 while (count--)
2476 {
2477 pfile->cur_token--;
2478 if (pfile->cur_token == pfile->cur_run->base
2479 /* Possible with -fpreprocessed and no leading #line. */
2480 && pfile->cur_run->prev != NULL)
2481 {
2482 pfile->cur_run = pfile->cur_run->prev;
2483 pfile->cur_token = pfile->cur_run->limit;
2484 }
2485 }
2486}
2487
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02002488/* Step back one (or more) tokens. Can only step back more than 1 if
Neil Boothbdcbe492001-09-13 20:05:17 +00002489 they are from the lexer, and not from macro expansion. */
Neil Boothb528a072000-11-12 11:46:21 +00002490void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002491_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +00002492{
Neil Boothbdcbe492001-09-13 20:05:17 +00002493 if (pfile->context->prev == NULL)
Ben Elliston5950c3c2008-07-14 05:09:48 +00002494 _cpp_backup_tokens_direct (pfile, count);
Neil Booth93c803682000-10-28 17:59:06 +00002495 else
2496 {
Neil Boothbdcbe492001-09-13 20:05:17 +00002497 if (count != 1)
2498 abort ();
Tom Tromey92582b72011-10-17 09:59:12 +00002499 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
Neil Booth82eda772002-06-04 13:07:06 +00002500 FIRST (pfile->context).token--;
Tom Tromey92582b72011-10-17 09:59:12 +00002501 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
Neil Booth82eda772002-06-04 13:07:06 +00002502 FIRST (pfile->context).ptoken--;
Tom Tromey92582b72011-10-17 09:59:12 +00002503 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2504 {
2505 FIRST (pfile->context).ptoken--;
2506 if (pfile->context->c.macro)
2507 {
2508 macro_context *m = pfile->context->c.mc;
2509 m->cur_virt_loc--;
2510#ifdef ENABLE_CHECKING
2511 if (m->cur_virt_loc < m->virt_locs)
2512 abort ();
2513#endif
2514 }
2515 else
2516 abort ();
2517 }
2518 else
2519 abort ();
Neil Booth93c803682000-10-28 17:59:06 +00002520 }
Neil Booth93c803682000-10-28 17:59:06 +00002521}
2522
2523/* #define directive parsing and handling. */
2524
Kazu Hiratada7d8302002-09-22 02:03:17 +00002525/* Returns nonzero if a macro redefinition warning is required. */
Neil Boothcbc69f82002-06-05 20:27:12 +00002526static bool
Jakub Jelinek8e680db2010-06-11 20:37:34 +02002527warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002528 const cpp_macro *macro2)
Neil Booth93c803682000-10-28 17:59:06 +00002529{
2530 const cpp_macro *macro1;
2531 unsigned int i;
2532
Neil Booth618cdda2001-02-25 09:43:03 +00002533 /* Some redefinitions need to be warned about regardless. */
2534 if (node->flags & NODE_WARN)
Neil Boothcbc69f82002-06-05 20:27:12 +00002535 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002536
Simon Baldwinc047ce92008-09-18 15:39:08 +00002537 /* Suppress warnings for builtins that lack the NODE_WARN flag. */
2538 if (node->flags & NODE_BUILTIN)
Jakub Jelinek8e680db2010-06-11 20:37:34 +02002539 {
2540 if (!pfile->cb.user_builtin_macro
2541 || !pfile->cb.user_builtin_macro (pfile, node))
2542 return false;
2543 }
Simon Baldwinc047ce92008-09-18 15:39:08 +00002544
Ben Elliston5950c3c2008-07-14 05:09:48 +00002545 /* Redefinitions of conditional (context-sensitive) macros, on
2546 the other hand, must be allowed silently. */
2547 if (node->flags & NODE_CONDITIONAL)
2548 return false;
2549
Neil Booth618cdda2001-02-25 09:43:03 +00002550 /* Redefinition of a macro is allowed if and only if the old and new
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002551 definitions are the same. (6.10.3 paragraph 2). */
Neil Booth93c803682000-10-28 17:59:06 +00002552 macro1 = node->value.macro;
2553
Neil Booth6618c5d2002-06-10 06:03:13 +00002554 /* Don't check count here as it can be different in valid
2555 traditional redefinitions with just whitespace differences. */
2556 if (macro1->paramc != macro2->paramc
Neil Booth93c803682000-10-28 17:59:06 +00002557 || macro1->fun_like != macro2->fun_like
Neil Booth28e0f042000-12-09 12:06:37 +00002558 || macro1->variadic != macro2->variadic)
Neil Boothcbc69f82002-06-05 20:27:12 +00002559 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002560
2561 /* Check parameter spellings. */
2562 for (i = 0; i < macro1->paramc; i++)
2563 if (macro1->params[i] != macro2->params[i])
Neil Boothcbc69f82002-06-05 20:27:12 +00002564 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002565
Neil Boothcbc69f82002-06-05 20:27:12 +00002566 /* Check the replacement text or tokens. */
2567 if (CPP_OPTION (pfile, traditional))
Neil Booth6618c5d2002-06-10 06:03:13 +00002568 return _cpp_expansions_different_trad (macro1, macro2);
Neil Boothcbc69f82002-06-05 20:27:12 +00002569
DJ Deloriea7f36da2003-06-01 14:55:15 -04002570 if (macro1->count != macro2->count)
2571 return true;
2572
2573 for (i = 0; i < macro1->count; i++)
2574 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2575 return true;
Neil Boothcbc69f82002-06-05 20:27:12 +00002576
2577 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002578}
2579
Neil Booth93c803682000-10-28 17:59:06 +00002580/* Free the definition of hashnode H. */
Neil Booth93c803682000-10-28 17:59:06 +00002581void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002582_cpp_free_definition (cpp_hashnode *h)
Zack Weinberg711b8822000-07-18 00:59:49 +00002583{
Neil Booth93c803682000-10-28 17:59:06 +00002584 /* Macros and assertions no longer have anything to free. */
2585 h->type = NT_VOID;
2586 /* Clear builtin flag in case of redefinition. */
Joseph Myers93d45d92008-04-02 20:42:53 +01002587 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
Neil Booth93c803682000-10-28 17:59:06 +00002588}
Zack Weinberg711b8822000-07-18 00:59:49 +00002589
Neil Booth14baae02001-09-17 18:26:12 +00002590/* Save parameter NODE to the parameter list of macro MACRO. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00002591 zero on success, nonzero if the parameter is a duplicate. */
Neil Boothc70f6ed2002-06-07 06:26:32 +00002592bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002593_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00002594{
Zack Weinberg4977bab2002-12-16 18:23:00 +00002595 unsigned int len;
Neil Booth93c803682000-10-28 17:59:06 +00002596 /* Constraint 6.10.3.6 - duplicate parameter names. */
Zack Weinberg4977bab2002-12-16 18:23:00 +00002597 if (node->flags & NODE_MACRO_ARG)
Zack Weinberg711b8822000-07-18 00:59:49 +00002598 {
John David Anglin0527bc42003-11-01 22:56:54 +00002599 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00002600 NODE_NAME (node));
Neil Booth23ff0222002-07-17 17:27:14 +00002601 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002602 }
2603
Neil Booth8c3b2692001-09-30 10:03:11 +00002604 if (BUFF_ROOM (pfile->a_buff)
2605 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2606 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +00002607
Neil Booth8c3b2692001-09-30 10:03:11 +00002608 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
Zack Weinberg4977bab2002-12-16 18:23:00 +00002609 node->flags |= NODE_MACRO_ARG;
2610 len = macro->paramc * sizeof (union _cpp_hashnode_value);
2611 if (len > pfile->macro_buffer_len)
2612 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002613 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
2614 len);
Zack Weinberg4977bab2002-12-16 18:23:00 +00002615 pfile->macro_buffer_len = len;
2616 }
2617 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
2618 = node->value;
2619
2620 node->value.arg_index = macro->paramc;
Neil Booth23ff0222002-07-17 17:27:14 +00002621 return false;
Neil Booth93c803682000-10-28 17:59:06 +00002622}
2623
Neil Booth23ff0222002-07-17 17:27:14 +00002624/* Check the syntax of the parameters in a MACRO definition. Returns
2625 false if an error occurs. */
2626static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002627parse_params (cpp_reader *pfile, cpp_macro *macro)
Neil Booth93c803682000-10-28 17:59:06 +00002628{
Neil Booth93c803682000-10-28 17:59:06 +00002629 unsigned int prev_ident = 0;
2630
Neil Booth93c803682000-10-28 17:59:06 +00002631 for (;;)
2632 {
Neil Booth345894b2001-09-16 13:44:29 +00002633 const cpp_token *token = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00002634
Neil Booth345894b2001-09-16 13:44:29 +00002635 switch (token->type)
Zack Weinberg711b8822000-07-18 00:59:49 +00002636 {
2637 default:
Jason Thorpe477cdac2002-04-07 03:12:23 +00002638 /* Allow/ignore comments in parameter lists if we are
2639 preserving comments in macro expansions. */
2640 if (token->type == CPP_COMMENT
2641 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
2642 continue;
2643
John David Anglin0527bc42003-11-01 22:56:54 +00002644 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00002645 "\"%s\" may not appear in macro parameter list",
Neil Booth345894b2001-09-16 13:44:29 +00002646 cpp_token_as_text (pfile, token));
Neil Booth23ff0222002-07-17 17:27:14 +00002647 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002648
Zack Weinberg711b8822000-07-18 00:59:49 +00002649 case CPP_NAME:
2650 if (prev_ident)
2651 {
John David Anglin0527bc42003-11-01 22:56:54 +00002652 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00002653 "macro parameters must be comma-separated");
Neil Booth23ff0222002-07-17 17:27:14 +00002654 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002655 }
Zack Weinberg711b8822000-07-18 00:59:49 +00002656 prev_ident = 1;
Neil Booth93c803682000-10-28 17:59:06 +00002657
Joseph Myers9a0c6182009-05-10 15:27:32 +01002658 if (_cpp_save_parameter (pfile, macro, token->val.node.node))
Neil Booth23ff0222002-07-17 17:27:14 +00002659 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002660 continue;
2661
2662 case CPP_CLOSE_PAREN:
Neil Booth93c803682000-10-28 17:59:06 +00002663 if (prev_ident || macro->paramc == 0)
Neil Booth23ff0222002-07-17 17:27:14 +00002664 return true;
Zack Weinberg711b8822000-07-18 00:59:49 +00002665
2666 /* Fall through to pick up the error. */
2667 case CPP_COMMA:
2668 if (!prev_ident)
2669 {
John David Anglin0527bc42003-11-01 22:56:54 +00002670 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
Neil Booth23ff0222002-07-17 17:27:14 +00002671 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002672 }
2673 prev_ident = 0;
2674 continue;
2675
2676 case CPP_ELLIPSIS:
Neil Booth28e0f042000-12-09 12:06:37 +00002677 macro->variadic = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00002678 if (!prev_ident)
2679 {
Neil Boothc70f6ed2002-06-07 06:26:32 +00002680 _cpp_save_parameter (pfile, macro,
2681 pfile->spec_nodes.n__VA_ARGS__);
Neil Booth93c803682000-10-28 17:59:06 +00002682 pfile->state.va_args_ok = 1;
Richard Hendersone5b79212004-02-19 14:18:50 -08002683 if (! CPP_OPTION (pfile, c99)
Joseph Myerse3339d02010-09-29 15:49:14 +01002684 && CPP_OPTION (pfile, cpp_pedantic)
Richard Hendersone5b79212004-02-19 14:18:50 -08002685 && CPP_OPTION (pfile, warn_variadic_macros))
Simon Baldwin87cf0652010-04-07 17:18:10 +00002686 cpp_pedwarning
2687 (pfile, CPP_W_VARIADIC_MACROS,
2688 "anonymous variadic macros were introduced in C99");
Zack Weinberg711b8822000-07-18 00:59:49 +00002689 }
Joseph Myerse3339d02010-09-29 15:49:14 +01002690 else if (CPP_OPTION (pfile, cpp_pedantic)
Richard Hendersone5b79212004-02-19 14:18:50 -08002691 && CPP_OPTION (pfile, warn_variadic_macros))
Simon Baldwin87cf0652010-04-07 17:18:10 +00002692 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2693 "ISO C does not permit named variadic macros");
Zack Weinberg711b8822000-07-18 00:59:49 +00002694
Neil Booth93c803682000-10-28 17:59:06 +00002695 /* We're at the end, and just expect a closing parenthesis. */
Neil Booth345894b2001-09-16 13:44:29 +00002696 token = _cpp_lex_token (pfile);
2697 if (token->type == CPP_CLOSE_PAREN)
Neil Booth23ff0222002-07-17 17:27:14 +00002698 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002699 /* Fall through. */
2700
2701 case CPP_EOF:
John David Anglin0527bc42003-11-01 22:56:54 +00002702 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
Neil Booth23ff0222002-07-17 17:27:14 +00002703 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002704 }
Zack Weinberg711b8822000-07-18 00:59:49 +00002705 }
2706}
2707
Neil Booth14baae02001-09-17 18:26:12 +00002708/* Allocate room for a token from a macro's replacement list. */
Neil Booth93c803682000-10-28 17:59:06 +00002709static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002710alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00002711{
Neil Booth8c3b2692001-09-30 10:03:11 +00002712 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2713 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
Zack Weinberg711b8822000-07-18 00:59:49 +00002714
Neil Booth8c3b2692001-09-30 10:03:11 +00002715 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
Neil Booth14baae02001-09-17 18:26:12 +00002716}
2717
Neil Boothd15a58c2002-01-03 18:32:55 +00002718/* Lex a token from the expansion of MACRO, but mark parameters as we
2719 find them and warn of traditional stringification. */
Neil Booth14baae02001-09-17 18:26:12 +00002720static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002721lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Neil Booth14baae02001-09-17 18:26:12 +00002722{
Tom Tromeyee380362007-01-30 15:46:01 +00002723 cpp_token *token, *saved_cur_token;
Neil Booth14baae02001-09-17 18:26:12 +00002724
Tom Tromeyee380362007-01-30 15:46:01 +00002725 saved_cur_token = pfile->cur_token;
Neil Booth14baae02001-09-17 18:26:12 +00002726 pfile->cur_token = alloc_expansion_token (pfile, macro);
2727 token = _cpp_lex_direct (pfile);
Tom Tromeyee380362007-01-30 15:46:01 +00002728 pfile->cur_token = saved_cur_token;
Neil Booth93c803682000-10-28 17:59:06 +00002729
Neil Boothd15a58c2002-01-03 18:32:55 +00002730 /* Is this a parameter? */
Zack Weinberg4977bab2002-12-16 18:23:00 +00002731 if (token->type == CPP_NAME
Joseph Myers9a0c6182009-05-10 15:27:32 +01002732 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
Zack Weinberg711b8822000-07-18 00:59:49 +00002733 {
Neil Booth93c803682000-10-28 17:59:06 +00002734 token->type = CPP_MACRO_ARG;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002735 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
Zack Weinberg711b8822000-07-18 00:59:49 +00002736 }
Neil Booth93c803682000-10-28 17:59:06 +00002737 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2738 && (token->type == CPP_STRING || token->type == CPP_CHAR))
2739 check_trad_stringification (pfile, macro, &token->val.str);
Zack Weinberg711b8822000-07-18 00:59:49 +00002740
Neil Booth93c803682000-10-28 17:59:06 +00002741 return token;
Zack Weinberg711b8822000-07-18 00:59:49 +00002742}
2743
Neil Boothcbc69f82002-06-05 20:27:12 +00002744static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002745create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00002746{
Neil Boothcbc69f82002-06-05 20:27:12 +00002747 cpp_token *token;
Neil Booth14baae02001-09-17 18:26:12 +00002748 const cpp_token *ctoken;
Simon Martin126e0732007-05-23 20:58:34 +00002749 bool following_paste_op = false;
2750 const char *paste_op_error_msg =
2751 N_("'##' cannot appear at either end of a macro expansion");
Joseph Myersaa508502009-04-19 18:10:56 +01002752 unsigned int num_extra_tokens = 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00002753
Neil Booth93c803682000-10-28 17:59:06 +00002754 /* Get the first token of the expansion (or the '(' of a
2755 function-like macro). */
Neil Booth14baae02001-09-17 18:26:12 +00002756 ctoken = _cpp_lex_token (pfile);
2757
2758 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
Zack Weinberg711b8822000-07-18 00:59:49 +00002759 {
Neil Boothcbc69f82002-06-05 20:27:12 +00002760 bool ok = parse_params (pfile, macro);
Neil Booth8c3b2692001-09-30 10:03:11 +00002761 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
2762 if (!ok)
Neil Boothcbc69f82002-06-05 20:27:12 +00002763 return false;
Neil Booth8c3b2692001-09-30 10:03:11 +00002764
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002765 /* Success. Commit or allocate the parameter array. */
2766 if (pfile->hash_table->alloc_subobject)
2767 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002768 cpp_hashnode **params =
2769 (cpp_hashnode **) pfile->hash_table->alloc_subobject
2770 (sizeof (cpp_hashnode *) * macro->paramc);
Paolo Bonzinibe0f1e52005-02-14 08:52:24 +00002771 memcpy (params, macro->params,
2772 sizeof (cpp_hashnode *) * macro->paramc);
2773 macro->params = params;
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002774 }
2775 else
2776 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
Neil Booth93c803682000-10-28 17:59:06 +00002777 macro->fun_like = 1;
Neil Booth93c803682000-10-28 17:59:06 +00002778 }
Neil Booth14baae02001-09-17 18:26:12 +00002779 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
Jakub Jelinekcae064e2005-04-05 22:07:06 +02002780 {
2781 /* While ISO C99 requires whitespace before replacement text
2782 in a macro definition, ISO C90 with TC1 allows there characters
2783 from the basic source character set. */
2784 if (CPP_OPTION (pfile, c99))
2785 cpp_error (pfile, CPP_DL_PEDWARN,
2786 "ISO C99 requires whitespace after the macro name");
2787 else
2788 {
2789 int warntype = CPP_DL_WARNING;
2790 switch (ctoken->type)
2791 {
2792 case CPP_ATSIGN:
2793 case CPP_AT_NAME:
2794 case CPP_OBJC_STRING:
2795 /* '@' is not in basic character set. */
2796 warntype = CPP_DL_PEDWARN;
2797 break;
2798 case CPP_OTHER:
2799 /* Basic character set sans letters, digits and _. */
2800 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
2801 ctoken->val.str.text[0]) == NULL)
2802 warntype = CPP_DL_PEDWARN;
2803 break;
2804 default:
2805 /* All other tokens start with a character from basic
2806 character set. */
2807 break;
2808 }
2809 cpp_error (pfile, warntype,
2810 "missing whitespace after the macro name");
2811 }
2812 }
Neil Booth93c803682000-10-28 17:59:06 +00002813
Neil Booth14baae02001-09-17 18:26:12 +00002814 if (macro->fun_like)
2815 token = lex_expansion_token (pfile, macro);
2816 else
2817 {
2818 token = alloc_expansion_token (pfile, macro);
2819 *token = *ctoken;
2820 }
Neil Booth93c803682000-10-28 17:59:06 +00002821
2822 for (;;)
2823 {
2824 /* Check the stringifying # constraint 6.10.3.2.1 of
2825 function-like macros when lexing the subsequent token. */
2826 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
Zack Weinberg711b8822000-07-18 00:59:49 +00002827 {
Neil Booth93c803682000-10-28 17:59:06 +00002828 if (token->type == CPP_MACRO_ARG)
2829 {
Joseph Myersaa508502009-04-19 18:10:56 +01002830 if (token->flags & PREV_WHITE)
2831 token->flags |= SP_PREV_WHITE;
2832 if (token[-1].flags & DIGRAPH)
2833 token->flags |= SP_DIGRAPH;
Neil Booth93c803682000-10-28 17:59:06 +00002834 token->flags &= ~PREV_WHITE;
2835 token->flags |= STRINGIFY_ARG;
2836 token->flags |= token[-1].flags & PREV_WHITE;
2837 token[-1] = token[0];
2838 macro->count--;
2839 }
2840 /* Let assembler get away with murder. */
Neil Boothbdb05a72000-11-26 17:31:13 +00002841 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +00002842 {
John David Anglin0527bc42003-11-01 22:56:54 +00002843 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00002844 "'#' is not followed by a macro parameter");
Neil Boothcbc69f82002-06-05 20:27:12 +00002845 return false;
Neil Booth93c803682000-10-28 17:59:06 +00002846 }
2847 }
2848
2849 if (token->type == CPP_EOF)
Simon Martin126e0732007-05-23 20:58:34 +00002850 {
2851 /* Paste operator constraint 6.10.3.3.1:
2852 Token-paste ##, can appear in both object-like and
2853 function-like macros, but not at the end. */
2854 if (following_paste_op)
2855 {
2856 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
2857 return false;
2858 }
2859 break;
2860 }
Neil Booth93c803682000-10-28 17:59:06 +00002861
2862 /* Paste operator constraint 6.10.3.3.1. */
2863 if (token->type == CPP_PASTE)
2864 {
2865 /* Token-paste ##, can appear in both object-like and
Simon Martin126e0732007-05-23 20:58:34 +00002866 function-like macros, but not at the beginning. */
2867 if (macro->count == 1)
Neil Booth93c803682000-10-28 17:59:06 +00002868 {
Simon Martin126e0732007-05-23 20:58:34 +00002869 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
Neil Boothcbc69f82002-06-05 20:27:12 +00002870 return false;
Neil Booth93c803682000-10-28 17:59:06 +00002871 }
2872
Joseph Myersaa508502009-04-19 18:10:56 +01002873 if (token[-1].flags & PASTE_LEFT)
2874 {
2875 macro->extra_tokens = 1;
2876 num_extra_tokens++;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002877 token->val.token_no = macro->count - 1;
Joseph Myersaa508502009-04-19 18:10:56 +01002878 }
2879 else
2880 {
2881 --macro->count;
2882 token[-1].flags |= PASTE_LEFT;
2883 if (token->flags & DIGRAPH)
2884 token[-1].flags |= SP_DIGRAPH;
2885 if (token->flags & PREV_WHITE)
2886 token[-1].flags |= SP_PREV_WHITE;
2887 }
Neil Booth93c803682000-10-28 17:59:06 +00002888 }
2889
Simon Martin126e0732007-05-23 20:58:34 +00002890 following_paste_op = (token->type == CPP_PASTE);
Neil Booth93c803682000-10-28 17:59:06 +00002891 token = lex_expansion_token (pfile, macro);
2892 }
2893
Neil Booth601328b2002-05-16 05:53:24 +00002894 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002895 macro->traditional = 0;
Neil Booth8c3b2692001-09-30 10:03:11 +00002896
Neil Booth4c2b6472000-11-11 13:19:01 +00002897 /* Don't count the CPP_EOF. */
2898 macro->count--;
Neil Booth93c803682000-10-28 17:59:06 +00002899
Neil Boothd15a58c2002-01-03 18:32:55 +00002900 /* Clear whitespace on first token for warn_of_redefinition(). */
Neil Booth8c3b2692001-09-30 10:03:11 +00002901 if (macro->count)
Neil Booth601328b2002-05-16 05:53:24 +00002902 macro->exp.tokens[0].flags &= ~PREV_WHITE;
Neil Booth8c3b2692001-09-30 10:03:11 +00002903
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002904 /* Commit or allocate the memory. */
2905 if (pfile->hash_table->alloc_subobject)
2906 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002907 cpp_token *tokns =
2908 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
2909 * macro->count);
Joseph Myersaa508502009-04-19 18:10:56 +01002910 if (num_extra_tokens)
2911 {
2912 /* Place second and subsequent ## or %:%: tokens in
2913 sequences of consecutive such tokens at the end of the
2914 list to preserve information about where they appear, how
2915 they are spelt and whether they are preceded by
2916 whitespace without otherwise interfering with macro
2917 expansion. */
2918 cpp_token *normal_dest = tokns;
2919 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
2920 unsigned int i;
2921 for (i = 0; i < macro->count; i++)
2922 {
2923 if (macro->exp.tokens[i].type == CPP_PASTE)
2924 *extra_dest++ = macro->exp.tokens[i];
2925 else
2926 *normal_dest++ = macro->exp.tokens[i];
2927 }
2928 }
2929 else
2930 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002931 macro->exp.tokens = tokns;
2932 }
2933 else
2934 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
Neil Booth8c3b2692001-09-30 10:03:11 +00002935
Neil Boothcbc69f82002-06-05 20:27:12 +00002936 return true;
2937}
Neil Booth44ed91a2000-10-29 11:37:18 +00002938
Kazu Hiratada7d8302002-09-22 02:03:17 +00002939/* Parse a macro and save its expansion. Returns nonzero on success. */
Neil Boothcbc69f82002-06-05 20:27:12 +00002940bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002941_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
Neil Boothcbc69f82002-06-05 20:27:12 +00002942{
2943 cpp_macro *macro;
2944 unsigned int i;
2945 bool ok;
2946
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002947 if (pfile->hash_table->alloc_subobject)
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002948 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
2949 (sizeof (cpp_macro));
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002950 else
2951 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
Neil Boothcbc69f82002-06-05 20:27:12 +00002952 macro->line = pfile->directive_line;
2953 macro->params = 0;
2954 macro->paramc = 0;
2955 macro->variadic = 0;
Neil Booth23345bb2003-03-14 21:47:50 +00002956 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
Neil Boothcbc69f82002-06-05 20:27:12 +00002957 macro->count = 0;
2958 macro->fun_like = 0;
Joseph Myersaa508502009-04-19 18:10:56 +01002959 macro->extra_tokens = 0;
Neil Booth7065e132001-02-14 07:38:20 +00002960 /* To suppress some diagnostics. */
Per Bothner12f9df42004-02-11 07:29:30 -08002961 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
Neil Booth7065e132001-02-14 07:38:20 +00002962
Neil Boothcbc69f82002-06-05 20:27:12 +00002963 if (CPP_OPTION (pfile, traditional))
2964 ok = _cpp_create_trad_definition (pfile, macro);
2965 else
2966 {
Neil Boothcbc69f82002-06-05 20:27:12 +00002967 ok = create_iso_definition (pfile, macro);
2968
Tom Tromeyee380362007-01-30 15:46:01 +00002969 /* We set the type for SEEN_EOL() in directives.c.
Neil Boothcbc69f82002-06-05 20:27:12 +00002970
2971 Longer term we should lex the whole line before coming here,
2972 and just copy the expansion. */
Neil Boothcbc69f82002-06-05 20:27:12 +00002973
2974 /* Stop the lexer accepting __VA_ARGS__. */
2975 pfile->state.va_args_ok = 0;
2976 }
2977
2978 /* Clear the fast argument lookup indices. */
2979 for (i = macro->paramc; i-- > 0; )
Zack Weinberg4977bab2002-12-16 18:23:00 +00002980 {
2981 struct cpp_hashnode *node = macro->params[i];
2982 node->flags &= ~ NODE_MACRO_ARG;
2983 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
2984 }
Neil Boothcbc69f82002-06-05 20:27:12 +00002985
2986 if (!ok)
2987 return ok;
2988
Neil Boothc2734e02002-07-26 16:29:31 +00002989 if (node->type == NT_MACRO)
Neil Booth93c803682000-10-28 17:59:06 +00002990 {
Neil Bootha69cbaa2002-07-23 22:57:49 +00002991 if (CPP_OPTION (pfile, warn_unused_macros))
2992 _cpp_warn_if_unused_macro (pfile, node, NULL);
2993
Neil Boothcbc69f82002-06-05 20:27:12 +00002994 if (warn_of_redefinition (pfile, node, macro))
Neil Booth93c803682000-10-28 17:59:06 +00002995 {
Simon Baldwin87cf0652010-04-07 17:18:10 +00002996 const int reason = (node->flags & NODE_BUILTIN)
2997 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
Joseph Myers148e4212009-03-29 23:56:07 +01002998 bool warned;
Simon Baldwin87cf0652010-04-07 17:18:10 +00002999
3000 warned = cpp_pedwarning_with_line (pfile, reason,
3001 pfile->directive_line, 0,
3002 "\"%s\" redefined",
3003 NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +00003004
Joseph Myers148e4212009-03-29 23:56:07 +01003005 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3006 cpp_error_with_line (pfile, CPP_DL_NOTE,
Neil Boothcbc69f82002-06-05 20:27:12 +00003007 node->value.macro->line, 0,
3008 "this is the location of the previous definition");
Zack Weinberg711b8822000-07-18 00:59:49 +00003009 }
Zack Weinberg711b8822000-07-18 00:59:49 +00003010 }
3011
Neil Boothc2734e02002-07-26 16:29:31 +00003012 if (node->type != NT_VOID)
3013 _cpp_free_definition (node);
3014
Zack Weinberg711b8822000-07-18 00:59:49 +00003015 /* Enter definition in hash table. */
Neil Booth93c803682000-10-28 17:59:06 +00003016 node->type = NT_MACRO;
3017 node->value.macro = macro;
Tom Tromey607f74e2007-11-30 18:24:01 +00003018 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
Tom Tromeyec460532008-01-22 21:43:49 +00003019 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3020 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3021 in the C standard, as something that one must use in C++.
3022 However DR#593 indicates that these aren't actually mentioned
3023 in the C++ standard. We special-case them anyway. */
3024 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3025 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
Neil Booth618cdda2001-02-25 09:43:03 +00003026 node->flags |= NODE_WARN;
Zack Weinberg711b8822000-07-18 00:59:49 +00003027
Ben Elliston5950c3c2008-07-14 05:09:48 +00003028 /* If user defines one of the conditional macros, remove the
3029 conditional flag */
3030 node->flags &= ~NODE_CONDITIONAL;
3031
Neil Booth93c803682000-10-28 17:59:06 +00003032 return ok;
Zack Weinberg711b8822000-07-18 00:59:49 +00003033}
3034
Neil Boothd15a58c2002-01-03 18:32:55 +00003035/* Warn if a token in STRING matches one of a function-like MACRO's
3036 parameters. */
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003037static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00003038check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3039 const cpp_string *string)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003040{
Neil Booth93c803682000-10-28 17:59:06 +00003041 unsigned int i, len;
Neil Booth6338b352003-04-23 22:44:06 +00003042 const uchar *p, *q, *limit;
Kazu Hiratadf383482002-05-22 22:02:16 +00003043
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003044 /* Loop over the string. */
Neil Booth6338b352003-04-23 22:44:06 +00003045 limit = string->text + string->len - 1;
3046 for (p = string->text + 1; p < limit; p = q)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003047 {
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003048 /* Find the start of an identifier. */
Greg McGary61c16b12000-09-15 21:25:02 +00003049 while (p < limit && !is_idstart (*p))
3050 p++;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003051
3052 /* Find the end of the identifier. */
3053 q = p;
Greg McGary61c16b12000-09-15 21:25:02 +00003054 while (q < limit && is_idchar (*q))
3055 q++;
Neil Booth93c803682000-10-28 17:59:06 +00003056
3057 len = q - p;
3058
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003059 /* Loop over the function macro arguments to see if the
3060 identifier inside the string matches one of them. */
Neil Booth93c803682000-10-28 17:59:06 +00003061 for (i = 0; i < macro->paramc; i++)
3062 {
3063 const cpp_hashnode *node = macro->params[i];
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003064
Neil Booth2a967f32001-05-20 06:26:45 +00003065 if (NODE_LEN (node) == len
3066 && !memcmp (p, NODE_NAME (node), len))
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003067 {
John David Anglin0527bc42003-11-01 22:56:54 +00003068 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinbergf458d1d2002-02-27 18:48:07 +00003069 "macro argument \"%s\" would be stringified in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +00003070 NODE_NAME (node));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003071 break;
3072 }
3073 }
3074 }
3075}
Neil Booth93c803682000-10-28 17:59:06 +00003076
Neil Booth70961712001-06-23 11:34:41 +00003077/* Returns the name, arguments and expansion of a macro, in a format
3078 suitable to be read back in again, and therefore also for DWARF 2
3079 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3080 Caller is expected to generate the "#define" bit if needed. The
Neil Booth93c803682000-10-28 17:59:06 +00003081 returned text is temporary, and automatically freed later. */
Neil Booth93c803682000-10-28 17:59:06 +00003082const unsigned char *
Jakub Jelinek8e680db2010-06-11 20:37:34 +02003083cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00003084{
3085 unsigned int i, len;
Jakub Jelinek8e680db2010-06-11 20:37:34 +02003086 const cpp_macro *macro;
Neil Booth93c803682000-10-28 17:59:06 +00003087 unsigned char *buffer;
3088
3089 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3090 {
Jakub Jelinek8e680db2010-06-11 20:37:34 +02003091 if (node->type != NT_MACRO
3092 || !pfile->cb.user_builtin_macro
3093 || !pfile->cb.user_builtin_macro (pfile, node))
3094 {
3095 cpp_error (pfile, CPP_DL_ICE,
3096 "invalid hash type %d in cpp_macro_definition",
3097 node->type);
3098 return 0;
3099 }
Neil Booth93c803682000-10-28 17:59:06 +00003100 }
3101
Jakub Jelinek8e680db2010-06-11 20:37:34 +02003102 macro = node->value.macro;
Neil Booth93c803682000-10-28 17:59:06 +00003103 /* Calculate length. */
Neil Booth8dc901d2002-05-29 19:30:07 +00003104 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
Neil Booth93c803682000-10-28 17:59:06 +00003105 if (macro->fun_like)
3106 {
Jim Blandy64d08262002-04-05 00:12:40 +00003107 len += 4; /* "()" plus possible final ".." of named
3108 varargs (we have + 1 below). */
Neil Booth93c803682000-10-28 17:59:06 +00003109 for (i = 0; i < macro->paramc; i++)
Jim Blandy64d08262002-04-05 00:12:40 +00003110 len += NODE_LEN (macro->params[i]) + 1; /* "," */
Neil Booth93c803682000-10-28 17:59:06 +00003111 }
3112
Eric Christopher6da55c02005-02-15 23:18:04 +00003113 /* This should match below where we fill in the buffer. */
Neil Booth278c4662002-06-19 05:40:08 +00003114 if (CPP_OPTION (pfile, traditional))
3115 len += _cpp_replacement_text_len (macro);
3116 else
Neil Booth93c803682000-10-28 17:59:06 +00003117 {
Joseph Myersaa508502009-04-19 18:10:56 +01003118 unsigned int count = macro_real_token_count (macro);
3119 for (i = 0; i < count; i++)
Neil Booth278c4662002-06-19 05:40:08 +00003120 {
3121 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00003122
Neil Booth278c4662002-06-19 05:40:08 +00003123 if (token->type == CPP_MACRO_ARG)
Joseph Myers9a0c6182009-05-10 15:27:32 +01003124 len += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
Neil Booth278c4662002-06-19 05:40:08 +00003125 else
Eric Christopher6da55c02005-02-15 23:18:04 +00003126 len += cpp_token_len (token);
3127
Neil Booth278c4662002-06-19 05:40:08 +00003128 if (token->flags & STRINGIFY_ARG)
3129 len++; /* "#" */
3130 if (token->flags & PASTE_LEFT)
3131 len += 3; /* " ##" */
Eric Christopher6da55c02005-02-15 23:18:04 +00003132 if (token->flags & PREV_WHITE)
3133 len++; /* " " */
Neil Booth278c4662002-06-19 05:40:08 +00003134 }
Neil Booth93c803682000-10-28 17:59:06 +00003135 }
3136
3137 if (len > pfile->macro_buffer_len)
Alexandre Oliva4b49c362001-01-09 09:30:43 +00003138 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00003139 pfile->macro_buffer = XRESIZEVEC (unsigned char,
3140 pfile->macro_buffer, len);
Alexandre Oliva4b49c362001-01-09 09:30:43 +00003141 pfile->macro_buffer_len = len;
3142 }
Neil Booth70961712001-06-23 11:34:41 +00003143
3144 /* Fill in the buffer. Start with the macro name. */
Neil Booth93c803682000-10-28 17:59:06 +00003145 buffer = pfile->macro_buffer;
Neil Booth70961712001-06-23 11:34:41 +00003146 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
3147 buffer += NODE_LEN (node);
Neil Booth93c803682000-10-28 17:59:06 +00003148
3149 /* Parameter names. */
3150 if (macro->fun_like)
3151 {
3152 *buffer++ = '(';
3153 for (i = 0; i < macro->paramc; i++)
3154 {
3155 cpp_hashnode *param = macro->params[i];
3156
3157 if (param != pfile->spec_nodes.n__VA_ARGS__)
3158 {
Neil Bootha28c50352001-05-16 22:02:09 +00003159 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3160 buffer += NODE_LEN (param);
Neil Booth93c803682000-10-28 17:59:06 +00003161 }
3162
3163 if (i + 1 < macro->paramc)
Kazu Hiratadf383482002-05-22 22:02:16 +00003164 /* Don't emit a space after the comma here; we're trying
3165 to emit a Dwarf-friendly definition, and the Dwarf spec
3166 forbids spaces in the argument list. */
Jim Blandy64d08262002-04-05 00:12:40 +00003167 *buffer++ = ',';
Neil Booth28e0f042000-12-09 12:06:37 +00003168 else if (macro->variadic)
Neil Booth93c803682000-10-28 17:59:06 +00003169 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3170 }
3171 *buffer++ = ')';
3172 }
3173
Jim Blandye37b38d2002-03-19 21:43:39 +00003174 /* The Dwarf spec requires a space after the macro name, even if the
3175 definition is the empty string. */
3176 *buffer++ = ' ';
3177
Neil Booth278c4662002-06-19 05:40:08 +00003178 if (CPP_OPTION (pfile, traditional))
3179 buffer = _cpp_copy_replacement_text (macro, buffer);
3180 else if (macro->count)
Neil Booth93c803682000-10-28 17:59:06 +00003181 /* Expansion tokens. */
Neil Booth93c803682000-10-28 17:59:06 +00003182 {
Joseph Myersaa508502009-04-19 18:10:56 +01003183 unsigned int count = macro_real_token_count (macro);
3184 for (i = 0; i < count; i++)
Neil Booth93c803682000-10-28 17:59:06 +00003185 {
Neil Booth601328b2002-05-16 05:53:24 +00003186 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00003187
3188 if (token->flags & PREV_WHITE)
3189 *buffer++ = ' ';
3190 if (token->flags & STRINGIFY_ARG)
3191 *buffer++ = '#';
3192
3193 if (token->type == CPP_MACRO_ARG)
3194 {
Neil Bootha28c50352001-05-16 22:02:09 +00003195 memcpy (buffer,
Joseph Myers9a0c6182009-05-10 15:27:32 +01003196 NODE_NAME (macro->params[token->val.macro_arg.arg_no - 1]),
3197 NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]));
3198 buffer += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
Neil Booth93c803682000-10-28 17:59:06 +00003199 }
3200 else
Geoffrey Keating47e20492005-03-12 10:44:06 +00003201 buffer = cpp_spell_token (pfile, token, buffer, false);
Neil Booth93c803682000-10-28 17:59:06 +00003202
3203 if (token->flags & PASTE_LEFT)
3204 {
3205 *buffer++ = ' ';
3206 *buffer++ = '#';
3207 *buffer++ = '#';
3208 /* Next has PREV_WHITE; see _cpp_create_definition. */
3209 }
3210 }
3211 }
3212
3213 *buffer = '\0';
3214 return pfile->macro_buffer;
3215}