From: Martin Liska Date: Tue, 17 Aug 2021 14:24:26 +0000 (+0200) Subject: gcov: fix output location for JSON mode. X-Git-Tag: upstream/12.2.0~5659 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b777f228b481ae881a7fbb09de367a053740932c;p=platform%2Fupstream%2Fgcc.git 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 --git a/gcc/gcov.c b/gcc/gcov.c index 5c651a9..cf0a49d 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