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