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 count_params PARAMS ((cpp_reader *, struct macro_info *));
44 static int is__va_args__ PARAMS ((cpp_reader *, const cpp_token *));
46 static int parse_define PARAMS((cpp_reader *, struct macro_info *));
47 static int check_macro_redefinition PARAMS((cpp_reader *, cpp_hashnode *hp,
48 const cpp_toklist *));
49 static const cpp_toklist * save_expansion PARAMS((cpp_reader *,
50 struct macro_info *));
51 static unsigned int find_param PARAMS ((const cpp_token *,
53 static cpp_toklist * alloc_macro PARAMS ((cpp_reader *, struct macro_info *));
54 static void check_trad_stringification PARAMS ((cpp_reader *,
55 const struct macro_info *,
58 /* These are all the tokens that can have something pasted after them.
59 Comma is included in the list only to support the GNU varargs extension
60 (where you write a ## b and a disappears if b is an empty rest argument).
61 CPP_OTHER is included because of Objective C's use of '@'. */
62 #define CAN_PASTE_AFTER(type) \
63 ((type) <= CPP_LAST_EQ || (type) == CPP_COLON || (type) == CPP_HASH \
64 || (type) == CPP_DEREF || (type) == CPP_DOT || (type) == CPP_NAME \
65 || (type) == CPP_INT || (type) == CPP_FLOAT || (type) == CPP_NUMBER \
66 || (type) == CPP_MACRO_ARG || (type) == CPP_PLACEMARKER \
67 || (type) == CPP_COMMA || (type) == CPP_OTHER)
69 /* Scans for a given token, returning the parameter number if found,
70 or 0 if not found. Scans from FIRST to TOKEN - 1 or the first
71 CPP_CLOSE_PAREN for TOKEN. */
73 find_param (first, token)
74 const cpp_token *first, *token;
76 unsigned int param = 0;
78 for (; first < token && first->type != CPP_CLOSE_PAREN; first++)
79 if (first->type == CPP_NAME || first->type == CPP_DEFINED)
82 if (first->val.node == token->val.node)
89 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
90 replacement list of a variable-arguments macro. TOKEN is assumed
91 to be of type CPP_NAME. */
93 is__va_args__ (pfile, token)
95 const cpp_token *token;
97 if (!CPP_PEDANTIC (pfile)
98 || token->val.node != pfile->spec_nodes->n__VA_ARGS__)
101 cpp_pedwarn_with_line (pfile, token->line, token->col,
102 "\"%s\" is only valid in the replacement list of a function-like macro",
103 token->val.node->name);
107 /* Counts the parameters to a function-like macro, the length of their
108 null-terminated names, and whether the macro is a variable-argument
109 one. FIRST is the token immediately after the open parenthesis,
110 INFO stores the data.
112 On success, info->first is updated to the token after the closing
113 parenthesis, i.e. the first token of the expansion. Otherwise
114 there was an error, which has been reported. */
116 count_params (pfile, info)
118 struct macro_info *info;
120 unsigned int prev_ident = 0;
121 const cpp_token *token;
126 info->first = info->first_param; /* Not a ')' indicating success. */
128 for (token = info->first_param;; token++)
133 cpp_error_with_line (pfile, token->line, token->col,
134 "token may not appear in macro parameter list");
139 cpp_error_with_line (pfile, token->line, token->col,
140 "missing ')' in macro parameter list");
144 continue; /* Ignore -C comments. */
146 case CPP_DEFINED: /* 'defined' may be used as a macro
151 cpp_error_with_line (pfile, token->line, token->col,
152 "macro parameters must be comma-separated");
156 /* Constraint 6.10.3.5 */
157 if (is__va_args__ (pfile, token))
160 /* Constraint 6.10.3.6 - duplicate parameter names. */
161 if (find_param (info->first, token))
163 cpp_error_with_line (pfile, token->line, token->col,
164 "duplicate macro parameter \"%s\"",
165 token->val.node->name);
171 info->paramlen += token->val.node->length + 1;
174 case CPP_CLOSE_PAREN:
175 if (prev_ident || info->paramc == 0)
178 /* Fall through to pick up the error. */
182 cpp_error_with_line (pfile, token->line, token->col,
183 "parameter name expected");
190 /* Convert ISO-style var_args to named varargs by changing
191 the ellipsis into an identifier with name __VA_ARGS__.
192 This simplifies other handling. */
195 cpp_token *tok = (cpp_token *) token;
197 tok->type = CPP_NAME;
198 tok->val.node = pfile->spec_nodes->n__VA_ARGS__;
201 info->paramlen += tok->val.node->length + 1;
203 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
205 "C89 does not permit anon varargs macros");
209 if (CPP_PEDANTIC (pfile))
211 "ISO C does not permit named varargs parameters");
214 info->flags |= VAR_ARGS;
216 if (token->type == CPP_CLOSE_PAREN)
222 info->first = token + 1;
223 if (!pfile->save_parameter_spellings)
229 /* Parses a #define directive. On success, returns zero, and INFO is
230 filled in appropriately. */
232 parse_define (pfile, info)
234 struct macro_info *info;
236 const cpp_token *token;
239 /* The first token after the macro's name. */
240 token = _cpp_get_token (pfile);
242 /* Constraint 6.10.3.5 */
243 if (is__va_args__ (pfile, token - 1))
246 while (token->type == CPP_COMMENT)
247 token++, prev_white = 1;
248 prev_white |= token->flags & PREV_WHITE;
250 if (token->type == CPP_OPEN_PAREN && !prev_white)
252 /* A function-like macro. */
253 info->first_param = token + 1;
254 count_params (pfile, info);
255 if (info->first[-1].type != CPP_CLOSE_PAREN)
260 /* An object-like macro. */
265 if (!prev_white && token->type != CPP_EOF)
266 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
269 /* Count tokens in expansion. We drop paste tokens, and stringize
270 tokens, so don't count them. */
271 info->ntokens = info->len = 0;
272 for (token = info->first; token->type != CPP_EOF; token++)
274 if (token->type == CPP_PASTE)
276 /* Token-paste ##, can appear in both object-like and
277 function-like macros, but not at the ends. Constraint
279 if (token == info->first || token[1].type == CPP_EOF)
281 cpp_error_with_line (pfile, token->line, token->col,
282 "'##' cannot appear at either end of a macro expansion");
287 else if (token->type == CPP_HASH)
289 /* Stringifying #, but a normal character in object-like
290 macros. Must come before a parameter name. Constraint
292 if (info->paramc >= 0)
294 if (token[1].type == CPP_NAME
295 && find_param (info->first_param, token + 1))
297 if (! CPP_OPTION (pfile, lang_asm))
299 cpp_error_with_line (pfile, token->line, token->col,
300 "'#' is not followed by a macro parameter");
305 else if (token->type == CPP_NAME)
307 /* Constraint 6.10.3.5 */
308 if (!(info->flags & VAR_ARGS) && is__va_args__ (pfile, token))
312 if (TOKEN_SPELL (token) == SPELL_STRING)
313 info->len += token->val.str.len;
319 /* Returns non-zero if a macro redefinition is trivial. */
321 check_macro_redefinition (pfile, hp, list2)
324 const cpp_toklist *list2;
326 const cpp_toklist *list1;
328 if (hp->type != T_MACRO)
329 return ! pfile->done_initializing;
331 /* Clear the whitespace and BOL flags of the first tokens. They get
332 altered during macro expansion, but is not significant here. */
333 list1 = hp->value.expansion;
334 list1->tokens[0].flags &= ~(PREV_WHITE|BOL);
335 list2->tokens[0].flags &= ~(PREV_WHITE|BOL);
337 if (!_cpp_equiv_toklists (list1, list2))
340 if (CPP_OPTION (pfile, pedantic)
342 && (list1->params_len != list2->params_len
343 || memcmp (list1->namebuf, list2->namebuf, list1->params_len)))
349 /* This is a dummy structure whose only purpose is getting alignment
354 cpp_token first_token;
357 /* Allocate space to hold the token list, its tokens, their text, and
358 the parameter names if needed. Empty expansions are stored as a
359 single placemarker token.
361 These are all allocated in a block together for performance
362 reasons. Therefore, this token list cannot be expanded like a
363 normal token list. Try to do so, and you lose. */
365 alloc_macro (pfile, info)
367 struct macro_info *info;
370 struct toklist_dummy *dummy;
373 /* Empty macros become a single placemarker token. */
374 if (info->ntokens == 0)
377 size = sizeof (struct toklist_dummy);
378 size += (info->ntokens - 1) * sizeof(cpp_token);
379 size += info->len + info->paramlen;
381 dummy = (struct toklist_dummy *) xmalloc (size);
382 list = (cpp_toklist *) dummy;
384 /* Initialize the monster. */
385 list->tokens = &dummy->first_token;
386 list->tokens_used = list->tokens_cap = info->ntokens;
388 list->namebuf = (unsigned char *) &list->tokens[info->ntokens];
389 list->name_used = list->name_cap = info->len + info->paramlen;
392 list->line = pfile->token_list.line;
393 list->file = pfile->token_list.file;
394 list->params_len = info->paramlen;
395 list->paramc = info->paramc;
396 list->flags = info->flags;
401 /* Free the definition of macro H. */
404 _cpp_free_definition (h)
407 if (h->type == T_MACRO)
408 free ((PTR) h->value.expansion);
409 else if (h->type == T_ASSERTION)
411 struct answer *temp, *next;
413 for (temp = h->value.answers; temp; temp = next)
421 h->value.expansion = NULL;
424 /* Copy the tokens of the expansion, beginning with info->first until
425 CPP_EOF. INFO contains information about the macro.
427 Change the type of macro arguments in the expansion from CPP_NAME
428 to CPP_MACRO_ARG. Remove #'s that represent stringification,
429 flagging the CPP_MACRO_ARG it operates on STRINGIFY. Remove ##'s,
430 flagging the token on its immediate left PASTE_LEFT. Returns the
431 token list for the macro expansion. */
432 static const cpp_toklist *
433 save_expansion (pfile, info)
435 struct macro_info *info;
437 const cpp_token *token;
442 list = alloc_macro (pfile, info);
445 /* Store the null-terminated parameter spellings of a macro, to
446 provide pedantic warnings to satisfy 6.10.3.2, or for use when
447 dumping macro definitions. They must go first. */
448 if (list->params_len)
449 for (token = info->first_param; token < info->first; token++)
450 if (token->type == CPP_NAME || token->type == CPP_DEFINED)
453 memcpy (buf, token->val.node->name, token->val.node->length + 1);
454 buf += token->val.node->length + 1;
458 for (token = info->first; token->type != CPP_EOF; token++)
460 unsigned int param_no;
466 if (list->paramc == -1)
469 /* Check if the name is a macro parameter. */
470 param_no = find_param (info->first_param, token);
473 dest->val.aux = param_no - 1;
475 dest->type = CPP_MACRO_ARG;
476 if (token[-1].type == CPP_HASH)
477 dest->flags = token[-1].flags | STRINGIFY_ARG;
479 dest->flags = token->flags; /* Particularly PREV_WHITE. */
480 /* Turn off PREV_WHITE if we immediately follow a paste.
481 That way, even if the paste turns out to be invalid, there
482 will be no space between the two tokens in the output. */
483 if (token[-1].type == CPP_PASTE)
484 dest->flags &= ~PREV_WHITE;
489 /* Set the paste flag on the token to our left, unless there
490 is no possible token to which it might be pasted. That
491 is critical for correct operation under some circumstances;
492 see gcc.dg/cpp/paste6.c. */
493 if (CAN_PASTE_AFTER (dest[-1].type) || (dest[-1].flags & NAMED_OP))
494 dest[-1].flags |= PASTE_LEFT;
495 else if (CPP_OPTION (pfile, warn_paste))
496 cpp_warning_with_line (pfile, dest[-1].line, dest[-1].col,
497 "nothing can be pasted after this token");
501 /* Stringifying #. Constraint 6.10.3.2.1 */
502 if (list->paramc >= 0 && token[1].type == CPP_NAME
503 && find_param (info->first_param, token + 1))
509 if (CPP_WTRADITIONAL (pfile) && list->paramc > 0)
510 check_trad_stringification (pfile, info, &token->val.str);
517 /* Copy the token. */
519 if (TOKEN_SPELL (token) == SPELL_STRING)
521 memcpy (buf, token->val.str.text, token->val.str.len);
522 dest->val.str.text = buf;
523 buf += dest->val.str.len;
525 if (token[-1].type == CPP_PASTE)
526 dest->flags &= ~PREV_WHITE;
530 /* Empty macros become a single placemarker token. */
531 if (dest == list->tokens)
533 dest->type = CPP_PLACEMARKER;
541 /* Parse a macro and save its expansion. Returns non-zero on success. */
543 _cpp_create_definition (pfile, hp)
547 struct macro_info info;
548 const cpp_toklist *list;
550 if (parse_define (pfile, &info))
552 list = save_expansion (pfile, &info);
554 /* Check for a redefinition. Redefinition of a macro is allowed if
555 and only if the old and new definitions are the same.
556 (6.10.3 paragraph 2). */
558 if (hp->type != T_VOID)
560 if (!check_macro_redefinition (pfile, hp, list))
562 cpp_pedwarn (pfile, "\"%s\" redefined", hp->name);
563 if (pfile->done_initializing && hp->type == T_MACRO)
564 cpp_pedwarn_with_file_and_line (pfile,
565 hp->value.expansion->file,
566 hp->value.expansion->line, 1,
567 "this is the location of the previous definition");
569 _cpp_free_definition (hp);
572 /* Enter definition in hash table. */
574 hp->value.expansion = list;
579 /* Warn if a token in `string' matches one of the function macro
580 arguments in `info'. This function assumes that the macro is a
581 function macro and not an object macro. */
583 check_trad_stringification (pfile, info, string)
585 const struct macro_info *info;
586 const cpp_string *string;
588 const U_CHAR *p, *q, *limit = string->text + string->len;
590 /* Loop over the string. */
591 for (p = string->text; p < limit; p = q)
593 const cpp_token *token;
595 /* Find the start of an identifier. */
596 while (p < limit && !is_idstart (*p))
599 /* Find the end of the identifier. */
601 while (q < limit && is_idchar (*q))
604 /* Loop over the function macro arguments to see if the
605 identifier inside the string matches one of them. */
606 for (token = info->first_param; token < info->first; token++)
608 /* Skip the commas in between the arguments. */
609 if (token->type != CPP_NAME)
612 if (token->val.node->length == (q - p)
613 && !memcmp (p, token->val.node->name, (q - p)))
616 "macro arg \"%.*s\" would be stringified with -traditional.",