llvm-cov: Added -a option for block data.
authorYuchen Wu <yuchenericwu@hotmail.com>
Tue, 10 Dec 2013 01:02:07 +0000 (01:02 +0000)
committerYuchen Wu <yuchenericwu@hotmail.com>
Tue, 10 Dec 2013 01:02:07 +0000 (01:02 +0000)
Similar to gcov, llvm-cov will now print out the block count at the end
of each block. Multiple blocks can end on the same line.

One computational difference is by using -a, llvm-cov will no longer
simply add the block counts together to form a line count. Instead, it
will take the maximum of the block counts on that line. This has a
similar effect to what gcov does, but generates more correct counts in
certain scenarios.

Also updated tests.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@196856 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/GCOV.h
lib/IR/GCOV.cpp
test/tools/llvm-cov/Inputs/test.cpp.orig_gcov [deleted file]
test/tools/llvm-cov/Inputs/test.h.orig_gcov [deleted file]
test/tools/llvm-cov/Inputs/test_-a.cpp.gcov [new file with mode: 0644]
test/tools/llvm-cov/Inputs/test_-a.h.gcov [new file with mode: 0644]
test/tools/llvm-cov/Inputs/test_no_options.cpp.gcov [new file with mode: 0644]
test/tools/llvm-cov/Inputs/test_no_options.h.gcov [new file with mode: 0644]
test/tools/llvm-cov/llvm-cov.test
tools/llvm-cov/llvm-cov.cpp

index 5c27316f57b84e0f89b234541a1756fbec39dd8e..852c0bef458f65fc74a8e53002e17c90dfc90008 100644 (file)
@@ -34,6 +34,13 @@ namespace GCOV {
   };
 } // end GCOV namespace
 
+/// GCOVOptions - A struct for passing gcov options between functions.
+struct GCOVOptions {
+  GCOVOptions(bool A): AllBlocks(A) {}
+
+  bool AllBlocks;
+};
+
 /// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
 /// read operations.
 class GCOVBuffer {
@@ -245,6 +252,7 @@ public:
   bool readGCNO(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
   bool readGCDA(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
   StringRef getFilename() const { return Filename; }
+  size_t getNumBlocks() const { return Blocks.size(); }
   void dump() const;
   void collectLineCounts(FileInfo &FI);
 private:
@@ -275,6 +283,7 @@ public:
     DstEdges.push_back(Edge);
   }
   void addLine(uint32_t N) { Lines.push_back(N); }
+  uint32_t getLastLine() const { return Lines.back(); }
   void addCount(size_t DstEdgeNo, uint64_t N);
   uint64_t getCount() const { return Counter; }
   size_t getNumSrcEdges() const { return SrcEdges.size(); }
@@ -305,7 +314,8 @@ public:
   }
   void setRunCount(uint32_t Runs) { RunCount = Runs; }
   void setProgramCount(uint32_t Programs) { ProgramCount = Programs; }
-  void print(StringRef GCNOFile, StringRef GCDAFile) const;
+  void print(StringRef GCNOFile, StringRef GCDAFile,
+             const GCOVOptions &Options) const;
 private:
   StringMap<LineData> LineInfo;
   uint32_t RunCount;
index c7a1102d10add0e09de4e874b3587c9afdf5b805..fb7370020a59869c119ef113ba85e878096c2e57 100644 (file)
@@ -358,7 +358,8 @@ void GCOVBlock::dump() const {
 // FileInfo implementation.
 
 /// print -  Print source files with collected line count information.
-void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) const {
+void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile,
+                     const GCOVOptions &Options) const {
   for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
          E = LineInfo.end(); I != E; ++I) {
     StringRef Filename = I->first();
@@ -385,13 +386,21 @@ void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) const {
     for (uint32_t i = 0; !AllLines.empty(); ++i) {
       LineData::const_iterator BlocksIt = Line.find(i);
 
-      // Add up the block counts to form line counts.
       if (BlocksIt != Line.end()) {
+        // Add up the block counts to form line counts.
         const BlockVector &Blocks = BlocksIt->second;
         uint64_t LineCount = 0;
         for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
                I != E; ++I) {
-          LineCount += (*I)->getCount();
+          const GCOVBlock *Block = *I;
+          if (Options.AllBlocks) {
+            // Only take the highest block count for that line.
+            uint64_t BlockCount = Block->getCount();
+            LineCount = LineCount > BlockCount ? LineCount : BlockCount;
+          } else {
+            // Sum up all of the block counts.
+            LineCount += Block->getCount();
+          }
         }
         if (LineCount == 0)
           OS << "    #####:";
@@ -403,6 +412,24 @@ void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) const {
       std::pair<StringRef, StringRef> P = AllLines.split('\n');
       OS << format("%5u:", i+1) << P.first << "\n";
       AllLines = P.second;
+
+      if (Options.AllBlocks && BlocksIt != Line.end()) {
+        // Output the counts for each block at the last line of the block.
+        uint32_t BlockNo = 0;
+        const BlockVector &Blocks = BlocksIt->second;
+        for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
+               I != E; ++I) {
+          const GCOVBlock *Block = *I;
+          if (Block->getLastLine() != i+1)
+            continue;
+
+          if (Block->getCount() == 0)
+            OS << "    $$$$$:";
+          else
+            OS << format("%9lu:", Block->getCount());
+          OS << format("%5u-block  %u\n", i+1, BlockNo++);
+        }
+      }
     }
   }
 }
