1 /* Part of CPP library. (Macro handling.)
2 Copyright (C) 1986, 87, 89, 92-96, 98, 1999 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
31 static int comp_def_part
PARAMS ((int, U_CHAR
*, int, U_CHAR
*,
33 static int change_newlines
PARAMS ((U_CHAR
*, int));
34 static void push_macro_expansion
PARAMS ((cpp_reader
*,
35 U_CHAR
*, int, HASHNODE
*));
36 static int unsafe_chars
PARAMS ((int, int));
37 static int macro_cleanup
PARAMS ((cpp_buffer
*, cpp_reader
*));
38 static enum cpp_token macarg
PARAMS ((cpp_reader
*, int));
39 static struct tm
*timestamp
PARAMS ((cpp_reader
*));
40 static void special_symbol
PARAMS ((HASHNODE
*, cpp_reader
*));
43 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
44 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
45 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
47 extern char *version_string
;
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
69 static DEFINITION
*collect_expansion
PARAMS ((cpp_reader
*, U_CHAR
*, U_CHAR
*,
70 int, struct arglist
*));
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
78 `use_count' is the number of times this macro arg is substituted
79 into the macro. If the actual use count exceeds 10,
80 the value stored is 10. */
82 /* raw and expanded are relative to ARG_BASE */
83 #define ARG_BASE ((pfile)->token_buffer)
87 /* Strings relative to pfile->token_buffer */
88 long raw
, expanded
, stringified
;
89 int raw_length
, expand_length
;
90 int stringified_length
;
96 /* Return hash function on name. must be compatible with the one
97 computed a step at a time, elsewhere */
100 hashf (name
, len
, hashsize
)
101 register const U_CHAR
*name
;
108 r
= HASHSTEP (r
, *name
++);
110 return MAKE_POS (r
) % hashsize
;
113 /* Find the most recent hash node for name "name" (ending with first
114 non-identifier char) installed by cpp_install
116 If LEN is >= 0, it is the length of the name.
117 Otherwise, compute the length by scanning the entire name.
119 If HASH is >= 0, it is the precomputed hash code.
120 Otherwise, compute the hash code. */
123 cpp_lookup (pfile
, name
, len
, hash
)
124 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
129 register const U_CHAR
*bp
;
130 register HASHNODE
*bucket
;
134 for (bp
= name
; is_idchar(*bp
); bp
++);
139 hash
= hashf (name
, len
, HASHSIZE
);
141 bucket
= pfile
->hashtab
[hash
];
144 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
146 bucket
= bucket
->next
;
148 return (HASHNODE
*) 0;
152 * Delete a hash node. Some weirdness to free junk from macros.
153 * More such weirdness will have to be added if you define more hash
154 * types that need it.
157 /* Note that the DEFINITION of a macro is removed from the hash table
158 but its storage is not freed. This would be a storage leak
159 except that it is not reasonable to keep undefining and redefining
160 large numbers of macros many times.
161 In any case, this is necessary, because a macro can be #undef'd
162 in the middle of reading the arguments to a call to it.
163 If #undef freed the DEFINITION, that would crash. */
170 if (hp
->prev
!= NULL
)
171 hp
->prev
->next
= hp
->next
;
172 if (hp
->next
!= NULL
)
173 hp
->next
->prev
= hp
->prev
;
175 /* make sure that the bucket chain header that
176 the deleted guy was on points to the right thing afterwards. */
177 if (hp
== *hp
->bucket_hdr
)
178 *hp
->bucket_hdr
= hp
->next
;
180 if (hp
->type
== T_MACRO
)
182 DEFINITION
*d
= hp
->value
.defn
;
183 struct reflist
*ap
, *nextap
;
185 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
)
191 free (d
->args
.argnames
);
198 /* Install a name in the main hash table, even if it is already there.
199 Name stops with first non alphanumeric, except leading '#'.
200 Caller must check against redefinition if that is desired.
201 delete_macro () removes things installed by cpp_install () in fifo order.
202 this is important because of the `defined' special symbol used
203 in #if, and also if pushdef/popdef directives are ever implemented.
205 If LEN is >= 0, it is the length of the name.
206 Otherwise, compute the length by scanning the entire name.
208 If HASH is >= 0, it is the precomputed hash code.
209 Otherwise, compute the hash code. */
212 cpp_install (pfile
, name
, len
, type
, value
, hash
)
220 register HASHNODE
*hp
;
221 register int i
, bucket
;
222 register const U_CHAR
*p
;
227 while (is_idchar(*p
))
233 hash
= hashf (name
, len
, HASHSIZE
);
235 i
= sizeof (HASHNODE
) + len
+ 1;
236 hp
= (HASHNODE
*) xmalloc (i
);
238 hp
->bucket_hdr
= &pfile
->hashtab
[bucket
];
239 hp
->next
= pfile
->hashtab
[bucket
];
240 pfile
->hashtab
[bucket
] = hp
;
242 if (hp
->next
!= NULL
)
246 hp
->value
.cpval
= value
;
247 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
248 bcopy (name
, hp
->name
, len
);
254 macro_cleanup (pbuf
, pfile
)
256 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
258 HASHNODE
*macro
= (HASHNODE
*) pbuf
->data
;
259 if (macro
->type
== T_DISABLED
)
260 macro
->type
= T_MACRO
;
261 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
267 /* Read a replacement list for a macro with parameters.
268 Build the DEFINITION structure.
269 Reads characters of text starting at BUF until END.
270 ARGLIST specifies the formal parameters to look for
271 in the text of the definition; NARGS is the number of args
272 in that list, or -1 for a macro name that wants no argument list.
273 MACRONAME is the macro name itself (so we can avoid recursive expansion)
274 and NAMELEN is its length in characters.
276 Note that comments, backslash-newlines, and leading white space
277 have already been deleted from the argument. */
280 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
284 struct arglist
*arglist
;
287 register U_CHAR
*p
, *lastp
, *exp_p
;
288 struct reflist
*endpat
= NULL
;
289 /* Pointer to first nonspace after last ## seen. */
291 /* Pointer to first nonspace after last single-# seen. */
292 U_CHAR
*stringify
= 0;
294 int expected_delimiter
= '\0';
296 /* Scan thru the replacement list, ignoring comments and quoted
297 strings, picking up on the macro calls. It does a linear search
298 thru the arg list on every potential symbol. Profiling might say
299 that something smarter should happen. */
303 cpp_fatal (pfile
, "internal error: limit < buf in collect_expansion");
304 limit
= buf
; /* treat it like a null defn */
307 /* Find the beginning of the trailing whitespace. */
309 while (p
< limit
&& is_space(limit
[-1]))
312 /* Allocate space for the text in the macro definition.
313 Leading and trailing whitespace chars need 2 bytes each.
314 Each other input char may or may not need 1 byte,
315 so this is an upper bound. The extra 5 are for invented
316 leading and trailing newline-marker and final null. */
317 maxsize
= (sizeof (DEFINITION
)
319 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
322 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
327 /* Add one initial space escape-marker to prevent accidental
328 token-pasting (often removed by macroexpand). */
332 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#')
334 cpp_error (pfile
, "`##' at start of macro definition");
338 /* Process the main body of the definition. */
342 register U_CHAR c
= *p
++;
346 if (!CPP_TRADITIONAL (pfile
))
352 if (expected_delimiter
!= '\0')
354 if (c
== expected_delimiter
)
355 expected_delimiter
= '\0';
358 expected_delimiter
= c
;
362 if (p
< limit
&& expected_delimiter
)
364 /* In a string, backslash goes through
365 and makes next char ordinary. */
371 /* # is ordinary inside a string. */
372 if (expected_delimiter
)
374 if (p
< limit
&& *p
== '#')
376 /* ##: concatenate preceding and following tokens. */
377 /* Take out the first #, discard preceding whitespace. */
379 while (exp_p
> lastp
&& is_hspace(exp_p
[-1]))
381 /* Skip the second #. */
383 /* Discard following whitespace. */
384 SKIP_WHITE_SPACE (p
);
387 cpp_error (pfile
, "`##' at end of macro definition");
391 /* Single #: stringify following argument ref.
392 Don't leave the # in the expansion. */
394 SKIP_WHITE_SPACE (p
);
395 if (p
== limit
|| !is_idstart(*p
)
396 || (*p
== 'L' && p
+ 1 < limit
&& (p
[1] == '\'' ||
399 "`#' operator is not followed by a macro argument name");
408 /* In -traditional mode, recognize arguments inside strings and
409 character constants, and ignore special properties of #.
410 Arguments inside strings are considered "stringified", but no
411 extra quote marks are supplied. */
416 if (expected_delimiter
!= '\0')
418 if (c
== expected_delimiter
)
419 expected_delimiter
= '\0';
422 expected_delimiter
= c
;
426 /* Backslash quotes delimiters and itself,
427 but not macro args. */
428 if (expected_delimiter
!= 0 && p
< limit
429 && (*p
== expected_delimiter
|| *p
== '\\'))
437 if (expected_delimiter
!= '\0')
438 /* No comments inside strings. */
442 /* If we find a comment that wasn't removed by
443 handle_directive, this must be -traditional.
444 So replace the comment with nothing at all. */
447 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
450 /* Mark this as a concatenation-point,
451 as if it had been ##. */
459 /* Handle the start of a symbol. */
460 if (is_idchar(c
) && nargs
> 0)
462 U_CHAR
*id_beg
= p
- 1;
466 while (p
!= limit
&& is_idchar(*p
))
471 && !(id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"')))
473 register struct arglist
*arg
;
475 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
)
477 struct reflist
*tpat
;
479 if (arg
->name
[0] == c
480 && arg
->length
== id_len
481 && strncmp (arg
->name
, id_beg
, id_len
) == 0)
483 if (expected_delimiter
&& CPP_OPTIONS
484 (pfile
)->warn_stringify
)
486 if (CPP_TRADITIONAL (pfile
))
489 "macro argument `%.*s' is stringified.",
495 "macro arg `%.*s' would be stringified with -traditional.",
499 /* If ANSI, don't actually substitute
501 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
503 /* make a pat node for this arg and append it
504 to the end of the pat list */
505 tpat
= (struct reflist
*)
506 xmalloc (sizeof (struct reflist
));
508 tpat
->raw_before
= concat
== id_beg
;
510 tpat
->rest_args
= arg
->rest_args
;
511 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
512 ? expected_delimiter
!= '\0'
513 : stringify
== id_beg
);
516 defn
->pattern
= tpat
;
521 tpat
->argno
= arg
->argno
;
522 tpat
->nchars
= exp_p
- lastp
;
524 register U_CHAR
*p1
= p
;
525 SKIP_WHITE_SPACE (p1
);
526 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
536 /* If this was not a macro arg, copy it into the expansion. */
539 register U_CHAR
*lim1
= p
;
543 if (stringify
== id_beg
)
545 "`#' operator should be followed by a macro argument name");
550 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
552 /* If ANSI, put in a "\r " marker to prevent token pasting.
553 But not if "inside a string" (which in ANSI mode
554 happens only for -D option). */
561 defn
->length
= exp_p
- defn
->expansion
;
563 /* Crash now if we overrun the allocated size. */
564 if (defn
->length
+ 1 > maxsize
)
568 /* This isn't worth the time it takes. */
569 /* give back excess storage */
570 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
577 * special extension string that can be added to the last macro argument to
578 * allow it to absorb the "rest" of the arguments when expanded. Ex:
579 * #define wow(a, b...) process (b, a, b)
580 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
581 * { wow (one, two); } -> { process (two, one, two); }
582 * if this "rest_arg" is used with the concat token '##' and if it is not
583 * supplied then the token attached to with ## will not be outputted. Ex:
584 * #define wow (a, b...) process (b ## , a, ## b)
585 * { wow (1, 2); } -> { process (2, 1, 2); }
586 * { wow (one); } -> { process (one); {
588 static char rest_extension
[] = "...";
589 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
591 /* Create a DEFINITION node from a #define directive. Arguments are
595 create_definition (buf
, limit
, pfile
, predefinition
)
600 U_CHAR
*bp
; /* temp ptr into input buffer */
601 U_CHAR
*symname
; /* remember where symbol name starts */
602 int sym_length
; /* and how long it is */
606 CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
608 int arglengths
= 0; /* Accumulate lengths of arg names
609 plus number of args. */
611 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
615 while (is_hspace(*bp
))
618 symname
= bp
; /* remember where it starts */
620 sym_length
= check_macro_name (pfile
, bp
);
623 /* Lossage will occur if identifiers or control keywords are broken
624 across lines using backslash. This is not the right place to take
629 struct arglist
*arg_ptrs
= NULL
;
633 SKIP_WHITE_SPACE (bp
);
635 /* Loop over macro argument names. */
638 struct arglist
*temp
;
640 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
642 temp
->next
= arg_ptrs
;
643 temp
->argno
= argno
++;
648 cpp_pedwarn (pfile
, "another parameter follows `%s'",
651 if (!is_idstart(*bp
))
652 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
654 /* Find the end of the arg name. */
655 while (is_idchar(*bp
))
658 /* do we have a "special" rest-args extension here? */
659 if ((size_t) (limit
- bp
) > REST_EXTENSION_LENGTH
660 && !strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
))
667 temp
->length
= bp
- temp
->name
;
669 bp
+= REST_EXTENSION_LENGTH
;
670 arglengths
+= temp
->length
+ 2;
671 SKIP_WHITE_SPACE (bp
);
672 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')'))
675 "badly punctuated parameter list in `#define'");
681 SKIP_WHITE_SPACE (bp
);
685 cpp_error (pfile
, "unterminated parameter list in `#define'");
689 struct arglist
*otemp
;
691 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
692 if (temp
->length
== otemp
->length
693 && strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0)
697 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
698 (void) strncpy (name
, temp
->name
, temp
->length
);
699 name
[temp
->length
] = '\0';
701 "duplicate argument name `%s' in `#define'",
708 ++bp
; /* skip paren */
709 SKIP_WHITE_SPACE (bp
);
710 /* now everything from bp before limit is the definition. */
711 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
712 defn
->rest_args
= rest_args
;
714 /* Now set defn->args.argnames to the result of concatenating
715 the argument names in reverse order
716 with comma-space between them. */
717 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
719 struct arglist
*temp
;
721 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
)
723 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
727 defn
->args
.argnames
[i
++] = ',';
728 defn
->args
.argnames
[i
++] = ' ';
731 defn
->args
.argnames
[i
] = 0;
736 /* Simple expansion or empty definition. */
743 SKIP_WHITE_SPACE (bp
);
746 /* Per C9x, missing white space after the name in a #define
747 of an object-like macro is always a constraint violation. */
749 "missing white space after `#define %.*s'",
750 sym_length
, symname
);
752 /* now everything from bp before limit is the definition. */
753 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
754 defn
->args
.argnames
= (U_CHAR
*) "";
760 /* OP is null if this is a predefinition */
761 defn
->predefined
= predefinition
;
763 mdef
.symnam
= symname
;
764 mdef
.symlen
= sym_length
;
774 * Parse a macro argument and append the info on PFILE's token_buffer.
775 * REST_ARGS means to absorb the rest of the args.
776 * Return nonzero to indicate a syntax error.
779 static enum cpp_token
780 macarg (pfile
, rest_args
)
785 enum cpp_token token
;
786 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
787 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
789 /* Try to parse as much of the argument as exists at this
790 input stack level. */
791 pfile
->no_macro_expand
++;
792 CPP_OPTIONS (pfile
)->no_line_commands
++;
795 token
= cpp_get_token (pfile
);
801 /* If we've hit end of file, it's an error (reported by caller).
802 Ditto if it's the end of cpp_expand_to_buffer text.
803 If we've hit end of macro, just continue. */
804 if (!CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
815 /* if we've returned to lowest level and
816 we aren't absorbing all args */
817 if (paren
== 0 && rest_args
== 0)
821 /* Remove ',' or ')' from argument buffer. */
822 CPP_ADJUST_WRITTEN (pfile
, -1);
829 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
830 CPP_OPTIONS (pfile
)->no_line_commands
--;
831 pfile
->no_macro_expand
--;
836 /* Turn newlines to spaces in the string of length LENGTH at START,
837 except inside of string constants.
838 The string is copied into itself with its beginning staying fixed. */
841 change_newlines (start
, length
)
845 register U_CHAR
*ibp
;
846 register U_CHAR
*obp
;
847 register U_CHAR
*limit
;
851 limit
= start
+ length
;
862 /* Notice and skip strings, so that we don't
863 delete newlines in them. */
871 if (c
== '\n' && quotec
== '\'')
889 time_t t
= time ((time_t *) 0);
890 pfile
->timebuf
= localtime (&t
);
892 return pfile
->timebuf
;
895 static const char * const monthnames
[] =
897 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
898 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
902 * expand things like __FILE__. Place the expansion into the output
903 * buffer *without* rescanning.
907 special_symbol (hp
, pfile
)
920 ip
= CPP_BUFFER (pfile
);
921 if (hp
->type
== T_BASE_FILE
)
923 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
924 ip
= CPP_PREV_BUFFER (ip
);
928 ip
= CPP_BUFFER (pfile
);
929 while (!ip
->nominal_fname
&& ip
!= CPP_NULL_BUFFER (pfile
))
930 ip
= CPP_PREV_BUFFER (ip
);
933 buf
= ip
->nominal_fname
;
937 CPP_RESERVE (pfile
, 3 + 4 * strlen (buf
));
938 quote_string (pfile
, buf
);
942 case T_INCLUDE_LEVEL
:
944 int true_indepth
= 0;
945 ip
= CPP_BUFFER (pfile
);
946 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
947 if (ip
->fname
!= NULL
)
950 CPP_RESERVE (pfile
, 10);
951 sprintf (CPP_PWRITTEN (pfile
), "%d", true_indepth
);
952 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
957 len
= strlen (version_string
);
958 CPP_RESERVE (pfile
, 3 + len
);
959 CPP_PUTC_Q (pfile
, '"');
960 CPP_PUTS_Q (pfile
, version_string
, len
);
961 CPP_PUTC_Q (pfile
, '"');
962 CPP_NUL_TERMINATE_Q (pfile
);
966 buf
= hp
->value
.cpval
;
973 CPP_RESERVE (pfile
, len
+ 1);
974 CPP_PUTS_Q (pfile
, buf
, len
);
975 CPP_NUL_TERMINATE_Q (pfile
);
979 CPP_RESERVE (pfile
, 2);
980 #ifdef STDC_0_IN_SYSTEM_HEADERS
981 ip
= CPP_BUFFER (pfile
);
982 while (!ip
->nominal_fname
&& ip
!= CPP_NULL_BUFFER (pfile
))
983 ip
= CPP_PREV_BUFFER (ip
);
984 if (ip
->system_header_p
985 && !cpp_lookup (pfile
, (U_CHAR
*) "__STRICT_ANSI__", 15, -1))
986 CPP_PUTC_Q (pfile
, '0');
989 CPP_PUTC_Q (pfile
, '1');
990 CPP_NUL_TERMINATE_Q (pfile
);
996 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &line
, NULL
);
998 CPP_RESERVE (pfile
, 10);
999 sprintf (CPP_PWRITTEN (pfile
), "%ld", line
);
1000 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
1009 CPP_RESERVE (pfile
, 20);
1010 timebuf
= timestamp (pfile
);
1011 if (hp
->type
== T_DATE
)
1012 sprintf (CPP_PWRITTEN (pfile
), "\"%s %2d %4d\"",
1013 monthnames
[timebuf
->tm_mon
],
1014 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
1016 sprintf (CPP_PWRITTEN (pfile
), "\"%02d:%02d:%02d\"",
1017 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
1019 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
1024 cpp_error (pfile
, "attempt to use poisoned `%s'.", hp
->name
);
1025 CPP_RESERVE (pfile
, 1);
1026 CPP_PUTC_Q (pfile
, '0');
1027 CPP_NUL_TERMINATE_Q (pfile
);
1031 cpp_fatal (pfile
, "cpplib internal error: invalid special hash type");
1036 /* Expand a macro call.
1037 HP points to the symbol that is the macro being called.
1038 Put the result of expansion onto the input stack
1039 so that subsequent input by our caller will use it.
1041 If macro wants arguments, caller has already verified that
1042 an argument list follows; arguments come from the input stack. */
1045 macroexpand (pfile
, hp
)
1051 register U_CHAR
*xbuf
;
1052 long start_line
, start_column
;
1054 struct argdata
*args
;
1055 long old_written
= CPP_WRITTEN (pfile
);
1057 int start_line
= instack
[indepth
].lineno
;
1059 int rest_args
, rest_zero
;
1063 /* This macro is being used inside a #if, which means it must be */
1064 /* recorded as a precondition. */
1065 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
1066 dump_single_macro (hp
, pcp_outfile
);
1069 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
1071 /* Check for and handle special symbols. */
1072 if (hp
->type
!= T_MACRO
)
1074 special_symbol (hp
, pfile
);
1075 xbuf_len
= CPP_WRITTEN (pfile
) - old_written
;
1076 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
1077 CPP_SET_WRITTEN (pfile
, old_written
);
1078 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
1079 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
1080 CPP_BUFFER (pfile
)->has_escapes
= 1;
1084 defn
= hp
->value
.defn
;
1085 nargs
= defn
->nargs
;
1086 pfile
->output_escapes
++;
1090 enum cpp_token token
= CPP_EOF
;
1092 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
1094 for (i
= 0; i
< nargs
; i
++)
1096 args
[i
].raw
= args
[i
].expanded
= 0;
1097 args
[i
].raw_length
= 0;
1098 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
1099 args
[i
].use_count
= 0;
1102 /* Parse all the macro args that are supplied. I counts them.
1103 The first NARGS args are stored in ARGS.
1104 The rest are discarded. If rest_args is set then we assume
1105 macarg absorbed the rest of the args. */
1109 FORWARD (1); /* Discard open-parenthesis before first arg. */
1114 if (i
< nargs
|| (nargs
== 0 && i
== 0))
1116 /* if we are working on last arg which absorbs rest of args... */
1117 if (i
== nargs
- 1 && defn
->rest_args
)
1119 args
[i
].raw
= CPP_WRITTEN (pfile
);
1120 token
= macarg (pfile
, rest_args
);
1121 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
1122 args
[i
].newlines
= 0; /* FIXME */
1125 token
= macarg (pfile
, 0);
1126 if (token
== CPP_EOF
|| token
== CPP_POP
)
1128 cpp_error_with_line (pfile
, start_line
, start_column
,
1129 "unterminated macro call");
1134 while (token
== CPP_COMMA
);
1136 /* If we got one arg but it was just whitespace, call that 0 args. */
1139 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
1140 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
1141 /* cpp.texi says for foo ( ) we provide one argument.
1142 However, if foo wants just 0 arguments, treat this as 0. */
1144 while (bp
!= lim
&& is_space(*bp
))
1150 /* Don't output an error message if we have already output one for
1151 a parse error above. */
1153 if (nargs
== 0 && i
> 0)
1155 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
1159 /* traditional C allows foo() if foo wants one argument. */
1160 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
1162 /* the rest args token is allowed to absorb 0 tokens */
1163 else if (i
== nargs
- 1 && defn
->rest_args
)
1166 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
1168 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
1170 cpp_error (pfile
, "macro `%s' used with only %d args",
1176 "macro `%s' used with too many (%d) args", hp
->name
, i
);
1180 /* If macro wants zero args, we parsed the arglist for checking only.
1181 Read directly from the macro definition. */
1184 xbuf
= defn
->expansion
;
1185 xbuf_len
= defn
->length
;
1189 register U_CHAR
*exp
= defn
->expansion
;
1190 register int offset
; /* offset in expansion,
1191 copied a piece at a time */
1192 register int totlen
; /* total amount of exp buffer filled so far */
1194 register struct reflist
*ap
, *last_ap
;
1196 /* Macro really takes args. Compute the expansion of this call. */
1198 /* Compute length in characters of the macro's expansion.
1199 Also count number of times each arg is used. */
1200 xbuf_len
= defn
->length
;
1201 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
1205 register struct argdata
*arg
= &args
[ap
->argno
];
1206 /* Stringify if it hasn't already been */
1207 if (arg
->stringified_length
< 0)
1209 int arglen
= arg
->raw_length
;
1213 /* Initially need_space is -1. Otherwise, 1 means the
1214 previous character was a space, but we suppressed it;
1215 0 means the previous character was a non-space. */
1216 int need_space
= -1;
1218 arg
->stringified
= CPP_WRITTEN (pfile
);
1219 if (!CPP_TRADITIONAL (pfile
))
1220 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
1221 for (; i
< arglen
; i
++)
1223 c
= (ARG_BASE
+ arg
->raw
)[i
];
1227 /* Internal sequences of whitespace are
1228 replaced by one space except within
1229 a string or char token. */
1232 if (CPP_WRITTEN (pfile
) > (unsigned) arg
->stringified
1233 && (CPP_PWRITTEN (pfile
))[-1] == '\r')
1235 /* "\r " escape markers are removed */
1236 CPP_ADJUST_WRITTEN (pfile
, -1);
1239 if (need_space
== 0)
1243 else if (need_space
> 0)
1244 CPP_PUTC (pfile
, ' ');
1259 else if (c
== '\"' || c
== '\'')
1263 /* Escape these chars */
1264 if (c
== '\"' || (in_string
&& c
== '\\'))
1265 CPP_PUTC (pfile
, '\\');
1267 CPP_PUTC (pfile
, c
);
1270 CPP_RESERVE (pfile
, 4);
1271 sprintf ((char *) CPP_PWRITTEN (pfile
), "\\%03o",
1273 CPP_ADJUST_WRITTEN (pfile
, 4);
1276 if (!CPP_TRADITIONAL (pfile
))
1277 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
1278 arg
->stringified_length
1279 = CPP_WRITTEN (pfile
) - arg
->stringified
;
1281 xbuf_len
+= args
[ap
->argno
].stringified_length
;
1283 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
1284 /* Add 4 for two newline-space markers to prevent
1285 token concatenation. */
1286 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
1289 /* We have an ordinary (expanded) occurrence of the arg.
1290 So compute its expansion, if we have not already. */
1291 if (args
[ap
->argno
].expand_length
< 0)
1293 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
1294 cpp_expand_to_buffer (pfile
,
1295 ARG_BASE
+ args
[ap
->argno
].raw
,
1296 args
[ap
->argno
].raw_length
);
1298 args
[ap
->argno
].expand_length
1299 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
1302 /* Add 4 for two newline-space markers to prevent
1303 token concatenation. */
1304 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
1306 if (args
[ap
->argno
].use_count
< 10)
1307 args
[ap
->argno
].use_count
++;
1310 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
1312 /* Generate in XBUF the complete expansion
1313 with arguments substituted in.
1314 TOTLEN is the total size generated so far.
1315 OFFSET is the index in the definition
1316 of where we are copying from. */
1317 offset
= totlen
= 0;
1318 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
1319 last_ap
= ap
, ap
= ap
->next
)
1321 register struct argdata
*arg
= &args
[ap
->argno
];
1322 int count_before
= totlen
;
1324 /* Add chars to XBUF. */
1325 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
1326 xbuf
[totlen
++] = exp
[offset
];
1328 /* If followed by an empty rest arg with concatenation,
1329 delete the last run of nonwhite chars. */
1330 if (rest_zero
&& totlen
> count_before
1331 && ((ap
->rest_args
&& ap
->raw_before
)
1332 || (last_ap
!= NULL
&& last_ap
->rest_args
1333 && last_ap
->raw_after
)))
1335 /* Delete final whitespace. */
1336 while (totlen
> count_before
&& is_space(xbuf
[totlen
- 1]))
1339 /* Delete the nonwhites before them. */
1340 while (totlen
> count_before
&& !is_space(xbuf
[totlen
- 1]))
1344 if (ap
->stringify
!= 0)
1346 bcopy (ARG_BASE
+ arg
->stringified
,
1347 xbuf
+ totlen
, arg
->stringified_length
);
1348 totlen
+= arg
->stringified_length
;
1350 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
1352 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
1353 U_CHAR
*l1
= p1
+ arg
->raw_length
;
1356 /* Arg is concatenated before: delete leading whitespace,
1357 whitespace markers, and no-reexpansion markers. */
1360 if (is_space(p1
[0]))
1362 else if (p1
[0] == '\r')
1370 /* Arg is concatenated after: delete trailing whitespace,
1371 whitespace markers, and no-reexpansion markers. */
1374 if (is_space(l1
[-1]))
1376 else if (l1
[-1] == '\r')
1378 else if (l1
[-1] == '-')
1380 if (l1
!= p1
+ 1 && l1
[-2] == '\r')
1390 /* Delete any no-reexpansion marker that precedes
1391 an identifier at the beginning of the argument. */
1392 if (p1
[0] == '\r' && p1
[1] == '-')
1395 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
1400 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
1401 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
1402 && !CPP_TRADITIONAL (pfile
)
1403 && unsafe_chars (xbuf
[totlen
- 1], expanded
[0]))
1405 xbuf
[totlen
++] = '\r';
1406 xbuf
[totlen
++] = ' ';
1409 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
1410 totlen
+= arg
->expand_length
;
1412 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
1413 && !CPP_TRADITIONAL (pfile
)
1414 && unsafe_chars (xbuf
[totlen
- 1], exp
[offset
]))
1416 xbuf
[totlen
++] = '\r';
1417 xbuf
[totlen
++] = ' ';
1420 /* If a macro argument with newlines is used multiple times,
1421 then only expand the newlines once. This avoids creating
1422 output lines which don't correspond to any input line,
1423 which confuses gdb and gcov. */
1424 if (arg
->use_count
> 1 && arg
->newlines
> 0)
1426 /* Don't bother doing change_newlines for subsequent
1430 = change_newlines (expanded
, arg
->expand_length
);
1434 if (totlen
> xbuf_len
)
1436 cpp_fatal (pfile
, "internal_error: buffer overrun in macroexpand");
1441 /* if there is anything left of the definition
1442 after handling the arg list, copy that in too. */
1444 for (i
= offset
; i
< defn
->length
; i
++)
1446 /* if we've reached the end of the macro */
1449 if (!(rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
1450 && last_ap
->raw_after
))
1451 xbuf
[totlen
++] = exp
[i
];
1459 pfile
->output_escapes
--;
1461 /* Now put the expansion on the input stack
1462 so our caller will commence reading from it. */
1463 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
1464 CPP_BUFFER (pfile
)->has_escapes
= 1;
1466 /* Pop the space we've used in the token_buffer for argument expansion. */
1467 CPP_SET_WRITTEN (pfile
, old_written
);
1469 /* Recursive macro use sometimes works traditionally.
1470 #define foo(x,y) bar (x (y,0), y)
1473 if (!CPP_TRADITIONAL (pfile
))
1474 hp
->type
= T_DISABLED
;
1477 /* Return 1 iff a token ending in C1 followed directly by a token C2
1478 could cause mis-tokenization. */
1481 unsafe_chars (c1
, c2
)
1487 if (c2
== c1
|| c2
== '=')
1491 case 'e': case 'E': case 'p': case 'P':
1492 if (c2
== '-' || c2
== '+')
1493 return 1; /* could extend a pre-processing number */
1497 if (c2
== '\'' || c2
== '\"')
1498 return 1; /* Could turn into L"xxx" or L'xxx'. */
1501 case '.': case '0': case '1': case '2': case '3':
1502 case '4': case '5': case '6': case '7': case '8': case '9':
1503 case '_': case 'a': case 'b': case 'c': case 'd': case 'f':
1504 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1505 case 'm': case 'n': case 'o': case 'q': case 'r': case 's':
1506 case 't': case 'u': case 'v': case 'w': case 'x': case 'y':
1507 case 'z': case 'A': case 'B': case 'C': case 'D': case 'F':
1508 case 'G': case 'H': case 'I': case 'J': case 'K': case 'M':
1509 case 'N': case 'O': case 'Q': case 'R': case 'S': case 'T':
1510 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
1512 /* We're in the middle of either a name or a pre-processing number. */
1513 return (is_idchar(c2
) || c2
== '.');
1515 case '<': case '>': case '!': case '%': case '#': case ':':
1516 case '^': case '&': case '|': case '*': case '/': case '=':
1517 return (c2
== c1
|| c2
== '=');
1523 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
1525 register U_CHAR
*xbuf
;
1529 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
1532 mbuf
->cleanup
= macro_cleanup
;
1535 /* The first chars of the expansion should be a "\r " added by
1536 collect_expansion. This is to prevent accidental token-pasting
1537 between the text preceding the macro invocation, and the macro
1540 We would like to avoid adding unneeded spaces (for the sake of
1541 tools that use cpp, such as imake). In some common cases we can
1542 tell that it is safe to omit the space.
1544 The character before the macro invocation cannot have been an
1545 idchar (or else it would have been pasted with the idchars of
1546 the macro name). Therefore, if the first non-space character
1547 of the expansion is an idchar, we do not need the extra space
1548 to prevent token pasting.
1550 Also, we don't need the extra space if the first char is '(',
1551 or some other (less common) characters. */
1553 if (xbuf
[0] == '\r' && xbuf
[1] == ' '
1554 && (is_idchar(xbuf
[2]) || xbuf
[2] == '(' || xbuf
[2] == '\''
1555 || xbuf
[2] == '\"'))
1558 /* Likewise, avoid the extra space at the end of the macro expansion
1559 if this is safe. We can do a better job here since we can know
1560 what the next char will be. */
1562 && mbuf
->rlimit
[-2] == '\r'
1563 && mbuf
->rlimit
[-1] == ' ')
1565 int c1
= mbuf
->rlimit
[-3];
1566 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
1567 if (c2
== EOF
|| !unsafe_chars (c1
, c2
))
1572 /* Return zero if two DEFINITIONs are isomorphic. */
1575 compare_defs (pfile
, d1
, d2
)
1577 DEFINITION
*d1
, *d2
;
1579 register struct reflist
*a1
, *a2
;
1580 register U_CHAR
*p1
= d1
->expansion
;
1581 register U_CHAR
*p2
= d2
->expansion
;
1584 if (d1
->nargs
!= d2
->nargs
)
1586 if (CPP_PEDANTIC (pfile
)
1587 && strcmp ((char *) d1
->args
.argnames
, (char *) d2
->args
.argnames
))
1589 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1590 a1
= a1
->next
, a2
= a2
->next
)
1592 if (!((a1
->nchars
== a2
->nchars
&& !strncmp (p1
, p2
, a1
->nchars
))
1593 || !comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1594 || a1
->argno
!= a2
->argno
1595 || a1
->stringify
!= a2
->stringify
1596 || a1
->raw_before
!= a2
->raw_before
1597 || a1
->raw_after
!= a2
->raw_after
)
1606 return comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1607 p2
, d2
->length
- (p2
- d2
->expansion
), 1);
1610 /* Return 1 if two parts of two macro definitions are effectively different.
1611 One of the parts starts at BEG1 and has LEN1 chars;
1612 the other has LEN2 chars at BEG2.
1613 Any sequence of whitespace matches any other sequence of whitespace.
1614 FIRST means these parts are the first of a macro definition;
1615 so ignore leading whitespace entirely.
1616 LAST means these parts are the last of a macro definition;
1617 so ignore trailing whitespace entirely. */
1620 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1622 U_CHAR
*beg1
, *beg2
;
1626 register U_CHAR
*end1
= beg1
+ len1
;
1627 register U_CHAR
*end2
= beg2
+ len2
;
1630 while (beg1
!= end1
&& is_space(*beg1
))
1632 while (beg2
!= end2
&& is_space(*beg2
))
1637 while (beg1
!= end1
&& is_space(end1
[-1]))
1639 while (beg2
!= end2
&& is_space(end2
[-1]))
1642 while (beg1
!= end1
&& beg2
!= end2
)
1644 if (is_space(*beg1
) && is_space(*beg2
))
1646 while (beg1
!= end1
&& is_space(*beg1
))
1648 while (beg2
!= end2
&& is_space(*beg2
))
1651 else if (*beg1
== *beg2
)
1659 return (beg1
!= end1
) || (beg2
!= end2
);
1662 /* Dump the definition of macro MACRO on stdout. The format is suitable
1663 to be read back in again. */
1666 dump_definition (pfile
, macro
)
1670 DEFINITION
*defn
= macro
.defn
;
1672 CPP_RESERVE (pfile
, macro
.symlen
+ sizeof "#define ");
1673 CPP_PUTS_Q (pfile
, "#define ", sizeof "#define " -1);
1674 CPP_PUTS_Q (pfile
, macro
.symnam
, macro
.symlen
);
1676 if (defn
->nargs
== -1)
1678 CPP_PUTC_Q (pfile
, ' ');
1680 /* The first and last two characters of a macro expansion are
1681 always "\r "; this needs to be trimmed out.
1682 So we need length-4 chars of space, plus one for the NUL. */
1683 CPP_RESERVE (pfile
, defn
->length
- 4 + 1);
1684 CPP_PUTS_Q (pfile
, defn
->expansion
+ 2, defn
->length
- 4);
1685 CPP_NUL_TERMINATE_Q (pfile
);
1690 unsigned char *argnames
= (unsigned char *) xstrdup (defn
->args
.argnames
);
1691 unsigned char **argv
= (unsigned char **) alloca (defn
->nargs
*
1693 int *argl
= (int *) alloca (defn
->nargs
* sizeof(int));
1697 /* First extract the argument list. */
1703 while (*x
!= ',' && *x
!= '\0') x
++;
1704 argl
[i
] = x
- argv
[i
];
1708 x
+= 2; /* skip the space after the comma */
1712 /* Now print out the argument list. */
1713 CPP_PUTC_Q (pfile
, '(');
1714 for (i
= 0; i
< defn
->nargs
; i
++)
1716 CPP_RESERVE (pfile
, argl
[i
] + 2);
1717 CPP_PUTS_Q (pfile
, argv
[i
], argl
[i
]);
1718 if (i
< defn
->nargs
-1)
1719 CPP_PUTS_Q (pfile
, ", ", 2);
1722 if (defn
->rest_args
)
1723 CPP_PUTS (pfile
, "...) ", 5);
1725 CPP_PUTS (pfile
, ") ", 2);
1727 /* Now the definition. */
1728 x
= defn
->expansion
;
1729 for (r
= defn
->pattern
; r
; r
= r
->next
)
1732 if (*x
== '\r') x
+= 2, i
-= 2;
1733 /* i chars for macro text, plus the length of the macro
1734 argument name, plus one for a stringify marker, plus two for
1735 each concatenation marker. */
1737 i
+ argl
[r
->argno
] + r
->stringify
1738 + (r
->raw_before
+ r
->raw_after
) * 2);
1740 if (i
> 0) CPP_PUTS_Q (pfile
, x
, i
);
1742 CPP_PUTS_Q (pfile
, "##", 2);
1744 CPP_PUTC_Q (pfile
, '#');
1745 CPP_PUTS_Q (pfile
, argv
[r
->argno
], argl
[r
->argno
]);
1746 if (r
->raw_after
&& !(r
->next
&& r
->next
->nchars
== 0
1747 && r
->next
->raw_before
))
1748 CPP_PUTS_Q (pfile
, "##", 2);
1753 i
= defn
->length
- (x
- defn
->expansion
) - 2;
1754 if (*x
== '\r') x
+= 2, i
-= 2;
1755 if (i
> 0) CPP_PUTS (pfile
, x
, i
);
1756 CPP_NUL_TERMINATE (pfile
);