blob: 89d70f1da9e454eb662a4e352078581f4f386104 [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,
Gary Funckb492b682012-01-09 08:48:43 +00004 2006, 2007, 2008, 2009, 2010, 2011, 2012 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 *);
Dodji Seketeli828a7f72012-05-29 09:36:29 +0000103static bool paste_tokens (cpp_reader *, source_location,
104 const cpp_token **, const cpp_token *);
Tom Tromey92582b72011-10-17 09:59:12 +0000105static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
106static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
107static void delete_macro_args (_cpp_buff*, unsigned num_args);
108static void set_arg_token (macro_arg *, const cpp_token *,
109 source_location, size_t,
110 enum macro_arg_token_kind,
111 bool);
112static const source_location *get_arg_token_location (const macro_arg *,
113 enum macro_arg_token_kind);
114static const cpp_token **arg_token_ptr_at (const macro_arg *,
115 size_t,
116 enum macro_arg_token_kind,
117 source_location **virt_location);
118
119static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
120 enum macro_arg_token_kind,
121 const macro_arg *,
122 const cpp_token **);
123static const cpp_token *macro_arg_token_iter_get_token
124(const macro_arg_token_iter *it);
125static source_location macro_arg_token_iter_get_location
126(const macro_arg_token_iter *);
127static void macro_arg_token_iter_forward (macro_arg_token_iter *);
128static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
129 source_location **);
130static size_t tokens_buff_count (_cpp_buff *);
131static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
Dodji Seketeli9b554be2011-12-05 09:20:59 +0000132static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
133 source_location *,
134 const cpp_token *,
135 source_location,
136 source_location,
137 const struct line_map *,
138 unsigned int);
Tom Tromey92582b72011-10-17 09:59:12 +0000139
140static const cpp_token **tokens_buff_add_token (_cpp_buff *,
141 source_location *,
142 const cpp_token *,
143 source_location,
144 source_location,
145 const struct line_map *,
146 unsigned int);
Dodji Seketeli9b554be2011-12-05 09:20:59 +0000147static inline void tokens_buff_remove_last_token (_cpp_buff *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000148static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
Tom Tromey92582b72011-10-17 09:59:12 +0000149 macro_arg *, source_location);
Jakub Jelinek765d6002008-01-25 10:01:27 +0100150static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
Tom Tromey92582b72011-10-17 09:59:12 +0000151 _cpp_buff **, unsigned *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000152static bool create_iso_definition (cpp_reader *, cpp_macro *);
Neil Booth93c803682000-10-28 17:59:06 +0000153
Neil Booth93c803682000-10-28 17:59:06 +0000154/* #define directive parsing and handling. */
155
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000156static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
157static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
Jakub Jelinek8e680db2010-06-11 20:37:34 +0200158static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000159 const cpp_macro *);
160static bool parse_params (cpp_reader *, cpp_macro *);
161static void check_trad_stringification (cpp_reader *, const cpp_macro *,
162 const cpp_string *);
Tom Tromey92582b72011-10-17 09:59:12 +0000163static bool reached_end_of_context (cpp_context *);
164static void consume_next_token_from_context (cpp_reader *pfile,
165 const cpp_token **,
166 source_location *);
167static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
Zack Weinberg711b8822000-07-18 00:59:49 +0000168
Dodji Seketeli36002182012-04-30 11:41:46 +0000169static cpp_hashnode* macro_of_context (cpp_context *context);
170
Dodji Seketeli828a7f72012-05-29 09:36:29 +0000171static bool in_macro_expansion_p (cpp_reader *pfile);
172
Tom Tromey64a1a422011-10-17 09:59:52 +0000173/* Statistical counter tracking the number of macros that got
174 expanded. */
175unsigned num_expanded_macros_counter = 0;
176/* Statistical counter tracking the total number tokens resulting
177 from macro expansion. */
178unsigned num_macro_tokens_counter = 0;
179
Neil Bootha69cbaa2002-07-23 22:57:49 +0000180/* Emits a warning if NODE is a macro defined in the main file that
181 has not been used. */
182int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000183_cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
184 void *v ATTRIBUTE_UNUSED)
Neil Bootha69cbaa2002-07-23 22:57:49 +0000185{
186 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
187 {
188 cpp_macro *macro = node->value.macro;
189
190 if (!macro->used
Per Bothner50f59cd2004-01-19 21:30:18 -0800191 && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000192 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
193 "macro \"%s\" is not used", NODE_NAME (node));
Neil Bootha69cbaa2002-07-23 22:57:49 +0000194 }
195
196 return 1;
197}
198
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000199/* Allocates and returns a CPP_STRING token, containing TEXT of length
200 LEN, after null-terminating it. TEXT must be in permanent storage. */
201static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000202new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
Zack Weinberg711b8822000-07-18 00:59:49 +0000203{
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000204 cpp_token *token = _cpp_temp_token (pfile);
Zack Weinberg711b8822000-07-18 00:59:49 +0000205
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000206 text[len] = '\0';
Neil Booth93c803682000-10-28 17:59:06 +0000207 token->type = CPP_STRING;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000208 token->val.str.len = len;
209 token->val.str.text = text;
Neil Booth93c803682000-10-28 17:59:06 +0000210 token->flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000211 return token;
Neil Booth93c803682000-10-28 17:59:06 +0000212}
213
Neil Booth93c803682000-10-28 17:59:06 +0000214static const char * const monthnames[] =
215{
216 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
217 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
218};
219
Zack Weinberg21b11492004-09-09 19:16:56 +0000220/* Helper function for builtin_macro. Returns the text generated by
221 a builtin macro. */
Neil Booth278c4662002-06-19 05:40:08 +0000222const uchar *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000223_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +0000224{
Neil Booth278c4662002-06-19 05:40:08 +0000225 const uchar *result = NULL;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000226 linenum_type number = 1;
Neil Booth644edda2001-10-02 12:57:24 +0000227
Neil Booth93c803682000-10-28 17:59:06 +0000228 switch (node->value.builtin)
229 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000230 default:
John David Anglin0527bc42003-11-01 22:56:54 +0000231 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +0000232 NODE_NAME (node));
Neil Booth278c4662002-06-19 05:40:08 +0000233 break;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000234
Grigory Zagorodnevbe8ac3e2006-02-18 09:25:31 +0000235 case BT_TIMESTAMP:
236 {
237 cpp_buffer *pbuffer = cpp_get_buffer (pfile);
238 if (pbuffer->timestamp == NULL)
239 {
240 /* Initialize timestamp value of the assotiated file. */
241 struct _cpp_file *file = cpp_get_file (pbuffer);
242 if (file)
243 {
244 /* Generate __TIMESTAMP__ string, that represents
245 the date and time of the last modification
246 of the current source file. The string constant
247 looks like "Sun Sep 16 01:03:52 1973". */
248 struct tm *tb = NULL;
249 struct stat *st = _cpp_get_file_stat (file);
250 if (st)
251 tb = localtime (&st->st_mtime);
252 if (tb)
253 {
254 char *str = asctime (tb);
255 size_t len = strlen (str);
256 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
257 buf[0] = '"';
258 strcpy ((char *) buf + 1, str);
259 buf[len] = '"';
260 pbuffer->timestamp = buf;
261 }
262 else
263 {
264 cpp_errno (pfile, CPP_DL_WARNING,
265 "could not determine file timestamp");
Kris Van Heesb6baa672008-04-18 13:58:08 +0000266 pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
Grigory Zagorodnevbe8ac3e2006-02-18 09:25:31 +0000267 }
268 }
269 }
270 result = pbuffer->timestamp;
271 }
272 break;
Neil Booth93c803682000-10-28 17:59:06 +0000273 case BT_FILE:
274 case BT_BASE_FILE:
Zack Weinberg711b8822000-07-18 00:59:49 +0000275 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000276 unsigned int len;
Neil Booth0bda4762000-12-11 07:45:16 +0000277 const char *name;
Neil Booth562a5c22002-04-21 18:46:42 +0000278 uchar *buf;
Tom Tromey46427372011-10-17 11:58:56 +0200279
280 if (node->value.builtin == BT_FILE)
281 name = linemap_get_expansion_filename (pfile->line_table,
282 pfile->line_table->highest_line);
283 else
284 {
Gary Funckb492b682012-01-09 08:48:43 +0000285 name = _cpp_get_file_name (pfile->main_file);
286 if (!name)
287 abort ();
Tom Tromey46427372011-10-17 11:58:56 +0200288 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000289 len = strlen (name);
Andrew Pinski651ed942005-11-04 00:23:01 +0000290 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
Neil Booth278c4662002-06-19 05:40:08 +0000291 result = buf;
292 *buf = '"';
293 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
294 *buf++ = '"';
295 *buf = '\0';
Zack Weinberg711b8822000-07-18 00:59:49 +0000296 }
Neil Booth644edda2001-10-02 12:57:24 +0000297 break;
298
Neil Booth93c803682000-10-28 17:59:06 +0000299 case BT_INCLUDE_LEVEL:
Neil Boothd8693c62001-08-21 23:05:12 +0000300 /* The line map depth counts the primary source as level 1, but
301 historically __INCLUDE_DEPTH__ has called the primary source
302 level 0. */
Per Bothner50f59cd2004-01-19 21:30:18 -0800303 number = pfile->line_table->depth - 1;
Neil Booth644edda2001-10-02 12:57:24 +0000304 break;
Neil Booth93c803682000-10-28 17:59:06 +0000305
306 case BT_SPECLINE:
307 /* If __LINE__ is embedded in a macro, it must expand to the
308 line of the macro's invocation, not its definition.
309 Otherwise things like assert() will not work properly. */
Tom Tromey46427372011-10-17 11:58:56 +0200310 number = linemap_get_expansion_line (pfile->line_table,
311 CPP_OPTION (pfile, traditional)
312 ? pfile->line_table->highest_line
313 : pfile->cur_token[-1].src_loc);
Neil Booth644edda2001-10-02 12:57:24 +0000314 break;
Neil Booth93c803682000-10-28 17:59:06 +0000315
Zack Weinberg5279d732002-05-16 19:03:02 +0000316 /* __STDC__ has the value 1 under normal circumstances.
317 However, if (a) we are in a system header, (b) the option
Zack Weinberg2a1dc0d2002-05-21 21:55:37 +0000318 stdc_0_in_system_headers is true (set by target config), and
319 (c) we are not in strictly conforming mode, then it has the
Jakub Jelinek83900992006-01-23 22:50:15 +0100320 value 0. (b) and (c) are already checked in cpp_init_builtins. */
Neil Booth93c803682000-10-28 17:59:06 +0000321 case BT_STDC:
Jakub Jelinek83900992006-01-23 22:50:15 +0100322 if (cpp_in_system_header (pfile))
323 number = 0;
324 else
325 number = 1;
Neil Booth644edda2001-10-02 12:57:24 +0000326 break;
Neil Booth93c803682000-10-28 17:59:06 +0000327
328 case BT_DATE:
329 case BT_TIME:
Neil Booth278c4662002-06-19 05:40:08 +0000330 if (pfile->date == NULL)
Neil Booth93c803682000-10-28 17:59:06 +0000331 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000332 /* Allocate __DATE__ and __TIME__ strings from permanent
333 storage. We only do this once, and don't generate them
334 at init time, because time() and localtime() are very
335 slow on some systems. */
Zack Weinberg56da7202002-08-02 04:18:16 +0000336 time_t tt;
337 struct tm *tb = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000338
Zack Weinberg56da7202002-08-02 04:18:16 +0000339 /* (time_t) -1 is a legitimate value for "number of seconds
340 since the Epoch", so we have to do a little dance to
341 distinguish that from a genuine error. */
342 errno = 0;
343 tt = time(NULL);
344 if (tt != (time_t)-1 || errno == 0)
345 tb = localtime (&tt);
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000346
Zack Weinberg56da7202002-08-02 04:18:16 +0000347 if (tb)
348 {
349 pfile->date = _cpp_unaligned_alloc (pfile,
350 sizeof ("\"Oct 11 1347\""));
351 sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000352 monthnames[tb->tm_mon], tb->tm_mday,
353 tb->tm_year + 1900);
Zack Weinberg56da7202002-08-02 04:18:16 +0000354
355 pfile->time = _cpp_unaligned_alloc (pfile,
356 sizeof ("\"12:34:56\""));
357 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
358 tb->tm_hour, tb->tm_min, tb->tm_sec);
359 }
360 else
361 {
John David Anglin0527bc42003-11-01 22:56:54 +0000362 cpp_errno (pfile, CPP_DL_WARNING,
Zack Weinberg56da7202002-08-02 04:18:16 +0000363 "could not determine date and time");
364
Kris Van Heesb6baa672008-04-18 13:58:08 +0000365 pfile->date = UC"\"??? ?? ????\"";
366 pfile->time = UC"\"??:??:??\"";
Zack Weinberg56da7202002-08-02 04:18:16 +0000367 }
Neil Booth93c803682000-10-28 17:59:06 +0000368 }
Neil Booth93c803682000-10-28 17:59:06 +0000369
Neil Booth644edda2001-10-02 12:57:24 +0000370 if (node->value.builtin == BT_DATE)
Neil Booth278c4662002-06-19 05:40:08 +0000371 result = pfile->date;
Neil Booth644edda2001-10-02 12:57:24 +0000372 else
Neil Booth278c4662002-06-19 05:40:08 +0000373 result = pfile->time;
Neil Booth644edda2001-10-02 12:57:24 +0000374 break;
Ollie Wilda7020452007-05-24 20:55:36 +0000375
376 case BT_COUNTER:
Ollie Wildccfc4c92007-07-30 18:29:20 +0000377 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
378 cpp_error (pfile, CPP_DL_ERROR,
379 "__COUNTER__ expanded inside directive with -fdirectives-only");
Ollie Wilda7020452007-05-24 20:55:36 +0000380 number = pfile->counter++;
381 break;
Neil Booth278c4662002-06-19 05:40:08 +0000382 }
Neil Booth644edda2001-10-02 12:57:24 +0000383
Neil Booth278c4662002-06-19 05:40:08 +0000384 if (result == NULL)
385 {
386 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
387 result = _cpp_unaligned_alloc (pfile, 21);
388 sprintf ((char *) result, "%u", number);
389 }
390
391 return result;
392}
393
394/* Convert builtin macros like __FILE__ to a token and push it on the
Zack Weinberg21b11492004-09-09 19:16:56 +0000395 context stack. Also handles _Pragma, for which a new token may not
396 be created. Returns 1 if it generates a new token context, 0 to
Neil Booth278c4662002-06-19 05:40:08 +0000397 return the token to the caller. */
398static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000399builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth278c4662002-06-19 05:40:08 +0000400{
401 const uchar *buf;
Neil Booth26aea072003-04-19 00:22:51 +0000402 size_t len;
403 char *nbuf;
Neil Booth278c4662002-06-19 05:40:08 +0000404
405 if (node->value.builtin == BT_PRAGMA)
406 {
Neil Booth644edda2001-10-02 12:57:24 +0000407 /* Don't interpret _Pragma within directives. The standard is
408 not clear on this, but to me this makes most sense. */
409 if (pfile->state.in_directive)
410 return 0;
411
Tom Tromey5b9a40d2007-10-31 14:50:13 +0000412 return _cpp_do__Pragma (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000413 }
Neil Booth644edda2001-10-02 12:57:24 +0000414
Neil Booth278c4662002-06-19 05:40:08 +0000415 buf = _cpp_builtin_macro_text (pfile, node);
Neil Booth26aea072003-04-19 00:22:51 +0000416 len = ustrlen (buf);
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000417 nbuf = (char *) alloca (len + 1);
Neil Booth26aea072003-04-19 00:22:51 +0000418 memcpy (nbuf, buf, len);
419 nbuf[len]='\n';
Neil Booth278c4662002-06-19 05:40:08 +0000420
Per Bothner40de9f72003-10-02 07:30:34 +0000421 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
Neil Booth26aea072003-04-19 00:22:51 +0000422 _cpp_clean_line (pfile);
Neil Booth278c4662002-06-19 05:40:08 +0000423
424 /* Set pfile->cur_token as required by _cpp_lex_direct. */
425 pfile->cur_token = _cpp_temp_token (pfile);
Richard Hendersonbc4071d2006-01-04 08:33:38 -0800426 _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
Neil Booth278c4662002-06-19 05:40:08 +0000427 if (pfile->buffer->cur != pfile->buffer->rlimit)
John David Anglin0527bc42003-11-01 22:56:54 +0000428 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
Neil Booth278c4662002-06-19 05:40:08 +0000429 NODE_NAME (node));
430 _cpp_pop_buffer (pfile);
431
Neil Booth644edda2001-10-02 12:57:24 +0000432 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000433}
434
Neil Boothd15a58c2002-01-03 18:32:55 +0000435/* Copies SRC, of length LEN, to DEST, adding backslashes before all
Andrew Pinski651ed942005-11-04 00:23:01 +0000436 backslashes and double quotes. DEST must be of sufficient size.
437 Returns a pointer to the end of the string. */
Neil Booth562a5c22002-04-21 18:46:42 +0000438uchar *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000439cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
Neil Booth93c803682000-10-28 17:59:06 +0000440{
441 while (len--)
442 {
Neil Booth562a5c22002-04-21 18:46:42 +0000443 uchar c = *src++;
Neil Booth93c803682000-10-28 17:59:06 +0000444
445 if (c == '\\' || c == '"')
446 {
447 *dest++ = '\\';
448 *dest++ = c;
449 }
450 else
Andrew Pinski651ed942005-11-04 00:23:01 +0000451 *dest++ = c;
Neil Booth93c803682000-10-28 17:59:06 +0000452 }
453
454 return dest;
455}
456
Neil Boothd15a58c2002-01-03 18:32:55 +0000457/* Convert a token sequence ARG to a single string token according to
458 the rules of the ISO C #-operator. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000459static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000460stringify_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +0000461{
Neil Booth6338b352003-04-23 22:44:06 +0000462 unsigned char *dest;
Neil Boothece54d52001-09-28 09:40:22 +0000463 unsigned int i, escape_it, backslash_count = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000464 const cpp_token *source = NULL;
Neil Boothece54d52001-09-28 09:40:22 +0000465 size_t len;
Neil Booth93c803682000-10-28 17:59:06 +0000466
Neil Booth6338b352003-04-23 22:44:06 +0000467 if (BUFF_ROOM (pfile->u_buff) < 3)
468 _cpp_extend_buff (pfile, &pfile->u_buff, 3);
469 dest = BUFF_FRONT (pfile->u_buff);
470 *dest++ = '"';
471
Neil Booth93c803682000-10-28 17:59:06 +0000472 /* Loop, reading in the argument's tokens. */
473 for (i = 0; i < arg->count; i++)
474 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000475 const cpp_token *token = arg->first[i];
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000476
477 if (token->type == CPP_PADDING)
478 {
Joseph Myers18f41a12009-04-12 23:20:02 +0100479 if (source == NULL
480 || (!(source->flags & PREV_WHITE)
481 && token->val.source == NULL))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000482 source = token->val.source;
483 continue;
484 }
Neil Booth93c803682000-10-28 17:59:06 +0000485
Kris Van Heesb6baa672008-04-18 13:58:08 +0000486 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
Ian Lance Taylorfd2ab212009-09-02 17:35:30 +0000487 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
Kris Van Heesb6baa672008-04-18 13:58:08 +0000488 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +0200489 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
490 || token->type == CPP_UTF8STRING);
Neil Booth93c803682000-10-28 17:59:06 +0000491
Neil Boothece54d52001-09-28 09:40:22 +0000492 /* Room for each char being written in octal, initial space and
Neil Booth6338b352003-04-23 22:44:06 +0000493 final quote and NUL. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000494 len = cpp_token_len (token);
Neil Booth93c803682000-10-28 17:59:06 +0000495 if (escape_it)
Neil Booth93c803682000-10-28 17:59:06 +0000496 len *= 4;
Neil Booth6338b352003-04-23 22:44:06 +0000497 len += 3;
Neil Booth93c803682000-10-28 17:59:06 +0000498
Neil Boothece54d52001-09-28 09:40:22 +0000499 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth93c803682000-10-28 17:59:06 +0000500 {
Neil Boothece54d52001-09-28 09:40:22 +0000501 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
Neil Booth8c3b2692001-09-30 10:03:11 +0000502 _cpp_extend_buff (pfile, &pfile->u_buff, len);
Neil Boothece54d52001-09-28 09:40:22 +0000503 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth93c803682000-10-28 17:59:06 +0000504 }
505
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000506 /* Leading white space? */
Neil Booth6338b352003-04-23 22:44:06 +0000507 if (dest - 1 != BUFF_FRONT (pfile->u_buff))
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000508 {
509 if (source == NULL)
510 source = token;
511 if (source->flags & PREV_WHITE)
512 *dest++ = ' ';
513 }
514 source = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000515
516 if (escape_it)
517 {
Neil Boothece54d52001-09-28 09:40:22 +0000518 _cpp_buff *buff = _cpp_get_buff (pfile, len);
519 unsigned char *buf = BUFF_FRONT (buff);
Geoffrey Keating47e20492005-03-12 10:44:06 +0000520 len = cpp_spell_token (pfile, token, buf, true) - buf;
Zack Weinbergdcc229e2002-03-14 18:17:18 +0000521 dest = cpp_quote_string (dest, buf, len);
Neil Boothece54d52001-09-28 09:40:22 +0000522 _cpp_release_buff (pfile, buff);
Neil Booth93c803682000-10-28 17:59:06 +0000523 }
524 else
Geoffrey Keating47e20492005-03-12 10:44:06 +0000525 dest = cpp_spell_token (pfile, token, dest, true);
Neil Booth93c803682000-10-28 17:59:06 +0000526
Neil Booth10676942003-04-22 19:28:00 +0000527 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
Neil Booth93c803682000-10-28 17:59:06 +0000528 backslash_count++;
529 else
530 backslash_count = 0;
531 }
532
533 /* Ignore the final \ of invalid string literals. */
534 if (backslash_count & 1)
535 {
John David Anglin0527bc42003-11-01 22:56:54 +0000536 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000537 "invalid string literal, ignoring final '\\'");
Neil Boothece54d52001-09-28 09:40:22 +0000538 dest--;
Neil Booth93c803682000-10-28 17:59:06 +0000539 }
540
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000541 /* Commit the memory, including NUL, and return the token. */
Neil Booth6338b352003-04-23 22:44:06 +0000542 *dest++ = '"';
Neil Boothece54d52001-09-28 09:40:22 +0000543 len = dest - BUFF_FRONT (pfile->u_buff);
544 BUFF_FRONT (pfile->u_buff) = dest + 1;
545 return new_string_token (pfile, dest - len, len);
Neil Booth93c803682000-10-28 17:59:06 +0000546}
547
Kazu Hiratada7d8302002-09-22 02:03:17 +0000548/* Try to paste two tokens. On success, return nonzero. In any
Neil Boothc9e7a602001-09-27 12:59:38 +0000549 case, PLHS is updated to point to the pasted token, which is
Dodji Seketeli828a7f72012-05-29 09:36:29 +0000550 guaranteed to not have the PASTE_LEFT flag set. LOCATION is
551 the virtual location used for error reporting. */
Neil Boothc9e7a602001-09-27 12:59:38 +0000552static bool
Dodji Seketeli828a7f72012-05-29 09:36:29 +0000553paste_tokens (cpp_reader *pfile, source_location location,
554 const cpp_token **plhs, const cpp_token *rhs)
Neil Boothd63eefb2000-11-20 23:59:26 +0000555{
Jakub Jelinekde000d22006-10-12 11:25:59 +0200556 unsigned char *buf, *end, *lhsend;
Tom Tromeyfca35e12007-05-02 19:33:44 +0000557 cpp_token *lhs;
Neil Boothc9e7a602001-09-27 12:59:38 +0000558 unsigned int len;
Neil Boothd63eefb2000-11-20 23:59:26 +0000559
Tom Tromeyfca35e12007-05-02 19:33:44 +0000560 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000561 buf = (unsigned char *) alloca (len);
Tom Tromeyfca35e12007-05-02 19:33:44 +0000562 end = lhsend = cpp_spell_token (pfile, *plhs, buf, false);
Neil Boothd63eefb2000-11-20 23:59:26 +0000563
Neil Boothc9e7a602001-09-27 12:59:38 +0000564 /* Avoid comment headers, since they are still processed in stage 3.
565 It is simpler to insert a space here, rather than modifying the
566 lexer to ignore comments in some circumstances. Simply returning
567 false doesn't work, since we want to clear the PASTE_LEFT flag. */
Tom Tromeyfca35e12007-05-02 19:33:44 +0000568 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
Neil Boothc9e7a602001-09-27 12:59:38 +0000569 *end++ = ' ';
Tom Tromeyf373b442007-11-01 18:20:48 +0000570 /* In one obscure case we might see padding here. */
571 if (rhs->type != CPP_PADDING)
572 end = cpp_spell_token (pfile, rhs, end, false);
Neil Booth26aea072003-04-19 00:22:51 +0000573 *end = '\n';
Neil Boothd63eefb2000-11-20 23:59:26 +0000574
Per Bothner40de9f72003-10-02 07:30:34 +0000575 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
Neil Booth26aea072003-04-19 00:22:51 +0000576 _cpp_clean_line (pfile);
Neil Boothd63eefb2000-11-20 23:59:26 +0000577
Neil Boothc9e7a602001-09-27 12:59:38 +0000578 /* Set pfile->cur_token as required by _cpp_lex_direct. */
579 pfile->cur_token = _cpp_temp_token (pfile);
Tom Tromeyfca35e12007-05-02 19:33:44 +0000580 lhs = _cpp_lex_direct (pfile);
Jakub Jelinekde000d22006-10-12 11:25:59 +0200581 if (pfile->buffer->cur != pfile->buffer->rlimit)
582 {
Tom Tromeyfca35e12007-05-02 19:33:44 +0000583 source_location saved_loc = lhs->src_loc;
584
Jakub Jelinekde000d22006-10-12 11:25:59 +0200585 _cpp_pop_buffer (pfile);
586 _cpp_backup_tokens (pfile, 1);
587 *lhsend = '\0';
Neil Boothd63eefb2000-11-20 23:59:26 +0000588
Tom Tromeyfca35e12007-05-02 19:33:44 +0000589 /* We have to remove the PASTE_LEFT flag from the old lhs, but
590 we want to keep the new location. */
591 *lhs = **plhs;
592 *plhs = lhs;
593 lhs->src_loc = saved_loc;
594 lhs->flags &= ~PASTE_LEFT;
595
Jakub Jelinekde000d22006-10-12 11:25:59 +0200596 /* Mandatory error for all apart from assembler. */
597 if (CPP_OPTION (pfile, lang) != CLK_ASM)
Dodji Seketeli828a7f72012-05-29 09:36:29 +0000598 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
Jakub Jelinekde000d22006-10-12 11:25:59 +0200599 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
600 buf, cpp_token_as_text (pfile, rhs));
601 return false;
602 }
603
Tom Tromeyfca35e12007-05-02 19:33:44 +0000604 *plhs = lhs;
Jakub Jelinekde000d22006-10-12 11:25:59 +0200605 _cpp_pop_buffer (pfile);
606 return true;
Neil Boothd63eefb2000-11-20 23:59:26 +0000607}
608
Neil Boothd15a58c2002-01-03 18:32:55 +0000609/* Handles an arbitrarily long sequence of ## operators, with initial
610 operand LHS. This implementation is left-associative,
611 non-recursive, and finishes a paste before handling succeeding
612 ones. If a paste fails, we back up to the RHS of the failing ##
613 operator before pushing the context containing the result of prior
614 successful pastes, with the effect that the RHS appears in the
615 output stream after the pasted LHS normally. */
Neil Booth93c803682000-10-28 17:59:06 +0000616static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000617paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
Neil Booth93c803682000-10-28 17:59:06 +0000618{
Tom Tromey92582b72011-10-17 09:59:12 +0000619 const cpp_token *rhs = NULL;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000620 cpp_context *context = pfile->context;
Dodji Seketeli0ff2b8a2012-04-30 11:41:21 +0000621 source_location virt_loc = 0;
622
Dodji Seketeli828a7f72012-05-29 09:36:29 +0000623 /* We are expanding a macro and we must have been called on a token
624 that appears at the left hand side of a ## operator. */
625 if (macro_of_context (pfile->context) == NULL
626 || (!(lhs->flags & PASTE_LEFT)))
Dodji Seketeli0ff2b8a2012-04-30 11:41:21 +0000627 abort ();
628
629 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
630 /* The caller must have called consume_next_token_from_context
631 right before calling us. That has incremented the pointer to
632 the current virtual location. So it now points to the location
633 of the token that comes right after *LHS. We want the
634 resulting pasted token to have the location of the current
635 *LHS, though. */
636 virt_loc = context->c.mc->cur_virt_loc[-1];
Dodji Seketeli828a7f72012-05-29 09:36:29 +0000637 else
638 /* We are not tracking macro expansion. So the best virtual
639 location we can get here is the expansion point of the macro we
640 are currently expanding. */
641 virt_loc = pfile->invocation_location;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000642
Neil Booth93c803682000-10-28 17:59:06 +0000643 do
644 {
645 /* Take the token directly from the current context. We can do
646 this, because we are in the replacement list of either an
647 object-like macro, or a function-like macro with arguments
648 inserted. In either case, the constraints to #define
Neil Boothd63eefb2000-11-20 23:59:26 +0000649 guarantee we have at least one more token. */
Tom Tromey92582b72011-10-17 09:59:12 +0000650 if (context->tokens_kind == TOKENS_KIND_DIRECT)
Neil Booth82eda772002-06-04 13:07:06 +0000651 rhs = FIRST (context).token++;
Tom Tromey92582b72011-10-17 09:59:12 +0000652 else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
Neil Booth82eda772002-06-04 13:07:06 +0000653 rhs = *FIRST (context).ptoken++;
Tom Tromey92582b72011-10-17 09:59:12 +0000654 else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
655 {
656 /* So we are in presence of an extended token context, which
657 means that each token in this context has a virtual
658 location attached to it. So let's not forget to update
659 the pointer to the current virtual location of the
660 current token when we update the pointer to the current
661 token */
662
663 rhs = *FIRST (context).ptoken++;
664 /* context->c.mc must be non-null, as if we were not in a
665 macro context, context->tokens_kind could not be equal to
666 TOKENS_KIND_EXTENDED. */
667 context->c.mc->cur_virt_loc++;
668 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000669
670 if (rhs->type == CPP_PADDING)
Tom Tromeyf373b442007-11-01 18:20:48 +0000671 {
672 if (rhs->flags & PASTE_LEFT)
673 abort ();
674 }
Dodji Seketeli828a7f72012-05-29 09:36:29 +0000675 if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
Jakub Jelinekde000d22006-10-12 11:25:59 +0200676 break;
Neil Booth93c803682000-10-28 17:59:06 +0000677 }
678 while (rhs->flags & PASTE_LEFT);
679
Neil Boothc9e7a602001-09-27 12:59:38 +0000680 /* Put the resulting token in its own context. */
Dodji Seketeli0ff2b8a2012-04-30 11:41:21 +0000681 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
682 {
683 source_location *virt_locs = NULL;
684 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
685 tokens_buff_add_token (token_buf, virt_locs, lhs,
686 virt_loc, 0, NULL, 0);
687 push_extended_tokens_context (pfile, context->c.mc->macro_node,
688 token_buf, virt_locs,
689 (const cpp_token **)token_buf->base, 1);
690 }
691 else
692 _cpp_push_token_context (pfile, NULL, lhs, 1);
Neil Booth93c803682000-10-28 17:59:06 +0000693}
694
Neil Booth1ce676a2002-06-09 20:04:17 +0000695/* Returns TRUE if the number of arguments ARGC supplied in an
696 invocation of the MACRO referenced by NODE is valid. An empty
697 invocation to a macro with no parameters should pass ARGC as zero.
698
699 Note that MACRO cannot necessarily be deduced from NODE, in case
700 NODE was redefined whilst collecting arguments. */
701bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000702_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
Neil Booth1ce676a2002-06-09 20:04:17 +0000703{
704 if (argc == macro->paramc)
705 return true;
706
707 if (argc < macro->paramc)
708 {
709 /* As an extension, a rest argument is allowed to not appear in
710 the invocation at all.
711 e.g. #define debug(format, args...) something
712 debug("string");
713
714 This is exactly the same as if there had been an empty rest
715 argument - debug("string", ). */
716
717 if (argc + 1 == macro->paramc && macro->variadic)
718 {
719 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
John David Anglin0527bc42003-11-01 22:56:54 +0000720 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth1ce676a2002-06-09 20:04:17 +0000721 "ISO C99 requires rest arguments to be used");
722 return true;
723 }
724
John David Anglin0527bc42003-11-01 22:56:54 +0000725 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1ce676a2002-06-09 20:04:17 +0000726 "macro \"%s\" requires %u arguments, but only %u given",
727 NODE_NAME (node), macro->paramc, argc);
728 }
729 else
John David Anglin0527bc42003-11-01 22:56:54 +0000730 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth1ce676a2002-06-09 20:04:17 +0000731 "macro \"%s\" passed %u arguments, but takes just %u",
732 NODE_NAME (node), argc, macro->paramc);
733
734 return false;
735}
736
Neil Boothd15a58c2002-01-03 18:32:55 +0000737/* Reads and returns the arguments to a function-like macro
738 invocation. Assumes the opening parenthesis has been processed.
739 If there is an error, emits an appropriate diagnostic and returns
740 NULL. Each argument is terminated by a CPP_EOF token, for the
Jakub Jelinek765d6002008-01-25 10:01:27 +0100741 future benefit of expand_arg(). If there are any deferred
742 #pragma directives among macro arguments, store pointers to the
Tom Tromey92582b72011-10-17 09:59:12 +0000743 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
744
745 What is returned is the buffer that contains the memory allocated
746 to hold the macro arguments. NODE is the name of the macro this
747 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
748 set to the actual number of macro arguments allocated in the
749 returned buffer. */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000750static _cpp_buff *
Jakub Jelinek765d6002008-01-25 10:01:27 +0100751collect_args (cpp_reader *pfile, const cpp_hashnode *node,
Tom Tromey92582b72011-10-17 09:59:12 +0000752 _cpp_buff **pragma_buff, unsigned *num_args)
Neil Booth93c803682000-10-28 17:59:06 +0000753{
Neil Boothb8af0ca2001-09-26 17:52:50 +0000754 _cpp_buff *buff, *base_buff;
755 cpp_macro *macro;
756 macro_arg *args, *arg;
757 const cpp_token *token;
758 unsigned int argc;
Tom Tromey92582b72011-10-17 09:59:12 +0000759 source_location virt_loc;
760 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
761 unsigned num_args_alloced = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000762
Neil Boothb8af0ca2001-09-26 17:52:50 +0000763 macro = node->value.macro;
764 if (macro->paramc)
765 argc = macro->paramc;
766 else
767 argc = 1;
Tom Tromey92582b72011-10-17 09:59:12 +0000768
769#define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
770#define ARG_TOKENS_EXTENT 1000
771
772 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
773 * sizeof (cpp_token *)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000774 + sizeof (macro_arg)));
775 base_buff = buff;
776 args = (macro_arg *) buff->base;
777 memset (args, 0, argc * sizeof (macro_arg));
Neil Boothece54d52001-09-28 09:40:22 +0000778 buff->cur = (unsigned char *) &args[argc];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000779 arg = args, argc = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000780
Neil Boothb8af0ca2001-09-26 17:52:50 +0000781 /* Collect the tokens making up each argument. We don't yet know
782 how many arguments have been supplied, whether too many or too
783 few. Hence the slightly bizarre usage of "argc" and "arg". */
784 do
Neil Booth93c803682000-10-28 17:59:06 +0000785 {
Neil Boothb8af0ca2001-09-26 17:52:50 +0000786 unsigned int paren_depth = 0;
787 unsigned int ntokens = 0;
Tom Tromey92582b72011-10-17 09:59:12 +0000788 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
789 num_args_alloced++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000790
Neil Booth93c803682000-10-28 17:59:06 +0000791 argc++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000792 arg->first = (const cpp_token **) buff->cur;
Tom Tromey92582b72011-10-17 09:59:12 +0000793 if (track_macro_expansion_p)
794 {
795 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
796 arg->virt_locs = XNEWVEC (source_location,
797 virt_locs_capacity);
798 }
Neil Booth93c803682000-10-28 17:59:06 +0000799
Neil Boothb8af0ca2001-09-26 17:52:50 +0000800 for (;;)
801 {
802 /* Require space for 2 new tokens (including a CPP_EOF). */
Neil Boothece54d52001-09-28 09:40:22 +0000803 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000804 {
Neil Booth8c3b2692001-09-30 10:03:11 +0000805 buff = _cpp_append_extend_buff (pfile, buff,
Tom Tromey92582b72011-10-17 09:59:12 +0000806 ARG_TOKENS_EXTENT
807 * sizeof (cpp_token *));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000808 arg->first = (const cpp_token **) buff->cur;
809 }
Tom Tromey92582b72011-10-17 09:59:12 +0000810 if (track_macro_expansion_p
811 && (ntokens + 2 > virt_locs_capacity))
812 {
813 virt_locs_capacity += ARG_TOKENS_EXTENT;
814 arg->virt_locs = XRESIZEVEC (source_location,
815 arg->virt_locs,
816 virt_locs_capacity);
817 }
Neil Booth93c803682000-10-28 17:59:06 +0000818
Tom Tromey92582b72011-10-17 09:59:12 +0000819 token = cpp_get_token_1 (pfile, &virt_loc);
Neil Boothb8af0ca2001-09-26 17:52:50 +0000820
821 if (token->type == CPP_PADDING)
822 {
823 /* Drop leading padding. */
824 if (ntokens == 0)
825 continue;
826 }
827 else if (token->type == CPP_OPEN_PAREN)
828 paren_depth++;
829 else if (token->type == CPP_CLOSE_PAREN)
830 {
831 if (paren_depth-- == 0)
832 break;
833 }
834 else if (token->type == CPP_COMMA)
835 {
836 /* A comma does not terminate an argument within
837 parentheses or as part of a variable argument. */
838 if (paren_depth == 0
839 && ! (macro->variadic && argc == macro->paramc))
840 break;
841 }
842 else if (token->type == CPP_EOF
843 || (token->type == CPP_HASH && token->flags & BOL))
844 break;
Jakub Jelinek765d6002008-01-25 10:01:27 +0100845 else if (token->type == CPP_PRAGMA)
846 {
847 cpp_token *newtok = _cpp_temp_token (pfile);
848
849 /* CPP_PRAGMA token lives in directive_result, which will
850 be overwritten on the next directive. */
851 *newtok = *token;
852 token = newtok;
853 do
854 {
855 if (*pragma_buff == NULL
856 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
857 {
858 _cpp_buff *next;
859 if (*pragma_buff == NULL)
860 *pragma_buff
861 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
862 else
863 {
864 next = *pragma_buff;
865 *pragma_buff
866 = _cpp_get_buff (pfile,
867 (BUFF_FRONT (*pragma_buff)
868 - (*pragma_buff)->base) * 2);
869 (*pragma_buff)->next = next;
870 }
871 }
872 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
873 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
874 if (token->type == CPP_PRAGMA_EOL)
875 break;
Tom Tromey92582b72011-10-17 09:59:12 +0000876 token = cpp_get_token_1 (pfile, &virt_loc);
Jakub Jelinek765d6002008-01-25 10:01:27 +0100877 }
878 while (token->type != CPP_EOF);
879
880 /* In deferred pragmas parsing_args and prevent_expansion
881 had been changed, reset it. */
882 pfile->state.parsing_args = 2;
883 pfile->state.prevent_expansion = 1;
884
885 if (token->type == CPP_EOF)
886 break;
887 else
888 continue;
889 }
Tom Tromey92582b72011-10-17 09:59:12 +0000890 set_arg_token (arg, token, virt_loc,
891 ntokens, MACRO_ARG_TOKEN_NORMAL,
892 CPP_OPTION (pfile, track_macro_expansion));
893 ntokens++;
Neil Boothb8af0ca2001-09-26 17:52:50 +0000894 }
895
896 /* Drop trailing padding. */
897 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
898 ntokens--;
899
900 arg->count = ntokens;
Tom Tromey92582b72011-10-17 09:59:12 +0000901 set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
902 ntokens, MACRO_ARG_TOKEN_NORMAL,
903 CPP_OPTION (pfile, track_macro_expansion));
Neil Boothb8af0ca2001-09-26 17:52:50 +0000904
905 /* Terminate the argument. Excess arguments loop back and
906 overwrite the final legitimate argument, before failing. */
907 if (argc <= macro->paramc)
908 {
Neil Boothece54d52001-09-28 09:40:22 +0000909 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
Neil Boothb8af0ca2001-09-26 17:52:50 +0000910 if (argc != macro->paramc)
911 arg++;
912 }
Neil Booth93c803682000-10-28 17:59:06 +0000913 }
Neil Boothe808ec92002-02-27 07:24:53 +0000914 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
Neil Booth93c803682000-10-28 17:59:06 +0000915
Neil Boothe808ec92002-02-27 07:24:53 +0000916 if (token->type == CPP_EOF)
Neil Booth93c803682000-10-28 17:59:06 +0000917 {
Neil Boothece54d52001-09-28 09:40:22 +0000918 /* We still need the CPP_EOF to end directives, and to end
919 pre-expansion of a macro argument. Step back is not
920 unconditional, since we don't want to return a CPP_EOF to our
921 callers at the end of an -include-d file. */
Neil Boothe808ec92002-02-27 07:24:53 +0000922 if (pfile->context->prev || pfile->state.in_directive)
Neil Boothb8af0ca2001-09-26 17:52:50 +0000923 _cpp_backup_tokens (pfile, 1);
John David Anglin0527bc42003-11-01 22:56:54 +0000924 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000925 "unterminated argument list invoking macro \"%s\"",
Neil Bootha28c50352001-05-16 22:02:09 +0000926 NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +0000927 }
Neil Booth1ce676a2002-06-09 20:04:17 +0000928 else
Neil Booth93c803682000-10-28 17:59:06 +0000929 {
Neil Booth1ce676a2002-06-09 20:04:17 +0000930 /* A single empty argument is counted as no argument. */
931 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
932 argc = 0;
933 if (_cpp_arguments_ok (pfile, macro, node, argc))
Neil Booth58551c22002-08-06 20:35:46 +0000934 {
935 /* GCC has special semantics for , ## b where b is a varargs
936 parameter: we remove the comma if b was omitted entirely.
937 If b was merely an empty argument, the comma is retained.
938 If the macro takes just one (varargs) parameter, then we
939 retain the comma only if we are standards conforming.
940
941 If FIRST is NULL replace_args () swallows the comma. */
942 if (macro->variadic && (argc < macro->paramc
943 || (argc == 1 && args[0].count == 0
944 && !CPP_OPTION (pfile, std))))
945 args[macro->paramc - 1].first = NULL;
Tom Tromey92582b72011-10-17 09:59:12 +0000946 if (num_args)
947 *num_args = num_args_alloced;
Neil Booth58551c22002-08-06 20:35:46 +0000948 return base_buff;
949 }
Neil Booth93c803682000-10-28 17:59:06 +0000950 }
951
Neil Booth1ce676a2002-06-09 20:04:17 +0000952 /* An error occurred. */
Neil Boothb8af0ca2001-09-26 17:52:50 +0000953 _cpp_release_buff (pfile, base_buff);
954 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000955}
956
Neil Boothd6da8362001-10-08 06:15:14 +0000957/* Search for an opening parenthesis to the macro of NODE, in such a
958 way that, if none is found, we don't lose the information in any
959 intervening padding tokens. If we find the parenthesis, collect
Jakub Jelinek765d6002008-01-25 10:01:27 +0100960 the arguments and return the buffer containing them. PRAGMA_BUFF
Tom Tromey92582b72011-10-17 09:59:12 +0000961 argument is the same as in collect_args. If NUM_ARGS is non-NULL,
962 *NUM_ARGS is set to the number of arguments contained in the
963 returned buffer. */
Neil Boothd6da8362001-10-08 06:15:14 +0000964static _cpp_buff *
Jakub Jelinek765d6002008-01-25 10:01:27 +0100965funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
Tom Tromey92582b72011-10-17 09:59:12 +0000966 _cpp_buff **pragma_buff, unsigned *num_args)
Neil Booth93c803682000-10-28 17:59:06 +0000967{
Neil Boothd6da8362001-10-08 06:15:14 +0000968 const cpp_token *token, *padding = NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000969
Neil Boothd6da8362001-10-08 06:15:14 +0000970 for (;;)
Neil Boothbdcbe492001-09-13 20:05:17 +0000971 {
Neil Boothd6da8362001-10-08 06:15:14 +0000972 token = cpp_get_token (pfile);
973 if (token->type != CPP_PADDING)
974 break;
975 if (padding == NULL
976 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
977 padding = token;
Neil Boothbdcbe492001-09-13 20:05:17 +0000978 }
Neil Booth93c803682000-10-28 17:59:06 +0000979
Neil Boothd6da8362001-10-08 06:15:14 +0000980 if (token->type == CPP_OPEN_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000981 {
Neil Boothd6da8362001-10-08 06:15:14 +0000982 pfile->state.parsing_args = 2;
Tom Tromey92582b72011-10-17 09:59:12 +0000983 return collect_args (pfile, node, pragma_buff, num_args);
Neil Booth93c803682000-10-28 17:59:06 +0000984 }
985
Neil Booth9ac3b1b2002-04-21 16:17:55 +0000986 /* CPP_EOF can be the end of macro arguments, or the end of the
987 file. We mustn't back up over the latter. Ugh. */
988 if (token->type != CPP_EOF || token == &pfile->eof)
989 {
990 /* Back up. We may have skipped padding, in which case backing
991 up more than one token when expanding macros is in general
992 too difficult. We re-insert it in its own context. */
993 _cpp_backup_tokens (pfile, 1);
994 if (padding)
Richard Hendersonbc4071d2006-01-04 08:33:38 -0800995 _cpp_push_token_context (pfile, NULL, padding, 1);
Neil Booth9ac3b1b2002-04-21 16:17:55 +0000996 }
Neil Boothd6da8362001-10-08 06:15:14 +0000997
998 return NULL;
Neil Booth93c803682000-10-28 17:59:06 +0000999}
1000
Joseph Myersaa508502009-04-19 18:10:56 +01001001/* Return the real number of tokens in the expansion of MACRO. */
1002static inline unsigned int
1003macro_real_token_count (const cpp_macro *macro)
1004{
1005 unsigned int i;
1006 if (__builtin_expect (!macro->extra_tokens, true))
1007 return macro->count;
1008 for (i = 0; i < macro->count; i++)
1009 if (macro->exp.tokens[i].type == CPP_PASTE)
1010 return i;
1011 abort ();
1012}
1013
Neil Boothd15a58c2002-01-03 18:32:55 +00001014/* Push the context of a macro with hash entry NODE onto the context
1015 stack. If we can successfully expand the macro, we push a context
1016 containing its yet-to-be-rescanned replacement list and return one.
Tom Tromey92582b72011-10-17 09:59:12 +00001017 If there were additionally any unexpanded deferred #pragma
1018 directives among macro arguments, push another context containing
1019 the pragma tokens before the yet-to-be-rescanned replacement list
1020 and return two. Otherwise, we don't push a context and return
1021 zero. LOCATION is the location of the expansion point of the
1022 macro. */
Neil Booth93c803682000-10-28 17:59:06 +00001023static int
Jakub Jelinek765d6002008-01-25 10:01:27 +01001024enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
Tom Tromey92582b72011-10-17 09:59:12 +00001025 const cpp_token *result, source_location location)
Neil Booth93c803682000-10-28 17:59:06 +00001026{
Neil Boothd15a58c2002-01-03 18:32:55 +00001027 /* The presence of a macro invalidates a file's controlling macro. */
Neil Booth644edda2001-10-02 12:57:24 +00001028 pfile->mi_valid = false;
1029
Neil Booth36207112002-05-24 19:26:30 +00001030 pfile->state.angled_headers = false;
1031
Dodji Seketeli828a7f72012-05-29 09:36:29 +00001032 /* From here to when we push the context for the macro later down
1033 this function, we need to flag the fact that we are about to
1034 expand a macro. This is useful when -ftrack-macro-expansion is
1035 turned off. In that case, we need to record the location of the
1036 expansion point of the top-most macro we are about to to expand,
1037 into pfile->invocation_location. But we must not record any such
1038 location once the process of expanding the macro starts; that is,
1039 we must not do that recording between now and later down this
1040 function where set this flag to FALSE. */
1041 pfile->about_to_expand_macro_p = true;
1042
Joseph Myers93d45d92008-04-02 20:42:53 +01001043 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
1044 {
1045 node->flags |= NODE_USED;
Jakub Jelinek8e680db2010-06-11 20:37:34 +02001046 if ((!pfile->cb.user_builtin_macro
1047 || !pfile->cb.user_builtin_macro (pfile, node))
1048 && pfile->cb.used_define)
Joseph Myers93d45d92008-04-02 20:42:53 +01001049 pfile->cb.used_define (pfile, pfile->directive_line, node);
1050 }
1051
Neil Boothd15a58c2002-01-03 18:32:55 +00001052 /* Handle standard macros. */
Neil Booth644edda2001-10-02 12:57:24 +00001053 if (! (node->flags & NODE_BUILTIN))
Neil Booth93c803682000-10-28 17:59:06 +00001054 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001055 cpp_macro *macro = node->value.macro;
Jakub Jelinek765d6002008-01-25 10:01:27 +01001056 _cpp_buff *pragma_buff = NULL;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001057
Neil Boothd6da8362001-10-08 06:15:14 +00001058 if (macro->fun_like)
1059 {
1060 _cpp_buff *buff;
Tom Tromey92582b72011-10-17 09:59:12 +00001061 unsigned num_args = 0;
Neil Boothd6da8362001-10-08 06:15:14 +00001062
1063 pfile->state.prevent_expansion++;
1064 pfile->keep_tokens++;
1065 pfile->state.parsing_args = 1;
Tom Tromey92582b72011-10-17 09:59:12 +00001066 buff = funlike_invocation_p (pfile, node, &pragma_buff,
1067 &num_args);
Neil Boothd6da8362001-10-08 06:15:14 +00001068 pfile->state.parsing_args = 0;
1069 pfile->keep_tokens--;
1070 pfile->state.prevent_expansion--;
1071
1072 if (buff == NULL)
1073 {
1074 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
Simon Baldwin87cf0652010-04-07 17:18:10 +00001075 cpp_warning (pfile, CPP_W_TRADITIONAL,
Neil Boothd6da8362001-10-08 06:15:14 +00001076 "function-like macro \"%s\" must be used with arguments in traditional C",
Simon Baldwin87cf0652010-04-07 17:18:10 +00001077 NODE_NAME (node));
Neil Boothd6da8362001-10-08 06:15:14 +00001078
Jakub Jelinek765d6002008-01-25 10:01:27 +01001079 if (pragma_buff)
1080 _cpp_release_buff (pfile, pragma_buff);
1081
Dodji Seketeli828a7f72012-05-29 09:36:29 +00001082 pfile->about_to_expand_macro_p = false;
Neil Boothd6da8362001-10-08 06:15:14 +00001083 return 0;
1084 }
1085
Neil Boothe808ec92002-02-27 07:24:53 +00001086 if (macro->paramc > 0)
Tom Tromey92582b72011-10-17 09:59:12 +00001087 replace_args (pfile, node, macro,
1088 (macro_arg *) buff->base,
1089 location);
1090 /* Free the memory used by the arguments of this
1091 function-like macro. This memory has been allocated by
1092 funlike_invocation_p and by replace_args. */
1093 delete_macro_args (buff, num_args);
Neil Boothd6da8362001-10-08 06:15:14 +00001094 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001095
1096 /* Disable the macro within its expansion. */
Neil Booth644edda2001-10-02 12:57:24 +00001097 node->flags |= NODE_DISABLED;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001098
Joseph Myers93d45d92008-04-02 20:42:53 +01001099 if (!(node->flags & NODE_USED))
1100 {
1101 node->flags |= NODE_USED;
1102 if (pfile->cb.used_define)
1103 pfile->cb.used_define (pfile, pfile->directive_line, node);
1104 }
1105
Arnaud Charlet3de8a542009-11-20 08:18:16 +00001106 if (pfile->cb.used)
Tom Tromey92582b72011-10-17 09:59:12 +00001107 pfile->cb.used (pfile, location, node);
Arnaud Charlet3de8a542009-11-20 08:18:16 +00001108
Neil Bootha69cbaa2002-07-23 22:57:49 +00001109 macro->used = 1;
1110
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001111 if (macro->paramc == 0)
Tom Tromey92582b72011-10-17 09:59:12 +00001112 {
1113 if (CPP_OPTION (pfile, track_macro_expansion))
1114 {
1115 unsigned int i, count = macro->count;
1116 const cpp_token *src = macro->exp.tokens;
1117 const struct line_map *map;
1118 source_location *virt_locs = NULL;
1119 _cpp_buff *macro_tokens =
1120 tokens_buff_new (pfile, count, &virt_locs);
1121
1122 /* Create a macro map to record the locations of the
1123 tokens that are involved in the expansion. LOCATION
1124 is the location of the macro expansion point. */
1125 map = linemap_enter_macro (pfile->line_table,
1126 node, location, count);
1127 for (i = 0; i < count; ++i)
1128 {
1129 tokens_buff_add_token (macro_tokens, virt_locs,
1130 src, src->src_loc,
1131 src->src_loc, map, i);
1132 ++src;
1133 }
1134 push_extended_tokens_context (pfile, node,
1135 macro_tokens,
1136 virt_locs,
1137 (const cpp_token **)
1138 macro_tokens->base,
1139 count);
Tom Tromey64a1a422011-10-17 09:59:52 +00001140 num_macro_tokens_counter += count;
Tom Tromey92582b72011-10-17 09:59:12 +00001141 }
1142 else
Tom Tromey64a1a422011-10-17 09:59:52 +00001143 {
1144 unsigned tokens_count = macro_real_token_count (macro);
1145 _cpp_push_token_context (pfile, node, macro->exp.tokens,
1146 tokens_count);
1147 num_macro_tokens_counter += tokens_count;
1148 }
Tom Tromey92582b72011-10-17 09:59:12 +00001149 }
Neil Booth644edda2001-10-02 12:57:24 +00001150
Jakub Jelinek765d6002008-01-25 10:01:27 +01001151 if (pragma_buff)
1152 {
1153 if (!pfile->state.in_directive)
1154 _cpp_push_token_context (pfile, NULL,
1155 padding_token (pfile, result), 1);
1156 do
1157 {
Tom Tromey64a1a422011-10-17 09:59:52 +00001158 unsigned tokens_count;
Jakub Jelinek765d6002008-01-25 10:01:27 +01001159 _cpp_buff *tail = pragma_buff->next;
1160 pragma_buff->next = NULL;
Tom Tromey64a1a422011-10-17 09:59:52 +00001161 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1162 - (const cpp_token **) pragma_buff->base);
Jakub Jelinek765d6002008-01-25 10:01:27 +01001163 push_ptoken_context (pfile, NULL, pragma_buff,
1164 (const cpp_token **) pragma_buff->base,
Tom Tromey64a1a422011-10-17 09:59:52 +00001165 tokens_count);
Jakub Jelinek765d6002008-01-25 10:01:27 +01001166 pragma_buff = tail;
Tom Tromey64a1a422011-10-17 09:59:52 +00001167 if (!CPP_OPTION (pfile, track_macro_expansion))
1168 num_macro_tokens_counter += tokens_count;
1169
Jakub Jelinek765d6002008-01-25 10:01:27 +01001170 }
1171 while (pragma_buff != NULL);
Dodji Seketeli828a7f72012-05-29 09:36:29 +00001172 pfile->about_to_expand_macro_p = false;
Jakub Jelinek765d6002008-01-25 10:01:27 +01001173 return 2;
1174 }
1175
Dodji Seketeli828a7f72012-05-29 09:36:29 +00001176 pfile->about_to_expand_macro_p = false;
Neil Booth644edda2001-10-02 12:57:24 +00001177 return 1;
Neil Booth93c803682000-10-28 17:59:06 +00001178 }
Neil Booth644edda2001-10-02 12:57:24 +00001179
Dodji Seketeli828a7f72012-05-29 09:36:29 +00001180 pfile->about_to_expand_macro_p = false;
Neil Boothd15a58c2002-01-03 18:32:55 +00001181 /* Handle built-in macros and the _Pragma operator. */
Neil Booth644edda2001-10-02 12:57:24 +00001182 return builtin_macro (pfile, node);
Zack Weinberg711b8822000-07-18 00:59:49 +00001183}
1184
Tom Tromey92582b72011-10-17 09:59:12 +00001185/* De-allocate the memory used by BUFF which is an array of instances
1186 of macro_arg. NUM_ARGS is the number of instances of macro_arg
1187 present in BUFF. */
1188static void
1189delete_macro_args (_cpp_buff *buff, unsigned num_args)
1190{
1191 macro_arg *macro_args;
1192 unsigned i;
1193
1194 if (buff == NULL)
1195 return;
1196
1197 macro_args = (macro_arg *) buff->base;
1198
1199 /* Walk instances of macro_arg to free their expanded tokens as well
1200 as their macro_arg::virt_locs members. */
1201 for (i = 0; i < num_args; ++i)
1202 {
1203 if (macro_args[i].expanded)
1204 {
1205 free (macro_args[i].expanded);
1206 macro_args[i].expanded = NULL;
1207 }
1208 if (macro_args[i].virt_locs)
1209 {
1210 free (macro_args[i].virt_locs);
1211 macro_args[i].virt_locs = NULL;
1212 }
1213 if (macro_args[i].expanded_virt_locs)
1214 {
1215 free (macro_args[i].expanded_virt_locs);
1216 macro_args[i].expanded_virt_locs = NULL;
1217 }
1218 }
1219 _cpp_free_buff (buff);
1220}
1221
1222/* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1223 to set, LOCATION is its virtual location. "Virtual" location means
1224 the location that encodes loci accross macro expansion. Otherwise
1225 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
1226 argument ARG is supposed to contain. Note that ARG must be
1227 tailored so that it has enough room to contain INDEX + 1 numbers of
1228 tokens, at least. */
1229static void
1230set_arg_token (macro_arg *arg, const cpp_token *token,
1231 source_location location, size_t index,
1232 enum macro_arg_token_kind kind,
1233 bool track_macro_exp_p)
1234{
1235 const cpp_token **token_ptr;
1236 source_location *loc = NULL;
1237
1238 token_ptr =
1239 arg_token_ptr_at (arg, index, kind,
1240 track_macro_exp_p ? &loc : NULL);
1241 *token_ptr = token;
1242
1243 if (loc != NULL)
1244 {
1245#ifdef ENABLE_CHECKING
1246 if (kind == MACRO_ARG_TOKEN_STRINGIFIED
1247 || !track_macro_exp_p)
1248 /* We can't set the location of a stringified argument
1249 token and we can't set any location if we aren't tracking
1250 macro expansion locations. */
1251 abort ();
1252#endif
1253 *loc = location;
1254 }
1255}
1256
1257/* Get the pointer to the location of the argument token of the
1258 function-like macro argument ARG. This function must be called
1259 only when we -ftrack-macro-expansion is on. */
1260static const source_location *
1261get_arg_token_location (const macro_arg *arg,
1262 enum macro_arg_token_kind kind)
1263{
1264 const source_location *loc = NULL;
1265 const cpp_token **token_ptr =
1266 arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1267
1268 if (token_ptr == NULL)
1269 return NULL;
1270
1271 return loc;
1272}
1273
1274/* Return the pointer to the INDEXth token of the macro argument ARG.
1275 KIND specifies the kind of token the macro argument ARG contains.
1276 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1277 of the virtual location of the returned token if the
1278 -ftrack-macro-expansion flag is on; otherwise, it's set to the
1279 spelling location of the returned token. */
1280static const cpp_token **
1281arg_token_ptr_at (const macro_arg *arg, size_t index,
1282 enum macro_arg_token_kind kind,
1283 source_location **virt_location)
1284{
1285 const cpp_token **tokens_ptr = NULL;
1286
1287 switch (kind)
1288 {
1289 case MACRO_ARG_TOKEN_NORMAL:
1290 tokens_ptr = arg->first;
1291 break;
1292 case MACRO_ARG_TOKEN_STRINGIFIED:
1293 tokens_ptr = (const cpp_token **) &arg->stringified;
1294 break;
1295 case MACRO_ARG_TOKEN_EXPANDED:
1296 tokens_ptr = arg->expanded;
1297 break;
1298 }
1299
1300 if (tokens_ptr == NULL)
1301 /* This can happen for e.g, an empty token argument to a
1302 funtion-like macro. */
1303 return tokens_ptr;
1304
1305 if (virt_location)
1306 {
1307 if (kind == MACRO_ARG_TOKEN_NORMAL)
1308 *virt_location = &arg->virt_locs[index];
1309 else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1310 *virt_location = &arg->expanded_virt_locs[index];
1311 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1312 *virt_location =
1313 (source_location *) &tokens_ptr[index]->src_loc;
1314 }
1315 return &tokens_ptr[index];
1316}
1317
1318/* Initialize an iterator so that it iterates over the tokens of a
1319 function-like macro argument. KIND is the kind of tokens we want
1320 ITER to iterate over. TOKEN_PTR points the first token ITER will
1321 iterate over. */
1322static void
1323macro_arg_token_iter_init (macro_arg_token_iter *iter,
1324 bool track_macro_exp_p,
1325 enum macro_arg_token_kind kind,
1326 const macro_arg *arg,
1327 const cpp_token **token_ptr)
1328{
1329 iter->track_macro_exp_p = track_macro_exp_p;
1330 iter->kind = kind;
1331 iter->token_ptr = token_ptr;
Dodji Seketelid17687f2011-10-18 08:44:49 +00001332 /* Unconditionally initialize this so that the compiler doesn't warn
1333 about iter->location_ptr being possibly uninitialized later after
1334 this code has been inlined somewhere. */
1335 iter->location_ptr = NULL;
Tom Tromey92582b72011-10-17 09:59:12 +00001336 if (track_macro_exp_p)
1337 iter->location_ptr = get_arg_token_location (arg, kind);
1338#ifdef ENABLE_CHECKING
1339 iter->num_forwards = 0;
1340 if (track_macro_exp_p
1341 && token_ptr != NULL
1342 && iter->location_ptr == NULL)
1343 abort ();
1344#endif
1345}
1346
1347/* Move the iterator one token forward. Note that if IT was
1348 initialized on an argument that has a stringified token, moving it
1349 foward doesn't make sense as a stringified token is essentially one
1350 string. */
1351static void
1352macro_arg_token_iter_forward (macro_arg_token_iter *it)
1353{
1354 switch (it->kind)
1355 {
1356 case MACRO_ARG_TOKEN_NORMAL:
1357 case MACRO_ARG_TOKEN_EXPANDED:
1358 it->token_ptr++;
1359 if (it->track_macro_exp_p)
1360 it->location_ptr++;
1361 break;
1362 case MACRO_ARG_TOKEN_STRINGIFIED:
1363#ifdef ENABLE_CHECKING
1364 if (it->num_forwards > 0)
1365 abort ();
1366#endif
1367 break;
1368 }
1369
1370#ifdef ENABLE_CHECKING
1371 it->num_forwards++;
1372#endif
1373}
1374
1375/* Return the token pointed to by the iterator. */
1376static const cpp_token *
1377macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1378{
1379#ifdef ENABLE_CHECKING
1380 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1381 && it->num_forwards > 0)
1382 abort ();
1383#endif
1384 if (it->token_ptr == NULL)
1385 return NULL;
1386 return *it->token_ptr;
1387}
1388
1389/* Return the location of the token pointed to by the iterator.*/
1390static source_location
1391macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1392{
1393#ifdef ENABLE_CHECKING
1394 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1395 && it->num_forwards > 0)
1396 abort ();
1397#endif
1398 if (it->track_macro_exp_p)
1399 return *it->location_ptr;
1400 else
1401 return (*it->token_ptr)->src_loc;
1402}
1403
1404/* Return the index of a token [resulting from macro expansion] inside
1405 the total list of tokens resulting from a given macro
1406 expansion. The index can be different depending on whether if we
1407 want each tokens resulting from function-like macro arguments
1408 expansion to have a different location or not.
1409
1410 E.g, consider this function-like macro:
1411
1412 #define M(x) x - 3
1413
1414 Then consider us "calling" it (and thus expanding it) like:
1415
1416 M(1+4)
1417
1418 It will be expanded into:
1419
1420 1+4-3
1421
1422 Let's consider the case of the token '4'.
1423
1424 Its index can be 2 (it's the third token of the set of tokens
1425 resulting from the expansion) or it can be 0 if we consider that
1426 all tokens resulting from the expansion of the argument "1+2" have
1427 the same index, which is 0. In this later case, the index of token
1428 '-' would then be 1 and the index of token '3' would be 2.
1429
1430 The later case is useful to use less memory e.g, for the case of
1431 the user using the option -ftrack-macro-expansion=1.
1432
1433 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1434 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
1435 parameter (inside the macro replacement list) that corresponds to
1436 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1437 of.
1438
1439 If we refer to the example above, for the '4' argument token,
1440 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1441 would be set to the token 'x', in the replacement list "x - 3" of
1442 macro M.
1443
1444 This is a subroutine of replace_args. */
1445inline static unsigned
1446expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1447 const cpp_token *cur_replacement_token,
1448 unsigned absolute_token_index)
1449{
1450 if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1451 return absolute_token_index;
1452 return cur_replacement_token - macro->exp.tokens;
1453}
1454
Neil Boothd15a58c2002-01-03 18:32:55 +00001455/* Replace the parameters in a function-like macro of NODE with the
1456 actual ARGS, and place the result in a newly pushed token context.
1457 Expand each argument before replacing, unless it is operated upon
Tom Tromey92582b72011-10-17 09:59:12 +00001458 by the # or ## operators. EXPANSION_POINT_LOC is the location of
1459 the expansion point of the macro. E.g, the location of the
1460 function-like macro invocation. */
Neil Booth93c803682000-10-28 17:59:06 +00001461static void
Tom Tromey92582b72011-10-17 09:59:12 +00001462replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1463 macro_arg *args, source_location expansion_point_loc)
Neil Booth93c803682000-10-28 17:59:06 +00001464{
1465 unsigned int i, total;
1466 const cpp_token *src, *limit;
Tom Tromey92582b72011-10-17 09:59:12 +00001467 const cpp_token **first = NULL;
Neil Booth93c803682000-10-28 17:59:06 +00001468 macro_arg *arg;
Tom Tromey92582b72011-10-17 09:59:12 +00001469 _cpp_buff *buff = NULL;
1470 source_location *virt_locs = NULL;
1471 unsigned int exp_count;
1472 const struct line_map *map = NULL;
1473 int track_macro_exp;
Neil Booth93c803682000-10-28 17:59:06 +00001474
Neil Booth93c803682000-10-28 17:59:06 +00001475 /* First, fully macro-expand arguments, calculating the number of
Neil Booth1e013d22001-09-26 21:44:35 +00001476 tokens in the final expansion as we go. The ordering of the if
1477 statements below is subtle; we must handle stringification before
1478 pasting. */
Tom Tromey92582b72011-10-17 09:59:12 +00001479
1480 /* EXP_COUNT is the number of tokens in the macro replacement
1481 list. TOTAL is the number of tokens /after/ macro parameters
1482 have been replaced by their arguments. */
1483 exp_count = macro_real_token_count (macro);
1484 total = exp_count;
1485 limit = macro->exp.tokens + exp_count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001486
Neil Booth601328b2002-05-16 05:53:24 +00001487 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth93c803682000-10-28 17:59:06 +00001488 if (src->type == CPP_MACRO_ARG)
1489 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001490 /* Leading and trailing padding tokens. */
1491 total += 2;
Tom Tromey92582b72011-10-17 09:59:12 +00001492 /* Account for leading and padding tokens in exp_count too.
1493 This is going to be important later down this function,
1494 when we want to handle the case of (track_macro_exp <
1495 2). */
1496 exp_count += 2;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001497
Neil Booth93c803682000-10-28 17:59:06 +00001498 /* We have an argument. If it is not being stringified or
1499 pasted it is macro-replaced before insertion. */
Joseph Myers9a0c6182009-05-10 15:27:32 +01001500 arg = &args[src->val.macro_arg.arg_no - 1];
Neil Booth4c2b6472000-11-11 13:19:01 +00001501
Neil Booth93c803682000-10-28 17:59:06 +00001502 if (src->flags & STRINGIFY_ARG)
1503 {
1504 if (!arg->stringified)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001505 arg->stringified = stringify_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +00001506 }
1507 else if ((src->flags & PASTE_LEFT)
Neil Booth601328b2002-05-16 05:53:24 +00001508 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
Neil Booth93c803682000-10-28 17:59:06 +00001509 total += arg->count - 1;
1510 else
1511 {
1512 if (!arg->expanded)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001513 expand_arg (pfile, arg);
Neil Booth93c803682000-10-28 17:59:06 +00001514 total += arg->expanded_count - 1;
1515 }
1516 }
1517
Tom Tromey92582b72011-10-17 09:59:12 +00001518 /* When the compiler is called with the -ftrack-macro-expansion
1519 flag, we need to keep track of the location of each token that
1520 results from macro expansion.
Neil Booth93c803682000-10-28 17:59:06 +00001521
Tom Tromey92582b72011-10-17 09:59:12 +00001522 A token resulting from macro expansion is not a new token. It is
1523 simply the same token as the token coming from the macro
1524 definition. The new things that are allocated are the buffer
1525 that holds the tokens resulting from macro expansion and a new
1526 location that records many things like the locus of the expansion
1527 point as well as the original locus inside the definition of the
1528 macro. This location is called a virtual location.
1529
1530 So the buffer BUFF holds a set of cpp_token*, and the buffer
1531 VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1532
1533 Both of these two buffers are going to be hung off of the macro
1534 context, when the latter is pushed. The memory allocated to
1535 store the tokens and their locations is going to be freed once
1536 the context of macro expansion is popped.
1537
1538 As far as tokens are concerned, the memory overhead of
1539 -ftrack-macro-expansion is proportional to the number of
1540 macros that get expanded multiplied by sizeof (source_location).
1541 The good news is that extra memory gets freed when the macro
1542 context is freed, i.e shortly after the macro got expanded. */
1543
1544 /* Is the -ftrack-macro-expansion flag in effect? */
1545 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1546
1547 /* Now allocate memory space for tokens and locations resulting from
1548 the macro expansion, copy the tokens and replace the arguments.
1549 This memory must be freed when the context of the macro MACRO is
1550 popped. */
1551 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1552
1553 first = (const cpp_token **) buff->base;
1554
1555 /* Create a macro map to record the locations of the tokens that are
1556 involved in the expansion. Note that the expansion point is set
1557 to the location of the closing parenthesis. Otherwise, the
1558 subsequent map created for the first token that comes after the
1559 macro map might have a wrong line number. That would lead to
1560 tokens with wrong line numbers after the macro expansion. This
1561 adds up to the memory overhead of the -ftrack-macro-expansion
1562 flag; for every macro that is expanded, a "macro map" is
1563 created. */
1564 if (track_macro_exp)
1565 {
1566 int num_macro_tokens = total;
1567 if (track_macro_exp < 2)
1568 /* Then the number of macro tokens won't take in account the
1569 fact that function-like macro arguments can expand to
1570 multiple tokens. This is to save memory at the expense of
1571 accuracy.
1572
1573 Suppose we have #define SQARE(A) A * A
1574
1575 And then we do SQARE(2+3)
1576
1577 Then the tokens 2, +, 3, will have the same location,
1578 saying they come from the expansion of the argument A. */
1579 num_macro_tokens = exp_count;
1580 map = linemap_enter_macro (pfile->line_table, node,
1581 expansion_point_loc,
1582 num_macro_tokens);
1583 }
1584 i = 0;
Neil Booth601328b2002-05-16 05:53:24 +00001585 for (src = macro->exp.tokens; src < limit; src++)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001586 {
Tom Tromey92582b72011-10-17 09:59:12 +00001587 unsigned int arg_tokens_count;
1588 macro_arg_token_iter from;
1589 const cpp_token **paste_flag = NULL;
1590 const cpp_token **tmp_token_ptr;
Neil Booth93c803682000-10-28 17:59:06 +00001591
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001592 if (src->type != CPP_MACRO_ARG)
1593 {
Tom Tromey92582b72011-10-17 09:59:12 +00001594 /* Allocate a virtual location for token SRC, and add that
1595 token and its virtual location into the buffers BUFF and
1596 VIRT_LOCS. */
1597 unsigned index = expanded_token_index (pfile, macro, src, i);
1598 tokens_buff_add_token (buff, virt_locs, src,
1599 src->src_loc, src->src_loc,
1600 map, index);
1601 i += 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001602 continue;
1603 }
1604
1605 paste_flag = 0;
Joseph Myers9a0c6182009-05-10 15:27:32 +01001606 arg = &args[src->val.macro_arg.arg_no - 1];
Tom Tromey92582b72011-10-17 09:59:12 +00001607 /* SRC is a macro parameter that we need to replace with its
1608 corresponding argument. So at some point we'll need to
1609 iterate over the tokens of the macro argument and copy them
1610 into the "place" now holding the correspondig macro
1611 parameter. We are going to use the iterator type
1612 macro_argo_token_iter to handle that iterating. The 'if'
1613 below is to initialize the iterator depending on the type of
1614 tokens the macro argument has. It also does some adjustment
1615 related to padding tokens and some pasting corner cases. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001616 if (src->flags & STRINGIFY_ARG)
Tom Tromey92582b72011-10-17 09:59:12 +00001617 {
1618 arg_tokens_count = 1;
1619 macro_arg_token_iter_init (&from,
1620 CPP_OPTION (pfile,
1621 track_macro_expansion),
1622 MACRO_ARG_TOKEN_STRINGIFIED,
1623 arg, &arg->stringified);
1624 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001625 else if (src->flags & PASTE_LEFT)
Tom Tromey92582b72011-10-17 09:59:12 +00001626 {
1627 arg_tokens_count = arg->count;
1628 macro_arg_token_iter_init (&from,
1629 CPP_OPTION (pfile,
1630 track_macro_expansion),
1631 MACRO_ARG_TOKEN_NORMAL,
1632 arg, arg->first);
1633 }
Neil Booth601328b2002-05-16 05:53:24 +00001634 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001635 {
Tom Tromey92582b72011-10-17 09:59:12 +00001636 int num_toks;
1637 arg_tokens_count = arg->count;
1638 macro_arg_token_iter_init (&from,
1639 CPP_OPTION (pfile,
1640 track_macro_expansion),
1641 MACRO_ARG_TOKEN_NORMAL,
1642 arg, arg->first);
1643
1644 num_toks = tokens_buff_count (buff);
1645
1646 if (num_toks != 0)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001647 {
Tom Tromey92582b72011-10-17 09:59:12 +00001648 /* So the current parameter token is pasted to the previous
1649 token in the replacement list. Let's look at what
1650 we have as previous and current arguments. */
1651
1652 /* This is the previous argument's token ... */
1653 tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1654
1655 if ((*tmp_token_ptr)->type == CPP_COMMA
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001656 && macro->variadic
Joseph Myers9a0c6182009-05-10 15:27:32 +01001657 && src->val.macro_arg.arg_no == macro->paramc)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001658 {
Tom Tromey92582b72011-10-17 09:59:12 +00001659 /* ... which is a comma; and the current parameter
1660 is the last parameter of a variadic function-like
1661 macro. If the argument to the current last
1662 parameter is NULL, then swallow the comma,
1663 otherwise drop the paste flag. */
1664 if (macro_arg_token_iter_get_token (&from) == NULL)
1665 tokens_buff_remove_last_token (buff);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001666 else
Tom Tromey92582b72011-10-17 09:59:12 +00001667 paste_flag = tmp_token_ptr;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001668 }
1669 /* Remove the paste flag if the RHS is a placemarker. */
Tom Tromey92582b72011-10-17 09:59:12 +00001670 else if (arg_tokens_count == 0)
1671 paste_flag = tmp_token_ptr;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001672 }
1673 }
1674 else
Tom Tromey92582b72011-10-17 09:59:12 +00001675 {
1676 arg_tokens_count = arg->expanded_count;
1677 macro_arg_token_iter_init (&from,
1678 CPP_OPTION (pfile,
1679 track_macro_expansion),
1680 MACRO_ARG_TOKEN_EXPANDED,
1681 arg, arg->expanded);
1682 }
Neil Booth93c803682000-10-28 17:59:06 +00001683
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001684 /* Padding on the left of an argument (unless RHS of ##). */
Zack Weinberga8d0dda2003-02-21 18:06:30 +00001685 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
Neil Booth601328b2002-05-16 05:53:24 +00001686 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001687 {
Tom Tromey92582b72011-10-17 09:59:12 +00001688 const cpp_token *t = padding_token (pfile, src);
1689 unsigned index = expanded_token_index (pfile, macro, src, i);
1690 /* Allocate a virtual location for the padding token and
1691 append the token and its location to BUFF and
1692 VIRT_LOCS. */
1693 tokens_buff_add_token (buff, virt_locs, t,
1694 t->src_loc, t->src_loc,
1695 map, index);
1696 }
1697
1698 if (arg_tokens_count)
1699 {
1700 /* So now we've got the number of tokens that make up the
1701 argument that is going to replace the current parameter
1702 in the macro's replacement list. */
1703 unsigned int j;
1704 for (j = 0; j < arg_tokens_count; ++j)
1705 {
1706 /* So if track_macro_exp is < 2, the user wants to
1707 save extra memory while tracking macro expansion
1708 locations. So in that case here is what we do:
1709
1710 Suppose we have #define SQARE(A) A * A
1711
1712 And then we do SQARE(2+3)
1713
1714 Then the tokens 2, +, 3, will have the same location,
1715 saying they come from the expansion of the argument
1716 A.
1717
1718 So that means we are going to ignore the COUNT tokens
1719 resulting from the expansion of the current macro
1720 arugment. In other words all the ARG_TOKENS_COUNT tokens
1721 resulting from the expansion of the macro argument will
1722 have the index I. Normally, each of those token should
1723 have index I+J. */
1724 unsigned token_index = i;
1725 unsigned index;
1726 if (track_macro_exp > 1)
1727 token_index += j;
1728
1729 index = expanded_token_index (pfile, macro, src, token_index);
1730 tokens_buff_add_token (buff, virt_locs,
1731 macro_arg_token_iter_get_token (&from),
1732 macro_arg_token_iter_get_location (&from),
1733 src->src_loc, map, index);
1734 macro_arg_token_iter_forward (&from);
1735 }
Neil Booth93c803682000-10-28 17:59:06 +00001736
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001737 /* With a non-empty argument on the LHS of ##, the last
1738 token should be flagged PASTE_LEFT. */
1739 if (src->flags & PASTE_LEFT)
Tom Tromey92582b72011-10-17 09:59:12 +00001740 paste_flag =
1741 (const cpp_token **) tokens_buff_last_token_ptr (buff);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001742 }
Andrew Haleye85edc92008-07-03 10:31:50 +00001743 else if (CPP_PEDANTIC (pfile) && ! macro->syshdr
1744 && ! CPP_OPTION (pfile, c99)
1745 && ! cpp_in_system_header (pfile))
1746 {
1747 cpp_error (pfile, CPP_DL_PEDWARN,
1748 "invoking macro %s argument %d: "
1749 "empty macro arguments are undefined"
1750 " in ISO C90 and ISO C++98",
1751 NODE_NAME (node),
Joseph Myers9a0c6182009-05-10 15:27:32 +01001752 src->val.macro_arg.arg_no);
Andrew Haleye85edc92008-07-03 10:31:50 +00001753 }
Neil Booth4c2b6472000-11-11 13:19:01 +00001754
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001755 /* Avoid paste on RHS (even case count == 0). */
1756 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
Tom Tromey92582b72011-10-17 09:59:12 +00001757 {
1758 const cpp_token *t = &pfile->avoid_paste;
1759 tokens_buff_add_token (buff, virt_locs,
1760 t, t->src_loc, t->src_loc,
1761 NULL, 0);
1762 }
Neil Booth26ec42e2001-01-28 11:22:23 +00001763
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001764 /* Add a new paste flag, or remove an unwanted one. */
1765 if (paste_flag)
1766 {
1767 cpp_token *token = _cpp_temp_token (pfile);
1768 token->type = (*paste_flag)->type;
Jakub Jelinek73096712005-03-04 16:33:23 +01001769 token->val = (*paste_flag)->val;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001770 if (src->flags & PASTE_LEFT)
1771 token->flags = (*paste_flag)->flags | PASTE_LEFT;
1772 else
1773 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1774 *paste_flag = token;
1775 }
Tom Tromey92582b72011-10-17 09:59:12 +00001776
1777 i += arg_tokens_count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001778 }
Neil Booth4c2b6472000-11-11 13:19:01 +00001779
Tom Tromey92582b72011-10-17 09:59:12 +00001780 if (track_macro_exp)
1781 push_extended_tokens_context (pfile, node, buff, virt_locs, first,
1782 tokens_buff_count (buff));
1783 else
1784 push_ptoken_context (pfile, node, buff, first,
1785 tokens_buff_count (buff));
Tom Tromey64a1a422011-10-17 09:59:52 +00001786
1787 num_macro_tokens_counter += tokens_buff_count (buff);
Neil Booth93c803682000-10-28 17:59:06 +00001788}
1789
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001790/* Return a special padding token, with padding inherited from SOURCE. */
1791static const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001792padding_token (cpp_reader *pfile, const cpp_token *source)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001793{
1794 cpp_token *result = _cpp_temp_token (pfile);
1795
1796 result->type = CPP_PADDING;
Paolo Bonzinibe0f1e52005-02-14 08:52:24 +00001797
1798 /* Data in GCed data structures cannot be made const so far, so we
1799 need a cast here. */
1800 result->val.source = (cpp_token *) source;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001801 result->flags = 0;
1802 return result;
1803}
1804
Neil Boothd15a58c2002-01-03 18:32:55 +00001805/* Get a new uninitialized context. Create a new one if we cannot
1806 re-use an old one. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001807static cpp_context *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001808next_context (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001809{
1810 cpp_context *result = pfile->context->next;
1811
1812 if (result == 0)
1813 {
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001814 result = XNEW (cpp_context);
Tom Tromey92582b72011-10-17 09:59:12 +00001815 memset (result, 0, sizeof (cpp_context));
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001816 result->prev = pfile->context;
1817 result->next = 0;
1818 pfile->context->next = result;
1819 }
1820
1821 pfile->context = result;
1822 return result;
1823}
1824
1825/* Push a list of pointers to tokens. */
1826static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001827push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1828 const cpp_token **first, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +00001829{
1830 cpp_context *context = next_context (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00001831
Tom Tromey92582b72011-10-17 09:59:12 +00001832 context->tokens_kind = TOKENS_KIND_INDIRECT;
1833 context->c.macro = macro;
Neil Booth1e013d22001-09-26 21:44:35 +00001834 context->buff = buff;
Neil Booth82eda772002-06-04 13:07:06 +00001835 FIRST (context).ptoken = first;
1836 LAST (context).ptoken = first + count;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001837}
1838
Dodji Seketeli36002182012-04-30 11:41:46 +00001839/* Push a list of tokens.
1840
1841 A NULL macro means that we should continue the current macro
1842 expansion, in essence. That means that if we are currently in a
1843 macro expansion context, we'll make the new pfile->context refer to
1844 the current macro. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001845void
1846_cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1847 const cpp_token *first, unsigned int count)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001848{
Dodji Seketeli36002182012-04-30 11:41:46 +00001849 cpp_context *context;
1850
1851 if (macro == NULL)
1852 macro = macro_of_context (pfile->context);
1853
1854 context = next_context (pfile);
Tom Tromey92582b72011-10-17 09:59:12 +00001855 context->tokens_kind = TOKENS_KIND_DIRECT;
1856 context->c.macro = macro;
1857 context->buff = NULL;
Dodji Seketeli36002182012-04-30 11:41:46 +00001858 FIRST (context).token = first;
1859 LAST (context).token = first + count;
Neil Booth93c803682000-10-28 17:59:06 +00001860}
1861
Tom Tromey92582b72011-10-17 09:59:12 +00001862/* Build a context containing a list of tokens as well as their
1863 virtual locations and push it. TOKENS_BUFF is the buffer that
1864 contains the tokens pointed to by FIRST. If TOKENS_BUFF is
1865 non-NULL, it means that the context owns it, meaning that
1866 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
Dodji Seketeli36002182012-04-30 11:41:46 +00001867 contains the virtual locations.
1868
1869 A NULL macro means that we should continue the current macro
1870 expansion, in essence. That means that if we are currently in a
1871 macro expansion context, we'll make the new pfile->context refer to
1872 the current macro. */
Tom Tromey92582b72011-10-17 09:59:12 +00001873static void
1874push_extended_tokens_context (cpp_reader *pfile,
1875 cpp_hashnode *macro,
1876 _cpp_buff *token_buff,
1877 source_location *virt_locs,
1878 const cpp_token **first,
1879 unsigned int count)
1880{
Dodji Seketeli36002182012-04-30 11:41:46 +00001881 cpp_context *context;
Tom Tromey92582b72011-10-17 09:59:12 +00001882 macro_context *m;
1883
Dodji Seketeli36002182012-04-30 11:41:46 +00001884 if (macro == NULL)
1885 macro = macro_of_context (pfile->context);
1886
1887 context = next_context (pfile);
Tom Tromey92582b72011-10-17 09:59:12 +00001888 context->tokens_kind = TOKENS_KIND_EXTENDED;
1889 context->buff = token_buff;
1890
1891 m = XNEW (macro_context);
1892 m->macro_node = macro;
1893 m->virt_locs = virt_locs;
1894 m->cur_virt_loc = virt_locs;
1895 context->c.mc = m;
1896 FIRST (context).ptoken = first;
1897 LAST (context).ptoken = first + count;
1898}
1899
Neil Boothcbc69f82002-06-05 20:27:12 +00001900/* Push a traditional macro's replacement text. */
1901void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001902_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
1903 const uchar *start, size_t len)
Neil Boothcbc69f82002-06-05 20:27:12 +00001904{
1905 cpp_context *context = next_context (pfile);
1906
Tom Tromey92582b72011-10-17 09:59:12 +00001907 context->tokens_kind = TOKENS_KIND_DIRECT;
1908 context->c.macro = macro;
Neil Boothcbc69f82002-06-05 20:27:12 +00001909 context->buff = NULL;
1910 CUR (context) = start;
Neil Booth1ce676a2002-06-09 20:04:17 +00001911 RLIMIT (context) = start + len;
Neil Booth974c43f2002-06-13 06:25:28 +00001912 macro->flags |= NODE_DISABLED;
Neil Boothcbc69f82002-06-05 20:27:12 +00001913}
1914
Tom Tromey92582b72011-10-17 09:59:12 +00001915/* Creates a buffer that holds tokens a.k.a "token buffer", usually
1916 for the purpose of storing them on a cpp_context. If VIRT_LOCS is
1917 non-null (which means that -ftrack-macro-expansion is on),
1918 *VIRT_LOCS is set to a newly allocated buffer that is supposed to
1919 hold the virtual locations of the tokens resulting from macro
1920 expansion. */
1921static _cpp_buff*
1922tokens_buff_new (cpp_reader *pfile, size_t len,
1923 source_location **virt_locs)
1924{
1925 size_t tokens_size = len * sizeof (cpp_token *);
1926 size_t locs_size = len * sizeof (source_location);
1927
1928 if (virt_locs != NULL)
1929 *virt_locs = XNEWVEC (source_location, locs_size);
1930 return _cpp_get_buff (pfile, tokens_size);
1931}
1932
1933/* Returns the number of tokens contained in a token buffer. The
1934 buffer holds a set of cpp_token*. */
1935static size_t
1936tokens_buff_count (_cpp_buff *buff)
1937{
1938 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
1939}
1940
1941/* Return a pointer to the last token contained in the token buffer
1942 BUFF. */
1943static const cpp_token **
1944tokens_buff_last_token_ptr (_cpp_buff *buff)
1945{
1946 return &((const cpp_token **) BUFF_FRONT (buff))[-1];
1947}
1948
1949/* Remove the last token contained in the token buffer TOKENS_BUFF.
1950 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
1951 containing the virtual locations of the tokens in TOKENS_BUFF; in
1952 which case the function updates that buffer as well. */
1953static inline void
1954tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
1955
1956{
1957 if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
1958 BUFF_FRONT (tokens_buff) =
1959 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
1960}
1961
1962/* Insert a token into the token buffer at the position pointed to by
1963 DEST. Note that the buffer is not enlarged so the previous token
1964 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
1965 means -ftrack-macro-expansion is effect; it then points to where to
1966 insert the virtual location of TOKEN. TOKEN is the token to
1967 insert. VIRT_LOC is the virtual location of the token, i.e, the
1968 location possibly encoding its locus accross macro expansion. If
1969 TOKEN is an argument of a function-like macro (inside a macro
1970 replacement list), PARM_DEF_LOC is the spelling location of the
1971 macro parameter that TOKEN is replacing, in the replacement list of
1972 the macro. If TOKEN is not an argument of a function-like macro or
1973 if it doesn't come from a macro expansion, then VIRT_LOC can just
1974 be set to the same value as PARM_DEF_LOC. If MAP is non null, it
1975 means TOKEN comes from a macro expansion and MAP is the macro map
1976 associated to the macro. MACRO_TOKEN_INDEX points to the index of
1977 the token in the macro map; it is not considered if MAP is NULL.
1978
1979 Upon successful completion this function returns the a pointer to
1980 the position of the token coming right after the insertion
1981 point. */
1982static inline const cpp_token **
1983tokens_buff_put_token_to (const cpp_token **dest,
1984 source_location *virt_loc_dest,
1985 const cpp_token *token,
1986 source_location virt_loc,
1987 source_location parm_def_loc,
1988 const struct line_map *map,
1989 unsigned int macro_token_index)
1990{
1991 source_location macro_loc = virt_loc;
1992 const cpp_token **result;
1993
1994 if (virt_loc_dest)
1995 {
1996 /* -ftrack-macro-expansion is on. */
1997 if (map)
1998 macro_loc = linemap_add_macro_token (map, macro_token_index,
1999 virt_loc, parm_def_loc);
2000 *virt_loc_dest = macro_loc;
2001 }
2002 *dest = token;
2003 result = &dest[1];
2004
2005 return result;
2006}
2007
2008/* Adds a token at the end of the tokens contained in BUFFER. Note
2009 that this function doesn't enlarge BUFFER when the number of tokens
2010 reaches BUFFER's size; it aborts in that situation.
2011
2012 TOKEN is the token to append. VIRT_LOC is the virtual location of
2013 the token, i.e, the location possibly encoding its locus accross
2014 macro expansion. If TOKEN is an argument of a function-like macro
2015 (inside a macro replacement list), PARM_DEF_LOC is the location of
2016 the macro parameter that TOKEN is replacing. If TOKEN doesn't come
2017 from a macro expansion, then VIRT_LOC can just be set to the same
2018 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
2019 from a macro expansion and MAP is the macro map associated to the
2020 macro. MACRO_TOKEN_INDEX points to the index of the token in the
2021 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
2022 non-null, it means -ftrack-macro-expansion is on; in which case
2023 this function adds the virtual location DEF_LOC to the VIRT_LOCS
2024 array, at the same index as the one of TOKEN in BUFFER. Upon
2025 successful completion this function returns the a pointer to the
2026 position of the token coming right after the insertion point. */
2027static const cpp_token **
2028tokens_buff_add_token (_cpp_buff *buffer,
2029 source_location *virt_locs,
2030 const cpp_token *token,
2031 source_location virt_loc,
2032 source_location parm_def_loc,
2033 const struct line_map *map,
2034 unsigned int macro_token_index)
2035{
2036 const cpp_token **result;
2037 source_location *virt_loc_dest = NULL;
2038 unsigned token_index =
2039 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2040
2041 /* Abort if we pass the end the buffer. */
2042 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2043 abort ();
2044
2045 if (virt_locs != NULL)
2046 virt_loc_dest = &virt_locs[token_index];
2047
2048 result =
2049 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2050 virt_loc_dest, token, virt_loc, parm_def_loc,
2051 map, macro_token_index);
2052
2053 BUFF_FRONT (buffer) = (unsigned char *) result;
2054 return result;
2055}
2056
2057/* Allocate space for the function-like macro argument ARG to store
2058 the tokens resulting from the macro-expansion of the tokens that
2059 make up ARG itself. That space is allocated in ARG->expanded and
2060 needs to be freed using free. */
2061static void
2062alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2063{
2064#ifdef ENABLE_CHECKING
2065 if (arg->expanded != NULL
2066 || arg->expanded_virt_locs != NULL)
2067 abort ();
2068#endif
2069 arg->expanded = XNEWVEC (const cpp_token *, capacity);
2070 if (CPP_OPTION (pfile, track_macro_expansion))
2071 arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2072
2073}
2074
2075/* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2076 tokens. */
2077static void
2078ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2079 size_t size, size_t *expanded_capacity)
2080{
2081 if (size <= *expanded_capacity)
2082 return;
2083
2084 size *= 2;
2085
2086 arg->expanded =
2087 XRESIZEVEC (const cpp_token *, arg->expanded, size);
2088 *expanded_capacity = size;
2089
2090 if (CPP_OPTION (pfile, track_macro_expansion))
2091 {
2092 if (arg->expanded_virt_locs == NULL)
2093 arg->expanded_virt_locs = XNEWVEC (source_location, size);
2094 else
2095 arg->expanded_virt_locs = XRESIZEVEC (source_location,
2096 arg->expanded_virt_locs,
2097 size);
2098 }
2099}
2100
Neil Boothd15a58c2002-01-03 18:32:55 +00002101/* Expand an argument ARG before replacing parameters in a
2102 function-like macro. This works by pushing a context with the
2103 argument's tokens, and then expanding that into a temporary buffer
2104 as if it were a normal part of the token stream. collect_args()
2105 has terminated the argument's tokens with a CPP_EOF so that we know
2106 when we have fully expanded the argument. */
Neil Booth93c803682000-10-28 17:59:06 +00002107static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002108expand_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth93c803682000-10-28 17:59:06 +00002109{
Tom Tromey92582b72011-10-17 09:59:12 +00002110 size_t capacity;
Neil Booth56941bf2002-09-20 19:44:09 +00002111 bool saved_warn_trad;
Tom Tromey92582b72011-10-17 09:59:12 +00002112 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002113
Tom Tromey92582b72011-10-17 09:59:12 +00002114 if (arg->count == 0
2115 || arg->expanded != NULL)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002116 return;
Neil Booth93c803682000-10-28 17:59:06 +00002117
Neil Booth56941bf2002-09-20 19:44:09 +00002118 /* Don't warn about funlike macros when pre-expanding. */
2119 saved_warn_trad = CPP_WTRADITIONAL (pfile);
2120 CPP_WTRADITIONAL (pfile) = 0;
2121
Tom Tromey92582b72011-10-17 09:59:12 +00002122 /* Loop, reading in the tokens of the argument. */
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002123 capacity = 256;
Tom Tromey92582b72011-10-17 09:59:12 +00002124 alloc_expanded_arg_mem (pfile, arg, capacity);
Neil Booth93c803682000-10-28 17:59:06 +00002125
Tom Tromey92582b72011-10-17 09:59:12 +00002126 if (track_macro_exp_p)
2127 push_extended_tokens_context (pfile, NULL, NULL,
2128 arg->virt_locs,
2129 arg->first,
2130 arg->count + 1);
2131 else
2132 push_ptoken_context (pfile, NULL, NULL,
2133 arg->first, arg->count + 1);
2134
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002135 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00002136 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002137 const cpp_token *token;
Tom Tromey92582b72011-10-17 09:59:12 +00002138 source_location location;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002139
Tom Tromey92582b72011-10-17 09:59:12 +00002140 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2141 &capacity);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002142
Tom Tromey92582b72011-10-17 09:59:12 +00002143 token = cpp_get_token_1 (pfile, &location);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002144
2145 if (token->type == CPP_EOF)
2146 break;
2147
Tom Tromey92582b72011-10-17 09:59:12 +00002148 set_arg_token (arg, token, location,
2149 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2150 CPP_OPTION (pfile, track_macro_expansion));
2151 arg->expanded_count++;
Neil Booth93c803682000-10-28 17:59:06 +00002152 }
Neil Booth93c803682000-10-28 17:59:06 +00002153
Neil Booth1e013d22001-09-26 21:44:35 +00002154 _cpp_pop_context (pfile);
Neil Booth56941bf2002-09-20 19:44:09 +00002155
2156 CPP_WTRADITIONAL (pfile) = saved_warn_trad;
Neil Booth93c803682000-10-28 17:59:06 +00002157}
2158
Dodji Seketeli36002182012-04-30 11:41:46 +00002159/* Returns the macro associated to the current context if we are in
2160 the context a macro expansion, NULL otherwise. */
2161static cpp_hashnode*
2162macro_of_context (cpp_context *context)
2163{
2164 if (context == NULL)
2165 return NULL;
2166
2167 return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2168 ? context->c.mc->macro_node
2169 : context->c.macro;
2170}
2171
Dodji Seketeli828a7f72012-05-29 09:36:29 +00002172/* Return TRUE iff we are expanding a macro or are about to start
2173 expanding one. If we are effectively expanding a macro, the
2174 function macro_of_context returns a pointer to the macro being
2175 expanded. */
2176static bool
2177in_macro_expansion_p (cpp_reader *pfile)
2178{
2179 if (pfile == NULL)
2180 return false;
2181
2182 return (pfile->about_to_expand_macro_p
2183 || macro_of_context (pfile->context));
2184}
2185
Neil Boothd15a58c2002-01-03 18:32:55 +00002186/* Pop the current context off the stack, re-enabling the macro if the
Tom Tromey92582b72011-10-17 09:59:12 +00002187 context represented a macro's replacement list. Initially the
2188 context structure was not freed so that we can re-use it later, but
2189 now we do free it to reduce peak memory consumption. */
Neil Booth93c803682000-10-28 17:59:06 +00002190void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002191_cpp_pop_context (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00002192{
Neil Booth1e013d22001-09-26 21:44:35 +00002193 cpp_context *context = pfile->context;
Neil Booth93c803682000-10-28 17:59:06 +00002194
Dodji Seketeli3ad64f52012-05-02 16:55:19 +00002195 /* We should not be popping the base context. */
2196 if (context == &pfile->base_context)
2197 abort ();
2198
Tom Tromey92582b72011-10-17 09:59:12 +00002199 if (context->c.macro)
2200 {
2201 cpp_hashnode *macro;
2202 if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2203 {
2204 macro_context *mc = context->c.mc;
2205 macro = mc->macro_node;
2206 /* If context->buff is set, it means the life time of tokens
2207 is bound to the life time of this context; so we must
2208 free the tokens; that means we must free the virtual
2209 locations of these tokens too. */
2210 if (context->buff && mc->virt_locs)
2211 {
2212 free (mc->virt_locs);
2213 mc->virt_locs = NULL;
2214 }
2215 free (mc);
2216 context->c.mc = NULL;
2217 }
2218 else
2219 macro = context->c.macro;
2220
2221 /* Beware that MACRO can be NULL in cases like when we are
2222 called from expand_arg. In those cases, a dummy context with
2223 tokens is pushed just for the purpose of walking them using
2224 cpp_get_token_1. In that case, no 'macro' field is set into
2225 the dummy context. */
Dodji Seketeli36002182012-04-30 11:41:46 +00002226 if (macro != NULL
2227 /* Several contiguous macro expansion contexts can be
2228 associated to the same macro; that means it's the same
2229 macro expansion that spans accross all these (sub)
2230 contexts. So we should re-enable an expansion-disabled
2231 macro only when we are sure we are really out of that
2232 macro expansion. */
2233 && macro_of_context (context->prev) != macro)
Tom Tromey92582b72011-10-17 09:59:12 +00002234 macro->flags &= ~NODE_DISABLED;
2235 }
Neil Booth1e013d22001-09-26 21:44:35 +00002236
2237 if (context->buff)
Tom Tromey92582b72011-10-17 09:59:12 +00002238 {
2239 /* Decrease memory peak consumption by freeing the memory used
2240 by the context. */
2241 _cpp_free_buff (context->buff);
2242 }
Neil Booth1e013d22001-09-26 21:44:35 +00002243
2244 pfile->context = context->prev;
Tom Tromey92582b72011-10-17 09:59:12 +00002245 /* decrease peak memory consumption by feeing the context. */
2246 pfile->context->next = NULL;
2247 free (context);
Neil Booth93c803682000-10-28 17:59:06 +00002248}
2249
Tom Tromey92582b72011-10-17 09:59:12 +00002250/* Return TRUE if we reached the end of the set of tokens stored in
2251 CONTEXT, FALSE otherwise. */
2252static inline bool
2253reached_end_of_context (cpp_context *context)
2254{
2255 if (context->tokens_kind == TOKENS_KIND_DIRECT)
2256 return FIRST (context).token == LAST (context).token;
2257 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2258 || context->tokens_kind == TOKENS_KIND_EXTENDED)
2259 return FIRST (context).ptoken == LAST (context).ptoken;
2260 else
2261 abort ();
2262}
2263
2264/* Consume the next token contained in the current context of PFILE,
2265 and return it in *TOKEN. It's "full location" is returned in
2266 *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2267 means the location encoding the locus of the token accross macro
2268 expansion; otherwise it's just is the "normal" location of the
2269 token which (*TOKEN)->src_loc. */
2270static inline void
2271consume_next_token_from_context (cpp_reader *pfile,
2272 const cpp_token ** token,
2273 source_location *location)
2274{
2275 cpp_context *c = pfile->context;
2276
2277 if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2278 {
2279 *token = FIRST (c).token;
2280 *location = (*token)->src_loc;
2281 FIRST (c).token++;
2282 }
2283 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2284 {
2285 *token = *FIRST (c).ptoken;
2286 *location = (*token)->src_loc;
2287 FIRST (c).ptoken++;
2288 }
2289 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2290 {
2291 macro_context *m = c->c.mc;
2292 *token = *FIRST (c).ptoken;
2293 if (m->virt_locs)
2294 {
2295 *location = *m->cur_virt_loc;
2296 m->cur_virt_loc++;
2297 }
2298 else
2299 *location = (*token)->src_loc;
2300 FIRST (c).ptoken++;
2301 }
2302 else
2303 abort ();
2304}
2305
2306/* In the traditional mode of the preprocessor, if we are currently in
2307 a directive, the location of a token must be the location of the
2308 start of the directive line. This function returns the proper
2309 location if we are in the traditional mode, and just returns
2310 LOCATION otherwise. */
2311
2312static inline source_location
2313maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2314{
2315 if (CPP_OPTION (pfile, traditional))
2316 {
2317 if (pfile->state.in_directive)
2318 return pfile->directive_line;
2319 }
2320 return location;
2321}
2322
2323/* Routine to get a token as well as its location.
Neil Booth7f2f1a62000-11-14 18:32:06 +00002324
2325 Macro expansions and directives are transparently handled,
2326 including entering included files. Thus tokens are post-macro
2327 expansion, and after any intervening directives. External callers
2328 see CPP_EOF only at EOF. Internal callers also see it when meeting
2329 a directive inside a macro call, when at the end of a directive and
2330 state.in_directive is still 1, and at the end of argument
Tom Tromey92582b72011-10-17 09:59:12 +00002331 pre-expansion.
2332
2333 LOC is an out parameter; *LOC is set to the location "as expected
2334 by the user". Please read the comment of
2335 cpp_get_token_with_location to learn more about the meaning of this
2336 location. */
2337static const cpp_token*
2338cpp_get_token_1 (cpp_reader *pfile, source_location *location)
Neil Booth93c803682000-10-28 17:59:06 +00002339{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002340 const cpp_token *result;
Tom Tromey92582b72011-10-17 09:59:12 +00002341 /* This token is a virtual token that either encodes a location
2342 related to macro expansion or a spelling location. */
2343 source_location virt_loc = 0;
Dodji Seketeli828a7f72012-05-29 09:36:29 +00002344 /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2345 to functions that push macro contexts. So let's save it so that
2346 we can restore it when we are about to leave this routine. */
2347 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002348
Neil Booth29b10742000-11-13 18:40:37 +00002349 for (;;)
Neil Booth93c803682000-10-28 17:59:06 +00002350 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002351 cpp_hashnode *node;
Neil Booth93c803682000-10-28 17:59:06 +00002352 cpp_context *context = pfile->context;
2353
Neil Booth93c803682000-10-28 17:59:06 +00002354 /* Context->prev == 0 <=> base context. */
Neil Boothbdcbe492001-09-13 20:05:17 +00002355 if (!context->prev)
Neil Booth29b10742000-11-13 18:40:37 +00002356 {
Tom Tromey92582b72011-10-17 09:59:12 +00002357 result = _cpp_lex_token (pfile);
2358 virt_loc = result->src_loc;
2359 }
2360 else if (!reached_end_of_context (context))
2361 {
2362 consume_next_token_from_context (pfile, &result,
2363 &virt_loc);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002364 if (result->flags & PASTE_LEFT)
Neil Boothec1a23e2001-01-31 07:48:54 +00002365 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002366 paste_all_tokens (pfile, result);
2367 if (pfile->state.in_directive)
2368 continue;
Tom Tromey92582b72011-10-17 09:59:12 +00002369 result = padding_token (pfile, result);
2370 goto out;
Neil Boothec1a23e2001-01-31 07:48:54 +00002371 }
Neil Booth29b10742000-11-13 18:40:37 +00002372 }
Neil Booth93c803682000-10-28 17:59:06 +00002373 else
2374 {
Tom Tromey64a1a422011-10-17 09:59:52 +00002375 if (pfile->context->c.macro)
2376 ++num_expanded_macros_counter;
Neil Boothbdcbe492001-09-13 20:05:17 +00002377 _cpp_pop_context (pfile);
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002378 if (pfile->state.in_directive)
2379 continue;
Tom Tromey92582b72011-10-17 09:59:12 +00002380 result = &pfile->avoid_paste;
2381 goto out;
Neil Booth93c803682000-10-28 17:59:06 +00002382 }
Neil Booth93c803682000-10-28 17:59:06 +00002383
Jason Thorpe477cdac2002-04-07 03:12:23 +00002384 if (pfile->state.in_directive && result->type == CPP_COMMENT)
2385 continue;
2386
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002387 if (result->type != CPP_NAME)
Neil Booth93c803682000-10-28 17:59:06 +00002388 break;
2389
Joseph Myers9a0c6182009-05-10 15:27:32 +01002390 node = result->val.node.node;
Neil Booth4c2b6472000-11-11 13:19:01 +00002391
Neil Booth644edda2001-10-02 12:57:24 +00002392 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2393 break;
Kazu Hiratadf383482002-05-22 22:02:16 +00002394
Neil Booth644edda2001-10-02 12:57:24 +00002395 if (!(node->flags & NODE_DISABLED))
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002396 {
Ben Elliston5950c3c2008-07-14 05:09:48 +00002397 int ret = 0;
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002398 /* If not in a macro context, and we're going to start an
2399 expansion, record the location. */
Dodji Seketeli828a7f72012-05-29 09:36:29 +00002400 if (!in_macro_expansion_p (pfile))
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002401 pfile->invocation_location = result->src_loc;
Jakub Jelinek765d6002008-01-25 10:01:27 +01002402 if (pfile->state.prevent_expansion)
2403 break;
Ben Elliston5950c3c2008-07-14 05:09:48 +00002404
2405 /* Conditional macros require that a predicate be evaluated
2406 first. */
Jakub Jelineka37a7b82009-03-30 17:00:52 +02002407 if ((node->flags & NODE_CONDITIONAL) != 0)
2408 {
2409 if (pfile->cb.macro_to_expand)
2410 {
2411 bool whitespace_after;
2412 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2413
2414 whitespace_after = (peek_tok->type == CPP_PADDING
2415 || (peek_tok->flags & PREV_WHITE));
2416 node = pfile->cb.macro_to_expand (pfile, result);
2417 if (node)
Tom Tromey92582b72011-10-17 09:59:12 +00002418 ret = enter_macro_context (pfile, node, result,
2419 virt_loc);
Jakub Jelineka37a7b82009-03-30 17:00:52 +02002420 else if (whitespace_after)
2421 {
2422 /* If macro_to_expand hook returned NULL and it
2423 ate some tokens, see if we don't need to add
2424 a padding token in between this and the
2425 next token. */
2426 peek_tok = cpp_peek_token (pfile, 0);
2427 if (peek_tok->type != CPP_PADDING
2428 && (peek_tok->flags & PREV_WHITE) == 0)
2429 _cpp_push_token_context (pfile, NULL,
2430 padding_token (pfile,
2431 peek_tok), 1);
2432 }
2433 }
2434 }
2435 else
Tom Tromey92582b72011-10-17 09:59:12 +00002436 ret = enter_macro_context (pfile, node, result,
2437 virt_loc);
Jakub Jelineka37a7b82009-03-30 17:00:52 +02002438 if (ret)
Ben Elliston5950c3c2008-07-14 05:09:48 +00002439 {
Jakub Jelinek765d6002008-01-25 10:01:27 +01002440 if (pfile->state.in_directive || ret == 2)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002441 continue;
Tom Tromey92582b72011-10-17 09:59:12 +00002442 result = padding_token (pfile, result);
2443 goto out;
Neil Boothbd969772001-02-01 19:13:53 +00002444 }
Neil Booth93c803682000-10-28 17:59:06 +00002445 }
Neil Booth644edda2001-10-02 12:57:24 +00002446 else
2447 {
Neil Boothd15a58c2002-01-03 18:32:55 +00002448 /* Flag this token as always unexpandable. FIXME: move this
2449 to collect_args()?. */
Neil Booth644edda2001-10-02 12:57:24 +00002450 cpp_token *t = _cpp_temp_token (pfile);
2451 t->type = result->type;
2452 t->flags = result->flags | NO_EXPAND;
Jakub Jelinek73096712005-03-04 16:33:23 +01002453 t->val = result->val;
Neil Booth644edda2001-10-02 12:57:24 +00002454 result = t;
2455 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +00002456
Neil Booth644edda2001-10-02 12:57:24 +00002457 break;
Neil Booth93c803682000-10-28 17:59:06 +00002458 }
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002459
Tom Tromey92582b72011-10-17 09:59:12 +00002460 out:
2461 if (location != NULL)
2462 {
2463 if (virt_loc == 0)
2464 virt_loc = result->src_loc;
2465 *location = virt_loc;
2466
2467 if (!CPP_OPTION (pfile, track_macro_expansion)
Dodji Seketeli828a7f72012-05-29 09:36:29 +00002468 && macro_of_context (pfile->context) != NULL)
Tom Tromey92582b72011-10-17 09:59:12 +00002469 /* We are in a macro expansion context, are not tracking
2470 virtual location, but were asked to report the location
2471 of the expansion point of the macro being expanded. */
2472 *location = pfile->invocation_location;
2473
2474 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2475 }
Dodji Seketeli828a7f72012-05-29 09:36:29 +00002476
2477 pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002478 return result;
Neil Booth93c803682000-10-28 17:59:06 +00002479}
2480
Tom Tromey92582b72011-10-17 09:59:12 +00002481/* External routine to get a token. Also used nearly everywhere
2482 internally, except for places where we know we can safely call
2483 _cpp_lex_token directly, such as lexing a directive name.
2484
2485 Macro expansions and directives are transparently handled,
2486 including entering included files. Thus tokens are post-macro
2487 expansion, and after any intervening directives. External callers
2488 see CPP_EOF only at EOF. Internal callers also see it when meeting
2489 a directive inside a macro call, when at the end of a directive and
2490 state.in_directive is still 1, and at the end of argument
2491 pre-expansion. */
2492const cpp_token *
2493cpp_get_token (cpp_reader *pfile)
2494{
2495 return cpp_get_token_1 (pfile, NULL);
2496}
2497
2498/* Like cpp_get_token, but also returns a virtual token location
2499 separate from the spelling location carried by the returned token.
2500
2501 LOC is an out parameter; *LOC is set to the location "as expected
2502 by the user". This matters when a token results from macro
2503 expansion; in that case the token's spelling location indicates the
2504 locus of the token in the definition of the macro but *LOC
2505 virtually encodes all the other meaningful locuses associated to
2506 the token.
2507
2508 What? virtual location? Yes, virtual location.
2509
2510 If the token results from macro expansion and if macro expansion
2511 location tracking is enabled its virtual location encodes (at the
2512 same time):
2513
2514 - the spelling location of the token
2515
2516 - the locus of the macro expansion point
2517
2518 - the locus of the point where the token got instantiated as part
2519 of the macro expansion process.
2520
2521 You have to use the linemap API to get the locus you are interested
2522 in from a given virtual location.
2523
2524 Note however that virtual locations are not necessarily ordered for
2525 relations '<' and '>'. One must use the function
2526 linemap_location_before_p instead of using the relational operator
2527 '<'.
2528
2529 If macro expansion tracking is off and if the token results from
2530 macro expansion the virtual location is the expansion point of the
2531 macro that got expanded.
2532
2533 When the token doesn't result from macro expansion, the virtual
2534 location is just the same thing as its spelling location. */
2535
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002536const cpp_token *
2537cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2538{
Dodji Seketeli828a7f72012-05-29 09:36:29 +00002539 return cpp_get_token_1 (pfile, loc);
Tom Tromey5ffeb9132007-09-06 16:24:05 +00002540}
2541
Neil Booth7065e132001-02-14 07:38:20 +00002542/* Returns true if we're expanding an object-like macro that was
2543 defined in a system header. Just checks the macro at the top of
2544 the stack. Used for diagnostic suppression. */
2545int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002546cpp_sys_macro_p (cpp_reader *pfile)
Neil Booth7065e132001-02-14 07:38:20 +00002547{
Dodji Seketeli4e65a472012-04-30 11:41:08 +00002548 cpp_hashnode *node = NULL;
2549
2550 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2551 node = pfile->context->c.mc->macro_node;
2552 else
2553 node = pfile->context->c.macro;
Neil Booth7065e132001-02-14 07:38:20 +00002554
Neil Booth644edda2001-10-02 12:57:24 +00002555 return node && node->value.macro && node->value.macro->syshdr;
Neil Booth7065e132001-02-14 07:38:20 +00002556}
2557
Neil Boothaf0d16c2002-04-22 17:48:02 +00002558/* Read each token in, until end of the current file. Directives are
2559 transparently processed. */
Neil Booth93c803682000-10-28 17:59:06 +00002560void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002561cpp_scan_nooutput (cpp_reader *pfile)
Neil Booth93c803682000-10-28 17:59:06 +00002562{
Per Bothner22234f52004-02-18 14:02:39 -08002563 /* Request a CPP_EOF token at the end of this file, rather than
2564 transparently continuing with the including file. */
2565 pfile->buffer->return_at_eof = true;
2566
Zack Weinbergc6e83802004-06-05 20:58:06 +00002567 pfile->state.discarding_output++;
2568 pfile->state.prevent_expansion++;
2569
Neil Booth590e1982002-07-01 12:47:54 +00002570 if (CPP_OPTION (pfile, traditional))
2571 while (_cpp_read_logical_line_trad (pfile))
2572 ;
2573 else
2574 while (cpp_get_token (pfile)->type != CPP_EOF)
2575 ;
Zack Weinbergc6e83802004-06-05 20:58:06 +00002576
2577 pfile->state.discarding_output--;
2578 pfile->state.prevent_expansion--;
Neil Booth93c803682000-10-28 17:59:06 +00002579}
2580
Ben Elliston5950c3c2008-07-14 05:09:48 +00002581/* Step back one or more tokens obtained from the lexer. */
2582void
2583_cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2584{
2585 pfile->lookaheads += count;
2586 while (count--)
2587 {
2588 pfile->cur_token--;
2589 if (pfile->cur_token == pfile->cur_run->base
2590 /* Possible with -fpreprocessed and no leading #line. */
2591 && pfile->cur_run->prev != NULL)
2592 {
2593 pfile->cur_run = pfile->cur_run->prev;
2594 pfile->cur_token = pfile->cur_run->limit;
2595 }
2596 }
2597}
2598
Jakub Jelinek1c90c6f2006-06-09 23:13:25 +02002599/* Step back one (or more) tokens. Can only step back more than 1 if
Neil Boothbdcbe492001-09-13 20:05:17 +00002600 they are from the lexer, and not from macro expansion. */
Neil Boothb528a072000-11-12 11:46:21 +00002601void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002602_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
Neil Booth93c803682000-10-28 17:59:06 +00002603{
Neil Boothbdcbe492001-09-13 20:05:17 +00002604 if (pfile->context->prev == NULL)
Ben Elliston5950c3c2008-07-14 05:09:48 +00002605 _cpp_backup_tokens_direct (pfile, count);
Neil Booth93c803682000-10-28 17:59:06 +00002606 else
2607 {
Neil Boothbdcbe492001-09-13 20:05:17 +00002608 if (count != 1)
2609 abort ();
Tom Tromey92582b72011-10-17 09:59:12 +00002610 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
Neil Booth82eda772002-06-04 13:07:06 +00002611 FIRST (pfile->context).token--;
Tom Tromey92582b72011-10-17 09:59:12 +00002612 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
Neil Booth82eda772002-06-04 13:07:06 +00002613 FIRST (pfile->context).ptoken--;
Tom Tromey92582b72011-10-17 09:59:12 +00002614 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2615 {
2616 FIRST (pfile->context).ptoken--;
2617 if (pfile->context->c.macro)
2618 {
2619 macro_context *m = pfile->context->c.mc;
2620 m->cur_virt_loc--;
2621#ifdef ENABLE_CHECKING
2622 if (m->cur_virt_loc < m->virt_locs)
2623 abort ();
2624#endif
2625 }
2626 else
2627 abort ();
2628 }
2629 else
2630 abort ();
Neil Booth93c803682000-10-28 17:59:06 +00002631 }
Neil Booth93c803682000-10-28 17:59:06 +00002632}
2633
2634/* #define directive parsing and handling. */
2635
Kazu Hiratada7d8302002-09-22 02:03:17 +00002636/* Returns nonzero if a macro redefinition warning is required. */
Neil Boothcbc69f82002-06-05 20:27:12 +00002637static bool
Jakub Jelinek8e680db2010-06-11 20:37:34 +02002638warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002639 const cpp_macro *macro2)
Neil Booth93c803682000-10-28 17:59:06 +00002640{
2641 const cpp_macro *macro1;
2642 unsigned int i;
2643
Neil Booth618cdda2001-02-25 09:43:03 +00002644 /* Some redefinitions need to be warned about regardless. */
2645 if (node->flags & NODE_WARN)
Neil Boothcbc69f82002-06-05 20:27:12 +00002646 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002647
Simon Baldwinc047ce92008-09-18 15:39:08 +00002648 /* Suppress warnings for builtins that lack the NODE_WARN flag. */
2649 if (node->flags & NODE_BUILTIN)
Jakub Jelinek8e680db2010-06-11 20:37:34 +02002650 {
2651 if (!pfile->cb.user_builtin_macro
2652 || !pfile->cb.user_builtin_macro (pfile, node))
2653 return false;
2654 }
Simon Baldwinc047ce92008-09-18 15:39:08 +00002655
Ben Elliston5950c3c2008-07-14 05:09:48 +00002656 /* Redefinitions of conditional (context-sensitive) macros, on
2657 the other hand, must be allowed silently. */
2658 if (node->flags & NODE_CONDITIONAL)
2659 return false;
2660
Neil Booth618cdda2001-02-25 09:43:03 +00002661 /* Redefinition of a macro is allowed if and only if the old and new
Kazu Hirataec5c56d2001-08-01 17:57:27 +00002662 definitions are the same. (6.10.3 paragraph 2). */
Neil Booth93c803682000-10-28 17:59:06 +00002663 macro1 = node->value.macro;
2664
Neil Booth6618c5d2002-06-10 06:03:13 +00002665 /* Don't check count here as it can be different in valid
2666 traditional redefinitions with just whitespace differences. */
2667 if (macro1->paramc != macro2->paramc
Neil Booth93c803682000-10-28 17:59:06 +00002668 || macro1->fun_like != macro2->fun_like
Neil Booth28e0f042000-12-09 12:06:37 +00002669 || macro1->variadic != macro2->variadic)
Neil Boothcbc69f82002-06-05 20:27:12 +00002670 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002671
2672 /* Check parameter spellings. */
2673 for (i = 0; i < macro1->paramc; i++)
2674 if (macro1->params[i] != macro2->params[i])
Neil Boothcbc69f82002-06-05 20:27:12 +00002675 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002676
Neil Boothcbc69f82002-06-05 20:27:12 +00002677 /* Check the replacement text or tokens. */
2678 if (CPP_OPTION (pfile, traditional))
Neil Booth6618c5d2002-06-10 06:03:13 +00002679 return _cpp_expansions_different_trad (macro1, macro2);
Neil Boothcbc69f82002-06-05 20:27:12 +00002680
DJ Deloriea7f36da2003-06-01 14:55:15 -04002681 if (macro1->count != macro2->count)
2682 return true;
2683
2684 for (i = 0; i < macro1->count; i++)
2685 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2686 return true;
Neil Boothcbc69f82002-06-05 20:27:12 +00002687
2688 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002689}
2690
Neil Booth93c803682000-10-28 17:59:06 +00002691/* Free the definition of hashnode H. */
Neil Booth93c803682000-10-28 17:59:06 +00002692void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002693_cpp_free_definition (cpp_hashnode *h)
Zack Weinberg711b8822000-07-18 00:59:49 +00002694{
Neil Booth93c803682000-10-28 17:59:06 +00002695 /* Macros and assertions no longer have anything to free. */
2696 h->type = NT_VOID;
2697 /* Clear builtin flag in case of redefinition. */
Joseph Myers93d45d92008-04-02 20:42:53 +01002698 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
Neil Booth93c803682000-10-28 17:59:06 +00002699}
Zack Weinberg711b8822000-07-18 00:59:49 +00002700
Neil Booth14baae02001-09-17 18:26:12 +00002701/* Save parameter NODE to the parameter list of macro MACRO. Returns
Kazu Hiratada7d8302002-09-22 02:03:17 +00002702 zero on success, nonzero if the parameter is a duplicate. */
Neil Boothc70f6ed2002-06-07 06:26:32 +00002703bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002704_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00002705{
Zack Weinberg4977bab2002-12-16 18:23:00 +00002706 unsigned int len;
Neil Booth93c803682000-10-28 17:59:06 +00002707 /* Constraint 6.10.3.6 - duplicate parameter names. */
Zack Weinberg4977bab2002-12-16 18:23:00 +00002708 if (node->flags & NODE_MACRO_ARG)
Zack Weinberg711b8822000-07-18 00:59:49 +00002709 {
John David Anglin0527bc42003-11-01 22:56:54 +00002710 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
Neil Boothebef4e82002-04-14 18:42:47 +00002711 NODE_NAME (node));
Neil Booth23ff0222002-07-17 17:27:14 +00002712 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002713 }
2714
Neil Booth8c3b2692001-09-30 10:03:11 +00002715 if (BUFF_ROOM (pfile->a_buff)
2716 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2717 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
Neil Booth93c803682000-10-28 17:59:06 +00002718
Neil Booth8c3b2692001-09-30 10:03:11 +00002719 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
Zack Weinberg4977bab2002-12-16 18:23:00 +00002720 node->flags |= NODE_MACRO_ARG;
2721 len = macro->paramc * sizeof (union _cpp_hashnode_value);
2722 if (len > pfile->macro_buffer_len)
2723 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002724 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
2725 len);
Zack Weinberg4977bab2002-12-16 18:23:00 +00002726 pfile->macro_buffer_len = len;
2727 }
2728 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
2729 = node->value;
2730
2731 node->value.arg_index = macro->paramc;
Neil Booth23ff0222002-07-17 17:27:14 +00002732 return false;
Neil Booth93c803682000-10-28 17:59:06 +00002733}
2734
Neil Booth23ff0222002-07-17 17:27:14 +00002735/* Check the syntax of the parameters in a MACRO definition. Returns
2736 false if an error occurs. */
2737static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002738parse_params (cpp_reader *pfile, cpp_macro *macro)
Neil Booth93c803682000-10-28 17:59:06 +00002739{
Neil Booth93c803682000-10-28 17:59:06 +00002740 unsigned int prev_ident = 0;
2741
Neil Booth93c803682000-10-28 17:59:06 +00002742 for (;;)
2743 {
Neil Booth345894b2001-09-16 13:44:29 +00002744 const cpp_token *token = _cpp_lex_token (pfile);
Neil Booth93c803682000-10-28 17:59:06 +00002745
Neil Booth345894b2001-09-16 13:44:29 +00002746 switch (token->type)
Zack Weinberg711b8822000-07-18 00:59:49 +00002747 {
2748 default:
Jason Thorpe477cdac2002-04-07 03:12:23 +00002749 /* Allow/ignore comments in parameter lists if we are
2750 preserving comments in macro expansions. */
2751 if (token->type == CPP_COMMENT
2752 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
2753 continue;
2754
John David Anglin0527bc42003-11-01 22:56:54 +00002755 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00002756 "\"%s\" may not appear in macro parameter list",
Neil Booth345894b2001-09-16 13:44:29 +00002757 cpp_token_as_text (pfile, token));
Neil Booth23ff0222002-07-17 17:27:14 +00002758 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002759
Zack Weinberg711b8822000-07-18 00:59:49 +00002760 case CPP_NAME:
2761 if (prev_ident)
2762 {
John David Anglin0527bc42003-11-01 22:56:54 +00002763 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00002764 "macro parameters must be comma-separated");
Neil Booth23ff0222002-07-17 17:27:14 +00002765 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002766 }
Zack Weinberg711b8822000-07-18 00:59:49 +00002767 prev_ident = 1;
Neil Booth93c803682000-10-28 17:59:06 +00002768
Joseph Myers9a0c6182009-05-10 15:27:32 +01002769 if (_cpp_save_parameter (pfile, macro, token->val.node.node))
Neil Booth23ff0222002-07-17 17:27:14 +00002770 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002771 continue;
2772
2773 case CPP_CLOSE_PAREN:
Neil Booth93c803682000-10-28 17:59:06 +00002774 if (prev_ident || macro->paramc == 0)
Neil Booth23ff0222002-07-17 17:27:14 +00002775 return true;
Zack Weinberg711b8822000-07-18 00:59:49 +00002776
2777 /* Fall through to pick up the error. */
2778 case CPP_COMMA:
2779 if (!prev_ident)
2780 {
John David Anglin0527bc42003-11-01 22:56:54 +00002781 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
Neil Booth23ff0222002-07-17 17:27:14 +00002782 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002783 }
2784 prev_ident = 0;
2785 continue;
2786
2787 case CPP_ELLIPSIS:
Neil Booth28e0f042000-12-09 12:06:37 +00002788 macro->variadic = 1;
Zack Weinberg711b8822000-07-18 00:59:49 +00002789 if (!prev_ident)
2790 {
Neil Boothc70f6ed2002-06-07 06:26:32 +00002791 _cpp_save_parameter (pfile, macro,
2792 pfile->spec_nodes.n__VA_ARGS__);
Neil Booth93c803682000-10-28 17:59:06 +00002793 pfile->state.va_args_ok = 1;
Richard Hendersone5b79212004-02-19 14:18:50 -08002794 if (! CPP_OPTION (pfile, c99)
Joseph Myerse3339d02010-09-29 15:49:14 +01002795 && CPP_OPTION (pfile, cpp_pedantic)
Richard Hendersone5b79212004-02-19 14:18:50 -08002796 && CPP_OPTION (pfile, warn_variadic_macros))
Simon Baldwin87cf0652010-04-07 17:18:10 +00002797 cpp_pedwarning
2798 (pfile, CPP_W_VARIADIC_MACROS,
2799 "anonymous variadic macros were introduced in C99");
Zack Weinberg711b8822000-07-18 00:59:49 +00002800 }
Joseph Myerse3339d02010-09-29 15:49:14 +01002801 else if (CPP_OPTION (pfile, cpp_pedantic)
Richard Hendersone5b79212004-02-19 14:18:50 -08002802 && CPP_OPTION (pfile, warn_variadic_macros))
Simon Baldwin87cf0652010-04-07 17:18:10 +00002803 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2804 "ISO C does not permit named variadic macros");
Zack Weinberg711b8822000-07-18 00:59:49 +00002805
Neil Booth93c803682000-10-28 17:59:06 +00002806 /* We're at the end, and just expect a closing parenthesis. */
Neil Booth345894b2001-09-16 13:44:29 +00002807 token = _cpp_lex_token (pfile);
2808 if (token->type == CPP_CLOSE_PAREN)
Neil Booth23ff0222002-07-17 17:27:14 +00002809 return true;
Neil Booth93c803682000-10-28 17:59:06 +00002810 /* Fall through. */
2811
2812 case CPP_EOF:
John David Anglin0527bc42003-11-01 22:56:54 +00002813 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
Neil Booth23ff0222002-07-17 17:27:14 +00002814 return false;
Zack Weinberg711b8822000-07-18 00:59:49 +00002815 }
Zack Weinberg711b8822000-07-18 00:59:49 +00002816 }
2817}
2818
Neil Booth14baae02001-09-17 18:26:12 +00002819/* Allocate room for a token from a macro's replacement list. */
Neil Booth93c803682000-10-28 17:59:06 +00002820static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002821alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00002822{
Neil Booth8c3b2692001-09-30 10:03:11 +00002823 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2824 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
Zack Weinberg711b8822000-07-18 00:59:49 +00002825
Neil Booth8c3b2692001-09-30 10:03:11 +00002826 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
Neil Booth14baae02001-09-17 18:26:12 +00002827}
2828
Neil Boothd15a58c2002-01-03 18:32:55 +00002829/* Lex a token from the expansion of MACRO, but mark parameters as we
2830 find them and warn of traditional stringification. */
Neil Booth14baae02001-09-17 18:26:12 +00002831static cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002832lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Neil Booth14baae02001-09-17 18:26:12 +00002833{
Tom Tromeyee380362007-01-30 15:46:01 +00002834 cpp_token *token, *saved_cur_token;
Neil Booth14baae02001-09-17 18:26:12 +00002835
Tom Tromeyee380362007-01-30 15:46:01 +00002836 saved_cur_token = pfile->cur_token;
Neil Booth14baae02001-09-17 18:26:12 +00002837 pfile->cur_token = alloc_expansion_token (pfile, macro);
2838 token = _cpp_lex_direct (pfile);
Tom Tromeyee380362007-01-30 15:46:01 +00002839 pfile->cur_token = saved_cur_token;
Neil Booth93c803682000-10-28 17:59:06 +00002840
Neil Boothd15a58c2002-01-03 18:32:55 +00002841 /* Is this a parameter? */
Zack Weinberg4977bab2002-12-16 18:23:00 +00002842 if (token->type == CPP_NAME
Joseph Myers9a0c6182009-05-10 15:27:32 +01002843 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
Zack Weinberg711b8822000-07-18 00:59:49 +00002844 {
Neil Booth93c803682000-10-28 17:59:06 +00002845 token->type = CPP_MACRO_ARG;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002846 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
Zack Weinberg711b8822000-07-18 00:59:49 +00002847 }
Neil Booth93c803682000-10-28 17:59:06 +00002848 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2849 && (token->type == CPP_STRING || token->type == CPP_CHAR))
2850 check_trad_stringification (pfile, macro, &token->val.str);
Zack Weinberg711b8822000-07-18 00:59:49 +00002851
Neil Booth93c803682000-10-28 17:59:06 +00002852 return token;
Zack Weinberg711b8822000-07-18 00:59:49 +00002853}
2854
Neil Boothcbc69f82002-06-05 20:27:12 +00002855static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002856create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg711b8822000-07-18 00:59:49 +00002857{
Neil Boothcbc69f82002-06-05 20:27:12 +00002858 cpp_token *token;
Neil Booth14baae02001-09-17 18:26:12 +00002859 const cpp_token *ctoken;
Simon Martin126e0732007-05-23 20:58:34 +00002860 bool following_paste_op = false;
2861 const char *paste_op_error_msg =
2862 N_("'##' cannot appear at either end of a macro expansion");
Joseph Myersaa508502009-04-19 18:10:56 +01002863 unsigned int num_extra_tokens = 0;
Zack Weinberg711b8822000-07-18 00:59:49 +00002864
Neil Booth93c803682000-10-28 17:59:06 +00002865 /* Get the first token of the expansion (or the '(' of a
2866 function-like macro). */
Neil Booth14baae02001-09-17 18:26:12 +00002867 ctoken = _cpp_lex_token (pfile);
2868
2869 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
Zack Weinberg711b8822000-07-18 00:59:49 +00002870 {
Neil Boothcbc69f82002-06-05 20:27:12 +00002871 bool ok = parse_params (pfile, macro);
Neil Booth8c3b2692001-09-30 10:03:11 +00002872 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
2873 if (!ok)
Neil Boothcbc69f82002-06-05 20:27:12 +00002874 return false;
Neil Booth8c3b2692001-09-30 10:03:11 +00002875
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002876 /* Success. Commit or allocate the parameter array. */
2877 if (pfile->hash_table->alloc_subobject)
2878 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002879 cpp_hashnode **params =
2880 (cpp_hashnode **) pfile->hash_table->alloc_subobject
2881 (sizeof (cpp_hashnode *) * macro->paramc);
Paolo Bonzinibe0f1e52005-02-14 08:52:24 +00002882 memcpy (params, macro->params,
2883 sizeof (cpp_hashnode *) * macro->paramc);
2884 macro->params = params;
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002885 }
2886 else
2887 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
Neil Booth93c803682000-10-28 17:59:06 +00002888 macro->fun_like = 1;
Neil Booth93c803682000-10-28 17:59:06 +00002889 }
Neil Booth14baae02001-09-17 18:26:12 +00002890 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
Jakub Jelinekcae064e2005-04-05 22:07:06 +02002891 {
2892 /* While ISO C99 requires whitespace before replacement text
2893 in a macro definition, ISO C90 with TC1 allows there characters
2894 from the basic source character set. */
2895 if (CPP_OPTION (pfile, c99))
2896 cpp_error (pfile, CPP_DL_PEDWARN,
2897 "ISO C99 requires whitespace after the macro name");
2898 else
2899 {
2900 int warntype = CPP_DL_WARNING;
2901 switch (ctoken->type)
2902 {
2903 case CPP_ATSIGN:
2904 case CPP_AT_NAME:
2905 case CPP_OBJC_STRING:
2906 /* '@' is not in basic character set. */
2907 warntype = CPP_DL_PEDWARN;
2908 break;
2909 case CPP_OTHER:
2910 /* Basic character set sans letters, digits and _. */
2911 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
2912 ctoken->val.str.text[0]) == NULL)
2913 warntype = CPP_DL_PEDWARN;
2914 break;
2915 default:
2916 /* All other tokens start with a character from basic
2917 character set. */
2918 break;
2919 }
2920 cpp_error (pfile, warntype,
2921 "missing whitespace after the macro name");
2922 }
2923 }
Neil Booth93c803682000-10-28 17:59:06 +00002924
Neil Booth14baae02001-09-17 18:26:12 +00002925 if (macro->fun_like)
2926 token = lex_expansion_token (pfile, macro);
2927 else
2928 {
2929 token = alloc_expansion_token (pfile, macro);
2930 *token = *ctoken;
2931 }
Neil Booth93c803682000-10-28 17:59:06 +00002932
2933 for (;;)
2934 {
2935 /* Check the stringifying # constraint 6.10.3.2.1 of
2936 function-like macros when lexing the subsequent token. */
2937 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
Zack Weinberg711b8822000-07-18 00:59:49 +00002938 {
Neil Booth93c803682000-10-28 17:59:06 +00002939 if (token->type == CPP_MACRO_ARG)
2940 {
Joseph Myersaa508502009-04-19 18:10:56 +01002941 if (token->flags & PREV_WHITE)
2942 token->flags |= SP_PREV_WHITE;
2943 if (token[-1].flags & DIGRAPH)
2944 token->flags |= SP_DIGRAPH;
Neil Booth93c803682000-10-28 17:59:06 +00002945 token->flags &= ~PREV_WHITE;
2946 token->flags |= STRINGIFY_ARG;
2947 token->flags |= token[-1].flags & PREV_WHITE;
2948 token[-1] = token[0];
2949 macro->count--;
2950 }
2951 /* Let assembler get away with murder. */
Neil Boothbdb05a72000-11-26 17:31:13 +00002952 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth93c803682000-10-28 17:59:06 +00002953 {
John David Anglin0527bc42003-11-01 22:56:54 +00002954 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +00002955 "'#' is not followed by a macro parameter");
Neil Boothcbc69f82002-06-05 20:27:12 +00002956 return false;
Neil Booth93c803682000-10-28 17:59:06 +00002957 }
2958 }
2959
2960 if (token->type == CPP_EOF)
Simon Martin126e0732007-05-23 20:58:34 +00002961 {
2962 /* Paste operator constraint 6.10.3.3.1:
2963 Token-paste ##, can appear in both object-like and
2964 function-like macros, but not at the end. */
2965 if (following_paste_op)
2966 {
2967 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
2968 return false;
2969 }
2970 break;
2971 }
Neil Booth93c803682000-10-28 17:59:06 +00002972
2973 /* Paste operator constraint 6.10.3.3.1. */
2974 if (token->type == CPP_PASTE)
2975 {
2976 /* Token-paste ##, can appear in both object-like and
Simon Martin126e0732007-05-23 20:58:34 +00002977 function-like macros, but not at the beginning. */
2978 if (macro->count == 1)
Neil Booth93c803682000-10-28 17:59:06 +00002979 {
Simon Martin126e0732007-05-23 20:58:34 +00002980 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
Neil Boothcbc69f82002-06-05 20:27:12 +00002981 return false;
Neil Booth93c803682000-10-28 17:59:06 +00002982 }
2983
Joseph Myersaa508502009-04-19 18:10:56 +01002984 if (token[-1].flags & PASTE_LEFT)
2985 {
2986 macro->extra_tokens = 1;
2987 num_extra_tokens++;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002988 token->val.token_no = macro->count - 1;
Joseph Myersaa508502009-04-19 18:10:56 +01002989 }
2990 else
2991 {
2992 --macro->count;
2993 token[-1].flags |= PASTE_LEFT;
2994 if (token->flags & DIGRAPH)
2995 token[-1].flags |= SP_DIGRAPH;
2996 if (token->flags & PREV_WHITE)
2997 token[-1].flags |= SP_PREV_WHITE;
2998 }
Neil Booth93c803682000-10-28 17:59:06 +00002999 }
3000
Simon Martin126e0732007-05-23 20:58:34 +00003001 following_paste_op = (token->type == CPP_PASTE);
Neil Booth93c803682000-10-28 17:59:06 +00003002 token = lex_expansion_token (pfile, macro);
3003 }
3004
Neil Booth601328b2002-05-16 05:53:24 +00003005 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00003006 macro->traditional = 0;
Neil Booth8c3b2692001-09-30 10:03:11 +00003007
Neil Booth4c2b6472000-11-11 13:19:01 +00003008 /* Don't count the CPP_EOF. */
3009 macro->count--;
Neil Booth93c803682000-10-28 17:59:06 +00003010
Neil Boothd15a58c2002-01-03 18:32:55 +00003011 /* Clear whitespace on first token for warn_of_redefinition(). */
Neil Booth8c3b2692001-09-30 10:03:11 +00003012 if (macro->count)
Neil Booth601328b2002-05-16 05:53:24 +00003013 macro->exp.tokens[0].flags &= ~PREV_WHITE;
Neil Booth8c3b2692001-09-30 10:03:11 +00003014
Geoffrey Keatingd8044162004-06-09 20:10:13 +00003015 /* Commit or allocate the memory. */
3016 if (pfile->hash_table->alloc_subobject)
3017 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00003018 cpp_token *tokns =
3019 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
3020 * macro->count);
Joseph Myersaa508502009-04-19 18:10:56 +01003021 if (num_extra_tokens)
3022 {
3023 /* Place second and subsequent ## or %:%: tokens in
3024 sequences of consecutive such tokens at the end of the
3025 list to preserve information about where they appear, how
3026 they are spelt and whether they are preceded by
3027 whitespace without otherwise interfering with macro
3028 expansion. */
3029 cpp_token *normal_dest = tokns;
3030 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
3031 unsigned int i;
3032 for (i = 0; i < macro->count; i++)
3033 {
3034 if (macro->exp.tokens[i].type == CPP_PASTE)
3035 *extra_dest++ = macro->exp.tokens[i];
3036 else
3037 *normal_dest++ = macro->exp.tokens[i];
3038 }
3039 }
3040 else
3041 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
Geoffrey Keatingd8044162004-06-09 20:10:13 +00003042 macro->exp.tokens = tokns;
3043 }
3044 else
3045 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
Neil Booth8c3b2692001-09-30 10:03:11 +00003046
Neil Boothcbc69f82002-06-05 20:27:12 +00003047 return true;
3048}
Neil Booth44ed91a2000-10-29 11:37:18 +00003049
Kazu Hiratada7d8302002-09-22 02:03:17 +00003050/* Parse a macro and save its expansion. Returns nonzero on success. */
Neil Boothcbc69f82002-06-05 20:27:12 +00003051bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00003052_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
Neil Boothcbc69f82002-06-05 20:27:12 +00003053{
3054 cpp_macro *macro;
3055 unsigned int i;
3056 bool ok;
3057
Geoffrey Keatingd8044162004-06-09 20:10:13 +00003058 if (pfile->hash_table->alloc_subobject)
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00003059 macro = (cpp_macro *) pfile->hash_table->alloc_subobject
3060 (sizeof (cpp_macro));
Geoffrey Keatingd8044162004-06-09 20:10:13 +00003061 else
3062 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
Neil Boothcbc69f82002-06-05 20:27:12 +00003063 macro->line = pfile->directive_line;
3064 macro->params = 0;
3065 macro->paramc = 0;
3066 macro->variadic = 0;
Neil Booth23345bb2003-03-14 21:47:50 +00003067 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
Neil Boothcbc69f82002-06-05 20:27:12 +00003068 macro->count = 0;
3069 macro->fun_like = 0;
Joseph Myersaa508502009-04-19 18:10:56 +01003070 macro->extra_tokens = 0;
Neil Booth7065e132001-02-14 07:38:20 +00003071 /* To suppress some diagnostics. */
Per Bothner12f9df42004-02-11 07:29:30 -08003072 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
Neil Booth7065e132001-02-14 07:38:20 +00003073
Neil Boothcbc69f82002-06-05 20:27:12 +00003074 if (CPP_OPTION (pfile, traditional))
3075 ok = _cpp_create_trad_definition (pfile, macro);
3076 else
3077 {
Neil Boothcbc69f82002-06-05 20:27:12 +00003078 ok = create_iso_definition (pfile, macro);
3079
Tom Tromeyee380362007-01-30 15:46:01 +00003080 /* We set the type for SEEN_EOL() in directives.c.
Neil Boothcbc69f82002-06-05 20:27:12 +00003081
3082 Longer term we should lex the whole line before coming here,
3083 and just copy the expansion. */
Neil Boothcbc69f82002-06-05 20:27:12 +00003084
3085 /* Stop the lexer accepting __VA_ARGS__. */
3086 pfile->state.va_args_ok = 0;
3087 }
3088
3089 /* Clear the fast argument lookup indices. */
3090 for (i = macro->paramc; i-- > 0; )
Zack Weinberg4977bab2002-12-16 18:23:00 +00003091 {
3092 struct cpp_hashnode *node = macro->params[i];
3093 node->flags &= ~ NODE_MACRO_ARG;
3094 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
3095 }
Neil Boothcbc69f82002-06-05 20:27:12 +00003096
3097 if (!ok)
3098 return ok;
3099
Neil Boothc2734e02002-07-26 16:29:31 +00003100 if (node->type == NT_MACRO)
Neil Booth93c803682000-10-28 17:59:06 +00003101 {
Neil Bootha69cbaa2002-07-23 22:57:49 +00003102 if (CPP_OPTION (pfile, warn_unused_macros))
3103 _cpp_warn_if_unused_macro (pfile, node, NULL);
3104
Neil Boothcbc69f82002-06-05 20:27:12 +00003105 if (warn_of_redefinition (pfile, node, macro))
Neil Booth93c803682000-10-28 17:59:06 +00003106 {
Simon Baldwin87cf0652010-04-07 17:18:10 +00003107 const int reason = (node->flags & NODE_BUILTIN)
3108 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
Joseph Myers148e4212009-03-29 23:56:07 +01003109 bool warned;
Simon Baldwin87cf0652010-04-07 17:18:10 +00003110
3111 warned = cpp_pedwarning_with_line (pfile, reason,
3112 pfile->directive_line, 0,
3113 "\"%s\" redefined",
3114 NODE_NAME (node));
Neil Booth93c803682000-10-28 17:59:06 +00003115
Joseph Myers148e4212009-03-29 23:56:07 +01003116 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3117 cpp_error_with_line (pfile, CPP_DL_NOTE,
Neil Boothcbc69f82002-06-05 20:27:12 +00003118 node->value.macro->line, 0,
3119 "this is the location of the previous definition");
Zack Weinberg711b8822000-07-18 00:59:49 +00003120 }
Zack Weinberg711b8822000-07-18 00:59:49 +00003121 }
3122
Neil Boothc2734e02002-07-26 16:29:31 +00003123 if (node->type != NT_VOID)
3124 _cpp_free_definition (node);
3125
Zack Weinberg711b8822000-07-18 00:59:49 +00003126 /* Enter definition in hash table. */
Neil Booth93c803682000-10-28 17:59:06 +00003127 node->type = NT_MACRO;
3128 node->value.macro = macro;
Tom Tromey607f74e2007-11-30 18:24:01 +00003129 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
Tom Tromeyec460532008-01-22 21:43:49 +00003130 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3131 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3132 in the C standard, as something that one must use in C++.
3133 However DR#593 indicates that these aren't actually mentioned
3134 in the C++ standard. We special-case them anyway. */
3135 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3136 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
Neil Booth618cdda2001-02-25 09:43:03 +00003137 node->flags |= NODE_WARN;
Zack Weinberg711b8822000-07-18 00:59:49 +00003138
Ben Elliston5950c3c2008-07-14 05:09:48 +00003139 /* If user defines one of the conditional macros, remove the
3140 conditional flag */
3141 node->flags &= ~NODE_CONDITIONAL;
3142
Neil Booth93c803682000-10-28 17:59:06 +00003143 return ok;
Zack Weinberg711b8822000-07-18 00:59:49 +00003144}
3145
Neil Boothd15a58c2002-01-03 18:32:55 +00003146/* Warn if a token in STRING matches one of a function-like MACRO's
3147 parameters. */
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003148static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00003149check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3150 const cpp_string *string)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003151{
Neil Booth93c803682000-10-28 17:59:06 +00003152 unsigned int i, len;
Neil Booth6338b352003-04-23 22:44:06 +00003153 const uchar *p, *q, *limit;
Kazu Hiratadf383482002-05-22 22:02:16 +00003154
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003155 /* Loop over the string. */
Neil Booth6338b352003-04-23 22:44:06 +00003156 limit = string->text + string->len - 1;
3157 for (p = string->text + 1; p < limit; p = q)
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003158 {
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003159 /* Find the start of an identifier. */
Greg McGary61c16b12000-09-15 21:25:02 +00003160 while (p < limit && !is_idstart (*p))
3161 p++;
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003162
3163 /* Find the end of the identifier. */
3164 q = p;
Greg McGary61c16b12000-09-15 21:25:02 +00003165 while (q < limit && is_idchar (*q))
3166 q++;
Neil Booth93c803682000-10-28 17:59:06 +00003167
3168 len = q - p;
3169
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003170 /* Loop over the function macro arguments to see if the
3171 identifier inside the string matches one of them. */
Neil Booth93c803682000-10-28 17:59:06 +00003172 for (i = 0; i < macro->paramc; i++)
3173 {
3174 const cpp_hashnode *node = macro->params[i];
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003175
Neil Booth2a967f32001-05-20 06:26:45 +00003176 if (NODE_LEN (node) == len
3177 && !memcmp (p, NODE_NAME (node), len))
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003178 {
John David Anglin0527bc42003-11-01 22:56:54 +00003179 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinbergf458d1d2002-02-27 18:48:07 +00003180 "macro argument \"%s\" would be stringified in traditional C",
Neil Boothebef4e82002-04-14 18:42:47 +00003181 NODE_NAME (node));
Kaveh R. Ghazie1aa5142000-09-10 03:41:50 +00003182 break;
3183 }
3184 }
3185 }
3186}
Neil Booth93c803682000-10-28 17:59:06 +00003187
Neil Booth70961712001-06-23 11:34:41 +00003188/* Returns the name, arguments and expansion of a macro, in a format
3189 suitable to be read back in again, and therefore also for DWARF 2
3190 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3191 Caller is expected to generate the "#define" bit if needed. The
Neil Booth93c803682000-10-28 17:59:06 +00003192 returned text is temporary, and automatically freed later. */
Neil Booth93c803682000-10-28 17:59:06 +00003193const unsigned char *
Jakub Jelinek8e680db2010-06-11 20:37:34 +02003194cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth93c803682000-10-28 17:59:06 +00003195{
3196 unsigned int i, len;
Jakub Jelinek8e680db2010-06-11 20:37:34 +02003197 const cpp_macro *macro;
Neil Booth93c803682000-10-28 17:59:06 +00003198 unsigned char *buffer;
3199
3200 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3201 {
Jakub Jelinek8e680db2010-06-11 20:37:34 +02003202 if (node->type != NT_MACRO
3203 || !pfile->cb.user_builtin_macro
3204 || !pfile->cb.user_builtin_macro (pfile, node))
3205 {
3206 cpp_error (pfile, CPP_DL_ICE,
3207 "invalid hash type %d in cpp_macro_definition",
3208 node->type);
3209 return 0;
3210 }
Neil Booth93c803682000-10-28 17:59:06 +00003211 }
3212
Jakub Jelinek8e680db2010-06-11 20:37:34 +02003213 macro = node->value.macro;
Neil Booth93c803682000-10-28 17:59:06 +00003214 /* Calculate length. */
Neil Booth8dc901d2002-05-29 19:30:07 +00003215 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
Neil Booth93c803682000-10-28 17:59:06 +00003216 if (macro->fun_like)
3217 {
Jim Blandy64d08262002-04-05 00:12:40 +00003218 len += 4; /* "()" plus possible final ".." of named
3219 varargs (we have + 1 below). */
Neil Booth93c803682000-10-28 17:59:06 +00003220 for (i = 0; i < macro->paramc; i++)
Jim Blandy64d08262002-04-05 00:12:40 +00003221 len += NODE_LEN (macro->params[i]) + 1; /* "," */
Neil Booth93c803682000-10-28 17:59:06 +00003222 }
3223
Eric Christopher6da55c02005-02-15 23:18:04 +00003224 /* This should match below where we fill in the buffer. */
Neil Booth278c4662002-06-19 05:40:08 +00003225 if (CPP_OPTION (pfile, traditional))
3226 len += _cpp_replacement_text_len (macro);
3227 else
Neil Booth93c803682000-10-28 17:59:06 +00003228 {
Joseph Myersaa508502009-04-19 18:10:56 +01003229 unsigned int count = macro_real_token_count (macro);
3230 for (i = 0; i < count; i++)
Neil Booth278c4662002-06-19 05:40:08 +00003231 {
3232 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00003233
Neil Booth278c4662002-06-19 05:40:08 +00003234 if (token->type == CPP_MACRO_ARG)
Joseph Myers9a0c6182009-05-10 15:27:32 +01003235 len += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
Neil Booth278c4662002-06-19 05:40:08 +00003236 else
Eric Christopher6da55c02005-02-15 23:18:04 +00003237 len += cpp_token_len (token);
3238
Neil Booth278c4662002-06-19 05:40:08 +00003239 if (token->flags & STRINGIFY_ARG)
3240 len++; /* "#" */
3241 if (token->flags & PASTE_LEFT)
3242 len += 3; /* " ##" */
Eric Christopher6da55c02005-02-15 23:18:04 +00003243 if (token->flags & PREV_WHITE)
3244 len++; /* " " */
Neil Booth278c4662002-06-19 05:40:08 +00003245 }
Neil Booth93c803682000-10-28 17:59:06 +00003246 }
3247
3248 if (len > pfile->macro_buffer_len)
Alexandre Oliva4b49c362001-01-09 09:30:43 +00003249 {
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00003250 pfile->macro_buffer = XRESIZEVEC (unsigned char,
3251 pfile->macro_buffer, len);
Alexandre Oliva4b49c362001-01-09 09:30:43 +00003252 pfile->macro_buffer_len = len;
3253 }
Neil Booth70961712001-06-23 11:34:41 +00003254
3255 /* Fill in the buffer. Start with the macro name. */
Neil Booth93c803682000-10-28 17:59:06 +00003256 buffer = pfile->macro_buffer;
Neil Booth70961712001-06-23 11:34:41 +00003257 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
3258 buffer += NODE_LEN (node);
Neil Booth93c803682000-10-28 17:59:06 +00003259
3260 /* Parameter names. */
3261 if (macro->fun_like)
3262 {
3263 *buffer++ = '(';
3264 for (i = 0; i < macro->paramc; i++)
3265 {
3266 cpp_hashnode *param = macro->params[i];
3267
3268 if (param != pfile->spec_nodes.n__VA_ARGS__)
3269 {
Neil Bootha28c50352001-05-16 22:02:09 +00003270 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3271 buffer += NODE_LEN (param);
Neil Booth93c803682000-10-28 17:59:06 +00003272 }
3273
3274 if (i + 1 < macro->paramc)
Kazu Hiratadf383482002-05-22 22:02:16 +00003275 /* Don't emit a space after the comma here; we're trying
3276 to emit a Dwarf-friendly definition, and the Dwarf spec
3277 forbids spaces in the argument list. */
Jim Blandy64d08262002-04-05 00:12:40 +00003278 *buffer++ = ',';
Neil Booth28e0f042000-12-09 12:06:37 +00003279 else if (macro->variadic)
Neil Booth93c803682000-10-28 17:59:06 +00003280 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3281 }
3282 *buffer++ = ')';
3283 }
3284
Jim Blandye37b38d2002-03-19 21:43:39 +00003285 /* The Dwarf spec requires a space after the macro name, even if the
3286 definition is the empty string. */
3287 *buffer++ = ' ';
3288
Neil Booth278c4662002-06-19 05:40:08 +00003289 if (CPP_OPTION (pfile, traditional))
3290 buffer = _cpp_copy_replacement_text (macro, buffer);
3291 else if (macro->count)
Neil Booth93c803682000-10-28 17:59:06 +00003292 /* Expansion tokens. */
Neil Booth93c803682000-10-28 17:59:06 +00003293 {
Joseph Myersaa508502009-04-19 18:10:56 +01003294 unsigned int count = macro_real_token_count (macro);
3295 for (i = 0; i < count; i++)
Neil Booth93c803682000-10-28 17:59:06 +00003296 {
Neil Booth601328b2002-05-16 05:53:24 +00003297 cpp_token *token = &macro->exp.tokens[i];
Neil Booth93c803682000-10-28 17:59:06 +00003298
3299 if (token->flags & PREV_WHITE)
3300 *buffer++ = ' ';
3301 if (token->flags & STRINGIFY_ARG)
3302 *buffer++ = '#';
3303
3304 if (token->type == CPP_MACRO_ARG)
3305 {
Neil Bootha28c50352001-05-16 22:02:09 +00003306 memcpy (buffer,
Joseph Myers9a0c6182009-05-10 15:27:32 +01003307 NODE_NAME (macro->params[token->val.macro_arg.arg_no - 1]),
3308 NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]));
3309 buffer += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
Neil Booth93c803682000-10-28 17:59:06 +00003310 }
3311 else
Geoffrey Keating47e20492005-03-12 10:44:06 +00003312 buffer = cpp_spell_token (pfile, token, buffer, false);
Neil Booth93c803682000-10-28 17:59:06 +00003313
3314 if (token->flags & PASTE_LEFT)
3315 {
3316 *buffer++ = ' ';
3317 *buffer++ = '#';
3318 *buffer++ = '#';
3319 /* Next has PREV_WHITE; see _cpp_create_definition. */
3320 }
3321 }
3322 }
3323
3324 *buffer = '\0';
3325 return pfile->macro_buffer;
3326}