OSDN Git Service

* cpphash.h, cpplib.c: Correct erroneous previous checkin.
[pf3gnuchains/gcc-fork.git] / gcc / cpplib.c
index 76e5355..719daaa 100644 (file)
@@ -178,6 +178,8 @@ DIRECTIVE_TABLE
 #undef D
 #undef DIRECTIVE_TABLE
 
+#define SEEN_EOL() (pfile->lexer_pos.output_line > pfile->line)
+
 /* Skip any remaining tokens in a directive.  */
 static void
 skip_rest_of_line (pfile)
@@ -194,7 +196,7 @@ skip_rest_of_line (pfile)
     _cpp_pop_context (pfile);
 
   /* Sweep up all tokens remaining on the line.  */
-  while (!pfile->state.next_bol)
+  while (! SEEN_EOL ())
     _cpp_lex_token (pfile, &token);
 }
 
@@ -203,7 +205,7 @@ static void
 check_eol (pfile)
      cpp_reader *pfile;
 {
-  if (!pfile->state.next_bol)
+  if (! SEEN_EOL ())
     {
       cpp_token token;
 
@@ -225,6 +227,7 @@ start_directive (pfile)
 
   /* Some handlers need the position of the # for diagnostics.  */
   pfile->directive_pos = pfile->lexer_pos;
+  pfile->directive_line = pfile->line;
 
   /* Don't save directive tokens for external clients.  */
   pfile->la_saved = pfile->la_write;
@@ -239,7 +242,11 @@ end_directive (pfile, skip_line)
 {
   /* We don't skip for an assembler #.  */
   if (skip_line)
-    skip_rest_of_line (pfile);
+    {
+      skip_rest_of_line (pfile);
+      /*  "Accept" the newline now.  */
+      pfile->line++;
+    }
 
   /* Restore state.  */
   pfile->la_write = pfile->la_saved;
@@ -394,19 +401,9 @@ run_directive (pfile, dir_no, type, buf, count)
      const char *buf;
      size_t count;
 {
-  unsigned int output_line = pfile->lexer_pos.output_line;
   cpp_buffer *buffer;
 
   buffer = cpp_push_buffer (pfile, (const U_CHAR *) buf, count, type, 0, 1);
-
-  if (dir_no == T_PRAGMA)
-    {
-      /* A kludge to avoid line markers for _Pragma.  */
-      pfile->lexer_pos.output_line = output_line;
-      /* Avoid interpretation of directives in a _Pragma string.  */
-      pfile->state.next_bol = 0;
-    }
-
   start_directive (pfile);
   pfile->state.prevent_expansion++;
   pfile->directive = &dtable[dir_no];
@@ -476,7 +473,7 @@ do_define (pfile)
     {
       if (_cpp_create_definition (pfile, node))
        if (pfile->cb.define)
-         (*pfile->cb.define) (pfile, node);
+         (*pfile->cb.define) (pfile, pfile->directive_line, node);
     }
 }
 
@@ -492,7 +489,7 @@ do_undef (pfile)
   if (node && node->type == NT_MACRO)
     {
       if (pfile->cb.undef)
-       (*pfile->cb.undef) (pfile, node);
+       (*pfile->cb.undef) (pfile, pfile->directive_line, node);
 
       if (node->flags & NODE_WARN)
        cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node));
@@ -625,7 +622,8 @@ do_include_common (pfile, type)
          /* Get out of macro context, if we are.  */
          end_directive (pfile, 1);
          if (pfile->cb.include)
-           (*pfile->cb.include) (pfile, pfile->directive->name, &header);
+           (*pfile->cb.include) (pfile, pfile->directive_line,
+                                 pfile->directive->name, &header);
 
          _cpp_execute_include (pfile, &header, type);
        }
@@ -715,7 +713,7 @@ do_line (pfile)
   cpp_buffer *buffer = pfile->buffer;
   enum lc_reason reason = LC_RENAME;
   unsigned long new_lineno;
-  unsigned int cap;
+  unsigned int cap, sysp = pfile->map->sysp;
   cpp_token token;
 
   /* C99 raised the minimum limit on #line numbers.  */
@@ -738,19 +736,20 @@ do_line (pfile)
   cpp_get_token (pfile, &token);
   if (token.type == CPP_STRING)
     {
-      const char *fname = (const char *) token.val.str.text;
+      buffer->nominal_fname = (const char *) token.val.str.text;
 
       /* Only accept flags for the # 55 form.  */
-      if (! pfile->state.line_extension)
-       check_eol (pfile);
-      else
+      if (pfile->state.line_extension)
        {
-         int flag = 0, sysp = 0;
+         int flag;
 
-         flag = read_flag (pfile, flag);
+         sysp = 0;
+         flag = read_flag (pfile, 0);
          if (flag == 1)
            {
              reason = LC_ENTER;
+             /* Fake an include for cpp_included ().  */
+             _cpp_fake_include (pfile, buffer->nominal_fname);
              flag = read_flag (pfile, flag);
            }
          else if (flag == 2)
@@ -763,42 +762,10 @@ do_line (pfile)
              sysp = 1;
              flag = read_flag (pfile, flag);
              if (flag == 4)
-               sysp = 2, read_flag (pfile, flag);
+               sysp = 2;
            }
-
-         if (reason == LC_ENTER)
-           {
-             /* Fake a buffer stack for diagnostics.  */
-             cpp_push_buffer (pfile, 0, 0, BUF_FAKE, fname, 0);
-             /* Fake an include for cpp_included.  */
-             _cpp_fake_include (pfile, fname);
-             buffer = pfile->buffer;
-           }
-         else if (reason == LC_LEAVE)
-           {
-             if (buffer->type != BUF_FAKE)
-               cpp_warning (pfile, "file \"%s\" left but not entered",
-                            buffer->nominal_fname);
-             else
-               {
-                 _cpp_pop_buffer (pfile);
-                 buffer = pfile->buffer;
-#ifdef ENABLE_CHECKING
-                 if (strcmp (buffer->nominal_fname, fname))
-                   cpp_warning (pfile, "expected to return to file \"%s\"",
-                                buffer->nominal_fname);
-                 if (buffer->lineno + 1 != new_lineno)
-                   cpp_warning (pfile, "expected to return to line number %u",
-                                buffer->lineno + 1);
-                 if (buffer->sysp != sysp)
-                   cpp_warning (pfile, "header flags for \"%s\" have changed",
-                                buffer->nominal_fname);
-#endif
-               }
-           }
-         buffer->sysp = sysp;
        }
-      buffer->nominal_fname = fname;
+      check_eol (pfile);
     }
   else if (token.type != CPP_EOF)
     {
@@ -808,36 +775,27 @@ do_line (pfile)
     }
 
   end_directive (pfile, 1);
