1 /* C preprocessor macro expansion for GDB.
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
5 This file is part of GDB.
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.
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.
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. */
23 #include "gdb_obstack.h"
27 #include "gdb_assert.h"
31 /* A resizeable, substringable string type. */
34 /* A string type that we can resize, quickly append to, and use to
35 refer to substrings of other strings. */
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
47 /* The number of characters in the string. */
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. */
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. */
61 /* For detecting token splicing.
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. */
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. */
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. */
82 init_buffer (struct macro_buffer *b, int n)
84 /* Small value for initial testing. */
89 b->text = (char *) xmalloc (n);
98 /* Set the macro buffer *BUF to refer to the LEN bytes at ADDR, as a
101 init_shared_buffer (struct macro_buffer *buf, char *addr, int len)
107 buf->last_token = -1;
111 /* Free the text of the buffer B. Raise an error if B is shared. */
113 free_buffer (struct macro_buffer *b)
115 gdb_assert (! b->shared);
121 /* A cleanup function for macro buffers. */
123 cleanup_macro_buffer (void *untyped_buf)
125 free_buffer ((struct macro_buffer *) untyped_buf);
129 /* Resize the buffer B to be at least N bytes long. Raise an error if
130 B shouldn't be resized. */
132 resize_buffer (struct macro_buffer *b, int n)
134 /* We shouldn't be trying to resize shared strings. */
135 gdb_assert (! b->shared);
143 b->text = xrealloc (b->text, b->size);
147 /* Append the character C to the buffer B. */
149 appendc (struct macro_buffer *b, int c)
151 int new_len = b->len + 1;
153 if (new_len > b->size)
154 resize_buffer (b, new_len);
161 /* Append the LEN bytes at ADDR to the buffer B. */
163 appendmem (struct macro_buffer *b, char *addr, int len)
165 int new_len = b->len + len;
167 if (new_len > b->size)
168 resize_buffer (b, new_len);
170 memcpy (b->text + b->len, addr, len);
176 /* Recognizing preprocessor tokens. */
180 is_whitespace (int c)
193 return ('0' <= c && c <= '9');
198 is_identifier_nondigit (int c)
201 || ('a' <= c && c <= 'z')
202 || ('A' <= c && c <= 'Z'));
207 set_token (struct macro_buffer *tok, char *start, char *end)
209 init_shared_buffer (tok, start, end - start);
212 /* Presumed; get_identifier may overwrite this. */
213 tok->is_identifier = 0;
218 get_comment (struct macro_buffer *tok, char *p, char *end)
235 set_token (tok, tok_start, p);
239 error ("Unterminated comment in macro expansion.");
251 set_token (tok, tok_start, p);
260 get_identifier (struct macro_buffer *tok, char *p, char *end)
263 && is_identifier_nondigit (*p))
268 && (is_identifier_nondigit (*p)
272 set_token (tok, tok_start, p);
273 tok->is_identifier = 1;
282 get_pp_number (struct macro_buffer *tok, char *p, char *end)
293 || is_identifier_nondigit (*p)
296 else if (p + 2 <= end
297 && strchr ("eEpP.", *p)
298 && (p[1] == '+' || p[1] == '-'))
304 set_token (tok, tok_start, p);
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
319 get_character_constant (struct macro_buffer *tok, char *p, char *end)
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] == '\''))
342 error ("Unmatched single quote.");
346 error ("A character constant must contain at least one "
360 set_token (tok, tok_start, p);
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. */
373 get_string_literal (struct macro_buffer *tok, char *p, char *end)
393 error ("Unterminated string in expression.");
400 error ("Newline characters may not appear in string "
411 set_token (tok, tok_start, p);
420 get_punctuator (struct macro_buffer *tok, char *p, char *end)
422 /* Here, speed is much less important than correctness and clarity. */
424 /* ISO/IEC 9899:1999 (E) Section 6.4.6 Paragraph 1 */
425 static const char * const punctuators[] = {
426 "[", "]", "(", ")", "{", "}", ".", "->",
427 "++", "--", "&", "*", "+", "-", "~", "!",
428 "/", "%", "<<", ">>", "<", ">", "<=", ">=", "==", "!=",
429 "^", "|", "&&", "||",
430 "?", ":", ";", "...",
431 "=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=",
433 "<:", ":>", "<%", "%>", "%:", "%:%:",
441 for (i = 0; punctuators[i]; i++)
443 const char *punctuator = punctuators[i];
445 if (p[0] == punctuator[0])
447 int len = strlen (punctuator);
450 && ! memcmp (p, punctuator, len))
452 set_token (tok, p, p + len);
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. */
470 get_token (struct macro_buffer *tok,
471 struct macro_buffer *src)
474 char *end = p + src->len;
476 gdb_assert (src->shared);
478 /* From the ISO C standard, ISO/IEC 9899:1999 (E), section 6.4:
487 each non-white-space character that cannot be one of the above
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. */
493 if (is_whitespace (*p))
495 else if (get_comment (tok, p, end))
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))
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;
519 /* We have found a "non-whitespace character that cannot be
520 one of the above." Make a token out of it. */
523 set_token (tok, p, p + 1);
524 consumed = p - src->text + tok->len;
525 src->text += consumed;
526 src->len -= consumed;
535 /* Appending token strings, with and without splicing */
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.
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.
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. */
554 append_tokens_without_splicing (struct macro_buffer *dest,
555 struct macro_buffer *src)
557 int original_dest_len = dest->len;
558 struct macro_buffer dest_tail, new_token;
560 gdb_assert (src->last_token != -1);
561 gdb_assert (dest->last_token != -1);
563 /* First, just try appending the two, and call get_token to see if
565 appendmem (dest, src->text, src->len);
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)
571 dest->last_token = original_dest_len + src->last_token;
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);
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))
590 /* No splice, so we're done. */
591 dest->last_token = original_dest_len + src->last_token;
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
598 dest->len = original_dest_len;
600 appendmem (dest, src->text, src->len);
602 init_shared_buffer (&dest_tail,
603 dest->text + dest->last_token,
604 dest->len - dest->last_token);
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))
611 /* No splice, so we're done. */
612 dest->last_token = original_dest_len + 1 + src->last_token;
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__, __LINE__,
619 "unable to avoid splicing tokens during macro expansion");
624 /* Expanding macros! */
627 /* A singly-linked list of the names of the macros we are currently
628 expanding --- for detecting expansion loops. */
629 struct macro_name_list {
631 struct macro_name_list *next;
635 /* Return non-zero if we are currently expanding the macro named NAME,
636 according to LIST; otherwise, return zero.
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. :( */
646 currently_rescanning (struct macro_name_list *list, const char *name)
648 for (; list; list = list->next)
649 if (strcmp (name, list->name) == 0)
656 /* Gather the arguments to a macro expansion.
658 NAME is the name of the macro being invoked. (It's only used for
659 printing error messages.)
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,
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.
669 If SRC doesn't contain a properly terminated argument list, then
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.
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.
687 Consume the tokens from SRC; after this call, SRC contains the text
688 following the invocation. */
690 static struct macro_buffer *
691 gather_arguments (const char *name, struct macro_buffer *src, int *argc_p)
693 struct macro_buffer tok;
694 int args_len, args_size;
695 struct macro_buffer *args = NULL;
696 struct cleanup *back_to = make_cleanup (free_current_contents, &args);
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
702 struct macro_buffer temp;
703 init_shared_buffer (&temp, src->text, src->len);
705 if (! get_token (&tok, &temp)
707 || tok.text[0] != '(')
709 discard_cleanups (back_to);
714 /* Consume SRC's opening paren. */
715 get_token (&tok, src);
718 args_size = 1; /* small for initial testing */
719 args = (struct macro_buffer *) xmalloc (sizeof (*args) * args_size);
723 struct macro_buffer *arg;
726 /* Make sure we have room for the next argument. */
727 if (args_len >= args_size)
730 args = xrealloc (args, sizeof (*args) * args_size);
733 /* Initialize the next argument. */
734 arg = &args[args_len++];
735 set_token (arg, src->text, src->text);
737 /* Gather the argument's tokens. */
741 char *start = src->text;
743 if (! get_token (&tok, src))
744 error ("Malformed argument list for macro `%s'.", name);
746 /* Is tok an opening paren? */
747 if (tok.len == 1 && tok.text[0] == '(')
750 /* Is tok is a closing paren? */
751 else if (tok.len == 1 && tok.text[0] == ')')
753 /* If it's a closing paren at the top level, then that's
754 the end of the argument list. */
757 discard_cleanups (back_to);
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)
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. */
775 arg->text = tok.text;
781 arg->len = (tok.text + tok.len) - arg->text;
782 arg->last_token = tok.text - arg->text;
789 /* The `expand' and `substitute_args' functions both invoke `scan'
790 recursively, so we need a forward declaration somewhere. */
791 static void scan (struct macro_buffer *dest,
792 struct macro_buffer *src,
793 struct macro_name_list *no_loop,
794 macro_lookup_ftype *lookup_func,
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.
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
807 substitute_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,
814 /* A macro buffer for the macro's replacement list. */
815 struct macro_buffer replacement_list;
817 init_shared_buffer (&replacement_list, (char *) def->replacement,
818 strlen (def->replacement));
820 gdb_assert (dest->len == 0);
821 dest->last_token = 0;
825 struct macro_buffer tok;
826 char *original_rl_start = replacement_list.text;
829 /* Find the next token in the replacement list. */
830 if (! get_token (&tok, &replacement_list))
833 /* Just for aesthetics. If we skipped some whitespace, copy
835 if (tok.text > original_rl_start)
837 appendmem (dest, original_rl_start, tok.text - original_rl_start);
838 dest->last_token = dest->len;
841 /* Is this token the stringification operator? */
843 && tok.text[0] == '#')
844 error ("Stringification is not implemented yet.");
846 /* Is this token the splicing operator? */
848 && tok.text[0] == '#'
849 && tok.text[1] == '#')
850 error ("Token splicing is not implemented yet.");
852 /* Is this token an identifier? */
853 if (tok.is_identifier)
857 /* Is it the magic varargs parameter? */
859 && ! memcmp (tok.text, "__VA_ARGS__", 11))
860 error ("Variable-arity macros not implemented yet.");
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))
867 struct macro_buffer arg_src;
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
874 init_shared_buffer (&arg_src, argv[i].text, argv[i].len);
875 scan (dest, &arg_src, no_loop, lookup_func, lookup_baton);
881 /* If it wasn't a parameter, then just copy it across. */
883 append_tokens_without_splicing (dest, &tok);
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.
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. */
898 expand (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,
906 struct macro_name_list new_no_loop;
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
913 new_no_loop.name = id;
914 new_no_loop.next = no_loop;
916 /* What kind of macro are we expanding? */
917 if (def->kind == macro_object_like)
919 struct macro_buffer replacement_list;
921 init_shared_buffer (&replacement_list, (char *) def->replacement,
922 strlen (def->replacement));
924 scan (dest, &replacement_list, &new_no_loop, lookup_func, lookup_baton);
927 else if (def->kind == macro_function_like)
929 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
931 struct macro_buffer *argv = NULL;
932 struct macro_buffer substituted;
933 struct macro_buffer substituted_src;
936 && strcmp (def->argv[def->argc - 1], "...") == 0)
937 error ("Varargs macros not implemented yet.");
939 make_cleanup (free_current_contents, &argv);
940 argv = gather_arguments (id, src, &argc);
942 /* If we couldn't find any argument list, then we don't expand
946 do_cleanups (back_to);
950 /* Check that we're passing an acceptable number of arguments for
952 if (argc != def->argc)
954 /* Remember that a sequence of tokens like "foo()" is a
955 valid invocation of a macro expecting either zero or one
960 error ("Wrong number of arguments to macro `%s' "
961 "(expected %d, got %d).",
962 id, def->argc, argc);
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);
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.
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
986 init_shared_buffer (&substituted_src, substituted.text, substituted.len);
987 scan (dest, &substituted_src, &new_no_loop, lookup_func, lookup_baton);
989 do_cleanups (back_to);
994 internal_error (__FILE__, __LINE__, "bad macro definition kind");
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.
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. */
1006 maybe_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,
1013 gdb_assert (src_first->shared);
1014 gdb_assert (src_rest->shared);
1015 gdb_assert (! dest->shared);
1017 /* Is this token an identifier? */
1018 if (src_first->is_identifier)
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;
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))
1031 /* Does this identifier have a macro definition in scope? */
1032 struct macro_definition *def = lookup_func (id, lookup_baton);
1034 if (def && expand (id, def, dest, src_rest, no_loop,
1035 lookup_func, lookup_baton))
1037 do_cleanups (back_to);
1042 do_cleanups (back_to);
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.
1053 SRC must be a shared buffer; DEST must not be one. */
1055 scan (struct macro_buffer *dest,
1056 struct macro_buffer *src,
1057 struct macro_name_list *no_loop,
1058 macro_lookup_ftype *lookup_func,
1061 gdb_assert (src->shared);
1062 gdb_assert (! dest->shared);
1066 struct macro_buffer tok;
1067 char *original_src_start = src->text;
1069 /* Find the next token in SRC. */
1070 if (! get_token (&tok, src))
1073 /* Just for aesthetics. If we skipped some whitespace, copy
1075 if (tok.text > original_src_start)
1077 appendmem (dest, original_src_start, tok.text - original_src_start);
1078 dest->last_token = dest->len;
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);
1087 /* Just for aesthetics. If there was any trailing whitespace in
1088 src, copy it to dest. */
1091 appendmem (dest, src->text, src->len);
1092 dest->last_token = dest->len;
1098 macro_expand (const char *source,
1099 macro_lookup_ftype *lookup_func,
1100 void *lookup_func_baton)
1102 struct macro_buffer src, dest;
1103 struct cleanup *back_to;
1105 init_shared_buffer (&src, (char *) source, strlen (source));
1107 init_buffer (&dest, 0);
1108 dest.last_token = 0;
1109 back_to = make_cleanup (cleanup_macro_buffer, &dest);
1111 scan (&dest, &src, 0, lookup_func, lookup_func_baton);
1113 appendc (&dest, '\0');
1115 discard_cleanups (back_to);
1121 macro_expand_once (const char *source,
1122 macro_lookup_ftype *lookup_func,
1123 void *lookup_func_baton)
1125 error ("Expand-once not implemented yet.");
1130 macro_expand_next (char **lexptr,
1131 macro_lookup_ftype *lookup_func,
1134 struct macro_buffer src, dest, tok;
1135 struct cleanup *back_to;
1137 /* Set up SRC to refer to the input text, pointed to by *lexptr. */
1138 init_shared_buffer (&src, *lexptr, strlen (*lexptr));
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);
1145 /* Get the text's first preprocessing token. */
1146 if (! get_token (&tok, &src))
1148 do_cleanups (back_to);
1152 /* If it's a macro invocation, expand it. */
1153 if (maybe_expand (&dest, &tok, &src, 0, lookup_func, lookup_baton))
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);
1165 /* It wasn't a macro invocation. */
1166 do_cleanups (back_to);