Only create gcc/configargs.h if gcc build directory is present
[official-gcc.git] / gcc / cppmacro.c
blob321b318ea16a9ec2ea0e72c4e9a90f8d32f0d961
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
11 later version.
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! */
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
31 /* Stores basic information about a macro, before it is allocated. */
32 struct macro_info
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. */
40 unsigned char flags;
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 *,
52 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 *,
56 const cpp_string *));
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. */
72 static unsigned int
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)
81 param++;
82 if (first->val.node == token->val.node)
83 return param;
86 return 0;
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. */
92 static int
93 is__va_args__ (pfile, token)
94 cpp_reader *pfile;
95 const cpp_token *token;
97 if (!CPP_PEDANTIC (pfile)
98 || token->val.node != pfile->spec_nodes->n__VA_ARGS__)
99 return 0;
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);
104 return 1;
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. */
115 static void
116 count_params (pfile, info)
117 cpp_reader *pfile;
118 struct macro_info *info;
120 unsigned int prev_ident = 0;
121 const cpp_token *token;
123 info->paramc = 0;
124 info->paramlen = 0;
125 info->flags = 0;
126 info->first = info->first_param; /* Not a ')' indicating success. */
128 for (token = info->first_param;; token++)
130 switch (token->type)
132 default:
133 cpp_error_with_line (pfile, token->line, token->col,
134 "token may not appear in macro parameter list");
135 return;
137 case CPP_EOF:
138 missing_paren:
139 cpp_error_with_line (pfile, token->line, token->col,
140 "missing ')' in macro parameter list");
141 return;
143 case CPP_COMMENT:
144 continue; /* Ignore -C comments. */
146 case CPP_DEFINED: /* 'defined' may be used as a macro
147 parameter name. */
148 case CPP_NAME:
149 if (prev_ident)
151 cpp_error_with_line (pfile, token->line, token->col,
152 "macro parameters must be comma-separated");
153 return;
156 /* Constraint 6.10.3.5 */
157 if (is__va_args__ (pfile, token))
158 return;
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);
166 return;
169 prev_ident = 1;
170 info->paramc++;
171 info->paramlen += token->val.node->length + 1;
172 continue;
174 case CPP_CLOSE_PAREN:
175 if (prev_ident || info->paramc == 0)
176 break;
178 /* Fall through to pick up the error. */
179 case CPP_COMMA:
180 if (!prev_ident)
182 cpp_error_with_line (pfile, token->line, token->col,
183 "parameter name expected");
184 return;
186 prev_ident = 0;
187 continue;
189 case CPP_ELLIPSIS:
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. */
193 if (!prev_ident)
195 cpp_token *tok = (cpp_token *) token;
197 tok->type = CPP_NAME;
198 tok->val.node = pfile->spec_nodes->n__VA_ARGS__;
200 info->paramc++;
201 info->paramlen += tok->val.node->length + 1;
203 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
204 cpp_pedwarn (pfile,
205 "C89 does not permit anon varargs macros");
207 else
209 if (CPP_PEDANTIC (pfile))
210 cpp_pedwarn (pfile,
211 "ISO C does not permit named varargs parameters");
214 info->flags |= VAR_ARGS;
215 token++;
216 if (token->type == CPP_CLOSE_PAREN)
217 break;
218 goto missing_paren;
221 /* Success. */
222 info->first = token + 1;
223 if (!pfile->save_parameter_spellings)
224 info->paramlen = 0;
225 return;
229 /* Parses a #define directive. On success, returns zero, and INFO is
230 filled in appropriately. */
231 static int
232 parse_define (pfile, info)
233 cpp_reader *pfile;
234 struct macro_info *info;
236 const cpp_token *token;
237 int prev_white = 0;
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))
244 return 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)
256 return 1;
258 else
260 /* An object-like macro. */
261 info->paramc = -1;
262 info->paramlen = 0;
263 info->flags = 0;
264 info->first = token;
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
278 6.10.3.3.1 */
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");
283 return 1;
285 continue;
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
291 6.10.3.2.1. */
292 if (info->paramc >= 0)
294 if (token[1].type == CPP_NAME
295 && find_param (info->first_param, token + 1))
296 continue;
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");
301 return 1;
305 else if (token->type == CPP_NAME)
307 /* Constraint 6.10.3.5 */
308 if (!(info->flags & VAR_ARGS) && is__va_args__ (pfile, token))
309 return 1;
311 info->ntokens++;
312 if (TOKEN_SPELL (token) == SPELL_STRING)
313 info->len += token->val.str.len;
316 return 0;
319 /* Returns non-zero if a macro redefinition is trivial. */
320 static int
321 check_macro_redefinition (pfile, hp, list2)
322 cpp_reader *pfile;
323 cpp_hashnode *hp;
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))
338 return 0;
340 if (CPP_OPTION (pfile, pedantic)
341 && list1->paramc > 0
342 && (list1->params_len != list2->params_len
343 || memcmp (list1->namebuf, list2->namebuf, list1->params_len)))
344 return 0;
346 return 1;
349 /* This is a dummy structure whose only purpose is getting alignment
350 correct. */
351 struct toklist_dummy
353 cpp_toklist list;
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. */
364 static cpp_toklist *
365 alloc_macro (pfile, info)
366 cpp_reader *pfile;
367 struct macro_info *info;
369 unsigned int size;
370 struct toklist_dummy *dummy;
371 cpp_toklist *list;
373 /* Empty macros become a single placemarker token. */
374 if (info->ntokens == 0)
375 info->ntokens = 1;
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;
391 list->directive = 0;
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;
398 return list;
401 /* Free the definition of macro H. */
403 void
404 _cpp_free_definition (h)
405 cpp_hashnode *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)
415 next = temp->next;
416 FREE_ANSWER (temp);
420 h->type = T_VOID;
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)
434 cpp_reader *pfile;
435 struct macro_info *info;
437 const cpp_token *token;
438 cpp_toklist *list;
439 cpp_token *dest;
440 unsigned char *buf;
442 list = alloc_macro (pfile, info);
443 buf = list->namebuf;
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)
452 /* Copy null too. */
453 memcpy (buf, token->val.node->name, token->val.node->length + 1);
454 buf += token->val.node->length + 1;
457 dest = list->tokens;
458 for (token = info->first; token->type != CPP_EOF; token++)
460 unsigned int param_no;
462 switch (token->type)
464 case CPP_DEFINED:
465 case CPP_NAME:
466 if (list->paramc == -1)
467 break;
469 /* Check if the name is a macro parameter. */
470 param_no = find_param (info->first_param, token);
471 if (param_no == 0)
472 break;
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;
478 else
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;
485 dest++;
486 continue;
488 case CPP_PASTE:
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");
498 continue;
500 case CPP_HASH:
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))
504 continue;
505 break;
507 case CPP_STRING:
508 case CPP_CHAR:
509 if (CPP_WTRADITIONAL (pfile) && list->paramc > 0)
510 check_trad_stringification (pfile, info, &token->val.str);
511 break;
513 default:
514 break;
517 /* Copy the token. */
518 *dest = *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;
527 dest++;
530 /* Empty macros become a single placemarker token. */
531 if (dest == list->tokens)
533 dest->type = CPP_PLACEMARKER;
534 dest->flags = 0;
535 dest->val.aux = 0;
538 return list;
541 /* Parse a macro and save its expansion. Returns non-zero on success. */
543 _cpp_create_definition (pfile, hp)
544 cpp_reader *pfile;
545 cpp_hashnode *hp;
547 struct macro_info info;
548 const cpp_toklist *list;
550 if (parse_define (pfile, &info))
551 return 0;
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. */
573 hp->type = T_MACRO;
574 hp->value.expansion = list;
576 return 1;
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. */
582 static void
583 check_trad_stringification (pfile, info, string)
584 cpp_reader *pfile;
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))
597 p++;
599 /* Find the end of the identifier. */
600 q = p;
601 while (q < limit && is_idchar (*q))
602 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)
610 continue;
612 if (token->val.node->length == (q - p)
613 && !memcmp (p, token->val.node->name, (q - p)))
615 cpp_warning (pfile,
616 "macro arg \"%.*s\" would be stringified with -traditional.",
617 (int) (q - p), p);
618 break;