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