1 //=-- CoverageMapping.h - Code coverage mapping support ---------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Code coverage mapping data is generated by clang and read by
11 // llvm-cov to show code coverage statistics for a file.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_PROFILEDATA_COVERAGEMAPPING_H_
16 #define LLVM_PROFILEDATA_COVERAGEMAPPING_H_
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <system_error>
25 struct CounterExpressions;
27 enum CoverageMappingVersion { CoverageMappingVersion1 };
29 /// \brief A Counter is an abstract value that describes how to compute the
30 /// execution count for a region of code using the collected profile count data.
32 enum CounterKind { Zero, CounterValueReference, Expression };
33 static const unsigned EncodingTagBits = 2;
34 static const unsigned EncodingTagMask = 0x3;
35 static const unsigned EncodingCounterTagAndExpansionRegionTagBits =
42 Counter(CounterKind Kind, unsigned ID) : Kind(Kind), ID(ID) {}
45 Counter() : Kind(Zero), ID(0) {}
47 CounterKind getKind() const { return Kind; }
49 bool isZero() const { return Kind == Zero; }
51 bool isExpression() const { return Kind == Expression; }
53 unsigned getCounterID() const { return ID; }
55 unsigned getExpressionID() const { return ID; }
57 bool operator==(const Counter &Other) const {
58 return Kind == Other.Kind && ID == Other.ID;
61 /// \brief Return the counter that represents the number zero.
62 static Counter getZero() { return Counter(); }
64 /// \brief Return the counter that corresponds to a specific profile counter.
65 static Counter getCounter(unsigned CounterId) {
66 return Counter(CounterValueReference, CounterId);
69 /// \brief Return the counter that corresponds to a specific
70 /// addition counter expression.
71 static Counter getExpression(unsigned ExpressionId) {
72 return Counter(Expression, ExpressionId);
76 /// \brief A Counter expression is a value that represents an arithmetic
77 /// operation with two counters.
78 struct CounterExpression {
79 enum ExprKind { Subtract, Add };
83 CounterExpression(ExprKind Kind, Counter LHS, Counter RHS)
84 : Kind(Kind), LHS(LHS), RHS(RHS) {}
86 bool operator==(const CounterExpression &Other) const {
87 return Kind == Other.Kind && LHS == Other.LHS && RHS == Other.RHS;
91 /// \brief A Counter expression builder is used to construct the
92 /// counter expressions. It avoids unecessary duplication
93 /// and simplifies algebraic expressions.
94 class CounterExpressionBuilder {
95 /// \brief A list of all the counter expressions
96 llvm::SmallVector<CounterExpression, 16> Expressions;
97 /// \brief An array of terms used in expression simplification.
98 llvm::SmallVector<int, 16> Terms;
100 /// \brief Return the counter which corresponds to the given expression.
102 /// If the given expression is already stored in the builder, a counter
103 /// that references that expression is returned. Otherwise, the given
104 /// expression is added to the builder's collection of expressions.
105 Counter get(const CounterExpression &E);
107 /// \brief Convert the expression tree represented by a counter
108 /// into a polynomial in the form of K1Counter1 + .. + KNCounterN
109 /// where K1 .. KN are integer constants that are stored in the Terms array.
110 void extractTerms(Counter C, int Sign = 1);
112 /// \brief Simplifies the given expression tree
113 /// by getting rid of algebraically redundant operations.
114 Counter simplify(Counter ExpressionTree);
117 CounterExpressionBuilder(unsigned NumCounterValues);
119 ArrayRef<CounterExpression> getExpressions() const { return Expressions; }
121 /// \brief Return a counter that represents the expression
122 /// that adds LHS and RHS.
123 Counter add(Counter LHS, Counter RHS);
125 /// \brief Return a counter that represents the expression
126 /// that subtracts RHS from LHS.
127 Counter subtract(Counter LHS, Counter RHS);
130 /// \brief A Counter mapping region associates a source range with
131 /// a specific counter.
132 struct CounterMappingRegion {
134 /// \brief A CodeRegion associates some code with a counter
137 /// \brief An ExpansionRegion represents a file expansion region that
138 /// associates a source range with the expansion of a virtual source file,
139 /// such as for a macro instantiation or #include file.
142 /// \brief A SkippedRegion represents a source range with code that
143 /// was skipped by a preprocessor or similar means.
148 unsigned FileID, ExpandedFileID;
149 unsigned LineStart, ColumnStart, LineEnd, ColumnEnd;
152 CounterMappingRegion(Counter Count, unsigned FileID, unsigned LineStart,
153 unsigned ColumnStart, unsigned LineEnd,
154 unsigned ColumnEnd, RegionKind Kind = CodeRegion)
155 : Count(Count), FileID(FileID), ExpandedFileID(0), LineStart(LineStart),
156 ColumnStart(ColumnStart), LineEnd(LineEnd), ColumnEnd(ColumnEnd),
159 bool operator<(const CounterMappingRegion &Other) const {
160 if (FileID != Other.FileID)
161 return FileID < Other.FileID;
162 if (LineStart == Other.LineStart)
163 return ColumnStart < Other.ColumnStart;
164 return LineStart < Other.LineStart;
168 /// \brief A Counter mapping context is used to connect the counters,
169 /// expressions and the obtained counter values.
170 class CounterMappingContext {
171 ArrayRef<CounterExpression> Expressions;
172 ArrayRef<uint64_t> CounterValues;
175 CounterMappingContext(ArrayRef<CounterExpression> Expressions,
176 ArrayRef<uint64_t> CounterValues = ArrayRef<uint64_t>())
177 : Expressions(Expressions), CounterValues(CounterValues) {}
179 void dump(const Counter &C, llvm::raw_ostream &OS) const;
180 void dump(const Counter &C) const { dump(C, llvm::outs()); }
182 /// \brief Return the number of times that a region of code
183 /// associated with this counter was executed.
184 int64_t evaluate(const Counter &C, std::error_code *Error) const;
185 int64_t evaluate(const Counter &C, std::error_code &Error) const {
187 return evaluate(C, &Error);
191 } // end namespace coverage
192 } // end namespace llvm
194 #endif // LLVM_PROFILEDATA_COVERAGEMAPPING_H_