[llvm] r354379 - [llvm-cov] Add support for gcov --hash-filenames option

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 19 12:45:00 PST 2019


Author: vedantk
Date: Tue Feb 19 12:45:00 2019
New Revision: 354379

URL: http://llvm.org/viewvc/llvm-project?rev=354379&view=rev
Log:
[llvm-cov] Add support for gcov --hash-filenames option

The patch adds support for --hash-filenames to llvm-cov. This option adds md5
hash of the source path to the name of the generated .gcov file. The option is
crucial for cases where you have multiple files with the same name but can't
use --preserve-paths as resulting filenames exceed the limit.

from gcov(1):

```
-x
--hash-filenames
    By default, gcov uses the full pathname of the source files to to
    create an output filename.  This can lead to long filenames that
    can overflow filesystem limits.  This option creates names of the
    form source-file##md5.gcov, where the source-file component is
    the final filename part and the md5 component is calculated from
    the full mangled name that would have been used otherwise.
```

Patch by Igor Ignatev!

Differential Revision: https://reviews.llvm.org/D58370

Added:
    llvm/trunk/test/tools/llvm-cov/Inputs/test_hash.output
Modified:
    llvm/trunk/docs/CommandGuide/llvm-cov.rst
    llvm/trunk/include/llvm/ProfileData/GCOV.h
    llvm/trunk/lib/ProfileData/GCOV.cpp
    llvm/trunk/test/tools/llvm-cov/llvm-cov.test
    llvm/trunk/tools/llvm-cov/gcov.cpp

Modified: llvm/trunk/docs/CommandGuide/llvm-cov.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/CommandGuide/llvm-cov.rst?rev=354379&r1=354378&r2=354379&view=diff
==============================================================================
--- llvm/trunk/docs/CommandGuide/llvm-cov.rst (original)
+++ llvm/trunk/docs/CommandGuide/llvm-cov.rst Tue Feb 19 12:45:00 2019
@@ -150,6 +150,11 @@ OPTIONS
 
  Display the version of llvm-cov.
 
+.. option:: -x, --hash-filenames
+
+ Use md5 hash of file name when naming the coverage output files. The source
+ file name will be suffixed by ``##`` followed by MD5 hash calculated for it.
+
 EXIT STATUS
 ^^^^^^^^^^^
 

Modified: llvm/trunk/include/llvm/ProfileData/GCOV.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/GCOV.h?rev=354379&r1=354378&r2=354379&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/GCOV.h (original)
+++ llvm/trunk/include/llvm/ProfileData/GCOV.h Tue Feb 19 12:45:00 2019
@@ -44,9 +44,10 @@ enum GCOVVersion { V402, V404, V704 };
 
 /// A struct for passing gcov options between functions.
 struct Options {
-  Options(bool A, bool B, bool C, bool F, bool P, bool U, bool L, bool N)
+  Options(bool A, bool B, bool C, bool F, bool P, bool U, bool L, bool N, bool X)
       : AllBlocks(A), BranchInfo(B), BranchCount(C), FuncCoverage(F),
-        PreservePaths(P), UncondBranch(U), LongFileNames(L), NoOutput(N) {}
+        PreservePaths(P), UncondBranch(U), LongFileNames(L), NoOutput(N),
+        HashFilenames(X) {}
 
   bool AllBlocks;
   bool BranchInfo;
@@ -56,6 +57,7 @@ struct Options {
   bool UncondBranch;
   bool LongFileNames;
   bool NoOutput;
+  bool HashFilenames;
 };
 
 } // end namespace GCOV

Modified: llvm/trunk/lib/ProfileData/GCOV.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/GCOV.cpp?rev=354379&r1=354378&r2=354379&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/GCOV.cpp (original)
+++ llvm/trunk/lib/ProfileData/GCOV.cpp Tue Feb 19 12:45:00 2019
@@ -18,6 +18,7 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/MD5.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <system_error>
@@ -686,7 +687,15 @@ std::string FileInfo::getCoveragePath(St
   if (Options.LongFileNames && !Filename.equals(MainFilename))
     CoveragePath =
         mangleCoveragePath(MainFilename, Options.PreservePaths) + "##";
-  CoveragePath += mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov";
+  CoveragePath += mangleCoveragePath(Filename, Options.PreservePaths);
+  if (Options.HashFilenames) {
+    MD5 Hasher;
+    MD5::MD5Result Result;
+    Hasher.update(Filename.str());
+    Hasher.final(Result);
+    CoveragePath += "##" + Result.digest().str().str();
+  }
+  CoveragePath += ".gcov";
   return CoveragePath;
 }
 

