Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- 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
// Instrumentation-based code coverage mapping generator
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CoverageMappingGen.h"
14
#include "CodeGenFunction.h"
15
#include "clang/AST/StmtVisitor.h"
16
#include "clang/Basic/Diagnostic.h"
17
#include "clang/Basic/FileManager.h"
18
#include "clang/Frontend/FrontendDiagnostic.h"
19
#include "clang/Lex/Lexer.h"
20
#include "llvm/ADT/Optional.h"
21
#include "llvm/ADT/SmallSet.h"
22
#include "llvm/ADT/StringExtras.h"
23
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
24
#include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
25
#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
26
#include "llvm/ProfileData/InstrProfReader.h"
27
#include "llvm/Support/FileSystem.h"
28
#include "llvm/Support/Path.h"
29
30
// This selects the coverage mapping format defined when `InstrProfData.inc`
31
// is textually included.
32
#define COVMAP_V3
33
34
static llvm::cl::opt<bool> EmptyLineCommentCoverage(
35
    "emptyline-comment-coverage",
36
    llvm::cl::desc("Emit emptylines and comment lines as skipped regions (only "
37
                   "disable it on test)"),
38
    llvm::cl::init(true), llvm::cl::Hidden);
39
40
using namespace clang;
41
using namespace CodeGen;
42
using namespace llvm::coverage;
43
44
CoverageSourceInfo *
45
80
CoverageMappingModuleGen::setUpCoverageCallbacks(Preprocessor &PP) {
46
80
  CoverageSourceInfo *CoverageInfo =
47
80
      new CoverageSourceInfo(PP.getSourceManager());
48
80
  PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(CoverageInfo));
49
80
  if (EmptyLineCommentCoverage) {
50
24
    PP.addCommentHandler(CoverageInfo);
51
24
    PP.setEmptylineHandler(CoverageInfo);
52
24
    PP.setPreprocessToken(true);
53
37.6k
    PP.setTokenWatcher([CoverageInfo](clang::Token Tok) {
54
      // Update previous token location.
55
37.6k
      CoverageInfo->PrevTokLoc = Tok.getLocation();
56
37.6k
      if (Tok.getKind() != clang::tok::eod)
57
29.3k
        CoverageInfo->updateNextTokLoc(Tok.getLocation());
58
37.6k
    });
59
24
  }
60
80
  return CoverageInfo;
61
80
}
62
63
832
void CoverageSourceInfo::AddSkippedRange(SourceRange Range) {
64
832
  if (EmptyLineCommentCoverage && 
!SkippedRanges.empty()819
&&
65
832
      
PrevTokLoc == SkippedRanges.back().PrevTokLoc800
&&
66
832
      SourceMgr.isWrittenInSameFile(SkippedRanges.back().Range.getEnd(),
67
592
                                    Range.getBegin()))
68
592
    SkippedRanges.back().Range.setEnd(Range.getEnd());
69
240
  else
70
240
    SkippedRanges.push_back({Range, PrevTokLoc});
71
832
}
72
73
33
void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range, SourceLocation) {
74
33
  AddSkippedRange(Range);
75
33
}
76
77
184
void CoverageSourceInfo::HandleEmptyline(SourceRange Range) {
78
184
  AddSkippedRange(Range);
79
184
}
80
81
615
bool CoverageSourceInfo::HandleComment(Preprocessor &PP, SourceRange Range) {
82
615
  AddSkippedRange(Range);
83
615
  return false;
84
615
}
85
86
29.3k
void CoverageSourceInfo::updateNextTokLoc(SourceLocation Loc) {
87
29.3k
  if (!SkippedRanges.empty() && 
SkippedRanges.back().NextTokLoc.isInvalid()3.17k
)
88
212
    SkippedRanges.back().NextTokLoc = Loc;
89
29.3k
}
90
91
namespace {
92
93
/// A region of source code that can be mapped to a counter.
94
class SourceMappingRegion {
95
  /// Primary Counter that is also used for Branch Regions for "True" branches.
96
  Counter Count;
97
98
  /// Secondary Counter used for Branch Regions for "False" branches.
99
  Optional<Counter> FalseCount;
100
101
  /// The region's starting location.
102
  Optional<SourceLocation> LocStart;
103
104
  /// The region's ending location.
105
  Optional<SourceLocation> LocEnd;
106
107
  /// Whether this region is a gap region. The count from a gap region is set
108
  /// as the line execution count if there are no other regions on the line.
109
  bool GapRegion;
110
111
public:
112
  SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart,
113
                      Optional<SourceLocation> LocEnd, bool GapRegion = false)
114
212
      : Count(Count), LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion) {
115
212
  }
116
117
  SourceMappingRegion(Counter Count, Optional<Counter> FalseCount,
118
                      Optional<SourceLocation> LocStart,
119
                      Optional<SourceLocation> LocEnd, bool GapRegion = false)
120
      : Count(Count), FalseCount(FalseCount), LocStart(LocStart),
121
1.98k
        LocEnd(LocEnd), GapRegion(GapRegion) {}
122
123
3.52k
  const Counter &getCounter() const { return Count; }
124
125
369
  const Counter &getFalseCounter() const {
126
369
    assert(FalseCount && "Region has no alternate counter");
127
0
    return *FalseCount;
128
369
  }
129
130
13
  void setCounter(Counter C) { Count = C; }
131
132
6.49k
  bool hasStartLoc() const { return LocStart.hasValue(); }
133
134
1.90k
  void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
135
136
10.0k
  SourceLocation getBeginLoc() const {
137
10.0k
    assert(LocStart && "Region has no start location");
138
0
    return *LocStart;
139
10.0k
  }
140
141
4.04k
  bool hasEndLoc() const { return LocEnd.hasValue(); }
142
143
1.83k
  void setEndLoc(SourceLocation Loc) {
144
1.83k
    assert(Loc.isValid() && "Setting an invalid end location");
145
0
    LocEnd = Loc;
146
1.83k
  }
147
148
5.57k
  SourceLocation getEndLoc() const {
149
5.57k
    assert(LocEnd && "Region has no end location");
150
0
    return *LocEnd;
151
5.57k
  }
152
153
1.92k
  bool isGap() const { return GapRegion; }
154
155
327
  void setGap(bool Gap) { GapRegion = Gap; }
156
157
3.42k
  bool isBranch() const { return FalseCount.hasValue(); }
158
};
159
160
/// Spelling locations for the start and end of a source region.
161
struct SpellingRegion {
162
  /// The line where the region starts.
163
  unsigned LineStart;
164
165
  /// The column where the region starts.
166
  unsigned ColumnStart;
167
168
  /// The line where the region ends.
169
  unsigned LineEnd;
170
171
  /// The column where the region ends.
172
  unsigned ColumnEnd;
173
174
  SpellingRegion(SourceManager &SM, SourceLocation LocStart,
175
5.14k
                 SourceLocation LocEnd) {
176
5.14k
    LineStart = SM.getSpellingLineNumber(LocStart);
177
5.14k
    ColumnStart = SM.getSpellingColumnNumber(LocStart);
178
5.14k
    LineEnd = SM.getSpellingLineNumber(LocEnd);
179
5.14k
    ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
180
5.14k
  }
181
182
  SpellingRegion(SourceManager &SM, SourceMappingRegion &R)
183
1.86k
      : SpellingRegion(SM, R.getBeginLoc(), R.getEndLoc()) {}
184
185
  /// Check if the start and end locations appear in source order, i.e
186
  /// top->bottom, left->right.
187
5.14k
  bool isInSourceOrder() const {
188
5.14k
    return (LineStart < LineEnd) ||
189
5.14k
           
(3.13k
LineStart == LineEnd3.13k
&&
ColumnStart <= ColumnEnd2.96k
);
190
5.14k
  }
191
};
192
193
/// Provides the common functionality for the different
194
/// coverage mapping region builders.
195
class CoverageMappingBuilder {
196
public:
197
  CoverageMappingModuleGen &CVM;
198
  SourceManager &SM;
199
  const LangOptions &LangOpts;
200
201
private:
202
  /// Map of clang's FileIDs to IDs used for coverage mapping.
203
  llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
204
      FileIDMapping;
205
206
public:
207
  /// The coverage mapping regions for this function
208
  llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
209
  /// The source mapping regions for this function.
210
  std::vector<SourceMappingRegion> SourceRegions;
211
212
  /// A set of regions which can be used as a filter.
213
  ///
214
  /// It is produced by emitExpansionRegions() and is used in
215
  /// emitSourceRegions() to suppress producing code regions if
216
  /// the same area is covered by expansion regions.
217
  typedef llvm::SmallSet<std::pair<SourceLocation, SourceLocation>, 8>
218
      SourceRegionFilter;
219
220
  CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
221
                         const LangOptions &LangOpts)
222
249
      : CVM(CVM), SM(SM), LangOpts(LangOpts) {}
223
224
  /// Return the precise end location for the given token.
225
5.49k
  SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
226
    // We avoid getLocForEndOfToken here, because it doesn't do what we want for
227
    // macro locations, which we just treat as expanded files.
228
5.49k
    unsigned TokLen =
229
5.49k
        Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
230
5.49k
    return Loc.getLocWithOffset(TokLen);
231
5.49k
  }
232
233
  /// Return the start location of an included file or expanded macro.
234
1.60k
  SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
235
1.60k
    if (Loc.isMacroID())
236
326
      return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
237
1.27k
    return SM.getLocForStartOfFile(SM.getFileID(Loc));
238
1.60k
  }
239
240
  /// Return the end location of an included file or expanded macro.
241
309
  SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
242
309
    if (Loc.isMacroID())
243
248
      return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
244
248
                                  SM.getFileOffset(Loc));
245
61
    return SM.getLocForEndOfFile(SM.getFileID(Loc));
246
309
  }
247
248
  /// Find out where the current file is included or macro is expanded.
249
7.93k
  SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
250
7.93k
    return Loc.isMacroID() ? 
