@@ -85,3 +85,197 @@ impl CounterExpression {
8585 Self { kind, lhs, rhs }
8686 }
8787}
88+
89+ /// Corresponds to enum `llvm::coverage::CounterMappingRegion::RegionKind`.
90+ ///
91+ /// Must match the layout of `LLVMRustCounterMappingRegionKind`.
92+ #[ derive( Copy , Clone , Debug ) ]
93+ #[ repr( C ) ]
94+ pub enum RegionKind {
95+ /// A CodeRegion associates some code with a counter
96+ CodeRegion = 0 ,
97+
98+ /// An ExpansionRegion represents a file expansion region that associates
99+ /// a source range with the expansion of a virtual source file, such as
100+ /// for a macro instantiation or #include file.
101+ ExpansionRegion = 1 ,
102+
103+ /// A SkippedRegion represents a source range with code that was skipped
104+ /// by a preprocessor or similar means.
105+ SkippedRegion = 2 ,
106+
107+ /// A GapRegion is like a CodeRegion, but its count is only set as the
108+ /// line execution count when its the only region in the line.
109+ GapRegion = 3 ,
110+
111+ /// A BranchRegion represents leaf-level boolean expressions and is
112+ /// associated with two counters, each representing the number of times the
113+ /// expression evaluates to true or false.
114+ BranchRegion = 4 ,
115+ }
116+
117+ /// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
118+ /// coverage map, in accordance with the
119+ /// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
120+ /// The struct composes fields representing the `Counter` type and value(s) (injected counter
121+ /// ID, or expression type and operands), the source file (an indirect index into a "filenames
122+ /// array", encoded separately), and source location (start and end positions of the represented
123+ /// code region).
124+ ///
125+ /// Corresponds to struct `llvm::coverage::CounterMappingRegion`.
126+ ///
127+ /// Must match the layout of `LLVMRustCounterMappingRegion`.
128+ #[ derive( Copy , Clone , Debug ) ]
129+ #[ repr( C ) ]
130+ pub struct CounterMappingRegion {
131+ /// The counter type and type-dependent counter data, if any.
132+ counter : Counter ,
133+
134+ /// If the `RegionKind` is a `BranchRegion`, this represents the counter
135+ /// for the false branch of the region.
136+ false_counter : Counter ,
137+
138+ /// An indirect reference to the source filename. In the LLVM Coverage Mapping Format, the
139+ /// file_id is an index into a function-specific `virtual_file_mapping` array of indexes
140+ /// that, in turn, are used to look up the filename for this region.
141+ file_id : u32 ,
142+
143+ /// If the `RegionKind` is an `ExpansionRegion`, the `expanded_file_id` can be used to find
144+ /// the mapping regions created as a result of macro expansion, by checking if their file id
145+ /// matches the expanded file id.
146+ expanded_file_id : u32 ,
147+
148+ /// 1-based starting line of the mapping region.
149+ start_line : u32 ,
150+
151+ /// 1-based starting column of the mapping region.
152+ start_col : u32 ,
153+
154+ /// 1-based ending line of the mapping region.
155+ end_line : u32 ,
156+
157+ /// 1-based ending column of the mapping region. If the high bit is set, the current
158+ /// mapping region is a gap area.
159+ end_col : u32 ,
160+
161+ kind : RegionKind ,
162+ }
163+
164+ impl CounterMappingRegion {
165+ pub ( crate ) fn code_region (
166+ counter : Counter ,
167+ file_id : u32 ,
168+ start_line : u32 ,
169+ start_col : u32 ,
170+ end_line : u32 ,
171+ end_col : u32 ,
172+ ) -> Self {
173+ Self {
174+ counter,
175+ false_counter : Counter :: zero ( ) ,
176+ file_id,
177+ expanded_file_id : 0 ,
178+ start_line,
179+ start_col,
180+ end_line,
181+ end_col,
182+ kind : RegionKind :: CodeRegion ,
183+ }
184+ }
185+
186+ // This function might be used in the future; the LLVM API is still evolving, as is coverage
187+ // support.
188+ #[ allow( dead_code) ]
189+ pub ( crate ) fn branch_region (
190+ counter : Counter ,
191+ false_counter : Counter ,
192+ file_id : u32 ,
193+ start_line : u32 ,
194+ start_col : u32 ,
195+ end_line : u32 ,
196+ end_col : u32 ,
197+ ) -> Self {
198+ Self {
199+ counter,
200+ false_counter,
201+ file_id,
202+ expanded_file_id : 0 ,
203+ start_line,
204+ start_col,
205+ end_line,
206+ end_col,
207+ kind : RegionKind :: BranchRegion ,
208+ }
209+ }
210+
211+ // This function might be used in the future; the LLVM API is still evolving, as is coverage
212+ // support.
213+ #[ allow( dead_code) ]
214+ pub ( crate ) fn expansion_region (
215+ file_id : u32 ,
216+ expanded_file_id : u32 ,
217+ start_line : u32 ,
218+ start_col : u32 ,
219+ end_line : u32 ,
220+ end_col : u32 ,
221+ ) -> Self {
222+ Self {
223+ counter : Counter :: zero ( ) ,
224+ false_counter : Counter :: zero ( ) ,
225+ file_id,
226+ expanded_file_id,
227+ start_line,
228+ start_col,
229+ end_line,
230+ end_col,
231+ kind : RegionKind :: ExpansionRegion ,
232+ }
233+ }
234+
235+ // This function might be used in the future; the LLVM API is still evolving, as is coverage
236+ // support.
237+ #[ allow( dead_code) ]
238+ pub ( crate ) fn skipped_region (
239+ file_id : u32 ,
240+ start_line : u32 ,
241+ start_col : u32 ,
242+ end_line : u32 ,
243+ end_col : u32 ,
244+ ) -> Self {
245+ Self {
246+ counter : Counter :: zero ( ) ,
247+ false_counter : Counter :: zero ( ) ,
248+ file_id,
249+ expanded_file_id : 0 ,
250+ start_line,
251+ start_col,
252+ end_line,
253+ end_col,
254+ kind : RegionKind :: SkippedRegion ,
255+ }
256+ }
257+
258+ // This function might be used in the future; the LLVM API is still evolving, as is coverage
259+ // support.
260+ #[ allow( dead_code) ]
261+ pub ( crate ) fn gap_region (
262+ counter : Counter ,
263+ file_id : u32 ,
264+ start_line : u32 ,
265+ start_col : u32 ,
266+ end_line : u32 ,
267+ end_col : u32 ,
268+ ) -> Self {
269+ Self {
270+ counter,
271+ false_counter : Counter :: zero ( ) ,
272+ file_id,
273+ expanded_file_id : 0 ,
274+ start_line,
275+ start_col,
276+ end_line,
277+ end_col : ( 1_u32 << 31 ) | end_col,
278+ kind : RegionKind :: GapRegion ,
279+ }
280+ }
281+ }
0 commit comments