macroexp.c revision 1.9 1 1.1 christos /* C preprocessor macro expansion for GDB.
2 1.9 christos Copyright (C) 2002-2020 Free Software Foundation, Inc.
3 1.1 christos Contributed by Red Hat, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "gdb_obstack.h"
22 1.1 christos #include "macrotab.h"
23 1.1 christos #include "macroexp.h"
24 1.9 christos #include "macroscope.h"
25 1.1 christos #include "c-lang.h"
26 1.1 christos
27 1.1 christos
28 1.1 christos
29 1.1 christos /* A resizeable, substringable string type. */
31 1.1 christos
32 1.1 christos
33 1.1 christos /* A string type that we can resize, quickly append to, and use to
34 1.1 christos refer to substrings of other strings. */
35 1.1 christos struct macro_buffer
36 1.1 christos {
37 1.1 christos /* An array of characters. The first LEN bytes are the real text,
38 1.1 christos but there are SIZE bytes allocated to the array. If SIZE is
39 1.1 christos zero, then this doesn't point to a malloc'ed block. If SHARED is
40 1.1 christos non-zero, then this buffer is actually a pointer into some larger
41 1.1 christos string, and we shouldn't append characters to it, etc. Because
42 1.1 christos of sharing, we can't assume in general that the text is
43 1.1 christos null-terminated. */
44 1.1 christos char *text;
45 1.1 christos
46 1.1 christos /* The number of characters in the string. */
47 1.1 christos int len;
48 1.1 christos
49 1.1 christos /* The number of characters allocated to the string. If SHARED is
50 1.1 christos non-zero, this is meaningless; in this case, we set it to zero so
51 1.1 christos that any "do we have room to append something?" tests will fail,
52 1.1 christos so we don't always have to check SHARED before using this field. */
53 1.1 christos int size;
54 1.1 christos
55 1.1 christos /* Zero if TEXT can be safely realloc'ed (i.e., it's its own malloc
56 1.7 christos block). Non-zero if TEXT is actually pointing into the middle of
57 1.7 christos some other block, or to a string literal, and we shouldn't
58 1.7 christos reallocate it. */
59 1.1 christos bool shared;
60 1.1 christos
61 1.1 christos /* For detecting token splicing.
62 1.1 christos
63 1.1 christos This is the index in TEXT of the first character of the token
64 1.1 christos that abuts the end of TEXT. If TEXT contains no tokens, then we
65 1.1 christos set this equal to LEN. If TEXT ends in whitespace, then there is
66 1.1 christos no token abutting the end of TEXT (it's just whitespace), and
67 1.1 christos again, we set this equal to LEN. We set this to -1 if we don't
68 1.8 christos know the nature of TEXT. */
69 1.1 christos int last_token = -1;
70 1.1 christos
71 1.1 christos /* If this buffer is holding the result from get_token, then this
72 1.8 christos is non-zero if it is an identifier token, zero otherwise. */
73 1.1 christos int is_identifier = 0;
74 1.1 christos
75 1.8 christos
76 1.8 christos macro_buffer ()
77 1.8 christos : text (NULL),
78 1.8 christos len (0),
79 1.8 christos size (0),
80 1.8 christos shared (false)
81 1.8 christos {
82 1.1 christos }
83 1.8 christos
84 1.8 christos /* Set the macro buffer to the empty string, guessing that its
85 1.8 christos final contents will fit in N bytes. (It'll get resized if it
86 1.8 christos doesn't, so the guess doesn't have to be right.) Allocate the
87 1.8 christos initial storage with xmalloc. */
88 1.8 christos explicit macro_buffer (int n)
89 1.8 christos : len (0),
90 1.8 christos size (n),
91 1.8 christos shared (false)
92 1.8 christos {
93 1.8 christos if (n > 0)
94 1.8 christos text = (char *) xmalloc (n);
95 1.8 christos else
96 1.8 christos text = NULL;
97 1.1 christos }
98 1.8 christos
99 1.8 christos /* Set the macro buffer to refer to the LEN bytes at ADDR, as a
100 1.8 christos shared substring. */
101 1.8 christos macro_buffer (const char *addr, int len)
102 1.8 christos {
103 1.8 christos set_shared (addr, len);
104 1.7 christos }
105 1.8 christos
106 1.8 christos /* Set the macro buffer to refer to the LEN bytes at ADDR, as a
107 1.8 christos shared substring. */
108 1.8 christos void set_shared (const char *addr, int len_)
109 1.8 christos {
110 1.8 christos text = (char *) addr;
111 1.8 christos len = len_;
112 1.8 christos size = 0;
113 1.8 christos shared = true;
114 1.1 christos }
115 1.8 christos
116 1.8 christos macro_buffer& operator= (const macro_buffer &src)
117 1.8 christos {
118 1.8 christos gdb_assert (src.shared);
119 1.8 christos gdb_assert (shared);
120 1.8 christos set_shared (src.text, src.len);
121 1.8 christos last_token = src.last_token;
122 1.8 christos is_identifier = src.is_identifier;
123 1.8 christos return *this;
124 1.1 christos }
125 1.8 christos
126 1.8 christos ~macro_buffer ()
127 1.8 christos {
128 1.8 christos if (! shared && size)
129 1.8 christos xfree (text);
130 1.1 christos }
131 1.9 christos
132 1.9 christos /* Release the text of the buffer to the caller. */
133 1.8 christos gdb::unique_xmalloc_ptr<char> release ()
134 1.8 christos {
135 1.8 christos gdb_assert (! shared);
136 1.8 christos gdb_assert (size);
137 1.8 christos char *result = text;
138 1.9 christos text = NULL;
139 1.8 christos return gdb::unique_xmalloc_ptr<char> (result);
140 1.1 christos }
141 1.8 christos
142 1.8 christos /* Resize the buffer to be at least N bytes long. Raise an error if
143 1.8 christos the buffer shouldn't be resized. */
144 1.8 christos void resize_buffer (int n)
145 1.8 christos {
146 1.8 christos /* We shouldn't be trying to resize shared strings. */
147 1.1 christos gdb_assert (! shared);
148 1.8 christos
149 1.8 christos if (size == 0)
150 1.8 christos size = n;
151 1.8 christos else
152 1.8 christos while (size <= n)
153 1.1 christos size *= 2;
154 1.8 christos
155 1.8 christos text = (char *) xrealloc (text, size);
156 1.1 christos }
157 1.8 christos
158 1.8 christos /* Append the character C to the buffer. */
159 1.8 christos void appendc (int c)
160 1.8 christos {
161 1.1 christos int new_len = len + 1;
162 1.8 christos
163 1.8 christos if (new_len > size)
164 1.1 christos resize_buffer (new_len);
165 1.8 christos
166 1.8 christos text[len] = c;
167 1.8 christos len = new_len;
168 1.1 christos }
169 1.8 christos
170 1.8 christos /* Append the COUNT bytes at ADDR to the buffer. */
171 1.8 christos void appendmem (const char *addr, int count)
172 1.8 christos {
173 1.1 christos int new_len = len + count;
174 1.8 christos
175 1.8 christos if (new_len > size)
176 1.1 christos resize_buffer (new_len);
177 1.8 christos
178 1.8 christos memcpy (text + len, addr, count);
179 1.8 christos len = new_len;
180 1.8 christos }
181 1.1 christos };
182 1.1 christos
183 1.1 christos
184 1.1 christos
185 1.1 christos /* Recognizing preprocessor tokens. */
187 1.1 christos
188 1.1 christos
189 1.1 christos int
190 1.1 christos macro_is_whitespace (int c)
191 1.1 christos {
192 1.1 christos return (c == ' '
193 1.1 christos || c == '\t'
194 1.1 christos || c == '\n'
195 1.1 christos || c == '\v'
196 1.1 christos || c == '\f');
197 1.1 christos }
198 1.1 christos
199 1.1 christos
200 1.1 christos int
201 1.1 christos macro_is_digit (int c)
202 1.1 christos {
203 1.1 christos return ('0' <= c && c <= '9');
204 1.1 christos }
205 1.1 christos
206 1.1 christos
207 1.1 christos int
208 1.1 christos macro_is_identifier_nondigit (int c)
209 1.1 christos {
210 1.1 christos return (c == '_'
211 1.1 christos || ('a' <= c && c <= 'z')
212 1.1 christos || ('A' <= c && c <= 'Z'));
213 1.1 christos }
214 1.1 christos
215 1.1 christos
216 1.1 christos static void
217 1.8 christos set_token (struct macro_buffer *tok, char *start, char *end)
218 1.1 christos {
219 1.1 christos tok->set_shared (start, end - start);
220 1.1 christos tok->last_token = 0;
221 1.1 christos
222 1.1 christos /* Presumed; get_identifier may overwrite this. */
223 1.1 christos tok->is_identifier = 0;
224 1.1 christos }
225 1.1 christos
226 1.1 christos
227 1.1 christos static int
228 1.1 christos get_comment (struct macro_buffer *tok, char *p, char *end)
229 1.1 christos {
230 1.1 christos if (p + 2 > end)
231 1.1 christos return 0;
232 1.1 christos else if (p[0] == '/'
233 1.1 christos && p[1] == '*')
234 1.1 christos {
235 1.1 christos char *tok_start = p;
236 1.1 christos
237 1.1 christos p += 2;
238 1.1 christos
239 1.1 christos for (; p < end; p++)
240 1.1 christos if (p + 2 <= end
241 1.1 christos && p[0] == '*'
242 1.1 christos && p[1] == '/')
243 1.1 christos {
244 1.1 christos p += 2;
245 1.1 christos set_token (tok, tok_start, p);
246 1.1 christos return 1;
247 1.1 christos }
248 1.1 christos
249 1.1 christos error (_("Unterminated comment in macro expansion."));
250 1.1 christos }
251 1.1 christos else if (p[0] == '/'
252 1.1 christos && p[1] == '/')
253 1.1 christos {
254 1.1 christos char *tok_start = p;
255 1.1 christos
256 1.1 christos p += 2;
257 1.1 christos for (; p < end; p++)
258 1.1 christos if (*p == '\n')
259 1.1 christos break;
260 1.1 christos
261 1.1 christos set_token (tok, tok_start, p);
262 1.1 christos return 1;
263 1.1 christos }
264 1.1 christos else
265 1.1 christos return 0;
266 1.1 christos }
267 1.1 christos
268 1.1 christos
269 1.1 christos static int
270 1.1 christos get_identifier (struct macro_buffer *tok, char *p, char *end)
271 1.1 christos {
272 1.1 christos if (p < end
273 1.1 christos && macro_is_identifier_nondigit (*p))
274 1.1 christos {
275 1.1 christos char *tok_start = p;
276 1.1 christos
277 1.1 christos while (p < end
278 1.1 christos && (macro_is_identifier_nondigit (*p)
279 1.1 christos || macro_is_digit (*p)))
280 1.1 christos p++;
281 1.1 christos
282 1.1 christos set_token (tok, tok_start, p);
283 1.1 christos tok->is_identifier = 1;
284 1.1 christos return 1;
285 1.1 christos }
286 1.1 christos else
287 1.1 christos return 0;
288 1.1 christos }
289 1.1 christos
290 1.1 christos
291 1.1 christos static int
292 1.1 christos get_pp_number (struct macro_buffer *tok, char *p, char *end)
293 1.1 christos {
294 1.1 christos if (p < end
295 1.1 christos && (macro_is_digit (*p)
296 1.1 christos || (*p == '.'
297 1.1 christos && p + 2 <= end
298 1.1 christos && macro_is_digit (p[1]))))
299 1.1 christos {
300 1.1 christos char *tok_start = p;
301 1.1 christos
302 1.1 christos while (p < end)
303 1.1 christos {
304 1.1 christos if (p + 2 <= end
305 1.1 christos && strchr ("eEpP", *p)
306 1.1 christos && (p[1] == '+' || p[1] == '-'))
307 1.1 christos p += 2;
308 1.1 christos else if (macro_is_digit (*p)
309 1.1 christos || macro_is_identifier_nondigit (*p)
310 1.1 christos || *p == '.')
311 1.1 christos p++;
312 1.1 christos else
313 1.1 christos break;
314 1.1 christos }
315 1.1 christos
316 1.1 christos set_token (tok, tok_start, p);
317 1.1 christos return 1;
318 1.1 christos }
319 1.1 christos else
320 1.1 christos return 0;
321 1.1 christos }
322 1.1 christos
323 1.1 christos
324 1.1 christos
325 1.1 christos /* If the text starting at P going up to (but not including) END
326 1.1 christos starts with a character constant, set *TOK to point to that
327 1.1 christos character constant, and return 1. Otherwise, return zero.
328 1.1 christos Signal an error if it contains a malformed or incomplete character
329 1.1 christos constant. */
330 1.1 christos static int
331 1.1 christos get_character_constant (struct macro_buffer *tok, char *p, char *end)
332 1.1 christos {
333 1.1 christos /* ISO/IEC 9899:1999 (E) Section 6.4.4.4 paragraph 1
334 1.1 christos But of course, what really matters is that we handle it the same
335 1.1 christos way GDB's C/C++ lexer does. So we call parse_escape in utils.c
336 1.1 christos to handle escape sequences. */
337 1.1 christos if ((p + 1 <= end && *p == '\'')
338 1.1 christos || (p + 2 <= end
339 1.1 christos && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
340 1.1 christos && p[1] == '\''))
341 1.1 christos {
342 1.1 christos char *tok_start = p;
343 1.1 christos int char_count = 0;
344 1.1 christos
345 1.1 christos if (*p == '\'')
346 1.1 christos p++;
347 1.1 christos else if (*p == 'L' || *p == 'u' || *p == 'U')
348 1.1 christos p += 2;
349 1.1 christos else
350 1.1 christos gdb_assert_not_reached ("unexpected character constant");
351 1.1 christos
352 1.1 christos for (;;)
353 1.1 christos {
354 1.1 christos if (p >= end)
355 1.1 christos error (_("Unmatched single quote."));
356 1.1 christos else if (*p == '\'')
357 1.1 christos {
358 1.1 christos if (!char_count)
359 1.1 christos error (_("A character constant must contain at least one "
360 1.1 christos "character."));
361 1.1 christos p++;
362 1.1 christos break;
363 1.1 christos }
364 1.1 christos else if (*p == '\\')
365 1.1 christos {
366 1.1 christos const char *s, *o;
367 1.1 christos
368 1.1 christos s = o = ++p;
369 1.1 christos char_count += c_parse_escape (&s, NULL);
370 1.1 christos p += s - o;
371 1.1 christos }
372 1.1 christos else
373 1.1 christos {
374 1.1 christos p++;
375 1.1 christos char_count++;
376 1.1 christos }
377 1.1 christos }
378 1.1 christos
379 1.1 christos set_token (tok, tok_start, p);
380 1.1 christos return 1;
381 1.1 christos }
382 1.1 christos else
383 1.1 christos return 0;
384 1.1 christos }
385 1.1 christos
386 1.1 christos
387 1.1 christos /* If the text starting at P going up to (but not including) END
388 1.1 christos starts with a string literal, set *TOK to point to that string
389 1.1 christos literal, and return 1. Otherwise, return zero. Signal an error if
390 1.1 christos it contains a malformed or incomplete string literal. */
391 1.1 christos static int
392 1.1 christos get_string_literal (struct macro_buffer *tok, char *p, char *end)
393 1.1 christos {
394 1.1 christos if ((p + 1 <= end
395 1.1 christos && *p == '"')
396 1.1 christos || (p + 2 <= end
397 1.1 christos && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
398 1.1 christos && p[1] == '"'))
399 1.1 christos {
400 1.1 christos char *tok_start = p;
401 1.1 christos
402 1.1 christos if (*p == '"')
403 1.1 christos p++;
404 1.1 christos else if (*p == 'L' || *p == 'u' || *p == 'U')
405 1.1 christos p += 2;
406 1.1 christos else
407 1.1 christos gdb_assert_not_reached ("unexpected string literal");
408 1.1 christos
409 1.1 christos for (;;)
410 1.1 christos {
411 1.1 christos if (p >= end)
412 1.1 christos error (_("Unterminated string in expression."));
413 1.1 christos else if (*p == '"')
414 1.1 christos {
415 1.1 christos p++;
416 1.1 christos break;
417 1.1 christos }
418 1.1 christos else if (*p == '\n')
419 1.1 christos error (_("Newline characters may not appear in string "
420 1.1 christos "constants."));
421 1.1 christos else if (*p == '\\')
422 1.1 christos {
423 1.1 christos const char *s, *o;
424 1.1 christos
425 1.1 christos s = o = ++p;
426 1.1 christos c_parse_escape (&s, NULL);
427 1.1 christos p += s - o;
428 1.1 christos }
429 1.1 christos else
430 1.1 christos p++;
431 1.1 christos }
432 1.1 christos
433 1.1 christos set_token (tok, tok_start, p);
434 1.1 christos return 1;
435 1.1 christos }
436 1.1 christos else
437 1.1 christos return 0;
438 1.1 christos }
439 1.1 christos
440 1.1 christos
441 1.1 christos static int
442 1.1 christos get_punctuator (struct macro_buffer *tok, char *p, char *end)
443 1.1 christos {
444 1.1 christos /* Here, speed is much less important than correctness and clarity. */
445 1.1 christos
446 1.1 christos /* ISO/IEC 9899:1999 (E) Section 6.4.6 Paragraph 1.
447 1.1 christos Note that this table is ordered in a special way. A punctuator
448 1.1 christos which is a prefix of another punctuator must appear after its
449 1.1 christos "extension". Otherwise, the wrong token will be returned. */
450 1.1 christos static const char * const punctuators[] = {
451 1.1 christos "[", "]", "(", ")", "{", "}", "?", ";", ",", "~",
452 1.1 christos "...", ".",
453 1.1 christos "->", "--", "-=", "-",
454 1.1 christos "++", "+=", "+",
455 1.1 christos "*=", "*",
456 1.1 christos "!=", "!",
457 1.1 christos "&&", "&=", "&",
458 1.1 christos "/=", "/",
459 1.1 christos "%>", "%:%:", "%:", "%=", "%",
460 1.1 christos "^=", "^",
461 1.1 christos "##", "#",
462 1.1 christos ":>", ":",
463 1.1 christos "||", "|=", "|",
464 1.1 christos "<<=", "<<", "<=", "<:", "<%", "<",
465 1.1 christos ">>=", ">>", ">=", ">",
466 1.1 christos "==", "=",
467 1.1 christos 0
468 1.1 christos };
469 1.1 christos
470 1.1 christos int i;
471 1.1 christos
472 1.1 christos if (p + 1 <= end)
473 1.1 christos {
474 1.1 christos for (i = 0; punctuators[i]; i++)
475 1.1 christos {
476 1.1 christos const char *punctuator = punctuators[i];
477 1.1 christos
478 1.1 christos if (p[0] == punctuator[0])
479 1.1 christos {
480 1.1 christos int len = strlen (punctuator);
481 1.1 christos
482 1.1 christos if (p + len <= end
483 1.1 christos && ! memcmp (p, punctuator, len))
484 1.1 christos {
485 1.1 christos set_token (tok, p, p + len);
486 1.1 christos return 1;
487 1.1 christos }
488 1.1 christos }
489 1.1 christos }
490 1.1 christos }
491 1.1 christos
492 1.1 christos return 0;
493 1.1 christos }
494 1.1 christos
495 1.1 christos
496 1.1 christos /* Peel the next preprocessor token off of SRC, and put it in TOK.
497 1.1 christos Mutate TOK to refer to the first token in SRC, and mutate SRC to
498 1.1 christos refer to the text after that token. SRC must be a shared buffer;
499 1.1 christos the resulting TOK will be shared, pointing into the same string SRC
500 1.1 christos does. Initialize TOK's last_token field. Return non-zero if we
501 1.1 christos succeed, or 0 if we didn't find any more tokens in SRC. */
502 1.1 christos static int
503 1.1 christos get_token (struct macro_buffer *tok,
504 1.1 christos struct macro_buffer *src)
505 1.1 christos {
506 1.1 christos char *p = src->text;
507 1.1 christos char *end = p + src->len;
508 1.1 christos
509 1.1 christos gdb_assert (src->shared);
510 1.1 christos
511 1.1 christos /* From the ISO C standard, ISO/IEC 9899:1999 (E), section 6.4:
512 1.1 christos
513 1.1 christos preprocessing-token:
514 1.1 christos header-name
515 1.1 christos identifier
516 1.1 christos pp-number
517 1.1 christos character-constant
518 1.1 christos string-literal
519 1.1 christos punctuator
520 1.1 christos each non-white-space character that cannot be one of the above
521 1.1 christos
522 1.1 christos We don't have to deal with header-name tokens, since those can
523 1.1 christos only occur after a #include, which we will never see. */
524 1.1 christos
525 1.1 christos while (p < end)
526 1.1 christos if (macro_is_whitespace (*p))
527 1.1 christos p++;
528 1.1 christos else if (get_comment (tok, p, end))
529 1.1 christos p += tok->len;
530 1.1 christos else if (get_pp_number (tok, p, end)
531 1.1 christos || get_character_constant (tok, p, end)
532 1.1 christos || get_string_literal (tok, p, end)
533 1.1 christos /* Note: the grammar in the standard seems to be
534 1.1 christos ambiguous: L'x' can be either a wide character
535 1.1 christos constant, or an identifier followed by a normal
536 1.1 christos character constant. By trying `get_identifier' after
537 1.1 christos we try get_character_constant and get_string_literal,
538 1.1 christos we give the wide character syntax precedence. Now,
539 1.1 christos since GDB doesn't handle wide character constants
540 1.1 christos anyway, is this the right thing to do? */
541 1.1 christos || get_identifier (tok, p, end)
542 1.1 christos || get_punctuator (tok, p, end))
543 1.1 christos {
544 1.1 christos /* How many characters did we consume, including whitespace? */
545 1.1 christos int consumed = p - src->text + tok->len;
546 1.1 christos
547 1.1 christos src->text += consumed;
548 1.1 christos src->len -= consumed;
549 1.1 christos return 1;
550 1.1 christos }
551 1.1 christos else
552 1.1 christos {
553 1.1 christos /* We have found a "non-whitespace character that cannot be
554 1.1 christos one of the above." Make a token out of it. */
555 1.1 christos int consumed;
556 1.1 christos
557 1.1 christos set_token (tok, p, p + 1);
558 1.1 christos consumed = p - src->text + tok->len;
559 1.1 christos src->text += consumed;
560 1.1 christos src->len -= consumed;
561 1.1 christos return 1;
562 1.1 christos }
563 1.1 christos
564 1.1 christos return 0;
565 1.1 christos }
566 1.1 christos
567 1.1 christos
568 1.1 christos
569 1.1 christos /* Appending token strings, with and without splicing */
571 1.1 christos
572 1.1 christos
573 1.1 christos /* Append the macro buffer SRC to the end of DEST, and ensure that
574 1.1 christos doing so doesn't splice the token at the end of SRC with the token
575 1.1 christos at the beginning of DEST. SRC and DEST must have their last_token
576 1.1 christos fields set. Upon return, DEST's last_token field is set correctly.
577 1.1 christos
578 1.1 christos For example:
579 1.1 christos
580 1.1 christos If DEST is "(" and SRC is "y", then we can return with
581 1.1 christos DEST set to "(y" --- we've simply appended the two buffers.
582 1.1 christos
583 1.1 christos However, if DEST is "x" and SRC is "y", then we must not return
584 1.1 christos with DEST set to "xy" --- that would splice the two tokens "x" and
585 1.1 christos "y" together to make a single token "xy". However, it would be
586 1.1 christos fine to return with DEST set to "x y". Similarly, "<" and "<" must
587 1.1 christos yield "< <", not "<<", etc. */
588 1.1 christos static void
589 1.1 christos append_tokens_without_splicing (struct macro_buffer *dest,
590 1.1 christos struct macro_buffer *src)
591 1.1 christos {
592 1.1 christos int original_dest_len = dest->len;
593 1.1 christos struct macro_buffer dest_tail, new_token;
594 1.1 christos
595 1.1 christos gdb_assert (src->last_token != -1);
596 1.1 christos gdb_assert (dest->last_token != -1);
597 1.8 christos
598 1.1 christos /* First, just try appending the two, and call get_token to see if
599 1.1 christos we got a splice. */
600 1.1 christos dest->appendmem (src->text, src->len);
601 1.1 christos
602 1.1 christos /* If DEST originally had no token abutting its end, then we can't
603 1.1 christos have spliced anything, so we're done. */
604 1.1 christos if (dest->last_token == original_dest_len)
605 1.1 christos {
606 1.1 christos dest->last_token = original_dest_len + src->last_token;
607 1.1 christos return;
608 1.1 christos }
609 1.8 christos
610 1.8 christos /* Set DEST_TAIL to point to the last token in DEST, followed by
611 1.1 christos all the stuff we just appended. */
612 1.1 christos dest_tail.set_shared (dest->text + dest->last_token,
613 1.1 christos dest->len - dest->last_token);
614 1.1 christos
615 1.1 christos /* Re-parse DEST's last token. We know that DEST used to contain
616 1.1 christos at least one token, so if it doesn't contain any after the
617 1.1 christos append, then we must have spliced "/" and "*" or "/" and "/" to
618 1.1 christos make a comment start. (Just for the record, I got this right
619 1.1 christos the first time. This is not a bug fix.) */
620 1.1 christos if (get_token (&new_token, &dest_tail)
621 1.1 christos && (new_token.text + new_token.len
622 1.1 christos == dest->text + original_dest_len))
623 1.1 christos {
624 1.1 christos /* No splice, so we're done. */
625 1.1 christos dest->last_token = original_dest_len + src->last_token;
626 1.1 christos return;
627 1.1 christos }
628 1.1 christos
629 1.1 christos /* Okay, a simple append caused a splice. Let's chop dest back to
630 1.8 christos its original length and try again, but separate the texts with a
631 1.8 christos space. */
632 1.1 christos dest->len = original_dest_len;
633 1.8 christos dest->appendc (' ');
634 1.8 christos dest->appendmem (src->text, src->len);
635 1.1 christos
636 1.1 christos dest_tail.set_shared (dest->text + dest->last_token,
637 1.1 christos dest->len - dest->last_token);
638 1.1 christos
639 1.1 christos /* Try to re-parse DEST's last token, as above. */
640 1.1 christos if (get_token (&new_token, &dest_tail)
641 1.1 christos && (new_token.text + new_token.len
642 1.1 christos == dest->text + original_dest_len))
643 1.1 christos {
644 1.1 christos /* No splice, so we're done. */
645 1.1 christos dest->last_token = original_dest_len + 1 + src->last_token;
646 1.1 christos return;
647 1.1 christos }
648 1.1 christos
649 1.1 christos /* As far as I know, there's no case where inserting a space isn't
650 1.1 christos enough to prevent a splice. */
651 1.1 christos internal_error (__FILE__, __LINE__,
652 1.1 christos _("unable to avoid splicing tokens during macro expansion"));
653 1.1 christos }
654 1.1 christos
655 1.1 christos /* Stringify an argument, and insert it into DEST. ARG is the text to
656 1.1 christos stringify; it is LEN bytes long. */
657 1.1 christos
658 1.1 christos static void
659 1.1 christos stringify (struct macro_buffer *dest, const char *arg, int len)
660 1.1 christos {
661 1.1 christos /* Trim initial whitespace from ARG. */
662 1.1 christos while (len > 0 && macro_is_whitespace (*arg))
663 1.1 christos {
664 1.1 christos ++arg;
665 1.1 christos --len;
666 1.1 christos }
667 1.1 christos
668 1.1 christos /* Trim trailing whitespace from ARG. */
669 1.1 christos while (len > 0 && macro_is_whitespace (arg[len - 1]))
670 1.8 christos --len;
671 1.1 christos
672 1.1 christos /* Insert the string. */
673 1.1 christos dest->appendc ('"');
674 1.1 christos while (len > 0)
675 1.1 christos {
676 1.1 christos /* We could try to handle strange cases here, like control
677 1.1 christos characters, but there doesn't seem to be much point. */
678 1.8 christos if (macro_is_whitespace (*arg))
679 1.1 christos {
680 1.1 christos /* Replace a sequence of whitespace with a single space. */
681 1.1 christos dest->appendc (' ');
682 1.1 christos while (len > 1 && macro_is_whitespace (arg[1]))
683 1.1 christos {
684 1.1 christos ++arg;
685 1.1 christos --len;
686 1.1 christos }
687 1.8 christos }
688 1.8 christos else if (*arg == '\\' || *arg == '"')
689 1.1 christos {
690 1.1 christos dest->appendc ('\\');
691 1.8 christos dest->appendc (*arg);
692 1.1 christos }
693 1.1 christos else
694 1.1 christos dest->appendc (*arg);
695 1.8 christos ++arg;
696 1.1 christos --len;
697 1.1 christos }
698 1.1 christos dest->appendc ('"');
699 1.1 christos dest->last_token = dest->len;
700 1.1 christos }
701 1.9 christos
702 1.1 christos /* See macroexp.h. */
703 1.1 christos
704 1.1 christos gdb::unique_xmalloc_ptr<char>
705 1.8 christos macro_stringify (const char *str)
706 1.1 christos {
707 1.1 christos int len = strlen (str);
708 1.8 christos struct macro_buffer buffer (len);
709 1.1 christos
710 1.8 christos stringify (&buffer, str, len);
711 1.1 christos buffer.appendc ('\0');
712 1.1 christos
713 1.1 christos return buffer.release ();
714 1.1 christos }
715 1.1 christos
716 1.1 christos
717 1.1 christos /* Expanding macros! */
719 1.1 christos
720 1.1 christos
721 1.1 christos /* A singly-linked list of the names of the macros we are currently
722 1.1 christos expanding --- for detecting expansion loops. */
723 1.1 christos struct macro_name_list {
724 1.1 christos const char *name;
725 1.1 christos struct macro_name_list *next;
726 1.1 christos };
727 1.1 christos
728 1.1 christos
729 1.1 christos /* Return non-zero if we are currently expanding the macro named NAME,
730 1.1 christos according to LIST; otherwise, return zero.
731 1.1 christos
732 1.1 christos You know, it would be possible to get rid of all the NO_LOOP
733 1.1 christos arguments to these functions by simply generating a new lookup
734 1.1 christos function and baton which refuses to find the definition for a
735 1.1 christos particular macro, and otherwise delegates the decision to another
736 1.1 christos function/baton pair. But that makes the linked list of excluded
737 1.1 christos macros chained through untyped baton pointers, which will make it
738 1.1 christos harder to debug. :( */
739 1.1 christos static int
740 1.1 christos currently_rescanning (struct macro_name_list *list, const char *name)
741 1.1 christos {
742 1.1 christos for (; list; list = list->next)
743 1.1 christos if (strcmp (name, list->name) == 0)
744 1.1 christos return 1;
745 1.1 christos
746 1.1 christos return 0;
747 1.1 christos }
748 1.1 christos
749 1.1 christos
750 1.1 christos /* Gather the arguments to a macro expansion.
751 1.1 christos
752 1.1 christos NAME is the name of the macro being invoked. (It's only used for
753 1.1 christos printing error messages.)
754 1.1 christos
755 1.1 christos Assume that SRC is the text of the macro invocation immediately
756 1.1 christos following the macro name. For example, if we're processing the
757 1.8 christos text foo(bar, baz), then NAME would be foo and SRC will be (bar,
758 1.1 christos baz).
759 1.1 christos
760 1.1 christos If SRC doesn't start with an open paren ( token at all, return
761 1.8 christos false, leave SRC unchanged, and don't set *ARGS_PTR to anything.
762 1.1 christos
763 1.1 christos If SRC doesn't contain a properly terminated argument list, then
764 1.1 christos raise an error.
765 1.1 christos
766 1.1 christos For a variadic macro, NARGS holds the number of formal arguments to
767 1.8 christos the macro. For a GNU-style variadic macro, this should be the
768 1.8 christos number of named arguments. For a non-variadic macro, NARGS should
769 1.8 christos be -1.
770 1.1 christos
771 1.1 christos Otherwise, return true and set *ARGS_PTR to a vector of macro
772 1.1 christos buffers referring to the argument texts. The macro buffers share
773 1.1 christos their text with SRC, and their last_token fields are initialized.
774 1.1 christos
775 1.1 christos NOTE WELL: if SRC starts with a open paren ( token followed
776 1.1 christos immediately by a close paren ) token (e.g., the invocation looks
777 1.1 christos like "foo()"), we treat that as one argument, which happens to be
778 1.1 christos the empty list of tokens. The caller should keep in mind that such
779 1.1 christos a sequence of tokens is a valid way to invoke one-parameter
780 1.1 christos function-like macros, but also a valid way to invoke zero-parameter
781 1.1 christos function-like macros. Eeew.
782 1.8 christos
783 1.8 christos Consume the tokens from SRC; after this call, SRC contains the text
784 1.8 christos following the invocation. */
785 1.1 christos
786 1.1 christos static bool
787 1.8 christos gather_arguments (const char *name, struct macro_buffer *src, int nargs,
788 1.1 christos std::vector<struct macro_buffer> *args_ptr)
789 1.1 christos {
790 1.1 christos struct macro_buffer tok;
791 1.1 christos std::vector<struct macro_buffer> args;
792 1.1 christos
793 1.8 christos /* Does SRC start with an opening paren token? Read from a copy of
794 1.1 christos SRC, so SRC itself is unaffected if we don't find an opening
795 1.1 christos paren. */
796 1.1 christos {
797 1.1 christos struct macro_buffer temp (src->text, src->len);
798 1.8 christos
799 1.1 christos if (! get_token (&tok, &temp)
800 1.1 christos || tok.len != 1
801 1.1 christos || tok.text[0] != '(')
802 1.1 christos return false;
803 1.1 christos }
804 1.1 christos
805 1.1 christos /* Consume SRC's opening paren. */
806 1.1 christos get_token (&tok, src);
807 1.1 christos
808 1.1 christos for (;;)
809 1.1 christos {
810 1.8 christos struct macro_buffer *arg;
811 1.8 christos int depth;
812 1.1 christos
813 1.1 christos /* Initialize the next argument. */
814 1.1 christos args.emplace_back ();
815 1.1 christos arg = &args.back ();
816 1.1 christos set_token (arg, src->text, src->text);
817 1.1 christos
818 1.1 christos /* Gather the argument's tokens. */
819 1.1 christos depth = 0;
820 1.8 christos for (;;)
821 1.1 christos {
822 1.1 christos if (! get_token (&tok, src))
823 1.1 christos error (_("Malformed argument list for macro `%s'."), name);
824 1.1 christos
825 1.1 christos /* Is tok an opening paren? */
826 1.1 christos if (tok.len == 1 && tok.text[0] == '(')
827 1.1 christos depth++;
828 1.1 christos
829 1.1 christos /* Is tok is a closing paren? */
830 1.1 christos else if (tok.len == 1 && tok.text[0] == ')')
831 1.1 christos {
832 1.1 christos /* If it's a closing paren at the top level, then that's
833 1.1 christos the end of the argument list. */
834 1.8 christos if (depth == 0)
835 1.1 christos {
836 1.8 christos /* In the varargs case, the last argument may be
837 1.8 christos missing. Add an empty argument in this case. */
838 1.1 christos if (nargs != -1 && args.size () == nargs - 1)
839 1.1 christos {
840 1.1 christos args.emplace_back ();
841 1.8 christos arg = &args.back ();
842 1.8 christos set_token (arg, src->text, src->text);
843 1.1 christos }
844 1.1 christos
845 1.1 christos *args_ptr = std::move (args);
846 1.1 christos return true;
847 1.1 christos }
848 1.1 christos
849 1.1 christos depth--;
850 1.1 christos }
851 1.1 christos
852 1.1 christos /* If tok is a comma at top level, then that's the end of
853 1.8 christos the current argument. However, if we are handling a
854 1.1 christos variadic macro and we are computing the last argument, we
855 1.1 christos want to include the comma and remaining tokens. */
856 1.1 christos else if (tok.len == 1 && tok.text[0] == ',' && depth == 0
857 1.1 christos && (nargs == -1 || args.size () < nargs))
858 1.1 christos break;
859 1.1 christos
860 1.1 christos /* Extend the current argument to enclose this token. If
861 1.1 christos this is the current argument's first token, leave out any
862 1.1 christos leading whitespace, just for aesthetics. */
863 1.1 christos if (arg->len == 0)
864 1.1 christos {
865 1.1 christos arg->text = tok.text;
866 1.1 christos arg->len = tok.len;
867 1.1 christos arg->last_token = 0;
868 1.1 christos }
869 1.1 christos else
870 1.1 christos {
871 1.1 christos arg->len = (tok.text + tok.len) - arg->text;
872 1.1 christos arg->last_token = tok.text - arg->text;
873 1.1 christos }
874 1.1 christos }
875 1.1 christos }
876 1.1 christos }
877 1.1 christos
878 1.1 christos
879 1.1 christos /* The `expand' and `substitute_args' functions both invoke `scan'
880 1.9 christos recursively, so we need a forward declaration somewhere. */
881 1.1 christos static void scan (struct macro_buffer *dest,
882 1.1 christos struct macro_buffer *src,
883 1.1 christos struct macro_name_list *no_loop,
884 1.1 christos const macro_scope &scope);
885 1.1 christos
886 1.1 christos /* A helper function for substitute_args.
887 1.1 christos
888 1.1 christos ARGV is a vector of all the arguments; ARGC is the number of
889 1.1 christos arguments. IS_VARARGS is true if the macro being substituted is a
890 1.1 christos varargs macro; in this case VA_ARG_NAME is the name of the
891 1.1 christos "variable" argument. VA_ARG_NAME is ignored if IS_VARARGS is
892 1.1 christos false.
893 1.1 christos
894 1.1 christos If the token TOK is the name of a parameter, return the parameter's
895 1.1 christos index. If TOK is not an argument, return -1. */
896 1.1 christos
897 1.1 christos static int
898 1.1 christos find_parameter (const struct macro_buffer *tok,
899 1.1 christos int is_varargs, const struct macro_buffer *va_arg_name,
900 1.1 christos int argc, const char * const *argv)
901 1.1 christos {
902 1.1 christos int i;
903 1.1 christos
904 1.1 christos if (! tok->is_identifier)
905 1.1 christos return -1;
906 1.1 christos
907 1.1 christos for (i = 0; i < argc; ++i)
908 1.1 christos if (tok->len == strlen (argv[i])
909 1.1 christos && !memcmp (tok->text, argv[i], tok->len))
910 1.1 christos return i;
911 1.1 christos
912 1.1 christos if (is_varargs && tok->len == va_arg_name->len
913 1.1 christos && ! memcmp (tok->text, va_arg_name->text, tok->len))
914 1.1 christos return argc - 1;
915 1.8 christos
916 1.8 christos return -1;
917 1.8 christos }
918 1.8 christos
919 1.8 christos /* Helper function for substitute_args that gets the next token and
920 1.8 christos updates the passed-in state variables. */
921 1.8 christos
922 1.8 christos static void
923 1.8 christos get_next_token_for_substitution (struct macro_buffer *replacement_list,
924 1.8 christos struct macro_buffer *token,
925 1.8 christos char **start,
926 1.8 christos struct macro_buffer *lookahead,
927 1.8 christos char **lookahead_start,
928 1.8 christos int *lookahead_valid,
929 1.8 christos bool *keep_going)
930 1.8 christos {
931 1.8 christos if (!*lookahead_valid)
932 1.8 christos *keep_going = false;
933 1.8 christos else
934 1.8 christos {
935 1.8 christos *keep_going = true;
936 1.8 christos *token = *lookahead;
937 1.8 christos *start = *lookahead_start;
938 1.8 christos *lookahead_start = replacement_list->text;
939 1.1 christos *lookahead_valid = get_token (lookahead, replacement_list);
940 1.8 christos }
941 1.1 christos }
942 1.1 christos
943 1.1 christos /* Given the macro definition DEF, being invoked with the actual
944 1.1 christos arguments given by ARGV, substitute the arguments into the
945 1.1 christos replacement list, and store the result in DEST.
946 1.1 christos
947 1.1 christos IS_VARARGS should be true if DEF is a varargs macro. In this case,
948 1.1 christos VA_ARG_NAME should be the name of the "variable" argument -- either
949 1.1 christos __VA_ARGS__ for c99-style varargs, or the final argument name, for
950 1.1 christos GNU-style varargs. If IS_VARARGS is false, this parameter is
951 1.1 christos ignored.
952 1.1 christos
953 1.1 christos If it is necessary to expand macro invocations in one of the
954 1.1 christos arguments, use LOOKUP_FUNC and LOOKUP_BATON to find the macro
955 1.8 christos definitions, and don't expand invocations of the macros listed in
956 1.1 christos NO_LOOP. */
957 1.1 christos
958 1.8 christos static void
959 1.1 christos substitute_args (struct macro_buffer *dest,
960 1.9 christos struct macro_definition *def,
961 1.1 christos int is_varargs, const struct macro_buffer *va_arg_name,
962 1.1 christos const std::vector<struct macro_buffer> &argv,
963 1.1 christos struct macro_name_list *no_loop,
964 1.1 christos const macro_scope &scope)
965 1.1 christos {
966 1.1 christos /* The token we are currently considering. */
967 1.1 christos struct macro_buffer tok;
968 1.1 christos /* The replacement list's pointer from just before TOK was lexed. */
969 1.1 christos char *original_rl_start;
970 1.1 christos /* We have a single lookahead token to handle token splicing. */
971 1.1 christos struct macro_buffer lookahead;
972 1.1 christos /* The lookahead token might not be valid. */
973 1.1 christos int lookahead_valid;
974 1.8 christos /* The replacement list's pointer from just before LOOKAHEAD was
975 1.8 christos lexed. */
976 1.8 christos char *lookahead_rl_start;
977 1.1 christos
978 1.1 christos /* A macro buffer for the macro's replacement list. */
979 1.1 christos struct macro_buffer replacement_list (def->replacement,
980 1.1 christos strlen (def->replacement));
981 1.1 christos
982 1.1 christos gdb_assert (dest->len == 0);
983 1.1 christos dest->last_token = 0;
984 1.1 christos
985 1.1 christos original_rl_start = replacement_list.text;
986 1.1 christos if (! get_token (&tok, &replacement_list))
987 1.8 christos return;
988 1.8 christos lookahead_rl_start = replacement_list.text;
989 1.8 christos lookahead_valid = get_token (&lookahead, &replacement_list);
990 1.8 christos
991 1.8 christos /* __VA_OPT__ state variable. The states are:
992 1.8 christos 0 - nothing happening
993 1.8 christos 1 - saw __VA_OPT__
994 1.8 christos >= 2 in __VA_OPT__, the value encodes the parenthesis depth. */
995 1.8 christos unsigned vaopt_state = 0;
996 1.8 christos
997 1.8 christos for (bool keep_going = true;
998 1.8 christos keep_going;
999 1.8 christos get_next_token_for_substitution (&replacement_list,
1000 1.8 christos &tok,
1001 1.8 christos &original_rl_start,
1002 1.1 christos &lookahead,
1003 1.8 christos &lookahead_rl_start,
1004 1.8 christos &lookahead_valid,
1005 1.8 christos &keep_going))
1006 1.8 christos {
1007 1.8 christos bool token_is_vaopt = (tok.len == 10
1008 1.8 christos && strncmp (tok.text, "__VA_OPT__", 10) == 0);
1009 1.8 christos
1010 1.8 christos if (vaopt_state > 0)
1011 1.8 christos {
1012 1.8 christos if (token_is_vaopt)
1013 1.8 christos error (_("__VA_OPT__ cannot appear inside __VA_OPT__"));
1014 1.8 christos else if (tok.len == 1 && tok.text[0] == '(')
1015 1.8 christos {
1016 1.8 christos ++vaopt_state;
1017 1.8 christos /* We just entered __VA_OPT__, so don't emit this
1018 1.8 christos token. */
1019 1.8 christos continue;
1020 1.8 christos }
1021 1.8 christos else if (vaopt_state == 1)
1022 1.8 christos error (_("__VA_OPT__ must be followed by an open parenthesis"));
1023 1.8 christos else if (tok.len == 1 && tok.text[0] == ')')
1024 1.8 christos {
1025 1.8 christos --vaopt_state;
1026 1.8 christos if (vaopt_state == 1)
1027 1.8 christos {
1028 1.8 christos /* Done with __VA_OPT__. */
1029 1.8 christos vaopt_state = 0;
1030 1.8 christos /* Don't emit. */
1031 1.8 christos continue;
1032 1.8 christos }
1033 1.8 christos }
1034 1.8 christos
1035 1.8 christos /* If __VA_ARGS__ is empty, then drop the contents of
1036 1.8 christos __VA_OPT__. */
1037 1.8 christos if (argv.back ().len == 0)
1038 1.8 christos continue;
1039 1.8 christos }
1040 1.8 christos else if (token_is_vaopt)
1041 1.8 christos {
1042 1.8 christos if (!is_varargs)
1043 1.8 christos error (_("__VA_OPT__ is only valid in a variadic macro"));
1044 1.8 christos vaopt_state = 1;
1045 1.1 christos /* Don't emit this token. */
1046 1.1 christos continue;
1047 1.1 christos }
1048 1.1 christos
1049 1.8 christos /* Just for aesthetics. If we skipped some whitespace, copy
1050 1.1 christos that to DEST. */
1051 1.1 christos if (tok.text > original_rl_start)
1052 1.1 christos {
1053 1.1 christos dest->appendmem (original_rl_start, tok.text - original_rl_start);
1054 1.1 christos dest->last_token = dest->len;
1055 1.1 christos }
1056 1.1 christos
1057 1.1 christos /* Is this token the stringification operator? */
1058 1.1 christos if (tok.len == 1
1059 1.1 christos && tok.text[0] == '#')
1060 1.1 christos {
1061 1.1 christos int arg;
1062 1.1 christos
1063 1.1 christos if (!lookahead_valid)
1064 1.1 christos error (_("Stringification operator requires an argument."));
1065 1.1 christos
1066 1.1 christos arg = find_parameter (&lookahead, is_varargs, va_arg_name,
1067 1.1 christos def->argc, def->argv);
1068 1.1 christos if (arg == -1)
1069 1.1 christos error (_("Argument to stringification operator must name "
1070 1.1 christos "a macro parameter."));
1071 1.1 christos
1072 1.1 christos stringify (dest, argv[arg].text, argv[arg].len);
1073 1.1 christos
1074 1.1 christos /* Read one token and let the loop iteration code handle the
1075 1.1 christos rest. */
1076 1.1 christos lookahead_rl_start = replacement_list.text;
1077 1.1 christos lookahead_valid = get_token (&lookahead, &replacement_list);
1078 1.1 christos }
1079 1.1 christos /* Is this token the splicing operator? */
1080 1.1 christos else if (tok.len == 2
1081 1.1 christos && tok.text[0] == '#'
1082 1.1 christos && tok.text[1] == '#')
1083 1.1 christos error (_("Stray splicing operator"));
1084 1.1 christos /* Is the next token the splicing operator? */
1085 1.1 christos else if (lookahead_valid
1086 1.1 christos && lookahead.len == 2
1087 1.1 christos && lookahead.text[0] == '#'
1088 1.1 christos && lookahead.text[1] == '#')
1089 1.1 christos {
1090 1.1 christos int finished = 0;
1091 1.1 christos int prev_was_comma = 0;
1092 1.1 christos
1093 1.1 christos /* Note that GCC warns if the result of splicing is not a
1094 1.1 christos token. In the debugger there doesn't seem to be much
1095 1.1 christos benefit from doing this. */
1096 1.1 christos
1097 1.1 christos /* Insert the first token. */
1098 1.1 christos if (tok.len == 1 && tok.text[0] == ',')
1099 1.1 christos prev_was_comma = 1;
1100 1.1 christos else
1101 1.1 christos {
1102 1.8 christos int arg = find_parameter (&tok, is_varargs, va_arg_name,
1103 1.1 christos def->argc, def->argv);
1104 1.8 christos
1105 1.1 christos if (arg != -1)
1106 1.1 christos dest->appendmem (argv[arg].text, argv[arg].len);
1107 1.1 christos else
1108 1.1 christos dest->appendmem (tok.text, tok.len);
1109 1.1 christos }
1110 1.1 christos
1111 1.1 christos /* Apply a possible sequence of ## operators. */
1112 1.1 christos for (;;)
1113 1.1 christos {
1114 1.1 christos if (! get_token (&tok, &replacement_list))
1115 1.1 christos error (_("Splicing operator at end of macro"));
1116 1.1 christos
1117 1.1 christos /* Handle a comma before a ##. If we are handling
1118 1.1 christos varargs, and the token on the right hand side is the
1119 1.1 christos varargs marker, and the final argument is empty or
1120 1.1 christos missing, then drop the comma. This is a GNU
1121 1.1 christos extension. There is one ambiguous case here,
1122 1.1 christos involving pedantic behavior with an empty argument,
1123 1.1 christos but we settle that in favor of GNU-style (GCC uses an
1124 1.1 christos option). If we aren't dealing with varargs, we
1125 1.1 christos simply insert the comma. */
1126 1.1 christos if (prev_was_comma)
1127 1.8 christos {
1128 1.8 christos if (! (is_varargs
1129 1.1 christos && tok.len == va_arg_name->len
1130 1.1 christos && !memcmp (tok.text, va_arg_name->text, tok.len)
1131 1.1 christos && argv.back ().len == 0))
1132 1.1 christos dest->appendmem (",", 1);
1133 1.1 christos prev_was_comma = 0;
1134 1.1 christos }
1135 1.1 christos
1136 1.1 christos /* Insert the token. If it is a parameter, insert the
1137 1.1 christos argument. If it is a comma, treat it specially. */
1138 1.1 christos if (tok.len == 1 && tok.text[0] == ',')
1139 1.1 christos prev_was_comma = 1;
1140 1.1 christos else
1141 1.1 christos {
1142 1.8 christos int arg = find_parameter (&tok, is_varargs, va_arg_name,
1143 1.1 christos def->argc, def->argv);
1144 1.8 christos
1145 1.1 christos if (arg != -1)
1146 1.1 christos dest->appendmem (argv[arg].text, argv[arg].len);
1147 1.1 christos else
1148 1.1 christos dest->appendmem (tok.text, tok.len);
1149 1.1 christos }
1150 1.1 christos
1151 1.1 christos /* Now read another token. If it is another splice, we
1152 1.1 christos loop. */
1153 1.1 christos original_rl_start = replacement_list.text;
1154 1.1 christos if (! get_token (&tok, &replacement_list))
1155 1.1 christos {
1156 1.1 christos finished = 1;
1157 1.1 christos break;
1158 1.1 christos }
1159 1.1 christos
1160 1.1 christos if (! (tok.len == 2
1161 1.1 christos && tok.text[0] == '#'
1162 1.1 christos && tok.text[1] == '#'))
1163 1.1 christos break;
1164 1.1 christos }
1165 1.8 christos
1166 1.1 christos if (prev_was_comma)
1167 1.1 christos {
1168 1.1 christos /* We saw a comma. Insert it now. */
1169 1.1 christos dest->appendmem (",", 1);
1170 1.1 christos }
1171 1.1 christos
1172 1.1 christos dest->last_token = dest->len;
1173 1.1 christos if (finished)
1174 1.1 christos lookahead_valid = 0;
1175 1.1 christos else
1176 1.1 christos {
1177 1.1 christos /* Set up for the loop iterator. */
1178 1.1 christos lookahead = tok;
1179 1.1 christos lookahead_rl_start = original_rl_start;
1180 1.1 christos lookahead_valid = 1;
1181 1.1 christos }
1182 1.1 christos }
1183 1.1 christos else
1184 1.1 christos {
1185 1.1 christos /* Is this token an identifier? */
1186 1.1 christos int substituted = 0;
1187 1.1 christos int arg = find_parameter (&tok, is_varargs, va_arg_name,
1188 1.1 christos def->argc, def->argv);
1189 1.1 christos
1190 1.1 christos if (arg != -1)
1191 1.1 christos {
1192 1.1 christos /* Expand any macro invocations in the argument text,
1193 1.8 christos and append the result to dest. Remember that scan
1194 1.9 christos mutates its source, so we need to scan a new buffer
1195 1.1 christos referring to the argument's text, not the argument
1196 1.1 christos itself. */
1197 1.1 christos struct macro_buffer arg_src (argv[arg].text, argv[arg].len);
1198 1.1 christos scan (dest, &arg_src, no_loop, scope);
1199 1.1 christos substituted = 1;
1200 1.1 christos }
1201 1.1 christos
1202 1.8 christos /* If it wasn't a parameter, then just copy it across. */
1203 1.1 christos if (! substituted)
1204 1.8 christos append_tokens_without_splicing (dest, &tok);
1205 1.8 christos }
1206 1.1 christos }
1207 1.1 christos
1208 1.1 christos if (vaopt_state > 0)
1209 1.1 christos error (_("Unterminated __VA_OPT__"));
1210 1.1 christos }
1211 1.1 christos
1212 1.1 christos
1213 1.1 christos /* Expand a call to a macro named ID, whose definition is DEF. Append
1214 1.1 christos its expansion to DEST. SRC is the input text following the ID
1215 1.1 christos token. We are currently rescanning the expansions of the macros
1216 1.1 christos named in NO_LOOP; don't re-expand them. Use LOOKUP_FUNC and
1217 1.1 christos LOOKUP_BATON to find definitions for any nested macro references.
1218 1.1 christos
1219 1.1 christos Return 1 if we decided to expand it, zero otherwise. (If it's a
1220 1.1 christos function-like macro name that isn't followed by an argument list,
1221 1.1 christos we don't expand it.) If we return zero, leave SRC unchanged. */
1222 1.1 christos static int
1223 1.1 christos expand (const char *id,
1224 1.9 christos struct macro_definition *def,
1225 1.1 christos struct macro_buffer *dest,
1226 1.1 christos struct macro_buffer *src,
1227 1.1 christos struct macro_name_list *no_loop,
1228 1.1 christos const macro_scope &scope)
1229 1.1 christos {
1230 1.1 christos struct macro_name_list new_no_loop;
1231 1.1 christos
1232 1.1 christos /* Create a new node to be added to the front of the no-expand list.
1233 1.1 christos This list is appropriate for re-scanning replacement lists, but
1234 1.1 christos it is *not* appropriate for scanning macro arguments; invocations
1235 1.1 christos of the macro whose arguments we are gathering *do* get expanded
1236 1.1 christos there. */
1237 1.1 christos new_no_loop.name = id;
1238 1.1 christos new_no_loop.next = no_loop;
1239 1.8 christos
1240 1.8 christos /* What kind of macro are we expanding? */
1241 1.1 christos if (def->kind == macro_object_like)
1242 1.9 christos {
1243 1.1 christos struct macro_buffer replacement_list (def->replacement,
1244 1.1 christos strlen (def->replacement));
1245 1.1 christos
1246 1.1 christos scan (dest, &replacement_list, &new_no_loop, scope);
1247 1.8 christos return 1;
1248 1.1 christos }
1249 1.1 christos else if (def->kind == macro_function_like)
1250 1.1 christos {
1251 1.1 christos struct macro_buffer va_arg_name;
1252 1.1 christos int is_varargs = 0;
1253 1.1 christos
1254 1.1 christos if (def->argc >= 1)
1255 1.1 christos {
1256 1.8 christos if (strcmp (def->argv[def->argc - 1], "...") == 0)
1257 1.1 christos {
1258 1.1 christos /* In C99-style varargs, substitution is done using
1259 1.1 christos __VA_ARGS__. */
1260 1.1 christos va_arg_name.set_shared ("__VA_ARGS__", strlen ("__VA_ARGS__"));
1261 1.1 christos is_varargs = 1;
1262 1.1 christos }
1263 1.1 christos else
1264 1.1 christos {
1265 1.1 christos int len = strlen (def->argv[def->argc - 1]);
1266 1.1 christos
1267 1.1 christos if (len > 3
1268 1.1 christos && strcmp (def->argv[def->argc - 1] + len - 3, "...") == 0)
1269 1.8 christos {
1270 1.1 christos /* In GNU-style varargs, the name of the
1271 1.1 christos substitution parameter is the name of the formal
1272 1.1 christos argument without the "...". */
1273 1.1 christos va_arg_name.set_shared (def->argv[def->argc - 1], len - 3);
1274 1.1 christos is_varargs = 1;
1275 1.8 christos }
1276 1.1 christos }
1277 1.1 christos }
1278 1.8 christos
1279 1.8 christos std::vector<struct macro_buffer> argv;
1280 1.8 christos /* If we couldn't find any argument list, then we don't expand
1281 1.1 christos this macro. */
1282 1.1 christos if (!gather_arguments (id, src, is_varargs ? def->argc : -1,
1283 1.1 christos &argv))
1284 1.8 christos return 0;
1285 1.1 christos
1286 1.8 christos /* Check that we're passing an acceptable number of arguments for
1287 1.1 christos this macro. */
1288 1.1 christos if (argv.size () != def->argc)
1289 1.1 christos {
1290 1.1 christos if (is_varargs && argv.size () >= def->argc - 1)
1291 1.1 christos {
1292 1.1 christos /* Ok. */
1293 1.8 christos }
1294 1.1 christos /* Remember that a sequence of tokens like "foo()" is a
1295 1.1 christos valid invocation of a macro expecting either zero or one
1296 1.1 christos arguments. */
1297 1.1 christos else if (! (argv.size () == 1
1298 1.8 christos && argv[0].len == 0
1299 1.1 christos && def->argc == 0))
1300 1.1 christos error (_("Wrong number of arguments to macro `%s' "
1301 1.1 christos "(expected %d, got %d)."),
1302 1.1 christos id, def->argc, int (argv.size ()));
1303 1.1 christos }
1304 1.1 christos
1305 1.1 christos /* Note that we don't expand macro invocations in the arguments
1306 1.1 christos yet --- we let subst_args take care of that. Parameters that
1307 1.8 christos appear as operands of the stringifying operator "#" or the
1308 1.1 christos splicing operator "##" don't get macro references expanded,
1309 1.9 christos so we can't really tell whether it's appropriate to macro-
1310 1.1 christos expand an argument until we see how it's being used. */
1311 1.1 christos struct macro_buffer substituted (0);
1312 1.1 christos substitute_args (&substituted, def, is_varargs, &va_arg_name,
1313 1.1 christos argv, no_loop, scope);
1314 1.1 christos
1315 1.1 christos /* Now `substituted' is the macro's replacement list, with all
1316 1.1 christos argument values substituted into it properly. Re-scan it for
1317 1.1 christos macro references, but don't expand invocations of this macro.
1318 1.1 christos
1319 1.1 christos We create a new buffer, `substituted_src', which points into
1320 1.1 christos `substituted', and scan that. We can't scan `substituted'
1321 1.8 christos itself, since the tokenization process moves the buffer's
1322 1.9 christos text pointer around, and we still need to be able to find
1323 1.1 christos `substituted's original text buffer after scanning it so we
1324 1.1 christos can free it. */
1325 1.1 christos struct macro_buffer substituted_src (substituted.text, substituted.len);
1326 1.1 christos scan (dest, &substituted_src, &new_no_loop, scope);
1327 1.1 christos
1328 1.1 christos return 1;
1329 1.1 christos }
1330 1.1 christos else
1331 1.1 christos internal_error (__FILE__, __LINE__, _("bad macro definition kind"));
1332 1.9 christos }
1333 1.1 christos
1334 1.1 christos
1335 1.1 christos /* If the single token in SRC_FIRST followed by the tokens in SRC_REST
1336 1.1 christos constitute a macro invocation not forbidden in NO_LOOP, append its
1337 1.1 christos expansion to DEST and return non-zero. Otherwise, return zero, and
1338 1.1 christos leave DEST unchanged.
1339 1.1 christos
1340 1.1 christos SRC_FIRST and SRC_REST must be shared buffers; DEST must not be one.
1341 1.1 christos SRC_FIRST must be a string built by get_token. */
1342 1.1 christos static int
1343 1.9 christos maybe_expand (struct macro_buffer *dest,
1344 1.1 christos struct macro_buffer *src_first,
1345 1.1 christos struct macro_buffer *src_rest,
1346 1.1 christos struct macro_name_list *no_loop,
1347 1.1 christos const macro_scope &scope)
1348 1.1 christos {
1349 1.1 christos gdb_assert (src_first->shared);
1350 1.1 christos gdb_assert (src_rest->shared);
1351 1.1 christos gdb_assert (! dest->shared);
1352 1.1 christos
1353 1.1 christos /* Is this token an identifier? */
1354 1.8 christos if (src_first->is_identifier)
1355 1.1 christos {
1356 1.1 christos /* Make a null-terminated copy of it, since that's what our
1357 1.1 christos lookup function expects. */
1358 1.8 christos std::string id (src_first->text, src_first->len);
1359 1.1 christos
1360 1.1 christos /* If we're currently re-scanning the result of expanding
1361 1.9 christos this macro, don't expand it again. */
1362 1.1 christos if (! currently_rescanning (no_loop, id.c_str ()))
1363 1.9 christos {
1364 1.8 christos /* Does this identifier have a macro definition in scope? */
1365 1.1 christos macro_definition *def = standard_macro_lookup (id.c_str (), scope);
1366 1.1 christos
1367 1.1 christos if (def && expand (id.c_str (), def, dest, src_rest, no_loop, scope))
1368 1.1 christos return 1;
1369 1.1 christos }
1370 1.1 christos }
1371 1.1 christos
1372 1.1 christos return 0;
1373 1.1 christos }
1374 1.1 christos
1375 1.1 christos
1376 1.1 christos /* Expand macro references in SRC, appending the results to DEST.
1377 1.1 christos Assume we are re-scanning the result of expanding the macros named
1378 1.1 christos in NO_LOOP, and don't try to re-expand references to them.
1379 1.1 christos
1380 1.1 christos SRC must be a shared buffer; DEST must not be one. */
1381 1.9 christos static void
1382 1.1 christos scan (struct macro_buffer *dest,
1383 1.1 christos struct macro_buffer *src,
1384 1.1 christos struct macro_name_list *no_loop,
1385 1.1 christos const macro_scope &scope)
1386 1.1 christos {
1387 1.1 christos gdb_assert (src->shared);
1388 1.1 christos gdb_assert (! dest->shared);
1389 1.1 christos
1390 1.1 christos for (;;)
1391 1.1 christos {
1392 1.1 christos struct macro_buffer tok;
1393 1.1 christos char *original_src_start = src->text;
1394 1.1 christos
1395 1.1 christos /* Find the next token in SRC. */
1396 1.1 christos if (! get_token (&tok, src))
1397 1.1 christos break;
1398 1.1 christos
1399 1.8 christos /* Just for aesthetics. If we skipped some whitespace, copy
1400 1.1 christos that to DEST. */
1401 1.1 christos if (tok.text > original_src_start)
1402 1.1 christos {
1403 1.9 christos dest->appendmem (original_src_start, tok.text - original_src_start);
1404 1.1 christos dest->last_token = dest->len;
1405 1.1 christos }
1406 1.1 christos
1407 1.1 christos if (! maybe_expand (dest, &tok, src, no_loop, scope))
1408 1.1 christos /* We didn't end up expanding tok as a macro reference, so
1409 1.1 christos simply append it to dest. */
1410 1.1 christos append_tokens_without_splicing (dest, &tok);
1411 1.1 christos }
1412 1.1 christos
1413 1.8 christos /* Just for aesthetics. If there was any trailing whitespace in
1414 1.1 christos src, copy it to dest. */
1415 1.1 christos if (src->len)
1416 1.1 christos {
1417 1.1 christos dest->appendmem (src->text, src->len);
1418 1.1 christos dest->last_token = dest->len;
1419 1.8 christos }
1420 1.9 christos }
1421 1.1 christos
1422 1.8 christos
1423 1.1 christos gdb::unique_xmalloc_ptr<char>
1424 1.8 christos macro_expand (const char *source, const macro_scope &scope)
1425 1.1 christos {
1426 1.1 christos struct macro_buffer src (source, strlen (source));
1427 1.9 christos
1428 1.1 christos struct macro_buffer dest (0);
1429 1.8 christos dest.last_token = 0;
1430 1.1 christos
1431 1.9 christos scan (&dest, &src, 0, scope);
1432 1.1 christos
1433 1.1 christos dest.appendc ('\0');
1434 1.1 christos
1435 1.8 christos return dest.release ();
1436 1.9 christos }
1437 1.1 christos
1438 1.1 christos
1439 1.1 christos gdb::unique_xmalloc_ptr<char>
1440 1.1 christos macro_expand_once (const char *source, const macro_scope &scope)
1441 1.9 christos {
1442 1.9 christos error (_("Expand-once not implemented yet."));
1443 1.1 christos }
1444 1.8 christos
1445 1.1 christos gdb::unique_xmalloc_ptr<char>
1446 1.1 christos macro_expand_next (const char **lexptr, const macro_scope &scope)
1447 1.8 christos {
1448 1.1 christos struct macro_buffer tok;
1449 1.1 christos
1450 1.8 christos /* Set up SRC to refer to the input text, pointed to by *lexptr. */
1451 1.1 christos struct macro_buffer src (*lexptr, strlen (*lexptr));
1452 1.1 christos
1453 1.1 christos /* Set up DEST to receive the expansion, if there is one. */
1454 1.1 christos struct macro_buffer dest (0);
1455 1.9 christos dest.last_token = 0;
1456 1.1 christos
1457 1.1 christos /* Get the text's first preprocessing token. */
1458 1.9 christos if (! get_token (&tok, &src))
1459 1.1 christos return nullptr;
1460 1.1 christos
1461 1.1 christos /* If it's a macro invocation, expand it. */
1462 1.1 christos if (maybe_expand (&dest, &tok, &src, 0, scope))
1463 1.8 christos {
1464 1.1 christos /* It was a macro invocation! Package up the expansion as a
1465 1.8 christos null-terminated string and return it. Set *lexptr to the
1466 1.1 christos start of the next token in the input. */
1467 1.1 christos dest.appendc ('\0');
1468 1.1 christos *lexptr = src.text;
1469 1.1 christos return dest.release ();
1470 1.9 christos }
1471 1.1 christos else
1472 1.1 christos {
1473 /* It wasn't a macro invocation. */
1474 return nullptr;
1475 }
1476 }
1477