]> granicus.if.org Git - llvm/commitdiff
[Coverage] Add an API to retrive all instantiations of a function (NFC)
authorVedant Kumar <vsk@apple.com>
Wed, 2 Aug 2017 23:35:25 +0000 (23:35 +0000)
committerVedant Kumar <vsk@apple.com>
Wed, 2 Aug 2017 23:35:25 +0000 (23:35 +0000)
The CoverageMapping::getInstantiations() API retrieved all function
records corresponding to functions with more than one instantiation (e.g
template functions with multiple specializations). However, there was no
simple way to determine *which* function a given record was an
instantiation of. This was an oversight, since it's useful to aggregate
coverage information over all instantiations of a function.

llvm-cov works around this by building a mapping of source locations to
instantiation sets, but this duplicates logic that libCoverage already
has (see FunctionInstantiationSetCollector).

This change adds a new API, CoverageMapping::getInstantiationGroups(),
which returns a list of InstantiationGroups. A group contains records
for each instantiation of some particular function, and also provides
utilities to get the total execution count within the group, the source
location of the common definition, etc.

This lets removes some hacky logic in llvm-cov by reusing
FunctionInstantiationSetCollector and makes the CoverageMapping API
friendlier for other clients.

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

include/llvm/ProfileData/Coverage/CoverageMapping.h
lib/ProfileData/Coverage/CoverageMapping.cpp
tools/llvm-cov/CodeCoverage.cpp
tools/llvm-cov/CoverageReport.cpp
tools/llvm-cov/CoverageSummaryInfo.cpp
tools/llvm-cov/CoverageSummaryInfo.h
unittests/ProfileData/CoverageMappingTest.cpp

index 980d47e7afe43f5c12e368fe6f5015fc813a0a58..74ca814bed906da1a7e9142be9dbb958e8b01efb 100644 (file)
@@ -396,6 +396,63 @@ struct CoverageSegment {
   }
 };
 
+/// An instantiation group contains a \c FunctionRecord list, such that each
+/// record corresponds to a distinct instantiation of the same function.
+///
+/// Note that it's possible for a function to have more than one instantiation
+/// (consider C++ template specializations or static inline functions).
+class InstantiationGroup {
+  friend class CoverageMapping;
+
+  unsigned Line;
+  unsigned Col;
+  std::vector<const FunctionRecord *> Instantiations;
+
+  InstantiationGroup(unsigned Line, unsigned Col,
+                     std::vector<const FunctionRecord *> Instantiations)
+      : Line(Line), Col(Col), Instantiations(std::move(Instantiations)) {}
+
+public:
+  InstantiationGroup(const InstantiationGroup &) = delete;
+  InstantiationGroup(InstantiationGroup &&) = default;
+
+  /// Get the number of instantiations in this group.
+  size_t size() const { return Instantiations.size(); }
+
+  /// Get the line where the common function was defined.
+  unsigned getLine() const { return Line; }
+
+  /// Get the column where the common function was defined.
+  unsigned getColumn() const { return Col; }
+
+  /// Check if the instantiations in this group have a common mangled name.
+  bool hasName() const {
+    for (unsigned I = 1, E = Instantiations.size(); I < E; ++I)
+      if (Instantiations[I]->Name != Instantiations[0]->Name)
+        return false;
+    return true;
+  }
+
+  /// Get the common mangled name for instantiations in this group.
+  StringRef getName() const {
+    assert(hasName() && "Instantiations don't have a shared name");
+    return Instantiations[0]->Name;
+  }
+
+  /// Get the total execution count of all instantiations in this group.
+  uint64_t getTotalExecutionCount() const {
+    uint64_t Count = 0;
+    for (const FunctionRecord *F : Instantiations)
+      Count += F->ExecutionCount;
+    return Count;
+  }
+
+  /// Get the instantiations in this group.
+  ArrayRef<const FunctionRecord *> getInstantiations() const {
+    return Instantiations;
+  }
+};
+
 /// \brief Coverage information to be processed or displayed.
 ///
 /// This represents the coverage of an entire file, expansion, or function. It
@@ -490,12 +547,12 @@ public:
                       FunctionRecordIterator());
   }
 
-  /// \brief Get the list of function instantiations in the file.
+  /// Get the list of function instantiation groups in a particular file.
   ///
-  /// Functions that are instantiated more than once, such as C++ template
-  /// specializations, have distinct coverage records for each instantiation.
-  std::vector<const FunctionRecord *>
-  getInstantiations(StringRef Filename) const;
+  /// Every instantiation group in a program is attributed to exactly one file:
+  /// the file in which the definition for the common function begins.
+  std::vector<InstantiationGroup>
+  getInstantiationGroups(StringRef Filename) const;
 
   /// \brief Get the coverage for a particular function.
   CoverageData getCoverageForFunction(const FunctionRecord &Function) const;
