]>
Commit | Line | Data |
---|---|---|
93c80368 | 1 | /* Part of CPP library. (Macro and #define handling.) |
711b8824 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998, |
4b49c365 | 3 | 1999, 2000, 2001 Free Software Foundation, Inc. |
711b8824 ZW |
4 | Written by Per Bothner, 1994. |
5 | Based on CCCP program by Paul Rubin, June 1986 | |
6 | Adapted to ANSI C, Richard Stallman, Jan 1987 | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by the | |
10 | Free Software Foundation; either version 2, or (at your option) any | |
11 | later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
21 | ||
22 | In other words, you are welcome to use, share and improve this program. | |
23 | You are forbidden to forbid anyone else to use, share and improve | |
24 | what you give them. Help stamp out software-hoarding! */ | |
25 | ||
26 | #include "config.h" | |
27 | #include "system.h" | |
93c80368 | 28 | #include "intl.h" /* for _("<command line>") below. */ |
711b8824 ZW |
29 | #include "cpplib.h" |
30 | #include "cpphash.h" | |
31 | ||
510fbf86 NB |
32 | #ifndef STDC_0_IN_SYSTEM_HEADERS |
33 | #define STDC_0_IN_SYSTEM_HEADERS 0 /* Boolean macro. */ | |
34 | #endif | |
35 | ||
93c80368 | 36 | struct cpp_macro |
711b8824 | 37 | { |
93c80368 NB |
38 | cpp_hashnode **params; /* Parameters, if any. */ |
39 | cpp_token *expansion; /* First token of replacement list. */ | |
40 | const char *file; /* Defined in file name. */ | |
41 | unsigned int line; /* Starting line number. */ | |
42 | unsigned int count; /* Number of tokens in expansion. */ | |
43 | unsigned short paramc; /* Number of parameters. */ | |
44 | unsigned int fun_like : 1; /* If a function-like macro. */ | |
28e0f040 | 45 | unsigned int variadic : 1; /* If a variadic macro. */ |
93c80368 NB |
46 | unsigned int disabled : 1; /* If macro is disabled. */ |
47 | }; | |
48 | ||
49 | typedef struct macro_arg macro_arg; | |
50 | struct macro_arg | |
51 | { | |
52 | cpp_token *first; /* First token in unexpanded argument. */ | |
53 | cpp_token *expanded; /* Macro-expanded argument. */ | |
54 | cpp_token *stringified; /* Stringified argument. */ | |
55 | unsigned int count; /* # of tokens in argument. */ | |
56 | unsigned int expanded_count; /* # of tokens in expanded argument. */ | |
711b8824 ZW |
57 | }; |
58 | ||
93c80368 NB |
59 | /* Macro expansion. */ |
60 | ||
61 | static void lock_pools PARAMS ((cpp_reader *)); | |
62 | static void unlock_pools PARAMS ((cpp_reader *)); | |
29b10746 | 63 | static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *)); |
93c80368 NB |
64 | static void builtin_macro PARAMS ((cpp_reader *, cpp_token *)); |
65 | static cpp_context *push_arg_context PARAMS ((cpp_reader *, macro_arg *)); | |
66 | static enum cpp_ttype parse_arg PARAMS ((cpp_reader *, macro_arg *, int)); | |
67 | static macro_arg *parse_args PARAMS ((cpp_reader *, const cpp_hashnode *)); | |
68 | static cpp_context *next_context PARAMS ((cpp_reader *)); | |
69 | static void expand_arg PARAMS ((cpp_reader *, macro_arg *)); | |
70 | static unsigned char *quote_string PARAMS ((unsigned char *, | |
71 | const unsigned char *, | |
72 | unsigned int)); | |
73 | static void make_string_token PARAMS ((cpp_pool *, cpp_token *, | |
74 | const U_CHAR *, unsigned int)); | |
75 | static void make_number_token PARAMS ((cpp_reader *, cpp_token *, int)); | |
76 | static void stringify_arg PARAMS ((cpp_reader *, macro_arg *)); | |
77 | static void paste_all_tokens PARAMS ((cpp_reader *, cpp_token *)); | |
d63eefbf | 78 | static int paste_tokens PARAMS ((cpp_reader *, cpp_token *, cpp_token *)); |
93c80368 NB |
79 | static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *, |
80 | struct toklist *)); | |
81 | static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *, | |
82 | struct toklist *)); | |
83 | ||
84 | /* Lookaheads. */ | |
85 | ||
86 | static void save_lookahead_token PARAMS ((cpp_reader *, const cpp_token *)); | |
87 | static void take_lookahead_token PARAMS ((cpp_reader *, cpp_token *)); | |
93c80368 NB |
88 | static cpp_lookahead *alloc_lookahead PARAMS ((cpp_reader *)); |
89 | static void free_lookahead PARAMS ((cpp_lookahead *)); | |
90 | ||
91 | /* #define directive parsing and handling. */ | |
92 | ||
93 | static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *)); | |
94 | static int check_macro_redefinition PARAMS ((cpp_reader *, | |
95 | const cpp_hashnode *, | |
96 | const cpp_macro *)); | |
97 | static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *)); | |
98 | static int parse_params PARAMS ((cpp_reader *, cpp_macro *)); | |
e1aa5140 | 99 | static void check_trad_stringification PARAMS ((cpp_reader *, |
93c80368 | 100 | const cpp_macro *, |
e1aa5140 | 101 | const cpp_string *)); |
711b8824 | 102 | |
93c80368 NB |
103 | /* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a |
104 | CPP_STRING token containing TEXT in quoted form. */ | |
105 | static void | |
106 | make_string_token (pool, token, text, len) | |
107 | cpp_pool *pool; | |
108 | cpp_token *token; | |
109 | const U_CHAR *text; | |
110 | unsigned int len; | |
111 | { | |
112 | U_CHAR *buf = _cpp_pool_alloc (pool, len * 4); | |
113 | ||
114 | token->type = CPP_STRING; | |
115 | token->val.str.text = buf; | |
116 | token->val.str.len = quote_string (buf, text, len) - buf; | |
117 | token->flags = 0; | |
118 | } | |
119 | ||
120 | /* Allocates and converts a temporary token to a CPP_NUMBER token, | |
121 | evaluating to NUMBER. */ | |
122 | static void | |
123 | make_number_token (pfile, token, number) | |
124 | cpp_reader *pfile; | |
125 | cpp_token *token; | |
126 | int number; | |
127 | { | |
49fe13f6 | 128 | unsigned char *buf = _cpp_pool_alloc (&pfile->ident_pool, 20); |
93c80368 NB |
129 | |
130 | sprintf ((char *) buf, "%d", number); | |
131 | token->type = CPP_NUMBER; | |
132 | token->val.str.text = buf; | |
133 | token->val.str.len = ustrlen (buf); | |
134 | token->flags = 0; | |
135 | } | |
136 | ||
137 | static const char * const monthnames[] = | |
138 | { | |
139 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
140 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
141 | }; | |
142 | ||
143 | /* Handle builtin macros like __FILE__. */ | |
144 | static void | |
145 | builtin_macro (pfile, token) | |
146 | cpp_reader *pfile; | |
147 | cpp_token *token; | |
148 | { | |
bd969772 | 149 | unsigned char flags = ((token->flags & PREV_WHITE) | AVOID_LPASTE); |
93c80368 | 150 | cpp_hashnode *node = token->val.node; |
93c80368 NB |
151 | |
152 | switch (node->value.builtin) | |
153 | { | |
154 | case BT_FILE: | |
155 | case BT_BASE_FILE: | |
711b8824 | 156 | { |
0bda4760 NB |
157 | const char *name; |
158 | cpp_buffer *buffer = pfile->buffer; | |
159 | ||
160 | if (node->value.builtin == BT_BASE_FILE) | |
161 | while (buffer->prev) | |
162 | buffer = buffer->prev; | |
163 | ||
164 | name = buffer->nominal_fname; | |
49fe13f6 | 165 | make_string_token (&pfile->ident_pool, token, |
0bda4760 | 166 | (const unsigned char *) name, strlen (name)); |
711b8824 | 167 | } |
93c80368 NB |
168 | break; |
169 | ||
170 | case BT_INCLUDE_LEVEL: | |
171 | /* pfile->include_depth counts the primary source as level 1, | |
172 | but historically __INCLUDE_DEPTH__ has called the primary | |
173 | source level 0. */ | |
174 | make_number_token (pfile, token, pfile->include_depth - 1); | |
175 | break; | |
176 | ||
177 | case BT_SPECLINE: | |
178 | /* If __LINE__ is embedded in a macro, it must expand to the | |
179 | line of the macro's invocation, not its definition. | |
180 | Otherwise things like assert() will not work properly. */ | |
181 | make_number_token (pfile, token, cpp_get_line (pfile)->line); | |
182 | break; | |
183 | ||
184 | case BT_STDC: | |
185 | { | |
186 | int stdc = 1; | |
187 | ||
510fbf86 | 188 | if (STDC_0_IN_SYSTEM_HEADERS && CPP_IN_SYSTEM_HEADER (pfile) |
8a47978d | 189 | && pfile->spec_nodes.n__STRICT_ANSI__->type == NT_VOID) |
93c80368 | 190 | stdc = 0; |
93c80368 NB |
191 | make_number_token (pfile, token, stdc); |
192 | } | |
193 | break; | |
711b8824 | 194 | |
93c80368 NB |
195 | case BT_DATE: |
196 | case BT_TIME: | |
197 | if (pfile->date.type == CPP_EOF) | |
198 | { | |
199 | /* Allocate __DATE__ and __TIME__ from permanent storage, | |
200 | and save them in pfile so we don't have to do this again. | |
201 | We don't generate these strings at init time because | |
202 | time() and localtime() are very slow on some systems. */ | |
203 | time_t tt = time (NULL); | |
204 | struct tm *tb = localtime (&tt); | |
205 | ||
206 | make_string_token (&pfile->ident_pool, &pfile->date, | |
207 | DSC("Oct 11 1347")); | |
208 | make_string_token (&pfile->ident_pool, &pfile->time, | |
209 | DSC("12:34:56")); | |
210 | ||
211 | sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d", | |
212 | monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900); | |
213 | sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d", | |
214 | tb->tm_hour, tb->tm_min, tb->tm_sec); | |
215 | } | |
216 | *token = node->value.builtin == BT_DATE ? pfile->date: pfile->time; | |
217 | break; | |
218 | ||
1ca894a0 MM |
219 | case BT_WEAK: |
220 | make_number_token (pfile, token, SUPPORTS_ONE_ONLY); | |
221 | break; | |
222 | ||
93c80368 NB |
223 | default: |
224 | cpp_ice (pfile, "invalid builtin macro \"%s\"", node->name); | |
225 | break; | |
226 | } | |
bd969772 NB |
227 | |
228 | token->flags = flags; | |
711b8824 ZW |
229 | } |
230 | ||
93c80368 NB |
231 | /* Used by cpperror.c to obtain the correct line and column to report |
232 | in a diagnostic. */ | |
233 | const cpp_lexer_pos * | |
234 | cpp_get_line (pfile) | |
711b8824 | 235 | cpp_reader *pfile; |
711b8824 | 236 | { |
93c80368 NB |
237 | return &pfile->lexer_pos; |
238 | } | |
711b8824 | 239 | |
93c80368 NB |
240 | static void |
241 | lock_pools (pfile) | |
242 | cpp_reader *pfile; | |
243 | { | |
93c80368 | 244 | _cpp_lock_pool (&pfile->argument_pool); |
711b8824 ZW |
245 | } |
246 | ||
93c80368 NB |
247 | static void |
248 | unlock_pools (pfile) | |
249 | cpp_reader *pfile; | |
250 | { | |
93c80368 NB |
251 | _cpp_unlock_pool (&pfile->argument_pool); |
252 | } | |
711b8824 | 253 | |
93c80368 NB |
254 | /* Adds backslashes before all backslashes and double quotes appearing |
255 | in strings. Non-printable characters are converted to octal. */ | |
256 | static U_CHAR * | |
257 | quote_string (dest, src, len) | |
258 | U_CHAR *dest; | |
259 | const U_CHAR *src; | |
260 | unsigned int len; | |
261 | { | |
262 | while (len--) | |
263 | { | |
264 | U_CHAR c = *src++; | |
711b8824 | 265 | |
93c80368 NB |
266 | if (c == '\\' || c == '"') |
267 | { | |
268 | *dest++ = '\\'; | |
269 | *dest++ = c; | |
270 | } | |
271 | else | |
272 | { | |
273 | if (ISPRINT (c)) | |
274 | *dest++ = c; | |
275 | else | |
711b8824 | 276 | { |
93c80368 NB |
277 | sprintf ((char *) dest, "\\%03o", c); |
278 | dest += 4; | |
711b8824 | 279 | } |
93c80368 NB |
280 | } |
281 | } | |
711b8824 | 282 | |
93c80368 NB |
283 | return dest; |
284 | } | |
711b8824 | 285 | |
93c80368 NB |
286 | /* Convert a token sequence to a single string token according to the |
287 | rules of the ISO C #-operator. */ | |
288 | static void | |
289 | stringify_arg (pfile, arg) | |
290 | cpp_reader *pfile; | |
291 | macro_arg *arg; | |
292 | { | |
49fe13f6 | 293 | cpp_pool *pool = &pfile->ident_pool; |
93c80368 NB |
294 | unsigned char *start = POOL_FRONT (pool); |
295 | unsigned int i, escape_it, total_len = 0, backslash_count = 0; | |
711b8824 | 296 | |
93c80368 NB |
297 | /* Loop, reading in the argument's tokens. */ |
298 | for (i = 0; i < arg->count; i++) | |
299 | { | |
300 | unsigned char *dest; | |
301 | const cpp_token *token = &arg->first[i]; | |
302 | unsigned int len = cpp_token_len (token); | |
711b8824 | 303 | |
93c80368 NB |
304 | escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING |
305 | || token->type == CPP_CHAR || token->type == CPP_WCHAR | |
306 | || token->type == CPP_OSTRING); | |
711b8824 | 307 | |
93c80368 NB |
308 | if (escape_it) |
309 | /* Worst case is each char is octal. */ | |
310 | len *= 4; | |
311 | len++; /* Room for initial space. */ | |
711b8824 | 312 | |
93c80368 NB |
313 | dest = &start[total_len]; |
314 | if (dest + len > POOL_LIMIT (pool)) | |
315 | { | |
316 | _cpp_next_chunk (pool, len, (unsigned char **) &start); | |
317 | dest = &start[total_len]; | |
318 | } | |
711b8824 | 319 | |
93c80368 | 320 | /* No leading white space. */ |
4c2b647d NB |
321 | if (token->flags & PREV_WHITE && total_len > 0) |
322 | *dest++ = ' '; | |
711b8824 | 323 | |
93c80368 NB |
324 | if (escape_it) |
325 | { | |
326 | unsigned char *buf = (unsigned char *) xmalloc (len); | |
327 | ||
328 | len = cpp_spell_token (pfile, token, buf) - buf; | |
329 | dest = quote_string (dest, buf, len); | |
330 | free (buf); | |
331 | } | |
332 | else | |
333 | dest = cpp_spell_token (pfile, token, dest); | |
334 | total_len = dest - start; | |
335 | ||
6c53ebff | 336 | if (token->type == CPP_OTHER && token->val.c == '\\') |
93c80368 NB |
337 | backslash_count++; |
338 | else | |
339 | backslash_count = 0; | |
340 | } | |
341 | ||
342 | /* Ignore the final \ of invalid string literals. */ | |
343 | if (backslash_count & 1) | |
344 | { | |
345 | cpp_warning (pfile, "invalid string literal, ignoring final '\\'"); | |
346 | total_len--; | |
347 | } | |
348 | ||
349 | POOL_COMMIT (pool, total_len); | |
350 | ||
351 | arg->stringified = xnew (cpp_token); | |
352 | arg->stringified->flags = 0; | |
353 | arg->stringified->type = CPP_STRING; | |
354 | arg->stringified->val.str.text = start; | |
355 | arg->stringified->val.str.len = total_len; | |
356 | } | |
357 | ||
d63eefbf NB |
358 | /* Try to paste two tokens. On success, the LHS becomes the pasted |
359 | token, and 0 is returned. For failure, we update the flags of the | |
360 | RHS appropriately and return non-zero. */ | |
361 | static int | |
362 | paste_tokens (pfile, lhs, rhs) | |
363 | cpp_reader *pfile; | |
364 | cpp_token *lhs, *rhs; | |
365 | { | |
366 | unsigned char flags; | |
367 | int digraph = 0; | |
368 | enum cpp_ttype type; | |
369 | ||
370 | type = cpp_can_paste (pfile, lhs, rhs, &digraph); | |
371 | ||
372 | if (type == CPP_EOF) | |
373 | { | |
6a6b1628 NB |
374 | /* Mandatory warning for all apart from assembler. */ |
375 | if (CPP_OPTION (pfile, lang) != CLK_ASM) | |
d63eefbf NB |
376 | cpp_warning (pfile, |
377 | "pasting \"%s\" and \"%s\" does not give a valid preprocessing token", | |
378 | cpp_token_as_text (pfile, lhs), | |
379 | cpp_token_as_text (pfile, rhs)); | |
380 | ||
381 | /* The standard states that behaviour is undefined. By the | |
6a6b1628 NB |
382 | principle of least surpise, we step back before the RHS, and |
383 | mark it to prevent macro expansion. Tests in the testsuite | |
384 | rely on clearing PREV_WHITE here, though you could argue we | |
385 | should actually set it. Assembler can have '.' in labels and | |
386 | so requires that we don't insert spaces there. Maybe we should | |
387 | change this to put out a space unless it's assembler. */ | |
d63eefbf NB |
388 | rhs->flags &= ~PREV_WHITE; |
389 | rhs->flags |= NO_EXPAND; | |
390 | return 1; | |
391 | } | |
392 | ||
393 | flags = lhs->flags & ~DIGRAPH; | |
394 | if (digraph) | |
395 | flags |= DIGRAPH; | |
396 | ||
397 | /* Identifiers and numbers need spellings to be pasted. */ | |
398 | if (type == CPP_NAME || type == CPP_NUMBER) | |
399 | { | |
400 | unsigned int total_len = cpp_token_len (lhs) + cpp_token_len (rhs); | |
401 | unsigned char *result, *end; | |
d63eefbf | 402 | |
49fe13f6 | 403 | result = _cpp_pool_alloc (&pfile->ident_pool, total_len + 1); |
d63eefbf NB |
404 | |
405 | /* Paste the spellings and null terminate. */ | |
406 | end = cpp_spell_token (pfile, rhs, cpp_spell_token (pfile, lhs, result)); | |
407 | *end = '\0'; | |
408 | total_len = end - result; | |
409 | ||
410 | if (type == CPP_NAME) | |
411 | { | |
412 | lhs->val.node = cpp_lookup (pfile, result, total_len); | |
413 | if (lhs->val.node->flags & NODE_OPERATOR) | |
414 | { | |
415 | flags |= NAMED_OP; | |
416 | lhs->type = lhs->val.node->value.operator; | |
417 | } | |
418 | } | |
419 | else | |
420 | { | |
421 | lhs->val.str.text = result; | |
422 | lhs->val.str.len = total_len; | |
423 | } | |
424 | } | |
425 | else if (type == CPP_WCHAR || type == CPP_WSTRING) | |
426 | lhs->val.str = rhs->val.str; | |
427 | ||
428 | /* Set type and flags after pasting spellings. */ | |
429 | lhs->type = type; | |
430 | lhs->flags = flags; | |
431 | ||
432 | return 0; | |
433 | } | |
434 | ||
93c80368 NB |
435 | /* Handles an arbitrarily long sequence of ## operators. This |
436 | implementation is left-associative, non-recursive, and finishes a | |
d63eefbf NB |
437 | paste before handling succeeding ones. If the paste fails, we back |
438 | up a token to just after the ## operator, with the effect that it | |
439 | appears in the output stream normally. */ | |
93c80368 NB |
440 | static void |
441 | paste_all_tokens (pfile, lhs) | |
442 | cpp_reader *pfile; | |
443 | cpp_token *lhs; | |
444 | { | |
93c80368 | 445 | cpp_token *rhs; |
d63eefbf | 446 | unsigned char orig_flags = lhs->flags; |
93c80368 NB |
447 | |
448 | do | |
449 | { | |
450 | /* Take the token directly from the current context. We can do | |
451 | this, because we are in the replacement list of either an | |
452 | object-like macro, or a function-like macro with arguments | |
453 | inserted. In either case, the constraints to #define | |
d63eefbf | 454 | guarantee we have at least one more token. */ |
93c80368 | 455 | rhs = pfile->context->list.first++; |
d63eefbf | 456 | if (paste_tokens (pfile, lhs, rhs)) |
93c80368 | 457 | { |
d63eefbf | 458 | /* We failed. Step back so we read the RHS in next. */ |
4c2b647d NB |
459 | pfile->context->list.first--; |
460 | break; | |
93c80368 | 461 | } |
93c80368 NB |
462 | } |
463 | while (rhs->flags & PASTE_LEFT); | |
464 | ||
465 | /* The pasted token has the PREV_WHITE flag of the LHS, is no longer | |
466 | PASTE_LEFT, and is subject to macro expansion. */ | |
467 | lhs->flags &= ~(PREV_WHITE | PASTE_LEFT | NO_EXPAND); | |
bd969772 | 468 | lhs->flags |= orig_flags & (PREV_WHITE | AVOID_LPASTE); |
93c80368 NB |
469 | } |
470 | ||
4c2b647d | 471 | /* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS |
28e0f040 NB |
472 | is non-zero if this is a variadic macro. Returns the type of the |
473 | token that caused reading to finish. */ | |
93c80368 | 474 | static enum cpp_ttype |
28e0f040 | 475 | parse_arg (pfile, arg, variadic) |
93c80368 NB |
476 | cpp_reader *pfile; |
477 | struct macro_arg *arg; | |
28e0f040 | 478 | int variadic; |
93c80368 NB |
479 | { |
480 | enum cpp_ttype result; | |
481 | unsigned int paren = 0; | |
8d9e9a08 | 482 | unsigned int line; |
93c80368 NB |
483 | |
484 | arg->first = (cpp_token *) POOL_FRONT (&pfile->argument_pool); | |
485 | for (;; arg->count++) | |
486 | { | |
487 | cpp_token *token = &arg->first[arg->count]; | |
488 | if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->argument_pool)) | |
489 | { | |
490 | _cpp_next_chunk (&pfile->argument_pool, sizeof (cpp_token), | |
491 | (unsigned char **) &arg->first); | |
492 | token = &arg->first[arg->count]; | |
493 | } | |
494 | ||
8d9e9a08 NB |
495 | /* Newlines in arguments are white space (6.10.3.10). */ |
496 | line = pfile->lexer_pos.output_line; | |
7f2f1a66 | 497 | cpp_get_token (pfile, token); |
8d9e9a08 NB |
498 | if (line != pfile->lexer_pos.output_line) |
499 | token->flags |= PREV_WHITE; | |
93c80368 | 500 | |
8d9e9a08 | 501 | result = token->type; |
93c80368 NB |
502 | if (result == CPP_OPEN_PAREN) |
503 | paren++; | |
504 | else if (result == CPP_CLOSE_PAREN && paren-- == 0) | |
505 | break; | |
28e0f040 NB |
506 | /* Commas are not terminators within parantheses or variadic. */ |
507 | else if (result == CPP_COMMA && paren == 0 && !variadic) | |
93c80368 NB |
508 | break; |
509 | else if (result == CPP_EOF) | |
510 | break; /* Error reported by caller. */ | |
93c80368 | 511 | } |
711b8824 | 512 | |
93c80368 NB |
513 | /* Commit the memory used to store the arguments. */ |
514 | POOL_COMMIT (&pfile->argument_pool, arg->count * sizeof (cpp_token)); | |
515 | ||
516 | return result; | |
711b8824 ZW |
517 | } |
518 | ||
93c80368 NB |
519 | /* Parse the arguments making up a macro invocation. */ |
520 | static macro_arg * | |
521 | parse_args (pfile, node) | |
522 | cpp_reader *pfile; | |
523 | const cpp_hashnode *node; | |
524 | { | |
525 | cpp_macro *macro = node->value.macro; | |
526 | macro_arg *args, *cur; | |
527 | enum cpp_ttype type; | |
528 | int argc, error = 0; | |
529 | ||
530 | /* Allocate room for at least one argument, and zero it out. */ | |
531 | argc = macro->paramc ? macro->paramc: 1; | |
532 | args = xcnewvec (macro_arg, argc); | |
533 | ||
534 | for (cur = args, argc = 0; ;) | |
535 | { | |
536 | argc++; | |
537 | ||
28e0f040 | 538 | type = parse_arg (pfile, cur, argc == macro->paramc && macro->variadic); |
93c80368 NB |
539 | if (type == CPP_CLOSE_PAREN || type == CPP_EOF) |
540 | break; | |
541 | ||
542 | /* Re-use the last argument for excess arguments. */ | |
543 | if (argc < macro->paramc) | |
544 | cur++; | |
545 | } | |
546 | ||
547 | if (type == CPP_EOF) | |
548 | { | |
549 | cpp_error (pfile, "unterminated argument list invoking macro \"%s\"", | |
550 | node->name); | |
551 | error = 1; | |
552 | } | |
553 | else if (argc < macro->paramc) | |
554 | { | |
555 | /* As an extension, a rest argument is allowed to not appear in | |
556 | the invocation at all. | |
557 | e.g. #define debug(format, args...) something | |
558 | debug("string"); | |
559 | ||
560 | This is exactly the same as if there had been an empty rest | |
561 | argument - debug("string", ). */ | |
562 | ||
28e0f040 | 563 | if (argc + 1 == macro->paramc && macro->variadic) |
93c80368 | 564 | { |
bdb05a7b | 565 | if (CPP_PEDANTIC (pfile)) |
93c80368 NB |
566 | cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used"); |
567 | } | |
568 | else | |
569 | { | |
570 | cpp_error (pfile, | |
571 | "macro \"%s\" requires %u arguments, but only %u given", | |
572 | node->name, macro->paramc, argc); | |
573 | error = 1; | |
574 | } | |
575 | } | |
576 | else if (argc > macro->paramc) | |
577 | { | |
4c2b647d NB |
578 | /* Empty argument to a macro taking no arguments is OK. */ |
579 | if (argc != 1 || cur->count) | |
93c80368 NB |
580 | { |
581 | cpp_error (pfile, | |
582 | "macro \"%s\" passed %u arguments, but takes just %u", | |
583 | node->name, argc, macro->paramc); | |
584 | error = 1; | |
585 | } | |
586 | } | |
587 | ||
588 | if (error) | |
589 | { | |
590 | free (args); | |
591 | args = 0; | |
592 | } | |
593 | ||
594 | return args; | |
595 | } | |
596 | ||
597 | static int | |
598 | funlike_invocation_p (pfile, node, list) | |
599 | cpp_reader *pfile; | |
600 | const cpp_hashnode *node; | |
601 | struct toklist *list; | |
602 | { | |
673b13e2 | 603 | cpp_context *orig; |
93c80368 NB |
604 | cpp_token maybe_paren; |
605 | macro_arg *args = 0; | |
8d9e9a08 | 606 | cpp_lexer_pos macro_pos; |
93c80368 | 607 | |
8d9e9a08 | 608 | macro_pos = pfile->lexer_pos; |
93c80368 NB |
609 | pfile->state.parsing_args = 1; |
610 | pfile->state.prevent_expansion++; | |
8aaef6e0 | 611 | orig = pfile->context; |
93c80368 NB |
612 | |
613 | cpp_start_lookahead (pfile); | |
614 | cpp_get_token (pfile, &maybe_paren); | |
615 | cpp_stop_lookahead (pfile, maybe_paren.type == CPP_OPEN_PAREN); | |
616 | ||
617 | if (maybe_paren.type == CPP_OPEN_PAREN) | |
618 | args = parse_args (pfile, node); | |
619 | else if (CPP_WTRADITIONAL (pfile)) | |
620 | cpp_warning (pfile, | |
621 | "function-like macro \"%s\" must be used with arguments in traditional C", | |
622 | node->name); | |
623 | ||
624 | /* Restore original context. */ | |
8aaef6e0 | 625 | pfile->context = orig; |
93c80368 NB |
626 | pfile->state.prevent_expansion--; |
627 | pfile->state.parsing_args = 0; | |
628 | ||
629 | if (args) | |
630 | { | |
8d9e9a08 NB |
631 | /* The macro's expansion appears where the name would have. */ |
632 | pfile->lexer_pos = macro_pos; | |
633 | ||
93c80368 | 634 | if (node->value.macro->paramc > 0) |
7f2f1a66 NB |
635 | { |
636 | /* Don't save tokens during pre-expansion. */ | |
637 | struct cpp_lookahead *la_saved = pfile->la_write; | |
638 | pfile->la_write = 0; | |
639 | replace_args (pfile, node->value.macro, args, list); | |
640 | pfile->la_write = la_saved; | |
641 | } | |
93c80368 NB |
642 | free (args); |
643 | } | |
644 | ||
645 | return args != 0; | |
646 | } | |
647 | ||
648 | /* Push the context of a macro onto the context stack. TOKEN is the | |
649 | macro name. If we can successfully start expanding the macro, | |
a5c3cccd NB |
650 | TOKEN is replaced with the first token of the expansion, and we |
651 | return non-zero. */ | |
711b8824 | 652 | static int |
29b10746 | 653 | enter_macro_context (pfile, node) |
711b8824 | 654 | cpp_reader *pfile; |
29b10746 | 655 | cpp_hashnode *node; |
711b8824 | 656 | { |
93c80368 | 657 | cpp_context *context; |
29b10746 | 658 | cpp_macro *macro = node->value.macro; |
93c80368 | 659 | struct toklist list; |
711b8824 | 660 | |
93c80368 NB |
661 | /* Save the position of the outermost macro invocation. */ |
662 | if (!pfile->context->prev) | |
8d9e9a08 | 663 | lock_pools (pfile); |
711b8824 | 664 | |
29b10746 | 665 | if (macro->fun_like && !funlike_invocation_p (pfile, node, &list)) |
93c80368 NB |
666 | { |
667 | if (!pfile->context->prev) | |
668 | unlock_pools (pfile); | |
a5c3cccd | 669 | return 0; |
93c80368 | 670 | } |
711b8824 | 671 | |
93c80368 | 672 | if (macro->paramc == 0) |
711b8824 | 673 | { |
4c2b647d NB |
674 | list.first = macro->expansion; |
675 | list.limit = macro->expansion + macro->count; | |
711b8824 | 676 | } |
4c2b647d | 677 | |
26ec42ee | 678 | /* Only push a macro context for non-empty replacement lists. */ |
29b10746 NB |
679 | if (list.first != list.limit) |
680 | { | |
29b10746 NB |
681 | context = next_context (pfile); |
682 | context->list = list; | |
683 | context->macro = macro; | |
26ec42ee | 684 | |
29b10746 NB |
685 | /* Disable the macro within its expansion. */ |
686 | macro->disabled = 1; | |
687 | } | |
93c80368 | 688 | |
a5c3cccd | 689 | return 1; |
93c80368 NB |
690 | } |
691 | ||
692 | /* Move to the next context. Create one if there is none. */ | |
693 | static cpp_context * | |
694 | next_context (pfile) | |
695 | cpp_reader *pfile; | |
696 | { | |
697 | cpp_context *prev = pfile->context; | |
698 | cpp_context *result = prev->next; | |
699 | ||
700 | if (result == 0) | |
711b8824 | 701 | { |
93c80368 NB |
702 | result = xnew (cpp_context); |
703 | prev->next = result; | |
704 | result->prev = prev; | |
705 | result->next = 0; | |
711b8824 ZW |
706 | } |
707 | ||
93c80368 NB |
708 | pfile->context = result; |
709 | return result; | |
710 | } | |
711 | ||
712 | static void | |
713 | replace_args (pfile, macro, args, list) | |
714 | cpp_reader *pfile; | |
715 | cpp_macro *macro; | |
716 | macro_arg *args; | |
717 | struct toklist *list; | |
718 | { | |
26ec42ee | 719 | unsigned char flags = 0; |
93c80368 NB |
720 | unsigned int i, total; |
721 | const cpp_token *src, *limit; | |
722 | cpp_token *dest; | |
723 | macro_arg *arg; | |
724 | ||
725 | src = macro->expansion; | |
726 | limit = src + macro->count; | |
727 | ||
728 | /* First, fully macro-expand arguments, calculating the number of | |
729 | tokens in the final expansion as we go. This ensures that the | |
730 | possible recursive use of argument_pool is fine. */ | |
731 | total = limit - src; | |
732 | for (; src < limit; src++) | |
733 | if (src->type == CPP_MACRO_ARG) | |
734 | { | |
735 | /* We have an argument. If it is not being stringified or | |
736 | pasted it is macro-replaced before insertion. */ | |
6c53ebff | 737 | arg = &args[src->val.arg_no - 1]; |
4c2b647d | 738 | |
93c80368 NB |
739 | if (src->flags & STRINGIFY_ARG) |
740 | { | |
741 | if (!arg->stringified) | |
742 | stringify_arg (pfile, arg); | |
743 | } | |
744 | else if ((src->flags & PASTE_LEFT) | |
745 | || (src > macro->expansion && (src[-1].flags & PASTE_LEFT))) | |
746 | total += arg->count - 1; | |
747 | else | |
748 | { | |
749 | if (!arg->expanded) | |
4c2b647d NB |
750 | { |
751 | arg->expanded_count = 0; | |
752 | if (arg->count) | |
753 | expand_arg (pfile, arg); | |
754 | } | |
93c80368 NB |
755 | total += arg->expanded_count - 1; |
756 | } | |
757 | } | |
758 | ||
759 | dest = (cpp_token *) _cpp_pool_alloc (&pfile->argument_pool, | |
760 | total * sizeof (cpp_token)); | |
761 | list->first = dest; | |
93c80368 NB |
762 | |
763 | for (src = macro->expansion; src < limit; src++) | |
764 | if (src->type == CPP_MACRO_ARG) | |
765 | { | |
766 | unsigned int count; | |
767 | const cpp_token *from; | |
768 | ||
6c53ebff | 769 | arg = &args[src->val.arg_no - 1]; |
93c80368 NB |
770 | if (src->flags & STRINGIFY_ARG) |
771 | from = arg->stringified, count = 1; | |
4c2b647d | 772 | else if (src->flags & PASTE_LEFT) |
93c80368 | 773 | count = arg->count, from = arg->first; |
4c2b647d NB |
774 | else if (src > macro->expansion && (src[-1].flags & PASTE_LEFT)) |
775 | { | |
776 | count = arg->count, from = arg->first; | |
777 | if (dest != list->first) | |
778 | { | |
779 | /* GCC has special semantics for , ## b where b is a | |
780 | varargs parameter: the comma disappears if b was | |
781 | given no actual arguments (not merely if b is an | |
782 | empty argument); otherwise pasting is turned off. */ | |
783 | if (dest[-1].type == CPP_COMMA | |
28e0f040 | 784 | && macro->variadic |
4c2b647d NB |
785 | && src->val.arg_no == macro->paramc) |
786 | { | |
787 | if (count == 0) | |
788 | dest--; | |
789 | else | |
790 | dest[-1].flags &= ~PASTE_LEFT; | |
791 | } | |
792 | /* Count == 0 is the RHS a placemarker case. */ | |
793 | else if (count == 0) | |
794 | dest[-1].flags &= ~PASTE_LEFT; | |
795 | } | |
796 | } | |
93c80368 NB |
797 | else |
798 | count = arg->expanded_count, from = arg->expanded; | |
93c80368 | 799 | |
4c2b647d NB |
800 | /* Count == 0 is the LHS a placemarker case. */ |
801 | if (count) | |
802 | { | |
803 | memcpy (dest, from, count * sizeof (cpp_token)); | |
804 | ||
805 | /* The first token gets PREV_WHITE of the CPP_MACRO_ARG. */ | |
806 | dest->flags &= ~PREV_WHITE; | |
807 | dest->flags |= src->flags & PREV_WHITE; | |
bd969772 | 808 | dest->flags |= AVOID_LPASTE; |
93c80368 | 809 | |
4c2b647d NB |
810 | /* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */ |
811 | dest[count - 1].flags |= src->flags & PASTE_LEFT; | |
93c80368 | 812 | |
4c2b647d NB |
813 | dest += count; |
814 | } | |
26ec42ee NB |
815 | |
816 | /* The token after the argument must avoid an accidental paste. */ | |
817 | flags = AVOID_LPASTE; | |
93c80368 NB |
818 | } |
819 | else | |
26ec42ee NB |
820 | { |
821 | *dest = *src; | |
822 | dest->flags |= flags; | |
823 | dest++; | |
824 | flags = 0; | |
825 | } | |
93c80368 | 826 | |
4c2b647d NB |
827 | list->limit = dest; |
828 | ||
93c80368 NB |
829 | /* Free the expanded arguments. */ |
830 | for (i = 0; i < macro->paramc; i++) | |
711b8824 | 831 | { |
93c80368 NB |
832 | if (args[i].expanded) |
833 | free (args[i].expanded); | |
834 | if (args[i].stringified) | |
835 | free (args[i].stringified); | |
836 | } | |
837 | } | |
838 | ||
839 | /* Subroutine of expand_arg to put the unexpanded tokens on the | |
840 | context stack. */ | |
841 | static cpp_context * | |
842 | push_arg_context (pfile, arg) | |
843 | cpp_reader *pfile; | |
844 | macro_arg *arg; | |
845 | { | |
846 | cpp_context *context = next_context (pfile); | |
847 | context->macro = 0; | |
848 | context->list.first = arg->first; | |
849 | context->list.limit = arg->first + arg->count; | |
850 | ||
851 | return context; | |
852 | } | |
853 | ||
854 | static void | |
855 | expand_arg (pfile, arg) | |
856 | cpp_reader *pfile; | |
857 | macro_arg *arg; | |
858 | { | |
859 | cpp_token *token; | |
860 | unsigned int capacity = 256; | |
861 | ||
862 | /* Loop, reading in the arguments. */ | |
863 | arg->expanded = (cpp_token *) xmalloc (capacity * sizeof (cpp_token)); | |
93c80368 NB |
864 | |
865 | push_arg_context (pfile, arg); | |
866 | do | |
867 | { | |
868 | if (arg->expanded_count >= capacity) | |
711b8824 | 869 | { |
93c80368 NB |
870 | capacity *= 2; |
871 | arg->expanded = (cpp_token *) | |
872 | xrealloc (arg->expanded, capacity * sizeof (cpp_token)); | |
873 | } | |
874 | token = &arg->expanded[arg->expanded_count++]; | |
7f2f1a66 | 875 | cpp_get_token (pfile, token); |
93c80368 NB |
876 | } |
877 | while (token->type != CPP_EOF); | |
878 | ||
879 | arg->expanded_count--; | |
880 | ||
881 | /* Pop the context we pushed. */ | |
882 | pfile->context = pfile->context->prev; | |
883 | } | |
884 | ||
885 | void | |
886 | _cpp_pop_context (pfile) | |
887 | cpp_reader *pfile; | |
888 | { | |
889 | cpp_context *context = pfile->context; | |
890 | ||
891 | pfile->context = context->prev; | |
8aaef6e0 NB |
892 | if (!pfile->context->prev && !pfile->state.parsing_args) |
893 | unlock_pools (pfile); | |
894 | ||
895 | /* Re-enable a macro, temporarily if parsing_args, when leaving its | |
896 | expansion. */ | |
897 | context->macro->disabled = 0; | |
93c80368 NB |
898 | } |
899 | ||
7f2f1a66 NB |
900 | /* Eternal routine to get a token. Also used nearly everywhere |
901 | internally, except for places where we know we can safely call | |
902 | the lexer directly, such as lexing a directive name. | |
903 | ||
904 | Macro expansions and directives are transparently handled, | |
905 | including entering included files. Thus tokens are post-macro | |
906 | expansion, and after any intervening directives. External callers | |
907 | see CPP_EOF only at EOF. Internal callers also see it when meeting | |
908 | a directive inside a macro call, when at the end of a directive and | |
909 | state.in_directive is still 1, and at the end of argument | |
910 | pre-expansion. */ | |
93c80368 | 911 | void |
7f2f1a66 | 912 | cpp_get_token (pfile, token) |
93c80368 NB |
913 | cpp_reader *pfile; |
914 | cpp_token *token; | |
915 | { | |
29b10746 | 916 | for (;;) |
93c80368 NB |
917 | { |
918 | cpp_context *context = pfile->context; | |
919 | ||
920 | if (pfile->la_read) | |
921 | take_lookahead_token (pfile, token); | |
922 | /* Context->prev == 0 <=> base context. */ | |
923 | else if (!context->prev) | |
924 | _cpp_lex_token (pfile, token); | |
925 | else if (context->list.first != context->list.limit) | |
29b10746 NB |
926 | { |
927 | *token = *context->list.first++; | |
bd969772 NB |
928 | token->flags |= pfile->buffer->saved_flags; |
929 | pfile->buffer->saved_flags = 0; | |
7f2f1a66 | 930 | /* PASTE_LEFT tokens can only appear in macro expansions. */ |
8d9e9a08 | 931 | if (token->flags & PASTE_LEFT) |
ec1a23e6 NB |
932 | { |
933 | paste_all_tokens (pfile, token); | |
bd969772 | 934 | pfile->buffer->saved_flags = AVOID_LPASTE; |
ec1a23e6 | 935 | } |
29b10746 | 936 | } |
93c80368 NB |
937 | else |
938 | { | |
939 | if (context->macro) | |
711b8824 | 940 | { |
26ec42ee | 941 | /* Avoid accidental paste at the end of a macro. */ |
bd969772 | 942 | pfile->buffer->saved_flags |= AVOID_LPASTE; |
93c80368 | 943 | _cpp_pop_context (pfile); |
29b10746 | 944 | continue; |
711b8824 | 945 | } |
a949941c | 946 | /* End of argument pre-expansion. */ |
93c80368 NB |
947 | token->type = CPP_EOF; |
948 | token->flags = 0; | |
b528a07e | 949 | return; |
93c80368 | 950 | } |
93c80368 | 951 | |
a5c3cccd | 952 | if (token->type != CPP_NAME) |
93c80368 NB |
953 | break; |
954 | ||
a5c3cccd NB |
955 | /* Handle macros and the _Pragma operator. */ |
956 | if (token->val.node->type == NT_MACRO | |
957 | && !pfile->state.prevent_expansion | |
958 | && !(token->flags & NO_EXPAND)) | |
93c80368 | 959 | { |
29b10746 | 960 | cpp_hashnode *node = token->val.node; |
4c2b647d | 961 | |
a5c3cccd NB |
962 | /* Macros invalidate controlling macros. */ |
963 | pfile->mi_state = MI_FAILED; | |
964 | ||
29b10746 | 965 | if (node->flags & NODE_BUILTIN) |
a5c3cccd NB |
966 | { |
967 | builtin_macro (pfile, token); | |
b7a0a5fa | 968 | pfile->buffer->saved_flags = AVOID_LPASTE; |
a5c3cccd NB |
969 | break; |
970 | } | |
971 | ||
29b10746 NB |
972 | if (node->value.macro->disabled) |
973 | token->flags |= NO_EXPAND; | |
974 | else if (enter_macro_context (pfile, node)) | |
bd969772 NB |
975 | { |
976 | /* Pass AVOID_LPASTE and our PREV_WHITE to next token. */ | |
977 | pfile->buffer->saved_flags = ((token->flags & PREV_WHITE) | |
978 | | AVOID_LPASTE); | |
979 | continue; | |
980 | } | |
93c80368 | 981 | } |
a5c3cccd | 982 | |
fe6c2db9 NB |
983 | /* Don't interpret _Pragma within directives. The standard is |
984 | not clear on this, but to me this makes most sense. */ | |
985 | if (token->val.node != pfile->spec_nodes.n__Pragma | |
986 | || pfile->state.in_directive) | |
93c80368 | 987 | break; |
a5c3cccd | 988 | |
7f2f1a66 NB |
989 | /* Handle it, and loop back for another token. MI is cleared |
990 | since this token came from either the lexer or a macro. */ | |
a5c3cccd | 991 | _cpp_do__Pragma (pfile); |
93c80368 | 992 | } |
93c80368 NB |
993 | |
994 | if (pfile->la_write) | |
995 | save_lookahead_token (pfile, token); | |
996 | } | |
997 | ||
998 | /* Read each token in, until EOF. Directives are transparently | |
999 | processed. */ | |
1000 | void | |
8dc4676d | 1001 | cpp_scan_buffer_nooutput (pfile, all_buffers) |
93c80368 | 1002 | cpp_reader *pfile; |
8dc4676d | 1003 | int all_buffers; |
93c80368 NB |
1004 | { |
1005 | cpp_token token; | |
8dc4676d | 1006 | cpp_buffer *buffer = all_buffers ? 0: pfile->buffer->prev; |
93c80368 NB |
1007 | |
1008 | do | |
1009 | do | |
1010 | cpp_get_token (pfile, &token); | |
1011 | while (token.type != CPP_EOF); | |
7463ef45 | 1012 | while (cpp_pop_buffer (pfile) != buffer); |
93c80368 NB |
1013 | } |
1014 | ||
1015 | /* Lookahead handling. */ | |
1016 | ||
1017 | static void | |
1018 | save_lookahead_token (pfile, token) | |
1019 | cpp_reader *pfile; | |
1020 | const cpp_token *token; | |
1021 | { | |
1022 | if (token->type != CPP_EOF) | |
1023 | { | |
1024 | cpp_lookahead *la = pfile->la_write; | |
1025 | cpp_token_with_pos *twp; | |
1026 | ||
1027 | if (la->count == la->cap) | |
711b8824 | 1028 | { |
93c80368 NB |
1029 | la->cap += la->cap + 8; |
1030 | la->tokens = (cpp_token_with_pos *) | |
1031 | xrealloc (la->tokens, la->cap * sizeof (cpp_token_with_pos)); | |
711b8824 | 1032 | } |
93c80368 NB |
1033 | |
1034 | twp = &la->tokens[la->count++]; | |
1035 | twp->token = *token; | |
1036 | twp->pos = *cpp_get_line (pfile); | |
711b8824 | 1037 | } |
711b8824 ZW |
1038 | } |
1039 | ||
93c80368 NB |
1040 | static void |
1041 | take_lookahead_token (pfile, token) | |
711b8824 | 1042 | cpp_reader *pfile; |
93c80368 | 1043 | cpp_token *token; |
711b8824 | 1044 | { |
93c80368 NB |
1045 | cpp_lookahead *la = pfile->la_read; |
1046 | cpp_token_with_pos *twp = &la->tokens[la->cur]; | |
711b8824 | 1047 | |
93c80368 NB |
1048 | *token = twp->token; |
1049 | pfile->lexer_pos = twp->pos; | |
711b8824 | 1050 | |
93c80368 | 1051 | if (++la->cur == la->count) |
b528a07e | 1052 | _cpp_release_lookahead (pfile); |
93c80368 | 1053 | } |
711b8824 | 1054 | |
93c80368 | 1055 | /* Moves the lookahead at the front of the read list to the free store. */ |
b528a07e NB |
1056 | void |
1057 | _cpp_release_lookahead (pfile) | |
93c80368 NB |
1058 | cpp_reader *pfile; |
1059 | { | |
1060 | cpp_lookahead *la = pfile->la_read; | |
711b8824 | 1061 | |
93c80368 NB |
1062 | pfile->la_read = la->next; |
1063 | la->next = pfile->la_unused; | |
1064 | pfile->la_unused = la; | |
1065 | unlock_pools (pfile); | |
1066 | } | |
711b8824 | 1067 | |
93c80368 NB |
1068 | /* Take a new lookahead from the free store, or allocate one if none. */ |
1069 | static cpp_lookahead * | |
1070 | alloc_lookahead (pfile) | |
1071 | cpp_reader *pfile; | |
1072 | { | |
1073 | cpp_lookahead *la = pfile->la_unused; | |
1074 | ||
1075 | if (la) | |
1076 | pfile->la_unused = la->next; | |
1077 | else | |
1078 | { | |
1079 | la = xnew (cpp_lookahead); | |
1080 | la->tokens = 0; | |
1081 | la->cap = 0; | |
1082 | } | |
1083 | ||
1084 | la->cur = la->count = 0; | |
1085 | return la; | |
711b8824 ZW |
1086 | } |
1087 | ||
93c80368 NB |
1088 | /* Free memory associated with a lookahead list. */ |
1089 | static void | |
1090 | free_lookahead (la) | |
1091 | cpp_lookahead *la; | |
711b8824 | 1092 | { |
93c80368 NB |
1093 | if (la->tokens) |
1094 | free ((PTR) la->tokens); | |
1095 | free ((PTR) la); | |
1096 | } | |
711b8824 | 1097 | |
93c80368 NB |
1098 | /* Free all the lookaheads of a cpp_reader. */ |
1099 | void | |
1100 | _cpp_free_lookaheads (pfile) | |
1101 | cpp_reader *pfile; | |
1102 | { | |
1103 | cpp_lookahead *la, *lan; | |
1104 | ||
1105 | if (pfile->la_read) | |
1106 | free_lookahead (pfile->la_read); | |
1107 | if (pfile->la_write) | |
1108 | free_lookahead (pfile->la_write); | |
1109 | ||
1110 | for (la = pfile->la_unused; la; la = lan) | |
1111 | { | |
1112 | lan = la->next; | |
1113 | free_lookahead (la); | |
1114 | } | |
1115 | } | |
711b8824 | 1116 | |
93c80368 NB |
1117 | /* Allocate a lookahead and move it to the front of the write list. */ |
1118 | void | |
1119 | cpp_start_lookahead (pfile) | |
711b8824 | 1120 | cpp_reader *pfile; |
711b8824 | 1121 | { |
93c80368 | 1122 | cpp_lookahead *la = alloc_lookahead (pfile); |
711b8824 | 1123 | |
93c80368 NB |
1124 | la->next = pfile->la_write; |
1125 | pfile->la_write = la; | |
711b8824 | 1126 | |
93c80368 | 1127 | la->pos = *cpp_get_line (pfile); |
711b8824 | 1128 | |
93c80368 NB |
1129 | /* Don't allow memory pools to be re-used whilst we're reading ahead. */ |
1130 | lock_pools (pfile); | |
1131 | } | |
711b8824 | 1132 | |
93c80368 NB |
1133 | /* Stop reading ahead - either step back, or drop the read ahead. */ |
1134 | void | |
1135 | cpp_stop_lookahead (pfile, drop) | |
1136 | cpp_reader *pfile; | |
1137 | int drop; | |
1138 | { | |
1139 | cpp_lookahead *la = pfile->la_write; | |
711b8824 | 1140 | |
93c80368 NB |
1141 | pfile->la_write = la->next; |
1142 | la->next = pfile->la_read; | |
1143 | pfile->la_read = la; | |
711b8824 | 1144 | |
93c80368 | 1145 | if (drop || la->count == 0) |
b528a07e | 1146 | _cpp_release_lookahead (pfile); |
93c80368 NB |
1147 | else |
1148 | pfile->lexer_pos = la->pos; | |
711b8824 ZW |
1149 | } |
1150 | ||
93c80368 NB |
1151 | /* Push a single token back to the front of the queue. Only to be |
1152 | used by cpplib, and only then when necessary. POS is the position | |
1153 | to report for the preceding token. */ | |
1154 | void | |
1155 | _cpp_push_token (pfile, token, pos) | |
1156 | cpp_reader *pfile; | |
1157 | const cpp_token *token; | |
1158 | const cpp_lexer_pos *pos; | |
1159 | { | |
1160 | cpp_start_lookahead (pfile); | |
1161 | save_lookahead_token (pfile, token); | |
1162 | cpp_stop_lookahead (pfile, 0); | |
1163 | pfile->lexer_pos = *pos; | |
1164 | } | |
1165 | ||
1166 | /* #define directive parsing and handling. */ | |
1167 | ||
1168 | /* Returns non-zero if a macro redefinition is trivial. */ | |
1169 | static int | |
1170 | check_macro_redefinition (pfile, node, macro2) | |
1171 | cpp_reader *pfile; | |
1172 | const cpp_hashnode *node; | |
1173 | const cpp_macro *macro2; | |
1174 | { | |
1175 | const cpp_macro *macro1; | |
1176 | unsigned int i; | |
1177 | ||
1178 | if (node->type != NT_MACRO || node->flags & NODE_BUILTIN) | |
1179 | return ! pfile->done_initializing; | |
1180 | ||
1181 | macro1 = node->value.macro; | |
1182 | ||
1183 | /* The quick failures. */ | |
1184 | if (macro1->count != macro2->count | |
1185 | || macro1->paramc != macro2->paramc | |
1186 | || macro1->fun_like != macro2->fun_like | |
28e0f040 | 1187 | || macro1->variadic != macro2->variadic) |
93c80368 NB |
1188 | return 0; |
1189 | ||
1190 | /* Check each token. */ | |
1191 | for (i = 0; i < macro1->count; i++) | |
1192 | if (! _cpp_equiv_tokens (¯o1->expansion[i], ¯o2->expansion[i])) | |
1193 | return 0; | |
1194 | ||
1195 | /* Check parameter spellings. */ | |
1196 | for (i = 0; i < macro1->paramc; i++) | |
1197 | if (macro1->params[i] != macro2->params[i]) | |
1198 | return 0; | |
1199 | ||
1200 | return 1; | |
1201 | } | |
1202 | ||
1203 | /* Free the definition of hashnode H. */ | |
711b8824 ZW |
1204 | |
1205 | void | |
1206 | _cpp_free_definition (h) | |
1207 | cpp_hashnode *h; | |
1208 | { | |
93c80368 NB |
1209 | /* Macros and assertions no longer have anything to free. */ |
1210 | h->type = NT_VOID; | |
1211 | /* Clear builtin flag in case of redefinition. */ | |
1212 | h->flags &= ~NODE_BUILTIN; | |
1213 | } | |
1214 | ||
1215 | static int | |
1216 | save_parameter (pfile, macro, node) | |
1217 | cpp_reader *pfile; | |
1218 | cpp_macro *macro; | |
1219 | cpp_hashnode *node; | |
1220 | { | |
1221 | cpp_hashnode **dest; | |
1222 | ||
1223 | /* Constraint 6.10.3.6 - duplicate parameter names. */ | |
1224 | if (node->arg_index) | |
709e9e50 | 1225 | { |
93c80368 NB |
1226 | cpp_error (pfile, "duplicate macro parameter \"%s\"", node->name); |
1227 | return 1; | |
1228 | } | |
709e9e50 | 1229 | |
93c80368 NB |
1230 | dest = ¯o->params[macro->paramc]; |
1231 | ||
1232 | /* Check we have room for the parameters. */ | |
1233 | if ((unsigned char *) (dest + 1) >= POOL_LIMIT (&pfile->macro_pool)) | |
1234 | { | |
1235 | _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_hashnode *), | |
1236 | (unsigned char **) ¯o->params); | |
1237 | dest = ¯o->params[macro->paramc]; | |
709e9e50 NB |
1238 | } |
1239 | ||
93c80368 NB |
1240 | *dest = node; |
1241 | node->arg_index = ++macro->paramc; | |
1242 | return 0; | |
711b8824 ZW |
1243 | } |
1244 | ||
93c80368 NB |
1245 | static int |
1246 | parse_params (pfile, macro) | |
711b8824 | 1247 | cpp_reader *pfile; |
93c80368 | 1248 | cpp_macro *macro; |
711b8824 | 1249 | { |
93c80368 NB |
1250 | cpp_token token; |
1251 | unsigned int prev_ident = 0; | |
711b8824 | 1252 | |
93c80368 NB |
1253 | macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool); |
1254 | for (;;) | |
711b8824 | 1255 | { |
93c80368 | 1256 | _cpp_lex_token (pfile, &token); |
711b8824 | 1257 | |
93c80368 | 1258 | switch (token.type) |
711b8824 | 1259 | { |
93c80368 NB |
1260 | default: |
1261 | cpp_error (pfile, "\"%s\" may not appear in macro parameter list", | |
1262 | cpp_token_as_text (pfile, &token)); | |
1263 | return 0; | |
1264 | ||
711b8824 | 1265 | case CPP_NAME: |
93c80368 NB |
1266 | if (prev_ident) |
1267 | { | |
1268 | cpp_error (pfile, "macro parameters must be comma-separated"); | |
1269 | return 0; | |
1270 | } | |
1271 | prev_ident = 1; | |
1272 | ||
1273 | if (save_parameter (pfile, macro, token.val.node)) | |
1274 | return 0; | |
1275 | continue; | |
711b8824 | 1276 | |
93c80368 NB |
1277 | case CPP_CLOSE_PAREN: |
1278 | if (prev_ident || macro->paramc == 0) | |
711b8824 | 1279 | break; |
711b8824 | 1280 | |
93c80368 NB |
1281 | /* Fall through to pick up the error. */ |
1282 | case CPP_COMMA: | |
1283 | if (!prev_ident) | |
1284 | { | |
1285 | cpp_error (pfile, "parameter name missing"); | |
1286 | return 0; | |
1287 | } | |
1288 | prev_ident = 0; | |
711b8824 ZW |
1289 | continue; |
1290 | ||
93c80368 | 1291 | case CPP_ELLIPSIS: |
28e0f040 | 1292 | macro->variadic = 1; |
93c80368 NB |
1293 | if (!prev_ident) |
1294 | { | |
1295 | save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__); | |
1296 | pfile->state.va_args_ok = 1; | |
1297 | if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic)) | |
1298 | cpp_pedwarn (pfile, | |
28e0f040 | 1299 | "anonymous variadic macros were introduced in C99"); |
93c80368 NB |
1300 | } |
1301 | else if (CPP_OPTION (pfile, pedantic)) | |
28e0f040 | 1302 | cpp_pedwarn (pfile, "ISO C does not permit named variadic macros"); |
711b8824 | 1303 | |
93c80368 NB |
1304 | /* We're at the end, and just expect a closing parenthesis. */ |
1305 | _cpp_lex_token (pfile, &token); | |
1306 | if (token.type == CPP_CLOSE_PAREN) | |
1307 | break; | |
1308 | /* Fall through. */ | |
711b8824 | 1309 | |
93c80368 NB |
1310 | case CPP_EOF: |
1311 | cpp_error (pfile, "missing ')' in macro parameter list"); | |
1312 | return 0; | |
711b8824 ZW |
1313 | } |
1314 | ||
93c80368 NB |
1315 | /* Success. Commit the parameter array. */ |
1316 | POOL_COMMIT (&pfile->macro_pool, | |
1317 | macro->paramc * sizeof (cpp_hashnode *)); | |
1318 | return 1; | |
711b8824 | 1319 | } |
93c80368 NB |
1320 | } |
1321 | ||
1322 | /* Lex a token from a macro's replacement list. Translate it to a | |
1323 | CPP_MACRO_ARG if appropriate. */ | |
1324 | static cpp_token * | |
1325 | lex_expansion_token (pfile, macro) | |
1326 | cpp_reader *pfile; | |
1327 | cpp_macro *macro; | |
1328 | { | |
1329 | cpp_token *token = ¯o->expansion[macro->count]; | |
711b8824 | 1330 | |
93c80368 NB |
1331 | /* Check we have room for the token. */ |
1332 | if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool)) | |
711b8824 | 1333 | { |
93c80368 NB |
1334 | _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token), |
1335 | (unsigned char **) ¯o->expansion); | |
1336 | token = ¯o->expansion[macro->count]; | |
711b8824 ZW |
1337 | } |
1338 | ||
93c80368 NB |
1339 | macro->count++; |
1340 | _cpp_lex_token (pfile, token); | |
1341 | ||
1342 | /* Is this an argument? */ | |
1343 | if (token->type == CPP_NAME && token->val.node->arg_index) | |
1344 | { | |
1345 | token->type = CPP_MACRO_ARG; | |
6c53ebff | 1346 | token->val.arg_no = token->val.node->arg_index; |
93c80368 NB |
1347 | } |
1348 | else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 | |
1349 | && (token->type == CPP_STRING || token->type == CPP_CHAR)) | |
1350 | check_trad_stringification (pfile, macro, &token->val.str); | |
1351 | ||
1352 | return token; | |
711b8824 ZW |
1353 | } |
1354 | ||
1355 | /* Parse a macro and save its expansion. Returns non-zero on success. */ | |
1356 | int | |
93c80368 | 1357 | _cpp_create_definition (pfile, node) |
711b8824 | 1358 | cpp_reader *pfile; |
93c80368 | 1359 | cpp_hashnode *node; |
711b8824 | 1360 | { |
93c80368 NB |
1361 | cpp_macro *macro; |
1362 | cpp_token *token; | |
1363 | unsigned int i, ok = 1; | |
1364 | ||
1365 | macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool, | |
1366 | sizeof (cpp_macro)); | |
1367 | macro->file = pfile->buffer->nominal_fname; | |
1368 | macro->line = pfile->directive_pos.line; | |
1369 | macro->params = 0; | |
1370 | macro->paramc = 0; | |
1371 | macro->fun_like = 0; | |
28e0f040 | 1372 | macro->variadic = 0; |
93c80368 NB |
1373 | macro->count = 0; |
1374 | macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool); | |
1375 | ||
1376 | /* Get the first token of the expansion (or the '(' of a | |
1377 | function-like macro). */ | |
1378 | token = lex_expansion_token (pfile, macro); | |
1379 | if (token->type == CPP_OPEN_PAREN && !(token->flags & PREV_WHITE)) | |
1380 | { | |
1381 | if (!(ok = parse_params (pfile, macro))) | |
1382 | goto cleanup; | |
1383 | macro->count = 0; | |
1384 | macro->fun_like = 1; | |
1385 | /* Some of the pool may have been used for the parameter store. */ | |
1386 | macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool); | |
1387 | token = lex_expansion_token (pfile, macro); | |
1388 | } | |
1389 | else if (token->type != CPP_EOF && !(token->flags & PREV_WHITE)) | |
1390 | cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name"); | |
711b8824 | 1391 | |
93c80368 NB |
1392 | /* Setting it here means we don't catch leading comments. */ |
1393 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); | |
711b8824 | 1394 | |
93c80368 NB |
1395 | for (;;) |
1396 | { | |
1397 | /* Check the stringifying # constraint 6.10.3.2.1 of | |
1398 | function-like macros when lexing the subsequent token. */ | |
1399 | if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) | |
1400 | { | |
1401 | if (token->type == CPP_MACRO_ARG) | |
1402 | { | |
1403 | token->flags &= ~PREV_WHITE; | |
1404 | token->flags |= STRINGIFY_ARG; | |
1405 | token->flags |= token[-1].flags & PREV_WHITE; | |
1406 | token[-1] = token[0]; | |
1407 | macro->count--; | |
1408 | } | |
1409 | /* Let assembler get away with murder. */ | |
bdb05a7b | 1410 | else if (CPP_OPTION (pfile, lang) != CLK_ASM) |
93c80368 NB |
1411 | { |
1412 | ok = 0; | |
1413 | cpp_error (pfile, "'#' is not followed by a macro parameter"); | |
1414 | goto cleanup; | |
1415 | } | |
1416 | } | |
1417 | ||
1418 | if (token->type == CPP_EOF) | |
1419 | break; | |
1420 | ||
1421 | /* Paste operator constraint 6.10.3.3.1. */ | |
1422 | if (token->type == CPP_PASTE) | |
1423 | { | |
1424 | /* Token-paste ##, can appear in both object-like and | |
1425 | function-like macros, but not at the ends. */ | |
1426 | if (--macro->count > 0) | |
1427 | token = lex_expansion_token (pfile, macro); | |
1428 | ||
1429 | if (macro->count == 0 || token->type == CPP_EOF) | |
1430 | { | |
1431 | ok = 0; | |
1432 | cpp_error (pfile, | |
1433 | "'##' cannot appear at either end of a macro expansion"); | |
1434 | goto cleanup; | |
1435 | } | |
711b8824 | 1436 | |
93c80368 NB |
1437 | token[-1].flags |= PASTE_LEFT; |
1438 | /* Give it a PREV_WHITE for -dM etc. */ | |
1439 | token->flags |= PREV_WHITE; | |
1440 | } | |
1441 | ||
1442 | token = lex_expansion_token (pfile, macro); | |
1443 | } | |
1444 | ||
4c2b647d NB |
1445 | /* Don't count the CPP_EOF. */ |
1446 | macro->count--; | |
93c80368 NB |
1447 | |
1448 | /* Clear the whitespace flag from the leading token. */ | |
1449 | macro->expansion[0].flags &= ~PREV_WHITE; | |
1450 | ||
44ed91a1 NB |
1451 | /* Implement the macro-defined-to-itself optimisation. */ |
1452 | macro->disabled = (macro->count == 1 && !macro->fun_like | |
1453 | && macro->expansion[0].type == CPP_NAME | |
1454 | && macro->expansion[0].val.node == node); | |
1455 | ||
93c80368 NB |
1456 | /* Commit the memory. */ |
1457 | POOL_COMMIT (&pfile->macro_pool, macro->count * sizeof (cpp_token)); | |
1458 | ||
1459 | /* Redefinition of a macro is allowed if and only if the old and new | |
1460 | definitions are the same. (6.10.3 paragraph 2). */ | |
1461 | if (node->type != NT_VOID) | |
711b8824 | 1462 | { |
93c80368 NB |
1463 | if (CPP_PEDANTIC (pfile) |
1464 | && !check_macro_redefinition (pfile, node, macro)) | |
711b8824 | 1465 | { |
93c80368 NB |
1466 | cpp_pedwarn_with_line (pfile, pfile->directive_pos.line, |
1467 | pfile->directive_pos.col, | |
1468 | "\"%s\" redefined", node->name); | |
1469 | ||
1470 | if (pfile->done_initializing && node->type == NT_MACRO | |
1471 | && !(node->flags & NODE_BUILTIN)) | |
711b8824 | 1472 | cpp_pedwarn_with_file_and_line (pfile, |
93c80368 NB |
1473 | node->value.macro->file, |
1474 | node->value.macro->line, 1, | |
711b8824 ZW |
1475 | "this is the location of the previous definition"); |
1476 | } | |
93c80368 | 1477 | _cpp_free_definition (node); |
711b8824 ZW |
1478 | } |
1479 | ||
1480 | /* Enter definition in hash table. */ | |
93c80368 NB |
1481 | node->type = NT_MACRO; |
1482 | node->value.macro = macro; | |
711b8824 | 1483 | |
93c80368 NB |
1484 | cleanup: |
1485 | ||
1486 | /* Stop the lexer accepting __VA_ARGS__. */ | |
1487 | pfile->state.va_args_ok = 0; | |
1488 | ||
1489 | /* Clear the fast argument lookup indices. */ | |
1490 | for (i = macro->paramc; i-- > 0; ) | |
1491 | macro->params[i]->arg_index = 0; | |
1492 | ||
1493 | return ok; | |
711b8824 ZW |
1494 | } |
1495 | ||
e1aa5140 KG |
1496 | /* Warn if a token in `string' matches one of the function macro |
1497 | arguments in `info'. This function assumes that the macro is a | |
1498 | function macro and not an object macro. */ | |
1499 | static void | |
93c80368 | 1500 | check_trad_stringification (pfile, macro, string) |
e1aa5140 | 1501 | cpp_reader *pfile; |
93c80368 | 1502 | const cpp_macro *macro; |
e1aa5140 KG |
1503 | const cpp_string *string; |
1504 | { | |
93c80368 | 1505 | unsigned int i, len; |
e1aa5140 KG |
1506 | const U_CHAR *p, *q, *limit = string->text + string->len; |
1507 | ||
1508 | /* Loop over the string. */ | |
1509 | for (p = string->text; p < limit; p = q) | |
1510 | { | |
e1aa5140 | 1511 | /* Find the start of an identifier. */ |
61c16b10 GM |
1512 | while (p < limit && !is_idstart (*p)) |
1513 | p++; | |
e1aa5140 KG |
1514 | |
1515 | /* Find the end of the identifier. */ | |
1516 | q = p; | |
61c16b10 GM |
1517 | while (q < limit && is_idchar (*q)) |
1518 | q++; | |
93c80368 NB |
1519 | |
1520 | len = q - p; | |
1521 | ||
e1aa5140 KG |
1522 | /* Loop over the function macro arguments to see if the |
1523 | identifier inside the string matches one of them. */ | |
93c80368 NB |
1524 | for (i = 0; i < macro->paramc; i++) |
1525 | { | |
1526 | const cpp_hashnode *node = macro->params[i]; | |
e1aa5140 | 1527 | |
93c80368 | 1528 | if (node->length == len && !memcmp (p, node->name, len)) |
e1aa5140 KG |
1529 | { |
1530 | cpp_warning (pfile, | |
93c80368 NB |
1531 | "macro argument \"%s\" would be stringified with -traditional.", |
1532 | node->name); | |
e1aa5140 KG |
1533 | break; |
1534 | } | |
1535 | } | |
1536 | } | |
1537 | } | |
93c80368 NB |
1538 | |
1539 | /* Returns the expansion of a macro, in a format suitable to be read | |
1540 | back in again, and therefore also for DWARF 2 debugging info. | |
1541 | Caller is expected to generate the "#define NAME" bit. The | |
1542 | returned text is temporary, and automatically freed later. */ | |
1543 | ||
1544 | const unsigned char * | |
1545 | cpp_macro_definition (pfile, node) | |
1546 | cpp_reader *pfile; | |
1547 | const cpp_hashnode *node; | |
1548 | { | |
1549 | unsigned int i, len; | |
1550 | const cpp_macro *macro = node->value.macro; | |
1551 | unsigned char *buffer; | |
1552 | ||
1553 | if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN)) | |
1554 | { | |
1555 | cpp_ice (pfile, "invalid hash type %d in dump_definition", node->type); | |
1556 | return 0; | |
1557 | } | |
1558 | ||
1559 | /* Calculate length. */ | |
1560 | len = 1; /* ' ' */ | |
1561 | if (macro->fun_like) | |
1562 | { | |
1563 | len += 3; /* "()" plus possible final "." of ellipsis. */ | |
1564 | for (i = 0; i < macro->paramc; i++) | |
1565 | len += macro->params[i]->length + 2; /* ", " */ | |
1566 | } | |
1567 | ||
4c2b647d | 1568 | for (i = 0; i < macro->count; i++) |
93c80368 | 1569 | { |
4c2b647d | 1570 | cpp_token *token = ¯o->expansion[i]; |
93c80368 | 1571 | |
4c2b647d NB |
1572 | if (token->type == CPP_MACRO_ARG) |
1573 | len += macro->params[token->val.arg_no - 1]->length; | |
1574 | else | |
1575 | len += cpp_token_len (token); /* Includes room for ' '. */ | |
1576 | if (token->flags & STRINGIFY_ARG) | |
1577 | len++; /* "#" */ | |
1578 | if (token->flags & PASTE_LEFT) | |
1579 | len += 3; /* " ##" */ | |
93c80368 NB |
1580 | } |
1581 | ||
1582 | if (len > pfile->macro_buffer_len) | |
4b49c365 AO |
1583 | { |
1584 | pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len); | |
1585 | pfile->macro_buffer_len = len; | |
1586 | } | |
93c80368 NB |
1587 | buffer = pfile->macro_buffer; |
1588 | ||
1589 | /* Parameter names. */ | |
1590 | if (macro->fun_like) | |
1591 | { | |
1592 | *buffer++ = '('; | |
1593 | for (i = 0; i < macro->paramc; i++) | |
1594 | { | |
1595 | cpp_hashnode *param = macro->params[i]; | |
1596 | ||
1597 | if (param != pfile->spec_nodes.n__VA_ARGS__) | |
1598 | { | |
1599 | memcpy (buffer, param->name, param->length); | |
1600 | buffer += param->length; | |
1601 | } | |
1602 | ||
1603 | if (i + 1 < macro->paramc) | |
1604 | *buffer++ = ',', *buffer++ = ' '; | |
28e0f040 | 1605 | else if (macro->variadic) |
93c80368 NB |
1606 | *buffer++ = '.', *buffer++ = '.', *buffer++ = '.'; |
1607 | } | |
1608 | *buffer++ = ')'; | |
1609 | } | |
1610 | ||
1611 | /* Expansion tokens. */ | |
4c2b647d | 1612 | if (macro->count) |
93c80368 NB |
1613 | { |
1614 | *buffer++ = ' '; | |
1615 | for (i = 0; i < macro->count; i++) | |
1616 | { | |
1617 | cpp_token *token = ¯o->expansion[i]; | |
1618 | ||
1619 | if (token->flags & PREV_WHITE) | |
1620 | *buffer++ = ' '; | |
1621 | if (token->flags & STRINGIFY_ARG) | |
1622 | *buffer++ = '#'; | |
1623 | ||
1624 | if (token->type == CPP_MACRO_ARG) | |
1625 | { | |
6c53ebff NB |
1626 | len = macro->params[token->val.arg_no - 1]->length; |
1627 | memcpy (buffer, macro->params[token->val.arg_no - 1]->name, len); | |
93c80368 NB |
1628 | buffer += len; |
1629 | } | |
1630 | else | |
1631 | buffer = cpp_spell_token (pfile, token, buffer); | |
1632 | ||
1633 | if (token->flags & PASTE_LEFT) | |
1634 | { | |
1635 | *buffer++ = ' '; | |
1636 | *buffer++ = '#'; | |
1637 | *buffer++ = '#'; | |
1638 | /* Next has PREV_WHITE; see _cpp_create_definition. */ | |
1639 | } | |
1640 | } | |
1641 | } | |
1642 | ||
1643 | *buffer = '\0'; | |
1644 | return pfile->macro_buffer; | |
1645 | } |