llvm-cov: Sink some reporting logic into CoverageMapping
authorJustin Bogner <mail@justinbogner.com>
Fri, 14 Nov 2014 01:50:32 +0000 (01:50 +0000)
committerJustin Bogner <mail@justinbogner.com>
Fri, 14 Nov 2014 01:50:32 +0000 (01:50 +0000)
This teaches CoverageMapping::getCoveredFunctions to filter to a
particular file and uses that to replace most of the logic found in
llvm-cov report.

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

include/llvm/ProfileData/CoverageMapping.h
lib/ProfileData/CoverageMapping.cpp
tools/llvm-cov/CodeCoverage.cpp
tools/llvm-cov/CoverageSummary.cpp
tools/llvm-cov/CoverageSummary.h

index c1f478459b90659c635b2e683650dda4c73b4901..38fc8ca7c77c54969cc3885a4dccf38f9c57462d 100644 (file)
@@ -18,6 +18,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/Hashing.h"
+#include "llvm/ADT/iterator.h"
 #include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/raw_ostream.h"
 #include <system_error>
@@ -240,6 +241,40 @@ struct FunctionRecord {
         ExecutionCount(ExecutionCount) {}
 };
 
+/// \brief Iterator over Functions, optionally filtered to a single file.
+class FunctionRecordIterator
+    : public iterator_facade_base<FunctionRecordIterator,
+                                  std::forward_iterator_tag, FunctionRecord> {
+  ArrayRef<FunctionRecord> Records;
+  ArrayRef<FunctionRecord>::iterator Current;
+  StringRef Filename;
+
+  /// \brief Skip records whose primary file is not \c Filename.
+  void skipOtherFiles();
+
+public:
+  FunctionRecordIterator(ArrayRef<FunctionRecord> Records_,
+                         StringRef Filename = "")
+      : Records(Records_), Current(Records.begin()), Filename(Filename) {
+    skipOtherFiles();
+  }
+
+  FunctionRecordIterator() : Current(Records.begin()) {}
+
+  bool operator==(const FunctionRecordIterator &RHS) const {
+    return Current == RHS.Current && Filename == RHS.Filename;
+  }
+
+  const FunctionRecord &operator*() const { return *Current; }
+
+  FunctionRecordIterator &operator++() {
+    assert(Current != Records.end() && "incremented past end");
+    ++Current;
+    skipOtherFiles();
+    return *this;
+  }
+};
+
 /// \brief Coverage information for a macro expansion or #included file.
 ///
 /// When covered code has pieces that can be expanded for more detail, such as a
@@ -342,7 +377,7 @@ public:
   unsigned getMismatchedCount() { return MismatchedFunctionCount; }
 
   /// \brief Returns the list of files that are covered.
-  std::vector<StringRef> getUniqueSourceFiles();
+  std::vector<StringRef> getUniqueSourceFiles() const;
 
   /// \brief Get the coverage for a particular file.
   ///
@@ -352,8 +387,16 @@ public:
   CoverageData getCoverageForFile(StringRef Filename);
 
   /// \brief Gets all of the functions covered by this profile.
-  ArrayRef<FunctionRecord> getCoveredFunctions() {
-    return ArrayRef<FunctionRecord>(Functions.data(), Functions.size());
+  iterator_range<FunctionRecordIterator> getCoveredFunctions() const {
+    return make_range(FunctionRecordIterator(Functions),
+                      FunctionRecordIterator());
+  }
+
+  /// \brief Gets all of the functions in a particular file.
+  iterator_range<FunctionRecordIterator>
+  getCoveredFunctions(StringRef Filename) const {
+    return make_range(FunctionRecordIterator(Functions, Filename),
+                      FunctionRecordIterator());
   }
 
   /// \brief Get the list of function instantiations in the file.
index c7dba2c13401aba61b1ad964647a7ad436a42df5..0ccebc2b97ec5f57188b4581a3eac8f925fad523 100644 (file)
@@ -170,6 +170,14 @@ ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
   llvm_unreachable("Unhandled CounterKind");
 }
 
+void FunctionRecordIterator::skipOtherFiles() {
+  while (Current != Records.end() && !Filename.empty() &&
+         Filename != Current->Filenames[0])
+    ++Current;
+  if (Current == Records.end())
+    *this = FunctionRecordIterator();
+}
+
 ErrorOr<std::unique_ptr<CoverageMapping>>
 CoverageMapping::load(ObjectFileCoverageMappingReader &CoverageReader,
                       IndexedInstrProfReader &ProfileReader) {
@@ -320,7 +328,7 @@ public:
 };
 }
 
