Coverage Report

Created: 2021-01-23 06:44

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