OSDN Git Service

* gdbinit.in: Update to reflect new identifier structure.
[pf3gnuchains/gcc-fork.git] / gcc / cpplex.c
index f57d482..4f0767e 100644 (file)
@@ -1,10 +1,9 @@
 /* CPP Library - lexical analysis.
-   Copyright (C) 2000 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
    Contributed by Per Bothner, 1994-95.
    Based on CCCP program by Paul Rubin, June 1986
    Adapted to ANSI C, Richard Stallman, Jan 1987
    Broken out to separate file, Zack Weinberg, Mar 2000
-   Single-pass line tokenization by Neil Booth, April 2000
 
 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
@@ -20,45 +19,18 @@ You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
-/* This lexer works with a single pass of the file.  Recently I
-   re-wrote it to minimize the places where we step backwards in the
-   input stream, to make future changes to support multi-byte
-   character sets fairly straight-forward.
-
-   There is now only one routine where we do step backwards:
-   skip_escaped_newlines.  This routine could probably also be changed
-   so that it doesn't need to step back.  One possibility is to use a
-   trick similar to that used in lex_period and lex_percent.  Two
-   extra characters might be needed, but skip_escaped_newlines itself
-   would probably be the only place that needs to be aware of that,
-   and changes to the remaining routines would probably only be needed
-   if they process a backslash.  */
-
 #include "config.h"
 #include "system.h"
+#include "coretypes.h"
+#include "tm.h"
 #include "cpplib.h"
 #include "cpphash.h"
 
-/* MULTIBYTE_CHARS support only works for native compilers.
-   ??? Ideally what we want is to model widechar support after
-   the current floating point support.  */
-#ifdef CROSS_COMPILE
-#undef MULTIBYTE_CHARS
-#endif
-
-#ifdef MULTIBYTE_CHARS
-#include "mbchar.h"
-#include <locale.h>
-#endif
-
-/* Tokens with SPELL_STRING store their spelling in the token list,
-   and it's length in the token->val.name.len.  */
 enum spell_type
 {
   SPELL_OPERATOR = 0,
-  SPELL_CHAR,
   SPELL_IDENT,
-  SPELL_STRING,
+  SPELL_LITERAL,
   SPELL_NONE
 };
 
@@ -68,52 +40,41 @@ struct token_spelling
   const unsigned char *name;
 };
 
-const unsigned char *digraph_spellings [] = {U"%:", U"%:%:", U"<:",
-                                            U":>", U"<%", U"%>"};
+static const unsigned char *const digraph_spellings[] =
+{ U"%:", U"%:%:", U"<:", U":>", U"<%", U"%>" };
 
 #define OP(e, s) { SPELL_OPERATOR, U s           },
 #define TK(e, s) { s,              U STRINGX (e) },
-const struct token_spelling token_spellings [N_TTYPES] = {TTYPE_TABLE };
+static const struct token_spelling token_spellings[N_TTYPES] = { TTYPE_TABLE };
 #undef OP
 #undef TK
 
 #define TOKEN_SPELL(token) (token_spellings[(token)->type].category)
 #define TOKEN_NAME(token) (token_spellings[(token)->type].name)
 
-static cppchar_t handle_newline PARAMS ((cpp_reader *, cppchar_t));
-static cppchar_t skip_escaped_newlines PARAMS ((cpp_reader *, cppchar_t));
-static cppchar_t get_effective_char PARAMS ((cpp_reader *));
-
-static int skip_block_comment PARAMS ((cpp_reader *));
+static void add_line_note PARAMS ((cpp_buffer *, const uchar *, unsigned int));
 static int skip_line_comment PARAMS ((cpp_reader *));
-static void adjust_column PARAMS ((cpp_reader *));
 static void skip_whitespace PARAMS ((cpp_reader *, cppchar_t));
-static cpp_hashnode *parse_identifier PARAMS ((cpp_reader *));
-static cpp_hashnode *parse_identifier_slow PARAMS ((cpp_reader *,
-                                                   const U_CHAR *));
-static void parse_number PARAMS ((cpp_reader *, cpp_string *, cppchar_t, int));
-static int unescaped_terminator_p PARAMS ((cpp_reader *, const U_CHAR *));
-static void parse_string PARAMS ((cpp_reader *, cpp_token *, cppchar_t));
-static void unterminated PARAMS ((cpp_reader *, int));
-static int trigraph_ok PARAMS ((cpp_reader *, cppchar_t));
-static void save_comment PARAMS ((cpp_reader *, cpp_token *, const U_CHAR *));
-static void lex_percent PARAMS ((cpp_reader *, cpp_token *));
-static void lex_dot PARAMS ((cpp_reader *, cpp_token *));
+static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *));
+static void lex_number PARAMS ((cpp_reader *, cpp_string *));
+static bool forms_identifier_p PARAMS ((cpp_reader *, int));
+static void lex_string PARAMS ((cpp_reader *, cpp_token *, const uchar *));
+static void save_comment PARAMS ((cpp_reader *, cpp_token *, const uchar *,
+                                 cppchar_t));
+static void create_literal PARAMS ((cpp_reader *, cpp_token *, const uchar *,
+                                   unsigned int, enum cpp_ttype));
 static int name_p PARAMS ((cpp_reader *, const cpp_string *));
-static int maybe_read_ucs PARAMS ((cpp_reader *, const unsigned char **,
-                                  const unsigned char *, unsigned int *));
-static cpp_token *lex_token PARAMS ((cpp_reader *, cpp_token *));
+static cppchar_t maybe_read_ucn PARAMS ((cpp_reader *, const uchar **));
 static tokenrun *next_tokenrun PARAMS ((tokenrun *));
 
-static cpp_chunk *new_chunk PARAMS ((unsigned int));
-static int chunk_suitable PARAMS ((cpp_pool *, cpp_chunk *, unsigned int));
 static unsigned int hex_digit_value PARAMS ((unsigned int));
+static _cpp_buff *new_buff PARAMS ((size_t));
+
 
 /* Utility routine:
 
    Compares, the token TOKEN to the NUL-terminated string STRING.
    TOKEN must be a CPP_NAME.  Returns 1 for equal, 0 for unequal.  */
-
 int
 cpp_ideq (token, string)
      const cpp_token *token;
@@ -122,340 +83,259 @@ cpp_ideq (token, string)
   if (token->type != CPP_NAME)
     return 0;
 
-  return !ustrcmp (NODE_NAME (token->val.node), (const U_CHAR *) string);
+  return !ustrcmp (NODE_NAME (token->val.node), (const uchar *) string);
 }
 
-/* Call when meeting a newline.  Returns the character after the newline
-   (or carriage-return newline combination), or EOF.  */
-static cppchar_t
-handle_newline (pfile, newline_char)
-     cpp_reader *pfile;
-     cppchar_t newline_char;
+/* Record a note TYPE at byte POS into the current cleaned logical
+   line.  */
+static void
+add_line_note (buffer, pos, type)
+     cpp_buffer *buffer;
+     const uchar *pos;
+     unsigned int type;
 {
-  cpp_buffer *buffer;
-  cppchar_t next = EOF;
-
-  pfile->line++;
-  buffer = pfile->buffer;
-  buffer->col_adjust = 0;
-  buffer->line_base = buffer->cur;
-
-  /* Handle CR-LF and LF-CR combinations, get the next character.  */
-  if (buffer->cur < buffer->rlimit)
+  if (buffer->notes_used == buffer->notes_cap)
     {
-      next = *buffer->cur++;
-      if (next + newline_char == '\r' + '\n')
-       {
-         buffer->line_base = buffer->cur;
-         if (buffer->cur < buffer->rlimit)
-           next = *buffer->cur++;
-         else
-           next = EOF;
-       }
+      buffer->notes_cap = buffer->notes_cap * 2 + 200;
+      buffer->notes = (_cpp_line_note *)
+       xrealloc (buffer->notes, buffer->notes_cap * sizeof (_cpp_line_note));
     }
 
-  buffer->read_ahead = next;
-  return next;
+  buffer->notes[buffer->notes_used].pos = pos;
+  buffer->notes[buffer->notes_used].type = type;
+  buffer->notes_used++;
 }
 
-/* Subroutine of skip_escaped_newlines; called when a trigraph is
-   encountered.  It warns if necessary, and returns true if the
-   trigraph should be honoured.  FROM_CHAR is the third character of a
-   trigraph, and presumed to be the previous character for position
-   reporting.  */
-static int
-trigraph_ok (pfile, from_char)
+/* Returns with a logical line that contains no escaped newlines or
+   trigraphs.  This is a time-critical inner loop.  */
+void
+_cpp_clean_line (pfile)
      cpp_reader *pfile;
-     cppchar_t from_char;
 {
-  int accept = CPP_OPTION (pfile, trigraphs);
-  
-  /* Don't warn about trigraphs in comments.  */
-  if (CPP_OPTION (pfile, warn_trigraphs) && !pfile->state.lexing_comment)
-    {
-      cpp_buffer *buffer = pfile->buffer;
-
-      if (accept)
-       cpp_warning_with_line (pfile, pfile->line, CPP_BUF_COL (buffer) - 2,
-                              "trigraph ??%c converted to %c",
-                              (int) from_char,
-                              (int) _cpp_trigraph_map[from_char]);
-      else if (buffer->cur != buffer->last_Wtrigraphs)
-       {
-         buffer->last_Wtrigraphs = buffer->cur;
-         cpp_warning_with_line (pfile, pfile->line,
-                                CPP_BUF_COL (buffer) - 2,
-                                "trigraph ??%c ignored", (int) from_char);
-       }
-    }
-
-  return accept;
-}
+  cpp_buffer *buffer;
+  const uchar *s;
+  uchar c, *d, *p;
 
-/* Assumes local variables buffer and result.  */
-#define ACCEPT_CHAR(t) \
-  do { result->type = t; buffer->read_ahead = EOF; } while (0)
-
-/* When we move to multibyte character sets, add to these something
-   that saves and restores the state of the multibyte conversion
-   library.  This probably involves saving and restoring a "cookie".
-   In the case of glibc it is an 8-byte structure, so is not a high
-   overhead operation.  In any case, it's out of the fast path.  */
-#define SAVE_STATE() do { saved_cur = buffer->cur; } while (0)
-#define RESTORE_STATE() do { buffer->cur = saved_cur; } while (0)
-
-/* Skips any escaped newlines introduced by NEXT, which is either a
-   '?' or a '\\'.  Returns the next character, which will also have
-   been placed in buffer->read_ahead.  This routine performs
-   preprocessing stages 1 and 2 of the ISO C standard.  */
-static cppchar_t
-skip_escaped_newlines (pfile, next)
-     cpp_reader *pfile;
-     cppchar_t next;
-{
-  cpp_buffer *buffer = pfile->buffer;
+  buffer = pfile->buffer;
+  buffer->cur_note = buffer->notes_used = 0;
+  buffer->cur = buffer->line_base = buffer->next_line;
+  buffer->need_line = false;
+  s = buffer->next_line - 1;
 
-  /* Only do this if we apply stages 1 and 2.  */
   if (!buffer->from_stage3)
     {
-      cppchar_t next1;
-      const unsigned char *saved_cur;
-      int space;
+      d = (uchar *) s;
 
-      do
+      for (;;)
        {
-         if (buffer->cur == buffer->rlimit)
-           break;
-      
-         SAVE_STATE ();
-         if (next == '?')
-           {
-             next1 = *buffer->cur++;
-             if (next1 != '?' || buffer->cur == buffer->rlimit)
-               {
-                 RESTORE_STATE ();
-                 break;
-               }
+         c = *++s;
+         *++d = c;
 
-             next1 = *buffer->cur++;
-             if (!_cpp_trigraph_map[next1]
-                 || !trigraph_ok (pfile, next1))
-               {
-                 RESTORE_STATE ();
-                 break;
-               }
-
-             /* We have a full trigraph here.  */
-             next = _cpp_trigraph_map[next1];
-             if (next != '\\' || buffer->cur == buffer->rlimit)
+         if (c == '\n' || c == '\r')
+           {
+                 /* Handle DOS line endings.  */
+             if (c == '\r' && s != buffer->rlimit && s[1] == '\n')
+               s++;
+             if (s == buffer->rlimit)
                break;
-             SAVE_STATE ();
-           }
 
-         /* We have a backslash, and room for at least one more character.  */
-         space = 0;
-         do
-           {
-             next1 = *buffer->cur++;
-             if (!is_nvspace (next1))
+             /* Escaped?  */
+             p = d;
+             while (p != buffer->next_line && is_nvspace (p[-1]))
+               p--;
+             if (p == buffer->next_line || p[-1] != '\\')
                break;
-             space = 1;
-           }
-         while (buffer->cur < buffer->rlimit);
 
-         if (!is_vspace (next1))
+             add_line_note (buffer, p - 1, p != d ? ' ': '\\');
+             d = p - 2;
+             buffer->next_line = p - 1;
+           }
+         else if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
            {
-             RESTORE_STATE ();
-             break;
+             /* Add a note regardless, for the benefit of -Wtrigraphs.  */
+             add_line_note (buffer, d, s[2]);
+             if (CPP_OPTION (pfile, trigraphs))
+               {
+                 *d = _cpp_trigraph_map[s[2]];
+                 s += 2;
+               }
            }
-
-         if (space && !pfile->state.lexing_comment)
-           cpp_warning (pfile, "backslash and newline separated by space");
-
-         next = handle_newline (pfile, next1);
-         if (next == EOF)
-           cpp_pedwarn (pfile, "backslash-newline at end of file");
        }
-      while (next == '\\' || next == '?');
+    }
+  else
+    {
+      do
+       s++;
+      while (*s != '\n' && *s != '\r');
+      d = (uchar *) s;
+
+      /* Handle DOS line endings.  */
+      if (*s == '\r' && s != buffer->rlimit && s[1] == '\n')
+       s++;
     }
 
-  buffer->read_ahead = next;
-  return next;
+  *d = '\n';
+  /* A sentinel note that should never be processed.  */
+  add_line_note (buffer, d + 1, '\n');
+  buffer->next_line = s + 1;
 }
 
