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