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