OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [libcpp/] [traditional.c] - Diff between revs 154 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 154 Rev 816
/* CPP Library - traditional lexical analysis and macro expansion.
/* CPP Library - traditional lexical analysis and macro expansion.
   Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
   Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
   Contributed by Neil Booth, May 2002
   Contributed by Neil Booth, May 2002
 
 
This program is free software; you can redistribute it and/or modify it
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
Free Software Foundation; either version 2, or (at your option) any
later version.
later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 
#include "config.h"
#include "config.h"
#include "system.h"
#include "system.h"
#include "cpplib.h"
#include "cpplib.h"
#include "internal.h"
#include "internal.h"
 
 
/* The replacement text of a function-like macro is stored as a
/* The replacement text of a function-like macro is stored as a
   contiguous sequence of aligned blocks, each representing the text
   contiguous sequence of aligned blocks, each representing the text
   between subsequent parameters.
   between subsequent parameters.
 
 
   Each block comprises the text between its surrounding parameters,
   Each block comprises the text between its surrounding parameters,
   the length of that text, and the one-based index of the following
   the length of that text, and the one-based index of the following
   parameter.  The final block in the replacement text is easily
   parameter.  The final block in the replacement text is easily
   recognizable as it has an argument index of zero.  */
   recognizable as it has an argument index of zero.  */
 
 
struct block
struct block
{
{
  unsigned int text_len;
  unsigned int text_len;
  unsigned short arg_index;
  unsigned short arg_index;
  uchar text[1];
  uchar text[1];
};
};
 
 
#define BLOCK_HEADER_LEN offsetof (struct block, text)
#define BLOCK_HEADER_LEN offsetof (struct block, text)
#define BLOCK_LEN(TEXT_LEN) CPP_ALIGN (BLOCK_HEADER_LEN + (TEXT_LEN))
#define BLOCK_LEN(TEXT_LEN) CPP_ALIGN (BLOCK_HEADER_LEN + (TEXT_LEN))
 
 
/* Structure holding information about a function-like macro
/* Structure holding information about a function-like macro
   invocation.  */
   invocation.  */
struct fun_macro
struct fun_macro
{
{
  /* Memory buffer holding the trad_arg array.  */
  /* Memory buffer holding the trad_arg array.  */
  _cpp_buff *buff;
  _cpp_buff *buff;
 
 
  /* An array of size the number of macro parameters + 1, containing
  /* An array of size the number of macro parameters + 1, containing
     the offsets of the start of each macro argument in the output
     the offsets of the start of each macro argument in the output
     buffer.  The argument continues until the character before the
     buffer.  The argument continues until the character before the
     start of the next one.  */
     start of the next one.  */
  size_t *args;
  size_t *args;
 
 
  /* The hashnode of the macro.  */
  /* The hashnode of the macro.  */
  cpp_hashnode *node;
  cpp_hashnode *node;
 
 
  /* The offset of the macro name in the output buffer.  */
  /* The offset of the macro name in the output buffer.  */
  size_t offset;
  size_t offset;
 
 
  /* The line the macro name appeared on.  */
  /* The line the macro name appeared on.  */
  unsigned int line;
  unsigned int line;
 
 
  /* Zero-based index of argument being currently lexed.  */
  /* Zero-based index of argument being currently lexed.  */
  unsigned int argc;
  unsigned int argc;
};
};
 
 
/* Lexing state.  It is mostly used to prevent macro expansion.  */
/* Lexing state.  It is mostly used to prevent macro expansion.  */
enum ls {ls_none = 0,            /* Normal state.  */
enum ls {ls_none = 0,            /* Normal state.  */
         ls_fun_open,           /* When looking for '('.  */
         ls_fun_open,           /* When looking for '('.  */
         ls_fun_close,          /* When looking for ')'.  */
         ls_fun_close,          /* When looking for ')'.  */
         ls_defined,            /* After defined.  */
         ls_defined,            /* After defined.  */
         ls_defined_close,      /* Looking for ')' of defined().  */
         ls_defined_close,      /* Looking for ')' of defined().  */
         ls_hash,               /* After # in preprocessor conditional.  */
         ls_hash,               /* After # in preprocessor conditional.  */
         ls_predicate,          /* After the predicate, maybe paren?  */
         ls_predicate,          /* After the predicate, maybe paren?  */
         ls_answer};            /* In answer to predicate.  */
         ls_answer};            /* In answer to predicate.  */
 
 
/* Lexing TODO: Maybe handle space in escaped newlines.  Stop lex.c
/* Lexing TODO: Maybe handle space in escaped newlines.  Stop lex.c
   from recognizing comments and directives during its lexing pass.  */
   from recognizing comments and directives during its lexing pass.  */
 
 
static const uchar *skip_whitespace (cpp_reader *, const uchar *, int);
static const uchar *skip_whitespace (cpp_reader *, const uchar *, int);
static cpp_hashnode *lex_identifier (cpp_reader *, const uchar *);
static cpp_hashnode *lex_identifier (cpp_reader *, const uchar *);
static const uchar *copy_comment (cpp_reader *, const uchar *, int);
static const uchar *copy_comment (cpp_reader *, const uchar *, int);
static void check_output_buffer (cpp_reader *, size_t);
static void check_output_buffer (cpp_reader *, size_t);
static void push_replacement_text (cpp_reader *, cpp_hashnode *);
static void push_replacement_text (cpp_reader *, cpp_hashnode *);
static bool scan_parameters (cpp_reader *, cpp_macro *);
static bool scan_parameters (cpp_reader *, cpp_macro *);
static bool recursive_macro (cpp_reader *, cpp_hashnode *);
static bool recursive_macro (cpp_reader *, cpp_hashnode *);
static void save_replacement_text (cpp_reader *, cpp_macro *, unsigned int);
static void save_replacement_text (cpp_reader *, cpp_macro *, unsigned int);
static void maybe_start_funlike (cpp_reader *, cpp_hashnode *, const uchar *,
static void maybe_start_funlike (cpp_reader *, cpp_hashnode *, const uchar *,
                                 struct fun_macro *);
                                 struct fun_macro *);
static void save_argument (struct fun_macro *, size_t);
static void save_argument (struct fun_macro *, size_t);
static void replace_args_and_push (cpp_reader *, struct fun_macro *);
static void replace_args_and_push (cpp_reader *, struct fun_macro *);
static size_t canonicalize_text (uchar *, const uchar *, size_t, uchar *);
static size_t canonicalize_text (uchar *, const uchar *, size_t, uchar *);
 
 
/* Ensures we have N bytes' space in the output buffer, and
/* Ensures we have N bytes' space in the output buffer, and
   reallocates it if not.  */
   reallocates it if not.  */
static void
static void
check_output_buffer (cpp_reader *pfile, size_t n)
check_output_buffer (cpp_reader *pfile, size_t n)
{
{
  /* We might need two bytes to terminate an unterminated comment, and
  /* We might need two bytes to terminate an unterminated comment, and
     one more to terminate the line with a NUL.  */
     one more to terminate the line with a NUL.  */
  n += 2 + 1;
  n += 2 + 1;
 
 
  if (n > (size_t) (pfile->out.limit - pfile->out.cur))
  if (n > (size_t) (pfile->out.limit - pfile->out.cur))
    {
    {
      size_t size = pfile->out.cur - pfile->out.base;
      size_t size = pfile->out.cur - pfile->out.base;
      size_t new_size = (size + n) * 3 / 2;
      size_t new_size = (size + n) * 3 / 2;
 
 
      pfile->out.base = XRESIZEVEC (unsigned char, pfile->out.base, new_size);
      pfile->out.base = XRESIZEVEC (unsigned char, pfile->out.base, new_size);
      pfile->out.limit = pfile->out.base + new_size;
      pfile->out.limit = pfile->out.base + new_size;
      pfile->out.cur = pfile->out.base + size;
      pfile->out.cur = pfile->out.base + size;
    }
    }
}
}
 
 
/* Skip a C-style block comment in a macro as a result of -CC.
/* Skip a C-style block comment in a macro as a result of -CC.
   Buffer->cur points to the initial asterisk of the comment.  */
   Buffer->cur points to the initial asterisk of the comment.  */
