Coverage Report

Created: 2022-05-17 06:19

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