-/* Obtain the next character, after trigraph conversion and skipping
-   an arbitrary string of escaped newlines.  The common case of no
-   trigraphs or escaped newlines falls through quickly.  */
-static cppchar_t
-get_effective_char (pfile)
+/* Process the notes created by add_line_note as far as the current
+   location.  */
+void
+_cpp_process_line_notes (pfile, in_comment)
      cpp_reader *pfile;
+     int in_comment;
 {
   cpp_buffer *buffer = pfile->buffer;
-  cppchar_t next = EOF;
 
-  if (buffer->cur < buffer->rlimit)
+  for (;;)
     {
-      next = *buffer->cur++;
-
-      /* '?' can introduce trigraphs (and therefore backslash); '\\'
-        can introduce escaped newlines, which we want to skip, or
-        UCNs, which, depending upon lexer state, we will handle in
-        the future.  */
-      if (next == '?' || next == '\\')
-       next = skip_escaped_newlines (pfile, next);
-    }
+      _cpp_line_note *note = &buffer->notes[buffer->cur_note];
+      unsigned int col;
+
+      if (note->pos > buffer->cur)
+       break;
+
+      buffer->cur_note++;
+      col = CPP_BUF_COLUMN (buffer, note->pos + 1);
+
+      if (note->type == '\\' || note->type == ' ')
+       {
+         if (note->type == ' ' && !in_comment)
+           cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
+                                "backslash and newline separated by space");
 
-  buffer->read_ahead = next;
-  return next;
+         if (buffer->next_line > buffer->rlimit)
+           {
+             cpp_error_with_line (pfile, DL_PEDWARN, pfile->line, col,
+                                  "backslash-newline at end of file");
+             /* Prevent "no newline at end of file" warning.  */
+             buffer->next_line = buffer->rlimit;
+           }
+
+         buffer->line_base = note->pos;
+         pfile->line++;
+       }
+      else if (_cpp_trigraph_map[note->type])
+       {
+         if (!in_comment && CPP_OPTION (pfile, warn_trigraphs))
+           {
+             if (CPP_OPTION (pfile, trigraphs))
+               cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
+                                    "trigraph ??%c converted to %c",
+                                    note->type,
+                                    (int) _cpp_trigraph_map[note->type]);
+             else
+               cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
+                                    "trigraph ??%c ignored",
+                                    note->type);
+           }
+       }
+      else
+       abort ();
+    }
 }
 
 /* Skip a C-style block comment.  We find the end of the comment by
    seeing if an asterisk is before every '/' we encounter.  Returns
-   non-zero if comment terminated by EOF, zero otherwise.  */
-static int
-skip_block_comment (pfile)
+   nonzero if comment terminated by EOF, zero otherwise.
+
+   Buffer->cur points to the initial asterisk of the comment.  */
+bool
+_cpp_skip_block_comment (pfile)
      cpp_reader *pfile;
 {
   cpp_buffer *buffer = pfile->buffer;
-  cppchar_t c = EOF, prevc = EOF;
+  cppchar_t c;
 
-  pfile->state.lexing_comment = 1;
-  while (buffer->cur != buffer->rlimit)
-    {
-      prevc = c, c = *buffer->cur++;
+  buffer->cur++;
+  if (*buffer->cur == '/')
+    buffer->cur++;
 
-    next_char:
-      /* FIXME: For speed, create a new character class of characters
-        of interest inside block comments.  */
-      if (c == '?' || c == '\\')
-       c = skip_escaped_newlines (pfile, c);
+  for (;;)
+    {
+      c = *buffer->cur++;
 
       /* People like decorating comments with '*', so check for '/'
         instead for efficiency.  */
       if (c == '/')
        {
-         if (prevc == '*')
+         if (buffer->cur[-2] == '*')
            break;
 
          /* Warn about potential nested comments, but not if the '/'
-            comes immediately before the true comment delimeter.
+            comes immediately before the true comment delimiter.
             Don't bother to get it right across escaped newlines.  */
          if (CPP_OPTION (pfile, warn_comments)
-             && buffer->cur != buffer->rlimit)
-           {
-             prevc = c, c = *buffer->cur++;
-             if (c == '*' && buffer->cur != buffer->rlimit)
-               {
-                 prevc = c, c = *buffer->cur++;
-                 if (c != '/') 
-                   cpp_warning_with_line (pfile, pfile->line,
-                                          CPP_BUF_COL (buffer) - 2,
-                                          "\"/*\" within comment");
-               }
-             goto next_char;
-           }
+             && buffer->cur[0] == '*' && buffer->cur[1] != '/')
+           cpp_error_with_line (pfile, DL_WARNING,
+                                pfile->line, CPP_BUF_COL (buffer),
+                                "\"/*\" within comment");
        }
-      else if (is_vspace (c))
+      else if (c == '\n')
        {
-         prevc = c, c = handle_newline (pfile, c);
-         goto next_char;
+         buffer->cur--;
+         _cpp_process_line_notes (pfile, true);
+         if (buffer->next_line >= buffer->rlimit)
+           return true;
+         _cpp_clean_line (pfile);
+         pfile->line++;
        }
-      else if (c == '\t')
-       adjust_column (pfile);
     }
 
-  pfile->state.lexing_comment = 0;
-  buffer->read_ahead = EOF;
-  return c != '/' || prevc != '*';
+  return false;
 }
 
-/* Skip a C++ line comment.  Handles escaped newlines.  Returns
-   non-zero if a multiline comment.  The following new line, if any,
-   is left in buffer->read_ahead.  */
+/* Skip a C++ line comment, leaving buffer->cur pointing to the
+   terminating newline.  Handles escaped newlines.  Returns nonzero
+   if a multiline comment.  */
 static int
 skip_line_comment (pfile)
      cpp_reader *pfile;
 {
   cpp_buffer *buffer = pfile->buffer;
   unsigned int orig_line = pfile->line;
-  cppchar_t c;
-
-  pfile->state.lexing_comment = 1;
-  do
-    {
-      c = EOF;
-      if (buffer->cur == buffer->rlimit)
-       break;
 
-      c = *buffer->cur++;
-      if (c == '?' || c == '\\')
-       c = skip_escaped_newlines (pfile, c);
-    }
-  while (!is_vspace (c));
+  while (*buffer->cur != '\n')
+    buffer->cur++;
 
-  pfile->state.lexing_comment = 0;
-  buffer->read_ahead = c;      /* Leave any newline for caller.  */
+  _cpp_process_line_notes (pfile, true);
   return orig_line != pfile->line;
 }
 
-/* pfile->buffer->cur is one beyond the \t character.  Update
-   col_adjust so we track the column correctly.  */
-static void
-adjust_column (pfile)
-     cpp_reader *pfile;
-{
-  cpp_buffer *buffer = pfile->buffer;
-  unsigned int col = CPP_BUF_COL (buffer) - 1; /* Zero-based column.  */
-
-  /* Round it up to multiple of the tabstop, but subtract 1 since the
-     tab itself occupies a character position.  */
-  buffer->col_adjust += (CPP_OPTION (pfile, tabstop)
-                        - col % CPP_OPTION (pfile, tabstop)) - 1;
-}
-
-/* Skips whitespace, saving the next non-whitespace character.
-   Adjusts pfile->col_adjust to account for tabs.  Without this,
-   tokens might be assigned an incorrect column.  */
+/* Skips whitespace, saving the next non-whitespace character.  */
 static void
 skip_whitespace (pfile, c)
      cpp_reader *pfile;
      cppchar_t c;
 {
   cpp_buffer *buffer = pfile->buffer;
-  unsigned int warned = 0;
+  bool saw_NUL = false;
 
   do
     {
       /* Horizontal space always OK.  */
-      if (c == ' ')
+      if (c == ' ' || c == '\t')
        ;
-      else if (c == '\t')
-       adjust_column (pfile);
       /* Just \f \v or \0 left.  */
       else if (c == '\0')
-       {
-         if (!warned)
-           {
-             cpp_warning (pfile, "null character(s) ignored");
-             warned = 1;
-           }
-       }
+       saw_NUL = true;
       else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
-       cpp_pedwarn_with_line (pfile, pfile->line,
-                              CPP_BUF_COL (buffer),
-                              "%s in preprocessing directive",
-                              c == '\f' ? "form feed" : "vertical tab");
+       cpp_error_with_line (pfile, DL_PEDWARN, pfile->line,
+                            CPP_BUF_COL (buffer),
+                            "%s in preprocessing directive",
+                            c == '\f' ? "form feed" : "vertical tab");
 
-      c = EOF;
-      if (buffer->cur == buffer->rlimit)
-       break;
       c = *buffer->cur++;
     }
   /* We only want non-vertical space, i.e. ' ' \t \f \v \0.  */
   while (is_nvspace (c));
 
-  /* Remember the next character.  */
-  buffer->read_ahead = c;
+  if (saw_NUL)
+    cpp_error (pfile, DL_WARNING, "null character(s) ignored");
+
+  buffer->cur--;
 }
 
 /* See if the characters of a number token are valid in a name (no
@@ -471,438 +351,236 @@ name_p (pfile, string)
     if (!is_idchar (string->text[i]))
       return 0;
 
-  return 1;  
+  return 1;
 }
 
-/* Parse an identifier, skipping embedded backslash-newlines.  This is
-   a critical inner loop.  The common case is an identifier which has
-   not been split by backslash-newline, does not contain a dollar
-   sign, and has already been scanned (roughly 10:1 ratio of
-   seen:unseen identifiers in normal code; the distribution is
-   Poisson-like).  Second most common case is a new identifier, not
-   split and no dollar sign.  The other possibilities are rare and
-   have been relegated to parse_identifier_slow.  */
+/* Returns TRUE if the sequence starting at buffer->cur is invalid in
+   an identifier.  FIRST is TRUE if this starts an identifier.  */
+static bool
+forms_identifier_p (pfile, first)
+     cpp_reader *pfile;
+     int first;
+{
+  cpp_buffer *buffer = pfile->buffer;
+
+  if (*buffer->cur == '$')
+    {
+      if (!CPP_OPTION (pfile, dollars_in_ident))
+       return false;
+
+      buffer->cur++;
+      if (CPP_PEDANTIC (pfile)
+         && !pfile->state.skipping
+         && !pfile->warned_dollar)
+       {
+         pfile->warned_dollar = true;
+         cpp_error (pfile, DL_PEDWARN, "'$' in identifier or number");
+       }
+
+      return true;
+    }
+
+  /* Is this a syntactically valid UCN?  */
+  if (0 && *buffer->cur == '\\'
+      && (buffer->cur[1] == 'u' || buffer->cur[1] == 'U'))
+    {
+      buffer->cur += 2;
+      if (_cpp_valid_ucn (pfile, &buffer->cur, 1 + !first))
+       return true;
+      buffer->cur -= 2;
+    }
 
+  return false;
+}
+
+/* Lex an identifier starting at BUFFER->CUR - 1.  */
 static cpp_hashnode *
-parse_identifier (pfile)
+lex_identifier (pfile, base)
      cpp_reader *pfile;