-std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() {
+std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
   std::vector<StringRef> Filenames;
   for (const auto &Function : getCoveredFunctions())
     for (const auto &Filename : Function.Filenames)
index d6fe9d3471219918d4c057895daef352b65bc0c4..093525eb3dd866ba91fe494259717c6a2b0f7ba3 100644 (file)
@@ -462,7 +462,7 @@ int CodeCoverageTool::report(int argc, const char **argv,
     return 1;
 
   CoverageSummary Summarizer;
-  Summarizer.createSummaries(Coverage->getCoveredFunctions());
+  Summarizer.createSummaries(*Coverage);
   CoverageReport Report(ViewOpts, Summarizer);
   if (SourceFiles.empty() && Filters.empty()) {
     Report.renderFileReports(llvm::outs());
index 785e9024ba7bb9476ced3303881aee5b643a1c8c..059c8c857e455c127c54eab82a0068275af9e5fd 100644 (file)
@@ -28,44 +28,16 @@ unsigned CoverageSummary::getFileID(StringRef Filename) {
 }
 
 void
-CoverageSummary::createSummaries(ArrayRef<coverage::FunctionRecord> Functions) {
-  std::vector<std::pair<unsigned, size_t>> FunctionFileIDs;
-
-  FunctionFileIDs.resize(Functions.size());
-  for (size_t I = 0, E = Functions.size(); I < E; ++I) {
-    StringRef Filename = Functions[I].Filenames[0];
-    FunctionFileIDs[I] = std::make_pair(getFileID(Filename), I);
-  }
-
-  // Sort the function records by file ids
-  std::sort(FunctionFileIDs.begin(), FunctionFileIDs.end(),
-            [](const std::pair<unsigned, size_t> &lhs,
-               const std::pair<unsigned, size_t> &rhs) {
-    return lhs.first < rhs.first;
-  });
-
-  // Create function summaries in a sorted order (by file ids)
-  FunctionSummaries.reserve(Functions.size());
-  for (size_t I = 0, E = Functions.size(); I < E; ++I)
-    FunctionSummaries.push_back(
-        FunctionCoverageSummary::get(Functions[FunctionFileIDs[I].second]));
-
-  // Create file summaries
-  size_t CurrentSummary = 0;
-  for (unsigned FileID = 0; FileID < Filenames.size(); ++FileID) {
-    // Gather the relevant functions summaries
-    auto PrevSummary = CurrentSummary;
-    while (CurrentSummary < FunctionSummaries.size() &&
-           FunctionFileIDs[CurrentSummary].first == FileID)
-      ++CurrentSummary;
-    ArrayRef<FunctionCoverageSummary> LocalSummaries(
-        FunctionSummaries.data() + PrevSummary,
-        FunctionSummaries.data() + CurrentSummary);
-    if (LocalSummaries.empty())
+CoverageSummary::createSummaries(const coverage::CoverageMapping &Coverage) {
+  for (StringRef Filename : Coverage.getUniqueSourceFiles()) {
+    size_t PrevSize = FunctionSummaries.size();
+    for (const auto &F : Coverage.getCoveredFunctions(Filename))
+      FunctionSummaries.push_back(FunctionCoverageSummary::get(F));
+    size_t Count = FunctionSummaries.size() - PrevSize;
+    if (Count == 0)
       continue;
-
-    FileSummaries.push_back(
-        FileCoverageSummary::get(Filenames[FileID], LocalSummaries));
+    FileSummaries.push_back(FileCoverageSummary::get(
+        Filename, makeArrayRef(FunctionSummaries.data() + PrevSize, Count)));
   }
 }
 
index b93103c8cf2c3236d17bc711adedfe56d784f989..9dbebde949e5ce5a09da7f808250165f17f1a6b8 100644 (file)
@@ -30,7 +30,7 @@ class CoverageSummary {
   unsigned getFileID(StringRef Filename);
 
 public:
-  void createSummaries(ArrayRef<coverage::FunctionRecord> Functions);
+  void createSummaries(const coverage::CoverageMapping &Coverage);
 
   ArrayRef<FileCoverageSummary> getFileSummaries() { return FileSummaries; }