]>
Commit | Line | Data |
---|---|---|
79bd622b | 1 | /* Part of CPP library. (Macro and #define handling.) |
806a3d45 | 2 | Copyright (C) 1986-2014 Free Software Foundation, Inc. |
69461e0d | 3 | Written by Per Bothner, 1994. |
4 | Based on CCCP program by Paul Rubin, June 1986 | |
5 | Adapted to ANSI C, Richard Stallman, Jan 1987 | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
6bc9506f | 9 | Free Software Foundation; either version 3, or (at your option) any |
69461e0d | 10 | later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
6bc9506f | 18 | along with this program; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. | |
69461e0d | 20 | |
21 | In other words, you are welcome to use, share and improve this program. | |
22 | You are forbidden to forbid anyone else to use, share and improve | |
23 | what you give them. Help stamp out software-hoarding! */ | |
24 | ||
25 | #include "config.h" | |
26 | #include "system.h" | |
27 | #include "cpplib.h" | |
d856c8a6 | 28 | #include "internal.h" |
69461e0d | 29 | |
79bd622b | 30 | typedef struct macro_arg macro_arg; |
ce70f433 | 31 | /* This structure represents the tokens of a macro argument. These |
32 | tokens can be macro themselves, in which case they can be either | |
33 | expanded or unexpanded. When they are expanded, this data | |
34 | structure keeps both the expanded and unexpanded forms. */ | |
79bd622b | 35 | struct macro_arg |
36 | { | |
f9b5f742 | 37 | const cpp_token **first; /* First token in unexpanded argument. */ |
1e625a2e | 38 | const cpp_token **expanded; /* Macro-expanded argument. */ |
f9b5f742 | 39 | const cpp_token *stringified; /* Stringified argument. */ |
79bd622b | 40 | unsigned int count; /* # of tokens in argument. */ |
41 | unsigned int expanded_count; /* # of tokens in expanded argument. */ | |
ce70f433 | 42 | source_location *virt_locs; /* Where virtual locations for |
43 | unexpanded tokens are stored. */ | |
44 | source_location *expanded_virt_locs; /* Where virtual locations for | |
45 | expanded tokens are | |
46 | stored. */ | |
47 | }; | |
48 | ||
49 | /* The kind of macro tokens which the instance of | |
50 | macro_arg_token_iter is supposed to iterate over. */ | |
51 | enum macro_arg_token_kind { | |
52 | MACRO_ARG_TOKEN_NORMAL, | |
53 | /* This is a macro argument token that got transformed into a string | |
54 | litteral, e.g. #foo. */ | |
55 | MACRO_ARG_TOKEN_STRINGIFIED, | |
56 | /* This is a token resulting from the expansion of a macro | |
57 | argument that was itself a macro. */ | |
58 | MACRO_ARG_TOKEN_EXPANDED | |
59 | }; | |
60 | ||
61 | /* An iterator over tokens coming from a function-like macro | |
62 | argument. */ | |
63 | typedef struct macro_arg_token_iter macro_arg_token_iter; | |
64 | struct macro_arg_token_iter | |
65 | { | |
66 | /* Whether or not -ftrack-macro-expansion is used. */ | |
67 | bool track_macro_exp_p; | |
68 | /* The kind of token over which we are supposed to iterate. */ | |
69 | enum macro_arg_token_kind kind; | |
70 | /* A pointer to the current token pointed to by the iterator. */ | |
71 | const cpp_token **token_ptr; | |
72 | /* A pointer to the "full" location of the current token. If | |
e8aa7eaf | 73 | -ftrack-macro-expansion is used this location tracks loci across |
ce70f433 | 74 | macro expansion. */ |
75 | const source_location *location_ptr; | |
76 | #ifdef ENABLE_CHECKING | |
77 | /* The number of times the iterator went forward. This useful only | |
78 | when checking is enabled. */ | |
79 | size_t num_forwards; | |
80 | #endif | |
69461e0d | 81 | }; |
82 | ||
109ca87a | 83 | /* Saved data about an identifier being used as a macro argument |
84 | name. */ | |
85 | struct macro_arg_saved_data { | |
86 | /* The canonical (UTF-8) spelling of this identifier. */ | |
87 | cpp_hashnode *canonical_node; | |
88 | /* The previous value of this identifier. */ | |
89 | union _cpp_hashnode_value value; | |
90 | }; | |
91 | ||
79bd622b | 92 | /* Macro expansion. */ |
93 | ||
45f9f140 | 94 | static int enter_macro_context (cpp_reader *, cpp_hashnode *, |
ce70f433 | 95 | const cpp_token *, source_location); |
2e82cf2c | 96 | static int builtin_macro (cpp_reader *, cpp_hashnode *, source_location); |
f7fdd7a1 | 97 | static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *, |
98 | const cpp_token **, unsigned int); | |
ce70f433 | 99 | static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *, |
100 | _cpp_buff *, source_location *, | |
101 | const cpp_token **, unsigned int); | |
45f9f140 | 102 | static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *, |
ce70f433 | 103 | _cpp_buff **, unsigned *); |
f7fdd7a1 | 104 | static cpp_context *next_context (cpp_reader *); |
105 | static const cpp_token *padding_token (cpp_reader *, const cpp_token *); | |
106 | static void expand_arg (cpp_reader *, macro_arg *); | |
107 | static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int); | |
108 | static const cpp_token *stringify_arg (cpp_reader *, macro_arg *); | |
109 | static void paste_all_tokens (cpp_reader *, const cpp_token *); | |
8c6425eb | 110 | static bool paste_tokens (cpp_reader *, source_location, |
111 | const cpp_token **, const cpp_token *); | |
ce70f433 | 112 | static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t); |
113 | static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *); | |
114 | static void delete_macro_args (_cpp_buff*, unsigned num_args); | |
115 | static void set_arg_token (macro_arg *, const cpp_token *, | |
116 | source_location, size_t, | |
117 | enum macro_arg_token_kind, | |
118 | bool); | |
119 | static const source_location *get_arg_token_location (const macro_arg *, | |
120 | enum macro_arg_token_kind); | |
121 | static const cpp_token **arg_token_ptr_at (const macro_arg *, | |
122 | size_t, | |
123 | enum macro_arg_token_kind, | |
124 | source_location **virt_location); | |
125 | ||
126 | static void macro_arg_token_iter_init (macro_arg_token_iter *, bool, | |
127 | enum macro_arg_token_kind, | |
128 | const macro_arg *, | |
129 | const cpp_token **); | |
130 | static const cpp_token *macro_arg_token_iter_get_token | |
131 | (const macro_arg_token_iter *it); | |
132 | static source_location macro_arg_token_iter_get_location | |
133 | (const macro_arg_token_iter *); | |
134 | static void macro_arg_token_iter_forward (macro_arg_token_iter *); | |
135 | static _cpp_buff *tokens_buff_new (cpp_reader *, size_t, | |
136 | source_location **); | |
137 | static size_t tokens_buff_count (_cpp_buff *); | |
138 | static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *); | |
80e34234 | 139 | static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **, |
140 | source_location *, | |
141 | const cpp_token *, | |
142 | source_location, | |
143 | source_location, | |
144 | const struct line_map *, | |
145 | unsigned int); | |
ce70f433 | 146 | |
147 | static const cpp_token **tokens_buff_add_token (_cpp_buff *, | |
148 | source_location *, | |
149 | const cpp_token *, | |
150 | source_location, | |
151 | source_location, | |
152 | const struct line_map *, | |
153 | unsigned int); | |
80e34234 | 154 | static inline void tokens_buff_remove_last_token (_cpp_buff *); |
f7fdd7a1 | 155 | static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *, |
ce70f433 | 156 | macro_arg *, source_location); |
45f9f140 | 157 | static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *, |
ce70f433 | 158 | _cpp_buff **, unsigned *); |
f7fdd7a1 | 159 | static bool create_iso_definition (cpp_reader *, cpp_macro *); |
79bd622b | 160 | |
79bd622b | 161 | /* #define directive parsing and handling. */ |
162 | ||
f7fdd7a1 | 163 | static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *); |
164 | static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *); | |
25692381 | 165 | static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *, |
f7fdd7a1 | 166 | const cpp_macro *); |
167 | static bool parse_params (cpp_reader *, cpp_macro *); | |
168 | static void check_trad_stringification (cpp_reader *, const cpp_macro *, | |
169 | const cpp_string *); | |
ce70f433 | 170 | static bool reached_end_of_context (cpp_context *); |
171 | static void consume_next_token_from_context (cpp_reader *pfile, | |
172 | const cpp_token **, | |
173 | source_location *); | |
174 | static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *); | |
69461e0d | 175 | |
00abfdc0 | 176 | static cpp_hashnode* macro_of_context (cpp_context *context); |
177 | ||
8c6425eb | 178 | static bool in_macro_expansion_p (cpp_reader *pfile); |
179 | ||
e77b8253 | 180 | /* Statistical counter tracking the number of macros that got |
181 | expanded. */ | |
182 | unsigned num_expanded_macros_counter = 0; | |
183 | /* Statistical counter tracking the total number tokens resulting | |
184 | from macro expansion. */ | |
185 | unsigned num_macro_tokens_counter = 0; | |
186 | ||
71a7c282 | 187 | /* Emits a warning if NODE is a macro defined in the main file that |
188 | has not been used. */ | |
189 | int | |
f7fdd7a1 | 190 | _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node, |
191 | void *v ATTRIBUTE_UNUSED) | |
71a7c282 | 192 | { |
193 | if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) | |
194 | { | |
195 | cpp_macro *macro = node->value.macro; | |
196 | ||
197 | if (!macro->used | |
ceec9c13 | 198 | && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line))) |
3a79f5da | 199 | cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0, |
200 | "macro \"%s\" is not used", NODE_NAME (node)); | |
71a7c282 | 201 | } |
202 | ||
203 | return 1; | |
204 | } | |
205 | ||
f9b5f742 | 206 | /* Allocates and returns a CPP_STRING token, containing TEXT of length |
207 | LEN, after null-terminating it. TEXT must be in permanent storage. */ | |
208 | static const cpp_token * | |
f7fdd7a1 | 209 | new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len) |
79bd622b | 210 | { |
f9b5f742 | 211 | cpp_token *token = _cpp_temp_token (pfile); |
79bd622b | 212 | |
f9b5f742 | 213 | text[len] = '\0'; |
79bd622b | 214 | token->type = CPP_STRING; |
f9b5f742 | 215 | token->val.str.len = len; |
216 | token->val.str.text = text; | |
79bd622b | 217 | token->flags = 0; |
f9b5f742 | 218 | return token; |
79bd622b | 219 | } |
220 | ||
79bd622b | 221 | static const char * const monthnames[] = |
222 | { | |
223 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
224 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
225 | }; | |
226 | ||
d6d3c909 | 227 | /* Helper function for builtin_macro. Returns the text generated by |
228 | a builtin macro. */ | |
9c343313 | 229 | const uchar * |
f7fdd7a1 | 230 | _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node) |
79bd622b | 231 | { |
9c343313 | 232 | const uchar *result = NULL; |
4999c35b | 233 | linenum_type number = 1; |
c7e5d924 | 234 | |
79bd622b | 235 | switch (node->value.builtin) |
236 | { | |
f9b5f742 | 237 | default: |
d80d2074 | 238 | cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", |
73328dce | 239 | NODE_NAME (node)); |
9c343313 | 240 | break; |
f9b5f742 | 241 | |
cca5dddc | 242 | case BT_TIMESTAMP: |
243 | { | |
9148bda3 | 244 | if (CPP_OPTION (pfile, warn_date_time)) |
5b1a0622 | 245 | cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent " |
246 | "reproducible builds", NODE_NAME (node)); | |
9148bda3 | 247 | |
cca5dddc | 248 | cpp_buffer *pbuffer = cpp_get_buffer (pfile); |
249 | if (pbuffer->timestamp == NULL) | |
250 | { | |
251 | /* Initialize timestamp value of the assotiated file. */ | |
252 | struct _cpp_file *file = cpp_get_file (pbuffer); | |
253 | if (file) | |
254 | { | |
255 | /* Generate __TIMESTAMP__ string, that represents | |
256 | the date and time of the last modification | |
257 | of the current source file. The string constant | |
258 | looks like "Sun Sep 16 01:03:52 1973". */ | |
259 | struct tm *tb = NULL; | |
260 | struct stat *st = _cpp_get_file_stat (file); | |
261 | if (st) | |
262 | tb = localtime (&st->st_mtime); | |
263 | if (tb) | |
264 | { | |
265 | char *str = asctime (tb); | |
266 | size_t len = strlen (str); | |
267 | unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2); | |
268 | buf[0] = '"'; | |
269 | strcpy ((char *) buf + 1, str); | |
270 | buf[len] = '"'; | |
271 | pbuffer->timestamp = buf; | |
272 | } | |
273 | else | |
274 | { | |
275 | cpp_errno (pfile, CPP_DL_WARNING, | |
276 | "could not determine file timestamp"); | |
924bbf02 | 277 | pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\""; |
cca5dddc | 278 | } |
279 | } | |
280 | } | |
281 | result = pbuffer->timestamp; | |
282 | } | |
283 | break; | |
79bd622b | 284 | case BT_FILE: |
285 | case BT_BASE_FILE: | |
69461e0d | 286 | { |
f9b5f742 | 287 | unsigned int len; |
6cee4464 | 288 | const char *name; |
b6d18b0a | 289 | uchar *buf; |
97bfb9ef | 290 | |
291 | if (node->value.builtin == BT_FILE) | |
292 | name = linemap_get_expansion_filename (pfile->line_table, | |
293 | pfile->line_table->highest_line); | |
294 | else | |
295 | { | |
5e791406 | 296 | name = _cpp_get_file_name (pfile->main_file); |
297 | if (!name) | |
298 | abort (); | |
97bfb9ef | 299 | } |
f9b5f742 | 300 | len = strlen (name); |
cc9012f7 | 301 | buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); |
9c343313 | 302 | result = buf; |
303 | *buf = '"'; | |
304 | buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len); | |
305 | *buf++ = '"'; | |
306 | *buf = '\0'; | |
69461e0d | 307 | } |
c7e5d924 | 308 | break; |
309 | ||
79bd622b | 310 | case BT_INCLUDE_LEVEL: |
4087823d | 311 | /* The line map depth counts the primary source as level 1, but |
312 | historically __INCLUDE_DEPTH__ has called the primary source | |
313 | level 0. */ | |
ceec9c13 | 314 | number = pfile->line_table->depth - 1; |
c7e5d924 | 315 | break; |
79bd622b | 316 | |
317 | case BT_SPECLINE: | |
318 | /* If __LINE__ is embedded in a macro, it must expand to the | |
319 | line of the macro's invocation, not its definition. | |
320 | Otherwise things like assert() will not work properly. */ | |
97bfb9ef | 321 | number = linemap_get_expansion_line (pfile->line_table, |
322 | CPP_OPTION (pfile, traditional) | |
323 | ? pfile->line_table->highest_line | |
324 | : pfile->cur_token[-1].src_loc); | |
c7e5d924 | 325 | break; |
79bd622b | 326 | |
63994318 | 327 | /* __STDC__ has the value 1 under normal circumstances. |
328 | However, if (a) we are in a system header, (b) the option | |
965e9876 | 329 | stdc_0_in_system_headers is true (set by target config), and |
330 | (c) we are not in strictly conforming mode, then it has the | |
31614f7c | 331 | value 0. (b) and (c) are already checked in cpp_init_builtins. */ |
79bd622b | 332 | case BT_STDC: |
31614f7c | 333 | if (cpp_in_system_header (pfile)) |
334 | number = 0; | |
335 | else | |
336 | number = 1; | |
c7e5d924 | 337 | break; |
69461e0d | 338 | |
79bd622b | 339 | case BT_DATE: |
340 | case BT_TIME: | |
9148bda3 | 341 | if (CPP_OPTION (pfile, warn_date_time)) |
5b1a0622 | 342 | cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent " |
343 | "reproducible builds", NODE_NAME (node)); | |
9c343313 | 344 | if (pfile->date == NULL) |
79bd622b | 345 | { |
f9b5f742 | 346 | /* Allocate __DATE__ and __TIME__ strings from permanent |
347 | storage. We only do this once, and don't generate them | |
348 | at init time, because time() and localtime() are very | |
349 | slow on some systems. */ | |
3385506f | 350 | time_t tt; |
351 | struct tm *tb = NULL; | |
352 | ||
353 | /* (time_t) -1 is a legitimate value for "number of seconds | |
354 | since the Epoch", so we have to do a little dance to | |
355 | distinguish that from a genuine error. */ | |
356 | errno = 0; | |
357 | tt = time(NULL); | |
358 | if (tt != (time_t)-1 || errno == 0) | |
359 | tb = localtime (&tt); | |
360 | ||
361 | if (tb) | |
362 | { | |
363 | pfile->date = _cpp_unaligned_alloc (pfile, | |
364 | sizeof ("\"Oct 11 1347\"")); | |
365 | sprintf ((char *) pfile->date, "\"%s %2d %4d\"", | |
f7fdd7a1 | 366 | monthnames[tb->tm_mon], tb->tm_mday, |
367 | tb->tm_year + 1900); | |
3385506f | 368 | |
369 | pfile->time = _cpp_unaligned_alloc (pfile, | |
370 | sizeof ("\"12:34:56\"")); | |
371 | sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"", | |
372 | tb->tm_hour, tb->tm_min, tb->tm_sec); | |
373 | } | |
374 | else | |
375 | { | |
d80d2074 | 376 | cpp_errno (pfile, CPP_DL_WARNING, |
3385506f | 377 | "could not determine date and time"); |
378 | ||
924bbf02 | 379 | pfile->date = UC"\"??? ?? ????\""; |
380 | pfile->time = UC"\"??:??:??\""; | |
3385506f | 381 | } |
79bd622b | 382 | } |
79bd622b | 383 | |
c7e5d924 | 384 | if (node->value.builtin == BT_DATE) |
9c343313 | 385 | result = pfile->date; |
c7e5d924 | 386 | else |
9c343313 | 387 | result = pfile->time; |
c7e5d924 | 388 | break; |
ce079f70 | 389 | |
390 | case BT_COUNTER: | |
fcde64dc | 391 | if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive) |
392 | cpp_error (pfile, CPP_DL_ERROR, | |
393 | "__COUNTER__ expanded inside directive with -fdirectives-only"); | |
ce079f70 | 394 | number = pfile->counter++; |
395 | break; | |
9c343313 | 396 | } |
397 | ||
398 | if (result == NULL) | |
399 | { | |
400 | /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */ | |
401 | result = _cpp_unaligned_alloc (pfile, 21); | |
402 | sprintf ((char *) result, "%u", number); | |
403 | } | |
404 | ||
405 | return result; | |
406 | } | |
407 | ||
408 | /* Convert builtin macros like __FILE__ to a token and push it on the | |
d6d3c909 | 409 | context stack. Also handles _Pragma, for which a new token may not |
410 | be created. Returns 1 if it generates a new token context, 0 to | |
2e82cf2c | 411 | return the token to the caller. LOC is the location of the expansion |
412 | point of the macro. */ | |
9c343313 | 413 | static int |
2e82cf2c | 414 | builtin_macro (cpp_reader *pfile, cpp_hashnode *node, source_location loc) |
9c343313 | 415 | { |
416 | const uchar *buf; | |
a54e0bf8 | 417 | size_t len; |
418 | char *nbuf; | |
c7e5d924 | 419 | |
9c343313 | 420 | if (node->value.builtin == BT_PRAGMA) |
421 | { | |
c7e5d924 | 422 | /* Don't interpret _Pragma within directives. The standard is |
423 | not clear on this, but to me this makes most sense. */ | |
424 | if (pfile->state.in_directive) | |
425 | return 0; | |
426 | ||
2d507e67 | 427 | return _cpp_do__Pragma (pfile); |
79bd622b | 428 | } |
c7e5d924 | 429 | |
9c343313 | 430 | buf = _cpp_builtin_macro_text (pfile, node); |
a54e0bf8 | 431 | len = ustrlen (buf); |
720aca92 | 432 | nbuf = (char *) alloca (len + 1); |
a54e0bf8 | 433 | memcpy (nbuf, buf, len); |
434 | nbuf[len]='\n'; | |
9c343313 | 435 | |
9eb74666 | 436 | cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true); |
a54e0bf8 | 437 | _cpp_clean_line (pfile); |
9c343313 | 438 | |
439 | /* Set pfile->cur_token as required by _cpp_lex_direct. */ | |
440 | pfile->cur_token = _cpp_temp_token (pfile); | |
a4cfdfed | 441 | cpp_token *token = _cpp_lex_direct (pfile); |
2e82cf2c | 442 | /* We should point to the expansion point of the builtin macro. */ |
443 | token->src_loc = loc; | |
a4cfdfed | 444 | if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) |
445 | { | |
446 | /* We are tracking tokens resulting from macro expansion. | |
447 | Create a macro line map and generate a virtual location for | |
448 | the token resulting from the expansion of the built-in | |
449 | macro. */ | |
450 | source_location *virt_locs = NULL; | |
451 | _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs); | |
452 | const line_map * map = | |
453 | linemap_enter_macro (pfile->line_table, node, | |
454 | token->src_loc, 1); | |
455 | tokens_buff_add_token (token_buf, virt_locs, token, | |
456 | pfile->line_table->builtin_location, | |
457 | pfile->line_table->builtin_location, | |
458 | map, /*macro_token_index=*/0); | |
459 | push_extended_tokens_context (pfile, node, token_buf, virt_locs, | |
460 | (const cpp_token **)token_buf->base, | |
461 | 1); | |
462 | } | |
463 | else | |
464 | _cpp_push_token_context (pfile, NULL, token, 1); | |
9c343313 | 465 | if (pfile->buffer->cur != pfile->buffer->rlimit) |
d80d2074 | 466 | cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", |
9c343313 | 467 | NODE_NAME (node)); |
468 | _cpp_pop_buffer (pfile); | |
469 | ||
c7e5d924 | 470 | return 1; |
69461e0d | 471 | } |
472 | ||
88cf66fa | 473 | /* Copies SRC, of length LEN, to DEST, adding backslashes before all |
cc9012f7 | 474 | backslashes and double quotes. DEST must be of sufficient size. |
475 | Returns a pointer to the end of the string. */ | |
b6d18b0a | 476 | uchar * |
f7fdd7a1 | 477 | cpp_quote_string (uchar *dest, const uchar *src, unsigned int len) |
79bd622b | 478 | { |
479 | while (len--) | |
480 | { | |
b6d18b0a | 481 | uchar c = *src++; |
69461e0d | 482 | |
79bd622b | 483 | if (c == '\\' || c == '"') |
484 | { | |
485 | *dest++ = '\\'; | |
486 | *dest++ = c; | |
487 | } | |
488 | else | |
cc9012f7 | 489 | *dest++ = c; |
79bd622b | 490 | } |
69461e0d | 491 | |
79bd622b | 492 | return dest; |
493 | } | |
69461e0d | 494 | |
88cf66fa | 495 | /* Convert a token sequence ARG to a single string token according to |
496 | the rules of the ISO C #-operator. */ | |
f9b5f742 | 497 | static const cpp_token * |
f7fdd7a1 | 498 | stringify_arg (cpp_reader *pfile, macro_arg *arg) |
79bd622b | 499 | { |
4970d4c2 | 500 | unsigned char *dest; |
1fdf6039 | 501 | unsigned int i, escape_it, backslash_count = 0; |
f9b5f742 | 502 | const cpp_token *source = NULL; |
1fdf6039 | 503 | size_t len; |
69461e0d | 504 | |
4970d4c2 | 505 | if (BUFF_ROOM (pfile->u_buff) < 3) |
506 | _cpp_extend_buff (pfile, &pfile->u_buff, 3); | |
507 | dest = BUFF_FRONT (pfile->u_buff); | |
508 | *dest++ = '"'; | |
509 | ||
79bd622b | 510 | /* Loop, reading in the argument's tokens. */ |
511 | for (i = 0; i < arg->count; i++) | |
512 | { | |
f9b5f742 | 513 | const cpp_token *token = arg->first[i]; |
f9b5f742 | 514 | |
515 | if (token->type == CPP_PADDING) | |
516 | { | |
e21164ef | 517 | if (source == NULL |
518 | || (!(source->flags & PREV_WHITE) | |
519 | && token->val.source == NULL)) | |
f9b5f742 | 520 | source = token->val.source; |
521 | continue; | |
522 | } | |
69461e0d | 523 | |
924bbf02 | 524 | escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR |
2a50bfcf | 525 | || token->type == CPP_WSTRING || token->type == CPP_WCHAR |
924bbf02 | 526 | || token->type == CPP_STRING32 || token->type == CPP_CHAR32 |
538ba11a | 527 | || token->type == CPP_STRING16 || token->type == CPP_CHAR16 |
1608bb4b | 528 | || token->type == CPP_UTF8STRING |
529 | || cpp_userdef_string_p (token->type) | |
530 | || cpp_userdef_char_p (token->type)); | |
df271348 | 531 | |
1fdf6039 | 532 | /* Room for each char being written in octal, initial space and |
4970d4c2 | 533 | final quote and NUL. */ |
f9b5f742 | 534 | len = cpp_token_len (token); |
79bd622b | 535 | if (escape_it) |
79bd622b | 536 | len *= 4; |
4970d4c2 | 537 | len += 3; |
69461e0d | 538 | |
1fdf6039 | 539 | if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len) |
79bd622b | 540 | { |
1fdf6039 | 541 | size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff); |
e6a5f963 | 542 | _cpp_extend_buff (pfile, &pfile->u_buff, len); |
1fdf6039 | 543 | dest = BUFF_FRONT (pfile->u_buff) + len_so_far; |
79bd622b | 544 | } |
69461e0d | 545 | |
f9b5f742 | 546 | /* Leading white space? */ |
4970d4c2 | 547 | if (dest - 1 != BUFF_FRONT (pfile->u_buff)) |
f9b5f742 | 548 | { |
549 | if (source == NULL) | |
550 | source = token; | |
551 | if (source->flags & PREV_WHITE) | |
552 | *dest++ = ' '; | |
553 | } | |
554 | source = NULL; | |
69461e0d | 555 | |
79bd622b | 556 | if (escape_it) |
557 | { | |
1fdf6039 | 558 | _cpp_buff *buff = _cpp_get_buff (pfile, len); |
559 | unsigned char *buf = BUFF_FRONT (buff); | |
bb1fa6bb | 560 | len = cpp_spell_token (pfile, token, buf, true) - buf; |
25266990 | 561 | dest = cpp_quote_string (dest, buf, len); |
1fdf6039 | 562 | _cpp_release_buff (pfile, buff); |
79bd622b | 563 | } |
564 | else | |
bb1fa6bb | 565 | dest = cpp_spell_token (pfile, token, dest, true); |
79bd622b | 566 | |
bc205914 | 567 | if (token->type == CPP_OTHER && token->val.str.text[0] == '\\') |
79bd622b | 568 | backslash_count++; |
569 | else | |
570 | backslash_count = 0; | |
571 | } | |
572 | ||
573 | /* Ignore the final \ of invalid string literals. */ | |
574 | if (backslash_count & 1) | |
575 | { | |
d80d2074 | 576 | cpp_error (pfile, CPP_DL_WARNING, |
73328dce | 577 | "invalid string literal, ignoring final '\\'"); |
1fdf6039 | 578 | dest--; |
79bd622b | 579 | } |
580 | ||
f9b5f742 | 581 | /* Commit the memory, including NUL, and return the token. */ |
4970d4c2 | 582 | *dest++ = '"'; |
1fdf6039 | 583 | len = dest - BUFF_FRONT (pfile->u_buff); |
584 | BUFF_FRONT (pfile->u_buff) = dest + 1; | |
585 | return new_string_token (pfile, dest - len, len); | |
79bd622b | 586 | } |
587 | ||
d10cfa8d | 588 | /* Try to paste two tokens. On success, return nonzero. In any |
1785b647 | 589 | case, PLHS is updated to point to the pasted token, which is |
8c6425eb | 590 | guaranteed to not have the PASTE_LEFT flag set. LOCATION is |
591 | the virtual location used for error reporting. */ | |
1785b647 | 592 | static bool |
8c6425eb | 593 | paste_tokens (cpp_reader *pfile, source_location location, |
594 | const cpp_token **plhs, const cpp_token *rhs) | |
08138c65 | 595 | { |
46139d3f | 596 | unsigned char *buf, *end, *lhsend; |
3127c357 | 597 | cpp_token *lhs; |
1785b647 | 598 | unsigned int len; |
1785b647 | 599 | |
3127c357 | 600 | len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1; |
720aca92 | 601 | buf = (unsigned char *) alloca (len); |
109ca87a | 602 | end = lhsend = cpp_spell_token (pfile, *plhs, buf, true); |
1785b647 | 603 | |
604 | /* Avoid comment headers, since they are still processed in stage 3. | |
605 | It is simpler to insert a space here, rather than modifying the | |
606 | lexer to ignore comments in some circumstances. Simply returning | |
607 | false doesn't work, since we want to clear the PASTE_LEFT flag. */ | |
3127c357 | 608 | if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ) |
1785b647 | 609 | *end++ = ' '; |
1991d8eb | 610 | /* In one obscure case we might see padding here. */ |
611 | if (rhs->type != CPP_PADDING) | |
109ca87a | 612 | end = cpp_spell_token (pfile, rhs, end, true); |
a54e0bf8 | 613 | *end = '\n'; |
1785b647 | 614 | |
9eb74666 | 615 | cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true); |
a54e0bf8 | 616 | _cpp_clean_line (pfile); |
1785b647 | 617 | |
618 | /* Set pfile->cur_token as required by _cpp_lex_direct. */ | |
619 | pfile->cur_token = _cpp_temp_token (pfile); | |
3127c357 | 620 | lhs = _cpp_lex_direct (pfile); |
46139d3f | 621 | if (pfile->buffer->cur != pfile->buffer->rlimit) |
622 | { | |
3127c357 | 623 | source_location saved_loc = lhs->src_loc; |
624 | ||
46139d3f | 625 | _cpp_pop_buffer (pfile); |
626 | _cpp_backup_tokens (pfile, 1); | |
627 | *lhsend = '\0'; | |
628 | ||
3127c357 | 629 | /* We have to remove the PASTE_LEFT flag from the old lhs, but |
630 | we want to keep the new location. */ | |
631 | *lhs = **plhs; | |
632 | *plhs = lhs; | |
633 | lhs->src_loc = saved_loc; | |
634 | lhs->flags &= ~PASTE_LEFT; | |
635 | ||
46139d3f | 636 | /* Mandatory error for all apart from assembler. */ |
637 | if (CPP_OPTION (pfile, lang) != CLK_ASM) | |
8c6425eb | 638 | cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, |
46139d3f | 639 | "pasting \"%s\" and \"%s\" does not give a valid preprocessing token", |
640 | buf, cpp_token_as_text (pfile, rhs)); | |
641 | return false; | |
642 | } | |
1785b647 | 643 | |
3127c357 | 644 | *plhs = lhs; |
46139d3f | 645 | _cpp_pop_buffer (pfile); |
646 | return true; | |
08138c65 | 647 | } |
648 | ||
88cf66fa | 649 | /* Handles an arbitrarily long sequence of ## operators, with initial |
650 | operand LHS. This implementation is left-associative, | |
651 | non-recursive, and finishes a paste before handling succeeding | |
652 | ones. If a paste fails, we back up to the RHS of the failing ## | |
653 | operator before pushing the context containing the result of prior | |
654 | successful pastes, with the effect that the RHS appears in the | |
655 | output stream after the pasted LHS normally. */ | |
79bd622b | 656 | static void |
f7fdd7a1 | 657 | paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs) |
79bd622b | 658 | { |
ce70f433 | 659 | const cpp_token *rhs = NULL; |
f9b5f742 | 660 | cpp_context *context = pfile->context; |
c55700de | 661 | source_location virt_loc = 0; |
662 | ||
8c6425eb | 663 | /* We are expanding a macro and we must have been called on a token |
664 | that appears at the left hand side of a ## operator. */ | |
665 | if (macro_of_context (pfile->context) == NULL | |
666 | || (!(lhs->flags & PASTE_LEFT))) | |
c55700de | 667 | abort (); |
668 | ||
669 | if (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
670 | /* The caller must have called consume_next_token_from_context | |
671 | right before calling us. That has incremented the pointer to | |
672 | the current virtual location. So it now points to the location | |
673 | of the token that comes right after *LHS. We want the | |
674 | resulting pasted token to have the location of the current | |
675 | *LHS, though. */ | |
676 | virt_loc = context->c.mc->cur_virt_loc[-1]; | |
8c6425eb | 677 | else |
678 | /* We are not tracking macro expansion. So the best virtual | |
679 | location we can get here is the expansion point of the macro we | |
680 | are currently expanding. */ | |
681 | virt_loc = pfile->invocation_location; | |
f9b5f742 | 682 | |
79bd622b | 683 | do |
684 | { | |
685 | /* Take the token directly from the current context. We can do | |
686 | this, because we are in the replacement list of either an | |
687 | object-like macro, or a function-like macro with arguments | |
688 | inserted. In either case, the constraints to #define | |
08138c65 | 689 | guarantee we have at least one more token. */ |
ce70f433 | 690 | if (context->tokens_kind == TOKENS_KIND_DIRECT) |
a854276a | 691 | rhs = FIRST (context).token++; |
ce70f433 | 692 | else if (context->tokens_kind == TOKENS_KIND_INDIRECT) |
a854276a | 693 | rhs = *FIRST (context).ptoken++; |
ce70f433 | 694 | else if (context->tokens_kind == TOKENS_KIND_EXTENDED) |
695 | { | |
696 | /* So we are in presence of an extended token context, which | |
697 | means that each token in this context has a virtual | |
698 | location attached to it. So let's not forget to update | |
699 | the pointer to the current virtual location of the | |
700 | current token when we update the pointer to the current | |
701 | token */ | |
702 | ||
703 | rhs = *FIRST (context).ptoken++; | |
704 | /* context->c.mc must be non-null, as if we were not in a | |
705 | macro context, context->tokens_kind could not be equal to | |
706 | TOKENS_KIND_EXTENDED. */ | |
707 | context->c.mc->cur_virt_loc++; | |
708 | } | |
f9b5f742 | 709 | |
710 | if (rhs->type == CPP_PADDING) | |
1991d8eb | 711 | { |
712 | if (rhs->flags & PASTE_LEFT) | |
713 | abort (); | |
714 | } | |
8c6425eb | 715 | if (!paste_tokens (pfile, virt_loc, &lhs, rhs)) |
46139d3f | 716 | break; |
79bd622b | 717 | } |
718 | while (rhs->flags & PASTE_LEFT); | |
719 | ||
1785b647 | 720 | /* Put the resulting token in its own context. */ |
c55700de | 721 | if (context->tokens_kind == TOKENS_KIND_EXTENDED) |
722 | { | |
723 | source_location *virt_locs = NULL; | |
724 | _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs); | |
725 | tokens_buff_add_token (token_buf, virt_locs, lhs, | |
726 | virt_loc, 0, NULL, 0); | |
727 | push_extended_tokens_context (pfile, context->c.mc->macro_node, | |
728 | token_buf, virt_locs, | |
729 | (const cpp_token **)token_buf->base, 1); | |
730 | } | |
731 | else | |
732 | _cpp_push_token_context (pfile, NULL, lhs, 1); | |
79bd622b | 733 | } |
734 | ||
06025647 | 735 | /* Returns TRUE if the number of arguments ARGC supplied in an |
736 | invocation of the MACRO referenced by NODE is valid. An empty | |
737 | invocation to a macro with no parameters should pass ARGC as zero. | |
738 | ||
739 | Note that MACRO cannot necessarily be deduced from NODE, in case | |
740 | NODE was redefined whilst collecting arguments. */ | |
741 | bool | |
f7fdd7a1 | 742 | _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc) |
06025647 | 743 | { |
744 | if (argc == macro->paramc) | |
745 | return true; | |
746 | ||
747 | if (argc < macro->paramc) | |
748 | { | |
49f161b6 | 749 | /* As an extension, variadic arguments are allowed to not appear in |
06025647 | 750 | the invocation at all. |
751 | e.g. #define debug(format, args...) something | |
752 | debug("string"); | |
753 | ||
49f161b6 | 754 | This is exactly the same as if an empty variadic list had been |
755 | supplied - debug("string", ). */ | |
06025647 | 756 | |
757 | if (argc + 1 == macro->paramc && macro->variadic) | |
758 | { | |
759 | if (CPP_PEDANTIC (pfile) && ! macro->syshdr) | |
49f161b6 | 760 | { |
761 | if (CPP_OPTION (pfile, cplusplus)) | |
762 | cpp_error (pfile, CPP_DL_PEDWARN, | |
763 | "ISO C++11 requires at least one argument " | |
764 | "for the \"...\" in a variadic macro"); | |
765 | else | |
766 | cpp_error (pfile, CPP_DL_PEDWARN, | |
767 | "ISO C99 requires at least one argument " | |
768 | "for the \"...\" in a variadic macro"); | |
769 | } | |
06025647 | 770 | return true; |
771 | } | |
772 | ||
d80d2074 | 773 | cpp_error (pfile, CPP_DL_ERROR, |
06025647 | 774 | "macro \"%s\" requires %u arguments, but only %u given", |
775 | NODE_NAME (node), macro->paramc, argc); | |
776 | } | |
777 | else | |
d80d2074 | 778 | cpp_error (pfile, CPP_DL_ERROR, |
06025647 | 779 | "macro \"%s\" passed %u arguments, but takes just %u", |
780 | NODE_NAME (node), argc, macro->paramc); | |
781 | ||
782 | return false; | |
783 | } | |
784 | ||
88cf66fa | 785 | /* Reads and returns the arguments to a function-like macro |
786 | invocation. Assumes the opening parenthesis has been processed. | |
787 | If there is an error, emits an appropriate diagnostic and returns | |
788 | NULL. Each argument is terminated by a CPP_EOF token, for the | |
45f9f140 | 789 | future benefit of expand_arg(). If there are any deferred |
790 | #pragma directives among macro arguments, store pointers to the | |
ce70f433 | 791 | CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer. |
792 | ||
793 | What is returned is the buffer that contains the memory allocated | |
794 | to hold the macro arguments. NODE is the name of the macro this | |
795 | function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is | |
796 | set to the actual number of macro arguments allocated in the | |
797 | returned buffer. */ | |
06c92cbc | 798 | static _cpp_buff * |
45f9f140 | 799 | collect_args (cpp_reader *pfile, const cpp_hashnode *node, |
ce70f433 | 800 | _cpp_buff **pragma_buff, unsigned *num_args) |
79bd622b | 801 | { |
06c92cbc | 802 | _cpp_buff *buff, *base_buff; |
803 | cpp_macro *macro; | |
804 | macro_arg *args, *arg; | |
805 | const cpp_token *token; | |
806 | unsigned int argc; | |
ce70f433 | 807 | source_location virt_loc; |
808 | bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion); | |
809 | unsigned num_args_alloced = 0; | |
06c92cbc | 810 | |
811 | macro = node->value.macro; | |
812 | if (macro->paramc) | |
813 | argc = macro->paramc; | |
814 | else | |
815 | argc = 1; | |
ce70f433 | 816 | |
817 | #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50 | |
818 | #define ARG_TOKENS_EXTENT 1000 | |
819 | ||
820 | buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG | |
821 | * sizeof (cpp_token *) | |
06c92cbc | 822 | + sizeof (macro_arg))); |
823 | base_buff = buff; | |
824 | args = (macro_arg *) buff->base; | |
825 | memset (args, 0, argc * sizeof (macro_arg)); | |
1fdf6039 | 826 | buff->cur = (unsigned char *) &args[argc]; |
06c92cbc | 827 | arg = args, argc = 0; |
828 | ||
829 | /* Collect the tokens making up each argument. We don't yet know | |
830 | how many arguments have been supplied, whether too many or too | |
831 | few. Hence the slightly bizarre usage of "argc" and "arg". */ | |
832 | do | |
79bd622b | 833 | { |
06c92cbc | 834 | unsigned int paren_depth = 0; |
835 | unsigned int ntokens = 0; | |
ce70f433 | 836 | unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; |
837 | num_args_alloced++; | |
79bd622b | 838 | |
06c92cbc | 839 | argc++; |
840 | arg->first = (const cpp_token **) buff->cur; | |
ce70f433 | 841 | if (track_macro_expansion_p) |
842 | { | |
843 | virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; | |
844 | arg->virt_locs = XNEWVEC (source_location, | |
845 | virt_locs_capacity); | |
846 | } | |
69461e0d | 847 | |
06c92cbc | 848 | for (;;) |
849 | { | |
850 | /* Require space for 2 new tokens (including a CPP_EOF). */ | |
1fdf6039 | 851 | if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit) |
06c92cbc | 852 | { |
e6a5f963 | 853 | buff = _cpp_append_extend_buff (pfile, buff, |
ce70f433 | 854 | ARG_TOKENS_EXTENT |
855 | * sizeof (cpp_token *)); | |
06c92cbc | 856 | arg->first = (const cpp_token **) buff->cur; |
857 | } | |
ce70f433 | 858 | if (track_macro_expansion_p |
859 | && (ntokens + 2 > virt_locs_capacity)) | |
860 | { | |
861 | virt_locs_capacity += ARG_TOKENS_EXTENT; | |
862 | arg->virt_locs = XRESIZEVEC (source_location, | |
863 | arg->virt_locs, | |
864 | virt_locs_capacity); | |
865 | } | |
f9b5f742 | 866 | |
ce70f433 | 867 | token = cpp_get_token_1 (pfile, &virt_loc); |
69461e0d | 868 | |
06c92cbc | 869 | if (token->type == CPP_PADDING) |
870 | { | |
871 | /* Drop leading padding. */ | |
872 | if (ntokens == 0) | |
873 | continue; | |
874 | } | |
875 | else if (token->type == CPP_OPEN_PAREN) | |
876 | paren_depth++; | |
877 | else if (token->type == CPP_CLOSE_PAREN) | |
878 | { | |
879 | if (paren_depth-- == 0) | |
880 | break; | |
881 | } | |
882 | else if (token->type == CPP_COMMA) | |
883 | { | |
884 | /* A comma does not terminate an argument within | |
885 | parentheses or as part of a variable argument. */ | |
886 | if (paren_depth == 0 | |
887 | && ! (macro->variadic && argc == macro->paramc)) | |
888 | break; | |
889 | } | |
890 | else if (token->type == CPP_EOF | |
891 | || (token->type == CPP_HASH && token->flags & BOL)) | |
892 | break; | |
45f9f140 | 893 | else if (token->type == CPP_PRAGMA) |
894 | { | |
895 | cpp_token *newtok = _cpp_temp_token (pfile); | |
896 | ||
897 | /* CPP_PRAGMA token lives in directive_result, which will | |
898 | be overwritten on the next directive. */ | |
899 | *newtok = *token; | |
900 | token = newtok; | |
901 | do | |
902 | { | |
903 | if (*pragma_buff == NULL | |
904 | || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *)) | |
905 | { | |
906 | _cpp_buff *next; | |
907 | if (*pragma_buff == NULL) | |
908 | *pragma_buff | |
909 | = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *)); | |
910 | else | |
911 | { | |
912 | next = *pragma_buff; | |
913 | *pragma_buff | |
914 | = _cpp_get_buff (pfile, | |
915 | (BUFF_FRONT (*pragma_buff) | |
916 | - (*pragma_buff)->base) * 2); | |
917 | (*pragma_buff)->next = next; | |
918 | } | |
919 | } | |
920 | *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token; | |
921 | BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *); | |
922 | if (token->type == CPP_PRAGMA_EOL) | |
923 | break; | |
ce70f433 | 924 | token = cpp_get_token_1 (pfile, &virt_loc); |
45f9f140 | 925 | } |
926 | while (token->type != CPP_EOF); | |
927 | ||
928 | /* In deferred pragmas parsing_args and prevent_expansion | |
929 | had been changed, reset it. */ | |
930 | pfile->state.parsing_args = 2; | |
931 | pfile->state.prevent_expansion = 1; | |
932 | ||
933 | if (token->type == CPP_EOF) | |
934 | break; | |
935 | else | |
936 | continue; | |
937 | } | |
ce70f433 | 938 | set_arg_token (arg, token, virt_loc, |
939 | ntokens, MACRO_ARG_TOKEN_NORMAL, | |
940 | CPP_OPTION (pfile, track_macro_expansion)); | |
941 | ntokens++; | |
06c92cbc | 942 | } |
79bd622b | 943 | |
06c92cbc | 944 | /* Drop trailing padding. */ |
945 | while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING) | |
946 | ntokens--; | |
79bd622b | 947 | |
06c92cbc | 948 | arg->count = ntokens; |
ce70f433 | 949 | set_arg_token (arg, &pfile->eof, pfile->eof.src_loc, |
950 | ntokens, MACRO_ARG_TOKEN_NORMAL, | |
951 | CPP_OPTION (pfile, track_macro_expansion)); | |
79bd622b | 952 | |
06c92cbc | 953 | /* Terminate the argument. Excess arguments loop back and |
954 | overwrite the final legitimate argument, before failing. */ | |
955 | if (argc <= macro->paramc) | |
956 | { | |
1fdf6039 | 957 | buff->cur = (unsigned char *) &arg->first[ntokens + 1]; |
06c92cbc | 958 | if (argc != macro->paramc) |
959 | arg++; | |
960 | } | |
79bd622b | 961 | } |
d6af0368 | 962 | while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF); |
79bd622b | 963 | |
d6af0368 | 964 | if (token->type == CPP_EOF) |
79bd622b | 965 | { |
1fdf6039 | 966 | /* We still need the CPP_EOF to end directives, and to end |
967 | pre-expansion of a macro argument. Step back is not | |
968 | unconditional, since we don't want to return a CPP_EOF to our | |
969 | callers at the end of an -include-d file. */ | |
d6af0368 | 970 | if (pfile->context->prev || pfile->state.in_directive) |
06c92cbc | 971 | _cpp_backup_tokens (pfile, 1); |
d80d2074 | 972 | cpp_error (pfile, CPP_DL_ERROR, |
73328dce | 973 | "unterminated argument list invoking macro \"%s\"", |
c86dbc5b | 974 | NODE_NAME (node)); |
79bd622b | 975 | } |
06025647 | 976 | else |
79bd622b | 977 | { |
06025647 | 978 | /* A single empty argument is counted as no argument. */ |
979 | if (argc == 1 && macro->paramc == 0 && args[0].count == 0) | |
980 | argc = 0; | |
981 | if (_cpp_arguments_ok (pfile, macro, node, argc)) | |
ed909a09 | 982 | { |
983 | /* GCC has special semantics for , ## b where b is a varargs | |
984 | parameter: we remove the comma if b was omitted entirely. | |
985 | If b was merely an empty argument, the comma is retained. | |
986 | If the macro takes just one (varargs) parameter, then we | |
987 | retain the comma only if we are standards conforming. | |
988 | ||
989 | If FIRST is NULL replace_args () swallows the comma. */ | |
990 | if (macro->variadic && (argc < macro->paramc | |
991 | || (argc == 1 && args[0].count == 0 | |
992 | && !CPP_OPTION (pfile, std)))) | |
993 | args[macro->paramc - 1].first = NULL; | |
ce70f433 | 994 | if (num_args) |
995 | *num_args = num_args_alloced; | |
ed909a09 | 996 | return base_buff; |
997 | } | |
79bd622b | 998 | } |
999 | ||
06025647 | 1000 | /* An error occurred. */ |
06c92cbc | 1001 | _cpp_release_buff (pfile, base_buff); |
1002 | return NULL; | |
79bd622b | 1003 | } |
1004 | ||
2b0e25fe | 1005 | /* Search for an opening parenthesis to the macro of NODE, in such a |
1006 | way that, if none is found, we don't lose the information in any | |
1007 | intervening padding tokens. If we find the parenthesis, collect | |
45f9f140 | 1008 | the arguments and return the buffer containing them. PRAGMA_BUFF |
ce70f433 | 1009 | argument is the same as in collect_args. If NUM_ARGS is non-NULL, |
1010 | *NUM_ARGS is set to the number of arguments contained in the | |
1011 | returned buffer. */ | |
2b0e25fe | 1012 | static _cpp_buff * |
45f9f140 | 1013 | funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node, |
ce70f433 | 1014 | _cpp_buff **pragma_buff, unsigned *num_args) |
79bd622b | 1015 | { |
2b0e25fe | 1016 | const cpp_token *token, *padding = NULL; |
f9b5f742 | 1017 | |
2b0e25fe | 1018 | for (;;) |
fb5ab82c | 1019 | { |
2b0e25fe | 1020 | token = cpp_get_token (pfile); |
1021 | if (token->type != CPP_PADDING) | |
1022 | break; | |
1023 | if (padding == NULL | |
1024 | || (!(padding->flags & PREV_WHITE) && token->val.source == NULL)) | |
1025 | padding = token; | |
fb5ab82c | 1026 | } |
79bd622b | 1027 | |
2b0e25fe | 1028 | if (token->type == CPP_OPEN_PAREN) |
79bd622b | 1029 | { |
2b0e25fe | 1030 | pfile->state.parsing_args = 2; |
ce70f433 | 1031 | return collect_args (pfile, node, pragma_buff, num_args); |
79bd622b | 1032 | } |
1033 | ||
7e2fc40e | 1034 | /* CPP_EOF can be the end of macro arguments, or the end of the |
1035 | file. We mustn't back up over the latter. Ugh. */ | |
1036 | if (token->type != CPP_EOF || token == &pfile->eof) | |
1037 | { | |
1038 | /* Back up. We may have skipped padding, in which case backing | |
1039 | up more than one token when expanding macros is in general | |
1040 | too difficult. We re-insert it in its own context. */ | |
1041 | _cpp_backup_tokens (pfile, 1); | |
1042 | if (padding) | |
b75b98aa | 1043 | _cpp_push_token_context (pfile, NULL, padding, 1); |
7e2fc40e | 1044 | } |
2b0e25fe | 1045 | |
1046 | return NULL; | |
79bd622b | 1047 | } |
1048 | ||
941f2388 | 1049 | /* Return the real number of tokens in the expansion of MACRO. */ |
1050 | static inline unsigned int | |
1051 | macro_real_token_count (const cpp_macro *macro) | |
1052 | { | |
1053 | unsigned int i; | |
1054 | if (__builtin_expect (!macro->extra_tokens, true)) | |
1055 | return macro->count; | |
1056 | for (i = 0; i < macro->count; i++) | |
1057 | if (macro->exp.tokens[i].type == CPP_PASTE) | |
1058 | return i; | |
1059 | abort (); | |
1060 | } | |
1061 | ||
88cf66fa | 1062 | /* Push the context of a macro with hash entry NODE onto the context |
1063 | stack. If we can successfully expand the macro, we push a context | |
1064 | containing its yet-to-be-rescanned replacement list and return one. | |
ce70f433 | 1065 | If there were additionally any unexpanded deferred #pragma |
1066 | directives among macro arguments, push another context containing | |
1067 | the pragma tokens before the yet-to-be-rescanned replacement list | |
1068 | and return two. Otherwise, we don't push a context and return | |
1069 | zero. LOCATION is the location of the expansion point of the | |
1070 | macro. */ | |
69461e0d | 1071 | static int |
45f9f140 | 1072 | enter_macro_context (cpp_reader *pfile, cpp_hashnode *node, |
ce70f433 | 1073 | const cpp_token *result, source_location location) |
69461e0d | 1074 | { |
88cf66fa | 1075 | /* The presence of a macro invalidates a file's controlling macro. */ |
c7e5d924 | 1076 | pfile->mi_valid = false; |
1077 | ||
606942e3 | 1078 | pfile->state.angled_headers = false; |
1079 | ||
8c6425eb | 1080 | /* From here to when we push the context for the macro later down |
1081 | this function, we need to flag the fact that we are about to | |
1082 | expand a macro. This is useful when -ftrack-macro-expansion is | |
1083 | turned off. In that case, we need to record the location of the | |
1084 | expansion point of the top-most macro we are about to to expand, | |
1085 | into pfile->invocation_location. But we must not record any such | |
1086 | location once the process of expanding the macro starts; that is, | |
1087 | we must not do that recording between now and later down this | |
1088 | function where set this flag to FALSE. */ | |
1089 | pfile->about_to_expand_macro_p = true; | |
1090 | ||
34c3de48 | 1091 | if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED)) |
1092 | { | |
1093 | node->flags |= NODE_USED; | |
25692381 | 1094 | if ((!pfile->cb.user_builtin_macro |
1095 | || !pfile->cb.user_builtin_macro (pfile, node)) | |
1096 | && pfile->cb.used_define) | |
34c3de48 | 1097 | pfile->cb.used_define (pfile, pfile->directive_line, node); |
1098 | } | |
1099 | ||
88cf66fa | 1100 | /* Handle standard macros. */ |
c7e5d924 | 1101 | if (! (node->flags & NODE_BUILTIN)) |
69461e0d | 1102 | { |
f9b5f742 | 1103 | cpp_macro *macro = node->value.macro; |
45f9f140 | 1104 | _cpp_buff *pragma_buff = NULL; |
3c7df4d3 | 1105 | |
2b0e25fe | 1106 | if (macro->fun_like) |
1107 | { | |
1108 | _cpp_buff *buff; | |
ce70f433 | 1109 | unsigned num_args = 0; |
2b0e25fe | 1110 | |
1111 | pfile->state.prevent_expansion++; | |
1112 | pfile->keep_tokens++; | |
1113 | pfile->state.parsing_args = 1; | |
ce70f433 | 1114 | buff = funlike_invocation_p (pfile, node, &pragma_buff, |
1115 | &num_args); | |
2b0e25fe | 1116 | pfile->state.parsing_args = 0; |
1117 | pfile->keep_tokens--; | |
1118 | pfile->state.prevent_expansion--; | |
1119 | ||
1120 | if (buff == NULL) | |
1121 | { | |
1122 | if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr) | |
3a79f5da | 1123 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
2b0e25fe | 1124 | "function-like macro \"%s\" must be used with arguments in traditional C", |
3a79f5da | 1125 | NODE_NAME (node)); |
2b0e25fe | 1126 | |
45f9f140 | 1127 | if (pragma_buff) |
1128 | _cpp_release_buff (pfile, pragma_buff); | |
1129 | ||
8c6425eb | 1130 | pfile->about_to_expand_macro_p = false; |
2b0e25fe | 1131 | return 0; |
1132 | } | |
1133 | ||
d6af0368 | 1134 | if (macro->paramc > 0) |
ce70f433 | 1135 | replace_args (pfile, node, macro, |
1136 | (macro_arg *) buff->base, | |
1137 | location); | |
1138 | /* Free the memory used by the arguments of this | |
1139 | function-like macro. This memory has been allocated by | |
1140 | funlike_invocation_p and by replace_args. */ | |
1141 | delete_macro_args (buff, num_args); | |
2b0e25fe | 1142 | } |
79bd622b | 1143 | |
f9b5f742 | 1144 | /* Disable the macro within its expansion. */ |
c7e5d924 | 1145 | node->flags |= NODE_DISABLED; |
79bd622b | 1146 | |
34c3de48 | 1147 | if (!(node->flags & NODE_USED)) |
1148 | { | |
1149 | node->flags |= NODE_USED; | |
1150 | if (pfile->cb.used_define) | |
1151 | pfile->cb.used_define (pfile, pfile->directive_line, node); | |
1152 | } | |
1153 | ||
116f0a5f | 1154 | if (pfile->cb.used) |
ce70f433 | 1155 | pfile->cb.used (pfile, location, node); |
116f0a5f | 1156 | |
71a7c282 | 1157 | macro->used = 1; |
1158 | ||
f9b5f742 | 1159 | if (macro->paramc == 0) |
ce70f433 | 1160 | { |
9b48364f | 1161 | unsigned tokens_count = macro_real_token_count (macro); |
ce70f433 | 1162 | if (CPP_OPTION (pfile, track_macro_expansion)) |
1163 | { | |
9b48364f | 1164 | unsigned int i; |
ce70f433 | 1165 | const cpp_token *src = macro->exp.tokens; |
1166 | const struct line_map *map; | |
1167 | source_location *virt_locs = NULL; | |
9b48364f | 1168 | _cpp_buff *macro_tokens |
1169 | = tokens_buff_new (pfile, tokens_count, &virt_locs); | |
ce70f433 | 1170 | |
1171 | /* Create a macro map to record the locations of the | |
1172 | tokens that are involved in the expansion. LOCATION | |
1173 | is the location of the macro expansion point. */ | |
9b48364f | 1174 | map = linemap_enter_macro (pfile->line_table, |
1175 | node, location, tokens_count); | |
1176 | for (i = 0; i < tokens_count; ++i) | |
ce70f433 | 1177 | { |
1178 | tokens_buff_add_token (macro_tokens, virt_locs, | |
1179 | src, src->src_loc, | |
1180 | src->src_loc, map, i); | |
1181 | ++src; | |
1182 | } | |
1183 | push_extended_tokens_context (pfile, node, | |
1184 | macro_tokens, | |
1185 | virt_locs, | |
1186 | (const cpp_token **) | |
1187 | macro_tokens->base, | |
9b48364f | 1188 | tokens_count); |
ce70f433 | 1189 | } |
1190 | else | |
9b48364f | 1191 | _cpp_push_token_context (pfile, node, macro->exp.tokens, |
1192 | tokens_count); | |
1193 | num_macro_tokens_counter += tokens_count; | |
ce70f433 | 1194 | } |
c7e5d924 | 1195 | |
45f9f140 | 1196 | if (pragma_buff) |
1197 | { | |
1198 | if (!pfile->state.in_directive) | |
1199 | _cpp_push_token_context (pfile, NULL, | |
1200 | padding_token (pfile, result), 1); | |
1201 | do | |
1202 | { | |
e77b8253 | 1203 | unsigned tokens_count; |
45f9f140 | 1204 | _cpp_buff *tail = pragma_buff->next; |
1205 | pragma_buff->next = NULL; | |
e77b8253 | 1206 | tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff) |
1207 | - (const cpp_token **) pragma_buff->base); | |
45f9f140 | 1208 | push_ptoken_context (pfile, NULL, pragma_buff, |
1209 | (const cpp_token **) pragma_buff->base, | |
e77b8253 | 1210 | tokens_count); |
45f9f140 | 1211 | pragma_buff = tail; |
e77b8253 | 1212 | if (!CPP_OPTION (pfile, track_macro_expansion)) |
1213 | num_macro_tokens_counter += tokens_count; | |
1214 | ||
45f9f140 | 1215 | } |
1216 | while (pragma_buff != NULL); | |
8c6425eb | 1217 | pfile->about_to_expand_macro_p = false; |
45f9f140 | 1218 | return 2; |
1219 | } | |
1220 | ||
8c6425eb | 1221 | pfile->about_to_expand_macro_p = false; |
c7e5d924 | 1222 | return 1; |
69461e0d | 1223 | } |
c7e5d924 | 1224 | |
8c6425eb | 1225 | pfile->about_to_expand_macro_p = false; |
88cf66fa | 1226 | /* Handle built-in macros and the _Pragma operator. */ |
2e82cf2c | 1227 | return builtin_macro (pfile, node, location); |
79bd622b | 1228 | } |
1229 | ||
ce70f433 | 1230 | /* De-allocate the memory used by BUFF which is an array of instances |
1231 | of macro_arg. NUM_ARGS is the number of instances of macro_arg | |
1232 | present in BUFF. */ | |
1233 | static void | |
1234 | delete_macro_args (_cpp_buff *buff, unsigned num_args) | |
1235 | { | |
1236 | macro_arg *macro_args; | |
1237 | unsigned i; | |
1238 | ||
1239 | if (buff == NULL) | |
1240 | return; | |
1241 | ||
1242 | macro_args = (macro_arg *) buff->base; | |
1243 | ||
1244 | /* Walk instances of macro_arg to free their expanded tokens as well | |
1245 | as their macro_arg::virt_locs members. */ | |
1246 | for (i = 0; i < num_args; ++i) | |
1247 | { | |
1248 | if (macro_args[i].expanded) | |
1249 | { | |
1250 | free (macro_args[i].expanded); | |
1251 | macro_args[i].expanded = NULL; | |
1252 | } | |
1253 | if (macro_args[i].virt_locs) | |
1254 | { | |
1255 | free (macro_args[i].virt_locs); | |
1256 | macro_args[i].virt_locs = NULL; | |
1257 | } | |
1258 | if (macro_args[i].expanded_virt_locs) | |
1259 | { | |
1260 | free (macro_args[i].expanded_virt_locs); | |
1261 | macro_args[i].expanded_virt_locs = NULL; | |
1262 | } | |
1263 | } | |
1264 | _cpp_free_buff (buff); | |
1265 | } | |
1266 | ||
1267 | /* Set the INDEXth token of the macro argument ARG. TOKEN is the token | |
1268 | to set, LOCATION is its virtual location. "Virtual" location means | |
e8aa7eaf | 1269 | the location that encodes loci across macro expansion. Otherwise |
ce70f433 | 1270 | it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the |
1271 | argument ARG is supposed to contain. Note that ARG must be | |
1272 | tailored so that it has enough room to contain INDEX + 1 numbers of | |
1273 | tokens, at least. */ | |
1274 | static void | |
1275 | set_arg_token (macro_arg *arg, const cpp_token *token, | |
1276 | source_location location, size_t index, | |
1277 | enum macro_arg_token_kind kind, | |
1278 | bool track_macro_exp_p) | |
1279 | { | |
1280 | const cpp_token **token_ptr; | |
1281 | source_location *loc = NULL; | |
1282 | ||
1283 | token_ptr = | |
1284 | arg_token_ptr_at (arg, index, kind, | |
1285 | track_macro_exp_p ? &loc : NULL); | |
1286 | *token_ptr = token; | |
1287 | ||
1288 | if (loc != NULL) | |
1289 | { | |
1290 | #ifdef ENABLE_CHECKING | |
1291 | if (kind == MACRO_ARG_TOKEN_STRINGIFIED | |
1292 | || !track_macro_exp_p) | |
1293 | /* We can't set the location of a stringified argument | |
1294 | token and we can't set any location if we aren't tracking | |
1295 | macro expansion locations. */ | |
1296 | abort (); | |
1297 | #endif | |
1298 | *loc = location; | |
1299 | } | |
1300 | } | |
1301 | ||
1302 | /* Get the pointer to the location of the argument token of the | |
1303 | function-like macro argument ARG. This function must be called | |
1304 | only when we -ftrack-macro-expansion is on. */ | |
1305 | static const source_location * | |
1306 | get_arg_token_location (const macro_arg *arg, | |
1307 | enum macro_arg_token_kind kind) | |
1308 | { | |
1309 | const source_location *loc = NULL; | |
1310 | const cpp_token **token_ptr = | |
1311 | arg_token_ptr_at (arg, 0, kind, (source_location **) &loc); | |
1312 | ||
1313 | if (token_ptr == NULL) | |
1314 | return NULL; | |
1315 | ||
1316 | return loc; | |
1317 | } | |
1318 | ||
1319 | /* Return the pointer to the INDEXth token of the macro argument ARG. | |
1320 | KIND specifies the kind of token the macro argument ARG contains. | |
1321 | If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address | |
1322 | of the virtual location of the returned token if the | |
1323 | -ftrack-macro-expansion flag is on; otherwise, it's set to the | |
1324 | spelling location of the returned token. */ | |
1325 | static const cpp_token ** | |
1326 | arg_token_ptr_at (const macro_arg *arg, size_t index, | |
1327 | enum macro_arg_token_kind kind, | |
1328 | source_location **virt_location) | |
1329 | { | |
1330 | const cpp_token **tokens_ptr = NULL; | |
1331 | ||
1332 | switch (kind) | |
1333 | { | |
1334 | case MACRO_ARG_TOKEN_NORMAL: | |
1335 | tokens_ptr = arg->first; | |
1336 | break; | |
1337 | case MACRO_ARG_TOKEN_STRINGIFIED: | |
1338 | tokens_ptr = (const cpp_token **) &arg->stringified; | |
1339 | break; | |
1340 | case MACRO_ARG_TOKEN_EXPANDED: | |
1341 | tokens_ptr = arg->expanded; | |
1342 | break; | |
1343 | } | |
1344 | ||
1345 | if (tokens_ptr == NULL) | |
1346 | /* This can happen for e.g, an empty token argument to a | |
1347 | funtion-like macro. */ | |
1348 | return tokens_ptr; | |
1349 | ||
1350 | if (virt_location) | |
1351 | { | |
1352 | if (kind == MACRO_ARG_TOKEN_NORMAL) | |
1353 | *virt_location = &arg->virt_locs[index]; | |
1354 | else if (kind == MACRO_ARG_TOKEN_EXPANDED) | |
1355 | *virt_location = &arg->expanded_virt_locs[index]; | |
1356 | else if (kind == MACRO_ARG_TOKEN_STRINGIFIED) | |
1357 | *virt_location = | |
1358 | (source_location *) &tokens_ptr[index]->src_loc; | |
1359 | } | |
1360 | return &tokens_ptr[index]; | |
1361 | } | |
1362 | ||
1363 | /* Initialize an iterator so that it iterates over the tokens of a | |
1364 | function-like macro argument. KIND is the kind of tokens we want | |
1365 | ITER to iterate over. TOKEN_PTR points the first token ITER will | |
1366 | iterate over. */ | |
1367 | static void | |
1368 | macro_arg_token_iter_init (macro_arg_token_iter *iter, | |
1369 | bool track_macro_exp_p, | |
1370 | enum macro_arg_token_kind kind, | |
1371 | const macro_arg *arg, | |
1372 | const cpp_token **token_ptr) | |
1373 | { | |
1374 | iter->track_macro_exp_p = track_macro_exp_p; | |
1375 | iter->kind = kind; | |
1376 | iter->token_ptr = token_ptr; | |
2a688977 | 1377 | /* Unconditionally initialize this so that the compiler doesn't warn |
1378 | about iter->location_ptr being possibly uninitialized later after | |
1379 | this code has been inlined somewhere. */ | |
1380 | iter->location_ptr = NULL; | |
ce70f433 | 1381 | if (track_macro_exp_p) |
1382 | iter->location_ptr = get_arg_token_location (arg, kind); | |
1383 | #ifdef ENABLE_CHECKING | |
1384 | iter->num_forwards = 0; | |
1385 | if (track_macro_exp_p | |
1386 | && token_ptr != NULL | |
1387 | && iter->location_ptr == NULL) | |
1388 | abort (); | |
1389 | #endif | |
1390 | } | |
1391 | ||
1392 | /* Move the iterator one token forward. Note that if IT was | |
1393 | initialized on an argument that has a stringified token, moving it | |
e8aa7eaf | 1394 | forward doesn't make sense as a stringified token is essentially one |
ce70f433 | 1395 | string. */ |
1396 | static void | |
1397 | macro_arg_token_iter_forward (macro_arg_token_iter *it) | |
1398 | { | |
1399 | switch (it->kind) | |
1400 | { | |
1401 | case MACRO_ARG_TOKEN_NORMAL: | |
1402 | case MACRO_ARG_TOKEN_EXPANDED: | |
1403 | it->token_ptr++; | |
1404 | if (it->track_macro_exp_p) | |
1405 | it->location_ptr++; | |
1406 | break; | |
1407 | case MACRO_ARG_TOKEN_STRINGIFIED: | |
1408 | #ifdef ENABLE_CHECKING | |
1409 | if (it->num_forwards > 0) | |
1410 | abort (); | |
1411 | #endif | |
1412 | break; | |
1413 | } | |
1414 | ||
1415 | #ifdef ENABLE_CHECKING | |
1416 | it->num_forwards++; | |
1417 | #endif | |
1418 | } | |
1419 | ||
1420 | /* Return the token pointed to by the iterator. */ | |
1421 | static const cpp_token * | |
1422 | macro_arg_token_iter_get_token (const macro_arg_token_iter *it) | |
1423 | { | |
1424 | #ifdef ENABLE_CHECKING | |
1425 | if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED | |
1426 | && it->num_forwards > 0) | |
1427 | abort (); | |
1428 | #endif | |
1429 | if (it->token_ptr == NULL) | |
1430 | return NULL; | |
1431 | return *it->token_ptr; | |
1432 | } | |
1433 | ||
1434 | /* Return the location of the token pointed to by the iterator.*/ | |
1435 | static source_location | |
1436 | macro_arg_token_iter_get_location (const macro_arg_token_iter *it) | |
1437 | { | |
1438 | #ifdef ENABLE_CHECKING | |
1439 | if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED | |
1440 | && it->num_forwards > 0) | |
1441 | abort (); | |
1442 | #endif | |
1443 | if (it->track_macro_exp_p) | |
1444 | return *it->location_ptr; | |
1445 | else | |
1446 | return (*it->token_ptr)->src_loc; | |
1447 | } | |
1448 | ||
1449 | /* Return the index of a token [resulting from macro expansion] inside | |
1450 | the total list of tokens resulting from a given macro | |
1451 | expansion. The index can be different depending on whether if we | |
1452 | want each tokens resulting from function-like macro arguments | |
1453 | expansion to have a different location or not. | |
1454 | ||
1455 | E.g, consider this function-like macro: | |
1456 | ||
1457 | #define M(x) x - 3 | |
1458 | ||
1459 | Then consider us "calling" it (and thus expanding it) like: | |
1460 | ||
1461 | M(1+4) | |
1462 | ||
1463 | It will be expanded into: | |
1464 | ||
1465 | 1+4-3 | |
1466 | ||
1467 | Let's consider the case of the token '4'. | |
1468 | ||
1469 | Its index can be 2 (it's the third token of the set of tokens | |
1470 | resulting from the expansion) or it can be 0 if we consider that | |
1471 | all tokens resulting from the expansion of the argument "1+2" have | |
1472 | the same index, which is 0. In this later case, the index of token | |
1473 | '-' would then be 1 and the index of token '3' would be 2. | |
1474 | ||
1475 | The later case is useful to use less memory e.g, for the case of | |
1476 | the user using the option -ftrack-macro-expansion=1. | |
1477 | ||
1478 | ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we | |
1479 | are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro | |
1480 | parameter (inside the macro replacement list) that corresponds to | |
1481 | the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index | |
1482 | of. | |
1483 | ||
1484 | If we refer to the example above, for the '4' argument token, | |
1485 | ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN | |
1486 | would be set to the token 'x', in the replacement list "x - 3" of | |
1487 | macro M. | |
1488 | ||
1489 | This is a subroutine of replace_args. */ | |
1490 | inline static unsigned | |
1491 | expanded_token_index (cpp_reader *pfile, cpp_macro *macro, | |
1492 | const cpp_token *cur_replacement_token, | |
1493 | unsigned absolute_token_index) | |
1494 | { | |
1495 | if (CPP_OPTION (pfile, track_macro_expansion) > 1) | |
1496 | return absolute_token_index; | |
1497 | return cur_replacement_token - macro->exp.tokens; | |
1498 | } | |
1499 | ||
88cf66fa | 1500 | /* Replace the parameters in a function-like macro of NODE with the |
1501 | actual ARGS, and place the result in a newly pushed token context. | |
1502 | Expand each argument before replacing, unless it is operated upon | |
ce70f433 | 1503 | by the # or ## operators. EXPANSION_POINT_LOC is the location of |
1504 | the expansion point of the macro. E.g, the location of the | |
1505 | function-like macro invocation. */ | |
79bd622b | 1506 | static void |
ce70f433 | 1507 | replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, |
1508 | macro_arg *args, source_location expansion_point_loc) | |
79bd622b | 1509 | { |
1510 | unsigned int i, total; | |
1511 | const cpp_token *src, *limit; | |
ce70f433 | 1512 | const cpp_token **first = NULL; |
79bd622b | 1513 | macro_arg *arg; |
ce70f433 | 1514 | _cpp_buff *buff = NULL; |
1515 | source_location *virt_locs = NULL; | |
1516 | unsigned int exp_count; | |
1517 | const struct line_map *map = NULL; | |
1518 | int track_macro_exp; | |
79bd622b | 1519 | |
79bd622b | 1520 | /* First, fully macro-expand arguments, calculating the number of |
084163dc | 1521 | tokens in the final expansion as we go. The ordering of the if |
1522 | statements below is subtle; we must handle stringification before | |
1523 | pasting. */ | |
ce70f433 | 1524 | |
1525 | /* EXP_COUNT is the number of tokens in the macro replacement | |
1526 | list. TOTAL is the number of tokens /after/ macro parameters | |
1527 | have been replaced by their arguments. */ | |
1528 | exp_count = macro_real_token_count (macro); | |
1529 | total = exp_count; | |
1530 | limit = macro->exp.tokens + exp_count; | |
f9b5f742 | 1531 | |
672f38da | 1532 | for (src = macro->exp.tokens; src < limit; src++) |
79bd622b | 1533 | if (src->type == CPP_MACRO_ARG) |
1534 | { | |
f9b5f742 | 1535 | /* Leading and trailing padding tokens. */ |
1536 | total += 2; | |
ce70f433 | 1537 | /* Account for leading and padding tokens in exp_count too. |
1538 | This is going to be important later down this function, | |
1539 | when we want to handle the case of (track_macro_exp < | |
1540 | 2). */ | |
1541 | exp_count += 2; | |
f9b5f742 | 1542 | |
79bd622b | 1543 | /* We have an argument. If it is not being stringified or |
1544 | pasted it is macro-replaced before insertion. */ | |
2ee04baa | 1545 | arg = &args[src->val.macro_arg.arg_no - 1]; |
3c7df4d3 | 1546 | |
79bd622b | 1547 | if (src->flags & STRINGIFY_ARG) |
1548 | { | |
1549 | if (!arg->stringified) | |
f9b5f742 | 1550 | arg->stringified = stringify_arg (pfile, arg); |
79bd622b | 1551 | } |
1552 | else if ((src->flags & PASTE_LEFT) | |
672f38da | 1553 | || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT))) |
79bd622b | 1554 | total += arg->count - 1; |
1555 | else | |
1556 | { | |
1557 | if (!arg->expanded) | |
f9b5f742 | 1558 | expand_arg (pfile, arg); |
79bd622b | 1559 | total += arg->expanded_count - 1; |
1560 | } | |
1561 | } | |
1562 | ||
ce70f433 | 1563 | /* When the compiler is called with the -ftrack-macro-expansion |
1564 | flag, we need to keep track of the location of each token that | |
1565 | results from macro expansion. | |
1566 | ||
1567 | A token resulting from macro expansion is not a new token. It is | |
1568 | simply the same token as the token coming from the macro | |
1569 | definition. The new things that are allocated are the buffer | |
1570 | that holds the tokens resulting from macro expansion and a new | |
1571 | location that records many things like the locus of the expansion | |
1572 | point as well as the original locus inside the definition of the | |
1573 | macro. This location is called a virtual location. | |
1574 | ||
1575 | So the buffer BUFF holds a set of cpp_token*, and the buffer | |
1576 | VIRT_LOCS holds the virtual locations of the tokens held by BUFF. | |
1577 | ||
1578 | Both of these two buffers are going to be hung off of the macro | |
1579 | context, when the latter is pushed. The memory allocated to | |
1580 | store the tokens and their locations is going to be freed once | |
1581 | the context of macro expansion is popped. | |
1582 | ||
1583 | As far as tokens are concerned, the memory overhead of | |
1584 | -ftrack-macro-expansion is proportional to the number of | |
1585 | macros that get expanded multiplied by sizeof (source_location). | |
1586 | The good news is that extra memory gets freed when the macro | |
1587 | context is freed, i.e shortly after the macro got expanded. */ | |
1588 | ||
1589 | /* Is the -ftrack-macro-expansion flag in effect? */ | |
1590 | track_macro_exp = CPP_OPTION (pfile, track_macro_expansion); | |
1591 | ||
1592 | /* Now allocate memory space for tokens and locations resulting from | |
1593 | the macro expansion, copy the tokens and replace the arguments. | |
1594 | This memory must be freed when the context of the macro MACRO is | |
1595 | popped. */ | |
1596 | buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL); | |
1597 | ||
084163dc | 1598 | first = (const cpp_token **) buff->base; |
79bd622b | 1599 | |
ce70f433 | 1600 | /* Create a macro map to record the locations of the tokens that are |
1601 | involved in the expansion. Note that the expansion point is set | |
1602 | to the location of the closing parenthesis. Otherwise, the | |
1603 | subsequent map created for the first token that comes after the | |
1604 | macro map might have a wrong line number. That would lead to | |
1605 | tokens with wrong line numbers after the macro expansion. This | |
1606 | adds up to the memory overhead of the -ftrack-macro-expansion | |
1607 | flag; for every macro that is expanded, a "macro map" is | |
1608 | created. */ | |
1609 | if (track_macro_exp) | |
1610 | { | |
1611 | int num_macro_tokens = total; | |
1612 | if (track_macro_exp < 2) | |
1613 | /* Then the number of macro tokens won't take in account the | |
1614 | fact that function-like macro arguments can expand to | |
1615 | multiple tokens. This is to save memory at the expense of | |
1616 | accuracy. | |
1617 | ||
1618 | Suppose we have #define SQARE(A) A * A | |
1619 | ||
1620 | And then we do SQARE(2+3) | |
1621 | ||
1622 | Then the tokens 2, +, 3, will have the same location, | |
1623 | saying they come from the expansion of the argument A. */ | |
1624 | num_macro_tokens = exp_count; | |
1625 | map = linemap_enter_macro (pfile->line_table, node, | |
1626 | expansion_point_loc, | |
1627 | num_macro_tokens); | |
1628 | } | |
1629 | i = 0; | |
672f38da | 1630 | for (src = macro->exp.tokens; src < limit; src++) |
f9b5f742 | 1631 | { |
ce70f433 | 1632 | unsigned int arg_tokens_count; |
1633 | macro_arg_token_iter from; | |
1634 | const cpp_token **paste_flag = NULL; | |
1635 | const cpp_token **tmp_token_ptr; | |
79bd622b | 1636 | |
f9b5f742 | 1637 | if (src->type != CPP_MACRO_ARG) |
1638 | { | |
ce70f433 | 1639 | /* Allocate a virtual location for token SRC, and add that |
1640 | token and its virtual location into the buffers BUFF and | |
1641 | VIRT_LOCS. */ | |
1642 | unsigned index = expanded_token_index (pfile, macro, src, i); | |
1643 | tokens_buff_add_token (buff, virt_locs, src, | |
1644 | src->src_loc, src->src_loc, | |
1645 | map, index); | |
1646 | i += 1; | |
f9b5f742 | 1647 | continue; |
1648 | } | |
79bd622b | 1649 | |
f9b5f742 | 1650 | paste_flag = 0; |
2ee04baa | 1651 | arg = &args[src->val.macro_arg.arg_no - 1]; |
ce70f433 | 1652 | /* SRC is a macro parameter that we need to replace with its |
1653 | corresponding argument. So at some point we'll need to | |
1654 | iterate over the tokens of the macro argument and copy them | |
1655 | into the "place" now holding the correspondig macro | |
1656 | parameter. We are going to use the iterator type | |
1657 | macro_argo_token_iter to handle that iterating. The 'if' | |
1658 | below is to initialize the iterator depending on the type of | |
1659 | tokens the macro argument has. It also does some adjustment | |
1660 | related to padding tokens and some pasting corner cases. */ | |
f9b5f742 | 1661 | if (src->flags & STRINGIFY_ARG) |
ce70f433 | 1662 | { |
1663 | arg_tokens_count = 1; | |
1664 | macro_arg_token_iter_init (&from, | |
1665 | CPP_OPTION (pfile, | |
1666 | track_macro_expansion), | |
1667 | MACRO_ARG_TOKEN_STRINGIFIED, | |
1668 | arg, &arg->stringified); | |
1669 | } | |
f9b5f742 | 1670 | else if (src->flags & PASTE_LEFT) |
ce70f433 | 1671 | { |
1672 | arg_tokens_count = arg->count; | |
1673 | macro_arg_token_iter_init (&from, | |
1674 | CPP_OPTION (pfile, | |
1675 | track_macro_expansion), | |
1676 | MACRO_ARG_TOKEN_NORMAL, | |
1677 | arg, arg->first); | |
1678 | } | |
672f38da | 1679 | else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)) |
f9b5f742 | 1680 | { |
ce70f433 | 1681 | int num_toks; |
1682 | arg_tokens_count = arg->count; | |
1683 | macro_arg_token_iter_init (&from, | |
1684 | CPP_OPTION (pfile, | |
1685 | track_macro_expansion), | |
1686 | MACRO_ARG_TOKEN_NORMAL, | |
1687 | arg, arg->first); | |
1688 | ||
1689 | num_toks = tokens_buff_count (buff); | |
1690 | ||
1691 | if (num_toks != 0) | |
f9b5f742 | 1692 | { |
ce70f433 | 1693 | /* So the current parameter token is pasted to the previous |
1694 | token in the replacement list. Let's look at what | |
1695 | we have as previous and current arguments. */ | |
1696 | ||
1697 | /* This is the previous argument's token ... */ | |
1698 | tmp_token_ptr = tokens_buff_last_token_ptr (buff); | |
1699 | ||
1700 | if ((*tmp_token_ptr)->type == CPP_COMMA | |
f9b5f742 | 1701 | && macro->variadic |
2ee04baa | 1702 | && src->val.macro_arg.arg_no == macro->paramc) |
f9b5f742 | 1703 | { |
ce70f433 | 1704 | /* ... which is a comma; and the current parameter |
1705 | is the last parameter of a variadic function-like | |
1706 | macro. If the argument to the current last | |
1707 | parameter is NULL, then swallow the comma, | |
1708 | otherwise drop the paste flag. */ | |
1709 | if (macro_arg_token_iter_get_token (&from) == NULL) | |
1710 | tokens_buff_remove_last_token (buff); | |
f9b5f742 | 1711 | else |
ce70f433 | 1712 | paste_flag = tmp_token_ptr; |
f9b5f742 | 1713 | } |
1714 | /* Remove the paste flag if the RHS is a placemarker. */ | |
ce70f433 | 1715 | else if (arg_tokens_count == 0) |
1716 | paste_flag = tmp_token_ptr; | |
f9b5f742 | 1717 | } |
1718 | } | |
1719 | else | |
ce70f433 | 1720 | { |
1721 | arg_tokens_count = arg->expanded_count; | |
1722 | macro_arg_token_iter_init (&from, | |
1723 | CPP_OPTION (pfile, | |
1724 | track_macro_expansion), | |
1725 | MACRO_ARG_TOKEN_EXPANDED, | |
1726 | arg, arg->expanded); | |
1727 | } | |
3c7df4d3 | 1728 | |
f9b5f742 | 1729 | /* Padding on the left of an argument (unless RHS of ##). */ |
7825551f | 1730 | if ((!pfile->state.in_directive || pfile->state.directive_wants_padding) |
672f38da | 1731 | && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)) |
ce70f433 | 1732 | { |
1733 | const cpp_token *t = padding_token (pfile, src); | |
1734 | unsigned index = expanded_token_index (pfile, macro, src, i); | |
1735 | /* Allocate a virtual location for the padding token and | |
1736 | append the token and its location to BUFF and | |
1737 | VIRT_LOCS. */ | |
1738 | tokens_buff_add_token (buff, virt_locs, t, | |
1739 | t->src_loc, t->src_loc, | |
1740 | map, index); | |
1741 | } | |
79bd622b | 1742 | |
ce70f433 | 1743 | if (arg_tokens_count) |
f9b5f742 | 1744 | { |
ce70f433 | 1745 | /* So now we've got the number of tokens that make up the |
1746 | argument that is going to replace the current parameter | |
1747 | in the macro's replacement list. */ | |
1748 | unsigned int j; | |
1749 | for (j = 0; j < arg_tokens_count; ++j) | |
1750 | { | |
1751 | /* So if track_macro_exp is < 2, the user wants to | |
1752 | save extra memory while tracking macro expansion | |
1753 | locations. So in that case here is what we do: | |
1754 | ||
1755 | Suppose we have #define SQARE(A) A * A | |
1756 | ||
1757 | And then we do SQARE(2+3) | |
1758 | ||
1759 | Then the tokens 2, +, 3, will have the same location, | |
1760 | saying they come from the expansion of the argument | |
1761 | A. | |
1762 | ||
1763 | So that means we are going to ignore the COUNT tokens | |
1764 | resulting from the expansion of the current macro | |
1765 | arugment. In other words all the ARG_TOKENS_COUNT tokens | |
1766 | resulting from the expansion of the macro argument will | |
1767 | have the index I. Normally, each of those token should | |
1768 | have index I+J. */ | |
1769 | unsigned token_index = i; | |
1770 | unsigned index; | |
1771 | if (track_macro_exp > 1) | |
1772 | token_index += j; | |
1773 | ||
1774 | index = expanded_token_index (pfile, macro, src, token_index); | |
1775 | tokens_buff_add_token (buff, virt_locs, | |
1776 | macro_arg_token_iter_get_token (&from), | |
1777 | macro_arg_token_iter_get_location (&from), | |
1778 | src->src_loc, map, index); | |
1779 | macro_arg_token_iter_forward (&from); | |
1780 | } | |
79bd622b | 1781 | |
f9b5f742 | 1782 | /* With a non-empty argument on the LHS of ##, the last |
1783 | token should be flagged PASTE_LEFT. */ | |
1784 | if (src->flags & PASTE_LEFT) | |
ce70f433 | 1785 | paste_flag = |
1786 | (const cpp_token **) tokens_buff_last_token_ptr (buff); | |
f9b5f742 | 1787 | } |
04afd878 | 1788 | else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99) |
1789 | && ! macro->syshdr && ! cpp_in_system_header (pfile)) | |
403c2b12 | 1790 | { |
49f161b6 | 1791 | if (CPP_OPTION (pfile, cplusplus)) |
04afd878 | 1792 | cpp_pedwarning (pfile, CPP_W_PEDANTIC, |
1793 | "invoking macro %s argument %d: " | |
1794 | "empty macro arguments are undefined" | |
1795 | " in ISO C++98", | |
1796 | NODE_NAME (node), src->val.macro_arg.arg_no); | |
806fe15e | 1797 | else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat)) |
04afd878 | 1798 | cpp_pedwarning (pfile, |
1799 | CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 | |
1800 | ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC, | |
1801 | "invoking macro %s argument %d: " | |
1802 | "empty macro arguments are undefined" | |
1803 | " in ISO C90", | |
1804 | NODE_NAME (node), src->val.macro_arg.arg_no); | |
403c2b12 | 1805 | } |
806fe15e | 1806 | else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 |
04afd878 | 1807 | && ! CPP_OPTION (pfile, cplusplus) |
1808 | && ! macro->syshdr && ! cpp_in_system_header (pfile)) | |
1809 | cpp_warning (pfile, CPP_W_C90_C99_COMPAT, | |
1810 | "invoking macro %s argument %d: " | |
1811 | "empty macro arguments are undefined" | |
1812 | " in ISO C90", | |
1813 | NODE_NAME (node), src->val.macro_arg.arg_no); | |
efdcc728 | 1814 | |
f9b5f742 | 1815 | /* Avoid paste on RHS (even case count == 0). */ |
1816 | if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) | |
ce70f433 | 1817 | { |
1818 | const cpp_token *t = &pfile->avoid_paste; | |
1819 | tokens_buff_add_token (buff, virt_locs, | |
1820 | t, t->src_loc, t->src_loc, | |
1821 | NULL, 0); | |
1822 | } | |
79bd622b | 1823 | |
f9b5f742 | 1824 | /* Add a new paste flag, or remove an unwanted one. */ |
1825 | if (paste_flag) | |
1826 | { | |
1827 | cpp_token *token = _cpp_temp_token (pfile); | |
1828 | token->type = (*paste_flag)->type; | |
960391da | 1829 | token->val = (*paste_flag)->val; |
f9b5f742 | 1830 | if (src->flags & PASTE_LEFT) |
1831 | token->flags = (*paste_flag)->flags | PASTE_LEFT; | |
1832 | else | |
1833 | token->flags = (*paste_flag)->flags & ~PASTE_LEFT; | |
1834 | *paste_flag = token; | |
1835 | } | |
3c7df4d3 | 1836 | |
ce70f433 | 1837 | i += arg_tokens_count; |
1838 | } | |
f9b5f742 | 1839 | |
ce70f433 | 1840 | if (track_macro_exp) |
1841 | push_extended_tokens_context (pfile, node, buff, virt_locs, first, | |
1842 | tokens_buff_count (buff)); | |
1843 | else | |
1844 | push_ptoken_context (pfile, node, buff, first, | |
1845 | tokens_buff_count (buff)); | |
e77b8253 | 1846 | |
1847 | num_macro_tokens_counter += tokens_buff_count (buff); | |
f9b5f742 | 1848 | } |
1849 | ||
1850 | /* Return a special padding token, with padding inherited from SOURCE. */ | |
1851 | static const cpp_token * | |
f7fdd7a1 | 1852 | padding_token (cpp_reader *pfile, const cpp_token *source) |
f9b5f742 | 1853 | { |
1854 | cpp_token *result = _cpp_temp_token (pfile); | |
1855 | ||
1856 | result->type = CPP_PADDING; | |
bb30d1f4 | 1857 | |
1858 | /* Data in GCed data structures cannot be made const so far, so we | |
1859 | need a cast here. */ | |
1860 | result->val.source = (cpp_token *) source; | |
f9b5f742 | 1861 | result->flags = 0; |
1862 | return result; | |
1863 | } | |
1864 | ||
88cf66fa | 1865 | /* Get a new uninitialized context. Create a new one if we cannot |
1866 | re-use an old one. */ | |
f9b5f742 | 1867 | static cpp_context * |
f7fdd7a1 | 1868 | next_context (cpp_reader *pfile) |
f9b5f742 | 1869 | { |
1870 | cpp_context *result = pfile->context->next; | |
1871 | ||
1872 | if (result == 0) | |
69461e0d | 1873 | { |
3b298764 | 1874 | result = XNEW (cpp_context); |
ce70f433 | 1875 | memset (result, 0, sizeof (cpp_context)); |
f9b5f742 | 1876 | result->prev = pfile->context; |
1877 | result->next = 0; | |
1878 | pfile->context->next = result; | |
79bd622b | 1879 | } |
f9b5f742 | 1880 | |
1881 | pfile->context = result; | |
1882 | return result; | |
79bd622b | 1883 | } |
1884 | ||
f9b5f742 | 1885 | /* Push a list of pointers to tokens. */ |
1886 | static void | |
f7fdd7a1 | 1887 | push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff, |
1888 | const cpp_token **first, unsigned int count) | |
79bd622b | 1889 | { |
1890 | cpp_context *context = next_context (pfile); | |
79bd622b | 1891 | |
ce70f433 | 1892 | context->tokens_kind = TOKENS_KIND_INDIRECT; |
1893 | context->c.macro = macro; | |
084163dc | 1894 | context->buff = buff; |
a854276a | 1895 | FIRST (context).ptoken = first; |
1896 | LAST (context).ptoken = first + count; | |
f9b5f742 | 1897 | } |
1898 | ||
00abfdc0 | 1899 | /* Push a list of tokens. |
1900 | ||
1901 | A NULL macro means that we should continue the current macro | |
1902 | expansion, in essence. That means that if we are currently in a | |
1903 | macro expansion context, we'll make the new pfile->context refer to | |
1904 | the current macro. */ | |
b75b98aa | 1905 | void |
1906 | _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro, | |
1907 | const cpp_token *first, unsigned int count) | |
f9b5f742 | 1908 | { |
00abfdc0 | 1909 | cpp_context *context; |
1910 | ||
1911 | if (macro == NULL) | |
1912 | macro = macro_of_context (pfile->context); | |
1913 | ||
1914 | context = next_context (pfile); | |
ce70f433 | 1915 | context->tokens_kind = TOKENS_KIND_DIRECT; |
1916 | context->c.macro = macro; | |
1917 | context->buff = NULL; | |
00abfdc0 | 1918 | FIRST (context).token = first; |
1919 | LAST (context).token = first + count; | |
79bd622b | 1920 | } |
1921 | ||
ce70f433 | 1922 | /* Build a context containing a list of tokens as well as their |
1923 | virtual locations and push it. TOKENS_BUFF is the buffer that | |
1924 | contains the tokens pointed to by FIRST. If TOKENS_BUFF is | |
1925 | non-NULL, it means that the context owns it, meaning that | |
1926 | _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that | |
00abfdc0 | 1927 | contains the virtual locations. |
1928 | ||
1929 | A NULL macro means that we should continue the current macro | |
1930 | expansion, in essence. That means that if we are currently in a | |
1931 | macro expansion context, we'll make the new pfile->context refer to | |
1932 | the current macro. */ | |
ce70f433 | 1933 | static void |
1934 | push_extended_tokens_context (cpp_reader *pfile, | |
1935 | cpp_hashnode *macro, | |
1936 | _cpp_buff *token_buff, | |
1937 | source_location *virt_locs, | |
1938 | const cpp_token **first, | |
1939 | unsigned int count) | |
1940 | { | |
00abfdc0 | 1941 | cpp_context *context; |
ce70f433 | 1942 | macro_context *m; |
1943 | ||
00abfdc0 | 1944 | if (macro == NULL) |
1945 | macro = macro_of_context (pfile->context); | |
1946 | ||
1947 | context = next_context (pfile); | |
ce70f433 | 1948 | context->tokens_kind = TOKENS_KIND_EXTENDED; |
1949 | context->buff = token_buff; | |
1950 | ||
1951 | m = XNEW (macro_context); | |
1952 | m->macro_node = macro; | |
1953 | m->virt_locs = virt_locs; | |
1954 | m->cur_virt_loc = virt_locs; | |
1955 | context->c.mc = m; | |
1956 | FIRST (context).ptoken = first; | |
1957 | LAST (context).ptoken = first + count; | |
1958 | } | |
1959 | ||
f15f6c8d | 1960 | /* Push a traditional macro's replacement text. */ |
1961 | void | |
f7fdd7a1 | 1962 | _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro, |
1963 | const uchar *start, size_t len) | |
f15f6c8d | 1964 | { |
1965 | cpp_context *context = next_context (pfile); | |
1966 | ||
ce70f433 | 1967 | context->tokens_kind = TOKENS_KIND_DIRECT; |
1968 | context->c.macro = macro; | |
f15f6c8d | 1969 | context->buff = NULL; |
1970 | CUR (context) = start; | |
06025647 | 1971 | RLIMIT (context) = start + len; |
01628c3c | 1972 | macro->flags |= NODE_DISABLED; |
f15f6c8d | 1973 | } |
1974 | ||
ce70f433 | 1975 | /* Creates a buffer that holds tokens a.k.a "token buffer", usually |
1976 | for the purpose of storing them on a cpp_context. If VIRT_LOCS is | |
1977 | non-null (which means that -ftrack-macro-expansion is on), | |
1978 | *VIRT_LOCS is set to a newly allocated buffer that is supposed to | |
1979 | hold the virtual locations of the tokens resulting from macro | |
1980 | expansion. */ | |
1981 | static _cpp_buff* | |
1982 | tokens_buff_new (cpp_reader *pfile, size_t len, | |
1983 | source_location **virt_locs) | |
1984 | { | |
1985 | size_t tokens_size = len * sizeof (cpp_token *); | |
1986 | size_t locs_size = len * sizeof (source_location); | |
1987 | ||
1988 | if (virt_locs != NULL) | |
1989 | *virt_locs = XNEWVEC (source_location, locs_size); | |
1990 | return _cpp_get_buff (pfile, tokens_size); | |
1991 | } | |
1992 | ||
1993 | /* Returns the number of tokens contained in a token buffer. The | |
1994 | buffer holds a set of cpp_token*. */ | |
1995 | static size_t | |
1996 | tokens_buff_count (_cpp_buff *buff) | |
1997 | { | |
1998 | return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *); | |
1999 | } | |
2000 | ||
2001 | /* Return a pointer to the last token contained in the token buffer | |
2002 | BUFF. */ | |
2003 | static const cpp_token ** | |
2004 | tokens_buff_last_token_ptr (_cpp_buff *buff) | |
2005 | { | |
2006 | return &((const cpp_token **) BUFF_FRONT (buff))[-1]; | |
2007 | } | |
2008 | ||
2009 | /* Remove the last token contained in the token buffer TOKENS_BUFF. | |
2010 | If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer | |
2011 | containing the virtual locations of the tokens in TOKENS_BUFF; in | |
2012 | which case the function updates that buffer as well. */ | |
2013 | static inline void | |
2014 | tokens_buff_remove_last_token (_cpp_buff *tokens_buff) | |
2015 | ||
2016 | { | |
2017 | if (BUFF_FRONT (tokens_buff) > tokens_buff->base) | |
2018 | BUFF_FRONT (tokens_buff) = | |
2019 | (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1]; | |
2020 | } | |
2021 | ||
2022 | /* Insert a token into the token buffer at the position pointed to by | |
2023 | DEST. Note that the buffer is not enlarged so the previous token | |
2024 | that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null, | |
2025 | means -ftrack-macro-expansion is effect; it then points to where to | |
2026 | insert the virtual location of TOKEN. TOKEN is the token to | |
2027 | insert. VIRT_LOC is the virtual location of the token, i.e, the | |
e8aa7eaf | 2028 | location possibly encoding its locus across macro expansion. If |
ce70f433 | 2029 | TOKEN is an argument of a function-like macro (inside a macro |
2030 | replacement list), PARM_DEF_LOC is the spelling location of the | |
2031 | macro parameter that TOKEN is replacing, in the replacement list of | |
2032 | the macro. If TOKEN is not an argument of a function-like macro or | |
2033 | if it doesn't come from a macro expansion, then VIRT_LOC can just | |
2034 | be set to the same value as PARM_DEF_LOC. If MAP is non null, it | |
2035 | means TOKEN comes from a macro expansion and MAP is the macro map | |
2036 | associated to the macro. MACRO_TOKEN_INDEX points to the index of | |
2037 | the token in the macro map; it is not considered if MAP is NULL. | |
2038 | ||
2039 | Upon successful completion this function returns the a pointer to | |
2040 | the position of the token coming right after the insertion | |
2041 | point. */ | |
2042 | static inline const cpp_token ** | |
2043 | tokens_buff_put_token_to (const cpp_token **dest, | |
2044 | source_location *virt_loc_dest, | |
2045 | const cpp_token *token, | |
2046 | source_location virt_loc, | |
2047 | source_location parm_def_loc, | |
2048 | const struct line_map *map, | |
2049 | unsigned int macro_token_index) | |
2050 | { | |
2051 | source_location macro_loc = virt_loc; | |
2052 | const cpp_token **result; | |
2053 | ||
2054 | if (virt_loc_dest) | |
2055 | { | |
2056 | /* -ftrack-macro-expansion is on. */ | |
2057 | if (map) | |
2058 | macro_loc = linemap_add_macro_token (map, macro_token_index, | |
2059 | virt_loc, parm_def_loc); | |
2060 | *virt_loc_dest = macro_loc; | |
2061 | } | |
2062 | *dest = token; | |
2063 | result = &dest[1]; | |
2064 | ||
2065 | return result; | |
2066 | } | |
2067 | ||
2068 | /* Adds a token at the end of the tokens contained in BUFFER. Note | |
2069 | that this function doesn't enlarge BUFFER when the number of tokens | |
2070 | reaches BUFFER's size; it aborts in that situation. | |
2071 | ||
2072 | TOKEN is the token to append. VIRT_LOC is the virtual location of | |
e8aa7eaf | 2073 | the token, i.e, the location possibly encoding its locus across |
ce70f433 | 2074 | macro expansion. If TOKEN is an argument of a function-like macro |
2075 | (inside a macro replacement list), PARM_DEF_LOC is the location of | |
2076 | the macro parameter that TOKEN is replacing. If TOKEN doesn't come | |
2077 | from a macro expansion, then VIRT_LOC can just be set to the same | |
2078 | value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes | |
2079 | from a macro expansion and MAP is the macro map associated to the | |
2080 | macro. MACRO_TOKEN_INDEX points to the index of the token in the | |
2081 | macro map; It is not considered if MAP is NULL. If VIRT_LOCS is | |
2082 | non-null, it means -ftrack-macro-expansion is on; in which case | |
2083 | this function adds the virtual location DEF_LOC to the VIRT_LOCS | |
2084 | array, at the same index as the one of TOKEN in BUFFER. Upon | |
2085 | successful completion this function returns the a pointer to the | |
2086 | position of the token coming right after the insertion point. */ | |
2087 | static const cpp_token ** | |
2088 | tokens_buff_add_token (_cpp_buff *buffer, | |
2089 | source_location *virt_locs, | |
2090 | const cpp_token *token, | |
2091 | source_location virt_loc, | |
2092 | source_location parm_def_loc, | |
2093 | const struct line_map *map, | |
2094 | unsigned int macro_token_index) | |
2095 | { | |
2096 | const cpp_token **result; | |
2097 | source_location *virt_loc_dest = NULL; | |
2098 | unsigned token_index = | |
2099 | (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *); | |
2100 | ||
2101 | /* Abort if we pass the end the buffer. */ | |
2102 | if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer)) | |
2103 | abort (); | |
2104 | ||
2105 | if (virt_locs != NULL) | |
2106 | virt_loc_dest = &virt_locs[token_index]; | |
2107 | ||
2108 | result = | |
2109 | tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer), | |
2110 | virt_loc_dest, token, virt_loc, parm_def_loc, | |
2111 | map, macro_token_index); | |
2112 | ||
2113 | BUFF_FRONT (buffer) = (unsigned char *) result; | |
2114 | return result; | |
2115 | } | |
2116 | ||
2117 | /* Allocate space for the function-like macro argument ARG to store | |
2118 | the tokens resulting from the macro-expansion of the tokens that | |
2119 | make up ARG itself. That space is allocated in ARG->expanded and | |
2120 | needs to be freed using free. */ | |
2121 | static void | |
2122 | alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity) | |
2123 | { | |
2124 | #ifdef ENABLE_CHECKING | |
2125 | if (arg->expanded != NULL | |
2126 | || arg->expanded_virt_locs != NULL) | |
2127 | abort (); | |
2128 | #endif | |
2129 | arg->expanded = XNEWVEC (const cpp_token *, capacity); | |
2130 | if (CPP_OPTION (pfile, track_macro_expansion)) | |
2131 | arg->expanded_virt_locs = XNEWVEC (source_location, capacity); | |
2132 | ||
2133 | } | |
2134 | ||
2135 | /* If necessary, enlarge ARG->expanded to so that it can contain SIZE | |
2136 | tokens. */ | |
2137 | static void | |
2138 | ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg, | |
2139 | size_t size, size_t *expanded_capacity) | |
2140 | { | |
2141 | if (size <= *expanded_capacity) | |
2142 | return; | |
2143 | ||
2144 | size *= 2; | |
2145 | ||
2146 | arg->expanded = | |
2147 | XRESIZEVEC (const cpp_token *, arg->expanded, size); | |
2148 | *expanded_capacity = size; | |
2149 | ||
2150 | if (CPP_OPTION (pfile, track_macro_expansion)) | |
2151 | { | |
2152 | if (arg->expanded_virt_locs == NULL) | |
2153 | arg->expanded_virt_locs = XNEWVEC (source_location, size); | |
2154 | else | |
2155 | arg->expanded_virt_locs = XRESIZEVEC (source_location, | |
2156 | arg->expanded_virt_locs, | |
2157 | size); | |
2158 | } | |
2159 | } | |
2160 | ||
88cf66fa | 2161 | /* Expand an argument ARG before replacing parameters in a |
2162 | function-like macro. This works by pushing a context with the | |
2163 | argument's tokens, and then expanding that into a temporary buffer | |
2164 | as if it were a normal part of the token stream. collect_args() | |
2165 | has terminated the argument's tokens with a CPP_EOF so that we know | |
2166 | when we have fully expanded the argument. */ | |
79bd622b | 2167 | static void |
f7fdd7a1 | 2168 | expand_arg (cpp_reader *pfile, macro_arg *arg) |
79bd622b | 2169 | { |
ce70f433 | 2170 | size_t capacity; |
dbb7d6f7 | 2171 | bool saved_warn_trad; |
ce70f433 | 2172 | bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion); |
f9b5f742 | 2173 | |
ce70f433 | 2174 | if (arg->count == 0 |
2175 | || arg->expanded != NULL) | |
f9b5f742 | 2176 | return; |
79bd622b | 2177 | |
dbb7d6f7 | 2178 | /* Don't warn about funlike macros when pre-expanding. */ |
2179 | saved_warn_trad = CPP_WTRADITIONAL (pfile); | |
2180 | CPP_WTRADITIONAL (pfile) = 0; | |
2181 | ||
ce70f433 | 2182 | /* Loop, reading in the tokens of the argument. */ |
f9b5f742 | 2183 | capacity = 256; |
ce70f433 | 2184 | alloc_expanded_arg_mem (pfile, arg, capacity); |
2185 | ||
2186 | if (track_macro_exp_p) | |
2187 | push_extended_tokens_context (pfile, NULL, NULL, | |
2188 | arg->virt_locs, | |
2189 | arg->first, | |
2190 | arg->count + 1); | |
2191 | else | |
2192 | push_ptoken_context (pfile, NULL, NULL, | |
2193 | arg->first, arg->count + 1); | |
79bd622b | 2194 | |
f9b5f742 | 2195 | for (;;) |
79bd622b | 2196 | { |
f9b5f742 | 2197 | const cpp_token *token; |
ce70f433 | 2198 | source_location location; |
f9b5f742 | 2199 | |
ce70f433 | 2200 | ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1, |
2201 | &capacity); | |
79bd622b | 2202 | |
ce70f433 | 2203 | token = cpp_get_token_1 (pfile, &location); |
79bd622b | 2204 | |
f9b5f742 | 2205 | if (token->type == CPP_EOF) |
2206 | break; | |
2207 | ||
ce70f433 | 2208 | set_arg_token (arg, token, location, |
2209 | arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED, | |
2210 | CPP_OPTION (pfile, track_macro_expansion)); | |
2211 | arg->expanded_count++; | |
f9b5f742 | 2212 | } |
2213 | ||
084163dc | 2214 | _cpp_pop_context (pfile); |
dbb7d6f7 | 2215 | |
2216 | CPP_WTRADITIONAL (pfile) = saved_warn_trad; | |
79bd622b | 2217 | } |
2218 | ||
00abfdc0 | 2219 | /* Returns the macro associated to the current context if we are in |
2220 | the context a macro expansion, NULL otherwise. */ | |
2221 | static cpp_hashnode* | |
2222 | macro_of_context (cpp_context *context) | |
2223 | { | |
2224 | if (context == NULL) | |
2225 | return NULL; | |
2226 | ||
2227 | return (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2228 | ? context->c.mc->macro_node | |
2229 | : context->c.macro; | |
2230 | } | |
2231 | ||
8c6425eb | 2232 | /* Return TRUE iff we are expanding a macro or are about to start |
2233 | expanding one. If we are effectively expanding a macro, the | |
2234 | function macro_of_context returns a pointer to the macro being | |
2235 | expanded. */ | |
2236 | static bool | |
2237 | in_macro_expansion_p (cpp_reader *pfile) | |
2238 | { | |
2239 | if (pfile == NULL) | |
2240 | return false; | |
2241 | ||
2242 | return (pfile->about_to_expand_macro_p | |
2243 | || macro_of_context (pfile->context)); | |
2244 | } | |
2245 | ||
88cf66fa | 2246 | /* Pop the current context off the stack, re-enabling the macro if the |
ce70f433 | 2247 | context represented a macro's replacement list. Initially the |
2248 | context structure was not freed so that we can re-use it later, but | |
2249 | now we do free it to reduce peak memory consumption. */ | |
79bd622b | 2250 | void |
f7fdd7a1 | 2251 | _cpp_pop_context (cpp_reader *pfile) |
79bd622b | 2252 | { |
084163dc | 2253 | cpp_context *context = pfile->context; |
2254 | ||
6cfa7465 | 2255 | /* We should not be popping the base context. */ |
2256 | if (context == &pfile->base_context) | |
2257 | abort (); | |
2258 | ||
ce70f433 | 2259 | if (context->c.macro) |
2260 | { | |
2261 | cpp_hashnode *macro; | |
2262 | if (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2263 | { | |
2264 | macro_context *mc = context->c.mc; | |
2265 | macro = mc->macro_node; | |
2266 | /* If context->buff is set, it means the life time of tokens | |
2267 | is bound to the life time of this context; so we must | |
2268 | free the tokens; that means we must free the virtual | |
2269 | locations of these tokens too. */ | |
2270 | if (context->buff && mc->virt_locs) | |
2271 | { | |
2272 | free (mc->virt_locs); | |
2273 | mc->virt_locs = NULL; | |
2274 | } | |
2275 | free (mc); | |
2276 | context->c.mc = NULL; | |
2277 | } | |
2278 | else | |
2279 | macro = context->c.macro; | |
2280 | ||
2281 | /* Beware that MACRO can be NULL in cases like when we are | |
2282 | called from expand_arg. In those cases, a dummy context with | |
2283 | tokens is pushed just for the purpose of walking them using | |
2284 | cpp_get_token_1. In that case, no 'macro' field is set into | |
2285 | the dummy context. */ | |
00abfdc0 | 2286 | if (macro != NULL |
2287 | /* Several contiguous macro expansion contexts can be | |
2288 | associated to the same macro; that means it's the same | |
e8aa7eaf | 2289 | macro expansion that spans across all these (sub) |
00abfdc0 | 2290 | contexts. So we should re-enable an expansion-disabled |
2291 | macro only when we are sure we are really out of that | |
2292 | macro expansion. */ | |
2293 | && macro_of_context (context->prev) != macro) | |
ce70f433 | 2294 | macro->flags &= ~NODE_DISABLED; |
2295 | } | |
084163dc | 2296 | |
2297 | if (context->buff) | |
ce70f433 | 2298 | { |
2299 | /* Decrease memory peak consumption by freeing the memory used | |
2300 | by the context. */ | |
2301 | _cpp_free_buff (context->buff); | |
2302 | } | |
79bd622b | 2303 | |
084163dc | 2304 | pfile->context = context->prev; |
ce70f433 | 2305 | /* decrease peak memory consumption by feeing the context. */ |
2306 | pfile->context->next = NULL; | |
2307 | free (context); | |
79bd622b | 2308 | } |
2309 | ||
ce70f433 | 2310 | /* Return TRUE if we reached the end of the set of tokens stored in |
2311 | CONTEXT, FALSE otherwise. */ | |
2312 | static inline bool | |
2313 | reached_end_of_context (cpp_context *context) | |
2314 | { | |
2315 | if (context->tokens_kind == TOKENS_KIND_DIRECT) | |
2316 | return FIRST (context).token == LAST (context).token; | |
2317 | else if (context->tokens_kind == TOKENS_KIND_INDIRECT | |
2318 | || context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2319 | return FIRST (context).ptoken == LAST (context).ptoken; | |
2320 | else | |
2321 | abort (); | |
2322 | } | |
2323 | ||
2324 | /* Consume the next token contained in the current context of PFILE, | |
2325 | and return it in *TOKEN. It's "full location" is returned in | |
2326 | *LOCATION. If -ftrack-macro-location is in effeect, fFull location" | |
e8aa7eaf | 2327 | means the location encoding the locus of the token across macro |
ce70f433 | 2328 | expansion; otherwise it's just is the "normal" location of the |
2329 | token which (*TOKEN)->src_loc. */ | |
2330 | static inline void | |
2331 | consume_next_token_from_context (cpp_reader *pfile, | |
2332 | const cpp_token ** token, | |
2333 | source_location *location) | |
2334 | { | |
2335 | cpp_context *c = pfile->context; | |
2336 | ||
2337 | if ((c)->tokens_kind == TOKENS_KIND_DIRECT) | |
2338 | { | |
2339 | *token = FIRST (c).token; | |
2340 | *location = (*token)->src_loc; | |
2341 | FIRST (c).token++; | |
2342 | } | |
2343 | else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT) | |
2344 | { | |
2345 | *token = *FIRST (c).ptoken; | |
2346 | *location = (*token)->src_loc; | |
2347 | FIRST (c).ptoken++; | |
2348 | } | |
2349 | else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED) | |
2350 | { | |
2351 | macro_context *m = c->c.mc; | |
2352 | *token = *FIRST (c).ptoken; | |
2353 | if (m->virt_locs) | |
2354 | { | |
2355 | *location = *m->cur_virt_loc; | |
2356 | m->cur_virt_loc++; | |
2357 | } | |
2358 | else | |
2359 | *location = (*token)->src_loc; | |
2360 | FIRST (c).ptoken++; | |
2361 | } | |
2362 | else | |
2363 | abort (); | |
2364 | } | |
2365 | ||
2366 | /* In the traditional mode of the preprocessor, if we are currently in | |
2367 | a directive, the location of a token must be the location of the | |
2368 | start of the directive line. This function returns the proper | |
2369 | location if we are in the traditional mode, and just returns | |
2370 | LOCATION otherwise. */ | |
2371 | ||
2372 | static inline source_location | |
2373 | maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location) | |
2374 | { | |
2375 | if (CPP_OPTION (pfile, traditional)) | |
2376 | { | |
2377 | if (pfile->state.in_directive) | |
2378 | return pfile->directive_line; | |
2379 | } | |
2380 | return location; | |
2381 | } | |
2382 | ||
2383 | /* Routine to get a token as well as its location. | |
343fd982 | 2384 | |
2385 | Macro expansions and directives are transparently handled, | |
2386 | including entering included files. Thus tokens are post-macro | |
2387 | expansion, and after any intervening directives. External callers | |
2388 | see CPP_EOF only at EOF. Internal callers also see it when meeting | |
2389 | a directive inside a macro call, when at the end of a directive and | |
2390 | state.in_directive is still 1, and at the end of argument | |
ce70f433 | 2391 | pre-expansion. |
2392 | ||
2393 | LOC is an out parameter; *LOC is set to the location "as expected | |
2394 | by the user". Please read the comment of | |
2395 | cpp_get_token_with_location to learn more about the meaning of this | |
2396 | location. */ | |
2397 | static const cpp_token* | |
2398 | cpp_get_token_1 (cpp_reader *pfile, source_location *location) | |
79bd622b | 2399 | { |
f9b5f742 | 2400 | const cpp_token *result; |
ce70f433 | 2401 | /* This token is a virtual token that either encodes a location |
2402 | related to macro expansion or a spelling location. */ | |
2403 | source_location virt_loc = 0; | |
8c6425eb | 2404 | /* pfile->about_to_expand_macro_p can be overriden by indirect calls |
2405 | to functions that push macro contexts. So let's save it so that | |
2406 | we can restore it when we are about to leave this routine. */ | |
2407 | bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p; | |
f9b5f742 | 2408 | |
def71b06 | 2409 | for (;;) |
79bd622b | 2410 | { |
f9b5f742 | 2411 | cpp_hashnode *node; |
79bd622b | 2412 | cpp_context *context = pfile->context; |
2413 | ||
79bd622b | 2414 | /* Context->prev == 0 <=> base context. */ |
fb5ab82c | 2415 | if (!context->prev) |
def71b06 | 2416 | { |
ce70f433 | 2417 | result = _cpp_lex_token (pfile); |
2418 | virt_loc = result->src_loc; | |
2419 | } | |
2420 | else if (!reached_end_of_context (context)) | |
2421 | { | |
2422 | consume_next_token_from_context (pfile, &result, | |
2423 | &virt_loc); | |
f9b5f742 | 2424 | if (result->flags & PASTE_LEFT) |
e55de93e | 2425 | { |
f9b5f742 | 2426 | paste_all_tokens (pfile, result); |
2427 | if (pfile->state.in_directive) | |
2428 | continue; | |
ce70f433 | 2429 | result = padding_token (pfile, result); |
2430 | goto out; | |
e55de93e | 2431 | } |
def71b06 | 2432 | } |
79bd622b | 2433 | else |
2434 | { | |
e77b8253 | 2435 | if (pfile->context->c.macro) |
2436 | ++num_expanded_macros_counter; | |
fb5ab82c | 2437 | _cpp_pop_context (pfile); |
f9b5f742 | 2438 | if (pfile->state.in_directive) |
2439 | continue; | |
ce70f433 | 2440 | result = &pfile->avoid_paste; |
2441 | goto out; | |
79bd622b | 2442 | } |
79bd622b | 2443 | |
d3f7919d | 2444 | if (pfile->state.in_directive && result->type == CPP_COMMENT) |
2445 | continue; | |
2446 | ||
f9b5f742 | 2447 | if (result->type != CPP_NAME) |
79bd622b | 2448 | break; |
2449 | ||
2ee04baa | 2450 | node = result->val.node.node; |
f9b5f742 | 2451 | |
c7e5d924 | 2452 | if (node->type != NT_MACRO || (result->flags & NO_EXPAND)) |
2453 | break; | |
b1a9ff83 | 2454 | |
c7e5d924 | 2455 | if (!(node->flags & NODE_DISABLED)) |
79bd622b | 2456 | { |
89768577 | 2457 | int ret = 0; |
931b0a0f | 2458 | /* If not in a macro context, and we're going to start an |
2459 | expansion, record the location. */ | |
8c6425eb | 2460 | if (!in_macro_expansion_p (pfile)) |
931b0a0f | 2461 | pfile->invocation_location = result->src_loc; |
45f9f140 | 2462 | if (pfile->state.prevent_expansion) |
2463 | break; | |
89768577 | 2464 | |
2465 | /* Conditional macros require that a predicate be evaluated | |
2466 | first. */ | |
4514949a | 2467 | if ((node->flags & NODE_CONDITIONAL) != 0) |
2468 | { | |
2469 | if (pfile->cb.macro_to_expand) | |
2470 | { | |
2471 | bool whitespace_after; | |
2472 | const cpp_token *peek_tok = cpp_peek_token (pfile, 0); | |
2473 | ||
2474 | whitespace_after = (peek_tok->type == CPP_PADDING | |
2475 | || (peek_tok->flags & PREV_WHITE)); | |
2476 | node = pfile->cb.macro_to_expand (pfile, result); | |
2477 | if (node) | |
ce70f433 | 2478 | ret = enter_macro_context (pfile, node, result, |
2479 | virt_loc); | |
4514949a | 2480 | else if (whitespace_after) |
2481 | { | |
2482 | /* If macro_to_expand hook returned NULL and it | |
2483 | ate some tokens, see if we don't need to add | |
2484 | a padding token in between this and the | |
2485 | next token. */ | |
2486 | peek_tok = cpp_peek_token (pfile, 0); | |
2487 | if (peek_tok->type != CPP_PADDING | |
2488 | && (peek_tok->flags & PREV_WHITE) == 0) | |
2489 | _cpp_push_token_context (pfile, NULL, | |
2490 | padding_token (pfile, | |
2491 | peek_tok), 1); | |
2492 | } | |
2493 | } | |
2494 | } | |
2495 | else | |
ce70f433 | 2496 | ret = enter_macro_context (pfile, node, result, |
2497 | virt_loc); | |
4514949a | 2498 | if (ret) |
89768577 | 2499 | { |
45f9f140 | 2500 | if (pfile->state.in_directive || ret == 2) |
f9b5f742 | 2501 | continue; |
ce70f433 | 2502 | result = padding_token (pfile, result); |
2503 | goto out; | |
8c2e2fc5 | 2504 | } |
79bd622b | 2505 | } |
c7e5d924 | 2506 | else |
2507 | { | |
88cf66fa | 2508 | /* Flag this token as always unexpandable. FIXME: move this |
2509 | to collect_args()?. */ | |
c7e5d924 | 2510 | cpp_token *t = _cpp_temp_token (pfile); |
2511 | t->type = result->type; | |
2512 | t->flags = result->flags | NO_EXPAND; | |
960391da | 2513 | t->val = result->val; |
c7e5d924 | 2514 | result = t; |
2515 | } | |
396ffa86 | 2516 | |
c7e5d924 | 2517 | break; |
79bd622b | 2518 | } |
f9b5f742 | 2519 | |
ce70f433 | 2520 | out: |
2521 | if (location != NULL) | |
2522 | { | |
2523 | if (virt_loc == 0) | |
2524 | virt_loc = result->src_loc; | |
2525 | *location = virt_loc; | |
2526 | ||
2527 | if (!CPP_OPTION (pfile, track_macro_expansion) | |
8c6425eb | 2528 | && macro_of_context (pfile->context) != NULL) |
ce70f433 | 2529 | /* We are in a macro expansion context, are not tracking |
2530 | virtual location, but were asked to report the location | |
2531 | of the expansion point of the macro being expanded. */ | |
2532 | *location = pfile->invocation_location; | |
2533 | ||
2534 | *location = maybe_adjust_loc_for_trad_cpp (pfile, *location); | |
2535 | } | |
8c6425eb | 2536 | |
2537 | pfile->about_to_expand_macro_p = saved_about_to_expand_macro; | |
f9b5f742 | 2538 | return result; |
79bd622b | 2539 | } |
2540 | ||
ce70f433 | 2541 | /* External routine to get a token. Also used nearly everywhere |
2542 | internally, except for places where we know we can safely call | |
2543 | _cpp_lex_token directly, such as lexing a directive name. | |
2544 | ||
2545 | Macro expansions and directives are transparently handled, | |
2546 | including entering included files. Thus tokens are post-macro | |
2547 | expansion, and after any intervening directives. External callers | |
2548 | see CPP_EOF only at EOF. Internal callers also see it when meeting | |
2549 | a directive inside a macro call, when at the end of a directive and | |
2550 | state.in_directive is still 1, and at the end of argument | |
2551 | pre-expansion. */ | |
2552 | const cpp_token * | |
2553 | cpp_get_token (cpp_reader *pfile) | |
2554 | { | |
2555 | return cpp_get_token_1 (pfile, NULL); | |
2556 | } | |
2557 | ||
2558 | /* Like cpp_get_token, but also returns a virtual token location | |
2559 | separate from the spelling location carried by the returned token. | |
2560 | ||
2561 | LOC is an out parameter; *LOC is set to the location "as expected | |
2562 | by the user". This matters when a token results from macro | |
2563 | expansion; in that case the token's spelling location indicates the | |
2564 | locus of the token in the definition of the macro but *LOC | |
2565 | virtually encodes all the other meaningful locuses associated to | |
2566 | the token. | |
2567 | ||
2568 | What? virtual location? Yes, virtual location. | |
2569 | ||
2570 | If the token results from macro expansion and if macro expansion | |
2571 | location tracking is enabled its virtual location encodes (at the | |
2572 | same time): | |
2573 | ||
2574 | - the spelling location of the token | |
2575 | ||
2576 | - the locus of the macro expansion point | |
2577 | ||
2578 | - the locus of the point where the token got instantiated as part | |
2579 | of the macro expansion process. | |
2580 | ||
2581 | You have to use the linemap API to get the locus you are interested | |
2582 | in from a given virtual location. | |
2583 | ||
2584 | Note however that virtual locations are not necessarily ordered for | |
2585 | relations '<' and '>'. One must use the function | |
2586 | linemap_location_before_p instead of using the relational operator | |
2587 | '<'. | |
2588 | ||
2589 | If macro expansion tracking is off and if the token results from | |
2590 | macro expansion the virtual location is the expansion point of the | |
2591 | macro that got expanded. | |
2592 | ||
2593 | When the token doesn't result from macro expansion, the virtual | |
2594 | location is just the same thing as its spelling location. */ | |
2595 | ||
931b0a0f | 2596 | const cpp_token * |
2597 | cpp_get_token_with_location (cpp_reader *pfile, source_location *loc) | |
2598 | { | |
8c6425eb | 2599 | return cpp_get_token_1 (pfile, loc); |
931b0a0f | 2600 | } |
2601 | ||
ae2348f6 | 2602 | /* Returns true if we're expanding an object-like macro that was |
2603 | defined in a system header. Just checks the macro at the top of | |
2604 | the stack. Used for diagnostic suppression. */ | |
2605 | int | |
f7fdd7a1 | 2606 | cpp_sys_macro_p (cpp_reader *pfile) |
ae2348f6 | 2607 | { |
d16f4f47 | 2608 | cpp_hashnode *node = NULL; |
2609 | ||
2610 | if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2611 | node = pfile->context->c.mc->macro_node; | |
2612 | else | |
2613 | node = pfile->context->c.macro; | |
ae2348f6 | 2614 | |
c7e5d924 | 2615 | return node && node->value.macro && node->value.macro->syshdr; |
ae2348f6 | 2616 | } |
2617 | ||
02516fb9 | 2618 | /* Read each token in, until end of the current file. Directives are |
2619 | transparently processed. */ | |
79bd622b | 2620 | void |
f7fdd7a1 | 2621 | cpp_scan_nooutput (cpp_reader *pfile) |
79bd622b | 2622 | { |
6e04daf1 | 2623 | /* Request a CPP_EOF token at the end of this file, rather than |
2624 | transparently continuing with the including file. */ | |
2625 | pfile->buffer->return_at_eof = true; | |
2626 | ||
3eb3f293 | 2627 | pfile->state.discarding_output++; |
2628 | pfile->state.prevent_expansion++; | |
2629 | ||
7951771a | 2630 | if (CPP_OPTION (pfile, traditional)) |
2631 | while (_cpp_read_logical_line_trad (pfile)) | |
2632 | ; | |
2633 | else | |
2634 | while (cpp_get_token (pfile)->type != CPP_EOF) | |
2635 | ; | |
3eb3f293 | 2636 | |
2637 | pfile->state.discarding_output--; | |
2638 | pfile->state.prevent_expansion--; | |
79bd622b | 2639 | } |
2640 | ||
89768577 | 2641 | /* Step back one or more tokens obtained from the lexer. */ |
2642 | void | |
2643 | _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count) | |
2644 | { | |
2645 | pfile->lookaheads += count; | |
2646 | while (count--) | |
2647 | { | |
2648 | pfile->cur_token--; | |
2649 | if (pfile->cur_token == pfile->cur_run->base | |
2650 | /* Possible with -fpreprocessed and no leading #line. */ | |
2651 | && pfile->cur_run->prev != NULL) | |
2652 | { | |
2653 | pfile->cur_run = pfile->cur_run->prev; | |
2654 | pfile->cur_token = pfile->cur_run->limit; | |
2655 | } | |
2656 | } | |
2657 | } | |
2658 | ||
add258d7 | 2659 | /* Step back one (or more) tokens. Can only step back more than 1 if |
fb5ab82c | 2660 | they are from the lexer, and not from macro expansion. */ |
920b5d41 | 2661 | void |
f7fdd7a1 | 2662 | _cpp_backup_tokens (cpp_reader *pfile, unsigned int count) |
79bd622b | 2663 | { |
fb5ab82c | 2664 | if (pfile->context->prev == NULL) |
89768577 | 2665 | _cpp_backup_tokens_direct (pfile, count); |
fb5ab82c | 2666 | else |
79bd622b | 2667 | { |
fb5ab82c | 2668 | if (count != 1) |
2669 | abort (); | |
ce70f433 | 2670 | if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT) |
a854276a | 2671 | FIRST (pfile->context).token--; |
ce70f433 | 2672 | else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT) |
a854276a | 2673 | FIRST (pfile->context).ptoken--; |
ce70f433 | 2674 | else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) |
2675 | { | |
2676 | FIRST (pfile->context).ptoken--; | |
2677 | if (pfile->context->c.macro) | |
2678 | { | |
2679 | macro_context *m = pfile->context->c.mc; | |
2680 | m->cur_virt_loc--; | |
2681 | #ifdef ENABLE_CHECKING | |
2682 | if (m->cur_virt_loc < m->virt_locs) | |
2683 | abort (); | |
2684 | #endif | |
2685 | } | |
2686 | else | |
2687 | abort (); | |
2688 | } | |
2689 | else | |
2690 | abort (); | |
79bd622b | 2691 | } |
2692 | } | |
69461e0d | 2693 | |
79bd622b | 2694 | /* #define directive parsing and handling. */ |
2695 | ||
d10cfa8d | 2696 | /* Returns nonzero if a macro redefinition warning is required. */ |
f15f6c8d | 2697 | static bool |
25692381 | 2698 | warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node, |
f7fdd7a1 | 2699 | const cpp_macro *macro2) |
79bd622b | 2700 | { |
2701 | const cpp_macro *macro1; | |
2702 | unsigned int i; | |
2703 | ||
31ca26b1 | 2704 | /* Some redefinitions need to be warned about regardless. */ |
2705 | if (node->flags & NODE_WARN) | |
f15f6c8d | 2706 | return true; |
79bd622b | 2707 | |
a7d2e480 | 2708 | /* Suppress warnings for builtins that lack the NODE_WARN flag, |
2709 | unless Wbuiltin-macro-redefined. */ | |
2710 | if (node->flags & NODE_BUILTIN | |
2711 | && (!pfile->cb.user_builtin_macro | |
2712 | || !pfile->cb.user_builtin_macro (pfile, node))) | |
2713 | return CPP_OPTION (pfile, warn_builtin_macro_redefined); | |
2ad0b097 | 2714 | |
89768577 | 2715 | /* Redefinitions of conditional (context-sensitive) macros, on |
2716 | the other hand, must be allowed silently. */ | |
2717 | if (node->flags & NODE_CONDITIONAL) | |
2718 | return false; | |
2719 | ||
31ca26b1 | 2720 | /* Redefinition of a macro is allowed if and only if the old and new |
2c0e001b | 2721 | definitions are the same. (6.10.3 paragraph 2). */ |
79bd622b | 2722 | macro1 = node->value.macro; |
2723 | ||
3396dc80 | 2724 | /* Don't check count here as it can be different in valid |
2725 | traditional redefinitions with just whitespace differences. */ | |
2726 | if (macro1->paramc != macro2->paramc | |
79bd622b | 2727 | || macro1->fun_like != macro2->fun_like |
53c052ca | 2728 | || macro1->variadic != macro2->variadic) |
f15f6c8d | 2729 | return true; |
79bd622b | 2730 | |
2731 | /* Check parameter spellings. */ | |
2732 | for (i = 0; i < macro1->paramc; i++) | |
2733 | if (macro1->params[i] != macro2->params[i]) | |
f15f6c8d | 2734 | return true; |
79bd622b | 2735 | |
f15f6c8d | 2736 | /* Check the replacement text or tokens. */ |
2737 | if (CPP_OPTION (pfile, traditional)) | |
3396dc80 | 2738 | return _cpp_expansions_different_trad (macro1, macro2); |
f15f6c8d | 2739 | |
9c7ff5cc | 2740 | if (macro1->count != macro2->count) |
2741 | return true; | |
2742 | ||
2743 | for (i = 0; i < macro1->count; i++) | |
2744 | if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i])) | |
2745 | return true; | |
f15f6c8d | 2746 | |
2747 | return false; | |
79bd622b | 2748 | } |
2749 | ||
2750 | /* Free the definition of hashnode H. */ | |
69461e0d | 2751 | void |
f7fdd7a1 | 2752 | _cpp_free_definition (cpp_hashnode *h) |
69461e0d | 2753 | { |
79bd622b | 2754 | /* Macros and assertions no longer have anything to free. */ |
2755 | h->type = NT_VOID; | |
2756 | /* Clear builtin flag in case of redefinition. */ | |
34c3de48 | 2757 | h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED); |
79bd622b | 2758 | } |
2759 | ||
109ca87a | 2760 | /* Save parameter NODE (spelling SPELLING) to the parameter list of |
2761 | macro MACRO. Returns zero on success, nonzero if the parameter is | |
2762 | a duplicate. */ | |
198b48a0 | 2763 | bool |
109ca87a | 2764 | _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node, |
2765 | cpp_hashnode *spelling) | |
79bd622b | 2766 | { |
805e22b2 | 2767 | unsigned int len; |
79bd622b | 2768 | /* Constraint 6.10.3.6 - duplicate parameter names. */ |
805e22b2 | 2769 | if (node->flags & NODE_MACRO_ARG) |
11d10d3f | 2770 | { |
d80d2074 | 2771 | cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"", |
73328dce | 2772 | NODE_NAME (node)); |
1ae6ed16 | 2773 | return true; |
79bd622b | 2774 | } |
11d10d3f | 2775 | |
e6a5f963 | 2776 | if (BUFF_ROOM (pfile->a_buff) |
2777 | < (macro->paramc + 1) * sizeof (cpp_hashnode *)) | |
2778 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *)); | |
11d10d3f | 2779 | |
109ca87a | 2780 | ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling; |
805e22b2 | 2781 | node->flags |= NODE_MACRO_ARG; |
109ca87a | 2782 | len = macro->paramc * sizeof (struct macro_arg_saved_data); |
805e22b2 | 2783 | if (len > pfile->macro_buffer_len) |
2784 | { | |
720aca92 | 2785 | pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer, |
2786 | len); | |
805e22b2 | 2787 | pfile->macro_buffer_len = len; |
2788 | } | |
109ca87a | 2789 | struct macro_arg_saved_data save; |
2790 | save.value = node->value; | |
2791 | save.canonical_node = node; | |
2792 | ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1] | |
2793 | = save; | |
805e22b2 | 2794 | |
2795 | node->value.arg_index = macro->paramc; | |
1ae6ed16 | 2796 | return false; |
69461e0d | 2797 | } |
2798 | ||
1ae6ed16 | 2799 | /* Check the syntax of the parameters in a MACRO definition. Returns |
2800 | false if an error occurs. */ | |
2801 | static bool | |
f7fdd7a1 | 2802 | parse_params (cpp_reader *pfile, cpp_macro *macro) |
69461e0d | 2803 | { |
79bd622b | 2804 | unsigned int prev_ident = 0; |
69461e0d | 2805 | |
79bd622b | 2806 | for (;;) |
69461e0d | 2807 | { |
c00e481c | 2808 | const cpp_token *token = _cpp_lex_token (pfile); |
69461e0d | 2809 | |
c00e481c | 2810 | switch (token->type) |
69461e0d | 2811 | { |
79bd622b | 2812 | default: |
d3f7919d | 2813 | /* Allow/ignore comments in parameter lists if we are |
2814 | preserving comments in macro expansions. */ | |
2815 | if (token->type == CPP_COMMENT | |
2816 | && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)) | |
2817 | continue; | |
2818 | ||
d80d2074 | 2819 | cpp_error (pfile, CPP_DL_ERROR, |
73328dce | 2820 | "\"%s\" may not appear in macro parameter list", |
c00e481c | 2821 | cpp_token_as_text (pfile, token)); |
1ae6ed16 | 2822 | return false; |
79bd622b | 2823 | |
69461e0d | 2824 | case CPP_NAME: |
79bd622b | 2825 | if (prev_ident) |
2826 | { | |
d80d2074 | 2827 | cpp_error (pfile, CPP_DL_ERROR, |
73328dce | 2828 | "macro parameters must be comma-separated"); |
1ae6ed16 | 2829 | return false; |
79bd622b | 2830 | } |
2831 | prev_ident = 1; | |
2832 | ||
109ca87a | 2833 | if (_cpp_save_parameter (pfile, macro, token->val.node.node, |
2834 | token->val.node.spelling)) | |
1ae6ed16 | 2835 | return false; |
79bd622b | 2836 | continue; |
69461e0d | 2837 | |
79bd622b | 2838 | case CPP_CLOSE_PAREN: |
2839 | if (prev_ident || macro->paramc == 0) | |
1ae6ed16 | 2840 | return true; |
69461e0d | 2841 | |
79bd622b | 2842 | /* Fall through to pick up the error. */ |
2843 | case CPP_COMMA: | |
2844 | if (!prev_ident) | |
2845 | { | |
d80d2074 | 2846 | cpp_error (pfile, CPP_DL_ERROR, "parameter name missing"); |
1ae6ed16 | 2847 | return false; |
79bd622b | 2848 | } |
2849 | prev_ident = 0; | |
69461e0d | 2850 | continue; |
2851 | ||
79bd622b | 2852 | case CPP_ELLIPSIS: |
53c052ca | 2853 | macro->variadic = 1; |
79bd622b | 2854 | if (!prev_ident) |
2855 | { | |
198b48a0 | 2856 | _cpp_save_parameter (pfile, macro, |
109ca87a | 2857 | pfile->spec_nodes.n__VA_ARGS__, |
198b48a0 | 2858 | pfile->spec_nodes.n__VA_ARGS__); |
79bd622b | 2859 | pfile->state.va_args_ok = 1; |
05d51900 | 2860 | if (! CPP_OPTION (pfile, c99) |
5ae82d58 | 2861 | && CPP_OPTION (pfile, cpp_pedantic) |
05d51900 | 2862 | && CPP_OPTION (pfile, warn_variadic_macros)) |
49f161b6 | 2863 | { |
2864 | if (CPP_OPTION (pfile, cplusplus)) | |
2865 | cpp_pedwarning | |
2866 | (pfile, CPP_W_VARIADIC_MACROS, | |
2867 | "anonymous variadic macros were introduced in C++11"); | |
2868 | else | |
2869 | cpp_pedwarning | |
2870 | (pfile, CPP_W_VARIADIC_MACROS, | |
2871 | "anonymous variadic macros were introduced in C99"); | |
2872 | } | |
806fe15e | 2873 | else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 |
890c2e2f | 2874 | && ! CPP_OPTION (pfile, cplusplus)) |
2875 | cpp_error (pfile, CPP_DL_WARNING, | |
2876 | "anonymous variadic macros were introduced in C99"); | |
79bd622b | 2877 | } |
5ae82d58 | 2878 | else if (CPP_OPTION (pfile, cpp_pedantic) |
05d51900 | 2879 | && CPP_OPTION (pfile, warn_variadic_macros)) |
49f161b6 | 2880 | { |
2881 | if (CPP_OPTION (pfile, cplusplus)) | |
2882 | cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, | |
2883 | "ISO C++ does not permit named variadic macros"); | |
2884 | else | |
2885 | cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, | |
3a79f5da | 2886 | "ISO C does not permit named variadic macros"); |
49f161b6 | 2887 | } |
69461e0d | 2888 | |
79bd622b | 2889 | /* We're at the end, and just expect a closing parenthesis. */ |
c00e481c | 2890 | token = _cpp_lex_token (pfile); |
2891 | if (token->type == CPP_CLOSE_PAREN) | |
1ae6ed16 | 2892 | return true; |
79bd622b | 2893 | /* Fall through. */ |
69461e0d | 2894 | |
79bd622b | 2895 | case CPP_EOF: |
d80d2074 | 2896 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list"); |
1ae6ed16 | 2897 | return false; |
69461e0d | 2898 | } |
69461e0d | 2899 | } |
79bd622b | 2900 | } |
2901 | ||
10b4496a | 2902 | /* Allocate room for a token from a macro's replacement list. */ |
79bd622b | 2903 | static cpp_token * |
f7fdd7a1 | 2904 | alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro) |
79bd622b | 2905 | { |
e6a5f963 | 2906 | if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token)) |
2907 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token)); | |
69461e0d | 2908 | |
e6a5f963 | 2909 | return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++]; |
10b4496a | 2910 | } |
2911 | ||
88cf66fa | 2912 | /* Lex a token from the expansion of MACRO, but mark parameters as we |
2913 | find them and warn of traditional stringification. */ | |
10b4496a | 2914 | static cpp_token * |
f7fdd7a1 | 2915 | lex_expansion_token (cpp_reader *pfile, cpp_macro *macro) |
10b4496a | 2916 | { |
e0ff7935 | 2917 | cpp_token *token, *saved_cur_token; |
10b4496a | 2918 | |
e0ff7935 | 2919 | saved_cur_token = pfile->cur_token; |
10b4496a | 2920 | pfile->cur_token = alloc_expansion_token (pfile, macro); |
2921 | token = _cpp_lex_direct (pfile); | |
e0ff7935 | 2922 | pfile->cur_token = saved_cur_token; |
79bd622b | 2923 | |
88cf66fa | 2924 | /* Is this a parameter? */ |
805e22b2 | 2925 | if (token->type == CPP_NAME |
2ee04baa | 2926 | && (token->val.node.node->flags & NODE_MACRO_ARG) != 0) |
79bd622b | 2927 | { |
109ca87a | 2928 | cpp_hashnode *spelling = token->val.node.spelling; |
79bd622b | 2929 | token->type = CPP_MACRO_ARG; |
2ee04baa | 2930 | token->val.macro_arg.arg_no = token->val.node.node->value.arg_index; |
109ca87a | 2931 | token->val.macro_arg.spelling = spelling; |
79bd622b | 2932 | } |
2933 | else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 | |
2934 | && (token->type == CPP_STRING || token->type == CPP_CHAR)) | |
2935 | check_trad_stringification (pfile, macro, &token->val.str); | |
2936 | ||
2937 | return token; | |
69461e0d | 2938 | } |
2939 | ||
f15f6c8d | 2940 | static bool |
f7fdd7a1 | 2941 | create_iso_definition (cpp_reader *pfile, cpp_macro *macro) |
69461e0d | 2942 | { |
f15f6c8d | 2943 | cpp_token *token; |
10b4496a | 2944 | const cpp_token *ctoken; |
3b6c638e | 2945 | bool following_paste_op = false; |
2946 | const char *paste_op_error_msg = | |
2947 | N_("'##' cannot appear at either end of a macro expansion"); | |
941f2388 | 2948 | unsigned int num_extra_tokens = 0; |
79bd622b | 2949 | |
2950 | /* Get the first token of the expansion (or the '(' of a | |
2951 | function-like macro). */ | |
10b4496a | 2952 | ctoken = _cpp_lex_token (pfile); |
2953 | ||
2954 | if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE)) | |
79bd622b | 2955 | { |
f15f6c8d | 2956 | bool ok = parse_params (pfile, macro); |
e6a5f963 | 2957 | macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff); |
2958 | if (!ok) | |
f15f6c8d | 2959 | return false; |
e6a5f963 | 2960 | |
c39ed964 | 2961 | /* Success. Commit or allocate the parameter array. */ |
2962 | if (pfile->hash_table->alloc_subobject) | |
2963 | { | |
720aca92 | 2964 | cpp_hashnode **params = |
2965 | (cpp_hashnode **) pfile->hash_table->alloc_subobject | |
2966 | (sizeof (cpp_hashnode *) * macro->paramc); | |
bb30d1f4 | 2967 | memcpy (params, macro->params, |
2968 | sizeof (cpp_hashnode *) * macro->paramc); | |
2969 | macro->params = params; | |
c39ed964 | 2970 | } |
2971 | else | |
2972 | BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc]; | |
79bd622b | 2973 | macro->fun_like = 1; |
79bd622b | 2974 | } |
10b4496a | 2975 | else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE)) |
99617355 | 2976 | { |
2977 | /* While ISO C99 requires whitespace before replacement text | |
49f161b6 | 2978 | in a macro definition, ISO C90 with TC1 allows characters |
2979 | from the basic source character set there. */ | |
99617355 | 2980 | if (CPP_OPTION (pfile, c99)) |
49f161b6 | 2981 | { |
2982 | if (CPP_OPTION (pfile, cplusplus)) | |
2983 | cpp_error (pfile, CPP_DL_PEDWARN, | |
2984 | "ISO C++11 requires whitespace after the macro name"); | |
2985 | else | |
2986 | cpp_error (pfile, CPP_DL_PEDWARN, | |
2987 | "ISO C99 requires whitespace after the macro name"); | |
2988 | } | |
99617355 | 2989 | else |
2990 | { | |
2991 | int warntype = CPP_DL_WARNING; | |
2992 | switch (ctoken->type) | |
2993 | { | |
2994 | case CPP_ATSIGN: | |
2995 | case CPP_AT_NAME: | |
2996 | case CPP_OBJC_STRING: | |
2997 | /* '@' is not in basic character set. */ | |
2998 | warntype = CPP_DL_PEDWARN; | |
2999 | break; | |
3000 | case CPP_OTHER: | |
3001 | /* Basic character set sans letters, digits and _. */ | |
3002 | if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~", | |
3003 | ctoken->val.str.text[0]) == NULL) | |
3004 | warntype = CPP_DL_PEDWARN; | |
3005 | break; | |
3006 | default: | |
3007 | /* All other tokens start with a character from basic | |
3008 | character set. */ | |
3009 | break; | |
3010 | } | |
3011 | cpp_error (pfile, warntype, | |
3012 | "missing whitespace after the macro name"); | |
3013 | } | |
3014 | } | |
69461e0d | 3015 | |
10b4496a | 3016 | if (macro->fun_like) |
3017 | token = lex_expansion_token (pfile, macro); | |
3018 | else | |
3019 | { | |
3020 | token = alloc_expansion_token (pfile, macro); | |
3021 | *token = *ctoken; | |
3022 | } | |
69461e0d | 3023 | |
79bd622b | 3024 | for (;;) |
3025 | { | |
3026 | /* Check the stringifying # constraint 6.10.3.2.1 of | |
3027 | function-like macros when lexing the subsequent token. */ | |
3028 | if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) | |
3029 | { | |
3030 | if (token->type == CPP_MACRO_ARG) | |
3031 | { | |
941f2388 | 3032 | if (token->flags & PREV_WHITE) |
3033 | token->flags |= SP_PREV_WHITE; | |
3034 | if (token[-1].flags & DIGRAPH) | |
3035 | token->flags |= SP_DIGRAPH; | |
79bd622b | 3036 | token->flags &= ~PREV_WHITE; |
3037 | token->flags |= STRINGIFY_ARG; | |
3038 | token->flags |= token[-1].flags & PREV_WHITE; | |
3039 | token[-1] = token[0]; | |
3040 | macro->count--; | |
3041 | } | |
3042 | /* Let assembler get away with murder. */ | |
5db5d057 | 3043 | else if (CPP_OPTION (pfile, lang) != CLK_ASM) |
79bd622b | 3044 | { |
d80d2074 | 3045 | cpp_error (pfile, CPP_DL_ERROR, |
73328dce | 3046 | "'#' is not followed by a macro parameter"); |
f15f6c8d | 3047 | return false; |
79bd622b | 3048 | } |
3049 | } | |
3050 | ||
3051 | if (token->type == CPP_EOF) | |
3b6c638e | 3052 | { |
3053 | /* Paste operator constraint 6.10.3.3.1: | |
3054 | Token-paste ##, can appear in both object-like and | |
3055 | function-like macros, but not at the end. */ | |
3056 | if (following_paste_op) | |
3057 | { | |
3058 | cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); | |
3059 | return false; | |
3060 | } | |
3061 | break; | |
3062 | } | |
79bd622b | 3063 | |
3064 | /* Paste operator constraint 6.10.3.3.1. */ | |
3065 | if (token->type == CPP_PASTE) | |
3066 | { | |
3067 | /* Token-paste ##, can appear in both object-like and | |
3b6c638e | 3068 | function-like macros, but not at the beginning. */ |
3069 | if (macro->count == 1) | |
79bd622b | 3070 | { |
3b6c638e | 3071 | cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); |
f15f6c8d | 3072 | return false; |
79bd622b | 3073 | } |
69461e0d | 3074 | |
941f2388 | 3075 | if (token[-1].flags & PASTE_LEFT) |
3076 | { | |
3077 | macro->extra_tokens = 1; | |
3078 | num_extra_tokens++; | |
2ee04baa | 3079 | token->val.token_no = macro->count - 1; |
941f2388 | 3080 | } |
3081 | else | |
3082 | { | |
3083 | --macro->count; | |
3084 | token[-1].flags |= PASTE_LEFT; | |
3085 | if (token->flags & DIGRAPH) | |
3086 | token[-1].flags |= SP_DIGRAPH; | |
3087 | if (token->flags & PREV_WHITE) | |
3088 | token[-1].flags |= SP_PREV_WHITE; | |
3089 | } | |
79bd622b | 3090 | } |
3091 | ||
3b6c638e | 3092 | following_paste_op = (token->type == CPP_PASTE); |
79bd622b | 3093 | token = lex_expansion_token (pfile, macro); |
3094 | } | |
3095 | ||
672f38da | 3096 | macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff); |
c39ed964 | 3097 | macro->traditional = 0; |
e6a5f963 | 3098 | |
3c7df4d3 | 3099 | /* Don't count the CPP_EOF. */ |
3100 | macro->count--; | |
79bd622b | 3101 | |
88cf66fa | 3102 | /* Clear whitespace on first token for warn_of_redefinition(). */ |
e6a5f963 | 3103 | if (macro->count) |
672f38da | 3104 | macro->exp.tokens[0].flags &= ~PREV_WHITE; |
e6a5f963 | 3105 | |
c39ed964 | 3106 | /* Commit or allocate the memory. */ |
3107 | if (pfile->hash_table->alloc_subobject) | |
3108 | { | |
720aca92 | 3109 | cpp_token *tokns = |
3110 | (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token) | |
3111 | * macro->count); | |
941f2388 | 3112 | if (num_extra_tokens) |
3113 | { | |
3114 | /* Place second and subsequent ## or %:%: tokens in | |
3115 | sequences of consecutive such tokens at the end of the | |
3116 | list to preserve information about where they appear, how | |
3117 | they are spelt and whether they are preceded by | |
3118 | whitespace without otherwise interfering with macro | |
3119 | expansion. */ | |
3120 | cpp_token *normal_dest = tokns; | |
3121 | cpp_token *extra_dest = tokns + macro->count - num_extra_tokens; | |
3122 | unsigned int i; | |
3123 | for (i = 0; i < macro->count; i++) | |
3124 | { | |
3125 | if (macro->exp.tokens[i].type == CPP_PASTE) | |
3126 | *extra_dest++ = macro->exp.tokens[i]; | |
3127 | else | |
3128 | *normal_dest++ = macro->exp.tokens[i]; | |
3129 | } | |
3130 | } | |
3131 | else | |
3132 | memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count); | |
c39ed964 | 3133 | macro->exp.tokens = tokns; |
3134 | } | |
3135 | else | |
3136 | BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count]; | |
e6a5f963 | 3137 | |
f15f6c8d | 3138 | return true; |
3139 | } | |
3140 | ||
d10cfa8d | 3141 | /* Parse a macro and save its expansion. Returns nonzero on success. */ |
f15f6c8d | 3142 | bool |
f7fdd7a1 | 3143 | _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node) |
f15f6c8d | 3144 | { |
3145 | cpp_macro *macro; | |
3146 | unsigned int i; | |
3147 | bool ok; | |
4dab9f9f | 3148 | |
c39ed964 | 3149 | if (pfile->hash_table->alloc_subobject) |
720aca92 | 3150 | macro = (cpp_macro *) pfile->hash_table->alloc_subobject |
3151 | (sizeof (cpp_macro)); | |
c39ed964 | 3152 | else |
3153 | macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro)); | |
f15f6c8d | 3154 | macro->line = pfile->directive_line; |
3155 | macro->params = 0; | |
3156 | macro->paramc = 0; | |
3157 | macro->variadic = 0; | |
b717e161 | 3158 | macro->used = !CPP_OPTION (pfile, warn_unused_macros); |
f15f6c8d | 3159 | macro->count = 0; |
3160 | macro->fun_like = 0; | |
941f2388 | 3161 | macro->extra_tokens = 0; |
ae2348f6 | 3162 | /* To suppress some diagnostics. */ |
610625e3 | 3163 | macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0; |
ae2348f6 | 3164 | |
f15f6c8d | 3165 | if (CPP_OPTION (pfile, traditional)) |
3166 | ok = _cpp_create_trad_definition (pfile, macro); | |
3167 | else | |
3168 | { | |
f15f6c8d | 3169 | ok = create_iso_definition (pfile, macro); |
3170 | ||
e0ff7935 | 3171 | /* We set the type for SEEN_EOL() in directives.c. |
f15f6c8d | 3172 | |
3173 | Longer term we should lex the whole line before coming here, | |
3174 | and just copy the expansion. */ | |
f15f6c8d | 3175 | |
3176 | /* Stop the lexer accepting __VA_ARGS__. */ | |
3177 | pfile->state.va_args_ok = 0; | |
3178 | } | |
3179 | ||
3180 | /* Clear the fast argument lookup indices. */ | |
3181 | for (i = macro->paramc; i-- > 0; ) | |
805e22b2 | 3182 | { |
109ca87a | 3183 | struct macro_arg_saved_data *save = |
3184 | &((struct macro_arg_saved_data *) pfile->macro_buffer)[i]; | |
3185 | struct cpp_hashnode *node = save->canonical_node; | |
805e22b2 | 3186 | node->flags &= ~ NODE_MACRO_ARG; |
109ca87a | 3187 | node->value = save->value; |
805e22b2 | 3188 | } |
f15f6c8d | 3189 | |
3190 | if (!ok) | |
3191 | return ok; | |
3192 | ||
70145d81 | 3193 | if (node->type == NT_MACRO) |
69461e0d | 3194 | { |
71a7c282 | 3195 | if (CPP_OPTION (pfile, warn_unused_macros)) |
3196 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
3197 | ||
f15f6c8d | 3198 | if (warn_of_redefinition (pfile, node, macro)) |
69461e0d | 3199 | { |
a7d2e480 | 3200 | const int reason = ((node->flags & NODE_BUILTIN) |
3201 | && !(node->flags & NODE_WARN)) | |
3a79f5da | 3202 | ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE; |
3a79f5da | 3203 | |
a7d2e480 | 3204 | bool warned = |
3205 | cpp_pedwarning_with_line (pfile, reason, | |
3206 | pfile->directive_line, 0, | |
3207 | "\"%s\" redefined", NODE_NAME (node)); | |
79bd622b | 3208 | |
7f5f3953 | 3209 | if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) |
3210 | cpp_error_with_line (pfile, CPP_DL_NOTE, | |
f15f6c8d | 3211 | node->value.macro->line, 0, |
3212 | "this is the location of the previous definition"); | |
69461e0d | 3213 | } |
69461e0d | 3214 | } |
3215 | ||
70145d81 | 3216 | if (node->type != NT_VOID) |
3217 | _cpp_free_definition (node); | |
3218 | ||
69461e0d | 3219 | /* Enter definition in hash table. */ |
79bd622b | 3220 | node->type = NT_MACRO; |
3221 | node->value.macro = macro; | |
5eb3761c | 3222 | if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")) |
a56d0856 | 3223 | && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS") |
3224 | /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned | |
3225 | in the C standard, as something that one must use in C++. | |
49f161b6 | 3226 | However DR#593 and C++11 indicate that they play no role in C++. |
3227 | We special-case them anyway. */ | |
a56d0856 | 3228 | && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS") |
3229 | && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS")) | |
31ca26b1 | 3230 | node->flags |= NODE_WARN; |
69461e0d | 3231 | |
89768577 | 3232 | /* If user defines one of the conditional macros, remove the |
3233 | conditional flag */ | |
3234 | node->flags &= ~NODE_CONDITIONAL; | |
3235 | ||
79bd622b | 3236 | return ok; |
69461e0d | 3237 | } |
3238 | ||
88cf66fa | 3239 | /* Warn if a token in STRING matches one of a function-like MACRO's |
3240 | parameters. */ | |
bceb9193 | 3241 | static void |
f7fdd7a1 | 3242 | check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro, |
3243 | const cpp_string *string) | |
bceb9193 | 3244 | { |
79bd622b | 3245 | unsigned int i, len; |
4970d4c2 | 3246 | const uchar *p, *q, *limit; |
b1a9ff83 | 3247 | |
bceb9193 | 3248 | /* Loop over the string. */ |
4970d4c2 | 3249 | limit = string->text + string->len - 1; |
3250 | for (p = string->text + 1; p < limit; p = q) | |
bceb9193 | 3251 | { |
bceb9193 | 3252 | /* Find the start of an identifier. */ |
7e118154 | 3253 | while (p < limit && !is_idstart (*p)) |
3254 | p++; | |
bceb9193 | 3255 | |
3256 | /* Find the end of the identifier. */ | |
3257 | q = p; | |
7e118154 | 3258 | while (q < limit && is_idchar (*q)) |
3259 | q++; | |
79bd622b | 3260 | |
3261 | len = q - p; | |
3262 | ||
bceb9193 | 3263 | /* Loop over the function macro arguments to see if the |
3264 | identifier inside the string matches one of them. */ | |
79bd622b | 3265 | for (i = 0; i < macro->paramc; i++) |
3266 | { | |
3267 | const cpp_hashnode *node = macro->params[i]; | |
bceb9193 | 3268 | |
0d086e18 | 3269 | if (NODE_LEN (node) == len |
3270 | && !memcmp (p, NODE_NAME (node), len)) | |
bceb9193 | 3271 | { |
d80d2074 | 3272 | cpp_error (pfile, CPP_DL_WARNING, |
455730ef | 3273 | "macro argument \"%s\" would be stringified in traditional C", |
73328dce | 3274 | NODE_NAME (node)); |
bceb9193 | 3275 | break; |
3276 | } | |
3277 | } | |
3278 | } | |
3279 | } | |
79bd622b | 3280 | |
4d9e9a28 | 3281 | /* Returns the name, arguments and expansion of a macro, in a format |
3282 | suitable to be read back in again, and therefore also for DWARF 2 | |
3283 | debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION". | |
3284 | Caller is expected to generate the "#define" bit if needed. The | |
79bd622b | 3285 | returned text is temporary, and automatically freed later. */ |
79bd622b | 3286 | const unsigned char * |
25692381 | 3287 | cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node) |
79bd622b | 3288 | { |
3289 | unsigned int i, len; | |
25692381 | 3290 | const cpp_macro *macro; |
79bd622b | 3291 | unsigned char *buffer; |
3292 | ||
3293 | if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN)) | |
3294 | { | |
25692381 | 3295 | if (node->type != NT_MACRO |
3296 | || !pfile->cb.user_builtin_macro | |
3297 | || !pfile->cb.user_builtin_macro (pfile, node)) | |
3298 | { | |
3299 | cpp_error (pfile, CPP_DL_ICE, | |
3300 | "invalid hash type %d in cpp_macro_definition", | |
3301 | node->type); | |
3302 | return 0; | |
3303 | } | |
79bd622b | 3304 | } |
3305 | ||
25692381 | 3306 | macro = node->value.macro; |
79bd622b | 3307 | /* Calculate length. */ |
109ca87a | 3308 | len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */ |
79bd622b | 3309 | if (macro->fun_like) |
3310 | { | |
3a68fd94 | 3311 | len += 4; /* "()" plus possible final ".." of named |
3312 | varargs (we have + 1 below). */ | |
79bd622b | 3313 | for (i = 0; i < macro->paramc; i++) |
3a68fd94 | 3314 | len += NODE_LEN (macro->params[i]) + 1; /* "," */ |
79bd622b | 3315 | } |
3316 | ||
9936e07d | 3317 | /* This should match below where we fill in the buffer. */ |
9c343313 | 3318 | if (CPP_OPTION (pfile, traditional)) |
3319 | len += _cpp_replacement_text_len (macro); | |
3320 | else | |
79bd622b | 3321 | { |
941f2388 | 3322 | unsigned int count = macro_real_token_count (macro); |
3323 | for (i = 0; i < count; i++) | |
9c343313 | 3324 | { |
3325 | cpp_token *token = ¯o->exp.tokens[i]; | |
79bd622b | 3326 | |
9c343313 | 3327 | if (token->type == CPP_MACRO_ARG) |
109ca87a | 3328 | len += NODE_LEN (token->val.macro_arg.spelling); |
9c343313 | 3329 | else |
9936e07d | 3330 | len += cpp_token_len (token); |
3331 | ||
9c343313 | 3332 | if (token->flags & STRINGIFY_ARG) |
3333 | len++; /* "#" */ | |
3334 | if (token->flags & PASTE_LEFT) | |
3335 | len += 3; /* " ##" */ | |
9936e07d | 3336 | if (token->flags & PREV_WHITE) |
3337 | len++; /* " " */ | |
9c343313 | 3338 | } |
79bd622b | 3339 | } |
3340 | ||
3341 | if (len > pfile->macro_buffer_len) | |
8744fb7e | 3342 | { |
720aca92 | 3343 | pfile->macro_buffer = XRESIZEVEC (unsigned char, |
3344 | pfile->macro_buffer, len); | |
8744fb7e | 3345 | pfile->macro_buffer_len = len; |
3346 | } | |
4d9e9a28 | 3347 | |
3348 | /* Fill in the buffer. Start with the macro name. */ | |
79bd622b | 3349 | buffer = pfile->macro_buffer; |
109ca87a | 3350 | buffer = _cpp_spell_ident_ucns (buffer, node); |
79bd622b | 3351 | |
3352 | /* Parameter names. */ | |
3353 | if (macro->fun_like) | |
3354 | { | |
3355 | *buffer++ = '('; | |
3356 | for (i = 0; i < macro->paramc; i++) | |
3357 | { | |
3358 | cpp_hashnode *param = macro->params[i]; | |
3359 | ||
3360 | if (param != pfile->spec_nodes.n__VA_ARGS__) | |
3361 | { | |
c86dbc5b | 3362 | memcpy (buffer, NODE_NAME (param), NODE_LEN (param)); |
3363 | buffer += NODE_LEN (param); | |
79bd622b | 3364 | } |
3365 | ||
3366 | if (i + 1 < macro->paramc) | |
b1a9ff83 | 3367 | /* Don't emit a space after the comma here; we're trying |
3368 | to emit a Dwarf-friendly definition, and the Dwarf spec | |
3369 | forbids spaces in the argument list. */ | |
3a68fd94 | 3370 | *buffer++ = ','; |
53c052ca | 3371 | else if (macro->variadic) |
79bd622b | 3372 | *buffer++ = '.', *buffer++ = '.', *buffer++ = '.'; |
3373 | } | |
3374 | *buffer++ = ')'; | |
3375 | } | |
3376 | ||
920e5093 | 3377 | /* The Dwarf spec requires a space after the macro name, even if the |
3378 | definition is the empty string. */ | |
3379 | *buffer++ = ' '; | |
3380 | ||
9c343313 | 3381 | if (CPP_OPTION (pfile, traditional)) |
3382 | buffer = _cpp_copy_replacement_text (macro, buffer); | |
3383 | else if (macro->count) | |
79bd622b | 3384 | /* Expansion tokens. */ |
79bd622b | 3385 | { |
941f2388 | 3386 | unsigned int count = macro_real_token_count (macro); |
3387 | for (i = 0; i < count; i++) | |
79bd622b | 3388 | { |
672f38da | 3389 | cpp_token *token = ¯o->exp.tokens[i]; |
79bd622b | 3390 | |
3391 | if (token->flags & PREV_WHITE) | |
3392 | *buffer++ = ' '; | |
3393 | if (token->flags & STRINGIFY_ARG) | |
3394 | *buffer++ = '#'; | |
3395 | ||
3396 | if (token->type == CPP_MACRO_ARG) | |
3397 | { | |
c86dbc5b | 3398 | memcpy (buffer, |
109ca87a | 3399 | NODE_NAME (token->val.macro_arg.spelling), |
3400 | NODE_LEN (token->val.macro_arg.spelling)); | |
3401 | buffer += NODE_LEN (token->val.macro_arg.spelling); | |
79bd622b | 3402 | } |
3403 | else | |
109ca87a | 3404 | buffer = cpp_spell_token (pfile, token, buffer, true); |
79bd622b | 3405 | |
3406 | if (token->flags & PASTE_LEFT) | |
3407 | { | |
3408 | *buffer++ = ' '; | |
3409 | *buffer++ = '#'; | |
3410 | *buffer++ = '#'; | |
3411 | /* Next has PREV_WHITE; see _cpp_create_definition. */ | |
3412 | } | |
3413 | } | |
3414 | } | |
3415 | ||
3416 | *buffer = '\0'; | |
3417 | return pfile->macro_buffer; | |
3418 | } |