blob: 7b0bea23f174a93a88ac91dec15ab828e0e7fc71 [file] [log] [blame]
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001/* Part of CPP library. (Macro handling.)
Zack Weinberg40ea76d2000-02-06 07:30:25 +00002 Copyright (C) 1986, 87, 89, 92-96, 98, 99, 2000
3 Free Software Foundation, Inc.
Per Bothner7f2935c1995-03-16 13:59:07 -08004 Written by Per Bothner, 1994.
Jeff Law38e01251998-05-06 15:09:07 -06005 Based on CCCP program by Paul Rubin, June 1986
Per Bothner7f2935c1995-03-16 13:59:07 -08006 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
Richard Kenner940d9d61995-06-15 07:33:25 -040020Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Per Bothner7f2935c1995-03-16 13:59:07 -080021
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
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000026#include "config.h"
27#include "system.h"
Per Bothner7f2935c1995-03-16 13:59:07 -080028#include "cpplib.h"
29#include "cpphash.h"
Zack Weinberg34ca9541999-04-14 09:40:56 +000030#undef abort
Per Bothner7f2935c1995-03-16 13:59:07 -080031
Zack Weinbergbb52fa72000-02-10 02:23:08 +000032static unsigned int hashf PARAMS ((const U_CHAR *, int));
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000033static int comp_def_part PARAMS ((int, U_CHAR *, int, U_CHAR *,
34 int, int));
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000035static void push_macro_expansion PARAMS ((cpp_reader *,
36 U_CHAR *, int, HASHNODE *));
37static int unsafe_chars PARAMS ((int, int));
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +000038static int macro_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
39static enum cpp_token macarg PARAMS ((cpp_reader *, int));
40static struct tm *timestamp PARAMS ((cpp_reader *));
41static void special_symbol PARAMS ((HASHNODE *, cpp_reader *));
42
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000043
Zack Weinberga9ae4481999-10-29 04:31:14 +000044#define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000045#define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
46#define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
47
48extern char *version_string;
49
50/* The arglist structure is built by create_definition to tell
51 collect_expansion where the argument names begin. That
52 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
53 would contain pointers to the strings x, y, and z.
54 collect_expansion would then build a DEFINITION node,
55 with reflist nodes pointing to the places x, y, and z had
56 appeared. So the arglist is just convenience data passed
57 between these two routines. It is not kept around after
58 the current #define has been processed and entered into the
59 hash table. */
60
61struct arglist
62{
63 struct arglist *next;
64 U_CHAR *name;
65 int length;
66 int argno;
67 char rest_args;
68};
69
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +000070static DEFINITION *collect_expansion PARAMS ((cpp_reader *, U_CHAR *, U_CHAR *,
71 int, struct arglist *));
72
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000073/* This structure represents one parsed argument in a macro call.
74 `raw' points to the argument text as written (`raw_length' is its length).
75 `expanded' points to the argument's macro-expansion
76 (its length is `expand_length').
77 `stringified_length' is the length the argument would have
Zack Weinberg4571dc22000-01-22 04:18:41 +000078 if stringified. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000079
80/* raw and expanded are relative to ARG_BASE */
81#define ARG_BASE ((pfile)->token_buffer)
82
83struct argdata
84{
85 /* Strings relative to pfile->token_buffer */
86 long raw, expanded, stringified;
87 int raw_length, expand_length;
88 int stringified_length;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000089};
90
91
Mike Stump0f413021996-07-03 22:07:53 +000092/* Return hash function on name. must be compatible with the one
93 computed a step at a time, elsewhere */
94
Zack Weinbergbb52fa72000-02-10 02:23:08 +000095static unsigned int
96hashf (s, len)
97 register const U_CHAR *s;
Per Bothner7f2935c1995-03-16 13:59:07 -080098 register int len;
Per Bothner7f2935c1995-03-16 13:59:07 -080099{
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000100 unsigned int n = len;
101 unsigned int r = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800102
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000103 do
104 r = r * 67 + (*s++ - 113);
105 while (--n);
106 return r + len;
Per Bothner7f2935c1995-03-16 13:59:07 -0800107}
108
Jeff Law38e01251998-05-06 15:09:07 -0600109/* Find the most recent hash node for name "name" (ending with first
Zack Weinberg122ae891999-02-25 14:24:40 +0000110 non-identifier char) installed by cpp_install
Mike Stump0f413021996-07-03 22:07:53 +0000111
112 If LEN is >= 0, it is the length of the name.
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000113 Otherwise, compute the length by scanning the entire name. */
Mike Stump0f413021996-07-03 22:07:53 +0000114
Per Bothner7f2935c1995-03-16 13:59:07 -0800115HASHNODE *
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000116cpp_lookup (pfile, name, len)
117 cpp_reader *pfile;
Per Bothner7f2935c1995-03-16 13:59:07 -0800118 const U_CHAR *name;
119 int len;
Per Bothner7f2935c1995-03-16 13:59:07 -0800120{
121 register const U_CHAR *bp;
122 register HASHNODE *bucket;
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000123 register unsigned int hash;
Per Bothner7f2935c1995-03-16 13:59:07 -0800124
125 if (len < 0)
126 {
Jason Merrill203588e2000-01-11 16:23:49 -0500127 for (bp = name; is_idchar (*bp); bp++);
Per Bothner7f2935c1995-03-16 13:59:07 -0800128 len = bp - name;
129 }
130
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000131 hash = hashf (name, len) % HASHSIZE;
Per Bothner7f2935c1995-03-16 13:59:07 -0800132
Zack Weinberg122ae891999-02-25 14:24:40 +0000133 bucket = pfile->hashtab[hash];
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000134 while (bucket)
135 {
136 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
137 return bucket;
138 bucket = bucket->next;
139 }
Mike Stump0f413021996-07-03 22:07:53 +0000140 return (HASHNODE *) 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800141}
142
Zack Weinbergcf4ed942000-02-10 23:47:04 +0000143/* Free a DEFINITION structure. Used by delete_macro, and by
144 do_define when redefining macros. */
145
146void
147free_definition (d)
148 DEFINITION *d;
149{
150 struct reflist *ap, *nextap;
151
152 for (ap = d->pattern; ap != NULL; ap = nextap)
153 {
154 nextap = ap->next;
155 free (ap);
156 }
157 if (d->nargs >= 0)
158 free (d->argnames);
159 free (d);
160}
161
Per Bothner7f2935c1995-03-16 13:59:07 -0800162/*
163 * Delete a hash node. Some weirdness to free junk from macros.
164 * More such weirdness will have to be added if you define more hash
165 * types that need it.
166 */
167
Per Bothner7f2935c1995-03-16 13:59:07 -0800168void
169delete_macro (hp)
170 HASHNODE *hp;
171{
172
173 if (hp->prev != NULL)
174 hp->prev->next = hp->next;
175 if (hp->next != NULL)
176 hp->next->prev = hp->prev;
177
178 /* make sure that the bucket chain header that
Mike Stump0f413021996-07-03 22:07:53 +0000179 the deleted guy was on points to the right thing afterwards. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800180 if (hp == *hp->bucket_hdr)
181 *hp->bucket_hdr = hp->next;
182
Per Bothner896fc321995-04-30 14:43:12 -0700183 if (hp->type == T_MACRO)
Zack Weinbergcf4ed942000-02-10 23:47:04 +0000184 free_definition (hp->value.defn);
Per Bothner896fc321995-04-30 14:43:12 -0700185
Per Bothner7f2935c1995-03-16 13:59:07 -0800186 free (hp);
187}
Mike Stump0f413021996-07-03 22:07:53 +0000188
189/* Install a name in the main hash table, even if it is already there.
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000190 Name stops with first non alphanumeric, except leading '#'.
191 Caller must check against redefinition if that is desired.
Zack Weinberg122ae891999-02-25 14:24:40 +0000192 delete_macro () removes things installed by cpp_install () in fifo order.
Mike Stump0f413021996-07-03 22:07:53 +0000193 this is important because of the `defined' special symbol used
194 in #if, and also if pushdef/popdef directives are ever implemented.
195
196 If LEN is >= 0, it is the length of the name.
197 Otherwise, compute the length by scanning the entire name.
198
199 If HASH is >= 0, it is the precomputed hash code.
200 Otherwise, compute the hash code. */
201
Per Bothner7f2935c1995-03-16 13:59:07 -0800202HASHNODE *
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000203cpp_install (pfile, name, len, type, value)
Zack Weinberg122ae891999-02-25 14:24:40 +0000204 cpp_reader *pfile;
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000205 const U_CHAR *name;
Per Bothner7f2935c1995-03-16 13:59:07 -0800206 int len;
207 enum node_type type;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000208 const char *value;
Per Bothner7f2935c1995-03-16 13:59:07 -0800209{
210 register HASHNODE *hp;
211 register int i, bucket;
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000212 register const U_CHAR *p;
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000213 unsigned int hash;
Per Bothner7f2935c1995-03-16 13:59:07 -0800214
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000215 if (len < 0)
216 {
217 p = name;
Zack Weinberga9ae4481999-10-29 04:31:14 +0000218 while (is_idchar(*p))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000219 p++;
220 len = p - name;
221 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800222
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000223 hash = hashf (name, len) % HASHSIZE;
Per Bothner7f2935c1995-03-16 13:59:07 -0800224
225 i = sizeof (HASHNODE) + len + 1;
226 hp = (HASHNODE *) xmalloc (i);
227 bucket = hash;
Zack Weinberg122ae891999-02-25 14:24:40 +0000228 hp->bucket_hdr = &pfile->hashtab[bucket];
229 hp->next = pfile->hashtab[bucket];
230 pfile->hashtab[bucket] = hp;
Per Bothner7f2935c1995-03-16 13:59:07 -0800231 hp->prev = NULL;
232 if (hp->next != NULL)
233 hp->next->prev = hp;
234 hp->type = type;
235 hp->length = len;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000236 hp->value.cpval = value;
Per Bothner7f2935c1995-03-16 13:59:07 -0800237 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
Zack Weinberg7061aa51998-12-15 11:09:16 +0000238 bcopy (name, hp->name, len);
Per Bothner7f2935c1995-03-16 13:59:07 -0800239 hp->name[len] = 0;
240 return hp;
241}
Per Bothner896fc321995-04-30 14:43:12 -0700242
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000243static int
244macro_cleanup (pbuf, pfile)
245 cpp_buffer *pbuf;
246 cpp_reader *pfile ATTRIBUTE_UNUSED;
247{
248 HASHNODE *macro = (HASHNODE *) pbuf->data;
249 if (macro->type == T_DISABLED)
250 macro->type = T_MACRO;
251 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
252 free (pbuf->buf);
253 return 0;
254}
255
256
257/* Read a replacement list for a macro with parameters.
258 Build the DEFINITION structure.
259 Reads characters of text starting at BUF until END.
260 ARGLIST specifies the formal parameters to look for
261 in the text of the definition; NARGS is the number of args
262 in that list, or -1 for a macro name that wants no argument list.
263 MACRONAME is the macro name itself (so we can avoid recursive expansion)
264 and NAMELEN is its length in characters.
265
266 Note that comments, backslash-newlines, and leading white space
267 have already been deleted from the argument. */
268
269static DEFINITION *
270collect_expansion (pfile, buf, limit, nargs, arglist)
271 cpp_reader *pfile;
272 U_CHAR *buf, *limit;
273 int nargs;
274 struct arglist *arglist;
275{
276 DEFINITION *defn;
277 register U_CHAR *p, *lastp, *exp_p;
278 struct reflist *endpat = NULL;
279 /* Pointer to first nonspace after last ## seen. */
280 U_CHAR *concat = 0;
281 /* Pointer to first nonspace after last single-# seen. */
282 U_CHAR *stringify = 0;
283 int maxsize;
284 int expected_delimiter = '\0';
285
286 /* Scan thru the replacement list, ignoring comments and quoted
287 strings, picking up on the macro calls. It does a linear search
288 thru the arg list on every potential symbol. Profiling might say
289 that something smarter should happen. */
290
291 if (limit < buf)
Zack Weinberg34ca9541999-04-14 09:40:56 +0000292 {
Zack Weinbergc1212d22000-02-06 23:46:18 +0000293 cpp_ice (pfile, "limit < buf in collect_expansion");
Zack Weinberg34ca9541999-04-14 09:40:56 +0000294 limit = buf; /* treat it like a null defn */
295 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000296
297 /* Find the beginning of the trailing whitespace. */
298 p = buf;
Zack Weinberga9ae4481999-10-29 04:31:14 +0000299 while (p < limit && is_space(limit[-1]))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000300 limit--;
301
302 /* Allocate space for the text in the macro definition.
303 Leading and trailing whitespace chars need 2 bytes each.
304 Each other input char may or may not need 1 byte,
305 so this is an upper bound. The extra 5 are for invented
Zack Weinberg4571dc22000-01-22 04:18:41 +0000306 leading and trailing escape-marker and final null. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000307 maxsize = (sizeof (DEFINITION)
308 + (limit - p) + 5);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000309 defn = (DEFINITION *) xcalloc (1, maxsize);
310
311 defn->nargs = nargs;
312 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
313 lastp = exp_p;
314
315 p = buf;
316
317 /* Add one initial space escape-marker to prevent accidental
318 token-pasting (often removed by macroexpand). */
Zack Weinberged45de91999-04-12 12:03:10 +0000319 *exp_p++ = '\r';
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000320 *exp_p++ = ' ';
321
322 if (limit - p >= 2 && p[0] == '#' && p[1] == '#')
323 {
324 cpp_error (pfile, "`##' at start of macro definition");
325 p += 2;
326 }
327
328 /* Process the main body of the definition. */
329 while (p < limit)
330 {
331 int skipped_arg = 0;
332 register U_CHAR c = *p++;
333
334 *exp_p++ = c;
335
336 if (!CPP_TRADITIONAL (pfile))
337 {
338 switch (c)
339 {
340 case '\'':
341 case '\"':
342 if (expected_delimiter != '\0')
343 {
344 if (c == expected_delimiter)
345 expected_delimiter = '\0';
346 }
347 else
348 expected_delimiter = c;
349 break;
350
351 case '\\':
352 if (p < limit && expected_delimiter)
353 {
354 /* In a string, backslash goes through
355 and makes next char ordinary. */
356 *exp_p++ = *p++;
357 }
358 break;
359
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000360 case '#':
361 /* # is ordinary inside a string. */
362 if (expected_delimiter)
363 break;
364 if (p < limit && *p == '#')
365 {
366 /* ##: concatenate preceding and following tokens. */
367 /* Take out the first #, discard preceding whitespace. */
368 exp_p--;
Zack Weinberga9ae4481999-10-29 04:31:14 +0000369 while (exp_p > lastp && is_hspace(exp_p[-1]))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000370 --exp_p;
371 /* Skip the second #. */
372 p++;
373 /* Discard following whitespace. */
374 SKIP_WHITE_SPACE (p);
375 concat = p;
376 if (p == limit)
377 cpp_error (pfile, "`##' at end of macro definition");
378 }
379 else if (nargs >= 0)
380 {
381 /* Single #: stringify following argument ref.
382 Don't leave the # in the expansion. */
383 exp_p--;
384 SKIP_WHITE_SPACE (p);
Zack Weinberga9ae4481999-10-29 04:31:14 +0000385 if (p == limit || !is_idstart(*p)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000386 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' ||
387 p[1] == '"')))
388 cpp_error (pfile,
389 "`#' operator is not followed by a macro argument name");
390 else
391 stringify = p;
392 }
393 break;
394 }
395 }
396 else
397 {
398 /* In -traditional mode, recognize arguments inside strings and
399 character constants, and ignore special properties of #.
400 Arguments inside strings are considered "stringified", but no
401 extra quote marks are supplied. */
402 switch (c)
403 {
404 case '\'':
405 case '\"':
406 if (expected_delimiter != '\0')
407 {
408 if (c == expected_delimiter)
409 expected_delimiter = '\0';
410 }
411 else
412 expected_delimiter = c;
413 break;
414
415 case '\\':
416 /* Backslash quotes delimiters and itself,
417 but not macro args. */
418 if (expected_delimiter != 0 && p < limit
419 && (*p == expected_delimiter || *p == '\\'))
420 {
421 *exp_p++ = *p++;
422 continue;
423 }
424 break;
425
426 case '/':
427 if (expected_delimiter != '\0')
428 /* No comments inside strings. */
429 break;
430 if (*p == '*')
431 {
432 /* If we find a comment that wasn't removed by
433 handle_directive, this must be -traditional.
434 So replace the comment with nothing at all. */
435 exp_p--;
436 p += 1;
437 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
438 p++;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000439 }
440 break;
441 }
442 }
443
444 /* Handle the start of a symbol. */
Zack Weinberga9ae4481999-10-29 04:31:14 +0000445 if (is_idchar(c) && nargs > 0)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000446 {
447 U_CHAR *id_beg = p - 1;
448 int id_len;
449
450 --exp_p;
Zack Weinberga9ae4481999-10-29 04:31:14 +0000451 while (p != limit && is_idchar(*p))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000452 p++;
453 id_len = p - id_beg;
454
Zack Weinberga9ae4481999-10-29 04:31:14 +0000455 if (is_idstart(c)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000456 && !(id_len == 1 && c == 'L' && (*p == '\'' || *p == '"')))
457 {
458 register struct arglist *arg;
459
460 for (arg = arglist; arg != NULL; arg = arg->next)
461 {
462 struct reflist *tpat;
463
464 if (arg->name[0] == c
465 && arg->length == id_len
466 && strncmp (arg->name, id_beg, id_len) == 0)
467 {
468 if (expected_delimiter && CPP_OPTIONS
469 (pfile)->warn_stringify)
470 {
471 if (CPP_TRADITIONAL (pfile))
472 {
473 cpp_warning (pfile,
474 "macro argument `%.*s' is stringified.",
475 id_len, arg->name);
476 }
477 else
478 {
479 cpp_warning (pfile,
480 "macro arg `%.*s' would be stringified with -traditional.",
481 id_len, arg->name);
482 }
483 }
484 /* If ANSI, don't actually substitute
485 inside a string. */
486 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
487 break;
488 /* make a pat node for this arg and append it
489 to the end of the pat list */
490 tpat = (struct reflist *)
491 xmalloc (sizeof (struct reflist));
492 tpat->next = NULL;
493 tpat->raw_before = concat == id_beg;
494 tpat->raw_after = 0;
495 tpat->rest_args = arg->rest_args;
496 tpat->stringify = (CPP_TRADITIONAL (pfile)
497 ? expected_delimiter != '\0'
498 : stringify == id_beg);
499
500 if (endpat == NULL)
501 defn->pattern = tpat;
502 else
503 endpat->next = tpat;
504 endpat = tpat;
505
506 tpat->argno = arg->argno;
507 tpat->nchars = exp_p - lastp;
508 {
509 register U_CHAR *p1 = p;
510 SKIP_WHITE_SPACE (p1);
511 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
512 tpat->raw_after = 1;
513 }
514 lastp = exp_p;
515 skipped_arg = 1;
516 break;
517 }
518 }
519 }
520
521 /* If this was not a macro arg, copy it into the expansion. */
522 if (!skipped_arg)
523 {
524 register U_CHAR *lim1 = p;
525 p = id_beg;
526 while (p != lim1)
527 *exp_p++ = *p++;
528 if (stringify == id_beg)
529 cpp_error (pfile,
530 "`#' operator should be followed by a macro argument name");
531 }
532 }
533 }
534
535 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
536 {
Zack Weinberged45de91999-04-12 12:03:10 +0000537 /* If ANSI, put in a "\r " marker to prevent token pasting.
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000538 But not if "inside a string" (which in ANSI mode
539 happens only for -D option). */
Zack Weinberged45de91999-04-12 12:03:10 +0000540 *exp_p++ = '\r';
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000541 *exp_p++ = ' ';
542 }
543
544 *exp_p = '\0';
545
546 defn->length = exp_p - defn->expansion;
547
548 /* Crash now if we overrun the allocated size. */
549 if (defn->length + 1 > maxsize)
550 abort ();
551
552#if 0
553/* This isn't worth the time it takes. */
554 /* give back excess storage */
555 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
556#endif
557
558 return defn;
559}
560
561/*
562 * special extension string that can be added to the last macro argument to
563 * allow it to absorb the "rest" of the arguments when expanded. Ex:
564 * #define wow(a, b...) process (b, a, b)
565 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
566 * { wow (one, two); } -> { process (two, one, two); }
567 * if this "rest_arg" is used with the concat token '##' and if it is not
568 * supplied then the token attached to with ## will not be outputted. Ex:
569 * #define wow (a, b...) process (b ## , a, ## b)
570 * { wow (1, 2); } -> { process (2, 1, 2); }
571 * { wow (one); } -> { process (one); {
572 */
573static char rest_extension[] = "...";
574#define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
575
576/* Create a DEFINITION node from a #define directive. Arguments are
577 as for do_define. */
578
579MACRODEF
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000580create_definition (buf, limit, pfile)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000581 U_CHAR *buf, *limit;
582 cpp_reader *pfile;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000583{
584 U_CHAR *bp; /* temp ptr into input buffer */
585 U_CHAR *symname; /* remember where symbol name starts */
586 int sym_length; /* and how long it is */
587 int rest_args = 0;
588 long line, col;
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000589 const char *file =
590 CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000591 DEFINITION *defn;
592 int arglengths = 0; /* Accumulate lengths of arg names
593 plus number of args. */
594 MACRODEF mdef;
595 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
596
597 bp = buf;
598
Zack Weinberga9ae4481999-10-29 04:31:14 +0000599 while (is_hspace(*bp))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000600 bp++;
601
602 symname = bp; /* remember where it starts */
603
Zack Weinberga9ae4481999-10-29 04:31:14 +0000604 sym_length = check_macro_name (pfile, bp);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000605 bp += sym_length;
606
607 /* Lossage will occur if identifiers or control keywords are broken
608 across lines using backslash. This is not the right place to take
609 care of that. */
610
611 if (*bp == '(')
612 {
613 struct arglist *arg_ptrs = NULL;
614 int argno = 0;
615
616 bp++; /* skip '(' */
617 SKIP_WHITE_SPACE (bp);
618
619 /* Loop over macro argument names. */
620 while (*bp != ')')
621 {
622 struct arglist *temp;
623
624 temp = (struct arglist *) alloca (sizeof (struct arglist));
625 temp->name = bp;
626 temp->next = arg_ptrs;
627 temp->argno = argno++;
628 temp->rest_args = 0;
629 arg_ptrs = temp;
630
631 if (rest_args)
632 cpp_pedwarn (pfile, "another parameter follows `%s'",
633 rest_extension);
634
Zack Weinberga9ae4481999-10-29 04:31:14 +0000635 if (!is_idstart(*bp))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000636 cpp_pedwarn (pfile, "invalid character in macro parameter name");
637
638 /* Find the end of the arg name. */
Zack Weinberga9ae4481999-10-29 04:31:14 +0000639 while (is_idchar(*bp))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000640 {
641 bp++;
642 /* do we have a "special" rest-args extension here? */
643 if ((size_t) (limit - bp) > REST_EXTENSION_LENGTH
644 && !strncmp (rest_extension, bp, REST_EXTENSION_LENGTH))
645 {
646 rest_args = 1;
647 temp->rest_args = 1;
648 break;
649 }
650 }
651 temp->length = bp - temp->name;
652 if (rest_args == 1)
653 bp += REST_EXTENSION_LENGTH;
654 arglengths += temp->length + 2;
655 SKIP_WHITE_SPACE (bp);
656 if (temp->length == 0 || (*bp != ',' && *bp != ')'))
657 {
658 cpp_error (pfile,
659 "badly punctuated parameter list in `#define'");
660 goto nope;
661 }
662 if (*bp == ',')
663 {
664 bp++;
665 SKIP_WHITE_SPACE (bp);
666 }
667 if (bp >= limit)
668 {
669 cpp_error (pfile, "unterminated parameter list in `#define'");
670 goto nope;
671 }
672 {
673 struct arglist *otemp;
674
675 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
676 if (temp->length == otemp->length
677 && strncmp (temp->name, otemp->name, temp->length) == 0)
678 {
679 U_CHAR *name;
680
681 name = (U_CHAR *) alloca (temp->length + 1);
682 (void) strncpy (name, temp->name, temp->length);
683 name[temp->length] = '\0';
684 cpp_error (pfile,
685 "duplicate argument name `%s' in `#define'",
686 name);
687 goto nope;
688 }
689 }
690 }
691
692 ++bp; /* skip paren */
693 SKIP_WHITE_SPACE (bp);
694 /* now everything from bp before limit is the definition. */
695 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
696 defn->rest_args = rest_args;
697
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000698 /* Now set defn->argnames to the result of concatenating
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000699 the argument names in reverse order
700 with comma-space between them. */
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000701 defn->argnames = (U_CHAR *) xmalloc (arglengths + 1);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000702 {
703 struct arglist *temp;
704 int i = 0;
705 for (temp = arg_ptrs; temp; temp = temp->next)
706 {
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000707 bcopy (temp->name, &defn->argnames[i], temp->length);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000708 i += temp->length;
709 if (temp->next != 0)
710 {
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000711 defn->argnames[i++] = ',';
712 defn->argnames[i++] = ' ';
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000713 }
714 }
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000715 defn->argnames[i] = 0;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000716 }
717 }
718 else
719 {
720 /* Simple expansion or empty definition. */
721
722 if (bp < limit)
723 {
Zack Weinberga9ae4481999-10-29 04:31:14 +0000724 if (is_hspace(*bp))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000725 {
726 bp++;
727 SKIP_WHITE_SPACE (bp);
728 }
729 else
730 /* Per C9x, missing white space after the name in a #define
731 of an object-like macro is always a constraint violation. */
732 cpp_pedwarn (pfile,
733 "missing white space after `#define %.*s'",
734 sym_length, symname);
735 }
736 /* now everything from bp before limit is the definition. */
737 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
Zack Weinbergbb52fa72000-02-10 02:23:08 +0000738 defn->argnames = (U_CHAR *) "";
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000739 }
740
741 defn->line = line;
742 defn->file = file;
743
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000744 mdef.defn = defn;
745 mdef.symnam = symname;
746 mdef.symlen = sym_length;
747
748 return mdef;
749
750nope:
751 mdef.defn = 0;
752 return mdef;
753}
754
755/*
756 * Parse a macro argument and append the info on PFILE's token_buffer.
757 * REST_ARGS means to absorb the rest of the args.
758 * Return nonzero to indicate a syntax error.
759 */
760
761static enum cpp_token
762macarg (pfile, rest_args)
763 cpp_reader *pfile;
764 int rest_args;
765{
766 int paren = 0;
767 enum cpp_token token;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000768
769 /* Try to parse as much of the argument as exists at this
770 input stack level. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000771 for (;;)
772 {
773 token = cpp_get_token (pfile);
774 switch (token)
775 {
776 case CPP_EOF:
Zack Weinberg564ad5f2000-02-10 00:26:47 +0000777 return token;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000778 case CPP_POP:
779 /* If we've hit end of file, it's an error (reported by caller).
780 Ditto if it's the end of cpp_expand_to_buffer text.
781 If we've hit end of macro, just continue. */
782 if (!CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
Zack Weinberg564ad5f2000-02-10 00:26:47 +0000783 return token;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000784 break;
785 case CPP_LPAREN:
786 paren++;
787 break;
788 case CPP_RPAREN:
789 if (--paren < 0)
790 goto found;
791 break;
792 case CPP_COMMA:
793 /* if we've returned to lowest level and
794 we aren't absorbing all args */
795 if (paren == 0 && rest_args == 0)
796 goto found;
797 break;
798 found:
799 /* Remove ',' or ')' from argument buffer. */
800 CPP_ADJUST_WRITTEN (pfile, -1);
Zack Weinberg564ad5f2000-02-10 00:26:47 +0000801 return token;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000802 default:;
803 }
804 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000805}
806
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000807
808static struct tm *
809timestamp (pfile)
810 cpp_reader *pfile;
811{
812 if (!pfile->timebuf)
813 {
814 time_t t = time ((time_t *) 0);
815 pfile->timebuf = localtime (&t);
816 }
817 return pfile->timebuf;
818}
819
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000820static const char * const monthnames[] =
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000821{
822 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
823 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
824};
825
826/*
827 * expand things like __FILE__. Place the expansion into the output
828 * buffer *without* rescanning.
829 */
830
831static void
832special_symbol (hp, pfile)
833 HASHNODE *hp;
834 cpp_reader *pfile;
835{
836 const char *buf;
837 int len;
838 cpp_buffer *ip;
839
840 switch (hp->type)
841 {
842 case T_FILE:
843 case T_BASE_FILE:
844 {
845 ip = CPP_BUFFER (pfile);
846 if (hp->type == T_BASE_FILE)
847 {
848 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
849 ip = CPP_PREV_BUFFER (ip);
850 }
851 else
852 {
853 ip = CPP_BUFFER (pfile);
854 while (!ip->nominal_fname && ip != CPP_NULL_BUFFER (pfile))
855 ip = CPP_PREV_BUFFER (ip);
856 }
857
858 buf = ip->nominal_fname;
859
860 if (!buf)
861 buf = "";
862 CPP_RESERVE (pfile, 3 + 4 * strlen (buf));
863 quote_string (pfile, buf);
864 return;
865 }
866
867 case T_INCLUDE_LEVEL:
868 {
869 int true_indepth = 0;
870 ip = CPP_BUFFER (pfile);
871 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
872 if (ip->fname != NULL)
873 true_indepth++;
874
875 CPP_RESERVE (pfile, 10);
876 sprintf (CPP_PWRITTEN (pfile), "%d", true_indepth);
877 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
878 return;
879 }
880
881 case T_VERSION:
882 len = strlen (version_string);
883 CPP_RESERVE (pfile, 3 + len);
884 CPP_PUTC_Q (pfile, '"');
885 CPP_PUTS_Q (pfile, version_string, len);
886 CPP_PUTC_Q (pfile, '"');
887 CPP_NUL_TERMINATE_Q (pfile);
888 return;
889
890 case T_CONST:
891 buf = hp->value.cpval;
892 if (!buf)
893 return;
894 if (*buf == '\0')
Zack Weinberged45de91999-04-12 12:03:10 +0000895 buf = "\r ";
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000896
897 len = strlen (buf);
898 CPP_RESERVE (pfile, len + 1);
899 CPP_PUTS_Q (pfile, buf, len);
900 CPP_NUL_TERMINATE_Q (pfile);
901 return;
902
903 case T_STDC:
904 CPP_RESERVE (pfile, 2);
905#ifdef STDC_0_IN_SYSTEM_HEADERS
906 ip = CPP_BUFFER (pfile);
907 while (!ip->nominal_fname && ip != CPP_NULL_BUFFER (pfile))
908 ip = CPP_PREV_BUFFER (ip);
909 if (ip->system_header_p
910 && !cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", 15, -1))
911 CPP_PUTC_Q (pfile, '0');
912 else
913#endif
914 CPP_PUTC_Q (pfile, '1');
915 CPP_NUL_TERMINATE_Q (pfile);
916 return;
917
918 case T_SPECLINE:
919 {
920 long line;
Zack Weinberg5e4df1a1999-04-09 20:02:39 +0000921 cpp_buf_line_and_col (cpp_file_buffer (pfile), &line, NULL);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000922
923 CPP_RESERVE (pfile, 10);
924 sprintf (CPP_PWRITTEN (pfile), "%ld", line);
925 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
926 return;
927 }
928
929 case T_DATE:
930 case T_TIME:
931 {
932 struct tm *timebuf;
933
934 CPP_RESERVE (pfile, 20);
935 timebuf = timestamp (pfile);
936 if (hp->type == T_DATE)
937 sprintf (CPP_PWRITTEN (pfile), "\"%s %2d %4d\"",
938 monthnames[timebuf->tm_mon],
939 timebuf->tm_mday, timebuf->tm_year + 1900);
940 else
941 sprintf (CPP_PWRITTEN (pfile), "\"%02d:%02d:%02d\"",
942 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
943
944 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
945 return;
946 }
947
Geoff Keatingfc009f91999-09-09 04:00:37 +0000948 case T_POISON:
949 cpp_error (pfile, "attempt to use poisoned `%s'.", hp->name);
950 CPP_RESERVE (pfile, 1);
951 CPP_PUTC_Q (pfile, '0');
952 CPP_NUL_TERMINATE_Q (pfile);
953 break;
954
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000955 default:
Zack Weinbergc1212d22000-02-06 23:46:18 +0000956 cpp_ice (pfile, "invalid special hash type");
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000957 return;
958 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000959}
960
961/* Expand a macro call.
962 HP points to the symbol that is the macro being called.
963 Put the result of expansion onto the input stack
964 so that subsequent input by our caller will use it.
965
966 If macro wants arguments, caller has already verified that
967 an argument list follows; arguments come from the input stack. */
968
969void
970macroexpand (pfile, hp)
971 cpp_reader *pfile;
972 HASHNODE *hp;
973{
974 int nargs;
975 DEFINITION *defn;
976 register U_CHAR *xbuf;
977 long start_line, start_column;
978 int xbuf_len;
Kaveh R. Ghazia544cfd2000-01-04 16:29:41 +0000979 struct argdata *args = 0;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000980 long old_written = CPP_WRITTEN (pfile);
Kaveh R. Ghazia544cfd2000-01-04 16:29:41 +0000981 int rest_args, rest_zero = 0;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000982 register int i;
983
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000984 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
985
986 /* Check for and handle special symbols. */
987 if (hp->type != T_MACRO)
988 {
989 special_symbol (hp, pfile);
990 xbuf_len = CPP_WRITTEN (pfile) - old_written;
991 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
992 CPP_SET_WRITTEN (pfile, old_written);
993 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
994 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
995 CPP_BUFFER (pfile)->has_escapes = 1;
996 return;
997 }
998
999 defn = hp->value.defn;
1000 nargs = defn->nargs;
1001 pfile->output_escapes++;
1002
1003 if (nargs >= 0)
1004 {
Zack Weinberg564ad5f2000-02-10 00:26:47 +00001005 enum cpp_token token;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001006
1007 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
1008
1009 for (i = 0; i < nargs; i++)
1010 {
1011 args[i].raw = args[i].expanded = 0;
1012 args[i].raw_length = 0;
1013 args[i].expand_length = args[i].stringified_length = -1;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001014 }
1015
1016 /* Parse all the macro args that are supplied. I counts them.
1017 The first NARGS args are stored in ARGS.
1018 The rest are discarded. If rest_args is set then we assume
1019 macarg absorbed the rest of the args. */
1020 i = 0;
1021 rest_args = 0;
Zack Weinberg564ad5f2000-02-10 00:26:47 +00001022
1023 /* Skip over the opening parenthesis. */
1024 CPP_OPTIONS (pfile)->discard_comments++;
1025 CPP_OPTIONS (pfile)->no_line_commands++;
1026 pfile->no_macro_expand++;
1027 pfile->no_directives++;
1028
1029 token = cpp_get_non_space_token (pfile);
1030 if (token != CPP_LPAREN)
1031 cpp_ice (pfile, "macroexpand: unexpected token %d (wanted LPAREN)",
1032 token);
1033 CPP_ADJUST_WRITTEN (pfile, -1);
1034
1035 token = CPP_EOF;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001036 do
1037 {
1038 if (rest_args)
1039 continue;
1040 if (i < nargs || (nargs == 0 && i == 0))
1041 {
1042 /* if we are working on last arg which absorbs rest of args... */
1043 if (i == nargs - 1 && defn->rest_args)
1044 rest_args = 1;
1045 args[i].raw = CPP_WRITTEN (pfile);
1046 token = macarg (pfile, rest_args);
1047 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001048 }
1049 else
1050 token = macarg (pfile, 0);
1051 if (token == CPP_EOF || token == CPP_POP)
Zack Weinberg564ad5f2000-02-10 00:26:47 +00001052 cpp_error_with_line (pfile, start_line, start_column,
1053 "unterminated macro call");
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001054 i++;
1055 }
1056 while (token == CPP_COMMA);
Zack Weinberg564ad5f2000-02-10 00:26:47 +00001057 CPP_OPTIONS (pfile)->discard_comments--;
1058 CPP_OPTIONS (pfile)->no_line_commands--;
1059 pfile->no_macro_expand--;
1060 pfile->no_directives--;
1061 if (token != CPP_RPAREN)
1062 return;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001063
1064 /* If we got one arg but it was just whitespace, call that 0 args. */
1065 if (i == 1)
1066 {
1067 register U_CHAR *bp = ARG_BASE + args[0].raw;
1068 register U_CHAR *lim = bp + args[0].raw_length;
1069 /* cpp.texi says for foo ( ) we provide one argument.
1070 However, if foo wants just 0 arguments, treat this as 0. */
1071 if (nargs == 0)
Zack Weinberga9ae4481999-10-29 04:31:14 +00001072 while (bp != lim && is_space(*bp))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001073 bp++;
1074 if (bp == lim)
1075 i = 0;
1076 }
1077
1078 /* Don't output an error message if we have already output one for
1079 a parse error above. */
1080 rest_zero = 0;
1081 if (nargs == 0 && i > 0)
1082 {
1083 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
1084 }
1085 else if (i < nargs)
1086 {
1087 /* traditional C allows foo() if foo wants one argument. */
1088 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
1089 ;
1090 /* the rest args token is allowed to absorb 0 tokens */
1091 else if (i == nargs - 1 && defn->rest_args)
1092 rest_zero = 1;
1093 else if (i == 0)
1094 cpp_error (pfile, "macro `%s' used without args", hp->name);
1095 else if (i == 1)
1096 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
1097 else
1098 cpp_error (pfile, "macro `%s' used with only %d args",
1099 hp->name, i);
1100 }
1101 else if (i > nargs)
1102 {
1103 cpp_error (pfile,
1104 "macro `%s' used with too many (%d) args", hp->name, i);
1105 }
1106 }
1107
1108 /* If macro wants zero args, we parsed the arglist for checking only.
1109 Read directly from the macro definition. */
1110 if (nargs <= 0)
1111 {
1112 xbuf = defn->expansion;
1113 xbuf_len = defn->length;
1114 }
1115 else
1116 {
1117 register U_CHAR *exp = defn->expansion;
1118 register int offset; /* offset in expansion,
1119 copied a piece at a time */
1120 register int totlen; /* total amount of exp buffer filled so far */
1121
1122 register struct reflist *ap, *last_ap;
1123
1124 /* Macro really takes args. Compute the expansion of this call. */
1125
1126 /* Compute length in characters of the macro's expansion.
1127 Also count number of times each arg is used. */
1128 xbuf_len = defn->length;
1129 for (ap = defn->pattern; ap != NULL; ap = ap->next)
1130 {
1131 if (ap->stringify)
1132 {
1133 register struct argdata *arg = &args[ap->argno];
1134 /* Stringify if it hasn't already been */
1135 if (arg->stringified_length < 0)
1136 {
1137 int arglen = arg->raw_length;
1138 int escaped = 0;
1139 int in_string = 0;
1140 int c;
1141 /* Initially need_space is -1. Otherwise, 1 means the
1142 previous character was a space, but we suppressed it;
1143 0 means the previous character was a non-space. */
1144 int need_space = -1;
1145 i = 0;
1146 arg->stringified = CPP_WRITTEN (pfile);
1147 if (!CPP_TRADITIONAL (pfile))
1148 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
1149 for (; i < arglen; i++)
1150 {
1151 c = (ARG_BASE + arg->raw)[i];
1152
1153 if (!in_string)
1154 {
Zack Weinbergeaefae02000-02-06 08:24:22 +00001155 /* Delete "\r " and "\r-" escapes. */
1156 if (c == '\r')
1157 {
1158 i++;
1159 continue;
1160 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001161 /* Internal sequences of whitespace are
1162 replaced by one space except within
1163 a string or char token. */
Zack Weinbergeaefae02000-02-06 08:24:22 +00001164 else if (is_space(c))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001165 {
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001166 if (need_space == 0)
1167 need_space = 1;
1168 continue;
1169 }
1170 else if (need_space > 0)
1171 CPP_PUTC (pfile, ' ');
1172 need_space = 0;
1173 }
1174
1175 if (escaped)
1176 escaped = 0;
1177 else
1178 {
1179 if (c == '\\')
1180 escaped = 1;
1181 if (in_string)
1182 {
1183 if (c == in_string)
1184 in_string = 0;
1185 }
1186 else if (c == '\"' || c == '\'')
1187 in_string = c;
1188 }
1189
1190 /* Escape these chars */
1191 if (c == '\"' || (in_string && c == '\\'))
1192 CPP_PUTC (pfile, '\\');
1193 if (ISPRINT (c))
1194 CPP_PUTC (pfile, c);
1195 else
1196 {
1197 CPP_RESERVE (pfile, 4);
1198 sprintf ((char *) CPP_PWRITTEN (pfile), "\\%03o",
1199 (unsigned int) c);
1200 CPP_ADJUST_WRITTEN (pfile, 4);
1201 }
1202 }
1203 if (!CPP_TRADITIONAL (pfile))
1204 CPP_PUTC (pfile, '\"'); /* insert ending quote */
1205 arg->stringified_length
1206 = CPP_WRITTEN (pfile) - arg->stringified;
1207 }
1208 xbuf_len += args[ap->argno].stringified_length;
1209 }
1210 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
Zack Weinberg4571dc22000-01-22 04:18:41 +00001211 /* Add 4 for two \r-space markers to prevent
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001212 token concatenation. */
1213 xbuf_len += args[ap->argno].raw_length + 4;
1214 else
1215 {
1216 /* We have an ordinary (expanded) occurrence of the arg.
1217 So compute its expansion, if we have not already. */
1218 if (args[ap->argno].expand_length < 0)
1219 {
1220 args[ap->argno].expanded = CPP_WRITTEN (pfile);
1221 cpp_expand_to_buffer (pfile,
1222 ARG_BASE + args[ap->argno].raw,
1223 args[ap->argno].raw_length);
1224
1225 args[ap->argno].expand_length
1226 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
1227 }
1228
Zack Weinberg4571dc22000-01-22 04:18:41 +00001229 /* Add 4 for two \r-space markers to prevent
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001230 token concatenation. */
1231 xbuf_len += args[ap->argno].expand_length + 4;
1232 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001233 }
1234
1235 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
1236
1237 /* Generate in XBUF the complete expansion
1238 with arguments substituted in.
1239 TOTLEN is the total size generated so far.
1240 OFFSET is the index in the definition
1241 of where we are copying from. */
1242 offset = totlen = 0;
1243 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
1244 last_ap = ap, ap = ap->next)
1245 {
1246 register struct argdata *arg = &args[ap->argno];
1247 int count_before = totlen;
1248
1249 /* Add chars to XBUF. */
1250 for (i = 0; i < ap->nchars; i++, offset++)
1251 xbuf[totlen++] = exp[offset];
1252
1253 /* If followed by an empty rest arg with concatenation,
1254 delete the last run of nonwhite chars. */
1255 if (rest_zero && totlen > count_before
1256 && ((ap->rest_args && ap->raw_before)
1257 || (last_ap != NULL && last_ap->rest_args
1258 && last_ap->raw_after)))
1259 {
1260 /* Delete final whitespace. */
Zack Weinberga9ae4481999-10-29 04:31:14 +00001261 while (totlen > count_before && is_space(xbuf[totlen - 1]))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001262 totlen--;
1263
1264 /* Delete the nonwhites before them. */
Zack Weinberga9ae4481999-10-29 04:31:14 +00001265 while (totlen > count_before && !is_space(xbuf[totlen - 1]))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001266 totlen--;
1267 }
1268
1269 if (ap->stringify != 0)
1270 {
1271 bcopy (ARG_BASE + arg->stringified,
1272 xbuf + totlen, arg->stringified_length);
1273 totlen += arg->stringified_length;
1274 }
1275 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
1276 {
1277 U_CHAR *p1 = ARG_BASE + arg->raw;
1278 U_CHAR *l1 = p1 + arg->raw_length;
1279 if (ap->raw_before)
1280 {
Zack Weinberg5d83f441999-08-04 20:39:33 +00001281 /* Arg is concatenated before: delete leading whitespace,
1282 whitespace markers, and no-reexpansion markers. */
1283 while (p1 != l1)
1284 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00001285 if (is_space(p1[0]))
Zack Weinberg5d83f441999-08-04 20:39:33 +00001286 p1++;
1287 else if (p1[0] == '\r')
1288 p1 += 2;
1289 else
1290 break;
1291 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001292 }
1293 if (ap->raw_after)
1294 {
1295 /* Arg is concatenated after: delete trailing whitespace,
1296 whitespace markers, and no-reexpansion markers. */
1297 while (p1 != l1)
1298 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00001299 if (is_space(l1[-1]))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001300 l1--;
Zack Weinberged45de91999-04-12 12:03:10 +00001301 else if (l1[-1] == '\r')
1302 l1--;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001303 else if (l1[-1] == '-')
1304 {
Zack Weinberged45de91999-04-12 12:03:10 +00001305 if (l1 != p1 + 1 && l1[-2] == '\r')
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001306 l1 -= 2;
1307 else
1308 break;
1309 }
1310 else
1311 break;
1312 }
1313 }
1314
1315 /* Delete any no-reexpansion marker that precedes
1316 an identifier at the beginning of the argument. */
Zack Weinberged45de91999-04-12 12:03:10 +00001317 if (p1[0] == '\r' && p1[1] == '-')
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001318 p1 += 2;
1319
1320 bcopy (p1, xbuf + totlen, l1 - p1);
1321 totlen += l1 - p1;
1322 }
1323 else
1324 {
1325 U_CHAR *expanded = ARG_BASE + arg->expanded;
1326 if (!ap->raw_before && totlen > 0 && arg->expand_length
1327 && !CPP_TRADITIONAL (pfile)
1328 && unsafe_chars (xbuf[totlen - 1], expanded[0]))
1329 {
Zack Weinberged45de91999-04-12 12:03:10 +00001330 xbuf[totlen++] = '\r';
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001331 xbuf[totlen++] = ' ';
1332 }
1333
1334 bcopy (expanded, xbuf + totlen, arg->expand_length);
1335 totlen += arg->expand_length;
1336
1337 if (!ap->raw_after && totlen > 0 && offset < defn->length
1338 && !CPP_TRADITIONAL (pfile)
1339 && unsafe_chars (xbuf[totlen - 1], exp[offset]))
1340 {
Zack Weinberged45de91999-04-12 12:03:10 +00001341 xbuf[totlen++] = '\r';
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001342 xbuf[totlen++] = ' ';
1343 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001344 }
1345
1346 if (totlen > xbuf_len)
Zack Weinberg34ca9541999-04-14 09:40:56 +00001347 {
Zack Weinbergc1212d22000-02-06 23:46:18 +00001348 cpp_ice (pfile, "buffer overrun in macroexpand");
Zack Weinberg34ca9541999-04-14 09:40:56 +00001349 return;
1350 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001351 }
1352
1353 /* if there is anything left of the definition
1354 after handling the arg list, copy that in too. */
1355
1356 for (i = offset; i < defn->length; i++)
1357 {
1358 /* if we've reached the end of the macro */
1359 if (exp[i] == ')')
1360 rest_zero = 0;
1361 if (!(rest_zero && last_ap != NULL && last_ap->rest_args
1362 && last_ap->raw_after))
1363 xbuf[totlen++] = exp[i];
1364 }
1365
1366 xbuf[totlen] = 0;
1367 xbuf_len = totlen;
1368
1369 }
1370
1371 pfile->output_escapes--;
1372
1373 /* Now put the expansion on the input stack
1374 so our caller will commence reading from it. */
1375 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
1376 CPP_BUFFER (pfile)->has_escapes = 1;
1377
1378 /* Pop the space we've used in the token_buffer for argument expansion. */
1379 CPP_SET_WRITTEN (pfile, old_written);
1380
1381 /* Recursive macro use sometimes works traditionally.
1382 #define foo(x,y) bar (x (y,0), y)
1383 foo (foo, baz) */
1384
1385 if (!CPP_TRADITIONAL (pfile))
1386 hp->type = T_DISABLED;
1387}
1388
1389/* Return 1 iff a token ending in C1 followed directly by a token C2
1390 could cause mis-tokenization. */
1391
1392static int
1393unsafe_chars (c1, c2)
1394 int c1, c2;
1395{
1396 switch (c1)
1397 {
Zack Weinberg5d83f441999-08-04 20:39:33 +00001398 case '+': case '-':
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001399 if (c2 == c1 || c2 == '=')
1400 return 1;
1401 goto letter;
1402
Zack Weinberg5d83f441999-08-04 20:39:33 +00001403 case 'e': case 'E': case 'p': case 'P':
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001404 if (c2 == '-' || c2 == '+')
1405 return 1; /* could extend a pre-processing number */
1406 goto letter;
1407
1408 case 'L':
1409 if (c2 == '\'' || c2 == '\"')
1410 return 1; /* Could turn into L"xxx" or L'xxx'. */
1411 goto letter;
1412
Zack Weinberg5d83f441999-08-04 20:39:33 +00001413 case '.': case '0': case '1': case '2': case '3':
1414 case '4': case '5': case '6': case '7': case '8': case '9':
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001415 case '_': case 'a': case 'b': case 'c': case 'd': case 'f':
1416 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1417 case 'm': case 'n': case 'o': case 'q': case 'r': case 's':
1418 case 't': case 'u': case 'v': case 'w': case 'x': case 'y':
1419 case 'z': case 'A': case 'B': case 'C': case 'D': case 'F':
1420 case 'G': case 'H': case 'I': case 'J': case 'K': case 'M':
1421 case 'N': case 'O': case 'Q': case 'R': case 'S': case 'T':
1422 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
1423 letter:
1424 /* We're in the middle of either a name or a pre-processing number. */
Zack Weinberga9ae4481999-10-29 04:31:14 +00001425 return (is_idchar(c2) || c2 == '.');
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001426
1427 case '<': case '>': case '!': case '%': case '#': case ':':
1428 case '^': case '&': case '|': case '*': case '/': case '=':
1429 return (c2 == c1 || c2 == '=');
1430 }
1431 return 0;
1432}
1433
1434static void
1435push_macro_expansion (pfile, xbuf, xbuf_len, hp)
1436 cpp_reader *pfile;
1437 register U_CHAR *xbuf;
1438 int xbuf_len;
1439 HASHNODE *hp;
1440{
1441 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
1442 if (mbuf == NULL)
1443 return;
1444 mbuf->cleanup = macro_cleanup;
1445 mbuf->data = hp;
1446
Zack Weinberged45de91999-04-12 12:03:10 +00001447 /* The first chars of the expansion should be a "\r " added by
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001448 collect_expansion. This is to prevent accidental token-pasting
1449 between the text preceding the macro invocation, and the macro
1450 expansion text.
1451
1452 We would like to avoid adding unneeded spaces (for the sake of
1453 tools that use cpp, such as imake). In some common cases we can
1454 tell that it is safe to omit the space.
1455
1456 The character before the macro invocation cannot have been an
1457 idchar (or else it would have been pasted with the idchars of
1458 the macro name). Therefore, if the first non-space character
1459 of the expansion is an idchar, we do not need the extra space
1460 to prevent token pasting.
1461
1462 Also, we don't need the extra space if the first char is '(',
1463 or some other (less common) characters. */
1464
Zack Weinberged45de91999-04-12 12:03:10 +00001465 if (xbuf[0] == '\r' && xbuf[1] == ' '
Zack Weinberga9ae4481999-10-29 04:31:14 +00001466 && (is_idchar(xbuf[2]) || xbuf[2] == '(' || xbuf[2] == '\''
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001467 || xbuf[2] == '\"'))
1468 mbuf->cur += 2;
1469
1470 /* Likewise, avoid the extra space at the end of the macro expansion
1471 if this is safe. We can do a better job here since we can know
1472 what the next char will be. */
1473 if (xbuf_len >= 3
Zack Weinberged45de91999-04-12 12:03:10 +00001474 && mbuf->rlimit[-2] == '\r'
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001475 && mbuf->rlimit[-1] == ' ')
1476 {
1477 int c1 = mbuf->rlimit[-3];
1478 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
1479 if (c2 == EOF || !unsafe_chars (c1, c2))
1480 mbuf->rlimit -= 2;
1481 }
1482}
1483
1484/* Return zero if two DEFINITIONs are isomorphic. */
1485
1486int
1487compare_defs (pfile, d1, d2)
1488 cpp_reader *pfile;
1489 DEFINITION *d1, *d2;
1490{
1491 register struct reflist *a1, *a2;
1492 register U_CHAR *p1 = d1->expansion;
1493 register U_CHAR *p2 = d2->expansion;
1494 int first = 1;
1495
1496 if (d1->nargs != d2->nargs)
1497 return 1;
1498 if (CPP_PEDANTIC (pfile)
Zack Weinbergbb52fa72000-02-10 02:23:08 +00001499 && strcmp ((char *) d1->argnames, (char *) d2->argnames))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001500 return 1;
1501 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1502 a1 = a1->next, a2 = a2->next)
1503 {
1504 if (!((a1->nchars == a2->nchars && !strncmp (p1, p2, a1->nchars))
1505 || !comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1506 || a1->argno != a2->argno
1507 || a1->stringify != a2->stringify
1508 || a1->raw_before != a2->raw_before
1509 || a1->raw_after != a2->raw_after)
1510 return 1;
1511 first = 0;
1512 p1 += a1->nchars;
1513 p2 += a2->nchars;
1514 }
1515 if (a1 != a2)
1516 return 1;
1517
1518 return comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1519 p2, d2->length - (p2 - d2->expansion), 1);
1520}
1521
1522/* Return 1 if two parts of two macro definitions are effectively different.
1523 One of the parts starts at BEG1 and has LEN1 chars;
1524 the other has LEN2 chars at BEG2.
1525 Any sequence of whitespace matches any other sequence of whitespace.
1526 FIRST means these parts are the first of a macro definition;
1527 so ignore leading whitespace entirely.
1528 LAST means these parts are the last of a macro definition;
1529 so ignore trailing whitespace entirely. */
1530
1531static int
1532comp_def_part (first, beg1, len1, beg2, len2, last)
1533 int first;
1534 U_CHAR *beg1, *beg2;
1535 int len1, len2;
1536 int last;
1537{
1538 register U_CHAR *end1 = beg1 + len1;
1539 register U_CHAR *end2 = beg2 + len2;
1540 if (first)
1541 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00001542 while (beg1 != end1 && is_space(*beg1))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001543 beg1++;
Zack Weinberga9ae4481999-10-29 04:31:14 +00001544 while (beg2 != end2 && is_space(*beg2))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001545 beg2++;
1546 }
1547 if (last)
1548 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00001549 while (beg1 != end1 && is_space(end1[-1]))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001550 end1--;
Zack Weinberga9ae4481999-10-29 04:31:14 +00001551 while (beg2 != end2 && is_space(end2[-1]))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001552 end2--;
1553 }
1554 while (beg1 != end1 && beg2 != end2)
1555 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00001556 if (is_space(*beg1) && is_space(*beg2))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001557 {
Zack Weinberga9ae4481999-10-29 04:31:14 +00001558 while (beg1 != end1 && is_space(*beg1))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001559 beg1++;
Zack Weinberga9ae4481999-10-29 04:31:14 +00001560 while (beg2 != end2 && is_space(*beg2))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001561 beg2++;
1562 }
1563 else if (*beg1 == *beg2)
1564 {
1565 beg1++;
1566 beg2++;
1567 }
1568 else
1569 break;
1570 }
1571 return (beg1 != end1) || (beg2 != end2);
1572}
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001573
1574/* Dump the definition of macro MACRO on stdout. The format is suitable
1575 to be read back in again. */
1576
1577void
1578dump_definition (pfile, macro)
1579 cpp_reader *pfile;
1580 MACRODEF macro;
1581{
1582 DEFINITION *defn = macro.defn;
1583
1584 CPP_RESERVE (pfile, macro.symlen + sizeof "#define ");
1585 CPP_PUTS_Q (pfile, "#define ", sizeof "#define " -1);
1586 CPP_PUTS_Q (pfile, macro.symnam, macro.symlen);
1587
1588 if (defn->nargs == -1)
1589 {
1590 CPP_PUTC_Q (pfile, ' ');
1591
1592 /* The first and last two characters of a macro expansion are
1593 always "\r "; this needs to be trimmed out.
1594 So we need length-4 chars of space, plus one for the NUL. */
1595 CPP_RESERVE (pfile, defn->length - 4 + 1);
1596 CPP_PUTS_Q (pfile, defn->expansion + 2, defn->length - 4);
1597 CPP_NUL_TERMINATE_Q (pfile);
1598 }
1599 else
1600 {
1601 struct reflist *r;
Zack Weinbergbb52fa72000-02-10 02:23:08 +00001602 unsigned char *argnames = (unsigned char *) xstrdup (defn->argnames);
Dave Brolleye7553be1999-06-07 10:35:27 +00001603 unsigned char **argv = (unsigned char **) alloca (defn->nargs *
1604 sizeof(char *));
1605 int *argl = (int *) alloca (defn->nargs * sizeof(int));
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001606 unsigned char *x;
1607 int i;
1608
1609 /* First extract the argument list. */
1610 x = argnames;
1611 i = defn->nargs;
1612 while (i--)
1613 {
1614 argv[i] = x;
1615 while (*x != ',' && *x != '\0') x++;
1616 argl[i] = x - argv[i];
1617 if (*x == ',')
1618 {
1619 *x = '\0';
1620 x += 2; /* skip the space after the comma */
1621 }
1622 }
1623
1624 /* Now print out the argument list. */
1625 CPP_PUTC_Q (pfile, '(');
1626 for (i = 0; i < defn->nargs; i++)
1627 {
1628 CPP_RESERVE (pfile, argl[i] + 2);
1629 CPP_PUTS_Q (pfile, argv[i], argl[i]);
1630 if (i < defn->nargs-1)
1631 CPP_PUTS_Q (pfile, ", ", 2);
1632 }
1633
1634 if (defn->rest_args)
1635 CPP_PUTS (pfile, "...) ", 5);
1636 else
1637 CPP_PUTS (pfile, ") ", 2);
1638
1639 /* Now the definition. */
1640 x = defn->expansion;
1641 for (r = defn->pattern; r; r = r->next)
1642 {
1643 i = r->nchars;
1644 if (*x == '\r') x += 2, i -= 2;
1645 /* i chars for macro text, plus the length of the macro
1646 argument name, plus one for a stringify marker, plus two for
1647 each concatenation marker. */
1648 CPP_RESERVE (pfile,
1649 i + argl[r->argno] + r->stringify
1650 + (r->raw_before + r->raw_after) * 2);
1651
1652 if (i > 0) CPP_PUTS_Q (pfile, x, i);
1653 if (r->raw_before)
1654 CPP_PUTS_Q (pfile, "##", 2);
1655 if (r->stringify)
1656 CPP_PUTC_Q (pfile, '#');
1657 CPP_PUTS_Q (pfile, argv[r->argno], argl[r->argno]);
1658 if (r->raw_after && !(r->next && r->next->nchars == 0
1659 && r->next->raw_before))
1660 CPP_PUTS_Q (pfile, "##", 2);
1661
1662 x += i;
1663 }
1664
1665 i = defn->length - (x - defn->expansion) - 2;
1666 if (*x == '\r') x += 2, i -= 2;
1667 if (i > 0) CPP_PUTS (pfile, x, i);
1668 CPP_NUL_TERMINATE (pfile);
1669 }
1670}