public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/marxin/heads/PR89961-fix-gcov-json-output-location-v3)] gcov: fix output location for JSON mode.
@ 2021-08-19  8:50 Martin Liska
  0 siblings, 0 replies; only message in thread
From: Martin Liska @ 2021-08-19  8:50 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:73c67d8797f9ded1bf2b64825079e3c277767a4b

commit 73c67d8797f9ded1bf2b64825079e3c277767a4b
Author: Martin Liska <mliska@suse.cz>
Date:   Tue Aug 17 16:24:26 2021 +0200

    gcov: fix output location for JSON mode.
    
            PR gcov-profile/89961
    
    gcc/ChangeLog:
    
            * gcov.c (make_gcov_file_name): Rewrite using std::string.
            (mangle_name): Simplify, do not used the second argument.
            (strip_extention): New function.
            (get_md5sum): Likewise.
            (get_gcov_intermediate_filename): Handle properly -p and -x
            options.
            (output_gcov_file): Use string type.
            (generate_results): Likewise.
            (md5sum_to_hex): Remove.

Diff:
---
 gcc/gcov.c | 158 ++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 79 insertions(+), 79 deletions(-)

diff --git a/gcc/gcov.c b/gcc/gcov.c
index 5c651a9bdce..cf0a49d8c30 100644
--- a/gcc/gcov.c
+++ b/gcc/gcov.c
@@ -662,8 +662,8 @@ static void accumulate_line_counts (source_info *);
 static void output_gcov_file (const char *, source_info *);
 static int output_branch_count (FILE *, int, const arc_info *);
 static void output_lines (FILE *, const source_info *);
-static char *make_gcov_file_name (const char *, const char *);
-static char *mangle_name (const char *, char *);
+static string make_gcov_file_name (const char *, const char *);
+static char *mangle_name (const char *);
 static void release_structures (void);
 extern int main (int, char **);
 
@@ -1134,6 +1134,41 @@ output_intermediate_json_line (json::array *object,
   object->append (lineo);
 }
 
+/* Strip filename extension in STR.  */
+
+static string
+strip_extention (string str)
+{
+  string::size_type pos = str.rfind ('.');
+  if (pos != string::npos)
+    str = str.substr (0, pos);
+
+  return str;
+}
+
+/* Calcualte md5sum for INPUT string and return it in hex string format.  */
+
+static string
+get_md5sum (const char *input)
+{
+  md5_ctx ctx;
+  char md5sum[16];
+  string str;
+
+  md5_init_ctx (&ctx);
+  md5_process_bytes (input, strlen (input), &ctx);
+  md5_finish_ctx (&ctx, md5sum);
+
+  for (unsigned i = 0; i < 16; i++)
+    {
+      char b[3];
+      sprintf (b, "%02x", (unsigned char)md5sum[i]);
+      str += b;
+    }
+
+  return str;
+}
+
 /* Get the name of the gcov file.  The return value must be free'd.
 
    It appends the '.gcov' extension to the *basename* of the file.
@@ -1143,20 +1178,26 @@ output_intermediate_json_line (json::array *object,
    input: foo.da,       output: foo.da.gcov
    input: a/b/foo.cc,   output: foo.cc.gcov  */
 
