Hi,

this started, because I saw get_next_line returns -1 on error, instead
of false: PR 77699.

But when I was there I also saw that read_line_num is using memmove on
non-aliased src & dest, instead of memcpy.  But then I see that
also adding a NUL byte is superfluos, and all the copying as well....


So the result is a cleanup of input.c that avoids lots of copying
altogether, because it is no longer necessary to do so.


Bootstrapped and reg-tested on x86_64-pc-linux-gnu.
Is it OK for trunk?


Thanks
Bernd.
2016-09-26  Bernd Edlinger  <bernd.edlin...@hotmail.de>

	PR preprocessor/77699
	* input.c (maybe_grow): Don't allocate one byte extra headroom.
	(get_next_line): Return false on error.
	(read_next_line): Removed, use get_next_line instead.
	(read_line_num): Don't copy the line.
	(location_get_source_line): Don't use static data.


Index: gcc/input.c
===================================================================
--- gcc/input.c	(revision 240471)
+++ gcc/input.c	(working copy)
@@ -432,7 +432,7 @@ maybe_grow (fcache *c)
     return;
 
   size_t size = c->size == 0 ? fcache_buffer_size : c->size * 2;
-  c->data = XRESIZEVEC (char, c->data, size + 1);
+  c->data = XRESIZEVEC (char, c->data, size);
   c->size = size;
 }
 
@@ -534,7 +534,7 @@ get_next_line (fcache *c, char **line, ssize_t *li
     }
 
   if (ferror (c->fp))
-    return -1;
+    return false;
 
   /* At this point, we've found the end of the of line.  It either
      points to the '\n' or to one byte after the last byte of the
@@ -597,36 +597,6 @@ get_next_line (fcache *c, char **line, ssize_t *li
   return true;
 }
 
-/* Reads the next line from FILE into *LINE.  If *LINE is too small
-   (or NULL) it is allocated (or extended) to have enough space to
-   containe the line.  *LINE_LENGTH must contain the size of the
-   initial*LINE buffer.  It's then updated by this function to the
-   actual length of the returned line.  Note that the returned line
-   can contain several zero bytes.  Also note that the returned string
-   is allocated in static storage that is going to be re-used by
-   subsequent invocations of read_line.  */
-
-static bool
-read_next_line (fcache *cache, char ** line, ssize_t *line_len)
-{
-  char *l = NULL;
-  ssize_t len = 0;
-
-  if (!get_next_line (cache, &l, &len))
-    return false;
-
-  if (*line == NULL)
-    *line = XNEWVEC (char, len);
-  else
-    if (*line_len < len)
-	*line = XRESIZEVEC (char, *line, len);
-
-  memcpy (*line, l, len);
-  *line_len = len;
-
-  return true;
-}
-
 /* Consume the next bytes coming from the cache (or from its
    underlying file if there are remaining unread bytes in the file)
    until we reach the next end-of-line (or end-of-file).  There is no
@@ -651,7 +621,7 @@ goto_next_line (fcache *cache)
 
 static bool
 read_line_num (fcache *c, size_t line_num,
-	       char ** line, ssize_t *line_len)
+	       char **line, ssize_t *line_len)
 {
   gcc_assert (line_num > 0);
 
@@ -705,12 +675,8 @@ read_line_num (fcache *c, size_t line_num,
 	    {
 	      /* We have the start/end of the line.  Let's just copy
 		 it again and we are done.  */
-	      ssize_t len = i->end_pos - i->start_pos + 1;
-	      if (*line_len < len)
-		*line = XRESIZEVEC (char, *line, len);
-	      memmove (*line, c->data + i->start_pos, len);
-	      (*line)[len - 1] = '\0';
-	      *line_len = --len;
+	      *line = c->data + i->start_pos;
+	      *line_len = i->end_pos - i->start_pos;
 	      return true;
 	    }
 
@@ -735,7 +701,7 @@ read_line_num (fcache *c, size_t line_num,
 
   /* The line we want is the next one.  Let's read and copy it back to
      the caller.  */
-  return read_next_line (c, line, line_len);
+  return get_next_line (c, line, line_len);
 }
 
 /* Return the physical source line that corresponds to FILE_PATH/LINE in a
@@ -748,8 +714,8 @@ const char *
 location_get_source_line (const char *file_path, int line,
 			  int *line_len)
 {
-  static char *buffer;
-  static ssize_t len;
+  char *buffer;
+  ssize_t len;
 
   if (line == 0)
     return NULL;

Reply via email to