]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cppmacro.c
configure.target: Force mips to use the generic cpu routines.
[thirdparty/gcc.git] / gcc / cppmacro.c
CommitLineData
93c80368 1/* Part of CPP library. (Macro and #define handling.)
711b8824 2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
5d8ebbd8 3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
711b8824
ZW
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
25
26#include "config.h"
27#include "system.h"
28#include "cpplib.h"
29#include "cpphash.h"
30
93c80368
NB
31typedef struct macro_arg macro_arg;
32struct macro_arg
33{
4ed5bcfb 34 const cpp_token **first; /* First token in unexpanded argument. */
6d2f8887 35 const cpp_token **expanded; /* Macro-expanded argument. */
4ed5bcfb 36 const cpp_token *stringified; /* Stringified argument. */
93c80368
NB
37 unsigned int count; /* # of tokens in argument. */
38 unsigned int expanded_count; /* # of tokens in expanded argument. */
711b8824
ZW
39};
40
93c80368
NB
41/* Macro expansion. */
42
29b10746 43static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
644eddaa 44static int builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
4ed5bcfb 45static void push_token_context
644eddaa 46 PARAMS ((cpp_reader *, cpp_hashnode *, const cpp_token *, unsigned int));
4ed5bcfb 47static void push_ptoken_context
644eddaa 48 PARAMS ((cpp_reader *, cpp_hashnode *, _cpp_buff *,
1e013d2e 49 const cpp_token **, unsigned int));
b8af0ca5 50static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
93c80368 51static cpp_context *next_context PARAMS ((cpp_reader *));
4ed5bcfb
NB
52static const cpp_token *padding_token
53 PARAMS ((cpp_reader *, const cpp_token *));
93c80368 54static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
562a5c27 55static const cpp_token *new_string_token PARAMS ((cpp_reader *, uchar *,
4ed5bcfb 56 unsigned int));
d15a58c0 57static const cpp_token *new_number_token PARAMS ((cpp_reader *, unsigned int));
4ed5bcfb
NB
58static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
59static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
c9e7a609
NB
60static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
61 const cpp_token *));
e808ec9c
NB
62static void replace_args PARAMS ((cpp_reader *, cpp_hashnode *, cpp_macro *,
63 macro_arg *));
d6da836d 64static _cpp_buff *funlike_invocation_p PARAMS ((cpp_reader *, cpp_hashnode *));
cbc69f84 65static bool create_iso_definition PARAMS ((cpp_reader *, cpp_macro *));
93c80368 66
93c80368
NB
67/* #define directive parsing and handling. */
68
14baae01 69static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
93c80368 70static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
cbc69f84
NB
71static bool warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
72 const cpp_macro *));
93c80368 73static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
e1aa5140 74static void check_trad_stringification PARAMS ((cpp_reader *,
93c80368 75 const cpp_macro *,
e1aa5140 76 const cpp_string *));
711b8824 77
4ed5bcfb
NB
78/* Allocates and returns a CPP_STRING token, containing TEXT of length
79 LEN, after null-terminating it. TEXT must be in permanent storage. */
80static const cpp_token *
81new_string_token (pfile, text, len)
82 cpp_reader *pfile;
83 unsigned char *text;
93c80368
NB
84 unsigned int len;
85{
4ed5bcfb 86 cpp_token *token = _cpp_temp_token (pfile);
93c80368 87
4ed5bcfb 88 text[len] = '\0';
93c80368 89 token->type = CPP_STRING;
4ed5bcfb
NB
90 token->val.str.len = len;
91 token->val.str.text = text;
93c80368 92 token->flags = 0;
4ed5bcfb 93 return token;
93c80368
NB
94}
95
4ed5bcfb
NB
96/* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */
97static const cpp_token *
98new_number_token (pfile, number)
93c80368 99 cpp_reader *pfile;
d15a58c0 100 unsigned int number;
93c80368 101{
4ed5bcfb 102 cpp_token *token = _cpp_temp_token (pfile);
d15a58c0
NB
103 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
104 unsigned char *buf = _cpp_unaligned_alloc (pfile, 21);
93c80368 105
d15a58c0 106 sprintf ((char *) buf, "%u", number);
93c80368
NB
107 token->type = CPP_NUMBER;
108 token->val.str.text = buf;
109 token->val.str.len = ustrlen (buf);
110 token->flags = 0;
4ed5bcfb 111 return token;
93c80368
NB
112}
113
114static const char * const monthnames[] =
115{
116 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
117 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
118};
119
644eddaa
NB
120/* Handle builtin macros like __FILE__, and push the resulting token
121 on the context stack. Also handles _Pragma, for which no new token
d15a58c0
NB
122 is created. Returns 1 if it generates a new token context, 0 to
123 return the token to the caller. */
644eddaa 124static int
4ed5bcfb 125builtin_macro (pfile, node)
93c80368 126 cpp_reader *pfile;
4ed5bcfb 127 cpp_hashnode *node;
93c80368 128{
644eddaa
NB
129 const cpp_token *result;
130
93c80368
NB
131 switch (node->value.builtin)
132 {
4ed5bcfb 133 default:
ebef4e8c
NB
134 cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
135 NODE_NAME (node));
644eddaa 136 return 0;
4ed5bcfb 137
93c80368
NB
138 case BT_FILE:
139 case BT_BASE_FILE:
711b8824 140 {
4ed5bcfb 141 unsigned int len;
0bda4760 142 const char *name;
562a5c27 143 uchar *buf;
bb74c963 144 const struct line_map *map = pfile->map;
0bda4760
NB
145
146 if (node->value.builtin == BT_BASE_FILE)
bb74c963
NB
147 while (! MAIN_FILE_P (map))
148 map = INCLUDED_FROM (&pfile->line_maps, map);
0bda4760 149
bb74c963 150 name = map->to_file;
4ed5bcfb 151 len = strlen (name);
ece54d54 152 buf = _cpp_unaligned_alloc (pfile, len * 4 + 1);
dcc229e5 153 len = cpp_quote_string (buf, (const unsigned char *) name, len) - buf;
4ed5bcfb 154
644eddaa 155 result = new_string_token (pfile, buf, len);
711b8824 156 }
644eddaa
NB
157 break;
158
93c80368 159 case BT_INCLUDE_LEVEL:
d8693c6f
NB
160 /* The line map depth counts the primary source as level 1, but
161 historically __INCLUDE_DEPTH__ has called the primary source
162 level 0. */
644eddaa
NB
163 result = new_number_token (pfile, pfile->line_maps.depth - 1);
164 break;
93c80368
NB
165
166 case BT_SPECLINE:
167 /* If __LINE__ is embedded in a macro, it must expand to the
168 line of the macro's invocation, not its definition.
169 Otherwise things like assert() will not work properly. */
644eddaa
NB
170 result = new_number_token (pfile,
171 SOURCE_LINE (pfile->map,
172 pfile->cur_token[-1].line));
173 break;
93c80368 174
5279d739
ZW
175 /* __STDC__ has the value 1 under normal circumstances.
176 However, if (a) we are in a system header, (b) the option
2a1dc0d8
ZW
177 stdc_0_in_system_headers is true (set by target config), and
178 (c) we are not in strictly conforming mode, then it has the
179 value 0. */
93c80368
NB
180 case BT_STDC:
181 {
5279d739 182 int stdc;
2a1dc0d8 183 enum c_lang lang = CPP_OPTION (pfile, lang);
5279d739
ZW
184 if (CPP_IN_SYSTEM_HEADER (pfile)
185 && CPP_OPTION (pfile, stdc_0_in_system_headers)
2a1dc0d8
ZW
186 && !(lang == CLK_STDC89 || lang == CLK_STDC94
187 || lang == CLK_STDC99)) /* || lang == CLK_CXX98 ? */
5279d739
ZW
188 stdc = 0;
189 else
190 stdc = 1;
191
644eddaa 192 result = new_number_token (pfile, stdc);
93c80368 193 }
644eddaa 194 break;
711b8824 195
93c80368
NB
196 case BT_DATE:
197 case BT_TIME:
198 if (pfile->date.type == CPP_EOF)
199 {
4ed5bcfb
NB
200 /* Allocate __DATE__ and __TIME__ strings from permanent
201 storage. We only do this once, and don't generate them
202 at init time, because time() and localtime() are very
203 slow on some systems. */
93c80368
NB
204 time_t tt = time (NULL);
205 struct tm *tb = localtime (&tt);
206
4ed5bcfb 207 pfile->date.val.str.text =
ece54d54 208 _cpp_unaligned_alloc (pfile, sizeof ("Oct 11 1347"));
4ed5bcfb
NB
209 pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
210 pfile->date.type = CPP_STRING;
211 pfile->date.flags = 0;
93c80368
NB
212 sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
213 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
4ed5bcfb
NB
214
215 pfile->time.val.str.text =
ece54d54 216 _cpp_unaligned_alloc (pfile, sizeof ("12:34:56"));
4ed5bcfb
NB
217 pfile->time.val.str.len = sizeof ("12:34:56") - 1;
218 pfile->time.type = CPP_STRING;
219 pfile->time.flags = 0;
93c80368
NB
220 sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
221 tb->tm_hour, tb->tm_min, tb->tm_sec);
222 }
93c80368 223
644eddaa
NB
224 if (node->value.builtin == BT_DATE)
225 result = &pfile->date;
226 else
227 result = &pfile->time;
228 break;
229
230 case BT_PRAGMA:
231 /* Don't interpret _Pragma within directives. The standard is
232 not clear on this, but to me this makes most sense. */
233 if (pfile->state.in_directive)
234 return 0;
235
236 _cpp_do__Pragma (pfile);
237 return 1;
93c80368 238 }
644eddaa
NB
239
240 push_token_context (pfile, NULL, result, 1);
241 return 1;
711b8824
ZW
242}
243
d15a58c0
NB
244/* Copies SRC, of length LEN, to DEST, adding backslashes before all
245 backslashes and double quotes. Non-printable characters are
dcc229e5
ZW
246 converted to octal. DEST must be of sufficient size. Returns
247 a pointer to the end of the string. */
562a5c27 248uchar *
dcc229e5 249cpp_quote_string (dest, src, len)
562a5c27
NB
250 uchar *dest;
251 const uchar *src;
93c80368
NB
252 unsigned int len;
253{
254 while (len--)
255 {
562a5c27 256 uchar c = *src++;
711b8824 257
93c80368
NB
258 if (c == '\\' || c == '"')
259 {
260 *dest++ = '\\';
261 *dest++ = c;
262 }
263 else
264 {
265 if (ISPRINT (c))
266 *dest++ = c;
267 else
711b8824 268 {
93c80368
NB
269 sprintf ((char *) dest, "\\%03o", c);
270 dest += 4;
711b8824 271 }
93c80368
NB
272 }
273 }
711b8824 274
93c80368
NB
275 return dest;
276}
711b8824 277
d15a58c0
NB
278/* Convert a token sequence ARG to a single string token according to
279 the rules of the ISO C #-operator. */
4ed5bcfb 280static const cpp_token *
93c80368
NB
281stringify_arg (pfile, arg)
282 cpp_reader *pfile;
283 macro_arg *arg;
284{
ece54d54
NB
285 unsigned char *dest = BUFF_FRONT (pfile->u_buff);
286 unsigned int i, escape_it, backslash_count = 0;
4ed5bcfb 287 const cpp_token *source = NULL;
ece54d54 288 size_t len;
711b8824 289
93c80368
NB
290 /* Loop, reading in the argument's tokens. */
291 for (i = 0; i < arg->count; i++)
292 {
4ed5bcfb 293 const cpp_token *token = arg->first[i];
4ed5bcfb
NB
294
295 if (token->type == CPP_PADDING)
296 {
297 if (source == NULL)
298 source = token->val.source;
299 continue;
300 }
711b8824 301
93c80368 302 escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
cc937581 303 || token->type == CPP_CHAR || token->type == CPP_WCHAR);
711b8824 304
ece54d54
NB
305 /* Room for each char being written in octal, initial space and
306 final NUL. */
4ed5bcfb 307 len = cpp_token_len (token);
93c80368 308 if (escape_it)
93c80368 309 len *= 4;
ece54d54 310 len += 2;
711b8824 311
ece54d54 312 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
93c80368 313 {
ece54d54 314 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
8c3b2693 315 _cpp_extend_buff (pfile, &pfile->u_buff, len);
ece54d54 316 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
93c80368 317 }
711b8824 318
4ed5bcfb 319 /* Leading white space? */
ece54d54 320 if (dest != BUFF_FRONT (pfile->u_buff))
4ed5bcfb
NB
321 {
322 if (source == NULL)
323 source = token;
324 if (source->flags & PREV_WHITE)
325 *dest++ = ' ';
326 }
327 source = NULL;
711b8824 328
93c80368
NB
329 if (escape_it)
330 {
ece54d54
NB
331 _cpp_buff *buff = _cpp_get_buff (pfile, len);
332 unsigned char *buf = BUFF_FRONT (buff);
93c80368 333 len = cpp_spell_token (pfile, token, buf) - buf;
dcc229e5 334 dest = cpp_quote_string (dest, buf, len);
ece54d54 335 _cpp_release_buff (pfile, buff);
93c80368
NB
336 }
337 else
338 dest = cpp_spell_token (pfile, token, dest);
93c80368 339
6c53ebff 340 if (token->type == CPP_OTHER && token->val.c == '\\')
93c80368
NB
341 backslash_count++;
342 else
343 backslash_count = 0;
344 }
345
346 /* Ignore the final \ of invalid string literals. */
347 if (backslash_count & 1)
348 {
ebef4e8c
NB
349 cpp_error (pfile, DL_WARNING,
350 "invalid string literal, ignoring final '\\'");
ece54d54 351 dest--;
93c80368
NB
352 }
353
4ed5bcfb 354 /* Commit the memory, including NUL, and return the token. */
ece54d54
NB
355 len = dest - BUFF_FRONT (pfile->u_buff);
356 BUFF_FRONT (pfile->u_buff) = dest + 1;
357 return new_string_token (pfile, dest - len, len);
93c80368
NB
358}
359
c9e7a609
NB
360/* Try to paste two tokens. On success, return non-zero. In any
361 case, PLHS is updated to point to the pasted token, which is
362 guaranteed to not have the PASTE_LEFT flag set. */
363static bool
364paste_tokens (pfile, plhs, rhs)
d63eefbf 365 cpp_reader *pfile;
c9e7a609 366 const cpp_token **plhs, *rhs;
d63eefbf 367{
c9e7a609
NB
368 unsigned char *buf, *end;
369 const cpp_token *lhs;
370 unsigned int len;
371 bool valid;
372
373 lhs = *plhs;
374 len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
375 buf = (unsigned char *) alloca (len);
376 end = cpp_spell_token (pfile, lhs, buf);
377
378 /* Avoid comment headers, since they are still processed in stage 3.
379 It is simpler to insert a space here, rather than modifying the
380 lexer to ignore comments in some circumstances. Simply returning
381 false doesn't work, since we want to clear the PASTE_LEFT flag. */
382 if (lhs->type == CPP_DIV
383 && (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
384 *end++ = ' ';
385 end = cpp_spell_token (pfile, rhs, end);
4d6baafa 386 *end = '\0';
c9e7a609
NB
387
388 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
389
390 /* Tweak the column number the lexer will report. */
391 pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
392
393 /* We don't want a leading # to be interpreted as a directive. */
394 pfile->buffer->saved_flags = 0;
395
396 /* Set pfile->cur_token as required by _cpp_lex_direct. */
397 pfile->cur_token = _cpp_temp_token (pfile);
398 *plhs = _cpp_lex_direct (pfile);
480709cc 399 valid = pfile->buffer->cur == pfile->buffer->rlimit;
c9e7a609
NB
400 _cpp_pop_buffer (pfile);
401
402 return valid;
d63eefbf
NB
403}
404
d15a58c0
NB
405/* Handles an arbitrarily long sequence of ## operators, with initial
406 operand LHS. This implementation is left-associative,
407 non-recursive, and finishes a paste before handling succeeding
408 ones. If a paste fails, we back up to the RHS of the failing ##
409 operator before pushing the context containing the result of prior
410 successful pastes, with the effect that the RHS appears in the
411 output stream after the pasted LHS normally. */
93c80368
NB
412static void
413paste_all_tokens (pfile, lhs)
414 cpp_reader *pfile;
4ed5bcfb 415 const cpp_token *lhs;
93c80368 416{
4ed5bcfb
NB
417 const cpp_token *rhs;
418 cpp_context *context = pfile->context;
419
93c80368
NB
420 do
421 {
422 /* Take the token directly from the current context. We can do
423 this, because we are in the replacement list of either an
424 object-like macro, or a function-like macro with arguments
425 inserted. In either case, the constraints to #define
d63eefbf 426 guarantee we have at least one more token. */
4ed5bcfb 427 if (context->direct_p)
82eda77e 428 rhs = FIRST (context).token++;
4ed5bcfb 429 else
82eda77e 430 rhs = *FIRST (context).ptoken++;
4ed5bcfb
NB
431
432 if (rhs->type == CPP_PADDING)
433 abort ();
434
c9e7a609 435 if (!paste_tokens (pfile, &lhs, rhs))
93c80368 436 {
4ed5bcfb 437 _cpp_backup_tokens (pfile, 1);
c9e7a609 438
c3bf3e6e 439 /* Mandatory error for all apart from assembler. */
c9e7a609 440 if (CPP_OPTION (pfile, lang) != CLK_ASM)
c3bf3e6e 441 cpp_error (pfile, DL_ERROR,
c9e7a609 442 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
ebef4e8c
NB
443 cpp_token_as_text (pfile, lhs),
444 cpp_token_as_text (pfile, rhs));
4c2b647d 445 break;
93c80368 446 }
93c80368
NB
447 }
448 while (rhs->flags & PASTE_LEFT);
449
c9e7a609
NB
450 /* Put the resulting token in its own context. */
451 push_token_context (pfile, NULL, lhs, 1);
93c80368
NB
452}
453
1ce676a0
NB
454/* Returns TRUE if the number of arguments ARGC supplied in an
455 invocation of the MACRO referenced by NODE is valid. An empty
456 invocation to a macro with no parameters should pass ARGC as zero.
457
458 Note that MACRO cannot necessarily be deduced from NODE, in case
459 NODE was redefined whilst collecting arguments. */
460bool
461_cpp_arguments_ok (pfile, macro, node, argc)
462 cpp_reader *pfile;
463 cpp_macro *macro;
464 const cpp_hashnode *node;
465 unsigned int argc;
466{
467 if (argc == macro->paramc)
468 return true;
469
470 if (argc < macro->paramc)
471 {
472 /* As an extension, a rest argument is allowed to not appear in
473 the invocation at all.
474 e.g. #define debug(format, args...) something
475 debug("string");
476
477 This is exactly the same as if there had been an empty rest
478 argument - debug("string", ). */
479
480 if (argc + 1 == macro->paramc && macro->variadic)
481 {
482 if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
483 cpp_error (pfile, DL_PEDWARN,
484 "ISO C99 requires rest arguments to be used");
485 return true;
486 }
487
488 cpp_error (pfile, DL_ERROR,
489 "macro \"%s\" requires %u arguments, but only %u given",
490 NODE_NAME (node), macro->paramc, argc);
491 }
492 else
493 cpp_error (pfile, DL_ERROR,
494 "macro \"%s\" passed %u arguments, but takes just %u",
495 NODE_NAME (node), argc, macro->paramc);
496
497 return false;
498}
499
d15a58c0
NB
500/* Reads and returns the arguments to a function-like macro
501 invocation. Assumes the opening parenthesis has been processed.
502 If there is an error, emits an appropriate diagnostic and returns
503 NULL. Each argument is terminated by a CPP_EOF token, for the
504 future benefit of expand_arg(). */
b8af0ca5
NB
505static _cpp_buff *
506collect_args (pfile, node)
93c80368 507 cpp_reader *pfile;
b8af0ca5 508 const cpp_hashnode *node;
93c80368 509{
b8af0ca5
NB
510 _cpp_buff *buff, *base_buff;
511 cpp_macro *macro;
512 macro_arg *args, *arg;
513 const cpp_token *token;
514 unsigned int argc;
b8af0ca5
NB
515
516 macro = node->value.macro;
517 if (macro->paramc)
518 argc = macro->paramc;
519 else
520 argc = 1;
521 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
522 + sizeof (macro_arg)));
523 base_buff = buff;
524 args = (macro_arg *) buff->base;
525 memset (args, 0, argc * sizeof (macro_arg));
ece54d54 526 buff->cur = (unsigned char *) &args[argc];
b8af0ca5
NB
527 arg = args, argc = 0;
528
529 /* Collect the tokens making up each argument. We don't yet know
530 how many arguments have been supplied, whether too many or too
531 few. Hence the slightly bizarre usage of "argc" and "arg". */
532 do
93c80368 533 {
b8af0ca5
NB
534 unsigned int paren_depth = 0;
535 unsigned int ntokens = 0;
93c80368 536
b8af0ca5
NB
537 argc++;
538 arg->first = (const cpp_token **) buff->cur;
711b8824 539
b8af0ca5
NB
540 for (;;)
541 {
542 /* Require space for 2 new tokens (including a CPP_EOF). */
ece54d54 543 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
b8af0ca5 544 {
8c3b2693
NB
545 buff = _cpp_append_extend_buff (pfile, buff,
546 1000 * sizeof (cpp_token *));
b8af0ca5
NB
547 arg->first = (const cpp_token **) buff->cur;
548 }
4ed5bcfb 549
b8af0ca5 550 token = cpp_get_token (pfile);
711b8824 551
b8af0ca5
NB
552 if (token->type == CPP_PADDING)
553 {
554 /* Drop leading padding. */
555 if (ntokens == 0)
556 continue;
557 }
558 else if (token->type == CPP_OPEN_PAREN)
559 paren_depth++;
560 else if (token->type == CPP_CLOSE_PAREN)
561 {
562 if (paren_depth-- == 0)
563 break;
564 }
565 else if (token->type == CPP_COMMA)
566 {
567 /* A comma does not terminate an argument within
568 parentheses or as part of a variable argument. */
569 if (paren_depth == 0
570 && ! (macro->variadic && argc == macro->paramc))
571 break;
572 }
573 else if (token->type == CPP_EOF
574 || (token->type == CPP_HASH && token->flags & BOL))
575 break;
93c80368 576
b8af0ca5
NB
577 arg->first[ntokens++] = token;
578 }
93c80368 579
b8af0ca5
NB
580 /* Drop trailing padding. */
581 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
582 ntokens--;
93c80368 583
b8af0ca5
NB
584 arg->count = ntokens;
585 arg->first[ntokens] = &pfile->eof;
93c80368 586
b8af0ca5
NB
587 /* Terminate the argument. Excess arguments loop back and
588 overwrite the final legitimate argument, before failing. */
589 if (argc <= macro->paramc)
590 {
ece54d54 591 buff->cur = (unsigned char *) &arg->first[ntokens + 1];
b8af0ca5
NB
592 if (argc != macro->paramc)
593 arg++;
594 }
93c80368 595 }
e808ec9c 596 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
93c80368 597
e808ec9c 598 if (token->type == CPP_EOF)
93c80368 599 {
ece54d54
NB
600 /* We still need the CPP_EOF to end directives, and to end
601 pre-expansion of a macro argument. Step back is not
602 unconditional, since we don't want to return a CPP_EOF to our
603 callers at the end of an -include-d file. */
e808ec9c 604 if (pfile->context->prev || pfile->state.in_directive)
b8af0ca5 605 _cpp_backup_tokens (pfile, 1);
ebef4e8c
NB
606 cpp_error (pfile, DL_ERROR,
607 "unterminated argument list invoking macro \"%s\"",
a28c5035 608 NODE_NAME (node));
93c80368 609 }
1ce676a0 610 else
93c80368 611 {
1ce676a0
NB
612 /* A single empty argument is counted as no argument. */
613 if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
614 argc = 0;
615 if (_cpp_arguments_ok (pfile, macro, node, argc))
616 return base_buff;
93c80368
NB
617 }
618
1ce676a0 619 /* An error occurred. */
b8af0ca5
NB
620 _cpp_release_buff (pfile, base_buff);
621 return NULL;
93c80368
NB
622}
623
d6da836d
NB
624/* Search for an opening parenthesis to the macro of NODE, in such a
625 way that, if none is found, we don't lose the information in any
626 intervening padding tokens. If we find the parenthesis, collect
627 the arguments and return the buffer containing them. */
628static _cpp_buff *
4ed5bcfb 629funlike_invocation_p (pfile, node)
93c80368 630 cpp_reader *pfile;
644eddaa 631 cpp_hashnode *node;
93c80368 632{
d6da836d 633 const cpp_token *token, *padding = NULL;
4ed5bcfb 634
d6da836d 635 for (;;)
bdcbe496 636 {
d6da836d
NB
637 token = cpp_get_token (pfile);
638 if (token->type != CPP_PADDING)
639 break;
640 if (padding == NULL
641 || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
642 padding = token;
bdcbe496 643 }
93c80368 644
d6da836d 645 if (token->type == CPP_OPEN_PAREN)
93c80368 646 {
d6da836d
NB
647 pfile->state.parsing_args = 2;
648 return collect_args (pfile, node);
93c80368
NB
649 }
650
9ac3b1be
NB
651 /* CPP_EOF can be the end of macro arguments, or the end of the
652 file. We mustn't back up over the latter. Ugh. */
653 if (token->type != CPP_EOF || token == &pfile->eof)
654 {
655 /* Back up. We may have skipped padding, in which case backing
656 up more than one token when expanding macros is in general
657 too difficult. We re-insert it in its own context. */
658 _cpp_backup_tokens (pfile, 1);
659 if (padding)
660 push_token_context (pfile, NULL, padding, 1);
661 }
d6da836d
NB
662
663 return NULL;
93c80368
NB
664}
665
d15a58c0
NB
666/* Push the context of a macro with hash entry NODE onto the context
667 stack. If we can successfully expand the macro, we push a context
668 containing its yet-to-be-rescanned replacement list and return one.
669 Otherwise, we don't push a context and return zero. */
711b8824 670static int
29b10746 671enter_macro_context (pfile, node)
711b8824 672 cpp_reader *pfile;
29b10746 673 cpp_hashnode *node;
711b8824 674{
d15a58c0 675 /* The presence of a macro invalidates a file's controlling macro. */
644eddaa
NB
676 pfile->mi_valid = false;
677
3620711b
NB
678 pfile->state.angled_headers = false;
679
d15a58c0 680 /* Handle standard macros. */
644eddaa 681 if (! (node->flags & NODE_BUILTIN))
711b8824 682 {
4ed5bcfb 683 cpp_macro *macro = node->value.macro;
4c2b647d 684
d6da836d
NB
685 if (macro->fun_like)
686 {
687 _cpp_buff *buff;
688
689 pfile->state.prevent_expansion++;
690 pfile->keep_tokens++;
691 pfile->state.parsing_args = 1;
692 buff = funlike_invocation_p (pfile, node);
693 pfile->state.parsing_args = 0;
694 pfile->keep_tokens--;
695 pfile->state.prevent_expansion--;
696
697 if (buff == NULL)
698 {
699 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
ebef4e8c 700 cpp_error (pfile, DL_WARNING,
d6da836d 701 "function-like macro \"%s\" must be used with arguments in traditional C",
ebef4e8c 702 NODE_NAME (node));
d6da836d
NB
703
704 return 0;
705 }
706
e808ec9c
NB
707 if (macro->paramc > 0)
708 replace_args (pfile, node, macro, (macro_arg *) buff->base);
d6da836d
NB
709 _cpp_release_buff (pfile, buff);
710 }
93c80368 711
4ed5bcfb 712 /* Disable the macro within its expansion. */
644eddaa 713 node->flags |= NODE_DISABLED;
93c80368 714
4ed5bcfb 715 if (macro->paramc == 0)
601328bb 716 push_token_context (pfile, node, macro->exp.tokens, macro->count);
644eddaa
NB
717
718 return 1;
711b8824 719 }
644eddaa 720
d15a58c0 721 /* Handle built-in macros and the _Pragma operator. */
644eddaa 722 return builtin_macro (pfile, node);
93c80368
NB
723}
724
d15a58c0
NB
725/* Replace the parameters in a function-like macro of NODE with the
726 actual ARGS, and place the result in a newly pushed token context.
727 Expand each argument before replacing, unless it is operated upon
728 by the # or ## operators. */
93c80368 729static void
e808ec9c 730replace_args (pfile, node, macro, args)
93c80368 731 cpp_reader *pfile;
644eddaa 732 cpp_hashnode *node;
e808ec9c 733 cpp_macro *macro;
93c80368 734 macro_arg *args;
93c80368
NB
735{
736 unsigned int i, total;
737 const cpp_token *src, *limit;
4ed5bcfb 738 const cpp_token **dest, **first;
93c80368 739 macro_arg *arg;
1e013d2e 740 _cpp_buff *buff;
93c80368 741
93c80368 742 /* First, fully macro-expand arguments, calculating the number of
1e013d2e
NB
743 tokens in the final expansion as we go. The ordering of the if
744 statements below is subtle; we must handle stringification before
745 pasting. */
4ed5bcfb 746 total = macro->count;
601328bb 747 limit = macro->exp.tokens + macro->count;
4ed5bcfb 748
601328bb 749 for (src = macro->exp.tokens; src < limit; src++)
93c80368
NB
750 if (src->type == CPP_MACRO_ARG)
751 {
4ed5bcfb
NB
752 /* Leading and trailing padding tokens. */
753 total += 2;
754
93c80368
NB
755 /* We have an argument. If it is not being stringified or
756 pasted it is macro-replaced before insertion. */
6c53ebff 757 arg = &args[src->val.arg_no - 1];
4c2b647d 758
93c80368
NB
759 if (src->flags & STRINGIFY_ARG)
760 {
761 if (!arg->stringified)
4ed5bcfb 762 arg->stringified = stringify_arg (pfile, arg);
93c80368
NB
763 }
764 else if ((src->flags & PASTE_LEFT)
601328bb 765 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
93c80368
NB
766 total += arg->count - 1;
767 else
768 {
769 if (!arg->expanded)
4ed5bcfb 770 expand_arg (pfile, arg);
93c80368
NB
771 total += arg->expanded_count - 1;
772 }
773 }
774
4ed5bcfb
NB
775 /* Now allocate space for the expansion, copy the tokens and replace
776 the arguments. */
1e013d2e
NB
777 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
778 first = (const cpp_token **) buff->base;
4ed5bcfb 779 dest = first;
93c80368 780
601328bb 781 for (src = macro->exp.tokens; src < limit; src++)
4ed5bcfb
NB
782 {
783 unsigned int count;
784 const cpp_token **from, **paste_flag;
93c80368 785
4ed5bcfb
NB
786 if (src->type != CPP_MACRO_ARG)
787 {
788 *dest++ = src;
789 continue;
790 }
93c80368 791
4ed5bcfb
NB
792 paste_flag = 0;
793 arg = &args[src->val.arg_no - 1];
794 if (src->flags & STRINGIFY_ARG)
795 count = 1, from = &arg->stringified;
796 else if (src->flags & PASTE_LEFT)
797 count = arg->count, from = arg->first;
601328bb 798 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
4ed5bcfb
NB
799 {
800 count = arg->count, from = arg->first;
801 if (dest != first)
802 {
803 /* GCC has special semantics for , ## b where b is a
804 varargs parameter: the comma disappears if b was
805 given no actual arguments (not merely if b is an
806 empty argument); otherwise the paste flag is removed. */
807 if (dest[-1]->type == CPP_COMMA
808 && macro->variadic
809 && src->val.arg_no == macro->paramc)
810 {
811 if (count == 0)
812 dest--;
813 else
814 paste_flag = dest - 1;
815 }
816 /* Remove the paste flag if the RHS is a placemarker. */
817 else if (count == 0)
818 paste_flag = dest - 1;
819 }
820 }
821 else
822 count = arg->expanded_count, from = arg->expanded;
4c2b647d 823
4ed5bcfb
NB
824 /* Padding on the left of an argument (unless RHS of ##). */
825 if (!pfile->state.in_directive
601328bb 826 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
4ed5bcfb 827 *dest++ = padding_token (pfile, src);
93c80368 828
4ed5bcfb
NB
829 if (count)
830 {
831 memcpy (dest, from, count * sizeof (cpp_token *));
832 dest += count;
93c80368 833
4ed5bcfb
NB
834 /* With a non-empty argument on the LHS of ##, the last
835 token should be flagged PASTE_LEFT. */
836 if (src->flags & PASTE_LEFT)
837 paste_flag = dest - 1;
838 }
26ec42ee 839
4ed5bcfb
NB
840 /* Avoid paste on RHS (even case count == 0). */
841 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
842 *dest++ = &pfile->avoid_paste;
93c80368 843
4ed5bcfb
NB
844 /* Add a new paste flag, or remove an unwanted one. */
845 if (paste_flag)
846 {
847 cpp_token *token = _cpp_temp_token (pfile);
848 token->type = (*paste_flag)->type;
849 token->val.str = (*paste_flag)->val.str;
850 if (src->flags & PASTE_LEFT)
851 token->flags = (*paste_flag)->flags | PASTE_LEFT;
852 else
853 token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
854 *paste_flag = token;
855 }
856 }
4c2b647d 857
93c80368
NB
858 /* Free the expanded arguments. */
859 for (i = 0; i < macro->paramc; i++)
4ed5bcfb
NB
860 if (args[i].expanded)
861 free (args[i].expanded);
862
644eddaa 863 push_ptoken_context (pfile, node, buff, first, dest - first);
4ed5bcfb
NB
864}
865
866/* Return a special padding token, with padding inherited from SOURCE. */
867static const cpp_token *
868padding_token (pfile, source)
869 cpp_reader *pfile;
870 const cpp_token *source;
871{
872 cpp_token *result = _cpp_temp_token (pfile);
873
874 result->type = CPP_PADDING;
875 result->val.source = source;
876 result->flags = 0;
877 return result;
878}
879
d15a58c0
NB
880/* Get a new uninitialized context. Create a new one if we cannot
881 re-use an old one. */
4ed5bcfb
NB
882static cpp_context *
883next_context (pfile)
884 cpp_reader *pfile;
885{
886 cpp_context *result = pfile->context->next;
887
888 if (result == 0)
711b8824 889 {
4ed5bcfb
NB
890 result = xnew (cpp_context);
891 result->prev = pfile->context;
892 result->next = 0;
893 pfile->context->next = result;
93c80368 894 }
4ed5bcfb
NB
895
896 pfile->context = result;
897 return result;
93c80368
NB
898}
899
4ed5bcfb
NB
900/* Push a list of pointers to tokens. */
901static void
1e013d2e 902push_ptoken_context (pfile, macro, buff, first, count)
93c80368 903 cpp_reader *pfile;
644eddaa 904 cpp_hashnode *macro;
1e013d2e 905 _cpp_buff *buff;
4ed5bcfb
NB
906 const cpp_token **first;
907 unsigned int count;
93c80368
NB
908{
909 cpp_context *context = next_context (pfile);
93c80368 910
4ed5bcfb
NB
911 context->direct_p = false;
912 context->macro = macro;
1e013d2e 913 context->buff = buff;
82eda77e
NB
914 FIRST (context).ptoken = first;
915 LAST (context).ptoken = first + count;
4ed5bcfb
NB
916}
917
918/* Push a list of tokens. */
919static void
920push_token_context (pfile, macro, first, count)
921 cpp_reader *pfile;
644eddaa 922 cpp_hashnode *macro;
4ed5bcfb
NB
923 const cpp_token *first;
924 unsigned int count;
925{
926 cpp_context *context = next_context (pfile);
927
928 context->direct_p = true;
929 context->macro = macro;
1e013d2e 930 context->buff = NULL;
82eda77e
NB
931 FIRST (context).token = first;
932 LAST (context).token = first + count;
93c80368
NB
933}
934
cbc69f84
NB
935/* Push a traditional macro's replacement text. */
936void
1ce676a0 937_cpp_push_text_context (pfile, macro, start, len)
cbc69f84
NB
938 cpp_reader *pfile;
939 cpp_hashnode *macro;
1ce676a0
NB
940 const uchar *start;
941 size_t len;
cbc69f84
NB
942{
943 cpp_context *context = next_context (pfile);
944
945 context->direct_p = true;
946 context->macro = macro;
947 context->buff = NULL;
948 CUR (context) = start;
1ce676a0 949 RLIMIT (context) = start + len;
974c43f1 950 macro->flags |= NODE_DISABLED;
cbc69f84
NB
951}
952
d15a58c0
NB
953/* Expand an argument ARG before replacing parameters in a
954 function-like macro. This works by pushing a context with the
955 argument's tokens, and then expanding that into a temporary buffer
956 as if it were a normal part of the token stream. collect_args()
957 has terminated the argument's tokens with a CPP_EOF so that we know
958 when we have fully expanded the argument. */
93c80368
NB
959static void
960expand_arg (pfile, arg)
961 cpp_reader *pfile;
962 macro_arg *arg;
963{
4ed5bcfb
NB
964 unsigned int capacity;
965
4ed5bcfb
NB
966 if (arg->count == 0)
967 return;
93c80368
NB
968
969 /* Loop, reading in the arguments. */
4ed5bcfb
NB
970 capacity = 256;
971 arg->expanded = (const cpp_token **)
972 xmalloc (capacity * sizeof (cpp_token *));
93c80368 973
1e013d2e 974 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
4ed5bcfb 975 for (;;)
93c80368 976 {
4ed5bcfb
NB
977 const cpp_token *token;
978
979 if (arg->expanded_count + 1 >= capacity)
711b8824 980 {
93c80368 981 capacity *= 2;
4ed5bcfb
NB
982 arg->expanded = (const cpp_token **)
983 xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
93c80368 984 }
93c80368 985
4ed5bcfb 986 token = cpp_get_token (pfile);
93c80368 987
4ed5bcfb
NB
988 if (token->type == CPP_EOF)
989 break;
990
991 arg->expanded[arg->expanded_count++] = token;
992 }
993
1e013d2e 994 _cpp_pop_context (pfile);
93c80368
NB
995}
996
d15a58c0
NB
997/* Pop the current context off the stack, re-enabling the macro if the
998 context represented a macro's replacement list. The context
999 structure is not freed so that we can re-use it later. */
93c80368
NB
1000void
1001_cpp_pop_context (pfile)
1002 cpp_reader *pfile;
1003{
1e013d2e
NB
1004 cpp_context *context = pfile->context;
1005
1e013d2e 1006 if (context->macro)
644eddaa 1007 context->macro->flags &= ~NODE_DISABLED;
1e013d2e
NB
1008
1009 if (context->buff)
1010 _cpp_release_buff (pfile, context->buff);
93c80368 1011
1e013d2e 1012 pfile->context = context->prev;
93c80368
NB
1013}
1014
7f2f1a66
NB
1015/* Eternal routine to get a token. Also used nearly everywhere
1016 internally, except for places where we know we can safely call
1017 the lexer directly, such as lexing a directive name.
1018
1019 Macro expansions and directives are transparently handled,
1020 including entering included files. Thus tokens are post-macro
1021 expansion, and after any intervening directives. External callers
1022 see CPP_EOF only at EOF. Internal callers also see it when meeting
1023 a directive inside a macro call, when at the end of a directive and
1024 state.in_directive is still 1, and at the end of argument
1025 pre-expansion. */
4ed5bcfb
NB
1026const cpp_token *
1027cpp_get_token (pfile)
93c80368 1028 cpp_reader *pfile;
93c80368 1029{
4ed5bcfb
NB
1030 const cpp_token *result;
1031
29b10746 1032 for (;;)
93c80368 1033 {
4ed5bcfb 1034 cpp_hashnode *node;
93c80368
NB
1035 cpp_context *context = pfile->context;
1036
93c80368 1037 /* Context->prev == 0 <=> base context. */
bdcbe496 1038 if (!context->prev)
4ed5bcfb 1039 result = _cpp_lex_token (pfile);
82eda77e 1040 else if (FIRST (context).token != LAST (context).token)
29b10746 1041 {
4ed5bcfb 1042 if (context->direct_p)
82eda77e 1043 result = FIRST (context).token++;
4ed5bcfb 1044 else
82eda77e 1045 result = *FIRST (context).ptoken++;
4ed5bcfb
NB
1046
1047 if (result->flags & PASTE_LEFT)
ec1a23e6 1048 {
4ed5bcfb
NB
1049 paste_all_tokens (pfile, result);
1050 if (pfile->state.in_directive)
1051 continue;
1052 return padding_token (pfile, result);
ec1a23e6 1053 }
29b10746 1054 }
93c80368
NB
1055 else
1056 {
bdcbe496 1057 _cpp_pop_context (pfile);
4ed5bcfb
NB
1058 if (pfile->state.in_directive)
1059 continue;
1060 return &pfile->avoid_paste;
93c80368 1061 }
93c80368 1062
477cdac7
JT
1063 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1064 continue;
1065
4ed5bcfb 1066 if (result->type != CPP_NAME)
93c80368
NB
1067 break;
1068
4ed5bcfb
NB
1069 node = result->val.node;
1070
644eddaa
NB
1071 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1072 break;
df383483 1073
644eddaa 1074 if (!(node->flags & NODE_DISABLED))
93c80368 1075 {
644eddaa
NB
1076 if (!pfile->state.prevent_expansion
1077 && enter_macro_context (pfile, node))
bd969772 1078 {
4ed5bcfb
NB
1079 if (pfile->state.in_directive)
1080 continue;
1081 return padding_token (pfile, result);
bd969772 1082 }
93c80368 1083 }
644eddaa
NB
1084 else
1085 {
d15a58c0
NB
1086 /* Flag this token as always unexpandable. FIXME: move this
1087 to collect_args()?. */
644eddaa
NB
1088 cpp_token *t = _cpp_temp_token (pfile);
1089 t->type = result->type;
1090 t->flags = result->flags | NO_EXPAND;
1091 t->val.str = result->val.str;
1092 result = t;
1093 }
a5c3cccd 1094
644eddaa 1095 break;
93c80368 1096 }
4ed5bcfb
NB
1097
1098 return result;
93c80368
NB
1099}
1100
7065e130
NB
1101/* Returns true if we're expanding an object-like macro that was
1102 defined in a system header. Just checks the macro at the top of
1103 the stack. Used for diagnostic suppression. */
1104int
c691145a 1105cpp_sys_macro_p (pfile)
7065e130
NB
1106 cpp_reader *pfile;
1107{
644eddaa 1108 cpp_hashnode *node = pfile->context->macro;
7065e130 1109
644eddaa 1110 return node && node->value.macro && node->value.macro->syshdr;
7065e130
NB
1111}
1112
af0d16cd
NB
1113/* Read each token in, until end of the current file. Directives are
1114 transparently processed. */
93c80368 1115void
ef6e958a 1116cpp_scan_nooutput (pfile)
93c80368
NB
1117 cpp_reader *pfile;
1118{
af0d16cd
NB
1119 /* Request a CPP_EOF token at the end of this file, rather than
1120 transparently continuing with the including file. */
1121 pfile->buffer->return_at_eof = true;
1122
4ed5bcfb
NB
1123 while (cpp_get_token (pfile)->type != CPP_EOF)
1124 ;
93c80368
NB
1125}
1126
bdcbe496
NB
1127/* Step back one (or more) tokens. Can only step mack more than 1 if
1128 they are from the lexer, and not from macro expansion. */
b528a07e 1129void
bdcbe496 1130_cpp_backup_tokens (pfile, count)
93c80368 1131 cpp_reader *pfile;
bdcbe496 1132 unsigned int count;
93c80368 1133{
bdcbe496 1134 if (pfile->context->prev == NULL)
93c80368 1135 {
bdcbe496
NB
1136 pfile->lookaheads += count;
1137 while (count--)
1138 {
3293c3e3
NB
1139 pfile->cur_token--;
1140 if (pfile->cur_token == pfile->cur_run->base
1141 /* Possible with -fpreprocessed and no leading #line. */
1142 && pfile->cur_run->prev != NULL)
bdcbe496 1143 {
bdcbe496
NB
1144 pfile->cur_run = pfile->cur_run->prev;
1145 pfile->cur_token = pfile->cur_run->limit;
1146 }
1147 }
93c80368 1148 }
bdcbe496 1149 else
93c80368 1150 {
bdcbe496
NB
1151 if (count != 1)
1152 abort ();
4ed5bcfb 1153 if (pfile->context->direct_p)
82eda77e 1154 FIRST (pfile->context).token--;
4ed5bcfb 1155 else
82eda77e 1156 FIRST (pfile->context).ptoken--;
93c80368
NB
1157 }
1158}
711b8824 1159
93c80368
NB
1160/* #define directive parsing and handling. */
1161
618cdda7 1162/* Returns non-zero if a macro redefinition warning is required. */
cbc69f84
NB
1163static bool
1164warn_of_redefinition (pfile, node, macro2)
1165 cpp_reader *pfile;
93c80368
NB
1166 const cpp_hashnode *node;
1167 const cpp_macro *macro2;
1168{
1169 const cpp_macro *macro1;
1170 unsigned int i;
1171
618cdda7
NB
1172 /* Some redefinitions need to be warned about regardless. */
1173 if (node->flags & NODE_WARN)
cbc69f84 1174 return true;
93c80368 1175
618cdda7 1176 /* Redefinition of a macro is allowed if and only if the old and new
ec5c56db 1177 definitions are the same. (6.10.3 paragraph 2). */
93c80368
NB
1178 macro1 = node->value.macro;
1179
6618c5d4
NB
1180 /* Don't check count here as it can be different in valid
1181 traditional redefinitions with just whitespace differences. */
1182 if (macro1->paramc != macro2->paramc
93c80368 1183 || macro1->fun_like != macro2->fun_like
28e0f040 1184 || macro1->variadic != macro2->variadic)
cbc69f84 1185 return true;
93c80368
NB
1186
1187 /* Check parameter spellings. */
1188 for (i = 0; i < macro1->paramc; i++)
1189 if (macro1->params[i] != macro2->params[i])
cbc69f84 1190 return true;
93c80368 1191
cbc69f84
NB
1192 /* Check the replacement text or tokens. */
1193 if (CPP_OPTION (pfile, traditional))
6618c5d4 1194 return _cpp_expansions_different_trad (macro1, macro2);
cbc69f84 1195
6618c5d4
NB
1196 if (macro1->count == macro2->count)
1197 for (i = 0; i < macro1->count; i++)
1198 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1199 return true;
cbc69f84
NB
1200
1201 return false;
93c80368
NB
1202}
1203
1204/* Free the definition of hashnode H. */
711b8824
ZW
1205void
1206_cpp_free_definition (h)
1207 cpp_hashnode *h;
1208{
93c80368
NB
1209 /* Macros and assertions no longer have anything to free. */
1210 h->type = NT_VOID;
1211 /* Clear builtin flag in case of redefinition. */
644eddaa 1212 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
93c80368
NB
1213}
1214
14baae01 1215/* Save parameter NODE to the parameter list of macro MACRO. Returns
f5143c46 1216 zero on success, non-zero if the parameter is a duplicate. */
c70f6ed3
NB
1217bool
1218_cpp_save_parameter (pfile, macro, node)
93c80368
NB
1219 cpp_reader *pfile;
1220 cpp_macro *macro;
1221 cpp_hashnode *node;
1222{
93c80368
NB
1223 /* Constraint 6.10.3.6 - duplicate parameter names. */
1224 if (node->arg_index)
709e9e50 1225 {
ebef4e8c
NB
1226 cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"",
1227 NODE_NAME (node));
93c80368
NB
1228 return 1;
1229 }
709e9e50 1230
8c3b2693
NB
1231 if (BUFF_ROOM (pfile->a_buff)
1232 < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1233 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
709e9e50 1234
8c3b2693
NB
1235 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1236 node->arg_index = macro->paramc;
93c80368 1237 return 0;
711b8824
ZW
1238}
1239
8c3b2693 1240/* Check the syntax of the parameters in a MACRO definition. */
93c80368
NB
1241static int
1242parse_params (pfile, macro)
711b8824 1243 cpp_reader *pfile;
93c80368 1244 cpp_macro *macro;
711b8824 1245{
93c80368 1246 unsigned int prev_ident = 0;
711b8824 1247
93c80368 1248 for (;;)
711b8824 1249 {
345894b4 1250 const cpp_token *token = _cpp_lex_token (pfile);
711b8824 1251
345894b4 1252 switch (token->type)
711b8824 1253 {
93c80368 1254 default:
477cdac7
JT
1255 /* Allow/ignore comments in parameter lists if we are
1256 preserving comments in macro expansions. */
1257 if (token->type == CPP_COMMENT
1258 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1259 continue;
1260
ebef4e8c
NB
1261 cpp_error (pfile, DL_ERROR,
1262 "\"%s\" may not appear in macro parameter list",
345894b4 1263 cpp_token_as_text (pfile, token));
93c80368
NB
1264 return 0;
1265
711b8824 1266 case CPP_NAME:
93c80368
NB
1267 if (prev_ident)
1268 {
ebef4e8c
NB
1269 cpp_error (pfile, DL_ERROR,
1270 "macro parameters must be comma-separated");
93c80368
NB
1271 return 0;
1272 }
1273 prev_ident = 1;
1274
c70f6ed3 1275 if (_cpp_save_parameter (pfile, macro, token->val.node))
93c80368
NB
1276 return 0;
1277 continue;
711b8824 1278
93c80368
NB
1279 case CPP_CLOSE_PAREN:
1280 if (prev_ident || macro->paramc == 0)
8c3b2693 1281 return 1;
711b8824 1282
93c80368
NB
1283 /* Fall through to pick up the error. */
1284 case CPP_COMMA:
1285 if (!prev_ident)
1286 {
ebef4e8c 1287 cpp_error (pfile, DL_ERROR, "parameter name missing");
93c80368
NB
1288 return 0;
1289 }
1290 prev_ident = 0;
711b8824
ZW
1291 continue;
1292
93c80368 1293 case CPP_ELLIPSIS:
28e0f040 1294 macro->variadic = 1;
93c80368
NB
1295 if (!prev_ident)
1296 {
c70f6ed3
NB
1297 _cpp_save_parameter (pfile, macro,
1298 pfile->spec_nodes.n__VA_ARGS__);
93c80368
NB
1299 pfile->state.va_args_ok = 1;
1300 if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
ebef4e8c
NB
1301 cpp_error (pfile, DL_PEDWARN,
1302 "anonymous variadic macros were introduced in C99");
93c80368
NB
1303 }
1304 else if (CPP_OPTION (pfile, pedantic))
ebef4e8c
NB
1305 cpp_error (pfile, DL_PEDWARN,
1306 "ISO C does not permit named variadic macros");
711b8824 1307
93c80368 1308 /* We're at the end, and just expect a closing parenthesis. */
345894b4
NB
1309 token = _cpp_lex_token (pfile);
1310 if (token->type == CPP_CLOSE_PAREN)
8c3b2693 1311 return 1;
93c80368 1312 /* Fall through. */
711b8824 1313
93c80368 1314 case CPP_EOF:
ebef4e8c 1315 cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list");
93c80368 1316 return 0;
711b8824 1317 }
711b8824 1318 }
93c80368
NB
1319}
1320
14baae01 1321/* Allocate room for a token from a macro's replacement list. */
93c80368 1322static cpp_token *
14baae01 1323alloc_expansion_token (pfile, macro)
93c80368
NB
1324 cpp_reader *pfile;
1325 cpp_macro *macro;
1326{
8c3b2693
NB
1327 if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1328 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
711b8824 1329
8c3b2693 1330 return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
14baae01
NB
1331}
1332
d15a58c0
NB
1333/* Lex a token from the expansion of MACRO, but mark parameters as we
1334 find them and warn of traditional stringification. */
14baae01
NB
1335static cpp_token *
1336lex_expansion_token (pfile, macro)
1337 cpp_reader *pfile;
1338 cpp_macro *macro;
1339{
1340 cpp_token *token;
1341
1342 pfile->cur_token = alloc_expansion_token (pfile, macro);
1343 token = _cpp_lex_direct (pfile);
93c80368 1344
d15a58c0 1345 /* Is this a parameter? */
93c80368
NB
1346 if (token->type == CPP_NAME && token->val.node->arg_index)
1347 {
1348 token->type = CPP_MACRO_ARG;
6c53ebff 1349 token->val.arg_no = token->val.node->arg_index;
93c80368
NB
1350 }
1351 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1352 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1353 check_trad_stringification (pfile, macro, &token->val.str);
1354
1355 return token;
711b8824
ZW
1356}
1357
cbc69f84
NB
1358static bool
1359create_iso_definition (pfile, macro)
711b8824 1360 cpp_reader *pfile;
cbc69f84 1361 cpp_macro *macro;
711b8824 1362{
cbc69f84 1363 cpp_token *token;
14baae01 1364 const cpp_token *ctoken;
93c80368
NB
1365
1366 /* Get the first token of the expansion (or the '(' of a
1367 function-like macro). */
14baae01
NB
1368 ctoken = _cpp_lex_token (pfile);
1369
1370 if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
93c80368 1371 {
cbc69f84 1372 bool ok = parse_params (pfile, macro);
8c3b2693
NB
1373 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1374 if (!ok)
cbc69f84 1375 return false;
8c3b2693
NB
1376
1377 /* Success. Commit the parameter array. */
562a5c27 1378 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
93c80368 1379 macro->fun_like = 1;
93c80368 1380 }
14baae01 1381 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
ebef4e8c
NB
1382 cpp_error (pfile, DL_PEDWARN,
1383 "ISO C requires whitespace after the macro name");
711b8824 1384
14baae01
NB
1385 if (macro->fun_like)
1386 token = lex_expansion_token (pfile, macro);
1387 else
1388 {
1389 token = alloc_expansion_token (pfile, macro);
1390 *token = *ctoken;
1391 }
711b8824 1392
93c80368
NB
1393 for (;;)
1394 {
1395 /* Check the stringifying # constraint 6.10.3.2.1 of
1396 function-like macros when lexing the subsequent token. */
1397 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1398 {
1399 if (token->type == CPP_MACRO_ARG)
1400 {
1401 token->flags &= ~PREV_WHITE;
1402 token->flags |= STRINGIFY_ARG;
1403 token->flags |= token[-1].flags & PREV_WHITE;
1404 token[-1] = token[0];
1405 macro->count--;
1406 }
1407 /* Let assembler get away with murder. */
bdb05a7b 1408 else if (CPP_OPTION (pfile, lang) != CLK_ASM)
93c80368 1409 {
ebef4e8c
NB
1410 cpp_error (pfile, DL_ERROR,
1411 "'#' is not followed by a macro parameter");
cbc69f84 1412 return false;
93c80368
NB
1413 }
1414 }
1415
1416 if (token->type == CPP_EOF)
1417 break;
1418
1419 /* Paste operator constraint 6.10.3.3.1. */
1420 if (token->type == CPP_PASTE)
1421 {
1422 /* Token-paste ##, can appear in both object-like and
1423 function-like macros, but not at the ends. */
1424 if (--macro->count > 0)
1425 token = lex_expansion_token (pfile, macro);
1426
1427 if (macro->count == 0 || token->type == CPP_EOF)
1428 {
ebef4e8c 1429 cpp_error (pfile, DL_ERROR,
93c80368 1430 "'##' cannot appear at either end of a macro expansion");
cbc69f84 1431 return false;
93c80368 1432 }
711b8824 1433
93c80368 1434 token[-1].flags |= PASTE_LEFT;
93c80368
NB
1435 }
1436
1437 token = lex_expansion_token (pfile, macro);
1438 }
1439
601328bb 1440 macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
8c3b2693 1441
4c2b647d
NB
1442 /* Don't count the CPP_EOF. */
1443 macro->count--;
93c80368 1444
d15a58c0 1445 /* Clear whitespace on first token for warn_of_redefinition(). */
8c3b2693 1446 if (macro->count)
601328bb 1447 macro->exp.tokens[0].flags &= ~PREV_WHITE;
8c3b2693
NB
1448
1449 /* Commit the memory. */
601328bb 1450 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
8c3b2693 1451
cbc69f84
NB
1452 return true;
1453}
1454
1455/* Parse a macro and save its expansion. Returns non-zero on success. */
1456bool
1457_cpp_create_definition (pfile, node)
1458 cpp_reader *pfile;
1459 cpp_hashnode *node;
1460{
1461 cpp_macro *macro;
1462 unsigned int i;
1463 bool ok;
44ed91a1 1464
cbc69f84
NB
1465 macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1466 macro->line = pfile->directive_line;
1467 macro->params = 0;
1468 macro->paramc = 0;
1469 macro->variadic = 0;
1470 macro->count = 0;
1471 macro->fun_like = 0;
7065e130 1472 /* To suppress some diagnostics. */
47d89cf3 1473 macro->syshdr = pfile->map->sysp != 0;
7065e130 1474
cbc69f84
NB
1475 if (CPP_OPTION (pfile, traditional))
1476 ok = _cpp_create_trad_definition (pfile, macro);
1477 else
1478 {
1479 cpp_token *saved_cur_token = pfile->cur_token;
1480
1481 ok = create_iso_definition (pfile, macro);
1482
1483 /* Restore lexer position because of games lex_expansion_token()
1484 plays lexing the macro. We set the type for SEEN_EOL() in
1485 cpplib.c.
1486
1487 Longer term we should lex the whole line before coming here,
1488 and just copy the expansion. */
1489 saved_cur_token[-1].type = pfile->cur_token[-1].type;
1490 pfile->cur_token = saved_cur_token;
1491
1492 /* Stop the lexer accepting __VA_ARGS__. */
1493 pfile->state.va_args_ok = 0;
1494 }
1495
1496 /* Clear the fast argument lookup indices. */
1497 for (i = macro->paramc; i-- > 0; )
1498 macro->params[i]->arg_index = 0;
1499
1500 if (!ok)
1501 return ok;
1502
93c80368 1503 if (node->type != NT_VOID)
711b8824 1504 {
cbc69f84 1505 if (warn_of_redefinition (pfile, node, macro))
711b8824 1506 {
ebef4e8c
NB
1507 cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0,
1508 "\"%s\" redefined", NODE_NAME (node));
93c80368 1509
618cdda7 1510 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
cbc69f84
NB
1511 cpp_error_with_line (pfile, DL_PEDWARN,
1512 node->value.macro->line, 0,
1513 "this is the location of the previous definition");
711b8824 1514 }
93c80368 1515 _cpp_free_definition (node);
711b8824
ZW
1516 }
1517
1518 /* Enter definition in hash table. */
93c80368
NB
1519 node->type = NT_MACRO;
1520 node->value.macro = macro;
a28c5035 1521 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
618cdda7 1522 node->flags |= NODE_WARN;
711b8824 1523
93c80368 1524 return ok;
711b8824
ZW
1525}
1526
d15a58c0
NB
1527/* Warn if a token in STRING matches one of a function-like MACRO's
1528 parameters. */
e1aa5140 1529static void
93c80368 1530check_trad_stringification (pfile, macro, string)
e1aa5140 1531 cpp_reader *pfile;
93c80368 1532 const cpp_macro *macro;
e1aa5140
KG
1533 const cpp_string *string;
1534{
93c80368 1535 unsigned int i, len;
562a5c27 1536 const uchar *p, *q, *limit = string->text + string->len;
df383483 1537
e1aa5140
KG
1538 /* Loop over the string. */
1539 for (p = string->text; p < limit; p = q)
1540 {
e1aa5140 1541 /* Find the start of an identifier. */
61c16b10
GM
1542 while (p < limit && !is_idstart (*p))
1543 p++;
e1aa5140
KG
1544
1545 /* Find the end of the identifier. */
1546 q = p;
61c16b10
GM
1547 while (q < limit && is_idchar (*q))
1548 q++;
93c80368
NB
1549
1550 len = q - p;
1551
e1aa5140
KG
1552 /* Loop over the function macro arguments to see if the
1553 identifier inside the string matches one of them. */
93c80368
NB
1554 for (i = 0; i < macro->paramc; i++)
1555 {
1556 const cpp_hashnode *node = macro->params[i];
e1aa5140 1557
2a967f3d
NB
1558 if (NODE_LEN (node) == len
1559 && !memcmp (p, NODE_NAME (node), len))
e1aa5140 1560 {
ebef4e8c 1561 cpp_error (pfile, DL_WARNING,
f458d1d5 1562 "macro argument \"%s\" would be stringified in traditional C",
ebef4e8c 1563 NODE_NAME (node));
e1aa5140
KG
1564 break;
1565 }
1566 }
1567 }
1568}
93c80368 1569
7096171b
NB
1570/* Returns the name, arguments and expansion of a macro, in a format
1571 suitable to be read back in again, and therefore also for DWARF 2
1572 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1573 Caller is expected to generate the "#define" bit if needed. The
93c80368 1574 returned text is temporary, and automatically freed later. */
93c80368
NB
1575const unsigned char *
1576cpp_macro_definition (pfile, node)
1577 cpp_reader *pfile;
1578 const cpp_hashnode *node;
1579{
1580 unsigned int i, len;
1581 const cpp_macro *macro = node->value.macro;
1582 unsigned char *buffer;
1583
1584 if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1585 {
ebef4e8c
NB
1586 cpp_error (pfile, DL_ICE,
1587 "invalid hash type %d in cpp_macro_definition", node->type);
93c80368
NB
1588 return 0;
1589 }
1590
1591 /* Calculate length. */
8dc901de 1592 len = NODE_LEN (node) + 2; /* ' ' and NUL. */
93c80368
NB
1593 if (macro->fun_like)
1594 {
64d08263
JB
1595 len += 4; /* "()" plus possible final ".." of named
1596 varargs (we have + 1 below). */
93c80368 1597 for (i = 0; i < macro->paramc; i++)
64d08263 1598 len += NODE_LEN (macro->params[i]) + 1; /* "," */
93c80368
NB
1599 }
1600
4c2b647d 1601 for (i = 0; i < macro->count; i++)
93c80368 1602 {
601328bb 1603 cpp_token *token = &macro->exp.tokens[i];
93c80368 1604
4c2b647d 1605 if (token->type == CPP_MACRO_ARG)
a28c5035 1606 len += NODE_LEN (macro->params[token->val.arg_no - 1]);
4c2b647d
NB
1607 else
1608 len += cpp_token_len (token); /* Includes room for ' '. */
1609 if (token->flags & STRINGIFY_ARG)
1610 len++; /* "#" */
1611 if (token->flags & PASTE_LEFT)
1612 len += 3; /* " ##" */
93c80368
NB
1613 }
1614
1615 if (len > pfile->macro_buffer_len)
4b49c365 1616 {
562a5c27 1617 pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
4b49c365
AO
1618 pfile->macro_buffer_len = len;
1619 }
7096171b
NB
1620
1621 /* Fill in the buffer. Start with the macro name. */
93c80368 1622 buffer = pfile->macro_buffer;
7096171b
NB
1623 memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1624 buffer += NODE_LEN (node);
93c80368
NB
1625
1626 /* Parameter names. */
1627 if (macro->fun_like)
1628 {
1629 *buffer++ = '(';
1630 for (i = 0; i < macro->paramc; i++)
1631 {
1632 cpp_hashnode *param = macro->params[i];
1633
1634 if (param != pfile->spec_nodes.n__VA_ARGS__)
1635 {
a28c5035
NB
1636 memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1637 buffer += NODE_LEN (param);
93c80368
NB
1638 }
1639
1640 if (i + 1 < macro->paramc)
df383483
KH
1641 /* Don't emit a space after the comma here; we're trying
1642 to emit a Dwarf-friendly definition, and the Dwarf spec
1643 forbids spaces in the argument list. */
64d08263 1644 *buffer++ = ',';
28e0f040 1645 else if (macro->variadic)
93c80368
NB
1646 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1647 }
1648 *buffer++ = ')';
1649 }
1650
e37b38d7
JB
1651 /* The Dwarf spec requires a space after the macro name, even if the
1652 definition is the empty string. */
1653 *buffer++ = ' ';
1654
93c80368 1655 /* Expansion tokens. */
4c2b647d 1656 if (macro->count)
93c80368 1657 {
93c80368
NB
1658 for (i = 0; i < macro->count; i++)
1659 {
601328bb 1660 cpp_token *token = &macro->exp.tokens[i];
93c80368
NB
1661
1662 if (token->flags & PREV_WHITE)
1663 *buffer++ = ' ';
1664 if (token->flags & STRINGIFY_ARG)
1665 *buffer++ = '#';
1666
1667 if (token->type == CPP_MACRO_ARG)
1668 {
a28c5035
NB
1669 len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1670 memcpy (buffer,
1671 NODE_NAME (macro->params[token->val.arg_no - 1]), len);
93c80368
NB
1672 buffer += len;
1673 }
1674 else
1675 buffer = cpp_spell_token (pfile, token, buffer);
1676
1677 if (token->flags & PASTE_LEFT)
1678 {
1679 *buffer++ = ' ';
1680 *buffer++ = '#';
1681 *buffer++ = '#';
1682 /* Next has PREV_WHITE; see _cpp_create_definition. */
1683 }
1684 }
1685 }
1686
1687 *buffer = '\0';
1688 return pfile->macro_buffer;
1689}