From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1851) id DB58B3850409; Fri, 20 Aug 2021 08:07:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB58B3850409 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Martin Liska To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-3038] gcov: fix output location for JSON mode. X-Act-Checkin: gcc X-Git-Author: Martin Liska X-Git-Refname: refs/heads/master X-Git-Oldrev: d2883be3c8e7b5fd17925ea67b99b7330e1a4f72 X-Git-Newrev: b777f228b481ae881a7fbb09de367a053740932c Message-Id: <20210820080720.DB58B3850409@sourceware.org> Date: Fri, 20 Aug 2021 08:07:20 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 20 Aug 2021 08:07:21 -0000 https://gcc.gnu.org/g:b777f228b481ae881a7fbb09de367a053740932c commit r12-3038-gb777f228b481ae881a7fbb09de367a053740932c Author: Martin Liska 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::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