Bug Summary

File:src/gnu/usr.bin/binutils/gdb/macroexp.c
Warning:line 741, column 17
Value stored to 'start' during its initialization is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name macroexp.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/binutils/obj/gdb -resource-dir /usr/local/lib/clang/13.0.0 -D PIE_DEFAULT=1 -I . -I /usr/src/gnu/usr.bin/binutils/gdb -I /usr/src/gnu/usr.bin/binutils/gdb/config -D LOCALEDIR="/usr/share/locale" -D HAVE_CONFIG_H -I /usr/src/gnu/usr.bin/binutils/gdb/../include/opcode -I ../bfd -I /usr/src/gnu/usr.bin/binutils/gdb/../bfd -I /usr/src/gnu/usr.bin/binutils/gdb/../include -I ../intl -I /usr/src/gnu/usr.bin/binutils/gdb/../intl -D MI_OUT=1 -D TUI=1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/gnu/usr.bin/binutils/obj/gdb -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -fcommon -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/gnu/usr.bin/binutils/gdb/macroexp.c
1/* C preprocessor macro expansion for GDB.
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "gdb_obstack.h"
24#include "bcache.h"
25#include "macrotab.h"
26#include "macroexp.h"
27#include "gdb_assert.h"
28
29
30
31/* A resizeable, substringable string type. */
32
33
34/* A string type that we can resize, quickly append to, and use to
35 refer to substrings of other strings. */
36struct macro_buffer
37{
38 /* An array of characters. The first LEN bytes are the real text,
39 but there are SIZE bytes allocated to the array. If SIZE is
40 zero, then this doesn't point to a malloc'ed block. If SHARED is
41 non-zero, then this buffer is actually a pointer into some larger
42 string, and we shouldn't append characters to it, etc. Because
43 of sharing, we can't assume in general that the text is
44 null-terminated. */
45 char *text;
46
47 /* The number of characters in the string. */
48 int len;
49
50 /* The number of characters allocated to the string. If SHARED is
51 non-zero, this is meaningless; in this case, we set it to zero so
52 that any "do we have room to append something?" tests will fail,
53 so we don't always have to check SHARED before using this field. */
54 int size;
55
56 /* Zero if TEXT can be safely realloc'ed (i.e., it's its own malloc
57 block). Non-zero if TEXT is actually pointing into the middle of
58 some other block, and we shouldn't reallocate it. */
59 int shared;
60
61 /* For detecting token splicing.
62
63 This is the index in TEXT of the first character of the token
64 that abuts the end of TEXT. If TEXT contains no tokens, then we
65 set this equal to LEN. If TEXT ends in whitespace, then there is
66 no token abutting the end of TEXT (it's just whitespace), and
67 again, we set this equal to LEN. We set this to -1 if we don't
68 know the nature of TEXT. */
69 int last_token;
70
71 /* If this buffer is holding the result from get_token, then this
72 is non-zero if it is an identifier token, zero otherwise. */
73 int is_identifier;
74};
75
76
77/* Set the macro buffer *B to the empty string, guessing that its
78 final contents will fit in N bytes. (It'll get resized if it
79 doesn't, so the guess doesn't have to be right.) Allocate the
80 initial storage with xmalloc. */
81static void
82init_buffer (struct macro_buffer *b, int n)
83{
84 /* Small value for initial testing. */
85 n = 1;
86
87 b->size = n;
88 if (n > 0)
89 b->text = (char *) xmalloc (n);
90 else
91 b->text = NULL((void*)0);
92 b->len = 0;
93 b->shared = 0;
94 b->last_token = -1;
95}
96
97
98/* Set the macro buffer *BUF to refer to the LEN bytes at ADDR, as a
99 shared substring. */
100static void
101init_shared_buffer (struct macro_buffer *buf, char *addr, int len)
102{
103 buf->text = addr;
104 buf->len = len;
105 buf->shared = 1;
106 buf->size = 0;
107 buf->last_token = -1;
108}
109
110
111/* Free the text of the buffer B. Raise an error if B is shared. */
112static void
113free_buffer (struct macro_buffer *b)
114{
115 gdb_assert (! b->shared)((void) ((! b->shared) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 115, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "! b->shared"
), 0)))
;
116 if (b->size)
117 xfree (b->text);
118}
119
120
121/* A cleanup function for macro buffers. */
122static void
123cleanup_macro_buffer (void *untyped_buf)
124{
125 free_buffer ((struct macro_buffer *) untyped_buf);
126}
127
128
129/* Resize the buffer B to be at least N bytes long. Raise an error if
130 B shouldn't be resized. */
131static void
132resize_buffer (struct macro_buffer *b, int n)
133{
134 /* We shouldn't be trying to resize shared strings. */
135 gdb_assert (! b->shared)((void) ((! b->shared) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 135, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "! b->shared"
), 0)))
;
136
137 if (b->size == 0)
138 b->size = n;
139 else
140 while (b->size <= n)
141 b->size *= 2;
142
143 b->text = xrealloc (b->text, b->size);
144}
145
146
147/* Append the character C to the buffer B. */
148static void
149appendc (struct macro_buffer *b, int c)
150{
151 int new_len = b->len + 1;
152
153 if (new_len > b->size)
154 resize_buffer (b, new_len);
155
156 b->text[b->len] = c;
157 b->len = new_len;
158}
159
160
161/* Append the LEN bytes at ADDR to the buffer B. */
162static void
163appendmem (struct macro_buffer *b, char *addr, int len)
164{
165 int new_len = b->len + len;
166
167 if (new_len > b->size)
168 resize_buffer (b, new_len);
169
170 memcpy (b->text + b->len, addr, len);
171 b->len = new_len;
172}
173
174
175
176/* Recognizing preprocessor tokens. */
177
178
179static int
180is_whitespace (int c)
181{
182 return (c == ' '
183 || c == '\t'
184 || c == '\n'
185 || c == '\v'
186 || c == '\f');
187}
188
189
190static int
191is_digit (int c)
192{
193 return ('0' <= c && c <= '9');
194}
195
196
197static int
198is_identifier_nondigit (int c)
199{
200 return (c == '_'
201 || ('a' <= c && c <= 'z')
202 || ('A' <= c && c <= 'Z'));
203}
204
205
206static void
207set_token (struct macro_buffer *tok, char *start, char *end)
208{
209 init_shared_buffer (tok, start, end - start);
210 tok->last_token = 0;
211
212 /* Presumed; get_identifier may overwrite this. */
213 tok->is_identifier = 0;
214}
215
216
217static int
218get_comment (struct macro_buffer *tok, char *p, char *end)
219{
220 if (p + 2 > end)
221 return 0;
222 else if (p[0] == '/'
223 && p[1] == '*')
224 {
225 char *tok_start = p;
226
227 p += 2;
228
229 for (; p < end; p++)
230 if (p + 2 <= end
231 && p[0] == '*'
232 && p[1] == '/')
233 {
234 p += 2;
235 set_token (tok, tok_start, p);
236 return 1;
237 }
238
239 error ("Unterminated comment in macro expansion.");
240 }
241 else if (p[0] == '/'
242 && p[1] == '/')
243 {
244 char *tok_start = p;
245
246 p += 2;
247 for (; p < end; p++)
248 if (*p == '\n')
249 break;
250
251 set_token (tok, tok_start, p);
252 return 1;
253 }
254 else
255 return 0;
256}
257
258
259static int
260get_identifier (struct macro_buffer *tok, char *p, char *end)
261{
262 if (p < end
263 && is_identifier_nondigit (*p))
264 {
265 char *tok_start = p;
266
267 while (p < end
268 && (is_identifier_nondigit (*p)
269 || is_digit (*p)))
270 p++;
271
272 set_token (tok, tok_start, p);
273 tok->is_identifier = 1;
274 return 1;
275 }
276 else
277 return 0;
278}
279
280
281static int
282get_pp_number (struct macro_buffer *tok, char *p, char *end)
283{
284 if (p < end
285 && (is_digit (*p)
286 || *p == '.'))
287 {
288 char *tok_start = p;
289
290 while (p < end)
291 {
292 if (is_digit (*p)
293 || is_identifier_nondigit (*p)
294 || *p == '.')
295 p++;
296 else if (p + 2 <= end
297 && strchr ("eEpP.", *p)
298 && (p[1] == '+' || p[1] == '-'))
299 p += 2;
300 else
301 break;
302 }
303
304 set_token (tok, tok_start, p);
305 return 1;
306 }
307 else
308 return 0;
309}
310
311
312
313/* If the text starting at P going up to (but not including) END
314 starts with a character constant, set *TOK to point to that
315 character constant, and return 1. Otherwise, return zero.
316 Signal an error if it contains a malformed or incomplete character
317 constant. */
318static int
319get_character_constant (struct macro_buffer *tok, char *p, char *end)
320{
321 /* ISO/IEC 9899:1999 (E) Section 6.4.4.4 paragraph 1
322 But of course, what really matters is that we handle it the same
323 way GDB's C/C++ lexer does. So we call parse_escape in utils.c
324 to handle escape sequences. */
325 if ((p + 1 <= end && *p == '\'')
326 || (p + 2 <= end && p[0] == 'L' && p[1] == '\''))
327 {
328 char *tok_start = p;
329 char *body_start;
330
331 if (*p == '\'')
332 p++;
333 else if (*p == 'L')
334 p += 2;
335 else
336 gdb_assert (0)((void) ((0) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 336, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "0"
), 0)))
;
337
338 body_start = p;
339 for (;;)
340 {
341 if (p >= end)
342 error ("Unmatched single quote.");
343 else if (*p == '\'')
344 {
345 if (p == body_start)
346 error ("A character constant must contain at least one "
347 "character.");
348 p++;
349 break;
350 }
351 else if (*p == '\\')
352 {
353 p++;
354 parse_escape (&p);
355 }
356 else
357 p++;
358 }
359
360 set_token (tok, tok_start, p);
361 return 1;
362 }
363 else
364 return 0;
365}
366
367
368/* If the text starting at P going up to (but not including) END
369 starts with a string literal, set *TOK to point to that string
370 literal, and return 1. Otherwise, return zero. Signal an error if
371 it contains a malformed or incomplete string literal. */
372static int
373get_string_literal (struct macro_buffer *tok, char *p, char *end)
374{
375 if ((p + 1 <= end
376 && *p == '\"')
377 || (p + 2 <= end
378 && p[0] == 'L'
379 && p[1] == '\"'))
380 {
381 char *tok_start = p;
382
383 if (*p == '\"')
384 p++;
385 else if (*p == 'L')
386 p += 2;
387 else
388 gdb_assert (0)((void) ((0) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 388, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "0"
), 0)))
;
389
390 for (;;)
391 {
392 if (p >= end)
393 error ("Unterminated string in expression.");
394 else if (*p == '\"')
395 {
396 p++;
397 break;
398 }
399 else if (*p == '\n')
400 error ("Newline characters may not appear in string "
401 "constants.");
402 else if (*p == '\\')
403 {
404 p++;
405 parse_escape (&p);
406 }
407 else
408 p++;
409 }
410
411 set_token (tok, tok_start, p);
412 return 1;
413 }
414 else
415 return 0;
416}
417
418
419static int
420get_punctuator (struct macro_buffer *tok, char *p, char *end)
421{
422 /* Here, speed is much less important than correctness and clarity. */
423
424 /* ISO/IEC 9899:1999 (E) Section 6.4.6 Paragraph 1 */
425 static const char * const punctuators[] = {
426 "[", "]", "(", ")", "{", "}", ".", "->",
427 "++", "--", "&", "*", "+", "-", "~", "!",
428 "/", "%", "<<", ">>", "<", ">", "<=", ">=", "==", "!=",
429 "^", "|", "&&", "||",
430 "?", ":", ";", "...",
431 "=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=",
432 ",", "#", "##",
433 "<:", ":>", "<%", "%>", "%:", "%:%:",
434 0
435 };
436
437 int i;
438
439 if (p + 1 <= end)
440 {
441 for (i = 0; punctuators[i]; i++)
442 {
443 const char *punctuator = punctuators[i];
444
445 if (p[0] == punctuator[0])
446 {
447 int len = strlen (punctuator);
448
449 if (p + len <= end
450 && ! memcmp (p, punctuator, len))
451 {
452 set_token (tok, p, p + len);
453 return 1;
454 }
455 }
456 }
457 }
458
459 return 0;
460}
461
462
463/* Peel the next preprocessor token off of SRC, and put it in TOK.
464 Mutate TOK to refer to the first token in SRC, and mutate SRC to
465 refer to the text after that token. SRC must be a shared buffer;
466 the resulting TOK will be shared, pointing into the same string SRC
467 does. Initialize TOK's last_token field. Return non-zero if we
468 succeed, or 0 if we didn't find any more tokens in SRC. */
469static int
470get_token (struct macro_buffer *tok,
471 struct macro_buffer *src)
472{
473 char *p = src->text;
474 char *end = p + src->len;
475
476 gdb_assert (src->shared)((void) ((src->shared) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 476, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "src->shared"
), 0)))
;
477
478 /* From the ISO C standard, ISO/IEC 9899:1999 (E), section 6.4:
479
480 preprocessing-token:
481 header-name
482 identifier
483 pp-number
484 character-constant
485 string-literal
486 punctuator
487 each non-white-space character that cannot be one of the above
488
489 We don't have to deal with header-name tokens, since those can
490 only occur after a #include, which we will never see. */
491
492 while (p < end)
493 if (is_whitespace (*p))
494 p++;
495 else if (get_comment (tok, p, end))
496 p += tok->len;
497 else if (get_pp_number (tok, p, end)
498 || get_character_constant (tok, p, end)
499 || get_string_literal (tok, p, end)
500 /* Note: the grammar in the standard seems to be
501 ambiguous: L'x' can be either a wide character
502 constant, or an identifier followed by a normal
503 character constant. By trying `get_identifier' after
504 we try get_character_constant and get_string_literal,
505 we give the wide character syntax precedence. Now,
506 since GDB doesn't handle wide character constants
507 anyway, is this the right thing to do? */
508 || get_identifier (tok, p, end)
509 || get_punctuator (tok, p, end))
510 {
511 /* How many characters did we consume, including whitespace? */
512 int consumed = p - src->text + tok->len;
513 src->text += consumed;
514 src->len -= consumed;
515 return 1;
516 }
517 else
518 {
519 /* We have found a "non-whitespace character that cannot be
520 one of the above." Make a token out of it. */
521 int consumed;
522
523 set_token (tok, p, p + 1);
524 consumed = p - src->text + tok->len;
525 src->text += consumed;
526 src->len -= consumed;
527 return 1;
528 }
529
530 return 0;
531}
532
533
534
535/* Appending token strings, with and without splicing */
536
537
538/* Append the macro buffer SRC to the end of DEST, and ensure that
539 doing so doesn't splice the token at the end of SRC with the token
540 at the beginning of DEST. SRC and DEST must have their last_token
541 fields set. Upon return, DEST's last_token field is set correctly.
542
543 For example:
544
545 If DEST is "(" and SRC is "y", then we can return with
546 DEST set to "(y" --- we've simply appended the two buffers.
547
548 However, if DEST is "x" and SRC is "y", then we must not return
549 with DEST set to "xy" --- that would splice the two tokens "x" and
550 "y" together to make a single token "xy". However, it would be
551 fine to return with DEST set to "x y". Similarly, "<" and "<" must
552 yield "< <", not "<<", etc. */
553static void
554append_tokens_without_splicing (struct macro_buffer *dest,
555 struct macro_buffer *src)
556{
557 int original_dest_len = dest->len;
558 struct macro_buffer dest_tail, new_token;
559
560 gdb_assert (src->last_token != -1)((void) ((src->last_token != -1) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 560, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "src->last_token != -1"
), 0)))
;
561 gdb_assert (dest->last_token != -1)((void) ((dest->last_token != -1) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 561, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "dest->last_token != -1"
), 0)))
;
562
563 /* First, just try appending the two, and call get_token to see if
564 we got a splice. */
565 appendmem (dest, src->text, src->len);
566
567 /* If DEST originally had no token abutting its end, then we can't
568 have spliced anything, so we're done. */
569 if (dest->last_token == original_dest_len)
570 {
571 dest->last_token = original_dest_len + src->last_token;
572 return;
573 }
574
575 /* Set DEST_TAIL to point to the last token in DEST, followed by
576 all the stuff we just appended. */
577 init_shared_buffer (&dest_tail,
578 dest->text + dest->last_token,
579 dest->len - dest->last_token);
580
581 /* Re-parse DEST's last token. We know that DEST used to contain
582 at least one token, so if it doesn't contain any after the
583 append, then we must have spliced "/" and "*" or "/" and "/" to
584 make a comment start. (Just for the record, I got this right
585 the first time. This is not a bug fix.) */
586 if (get_token (&new_token, &dest_tail)
587 && (new_token.text + new_token.len
588 == dest->text + original_dest_len))
589 {
590 /* No splice, so we're done. */
591 dest->last_token = original_dest_len + src->last_token;
592 return;
593 }
594
595 /* Okay, a simple append caused a splice. Let's chop dest back to
596 its original length and try again, but separate the texts with a
597 space. */
598 dest->len = original_dest_len;
599 appendc (dest, ' ');
600 appendmem (dest, src->text, src->len);
601
602 init_shared_buffer (&dest_tail,
603 dest->text + dest->last_token,
604 dest->len - dest->last_token);
605
606 /* Try to re-parse DEST's last token, as above. */
607 if (get_token (&new_token, &dest_tail)
608 && (new_token.text + new_token.len
609 == dest->text + original_dest_len))
610 {
611 /* No splice, so we're done. */
612 dest->last_token = original_dest_len + 1 + src->last_token;
613 return;
614 }
615
616 /* As far as I know, there's no case where inserting a space isn't
617 enough to prevent a splice. */
618 internal_error (__FILE__"/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c", __LINE__618,
619 "unable to avoid splicing tokens during macro expansion");
620}
621
622
623
624/* Expanding macros! */
625
626
627/* A singly-linked list of the names of the macros we are currently
628 expanding --- for detecting expansion loops. */
629struct macro_name_list {
630 const char *name;
631 struct macro_name_list *next;
632};
633
634
635/* Return non-zero if we are currently expanding the macro named NAME,
636 according to LIST; otherwise, return zero.
637
638 You know, it would be possible to get rid of all the NO_LOOP
639 arguments to these functions by simply generating a new lookup
640 function and baton which refuses to find the definition for a
641 particular macro, and otherwise delegates the decision to another
642 function/baton pair. But that makes the linked list of excluded
643 macros chained through untyped baton pointers, which will make it
644 harder to debug. :( */
645static int
646currently_rescanning (struct macro_name_list *list, const char *name)
647{
648 for (; list; list = list->next)
649 if (strcmp (name, list->name) == 0)
650 return 1;
651
652 return 0;
653}
654
655
656/* Gather the arguments to a macro expansion.
657
658 NAME is the name of the macro being invoked. (It's only used for
659 printing error messages.)
660
661 Assume that SRC is the text of the macro invocation immediately
662 following the macro name. For example, if we're processing the
663 text foo(bar, baz), then NAME would be foo and SRC will be (bar,
664 baz).
665
666 If SRC doesn't start with an open paren ( token at all, return
667 zero, leave SRC unchanged, and don't set *ARGC_P to anything.
668
669 If SRC doesn't contain a properly terminated argument list, then
670 raise an error.
671
672 Otherwise, return a pointer to the first element of an array of
673 macro buffers referring to the argument texts, and set *ARGC_P to
674 the number of arguments we found --- the number of elements in the
675 array. The macro buffers share their text with SRC, and their
676 last_token fields are initialized. The array is allocated with
677 xmalloc, and the caller is responsible for freeing it.
678
679 NOTE WELL: if SRC starts with a open paren ( token followed
680 immediately by a close paren ) token (e.g., the invocation looks
681 like "foo()"), we treat that as one argument, which happens to be
682 the empty list of tokens. The caller should keep in mind that such
683 a sequence of tokens is a valid way to invoke one-parameter
684 function-like macros, but also a valid way to invoke zero-parameter
685 function-like macros. Eeew.
686
687 Consume the tokens from SRC; after this call, SRC contains the text
688 following the invocation. */
689
690static struct macro_buffer *
691gather_arguments (const char *name, struct macro_buffer *src, int *argc_p)
692{
693 struct macro_buffer tok;
694 int args_len, args_size;
695 struct macro_buffer *args = NULL((void*)0);
696 struct cleanup *back_to = make_cleanup (free_current_contents, &args);
697
698 /* Does SRC start with an opening paren token? Read from a copy of
699 SRC, so SRC itself is unaffected if we don't find an opening
700 paren. */
701 {
702 struct macro_buffer temp;
703 init_shared_buffer (&temp, src->text, src->len);
704
705 if (! get_token (&tok, &temp)
706 || tok.len != 1
707 || tok.text[0] != '(')
708 {
709 discard_cleanups (back_to);
710 return 0;
711 }
712 }
713
714 /* Consume SRC's opening paren. */
715 get_token (&tok, src);
716
717 args_len = 0;
718 args_size = 1; /* small for initial testing */
719 args = (struct macro_buffer *) xmalloc (sizeof (*args) * args_size);
720
721 for (;;)
722 {
723 struct macro_buffer *arg;
724 int depth;
725
726 /* Make sure we have room for the next argument. */
727 if (args_len >= args_size)
728 {
729 args_size *= 2;
730 args = xrealloc (args, sizeof (*args) * args_size);
731 }
732
733 /* Initialize the next argument. */
734 arg = &args[args_len++];
735 set_token (arg, src->text, src->text);
736
737 /* Gather the argument's tokens. */
738 depth = 0;
739 for (;;)
740 {
741 char *start = src->text;
Value stored to 'start' during its initialization is never read
742
743 if (! get_token (&tok, src))
744 error ("Malformed argument list for macro `%s'.", name);
745
746 /* Is tok an opening paren? */
747 if (tok.len == 1 && tok.text[0] == '(')
748 depth++;
749
750 /* Is tok is a closing paren? */
751 else if (tok.len == 1 && tok.text[0] == ')')
752 {
753 /* If it's a closing paren at the top level, then that's
754 the end of the argument list. */
755 if (depth == 0)
756 {
757 discard_cleanups (back_to);
758 *argc_p = args_len;
759 return args;
760 }
761
762 depth--;
763 }
764
765 /* If tok is a comma at top level, then that's the end of
766 the current argument. */
767 else if (tok.len == 1 && tok.text[0] == ',' && depth == 0)
768 break;
769
770 /* Extend the current argument to enclose this token. If
771 this is the current argument's first token, leave out any
772 leading whitespace, just for aesthetics. */
773 if (arg->len == 0)
774 {
775 arg->text = tok.text;
776 arg->len = tok.len;
777 arg->last_token = 0;
778 }
779 else
780 {
781 arg->len = (tok.text + tok.len) - arg->text;
782 arg->last_token = tok.text - arg->text;
783 }
784 }
785 }
786}
787
788
789/* The `expand' and `substitute_args' functions both invoke `scan'
790 recursively, so we need a forward declaration somewhere. */
791static void scan (struct macro_buffer *dest,
792 struct macro_buffer *src,
793 struct macro_name_list *no_loop,
794 macro_lookup_ftype *lookup_func,
795 void *lookup_baton);
796
797
798/* Given the macro definition DEF, being invoked with the actual
799 arguments given by ARGC and ARGV, substitute the arguments into the
800 replacement list, and store the result in DEST.
801
802 If it is necessary to expand macro invocations in one of the
803 arguments, use LOOKUP_FUNC and LOOKUP_BATON to find the macro
804 definitions, and don't expand invocations of the macros listed in
805 NO_LOOP. */
806static void
807substitute_args (struct macro_buffer *dest,
808 struct macro_definition *def,
809 int argc, struct macro_buffer *argv,
810 struct macro_name_list *no_loop,
811 macro_lookup_ftype *lookup_func,
812 void *lookup_baton)
813{
814 /* A macro buffer for the macro's replacement list. */
815 struct macro_buffer replacement_list;
816
817 init_shared_buffer (&replacement_list, (char *) def->replacement,
818 strlen (def->replacement));
819
820 gdb_assert (dest->len == 0)((void) ((dest->len == 0) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 820, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "dest->len == 0"
), 0)))
;
821 dest->last_token = 0;
822
823 for (;;)
824 {
825 struct macro_buffer tok;
826 char *original_rl_start = replacement_list.text;
827 int substituted = 0;
828
829 /* Find the next token in the replacement list. */
830 if (! get_token (&tok, &replacement_list))
831 break;
832
833 /* Just for aesthetics. If we skipped some whitespace, copy
834 that to DEST. */
835 if (tok.text > original_rl_start)
836 {
837 appendmem (dest, original_rl_start, tok.text - original_rl_start);
838 dest->last_token = dest->len;
839 }
840
841 /* Is this token the stringification operator? */
842 if (tok.len == 1
843 && tok.text[0] == '#')
844 error ("Stringification is not implemented yet.");
845
846 /* Is this token the splicing operator? */
847 if (tok.len == 2
848 && tok.text[0] == '#'
849 && tok.text[1] == '#')
850 error ("Token splicing is not implemented yet.");
851
852 /* Is this token an identifier? */
853 if (tok.is_identifier)
854 {
855 int i;
856
857 /* Is it the magic varargs parameter? */
858 if (tok.len == 11
859 && ! memcmp (tok.text, "__VA_ARGS__", 11))
860 error ("Variable-arity macros not implemented yet.");
861
862 /* Is it one of the parameters? */
863 for (i = 0; i < def->argc; i++)
864 if (tok.len == strlen (def->argv[i])
865 && ! memcmp (tok.text, def->argv[i], tok.len))
866 {
867 struct macro_buffer arg_src;
868
869 /* Expand any macro invocations in the argument text,
870 and append the result to dest. Remember that scan
871 mutates its source, so we need to scan a new buffer
872 referring to the argument's text, not the argument
873 itself. */
874 init_shared_buffer (&arg_src, argv[i].text, argv[i].len);
875 scan (dest, &arg_src, no_loop, lookup_func, lookup_baton);
876 substituted = 1;
877 break;
878 }
879 }
880
881 /* If it wasn't a parameter, then just copy it across. */
882 if (! substituted)
883 append_tokens_without_splicing (dest, &tok);
884 }
885}
886
887
888/* Expand a call to a macro named ID, whose definition is DEF. Append
889 its expansion to DEST. SRC is the input text following the ID
890 token. We are currently rescanning the expansions of the macros
891 named in NO_LOOP; don't re-expand them. Use LOOKUP_FUNC and
892 LOOKUP_BATON to find definitions for any nested macro references.
893
894 Return 1 if we decided to expand it, zero otherwise. (If it's a
895 function-like macro name that isn't followed by an argument list,
896 we don't expand it.) If we return zero, leave SRC unchanged. */
897static int
898expand (const char *id,
899 struct macro_definition *def,
900 struct macro_buffer *dest,
901 struct macro_buffer *src,
902 struct macro_name_list *no_loop,
903 macro_lookup_ftype *lookup_func,
904 void *lookup_baton)
905{
906 struct macro_name_list new_no_loop;
907
908 /* Create a new node to be added to the front of the no-expand list.
909 This list is appropriate for re-scanning replacement lists, but
910 it is *not* appropriate for scanning macro arguments; invocations
911 of the macro whose arguments we are gathering *do* get expanded
912 there. */
913 new_no_loop.name = id;
914 new_no_loop.next = no_loop;
915
916 /* What kind of macro are we expanding? */
917 if (def->kind == macro_object_like)
918 {
919 struct macro_buffer replacement_list;
920
921 init_shared_buffer (&replacement_list, (char *) def->replacement,
922 strlen (def->replacement));
923
924 scan (dest, &replacement_list, &new_no_loop, lookup_func, lookup_baton);
925 return 1;
926 }
927 else if (def->kind == macro_function_like)
928 {
929 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
930 int argc;
931 struct macro_buffer *argv = NULL((void*)0);
932 struct macro_buffer substituted;
933 struct macro_buffer substituted_src;
934
935 if (def->argc >= 1
936 && strcmp (def->argv[def->argc - 1], "...") == 0)
937 error ("Varargs macros not implemented yet.");
938
939 make_cleanup (free_current_contents, &argv);
940 argv = gather_arguments (id, src, &argc);
941
942 /* If we couldn't find any argument list, then we don't expand
943 this macro. */
944 if (! argv)
945 {
946 do_cleanups (back_to);
947 return 0;
948 }
949
950 /* Check that we're passing an acceptable number of arguments for
951 this macro. */
952 if (argc != def->argc)
953 {
954 /* Remember that a sequence of tokens like "foo()" is a
955 valid invocation of a macro expecting either zero or one
956 arguments. */
957 if (! (argc == 1
958 && argv[0].len == 0
959 && def->argc == 0))
960 error ("Wrong number of arguments to macro `%s' "
961 "(expected %d, got %d).",
962 id, def->argc, argc);
963 }
964
965 /* Note that we don't expand macro invocations in the arguments
966 yet --- we let subst_args take care of that. Parameters that
967 appear as operands of the stringifying operator "#" or the
968 splicing operator "##" don't get macro references expanded,
969 so we can't really tell whether it's appropriate to macro-
970 expand an argument until we see how it's being used. */
971 init_buffer (&substituted, 0);
972 make_cleanup (cleanup_macro_buffer, &substituted);
973 substitute_args (&substituted, def, argc, argv, no_loop,
974 lookup_func, lookup_baton);
975
976 /* Now `substituted' is the macro's replacement list, with all
977 argument values substituted into it properly. Re-scan it for
978 macro references, but don't expand invocations of this macro.
979
980 We create a new buffer, `substituted_src', which points into
981 `substituted', and scan that. We can't scan `substituted'
982 itself, since the tokenization process moves the buffer's
983 text pointer around, and we still need to be able to find
984 `substituted's original text buffer after scanning it so we
985 can free it. */
986 init_shared_buffer (&substituted_src, substituted.text, substituted.len);
987 scan (dest, &substituted_src, &new_no_loop, lookup_func, lookup_baton);
988
989 do_cleanups (back_to);
990
991 return 1;
992 }
993 else
994 internal_error (__FILE__"/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c", __LINE__994, "bad macro definition kind");
995}
996
997
998/* If the single token in SRC_FIRST followed by the tokens in SRC_REST
999 constitute a macro invokation not forbidden in NO_LOOP, append its
1000 expansion to DEST and return non-zero. Otherwise, return zero, and
1001 leave DEST unchanged.
1002
1003 SRC_FIRST and SRC_REST must be shared buffers; DEST must not be one.
1004 SRC_FIRST must be a string built by get_token. */
1005static int
1006maybe_expand (struct macro_buffer *dest,
1007 struct macro_buffer *src_first,
1008 struct macro_buffer *src_rest,
1009 struct macro_name_list *no_loop,
1010 macro_lookup_ftype *lookup_func,
1011 void *lookup_baton)
1012{
1013 gdb_assert (src_first->shared)((void) ((src_first->shared) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 1013, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "src_first->shared"
), 0)))
;
1014 gdb_assert (src_rest->shared)((void) ((src_rest->shared) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 1014, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "src_rest->shared"
), 0)))
;
1015 gdb_assert (! dest->shared)((void) ((! dest->shared) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 1015, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "! dest->shared"
), 0)))
;
1016
1017 /* Is this token an identifier? */
1018 if (src_first->is_identifier)
1019 {
1020 /* Make a null-terminated copy of it, since that's what our
1021 lookup function expects. */
1022 char *id = xmalloc (src_first->len + 1);
1023 struct cleanup *back_to = make_cleanup (xfree, id);
1024 memcpy (id, src_first->text, src_first->len);
1025 id[src_first->len] = 0;
1026
1027 /* If we're currently re-scanning the result of expanding
1028 this macro, don't expand it again. */
1029 if (! currently_rescanning (no_loop, id))
1030 {
1031 /* Does this identifier have a macro definition in scope? */
1032 struct macro_definition *def = lookup_func (id, lookup_baton);
1033
1034 if (def && expand (id, def, dest, src_rest, no_loop,
1035 lookup_func, lookup_baton))
1036 {
1037 do_cleanups (back_to);
1038 return 1;
1039 }
1040 }
1041
1042 do_cleanups (back_to);
1043 }
1044
1045 return 0;
1046}
1047
1048
1049/* Expand macro references in SRC, appending the results to DEST.
1050 Assume we are re-scanning the result of expanding the macros named
1051 in NO_LOOP, and don't try to re-expand references to them.
1052
1053 SRC must be a shared buffer; DEST must not be one. */
1054static void
1055scan (struct macro_buffer *dest,
1056 struct macro_buffer *src,
1057 struct macro_name_list *no_loop,
1058 macro_lookup_ftype *lookup_func,
1059 void *lookup_baton)
1060{
1061 gdb_assert (src->shared)((void) ((src->shared) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 1061, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "src->shared"
), 0)))
;
1062 gdb_assert (! dest->shared)((void) ((! dest->shared) ? 0 : (internal_error ("/usr/src/gnu/usr.bin/binutils/gdb/macroexp.c"
, 1062, "%s: Assertion `%s' failed.", __PRETTY_FUNCTION__, "! dest->shared"
), 0)))
;
1063
1064 for (;;)
1065 {
1066 struct macro_buffer tok;
1067 char *original_src_start = src->text;
1068
1069 /* Find the next token in SRC. */
1070 if (! get_token (&tok, src))
1071 break;
1072
1073 /* Just for aesthetics. If we skipped some whitespace, copy
1074 that to DEST. */
1075 if (tok.text > original_src_start)
1076 {
1077 appendmem (dest, original_src_start, tok.text - original_src_start);
1078 dest->last_token = dest->len;
1079 }
1080
1081 if (! maybe_expand (dest, &tok, src, no_loop, lookup_func, lookup_baton))
1082 /* We didn't end up expanding tok as a macro reference, so
1083 simply append it to dest. */
1084 append_tokens_without_splicing (dest, &tok);
1085 }
1086
1087 /* Just for aesthetics. If there was any trailing whitespace in
1088 src, copy it to dest. */
1089 if (src->len)
1090 {
1091 appendmem (dest, src->text, src->len);
1092 dest->last_token = dest->len;
1093 }
1094}
1095
1096
1097char *
1098macro_expand (const char *source,
1099 macro_lookup_ftype *lookup_func,
1100 void *lookup_func_baton)
1101{
1102 struct macro_buffer src, dest;
1103 struct cleanup *back_to;
1104
1105 init_shared_buffer (&src, (char *) source, strlen (source));
1106
1107 init_buffer (&dest, 0);
1108 dest.last_token = 0;
1109 back_to = make_cleanup (cleanup_macro_buffer, &dest);
1110
1111 scan (&dest, &src, 0, lookup_func, lookup_func_baton);
1112
1113 appendc (&dest, '\0');
1114
1115 discard_cleanups (back_to);
1116 return dest.text;
1117}
1118
1119
1120char *
1121macro_expand_once (const char *source,
1122 macro_lookup_ftype *lookup_func,
1123 void *lookup_func_baton)
1124{
1125 error ("Expand-once not implemented yet.");
1126}
1127
1128
1129char *
1130macro_expand_next (char **lexptr,
1131 macro_lookup_ftype *lookup_func,
1132 void *lookup_baton)
1133{
1134 struct macro_buffer src, dest, tok;
1135 struct cleanup *back_to;
1136
1137 /* Set up SRC to refer to the input text, pointed to by *lexptr. */
1138 init_shared_buffer (&src, *lexptr, strlen (*lexptr));
1139
1140 /* Set up DEST to receive the expansion, if there is one. */
1141 init_buffer (&dest, 0);
1142 dest.last_token = 0;
1143 back_to = make_cleanup (cleanup_macro_buffer, &dest);
1144
1145 /* Get the text's first preprocessing token. */
1146 if (! get_token (&tok, &src))
1147 {
1148 do_cleanups (back_to);
1149 return 0;
1150 }
1151
1152 /* If it's a macro invocation, expand it. */
1153 if (maybe_expand (&dest, &tok, &src, 0, lookup_func, lookup_baton))
1154 {
1155 /* It was a macro invocation! Package up the expansion as a
1156 null-terminated string and return it. Set *lexptr to the
1157 start of the next token in the input. */
1158 appendc (&dest, '\0');
1159 discard_cleanups (back_to);
1160 *lexptr = src.text;
1161 return dest.text;
1162 }
1163 else
1164 {
1165 /* It wasn't a macro invocation. */
1166 do_cleanups (back_to);
1167 return 0;
1168 }
1169}