OSDN Git Service

2004-05-13 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / cpplex.c
index 4630645..d03096c 100644 (file)
@@ -1,5 +1,5 @@
 /* CPP Library - lexical analysis.
-   Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002, 2003, 2004 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
@@ -21,8 +21,6 @@ Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "config.h"
 #include "system.h"
-#include "coretypes.h"
-#include "tm.h"
 #include "cpplib.h"
 #include "cpphash.h"
 
@@ -90,8 +88,8 @@ add_line_note (cpp_buffer *buffer, const uchar *pos, unsigned int type)
   if (buffer->notes_used == buffer->notes_cap)
     {
       buffer->notes_cap = buffer->notes_cap * 2 + 200;
-      buffer->notes = (_cpp_line_note *)
-       xrealloc (buffer->notes, buffer->notes_cap * sizeof (_cpp_line_note));
+      buffer->notes = xrealloc (buffer->notes,
+                               buffer->notes_cap * sizeof (_cpp_line_note));
     }
 
   buffer->notes[buffer->notes_used].pos = pos;
@@ -116,7 +114,57 @@ _cpp_clean_line (cpp_reader *pfile)
 
   if (!buffer->from_stage3)
     {
-      d = (uchar *) s;
+      /* Short circuit for the common case of an un-escaped line with
+        no trigraphs.  The primary win here is by not writing any
+        data back to memory until we have to.  */
+      for (;;)
+       {
+         c = *++s;
+         if (c == '\n' || c == '\r')
+           {
+             d = (uchar *) s;
+
+             if (s == buffer->rlimit)
+               goto done;
+
+             /* DOS line ending? */
+             if (c == '\r' && s[1] == '\n')
+               s++;
+
+             if (s == buffer->rlimit)
+               goto done;
+
+             /* check for escaped newline */
+             p = d;
+             while (p != buffer->next_line && is_nvspace (p[-1]))
+               p--;
+             if (p == buffer->next_line || p[-1] != '\\')
+               goto done;
+
+             /* Have an escaped newline; process it and proceed to
+                the slow path.  */
+             add_line_note (buffer, p - 1, p != d ? ' ' : '\\');
+             d = p - 2;
+             buffer->next_line = p - 1;
+             break;
+           }
+         if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
+           {
+             /* Have a trigraph.  We may or may not have to convert
+                it.  Add a line note regardless, for -Wtrigraphs.  */
+             add_line_note (buffer, s, s[2]);
+             if (CPP_OPTION (pfile, trigraphs))
+               {
+                 /* We do, and that means we have to switch to the
+                    slow path.  */
+                 d = (uchar *) s;
+                 *d = _cpp_trigraph_map[s[2]];
+                 s += 2;
+                 break;
+               }
+           }
+       }
+
 
       for (;;)
        {
@@ -166,6 +214,7 @@ _cpp_clean_line (cpp_reader *pfile)
        s++;
     }
 
+ done:
   *d = '\n';
   /* A sentinel note that should never be processed.  */
   add_line_note (buffer, d + 1, '\n');
@@ -221,19 +270,19 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
       if (note->type == '\\' || note->type == ' ')
        {
          if (note->type == ' ' && !in_comment)
-           cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
+           cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
                                 "backslash and newline separated by space");
 
          if (buffer->next_line > buffer->rlimit)
            {
-             cpp_error_with_line (pfile, DL_PEDWARN, pfile->line, col,
+             cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_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++;
+         CPP_INCREMENT_LINE (pfile, 0);
        }
       else if (_cpp_trigraph_map[note->type])
        {
@@ -241,14 +290,17 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
              && (!in_comment || warn_in_comment (pfile, note)))
            {
              if (CPP_OPTION (pfile, trigraphs))
-               cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
+               cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_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);
+               {
+                 cpp_error_with_line 
+                   (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
+                    "trigraph ??%c ignored, use -trigraphs to enable",
+                    note->type);
+               }
            }
        }
       else