SM.getImmediateExpansionRange(Loc).getBegin()2.18k
251
7.93k
                           : 
SM.getIncludeLoc(SM.getFileID(Loc))5.74k
;
252
7.93k
  }
253
254
  /// Return true if \c Loc is a location in a built-in macro.
255
11.1k
  bool isInBuiltin(SourceLocation Loc) {
256
11.1k
    return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>";
257
11.1k
  }
258
259
  /// Check whether \c Loc is included or expanded from \c Parent.
260
775
  bool isNestedIn(SourceLocation Loc, FileID Parent) {
261
1.01k
    do {
262
1.01k
      Loc = getIncludeOrExpansionLoc(Loc);
263
1.01k
      if (Loc.isInvalid())
264
448
        return false;
265
1.01k
    } while (
!SM.isInFileID(Loc, Parent)567
);
266
327
    return true;
267
775
  }
268
269
  /// Get the start of \c S ignoring macro arguments and builtin macros.
270
6.27k
  SourceLocation getStart(const Stmt *S) {
271
6.27k
    SourceLocation Loc = S->getBeginLoc();
272
6.54k
    while (SM.isMacroArgExpansion(Loc) || 
isInBuiltin(Loc)6.27k
)
273
270
      Loc = SM.getImmediateExpansionRange(Loc).getBegin();
274
6.27k
    return Loc;
275
6.27k
  }
276
277
  /// Get the end of \c S ignoring macro arguments and builtin macros.
278
4.84k
  SourceLocation getEnd(const Stmt *S) {
279
4.84k
    SourceLocation Loc = S->getEndLoc();
280
5.04k
    while (SM.isMacroArgExpansion(Loc) || 
isInBuiltin(Loc)4.84k
)
281
199
      Loc = SM.getImmediateExpansionRange(Loc).getBegin();
282
4.84k
    return getPreciseTokenLocEnd(Loc);
283
4.84k
  }
284
285
  /// Find the set of files we have regions for and assign IDs
286
  ///
287
  /// Fills \c Mapping with the virtual file mapping needed to write out
288
  /// coverage and collects the necessary file information to emit source and
289
  /// expansion regions.
290
249
  void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
291
249
    FileIDMapping.clear();
292
293
249
    llvm::SmallSet<FileID, 8> Visited;
294
249
    SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
295
1.96k
    for (const auto &Region : SourceRegions) {
296
1.96k
      SourceLocation Loc = Region.getBeginLoc();
297
1.96k
      FileID File = SM.getFileID(Loc);
298
1.96k
      if (!Visited.insert(File).second)
299
1.52k
        continue;
300
301
      // Do not map FileID's associated with system headers.
302
436
      if (SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
303
3
        continue;
304
305
433
      unsigned Depth = 0;
306
433
      for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
307
728
           Parent.isValid(); 
Parent = getIncludeOrExpansionLoc(Parent)295
)
308
295
        ++Depth;
309
433
      FileLocs.push_back(std::make_pair(Loc, Depth));
310
433
    }
311
249
    llvm::stable_sort(FileLocs, llvm::less_second());
312
313
433
    for (const auto &FL : FileLocs) {
314
433
      SourceLocation Loc = FL.first;
315
433
      FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
316
433
      auto Entry = SM.getFileEntryForID(SpellingFile);
317
433
      if (!Entry)
318
8
        continue;
319
320
425
      FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
321
425
      Mapping.push_back(CVM.getFileID(Entry));
322
425
    }
323
249
  }
324
325
  /// Get the coverage mapping file ID for \c Loc.
326
  ///
327
  /// If such file id doesn't exist, return None.
328
2.99k
  Optional<unsigned> getCoverageFileID(SourceLocation Loc) {
329
2.99k
    auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
330
2.99k
    if (Mapping != FileIDMapping.end())
331
2.82k
      return Mapping->second.first;
332
167
    return None;
333
2.99k
  }
334
335
  /// This shrinks the skipped range if it spans a line that contains a
336
  /// non-comment token. If shrinking the skipped range would make it empty,
337
  /// this returns None.
338
  Optional<SpellingRegion> adjustSkippedRange(SourceManager &SM,
339
                                              SourceLocation LocStart,
340
                                              SourceLocation LocEnd,
341
                                              SourceLocation PrevTokLoc,
342
530
                                              SourceLocation NextTokLoc) {
343
530
    SpellingRegion SR{SM, LocStart, LocEnd};
344
530
    SR.ColumnStart = 1;
345
530
    if (PrevTokLoc.isValid() && 
SM.isWrittenInSameFile(LocStart, PrevTokLoc)517
&&
346
530
        
SR.LineStart == SM.getSpellingLineNumber(PrevTokLoc)452
)
347
321
      SR.LineStart++;
348
530
    if (NextTokLoc.isValid() && 
SM.isWrittenInSameFile(LocEnd, NextTokLoc)516
&&
349
530
        
SR.LineEnd == SM.getSpellingLineNumber(NextTokLoc)512
) {
350
14
      SR.LineEnd--;
351
14
      SR.ColumnEnd++;
352
14
    }
353
530
    if (SR.isInSourceOrder())
354
360
      return SR;
355
170
    return None;
356
530
  }
357
358
  /// Gather all the regions that were skipped by the preprocessor
359
  /// using the constructs like #if or comments.
360
229
  void gatherSkippedRegions() {
361
    /// An array of the minimum lineStarts and the maximum lineEnds
362
    /// for mapping regions from the appropriate source files.
363
229
    llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
364
229
    FileLineRanges.resize(
365
229
        FileIDMapping.size(),
366
229
        std::make_pair(std::numeric_limits<unsigned>::max(), 0));
367
2.08k
    for (const auto &R : MappingRegions) {
368
2.08k
      FileLineRanges[R.FileID].first =
369
2.08k
          std::min(FileLineRanges[R.FileID].first, R.LineStart);
370
2.08k
      FileLineRanges[R.FileID].second =
371
2.08k
          std::max(FileLineRanges[R.FileID].second, R.LineEnd);
372
2.08k
    }
373
374
229
    auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
375
681
    for (auto &I : SkippedRanges) {
376
681
      SourceRange Range = I.Range;
377
681
      auto LocStart = Range.getBegin();
378
681
      auto LocEnd = Range.getEnd();
379
681
      assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
380
681
             "region spans multiple files");
381
382
0
      auto CovFileID = getCoverageFileID(LocStart);
383
681
      if (!CovFileID)
384
151
        continue;
385
530
      Optional<SpellingRegion> SR =
386
530
          adjustSkippedRange(SM, LocStart, LocEnd, I.PrevTokLoc, I.NextTokLoc);
387
530
      if (!SR.hasValue())
388
170
        continue;
389
360
      auto Region = CounterMappingRegion::makeSkipped(
390
360
          *CovFileID, SR->LineStart, SR->ColumnStart, SR->LineEnd,
391
360
          SR->ColumnEnd);
392
      // Make sure that we only collect the regions that are inside
393
      // the source code of this function.
394
360
      if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
395
360
          
Region.LineEnd <= FileLineRanges[*CovFileID].second107
)
396
36
        MappingRegions.push_back(Region);
397
360
    }
398
229
  }
399
400
  /// Generate the coverage counter mapping regions from collected
401
  /// source regions.
402
249
  void emitSourceRegions(const SourceRegionFilter &Filter) {
403
1.96k
    for (const auto &Region : SourceRegions) {
404
1.96k
      assert(Region.hasEndLoc() && "incomplete region");
405
406
0
      SourceLocation LocStart = Region.getBeginLoc();
407
1.96k
      assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
408
409
      // Ignore regions from system headers.
410
1.96k
      if (SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
411
9
        continue;
412
413
1.95k
      auto CovFileID = getCoverageFileID(LocStart);
414
      // Ignore regions that don't have a file, such as builtin macros.
415
1.95k
      if (!CovFileID)
416
9
        continue;
417
418
1.94k
      SourceLocation LocEnd = Region.getEndLoc();
419
1.94k
      assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
420
1.94k
             "region spans multiple files");
421
422
      // Don't add code regions for the area covered by expansion regions.
423
      // This not only suppresses redundant regions, but sometimes prevents
424
      // creating regions with wrong counters if, for example, a statement's
425
      // body ends at the end of a nested macro.
426
1.94k
      if (Filter.count(std::make_pair(LocStart, LocEnd)))
427
17
        continue;
428
429
      // Find the spelling locations for the mapping region.
430
1.92k
      SpellingRegion SR{SM, LocStart, LocEnd};
431
1.92k
      assert(SR.isInSourceOrder() && "region start and end out of order");
432
433
1.92k
      if (Region.isGap()) {
434
327
        MappingRegions.push_back(CounterMappingRegion::makeGapRegion(
435
327
            Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
436
327
            SR.LineEnd, SR.ColumnEnd));
437
1.60k
      } else if (Region.isBranch()) {
438
369
        MappingRegions.push_back(CounterMappingRegion::makeBranchRegion(
439
369
            Region.getCounter(), Region.getFalseCounter(), *CovFileID,
440
369
            SR.LineStart, SR.ColumnStart, SR.LineEnd, SR.ColumnEnd));
441
1.23k
      } else {
442
1.23k
        MappingRegions.push_back(CounterMappingRegion::makeRegion(
443
1.23k
            Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
444
1.23k
            SR.LineEnd, SR.ColumnEnd));
445
1.23k
      }
446
1.92k
    }
447
249
  }
448
449
  /// Generate expansion regions for each virtual file we've seen.
