This repository was archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathCoverageMappingGen.cpp
1440 lines (1219 loc) · 51.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++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Instrumentation-based code coverage mapping generator
//
//===----------------------------------------------------------------------===//
#include "CoverageMappingGen.h"
#include "CodeGenFunction.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Optional.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"
using namespace clang;
using namespace CodeGen;
using namespace llvm::coverage;
void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range, SourceLocation) {
SkippedRanges.push_back(Range);
}
namespace {
/// \brief A region of source code that can be mapped to a counter.
class SourceMappingRegion {
Counter Count;
/// \brief The region's starting location.
Optional<SourceLocation> LocStart;
/// \brief The region's ending location.
Optional<SourceLocation> LocEnd;
/// Whether this region should be emitted after its parent is emitted.
bool DeferRegion;
/// 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;
public:
SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart,
Optional<SourceLocation> LocEnd, bool DeferRegion = false,
bool GapRegion = false)
: Count(Count), LocStart(LocStart), LocEnd(LocEnd),
DeferRegion(DeferRegion), GapRegion(GapRegion) {}
const Counter &getCounter() const { return Count; }
void setCounter(Counter C) { Count = C; }
bool hasStartLoc() const { return LocStart.hasValue(); }
void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
SourceLocation getStartLoc() const {
assert(LocStart && "Region has no start location");
return *LocStart;
}
bool hasEndLoc() const { return LocEnd.hasValue(); }
void setEndLoc(SourceLocation Loc) { LocEnd = Loc; }
SourceLocation getEndLoc() const {
assert(LocEnd && "Region has no end location");
return *LocEnd;
}
bool isDeferred() const { return DeferRegion; }
void setDeferred(bool Deferred) { DeferRegion = Deferred; }
bool isGap() const { return GapRegion; }
void setGap(bool Gap) { GapRegion = Gap; }
};
/// 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.getStartLoc(), 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);
}
};
/// \brief Provides the common functionality for the different
/// coverage mapping region builders.
class CoverageMappingBuilder {
public:
CoverageMappingModuleGen &CVM;
SourceManager &SM;
const LangOptions &LangOpts;
private:
/// \brief Map of clang's FileIDs to IDs used for coverage mapping.
llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
FileIDMapping;
public:
/// \brief The coverage mapping regions for this function
llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
/// \brief The source mapping regions for this function.
std::vector<SourceMappingRegion> SourceRegions;
/// \brief 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) {}
/// \brief 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);
}
/// \brief 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));
}
/// \brief 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));
}
/// \brief Find out where the current file is included or macro is expanded.
SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).first
: SM.getIncludeLoc(SM.getFileID(Loc));
}
/// \brief 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>";
}
/// \brief 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;
}
/// \brief Get the start of \c S ignoring macro arguments and builtin macros.
SourceLocation getStart(const Stmt *S) {
SourceLocation Loc = S->getLocStart();
while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
Loc = SM.getImmediateExpansionRange(Loc).first;
return Loc;
}
/// \brief Get the end of \c S ignoring macro arguments and builtin macros.
SourceLocation getEnd(const Stmt *S) {
SourceLocation Loc = S->getLocEnd();
while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
Loc = SM.getImmediateExpansionRange(Loc).first;
return getPreciseTokenLocEnd(Loc);
}
/// \brief 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.getStartLoc();
FileID File = SM.getFileID(Loc);
if (!Visited.insert(File).second)
continue;
// Do not map FileID's associated with system headers.
if (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));
}
std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second());
for (const auto &FL : FileLocs) {
SourceLocation Loc = FL.first;
FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
auto Entry = SM.getFileEntryForID(SpellingFile);
if (!Entry)
continue;
FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
Mapping.push_back(CVM.getFileID(Entry));
}
}
/// \brief Get the coverage mapping file ID for \c Loc.
///
/// If such file id doesn't exist, return None.
Optional<unsigned> getCoverageFileID(SourceLocation Loc) {
auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
if (Mapping != FileIDMapping.end())
return Mapping->second.first;
return None;
}
/// \brief Gather all the regions that were skipped by the preprocessor
/// using the constructs like #if.
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 (const auto &I : SkippedRanges) {
auto LocStart = I.getBegin();
auto LocEnd = I.getEnd();
assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
"region spans multiple files");
auto CovFileID = getCoverageFileID(LocStart);
if (!CovFileID)
continue;
SpellingRegion SR{SM, LocStart, LocEnd};
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 souce code of this function.
if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
Region.LineEnd <= FileLineRanges[*CovFileID].second)
MappingRegions.push_back(Region);
}
}
/// \brief 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.getStartLoc();
assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
// Ignore regions from system headers.
if (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 {
MappingRegions.push_back(CounterMappingRegion::makeRegion(
Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
SR.LineEnd, SR.ColumnEnd));
}
}
}
/// \brief 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;
}
};
/// \brief 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);
}
/// \brief 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, None, MappingRegions);
Writer.write(OS);
}
};
/// \brief 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> {
/// \brief The map of statements to count values.
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
/// \brief A stack of currently live regions.
std::vector<SourceMappingRegion> RegionStack;
/// The currently deferred region: its end location and count can be set once
/// its parent has been popped from the region stack.
Optional<SourceMappingRegion> DeferredRegion;
CounterExpressionBuilder Builder;
/// \brief 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;
/// Location of the last terminated region.
Optional<std::pair<SourceLocation, size_t>> LastTerminatedRegion;
/// \brief Return a counter for the subtraction of \c RHS from \c LHS
Counter subtractCounters(Counter LHS, Counter RHS) {
return Builder.subtract(LHS, RHS);
}
/// \brief Return a counter for the sum of \c LHS and \c RHS.
Counter addCounters(Counter LHS, Counter RHS) {
return Builder.add(LHS, RHS);
}
Counter addCounters(Counter C1, Counter C2, Counter C3) {
return addCounters(addCounters(C1, C2), C3);
}
/// \brief 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]);
}
/// \brief 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, Optional<SourceLocation> StartLoc = None,
Optional<SourceLocation> EndLoc = None) {
if (StartLoc) {
MostRecentLocation = *StartLoc;
completeDeferred(Count, MostRecentLocation);
}
RegionStack.emplace_back(Count, StartLoc, EndLoc);
return RegionStack.size() - 1;
}
/// Complete any pending deferred region by setting its end location and
/// count, and then pushing it onto the region stack.
size_t completeDeferred(Counter Count, SourceLocation DeferredEndLoc) {
size_t Index = RegionStack.size();
if (!DeferredRegion)
return Index;
// Consume the pending region.
SourceMappingRegion DR = DeferredRegion.getValue();
DeferredRegion = None;
// If the region ends in an expansion, find the expansion site.
FileID StartFile = SM.getFileID(DR.getStartLoc());
if (SM.getFileID(DeferredEndLoc) != StartFile) {
if (isNestedIn(DeferredEndLoc, StartFile)) {
do {
DeferredEndLoc = getIncludeOrExpansionLoc(DeferredEndLoc);
} while (StartFile != SM.getFileID(DeferredEndLoc));
} else {
return Index;
}
}
// The parent of this deferred region ends where the containing decl ends,
// so the region isn't useful.
if (DR.getStartLoc() == DeferredEndLoc)
return Index;
// If we're visiting statements in non-source order (e.g switch cases or
// a loop condition) we can't construct a sensible deferred region.
if (!SpellingRegion(SM, DR.getStartLoc(), DeferredEndLoc).isInSourceOrder())
return Index;
DR.setGap(true);
DR.setCounter(Count);
DR.setEndLoc(DeferredEndLoc);
handleFileExit(DeferredEndLoc);
RegionStack.push_back(DR);
return Index;
}
/// Complete a deferred region created after a terminated region at the
/// top-level.
void completeTopLevelDeferredRegion(Counter Count,
SourceLocation DeferredEndLoc) {
if (DeferredRegion || !LastTerminatedRegion)
return;
if (LastTerminatedRegion->second != RegionStack.size())
return;
SourceLocation Start = LastTerminatedRegion->first;
if (SM.getFileID(Start) != SM.getMainFileID())
return;
SourceMappingRegion DR = RegionStack.back();
DR.setStartLoc(Start);
DR.setDeferred(false);
DeferredRegion = DR;
completeDeferred(Count, DeferredEndLoc);
}
/// \brief 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");
bool ParentOfDeferredRegion = false;
while (RegionStack.size() > ParentIndex) {
SourceMappingRegion &Region = RegionStack.back();
if (Region.hasStartLoc()) {
SourceLocation StartLoc = Region.getStartLoc();
SourceLocation EndLoc = Region.hasEndLoc()
? Region.getEndLoc()
: RegionStack[ParentIndex].getEndLoc();
while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
// The region ends in a nested file or macro expansion. Create a
// separate region for each expansion.
SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
if (!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");
}
Region.setEndLoc(EndLoc);
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.getStartLoc(), EndLoc));
assert(SpellingRegion(SM, Region).isInSourceOrder());
SourceRegions.push_back(Region);
if (ParentOfDeferredRegion) {
ParentOfDeferredRegion = false;
// If there's an existing deferred region, keep the old one, because
// it means there are two consecutive returns (or a similar pattern).
if (!DeferredRegion.hasValue() &&
// File IDs aren't gathered within macro expansions, so it isn't
// useful to try and create a deferred region inside of one.
!EndLoc.isMacroID())
DeferredRegion =
SourceMappingRegion(Counter::getZero(), EndLoc, None);
}
} else if (Region.isDeferred()) {
assert(!ParentOfDeferredRegion && "Consecutive deferred regions");
ParentOfDeferredRegion = true;
}
RegionStack.pop_back();
// If the zero region pushed after the last terminated region no longer
// exists, clear its cached information.
if (LastTerminatedRegion &&
RegionStack.size() < LastTerminatedRegion->second)
LastTerminatedRegion = None;
}
assert(!ParentOfDeferredRegion && "Deferred region with no parent");
}
/// \brief Return the currently active region.
SourceMappingRegion &getRegion() {
assert(!RegionStack.empty() && "statement has no region");
return RegionStack.back();
}
/// \brief Propagate counts through the children of \c S.
Counter propagateCounts(Counter TopCount, const Stmt *S) {
SourceLocation StartLoc = getStart(S);
SourceLocation EndLoc = getEnd(S);
size_t Index = pushRegion(TopCount, StartLoc, EndLoc);
Visit(S);
Counter ExitCount = getRegion().getCounter();
popRegions(Index);
// The statement may be spanned by an expansion. Make sure we handle a file
// exit out of this expansion before moving to the next statement.
if (SM.isBeforeInTranslationUnit(StartLoc, S->getLocStart()))
MostRecentLocation = EndLoc;
return ExitCount;
}
/// \brief Check whether a region with bounds \c StartLoc and \c EndLoc
/// is already added to \c SourceRegions.
bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc) {
return SourceRegions.rend() !=
std::find_if(SourceRegions.rbegin(), SourceRegions.rend(),
[&](const SourceMappingRegion &Region) {
return Region.getStartLoc() == StartLoc &&
Region.getEndLoc() == EndLoc;
});
}
/// \brief Adjust the most recently visited location to \c EndLoc.
///
/// This should be used after visiting any statements in non-source order.
void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
MostRecentLocation = EndLoc;
// The code region for a whole macro is created in handleFileExit() when
// it detects exiting of the virtual file of that macro. If we visited
// statements in non-source order, we might already have such a region
// added, for example, if a body of a loop is divided among multiple
// macros. Avoid adding duplicate regions in such case.
if (getRegion().hasEndLoc() &&
MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) &&
isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
MostRecentLocation))
MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
}
/// \brief Adjust regions and state when \c NewLoc exits a file.
///
/// If moving from our most recently tracked location to \c NewLoc exits any
/// files, this adjusts our current region stack and creates the file regions
/// for the exited file.
void handleFileExit(SourceLocation NewLoc) {
if (NewLoc.isInvalid() ||
SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
return;
// If NewLoc is not in a file that contains MostRecentLocation, walk up to
// find the common ancestor.
SourceLocation LCA = NewLoc;
FileID ParentFile = SM.getFileID(LCA);
while (!isNestedIn(MostRecentLocation, ParentFile)) {
LCA = getIncludeOrExpansionLoc(LCA);
if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
// Since there isn't a common ancestor, no file was exited. We just need
// to adjust our location to the new file.
MostRecentLocation = NewLoc;
return;
}
ParentFile = SM.getFileID(LCA);
}
llvm::SmallSet<SourceLocation, 8> StartLocs;
Optional<Counter> ParentCounter;
for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
if (!I.hasStartLoc())
continue;
SourceLocation Loc = I.getStartLoc();
if (!isNestedIn(Loc, ParentFile)) {
ParentCounter = I.getCounter();
break;
}
while (!SM.isInFileID(Loc, ParentFile)) {
// The most nested region for each start location is the one with the
// correct count. We avoid creating redundant regions by stopping once
// we've seen this region.
if (StartLocs.insert(Loc).second)
SourceRegions.emplace_back(I.getCounter(), Loc,
getEndOfFileOrMacro(Loc));
Loc = getIncludeOrExpansionLoc(Loc);
}
I.setStartLoc(getPreciseTokenLocEnd(Loc));
}
if (ParentCounter) {
// If the file is contained completely by another region and doesn't
// immediately start its own region, the whole file gets a region
// corresponding to the parent.
SourceLocation Loc = MostRecentLocation;
while (isNestedIn(Loc, ParentFile)) {
SourceLocation FileStart = getStartOfFileOrMacro(Loc);
if (StartLocs.insert(FileStart).second) {
SourceRegions.emplace_back(*ParentCounter, FileStart,
getEndOfFileOrMacro(Loc));
assert(SpellingRegion(SM, SourceRegions.back()).isInSourceOrder());
}
Loc = getIncludeOrExpansionLoc(Loc);
}
}
MostRecentLocation = NewLoc;
}
/// \brief Ensure that \c S is included in the current region.
void extendRegion(const Stmt *S) {
SourceMappingRegion &Region = getRegion();
SourceLocation StartLoc = getStart(S);
handleFileExit(StartLoc);
if (!Region.hasStartLoc())
Region.setStartLoc(StartLoc);
completeDeferred(Region.getCounter(), StartLoc);
}
/// \brief Mark \c S as a terminator, starting a zero region.
void terminateRegion(const Stmt *S) {
extendRegion(S);
SourceMappingRegion &Region = getRegion();
SourceLocation EndLoc = getEnd(S);
if (!Region.hasEndLoc())
Region.setEndLoc(EndLoc);
pushRegion(Counter::getZero());
auto &ZeroRegion = getRegion();
ZeroRegion.setDeferred(true);
LastTerminatedRegion = {EndLoc, RegionStack.size()};
}
/// Find a valid gap range between \p AfterLoc and \p BeforeLoc.
Optional<SourceRange> findGapAreaBetween(SourceLocation AfterLoc,
SourceLocation BeforeLoc) {
// If the start and end locations of the gap are both within the same macro
// file, the range may not be in source order.
if (AfterLoc.isMacroID() || BeforeLoc.isMacroID())
return None;
if (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc))
return None;
return {{AfterLoc, BeforeLoc}};
}
/// Find the source range after \p AfterStmt and before \p BeforeStmt.
Optional<SourceRange> findGapAreaBetween(const Stmt *AfterStmt,
const Stmt *BeforeStmt) {
return findGapAreaBetween(getPreciseTokenLocEnd(getEnd(AfterStmt)),
getStart(BeforeStmt));
}
/// Emit a gap region between \p StartLoc and \p EndLoc with the given count.
void fillGapAreaWithCount(SourceLocation StartLoc, SourceLocation EndLoc,
Counter Count) {
if (StartLoc == EndLoc)
return;
assert(SpellingRegion(SM, StartLoc, EndLoc).isInSourceOrder());
handleFileExit(StartLoc);
size_t Index = pushRegion(Count, StartLoc, EndLoc);
getRegion().setGap(true);
handleFileExit(EndLoc);
popRegions(Index);
}
/// \brief Keep counts of breaks and continues inside loops.
struct BreakContinue {
Counter BreakCount;
Counter ContinueCount;
};
SmallVector<BreakContinue, 8> BreakContinueStack;
CounterCoverageMappingBuilder(
CoverageMappingModuleGen &CVM,
llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
const LangOptions &LangOpts)
: CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
DeferredRegion(None) {}
/// \brief Write the mapping data to the output stream
void write(llvm::raw_ostream &OS) {
llvm::SmallVector<unsigned, 8> VirtualFileMapping;
gatherFileIDs(VirtualFileMapping);
SourceRegionFilter Filter = emitExpansionRegions();
assert(!DeferredRegion && "Deferred region never completed");
emitSourceRegions(Filter);
gatherSkippedRegions();
if (MappingRegions.empty())
return;
CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
MappingRegions);
Writer.write(OS);
}
void VisitStmt(const Stmt *S) {
if (S->getLocStart().isValid())
extendRegion(S);
for (const Stmt *Child : S->children())
if (Child)
this->Visit(Child);
handleFileExit(getEnd(S));
}
/// Determine whether the final deferred region emitted in \p Body should be
/// discarded.
static bool discardFinalDeferredRegionInDecl(Stmt *Body) {
if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
Stmt *LastStmt = CS->body_back();
if (auto *IfElse = dyn_cast<IfStmt>(LastStmt)) {
if (auto *Else = dyn_cast_or_null<CompoundStmt>(IfElse->getElse()))
LastStmt = Else->body_back();
else
LastStmt = IfElse->getElse();
}
return dyn_cast_or_null<ReturnStmt>(LastStmt);
}
return false;
}
void VisitDecl(const Decl *D) {
assert(!DeferredRegion && "Deferred region never completed");
Stmt *Body = D->getBody();
// Do not propagate region counts into system headers.
if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
return;
Counter ExitCount = propagateCounts(getRegionCounter(Body), Body);
assert(RegionStack.empty() && "Regions entered but never exited");
if (DeferredRegion) {
// Complete (or discard) any deferred regions introduced by the last
// statement.
if (discardFinalDeferredRegionInDecl(Body))
DeferredRegion = None;
else
popRegions(completeDeferred(ExitCount, getEnd(Body)));
}
}
void VisitReturnStmt(const ReturnStmt *S) {
extendRegion(S);
if (S->getRetValue())
Visit(S->getRetValue());
terminateRegion(S);
}
void VisitCXXThrowExpr(const CXXThrowExpr *E) {
extendRegion(E);
if (E->getSubExpr())
Visit(E->getSubExpr());
terminateRegion(E);
}
void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
void VisitLabelStmt(const LabelStmt *S) {
Counter LabelCount = getRegionCounter(S);
SourceLocation Start = getStart(S);
completeTopLevelDeferredRegion(LabelCount, Start);
// We can't extendRegion here or we risk overlapping with our new region.
handleFileExit(Start);
pushRegion(LabelCount, Start);
Visit(S->getSubStmt());
}
void VisitBreakStmt(const BreakStmt *S) {
assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
BreakContinueStack.back().BreakCount = addCounters(
BreakContinueStack.back().BreakCount, getRegion().getCounter());
// FIXME: a break in a switch should terminate regions for all preceding
// case statements, not just the most recent one.
terminateRegion(S);
}
void VisitContinueStmt(const ContinueStmt *S) {
assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
BreakContinueStack.back().ContinueCount = addCounters(
BreakContinueStack.back().ContinueCount, getRegion().getCounter());
terminateRegion(S);
}
void VisitCallExpr(const CallExpr *E) {
VisitStmt(E);
// Terminate the region when we hit a noreturn function.
// (This is helpful dealing with switch statements.)
QualType CalleeType = E->getCallee()->getType();
if (getFunctionExtInfo(*CalleeType).getNoReturn())
terminateRegion(E);
}
void VisitWhileStmt(const WhileStmt *S) {
extendRegion(S);
Counter ParentCount = getRegion().getCounter();
Counter BodyCount = getRegionCounter(S);
// Handle the body first so that we can get the backedge count.
BreakContinueStack.push_back(BreakContinue());
extendRegion(S->getBody());
Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
BreakContinue BC = BreakContinueStack.pop_back_val();
// Go back to handle the condition.
Counter CondCount =
addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
propagateCounts(CondCount, S->getCond());
adjustForOutOfOrderTraversal(getEnd(S));
// The body count applies to the area immediately after the increment.
auto Gap = findGapAreaBetween(S->getCond(), S->getBody());
if (Gap)
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
Counter OutCount =
addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
if (OutCount != ParentCount)
pushRegion(OutCount);
}
void VisitDoStmt(const DoStmt *S) {
extendRegion(S);
Counter ParentCount = getRegion().getCounter();
Counter BodyCount = getRegionCounter(S);
BreakContinueStack.push_back(BreakContinue());
extendRegion(S->getBody());
Counter BackedgeCount =
propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
BreakContinue BC = BreakContinueStack.pop_back_val();
Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
propagateCounts(CondCount, S->getCond());
Counter OutCount =
addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
if (OutCount != ParentCount)
pushRegion(OutCount);
}
void VisitForStmt(const ForStmt *S) {
extendRegion(S);
if (S->getInit())
Visit(S->getInit());
Counter ParentCount = getRegion().getCounter();
Counter BodyCount = getRegionCounter(S);
// Handle the body first so that we can get the backedge count.
BreakContinueStack.push_back(BreakContinue());
extendRegion(S->getBody());
Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
BreakContinue BC = BreakContinueStack.pop_back_val();
// The increment is essentially part of the body but it needs to include
// the count for all the continue statements.
if (const Stmt *Inc = S->getInc())
propagateCounts(addCounters(BackedgeCount, BC.ContinueCount), Inc);
// Go back to handle the condition.
Counter CondCount =
addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
if (const Expr *Cond = S->getCond()) {
propagateCounts(CondCount, Cond);
adjustForOutOfOrderTraversal(getEnd(S));
}