-
Notifications
You must be signed in to change notification settings - Fork 12.6k
/
Copy pathCoverageMappingGen.cpp
2461 lines (2094 loc) · 90.8 KB
/
CoverageMappingGen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Instrumentation-based code coverage mapping generator
//
//===----------------------------------------------------------------------===//
#include "CoverageMappingGen.h"
#include "CodeGenFunction.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
#include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
#include "llvm/ProfileData/InstrProfReader.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include <optional>
// This selects the coverage mapping format defined when `InstrProfData.inc`
// is textually included.
#define COVMAP_V3
namespace llvm {
cl::opt<bool>
EnableSingleByteCoverage("enable-single-byte-coverage",
llvm::cl::ZeroOrMore,
llvm::cl::desc("Enable single byte coverage"),
llvm::cl::Hidden, llvm::cl::init(false));
} // namespace llvm
static llvm::cl::opt<bool> EmptyLineCommentCoverage(
"emptyline-comment-coverage",
llvm::cl::desc("Emit emptylines and comment lines as skipped regions (only "
"disable it on test)"),
llvm::cl::init(true), llvm::cl::Hidden);
llvm::cl::opt<bool> SystemHeadersCoverage(
"system-headers-coverage",
llvm::cl::desc("Enable collecting coverage from system headers"),
llvm::cl::init(false), llvm::cl::Hidden);
using namespace clang;
using namespace CodeGen;
using namespace llvm::coverage;
CoverageSourceInfo *
CoverageMappingModuleGen::setUpCoverageCallbacks(Preprocessor &PP) {
CoverageSourceInfo *CoverageInfo =
new CoverageSourceInfo(PP.getSourceManager());
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(CoverageInfo));
if (EmptyLineCommentCoverage) {
PP.addCommentHandler(CoverageInfo);
PP.setEmptylineHandler(CoverageInfo);
PP.setPreprocessToken(true);
PP.setTokenWatcher([CoverageInfo](clang::Token Tok) {
// Update previous token location.
CoverageInfo->PrevTokLoc = Tok.getLocation();
if (Tok.getKind() != clang::tok::eod)
CoverageInfo->updateNextTokLoc(Tok.getLocation());
});
}
return CoverageInfo;
}
void CoverageSourceInfo::AddSkippedRange(SourceRange Range,
SkippedRange::Kind RangeKind) {
if (EmptyLineCommentCoverage && !SkippedRanges.empty() &&
PrevTokLoc == SkippedRanges.back().PrevTokLoc &&
SourceMgr.isWrittenInSameFile(SkippedRanges.back().Range.getEnd(),
Range.getBegin()))
SkippedRanges.back().Range.setEnd(Range.getEnd());
else
SkippedRanges.push_back({Range, RangeKind, PrevTokLoc});
}
void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range, SourceLocation) {
AddSkippedRange(Range, SkippedRange::PPIfElse);
}
void CoverageSourceInfo::HandleEmptyline(SourceRange Range) {
AddSkippedRange(Range, SkippedRange::EmptyLine);
}
bool CoverageSourceInfo::HandleComment(Preprocessor &PP, SourceRange Range) {
AddSkippedRange(Range, SkippedRange::Comment);
return false;
}
void CoverageSourceInfo::updateNextTokLoc(SourceLocation Loc) {
if (!SkippedRanges.empty() && SkippedRanges.back().NextTokLoc.isInvalid())
SkippedRanges.back().NextTokLoc = Loc;
}
namespace {
/// A region of source code that can be mapped to a counter.
class SourceMappingRegion {
/// Primary Counter that is also used for Branch Regions for "True" branches.
Counter Count;
/// Secondary Counter used for Branch Regions for "False" branches.
std::optional<Counter> FalseCount;
/// Parameters used for Modified Condition/Decision Coverage
mcdc::Parameters MCDCParams;
/// The region's starting location.
std::optional<SourceLocation> LocStart;
/// The region's ending location.
std::optional<SourceLocation> LocEnd;
/// Whether this region is a gap region. The count from a gap region is set
/// as the line execution count if there are no other regions on the line.
bool GapRegion;
/// Whetever this region is skipped ('if constexpr' or 'if consteval' untaken
/// branch, or anything skipped but not empty line / comments)
bool SkippedRegion;
public:
SourceMappingRegion(Counter Count, std::optional<SourceLocation> LocStart,
std::optional<SourceLocation> LocEnd,
bool GapRegion = false)
: Count(Count), LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion),
SkippedRegion(false) {}
SourceMappingRegion(Counter Count, std::optional<Counter> FalseCount,
mcdc::Parameters MCDCParams,
std::optional<SourceLocation> LocStart,
std::optional<SourceLocation> LocEnd,
bool GapRegion = false)
: Count(Count), FalseCount(FalseCount), MCDCParams(MCDCParams),
LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion),
SkippedRegion(false) {}
SourceMappingRegion(mcdc::Parameters MCDCParams,
std::optional<SourceLocation> LocStart,
std::optional<SourceLocation> LocEnd)
: MCDCParams(MCDCParams), LocStart(LocStart), LocEnd(LocEnd),
GapRegion(false), SkippedRegion(false) {}
const Counter &getCounter() const { return Count; }
const Counter &getFalseCounter() const {
assert(FalseCount && "Region has no alternate counter");
return *FalseCount;
}
void setCounter(Counter C) { Count = C; }
bool hasStartLoc() const { return LocStart.has_value(); }
void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
SourceLocation getBeginLoc() const {
assert(LocStart && "Region has no start location");
return *LocStart;
}
bool hasEndLoc() const { return LocEnd.has_value(); }
void setEndLoc(SourceLocation Loc) {
assert(Loc.isValid() && "Setting an invalid end location");
LocEnd = Loc;
}
SourceLocation getEndLoc() const {
assert(LocEnd && "Region has no end location");
return *LocEnd;
}
bool isGap() const { return GapRegion; }
void setGap(bool Gap) { GapRegion = Gap; }
bool isSkipped() const { return SkippedRegion; }
void setSkipped(bool Skipped) { SkippedRegion = Skipped; }
bool isBranch() const { return FalseCount.has_value(); }
bool isMCDCDecision() const {
const auto *DecisionParams =
std::get_if<mcdc::DecisionParameters>(&MCDCParams);
assert(!DecisionParams || DecisionParams->NumConditions > 0);
return DecisionParams;
}
const auto &getMCDCDecisionParams() const {
return mcdc::getParams<const mcdc::DecisionParameters>(MCDCParams);
}
const mcdc::Parameters &getMCDCParams() const { return MCDCParams; }
};
/// Spelling locations for the start and end of a source region.
struct SpellingRegion {
/// The line where the region starts.
unsigned LineStart;
/// The column where the region starts.
unsigned ColumnStart;
/// The line where the region ends.
unsigned LineEnd;
/// The column where the region ends.
unsigned ColumnEnd;
SpellingRegion(SourceManager &SM, SourceLocation LocStart,
SourceLocation LocEnd) {
LineStart = SM.getSpellingLineNumber(LocStart);
ColumnStart = SM.getSpellingColumnNumber(LocStart);
LineEnd = SM.getSpellingLineNumber(LocEnd);
ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
}
SpellingRegion(SourceManager &SM, SourceMappingRegion &R)
: SpellingRegion(SM, R.getBeginLoc(), R.getEndLoc()) {}
/// Check if the start and end locations appear in source order, i.e
/// top->bottom, left->right.
bool isInSourceOrder() const {
return (LineStart < LineEnd) ||
(LineStart == LineEnd && ColumnStart <= ColumnEnd);
}
};
/// Provides the common functionality for the different
/// coverage mapping region builders.
class CoverageMappingBuilder {
public:
CoverageMappingModuleGen &CVM;
SourceManager &SM;
const LangOptions &LangOpts;
private:
/// Map of clang's FileIDs to IDs used for coverage mapping.
llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
FileIDMapping;
public:
/// The coverage mapping regions for this function
llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
/// The source mapping regions for this function.
std::vector<SourceMappingRegion> SourceRegions;
/// A set of regions which can be used as a filter.
///
/// It is produced by emitExpansionRegions() and is used in
/// emitSourceRegions() to suppress producing code regions if
/// the same area is covered by expansion regions.
typedef llvm::SmallSet<std::pair<SourceLocation, SourceLocation>, 8>
SourceRegionFilter;
CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
const LangOptions &LangOpts)
: CVM(CVM), SM(SM), LangOpts(LangOpts) {}
/// Return the precise end location for the given token.
SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
// We avoid getLocForEndOfToken here, because it doesn't do what we want for
// macro locations, which we just treat as expanded files.
unsigned TokLen =
Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
return Loc.getLocWithOffset(TokLen);
}
/// Return the start location of an included file or expanded macro.
SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
if (Loc.isMacroID())
return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
return SM.getLocForStartOfFile(SM.getFileID(Loc));
}
/// Return the end location of an included file or expanded macro.
SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
if (Loc.isMacroID())
return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
SM.getFileOffset(Loc));
return SM.getLocForEndOfFile(SM.getFileID(Loc));
}
/// Find out where the current file is included or macro is expanded.
SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).getBegin()
: SM.getIncludeLoc(SM.getFileID(Loc));
}
/// Return true if \c Loc is a location in a built-in macro.
bool isInBuiltin(SourceLocation Loc) {
return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>";
}
/// Check whether \c Loc is included or expanded from \c Parent.
bool isNestedIn(SourceLocation Loc, FileID Parent) {
do {
Loc = getIncludeOrExpansionLoc(Loc);
if (Loc.isInvalid())
return false;
} while (!SM.isInFileID(Loc, Parent));
return true;
}
/// Get the start of \c S ignoring macro arguments and builtin macros.
SourceLocation getStart(const Stmt *S) {
SourceLocation Loc = S->getBeginLoc();
while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
Loc = SM.getImmediateExpansionRange(Loc).getBegin();
return Loc;
}
/// Get the end of \c S ignoring macro arguments and builtin macros.
SourceLocation getEnd(const Stmt *S) {
SourceLocation Loc = S->getEndLoc();
while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
Loc = SM.getImmediateExpansionRange(Loc).getBegin();
return getPreciseTokenLocEnd(Loc);
}
/// Find the set of files we have regions for and assign IDs
///
/// Fills \c Mapping with the virtual file mapping needed to write out
/// coverage and collects the necessary file information to emit source and
/// expansion regions.
void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
FileIDMapping.clear();
llvm::SmallSet<FileID, 8> Visited;
SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
for (const auto &Region : SourceRegions) {
SourceLocation Loc = Region.getBeginLoc();
FileID File = SM.getFileID(Loc);
if (!Visited.insert(File).second)
continue;
// Do not map FileID's associated with system headers unless collecting
// coverage from system headers is explicitly enabled.
if (!SystemHeadersCoverage && SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
continue;
unsigned Depth = 0;
for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent))
++Depth;
FileLocs.push_back(std::make_pair(Loc, Depth));
}
llvm::stable_sort(FileLocs, llvm::less_second());
for (const auto &FL : FileLocs) {
SourceLocation Loc = FL.first;
FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
auto Entry = SM.getFileEntryRefForID(SpellingFile);
if (!Entry)
continue;
FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
Mapping.push_back(CVM.getFileID(*Entry));
}
}
/// Get the coverage mapping file ID for \c Loc.
///
/// If such file id doesn't exist, return std::nullopt.
std::optional<unsigned> getCoverageFileID(SourceLocation Loc) {
auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
if (Mapping != FileIDMapping.end())
return Mapping->second.first;
return std::nullopt;
}
/// This shrinks the skipped range if it spans a line that contains a
/// non-comment token. If shrinking the skipped range would make it empty,
/// this returns std::nullopt.
/// Note this function can potentially be expensive because
/// getSpellingLineNumber uses getLineNumber, which is expensive.
std::optional<SpellingRegion> adjustSkippedRange(SourceManager &SM,
SourceLocation LocStart,
SourceLocation LocEnd,
SourceLocation PrevTokLoc,
SourceLocation NextTokLoc) {
SpellingRegion SR{SM, LocStart, LocEnd};
SR.ColumnStart = 1;
if (PrevTokLoc.isValid() && SM.isWrittenInSameFile(LocStart, PrevTokLoc) &&
SR.LineStart == SM.getSpellingLineNumber(PrevTokLoc))
SR.LineStart++;
if (NextTokLoc.isValid() && SM.isWrittenInSameFile(LocEnd, NextTokLoc) &&
SR.LineEnd == SM.getSpellingLineNumber(NextTokLoc)) {
SR.LineEnd--;
SR.ColumnEnd++;
}
if (SR.isInSourceOrder())
return SR;
return std::nullopt;
}
/// Gather all the regions that were skipped by the preprocessor
/// using the constructs like #if or comments.
void gatherSkippedRegions() {
/// An array of the minimum lineStarts and the maximum lineEnds
/// for mapping regions from the appropriate source files.
llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
FileLineRanges.resize(
FileIDMapping.size(),
std::make_pair(std::numeric_limits<unsigned>::max(), 0));
for (const auto &R : MappingRegions) {
FileLineRanges[R.FileID].first =
std::min(FileLineRanges[R.FileID].first, R.LineStart);
FileLineRanges[R.FileID].second =
std::max(FileLineRanges[R.FileID].second, R.LineEnd);
}
auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
for (auto &I : SkippedRanges) {
SourceRange Range = I.Range;
auto LocStart = Range.getBegin();
auto LocEnd = Range.getEnd();
assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
"region spans multiple files");
auto CovFileID = getCoverageFileID(LocStart);
if (!CovFileID)
continue;
std::optional<SpellingRegion> SR;
if (I.isComment())
SR = adjustSkippedRange(SM, LocStart, LocEnd, I.PrevTokLoc,
I.NextTokLoc);
else if (I.isPPIfElse() || I.isEmptyLine())
SR = {SM, LocStart, LocEnd};
if (!SR)
continue;
auto Region = CounterMappingRegion::makeSkipped(
*CovFileID, SR->LineStart, SR->ColumnStart, SR->LineEnd,
SR->ColumnEnd);
// Make sure that we only collect the regions that are inside
// the source code of this function.
if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
Region.LineEnd <= FileLineRanges[*CovFileID].second)
MappingRegions.push_back(Region);
}
}
/// Generate the coverage counter mapping regions from collected
/// source regions.
void emitSourceRegions(const SourceRegionFilter &Filter) {
for (const auto &Region : SourceRegions) {
assert(Region.hasEndLoc() && "incomplete region");
SourceLocation LocStart = Region.getBeginLoc();
assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
// Ignore regions from system headers unless collecting coverage from
// system headers is explicitly enabled.
if (!SystemHeadersCoverage &&
SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
continue;
auto CovFileID = getCoverageFileID(LocStart);
// Ignore regions that don't have a file, such as builtin macros.
if (!CovFileID)
continue;
SourceLocation LocEnd = Region.getEndLoc();
assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
"region spans multiple files");
// Don't add code regions for the area covered by expansion regions.
// This not only suppresses redundant regions, but sometimes prevents
// creating regions with wrong counters if, for example, a statement's
// body ends at the end of a nested macro.
if (Filter.count(std::make_pair(LocStart, LocEnd)))
continue;
// Find the spelling locations for the mapping region.
SpellingRegion SR{SM, LocStart, LocEnd};
assert(SR.isInSourceOrder() && "region start and end out of order");
if (Region.isGap()) {
MappingRegions.push_back(CounterMappingRegion::makeGapRegion(
Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
SR.LineEnd, SR.ColumnEnd));
} else if (Region.isSkipped()) {
MappingRegions.push_back(CounterMappingRegion::makeSkipped(
*CovFileID, SR.LineStart, SR.ColumnStart, SR.LineEnd,
SR.ColumnEnd));
} else if (Region.isBranch()) {
MappingRegions.push_back(CounterMappingRegion::makeBranchRegion(
Region.getCounter(), Region.getFalseCounter(), *CovFileID,
SR.LineStart, SR.ColumnStart, SR.LineEnd, SR.ColumnEnd,
Region.getMCDCParams()));
} else if (Region.isMCDCDecision()) {
MappingRegions.push_back(CounterMappingRegion::makeDecisionRegion(
Region.getMCDCDecisionParams(), *CovFileID, SR.LineStart,
SR.ColumnStart, SR.LineEnd, SR.ColumnEnd));
} else {
MappingRegions.push_back(CounterMappingRegion::makeRegion(
Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
SR.LineEnd, SR.ColumnEnd));
}
}
}
/// Generate expansion regions for each virtual file we've seen.
SourceRegionFilter emitExpansionRegions() {
SourceRegionFilter Filter;
for (const auto &FM : FileIDMapping) {
SourceLocation ExpandedLoc = FM.second.second;
SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
if (ParentLoc.isInvalid())
continue;
auto ParentFileID = getCoverageFileID(ParentLoc);
if (!ParentFileID)
continue;
auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
assert(ExpandedFileID && "expansion in uncovered file");
SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
"region spans multiple files");
Filter.insert(std::make_pair(ParentLoc, LocEnd));
SpellingRegion SR{SM, ParentLoc, LocEnd};
assert(SR.isInSourceOrder() && "region start and end out of order");
MappingRegions.push_back(CounterMappingRegion::makeExpansion(
*ParentFileID, *ExpandedFileID, SR.LineStart, SR.ColumnStart,
SR.LineEnd, SR.ColumnEnd));
}
return Filter;
}
};
/// Creates unreachable coverage regions for the functions that
/// are not emitted.
struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
const LangOptions &LangOpts)
: CoverageMappingBuilder(CVM, SM, LangOpts) {}
void VisitDecl(const Decl *D) {
if (!D->hasBody())
return;
auto Body = D->getBody();
SourceLocation Start = getStart(Body);
SourceLocation End = getEnd(Body);
if (!SM.isWrittenInSameFile(Start, End)) {
// Walk up to find the common ancestor.
// Correct the locations accordingly.
FileID StartFileID = SM.getFileID(Start);
FileID EndFileID = SM.getFileID(End);
while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) {
Start = getIncludeOrExpansionLoc(Start);
assert(Start.isValid() &&
"Declaration start location not nested within a known region");
StartFileID = SM.getFileID(Start);
}
while (StartFileID != EndFileID) {
End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
assert(End.isValid() &&
"Declaration end location not nested within a known region");
EndFileID = SM.getFileID(End);
}
}
SourceRegions.emplace_back(Counter(), Start, End);
}
/// Write the mapping data to the output stream
void write(llvm::raw_ostream &OS) {
SmallVector<unsigned, 16> FileIDMapping;
gatherFileIDs(FileIDMapping);
emitSourceRegions(SourceRegionFilter());
if (MappingRegions.empty())
return;
CoverageMappingWriter Writer(FileIDMapping, std::nullopt, MappingRegions);
Writer.write(OS);
}
};
/// A wrapper object for maintaining stacks to track the resursive AST visitor
/// walks for the purpose of assigning IDs to leaf-level conditions measured by
/// MC/DC. The object is created with a reference to the MCDCBitmapMap that was
/// created during the initial AST walk. The presence of a bitmap associated
/// with a boolean expression (top-level logical operator nest) indicates that
/// the boolean expression qualified for MC/DC. The resulting condition IDs
/// are preserved in a map reference that is also provided during object
/// creation.
struct MCDCCoverageBuilder {
/// The AST walk recursively visits nested logical-AND or logical-OR binary
/// operator nodes and then visits their LHS and RHS children nodes. As this
/// happens, the algorithm will assign IDs to each operator's LHS and RHS side
/// as the walk moves deeper into the nest. At each level of the recursive
/// nest, the LHS and RHS may actually correspond to larger subtrees (not
/// leaf-conditions). If this is the case, when that node is visited, the ID
/// assigned to the subtree is re-assigned to its LHS, and a new ID is given
/// to its RHS. At the end of the walk, all leaf-level conditions will have a
/// unique ID -- keep in mind that the final set of IDs may not be in
/// numerical order from left to right.
///
/// Example: "x = (A && B) || (C && D) || (D && F)"
///
/// Visit Depth1:
/// (A && B) || (C && D) || (D && F)
/// ^-------LHS--------^ ^-RHS--^
/// ID=1 ID=2
///
/// Visit LHS-Depth2:
/// (A && B) || (C && D)
/// ^-LHS--^ ^-RHS--^
/// ID=1 ID=3
///
/// Visit LHS-Depth3:
/// (A && B)
/// LHS RHS
/// ID=1 ID=4
///
/// Visit RHS-Depth3:
/// (C && D)
/// LHS RHS
/// ID=3 ID=5
///
/// Visit RHS-Depth2: (D && F)
/// LHS RHS
/// ID=2 ID=6
///
/// Visit Depth1:
/// (A && B) || (C && D) || (D && F)
/// ID=1 ID=4 ID=3 ID=5 ID=2 ID=6
///
/// A node ID of '0' always means MC/DC isn't being tracked.
///
/// As the AST walk proceeds recursively, the algorithm will also use a stack
/// to track the IDs of logical-AND and logical-OR operations on the RHS so
/// that it can be determined which nodes are executed next, depending on how
/// a LHS or RHS of a logical-AND or logical-OR is evaluated. This
/// information relies on the assigned IDs and are embedded within the
/// coverage region IDs of each branch region associated with a leaf-level
/// condition. This information helps the visualization tool reconstruct all
/// possible test vectors for the purposes of MC/DC analysis. If a "next" node
/// ID is '0', it means it's the end of the test vector. The following rules
/// are used:
///
/// For logical-AND ("LHS && RHS"):
/// - If LHS is TRUE, execution goes to the RHS node.
/// - If LHS is FALSE, execution goes to the LHS node of the next logical-OR.
/// If that does not exist, execution exits (ID == 0).
///
/// - If RHS is TRUE, execution goes to LHS node of the next logical-AND.
/// If that does not exist, execution exits (ID == 0).
/// - If RHS is FALSE, execution goes to the LHS node of the next logical-OR.
/// If that does not exist, execution exits (ID == 0).
///
/// For logical-OR ("LHS || RHS"):
/// - If LHS is TRUE, execution goes to the LHS node of the next logical-AND.
/// If that does not exist, execution exits (ID == 0).
/// - If LHS is FALSE, execution goes to the RHS node.
///
/// - If RHS is TRUE, execution goes to LHS node of the next logical-AND.
/// If that does not exist, execution exits (ID == 0).
/// - If RHS is FALSE, execution goes to the LHS node of the next logical-OR.
/// If that does not exist, execution exits (ID == 0).
///
/// Finally, the condition IDs are also used when instrumenting the code to
/// indicate a unique offset into a temporary bitmap that represents the true
/// or false evaluation of that particular condition.
///
/// NOTE regarding the use of CodeGenFunction::stripCond(). Even though, for
/// simplicity, parentheses and unary logical-NOT operators are considered
/// part of their underlying condition for both MC/DC and branch coverage, the
/// condition IDs themselves are assigned and tracked using the underlying
/// condition itself. This is done solely for consistency since parentheses
/// and logical-NOTs are ignored when checking whether the condition is
/// actually an instrumentable condition. This can also make debugging a bit
/// easier.
private:
CodeGenModule &CGM;
llvm::SmallVector<mcdc::ConditionIDs> DecisionStack;
MCDC::State &MCDCState;
mcdc::ConditionID NextID = 0;
bool NotMapped = false;
/// Represent a sentinel value as a pair of final decisions for the bottom
// of DecisionStack.
static constexpr mcdc::ConditionIDs DecisionStackSentinel{-1, -1};
/// Is this a logical-AND operation?
bool isLAnd(const BinaryOperator *E) const {
return E->getOpcode() == BO_LAnd;
}
public:
MCDCCoverageBuilder(CodeGenModule &CGM, MCDC::State &MCDCState)
: CGM(CGM), DecisionStack(1, DecisionStackSentinel),
MCDCState(MCDCState) {}
/// Return whether the build of the control flow map is at the top-level
/// (root) of a logical operator nest in a boolean expression prior to the
/// assignment of condition IDs.
bool isIdle() const { return (NextID == 0 && !NotMapped); }
/// Return whether any IDs have been assigned in the build of the control
/// flow map, indicating that the map is being generated for this boolean
/// expression.
bool isBuilding() const { return (NextID > 0); }
/// Set the given condition's ID.
void setCondID(const Expr *Cond, mcdc::ConditionID ID) {
MCDCState.BranchByStmt[CodeGenFunction::stripCond(Cond)].ID = ID;
}
/// Return the ID of a given condition.
mcdc::ConditionID getCondID(const Expr *Cond) const {
auto I = MCDCState.BranchByStmt.find(CodeGenFunction::stripCond(Cond));
if (I == MCDCState.BranchByStmt.end())
return -1;
else
return I->second.ID;
}
/// Return the LHS Decision ([0,0] if not set).
const mcdc::ConditionIDs &back() const { return DecisionStack.back(); }
/// Push the binary operator statement to track the nest level and assign IDs
/// to the operator's LHS and RHS. The RHS may be a larger subtree that is
/// broken up on successive levels.
void pushAndAssignIDs(const BinaryOperator *E) {
if (!CGM.getCodeGenOpts().MCDCCoverage)
return;
// If binary expression is disqualified, don't do mapping.
if (!isBuilding() &&
!MCDCState.DecisionByStmt.contains(CodeGenFunction::stripCond(E)))
NotMapped = true;
// Don't go any further if we don't need to map condition IDs.
if (NotMapped)
return;
const mcdc::ConditionIDs &ParentDecision = DecisionStack.back();
// If the operator itself has an assigned ID, this means it represents a
// larger subtree. In this case, assign that ID to its LHS node. Its RHS
// will receive a new ID below. Otherwise, assign ID+1 to LHS.
if (MCDCState.BranchByStmt.contains(CodeGenFunction::stripCond(E)))
setCondID(E->getLHS(), getCondID(E));
else
setCondID(E->getLHS(), NextID++);
// Assign a ID+1 for the RHS.
mcdc::ConditionID RHSid = NextID++;
setCondID(E->getRHS(), RHSid);
// Push the LHS decision IDs onto the DecisionStack.
if (isLAnd(E))
DecisionStack.push_back({ParentDecision[false], RHSid});
else
DecisionStack.push_back({RHSid, ParentDecision[true]});
}
/// Pop and return the LHS Decision ([0,0] if not set).
mcdc::ConditionIDs pop() {
if (!CGM.getCodeGenOpts().MCDCCoverage || NotMapped)
return DecisionStackSentinel;
assert(DecisionStack.size() > 1);
return DecisionStack.pop_back_val();
}
/// Return the total number of conditions and reset the state. The number of
/// conditions is zero if the expression isn't mapped.
unsigned getTotalConditionsAndReset(const BinaryOperator *E) {
if (!CGM.getCodeGenOpts().MCDCCoverage)
return 0;
assert(!isIdle());
assert(DecisionStack.size() == 1);
// Reset state if not doing mapping.
if (NotMapped) {
NotMapped = false;
assert(NextID == 0);
return 0;
}
// Set number of conditions and reset.
unsigned TotalConds = NextID;
// Reset ID back to beginning.
NextID = 0;
return TotalConds;
}
};
/// A StmtVisitor that creates coverage mapping regions which map
/// from the source code locations to the PGO counters.
struct CounterCoverageMappingBuilder
: public CoverageMappingBuilder,
public ConstStmtVisitor<CounterCoverageMappingBuilder> {
/// The map of statements to count values.
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
MCDC::State &MCDCState;
/// A stack of currently live regions.
llvm::SmallVector<SourceMappingRegion> RegionStack;
/// An object to manage MCDC regions.
MCDCCoverageBuilder MCDCBuilder;
CounterExpressionBuilder Builder;
/// A location in the most recently visited file or macro.
///
/// This is used to adjust the active source regions appropriately when
/// expressions cross file or macro boundaries.
SourceLocation MostRecentLocation;
/// Whether the visitor at a terminate statement.
bool HasTerminateStmt = false;
/// Gap region counter after terminate statement.
Counter GapRegionCounter;
/// Return a counter for the subtraction of \c RHS from \c LHS
Counter subtractCounters(Counter LHS, Counter RHS, bool Simplify = true) {
assert(!llvm::EnableSingleByteCoverage &&
"cannot add counters when single byte coverage mode is enabled");
return Builder.subtract(LHS, RHS, Simplify);
}
/// Return a counter for the sum of \c LHS and \c RHS.
Counter addCounters(Counter LHS, Counter RHS, bool Simplify = true) {
assert(!llvm::EnableSingleByteCoverage &&
"cannot add counters when single byte coverage mode is enabled");
return Builder.add(LHS, RHS, Simplify);
}
Counter addCounters(Counter C1, Counter C2, Counter C3,
bool Simplify = true) {
assert(!llvm::EnableSingleByteCoverage &&
"cannot add counters when single byte coverage mode is enabled");
return addCounters(addCounters(C1, C2, Simplify), C3, Simplify);
}
/// Return the region counter for the given statement.
///
/// This should only be called on statements that have a dedicated counter.
Counter getRegionCounter(const Stmt *S) {
return Counter::getCounter(CounterMap[S]);
}
/// Push a region onto the stack.
///
/// Returns the index on the stack where the region was pushed. This can be
/// used with popRegions to exit a "scope", ending the region that was pushed.
size_t pushRegion(Counter Count,
std::optional<SourceLocation> StartLoc = std::nullopt,
std::optional<SourceLocation> EndLoc = std::nullopt,
std::optional<Counter> FalseCount = std::nullopt,
const mcdc::Parameters &BranchParams = std::monostate()) {
if (StartLoc && !FalseCount) {
MostRecentLocation = *StartLoc;
}
// If either of these locations is invalid, something elsewhere in the
// compiler has broken.
assert((!StartLoc || StartLoc->isValid()) && "Start location is not valid");
assert((!EndLoc || EndLoc->isValid()) && "End location is not valid");
// However, we can still recover without crashing.
// If either location is invalid, set it to std::nullopt to avoid
// letting users of RegionStack think that region has a valid start/end
// location.
if (StartLoc && StartLoc->isInvalid())
StartLoc = std::nullopt;
if (EndLoc && EndLoc->isInvalid())
EndLoc = std::nullopt;
RegionStack.emplace_back(Count, FalseCount, BranchParams, StartLoc, EndLoc);
return RegionStack.size() - 1;
}
size_t pushRegion(const mcdc::DecisionParameters &DecisionParams,
std::optional<SourceLocation> StartLoc = std::nullopt,
std::optional<SourceLocation> EndLoc = std::nullopt) {
RegionStack.emplace_back(DecisionParams, StartLoc, EndLoc);
return RegionStack.size() - 1;
}
size_t locationDepth(SourceLocation Loc) {
size_t Depth = 0;
while (Loc.isValid()) {
Loc = getIncludeOrExpansionLoc(Loc);
Depth++;
}
return Depth;
}
/// Pop regions from the stack into the function's list of regions.
///
/// Adds all regions from \c ParentIndex to the top of the stack to the
/// function's \c SourceRegions.
void popRegions(size_t ParentIndex) {
assert(RegionStack.size() >= ParentIndex && "parent not in stack");
while (RegionStack.size() > ParentIndex) {
SourceMappingRegion &Region = RegionStack.back();
if (Region.hasStartLoc() &&
(Region.hasEndLoc() || RegionStack[ParentIndex].hasEndLoc())) {
SourceLocation StartLoc = Region.getBeginLoc();
SourceLocation EndLoc = Region.hasEndLoc()
? Region.getEndLoc()
: RegionStack[ParentIndex].getEndLoc();
bool isBranch = Region.isBranch();
size_t StartDepth = locationDepth(StartLoc);
size_t EndDepth = locationDepth(EndLoc);
while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
bool UnnestStart = StartDepth >= EndDepth;
bool UnnestEnd = EndDepth >= StartDepth;
if (UnnestEnd) {
// The region ends in a nested file or macro expansion. If the
// region is not a branch region, create a separate region for each
// expansion, and for all regions, update the EndLoc. Branch
// regions should not be split in order to keep a straightforward
// correspondance between the region and its associated branch
// condition, even if the condition spans multiple depths.
SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
if (!isBranch && !isRegionAlreadyAdded(NestedLoc, EndLoc))
SourceRegions.emplace_back(Region.getCounter(), NestedLoc,
EndLoc);
EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
if (EndLoc.isInvalid())
llvm::report_fatal_error(
"File exit not handled before popRegions");
EndDepth--;
}
if (UnnestStart) {
// The region ends in a nested file or macro expansion. If the
// region is not a branch region, create a separate region for each
// expansion, and for all regions, update the StartLoc. Branch
// regions should not be split in order to keep a straightforward
// correspondance between the region and its associated branch
// condition, even if the condition spans multiple depths.
SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc);
assert(SM.isWrittenInSameFile(StartLoc, NestedLoc));
if (!isBranch && !isRegionAlreadyAdded(StartLoc, NestedLoc))
SourceRegions.emplace_back(Region.getCounter(), StartLoc,
NestedLoc);
StartLoc = getIncludeOrExpansionLoc(StartLoc);
if (StartLoc.isInvalid())
llvm::report_fatal_error(
"File exit not handled before popRegions");
StartDepth--;
}
}
Region.setStartLoc(StartLoc);
Region.setEndLoc(EndLoc);
if (!isBranch) {
MostRecentLocation = EndLoc;
// If this region happens to span an entire expansion, we need to
// make sure we don't overlap the parent region with it.
if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
EndLoc == getEndOfFileOrMacro(EndLoc))
MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
}
assert(SM.isWrittenInSameFile(Region.getBeginLoc(), EndLoc));
assert(SpellingRegion(SM, Region).isInSourceOrder());
SourceRegions.push_back(Region);
}
RegionStack.pop_back();
}
}
/// Return the currently active region.