1130803Smarcel/* C preprocessor macro expansion for GDB. 2130803Smarcel Copyright 2002 Free Software Foundation, Inc. 3130803Smarcel Contributed by Red Hat, Inc. 4130803Smarcel 5130803Smarcel This file is part of GDB. 6130803Smarcel 7130803Smarcel This program is free software; you can redistribute it and/or modify 8130803Smarcel it under the terms of the GNU General Public License as published by 9130803Smarcel the Free Software Foundation; either version 2 of the License, or 10130803Smarcel (at your option) any later version. 11130803Smarcel 12130803Smarcel This program is distributed in the hope that it will be useful, 13130803Smarcel but WITHOUT ANY WARRANTY; without even the implied warranty of 14130803Smarcel MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15130803Smarcel GNU General Public License for more details. 16130803Smarcel 17130803Smarcel You should have received a copy of the GNU General Public License 18130803Smarcel along with this program; if not, write to the Free Software 19130803Smarcel Foundation, Inc., 59 Temple Place - Suite 330, 20130803Smarcel Boston, MA 02111-1307, USA. */ 21130803Smarcel 22130803Smarcel#include "defs.h" 23130803Smarcel#include "gdb_obstack.h" 24130803Smarcel#include "bcache.h" 25130803Smarcel#include "macrotab.h" 26130803Smarcel#include "macroexp.h" 27130803Smarcel#include "gdb_assert.h" 28130803Smarcel 29130803Smarcel 30130803Smarcel 31130803Smarcel/* A resizeable, substringable string type. */ 32130803Smarcel 33130803Smarcel 34130803Smarcel/* A string type that we can resize, quickly append to, and use to 35130803Smarcel refer to substrings of other strings. */ 36130803Smarcelstruct macro_buffer 37130803Smarcel{ 38130803Smarcel /* An array of characters. The first LEN bytes are the real text, 39130803Smarcel but there are SIZE bytes allocated to the array. If SIZE is 40130803Smarcel zero, then this doesn't point to a malloc'ed block. If SHARED is 41130803Smarcel non-zero, then this buffer is actually a pointer into some larger 42130803Smarcel string, and we shouldn't append characters to it, etc. Because 43130803Smarcel of sharing, we can't assume in general that the text is 44130803Smarcel null-terminated. */ 45130803Smarcel char *text; 46130803Smarcel 47130803Smarcel /* The number of characters in the string. */ 48130803Smarcel int len; 49130803Smarcel 50130803Smarcel /* The number of characters allocated to the string. If SHARED is 51130803Smarcel non-zero, this is meaningless; in this case, we set it to zero so 52130803Smarcel that any "do we have room to append something?" tests will fail, 53130803Smarcel so we don't always have to check SHARED before using this field. */ 54130803Smarcel int size; 55130803Smarcel 56130803Smarcel /* Zero if TEXT can be safely realloc'ed (i.e., it's its own malloc 57130803Smarcel block). Non-zero if TEXT is actually pointing into the middle of 58130803Smarcel some other block, and we shouldn't reallocate it. */ 59130803Smarcel int shared; 60130803Smarcel 61130803Smarcel /* For detecting token splicing. 62130803Smarcel 63130803Smarcel This is the index in TEXT of the first character of the token 64130803Smarcel that abuts the end of TEXT. If TEXT contains no tokens, then we 65130803Smarcel set this equal to LEN. If TEXT ends in whitespace, then there is 66130803Smarcel no token abutting the end of TEXT (it's just whitespace), and 67130803Smarcel again, we set this equal to LEN. We set this to -1 if we don't 68130803Smarcel know the nature of TEXT. */ 69130803Smarcel int last_token; 70130803Smarcel 71130803Smarcel /* If this buffer is holding the result from get_token, then this 72130803Smarcel is non-zero if it is an identifier token, zero otherwise. */ 73130803Smarcel int is_identifier; 74130803Smarcel}; 75130803Smarcel 76130803Smarcel 77130803Smarcel/* Set the macro buffer *B to the empty string, guessing that its 78130803Smarcel final contents will fit in N bytes. (It'll get resized if it 79130803Smarcel doesn't, so the guess doesn't have to be right.) Allocate the 80130803Smarcel initial storage with xmalloc. */ 81130803Smarcelstatic void 82130803Smarcelinit_buffer (struct macro_buffer *b, int n) 83130803Smarcel{ 84130803Smarcel /* Small value for initial testing. */ 85130803Smarcel n = 1; 86130803Smarcel 87130803Smarcel b->size = n; 88130803Smarcel if (n > 0) 89130803Smarcel b->text = (char *) xmalloc (n); 90130803Smarcel else 91130803Smarcel b->text = NULL; 92130803Smarcel b->len = 0; 93130803Smarcel b->shared = 0; 94130803Smarcel b->last_token = -1; 95130803Smarcel} 96130803Smarcel 97130803Smarcel 98130803Smarcel/* Set the macro buffer *BUF to refer to the LEN bytes at ADDR, as a 99130803Smarcel shared substring. */ 100130803Smarcelstatic void 101130803Smarcelinit_shared_buffer (struct macro_buffer *buf, char *addr, int len) 102130803Smarcel{ 103130803Smarcel buf->text = addr; 104130803Smarcel buf->len = len; 105130803Smarcel buf->shared = 1; 106130803Smarcel buf->size = 0; 107130803Smarcel buf->last_token = -1; 108130803Smarcel} 109130803Smarcel 110130803Smarcel 111130803Smarcel/* Free the text of the buffer B. Raise an error if B is shared. */ 112130803Smarcelstatic void 113130803Smarcelfree_buffer (struct macro_buffer *b) 114130803Smarcel{ 115130803Smarcel gdb_assert (! b->shared); 116130803Smarcel if (b->size) 117130803Smarcel xfree (b->text); 118130803Smarcel} 119130803Smarcel 120130803Smarcel 121130803Smarcel/* A cleanup function for macro buffers. */ 122130803Smarcelstatic void 123130803Smarcelcleanup_macro_buffer (void *untyped_buf) 124130803Smarcel{ 125130803Smarcel free_buffer ((struct macro_buffer *) untyped_buf); 126130803Smarcel} 127130803Smarcel 128130803Smarcel 129130803Smarcel/* Resize the buffer B to be at least N bytes long. Raise an error if 130130803Smarcel B shouldn't be resized. */ 131130803Smarcelstatic void 132130803Smarcelresize_buffer (struct macro_buffer *b, int n) 133130803Smarcel{ 134130803Smarcel /* We shouldn't be trying to resize shared strings. */ 135130803Smarcel gdb_assert (! b->shared); 136130803Smarcel 137130803Smarcel if (b->size == 0) 138130803Smarcel b->size = n; 139130803Smarcel else 140130803Smarcel while (b->size <= n) 141130803Smarcel b->size *= 2; 142130803Smarcel 143130803Smarcel b->text = xrealloc (b->text, b->size); 144130803Smarcel} 145130803Smarcel 146130803Smarcel 147130803Smarcel/* Append the character C to the buffer B. */ 148130803Smarcelstatic void 149130803Smarcelappendc (struct macro_buffer *b, int c) 150130803Smarcel{ 151130803Smarcel int new_len = b->len + 1; 152130803Smarcel 153130803Smarcel if (new_len > b->size) 154130803Smarcel resize_buffer (b, new_len); 155130803Smarcel 156130803Smarcel b->text[b->len] = c; 157130803Smarcel b->len = new_len; 158130803Smarcel} 159130803Smarcel 160130803Smarcel 161130803Smarcel/* Append the LEN bytes at ADDR to the buffer B. */ 162130803Smarcelstatic void 163130803Smarcelappendmem (struct macro_buffer *b, char *addr, int len) 164130803Smarcel{ 165130803Smarcel int new_len = b->len + len; 166130803Smarcel 167130803Smarcel if (new_len > b->size) 168130803Smarcel resize_buffer (b, new_len); 169130803Smarcel 170130803Smarcel memcpy (b->text + b->len, addr, len); 171130803Smarcel b->len = new_len; 172130803Smarcel} 173130803Smarcel 174130803Smarcel 175130803Smarcel 176130803Smarcel/* Recognizing preprocessor tokens. */ 177130803Smarcel 178130803Smarcel 179130803Smarcelstatic int 180130803Smarcelis_whitespace (int c) 181130803Smarcel{ 182130803Smarcel return (c == ' ' 183130803Smarcel || c == '\t' 184130803Smarcel || c == '\n' 185130803Smarcel || c == '\v' 186130803Smarcel || c == '\f'); 187130803Smarcel} 188130803Smarcel 189130803Smarcel 190130803Smarcelstatic int 191130803Smarcelis_digit (int c) 192130803Smarcel{ 193130803Smarcel return ('0' <= c && c <= '9'); 194130803Smarcel} 195130803Smarcel 196130803Smarcel 197130803Smarcelstatic int 198130803Smarcelis_identifier_nondigit (int c) 199130803Smarcel{ 200130803Smarcel return (c == '_' 201130803Smarcel || ('a' <= c && c <= 'z') 202130803Smarcel || ('A' <= c && c <= 'Z')); 203130803Smarcel} 204130803Smarcel 205130803Smarcel 206130803Smarcelstatic void 207130803Smarcelset_token (struct macro_buffer *tok, char *start, char *end) 208130803Smarcel{ 209130803Smarcel init_shared_buffer (tok, start, end - start); 210130803Smarcel tok->last_token = 0; 211130803Smarcel 212130803Smarcel /* Presumed; get_identifier may overwrite this. */ 213130803Smarcel tok->is_identifier = 0; 214130803Smarcel} 215130803Smarcel 216130803Smarcel 217130803Smarcelstatic int 218130803Smarcelget_comment (struct macro_buffer *tok, char *p, char *end) 219130803Smarcel{ 220130803Smarcel if (p + 2 > end) 221130803Smarcel return 0; 222130803Smarcel else if (p[0] == '/' 223130803Smarcel && p[1] == '*') 224130803Smarcel { 225130803Smarcel char *tok_start = p; 226130803Smarcel 227130803Smarcel p += 2; 228130803Smarcel 229130803Smarcel for (; p < end; p++) 230130803Smarcel if (p + 2 <= end 231130803Smarcel && p[0] == '*' 232130803Smarcel && p[1] == '/') 233130803Smarcel { 234130803Smarcel p += 2; 235130803Smarcel set_token (tok, tok_start, p); 236130803Smarcel return 1; 237130803Smarcel } 238130803Smarcel 239130803Smarcel error ("Unterminated comment in macro expansion."); 240130803Smarcel } 241130803Smarcel else if (p[0] == '/' 242130803Smarcel && p[1] == '/') 243130803Smarcel { 244130803Smarcel char *tok_start = p; 245130803Smarcel 246130803Smarcel p += 2; 247130803Smarcel for (; p < end; p++) 248130803Smarcel if (*p == '\n') 249130803Smarcel break; 250130803Smarcel 251130803Smarcel set_token (tok, tok_start, p); 252130803Smarcel return 1; 253130803Smarcel } 254130803Smarcel else 255130803Smarcel return 0; 256130803Smarcel} 257130803Smarcel 258130803Smarcel 259130803Smarcelstatic int 260130803Smarcelget_identifier (struct macro_buffer *tok, char *p, char *end) 261130803Smarcel{ 262130803Smarcel if (p < end 263130803Smarcel && is_identifier_nondigit (*p)) 264130803Smarcel { 265130803Smarcel char *tok_start = p; 266130803Smarcel 267130803Smarcel while (p < end 268130803Smarcel && (is_identifier_nondigit (*p) 269130803Smarcel || is_digit (*p))) 270130803Smarcel p++; 271130803Smarcel 272130803Smarcel set_token (tok, tok_start, p); 273130803Smarcel tok->is_identifier = 1; 274130803Smarcel return 1; 275130803Smarcel } 276130803Smarcel else 277130803Smarcel return 0; 278130803Smarcel} 279130803Smarcel 280130803Smarcel 281130803Smarcelstatic int 282130803Smarcelget_pp_number (struct macro_buffer *tok, char *p, char *end) 283130803Smarcel{ 284130803Smarcel if (p < end 285130803Smarcel && (is_digit (*p) 286130803Smarcel || *p == '.')) 287130803Smarcel { 288130803Smarcel char *tok_start = p; 289130803Smarcel 290130803Smarcel while (p < end) 291130803Smarcel { 292130803Smarcel if (is_digit (*p) 293130803Smarcel || is_identifier_nondigit (*p) 294130803Smarcel || *p == '.') 295130803Smarcel p++; 296130803Smarcel else if (p + 2 <= end 297130803Smarcel && strchr ("eEpP.", *p) 298130803Smarcel && (p[1] == '+' || p[1] == '-')) 299130803Smarcel p += 2; 300130803Smarcel else 301130803Smarcel break; 302130803Smarcel } 303130803Smarcel 304130803Smarcel set_token (tok, tok_start, p); 305130803Smarcel return 1; 306130803Smarcel } 307130803Smarcel else 308130803Smarcel return 0; 309130803Smarcel} 310130803Smarcel 311130803Smarcel 312130803Smarcel 313130803Smarcel/* If the text starting at P going up to (but not including) END 314130803Smarcel starts with a character constant, set *TOK to point to that 315130803Smarcel character constant, and return 1. Otherwise, return zero. 316130803Smarcel Signal an error if it contains a malformed or incomplete character 317130803Smarcel constant. */ 318130803Smarcelstatic int 319130803Smarcelget_character_constant (struct macro_buffer *tok, char *p, char *end) 320130803Smarcel{ 321130803Smarcel /* ISO/IEC 9899:1999 (E) Section 6.4.4.4 paragraph 1 322130803Smarcel But of course, what really matters is that we handle it the same 323130803Smarcel way GDB's C/C++ lexer does. So we call parse_escape in utils.c 324130803Smarcel to handle escape sequences. */ 325130803Smarcel if ((p + 1 <= end && *p == '\'') 326130803Smarcel || (p + 2 <= end && p[0] == 'L' && p[1] == '\'')) 327130803Smarcel { 328130803Smarcel char *tok_start = p; 329130803Smarcel char *body_start; 330130803Smarcel 331130803Smarcel if (*p == '\'') 332130803Smarcel p++; 333130803Smarcel else if (*p == 'L') 334130803Smarcel p += 2; 335130803Smarcel else 336130803Smarcel gdb_assert (0); 337130803Smarcel 338130803Smarcel body_start = p; 339130803Smarcel for (;;) 340130803Smarcel { 341130803Smarcel if (p >= end) 342130803Smarcel error ("Unmatched single quote."); 343130803Smarcel else if (*p == '\'') 344130803Smarcel { 345130803Smarcel if (p == body_start) 346130803Smarcel error ("A character constant must contain at least one " 347130803Smarcel "character."); 348130803Smarcel p++; 349130803Smarcel break; 350130803Smarcel } 351130803Smarcel else if (*p == '\\') 352130803Smarcel { 353130803Smarcel p++; 354130803Smarcel parse_escape (&p); 355130803Smarcel } 356130803Smarcel else 357130803Smarcel p++; 358130803Smarcel } 359130803Smarcel 360130803Smarcel set_token (tok, tok_start, p); 361130803Smarcel return 1; 362130803Smarcel } 363130803Smarcel else 364130803Smarcel return 0; 365130803Smarcel} 366130803Smarcel 367130803Smarcel 368130803Smarcel/* If the text starting at P going up to (but not including) END 369130803Smarcel starts with a string literal, set *TOK to point to that string 370130803Smarcel literal, and return 1. Otherwise, return zero. Signal an error if 371130803Smarcel it contains a malformed or incomplete string literal. */ 372130803Smarcelstatic int 373130803Smarcelget_string_literal (struct macro_buffer *tok, char *p, char *end) 374130803Smarcel{ 375130803Smarcel if ((p + 1 <= end 376130803Smarcel && *p == '\"') 377130803Smarcel || (p + 2 <= end 378130803Smarcel && p[0] == 'L' 379130803Smarcel && p[1] == '\"')) 380130803Smarcel { 381130803Smarcel char *tok_start = p; 382130803Smarcel 383130803Smarcel if (*p == '\"') 384130803Smarcel p++; 385130803Smarcel else if (*p == 'L') 386130803Smarcel p += 2; 387130803Smarcel else 388130803Smarcel gdb_assert (0); 389130803Smarcel 390130803Smarcel for (;;) 391130803Smarcel { 392130803Smarcel if (p >= end) 393130803Smarcel error ("Unterminated string in expression."); 394130803Smarcel else if (*p == '\"') 395130803Smarcel { 396130803Smarcel p++; 397130803Smarcel break; 398130803Smarcel } 399130803Smarcel else if (*p == '\n') 400130803Smarcel error ("Newline characters may not appear in string " 401130803Smarcel "constants."); 402130803Smarcel else if (*p == '\\') 403130803Smarcel { 404130803Smarcel p++; 405130803Smarcel parse_escape (&p); 406130803Smarcel } 407130803Smarcel else 408130803Smarcel p++; 409130803Smarcel } 410130803Smarcel 411130803Smarcel set_token (tok, tok_start, p); 412130803Smarcel return 1; 413130803Smarcel } 414130803Smarcel else 415130803Smarcel return 0; 416130803Smarcel} 417130803Smarcel 418130803Smarcel 419130803Smarcelstatic int 420130803Smarcelget_punctuator (struct macro_buffer *tok, char *p, char *end) 421130803Smarcel{ 422130803Smarcel /* Here, speed is much less important than correctness and clarity. */ 423130803Smarcel 424130803Smarcel /* ISO/IEC 9899:1999 (E) Section 6.4.6 Paragraph 1 */ 425130803Smarcel static const char * const punctuators[] = { 426130803Smarcel "[", "]", "(", ")", "{", "}", ".", "->", 427130803Smarcel "++", "--", "&", "*", "+", "-", "~", "!", 428130803Smarcel "/", "%", "<<", ">>", "<", ">", "<=", ">=", "==", "!=", 429130803Smarcel "^", "|", "&&", "||", 430130803Smarcel "?", ":", ";", "...", 431130803Smarcel "=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=", 432130803Smarcel ",", "#", "##", 433130803Smarcel "<:", ":>", "<%", "%>", "%:", "%:%:", 434130803Smarcel 0 435130803Smarcel }; 436130803Smarcel 437130803Smarcel int i; 438130803Smarcel 439130803Smarcel if (p + 1 <= end) 440130803Smarcel { 441130803Smarcel for (i = 0; punctuators[i]; i++) 442130803Smarcel { 443130803Smarcel const char *punctuator = punctuators[i]; 444130803Smarcel 445130803Smarcel if (p[0] == punctuator[0]) 446130803Smarcel { 447130803Smarcel int len = strlen (punctuator); 448130803Smarcel 449130803Smarcel if (p + len <= end 450130803Smarcel && ! memcmp (p, punctuator, len)) 451130803Smarcel { 452130803Smarcel set_token (tok, p, p + len); 453130803Smarcel return 1; 454130803Smarcel } 455130803Smarcel } 456130803Smarcel } 457130803Smarcel } 458130803Smarcel 459130803Smarcel return 0; 460130803Smarcel} 461130803Smarcel 462130803Smarcel 463130803Smarcel/* Peel the next preprocessor token off of SRC, and put it in TOK. 464130803Smarcel Mutate TOK to refer to the first token in SRC, and mutate SRC to 465130803Smarcel refer to the text after that token. SRC must be a shared buffer; 466130803Smarcel the resulting TOK will be shared, pointing into the same string SRC 467130803Smarcel does. Initialize TOK's last_token field. Return non-zero if we 468130803Smarcel succeed, or 0 if we didn't find any more tokens in SRC. */ 469130803Smarcelstatic int 470130803Smarcelget_token (struct macro_buffer *tok, 471130803Smarcel struct macro_buffer *src) 472130803Smarcel{ 473130803Smarcel char *p = src->text; 474130803Smarcel char *end = p + src->len; 475130803Smarcel 476130803Smarcel gdb_assert (src->shared); 477130803Smarcel 478130803Smarcel /* From the ISO C standard, ISO/IEC 9899:1999 (E), section 6.4: 479130803Smarcel 480130803Smarcel preprocessing-token: 481130803Smarcel header-name 482130803Smarcel identifier 483130803Smarcel pp-number 484130803Smarcel character-constant 485130803Smarcel string-literal 486130803Smarcel punctuator 487130803Smarcel each non-white-space character that cannot be one of the above 488130803Smarcel 489130803Smarcel We don't have to deal with header-name tokens, since those can 490130803Smarcel only occur after a #include, which we will never see. */ 491130803Smarcel 492130803Smarcel while (p < end) 493130803Smarcel if (is_whitespace (*p)) 494130803Smarcel p++; 495130803Smarcel else if (get_comment (tok, p, end)) 496130803Smarcel p += tok->len; 497130803Smarcel else if (get_pp_number (tok, p, end) 498130803Smarcel || get_character_constant (tok, p, end) 499130803Smarcel || get_string_literal (tok, p, end) 500130803Smarcel /* Note: the grammar in the standard seems to be 501130803Smarcel ambiguous: L'x' can be either a wide character 502130803Smarcel constant, or an identifier followed by a normal 503130803Smarcel character constant. By trying `get_identifier' after 504130803Smarcel we try get_character_constant and get_string_literal, 505130803Smarcel we give the wide character syntax precedence. Now, 506130803Smarcel since GDB doesn't handle wide character constants 507130803Smarcel anyway, is this the right thing to do? */ 508130803Smarcel || get_identifier (tok, p, end) 509130803Smarcel || get_punctuator (tok, p, end)) 510130803Smarcel { 511130803Smarcel /* How many characters did we consume, including whitespace? */ 512130803Smarcel int consumed = p - src->text + tok->len; 513130803Smarcel src->text += consumed; 514130803Smarcel src->len -= consumed; 515130803Smarcel return 1; 516130803Smarcel } 517130803Smarcel else 518130803Smarcel { 519130803Smarcel /* We have found a "non-whitespace character that cannot be 520130803Smarcel one of the above." Make a token out of it. */ 521130803Smarcel int consumed; 522130803Smarcel 523130803Smarcel set_token (tok, p, p + 1); 524130803Smarcel consumed = p - src->text + tok->len; 525130803Smarcel src->text += consumed; 526130803Smarcel src->len -= consumed; 527130803Smarcel return 1; 528130803Smarcel } 529130803Smarcel 530130803Smarcel return 0; 531130803Smarcel} 532130803Smarcel 533130803Smarcel 534130803Smarcel 535130803Smarcel/* Appending token strings, with and without splicing */ 536130803Smarcel 537130803Smarcel 538130803Smarcel/* Append the macro buffer SRC to the end of DEST, and ensure that 539130803Smarcel doing so doesn't splice the token at the end of SRC with the token 540130803Smarcel at the beginning of DEST. SRC and DEST must have their last_token 541130803Smarcel fields set. Upon return, DEST's last_token field is set correctly. 542130803Smarcel 543130803Smarcel For example: 544130803Smarcel 545130803Smarcel If DEST is "(" and SRC is "y", then we can return with 546130803Smarcel DEST set to "(y" --- we've simply appended the two buffers. 547130803Smarcel 548130803Smarcel However, if DEST is "x" and SRC is "y", then we must not return 549130803Smarcel with DEST set to "xy" --- that would splice the two tokens "x" and 550130803Smarcel "y" together to make a single token "xy". However, it would be 551130803Smarcel fine to return with DEST set to "x y". Similarly, "<" and "<" must 552130803Smarcel yield "< <", not "<<", etc. */ 553130803Smarcelstatic void 554130803Smarcelappend_tokens_without_splicing (struct macro_buffer *dest, 555130803Smarcel struct macro_buffer *src) 556130803Smarcel{ 557130803Smarcel int original_dest_len = dest->len; 558130803Smarcel struct macro_buffer dest_tail, new_token; 559130803Smarcel 560130803Smarcel gdb_assert (src->last_token != -1); 561130803Smarcel gdb_assert (dest->last_token != -1); 562130803Smarcel 563130803Smarcel /* First, just try appending the two, and call get_token to see if 564130803Smarcel we got a splice. */ 565130803Smarcel appendmem (dest, src->text, src->len); 566130803Smarcel 567130803Smarcel /* If DEST originally had no token abutting its end, then we can't 568130803Smarcel have spliced anything, so we're done. */ 569130803Smarcel if (dest->last_token == original_dest_len) 570130803Smarcel { 571130803Smarcel dest->last_token = original_dest_len + src->last_token; 572130803Smarcel return; 573130803Smarcel } 574130803Smarcel 575130803Smarcel /* Set DEST_TAIL to point to the last token in DEST, followed by 576130803Smarcel all the stuff we just appended. */ 577130803Smarcel init_shared_buffer (&dest_tail, 578130803Smarcel dest->text + dest->last_token, 579130803Smarcel dest->len - dest->last_token); 580130803Smarcel 581130803Smarcel /* Re-parse DEST's last token. We know that DEST used to contain 582130803Smarcel at least one token, so if it doesn't contain any after the 583130803Smarcel append, then we must have spliced "/" and "*" or "/" and "/" to 584130803Smarcel make a comment start. (Just for the record, I got this right 585130803Smarcel the first time. This is not a bug fix.) */ 586130803Smarcel if (get_token (&new_token, &dest_tail) 587130803Smarcel && (new_token.text + new_token.len 588130803Smarcel == dest->text + original_dest_len)) 589130803Smarcel { 590130803Smarcel /* No splice, so we're done. */ 591130803Smarcel dest->last_token = original_dest_len + src->last_token; 592130803Smarcel return; 593130803Smarcel } 594130803Smarcel 595130803Smarcel /* Okay, a simple append caused a splice. Let's chop dest back to 596130803Smarcel its original length and try again, but separate the texts with a 597130803Smarcel space. */ 598130803Smarcel dest->len = original_dest_len; 599130803Smarcel appendc (dest, ' '); 600130803Smarcel appendmem (dest, src->text, src->len); 601130803Smarcel 602130803Smarcel init_shared_buffer (&dest_tail, 603130803Smarcel dest->text + dest->last_token, 604130803Smarcel dest->len - dest->last_token); 605130803Smarcel 606130803Smarcel /* Try to re-parse DEST's last token, as above. */ 607130803Smarcel if (get_token (&new_token, &dest_tail) 608130803Smarcel && (new_token.text + new_token.len 609130803Smarcel == dest->text + original_dest_len)) 610130803Smarcel { 611130803Smarcel /* No splice, so we're done. */ 612130803Smarcel dest->last_token = original_dest_len + 1 + src->last_token; 613130803Smarcel return; 614130803Smarcel } 615130803Smarcel 616130803Smarcel /* As far as I know, there's no case where inserting a space isn't 617130803Smarcel enough to prevent a splice. */ 618130803Smarcel internal_error (__FILE__, __LINE__, 619130803Smarcel "unable to avoid splicing tokens during macro expansion"); 620130803Smarcel} 621130803Smarcel 622130803Smarcel 623130803Smarcel 624130803Smarcel/* Expanding macros! */ 625130803Smarcel 626130803Smarcel 627130803Smarcel/* A singly-linked list of the names of the macros we are currently 628130803Smarcel expanding --- for detecting expansion loops. */ 629130803Smarcelstruct macro_name_list { 630130803Smarcel const char *name; 631130803Smarcel struct macro_name_list *next; 632130803Smarcel}; 633130803Smarcel 634130803Smarcel 635130803Smarcel/* Return non-zero if we are currently expanding the macro named NAME, 636130803Smarcel according to LIST; otherwise, return zero. 637130803Smarcel 638130803Smarcel You know, it would be possible to get rid of all the NO_LOOP 639130803Smarcel arguments to these functions by simply generating a new lookup 640130803Smarcel function and baton which refuses to find the definition for a 641130803Smarcel particular macro, and otherwise delegates the decision to another 642130803Smarcel function/baton pair. But that makes the linked list of excluded 643130803Smarcel macros chained through untyped baton pointers, which will make it 644130803Smarcel harder to debug. :( */ 645130803Smarcelstatic int 646130803Smarcelcurrently_rescanning (struct macro_name_list *list, const char *name) 647130803Smarcel{ 648130803Smarcel for (; list; list = list->next) 649130803Smarcel if (strcmp (name, list->name) == 0) 650130803Smarcel return 1; 651130803Smarcel 652130803Smarcel return 0; 653130803Smarcel} 654130803Smarcel 655130803Smarcel 656130803Smarcel/* Gather the arguments to a macro expansion. 657130803Smarcel 658130803Smarcel NAME is the name of the macro being invoked. (It's only used for 659130803Smarcel printing error messages.) 660130803Smarcel 661130803Smarcel Assume that SRC is the text of the macro invocation immediately 662130803Smarcel following the macro name. For example, if we're processing the 663130803Smarcel text foo(bar, baz), then NAME would be foo and SRC will be (bar, 664130803Smarcel baz). 665130803Smarcel 666130803Smarcel If SRC doesn't start with an open paren ( token at all, return 667130803Smarcel zero, leave SRC unchanged, and don't set *ARGC_P to anything. 668130803Smarcel 669130803Smarcel If SRC doesn't contain a properly terminated argument list, then 670130803Smarcel raise an error. 671130803Smarcel 672130803Smarcel Otherwise, return a pointer to the first element of an array of 673130803Smarcel macro buffers referring to the argument texts, and set *ARGC_P to 674130803Smarcel the number of arguments we found --- the number of elements in the 675130803Smarcel array. The macro buffers share their text with SRC, and their 676130803Smarcel last_token fields are initialized. The array is allocated with 677130803Smarcel xmalloc, and the caller is responsible for freeing it. 678130803Smarcel 679130803Smarcel NOTE WELL: if SRC starts with a open paren ( token followed 680130803Smarcel immediately by a close paren ) token (e.g., the invocation looks 681130803Smarcel like "foo()"), we treat that as one argument, which happens to be 682130803Smarcel the empty list of tokens. The caller should keep in mind that such 683130803Smarcel a sequence of tokens is a valid way to invoke one-parameter 684130803Smarcel function-like macros, but also a valid way to invoke zero-parameter 685130803Smarcel function-like macros. Eeew. 686130803Smarcel 687130803Smarcel Consume the tokens from SRC; after this call, SRC contains the text 688130803Smarcel following the invocation. */ 689130803Smarcel 690130803Smarcelstatic struct macro_buffer * 691130803Smarcelgather_arguments (const char *name, struct macro_buffer *src, int *argc_p) 692130803Smarcel{ 693130803Smarcel struct macro_buffer tok; 694130803Smarcel int args_len, args_size; 695130803Smarcel struct macro_buffer *args = NULL; 696130803Smarcel struct cleanup *back_to = make_cleanup (free_current_contents, &args); 697130803Smarcel 698130803Smarcel /* Does SRC start with an opening paren token? Read from a copy of 699130803Smarcel SRC, so SRC itself is unaffected if we don't find an opening 700130803Smarcel paren. */ 701130803Smarcel { 702130803Smarcel struct macro_buffer temp; 703130803Smarcel init_shared_buffer (&temp, src->text, src->len); 704130803Smarcel 705130803Smarcel if (! get_token (&tok, &temp) 706130803Smarcel || tok.len != 1 707130803Smarcel || tok.text[0] != '(') 708130803Smarcel { 709130803Smarcel discard_cleanups (back_to); 710130803Smarcel return 0; 711130803Smarcel } 712130803Smarcel } 713130803Smarcel 714130803Smarcel /* Consume SRC's opening paren. */ 715130803Smarcel get_token (&tok, src); 716130803Smarcel 717130803Smarcel args_len = 0; 718130803Smarcel args_size = 1; /* small for initial testing */ 719130803Smarcel args = (struct macro_buffer *) xmalloc (sizeof (*args) * args_size); 720130803Smarcel 721130803Smarcel for (;;) 722130803Smarcel { 723130803Smarcel struct macro_buffer *arg; 724130803Smarcel int depth; 725130803Smarcel 726130803Smarcel /* Make sure we have room for the next argument. */ 727130803Smarcel if (args_len >= args_size) 728130803Smarcel { 729130803Smarcel args_size *= 2; 730130803Smarcel args = xrealloc (args, sizeof (*args) * args_size); 731130803Smarcel } 732130803Smarcel 733130803Smarcel /* Initialize the next argument. */ 734130803Smarcel arg = &args[args_len++]; 735130803Smarcel set_token (arg, src->text, src->text); 736130803Smarcel 737130803Smarcel /* Gather the argument's tokens. */ 738130803Smarcel depth = 0; 739130803Smarcel for (;;) 740130803Smarcel { 741130803Smarcel char *start = src->text; 742130803Smarcel 743130803Smarcel if (! get_token (&tok, src)) 744130803Smarcel error ("Malformed argument list for macro `%s'.", name); 745130803Smarcel 746130803Smarcel /* Is tok an opening paren? */ 747130803Smarcel if (tok.len == 1 && tok.text[0] == '(') 748130803Smarcel depth++; 749130803Smarcel 750130803Smarcel /* Is tok is a closing paren? */ 751130803Smarcel else if (tok.len == 1 && tok.text[0] == ')') 752130803Smarcel { 753130803Smarcel /* If it's a closing paren at the top level, then that's 754130803Smarcel the end of the argument list. */ 755130803Smarcel if (depth == 0) 756130803Smarcel { 757130803Smarcel discard_cleanups (back_to); 758130803Smarcel *argc_p = args_len; 759130803Smarcel return args; 760130803Smarcel } 761130803Smarcel 762130803Smarcel depth--; 763130803Smarcel } 764130803Smarcel 765130803Smarcel /* If tok is a comma at top level, then that's the end of 766130803Smarcel the current argument. */ 767130803Smarcel else if (tok.len == 1 && tok.text[0] == ',' && depth == 0) 768130803Smarcel break; 769130803Smarcel 770130803Smarcel /* Extend the current argument to enclose this token. If 771130803Smarcel this is the current argument's first token, leave out any 772130803Smarcel leading whitespace, just for aesthetics. */ 773130803Smarcel if (arg->len == 0) 774130803Smarcel { 775130803Smarcel arg->text = tok.text; 776130803Smarcel arg->len = tok.len; 777130803Smarcel arg->last_token = 0; 778130803Smarcel } 779130803Smarcel else 780130803Smarcel { 781130803Smarcel arg->len = (tok.text + tok.len) - arg->text; 782130803Smarcel arg->last_token = tok.text - arg->text; 783130803Smarcel } 784130803Smarcel } 785130803Smarcel } 786130803Smarcel} 787130803Smarcel 788130803Smarcel 789130803Smarcel/* The `expand' and `substitute_args' functions both invoke `scan' 790130803Smarcel recursively, so we need a forward declaration somewhere. */ 791130803Smarcelstatic void scan (struct macro_buffer *dest, 792130803Smarcel struct macro_buffer *src, 793130803Smarcel struct macro_name_list *no_loop, 794130803Smarcel macro_lookup_ftype *lookup_func, 795130803Smarcel void *lookup_baton); 796130803Smarcel 797130803Smarcel 798130803Smarcel/* Given the macro definition DEF, being invoked with the actual 799130803Smarcel arguments given by ARGC and ARGV, substitute the arguments into the 800130803Smarcel replacement list, and store the result in DEST. 801130803Smarcel 802130803Smarcel If it is necessary to expand macro invocations in one of the 803130803Smarcel arguments, use LOOKUP_FUNC and LOOKUP_BATON to find the macro 804130803Smarcel definitions, and don't expand invocations of the macros listed in 805130803Smarcel NO_LOOP. */ 806130803Smarcelstatic void 807130803Smarcelsubstitute_args (struct macro_buffer *dest, 808130803Smarcel struct macro_definition *def, 809130803Smarcel int argc, struct macro_buffer *argv, 810130803Smarcel struct macro_name_list *no_loop, 811130803Smarcel macro_lookup_ftype *lookup_func, 812130803Smarcel void *lookup_baton) 813130803Smarcel{ 814130803Smarcel /* A macro buffer for the macro's replacement list. */ 815130803Smarcel struct macro_buffer replacement_list; 816130803Smarcel 817130803Smarcel init_shared_buffer (&replacement_list, (char *) def->replacement, 818130803Smarcel strlen (def->replacement)); 819130803Smarcel 820130803Smarcel gdb_assert (dest->len == 0); 821130803Smarcel dest->last_token = 0; 822130803Smarcel 823130803Smarcel for (;;) 824130803Smarcel { 825130803Smarcel struct macro_buffer tok; 826130803Smarcel char *original_rl_start = replacement_list.text; 827130803Smarcel int substituted = 0; 828130803Smarcel 829130803Smarcel /* Find the next token in the replacement list. */ 830130803Smarcel if (! get_token (&tok, &replacement_list)) 831130803Smarcel break; 832130803Smarcel 833130803Smarcel /* Just for aesthetics. If we skipped some whitespace, copy 834130803Smarcel that to DEST. */ 835130803Smarcel if (tok.text > original_rl_start) 836130803Smarcel { 837130803Smarcel appendmem (dest, original_rl_start, tok.text - original_rl_start); 838130803Smarcel dest->last_token = dest->len; 839130803Smarcel } 840130803Smarcel 841130803Smarcel /* Is this token the stringification operator? */ 842130803Smarcel if (tok.len == 1 843130803Smarcel && tok.text[0] == '#') 844130803Smarcel error ("Stringification is not implemented yet."); 845130803Smarcel 846130803Smarcel /* Is this token the splicing operator? */ 847130803Smarcel if (tok.len == 2 848130803Smarcel && tok.text[0] == '#' 849130803Smarcel && tok.text[1] == '#') 850130803Smarcel error ("Token splicing is not implemented yet."); 851130803Smarcel 852130803Smarcel /* Is this token an identifier? */ 853130803Smarcel if (tok.is_identifier) 854130803Smarcel { 855130803Smarcel int i; 856130803Smarcel 857130803Smarcel /* Is it the magic varargs parameter? */ 858130803Smarcel if (tok.len == 11 859130803Smarcel && ! memcmp (tok.text, "__VA_ARGS__", 11)) 860130803Smarcel error ("Variable-arity macros not implemented yet."); 861130803Smarcel 862130803Smarcel /* Is it one of the parameters? */ 863130803Smarcel for (i = 0; i < def->argc; i++) 864130803Smarcel if (tok.len == strlen (def->argv[i]) 865130803Smarcel && ! memcmp (tok.text, def->argv[i], tok.len)) 866130803Smarcel { 867130803Smarcel struct macro_buffer arg_src; 868130803Smarcel 869130803Smarcel /* Expand any macro invocations in the argument text, 870130803Smarcel and append the result to dest. Remember that scan 871130803Smarcel mutates its source, so we need to scan a new buffer 872130803Smarcel referring to the argument's text, not the argument 873130803Smarcel itself. */ 874130803Smarcel init_shared_buffer (&arg_src, argv[i].text, argv[i].len); 875130803Smarcel scan (dest, &arg_src, no_loop, lookup_func, lookup_baton); 876130803Smarcel substituted = 1; 877130803Smarcel break; 878130803Smarcel } 879130803Smarcel } 880130803Smarcel 881130803Smarcel /* If it wasn't a parameter, then just copy it across. */ 882130803Smarcel if (! substituted) 883130803Smarcel append_tokens_without_splicing (dest, &tok); 884130803Smarcel } 885130803Smarcel} 886130803Smarcel 887130803Smarcel 888130803Smarcel/* Expand a call to a macro named ID, whose definition is DEF. Append 889130803Smarcel its expansion to DEST. SRC is the input text following the ID 890130803Smarcel token. We are currently rescanning the expansions of the macros 891130803Smarcel named in NO_LOOP; don't re-expand them. Use LOOKUP_FUNC and 892130803Smarcel LOOKUP_BATON to find definitions for any nested macro references. 893130803Smarcel 894130803Smarcel Return 1 if we decided to expand it, zero otherwise. (If it's a 895130803Smarcel function-like macro name that isn't followed by an argument list, 896130803Smarcel we don't expand it.) If we return zero, leave SRC unchanged. */ 897130803Smarcelstatic int 898130803Smarcelexpand (const char *id, 899130803Smarcel struct macro_definition *def, 900130803Smarcel struct macro_buffer *dest, 901130803Smarcel struct macro_buffer *src, 902130803Smarcel struct macro_name_list *no_loop, 903130803Smarcel macro_lookup_ftype *lookup_func, 904130803Smarcel void *lookup_baton) 905130803Smarcel{ 906130803Smarcel struct macro_name_list new_no_loop; 907130803Smarcel 908130803Smarcel /* Create a new node to be added to the front of the no-expand list. 909130803Smarcel This list is appropriate for re-scanning replacement lists, but 910130803Smarcel it is *not* appropriate for scanning macro arguments; invocations 911130803Smarcel of the macro whose arguments we are gathering *do* get expanded 912130803Smarcel there. */ 913130803Smarcel new_no_loop.name = id; 914130803Smarcel new_no_loop.next = no_loop; 915130803Smarcel 916130803Smarcel /* What kind of macro are we expanding? */ 917130803Smarcel if (def->kind == macro_object_like) 918130803Smarcel { 919130803Smarcel struct macro_buffer replacement_list; 920130803Smarcel 921130803Smarcel init_shared_buffer (&replacement_list, (char *) def->replacement, 922130803Smarcel strlen (def->replacement)); 923130803Smarcel 924130803Smarcel scan (dest, &replacement_list, &new_no_loop, lookup_func, lookup_baton); 925130803Smarcel return 1; 926130803Smarcel } 927130803Smarcel else if (def->kind == macro_function_like) 928130803Smarcel { 929130803Smarcel struct cleanup *back_to = make_cleanup (null_cleanup, 0); 930130803Smarcel int argc; 931130803Smarcel struct macro_buffer *argv = NULL; 932130803Smarcel struct macro_buffer substituted; 933130803Smarcel struct macro_buffer substituted_src; 934130803Smarcel 935130803Smarcel if (def->argc >= 1 936130803Smarcel && strcmp (def->argv[def->argc - 1], "...") == 0) 937130803Smarcel error ("Varargs macros not implemented yet."); 938130803Smarcel 939130803Smarcel make_cleanup (free_current_contents, &argv); 940130803Smarcel argv = gather_arguments (id, src, &argc); 941130803Smarcel 942130803Smarcel /* If we couldn't find any argument list, then we don't expand 943130803Smarcel this macro. */ 944130803Smarcel if (! argv) 945130803Smarcel { 946130803Smarcel do_cleanups (back_to); 947130803Smarcel return 0; 948130803Smarcel } 949130803Smarcel 950130803Smarcel /* Check that we're passing an acceptable number of arguments for 951130803Smarcel this macro. */ 952130803Smarcel if (argc != def->argc) 953130803Smarcel { 954130803Smarcel /* Remember that a sequence of tokens like "foo()" is a 955130803Smarcel valid invocation of a macro expecting either zero or one 956130803Smarcel arguments. */ 957130803Smarcel if (! (argc == 1 958130803Smarcel && argv[0].len == 0 959130803Smarcel && def->argc == 0)) 960130803Smarcel error ("Wrong number of arguments to macro `%s' " 961130803Smarcel "(expected %d, got %d).", 962130803Smarcel id, def->argc, argc); 963130803Smarcel } 964130803Smarcel 965130803Smarcel /* Note that we don't expand macro invocations in the arguments 966130803Smarcel yet --- we let subst_args take care of that. Parameters that 967130803Smarcel appear as operands of the stringifying operator "#" or the 968130803Smarcel splicing operator "##" don't get macro references expanded, 969130803Smarcel so we can't really tell whether it's appropriate to macro- 970130803Smarcel expand an argument until we see how it's being used. */ 971130803Smarcel init_buffer (&substituted, 0); 972130803Smarcel make_cleanup (cleanup_macro_buffer, &substituted); 973130803Smarcel substitute_args (&substituted, def, argc, argv, no_loop, 974130803Smarcel lookup_func, lookup_baton); 975130803Smarcel 976130803Smarcel /* Now `substituted' is the macro's replacement list, with all 977130803Smarcel argument values substituted into it properly. Re-scan it for 978130803Smarcel macro references, but don't expand invocations of this macro. 979130803Smarcel 980130803Smarcel We create a new buffer, `substituted_src', which points into 981130803Smarcel `substituted', and scan that. We can't scan `substituted' 982130803Smarcel itself, since the tokenization process moves the buffer's 983130803Smarcel text pointer around, and we still need to be able to find 984130803Smarcel `substituted's original text buffer after scanning it so we 985130803Smarcel can free it. */ 986130803Smarcel init_shared_buffer (&substituted_src, substituted.text, substituted.len); 987130803Smarcel scan (dest, &substituted_src, &new_no_loop, lookup_func, lookup_baton); 988130803Smarcel 989130803Smarcel do_cleanups (back_to); 990130803Smarcel 991130803Smarcel return 1; 992130803Smarcel } 993130803Smarcel else 994130803Smarcel internal_error (__FILE__, __LINE__, "bad macro definition kind"); 995130803Smarcel} 996130803Smarcel 997130803Smarcel 998130803Smarcel/* If the single token in SRC_FIRST followed by the tokens in SRC_REST 999130803Smarcel constitute a macro invokation not forbidden in NO_LOOP, append its 1000130803Smarcel expansion to DEST and return non-zero. Otherwise, return zero, and 1001130803Smarcel leave DEST unchanged. 1002130803Smarcel 1003130803Smarcel SRC_FIRST and SRC_REST must be shared buffers; DEST must not be one. 1004130803Smarcel SRC_FIRST must be a string built by get_token. */ 1005130803Smarcelstatic int 1006130803Smarcelmaybe_expand (struct macro_buffer *dest, 1007130803Smarcel struct macro_buffer *src_first, 1008130803Smarcel struct macro_buffer *src_rest, 1009130803Smarcel struct macro_name_list *no_loop, 1010130803Smarcel macro_lookup_ftype *lookup_func, 1011130803Smarcel void *lookup_baton) 1012130803Smarcel{ 1013130803Smarcel gdb_assert (src_first->shared); 1014130803Smarcel gdb_assert (src_rest->shared); 1015130803Smarcel gdb_assert (! dest->shared); 1016130803Smarcel 1017130803Smarcel /* Is this token an identifier? */ 1018130803Smarcel if (src_first->is_identifier) 1019130803Smarcel { 1020130803Smarcel /* Make a null-terminated copy of it, since that's what our 1021130803Smarcel lookup function expects. */ 1022130803Smarcel char *id = xmalloc (src_first->len + 1); 1023130803Smarcel struct cleanup *back_to = make_cleanup (xfree, id); 1024130803Smarcel memcpy (id, src_first->text, src_first->len); 1025130803Smarcel id[src_first->len] = 0; 1026130803Smarcel 1027130803Smarcel /* If we're currently re-scanning the result of expanding 1028130803Smarcel this macro, don't expand it again. */ 1029130803Smarcel if (! currently_rescanning (no_loop, id)) 1030130803Smarcel { 1031130803Smarcel /* Does this identifier have a macro definition in scope? */ 1032130803Smarcel struct macro_definition *def = lookup_func (id, lookup_baton); 1033130803Smarcel 1034130803Smarcel if (def && expand (id, def, dest, src_rest, no_loop, 1035130803Smarcel lookup_func, lookup_baton)) 1036130803Smarcel { 1037130803Smarcel do_cleanups (back_to); 1038130803Smarcel return 1; 1039130803Smarcel } 1040130803Smarcel } 1041130803Smarcel 1042130803Smarcel do_cleanups (back_to); 1043130803Smarcel } 1044130803Smarcel 1045130803Smarcel return 0; 1046130803Smarcel} 1047130803Smarcel 1048130803Smarcel 1049130803Smarcel/* Expand macro references in SRC, appending the results to DEST. 1050130803Smarcel Assume we are re-scanning the result of expanding the macros named 1051130803Smarcel in NO_LOOP, and don't try to re-expand references to them. 1052130803Smarcel 1053130803Smarcel SRC must be a shared buffer; DEST must not be one. */ 1054130803Smarcelstatic void 1055130803Smarcelscan (struct macro_buffer *dest, 1056130803Smarcel struct macro_buffer *src, 1057130803Smarcel struct macro_name_list *no_loop, 1058130803Smarcel macro_lookup_ftype *lookup_func, 1059130803Smarcel void *lookup_baton) 1060130803Smarcel{ 1061130803Smarcel gdb_assert (src->shared); 1062130803Smarcel gdb_assert (! dest->shared); 1063130803Smarcel 1064130803Smarcel for (;;) 1065130803Smarcel { 1066130803Smarcel struct macro_buffer tok; 1067130803Smarcel char *original_src_start = src->text; 1068130803Smarcel 1069130803Smarcel /* Find the next token in SRC. */ 1070130803Smarcel if (! get_token (&tok, src)) 1071130803Smarcel break; 1072130803Smarcel 1073130803Smarcel /* Just for aesthetics. If we skipped some whitespace, copy 1074130803Smarcel that to DEST. */ 1075130803Smarcel if (tok.text > original_src_start) 1076130803Smarcel { 1077130803Smarcel appendmem (dest, original_src_start, tok.text - original_src_start); 1078130803Smarcel dest->last_token = dest->len; 1079130803Smarcel } 1080130803Smarcel 1081130803Smarcel if (! maybe_expand (dest, &tok, src, no_loop, lookup_func, lookup_baton)) 1082130803Smarcel /* We didn't end up expanding tok as a macro reference, so 1083130803Smarcel simply append it to dest. */ 1084130803Smarcel append_tokens_without_splicing (dest, &tok); 1085130803Smarcel } 1086130803Smarcel 1087130803Smarcel /* Just for aesthetics. If there was any trailing whitespace in 1088130803Smarcel src, copy it to dest. */ 1089130803Smarcel if (src->len) 1090130803Smarcel { 1091130803Smarcel appendmem (dest, src->text, src->len); 1092130803Smarcel dest->last_token = dest->len; 1093130803Smarcel } 1094130803Smarcel} 1095130803Smarcel 1096130803Smarcel 1097130803Smarcelchar * 1098130803Smarcelmacro_expand (const char *source, 1099130803Smarcel macro_lookup_ftype *lookup_func, 1100130803Smarcel void *lookup_func_baton) 1101130803Smarcel{ 1102130803Smarcel struct macro_buffer src, dest; 1103130803Smarcel struct cleanup *back_to; 1104130803Smarcel 1105130803Smarcel init_shared_buffer (&src, (char *) source, strlen (source)); 1106130803Smarcel 1107130803Smarcel init_buffer (&dest, 0); 1108130803Smarcel dest.last_token = 0; 1109130803Smarcel back_to = make_cleanup (cleanup_macro_buffer, &dest); 1110130803Smarcel 1111130803Smarcel scan (&dest, &src, 0, lookup_func, lookup_func_baton); 1112130803Smarcel 1113130803Smarcel appendc (&dest, '\0'); 1114130803Smarcel 1115130803Smarcel discard_cleanups (back_to); 1116130803Smarcel return dest.text; 1117130803Smarcel} 1118130803Smarcel 1119130803Smarcel 1120130803Smarcelchar * 1121130803Smarcelmacro_expand_once (const char *source, 1122130803Smarcel macro_lookup_ftype *lookup_func, 1123130803Smarcel void *lookup_func_baton) 1124130803Smarcel{ 1125130803Smarcel error ("Expand-once not implemented yet."); 1126130803Smarcel} 1127130803Smarcel 1128130803Smarcel 1129130803Smarcelchar * 1130130803Smarcelmacro_expand_next (char **lexptr, 1131130803Smarcel macro_lookup_ftype *lookup_func, 1132130803Smarcel void *lookup_baton) 1133130803Smarcel{ 1134130803Smarcel struct macro_buffer src, dest, tok; 1135130803Smarcel struct cleanup *back_to; 1136130803Smarcel 1137130803Smarcel /* Set up SRC to refer to the input text, pointed to by *lexptr. */ 1138130803Smarcel init_shared_buffer (&src, *lexptr, strlen (*lexptr)); 1139130803Smarcel 1140130803Smarcel /* Set up DEST to receive the expansion, if there is one. */ 1141130803Smarcel init_buffer (&dest, 0); 1142130803Smarcel dest.last_token = 0; 1143130803Smarcel back_to = make_cleanup (cleanup_macro_buffer, &dest); 1144130803Smarcel 1145130803Smarcel /* Get the text's first preprocessing token. */ 1146130803Smarcel if (! get_token (&tok, &src)) 1147130803Smarcel { 1148130803Smarcel do_cleanups (back_to); 1149130803Smarcel return 0; 1150130803Smarcel } 1151130803Smarcel 1152130803Smarcel /* If it's a macro invocation, expand it. */ 1153130803Smarcel if (maybe_expand (&dest, &tok, &src, 0, lookup_func, lookup_baton)) 1154130803Smarcel { 1155130803Smarcel /* It was a macro invocation! Package up the expansion as a 1156130803Smarcel null-terminated string and return it. Set *lexptr to the 1157130803Smarcel start of the next token in the input. */ 1158130803Smarcel appendc (&dest, '\0'); 1159130803Smarcel discard_cleanups (back_to); 1160130803Smarcel *lexptr = src.text; 1161130803Smarcel return dest.text; 1162130803Smarcel } 1163130803Smarcel else 1164130803Smarcel { 1165130803Smarcel /* It wasn't a macro invocation. */ 1166130803Smarcel do_cleanups (back_to); 1167130803Smarcel return 0; 1168130803Smarcel } 1169130803Smarcel} 1170