summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVedant Kumar <vsk@apple.com>2017-08-02 23:35:25 +0000
committerVedant Kumar <vsk@apple.com>2017-08-02 23:35:25 +0000
commita8dfa81a14129ecbed6ed759421ec5936bb684f3 (patch)
treeb4b1f57bb8f11cb425e410be519dfc2a8513e0d4
parentf6b53455dfc6d257276e3f088d18fe7edb27c97d (diff)
[Coverage] Add an API to retrive all instantiations of a function (NFC)
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
-rw-r--r--include/llvm/ProfileData/Coverage/CoverageMapping.h67
-rw-r--r--lib/ProfileData/Coverage/CoverageMapping.cpp14
-rw-r--r--tools/llvm-cov/CodeCoverage.cpp38
-rw-r--r--tools/llvm-cov/CoverageReport.cpp30
-rw-r--r--tools/llvm-cov/CoverageSummaryInfo.cpp37
-rw-r--r--tools/llvm-cov/CoverageSummaryInfo.h10
-rw-r--r--unittests/ProfileData/CoverageMappingTest.cpp7
7 files changed, 140 insertions, 63 deletions
diff --git a/include/llvm/ProfileData/Coverage/CoverageMapping.h b/include/llvm/ProfileData/Coverage/CoverageMapping.h
index 980d47e7afe..74ca814bed9 100644
--- a/include/llvm/ProfileData/Coverage/CoverageMapping.h
+++ b/include/llvm/ProfileData/Coverage/CoverageMapping.h
@@ -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;
diff --git a/lib/ProfileData/Coverage/CoverageMapping.cpp b/lib/ProfileData/Coverage/CoverageMapping.cpp
index c435cb6ac84..346ac76a6f7 100644
--- a/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -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;
}
diff --git a/tools/llvm-cov/CodeCoverage.cpp b/tools/llvm-cov/CodeCoverage.cpp
index c16d7b44ad6..7098b51ce7d 100644
--- a/tools/llvm-cov/CodeCoverage.cpp
+++ b/tools/llvm-cov/CodeCoverage.cpp
@@ -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;
}
diff --git a/tools/llvm-cov/CoverageReport.cpp b/tools/llvm-cov/CoverageReport.cpp
index c68bb9048df..7c273a2430c 100644
--- a/tools/llvm-cov/CoverageReport.cpp
+++ b/tools/llvm-cov/CoverageReport.cpp
@@ -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);
diff --git a/tools/llvm-cov/CoverageSummaryInfo.cpp b/tools/llvm-cov/CoverageSummaryInfo.cpp
index 21aa7ff73a0..9c0027b148c 100644
--- a/tools/llvm-cov/CoverageSummaryInfo.cpp
+++ b/tools/llvm-cov/CoverageSummaryInfo.cpp
@@ -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;
}
diff --git a/tools/llvm-cov/CoverageSummaryInfo.h b/tools/llvm-cov/CoverageSummaryInfo.h
index 680fc375768..1603731d982 100644
--- a/tools/llvm-cov/CoverageSummaryInfo.h
+++ b/tools/llvm-cov/CoverageSummaryInfo.h
@@ -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.
diff --git a/unittests/ProfileData/CoverageMappingTest.cpp b/unittests/ProfileData/CoverageMappingTest.cpp
index 6588e753eab..7b3abf79d33 100644
--- a/unittests/ProfileData/CoverageMappingTest.cpp
+++ b/unittests/ProfileData/CoverageMappingTest.cpp
@@ -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) {