Skip to content

Commit f0db35b

Browse files
authored
[MC/DC] Refactor: Introduce MCDCTypes.h for coverage::mcdc (#81459)
They can be also used in `clang`. Introduce the lightweight header instead of `CoverageMapping.h`. This includes for now: * `mcdc::ConditionID` * `mcdc::Parameters`
1 parent 815a846 commit f0db35b

File tree

6 files changed

+80
-59
lines changed

6 files changed

+80
-59
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ void CoverageSourceInfo::updateNextTokLoc(SourceLocation Loc) {
9595
}
9696

9797
namespace {
98-
using MCDCConditionID = CounterMappingRegion::MCDCConditionID;
99-
using MCDCParameters = CounterMappingRegion::MCDCParameters;
100-
10198
/// A region of source code that can be mapped to a counter.
10299
class SourceMappingRegion {
103100
/// Primary Counter that is also used for Branch Regions for "True" branches.
@@ -107,7 +104,7 @@ class SourceMappingRegion {
107104
std::optional<Counter> FalseCount;
108105

109106
/// Parameters used for Modified Condition/Decision Coverage
110-
MCDCParameters MCDCParams;
107+
mcdc::Parameters MCDCParams;
111108

112109
/// The region's starting location.
113110
std::optional<SourceLocation> LocStart;
@@ -131,15 +128,15 @@ class SourceMappingRegion {
131128
SkippedRegion(false) {}
132129

133130
SourceMappingRegion(Counter Count, std::optional<Counter> FalseCount,
134-
MCDCParameters MCDCParams,
131+
mcdc::Parameters MCDCParams,
135132
std::optional<SourceLocation> LocStart,
136133
std::optional<SourceLocation> LocEnd,
137134
bool GapRegion = false)
138135
: Count(Count), FalseCount(FalseCount), MCDCParams(MCDCParams),
139136
LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion),
140137
SkippedRegion(false) {}
141138

142-
SourceMappingRegion(MCDCParameters MCDCParams,
139+
SourceMappingRegion(mcdc::Parameters MCDCParams,
143140
std::optional<SourceLocation> LocStart,
144141
std::optional<SourceLocation> LocEnd)
145142
: MCDCParams(MCDCParams), LocStart(LocStart), LocEnd(LocEnd),
@@ -187,7 +184,7 @@ class SourceMappingRegion {
187184

188185
bool isMCDCDecision() const { return MCDCParams.NumConditions != 0; }
189186

190-
const MCDCParameters &getMCDCParams() const { return MCDCParams; }
187+
const mcdc::Parameters &getMCDCParams() const { return MCDCParams; }
191188
};
192189

193190
/// Spelling locations for the start and end of a source region.
@@ -587,8 +584,8 @@ struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
587584
struct MCDCCoverageBuilder {
588585

589586
struct DecisionIDPair {
590-
MCDCConditionID TrueID = 0;
591-
MCDCConditionID FalseID = 0;
587+
mcdc::ConditionID TrueID = 0;
588+
mcdc::ConditionID FalseID = 0;
592589
};
593590

594591
/// The AST walk recursively visits nested logical-AND or logical-OR binary
@@ -682,9 +679,9 @@ struct MCDCCoverageBuilder {
682679
CodeGenModule &CGM;
683680

684681
llvm::SmallVector<DecisionIDPair> DecisionStack;
685-
llvm::DenseMap<const Stmt *, MCDCConditionID> &CondIDs;
682+
llvm::DenseMap<const Stmt *, mcdc::ConditionID> &CondIDs;
686683
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap;
687-
MCDCConditionID NextID = 1;
684+
mcdc::ConditionID NextID = 1;
688685
bool NotMapped = false;
689686

690687
/// Represent a sentinel value of [0,0] for the bottom of DecisionStack.
@@ -696,9 +693,10 @@ struct MCDCCoverageBuilder {
696693
}
697694

698695
public:
699-
MCDCCoverageBuilder(CodeGenModule &CGM,
700-
llvm::DenseMap<const Stmt *, MCDCConditionID> &CondIDMap,
701-
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap)
696+
MCDCCoverageBuilder(
697+
CodeGenModule &CGM,
698+
llvm::DenseMap<const Stmt *, mcdc::ConditionID> &CondIDMap,
699+
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap)
702700
: CGM(CGM), DecisionStack(1, DecisionStackSentinel), CondIDs(CondIDMap),
703701
MCDCBitmapMap(MCDCBitmapMap) {}
704702

@@ -713,12 +711,12 @@ struct MCDCCoverageBuilder {
713711
bool isBuilding() const { return (NextID > 1); }
714712

715713
/// Set the given condition's ID.
716-
void setCondID(const Expr *Cond, MCDCConditionID ID) {
714+
void setCondID(const Expr *Cond, mcdc::ConditionID ID) {
717715
CondIDs[CodeGenFunction::stripCond(Cond)] = ID;
718716
}
719717

720718
/// Return the ID of a given condition.
721-
MCDCConditionID getCondID(const Expr *Cond) const {
719+
mcdc::ConditionID getCondID(const Expr *Cond) const {
722720
auto I = CondIDs.find(CodeGenFunction::stripCond(Cond));
723721
if (I == CondIDs.end())
724722
return 0;
@@ -755,7 +753,7 @@ struct MCDCCoverageBuilder {
755753
setCondID(E->getLHS(), NextID++);
756754

757755
// Assign a ID+1 for the RHS.
758-
MCDCConditionID RHSid = NextID++;
756+
mcdc::ConditionID RHSid = NextID++;
759757
setCondID(E->getRHS(), RHSid);
760758

761759
// Push the LHS decision IDs onto the DecisionStack.
@@ -865,8 +863,8 @@ struct CounterCoverageMappingBuilder
865863
std::optional<SourceLocation> StartLoc = std::nullopt,
866864
std::optional<SourceLocation> EndLoc = std::nullopt,
867865
std::optional<Counter> FalseCount = std::nullopt,
868-
MCDCConditionID ID = 0, MCDCConditionID TrueID = 0,
869-
MCDCConditionID FalseID = 0) {
866+
mcdc::ConditionID ID = 0, mcdc::ConditionID TrueID = 0,
867+
mcdc::ConditionID FalseID = 0) {
870868

871869
if (StartLoc && !FalseCount) {
872870
MostRecentLocation = *StartLoc;
@@ -886,7 +884,7 @@ struct CounterCoverageMappingBuilder
886884
if (EndLoc && EndLoc->isInvalid())
887885
EndLoc = std::nullopt;
888886
RegionStack.emplace_back(Count, FalseCount,
889-
MCDCParameters{0, 0, ID, TrueID, FalseID},
887+
mcdc::Parameters{0, 0, ID, TrueID, FalseID},
890888
StartLoc, EndLoc);
891889

892890
return RegionStack.size() - 1;
@@ -896,7 +894,7 @@ struct CounterCoverageMappingBuilder
896894
std::optional<SourceLocation> StartLoc = std::nullopt,
897895
std::optional<SourceLocation> EndLoc = std::nullopt) {
898896

899-
RegionStack.emplace_back(MCDCParameters{BitmapIdx, Conditions}, StartLoc,
897+
RegionStack.emplace_back(mcdc::Parameters{BitmapIdx, Conditions}, StartLoc,
900898
EndLoc);
901899

902900
return RegionStack.size() - 1;
@@ -1042,9 +1040,9 @@ struct CounterCoverageMappingBuilder
10421040
// function's SourceRegions) because it doesn't apply to any other source
10431041
// code other than the Condition.
10441042
if (CodeGenFunction::isInstrumentedCondition(C)) {
1045-
MCDCConditionID ID = MCDCBuilder.getCondID(C);
1046-
MCDCConditionID TrueID = IDPair.TrueID;
1047-
MCDCConditionID FalseID = IDPair.FalseID;
1043+
mcdc::ConditionID ID = MCDCBuilder.getCondID(C);
1044+
mcdc::ConditionID TrueID = IDPair.TrueID;
1045+
mcdc::ConditionID FalseID = IDPair.FalseID;
10481046

10491047
// If a condition can fold to true or false, the corresponding branch
10501048
// will be removed. Create a region with both counters hard-coded to
@@ -1151,9 +1149,9 @@ struct CounterCoverageMappingBuilder
11511149
if (I.isBranch())
11521150
SourceRegions.emplace_back(
11531151
I.getCounter(), I.getFalseCounter(),
1154-
MCDCParameters{0, 0, I.getMCDCParams().ID,
1155-
I.getMCDCParams().TrueID,
1156-
I.getMCDCParams().FalseID},
1152+
mcdc::Parameters{0, 0, I.getMCDCParams().ID,
1153+
I.getMCDCParams().TrueID,
1154+
I.getMCDCParams().FalseID},
11571155
Loc, getEndOfFileOrMacro(Loc), I.isBranch());
11581156
else
11591157
SourceRegions.emplace_back(I.getCounter(), Loc,
@@ -1338,7 +1336,7 @@ struct CounterCoverageMappingBuilder
13381336
CoverageMappingModuleGen &CVM,
13391337
llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
13401338
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap,
1341-
llvm::DenseMap<const Stmt *, MCDCConditionID> &CondIDMap,
1339+
llvm::DenseMap<const Stmt *, mcdc::ConditionID> &CondIDMap,
13421340
SourceManager &SM, const LangOptions &LangOpts)
13431341
: CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
13441342
MCDCBitmapMap(MCDCBitmapMap),

llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ADT/iterator.h"
2424
#include "llvm/ADT/iterator_range.h"
2525
#include "llvm/Object/BuildID.h"
26+
#include "llvm/ProfileData/Coverage/MCDCTypes.h"
2627
#include "llvm/ProfileData/InstrProf.h"
2728
#include "llvm/Support/Alignment.h"
2829
#include "llvm/Support/Compiler.h"
@@ -249,27 +250,14 @@ struct CounterMappingRegion {
249250
MCDCBranchRegion
250251
};
251252

252-
using MCDCConditionID = unsigned int;
253-
struct MCDCParameters {
254-
/// Byte Index of Bitmap Coverage Object for a Decision Region.
255-
unsigned BitmapIdx = 0;
256-
257-
/// Number of Conditions used for a Decision Region.
258-
unsigned NumConditions = 0;
259-
260-
/// IDs used to represent a branch region and other branch regions
261-
/// evaluated based on True and False branches.
262-
MCDCConditionID ID = 0, TrueID = 0, FalseID = 0;
263-
};
264-
265253
/// Primary Counter that is also used for Branch Regions (TrueCount).
266254
Counter Count;
267255

268256
/// Secondary Counter used for Branch Regions (FalseCount).
269257
Counter FalseCount;
270258

271259
/// Parameters used for Modified Condition/Decision Coverage
272-
MCDCParameters MCDCParams;
260+
mcdc::Parameters MCDCParams;
273261

274262
unsigned FileID = 0;
275263
unsigned ExpandedFileID = 0;
@@ -285,7 +273,7 @@ struct CounterMappingRegion {
285273
ColumnEnd(ColumnEnd), Kind(Kind) {}
286274

287275
CounterMappingRegion(Counter Count, Counter FalseCount,
288-
MCDCParameters MCDCParams, unsigned FileID,
276+
mcdc::Parameters MCDCParams, unsigned FileID,
289277
unsigned ExpandedFileID, unsigned LineStart,
290278
unsigned ColumnStart, unsigned LineEnd,
291279
unsigned ColumnEnd, RegionKind Kind)
@@ -294,7 +282,7 @@ struct CounterMappingRegion {
294282
ColumnStart(ColumnStart), LineEnd(LineEnd), ColumnEnd(ColumnEnd),
295283
Kind(Kind) {}
296284

297-
CounterMappingRegion(MCDCParameters MCDCParams, unsigned FileID,
285+
CounterMappingRegion(mcdc::Parameters MCDCParams, unsigned FileID,
298286
unsigned LineStart, unsigned ColumnStart,
299287
unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind)
300288
: MCDCParams(MCDCParams), FileID(FileID), LineStart(LineStart),
@@ -334,23 +322,24 @@ struct CounterMappingRegion {
334322
makeBranchRegion(Counter Count, Counter FalseCount, unsigned FileID,
335323
unsigned LineStart, unsigned ColumnStart, unsigned LineEnd,
336324
unsigned ColumnEnd) {
337-
return CounterMappingRegion(Count, FalseCount, MCDCParameters(), FileID, 0,
338-
LineStart, ColumnStart, LineEnd, ColumnEnd,
325+
return CounterMappingRegion(Count, FalseCount, mcdc::Parameters(), FileID,
326+
0, LineStart, ColumnStart, LineEnd, ColumnEnd,
339327
BranchRegion);
340328
}
341329

342330
static CounterMappingRegion
343-
makeBranchRegion(Counter Count, Counter FalseCount, MCDCParameters MCDCParams,
344-
unsigned FileID, unsigned LineStart, unsigned ColumnStart,
345-
unsigned LineEnd, unsigned ColumnEnd) {
331+
makeBranchRegion(Counter Count, Counter FalseCount,
332+
mcdc::Parameters MCDCParams, unsigned FileID,
333+
unsigned LineStart, unsigned ColumnStart, unsigned LineEnd,
334+
unsigned ColumnEnd) {
346335
return CounterMappingRegion(Count, FalseCount, MCDCParams, FileID, 0,
347336
LineStart, ColumnStart, LineEnd, ColumnEnd,
348337
MCDCParams.ID == 0 ? BranchRegion
349338
: MCDCBranchRegion);
350339
}
351340

352341
static CounterMappingRegion
353-
makeDecisionRegion(MCDCParameters MCDCParams, unsigned FileID,
342+
makeDecisionRegion(mcdc::Parameters MCDCParams, unsigned FileID,
354343
unsigned LineStart, unsigned ColumnStart, unsigned LineEnd,
355344
unsigned ColumnEnd) {
356345
return CounterMappingRegion(MCDCParams, FileID, LineStart, ColumnStart,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===- MCDCTypes.h - Types related to MC/DC Coverage ------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Types related to MC/DC Coverage.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_PROFILEDATA_COVERAGE_MCDCTYPES_H
14+
#define LLVM_PROFILEDATA_COVERAGE_MCDCTYPES_H
15+
16+
namespace llvm::coverage::mcdc {
17+
18+
/// The ID for MCDCBranch.
19+
using ConditionID = unsigned int;
20+
21+
/// MC/DC-specifig parameters
22+
struct Parameters {
23+
/// Byte Index of Bitmap Coverage Object for a Decision Region.
24+
unsigned BitmapIdx = 0;
25+
26+
/// Number of Conditions used for a Decision Region.
27+
unsigned NumConditions = 0;
28+
29+
/// IDs used to represent a branch region and other branch regions
30+
/// evaluated based on True and False branches.
31+
ConditionID ID = 0, TrueID = 0, FalseID = 0;
32+
};
33+
34+
} // namespace llvm::coverage::mcdc
35+
36+
#endif // LLVM_PROFILEDATA_COVERAGE_MCDCTYPES_H

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ class MCDCDecisionRecorder {
524524

525525
/// IDs that are stored in MCDCBranches
526526
/// Complete when all IDs (1 to NumConditions) are met.
527-
DenseSet<CounterMappingRegion::MCDCConditionID> ConditionIDs;
527+
DenseSet<mcdc::ConditionID> ConditionIDs;
528528

529529
/// Set of IDs of Expansion(s) that are relevant to DecisionRegion
530530
/// and its children (via expansions).

llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,9 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray(
371371

372372
auto CMR = CounterMappingRegion(
373373
C, C2,
374-
CounterMappingRegion::MCDCParameters{
375-
static_cast<unsigned>(BIDX), static_cast<unsigned>(NC),
376-
static_cast<unsigned>(ID), static_cast<unsigned>(TID),
377-
static_cast<unsigned>(FID)},
374+
mcdc::Parameters{static_cast<unsigned>(BIDX), static_cast<unsigned>(NC),
375+
static_cast<unsigned>(ID), static_cast<unsigned>(TID),
376+
static_cast<unsigned>(FID)},
378377
InferredFileID, ExpandedFileID, LineStart, ColumnStart,
379378
LineStart + NumLines, ColumnEnd, Kind);
380379
if (CMR.startLoc() > CMR.endLoc())

llvm/unittests/ProfileData/CoverageMappingTest.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ struct CoverageMappingTest : ::testing::TestWithParam<std::tuple<bool, bool>> {
197197
auto &Regions = InputFunctions.back().Regions;
198198
unsigned FileID = getFileIndexForFunction(File);
199199
Regions.push_back(CounterMappingRegion::makeDecisionRegion(
200-
CounterMappingRegion::MCDCParameters{Mask, NC}, FileID, LS, CS, LE,
201-
CE));
200+
mcdc::Parameters{Mask, NC}, FileID, LS, CS, LE, CE));
202201
}
203202

204203
void addMCDCBranchCMR(Counter C1, Counter C2, unsigned ID, unsigned TrueID,
@@ -207,8 +206,8 @@ struct CoverageMappingTest : ::testing::TestWithParam<std::tuple<bool, bool>> {
207206
auto &Regions = InputFunctions.back().Regions;
208207
unsigned FileID = getFileIndexForFunction(File);
209208
Regions.push_back(CounterMappingRegion::makeBranchRegion(
210-
C1, C2, CounterMappingRegion::MCDCParameters{0, 0, ID, TrueID, FalseID},
211-
FileID, LS, CS, LE, CE));
209+
C1, C2, mcdc::Parameters{0, 0, ID, TrueID, FalseID}, FileID, LS, CS, LE,
210+
CE));
212211
}
213212

214213
void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,

0 commit comments

Comments
 (0)