]>
Commit | Line | Data |
---|---|---|
93c80368 | 1 | /* Part of CPP library. (Macro and #define handling.) |
83ffe9cd | 2 | Copyright (C) 1986-2023 Free Software Foundation, Inc. |
711b8824 ZW |
3 | Written by Per Bothner, 1994. |
4 | Based on CCCP program by Paul Rubin, June 1986 | |
5 | Adapted to ANSI C, Richard Stallman, Jan 1987 | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
748086b7 | 9 | Free Software Foundation; either version 3, or (at your option) any |
711b8824 ZW |
10 | later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
748086b7 JJ |
18 | along with this program; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. | |
711b8824 ZW |
20 | |
21 | In other words, you are welcome to use, share and improve this program. | |
22 | You are forbidden to forbid anyone else to use, share and improve | |
23 | what you give them. Help stamp out software-hoarding! */ | |
24 | ||
25 | #include "config.h" | |
26 | #include "system.h" | |
27 | #include "cpplib.h" | |
4f4e53dd | 28 | #include "internal.h" |
711b8824 | 29 | |
93c80368 | 30 | typedef struct macro_arg macro_arg; |
92582b75 TT |
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. */ | |
93c80368 NB |
35 | struct macro_arg |
36 | { | |
4ed5bcfb | 37 | const cpp_token **first; /* First token in unexpanded argument. */ |
6d2f8887 | 38 | const cpp_token **expanded; /* Macro-expanded argument. */ |
4ed5bcfb | 39 | const cpp_token *stringified; /* Stringified argument. */ |
93c80368 NB |
40 | unsigned int count; /* # of tokens in argument. */ |
41 | unsigned int expanded_count; /* # of tokens in expanded argument. */ | |
620e594b | 42 | location_t *virt_locs; /* Where virtual locations for |
92582b75 | 43 | unexpanded tokens are stored. */ |
620e594b | 44 | location_t *expanded_virt_locs; /* Where virtual locations for |
92582b75 TT |
45 | expanded tokens are |
46 | stored. */ | |
47 | }; | |
48 | ||
49 | /* The kind of macro tokens which the instance of | |
50 | macro_arg_token_iter is supposed to iterate over. */ | |
51 | enum macro_arg_token_kind { | |
52 | MACRO_ARG_TOKEN_NORMAL, | |
53 | /* This is a macro argument token that got transformed into a string | |
8fa4bd81 | 54 | literal, e.g. #foo. */ |
92582b75 TT |
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 | |
7d9641cc | 73 | -ftrack-macro-expansion is used this location tracks loci across |
92582b75 | 74 | macro expansion. */ |
620e594b | 75 | const location_t *location_ptr; |
22d66382 | 76 | #if CHECKING_P |
92582b75 TT |
77 | /* The number of times the iterator went forward. This useful only |
78 | when checking is enabled. */ | |
79 | size_t num_forwards; | |
80 | #endif | |
711b8824 ZW |
81 | }; |
82 | ||
be5ffc59 JM |
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; | |
a570d97f | 88 | /* The previous value & type of this identifier. */ |
be5ffc59 | 89 | union _cpp_hashnode_value value; |
a570d97f | 90 | node_type type; |
be5ffc59 JM |
91 | }; |
92 | ||
fb771b9d TT |
93 | static const char *vaopt_paste_error = |
94 | N_("'##' cannot appear at either end of __VA_OPT__"); | |
95 | ||
e235031d JJ |
96 | static void expand_arg (cpp_reader *, macro_arg *); |
97 | ||
fb771b9d TT |
98 | /* A class for tracking __VA_OPT__ state while iterating over a |
99 | sequence of tokens. This is used during both macro definition and | |
100 | expansion. */ | |
101 | class vaopt_state { | |
102 | ||
103 | public: | |
104 | ||
e235031d JJ |
105 | enum update_type |
106 | { | |
107 | ERROR, | |
108 | DROP, | |
109 | INCLUDE, | |
110 | BEGIN, | |
111 | END | |
112 | }; | |
113 | ||
fb771b9d TT |
114 | /* Initialize the state tracker. ANY_ARGS is true if variable |
115 | arguments were provided to the macro invocation. */ | |
e235031d | 116 | vaopt_state (cpp_reader *pfile, bool is_variadic, macro_arg *arg) |
fb771b9d | 117 | : m_pfile (pfile), |
e235031d | 118 | m_arg (arg), |
fb771b9d | 119 | m_variadic (is_variadic), |
fb771b9d | 120 | m_last_was_paste (false), |
d5659997 | 121 | m_stringify (false), |
60887f8c | 122 | m_state (0), |
fb771b9d | 123 | m_paste_location (0), |
e235031d JJ |
124 | m_location (0), |
125 | m_update (ERROR) | |
fb771b9d TT |
126 | { |
127 | } | |
128 | ||
fb771b9d TT |
129 | /* Given a token, update the state of this tracker and return a |
130 | boolean indicating whether the token should be be included in the | |
131 | expansion. */ | |
132 | update_type update (const cpp_token *token) | |
133 | { | |
134 | /* If the macro isn't variadic, just don't bother. */ | |
135 | if (!m_variadic) | |
136 | return INCLUDE; | |
137 | ||
138 | if (token->type == CPP_NAME | |
139 | && token->val.node.node == m_pfile->spec_nodes.n__VA_OPT__) | |
140 | { | |
141 | if (m_state > 0) | |
142 | { | |
143 | cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc, | |
144 | "__VA_OPT__ may not appear in a __VA_OPT__"); | |
145 | return ERROR; | |
146 | } | |
147 | ++m_state; | |
148 | m_location = token->src_loc; | |
d5659997 | 149 | m_stringify = (token->flags & STRINGIFY_ARG) != 0; |
60887f8c | 150 | return BEGIN; |
fb771b9d TT |
151 | } |
152 | else if (m_state == 1) | |
153 | { | |
154 | if (token->type != CPP_OPEN_PAREN) | |
155 | { | |
156 | cpp_error_at (m_pfile, CPP_DL_ERROR, m_location, | |
157 | "__VA_OPT__ must be followed by an " | |
158 | "open parenthesis"); | |
159 | return ERROR; | |
160 | } | |
161 | ++m_state; | |
e235031d JJ |
162 | if (m_update == ERROR) |
163 | { | |
164 | if (m_arg == NULL) | |
165 | m_update = INCLUDE; | |
166 | else | |
167 | { | |
168 | m_update = DROP; | |
169 | if (!m_arg->expanded) | |
170 | expand_arg (m_pfile, m_arg); | |
171 | for (unsigned idx = 0; idx < m_arg->expanded_count; ++idx) | |
172 | if (m_arg->expanded[idx]->type != CPP_PADDING) | |
173 | { | |
174 | m_update = INCLUDE; | |
175 | break; | |
176 | } | |
177 | } | |
178 | } | |
fb771b9d TT |
179 | return DROP; |
180 | } | |
181 | else if (m_state >= 2) | |
182 | { | |
183 | if (m_state == 2 && token->type == CPP_PASTE) | |
184 | { | |
185 | cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc, | |
186 | vaopt_paste_error); | |
187 | return ERROR; | |
188 | } | |
189 | /* Advance states before further considering this token, in | |
190 | case we see a close paren immediately after the open | |
191 | paren. */ | |
192 | if (m_state == 2) | |
193 | ++m_state; | |
194 | ||
195 | bool was_paste = m_last_was_paste; | |
196 | m_last_was_paste = false; | |
197 | if (token->type == CPP_PASTE) | |
198 | { | |
199 | m_last_was_paste = true; | |
200 | m_paste_location = token->src_loc; | |
201 | } | |
202 | else if (token->type == CPP_OPEN_PAREN) | |
203 | ++m_state; | |
204 | else if (token->type == CPP_CLOSE_PAREN) | |
205 | { | |
206 | --m_state; | |
207 | if (m_state == 2) | |
208 | { | |
209 | /* Saw the final paren. */ | |
210 | m_state = 0; | |
211 | ||
212 | if (was_paste) | |
213 | { | |
214 | cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc, | |
215 | vaopt_paste_error); | |
216 | return ERROR; | |
217 | } | |
218 | ||
60887f8c | 219 | return END; |
fb771b9d TT |
220 | } |
221 | } | |
e235031d | 222 | return m_update; |
fb771b9d TT |
223 | } |
224 | ||
225 | /* Nothing to do with __VA_OPT__. */ | |
226 | return INCLUDE; | |
227 | } | |
228 | ||
229 | /* Ensure that any __VA_OPT__ was completed. If ok, return true. | |
230 | Otherwise, issue an error and return false. */ | |
231 | bool completed () | |
232 | { | |
233 | if (m_variadic && m_state != 0) | |
234 | cpp_error_at (m_pfile, CPP_DL_ERROR, m_location, | |
235 | "unterminated __VA_OPT__"); | |
236 | return m_state == 0; | |
237 | } | |
238 | ||
d5659997 JJ |
239 | /* Return true for # __VA_OPT__. */ |
240 | bool stringify () const | |
241 | { | |
242 | return m_stringify; | |
243 | } | |
244 | ||
fb771b9d TT |
245 | private: |
246 | ||
247 | /* The cpp_reader. */ | |
248 | cpp_reader *m_pfile; | |
249 | ||
e235031d JJ |
250 | /* The __VA_ARGS__ argument. */ |
251 | macro_arg *m_arg; | |
252 | ||
fb771b9d TT |
253 | /* True if the macro is variadic. */ |
254 | bool m_variadic; | |
60887f8c JM |
255 | /* If true, the previous token was ##. This is used to detect when |
256 | a paste occurs at the end of the sequence. */ | |
257 | bool m_last_was_paste; | |
d5659997 JJ |
258 | /* True for #__VA_OPT__. */ |
259 | bool m_stringify; | |
fb771b9d TT |
260 | |
261 | /* The state variable: | |
262 | 0 means not parsing | |
263 | 1 means __VA_OPT__ seen, looking for "(" | |
264 | 2 means "(" seen (so the next token can't be "##") | |
265 | >= 3 means looking for ")", the number encodes the paren depth. */ | |
266 | int m_state; | |
267 | ||
fb771b9d | 268 | /* The location of the paste token. */ |
620e594b | 269 | location_t m_paste_location; |
fb771b9d TT |
270 | |
271 | /* Location of the __VA_OPT__ token. */ | |
620e594b | 272 | location_t m_location; |
e235031d JJ |
273 | |
274 | /* If __VA_ARGS__ substitutes to no preprocessing tokens, | |
275 | INCLUDE, otherwise DROP. ERROR when unknown yet. */ | |
276 | update_type m_update; | |
fb771b9d TT |
277 | }; |
278 | ||
93c80368 NB |
279 | /* Macro expansion. */ |
280 | ||
13f93cf5 NS |
281 | static cpp_macro *get_deferred_or_lazy_macro (cpp_reader *, cpp_hashnode *, |
282 | location_t); | |
765d600a | 283 | static int enter_macro_context (cpp_reader *, cpp_hashnode *, |
620e594b | 284 | const cpp_token *, location_t); |
64824205 | 285 | static int builtin_macro (cpp_reader *, cpp_hashnode *, |
620e594b | 286 | location_t, location_t); |
6cf87ca4 ZW |
287 | static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *, |
288 | const cpp_token **, unsigned int); | |
92582b75 | 289 | static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *, |
620e594b | 290 | _cpp_buff *, location_t *, |
92582b75 | 291 | const cpp_token **, unsigned int); |
765d600a | 292 | static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *, |
92582b75 | 293 | _cpp_buff **, unsigned *); |
6cf87ca4 ZW |
294 | static cpp_context *next_context (cpp_reader *); |
295 | static const cpp_token *padding_token (cpp_reader *, const cpp_token *); | |
6cf87ca4 | 296 | static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int); |
d5659997 | 297 | static const cpp_token *stringify_arg (cpp_reader *, const cpp_token **, |
ac5fd364 | 298 | unsigned int); |
6cf87ca4 | 299 | static void paste_all_tokens (cpp_reader *, const cpp_token *); |
620e594b | 300 | static bool paste_tokens (cpp_reader *, location_t, |
828a7f76 | 301 | const cpp_token **, const cpp_token *); |
92582b75 TT |
302 | static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t); |
303 | static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *); | |
304 | static void delete_macro_args (_cpp_buff*, unsigned num_args); | |
305 | static void set_arg_token (macro_arg *, const cpp_token *, | |
620e594b | 306 | location_t, size_t, |
92582b75 TT |
307 | enum macro_arg_token_kind, |
308 | bool); | |
620e594b | 309 | static const location_t *get_arg_token_location (const macro_arg *, |
92582b75 TT |
310 | enum macro_arg_token_kind); |
311 | static const cpp_token **arg_token_ptr_at (const macro_arg *, | |
312 | size_t, | |
313 | enum macro_arg_token_kind, | |
620e594b | 314 | location_t **virt_location); |
92582b75 TT |
315 | |
316 | static void macro_arg_token_iter_init (macro_arg_token_iter *, bool, | |
317 | enum macro_arg_token_kind, | |
318 | const macro_arg *, | |
319 | const cpp_token **); | |
320 | static const cpp_token *macro_arg_token_iter_get_token | |
321 | (const macro_arg_token_iter *it); | |
620e594b | 322 | static location_t macro_arg_token_iter_get_location |
92582b75 TT |
323 | (const macro_arg_token_iter *); |
324 | static void macro_arg_token_iter_forward (macro_arg_token_iter *); | |
325 | static _cpp_buff *tokens_buff_new (cpp_reader *, size_t, | |
620e594b | 326 | location_t **); |
92582b75 TT |
327 | static size_t tokens_buff_count (_cpp_buff *); |
328 | static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *); | |
9b554be9 | 329 | static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **, |
620e594b | 330 | location_t *, |
9b554be9 | 331 | const cpp_token *, |
620e594b DM |
332 | location_t, |
333 | location_t, | |
0e50b624 | 334 | const line_map_macro *, |
9b554be9 | 335 | unsigned int); |
92582b75 TT |
336 | |
337 | static const cpp_token **tokens_buff_add_token (_cpp_buff *, | |
620e594b | 338 | location_t *, |
92582b75 | 339 | const cpp_token *, |
620e594b DM |
340 | location_t, |
341 | location_t, | |
0e50b624 | 342 | const line_map_macro *, |
92582b75 | 343 | unsigned int); |
9b554be9 | 344 | static inline void tokens_buff_remove_last_token (_cpp_buff *); |
6cf87ca4 | 345 | static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *, |
620e594b | 346 | macro_arg *, location_t); |
765d600a | 347 | static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *, |
92582b75 | 348 | _cpp_buff **, unsigned *); |
10f04917 | 349 | static cpp_macro *create_iso_definition (cpp_reader *); |
93c80368 | 350 | |
93c80368 NB |
351 | /* #define directive parsing and handling. */ |
352 | ||
10f04917 | 353 | static cpp_macro *lex_expansion_token (cpp_reader *, cpp_macro *); |
729a01f7 | 354 | static bool parse_params (cpp_reader *, unsigned *, bool *); |
6cf87ca4 ZW |
355 | static void check_trad_stringification (cpp_reader *, const cpp_macro *, |
356 | const cpp_string *); | |
92582b75 TT |
357 | static bool reached_end_of_context (cpp_context *); |
358 | static void consume_next_token_from_context (cpp_reader *pfile, | |
359 | const cpp_token **, | |
620e594b DM |
360 | location_t *); |
361 | static const cpp_token* cpp_get_token_1 (cpp_reader *, location_t *); | |
711b8824 | 362 | |
3600218c DS |
363 | static cpp_hashnode* macro_of_context (cpp_context *context); |
364 | ||
64a1a422 TT |
365 | /* Statistical counter tracking the number of macros that got |
366 | expanded. */ | |
367 | unsigned num_expanded_macros_counter = 0; | |
368 | /* Statistical counter tracking the total number tokens resulting | |
369 | from macro expansion. */ | |
370 | unsigned num_macro_tokens_counter = 0; | |
371 | ||
c04babd9 JJ |
372 | /* Wrapper around cpp_get_token to skip CPP_PADDING tokens |
373 | and not consume CPP_EOF. */ | |
374 | static const cpp_token * | |
375 | cpp_get_token_no_padding (cpp_reader *pfile) | |
376 | { | |
377 | for (;;) | |
378 | { | |
379 | const cpp_token *ret = cpp_peek_token (pfile, 0); | |
380 | if (ret->type == CPP_EOF) | |
381 | return ret; | |
382 | ret = cpp_get_token (pfile); | |
383 | if (ret->type != CPP_PADDING) | |
384 | return ret; | |
385 | } | |
386 | } | |
387 | ||
3d056cbf NS |
388 | /* Handle meeting "__has_include" builtin macro. */ |
389 | ||
390 | static int | |
391 | builtin_has_include (cpp_reader *pfile, cpp_hashnode *op, bool has_next) | |
392 | { | |
393 | int result = 0; | |
394 | ||
f8d6e448 JJ |
395 | if (!pfile->state.in_directive) |
396 | cpp_error (pfile, CPP_DL_ERROR, | |
397 | "\"%s\" used outside of preprocessing directive", | |
398 | NODE_NAME (op)); | |
399 | ||
3d056cbf | 400 | pfile->state.angled_headers = true; |
c04babd9 | 401 | const cpp_token *token = cpp_get_token_no_padding (pfile); |
3d056cbf NS |
402 | bool paren = token->type == CPP_OPEN_PAREN; |
403 | if (paren) | |
c04babd9 | 404 | token = cpp_get_token_no_padding (pfile); |
3d056cbf NS |
405 | else |
406 | cpp_error (pfile, CPP_DL_ERROR, | |
407 | "missing '(' before \"%s\" operand", NODE_NAME (op)); | |
408 | pfile->state.angled_headers = false; | |
409 | ||
410 | bool bracket = token->type != CPP_STRING; | |
411 | char *fname = NULL; | |
412 | if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME) | |
413 | { | |
414 | fname = XNEWVEC (char, token->val.str.len - 1); | |
415 | memcpy (fname, token->val.str.text + 1, token->val.str.len - 2); | |
416 | fname[token->val.str.len - 2] = '\0'; | |
417 | } | |
418 | else if (token->type == CPP_LESS) | |
419 | fname = _cpp_bracket_include (pfile); | |
420 | else | |
421 | cpp_error (pfile, CPP_DL_ERROR, | |
422 | "operator \"%s\" requires a header-name", NODE_NAME (op)); | |
423 | ||
424 | if (fname) | |
425 | { | |
426 | /* Do not do the lookup if we're skipping, that's unnecessary | |
427 | IO. */ | |
428 | if (!pfile->state.skip_eval | |
429 | && _cpp_has_header (pfile, fname, bracket, | |
430 | has_next ? IT_INCLUDE_NEXT : IT_INCLUDE)) | |
431 | result = 1; | |
432 | ||
433 | XDELETEVEC (fname); | |
434 | } | |
435 | ||
c04babd9 JJ |
436 | if (paren |
437 | && cpp_get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN) | |
3d056cbf NS |
438 | cpp_error (pfile, CPP_DL_ERROR, |
439 | "missing ')' after \"%s\" operand", NODE_NAME (op)); | |
440 | ||
441 | return result; | |
442 | } | |
443 | ||
a69cbaac NB |
444 | /* Emits a warning if NODE is a macro defined in the main file that |
445 | has not been used. */ | |
446 | int | |
6cf87ca4 ZW |
447 | _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node, |
448 | void *v ATTRIBUTE_UNUSED) | |
a69cbaac | 449 | { |
3f6677f4 | 450 | if (cpp_user_macro_p (node)) |
a69cbaac NB |
451 | { |
452 | cpp_macro *macro = node->value.macro; | |
453 | ||
454 | if (!macro->used | |
0e50b624 DM |
455 | && MAIN_FILE_P (linemap_check_ordinary |
456 | (linemap_lookup (pfile->line_table, | |
457 | macro->line)))) | |
87cf0651 SB |
458 | cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0, |
459 | "macro \"%s\" is not used", NODE_NAME (node)); | |
a69cbaac NB |
460 | } |
461 | ||
462 | return 1; | |
463 | } | |
464 | ||
4ed5bcfb NB |
465 | /* Allocates and returns a CPP_STRING token, containing TEXT of length |
466 | LEN, after null-terminating it. TEXT must be in permanent storage. */ | |
467 | static const cpp_token * | |
6cf87ca4 | 468 | new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len) |
93c80368 | 469 | { |
4ed5bcfb | 470 | cpp_token *token = _cpp_temp_token (pfile); |
93c80368 | 471 | |
4ed5bcfb | 472 | text[len] = '\0'; |
93c80368 | 473 | token->type = CPP_STRING; |
4ed5bcfb NB |
474 | token->val.str.len = len; |
475 | token->val.str.text = text; | |
93c80368 | 476 | token->flags = 0; |
4ed5bcfb | 477 | return token; |
93c80368 NB |
478 | } |
479 | ||
93c80368 NB |
480 | static const char * const monthnames[] = |
481 | { | |
482 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
483 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
484 | }; | |
485 | ||
21b11495 ZW |
486 | /* Helper function for builtin_macro. Returns the text generated by |
487 | a builtin macro. */ | |
278c4662 | 488 | const uchar * |
64824205 | 489 | _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node, |
620e594b | 490 | location_t loc) |
93c80368 | 491 | { |
278c4662 | 492 | const uchar *result = NULL; |
1bb64668 | 493 | linenum_type number = 1; |
644eddaa | 494 | |
93c80368 NB |
495 | switch (node->value.builtin) |
496 | { | |
4ed5bcfb | 497 | default: |
0527bc4e | 498 | cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", |
ebef4e8c | 499 | NODE_NAME (node)); |
278c4662 | 500 | break; |
4ed5bcfb | 501 | |
be8ac3e2 GZ |
502 | case BT_TIMESTAMP: |
503 | { | |
e8ff5196 | 504 | if (CPP_OPTION (pfile, warn_date_time)) |
5157b91e TB |
505 | cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent " |
506 | "reproducible builds", NODE_NAME (node)); | |
e8ff5196 | 507 | |
be8ac3e2 GZ |
508 | cpp_buffer *pbuffer = cpp_get_buffer (pfile); |
509 | if (pbuffer->timestamp == NULL) | |
510 | { | |
511 | /* Initialize timestamp value of the assotiated file. */ | |
512 | struct _cpp_file *file = cpp_get_file (pbuffer); | |
513 | if (file) | |
514 | { | |
515 | /* Generate __TIMESTAMP__ string, that represents | |
516 | the date and time of the last modification | |
517 | of the current source file. The string constant | |
518 | looks like "Sun Sep 16 01:03:52 1973". */ | |
519 | struct tm *tb = NULL; | |
520 | struct stat *st = _cpp_get_file_stat (file); | |
521 | if (st) | |
522 | tb = localtime (&st->st_mtime); | |
523 | if (tb) | |
524 | { | |
525 | char *str = asctime (tb); | |
526 | size_t len = strlen (str); | |
527 | unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2); | |
528 | buf[0] = '"'; | |
529 | strcpy ((char *) buf + 1, str); | |
530 | buf[len] = '"'; | |
531 | pbuffer->timestamp = buf; | |
532 | } | |
533 | else | |
534 | { | |
535 | cpp_errno (pfile, CPP_DL_WARNING, | |
536 | "could not determine file timestamp"); | |
b6baa67d | 537 | pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\""; |
be8ac3e2 GZ |
538 | } |
539 | } | |
540 | } | |
541 | result = pbuffer->timestamp; | |
542 | } | |
543 | break; | |
93c80368 | 544 | case BT_FILE: |
1a9b3f04 | 545 | case BT_FILE_NAME: |
93c80368 | 546 | case BT_BASE_FILE: |
711b8824 | 547 | { |
4ed5bcfb | 548 | unsigned int len; |
0bda4760 | 549 | const char *name; |
562a5c27 | 550 | uchar *buf; |
1a9b3f04 CL |
551 | |
552 | if (node->value.builtin == BT_FILE | |
553 | || node->value.builtin == BT_FILE_NAME) | |
554 | { | |
555 | name = linemap_get_expansion_filename (pfile->line_table, | |
556 | pfile->line_table->highest_line); | |
557 | if ((node->value.builtin == BT_FILE_NAME) && name) | |
558 | name = lbasename (name); | |
559 | } | |
46427374 TT |
560 | else |
561 | { | |
b492b686 GF |
562 | name = _cpp_get_file_name (pfile->main_file); |
563 | if (!name) | |
564 | abort (); | |
46427374 | 565 | } |
11543b27 | 566 | if (pfile->cb.remap_filename && !pfile->state.in_directive) |
7365279f | 567 | name = pfile->cb.remap_filename (name); |
4ed5bcfb | 568 | len = strlen (name); |
651ed942 | 569 | buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); |
278c4662 NB |
570 | result = buf; |
571 | *buf = '"'; | |
572 | buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len); | |
573 | *buf++ = '"'; | |
574 | *buf = '\0'; | |
711b8824 | 575 | } |
644eddaa NB |
576 | break; |
577 | ||
93c80368 | 578 | case BT_INCLUDE_LEVEL: |
d8693c6f NB |
579 | /* The line map depth counts the primary source as level 1, but |
580 | historically __INCLUDE_DEPTH__ has called the primary source | |
581 | level 0. */ | |
50f59cd7 | 582 | number = pfile->line_table->depth - 1; |
644eddaa | 583 | break; |
93c80368 NB |
584 | |
585 | case BT_SPECLINE: | |
586 | /* If __LINE__ is embedded in a macro, it must expand to the | |
587 | line of the macro's invocation, not its definition. | |
64824205 RH |
588 | Otherwise things like assert() will not work properly. |
589 | See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */ | |
590 | if (CPP_OPTION (pfile, traditional)) | |
591 | loc = pfile->line_table->highest_line; | |
592 | else | |
593 | loc = linemap_resolve_location (pfile->line_table, loc, | |
594 | LRK_MACRO_EXPANSION_POINT, NULL); | |
595 | number = linemap_get_expansion_line (pfile->line_table, loc); | |
644eddaa | 596 | break; |
93c80368 | 597 | |
5279d739 ZW |
598 | /* __STDC__ has the value 1 under normal circumstances. |
599 | However, if (a) we are in a system header, (b) the option | |
2a1dc0d8 ZW |
600 | stdc_0_in_system_headers is true (set by target config), and |
601 | (c) we are not in strictly conforming mode, then it has the | |
83900997 | 602 | value 0. (b) and (c) are already checked in cpp_init_builtins. */ |
93c80368 | 603 | case BT_STDC: |
bf425849 | 604 | if (_cpp_in_system_header (pfile)) |
83900997 JJ |
605 | number = 0; |
606 | else | |
607 | number = 1; | |
644eddaa | 608 | break; |
711b8824 | 609 | |
93c80368 NB |
610 | case BT_DATE: |
611 | case BT_TIME: | |
e8ff5196 | 612 | if (CPP_OPTION (pfile, warn_date_time)) |
5157b91e TB |
613 | cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent " |
614 | "reproducible builds", NODE_NAME (node)); | |
278c4662 | 615 | if (pfile->date == NULL) |
93c80368 | 616 | { |
4ed5bcfb NB |
617 | /* Allocate __DATE__ and __TIME__ strings from permanent |
618 | storage. We only do this once, and don't generate them | |
619 | at init time, because time() and localtime() are very | |
620 | slow on some systems. */ | |
56da7207 | 621 | time_t tt; |
4b5f564a | 622 | auto kind = cpp_get_date (pfile, &tt); |
56da7207 | 623 | |
4b5f564a | 624 | if (kind == CPP_time_kind::UNKNOWN) |
174f6622 | 625 | { |
4b5f564a NS |
626 | cpp_errno (pfile, CPP_DL_WARNING, |
627 | "could not determine date and time"); | |
628 | ||
629 | pfile->date = UC"\"??? ?? ????\""; | |
630 | pfile->time = UC"\"??:??:??\""; | |
174f6622 | 631 | } |
4b5f564a | 632 | else |
56da7207 | 633 | { |
4b5f564a NS |
634 | struct tm *tb = (kind == CPP_time_kind::FIXED |
635 | ? gmtime : localtime) (&tt); | |
636 | ||
56da7207 ZW |
637 | pfile->date = _cpp_unaligned_alloc (pfile, |
638 | sizeof ("\"Oct 11 1347\"")); | |
639 | sprintf ((char *) pfile->date, "\"%s %2d %4d\"", | |
6cf87ca4 ZW |
640 | monthnames[tb->tm_mon], tb->tm_mday, |
641 | tb->tm_year + 1900); | |
56da7207 ZW |
642 | |
643 | pfile->time = _cpp_unaligned_alloc (pfile, | |
644 | sizeof ("\"12:34:56\"")); | |
645 | sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"", | |
646 | tb->tm_hour, tb->tm_min, tb->tm_sec); | |
647 | } | |
93c80368 | 648 | } |
93c80368 | 649 | |
644eddaa | 650 | if (node->value.builtin == BT_DATE) |
278c4662 | 651 | result = pfile->date; |
644eddaa | 652 | else |
278c4662 | 653 | result = pfile->time; |
644eddaa | 654 | break; |
a702045a OW |
655 | |
656 | case BT_COUNTER: | |
ccfc4c91 OW |
657 | if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive) |
658 | cpp_error (pfile, CPP_DL_ERROR, | |
659 | "__COUNTER__ expanded inside directive with -fdirectives-only"); | |
a702045a OW |
660 | number = pfile->counter++; |
661 | break; | |
1f8d3e84 JJ |
662 | |
663 | case BT_HAS_ATTRIBUTE: | |
1d00f8c8 JM |
664 | number = pfile->cb.has_attribute (pfile, false); |
665 | break; | |
666 | ||
667 | case BT_HAS_STD_ATTRIBUTE: | |
668 | number = pfile->cb.has_attribute (pfile, true); | |
1f8d3e84 | 669 | break; |
ad1539d5 MS |
670 | |
671 | case BT_HAS_BUILTIN: | |
672 | number = pfile->cb.has_builtin (pfile); | |
673 | break; | |
3d056cbf NS |
674 | |
675 | case BT_HAS_INCLUDE: | |
676 | case BT_HAS_INCLUDE_NEXT: | |
677 | number = builtin_has_include (pfile, node, | |
678 | node->value.builtin == BT_HAS_INCLUDE_NEXT); | |
679 | break; | |
278c4662 NB |
680 | } |
681 | ||
682 | if (result == NULL) | |
683 | { | |
684 | /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */ | |
685 | result = _cpp_unaligned_alloc (pfile, 21); | |
686 | sprintf ((char *) result, "%u", number); | |
687 | } | |
688 | ||
689 | return result; | |
690 | } | |
691 | ||
4b5f564a NS |
692 | /* Get an idempotent date. Either the cached value, the value from |
693 | source epoch, or failing that, the value from time(2). Use this | |
694 | during compilation so that every time stamp is the same. */ | |
695 | CPP_time_kind | |
696 | cpp_get_date (cpp_reader *pfile, time_t *result) | |
697 | { | |
698 | if (!pfile->time_stamp_kind) | |
699 | { | |
700 | int kind = 0; | |
701 | if (pfile->cb.get_source_date_epoch) | |
702 | { | |
703 | /* Try reading the fixed epoch. */ | |
704 | pfile->time_stamp = pfile->cb.get_source_date_epoch (pfile); | |
705 | if (pfile->time_stamp != time_t (-1)) | |
706 | kind = int (CPP_time_kind::FIXED); | |
707 | } | |
708 | ||
709 | if (!kind) | |
710 | { | |
711 | /* Pedantically time_t (-1) is a legitimate value for | |
712 | "number of seconds since the Epoch". It is a silly | |
713 | time. */ | |
714 | errno = 0; | |
715 | pfile->time_stamp = time (nullptr); | |
716 | /* Annoyingly a library could legally set errno and return a | |
717 | valid time! Bad library! */ | |
718 | if (pfile->time_stamp == time_t (-1) && errno) | |
719 | kind = errno; | |
720 | else | |
721 | kind = int (CPP_time_kind::DYNAMIC); | |
722 | } | |
723 | ||
724 | pfile->time_stamp_kind = kind; | |
725 | } | |
726 | ||
727 | *result = pfile->time_stamp; | |
728 | if (pfile->time_stamp_kind >= 0) | |
729 | { | |
730 | errno = pfile->time_stamp_kind; | |
731 | return CPP_time_kind::UNKNOWN; | |
732 | } | |
733 | ||
734 | return CPP_time_kind (pfile->time_stamp_kind); | |
735 | } | |
736 | ||
278c4662 | 737 | /* Convert builtin macros like __FILE__ to a token and push it on the |
21b11495 ZW |
738 | context stack. Also handles _Pragma, for which a new token may not |
739 | be created. Returns 1 if it generates a new token context, 0 to | |
61eb99f6 MP |
740 | return the token to the caller. LOC is the location of the expansion |
741 | point of the macro. */ | |
278c4662 | 742 | static int |
64824205 | 743 | builtin_macro (cpp_reader *pfile, cpp_hashnode *node, |
620e594b | 744 | location_t loc, location_t expand_loc) |
278c4662 NB |
745 | { |
746 | const uchar *buf; | |
26aea073 NB |
747 | size_t len; |
748 | char *nbuf; | |
644eddaa | 749 | |
278c4662 NB |
750 | if (node->value.builtin == BT_PRAGMA) |
751 | { | |
644eddaa | 752 | /* Don't interpret _Pragma within directives. The standard is |
a6e0d593 JJ |
753 | not clear on this, but to me this makes most sense. |
754 | Similarly, don't interpret _Pragma inside expand_args, we might | |
755 | need to stringize it later on. */ | |
756 | if (pfile->state.in_directive || pfile->state.ignore__Pragma) | |
644eddaa NB |
757 | return 0; |
758 | ||
0afff540 | 759 | return _cpp_do__Pragma (pfile, loc); |
93c80368 | 760 | } |
644eddaa | 761 | |
64824205 | 762 | buf = _cpp_builtin_macro_text (pfile, node, expand_loc); |
26aea073 | 763 | len = ustrlen (buf); |
c3f829c1 | 764 | nbuf = (char *) alloca (len + 1); |
26aea073 NB |
765 | memcpy (nbuf, buf, len); |
766 | nbuf[len]='\n'; | |
278c4662 | 767 | |
40de9f76 | 768 | cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true); |
26aea073 | 769 | _cpp_clean_line (pfile); |
278c4662 NB |
770 | |
771 | /* Set pfile->cur_token as required by _cpp_lex_direct. */ | |
772 | pfile->cur_token = _cpp_temp_token (pfile); | |
c468587a | 773 | cpp_token *token = _cpp_lex_direct (pfile); |
61eb99f6 MP |
774 | /* We should point to the expansion point of the builtin macro. */ |
775 | token->src_loc = loc; | |
c468587a DS |
776 | if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) |
777 | { | |
778 | /* We are tracking tokens resulting from macro expansion. | |
779 | Create a macro line map and generate a virtual location for | |
780 | the token resulting from the expansion of the built-in | |
781 | macro. */ | |
620e594b | 782 | location_t *virt_locs = NULL; |
c468587a | 783 | _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs); |
0e50b624 | 784 | const line_map_macro * map = |
64824205 | 785 | linemap_enter_macro (pfile->line_table, node, loc, 1); |
c468587a DS |
786 | tokens_buff_add_token (token_buf, virt_locs, token, |
787 | pfile->line_table->builtin_location, | |
788 | pfile->line_table->builtin_location, | |
789 | map, /*macro_token_index=*/0); | |
790 | push_extended_tokens_context (pfile, node, token_buf, virt_locs, | |
791 | (const cpp_token **)token_buf->base, | |
792 | 1); | |
793 | } | |
794 | else | |
795 | _cpp_push_token_context (pfile, NULL, token, 1); | |
278c4662 | 796 | if (pfile->buffer->cur != pfile->buffer->rlimit) |
0527bc4e | 797 | cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", |
278c4662 NB |
798 | NODE_NAME (node)); |
799 | _cpp_pop_buffer (pfile); | |
800 | ||
644eddaa | 801 | return 1; |
711b8824 ZW |
802 | } |
803 | ||
d15a58c0 | 804 | /* Copies SRC, of length LEN, to DEST, adding backslashes before all |
651ed942 AP |
805 | backslashes and double quotes. DEST must be of sufficient size. |
806 | Returns a pointer to the end of the string. */ | |
562a5c27 | 807 | uchar * |
6cf87ca4 | 808 | cpp_quote_string (uchar *dest, const uchar *src, unsigned int len) |
93c80368 NB |
809 | { |
810 | while (len--) | |
811 | { | |
562a5c27 | 812 | uchar c = *src++; |
711b8824 | 813 | |
35b82d26 | 814 | switch (c) |
93c80368 | 815 | { |
35b82d26 NS |
816 | case '\n': |
817 | /* Naked LF can appear in raw string literals */ | |
818 | c = 'n'; | |
819 | /* FALLTHROUGH */ | |
820 | ||
821 | case '\\': | |
822 | case '"': | |
93c80368 | 823 | *dest++ = '\\'; |
35b82d26 NS |
824 | /* FALLTHROUGH */ |
825 | ||
826 | default: | |
93c80368 NB |
827 | *dest++ = c; |
828 | } | |
93c80368 | 829 | } |
711b8824 | 830 | |
93c80368 NB |
831 | return dest; |
832 | } | |
711b8824 | 833 | |
d5659997 JJ |
834 | /* Convert a token sequence FIRST to FIRST+COUNT-1 to a single string token |
835 | according to the rules of the ISO C #-operator. */ | |
4ed5bcfb | 836 | static const cpp_token * |
ac5fd364 | 837 | stringify_arg (cpp_reader *pfile, const cpp_token **first, unsigned int count) |
93c80368 | 838 | { |
6338b358 | 839 | unsigned char *dest; |
ece54d54 | 840 | unsigned int i, escape_it, backslash_count = 0; |
4ed5bcfb | 841 | const cpp_token *source = NULL; |
ece54d54 | 842 | size_t len; |
711b8824 | 843 | |
6338b358 NB |
844 | if (BUFF_ROOM (pfile->u_buff) < 3) |
845 | _cpp_extend_buff (pfile, &pfile->u_buff, 3); | |
846 | dest = BUFF_FRONT (pfile->u_buff); | |
847 | *dest++ = '"'; | |
848 | ||
93c80368 | 849 | /* Loop, reading in the argument's tokens. */ |
d5659997 | 850 | for (i = 0; i < count; i++) |
93c80368 | 851 | { |
d5659997 JJ |
852 | const cpp_token *token = first[i]; |
853 | ||
4ed5bcfb NB |
854 | if (token->type == CPP_PADDING) |
855 | { | |
18f41a1b JM |
856 | if (source == NULL |
857 | || (!(source->flags & PREV_WHITE) | |
858 | && token->val.source == NULL)) | |
4ed5bcfb NB |
859 | source = token->val.source; |
860 | continue; | |
861 | } | |
711b8824 | 862 | |
b6baa67d | 863 | escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR |
fd2ab214 | 864 | || token->type == CPP_WSTRING || token->type == CPP_WCHAR |
b6baa67d | 865 | || token->type == CPP_STRING32 || token->type == CPP_CHAR32 |
2c6e3f55 | 866 | || token->type == CPP_STRING16 || token->type == CPP_CHAR16 |
fe95b036 | 867 | || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR |
1251f7f1 ESR |
868 | || cpp_userdef_string_p (token->type) |
869 | || cpp_userdef_char_p (token->type)); | |
49039169 | 870 | |
ece54d54 | 871 | /* Room for each char being written in octal, initial space and |
6338b358 | 872 | final quote and NUL. */ |
4ed5bcfb | 873 | len = cpp_token_len (token); |
93c80368 | 874 | if (escape_it) |
93c80368 | 875 | len *= 4; |
6338b358 | 876 | len += 3; |
711b8824 | 877 | |
ece54d54 | 878 | if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len) |
93c80368 | 879 | { |
ece54d54 | 880 | size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff); |
8c3b2693 | 881 | _cpp_extend_buff (pfile, &pfile->u_buff, len); |
ece54d54 | 882 | dest = BUFF_FRONT (pfile->u_buff) + len_so_far; |
93c80368 | 883 | } |
711b8824 | 884 | |
4ed5bcfb | 885 | /* Leading white space? */ |
6338b358 | 886 | if (dest - 1 != BUFF_FRONT (pfile->u_buff)) |
4ed5bcfb NB |
887 | { |
888 | if (source == NULL) | |
889 | source = token; | |
890 | if (source->flags & PREV_WHITE) | |
891 | *dest++ = ' '; | |
892 | } | |
893 | source = NULL; | |
711b8824 | 894 | |
93c80368 NB |
895 | if (escape_it) |
896 | { | |
ece54d54 NB |
897 | _cpp_buff *buff = _cpp_get_buff (pfile, len); |
898 | unsigned char *buf = BUFF_FRONT (buff); | |
47e20491 | 899 | len = cpp_spell_token (pfile, token, buf, true) - buf; |
dcc229e5 | 900 | dest = cpp_quote_string (dest, buf, len); |
ece54d54 | 901 | _cpp_release_buff (pfile, buff); |
93c80368 NB |
902 | } |
903 | else | |
47e20491 | 904 | dest = cpp_spell_token (pfile, token, dest, true); |
93c80368 | 905 | |
1067694a | 906 | if (token->type == CPP_OTHER && token->val.str.text[0] == '\\') |
93c80368 NB |
907 | backslash_count++; |
908 | else | |
909 | backslash_count = 0; | |
910 | } | |
911 | ||
912 | /* Ignore the final \ of invalid string literals. */ | |
913 | if (backslash_count & 1) | |
914 | { | |
0527bc4e | 915 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c | 916 | "invalid string literal, ignoring final '\\'"); |
ece54d54 | 917 | dest--; |
93c80368 NB |
918 | } |
919 | ||
4ed5bcfb | 920 | /* Commit the memory, including NUL, and return the token. */ |
6338b358 | 921 | *dest++ = '"'; |
ece54d54 NB |
922 | len = dest - BUFF_FRONT (pfile->u_buff); |
923 | BUFF_FRONT (pfile->u_buff) = dest + 1; | |
924 | return new_string_token (pfile, dest - len, len); | |
93c80368 NB |
925 | } |
926 | ||
da7d8304 | 927 | /* Try to paste two tokens. On success, return nonzero. In any |
c9e7a609 | 928 | case, PLHS is updated to point to the pasted token, which is |
828a7f76 DS |
929 | guaranteed to not have the PASTE_LEFT flag set. LOCATION is |
930 | the virtual location used for error reporting. */ | |
c9e7a609 | 931 | static bool |
620e594b | 932 | paste_tokens (cpp_reader *pfile, location_t location, |
828a7f76 | 933 | const cpp_token **plhs, const cpp_token *rhs) |
d63eefbf | 934 | { |
de000d22 | 935 | unsigned char *buf, *end, *lhsend; |
fca35e1b | 936 | cpp_token *lhs; |
c9e7a609 | 937 | unsigned int len; |
c9e7a609 | 938 | |
d5659997 | 939 | len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 2; |
c3f829c1 | 940 | buf = (unsigned char *) alloca (len); |
be5ffc59 | 941 | end = lhsend = cpp_spell_token (pfile, *plhs, buf, true); |
c9e7a609 NB |
942 | |
943 | /* Avoid comment headers, since they are still processed in stage 3. | |
944 | It is simpler to insert a space here, rather than modifying the | |
945 | lexer to ignore comments in some circumstances. Simply returning | |
946 | false doesn't work, since we want to clear the PASTE_LEFT flag. */ | |
fca35e1b | 947 | if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ) |
c9e7a609 | 948 | *end++ = ' '; |
f373b44d TT |
949 | /* In one obscure case we might see padding here. */ |
950 | if (rhs->type != CPP_PADDING) | |
be5ffc59 | 951 | end = cpp_spell_token (pfile, rhs, end, true); |
26aea073 | 952 | *end = '\n'; |
c9e7a609 | 953 | |
40de9f76 | 954 | cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true); |
26aea073 | 955 | _cpp_clean_line (pfile); |
c9e7a609 NB |
956 | |
957 | /* Set pfile->cur_token as required by _cpp_lex_direct. */ | |
958 | pfile->cur_token = _cpp_temp_token (pfile); | |
fca35e1b | 959 | lhs = _cpp_lex_direct (pfile); |
de000d22 JJ |
960 | if (pfile->buffer->cur != pfile->buffer->rlimit) |
961 | { | |
620e594b | 962 | location_t saved_loc = lhs->src_loc; |
fca35e1b | 963 | |
de000d22 | 964 | _cpp_pop_buffer (pfile); |
d5659997 JJ |
965 | |
966 | unsigned char *rhsstart = lhsend; | |
967 | if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ) | |
968 | rhsstart++; | |
de000d22 | 969 | |
fca35e1b TT |
970 | /* We have to remove the PASTE_LEFT flag from the old lhs, but |
971 | we want to keep the new location. */ | |
972 | *lhs = **plhs; | |
973 | *plhs = lhs; | |
974 | lhs->src_loc = saved_loc; | |
975 | lhs->flags &= ~PASTE_LEFT; | |
976 | ||
de000d22 JJ |
977 | /* Mandatory error for all apart from assembler. */ |
978 | if (CPP_OPTION (pfile, lang) != CLK_ASM) | |
828a7f76 | 979 | cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, |
d5659997 JJ |
980 | "pasting \"%.*s\" and \"%.*s\" does not give " |
981 | "a valid preprocessing token", | |
982 | (int) (lhsend - buf), buf, | |
983 | (int) (end - rhsstart), rhsstart); | |
de000d22 JJ |
984 | return false; |
985 | } | |
c9e7a609 | 986 | |
ac5fd364 | 987 | lhs->flags |= (*plhs)->flags & (PREV_WHITE | PREV_FALLTHROUGH); |
fca35e1b | 988 | *plhs = lhs; |
de000d22 JJ |
989 | _cpp_pop_buffer (pfile); |
990 | return true; | |
d63eefbf NB |
991 | } |
992 | ||
d15a58c0 NB |
993 | /* Handles an arbitrarily long sequence of ## operators, with initial |
994 | operand LHS. This implementation is left-associative, | |
995 | non-recursive, and finishes a paste before handling succeeding | |
996 | ones. If a paste fails, we back up to the RHS of the failing ## | |
997 | operator before pushing the context containing the result of prior | |
998 | successful pastes, with the effect that the RHS appears in the | |
999 | output stream after the pasted LHS normally. */ | |
93c80368 | 1000 | static void |
6cf87ca4 | 1001 | paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs) |
93c80368 | 1002 | { |
92582b75 | 1003 | const cpp_token *rhs = NULL; |
4ed5bcfb | 1004 | cpp_context *context = pfile->context; |
620e594b | 1005 | location_t virt_loc = 0; |
0ff2b8a0 | 1006 | |
828a7f76 DS |
1007 | /* We are expanding a macro and we must have been called on a token |
1008 | that appears at the left hand side of a ## operator. */ | |
1009 | if (macro_of_context (pfile->context) == NULL | |
1010 | || (!(lhs->flags & PASTE_LEFT))) | |
0ff2b8a0 DS |
1011 | abort (); |
1012 | ||
1013 | if (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
1014 | /* The caller must have called consume_next_token_from_context | |
1015 | right before calling us. That has incremented the pointer to | |
1016 | the current virtual location. So it now points to the location | |
1017 | of the token that comes right after *LHS. We want the | |
1018 | resulting pasted token to have the location of the current | |
1019 | *LHS, though. */ | |
1020 | virt_loc = context->c.mc->cur_virt_loc[-1]; | |
828a7f76 DS |
1021 | else |
1022 | /* We are not tracking macro expansion. So the best virtual | |
1023 | location we can get here is the expansion point of the macro we | |
1024 | are currently expanding. */ | |
1025 | virt_loc = pfile->invocation_location; | |
4ed5bcfb | 1026 | |
93c80368 NB |
1027 | do |
1028 | { | |
1029 | /* Take the token directly from the current context. We can do | |
1030 | this, because we are in the replacement list of either an | |
1031 | object-like macro, or a function-like macro with arguments | |
1032 | inserted. In either case, the constraints to #define | |
d63eefbf | 1033 | guarantee we have at least one more token. */ |
92582b75 | 1034 | if (context->tokens_kind == TOKENS_KIND_DIRECT) |
82eda77e | 1035 | rhs = FIRST (context).token++; |
92582b75 | 1036 | else if (context->tokens_kind == TOKENS_KIND_INDIRECT) |
82eda77e | 1037 | rhs = *FIRST (context).ptoken++; |
92582b75 TT |
1038 | else if (context->tokens_kind == TOKENS_KIND_EXTENDED) |
1039 | { | |
1040 | /* So we are in presence of an extended token context, which | |
1041 | means that each token in this context has a virtual | |
1042 | location attached to it. So let's not forget to update | |
1043 | the pointer to the current virtual location of the | |
1044 | current token when we update the pointer to the current | |
1045 | token */ | |
1046 | ||
1047 | rhs = *FIRST (context).ptoken++; | |
1048 | /* context->c.mc must be non-null, as if we were not in a | |
1049 | macro context, context->tokens_kind could not be equal to | |
1050 | TOKENS_KIND_EXTENDED. */ | |
1051 | context->c.mc->cur_virt_loc++; | |
1052 | } | |
4ed5bcfb NB |
1053 | |
1054 | if (rhs->type == CPP_PADDING) | |
f373b44d TT |
1055 | { |
1056 | if (rhs->flags & PASTE_LEFT) | |
1057 | abort (); | |
1058 | } | |
828a7f76 | 1059 | if (!paste_tokens (pfile, virt_loc, &lhs, rhs)) |
d5659997 JJ |
1060 | { |
1061 | _cpp_backup_tokens (pfile, 1); | |
1062 | break; | |
1063 | } | |
93c80368 NB |
1064 | } |
1065 | while (rhs->flags & PASTE_LEFT); | |
1066 | ||
c9e7a609 | 1067 | /* Put the resulting token in its own context. */ |
0ff2b8a0 DS |
1068 | if (context->tokens_kind == TOKENS_KIND_EXTENDED) |
1069 | { | |
620e594b | 1070 | location_t *virt_locs = NULL; |
0ff2b8a0 DS |
1071 | _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs); |
1072 | tokens_buff_add_token (token_buf, virt_locs, lhs, | |
1073 | virt_loc, 0, NULL, 0); | |
1074 | push_extended_tokens_context (pfile, context->c.mc->macro_node, | |
1075 | token_buf, virt_locs, | |
1076 | (const cpp_token **)token_buf->base, 1); | |
1077 | } | |
1078 | else | |
1079 | _cpp_push_token_context (pfile, NULL, lhs, 1); | |
93c80368 NB |
1080 | } |
1081 | ||
1ce676a0 NB |
1082 | /* Returns TRUE if the number of arguments ARGC supplied in an |
1083 | invocation of the MACRO referenced by NODE is valid. An empty | |
1084 | invocation to a macro with no parameters should pass ARGC as zero. | |
1085 | ||
1086 | Note that MACRO cannot necessarily be deduced from NODE, in case | |
1087 | NODE was redefined whilst collecting arguments. */ | |
1088 | bool | |
6cf87ca4 | 1089 | _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc) |
1ce676a0 NB |
1090 | { |
1091 | if (argc == macro->paramc) | |
1092 | return true; | |
1093 | ||
1094 | if (argc < macro->paramc) | |
1095 | { | |
ce53cf7b | 1096 | /* In C++20 and C2X (here the va_opt flag is used), and also as a GNU |
fb771b9d | 1097 | extension, variadic arguments are allowed to not appear in |
1ce676a0 NB |
1098 | the invocation at all. |
1099 | e.g. #define debug(format, args...) something | |
1100 | debug("string"); | |
1101 | ||
3976796b ESR |
1102 | This is exactly the same as if an empty variadic list had been |
1103 | supplied - debug("string", ). */ | |
1ce676a0 NB |
1104 | |
1105 | if (argc + 1 == macro->paramc && macro->variadic) | |
1106 | { | |
fb771b9d TT |
1107 | if (CPP_PEDANTIC (pfile) && ! macro->syshdr |
1108 | && ! CPP_OPTION (pfile, va_opt)) | |
3976796b ESR |
1109 | { |
1110 | if (CPP_OPTION (pfile, cplusplus)) | |
1111 | cpp_error (pfile, CPP_DL_PEDWARN, | |
1112 | "ISO C++11 requires at least one argument " | |
1113 | "for the \"...\" in a variadic macro"); | |
1114 | else | |
1115 | cpp_error (pfile, CPP_DL_PEDWARN, | |
1116 | "ISO C99 requires at least one argument " | |
1117 | "for the \"...\" in a variadic macro"); | |
1118 | } | |
1ce676a0 NB |
1119 | return true; |
1120 | } | |
1121 | ||
0527bc4e | 1122 | cpp_error (pfile, CPP_DL_ERROR, |
1ce676a0 NB |
1123 | "macro \"%s\" requires %u arguments, but only %u given", |
1124 | NODE_NAME (node), macro->paramc, argc); | |
1125 | } | |
1126 | else | |
0527bc4e | 1127 | cpp_error (pfile, CPP_DL_ERROR, |
1ce676a0 NB |
1128 | "macro \"%s\" passed %u arguments, but takes just %u", |
1129 | NODE_NAME (node), argc, macro->paramc); | |
1130 | ||
954ad112 DM |
1131 | if (macro->line > RESERVED_LOCATION_COUNT) |
1132 | cpp_error_at (pfile, CPP_DL_NOTE, macro->line, "macro \"%s\" defined here", | |
1133 | NODE_NAME (node)); | |
1134 | ||
1ce676a0 NB |
1135 | return false; |
1136 | } | |
1137 | ||
d15a58c0 NB |
1138 | /* Reads and returns the arguments to a function-like macro |
1139 | invocation. Assumes the opening parenthesis has been processed. | |
1140 | If there is an error, emits an appropriate diagnostic and returns | |
1141 | NULL. Each argument is terminated by a CPP_EOF token, for the | |
765d600a JJ |
1142 | future benefit of expand_arg(). If there are any deferred |
1143 | #pragma directives among macro arguments, store pointers to the | |
92582b75 TT |
1144 | CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer. |
1145 | ||
1146 | What is returned is the buffer that contains the memory allocated | |
1147 | to hold the macro arguments. NODE is the name of the macro this | |
1148 | function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is | |
1149 | set to the actual number of macro arguments allocated in the | |
1150 | returned buffer. */ | |
b8af0ca5 | 1151 | static _cpp_buff * |
765d600a | 1152 | collect_args (cpp_reader *pfile, const cpp_hashnode *node, |
92582b75 | 1153 | _cpp_buff **pragma_buff, unsigned *num_args) |
93c80368 | 1154 | { |
b8af0ca5 NB |
1155 | _cpp_buff *buff, *base_buff; |
1156 | cpp_macro *macro; | |
1157 | macro_arg *args, *arg; | |
1158 | const cpp_token *token; | |
1159 | unsigned int argc; | |
620e594b | 1160 | location_t virt_loc; |
92582b75 TT |
1161 | bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion); |
1162 | unsigned num_args_alloced = 0; | |
b8af0ca5 NB |
1163 | |
1164 | macro = node->value.macro; | |
1165 | if (macro->paramc) | |
1166 | argc = macro->paramc; | |
1167 | else | |
1168 | argc = 1; | |
92582b75 TT |
1169 | |
1170 | #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50 | |
1171 | #define ARG_TOKENS_EXTENT 1000 | |
1172 | ||
1173 | buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG | |
1174 | * sizeof (cpp_token *) | |
b8af0ca5 NB |
1175 | + sizeof (macro_arg))); |
1176 | base_buff = buff; | |
1177 | args = (macro_arg *) buff->base; | |
1178 | memset (args, 0, argc * sizeof (macro_arg)); | |
ece54d54 | 1179 | buff->cur = (unsigned char *) &args[argc]; |
b8af0ca5 NB |
1180 | arg = args, argc = 0; |
1181 | ||
1182 | /* Collect the tokens making up each argument. We don't yet know | |
1183 | how many arguments have been supplied, whether too many or too | |
1184 | few. Hence the slightly bizarre usage of "argc" and "arg". */ | |
1185 | do | |
93c80368 | 1186 | { |
b8af0ca5 NB |
1187 | unsigned int paren_depth = 0; |
1188 | unsigned int ntokens = 0; | |
92582b75 TT |
1189 | unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; |
1190 | num_args_alloced++; | |
93c80368 | 1191 | |
b8af0ca5 NB |
1192 | argc++; |
1193 | arg->first = (const cpp_token **) buff->cur; | |
92582b75 TT |
1194 | if (track_macro_expansion_p) |
1195 | { | |
1196 | virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; | |
620e594b | 1197 | arg->virt_locs = XNEWVEC (location_t, |
92582b75 TT |
1198 | virt_locs_capacity); |
1199 | } | |
711b8824 | 1200 | |
b8af0ca5 NB |
1201 | for (;;) |
1202 | { | |
1203 | /* Require space for 2 new tokens (including a CPP_EOF). */ | |
ece54d54 | 1204 | if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit) |
b8af0ca5 | 1205 | { |
8c3b2693 | 1206 | buff = _cpp_append_extend_buff (pfile, buff, |
92582b75 TT |
1207 | ARG_TOKENS_EXTENT |
1208 | * sizeof (cpp_token *)); | |
b8af0ca5 NB |
1209 | arg->first = (const cpp_token **) buff->cur; |
1210 | } | |
92582b75 TT |
1211 | if (track_macro_expansion_p |
1212 | && (ntokens + 2 > virt_locs_capacity)) | |
1213 | { | |
1214 | virt_locs_capacity += ARG_TOKENS_EXTENT; | |
620e594b | 1215 | arg->virt_locs = XRESIZEVEC (location_t, |
92582b75 TT |
1216 | arg->virt_locs, |
1217 | virt_locs_capacity); | |
1218 | } | |
4ed5bcfb | 1219 | |
92582b75 | 1220 | token = cpp_get_token_1 (pfile, &virt_loc); |
711b8824 | 1221 | |
b8af0ca5 NB |
1222 | if (token->type == CPP_PADDING) |
1223 | { | |
1224 | /* Drop leading padding. */ | |
1225 | if (ntokens == 0) | |
1226 | continue; | |
1227 | } | |
1228 | else if (token->type == CPP_OPEN_PAREN) | |
1229 | paren_depth++; | |
1230 | else if (token->type == CPP_CLOSE_PAREN) | |
1231 | { | |
1232 | if (paren_depth-- == 0) | |
1233 | break; | |
1234 | } | |
1235 | else if (token->type == CPP_COMMA) | |
1236 | { | |
1237 | /* A comma does not terminate an argument within | |
1238 | parentheses or as part of a variable argument. */ | |
1239 | if (paren_depth == 0 | |
1240 | && ! (macro->variadic && argc == macro->paramc)) | |
1241 | break; | |
1242 | } | |
1243 | else if (token->type == CPP_EOF | |
1244 | || (token->type == CPP_HASH && token->flags & BOL)) | |
1245 | break; | |
0078a058 | 1246 | else if (token->type == CPP_PRAGMA && !(token->flags & PRAGMA_OP)) |
765d600a JJ |
1247 | { |
1248 | cpp_token *newtok = _cpp_temp_token (pfile); | |
1249 | ||
1250 | /* CPP_PRAGMA token lives in directive_result, which will | |
1251 | be overwritten on the next directive. */ | |
1252 | *newtok = *token; | |
1253 | token = newtok; | |
1254 | do | |
1255 | { | |
1256 | if (*pragma_buff == NULL | |
1257 | || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *)) | |
1258 | { | |
1259 | _cpp_buff *next; | |
1260 | if (*pragma_buff == NULL) | |
1261 | *pragma_buff | |
1262 | = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *)); | |
1263 | else | |
1264 | { | |
1265 | next = *pragma_buff; | |
1266 | *pragma_buff | |
1267 | = _cpp_get_buff (pfile, | |
1268 | (BUFF_FRONT (*pragma_buff) | |
1269 | - (*pragma_buff)->base) * 2); | |
1270 | (*pragma_buff)->next = next; | |
1271 | } | |
1272 | } | |
1273 | *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token; | |
1274 | BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *); | |
1275 | if (token->type == CPP_PRAGMA_EOL) | |
1276 | break; | |
92582b75 | 1277 | token = cpp_get_token_1 (pfile, &virt_loc); |
765d600a JJ |
1278 | } |
1279 | while (token->type != CPP_EOF); | |
1280 | ||
1281 | /* In deferred pragmas parsing_args and prevent_expansion | |
1282 | had been changed, reset it. */ | |
1283 | pfile->state.parsing_args = 2; | |
1284 | pfile->state.prevent_expansion = 1; | |
1285 | ||
1286 | if (token->type == CPP_EOF) | |
1287 | break; | |
1288 | else | |
1289 | continue; | |
1290 | } | |
92582b75 TT |
1291 | set_arg_token (arg, token, virt_loc, |
1292 | ntokens, MACRO_ARG_TOKEN_NORMAL, | |
1293 | CPP_OPTION (pfile, track_macro_expansion)); | |
1294 | ntokens++; | |
b8af0ca5 | 1295 | } |
93c80368 | 1296 | |
b8af0ca5 NB |
1297 | /* Drop trailing padding. */ |
1298 | while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING) | |
1299 | ntokens--; | |
93c80368 | 1300 | |
b8af0ca5 | 1301 | arg->count = ntokens; |
5abe05b4 NS |
1302 | /* Append an EOF to mark end-of-argument. */ |
1303 | set_arg_token (arg, &pfile->endarg, token->src_loc, | |
92582b75 TT |
1304 | ntokens, MACRO_ARG_TOKEN_NORMAL, |
1305 | CPP_OPTION (pfile, track_macro_expansion)); | |
93c80368 | 1306 | |
b8af0ca5 NB |
1307 | /* Terminate the argument. Excess arguments loop back and |
1308 | overwrite the final legitimate argument, before failing. */ | |
1309 | if (argc <= macro->paramc) | |
1310 | { | |
ece54d54 | 1311 | buff->cur = (unsigned char *) &arg->first[ntokens + 1]; |
b8af0ca5 NB |
1312 | if (argc != macro->paramc) |
1313 | arg++; | |
1314 | } | |
93c80368 | 1315 | } |
e808ec9c | 1316 | while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF); |
93c80368 | 1317 | |
e808ec9c | 1318 | if (token->type == CPP_EOF) |
93c80368 | 1319 | { |
dbcc6b15 NS |
1320 | /* Unless the EOF is marking the end of an argument, it's a fake |
1321 | one from the end of a file that _cpp_clean_line will not have | |
1322 | advanced past. */ | |
1323 | if (token == &pfile->endarg) | |
b8af0ca5 | 1324 | _cpp_backup_tokens (pfile, 1); |
0527bc4e | 1325 | cpp_error (pfile, CPP_DL_ERROR, |
ebef4e8c | 1326 | "unterminated argument list invoking macro \"%s\"", |
a28c5035 | 1327 | NODE_NAME (node)); |
93c80368 | 1328 | } |
1ce676a0 | 1329 | else |
93c80368 | 1330 | { |
1ce676a0 NB |
1331 | /* A single empty argument is counted as no argument. */ |
1332 | if (argc == 1 && macro->paramc == 0 && args[0].count == 0) | |
1333 | argc = 0; | |
1334 | if (_cpp_arguments_ok (pfile, macro, node, argc)) | |
58551c23 NB |
1335 | { |
1336 | /* GCC has special semantics for , ## b where b is a varargs | |
1337 | parameter: we remove the comma if b was omitted entirely. | |
1338 | If b was merely an empty argument, the comma is retained. | |
1339 | If the macro takes just one (varargs) parameter, then we | |
1340 | retain the comma only if we are standards conforming. | |
1341 | ||
1342 | If FIRST is NULL replace_args () swallows the comma. */ | |
1343 | if (macro->variadic && (argc < macro->paramc | |
1344 | || (argc == 1 && args[0].count == 0 | |
1345 | && !CPP_OPTION (pfile, std)))) | |
1346 | args[macro->paramc - 1].first = NULL; | |
92582b75 TT |
1347 | if (num_args) |
1348 | *num_args = num_args_alloced; | |
58551c23 NB |
1349 | return base_buff; |
1350 | } | |
93c80368 NB |
1351 | } |
1352 | ||
1ce676a0 | 1353 | /* An error occurred. */ |
b8af0ca5 NB |
1354 | _cpp_release_buff (pfile, base_buff); |
1355 | return NULL; | |
93c80368 NB |
1356 | } |
1357 | ||
d6da836d NB |
1358 | /* Search for an opening parenthesis to the macro of NODE, in such a |
1359 | way that, if none is found, we don't lose the information in any | |
1360 | intervening padding tokens. If we find the parenthesis, collect | |
765d600a | 1361 | the arguments and return the buffer containing them. PRAGMA_BUFF |
92582b75 TT |
1362 | argument is the same as in collect_args. If NUM_ARGS is non-NULL, |
1363 | *NUM_ARGS is set to the number of arguments contained in the | |
1364 | returned buffer. */ | |
d6da836d | 1365 | static _cpp_buff * |
765d600a | 1366 | funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node, |
92582b75 | 1367 | _cpp_buff **pragma_buff, unsigned *num_args) |
93c80368 | 1368 | { |
d6da836d | 1369 | const cpp_token *token, *padding = NULL; |
4ed5bcfb | 1370 | |
d6da836d | 1371 | for (;;) |
bdcbe496 | 1372 | { |
d6da836d NB |
1373 | token = cpp_get_token (pfile); |
1374 | if (token->type != CPP_PADDING) | |
1375 | break; | |
95ac5635 | 1376 | gcc_assert ((token->flags & PREV_WHITE) == 0); |
d6da836d | 1377 | if (padding == NULL |
95ac5635 JJ |
1378 | || padding->val.source == NULL |
1379 | || (!(padding->val.source->flags & PREV_WHITE) | |
1380 | && token->val.source == NULL)) | |
d6da836d | 1381 | padding = token; |
bdcbe496 | 1382 | } |
93c80368 | 1383 | |
d6da836d | 1384 | if (token->type == CPP_OPEN_PAREN) |
93c80368 | 1385 | { |
d6da836d | 1386 | pfile->state.parsing_args = 2; |
92582b75 | 1387 | return collect_args (pfile, node, pragma_buff, num_args); |
93c80368 | 1388 | } |
dbcc6b15 NS |
1389 | |
1390 | /* Back up. A CPP_EOF is either an EOF from an argument we're | |
1391 | expanding, or a fake one from lex_direct. We want to backup the | |
1392 | former, but not the latter. We may have skipped padding, in | |
1393 | which case backing up more than one token when expanding macros | |
1394 | is in general too difficult. We re-insert it in its own | |
1395 | context. */ | |
1396 | if (token->type != CPP_EOF || token == &pfile->endarg) | |
1397 | { | |
1398 | _cpp_backup_tokens (pfile, 1); | |
1399 | if (padding) | |
1400 | _cpp_push_token_context (pfile, NULL, padding, 1); | |
1401 | } | |
d6da836d NB |
1402 | |
1403 | return NULL; | |
93c80368 NB |
1404 | } |
1405 | ||
aa508502 JM |
1406 | /* Return the real number of tokens in the expansion of MACRO. */ |
1407 | static inline unsigned int | |
1408 | macro_real_token_count (const cpp_macro *macro) | |
1409 | { | |
aa508502 JM |
1410 | if (__builtin_expect (!macro->extra_tokens, true)) |
1411 | return macro->count; | |
10f04917 NS |
1412 | |
1413 | for (unsigned i = macro->count; i--;) | |
1414 | if (macro->exp.tokens[i].type != CPP_PASTE) | |
1415 | return i + 1; | |
1416 | ||
1417 | return 0; | |
aa508502 JM |
1418 | } |
1419 | ||
d15a58c0 NB |
1420 | /* Push the context of a macro with hash entry NODE onto the context |
1421 | stack. If we can successfully expand the macro, we push a context | |
1422 | containing its yet-to-be-rescanned replacement list and return one. | |
92582b75 TT |
1423 | If there were additionally any unexpanded deferred #pragma |
1424 | directives among macro arguments, push another context containing | |
1425 | the pragma tokens before the yet-to-be-rescanned replacement list | |
1426 | and return two. Otherwise, we don't push a context and return | |
1427 | zero. LOCATION is the location of the expansion point of the | |
1428 | macro. */ | |
711b8824 | 1429 | static int |
765d600a | 1430 | enter_macro_context (cpp_reader *pfile, cpp_hashnode *node, |
620e594b | 1431 | const cpp_token *result, location_t location) |
711b8824 | 1432 | { |
d15a58c0 | 1433 | /* The presence of a macro invalidates a file's controlling macro. */ |
644eddaa NB |
1434 | pfile->mi_valid = false; |
1435 | ||
3620711b NB |
1436 | pfile->state.angled_headers = false; |
1437 | ||
828a7f76 DS |
1438 | /* From here to when we push the context for the macro later down |
1439 | this function, we need to flag the fact that we are about to | |
1440 | expand a macro. This is useful when -ftrack-macro-expansion is | |
1441 | turned off. In that case, we need to record the location of the | |
1442 | expansion point of the top-most macro we are about to to expand, | |
1443 | into pfile->invocation_location. But we must not record any such | |
1444 | location once the process of expanding the macro starts; that is, | |
1445 | we must not do that recording between now and later down this | |
1446 | function where set this flag to FALSE. */ | |
1447 | pfile->about_to_expand_macro_p = true; | |
1448 | ||
3f6677f4 | 1449 | if (cpp_user_macro_p (node)) |
711b8824 | 1450 | { |
4ed5bcfb | 1451 | cpp_macro *macro = node->value.macro; |
765d600a | 1452 | _cpp_buff *pragma_buff = NULL; |
4c2b647d | 1453 | |
d6da836d NB |
1454 | if (macro->fun_like) |
1455 | { | |
1456 | _cpp_buff *buff; | |
92582b75 | 1457 | unsigned num_args = 0; |
d6da836d NB |
1458 | |
1459 | pfile->state.prevent_expansion++; | |
1460 | pfile->keep_tokens++; | |
1461 | pfile->state.parsing_args = 1; | |
92582b75 TT |
1462 | buff = funlike_invocation_p (pfile, node, &pragma_buff, |
1463 | &num_args); | |
d6da836d NB |
1464 | pfile->state.parsing_args = 0; |
1465 | pfile->keep_tokens--; | |
1466 | pfile->state.prevent_expansion--; | |
1467 | ||
1468 | if (buff == NULL) | |
1469 | { | |
1470 | if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr) | |
87cf0651 | 1471 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
d6da836d | 1472 | "function-like macro \"%s\" must be used with arguments in traditional C", |
87cf0651 | 1473 | NODE_NAME (node)); |
d6da836d | 1474 | |
765d600a JJ |
1475 | if (pragma_buff) |
1476 | _cpp_release_buff (pfile, pragma_buff); | |
1477 | ||
828a7f76 | 1478 | pfile->about_to_expand_macro_p = false; |
d6da836d NB |
1479 | return 0; |
1480 | } | |
1481 | ||
e808ec9c | 1482 | if (macro->paramc > 0) |
92582b75 TT |
1483 | replace_args (pfile, node, macro, |
1484 | (macro_arg *) buff->base, | |
1485 | location); | |
1486 | /* Free the memory used by the arguments of this | |
1487 | function-like macro. This memory has been allocated by | |
1488 | funlike_invocation_p and by replace_args. */ | |
1489 | delete_macro_args (buff, num_args); | |
d6da836d | 1490 | } |
93c80368 | 1491 | |
4ed5bcfb | 1492 | /* Disable the macro within its expansion. */ |
644eddaa | 1493 | node->flags |= NODE_DISABLED; |
93c80368 | 1494 | |
800c0e98 NS |
1495 | /* Laziness can only affect the expansion tokens of the macro, |
1496 | not its fun-likeness or parameters. */ | |
e9a2e208 | 1497 | _cpp_maybe_notify_macro_use (pfile, node, location); |
3de8a540 | 1498 | if (pfile->cb.used) |
92582b75 | 1499 | pfile->cb.used (pfile, location, node); |
3de8a540 | 1500 | |
a69cbaac NB |
1501 | macro->used = 1; |
1502 | ||
4ed5bcfb | 1503 | if (macro->paramc == 0) |
92582b75 | 1504 | { |
179652df | 1505 | unsigned tokens_count = macro_real_token_count (macro); |
92582b75 TT |
1506 | if (CPP_OPTION (pfile, track_macro_expansion)) |
1507 | { | |
179652df | 1508 | unsigned int i; |
92582b75 | 1509 | const cpp_token *src = macro->exp.tokens; |
0e50b624 | 1510 | const line_map_macro *map; |
620e594b | 1511 | location_t *virt_locs = NULL; |
179652df JJ |
1512 | _cpp_buff *macro_tokens |
1513 | = tokens_buff_new (pfile, tokens_count, &virt_locs); | |
92582b75 TT |
1514 | |
1515 | /* Create a macro map to record the locations of the | |
1516 | tokens that are involved in the expansion. LOCATION | |
1517 | is the location of the macro expansion point. */ | |
179652df JJ |
1518 | map = linemap_enter_macro (pfile->line_table, |
1519 | node, location, tokens_count); | |
1520 | for (i = 0; i < tokens_count; ++i) | |
92582b75 TT |
1521 | { |
1522 | tokens_buff_add_token (macro_tokens, virt_locs, | |
1523 | src, src->src_loc, | |
1524 | src->src_loc, map, i); | |
1525 | ++src; | |
1526 | } | |
1527 | push_extended_tokens_context (pfile, node, | |
1528 | macro_tokens, | |
1529 | virt_locs, | |
1530 | (const cpp_token **) | |
1531 | macro_tokens->base, | |
179652df | 1532 | tokens_count); |
92582b75 TT |
1533 | } |
1534 | else | |
179652df JJ |
1535 | _cpp_push_token_context (pfile, node, macro->exp.tokens, |
1536 | tokens_count); | |
1537 | num_macro_tokens_counter += tokens_count; | |
92582b75 | 1538 | } |
644eddaa | 1539 | |
765d600a JJ |
1540 | if (pragma_buff) |
1541 | { | |
1542 | if (!pfile->state.in_directive) | |
1543 | _cpp_push_token_context (pfile, NULL, | |
1544 | padding_token (pfile, result), 1); | |
1545 | do | |
1546 | { | |
64a1a422 | 1547 | unsigned tokens_count; |
765d600a JJ |
1548 | _cpp_buff *tail = pragma_buff->next; |
1549 | pragma_buff->next = NULL; | |
64a1a422 TT |
1550 | tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff) |
1551 | - (const cpp_token **) pragma_buff->base); | |
765d600a JJ |
1552 | push_ptoken_context (pfile, NULL, pragma_buff, |
1553 | (const cpp_token **) pragma_buff->base, | |
64a1a422 | 1554 | tokens_count); |
765d600a | 1555 | pragma_buff = tail; |
64a1a422 TT |
1556 | if (!CPP_OPTION (pfile, track_macro_expansion)) |
1557 | num_macro_tokens_counter += tokens_count; | |
1558 | ||
765d600a JJ |
1559 | } |
1560 | while (pragma_buff != NULL); | |
828a7f76 | 1561 | pfile->about_to_expand_macro_p = false; |
765d600a JJ |
1562 | return 2; |
1563 | } | |
1564 | ||
828a7f76 | 1565 | pfile->about_to_expand_macro_p = false; |
644eddaa | 1566 | return 1; |
711b8824 | 1567 | } |
644eddaa | 1568 | |
828a7f76 | 1569 | pfile->about_to_expand_macro_p = false; |
d15a58c0 | 1570 | /* Handle built-in macros and the _Pragma operator. */ |
f8abc9ba | 1571 | { |
620e594b | 1572 | location_t expand_loc; |
64824205 | 1573 | |
f8abc9ba | 1574 | if (/* The top-level macro invocation that triggered the expansion |
3f6677f4 NS |
1575 | we are looking at is with a function-like user macro ... */ |
1576 | cpp_fun_like_macro_p (pfile->top_most_macro_node) | |
6f41f92b BE |
1577 | /* ... and we are tracking the macro expansion. */ |
1578 | && CPP_OPTION (pfile, track_macro_expansion)) | |
1579 | /* Then the location of the end of the macro invocation is the | |
1580 | location of the expansion point of this macro. */ | |
1581 | expand_loc = location; | |
f8abc9ba | 1582 | else |
6f41f92b BE |
1583 | /* Otherwise, the location of the end of the macro invocation is |
1584 | the location of the expansion point of that top-level macro | |
1585 | invocation. */ | |
1586 | expand_loc = pfile->invocation_location; | |
f8abc9ba | 1587 | |
6f41f92b | 1588 | return builtin_macro (pfile, node, location, expand_loc); |
f8abc9ba | 1589 | } |
93c80368 NB |
1590 | } |
1591 | ||
92582b75 TT |
1592 | /* De-allocate the memory used by BUFF which is an array of instances |
1593 | of macro_arg. NUM_ARGS is the number of instances of macro_arg | |
1594 | present in BUFF. */ | |
1595 | static void | |
1596 | delete_macro_args (_cpp_buff *buff, unsigned num_args) | |
1597 | { | |
1598 | macro_arg *macro_args; | |
1599 | unsigned i; | |
1600 | ||
1601 | if (buff == NULL) | |
1602 | return; | |
1603 | ||
1604 | macro_args = (macro_arg *) buff->base; | |
1605 | ||
1606 | /* Walk instances of macro_arg to free their expanded tokens as well | |
1607 | as their macro_arg::virt_locs members. */ | |
1608 | for (i = 0; i < num_args; ++i) | |
1609 | { | |
1610 | if (macro_args[i].expanded) | |
1611 | { | |
1612 | free (macro_args[i].expanded); | |
1613 | macro_args[i].expanded = NULL; | |
1614 | } | |
1615 | if (macro_args[i].virt_locs) | |
1616 | { | |
1617 | free (macro_args[i].virt_locs); | |
1618 | macro_args[i].virt_locs = NULL; | |
1619 | } | |
1620 | if (macro_args[i].expanded_virt_locs) | |
1621 | { | |
1622 | free (macro_args[i].expanded_virt_locs); | |
1623 | macro_args[i].expanded_virt_locs = NULL; | |
1624 | } | |
1625 | } | |
1626 | _cpp_free_buff (buff); | |
1627 | } | |
1628 | ||
1629 | /* Set the INDEXth token of the macro argument ARG. TOKEN is the token | |
1630 | to set, LOCATION is its virtual location. "Virtual" location means | |
7d9641cc | 1631 | the location that encodes loci across macro expansion. Otherwise |
92582b75 TT |
1632 | it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the |
1633 | argument ARG is supposed to contain. Note that ARG must be | |
1634 | tailored so that it has enough room to contain INDEX + 1 numbers of | |
1635 | tokens, at least. */ | |
1636 | static void | |
1637 | set_arg_token (macro_arg *arg, const cpp_token *token, | |
620e594b | 1638 | location_t location, size_t index, |
92582b75 TT |
1639 | enum macro_arg_token_kind kind, |
1640 | bool track_macro_exp_p) | |
1641 | { | |
1642 | const cpp_token **token_ptr; | |
620e594b | 1643 | location_t *loc = NULL; |
92582b75 TT |
1644 | |
1645 | token_ptr = | |
1646 | arg_token_ptr_at (arg, index, kind, | |
1647 | track_macro_exp_p ? &loc : NULL); | |
1648 | *token_ptr = token; | |
1649 | ||
1650 | if (loc != NULL) | |
1651 | { | |
22d66382 MM |
1652 | /* We can't set the location of a stringified argument |
1653 | token and we can't set any location if we aren't tracking | |
1654 | macro expansion locations. */ | |
1655 | gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED | |
1656 | && track_macro_exp_p); | |
92582b75 TT |
1657 | *loc = location; |
1658 | } | |
1659 | } | |
1660 | ||
1661 | /* Get the pointer to the location of the argument token of the | |
1662 | function-like macro argument ARG. This function must be called | |
1663 | only when we -ftrack-macro-expansion is on. */ | |
620e594b | 1664 | static const location_t * |
92582b75 TT |
1665 | get_arg_token_location (const macro_arg *arg, |
1666 | enum macro_arg_token_kind kind) | |
1667 | { | |
620e594b | 1668 | const location_t *loc = NULL; |
92582b75 | 1669 | const cpp_token **token_ptr = |
620e594b | 1670 | arg_token_ptr_at (arg, 0, kind, (location_t **) &loc); |
92582b75 TT |
1671 | |
1672 | if (token_ptr == NULL) | |
1673 | return NULL; | |
1674 | ||
1675 | return loc; | |
1676 | } | |
1677 | ||
1678 | /* Return the pointer to the INDEXth token of the macro argument ARG. | |
1679 | KIND specifies the kind of token the macro argument ARG contains. | |
1680 | If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address | |
1681 | of the virtual location of the returned token if the | |
1682 | -ftrack-macro-expansion flag is on; otherwise, it's set to the | |
1683 | spelling location of the returned token. */ | |
1684 | static const cpp_token ** | |
1685 | arg_token_ptr_at (const macro_arg *arg, size_t index, | |
1686 | enum macro_arg_token_kind kind, | |
620e594b | 1687 | location_t **virt_location) |
92582b75 TT |
1688 | { |
1689 | const cpp_token **tokens_ptr = NULL; | |
1690 | ||
1691 | switch (kind) | |
1692 | { | |
1693 | case MACRO_ARG_TOKEN_NORMAL: | |
1694 | tokens_ptr = arg->first; | |
1695 | break; | |
1696 | case MACRO_ARG_TOKEN_STRINGIFIED: | |
1697 | tokens_ptr = (const cpp_token **) &arg->stringified; | |
1698 | break; | |
1699 | case MACRO_ARG_TOKEN_EXPANDED: | |
1700 | tokens_ptr = arg->expanded; | |
1701 | break; | |
1702 | } | |
1703 | ||
1704 | if (tokens_ptr == NULL) | |
1705 | /* This can happen for e.g, an empty token argument to a | |
1706 | funtion-like macro. */ | |
1707 | return tokens_ptr; | |
1708 | ||
1709 | if (virt_location) | |
1710 | { | |
1711 | if (kind == MACRO_ARG_TOKEN_NORMAL) | |
1712 | *virt_location = &arg->virt_locs[index]; | |
1713 | else if (kind == MACRO_ARG_TOKEN_EXPANDED) | |
1714 | *virt_location = &arg->expanded_virt_locs[index]; | |
1715 | else if (kind == MACRO_ARG_TOKEN_STRINGIFIED) | |
1716 | *virt_location = | |
620e594b | 1717 | (location_t *) &tokens_ptr[index]->src_loc; |
92582b75 TT |
1718 | } |
1719 | return &tokens_ptr[index]; | |
1720 | } | |
1721 | ||
1722 | /* Initialize an iterator so that it iterates over the tokens of a | |
1723 | function-like macro argument. KIND is the kind of tokens we want | |
1724 | ITER to iterate over. TOKEN_PTR points the first token ITER will | |
1725 | iterate over. */ | |
1726 | static void | |
1727 | macro_arg_token_iter_init (macro_arg_token_iter *iter, | |
1728 | bool track_macro_exp_p, | |
1729 | enum macro_arg_token_kind kind, | |
1730 | const macro_arg *arg, | |
1731 | const cpp_token **token_ptr) | |
1732 | { | |
1733 | iter->track_macro_exp_p = track_macro_exp_p; | |
1734 | iter->kind = kind; | |
1735 | iter->token_ptr = token_ptr; | |
d17687f6 DS |
1736 | /* Unconditionally initialize this so that the compiler doesn't warn |
1737 | about iter->location_ptr being possibly uninitialized later after | |
1738 | this code has been inlined somewhere. */ | |
1739 | iter->location_ptr = NULL; | |
92582b75 TT |
1740 | if (track_macro_exp_p) |
1741 | iter->location_ptr = get_arg_token_location (arg, kind); | |
22d66382 | 1742 | #if CHECKING_P |
92582b75 TT |
1743 | iter->num_forwards = 0; |
1744 | if (track_macro_exp_p | |
1745 | && token_ptr != NULL | |
1746 | && iter->location_ptr == NULL) | |
1747 | abort (); | |
1748 | #endif | |
1749 | } | |
1750 | ||
1751 | /* Move the iterator one token forward. Note that if IT was | |
1752 | initialized on an argument that has a stringified token, moving it | |
7d9641cc | 1753 | forward doesn't make sense as a stringified token is essentially one |
92582b75 TT |
1754 | string. */ |
1755 | static void | |
1756 | macro_arg_token_iter_forward (macro_arg_token_iter *it) | |
1757 | { | |
1758 | switch (it->kind) | |
1759 | { | |
1760 | case MACRO_ARG_TOKEN_NORMAL: | |
1761 | case MACRO_ARG_TOKEN_EXPANDED: | |
1762 | it->token_ptr++; | |
1763 | if (it->track_macro_exp_p) | |
1764 | it->location_ptr++; | |
1765 | break; | |
1766 | case MACRO_ARG_TOKEN_STRINGIFIED: | |
22d66382 | 1767 | #if CHECKING_P |
92582b75 TT |
1768 | if (it->num_forwards > 0) |
1769 | abort (); | |
1770 | #endif | |
1771 | break; | |
1772 | } | |
1773 | ||
22d66382 | 1774 | #if CHECKING_P |
92582b75 TT |
1775 | it->num_forwards++; |
1776 | #endif | |
1777 | } | |
1778 | ||
1779 | /* Return the token pointed to by the iterator. */ | |
1780 | static const cpp_token * | |
1781 | macro_arg_token_iter_get_token (const macro_arg_token_iter *it) | |
1782 | { | |
22d66382 | 1783 | #if CHECKING_P |
92582b75 TT |
1784 | if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED |
1785 | && it->num_forwards > 0) | |
1786 | abort (); | |
1787 | #endif | |
1788 | if (it->token_ptr == NULL) | |
1789 | return NULL; | |
1790 | return *it->token_ptr; | |
1791 | } | |
1792 | ||
1793 | /* Return the location of the token pointed to by the iterator.*/ | |
620e594b | 1794 | static location_t |
92582b75 TT |
1795 | macro_arg_token_iter_get_location (const macro_arg_token_iter *it) |
1796 | { | |
22d66382 | 1797 | #if CHECKING_P |
92582b75 TT |
1798 | if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED |
1799 | && it->num_forwards > 0) | |
1800 | abort (); | |
1801 | #endif | |
1802 | if (it->track_macro_exp_p) | |
1803 | return *it->location_ptr; | |
1804 | else | |
1805 | return (*it->token_ptr)->src_loc; | |
1806 | } | |
1807 | ||
1808 | /* Return the index of a token [resulting from macro expansion] inside | |
1809 | the total list of tokens resulting from a given macro | |
1810 | expansion. The index can be different depending on whether if we | |
1811 | want each tokens resulting from function-like macro arguments | |
1812 | expansion to have a different location or not. | |
1813 | ||
1814 | E.g, consider this function-like macro: | |
1815 | ||
1816 | #define M(x) x - 3 | |
1817 | ||
1818 | Then consider us "calling" it (and thus expanding it) like: | |
1819 | ||
1820 | M(1+4) | |
1821 | ||
1822 | It will be expanded into: | |
1823 | ||
1824 | 1+4-3 | |
1825 | ||
1826 | Let's consider the case of the token '4'. | |
1827 | ||
1828 | Its index can be 2 (it's the third token of the set of tokens | |
1829 | resulting from the expansion) or it can be 0 if we consider that | |
1830 | all tokens resulting from the expansion of the argument "1+2" have | |
1831 | the same index, which is 0. In this later case, the index of token | |
1832 | '-' would then be 1 and the index of token '3' would be 2. | |
1833 | ||
1834 | The later case is useful to use less memory e.g, for the case of | |
1835 | the user using the option -ftrack-macro-expansion=1. | |
1836 | ||
1837 | ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we | |
1838 | are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro | |
1839 | parameter (inside the macro replacement list) that corresponds to | |
1840 | the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index | |
1841 | of. | |
1842 | ||
1843 | If we refer to the example above, for the '4' argument token, | |
1844 | ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN | |
1845 | would be set to the token 'x', in the replacement list "x - 3" of | |
1846 | macro M. | |
1847 | ||
1848 | This is a subroutine of replace_args. */ | |
1849 | inline static unsigned | |
1850 | expanded_token_index (cpp_reader *pfile, cpp_macro *macro, | |
1851 | const cpp_token *cur_replacement_token, | |
1852 | unsigned absolute_token_index) | |
1853 | { | |
1854 | if (CPP_OPTION (pfile, track_macro_expansion) > 1) | |
1855 | return absolute_token_index; | |
1856 | return cur_replacement_token - macro->exp.tokens; | |
1857 | } | |
1858 | ||
60887f8c JM |
1859 | /* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG. */ |
1860 | ||
1861 | static void | |
1862 | copy_paste_flag (cpp_reader *pfile, const cpp_token **paste_flag, | |
1863 | const cpp_token *src) | |
1864 | { | |
1865 | cpp_token *token = _cpp_temp_token (pfile); | |
1866 | token->type = (*paste_flag)->type; | |
1867 | token->val = (*paste_flag)->val; | |
1868 | if (src->flags & PASTE_LEFT) | |
1869 | token->flags = (*paste_flag)->flags | PASTE_LEFT; | |
1870 | else | |
1871 | token->flags = (*paste_flag)->flags & ~PASTE_LEFT; | |
1872 | *paste_flag = token; | |
1873 | } | |
1874 | ||
1875 | /* True IFF the last token emitted into BUFF (if any) is PTR. */ | |
1876 | ||
1877 | static bool | |
1878 | last_token_is (_cpp_buff *buff, const cpp_token **ptr) | |
1879 | { | |
1880 | return (ptr && tokens_buff_last_token_ptr (buff) == ptr); | |
1881 | } | |
1882 | ||
d15a58c0 NB |
1883 | /* Replace the parameters in a function-like macro of NODE with the |
1884 | actual ARGS, and place the result in a newly pushed token context. | |
1885 | Expand each argument before replacing, unless it is operated upon | |
92582b75 TT |
1886 | by the # or ## operators. EXPANSION_POINT_LOC is the location of |
1887 | the expansion point of the macro. E.g, the location of the | |
1888 | function-like macro invocation. */ | |
93c80368 | 1889 | static void |
92582b75 | 1890 | replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, |
620e594b | 1891 | macro_arg *args, location_t expansion_point_loc) |
93c80368 NB |
1892 | { |
1893 | unsigned int i, total; | |
1894 | const cpp_token *src, *limit; | |
92582b75 | 1895 | const cpp_token **first = NULL; |
93c80368 | 1896 | macro_arg *arg; |
92582b75 | 1897 | _cpp_buff *buff = NULL; |
620e594b | 1898 | location_t *virt_locs = NULL; |
92582b75 | 1899 | unsigned int exp_count; |
0e50b624 | 1900 | const line_map_macro *map = NULL; |
92582b75 | 1901 | int track_macro_exp; |
93c80368 | 1902 | |
93c80368 | 1903 | /* First, fully macro-expand arguments, calculating the number of |
1e013d2e NB |
1904 | tokens in the final expansion as we go. The ordering of the if |
1905 | statements below is subtle; we must handle stringification before | |
1906 | pasting. */ | |
92582b75 TT |
1907 | |
1908 | /* EXP_COUNT is the number of tokens in the macro replacement | |
1909 | list. TOTAL is the number of tokens /after/ macro parameters | |
1910 | have been replaced by their arguments. */ | |
1911 | exp_count = macro_real_token_count (macro); | |
1912 | total = exp_count; | |
1913 | limit = macro->exp.tokens + exp_count; | |
4ed5bcfb | 1914 | |
601328bb | 1915 | for (src = macro->exp.tokens; src < limit; src++) |
93c80368 NB |
1916 | if (src->type == CPP_MACRO_ARG) |
1917 | { | |
4ed5bcfb NB |
1918 | /* Leading and trailing padding tokens. */ |
1919 | total += 2; | |
92582b75 TT |
1920 | /* Account for leading and padding tokens in exp_count too. |
1921 | This is going to be important later down this function, | |
1922 | when we want to handle the case of (track_macro_exp < | |
1923 | 2). */ | |
1924 | exp_count += 2; | |
4ed5bcfb | 1925 | |
93c80368 NB |
1926 | /* We have an argument. If it is not being stringified or |
1927 | pasted it is macro-replaced before insertion. */ | |
9a0c6187 | 1928 | arg = &args[src->val.macro_arg.arg_no - 1]; |
4c2b647d | 1929 | |
93c80368 NB |
1930 | if (src->flags & STRINGIFY_ARG) |
1931 | { | |
1932 | if (!arg->stringified) | |
ac5fd364 | 1933 | arg->stringified = stringify_arg (pfile, arg->first, arg->count); |
93c80368 NB |
1934 | } |
1935 | else if ((src->flags & PASTE_LEFT) | |
10f04917 | 1936 | || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))) |
93c80368 NB |
1937 | total += arg->count - 1; |
1938 | else | |
1939 | { | |
1940 | if (!arg->expanded) | |
4ed5bcfb | 1941 | expand_arg (pfile, arg); |
93c80368 NB |
1942 | total += arg->expanded_count - 1; |
1943 | } | |
1944 | } | |
1945 | ||
92582b75 TT |
1946 | /* When the compiler is called with the -ftrack-macro-expansion |
1947 | flag, we need to keep track of the location of each token that | |
1948 | results from macro expansion. | |
1949 | ||
1950 | A token resulting from macro expansion is not a new token. It is | |
1951 | simply the same token as the token coming from the macro | |
1952 | definition. The new things that are allocated are the buffer | |
1953 | that holds the tokens resulting from macro expansion and a new | |
1954 | location that records many things like the locus of the expansion | |
1955 | point as well as the original locus inside the definition of the | |
1956 | macro. This location is called a virtual location. | |
1957 | ||
1958 | So the buffer BUFF holds a set of cpp_token*, and the buffer | |
1959 | VIRT_LOCS holds the virtual locations of the tokens held by BUFF. | |
1960 | ||
1961 | Both of these two buffers are going to be hung off of the macro | |
1962 | context, when the latter is pushed. The memory allocated to | |
1963 | store the tokens and their locations is going to be freed once | |
1964 | the context of macro expansion is popped. | |
1965 | ||
1966 | As far as tokens are concerned, the memory overhead of | |
1967 | -ftrack-macro-expansion is proportional to the number of | |
620e594b | 1968 | macros that get expanded multiplied by sizeof (location_t). |
92582b75 TT |
1969 | The good news is that extra memory gets freed when the macro |
1970 | context is freed, i.e shortly after the macro got expanded. */ | |
1971 | ||
1972 | /* Is the -ftrack-macro-expansion flag in effect? */ | |
1973 | track_macro_exp = CPP_OPTION (pfile, track_macro_expansion); | |
1974 | ||
1975 | /* Now allocate memory space for tokens and locations resulting from | |
1976 | the macro expansion, copy the tokens and replace the arguments. | |
1977 | This memory must be freed when the context of the macro MACRO is | |
1978 | popped. */ | |
1979 | buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL); | |
1980 | ||
1e013d2e | 1981 | first = (const cpp_token **) buff->base; |
93c80368 | 1982 | |
92582b75 TT |
1983 | /* Create a macro map to record the locations of the tokens that are |
1984 | involved in the expansion. Note that the expansion point is set | |
1985 | to the location of the closing parenthesis. Otherwise, the | |
1986 | subsequent map created for the first token that comes after the | |
1987 | macro map might have a wrong line number. That would lead to | |
1988 | tokens with wrong line numbers after the macro expansion. This | |
1989 | adds up to the memory overhead of the -ftrack-macro-expansion | |
1990 | flag; for every macro that is expanded, a "macro map" is | |
1991 | created. */ | |
1992 | if (track_macro_exp) | |
1993 | { | |
1994 | int num_macro_tokens = total; | |
1995 | if (track_macro_exp < 2) | |
1996 | /* Then the number of macro tokens won't take in account the | |
1997 | fact that function-like macro arguments can expand to | |
1998 | multiple tokens. This is to save memory at the expense of | |
1999 | accuracy. | |
2000 | ||
8fa4bd81 | 2001 | Suppose we have #define SQUARE(A) A * A |
92582b75 | 2002 | |
8fa4bd81 | 2003 | And then we do SQUARE(2+3) |
92582b75 TT |
2004 | |
2005 | Then the tokens 2, +, 3, will have the same location, | |
2006 | saying they come from the expansion of the argument A. */ | |
2007 | num_macro_tokens = exp_count; | |
2008 | map = linemap_enter_macro (pfile->line_table, node, | |
2009 | expansion_point_loc, | |
2010 | num_macro_tokens); | |
2011 | } | |
2012 | i = 0; | |
e235031d | 2013 | vaopt_state vaopt_tracker (pfile, macro->variadic, &args[macro->paramc - 1]); |
60887f8c | 2014 | const cpp_token **vaopt_start = NULL; |
601328bb | 2015 | for (src = macro->exp.tokens; src < limit; src++) |
4ed5bcfb | 2016 | { |
92582b75 TT |
2017 | unsigned int arg_tokens_count; |
2018 | macro_arg_token_iter from; | |
2019 | const cpp_token **paste_flag = NULL; | |
2020 | const cpp_token **tmp_token_ptr; | |
93c80368 | 2021 | |
fb771b9d | 2022 | /* __VA_OPT__ handling. */ |
60887f8c | 2023 | vaopt_state::update_type vostate = vaopt_tracker.update (src); |
e928cf47 | 2024 | if (__builtin_expect (vostate != vaopt_state::INCLUDE, false)) |
60887f8c JM |
2025 | { |
2026 | if (vostate == vaopt_state::BEGIN) | |
2027 | { | |
2028 | /* Padding on the left of __VA_OPT__ (unless RHS of ##). */ | |
2029 | if (src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)) | |
2030 | { | |
2031 | const cpp_token *t = padding_token (pfile, src); | |
2032 | unsigned index = expanded_token_index (pfile, macro, src, i); | |
2033 | /* Allocate a virtual location for the padding token and | |
2034 | append the token and its location to BUFF and | |
2035 | VIRT_LOCS. */ | |
2036 | tokens_buff_add_token (buff, virt_locs, t, | |
2037 | t->src_loc, t->src_loc, | |
2038 | map, index); | |
2039 | } | |
2040 | vaopt_start = tokens_buff_last_token_ptr (buff); | |
2041 | } | |
2042 | else if (vostate == vaopt_state::END) | |
2043 | { | |
2044 | const cpp_token **start = vaopt_start; | |
2045 | vaopt_start = NULL; | |
2046 | ||
60887f8c | 2047 | paste_flag = tokens_buff_last_token_ptr (buff); |
60887f8c | 2048 | |
d5659997 JJ |
2049 | if (vaopt_tracker.stringify ()) |
2050 | { | |
2051 | unsigned int count | |
2052 | = start ? paste_flag - start : tokens_buff_count (buff); | |
ac5fd364 JJ |
2053 | const cpp_token **first |
2054 | = start ? start + 1 | |
2055 | : (const cpp_token **) (buff->base); | |
2056 | unsigned int i, j; | |
2057 | ||
2058 | /* Paste any tokens that need to be pasted before calling | |
2059 | stringify_arg, because stringify_arg uses pfile->u_buff | |
2060 | which paste_tokens can use as well. */ | |
2061 | for (i = 0, j = 0; i < count; i++, j++) | |
2062 | { | |
2063 | const cpp_token *token = first[i]; | |
2064 | ||
2065 | if (token->flags & PASTE_LEFT) | |
2066 | { | |
2067 | location_t virt_loc = pfile->invocation_location; | |
2068 | const cpp_token *rhs; | |
2069 | do | |
2070 | { | |
2071 | if (i == count) | |
2072 | abort (); | |
2073 | rhs = first[++i]; | |
2074 | if (!paste_tokens (pfile, virt_loc, &token, rhs)) | |
2075 | { | |
2076 | --i; | |
2077 | break; | |
2078 | } | |
2079 | } | |
2080 | while (rhs->flags & PASTE_LEFT); | |
2081 | } | |
2082 | ||
2083 | first[j] = token; | |
2084 | } | |
2085 | if (j != i) | |
2086 | { | |
2087 | while (i-- != j) | |
2088 | tokens_buff_remove_last_token (buff); | |
2089 | count = j; | |
2090 | } | |
2091 | ||
2092 | const cpp_token *t = stringify_arg (pfile, first, count); | |
d5659997 JJ |
2093 | while (count--) |
2094 | tokens_buff_remove_last_token (buff); | |
2095 | if (src->flags & PASTE_LEFT) | |
2096 | copy_paste_flag (pfile, &t, src); | |
2097 | tokens_buff_add_token (buff, virt_locs, | |
2098 | t, t->src_loc, t->src_loc, | |
2099 | NULL, 0); | |
5545d1ed | 2100 | continue; |
d5659997 | 2101 | } |
5545d1ed JJ |
2102 | if (start && paste_flag == start && (*start)->flags & PASTE_LEFT) |
2103 | /* If __VA_OPT__ expands to nothing (either because __VA_ARGS__ | |
2104 | is empty or because it is __VA_OPT__() ), drop PASTE_LEFT | |
2105 | flag from previous token. */ | |
2106 | copy_paste_flag (pfile, start, &pfile->avoid_paste); | |
2107 | if (src->flags & PASTE_LEFT) | |
60887f8c | 2108 | { |
ac6e77aa JM |
2109 | /* Don't avoid paste after all. */ |
2110 | while (paste_flag && paste_flag != start | |
2111 | && *paste_flag == &pfile->avoid_paste) | |
2112 | { | |
2113 | tokens_buff_remove_last_token (buff); | |
2114 | paste_flag = tokens_buff_last_token_ptr (buff); | |
2115 | } | |
2116 | ||
60887f8c JM |
2117 | /* With a non-empty __VA_OPT__ on the LHS of ##, the last |
2118 | token should be flagged PASTE_LEFT. */ | |
2119 | if (paste_flag && (*paste_flag)->type != CPP_PADDING) | |
2120 | copy_paste_flag (pfile, paste_flag, src); | |
2121 | } | |
2122 | else | |
2123 | { | |
2124 | /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or | |
2125 | __VA_OPT__(c)__VA_OPT__(d). */ | |
2126 | const cpp_token *t = &pfile->avoid_paste; | |
2127 | tokens_buff_add_token (buff, virt_locs, | |
2128 | t, t->src_loc, t->src_loc, | |
2129 | NULL, 0); | |
2130 | } | |
2131 | } | |
2132 | continue; | |
2133 | } | |
fb771b9d | 2134 | |
4ed5bcfb NB |
2135 | if (src->type != CPP_MACRO_ARG) |
2136 | { | |
92582b75 TT |
2137 | /* Allocate a virtual location for token SRC, and add that |
2138 | token and its virtual location into the buffers BUFF and | |
2139 | VIRT_LOCS. */ | |
2140 | unsigned index = expanded_token_index (pfile, macro, src, i); | |
2141 | tokens_buff_add_token (buff, virt_locs, src, | |
2142 | src->src_loc, src->src_loc, | |
2143 | map, index); | |
2144 | i += 1; | |
4ed5bcfb NB |
2145 | continue; |
2146 | } | |
93c80368 | 2147 | |
4ed5bcfb | 2148 | paste_flag = 0; |
9a0c6187 | 2149 | arg = &args[src->val.macro_arg.arg_no - 1]; |
92582b75 TT |
2150 | /* SRC is a macro parameter that we need to replace with its |
2151 | corresponding argument. So at some point we'll need to | |
2152 | iterate over the tokens of the macro argument and copy them | |
2153 | into the "place" now holding the correspondig macro | |
2154 | parameter. We are going to use the iterator type | |
2155 | macro_argo_token_iter to handle that iterating. The 'if' | |
2156 | below is to initialize the iterator depending on the type of | |
2157 | tokens the macro argument has. It also does some adjustment | |
2158 | related to padding tokens and some pasting corner cases. */ | |
4ed5bcfb | 2159 | if (src->flags & STRINGIFY_ARG) |
92582b75 TT |
2160 | { |
2161 | arg_tokens_count = 1; | |
2162 | macro_arg_token_iter_init (&from, | |
2163 | CPP_OPTION (pfile, | |
2164 | track_macro_expansion), | |
2165 | MACRO_ARG_TOKEN_STRINGIFIED, | |
2166 | arg, &arg->stringified); | |
2167 | } | |
4ed5bcfb | 2168 | else if (src->flags & PASTE_LEFT) |
92582b75 TT |
2169 | { |
2170 | arg_tokens_count = arg->count; | |
2171 | macro_arg_token_iter_init (&from, | |
2172 | CPP_OPTION (pfile, | |
2173 | track_macro_expansion), | |
2174 | MACRO_ARG_TOKEN_NORMAL, | |
2175 | arg, arg->first); | |
2176 | } | |
601328bb | 2177 | else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)) |
4ed5bcfb | 2178 | { |
92582b75 TT |
2179 | int num_toks; |
2180 | arg_tokens_count = arg->count; | |
2181 | macro_arg_token_iter_init (&from, | |
2182 | CPP_OPTION (pfile, | |
2183 | track_macro_expansion), | |
2184 | MACRO_ARG_TOKEN_NORMAL, | |
2185 | arg, arg->first); | |
2186 | ||
2187 | num_toks = tokens_buff_count (buff); | |
2188 | ||
2189 | if (num_toks != 0) | |
4ed5bcfb | 2190 | { |
92582b75 TT |
2191 | /* So the current parameter token is pasted to the previous |
2192 | token in the replacement list. Let's look at what | |
2193 | we have as previous and current arguments. */ | |
2194 | ||
2195 | /* This is the previous argument's token ... */ | |
2196 | tmp_token_ptr = tokens_buff_last_token_ptr (buff); | |
2197 | ||
2198 | if ((*tmp_token_ptr)->type == CPP_COMMA | |
4ed5bcfb | 2199 | && macro->variadic |
9a0c6187 | 2200 | && src->val.macro_arg.arg_no == macro->paramc) |
4ed5bcfb | 2201 | { |
92582b75 TT |
2202 | /* ... which is a comma; and the current parameter |
2203 | is the last parameter of a variadic function-like | |
2204 | macro. If the argument to the current last | |
2205 | parameter is NULL, then swallow the comma, | |
2206 | otherwise drop the paste flag. */ | |
2207 | if (macro_arg_token_iter_get_token (&from) == NULL) | |
2208 | tokens_buff_remove_last_token (buff); | |
4ed5bcfb | 2209 | else |
92582b75 | 2210 | paste_flag = tmp_token_ptr; |
4ed5bcfb | 2211 | } |
e928cf47 JJ |
2212 | /* Remove the paste flag if the RHS is a placemarker. */ |
2213 | else if (arg_tokens_count == 0) | |
92582b75 | 2214 | paste_flag = tmp_token_ptr; |
4ed5bcfb NB |
2215 | } |
2216 | } | |
2217 | else | |
92582b75 TT |
2218 | { |
2219 | arg_tokens_count = arg->expanded_count; | |
2220 | macro_arg_token_iter_init (&from, | |
2221 | CPP_OPTION (pfile, | |
2222 | track_macro_expansion), | |
2223 | MACRO_ARG_TOKEN_EXPANDED, | |
2224 | arg, arg->expanded); | |
60887f8c JM |
2225 | |
2226 | if (last_token_is (buff, vaopt_start)) | |
2227 | { | |
2228 | /* We're expanding an arg at the beginning of __VA_OPT__. | |
2229 | Skip padding. */ | |
2230 | while (arg_tokens_count) | |
2231 | { | |
2232 | const cpp_token *t = macro_arg_token_iter_get_token (&from); | |
2233 | if (t->type != CPP_PADDING) | |
2234 | break; | |
2235 | macro_arg_token_iter_forward (&from); | |
2236 | --arg_tokens_count; | |
2237 | } | |
2238 | } | |
92582b75 | 2239 | } |
4c2b647d | 2240 | |
4ed5bcfb | 2241 | /* Padding on the left of an argument (unless RHS of ##). */ |
a8d0ddaf | 2242 | if ((!pfile->state.in_directive || pfile->state.directive_wants_padding) |
e928cf47 JJ |
2243 | && src != macro->exp.tokens |
2244 | && !(src[-1].flags & PASTE_LEFT) | |
60887f8c | 2245 | && !last_token_is (buff, vaopt_start)) |
92582b75 TT |
2246 | { |
2247 | const cpp_token *t = padding_token (pfile, src); | |
2248 | unsigned index = expanded_token_index (pfile, macro, src, i); | |
2249 | /* Allocate a virtual location for the padding token and | |
2250 | append the token and its location to BUFF and | |
2251 | VIRT_LOCS. */ | |
2252 | tokens_buff_add_token (buff, virt_locs, t, | |
2253 | t->src_loc, t->src_loc, | |
2254 | map, index); | |
2255 | } | |
93c80368 | 2256 | |
92582b75 | 2257 | if (arg_tokens_count) |
4ed5bcfb | 2258 | { |
92582b75 TT |
2259 | /* So now we've got the number of tokens that make up the |
2260 | argument that is going to replace the current parameter | |
2261 | in the macro's replacement list. */ | |
2262 | unsigned int j; | |
2263 | for (j = 0; j < arg_tokens_count; ++j) | |
2264 | { | |
2265 | /* So if track_macro_exp is < 2, the user wants to | |
2266 | save extra memory while tracking macro expansion | |
2267 | locations. So in that case here is what we do: | |
2268 | ||
8fa4bd81 | 2269 | Suppose we have #define SQUARE(A) A * A |
92582b75 | 2270 | |
8fa4bd81 | 2271 | And then we do SQUARE(2+3) |
92582b75 TT |
2272 | |
2273 | Then the tokens 2, +, 3, will have the same location, | |
2274 | saying they come from the expansion of the argument | |
2275 | A. | |
2276 | ||
2277 | So that means we are going to ignore the COUNT tokens | |
2278 | resulting from the expansion of the current macro | |
8fa4bd81 | 2279 | argument. In other words all the ARG_TOKENS_COUNT tokens |
92582b75 | 2280 | resulting from the expansion of the macro argument will |
8fa4bd81 | 2281 | have the index I. Normally, each of those tokens should |
92582b75 TT |
2282 | have index I+J. */ |
2283 | unsigned token_index = i; | |
2284 | unsigned index; | |
2285 | if (track_macro_exp > 1) | |
2286 | token_index += j; | |
2287 | ||
2288 | index = expanded_token_index (pfile, macro, src, token_index); | |
e928cf47 | 2289 | const cpp_token *tok = macro_arg_token_iter_get_token (&from); |
e928cf47 | 2290 | tokens_buff_add_token (buff, virt_locs, tok, |
92582b75 TT |
2291 | macro_arg_token_iter_get_location (&from), |
2292 | src->src_loc, map, index); | |
2293 | macro_arg_token_iter_forward (&from); | |
2294 | } | |
93c80368 | 2295 | |
4ed5bcfb NB |
2296 | /* With a non-empty argument on the LHS of ##, the last |
2297 | token should be flagged PASTE_LEFT. */ | |
2298 | if (src->flags & PASTE_LEFT) | |
8fa4bd81 JM |
2299 | paste_flag |
2300 | = (const cpp_token **) tokens_buff_last_token_ptr (buff); | |
4ed5bcfb | 2301 | } |
2b71f4a4 | 2302 | else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99) |
bf425849 | 2303 | && ! macro->syshdr && ! _cpp_in_system_header (pfile)) |
e85edc9e | 2304 | { |
3976796b | 2305 | if (CPP_OPTION (pfile, cplusplus)) |
2b71f4a4 MLI |
2306 | cpp_pedwarning (pfile, CPP_W_PEDANTIC, |
2307 | "invoking macro %s argument %d: " | |
2308 | "empty macro arguments are undefined" | |
2309 | " in ISO C++98", | |
2310 | NODE_NAME (node), src->val.macro_arg.arg_no); | |
177cce46 | 2311 | else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat)) |
2b71f4a4 MLI |
2312 | cpp_pedwarning (pfile, |
2313 | CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 | |
2314 | ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC, | |
2315 | "invoking macro %s argument %d: " | |
2316 | "empty macro arguments are undefined" | |
2317 | " in ISO C90", | |
2318 | NODE_NAME (node), src->val.macro_arg.arg_no); | |
e85edc9e | 2319 | } |
177cce46 | 2320 | else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 |
2b71f4a4 | 2321 | && ! CPP_OPTION (pfile, cplusplus) |
bf425849 | 2322 | && ! macro->syshdr && ! _cpp_in_system_header (pfile)) |
2b71f4a4 MLI |
2323 | cpp_warning (pfile, CPP_W_C90_C99_COMPAT, |
2324 | "invoking macro %s argument %d: " | |
2325 | "empty macro arguments are undefined" | |
2326 | " in ISO C90", | |
2327 | NODE_NAME (node), src->val.macro_arg.arg_no); | |
26ec42ee | 2328 | |
4ed5bcfb | 2329 | /* Avoid paste on RHS (even case count == 0). */ |
e928cf47 | 2330 | if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) |
92582b75 TT |
2331 | { |
2332 | const cpp_token *t = &pfile->avoid_paste; | |
2333 | tokens_buff_add_token (buff, virt_locs, | |
2334 | t, t->src_loc, t->src_loc, | |
2335 | NULL, 0); | |
2336 | } | |
93c80368 | 2337 | |
4ed5bcfb NB |
2338 | /* Add a new paste flag, or remove an unwanted one. */ |
2339 | if (paste_flag) | |
60887f8c | 2340 | copy_paste_flag (pfile, paste_flag, src); |
4c2b647d | 2341 | |
92582b75 TT |
2342 | i += arg_tokens_count; |
2343 | } | |
4ed5bcfb | 2344 | |
92582b75 TT |
2345 | if (track_macro_exp) |
2346 | push_extended_tokens_context (pfile, node, buff, virt_locs, first, | |
2347 | tokens_buff_count (buff)); | |
2348 | else | |
2349 | push_ptoken_context (pfile, node, buff, first, | |
2350 | tokens_buff_count (buff)); | |
64a1a422 TT |
2351 | |
2352 | num_macro_tokens_counter += tokens_buff_count (buff); | |
4ed5bcfb NB |
2353 | } |
2354 | ||
2355 | /* Return a special padding token, with padding inherited from SOURCE. */ | |
2356 | static const cpp_token * | |
6cf87ca4 | 2357 | padding_token (cpp_reader *pfile, const cpp_token *source) |
4ed5bcfb NB |
2358 | { |
2359 | cpp_token *result = _cpp_temp_token (pfile); | |
2360 | ||
2361 | result->type = CPP_PADDING; | |
be0f1e54 PB |
2362 | |
2363 | /* Data in GCed data structures cannot be made const so far, so we | |
2364 | need a cast here. */ | |
2365 | result->val.source = (cpp_token *) source; | |
4ed5bcfb NB |
2366 | result->flags = 0; |
2367 | return result; | |
2368 | } | |
2369 | ||
d15a58c0 NB |
2370 | /* Get a new uninitialized context. Create a new one if we cannot |
2371 | re-use an old one. */ | |
4ed5bcfb | 2372 | static cpp_context * |
6cf87ca4 | 2373 | next_context (cpp_reader *pfile) |
4ed5bcfb NB |
2374 | { |
2375 | cpp_context *result = pfile->context->next; | |
2376 | ||
2377 | if (result == 0) | |
711b8824 | 2378 | { |
72bb2c39 | 2379 | result = XNEW (cpp_context); |
92582b75 | 2380 | memset (result, 0, sizeof (cpp_context)); |
4ed5bcfb NB |
2381 | result->prev = pfile->context; |
2382 | result->next = 0; | |
2383 | pfile->context->next = result; | |
93c80368 | 2384 | } |
4ed5bcfb NB |
2385 | |
2386 | pfile->context = result; | |
2387 | return result; | |
93c80368 NB |
2388 | } |
2389 | ||
4ed5bcfb NB |
2390 | /* Push a list of pointers to tokens. */ |
2391 | static void | |
6cf87ca4 ZW |
2392 | push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff, |
2393 | const cpp_token **first, unsigned int count) | |
93c80368 NB |
2394 | { |
2395 | cpp_context *context = next_context (pfile); | |
93c80368 | 2396 | |
92582b75 TT |
2397 | context->tokens_kind = TOKENS_KIND_INDIRECT; |
2398 | context->c.macro = macro; | |
1e013d2e | 2399 | context->buff = buff; |
82eda77e NB |
2400 | FIRST (context).ptoken = first; |
2401 | LAST (context).ptoken = first + count; | |
4ed5bcfb NB |
2402 | } |
2403 | ||
3600218c DS |
2404 | /* Push a list of tokens. |
2405 | ||
2406 | A NULL macro means that we should continue the current macro | |
2407 | expansion, in essence. That means that if we are currently in a | |
2408 | macro expansion context, we'll make the new pfile->context refer to | |
2409 | the current macro. */ | |
bc4071dd RH |
2410 | void |
2411 | _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro, | |
2412 | const cpp_token *first, unsigned int count) | |
4ed5bcfb | 2413 | { |
3600218c DS |
2414 | cpp_context *context; |
2415 | ||
2416 | if (macro == NULL) | |
2417 | macro = macro_of_context (pfile->context); | |
2418 | ||
2419 | context = next_context (pfile); | |
92582b75 TT |
2420 | context->tokens_kind = TOKENS_KIND_DIRECT; |
2421 | context->c.macro = macro; | |
2422 | context->buff = NULL; | |
3600218c DS |
2423 | FIRST (context).token = first; |
2424 | LAST (context).token = first + count; | |
93c80368 NB |
2425 | } |
2426 | ||
92582b75 TT |
2427 | /* Build a context containing a list of tokens as well as their |
2428 | virtual locations and push it. TOKENS_BUFF is the buffer that | |
2429 | contains the tokens pointed to by FIRST. If TOKENS_BUFF is | |
2430 | non-NULL, it means that the context owns it, meaning that | |
2431 | _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that | |
3600218c DS |
2432 | contains the virtual locations. |
2433 | ||
2434 | A NULL macro means that we should continue the current macro | |
2435 | expansion, in essence. That means that if we are currently in a | |
2436 | macro expansion context, we'll make the new pfile->context refer to | |
2437 | the current macro. */ | |
92582b75 TT |
2438 | static void |
2439 | push_extended_tokens_context (cpp_reader *pfile, | |
2440 | cpp_hashnode *macro, | |
2441 | _cpp_buff *token_buff, | |
620e594b | 2442 | location_t *virt_locs, |
92582b75 TT |
2443 | const cpp_token **first, |
2444 | unsigned int count) | |
2445 | { | |
3600218c | 2446 | cpp_context *context; |
92582b75 TT |
2447 | macro_context *m; |
2448 | ||
3600218c DS |
2449 | if (macro == NULL) |
2450 | macro = macro_of_context (pfile->context); | |
2451 | ||
2452 | context = next_context (pfile); | |
92582b75 TT |
2453 | context->tokens_kind = TOKENS_KIND_EXTENDED; |
2454 | context->buff = token_buff; | |
2455 | ||
2456 | m = XNEW (macro_context); | |
2457 | m->macro_node = macro; | |
2458 | m->virt_locs = virt_locs; | |
2459 | m->cur_virt_loc = virt_locs; | |
2460 | context->c.mc = m; | |
2461 | FIRST (context).ptoken = first; | |
2462 | LAST (context).ptoken = first + count; | |
2463 | } | |
2464 | ||
cbc69f84 NB |
2465 | /* Push a traditional macro's replacement text. */ |
2466 | void | |
6cf87ca4 ZW |
2467 | _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro, |
2468 | const uchar *start, size_t len) | |
cbc69f84 NB |
2469 | { |
2470 | cpp_context *context = next_context (pfile); | |
2471 | ||
92582b75 TT |
2472 | context->tokens_kind = TOKENS_KIND_DIRECT; |
2473 | context->c.macro = macro; | |
cbc69f84 NB |
2474 | context->buff = NULL; |
2475 | CUR (context) = start; | |
1ce676a0 | 2476 | RLIMIT (context) = start + len; |
974c43f1 | 2477 | macro->flags |= NODE_DISABLED; |
cbc69f84 NB |
2478 | } |
2479 | ||
92582b75 TT |
2480 | /* Creates a buffer that holds tokens a.k.a "token buffer", usually |
2481 | for the purpose of storing them on a cpp_context. If VIRT_LOCS is | |
2482 | non-null (which means that -ftrack-macro-expansion is on), | |
2483 | *VIRT_LOCS is set to a newly allocated buffer that is supposed to | |
2484 | hold the virtual locations of the tokens resulting from macro | |
2485 | expansion. */ | |
2486 | static _cpp_buff* | |
2487 | tokens_buff_new (cpp_reader *pfile, size_t len, | |
620e594b | 2488 | location_t **virt_locs) |
92582b75 TT |
2489 | { |
2490 | size_t tokens_size = len * sizeof (cpp_token *); | |
620e594b | 2491 | size_t locs_size = len * sizeof (location_t); |
92582b75 TT |
2492 | |
2493 | if (virt_locs != NULL) | |
620e594b | 2494 | *virt_locs = XNEWVEC (location_t, locs_size); |
92582b75 TT |
2495 | return _cpp_get_buff (pfile, tokens_size); |
2496 | } | |
2497 | ||
2498 | /* Returns the number of tokens contained in a token buffer. The | |
2499 | buffer holds a set of cpp_token*. */ | |
2500 | static size_t | |
2501 | tokens_buff_count (_cpp_buff *buff) | |
2502 | { | |
2503 | return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *); | |
2504 | } | |
2505 | ||
2506 | /* Return a pointer to the last token contained in the token buffer | |
2507 | BUFF. */ | |
2508 | static const cpp_token ** | |
2509 | tokens_buff_last_token_ptr (_cpp_buff *buff) | |
2510 | { | |
60887f8c JM |
2511 | if (BUFF_FRONT (buff) == buff->base) |
2512 | return NULL; | |
92582b75 TT |
2513 | return &((const cpp_token **) BUFF_FRONT (buff))[-1]; |
2514 | } | |
2515 | ||
2516 | /* Remove the last token contained in the token buffer TOKENS_BUFF. | |
2517 | If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer | |
2518 | containing the virtual locations of the tokens in TOKENS_BUFF; in | |
2519 | which case the function updates that buffer as well. */ | |
2520 | static inline void | |
2521 | tokens_buff_remove_last_token (_cpp_buff *tokens_buff) | |
2522 | ||
2523 | { | |
2524 | if (BUFF_FRONT (tokens_buff) > tokens_buff->base) | |
2525 | BUFF_FRONT (tokens_buff) = | |
2526 | (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1]; | |
2527 | } | |
2528 | ||
2529 | /* Insert a token into the token buffer at the position pointed to by | |
2530 | DEST. Note that the buffer is not enlarged so the previous token | |
2531 | that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null, | |
2532 | means -ftrack-macro-expansion is effect; it then points to where to | |
2533 | insert the virtual location of TOKEN. TOKEN is the token to | |
2534 | insert. VIRT_LOC is the virtual location of the token, i.e, the | |
7d9641cc | 2535 | location possibly encoding its locus across macro expansion. If |
92582b75 TT |
2536 | TOKEN is an argument of a function-like macro (inside a macro |
2537 | replacement list), PARM_DEF_LOC is the spelling location of the | |
2538 | macro parameter that TOKEN is replacing, in the replacement list of | |
2539 | the macro. If TOKEN is not an argument of a function-like macro or | |
2540 | if it doesn't come from a macro expansion, then VIRT_LOC can just | |
2541 | be set to the same value as PARM_DEF_LOC. If MAP is non null, it | |
2542 | means TOKEN comes from a macro expansion and MAP is the macro map | |
2543 | associated to the macro. MACRO_TOKEN_INDEX points to the index of | |
2544 | the token in the macro map; it is not considered if MAP is NULL. | |
2545 | ||
2546 | Upon successful completion this function returns the a pointer to | |
2547 | the position of the token coming right after the insertion | |
2548 | point. */ | |
2549 | static inline const cpp_token ** | |
2550 | tokens_buff_put_token_to (const cpp_token **dest, | |
620e594b | 2551 | location_t *virt_loc_dest, |
92582b75 | 2552 | const cpp_token *token, |
620e594b DM |
2553 | location_t virt_loc, |
2554 | location_t parm_def_loc, | |
0e50b624 | 2555 | const line_map_macro *map, |
92582b75 TT |
2556 | unsigned int macro_token_index) |
2557 | { | |
620e594b | 2558 | location_t macro_loc = virt_loc; |
92582b75 TT |
2559 | const cpp_token **result; |
2560 | ||
2561 | if (virt_loc_dest) | |
2562 | { | |
2563 | /* -ftrack-macro-expansion is on. */ | |
2564 | if (map) | |
2565 | macro_loc = linemap_add_macro_token (map, macro_token_index, | |
2566 | virt_loc, parm_def_loc); | |
2567 | *virt_loc_dest = macro_loc; | |
2568 | } | |
2569 | *dest = token; | |
2570 | result = &dest[1]; | |
2571 | ||
2572 | return result; | |
2573 | } | |
2574 | ||
2575 | /* Adds a token at the end of the tokens contained in BUFFER. Note | |
2576 | that this function doesn't enlarge BUFFER when the number of tokens | |
2577 | reaches BUFFER's size; it aborts in that situation. | |
2578 | ||
2579 | TOKEN is the token to append. VIRT_LOC is the virtual location of | |
7d9641cc | 2580 | the token, i.e, the location possibly encoding its locus across |
92582b75 TT |
2581 | macro expansion. If TOKEN is an argument of a function-like macro |
2582 | (inside a macro replacement list), PARM_DEF_LOC is the location of | |
2583 | the macro parameter that TOKEN is replacing. If TOKEN doesn't come | |
2584 | from a macro expansion, then VIRT_LOC can just be set to the same | |
2585 | value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes | |
2586 | from a macro expansion and MAP is the macro map associated to the | |
2587 | macro. MACRO_TOKEN_INDEX points to the index of the token in the | |
2588 | macro map; It is not considered if MAP is NULL. If VIRT_LOCS is | |
2589 | non-null, it means -ftrack-macro-expansion is on; in which case | |
2590 | this function adds the virtual location DEF_LOC to the VIRT_LOCS | |
2591 | array, at the same index as the one of TOKEN in BUFFER. Upon | |
2592 | successful completion this function returns the a pointer to the | |
2593 | position of the token coming right after the insertion point. */ | |
2594 | static const cpp_token ** | |
2595 | tokens_buff_add_token (_cpp_buff *buffer, | |
620e594b | 2596 | location_t *virt_locs, |
92582b75 | 2597 | const cpp_token *token, |
620e594b DM |
2598 | location_t virt_loc, |
2599 | location_t parm_def_loc, | |
0e50b624 | 2600 | const line_map_macro *map, |
92582b75 TT |
2601 | unsigned int macro_token_index) |
2602 | { | |
2603 | const cpp_token **result; | |
620e594b | 2604 | location_t *virt_loc_dest = NULL; |
92582b75 TT |
2605 | unsigned token_index = |
2606 | (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *); | |
2607 | ||
2608 | /* Abort if we pass the end the buffer. */ | |
2609 | if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer)) | |
2610 | abort (); | |
2611 | ||
2612 | if (virt_locs != NULL) | |
2613 | virt_loc_dest = &virt_locs[token_index]; | |
2614 | ||
2615 | result = | |
2616 | tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer), | |
2617 | virt_loc_dest, token, virt_loc, parm_def_loc, | |
2618 | map, macro_token_index); | |
2619 | ||
2620 | BUFF_FRONT (buffer) = (unsigned char *) result; | |
2621 | return result; | |
2622 | } | |
2623 | ||
2624 | /* Allocate space for the function-like macro argument ARG to store | |
2625 | the tokens resulting from the macro-expansion of the tokens that | |
2626 | make up ARG itself. That space is allocated in ARG->expanded and | |
2627 | needs to be freed using free. */ | |
2628 | static void | |
2629 | alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity) | |
2630 | { | |
22d66382 MM |
2631 | gcc_checking_assert (arg->expanded == NULL |
2632 | && arg->expanded_virt_locs == NULL); | |
2633 | ||
92582b75 TT |
2634 | arg->expanded = XNEWVEC (const cpp_token *, capacity); |
2635 | if (CPP_OPTION (pfile, track_macro_expansion)) | |
620e594b | 2636 | arg->expanded_virt_locs = XNEWVEC (location_t, capacity); |
92582b75 TT |
2637 | |
2638 | } | |
2639 | ||
2640 | /* If necessary, enlarge ARG->expanded to so that it can contain SIZE | |
2641 | tokens. */ | |
2642 | static void | |
2643 | ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg, | |
2644 | size_t size, size_t *expanded_capacity) | |
2645 | { | |
2646 | if (size <= *expanded_capacity) | |
2647 | return; | |
2648 | ||
2649 | size *= 2; | |
2650 | ||
2651 | arg->expanded = | |
2652 | XRESIZEVEC (const cpp_token *, arg->expanded, size); | |
2653 | *expanded_capacity = size; | |
2654 | ||
2655 | if (CPP_OPTION (pfile, track_macro_expansion)) | |
2656 | { | |
2657 | if (arg->expanded_virt_locs == NULL) | |
620e594b | 2658 | arg->expanded_virt_locs = XNEWVEC (location_t, size); |
92582b75 | 2659 | else |
620e594b | 2660 | arg->expanded_virt_locs = XRESIZEVEC (location_t, |
92582b75 TT |
2661 | arg->expanded_virt_locs, |
2662 | size); | |
2663 | } | |
2664 | } | |
2665 | ||
d15a58c0 NB |
2666 | /* Expand an argument ARG before replacing parameters in a |
2667 | function-like macro. This works by pushing a context with the | |
2668 | argument's tokens, and then expanding that into a temporary buffer | |
2669 | as if it were a normal part of the token stream. collect_args() | |
2670 | has terminated the argument's tokens with a CPP_EOF so that we know | |
2671 | when we have fully expanded the argument. */ | |
93c80368 | 2672 | static void |
6cf87ca4 | 2673 | expand_arg (cpp_reader *pfile, macro_arg *arg) |
93c80368 | 2674 | { |
92582b75 | 2675 | size_t capacity; |
56941bf2 | 2676 | bool saved_warn_trad; |
92582b75 | 2677 | bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion); |
a6e0d593 | 2678 | bool saved_ignore__Pragma; |
4ed5bcfb | 2679 | |
92582b75 TT |
2680 | if (arg->count == 0 |
2681 | || arg->expanded != NULL) | |
4ed5bcfb | 2682 | return; |
93c80368 | 2683 | |
56941bf2 NB |
2684 | /* Don't warn about funlike macros when pre-expanding. */ |
2685 | saved_warn_trad = CPP_WTRADITIONAL (pfile); | |
2686 | CPP_WTRADITIONAL (pfile) = 0; | |
2687 | ||
92582b75 | 2688 | /* Loop, reading in the tokens of the argument. */ |
4ed5bcfb | 2689 | capacity = 256; |
92582b75 TT |
2690 | alloc_expanded_arg_mem (pfile, arg, capacity); |
2691 | ||
2692 | if (track_macro_exp_p) | |
2693 | push_extended_tokens_context (pfile, NULL, NULL, | |
2694 | arg->virt_locs, | |
2695 | arg->first, | |
2696 | arg->count + 1); | |
2697 | else | |
2698 | push_ptoken_context (pfile, NULL, NULL, | |
2699 | arg->first, arg->count + 1); | |
93c80368 | 2700 | |
a6e0d593 JJ |
2701 | saved_ignore__Pragma = pfile->state.ignore__Pragma; |
2702 | pfile->state.ignore__Pragma = 1; | |
2703 | ||
4ed5bcfb | 2704 | for (;;) |
93c80368 | 2705 | { |
4ed5bcfb | 2706 | const cpp_token *token; |
620e594b | 2707 | location_t location; |
4ed5bcfb | 2708 | |
92582b75 TT |
2709 | ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1, |
2710 | &capacity); | |
93c80368 | 2711 | |
92582b75 | 2712 | token = cpp_get_token_1 (pfile, &location); |
93c80368 | 2713 | |
4ed5bcfb NB |
2714 | if (token->type == CPP_EOF) |
2715 | break; | |
2716 | ||
92582b75 TT |
2717 | set_arg_token (arg, token, location, |
2718 | arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED, | |
2719 | CPP_OPTION (pfile, track_macro_expansion)); | |
2720 | arg->expanded_count++; | |
4ed5bcfb NB |
2721 | } |
2722 | ||
1e013d2e | 2723 | _cpp_pop_context (pfile); |
56941bf2 NB |
2724 | |
2725 | CPP_WTRADITIONAL (pfile) = saved_warn_trad; | |
a6e0d593 | 2726 | pfile->state.ignore__Pragma = saved_ignore__Pragma; |
93c80368 NB |
2727 | } |
2728 | ||
3600218c DS |
2729 | /* Returns the macro associated to the current context if we are in |
2730 | the context a macro expansion, NULL otherwise. */ | |
2731 | static cpp_hashnode* | |
2732 | macro_of_context (cpp_context *context) | |
2733 | { | |
2734 | if (context == NULL) | |
2735 | return NULL; | |
2736 | ||
2737 | return (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2738 | ? context->c.mc->macro_node | |
2739 | : context->c.macro; | |
2740 | } | |
2741 | ||
828a7f76 DS |
2742 | /* Return TRUE iff we are expanding a macro or are about to start |
2743 | expanding one. If we are effectively expanding a macro, the | |
2744 | function macro_of_context returns a pointer to the macro being | |
2745 | expanded. */ | |
2746 | static bool | |
2747 | in_macro_expansion_p (cpp_reader *pfile) | |
2748 | { | |
2749 | if (pfile == NULL) | |
2750 | return false; | |
2751 | ||
2752 | return (pfile->about_to_expand_macro_p | |
2753 | || macro_of_context (pfile->context)); | |
2754 | } | |
2755 | ||
d15a58c0 | 2756 | /* Pop the current context off the stack, re-enabling the macro if the |
92582b75 TT |
2757 | context represented a macro's replacement list. Initially the |
2758 | context structure was not freed so that we can re-use it later, but | |
2759 | now we do free it to reduce peak memory consumption. */ | |
93c80368 | 2760 | void |
6cf87ca4 | 2761 | _cpp_pop_context (cpp_reader *pfile) |
93c80368 | 2762 | { |
1e013d2e NB |
2763 | cpp_context *context = pfile->context; |
2764 | ||
3ad64f53 | 2765 | /* We should not be popping the base context. */ |
dbcc6b15 | 2766 | gcc_assert (context != &pfile->base_context); |
3ad64f53 | 2767 | |
92582b75 TT |
2768 | if (context->c.macro) |
2769 | { | |
2770 | cpp_hashnode *macro; | |
2771 | if (context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2772 | { | |
2773 | macro_context *mc = context->c.mc; | |
2774 | macro = mc->macro_node; | |
2775 | /* If context->buff is set, it means the life time of tokens | |
2776 | is bound to the life time of this context; so we must | |
2777 | free the tokens; that means we must free the virtual | |
2778 | locations of these tokens too. */ | |
2779 | if (context->buff && mc->virt_locs) | |
2780 | { | |
2781 | free (mc->virt_locs); | |
2782 | mc->virt_locs = NULL; | |
2783 | } | |
2784 | free (mc); | |
2785 | context->c.mc = NULL; | |
2786 | } | |
2787 | else | |
2788 | macro = context->c.macro; | |
2789 | ||
2790 | /* Beware that MACRO can be NULL in cases like when we are | |
2791 | called from expand_arg. In those cases, a dummy context with | |
2792 | tokens is pushed just for the purpose of walking them using | |
2793 | cpp_get_token_1. In that case, no 'macro' field is set into | |
2794 | the dummy context. */ | |
3600218c DS |
2795 | if (macro != NULL |
2796 | /* Several contiguous macro expansion contexts can be | |
2797 | associated to the same macro; that means it's the same | |
7d9641cc | 2798 | macro expansion that spans across all these (sub) |
3600218c DS |
2799 | contexts. So we should re-enable an expansion-disabled |
2800 | macro only when we are sure we are really out of that | |
2801 | macro expansion. */ | |
2802 | && macro_of_context (context->prev) != macro) | |
92582b75 | 2803 | macro->flags &= ~NODE_DISABLED; |
f8abc9ba DS |
2804 | |
2805 | if (macro == pfile->top_most_macro_node && context->prev == NULL) | |
2806 | /* We are popping the context of the top-most macro node. */ | |
2807 | pfile->top_most_macro_node = NULL; | |
92582b75 | 2808 | } |
1e013d2e NB |
2809 | |
2810 | if (context->buff) | |
92582b75 TT |
2811 | { |
2812 | /* Decrease memory peak consumption by freeing the memory used | |
2813 | by the context. */ | |
2814 | _cpp_free_buff (context->buff); | |
2815 | } | |
93c80368 | 2816 | |
1e013d2e | 2817 | pfile->context = context->prev; |
92582b75 TT |
2818 | /* decrease peak memory consumption by feeing the context. */ |
2819 | pfile->context->next = NULL; | |
2820 | free (context); | |
93c80368 NB |
2821 | } |
2822 | ||
92582b75 TT |
2823 | /* Return TRUE if we reached the end of the set of tokens stored in |
2824 | CONTEXT, FALSE otherwise. */ | |
2825 | static inline bool | |
2826 | reached_end_of_context (cpp_context *context) | |
2827 | { | |
2828 | if (context->tokens_kind == TOKENS_KIND_DIRECT) | |
2829 | return FIRST (context).token == LAST (context).token; | |
2830 | else if (context->tokens_kind == TOKENS_KIND_INDIRECT | |
2831 | || context->tokens_kind == TOKENS_KIND_EXTENDED) | |
2832 | return FIRST (context).ptoken == LAST (context).ptoken; | |
2833 | else | |
2834 | abort (); | |
2835 | } | |
2836 | ||
2837 | /* Consume the next token contained in the current context of PFILE, | |
2838 | and return it in *TOKEN. It's "full location" is returned in | |
2839 | *LOCATION. If -ftrack-macro-location is in effeect, fFull location" | |
7d9641cc | 2840 | means the location encoding the locus of the token across macro |
92582b75 TT |
2841 | expansion; otherwise it's just is the "normal" location of the |
2842 | token which (*TOKEN)->src_loc. */ | |
2843 | static inline void | |
2844 | consume_next_token_from_context (cpp_reader *pfile, | |
2845 | const cpp_token ** token, | |
620e594b | 2846 | location_t *location) |
92582b75 TT |
2847 | { |
2848 | cpp_context *c = pfile->context; | |
2849 | ||
2850 | if ((c)->tokens_kind == TOKENS_KIND_DIRECT) | |
2851 | { | |
2852 | *token = FIRST (c).token; | |
2853 | *location = (*token)->src_loc; | |
2854 | FIRST (c).token++; | |
2855 | } | |
2856 | else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT) | |
2857 | { | |
2858 | *token = *FIRST (c).ptoken; | |
2859 | *location = (*token)->src_loc; | |
2860 | FIRST (c).ptoken++; | |
2861 | } | |
2862 | else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED) | |
2863 | { | |
2864 | macro_context *m = c->c.mc; | |
2865 | *token = *FIRST (c).ptoken; | |
2866 | if (m->virt_locs) | |
2867 | { | |
2868 | *location = *m->cur_virt_loc; | |
2869 | m->cur_virt_loc++; | |
2870 | } | |
2871 | else | |
2872 | *location = (*token)->src_loc; | |
2873 | FIRST (c).ptoken++; | |
2874 | } | |
2875 | else | |
2876 | abort (); | |
2877 | } | |
2878 | ||
2879 | /* In the traditional mode of the preprocessor, if we are currently in | |
2880 | a directive, the location of a token must be the location of the | |
2881 | start of the directive line. This function returns the proper | |
2882 | location if we are in the traditional mode, and just returns | |
2883 | LOCATION otherwise. */ | |
2884 | ||
620e594b DM |
2885 | static inline location_t |
2886 | maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, location_t location) | |
92582b75 TT |
2887 | { |
2888 | if (CPP_OPTION (pfile, traditional)) | |
2889 | { | |
2890 | if (pfile->state.in_directive) | |
2891 | return pfile->directive_line; | |
2892 | } | |
2893 | return location; | |
2894 | } | |
2895 | ||
2896 | /* Routine to get a token as well as its location. | |
7f2f1a66 NB |
2897 | |
2898 | Macro expansions and directives are transparently handled, | |
2899 | including entering included files. Thus tokens are post-macro | |
2900 | expansion, and after any intervening directives. External callers | |
2901 | see CPP_EOF only at EOF. Internal callers also see it when meeting | |
2902 | a directive inside a macro call, when at the end of a directive and | |
2903 | state.in_directive is still 1, and at the end of argument | |
92582b75 TT |
2904 | pre-expansion. |
2905 | ||
2906 | LOC is an out parameter; *LOC is set to the location "as expected | |
2907 | by the user". Please read the comment of | |
2908 | cpp_get_token_with_location to learn more about the meaning of this | |
2909 | location. */ | |
2910 | static const cpp_token* | |
620e594b | 2911 | cpp_get_token_1 (cpp_reader *pfile, location_t *location) |
93c80368 | 2912 | { |
4ed5bcfb | 2913 | const cpp_token *result; |
92582b75 TT |
2914 | /* This token is a virtual token that either encodes a location |
2915 | related to macro expansion or a spelling location. */ | |
620e594b | 2916 | location_t virt_loc = 0; |
828a7f76 DS |
2917 | /* pfile->about_to_expand_macro_p can be overriden by indirect calls |
2918 | to functions that push macro contexts. So let's save it so that | |
2919 | we can restore it when we are about to leave this routine. */ | |
2920 | bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p; | |
4ed5bcfb | 2921 | |
29b10746 | 2922 | for (;;) |
93c80368 | 2923 | { |
4ed5bcfb | 2924 | cpp_hashnode *node; |
93c80368 NB |
2925 | cpp_context *context = pfile->context; |
2926 | ||
93c80368 | 2927 | /* Context->prev == 0 <=> base context. */ |
bdcbe496 | 2928 | if (!context->prev) |
29b10746 | 2929 | { |
92582b75 TT |
2930 | result = _cpp_lex_token (pfile); |
2931 | virt_loc = result->src_loc; | |
2932 | } | |
2933 | else if (!reached_end_of_context (context)) | |
2934 | { | |
2935 | consume_next_token_from_context (pfile, &result, | |
2936 | &virt_loc); | |
4ed5bcfb | 2937 | if (result->flags & PASTE_LEFT) |
ec1a23e6 | 2938 | { |
4ed5bcfb NB |
2939 | paste_all_tokens (pfile, result); |
2940 | if (pfile->state.in_directive) | |
2941 | continue; | |
92582b75 TT |
2942 | result = padding_token (pfile, result); |
2943 | goto out; | |
ec1a23e6 | 2944 | } |
29b10746 | 2945 | } |
93c80368 NB |
2946 | else |
2947 | { | |
64a1a422 TT |
2948 | if (pfile->context->c.macro) |
2949 | ++num_expanded_macros_counter; | |
bdcbe496 | 2950 | _cpp_pop_context (pfile); |
4ed5bcfb NB |
2951 | if (pfile->state.in_directive) |
2952 | continue; | |
92582b75 TT |
2953 | result = &pfile->avoid_paste; |
2954 | goto out; | |
93c80368 | 2955 | } |
93c80368 | 2956 | |
477cdac7 JT |
2957 | if (pfile->state.in_directive && result->type == CPP_COMMENT) |
2958 | continue; | |
2959 | ||
4ed5bcfb | 2960 | if (result->type != CPP_NAME) |
93c80368 NB |
2961 | break; |
2962 | ||
9a0c6187 | 2963 | node = result->val.node.node; |
4ed5bcfb | 2964 | |
a570d97f | 2965 | if (node->type == NT_VOID || (result->flags & NO_EXPAND)) |
644eddaa | 2966 | break; |
df383483 | 2967 | |
13f93cf5 NS |
2968 | if (!(node->flags & NODE_USED) |
2969 | && node->type == NT_USER_MACRO | |
2970 | && !node->value.macro | |
2971 | && !cpp_get_deferred_macro (pfile, node, result->src_loc)) | |
2972 | break; | |
2973 | ||
644eddaa | 2974 | if (!(node->flags & NODE_DISABLED)) |
93c80368 | 2975 | { |
5950c3c9 | 2976 | int ret = 0; |
5ffeb913 | 2977 | /* If not in a macro context, and we're going to start an |
f8abc9ba DS |
2978 | expansion, record the location and the top level macro |
2979 | about to be expanded. */ | |
828a7f76 | 2980 | if (!in_macro_expansion_p (pfile)) |
f8abc9ba DS |
2981 | { |
2982 | pfile->invocation_location = result->src_loc; | |
2983 | pfile->top_most_macro_node = node; | |
2984 | } | |
765d600a JJ |
2985 | if (pfile->state.prevent_expansion) |
2986 | break; | |
5950c3c9 BE |
2987 | |
2988 | /* Conditional macros require that a predicate be evaluated | |
2989 | first. */ | |
a37a7b8a JJ |
2990 | if ((node->flags & NODE_CONDITIONAL) != 0) |
2991 | { | |
2992 | if (pfile->cb.macro_to_expand) | |
2993 | { | |
2994 | bool whitespace_after; | |
2995 | const cpp_token *peek_tok = cpp_peek_token (pfile, 0); | |
2996 | ||
2997 | whitespace_after = (peek_tok->type == CPP_PADDING | |
2998 | || (peek_tok->flags & PREV_WHITE)); | |
2999 | node = pfile->cb.macro_to_expand (pfile, result); | |
3000 | if (node) | |
a641d6d3 | 3001 | ret = enter_macro_context (pfile, node, result, virt_loc); |
a37a7b8a JJ |
3002 | else if (whitespace_after) |
3003 | { | |
3004 | /* If macro_to_expand hook returned NULL and it | |
3005 | ate some tokens, see if we don't need to add | |
3006 | a padding token in between this and the | |
3007 | next token. */ | |
3008 | peek_tok = cpp_peek_token (pfile, 0); | |
3009 | if (peek_tok->type != CPP_PADDING | |
3010 | && (peek_tok->flags & PREV_WHITE) == 0) | |
3011 | _cpp_push_token_context (pfile, NULL, | |
3012 | padding_token (pfile, | |
3013 | peek_tok), 1); | |
3014 | } | |
3015 | } | |
3016 | } | |
3017 | else | |
a641d6d3 | 3018 | ret = enter_macro_context (pfile, node, result, virt_loc); |
a37a7b8a | 3019 | if (ret) |
5950c3c9 | 3020 | { |
765d600a | 3021 | if (pfile->state.in_directive || ret == 2) |
4ed5bcfb | 3022 | continue; |
92582b75 TT |
3023 | result = padding_token (pfile, result); |
3024 | goto out; | |
bd969772 | 3025 | } |
93c80368 | 3026 | } |
644eddaa NB |
3027 | else |
3028 | { | |
d15a58c0 NB |
3029 | /* Flag this token as always unexpandable. FIXME: move this |
3030 | to collect_args()?. */ | |
644eddaa NB |
3031 | cpp_token *t = _cpp_temp_token (pfile); |
3032 | t->type = result->type; | |
3033 | t->flags = result->flags | NO_EXPAND; | |
73096711 | 3034 | t->val = result->val; |
644eddaa NB |
3035 | result = t; |
3036 | } | |
a5c3cccd | 3037 | |
644eddaa | 3038 | break; |
93c80368 | 3039 | } |
4ed5bcfb | 3040 | |
92582b75 TT |
3041 | out: |
3042 | if (location != NULL) | |
3043 | { | |
3044 | if (virt_loc == 0) | |
3045 | virt_loc = result->src_loc; | |
3046 | *location = virt_loc; | |
3047 | ||
3048 | if (!CPP_OPTION (pfile, track_macro_expansion) | |
828a7f76 | 3049 | && macro_of_context (pfile->context) != NULL) |
92582b75 TT |
3050 | /* We are in a macro expansion context, are not tracking |
3051 | virtual location, but were asked to report the location | |
3052 | of the expansion point of the macro being expanded. */ | |
3053 | *location = pfile->invocation_location; | |
3054 | ||
3055 | *location = maybe_adjust_loc_for_trad_cpp (pfile, *location); | |
3056 | } | |
828a7f76 DS |
3057 | |
3058 | pfile->about_to_expand_macro_p = saved_about_to_expand_macro; | |
c9c3d5f2 NS |
3059 | |
3060 | if (pfile->state.directive_file_token | |
3061 | && !pfile->state.parsing_args | |
3062 | && !(result->type == CPP_PADDING || result->type == CPP_COMMENT) | |
3063 | && !(15 & --pfile->state.directive_file_token)) | |
3064 | { | |
3065 | /* Do header-name frobbery. Concatenate < ... > as approprate. | |
3066 | Do header search if needed, and finally drop the outer <> or | |
3067 | "". */ | |
3068 | pfile->state.angled_headers = false; | |
3069 | ||
3070 | /* Do angle-header reconstitution. Then do include searching. | |
3071 | We'll always end up with a ""-quoted header-name in that | |
3072 | case. If searching finds nothing, we emit a diagnostic and | |
3073 | an empty string. */ | |
3074 | size_t len = 0; | |
3075 | char *fname = NULL; | |
3076 | ||
3077 | cpp_token *tmp = _cpp_temp_token (pfile); | |
3078 | *tmp = *result; | |
3079 | ||
3080 | tmp->type = CPP_HEADER_NAME; | |
3081 | bool need_search = !pfile->state.directive_file_token; | |
3082 | pfile->state.directive_file_token = 0; | |
3083 | ||
3084 | bool angle = result->type != CPP_STRING; | |
3085 | if (result->type == CPP_HEADER_NAME | |
3086 | || (result->type == CPP_STRING && result->val.str.text[0] != 'R')) | |
3087 | { | |
3088 | len = result->val.str.len - 2; | |
3089 | fname = XNEWVEC (char, len + 1); | |
3090 | memcpy (fname, result->val.str.text + 1, len); | |
3091 | fname[len] = 0; | |
3092 | } | |
3093 | else if (result->type == CPP_LESS) | |
3094 | fname = _cpp_bracket_include (pfile); | |
3095 | ||
3096 | if (fname) | |
3097 | { | |
3098 | /* We have a header-name. Look it up. This will emit an | |
3099 | unfound diagnostic. Canonicalize the found name. */ | |
3100 | const char *found = fname; | |
3101 | ||
3102 | if (need_search) | |
3103 | { | |
1f9db692 | 3104 | found = _cpp_find_header_unit (pfile, fname, angle, tmp->src_loc); |
c9c3d5f2 NS |
3105 | if (!found) |
3106 | found = ""; | |
3107 | len = strlen (found); | |
3108 | } | |
3109 | /* Force a leading './' if it's not absolute. */ | |
3110 | bool dotme = (found[0] == '.' ? !IS_DIR_SEPARATOR (found[1]) | |
3111 | : found[0] && !IS_ABSOLUTE_PATH (found)); | |
3112 | ||
3113 | if (BUFF_ROOM (pfile->u_buff) < len + 1 + dotme * 2) | |
3114 | _cpp_extend_buff (pfile, &pfile->u_buff, len + 1 + dotme * 2); | |
3115 | unsigned char *buf = BUFF_FRONT (pfile->u_buff); | |
3116 | size_t pos = 0; | |
3117 | ||
3118 | if (dotme) | |
3119 | { | |
3120 | buf[pos++] = '.'; | |
3121 | /* Apparently '/' is unconditional. */ | |
3122 | buf[pos++] = '/'; | |
3123 | } | |
3124 | memcpy (&buf[pos], found, len); | |
3125 | pos += len; | |
3126 | buf[pos] = 0; | |
3127 | ||
3128 | tmp->val.str.len = pos; | |
3129 | tmp->val.str.text = buf; | |
3130 | ||
3131 | tmp->type = CPP_HEADER_NAME; | |
3132 | XDELETEVEC (fname); | |
3133 | ||
3134 | result = tmp; | |
3135 | } | |
3136 | } | |
3137 | ||
4ed5bcfb | 3138 | return result; |
93c80368 NB |
3139 | } |
3140 | ||
92582b75 TT |
3141 | /* External routine to get a token. Also used nearly everywhere |
3142 | internally, except for places where we know we can safely call | |
3143 | _cpp_lex_token directly, such as lexing a directive name. | |
3144 | ||
3145 | Macro expansions and directives are transparently handled, | |
3146 | including entering included files. Thus tokens are post-macro | |
3147 | expansion, and after any intervening directives. External callers | |
3148 | see CPP_EOF only at EOF. Internal callers also see it when meeting | |
3149 | a directive inside a macro call, when at the end of a directive and | |
3150 | state.in_directive is still 1, and at the end of argument | |
3151 | pre-expansion. */ | |
3152 | const cpp_token * | |
3153 | cpp_get_token (cpp_reader *pfile) | |
3154 | { | |
3155 | return cpp_get_token_1 (pfile, NULL); | |
3156 | } | |
3157 | ||
3158 | /* Like cpp_get_token, but also returns a virtual token location | |
3159 | separate from the spelling location carried by the returned token. | |
3160 | ||
3161 | LOC is an out parameter; *LOC is set to the location "as expected | |
3162 | by the user". This matters when a token results from macro | |
3163 | expansion; in that case the token's spelling location indicates the | |
3164 | locus of the token in the definition of the macro but *LOC | |
3165 | virtually encodes all the other meaningful locuses associated to | |
3166 | the token. | |
3167 | ||
3168 | What? virtual location? Yes, virtual location. | |
3169 | ||
3170 | If the token results from macro expansion and if macro expansion | |
3171 | location tracking is enabled its virtual location encodes (at the | |
3172 | same time): | |
3173 | ||
3174 | - the spelling location of the token | |
3175 | ||
3176 | - the locus of the macro expansion point | |
3177 | ||
3178 | - the locus of the point where the token got instantiated as part | |
3179 | of the macro expansion process. | |
3180 | ||
3181 | You have to use the linemap API to get the locus you are interested | |
3182 | in from a given virtual location. | |
3183 | ||
3184 | Note however that virtual locations are not necessarily ordered for | |
3185 | relations '<' and '>'. One must use the function | |
3186 | linemap_location_before_p instead of using the relational operator | |
3187 | '<'. | |
3188 | ||
3189 | If macro expansion tracking is off and if the token results from | |
3190 | macro expansion the virtual location is the expansion point of the | |
3191 | macro that got expanded. | |
3192 | ||
3193 | When the token doesn't result from macro expansion, the virtual | |
3194 | location is just the same thing as its spelling location. */ | |
3195 | ||
5ffeb913 | 3196 | const cpp_token * |
620e594b | 3197 | cpp_get_token_with_location (cpp_reader *pfile, location_t *loc) |
5ffeb913 | 3198 | { |
828a7f76 | 3199 | return cpp_get_token_1 (pfile, loc); |
5ffeb913 TT |
3200 | } |
3201 | ||
7065e130 NB |
3202 | /* Returns true if we're expanding an object-like macro that was |
3203 | defined in a system header. Just checks the macro at the top of | |
408d88af JJ |
3204 | the stack. Used for diagnostic suppression. |
3205 | Also return true for builtin macros. */ | |
7065e130 | 3206 | int |
6cf87ca4 | 3207 | cpp_sys_macro_p (cpp_reader *pfile) |
7065e130 | 3208 | { |
4e65a470 DS |
3209 | cpp_hashnode *node = NULL; |
3210 | ||
3211 | if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) | |
3212 | node = pfile->context->c.mc->macro_node; | |
3213 | else | |
3214 | node = pfile->context->c.macro; | |
7065e130 | 3215 | |
408d88af JJ |
3216 | if (!node) |
3217 | return false; | |
3218 | if (cpp_builtin_macro_p (node)) | |
3219 | return true; | |
3220 | return node->value.macro && node->value.macro->syshdr; | |
7065e130 NB |
3221 | } |
3222 | ||
af0d16cd NB |
3223 | /* Read each token in, until end of the current file. Directives are |
3224 | transparently processed. */ | |
93c80368 | 3225 | void |
6cf87ca4 | 3226 | cpp_scan_nooutput (cpp_reader *pfile) |
93c80368 | 3227 | { |
22234f56 PB |
3228 | /* Request a CPP_EOF token at the end of this file, rather than |
3229 | transparently continuing with the including file. */ | |
3230 | pfile->buffer->return_at_eof = true; | |
3231 | ||
c6e83800 ZW |
3232 | pfile->state.discarding_output++; |
3233 | pfile->state.prevent_expansion++; | |
3234 | ||
590e1987 NB |
3235 | if (CPP_OPTION (pfile, traditional)) |
3236 | while (_cpp_read_logical_line_trad (pfile)) | |
3237 | ; | |
3238 | else | |
3239 | while (cpp_get_token (pfile)->type != CPP_EOF) | |
3240 | ; | |
c6e83800 ZW |
3241 | |
3242 | pfile->state.discarding_output--; | |
3243 | pfile->state.prevent_expansion--; | |
93c80368 NB |
3244 | } |
3245 | ||
5950c3c9 BE |
3246 | /* Step back one or more tokens obtained from the lexer. */ |
3247 | void | |
3248 | _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count) | |
3249 | { | |
3250 | pfile->lookaheads += count; | |
3251 | while (count--) | |
3252 | { | |
3253 | pfile->cur_token--; | |
3254 | if (pfile->cur_token == pfile->cur_run->base | |
3255 | /* Possible with -fpreprocessed and no leading #line. */ | |
3256 | && pfile->cur_run->prev != NULL) | |
3257 | { | |
3258 | pfile->cur_run = pfile->cur_run->prev; | |
3259 | pfile->cur_token = pfile->cur_run->limit; | |
3260 | } | |
3261 | } | |
3262 | } | |
3263 | ||
1c90c6f9 | 3264 | /* Step back one (or more) tokens. Can only step back more than 1 if |
bdcbe496 | 3265 | they are from the lexer, and not from macro expansion. */ |
b528a07e | 3266 | void |
6cf87ca4 | 3267 | _cpp_backup_tokens (cpp_reader *pfile, unsigned int count) |
93c80368 | 3268 | { |
bdcbe496 | 3269 | if (pfile->context->prev == NULL) |
5950c3c9 | 3270 | _cpp_backup_tokens_direct (pfile, count); |
bdcbe496 | 3271 | else |
93c80368 | 3272 | { |
bdcbe496 NB |
3273 | if (count != 1) |
3274 | abort (); | |
92582b75 | 3275 | if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT) |
82eda77e | 3276 | FIRST (pfile->context).token--; |
92582b75 | 3277 | else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT) |
82eda77e | 3278 | FIRST (pfile->context).ptoken--; |
92582b75 TT |
3279 | else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) |
3280 | { | |
3281 | FIRST (pfile->context).ptoken--; | |
3282 | if (pfile->context->c.macro) | |
3283 | { | |
3284 | macro_context *m = pfile->context->c.mc; | |
3285 | m->cur_virt_loc--; | |
22d66382 | 3286 | gcc_checking_assert (m->cur_virt_loc >= m->virt_locs); |
92582b75 TT |
3287 | } |
3288 | else | |
3289 | abort (); | |
3290 | } | |
3291 | else | |
3292 | abort (); | |
93c80368 NB |
3293 | } |
3294 | } | |
711b8824 | 3295 | |
93c80368 NB |
3296 | /* #define directive parsing and handling. */ |
3297 | ||
c9fb347e | 3298 | /* Returns true if a macro redefinition warning is required. */ |
cbc69f84 | 3299 | static bool |
8e680db5 | 3300 | warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node, |
6cf87ca4 | 3301 | const cpp_macro *macro2) |
93c80368 | 3302 | { |
618cdda7 NB |
3303 | /* Some redefinitions need to be warned about regardless. */ |
3304 | if (node->flags & NODE_WARN) | |
cbc69f84 | 3305 | return true; |
93c80368 | 3306 | |
b753b37b MLI |
3307 | /* Suppress warnings for builtins that lack the NODE_WARN flag, |
3308 | unless Wbuiltin-macro-redefined. */ | |
800c0e98 | 3309 | if (cpp_builtin_macro_p (node)) |
b753b37b | 3310 | return CPP_OPTION (pfile, warn_builtin_macro_redefined); |
c047ce93 | 3311 | |
5950c3c9 BE |
3312 | /* Redefinitions of conditional (context-sensitive) macros, on |
3313 | the other hand, must be allowed silently. */ | |
3314 | if (node->flags & NODE_CONDITIONAL) | |
3315 | return false; | |
3316 | ||
13f93cf5 NS |
3317 | if (cpp_macro *macro1 = get_deferred_or_lazy_macro (pfile, node, macro2->line)) |
3318 | return cpp_compare_macros (macro1, macro2); | |
3319 | return false; | |
e9a2e208 NS |
3320 | } |
3321 | ||
3322 | /* Return TRUE if MACRO1 and MACRO2 differ. */ | |
3323 | ||
13f93cf5 NS |
3324 | bool |
3325 | cpp_compare_macros (const cpp_macro *macro1, const cpp_macro *macro2) | |
e9a2e208 | 3326 | { |
618cdda7 | 3327 | /* Redefinition of a macro is allowed if and only if the old and new |
ec5c56db | 3328 | definitions are the same. (6.10.3 paragraph 2). */ |
93c80368 | 3329 | |
6618c5d4 NB |
3330 | /* Don't check count here as it can be different in valid |
3331 | traditional redefinitions with just whitespace differences. */ | |
3332 | if (macro1->paramc != macro2->paramc | |
93c80368 | 3333 | || macro1->fun_like != macro2->fun_like |
28e0f040 | 3334 | || macro1->variadic != macro2->variadic) |
cbc69f84 | 3335 | return true; |
93c80368 NB |
3336 | |
3337 | /* Check parameter spellings. */ | |
c9fb347e | 3338 | for (unsigned i = macro1->paramc; i--; ) |
3fb558b1 | 3339 | if (macro1->parm.params[i] != macro2->parm.params[i]) |
cbc69f84 | 3340 | return true; |
93c80368 | 3341 | |
cbc69f84 | 3342 | /* Check the replacement text or tokens. */ |
c9fb347e | 3343 | if (macro1->kind == cmk_traditional) |
6618c5d4 | 3344 | return _cpp_expansions_different_trad (macro1, macro2); |
cbc69f84 | 3345 | |
a7f36da3 DD |
3346 | if (macro1->count != macro2->count) |
3347 | return true; | |
3348 | ||
c9fb347e | 3349 | for (unsigned i= macro1->count; i--; ) |
a7f36da3 DD |
3350 | if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i])) |
3351 | return true; | |
cbc69f84 NB |
3352 | |
3353 | return false; | |
93c80368 NB |
3354 | } |
3355 | ||
3356 | /* Free the definition of hashnode H. */ | |
711b8824 | 3357 | void |
6cf87ca4 | 3358 | _cpp_free_definition (cpp_hashnode *h) |
711b8824 | 3359 | { |
93c80368 NB |
3360 | /* Macros and assertions no longer have anything to free. */ |
3361 | h->type = NT_VOID; | |
a570d97f NS |
3362 | h->value.answers = NULL; |
3363 | h->flags &= ~(NODE_DISABLED | NODE_USED); | |
93c80368 NB |
3364 | } |
3365 | ||
be5ffc59 | 3366 | /* Save parameter NODE (spelling SPELLING) to the parameter list of |
729a01f7 | 3367 | macro MACRO. Returns true on success, false on failure. */ |
c70f6ed3 | 3368 | bool |
729a01f7 | 3369 | _cpp_save_parameter (cpp_reader *pfile, unsigned n, cpp_hashnode *node, |
be5ffc59 | 3370 | cpp_hashnode *spelling) |
93c80368 | 3371 | { |
93c80368 | 3372 | /* Constraint 6.10.3.6 - duplicate parameter names. */ |
a570d97f | 3373 | if (node->type == NT_MACRO_ARG) |
709e9e50 | 3374 | { |
0527bc4e | 3375 | cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"", |
ebef4e8c | 3376 | NODE_NAME (node)); |
729a01f7 | 3377 | return false; |
93c80368 | 3378 | } |
709e9e50 | 3379 | |
729a01f7 | 3380 | unsigned len = (n + 1) * sizeof (struct macro_arg_saved_data); |
4977bab6 ZW |
3381 | if (len > pfile->macro_buffer_len) |
3382 | { | |
729a01f7 NS |
3383 | pfile->macro_buffer |
3384 | = XRESIZEVEC (unsigned char, pfile->macro_buffer, len); | |
4977bab6 ZW |
3385 | pfile->macro_buffer_len = len; |
3386 | } | |
4977bab6 | 3387 | |
729a01f7 NS |
3388 | macro_arg_saved_data *saved = (macro_arg_saved_data *)pfile->macro_buffer; |
3389 | saved[n].canonical_node = node; | |
3390 | saved[n].value = node->value; | |
a570d97f | 3391 | saved[n].type = node->type; |
729a01f7 | 3392 | |
10f04917 NS |
3393 | void *base = _cpp_reserve_room (pfile, n * sizeof (cpp_hashnode *), |
3394 | sizeof (cpp_hashnode *)); | |
3395 | ((cpp_hashnode **)base)[n] = spelling; | |
729a01f7 NS |
3396 | |
3397 | /* Morph into a macro arg. */ | |
a570d97f | 3398 | node->type = NT_MACRO_ARG; |
729a01f7 NS |
3399 | /* Index is 1 based. */ |
3400 | node->value.arg_index = n + 1; | |
3401 | ||
3402 | return true; | |
3403 | } | |
3404 | ||
3405 | /* Restore the parameters to their previous state. */ | |
3406 | void | |
3407 | _cpp_unsave_parameters (cpp_reader *pfile, unsigned n) | |
3408 | { | |
3409 | /* Clear the fast argument lookup indices. */ | |
3410 | while (n--) | |
3411 | { | |
3412 | struct macro_arg_saved_data *save = | |
3413 | &((struct macro_arg_saved_data *) pfile->macro_buffer)[n]; | |
3414 | ||
3415 | struct cpp_hashnode *node = save->canonical_node; | |
a570d97f | 3416 | node->type = save->type; |
729a01f7 | 3417 | node->value = save->value; |
729a01f7 | 3418 | } |
711b8824 ZW |
3419 | } |
3420 | ||
729a01f7 NS |
3421 | /* Check the syntax of the parameters in a MACRO definition. Return |
3422 | false on failure. Set *N_PTR and *VARADIC_PTR as appropriate. | |
3423 | '(' ')' | |
3424 | '(' parm-list ',' last-parm ')' | |
3425 | '(' last-parm ')' | |
3426 | parm-list: name | |
3427 | | parm-list, name | |
3428 | last-parm: name | |
3429 | | name '...' | |
3430 | | '...' | |
3431 | */ | |
a570d97f | 3432 | |
23ff0223 | 3433 | static bool |
729a01f7 | 3434 | parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *varadic_ptr) |
711b8824 | 3435 | { |
729a01f7 NS |
3436 | unsigned nparms = 0; |
3437 | bool ok = false; | |
711b8824 | 3438 | |
729a01f7 | 3439 | for (bool prev_ident = false;;) |
711b8824 | 3440 | { |
345894b4 | 3441 | const cpp_token *token = _cpp_lex_token (pfile); |
711b8824 | 3442 | |
345894b4 | 3443 | switch (token->type) |
711b8824 | 3444 | { |
729a01f7 | 3445 | case CPP_COMMENT: |
477cdac7 JT |
3446 | /* Allow/ignore comments in parameter lists if we are |
3447 | preserving comments in macro expansions. */ | |
729a01f7 NS |
3448 | if (!CPP_OPTION (pfile, discard_comments_in_macro_exp)) |
3449 | break; | |
477cdac7 | 3450 | |
729a01f7 NS |
3451 | /* FALLTHRU */ |
3452 | default: | |
3453 | bad: | |
3454 | { | |
3455 | const char *const msgs[5] = | |
3456 | { | |
3457 | N_("expected parameter name, found \"%s\""), | |
3458 | N_("expected ',' or ')', found \"%s\""), | |
3459 | N_("expected parameter name before end of line"), | |
3460 | N_("expected ')' before end of line"), | |
3461 | N_("expected ')' after \"...\"") | |
3462 | }; | |
3463 | unsigned ix = prev_ident; | |
3464 | const unsigned char *as_text = NULL; | |
3465 | if (*varadic_ptr) | |
3466 | ix = 4; | |
3467 | else if (token->type == CPP_EOF) | |
3468 | ix += 2; | |
3469 | else | |
3470 | as_text = cpp_token_as_text (pfile, token); | |
3471 | cpp_error (pfile, CPP_DL_ERROR, msgs[ix], as_text); | |
3472 | } | |
3473 | goto out; | |
93c80368 | 3474 | |
711b8824 | 3475 | case CPP_NAME: |
729a01f7 NS |
3476 | if (prev_ident || *varadic_ptr) |
3477 | goto bad; | |
3478 | prev_ident = true; | |
3479 | ||
3480 | if (!_cpp_save_parameter (pfile, nparms, token->val.node.node, | |
3481 | token->val.node.spelling)) | |
3482 | goto out; | |
3483 | nparms++; | |
3484 | break; | |
711b8824 | 3485 | |
93c80368 | 3486 | case CPP_CLOSE_PAREN: |
729a01f7 NS |
3487 | if (prev_ident || !nparms || *varadic_ptr) |
3488 | { | |
3489 | ok = true; | |
3490 | goto out; | |
3491 | } | |
711b8824 | 3492 | |
191816a3 | 3493 | /* FALLTHRU */ |
93c80368 | 3494 | case CPP_COMMA: |
729a01f7 NS |
3495 | if (!prev_ident || *varadic_ptr) |
3496 | goto bad; | |
3497 | prev_ident = false; | |
3498 | break; | |
711b8824 | 3499 | |
93c80368 | 3500 | case CPP_ELLIPSIS: |
729a01f7 NS |
3501 | if (*varadic_ptr) |
3502 | goto bad; | |
3503 | *varadic_ptr = true; | |
93c80368 NB |
3504 | if (!prev_ident) |
3505 | { | |
729a01f7 NS |
3506 | /* An ISO bare ellipsis. */ |
3507 | _cpp_save_parameter (pfile, nparms, | |
be5ffc59 | 3508 | pfile->spec_nodes.n__VA_ARGS__, |
c70f6ed3 | 3509 | pfile->spec_nodes.n__VA_ARGS__); |
729a01f7 | 3510 | nparms++; |
93c80368 | 3511 | pfile->state.va_args_ok = 1; |
e5b79219 | 3512 | if (! CPP_OPTION (pfile, c99) |
e3339d0f | 3513 | && CPP_OPTION (pfile, cpp_pedantic) |
e5b79219 | 3514 | && CPP_OPTION (pfile, warn_variadic_macros)) |
729a01f7 NS |
3515 | cpp_pedwarning |
3516 | (pfile, CPP_W_VARIADIC_MACROS, | |
3517 | CPP_OPTION (pfile, cplusplus) | |
3518 | ? N_("anonymous variadic macros were introduced in C++11") | |
3519 | : N_("anonymous variadic macros were introduced in C99")); | |
177cce46 | 3520 | else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 |
f3bede71 MP |
3521 | && ! CPP_OPTION (pfile, cplusplus)) |
3522 | cpp_error (pfile, CPP_DL_WARNING, | |
3523 | "anonymous variadic macros were introduced in C99"); | |
93c80368 | 3524 | } |
e3339d0f | 3525 | else if (CPP_OPTION (pfile, cpp_pedantic) |
e5b79219 | 3526 | && CPP_OPTION (pfile, warn_variadic_macros)) |
729a01f7 NS |
3527 | cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, |
3528 | CPP_OPTION (pfile, cplusplus) | |
3529 | ? N_("ISO C++ does not permit named variadic macros") | |
3530 | : N_("ISO C does not permit named variadic macros")); | |
3531 | break; | |
711b8824 | 3532 | } |
711b8824 | 3533 | } |
729a01f7 NS |
3534 | |
3535 | out: | |
3536 | *n_ptr = nparms; | |
3537 | ||
3538 | return ok; | |
93c80368 NB |
3539 | } |
3540 | ||
d15a58c0 NB |
3541 | /* Lex a token from the expansion of MACRO, but mark parameters as we |
3542 | find them and warn of traditional stringification. */ | |
10f04917 | 3543 | static cpp_macro * |
6cf87ca4 | 3544 | lex_expansion_token (cpp_reader *pfile, cpp_macro *macro) |
14baae01 | 3545 | { |
10f04917 NS |
3546 | macro = (cpp_macro *)_cpp_reserve_room (pfile, |
3547 | sizeof (cpp_macro) - sizeof (cpp_token) | |
3548 | + macro->count * sizeof (cpp_token), | |
3549 | sizeof (cpp_token)); | |
3550 | cpp_token *saved_cur_token = pfile->cur_token; | |
3551 | pfile->cur_token = ¯o->exp.tokens[macro->count]; | |
3552 | cpp_token *token = _cpp_lex_direct (pfile); | |
ee380365 | 3553 | pfile->cur_token = saved_cur_token; |
93c80368 | 3554 | |
d15a58c0 | 3555 | /* Is this a parameter? */ |
a570d97f | 3556 | if (token->type == CPP_NAME && token->val.node.node->type == NT_MACRO_ARG) |
93c80368 | 3557 | { |
a570d97f | 3558 | /* Morph into a parameter reference. */ |
be5ffc59 | 3559 | cpp_hashnode *spelling = token->val.node.spelling; |
93c80368 | 3560 | token->type = CPP_MACRO_ARG; |
9a0c6187 | 3561 | token->val.macro_arg.arg_no = token->val.node.node->value.arg_index; |
be5ffc59 | 3562 | token->val.macro_arg.spelling = spelling; |
93c80368 NB |
3563 | } |
3564 | else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 | |
3565 | && (token->type == CPP_STRING || token->type == CPP_CHAR)) | |
3566 | check_trad_stringification (pfile, macro, &token->val.str); | |
3567 | ||
10f04917 | 3568 | return macro; |
711b8824 ZW |
3569 | } |
3570 | ||
10f04917 NS |
3571 | static cpp_macro * |
3572 | create_iso_definition (cpp_reader *pfile) | |
711b8824 | 3573 | { |
126e073b SM |
3574 | bool following_paste_op = false; |
3575 | const char *paste_op_error_msg = | |
3576 | N_("'##' cannot appear at either end of a macro expansion"); | |
aa508502 | 3577 | unsigned int num_extra_tokens = 0; |
729a01f7 | 3578 | unsigned nparms = 0; |
10f04917 | 3579 | cpp_hashnode **params = NULL; |
729a01f7 NS |
3580 | bool varadic = false; |
3581 | bool ok = false; | |
10f04917 | 3582 | cpp_macro *macro = NULL; |
93c80368 | 3583 | |
10f04917 NS |
3584 | /* Look at the first token, to see if this is a function-like |
3585 | macro. */ | |
3586 | cpp_token first; | |
3587 | cpp_token *saved_cur_token = pfile->cur_token; | |
3588 | pfile->cur_token = &first; | |
3589 | cpp_token *token = _cpp_lex_direct (pfile); | |
3590 | pfile->cur_token = saved_cur_token; | |
14baae01 | 3591 | |
10f04917 | 3592 | if (token->flags & PREV_WHITE) |
729a01f7 | 3593 | /* Preceeded by space, must be part of expansion. */; |
10f04917 | 3594 | else if (token->type == CPP_OPEN_PAREN) |
93c80368 | 3595 | { |
729a01f7 NS |
3596 | /* An open-paren, get a parameter list. */ |
3597 | if (!parse_params (pfile, &nparms, &varadic)) | |
3598 | goto out; | |
8c3b2693 | 3599 | |
10f04917 NS |
3600 | params = (cpp_hashnode **)_cpp_commit_buff |
3601 | (pfile, sizeof (cpp_hashnode *) * nparms); | |
3602 | token = NULL; | |
93c80368 | 3603 | } |
10f04917 NS |
3604 | else if (token->type != CPP_EOF |
3605 | && !(token->type == CPP_COMMENT | |
3606 | && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))) | |
cae064e7 JJ |
3607 | { |
3608 | /* While ISO C99 requires whitespace before replacement text | |
3976796b ESR |
3609 | in a macro definition, ISO C90 with TC1 allows characters |
3610 | from the basic source character set there. */ | |
cae064e7 | 3611 | if (CPP_OPTION (pfile, c99)) |
729a01f7 NS |
3612 | cpp_error (pfile, CPP_DL_PEDWARN, |
3613 | CPP_OPTION (pfile, cplusplus) | |
3614 | ? N_("ISO C++11 requires whitespace after the macro name") | |
3615 | : N_("ISO C99 requires whitespace after the macro name")); | |
cae064e7 JJ |
3616 | else |
3617 | { | |
c24300ba | 3618 | enum cpp_diagnostic_level warntype = CPP_DL_WARNING; |
10f04917 | 3619 | switch (token->type) |
cae064e7 JJ |
3620 | { |
3621 | case CPP_ATSIGN: | |
3622 | case CPP_AT_NAME: | |
3623 | case CPP_OBJC_STRING: | |
3624 | /* '@' is not in basic character set. */ | |
3625 | warntype = CPP_DL_PEDWARN; | |
3626 | break; | |
3627 | case CPP_OTHER: | |
3628 | /* Basic character set sans letters, digits and _. */ | |
3629 | if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~", | |
10f04917 | 3630 | token->val.str.text[0]) == NULL) |
cae064e7 JJ |
3631 | warntype = CPP_DL_PEDWARN; |
3632 | break; | |
3633 | default: | |
3634 | /* All other tokens start with a character from basic | |
3635 | character set. */ | |
3636 | break; | |
3637 | } | |
3638 | cpp_error (pfile, warntype, | |
3639 | "missing whitespace after the macro name"); | |
3640 | } | |
3641 | } | |
711b8824 | 3642 | |
10f04917 NS |
3643 | macro = _cpp_new_macro (pfile, cmk_macro, |
3644 | _cpp_reserve_room (pfile, 0, sizeof (cpp_macro))); | |
3645 | ||
3646 | if (!token) | |
3647 | { | |
3648 | macro->variadic = varadic; | |
3649 | macro->paramc = nparms; | |
3fb558b1 | 3650 | macro->parm.params = params; |
10f04917 NS |
3651 | macro->fun_like = true; |
3652 | } | |
14baae01 NB |
3653 | else |
3654 | { | |
10f04917 NS |
3655 | /* Preserve the token we peeked, there is already a single slot for it. */ |
3656 | macro->exp.tokens[0] = *token; | |
3657 | token = ¯o->exp.tokens[0]; | |
3658 | macro->count = 1; | |
14baae01 | 3659 | } |
711b8824 | 3660 | |
e235031d | 3661 | for (vaopt_state vaopt_tracker (pfile, macro->variadic, NULL);; token = NULL) |
93c80368 | 3662 | { |
10f04917 NS |
3663 | if (!token) |
3664 | { | |
3665 | macro = lex_expansion_token (pfile, macro); | |
3666 | token = ¯o->exp.tokens[macro->count++]; | |
3667 | } | |
3668 | ||
93c80368 NB |
3669 | /* Check the stringifying # constraint 6.10.3.2.1 of |
3670 | function-like macros when lexing the subsequent token. */ | |
3671 | if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) | |
3672 | { | |
d5659997 JJ |
3673 | if (token->type == CPP_MACRO_ARG |
3674 | || (macro->variadic | |
3675 | && token->type == CPP_NAME | |
3676 | && token->val.node.node == pfile->spec_nodes.n__VA_OPT__)) | |
93c80368 | 3677 | { |
aa508502 JM |
3678 | if (token->flags & PREV_WHITE) |
3679 | token->flags |= SP_PREV_WHITE; | |
3680 | if (token[-1].flags & DIGRAPH) | |
3681 | token->flags |= SP_DIGRAPH; | |
93c80368 NB |
3682 | token->flags &= ~PREV_WHITE; |
3683 | token->flags |= STRINGIFY_ARG; | |
3684 | token->flags |= token[-1].flags & PREV_WHITE; | |
3685 | token[-1] = token[0]; | |
3686 | macro->count--; | |
3687 | } | |
3688 | /* Let assembler get away with murder. */ | |
bdb05a7b | 3689 | else if (CPP_OPTION (pfile, lang) != CLK_ASM) |
93c80368 | 3690 | { |
0527bc4e | 3691 | cpp_error (pfile, CPP_DL_ERROR, |
ebef4e8c | 3692 | "'#' is not followed by a macro parameter"); |
729a01f7 | 3693 | goto out; |
93c80368 NB |
3694 | } |
3695 | } | |
3696 | ||
3697 | if (token->type == CPP_EOF) | |
126e073b SM |
3698 | { |
3699 | /* Paste operator constraint 6.10.3.3.1: | |
3700 | Token-paste ##, can appear in both object-like and | |
3701 | function-like macros, but not at the end. */ | |
3702 | if (following_paste_op) | |
3703 | { | |
3704 | cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); | |
729a01f7 | 3705 | goto out; |
126e073b | 3706 | } |
729a01f7 NS |
3707 | if (!vaopt_tracker.completed ()) |
3708 | goto out; | |
126e073b SM |
3709 | break; |
3710 | } | |
93c80368 NB |
3711 | |
3712 | /* Paste operator constraint 6.10.3.3.1. */ | |
3713 | if (token->type == CPP_PASTE) | |
3714 | { | |
3715 | /* Token-paste ##, can appear in both object-like and | |
126e073b SM |
3716 | function-like macros, but not at the beginning. */ |
3717 | if (macro->count == 1) | |
93c80368 | 3718 | { |
126e073b | 3719 | cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); |
729a01f7 | 3720 | goto out; |
93c80368 | 3721 | } |
711b8824 | 3722 | |
10f04917 | 3723 | if (following_paste_op) |
aa508502 | 3724 | { |
10f04917 NS |
3725 | /* Consecutive paste operators. This one will be moved |
3726 | to the end. */ | |
aa508502 | 3727 | num_extra_tokens++; |
9a0c6187 | 3728 | token->val.token_no = macro->count - 1; |
aa508502 JM |
3729 | } |
3730 | else | |
3731 | { | |
10f04917 | 3732 | /* Drop the paste operator. */ |
aa508502 JM |
3733 | --macro->count; |
3734 | token[-1].flags |= PASTE_LEFT; | |
3735 | if (token->flags & DIGRAPH) | |
3736 | token[-1].flags |= SP_DIGRAPH; | |
3737 | if (token->flags & PREV_WHITE) | |
3738 | token[-1].flags |= SP_PREV_WHITE; | |
3739 | } | |
10f04917 | 3740 | following_paste_op = true; |
93c80368 | 3741 | } |
10f04917 NS |
3742 | else |
3743 | following_paste_op = false; | |
93c80368 | 3744 | |
fb771b9d | 3745 | if (vaopt_tracker.update (token) == vaopt_state::ERROR) |
729a01f7 | 3746 | goto out; |
93c80368 NB |
3747 | } |
3748 | ||
729a01f7 NS |
3749 | /* We're committed to winning now. */ |
3750 | ok = true; | |
fb771b9d | 3751 | |
4c2b647d NB |
3752 | /* Don't count the CPP_EOF. */ |
3753 | macro->count--; | |
93c80368 | 3754 | |
10f04917 NS |
3755 | macro = (cpp_macro *)_cpp_commit_buff |
3756 | (pfile, sizeof (cpp_macro) - sizeof (cpp_token) | |
3757 | + sizeof (cpp_token) * macro->count); | |
3758 | ||
c9fb347e | 3759 | /* Clear whitespace on first token. */ |
8c3b2693 | 3760 | if (macro->count) |
601328bb | 3761 | macro->exp.tokens[0].flags &= ~PREV_WHITE; |
8c3b2693 | 3762 | |
10f04917 | 3763 | if (num_extra_tokens) |
d8044160 | 3764 | { |
10f04917 NS |
3765 | /* Place second and subsequent ## or %:%: tokens in sequences of |
3766 | consecutive such tokens at the end of the list to preserve | |
3767 | information about where they appear, how they are spelt and | |
3768 | whether they are preceded by whitespace without otherwise | |
3769 | interfering with macro expansion. Remember, this is | |
3770 | extremely rare, so efficiency is not a priority. */ | |
3771 | cpp_token *temp = (cpp_token *)_cpp_reserve_room | |
3772 | (pfile, 0, num_extra_tokens * sizeof (cpp_token)); | |
3773 | unsigned extra_ix = 0, norm_ix = 0; | |
3774 | cpp_token *exp = macro->exp.tokens; | |
3775 | for (unsigned ix = 0; ix != macro->count; ix++) | |
3776 | if (exp[ix].type == CPP_PASTE) | |
3777 | temp[extra_ix++] = exp[ix]; | |
3778 | else | |
3779 | exp[norm_ix++] = exp[ix]; | |
3780 | memcpy (&exp[norm_ix], temp, num_extra_tokens * sizeof (cpp_token)); | |
3781 | ||
3782 | /* Record there are extra tokens. */ | |
3783 | macro->extra_tokens = 1; | |
d8044160 | 3784 | } |
8c3b2693 | 3785 | |
729a01f7 NS |
3786 | out: |
3787 | pfile->state.va_args_ok = 0; | |
3788 | _cpp_unsave_parameters (pfile, nparms); | |
3789 | ||
10f04917 | 3790 | return ok ? macro : NULL; |
cbc69f84 NB |
3791 | } |
3792 | ||
10f04917 NS |
3793 | cpp_macro * |
3794 | _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement) | |
cbc69f84 | 3795 | { |
10f04917 | 3796 | cpp_macro *macro = (cpp_macro *) placement; |
44ed91a1 | 3797 | |
e9a2e208 NS |
3798 | /* Zero init all the fields. This'll tell the compiler know all the |
3799 | following inits are writing a virgin object. */ | |
3800 | memset (macro, 0, offsetof (cpp_macro, exp)); | |
3801 | ||
cbc69f84 | 3802 | macro->line = pfile->directive_line; |
3fb558b1 | 3803 | macro->parm.params = 0; |
800c0e98 | 3804 | macro->lazy = 0; |
cbc69f84 NB |
3805 | macro->paramc = 0; |
3806 | macro->variadic = 0; | |
23345bbb | 3807 | macro->used = !CPP_OPTION (pfile, warn_unused_macros); |
cbc69f84 NB |
3808 | macro->count = 0; |
3809 | macro->fun_like = 0; | |
62c5ea52 | 3810 | macro->imported_p = false; |
aa508502 | 3811 | macro->extra_tokens = 0; |
7065e130 | 3812 | /* To suppress some diagnostics. */ |
12f9df4e | 3813 | macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0; |
7065e130 | 3814 | |
10f04917 NS |
3815 | macro->kind = kind; |
3816 | ||
3817 | return macro; | |
3818 | } | |
3819 | ||
3820 | /* Parse a macro and save its expansion. Returns nonzero on success. */ | |
3821 | bool | |
3822 | _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node) | |
3823 | { | |
3824 | cpp_macro *macro; | |
3825 | ||
cbc69f84 | 3826 | if (CPP_OPTION (pfile, traditional)) |
10f04917 | 3827 | macro = _cpp_create_trad_definition (pfile); |
cbc69f84 | 3828 | else |
10f04917 | 3829 | macro = create_iso_definition (pfile); |
cbc69f84 | 3830 | |
10f04917 NS |
3831 | if (!macro) |
3832 | return false; | |
cbc69f84 | 3833 | |
10f04917 | 3834 | if (cpp_macro_p (node)) |
711b8824 | 3835 | { |
a69cbaac NB |
3836 | if (CPP_OPTION (pfile, warn_unused_macros)) |
3837 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
3838 | ||
cbc69f84 | 3839 | if (warn_of_redefinition (pfile, node, macro)) |
711b8824 | 3840 | { |
c24300ba | 3841 | const enum cpp_warning_reason reason |
3f6677f4 NS |
3842 | = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN)) |
3843 | ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE; | |
87cf0651 | 3844 | |
b753b37b MLI |
3845 | bool warned = |
3846 | cpp_pedwarning_with_line (pfile, reason, | |
3847 | pfile->directive_line, 0, | |
3848 | "\"%s\" redefined", NODE_NAME (node)); | |
93c80368 | 3849 | |
3f6677f4 | 3850 | if (warned && cpp_user_macro_p (node)) |
148e4216 | 3851 | cpp_error_with_line (pfile, CPP_DL_NOTE, |
cbc69f84 NB |
3852 | node->value.macro->line, 0, |
3853 | "this is the location of the previous definition"); | |
711b8824 | 3854 | } |
3f6677f4 | 3855 | _cpp_free_definition (node); |
711b8824 ZW |
3856 | } |
3857 | ||
3858 | /* Enter definition in hash table. */ | |
a570d97f | 3859 | node->type = NT_USER_MACRO; |
93c80368 | 3860 | node->value.macro = macro; |
607f74e9 | 3861 | if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")) |
ec46053b TT |
3862 | && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS") |
3863 | /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned | |
3864 | in the C standard, as something that one must use in C++. | |
3976796b ESR |
3865 | However DR#593 and C++11 indicate that they play no role in C++. |
3866 | We special-case them anyway. */ | |
ec46053b TT |
3867 | && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS") |
3868 | && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS")) | |
618cdda7 | 3869 | node->flags |= NODE_WARN; |
711b8824 | 3870 | |
5950c3c9 BE |
3871 | /* If user defines one of the conditional macros, remove the |
3872 | conditional flag */ | |
3873 | node->flags &= ~NODE_CONDITIONAL; | |
3874 | ||
10f04917 | 3875 | return true; |
711b8824 ZW |
3876 | } |
3877 | ||
800c0e98 NS |
3878 | extern void |
3879 | cpp_define_lazily (cpp_reader *pfile, cpp_hashnode *node, unsigned num) | |
3880 | { | |
3881 | cpp_macro *macro = node->value.macro; | |
3882 | ||
7692e253 | 3883 | gcc_checking_assert (pfile->cb.user_lazy_macro && macro && num < UCHAR_MAX); |
800c0e98 NS |
3884 | |
3885 | macro->lazy = num + 1; | |
3886 | } | |
3887 | ||
13f93cf5 NS |
3888 | /* NODE is a deferred macro, resolve it, returning the definition |
3889 | (which may be NULL). */ | |
3890 | cpp_macro * | |
3891 | cpp_get_deferred_macro (cpp_reader *pfile, cpp_hashnode *node, | |
3892 | location_t loc) | |
3893 | { | |
62c5ea52 NS |
3894 | gcc_checking_assert (node->type == NT_USER_MACRO); |
3895 | ||
13f93cf5 NS |
3896 | node->value.macro = pfile->cb.user_deferred_macro (pfile, loc, node); |
3897 | ||
3898 | if (!node->value.macro) | |
3899 | node->type = NT_VOID; | |
3900 | ||
3901 | return node->value.macro; | |
3902 | } | |
3903 | ||
3904 | static cpp_macro * | |
3905 | get_deferred_or_lazy_macro (cpp_reader *pfile, cpp_hashnode *node, | |
3906 | location_t loc) | |
3907 | { | |
3908 | cpp_macro *macro = node->value.macro; | |
3909 | if (!macro) | |
3910 | { | |
3911 | macro = cpp_get_deferred_macro (pfile, node, loc); | |
62c5ea52 | 3912 | gcc_checking_assert (!macro || !macro->lazy); |
13f93cf5 | 3913 | } |
62c5ea52 | 3914 | else if (macro->lazy) |
13f93cf5 NS |
3915 | { |
3916 | pfile->cb.user_lazy_macro (pfile, macro, macro->lazy - 1); | |
3917 | macro->lazy = 0; | |
3918 | } | |
3919 | ||
3920 | return macro; | |
3921 | } | |
3922 | ||
3f6677f4 | 3923 | /* Notify the use of NODE in a macro-aware context (i.e. expanding it, |
e9a2e208 NS |
3924 | or testing its existance). Also applies any lazy definition. |
3925 | Return FALSE if the macro isn't really there. */ | |
3f6677f4 | 3926 | |
13f93cf5 | 3927 | extern bool |
e9a2e208 NS |
3928 | _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node, |
3929 | location_t loc) | |
3f6677f4 NS |
3930 | { |
3931 | node->flags |= NODE_USED; | |
3932 | switch (node->type) | |
3933 | { | |
a570d97f | 3934 | case NT_USER_MACRO: |
13f93cf5 NS |
3935 | if (!get_deferred_or_lazy_macro (pfile, node, loc)) |
3936 | return false; | |
a570d97f | 3937 | /* FALLTHROUGH. */ |
3f6677f4 | 3938 | |
a570d97f | 3939 | case NT_BUILTIN_MACRO: |
3f6677f4 | 3940 | if (pfile->cb.used_define) |
e9a2e208 | 3941 | pfile->cb.used_define (pfile, loc, node); |
3f6677f4 NS |
3942 | break; |
3943 | ||
3944 | case NT_VOID: | |
3945 | if (pfile->cb.used_undef) | |
e9a2e208 | 3946 | pfile->cb.used_undef (pfile, loc, node); |
3f6677f4 NS |
3947 | break; |
3948 | ||
3949 | default: | |
3950 | abort (); | |
3951 | } | |
13f93cf5 NS |
3952 | |
3953 | return true; | |
3f6677f4 NS |
3954 | } |
3955 | ||
d15a58c0 NB |
3956 | /* Warn if a token in STRING matches one of a function-like MACRO's |
3957 | parameters. */ | |
e1aa5140 | 3958 | static void |
6cf87ca4 ZW |
3959 | check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro, |
3960 | const cpp_string *string) | |
e1aa5140 | 3961 | { |
93c80368 | 3962 | unsigned int i, len; |
6338b358 | 3963 | const uchar *p, *q, *limit; |
df383483 | 3964 | |
e1aa5140 | 3965 | /* Loop over the string. */ |
6338b358 NB |
3966 | limit = string->text + string->len - 1; |
3967 | for (p = string->text + 1; p < limit; p = q) | |
e1aa5140 | 3968 | { |
e1aa5140 | 3969 | /* Find the start of an identifier. */ |
61c16b10 GM |
3970 | while (p < limit && !is_idstart (*p)) |
3971 | p++; | |
e1aa5140 KG |
3972 | |
3973 | /* Find the end of the identifier. */ | |
3974 | q = p; | |
61c16b10 GM |
3975 | while (q < limit && is_idchar (*q)) |
3976 | q++; | |
93c80368 NB |
3977 | |
3978 | len = q - p; | |
3979 | ||
e1aa5140 KG |
3980 | /* Loop over the function macro arguments to see if the |
3981 | identifier inside the string matches one of them. */ | |
93c80368 NB |
3982 | for (i = 0; i < macro->paramc; i++) |
3983 | { | |
3fb558b1 | 3984 | const cpp_hashnode *node = macro->parm.params[i]; |
e1aa5140 | 3985 | |
2a967f3d NB |
3986 | if (NODE_LEN (node) == len |
3987 | && !memcmp (p, NODE_NAME (node), len)) | |
e1aa5140 | 3988 | { |
34b81eb9 | 3989 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
f458d1d5 | 3990 | "macro argument \"%s\" would be stringified in traditional C", |
ebef4e8c | 3991 | NODE_NAME (node)); |
e1aa5140 KG |
3992 | break; |
3993 | } | |
3994 | } | |
3995 | } | |
3996 | } | |
93c80368 | 3997 | |
7096171b NB |
3998 | /* Returns the name, arguments and expansion of a macro, in a format |
3999 | suitable to be read back in again, and therefore also for DWARF 2 | |
4000 | debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION". | |
4001 | Caller is expected to generate the "#define" bit if needed. The | |
93c80368 | 4002 | returned text is temporary, and automatically freed later. */ |
93c80368 | 4003 | const unsigned char * |
8e680db5 | 4004 | cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node) |
93c80368 | 4005 | { |
800c0e98 NS |
4006 | gcc_checking_assert (cpp_user_macro_p (node)); |
4007 | ||
13f93cf5 NS |
4008 | if (const cpp_macro *macro = get_deferred_or_lazy_macro (pfile, node, 0)) |
4009 | return cpp_macro_definition (pfile, node, macro); | |
4010 | return NULL; | |
4011 | } | |
4012 | ||
4013 | const unsigned char * | |
4014 | cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node, | |
4015 | const cpp_macro *macro) | |
4016 | { | |
4017 | unsigned int i, len; | |
4018 | unsigned char *buffer; | |
93c80368 NB |
4019 | |
4020 | /* Calculate length. */ | |
be5ffc59 | 4021 | len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */ |
93c80368 NB |
4022 | if (macro->fun_like) |
4023 | { | |
64d08263 JB |
4024 | len += 4; /* "()" plus possible final ".." of named |
4025 | varargs (we have + 1 below). */ | |
93c80368 | 4026 | for (i = 0; i < macro->paramc; i++) |
3fb558b1 | 4027 | len += NODE_LEN (macro->parm.params[i]) + 1; /* "," */ |
93c80368 NB |
4028 | } |
4029 | ||
6da55c00 | 4030 | /* This should match below where we fill in the buffer. */ |
278c4662 NB |
4031 | if (CPP_OPTION (pfile, traditional)) |
4032 | len += _cpp_replacement_text_len (macro); | |
4033 | else | |
93c80368 | 4034 | { |
aa508502 JM |
4035 | unsigned int count = macro_real_token_count (macro); |
4036 | for (i = 0; i < count; i++) | |
278c4662 | 4037 | { |
10f04917 | 4038 | const cpp_token *token = ¯o->exp.tokens[i]; |
93c80368 | 4039 | |
278c4662 | 4040 | if (token->type == CPP_MACRO_ARG) |
be5ffc59 | 4041 | len += NODE_LEN (token->val.macro_arg.spelling); |
278c4662 | 4042 | else |
6da55c00 EC |
4043 | len += cpp_token_len (token); |
4044 | ||
278c4662 NB |
4045 | if (token->flags & STRINGIFY_ARG) |
4046 | len++; /* "#" */ | |
4047 | if (token->flags & PASTE_LEFT) | |
4048 | len += 3; /* " ##" */ | |
6da55c00 EC |
4049 | if (token->flags & PREV_WHITE) |
4050 | len++; /* " " */ | |
278c4662 | 4051 | } |
93c80368 NB |
4052 | } |
4053 | ||
4054 | if (len > pfile->macro_buffer_len) | |
4b49c365 | 4055 | { |
c3f829c1 GDR |
4056 | pfile->macro_buffer = XRESIZEVEC (unsigned char, |
4057 | pfile->macro_buffer, len); | |
4b49c365 AO |
4058 | pfile->macro_buffer_len = len; |
4059 | } | |
7096171b NB |
4060 | |
4061 | /* Fill in the buffer. Start with the macro name. */ | |
93c80368 | 4062 | buffer = pfile->macro_buffer; |
be5ffc59 | 4063 | buffer = _cpp_spell_ident_ucns (buffer, node); |
93c80368 NB |
4064 | |
4065 | /* Parameter names. */ | |
4066 | if (macro->fun_like) | |
4067 | { | |
4068 | *buffer++ = '('; | |
4069 | for (i = 0; i < macro->paramc; i++) | |
4070 | { | |
3fb558b1 | 4071 | cpp_hashnode *param = macro->parm.params[i]; |
93c80368 NB |
4072 | |
4073 | if (param != pfile->spec_nodes.n__VA_ARGS__) | |
4074 | { | |
a28c5035 NB |
4075 | memcpy (buffer, NODE_NAME (param), NODE_LEN (param)); |
4076 | buffer += NODE_LEN (param); | |
93c80368 NB |
4077 | } |
4078 | ||
4079 | if (i + 1 < macro->paramc) | |
df383483 KH |
4080 | /* Don't emit a space after the comma here; we're trying |
4081 | to emit a Dwarf-friendly definition, and the Dwarf spec | |
4082 | forbids spaces in the argument list. */ | |
64d08263 | 4083 | *buffer++ = ','; |
28e0f040 | 4084 | else if (macro->variadic) |
93c80368 NB |
4085 | *buffer++ = '.', *buffer++ = '.', *buffer++ = '.'; |
4086 | } | |
4087 | *buffer++ = ')'; | |
4088 | } | |
4089 | ||
e37b38d7 JB |
4090 | /* The Dwarf spec requires a space after the macro name, even if the |
4091 | definition is the empty string. */ | |
4092 | *buffer++ = ' '; | |
4093 | ||
278c4662 NB |
4094 | if (CPP_OPTION (pfile, traditional)) |
4095 | buffer = _cpp_copy_replacement_text (macro, buffer); | |
4096 | else if (macro->count) | |
93c80368 | 4097 | /* Expansion tokens. */ |
93c80368 | 4098 | { |
aa508502 JM |
4099 | unsigned int count = macro_real_token_count (macro); |
4100 | for (i = 0; i < count; i++) | |
93c80368 | 4101 | { |
10f04917 | 4102 | const cpp_token *token = ¯o->exp.tokens[i]; |
93c80368 NB |
4103 | |
4104 | if (token->flags & PREV_WHITE) | |
4105 | *buffer++ = ' '; | |
4106 | if (token->flags & STRINGIFY_ARG) | |
4107 | *buffer++ = '#'; | |
4108 | ||
4109 | if (token->type == CPP_MACRO_ARG) | |
4110 | { | |
a28c5035 | 4111 | memcpy (buffer, |
be5ffc59 JM |
4112 | NODE_NAME (token->val.macro_arg.spelling), |
4113 | NODE_LEN (token->val.macro_arg.spelling)); | |
4114 | buffer += NODE_LEN (token->val.macro_arg.spelling); | |
93c80368 NB |
4115 | } |
4116 | else | |
be5ffc59 | 4117 | buffer = cpp_spell_token (pfile, token, buffer, true); |
93c80368 NB |
4118 | |
4119 | if (token->flags & PASTE_LEFT) | |
4120 | { | |
4121 | *buffer++ = ' '; | |
4122 | *buffer++ = '#'; | |
4123 | *buffer++ = '#'; | |
4124 | /* Next has PREV_WHITE; see _cpp_create_definition. */ | |
4125 | } | |
4126 | } | |
4127 | } | |
4128 | ||
4129 | *buffer = '\0'; | |
4130 | return pfile->macro_buffer; | |
4131 | } |