static void
static void
skip_macro_block_comment (cpp_reader *pfile)
skip_macro_block_comment (cpp_reader *pfile)
{
{
  const uchar *cur = pfile->buffer->cur;
  const uchar *cur = pfile->buffer->cur;
 
 
  cur++;
  cur++;
  if (*cur == '/')
  if (*cur == '/')
    cur++;
    cur++;
 
 
  /* People like decorating comments with '*', so check for '/'
  /* People like decorating comments with '*', so check for '/'
     instead for efficiency.  */
     instead for efficiency.  */
  while(! (*cur++ == '/' && cur[-2] == '*') )
  while(! (*cur++ == '/' && cur[-2] == '*') )
    ;
    ;
 
 
  pfile->buffer->cur = cur;
  pfile->buffer->cur = cur;
}
}
 
 
/* CUR points to the asterisk introducing a comment in the current
/* CUR points to the asterisk introducing a comment in the current
   context.  IN_DEFINE is true if we are in the replacement text of a
   context.  IN_DEFINE is true if we are in the replacement text of a
   macro.
   macro.
 
 
   The asterisk and following comment is copied to the buffer pointed
   The asterisk and following comment is copied to the buffer pointed
   to by pfile->out.cur, which must be of sufficient size.
   to by pfile->out.cur, which must be of sufficient size.
   Unterminated comments are diagnosed, and correctly terminated in
   Unterminated comments are diagnosed, and correctly terminated in
   the output.  pfile->out.cur is updated depending upon IN_DEFINE,
   the output.  pfile->out.cur is updated depending upon IN_DEFINE,
   -C, -CC and pfile->state.in_directive.
   -C, -CC and pfile->state.in_directive.
 
 
   Returns a pointer to the first character after the comment in the
   Returns a pointer to the first character after the comment in the
   input buffer.  */
   input buffer.  */
static const uchar *
static const uchar *
copy_comment (cpp_reader *pfile, const uchar *cur, int in_define)
copy_comment (cpp_reader *pfile, const uchar *cur, int in_define)
{
{
  bool unterminated, copy = false;
  bool unterminated, copy = false;
  source_location src_loc = pfile->line_table->highest_line;
  source_location src_loc = pfile->line_table->highest_line;
  cpp_buffer *buffer = pfile->buffer;
  cpp_buffer *buffer = pfile->buffer;
 
 
  buffer->cur = cur;
  buffer->cur = cur;
  if (pfile->context->prev)
  if (pfile->context->prev)
    unterminated = false, skip_macro_block_comment (pfile);
    unterminated = false, skip_macro_block_comment (pfile);
  else
  else
    unterminated = _cpp_skip_block_comment (pfile);
    unterminated = _cpp_skip_block_comment (pfile);
 
 
  if (unterminated)
  if (unterminated)
    cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
    cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
                         "unterminated comment");
                         "unterminated comment");
 
 
  /* Comments in directives become spaces so that tokens are properly
  /* Comments in directives become spaces so that tokens are properly
     separated when the ISO preprocessor re-lexes the line.  The
     separated when the ISO preprocessor re-lexes the line.  The
     exception is #define.  */
     exception is #define.  */
  if (pfile->state.in_directive)
  if (pfile->state.in_directive)
    {
    {
      if (in_define)
      if (in_define)
        {
        {
          if (CPP_OPTION (pfile, discard_comments_in_macro_exp))
          if (CPP_OPTION (pfile, discard_comments_in_macro_exp))
            pfile->out.cur--;
            pfile->out.cur--;
          else
          else
            copy = true;
            copy = true;
        }
        }
      else
      else
        pfile->out.cur[-1] = ' ';
        pfile->out.cur[-1] = ' ';
    }
    }
  else if (CPP_OPTION (pfile, discard_comments))
  else if (CPP_OPTION (pfile, discard_comments))
    pfile->out.cur--;
    pfile->out.cur--;
  else
  else
    copy = true;
    copy = true;
 
 
  if (copy)
  if (copy)
    {
    {
      size_t len = (size_t) (buffer->cur - cur);
      size_t len = (size_t) (buffer->cur - cur);
      memcpy (pfile->out.cur, cur, len);
      memcpy (pfile->out.cur, cur, len);
      pfile->out.cur += len;
      pfile->out.cur += len;
      if (unterminated)
      if (unterminated)
        {
        {
          *pfile->out.cur++ = '*';
          *pfile->out.cur++ = '*';
          *pfile->out.cur++ = '/';
          *pfile->out.cur++ = '/';
        }
        }
    }
    }
 
 
  return buffer->cur;
  return buffer->cur;
}
}
 
 
/* CUR points to any character in the input buffer.  Skips over all
/* CUR points to any character in the input buffer.  Skips over all
   contiguous horizontal white space and NULs, including comments if
   contiguous horizontal white space and NULs, including comments if
   SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
   SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
   character or the end of the current context.  Escaped newlines are
   character or the end of the current context.  Escaped newlines are
   removed.
   removed.
 
 
   The whitespace is copied verbatim to the output buffer, except that
   The whitespace is copied verbatim to the output buffer, except that
   comments are handled as described in copy_comment().
   comments are handled as described in copy_comment().
   pfile->out.cur is updated.
   pfile->out.cur is updated.
 
 
   Returns a pointer to the first character after the whitespace in
   Returns a pointer to the first character after the whitespace in
   the input buffer.  */
   the input buffer.  */
static const uchar *
static const uchar *
skip_whitespace (cpp_reader *pfile, const uchar *cur, int skip_comments)
skip_whitespace (cpp_reader *pfile, const uchar *cur, int skip_comments)
{
{
  uchar *out = pfile->out.cur;
  uchar *out = pfile->out.cur;
 
 
  for (;;)
  for (;;)
    {
    {
      unsigned int c = *cur++;
      unsigned int c = *cur++;
      *out++ = c;
      *out++ = c;
 
 
      if (is_nvspace (c))
      if (is_nvspace (c))
        continue;
        continue;
 
 
      if (c == '/' && *cur == '*' && skip_comments)
      if (c == '/' && *cur == '*' && skip_comments)
        {
        {
          pfile->out.cur = out;
          pfile->out.cur = out;
          cur = copy_comment (pfile, cur, false /* in_define */);
          cur = copy_comment (pfile, cur, false /* in_define */);
          out = pfile->out.cur;
          out = pfile->out.cur;
          continue;
          continue;
        }
        }
 
 
      out--;
      out--;
      break;
      break;
    }
    }
 
 
  pfile->out.cur = out;
  pfile->out.cur = out;
  return cur - 1;
  return cur - 1;
}
}
 
 
/* Lexes and outputs an identifier starting at CUR, which is assumed
/* Lexes and outputs an identifier starting at CUR, which is assumed
   to point to a valid first character of an identifier.  Returns
   to point to a valid first character of an identifier.  Returns
   the hashnode, and updates out.cur.  */
   the hashnode, and updates out.cur.  */
static cpp_hashnode *
static cpp_hashnode *
lex_identifier (cpp_reader *pfile, const uchar *cur)
lex_identifier (cpp_reader *pfile, const uchar *cur)
{
{
  size_t len;
  size_t len;
  uchar *out = pfile->out.cur;
  uchar *out = pfile->out.cur;
  cpp_hashnode *result;
  cpp_hashnode *result;
 
 
  do
  do
    *out++ = *cur++;
    *out++ = *cur++;
  while (is_numchar (*cur));
  while (is_numchar (*cur));
 
 
  CUR (pfile->context) = cur;
  CUR (pfile->context) = cur;
  len = out - pfile->out.cur;
  len = out - pfile->out.cur;
  result = (cpp_hashnode *) ht_lookup (pfile->hash_table, pfile->out.cur,
  result = (cpp_hashnode *) ht_lookup (pfile->hash_table, pfile->out.cur,
                                       len, HT_ALLOC);
                                       len, HT_ALLOC);
  pfile->out.cur = out;
  pfile->out.cur = out;
  return result;
  return result;
}
}
 
 
/* Overlays the true file buffer temporarily with text of length LEN
/* Overlays the true file buffer temporarily with text of length LEN
   starting at START.  The true buffer is restored upon calling
   starting at START.  The true buffer is restored upon calling
   restore_buff().  */
   restore_buff().  */
