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