-  buffer->lineno = new_lineno - 1;
-  _cpp_do_file_change (pfile, reason);
+  _cpp_do_file_change (pfile, reason, (const char *) buffer->nominal_fname,
+                      new_lineno, sysp);
 }
 
-/* Arrange the file_change callback.  It is assumed that the next line
-   is given by incrementing buffer->lineno and pfile->line.  */
+/* Arrange the file_change callback.  pfile->line has changed to
+   FILE_LINE of TO_FILE, for reason REASON.  SYSP is 1 for a system
+   header, 2 for a sytem header that needs to be extern "C" protected,
+   and zero otherwise.  */
 void
-_cpp_do_file_change (pfile, reason)
+_cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
      cpp_reader *pfile;
      enum lc_reason reason;
+     const char *to_file;
+     unsigned int file_line;
+     unsigned int sysp;
 {
-  cpp_buffer *buffer;
-  struct line_map *map;
-
-  buffer = pfile->buffer;
-  map = add_line_map (&pfile->line_maps, reason,
-                     pfile->line + 1, buffer->nominal_fname, buffer->lineno + 1);
+  pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
+                            pfile->line, to_file, file_line);
 
   if (pfile->cb.file_change)
-    {
-      cpp_file_change fc;
-      
-      fc.map = map;
-      fc.line = pfile->line + 1;
-      fc.reason = reason;
-      fc.sysp = buffer->sysp;
-      fc.externc = CPP_OPTION (pfile, cplusplus) && buffer->sysp == 2;
-
-      (*pfile->cb.file_change) (pfile, &fc);
-    }
+    (*pfile->cb.file_change) (pfile, pfile->map);
 }
 
 /*
@@ -888,7 +846,7 @@ do_ident (pfile)
   if (str.type != CPP_STRING)
     cpp_error (pfile, "invalid #ident");
   else if (pfile->cb.ident)
-    (*pfile->cb.ident) (pfile, &str.val.str);
+    (*pfile->cb.ident) (pfile, pfile->directive_line, &str.val.str);
 
   check_eol (pfile);
 }
@@ -1042,7 +1000,7 @@ do_pragma (pfile)
   if (handler)
     (*handler) (pfile);
   else if (pfile->cb.def_pragma)
-    (*pfile->cb.def_pragma) (pfile);
+    (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
 }
 
 static void
@@ -1193,16 +1151,19 @@ _cpp_do__Pragma (pfile)
   cpp_token string;
   unsigned char *buffer;
   unsigned int len;
+  cpp_lexer_pos orig_pos;
 
+  orig_pos = pfile->lexer_pos;
   if (get__Pragma_string (pfile, &string))
+    cpp_error (pfile, "_Pragma takes a parenthesized string literal");
+  else
     {
-      cpp_error (pfile, "_Pragma takes a parenthesized string literal");
-      return;
+      buffer = destringize (&string.val.str, &len);
+      run_directive (pfile, T_PRAGMA, BUF_PRAGMA, (char *) buffer, len);
+      free ((PTR) buffer);
+      pfile->lexer_pos = orig_pos;
+      pfile->line = pfile->lexer_pos.line;
     }
-
-  buffer = destringize (&string.val.str, &len);
-  run_directive (pfile, T_PRAGMA, BUF_PRAGMA, (char *) buffer, len);
-  free ((PTR) buffer);
 }
 
 /* Just ignore #sccs, on systems where we define it at all.  */