void
void
_cpp_overlay_buffer (cpp_reader *pfile, const uchar *start, size_t len)
_cpp_overlay_buffer (cpp_reader *pfile, const uchar *start, size_t len)
{
{
  cpp_buffer *buffer = pfile->buffer;
  cpp_buffer *buffer = pfile->buffer;
 
 
  pfile->overlaid_buffer = buffer;
  pfile->overlaid_buffer = buffer;
  pfile->saved_cur = buffer->cur;
  pfile->saved_cur = buffer->cur;
  pfile->saved_rlimit = buffer->rlimit;
  pfile->saved_rlimit = buffer->rlimit;
  pfile->saved_line_base = buffer->next_line;
  pfile->saved_line_base = buffer->next_line;
  buffer->need_line = false;
  buffer->need_line = false;
 
 
  buffer->cur = start;
  buffer->cur = start;
  buffer->line_base = start;
  buffer->line_base = start;
  buffer->rlimit = start + len;
  buffer->rlimit = start + len;
}
}
 
 
/* Restores a buffer overlaid by _cpp_overlay_buffer().  */
/* Restores a buffer overlaid by _cpp_overlay_buffer().  */
void
void
_cpp_remove_overlay (cpp_reader *pfile)
_cpp_remove_overlay (cpp_reader *pfile)
{
{
  cpp_buffer *buffer = pfile->overlaid_buffer;
  cpp_buffer *buffer = pfile->overlaid_buffer;
 
 
  buffer->cur = pfile->saved_cur;
  buffer->cur = pfile->saved_cur;
  buffer->rlimit = pfile->saved_rlimit;
  buffer->rlimit = pfile->saved_rlimit;
  buffer->line_base = pfile->saved_line_base;
  buffer->line_base = pfile->saved_line_base;
  buffer->need_line = true;
  buffer->need_line = true;
 
 
  pfile->overlaid_buffer = NULL;
  pfile->overlaid_buffer = NULL;
}
}
 
 
/* Reads a logical line into the output buffer.  Returns TRUE if there
/* Reads a logical line into the output buffer.  Returns TRUE if there
   is more text left in the buffer.  */
   is more text left in the buffer.  */
bool
bool
_cpp_read_logical_line_trad (cpp_reader *pfile)
_cpp_read_logical_line_trad (cpp_reader *pfile)
{
{
  do
  do
    {
    {
      if (pfile->buffer->need_line && !_cpp_get_fresh_line (pfile))
      if (pfile->buffer->need_line && !_cpp_get_fresh_line (pfile))
        return false;
        return false;
    }
    }
  while (!_cpp_scan_out_logical_line (pfile, NULL) || pfile->state.skipping);
  while (!_cpp_scan_out_logical_line (pfile, NULL) || pfile->state.skipping);
 
 
  return pfile->buffer != NULL;
  return pfile->buffer != NULL;
}
}
 
 
/* Set up state for finding the opening '(' of a function-like
/* Set up state for finding the opening '(' of a function-like
   macro.  */
   macro.  */
static void
static void
maybe_start_funlike (cpp_reader *pfile, cpp_hashnode *node, const uchar *start, struct fun_macro *macro)
maybe_start_funlike (cpp_reader *pfile, cpp_hashnode *node, const uchar *start, struct fun_macro *macro)
{
{
  unsigned int n = node->value.macro->paramc + 1;
  unsigned int n = node->value.macro->paramc + 1;
 
 
  if (macro->buff)
  if (macro->buff)
    _cpp_release_buff (pfile, macro->buff);
    _cpp_release_buff (pfile, macro->buff);
  macro->buff = _cpp_get_buff (pfile, n * sizeof (size_t));
  macro->buff = _cpp_get_buff (pfile, n * sizeof (size_t));
  macro->args = (size_t *) BUFF_FRONT (macro->buff);
  macro->args = (size_t *) BUFF_FRONT (macro->buff);
  macro->node = node;
  macro->node = node;
  macro->offset = start - pfile->out.base;
  macro->offset = start - pfile->out.base;
  macro->argc = 0;
  macro->argc = 0;
}
}
 
 
/* Save the OFFSET of the start of the next argument to MACRO.  */
/* Save the OFFSET of the start of the next argument to MACRO.  */
static void
static void
save_argument (struct fun_macro *macro, size_t offset)
save_argument (struct fun_macro *macro, size_t offset)
{
{
  macro->argc++;
  macro->argc++;
  if (macro->argc <= macro->node->value.macro->paramc)
  if (macro->argc <= macro->node->value.macro->paramc)
    macro->args[macro->argc] = offset;
    macro->args[macro->argc] = offset;
}
}
 
 
/* Copies the next logical line in the current buffer (starting at
/* Copies the next logical line in the current buffer (starting at
   buffer->cur) to the output buffer.  The output is guaranteed to
   buffer->cur) to the output buffer.  The output is guaranteed to
   terminate with a NUL character.  buffer->cur is updated.
   terminate with a NUL character.  buffer->cur is updated.
 
 
   If MACRO is non-NULL, then we are scanning the replacement list of
   If MACRO is non-NULL, then we are scanning the replacement list of
   MACRO, and we call save_replacement_text() every time we meet an
   MACRO, and we call save_replacement_text() every time we meet an
   argument.  */
   argument.  */
