diff mbox

Force use of absolute path names for gcov

Message ID HE1PR0701MB216959FBBD4D2243F34F97BDE41A0@HE1PR0701MB2169.eurprd07.prod.outlook.com
State New
Headers show

Commit Message

Bernd Edlinger April 21, 2017, 6:36 p.m. UTC
Hi!


If gcov is used in projects where gcc is invoked in different
directories, it may happen that the same source is compiled
with different relative names, and the gcov tool is thus unable
to find out, if it is the same source file or not, or to dump the
source at all, which is a limitation of the gcov tool.

So I would like to add a -fprofile-abs-path option that
forces absolute path names in gcno files, which allows gcov
to get the true canonicalized source name.


Bootstrapped and reg-tested on x86_64-pc-linux-gnu.
Is it OK for trunk?


Thanks
Bernd.

Comments

Joseph Myers April 21, 2017, 7:50 p.m. UTC | #1
On Fri, 21 Apr 2017, Bernd Edlinger wrote:

> So I would like to add a -fprofile-abs-path option that
> forces absolute path names in gcno files, which allows gcov
> to get the true canonicalized source name.

I don't see any actual documentation of this option in the patch (you add 
it to the summary list of options, and mention it in text under the 
documentation of --coverage, but don't have any actual @item 
-fprofile-abs-path / @opindex fprofile-abs-path paragraph with text 
describing what the option does).
Bernd Edlinger April 21, 2017, 8:26 p.m. UTC | #2
On 04/21/17 21:50, Joseph Myers wrote:
> On Fri, 21 Apr 2017, Bernd Edlinger wrote:
>
>> So I would like to add a -fprofile-abs-path option that
>> forces absolute path names in gcno files, which allows gcov
>> to get the true canonicalized source name.
>
> I don't see any actual documentation of this option in the patch (you add
> it to the summary list of options, and mention it in text under the
> documentation of --coverage, but don't have any actual @item
> -fprofile-abs-path / @opindex fprofile-abs-path paragraph with text
> describing what the option does).
>

Ah yes, thanks.

So I'll add one more sentence to invoke.texi:

@@ -10696,6 +10713,12 @@
  generate test coverage data.  Coverage data matches the source files
  more closely if you do not optimize.

+@item -fprofile-abs-path
+@opindex fprofile-abs-path
+Automatically convert relative source file names to absolute path names
+in the @file{.gcno} files.  This allows @command{gcov} to find the correct
+sources in projects with multiple directories.
+
  @item -fprofile-dir=@var{path}
  @opindex fprofile-dir




Bernd.
diff mbox

Patch

Index: gcc/common.opt
===================================================================
--- gcc/common.opt	(revision 246571)
+++ gcc/common.opt	(working copy)
@@ -1965,6 +1965,10 @@  fprofile
 Common Report Var(profile_flag)
 Enable basic program profiling code.
 
+fprofile-abs-path
+Common Report Var(profile_abs_path_flag)
+Generate absolute source path names for gcov.
+
 fprofile-arcs
 Common Report Var(profile_arc_flag)
 Insert arc-based program profiling code.
