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_macro_args PARAMS ((FILE *, const cpp_toklist *));
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 CPP_OTHER is included because of Objective C's use of '@'. */
60 #define CAN_PASTE_AFTER(type) \
61 ((type) <= CPP_LAST_EQ || (type) == CPP_COLON || (type) == CPP_HASH \
62 || (type) == CPP_DEREF || (type) == CPP_DOT || (type) == CPP_NAME \
63 || (type) == CPP_INT || (type) == CPP_FLOAT || (type) == CPP_NUMBER \
64 || (type) == CPP_MACRO_ARG || (type) == CPP_PLACEMARKER \
65 || (type) == CPP_COMMA || (type) == CPP_OTHER)
67 /* Scans for a given token, returning the parameter number if found,
68 or 0 if not found. Scans from FIRST to TOKEN - 1 or the first
69 CPP_CLOSE_PAREN for TOKEN. */
71 find_param (first, token)
72 const cpp_token *first, *token;
74 unsigned int param = 0;
76 for (; first < token && first->type != CPP_CLOSE_PAREN; first++)
77 if (first->type == CPP_NAME || first->type == CPP_DEFINED)
80 if (first->val.node == token->val.node)
87 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
88 replacement list of a variable-arguments macro. TOKEN is assumed
89 to be of type CPP_NAME. */
91 is__va_args__ (pfile, token)
93 const cpp_token *token;
95 if (!CPP_PEDANTIC (pfile)
96 || token->val.node != pfile->spec_nodes->n__VA_ARGS__)
99 cpp_pedwarn_with_line (pfile, token->line, token->col,
100 "\"%s\" is only valid in the replacement list of a function-like macro",
101 token->val.node->name);
105 /* Counts the parameters to a function-like macro, the length of their
106 null-terminated names, and whether the macro is a variable-argument
107 one. FIRST is the token immediately after the open parenthesis,
108 INFO stores the data.
110 On success, info->first is updated to the token after the closing
111 parenthesis, i.e. the first token of the expansion. Otherwise
112 there was an error, which has been reported. */
114 count_params (pfile, info)
116 struct macro_info *info;
118 unsigned int prev_ident = 0;
119 const cpp_token *token;
124 info->first = info->first_param; /* Not a ')' indicating success. */
126 for (token = info->first_param;; token++)
131 cpp_error_with_line (pfile, token->line, token->col,
132 "token may not appear in macro parameter list");
137 cpp_error_with_line (pfile, token->line, token->col,
138 "missing ')' in macro parameter list");
142 continue; /* Ignore -C comments. */
144 case CPP_DEFINED: /* 'defined' may be used as a macro
149 cpp_error_with_line (pfile, token->line, token->col,
150 "macro parameters must be comma-separated");
154 /* Constraint 6.10.3.5 */
155 if (is__va_args__ (pfile, token))
158 /* Constraint 6.10.3.6 - duplicate parameter names. */
159 if (find_param (info->first, token))
161 cpp_error_with_line (pfile, token->line, token->col,
162 "duplicate macro parameter \"%s\"",
163 token->val.node->name);
169 info->paramlen += token->val.node->length + 1;
172 case CPP_CLOSE_PAREN:
173 if (prev_ident || info->paramc == 0)
176 /* Fall through to pick up the error. */
180 cpp_error_with_line (pfile, token->line, token->col,
181 "parameter name expected");
188 /* Convert ISO-style var_args to named varargs by changing
189 the ellipsis into an identifier with name __VA_ARGS__.
190 This simplifies other handling. */
193 cpp_token *tok = (cpp_token *) token;
195 tok->type = CPP_NAME;
196 tok->val.node = pfile->spec_nodes->n__VA_ARGS__;
199 info->paramlen += tok->val.node->length + 1;
201 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
203 "C89 does not permit anon varargs macros");
207 if (CPP_PEDANTIC (pfile))
209 "ISO C does not permit named varargs parameters");
212 info->flags |= VAR_ARGS;
214 if (token->type == CPP_CLOSE_PAREN)
220 info->first = token + 1;
221 if (!pfile->save_parameter_spellings)
227 /* Parses a #define directive. On success, returns zero, and INFO is
228 filled in appropriately. */
230 parse_define (pfile, info)
232 struct macro_info *info;
234 const cpp_token *token;
237 /* The first token after the macro's name. */
238 token = _cpp_get_token (pfile);
240 /* Constraint 6.10.3.5 */
241 if (is__va_args__ (pfile, token - 1))
244 while (token->type == CPP_COMMENT)
245 token++, prev_white = 1;
246 prev_white |= token->flags & PREV_WHITE;
248 if (token->type == CPP_OPEN_PAREN && !prev_white)
250 /* A function-like macro. */
251 info->first_param = token + 1;
252 count_params (pfile, info);
253 if (info->first[-1].type != CPP_CLOSE_PAREN)
258 /* An object-like macro. */
263 if (!prev_white && token->type != CPP_EOF)
264 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
267 /* Count tokens in expansion. We drop paste tokens, and stringize
268 tokens, so don't count them. */
269 info->ntokens = info->len = 0;
270 for (token = info->first; token->type != CPP_EOF; token++)
272 if (token->type == CPP_PASTE)
274 /* Token-paste ##, can appear in both object-like and
275 function-like macros, but not at the ends. Constraint
277 if (token == info->first || token[1].type == CPP_EOF)
279 cpp_error_with_line (pfile, token->line, token->col,
280 "'##' cannot appear at either end of a macro expansion");
285 else if (token->type == CPP_HASH)
287 /* Stringifying #, but a normal character in object-like
288 macros. Must come before a parameter name. Constraint
290 if (info->paramc >= 0)
292 if (token[1].type == CPP_NAME
293 && find_param (info->first_param, token + 1))
295 if (! CPP_OPTION (pfile, lang_asm))
297 cpp_error_with_line (pfile, token->line, token->col,
298 "'#' is not followed by a macro parameter");
303 else if (token->type == CPP_NAME)
305 /* Constraint 6.10.3.5 */
306 if (!(info->flags & VAR_ARGS) && is__va_args__ (pfile, token))
310 if (TOKEN_SPELL (token) == SPELL_STRING)
311 info->len += token->val.str.len;
317 /* Returns non-zero if a macro redefinition is trivial. */
319 check_macro_redefinition (pfile, hp, list2)
322 const cpp_toklist *list2;
324 const cpp_toklist *list1;
326 if (hp->type != T_MACRO)
327 return ! pfile->done_initializing;
329 /* Clear the whitespace and BOL flags of the first tokens. They get
330 altered during macro expansion, but is not significant here. */
331 list1 = hp->value.expansion;
332 list1->tokens[0].flags &= ~(PREV_WHITE|BOL);
333 list2->tokens[0].flags &= ~(PREV_WHITE|BOL);
335 if (!_cpp_equiv_toklists (list1, list2))
338 if (CPP_OPTION (pfile, pedantic)
340 && (list1->params_len != list2->params_len
341 || memcmp (list1->namebuf, list2->namebuf, list1->params_len)))
347 /* This is a dummy structure whose only purpose is getting alignment
352 cpp_token first_token;
355 /* Allocate space to hold the token list, its tokens, their text, and
356 the parameter names if needed. Empty expansions are stored as a
357 single placemarker token.
359 These are all allocated in a block together for performance
360 reasons. Therefore, this token list cannot be expanded like a
361 normal token list. Try to do so, and you lose. */
363 alloc_macro (pfile, info)
365 struct macro_info *info;
368 struct toklist_dummy *dummy;
371 /* Empty macros become a single placemarker token. */
372 if (info->ntokens == 0)
375 size = sizeof (struct toklist_dummy);
376 size += (info->ntokens - 1) * sizeof(cpp_token);
377 size += info->len + info->paramlen;
379 dummy = (struct toklist_dummy *) xmalloc (size);
380 list = (cpp_toklist *) dummy;
382 /* Initialize the monster. */
383 list->tokens = &dummy->first_token;
384 list->tokens_used = list->tokens_cap = info->ntokens;
386 list->namebuf = (unsigned char *) &list->tokens[info->ntokens];
387 list->name_used = list->name_cap = info->len + info->paramlen;
390 list->line = pfile->token_list.line;
391 list->file = pfile->token_list.file;
392 list->params_len = info->paramlen;
393 list->paramc = info->paramc;
394 list->flags = info->flags;
399 /* Free the definition of macro H. */
402 _cpp_free_definition (h)
405 if (h->type == T_MACRO)
406 free ((PTR) h->value.expansion);
407 else if (h->type == T_ASSERTION)
409 struct answer *temp, *next;
411 for (temp = h->value.answers; temp; temp = next)
419 h->value.expansion = NULL;
422 /* Copy the tokens of the expansion, beginning with info->first until
423 CPP_EOF. INFO contains information about the macro.
425 Change the type of macro arguments in the expansion from CPP_NAME
426 to CPP_MACRO_ARG. Remove #'s that represent stringification,
427 flagging the CPP_MACRO_ARG it operates on STRINGIFY. Remove ##'s,
428 flagging the token on its immediate left PASTE_LEFT. Returns the
429 token list for the macro expansion. */
430 static const cpp_toklist *
431 save_expansion (pfile, info)
433 struct macro_info *info;
435 const cpp_token *token;
440 list = alloc_macro (pfile, info);
443 /* Store the null-terminated parameter spellings of a macro, to
444 provide pedantic warnings to satisfy 6.10.3.2, or for use when
445 dumping macro definitions. They must go first. */
446 if (list->params_len)
447 for (token = info->first_param; token < info->first; token++)
448 if (token->type == CPP_NAME || token->type == CPP_DEFINED)
451 memcpy (buf, token->val.node->name, token->val.node->length + 1);
452 buf += token->val.node->length + 1;
456 for (token = info->first; token->type != CPP_EOF; token++)
458 unsigned int param_no;
464 if (list->paramc == -1)
467 /* Check if the name is a macro parameter. */
468 param_no = find_param (info->first_param, token);
471 dest->val.aux = param_no - 1;
473 dest->type = CPP_MACRO_ARG;
474 if (token[-1].type == CPP_HASH)
475 dest->flags = token[-1].flags | STRINGIFY_ARG;
477 dest->flags = token->flags; /* Particularly PREV_WHITE. */
478 /* Turn off PREV_WHITE if we immediately follow a paste.
479 That way, even if the paste turns out to be invalid, there
480 will be no space between the two tokens in the output. */
481 if (token[-1].type == CPP_PASTE)
482 dest->flags &= ~PREV_WHITE;
487 /* Set the paste flag on the token to our left, unless there
488 is no possible token to which it might be pasted. That
489 is critical for correct operation under some circumstances;
490 see gcc.dg/cpp/paste6.c. */
491 if (CAN_PASTE_AFTER (dest[-1].type) || (dest[-1].flags & NAMED_OP))
492 dest[-1].flags |= PASTE_LEFT;
493 else if (CPP_OPTION (pfile, warn_paste))
494 cpp_warning_with_line (pfile, dest[-1].line, dest[-1].col,
495 "nothing can be pasted after this token");
499 /* Stringifying #. Constraint 6.10.3.2.1 */
500 if (list->paramc >= 0 && token[1].type == CPP_NAME
501 && find_param (info->first_param, token + 1))
509 /* Copy the token. */
511 if (TOKEN_SPELL (token) == SPELL_STRING)
513 memcpy (buf, token->val.str.text, token->val.str.len);
514 dest->val.str.text = buf;
515 buf += dest->val.str.len;
517 if (token[-1].type == CPP_PASTE)
518 dest->flags &= ~PREV_WHITE;
522 /* Empty macros become a single placemarker token. */
523 if (dest == list->tokens)
525 dest->type = CPP_PLACEMARKER;
533 /* Parse a macro and save its expansion. Returns non-zero on success. */
535 _cpp_create_definition (pfile, hp)
539 struct macro_info info;
540 const cpp_toklist *list;
542 if (parse_define (pfile, &info))
544 list = save_expansion (pfile, &info);
546 /* Check for a redefinition. Redefinition of a macro is allowed if
547 and only if the old and new definitions are the same.
548 (6.10.3 paragraph 2). */
550 if (hp->type != T_VOID)
552 if (!check_macro_redefinition (pfile, hp, list))
554 cpp_pedwarn (pfile, "\"%s\" redefined", hp->name);
555 if (pfile->done_initializing && hp->type == T_MACRO)
556 cpp_pedwarn_with_file_and_line (pfile,
557 hp->value.expansion->file,
558 hp->value.expansion->line, 1,
559 "this is the location of the previous definition");
561 _cpp_free_definition (hp);
564 /* Enter definition in hash table. */
566 hp->value.expansion = list;
571 /* Dump the definition of macro MACRO on FP. The format is suitable
572 to be read back in again. Caller is expected to generate the
573 "#define NAME" bit. */
576 cpp_dump_definition (pfile, fp, hp)
579 const cpp_hashnode *hp;
581 const cpp_toklist *list = hp->value.expansion;
583 if (hp->type != T_MACRO)
585 cpp_ice (pfile, "invalid hash type %d in dump_definition", hp->type);
589 if (list->paramc >= 0)
590 dump_macro_args (fp, list);
593 cpp_output_list (pfile, fp, list, list->tokens);
597 dump_macro_args (fp, list)
599 const cpp_toklist *list;
602 const U_CHAR *param = list->namebuf;
605 for (i = 0; i++ < list->paramc;)
609 len = ustrlen (param);
610 if (!(list->flags & VAR_ARGS) || ustrcmp (param, U"__VA_ARGS__"))
612 if (i < list->paramc)
614 else if (list->flags & VAR_ARGS)