OSDN Git Service

2003-02-12 Phil Edwards <pme@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / cpptrad.c
index 22320c4..92c4ee3 100644 (file)
@@ -18,17 +18,19 @@ 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"
 
 /* The replacement text of a function-like macro is stored as a
    contiguous sequence of aligned blocks, each representing the text
-   between subsequent parameters in that text.
+   between subsequent parameters.
 
-   Each block comprises the length of text contained therein, the
-   one-based index of the argument that immediately follows that text,
-   and the text itself.  The final block in the macro expansion is
-   easily recognizable as it has an argument index of zero.  */
+   Each block comprises the text between its surrounding parameters,
+   the length of that text, and the one-based index of the following
+   parameter.  The final block in the replacement text is easily
+   recognizable as it has an argument index of zero.  */
 
 struct block
 {
@@ -139,9 +141,9 @@ handle_newline (pfile, cur)
   return cur + 1;
 }
 
-/* CUR points to any character in the buffer, not necessarily a
-   backslash.  Advances CUR until all escaped newlines are skipped,
-   and returns the new position.
+/* CUR points to any character in the current context, not necessarily
+   backslash.  Advances CUR until all escaped newlines are skipped,
+   and returns the new position without updating the context.
 
    Warns if a file buffer ends in an escaped newline.  */
 static const uchar *
@@ -160,9 +162,9 @@ skip_escaped_newlines (pfile, cur)
   return cur;
 }
 
-/* CUR points to the asterisk introducing a comment in the input
-   buffer.  IN_DEFINE is true if we are in the replacement text
-   of a macro.
+/* CUR points to the asterisk introducing a comment in the current
+   context.  IN_DEFINE is true if we are in the replacement text of a
+   macro.
 
    The asterisk and following comment is copied to the buffer pointed
    to by pfile->out.cur, which must be of sufficient size.
@@ -284,7 +286,7 @@ skip_whitespace (pfile, cur, skip_comments)
       out--;
       if (c == '\\' && is_vspace (*cur))
        {
-         cur = skip_escaped_newlines (pfile, cur);
+         cur = skip_escaped_newlines (pfile, cur - 1);
          continue;
        }
 
@@ -364,31 +366,24 @@ bool
 _cpp_read_logical_line_trad (pfile)
      cpp_reader *pfile;
 {
-  cpp_buffer *buffer = pfile->buffer;
-
   do
     {
-      if (buffer->cur == buffer->rlimit)
+      if (pfile->buffer->cur == pfile->buffer->rlimit)
        {
          bool stop = true;
 
          /* Don't pop the last buffer.  */
-         if (buffer->prev)
+         if (pfile->buffer->prev)
            {
-             stop = buffer->return_at_eof;
+             stop = pfile->buffer->return_at_eof;
              _cpp_pop_buffer (pfile);
-             buffer = pfile->buffer;
            }
 
          if (stop)
            return false;
        }
 
-      CUR (pfile->context) = buffer->cur;
-      RLIMIT (pfile->context) = buffer->rlimit;
       scan_out_logical_line (pfile, NULL);
-      buffer = pfile->buffer;
-      buffer->cur = CUR (pfile->context);
     }
   while (pfile->state.skipping);
 
@@ -426,9 +421,9 @@ save_argument (macro, offset)
     macro->args[macro->argc] = offset;
 }
 
-/* Copies the next logical line in the current buffer to the output
-   buffer.  The output is guaranteed to terminate with a NUL
-   character.
+/* Copies the next logical line in the current buffer (starting at
+   buffer->cur) to the output buffer.  The output is guaranteed to
+   terminate with a NUL character.  buffer->cur is updated.
 
    If MACRO is non-NULL, then we are scanning the replacement list of
    MACRO, and we call save_replacement_text() every time we meet an
@@ -442,12 +437,17 @@ scan_out_logical_line (pfile, macro)
   const uchar *cur;
   uchar *out;
   struct fun_macro fmacro;
-  unsigned int c, paren_depth = 0, quote = 0;
+  unsigned int c, paren_depth = 0, quote;
   enum ls lex_state = ls_none;
+  bool header_ok;
 
   fmacro.buff = NULL;
 
  start_logical_line:
+  quote = 0;
+  header_ok = pfile->state.angled_headers;
+  CUR (pfile->context) = pfile->buffer->cur;
+  RLIMIT (pfile->context) = pfile->buffer->rlimit;
   pfile->out.cur = pfile->out.base;
   pfile->out.first_line = pfile->line;
  new_context:
@@ -495,7 +495,8 @@ scan_out_logical_line (pfile, macro)
          if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
              && !pfile->state.in_directive)
            {
-             /* Newlines in arguments become a space.  */
+             /* Newlines in arguments become a space, but we don't
+                clear any in-progress quote.  */
              if (lex_state == ls_fun_close)
                out[-1] = ' ';
              continue;
