Mercurial > hg > CbC > CbC_gcc
comparison libcpp/macro.c @ 111:04ced10e8804
gcc 7
author | kono |
---|---|
date | Fri, 27 Oct 2017 22:46:09 +0900 |
parents | f6334be47118 |
children | 84e7813d76e9 |
comparison
equal
deleted
inserted
replaced
68:561a7518be6b | 111:04ced10e8804 |
---|---|
1 /* Part of CPP library. (Macro and #define handling.) | 1 /* Part of CPP library. (Macro and #define handling.) |
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998, | 2 Copyright (C) 1986-2017 Free Software Foundation, Inc. |
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, | |
4 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. | |
5 Written by Per Bothner, 1994. | 3 Written by Per Bothner, 1994. |
6 Based on CCCP program by Paul Rubin, June 1986 | 4 Based on CCCP program by Paul Rubin, June 1986 |
7 Adapted to ANSI C, Richard Stallman, Jan 1987 | 5 Adapted to ANSI C, Richard Stallman, Jan 1987 |
8 | 6 |
9 This program is free software; you can redistribute it and/or modify it | 7 This program is free software; you can redistribute it and/or modify it |
28 #include "system.h" | 26 #include "system.h" |
29 #include "cpplib.h" | 27 #include "cpplib.h" |
30 #include "internal.h" | 28 #include "internal.h" |
31 | 29 |
32 typedef struct macro_arg macro_arg; | 30 typedef struct macro_arg macro_arg; |
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. */ | |
33 struct macro_arg | 35 struct macro_arg |
34 { | 36 { |
35 const cpp_token **first; /* First token in unexpanded argument. */ | 37 const cpp_token **first; /* First token in unexpanded argument. */ |
36 const cpp_token **expanded; /* Macro-expanded argument. */ | 38 const cpp_token **expanded; /* Macro-expanded argument. */ |
37 const cpp_token *stringified; /* Stringified argument. */ | 39 const cpp_token *stringified; /* Stringified argument. */ |
38 unsigned int count; /* # of tokens in argument. */ | 40 unsigned int count; /* # of tokens in argument. */ |
39 unsigned int expanded_count; /* # of tokens in expanded argument. */ | 41 unsigned int expanded_count; /* # of tokens in expanded argument. */ |
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. */ | |
40 }; | 47 }; |
41 | 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 | |
73 -ftrack-macro-expansion is used this location tracks loci across | |
74 macro expansion. */ | |
75 const source_location *location_ptr; | |
76 #if CHECKING_P | |
77 /* The number of times the iterator went forward. This useful only | |
78 when checking is enabled. */ | |
79 size_t num_forwards; | |
80 #endif | |
81 }; | |
82 | |
83 /* Saved data about an identifier being used as a macro argument | |
84 name. */ | |
85 struct macro_arg_saved_data { | |
86 /* The canonical (UTF-8) spelling of this identifier. */ | |
87 cpp_hashnode *canonical_node; | |
88 /* The previous value of this identifier. */ | |
89 union _cpp_hashnode_value value; | |
90 }; | |
91 | |
42 /* Macro expansion. */ | 92 /* Macro expansion. */ |
43 | 93 |
44 static int enter_macro_context (cpp_reader *, cpp_hashnode *, | 94 static int enter_macro_context (cpp_reader *, cpp_hashnode *, |
45 const cpp_token *); | 95 const cpp_token *, source_location); |
46 static int builtin_macro (cpp_reader *, cpp_hashnode *); | 96 static int builtin_macro (cpp_reader *, cpp_hashnode *, |
97 source_location, source_location); | |
47 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *, | 98 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *, |
48 const cpp_token **, unsigned int); | 99 const cpp_token **, unsigned int); |
100 static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *, | |
101 _cpp_buff *, source_location *, | |
102 const cpp_token **, unsigned int); | |
49 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *, | 103 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *, |
50 _cpp_buff **); | 104 _cpp_buff **, unsigned *); |
51 static cpp_context *next_context (cpp_reader *); | 105 static cpp_context *next_context (cpp_reader *); |
52 static const cpp_token *padding_token (cpp_reader *, const cpp_token *); | 106 static const cpp_token *padding_token (cpp_reader *, const cpp_token *); |
53 static void expand_arg (cpp_reader *, macro_arg *); | 107 static void expand_arg (cpp_reader *, macro_arg *); |
54 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int); | 108 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int); |
55 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *); | 109 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *); |
56 static void paste_all_tokens (cpp_reader *, const cpp_token *); | 110 static void paste_all_tokens (cpp_reader *, const cpp_token *); |
57 static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *); | 111 static bool paste_tokens (cpp_reader *, source_location, |
112 const cpp_token **, const cpp_token *); | |
113 static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t); | |
114 static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *); | |
115 static void delete_macro_args (_cpp_buff*, unsigned num_args); | |
116 static void set_arg_token (macro_arg *, const cpp_token *, | |
117 source_location, size_t, | |
118 enum macro_arg_token_kind, | |
119 bool); | |
120 static const source_location *get_arg_token_location (const macro_arg *, | |
121 enum macro_arg_token_kind); | |
122 static const cpp_token **arg_token_ptr_at (const macro_arg *, | |
123 size_t, | |
124 enum macro_arg_token_kind, | |
125 source_location **virt_location); | |
126 | |
127 static void macro_arg_token_iter_init (macro_arg_token_iter *, bool, | |
128 enum macro_arg_token_kind, | |
129 const macro_arg *, | |
130 const cpp_token **); | |
131 static const cpp_token *macro_arg_token_iter_get_token | |
132 (const macro_arg_token_iter *it); | |
133 static source_location macro_arg_token_iter_get_location | |
134 (const macro_arg_token_iter *); | |
135 static void macro_arg_token_iter_forward (macro_arg_token_iter *); | |
136 static _cpp_buff *tokens_buff_new (cpp_reader *, size_t, | |
137 source_location **); | |
138 static size_t tokens_buff_count (_cpp_buff *); | |
139 static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *); | |
140 static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **, | |
141 source_location *, | |
142 const cpp_token *, | |
143 source_location, | |
144 source_location, | |
145 const line_map_macro *, | |
146 unsigned int); | |
147 | |
148 static const cpp_token **tokens_buff_add_token (_cpp_buff *, | |
149 source_location *, | |
150 const cpp_token *, | |
151 source_location, | |
152 source_location, | |
153 const line_map_macro *, | |
154 unsigned int); | |
155 static inline void tokens_buff_remove_last_token (_cpp_buff *); | |
58 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *, | 156 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *, |
59 macro_arg *); | 157 macro_arg *, source_location); |
60 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *, | 158 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *, |
61 _cpp_buff **); | 159 _cpp_buff **, unsigned *); |
62 static bool create_iso_definition (cpp_reader *, cpp_macro *); | 160 static bool create_iso_definition (cpp_reader *, cpp_macro *); |
63 | 161 |
64 /* #define directive parsing and handling. */ | 162 /* #define directive parsing and handling. */ |
65 | 163 |
66 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *); | 164 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *); |
68 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *, | 166 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *, |
69 const cpp_macro *); | 167 const cpp_macro *); |
70 static bool parse_params (cpp_reader *, cpp_macro *); | 168 static bool parse_params (cpp_reader *, cpp_macro *); |
71 static void check_trad_stringification (cpp_reader *, const cpp_macro *, | 169 static void check_trad_stringification (cpp_reader *, const cpp_macro *, |
72 const cpp_string *); | 170 const cpp_string *); |
171 static bool reached_end_of_context (cpp_context *); | |
172 static void consume_next_token_from_context (cpp_reader *pfile, | |
173 const cpp_token **, | |
174 source_location *); | |
175 static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *); | |
176 | |
177 static cpp_hashnode* macro_of_context (cpp_context *context); | |
178 | |
179 static bool in_macro_expansion_p (cpp_reader *pfile); | |
180 | |
181 /* Statistical counter tracking the number of macros that got | |
182 expanded. */ | |
183 unsigned num_expanded_macros_counter = 0; | |
184 /* Statistical counter tracking the total number tokens resulting | |
185 from macro expansion. */ | |
186 unsigned num_macro_tokens_counter = 0; | |
73 | 187 |
74 /* Emits a warning if NODE is a macro defined in the main file that | 188 /* Emits a warning if NODE is a macro defined in the main file that |
75 has not been used. */ | 189 has not been used. */ |
76 int | 190 int |
77 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node, | 191 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node, |
80 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) | 194 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) |
81 { | 195 { |
82 cpp_macro *macro = node->value.macro; | 196 cpp_macro *macro = node->value.macro; |
83 | 197 |
84 if (!macro->used | 198 if (!macro->used |
85 && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line))) | 199 && MAIN_FILE_P (linemap_check_ordinary |
200 (linemap_lookup (pfile->line_table, | |
201 macro->line)))) | |
86 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0, | 202 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0, |
87 "macro \"%s\" is not used", NODE_NAME (node)); | 203 "macro \"%s\" is not used", NODE_NAME (node)); |
88 } | 204 } |
89 | 205 |
90 return 1; | 206 return 1; |
112 }; | 228 }; |
113 | 229 |
114 /* Helper function for builtin_macro. Returns the text generated by | 230 /* Helper function for builtin_macro. Returns the text generated by |
115 a builtin macro. */ | 231 a builtin macro. */ |
116 const uchar * | 232 const uchar * |
117 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node) | 233 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node, |
118 { | 234 source_location loc) |
119 const struct line_map *map; | 235 { |
120 const uchar *result = NULL; | 236 const uchar *result = NULL; |
121 linenum_type number = 1; | 237 linenum_type number = 1; |
122 | 238 |
123 switch (node->value.builtin) | 239 switch (node->value.builtin) |
124 { | 240 { |
127 NODE_NAME (node)); | 243 NODE_NAME (node)); |
128 break; | 244 break; |
129 | 245 |
130 case BT_TIMESTAMP: | 246 case BT_TIMESTAMP: |
131 { | 247 { |
248 if (CPP_OPTION (pfile, warn_date_time)) | |
249 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent " | |
250 "reproducible builds", NODE_NAME (node)); | |
251 | |
132 cpp_buffer *pbuffer = cpp_get_buffer (pfile); | 252 cpp_buffer *pbuffer = cpp_get_buffer (pfile); |
133 if (pbuffer->timestamp == NULL) | 253 if (pbuffer->timestamp == NULL) |
134 { | 254 { |
135 /* Initialize timestamp value of the assotiated file. */ | 255 /* Initialize timestamp value of the assotiated file. */ |
136 struct _cpp_file *file = cpp_get_file (pbuffer); | 256 struct _cpp_file *file = cpp_get_file (pbuffer); |
169 case BT_BASE_FILE: | 289 case BT_BASE_FILE: |
170 { | 290 { |
171 unsigned int len; | 291 unsigned int len; |
172 const char *name; | 292 const char *name; |
173 uchar *buf; | 293 uchar *buf; |
174 map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line); | 294 |
175 | 295 if (node->value.builtin == BT_FILE) |
176 if (node->value.builtin == BT_BASE_FILE) | 296 name = linemap_get_expansion_filename (pfile->line_table, |
177 while (! MAIN_FILE_P (map)) | 297 pfile->line_table->highest_line); |
178 map = INCLUDED_FROM (pfile->line_table, map); | 298 else |
179 | 299 { |
180 name = map->to_file; | 300 name = _cpp_get_file_name (pfile->main_file); |
301 if (!name) | |
302 abort (); | |
303 } | |
181 len = strlen (name); | 304 len = strlen (name); |
182 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); | 305 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); |
183 result = buf; | 306 result = buf; |
184 *buf = '"'; | 307 *buf = '"'; |
185 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len); | 308 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len); |
194 level 0. */ | 317 level 0. */ |
195 number = pfile->line_table->depth - 1; | 318 number = pfile->line_table->depth - 1; |
196 break; | 319 break; |
197 | 320 |
198 case BT_SPECLINE: | 321 case BT_SPECLINE: |
199 map = &pfile->line_table->maps[pfile->line_table->used-1]; | |
200 /* If __LINE__ is embedded in a macro, it must expand to the | 322 /* If __LINE__ is embedded in a macro, it must expand to the |
201 line of the macro's invocation, not its definition. | 323 line of the macro's invocation, not its definition. |
202 Otherwise things like assert() will not work properly. */ | 324 Otherwise things like assert() will not work properly. |
203 number = SOURCE_LINE (map, | 325 See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */ |
204 CPP_OPTION (pfile, traditional) | 326 if (CPP_OPTION (pfile, traditional)) |
205 ? pfile->line_table->highest_line | 327 loc = pfile->line_table->highest_line; |
206 : pfile->cur_token[-1].src_loc); | 328 else |
329 loc = linemap_resolve_location (pfile->line_table, loc, | |
330 LRK_MACRO_EXPANSION_POINT, NULL); | |
331 number = linemap_get_expansion_line (pfile->line_table, loc); | |
207 break; | 332 break; |
208 | 333 |
209 /* __STDC__ has the value 1 under normal circumstances. | 334 /* __STDC__ has the value 1 under normal circumstances. |
210 However, if (a) we are in a system header, (b) the option | 335 However, if (a) we are in a system header, (b) the option |
211 stdc_0_in_system_headers is true (set by target config), and | 336 stdc_0_in_system_headers is true (set by target config), and |
218 number = 1; | 343 number = 1; |
219 break; | 344 break; |
220 | 345 |
221 case BT_DATE: | 346 case BT_DATE: |
222 case BT_TIME: | 347 case BT_TIME: |
348 if (CPP_OPTION (pfile, warn_date_time)) | |
349 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent " | |
350 "reproducible builds", NODE_NAME (node)); | |
223 if (pfile->date == NULL) | 351 if (pfile->date == NULL) |
224 { | 352 { |
225 /* Allocate __DATE__ and __TIME__ strings from permanent | 353 /* Allocate __DATE__ and __TIME__ strings from permanent |
226 storage. We only do this once, and don't generate them | 354 storage. We only do this once, and don't generate them |
227 at init time, because time() and localtime() are very | 355 at init time, because time() and localtime() are very |
228 slow on some systems. */ | 356 slow on some systems. */ |
229 time_t tt; | 357 time_t tt; |
230 struct tm *tb = NULL; | 358 struct tm *tb = NULL; |
231 | 359 |
232 /* (time_t) -1 is a legitimate value for "number of seconds | 360 /* Set a reproducible timestamp for __DATE__ and __TIME__ macro |
233 since the Epoch", so we have to do a little dance to | 361 if SOURCE_DATE_EPOCH is defined. */ |
234 distinguish that from a genuine error. */ | 362 if (pfile->source_date_epoch == (time_t) -2 |
235 errno = 0; | 363 && pfile->cb.get_source_date_epoch != NULL) |
236 tt = time(NULL); | 364 pfile->source_date_epoch = pfile->cb.get_source_date_epoch (pfile); |
237 if (tt != (time_t)-1 || errno == 0) | 365 |
238 tb = localtime (&tt); | 366 if (pfile->source_date_epoch >= (time_t) 0) |
367 tb = gmtime (&pfile->source_date_epoch); | |
368 else | |
369 { | |
370 /* (time_t) -1 is a legitimate value for "number of seconds | |
371 since the Epoch", so we have to do a little dance to | |
372 distinguish that from a genuine error. */ | |
373 errno = 0; | |
374 tt = time (NULL); | |
375 if (tt != (time_t)-1 || errno == 0) | |
376 tb = localtime (&tt); | |
377 } | |
239 | 378 |
240 if (tb) | 379 if (tb) |
241 { | 380 { |
242 pfile->date = _cpp_unaligned_alloc (pfile, | 381 pfile->date = _cpp_unaligned_alloc (pfile, |
243 sizeof ("\"Oct 11 1347\"")); | 382 sizeof ("\"Oct 11 1347\"")); |
270 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive) | 409 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive) |
271 cpp_error (pfile, CPP_DL_ERROR, | 410 cpp_error (pfile, CPP_DL_ERROR, |
272 "__COUNTER__ expanded inside directive with -fdirectives-only"); | 411 "__COUNTER__ expanded inside directive with -fdirectives-only"); |
273 number = pfile->counter++; | 412 number = pfile->counter++; |
274 break; | 413 break; |
414 | |
415 case BT_HAS_ATTRIBUTE: | |
416 number = pfile->cb.has_attribute (pfile); | |
417 break; | |
275 } | 418 } |
276 | 419 |
277 if (result == NULL) | 420 if (result == NULL) |
278 { | 421 { |
279 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */ | 422 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */ |
285 } | 428 } |
286 | 429 |
287 /* Convert builtin macros like __FILE__ to a token and push it on the | 430 /* Convert builtin macros like __FILE__ to a token and push it on the |
288 context stack. Also handles _Pragma, for which a new token may not | 431 context stack. Also handles _Pragma, for which a new token may not |
289 be created. Returns 1 if it generates a new token context, 0 to | 432 be created. Returns 1 if it generates a new token context, 0 to |
290 return the token to the caller. */ | 433 return the token to the caller. LOC is the location of the expansion |
434 point of the macro. */ | |
291 static int | 435 static int |
292 builtin_macro (cpp_reader *pfile, cpp_hashnode *node) | 436 builtin_macro (cpp_reader *pfile, cpp_hashnode *node, |
437 source_location loc, source_location expand_loc) | |
293 { | 438 { |
294 const uchar *buf; | 439 const uchar *buf; |
295 size_t len; | 440 size_t len; |
296 char *nbuf; | 441 char *nbuf; |
297 | 442 |
300 /* Don't interpret _Pragma within directives. The standard is | 445 /* Don't interpret _Pragma within directives. The standard is |
301 not clear on this, but to me this makes most sense. */ | 446 not clear on this, but to me this makes most sense. */ |
302 if (pfile->state.in_directive) | 447 if (pfile->state.in_directive) |
303 return 0; | 448 return 0; |
304 | 449 |
305 return _cpp_do__Pragma (pfile); | 450 return _cpp_do__Pragma (pfile, loc); |
306 } | 451 } |
307 | 452 |
308 buf = _cpp_builtin_macro_text (pfile, node); | 453 buf = _cpp_builtin_macro_text (pfile, node, expand_loc); |
309 len = ustrlen (buf); | 454 len = ustrlen (buf); |
310 nbuf = (char *) alloca (len + 1); | 455 nbuf = (char *) alloca (len + 1); |
311 memcpy (nbuf, buf, len); | 456 memcpy (nbuf, buf, len); |
312 nbuf[len]='\n'; | 457 nbuf[len]='\n'; |
313 | 458 |
314 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true); | 459 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true); |
315 _cpp_clean_line (pfile); | 460 _cpp_clean_line (pfile); |
316 | 461 |
317 /* Set pfile->cur_token as required by _cpp_lex_direct. */ | 462 /* Set pfile->cur_token as required by _cpp_lex_direct. */ |
318 pfile->cur_token = _cpp_temp_token (pfile); | 463 pfile->cur_token = _cpp_temp_token (pfile); |
319 _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1); | 464 cpp_token *token = _cpp_lex_direct (pfile); |
465 /* We should point to the expansion point of the builtin macro. */ | |
466 token->src_loc = loc; | |
467 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) | |
468 { | |
469 /* We are tracking tokens resulting from macro expansion. | |
470 Create a macro line map and generate a virtual location for | |
471 the token resulting from the expansion of the built-in | |
472 macro. */ | |
473 source_location *virt_locs = NULL; | |
474 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs); | |
475 const line_map_macro * map = | |
476 linemap_enter_macro (pfile->line_table, node, loc, 1); | |
477 tokens_buff_add_token (token_buf, virt_locs, token, | |
478 pfile->line_table->builtin_location, | |
479 pfile->line_table->builtin_location, | |
480 map, /*macro_token_index=*/0); | |
481 push_extended_tokens_context (pfile, node, token_buf, virt_locs, | |
482 (const cpp_token **)token_buf->base, | |
483 1); | |
484 } | |
485 else | |
486 _cpp_push_token_context (pfile, NULL, token, 1); | |
320 if (pfile->buffer->cur != pfile->buffer->rlimit) | 487 if (pfile->buffer->cur != pfile->buffer->rlimit) |
321 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", | 488 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", |
322 NODE_NAME (node)); | 489 NODE_NAME (node)); |
323 _cpp_pop_buffer (pfile); | 490 _cpp_pop_buffer (pfile); |
324 | 491 |
333 { | 500 { |
334 while (len--) | 501 while (len--) |
335 { | 502 { |
336 uchar c = *src++; | 503 uchar c = *src++; |
337 | 504 |
338 if (c == '\\' || c == '"') | 505 switch (c) |
339 { | 506 { |
507 case '\n': | |
508 /* Naked LF can appear in raw string literals */ | |
509 c = 'n'; | |
510 /* FALLTHROUGH */ | |
511 | |
512 case '\\': | |
513 case '"': | |
340 *dest++ = '\\'; | 514 *dest++ = '\\'; |
515 /* FALLTHROUGH */ | |
516 | |
517 default: | |
341 *dest++ = c; | 518 *dest++ = c; |
342 } | 519 } |
343 else | |
344 *dest++ = c; | |
345 } | 520 } |
346 | 521 |
347 return dest; | 522 return dest; |
348 } | 523 } |
349 | 524 |
378 | 553 |
379 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR | 554 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR |
380 || token->type == CPP_WSTRING || token->type == CPP_WCHAR | 555 || token->type == CPP_WSTRING || token->type == CPP_WCHAR |
381 || token->type == CPP_STRING32 || token->type == CPP_CHAR32 | 556 || token->type == CPP_STRING32 || token->type == CPP_CHAR32 |
382 || token->type == CPP_STRING16 || token->type == CPP_CHAR16 | 557 || token->type == CPP_STRING16 || token->type == CPP_CHAR16 |
383 || token->type == CPP_UTF8STRING); | 558 || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR |
559 || cpp_userdef_string_p (token->type) | |
560 || cpp_userdef_char_p (token->type)); | |
384 | 561 |
385 /* Room for each char being written in octal, initial space and | 562 /* Room for each char being written in octal, initial space and |
386 final quote and NUL. */ | 563 final quote and NUL. */ |
387 len = cpp_token_len (token); | 564 len = cpp_token_len (token); |
388 if (escape_it) | 565 if (escape_it) |
438 return new_string_token (pfile, dest - len, len); | 615 return new_string_token (pfile, dest - len, len); |
439 } | 616 } |
440 | 617 |
441 /* Try to paste two tokens. On success, return nonzero. In any | 618 /* Try to paste two tokens. On success, return nonzero. In any |
442 case, PLHS is updated to point to the pasted token, which is | 619 case, PLHS is updated to point to the pasted token, which is |
443 guaranteed to not have the PASTE_LEFT flag set. */ | 620 guaranteed to not have the PASTE_LEFT flag set. LOCATION is |
621 the virtual location used for error reporting. */ | |
444 static bool | 622 static bool |
445 paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs) | 623 paste_tokens (cpp_reader *pfile, source_location location, |
624 const cpp_token **plhs, const cpp_token *rhs) | |
446 { | 625 { |
447 unsigned char *buf, *end, *lhsend; | 626 unsigned char *buf, *end, *lhsend; |
448 cpp_token *lhs; | 627 cpp_token *lhs; |
449 unsigned int len; | 628 unsigned int len; |
450 | 629 |
451 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1; | 630 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1; |
452 buf = (unsigned char *) alloca (len); | 631 buf = (unsigned char *) alloca (len); |
453 end = lhsend = cpp_spell_token (pfile, *plhs, buf, false); | 632 end = lhsend = cpp_spell_token (pfile, *plhs, buf, true); |
454 | 633 |
455 /* Avoid comment headers, since they are still processed in stage 3. | 634 /* Avoid comment headers, since they are still processed in stage 3. |
456 It is simpler to insert a space here, rather than modifying the | 635 It is simpler to insert a space here, rather than modifying the |
457 lexer to ignore comments in some circumstances. Simply returning | 636 lexer to ignore comments in some circumstances. Simply returning |
458 false doesn't work, since we want to clear the PASTE_LEFT flag. */ | 637 false doesn't work, since we want to clear the PASTE_LEFT flag. */ |
459 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ) | 638 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ) |
460 *end++ = ' '; | 639 *end++ = ' '; |
461 /* In one obscure case we might see padding here. */ | 640 /* In one obscure case we might see padding here. */ |
462 if (rhs->type != CPP_PADDING) | 641 if (rhs->type != CPP_PADDING) |
463 end = cpp_spell_token (pfile, rhs, end, false); | 642 end = cpp_spell_token (pfile, rhs, end, true); |
464 *end = '\n'; | 643 *end = '\n'; |
465 | 644 |
466 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true); | 645 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true); |
467 _cpp_clean_line (pfile); | 646 _cpp_clean_line (pfile); |
468 | 647 |
484 lhs->src_loc = saved_loc; | 663 lhs->src_loc = saved_loc; |
485 lhs->flags &= ~PASTE_LEFT; | 664 lhs->flags &= ~PASTE_LEFT; |
486 | 665 |
487 /* Mandatory error for all apart from assembler. */ | 666 /* Mandatory error for all apart from assembler. */ |
488 if (CPP_OPTION (pfile, lang) != CLK_ASM) | 667 if (CPP_OPTION (pfile, lang) != CLK_ASM) |
489 cpp_error (pfile, CPP_DL_ERROR, | 668 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, |
490 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token", | 669 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token", |
491 buf, cpp_token_as_text (pfile, rhs)); | 670 buf, cpp_token_as_text (pfile, rhs)); |
492 return false; | 671 return false; |
493 } | 672 } |
494 | 673 |
505 successful pastes, with the effect that the RHS appears in the | 684 successful pastes, with the effect that the RHS appears in the |
506 output stream after the pasted LHS normally. */ | 685 output stream after the pasted LHS normally. */ |
507 static void | 686 static void |
508 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs) | 687 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs) |
509 { | 688 { |
510 const cpp_token *rhs; | 689 const cpp_token *rhs = NULL; |
511 cpp_context *context = pfile->context; | 690 cpp_context *context = pfile->context; |
691 source_location virt_loc = 0; | |
692 | |
693 /* We are expanding a macro and we must have been called on a token | |
694 that appears at the left hand side of a ## operator. */ | |
695 if (macro_of_context (pfile->context) == NULL | |
696 || (!(lhs->flags & PASTE_LEFT))) | |
697 abort (); | |
698 | |
699 if (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
700 /* The caller must have called consume_next_token_from_context | |
701 right before calling us. That has incremented the pointer to | |
702 the current virtual location. So it now points to the location | |
703 of the token that comes right after *LHS. We want the | |
704 resulting pasted token to have the location of the current | |
705 *LHS, though. */ | |
706 virt_loc = context->c.mc->cur_virt_loc[-1]; | |
707 else | |
708 /* We are not tracking macro expansion. So the best virtual | |
709 location we can get here is the expansion point of the macro we | |
710 are currently expanding. */ | |
711 virt_loc = pfile->invocation_location; | |
512 | 712 |
513 do | 713 do |
514 { | 714 { |
515 /* Take the token directly from the current context. We can do | 715 /* Take the token directly from the current context. We can do |
516 this, because we are in the replacement list of either an | 716 this, because we are in the replacement list of either an |
517 object-like macro, or a function-like macro with arguments | 717 object-like macro, or a function-like macro with arguments |
518 inserted. In either case, the constraints to #define | 718 inserted. In either case, the constraints to #define |
519 guarantee we have at least one more token. */ | 719 guarantee we have at least one more token. */ |
520 if (context->direct_p) | 720 if (context->tokens_kind == TOKENS_KIND_DIRECT) |
521 rhs = FIRST (context).token++; | 721 rhs = FIRST (context).token++; |
522 else | 722 else if (context->tokens_kind == TOKENS_KIND_INDIRECT) |
523 rhs = *FIRST (context).ptoken++; | 723 rhs = *FIRST (context).ptoken++; |
724 else if (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
725 { | |
726 /* So we are in presence of an extended token context, which | |
727 means that each token in this context has a virtual | |
728 location attached to it. So let's not forget to update | |
729 the pointer to the current virtual location of the | |
730 current token when we update the pointer to the current | |
731 token */ | |
732 | |
733 rhs = *FIRST (context).ptoken++; | |
734 /* context->c.mc must be non-null, as if we were not in a | |
735 macro context, context->tokens_kind could not be equal to | |
736 TOKENS_KIND_EXTENDED. */ | |
737 context->c.mc->cur_virt_loc++; | |
738 } | |
524 | 739 |
525 if (rhs->type == CPP_PADDING) | 740 if (rhs->type == CPP_PADDING) |
526 { | 741 { |
527 if (rhs->flags & PASTE_LEFT) | 742 if (rhs->flags & PASTE_LEFT) |
528 abort (); | 743 abort (); |
529 } | 744 } |
530 if (!paste_tokens (pfile, &lhs, rhs)) | 745 if (!paste_tokens (pfile, virt_loc, &lhs, rhs)) |
531 break; | 746 break; |
532 } | 747 } |
533 while (rhs->flags & PASTE_LEFT); | 748 while (rhs->flags & PASTE_LEFT); |
534 | 749 |
535 /* Put the resulting token in its own context. */ | 750 /* Put the resulting token in its own context. */ |
536 _cpp_push_token_context (pfile, NULL, lhs, 1); | 751 if (context->tokens_kind == TOKENS_KIND_EXTENDED) |
752 { | |
753 source_location *virt_locs = NULL; | |
754 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs); | |
755 tokens_buff_add_token (token_buf, virt_locs, lhs, | |
756 virt_loc, 0, NULL, 0); | |
757 push_extended_tokens_context (pfile, context->c.mc->macro_node, | |
758 token_buf, virt_locs, | |
759 (const cpp_token **)token_buf->base, 1); | |
760 } | |
761 else | |
762 _cpp_push_token_context (pfile, NULL, lhs, 1); | |
537 } | 763 } |
538 | 764 |
539 /* Returns TRUE if the number of arguments ARGC supplied in an | 765 /* Returns TRUE if the number of arguments ARGC supplied in an |
540 invocation of the MACRO referenced by NODE is valid. An empty | 766 invocation of the MACRO referenced by NODE is valid. An empty |
541 invocation to a macro with no parameters should pass ARGC as zero. | 767 invocation to a macro with no parameters should pass ARGC as zero. |
548 if (argc == macro->paramc) | 774 if (argc == macro->paramc) |
549 return true; | 775 return true; |
550 | 776 |
551 if (argc < macro->paramc) | 777 if (argc < macro->paramc) |
552 { | 778 { |
553 /* As an extension, a rest argument is allowed to not appear in | 779 /* As an extension, variadic arguments are allowed to not appear in |
554 the invocation at all. | 780 the invocation at all. |
555 e.g. #define debug(format, args...) something | 781 e.g. #define debug(format, args...) something |
556 debug("string"); | 782 debug("string"); |
557 | 783 |
558 This is exactly the same as if there had been an empty rest | 784 This is exactly the same as if an empty variadic list had been |
559 argument - debug("string", ). */ | 785 supplied - debug("string", ). */ |
560 | 786 |
561 if (argc + 1 == macro->paramc && macro->variadic) | 787 if (argc + 1 == macro->paramc && macro->variadic) |
562 { | 788 { |
563 if (CPP_PEDANTIC (pfile) && ! macro->syshdr) | 789 if (CPP_PEDANTIC (pfile) && ! macro->syshdr) |
564 cpp_error (pfile, CPP_DL_PEDWARN, | 790 { |
565 "ISO C99 requires rest arguments to be used"); | 791 if (CPP_OPTION (pfile, cplusplus)) |
792 cpp_error (pfile, CPP_DL_PEDWARN, | |
793 "ISO C++11 requires at least one argument " | |
794 "for the \"...\" in a variadic macro"); | |
795 else | |
796 cpp_error (pfile, CPP_DL_PEDWARN, | |
797 "ISO C99 requires at least one argument " | |
798 "for the \"...\" in a variadic macro"); | |
799 } | |
566 return true; | 800 return true; |
567 } | 801 } |
568 | 802 |
569 cpp_error (pfile, CPP_DL_ERROR, | 803 cpp_error (pfile, CPP_DL_ERROR, |
570 "macro \"%s\" requires %u arguments, but only %u given", | 804 "macro \"%s\" requires %u arguments, but only %u given", |
582 invocation. Assumes the opening parenthesis has been processed. | 816 invocation. Assumes the opening parenthesis has been processed. |
583 If there is an error, emits an appropriate diagnostic and returns | 817 If there is an error, emits an appropriate diagnostic and returns |
584 NULL. Each argument is terminated by a CPP_EOF token, for the | 818 NULL. Each argument is terminated by a CPP_EOF token, for the |
585 future benefit of expand_arg(). If there are any deferred | 819 future benefit of expand_arg(). If there are any deferred |
586 #pragma directives among macro arguments, store pointers to the | 820 #pragma directives among macro arguments, store pointers to the |
587 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer. */ | 821 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer. |
822 | |
823 What is returned is the buffer that contains the memory allocated | |
824 to hold the macro arguments. NODE is the name of the macro this | |
825 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is | |
826 set to the actual number of macro arguments allocated in the | |
827 returned buffer. */ | |
588 static _cpp_buff * | 828 static _cpp_buff * |
589 collect_args (cpp_reader *pfile, const cpp_hashnode *node, | 829 collect_args (cpp_reader *pfile, const cpp_hashnode *node, |
590 _cpp_buff **pragma_buff) | 830 _cpp_buff **pragma_buff, unsigned *num_args) |
591 { | 831 { |
592 _cpp_buff *buff, *base_buff; | 832 _cpp_buff *buff, *base_buff; |
593 cpp_macro *macro; | 833 cpp_macro *macro; |
594 macro_arg *args, *arg; | 834 macro_arg *args, *arg; |
595 const cpp_token *token; | 835 const cpp_token *token; |
596 unsigned int argc; | 836 unsigned int argc; |
837 source_location virt_loc; | |
838 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion); | |
839 unsigned num_args_alloced = 0; | |
597 | 840 |
598 macro = node->value.macro; | 841 macro = node->value.macro; |
599 if (macro->paramc) | 842 if (macro->paramc) |
600 argc = macro->paramc; | 843 argc = macro->paramc; |
601 else | 844 else |
602 argc = 1; | 845 argc = 1; |
603 buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *) | 846 |
847 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50 | |
848 #define ARG_TOKENS_EXTENT 1000 | |
849 | |
850 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG | |
851 * sizeof (cpp_token *) | |
604 + sizeof (macro_arg))); | 852 + sizeof (macro_arg))); |
605 base_buff = buff; | 853 base_buff = buff; |
606 args = (macro_arg *) buff->base; | 854 args = (macro_arg *) buff->base; |
607 memset (args, 0, argc * sizeof (macro_arg)); | 855 memset (args, 0, argc * sizeof (macro_arg)); |
608 buff->cur = (unsigned char *) &args[argc]; | 856 buff->cur = (unsigned char *) &args[argc]; |
613 few. Hence the slightly bizarre usage of "argc" and "arg". */ | 861 few. Hence the slightly bizarre usage of "argc" and "arg". */ |
614 do | 862 do |
615 { | 863 { |
616 unsigned int paren_depth = 0; | 864 unsigned int paren_depth = 0; |
617 unsigned int ntokens = 0; | 865 unsigned int ntokens = 0; |
866 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; | |
867 num_args_alloced++; | |
618 | 868 |
619 argc++; | 869 argc++; |
620 arg->first = (const cpp_token **) buff->cur; | 870 arg->first = (const cpp_token **) buff->cur; |
871 if (track_macro_expansion_p) | |
872 { | |
873 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; | |
874 arg->virt_locs = XNEWVEC (source_location, | |
875 virt_locs_capacity); | |
876 } | |
621 | 877 |
622 for (;;) | 878 for (;;) |
623 { | 879 { |
624 /* Require space for 2 new tokens (including a CPP_EOF). */ | 880 /* Require space for 2 new tokens (including a CPP_EOF). */ |
625 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit) | 881 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit) |
626 { | 882 { |
627 buff = _cpp_append_extend_buff (pfile, buff, | 883 buff = _cpp_append_extend_buff (pfile, buff, |
628 1000 * sizeof (cpp_token *)); | 884 ARG_TOKENS_EXTENT |
885 * sizeof (cpp_token *)); | |
629 arg->first = (const cpp_token **) buff->cur; | 886 arg->first = (const cpp_token **) buff->cur; |
630 } | 887 } |
631 | 888 if (track_macro_expansion_p |
632 token = cpp_get_token (pfile); | 889 && (ntokens + 2 > virt_locs_capacity)) |
890 { | |
891 virt_locs_capacity += ARG_TOKENS_EXTENT; | |
892 arg->virt_locs = XRESIZEVEC (source_location, | |
893 arg->virt_locs, | |
894 virt_locs_capacity); | |
895 } | |
896 | |
897 token = cpp_get_token_1 (pfile, &virt_loc); | |
633 | 898 |
634 if (token->type == CPP_PADDING) | 899 if (token->type == CPP_PADDING) |
635 { | 900 { |
636 /* Drop leading padding. */ | 901 /* Drop leading padding. */ |
637 if (ntokens == 0) | 902 if (ntokens == 0) |
684 } | 949 } |
685 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token; | 950 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token; |
686 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *); | 951 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *); |
687 if (token->type == CPP_PRAGMA_EOL) | 952 if (token->type == CPP_PRAGMA_EOL) |
688 break; | 953 break; |
689 token = cpp_get_token (pfile); | 954 token = cpp_get_token_1 (pfile, &virt_loc); |
690 } | 955 } |
691 while (token->type != CPP_EOF); | 956 while (token->type != CPP_EOF); |
692 | 957 |
693 /* In deferred pragmas parsing_args and prevent_expansion | 958 /* In deferred pragmas parsing_args and prevent_expansion |
694 had been changed, reset it. */ | 959 had been changed, reset it. */ |
698 if (token->type == CPP_EOF) | 963 if (token->type == CPP_EOF) |
699 break; | 964 break; |
700 else | 965 else |
701 continue; | 966 continue; |
702 } | 967 } |
703 | 968 set_arg_token (arg, token, virt_loc, |
704 arg->first[ntokens++] = token; | 969 ntokens, MACRO_ARG_TOKEN_NORMAL, |
970 CPP_OPTION (pfile, track_macro_expansion)); | |
971 ntokens++; | |
705 } | 972 } |
706 | 973 |
707 /* Drop trailing padding. */ | 974 /* Drop trailing padding. */ |
708 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING) | 975 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING) |
709 ntokens--; | 976 ntokens--; |
710 | 977 |
711 arg->count = ntokens; | 978 arg->count = ntokens; |
712 arg->first[ntokens] = &pfile->eof; | 979 set_arg_token (arg, &pfile->eof, pfile->eof.src_loc, |
980 ntokens, MACRO_ARG_TOKEN_NORMAL, | |
981 CPP_OPTION (pfile, track_macro_expansion)); | |
713 | 982 |
714 /* Terminate the argument. Excess arguments loop back and | 983 /* Terminate the argument. Excess arguments loop back and |
715 overwrite the final legitimate argument, before failing. */ | 984 overwrite the final legitimate argument, before failing. */ |
716 if (argc <= macro->paramc) | 985 if (argc <= macro->paramc) |
717 { | 986 { |
750 If FIRST is NULL replace_args () swallows the comma. */ | 1019 If FIRST is NULL replace_args () swallows the comma. */ |
751 if (macro->variadic && (argc < macro->paramc | 1020 if (macro->variadic && (argc < macro->paramc |
752 || (argc == 1 && args[0].count == 0 | 1021 || (argc == 1 && args[0].count == 0 |
753 && !CPP_OPTION (pfile, std)))) | 1022 && !CPP_OPTION (pfile, std)))) |
754 args[macro->paramc - 1].first = NULL; | 1023 args[macro->paramc - 1].first = NULL; |
1024 if (num_args) | |
1025 *num_args = num_args_alloced; | |
755 return base_buff; | 1026 return base_buff; |
756 } | 1027 } |
757 } | 1028 } |
758 | 1029 |
759 /* An error occurred. */ | 1030 /* An error occurred. */ |
763 | 1034 |
764 /* Search for an opening parenthesis to the macro of NODE, in such a | 1035 /* Search for an opening parenthesis to the macro of NODE, in such a |
765 way that, if none is found, we don't lose the information in any | 1036 way that, if none is found, we don't lose the information in any |
766 intervening padding tokens. If we find the parenthesis, collect | 1037 intervening padding tokens. If we find the parenthesis, collect |
767 the arguments and return the buffer containing them. PRAGMA_BUFF | 1038 the arguments and return the buffer containing them. PRAGMA_BUFF |
768 argument is the same as in collect_args. */ | 1039 argument is the same as in collect_args. If NUM_ARGS is non-NULL, |
1040 *NUM_ARGS is set to the number of arguments contained in the | |
1041 returned buffer. */ | |
769 static _cpp_buff * | 1042 static _cpp_buff * |
770 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node, | 1043 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node, |
771 _cpp_buff **pragma_buff) | 1044 _cpp_buff **pragma_buff, unsigned *num_args) |
772 { | 1045 { |
773 const cpp_token *token, *padding = NULL; | 1046 const cpp_token *token, *padding = NULL; |
774 | 1047 |
775 for (;;) | 1048 for (;;) |
776 { | 1049 { |
783 } | 1056 } |
784 | 1057 |
785 if (token->type == CPP_OPEN_PAREN) | 1058 if (token->type == CPP_OPEN_PAREN) |
786 { | 1059 { |
787 pfile->state.parsing_args = 2; | 1060 pfile->state.parsing_args = 2; |
788 return collect_args (pfile, node, pragma_buff); | 1061 return collect_args (pfile, node, pragma_buff, num_args); |
789 } | 1062 } |
790 | 1063 |
791 /* CPP_EOF can be the end of macro arguments, or the end of the | 1064 /* CPP_EOF can be the end of macro arguments, or the end of the |
792 file. We mustn't back up over the latter. Ugh. */ | 1065 file. We mustn't back up over the latter. Ugh. */ |
793 if (token->type != CPP_EOF || token == &pfile->eof) | 1066 if (token->type != CPP_EOF || token == &pfile->eof) |
817 } | 1090 } |
818 | 1091 |
819 /* Push the context of a macro with hash entry NODE onto the context | 1092 /* Push the context of a macro with hash entry NODE onto the context |
820 stack. If we can successfully expand the macro, we push a context | 1093 stack. If we can successfully expand the macro, we push a context |
821 containing its yet-to-be-rescanned replacement list and return one. | 1094 containing its yet-to-be-rescanned replacement list and return one. |
822 If there were additionally any unexpanded deferred #pragma directives | 1095 If there were additionally any unexpanded deferred #pragma |
823 among macro arguments, push another context containing the | 1096 directives among macro arguments, push another context containing |
824 pragma tokens before the yet-to-be-rescanned replacement list | 1097 the pragma tokens before the yet-to-be-rescanned replacement list |
825 and return two. Otherwise, we don't push a context and return zero. */ | 1098 and return two. Otherwise, we don't push a context and return |
1099 zero. LOCATION is the location of the expansion point of the | |
1100 macro. */ | |
826 static int | 1101 static int |
827 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node, | 1102 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node, |
828 const cpp_token *result) | 1103 const cpp_token *result, source_location location) |
829 { | 1104 { |
830 /* The presence of a macro invalidates a file's controlling macro. */ | 1105 /* The presence of a macro invalidates a file's controlling macro. */ |
831 pfile->mi_valid = false; | 1106 pfile->mi_valid = false; |
832 | 1107 |
833 pfile->state.angled_headers = false; | 1108 pfile->state.angled_headers = false; |
1109 | |
1110 /* From here to when we push the context for the macro later down | |
1111 this function, we need to flag the fact that we are about to | |
1112 expand a macro. This is useful when -ftrack-macro-expansion is | |
1113 turned off. In that case, we need to record the location of the | |
1114 expansion point of the top-most macro we are about to to expand, | |
1115 into pfile->invocation_location. But we must not record any such | |
1116 location once the process of expanding the macro starts; that is, | |
1117 we must not do that recording between now and later down this | |
1118 function where set this flag to FALSE. */ | |
1119 pfile->about_to_expand_macro_p = true; | |
834 | 1120 |
835 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED)) | 1121 if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED)) |
836 { | 1122 { |
837 node->flags |= NODE_USED; | 1123 node->flags |= NODE_USED; |
838 if ((!pfile->cb.user_builtin_macro | 1124 if ((!pfile->cb.user_builtin_macro |
848 _cpp_buff *pragma_buff = NULL; | 1134 _cpp_buff *pragma_buff = NULL; |
849 | 1135 |
850 if (macro->fun_like) | 1136 if (macro->fun_like) |
851 { | 1137 { |
852 _cpp_buff *buff; | 1138 _cpp_buff *buff; |
1139 unsigned num_args = 0; | |
853 | 1140 |
854 pfile->state.prevent_expansion++; | 1141 pfile->state.prevent_expansion++; |
855 pfile->keep_tokens++; | 1142 pfile->keep_tokens++; |
856 pfile->state.parsing_args = 1; | 1143 pfile->state.parsing_args = 1; |
857 buff = funlike_invocation_p (pfile, node, &pragma_buff); | 1144 buff = funlike_invocation_p (pfile, node, &pragma_buff, |
1145 &num_args); | |
858 pfile->state.parsing_args = 0; | 1146 pfile->state.parsing_args = 0; |
859 pfile->keep_tokens--; | 1147 pfile->keep_tokens--; |
860 pfile->state.prevent_expansion--; | 1148 pfile->state.prevent_expansion--; |
861 | 1149 |
862 if (buff == NULL) | 1150 if (buff == NULL) |
867 NODE_NAME (node)); | 1155 NODE_NAME (node)); |
868 | 1156 |
869 if (pragma_buff) | 1157 if (pragma_buff) |
870 _cpp_release_buff (pfile, pragma_buff); | 1158 _cpp_release_buff (pfile, pragma_buff); |
871 | 1159 |
1160 pfile->about_to_expand_macro_p = false; | |
872 return 0; | 1161 return 0; |
873 } | 1162 } |
874 | 1163 |
875 if (macro->paramc > 0) | 1164 if (macro->paramc > 0) |
876 replace_args (pfile, node, macro, (macro_arg *) buff->base); | 1165 replace_args (pfile, node, macro, |
877 _cpp_release_buff (pfile, buff); | 1166 (macro_arg *) buff->base, |
1167 location); | |
1168 /* Free the memory used by the arguments of this | |
1169 function-like macro. This memory has been allocated by | |
1170 funlike_invocation_p and by replace_args. */ | |
1171 delete_macro_args (buff, num_args); | |
878 } | 1172 } |
879 | 1173 |
880 /* Disable the macro within its expansion. */ | 1174 /* Disable the macro within its expansion. */ |
881 node->flags |= NODE_DISABLED; | 1175 node->flags |= NODE_DISABLED; |
882 | 1176 |
886 if (pfile->cb.used_define) | 1180 if (pfile->cb.used_define) |
887 pfile->cb.used_define (pfile, pfile->directive_line, node); | 1181 pfile->cb.used_define (pfile, pfile->directive_line, node); |
888 } | 1182 } |
889 | 1183 |
890 if (pfile->cb.used) | 1184 if (pfile->cb.used) |
891 pfile->cb.used (pfile, result->src_loc, node); | 1185 pfile->cb.used (pfile, location, node); |
892 | 1186 |
893 macro->used = 1; | 1187 macro->used = 1; |
894 | 1188 |
895 if (macro->paramc == 0) | 1189 if (macro->paramc == 0) |
896 _cpp_push_token_context (pfile, node, macro->exp.tokens, | 1190 { |
897 macro_real_token_count (macro)); | 1191 unsigned tokens_count = macro_real_token_count (macro); |
1192 if (CPP_OPTION (pfile, track_macro_expansion)) | |
1193 { | |
1194 unsigned int i; | |
1195 const cpp_token *src = macro->exp.tokens; | |
1196 const line_map_macro *map; | |
1197 source_location *virt_locs = NULL; | |
1198 _cpp_buff *macro_tokens | |
1199 = tokens_buff_new (pfile, tokens_count, &virt_locs); | |
1200 | |
1201 /* Create a macro map to record the locations of the | |
1202 tokens that are involved in the expansion. LOCATION | |
1203 is the location of the macro expansion point. */ | |
1204 map = linemap_enter_macro (pfile->line_table, | |
1205 node, location, tokens_count); | |
1206 for (i = 0; i < tokens_count; ++i) | |
1207 { | |
1208 tokens_buff_add_token (macro_tokens, virt_locs, | |
1209 src, src->src_loc, | |
1210 src->src_loc, map, i); | |
1211 ++src; | |
1212 } | |
1213 push_extended_tokens_context (pfile, node, | |
1214 macro_tokens, | |
1215 virt_locs, | |
1216 (const cpp_token **) | |
1217 macro_tokens->base, | |
1218 tokens_count); | |
1219 } | |
1220 else | |
1221 _cpp_push_token_context (pfile, node, macro->exp.tokens, | |
1222 tokens_count); | |
1223 num_macro_tokens_counter += tokens_count; | |
1224 } | |
898 | 1225 |
899 if (pragma_buff) | 1226 if (pragma_buff) |
900 { | 1227 { |
901 if (!pfile->state.in_directive) | 1228 if (!pfile->state.in_directive) |
902 _cpp_push_token_context (pfile, NULL, | 1229 _cpp_push_token_context (pfile, NULL, |
903 padding_token (pfile, result), 1); | 1230 padding_token (pfile, result), 1); |
904 do | 1231 do |
905 { | 1232 { |
1233 unsigned tokens_count; | |
906 _cpp_buff *tail = pragma_buff->next; | 1234 _cpp_buff *tail = pragma_buff->next; |
907 pragma_buff->next = NULL; | 1235 pragma_buff->next = NULL; |
1236 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff) | |
1237 - (const cpp_token **) pragma_buff->base); | |
908 push_ptoken_context (pfile, NULL, pragma_buff, | 1238 push_ptoken_context (pfile, NULL, pragma_buff, |
909 (const cpp_token **) pragma_buff->base, | 1239 (const cpp_token **) pragma_buff->base, |
910 ((const cpp_token **) BUFF_FRONT (pragma_buff) | 1240 tokens_count); |
911 - (const cpp_token **) pragma_buff->base)); | |
912 pragma_buff = tail; | 1241 pragma_buff = tail; |
1242 if (!CPP_OPTION (pfile, track_macro_expansion)) | |
1243 num_macro_tokens_counter += tokens_count; | |
1244 | |
913 } | 1245 } |
914 while (pragma_buff != NULL); | 1246 while (pragma_buff != NULL); |
1247 pfile->about_to_expand_macro_p = false; | |
915 return 2; | 1248 return 2; |
916 } | 1249 } |
917 | 1250 |
1251 pfile->about_to_expand_macro_p = false; | |
918 return 1; | 1252 return 1; |
919 } | 1253 } |
920 | 1254 |
1255 pfile->about_to_expand_macro_p = false; | |
921 /* Handle built-in macros and the _Pragma operator. */ | 1256 /* Handle built-in macros and the _Pragma operator. */ |
922 return builtin_macro (pfile, node); | 1257 { |
1258 source_location loc, expand_loc; | |
1259 | |
1260 if (/* The top-level macro invocation that triggered the expansion | |
1261 we are looking at is with a standard macro ...*/ | |
1262 !(pfile->top_most_macro_node->flags & NODE_BUILTIN) | |
1263 /* ... and it's a function-like macro invocation. */ | |
1264 && pfile->top_most_macro_node->value.macro->fun_like) | |
1265 { | |
1266 /* Then the location of the end of the macro invocation is the | |
1267 location of the closing parenthesis. */ | |
1268 loc = pfile->cur_token[-1].src_loc; | |
1269 expand_loc = loc; | |
1270 } | |
1271 else | |
1272 { | |
1273 /* Otherwise, the location of the end of the macro invocation is | |
1274 the location of the expansion point of that top-level macro | |
1275 invocation. */ | |
1276 loc = location; | |
1277 expand_loc = pfile->invocation_location; | |
1278 } | |
1279 | |
1280 return builtin_macro (pfile, node, loc, expand_loc); | |
1281 } | |
1282 } | |
1283 | |
1284 /* De-allocate the memory used by BUFF which is an array of instances | |
1285 of macro_arg. NUM_ARGS is the number of instances of macro_arg | |
1286 present in BUFF. */ | |
1287 static void | |
1288 delete_macro_args (_cpp_buff *buff, unsigned num_args) | |
1289 { | |
1290 macro_arg *macro_args; | |
1291 unsigned i; | |
1292 | |
1293 if (buff == NULL) | |
1294 return; | |
1295 | |
1296 macro_args = (macro_arg *) buff->base; | |
1297 | |
1298 /* Walk instances of macro_arg to free their expanded tokens as well | |
1299 as their macro_arg::virt_locs members. */ | |
1300 for (i = 0; i < num_args; ++i) | |
1301 { | |
1302 if (macro_args[i].expanded) | |
1303 { | |
1304 free (macro_args[i].expanded); | |
1305 macro_args[i].expanded = NULL; | |
1306 } | |
1307 if (macro_args[i].virt_locs) | |
1308 { | |
1309 free (macro_args[i].virt_locs); | |
1310 macro_args[i].virt_locs = NULL; | |
1311 } | |
1312 if (macro_args[i].expanded_virt_locs) | |
1313 { | |
1314 free (macro_args[i].expanded_virt_locs); | |
1315 macro_args[i].expanded_virt_locs = NULL; | |
1316 } | |
1317 } | |
1318 _cpp_free_buff (buff); | |
1319 } | |
1320 | |
1321 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token | |
1322 to set, LOCATION is its virtual location. "Virtual" location means | |
1323 the location that encodes loci across macro expansion. Otherwise | |
1324 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the | |
1325 argument ARG is supposed to contain. Note that ARG must be | |
1326 tailored so that it has enough room to contain INDEX + 1 numbers of | |
1327 tokens, at least. */ | |
1328 static void | |
1329 set_arg_token (macro_arg *arg, const cpp_token *token, | |
1330 source_location location, size_t index, | |
1331 enum macro_arg_token_kind kind, | |
1332 bool track_macro_exp_p) | |
1333 { | |
1334 const cpp_token **token_ptr; | |
1335 source_location *loc = NULL; | |
1336 | |
1337 token_ptr = | |
1338 arg_token_ptr_at (arg, index, kind, | |
1339 track_macro_exp_p ? &loc : NULL); | |
1340 *token_ptr = token; | |
1341 | |
1342 if (loc != NULL) | |
1343 { | |
1344 /* We can't set the location of a stringified argument | |
1345 token and we can't set any location if we aren't tracking | |
1346 macro expansion locations. */ | |
1347 gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED | |
1348 && track_macro_exp_p); | |
1349 *loc = location; | |
1350 } | |
1351 } | |
1352 | |
1353 /* Get the pointer to the location of the argument token of the | |
1354 function-like macro argument ARG. This function must be called | |
1355 only when we -ftrack-macro-expansion is on. */ | |
1356 static const source_location * | |
1357 get_arg_token_location (const macro_arg *arg, | |
1358 enum macro_arg_token_kind kind) | |
1359 { | |
1360 const source_location *loc = NULL; | |
1361 const cpp_token **token_ptr = | |
1362 arg_token_ptr_at (arg, 0, kind, (source_location **) &loc); | |
1363 | |
1364 if (token_ptr == NULL) | |
1365 return NULL; | |
1366 | |
1367 return loc; | |
1368 } | |
1369 | |
1370 /* Return the pointer to the INDEXth token of the macro argument ARG. | |
1371 KIND specifies the kind of token the macro argument ARG contains. | |
1372 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address | |
1373 of the virtual location of the returned token if the | |
1374 -ftrack-macro-expansion flag is on; otherwise, it's set to the | |
1375 spelling location of the returned token. */ | |
1376 static const cpp_token ** | |
1377 arg_token_ptr_at (const macro_arg *arg, size_t index, | |
1378 enum macro_arg_token_kind kind, | |
1379 source_location **virt_location) | |
1380 { | |
1381 const cpp_token **tokens_ptr = NULL; | |
1382 | |
1383 switch (kind) | |
1384 { | |
1385 case MACRO_ARG_TOKEN_NORMAL: | |
1386 tokens_ptr = arg->first; | |
1387 break; | |
1388 case MACRO_ARG_TOKEN_STRINGIFIED: | |
1389 tokens_ptr = (const cpp_token **) &arg->stringified; | |
1390 break; | |
1391 case MACRO_ARG_TOKEN_EXPANDED: | |
1392 tokens_ptr = arg->expanded; | |
1393 break; | |
1394 } | |
1395 | |
1396 if (tokens_ptr == NULL) | |
1397 /* This can happen for e.g, an empty token argument to a | |
1398 funtion-like macro. */ | |
1399 return tokens_ptr; | |
1400 | |
1401 if (virt_location) | |
1402 { | |
1403 if (kind == MACRO_ARG_TOKEN_NORMAL) | |
1404 *virt_location = &arg->virt_locs[index]; | |
1405 else if (kind == MACRO_ARG_TOKEN_EXPANDED) | |
1406 *virt_location = &arg->expanded_virt_locs[index]; | |
1407 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED) | |
1408 *virt_location = | |
1409 (source_location *) &tokens_ptr[index]->src_loc; | |
1410 } | |
1411 return &tokens_ptr[index]; | |
1412 } | |
1413 | |
1414 /* Initialize an iterator so that it iterates over the tokens of a | |
1415 function-like macro argument. KIND is the kind of tokens we want | |
1416 ITER to iterate over. TOKEN_PTR points the first token ITER will | |
1417 iterate over. */ | |
1418 static void | |
1419 macro_arg_token_iter_init (macro_arg_token_iter *iter, | |
1420 bool track_macro_exp_p, | |
1421 enum macro_arg_token_kind kind, | |
1422 const macro_arg *arg, | |
1423 const cpp_token **token_ptr) | |
1424 { | |
1425 iter->track_macro_exp_p = track_macro_exp_p; | |
1426 iter->kind = kind; | |
1427 iter->token_ptr = token_ptr; | |
1428 /* Unconditionally initialize this so that the compiler doesn't warn | |
1429 about iter->location_ptr being possibly uninitialized later after | |
1430 this code has been inlined somewhere. */ | |
1431 iter->location_ptr = NULL; | |
1432 if (track_macro_exp_p) | |
1433 iter->location_ptr = get_arg_token_location (arg, kind); | |
1434 #if CHECKING_P | |
1435 iter->num_forwards = 0; | |
1436 if (track_macro_exp_p | |
1437 && token_ptr != NULL | |
1438 && iter->location_ptr == NULL) | |
1439 abort (); | |
1440 #endif | |
1441 } | |
1442 | |
1443 /* Move the iterator one token forward. Note that if IT was | |
1444 initialized on an argument that has a stringified token, moving it | |
1445 forward doesn't make sense as a stringified token is essentially one | |
1446 string. */ | |
1447 static void | |
1448 macro_arg_token_iter_forward (macro_arg_token_iter *it) | |
1449 { | |
1450 switch (it->kind) | |
1451 { | |
1452 case MACRO_ARG_TOKEN_NORMAL: | |
1453 case MACRO_ARG_TOKEN_EXPANDED: | |
1454 it->token_ptr++; | |
1455 if (it->track_macro_exp_p) | |
1456 it->location_ptr++; | |
1457 break; | |
1458 case MACRO_ARG_TOKEN_STRINGIFIED: | |
1459 #if CHECKING_P | |
1460 if (it->num_forwards > 0) | |
1461 abort (); | |
1462 #endif | |
1463 break; | |
1464 } | |
1465 | |
1466 #if CHECKING_P | |
1467 it->num_forwards++; | |
1468 #endif | |
1469 } | |
1470 | |
1471 /* Return the token pointed to by the iterator. */ | |
1472 static const cpp_token * | |
1473 macro_arg_token_iter_get_token (const macro_arg_token_iter *it) | |
1474 { | |
1475 #if CHECKING_P | |
1476 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED | |
1477 && it->num_forwards > 0) | |
1478 abort (); | |
1479 #endif | |
1480 if (it->token_ptr == NULL) | |
1481 return NULL; | |
1482 return *it->token_ptr; | |
1483 } | |
1484 | |
1485 /* Return the location of the token pointed to by the iterator.*/ | |
1486 static source_location | |
1487 macro_arg_token_iter_get_location (const macro_arg_token_iter *it) | |
1488 { | |
1489 #if CHECKING_P | |
1490 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED | |
1491 && it->num_forwards > 0) | |
1492 abort (); | |
1493 #endif | |
1494 if (it->track_macro_exp_p) | |
1495 return *it->location_ptr; | |
1496 else | |
1497 return (*it->token_ptr)->src_loc; | |
1498 } | |
1499 | |
1500 /* Return the index of a token [resulting from macro expansion] inside | |
1501 the total list of tokens resulting from a given macro | |
1502 expansion. The index can be different depending on whether if we | |
1503 want each tokens resulting from function-like macro arguments | |
1504 expansion to have a different location or not. | |
1505 | |
1506 E.g, consider this function-like macro: | |
1507 | |
1508 #define M(x) x - 3 | |
1509 | |
1510 Then consider us "calling" it (and thus expanding it) like: | |
1511 | |
1512 M(1+4) | |
1513 | |
1514 It will be expanded into: | |
1515 | |
1516 1+4-3 | |
1517 | |
1518 Let's consider the case of the token '4'. | |
1519 | |
1520 Its index can be 2 (it's the third token of the set of tokens | |
1521 resulting from the expansion) or it can be 0 if we consider that | |
1522 all tokens resulting from the expansion of the argument "1+2" have | |
1523 the same index, which is 0. In this later case, the index of token | |
1524 '-' would then be 1 and the index of token '3' would be 2. | |
1525 | |
1526 The later case is useful to use less memory e.g, for the case of | |
1527 the user using the option -ftrack-macro-expansion=1. | |
1528 | |
1529 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we | |
1530 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro | |
1531 parameter (inside the macro replacement list) that corresponds to | |
1532 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index | |
1533 of. | |
1534 | |
1535 If we refer to the example above, for the '4' argument token, | |
1536 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN | |
1537 would be set to the token 'x', in the replacement list "x - 3" of | |
1538 macro M. | |
1539 | |
1540 This is a subroutine of replace_args. */ | |
1541 inline static unsigned | |
1542 expanded_token_index (cpp_reader *pfile, cpp_macro *macro, | |
1543 const cpp_token *cur_replacement_token, | |
1544 unsigned absolute_token_index) | |
1545 { | |
1546 if (CPP_OPTION (pfile, track_macro_expansion) > 1) | |
1547 return absolute_token_index; | |
1548 return cur_replacement_token - macro->exp.tokens; | |
923 } | 1549 } |
924 | 1550 |
925 /* Replace the parameters in a function-like macro of NODE with the | 1551 /* Replace the parameters in a function-like macro of NODE with the |
926 actual ARGS, and place the result in a newly pushed token context. | 1552 actual ARGS, and place the result in a newly pushed token context. |
927 Expand each argument before replacing, unless it is operated upon | 1553 Expand each argument before replacing, unless it is operated upon |
928 by the # or ## operators. */ | 1554 by the # or ## operators. EXPANSION_POINT_LOC is the location of |
1555 the expansion point of the macro. E.g, the location of the | |
1556 function-like macro invocation. */ | |
929 static void | 1557 static void |
930 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args) | 1558 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, |
1559 macro_arg *args, source_location expansion_point_loc) | |
931 { | 1560 { |
932 unsigned int i, total; | 1561 unsigned int i, total; |
933 const cpp_token *src, *limit; | 1562 const cpp_token *src, *limit; |
934 const cpp_token **dest, **first; | 1563 const cpp_token **first = NULL; |
935 macro_arg *arg; | 1564 macro_arg *arg; |
936 _cpp_buff *buff; | 1565 _cpp_buff *buff = NULL; |
937 unsigned int count; | 1566 source_location *virt_locs = NULL; |
1567 unsigned int exp_count; | |
1568 const line_map_macro *map = NULL; | |
1569 int track_macro_exp; | |
938 | 1570 |
939 /* First, fully macro-expand arguments, calculating the number of | 1571 /* First, fully macro-expand arguments, calculating the number of |
940 tokens in the final expansion as we go. The ordering of the if | 1572 tokens in the final expansion as we go. The ordering of the if |
941 statements below is subtle; we must handle stringification before | 1573 statements below is subtle; we must handle stringification before |
942 pasting. */ | 1574 pasting. */ |
943 count = macro_real_token_count (macro); | 1575 |
944 total = count; | 1576 /* EXP_COUNT is the number of tokens in the macro replacement |
945 limit = macro->exp.tokens + count; | 1577 list. TOTAL is the number of tokens /after/ macro parameters |
1578 have been replaced by their arguments. */ | |
1579 exp_count = macro_real_token_count (macro); | |
1580 total = exp_count; | |
1581 limit = macro->exp.tokens + exp_count; | |
946 | 1582 |
947 for (src = macro->exp.tokens; src < limit; src++) | 1583 for (src = macro->exp.tokens; src < limit; src++) |
948 if (src->type == CPP_MACRO_ARG) | 1584 if (src->type == CPP_MACRO_ARG) |
949 { | 1585 { |
950 /* Leading and trailing padding tokens. */ | 1586 /* Leading and trailing padding tokens. */ |
951 total += 2; | 1587 total += 2; |
1588 /* Account for leading and padding tokens in exp_count too. | |
1589 This is going to be important later down this function, | |
1590 when we want to handle the case of (track_macro_exp < | |
1591 2). */ | |
1592 exp_count += 2; | |
952 | 1593 |
953 /* We have an argument. If it is not being stringified or | 1594 /* We have an argument. If it is not being stringified or |
954 pasted it is macro-replaced before insertion. */ | 1595 pasted it is macro-replaced before insertion. */ |
955 arg = &args[src->val.macro_arg.arg_no - 1]; | 1596 arg = &args[src->val.macro_arg.arg_no - 1]; |
956 | 1597 |
968 expand_arg (pfile, arg); | 1609 expand_arg (pfile, arg); |
969 total += arg->expanded_count - 1; | 1610 total += arg->expanded_count - 1; |
970 } | 1611 } |
971 } | 1612 } |
972 | 1613 |
973 /* Now allocate space for the expansion, copy the tokens and replace | 1614 /* When the compiler is called with the -ftrack-macro-expansion |
974 the arguments. */ | 1615 flag, we need to keep track of the location of each token that |
975 buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *)); | 1616 results from macro expansion. |
1617 | |
1618 A token resulting from macro expansion is not a new token. It is | |
1619 simply the same token as the token coming from the macro | |
1620 definition. The new things that are allocated are the buffer | |
1621 that holds the tokens resulting from macro expansion and a new | |
1622 location that records many things like the locus of the expansion | |
1623 point as well as the original locus inside the definition of the | |
1624 macro. This location is called a virtual location. | |
1625 | |
1626 So the buffer BUFF holds a set of cpp_token*, and the buffer | |
1627 VIRT_LOCS holds the virtual locations of the tokens held by BUFF. | |
1628 | |
1629 Both of these two buffers are going to be hung off of the macro | |
1630 context, when the latter is pushed. The memory allocated to | |
1631 store the tokens and their locations is going to be freed once | |
1632 the context of macro expansion is popped. | |
1633 | |
1634 As far as tokens are concerned, the memory overhead of | |
1635 -ftrack-macro-expansion is proportional to the number of | |
1636 macros that get expanded multiplied by sizeof (source_location). | |
1637 The good news is that extra memory gets freed when the macro | |
1638 context is freed, i.e shortly after the macro got expanded. */ | |
1639 | |
1640 /* Is the -ftrack-macro-expansion flag in effect? */ | |
1641 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion); | |
1642 | |
1643 /* Now allocate memory space for tokens and locations resulting from | |
1644 the macro expansion, copy the tokens and replace the arguments. | |
1645 This memory must be freed when the context of the macro MACRO is | |
1646 popped. */ | |
1647 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL); | |
1648 | |
976 first = (const cpp_token **) buff->base; | 1649 first = (const cpp_token **) buff->base; |
977 dest = first; | 1650 |
978 | 1651 /* Create a macro map to record the locations of the tokens that are |
1652 involved in the expansion. Note that the expansion point is set | |
1653 to the location of the closing parenthesis. Otherwise, the | |
1654 subsequent map created for the first token that comes after the | |
1655 macro map might have a wrong line number. That would lead to | |
1656 tokens with wrong line numbers after the macro expansion. This | |
1657 adds up to the memory overhead of the -ftrack-macro-expansion | |
1658 flag; for every macro that is expanded, a "macro map" is | |
1659 created. */ | |
1660 if (track_macro_exp) | |
1661 { | |
1662 int num_macro_tokens = total; | |
1663 if (track_macro_exp < 2) | |
1664 /* Then the number of macro tokens won't take in account the | |
1665 fact that function-like macro arguments can expand to | |
1666 multiple tokens. This is to save memory at the expense of | |
1667 accuracy. | |
1668 | |
1669 Suppose we have #define SQARE(A) A * A | |
1670 | |
1671 And then we do SQARE(2+3) | |
1672 | |
1673 Then the tokens 2, +, 3, will have the same location, | |
1674 saying they come from the expansion of the argument A. */ | |
1675 num_macro_tokens = exp_count; | |
1676 map = linemap_enter_macro (pfile->line_table, node, | |
1677 expansion_point_loc, | |
1678 num_macro_tokens); | |
1679 } | |
1680 i = 0; | |
979 for (src = macro->exp.tokens; src < limit; src++) | 1681 for (src = macro->exp.tokens; src < limit; src++) |
980 { | 1682 { |
981 unsigned int count; | 1683 unsigned int arg_tokens_count; |
982 const cpp_token **from, **paste_flag; | 1684 macro_arg_token_iter from; |
1685 const cpp_token **paste_flag = NULL; | |
1686 const cpp_token **tmp_token_ptr; | |
983 | 1687 |
984 if (src->type != CPP_MACRO_ARG) | 1688 if (src->type != CPP_MACRO_ARG) |
985 { | 1689 { |
986 *dest++ = src; | 1690 /* Allocate a virtual location for token SRC, and add that |
1691 token and its virtual location into the buffers BUFF and | |
1692 VIRT_LOCS. */ | |
1693 unsigned index = expanded_token_index (pfile, macro, src, i); | |
1694 tokens_buff_add_token (buff, virt_locs, src, | |
1695 src->src_loc, src->src_loc, | |
1696 map, index); | |
1697 i += 1; | |
987 continue; | 1698 continue; |
988 } | 1699 } |
989 | 1700 |
990 paste_flag = 0; | 1701 paste_flag = 0; |
991 arg = &args[src->val.macro_arg.arg_no - 1]; | 1702 arg = &args[src->val.macro_arg.arg_no - 1]; |
1703 /* SRC is a macro parameter that we need to replace with its | |
1704 corresponding argument. So at some point we'll need to | |
1705 iterate over the tokens of the macro argument and copy them | |
1706 into the "place" now holding the correspondig macro | |
1707 parameter. We are going to use the iterator type | |
1708 macro_argo_token_iter to handle that iterating. The 'if' | |
1709 below is to initialize the iterator depending on the type of | |
1710 tokens the macro argument has. It also does some adjustment | |
1711 related to padding tokens and some pasting corner cases. */ | |
992 if (src->flags & STRINGIFY_ARG) | 1712 if (src->flags & STRINGIFY_ARG) |
993 count = 1, from = &arg->stringified; | 1713 { |
1714 arg_tokens_count = 1; | |
1715 macro_arg_token_iter_init (&from, | |
1716 CPP_OPTION (pfile, | |
1717 track_macro_expansion), | |
1718 MACRO_ARG_TOKEN_STRINGIFIED, | |
1719 arg, &arg->stringified); | |
1720 } | |
994 else if (src->flags & PASTE_LEFT) | 1721 else if (src->flags & PASTE_LEFT) |
995 count = arg->count, from = arg->first; | 1722 { |
1723 arg_tokens_count = arg->count; | |
1724 macro_arg_token_iter_init (&from, | |
1725 CPP_OPTION (pfile, | |
1726 track_macro_expansion), | |
1727 MACRO_ARG_TOKEN_NORMAL, | |
1728 arg, arg->first); | |
1729 } | |
996 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)) | 1730 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)) |
997 { | 1731 { |
998 count = arg->count, from = arg->first; | 1732 int num_toks; |
999 if (dest != first) | 1733 arg_tokens_count = arg->count; |
1000 { | 1734 macro_arg_token_iter_init (&from, |
1001 if (dest[-1]->type == CPP_COMMA | 1735 CPP_OPTION (pfile, |
1736 track_macro_expansion), | |
1737 MACRO_ARG_TOKEN_NORMAL, | |
1738 arg, arg->first); | |
1739 | |
1740 num_toks = tokens_buff_count (buff); | |
1741 | |
1742 if (num_toks != 0) | |
1743 { | |
1744 /* So the current parameter token is pasted to the previous | |
1745 token in the replacement list. Let's look at what | |
1746 we have as previous and current arguments. */ | |
1747 | |
1748 /* This is the previous argument's token ... */ | |
1749 tmp_token_ptr = tokens_buff_last_token_ptr (buff); | |
1750 | |
1751 if ((*tmp_token_ptr)->type == CPP_COMMA | |
1002 && macro->variadic | 1752 && macro->variadic |
1003 && src->val.macro_arg.arg_no == macro->paramc) | 1753 && src->val.macro_arg.arg_no == macro->paramc) |
1004 { | 1754 { |
1005 /* Swallow a pasted comma if from == NULL, otherwise | 1755 /* ... which is a comma; and the current parameter |
1006 drop the paste flag. */ | 1756 is the last parameter of a variadic function-like |
1007 if (from == NULL) | 1757 macro. If the argument to the current last |
1008 dest--; | 1758 parameter is NULL, then swallow the comma, |
1759 otherwise drop the paste flag. */ | |
1760 if (macro_arg_token_iter_get_token (&from) == NULL) | |
1761 tokens_buff_remove_last_token (buff); | |
1009 else | 1762 else |
1010 paste_flag = dest - 1; | 1763 paste_flag = tmp_token_ptr; |
1011 } | 1764 } |
1012 /* Remove the paste flag if the RHS is a placemarker. */ | 1765 /* Remove the paste flag if the RHS is a placemarker. */ |
1013 else if (count == 0) | 1766 else if (arg_tokens_count == 0) |
1014 paste_flag = dest - 1; | 1767 paste_flag = tmp_token_ptr; |
1015 } | 1768 } |
1016 } | 1769 } |
1017 else | 1770 else |
1018 count = arg->expanded_count, from = arg->expanded; | 1771 { |
1772 arg_tokens_count = arg->expanded_count; | |
1773 macro_arg_token_iter_init (&from, | |
1774 CPP_OPTION (pfile, | |
1775 track_macro_expansion), | |
1776 MACRO_ARG_TOKEN_EXPANDED, | |
1777 arg, arg->expanded); | |
1778 } | |
1019 | 1779 |
1020 /* Padding on the left of an argument (unless RHS of ##). */ | 1780 /* Padding on the left of an argument (unless RHS of ##). */ |
1021 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding) | 1781 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding) |
1022 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)) | 1782 && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)) |
1023 *dest++ = padding_token (pfile, src); | 1783 { |
1024 | 1784 const cpp_token *t = padding_token (pfile, src); |
1025 if (count) | 1785 unsigned index = expanded_token_index (pfile, macro, src, i); |
1026 { | 1786 /* Allocate a virtual location for the padding token and |
1027 memcpy (dest, from, count * sizeof (cpp_token *)); | 1787 append the token and its location to BUFF and |
1028 dest += count; | 1788 VIRT_LOCS. */ |
1789 tokens_buff_add_token (buff, virt_locs, t, | |
1790 t->src_loc, t->src_loc, | |
1791 map, index); | |
1792 } | |
1793 | |
1794 if (arg_tokens_count) | |
1795 { | |
1796 /* So now we've got the number of tokens that make up the | |
1797 argument that is going to replace the current parameter | |
1798 in the macro's replacement list. */ | |
1799 unsigned int j; | |
1800 for (j = 0; j < arg_tokens_count; ++j) | |
1801 { | |
1802 /* So if track_macro_exp is < 2, the user wants to | |
1803 save extra memory while tracking macro expansion | |
1804 locations. So in that case here is what we do: | |
1805 | |
1806 Suppose we have #define SQARE(A) A * A | |
1807 | |
1808 And then we do SQARE(2+3) | |
1809 | |
1810 Then the tokens 2, +, 3, will have the same location, | |
1811 saying they come from the expansion of the argument | |
1812 A. | |
1813 | |
1814 So that means we are going to ignore the COUNT tokens | |
1815 resulting from the expansion of the current macro | |
1816 arugment. In other words all the ARG_TOKENS_COUNT tokens | |
1817 resulting from the expansion of the macro argument will | |
1818 have the index I. Normally, each of those token should | |
1819 have index I+J. */ | |
1820 unsigned token_index = i; | |
1821 unsigned index; | |
1822 if (track_macro_exp > 1) | |
1823 token_index += j; | |
1824 | |
1825 index = expanded_token_index (pfile, macro, src, token_index); | |
1826 tokens_buff_add_token (buff, virt_locs, | |
1827 macro_arg_token_iter_get_token (&from), | |
1828 macro_arg_token_iter_get_location (&from), | |
1829 src->src_loc, map, index); | |
1830 macro_arg_token_iter_forward (&from); | |
1831 } | |
1029 | 1832 |
1030 /* With a non-empty argument on the LHS of ##, the last | 1833 /* With a non-empty argument on the LHS of ##, the last |
1031 token should be flagged PASTE_LEFT. */ | 1834 token should be flagged PASTE_LEFT. */ |
1032 if (src->flags & PASTE_LEFT) | 1835 if (src->flags & PASTE_LEFT) |
1033 paste_flag = dest - 1; | 1836 paste_flag = |
1034 } | 1837 (const cpp_token **) tokens_buff_last_token_ptr (buff); |
1035 else if (CPP_PEDANTIC (pfile) && ! macro->syshdr | 1838 } |
1036 && ! CPP_OPTION (pfile, c99) | 1839 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99) |
1037 && ! cpp_in_system_header (pfile)) | 1840 && ! macro->syshdr && ! cpp_in_system_header (pfile)) |
1038 { | 1841 { |
1039 cpp_error (pfile, CPP_DL_PEDWARN, | 1842 if (CPP_OPTION (pfile, cplusplus)) |
1843 cpp_pedwarning (pfile, CPP_W_PEDANTIC, | |
1844 "invoking macro %s argument %d: " | |
1845 "empty macro arguments are undefined" | |
1846 " in ISO C++98", | |
1847 NODE_NAME (node), src->val.macro_arg.arg_no); | |
1848 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat)) | |
1849 cpp_pedwarning (pfile, | |
1850 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 | |
1851 ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC, | |
1852 "invoking macro %s argument %d: " | |
1853 "empty macro arguments are undefined" | |
1854 " in ISO C90", | |
1855 NODE_NAME (node), src->val.macro_arg.arg_no); | |
1856 } | |
1857 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 | |
1858 && ! CPP_OPTION (pfile, cplusplus) | |
1859 && ! macro->syshdr && ! cpp_in_system_header (pfile)) | |
1860 cpp_warning (pfile, CPP_W_C90_C99_COMPAT, | |
1040 "invoking macro %s argument %d: " | 1861 "invoking macro %s argument %d: " |
1041 "empty macro arguments are undefined" | 1862 "empty macro arguments are undefined" |
1042 " in ISO C90 and ISO C++98", | 1863 " in ISO C90", |
1043 NODE_NAME (node), | 1864 NODE_NAME (node), src->val.macro_arg.arg_no); |
1044 src->val.macro_arg.arg_no); | |
1045 } | |
1046 | 1865 |
1047 /* Avoid paste on RHS (even case count == 0). */ | 1866 /* Avoid paste on RHS (even case count == 0). */ |
1048 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) | 1867 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) |
1049 *dest++ = &pfile->avoid_paste; | 1868 { |
1869 const cpp_token *t = &pfile->avoid_paste; | |
1870 tokens_buff_add_token (buff, virt_locs, | |
1871 t, t->src_loc, t->src_loc, | |
1872 NULL, 0); | |
1873 } | |
1050 | 1874 |
1051 /* Add a new paste flag, or remove an unwanted one. */ | 1875 /* Add a new paste flag, or remove an unwanted one. */ |
1052 if (paste_flag) | 1876 if (paste_flag) |
1053 { | 1877 { |
1054 cpp_token *token = _cpp_temp_token (pfile); | 1878 cpp_token *token = _cpp_temp_token (pfile); |
1058 token->flags = (*paste_flag)->flags | PASTE_LEFT; | 1882 token->flags = (*paste_flag)->flags | PASTE_LEFT; |
1059 else | 1883 else |
1060 token->flags = (*paste_flag)->flags & ~PASTE_LEFT; | 1884 token->flags = (*paste_flag)->flags & ~PASTE_LEFT; |
1061 *paste_flag = token; | 1885 *paste_flag = token; |
1062 } | 1886 } |
1063 } | 1887 |
1064 | 1888 i += arg_tokens_count; |
1065 /* Free the expanded arguments. */ | 1889 } |
1066 for (i = 0; i < macro->paramc; i++) | 1890 |
1067 if (args[i].expanded) | 1891 if (track_macro_exp) |
1068 free (args[i].expanded); | 1892 push_extended_tokens_context (pfile, node, buff, virt_locs, first, |
1069 | 1893 tokens_buff_count (buff)); |
1070 push_ptoken_context (pfile, node, buff, first, dest - first); | 1894 else |
1895 push_ptoken_context (pfile, node, buff, first, | |
1896 tokens_buff_count (buff)); | |
1897 | |
1898 num_macro_tokens_counter += tokens_buff_count (buff); | |
1071 } | 1899 } |
1072 | 1900 |
1073 /* Return a special padding token, with padding inherited from SOURCE. */ | 1901 /* Return a special padding token, with padding inherited from SOURCE. */ |
1074 static const cpp_token * | 1902 static const cpp_token * |
1075 padding_token (cpp_reader *pfile, const cpp_token *source) | 1903 padding_token (cpp_reader *pfile, const cpp_token *source) |
1093 cpp_context *result = pfile->context->next; | 1921 cpp_context *result = pfile->context->next; |
1094 | 1922 |
1095 if (result == 0) | 1923 if (result == 0) |
1096 { | 1924 { |
1097 result = XNEW (cpp_context); | 1925 result = XNEW (cpp_context); |
1926 memset (result, 0, sizeof (cpp_context)); | |
1098 result->prev = pfile->context; | 1927 result->prev = pfile->context; |
1099 result->next = 0; | 1928 result->next = 0; |
1100 pfile->context->next = result; | 1929 pfile->context->next = result; |
1101 } | 1930 } |
1102 | 1931 |
1109 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff, | 1938 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff, |
1110 const cpp_token **first, unsigned int count) | 1939 const cpp_token **first, unsigned int count) |
1111 { | 1940 { |
1112 cpp_context *context = next_context (pfile); | 1941 cpp_context *context = next_context (pfile); |
1113 | 1942 |
1114 context->direct_p = false; | 1943 context->tokens_kind = TOKENS_KIND_INDIRECT; |
1115 context->macro = macro; | 1944 context->c.macro = macro; |
1116 context->buff = buff; | 1945 context->buff = buff; |
1117 FIRST (context).ptoken = first; | 1946 FIRST (context).ptoken = first; |
1118 LAST (context).ptoken = first + count; | 1947 LAST (context).ptoken = first + count; |
1119 } | 1948 } |
1120 | 1949 |
1121 /* Push a list of tokens. */ | 1950 /* Push a list of tokens. |
1951 | |
1952 A NULL macro means that we should continue the current macro | |
1953 expansion, in essence. That means that if we are currently in a | |
1954 macro expansion context, we'll make the new pfile->context refer to | |
1955 the current macro. */ | |
1122 void | 1956 void |
1123 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro, | 1957 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro, |
1124 const cpp_token *first, unsigned int count) | 1958 const cpp_token *first, unsigned int count) |
1125 { | 1959 { |
1126 cpp_context *context = next_context (pfile); | 1960 cpp_context *context; |
1127 | 1961 |
1128 context->direct_p = true; | 1962 if (macro == NULL) |
1129 context->macro = macro; | 1963 macro = macro_of_context (pfile->context); |
1130 context->buff = NULL; | 1964 |
1131 FIRST (context).token = first; | 1965 context = next_context (pfile); |
1132 LAST (context).token = first + count; | 1966 context->tokens_kind = TOKENS_KIND_DIRECT; |
1967 context->c.macro = macro; | |
1968 context->buff = NULL; | |
1969 FIRST (context).token = first; | |
1970 LAST (context).token = first + count; | |
1971 } | |
1972 | |
1973 /* Build a context containing a list of tokens as well as their | |
1974 virtual locations and push it. TOKENS_BUFF is the buffer that | |
1975 contains the tokens pointed to by FIRST. If TOKENS_BUFF is | |
1976 non-NULL, it means that the context owns it, meaning that | |
1977 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that | |
1978 contains the virtual locations. | |
1979 | |
1980 A NULL macro means that we should continue the current macro | |
1981 expansion, in essence. That means that if we are currently in a | |
1982 macro expansion context, we'll make the new pfile->context refer to | |
1983 the current macro. */ | |
1984 static void | |
1985 push_extended_tokens_context (cpp_reader *pfile, | |
1986 cpp_hashnode *macro, | |
1987 _cpp_buff *token_buff, | |
1988 source_location *virt_locs, | |
1989 const cpp_token **first, | |
1990 unsigned int count) | |
1991 { | |
1992 cpp_context *context; | |
1993 macro_context *m; | |
1994 | |
1995 if (macro == NULL) | |
1996 macro = macro_of_context (pfile->context); | |
1997 | |
1998 context = next_context (pfile); | |
1999 context->tokens_kind = TOKENS_KIND_EXTENDED; | |
2000 context->buff = token_buff; | |
2001 | |
2002 m = XNEW (macro_context); | |
2003 m->macro_node = macro; | |
2004 m->virt_locs = virt_locs; | |
2005 m->cur_virt_loc = virt_locs; | |
2006 context->c.mc = m; | |
2007 FIRST (context).ptoken = first; | |
2008 LAST (context).ptoken = first + count; | |
1133 } | 2009 } |
1134 | 2010 |
1135 /* Push a traditional macro's replacement text. */ | 2011 /* Push a traditional macro's replacement text. */ |
1136 void | 2012 void |
1137 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro, | 2013 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro, |
1138 const uchar *start, size_t len) | 2014 const uchar *start, size_t len) |
1139 { | 2015 { |
1140 cpp_context *context = next_context (pfile); | 2016 cpp_context *context = next_context (pfile); |
1141 | 2017 |
1142 context->direct_p = true; | 2018 context->tokens_kind = TOKENS_KIND_DIRECT; |
1143 context->macro = macro; | 2019 context->c.macro = macro; |
1144 context->buff = NULL; | 2020 context->buff = NULL; |
1145 CUR (context) = start; | 2021 CUR (context) = start; |
1146 RLIMIT (context) = start + len; | 2022 RLIMIT (context) = start + len; |
1147 macro->flags |= NODE_DISABLED; | 2023 macro->flags |= NODE_DISABLED; |
2024 } | |
2025 | |
2026 /* Creates a buffer that holds tokens a.k.a "token buffer", usually | |
2027 for the purpose of storing them on a cpp_context. If VIRT_LOCS is | |
2028 non-null (which means that -ftrack-macro-expansion is on), | |
2029 *VIRT_LOCS is set to a newly allocated buffer that is supposed to | |
2030 hold the virtual locations of the tokens resulting from macro | |
2031 expansion. */ | |
2032 static _cpp_buff* | |
2033 tokens_buff_new (cpp_reader *pfile, size_t len, | |
2034 source_location **virt_locs) | |
2035 { | |
2036 size_t tokens_size = len * sizeof (cpp_token *); | |
2037 size_t locs_size = len * sizeof (source_location); | |
2038 | |
2039 if (virt_locs != NULL) | |
2040 *virt_locs = XNEWVEC (source_location, locs_size); | |
2041 return _cpp_get_buff (pfile, tokens_size); | |
2042 } | |
2043 | |
2044 /* Returns the number of tokens contained in a token buffer. The | |
2045 buffer holds a set of cpp_token*. */ | |
2046 static size_t | |
2047 tokens_buff_count (_cpp_buff *buff) | |
2048 { | |
2049 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *); | |
2050 } | |
2051 | |
2052 /* Return a pointer to the last token contained in the token buffer | |
2053 BUFF. */ | |
2054 static const cpp_token ** | |
2055 tokens_buff_last_token_ptr (_cpp_buff *buff) | |
2056 { | |
2057 return &((const cpp_token **) BUFF_FRONT (buff))[-1]; | |
2058 } | |
2059 | |
2060 /* Remove the last token contained in the token buffer TOKENS_BUFF. | |
2061 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer | |
2062 containing the virtual locations of the tokens in TOKENS_BUFF; in | |
2063 which case the function updates that buffer as well. */ | |
2064 static inline void | |
2065 tokens_buff_remove_last_token (_cpp_buff *tokens_buff) | |
2066 | |
2067 { | |
2068 if (BUFF_FRONT (tokens_buff) > tokens_buff->base) | |
2069 BUFF_FRONT (tokens_buff) = | |
2070 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1]; | |
2071 } | |
2072 | |
2073 /* Insert a token into the token buffer at the position pointed to by | |
2074 DEST. Note that the buffer is not enlarged so the previous token | |
2075 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null, | |
2076 means -ftrack-macro-expansion is effect; it then points to where to | |
2077 insert the virtual location of TOKEN. TOKEN is the token to | |
2078 insert. VIRT_LOC is the virtual location of the token, i.e, the | |
2079 location possibly encoding its locus across macro expansion. If | |
2080 TOKEN is an argument of a function-like macro (inside a macro | |
2081 replacement list), PARM_DEF_LOC is the spelling location of the | |
2082 macro parameter that TOKEN is replacing, in the replacement list of | |
2083 the macro. If TOKEN is not an argument of a function-like macro or | |
2084 if it doesn't come from a macro expansion, then VIRT_LOC can just | |
2085 be set to the same value as PARM_DEF_LOC. If MAP is non null, it | |
2086 means TOKEN comes from a macro expansion and MAP is the macro map | |
2087 associated to the macro. MACRO_TOKEN_INDEX points to the index of | |
2088 the token in the macro map; it is not considered if MAP is NULL. | |
2089 | |
2090 Upon successful completion this function returns the a pointer to | |
2091 the position of the token coming right after the insertion | |
2092 point. */ | |
2093 static inline const cpp_token ** | |
2094 tokens_buff_put_token_to (const cpp_token **dest, | |
2095 source_location *virt_loc_dest, | |
2096 const cpp_token *token, | |
2097 source_location virt_loc, | |
2098 source_location parm_def_loc, | |
2099 const line_map_macro *map, | |
2100 unsigned int macro_token_index) | |
2101 { | |
2102 source_location macro_loc = virt_loc; | |
2103 const cpp_token **result; | |
2104 | |
2105 if (virt_loc_dest) | |
2106 { | |
2107 /* -ftrack-macro-expansion is on. */ | |
2108 if (map) | |
2109 macro_loc = linemap_add_macro_token (map, macro_token_index, | |
2110 virt_loc, parm_def_loc); | |
2111 *virt_loc_dest = macro_loc; | |
2112 } | |
2113 *dest = token; | |
2114 result = &dest[1]; | |
2115 | |
2116 return result; | |
2117 } | |
2118 | |
2119 /* Adds a token at the end of the tokens contained in BUFFER. Note | |
2120 that this function doesn't enlarge BUFFER when the number of tokens | |
2121 reaches BUFFER's size; it aborts in that situation. | |
2122 | |
2123 TOKEN is the token to append. VIRT_LOC is the virtual location of | |
2124 the token, i.e, the location possibly encoding its locus across | |
2125 macro expansion. If TOKEN is an argument of a function-like macro | |
2126 (inside a macro replacement list), PARM_DEF_LOC is the location of | |
2127 the macro parameter that TOKEN is replacing. If TOKEN doesn't come | |
2128 from a macro expansion, then VIRT_LOC can just be set to the same | |
2129 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes | |
2130 from a macro expansion and MAP is the macro map associated to the | |
2131 macro. MACRO_TOKEN_INDEX points to the index of the token in the | |
2132 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is | |
2133 non-null, it means -ftrack-macro-expansion is on; in which case | |
2134 this function adds the virtual location DEF_LOC to the VIRT_LOCS | |
2135 array, at the same index as the one of TOKEN in BUFFER. Upon | |
2136 successful completion this function returns the a pointer to the | |
2137 position of the token coming right after the insertion point. */ | |
2138 static const cpp_token ** | |
2139 tokens_buff_add_token (_cpp_buff *buffer, | |
2140 source_location *virt_locs, | |
2141 const cpp_token *token, | |
2142 source_location virt_loc, | |
2143 source_location parm_def_loc, | |
2144 const line_map_macro *map, | |
2145 unsigned int macro_token_index) | |
2146 { | |
2147 const cpp_token **result; | |
2148 source_location *virt_loc_dest = NULL; | |
2149 unsigned token_index = | |
2150 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *); | |
2151 | |
2152 /* Abort if we pass the end the buffer. */ | |
2153 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer)) | |
2154 abort (); | |
2155 | |
2156 if (virt_locs != NULL) | |
2157 virt_loc_dest = &virt_locs[token_index]; | |
2158 | |
2159 result = | |
2160 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer), | |
2161 virt_loc_dest, token, virt_loc, parm_def_loc, | |
2162 map, macro_token_index); | |
2163 | |
2164 BUFF_FRONT (buffer) = (unsigned char *) result; | |
2165 return result; | |
2166 } | |
2167 | |
2168 /* Allocate space for the function-like macro argument ARG to store | |
2169 the tokens resulting from the macro-expansion of the tokens that | |
2170 make up ARG itself. That space is allocated in ARG->expanded and | |
2171 needs to be freed using free. */ | |
2172 static void | |
2173 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity) | |
2174 { | |
2175 gcc_checking_assert (arg->expanded == NULL | |
2176 && arg->expanded_virt_locs == NULL); | |
2177 | |
2178 arg->expanded = XNEWVEC (const cpp_token *, capacity); | |
2179 if (CPP_OPTION (pfile, track_macro_expansion)) | |
2180 arg->expanded_virt_locs = XNEWVEC (source_location, capacity); | |
2181 | |
2182 } | |
2183 | |
2184 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE | |
2185 tokens. */ | |
2186 static void | |
2187 ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg, | |
2188 size_t size, size_t *expanded_capacity) | |
2189 { | |
2190 if (size <= *expanded_capacity) | |
2191 return; | |
2192 | |
2193 size *= 2; | |
2194 | |
2195 arg->expanded = | |
2196 XRESIZEVEC (const cpp_token *, arg->expanded, size); | |
2197 *expanded_capacity = size; | |
2198 | |
2199 if (CPP_OPTION (pfile, track_macro_expansion)) | |
2200 { | |
2201 if (arg->expanded_virt_locs == NULL) | |
2202 arg->expanded_virt_locs = XNEWVEC (source_location, size); | |
2203 else | |
2204 arg->expanded_virt_locs = XRESIZEVEC (source_location, | |
2205 arg->expanded_virt_locs, | |
2206 size); | |
2207 } | |
1148 } | 2208 } |
1149 | 2209 |
1150 /* Expand an argument ARG before replacing parameters in a | 2210 /* Expand an argument ARG before replacing parameters in a |
1151 function-like macro. This works by pushing a context with the | 2211 function-like macro. This works by pushing a context with the |
1152 argument's tokens, and then expanding that into a temporary buffer | 2212 argument's tokens, and then expanding that into a temporary buffer |
1154 has terminated the argument's tokens with a CPP_EOF so that we know | 2214 has terminated the argument's tokens with a CPP_EOF so that we know |
1155 when we have fully expanded the argument. */ | 2215 when we have fully expanded the argument. */ |
1156 static void | 2216 static void |
1157 expand_arg (cpp_reader *pfile, macro_arg *arg) | 2217 expand_arg (cpp_reader *pfile, macro_arg *arg) |
1158 { | 2218 { |
1159 unsigned int capacity; | 2219 size_t capacity; |
1160 bool saved_warn_trad; | 2220 bool saved_warn_trad; |
1161 | 2221 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion); |
1162 if (arg->count == 0) | 2222 |
2223 if (arg->count == 0 | |
2224 || arg->expanded != NULL) | |
1163 return; | 2225 return; |
1164 | 2226 |
1165 /* Don't warn about funlike macros when pre-expanding. */ | 2227 /* Don't warn about funlike macros when pre-expanding. */ |
1166 saved_warn_trad = CPP_WTRADITIONAL (pfile); | 2228 saved_warn_trad = CPP_WTRADITIONAL (pfile); |
1167 CPP_WTRADITIONAL (pfile) = 0; | 2229 CPP_WTRADITIONAL (pfile) = 0; |
1168 | 2230 |
1169 /* Loop, reading in the arguments. */ | 2231 /* Loop, reading in the tokens of the argument. */ |
1170 capacity = 256; | 2232 capacity = 256; |
1171 arg->expanded = XNEWVEC (const cpp_token *, capacity); | 2233 alloc_expanded_arg_mem (pfile, arg, capacity); |
1172 | 2234 |
1173 push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1); | 2235 if (track_macro_exp_p) |
2236 push_extended_tokens_context (pfile, NULL, NULL, | |
2237 arg->virt_locs, | |
2238 arg->first, | |
2239 arg->count + 1); | |
2240 else | |
2241 push_ptoken_context (pfile, NULL, NULL, | |
2242 arg->first, arg->count + 1); | |
2243 | |
1174 for (;;) | 2244 for (;;) |
1175 { | 2245 { |
1176 const cpp_token *token; | 2246 const cpp_token *token; |
1177 | 2247 source_location location; |
1178 if (arg->expanded_count + 1 >= capacity) | 2248 |
1179 { | 2249 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1, |
1180 capacity *= 2; | 2250 &capacity); |
1181 arg->expanded = XRESIZEVEC (const cpp_token *, arg->expanded, | 2251 |
1182 capacity); | 2252 token = cpp_get_token_1 (pfile, &location); |
1183 } | |
1184 | |
1185 token = cpp_get_token (pfile); | |
1186 | 2253 |
1187 if (token->type == CPP_EOF) | 2254 if (token->type == CPP_EOF) |
1188 break; | 2255 break; |
1189 | 2256 |
1190 arg->expanded[arg->expanded_count++] = token; | 2257 set_arg_token (arg, token, location, |
2258 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED, | |
2259 CPP_OPTION (pfile, track_macro_expansion)); | |
2260 arg->expanded_count++; | |
1191 } | 2261 } |
1192 | 2262 |
1193 _cpp_pop_context (pfile); | 2263 _cpp_pop_context (pfile); |
1194 | 2264 |
1195 CPP_WTRADITIONAL (pfile) = saved_warn_trad; | 2265 CPP_WTRADITIONAL (pfile) = saved_warn_trad; |
1196 } | 2266 } |
1197 | 2267 |
2268 /* Returns the macro associated to the current context if we are in | |
2269 the context a macro expansion, NULL otherwise. */ | |
2270 static cpp_hashnode* | |
2271 macro_of_context (cpp_context *context) | |
2272 { | |
2273 if (context == NULL) | |
2274 return NULL; | |
2275 | |
2276 return (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2277 ? context->c.mc->macro_node | |
2278 : context->c.macro; | |
2279 } | |
2280 | |
2281 /* Return TRUE iff we are expanding a macro or are about to start | |
2282 expanding one. If we are effectively expanding a macro, the | |
2283 function macro_of_context returns a pointer to the macro being | |
2284 expanded. */ | |
2285 static bool | |
2286 in_macro_expansion_p (cpp_reader *pfile) | |
2287 { | |
2288 if (pfile == NULL) | |
2289 return false; | |
2290 | |
2291 return (pfile->about_to_expand_macro_p | |
2292 || macro_of_context (pfile->context)); | |
2293 } | |
2294 | |
1198 /* Pop the current context off the stack, re-enabling the macro if the | 2295 /* Pop the current context off the stack, re-enabling the macro if the |
1199 context represented a macro's replacement list. The context | 2296 context represented a macro's replacement list. Initially the |
1200 structure is not freed so that we can re-use it later. */ | 2297 context structure was not freed so that we can re-use it later, but |
2298 now we do free it to reduce peak memory consumption. */ | |
1201 void | 2299 void |
1202 _cpp_pop_context (cpp_reader *pfile) | 2300 _cpp_pop_context (cpp_reader *pfile) |
1203 { | 2301 { |
1204 cpp_context *context = pfile->context; | 2302 cpp_context *context = pfile->context; |
1205 | 2303 |
1206 if (context->macro) | 2304 /* We should not be popping the base context. */ |
1207 context->macro->flags &= ~NODE_DISABLED; | 2305 if (context == &pfile->base_context) |
2306 abort (); | |
2307 | |
2308 if (context->c.macro) | |
2309 { | |
2310 cpp_hashnode *macro; | |
2311 if (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2312 { | |
2313 macro_context *mc = context->c.mc; | |
2314 macro = mc->macro_node; | |
2315 /* If context->buff is set, it means the life time of tokens | |
2316 is bound to the life time of this context; so we must | |
2317 free the tokens; that means we must free the virtual | |
2318 locations of these tokens too. */ | |
2319 if (context->buff && mc->virt_locs) | |
2320 { | |
2321 free (mc->virt_locs); | |
2322 mc->virt_locs = NULL; | |
2323 } | |
2324 free (mc); | |
2325 context->c.mc = NULL; | |
2326 } | |
2327 else | |
2328 macro = context->c.macro; | |
2329 | |
2330 /* Beware that MACRO can be NULL in cases like when we are | |
2331 called from expand_arg. In those cases, a dummy context with | |
2332 tokens is pushed just for the purpose of walking them using | |
2333 cpp_get_token_1. In that case, no 'macro' field is set into | |
2334 the dummy context. */ | |
2335 if (macro != NULL | |
2336 /* Several contiguous macro expansion contexts can be | |
2337 associated to the same macro; that means it's the same | |
2338 macro expansion that spans across all these (sub) | |
2339 contexts. So we should re-enable an expansion-disabled | |
2340 macro only when we are sure we are really out of that | |
2341 macro expansion. */ | |
2342 && macro_of_context (context->prev) != macro) | |
2343 macro->flags &= ~NODE_DISABLED; | |
2344 | |
2345 if (macro == pfile->top_most_macro_node && context->prev == NULL) | |
2346 /* We are popping the context of the top-most macro node. */ | |
2347 pfile->top_most_macro_node = NULL; | |
2348 } | |
1208 | 2349 |
1209 if (context->buff) | 2350 if (context->buff) |
1210 _cpp_release_buff (pfile, context->buff); | 2351 { |
2352 /* Decrease memory peak consumption by freeing the memory used | |
2353 by the context. */ | |
2354 _cpp_free_buff (context->buff); | |
2355 } | |
1211 | 2356 |
1212 pfile->context = context->prev; | 2357 pfile->context = context->prev; |
1213 } | 2358 /* decrease peak memory consumption by feeing the context. */ |
1214 | 2359 pfile->context->next = NULL; |
1215 /* External routine to get a token. Also used nearly everywhere | 2360 free (context); |
1216 internally, except for places where we know we can safely call | 2361 } |
1217 _cpp_lex_token directly, such as lexing a directive name. | 2362 |
2363 /* Return TRUE if we reached the end of the set of tokens stored in | |
2364 CONTEXT, FALSE otherwise. */ | |
2365 static inline bool | |
2366 reached_end_of_context (cpp_context *context) | |
2367 { | |
2368 if (context->tokens_kind == TOKENS_KIND_DIRECT) | |
2369 return FIRST (context).token == LAST (context).token; | |
2370 else if (context->tokens_kind == TOKENS_KIND_INDIRECT | |
2371 || context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2372 return FIRST (context).ptoken == LAST (context).ptoken; | |
2373 else | |
2374 abort (); | |
2375 } | |
2376 | |
2377 /* Consume the next token contained in the current context of PFILE, | |
2378 and return it in *TOKEN. It's "full location" is returned in | |
2379 *LOCATION. If -ftrack-macro-location is in effeect, fFull location" | |
2380 means the location encoding the locus of the token across macro | |
2381 expansion; otherwise it's just is the "normal" location of the | |
2382 token which (*TOKEN)->src_loc. */ | |
2383 static inline void | |
2384 consume_next_token_from_context (cpp_reader *pfile, | |
2385 const cpp_token ** token, | |
2386 source_location *location) | |
2387 { | |
2388 cpp_context *c = pfile->context; | |
2389 | |
2390 if ((c)->tokens_kind == TOKENS_KIND_DIRECT) | |
2391 { | |
2392 *token = FIRST (c).token; | |
2393 *location = (*token)->src_loc; | |
2394 FIRST (c).token++; | |
2395 } | |
2396 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT) | |
2397 { | |
2398 *token = *FIRST (c).ptoken; | |
2399 *location = (*token)->src_loc; | |
2400 FIRST (c).ptoken++; | |
2401 } | |
2402 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED) | |
2403 { | |
2404 macro_context *m = c->c.mc; | |
2405 *token = *FIRST (c).ptoken; | |
2406 if (m->virt_locs) | |
2407 { | |
2408 *location = *m->cur_virt_loc; | |
2409 m->cur_virt_loc++; | |
2410 } | |
2411 else | |
2412 *location = (*token)->src_loc; | |
2413 FIRST (c).ptoken++; | |
2414 } | |
2415 else | |
2416 abort (); | |
2417 } | |
2418 | |
2419 /* In the traditional mode of the preprocessor, if we are currently in | |
2420 a directive, the location of a token must be the location of the | |
2421 start of the directive line. This function returns the proper | |
2422 location if we are in the traditional mode, and just returns | |
2423 LOCATION otherwise. */ | |
2424 | |
2425 static inline source_location | |
2426 maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location) | |
2427 { | |
2428 if (CPP_OPTION (pfile, traditional)) | |
2429 { | |
2430 if (pfile->state.in_directive) | |
2431 return pfile->directive_line; | |
2432 } | |
2433 return location; | |
2434 } | |
2435 | |
2436 /* Routine to get a token as well as its location. | |
1218 | 2437 |
1219 Macro expansions and directives are transparently handled, | 2438 Macro expansions and directives are transparently handled, |
1220 including entering included files. Thus tokens are post-macro | 2439 including entering included files. Thus tokens are post-macro |
1221 expansion, and after any intervening directives. External callers | 2440 expansion, and after any intervening directives. External callers |
1222 see CPP_EOF only at EOF. Internal callers also see it when meeting | 2441 see CPP_EOF only at EOF. Internal callers also see it when meeting |
1223 a directive inside a macro call, when at the end of a directive and | 2442 a directive inside a macro call, when at the end of a directive and |
1224 state.in_directive is still 1, and at the end of argument | 2443 state.in_directive is still 1, and at the end of argument |
1225 pre-expansion. */ | 2444 pre-expansion. |
1226 const cpp_token * | 2445 |
1227 cpp_get_token (cpp_reader *pfile) | 2446 LOC is an out parameter; *LOC is set to the location "as expected |
2447 by the user". Please read the comment of | |
2448 cpp_get_token_with_location to learn more about the meaning of this | |
2449 location. */ | |
2450 static const cpp_token* | |
2451 cpp_get_token_1 (cpp_reader *pfile, source_location *location) | |
1228 { | 2452 { |
1229 const cpp_token *result; | 2453 const cpp_token *result; |
1230 bool can_set = pfile->set_invocation_location; | 2454 /* This token is a virtual token that either encodes a location |
1231 pfile->set_invocation_location = false; | 2455 related to macro expansion or a spelling location. */ |
2456 source_location virt_loc = 0; | |
2457 /* pfile->about_to_expand_macro_p can be overriden by indirect calls | |
2458 to functions that push macro contexts. So let's save it so that | |
2459 we can restore it when we are about to leave this routine. */ | |
2460 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p; | |
1232 | 2461 |
1233 for (;;) | 2462 for (;;) |
1234 { | 2463 { |
1235 cpp_hashnode *node; | 2464 cpp_hashnode *node; |
1236 cpp_context *context = pfile->context; | 2465 cpp_context *context = pfile->context; |
1237 | 2466 |
1238 /* Context->prev == 0 <=> base context. */ | 2467 /* Context->prev == 0 <=> base context. */ |
1239 if (!context->prev) | 2468 if (!context->prev) |
1240 result = _cpp_lex_token (pfile); | 2469 { |
1241 else if (FIRST (context).token != LAST (context).token) | 2470 result = _cpp_lex_token (pfile); |
1242 { | 2471 virt_loc = result->src_loc; |
1243 if (context->direct_p) | 2472 } |
1244 result = FIRST (context).token++; | 2473 else if (!reached_end_of_context (context)) |
1245 else | 2474 { |
1246 result = *FIRST (context).ptoken++; | 2475 consume_next_token_from_context (pfile, &result, |
1247 | 2476 &virt_loc); |
1248 if (result->flags & PASTE_LEFT) | 2477 if (result->flags & PASTE_LEFT) |
1249 { | 2478 { |
1250 paste_all_tokens (pfile, result); | 2479 paste_all_tokens (pfile, result); |
1251 if (pfile->state.in_directive) | 2480 if (pfile->state.in_directive) |
1252 continue; | 2481 continue; |
1253 return padding_token (pfile, result); | 2482 result = padding_token (pfile, result); |
2483 goto out; | |
1254 } | 2484 } |
1255 } | 2485 } |
1256 else | 2486 else |
1257 { | 2487 { |
2488 if (pfile->context->c.macro) | |
2489 ++num_expanded_macros_counter; | |
1258 _cpp_pop_context (pfile); | 2490 _cpp_pop_context (pfile); |
1259 if (pfile->state.in_directive) | 2491 if (pfile->state.in_directive) |
1260 continue; | 2492 continue; |
1261 return &pfile->avoid_paste; | 2493 result = &pfile->avoid_paste; |
2494 goto out; | |
1262 } | 2495 } |
1263 | 2496 |
1264 if (pfile->state.in_directive && result->type == CPP_COMMENT) | 2497 if (pfile->state.in_directive && result->type == CPP_COMMENT) |
1265 continue; | 2498 continue; |
1266 | 2499 |
1274 | 2507 |
1275 if (!(node->flags & NODE_DISABLED)) | 2508 if (!(node->flags & NODE_DISABLED)) |
1276 { | 2509 { |
1277 int ret = 0; | 2510 int ret = 0; |
1278 /* If not in a macro context, and we're going to start an | 2511 /* If not in a macro context, and we're going to start an |
1279 expansion, record the location. */ | 2512 expansion, record the location and the top level macro |
1280 if (can_set && !context->macro) | 2513 about to be expanded. */ |
1281 pfile->invocation_location = result->src_loc; | 2514 if (!in_macro_expansion_p (pfile)) |
2515 { | |
2516 pfile->invocation_location = result->src_loc; | |
2517 pfile->top_most_macro_node = node; | |
2518 } | |
1282 if (pfile->state.prevent_expansion) | 2519 if (pfile->state.prevent_expansion) |
1283 break; | 2520 break; |
1284 | 2521 |
1285 /* Conditional macros require that a predicate be evaluated | 2522 /* Conditional macros require that a predicate be evaluated |
1286 first. */ | 2523 first. */ |
1293 | 2530 |
1294 whitespace_after = (peek_tok->type == CPP_PADDING | 2531 whitespace_after = (peek_tok->type == CPP_PADDING |
1295 || (peek_tok->flags & PREV_WHITE)); | 2532 || (peek_tok->flags & PREV_WHITE)); |
1296 node = pfile->cb.macro_to_expand (pfile, result); | 2533 node = pfile->cb.macro_to_expand (pfile, result); |
1297 if (node) | 2534 if (node) |
1298 ret = enter_macro_context (pfile, node, result); | 2535 ret = enter_macro_context (pfile, node, result, |
2536 virt_loc); | |
1299 else if (whitespace_after) | 2537 else if (whitespace_after) |
1300 { | 2538 { |
1301 /* If macro_to_expand hook returned NULL and it | 2539 /* If macro_to_expand hook returned NULL and it |
1302 ate some tokens, see if we don't need to add | 2540 ate some tokens, see if we don't need to add |
1303 a padding token in between this and the | 2541 a padding token in between this and the |
1310 peek_tok), 1); | 2548 peek_tok), 1); |
1311 } | 2549 } |
1312 } | 2550 } |
1313 } | 2551 } |
1314 else | 2552 else |
1315 ret = enter_macro_context (pfile, node, result); | 2553 ret = enter_macro_context (pfile, node, result, |
2554 virt_loc); | |
1316 if (ret) | 2555 if (ret) |
1317 { | 2556 { |
1318 if (pfile->state.in_directive || ret == 2) | 2557 if (pfile->state.in_directive || ret == 2) |
1319 continue; | 2558 continue; |
1320 return padding_token (pfile, result); | 2559 result = padding_token (pfile, result); |
2560 goto out; | |
1321 } | 2561 } |
1322 } | 2562 } |
1323 else | 2563 else |
1324 { | 2564 { |
1325 /* Flag this token as always unexpandable. FIXME: move this | 2565 /* Flag this token as always unexpandable. FIXME: move this |
1332 } | 2572 } |
1333 | 2573 |
1334 break; | 2574 break; |
1335 } | 2575 } |
1336 | 2576 |
2577 out: | |
2578 if (location != NULL) | |
2579 { | |
2580 if (virt_loc == 0) | |
2581 virt_loc = result->src_loc; | |
2582 *location = virt_loc; | |
2583 | |
2584 if (!CPP_OPTION (pfile, track_macro_expansion) | |
2585 && macro_of_context (pfile->context) != NULL) | |
2586 /* We are in a macro expansion context, are not tracking | |
2587 virtual location, but were asked to report the location | |
2588 of the expansion point of the macro being expanded. */ | |
2589 *location = pfile->invocation_location; | |
2590 | |
2591 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location); | |
2592 } | |
2593 | |
2594 pfile->about_to_expand_macro_p = saved_about_to_expand_macro; | |
1337 return result; | 2595 return result; |
1338 } | 2596 } |
1339 | 2597 |
1340 /* Like cpp_get_token, but also returns a location separate from the | 2598 /* External routine to get a token. Also used nearly everywhere |
1341 one provided by the returned token. LOC is an out parameter; *LOC | 2599 internally, except for places where we know we can safely call |
1342 is set to the location "as expected by the user". This matters | 2600 _cpp_lex_token directly, such as lexing a directive name. |
1343 when a token results from macro expansion -- the token's location | 2601 |
1344 will indicate where the macro is defined, but *LOC will be the | 2602 Macro expansions and directives are transparently handled, |
1345 location of the start of the expansion. */ | 2603 including entering included files. Thus tokens are post-macro |
2604 expansion, and after any intervening directives. External callers | |
2605 see CPP_EOF only at EOF. Internal callers also see it when meeting | |
2606 a directive inside a macro call, when at the end of a directive and | |
2607 state.in_directive is still 1, and at the end of argument | |
2608 pre-expansion. */ | |
2609 const cpp_token * | |
2610 cpp_get_token (cpp_reader *pfile) | |
2611 { | |
2612 return cpp_get_token_1 (pfile, NULL); | |
2613 } | |
2614 | |
2615 /* Like cpp_get_token, but also returns a virtual token location | |
2616 separate from the spelling location carried by the returned token. | |
2617 | |
2618 LOC is an out parameter; *LOC is set to the location "as expected | |
2619 by the user". This matters when a token results from macro | |
2620 expansion; in that case the token's spelling location indicates the | |
2621 locus of the token in the definition of the macro but *LOC | |
2622 virtually encodes all the other meaningful locuses associated to | |
2623 the token. | |
2624 | |
2625 What? virtual location? Yes, virtual location. | |
2626 | |
2627 If the token results from macro expansion and if macro expansion | |
2628 location tracking is enabled its virtual location encodes (at the | |
2629 same time): | |
2630 | |
2631 - the spelling location of the token | |
2632 | |
2633 - the locus of the macro expansion point | |
2634 | |
2635 - the locus of the point where the token got instantiated as part | |
2636 of the macro expansion process. | |
2637 | |
2638 You have to use the linemap API to get the locus you are interested | |
2639 in from a given virtual location. | |
2640 | |
2641 Note however that virtual locations are not necessarily ordered for | |
2642 relations '<' and '>'. One must use the function | |
2643 linemap_location_before_p instead of using the relational operator | |
2644 '<'. | |
2645 | |
2646 If macro expansion tracking is off and if the token results from | |
2647 macro expansion the virtual location is the expansion point of the | |
2648 macro that got expanded. | |
2649 | |
2650 When the token doesn't result from macro expansion, the virtual | |
2651 location is just the same thing as its spelling location. */ | |
2652 | |
1346 const cpp_token * | 2653 const cpp_token * |
1347 cpp_get_token_with_location (cpp_reader *pfile, source_location *loc) | 2654 cpp_get_token_with_location (cpp_reader *pfile, source_location *loc) |
1348 { | 2655 { |
1349 const cpp_token *result; | 2656 return cpp_get_token_1 (pfile, loc); |
1350 | |
1351 pfile->set_invocation_location = true; | |
1352 result = cpp_get_token (pfile); | |
1353 if (pfile->context->macro) | |
1354 *loc = pfile->invocation_location; | |
1355 else | |
1356 *loc = result->src_loc; | |
1357 | |
1358 return result; | |
1359 } | 2657 } |
1360 | 2658 |
1361 /* Returns true if we're expanding an object-like macro that was | 2659 /* Returns true if we're expanding an object-like macro that was |
1362 defined in a system header. Just checks the macro at the top of | 2660 defined in a system header. Just checks the macro at the top of |
1363 the stack. Used for diagnostic suppression. */ | 2661 the stack. Used for diagnostic suppression. */ |
1364 int | 2662 int |
1365 cpp_sys_macro_p (cpp_reader *pfile) | 2663 cpp_sys_macro_p (cpp_reader *pfile) |
1366 { | 2664 { |
1367 cpp_hashnode *node = pfile->context->macro; | 2665 cpp_hashnode *node = NULL; |
2666 | |
2667 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2668 node = pfile->context->c.mc->macro_node; | |
2669 else | |
2670 node = pfile->context->c.macro; | |
1368 | 2671 |
1369 return node && node->value.macro && node->value.macro->syshdr; | 2672 return node && node->value.macro && node->value.macro->syshdr; |
1370 } | 2673 } |
1371 | 2674 |
1372 /* Read each token in, until end of the current file. Directives are | 2675 /* Read each token in, until end of the current file. Directives are |
1419 _cpp_backup_tokens_direct (pfile, count); | 2722 _cpp_backup_tokens_direct (pfile, count); |
1420 else | 2723 else |
1421 { | 2724 { |
1422 if (count != 1) | 2725 if (count != 1) |
1423 abort (); | 2726 abort (); |
1424 if (pfile->context->direct_p) | 2727 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT) |
1425 FIRST (pfile->context).token--; | 2728 FIRST (pfile->context).token--; |
2729 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT) | |
2730 FIRST (pfile->context).ptoken--; | |
2731 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2732 { | |
2733 FIRST (pfile->context).ptoken--; | |
2734 if (pfile->context->c.macro) | |
2735 { | |
2736 macro_context *m = pfile->context->c.mc; | |
2737 m->cur_virt_loc--; | |
2738 gcc_checking_assert (m->cur_virt_loc >= m->virt_locs); | |
2739 } | |
2740 else | |
2741 abort (); | |
2742 } | |
1426 else | 2743 else |
1427 FIRST (pfile->context).ptoken--; | 2744 abort (); |
1428 } | 2745 } |
1429 } | 2746 } |
1430 | 2747 |
1431 /* #define directive parsing and handling. */ | 2748 /* #define directive parsing and handling. */ |
1432 | 2749 |
1440 | 2757 |
1441 /* Some redefinitions need to be warned about regardless. */ | 2758 /* Some redefinitions need to be warned about regardless. */ |
1442 if (node->flags & NODE_WARN) | 2759 if (node->flags & NODE_WARN) |
1443 return true; | 2760 return true; |
1444 | 2761 |
1445 /* Suppress warnings for builtins that lack the NODE_WARN flag. */ | 2762 /* Suppress warnings for builtins that lack the NODE_WARN flag, |
1446 if (node->flags & NODE_BUILTIN) | 2763 unless Wbuiltin-macro-redefined. */ |
1447 { | 2764 if (node->flags & NODE_BUILTIN |
1448 if (!pfile->cb.user_builtin_macro | 2765 && (!pfile->cb.user_builtin_macro |
1449 || !pfile->cb.user_builtin_macro (pfile, node)) | 2766 || !pfile->cb.user_builtin_macro (pfile, node))) |
1450 return false; | 2767 return CPP_OPTION (pfile, warn_builtin_macro_redefined); |
1451 } | |
1452 | 2768 |
1453 /* Redefinitions of conditional (context-sensitive) macros, on | 2769 /* Redefinitions of conditional (context-sensitive) macros, on |
1454 the other hand, must be allowed silently. */ | 2770 the other hand, must be allowed silently. */ |
1455 if (node->flags & NODE_CONDITIONAL) | 2771 if (node->flags & NODE_CONDITIONAL) |
1456 return false; | 2772 return false; |
1493 h->type = NT_VOID; | 2809 h->type = NT_VOID; |
1494 /* Clear builtin flag in case of redefinition. */ | 2810 /* Clear builtin flag in case of redefinition. */ |
1495 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED); | 2811 h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED); |
1496 } | 2812 } |
1497 | 2813 |
1498 /* Save parameter NODE to the parameter list of macro MACRO. Returns | 2814 /* Save parameter NODE (spelling SPELLING) to the parameter list of |
1499 zero on success, nonzero if the parameter is a duplicate. */ | 2815 macro MACRO. Returns zero on success, nonzero if the parameter is |
2816 a duplicate. */ | |
1500 bool | 2817 bool |
1501 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node) | 2818 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node, |
2819 cpp_hashnode *spelling) | |
1502 { | 2820 { |
1503 unsigned int len; | 2821 unsigned int len; |
1504 /* Constraint 6.10.3.6 - duplicate parameter names. */ | 2822 /* Constraint 6.10.3.6 - duplicate parameter names. */ |
1505 if (node->flags & NODE_MACRO_ARG) | 2823 if (node->flags & NODE_MACRO_ARG) |
1506 { | 2824 { |
1511 | 2829 |
1512 if (BUFF_ROOM (pfile->a_buff) | 2830 if (BUFF_ROOM (pfile->a_buff) |
1513 < (macro->paramc + 1) * sizeof (cpp_hashnode *)) | 2831 < (macro->paramc + 1) * sizeof (cpp_hashnode *)) |
1514 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *)); | 2832 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *)); |
1515 | 2833 |
1516 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node; | 2834 ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling; |
1517 node->flags |= NODE_MACRO_ARG; | 2835 node->flags |= NODE_MACRO_ARG; |
1518 len = macro->paramc * sizeof (union _cpp_hashnode_value); | 2836 len = macro->paramc * sizeof (struct macro_arg_saved_data); |
1519 if (len > pfile->macro_buffer_len) | 2837 if (len > pfile->macro_buffer_len) |
1520 { | 2838 { |
1521 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer, | 2839 pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer, |
1522 len); | 2840 len); |
1523 pfile->macro_buffer_len = len; | 2841 pfile->macro_buffer_len = len; |
1524 } | 2842 } |
1525 ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1] | 2843 struct macro_arg_saved_data save; |
1526 = node->value; | 2844 save.value = node->value; |
2845 save.canonical_node = node; | |
2846 ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1] | |
2847 = save; | |
1527 | 2848 |
1528 node->value.arg_index = macro->paramc; | 2849 node->value.arg_index = macro->paramc; |
1529 return false; | 2850 return false; |
1530 } | 2851 } |
1531 | 2852 |
1561 "macro parameters must be comma-separated"); | 2882 "macro parameters must be comma-separated"); |
1562 return false; | 2883 return false; |
1563 } | 2884 } |
1564 prev_ident = 1; | 2885 prev_ident = 1; |
1565 | 2886 |
1566 if (_cpp_save_parameter (pfile, macro, token->val.node.node)) | 2887 if (_cpp_save_parameter (pfile, macro, token->val.node.node, |
2888 token->val.node.spelling)) | |
1567 return false; | 2889 return false; |
1568 continue; | 2890 continue; |
1569 | 2891 |
1570 case CPP_CLOSE_PAREN: | 2892 case CPP_CLOSE_PAREN: |
1571 if (prev_ident || macro->paramc == 0) | 2893 if (prev_ident || macro->paramc == 0) |
1572 return true; | 2894 return true; |
1573 | 2895 |
1574 /* Fall through to pick up the error. */ | 2896 /* Fall through to pick up the error. */ |
2897 /* FALLTHRU */ | |
1575 case CPP_COMMA: | 2898 case CPP_COMMA: |
1576 if (!prev_ident) | 2899 if (!prev_ident) |
1577 { | 2900 { |
1578 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing"); | 2901 cpp_error (pfile, CPP_DL_ERROR, "parameter name missing"); |
1579 return false; | 2902 return false; |
1584 case CPP_ELLIPSIS: | 2907 case CPP_ELLIPSIS: |
1585 macro->variadic = 1; | 2908 macro->variadic = 1; |
1586 if (!prev_ident) | 2909 if (!prev_ident) |
1587 { | 2910 { |
1588 _cpp_save_parameter (pfile, macro, | 2911 _cpp_save_parameter (pfile, macro, |
2912 pfile->spec_nodes.n__VA_ARGS__, | |
1589 pfile->spec_nodes.n__VA_ARGS__); | 2913 pfile->spec_nodes.n__VA_ARGS__); |
1590 pfile->state.va_args_ok = 1; | 2914 pfile->state.va_args_ok = 1; |
1591 if (! CPP_OPTION (pfile, c99) | 2915 if (! CPP_OPTION (pfile, c99) |
1592 && CPP_OPTION (pfile, cpp_pedantic) | 2916 && CPP_OPTION (pfile, cpp_pedantic) |
1593 && CPP_OPTION (pfile, warn_variadic_macros)) | 2917 && CPP_OPTION (pfile, warn_variadic_macros)) |
1594 cpp_pedwarning | 2918 { |
1595 (pfile, CPP_W_VARIADIC_MACROS, | 2919 if (CPP_OPTION (pfile, cplusplus)) |
1596 "anonymous variadic macros were introduced in C99"); | 2920 cpp_pedwarning |
2921 (pfile, CPP_W_VARIADIC_MACROS, | |
2922 "anonymous variadic macros were introduced in C++11"); | |
2923 else | |
2924 cpp_pedwarning | |
2925 (pfile, CPP_W_VARIADIC_MACROS, | |
2926 "anonymous variadic macros were introduced in C99"); | |
2927 } | |
2928 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 | |
2929 && ! CPP_OPTION (pfile, cplusplus)) | |
2930 cpp_error (pfile, CPP_DL_WARNING, | |
2931 "anonymous variadic macros were introduced in C99"); | |
1597 } | 2932 } |
1598 else if (CPP_OPTION (pfile, cpp_pedantic) | 2933 else if (CPP_OPTION (pfile, cpp_pedantic) |
1599 && CPP_OPTION (pfile, warn_variadic_macros)) | 2934 && CPP_OPTION (pfile, warn_variadic_macros)) |
1600 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, | 2935 { |
2936 if (CPP_OPTION (pfile, cplusplus)) | |
2937 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, | |
2938 "ISO C++ does not permit named variadic macros"); | |
2939 else | |
2940 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, | |
1601 "ISO C does not permit named variadic macros"); | 2941 "ISO C does not permit named variadic macros"); |
2942 } | |
1602 | 2943 |
1603 /* We're at the end, and just expect a closing parenthesis. */ | 2944 /* We're at the end, and just expect a closing parenthesis. */ |
1604 token = _cpp_lex_token (pfile); | 2945 token = _cpp_lex_token (pfile); |
1605 if (token->type == CPP_CLOSE_PAREN) | 2946 if (token->type == CPP_CLOSE_PAREN) |
1606 return true; | 2947 return true; |
1637 | 2978 |
1638 /* Is this a parameter? */ | 2979 /* Is this a parameter? */ |
1639 if (token->type == CPP_NAME | 2980 if (token->type == CPP_NAME |
1640 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0) | 2981 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0) |
1641 { | 2982 { |
2983 cpp_hashnode *spelling = token->val.node.spelling; | |
1642 token->type = CPP_MACRO_ARG; | 2984 token->type = CPP_MACRO_ARG; |
1643 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index; | 2985 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index; |
2986 token->val.macro_arg.spelling = spelling; | |
1644 } | 2987 } |
1645 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 | 2988 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 |
1646 && (token->type == CPP_STRING || token->type == CPP_CHAR)) | 2989 && (token->type == CPP_STRING || token->type == CPP_CHAR)) |
1647 check_trad_stringification (pfile, macro, &token->val.str); | 2990 check_trad_stringification (pfile, macro, &token->val.str); |
1648 | 2991 |
1685 macro->fun_like = 1; | 3028 macro->fun_like = 1; |
1686 } | 3029 } |
1687 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE)) | 3030 else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE)) |
1688 { | 3031 { |
1689 /* While ISO C99 requires whitespace before replacement text | 3032 /* While ISO C99 requires whitespace before replacement text |
1690 in a macro definition, ISO C90 with TC1 allows there characters | 3033 in a macro definition, ISO C90 with TC1 allows characters |
1691 from the basic source character set. */ | 3034 from the basic source character set there. */ |
1692 if (CPP_OPTION (pfile, c99)) | 3035 if (CPP_OPTION (pfile, c99)) |
1693 cpp_error (pfile, CPP_DL_PEDWARN, | 3036 { |
1694 "ISO C99 requires whitespace after the macro name"); | 3037 if (CPP_OPTION (pfile, cplusplus)) |
3038 cpp_error (pfile, CPP_DL_PEDWARN, | |
3039 "ISO C++11 requires whitespace after the macro name"); | |
3040 else | |
3041 cpp_error (pfile, CPP_DL_PEDWARN, | |
3042 "ISO C99 requires whitespace after the macro name"); | |
3043 } | |
1695 else | 3044 else |
1696 { | 3045 { |
1697 int warntype = CPP_DL_WARNING; | 3046 int warntype = CPP_DL_WARNING; |
1698 switch (ctoken->type) | 3047 switch (ctoken->type) |
1699 { | 3048 { |
1884 } | 3233 } |
1885 | 3234 |
1886 /* Clear the fast argument lookup indices. */ | 3235 /* Clear the fast argument lookup indices. */ |
1887 for (i = macro->paramc; i-- > 0; ) | 3236 for (i = macro->paramc; i-- > 0; ) |
1888 { | 3237 { |
1889 struct cpp_hashnode *node = macro->params[i]; | 3238 struct macro_arg_saved_data *save = |
3239 &((struct macro_arg_saved_data *) pfile->macro_buffer)[i]; | |
3240 struct cpp_hashnode *node = save->canonical_node; | |
1890 node->flags &= ~ NODE_MACRO_ARG; | 3241 node->flags &= ~ NODE_MACRO_ARG; |
1891 node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i]; | 3242 node->value = save->value; |
1892 } | 3243 } |
1893 | 3244 |
1894 if (!ok) | 3245 if (!ok) |
1895 return ok; | 3246 return ok; |
1896 | 3247 |
1899 if (CPP_OPTION (pfile, warn_unused_macros)) | 3250 if (CPP_OPTION (pfile, warn_unused_macros)) |
1900 _cpp_warn_if_unused_macro (pfile, node, NULL); | 3251 _cpp_warn_if_unused_macro (pfile, node, NULL); |
1901 | 3252 |
1902 if (warn_of_redefinition (pfile, node, macro)) | 3253 if (warn_of_redefinition (pfile, node, macro)) |
1903 { | 3254 { |
1904 const int reason = (node->flags & NODE_BUILTIN) | 3255 const int reason = ((node->flags & NODE_BUILTIN) |
3256 && !(node->flags & NODE_WARN)) | |
1905 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE; | 3257 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE; |
1906 bool warned; | 3258 |
1907 | 3259 bool warned = |
1908 warned = cpp_pedwarning_with_line (pfile, reason, | 3260 cpp_pedwarning_with_line (pfile, reason, |
1909 pfile->directive_line, 0, | 3261 pfile->directive_line, 0, |
1910 "\"%s\" redefined", | 3262 "\"%s\" redefined", NODE_NAME (node)); |
1911 NODE_NAME (node)); | |
1912 | 3263 |
1913 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) | 3264 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) |
1914 cpp_error_with_line (pfile, CPP_DL_NOTE, | 3265 cpp_error_with_line (pfile, CPP_DL_NOTE, |
1915 node->value.macro->line, 0, | 3266 node->value.macro->line, 0, |
1916 "this is the location of the previous definition"); | 3267 "this is the location of the previous definition"); |
1925 node->value.macro = macro; | 3276 node->value.macro = macro; |
1926 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")) | 3277 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")) |
1927 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS") | 3278 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS") |
1928 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned | 3279 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned |
1929 in the C standard, as something that one must use in C++. | 3280 in the C standard, as something that one must use in C++. |
1930 However DR#593 indicates that these aren't actually mentioned | 3281 However DR#593 and C++11 indicate that they play no role in C++. |
1931 in the C++ standard. We special-case them anyway. */ | 3282 We special-case them anyway. */ |
1932 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS") | 3283 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS") |
1933 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS")) | 3284 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS")) |
1934 node->flags |= NODE_WARN; | 3285 node->flags |= NODE_WARN; |
1935 | 3286 |
1936 /* If user defines one of the conditional macros, remove the | 3287 /* If user defines one of the conditional macros, remove the |
1978 NODE_NAME (node)); | 3329 NODE_NAME (node)); |
1979 break; | 3330 break; |
1980 } | 3331 } |
1981 } | 3332 } |
1982 } | 3333 } |
3334 } | |
3335 | |
3336 /* Returns true of NODE is a function-like macro. */ | |
3337 bool | |
3338 cpp_fun_like_macro_p (cpp_hashnode *node) | |
3339 { | |
3340 return (node->type == NT_MACRO | |
3341 && (node->flags & (NODE_BUILTIN | NODE_MACRO_ARG)) == 0 | |
3342 && node->value.macro->fun_like); | |
1983 } | 3343 } |
1984 | 3344 |
1985 /* Returns the name, arguments and expansion of a macro, in a format | 3345 /* Returns the name, arguments and expansion of a macro, in a format |
1986 suitable to be read back in again, and therefore also for DWARF 2 | 3346 suitable to be read back in again, and therefore also for DWARF 2 |
1987 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION". | 3347 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION". |
2007 } | 3367 } |
2008 } | 3368 } |
2009 | 3369 |
2010 macro = node->value.macro; | 3370 macro = node->value.macro; |
2011 /* Calculate length. */ | 3371 /* Calculate length. */ |
2012 len = NODE_LEN (node) + 2; /* ' ' and NUL. */ | 3372 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */ |
2013 if (macro->fun_like) | 3373 if (macro->fun_like) |
2014 { | 3374 { |
2015 len += 4; /* "()" plus possible final ".." of named | 3375 len += 4; /* "()" plus possible final ".." of named |
2016 varargs (we have + 1 below). */ | 3376 varargs (we have + 1 below). */ |
2017 for (i = 0; i < macro->paramc; i++) | 3377 for (i = 0; i < macro->paramc; i++) |
2027 for (i = 0; i < count; i++) | 3387 for (i = 0; i < count; i++) |
2028 { | 3388 { |
2029 cpp_token *token = ¯o->exp.tokens[i]; | 3389 cpp_token *token = ¯o->exp.tokens[i]; |
2030 | 3390 |
2031 if (token->type == CPP_MACRO_ARG) | 3391 if (token->type == CPP_MACRO_ARG) |
2032 len += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]); | 3392 len += NODE_LEN (token->val.macro_arg.spelling); |
2033 else | 3393 else |
2034 len += cpp_token_len (token); | 3394 len += cpp_token_len (token); |
2035 | 3395 |
2036 if (token->flags & STRINGIFY_ARG) | 3396 if (token->flags & STRINGIFY_ARG) |
2037 len++; /* "#" */ | 3397 len++; /* "#" */ |
2049 pfile->macro_buffer_len = len; | 3409 pfile->macro_buffer_len = len; |
2050 } | 3410 } |
2051 | 3411 |
2052 /* Fill in the buffer. Start with the macro name. */ | 3412 /* Fill in the buffer. Start with the macro name. */ |
2053 buffer = pfile->macro_buffer; | 3413 buffer = pfile->macro_buffer; |
2054 memcpy (buffer, NODE_NAME (node), NODE_LEN (node)); | 3414 buffer = _cpp_spell_ident_ucns (buffer, node); |
2055 buffer += NODE_LEN (node); | |
2056 | 3415 |
2057 /* Parameter names. */ | 3416 /* Parameter names. */ |
2058 if (macro->fun_like) | 3417 if (macro->fun_like) |
2059 { | 3418 { |
2060 *buffer++ = '('; | 3419 *buffer++ = '('; |
2099 *buffer++ = '#'; | 3458 *buffer++ = '#'; |
2100 | 3459 |
2101 if (token->type == CPP_MACRO_ARG) | 3460 if (token->type == CPP_MACRO_ARG) |
2102 { | 3461 { |
2103 memcpy (buffer, | 3462 memcpy (buffer, |
2104 NODE_NAME (macro->params[token->val.macro_arg.arg_no - 1]), | 3463 NODE_NAME (token->val.macro_arg.spelling), |
2105 NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1])); | 3464 NODE_LEN (token->val.macro_arg.spelling)); |
2106 buffer += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]); | 3465 buffer += NODE_LEN (token->val.macro_arg.spelling); |
2107 } | 3466 } |
2108 else | 3467 else |
2109 buffer = cpp_spell_token (pfile, token, buffer, false); | 3468 buffer = cpp_spell_token (pfile, token, buffer, true); |
2110 | 3469 |
2111 if (token->flags & PASTE_LEFT) | 3470 if (token->flags & PASTE_LEFT) |
2112 { | 3471 { |
2113 *buffer++ = ' '; | 3472 *buffer++ = ' '; |
2114 *buffer++ = '#'; | 3473 *buffer++ = '#'; |