diff mbox series

Introduce -Wcoverage-invalid-line-number

Message ID 22f1ed9b-d967-8e97-0e2a-4d069c0c2f26@suse.cz
State New
Headers show
Series Introduce -Wcoverage-invalid-line-number | expand

Commit Message

Martin Liška June 1, 2021, 2:09 p.m. UTC
Hello.

As seen in the PR, one can easily corrupt line number information and
we can end up with a function that ends before it starts ;)
I'm adding a new warning for that instead of the ICE.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

	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                  |  4 ++++
  gcc/coverage.c                  | 31 ++++++++++++++++++++-----------
  gcc/doc/invoke.texi             | 11 +++++++++++
  gcc/testsuite/gcc.dg/pr100788.c | 13 +++++++++++++
  gcc/toplev.c                    | 19 +++++++++++++------
  5 files changed, 61 insertions(+), 17 deletions(-)
  create mode 100644 gcc/testsuite/gcc.dg/pr100788.c

Comments

Jeff Law June 9, 2021, 4:14 p.m. UTC | #1
On 6/1/2021 8:09 AM, Martin Liška wrote:
> Hello.
>
> As seen in the PR, one can easily corrupt line number information and
> we can end up with a function that ends before it starts ;)
> I'm adding a new warning for that instead of the ICE.
>
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
>
> Ready to be installed?
> Thanks,
> Martin
>
>     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.
OK
jeff
diff mbox series

Patch

diff --git a/gcc/common.opt b/gcc/common.opt
index ffb968d90f8..509937da24f 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -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.
diff --git a/gcc/coverage.c b/gcc/coverage.c
index 5a344cdfc17..dfc8108d5d8 100644
--- a/gcc/coverage.c
+++ b/gcc/coverage.c
@@ -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);
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 08c3206b719..e91680ab329 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -5795,6 +5795,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
index 00000000000..6f510ecf57c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr100788.c
@@ -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 ();
+}
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 6a6ebe9bb8c..55e7550151f 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -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