[PATCH][BACKPORT] coverage: handle function beginning

Martin Liška mliska@suse.cz
Fri Aug 13 13:49:49 GMT 2021


It's backport of 8bf728aecc4fea46b4490e950b9ae229f90597b0, where I'm not willing
to introduce a new warning.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests on gcc-11 branch.
I'm going to push the revision.

Martin

	PR gcov-profile/100788

gcc/ChangeLog:

	* coverage.c (coverage_begin_function): Update function
	  beginning when #line macro is used.

gcc/testsuite/ChangeLog:

	* gcc.dg/pr100788.c: New test.
---
  gcc/coverage.c                  | 28 +++++++++++++++++-----------
  gcc/testsuite/gcc.dg/pr100788.c | 13 +++++++++++++
  2 files changed, 30 insertions(+), 11 deletions(-)
  create mode 100644 gcc/testsuite/gcc.dg/pr100788.c

diff --git a/gcc/coverage.c b/gcc/coverage.c
index dc9ea6f3ee1..9b98e0173ab 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,24 @@ 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)
+    {
+      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/testsuite/gcc.dg/pr100788.c b/gcc/testsuite/gcc.dg/pr100788.c
new file mode 100644
index 00000000000..5d54329f752
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr100788.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "--coverage" } */
+
+void
+foo()
+{
+#line 1
+}
+
+int main()
+{
+  foo ();
+}
-- 
2.32.0



More information about the Gcc-patches mailing list