@@ -1754,7 +1715,7 @@ cpp_get_callbacks (pfile)
 }
 
 /* The line map set.  */
-struct line_maps *
+const struct line_maps *
 cpp_get_line_maps (pfile)
      cpp_reader *pfile;
 {
@@ -1784,38 +1745,26 @@ cpp_push_buffer (pfile, buffer, len, type, filename, return_at_eof)
 {
   cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
 
-  if (type == BUF_FAKE)
-    {
-      /* A copy of the current buffer, just with a new name and type.  */
-      memcpy (new, pfile->buffer, sizeof (cpp_buffer));
-      new->type = BUF_FAKE;
-    }
-  else
-    {
-      if (type == BUF_BUILTIN)
-       filename = _("<builtin>");
-      else if (type == BUF_CL_OPTION)
-       filename = _("<command line>");
-      else if (type == BUF_PRAGMA)
-       filename = "<_Pragma>";
+  if (type == BUF_BUILTIN)
+    filename = _("<builtin>");
+  else if (type == BUF_CL_OPTION)
+    filename = _("<command line>");
+  else if (type == BUF_PRAGMA)
+    filename = "<_Pragma>";
 
-      /* Clears, amongst other things, if_stack and mi_cmacro.  */
-      memset (new, 0, sizeof (cpp_buffer));
+  /* Clears, amongst other things, if_stack and mi_cmacro.  */
+  memset (new, 0, sizeof (cpp_buffer));
 
-      new->line_base = new->buf = new->cur = buffer;
-      new->rlimit = buffer + len;
-      new->sysp = 0;
+  new->line_base = new->buf = new->cur = buffer;
+  new->rlimit = buffer + len;
 
-      /* No read ahead or extra char initially.  */
-      new->read_ahead = EOF;
-      new->extra_char = EOF;
+  /* No read ahead or extra char initially.  */
+  new->read_ahead = EOF;
+  new->extra_char = EOF;
 
-      /* Preprocessed files, builtins, _Pragma and command line
-        options don't do trigraph and escaped newline processing.  */
-      new->from_stage3 = type != BUF_FILE || CPP_OPTION (pfile, preprocessed);
-
-      pfile->lexer_pos.output_line = 1;
-    }
+  /* Preprocessed files, builtins, _Pragma and command line
+     options don't do trigraph and escaped newline processing.  */
+  new->from_stage3 = type != BUF_FILE || CPP_OPTION (pfile, preprocessed);
 
   if (*filename == '\0')
     new->nominal_fname = _("<stdin>");
@@ -1824,11 +1773,8 @@ cpp_push_buffer (pfile, buffer, len, type, filename, return_at_eof)
   new->type = type;
   new->prev = pfile->buffer;
   new->pfile = pfile;
-  new->include_stack_listed = 0;
-  new->lineno = 1;
   new->return_at_eof = return_at_eof;
 
-  pfile->state.next_bol = 1;
   pfile->buffer_stack_depth++;
   pfile->buffer = new;
 
@@ -1842,51 +1788,36 @@ void
 _cpp_pop_buffer (pfile)
      cpp_reader *pfile;
 {
-  cpp_buffer *buffer;
+  cpp_buffer *buffer = pfile->buffer;
   struct if_stack *ifs;
 
-  for (;;)
-    {
-      buffer = pfile->buffer;
-      /* Walk back up the conditional stack till we reach its level at
-        entry to this file, issuing error messages.  */
-      for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
-       cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
-                            "unterminated #%s", dtable[ifs->type].name);
-
-      if (buffer->type == BUF_FAKE)
-       buffer->prev->cur = buffer->cur;
-      else if (buffer->type == BUF_FILE)
-       _cpp_pop_file_buffer (pfile, buffer);
-
-      pfile->buffer = buffer->prev;
-      pfile->buffer_stack_depth--;
-
-      /* Callbacks only generated for faked or real files.  */
-      if (buffer->type != BUF_FILE && buffer->type != BUF_FAKE)
-       break;
-         
-      /* No callback for EOF of last file.  */
-      if (!pfile->buffer)
-       break;
+  /* Walk back up the conditional stack till we reach its level at
+     entry to this file, issuing error messages.  */
+  for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
+    cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
+                        "unterminated #%s", dtable[ifs->type].name);
 
-      /* do_line does its own call backs.  */
-      pfile->buffer->include_stack_listed = 0;
-      if (pfile->directive == &dtable[T_LINE])
-       break;
+  /* Update the reader's buffer before _cpp_do_file_change.  */
+  pfile->buffer = buffer->prev;
+  pfile->buffer_stack_depth--;
 
-      pfile->line--;           /* We have a '\n' at the end of #include.  */
-      _cpp_do_file_change (pfile, LC_LEAVE);
-      if (pfile->buffer->type == BUF_FILE)
-       break;
+  if (buffer->type == BUF_FILE)
+    {
+      /* Callbacks are not generated for popping the main file.  */
+      if (buffer->prev)
+       _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
 
-      cpp_warning (pfile, "file \"%s\" entered but not left",
-                  buffer->nominal_fname);
+      _cpp_pop_file_buffer (pfile, buffer);
     }
 
   obstack_free (&pfile->buffer_ob, buffer);
 
-  pfile->state.skipping = 0;   /* In case missing #endif.  */
+  /* The output line can fall out of sync if we missed the final
+     newline from the previous buffer, for example because of an
+     unterminated comment.  Similarly, skipping needs to be cleared in
+     case of a missing #endif.  */
+  pfile->lexer_pos.output_line = pfile->line;
+  pfile->state.skipping = 0;
 }
 
 void