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