bool
bool
_cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro)
_cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro)
{
{
  bool result = true;
  bool result = true;
  cpp_context *context;
  cpp_context *context;
  const uchar *cur;
  const uchar *cur;
  uchar *out;
  uchar *out;
  struct fun_macro fmacro;
  struct fun_macro fmacro;
  unsigned int c, paren_depth = 0, quote;
  unsigned int c, paren_depth = 0, quote;
  enum ls lex_state = ls_none;
  enum ls lex_state = ls_none;
  bool header_ok;
  bool header_ok;
  const uchar *start_of_input_line;
  const uchar *start_of_input_line;
 
 
  fmacro.buff = NULL;
  fmacro.buff = NULL;
 
 
  quote = 0;
  quote = 0;
  header_ok = pfile->state.angled_headers;
  header_ok = pfile->state.angled_headers;
  CUR (pfile->context) = pfile->buffer->cur;
  CUR (pfile->context) = pfile->buffer->cur;
  RLIMIT (pfile->context) = pfile->buffer->rlimit;
  RLIMIT (pfile->context) = pfile->buffer->rlimit;
  pfile->out.cur = pfile->out.base;
  pfile->out.cur = pfile->out.base;
  pfile->out.first_line = pfile->line_table->highest_line;
  pfile->out.first_line = pfile->line_table->highest_line;
  /* start_of_input_line is needed to make sure that directives really,
  /* start_of_input_line is needed to make sure that directives really,
     really start at the first character of the line.  */
     really start at the first character of the line.  */
  start_of_input_line = pfile->buffer->cur;
  start_of_input_line = pfile->buffer->cur;
 new_context:
 new_context:
  context = pfile->context;
  context = pfile->context;
  cur = CUR (context);
  cur = CUR (context);
  check_output_buffer (pfile, RLIMIT (context) - cur);
  check_output_buffer (pfile, RLIMIT (context) - cur);
  out = pfile->out.cur;
  out = pfile->out.cur;
 
 
  for (;;)
  for (;;)
    {
    {
      if (!context->prev
      if (!context->prev
          && cur >= pfile->buffer->notes[pfile->buffer->cur_note].pos)
          && cur >= pfile->buffer->notes[pfile->buffer->cur_note].pos)
        {
        {
          pfile->buffer->cur = cur;
          pfile->buffer->cur = cur;
          _cpp_process_line_notes (pfile, false);
          _cpp_process_line_notes (pfile, false);
        }
        }
      c = *cur++;
      c = *cur++;
      *out++ = c;
      *out++ = c;
 
 
      /* Whitespace should "continue" out of the switch,
      /* Whitespace should "continue" out of the switch,
         non-whitespace should "break" out of it.  */
         non-whitespace should "break" out of it.  */
      switch (c)
      switch (c)
        {
        {
        case ' ':
        case ' ':
        case '\t':
        case '\t':
        case '\f':
        case '\f':
        case '\v':
        case '\v':
        case '\0':
        case '\0':
          continue;
          continue;
 
 
        case '\n':
        case '\n':
          /* If this is a macro's expansion, pop it.  */
          /* If this is a macro's expansion, pop it.  */
          if (context->prev)
          if (context->prev)
            {
            {
              pfile->out.cur = out - 1;
              pfile->out.cur = out - 1;
              _cpp_pop_context (pfile);
              _cpp_pop_context (pfile);
              goto new_context;
              goto new_context;
            }
            }
 
 
          /* Omit the newline from the output buffer.  */
          /* Omit the newline from the output buffer.  */
          pfile->out.cur = out - 1;
          pfile->out.cur = out - 1;
          pfile->buffer->cur = cur;
          pfile->buffer->cur = cur;
          pfile->buffer->need_line = true;
          pfile->buffer->need_line = true;
          CPP_INCREMENT_LINE (pfile, 0);
          CPP_INCREMENT_LINE (pfile, 0);
 
 
          if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
          if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
              && !pfile->state.in_directive
              && !pfile->state.in_directive
              && _cpp_get_fresh_line (pfile))
              && _cpp_get_fresh_line (pfile))
            {
            {
              /* Newlines in arguments become a space, but we don't
              /* Newlines in arguments become a space, but we don't
                 clear any in-progress quote.  */
                 clear any in-progress quote.  */
              if (lex_state == ls_fun_close)
              if (lex_state == ls_fun_close)
                out[-1] = ' ';
                out[-1] = ' ';
              cur = pfile->buffer->cur;
              cur = pfile->buffer->cur;
              continue;
              continue;
            }
            }
          goto done;
          goto done;
 
 
        case '<':
        case '<':
          if (header_ok)
          if (header_ok)
            quote = '>';
            quote = '>';
          break;
          break;
        case '>':
        case '>':
          if (c == quote)
          if (c == quote)
            quote = 0;
            quote = 0;
          break;
          break;
 
 
        case '"':
        case '"':
        case '\'':
        case '\'':
          if (c == quote)
          if (c == quote)
            quote = 0;
            quote = 0;
          else if (!quote)
          else if (!quote)
            quote = c;
            quote = c;
          break;
          break;
 
 
        case '\\':
        case '\\':
          /* Skip escaped quotes here, it's easier than above.  */
          /* Skip escaped quotes here, it's easier than above.  */
          if (*cur == '\\' || *cur == '"' || *cur == '\'')
          if (*cur == '\\' || *cur == '"' || *cur == '\'')
            *out++ = *cur++;
            *out++ = *cur++;
          break;
          break;
 
 
        case '/':
        case '/':
          /* Traditional CPP does not recognize comments within
          /* Traditional CPP does not recognize comments within
             literals.  */
             literals.  */
          if (!quote && *cur == '*')
          if (!quote && *cur == '*')
            {
            {
              pfile->out.cur = out;
              pfile->out.cur = out;
              cur = copy_comment (pfile, cur, macro != 0);
              cur = copy_comment (pfile, cur, macro != 0);
              out = pfile->out.cur;
              out = pfile->out.cur;
              continue;
              continue;
            }
            }
          break;
          break;
 
 
        case '_':
        case '_':
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
        case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
        case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
        case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
        case 's': case 't': case 'u': case 'v': case 'w': case 'x':
        case 's': case 't': case 'u': case 'v': case 'w': case 'x':
        case 'y': case 'z':
        case 'y': case 'z':
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
        case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
        case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
        case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
        case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
        case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
        case 'Y': case 'Z':
        case 'Y': case 'Z':
          if (!pfile->state.skipping && (quote == 0 || macro))
          if (!pfile->state.skipping && (quote == 0 || macro))
            {
            {
              cpp_hashnode *node;
              cpp_hashnode *node;
              uchar *out_start = out - 1;
              uchar *out_start = out - 1;
 
 
              pfile->out.cur = out_start;
              pfile->out.cur = out_start;
              node = lex_identifier (pfile, cur - 1);
              node = lex_identifier (pfile, cur - 1);
              out = pfile->out.cur;
              out = pfile->out.cur;
              cur = CUR (context);
              cur = CUR (context);
 
 
              if (node->type == NT_MACRO
              if (node->type == NT_MACRO
                  /* Should we expand for ls_answer?  */
                  /* Should we expand for ls_answer?  */
                  && (lex_state == ls_none || lex_state == ls_fun_open)
                  && (lex_state == ls_none || lex_state == ls_fun_open)
                  && !pfile->state.prevent_expansion)
                  && !pfile->state.prevent_expansion)
                {
                {
                  /* Macros invalidate MI optimization.  */
                  /* Macros invalidate MI optimization.  */
                  pfile->mi_valid = false;
                  pfile->mi_valid = false;
                  if (! (node->flags & NODE_BUILTIN)
                  if (! (node->flags & NODE_BUILTIN)
                      && node->value.macro->fun_like)
                      && node->value.macro->fun_like)
                    {
                    {
                      maybe_start_funlike (pfile, node, out_start, &fmacro);
                      maybe_start_funlike (pfile, node, out_start, &fmacro);
                      lex_state = ls_fun_open;
                      lex_state = ls_fun_open;
                      fmacro.line = pfile->line_table->highest_line;
                      fmacro.line = pfile->line_table->highest_line;
                      continue;
                      continue;
                    }
                    }
                  else if (!recursive_macro (pfile, node))
                  else if (!recursive_macro (pfile, node))
                    {
                    {
                      /* Remove the object-like macro's name from the
                      /* Remove the object-like macro's name from the
                         output, and push its replacement text.  */
                         output, and push its replacement text.  */
                      pfile->out.cur = out_start;
                      pfile->out.cur = out_start;
                      push_replacement_text (pfile, node);
                      push_replacement_text (pfile, node);
                      lex_state = ls_none;
                      lex_state = ls_none;
                      goto new_context;
                      goto new_context;
                    }
                    }
                }
                }
              else if (macro && (node->flags & NODE_MACRO_ARG) != 0)
              else if (macro && (node->flags & NODE_MACRO_ARG) != 0)
                {
                {
                  /* Found a parameter in the replacement text of a
                  /* Found a parameter in the replacement text of a
                     #define.  Remove its name from the output.  */
                     #define.  Remove its name from the output.  */
                  pfile->out.cur = out_start;
                  pfile->out.cur = out_start;
                  save_replacement_text (pfile, macro, node->value.arg_index);
                  save_replacement_text (pfile, macro, node->value.arg_index);
                  out = pfile->out.base;
                  out = pfile->out.base;
                }
                }
              else if (lex_state == ls_hash)
              else if (lex_state == ls_hash)
                {
                {
                  lex_state = ls_predicate;
                  lex_state = ls_predicate;
                  continue;
                  continue;
                }
                }
              else if (pfile->state.in_expression
              else if (pfile->state.in_expression
                       && node == pfile->spec_nodes.n_defined)
                       && node == pfile->spec_nodes.n_defined)
                {
                {
                  lex_state = ls_defined;
                  lex_state = ls_defined;
                  continue;
                  continue;
                }
                }
            }
            }
          break;
          break;
 
 
        case '(':
        case '(':
          if (quote == 0)
          if (quote == 0)
            {
            {
              paren_depth++;
              paren_depth++;
              if (lex_state == ls_fun_open)
              if (lex_state == ls_fun_open)
                {
                {
                  if (recursive_macro (pfile, fmacro.node))
                  if (recursive_macro (pfile, fmacro.node))
                    lex_state = ls_none;
                    lex_state = ls_none;
                  else
                  else
                    {
                    {
                      lex_state = ls_fun_close;
                      lex_state = ls_fun_close;
                      paren_depth = 1;
                      paren_depth = 1;
                      out = pfile->out.base + fmacro.offset;
                      out = pfile->out.base + fmacro.offset;
                      fmacro.args[0] = fmacro.offset;
                      fmacro.args[0] = fmacro.offset;
                    }
                    }
                }
                }
              else if (lex_state == ls_predicate)
              else if (lex_state == ls_predicate)
                lex_state = ls_answer;
                lex_state = ls_answer;
              else if (lex_state == ls_defined)
              else if (lex_state == ls_defined)
                lex_state = ls_defined_close;
                lex_state = ls_defined_close;
            }
            }
          break;
          break;
 
 
        case ',':
        case ',':
          if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1)
          if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1)
            save_argument (&fmacro, out - pfile->out.base);
            save_argument (&fmacro, out - pfile->out.base);
          break;
          break;
 
 
        case ')':
        case ')':
          if (quote == 0)
          if (quote == 0)
            {
            {
              paren_depth--;
              paren_depth--;
              if (lex_state == ls_fun_close && paren_depth == 0)
              if (lex_state == ls_fun_close && paren_depth == 0)
                {
                {
                  cpp_macro *m = fmacro.node->value.macro;
                  cpp_macro *m = fmacro.node->value.macro;
 
 
                  m->used = 1;
                  m->used = 1;
                  lex_state = ls_none;
                  lex_state = ls_none;
                  save_argument (&fmacro, out - pfile->out.base);
                  save_argument (&fmacro, out - pfile->out.base);
 
 
                  /* A single zero-length argument is no argument.  */
                  /* A single zero-length argument is no argument.  */
                  if (fmacro.argc == 1
                  if (fmacro.argc == 1
                      && m->paramc == 0
                      && m->paramc == 0
                      && out == pfile->out.base + fmacro.offset + 1)
                      && out == pfile->out.base + fmacro.offset + 1)
                    fmacro.argc = 0;
                    fmacro.argc = 0;
 
 
                  if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
                  if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
                    {
                    {
                      /* Remove the macro's invocation from the
                      /* Remove the macro's invocation from the
                         output, and push its replacement text.  */
                         output, and push its replacement text.  */
                      pfile->out.cur = (pfile->out.base
                      pfile->out.cur = (pfile->out.base
                                             + fmacro.offset);
                                             + fmacro.offset);
                      CUR (context) = cur;
                      CUR (context) = cur;
                      replace_args_and_push (pfile, &fmacro);
                      replace_args_and_push (pfile, &fmacro);
                      goto new_context;
                      goto new_context;
                    }
                    }
                }
                }
              else if (lex_state == ls_answer || lex_state == ls_defined_close)
              else if (lex_state == ls_answer || lex_state == ls_defined_close)
                lex_state = ls_none;
                lex_state = ls_none;
            }
            }
          break;
          break;
 
 
        case '#':
        case '#':
          if (cur - 1 == start_of_input_line
          if (cur - 1 == start_of_input_line
              /* A '#' from a macro doesn't start a directive.  */
              /* A '#' from a macro doesn't start a directive.  */
              && !pfile->context->prev
              && !pfile->context->prev
              && !pfile->state.in_directive)
              && !pfile->state.in_directive)
            {
            {
              /* A directive.  With the way _cpp_handle_directive
              /* A directive.  With the way _cpp_handle_directive
                 currently works, we only want to call it if either we
                 currently works, we only want to call it if either we
                 know the directive is OK, or we want it to fail and
                 know the directive is OK, or we want it to fail and
                 be removed from the output.  If we want it to be
                 be removed from the output.  If we want it to be
                 passed through (the assembler case) then we must not
                 passed through (the assembler case) then we must not
                 call _cpp_handle_directive.  */
                 call _cpp_handle_directive.  */
              pfile->out.cur = out;
              pfile->out.cur = out;
              cur = skip_whitespace (pfile, cur, true /* skip_comments */);
              cur = skip_whitespace (pfile, cur, true /* skip_comments */);
              out = pfile->out.cur;
              out = pfile->out.cur;
 
 
              if (*cur == '\n')
              if (*cur == '\n')
                {
                {
                  /* Null directive.  Ignore it and don't invalidate
                  /* Null directive.  Ignore it and don't invalidate
                     the MI optimization.  */
                     the MI optimization.  */
                  pfile->buffer->need_line = true;
                  pfile->buffer->need_line = true;
                  CPP_INCREMENT_LINE (pfile, 0);
                  CPP_INCREMENT_LINE (pfile, 0);
                  result = false;
                  result = false;
                  goto done;
                  goto done;
                }
                }
              else
              else
                {
                {
                  bool do_it = false;
                  bool do_it = false;
 
 
                  if (is_numstart (*cur)
                  if (is_numstart (*cur)
                      && CPP_OPTION (pfile, lang) != CLK_ASM)
                      && CPP_OPTION (pfile, lang) != CLK_ASM)
                    do_it = true;
                    do_it = true;
                  else if (is_idstart (*cur))
                  else if (is_idstart (*cur))
                    /* Check whether we know this directive, but don't
                    /* Check whether we know this directive, but don't
                       advance.  */
                       advance.  */
                    do_it = lex_identifier (pfile, cur)->is_directive;
                    do_it = lex_identifier (pfile, cur)->is_directive;
 
 
                  if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM)
                  if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM)
                    {
                    {
                      /* This is a kludge.  We want to have the ISO
                      /* This is a kludge.  We want to have the ISO
                         preprocessor lex the next token.  */
                         preprocessor lex the next token.  */
                      pfile->buffer->cur = cur;
                      pfile->buffer->cur = cur;
                      _cpp_handle_directive (pfile, false /* indented */);
                      _cpp_handle_directive (pfile, false /* indented */);
                      result = false;
                      result = false;
                      goto done;
                      goto done;
                    }
                    }
                }
                }
            }
            }
 
 
          if (pfile->state.in_expression)
          if (pfile->state.in_expression)
            {
            {
              lex_state = ls_hash;
              lex_state = ls_hash;
              continue;
              continue;
            }
            }
          break;
          break;
 
 
        default:
        default:
          break;
          break;
        }
        }
 
 
      /* Non-whitespace disables MI optimization and stops treating
      /* Non-whitespace disables MI optimization and stops treating
         '<' as a quote in #include.  */
         '<' as a quote in #include.  */
      header_ok = false;
      header_ok = false;
      if (!pfile->state.in_directive)
      if (!pfile->state.in_directive)
        pfile->mi_valid = false;
        pfile->mi_valid = false;
 
 
      if (lex_state == ls_none)
      if (lex_state == ls_none)
        continue;
        continue;
 
 
      /* Some of these transitions of state are syntax errors.  The
      /* Some of these transitions of state are syntax errors.  The
         ISO preprocessor will issue errors later.  */
         ISO preprocessor will issue errors later.  */
      if (lex_state == ls_fun_open)
      if (lex_state == ls_fun_open)
        /* Missing '('.  */
        /* Missing '('.  */
        lex_state = ls_none;
        lex_state = ls_none;
      else if (lex_state == ls_hash
      else if (lex_state == ls_hash
               || lex_state == ls_predicate
               || lex_state == ls_predicate
               || lex_state == ls_defined)
               || lex_state == ls_defined)
        lex_state = ls_none;
        lex_state = ls_none;
 
 
      /* ls_answer and ls_defined_close keep going until ')'.  */
      /* ls_answer and ls_defined_close keep going until ')'.  */
    }
    }
 
 
 done:
 done:
  if (fmacro.buff)
  if (fmacro.buff)
    _cpp_release_buff (pfile, fmacro.buff);
    _cpp_release_buff (pfile, fmacro.buff);
 
 
  if (lex_state == ls_fun_close)
  if (lex_state == ls_fun_close)
    cpp_error_with_line (pfile, CPP_DL_ERROR, fmacro.line, 0,
    cpp_error_with_line (pfile, CPP_DL_ERROR, fmacro.line, 0,
                         "unterminated argument list invoking macro \"%s\"",
                         "unterminated argument list invoking macro \"%s\"",
                         NODE_NAME (fmacro.node));
                         NODE_NAME (fmacro.node));
  return result;
  return result;
}
}
 
 
/* Push a context holding the replacement text of the macro NODE on
/* Push a context holding the replacement text of the macro NODE on
   the context stack.  NODE is either object-like, or a function-like
   the context stack.  NODE is either object-like, or a function-like
   macro with no arguments.  */
   macro with no arguments.  */
