]>
Commit | Line | Data |
---|---|---|
6de1e2a9 | 1 | /* Part of CPP library. (Macro handling.) |
5e7b4e25 JL |
2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998, |
3 | 1999, 2000 Free Software Foundation, Inc. | |
7f2935c7 | 4 | Written by Per Bothner, 1994. |
38e01259 | 5 | Based on CCCP program by Paul Rubin, June 1986 |
7f2935c7 PB |
6 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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 | |
940d9d63 | 20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
7f2935c7 PB |
21 | |
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! */ | |
25 | ||
b04cd507 KG |
26 | #include "config.h" |
27 | #include "system.h" | |
7f2935c7 PB |
28 | #include "cpplib.h" |
29 | #include "cpphash.h" | |
d35364d1 | 30 | #include "hashtab.h" |
34ca9541 | 31 | #undef abort |
7f2935c7 | 32 | |
d35364d1 ZW |
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 *)); | |
1a7b4c69 | 36 | static int dump_hash_helper PARAMS ((void **, void *)); |
d35364d1 | 37 | |
6de1e2a9 ZW |
38 | static int comp_def_part PARAMS ((int, U_CHAR *, int, U_CHAR *, |
39 | int, int)); | |
6de1e2a9 ZW |
40 | static void push_macro_expansion PARAMS ((cpp_reader *, |
41 | U_CHAR *, int, HASHNODE *)); | |
455d2586 | 42 | static int unsafe_chars PARAMS ((cpp_reader *, int, int)); |
bcc5cac9 KG |
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 *)); | |
47 | ||
d35364d1 ZW |
48 | /* Initial hash table size. (It can grow if necessary - see hashtab.c.) */ |
49 | #define HASHSIZE 500 | |
50 | ||
6de1e2a9 ZW |
51 | /* The arglist structure is built by create_definition to tell |
52 | collect_expansion where the argument names begin. That | |
53 | is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist | |
54 | would contain pointers to the strings x, y, and z. | |
55 | collect_expansion would then build a DEFINITION node, | |
56 | with reflist nodes pointing to the places x, y, and z had | |
57 | appeared. So the arglist is just convenience data passed | |
58 | between these two routines. It is not kept around after | |
59 | the current #define has been processed and entered into the | |
60 | hash table. */ | |
61 | ||
ba412f14 | 62 | struct arg |
6de1e2a9 | 63 | { |
6de1e2a9 | 64 | U_CHAR *name; |
ba412f14 ZW |
65 | int len; |
66 | char rest_arg; | |
67 | }; | |
68 | ||
69 | struct arglist | |
70 | { | |
71 | U_CHAR *namebuf; | |
72 | struct arg *argv; | |
73 | int argc; | |
6de1e2a9 ZW |
74 | }; |
75 | ||
ba412f14 ZW |
76 | |
77 | static DEFINITION *collect_expansion PARAMS ((cpp_reader *, struct arglist *)); | |
78 | static struct arglist *collect_formal_parameters PARAMS ((cpp_reader *)); | |
bcc5cac9 | 79 | |
6de1e2a9 ZW |
80 | /* This structure represents one parsed argument in a macro call. |
81 | `raw' points to the argument text as written (`raw_length' is its length). | |
82 | `expanded' points to the argument's macro-expansion | |
83 | (its length is `expand_length'). | |
84 | `stringified_length' is the length the argument would have | |
4571dc27 | 85 | if stringified. */ |
6de1e2a9 ZW |
86 | |
87 | /* raw and expanded are relative to ARG_BASE */ | |
88 | #define ARG_BASE ((pfile)->token_buffer) | |
89 | ||
90 | struct argdata | |
91 | { | |
92 | /* Strings relative to pfile->token_buffer */ | |
93 | long raw, expanded, stringified; | |
94 | int raw_length, expand_length; | |
95 | int stringified_length; | |
6de1e2a9 ZW |
96 | }; |
97 | ||
29a72a4f ZW |
98 | /* Calculate hash of a string of length LEN. */ |
99 | unsigned int | |
100 | _cpp_calc_hash (str, len) | |
101 | const U_CHAR *str; | |
102 | size_t len; | |
7f2935c7 | 103 | { |
29a72a4f ZW |
104 | size_t n = len; |
105 | unsigned int r = 0; | |
7f2935c7 | 106 | |
bb52fa7f | 107 | do |
29a72a4f | 108 | r = r * 67 + (*str++ - 113); |
bb52fa7f ZW |
109 | while (--n); |
110 | return r + len; | |
7f2935c7 PB |
111 | } |
112 | ||
29a72a4f ZW |
113 | /* Calculate hash of a HASHNODE structure. */ |
114 | static unsigned int | |
115 | hash_HASHNODE (x) | |
116 | const void *x; | |
117 | { | |
118 | const HASHNODE *h = (const HASHNODE *)x; | |
119 | return h->hash; | |
120 | } | |
121 | ||
d35364d1 ZW |
122 | /* Compare two HASHNODE structures. */ |
123 | static int | |
124 | eq_HASHNODE (x, y) | |
125 | const void *x; | |
126 | const void *y; | |
127 | { | |
128 | const HASHNODE *a = (const HASHNODE *)x; | |
129 | const HASHNODE *b = (const HASHNODE *)y; | |
130 | ||
131 | return (a->length == b->length | |
132 | && !strncmp (a->name, b->name, a->length)); | |
133 | } | |
134 | ||
135 | /* Destroy a HASHNODE. */ | |
136 | static void | |
137 | del_HASHNODE (x) | |
138 | void *x; | |
139 | { | |
140 | HASHNODE *h = (HASHNODE *)x; | |
141 | ||
142 | if (h->type == T_MACRO) | |
143 | _cpp_free_definition (h->value.defn); | |
144 | free ((void *) h->name); | |
145 | free (h); | |
146 | } | |
147 | ||
148 | /* Allocate and initialize a HASHNODE structure. | |
149 | Caller must fill in the value field. */ | |
150 | ||
151 | HASHNODE * | |
152 | _cpp_make_hashnode (name, len, type, hash) | |
153 | const U_CHAR *name; | |
154 | size_t len; | |
155 | enum node_type type; | |
156 | unsigned long hash; | |
157 | { | |
158 | HASHNODE *hp = (HASHNODE *) xmalloc (sizeof (HASHNODE)); | |
159 | U_CHAR *p = xmalloc (len + 1); | |
160 | ||
161 | hp->type = type; | |
162 | hp->length = len; | |
163 | hp->name = p; | |
164 | hp->hash = hash; | |
165 | ||
166 | memcpy (p, name, len); | |
167 | p[len] = 0; | |
168 | ||
169 | return hp; | |
170 | } | |
171 | ||
172 | /* Find the hash node for name "name", which ends at the first | |
173 | non-identifier char. | |
0f41302f MS |
174 | |
175 | If LEN is >= 0, it is the length of the name. | |
d35364d1 | 176 | Otherwise, compute the length now. */ |
0f41302f | 177 | |
7f2935c7 | 178 | HASHNODE * |
b0699dad | 179 | _cpp_lookup (pfile, name, len) |
bb52fa7f | 180 | cpp_reader *pfile; |
7f2935c7 PB |
181 | const U_CHAR *name; |
182 | int len; | |
7f2935c7 | 183 | { |
d35364d1 ZW |
184 | const U_CHAR *bp; |
185 | HASHNODE dummy; | |
7f2935c7 PB |
186 | |
187 | if (len < 0) | |
188 | { | |
203588e7 | 189 | for (bp = name; is_idchar (*bp); bp++); |
7f2935c7 PB |
190 | len = bp - name; |
191 | } | |
192 | ||
d35364d1 ZW |
193 | dummy.name = name; |
194 | dummy.length = len; | |
29a72a4f | 195 | dummy.hash = _cpp_calc_hash (name, len); |
d35364d1 | 196 | |
29a72a4f ZW |
197 | return (HASHNODE *) htab_find_with_hash (pfile->hashtab, |
198 | (void *)&dummy, dummy.hash); | |
d35364d1 ZW |
199 | } |
200 | ||
201 | /* Find the hashtable slot for name "name". Used to insert or delete. */ | |
202 | HASHNODE ** | |
203 | _cpp_lookup_slot (pfile, name, len, insert, hash) | |
204 | cpp_reader *pfile; | |
205 | const U_CHAR *name; | |
206 | int len; | |
207 | int insert; | |
208 | unsigned long *hash; | |
209 | { | |
210 | const U_CHAR *bp; | |
211 | HASHNODE dummy; | |
212 | HASHNODE **slot; | |
7f2935c7 | 213 | |
d35364d1 | 214 | if (len < 0) |
6de1e2a9 | 215 | { |
d35364d1 ZW |
216 | for (bp = name; is_idchar (*bp); bp++); |
217 | len = bp - name; | |
6de1e2a9 | 218 | } |
d35364d1 ZW |
219 | |
220 | dummy.name = name; | |
221 | dummy.length = len; | |
29a72a4f | 222 | dummy.hash = _cpp_calc_hash (name, len); |
d35364d1 | 223 | |
29a72a4f ZW |
224 | slot = (HASHNODE **) htab_find_slot_with_hash (pfile->hashtab, |
225 | (void *)&dummy, | |
226 | dummy.hash, insert); | |
d35364d1 ZW |
227 | if (insert) |
228 | *hash = dummy.hash; | |
229 | return slot; | |
230 | } | |
231 | ||
232 | /* Init the hash table. In here so it can see the hash and eq functions. */ | |
233 | void | |
234 | _cpp_init_macro_hash (pfile) | |
235 | cpp_reader *pfile; | |
236 | { | |
237 | pfile->hashtab = htab_create (HASHSIZE, hash_HASHNODE, | |
238 | eq_HASHNODE, del_HASHNODE); | |
7f2935c7 PB |
239 | } |
240 | ||
cf4ed945 ZW |
241 | /* Free a DEFINITION structure. Used by delete_macro, and by |
242 | do_define when redefining macros. */ | |
243 | ||
244 | void | |
b0699dad | 245 | _cpp_free_definition (d) |
cf4ed945 ZW |
246 | DEFINITION *d; |
247 | { | |
248 | struct reflist *ap, *nextap; | |
249 | ||
250 | for (ap = d->pattern; ap != NULL; ap = nextap) | |
251 | { | |
252 | nextap = ap->next; | |
253 | free (ap); | |
254 | } | |
05ecd0e9 | 255 | if (d->argnames) |
cf4ed945 ZW |
256 | free (d->argnames); |
257 | free (d); | |
258 | } | |
259 | ||
6de1e2a9 ZW |
260 | static int |
261 | macro_cleanup (pbuf, pfile) | |
262 | cpp_buffer *pbuf; | |
263 | cpp_reader *pfile ATTRIBUTE_UNUSED; | |
264 | { | |
ff2b53ef | 265 | HASHNODE *macro = pbuf->macro; |
6de1e2a9 ZW |
266 | if (macro->type == T_DISABLED) |
267 | macro->type = T_MACRO; | |
268 | if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion) | |
7ceb3598 | 269 | free ((PTR) pbuf->buf); |
6de1e2a9 ZW |
270 | return 0; |
271 | } | |
272 | ||
ba412f14 ZW |
273 | /* Read a replacement list for a macro, and build the DEFINITION |
274 | structure. ARGLIST specifies the formal parameters to look for in | |
275 | the text of the definition. If ARGLIST is null, this is an | |
276 | object-like macro; if it points to an empty arglist, this is a | |
277 | function-like macro with no arguments. | |
278 | ||
279 | A good half of this is devoted to supporting -traditional. | |
280 | Kill me now. */ | |
6de1e2a9 ZW |
281 | |
282 | static DEFINITION * | |
ba412f14 | 283 | collect_expansion (pfile, arglist) |
6de1e2a9 | 284 | cpp_reader *pfile; |
6de1e2a9 ZW |
285 | struct arglist *arglist; |
286 | { | |
287 | DEFINITION *defn; | |
ba412f14 ZW |
288 | struct reflist *pat = 0, *endpat = 0; |
289 | enum cpp_token token; | |
290 | long start, here, last; | |
291 | int i; | |
292 | int argc; | |
293 | size_t len; | |
294 | struct arg *argv; | |
295 | U_CHAR *tok, *exp; | |
296 | enum { START = 0, NORM, ARG, STRIZE, PASTE } last_token = START; | |
297 | ||
298 | if (arglist) | |
34ca9541 | 299 | { |
ba412f14 ZW |
300 | argv = arglist->argv; |
301 | argc = arglist->argc; | |
34ca9541 | 302 | } |
ba412f14 | 303 | else |
6de1e2a9 | 304 | { |
ba412f14 ZW |
305 | argv = 0; |
306 | argc = 0; | |
6de1e2a9 ZW |
307 | } |
308 | ||
ba412f14 ZW |
309 | last = start = CPP_WRITTEN (pfile); |
310 | last -= 2; /* two extra chars for the leading escape */ | |
311 | for (;;) | |
6de1e2a9 | 312 | { |
ff2b53ef ZW |
313 | /* Macro expansion is off, so we are guaranteed not to see POP |
314 | or EOF. */ | |
ba412f14 | 315 | here = CPP_WRITTEN (pfile); |
ff2b53ef | 316 | token = _cpp_get_define_token (pfile); |
ba412f14 ZW |
317 | tok = pfile->token_buffer + here; |
318 | switch (token) | |
6de1e2a9 | 319 | { |
ba412f14 ZW |
320 | case CPP_POP: |
321 | case CPP_EOF: | |
ff2b53ef ZW |
322 | cpp_ice (pfile, "EOF in collect_expansion"); |
323 | /* fall through */ | |
ba412f14 | 324 | case CPP_VSPACE: |
ba412f14 ZW |
325 | goto done; |
326 | ||
327 | case CPP_HSPACE: | |
328 | if (last_token == STRIZE || last_token == PASTE | |
329 | || last_token == START) | |
330 | CPP_SET_WRITTEN (pfile, here); | |
331 | break; | |
6de1e2a9 | 332 | |
ba412f14 | 333 | case CPP_STRINGIZE: |
29a72a4f ZW |
334 | /* # is not special in object-like macros. It is special in |
335 | function-like macros with no args. (6.10.3.2 para 1.) */ | |
336 | if (arglist == NULL) | |
337 | goto norm; | |
338 | /* # is not special immediately after PASTE. | |
339 | (Implied by 6.10.3.3 para 4.) */ | |
ba412f14 | 340 | if (last_token == PASTE) |
ba412f14 ZW |
341 | goto norm; |
342 | last_token = STRIZE; | |
343 | CPP_SET_WRITTEN (pfile, here); /* delete from replacement text */ | |
344 | break; | |
6de1e2a9 | 345 | |
ba412f14 ZW |
346 | case CPP_TOKPASTE: |
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) | |
351 | { | |
352 | endpat->raw_after = 1; | |
353 | last_token = PASTE; | |
354 | CPP_SET_WRITTEN (pfile, last); | |
6de1e2a9 ZW |
355 | break; |
356 | } | |
ba412f14 ZW |
357 | else if (last_token == PASTE) |
358 | /* ## ## - the second ## is ordinary. */ | |
359 | goto norm; | |
7d26bd16 JM |
360 | else if (last_token == START) |
361 | cpp_error (pfile, "`##' at start of macro definition"); | |
ba412f14 ZW |
362 | |
363 | /* Discard the token and any hspace before it. */ | |
364 | while (is_hspace (pfile->token_buffer[here-1])) | |
365 | here--; | |
366 | CPP_SET_WRITTEN (pfile, here); | |
367 | ||
368 | if (last_token == STRIZE) | |
369 | /* Oops - that wasn't a stringify operator. */ | |
370 | CPP_PUTC (pfile, '#'); | |
371 | last_token = PASTE; | |
372 | break; | |
6de1e2a9 | 373 | |
ba412f14 ZW |
374 | case CPP_COMMENT: |
375 | /* We must be in -traditional mode. Pretend this was a | |
376 | token paste, but only if there was no leading or | |
ff2b53ef ZW |
377 | trailing space and it's in the middle of the line. |
378 | _cpp_lex_token won't return a COMMENT if there was trailing | |
379 | space. */ | |
ba412f14 | 380 | CPP_SET_WRITTEN (pfile, here); |
29a72a4f ZW |
381 | if (last_token == START) |
382 | break; | |
ba412f14 ZW |
383 | if (is_hspace (pfile->token_buffer[here-1])) |
384 | break; | |
ba412f14 ZW |
385 | if (last_token == ARG) |
386 | endpat->raw_after = 1; | |
387 | last_token = PASTE; | |
388 | break; | |
6de1e2a9 | 389 | |
ba412f14 ZW |
390 | case CPP_STRING: |
391 | case CPP_CHAR: | |
392 | if (last_token == STRIZE) | |
393 | cpp_error (pfile, "`#' is not followed by a macro argument name"); | |
6de1e2a9 | 394 | |
07aa0b04 | 395 | if (CPP_TRADITIONAL (pfile) || CPP_WTRADITIONAL (pfile)) |
ba412f14 ZW |
396 | goto maybe_trad_stringify; |
397 | else | |
398 | goto norm; | |
399 | ||
400 | case CPP_NAME: | |
401 | for (i = 0; i < argc; i++) | |
402 | if (!strncmp (tok, argv[i].name, argv[i].len) | |
403 | && ! is_idchar (tok[argv[i].len])) | |
404 | goto addref; | |
405 | ||
406 | /* fall through */ | |
407 | default: | |
408 | norm: | |
409 | if (last_token == STRIZE) | |
410 | cpp_error (pfile, "`#' is not followed by a macro argument name"); | |
411 | last_token = NORM; | |
412 | break; | |
413 | } | |
414 | continue; | |
6de1e2a9 | 415 | |
ba412f14 ZW |
416 | addref: |
417 | { | |
418 | struct reflist *tpat; | |
419 | ||
420 | /* Make a pat node for this arg and add it to the pat list */ | |
421 | tpat = (struct reflist *) xmalloc (sizeof (struct reflist)); | |
422 | tpat->next = NULL; | |
423 | tpat->raw_before = (last_token == PASTE); | |
424 | tpat->raw_after = 0; | |
425 | tpat->stringify = (last_token == STRIZE); | |
426 | tpat->rest_args = argv[i].rest_arg; | |
427 | tpat->argno = i; | |
428 | tpat->nchars = here - last; | |
429 | ||
430 | if (endpat == NULL) | |
431 | pat = tpat; | |
432 | else | |
433 | endpat->next = tpat; | |
434 | endpat = tpat; | |
435 | last = here; | |
436 | } | |
437 | CPP_SET_WRITTEN (pfile, here); /* discard arg name */ | |
438 | last_token = ARG; | |
439 | continue; | |
6de1e2a9 | 440 | |
ba412f14 | 441 | maybe_trad_stringify: |
c45da1ca | 442 | last_token = NORM; |
ba412f14 ZW |
443 | { |
444 | U_CHAR *base, *p, *limit; | |
445 | struct reflist *tpat; | |
6de1e2a9 | 446 | |
ba412f14 ZW |
447 | base = p = pfile->token_buffer + here; |
448 | limit = CPP_PWRITTEN (pfile); | |
449 | ||
450 | while (++p < limit) | |
451 | { | |
452 | if (is_idstart (*p)) | |
453 | continue; | |
454 | for (i = 0; i < argc; i++) | |
455 | if (!strncmp (tok, argv[i].name, argv[i].len) | |
456 | && ! is_idchar (tok[argv[i].len])) | |
457 | goto mts_addref; | |
458 | continue; | |
459 | ||
460 | mts_addref: | |
461 | if (!CPP_TRADITIONAL (pfile)) | |
462 | { | |
463 | /* Must have got here because of -Wtraditional. */ | |
464 | cpp_warning (pfile, | |
465 | "macro argument `%.*s' would be stringified with -traditional", | |
466 | (int) argv[i].len, argv[i].name); | |
467 | continue; | |
468 | } | |
07aa0b04 | 469 | if (CPP_WTRADITIONAL (pfile)) |
ba412f14 ZW |
470 | cpp_warning (pfile, "macro argument `%.*s' is stringified", |
471 | (int) argv[i].len, argv[i].name); | |
472 | ||
473 | /* Remove the argument from the string. */ | |
474 | memmove (p, p + argv[i].len, limit - (p + argv[i].len)); | |
475 | limit -= argv[i].len; | |
476 | ||
477 | /* Make a pat node for this arg and add it to the pat list */ | |
478 | tpat = (struct reflist *) xmalloc (sizeof (struct reflist)); | |
479 | tpat->next = NULL; | |
480 | ||
481 | /* Don't attempt to paste this with anything. */ | |
482 | tpat->raw_before = 0; | |
483 | tpat->raw_after = 0; | |
484 | tpat->stringify = 1; | |
485 | tpat->rest_args = argv[i].rest_arg; | |
486 | tpat->argno = i; | |
487 | tpat->nchars = (p - base) + here - last; | |
488 | ||
489 | if (endpat == NULL) | |
490 | pat = tpat; | |
491 | else | |
492 | endpat->next = tpat; | |
493 | endpat = tpat; | |
494 | last = (p - base) + here; | |
495 | } | |
496 | CPP_ADJUST_WRITTEN (pfile, CPP_PWRITTEN (pfile) - limit); | |
497 | } | |
498 | } | |
499 | done: | |
500 | ||
501 | if (last_token == STRIZE) | |
502 | cpp_error (pfile, "`#' is not followed by a macro argument name"); | |
503 | else if (last_token == PASTE) | |
504 | cpp_error (pfile, "`##' at end of macro definition"); | |
505 | ||
c45da1ca ZW |
506 | if (last_token == START) |
507 | { | |
508 | /* Empty macro definition. */ | |
7ceb3598 | 509 | exp = (U_CHAR *) xstrdup ("\r \r "); |
c45da1ca ZW |
510 | len = 1; |
511 | } | |
512 | else | |
513 | { | |
514 | /* Trim trailing white space from definition. */ | |
515 | here = CPP_WRITTEN (pfile); | |
516 | while (here > last && is_hspace (pfile->token_buffer [here-1])) | |
517 | here--; | |
518 | CPP_SET_WRITTEN (pfile, here); | |
c45da1ca ZW |
519 | CPP_NUL_TERMINATE (pfile); |
520 | len = CPP_WRITTEN (pfile) - start + 1; | |
7ceb3598 NB |
521 | /* space for no-concat markers at either end */ |
522 | exp = (U_CHAR *) xmalloc (len + 4); | |
c45da1ca ZW |
523 | exp[0] = '\r'; |
524 | exp[1] = ' '; | |
525 | exp[len + 1] = '\r'; | |
526 | exp[len + 2] = ' '; | |
527 | exp[len + 3] = '\0'; | |
528 | memcpy (&exp[2], pfile->token_buffer + start, len - 1); | |
529 | } | |
530 | ||
ba412f14 ZW |
531 | CPP_SET_WRITTEN (pfile, start); |
532 | ||
533 | defn = (DEFINITION *) xmalloc (sizeof (DEFINITION)); | |
534 | defn->length = len + 3; | |
535 | defn->expansion = exp; | |
536 | defn->pattern = pat; | |
537 | defn->rest_args = 0; | |
538 | if (arglist) | |
539 | { | |
540 | defn->nargs = argc; | |
541 | defn->argnames = arglist->namebuf; | |
542 | if (argv) | |
543 | { | |
544 | defn->rest_args = argv[argc - 1].rest_arg; | |
545 | free (argv); | |
6de1e2a9 | 546 | } |
ba412f14 | 547 | free (arglist); |
6de1e2a9 | 548 | } |
ba412f14 | 549 | else |
6de1e2a9 | 550 | { |
ba412f14 ZW |
551 | defn->nargs = -1; |
552 | defn->argnames = 0; | |
553 | defn->rest_args = 0; | |
6de1e2a9 | 554 | } |
6de1e2a9 ZW |
555 | return defn; |
556 | } | |
557 | ||
ba412f14 ZW |
558 | static struct arglist * |
559 | collect_formal_parameters (pfile) | |
6de1e2a9 | 560 | cpp_reader *pfile; |
6de1e2a9 | 561 | { |
ba412f14 ZW |
562 | struct arglist *result = 0; |
563 | struct arg *argv = 0; | |
38b24ee2 | 564 | U_CHAR *namebuf = (U_CHAR *) xstrdup (""); |
6de1e2a9 | 565 | |
ba412f14 ZW |
566 | U_CHAR *name, *tok; |
567 | size_t argslen = 1; | |
568 | int len; | |
569 | int argc = 0; | |
570 | int i; | |
571 | enum cpp_token token; | |
572 | long old_written; | |
6de1e2a9 | 573 | |
ba412f14 | 574 | old_written = CPP_WRITTEN (pfile); |
45b966db | 575 | token = _cpp_get_directive_token (pfile); |
ba412f14 ZW |
576 | if (token != CPP_LPAREN) |
577 | { | |
578 | cpp_ice (pfile, "first token = %d not %d in collect_formal_parameters", | |
579 | token, CPP_LPAREN); | |
580 | goto invalid; | |
581 | } | |
6de1e2a9 | 582 | |
ba412f14 ZW |
583 | argv = (struct arg *) xmalloc (sizeof (struct arg)); |
584 | argv[argc].len = 0; | |
585 | argv[argc].rest_arg = 0; | |
586 | for (;;) | |
587 | { | |
588 | CPP_SET_WRITTEN (pfile, old_written); | |
45b966db | 589 | token = _cpp_get_directive_token (pfile); |
ba412f14 ZW |
590 | switch (token) |
591 | { | |
592 | case CPP_NAME: | |
593 | tok = pfile->token_buffer + old_written; | |
594 | len = CPP_PWRITTEN (pfile) - tok; | |
595 | if (namebuf | |
7ceb3598 | 596 | && (name = (U_CHAR *) strstr (namebuf, tok)) |
ba412f14 ZW |
597 | && name[len] == ',' |
598 | && (name == namebuf || name[-1] == ',')) | |
599 | { | |
600 | cpp_error (pfile, "duplicate macro argument name `%s'", tok); | |
601 | continue; | |
602 | } | |
ae79697b | 603 | if (CPP_PEDANTIC (pfile) && CPP_OPTION (pfile, c99) |
1690826f AO |
604 | && len == sizeof "__VA_ARGS__" - 1 |
605 | && !strncmp (tok, "__VA_ARGS__", len)) | |
38b24ee2 ZW |
606 | cpp_pedwarn (pfile, |
607 | "C99 does not permit use of `__VA_ARGS__' as a macro argument name"); | |
7ceb3598 | 608 | namebuf = (U_CHAR *) xrealloc (namebuf, argslen + len + 1); |
ba412f14 ZW |
609 | name = &namebuf[argslen - 1]; |
610 | argslen += len + 1; | |
611 | ||
612 | memcpy (name, tok, len); | |
613 | name[len] = ','; | |
614 | name[len+1] = '\0'; | |
615 | argv[argc].len = len; | |
616 | argv[argc].rest_arg = 0; | |
617 | break; | |
6de1e2a9 | 618 | |
ba412f14 ZW |
619 | case CPP_COMMA: |
620 | argc++; | |
7ceb3598 | 621 | argv = (struct arg *) xrealloc (argv, (argc + 1)*sizeof(struct arg)); |
ba412f14 ZW |
622 | argv[argc].len = 0; |
623 | break; | |
6de1e2a9 | 624 | |
ba412f14 ZW |
625 | case CPP_RPAREN: |
626 | goto done; | |
6de1e2a9 | 627 | |
ba412f14 ZW |
628 | case CPP_3DOTS: |
629 | goto rest_arg; | |
6de1e2a9 | 630 | |
ba412f14 ZW |
631 | case CPP_VSPACE: |
632 | cpp_error (pfile, "missing right paren in macro argument list"); | |
633 | goto invalid; | |
6de1e2a9 | 634 | |
ba412f14 ZW |
635 | default: |
636 | cpp_error (pfile, "syntax error in #define"); | |
637 | goto invalid; | |
638 | } | |
639 | } | |
6de1e2a9 | 640 | |
ba412f14 ZW |
641 | rest_arg: |
642 | /* There are two possible styles for a vararg macro: | |
643 | the C99 way: #define foo(a, ...) a, __VA_ARGS__ | |
644 | the gnu way: #define foo(a, b...) a, b | |
645 | The C99 way can be considered a special case of the gnu way. | |
646 | There are also some constraints to worry about, but we'll handle | |
647 | those elsewhere. */ | |
648 | if (argv[argc].len == 0) | |
649 | { | |
ae79697b | 650 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)) |
ba412f14 | 651 | cpp_pedwarn (pfile, "C89 does not permit varargs macros"); |
6de1e2a9 | 652 | |
ba412f14 | 653 | len = sizeof "__VA_ARGS__" - 1; |
7ceb3598 | 654 | namebuf = (U_CHAR *) xrealloc (namebuf, argslen + len + 1); |
ba412f14 ZW |
655 | name = &namebuf[argslen - 1]; |
656 | argslen += len; | |
657 | memcpy (name, "__VA_ARGS__", len); | |
ba412f14 ZW |
658 | argv[argc].len = len; |
659 | } | |
660 | else | |
661 | if (CPP_PEDANTIC (pfile)) | |
662 | cpp_pedwarn (pfile, "ISO C does not permit named varargs macros"); | |
663 | ||
664 | argv[argc].rest_arg = 1; | |
ba412f14 | 665 | |
45b966db | 666 | token = _cpp_get_directive_token (pfile); |
ba412f14 ZW |
667 | if (token != CPP_RPAREN) |
668 | { | |
669 | cpp_error (pfile, "another parameter follows `...'"); | |
670 | goto invalid; | |
671 | } | |
6de1e2a9 | 672 | |
ba412f14 ZW |
673 | done: |
674 | /* Go through argv and fix up the pointers. */ | |
675 | len = 0; | |
676 | for (i = 0; i <= argc; i++) | |
677 | { | |
678 | argv[i].name = namebuf + len; | |
679 | len += argv[i].len + 1; | |
38b24ee2 | 680 | namebuf[len - 1] = '\0'; |
ba412f14 | 681 | } |
6de1e2a9 | 682 | |
ba412f14 | 683 | CPP_SET_WRITTEN (pfile, old_written); |
6de1e2a9 | 684 | |
ba412f14 ZW |
685 | result = (struct arglist *) xmalloc (sizeof (struct arglist)); |
686 | if (namebuf[0] != '\0') | |
687 | { | |
688 | result->namebuf = namebuf; | |
689 | result->argc = argc + 1; | |
690 | result->argv = argv; | |
6de1e2a9 ZW |
691 | } |
692 | else | |
693 | { | |
ba412f14 ZW |
694 | free (namebuf); |
695 | result->namebuf = 0; | |
696 | result->argc = 0; | |
697 | result->argv = 0; | |
698 | } | |
6de1e2a9 | 699 | |
ba412f14 ZW |
700 | return result; |
701 | ||
702 | invalid: | |
703 | if (argv) | |
704 | free (argv); | |
705 | if (namebuf) | |
706 | free (namebuf); | |
707 | return 0; | |
708 | } | |
709 | ||
710 | /* Create a DEFINITION node for a macro. The reader's point is just | |
711 | after the macro name. If FUNLIKE is true, this is a function-like | |
712 | macro. */ | |
713 | ||
714 | DEFINITION * | |
b0699dad | 715 | _cpp_create_definition (pfile, funlike) |
ba412f14 ZW |
716 | cpp_reader *pfile; |
717 | int funlike; | |
718 | { | |
719 | struct arglist *args = 0; | |
720 | long line, col; | |
721 | const char *file; | |
722 | DEFINITION *defn; | |
723 | ||
724 | cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col); | |
725 | file = CPP_BUFFER (pfile)->nominal_fname; | |
726 | ||
ba412f14 ZW |
727 | if (funlike) |
728 | { | |
729 | args = collect_formal_parameters (pfile); | |
730 | if (args == 0) | |
ff2b53ef | 731 | return 0; |
6de1e2a9 ZW |
732 | } |
733 | ||
ba412f14 ZW |
734 | defn = collect_expansion (pfile, args); |
735 | if (defn == 0) | |
ff2b53ef | 736 | return 0; |
ba412f14 | 737 | |
6de1e2a9 ZW |
738 | defn->line = line; |
739 | defn->file = file; | |
ba412f14 | 740 | defn->col = col; |
ba412f14 | 741 | return defn; |
6de1e2a9 ZW |
742 | } |
743 | ||
744 | /* | |
745 | * Parse a macro argument and append the info on PFILE's token_buffer. | |
746 | * REST_ARGS means to absorb the rest of the args. | |
747 | * Return nonzero to indicate a syntax error. | |
748 | */ | |
749 | ||
750 | static enum cpp_token | |
751 | macarg (pfile, rest_args) | |
752 | cpp_reader *pfile; | |
753 | int rest_args; | |
754 | { | |
755 | int paren = 0; | |
756 | enum cpp_token token; | |
6de1e2a9 ZW |
757 | |
758 | /* Try to parse as much of the argument as exists at this | |
759 | input stack level. */ | |
6de1e2a9 ZW |
760 | for (;;) |
761 | { | |
762 | token = cpp_get_token (pfile); | |
763 | switch (token) | |
764 | { | |
765 | case CPP_EOF: | |
564ad5f4 | 766 | return token; |
6de1e2a9 ZW |
767 | case CPP_POP: |
768 | /* If we've hit end of file, it's an error (reported by caller). | |
769 | Ditto if it's the end of cpp_expand_to_buffer text. | |
770 | If we've hit end of macro, just continue. */ | |
771 | if (!CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile))) | |
564ad5f4 | 772 | return token; |
6de1e2a9 ZW |
773 | break; |
774 | case CPP_LPAREN: | |
775 | paren++; | |
776 | break; | |
777 | case CPP_RPAREN: | |
778 | if (--paren < 0) | |
779 | goto found; | |
780 | break; | |
781 | case CPP_COMMA: | |
782 | /* if we've returned to lowest level and | |
783 | we aren't absorbing all args */ | |
784 | if (paren == 0 && rest_args == 0) | |
785 | goto found; | |
786 | break; | |
787 | found: | |
788 | /* Remove ',' or ')' from argument buffer. */ | |
789 | CPP_ADJUST_WRITTEN (pfile, -1); | |
564ad5f4 | 790 | return token; |
6de1e2a9 ZW |
791 | default:; |
792 | } | |
793 | } | |
6de1e2a9 ZW |
794 | } |
795 | \f | |
6de1e2a9 ZW |
796 | |
797 | static struct tm * | |
798 | timestamp (pfile) | |
799 | cpp_reader *pfile; | |
800 | { | |
801 | if (!pfile->timebuf) | |
802 | { | |
803 | time_t t = time ((time_t *) 0); | |
804 | pfile->timebuf = localtime (&t); | |
805 | } | |
806 | return pfile->timebuf; | |
807 | } | |
808 | ||
bcc5cac9 | 809 | static const char * const monthnames[] = |
6de1e2a9 ZW |
810 | { |
811 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
812 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", | |
813 | }; | |
814 | ||
45b966db ZW |
815 | /* Place into PFILE a quoted string representing the string SRC. |
816 | Caller must reserve enough space in pfile->token_buffer. */ | |
817 | ||
818 | void | |
819 | _cpp_quote_string (pfile, src) | |
820 | cpp_reader *pfile; | |
821 | const char *src; | |
822 | { | |
823 | U_CHAR c; | |
824 | ||
825 | CPP_PUTC_Q (pfile, '\"'); | |
826 | for (;;) | |
827 | switch ((c = *src++)) | |
828 | { | |
829 | default: | |
830 | if (ISPRINT (c)) | |
831 | CPP_PUTC_Q (pfile, c); | |
832 | else | |
833 | { | |
834 | sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c); | |
835 | CPP_ADJUST_WRITTEN (pfile, 4); | |
836 | } | |
837 | break; | |
838 | ||
839 | case '\"': | |
840 | case '\\': | |
841 | CPP_PUTC_Q (pfile, '\\'); | |
842 | CPP_PUTC_Q (pfile, c); | |
843 | break; | |
844 | ||
845 | case '\0': | |
846 | CPP_PUTC_Q (pfile, '\"'); | |
847 | CPP_NUL_TERMINATE_Q (pfile); | |
848 | return; | |
849 | } | |
850 | } | |
851 | ||
6de1e2a9 ZW |
852 | /* |
853 | * expand things like __FILE__. Place the expansion into the output | |
854 | * buffer *without* rescanning. | |
855 | */ | |
856 | ||
857 | static void | |
858 | special_symbol (hp, pfile) | |
859 | HASHNODE *hp; | |
860 | cpp_reader *pfile; | |
861 | { | |
862 | const char *buf; | |
863 | int len; | |
864 | cpp_buffer *ip; | |
865 | ||
866 | switch (hp->type) | |
867 | { | |
868 | case T_FILE: | |
869 | case T_BASE_FILE: | |
870 | { | |
38b24ee2 | 871 | ip = cpp_file_buffer (pfile); |
6de1e2a9 ZW |
872 | if (hp->type == T_BASE_FILE) |
873 | { | |
38b24ee2 | 874 | while (CPP_PREV_BUFFER (ip) != NULL) |
6de1e2a9 ZW |
875 | ip = CPP_PREV_BUFFER (ip); |
876 | } | |
877 | ||
878 | buf = ip->nominal_fname; | |
879 | ||
880 | if (!buf) | |
881 | buf = ""; | |
882 | CPP_RESERVE (pfile, 3 + 4 * strlen (buf)); | |
45b966db | 883 | _cpp_quote_string (pfile, buf); |
6de1e2a9 ZW |
884 | return; |
885 | } | |
886 | ||
887 | case T_INCLUDE_LEVEL: | |
888 | { | |
38b24ee2 ZW |
889 | int true_indepth = 1; |
890 | ip = cpp_file_buffer (pfile); | |
891 | while ((ip = CPP_PREV_BUFFER (ip)) != NULL) | |
892 | true_indepth++; | |
6de1e2a9 ZW |
893 | |
894 | CPP_RESERVE (pfile, 10); | |
895 | sprintf (CPP_PWRITTEN (pfile), "%d", true_indepth); | |
896 | CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile))); | |
897 | return; | |
898 | } | |
899 | ||
900 | case T_VERSION: | |
6feb7728 | 901 | len = strlen (hp->value.cpval); |
6de1e2a9 ZW |
902 | CPP_RESERVE (pfile, 3 + len); |
903 | CPP_PUTC_Q (pfile, '"'); | |
6feb7728 | 904 | CPP_PUTS_Q (pfile, hp->value.cpval, len); |
6de1e2a9 ZW |
905 | CPP_PUTC_Q (pfile, '"'); |
906 | CPP_NUL_TERMINATE_Q (pfile); | |
907 | return; | |
908 | ||
909 | case T_CONST: | |
910 | buf = hp->value.cpval; | |
911 | if (!buf) | |
912 | return; | |
913 | if (*buf == '\0') | |
ed45de98 | 914 | buf = "\r "; |
6de1e2a9 ZW |
915 | |
916 | len = strlen (buf); | |
917 | CPP_RESERVE (pfile, len + 1); | |
918 | CPP_PUTS_Q (pfile, buf, len); | |
919 | CPP_NUL_TERMINATE_Q (pfile); | |
920 | return; | |
921 | ||
922 | case T_STDC: | |
923 | CPP_RESERVE (pfile, 2); | |
924 | #ifdef STDC_0_IN_SYSTEM_HEADERS | |
38b24ee2 | 925 | ip = cpp_file_buffer (pfile); |
6de1e2a9 | 926 | if (ip->system_header_p |
707beebb | 927 | && !cpp_defined (pfile, (const U_CHAR *) "__STRICT_ANSI__", 15)) |
6de1e2a9 ZW |
928 | CPP_PUTC_Q (pfile, '0'); |
929 | else | |
930 | #endif | |
931 | CPP_PUTC_Q (pfile, '1'); | |
932 | CPP_NUL_TERMINATE_Q (pfile); | |
933 | return; | |
934 | ||
935 | case T_SPECLINE: | |
936 | { | |
937 | long line; | |
5e4df1ae | 938 | cpp_buf_line_and_col (cpp_file_buffer (pfile), &line, NULL); |
6de1e2a9 ZW |
939 | |
940 | CPP_RESERVE (pfile, 10); | |
941 | sprintf (CPP_PWRITTEN (pfile), "%ld", line); | |
942 | CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile))); | |
943 | return; | |
944 | } | |
945 | ||
946 | case T_DATE: | |
947 | case T_TIME: | |
948 | { | |
949 | struct tm *timebuf; | |
950 | ||
951 | CPP_RESERVE (pfile, 20); | |
952 | timebuf = timestamp (pfile); | |
953 | if (hp->type == T_DATE) | |
954 | sprintf (CPP_PWRITTEN (pfile), "\"%s %2d %4d\"", | |
955 | monthnames[timebuf->tm_mon], | |
956 | timebuf->tm_mday, timebuf->tm_year + 1900); | |
957 | else | |
958 | sprintf (CPP_PWRITTEN (pfile), "\"%02d:%02d:%02d\"", | |
959 | timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec); | |
960 | ||
961 | CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile))); | |
962 | return; | |
963 | } | |
964 | ||
fc009f96 GK |
965 | case T_POISON: |
966 | cpp_error (pfile, "attempt to use poisoned `%s'.", hp->name); | |
967 | CPP_RESERVE (pfile, 1); | |
968 | CPP_PUTC_Q (pfile, '0'); | |
969 | CPP_NUL_TERMINATE_Q (pfile); | |
970 | break; | |
971 | ||
6de1e2a9 | 972 | default: |
c1212d2f | 973 | cpp_ice (pfile, "invalid special hash type"); |
6de1e2a9 ZW |
974 | return; |
975 | } | |
6de1e2a9 ZW |
976 | } |
977 | ||
978 | /* Expand a macro call. | |
979 | HP points to the symbol that is the macro being called. | |
980 | Put the result of expansion onto the input stack | |
981 | so that subsequent input by our caller will use it. | |
982 | ||
983 | If macro wants arguments, caller has already verified that | |
984 | an argument list follows; arguments come from the input stack. */ | |
985 | ||
986 | void | |
b0699dad | 987 | _cpp_macroexpand (pfile, hp) |
6de1e2a9 ZW |
988 | cpp_reader *pfile; |
989 | HASHNODE *hp; | |
990 | { | |
991 | int nargs; | |
992 | DEFINITION *defn; | |
993 | register U_CHAR *xbuf; | |
994 | long start_line, start_column; | |
995 | int xbuf_len; | |
a544cfd2 | 996 | struct argdata *args = 0; |
6de1e2a9 | 997 | long old_written = CPP_WRITTEN (pfile); |
a544cfd2 | 998 | int rest_args, rest_zero = 0; |
6de1e2a9 ZW |
999 | register int i; |
1000 | ||
6de1e2a9 ZW |
1001 | cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column); |
1002 | ||
1003 | /* Check for and handle special symbols. */ | |
1004 | if (hp->type != T_MACRO) | |
1005 | { | |
1006 | special_symbol (hp, pfile); | |
1007 | xbuf_len = CPP_WRITTEN (pfile) - old_written; | |
1008 | xbuf = (U_CHAR *) xmalloc (xbuf_len + 1); | |
1009 | CPP_SET_WRITTEN (pfile, old_written); | |
ba412f14 | 1010 | memcpy (xbuf, CPP_PWRITTEN (pfile), xbuf_len + 1); |
6de1e2a9 ZW |
1011 | push_macro_expansion (pfile, xbuf, xbuf_len, hp); |
1012 | CPP_BUFFER (pfile)->has_escapes = 1; | |
1013 | return; | |
1014 | } | |
1015 | ||
1016 | defn = hp->value.defn; | |
1017 | nargs = defn->nargs; | |
1018 | pfile->output_escapes++; | |
1019 | ||
1020 | if (nargs >= 0) | |
1021 | { | |
564ad5f4 | 1022 | enum cpp_token token; |
6de1e2a9 ZW |
1023 | |
1024 | args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata)); | |
1025 | ||
1026 | for (i = 0; i < nargs; i++) | |
1027 | { | |
1028 | args[i].raw = args[i].expanded = 0; | |
1029 | args[i].raw_length = 0; | |
1030 | args[i].expand_length = args[i].stringified_length = -1; | |
6de1e2a9 ZW |
1031 | } |
1032 | ||
1033 | /* Parse all the macro args that are supplied. I counts them. | |
1034 | The first NARGS args are stored in ARGS. | |
1035 | The rest are discarded. If rest_args is set then we assume | |
1036 | macarg absorbed the rest of the args. */ | |
1037 | i = 0; | |
1038 | rest_args = 0; | |
564ad5f4 ZW |
1039 | |
1040 | /* Skip over the opening parenthesis. */ | |
ae79697b ZW |
1041 | CPP_OPTION (pfile, discard_comments)++; |
1042 | CPP_OPTION (pfile, no_line_commands)++; | |
564ad5f4 ZW |
1043 | pfile->no_macro_expand++; |
1044 | pfile->no_directives++; | |
1045 | ||
1046 | token = cpp_get_non_space_token (pfile); | |
1047 | if (token != CPP_LPAREN) | |
1048 | cpp_ice (pfile, "macroexpand: unexpected token %d (wanted LPAREN)", | |
1049 | token); | |
1050 | CPP_ADJUST_WRITTEN (pfile, -1); | |
1051 | ||
1052 | token = CPP_EOF; | |
6de1e2a9 ZW |
1053 | do |
1054 | { | |
1055 | if (rest_args) | |
1056 | continue; | |
1057 | if (i < nargs || (nargs == 0 && i == 0)) | |
1058 | { | |
1059 | /* if we are working on last arg which absorbs rest of args... */ | |
1060 | if (i == nargs - 1 && defn->rest_args) | |
1061 | rest_args = 1; | |
1062 | args[i].raw = CPP_WRITTEN (pfile); | |
1063 | token = macarg (pfile, rest_args); | |
1064 | args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw; | |
6de1e2a9 ZW |
1065 | } |
1066 | else | |
1067 | token = macarg (pfile, 0); | |
1068 | if (token == CPP_EOF || token == CPP_POP) | |
564ad5f4 ZW |
1069 | cpp_error_with_line (pfile, start_line, start_column, |
1070 | "unterminated macro call"); | |
6de1e2a9 ZW |
1071 | i++; |
1072 | } | |
1073 | while (token == CPP_COMMA); | |
ae79697b ZW |
1074 | CPP_OPTION (pfile, discard_comments)--; |
1075 | CPP_OPTION (pfile, no_line_commands)--; | |
564ad5f4 ZW |
1076 | pfile->no_macro_expand--; |
1077 | pfile->no_directives--; | |
1078 | if (token != CPP_RPAREN) | |
1079 | return; | |
6de1e2a9 ZW |
1080 | |
1081 | /* If we got one arg but it was just whitespace, call that 0 args. */ | |
1082 | if (i == 1) | |
1083 | { | |
1084 | register U_CHAR *bp = ARG_BASE + args[0].raw; | |
1085 | register U_CHAR *lim = bp + args[0].raw_length; | |
1086 | /* cpp.texi says for foo ( ) we provide one argument. | |
1087 | However, if foo wants just 0 arguments, treat this as 0. */ | |
1088 | if (nargs == 0) | |
a9ae4483 | 1089 | while (bp != lim && is_space(*bp)) |
6de1e2a9 ZW |
1090 | bp++; |
1091 | if (bp == lim) | |
1092 | i = 0; | |
1093 | } | |
1094 | ||
1095 | /* Don't output an error message if we have already output one for | |
1096 | a parse error above. */ | |
1097 | rest_zero = 0; | |
1098 | if (nargs == 0 && i > 0) | |
1099 | { | |
1100 | cpp_error (pfile, "arguments given to macro `%s'", hp->name); | |
1101 | } | |
1102 | else if (i < nargs) | |
1103 | { | |
1104 | /* traditional C allows foo() if foo wants one argument. */ | |
1105 | if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile)) | |
1106 | ; | |
1107 | /* the rest args token is allowed to absorb 0 tokens */ | |
1108 | else if (i == nargs - 1 && defn->rest_args) | |
1109 | rest_zero = 1; | |
1110 | else if (i == 0) | |
1111 | cpp_error (pfile, "macro `%s' used without args", hp->name); | |
1112 | else if (i == 1) | |
1113 | cpp_error (pfile, "macro `%s' used with just one arg", hp->name); | |
1114 | else | |
1115 | cpp_error (pfile, "macro `%s' used with only %d args", | |
1116 | hp->name, i); | |
1117 | } | |
1118 | else if (i > nargs) | |
1119 | { | |
1120 | cpp_error (pfile, | |
1121 | "macro `%s' used with too many (%d) args", hp->name, i); | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | /* If macro wants zero args, we parsed the arglist for checking only. | |
1126 | Read directly from the macro definition. */ | |
1127 | if (nargs <= 0) | |
1128 | { | |
1129 | xbuf = defn->expansion; | |
1130 | xbuf_len = defn->length; | |
1131 | } | |
1132 | else | |
1133 | { | |
1134 | register U_CHAR *exp = defn->expansion; | |
1135 | register int offset; /* offset in expansion, | |
1136 | copied a piece at a time */ | |
1137 | register int totlen; /* total amount of exp buffer filled so far */ | |
1138 | ||
1139 | register struct reflist *ap, *last_ap; | |
1140 | ||
1141 | /* Macro really takes args. Compute the expansion of this call. */ | |
1142 | ||
1143 | /* Compute length in characters of the macro's expansion. | |
1144 | Also count number of times each arg is used. */ | |
1145 | xbuf_len = defn->length; | |
1146 | for (ap = defn->pattern; ap != NULL; ap = ap->next) | |
1147 | { | |
1148 | if (ap->stringify) | |
1149 | { | |
1150 | register struct argdata *arg = &args[ap->argno]; | |
1151 | /* Stringify if it hasn't already been */ | |
1152 | if (arg->stringified_length < 0) | |
1153 | { | |
1154 | int arglen = arg->raw_length; | |
1155 | int escaped = 0; | |
1156 | int in_string = 0; | |
1157 | int c; | |
1158 | /* Initially need_space is -1. Otherwise, 1 means the | |
1159 | previous character was a space, but we suppressed it; | |
1160 | 0 means the previous character was a non-space. */ | |
1161 | int need_space = -1; | |
1162 | i = 0; | |
1163 | arg->stringified = CPP_WRITTEN (pfile); | |
1164 | if (!CPP_TRADITIONAL (pfile)) | |
1165 | CPP_PUTC (pfile, '\"'); /* insert beginning quote */ | |
1166 | for (; i < arglen; i++) | |
1167 | { | |
1168 | c = (ARG_BASE + arg->raw)[i]; | |
1169 | ||
1170 | if (!in_string) | |
1171 | { | |
eaefae0e ZW |
1172 | /* Delete "\r " and "\r-" escapes. */ |
1173 | if (c == '\r') | |
1174 | { | |
1175 | i++; | |
1176 | continue; | |
1177 | } | |
6de1e2a9 ZW |
1178 | /* Internal sequences of whitespace are |
1179 | replaced by one space except within | |
1180 | a string or char token. */ | |
eaefae0e | 1181 | else if (is_space(c)) |
6de1e2a9 | 1182 | { |
6de1e2a9 ZW |
1183 | if (need_space == 0) |
1184 | need_space = 1; | |
1185 | continue; | |
1186 | } | |
1187 | else if (need_space > 0) | |
1188 | CPP_PUTC (pfile, ' '); | |
1189 | need_space = 0; | |
1190 | } | |
1191 | ||
1192 | if (escaped) | |
1193 | escaped = 0; | |
1194 | else | |
1195 | { | |
1196 | if (c == '\\') | |
1197 | escaped = 1; | |
1198 | if (in_string) | |
1199 | { | |
1200 | if (c == in_string) | |
1201 | in_string = 0; | |
1202 | } | |
1203 | else if (c == '\"' || c == '\'') | |
1204 | in_string = c; | |
1205 | } | |
1206 | ||
1207 | /* Escape these chars */ | |
1208 | if (c == '\"' || (in_string && c == '\\')) | |
1209 | CPP_PUTC (pfile, '\\'); | |
1210 | if (ISPRINT (c)) | |
1211 | CPP_PUTC (pfile, c); | |
1212 | else | |
1213 | { | |
1214 | CPP_RESERVE (pfile, 4); | |
1215 | sprintf ((char *) CPP_PWRITTEN (pfile), "\\%03o", | |
1216 | (unsigned int) c); | |
1217 | CPP_ADJUST_WRITTEN (pfile, 4); | |
1218 | } | |
1219 | } | |
1220 | if (!CPP_TRADITIONAL (pfile)) | |
1221 | CPP_PUTC (pfile, '\"'); /* insert ending quote */ | |
1222 | arg->stringified_length | |
1223 | = CPP_WRITTEN (pfile) - arg->stringified; | |
1224 | } | |
1225 | xbuf_len += args[ap->argno].stringified_length; | |
1226 | } | |
1227 | else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile)) | |
4571dc27 | 1228 | /* Add 4 for two \r-space markers to prevent |
6de1e2a9 ZW |
1229 | token concatenation. */ |
1230 | xbuf_len += args[ap->argno].raw_length + 4; | |
1231 | else | |
1232 | { | |
1233 | /* We have an ordinary (expanded) occurrence of the arg. | |
1234 | So compute its expansion, if we have not already. */ | |
1235 | if (args[ap->argno].expand_length < 0) | |
1236 | { | |
1237 | args[ap->argno].expanded = CPP_WRITTEN (pfile); | |
1238 | cpp_expand_to_buffer (pfile, | |
1239 | ARG_BASE + args[ap->argno].raw, | |
1240 | args[ap->argno].raw_length); | |
1241 | ||
1242 | args[ap->argno].expand_length | |
1243 | = CPP_WRITTEN (pfile) - args[ap->argno].expanded; | |
1244 | } | |
1245 | ||
4571dc27 | 1246 | /* Add 4 for two \r-space markers to prevent |
6de1e2a9 ZW |
1247 | token concatenation. */ |
1248 | xbuf_len += args[ap->argno].expand_length + 4; | |
1249 | } | |
6de1e2a9 ZW |
1250 | } |
1251 | ||
1252 | xbuf = (U_CHAR *) xmalloc (xbuf_len + 1); | |
1253 | ||
1254 | /* Generate in XBUF the complete expansion | |
1255 | with arguments substituted in. | |
1256 | TOTLEN is the total size generated so far. | |
1257 | OFFSET is the index in the definition | |
1258 | of where we are copying from. */ | |
1259 | offset = totlen = 0; | |
1260 | for (last_ap = NULL, ap = defn->pattern; ap != NULL; | |
1261 | last_ap = ap, ap = ap->next) | |
1262 | { | |
1263 | register struct argdata *arg = &args[ap->argno]; | |
1264 | int count_before = totlen; | |
1265 | ||
1266 | /* Add chars to XBUF. */ | |
ba412f14 ZW |
1267 | i = ap->nchars; |
1268 | memcpy (&xbuf[totlen], &exp[offset], i); | |
1269 | totlen += i; | |
1270 | offset += i; | |
6de1e2a9 ZW |
1271 | |
1272 | /* If followed by an empty rest arg with concatenation, | |
1273 | delete the last run of nonwhite chars. */ | |
1274 | if (rest_zero && totlen > count_before | |
1275 | && ((ap->rest_args && ap->raw_before) | |
1276 | || (last_ap != NULL && last_ap->rest_args | |
1277 | && last_ap->raw_after))) | |
1278 | { | |
1279 | /* Delete final whitespace. */ | |
a9ae4483 | 1280 | while (totlen > count_before && is_space(xbuf[totlen - 1])) |
6de1e2a9 ZW |
1281 | totlen--; |
1282 | ||
1283 | /* Delete the nonwhites before them. */ | |
a9ae4483 | 1284 | while (totlen > count_before && !is_space(xbuf[totlen - 1])) |
6de1e2a9 ZW |
1285 | totlen--; |
1286 | } | |
1287 | ||
1288 | if (ap->stringify != 0) | |
1289 | { | |
ba412f14 ZW |
1290 | memcpy (xbuf + totlen, ARG_BASE + arg->stringified, |
1291 | arg->stringified_length); | |
6de1e2a9 ZW |
1292 | totlen += arg->stringified_length; |
1293 | } | |
1294 | else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile)) | |
1295 | { | |
1296 | U_CHAR *p1 = ARG_BASE + arg->raw; | |
1297 | U_CHAR *l1 = p1 + arg->raw_length; | |
1298 | if (ap->raw_before) | |
1299 | { | |
5d83f44b ZW |
1300 | /* Arg is concatenated before: delete leading whitespace, |
1301 | whitespace markers, and no-reexpansion markers. */ | |
1302 | while (p1 != l1) | |
1303 | { | |
a9ae4483 | 1304 | if (is_space(p1[0])) |
5d83f44b ZW |
1305 | p1++; |
1306 | else if (p1[0] == '\r') | |
1307 | p1 += 2; | |
1308 | else | |
1309 | break; | |
1310 | } | |
6de1e2a9 ZW |
1311 | } |
1312 | if (ap->raw_after) | |
1313 | { | |
1314 | /* Arg is concatenated after: delete trailing whitespace, | |
1315 | whitespace markers, and no-reexpansion markers. */ | |
1316 | while (p1 != l1) | |
1317 | { | |
a9ae4483 | 1318 | if (is_space(l1[-1])) |
6de1e2a9 | 1319 | l1--; |
ed45de98 ZW |
1320 | else if (l1[-1] == '\r') |
1321 | l1--; | |
6de1e2a9 ZW |
1322 | else if (l1[-1] == '-') |
1323 | { | |
ed45de98 | 1324 | if (l1 != p1 + 1 && l1[-2] == '\r') |
6de1e2a9 ZW |
1325 | l1 -= 2; |
1326 | else | |
1327 | break; | |
1328 | } | |
1329 | else | |
1330 | break; | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | /* Delete any no-reexpansion marker that precedes | |
1335 | an identifier at the beginning of the argument. */ | |
ed45de98 | 1336 | if (p1[0] == '\r' && p1[1] == '-') |
6de1e2a9 ZW |
1337 | p1 += 2; |
1338 | ||
ba412f14 | 1339 | memcpy (xbuf + totlen, p1, l1 - p1); |
6de1e2a9 ZW |
1340 | totlen += l1 - p1; |
1341 | } | |
1342 | else | |
1343 | { | |
1344 | U_CHAR *expanded = ARG_BASE + arg->expanded; | |
1345 | if (!ap->raw_before && totlen > 0 && arg->expand_length | |
1346 | && !CPP_TRADITIONAL (pfile) | |
455d2586 | 1347 | && unsafe_chars (pfile, xbuf[totlen - 1], expanded[0])) |
6de1e2a9 | 1348 | { |
ed45de98 | 1349 | xbuf[totlen++] = '\r'; |
6de1e2a9 ZW |
1350 | xbuf[totlen++] = ' '; |
1351 | } | |
1352 | ||
ba412f14 | 1353 | memcpy (xbuf + totlen, expanded, arg->expand_length); |
6de1e2a9 ZW |
1354 | totlen += arg->expand_length; |
1355 | ||
1356 | if (!ap->raw_after && totlen > 0 && offset < defn->length | |
1357 | && !CPP_TRADITIONAL (pfile) | |
455d2586 | 1358 | && unsafe_chars (pfile, xbuf[totlen - 1], exp[offset])) |
6de1e2a9 | 1359 | { |
ed45de98 | 1360 | xbuf[totlen++] = '\r'; |
6de1e2a9 ZW |
1361 | xbuf[totlen++] = ' '; |
1362 | } | |
6de1e2a9 ZW |
1363 | } |
1364 | ||
1365 | if (totlen > xbuf_len) | |
34ca9541 | 1366 | { |
c1212d2f | 1367 | cpp_ice (pfile, "buffer overrun in macroexpand"); |
34ca9541 ZW |
1368 | return; |
1369 | } | |
6de1e2a9 ZW |
1370 | } |
1371 | ||
1372 | /* if there is anything left of the definition | |
1373 | after handling the arg list, copy that in too. */ | |
1374 | ||
1375 | for (i = offset; i < defn->length; i++) | |
1376 | { | |
1377 | /* if we've reached the end of the macro */ | |
1378 | if (exp[i] == ')') | |
1379 | rest_zero = 0; | |
1380 | if (!(rest_zero && last_ap != NULL && last_ap->rest_args | |
1381 | && last_ap->raw_after)) | |
1382 | xbuf[totlen++] = exp[i]; | |
1383 | } | |
1384 | ||
1385 | xbuf[totlen] = 0; | |
1386 | xbuf_len = totlen; | |
1387 | ||
1388 | } | |
1389 | ||
1390 | pfile->output_escapes--; | |
1391 | ||
1392 | /* Now put the expansion on the input stack | |
1393 | so our caller will commence reading from it. */ | |
1394 | push_macro_expansion (pfile, xbuf, xbuf_len, hp); | |
1395 | CPP_BUFFER (pfile)->has_escapes = 1; | |
1396 | ||
1397 | /* Pop the space we've used in the token_buffer for argument expansion. */ | |
1398 | CPP_SET_WRITTEN (pfile, old_written); | |
1399 | ||
1400 | /* Recursive macro use sometimes works traditionally. | |
1401 | #define foo(x,y) bar (x (y,0), y) | |
1402 | foo (foo, baz) */ | |
1403 | ||
1404 | if (!CPP_TRADITIONAL (pfile)) | |
1405 | hp->type = T_DISABLED; | |
1406 | } | |
1407 | ||
1408 | /* Return 1 iff a token ending in C1 followed directly by a token C2 | |
1409 | could cause mis-tokenization. */ | |
1410 | ||
1411 | static int | |
455d2586 ZW |
1412 | unsafe_chars (pfile, c1, c2) |
1413 | cpp_reader *pfile; | |
6de1e2a9 ZW |
1414 | int c1, c2; |
1415 | { | |
1416 | switch (c1) | |
1417 | { | |
ff2b53ef ZW |
1418 | case EOF: |
1419 | /* We don't know what the previous character was. We do know | |
1420 | that it can't have been an idchar (or else it would have been | |
1421 | pasted with the idchars of the macro name), and there are a | |
1422 | number of second characters for which it doesn't matter what | |
1423 | the first was. */ | |
1424 | if (is_idchar (c2) || c2 == '\'' || c2 == '\"' | |
1425 | || c2 == '(' || c2 == '[' || c2 == '{' | |
1426 | || c2 == ')' || c2 == ']' || c2 == '}') | |
1427 | return 0; | |
1428 | return 1; | |
1429 | ||
5d83f44b | 1430 | case '+': case '-': |
6de1e2a9 ZW |
1431 | if (c2 == c1 || c2 == '=') |
1432 | return 1; | |
1433 | goto letter; | |
1434 | ||
5d83f44b | 1435 | case 'e': case 'E': case 'p': case 'P': |
6de1e2a9 ZW |
1436 | if (c2 == '-' || c2 == '+') |
1437 | return 1; /* could extend a pre-processing number */ | |
1438 | goto letter; | |
1439 | ||
455d2586 | 1440 | case '$': |
ae79697b | 1441 | if (CPP_OPTION (pfile, dollars_in_ident)) |
455d2586 ZW |
1442 | goto letter; |
1443 | return 0; | |
1444 | ||
6de1e2a9 ZW |
1445 | case 'L': |
1446 | if (c2 == '\'' || c2 == '\"') | |
1447 | return 1; /* Could turn into L"xxx" or L'xxx'. */ | |
1448 | goto letter; | |
1449 | ||
5d83f44b ZW |
1450 | case '.': case '0': case '1': case '2': case '3': |
1451 | case '4': case '5': case '6': case '7': case '8': case '9': | |
6de1e2a9 ZW |
1452 | case '_': case 'a': case 'b': case 'c': case 'd': case 'f': |
1453 | case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': | |
1454 | case 'm': case 'n': case 'o': case 'q': case 'r': case 's': | |
1455 | case 't': case 'u': case 'v': case 'w': case 'x': case 'y': | |
1456 | case 'z': case 'A': case 'B': case 'C': case 'D': case 'F': | |
1457 | case 'G': case 'H': case 'I': case 'J': case 'K': case 'M': | |
1458 | case 'N': case 'O': case 'Q': case 'R': case 'S': case 'T': | |
1459 | case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': | |
1460 | letter: | |
1461 | /* We're in the middle of either a name or a pre-processing number. */ | |
a9ae4483 | 1462 | return (is_idchar(c2) || c2 == '.'); |
6de1e2a9 ZW |
1463 | |
1464 | case '<': case '>': case '!': case '%': case '#': case ':': | |
1465 | case '^': case '&': case '|': case '*': case '/': case '=': | |
1466 | return (c2 == c1 || c2 == '='); | |
1467 | } | |
1468 | return 0; | |
1469 | } | |
1470 | ||
1471 | static void | |
ff2b53ef | 1472 | push_macro_expansion (pfile, xbuf, len, hp) |
6de1e2a9 ZW |
1473 | cpp_reader *pfile; |
1474 | register U_CHAR *xbuf; | |
ff2b53ef | 1475 | int len; |
6de1e2a9 ZW |
1476 | HASHNODE *hp; |
1477 | { | |
ff2b53ef ZW |
1478 | cpp_buffer *mbuf; |
1479 | int advance_cur = 0; | |
6de1e2a9 | 1480 | |
ed45de98 | 1481 | /* The first chars of the expansion should be a "\r " added by |
6de1e2a9 ZW |
1482 | collect_expansion. This is to prevent accidental token-pasting |
1483 | between the text preceding the macro invocation, and the macro | |
1484 | expansion text. | |
1485 | ||
1486 | We would like to avoid adding unneeded spaces (for the sake of | |
1487 | tools that use cpp, such as imake). In some common cases we can | |
ff2b53ef | 1488 | tell that it is safe to omit the space. */ |
6de1e2a9 | 1489 | |
ed45de98 | 1490 | if (xbuf[0] == '\r' && xbuf[1] == ' ' |
ff2b53ef ZW |
1491 | && !unsafe_chars (pfile, EOF, xbuf[2])) |
1492 | advance_cur = 1; | |
6de1e2a9 ZW |
1493 | |
1494 | /* Likewise, avoid the extra space at the end of the macro expansion | |
1495 | if this is safe. We can do a better job here since we can know | |
1496 | what the next char will be. */ | |
ff2b53ef ZW |
1497 | if (len >= 3 |
1498 | && xbuf[len-2] == '\r' | |
1499 | && xbuf[len-1] == ' ') | |
6de1e2a9 | 1500 | { |
ff2b53ef ZW |
1501 | int c = CPP_BUF_PEEK (CPP_BUFFER (pfile)); |
1502 | if (c == EOF || !unsafe_chars (pfile, xbuf[len-3], c)) | |
1503 | len -= 2; | |
6de1e2a9 | 1504 | } |
ff2b53ef ZW |
1505 | |
1506 | mbuf = cpp_push_buffer (pfile, xbuf, len); | |
1507 | if (mbuf == NULL) | |
1508 | return; | |
1509 | if (advance_cur) | |
1510 | mbuf->cur += 2; | |
1511 | mbuf->cleanup = macro_cleanup; | |
1512 | mbuf->macro = hp; | |
6de1e2a9 ZW |
1513 | } |
1514 | ||
1515 | /* Return zero if two DEFINITIONs are isomorphic. */ | |
1516 | ||
1517 | int | |
b0699dad | 1518 | _cpp_compare_defs (pfile, d1, d2) |
6de1e2a9 ZW |
1519 | cpp_reader *pfile; |
1520 | DEFINITION *d1, *d2; | |
1521 | { | |
38b24ee2 ZW |
1522 | struct reflist *a1, *a2; |
1523 | U_CHAR *p1 = d1->expansion; | |
1524 | U_CHAR *p2 = d2->expansion; | |
6de1e2a9 ZW |
1525 | int first = 1; |
1526 | ||
1527 | if (d1->nargs != d2->nargs) | |
1528 | return 1; | |
1529 | if (CPP_PEDANTIC (pfile) | |
38b24ee2 ZW |
1530 | && d1->argnames && d2->argnames) |
1531 | { | |
1532 | U_CHAR *arg1 = d1->argnames; | |
1533 | U_CHAR *arg2 = d2->argnames; | |
1534 | size_t len; | |
1535 | int i = d1->nargs; | |
1536 | while (i--) | |
1537 | { | |
8051b178 | 1538 | len = strlen (arg1) + 1; |
38b24ee2 ZW |
1539 | if (strcmp (arg1, arg2)) |
1540 | return 1; | |
1541 | arg1 += len; | |
1542 | arg2 += len; | |
1543 | } | |
1544 | } | |
6de1e2a9 ZW |
1545 | for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2; |
1546 | a1 = a1->next, a2 = a2->next) | |
1547 | { | |
1548 | if (!((a1->nchars == a2->nchars && !strncmp (p1, p2, a1->nchars)) | |
1549 | || !comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0)) | |
1550 | || a1->argno != a2->argno | |
1551 | || a1->stringify != a2->stringify | |
1552 | || a1->raw_before != a2->raw_before | |
1553 | || a1->raw_after != a2->raw_after) | |
1554 | return 1; | |
1555 | first = 0; | |
1556 | p1 += a1->nchars; | |
1557 | p2 += a2->nchars; | |
1558 | } | |
1559 | if (a1 != a2) | |
1560 | return 1; | |
1561 | ||
1562 | return comp_def_part (first, p1, d1->length - (p1 - d1->expansion), | |
1563 | p2, d2->length - (p2 - d2->expansion), 1); | |
1564 | } | |
1565 | ||
1566 | /* Return 1 if two parts of two macro definitions are effectively different. | |
1567 | One of the parts starts at BEG1 and has LEN1 chars; | |
1568 | the other has LEN2 chars at BEG2. | |
1569 | Any sequence of whitespace matches any other sequence of whitespace. | |
1570 | FIRST means these parts are the first of a macro definition; | |
1571 | so ignore leading whitespace entirely. | |
1572 | LAST means these parts are the last of a macro definition; | |
1573 | so ignore trailing whitespace entirely. */ | |
1574 | ||
1575 | static int | |
1576 | comp_def_part (first, beg1, len1, beg2, len2, last) | |
1577 | int first; | |
1578 | U_CHAR *beg1, *beg2; | |
1579 | int len1, len2; | |
1580 | int last; | |
1581 | { | |
1582 | register U_CHAR *end1 = beg1 + len1; | |
1583 | register U_CHAR *end2 = beg2 + len2; | |
1584 | if (first) | |
1585 | { | |
a9ae4483 | 1586 | while (beg1 != end1 && is_space(*beg1)) |
6de1e2a9 | 1587 | beg1++; |
a9ae4483 | 1588 | while (beg2 != end2 && is_space(*beg2)) |
6de1e2a9 ZW |
1589 | beg2++; |
1590 | } | |
1591 | if (last) | |
1592 | { | |
a9ae4483 | 1593 | while (beg1 != end1 && is_space(end1[-1])) |
6de1e2a9 | 1594 | end1--; |
a9ae4483 | 1595 | while (beg2 != end2 && is_space(end2[-1])) |
6de1e2a9 ZW |
1596 | end2--; |
1597 | } | |
1598 | while (beg1 != end1 && beg2 != end2) | |
1599 | { | |
a9ae4483 | 1600 | if (is_space(*beg1) && is_space(*beg2)) |
6de1e2a9 | 1601 | { |
a9ae4483 | 1602 | while (beg1 != end1 && is_space(*beg1)) |
6de1e2a9 | 1603 | beg1++; |
a9ae4483 | 1604 | while (beg2 != end2 && is_space(*beg2)) |
6de1e2a9 ZW |
1605 | beg2++; |
1606 | } | |
1607 | else if (*beg1 == *beg2) | |
1608 | { | |
1609 | beg1++; | |
1610 | beg2++; | |
1611 | } | |
1612 | else | |
1613 | break; | |
1614 | } | |
1615 | return (beg1 != end1) || (beg2 != end2); | |
1616 | } | |
3caee4a8 ZW |
1617 | |
1618 | /* Dump the definition of macro MACRO on stdout. The format is suitable | |
1619 | to be read back in again. */ | |
1620 | ||
1621 | void | |
b0699dad | 1622 | _cpp_dump_definition (pfile, sym, len, defn) |
3caee4a8 | 1623 | cpp_reader *pfile; |
a2a76ce7 ZW |
1624 | const U_CHAR *sym; |
1625 | long len; | |
1626 | DEFINITION *defn; | |
3caee4a8 | 1627 | { |
c45da1ca | 1628 | if (pfile->lineno == 0) |
45b966db | 1629 | _cpp_output_line_command (pfile, same_file); |
c45da1ca | 1630 | |
a2a76ce7 | 1631 | CPP_RESERVE (pfile, len + sizeof "#define "); |
3caee4a8 | 1632 | CPP_PUTS_Q (pfile, "#define ", sizeof "#define " -1); |
a2a76ce7 | 1633 | CPP_PUTS_Q (pfile, sym, len); |
3caee4a8 ZW |
1634 | |
1635 | if (defn->nargs == -1) | |
1636 | { | |
1637 | CPP_PUTC_Q (pfile, ' '); | |
1638 | ||
1639 | /* The first and last two characters of a macro expansion are | |
1640 | always "\r "; this needs to be trimmed out. | |
1641 | So we need length-4 chars of space, plus one for the NUL. */ | |
1642 | CPP_RESERVE (pfile, defn->length - 4 + 1); | |
1643 | CPP_PUTS_Q (pfile, defn->expansion + 2, defn->length - 4); | |
3caee4a8 ZW |
1644 | } |
1645 | else | |
1646 | { | |
1647 | struct reflist *r; | |
e7553be5 DB |
1648 | unsigned char **argv = (unsigned char **) alloca (defn->nargs * |
1649 | sizeof(char *)); | |
1650 | int *argl = (int *) alloca (defn->nargs * sizeof(int)); | |
3caee4a8 ZW |
1651 | unsigned char *x; |
1652 | int i; | |
1653 | ||
1654 | /* First extract the argument list. */ | |
38b24ee2 ZW |
1655 | x = defn->argnames; |
1656 | for (i = 0; i < defn->nargs; i++) | |
3caee4a8 ZW |
1657 | { |
1658 | argv[i] = x; | |
38b24ee2 ZW |
1659 | argl[i] = strlen (x); |
1660 | x += argl[i] + 1; | |
3caee4a8 ZW |
1661 | } |
1662 | ||
1663 | /* Now print out the argument list. */ | |
1664 | CPP_PUTC_Q (pfile, '('); | |
1665 | for (i = 0; i < defn->nargs; i++) | |
1666 | { | |
1667 | CPP_RESERVE (pfile, argl[i] + 2); | |
38b24ee2 ZW |
1668 | if (!(i == defn->nargs-1 && defn->rest_args |
1669 | && !strcmp (argv[i], "__VA_ARGS__"))) | |
1670 | CPP_PUTS_Q (pfile, argv[i], argl[i]); | |
3caee4a8 ZW |
1671 | if (i < defn->nargs-1) |
1672 | CPP_PUTS_Q (pfile, ", ", 2); | |
1673 | } | |
3caee4a8 | 1674 | if (defn->rest_args) |
38b24ee2 ZW |
1675 | CPP_PUTS (pfile, "...", 3); |
1676 | CPP_PUTS (pfile, ") ", 2); | |
3caee4a8 ZW |
1677 | |
1678 | /* Now the definition. */ | |
1679 | x = defn->expansion; | |
1680 | for (r = defn->pattern; r; r = r->next) | |
1681 | { | |
1682 | i = r->nchars; | |
1683 | if (*x == '\r') x += 2, i -= 2; | |
1684 | /* i chars for macro text, plus the length of the macro | |
1685 | argument name, plus one for a stringify marker, plus two for | |
1686 | each concatenation marker. */ | |
1687 | CPP_RESERVE (pfile, | |
1688 | i + argl[r->argno] + r->stringify | |
1689 | + (r->raw_before + r->raw_after) * 2); | |
1690 | ||
1691 | if (i > 0) CPP_PUTS_Q (pfile, x, i); | |
1692 | if (r->raw_before) | |
1693 | CPP_PUTS_Q (pfile, "##", 2); | |
1694 | if (r->stringify) | |
1695 | CPP_PUTC_Q (pfile, '#'); | |
1696 | CPP_PUTS_Q (pfile, argv[r->argno], argl[r->argno]); | |
1697 | if (r->raw_after && !(r->next && r->next->nchars == 0 | |
1698 | && r->next->raw_before)) | |
1699 | CPP_PUTS_Q (pfile, "##", 2); | |
1700 | ||
1701 | x += i; | |
1702 | } | |
1703 | ||
1704 | i = defn->length - (x - defn->expansion) - 2; | |
1705 | if (*x == '\r') x += 2, i -= 2; | |
1706 | if (i > 0) CPP_PUTS (pfile, x, i); | |
3caee4a8 | 1707 | } |
c45da1ca ZW |
1708 | if (pfile->lineno == 0) |
1709 | CPP_PUTC (pfile, '\n'); | |
1710 | CPP_NUL_TERMINATE (pfile); | |
3caee4a8 | 1711 | } |
d35364d1 ZW |
1712 | |
1713 | /* Dump out the hash table. */ | |
1714 | static int | |
1715 | dump_hash_helper (h, p) | |
1a7b4c69 | 1716 | void **h; |
d35364d1 ZW |
1717 | void *p; |
1718 | { | |
1a7b4c69 | 1719 | HASHNODE *hp = (HASHNODE *)*h; |
d35364d1 ZW |
1720 | cpp_reader *pfile = (cpp_reader *)p; |
1721 | ||
194ae3f6 ZW |
1722 | if (hp->type == T_MACRO) |
1723 | { | |
1724 | _cpp_dump_definition (pfile, hp->name, hp->length, hp->value.defn); | |
1725 | CPP_PUTC (pfile, '\n'); | |
1726 | } | |
d35364d1 ZW |
1727 | return 1; |
1728 | } | |
1729 | ||
1730 | void | |
1731 | _cpp_dump_macro_hash (pfile) | |
1732 | cpp_reader *pfile; | |
1733 | { | |
1734 | htab_traverse (pfile->hashtab, dump_hash_helper, pfile); | |
1735 | } |