@@ -503,15 +504,12 @@ scan_out_logical_line (pfile, macro)
          goto done;
 
        case '<':
-         if (pfile->state.angled_headers && !quote)
+         if (header_ok)
            quote = '>';
          break;
        case '>':
-         if (pfile->state.angled_headers && c == quote)
-           {
-             pfile->state.angled_headers = false;
-             quote = 0;
-           }
+         if (c == quote)
+           quote = 0;
          break;
 
        case '"':
@@ -579,8 +577,7 @@ scan_out_logical_line (pfile, macro)
              if (node->type == NT_MACRO
                  /* Should we expand for ls_answer?  */
                  && (lex_state == ls_none || lex_state == ls_fun_open)
-                 && !pfile->state.prevent_expansion
-                 && !recursive_macro (pfile, node))
+                 && !pfile->state.prevent_expansion)
                {
                  /* Macros invalidate MI optimization.  */
                  pfile->mi_valid = false;
@@ -592,7 +589,7 @@ scan_out_logical_line (pfile, macro)
                      fmacro.line = pfile->line;
                      continue;
                    }
-                 else
+                 else if (!recursive_macro (pfile, node))
                    {
                      /* Remove the object-like macro's name from the
                         output, and push its replacement text.  */
@@ -602,12 +599,12 @@ scan_out_logical_line (pfile, macro)
                      goto new_context;
                    }
                }
-             else if (macro && node->arg_index)
+             else if (macro && (node->flags & NODE_MACRO_ARG) != 0)
                {
                  /* Found a parameter in the replacement text of a
                     #define.  Remove its name from the output.  */
                  pfile->out.cur = out_start;
-                 save_replacement_text (pfile, macro, node->arg_index);
+                 save_replacement_text (pfile, macro, node->value.arg_index);
                  out = pfile->out.base;
                }
              else if (lex_state == ls_hash)
@@ -630,10 +627,15 @@ scan_out_logical_line (pfile, macro)
              paren_depth++;
              if (lex_state == ls_fun_open)
                {
-                 lex_state = ls_fun_close;
-                 paren_depth = 1;
-                 out = pfile->out.base + fmacro.offset;
-                 fmacro.args[0] = fmacro.offset;
+                 if (recursive_macro (pfile, fmacro.node))
+                   lex_state = ls_none;
+                 else
+                   {
+                     lex_state = ls_fun_close;
+                     paren_depth = 1;
+                     out = pfile->out.base + fmacro.offset;
+                     fmacro.args[0] = fmacro.offset;
+                   }
                }
              else if (lex_state == ls_predicate)
                lex_state = ls_answer;
@@ -655,6 +657,7 @@ scan_out_logical_line (pfile, macro)
                {
                  cpp_macro *m = fmacro.node->value.macro;
 
+                 m->used = 1;
                  lex_state = ls_none;
                  save_argument (&fmacro, out - pfile->out.base);
 
@@ -681,15 +684,53 @@ scan_out_logical_line (pfile, macro)
          break;
 
        case '#':
-         /* At start of a line it's a directive.  */
-         if (out - 1 == pfile->out.base && !pfile->state.in_directive)
+         if (out - 1 == pfile->out.base
+             /* A '#' from a macro doesn't start a directive.  */
+             && !pfile->context->prev
+             && !pfile->state.in_directive)
            {
-             /* This is a kludge.  We want to have the ISO
-                preprocessor lex the next token.  */
-             pfile->buffer->cur = cur;
-             if (_cpp_handle_directive (pfile, false /* indented */))
-               goto start_logical_line;
+             /* A directive.  With the way _cpp_handle_directive
+                currently works, we only want to call it if either we
+                know the directive is OK, or we want it to fail and
+                be removed from the output.  If we want it to be
+                passed through (the assembler case) then we must not
+                call _cpp_handle_directive.  */
+             pfile->out.cur = out;
+             cur = skip_whitespace (pfile, cur, true /* skip_comments */);
+             out = pfile->out.cur;
+
+             if (is_vspace (*cur))
+               {
+                 /* Null directive.  Ignore it and don't invalidate
+                    the MI optimization.  */
+                 out = pfile->out.base;
+                 continue;
+               }
+             else
+               {
+                 bool do_it = false;
+
+                 if (is_numstart (*cur)
+                     && CPP_OPTION (pfile, lang) != CLK_ASM)
+                   do_it = true;
+                 else if (is_idstart (*cur))
+                   /* Check whether we know this directive, but don't
+                      advance.  */
+                   do_it = lex_identifier (pfile, cur)->is_directive;
+
+                 if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM)
+                   {
+                     /* This is a kludge.  We want to have the ISO
+                        preprocessor lex the next token.  */
+                     pfile->buffer->cur = cur;
+                     _cpp_handle_directive (pfile, false /* indented */);
+                     /* #include changes pfile->buffer so we need to
+                        update the limits of the current context.  */
+                     goto start_logical_line;
+                   }
+               }
            }