static void
static void
push_replacement_text (cpp_reader *pfile, cpp_hashnode *node)
push_replacement_text (cpp_reader *pfile, cpp_hashnode *node)
{
{
  size_t len;
  size_t len;
  const uchar *text;
  const uchar *text;
  uchar *buf;
  uchar *buf;
 
 
  if (node->flags & NODE_BUILTIN)
  if (node->flags & NODE_BUILTIN)
    {
    {
      text = _cpp_builtin_macro_text (pfile, node);
      text = _cpp_builtin_macro_text (pfile, node);
      len = ustrlen (text);
      len = ustrlen (text);
      buf = _cpp_unaligned_alloc (pfile, len + 1);
      buf = _cpp_unaligned_alloc (pfile, len + 1);
      memcpy (buf, text, len);
      memcpy (buf, text, len);
      buf[len]='\n';
      buf[len]='\n';
      text = buf;
      text = buf;
    }
    }
  else
  else
    {
    {
      cpp_macro *macro = node->value.macro;
      cpp_macro *macro = node->value.macro;
      macro->used = 1;
      macro->used = 1;
      text = macro->exp.text;
      text = macro->exp.text;
      macro->traditional = 1;
      macro->traditional = 1;
      len = macro->count;
      len = macro->count;
    }
    }
 
 
  _cpp_push_text_context (pfile, node, text, len);
  _cpp_push_text_context (pfile, node, text, len);
}
}
 
 
/* Returns TRUE if traditional macro recursion is detected.  */
/* Returns TRUE if traditional macro recursion is detected.  */
static bool
static bool
recursive_macro (cpp_reader *pfile, cpp_hashnode *node)
recursive_macro (cpp_reader *pfile, cpp_hashnode *node)
{
{
  bool recursing = !!(node->flags & NODE_DISABLED);
  bool recursing = !!(node->flags & NODE_DISABLED);
 
 
  /* Object-like macros that are already expanding are necessarily
  /* Object-like macros that are already expanding are necessarily
     recursive.
     recursive.
 
 
     However, it is possible to have traditional function-like macros
     However, it is possible to have traditional function-like macros
     that are not infinitely recursive but recurse to any given depth.
     that are not infinitely recursive but recurse to any given depth.
     Further, it is easy to construct examples that get ever longer
     Further, it is easy to construct examples that get ever longer
     until the point they stop recursing.  So there is no easy way to
     until the point they stop recursing.  So there is no easy way to
     detect true recursion; instead we assume any expansion more than
     detect true recursion; instead we assume any expansion more than
     20 deep since the first invocation of this macro must be
     20 deep since the first invocation of this macro must be
     recursing.  */
     recursing.  */
  if (recursing && node->value.macro->fun_like)
  if (recursing && node->value.macro->fun_like)
    {
    {
      size_t depth = 0;
      size_t depth = 0;
      cpp_context *context = pfile->context;
      cpp_context *context = pfile->context;
 
 
      do
      do
        {
        {
          depth++;
          depth++;
          if (context->macro == node && depth > 20)
          if (context->macro == node && depth > 20)
            break;
            break;
          context = context->prev;
          context = context->prev;
        }
        }
      while (context);
      while (context);
      recursing = context != NULL;
      recursing = context != NULL;
    }
    }
 
 
  if (recursing)
  if (recursing)
    cpp_error (pfile, CPP_DL_ERROR,
    cpp_error (pfile, CPP_DL_ERROR,
               "detected recursion whilst expanding macro \"%s\"",
               "detected recursion whilst expanding macro \"%s\"",
               NODE_NAME (node));
               NODE_NAME (node));
 
 
  return recursing;
  return recursing;
}
}
 
 
/* Return the length of the replacement text of a function-like or
/* Return the length of the replacement text of a function-like or
   object-like non-builtin macro.  */
   object-like non-builtin macro.  */