+     const uchar *base;
 {
   cpp_hashnode *result;
-  const U_CHAR *cur, *rlimit;
+  const uchar *cur;
 
-  /* Fast-path loop.  Skim over a normal identifier.
-     N.B. ISIDNUM does not include $.  */
-  cur    = pfile->buffer->cur - 1;
-  rlimit = pfile->buffer->rlimit;
   do
-    cur++;
-  while (cur < rlimit && ISIDNUM (*cur));
-
-  /* Check for slow-path cases.  */
-  if (cur < rlimit && (*cur == '?' || *cur == '\\' || *cur == '$'))
-    result = parse_identifier_slow (pfile, cur);
-  else
     {
-      const U_CHAR *base = pfile->buffer->cur - 1;
-      result = (cpp_hashnode *)
-       ht_lookup (pfile->hash_table, base, cur - base, HT_ALLOC);
+      cur = pfile->buffer->cur;
+
+      /* N.B. ISIDNUM does not include $.  */
+      while (ISIDNUM (*cur))
+       cur++;
+
       pfile->buffer->cur = cur;
     }
+  while (forms_identifier_p (pfile, false));
 
-  /* Rarely, identifiers require diagnostics when lexed.
-     XXX Has to be forced out of the fast path.  */
+  result = (cpp_hashnode *)
+    ht_lookup (pfile->hash_table, base, cur - base, HT_ALLOC);
+
+  /* Rarely, identifiers require diagnostics when lexed.  */
   if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
                        && !pfile->state.skipping, 0))
     {
       /* It is allowed to poison the same identifier twice.  */
       if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
-       cpp_error (pfile, "attempt to use poisoned \"%s\"",
+       cpp_error (pfile, DL_ERROR, "attempt to use poisoned \"%s\"",
                   NODE_NAME (result));
 
       /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
         replacement list of a variadic macro.  */
       if (result == pfile->spec_nodes.n__VA_ARGS__
          && !pfile->state.va_args_ok)
-       cpp_pedwarn (pfile,
+       cpp_error (pfile, DL_PEDWARN,
        "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
     }
 
   return result;
 }
 
-/* Slow path.  This handles identifiers which have been split, and
-   identifiers which contain dollar signs.  The part of the identifier
-   from PFILE->buffer->cur-1 to CUR has already been scanned.  */
-static cpp_hashnode *
-parse_identifier_slow (pfile, cur)
-     cpp_reader *pfile;
-     const U_CHAR *cur;
-{
-  cpp_buffer *buffer = pfile->buffer;
-  const U_CHAR *base = buffer->cur - 1;
-  struct obstack *stack = &pfile->hash_table->stack;
-  unsigned int c, saw_dollar = 0, len;
-
-  /* Copy the part of the token which is known to be okay.  */
-  obstack_grow (stack, base, cur - base);
-
-  /* Now process the part which isn't.  We are looking at one of
-     '$', '\\', or '?' on entry to this loop.  */
-  c = *cur++;
-  buffer->cur = cur;
-  do
-    {
-      while (is_idchar (c))
-        {
-          obstack_1grow (stack, c);
-
-          if (c == '$')
-            saw_dollar++;
-
-          c = EOF;
-          if (buffer->cur == buffer->rlimit)
-            break;
-
-          c = *buffer->cur++;
-        }
-
-      /* Potential escaped newline?  */
-      if (c != '?' && c != '\\')
-        break;
-      c = skip_escaped_newlines (pfile, c);
-    }
-  while (is_idchar (c));
-
-  /* Remember the next character.  */
-  buffer->read_ahead = c;
-
-  /* $ is not a identifier character in the standard, but is commonly
-     accepted as an extension.  Don't warn about it in skipped
-     conditional blocks.  */
-  if (saw_dollar && CPP_PEDANTIC (pfile) && ! pfile->state.skipping)
-    cpp_pedwarn (pfile, "'$' character(s) in identifier");
-
-  /* Identifiers are null-terminated.  */
-  len = obstack_object_size (stack);
-  obstack_1grow (stack, '\0');
-
-  return (cpp_hashnode *)
-    ht_lookup (pfile->hash_table, obstack_finish (stack), len, HT_ALLOCED);
-}
-
-/* Parse a number, skipping embedded backslash-newlines.  */
+/* Lex a number to NUMBER starting at BUFFER->CUR - 1.  */
 static void
-parse_number (pfile, number, c, leading_period)
+lex_number (pfile, number)
      cpp_reader *pfile;
      cpp_string *number;
-     cppchar_t c;
-     int leading_period;
 {
-  cpp_buffer *buffer = pfile->buffer;
-  cpp_pool *pool = &pfile->ident_pool;
-  unsigned char *dest, *limit;
+  const uchar *cur;
+  const uchar *base;
+  uchar *dest;
 
-  dest = POOL_FRONT (pool);
-  limit = POOL_LIMIT (pool);
-
-  /* Place a leading period.  */
-  if (leading_period)
-    {
-      if (dest >= limit)
-       limit = _cpp_next_chunk (pool, 0, &dest);
-      *dest++ = '.';
-    }
-  
+  base = pfile->buffer->cur - 1;
   do
     {
-      do
-       {
-         /* Need room for terminating null.  */
-         if (dest + 1 >= limit)
-           limit = _cpp_next_chunk (pool, 0, &dest);
-         *dest++ = c;
+      cur = pfile->buffer->cur;
 
-         c = EOF;
-         if (buffer->cur == buffer->rlimit)
-           break;
-
-         c = *buffer->cur++;
-       }
-      while (is_numchar (c) || c == '.' || VALID_SIGN (c, dest[-1]));
+      /* N.B. ISIDNUM does not include $.  */
+      while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1]))
+       cur++;
 
-      /* Potential escaped newline?  */
-      if (c != '?' && c != '\\')
-       break;
-      c = skip_escaped_newlines (pfile, c);
+      pfile->buffer->cur = cur;
     }
-  while (is_numchar (c) || c == '.' || VALID_SIGN (c, dest[-1]));
+  while (forms_identifier_p (pfile, false));
 
-  /* Remember the next character.  */
-  buffer->read_ahead = c;
-
-  /* Null-terminate the number.  */
-  *dest = '\0';
-
-  number->text = POOL_FRONT (pool);
-  number->len = dest - number->text;
-  POOL_COMMIT (pool, number->len + 1);
+  number->len = cur - base;
+  dest = _cpp_unaligned_alloc (pfile, number->len + 1);
+  memcpy (dest, base, number->len);
+  dest[number->len] = '\0';
+  number->text = dest;
 }
 
-/* Subroutine of parse_string.  Emits error for unterminated strings.  */
+/* Create a token of type TYPE with a literal spelling.  */
 static void
-unterminated (pfile, term)
+create_literal (pfile, token, base, len, type)
      cpp_reader *pfile;
-     int term;
-{
-  cpp_error (pfile, "missing terminating %c character", term);
-
-  if (term == '\"' && pfile->mls_line && pfile->mls_line != pfile->line)
-    {
-      cpp_error_with_line (pfile, pfile->mls_line, pfile->mls_col,
-                          "possible start of unterminated string literal");
-      pfile->mls_line = 0;
-    }
-}
-
-/* Subroutine of parse_string.  */
-static int
-unescaped_terminator_p (pfile, dest)
-     cpp_reader *pfile;
-     const unsigned char *dest;
+     cpp_token *token;
+     const uchar *base;
+     unsigned int len;
+     enum cpp_ttype type;
 {
-  const unsigned char *start, *temp;
-
-  /* In #include-style directives, terminators are not escapeable.  */
-  if (pfile->state.angled_headers)
-    return 1;
-
-  start = POOL_FRONT (&pfile->ident_pool);
+  uchar *dest = _cpp_unaligned_alloc (pfile, len + 1);
 
-  /* An odd number of consecutive backslashes represents an escaped
-     terminator.  */
-  for (temp = dest; temp > start && temp[-1] == '\\'; temp--)
-    ;
-
-  return ((dest - temp) & 1) == 0;
+  memcpy (dest, base, len);
+  dest[len] = '\0';
+  token->type = type;
+  token->val.str.len = len;
+  token->val.str.text = dest;
 }
 
-/* Parses a string, character constant, or angle-bracketed header file
-   name.  Handles embedded trigraphs and escaped newlines.  The stored
-   string is guaranteed NUL-terminated, but it is not guaranteed that
-   this is the first NUL since embedded NULs are preserved.
+/* Lexes a string, character constant, or angle-bracketed header file
+   name.  The stored string contains the spelling, including opening
+   quote and leading any leading 'L'.  It returns the type of the
+   literal, or CPP_OTHER if it was not properly terminated.
 
-   Multi-line strings are allowed, but they are deprecated.  */
+   The spelling is NUL-terminated, but it is not guaranteed that this
+   is the first NUL since embedded NULs are preserved.  */
 static void
-parse_string (pfile, token, terminator)
+lex_string (pfile, token, base)
      cpp_reader *pfile;
      cpp_token *token;
-     cppchar_t terminator;
+     const uchar *base;
 {
-  cpp_buffer *buffer = pfile->buffer;
-  cpp_pool *pool = &pfile->ident_pool;
-  unsigned char *dest, *limit;
-  cppchar_t c;
-  bool warned_nulls = false, warned_multi = false;
-
-  dest = POOL_FRONT (pool);
-  limit = POOL_LIMIT (pool);
+  bool saw_NUL = false;
+  const uchar *cur;
+  cppchar_t terminator;
+  enum cpp_ttype type;
+
+  cur = base;
+  terminator = *cur++;
+  if (terminator == 'L')
+    terminator = *cur++;
+  if (terminator == '\"')
+    type = *base == 'L' ? CPP_WSTRING: CPP_STRING;
+  else if (terminator == '\'')
+    type = *base == 'L' ? CPP_WCHAR: CPP_CHAR;
+  else
+    terminator = '>', type = CPP_HEADER_NAME;
 
   for (;;)
     {
-      if (buffer->cur == buffer->rlimit)
-       c = EOF;
-      else
-       c = *buffer->cur++;
-
-    have_char:
-      /* We need space for the terminating NUL.  */
-      if (dest >= limit)
-       limit = _cpp_next_chunk (pool, 0, &dest);
-
-      if (c == EOF)
-       {
-         unterminated (pfile, terminator);
-         break;
-       }
-
-      /* Handle trigraphs, escaped newlines etc.  */
-      if (c == '?' || c == '\\')
-       c = skip_escaped_newlines (pfile, c);
+      cppchar_t c = *cur++;
 
-      if (c == terminator && unescaped_terminator_p (pfile, dest))
+      /* In #include-style directives, terminators are not escapable.  */
+      if (c == '\\' && !pfile->state.angled_headers && *cur != '\n')
+       cur++;
+      else if (c == terminator)
+       break;
+      else if (c == '\n')
        {
-         c = EOF;
+         cur--;
+         type = CPP_OTHER;
          break;
        }
-      else if (is_vspace (c))
-       {
-         /* In assembly language, silently terminate string and
-            character literals at end of line.  This is a kludge
-            around not knowing where comments are.  */
-         if (CPP_OPTION (pfile, lang) == CLK_ASM && terminator != '>')
-           break;
-
-         /* Character constants and header names may not extend over
-            multiple lines.  In Standard C, neither may strings.
-            Unfortunately, we accept multiline strings as an
-            extension, except in #include family directives.  */
-         if (terminator != '"' || pfile->state.angled_headers)
-           {
-             unterminated (pfile, terminator);
-             break;
-           }
-
-         if (!warned_multi)
-           {
-             warned_multi = true;
-             cpp_pedwarn (pfile, "multi-line string literals are deprecated");
-           }
-
-         if (pfile->mls_line == 0)
-           {
-             pfile->mls_line = token->line;
-             pfile->mls_col = token->col;
-           }
-             
-         c = handle_newline (pfile, c);
-         *dest++ = '\n';
-         goto have_char;
-       }
-      else if (c == '\0' && !warned_nulls)
-       {
-         warned_nulls = true;
-         cpp_warning (pfile, "null character(s) preserved in literal");
-       }
-
-      *dest++ = c;
+      else if (c == '\0')
+       saw_NUL = true;
     }
 
-  /* Remember the next character.  */
-  buffer->read_ahead = c;
-  *dest = '\0';
+  if (saw_NUL && !pfile->state.skipping)
+    cpp_error (pfile, DL_WARNING, "null character(s) preserved in literal");
 
-  token->val.str.text = POOL_FRONT (pool);
-  token->val.str.len = dest - token->val.str.text;
-  POOL_COMMIT (pool, token->val.str.len + 1);
+  pfile->buffer->cur = cur;
+  create_literal (pfile, token, base, cur - base, type);
 }
 
 /* The stored comment includes the comment start and any terminator.  */
 static void
-save_comment (pfile, token, from)
+save_comment (pfile, token, from, type)
      cpp_reader *pfile;
      cpp_token *token;
      const unsigned char *from;
+     cppchar_t type;
 {
   unsigned char *buffer;
-  unsigned int len;
-  
+  unsigned int len, clen;
+
   len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'.  */
+
   /* C++ comments probably (not definitely) have moved past a new
      line, which we don't want to save in the comment.  */
-  if (pfile->buffer->read_ahead != EOF)
+  if (is_vspace (pfile->buffer->cur[-1]))
     len--;
-  buffer = _cpp_pool_alloc (&pfile->ident_pool, len);
-  
-  token->type = CPP_COMMENT;
-  token->val.str.len = len;
-  token->val.str.text = buffer;
-
-  buffer[0] = '/';
-  memcpy (buffer + 1, from, len - 1);
-}
 
-/* Subroutine of lex_token to handle '%'.  A little tricky, since we
-   want to avoid stepping back when lexing %:%X.  */
-static void
-lex_percent (pfile, result)
-     cpp_reader *pfile;
-     cpp_token *result;
-{
-  cpp_buffer *buffer= pfile->buffer;
-  cppchar_t c;
+  /* If we are currently in a directive, then we need to store all
+     C++ comments as C comments internally, and so we need to
+     allocate a little extra space in that case.
 
-  result->type = CPP_MOD;
-  /* Parsing %:%X could leave an extra character.  */
-  if (buffer->extra_char == EOF)
-    c = get_effective_char (pfile);
-  else
-    {
-      c = buffer->read_ahead = buffer->extra_char;
-      buffer->extra_char = EOF;
-    }
+     Note that the only time we encounter a directive here is
+     when we are saving comments in a "#define".  */
+  clen = (pfile->state.in_directive && type == '/') ? len + 2 : len;
 
-  if (c == '=')
-    ACCEPT_CHAR (CPP_MOD_EQ);
-  else if (CPP_OPTION (pfile, digraphs))
-    {
-      if (c == ':')
-       {
-         result->flags |= DIGRAPH;
-         ACCEPT_CHAR (CPP_HASH);
-         if (get_effective_char (pfile) == '%')
-           {
-             buffer->extra_char = get_effective_char (pfile);
-             if (buffer->extra_char == ':')
-               {
-                 buffer->extra_char = EOF;
-                 ACCEPT_CHAR (CPP_PASTE);
-               }
-             else
-               /* We'll catch the extra_char when we're called back.  */
-               buffer->read_ahead = '%';
-           }
-       }
-      else if (c == '>')
-       {
-         result->flags |= DIGRAPH;
-         ACCEPT_CHAR (CPP_CLOSE_BRACE);
-       }
-    }
-}
+  buffer = _cpp_unaligned_alloc (pfile, clen);
 
