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