size_t
size_t
_cpp_replacement_text_len (const cpp_macro *macro)
_cpp_replacement_text_len (const cpp_macro *macro)
{
{
  size_t len;
  size_t len;
 
 
  if (macro->fun_like && (macro->paramc != 0))
  if (macro->fun_like && (macro->paramc != 0))
    {
    {
      const uchar *exp;
      const uchar *exp;
 
 
      len = 0;
      len = 0;
      for (exp = macro->exp.text;;)
      for (exp = macro->exp.text;;)
        {
        {
          struct block *b = (struct block *) exp;
          struct block *b = (struct block *) exp;
 
 
          len += b->text_len;
          len += b->text_len;
          if (b->arg_index == 0)
          if (b->arg_index == 0)
            break;
            break;
          len += NODE_LEN (macro->params[b->arg_index - 1]);
          len += NODE_LEN (macro->params[b->arg_index - 1]);
          exp += BLOCK_LEN (b->text_len);
          exp += BLOCK_LEN (b->text_len);
        }
        }
    }
    }
  else
  else
    len = macro->count;
    len = macro->count;
 
 
  return len;
  return len;
}
}
 
 
/* Copy the replacement text of MACRO to DEST, which must be of
/* Copy the replacement text of MACRO to DEST, which must be of
   sufficient size.  It is not NUL-terminated.  The next character is
   sufficient size.  It is not NUL-terminated.  The next character is
   returned.  */
   returned.  */
uchar *
uchar *
_cpp_copy_replacement_text (const cpp_macro *macro, uchar *dest)
_cpp_copy_replacement_text (const cpp_macro *macro, uchar *dest)
{
{
  if (macro->fun_like && (macro->paramc != 0))
  if (macro->fun_like && (macro->paramc != 0))
    {
    {
      const uchar *exp;
      const uchar *exp;
 
 
      for (exp = macro->exp.text;;)
      for (exp = macro->exp.text;;)
        {
        {
          struct block *b = (struct block *) exp;
          struct block *b = (struct block *) exp;
          cpp_hashnode *param;
          cpp_hashnode *param;
 
 
          memcpy (dest, b->text, b->text_len);
          memcpy (dest, b->text, b->text_len);
          dest += b->text_len;
          dest += b->text_len;
          if (b->arg_index == 0)
          if (b->arg_index == 0)
            break;
            break;
          param = macro->params[b->arg_index - 1];
          param = macro->params[b->arg_index - 1];
          memcpy (dest, NODE_NAME (param), NODE_LEN (param));
          memcpy (dest, NODE_NAME (param), NODE_LEN (param));
          dest += NODE_LEN (param);
          dest += NODE_LEN (param);
          exp += BLOCK_LEN (b->text_len);
          exp += BLOCK_LEN (b->text_len);
        }
        }
    }
    }
  else
  else
    {
    {
      memcpy (dest, macro->exp.text, macro->count);
      memcpy (dest, macro->exp.text, macro->count);
      dest += macro->count;
      dest += macro->count;
    }
    }
 
 
  return dest;
  return dest;
}
}
 
 
/* Push a context holding the replacement text of the macro NODE on
/* Push a context holding the replacement text of the macro NODE on
   the context stack.  NODE is either object-like, or a function-like
   the context stack.  NODE is either object-like, or a function-like
   macro with no arguments.  */
   macro with no arguments.  */