-/* Subroutine of lex_token to handle '.'.  This is tricky, since we
-   want to avoid stepping back when lexing '...' or '.123'.  In the
-   latter case we should also set a flag for parse_number.  */
-static void
-lex_dot (pfile, result)
-     cpp_reader *pfile;
-     cpp_token *result;
-{
-  cpp_buffer *buffer = pfile->buffer;
-  cppchar_t c;
+  token->type = CPP_COMMENT;
+  token->val.str.len = clen;
+  token->val.str.text = buffer;
 
-  /* Parsing ..X could leave an extra character.  */
-  if (buffer->extra_char == EOF)
-    c = get_effective_char (pfile);
-  else
-    {
-      c = buffer->read_ahead = buffer->extra_char;
-      buffer->extra_char = EOF;
-    }
+  buffer[0] = '/';
+  memcpy (buffer + 1, from, len - 1);
 
-  /* All known character sets have 0...9 contiguous.  */
-  if (c >= '0' && c <= '9')
-    {
-      result->type = CPP_NUMBER;
-      parse_number (pfile, &result->val.str, c, 1);
-    }
-  else
+  /* Finish conversion to a C comment, if necessary.  */
+  if (pfile->state.in_directive && type == '/')
     {
-      result->type = CPP_DOT;
-      if (c == '.')
-       {
-         buffer->extra_char = get_effective_char (pfile);
-         if (buffer->extra_char == '.')
-           {
-             buffer->extra_char = EOF;
-             ACCEPT_CHAR (CPP_ELLIPSIS);
-           }
-         else
-           /* We'll catch the extra_char when we're called back.  */
-           buffer->read_ahead = '.';
-       }
-      else if (c == '*' && CPP_OPTION (pfile, cplusplus))
-       ACCEPT_CHAR (CPP_DOT_STAR);
+      buffer[1] = '*';
+      buffer[clen - 2] = '*';
+      buffer[clen - 1] = '/';
     }
 }
 
@@ -932,11 +610,35 @@ next_tokenrun (run)
   return run->next;
 }
 
-/* Lex a token into RESULT (external interface).  */
-void
-_cpp_lex_token (pfile, dest)
+/* Allocate a single token that is invalidated at the same time as the
+   rest of the tokens on the line.  Has its line and col set to the
+   same as the last lexed token, so that diagnostics appear in the
+   right place.  */
+cpp_token *
+_cpp_temp_token (pfile)
+     cpp_reader *pfile;
+{
+  cpp_token *old, *result;
+
+  old = pfile->cur_token - 1;
+  if (pfile->cur_token == pfile->cur_run->limit)
+    {
+      pfile->cur_run = next_tokenrun (pfile->cur_run);
+      pfile->cur_token = pfile->cur_run->base;
+    }
+
+  result = pfile->cur_token++;
+  result->line = old->line;
+  result->col = old->col;
+  return result;
+}
+
+/* Lex a token into RESULT (external interface).  Takes care of issues
+   like directive handling, token lookahead, multiple include
+   optimization and skipping.  */
+const cpp_token *
+_cpp_lex_token (pfile)
      cpp_reader *pfile;
-     cpp_token *dest;
 {
   cpp_token *result;
 
@@ -947,19 +649,24 @@ _cpp_lex_token (pfile, dest)
          pfile->cur_run = next_tokenrun (pfile->cur_run);
          pfile->cur_token = pfile->cur_run->base;
        }
-      result = pfile->cur_token++;
 
       if (pfile->lookaheads)
-       pfile->lookaheads--;
+       {
+         pfile->lookaheads--;
+         result = pfile->cur_token++;
+       }
       else
-       result = lex_token (pfile, result);
+       result = _cpp_lex_direct (pfile);
 
       if (result->flags & BOL)
        {
          /* Is this a directive.  If _cpp_handle_directive returns
             false, it is an assembler #.  */
          if (result->type == CPP_HASH
-             && !pfile->state.parsing_args
+             /* 6.10.3 p 11: Directives in a list of macro arguments
+                gives undefined behavior.  This implementation
+                handles the directive as normal.  */
+             && pfile->state.parsing_args != 1
              && _cpp_handle_directive (pfile, result->flags & PREV_WHITE))
            continue;
          if (pfile->cb.line_change && !pfile->state.skipping)
@@ -971,7 +678,7 @@ _cpp_lex_token (pfile, dest)
        break;
 
       /* Outside a directive, invalidate controlling macros.  At file
-        EOF, lex_token takes care of popping the buffer, so we never
+        EOF, _cpp_lex_direct takes care of popping the buffer, so we never
         get here and MI optimisation works.  */
       pfile->mi_valid = false;
 
@@ -979,119 +686,154 @@ _cpp_lex_token (pfile, dest)
        break;
     }
 
-  *dest = *result;
+  return result;
+}
+
+/* Returns true if a fresh line has been loaded.  */
+bool
+_cpp_get_fresh_line (pfile)
+     cpp_reader *pfile;
+{
+  /* We can't get a new line until we leave the current directive.  */
+  if (pfile->state.in_directive)
+    return false;
+
+  for (;;)
+    {
+      cpp_buffer *buffer = pfile->buffer;
+
+      if (!buffer->need_line)
+       return true;
+
+      if (buffer->next_line < buffer->rlimit)
+       {
+         _cpp_clean_line (pfile);
+         return true;
+       }
+
+      /* First, get out of parsing arguments state.  */
+      if (pfile->state.parsing_args)
+       return false;
+
+      /* End of buffer.  Non-empty files should end in a newline.  */
+      if (buffer->buf != buffer->rlimit
+         && buffer->next_line > buffer->rlimit
+         && !buffer->from_stage3)
+       {
+         /* Only warn once.  */
+         buffer->next_line = buffer->rlimit;
+         cpp_error_with_line (pfile, DL_PEDWARN, pfile->line - 1,
+                              CPP_BUF_COLUMN (buffer, buffer->cur),
+                              "no newline at end of file");
+       }
+      if (!buffer->prev)
+       return false;
+
+      if (buffer->return_at_eof)
+       {
+         _cpp_pop_buffer (pfile);
+         return false;
+       }
+
+      _cpp_pop_buffer (pfile);
+    }
 }
 
-/* Lex a token into RESULT.  When meeting a newline, returns CPP_EOF
-   if parsing a directive, otherwise returns to the start of the token
-   buffer if permissible.  Returns the location of the lexed token.  */
-static cpp_token *
-lex_token (pfile, result)
+#define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE)         \
+  do                                                   \
+    {                                                  \
+      result->type = ELSE_TYPE;                                \
+      if (*buffer->cur == CHAR)                                \
+       buffer->cur++, result->type = THEN_TYPE;        \
+    }                                                  \
+  while (0)
+
+/* Lex a token into pfile->cur_token, which is also incremented, to
+   get diagnostics pointing to the correct location.
+
+   Does not handle issues such as token lookahead, multiple-include
+   optimisation, directives, skipping etc.  This function is only
+   suitable for use by _cpp_lex_token, and in special cases like
+   lex_expansion_token which doesn't care for any of these issues.
+
+   When meeting a newline, returns CPP_EOF if parsing a directive,
+   otherwise returns to the start of the token buffer if permissible.
+   Returns the location of the lexed token.  */
+cpp_token *
+_cpp_lex_direct (pfile)
      cpp_reader *pfile;
-     cpp_token *result;
 {
   cppchar_t c;
   cpp_buffer *buffer;
   const unsigned char *comment_start;
+  cpp_token *result = pfile->cur_token++;
 
  fresh_line:
+  result->flags = 0;
+  if (pfile->buffer->need_line)
+    {
+      if (!_cpp_get_fresh_line (pfile))
+       {
+         result->type = CPP_EOF;
+         if (!pfile->state.in_directive)
+           {
+             /* Tell the compiler the line number of the EOF token.  */
+             result->line = pfile->line;
+             result->flags = BOL;
+           }
+         return result;
+       }
+      if (!pfile->keep_tokens)
+       {
+         pfile->cur_run = &pfile->base_run;
+         result = pfile->base_run.base;
+         pfile->cur_token = result + 1;
+       }
+      result->flags = BOL;
+      if (pfile->state.parsing_args == 2)
+       result->flags |= PREV_WHITE;
+    }
   buffer = pfile->buffer;
-  result->flags = buffer->saved_flags;
-  buffer->saved_flags = 0;
  update_tokens_line:
   result->line = pfile->line;
 
  skipped_white:
-  c = buffer->read_ahead;
-  if (c == EOF && buffer->cur < buffer->rlimit)
-    c = *buffer->cur++;
+  if (buffer->cur >= buffer->notes[buffer->cur_note].pos
+      && !pfile->overlaid_buffer)
+    {
+      _cpp_process_line_notes (pfile, false);
+      result->line = pfile->line;
+    }
+  c = *buffer->cur++;
   result->col = CPP_BUF_COLUMN (buffer, buffer->cur);
-  buffer->read_ahead = EOF;
 
- trigraph:
   switch (c)
     {
-    case EOF:
-      buffer->saved_flags = BOL;
-      if (!pfile->state.parsing_args && !pfile->state.in_directive)
-       {
-         if (buffer->cur != buffer->line_base)
-           {
-             /* Non-empty files should end in a newline.  Don't warn
-                for command line and _Pragma buffers.  */
-             if (!buffer->from_stage3)
-               cpp_pedwarn (pfile, "no newline at end of file");
-             handle_newline (pfile, '\n');
-           }
-
-         /* Don't pop the last buffer.  */
-         if (buffer->prev)
-           {
-             unsigned char stop = buffer->return_at_eof;
-
-             _cpp_pop_buffer (pfile);
-             if (!stop)
-               goto fresh_line;
-           }
-       }
-      result->type = CPP_EOF;
-      break;
-
     case ' ': case '\t': case '\f': case '\v': case '\0':
-      skip_whitespace (pfile, c);
       result->flags |= PREV_WHITE;
+      skip_whitespace (pfile, c);
       goto skipped_white;
 
-    case '\n': case '\r':
-      handle_newline (pfile, c);
-      buffer->saved_flags = BOL;
-      if (! pfile->state.in_directive)
-       {
-         if (!pfile->keep_tokens)
-           {
-             pfile->cur_run = &pfile->base_run;
-             result = pfile->base_run.base;
-             pfile->cur_token = result + 1;
-           }
-         goto fresh_line;
-       }
-      result->type = CPP_EOF;
-      break;
-
-    case '?':
-    case '\\':
-      /* These could start an escaped newline, or '?' a trigraph.  Let
-        skip_escaped_newlines do all the work.  */
-      {
-       unsigned int line = pfile->line;
-
-       c = skip_escaped_newlines (pfile, c);
-       if (line != pfile->line)
-         /* We had at least one escaped newline of some sort, and the
-            next character is in buffer->read_ahead.  Update the
-            token's line and column.  */
-           goto update_tokens_line;
-
-       /* We are either the original '?' or '\\', or a trigraph.  */
-       result->type = CPP_QUERY;
-       buffer->read_ahead = EOF;
-       if (c == '\\')
-         goto random_char;
-       else if (c != '?')
-         goto trigraph;
-      }
-      break;
+    case '\n':
+      pfile->line++;
+      buffer->need_line = true;
+      goto fresh_line;
 
     case '0': case '1': case '2': case '3': case '4':
     case '5': case '6': case '7': case '8': case '9':
       result->type = CPP_NUMBER;
-      parse_number (pfile, &result->val.str, c, 0);
+      lex_number (pfile, &result->val.str);
       break;
 
-    case '$':
-      if (!CPP_OPTION (pfile, dollars_in_ident))
-       goto random_char;
-      /* Fall through...  */
+    case 'L':
+      /* 'L' may introduce wide characters or strings.  */
+      if (*buffer->cur == '\'' || *buffer->cur == '"')
+       {
+         lex_string (pfile, result, buffer->cur - 1);
+         break;
+       }
+      /* Fall through.  */
 
     case '_':
     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
@@ -1100,80 +842,66 @@ lex_token (pfile, result)
     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
     case 'y': case 'z':
     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 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
     case 'Y': case 'Z':
       result->type = CPP_NAME;
-      result->val.node = parse_identifier (pfile);
+      result->val.node = lex_identifier (pfile, buffer->cur - 1);
 
-      /* 'L' may introduce wide characters or strings.  */
-      if (result->val.node == pfile->spec_nodes.n_L)
-       {
-         c = buffer->read_ahead;
-         if (c == EOF && buffer->cur < buffer->rlimit)
-           c = *buffer->cur;
-         if (c == '\'' || c == '"')
-           {
-             buffer->cur++;
-             ACCEPT_CHAR (c == '"' ? CPP_WSTRING: CPP_WCHAR);
-             goto make_string;
-           }
-       }
       /* Convert named operators to their proper types.  */
-      else if (result->val.node->flags & NODE_OPERATOR)
+      if (result->val.node->flags & NODE_OPERATOR)
        {
          result->flags |= NAMED_OP;
-         result->type = result->val.node->value.operator;
+         result->type = result->val.node->directive_index;
        }
       break;
 
     case '\'':
     case '"':
-      result->type = c == '"' ? CPP_STRING: CPP_CHAR;
-    make_string:
-      parse_string (pfile, result, c);
+      lex_string (pfile, result, buffer->cur - 1);
       break;
 
     case '/':
       /* A potential block or line comment.  */
       comment_start = buffer->cur;
-      result->type = CPP_DIV;
-      c = get_effective_char (pfile);
-      if (c == '=')
-       ACCEPT_CHAR (CPP_DIV_EQ);
-      if (c != '/' && c != '*')
-       break;
+      c = *buffer->cur;
       
       if (c == '*')
        {
-         if (skip_block_comment (pfile))
-           cpp_error (pfile, "unterminated comment");
+         if (_cpp_skip_block_comment (pfile))
+           cpp_error (pfile, DL_ERROR, "unterminated comment");
        }
-      else
+      else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments)
+                           || CPP_IN_SYSTEM_HEADER (pfile)))
        {
-         if (!CPP_OPTION (pfile, cplusplus_comments)
-             && !CPP_IN_SYSTEM_HEADER (pfile))
-           break;
-
          /* Warn about comments only if pedantically GNUC89, and not
             in system headers.  */
          if (CPP_OPTION (pfile, lang) == CLK_GNUC89 && CPP_PEDANTIC (pfile)
              && ! buffer->warned_cplusplus_comments)
            {
-             cpp_pedwarn (pfile,
-                          "C++ style comments are not allowed in ISO C89");
-             cpp_pedwarn (pfile,
-                          "(this will be reported only once per input file)");
+             cpp_error (pfile, DL_PEDWARN,
+                        "C++ style comments are not allowed in ISO C90");
+             cpp_error (pfile, DL_PEDWARN,
+                        "(this will be reported only once per input file)");
              buffer->warned_cplusplus_comments = 1;
            }
 
-         /* Skip_line_comment updates buffer->read_ahead.  */
          if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments))
