* cpplib.c (_cpp_check_directive): Issue -Wtraditional
[official-gcc.git] / gcc / cppmacro.c
blob03a9a99659da3214653ec55036c62cb115f3d46f
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 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 *,
53 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. */
68 static unsigned int
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)
77 param++;
78 if (first->val.node == token->val.node)
79 return param;
82 return 0;
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. */
88 static int
89 is__va_args__ (pfile, token)
90 cpp_reader *pfile;
91 const cpp_token *token;
93 if (!CPP_PEDANTIC (pfile)
94 || token->val.node != pfile->spec_nodes->n__VA_ARGS__)
95 return 0;
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);
100 return 1;
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. */
111 static void
112 count_params (pfile, info)
113 cpp_reader *pfile;
114 struct macro_info *info;
116 unsigned int prev_ident = 0;
117 const cpp_token *token;
119 info->paramc = 0;
120 info->paramlen = 0;
121 info->flags = 0;
122 info->first = info->first_param; /* Not a ')' indicating success. */
124 for (token = info->first_param;; token++)
126 switch (token->type)
128 default:
129 cpp_error_with_line (pfile, token->line, token->col,
130 "illegal token in macro parameter list");
131 return;
133 case CPP_EOF:
134 missing_paren:
135 cpp_error_with_line (pfile, token->line, token->col,
136 "missing ')' in macro parameter list");
137 return;
139 case CPP_COMMENT:
140 continue; /* Ignore -C comments. */
142 case CPP_NAME:
143 if (prev_ident)
145 cpp_error_with_line (pfile, token->line, token->col,
146 "macro parameters must be comma-separated");
147 return;
150 /* Constraint 6.10.3.5 */
151 if (is__va_args__ (pfile, token))
152 return;
154 /* Constraint 6.10.3.6 - duplicate parameter names. */
155 if (find_param (info->first, token))
157 cpp_error_with_line (pfile, token->line, token->col,
158 "duplicate macro parameter \"%s\"",
159 token->val.node->name);
160 return;
163 prev_ident = 1;
164 info->paramc++;
165 info->paramlen += token->val.node->length + 1;
166 continue;
168 case CPP_CLOSE_PAREN:
169 if (prev_ident || info->paramc == 0)
170 break;
172 /* Fall through to pick up the error. */
173 case CPP_COMMA:
174 if (!prev_ident)
176 cpp_error_with_line (pfile, token->line, token->col,
177 "parameter name expected");
178 return;
180 prev_ident = 0;
181 continue;
183 case CPP_ELLIPSIS:
184 /* Convert ISO-style var_args to named varargs by changing
185 the ellipsis into an identifier with name __VA_ARGS__.
186 This simplifies other handling. */
187 if (!prev_ident)
189 cpp_token *tok = (cpp_token *) token;
191 tok->type = CPP_NAME;
192 tok->val.node = pfile->spec_nodes->n__VA_ARGS__;
194 info->paramc++;
195 info->paramlen += tok->val.node->length + 1;
197 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
198 cpp_pedwarn (pfile,
199 "C89 does not permit anon varargs macros");
201 else
203 if (CPP_PEDANTIC (pfile))
204 cpp_pedwarn (pfile,
205 "ISO C does not permit named varargs parameters");
208 info->flags |= VAR_ARGS;
209 token++;
210 if (token->type == CPP_CLOSE_PAREN)
211 break;
212 goto missing_paren;
215 /* Success. */
216 info->first = token + 1;
217 if (!pfile->save_parameter_spellings)
218 info->paramlen = 0;
219 return;
223 /* Parses a #define directive. On success, returns zero, and INFO is
224 filled in appropriately. */
225 static int
226 parse_define (pfile, info)
227 cpp_reader *pfile;
228 struct macro_info *info;
230 const cpp_token *token;
231 int prev_white = 0;
233 /* The first token after the macro's name. */
234 token = _cpp_get_token (pfile);
236 /* Constraint 6.10.3.5 */
237 if (is__va_args__ (pfile, token - 1))
238 return 1;
240 while (token->type == CPP_COMMENT)
241 token++, prev_white = 1;
242 prev_white |= token->flags & PREV_WHITE;
244 if (token->type == CPP_OPEN_PAREN && !prev_white)
246 /* A function-like macro. */
247 info->first_param = token + 1;
248 count_params (pfile, info);
249 if (info->first[-1].type != CPP_CLOSE_PAREN)
250 return 1;
252 else
254 /* An object-like macro. */
255 info->paramc = -1;
256 info->paramlen = 0;
257 info->flags = 0;
258 info->first = token;
259 if (!prev_white && token->type != CPP_EOF)
260 cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
263 /* Count tokens in expansion. We drop paste tokens, and stringize
264 tokens, so don't count them. */
265 info->ntokens = info->len = 0;
266 for (token = info->first; token->type != CPP_EOF; token++)
268 if (token->type == CPP_PASTE)
270 /* Token-paste ##, can appear in both object-like and
271 function-like macros, but not at the ends. Constraint
272 6.10.3.3.1 */
273 if (token == info->first || token[1].type == CPP_EOF)
275 cpp_error_with_line (pfile, token->line, token->col,
276 "'##' cannot appear at either end of a macro expansion");
277 return 1;
279 continue;
281 else if (token->type == CPP_HASH)
283 /* Stringifying #, but a normal character in object-like
284 macros. Must come before a parameter name. Constraint
285 6.10.3.2.1. */
286 if (info->paramc >= 0)
288 if (token[1].type == CPP_NAME
289 && find_param (info->first_param, token + 1))
290 continue;
291 if (! CPP_OPTION (pfile, lang_asm))
293 cpp_error_with_line (pfile, token->line, token->col,
294 "'#' is not followed by a macro parameter");
295 return 1;
299 else if (token->type == CPP_NAME)
301 /* Constraint 6.10.3.5 */
302 if (!(info->flags & VAR_ARGS) && is__va_args__ (pfile, token))
303 return 1;
305 info->ntokens++;
306 if (TOKEN_SPELL (token) == SPELL_STRING)
307 info->len += token->val.str.len;
310 return 0;
313 /* Returns non-zero if a macro redefinition is trivial. */
314 static int
315 check_macro_redefinition (pfile, hp, list2)
316 cpp_reader *pfile;
317 cpp_hashnode *hp;
318 const cpp_toklist *list2;
320 const cpp_toklist *list1;
322 if (hp->type != T_MACRO)
323 return ! pfile->done_initializing;
325 /* Clear the whitespace and BOL flags of the first tokens. They get
326 altered during macro expansion, but is not significant here. */
327 list1 = hp->value.expansion;
328 list1->tokens[0].flags &= ~(PREV_WHITE|BOL);
329 list2->tokens[0].flags &= ~(PREV_WHITE|BOL);
331 if (!_cpp_equiv_toklists (list1, list2))
332 return 0;
334 if (CPP_OPTION (pfile, pedantic)
335 && list1->paramc > 0
336 && (list1->params_len != list2->params_len
337 || memcmp (list1->namebuf, list2->namebuf, list1->params_len)))
338 return 0;
340 return 1;
343 /* This is a dummy structure whose only purpose is getting alignment
344 correct. */
345 struct toklist_dummy
347 cpp_toklist list;
348 cpp_token first_token;
351 /* Allocate space to hold the token list, its tokens, their text, and
352 the parameter names if needed. Empty expansions are stored as a
353 single placemarker token.
355 These are all allocated in a block together for performance
356 reasons. Therefore, this token list cannot be expanded like a
357 normal token list. Try to do so, and you lose. */
358 static cpp_toklist *
359 alloc_macro (pfile, info)
360 cpp_reader *pfile;
361 struct macro_info *info;
363 unsigned int size;
364 struct toklist_dummy *dummy;
365 cpp_toklist *list;
367 /* Empty macros become a single placemarker token. */
368 if (info->ntokens == 0)
369 info->ntokens = 1;
371 size = sizeof (struct toklist_dummy);
372 size += (info->ntokens - 1) * sizeof(cpp_token);
373 size += info->len + info->paramlen;
375 dummy = (struct toklist_dummy *) xmalloc (size);
376 list = (cpp_toklist *) dummy;
378 /* Initialize the monster. */
379 list->tokens = &dummy->first_token;
380 list->tokens_used = list->tokens_cap = info->ntokens;
382 list->namebuf = (unsigned char *) &list->tokens[info->ntokens];
383 list->name_used = list->name_cap = info->len + info->paramlen;
385 list->directive = 0;
386 list->line = pfile->token_list.line;
387 list->file = pfile->token_list.file;
388 list->params_len = info->paramlen;
389 list->paramc = info->paramc;
390 list->flags = info->flags;
392 return list;
395 /* Free the definition of macro H. */
397 void
398 _cpp_free_definition (h)
399 cpp_hashnode *h;
401 if (h->type == T_MACRO)
402 free ((PTR) h->value.expansion);
403 h->value.expansion = NULL;
406 /* Copy the tokens of the expansion, beginning with info->first until
407 CPP_EOF. INFO contains information about the macro.
409 Change the type of macro arguments in the expansion from CPP_NAME
410 to CPP_MACRO_ARG. Remove #'s that represent stringification,
411 flagging the CPP_MACRO_ARG it operates on STRINGIFY. Remove ##'s,
412 flagging the token on its immediate left PASTE_LEFT. Returns the
413 token list for the macro expansion. */
414 static const cpp_toklist *
415 save_expansion (pfile, info)
416 cpp_reader *pfile;
417 struct macro_info *info;
419 const cpp_token *token;
420 cpp_toklist *list;
421 cpp_token *dest;
422 unsigned char *buf;
424 list = alloc_macro (pfile, info);
425 buf = list->namebuf;
427 /* Store the null-terminated parameter spellings of a macro, to
428 provide pedantic warnings to satisfy 6.10.3.2, or for use when
429 dumping macro definitions. They must go first. */
430 if (list->params_len)
431 for (token = info->first_param; token < info->first; token++)
432 if (token->type == CPP_NAME)
434 /* Copy null too. */
435 memcpy (buf, token->val.node->name, token->val.node->length + 1);
436 buf += token->val.node->length + 1;
439 dest = list->tokens;
440 for (token = info->first; token->type != CPP_EOF; token++)
442 unsigned int param_no;
444 switch (token->type)
446 case CPP_NAME:
447 if (list->paramc == -1)
448 break;
450 /* Check if the name is a macro parameter. */
451 param_no = find_param (info->first_param, token);
452 if (param_no == 0)
453 break;
454 dest->val.aux = param_no - 1;
456 dest->type = CPP_MACRO_ARG;
457 if (token[-1].type == CPP_HASH)
458 dest->flags = token[-1].flags | STRINGIFY_ARG;
459 else
460 dest->flags = token->flags; /* Particularly PREV_WHITE. */
461 /* Turn off PREV_WHITE if we immediately follow a paste.
462 That way, even if the paste turns out to be illegal, there
463 will be no space between the two tokens in the output. */
464 if (token[-1].type == CPP_PASTE)
465 dest->flags &= ~PREV_WHITE;
466 dest++;
467 continue;
469 case CPP_PASTE:
470 /* Set the paste flag on the token to our left, unless there
471 is no possible token to which it might be pasted. That
472 is critical for correct operation under some circumstances;
473 see gcc.dg/cpp/paste6.c. */
474 if (CAN_PASTE_AFTER (dest[-1].type) || (dest[-1].flags & NAMED_OP))
475 dest[-1].flags |= PASTE_LEFT;
476 else if (CPP_OPTION (pfile, warn_paste))
477 cpp_warning_with_line (pfile, dest[-1].line, dest[-1].col,
478 "nothing can be pasted after this token");
479 continue;
481 case CPP_HASH:
482 /* Stringifying #. Constraint 6.10.3.2.1 */
483 if (list->paramc >= 0 && token[1].type == CPP_NAME
484 && find_param (info->first_param, token + 1))
485 continue;
486 break;
488 default:
489 break;
492 /* Copy the token. */
493 *dest = *token;
494 if (TOKEN_SPELL (token) == SPELL_STRING)
496 memcpy (buf, token->val.str.text, token->val.str.len);
497 dest->val.str.text = buf;
498 buf += dest->val.str.len;
500 if (token[-1].type == CPP_PASTE)
501 dest->flags &= ~PREV_WHITE;
502 dest++;
505 /* Empty macros become a single placemarker token. */
506 if (dest == list->tokens)
508 dest->type = CPP_PLACEMARKER;
509 dest->flags = 0;
510 dest->val.aux = 0;
513 return list;
516 /* Parse a macro and save its expansion. Returns non-zero on success. */
518 _cpp_create_definition (pfile, hp)
519 cpp_reader *pfile;
520 cpp_hashnode *hp;
522 struct macro_info info;
523 const cpp_toklist *list;
525 if (parse_define (pfile, &info))
526 return 0;
527 list = save_expansion (pfile, &info);
529 /* Check for a redefinition. Redefinition of a macro is allowed if
530 and only if the old and new definitions are the same.
531 (6.10.3 paragraph 2). */
533 if (hp->type != T_VOID)
535 if (!check_macro_redefinition (pfile, hp, list))
537 cpp_pedwarn (pfile, "\"%s\" redefined", hp->name);
538 if (pfile->done_initializing && hp->type == T_MACRO)
539 cpp_pedwarn_with_file_and_line (pfile,
540 hp->value.expansion->file,
541 hp->value.expansion->line, 1,
542 "this is the location of the previous definition");
544 _cpp_free_definition (hp);
547 /* Enter definition in hash table. */
548 hp->type = T_MACRO;
549 hp->value.expansion = list;
551 return 1;
554 /* Dump the definition of macro MACRO on stdout. The format is suitable
555 to be read back in again. */
557 void
558 _cpp_dump_definition (pfile, hp)
559 cpp_reader *pfile;
560 cpp_hashnode *hp;
562 CPP_RESERVE (pfile, hp->length + sizeof "#define ");
563 CPP_PUTS_Q (pfile, "#define ", sizeof "#define " - 1);
564 CPP_PUTS_Q (pfile, hp->name, hp->length);
566 if (hp->type == T_MACRO)
568 if (hp->value.expansion->paramc >= 0)
569 dump_funlike_macro (pfile, hp);
570 else
572 const cpp_toklist *list = hp->value.expansion;
573 list->tokens[0].flags &= ~BOL;
574 list->tokens[0].flags |= PREV_WHITE;
575 _cpp_dump_list (pfile, list, list->tokens, 1);
578 else
579 cpp_ice (pfile, "invalid hash type %d in dump_definition", hp->type);
581 if (CPP_BUFFER (pfile) == 0 || ! pfile->done_initializing)
582 CPP_PUTC (pfile, '\n');
585 static void
586 dump_funlike_macro (pfile, node)
587 cpp_reader *pfile;
588 cpp_hashnode *node;
590 int i = 0;
591 const cpp_toklist * list = node->value.expansion;
592 const U_CHAR *param;
594 param = list->namebuf;
595 CPP_PUTC_Q (pfile, '(');
596 for (i = 0; i++ < list->paramc;)
598 unsigned int len;
600 len = ustrlen (param);
601 CPP_PUTS (pfile, param, len);
602 if (i < list->paramc)
603 CPP_PUTS(pfile, ", ", 2);
604 else if (list->flags & VAR_ARGS)
606 if (!ustrcmp (param, U"__VA_ARGS__"))
607 pfile->limit -= sizeof (U"__VA_ARGS__") - 1;
608 CPP_PUTS_Q (pfile, "...", 3);
610 param += len + 1;
612 CPP_PUTC (pfile, ')');
613 list->tokens[0].flags &= ~BOL;
614 list->tokens[0].flags |= PREV_WHITE;
615 _cpp_dump_list (pfile, list, list->tokens, 1);