Introduce -Wcoverage-invalid-line-number
authorMartin Liska <mliska@suse.cz>
Tue, 1 Jun 2021 13:13:18 +0000 (15:13 +0200)
committerMartin Liska <mliska@suse.cz>
Fri, 11 Jun 2021 09:58:20 +0000 (11:58 +0200)
PR gcov-profile/100788

gcc/ChangeLog:

* common.opt: Add new option.
* coverage.c (coverage_begin_function): Emit warning instead on
the internal compiler error.
* doc/invoke.texi: Document the option.
* toplev.c (process_options): Enable it by default.

gcc/testsuite/ChangeLog:

* gcc.dg/pr100788.c: New test.

gcc/common.opt
gcc/coverage.c
gcc/doc/invoke.texi
gcc/testsuite/gcc.dg/pr100788.c [new file with mode: 0644]
gcc/toplev.c

index 7f5fc39..a1353e0 100644 (file)
@@ -826,6 +826,10 @@ Wcoverage-mismatch
 Common Var(warn_coverage_mismatch) Init(1) Warning
 Warn in case profiles in -fprofile-use do not match.
 
+Wcoverage-invalid-line-number
+Common Var(warn_coverage_invalid_linenum) Init(1) Warning
+Warn in case a function ends earlier than it begins due to an invalid linenum macros.
+
 Wmissing-profile
 Common Var(warn_missing_profile) Init(1) Warning
 Warn in case profiles in -fprofile-use do not exist.
index 5a344cd..dfc8108 100644 (file)
@@ -622,18 +622,16 @@ coverage_compute_cfg_checksum (struct function *fn)
 int
 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
 {
-  expanded_location xloc;
-  unsigned long offset;
-
   /* We don't need to output .gcno file unless we're under -ftest-coverage
      (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
   if (no_coverage || !bbg_file_name)
     return 0;
 
-  xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
+  expanded_location startloc
+    = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
 
   /* Announce function */
-  offset = gcov_write_tag (GCOV_TAG_FUNCTION);
+  unsigned long offset = gcov_write_tag (GCOV_TAG_FUNCTION);
   if (param_profile_func_internal_id)
     gcov_write_unsigned (current_function_funcdef_no + 1);
   else
@@ -650,16 +648,27 @@ coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
   gcov_write_unsigned (DECL_ARTIFICIAL (current_function_decl)
                       && !DECL_FUNCTION_VERSIONED (current_function_decl)
                       && !DECL_LAMBDA_FUNCTION_P (current_function_decl));
-  gcov_write_filename (xloc.file);
-  gcov_write_unsigned (xloc.line);
-  gcov_write_unsigned (xloc.column);
+  gcov_write_filename (startloc.file);
+  gcov_write_unsigned (startloc.line);
+  gcov_write_unsigned (startloc.column);
 
   expanded_location endloc = expand_location (cfun->function_end_locus);
 
   /* Function can start in a single file and end in another one.  */
-  int end_line = endloc.file == xloc.file ? endloc.line : xloc.line;
-  int end_column = endloc.file == xloc.file ? endloc.column: xloc.column;
-  gcc_assert (xloc.line <= end_line);
+  int end_line
+    = endloc.file == startloc.file ? endloc.line : startloc.line;
+  int end_column
+    = endloc.file == startloc.file ? endloc.column: startloc.column;
+
+  if (startloc.line > end_line)
+    {
+      warning_at (DECL_SOURCE_LOCATION (current_function_decl),
+                 OPT_Wcoverage_invalid_line_number,
+                 "function starts on a higher line number than it ends");
+      end_line = startloc.line;
+      end_column = startloc.column;
+    }
+
   gcov_write_unsigned (end_line);
   gcov_write_unsigned (end_column);
   gcov_write_length (offset);
index 20d91be..90a50ea 100644 (file)
@@ -5794,6 +5794,17 @@ poorly optimized code and is useful only in the
 case of very minor changes such as bug fixes to an existing code-base.
 Completely disabling the warning is not recommended.
 
+@item -Wno-coverage-invalid-line-number
+@opindex Wno-coverage-invalid-line-number
+@opindex Wcoverage-invalid-line-number
+Warn in case a function ends earlier than it begins due
+to an invalid linenum macros.  The warning is emitted only
+with @option{--coverage} enabled.
+ By default, this warning is enabled and is treated as an
+error.  @option{-Wno-coverage-invalid-line-number} can be used to disable the
+warning or @option{-Wno-error=coverage-invalid-line-number} can be used to
+disable the error.
+
 @item -Wno-cpp
 @r{(C, Objective-C, C++, Objective-C++ and Fortran only)}
 @opindex Wno-cpp
diff --git a/gcc/testsuite/gcc.dg/pr100788.c b/gcc/testsuite/gcc.dg/pr100788.c
new file mode 100644 (file)
index 0000000..6f510ec
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "--coverage -Wno-error=coverage-invalid-line-number" } */
+
+void
+foo() // { dg-warning "function starts on a higher line number than it ends" }
+{
+#line 1
+}
+
+int main()
+{
+  foo ();
+}
index 6a6ebe9..55e7550 100644 (file)
@@ -1744,12 +1744,19 @@ process_options (void)
 
   /* Enable -Werror=coverage-mismatch when -Werror and -Wno-error
      have not been set.  */
-  if (!global_options_set.x_warnings_are_errors
-      && warn_coverage_mismatch
-      && (global_dc->classify_diagnostic[OPT_Wcoverage_mismatch] ==
-          DK_UNSPECIFIED))
-    diagnostic_classify_diagnostic (global_dc, OPT_Wcoverage_mismatch,
-                                    DK_ERROR, UNKNOWN_LOCATION);
+  if (!global_options_set.x_warnings_are_errors)
+    {
+      if (warn_coverage_mismatch
+         && (global_dc->classify_diagnostic[OPT_Wcoverage_mismatch] ==
+             DK_UNSPECIFIED))
+       diagnostic_classify_diagnostic (global_dc, OPT_Wcoverage_mismatch,
+                                       DK_ERROR, UNKNOWN_LOCATION);
+      if (warn_coverage_invalid_linenum
+         && (global_dc->classify_diagnostic[OPT_Wcoverage_invalid_line_number] ==
+             DK_UNSPECIFIED))
+       diagnostic_classify_diagnostic (global_dc, OPT_Wcoverage_invalid_line_number,
+                                       DK_ERROR, UNKNOWN_LOCATION);
+    }
 
   /* Save the current optimization options.  */
   optimization_default_node