-           cpp_warning (pfile, "multi-line comment");
+           cpp_error (pfile, DL_WARNING, "multi-line comment");
+       }
+      else if (c == '=')
+       {
+         buffer->cur++;
+         result->type = CPP_DIV_EQ;
+         break;
+       }
+      else
+       {
+         result->type = CPP_DIV;
+         break;
        }
 
-      /* Skipping the comment has updated buffer->read_ahead.  */
       if (!pfile->state.save_comments)
        {
          result->flags |= PREV_WHITE;
@@ -1181,158 +909,156 @@ lex_token (pfile, result)
        }
 
       /* Save the comment as a token in its own right.  */
-      save_comment (pfile, result, comment_start);
-      /* Don't do MI optimisation.  */
+      save_comment (pfile, result, comment_start, c);
       break;
 
     case '<':
       if (pfile->state.angled_headers)
        {
-         result->type = CPP_HEADER_NAME;
-         c = '>';              /* terminator.  */
-         goto make_string;
+         lex_string (pfile, result, buffer->cur - 1);
+         break;
        }
 
       result->type = CPP_LESS;
-      c = get_effective_char (pfile);
-      if (c == '=')
-       ACCEPT_CHAR (CPP_LESS_EQ);
-      else if (c == '<')
-       {
-         ACCEPT_CHAR (CPP_LSHIFT);
-         if (get_effective_char (pfile) == '=')
-           ACCEPT_CHAR (CPP_LSHIFT_EQ);
-       }
-      else if (c == '?' && CPP_OPTION (pfile, cplusplus))
+      if (*buffer->cur == '=')
+       buffer->cur++, result->type = CPP_LESS_EQ;
+      else if (*buffer->cur == '<')
        {
-         ACCEPT_CHAR (CPP_MIN);
-         if (get_effective_char (pfile) == '=')
-           ACCEPT_CHAR (CPP_MIN_EQ);
+         buffer->cur++;
+         IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT);
        }
-      else if (c == ':' && CPP_OPTION (pfile, digraphs))
+      else if (*buffer->cur == '?' && CPP_OPTION (pfile, cplusplus))
        {
-         ACCEPT_CHAR (CPP_OPEN_SQUARE);
-         result->flags |= DIGRAPH;
+         buffer->cur++;
+         IF_NEXT_IS ('=', CPP_MIN_EQ, CPP_MIN);
        }
-      else if (c == '%' && CPP_OPTION (pfile, digraphs))
+      else if (CPP_OPTION (pfile, digraphs))
        {
-         ACCEPT_CHAR (CPP_OPEN_BRACE);
-         result->flags |= DIGRAPH;
+         if (*buffer->cur == ':')
+           {
+             buffer->cur++;
+             result->flags |= DIGRAPH;
+             result->type = CPP_OPEN_SQUARE;
+           }
+         else if (*buffer->cur == '%')
+           {
+             buffer->cur++;
+             result->flags |= DIGRAPH;
+             result->type = CPP_OPEN_BRACE;
+           }
        }
       break;
 
     case '>':
       result->type = CPP_GREATER;
-      c = get_effective_char (pfile);
-      if (c == '=')
-       ACCEPT_CHAR (CPP_GREATER_EQ);
-      else if (c == '>')
+      if (*buffer->cur == '=')
+       buffer->cur++, result->type = CPP_GREATER_EQ;
+      else if (*buffer->cur == '>')
        {
-         ACCEPT_CHAR (CPP_RSHIFT);
-         if (get_effective_char (pfile) == '=')
-           ACCEPT_CHAR (CPP_RSHIFT_EQ);
+         buffer->cur++;
+         IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT);
        }
-      else if (c == '?' && CPP_OPTION (pfile, cplusplus))
+      else if (*buffer->cur == '?' && CPP_OPTION (pfile, cplusplus))
        {
-         ACCEPT_CHAR (CPP_MAX);
-         if (get_effective_char (pfile) == '=')
-           ACCEPT_CHAR (CPP_MAX_EQ);
+         buffer->cur++;
+         IF_NEXT_IS ('=', CPP_MAX_EQ, CPP_MAX);
        }
       break;
 
     case '%':
-      lex_percent (pfile, result);
+      result->type = CPP_MOD;
+      if (*buffer->cur == '=')
+       buffer->cur++, result->type = CPP_MOD_EQ;
+      else if (CPP_OPTION (pfile, digraphs))
+       {
+         if (*buffer->cur == ':')
+           {
+             buffer->cur++;
+             result->flags |= DIGRAPH;
+             result->type = CPP_HASH;
+             if (*buffer->cur == '%' && buffer->cur[1] == ':')
+               buffer->cur += 2, result->type = CPP_PASTE;
+           }
+         else if (*buffer->cur == '>')
+           {
+             buffer->cur++;
+             result->flags |= DIGRAPH;
+             result->type = CPP_CLOSE_BRACE;
+           }
+       }
       break;
 
     case '.':
-      lex_dot (pfile, result);
+      result->type = CPP_DOT;
+      if (ISDIGIT (*buffer->cur))
+       {
+         result->type = CPP_NUMBER;
+         lex_number (pfile, &result->val.str);
+       }
+      else if (*buffer->cur == '.' && buffer->cur[1] == '.')
+       buffer->cur += 2, result->type = CPP_ELLIPSIS;
+      else if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
+       buffer->cur++, result->type = CPP_DOT_STAR;
       break;
 
     case '+':
       result->type = CPP_PLUS;
-      c = get_effective_char (pfile);
-      if (c == '=')
-       ACCEPT_CHAR (CPP_PLUS_EQ);
-      else if (c == '+')
-       ACCEPT_CHAR (CPP_PLUS_PLUS);
+      if (*buffer->cur == '+')
+       buffer->cur++, result->type = CPP_PLUS_PLUS;
+      else if (*buffer->cur == '=')
+       buffer->cur++, result->type = CPP_PLUS_EQ;
       break;
 
     case '-':
       result->type = CPP_MINUS;
-      c = get_effective_char (pfile);
-      if (c == '>')
+      if (*buffer->cur == '>')
        {
-         ACCEPT_CHAR (CPP_DEREF);
-         if (CPP_OPTION (pfile, cplusplus)
-             && get_effective_char (pfile) == '*')
-           ACCEPT_CHAR (CPP_DEREF_STAR);
+         buffer->cur++;
+         result->type = CPP_DEREF;
+         if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
+           buffer->cur++, result->type = CPP_DEREF_STAR;
        }
-      else if (c == '=')
-       ACCEPT_CHAR (CPP_MINUS_EQ);
-      else if (c == '-')
-       ACCEPT_CHAR (CPP_MINUS_MINUS);
-      break;
-
-    case '*':
-      result->type = CPP_MULT;
-      if (get_effective_char (pfile) == '=')
-       ACCEPT_CHAR (CPP_MULT_EQ);
-      break;
-
-    case '=':
-      result->type = CPP_EQ;
-      if (get_effective_char (pfile) == '=')
-       ACCEPT_CHAR (CPP_EQ_EQ);
-      break;
-
-    case '!':
-      result->type = CPP_NOT;
-      if (get_effective_char (pfile) == '=')
-       ACCEPT_CHAR (CPP_NOT_EQ);
+      else if (*buffer->cur == '-')
+       buffer->cur++, result->type = CPP_MINUS_MINUS;
+      else if (*buffer->cur == '=')
+       buffer->cur++, result->type = CPP_MINUS_EQ;
       break;
 
     case '&':
       result->type = CPP_AND;
-      c = get_effective_char (pfile);
-      if (c == '=')
-       ACCEPT_CHAR (CPP_AND_EQ);
-      else if (c == '&')
-       ACCEPT_CHAR (CPP_AND_AND);
-      break;
-         
-    case '#':
-      result->type = CPP_HASH;
-      if (get_effective_char (pfile) == '#')
-         ACCEPT_CHAR (CPP_PASTE);
+      if (*buffer->cur == '&')
+       buffer->cur++, result->type = CPP_AND_AND;
+      else if (*buffer->cur == '=')
+       buffer->cur++, result->type = CPP_AND_EQ;
       break;
 
     case '|':
       result->type = CPP_OR;
-      c = get_effective_char (pfile);
-      if (c == '=')
-       ACCEPT_CHAR (CPP_OR_EQ);
-      else if (c == '|')
-       ACCEPT_CHAR (CPP_OR_OR);
-      break;
-
-    case '^':
-      result->type = CPP_XOR;
-      if (get_effective_char (pfile) == '=')
-       ACCEPT_CHAR (CPP_XOR_EQ);
+      if (*buffer->cur == '|')
+       buffer->cur++, result->type = CPP_OR_OR;
+      else if (*buffer->cur == '=')
+       buffer->cur++, result->type = CPP_OR_EQ;
       break;
 
     case ':':
       result->type = CPP_COLON;
-      c = get_effective_char (pfile);
-      if (c == ':' && CPP_OPTION (pfile, cplusplus))
-       ACCEPT_CHAR (CPP_SCOPE);
-      else if (c == '>' && CPP_OPTION (pfile, digraphs))
+      if (*buffer->cur == ':' && CPP_OPTION (pfile, cplusplus))
+       buffer->cur++, result->type = CPP_SCOPE;
+      else if (*buffer->cur == '>' && CPP_OPTION (pfile, digraphs))
        {
+         buffer->cur++;
          result->flags |= DIGRAPH;
-         ACCEPT_CHAR (CPP_CLOSE_SQUARE);
+         result->type = CPP_CLOSE_SQUARE;
        }
       break;
 
+    case '*': IF_NEXT_IS ('=', CPP_MULT_EQ, CPP_MULT); break;
+    case '=': IF_NEXT_IS ('=', CPP_EQ_EQ, CPP_EQ); break;
+    case '!': IF_NEXT_IS ('=', CPP_NOT_EQ, CPP_NOT); break;
+    case '^': IF_NEXT_IS ('=', CPP_XOR_EQ, CPP_XOR); break;
+    case '#': IF_NEXT_IS ('#', CPP_PASTE, CPP_HASH); break;
+
+    case '?': result->type = CPP_QUERY; break;
     case '~': result->type = CPP_COMPL; break;
     case ',': result->type = CPP_COMMA; break;
     case '(': result->type = CPP_OPEN_PAREN; break;
@@ -1343,21 +1069,33 @@ lex_token (pfile, result)
     case '}': result->type = CPP_CLOSE_BRACE; break;
     case ';': result->type = CPP_SEMICOLON; break;
 
-      /* @ is a punctuator in Objective C.  */
+      /* @ is a punctuator in Objective-C.  */
     case '@': result->type = CPP_ATSIGN; break;
 
-    random_char:
+    case '$':
+    case '\\':
+      {
+       const uchar *base = --buffer->cur;
+
+       if (forms_identifier_p (pfile, true))
+         {
+           result->type = CPP_NAME;
+           result->val.node = lex_identifier (pfile, base);
+           break;
+         }
+       buffer->cur++;
+      }
+
     default:
-      result->type = CPP_OTHER;
-      result->val.c = c;
+      create_literal (pfile, result, buffer->cur - 1, 1, CPP_OTHER);
       break;
     }
 
   return result;
 }
 
-/* An upper bound on the number of bytes needed to spell a token,
-   including preceding whitespace.  */
+/* An upper bound on the number of bytes needed to spell TOKEN.
+   Does not include preceding whitespace.  */
 unsigned int
 cpp_token_len (token)
      const cpp_token *token;
@@ -1366,12 +1104,12 @@ cpp_token_len (token)
 
   switch (TOKEN_SPELL (token))
     {
-    default:           len = 0;                                break;
-    case SPELL_STRING: len = token->val.str.len;               break;
+    default:           len = 4;                                break;
+    case SPELL_LITERAL:        len = token->val.str.len;               break;
     case SPELL_IDENT:  len = NODE_LEN (token->val.node);       break;
     }
