Skip to content

Commit d31f768

Browse files
committed
[SIL] Introduce skipped coverage ranges
Introduce a skipped region kind, printed using a `skipped` keyword.
1 parent 6a58991 commit d31f768

File tree

6 files changed

+80
-15
lines changed

6 files changed

+80
-15
lines changed

include/swift/SIL/SILCoverageMap.h

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,52 @@ namespace swift {
4141
class SILCoverageMap : public llvm::ilist_node<SILCoverageMap>,
4242
public SILAllocated<SILCoverageMap> {
4343
public:
44-
struct MappedRegion {
44+
class MappedRegion {
45+
public:
46+
enum class Kind {
47+
/// A code region, which represents a regular region of source code.
48+
Code,
49+
50+
/// A skipped region, which represents a region that cannot have any
51+
/// coverage associated with it. This is used for e.g the inactive body of
52+
/// a \c #if.
53+
Skipped
54+
};
55+
56+
Kind RegionKind;
4557
unsigned StartLine;
4658
unsigned StartCol;
4759
unsigned EndLine;
4860
unsigned EndCol;
4961
llvm::coverage::Counter Counter;
5062

51-
MappedRegion(unsigned StartLine, unsigned StartCol, unsigned EndLine,
52-
unsigned EndCol, llvm::coverage::Counter Counter)
53-
: StartLine(StartLine), StartCol(StartCol), EndLine(EndLine),
54-
EndCol(EndCol), Counter(Counter) {}
63+
private:
64+
MappedRegion(Kind RegionKind, unsigned StartLine, unsigned StartCol,
65+
unsigned EndLine, unsigned EndCol,
66+
llvm::coverage::Counter Counter)
67+
: RegionKind(RegionKind), StartLine(StartLine), StartCol(StartCol),
68+
EndLine(EndLine), EndCol(EndCol), Counter(Counter) {}
69+
70+
public:
71+
/// A code region, which represents a regular region of source code.
72+
static MappedRegion code(unsigned StartLine, unsigned StartCol,
73+
unsigned EndLine, unsigned EndCol,
74+
llvm::coverage::Counter Counter) {
75+
return MappedRegion(Kind::Code, StartLine, StartCol, EndLine, EndCol,
76+
Counter);
77+
}
78+
79+
/// A skipped region, which represents a region that cannot have any
80+
/// coverage associated with it. This is used for e.g the inactive body of
81+
/// a \c #if.
82+
static MappedRegion skipped(unsigned StartLine, unsigned StartCol,
83+
unsigned EndLine, unsigned EndCol) {
84+
return MappedRegion(Kind::Skipped, StartLine, StartCol, EndLine, EndCol,
85+
llvm::coverage::Counter());
86+
}
87+
88+
/// Retrieve the equivalent LLVM mapped region.
89+
llvm::coverage::CounterMappingRegion getLLVMRegion(unsigned FileID) const;
5590
};
5691

5792
private:

lib/IRGen/GenCoverage.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,9 @@ void IRGenModule::emitCoverageMaps(ArrayRef<const SILCoverageMap *> Mappings) {
143143

144144
std::vector<CounterMappingRegion> Regions;
145145
for (const auto &MR : M->getMappedRegions()) {
146-
// The SubFileID here is 0, because it's an index into VirtualFileMapping,
146+
// The FileID here is 0, because it's an index into VirtualFileMapping,
147147
// and we only ever have a single file associated for a function.
148-
Regions.emplace_back(CounterMappingRegion::makeRegion(
149-
MR.Counter, /*SubFileID*/ 0, MR.StartLine, MR.StartCol, MR.EndLine,
150-
MR.EndCol));
148+
Regions.push_back(MR.getLLVMRegion(/*FileID*/ 0));
151149
}
152150
// Append each function's regions into the encoded buffer.
153151
ArrayRef<unsigned> VirtualFileMapping(FileID);

lib/SIL/IR/SILCoverageMap.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using namespace swift;
2323

2424
using llvm::coverage::CounterExpression;
25+
using llvm::coverage::CounterMappingRegion;
2526

2627
SILCoverageMap *
2728
SILCoverageMap::create(SILModule &M, SourceFile *ParentSourceFile,
@@ -56,6 +57,19 @@ SILCoverageMap::SILCoverageMap(SourceFile *ParentSourceFile, uint64_t Hash)
5657

5758
SILCoverageMap::~SILCoverageMap() {}
5859

60+
CounterMappingRegion
61+
SILCoverageMap::MappedRegion::getLLVMRegion(unsigned int FileID) const {
62+
switch (RegionKind) {
63+
case MappedRegion::Kind::Code:
64+
return CounterMappingRegion::makeRegion(Counter, FileID, StartLine,
65+
StartCol, EndLine, EndCol);
66+
case MappedRegion::Kind::Skipped:
67+
return CounterMappingRegion::makeSkipped(FileID, StartLine, StartCol,
68+
EndLine, EndCol);
69+
}
70+
llvm_unreachable("Unhandled case in switch!");
71+
}
72+
5973
namespace {
6074
struct Printer {
6175
const llvm::coverage::Counter &C;

lib/SIL/IR/SILPrinter.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4168,7 +4168,14 @@ void SILCoverageMap::print(SILPrintContext &PrintCtx) const {
41684168
for (auto &MR : getMappedRegions()) {
41694169
OS << " " << MR.StartLine << ":" << MR.StartCol << " -> " << MR.EndLine
41704170
<< ":" << MR.EndCol << " : ";
4171-
printCounter(OS, MR.Counter);
4171+
switch (MR.RegionKind) {
4172+
case MappedRegion::Kind::Code:
4173+
printCounter(OS, MR.Counter);
4174+
break;
4175+
case MappedRegion::Kind::Skipped:
4176+
OS << "skipped";
4177+
break;
4178+
}
41724179
OS << "\n";
41734180
}
41744181
OS << "}\n\n";

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8621,13 +8621,22 @@ bool SILParserState::parseSILCoverageMap(Parser &P) {
86218621
break;
86228622
}
86238623

8624-
auto Counter = State.parseSILCoverageExpr(Builder);
8625-
if (!Counter) {
8626-
BodyHasError = true;
8627-
break;
8624+
using MappedRegion = SILCoverageMap::MappedRegion;
8625+
8626+
if (P.Tok.isContextualKeyword("skipped")) {
8627+
P.consumeToken();
8628+
Regions.push_back(
8629+
MappedRegion::skipped(StartLine, StartCol, EndLine, EndCol));
8630+
} else {
8631+
auto Counter = State.parseSILCoverageExpr(Builder);
8632+
if (!Counter) {
8633+
BodyHasError = true;
8634+
break;
8635+
}
8636+
Regions.push_back(
8637+
MappedRegion::code(StartLine, StartCol, EndLine, EndCol, *Counter));
86288638
}
86298639

8630-
Regions.emplace_back(StartLine, StartCol, EndLine, EndCol, *Counter);
86318640
} while (P.Tok.isNot(tok::r_brace) && P.Tok.isNot(tok::eof));
86328641
}
86338642
if (BodyHasError)

test/SIL/Parser/coverage_maps.sil

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ sil_coverage_map "coverage_maps.sil" "someFunction" "coverage_maps.sil:someFunct
1818
// CHECK: 17:9 -> 17:17 : ((0 + 2) - 3)
1919
// CHECK: 17:9 -> 17:17 : ((0 + 2) - 3)
2020
// CHECK: 22:11 -> 139:2 : 1
21+
// CHECK: 54:1 -> 55:1 : skipped
2122
// CHECK: }
2223
sil_coverage_map "/some/other/file.sil" "someFunction" "/some/other/file.sil:someFunction" 0 {
2324
4:19 -> 31:1 : 0
@@ -26,4 +27,5 @@ sil_coverage_map "/some/other/file.sil" "someFunction" "/some/other/file.sil:som
2627
17:9 -> 17:17: ((0 + 2) - 3)
2728
17:9 -> 17:17: (0 + (2 - 3))
2829
22:11->139:2 : (zero + 1)
30+
54:1 -> 55:1 : skipped
2931
}

0 commit comments

Comments
 (0)