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! */
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
36 /* This is the second argument to eq_HASHNODE. */
40 unsigned short length
;
43 /* Initial hash table size. (It can grow if necessary - see hashtab.c.) */
46 static unsigned int hash_HASHNODE
PARAMS ((const void *));
47 static int eq_HASHNODE
PARAMS ((const void *, const void *));
48 static int dump_hash_helper
PARAMS ((void **, void *));
50 static void dump_funlike_macro
PARAMS ((cpp_reader
*, cpp_hashnode
*));
52 static const cpp_token
*count_params
PARAMS ((cpp_reader
*,
55 static int is__va_args__
PARAMS ((cpp_reader
*, const cpp_token
*));
56 static cpp_toklist
*parse_define
PARAMS((cpp_reader
*));
57 static int check_macro_redefinition
PARAMS((cpp_reader
*, cpp_hashnode
*hp
,
58 const cpp_toklist
*));
59 static int save_expansion
PARAMS((cpp_reader
*, cpp_toklist
*,
60 const cpp_token
*, const cpp_token
*));
61 static unsigned int find_param
PARAMS ((const cpp_token
*,
64 static const unsigned char var_args_str
[] = "__VA_ARGS__";
66 /* Calculate hash of a string of length LEN. */
68 _cpp_calc_hash (str
, len
)
76 r
= r
* 67 + (*str
++ - 113);
81 /* Calculate hash of a cpp_hashnode structure. */
86 const cpp_hashnode
*h
= (const cpp_hashnode
*)x
;
90 /* Compare a cpp_hashnode structure (already in the table) with a
91 hashdummy structure (not yet in the table). This relies on the
92 rule that the existing entry is the first argument, the potential
93 entry the second. It also relies on the comparison function never
94 being called except as a direct consequence of a call to
95 htab_find(_slot)_with_hash. */
101 const cpp_hashnode
*a
= (const cpp_hashnode
*)x
;
102 const struct hashdummy
*b
= (const struct hashdummy
*)y
;
104 return (a
->length
== b
->length
105 && !ustrncmp (a
->name
, b
->name
, a
->length
));
108 /* Find the hash node for name "name", of length LEN. */
111 cpp_lookup (pfile
, name
, len
)
116 struct hashdummy dummy
;
117 cpp_hashnode
*new, **slot
;
123 hash
= _cpp_calc_hash (name
, len
);
125 slot
= (cpp_hashnode
**)
126 htab_find_slot_with_hash (pfile
->hashtab
, (void *)&dummy
, hash
, INSERT
);
130 /* Create a new hash node. */
131 p
= obstack_alloc (pfile
->hash_ob
, sizeof (cpp_hashnode
) + len
);
132 new = (cpp_hashnode
*)p
;
133 p
+= offsetof (cpp_hashnode
, name
);
139 new->value
.expansion
= NULL
;
141 memcpy (p
, name
, len
);
148 /* Set up and tear down internal structures for macro expansion. */
150 _cpp_init_macros (pfile
)
153 pfile
->hashtab
= htab_create (HASHSIZE
, hash_HASHNODE
,
154 eq_HASHNODE
, (htab_del
) _cpp_free_definition
);
155 pfile
->hash_ob
= xnew (struct obstack
);
156 obstack_init (pfile
->hash_ob
);
160 _cpp_cleanup_macros (pfile
)
163 htab_delete (pfile
->hashtab
);
164 obstack_free (pfile
->hash_ob
, 0);
165 free (pfile
->hash_ob
);
168 /* Free the definition of macro H. */
171 _cpp_free_definition (h
)
174 if (h
->type
== T_MACRO
)
175 _cpp_free_toklist (h
->value
.expansion
);
176 h
->value
.expansion
= NULL
;
179 /* Scans for a given token, returning the parameter number if found,
180 or 0 if not found. Scans from FIRST to TOKEN - 1 or the first
181 CPP_CLOSE_PAREN for TOKEN. */
183 find_param (first
, token
)
184 const cpp_token
*first
, *token
;
186 unsigned int param
= 0;
188 for (; first
< token
&& first
->type
!= CPP_CLOSE_PAREN
; first
++)
189 if (first
->type
== CPP_NAME
)
192 if (first
->val
.name
.len
== token
->val
.name
.len
193 && !memcmp (first
->val
.name
.text
, token
->val
.name
.text
,
194 token
->val
.name
.len
))
201 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
202 replacement list of a variable-arguments macro. TOKEN is assumed
203 to be of type CPP_NAME. */
205 is__va_args__ (pfile
, token
)
207 const cpp_token
*token
;
209 if (!CPP_OPTION (pfile
, pedantic
)
210 || token
->val
.name
.len
!= sizeof (var_args_str
) - 1
211 || ustrncmp (token
->val
.name
.text
, var_args_str
,
212 sizeof (var_args_str
) - 1))
215 cpp_pedwarn_with_line (pfile
, token
->line
, token
->col
,
216 "\"%s\" is only valid in the replacement list of a function-like macro",
221 /* Counts the parameters to a function like macro, and saves their
222 spellings if necessary. Returns the token that we stopped scanning
223 at; if it's type isn't CPP_CLOSE_PAREN there was an error, which
224 has been reported. */
225 static const cpp_token
*
226 count_params (pfile
, first
, list
)
228 const cpp_token
*first
;
231 unsigned int params_len
= 0, prev_ident
= 0;
232 const cpp_token
*token
, *temp
;
235 for (token
= first
;; token
++)
241 cpp_error_with_line (pfile
, token
->line
, token
->col
,
242 "missing ')' in macro parameter list");
246 continue; /* Ignore -C comments. */
251 cpp_error_with_line (pfile
, token
->line
, token
->col
,
252 "macro parameters must be comma-separated");
256 /* Constraint 6.10.3.5 */
257 if (is__va_args__ (pfile
, token
))
260 params_len
+= token
->val
.name
.len
+ 1;
264 /* Constraint 6.10.3.6 - duplicate parameter names. */
265 if (find_param (first
, token
))
267 cpp_error_with_line (pfile
, token
->line
, token
->col
,
268 "duplicate macro parameter \"%.*s\"",
269 (int) token
->val
.name
.len
,
270 token
->val
.name
.text
);
276 cpp_error_with_line (pfile
, token
->line
, token
->col
,
277 "illegal token in macro parameter list");
280 case CPP_CLOSE_PAREN
:
281 if (prev_ident
|| list
->paramc
== 0)
284 /* Fall through to pick up the error. */
288 cpp_error_with_line (pfile
, token
->line
, token
->col
,
289 "parameter name expected");
290 if (token
->type
== CPP_CLOSE_PAREN
)
291 token
--; /* Return the ',' not ')'. */
298 /* Convert ISO-style var_args to named varargs by changing
299 the ellipsis into an identifier with name __VA_ARGS__.
300 This simplifies other handling. We can safely have its
301 text outside list->namebuf because there is no reason to
302 extend the size of the list's namebuf (and thus change
303 the pointer) in do_define. */
306 cpp_token
*tok
= (cpp_token
*) token
;
308 tok
->type
= CPP_NAME
;
309 tok
->val
.name
.len
= sizeof (var_args_str
) - 1;
310 tok
->val
.name
.text
= var_args_str
; /* Safe. */
313 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, c99
))
315 "C89 does not permit anon varargs macros");
319 list
->flags
|= GNU_REST_ARGS
;
320 if (CPP_PEDANTIC (pfile
))
322 "ISO C does not permit named varargs parameters");
325 list
->flags
|= VAR_ARGS
;
327 if (token
->type
== CPP_CLOSE_PAREN
)
334 /* Store the null-terminated parameter spellings of a function, to
335 provide pedantic warnings to satisfy 6.10.3.2, or for use when
336 dumping macro definitions. */
337 if (list
->paramc
> 0 && pfile
->save_parameter_spellings
)
341 _cpp_reserve_name_space (list
, params_len
);
342 list
->params_len
= list
->name_used
= params_len
;
344 for (temp
= first
; temp
<= token
; temp
++)
345 if (temp
->type
== CPP_NAME
)
347 memcpy (buf
, temp
->val
.name
.text
, temp
->val
.name
.len
);
348 buf
+= temp
->val
.name
.len
;
357 /* Parses a #define directive. Returns null pointer on error. */
362 const cpp_token
*token
, *first_param
;
366 /* The first token after the macro's name. */
367 token
= cpp_get_token (pfile
);
369 /* Constraint 6.10.3.5 */
370 if (is__va_args__ (pfile
, token
- 1))
373 while (token
->type
== CPP_COMMENT
)
374 token
++, prev_white
= 1;
376 /* Allocate the expansion's list. It will go in the hash table. */
377 list
= (cpp_toklist
*) xmalloc (sizeof (cpp_toklist
));
378 _cpp_init_toklist (list
, 0);
379 first_param
= token
+ 1;
380 list
->paramc
= -1; /* Object-like macro. */
382 if (!prev_white
&& !(token
->flags
& PREV_WHITE
))
384 if (token
->type
== CPP_OPEN_PAREN
)
386 token
= count_params (pfile
, first_param
, list
);
387 if (token
->type
!= CPP_CLOSE_PAREN
)
391 else if (token
->type
!= CPP_EOF
)
393 "ISO C requires whitespace after the macro name");
396 if (save_expansion (pfile
, list
, token
, first_param
))
399 _cpp_free_toklist (list
);
407 check_macro_redefinition (pfile
, hp
, list2
)
410 const cpp_toklist
*list2
;
412 const cpp_toklist
*list1
;
414 if (hp
->type
!= T_MACRO
)
415 return ! pfile
->done_initializing
;
417 /* Clear the whitespace and BOL flags of the first tokens. They get
418 altered during macro expansion, but is not significant here. */
419 list1
= hp
->value
.expansion
;
420 list1
->tokens
[0].flags
&= ~(PREV_WHITE
|BOL
);
421 list2
->tokens
[0].flags
&= ~(PREV_WHITE
|BOL
);
423 if (!_cpp_equiv_toklists (list1
, list2
))
426 if (CPP_OPTION (pfile
, pedantic
)
428 && (list1
->params_len
!= list2
->params_len
429 || memcmp (list1
->namebuf
, list2
->namebuf
, list1
->params_len
)))
435 /* Copy the tokens of the expansion. Change the type of macro
436 arguments from CPP_NAME to CPP_MACRO_ARG. Remove #'s that
437 represent stringification, flagging the CPP_MACRO_ARG it operates
438 on STRINGIFY. Remove ##'s, flagging the token on its immediate
439 left PASTE_LEFT. Returns non-zero on error. */
441 save_expansion (pfile
, list
, first
, first_param
)
444 const cpp_token
*first
;
445 const cpp_token
*first_param
;
447 const cpp_token
*token
;
449 unsigned int len
, ntokens
;
452 /* Count tokens in expansion. We drop paste tokens, and stringize
453 tokens, so don't count them. */
455 for (token
= first
; token
->type
!= CPP_EOF
; token
++)
459 if (token
->type
== CPP_PASTE
)
461 /* Token-paste ##, but is a normal token if traditional. */
462 if (! CPP_TRADITIONAL (pfile
))
464 msg
= "\"##\" cannot appear at either end of a macro expansion";
465 /* Constraint 6.10.3.3.1 */
466 if (token
== first
|| token
[1].type
== CPP_EOF
)
471 else if (token
->type
== CPP_HASH
)
473 /* Stringifying #, but is a normal character if traditional,
474 or in object-like macros. Constraint 6.10.3.2.1. */
475 if (list
->paramc
>= 0 && ! CPP_TRADITIONAL (pfile
))
477 if (token
[1].type
== CPP_NAME
478 && find_param (first_param
, token
+ 1))
480 if (! CPP_OPTION (pfile
, lang_asm
))
482 msg
= "'#' is not followed by a macro parameter";
484 cpp_error_with_line (pfile
, token
->line
, token
->col
, msg
);
489 else if (token
->type
== CPP_NAME
)
491 /* Constraint 6.10.3.5 */
492 if (!(list
->flags
& VAR_ARGS
) && is__va_args__ (pfile
, token
))
496 if (token_spellings
[token
->type
].type
> SPELL_NONE
)
497 len
+= token
->val
.name
.len
;
500 /* Allocate space to hold the tokens. Empty expansions are stored
501 as a single placemarker token. */
504 _cpp_expand_token_space (list
, ntokens
);
506 _cpp_expand_name_space (list
, len
);
509 buf
= list
->namebuf
+ list
->name_used
;
510 for (token
= first
; token
->type
!= CPP_EOF
; token
++)
512 unsigned int param_no
;
517 if (list
->paramc
== -1)
520 /* Check if the name is a macro parameter. */
521 param_no
= find_param (first_param
, token
);
524 dest
->val
.aux
= param_no
- 1;
526 dest
->type
= CPP_MACRO_ARG
;
527 if (token
[-1].type
== CPP_HASH
&& ! CPP_TRADITIONAL (pfile
))
528 dest
->flags
= token
[-1].flags
| STRINGIFY_ARG
;
530 dest
->flags
= token
->flags
; /* Particularly PREV_WHITE. */
535 if (! CPP_TRADITIONAL (pfile
))
537 dest
[-1].flags
|= PASTE_LEFT
;
543 /* Stringifying #. Constraint 6.10.3.2.1 */
544 if (list
->paramc
>= 0 && ! CPP_TRADITIONAL (pfile
)
545 && token
[1].type
== CPP_NAME
546 && find_param (first_param
, token
+ 1))
554 /* Copy the token. */
556 if (token_spellings
[token
->type
].type
> SPELL_NONE
)
558 memcpy (buf
, token
->val
.name
.text
, token
->val
.name
.len
);
559 dest
->val
.name
.text
= buf
;
560 buf
+= dest
->val
.name
.len
;
565 if (dest
== list
->tokens
)
567 dest
->type
= CPP_PLACEMARKER
;
571 list
->tokens_used
= ntokens
;
572 list
->line
= pfile
->token_list
.line
;
573 list
->file
= pfile
->token_list
.file
;
574 list
->name_used
= len
;
580 _cpp_create_definition (pfile
, hp
)
586 list
= parse_define (pfile
);
590 /* Check for a redefinition. Redefinition of a macro is allowed if
591 and only if the old and new definitions are the same.
592 (6.10.3 paragraph 2). */
594 if (hp
->type
!= T_VOID
)
596 if (!check_macro_redefinition (pfile
, hp
, list
))
598 cpp_pedwarn (pfile
, "\"%s\" redefined", hp
->name
);
599 if (pfile
->done_initializing
&& hp
->type
== T_MACRO
)
600 cpp_pedwarn_with_file_and_line (pfile
,
601 hp
->value
.expansion
->file
,
602 hp
->value
.expansion
->line
, 1,
603 "this is the location of the previous definition");
605 _cpp_free_definition (hp
);
608 /* Enter definition in hash table. */
610 hp
->value
.expansion
= list
;
615 /* Dump the definition of macro MACRO on stdout. The format is suitable
616 to be read back in again. */
619 _cpp_dump_definition (pfile
, hp
)
623 CPP_RESERVE (pfile
, hp
->length
+ sizeof "#define ");
624 CPP_PUTS_Q (pfile
, "#define ", sizeof "#define " - 1);
625 CPP_PUTS_Q (pfile
, hp
->name
, hp
->length
);
627 if (hp
->type
== T_MACRO
)
629 if (hp
->value
.expansion
->paramc
>= 0)
630 dump_funlike_macro (pfile
, hp
);
633 const cpp_toklist
*list
= hp
->value
.expansion
;
634 list
->tokens
[0].flags
&= ~BOL
;
635 list
->tokens
[0].flags
|= PREV_WHITE
;
636 _cpp_dump_list (pfile
, list
, list
->tokens
, 1);
640 cpp_ice (pfile
, "invalid hash type %d in dump_definition", hp
->type
);
642 if (CPP_BUFFER (pfile
) == 0 || ! pfile
->done_initializing
)
643 CPP_PUTC (pfile
, '\n');
647 dump_funlike_macro (pfile
, node
)
652 const cpp_toklist
* list
= node
->value
.expansion
;
655 param
= list
->namebuf
;
656 CPP_PUTC_Q (pfile
, '(');
657 for (i
= 0; i
++ < list
->paramc
;)
661 len
= ustrlen (param
);
662 CPP_PUTS (pfile
, param
, len
);
663 if (i
< list
->paramc
)
664 CPP_PUTS(pfile
, ", ", 2);
665 else if (list
->flags
& VAR_ARGS
)
667 if (!ustrcmp (param
, var_args_str
))
668 pfile
->limit
-= sizeof (var_args_str
) - 1;
669 CPP_PUTS (pfile
, "...", 3);
673 CPP_PUTC (pfile
, ')');
674 list
->tokens
[0].flags
&= ~BOL
;
675 list
->tokens
[0].flags
|= PREV_WHITE
;
676 _cpp_dump_list (pfile
, list
, list
->tokens
, 1);
679 /* Dump out the hash table. */
681 dump_hash_helper (h
, p
)
685 cpp_hashnode
*hp
= (cpp_hashnode
*)*h
;
686 cpp_reader
*pfile
= (cpp_reader
*)p
;
688 if (hp
->type
== T_MACRO
)
689 _cpp_dump_definition (pfile
, hp
);
694 _cpp_dump_macro_hash (pfile
)
697 htab_traverse (pfile
->hashtab
, dump_hash_helper
, pfile
);