diff mbox series

[RFC/gcov,06/12] gcov-tool: Support file input from stdin

Message ID 20220331113515.35764-7-sebastian.huber@embedded-brains.de
State New
Headers show
Series Add merge-stream subcommand to gcov-tool | expand

Commit Message

Sebastian Huber March 31, 2022, 11:35 a.m. UTC
gcc/

	* gcov-io.cc (GCOV_MODE_STDIN): Define.
	(gcov_position): For gcov-tool, return calculated position if file is
	stdin.
	(gcov_open):  For gcov-tool, use stdin if filename is NULL.
	(gcov_close): For gcov-tool, do not close stdin.
	(gcov_read_bytes): For gcov-tool, update position if file is stdin.
	(gcov_sync): For gcov-tool, discard input if file is stdin.
---
 gcc/gcov-io.cc | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

Comments

Martin Liška April 7, 2022, 8:32 a.m. UTC | #1
On 3/31/22 13:35, Sebastian Huber wrote:
> gcc/
> 
> 	* gcov-io.cc (GCOV_MODE_STDIN): Define.
> 	(gcov_position): For gcov-tool, return calculated position if file is
> 	stdin.
> 	(gcov_open):  For gcov-tool, use stdin if filename is NULL.
> 	(gcov_close): For gcov-tool, do not close stdin.
> 	(gcov_read_bytes): For gcov-tool, update position if file is stdin.
> 	(gcov_sync): For gcov-tool, discard input if file is stdin.
> ---
>   gcc/gcov-io.cc | 38 ++++++++++++++++++++++++++++++++++++++
>   1 file changed, 38 insertions(+)
> 
> diff --git a/gcc/gcov-io.cc b/gcc/gcov-io.cc
> index fee3130f94a..177f81166a6 100644
> --- a/gcc/gcov-io.cc
> +++ b/gcc/gcov-io.cc
> @@ -35,8 +35,13 @@ struct gcov_var
>     int error;			/* < 0 overflow, > 0 disk error.  */
>     int mode;			/* < 0 writing, > 0 reading.  */
>     int endian;			/* Swap endianness.  */
> +#ifdef IN_GCOV_TOOL
> +  gcov_position_t pos;		/* File position for stdin support. */

One more space after dot.

