Coverage Report

Created: 2022-07-16 07:03

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