450
229
  SourceRegionFilter emitExpansionRegions() {
451
229
    SourceRegionFilter Filter;
452
405
    for (const auto &FM : FileIDMapping) {
453
405
      SourceLocation ExpandedLoc = FM.second.second;
454
405
      SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
455
405
      if (ParentLoc.isInvalid())
456
221
        continue;
457
458
184
      auto ParentFileID = getCoverageFileID(ParentLoc);
459
184
      if (!ParentFileID)
460
7
        continue;
461
177
      auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
462
177
      assert(ExpandedFileID && "expansion in uncovered file");
463
464
0
      SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
465
177
      assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
466
177
             "region spans multiple files");
467
0
      Filter.insert(std::make_pair(ParentLoc, LocEnd));
468
469
177
      SpellingRegion SR{SM, ParentLoc, LocEnd};
470
177
      assert(SR.isInSourceOrder() && "region start and end out of order");
471
0
      MappingRegions.push_back(CounterMappingRegion::makeExpansion(
472
177
          *ParentFileID, *ExpandedFileID, SR.LineStart, SR.ColumnStart,
473
177
          SR.LineEnd, SR.ColumnEnd));
474
177
    }
475
229
    return Filter;
476
229
  }
477
};
478
479
/// Creates unreachable coverage regions for the functions that
480
/// are not emitted.
481
struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
482
  EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
483
                              const LangOptions &LangOpts)
484
20
      : CoverageMappingBuilder(CVM, SM, LangOpts) {}
485
486
20
  void VisitDecl(const Decl *D) {
487
20
    if (!D->hasBody())
488
0
      return;
489
20
    auto Body = D->getBody();
490
20
    SourceLocation Start = getStart(Body);
491
20
    SourceLocation End = getEnd(Body);
492
20
    if (!SM.isWrittenInSameFile(Start, End)) {
493
      // Walk up to find the common ancestor.
494
      // Correct the locations accordingly.
495
6
      FileID StartFileID = SM.getFileID(Start);
496
6
      FileID EndFileID = SM.getFileID(End);
497
10
      while (StartFileID != EndFileID && 
!isNestedIn(End, StartFileID)8
) {
498
4
        Start = getIncludeOrExpansionLoc(Start);
499
4
        assert(Start.isValid() &&
500
4
               "Declaration start location not nested within a known region");
501
0
        StartFileID = SM.getFileID(Start);
502
4
      }
503
10
      while (StartFileID != EndFileID) {
504
4
        End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
505
4
        assert(End.isValid() &&
506
4
               "Declaration end location not nested within a known region");
507
0
        EndFileID = SM.getFileID(End);
508
4
      }
509
6
    }
510
20
    SourceRegions.emplace_back(Counter(), Start, End);
511
20
  }
512
513
  /// Write the mapping data to the output stream
514
20
  void write(llvm::raw_ostream &OS) {
515
20
    SmallVector<unsigned, 16> FileIDMapping;
516
20
    gatherFileIDs(FileIDMapping);
517
20
    emitSourceRegions(SourceRegionFilter());
518
519
20
    if (MappingRegions.empty())
520
0
      return;
521
522
20
    CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions);
523
20
    Writer.write(OS);
524
20
  }
525
};
526
527
/// A StmtVisitor that creates coverage mapping regions which map
528
/// from the source code locations to the PGO counters.
529
struct CounterCoverageMappingBuilder
530
    : public CoverageMappingBuilder,
531
      public ConstStmtVisitor<CounterCoverageMappingBuilder> {
532
  /// The map of statements to count values.
533
  llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
534
535
  /// A stack of currently live regions.
536
  std::vector<SourceMappingRegion> RegionStack;
537
538
  CounterExpressionBuilder Builder;
539
540
  /// A location in the most recently visited file or macro.
541
  ///
542
  /// This is used to adjust the active source regions appropriately when
543
  /// expressions cross file or macro boundaries.
544
  SourceLocation MostRecentLocation;
545
546
  /// Whether the visitor at a terminate statement.
547
  bool HasTerminateStmt = false;
548
549
  /// Gap region counter after terminate statement.
550
  Counter GapRegionCounter;
551
552
  /// Return a counter for the subtraction of \c RHS from \c LHS
553
663
  Counter subtractCounters(Counter LHS, Counter RHS) {
554
663
    return Builder.subtract(LHS, RHS);
555
663
  }
556
557
  /// Return a counter for the sum of \c LHS and \c RHS.
558
542
  Counter addCounters(Counter LHS, Counter RHS) {
559
542
    return Builder.add(LHS, RHS);
560
542
  }
561
562
100
  Counter addCounters(Counter C1, Counter C2, Counter C3) {
563
100
    return addCounters(addCounters(C1, C2), C3);
564
100
  }
565
566
  /// Return the region counter for the given statement.
567
  ///
568
  /// This should only be called on statements that have a dedicated counter.
569
879
  Counter getRegionCounter(const Stmt *S) {
570
879
    return Counter::getCounter(CounterMap[S]);
571
879
  }
572
573
  /// Push a region onto the stack.
574
  ///
575
  /// Returns the index on the stack where the region was pushed. This can be
576
  /// used with popRegions to exit a "scope", ending the region that was pushed.
577
  size_t pushRegion(Counter Count, Optional<SourceLocation> StartLoc = None,
578
                    Optional<SourceLocation> EndLoc = None,
579
1.98k
                    Optional<Counter> FalseCount = None) {
580
581
1.98k
    if (StartLoc && 
!FalseCount.hasValue()1.64k
) {
582
1.27k
      MostRecentLocation = *StartLoc;
583
1.27k
    }
584
585
1.98k
    RegionStack.emplace_back(Count, FalseCount, StartLoc, EndLoc);
586
587
1.98k
    return RegionStack.size() - 1;
588
1.98k
  }
589
590
4.20k
  size_t locationDepth(SourceLocation Loc) {
591
4.20k
    size_t Depth = 0;
592
9.41k
    while (Loc.isValid()) {
593
5.20k
      Loc = getIncludeOrExpansionLoc(Loc);
594
5.20k
      Depth++;
595
5.20k
    }
596
4.20k
    return Depth;
597
4.20k
  }
598
599
  /// Pop regions from the stack into the function's list of regions.
600
  ///
601
  /// Adds all regions from \c ParentIndex to the top of the stack to the
602
  /// function's \c SourceRegions.
603
1.61k
  void popRegions(size_t ParentIndex) {
604
1.61k
    assert(RegionStack.size() >= ParentIndex && "parent not in stack");
605
3.59k
    while (RegionStack.size() > ParentIndex) {
606
1.98k
      SourceMappingRegion &Region = RegionStack.back();
607
1.98k
      if (Region.hasStartLoc()) {
608
1.75k
        SourceLocation StartLoc = Region.getBeginLoc();
609
1.75k
        SourceLocation EndLoc = Region.hasEndLoc()
610
1.75k
                                    ? 
Region.getEndLoc()1.67k
611
1.75k
                                    : 
RegionStack[ParentIndex].getEndLoc()75
;
612
1.75k
        bool isBranch = Region.isBranch();
613
1.75k
        size_t StartDepth = locationDepth(StartLoc);
614
1.75k
        size_t EndDepth = locationDepth(EndLoc);
615
1.78k
        while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
616
33
          bool UnnestStart = StartDepth >= EndDepth;
617
33
          bool UnnestEnd = EndDepth >= StartDepth;
618
33
          if (UnnestEnd) {
619
            // The region ends in a nested file or macro expansion. If the
620
            // region is not a branch region, create a separate region for each
621
            // expansion, and for all regions, update the EndLoc. Branch
622
            // regions should not be split in order to keep a straightforward
623
            // correspondance between the region and its associated branch
624
            // condition, even if the condition spans multiple depths.
625
28
            SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
626
28
            assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
627
628
28
            if (!isBranch && !isRegionAlreadyAdded(NestedLoc, EndLoc))
629
25
              SourceRegions.emplace_back(Region.getCounter(), NestedLoc,
630
25
                                         EndLoc);
631
632
28
            EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
633
28
            if (EndLoc.isInvalid())
634
0
              llvm::report_fatal_error(
635
0
                  "File exit not handled before popRegions");
636
28
            EndDepth--;
637
28
          }
638
33
          if (UnnestStart) {
639
            // The region ends in a nested file or macro expansion. If the
640
            // region is not a branch region, create a separate region for each
641
            // expansion, and for all regions, update the StartLoc. Branch
642
            // regions should not be split in order to keep a straightforward
643
            // correspondance between the region and its associated branch
644
            // condition, even if the condition spans multiple depths.
645
5
            SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc);
646
5
            assert(SM.isWrittenInSameFile(StartLoc, NestedLoc));
647
648
5
            if (!isBranch && 
!isRegionAlreadyAdded(StartLoc, NestedLoc)3
)
649
1
              SourceRegions.emplace_back(Region.getCounter(), StartLoc,
650
1
                                         NestedLoc);
651
652
5
            StartLoc = getIncludeOrExpansionLoc(StartLoc);
653
5
            if (StartLoc.isInvalid())
654
0
              llvm::report_fatal_error(
655
0
                  "File exit not handled before popRegions");
656
5
            StartDepth--;
657
5
          }
658
33
        }
659
1.75k
        Region.setStartLoc(StartLoc);
660
1.75k
        Region.setEndLoc(EndLoc);
661
662
1.75k
        if (!isBranch) {
663
1.38k
          MostRecentLocation = EndLoc;
664
          // If this region happens to span an entire expansion, we need to
665
          // make sure we don't overlap the parent region with it.
666
1.38k
          if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
667
1.38k
              
EndLoc == getEndOfFileOrMacro(EndLoc)57
)
668
56
            MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
669
1.38k
        }
670
671
1.75k
        assert(SM.isWrittenInSameFile(Region.getBeginLoc(), EndLoc));
672
0
        assert(SpellingRegion(SM, Region).isInSourceOrder());
673
0
        SourceRegions.push_back(Region);
674
1.75k
        }
675
0
      RegionStack.pop_back();
676
1.98k
    }
677
1.61k
  }
678
679
  /// Return the currently active region.
680
6.29k
  SourceMappingRegion &getRegion() {
681
6.29k
    assert(!RegionStack.empty() && "statement has no region");
682
0
    return RegionStack.back();
683
6.29k
  }
684
685
  /// Propagate counts through the children of \p S if \p VisitChildren is true.