Index: gcc/coverage.c
===================================================================
--- gcc/coverage.c	(revision 246571)
+++ gcc/coverage.c	(working copy)
@@ -663,7 +663,7 @@  coverage_begin_function (unsigned lineno_checksum,
   gcov_write_unsigned (cfg_checksum);
   gcov_write_string (IDENTIFIER_POINTER
 		     (DECL_ASSEMBLER_NAME (current_function_decl)));
-  gcov_write_string (xloc.file);
+  gcov_write_filename (xloc.file);
   gcov_write_unsigned (xloc.line);
   gcov_write_length (offset);
 
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 246571)
+++ gcc/doc/invoke.texi	(working copy)
@@ -441,6 +441,7 @@  Objective-C and Objective-C++ Dialects}.
 @item Program Instrumentation Options
 @xref{Instrumentation Options,,Program Instrumentation Options}.
 @gccoptlist{-p  -pg  -fprofile-arcs  --coverage  -ftest-coverage @gol
+-fprofile-abs-path @gol
 -fprofile-dir=@var{path}  -fprofile-generate  -fprofile-generate=@var{path} @gol
 -fsanitize=@var{style}  -fsanitize-recover  -fsanitize-recover=@var{style} @gol
 -fasan-shadow-offset=@var{number}  -fsanitize-sections=@var{s1},@var{s2},... @gol
@@ -10639,6 +10640,12 @@  additional @option{-ftest-coverage} option.  You d
 every source file in a program.
 
 @item
+Compile the source files additionally with @option{-fprofile-abs-path}
+to create absolute path names in the @file{.gcno} files.  This allows
+@command{gcov} to find the correct sources in projects with multiple
+directories.
+
+@item
 Link your object files with @option{-lgcov} or @option{-fprofile-arcs}
 (the latter implies the former).
 
Index: gcc/gcov-io.c
===================================================================
--- gcc/gcov-io.c	(revision 246571)
+++ gcc/gcov-io.c	(working copy)
@@ -353,6 +353,37 @@  gcov_write_string (const char *string)
 #endif
 
 #if !IN_LIBGCOV
+/* Write FILENAME to coverage file.  Sets error flag on file
+   error, overflow flag on overflow */
+
+GCOV_LINKAGE void
+gcov_write_filename (const char *filename)
+{
+  char buf[1024];
+  size_t len;
+
+  if (profile_abs_path_flag && filename && filename[0]
+      && !(IS_DIR_SEPARATOR (filename[0])
+#if HAVE_DOS_BASED_FILE_SYSTEM
+	   || filename[1] == ':'
+#endif
+	  )
+      && (len = strlen (filename)) < sizeof (buf) - 1)
+    {
+      if (getcwd (buf, sizeof (buf) - len - 1) != NULL)
+	{
+	  if (buf[0] && !IS_DIR_SEPARATOR (buf[strlen (buf) - 1]))
+	    strcat (buf, "/");
+	  strcat (buf, filename);
+	  filename = buf;
+	}
+    }
+
+  return gcov_write_string (filename);
+}
+#endif
+
+#if !IN_LIBGCOV
 /* Write a tag TAG and reserve space for the record length. Return a
    value to be used for gcov_write_length.  */
 
Index: gcc/gcov-io.h
===================================================================
--- gcc/gcov-io.h	(revision 246571)
+++ gcc/gcov-io.h	(working copy)
@@ -388,6 +388,7 @@  GCOV_LINKAGE void gcov_write_unsigned (gcov_unsign
 /* Available only in compiler */
 GCOV_LINKAGE unsigned gcov_histo_index (gcov_type value);
 GCOV_LINKAGE void gcov_write_string (const char *);
+GCOV_LINKAGE void gcov_write_filename (const char *);
 GCOV_LINKAGE gcov_position_t gcov_write_tag (gcov_unsigned_t);
 GCOV_LINKAGE void gcov_write_length (gcov_position_t /*position*/);
 #endif
Index: gcc/profile.c
===================================================================
--- gcc/profile.c	(revision 246571)
+++ gcc/profile.c	(working copy)
@@ -956,7 +956,7 @@  output_location (char const *file_name, int line,
 	{
 	  prev_file_name = file_name;
 	  gcov_write_unsigned (0);
-	  gcov_write_string (prev_file_name);
+	  gcov_write_filename (prev_file_name);
 	}
       if (line_differs)
 	{
Index: gcc/testsuite/gcc.misc-tests/gcov-1a.c
===================================================================
--- gcc/testsuite/gcc.misc-tests/gcov-1a.c	(revision 0)
+++ gcc/testsuite/gcc.misc-tests/gcov-1a.c	(working copy)
@@ -0,0 +1,20 @@ 
+/* Test Gcov basics.  */
+
+/* { dg-options "-fprofile-arcs -ftest-coverage -fprofile-abs-path" } */
+/* { dg-do run { target native } } */
+
+void noop ()
+{
+}
+
+int main ()
+{
+  int i;
+
+  for (i = 0; i < 10; i++)	/* count(11) */
+    noop ();			/* count(10) */
+
+  return 0;			/* count(1) */
+}
+
+/* { dg-final { run-gcov gcov-1a.c } } */