Added: llvm/trunk/test/tools/llvm-cov/Inputs/test_hash.output
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/Inputs/test_hash.output?rev=354379&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-cov/Inputs/test_hash.output (added)
+++ llvm/trunk/test/tools/llvm-cov/Inputs/test_hash.output Tue Feb 19 12:45:00 2019
@@ -0,0 +1,8 @@
+File 'srcdir/./nested_dir/../test.cpp'
+Lines executed:84.21% of 38
+srcdir/./nested_dir/../test.cpp:creating 'test.cpp##a806e5b3093cd6f683da88c0da150daf.gcov'
+
+File 'srcdir/./nested_dir/../test.h'
+Lines executed:100.00% of 1
+srcdir/./nested_dir/../test.h:creating 'test.h##0cbee7e2421fa4517420ac4f935620ca.gcov'
+

Modified: llvm/trunk/test/tools/llvm-cov/llvm-cov.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/llvm-cov.test?rev=354379&r1=354378&r2=354379&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-cov/llvm-cov.test (original)
+++ llvm/trunk/test/tools/llvm-cov/llvm-cov.test Tue Feb 19 12:45:00 2019
@@ -63,6 +63,11 @@ RUN: llvm-cov gcov -lp -gcno test_paths.
 RUN: diff -aub test_paths.cpp.gcov srcdir#^#test_paths.cpp##srcdir#nested_dir#^#test.cpp.gcov
 RUN: diff -aub test_paths.h.gcov srcdir#^#test_paths.cpp##srcdir#nested_dir#^#test.h.gcov
 
+# Hash pathnames.
+RUN: llvm-cov gcov -x -gcno test_paths.gcno -gcda test_paths.gcda srcdir/../test_paths.cpp | diff -u test_hash.output -
+RUN: diff -aub test_paths.cpp.gcov test.cpp##a806e5b3093cd6f683da88c0da150daf.gcov
+RUN: diff -aub test_paths.h.gcov test.h##0cbee7e2421fa4517420ac4f935620ca.gcov
+
 # Function summaries. This changes stdout, but not the gcov files.
 RUN: llvm-cov gcov test.c -f | diff -u test_-f.output -
 RUN: diff -aub test_no_options.cpp.gcov test.cpp.gcov

Modified: llvm/trunk/tools/llvm-cov/gcov.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-cov/gcov.cpp?rev=354379&r1=354378&r2=354379&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-cov/gcov.cpp (original)
+++ llvm/trunk/tools/llvm-cov/gcov.cpp Tue Feb 19 12:45:00 2019
@@ -124,6 +124,11 @@ int gcovMain(int argc, const char *argv[
                                       "(requires -b)"));
   cl::alias UncondBranchA("unconditional-branches", cl::aliasopt(UncondBranch));
 
+  cl::opt<bool> HashFilenames("x", cl::Grouping, cl::init(false),
+                              cl::desc("Hash long pathnames"));
+  cl::alias HashFilenamesA("hash-filenames", cl::aliasopt(HashFilenames));
+
+
   cl::OptionCategory DebugCat("Internal and debugging options");
   cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
                          cl::desc("Dump the gcov file to stderr"));
@@ -135,7 +140,8 @@ int gcovMain(int argc, const char *argv[
   cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
 
   GCOV::Options Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
-                        PreservePaths, UncondBranch, LongNames, NoOutput);
+                        PreservePaths, UncondBranch, LongNames, NoOutput,
+                        HashFilenames);
 
   for (const auto &SourceFile : SourceFiles)
     reportCoverage(SourceFile, ObjectDir, InputGCNO, InputGCDA, DumpGCOV,




More information about the llvm-commits mailing list