> +#endif
>   } gcov_var;
>   
> +#define GCOV_MODE_STDIN 2
> +
>   /* Save the current position in the gcov file.  */
>   /* We need to expose this function when compiling for gcov-tool.  */
>   #ifndef IN_GCOV_TOOL
> @@ -45,6 +50,10 @@ static inline
>   gcov_position_t
>   gcov_position (void)
>   {
> +#ifdef IN_GCOV_TOOL
> +  if (gcov_var.mode == GCOV_MODE_STDIN)
> +    return gcov_var.pos;
> +#endif
>     return ftell (gcov_var.file);
>   }
>   
> @@ -108,6 +117,16 @@ gcov_open (const char *name, int mode)
>   #if !IN_LIBGCOV || defined (IN_GCOV_TOOL)
>     gcov_var.endian = 0;
>   #endif
> +#ifdef IN_GCOV_TOOL
> +  gcov_var.pos = 0;
> +  if (!name)
> +    {
> +      gcov_nonruntime_assert (gcov_var.mode > 0);
> +      gcov_var.file = stdin;
> +      gcov_var.mode = GCOV_MODE_STDIN;
> +      return 1;
> +    }
> +#endif
>   #if GCOV_LOCKED
>     if (mode > 0)
>       {
> @@ -190,6 +209,11 @@ gcov_open (const char *name, int mode)
>   GCOV_LINKAGE int
>   gcov_close (void)
>   {
> +#ifdef IN_GCOV_TOOL
> +  if (gcov_var.file == stdin)
> +    gcov_var.file = 0;
> +  else
> +#endif
>     if (gcov_var.file)
>       {
>         if (fclose (gcov_var.file))
> @@ -363,6 +387,9 @@ gcov_read_bytes (void *buffer, unsigned count)
>     if (read != 1)
>       return NULL;
>   
> +#ifdef IN_GCOV_TOOL
> +  gcov_var.pos += count;
> +#endif
>     return buffer;
>   }
>   
> @@ -499,6 +526,17 @@ gcov_sync (gcov_position_t base, gcov_unsigned_t length)
>   {
>     gcov_nonruntime_assert (gcov_var.mode > 0);
>     base += length;
> +#ifdef IN_GCOV_TOOL
> +  if (gcov_var.mode == GCOV_MODE_STDIN)
> +    {
> +      while (gcov_var.pos < base)
> +	{
> +	  ++gcov_var.pos;
> +	  (void)fgetc(gcov_var.file);

A space after fgetc, please.

Martin

> +	}
> +      return;
> +    }
> +#endif
>     fseek (gcov_var.file, base, SEEK_SET);
>   }
>   #endif
diff mbox series

Patch

diff --git a/gcc/gcov-io.cc b/gcc/gcov-io.cc
index fee3130f94a..177f81166a6 100644
--- a/gcc/gcov-io.cc
+++ b/gcc/gcov-io.cc
@@ -35,8 +35,13 @@  struct gcov_var
   int error;			/* < 0 overflow, > 0 disk error.  */
   int mode;			/* < 0 writing, > 0 reading.  */
   int endian;			/* Swap endianness.  */
+#ifdef IN_GCOV_TOOL
+  gcov_position_t pos;		/* File position for stdin support. */
+#endif
 } gcov_var;
 
+#define GCOV_MODE_STDIN 2
+
 /* Save the current position in the gcov file.  */
 /* We need to expose this function when compiling for gcov-tool.  */
 #ifndef IN_GCOV_TOOL
@@ -45,6 +50,10 @@  static inline
 gcov_position_t
 gcov_position (void)
 {
+#ifdef IN_GCOV_TOOL
+  if (gcov_var.mode == GCOV_MODE_STDIN)
+    return gcov_var.pos;
+#endif
   return ftell (gcov_var.file);
 }
 
@@ -108,6 +117,16 @@  gcov_open (const char *name, int mode)
 #if !IN_LIBGCOV || defined (IN_GCOV_TOOL)
   gcov_var.endian = 0;
 #endif
+#ifdef IN_GCOV_TOOL
+  gcov_var.pos = 0;
+  if (!name)
+    {
+      gcov_nonruntime_assert (gcov_var.mode > 0);
+      gcov_var.file = stdin;
+      gcov_var.mode = GCOV_MODE_STDIN;
+      return 1;
+    }
+#endif
 #if GCOV_LOCKED
   if (mode > 0)
     {
@@ -190,6 +209,11 @@  gcov_open (const char *name, int mode)
 GCOV_LINKAGE int
 gcov_close (void)
 {
+#ifdef IN_GCOV_TOOL
+  if (gcov_var.file == stdin)
+    gcov_var.file = 0;
+  else
+#endif
   if (gcov_var.file)
     {
       if (fclose (gcov_var.file))
@@ -363,6 +387,9 @@  gcov_read_bytes (void *buffer, unsigned count)
   if (read != 1)
     return NULL;
 
+#ifdef IN_GCOV_TOOL
+  gcov_var.pos += count;
+#endif
   return buffer;
 }
 
@@ -499,6 +526,17 @@  gcov_sync (gcov_position_t base, gcov_unsigned_t length)
 {
   gcov_nonruntime_assert (gcov_var.mode > 0);
   base += length;
+#ifdef IN_GCOV_TOOL
+  if (gcov_var.mode == GCOV_MODE_STDIN)
+    {
+      while (gcov_var.pos < base)
+	{
+	  ++gcov_var.pos;
+	  (void)fgetc(gcov_var.file);
+	}
+      return;
+    }
+#endif
   fseek (gcov_var.file, base, SEEK_SET);
 }
 #endif