686
  /// Otherwise, only emit a count for \p S itself.
687
  Counter propagateCounts(Counter TopCount, const Stmt *S,
688
914
                          bool VisitChildren = true) {
689
914
    SourceLocation StartLoc = getStart(S);
690
914
    SourceLocation EndLoc = getEnd(S);
691
914
    size_t Index = pushRegion(TopCount, StartLoc, EndLoc);
692
914
    if (VisitChildren)
693
904
      Visit(S);
694
914
    Counter ExitCount = getRegion().getCounter();
695
914
    popRegions(Index);
696
697
    // The statement may be spanned by an expansion. Make sure we handle a file
698
    // exit out of this expansion before moving to the next statement.
699
914
    if (SM.isBeforeInTranslationUnit(StartLoc, S->getBeginLoc()))
700
36
      MostRecentLocation = EndLoc;
701
702
914
    return ExitCount;
703
914
  }
704
705
  /// Determine whether the given condition can be constant folded.
706
339
  bool ConditionFoldsToBool(const Expr *Cond) {
707
339
    Expr::EvalResult Result;
708
339
    return (Cond->EvaluateAsInt(Result, CVM.getCodeGenModule().getContext()));
709
339
  }
710
711
  /// Create a Branch Region around an instrumentable condition for coverage
712
  /// and add it to the function's SourceRegions.  A branch region tracks a
713
  /// "True" counter and a "False" counter for boolean expressions that
714
  /// result in the generation of a branch.
715
399
  void createBranchRegion(const Expr *C, Counter TrueCnt, Counter FalseCnt) {
716
    // Check for NULL conditions.
717
399
    if (!C)
718
12
      return;
719
720
    // Ensure we are an instrumentable condition (i.e. no "&&" or "||").  Push
721
    // region onto RegionStack but immediately pop it (which adds it to the
722
    // function's SourceRegions) because it doesn't apply to any other source
723
    // code other than the Condition.
724
387
    if (CodeGenFunction::isInstrumentedCondition(C)) {
725
      // If a condition can fold to true or false, the corresponding branch
726
      // will be removed.  Create a region with both counters hard-coded to
727
      // zero. This allows us to visualize them in a special way.
728
      // Alternatively, we can prevent any optimization done via
729
      // constant-folding by ensuring that ConstantFoldsToSimpleInteger() in
730
      // CodeGenFunction.c always returns false, but that is very heavy-handed.
731
339
      if (ConditionFoldsToBool(C))
732
74
        popRegions(pushRegion(Counter::getZero(), getStart(C), getEnd(C),
733
74
                              Counter::getZero()));
734
265
      else
735
        // Otherwise, create a region with the True counter and False counter.
736
265
        popRegions(pushRegion(TrueCnt, getStart(C), getEnd(C), FalseCnt));
737
339
    }
738
387
  }
739
740
  /// Create a Branch Region around a SwitchCase for code coverage
741
  /// and add it to the function's SourceRegions.
742
  void createSwitchCaseRegion(const SwitchCase *SC, Counter TrueCnt,
743
32
                              Counter FalseCnt) {
744
    // Push region onto RegionStack but immediately pop it (which adds it to
745
    // the function's SourceRegions) because it doesn't apply to any other
746
    // source other than the SwitchCase.
747
32
    popRegions(pushRegion(TrueCnt, getStart(SC), SC->getColonLoc(), FalseCnt));
748
32
  }
749
750
  /// Check whether a region with bounds \c StartLoc and \c EndLoc
751
  /// is already added to \c SourceRegions.
752
  bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc,
753
39
                            bool isBranch = false) {
754
39
    return SourceRegions.rend() !=
755
39
           std::find_if(SourceRegions.rbegin(), SourceRegions.rend(),
756
537
                        [&](const SourceMappingRegion &Region) {
757
537
                          return Region.getBeginLoc() == StartLoc &&
758
537
                                 
Region.getEndLoc() == EndLoc19
&&
759
537
                                 
Region.isBranch() == isBranch14
;
760
537
                        });
761
39
  }
762
763
  /// Adjust the most recently visited location to \c EndLoc.
764
  ///
765
  /// This should be used after visiting any statements in non-source order.
766
53
  void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
767
53
    MostRecentLocation = EndLoc;
768
    // The code region for a whole macro is created in handleFileExit() when
769
    // it detects exiting of the virtual file of that macro. If we visited
770
    // statements in non-source order, we might already have such a region
771
    // added, for example, if a body of a loop is divided among multiple
772
    // macros. Avoid adding duplicate regions in such case.
773
53
    if (getRegion().hasEndLoc() &&
774
53
        
MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation)46
&&
775
53
        isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
776
8
                             MostRecentLocation, getRegion().isBranch()))
777
6
      MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
778
53
  }
779
780
  /// Adjust regions and state when \c NewLoc exits a file.
781
  ///
782
  /// If moving from our most recently tracked location to \c NewLoc exits any
783
  /// files, this adjusts our current region stack and creates the file regions
784
  /// for the exited file.
785
8.05k
  void handleFileExit(SourceLocation NewLoc) {
786
8.05k
    if (NewLoc.isInvalid() ||
787
8.05k
        
SM.isWrittenInSameFile(MostRecentLocation, NewLoc)8.03k
)
788
7.78k
      return;
789
790
    // If NewLoc is not in a file that contains MostRecentLocation, walk up to
791
    // find the common ancestor.
792
261
    SourceLocation LCA = NewLoc;
793
261
    FileID ParentFile = SM.getFileID(LCA);
794
324
    while (!isNestedIn(MostRecentLocation, ParentFile)) {
795
194
      LCA = getIncludeOrExpansionLoc(LCA);
796
194
      if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
797
        // Since there isn't a common ancestor, no file was exited. We just need
798
        // to adjust our location to the new file.
799
131
        MostRecentLocation = NewLoc;
800
131
        return;
801
131
      }
802
63
      ParentFile = SM.getFileID(LCA);
803
63
    }
804
805
130
    llvm::SmallSet<SourceLocation, 8> StartLocs;
806
130
    Optional<Counter> ParentCounter;
807
189
    for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
808
189
      if (!I.hasStartLoc())
809
12
        continue;
810
177
      SourceLocation Loc = I.getBeginLoc();
811
177
      if (!isNestedIn(Loc, ParentFile)) {
812
125
        ParentCounter = I.getCounter();
813
125
        break;
814
125
      }
815
816
110
      
while (52
!SM.isInFileID(Loc, ParentFile)) {
817
        // The most nested region for each start location is the one with the
818
        // correct count. We avoid creating redundant regions by stopping once
819
        // we've seen this region.
820
58
        if (StartLocs.insert(Loc).second) {
821
55
          if (I.isBranch())
822
0
            SourceRegions.emplace_back(I.getCounter(), I.getFalseCounter(), Loc,
823
0
                                       getEndOfFileOrMacro(Loc), I.isBranch());
824
55
          else
825
55
            SourceRegions.emplace_back(I.getCounter(), Loc,
826
55
                                       getEndOfFileOrMacro(Loc));
827
55
        }
828
58
        Loc = getIncludeOrExpansionLoc(Loc);
829
58
      }
830
52
      I.setStartLoc(getPreciseTokenLocEnd(Loc));
831
52
    }
832
833
130
    if (ParentCounter) {
834
      // If the file is contained completely by another region and doesn't
835
      // immediately start its own region, the whole file gets a region
836
      // corresponding to the parent.
837
125
      SourceLocation Loc = MostRecentLocation;
838
266
      while (isNestedIn(Loc, ParentFile)) {
839
141
        SourceLocation FileStart = getStartOfFileOrMacro(Loc);
840
141
        if (StartLocs.insert(FileStart).second) {
841
111
          SourceRegions.emplace_back(*ParentCounter, FileStart,
842
111
                                     getEndOfFileOrMacro(Loc));
843
111
          assert(SpellingRegion(SM, SourceRegions.back()).isInSourceOrder());
844
111
        }
845
0
        Loc = getIncludeOrExpansionLoc(Loc);
846
141
      }
847
125
    }
848
849
130
    MostRecentLocation = NewLoc;
850
130
  }
851
852
  /// Ensure that \c S is included in the current region.
853
4.29k
  void extendRegion(const Stmt *S) {
854
4.29k
    SourceMappingRegion &Region = getRegion();
855
4.29k
    SourceLocation StartLoc = getStart(S);
856
857
4.29k
    handleFileExit(StartLoc);
858
4.29k
    if (!Region.hasStartLoc())
859
106
      Region.setStartLoc(StartLoc);
860
4.29k
  }
861
862
  /// Mark \c S as a terminator, starting a zero region.
863
228
  void terminateRegion(const Stmt *S) {
864
228
    extendRegion(S);
865
228
    SourceMappingRegion &Region = getRegion();
866
228
    SourceLocation EndLoc = getEnd(S);
867
228
    if (!Region.hasEndLoc())
868
51
      Region.setEndLoc(EndLoc);
869
228
    pushRegion(Counter::getZero());
870
228
    HasTerminateStmt = true;
871
228
  }
872
873
  /// Find a valid gap range between \p AfterLoc and \p BeforeLoc.