diff --git a/test/tools/llvm-cov/Inputs/test.cpp.orig_gcov b/test/tools/llvm-cov/Inputs/test.cpp.orig_gcov
deleted file mode 100644 (file)
index 871e3ba..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-        -:    0:Source:test.cpp
-        -:    0:Graph:test.gcno
-        -:    0:Data:test.gcda
-        -:    0:Runs:2
-        -:    0:Programs:1
-        -:    1:#include "test.h"
-        -:    2:#include <cstdlib>
-        -:    3:
-        -:    4:bool on = false;
-        -:    5:int len = 42;
-        -:    6:double grid[10][10] = {0};
-        -:    7:const char * hello = "world";
-        -:    8:const char * world = "hello";
-        -:    9:
-8589934592:   10:void A::B() {}
-        -:   11:
-    #####:   12:void useless() {}
-        -:   13:
-        -:   14:double more_useless() {
-    #####:   15:  return 0;
-        -:   16:}
-        -:   17:
-        -:   18:int foo() {
-        2:   19:  on = true;
-        2:   20:  return 3;
-        -:   21:}
-        -:   22:
-        -:   23:int bar() {
-    #####:   24:  len--;
-    #####:   25:  return foo() + 45;
-        -:   26:}
-        -:   27:
-        8:   28:void assign(int ii, int jj) {
-        8:   29:  grid[ii][jj] = (ii+1) * (jj+1);
-        8:   30:}
-        -:   31:
-        -:   32:void initialize_grid() {
-       12:   33:  for (int ii = 0; ii < 2; ii++)
-       24:   34:    for (int jj = 0; jj < 2; jj++)
-       12:   35:      assign(ii, jj);
-        2:   36:}
-        -:   37:
-        -:   38:int main() {
-        2:   39:  initialize_grid();
-        -:   40:
-        2:   41:  int a = 2;
-        2:   42:  on = rand() % 2;
-        2:   43:  if (on) {
-        2:   44:    foo();
-        2:   45:    ++a;
-        2:   46:  } else {
-    #####:   47:    bar();
-    #####:   48:    a += rand();
-        -:   49:  }
-        -:   50:
-       44:   51:  for (int ii = 0; ii < 10; ++ii) {
-       20:   52:    switch (rand() % 5) {
-        -:   53:      case 0:
-        4:   54:        a += rand();
-        4:   55:        break;
-        -:   56:      case 1:
-        -:   57:      case 2:
-        2:   58:        a += rand() / rand();
-        2:   59:        break;
-        -:   60:      case 3:
-        6:   61:        a -= rand();
-        6:   62:        break;
-        -:   63:      default:
-        8:   64:        a = -1;
-        8:   65:    }
-       20:   66:  }
-        -:   67:
-        2:   68:  A thing;
-17179869188:   69:  for (uint64_t ii = 0; ii < 4294967296; ++ii)
-8589934592:   70:    thing.B();
-        -:   71:
-        2:   72:  return a + 8 + grid[2][3] + len;
-        -:   73:  return more_useless();
-        -:   74:}
diff --git a/test/tools/llvm-cov/Inputs/test.h.orig_gcov b/test/tools/llvm-cov/Inputs/test.h.orig_gcov
deleted file mode 100644 (file)
index 4ba58c9..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-        -:    0:Source:./test.h
-        -:    0:Graph:test.gcno
-        -:    0:Data:test.gcda
-        -:    0:Runs:2
-        -:    0:Programs:1
-        4:    1:struct A {
-        -:    2:  virtual void B();
-        -:    3:};
diff --git a/test/tools/llvm-cov/Inputs/test_-a.cpp.gcov b/test/tools/llvm-cov/Inputs/test_-a.cpp.gcov
new file mode 100644 (file)
index 0000000..c2210d5
--- /dev/null
@@ -0,0 +1,111 @@
+        -:    0:Source:test.cpp
+        -:    0:Graph:test.gcno
+        -:    0:Data:test.gcda
+        -:    0:Runs:2
+        -:    0:Programs:1
+        -:    1:#include "test.h"
+        -:    2:#include <cstdlib>
+        -:    3:
+        -:    4:bool on = false;
+        -:    5:int len = 42;
+        -:    6:double grid[10][10] = {0};
+        -:    7:const char * hello = "world";
+        -:    8:const char * world = "hello";
+        -:    9:
+8589934592:   10:void A::B() {}
+8589934592:   10-block  0
+        -:   11:
+    #####:   12:void useless() {}
+    $$$$$:   12-block  0
+        -:   13:
+        -:   14:double more_useless() {
+    #####:   15:  return 0;
+    $$$$$:   15-block  0
+        -:   16:}
+        -:   17:
+        -:   18:int foo() {
+        2:   19:  on = true;
+        2:   20:  return 3;
+        2:   20-block  0
+        -:   21:}
+        -:   22:
+        -:   23:int bar() {
+    #####:   24:  len--;
+    #####:   25:  return foo() + 45;
+    $$$$$:   25-block  0
+        -:   26:}
+        -:   27:
+        8:   28:void assign(int ii, int jj) {
+        8:   29:  grid[ii][jj] = (ii+1) * (jj+1);
+        8:   30:}
+        8:   30-block  0
+        -:   31:
+        -:   32:void initialize_grid() {
+        6:   33:  for (int ii = 0; ii < 2; ii++)
+        2:   33-block  0
+        6:   33-block  1
+        4:   33-block  2
+       12:   34:    for (int jj = 0; jj < 2; jj++)
+        4:   34-block  0
+       12:   34-block  1
+        8:   34-block  2
+        8:   35:      assign(ii, jj);
+        8:   35-block  0
+        4:   35-block  1
+        2:   36:}
+        2:   36-block  0
+        -:   37:
+        -:   38:int main() {
+        2:   39:  initialize_grid();
+        -:   40:
+        2:   41:  int a = 2;
+        2:   42:  on = rand() % 2;
+        2:   43:  if (on) {
+        2:   43-block  0
+        2:   44:    foo();
+        2:   45:    ++a;
+        2:   46:  } else {
+        2:   46-block  0
+    #####:   47:    bar();
+    #####:   48:    a += rand();
+    $$$$$:   48-block  0
+        -:   49:  }
+        -:   50:
+       22:   51:  for (int ii = 0; ii < 10; ++ii) {
+        2:   51-block  0
+       22:   51-block  1
+       20:   51-block  2
+       20:   52:    switch (rand() % 5) {
+       20:   52-block  0
+        -:   53:      case 0:
+        4:   54:        a += rand();
+        4:   55:        break;
+        4:   55-block  0
+        -:   56:      case 1:
+        -:   57:      case 2:
+        2:   58:        a += rand() / rand();
+        2:   59:        break;
+        2:   59-block  0
+        -:   60:      case 3:
+        6:   61:        a -= rand();
+        6:   62:        break;
+        6:   62-block  0
+        -:   63:      default:
+        8:   64:        a = -1;
+        8:   65:    }
+        8:   65-block  0
+       20:   66:  }
+       20:   66-block  0
+        -:   67:
+        2:   68:  A thing;
+8589934594:   69:  for (uint64_t ii = 0; ii < 4294967296; ++ii)
+        2:   69-block  0
+8589934594:   69-block  1
+8589934592:   69-block  2
+8589934592:   70:    thing.B();
+8589934592:   70-block  0
+        -:   71:
+        2:   72:  return a + 8 + grid[2][3] + len;
+        2:   72-block  0
+        -:   73:  return more_useless();
+        -:   74:}
diff --git a/test/tools/llvm-cov/Inputs/test_-a.h.gcov b/test/tools/llvm-cov/Inputs/test_-a.h.gcov
new file mode 100644 (file)
index 0000000..a5fe62b
--- /dev/null
@@ -0,0 +1,10 @@
+        -:    0:Source:./test.h
+        -:    0:Graph:test.gcno
+        -:    0:Data:test.gcda
+        -:    0:Runs:2
+        -:    0:Programs:1
+        2:    1:struct A {
+        2:    1-block  0
+        2:    1-block  1
+        -:    2:  virtual void B();
+        -:    3:};
diff --git a/test/tools/llvm-cov/Inputs/test_no_options.cpp.gcov b/test/tools/llvm-cov/Inputs/test_no_options.cpp.gcov
new file mode 100644 (file)
index 0000000..871e3ba
--- /dev/null
@@ -0,0 +1,79 @@
+        -:    0:Source:test.cpp
+        -:    0:Graph:test.gcno
+        -:    0:Data:test.gcda
+        -:    0:Runs:2
+        -:    0:Programs:1
+        -:    1:#include "test.h"
+        -:    2:#include <cstdlib>
+        -:    3:
+        -:    4:bool on = false;
+        -:    5:int len = 42;
+        -:    6:double grid[10][10] = {0};
+        -:    7:const char * hello = "world";
+        -:    8:const char * world = "hello";
+        -:    9:
+8589934592:   10:void A::B() {}
+        -:   11:
+    #####:   12:void useless() {}
+        -:   13:
+        -:   14:double more_useless() {
+    #####:   15:  return 0;
+        -:   16:}
+        -:   17:
+        -:   18:int foo() {
+        2:   19:  on = true;
+        2:   20:  return 3;
+        -:   21:}
+        -:   22:
+        -:   23:int bar() {
+    #####:   24:  len--;
+    #####:   25:  return foo() + 45;
+        -:   26:}
+        -:   27:
+        8:   28:void assign(int ii, int jj) {
+        8:   29:  grid[ii][jj] = (ii+1) * (jj+1);
+        8:   30:}
+        -:   31:
+        -:   32:void initialize_grid() {
+       12:   33:  for (int ii = 0; ii < 2; ii++)
+       24:   34:    for (int jj = 0; jj < 2; jj++)
+       12:   35:      assign(ii, jj);
+        2:   36:}
+        -:   37:
+        -:   38:int main() {
+        2:   39:  initialize_grid();
+        -:   40:
+        2:   41:  int a = 2;
+        2:   42:  on = rand() % 2;
+        2:   43:  if (on) {
+        2:   44:    foo();
+        2:   45:    ++a;
+        2:   46:  } else {
+    #####:   47:    bar();
+    #####:   48:    a += rand();
+        -:   49:  }
+        -:   50:
+       44:   51:  for (int ii = 0; ii < 10; ++ii) {
+       20:   52:    switch (rand() % 5) {
+        -:   53:      case 0:
+        4:   54:        a += rand();
+        4:   55:        break;
+        -:   56:      case 1:
+        -:   57:      case 2:
+        2:   58:        a += rand() / rand();
+        2:   59:        break;
+        -:   60:      case 3:
+        6:   61:        a -= rand();
+        6:   62:        break;
+        -:   63:      default:
+        8:   64:        a = -1;
+        8:   65:    }
+       20:   66:  }
+        -:   67:
+        2:   68:  A thing;
+17179869188:   69:  for (uint64_t ii = 0; ii < 4294967296; ++ii)
+8589934592:   70:    thing.B();
+        -:   71:
+        2:   72:  return a + 8 + grid[2][3] + len;
+        -:   73:  return more_useless();
+        -:   74:}
diff --git a/test/tools/llvm-cov/Inputs/test_no_options.h.gcov b/test/tools/llvm-cov/Inputs/test_no_options.h.gcov
new file mode 100644 (file)
index 0000000..4ba58c9
--- /dev/null
@@ -0,0 +1,8 @@
+        -:    0:Source:./test.h
+        -:    0:Graph:test.gcno
+        -:    0:Data:test.gcda
+        -:    0:Runs:2
+        -:    0:Programs:1
+        4:    1:struct A {
+        -:    2:  virtual void B();
+        -:    3:};
index 184f858b8c7a638a1fe494b2442efaa3865b095d..43725a3cbdd8074c24753008d70a4e21541673da 100644 (file)
@@ -6,8 +6,12 @@ RUN: cd %t
 RUN: cp %p/Inputs/test* .
 
 RUN: llvm-cov -gcno=test.gcno -gcda=test.gcda
