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