@@ -265,43 +317,53 @@ bool
 _cpp_skip_block_comment (cpp_reader *pfile)
 {
   cpp_buffer *buffer = pfile->buffer;
-  cppchar_t c;
+  const uchar *cur = buffer->cur;
+  uchar c;
 
-  buffer->cur++;
-  if (*buffer->cur == '/')
-    buffer->cur++;
+  cur++;
+  if (*cur == '/')
+    cur++;
 
   for (;;)
     {
-      c = *buffer->cur++;
-
       /* People like decorating comments with '*', so check for '/'
         instead for efficiency.  */
+      c = *cur++;
+
       if (c == '/')
        {
-         if (buffer->cur[-2] == '*')
+         if (cur[-2] == '*')
            break;
 
          /* Warn about potential nested comments, but not if the '/'
             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[0] == '*' && buffer->cur[1] != '/')
-           cpp_error_with_line (pfile, DL_WARNING,
-                                pfile->line, CPP_BUF_COL (buffer),
-                                "\"/*\" within comment");
+             && cur[0] == '*' && cur[1] != '/')
+           {
+             buffer->cur = cur;
+             cpp_error_with_line (pfile, CPP_DL_WARNING,
+                                  pfile->line_table->highest_line, CPP_BUF_COL (buffer),
+                                  "\"/*\" within comment");
+           }
        }
       else if (c == '\n')
        {
-         buffer->cur--;
+         unsigned int cols;
+         buffer->cur = cur - 1;
          _cpp_process_line_notes (pfile, true);
          if (buffer->next_line >= buffer->rlimit)
            return true;
          _cpp_clean_line (pfile);
-         pfile->line++;
+
+         cols = buffer->next_line - buffer->line_base;
+         CPP_INCREMENT_LINE (pfile, cols);
+
+         cur = buffer->cur;
        }
     }
 
+  buffer->cur = cur;
   _cpp_process_line_notes (pfile, true);
   return false;
 }
@@ -313,13 +375,13 @@ static int
 skip_line_comment (cpp_reader *pfile)
 {
   cpp_buffer *buffer = pfile->buffer;
-  unsigned int orig_line = pfile->line;
+  unsigned int orig_line = pfile->line_table->highest_line;
 
   while (*buffer->cur != '\n')
     buffer->cur++;
 
   _cpp_process_line_notes (pfile, true);
-  return orig_line != pfile->line;
+  return orig_line != pfile->line_table->highest_line;
 }
 
 /* Skips whitespace, saving the next non-whitespace character.  */
@@ -338,7 +400,7 @@ skip_whitespace (cpp_reader *pfile, cppchar_t c)
       else if (c == '\0')
        saw_NUL = true;
       else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
-       cpp_error_with_line (pfile, DL_PEDWARN, pfile->line,
+       cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line,
                             CPP_BUF_COL (buffer),
                             "%s in preprocessing directive",
                             c == '\f' ? "form feed" : "vertical tab");
@@ -349,7 +411,7 @@ skip_whitespace (cpp_reader *pfile, cppchar_t c)
   while (is_nvspace (c));
 
   if (saw_NUL)
-    cpp_error (pfile, DL_WARNING, "null character(s) ignored");
+    cpp_error (pfile, CPP_DL_WARNING, "null character(s) ignored");
 
   buffer->cur--;
 }
@@ -384,7 +446,7 @@ forms_identifier_p (cpp_reader *pfile, int first)
       if (CPP_OPTION (pfile, warn_dollars) && !pfile->state.skipping)
        {
          CPP_OPTION (pfile, warn_dollars) = 0;
-         cpp_error (pfile, DL_PEDWARN, "'$' in identifier or number");
+         cpp_error (pfile, CPP_DL_PEDWARN, "'$' in identifier or number");
        }
 
       return true;
@@ -431,14 +493,14 @@ lex_identifier (cpp_reader *pfile, const uchar *base)
     {
       /* It is allowed to poison the same identifier twice.  */
       if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
-       cpp_error (pfile, DL_ERROR, "attempt to use poisoned \"%s\"",
+       cpp_error (pfile, CPP_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_error (pfile, DL_PEDWARN,
+       cpp_error (pfile, CPP_DL_PEDWARN,
                   "__VA_ARGS__ can only appear in the expansion"
                   " of a C99 variadic macro");
     }
@@ -534,7 +596,8 @@ lex_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
     }
 
   if (saw_NUL && !pfile->state.skipping)
-    cpp_error (pfile, DL_WARNING, "null character(s) preserved in literal");
+    cpp_error (pfile, CPP_DL_WARNING,
+              "null character(s) preserved in literal");
 
   pfile->buffer->cur = cur;
   create_literal (pfile, token, base, cur - base, type);
@@ -621,8 +684,7 @@ _cpp_temp_token (cpp_reader *pfile)
     }
 
   result = pfile->cur_token++;
-  result->line = old->line;
-  result->col = old->col;
+  result->src_loc = old->src_loc;
   return result;
 }
 
@@ -685,6 +747,8 @@ _cpp_lex_token (cpp_reader *pfile)
 bool
 _cpp_get_fresh_line (cpp_reader *pfile)
 {
+  int return_at_eof;
+
   /* We can't get a new line until we leave the current directive.  */
   if (pfile->state.in_directive)
     return false;
@@ -713,21 +777,15 @@ _cpp_get_fresh_line (cpp_reader *pfile)
        {
          /* Only warn once.  */
          buffer->next_line = buffer->rlimit;
-         cpp_error_with_line (pfile, DL_PEDWARN, pfile->line - 1,
+         cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line,
                               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;
-       }
 
+      return_at_eof = buffer->return_at_eof;
       _cpp_pop_buffer (pfile);
+      if (pfile->buffer == NULL || return_at_eof)
+       return false;
     }
 }
 
@@ -761,7 +819,8 @@ _cpp_lex_direct (cpp_reader *pfile)
 
  fresh_line:
   result->flags = 0;
-  if (pfile->buffer->need_line)
+  buffer = pfile->buffer;
+  if (buffer->need_line)
     {
       if (!_cpp_get_fresh_line (pfile))
        {
@@ -769,7 +828,7 @@ _cpp_lex_direct (cpp_reader *pfile)
          if (!pfile->state.in_directive)
            {
              /* Tell the compiler the line number of the EOF token.  */
-             result->line = pfile->line;
+             result->src_loc = pfile->line_table->highest_line;
              result->flags = BOL;
            }
          return result;
@@ -786,17 +845,19 @@ _cpp_lex_direct (cpp_reader *pfile)
     }
   buffer = pfile->buffer;
  update_tokens_line:
-  result->line = pfile->line;
+  result->src_loc = pfile->line_table->highest_line;
 
  skipped_white:
   if (buffer->cur >= buffer->notes[buffer->cur_note].pos
       && !pfile->overlaid_buffer)
     {
       _cpp_process_line_notes (pfile, false);
-      result->line = pfile->line;
+      result->src_loc = pfile->line_table->highest_line;
     }
   c = *buffer->cur++;
-  result->col = CPP_BUF_COLUMN (buffer, buffer->cur);
+
+  LINEMAP_POSITION_FOR_COLUMN (result->src_loc, pfile->line_table,
+                              CPP_BUF_COLUMN (buffer, buffer->cur));
 
   switch (c)
     {
@@ -806,7 +867,8 @@ _cpp_lex_direct (cpp_reader *pfile)
       goto skipped_white;
 
     case '\n':
-      pfile->line++;
+      if (buffer->cur < buffer->rlimit)
+       CPP_INCREMENT_LINE (pfile, 0);
       buffer->need_line = true;
       goto fresh_line;
 
@@ -860,25 +922,25 @@ _cpp_lex_direct (cpp_reader *pfile)
       if (c == '*')
        {
          if (_cpp_skip_block_comment (pfile))
-           cpp_error (pfile, DL_ERROR, "unterminated comment");
+           cpp_error (pfile, CPP_DL_ERROR, "unterminated comment");
        }
       else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments)
-                           || CPP_IN_SYSTEM_HEADER (pfile)))
+                           || cpp_in_system_header (pfile)))
        {
          /* 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_error (pfile, DL_PEDWARN,
+             cpp_error (pfile, CPP_DL_PEDWARN,
                         "C++ style comments are not allowed in ISO C90");
-             cpp_error (pfile, DL_PEDWARN,
+             cpp_error (pfile, CPP_DL_PEDWARN,
                         "(this will be reported only once per input file)");
              buffer->warned_cplusplus_comments = 1;
            }
 
          if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments))
-           cpp_error (pfile, DL_WARNING, "multi-line comment");
+           cpp_error (pfile, CPP_DL_WARNING, "multi-line comment");
        }
       else if (c == '=')
        {
@@ -1141,7 +1203,8 @@ cpp_spell_token (cpp_reader *pfile, const cpp_token *token,
       break;
 
     case SPELL_NONE:
-      cpp_error (pfile, DL_ICE, "unspellable token %s", TOKEN_NAME (token));
+      cpp_error (pfile, CPP_DL_ICE,
+                "unspellable token %s", TOKEN_NAME (token));
       break;
     }