874
  Optional<SourceRange> findGapAreaBetween(SourceLocation AfterLoc,
875
353
                                           SourceLocation BeforeLoc) {
876
    // If AfterLoc is in function-like macro, use the right parenthesis
877
    // location.
878
353
    if (AfterLoc.isMacroID()) {
879
55
      FileID FID = SM.getFileID(AfterLoc);
880
55
      const SrcMgr::ExpansionInfo *EI = &SM.getSLocEntry(FID).getExpansion();
881
55
      if (EI->isFunctionMacroExpansion())
882
16
        AfterLoc = EI->getExpansionLocEnd();
883
55
    }
884
885
353
    size_t StartDepth = locationDepth(AfterLoc);
886
353
    size_t EndDepth = locationDepth(BeforeLoc);
887
435
    while (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc)) {
888
82
      bool UnnestStart = StartDepth >= EndDepth;
889
82
      bool UnnestEnd = EndDepth >= StartDepth;
890
82
      if (UnnestEnd) {
891
48
        assert(SM.isWrittenInSameFile(getStartOfFileOrMacro(BeforeLoc),
892
48
                                      BeforeLoc));
893
894
0
        BeforeLoc = getIncludeOrExpansionLoc(BeforeLoc);
895
48
        assert(BeforeLoc.isValid());
896
0
        EndDepth--;
897
48
      }
898
82
      if (UnnestStart) {
899
35
        assert(SM.isWrittenInSameFile(AfterLoc,
900
35
                                      getEndOfFileOrMacro(AfterLoc)));
901
902
0
        AfterLoc = getIncludeOrExpansionLoc(AfterLoc);
903
35
        assert(AfterLoc.isValid());
904
0
        AfterLoc = getPreciseTokenLocEnd(AfterLoc);
905
35
        assert(AfterLoc.isValid());
906
0
        StartDepth--;
907
35
      }
908
82
    }
909
353
    AfterLoc = getPreciseTokenLocEnd(AfterLoc);
910
    // If the start and end locations of the gap are both within the same macro
911
    // file, the range may not be in source order.
912
353
    if (AfterLoc.isMacroID() || 
BeforeLoc.isMacroID()328
)
913
25
      return None;
914
328
    if (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc) ||
915
328
        !SpellingRegion(SM, AfterLoc, BeforeLoc).isInSourceOrder())
916
7
      return None;
917
321
    return {{AfterLoc, BeforeLoc}};
918
328
  }
919
920
  /// Emit a gap region between \p StartLoc and \p EndLoc with the given count.
921
  void fillGapAreaWithCount(SourceLocation StartLoc, SourceLocation EndLoc,
922
321
                            Counter Count) {
923
321
    if (StartLoc == EndLoc)
924
6
      return;
925
315
    assert(SpellingRegion(SM, StartLoc, EndLoc).isInSourceOrder());
926
0
    handleFileExit(StartLoc);
927
315
    size_t Index = pushRegion(Count, StartLoc, EndLoc);
928
315
    getRegion().setGap(true);
929
315
    handleFileExit(EndLoc);
930
315
    popRegions(Index);
931
315
  }
932
933
  /// Keep counts of breaks and continues inside loops.
934
  struct BreakContinue {
935
    Counter BreakCount;
936
    Counter ContinueCount;
937
  };
938
  SmallVector<BreakContinue, 8> BreakContinueStack;
939
940
  CounterCoverageMappingBuilder(
941
      CoverageMappingModuleGen &CVM,
942
      llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
943
      const LangOptions &LangOpts)
944
229
      : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap) {}
945
946
  /// Write the mapping data to the output stream
947
229
  void write(llvm::raw_ostream &OS) {
948
229
    llvm::SmallVector<unsigned, 8> VirtualFileMapping;
949
229
    gatherFileIDs(VirtualFileMapping);
950
229
    SourceRegionFilter Filter = emitExpansionRegions();
951
229
    emitSourceRegions(Filter);
952
229
    gatherSkippedRegions();
953
954
229
    if (MappingRegions.empty())
955
1
      return;
956
957
228
    CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
958
228
                                 MappingRegions);
959
228
    Writer.write(OS);
960
228
  }
961
962
3.01k
  void VisitStmt(const Stmt *S) {
963
3.01k
    if (S->getBeginLoc().isValid())
964
2.99k
      extendRegion(S);
965
3.01k
    const Stmt *LastStmt = nullptr;
966
3.01k
    bool SaveTerminateStmt = HasTerminateStmt;
967
3.01k
    HasTerminateStmt = false;
968
3.01k
    GapRegionCounter = Counter::getZero();
969
3.01k
    for (const Stmt *Child : S->children())
970
2.44k
      if (Child) {
971
        // If last statement contains terminate statements, add a gap area
972
        // between the two statements. Skipping attributed statements, because
973
        // they don't have valid start location.
974
2.44k
        if (LastStmt && 
HasTerminateStmt769
&&
!dyn_cast<AttributedStmt>(Child)104
) {
975
104
          auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child));
976
104
          if (Gap)
977
101
            fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(),
978
101
                                 GapRegionCounter);
979
104
          SaveTerminateStmt = true;
980
104
          HasTerminateStmt = false;
981
104
        }
982
2.44k
        this->Visit(Child);
983
2.44k
        LastStmt = Child;
984
2.44k
      }
985
3.01k
    if (SaveTerminateStmt)
986
91
      HasTerminateStmt = true;
987
3.01k
    handleFileExit(getEnd(S));
988
3.01k
  }
989
990
229
  void VisitDecl(const Decl *D) {
991
229
    Stmt *Body = D->getBody();
992
993
    // Do not propagate region counts into system headers.
994
229
    if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
995
1
      return;
996
997
    // Do not visit the artificial children nodes of defaulted methods. The
998
    // lexer may not be able to report back precise token end locations for
999
    // these children nodes (llvm.org/PR39822), and moreover users will not be
1000
    // able to see coverage for them.
1001
228
    bool Defaulted = false;
1002
228
    if (auto *Method = dyn_cast<CXXMethodDecl>(D))
1003
29
      Defaulted = Method->isDefaulted();
1004
1005
228
    propagateCounts(getRegionCounter(Body), Body,
1006
228
                    /*VisitChildren=*/!Defaulted);
1007
228
    assert(RegionStack.empty() && "Regions entered but never exited");
1008
228
  }
1009
1010
182
  void VisitReturnStmt(const ReturnStmt *S) {
1011
182
    extendRegion(S);
1012
182
    if (S->getRetValue())
1013
137
      Visit(S->getRetValue());
1014
182
    terminateRegion(S);
1015
182
  }
1016
1017
1
  void VisitCoroutineBodyStmt(const CoroutineBodyStmt *S) {
1018
1
    extendRegion(S);
1019
1
    Visit(S->getBody());
1020
1
  }
1021
1022
2
  void VisitCoreturnStmt(const CoreturnStmt *S) {
1023
2
    extendRegion(S);
1024
2
    if (S->getOperand())
1025
2
      Visit(S->getOperand());
1026
2
    terminateRegion(S);
1027
2
  }
1028
1029
3
  void VisitCXXThrowExpr(const CXXThrowExpr *E) {
1030
3
    extendRegion(E);
1031
3
    if (E->getSubExpr())
1032
3
      Visit(E->getSubExpr());
1033
3
    terminateRegion(E);
1034
3
  }
1035
1036
11
  void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
1037
1038
14
  void VisitLabelStmt(const LabelStmt *S) {
1039
14
    Counter LabelCount = getRegionCounter(S);
1040
14
    SourceLocation Start = getStart(S);
1041
    // We can't extendRegion here or we risk overlapping with our new region.
1042
14
    handleFileExit(Start);
1043
14
    pushRegion(LabelCount, Start);
1044
14
    Visit(S->getSubStmt());
1045
14
  }
1046
1047
21
  void VisitBreakStmt(const BreakStmt *S) {
1048
21
    assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
1049
0
    BreakContinueStack.back().BreakCount = addCounters(
1050
21
        BreakContinueStack.back().BreakCount, getRegion().getCounter());
1051
    // FIXME: a break in a switch should terminate regions for all preceding
1052
    // case statements, not just the most recent one.
1053
21
    terminateRegion(S);
1054
21
  }
1055
1056
6
  void VisitContinueStmt(const ContinueStmt *S) {
1057
6
    assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
1058
0
    BreakContinueStack.back().ContinueCount = addCounters(
1059
6
        BreakContinueStack.back().ContinueCount, getRegion().getCounter());
1060
6
    terminateRegion(S);
1061
6
  }
1062
1063
105
  void VisitCallExpr(const CallExpr *E) {
1064
105
    VisitStmt(E);
1065
1066
    // Terminate the region when we hit a noreturn function.
1067
    // (This is helpful dealing with switch statements.)
1068
105
    QualType CalleeType = E->getCallee()->getType();
1069
105
    if (getFunctionExtInfo(*CalleeType).getNoReturn())
1070
3
      terminateRegion(E);
1071
105
  }
1072
1073
34
  void VisitWhileStmt(const WhileStmt *S) {
1074
34
    extendRegion(S);
1075
1076
34
    Counter ParentCount = getRegion().getCounter();
1077
34
    Counter BodyCount = getRegionCounter(S);
1078
1079
    // Handle the body first so that we can get the backedge count.
1080
34
    BreakContinueStack.push_back(BreakContinue());
1081
34
    extendRegion(S->getBody());
1082
34
    Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1083
34
    BreakContinue BC = BreakContinueStack.pop_back_val();
1084
1085
34
    bool BodyHasTerminateStmt = HasTerminateStmt;
1086
34
    HasTerminateStmt = false;
1087
1088
    // Go back to handle the condition.
1089
34
    Counter CondCount =
1090
34
        addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1091
34
    propagateCounts(CondCount, S->getCond());
1092
34
    adjustForOutOfOrderTraversal(getEnd(S));
1093
1094
    // The body count applies to the area immediately after the increment.
1095
34
    auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1096
34
    if (Gap)
1097
31
      fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1098
1099
34
    Counter OutCount =
1100
34
        addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
1101
34
    if (OutCount != ParentCount) {
1102
7
      pushRegion(OutCount);
1103
7
      GapRegionCounter = OutCount;
1104
7
      if (BodyHasTerminateStmt)
1105
6
        HasTerminateStmt = true;
1106
7
    }
1107
1108
    // Create Branch Region around condition.
1109
34
    createBranchRegion(S->getCond(), BodyCount,
1110
34
                       subtractCounters(CondCount, BodyCount));
1111
34
  }