static void
static void
replace_args_and_push (cpp_reader *pfile, struct fun_macro *fmacro)
replace_args_and_push (cpp_reader *pfile, struct fun_macro *fmacro)
{
{
  cpp_macro *macro = fmacro->node->value.macro;
  cpp_macro *macro = fmacro->node->value.macro;
 
 
  if (macro->paramc == 0)
  if (macro->paramc == 0)
    push_replacement_text (pfile, fmacro->node);
    push_replacement_text (pfile, fmacro->node);
  else
  else
    {
    {
      const uchar *exp;
      const uchar *exp;
      uchar *p;
      uchar *p;
      _cpp_buff *buff;
      _cpp_buff *buff;
      size_t len = 0;
      size_t len = 0;
 
 
      /* Calculate the length of the argument-replaced text.  */
      /* Calculate the length of the argument-replaced text.  */
      for (exp = macro->exp.text;;)
      for (exp = macro->exp.text;;)
        {
        {
          struct block *b = (struct block *) exp;
          struct block *b = (struct block *) exp;
 
 
          len += b->text_len;
          len += b->text_len;
          if (b->arg_index == 0)
          if (b->arg_index == 0)
            break;
            break;
          len += (fmacro->args[b->arg_index]
          len += (fmacro->args[b->arg_index]
                  - fmacro->args[b->arg_index - 1] - 1);
                  - fmacro->args[b->arg_index - 1] - 1);
          exp += BLOCK_LEN (b->text_len);
          exp += BLOCK_LEN (b->text_len);
        }
        }
 
 
      /* Allocate room for the expansion plus \n.  */
      /* Allocate room for the expansion plus \n.  */
      buff = _cpp_get_buff (pfile, len + 1);
      buff = _cpp_get_buff (pfile, len + 1);
 
 
      /* Copy the expansion and replace arguments.  */
      /* Copy the expansion and replace arguments.  */
      p = BUFF_FRONT (buff);
      p = BUFF_FRONT (buff);
      for (exp = macro->exp.text;;)
      for (exp = macro->exp.text;;)
        {
        {
          struct block *b = (struct block *) exp;
          struct block *b = (struct block *) exp;
          size_t arglen;
          size_t arglen;
 
 
          memcpy (p, b->text, b->text_len);
          memcpy (p, b->text, b->text_len);
          p += b->text_len;
          p += b->text_len;
          if (b->arg_index == 0)
          if (b->arg_index == 0)
            break;
            break;
          arglen = (fmacro->args[b->arg_index]
          arglen = (fmacro->args[b->arg_index]
                    - fmacro->args[b->arg_index - 1] - 1);
                    - fmacro->args[b->arg_index - 1] - 1);
          memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1],
          memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1],
                  arglen);
                  arglen);
          p += arglen;
          p += arglen;
          exp += BLOCK_LEN (b->text_len);
          exp += BLOCK_LEN (b->text_len);
        }
        }
 
 
      /* \n-terminate.  */
      /* \n-terminate.  */
      *p = '\n';
      *p = '\n';
      _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
      _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
 
 
      /* So we free buffer allocation when macro is left.  */
      /* So we free buffer allocation when macro is left.  */
      pfile->context->buff = buff;
      pfile->context->buff = buff;
    }
    }
}
}
 
 
/* Read and record the parameters, if any, of a function-like macro
/* Read and record the parameters, if any, of a function-like macro
   definition.  Destroys pfile->out.cur.
   definition.  Destroys pfile->out.cur.
 
 
   Returns true on success, false on failure (syntax error or a
   Returns true on success, false on failure (syntax error or a
   duplicate parameter).  On success, CUR (pfile->context) is just
   duplicate parameter).  On success, CUR (pfile->context) is just
   past the closing parenthesis.  */
   past the closing parenthesis.  */
static bool
static bool
scan_parameters (cpp_reader *pfile, cpp_macro *macro)
scan_parameters (cpp_reader *pfile, cpp_macro *macro)
{
{
  const uchar *cur = CUR (pfile->context) + 1;
  const uchar *cur = CUR (pfile->context) + 1;
  bool ok;
  bool ok;
 
 
  for (;;)
  for (;;)
    {
    {
      cur = skip_whitespace (pfile, cur, true /* skip_comments */);
      cur = skip_whitespace (pfile, cur, true /* skip_comments */);
 
 
      if (is_idstart (*cur))
      if (is_idstart (*cur))
        {
        {
          ok = false;
          ok = false;
          if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
          if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
            break;
            break;
          cur = skip_whitespace (pfile, CUR (pfile->context),
          cur = skip_whitespace (pfile, CUR (pfile->context),
                                 true /* skip_comments */);
                                 true /* skip_comments */);
          if (*cur == ',')
          if (*cur == ',')
            {
            {
              cur++;
              cur++;
              continue;
              continue;
            }
            }
          ok = (*cur == ')');
          ok = (*cur == ')');
          break;
          break;
        }
        }
 
 
      ok = (*cur == ')' && macro->paramc == 0);
      ok = (*cur == ')' && macro->paramc == 0);
      break;
      break;
    }
    }
 
 
  if (!ok)
  if (!ok)
    cpp_error (pfile, CPP_DL_ERROR, "syntax error in macro parameter list");
    cpp_error (pfile, CPP_DL_ERROR, "syntax error in macro parameter list");
 
 
  CUR (pfile->context) = cur + (*cur == ')');
  CUR (pfile->context) = cur + (*cur == ')');
 
 
  return ok;
  return ok;
}
}
 
 
/* Save the text from pfile->out.base to pfile->out.cur as
/* Save the text from pfile->out.base to pfile->out.cur as
   the replacement text for the current macro, followed by argument
   the replacement text for the current macro, followed by argument
   ARG_INDEX, with zero indicating the end of the replacement
   ARG_INDEX, with zero indicating the end of the replacement
   text.  */
   text.  */
static void
static void
save_replacement_text (cpp_reader *pfile, cpp_macro *macro,
save_replacement_text (cpp_reader *pfile, cpp_macro *macro,
                       unsigned int arg_index)
                       unsigned int arg_index)
{
{
  size_t len = pfile->out.cur - pfile->out.base;
  size_t len = pfile->out.cur - pfile->out.base;
  uchar *exp;
  uchar *exp;
 
 
  if (macro->paramc == 0)
  if (macro->paramc == 0)
    {
    {
      /* Object-like and function-like macros without parameters
      /* Object-like and function-like macros without parameters
         simply store their \n-terminated replacement text.  */
         simply store their \n-terminated replacement text.  */
      exp = _cpp_unaligned_alloc (pfile, len + 1);
      exp = _cpp_unaligned_alloc (pfile, len + 1);
      memcpy (exp, pfile->out.base, len);
      memcpy (exp, pfile->out.base, len);
      exp[len] = '\n';
      exp[len] = '\n';
      macro->exp.text = exp;
      macro->exp.text = exp;
      macro->traditional = 1;
      macro->traditional = 1;
      macro->count = len;
      macro->count = len;
    }
    }
  else
  else
    {
    {
      /* Store the text's length (unsigned int), the argument index
      /* Store the text's length (unsigned int), the argument index
         (unsigned short, base 1) and then the text.  */
         (unsigned short, base 1) and then the text.  */
      size_t blen = BLOCK_LEN (len);
      size_t blen = BLOCK_LEN (len);
      struct block *block;
      struct block *block;
 
 
      if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
      if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
        _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
        _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
 
 
      exp = BUFF_FRONT (pfile->a_buff);
      exp = BUFF_FRONT (pfile->a_buff);
      block = (struct block *) (exp + macro->count);
      block = (struct block *) (exp + macro->count);
      macro->exp.text = exp;
      macro->exp.text = exp;
      macro->traditional = 1;
      macro->traditional = 1;
 
 
      /* Write out the block information.  */
      /* Write out the block information.  */
      block->text_len = len;
      block->text_len = len;
      block->arg_index = arg_index;
      block->arg_index = arg_index;
      memcpy (block->text, pfile->out.base, len);
      memcpy (block->text, pfile->out.base, len);
 
 
      /* Lex the rest into the start of the output buffer.  */
      /* Lex the rest into the start of the output buffer.  */
      pfile->out.cur = pfile->out.base;
      pfile->out.cur = pfile->out.base;
 
 
      macro->count += blen;
      macro->count += blen;
 
 
      /* If we've finished, commit the memory.  */
      /* If we've finished, commit the memory.  */
      if (arg_index == 0)
      if (arg_index == 0)
        BUFF_FRONT (pfile->a_buff) += macro->count;
        BUFF_FRONT (pfile->a_buff) += macro->count;
    }
    }
}
}
 
 
/* Analyze and save the replacement text of a macro.  Returns true on
/* Analyze and save the replacement text of a macro.  Returns true on
   success.  */
   success.  */
