public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/omp/gcc-10] d: Use read() to load contents of stdin into memory.
@ 2020-09-01 10:01 Tobias Burnus
  0 siblings, 0 replies; only message in thread
From: Tobias Burnus @ 2020-09-01 10:01 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:0989a6df97887a448612af2dbad568b8997fb304

commit 0989a6df97887a448612af2dbad568b8997fb304
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Tue Aug 25 11:23:24 2020 +0200

    d: Use read() to load contents of stdin into memory.
    
    This would be an improvement over reading one character at a time.
    
    An ICE was discovered when mixing reading from stdin with `-v', this has been
    fixed in upstream DMD and backported as well.
    
    Reviewed-on: https://github.com/dlang/dmd/pull/11620
    
    gcc/d/ChangeLog:
    
            * d-lang.cc (d_parse_file): Use read() to load contents from stdin,
            allow the front-end to free the memory after parsing.
            * dmd/func.c (FuncDeclaration::semantic): Use module filename if
            searchPath returns NULL.
    
    (cherry picked from commit 7421802276e737c2da297599121480833db92de9)

Diff:
---
 gcc/d/d-lang.cc  | 43 +++++++++++++++++++++++++++----------------
 gcc/d/dmd/func.c |  5 +++--
 2 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/gcc/d/d-lang.cc b/gcc/d/d-lang.cc
index 6848c5e9a1c..4f1bf4a6e6a 100644
--- a/gcc/d/d-lang.cc
+++ b/gcc/d/d-lang.cc
@@ -1026,32 +1026,43 @@ d_parse_file (void)
     {
       if (strcmp (in_fnames[i], "-") == 0)
 	{
-	  /* Handling stdin, generate a unique name for the module.  */
-	  obstack buffer;
-	  gcc_obstack_init (&buffer);
-	  int c;
+	  /* Load the entire contents of stdin into memory.  8 kilobytes should
+	     be a good enough initial size, but double on each iteration.
+	     16 bytes are added for the final '\n' and 15 bytes of padding.  */
+	  ssize_t size = 8 * 1024;
+	  uchar *buffer = XNEWVEC (uchar, size + 16);
+	  ssize_t len = 0;
+	  ssize_t count;
+
+	  while ((count = read (STDIN_FILENO, buffer + len, size - len)) > 0)
+	    {
+	      len += count;
+	      if (len == size)
+		{
+		  size *= 2;
+		  buffer = XRESIZEVEC (uchar, buffer, size + 16);
+		}
+	    }
+
+	  if (count < 0)
+	    {
+	      error (Loc ("stdin", 0, 0), "%s", xstrerror (errno));
+	      free (buffer);
+	      continue;
+	    }
 
+	  /* Handling stdin, generate a unique name for the module.  */
 	  Module *m = Module::create (in_fnames[i],
 				      Identifier::generateId ("__stdin"),
 				      global.params.doDocComments,
 				      global.params.doHdrGeneration);
 	  modules.push (m);
 
-	  /* Load the entire contents of stdin into memory.  */
-	  while ((c = getc (stdin)) != EOF)
-	    obstack_1grow (&buffer, c);
-
-	  if (!obstack_object_size (&buffer))
-	    obstack_1grow (&buffer, '\0');
-
 	  /* Overwrite the source file for the module, the one created by
 	     Module::create would have a forced a `.d' suffix.  */
 	  m->srcfile = File::create ("<stdin>");
-	  m->srcfile->len = obstack_object_size (&buffer);
-	  m->srcfile->buffer = (unsigned char *) obstack_finish (&buffer);
-
-	  /* Tell the front-end not to free the buffer after parsing.  */
-	  m->srcfile->ref = 1;
+	  m->srcfile->len = len;
+	  m->srcfile->buffer = buffer;
 	}
       else
 	{
diff --git a/gcc/d/dmd/func.c b/gcc/d/dmd/func.c
index 621405eef3e..8389c00608c 100644
--- a/gcc/d/dmd/func.c
+++ b/gcc/d/dmd/func.c
@@ -1212,8 +1212,9 @@ Ldone:
         if (type && mod)
         {
             printedMain = true;
-            const char *name = FileName::searchPath(global.path, mod->srcfile->toChars(), true);
-            message("entry     %-10s\t%s", type, name);
+            const char *name = mod->srcfile->toChars();
+            const char *path = FileName::searchPath(global.path, name, true);
+            message("entry     %-10s\t%s", type, path ? path : name);
         }
     }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-09-01 10:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-01 10:01 [gcc/devel/omp/gcc-10] d: Use read() to load contents of stdin into memory Tobias Burnus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).