1112
1113
10
  void VisitDoStmt(const DoStmt *S) {
1114
10
    extendRegion(S);
1115
1116
10
    Counter ParentCount = getRegion().getCounter();
1117
10
    Counter BodyCount = getRegionCounter(S);
1118
1119
10
    BreakContinueStack.push_back(BreakContinue());
1120
10
    extendRegion(S->getBody());
1121
10
    Counter BackedgeCount =
1122
10
        propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
1123
10
    BreakContinue BC = BreakContinueStack.pop_back_val();
1124
1125
10
    bool BodyHasTerminateStmt = HasTerminateStmt;
1126
10
    HasTerminateStmt = false;
1127
1128
10
    Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
1129
10
    propagateCounts(CondCount, S->getCond());
1130
1131
10
    Counter OutCount =
1132
10
        addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
1133
10
    if (OutCount != ParentCount) {
1134
1
      pushRegion(OutCount);
1135
1
      GapRegionCounter = OutCount;
1136
1
    }
1137
1138
    // Create Branch Region around condition.
1139
10
    createBranchRegion(S->getCond(), BodyCount,
1140
10
                       subtractCounters(CondCount, BodyCount));
1141
1142
10
    if (BodyHasTerminateStmt)
1143
1
      HasTerminateStmt = true;
1144
10
  }
1145
1146
31
  void VisitForStmt(const ForStmt *S) {
1147
31
    extendRegion(S);
1148
31
    if (S->getInit())
1149
18
      Visit(S->getInit());
1150
1151
31
    Counter ParentCount = getRegion().getCounter();
1152
31
    Counter BodyCount = getRegionCounter(S);
1153
1154
    // The loop increment may contain a break or continue.
1155
31
    if (S->getInc())
1156
19
      BreakContinueStack.emplace_back();
1157
1158
    // Handle the body first so that we can get the backedge count.
1159
31
    BreakContinueStack.emplace_back();
1160
31
    extendRegion(S->getBody());
1161
31
    Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1162
31
    BreakContinue BodyBC = BreakContinueStack.pop_back_val();
1163
1164
31
    bool BodyHasTerminateStmt = HasTerminateStmt;
1165
31
    HasTerminateStmt = false;
1166
1167
    // The increment is essentially part of the body but it needs to include
1168
    // the count for all the continue statements.
1169
31
    BreakContinue IncrementBC;
1170
31
    if (const Stmt *Inc = S->getInc()) {
1171
19
      propagateCounts(addCounters(BackedgeCount, BodyBC.ContinueCount), Inc);
1172
19
      IncrementBC = BreakContinueStack.pop_back_val();
1173
19
    }
1174
1175
    // Go back to handle the condition.
1176
31
    Counter CondCount = addCounters(
1177
31
        addCounters(ParentCount, BackedgeCount, BodyBC.ContinueCount),
1178
31
        IncrementBC.ContinueCount);
1179
31
    if (const Expr *Cond = S->getCond()) {
1180
19
      propagateCounts(CondCount, Cond);
1181
19
      adjustForOutOfOrderTraversal(getEnd(S));
1182
19
    }
1183
1184
    // The body count applies to the area immediately after the increment.
1185
31
    auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1186
31
    if (Gap)
1187
27
      fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1188
1189
31
    Counter OutCount = addCounters(BodyBC.BreakCount, IncrementBC.BreakCount,
1190
31
                                   subtractCounters(CondCount, BodyCount));
1191
31
    if (OutCount != ParentCount) {
1192
9
      pushRegion(OutCount);
1193
9
      GapRegionCounter = OutCount;
1194
9
      if (BodyHasTerminateStmt)
1195
8
        HasTerminateStmt = true;
1196
9
    }
1197
1198
    // Create Branch Region around condition.
1199
31
    createBranchRegion(S->getCond(), BodyCount,
1200
31
                       subtractCounters(CondCount, BodyCount));
1201
31
  }
1202
1203
3
  void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
1204
3
    extendRegion(S);
1205
3
    if (S->getInit())
1206
0
      Visit(S->getInit());
1207
3
    Visit(S->getLoopVarStmt());
1208
3
    Visit(S->getRangeStmt());
1209
1210
3
    Counter ParentCount = getRegion().getCounter();
1211
3
    Counter BodyCount = getRegionCounter(S);
1212
1213
3
    BreakContinueStack.push_back(BreakContinue());
1214
3
    extendRegion(S->getBody());
1215
3
    Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1216
3
    BreakContinue BC = BreakContinueStack.pop_back_val();
1217
1218
3
    bool BodyHasTerminateStmt = HasTerminateStmt;
1219
3
    HasTerminateStmt = false;
1220
1221
    // The body count applies to the area immediately after the range.
1222
3
    auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1223
3
    if (Gap)
1224
3
      fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1225
1226
3
    Counter LoopCount =
1227
3
        addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1228
3
    Counter OutCount =
1229
3
        addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
1230
3
    if (OutCount != ParentCount) {
1231
1
      pushRegion(OutCount);
1232
1
      GapRegionCounter = OutCount;
1233
1
      if (BodyHasTerminateStmt)
1234
1
        HasTerminateStmt = true;
1235
1
    }
1236
1237
    // Create Branch Region around condition.
1238
3
    createBranchRegion(S->getCond(), BodyCount,
1239
3
                       subtractCounters(LoopCount, BodyCount));
1240
3
  }
1241
1242
1
  void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
1243
1
    extendRegion(S);
1244
1
    Visit(S->getElement());
1245
1246
1
    Counter ParentCount = getRegion().getCounter();
1247
1
    Counter BodyCount = getRegionCounter(S);
1248
1249
1
    BreakContinueStack.push_back(BreakContinue());
1250
1
    extendRegion(S->getBody());
1251
1
    Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1252
1
    BreakContinue BC = BreakContinueStack.pop_back_val();
1253
1254
    // The body count applies to the area immediately after the collection.
1255
1
    auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1256
1
    if (Gap)
1257
1
      fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1258
1259
1
    Counter LoopCount =
1260
1
        addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1261
1
    Counter OutCount =
1262
1
        addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
1263
1
    if (OutCount != ParentCount) {
1264
0
      pushRegion(OutCount);
1265
0
      GapRegionCounter = OutCount;
1266
0
    }
1267
1
  }
1268
1269
18
  void VisitSwitchStmt(const SwitchStmt *S) {
1270
18
    extendRegion(S);
1271
18
    if (S->getInit())
1272
1
      Visit(S->getInit());
1273
18
    Visit(S->getCond());
1274
1275
18
    BreakContinueStack.push_back(BreakContinue());
1276
1277
18
    const Stmt *Body = S->getBody();
1278
18
    extendRegion(Body);
1279
18
    if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
1280
14
      if (!CS->body_empty()) {
1281
        // Make a region for the body of the switch.  If the body starts with
1282
        // a case, that case will reuse this region; otherwise, this covers
1283
        // the unreachable code at the beginning of the switch body.
1284
12
        size_t Index = pushRegion(Counter::getZero(), getStart(CS));
1285
12
        getRegion().setGap(true);
1286
12
        Visit(Body);
1287
1288
        // Set the end for the body of the switch, if it isn't already set.
1289
65
        for (size_t i = RegionStack.size(); i != Index; 
--i53
) {
1290
53
          if (!RegionStack[i - 1].hasEndLoc())
1291
33
            RegionStack[i - 1].setEndLoc(getEnd(CS->body_back()));
1292
53
        }
1293
1294
12
        popRegions(Index);
1295
12
      }
1296
14
    } else
1297
4
      propagateCounts(Counter::getZero(), Body);
1298
18
    BreakContinue BC = BreakContinueStack.pop_back_val();
1299
1300
18
    if (!BreakContinueStack.empty())
1301
2
      BreakContinueStack.back().ContinueCount = addCounters(
1302
2
          BreakContinueStack.back().ContinueCount, BC.ContinueCount);
1303
1304
18
    Counter ParentCount = getRegion().getCounter();
1305
18
    Counter ExitCount = getRegionCounter(S);
1306
18
    SourceLocation ExitLoc = getEnd(S);
1307
18
    pushRegion(ExitCount);
1308
18
    GapRegionCounter = ExitCount;
1309
1310
    // Ensure that handleFileExit recognizes when the end location is located
1311
    // in a different file.
1312
18
    MostRecentLocation = getStart(S);
1313
18
    handleFileExit(ExitLoc);
1314
1315
    // Create a Branch Region around each Case. Subtract the case's
1316
    // counter from the Parent counter to track the "False" branch count.
1317
18
    Counter CaseCountSum;
1318
18
    bool HasDefaultCase = false;
1319
18
    const SwitchCase *Case = S->getSwitchCaseList();
1320
50
    for (; Case; 
Case = Case->getNextSwitchCase()32
) {
1321
32
      HasDefaultCase = HasDefaultCase || 
isa<DefaultStmt>(Case)24
;
1322
32
      CaseCountSum = addCounters(CaseCountSum, getRegionCounter(Case));
1323
32
      createSwitchCaseRegion(
1324
32
          Case, getRegionCounter(Case),
1325
32
          subtractCounters(ParentCount, getRegionCounter(Case)));
1326
32
    }
1327
1328
    // If no explicit default case exists, create a branch region to represent
1329
    // the hidden branch, which will be added later by the CodeGen. This region
1330
    // will be associated with the switch statement's condition.
1331
18
    if (!HasDefaultCase) {
1332
9
      Counter DefaultTrue = subtractCounters(ParentCount, CaseCountSum);
1333
9
      Counter DefaultFalse = subtractCounters(ParentCount, DefaultTrue);
1334
9
      createBranchRegion(S->getCond(), DefaultTrue, DefaultFalse);
1335
9
    }
1336
18
  }
