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 static unsigned int hash_HASHNODE PARAMS ((const void *));
34 static int eq_HASHNODE PARAMS ((const void *, const void *));
35 static void del_HASHNODE PARAMS ((void *));
36 static int dump_hash_helper PARAMS ((void *, void *));
38 static int comp_def_part PARAMS ((int, U_CHAR *, int, U_CHAR *,
40 static void push_macro_expansion PARAMS ((cpp_reader *,
41 U_CHAR *, int, HASHNODE *));
42 static int unsafe_chars PARAMS ((cpp_reader *, int, int));
43 static int macro_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
44 static enum cpp_token macarg PARAMS ((cpp_reader *, int));
45 static struct tm *timestamp PARAMS ((cpp_reader *));
46 static void special_symbol PARAMS ((HASHNODE *, cpp_reader *));
48 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
49 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
50 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
52 /* Initial hash table size. (It can grow if necessary - see hashtab.c.) */
55 /* The arglist structure is built by create_definition to tell
56 collect_expansion where the argument names begin. That
57 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
58 would contain pointers to the strings x, y, and z.
59 collect_expansion would then build a DEFINITION node,
60 with reflist nodes pointing to the places x, y, and z had
61 appeared. So the arglist is just convenience data passed
62 between these two routines. It is not kept around after
63 the current #define has been processed and entered into the
81 static DEFINITION *collect_expansion PARAMS ((cpp_reader *, struct arglist *));
82 static struct arglist *collect_formal_parameters PARAMS ((cpp_reader *));
84 /* This structure represents one parsed argument in a macro call.
85 `raw' points to the argument text as written (`raw_length' is its length).
86 `expanded' points to the argument's macro-expansion
87 (its length is `expand_length').
88 `stringified_length' is the length the argument would have
91 /* raw and expanded are relative to ARG_BASE */
92 #define ARG_BASE ((pfile)->token_buffer)
96 /* Strings relative to pfile->token_buffer */
97 long raw, expanded, stringified;
98 int raw_length, expand_length;
99 int stringified_length;
102 /* Calculate hash of a HASHNODE structure. */
107 HASHNODE *h = (HASHNODE *)x;
108 const U_CHAR *s = h->name;
109 unsigned int len = h->length;
110 unsigned int n = len, r = 0;
112 if (h->hash != (unsigned long)-1)
116 r = r * 67 + (*s++ - 113);
122 /* Compare two HASHNODE structures. */
128 const HASHNODE *a = (const HASHNODE *)x;
129 const HASHNODE *b = (const HASHNODE *)y;
131 return (a->length == b->length
132 && !strncmp (a->name, b->name, a->length));
135 /* Destroy a HASHNODE. */
140 HASHNODE *h = (HASHNODE *)x;
142 if (h->type == T_MACRO)
143 _cpp_free_definition (h->value.defn);
144 free ((void *) h->name);
148 /* Allocate and initialize a HASHNODE structure.
149 Caller must fill in the value field. */
152 _cpp_make_hashnode (name, len, type, hash)
158 HASHNODE *hp = (HASHNODE *) xmalloc (sizeof (HASHNODE));
159 U_CHAR *p = xmalloc (len + 1);
166 memcpy (p, name, len);
172 /* Find the hash node for name "name", which ends at the first
175 If LEN is >= 0, it is the length of the name.
176 Otherwise, compute the length now. */
179 _cpp_lookup (pfile, name, len)
189 for (bp = name; is_idchar (*bp); bp++);
197 return (HASHNODE *) htab_find (pfile->hashtab, (void *)&dummy);
200 /* Find the hashtable slot for name "name". Used to insert or delete. */
202 _cpp_lookup_slot (pfile, name, len, insert, hash)
215 for (bp = name; is_idchar (*bp); bp++);
223 slot = (HASHNODE **) htab_find_slot (pfile->hashtab, (void *)&dummy, insert);
229 /* Init the hash table. In here so it can see the hash and eq functions. */
231 _cpp_init_macro_hash (pfile)
234 pfile->hashtab = htab_create (HASHSIZE, hash_HASHNODE,
235 eq_HASHNODE, del_HASHNODE);
238 /* Free a DEFINITION structure. Used by delete_macro, and by
239 do_define when redefining macros. */
242 _cpp_free_definition (d)
245 struct reflist *ap, *nextap;
247 for (ap = d->pattern; ap != NULL; ap = nextap)
258 macro_cleanup (pbuf, pfile)
260 cpp_reader *pfile ATTRIBUTE_UNUSED;
262 HASHNODE *macro = (HASHNODE *) pbuf->data;
263 if (macro->type == T_DISABLED)
264 macro->type = T_MACRO;
265 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
266 free ((PTR) pbuf->buf);
270 /* Read a replacement list for a macro, and build the DEFINITION
271 structure. ARGLIST specifies the formal parameters to look for in
272 the text of the definition. If ARGLIST is null, this is an
273 object-like macro; if it points to an empty arglist, this is a
274 function-like macro with no arguments.
276 A good half of this is devoted to supporting -traditional.
280 collect_expansion (pfile, arglist)
282 struct arglist *arglist;
285 struct reflist *pat = 0, *endpat = 0;
286 enum cpp_token token;
287 long start, here, last;
293 enum { START = 0, NORM, ARG, STRIZE, PASTE } last_token = START;
297 argv = arglist->argv;
298 argc = arglist->argc;
306 last = start = CPP_WRITTEN (pfile);
307 last -= 2; /* two extra chars for the leading escape */
310 /* We use cpp_get_token because _cpp_get_directive_token would
311 discard whitespace and we can't cope with that yet. Macro
312 expansion is off, so we are guaranteed not to see POP or EOF. */
314 while (PEEKC () == '\r')
317 CPP_BUMP_LINE (pfile);
319 if (PEEKC () == '\n')
321 here = CPP_WRITTEN (pfile);
322 token = cpp_get_token (pfile);
323 tok = pfile->token_buffer + here;
329 cpp_ice (pfile, "EOF or VSPACE in collect_expansion");
333 if (last_token == STRIZE || last_token == PASTE
334 || last_token == START)
335 CPP_SET_WRITTEN (pfile, here);
339 if (last_token == PASTE)
340 /* Not really a stringifier. */
343 CPP_SET_WRITTEN (pfile, here); /* delete from replacement text */
347 /* If the last token was an argument, discard this token and
348 any hspace between it and the argument's position. Then
349 mark the arg raw_after. */
350 if (last_token == ARG)
352 endpat->raw_after = 1;
354 CPP_SET_WRITTEN (pfile, last);
357 else if (last_token == PASTE)
358 /* ## ## - the second ## is ordinary. */
360 else if (last_token == START)
361 cpp_error (pfile, "`##' at start of macro definition");
363 /* Discard the token and any hspace before it. */
364 while (is_hspace (pfile->token_buffer[here-1]))
366 CPP_SET_WRITTEN (pfile, here);
368 if (last_token == STRIZE)
369 /* Oops - that wasn't a stringify operator. */
370 CPP_PUTC (pfile, '#');
375 /* We must be in -traditional mode. Pretend this was a
376 token paste, but only if there was no leading or
378 CPP_SET_WRITTEN (pfile, here);
379 if (is_hspace (pfile->token_buffer[here-1]))
381 if (is_hspace (PEEKC ()))
383 if (last_token == ARG)
384 endpat->raw_after = 1;
390 if (last_token == STRIZE)
391 cpp_error (pfile, "`#' is not followed by a macro argument name");
393 if (CPP_TRADITIONAL (pfile) || CPP_OPTIONS (pfile)->warn_stringify)
394 goto maybe_trad_stringify;
399 for (i = 0; i < argc; i++)
400 if (!strncmp (tok, argv[i].name, argv[i].len)
401 && ! is_idchar (tok[argv[i].len]))
407 if (last_token == STRIZE)
408 cpp_error (pfile, "`#' is not followed by a macro argument name");
416 struct reflist *tpat;
418 /* Make a pat node for this arg and add it to the pat list */
419 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
421 tpat->raw_before = (last_token == PASTE);
423 tpat->stringify = (last_token == STRIZE);
424 tpat->rest_args = argv[i].rest_arg;
426 tpat->nchars = here - last;
435 CPP_SET_WRITTEN (pfile, here); /* discard arg name */
439 maybe_trad_stringify:
442 U_CHAR *base, *p, *limit;
443 struct reflist *tpat;
445 base = p = pfile->token_buffer + here;
446 limit = CPP_PWRITTEN (pfile);
452 for (i = 0; i < argc; i++)
453 if (!strncmp (tok, argv[i].name, argv[i].len)
454 && ! is_idchar (tok[argv[i].len]))
459 if (!CPP_TRADITIONAL (pfile))
461 /* Must have got here because of -Wtraditional. */
463 "macro argument `%.*s' would be stringified with -traditional",
464 (int) argv[i].len, argv[i].name);
467 if (CPP_OPTIONS (pfile)->warn_stringify)
468 cpp_warning (pfile, "macro argument `%.*s' is stringified",
469 (int) argv[i].len, argv[i].name);
471 /* Remove the argument from the string. */
472 memmove (p, p + argv[i].len, limit - (p + argv[i].len));
473 limit -= argv[i].len;
475 /* Make a pat node for this arg and add it to the pat list */
476 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
479 /* Don't attempt to paste this with anything. */
480 tpat->raw_before = 0;
483 tpat->rest_args = argv[i].rest_arg;
485 tpat->nchars = (p - base) + here - last;
492 last = (p - base) + here;
494 CPP_ADJUST_WRITTEN (pfile, CPP_PWRITTEN (pfile) - limit);
499 if (last_token == STRIZE)
500 cpp_error (pfile, "`#' is not followed by a macro argument name");
501 else if (last_token == PASTE)
502 cpp_error (pfile, "`##' at end of macro definition");
504 if (last_token == START)
506 /* Empty macro definition. */
507 exp = (U_CHAR *) xstrdup ("\r \r ");
512 /* Trim trailing white space from definition. */
513 here = CPP_WRITTEN (pfile);
514 while (here > last && is_hspace (pfile->token_buffer [here-1]))
516 CPP_SET_WRITTEN (pfile, here);
517 CPP_NUL_TERMINATE (pfile);
518 len = CPP_WRITTEN (pfile) - start + 1;
519 /* space for no-concat markers at either end */
520 exp = (U_CHAR *) xmalloc (len + 4);
526 memcpy (&exp[2], pfile->token_buffer + start, len - 1);
529 CPP_SET_WRITTEN (pfile, start);
531 defn = (DEFINITION *) xmalloc (sizeof (DEFINITION));
532 defn->length = len + 3;
533 defn->expansion = exp;
539 defn->argnames = arglist->namebuf;
542 defn->rest_args = argv[argc - 1].rest_arg;
556 static struct arglist *
557 collect_formal_parameters (pfile)
560 struct arglist *result = 0;
561 struct arg *argv = 0;
562 U_CHAR *namebuf = (U_CHAR *) xstrdup ("");
569 enum cpp_token token;
572 old_written = CPP_WRITTEN (pfile);
573 token = _cpp_get_directive_token (pfile);
574 if (token != CPP_LPAREN)
576 cpp_ice (pfile, "first token = %d not %d in collect_formal_parameters",
581 argv = (struct arg *) xmalloc (sizeof (struct arg));
583 argv[argc].rest_arg = 0;
586 CPP_SET_WRITTEN (pfile, old_written);
587 token = _cpp_get_directive_token (pfile);
591 tok = pfile->token_buffer + old_written;
592 len = CPP_PWRITTEN (pfile) - tok;
594 && (name = (U_CHAR *) strstr (namebuf, tok))
596 && (name == namebuf || name[-1] == ','))
598 cpp_error (pfile, "duplicate macro argument name `%s'", tok);
601 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->c99
602 && !strncmp (tok, "__VA_ARGS__", sizeof "__VA_ARGS__" - 1))
604 "C99 does not permit use of `__VA_ARGS__' as a macro argument name");
605 namebuf = (U_CHAR *) xrealloc (namebuf, argslen + len + 1);
606 name = &namebuf[argslen - 1];
609 memcpy (name, tok, len);
612 argv[argc].len = len;
613 argv[argc].rest_arg = 0;
618 argv = (struct arg *) xrealloc (argv, (argc + 1)*sizeof(struct arg));
629 cpp_error (pfile, "missing right paren in macro argument list");
633 cpp_error (pfile, "syntax error in #define");
639 /* There are two possible styles for a vararg macro:
640 the C99 way: #define foo(a, ...) a, __VA_ARGS__
641 the gnu way: #define foo(a, b...) a, b
642 The C99 way can be considered a special case of the gnu way.
643 There are also some constraints to worry about, but we'll handle
645 if (argv[argc].len == 0)
647 if (CPP_PEDANTIC (pfile) && ! CPP_OPTIONS (pfile)->c99)
648 cpp_pedwarn (pfile, "C89 does not permit varargs macros");
650 len = sizeof "__VA_ARGS__" - 1;
651 namebuf = (U_CHAR *) xrealloc (namebuf, argslen + len + 1);
652 name = &namebuf[argslen - 1];
654 memcpy (name, "__VA_ARGS__", len);
655 argv[argc].len = len;
658 if (CPP_PEDANTIC (pfile))
659 cpp_pedwarn (pfile, "ISO C does not permit named varargs macros");
661 argv[argc].rest_arg = 1;
663 token = _cpp_get_directive_token (pfile);
664 if (token != CPP_RPAREN)
666 cpp_error (pfile, "another parameter follows `...'");
671 /* Go through argv and fix up the pointers. */
673 for (i = 0; i <= argc; i++)
675 argv[i].name = namebuf + len;
676 len += argv[i].len + 1;
677 namebuf[len - 1] = '\0';
680 CPP_SET_WRITTEN (pfile, old_written);
682 result = (struct arglist *) xmalloc (sizeof (struct arglist));
683 if (namebuf[0] != '\0')
685 result->namebuf = namebuf;
686 result->argc = argc + 1;
707 /* Create a DEFINITION node for a macro. The reader's point is just
708 after the macro name. If FUNLIKE is true, this is a function-like
712 _cpp_create_definition (pfile, funlike)
716 struct arglist *args = 0;
721 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
722 file = CPP_BUFFER (pfile)->nominal_fname;
724 pfile->no_macro_expand++;
725 pfile->parsing_define_directive++;
726 CPP_OPTIONS (pfile)->discard_comments++;
727 CPP_OPTIONS (pfile)->no_line_commands++;
731 args = collect_formal_parameters (pfile);
736 defn = collect_expansion (pfile, args);
744 pfile->no_macro_expand--;
745 pfile->parsing_define_directive--;
746 CPP_OPTIONS (pfile)->discard_comments--;
747 CPP_OPTIONS (pfile)->no_line_commands--;
751 pfile->no_macro_expand--;
752 pfile->parsing_define_directive--;
753 CPP_OPTIONS (pfile)->discard_comments--;
754 CPP_OPTIONS (pfile)->no_line_commands--;
759 * Parse a macro argument and append the info on PFILE's token_buffer.
760 * REST_ARGS means to absorb the rest of the args.
761 * Return nonzero to indicate a syntax error.
764 static enum cpp_token
765 macarg (pfile, rest_args)
770 enum cpp_token token;
772 /* Try to parse as much of the argument as exists at this
773 input stack level. */
776 token = cpp_get_token (pfile);
782 /* If we've hit end of file, it's an error (reported by caller).
783 Ditto if it's the end of cpp_expand_to_buffer text.
784 If we've hit end of macro, just continue. */
785 if (!CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
796 /* if we've returned to lowest level and
797 we aren't absorbing all args */
798 if (paren == 0 && rest_args == 0)
802 /* Remove ',' or ')' from argument buffer. */
803 CPP_ADJUST_WRITTEN (pfile, -1);
817 time_t t = time ((time_t *) 0);
818 pfile->timebuf = localtime (&t);
820 return pfile->timebuf;
823 static const char * const monthnames[] =
825 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
826 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
829 /* Place into PFILE a quoted string representing the string SRC.
830 Caller must reserve enough space in pfile->token_buffer. */
833 _cpp_quote_string (pfile, src)
839 CPP_PUTC_Q (pfile, '\"');
841 switch ((c = *src++))
845 CPP_PUTC_Q (pfile, c);
848 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
849 CPP_ADJUST_WRITTEN (pfile, 4);
855 CPP_PUTC_Q (pfile, '\\');
856 CPP_PUTC_Q (pfile, c);
860 CPP_PUTC_Q (pfile, '\"');
861 CPP_NUL_TERMINATE_Q (pfile);
867 * expand things like __FILE__. Place the expansion into the output
868 * buffer *without* rescanning.
872 special_symbol (hp, pfile)
885 ip = cpp_file_buffer (pfile);
886 if (hp->type == T_BASE_FILE)
888 while (CPP_PREV_BUFFER (ip) != NULL)
889 ip = CPP_PREV_BUFFER (ip);
892 buf = ip->nominal_fname;
896 CPP_RESERVE (pfile, 3 + 4 * strlen (buf));
897 _cpp_quote_string (pfile, buf);
901 case T_INCLUDE_LEVEL:
903 int true_indepth = 1;
904 ip = cpp_file_buffer (pfile);
905 while ((ip = CPP_PREV_BUFFER (ip)) != NULL)
908 CPP_RESERVE (pfile, 10);
909 sprintf (CPP_PWRITTEN (pfile), "%d", true_indepth);
910 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
915 len = strlen (hp->value.cpval);
916 CPP_RESERVE (pfile, 3 + len);
917 CPP_PUTC_Q (pfile, '"');
918 CPP_PUTS_Q (pfile, hp->value.cpval, len);
919 CPP_PUTC_Q (pfile, '"');
920 CPP_NUL_TERMINATE_Q (pfile);
924 buf = hp->value.cpval;
931 CPP_RESERVE (pfile, len + 1);
932 CPP_PUTS_Q (pfile, buf, len);
933 CPP_NUL_TERMINATE_Q (pfile);
937 CPP_RESERVE (pfile, 2);
938 #ifdef STDC_0_IN_SYSTEM_HEADERS
939 ip = cpp_file_buffer (pfile);
940 if (ip->system_header_p
941 && !cpp_defined (pfile, (const U_CHAR *) "__STRICT_ANSI__", 15))
942 CPP_PUTC_Q (pfile, '0');
945 CPP_PUTC_Q (pfile, '1');
946 CPP_NUL_TERMINATE_Q (pfile);
952 cpp_buf_line_and_col (cpp_file_buffer (pfile), &line, NULL);
954 CPP_RESERVE (pfile, 10);
955 sprintf (CPP_PWRITTEN (pfile), "%ld", line);
956 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
965 CPP_RESERVE (pfile, 20);
966 timebuf = timestamp (pfile);
967 if (hp->type == T_DATE)
968 sprintf (CPP_PWRITTEN (pfile), "\"%s %2d %4d\"",
969 monthnames[timebuf->tm_mon],
970 timebuf->tm_mday, timebuf->tm_year + 1900);
972 sprintf (CPP_PWRITTEN (pfile), "\"%02d:%02d:%02d\"",
973 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
975 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
980 cpp_error (pfile, "attempt to use poisoned `%s'.", hp->name);
981 CPP_RESERVE (pfile, 1);
982 CPP_PUTC_Q (pfile, '0');
983 CPP_NUL_TERMINATE_Q (pfile);
987 cpp_ice (pfile, "invalid special hash type");
992 /* Expand a macro call.
993 HP points to the symbol that is the macro being called.
994 Put the result of expansion onto the input stack
995 so that subsequent input by our caller will use it.
997 If macro wants arguments, caller has already verified that
998 an argument list follows; arguments come from the input stack. */
1001 _cpp_macroexpand (pfile, hp)
1007 register U_CHAR *xbuf;
1008 long start_line, start_column;
1010 struct argdata *args = 0;
1011 long old_written = CPP_WRITTEN (pfile);
1012 int rest_args, rest_zero = 0;
1015 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
1017 /* Check for and handle special symbols. */
1018 if (hp->type != T_MACRO)
1020 special_symbol (hp, pfile);
1021 xbuf_len = CPP_WRITTEN (pfile) - old_written;
1022 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
1023 CPP_SET_WRITTEN (pfile, old_written);
1024 memcpy (xbuf, CPP_PWRITTEN (pfile), xbuf_len + 1);
1025 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
1026 CPP_BUFFER (pfile)->has_escapes = 1;
1030 defn = hp->value.defn;
1031 nargs = defn->nargs;
1032 pfile->output_escapes++;
1036 enum cpp_token token;
1038 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
1040 for (i = 0; i < nargs; i++)
1042 args[i].raw = args[i].expanded = 0;
1043 args[i].raw_length = 0;
1044 args[i].expand_length = args[i].stringified_length = -1;
1047 /* Parse all the macro args that are supplied. I counts them.
1048 The first NARGS args are stored in ARGS.
1049 The rest are discarded. If rest_args is set then we assume
1050 macarg absorbed the rest of the args. */
1054 /* Skip over the opening parenthesis. */
1055 CPP_OPTIONS (pfile)->discard_comments++;
1056 CPP_OPTIONS (pfile)->no_line_commands++;
1057 pfile->no_macro_expand++;
1058 pfile->no_directives++;
1060 token = cpp_get_non_space_token (pfile);
1061 if (token != CPP_LPAREN)
1062 cpp_ice (pfile, "macroexpand: unexpected token %d (wanted LPAREN)",
1064 CPP_ADJUST_WRITTEN (pfile, -1);
1071 if (i < nargs || (nargs == 0 && i == 0))
1073 /* if we are working on last arg which absorbs rest of args... */
1074 if (i == nargs - 1 && defn->rest_args)
1076 args[i].raw = CPP_WRITTEN (pfile);
1077 token = macarg (pfile, rest_args);
1078 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
1081 token = macarg (pfile, 0);
1082 if (token == CPP_EOF || token == CPP_POP)
1083 cpp_error_with_line (pfile, start_line, start_column,
1084 "unterminated macro call");
1087 while (token == CPP_COMMA);
1088 CPP_OPTIONS (pfile)->discard_comments--;
1089 CPP_OPTIONS (pfile)->no_line_commands--;
1090 pfile->no_macro_expand--;
1091 pfile->no_directives--;
1092 if (token != CPP_RPAREN)
1095 /* If we got one arg but it was just whitespace, call that 0 args. */
1098 register U_CHAR *bp = ARG_BASE + args[0].raw;
1099 register U_CHAR *lim = bp + args[0].raw_length;
1100 /* cpp.texi says for foo ( ) we provide one argument.
1101 However, if foo wants just 0 arguments, treat this as 0. */
1103 while (bp != lim && is_space(*bp))
1109 /* Don't output an error message if we have already output one for
1110 a parse error above. */
1112 if (nargs == 0 && i > 0)
1114 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
1118 /* traditional C allows foo() if foo wants one argument. */
1119 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
1121 /* the rest args token is allowed to absorb 0 tokens */
1122 else if (i == nargs - 1 && defn->rest_args)
1125 cpp_error (pfile, "macro `%s' used without args", hp->name);
1127 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
1129 cpp_error (pfile, "macro `%s' used with only %d args",
1135 "macro `%s' used with too many (%d) args", hp->name, i);
1139 /* If macro wants zero args, we parsed the arglist for checking only.
1140 Read directly from the macro definition. */
1143 xbuf = defn->expansion;
1144 xbuf_len = defn->length;
1148 register U_CHAR *exp = defn->expansion;
1149 register int offset; /* offset in expansion,
1150 copied a piece at a time */
1151 register int totlen; /* total amount of exp buffer filled so far */
1153 register struct reflist *ap, *last_ap;
1155 /* Macro really takes args. Compute the expansion of this call. */
1157 /* Compute length in characters of the macro's expansion.
1158 Also count number of times each arg is used. */
1159 xbuf_len = defn->length;
1160 for (ap = defn->pattern; ap != NULL; ap = ap->next)
1164 register struct argdata *arg = &args[ap->argno];
1165 /* Stringify if it hasn't already been */
1166 if (arg->stringified_length < 0)
1168 int arglen = arg->raw_length;
1172 /* Initially need_space is -1. Otherwise, 1 means the
1173 previous character was a space, but we suppressed it;
1174 0 means the previous character was a non-space. */
1175 int need_space = -1;
1177 arg->stringified = CPP_WRITTEN (pfile);
1178 if (!CPP_TRADITIONAL (pfile))
1179 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
1180 for (; i < arglen; i++)
1182 c = (ARG_BASE + arg->raw)[i];
1186 /* Delete "\r " and "\r-" escapes. */
1192 /* Internal sequences of whitespace are
1193 replaced by one space except within
1194 a string or char token. */
1195 else if (is_space(c))
1197 if (need_space == 0)
1201 else if (need_space > 0)
1202 CPP_PUTC (pfile, ' ');
1217 else if (c == '\"' || c == '\'')
1221 /* Escape these chars */
1222 if (c == '\"' || (in_string && c == '\\'))
1223 CPP_PUTC (pfile, '\\');
1225 CPP_PUTC (pfile, c);
1228 CPP_RESERVE (pfile, 4);
1229 sprintf ((char *) CPP_PWRITTEN (pfile), "\\%03o",
1231 CPP_ADJUST_WRITTEN (pfile, 4);
1234 if (!CPP_TRADITIONAL (pfile))
1235 CPP_PUTC (pfile, '\"'); /* insert ending quote */
1236 arg->stringified_length
1237 = CPP_WRITTEN (pfile) - arg->stringified;
1239 xbuf_len += args[ap->argno].stringified_length;
1241 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
1242 /* Add 4 for two \r-space markers to prevent
1243 token concatenation. */
1244 xbuf_len += args[ap->argno].raw_length + 4;
1247 /* We have an ordinary (expanded) occurrence of the arg.
1248 So compute its expansion, if we have not already. */
1249 if (args[ap->argno].expand_length < 0)
1251 args[ap->argno].expanded = CPP_WRITTEN (pfile);
1252 cpp_expand_to_buffer (pfile,
1253 ARG_BASE + args[ap->argno].raw,
1254 args[ap->argno].raw_length);
1256 args[ap->argno].expand_length
1257 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
1260 /* Add 4 for two \r-space markers to prevent
1261 token concatenation. */
1262 xbuf_len += args[ap->argno].expand_length + 4;
1266 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
1268 /* Generate in XBUF the complete expansion
1269 with arguments substituted in.
1270 TOTLEN is the total size generated so far.
1271 OFFSET is the index in the definition
1272 of where we are copying from. */
1273 offset = totlen = 0;
1274 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
1275 last_ap = ap, ap = ap->next)
1277 register struct argdata *arg = &args[ap->argno];
1278 int count_before = totlen;
1280 /* Add chars to XBUF. */
1282 memcpy (&xbuf[totlen], &exp[offset], i);
1286 /* If followed by an empty rest arg with concatenation,
1287 delete the last run of nonwhite chars. */
1288 if (rest_zero && totlen > count_before
1289 && ((ap->rest_args && ap->raw_before)
1290 || (last_ap != NULL && last_ap->rest_args
1291 && last_ap->raw_after)))
1293 /* Delete final whitespace. */
1294 while (totlen > count_before && is_space(xbuf[totlen - 1]))
1297 /* Delete the nonwhites before them. */
1298 while (totlen > count_before && !is_space(xbuf[totlen - 1]))
1302 if (ap->stringify != 0)
1304 memcpy (xbuf + totlen, ARG_BASE + arg->stringified,
1305 arg->stringified_length);
1306 totlen += arg->stringified_length;
1308 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
1310 U_CHAR *p1 = ARG_BASE + arg->raw;
1311 U_CHAR *l1 = p1 + arg->raw_length;
1314 /* Arg is concatenated before: delete leading whitespace,
1315 whitespace markers, and no-reexpansion markers. */
1318 if (is_space(p1[0]))
1320 else if (p1[0] == '\r')
1328 /* Arg is concatenated after: delete trailing whitespace,
1329 whitespace markers, and no-reexpansion markers. */
1332 if (is_space(l1[-1]))
1334 else if (l1[-1] == '\r')
1336 else if (l1[-1] == '-')
1338 if (l1 != p1 + 1 && l1[-2] == '\r')
1348 /* Delete any no-reexpansion marker that precedes
1349 an identifier at the beginning of the argument. */
1350 if (p1[0] == '\r' && p1[1] == '-')
1353 memcpy (xbuf + totlen, p1, l1 - p1);
1358 U_CHAR *expanded = ARG_BASE + arg->expanded;
1359 if (!ap->raw_before && totlen > 0 && arg->expand_length
1360 && !CPP_TRADITIONAL (pfile)
1361 && unsafe_chars (pfile, xbuf[totlen - 1], expanded[0]))
1363 xbuf[totlen++] = '\r';
1364 xbuf[totlen++] = ' ';
1367 memcpy (xbuf + totlen, expanded, arg->expand_length);
1368 totlen += arg->expand_length;
1370 if (!ap->raw_after && totlen > 0 && offset < defn->length
1371 && !CPP_TRADITIONAL (pfile)
1372 && unsafe_chars (pfile, xbuf[totlen - 1], exp[offset]))
1374 xbuf[totlen++] = '\r';
1375 xbuf[totlen++] = ' ';
1379 if (totlen > xbuf_len)
1381 cpp_ice (pfile, "buffer overrun in macroexpand");
1386 /* if there is anything left of the definition
1387 after handling the arg list, copy that in too. */
1389 for (i = offset; i < defn->length; i++)
1391 /* if we've reached the end of the macro */
1394 if (!(rest_zero && last_ap != NULL && last_ap->rest_args
1395 && last_ap->raw_after))
1396 xbuf[totlen++] = exp[i];
1404 pfile->output_escapes--;
1406 /* Now put the expansion on the input stack
1407 so our caller will commence reading from it. */
1408 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
1409 CPP_BUFFER (pfile)->has_escapes = 1;
1411 /* Pop the space we've used in the token_buffer for argument expansion. */
1412 CPP_SET_WRITTEN (pfile, old_written);
1414 /* Recursive macro use sometimes works traditionally.
1415 #define foo(x,y) bar (x (y,0), y)
1418 if (!CPP_TRADITIONAL (pfile))
1419 hp->type = T_DISABLED;
1422 /* Return 1 iff a token ending in C1 followed directly by a token C2
1423 could cause mis-tokenization. */
1426 unsafe_chars (pfile, c1, c2)
1433 if (c2 == c1 || c2 == '=')
1437 case 'e': case 'E': case 'p': case 'P':
1438 if (c2 == '-' || c2 == '+')
1439 return 1; /* could extend a pre-processing number */
1443 if (CPP_OPTIONS (pfile)->dollars_in_ident)
1448 if (c2 == '\'' || c2 == '\"')
1449 return 1; /* Could turn into L"xxx" or L'xxx'. */
1452 case '.': case '0': case '1': case '2': case '3':
1453 case '4': case '5': case '6': case '7': case '8': case '9':
1454 case '_': case 'a': case 'b': case 'c': case 'd': case 'f':
1455 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1456 case 'm': case 'n': case 'o': case 'q': case 'r': case 's':
1457 case 't': case 'u': case 'v': case 'w': case 'x': case 'y':
1458 case 'z': case 'A': case 'B': case 'C': case 'D': case 'F':
1459 case 'G': case 'H': case 'I': case 'J': case 'K': case 'M':
1460 case 'N': case 'O': case 'Q': case 'R': case 'S': case 'T':
1461 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
1463 /* We're in the middle of either a name or a pre-processing number. */
1464 return (is_idchar(c2) || c2 == '.');
1466 case '<': case '>': case '!': case '%': case '#': case ':':
1467 case '^': case '&': case '|': case '*': case '/': case '=':
1468 return (c2 == c1 || c2 == '=');
1474 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
1476 register U_CHAR *xbuf;
1480 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
1483 mbuf->cleanup = macro_cleanup;
1486 /* The first chars of the expansion should be a "\r " added by
1487 collect_expansion. This is to prevent accidental token-pasting
1488 between the text preceding the macro invocation, and the macro
1491 We would like to avoid adding unneeded spaces (for the sake of
1492 tools that use cpp, such as imake). In some common cases we can
1493 tell that it is safe to omit the space.
1495 The character before the macro invocation cannot have been an
1496 idchar (or else it would have been pasted with the idchars of
1497 the macro name). Therefore, if the first non-space character
1498 of the expansion is an idchar, we do not need the extra space
1499 to prevent token pasting.
1501 Also, we don't need the extra space if the first char is '(',
1502 or some other (less common) characters. */
1504 if (xbuf[0] == '\r' && xbuf[1] == ' '
1505 && (is_idchar(xbuf[2]) || xbuf[2] == '(' || xbuf[2] == '\''
1506 || xbuf[2] == '\"'))
1509 /* Likewise, avoid the extra space at the end of the macro expansion
1510 if this is safe. We can do a better job here since we can know
1511 what the next char will be. */
1513 && mbuf->rlimit[-2] == '\r'
1514 && mbuf->rlimit[-1] == ' ')
1516 int c1 = mbuf->rlimit[-3];
1517 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
1518 if (c2 == EOF || !unsafe_chars (pfile, c1, c2))
1523 /* Return zero if two DEFINITIONs are isomorphic. */
1526 _cpp_compare_defs (pfile, d1, d2)
1528 DEFINITION *d1, *d2;
1530 struct reflist *a1, *a2;
1531 U_CHAR *p1 = d1->expansion;
1532 U_CHAR *p2 = d2->expansion;
1535 if (d1->nargs != d2->nargs)
1537 if (CPP_PEDANTIC (pfile)
1538 && d1->argnames && d2->argnames)
1540 U_CHAR *arg1 = d1->argnames;
1541 U_CHAR *arg2 = d2->argnames;
1546 len = strlen (arg1) + 1;
1547 if (strcmp (arg1, arg2))
1553 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1554 a1 = a1->next, a2 = a2->next)
1556 if (!((a1->nchars == a2->nchars && !strncmp (p1, p2, a1->nchars))
1557 || !comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1558 || a1->argno != a2->argno
1559 || a1->stringify != a2->stringify
1560 || a1->raw_before != a2->raw_before
1561 || a1->raw_after != a2->raw_after)
1570 return comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1571 p2, d2->length - (p2 - d2->expansion), 1);
1574 /* Return 1 if two parts of two macro definitions are effectively different.
1575 One of the parts starts at BEG1 and has LEN1 chars;
1576 the other has LEN2 chars at BEG2.
1577 Any sequence of whitespace matches any other sequence of whitespace.
1578 FIRST means these parts are the first of a macro definition;
1579 so ignore leading whitespace entirely.
1580 LAST means these parts are the last of a macro definition;
1581 so ignore trailing whitespace entirely. */
1584 comp_def_part (first, beg1, len1, beg2, len2, last)
1586 U_CHAR *beg1, *beg2;
1590 register U_CHAR *end1 = beg1 + len1;
1591 register U_CHAR *end2 = beg2 + len2;
1594 while (beg1 != end1 && is_space(*beg1))
1596 while (beg2 != end2 && is_space(*beg2))
1601 while (beg1 != end1 && is_space(end1[-1]))
1603 while (beg2 != end2 && is_space(end2[-1]))
1606 while (beg1 != end1 && beg2 != end2)
1608 if (is_space(*beg1) && is_space(*beg2))
1610 while (beg1 != end1 && is_space(*beg1))
1612 while (beg2 != end2 && is_space(*beg2))
1615 else if (*beg1 == *beg2)
1623 return (beg1 != end1) || (beg2 != end2);
1626 /* Dump the definition of macro MACRO on stdout. The format is suitable
1627 to be read back in again. */
1630 _cpp_dump_definition (pfile, sym, len, defn)
1636 if (pfile->lineno == 0)
1637 _cpp_output_line_command (pfile, same_file);
1639 CPP_RESERVE (pfile, len + sizeof "#define ");
1640 CPP_PUTS_Q (pfile, "#define ", sizeof "#define " -1);
1641 CPP_PUTS_Q (pfile, sym, len);
1643 if (defn->nargs == -1)
1645 CPP_PUTC_Q (pfile, ' ');
1647 /* The first and last two characters of a macro expansion are
1648 always "\r "; this needs to be trimmed out.
1649 So we need length-4 chars of space, plus one for the NUL. */
1650 CPP_RESERVE (pfile, defn->length - 4 + 1);
1651 CPP_PUTS_Q (pfile, defn->expansion + 2, defn->length - 4);
1656 unsigned char **argv = (unsigned char **) alloca (defn->nargs *
1658 int *argl = (int *) alloca (defn->nargs * sizeof(int));
1662 /* First extract the argument list. */
1664 for (i = 0; i < defn->nargs; i++)
1667 argl[i] = strlen (x);
1671 /* Now print out the argument list. */
1672 CPP_PUTC_Q (pfile, '(');
1673 for (i = 0; i < defn->nargs; i++)
1675 CPP_RESERVE (pfile, argl[i] + 2);
1676 if (!(i == defn->nargs-1 && defn->rest_args
1677 && !strcmp (argv[i], "__VA_ARGS__")))
1678 CPP_PUTS_Q (pfile, argv[i], argl[i]);
1679 if (i < defn->nargs-1)
1680 CPP_PUTS_Q (pfile, ", ", 2);
1682 if (defn->rest_args)
1683 CPP_PUTS (pfile, "...", 3);
1684 CPP_PUTS (pfile, ") ", 2);
1686 /* Now the definition. */
1687 x = defn->expansion;
1688 for (r = defn->pattern; r; r = r->next)
1691 if (*x == '\r') x += 2, i -= 2;
1692 /* i chars for macro text, plus the length of the macro
1693 argument name, plus one for a stringify marker, plus two for
1694 each concatenation marker. */
1696 i + argl[r->argno] + r->stringify
1697 + (r->raw_before + r->raw_after) * 2);
1699 if (i > 0) CPP_PUTS_Q (pfile, x, i);
1701 CPP_PUTS_Q (pfile, "##", 2);
1703 CPP_PUTC_Q (pfile, '#');
1704 CPP_PUTS_Q (pfile, argv[r->argno], argl[r->argno]);
1705 if (r->raw_after && !(r->next && r->next->nchars == 0
1706 && r->next->raw_before))
1707 CPP_PUTS_Q (pfile, "##", 2);
1712 i = defn->length - (x - defn->expansion) - 2;
1713 if (*x == '\r') x += 2, i -= 2;
1714 if (i > 0) CPP_PUTS (pfile, x, i);
1716 if (pfile->lineno == 0)
1717 CPP_PUTC (pfile, '\n');
1718 CPP_NUL_TERMINATE (pfile);
1721 /* Dump out the hash table. */
1723 dump_hash_helper (h, p)
1727 HASHNODE *hp = (HASHNODE *)h;
1728 cpp_reader *pfile = (cpp_reader *)p;
1730 _cpp_dump_definition (pfile, hp->name, hp->length, hp->value.defn);
1735 _cpp_dump_macro_hash (pfile)
1738 htab_traverse (pfile->hashtab, dump_hash_helper, pfile);