-  /* 1 for whitespace, 4 for comment delimeters.  */
-  return len + 5;
+
+  return len;
 }
 
 /* Write the spelling of a token TOKEN to BUFFER.  The buffer must
@@ -1398,59 +1136,40 @@ cpp_spell_token (pfile, token, buffer)
          goto spell_ident;
        else
          spelling = TOKEN_NAME (token);
-       
+
        while ((c = *spelling++) != '\0')
          *buffer++ = c;
       }
       break;
 
+    spell_ident:
     case SPELL_IDENT:
-      spell_ident:
       memcpy (buffer, NODE_NAME (token->val.node), NODE_LEN (token->val.node));
       buffer += NODE_LEN (token->val.node);
       break;
 
-    case SPELL_STRING:
-      {
-       int left, right, tag;
-       switch (token->type)
-         {
-         case CPP_STRING:      left = '"';  right = '"';  tag = '\0'; break;
-         case CPP_WSTRING:     left = '"';  right = '"';  tag = 'L';  break;
-         case CPP_CHAR:        left = '\''; right = '\''; tag = '\0'; break;
-         case CPP_WCHAR:       left = '\''; right = '\''; tag = 'L';  break;
-         case CPP_HEADER_NAME: left = '<';  right = '>';  tag = '\0'; break;
-         default:              left = '\0'; right = '\0'; tag = '\0'; break;
-         }
-       if (tag) *buffer++ = tag;
-       if (left) *buffer++ = left;
-       memcpy (buffer, token->val.str.text, token->val.str.len);
-       buffer += token->val.str.len;
-       if (right) *buffer++ = right;
-      }
-      break;
-
-    case SPELL_CHAR:
-      *buffer++ = token->val.c;
+    case SPELL_LITERAL:
+      memcpy (buffer, token->val.str.text, token->val.str.len);
+      buffer += token->val.str.len;
       break;
 
     case SPELL_NONE:
-      cpp_ice (pfile, "Unspellable token %s", TOKEN_NAME (token));
+      cpp_error (pfile, DL_ICE, "unspellable token %s", TOKEN_NAME (token));
       break;
     }
 
   return buffer;
 }
 
-/* Returns a token as a null-terminated string.  The string is
-   temporary, and automatically freed later.  Useful for diagnostics.  */
+/* Returns TOKEN spelt as a null-terminated string.  The string is
+   freed when the reader is destroyed.  Useful for diagnostics.  */
 unsigned char *
 cpp_token_as_text (pfile, token)
      cpp_reader *pfile;
      const cpp_token *token;
-{
-  unsigned int len = cpp_token_len (token);
-  unsigned char *start = _cpp_pool_alloc (&pfile->ident_pool, len), *end;
+{ 
+  unsigned int len = cpp_token_len (token) + 1;
+  unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end;
 
   end = cpp_spell_token (pfile, token, start);
   end[0] = '\0';
@@ -1458,7 +1177,8 @@ cpp_token_as_text (pfile, token)
   return start;
 }
 
-/* Used by C front ends.  Should really move to using cpp_token_as_text.  */
+/* Used by C front ends, which really should move to using
+   cpp_token_as_text.  */
 const char *
 cpp_type2name (type)
      enum cpp_ttype type;
@@ -1466,22 +1186,20 @@ cpp_type2name (type)
   return (const char *) token_spellings[type].name;
 }
 
-/* Writes the spelling of token to FP.  Separate from cpp_spell_token
-   for efficiency - to avoid double-buffering.  Also, outputs a space
-   if PREV_WHITE is flagged.  */
+/* Writes the spelling of token to FP, without any preceding space.
+   Separated from cpp_spell_token for efficiency - to avoid stdio
+   double-buffering.  */
 void
 cpp_output_token (token, fp)
      const cpp_token *token;
      FILE *fp;
 {
-  if (token->flags & PREV_WHITE)
-    putc (' ', fp);
-
   switch (TOKEN_SPELL (token))
     {
     case SPELL_OPERATOR:
       {
        const unsigned char *spelling;
+       int c;
 
        if (token->flags & DIGRAPH)
          spelling
@@ -1491,36 +1209,20 @@ cpp_output_token (token, fp)
        else
          spelling = TOKEN_NAME (token);
 
-       ufputs (spelling, fp);
+       c = *spelling;
+       do
+         putc (c, fp);
+       while ((c = *++spelling) != '\0');
       }
       break;
 
     spell_ident:
     case SPELL_IDENT:
-      ufputs (NODE_NAME (token->val.node), fp);
+      fwrite (NODE_NAME (token->val.node), 1, NODE_LEN (token->val.node), fp);
     break;
 
-    case SPELL_STRING:
-      {
-       int left, right, tag;
-       switch (token->type)
-         {
-         case CPP_STRING:      left = '"';  right = '"';  tag = '\0'; break;
-         case CPP_WSTRING:     left = '"';  right = '"';  tag = 'L';  break;
-         case CPP_CHAR:        left = '\''; right = '\''; tag = '\0'; break;
-         case CPP_WCHAR:       left = '\''; right = '\''; tag = 'L';  break;
-         case CPP_HEADER_NAME: left = '<';  right = '>';  tag = '\0'; break;
-         default:              left = '\0'; right = '\0'; tag = '\0'; break;
-         }
-       if (tag) putc (tag, fp);
-       if (left) putc (left, fp);
-       fwrite (token->val.str.text, 1, token->val.str.len, fp);
-       if (right) putc (right, fp);
-      }
-      break;
-
-    case SPELL_CHAR:
-      putc (token->val.c, fp);
+    case SPELL_LITERAL:
+      fwrite (token->val.str.text, 1, token->val.str.len, fp);
       break;
 
     case SPELL_NONE:
@@ -1540,13 +1242,11 @@ _cpp_equiv_tokens (a, b)
       default:                 /* Keep compiler happy.  */
       case SPELL_OPERATOR:
        return 1;
-      case SPELL_CHAR:
-       return a->val.c == b->val.c; /* Character.  */
       case SPELL_NONE:
        return (a->type != CPP_MACRO_ARG || a->val.arg_no == b->val.arg_no);
       case SPELL_IDENT:
        return a->val.node == b->val.node;
-      case SPELL_STRING:
+      case SPELL_LITERAL:
        return (a->val.str.len == b->val.str.len
                && !memcmp (a->val.str.text, b->val.str.text,
                            a->val.str.len));
@@ -1555,117 +1255,10 @@ _cpp_equiv_tokens (a, b)
   return 0;
 }
 
-/* Determine whether two tokens can be pasted together, and if so,
-   what the resulting token is.  Returns CPP_EOF if the tokens cannot
-   be pasted, or the appropriate type for the merged token if they
-   can.  */
-enum cpp_ttype
-cpp_can_paste (pfile, token1, token2, digraph)
-     cpp_reader * pfile;
-     const cpp_token *token1, *token2;
-     int* digraph;
-{
-  enum cpp_ttype a = token1->type, b = token2->type;
-  int cxx = CPP_OPTION (pfile, cplusplus);
-
-  /* Treat named operators as if they were ordinary NAMEs.  */
-  if (token1->flags & NAMED_OP)
-    a = CPP_NAME;
-  if (token2->flags & NAMED_OP)
-    b = CPP_NAME;
-
-  if ((int) a <= (int) CPP_LAST_EQ && b == CPP_EQ)
-    return (enum cpp_ttype) ((int) a + ((int) CPP_EQ_EQ - (int) CPP_EQ));
-
-  switch (a)
-    {
-    case CPP_GREATER:
-      if (b == a) return CPP_RSHIFT;
-      if (b == CPP_QUERY && cxx)       return CPP_MAX;
-      if (b == CPP_GREATER_EQ) return CPP_RSHIFT_EQ;
-      break;
-    case CPP_LESS:
-      if (b == a) return CPP_LSHIFT;
-      if (b == CPP_QUERY && cxx)       return CPP_MIN;
-      if (b == CPP_LESS_EQ)    return CPP_LSHIFT_EQ;
-      if (CPP_OPTION (pfile, digraphs))
-       {
-         if (b == CPP_COLON)
-           {*digraph = 1; return CPP_OPEN_SQUARE;} /* <: digraph */
-         if (b == CPP_MOD)
-           {*digraph = 1; return CPP_OPEN_BRACE;}      /* <% digraph */
-       }
-      break;
-
-    case CPP_PLUS: if (b == a) return CPP_PLUS_PLUS; break;
-    case CPP_AND:  if (b == a) return CPP_AND_AND; break;
-    case CPP_OR:   if (b == a) return CPP_OR_OR;   break;
-
-    case CPP_MINUS:
-      if (b == a)              return CPP_MINUS_MINUS;
-      if (b == CPP_GREATER)    return CPP_DEREF;
-      break;
-    case CPP_COLON:
-      if (b == a && cxx)       return CPP_SCOPE;
-      if (b == CPP_GREATER && CPP_OPTION (pfile, digraphs))
-       {*digraph = 1; return CPP_CLOSE_SQUARE;} /* :> digraph */
-      break;
-
-    case CPP_MOD:
-      if (CPP_OPTION (pfile, digraphs))
-       {
-         if (b == CPP_GREATER)
-           {*digraph = 1; return CPP_CLOSE_BRACE;}  /* %> digraph */
-         if (b == CPP_COLON)
-           {*digraph = 1; return CPP_HASH;}         /* %: digraph */
-       }
-      break;
-    case CPP_DEREF:
-      if (b == CPP_MULT && cxx)        return CPP_DEREF_STAR;
-      break;
-    case CPP_DOT:
-      if (b == CPP_MULT && cxx)        return CPP_DOT_STAR;
-      if (b == CPP_NUMBER)     return CPP_NUMBER;
-      break;
-
-    case CPP_HASH:
-      if (b == a && (token1->flags & DIGRAPH) == (token2->flags & DIGRAPH))
-       /* %:%: digraph */
-       {*digraph = (token1->flags & DIGRAPH); return CPP_PASTE;}
-      break;
-
-    case CPP_NAME:
-      if (b == CPP_NAME)       return CPP_NAME;
-      if (b == CPP_NUMBER
-         && name_p (pfile, &token2->val.str)) return CPP_NAME;
-      if (b == CPP_CHAR
-         && token1->val.node == pfile->spec_nodes.n_L) return CPP_WCHAR;
-      if (b == CPP_STRING
-         && token1->val.node == pfile->spec_nodes.n_L) return CPP_WSTRING;
-      break;
-
-    case CPP_NUMBER:
-      if (b == CPP_NUMBER)     return CPP_NUMBER;
-      if (b == CPP_NAME)       return CPP_NUMBER;
-      if (b == CPP_DOT)                return CPP_NUMBER;
-      /* Numbers cannot have length zero, so this is safe.  */
-      if ((b == CPP_PLUS || b == CPP_MINUS)
-         && VALID_SIGN ('+', token1->val.str.text[token1->val.str.len - 1]))
-       return CPP_NUMBER;
-      break;
-
-    default:
-      break;
-    }
-
-  return CPP_EOF;
-}
-
 /* Returns nonzero if a space should be inserted to avoid an
    accidental token paste for output.  For simplicity, it is
    conservative, and occasionally advises a space where one is not
    needed, e.g. "." and ".2".  */
-
 int
 cpp_avoid_paste (pfile, token1, token2)
      cpp_reader *pfile;
@@ -1709,9 +1302,12 @@ cpp_avoid_paste (pfile, token1, token2)
                                || b == CPP_CHAR || b == CPP_STRING); /* L */
     case CPP_NUMBER:   return (b == CPP_NUMBER || b == CPP_NAME
                                || c == '.' || c == '+' || c == '-');
-    case CPP_OTHER:    return (CPP_OPTION (pfile, objc)
-                               && token1->val.c == '@'
-                               && (b == CPP_NAME || b == CPP_STRING));
+                                     /* UCNs */
+    case CPP_OTHER:    return ((token1->val.str.text[0] == '\\'
+                                && b == CPP_NAME)
+                               || (CPP_OPTION (pfile, objc)
+                                   && token1->val.str.text[0] == '@'
+                                   && (b == CPP_NAME || b == CPP_STRING)));
     default:           break;
     }
 
