1 /* Part of CPP library. (Macro handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000 Free Software Foundation, Inc.
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
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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! */
31 /* Stores basic information about a macro, before it is allocated. */
34 const cpp_token *first_param; /* First parameter token. */
35 const cpp_token *first; /* First expansion token. */
36 unsigned int paramlen; /* Length of parameter names. */
37 unsigned int len; /* Length of token strings. */
38 unsigned int ntokens; /* Number of tokens in expansion. */
39 short paramc; /* Number of parameters. */
43 static void dump_funlike_macro PARAMS ((cpp_reader *, cpp_hashnode *));
44 static void count_params PARAMS ((cpp_reader *, struct macro_info *));
45 static int is__va_args__ PARAMS ((cpp_reader *, const cpp_token *));
47 static int parse_define PARAMS((cpp_reader *, struct macro_info *));
48 static int check_macro_redefinition PARAMS((cpp_reader *, cpp_hashnode *hp,
49 const cpp_toklist *));
50 static const cpp_toklist * save_expansion PARAMS((cpp_reader *,
51 struct macro_info *));
52 static unsigned int find_param PARAMS ((const cpp_token *,
54 static cpp_toklist * alloc_macro PARAMS ((cpp_reader *, struct macro_info *));
56 /* These are all the tokens that can have something pasted after them.
57 Comma is included in the list only to support the GNU varargs extension
58 (where you write a ## b and a disappears if b is an empty rest argument). */
59 #define CAN_PASTE_AFTER(type) \
60 ((type) <= CPP_LAST_EQ || (type) == CPP_COLON || (type) == CPP_HASH \
61 || (type) == CPP_DEREF || (type) == CPP_DOT || (type) == CPP_NAME \
62 || (type) == CPP_INT || (type) == CPP_FLOAT || (type) == CPP_NUMBER \
63 || (type) == CPP_MACRO_ARG || (type) == CPP_PLACEMARKER || (type) == CPP_COMMA)
65 /* Scans for a given token, returning the parameter number if found,
66 or 0 if not found. Scans from FIRST to TOKEN - 1 or the first
67 CPP_CLOSE_PAREN for TOKEN. */
69 find_param (first, token)
70 const cpp_token *first, *token;
72 unsigned int param = 0;
74 for (; first < token && first->type != CPP_CLOSE_PAREN; first++)
75 if (first->type == CPP_NAME || first->type == CPP_DEFINED)
78 if (first->val.node == token->val.node)
85 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
86 replacement list of a variable-arguments macro. TOKEN is assumed
87 to be of type CPP_NAME. */
89 is__va_args__ (pfile, token)
91 const cpp_token *token;
93 if (!CPP_PEDANTIC (pfile)
94 || token->val.node != pfile->spec_nodes->n__VA_ARGS__)
97 cpp_pedwarn_with_line (pfile, token->line, token->col,
98 "\"%s\" is only valid in the replacement list of a function-like macro",
99 token->val.node->name);
103 /* Counts the parameters to a function-like macro, the length of their
104 null-terminated names, and whether the macro is a variable-argument
105 one. FIRST is the token immediately after the open parenthesis,
106 INFO stores the data.
108 On success, info->first is updated to the token after the closing
109 parenthesis, i.e. the first token of the expansion. Otherwise
110 there was an error, which has been reported. */
112 count_params (pfile, info)
114 struct macro_info *info;
116 unsigned int prev_ident = 0;
117 const cpp_token *token;
122 info->first = info->first_param; /* Not a ')' indicating success. */
124 for (token = info->first_param;; token++)
129 cpp_error_with_line (pfile, token->line, token->col,
130 "illegal token in macro parameter list");
135 cpp_error_with_line (pfile, token->line, token->col,
136 "missing ')' in macro parameter list");
140 continue; /* Ignore -C comments. */
142 case CPP_DEFINED: /* 'defined' may be used as a macro
147 cpp_error_with_line (pfile, token->line, token->col,
148 "macro parameters must be comma-separated");
152 /* Constraint 6.10.3.5 */
153 if (is__va_args__ (pfile, token))
156 /* Constraint 6.10.3.6 - duplicate parameter names. */
157 if (find_param (info->first, token))
159 cpp_error_with_line (pfile, token->line, token->col,
160 "duplicate macro parameter \"%s\"",
161 token->val.node->name);
167 info->paramlen += token->val.node->length + 1;
170 case CPP_CLOSE_PAREN:
171 if (prev_ident || info->paramc == 0)
174 /* Fall through to pick up the error. */
178 cpp_error_with_line (pfile, token->line, token->col,
179 "parameter name expected");
186 /* Convert ISO-style var_args to named varargs by changing
187 the ellipsis into an identifier with name __VA_ARGS__.
188 This simplifies other handling. */
191 cpp_token *tok = (cpp_token *) token;
193 tok->type = CPP_NAME;
194 tok->val.node = pfile->spec_nodes->n__VA_ARGS__;
197 info->paramlen += tok->val.node->length + 1;
199 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
201 "C89 does not permit anon varargs macros");
205 if (CPP_PEDANTIC (pfile))
207 "ISO C does not permit named varargs parameters");
210 info->flags |= VAR_ARGS;
212 if (token->type == CPP_CLOSE_PAREN)
218 info->first = token + 1;
219 if (!pfile->save_parameter_spellings)
225 /* Parses a #define directive. On success, returns zero, and INFO is
226 filled in appropriately. */
228 parse_define (pfile, info)
230 struct macro_info *info;
232 const cpp_token *token;
235 /* The first token after the macro's name. */
236 token = _cpp_get_token (pfile);
238 /* Constraint 6.10.3.5 */
239 if (is__va_args__ (pfile, token - 1))
242 while (token->type == CPP_COMMENT)
243 token++, prev_white = 1;
244 prev_white |= token->flags & PREV_WHITE;
246 if (token->type == CPP_OPEN_PAREN && !prev_white)
248 /* A function-like macro. */
249 info->first_param = token + 1;
250 count_params (pfile, info);
251 if (info->first[-1].type != CPP_CLOSE_PAREN)
256 /* An object-like macro. */
261 if (!prev_white && token->type != CPP_EOF)
262 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
265 /* Count tokens in expansion. We drop paste tokens, and stringize
266 tokens, so don't count them. */
267 info->ntokens = info->len = 0;
268 for (token = info->first; token->type != CPP_EOF; token++)
270 if (token->type == CPP_PASTE)
272 /* Token-paste ##, can appear in both object-like and
273 function-like macros, but not at the ends. Constraint
275 if (token == info->first || token[1].type == CPP_EOF)
277 cpp_error_with_line (pfile, token->line, token->col,
278 "'##' cannot appear at either end of a macro expansion");
283 else if (token->type == CPP_HASH)
285 /* Stringifying #, but a normal character in object-like
286 macros. Must come before a parameter name. Constraint
288 if (info->paramc >= 0)
290 if (token[1].type == CPP_NAME
291 && find_param (info->first_param, token + 1))
293 if (! CPP_OPTION (pfile, lang_asm))
295 cpp_error_with_line (pfile, token->line, token->col,
296 "'#' is not followed by a macro parameter");
301 else if (token->type == CPP_NAME)
303 /* Constraint 6.10.3.5 */
304 if (!(info->flags & VAR_ARGS) && is__va_args__ (pfile, token))
308 if (TOKEN_SPELL (token) == SPELL_STRING)
309 info->len += token->val.str.len;
315 /* Returns non-zero if a macro redefinition is trivial. */
317 check_macro_redefinition (pfile, hp, list2)
320 const cpp_toklist *list2;
322 const cpp_toklist *list1;
324 if (hp->type != T_MACRO)
325 return ! pfile->done_initializing;
327 /* Clear the whitespace and BOL flags of the first tokens. They get
328 altered during macro expansion, but is not significant here. */
329 list1 = hp->value.expansion;
330 list1->tokens[0].flags &= ~(PREV_WHITE|BOL);
331 list2->tokens[0].flags &= ~(PREV_WHITE|BOL);
333 if (!_cpp_equiv_toklists (list1, list2))
336 if (CPP_OPTION (pfile, pedantic)
338 && (list1->params_len != list2->params_len
339 || memcmp (list1->namebuf, list2->namebuf, list1->params_len)))
345 /* This is a dummy structure whose only purpose is getting alignment
350 cpp_token first_token;
353 /* Allocate space to hold the token list, its tokens, their text, and
354 the parameter names if needed. Empty expansions are stored as a
355 single placemarker token.
357 These are all allocated in a block together for performance
358 reasons. Therefore, this token list cannot be expanded like a
359 normal token list. Try to do so, and you lose. */
361 alloc_macro (pfile, info)
363 struct macro_info *info;
366 struct toklist_dummy *dummy;
369 /* Empty macros become a single placemarker token. */
370 if (info->ntokens == 0)
373 size = sizeof (struct toklist_dummy);
374 size += (info->ntokens - 1) * sizeof(cpp_token);
375 size += info->len + info->paramlen;
377 dummy = (struct toklist_dummy *) xmalloc (size);
378 list = (cpp_toklist *) dummy;
380 /* Initialize the monster. */
381 list->tokens = &dummy->first_token;
382 list->tokens_used = list->tokens_cap = info->ntokens;
384 list->namebuf = (unsigned char *) &list->tokens[info->ntokens];
385 list->name_used = list->name_cap = info->len + info->paramlen;
388 list->line = pfile->token_list.line;
389 list->file = pfile->token_list.file;
390 list->params_len = info->paramlen;
391 list->paramc = info->paramc;
392 list->flags = info->flags;
397 /* Free the definition of macro H. */
400 _cpp_free_definition (h)
403 if (h->type == T_MACRO)
404 free ((PTR) h->value.expansion);
405 h->value.expansion = NULL;
408 /* Copy the tokens of the expansion, beginning with info->first until
409 CPP_EOF. INFO contains information about the macro.
411 Change the type of macro arguments in the expansion from CPP_NAME
412 to CPP_MACRO_ARG. Remove #'s that represent stringification,
413 flagging the CPP_MACRO_ARG it operates on STRINGIFY. Remove ##'s,
414 flagging the token on its immediate left PASTE_LEFT. Returns the
415 token list for the macro expansion. */
416 static const cpp_toklist *
417 save_expansion (pfile, info)
419 struct macro_info *info;
421 const cpp_token *token;
426 list = alloc_macro (pfile, info);
429 /* Store the null-terminated parameter spellings of a macro, to
430 provide pedantic warnings to satisfy 6.10.3.2, or for use when
431 dumping macro definitions. They must go first. */
432 if (list->params_len)
433 for (token = info->first_param; token < info->first; token++)
434 if (token->type == CPP_NAME || token->type == CPP_DEFINED)
437 memcpy (buf, token->val.node->name, token->val.node->length + 1);
438 buf += token->val.node->length + 1;
442 for (token = info->first; token->type != CPP_EOF; token++)
444 unsigned int param_no;
450 if (list->paramc == -1)
453 /* Check if the name is a macro parameter. */
454 param_no = find_param (info->first_param, token);
457 dest->val.aux = param_no - 1;
459 dest->type = CPP_MACRO_ARG;
460 if (token[-1].type == CPP_HASH)
461 dest->flags = token[-1].flags | STRINGIFY_ARG;
463 dest->flags = token->flags; /* Particularly PREV_WHITE. */
464 /* Turn off PREV_WHITE if we immediately follow a paste.
465 That way, even if the paste turns out to be illegal, there
466 will be no space between the two tokens in the output. */
467 if (token[-1].type == CPP_PASTE)
468 dest->flags &= ~PREV_WHITE;
473 /* Set the paste flag on the token to our left, unless there
474 is no possible token to which it might be pasted. That
475 is critical for correct operation under some circumstances;
476 see gcc.dg/cpp/paste6.c. */
477 if (CAN_PASTE_AFTER (dest[-1].type) || (dest[-1].flags & NAMED_OP))
478 dest[-1].flags |= PASTE_LEFT;
479 else if (CPP_OPTION (pfile, warn_paste))
480 cpp_warning_with_line (pfile, dest[-1].line, dest[-1].col,
481 "nothing can be pasted after this token");
485 /* Stringifying #. Constraint 6.10.3.2.1 */
486 if (list->paramc >= 0 && token[1].type == CPP_NAME
487 && find_param (info->first_param, token + 1))
495 /* Copy the token. */
497 if (TOKEN_SPELL (token) == SPELL_STRING)
499 memcpy (buf, token->val.str.text, token->val.str.len);
500 dest->val.str.text = buf;
501 buf += dest->val.str.len;
503 if (token[-1].type == CPP_PASTE)
504 dest->flags &= ~PREV_WHITE;
508 /* Empty macros become a single placemarker token. */
509 if (dest == list->tokens)
511 dest->type = CPP_PLACEMARKER;
519 /* Parse a macro and save its expansion. Returns non-zero on success. */
521 _cpp_create_definition (pfile, hp)
525 struct macro_info info;
526 const cpp_toklist *list;
528 if (parse_define (pfile, &info))
530 list = save_expansion (pfile, &info);
532 /* Check for a redefinition. Redefinition of a macro is allowed if
533 and only if the old and new definitions are the same.
534 (6.10.3 paragraph 2). */
536 if (hp->type != T_VOID)
538 if (!check_macro_redefinition (pfile, hp, list))
540 cpp_pedwarn (pfile, "\"%s\" redefined", hp->name);
541 if (pfile->done_initializing && hp->type == T_MACRO)
542 cpp_pedwarn_with_file_and_line (pfile,
543 hp->value.expansion->file,
544 hp->value.expansion->line, 1,
545 "this is the location of the previous definition");
547 _cpp_free_definition (hp);
550 /* Enter definition in hash table. */
552 hp->value.expansion = list;
557 /* Dump the definition of macro MACRO on stdout. The format is suitable
558 to be read back in again. */
561 _cpp_dump_definition (pfile, hp)
565 CPP_RESERVE (pfile, hp->length + sizeof "#define ");
566 CPP_PUTS_Q (pfile, "#define ", sizeof "#define " - 1);
567 CPP_PUTS_Q (pfile, hp->name, hp->length);
569 if (hp->type == T_MACRO)
571 if (hp->value.expansion->paramc >= 0)
572 dump_funlike_macro (pfile, hp);
575 const cpp_toklist *list = hp->value.expansion;
576 list->tokens[0].flags &= ~BOL;
577 list->tokens[0].flags |= PREV_WHITE;
578 _cpp_dump_list (pfile, list, list->tokens, 1);
582 cpp_ice (pfile, "invalid hash type %d in dump_definition", hp->type);
584 if (CPP_BUFFER (pfile) == 0 || ! pfile->done_initializing)
585 CPP_PUTC (pfile, '\n');
589 dump_funlike_macro (pfile, node)
594 const cpp_toklist * list = node->value.expansion;
597 param = list->namebuf;
598 CPP_PUTC_Q (pfile, '(');
599 for (i = 0; i++ < list->paramc;)
603 len = ustrlen (param);
604 CPP_PUTS (pfile, param, len);
605 if (i < list->paramc)
606 CPP_PUTS(pfile, ", ", 2);
607 else if (list->flags & VAR_ARGS)
609 if (!ustrcmp (param, U"__VA_ARGS__"))
610 pfile->limit -= sizeof (U"__VA_ARGS__") - 1;
611 CPP_PUTS_Q (pfile, "...", 3);
615 CPP_PUTC (pfile, ')');
616 list->tokens[0].flags &= ~BOL;
617 list->tokens[0].flags |= PREV_WHITE;
618 _cpp_dump_list (pfile, list, list->tokens, 1);