bool
bool
_cpp_create_trad_definition (cpp_reader *pfile, cpp_macro *macro)
_cpp_create_trad_definition (cpp_reader *pfile, cpp_macro *macro)
{
{
  const uchar *cur;
  const uchar *cur;
  uchar *limit;
  uchar *limit;
  cpp_context *context = pfile->context;
  cpp_context *context = pfile->context;
 
 
  /* The context has not been set up for command line defines, and CUR
  /* The context has not been set up for command line defines, and CUR
     has not been updated for the macro name for in-file defines.  */
     has not been updated for the macro name for in-file defines.  */
  pfile->out.cur = pfile->out.base;
  pfile->out.cur = pfile->out.base;
  CUR (context) = pfile->buffer->cur;
  CUR (context) = pfile->buffer->cur;
  RLIMIT (context) = pfile->buffer->rlimit;
  RLIMIT (context) = pfile->buffer->rlimit;
  check_output_buffer (pfile, RLIMIT (context) - CUR (context));
  check_output_buffer (pfile, RLIMIT (context) - CUR (context));
 
 
  /* Is this a function-like macro?  */
  /* Is this a function-like macro?  */
  if (* CUR (context) == '(')
  if (* CUR (context) == '(')
    {
    {
      bool ok = scan_parameters (pfile, macro);
      bool ok = scan_parameters (pfile, macro);
 
 
      /* Remember the params so we can clear NODE_MACRO_ARG flags.  */
      /* Remember the params so we can clear NODE_MACRO_ARG flags.  */
      macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
      macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
 
 
      /* Setting macro to NULL indicates an error occurred, and
      /* Setting macro to NULL indicates an error occurred, and
         prevents unnecessary work in _cpp_scan_out_logical_line.  */
         prevents unnecessary work in _cpp_scan_out_logical_line.  */
      if (!ok)
      if (!ok)
        macro = NULL;
        macro = NULL;
      else
      else
        {
        {
          BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
          BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
          macro->fun_like = 1;
          macro->fun_like = 1;
        }
        }
    }
    }
 
 
  /* Skip leading whitespace in the replacement text.  */
  /* Skip leading whitespace in the replacement text.  */
  pfile->buffer->cur
  pfile->buffer->cur
    = skip_whitespace (pfile, CUR (context),
    = skip_whitespace (pfile, CUR (context),
                       CPP_OPTION (pfile, discard_comments_in_macro_exp));
                       CPP_OPTION (pfile, discard_comments_in_macro_exp));
 
 
  pfile->state.prevent_expansion++;
  pfile->state.prevent_expansion++;
  _cpp_scan_out_logical_line (pfile, macro);
  _cpp_scan_out_logical_line (pfile, macro);
  pfile->state.prevent_expansion--;
  pfile->state.prevent_expansion--;
 
 
  if (!macro)
  if (!macro)
    return false;
    return false;
 
 
  /* Skip trailing white space.  */
  /* Skip trailing white space.  */
  cur = pfile->out.base;
  cur = pfile->out.base;
  limit = pfile->out.cur;
  limit = pfile->out.cur;
  while (limit > cur && is_space (limit[-1]))
  while (limit > cur && is_space (limit[-1]))
    limit--;
    limit--;
  pfile->out.cur = limit;
  pfile->out.cur = limit;
  save_replacement_text (pfile, macro, 0);
  save_replacement_text (pfile, macro, 0);
 
 
  return true;
  return true;
}
}
 
 
/* Copy SRC of length LEN to DEST, but convert all contiguous
/* Copy SRC of length LEN to DEST, but convert all contiguous
   whitespace to a single space, provided it is not in quotes.  The
   whitespace to a single space, provided it is not in quotes.  The
   quote currently in effect is pointed to by PQUOTE, and is updated
   quote currently in effect is pointed to by PQUOTE, and is updated
   by the function.  Returns the number of bytes copied.  */
   by the function.  Returns the number of bytes copied.  */
static size_t
static size_t
canonicalize_text (uchar *dest, const uchar *src, size_t len, uchar *pquote)
canonicalize_text (uchar *dest, const uchar *src, size_t len, uchar *pquote)
{
{
  uchar *orig_dest = dest;
  uchar *orig_dest = dest;
  uchar quote = *pquote;
  uchar quote = *pquote;
 
 
  while (len)
  while (len)
    {
    {
      if (is_space (*src) && !quote)
      if (is_space (*src) && !quote)
        {
        {
          do
          do
            src++, len--;
            src++, len--;
          while (len && is_space (*src));
          while (len && is_space (*src));
          *dest++ = ' ';
          *dest++ = ' ';
        }
        }
      else
      else
        {
        {
          if (*src == '\'' || *src == '"')
          if (*src == '\'' || *src == '"')
            {
            {
              if (!quote)
              if (!quote)
                quote = *src;
                quote = *src;
              else if (quote == *src)
              else if (quote == *src)
                quote = 0;
                quote = 0;
            }
            }
          *dest++ = *src++, len--;
          *dest++ = *src++, len--;
        }
        }
    }
    }
 
 
  *pquote = quote;
  *pquote = quote;
  return dest - orig_dest;
  return dest - orig_dest;
}
}
 
 
/* Returns true if MACRO1 and MACRO2 have expansions different other
/* Returns true if MACRO1 and MACRO2 have expansions different other
   than in the form of their whitespace.  */
   than in the form of their whitespace.  */
bool
bool
_cpp_expansions_different_trad (const cpp_macro *macro1,
_cpp_expansions_different_trad (const cpp_macro *macro1,
                                const cpp_macro *macro2)
                                const cpp_macro *macro2)
{
{
  uchar *p1 = XNEWVEC (uchar, macro1->count + macro2->count);
  uchar *p1 = XNEWVEC (uchar, macro1->count + macro2->count);
  uchar *p2 = p1 + macro1->count;
  uchar *p2 = p1 + macro1->count;
  uchar quote1 = 0, quote2 = 0;
  uchar quote1 = 0, quote2 = 0;
  bool mismatch;
  bool mismatch;
  size_t len1, len2;
  size_t len1, len2;
 
 
  if (macro1->paramc > 0)
  if (macro1->paramc > 0)
    {
    {
      const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
      const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
 
 
      mismatch = true;
      mismatch = true;
      for (;;)
      for (;;)
        {
        {
          struct block *b1 = (struct block *) exp1;
          struct block *b1 = (struct block *) exp1;
          struct block *b2 = (struct block *) exp2;
          struct block *b2 = (struct block *) exp2;
 
 
          if (b1->arg_index != b2->arg_index)
          if (b1->arg_index != b2->arg_index)
            break;
            break;
 
 
          len1 = canonicalize_text (p1, b1->text, b1->text_len, &quote1);
          len1 = canonicalize_text (p1, b1->text, b1->text_len, &quote1);
          len2 = canonicalize_text (p2, b2->text, b2->text_len, &quote2);
          len2 = canonicalize_text (p2, b2->text, b2->text_len, &quote2);
          if (len1 != len2 || memcmp (p1, p2, len1))
          if (len1 != len2 || memcmp (p1, p2, len1))
            break;
            break;
          if (b1->arg_index == 0)
          if (b1->arg_index == 0)
            {
            {
              mismatch = false;
              mismatch = false;
              break;
              break;
            }
            }
          exp1 += BLOCK_LEN (b1->text_len);
          exp1 += BLOCK_LEN (b1->text_len);
          exp2 += BLOCK_LEN (b2->text_len);
          exp2 += BLOCK_LEN (b2->text_len);
        }
        }
    }
    }
  else
  else
    {
    {
      len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, &quote1);
      len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, &quote1);
      len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, &quote2);
      len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, &quote2);
      mismatch = (len1 != len2 || memcmp (p1, p2, len1));
      mismatch = (len1 != len2 || memcmp (p1, p2, len1));
    }
    }
 
 
  free (p1);
  free (p1);
  return mismatch;
  return mismatch;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.