1337
1338
32
  void VisitSwitchCase(const SwitchCase *S) {
1339
32
    extendRegion(S);
1340
1341
32
    SourceMappingRegion &Parent = getRegion();
1342
1343
32
    Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
1344
    // Reuse the existing region if it starts at our label. This is typical of
1345
    // the first case in a switch.
1346
32
    if (Parent.hasStartLoc() && Parent.getBeginLoc() == getStart(S))
1347
13
      Parent.setCounter(Count);
1348
19
    else
1349
19
      pushRegion(Count, getStart(S));
1350
1351
32
    GapRegionCounter = Count;
1352
1353
32
    if (const auto *CS = dyn_cast<CaseStmt>(S)) {
1354
23
      Visit(CS->getLHS());
1355
23
      if (const Expr *RHS = CS->getRHS())
1356
0
        Visit(RHS);
1357
23
    }
1358
32
    Visit(S->getSubStmt());
1359
32
  }
1360
1361
131
  void VisitIfStmt(const IfStmt *S) {
1362
131
    extendRegion(S);
1363
131
    if (S->getInit())
1364
1
      Visit(S->getInit());
1365
1366
    // Extend into the condition before we propagate through it below - this is
1367
    // needed to handle macros that generate the "if" but not the condition.
1368
131
    extendRegion(S->getCond());
1369
1370
131
    Counter ParentCount = getRegion().getCounter();
1371
131
    Counter ThenCount = getRegionCounter(S);
1372
1373
    // Emitting a counter for the condition makes it easier to interpret the
1374
    // counter for the body when looking at the coverage.
1375
131
    propagateCounts(ParentCount, S->getCond());
1376
1377
    // The 'then' count applies to the area immediately after the condition.
1378
131
    auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getThen()));
1379
131
    if (Gap)
1380
115
      fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ThenCount);
1381
1382
131
    extendRegion(S->getThen());
1383
131
    Counter OutCount = propagateCounts(ThenCount, S->getThen());
1384
1385
131
    Counter ElseCount = subtractCounters(ParentCount, ThenCount);
1386
131
    if (const Stmt *Else = S->getElse()) {
1387
38
      bool ThenHasTerminateStmt = HasTerminateStmt;
1388
38
      HasTerminateStmt = false;
1389
1390
      // The 'else' count applies to the area immediately after the 'then'.
1391
38
      Gap = findGapAreaBetween(getEnd(S->getThen()), getStart(Else));
1392
38
      if (Gap)
1393
34
        fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ElseCount);
1394
38
      extendRegion(Else);
1395
38
      OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
1396
1397
38
      if (ThenHasTerminateStmt)
1398
16
        HasTerminateStmt = true;
1399
38
    } else
1400
93
      OutCount = addCounters(OutCount, ElseCount);
1401
1402
131
    if (OutCount != ParentCount) {
1403
70
      pushRegion(OutCount);
1404
70
      GapRegionCounter = OutCount;
1405
70
    }
1406
1407
    // Create Branch Region around condition.
1408
131
    createBranchRegion(S->getCond(), ThenCount,
1409
131
                       subtractCounters(ParentCount, ThenCount));
1410
131
  }
1411
1412
7
  void VisitCXXTryStmt(const CXXTryStmt *S) {
1413
7
    extendRegion(S);
1414
    // Handle macros that generate the "try" but not the rest.
1415
7
    extendRegion(S->getTryBlock());
1416
1417
7
    Counter ParentCount = getRegion().getCounter();
1418
7
    propagateCounts(ParentCount, S->getTryBlock());
1419
1420
16
    for (unsigned I = 0, E = S->getNumHandlers(); I < E; 
++I9
)
1421
9
      Visit(S->getHandler(I));
1422
1423
7
    Counter ExitCount = getRegionCounter(S);
1424
7
    pushRegion(ExitCount);
1425
7
  }
1426
1427
9
  void VisitCXXCatchStmt(const CXXCatchStmt *S) {
1428
9
    propagateCounts(getRegionCounter(S), S->getHandlerBlock());
1429
9
  }
1430
1431
13
  void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
1432
13
    extendRegion(E);
1433
1434
13
    Counter ParentCount = getRegion().getCounter();
1435
13
    Counter TrueCount = getRegionCounter(E);
1436
1437
13
    propagateCounts(ParentCount, E->getCond());
1438
1439
13
    if (!isa<BinaryConditionalOperator>(E)) {
1440
      // The 'then' count applies to the area immediately after the condition.
1441
11
      auto Gap =
1442
11
          findGapAreaBetween(E->getQuestionLoc(), getStart(E->getTrueExpr()));
1443
11
      if (Gap)
1444
9
        fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), TrueCount);
1445
1446
11
      extendRegion(E->getTrueExpr());
1447
11
      propagateCounts(TrueCount, E->getTrueExpr());
1448
11
    }
1449
1450
13
    extendRegion(E->getFalseExpr());
1451
13
    propagateCounts(subtractCounters(ParentCount, TrueCount),
1452
13
                    E->getFalseExpr());
1453
1454
    // Create Branch Region around condition.
1455
13
    createBranchRegion(E->getCond(), TrueCount,
1456
13
                       subtractCounters(ParentCount, TrueCount));
1457
13
  }
1458
1459
47
  void VisitBinLAnd(const BinaryOperator *E) {
1460
47
    extendRegion(E->getLHS());
1461
47
    propagateCounts(getRegion().getCounter(), E->getLHS());
1462
47
    handleFileExit(getEnd(E->getLHS()));
1463
1464
    // Counter tracks the right hand side of a logical and operator.
1465
47
    extendRegion(E->getRHS());
1466
47
    propagateCounts(getRegionCounter(E), E->getRHS());
1467
1468
    // Extract the RHS's Execution Counter.
1469
47
    Counter RHSExecCnt = getRegionCounter(E);
1470
1471
    // Extract the RHS's "True" Instance Counter.
1472
47
    Counter RHSTrueCnt = getRegionCounter(E->getRHS());
1473
1474
    // Extract the Parent Region Counter.
1475
47
    Counter ParentCnt = getRegion().getCounter();
1476
1477
    // Create Branch Region around LHS condition.
1478
47
    createBranchRegion(E->getLHS(), RHSExecCnt,
1479
47
                       subtractCounters(ParentCnt, RHSExecCnt));
1480
1481
    // Create Branch Region around RHS condition.
1482
47
    createBranchRegion(E->getRHS(), RHSTrueCnt,
1483
47
                       subtractCounters(RHSExecCnt, RHSTrueCnt));
1484
47
  }
1485
1486
37
  void VisitBinLOr(const BinaryOperator *E) {
1487
37
    extendRegion(E->getLHS());
1488
37
    propagateCounts(getRegion().getCounter(), E->getLHS());
1489
37
    handleFileExit(getEnd(E->getLHS()));
1490
1491
    // Counter tracks the right hand side of a logical or operator.
1492
37
    extendRegion(E->getRHS());
1493
37
    propagateCounts(getRegionCounter(E), E->getRHS());
1494
1495
    // Extract the RHS's Execution Counter.
1496
37
    Counter RHSExecCnt = getRegionCounter(E);
1497
1498
    // Extract the RHS's "False" Instance Counter.
1499
37
    Counter RHSFalseCnt = getRegionCounter(E->getRHS());
1500
1501
    // Extract the Parent Region Counter.
1502
37
    Counter ParentCnt = getRegion().getCounter();
1503
1504
    // Create Branch Region around LHS condition.
1505
37
    createBranchRegion(E->getLHS(), subtractCounters(ParentCnt, RHSExecCnt),
1506
37
                       RHSExecCnt);
1507
1508
    // Create Branch Region around RHS condition.
1509
37
    createBranchRegion(E->getRHS(), subtractCounters(RHSExecCnt, RHSFalseCnt),
1510
37
                       RHSFalseCnt);
1511
37
  }
1512
1513
2
  void VisitLambdaExpr(const LambdaExpr *LE) {
1514
    // Lambdas are treated as their own functions for now, so we shouldn't
1515
    // propagate counts into them.
1516
2
  }
1517
};
1518
1519
} // end anonymous namespace
1520
1521
static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
1522
                 ArrayRef<CounterExpression> Expressions,
1523
208
                 ArrayRef<CounterMappingRegion> Regions) {
1524
208
  OS << FunctionName << ":\n";
1525
208
  CounterMappingContext Ctx(Expressions);
1526
2.09k
  for (const auto &R : Regions) {
1527
2.09k
    OS.indent(2);
1528
2.09k
    switch (R.Kind) {
1529
1.18k
    case CounterMappingRegion::CodeRegion:
1530
1.18k
      break;
1531
177
    case CounterMappingRegion::ExpansionRegion:
1532
177
      OS << "Expansion,";
1533
177
      break;
1534
36
    case CounterMappingRegion::SkippedRegion:
1535
36
      OS << "Skipped,";
1536
36
      break;
1537
325
    case CounterMappingRegion::GapRegion:
1538
325
      OS << "Gap,";
1539
325
      break;
1540
368
    case CounterMappingRegion::BranchRegion:
1541
368
      OS << "Branch,";
1542
368
      break;
1543
2.09k
    }
1544
1545
2.09k
    OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
1546
2.09k
       << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
1547
2.09k
    Ctx.dump(R.Count, OS);
1548
1549
2.09k
    if (R.Kind == CounterMappingRegion::BranchRegion) {
1550
368
      OS << ", ";
1551
368
      Ctx.dump(R.FalseCount, OS);
1552
368
    }
1553
1554
2.09k
    if (R.Kind == CounterMappingRegion::ExpansionRegion)
1555
177
      OS << " (Expanded file = " << R.ExpandedFileID << ")";
1556
2.09k
    OS << "\n";
1557
2.09k
  }
1558
208
}
1559
1560
CoverageMappingModuleGen::CoverageMappingModuleGen(
1561
    CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
1562
80
    : CGM(CGM), SourceInfo(SourceInfo) {
1563
80
  CoveragePrefixMap = CGM.getCodeGenOpts().CoveragePrefixMap;
1564
80
}
1565
1566
288
std::string CoverageMappingModuleGen::getCurrentDirname() {
1567
288
  if (!CGM.getCodeGenOpts().CoverageCompilationDir.empty())
1568
2
    return CGM.getCodeGenOpts().CoverageCompilationDir;
1569
1570
286
  SmallString<256> CWD;
1571
286
  llvm::sys::fs::current_path(CWD);
1572
286
  return CWD.str().str();
1573
288
}
1574
1575
604
std::string CoverageMappingModuleGen::normalizeFilename(StringRef Filename) {
1576
604
  llvm::SmallString<256> Path(Filename);
1577
604
  llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1578
604
  for (const auto &Entry : CoveragePrefixMap) {
1579
6
    if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
1580
4
      break;
1581
6
  }
1582
604
  return Path.str().str();
1583
604
}
1584
1585
static std::string getInstrProfSection(const CodeGenModule &CGM,
1586
328
                                       llvm::InstrProfSectKind SK) {
1587
328
  return llvm::getInstrProfSectionName(
1588
328
      SK, CGM.getContext().getTargetInfo().getTriple().getObjectFormat());
1589
328
}
1590
1591
void CoverageMappingModuleGen::emitFunctionMappingRecord(
1592
248
    const FunctionInfo &Info, uint64_t FilenamesRef) {
1593
248
  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1594
1595
  // Assign a name to the function record. This is used to merge duplicates.
1596
248
  std::string FuncRecordName = "__covrec_" + llvm::utohexstr(Info.NameHash);
1597
1598
  // A dummy description for a function included-but-not-used in a TU can be
1599
  // replaced by full description provided by a different TU. The two kinds of
1600
  // descriptions play distinct roles: therefore, assign them different names
1601
  // to prevent `linkonce_odr` merging.
1602
248
  if (Info.IsUsed)
1603
228
    FuncRecordName += "u";
1604
1605
  // Create the function record type.
1606
248
  const uint64_t NameHash = Info.NameHash;
1607
248
  const uint64_t FuncHash = Info.FuncHash;
1608
248
  const std::string &CoverageMapping = Info.CoverageMapping;
1609
1.24k
#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
1610
248
  llvm::Type *FunctionRecordTypes[] = {
1611
248
#include "llvm/ProfileData/InstrProfData.inc"
1612
248
  };
1613
248
  auto *FunctionRecordTy =
1614
248
      llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes),
1615
248
                            /*isPacked=*/true);
