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
*));
57 /* Scans for a given token, returning the parameter number if found,
58 or 0 if not found. Scans from FIRST to TOKEN - 1 or the first
59 CPP_CLOSE_PAREN for TOKEN. */
61 find_param (first
, token
)
62 const cpp_token
*first
, *token
;
64 unsigned int param
= 0;
66 for (; first
< token
&& first
->type
!= CPP_CLOSE_PAREN
; first
++)
67 if (first
->type
== CPP_NAME
)
70 if (first
->val
.node
== token
->val
.node
)
77 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
78 replacement list of a variable-arguments macro. TOKEN is assumed
79 to be of type CPP_NAME. */
81 is__va_args__ (pfile
, token
)
83 const cpp_token
*token
;
85 if (!CPP_PEDANTIC (pfile
)
86 || token
->val
.node
!= pfile
->spec_nodes
->n__VA_ARGS__
)
89 cpp_pedwarn_with_line (pfile
, token
->line
, token
->col
,
90 "\"%s\" is only valid in the replacement list of a function-like macro",
91 token
->val
.node
->name
);
95 /* Counts the parameters to a function-like macro, the length of their
96 null-terminated names, and whether the macro is a variable-argument
97 one. FIRST is the token immediately after the open parenthesis,
100 On success, info->first is updated to the token after the closing
101 parenthesis, i.e. the first token of the expansion. Otherwise
102 there was an error, which has been reported. */
104 count_params (pfile
, info
)
106 struct macro_info
*info
;
108 unsigned int prev_ident
= 0;
109 const cpp_token
*token
;
114 info
->first
= info
->first_param
; /* Not a ')' indicating success. */
116 for (token
= info
->first_param
;; token
++)
121 cpp_error_with_line (pfile
, token
->line
, token
->col
,
122 "illegal token in macro parameter list");
127 cpp_error_with_line (pfile
, token
->line
, token
->col
,
128 "missing ')' in macro parameter list");
132 continue; /* Ignore -C comments. */
137 cpp_error_with_line (pfile
, token
->line
, token
->col
,
138 "macro parameters must be comma-separated");
142 /* Constraint 6.10.3.5 */
143 if (is__va_args__ (pfile
, token
))
146 /* Constraint 6.10.3.6 - duplicate parameter names. */
147 if (find_param (info
->first
, token
))
149 cpp_error_with_line (pfile
, token
->line
, token
->col
,
150 "duplicate macro parameter \"%s\"",
151 token
->val
.node
->name
);
157 info
->paramlen
+= token
->val
.node
->length
+ 1;
160 case CPP_CLOSE_PAREN
:
161 if (prev_ident
|| info
->paramc
== 0)
164 /* Fall through to pick up the error. */
168 cpp_error_with_line (pfile
, token
->line
, token
->col
,
169 "parameter name expected");
176 /* Convert ISO-style var_args to named varargs by changing
177 the ellipsis into an identifier with name __VA_ARGS__.
178 This simplifies other handling. */
181 cpp_token
*tok
= (cpp_token
*) token
;
183 tok
->type
= CPP_NAME
;
184 tok
->val
.node
= pfile
->spec_nodes
->n__VA_ARGS__
;
187 info
->paramlen
+= tok
->val
.node
->length
+ 1;
189 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, c99
))
191 "C89 does not permit anon varargs macros");
195 info
->flags
|= GNU_REST_ARGS
;
196 if (CPP_PEDANTIC (pfile
))
198 "ISO C does not permit named varargs parameters");
201 info
->flags
|= VAR_ARGS
;
203 if (token
->type
== CPP_CLOSE_PAREN
)
209 info
->first
= token
+ 1;
210 if (!pfile
->save_parameter_spellings
)
216 /* Parses a #define directive. On success, returns zero, and INFO is
217 filled in appropriately. */
219 parse_define (pfile
, info
)
221 struct macro_info
*info
;
223 const cpp_token
*token
;
226 /* The first token after the macro's name. */
227 token
= _cpp_get_token (pfile
);
229 /* Constraint 6.10.3.5 */
230 if (is__va_args__ (pfile
, token
- 1))
233 while (token
->type
== CPP_COMMENT
)
234 token
++, prev_white
= 1;
235 prev_white
|= token
->flags
& PREV_WHITE
;
237 if (token
->type
== CPP_OPEN_PAREN
&& !prev_white
)
239 /* A function-like macro. */
240 info
->first_param
= token
+ 1;
241 count_params (pfile
, info
);
242 if (info
->first
[-1].type
!= CPP_CLOSE_PAREN
)
247 /* An object-like macro. */
252 if (!prev_white
&& token
->type
!= CPP_EOF
)
253 cpp_pedwarn (pfile
, "ISO C requires whitespace after the macro name");
256 /* Count tokens in expansion. We drop paste tokens, and stringize
257 tokens, so don't count them. */
258 info
->ntokens
= info
->len
= 0;
259 for (token
= info
->first
; token
->type
!= CPP_EOF
; token
++)
261 if (token
->type
== CPP_PASTE
)
263 /* Token-paste ##, can appear in both object-like and
264 function-like macros, but not at the ends. Constraint
266 if (token
== info
->first
|| token
[1].type
== CPP_EOF
)
268 cpp_error_with_line (pfile
, token
->line
, token
->col
,
269 "'##' cannot appear at either end of a macro expansion");
274 else if (token
->type
== CPP_HASH
)
276 /* Stringifying #, but a normal character in object-like
277 macros. Must come before a parameter name. Constraint
279 if (info
->paramc
>= 0)
281 if (token
[1].type
== CPP_NAME
282 && find_param (info
->first_param
, token
+ 1))
284 if (! CPP_OPTION (pfile
, lang_asm
))
286 cpp_error_with_line (pfile
, token
->line
, token
->col
,
287 "'#' is not followed by a macro parameter");
292 else if (token
->type
== CPP_NAME
)
294 /* Constraint 6.10.3.5 */
295 if (!(info
->flags
& VAR_ARGS
) && is__va_args__ (pfile
, token
))
297 /* It might be worth doing a check here that we aren't a
298 macro argument, since we don't store the text of macro
299 arguments. This would reduce "len" and save space. */
302 if (TOKEN_SPELL (token
) == SPELL_STRING
)
303 info
->len
+= token
->val
.str
.len
;
309 /* Returns non-zero if a macro redefinition is trivial. */
311 check_macro_redefinition (pfile
, hp
, list2
)
314 const cpp_toklist
*list2
;
316 const cpp_toklist
*list1
;
318 if (hp
->type
!= T_MACRO
)
319 return ! pfile
->done_initializing
;
321 /* Clear the whitespace and BOL flags of the first tokens. They get
322 altered during macro expansion, but is not significant here. */
323 list1
= hp
->value
.expansion
;
324 list1
->tokens
[0].flags
&= ~(PREV_WHITE
|BOL
);
325 list2
->tokens
[0].flags
&= ~(PREV_WHITE
|BOL
);
327 if (!_cpp_equiv_toklists (list1
, list2
))
330 if (CPP_OPTION (pfile
, pedantic
)
332 && (list1
->params_len
!= list2
->params_len
333 || memcmp (list1
->namebuf
, list2
->namebuf
, list1
->params_len
)))
339 /* This is a dummy structure whose only purpose is getting alignment
344 cpp_token first_token
;
347 /* Allocate space to hold the token list, its tokens, their text, and
348 the parameter names if needed. Empty expansions are stored as a
349 single placemarker token.
351 These are all allocated in a block together for performance
352 reasons. Therefore, this token list cannot be expanded like a
353 normal token list. Try to do so, and you lose. */
355 alloc_macro (pfile
, info
)
357 struct macro_info
*info
;
360 struct toklist_dummy
*dummy
;
363 /* Empty macros become a single placemarker token. */
364 if (info
->ntokens
== 0)
367 size
= sizeof (struct toklist_dummy
);
368 size
+= (info
->ntokens
- 1) * sizeof(cpp_token
);
369 size
+= info
->len
+ info
->paramlen
;
371 dummy
= (struct toklist_dummy
*) xmalloc (size
);
372 list
= (cpp_toklist
*) dummy
;
374 /* Initialize the monster. */
375 list
->tokens
= &dummy
->first_token
;
376 list
->tokens_used
= list
->tokens_cap
= info
->ntokens
;
378 list
->namebuf
= (unsigned char *) &list
->tokens
[info
->ntokens
];
379 list
->name_used
= list
->name_cap
= info
->len
+ info
->paramlen
;
382 list
->line
= pfile
->token_list
.line
;
383 list
->file
= pfile
->token_list
.file
;
384 list
->params_len
= info
->paramlen
;
385 list
->paramc
= info
->paramc
;
386 list
->flags
= info
->flags
;
391 /* Free the definition of macro H. */
394 _cpp_free_definition (h
)
397 if (h
->type
== T_MACRO
)
398 free ((PTR
) h
->value
.expansion
);
399 h
->value
.expansion
= NULL
;
402 /* Copy the tokens of the expansion, beginning with info->first until
403 CPP_EOF. INFO contains information about the macro.
405 Change the type of macro arguments in the expansion from CPP_NAME
406 to CPP_MACRO_ARG. Remove #'s that represent stringification,
407 flagging the CPP_MACRO_ARG it operates on STRINGIFY. Remove ##'s,
408 flagging the token on its immediate left PASTE_LEFT. Returns the
409 token list for the macro expansion. */
410 static const cpp_toklist
*
411 save_expansion (pfile
, info
)
413 struct macro_info
*info
;
415 const cpp_token
*token
;
420 list
= alloc_macro (pfile
, info
);
423 /* Store the null-terminated parameter spellings of a macro, to
424 provide pedantic warnings to satisfy 6.10.3.2, or for use when
425 dumping macro definitions. They must go first. */
426 if (list
->params_len
)
427 for (token
= info
->first_param
; token
< info
->first
; token
++)
428 if (token
->type
== CPP_NAME
)
431 memcpy (buf
, token
->val
.node
->name
, token
->val
.node
->length
+ 1);
432 buf
+= token
->val
.node
->length
+ 1;
436 for (token
= info
->first
; token
->type
!= CPP_EOF
; token
++)
438 unsigned int param_no
;
443 if (list
->paramc
== -1)
446 /* Check if the name is a macro parameter. */
447 param_no
= find_param (info
->first_param
, token
);
450 dest
->val
.aux
= param_no
- 1;
452 dest
->type
= CPP_MACRO_ARG
;
453 if (token
[-1].type
== CPP_HASH
)
454 dest
->flags
= token
[-1].flags
| STRINGIFY_ARG
;
456 dest
->flags
= token
->flags
; /* Particularly PREV_WHITE. */
457 /* Turn off PREV_WHITE if we immediately follow a paste.
458 That way, even if the paste turns out to be illegal, there
459 will be no space between the two tokens in the output. */
460 if (token
[-1].type
== CPP_PASTE
)
461 dest
->flags
&= ~PREV_WHITE
;
466 dest
[-1].flags
|= PASTE_LEFT
;
470 /* Stringifying #. Constraint 6.10.3.2.1 */
471 if (list
->paramc
>= 0 && token
[1].type
== CPP_NAME
472 && find_param (info
->first_param
, token
+ 1))
480 /* Copy the token. */
482 if (TOKEN_SPELL (token
) == SPELL_STRING
)
484 memcpy (buf
, token
->val
.str
.text
, token
->val
.str
.len
);
485 dest
->val
.str
.text
= buf
;
486 buf
+= dest
->val
.str
.len
;
488 if (token
[-1].type
== CPP_PASTE
)
489 dest
->flags
&= ~PREV_WHITE
;
493 /* Empty macros become a single placemarker token. */
494 if (dest
== list
->tokens
)
496 dest
->type
= CPP_PLACEMARKER
;
503 /* Parse a macro and save its expansion. Returns non-zero on success. */
505 _cpp_create_definition (pfile
, hp
)
509 struct macro_info info
;
510 const cpp_toklist
*list
;
512 if (parse_define (pfile
, &info
))
514 list
= save_expansion (pfile
, &info
);
516 /* Check for a redefinition. Redefinition of a macro is allowed if
517 and only if the old and new definitions are the same.
518 (6.10.3 paragraph 2). */
520 if (hp
->type
!= T_VOID
)
522 if (!check_macro_redefinition (pfile
, hp
, list
))
524 cpp_pedwarn (pfile
, "\"%s\" redefined", hp
->name
);
525 if (pfile
->done_initializing
&& hp
->type
== T_MACRO
)
526 cpp_pedwarn_with_file_and_line (pfile
,
527 hp
->value
.expansion
->file
,
528 hp
->value
.expansion
->line
, 1,
529 "this is the location of the previous definition");
531 _cpp_free_definition (hp
);
534 /* Enter definition in hash table. */
536 hp
->value
.expansion
= list
;
541 /* Dump the definition of macro MACRO on stdout. The format is suitable
542 to be read back in again. */
545 _cpp_dump_definition (pfile
, hp
)
549 CPP_RESERVE (pfile
, hp
->length
+ sizeof "#define ");
550 CPP_PUTS_Q (pfile
, "#define ", sizeof "#define " - 1);
551 CPP_PUTS_Q (pfile
, hp
->name
, hp
->length
);
553 if (hp
->type
== T_MACRO
)
555 if (hp
->value
.expansion
->paramc
>= 0)
556 dump_funlike_macro (pfile
, hp
);
559 const cpp_toklist
*list
= hp
->value
.expansion
;
560 list
->tokens
[0].flags
&= ~BOL
;
561 list
->tokens
[0].flags
|= PREV_WHITE
;
562 _cpp_dump_list (pfile
, list
, list
->tokens
, 1);
566 cpp_ice (pfile
, "invalid hash type %d in dump_definition", hp
->type
);
568 if (CPP_BUFFER (pfile
) == 0 || ! pfile
->done_initializing
)
569 CPP_PUTC (pfile
, '\n');
573 dump_funlike_macro (pfile
, node
)
578 const cpp_toklist
* list
= node
->value
.expansion
;
581 param
= list
->namebuf
;
582 CPP_PUTC_Q (pfile
, '(');
583 for (i
= 0; i
++ < list
->paramc
;)
587 len
= ustrlen (param
);
588 CPP_PUTS (pfile
, param
, len
);
589 if (i
< list
->paramc
)
590 CPP_PUTS(pfile
, ", ", 2);
591 else if (list
->flags
& VAR_ARGS
)
593 if (!ustrcmp (param
, U
"__VA_ARGS__"))
594 pfile
->limit
-= sizeof (U
"__VA_ARGS__") - 1;
595 CPP_PUTS_Q (pfile
, "...", 3);
599 CPP_PUTC (pfile
, ')');
600 list
->tokens
[0].flags
&= ~BOL
;
601 list
->tokens
[0].flags
|= PREV_WHITE
;
602 _cpp_dump_list (pfile
, list
, list
->tokens
, 1);