@@ -1719,20 +1315,22 @@ cpp_avoid_paste (pfile, token1, token2)
 }
 
 /* Output all the remaining tokens on the current line, and a newline
-   character, to FP.  Leading whitespace is removed.  */
+   character, to FP.  Leading whitespace is removed.  If there are
+   macros, special token padding is not performed.  */
 void
 cpp_output_line (pfile, fp)
      cpp_reader *pfile;
      FILE *fp;
 {
-  cpp_token token;
+  const cpp_token *token;
 
-  cpp_get_token (pfile, &token);
-  token.flags &= ~PREV_WHITE;
-  while (token.type != CPP_EOF)
+  token = cpp_get_token (pfile);
+  while (token->type != CPP_EOF)
     {
-      cpp_output_token (&token, fp);
-      cpp_get_token (pfile, &token);
+      cpp_output_token (token, fp);
+      token = cpp_get_token (pfile);
+      if (token->flags & PREV_WHITE)
+       putc (' ', fp);
     }
 
   putc ('\n', fp);
@@ -1743,132 +1341,84 @@ static unsigned int
 hex_digit_value (c)
      unsigned int c;
 {
-  if (c >= 'a' && c <= 'f')
-    return c - 'a' + 10;
-  if (c >= 'A' && c <= 'F')
-    return c - 'A' + 10;
-  if (c >= '0' && c <= '9')
-    return c - '0';
-  abort ();
+  if (hex_p (c))
+    return hex_value (c);
+  else
+    abort ();
 }
 
-/* Parse a '\uNNNN' or '\UNNNNNNNN' sequence.  Returns 1 to indicate
-   failure if cpplib is not parsing C++ or C99.  Such failure is
-   silent, and no variables are updated.  Otherwise returns 0, and
-   warns if -Wtraditional.
-
-   [lex.charset]: The character designated by the universal character
-   name \UNNNNNNNN is that character whose character short name in
-   ISO/IEC 10646 is NNNNNNNN; the character designated by the
-   universal character name \uNNNN is that character whose character
-   short name in ISO/IEC 10646 is 0000NNNN.  If the hexadecimal value
-   for a universal character name is less than 0x20 or in the range
-   0x7F-0x9F (inclusive), or if the universal character name
-   designates a character in the basic source character set, then the
-   program is ill-formed.
-
-   We assume that wchar_t is Unicode, so we don't need to do any
-   mapping.  Is this ever wrong?
-
-   PC points to the 'u' or 'U', PSTR is points to the byte after PC,
-   LIMIT is the end of the string or charconst.  PSTR is updated to
-   point after the UCS on return, and the UCS is written into PC.  */
-
-static int
-maybe_read_ucs (pfile, pstr, limit, pc)
+/* Read a possible universal character name starting at *PSTR.  */
+static cppchar_t
+maybe_read_ucn (pfile, pstr)
      cpp_reader *pfile;
-     const unsigned char **pstr;
-     const unsigned char *limit;
-     unsigned int *pc;
+     const uchar **pstr;
 {
-  const unsigned char *p = *pstr;
-  unsigned int code = 0;
-  unsigned int c = *pc, length;
-
-  /* Only attempt to interpret a UCS for C++ and C99.  */
-  if (! (CPP_OPTION (pfile, cplusplus) || CPP_OPTION (pfile, c99)))
-    return 1;
-
-  if (CPP_WTRADITIONAL (pfile))
-    cpp_warning (pfile, "the meaning of '\\%c' varies with -traditional", c);
+  cppchar_t result, c = (*pstr)[-1];
 
-  length = (c == 'u' ? 4: 8);
-
-  if ((size_t) (limit - p) < length)
-    {
-      cpp_error (pfile, "incomplete universal-character-name");
-      /* Skip to the end to avoid more diagnostics.  */
-      p = limit;
-    }
-  else
+  result = _cpp_valid_ucn (pfile, pstr, false);
+  if (result)
     {
-      for (; length; length--, p++)
+      if (CPP_WTRADITIONAL (pfile))
+       cpp_error (pfile, DL_WARNING,
+                  "the meaning of '\\%c' is different in traditional C",
+                  (int) c);
+
+      if (CPP_OPTION (pfile, EBCDIC))
        {
-         c = *p;
-         if (ISXDIGIT (c))
-           code = (code << 4) + hex_digit_value (c);
-         else
-           {
-             cpp_error (pfile,
-                        "non-hex digit '%c' in universal-character-name", c);
-             /* We shouldn't skip in case there are multibyte chars.  */
-             break;
-           }
+         cpp_error (pfile, DL_ERROR,
+                    "universal character with an EBCDIC target");
+         result = 0x3f;  /* EBCDIC invalid character */
        }
     }
 
-#ifdef TARGET_EBCDIC
-  cpp_error (pfile, "universal-character-name on EBCDIC target");
-  code = 0x3f;  /* EBCDIC invalid character */
-#else
- /* True extended characters are OK.  */
-  if (code >= 0xa0
-      && !(code & 0x80000000)
-      && !(code >= 0xD800 && code <= 0xDFFF))
-    ;
-  /* The standard permits $, @ and ` to be specified as UCNs.  We use
-     hex escapes so that this also works with EBCDIC hosts.  */
-  else if (code == 0x24 || code == 0x40 || code == 0x60)
-    ;
-  /* Don't give another error if one occurred above.  */
-  else if (length == 0)
-    cpp_error (pfile, "universal-character-name out of range");
-#endif
-
-  *pstr = p;
-  *pc = code;
-  return 0;
+  return result;
 }
 
-/* Interpret an escape sequence, and return its value.  PSTR points to
-   the input pointer, which is just after the backslash.  LIMIT is how
-   much text we have.  MASK is a bitmask for the precision for the
-   destination type (char or wchar_t).  TRADITIONAL, if true, does not
-   interpret escapes that did not exist in traditional C.
-
-   Handles all relevant diagnostics.  */
-
-unsigned int
-cpp_parse_escape (pfile, pstr, limit, mask, traditional)
+/* Returns the value of an escape sequence, truncated to the correct
+   target precision.  PSTR points to the input pointer, which is just
+   after the backslash.  LIMIT is how much text we have.  WIDE is true
+   if the escape sequence is part of a wide character constant or
+   string literal.  Handles all relevant diagnostics.  */
+cppchar_t
+cpp_parse_escape (pfile, pstr, limit, wide)
      cpp_reader *pfile;
      const unsigned char **pstr;
      const unsigned char *limit;
-     unsigned HOST_WIDE_INT mask;
-     int traditional;
+     int wide;
 {
+  /* Values of \a \b \e \f \n \r \t \v respectively.  */
+  static const uchar ascii[]  = {  7,  8, 27, 12, 10, 13,  9, 11 };
+  static const uchar ebcdic[] = { 47, 22, 39, 12, 21, 13,  5, 11 };
+
   int unknown = 0;
-  const unsigned char *str = *pstr;
-  unsigned int c = *str++;
+  const unsigned char *str = *pstr, *charconsts;
+  cppchar_t c, ucn, mask;
+  unsigned int width;
+
+  if (CPP_OPTION (pfile, EBCDIC))
+    charconsts = ebcdic;
+  else
+    charconsts = ascii;
+
+  if (wide)
+    width = CPP_OPTION (pfile, wchar_precision);
+  else
+    width = CPP_OPTION (pfile, char_precision);
+  if (width < BITS_PER_CPPCHAR_T)
+    mask = ((cppchar_t) 1 << width) - 1;
+  else
+    mask = ~0;
 
+  c = *str++;
   switch (c)
     {
     case '\\': case '\'': case '"': case '?': break;
-    case 'b': c = TARGET_BS;     break;
-    case 'f': c = TARGET_FF;     break;
-    case 'n': c = TARGET_NEWLINE; break;
-    case 'r': c = TARGET_CR;     break;
-    case 't': c = TARGET_TAB;    break;
-    case 'v': c = TARGET_VT;     break;
+    case 'b': c = charconsts[1];  break;
+    case 'f': c = charconsts[3];  break;
+    case 'n': c = charconsts[4];  break;
+    case 'r': c = charconsts[5];  break;
+    case 't': c = charconsts[6];  break;
+    case 'v': c = charconsts[7];  break;
 
     case '(': case '{': case '[': case '%':
       /* '\(', etc, are used at beginning of line to avoid confusing Emacs.
@@ -1878,58 +1428,65 @@ cpp_parse_escape (pfile, pstr, limit, mask, traditional)
 
     case 'a':
       if (CPP_WTRADITIONAL (pfile))
-       cpp_warning (pfile, "the meaning of '\\a' varies with -traditional");
-      if (!traditional)
-       c = TARGET_BELL;
+       cpp_error (pfile, DL_WARNING,
+                  "the meaning of '\\a' is different in traditional C");
+      c = charconsts[0];
       break;
 
     case 'e': case 'E':
       if (CPP_PEDANTIC (pfile))
-       cpp_pedwarn (pfile, "non-ISO-standard escape sequence, '\\%c'", c);
-      c = TARGET_ESC;
+       cpp_error (pfile, DL_PEDWARN,
+                  "non-ISO-standard escape sequence, '\\%c'", (int) c);
+      c = charconsts[2];
       break;
-      
+
     case 'u': case 'U':
-      unknown = maybe_read_ucs (pfile, &str, limit, &c);
+      ucn = maybe_read_ucn (pfile, &str);
+      if (ucn)
+       c = ucn;
+      else
+       unknown = true;
       break;
 
     case 'x':
       if (CPP_WTRADITIONAL (pfile))
-       cpp_warning (pfile, "the meaning of '\\x' varies with -traditional");
+       cpp_error (pfile, DL_WARNING,
+                  "the meaning of '\\x' is different in traditional C");
 
-      if (!traditional)
-       {
-         unsigned int i = 0, overflow = 0;
-         int digits_found = 0;
+      {
+       cppchar_t i = 0, overflow = 0;
+       int digits_found = 0;
 
-         while (str < limit)
-           {
-             c = *str;
-             if (! ISXDIGIT (c))
-               break;
-             str++;
-             overflow |= i ^ (i << 4 >> 4);
-             i = (i << 4) + hex_digit_value (c);
-             digits_found = 1;
-           }
+       while (str < limit)
+         {
+           c = *str;
+           if (! ISXDIGIT (c))
+             break;
+           str++;
+           overflow |= i ^ (i << 4 >> 4);
+           i = (i << 4) + hex_digit_value (c);
+           digits_found = 1;
+         }
 
-         if (!digits_found)
-           cpp_error (pfile, "\\x used with no following hex digits");
+       if (!digits_found)
+         cpp_error (pfile, DL_ERROR,
+                      "\\x used with no following hex digits");
 
-         if (overflow | (i != (i & mask)))
-           {
-             cpp_pedwarn (pfile, "hex escape sequence out of range");
-             i &= mask;
-           }
-         c = i;
-       }
+       if (overflow | (i != (i & mask)))
+         {
+           cpp_error (pfile, DL_PEDWARN,
+                      "hex escape sequence out of range");
+           i &= mask;
+         }
+       c = i;
+      }
       break;
 
     case '0':  case '1':  case '2':  case '3':
     case '4':  case '5':  case '6':  case '7':
       {
-       unsigned int i = c - '0';
-       int count = 0;
+       size_t count = 0;
+       cppchar_t i = c - '0';
 
        while (str < limit && ++count < 3)
          {
@@ -1942,7 +1499,8 @@ cpp_parse_escape (pfile, pstr, limit, mask, traditional)
 
        if (i != (i & mask))
          {
-           cpp_pedwarn (pfile, "octal escape sequence out of range");
+           cpp_error (pfile, DL_PEDWARN,
+                      "octal escape sequence out of range");
            i &= mask;
          }
        c = i;
@@ -1957,277 +1515,298 @@ cpp_parse_escape (pfile, pstr, limit, mask, traditional)
   if (unknown)
     {
       if (ISGRAPH (c))
-       cpp_pedwarn (pfile, "unknown escape sequence '\\%c'", c);
+       cpp_error (pfile, DL_PEDWARN,
+                  "unknown escape sequence '\\%c'", (int) c);
       else
-       cpp_pedwarn (pfile, "unknown escape sequence: '\\%03o'", c);
+       cpp_error (pfile, DL_PEDWARN,
+                  "unknown escape sequence: '\\%03o'", (int) c);
     }
 
   if (c > mask)
-    cpp_pedwarn (pfile, "escape sequence out of range for character");
+    {
+      cpp_error (pfile, DL_PEDWARN, "escape sequence out of range for its type");
+      c &= mask;
+    }
 
   *pstr = str;
   return c;
 }
 
-#ifndef MAX_CHAR_TYPE_SIZE
-#define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
-#endif
-
-#ifndef MAX_WCHAR_TYPE_SIZE
-#define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
-#endif
-
 /* Interpret a (possibly wide) character constant in TOKEN.
-   WARN_MULTI warns about multi-character charconsts, if not
-   TRADITIONAL.  TRADITIONAL also indicates not to interpret escapes
-   that did not exist in traditional C.  PCHARS_SEEN points to a
-   variable that is filled in with the number of characters seen.  */
-HOST_WIDE_INT
-cpp_interpret_charconst (pfile, token, warn_multi, traditional, pchars_seen)
+   WARN_MULTI warns about multi-character charconsts.  PCHARS_SEEN
+   points to a variable that is filled in with the number of
+   characters seen, and UNSIGNEDP to a variable that indicates whether
+   the result has signed type.  */
+cppchar_t
+cpp_interpret_charconst (pfile, token, pchars_seen, unsignedp)
      cpp_reader *pfile;
      const cpp_token *token;
-     int warn_multi;
-     int traditional;
      unsigned int *pchars_seen;
+     int *unsignedp;
 {
-  const unsigned char *str = token->val.str.text;
-  const unsigned char *limit = str + token->val.str.len;
+  const unsigned char *str, *limit;
   unsigned int chars_seen = 0;
-  unsigned int width, max_chars, c;
-  unsigned HOST_WIDE_INT mask;
-  HOST_WIDE_INT result = 0;
+  size_t width, max_chars;
+  cppchar_t c, mask, result = 0;
+  bool unsigned_p;
 
-#ifdef MULTIBYTE_CHARS
-  (void) local_mbtowc (NULL, NULL, 0);
-#endif
+  str = token->val.str.text + 1 + (token->type == CPP_WCHAR);
+  limit = token->val.str.text + token->val.str.len - 1;
 
-  /* Width in bits.  */
   if (token->type == CPP_CHAR)
-    width = MAX_CHAR_TYPE_SIZE;
+    {
+      width = CPP_OPTION (pfile, char_precision);
+      max_chars = CPP_OPTION (pfile, int_precision) / width;
+      unsigned_p = CPP_OPTION (pfile, unsigned_char);
+    }
   else
-    width = MAX_WCHAR_TYPE_SIZE;
+    {
+      width = CPP_OPTION (pfile, wchar_precision);
+      max_chars = 1;
+      unsigned_p = CPP_OPTION (pfile, unsigned_wchar);
+    }
 
-  if (width < HOST_BITS_PER_WIDE_INT)
-    mask = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
+  if (width < BITS_PER_CPPCHAR_T)
+    mask = ((cppchar_t) 1 << width) - 1;
   else
     mask = ~0;
-  max_chars = HOST_BITS_PER_WIDE_INT / width;
 
   while (str < limit)
     {
-#ifdef MULTIBYTE_CHARS
-      wchar_t wc;
-      int char_len;
-
-      char_len = local_mbtowc (&wc, str, limit - str);
-      if (char_len == -1)
-       {
-         cpp_warning (pfile, "ignoring invalid multibyte character");
-         c = *str++;
-       }
-      else
-       {
-         str += char_len;
-         c = wc;
-       }
-#else
       c = *str++;
-#endif
 
       if (c == '\\')
-       c = cpp_parse_escape (pfile, &str, limit, mask, traditional);
+       c = cpp_parse_escape (pfile, &str, limit, token->type == CPP_WCHAR);
 
 #ifdef MAP_CHARACTER
       if (ISPRINT (c))
        c = MAP_CHARACTER (c);
 #endif
-      
-      /* Merge character into result; ignore excess chars.  */
-      if (++chars_seen <= max_chars)
-       {
-         if (width < HOST_BITS_PER_WIDE_INT)
-           result = (result << width) | (c & mask);
-         else
-           result = c;
-       }
+
+      chars_seen++;
+
+      /* Truncate the character, scale the result and merge the two.  */
+      c &= mask;
+      if (width < BITS_PER_CPPCHAR_T)
+       result = (result << width) | c;
+      else
+       result = c;
     }
 
   if (chars_seen == 0)
-    cpp_error (pfile, "empty character constant");
-  else if (chars_seen > max_chars)
+    cpp_error (pfile, DL_ERROR, "empty character constant");
+  else if (chars_seen > 1)
     {
-      chars_seen = max_chars;
-      cpp_warning (pfile, "character constant too long");
+      /* Multichar charconsts are of type int and therefore signed.  */
+      unsigned_p = 0;
+
+      if (chars_seen > max_chars)
+       {
+         chars_seen = max_chars;
+         cpp_error (pfile, DL_WARNING,
+                    "character constant too long for its type");
+       }
+      else if (CPP_OPTION (pfile, warn_multichar))
+       cpp_error (pfile, DL_WARNING, "multi-character character constant");
     }
-  else if (chars_seen > 1 && !traditional && warn_multi)
-    cpp_warning (pfile, "multi-character character constant");
 
-  /* If char type is signed, sign-extend the constant.  The
-     __CHAR_UNSIGNED__ macro is set by the driver if appropriate.  */
-  if (token->type == CPP_CHAR && chars_seen)
+  /* Sign-extend or truncate the constant to cppchar_t.  The value is
+     in WIDTH bits, but for multi-char charconsts it's value is the
+     full target type's width.  */
+  if (chars_seen > 1)
+    width *= max_chars;
+  if (width < BITS_PER_CPPCHAR_T)
     {
-      unsigned int nbits = chars_seen * width;
-      unsigned int mask = (unsigned int) ~0 >> (HOST_BITS_PER_INT - nbits);
-
-      if (pfile->spec_nodes.n__CHAR_UNSIGNED__->type == NT_MACRO
-         || ((result >> (nbits - 1)) & 1) == 0)
+      mask = ((cppchar_t) 1 << width) - 1;
+      if (unsigned_p || !(result & (1 << (width - 1))))
        result &= mask;
       else
        result |= ~mask;
     }
 
   *pchars_seen = chars_seen;
+  *unsignedp = unsigned_p;
   return result;
 }
 
-/* Memory pools.  */
+/* Memory buffers.  Changing these three constants can have a dramatic
+   effect on performance.  The values here are reasonable defaults,
+   but might be tuned.  If you adjust them, be sure to test across a
+   range of uses of cpplib, including heavy nested function-like macro
+   expansion.  Also check the change in peak memory usage (NJAMD is a
+   good tool for this).  */
+#define MIN_BUFF_SIZE 8000
+#define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2)
+#define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \
+       (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2)
+
+#if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0)
+  #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
+#endif
 
-struct dummy
+/* Create a new allocation buffer.  Place the control block at the end
+   of the buffer, so that buffer overflows will cause immediate chaos.  */
+static _cpp_buff *
+new_buff (len)
+     size_t len;
 {
-  char c;
-  union
-  {
-    double d;
-    int *p;
-  } u;
-};
+  _cpp_buff *result;
+  unsigned char *base;
 
-#define DEFAULT_ALIGNMENT (offsetof (struct dummy, u))
+  if (len < MIN_BUFF_SIZE)
+    len = MIN_BUFF_SIZE;
+  len = CPP_ALIGN (len);
 
-static int
-chunk_suitable (pool, chunk, size)
-     cpp_pool *pool;
-     cpp_chunk *chunk;
-     unsigned int size;
-{
-  /* Being at least twice SIZE means we can use memcpy in
-     _cpp_next_chunk rather than memmove.  Besides, it's a good idea
-     anyway.  */
-  return (chunk && pool->locked != chunk
-         && (unsigned int) (chunk->limit - chunk->base) >= size * 2);
+  base = xmalloc (len + sizeof (_cpp_buff));
+  result = (_cpp_buff *) (base + len);
+  result->base = base;
+  result->cur = base;
+  result->limit = base + len;
+  result->next = NULL;
+  return result;
 }
 
-/* Returns the end of the new pool.  PTR points to a char in the old
-   pool, and is updated to point to the same char in the new pool.  */
-unsigned char *
-_cpp_next_chunk (pool, len, ptr)
-     cpp_pool *pool;
-     unsigned int len;
-     unsigned char **ptr;
+/* Place a chain of unwanted allocation buffers on the free list.  */
+void
+_cpp_release_buff (pfile, buff)
+     cpp_reader *pfile;
+     _cpp_buff *buff;
 {
-  cpp_chunk *chunk = pool->cur->next;
-
-  /* LEN is the minimum size we want in the new pool.  */
-  len += POOL_ROOM (pool);
-  if (! chunk_suitable (pool, chunk, len))
-    {
-      chunk = new_chunk (POOL_SIZE (pool) * 2 + len);
-
-      chunk->next = pool->cur->next;
-      pool->cur->next = chunk;
-    }
-
-  /* Update the pointer before changing chunk's front.  */
-  if (ptr)
-    *ptr += chunk->base - POOL_FRONT (pool);
+  _cpp_buff *end = buff;
 
-  memcpy (chunk->base, POOL_FRONT (pool), POOL_ROOM (pool));
-  chunk->front = chunk->base;
-
-  pool->cur = chunk;
-  return POOL_LIMIT (pool);
+  while (end->next)
+    end = end->next;
+  end->next = pfile->free_buffs;
+  pfile->free_buffs = buff;
 }
 
-static cpp_chunk *
-new_chunk (size)
-     unsigned int size;
+/* Return a free buffer of size at least MIN_SIZE.  */
+_cpp_buff *
+_cpp_get_buff (pfile, min_size)
+     cpp_reader *pfile;
+     size_t min_size;
 {
-  unsigned char *base;
-  cpp_chunk *result;
+  _cpp_buff *result, **p;
 
-  size = POOL_ALIGN (size, DEFAULT_ALIGNMENT);
-  base = (unsigned char *) xmalloc (size + sizeof (cpp_chunk));
-  /* Put the chunk descriptor at the end.  Then chunk overruns will
-     cause obvious chaos.  */
-  result = (cpp_chunk *) (base + size);
-  result->base = base;
-  result->front = base;
-  result->limit = base + size;
-  result->next = 0;
+  for (p = &pfile->free_buffs;; p = &(*p)->next)
+    {
+      size_t size;
+
+      if (*p == NULL)
+       return new_buff (min_size);
+      result = *p;
+      size = result->limit - result->base;
+      /* Return a buffer that's big enough, but don't waste one that's
+         way too big.  */
+      if (size >= min_size && size <= BUFF_SIZE_UPPER_BOUND (min_size))
+       break;
+    }
 
+  *p = result->next;
+  result->next = NULL;
+  result->cur = result->base;
   return result;
 }
 
-void
-_cpp_init_pool (pool, size, align, temp)
-     cpp_pool *pool;
-     unsigned int size, align, temp;
+/* Creates a new buffer with enough space to hold the uncommitted
+   remaining bytes of BUFF, and at least MIN_EXTRA more bytes.  Copies
+   the excess bytes to the new buffer.  Chains the new buffer after
+   BUFF, and returns the new buffer.  */
+_cpp_buff *
+_cpp_append_extend_buff (pfile, buff, min_extra)
+     cpp_reader *pfile;
+     _cpp_buff *buff;
+     size_t min_extra;
 {
-  if (align == 0)
-    align = DEFAULT_ALIGNMENT;
-  if (align & (align - 1))
-    abort ();
-  pool->align = align;
-  pool->first = new_chunk (size);
-  pool->cur = pool->first;
-  pool->locked = 0;
-  pool->locks = 0;
-  if (temp)
-    pool->cur->next = pool->cur;
-}
+  size_t size = EXTENDED_BUFF_SIZE (buff, min_extra);
+  _cpp_buff *new_buff = _cpp_get_buff (pfile, size);
 
-void
-_cpp_lock_pool (pool)
-     cpp_pool *pool;
-{
-  if (pool->locks++ == 0)
-    pool->locked = pool->cur;
+  buff->next = new_buff;
+  memcpy (new_buff->base, buff->cur, BUFF_ROOM (buff));
+  return new_buff;
 }
 
+/* Creates a new buffer with enough space to hold the uncommitted
+   remaining bytes of the buffer pointed to by BUFF, and at least
+   MIN_EXTRA more bytes.  Copies the excess bytes to the new buffer.
+   Chains the new buffer before the buffer pointed to by BUFF, and
+   updates the pointer to point to the new buffer.  */
 void
-_cpp_unlock_pool (pool)
-     cpp_pool *pool;
+_cpp_extend_buff (pfile, pbuff, min_extra)
+     cpp_reader *pfile;
+     _cpp_buff **pbuff;
+     size_t min_extra;
 {
-  if (--pool->locks == 0)
-    pool->locked = 0;
+  _cpp_buff *new_buff, *old_buff = *pbuff;
+  size_t size = EXTENDED_BUFF_SIZE (old_buff, min_extra);
+
+  new_buff = _cpp_get_buff (pfile, size);
+  memcpy (new_buff->base, old_buff->cur, BUFF_ROOM (old_buff));
+  new_buff->next = old_buff;
+  *pbuff = new_buff;
 }
 
+/* Free a chain of buffers starting at BUFF.  */
 void
-_cpp_free_pool (pool)
-     cpp_pool *pool;
+_cpp_free_buff (buff)
+     _cpp_buff *buff;
 {
-  cpp_chunk *chunk = pool->first, *next;
+  _cpp_buff *next;
 
-  do
+  for (; buff; buff = next)
     {
-      next = chunk->next;
-      free (chunk->base);
-      chunk = next;
+      next = buff->next;
+      free (buff->base);
     }
-  while (chunk && chunk != pool->first);
 }
 
-/* Reserve LEN bytes from a memory pool.  */
+/* Allocate permanent, unaligned storage of length LEN.  */
 unsigned char *
-_cpp_pool_reserve (pool, len)
-     cpp_pool *pool;
-     unsigned int len;
+_cpp_unaligned_alloc (pfile, len)
+     cpp_reader *pfile;
+     size_t len;
 {
-  len = POOL_ALIGN (len, pool->align);
-  if (len > (unsigned int) POOL_ROOM (pool))
-    _cpp_next_chunk (pool, len, 0);
+  _cpp_buff *buff = pfile->u_buff;
+  unsigned char *result = buff->cur;
+
+  if (len > (size_t) (buff->limit - result))
+    {
+      buff = _cpp_get_buff (pfile, len);
+      buff->next = pfile->u_buff;
+      pfile->u_buff = buff;
+      result = buff->cur;
+    }
 
-  return POOL_FRONT (pool);
+  buff->cur = result + len;
+  return result;
 }
 
-/* Allocate LEN bytes from a memory pool.  */
+/* Allocate permanent, unaligned storage of length LEN from a_buff.
+   That buffer is used for growing allocations when saving macro
+   replacement lists in a #define, and when parsing an answer to an
+   assertion in #assert, #unassert or #if (and therefore possibly
+   whilst expanding macros).  It therefore must not be used by any
+   code that they might call: specifically the lexer and the guts of
+   the macro expander.
+
+   All existing other uses clearly fit this restriction: storing
+   registered pragmas during initialization.  */
 unsigned char *
-_cpp_pool_alloc (pool, len)
-     cpp_pool *pool;
-     unsigned int len;
+_cpp_aligned_alloc (pfile, len)
+     cpp_reader *pfile;
+     size_t len;
 {
-  unsigned char *result = _cpp_pool_reserve (pool, len);
+  _cpp_buff *buff = pfile->a_buff;
+  unsigned char *result = buff->cur;
+
+  if (len > (size_t) (buff->limit - result))
+    {
+      buff = _cpp_get_buff (pfile, len);
+      buff->next = pfile->a_buff;
+      pfile->a_buff = buff;
+      result = buff->cur;
+    }
 
-  POOL_COMMIT (pool, len);
+  buff->cur = result + len;
   return result;
 }