1616
1617
  // Create the function record constant.
1618
1.24k
#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
1619
248
  llvm::Constant *FunctionRecordVals[] = {
1620
248
      #include "llvm/ProfileData/InstrProfData.inc"
1621
248
  };
1622
248
  auto *FuncRecordConstant = llvm::ConstantStruct::get(
1623
248
      FunctionRecordTy, makeArrayRef(FunctionRecordVals));
1624
1625
  // Create the function record global.
1626
248
  auto *FuncRecord = new llvm::GlobalVariable(
1627
248
      CGM.getModule(), FunctionRecordTy, /*isConstant=*/true,
1628
248
      llvm::GlobalValue::LinkOnceODRLinkage, FuncRecordConstant,
1629
248
      FuncRecordName);
1630
248
  FuncRecord->setVisibility(llvm::GlobalValue::HiddenVisibility);
1631
248
  FuncRecord->setSection(getInstrProfSection(CGM, llvm::IPSK_covfun));
1632
248
  FuncRecord->setAlignment(llvm::Align(8));
1633
248
  if (CGM.supportsCOMDAT())
1634
35
    FuncRecord->setComdat(CGM.getModule().getOrInsertComdat(FuncRecordName));
1635
1636
  // Make sure the data doesn't get deleted.
1637
248
  CGM.addUsedGlobal(FuncRecord);
1638
248
}
1639
1640
void CoverageMappingModuleGen::addFunctionMappingRecord(
1641
    llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
1642
248
    const std::string &CoverageMapping, bool IsUsed) {
1643
248
  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1644
248
  const uint64_t NameHash = llvm::IndexedInstrProf::ComputeHash(NameValue);
1645
248
  FunctionRecords.push_back({NameHash, FuncHash, CoverageMapping, IsUsed});
1646
1647
248
  if (!IsUsed)
1648
20
    FunctionNames.push_back(
1649
20
        llvm::ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx)));
1650
1651
248
  if (CGM.getCodeGenOpts().DumpCoverageMapping) {
1652
    // Dump the coverage mapping data for this function by decoding the
1653
    // encoded data. This allows us to dump the mapping regions which were
1654
    // also processed by the CoverageMappingWriter which performs
1655
    // additional minimization operations such as reducing the number of
1656
    // expressions.
1657
208
    llvm::SmallVector<std::string, 16> FilenameStrs;
1658
208
    std::vector<StringRef> Filenames;
1659
208
    std::vector<CounterExpression> Expressions;
1660
208
    std::vector<CounterMappingRegion> Regions;
1661
208
    FilenameStrs.resize(FileEntries.size() + 1);
1662
208
    FilenameStrs[0] = normalizeFilename(getCurrentDirname());
1663
225
    for (const auto &Entry : FileEntries) {
1664
225
      auto I = Entry.second;
1665
225
      FilenameStrs[I] = normalizeFilename(Entry.first->getName());
1666
225
    }
1667
208
    ArrayRef<std::string> FilenameRefs = llvm::makeArrayRef(FilenameStrs);
1668
208
    RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
1669
208
                                    Expressions, Regions);
1670
208
    if (Reader.read())
1671
0
      return;
1672
208
    dump(llvm::outs(), NameValue, Expressions, Regions);
1673
208
  }
1674
248
}
1675
1676
80
void CoverageMappingModuleGen::emit() {
1677
80
  if (FunctionRecords.empty())
1678
0
    return;
1679
80
  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
1680
80
  auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
1681
1682
  // Create the filenames and merge them with coverage mappings
1683
80
  llvm::SmallVector<std::string, 16> FilenameStrs;
1684
80
  FilenameStrs.resize(FileEntries.size() + 1);
1685
  // The first filename is the current working directory.
1686
80
  FilenameStrs[0] = normalizeFilename(getCurrentDirname());
1687
91
  for (const auto &Entry : FileEntries) {
1688
91
    auto I = Entry.second;
1689
91
    FilenameStrs[I] = normalizeFilename(Entry.first->getName());
1690
91
  }
1691
1692
80
  std::string Filenames;
1693
80
  {
1694
80
    llvm::raw_string_ostream OS(Filenames);
1695
80
    CoverageFilenamesSectionWriter(FilenameStrs).write(OS);
1696
80
  }
1697
80
  auto *FilenamesVal =
1698
80
      llvm::ConstantDataArray::getString(Ctx, Filenames, false);
1699
80
  const int64_t FilenamesRef = llvm::IndexedInstrProf::ComputeHash(Filenames);
1700
1701
  // Emit the function records.
1702
80
  for (const FunctionInfo &Info : FunctionRecords)
1703
248
    emitFunctionMappingRecord(Info, FilenamesRef);
1704
1705
80
  const unsigned NRecords = 0;
1706
80
  const size_t FilenamesSize = Filenames.size();
1707
80
  const unsigned CoverageMappingSize = 0;
1708
80
  llvm::Type *CovDataHeaderTypes[] = {
1709
320
#define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
1710
80
#include "llvm/ProfileData/InstrProfData.inc"
1711
80
  };
1712
80
  auto CovDataHeaderTy =
1713
80
      llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes));
1714
80
  llvm::Constant *CovDataHeaderVals[] = {
1715
320
#define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
1716
80
#include "llvm/ProfileData/InstrProfData.inc"
1717
80
  };
1718
80
  auto CovDataHeaderVal = llvm::ConstantStruct::get(
1719
80
      CovDataHeaderTy, makeArrayRef(CovDataHeaderVals));
1720
1721
  // Create the coverage data record
1722
80
  llvm::Type *CovDataTypes[] = {CovDataHeaderTy, FilenamesVal->getType()};
1723
80
  auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
1724
80
  llvm::Constant *TUDataVals[] = {CovDataHeaderVal, FilenamesVal};
1725
80
  auto CovDataVal =
1726
80
      llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
1727
80
  auto CovData = new llvm::GlobalVariable(
1728
80
      CGM.getModule(), CovDataTy, true, llvm::GlobalValue::PrivateLinkage,
1729
80
      CovDataVal, llvm::getCoverageMappingVarName());
1730
1731
80
  CovData->setSection(getInstrProfSection(CGM, llvm::IPSK_covmap));
1732
80
  CovData->setAlignment(llvm::Align(8));
1733
1734
  // Make sure the data doesn't get deleted.
1735
80
  CGM.addUsedGlobal(CovData);
1736
  // Create the deferred function records array
1737
80
  if (!FunctionNames.empty()) {
1738
9
    auto NamesArrTy = llvm::ArrayType::get(llvm::Type::getInt8PtrTy(Ctx),
1739
9
                                           FunctionNames.size());
1740
9
    auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
1741
    // This variable will *NOT* be emitted to the object file. It is used
1742
    // to pass the list of names referenced to codegen.
1743
9
    new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
1744
9
                             llvm::GlobalValue::InternalLinkage, NamesArrVal,
1745
9
                             llvm::getCoverageUnusedNamesVarName());
1746
9
  }
1747
80
}
1748
1749
425
unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
1750
425
  auto It = FileEntries.find(File);
1751
425
  if (It != FileEntries.end())
1752
334
    return It->second;
1753
91
  unsigned FileID = FileEntries.size() + 1;
1754
91
  FileEntries.insert(std::make_pair(File, FileID));
1755
91
  return FileID;
1756
425
}
1757
1758
void CoverageMappingGen::emitCounterMapping(const Decl *D,
1759
229
                                            llvm::raw_ostream &OS) {
1760
229
  assert(CounterMap);
1761
0
  CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
1762
229
  Walker.VisitDecl(D);
1763
229
  Walker.write(OS);
1764
229
}
1765
1766
void CoverageMappingGen::emitEmptyMapping(const Decl *D,
1767
20
                                          llvm::raw_ostream &OS) {
1768
20
  EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
1769
20
  Walker.VisitDecl(D);
1770
20
  Walker.write(OS);
1771
20
}