LCOV - code coverage report
Current view: top level - include/llvm/ProfileData/Coverage - CoverageMapping.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 93 99 93.9 %
Date: 2017-09-14 15:23:50 Functions: 17 21 81.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- CoverageMapping.h - Code coverage mapping support --------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Code coverage mapping data is generated by clang and read by
      11             : // llvm-cov to show code coverage statistics for a file.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #ifndef LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H
      16             : #define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H
      17             : 
      18             : #include "llvm/ADT/ArrayRef.h"
      19             : #include "llvm/ADT/DenseMap.h"
      20             : #include "llvm/ADT/Hashing.h"
      21             : #include "llvm/ADT/None.h"
      22             : #include "llvm/ADT/StringRef.h"
      23             : #include "llvm/ADT/StringSet.h"
      24             : #include "llvm/ADT/iterator.h"
      25             : #include "llvm/ADT/iterator_range.h"
      26             : #include "llvm/ProfileData/InstrProf.h"
      27             : #include "llvm/Support/Compiler.h"
      28             : #include "llvm/Support/Debug.h"
      29             : #include "llvm/Support/Endian.h"
      30             : #include "llvm/Support/Error.h"
      31             : #include "llvm/Support/raw_ostream.h"
      32             : #include <cassert>
      33             : #include <cstdint>
      34             : #include <iterator>
      35             : #include <memory>
      36             : #include <string>
      37             : #include <system_error>
      38             : #include <tuple>
      39             : #include <utility>
      40             : #include <vector>
      41             : 
      42             : namespace llvm {
      43             : 
      44             : class IndexedInstrProfReader;
      45             : 
      46             : namespace coverage {
      47             : 
      48             : class CoverageMappingReader;
      49             : struct CoverageMappingRecord;
      50             : 
      51             : enum class coveragemap_error {
      52             :   success = 0,
      53             :   eof,
      54             :   no_data_found,
      55             :   unsupported_version,
      56             :   truncated,
      57             :   malformed
      58             : };
      59             : 
      60             : const std::error_category &coveragemap_category();
      61             : 
      62             : inline std::error_code make_error_code(coveragemap_error E) {
      63           0 :   return std::error_code(static_cast<int>(E), coveragemap_category());
      64             : }
      65             : 
      66         362 : class CoverageMapError : public ErrorInfo<CoverageMapError> {
      67             : public:
      68         362 :   CoverageMapError(coveragemap_error Err) : Err(Err) {
      69             :     assert(Err != coveragemap_error::success && "Not an error");
      70             :   }
      71             : 
      72             :   std::string message() const override;
      73             : 
      74           0 :   void log(raw_ostream &OS) const override { OS << message(); }
      75             : 
      76           0 :   std::error_code convertToErrorCode() const override {
      77           0 :     return make_error_code(Err);
      78             :   }
      79             : 
      80             :   coveragemap_error get() const { return Err; }
      81             : 
      82             :   static char ID;
      83             : 
      84             : private:
      85             :   coveragemap_error Err;
      86             : };
      87             : 
      88             : /// A Counter is an abstract value that describes how to compute the
      89             : /// execution count for a region of code using the collected profile count data.
      90             : struct Counter {
      91             :   enum CounterKind { Zero, CounterValueReference, Expression };
      92             :   static const unsigned EncodingTagBits = 2;
      93             :   static const unsigned EncodingTagMask = 0x3;
      94             :   static const unsigned EncodingCounterTagAndExpansionRegionTagBits =
      95             :       EncodingTagBits + 1;
      96             : 
      97             : private:
      98             :   CounterKind Kind = Zero;
      99             :   unsigned ID = 0;
     100             : 
     101             :   Counter(CounterKind Kind, unsigned ID) : Kind(Kind), ID(ID) {}
     102             : 
     103             : public:
     104             :   Counter() = default;
     105             : 
     106             :   CounterKind getKind() const { return Kind; }
     107             : 
     108             :   bool isZero() const { return Kind == Zero; }
     109             : 
     110             :   bool isExpression() const { return Kind == Expression; }
     111             : 
     112             :   unsigned getCounterID() const { return ID; }
     113             : 
     114             :   unsigned getExpressionID() const { return ID; }
     115             : 
     116             :   friend bool operator==(const Counter &LHS, const Counter &RHS) {
     117        5618 :     return LHS.Kind == RHS.Kind && LHS.ID == RHS.ID;
     118             :   }
     119             : 
     120             :   friend bool operator!=(const Counter &LHS, const Counter &RHS) {
     121          58 :     return !(LHS == RHS);
     122             :   }
     123             : 
     124             :   friend bool operator<(const Counter &LHS, const Counter &RHS) {
     125             :     return std::tie(LHS.Kind, LHS.ID) < std::tie(RHS.Kind, RHS.ID);
     126             :   }
     127             : 
     128             :   /// Return the counter that represents the number zero.
     129         253 :   static Counter getZero() { return Counter(); }
     130             : 
     131             :   /// Return the counter that corresponds to a specific profile counter.
     132             :   static Counter getCounter(unsigned CounterId) {
     133       10737 :     return Counter(CounterValueReference, CounterId);
     134             :   }
     135             : 
     136             :   /// Return the counter that corresponds to a specific addition counter
     137             :   /// expression.
     138             :   static Counter getExpression(unsigned ExpressionId) {
     139        1785 :     return Counter(Expression, ExpressionId);
     140             :   }
     141             : };
     142             : 
     143             : /// A Counter expression is a value that represents an arithmetic operation
     144             : /// with two counters.
     145             : struct CounterExpression {
     146             :   enum ExprKind { Subtract, Add };
     147             :   ExprKind Kind;
     148             :   Counter LHS, RHS;
     149             : 
     150             :   CounterExpression(ExprKind Kind, Counter LHS, Counter RHS)
     151        5234 :       : Kind(Kind), LHS(LHS), RHS(RHS) {}
     152             : };
     153             : 
     154             : /// A Counter expression builder is used to construct the counter expressions.
     155             : /// It avoids unnecessary duplication and simplifies algebraic expressions.
     156         816 : class CounterExpressionBuilder {
     157             :   /// A list of all the counter expressions
     158             :   std::vector<CounterExpression> Expressions;
     159             : 
     160             :   /// A lookup table for the index of a given expression.
     161             :   DenseMap<CounterExpression, unsigned> ExpressionIndices;
     162             : 
     163             :   /// Return the counter which corresponds to the given expression.
     164             :   ///
     165             :   /// If the given expression is already stored in the builder, a counter
     166             :   /// that references that expression is returned. Otherwise, the given
     167             :   /// expression is added to the builder's collection of expressions.
     168             :   Counter get(const CounterExpression &E);
     169             : 
     170             :   /// Represents a term in a counter expression tree.
     171             :   struct Term {
     172             :     unsigned CounterID;
     173             :     int Factor;
     174             : 
     175             :     Term(unsigned CounterID, int Factor)
     176        1277 :         : CounterID(CounterID), Factor(Factor) {}
     177             :   };
     178             : 
     179             :   /// Gather the terms of the expression tree for processing.
     180             :   ///
     181             :   /// This collects each addition and subtraction referenced by the counter into
     182             :   /// a sequence that can be sorted and combined to build a simplified counter
     183             :   /// expression.
     184             :   void extractTerms(Counter C, int Sign, SmallVectorImpl<Term> &Terms);
     185             : 
     186             :   /// Simplifies the given expression tree
     187             :   /// by getting rid of algebraically redundant operations.
     188             :   Counter simplify(Counter ExpressionTree);
     189             : 
     190             : public:
     191         270 :   ArrayRef<CounterExpression> getExpressions() const { return Expressions; }
     192             : 
     193             :   /// Return a counter that represents the expression that adds LHS and RHS.
     194             :   Counter add(Counter LHS, Counter RHS);
     195             : 
     196             :   /// Return a counter that represents the expression that subtracts RHS from
     197             :   /// LHS.
     198             :   Counter subtract(Counter LHS, Counter RHS);
     199             : };
     200             : 
     201             : using LineColPair = std::pair<unsigned, unsigned>;
     202             : 
     203             : /// A Counter mapping region associates a source range with a specific counter.
     204             : struct CounterMappingRegion {
     205             :   enum RegionKind {
     206             :     /// A CodeRegion associates some code with a counter
     207             :     CodeRegion,
     208             : 
     209             :     /// An ExpansionRegion represents a file expansion region that associates 
     210             :     /// a source range with the expansion of a virtual source file, such as
     211             :     /// for a macro instantiation or #include file.
     212             :     ExpansionRegion,
     213             : 
     214             :     /// A SkippedRegion represents a source range with code that was skipped
     215             :     /// by a preprocessor or similar means.
     216             :     SkippedRegion
     217             :   };
     218             : 
     219             :   Counter Count;
     220             :   unsigned FileID, ExpandedFileID;
     221             :   unsigned LineStart, ColumnStart, LineEnd, ColumnEnd;
     222             :   RegionKind Kind;
     223             : 
     224             :   CounterMappingRegion(Counter Count, unsigned FileID, unsigned ExpandedFileID,
     225             :                        unsigned LineStart, unsigned ColumnStart,
     226             :                        unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind)
     227        3328 :       : Count(Count), FileID(FileID), ExpandedFileID(ExpandedFileID),
     228             :         LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd),
     229        3328 :         ColumnEnd(ColumnEnd), Kind(Kind) {}
     230             : 
     231             :   static CounterMappingRegion
     232             :   makeRegion(Counter Count, unsigned FileID, unsigned LineStart,
     233             :              unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
     234             :     return CounterMappingRegion(Count, FileID, 0, LineStart, ColumnStart,
     235        1152 :                                 LineEnd, ColumnEnd, CodeRegion);
     236             :   }
     237             : 
     238             :   static CounterMappingRegion
     239             :   makeExpansion(unsigned FileID, unsigned ExpandedFileID, unsigned LineStart,
     240             :                 unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
     241             :     return CounterMappingRegion(Counter(), FileID, ExpandedFileID, LineStart,
     242             :                                 ColumnStart, LineEnd, ColumnEnd,
     243         326 :                                 ExpansionRegion);
     244             :   }
     245             : 
     246             :   static CounterMappingRegion
     247             :   makeSkipped(unsigned FileID, unsigned LineStart, unsigned ColumnStart,
     248             :               unsigned LineEnd, unsigned ColumnEnd) {
     249             :     return CounterMappingRegion(Counter(), FileID, 0, LineStart, ColumnStart,
     250          34 :                                 LineEnd, ColumnEnd, SkippedRegion);
     251             :   }
     252             : 
     253             :   inline LineColPair startLoc() const {
     254       30185 :     return LineColPair(LineStart, ColumnStart);
     255             :   }
     256             : 
     257        9639 :   inline LineColPair endLoc() const { return LineColPair(LineEnd, ColumnEnd); }
     258             : };
     259             : 
     260             : /// Associates a source range with an execution count.
     261             : struct CountedRegion : public CounterMappingRegion {
     262             :   uint64_t ExecutionCount;
     263             : 
     264             :   CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount)
     265         959 :       : CounterMappingRegion(R), ExecutionCount(ExecutionCount) {}
     266             : };
     267             : 
     268             : /// A Counter mapping context is used to connect the counters, expressions
     269             : /// and the obtained counter values.
     270             : class CounterMappingContext {
     271             :   ArrayRef<CounterExpression> Expressions;
     272             :   ArrayRef<uint64_t> CounterValues;
     273             : 
     274             : public:
     275             :   CounterMappingContext(ArrayRef<CounterExpression> Expressions,
     276             :                         ArrayRef<uint64_t> CounterValues = None)
     277         413 :       : Expressions(Expressions), CounterValues(CounterValues) {}
     278             : 
     279         283 :   void setCounts(ArrayRef<uint64_t> Counts) { CounterValues = Counts; }
     280             : 
     281             :   void dump(const Counter &C, raw_ostream &OS) const;
     282             :   void dump(const Counter &C) const { dump(C, dbgs()); }
     283             : 
     284             :   /// Return the number of times that a region of code associated with this
     285             :   /// counter was executed.
     286             :   Expected<int64_t> evaluate(const Counter &C) const;
     287             : };
     288             : 
     289             : /// Code coverage information for a single function.
     290        2157 : struct FunctionRecord {
     291             :   /// Raw function name.
     292             :   std::string Name;
     293             :   /// Associated files.
     294             :   std::vector<std::string> Filenames;
     295             :   /// Regions in the function along with their counts.
     296             :   std::vector<CountedRegion> CountedRegions;
     297             :   /// The number of times this function was executed.
     298             :   uint64_t ExecutionCount;
     299             : 
     300         283 :   FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames)
     301        1981 :       : Name(Name), Filenames(Filenames.begin(), Filenames.end()) {}
     302             : 
     303        1744 :   FunctionRecord(FunctionRecord &&FR) = default;
     304             :   FunctionRecord &operator=(FunctionRecord &&) = default;
     305             : 
     306             :   void pushRegion(CounterMappingRegion Region, uint64_t Count) {
     307        1918 :     if (CountedRegions.empty())
     308         283 :       ExecutionCount = Count;
     309         959 :     CountedRegions.emplace_back(Region, Count);
     310             :   }
     311             : };
     312             : 
     313             : /// Iterator over Functions, optionally filtered to a single file.
     314             : class FunctionRecordIterator
     315             :     : public iterator_facade_base<FunctionRecordIterator,
     316             :                                   std::forward_iterator_tag, FunctionRecord> {
     317             :   ArrayRef<FunctionRecord> Records;
     318             :   ArrayRef<FunctionRecord>::iterator Current;
     319             :   StringRef Filename;
     320             : 
     321             :   /// Skip records whose primary file is not \c Filename.
     322             :   void skipOtherFiles();
     323             : 
     324             : public:
     325             :   FunctionRecordIterator(ArrayRef<FunctionRecord> Records_,
     326             :                          StringRef Filename = "")
     327         225 :       : Records(Records_), Current(Records.begin()), Filename(Filename) {
     328         225 :     skipOtherFiles();
     329             :   }
     330             : 
     331         425 :   FunctionRecordIterator() : Current(Records.begin()) {}
     332             : 
     333         672 :   bool operator==(const FunctionRecordIterator &RHS) const {
     334        1344 :     return Current == RHS.Current && Filename == RHS.Filename;
     335             :   }
     336             : 
     337             :   const FunctionRecord &operator*() const { return *Current; }
     338             : 
     339             :   FunctionRecordIterator &operator++() {
     340             :     assert(Current != Records.end() && "incremented past end");
     341         472 :     ++Current;
     342         472 :     skipOtherFiles();
     343             :     return *this;
     344             :   }
     345             : };
     346             : 
     347             : /// Coverage information for a macro expansion or #included file.
     348             : ///
     349             : /// When covered code has pieces that can be expanded for more detail, such as a
     350             : /// preprocessor macro use and its definition, these are represented as
     351             : /// expansions whose coverage can be looked up independently.
     352             : struct ExpansionRecord {
     353             :   /// The abstract file this expansion covers.
     354             :   unsigned FileID;
     355             :   /// The region that expands to this record.
     356             :   const CountedRegion &Region;
     357             :   /// Coverage for the expansion.
     358             :   const FunctionRecord &Function;
     359             : 
     360             :   ExpansionRecord(const CountedRegion &Region,
     361             :                   const FunctionRecord &Function)
     362          20 :       : FileID(Region.ExpandedFileID), Region(Region), Function(Function) {}
     363             : };
     364             : 
     365             : /// The execution count information starting at a point in a file.
     366             : ///
     367             : /// A sequence of CoverageSegments gives execution counts for a file in format
     368             : /// that's simple to iterate through for processing.
     369             : struct CoverageSegment {
     370             :   /// The line where this segment begins.
     371             :   unsigned Line;
     372             :   /// The column where this segment begins.
     373             :   unsigned Col;
     374             :   /// The execution count, or zero if no count was recorded.
     375             :   uint64_t Count;
     376             :   /// When false, the segment was uninstrumented or skipped.
     377             :   bool HasCount;
     378             :   /// Whether this enters a new region or returns to a previous count.
     379             :   bool IsRegionEntry;
     380             : 
     381             :   CoverageSegment(unsigned Line, unsigned Col, bool IsRegionEntry)
     382         300 :       : Line(Line), Col(Col), Count(0), HasCount(false),
     383         300 :         IsRegionEntry(IsRegionEntry) {}
     384             : 
     385             :   CoverageSegment(unsigned Line, unsigned Col, uint64_t Count,
     386             :                   bool IsRegionEntry)
     387        1308 :       : Line(Line), Col(Col), Count(Count), HasCount(true),
     388        1308 :         IsRegionEntry(IsRegionEntry) {}
     389             : 
     390         304 :   friend bool operator==(const CoverageSegment &L, const CoverageSegment &R) {
     391         304 :     return std::tie(L.Line, L.Col, L.Count, L.HasCount, L.IsRegionEntry) ==
     392        1520 :            std::tie(R.Line, R.Col, R.Count, R.HasCount, R.IsRegionEntry);
     393             :   }
     394             : };
     395             : 
     396             : /// An instantiation group contains a \c FunctionRecord list, such that each
     397             : /// record corresponds to a distinct instantiation of the same function.
     398             : ///
     399             : /// Note that it's possible for a function to have more than one instantiation
     400             : /// (consider C++ template specializations or static inline functions).
     401         578 : class InstantiationGroup {
     402             :   friend class CoverageMapping;
     403             : 
     404             :   unsigned Line;
     405             :   unsigned Col;
     406             :   std::vector<const FunctionRecord *> Instantiations;
     407             : 
     408             :   InstantiationGroup(unsigned Line, unsigned Col,
     409             :                      std::vector<const FunctionRecord *> Instantiations)
     410         246 :       : Line(Line), Col(Col), Instantiations(std::move(Instantiations)) {}
     411             : 
     412             : public:
     413             :   InstantiationGroup(const InstantiationGroup &) = delete;
     414         332 :   InstantiationGroup(InstantiationGroup &&) = default;
     415             : 
     416             :   /// Get the number of instantiations in this group.
     417          63 :   size_t size() const { return Instantiations.size(); }
     418             : 
     419             :   /// Get the line where the common function was defined.
     420             :   unsigned getLine() const { return Line; }
     421             : 
     422             :   /// Get the column where the common function was defined.
     423             :   unsigned getColumn() const { return Col; }
     424             : 
     425             :   /// Check if the instantiations in this group have a common mangled name.
     426          62 :   bool hasName() const {
     427         124 :     for (unsigned I = 1, E = Instantiations.size(); I < E; ++I)
     428          15 :       if (Instantiations[I]->Name != Instantiations[0]->Name)
     429             :         return false;
     430             :     return true;
     431             :   }
     432             : 
     433             :   /// Get the common mangled name for instantiations in this group.
     434             :   StringRef getName() const {
     435             :     assert(hasName() && "Instantiations don't have a shared name");
     436         114 :     return Instantiations[0]->Name;
     437             :   }
     438             : 
     439             :   /// Get the total execution count of all instantiations in this group.
     440             :   uint64_t getTotalExecutionCount() const {
     441          62 :     uint64_t Count = 0;
     442         315 :     for (const FunctionRecord *F : Instantiations)
     443          67 :       Count += F->ExecutionCount;
     444             :     return Count;
     445             :   }
     446             : 
     447             :   /// Get the instantiations in this group.
     448             :   ArrayRef<const FunctionRecord *> getInstantiations() const {
     449         144 :     return Instantiations;
     450             :   }
     451             : };
     452             : 
     453             : /// Coverage information to be processed or displayed.
     454             : ///
     455             : /// This represents the coverage of an entire file, expansion, or function. It
     456             : /// provides a sequence of CoverageSegments to iterate through, as well as the
     457             : /// list of expansions that can be further processed.
     458        1840 : class CoverageData {
     459             :   friend class CoverageMapping;
     460             : 
     461             :   std::string Filename;
     462             :   std::vector<CoverageSegment> Segments;
     463             :   std::vector<ExpansionRecord> Expansions;
     464             : 
     465             : public:
     466           0 :   CoverageData() = default;
     467             : 
     468         672 :   CoverageData(StringRef Filename) : Filename(Filename) {}
     469             : 
     470             :   /// Get the name of the file this data covers.
     471          64 :   StringRef getFilename() const { return Filename; }
     472             : 
     473             :   /// Get an iterator over the coverage segments for this object. The segments
     474             :   /// are guaranteed to be uniqued and sorted by location.
     475             :   std::vector<CoverageSegment>::const_iterator begin() const {
     476         223 :     return Segments.begin();
     477             :   }
     478             : 
     479             :   std::vector<CoverageSegment>::const_iterator end() const {
     480         211 :     return Segments.end();
     481             :   }
     482             : 
     483         152 :   bool empty() const { return Segments.empty(); }
     484             : 
     485             :   /// Expansions that can be further processed.
     486         162 :   ArrayRef<ExpansionRecord> getExpansions() const { return Expansions; }
     487             : };
     488             : 
     489             : /// The mapping of profile information to coverage data.
     490             : ///
     491             : /// This is the main interface to get coverage information, using a profile to
     492             : /// fill out execution counts.
     493         344 : class CoverageMapping {
     494             :   StringSet<> FunctionNames;
     495             :   std::vector<FunctionRecord> Functions;
     496             :   unsigned MismatchedFunctionCount = 0;
     497             : 
     498         516 :   CoverageMapping() = default;
     499             : 
     500             :   /// Add a function record corresponding to \p Record.
     501             :   Error loadFunctionRecord(const CoverageMappingRecord &Record,
     502             :                            IndexedInstrProfReader &ProfileReader);
     503             : 
     504             : public:
     505             :   CoverageMapping(const CoverageMapping &) = delete;
     506             :   CoverageMapping &operator=(const CoverageMapping &) = delete;
     507             : 
     508             :   /// Load the coverage mapping using the given readers.
     509             :   static Expected<std::unique_ptr<CoverageMapping>>
     510             :   load(ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
     511             :        IndexedInstrProfReader &ProfileReader);
     512             : 
     513             :   /// Load the coverage mapping from the given object files and profile. If
     514             :   /// \p Arches is non-empty, it must specify an architecture for each object.
     515             :   static Expected<std::unique_ptr<CoverageMapping>>
     516             :   load(ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
     517             :        ArrayRef<StringRef> Arches = None);
     518             : 
     519             :   /// The number of functions that couldn't have their profiles mapped.
     520             :   ///
     521             :   /// This is a count of functions whose profile is out of date or otherwise
     522             :   /// can't be associated with any coverage information.
     523             :   unsigned getMismatchedCount() { return MismatchedFunctionCount; }
     524             : 
     525             :   /// Returns a lexicographically sorted, unique list of files that are
     526             :   /// covered.
     527             :   std::vector<StringRef> getUniqueSourceFiles() const;
     528             : 
     529             :   /// Get the coverage for a particular file.
     530             :   ///
     531             :   /// The given filename must be the name as recorded in the coverage
     532             :   /// information. That is, only names returned from getUniqueSourceFiles will
     533             :   /// yield a result.
     534             :   CoverageData getCoverageForFile(StringRef Filename) const;
     535             : 
     536             :   /// Get the coverage for a particular function.
     537             :   CoverageData getCoverageForFunction(const FunctionRecord &Function) const;
     538             : 
     539             :   /// Get the coverage for an expansion within a coverage set.
     540             :   CoverageData getCoverageForExpansion(const ExpansionRecord &Expansion) const;
     541             : 
     542             :   /// Gets all of the functions covered by this profile.
     543         222 :   iterator_range<FunctionRecordIterator> getCoveredFunctions() const {
     544             :     return make_range(FunctionRecordIterator(Functions),
     545         888 :                       FunctionRecordIterator());
     546             :   }
     547             : 
     548             :   /// Gets all of the functions in a particular file.
     549             :   iterator_range<FunctionRecordIterator>
     550           3 :   getCoveredFunctions(StringRef Filename) const {
     551             :     return make_range(FunctionRecordIterator(Functions, Filename),
     552          12 :                       FunctionRecordIterator());
     553             :   }
     554             : 
     555             :   /// Get the list of function instantiation groups in a particular file.
     556             :   ///
     557             :   /// Every instantiation group in a program is attributed to exactly one file:
     558             :   /// the file in which the definition for the common function begins.
     559             :   std::vector<InstantiationGroup>
     560             :   getInstantiationGroups(StringRef Filename) const;
     561             : };
     562             : 
     563             : // Profile coverage map has the following layout:
     564             : // [CoverageMapFileHeader]
     565             : // [ArrayStart]
     566             : //  [CovMapFunctionRecord]
     567             : //  [CovMapFunctionRecord]
     568             : //  ...
     569             : // [ArrayEnd]
     570             : // [Encoded Region Mapping Data]
     571             : LLVM_PACKED_START
     572             : template <class IntPtrT> struct CovMapFunctionRecordV1 {
     573             : #define COVMAP_V1
     574             : #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
     575             : #include "llvm/ProfileData/InstrProfData.inc"
     576             : #undef COVMAP_V1
     577             : 
     578             :   // Return the structural hash associated with the function.
     579             :   template <support::endianness Endian> uint64_t getFuncHash() const {
     580         256 :     return support::endian::byte_swap<uint64_t, Endian>(FuncHash);
     581             :   }
     582             : 
     583             :   // Return the coverage map data size for the funciton.
     584             :   template <support::endianness Endian> uint32_t getDataSize() const {
     585         256 :     return support::endian::byte_swap<uint32_t, Endian>(DataSize);
     586             :   }
     587             : 
     588             :   // Return function lookup key. The value is consider opaque.
     589             :   template <support::endianness Endian> IntPtrT getFuncNameRef() const {
     590         508 :     return support::endian::byte_swap<IntPtrT, Endian>(NamePtr);
     591             :   }
     592             : 
     593             :   // Return the PGO name of the function */
     594             :   template <support::endianness Endian>
     595         126 :   Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
     596         126 :     IntPtrT NameRef = getFuncNameRef<Endian>();
     597         252 :     uint32_t NameS = support::endian::byte_swap<uint32_t, Endian>(NameSize);
     598         126 :     FuncName = ProfileNames.getFuncName(NameRef, NameS);
     599         252 :     if (NameS && FuncName.empty())
     600           0 :       return make_error<CoverageMapError>(coveragemap_error::malformed);
     601         378 :     return Error::success();
     602             :   }
     603             : };
     604             : 
     605             : struct CovMapFunctionRecord {
     606             : #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
     607             : #include "llvm/ProfileData/InstrProfData.inc"
     608             : 
     609             :   // Return the structural hash associated with the function.
     610             :   template <support::endianness Endian> uint64_t getFuncHash() const {
     611         134 :     return support::endian::byte_swap<uint64_t, Endian>(FuncHash);
     612             :   }
     613             : 
     614             :   // Return the coverage map data size for the funciton.
     615             :   template <support::endianness Endian> uint32_t getDataSize() const {
     616         134 :     return support::endian::byte_swap<uint32_t, Endian>(DataSize);
     617             :   }
     618             : 
     619             :   // Return function lookup key. The value is consider opaque.
     620             :   template <support::endianness Endian> uint64_t getFuncNameRef() const {
     621         264 :     return support::endian::byte_swap<uint64_t, Endian>(NameRef);
     622             :   }
     623             : 
     624             :   // Return the PGO name of the function */
     625             :   template <support::endianness Endian>
     626             :   Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
     627          65 :     uint64_t NameRef = getFuncNameRef<Endian>();
     628          65 :     FuncName = ProfileNames.getFuncName(NameRef);
     629         195 :     return Error::success();
     630             :   }
     631             : };
     632             : 
     633             : // Per module coverage mapping data header, i.e. CoverageMapFileHeader
     634             : // documented above.
     635             : struct CovMapHeader {
     636             : #define COVMAP_HEADER(Type, LLVMType, Name, Init) Type Name;
     637             : #include "llvm/ProfileData/InstrProfData.inc"
     638             :   template <support::endianness Endian> uint32_t getNRecords() const {
     639         196 :     return support::endian::byte_swap<uint32_t, Endian>(NRecords);
     640             :   }
     641             : 
     642             :   template <support::endianness Endian> uint32_t getFilenamesSize() const {
     643         196 :     return support::endian::byte_swap<uint32_t, Endian>(FilenamesSize);
     644             :   }
     645             : 
     646             :   template <support::endianness Endian> uint32_t getCoverageSize() const {
     647         196 :     return support::endian::byte_swap<uint32_t, Endian>(CoverageSize);
     648             :   }
     649             : 
     650             :   template <support::endianness Endian> uint32_t getVersion() const {
     651         164 :     return support::endian::byte_swap<uint32_t, Endian>(Version);
     652             :   }
     653             : };
     654             : 
     655             : LLVM_PACKED_END
     656             : 
     657             : enum CovMapVersion {
     658             :   Version1 = 0,
     659             :   // Function's name reference from CovMapFuncRecord is changed from raw
     660             :   // name string pointer to MD5 to support name section compression. Name
     661             :   // section is also compressed.
     662             :   Version2 = 1,
     663             :   // The current version is Version2
     664             :   CurrentVersion = INSTR_PROF_COVMAP_VERSION
     665             : };
     666             : 
     667             : template <int CovMapVersion, class IntPtrT> struct CovMapTraits {
     668             :   using CovMapFuncRecordType = CovMapFunctionRecord;
     669             :   using NameRefType = uint64_t;
     670             : };
     671             : 
     672             : template <class IntPtrT> struct CovMapTraits<CovMapVersion::Version1, IntPtrT> {
     673             :   using CovMapFuncRecordType = CovMapFunctionRecordV1<IntPtrT>;
     674             :   using NameRefType = IntPtrT;
     675             : };
     676             : 
     677             : } // end namespace coverage
     678             : 
     679             : /// Provide DenseMapInfo for CounterExpression
     680             : template<> struct DenseMapInfo<coverage::CounterExpression> {
     681             :   static inline coverage::CounterExpression getEmptyKey() {
     682             :     using namespace coverage;
     683             : 
     684             :     return CounterExpression(CounterExpression::ExprKind::Subtract,
     685             :                              Counter::getCounter(~0U),
     686        6591 :                              Counter::getCounter(~0U));
     687             :   }
     688             : 
     689             :   static inline coverage::CounterExpression getTombstoneKey() {
     690             :     using namespace coverage;
     691             : 
     692             :     return CounterExpression(CounterExpression::ExprKind::Add,
     693             :                              Counter::getCounter(~0U),
     694        4728 :                              Counter::getCounter(~0U));
     695             :   }
     696             : 
     697        1575 :   static unsigned getHashValue(const coverage::CounterExpression &V) {
     698             :     return static_cast<unsigned>(
     699        4725 :         hash_combine(V.Kind, V.LHS.getKind(), V.LHS.getCounterID(),
     700        6300 :                      V.RHS.getKind(), V.RHS.getCounterID()));
     701             :   }
     702             : 
     703             :   static bool isEqual(const coverage::CounterExpression &LHS,
     704             :                       const coverage::CounterExpression &RHS) {
     705        6853 :     return LHS.Kind == RHS.Kind && LHS.LHS == RHS.LHS && LHS.RHS == RHS.RHS;
     706             :   }
     707             : };
     708             : 
     709             : } // end namespace llvm
     710             : 
     711             : #endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H

Generated by: LCOV version 1.13