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