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