-static char *
-get_gcov_intermediate_filename (const char *file_name)
+static string
+get_gcov_intermediate_filename (const char *input_file_name)
 {
-  const char *gcov = ".gcov.json.gz";
-  char *result;
-  const char *cptr;
+  string base = basename (input_file_name);
+  string str = strip_extention (base);
 
-  /* Find the 'basename'.  */
-  cptr = lbasename (file_name);
-
-  result = XNEWVEC (char, strlen (cptr) + strlen (gcov) + 1);
-  sprintf (result, "%s%s", cptr, gcov);
+  if (flag_hash_filenames)
+    {
+      str += "##";
+      str += get_md5sum (input_file_name);
+    }
+  else if (flag_preserve_paths && base != input_file_name)
+    {
+      str += "##";
+      str += mangle_path (input_file_name);
+      str = strip_extention (str);
+    }
 
-  return result;
+  str += ".gcov.json.gz";
+  return str.c_str ();
 }
 
 /* Output the result in JSON intermediate format.
@@ -1416,7 +1457,9 @@ process_all_functions (void)
 static void
 output_gcov_file (const char *file_name, source_info *src)
 {
-  char *gcov_file_name = make_gcov_file_name (file_name, src->coverage.name);
+  string gcov_file_name_str
+    = make_gcov_file_name (file_name, src->coverage.name);
+  const char *gcov_file_name = gcov_file_name_str.c_str ();
 
   if (src->coverage.lines)
     {
@@ -1438,13 +1481,12 @@ output_gcov_file (const char *file_name, source_info *src)
       unlink (gcov_file_name);
       fnotice (stdout, "Removing '%s'\n", gcov_file_name);
     }
-  free (gcov_file_name);
 }
 
 static void
 generate_results (const char *file_name)
 {
-  char *gcov_intermediate_filename;
+  string gcov_intermediate_filename;
 
   for (vector<function_info *>::iterator it = functions.begin ();
        it != functions.end (); it++)
@@ -1547,11 +1589,13 @@ generate_results (const char *file_name)
 	  root->print (&pp);
 	  pp_formatted_text (&pp);
 
-	  gzFile output = gzopen (gcov_intermediate_filename, "w");
+	  fnotice (stdout, "Creating '%s'\n",
+		   gcov_intermediate_filename.c_str ());
+	  gzFile output = gzopen (gcov_intermediate_filename.c_str (), "w");
 	  if (output == NULL)
 	    {
 	      fnotice (stderr, "Cannot open JSON output file %s\n",
-		       gcov_intermediate_filename);
+		       gcov_intermediate_filename.c_str ());
 	      return;
 	    }
 
@@ -1559,7 +1603,7 @@ generate_results (const char *file_name)
 	      || gzclose (output))
 	    {
 	      fnotice (stderr, "Error writing JSON output file %s\n",
-		       gcov_intermediate_filename);
+		       gcov_intermediate_filename.c_str ());
 	      return;
 	    }
 	}
@@ -2546,15 +2590,6 @@ canonicalize_name (const char *name)
   return result;
 }
 
-/* Print hex representation of 16 bytes from SUM and write it to BUFFER.  */
-
-static void
-md5sum_to_hex (const char *sum, char *buffer)
-{
-  for (unsigned i = 0; i < 16; i++)
-    sprintf (buffer + (2 * i), "%02x", (unsigned char)sum[i]);
-}
-
 /* Generate an output file name. INPUT_NAME is the canonicalized main
    input file and SRC_NAME is the canonicalized file name.
    LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation.  With
@@ -2567,77 +2602,42 @@ md5sum_to_hex (const char *sum, char *buffer)
    component.  (Remember, the canonicalized name will already have
    elided '.' components and converted \\ separators.)  */
 
-static char *
+static string
 make_gcov_file_name (const char *input_name, const char *src_name)
 {
-  char *ptr;
-  char *result;
-
-  if (flag_long_names && input_name && strcmp (src_name, input_name))
-    {
-      /* Generate the input filename part.  */
-      result = XNEWVEC (char, strlen (input_name) + strlen (src_name) + 10);
-
-      ptr = result;
-      ptr = mangle_name (input_name, ptr);
-      ptr[0] = ptr[1] = '#';
-      ptr += 2;
-    }
-  else
-    {
-      result = XNEWVEC (char, strlen (src_name) + 10);
-      ptr = result;
-    }
-
-  ptr = mangle_name (src_name, ptr);
-  strcpy (ptr, ".gcov");
+  string str;
 
   /* When hashing filenames, we shorten them by only using the filename
      component and appending a hash of the full (mangled) pathname.  */
   if (flag_hash_filenames)
+    str = (string (mangle_name (src_name)) + "##"
+	   + get_md5sum (src_name) + ".gcov");
+  else
     {
-      md5_ctx ctx;
-      char md5sum[16];
-      char md5sum_hex[33];
-
-      md5_init_ctx (&ctx);
-      md5_process_bytes (src_name, strlen (src_name), &ctx);
-      md5_finish_ctx (&ctx, md5sum);
-      md5sum_to_hex (md5sum, md5sum_hex);
-      free (result);
+      if (flag_long_names && input_name && strcmp (src_name, input_name) != 0)
+	{
+	  str += mangle_name (input_name);
+	  str += "##";
+	}
 
-      result = XNEWVEC (char, strlen (src_name) + 50);
-      ptr = result;
-      ptr = mangle_name (src_name, ptr);
-      ptr[0] = ptr[1] = '#';
-      ptr += 2;
-      memcpy (ptr, md5sum_hex, 32);
-      ptr += 32;
-      strcpy (ptr, ".gcov");
+      str += mangle_name (src_name);
+      str += ".gcov";
     }
 
-  return result;
+  return str;
 }
 
 /* Mangle BASE name, copy it at the beginning of PTR buffer and
    return address of the \0 character of the buffer.  */
 
 static char *
-mangle_name (char const *base, char *ptr)
+mangle_name (char const *base)
 {
-  size_t len;
-
   /* Generate the source filename part.  */
   if (!flag_preserve_paths)
-    base = lbasename (base);
+    return xstrdup (lbasename (base));
   else
-    base = mangle_path (base);
-
-  len = strlen (base);
-  memcpy (ptr, base, len);
-  ptr += len;
-
-  return ptr;
+    return mangle_path (base);
 }
 
 /* Scan through the bb_data for each line in the block, increment


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

only message in thread, other threads:[~2021-08-19  8:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-19  8:50 [gcc(refs/users/marxin/heads/PR89961-fix-gcov-json-output-location-v3)] gcov: fix output location for JSON mode Martin Liska

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).