-RUN: diff -aub test.cpp.orig_gcov test.cpp.gcov
-RUN: diff -aub test.h.orig_gcov test.h.gcov
+RUN: diff -aub test_no_options.cpp.gcov test.cpp.gcov
+RUN: diff -aub test_no_options.h.gcov test.h.gcov
+
+RUN: llvm-cov -gcno=test.gcno -gcda=test.gcda -a
+RUN: diff -aub test_-a.cpp.gcov test.cpp.gcov
+RUN: diff -aub test_-a.h.gcov test.h.gcov
 
 RUN: not llvm-cov -gcno=test_read_fail.gcno -gcda=test.gcda
 
index 7d3277aa8bdec9a34d5f2480b030e8ab0903429a..235670b7ce8a90c60096c9d5602b29fe9bf0dffc 100644 (file)
@@ -30,6 +30,9 @@ InputGCNO("gcno", cl::desc("<input gcno file>"), cl::init(""));
 static cl::opt<std::string>
 InputGCDA("gcda", cl::desc("<input gcda file>"), cl::init(""));
 
+static cl::opt<bool>
+AllBlocks("a", cl::init(false), cl::desc("display all block info"));
+
 //===----------------------------------------------------------------------===//
 int main(int argc, char **argv) {
   // Print a stack trace if we signal out.
@@ -67,12 +70,11 @@ int main(int argc, char **argv) {
     }
   }
 
-
   if (DumpGCOV)
     GF.dump();
 
   FileInfo FI;
   GF.collectLineCounts(FI);
-  FI.print(InputGCNO, InputGCDA);
+  FI.print(InputGCNO, InputGCDA, GCOVOptions(AllBlocks));
   return 0;
 }