+
          if (pfile->state.in_expression)
            {
              lex_state = ls_hash;
@@ -701,6 +742,12 @@ scan_out_logical_line (pfile, macro)
          break;
        }
 
+      /* Non-whitespace disables MI optimization and stops treating
+        '<' as a quote in #include.  */
+      header_ok = false;
+      if (!pfile->state.in_directive)
+       pfile->mi_valid = false;
+
       if (lex_state == ls_none)
        continue;
 
@@ -719,7 +766,7 @@ scan_out_logical_line (pfile, macro)
 
  done:
   out[-1] = '\0';
-  CUR (context) = cur;
+  pfile->buffer->cur = cur;
   pfile->out.cur = out - 1;
   if (fmacro.buff)
     _cpp_release_buff (pfile, fmacro.buff);
@@ -749,6 +796,7 @@ push_replacement_text (pfile, node)
   else
     {
       cpp_macro *macro = node->value.macro;
+      macro->used = 1;
       text = macro->exp.text;
       len = macro->count;
     }
@@ -762,7 +810,7 @@ recursive_macro (pfile, node)
      cpp_reader *pfile;
      cpp_hashnode *node;
 {
-  bool recursing = node->flags & NODE_DISABLED;
+  bool recursing = !!(node->flags & NODE_DISABLED);
 
   /* Object-like macros that are already expanding are necessarily
      recursive.
@@ -1032,11 +1080,17 @@ _cpp_create_trad_definition (pfile, macro)
 {
   const uchar *cur;
   uchar *limit;
+  cpp_context *context = pfile->context;
 
-  CUR (pfile->context) = pfile->buffer->cur;
+  /* The context has not been set up for command line defines, and CUR
+     has not been updated for the macro name for in-file defines.  */
+  pfile->out.cur = pfile->out.base;
+  CUR (context) = pfile->buffer->cur;
+  RLIMIT (context) = pfile->buffer->rlimit;
+  check_output_buffer (pfile, RLIMIT (context) - CUR (context));
 
   /* Is this a function-like macro?  */
-  if (* CUR (pfile->context) == '(')
+  if (* CUR (context) == '(')
     {
       /* Setting macro to NULL indicates an error occurred, and
         prevents unnecessary work in scan_out_logical_line.  */
@@ -1052,8 +1106,8 @@ _cpp_create_trad_definition (pfile, macro)
     }
 
   /* Skip leading whitespace in the replacement text.  */
-  CUR (pfile->context)
-    = skip_whitespace (pfile, CUR (pfile->context),
+  pfile->buffer->cur
+    = skip_whitespace (pfile, CUR (context),
                       CPP_OPTION (pfile, discard_comments_in_macro_exp));
 
   pfile->state.prevent_expansion++;
@@ -1162,20 +1216,3 @@ _cpp_expansions_different_trad (macro1, macro2)
   free (p1);
   return mismatch;
 }
-
-/* Prepare to be able to scan the current buffer.  */
-void
-_cpp_set_trad_context (pfile)
-     cpp_reader *pfile;
-{
-  cpp_buffer *buffer = pfile->buffer;
-  cpp_context *context = pfile->context;
-
-  if (pfile->context->prev)
-    abort ();
-
-  pfile->out.cur = pfile->out.base;
-  CUR (context) = buffer->cur;
-  RLIMIT (context) = buffer->rlimit;
-  check_output_buffer (pfile, RLIMIT (context) - CUR (context));
-}