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