]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gcov: return proper exit code when error happens
authorMartin Liska <mliska@suse.cz>
Mon, 18 Oct 2021 09:30:55 +0000 (11:30 +0200)
committerMartin Liska <mliska@suse.cz>
Mon, 18 Oct 2021 11:12:29 +0000 (13:12 +0200)
PR gcov-profile/102746
PR gcov-profile/102747

gcc/ChangeLog:

* gcov.c (main): Return return_code.
(output_gcov_file): Mark return_code when error happens.
(generate_results): Likewise.
(read_graph_file): Likewise.
(read_count_file): Likewise.

gcc/gcov.c

index 3672ae7a6f8e09fb65dafada68faae9969c865b4..34f53ac2d787d713533b8ba3d2899b2f5382598b 100644 (file)
@@ -638,6 +638,9 @@ static int flag_preserve_paths = 0;
 
 static int flag_counts = 0;
 
+/* Return code of the tool invocation.  */
+static int return_code = 0;
+
 /* Forward declarations.  */
 static int process_args (int, char **);
 static void print_usage (int) ATTRIBUTE_NORETURN;
@@ -907,7 +910,7 @@ main (int argc, char **argv)
   if (!flag_use_stdout)
     executed_summary (total_lines, total_executed);
 
-  return 0;
+  return return_code;
 }
 \f
 /* Print a usage message and exit.  If ERROR_P is nonzero, this is an error,
@@ -1467,12 +1470,18 @@ output_gcov_file (const char *file_name, source_info *src)
          fnotice (stdout, "Creating '%s'\n", gcov_file_name);
          output_lines (gcov_file, src);
          if (ferror (gcov_file))
-           fnotice (stderr, "Error writing output file '%s'\n",
-                    gcov_file_name);
+           {
+             fnotice (stderr, "Error writing output file '%s'\n",
+                      gcov_file_name);
+             return_code = 6;
+           }
          fclose (gcov_file);
        }
       else
-       fnotice (stderr, "Could not open output file '%s'\n", gcov_file_name);
+       {
+         fnotice (stderr, "Could not open output file '%s'\n", gcov_file_name);
+         return_code = 6;
+       }
     }
   else
     {
@@ -1594,6 +1603,7 @@ generate_results (const char *file_name)
            {
              fnotice (stderr, "Cannot open JSON output file %s\n",
                       gcov_intermediate_filename.c_str ());
+             return_code = 6;
              return;
            }
 
@@ -1602,6 +1612,7 @@ generate_results (const char *file_name)
            {
              fnotice (stderr, "Error writing JSON output file %s\n",
                       gcov_intermediate_filename.c_str ());
+             return_code = 6;
              return;
            }
        }
@@ -1790,12 +1801,14 @@ read_graph_file (void)
   if (!gcov_open (bbg_file_name, 1))
     {
       fnotice (stderr, "%s:cannot open notes file\n", bbg_file_name);
+      return_code = 1;
       return;
     }
   bbg_file_time = gcov_time ();
   if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC))
     {
       fnotice (stderr, "%s:not a gcov notes file\n", bbg_file_name);
+      return_code = 2;
       gcov_close ();
       return;
     }
@@ -1810,6 +1823,7 @@ read_graph_file (void)
 
       fnotice (stderr, "%s:version '%.4s', prefer '%.4s'\n",
               bbg_file_name, v, e);
+      return_code = 3;
     }
   bbg_stamp = gcov_read_unsigned ();
   /* Read checksum.  */
@@ -1977,6 +1991,7 @@ read_graph_file (void)
        {
        corrupt:;
          fnotice (stderr, "%s:corrupted\n", bbg_file_name);
+         return_code = 4;
          break;
        }
     }
@@ -2009,6 +2024,7 @@ read_count_file (void)
   if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
     {
       fnotice (stderr, "%s:not a gcov data file\n", da_file_name);
+      return_code = 2;
     cleanup:;
       gcov_close ();
       return 1;
@@ -2023,11 +2039,13 @@ read_count_file (void)
 
       fnotice (stderr, "%s:version '%.4s', prefer version '%.4s'\n",
               da_file_name, v, e);
+      return_code = 3;
     }
   tag = gcov_read_unsigned ();
   if (tag != bbg_stamp)
     {
       fnotice (stderr, "%s:stamp mismatch with notes file\n", da_file_name);
+      return_code = 5;
       goto cleanup;
     }
 
@@ -2088,6 +2106,7 @@ read_count_file (void)
                   ? N_("%s:overflowed\n")
                   : N_("%s:corrupted\n"),
                   da_file_name);
+         return_code = 4;
          goto cleanup;
        }
     }