index c435cb6ac84914cf2dd9106687d75416dfea8672..346ac76a6f7ab27c717199475b9a72680cf48445 100644 (file)
@@ -510,8 +510,8 @@ CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
   return FileCoverage;
 }
 
-std::vector<const FunctionRecord *>
-CoverageMapping::getInstantiations(StringRef Filename) const {
+std::vector<InstantiationGroup>
+CoverageMapping::getInstantiationGroups(StringRef Filename) const {
   FunctionInstantiationSetCollector InstantiationSetCollector;
   for (const auto &Function : Functions) {
     auto MainFileID = findMainViewFileID(Filename, Function);
@@ -520,12 +520,12 @@ CoverageMapping::getInstantiations(StringRef Filename) const {
     InstantiationSetCollector.insert(Function, *MainFileID);
   }
 
-  std::vector<const FunctionRecord *> Result;
+  std::vector<InstantiationGroup> Result;
   for (const auto &InstantiationSet : InstantiationSetCollector) {
-    if (InstantiationSet.second.size() < 2)
-      continue;
-    Result.insert(Result.end(), InstantiationSet.second.begin(),
-                  InstantiationSet.second.end());
+    InstantiationGroup IG{InstantiationSet.first.first,
+                          InstantiationSet.first.second,
+                          std::move(InstantiationSet.second)};
+    Result.emplace_back(std::move(IG));
   }
   return Result;
 }
index c16d7b44ad628c3d3b890c1cc2b6520741605ba7..7098b51ce7d8d330620f03eba5defe11ecb5e70f 100644 (file)
@@ -291,25 +291,31 @@ CodeCoverageTool::createSourceFileView(StringRef SourceFile,
   if (!ViewOpts.ShowFunctionInstantiations)
     return View;
 
-  for (const auto *Function : Coverage.getInstantiations(SourceFile)) {
-    std::unique_ptr<SourceCoverageView> SubView{nullptr};
+  for (const auto &Group : Coverage.getInstantiationGroups(SourceFile)) {
+    // Skip functions which have a single instantiation.
+    if (Group.size() < 2)
+      continue;
 
-    StringRef Funcname = DC.demangle(Function->Name);
+    for (const FunctionRecord *Function : Group.getInstantiations()) {
+      std::unique_ptr<SourceCoverageView> SubView{nullptr};
 
-    if (Function->ExecutionCount > 0) {
-      auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
-      auto SubViewExpansions = SubViewCoverage.getExpansions();
-      SubView = SourceCoverageView::create(
-          Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
-      attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
-    }
+      StringRef Funcname = DC.demangle(Function->Name);
 
-    unsigned FileID = Function->CountedRegions.front().FileID;
-    unsigned Line = 0;
-    for (const auto &CR : Function->CountedRegions)
-      if (CR.FileID == FileID)
-        Line = std::max(CR.LineEnd, Line);
-    View->addInstantiation(Funcname, Line, std::move(SubView));
+      if (Function->ExecutionCount > 0) {
+        auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
+        auto SubViewExpansions = SubViewCoverage.getExpansions();
+        SubView = SourceCoverageView::create(
+            Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
+        attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
+      }
+
+      unsigned FileID = Function->CountedRegions.front().FileID;
+      unsigned Line = 0;
+      for (const auto &CR : Function->CountedRegions)
+        if (CR.FileID == FileID)
+          Line = std::max(CR.LineEnd, Line);
+      View->addInstantiation(Funcname, Line, std::move(SubView));
+    }
   }
   return View;
 }
index c68bb9048df1b18bf39fbeeb1f4cd8ee93022d3f..7c273a2430c3c4b1fd4cef6a8fc8ec62a3875164 100644 (file)
@@ -317,25 +317,19 @@ CoverageReport::prepareFileReports(const coverage::CoverageMapping &Coverage,
   for (StringRef Filename : Files) {
     FileCoverageSummary Summary(Filename.drop_front(LCP));
 
-    // Map source locations to aggregate function coverage summaries.
-    DenseMap<std::pair<unsigned, unsigned>, FunctionCoverageSummary> Summaries;
-
-    for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
-      FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
-      auto StartLoc = F.CountedRegions[0].startLoc();
-
-      auto UniquedSummary = Summaries.insert({StartLoc, Function});
-      if (!UniquedSummary.second)
-        UniquedSummary.first->second.update(Function);
-
-      Summary.addInstantiation(Function);
-      Totals.addInstantiation(Function);
-    }
+    for (const auto &Group : Coverage.getInstantiationGroups(Filename)) {
+      std::vector<FunctionCoverageSummary> InstantiationSummaries;
+      for (const coverage::FunctionRecord *F : Group.getInstantiations()) {
+        auto InstantiationSummary = FunctionCoverageSummary::get(*F);
+        Summary.addInstantiation(InstantiationSummary);
+        Totals.addInstantiation(InstantiationSummary);
+        InstantiationSummaries.push_back(InstantiationSummary);
+      }
 
-    for (const auto &UniquedSummary : Summaries) {
-      const FunctionCoverageSummary &FCS = UniquedSummary.second;
-      Summary.addFunction(FCS);
-      Totals.addFunction(FCS);
+      auto GroupSummary =
+          FunctionCoverageSummary::get(Group, InstantiationSummaries);
+      Summary.addFunction(GroupSummary);
+      Totals.addFunction(GroupSummary);
     }
 
     FileReports.push_back(Summary);
index 21aa7ff73a05628870d80894a0ad8d5c5841056e..9c0027b148c6859e6ee9f6384b94cbd0b8c568aa 100644 (file)
@@ -70,14 +70,31 @@ FunctionCoverageSummary::get(const coverage::FunctionRecord &Function) {
       LineCoverageInfo(CoveredLines, NumLines));
 }
 
-void FunctionCoverageSummary::update(const FunctionCoverageSummary &Summary) {
-  ExecutionCount += Summary.ExecutionCount;
-  RegionCoverage.Covered =
-      std::max(RegionCoverage.Covered, Summary.RegionCoverage.Covered);
-  RegionCoverage.NotCovered =
-      std::min(RegionCoverage.NotCovered, Summary.RegionCoverage.NotCovered);
-  LineCoverage.Covered =
-      std::max(LineCoverage.Covered, Summary.LineCoverage.Covered);
-  LineCoverage.NotCovered =
-      std::min(LineCoverage.NotCovered, Summary.LineCoverage.NotCovered);
+FunctionCoverageSummary
+FunctionCoverageSummary::get(const InstantiationGroup &Group,
+                             ArrayRef<FunctionCoverageSummary> Summaries) {
+  std::string Name;
+  if (Group.hasName()) {
+    Name = Group.getName();
+  } else {
+    llvm::raw_string_ostream OS(Name);
+    OS << "Definition at line " << Group.getLine() << ", column "
+       << Group.getColumn();
+  }
+
+  FunctionCoverageSummary Summary(std::move(Name));
+  Summary.ExecutionCount = Group.getTotalExecutionCount();
+  Summary.RegionCoverage = Summaries[0].RegionCoverage;
+  Summary.LineCoverage = Summaries[0].LineCoverage;
+  for (const auto &FCS : Summaries.drop_front()) {
+    Summary.RegionCoverage.Covered =
+        std::max(FCS.RegionCoverage.Covered, Summary.RegionCoverage.Covered);
+    Summary.RegionCoverage.NotCovered = std::min(
+        FCS.RegionCoverage.NotCovered, Summary.RegionCoverage.NotCovered);
+    Summary.LineCoverage.Covered =
+        std::max(FCS.LineCoverage.Covered, Summary.LineCoverage.Covered);
+    Summary.LineCoverage.NotCovered =
+        std::min(FCS.LineCoverage.NotCovered, Summary.LineCoverage.NotCovered);
+  }
+  return Summary;
 }
index 680fc3757686f6c0c5f16d184c3029a413ce57b4..1603731d9828df7c890f6ff89c50df07284b2833 100644 (file)
@@ -115,7 +115,7 @@ struct FunctionCoverageInfo {
 
 /// \brief A summary of function's code coverage.
 struct FunctionCoverageSummary {
-  StringRef Name;
+  std::string Name;
   uint64_t ExecutionCount;
   RegionCoverageInfo RegionCoverage;
   LineCoverageInfo LineCoverage;
@@ -134,9 +134,11 @@ struct FunctionCoverageSummary {
   static FunctionCoverageSummary
   get(const coverage::FunctionRecord &Function);
 
-  /// \brief Update the summary with information from another instantiation
-  /// of this function.
-  void update(const FunctionCoverageSummary &Summary);
+  /// Compute the code coverage summary for an instantiation group \p Group,
+  /// given a list of summaries for each instantiation in \p Summaries.
+  static FunctionCoverageSummary
+  get(const coverage::InstantiationGroup &Group,
+      ArrayRef<FunctionCoverageSummary> Summaries);
 };
 
 /// \brief A summary of file's code coverage.
index 6588e753eab015623d54bbf44f1586d0cb4726f6..7b3abf79d3370f34b7af9d68928bb090e37e7eee 100644 (file)
@@ -548,9 +548,10 @@ TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
 
   EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded());
 
-  std::vector<const FunctionRecord *> Instantiations =
-      LoadedCoverage->getInstantiations("expanded");
-  ASSERT_TRUE(Instantiations.empty());
+  std::vector<InstantiationGroup> InstantiationGroups =
+      LoadedCoverage->getInstantiationGroups("expanded");
+  for (const auto &Group : InstantiationGroups)
+    ASSERT_EQ(Group.size(), 1U);
 }
 
 TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {