diff options
author | Vedant Kumar <vsk@apple.com> | 2017-08-02 23:35:25 +0000 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2017-08-02 23:35:25 +0000 |
commit | a8dfa81a14129ecbed6ed759421ec5936bb684f3 (patch) | |
tree | b4b1f57bb8f11cb425e410be519dfc2a8513e0d4 /tools/llvm-cov | |
parent | f6b53455dfc6d257276e3f088d18fe7edb27c97d (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
Diffstat (limited to 'tools/llvm-cov')
-rw-r--r-- | tools/llvm-cov/CodeCoverage.cpp | 38 | ||||
-rw-r--r-- | tools/llvm-cov/CoverageReport.cpp | 30 | ||||
-rw-r--r-- | tools/llvm-cov/CoverageSummaryInfo.cpp | 37 | ||||
-rw-r--r-- | tools/llvm-cov/CoverageSummaryInfo.h | 10 |
4 files changed, 67 insertions, 48 deletions
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. |