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