clang 19.0.0git
CoverageMappingGen.cpp
Go to the documentation of this file.
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"
19#include "clang/Lex/Lexer.h"
20#include "llvm/ADT/DenseSet.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#include <optional>
30
31// This selects the coverage mapping format defined when `InstrProfData.inc`
32// is textually included.
33#define COVMAP_V3
34
35namespace llvm {
36cl::opt<bool>
37 EnableSingleByteCoverage("enable-single-byte-coverage",
38 llvm::cl::ZeroOrMore,
39 llvm::cl::desc("Enable single byte coverage"),
40 llvm::cl::Hidden, llvm::cl::init(false));
41} // namespace llvm
42
43static llvm::cl::opt<bool> EmptyLineCommentCoverage(
44 "emptyline-comment-coverage",
45 llvm::cl::desc("Emit emptylines and comment lines as skipped regions (only "
46 "disable it on test)"),
47 llvm::cl::init(true), llvm::cl::Hidden);
48
49llvm::cl::opt<bool> SystemHeadersCoverage(
50 "system-headers-coverage",
51 llvm::cl::desc("Enable collecting coverage from system headers"),
52 llvm::cl::init(false), llvm::cl::Hidden);
53
54using namespace clang;
55using namespace CodeGen;
56using namespace llvm::coverage;
57
60 CoverageSourceInfo *CoverageInfo =
62 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(CoverageInfo));
64 PP.addCommentHandler(CoverageInfo);
65 PP.setEmptylineHandler(CoverageInfo);
66 PP.setPreprocessToken(true);
67 PP.setTokenWatcher([CoverageInfo](clang::Token Tok) {
68 // Update previous token location.
69 CoverageInfo->PrevTokLoc = Tok.getLocation();
70 if (Tok.getKind() != clang::tok::eod)
71 CoverageInfo->updateNextTokLoc(Tok.getLocation());
72 });
73 }
74 return CoverageInfo;
75}
76
78 SkippedRange::Kind RangeKind) {
79 if (EmptyLineCommentCoverage && !SkippedRanges.empty() &&
80 PrevTokLoc == SkippedRanges.back().PrevTokLoc &&
81 SourceMgr.isWrittenInSameFile(SkippedRanges.back().Range.getEnd(),
82 Range.getBegin()))
83 SkippedRanges.back().Range.setEnd(Range.getEnd());
84 else
85 SkippedRanges.push_back({Range, RangeKind, PrevTokLoc});
86}
87
89 AddSkippedRange(Range, SkippedRange::PPIfElse);
90}
91
93 AddSkippedRange(Range, SkippedRange::EmptyLine);
94}
95
97 AddSkippedRange(Range, SkippedRange::Comment);
98 return false;
99}
100
102 if (!SkippedRanges.empty() && SkippedRanges.back().NextTokLoc.isInvalid())
103 SkippedRanges.back().NextTokLoc = Loc;
104}
105
106namespace {
107/// A region of source code that can be mapped to a counter.
108class SourceMappingRegion {
109 /// Primary Counter that is also used for Branch Regions for "True" branches.
110 Counter Count;
111
112 /// Secondary Counter used for Branch Regions for "False" branches.
113 std::optional<Counter> FalseCount;
114
115 /// Parameters used for Modified Condition/Decision Coverage
116 mcdc::Parameters MCDCParams;
117
118 /// The region's starting location.
119 std::optional<SourceLocation> LocStart;
120
121 /// The region's ending location.
122 std::optional<SourceLocation> LocEnd;
123
124 /// Whether this region is a gap region. The count from a gap region is set
125 /// as the line execution count if there are no other regions on the line.
126 bool GapRegion;
127
128 /// Whetever this region is skipped ('if constexpr' or 'if consteval' untaken
129 /// branch, or anything skipped but not empty line / comments)
130 bool SkippedRegion;
131
132public:
133 SourceMappingRegion(Counter Count, std::optional<SourceLocation> LocStart,
134 std::optional<SourceLocation> LocEnd,
135 bool GapRegion = false)
136 : Count(Count), LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion),
137 SkippedRegion(false) {}
138
139 SourceMappingRegion(Counter Count, std::optional<Counter> FalseCount,
140 mcdc::Parameters MCDCParams,
141 std::optional<SourceLocation> LocStart,
142 std::optional<SourceLocation> LocEnd,
143 bool GapRegion = false)
144 : Count(Count), FalseCount(FalseCount), MCDCParams(MCDCParams),
145 LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion),
146 SkippedRegion(false) {}
147
148 SourceMappingRegion(mcdc::Parameters MCDCParams,
149 std::optional<SourceLocation> LocStart,
150 std::optional<SourceLocation> LocEnd)
151 : MCDCParams(MCDCParams), LocStart(LocStart), LocEnd(LocEnd),
152 GapRegion(false), SkippedRegion(false) {}
153
154 const Counter &getCounter() const { return Count; }
155
156 const Counter &getFalseCounter() const {
157 assert(FalseCount && "Region has no alternate counter");
158 return *FalseCount;
159 }
160
161 void setCounter(Counter C) { Count = C; }
162
163 bool hasStartLoc() const { return LocStart.has_value(); }
164
165 void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
166
167 SourceLocation getBeginLoc() const {
168 assert(LocStart && "Region has no start location");
169 return *LocStart;
170 }
171
172 bool hasEndLoc() const { return LocEnd.has_value(); }
173
174 void setEndLoc(SourceLocation Loc) {
175 assert(Loc.isValid() && "Setting an invalid end location");
176 LocEnd = Loc;
177 }
178
179 SourceLocation getEndLoc() const {
180 assert(LocEnd && "Region has no end location");
181 return *LocEnd;
182 }
183
184 bool isGap() const { return GapRegion; }
185
186 void setGap(bool Gap) { GapRegion = Gap; }
187
188 bool isSkipped() const { return SkippedRegion; }
189
190 void setSkipped(bool Skipped) { SkippedRegion = Skipped; }
191
192 bool isBranch() const { return FalseCount.has_value(); }
193
194 bool isMCDCDecision() const {
195 return std::holds_alternative<mcdc::DecisionParameters>(MCDCParams);
196 }
197
198 const auto &getMCDCDecisionParams() const {
199 return mcdc::getParams<const mcdc::DecisionParameters>(MCDCParams);
200 }
201
202 const mcdc::Parameters &getMCDCParams() const { return MCDCParams; }
203};
204
205/// Spelling locations for the start and end of a source region.
206struct SpellingRegion {
207 /// The line where the region starts.
208 unsigned LineStart;
209
210 /// The column where the region starts.
211 unsigned ColumnStart;
212
213 /// The line where the region ends.
214 unsigned LineEnd;
215
216 /// The column where the region ends.
217 unsigned ColumnEnd;
218
219 SpellingRegion(SourceManager &SM, SourceLocation LocStart,
220 SourceLocation LocEnd) {
221 LineStart = SM.getSpellingLineNumber(LocStart);
222 ColumnStart = SM.getSpellingColumnNumber(LocStart);
223 LineEnd = SM.getSpellingLineNumber(LocEnd);
224 ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
225 }
226
227 SpellingRegion(SourceManager &SM, SourceMappingRegion &R)
228 : SpellingRegion(SM, R.getBeginLoc(), R.getEndLoc()) {}
229
230 /// Check if the start and end locations appear in source order, i.e
231 /// top->bottom, left->right.
232 bool isInSourceOrder() const {
233 return (LineStart < LineEnd) ||
234 (LineStart == LineEnd && ColumnStart <= ColumnEnd);
235 }
236};
237
238/// Provides the common functionality for the different
239/// coverage mapping region builders.
240class CoverageMappingBuilder {
241public:
244 const LangOptions &LangOpts;
245
246private:
247 /// Map of clang's FileIDs to IDs used for coverage mapping.
248 llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
249 FileIDMapping;
250
251public:
252 /// The coverage mapping regions for this function
254 /// The source mapping regions for this function.
255 std::vector<SourceMappingRegion> SourceRegions;
256
257 /// A set of regions which can be used as a filter.
258 ///
259 /// It is produced by emitExpansionRegions() and is used in
260 /// emitSourceRegions() to suppress producing code regions if
261 /// the same area is covered by expansion regions.
262 typedef llvm::SmallSet<std::pair<SourceLocation, SourceLocation>, 8>
263 SourceRegionFilter;
264
265 CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
266 const LangOptions &LangOpts)
267 : CVM(CVM), SM(SM), LangOpts(LangOpts) {}
268
269 /// Return the precise end location for the given token.
270 SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
271 // We avoid getLocForEndOfToken here, because it doesn't do what we want for
272 // macro locations, which we just treat as expanded files.
273 unsigned TokLen =
274 Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
275 return Loc.getLocWithOffset(TokLen);
276 }
277
278 /// Return the start location of an included file or expanded macro.
279 SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
280 if (Loc.isMacroID())
281 return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
282 return SM.getLocForStartOfFile(SM.getFileID(Loc));
283 }
284
285 /// Return the end location of an included file or expanded macro.
286 SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
287 if (Loc.isMacroID())
288 return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
289 SM.getFileOffset(Loc));
290 return SM.getLocForEndOfFile(SM.getFileID(Loc));
291 }
292
293 /// Find out where the current file is included or macro is expanded.
294 SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
295 return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).getBegin()
296 : SM.getIncludeLoc(SM.getFileID(Loc));
297 }
298
299 /// Return true if \c Loc is a location in a built-in macro.
300 bool isInBuiltin(SourceLocation Loc) {
301 return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>";
302 }
303
304 /// Check whether \c Loc is included or expanded from \c Parent.
305 bool isNestedIn(SourceLocation Loc, FileID Parent) {
306 do {
307 Loc = getIncludeOrExpansionLoc(Loc);
308 if (Loc.isInvalid())
309 return false;
310 } while (!SM.isInFileID(Loc, Parent));
311 return true;
312 }
313
314 /// Get the start of \c S ignoring macro arguments and builtin macros.
315 SourceLocation getStart(const Stmt *S) {
316 SourceLocation Loc = S->getBeginLoc();
317 while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
318 Loc = SM.getImmediateExpansionRange(Loc).getBegin();
319 return Loc;
320 }
321
322 /// Get the end of \c S ignoring macro arguments and builtin macros.
323 SourceLocation getEnd(const Stmt *S) {
324 SourceLocation Loc = S->getEndLoc();
325 while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
326 Loc = SM.getImmediateExpansionRange(Loc).getBegin();
327 return getPreciseTokenLocEnd(Loc);
328 }
329
330 /// Find the set of files we have regions for and assign IDs
331 ///
332 /// Fills \c Mapping with the virtual file mapping needed to write out
333 /// coverage and collects the necessary file information to emit source and
334 /// expansion regions.
335 void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
336 FileIDMapping.clear();
337
338 llvm::SmallSet<FileID, 8> Visited;
340 for (auto &Region : SourceRegions) {
341 SourceLocation Loc = Region.getBeginLoc();
342
343 // Replace Loc with FileLoc if it is expanded with system headers.
344 if (!SystemHeadersCoverage && SM.isInSystemMacro(Loc)) {
345 auto BeginLoc = SM.getSpellingLoc(Loc);
346 auto EndLoc = SM.getSpellingLoc(Region.getEndLoc());
347 if (SM.isWrittenInSameFile(BeginLoc, EndLoc)) {
348 Loc = SM.getFileLoc(Loc);
349 Region.setStartLoc(Loc);
350 Region.setEndLoc(SM.getFileLoc(Region.getEndLoc()));
351 }
352 }
353
354 FileID File = SM.getFileID(Loc);
355 if (!Visited.insert(File).second)
356 continue;
357
358 assert(SystemHeadersCoverage ||
359 !SM.isInSystemHeader(SM.getSpellingLoc(Loc)));
360
361 unsigned Depth = 0;
362 for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
363 Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent))
364 ++Depth;
365 FileLocs.push_back(std::make_pair(Loc, Depth));
366 }
367 llvm::stable_sort(FileLocs, llvm::less_second());
368
369 for (const auto &FL : FileLocs) {
370 SourceLocation Loc = FL.first;
371 FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
372 auto Entry = SM.getFileEntryRefForID(SpellingFile);
373 if (!Entry)
374 continue;
375
376 FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
377 Mapping.push_back(CVM.getFileID(*Entry));
378 }
379 }
380
381 /// Get the coverage mapping file ID for \c Loc.
382 ///
383 /// If such file id doesn't exist, return std::nullopt.
384 std::optional<unsigned> getCoverageFileID(SourceLocation Loc) {
385 auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
386 if (Mapping != FileIDMapping.end())
387 return Mapping->second.first;
388 return std::nullopt;
389 }
390
391 /// This shrinks the skipped range if it spans a line that contains a
392 /// non-comment token. If shrinking the skipped range would make it empty,
393 /// this returns std::nullopt.
394 /// Note this function can potentially be expensive because
395 /// getSpellingLineNumber uses getLineNumber, which is expensive.
396 std::optional<SpellingRegion> adjustSkippedRange(SourceManager &SM,
397 SourceLocation LocStart,
398 SourceLocation LocEnd,
399 SourceLocation PrevTokLoc,
400 SourceLocation NextTokLoc) {
401 SpellingRegion SR{SM, LocStart, LocEnd};
402 SR.ColumnStart = 1;
403 if (PrevTokLoc.isValid() && SM.isWrittenInSameFile(LocStart, PrevTokLoc) &&
404 SR.LineStart == SM.getSpellingLineNumber(PrevTokLoc))
405 SR.LineStart++;
406 if (NextTokLoc.isValid() && SM.isWrittenInSameFile(LocEnd, NextTokLoc) &&
407 SR.LineEnd == SM.getSpellingLineNumber(NextTokLoc)) {
408 SR.LineEnd--;
409 SR.ColumnEnd++;
410 }
411 if (SR.isInSourceOrder())
412 return SR;
413 return std::nullopt;
414 }
415
416 /// Gather all the regions that were skipped by the preprocessor
417 /// using the constructs like #if or comments.
418 void gatherSkippedRegions() {
419 /// An array of the minimum lineStarts and the maximum lineEnds
420 /// for mapping regions from the appropriate source files.
422 FileLineRanges.resize(
423 FileIDMapping.size(),
424 std::make_pair(std::numeric_limits<unsigned>::max(), 0));
425 for (const auto &R : MappingRegions) {
426 FileLineRanges[R.FileID].first =
427 std::min(FileLineRanges[R.FileID].first, R.LineStart);
428 FileLineRanges[R.FileID].second =
429 std::max(FileLineRanges[R.FileID].second, R.LineEnd);
430 }
431
432 auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
433 for (auto &I : SkippedRanges) {
434 SourceRange Range = I.Range;
435 auto LocStart = Range.getBegin();
436 auto LocEnd = Range.getEnd();
437 assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
438 "region spans multiple files");
439
440 auto CovFileID = getCoverageFileID(LocStart);
441 if (!CovFileID)
442 continue;
443 std::optional<SpellingRegion> SR;
444 if (I.isComment())
445 SR = adjustSkippedRange(SM, LocStart, LocEnd, I.PrevTokLoc,
446 I.NextTokLoc);
447 else if (I.isPPIfElse() || I.isEmptyLine())
448 SR = {SM, LocStart, LocEnd};
449
450 if (!SR)
451 continue;
452 auto Region = CounterMappingRegion::makeSkipped(
453 *CovFileID, SR->LineStart, SR->ColumnStart, SR->LineEnd,
454 SR->ColumnEnd);
455 // Make sure that we only collect the regions that are inside
456 // the source code of this function.
457 if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
458 Region.LineEnd <= FileLineRanges[*CovFileID].second)
459 MappingRegions.push_back(Region);
460 }
461 }
462
463 /// Generate the coverage counter mapping regions from collected
464 /// source regions.
465 void emitSourceRegions(const SourceRegionFilter &Filter) {
466 for (const auto &Region : SourceRegions) {
467 assert(Region.hasEndLoc() && "incomplete region");
468
469 SourceLocation LocStart = Region.getBeginLoc();
470 assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
471
472 // Ignore regions from system headers unless collecting coverage from
473 // system headers is explicitly enabled.
475 SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
476 continue;
477
478 auto CovFileID = getCoverageFileID(LocStart);
479 // Ignore regions that don't have a file, such as builtin macros.
480 if (!CovFileID)
481 continue;
482
483 SourceLocation LocEnd = Region.getEndLoc();
484 assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
485 "region spans multiple files");
486
487 // Don't add code regions for the area covered by expansion regions.
488 // This not only suppresses redundant regions, but sometimes prevents
489 // creating regions with wrong counters if, for example, a statement's
490 // body ends at the end of a nested macro.
491 if (Filter.count(std::make_pair(LocStart, LocEnd)))
492 continue;
493
494 // Find the spelling locations for the mapping region.
495 SpellingRegion SR{SM, LocStart, LocEnd};
496 assert(SR.isInSourceOrder() && "region start and end out of order");
497
498 if (Region.isGap()) {
499 MappingRegions.push_back(CounterMappingRegion::makeGapRegion(
500 Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
501 SR.LineEnd, SR.ColumnEnd));
502 } else if (Region.isSkipped()) {
503 MappingRegions.push_back(CounterMappingRegion::makeSkipped(
504 *CovFileID, SR.LineStart, SR.ColumnStart, SR.LineEnd,
505 SR.ColumnEnd));
506 } else if (Region.isBranch()) {
507 MappingRegions.push_back(CounterMappingRegion::makeBranchRegion(
508 Region.getCounter(), Region.getFalseCounter(), *CovFileID,
509 SR.LineStart, SR.ColumnStart, SR.LineEnd, SR.ColumnEnd,
510 Region.getMCDCParams()));
511 } else if (Region.isMCDCDecision()) {
512 MappingRegions.push_back(CounterMappingRegion::makeDecisionRegion(
513 Region.getMCDCDecisionParams(), *CovFileID, SR.LineStart,
514 SR.ColumnStart, SR.LineEnd, SR.ColumnEnd));
515 } else {
516 MappingRegions.push_back(CounterMappingRegion::makeRegion(
517 Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
518 SR.LineEnd, SR.ColumnEnd));
519 }
520 }
521 }
522
523 /// Generate expansion regions for each virtual file we've seen.
524 SourceRegionFilter emitExpansionRegions() {
525 SourceRegionFilter Filter;
526 for (const auto &FM : FileIDMapping) {
527 SourceLocation ExpandedLoc = FM.second.second;
528 SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
529 if (ParentLoc.isInvalid())
530 continue;
531
532 auto ParentFileID = getCoverageFileID(ParentLoc);
533 if (!ParentFileID)
534 continue;
535 auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
536 assert(ExpandedFileID && "expansion in uncovered file");
537
538 SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
539 assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
540 "region spans multiple files");
541 Filter.insert(std::make_pair(ParentLoc, LocEnd));
542
543 SpellingRegion SR{SM, ParentLoc, LocEnd};
544 assert(SR.isInSourceOrder() && "region start and end out of order");
545 MappingRegions.push_back(CounterMappingRegion::makeExpansion(
546 *ParentFileID, *ExpandedFileID, SR.LineStart, SR.ColumnStart,
547 SR.LineEnd, SR.ColumnEnd));
548 }
549 return Filter;
550 }
551};
552
553/// Creates unreachable coverage regions for the functions that
554/// are not emitted.
555struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
556 EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
557 const LangOptions &LangOpts)
558 : CoverageMappingBuilder(CVM, SM, LangOpts) {}
559
560 void VisitDecl(const Decl *D) {
561 if (!D->hasBody())
562 return;
563 auto Body = D->getBody();
564 SourceLocation Start = getStart(Body);
565 SourceLocation End = getEnd(Body);
566 if (!SM.isWrittenInSameFile(Start, End)) {
567 // Walk up to find the common ancestor.
568 // Correct the locations accordingly.
569 FileID StartFileID = SM.getFileID(Start);
570 FileID EndFileID = SM.getFileID(End);
571 while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) {
572 Start = getIncludeOrExpansionLoc(Start);
573 assert(Start.isValid() &&
574 "Declaration start location not nested within a known region");
575 StartFileID = SM.getFileID(Start);
576 }
577 while (StartFileID != EndFileID) {
578 End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
579 assert(End.isValid() &&
580 "Declaration end location not nested within a known region");
581 EndFileID = SM.getFileID(End);
582 }
583 }
584 SourceRegions.emplace_back(Counter(), Start, End);
585 }
586
587 /// Write the mapping data to the output stream
588 void write(llvm::raw_ostream &OS) {
589 SmallVector<unsigned, 16> FileIDMapping;
590 gatherFileIDs(FileIDMapping);
591 emitSourceRegions(SourceRegionFilter());
592
593 if (MappingRegions.empty())
594 return;
595
596 CoverageMappingWriter Writer(FileIDMapping, std::nullopt, MappingRegions);
597 Writer.write(OS);
598 }
599};
600
601/// A wrapper object for maintaining stacks to track the resursive AST visitor
602/// walks for the purpose of assigning IDs to leaf-level conditions measured by
603/// MC/DC. The object is created with a reference to the MCDCBitmapMap that was
604/// created during the initial AST walk. The presence of a bitmap associated
605/// with a boolean expression (top-level logical operator nest) indicates that
606/// the boolean expression qualified for MC/DC. The resulting condition IDs
607/// are preserved in a map reference that is also provided during object
608/// creation.
609struct MCDCCoverageBuilder {
610
611 /// The AST walk recursively visits nested logical-AND or logical-OR binary
612 /// operator nodes and then visits their LHS and RHS children nodes. As this
613 /// happens, the algorithm will assign IDs to each operator's LHS and RHS side
614 /// as the walk moves deeper into the nest. At each level of the recursive
615 /// nest, the LHS and RHS may actually correspond to larger subtrees (not
616 /// leaf-conditions). If this is the case, when that node is visited, the ID
617 /// assigned to the subtree is re-assigned to its LHS, and a new ID is given
618 /// to its RHS. At the end of the walk, all leaf-level conditions will have a
619 /// unique ID -- keep in mind that the final set of IDs may not be in
620 /// numerical order from left to right.
621 ///
622 /// Example: "x = (A && B) || (C && D) || (D && F)"
623 ///
624 /// Visit Depth1:
625 /// (A && B) || (C && D) || (D && F)
626 /// ^-------LHS--------^ ^-RHS--^
627 /// ID=1 ID=2
628 ///
629 /// Visit LHS-Depth2:
630 /// (A && B) || (C && D)
631 /// ^-LHS--^ ^-RHS--^
632 /// ID=1 ID=3
633 ///
634 /// Visit LHS-Depth3:
635 /// (A && B)
636 /// LHS RHS
637 /// ID=1 ID=4
638 ///
639 /// Visit RHS-Depth3:
640 /// (C && D)
641 /// LHS RHS
642 /// ID=3 ID=5
643 ///
644 /// Visit RHS-Depth2: (D && F)
645 /// LHS RHS
646 /// ID=2 ID=6
647 ///
648 /// Visit Depth1:
649 /// (A && B) || (C && D) || (D && F)
650 /// ID=1 ID=4 ID=3 ID=5 ID=2 ID=6
651 ///
652 /// A node ID of '0' always means MC/DC isn't being tracked.
653 ///
654 /// As the AST walk proceeds recursively, the algorithm will also use a stack
655 /// to track the IDs of logical-AND and logical-OR operations on the RHS so
656 /// that it can be determined which nodes are executed next, depending on how
657 /// a LHS or RHS of a logical-AND or logical-OR is evaluated. This
658 /// information relies on the assigned IDs and are embedded within the
659 /// coverage region IDs of each branch region associated with a leaf-level
660 /// condition. This information helps the visualization tool reconstruct all
661 /// possible test vectors for the purposes of MC/DC analysis. If a "next" node
662 /// ID is '0', it means it's the end of the test vector. The following rules
663 /// are used:
664 ///
665 /// For logical-AND ("LHS && RHS"):
666 /// - If LHS is TRUE, execution goes to the RHS node.
667 /// - If LHS is FALSE, execution goes to the LHS node of the next logical-OR.
668 /// If that does not exist, execution exits (ID == 0).
669 ///
670 /// - If RHS is TRUE, execution goes to LHS node of the next logical-AND.
671 /// If that does not exist, execution exits (ID == 0).
672 /// - If RHS is FALSE, execution goes to the LHS node of the next logical-OR.
673 /// If that does not exist, execution exits (ID == 0).
674 ///
675 /// For logical-OR ("LHS || RHS"):
676 /// - If LHS is TRUE, execution goes to the LHS node of the next logical-AND.
677 /// If that does not exist, execution exits (ID == 0).
678 /// - If LHS is FALSE, execution goes to the RHS node.
679 ///
680 /// - If RHS is TRUE, execution goes to LHS node of the next logical-AND.
681 /// If that does not exist, execution exits (ID == 0).
682 /// - If RHS is FALSE, execution goes to the LHS node of the next logical-OR.
683 /// If that does not exist, execution exits (ID == 0).
684 ///
685 /// Finally, the condition IDs are also used when instrumenting the code to
686 /// indicate a unique offset into a temporary bitmap that represents the true
687 /// or false evaluation of that particular condition.
688 ///
689 /// NOTE regarding the use of CodeGenFunction::stripCond(). Even though, for
690 /// simplicity, parentheses and unary logical-NOT operators are considered
691 /// part of their underlying condition for both MC/DC and branch coverage, the
692 /// condition IDs themselves are assigned and tracked using the underlying
693 /// condition itself. This is done solely for consistency since parentheses
694 /// and logical-NOTs are ignored when checking whether the condition is
695 /// actually an instrumentable condition. This can also make debugging a bit
696 /// easier.
697
698private:
699 CodeGenModule &CGM;
700
702 MCDC::State &MCDCState;
703 mcdc::ConditionID NextID = 0;
704 bool NotMapped = false;
705
706 /// Represent a sentinel value as a pair of final decisions for the bottom
707 // of DecisionStack.
708 static constexpr mcdc::ConditionIDs DecisionStackSentinel{-1, -1};
709
710 /// Is this a logical-AND operation?
711 bool isLAnd(const BinaryOperator *E) const {
712 return E->getOpcode() == BO_LAnd;
713 }
714
715public:
716 MCDCCoverageBuilder(CodeGenModule &CGM, MCDC::State &MCDCState)
717 : CGM(CGM), DecisionStack(1, DecisionStackSentinel),
718 MCDCState(MCDCState) {}
719
720 /// Return whether the build of the control flow map is at the top-level
721 /// (root) of a logical operator nest in a boolean expression prior to the
722 /// assignment of condition IDs.
723 bool isIdle() const { return (NextID == 0 && !NotMapped); }
724
725 /// Return whether any IDs have been assigned in the build of the control
726 /// flow map, indicating that the map is being generated for this boolean
727 /// expression.
728 bool isBuilding() const { return (NextID > 0); }
729
730 /// Set the given condition's ID.
731 void setCondID(const Expr *Cond, mcdc::ConditionID ID) {
732 MCDCState.BranchByStmt[CodeGenFunction::stripCond(Cond)].ID = ID;
733 }
734
735 /// Return the ID of a given condition.
736 mcdc::ConditionID getCondID(const Expr *Cond) const {
737 auto I = MCDCState.BranchByStmt.find(CodeGenFunction::stripCond(Cond));
738 if (I == MCDCState.BranchByStmt.end())
739 return -1;
740 else
741 return I->second.ID;
742 }
743
744 /// Return the LHS Decision ([0,0] if not set).
745 const mcdc::ConditionIDs &back() const { return DecisionStack.back(); }
746
747 /// Push the binary operator statement to track the nest level and assign IDs
748 /// to the operator's LHS and RHS. The RHS may be a larger subtree that is
749 /// broken up on successive levels.
750 void pushAndAssignIDs(const BinaryOperator *E) {
751 if (!CGM.getCodeGenOpts().MCDCCoverage)
752 return;
753
754 // If binary expression is disqualified, don't do mapping.
755 if (!isBuilding() &&
756 !MCDCState.DecisionByStmt.contains(CodeGenFunction::stripCond(E)))
757 NotMapped = true;
758
759 // Don't go any further if we don't need to map condition IDs.
760 if (NotMapped)
761 return;
762
763 const mcdc::ConditionIDs &ParentDecision = DecisionStack.back();
764
765 // If the operator itself has an assigned ID, this means it represents a
766 // larger subtree. In this case, assign that ID to its LHS node. Its RHS
767 // will receive a new ID below. Otherwise, assign ID+1 to LHS.
768 if (MCDCState.BranchByStmt.contains(CodeGenFunction::stripCond(E)))
769 setCondID(E->getLHS(), getCondID(E));
770 else
771 setCondID(E->getLHS(), NextID++);
772
773 // Assign a ID+1 for the RHS.
774 mcdc::ConditionID RHSid = NextID++;
775 setCondID(E->getRHS(), RHSid);
776
777 // Push the LHS decision IDs onto the DecisionStack.
778 if (isLAnd(E))
779 DecisionStack.push_back({ParentDecision[false], RHSid});
780 else
781 DecisionStack.push_back({RHSid, ParentDecision[true]});
782 }
783
784 /// Pop and return the LHS Decision ([0,0] if not set).
785 mcdc::ConditionIDs pop() {
786 if (!CGM.getCodeGenOpts().MCDCCoverage || NotMapped)
787 return DecisionStackSentinel;
788
789 assert(DecisionStack.size() > 1);
790 return DecisionStack.pop_back_val();
791 }
792
793 /// Return the total number of conditions and reset the state. The number of
794 /// conditions is zero if the expression isn't mapped.
795 unsigned getTotalConditionsAndReset(const BinaryOperator *E) {
796 if (!CGM.getCodeGenOpts().MCDCCoverage)
797 return 0;
798
799 assert(!isIdle());
800 assert(DecisionStack.size() == 1);
801
802 // Reset state if not doing mapping.
803 if (NotMapped) {
804 NotMapped = false;
805 assert(NextID == 0);
806 return 0;
807 }
808
809 // Set number of conditions and reset.
810 unsigned TotalConds = NextID;
811
812 // Reset ID back to beginning.
813 NextID = 0;
814
815 return TotalConds;
816 }
817};
818
819/// A StmtVisitor that creates coverage mapping regions which map
820/// from the source code locations to the PGO counters.
821struct CounterCoverageMappingBuilder
822 : public CoverageMappingBuilder,
823 public ConstStmtVisitor<CounterCoverageMappingBuilder> {
824 /// The map of statements to count values.
825 llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
826
827 MCDC::State &MCDCState;
828
829 /// A stack of currently live regions.
831
832 /// Set if the Expr should be handled as a leaf even if it is kind of binary
833 /// logical ops (&&, ||).
835
836 /// An object to manage MCDC regions.
837 MCDCCoverageBuilder MCDCBuilder;
838
839 CounterExpressionBuilder Builder;
840
841 /// A location in the most recently visited file or macro.
842 ///
843 /// This is used to adjust the active source regions appropriately when
844 /// expressions cross file or macro boundaries.
845 SourceLocation MostRecentLocation;
846
847 /// Whether the visitor at a terminate statement.
848 bool HasTerminateStmt = false;
849
850 /// Gap region counter after terminate statement.
851 Counter GapRegionCounter;
852
853 /// Return a counter for the subtraction of \c RHS from \c LHS
854 Counter subtractCounters(Counter LHS, Counter RHS, bool Simplify = true) {
856 "cannot add counters when single byte coverage mode is enabled");
857 return Builder.subtract(LHS, RHS, Simplify);
858 }
859
860 /// Return a counter for the sum of \c LHS and \c RHS.
861 Counter addCounters(Counter LHS, Counter RHS, bool Simplify = true) {
863 "cannot add counters when single byte coverage mode is enabled");
864 return Builder.add(LHS, RHS, Simplify);
865 }
866
867 Counter addCounters(Counter C1, Counter C2, Counter C3,
868 bool Simplify = true) {
870 "cannot add counters when single byte coverage mode is enabled");
871 return addCounters(addCounters(C1, C2, Simplify), C3, Simplify);
872 }
873
874 /// Return the region counter for the given statement.
875 ///
876 /// This should only be called on statements that have a dedicated counter.
877 Counter getRegionCounter(const Stmt *S) {
878 return Counter::getCounter(CounterMap[S]);
879 }
880
881 /// Push a region onto the stack.
882 ///
883 /// Returns the index on the stack where the region was pushed. This can be
884 /// used with popRegions to exit a "scope", ending the region that was pushed.
885 size_t pushRegion(Counter Count,
886 std::optional<SourceLocation> StartLoc = std::nullopt,
887 std::optional<SourceLocation> EndLoc = std::nullopt,
888 std::optional<Counter> FalseCount = std::nullopt,
889 const mcdc::Parameters &BranchParams = std::monostate()) {
890
891 if (StartLoc && !FalseCount) {
892 MostRecentLocation = *StartLoc;
893 }
894
895 // If either of these locations is invalid, something elsewhere in the
896 // compiler has broken.
897 assert((!StartLoc || StartLoc->isValid()) && "Start location is not valid");
898 assert((!EndLoc || EndLoc->isValid()) && "End location is not valid");
899
900 // However, we can still recover without crashing.
901 // If either location is invalid, set it to std::nullopt to avoid
902 // letting users of RegionStack think that region has a valid start/end
903 // location.
904 if (StartLoc && StartLoc->isInvalid())
905 StartLoc = std::nullopt;
906 if (EndLoc && EndLoc->isInvalid())
907 EndLoc = std::nullopt;
908 RegionStack.emplace_back(Count, FalseCount, BranchParams, StartLoc, EndLoc);
909
910 return RegionStack.size() - 1;
911 }
912
913 size_t pushRegion(const mcdc::DecisionParameters &DecisionParams,
914 std::optional<SourceLocation> StartLoc = std::nullopt,
915 std::optional<SourceLocation> EndLoc = std::nullopt) {
916
917 RegionStack.emplace_back(DecisionParams, StartLoc, EndLoc);
918
919 return RegionStack.size() - 1;
920 }
921
922 size_t locationDepth(SourceLocation Loc) {
923 size_t Depth = 0;
924 while (Loc.isValid()) {
925 Loc = getIncludeOrExpansionLoc(Loc);
926 Depth++;
927 }
928 return Depth;
929 }
930
931 /// Pop regions from the stack into the function's list of regions.
932 ///
933 /// Adds all regions from \c ParentIndex to the top of the stack to the
934 /// function's \c SourceRegions.
935 void popRegions(size_t ParentIndex) {
936 assert(RegionStack.size() >= ParentIndex && "parent not in stack");
937 while (RegionStack.size() > ParentIndex) {
938 SourceMappingRegion &Region = RegionStack.back();
939 if (Region.hasStartLoc() &&
940 (Region.hasEndLoc() || RegionStack[ParentIndex].hasEndLoc())) {
941 SourceLocation StartLoc = Region.getBeginLoc();
942 SourceLocation EndLoc = Region.hasEndLoc()
943 ? Region.getEndLoc()
944 : RegionStack[ParentIndex].getEndLoc();
945 bool isBranch = Region.isBranch();
946 size_t StartDepth = locationDepth(StartLoc);
947 size_t EndDepth = locationDepth(EndLoc);
948 while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
949 bool UnnestStart = StartDepth >= EndDepth;
950 bool UnnestEnd = EndDepth >= StartDepth;
951 if (UnnestEnd) {
952 // The region ends in a nested file or macro expansion. If the
953 // region is not a branch region, create a separate region for each
954 // expansion, and for all regions, update the EndLoc. Branch
955 // regions should not be split in order to keep a straightforward
956 // correspondance between the region and its associated branch
957 // condition, even if the condition spans multiple depths.
958 SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
959 assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
960
961 if (!isBranch && !isRegionAlreadyAdded(NestedLoc, EndLoc))
962 SourceRegions.emplace_back(Region.getCounter(), NestedLoc,
963 EndLoc);
964
965 EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
966 if (EndLoc.isInvalid())
967 llvm::report_fatal_error(
968 "File exit not handled before popRegions");
969 EndDepth--;
970 }
971 if (UnnestStart) {
972 // The region ends in a nested file or macro expansion. If the
973 // region is not a branch region, create a separate region for each
974 // expansion, and for all regions, update the StartLoc. Branch
975 // regions should not be split in order to keep a straightforward
976 // correspondance between the region and its associated branch
977 // condition, even if the condition spans multiple depths.
978 SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc);
979 assert(SM.isWrittenInSameFile(StartLoc, NestedLoc));
980
981 if (!isBranch && !isRegionAlreadyAdded(StartLoc, NestedLoc))
982 SourceRegions.emplace_back(Region.getCounter(), StartLoc,
983 NestedLoc);
984
985 StartLoc = getIncludeOrExpansionLoc(StartLoc);
986 if (StartLoc.isInvalid())
987 llvm::report_fatal_error(
988 "File exit not handled before popRegions");
989 StartDepth--;
990 }
991 }
992 Region.setStartLoc(StartLoc);
993 Region.setEndLoc(EndLoc);
994
995 if (!isBranch) {
996 MostRecentLocation = EndLoc;
997 // If this region happens to span an entire expansion, we need to
998 // make sure we don't overlap the parent region with it.
999 if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
1000 EndLoc == getEndOfFileOrMacro(EndLoc))
1001 MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
1002 }
1003
1004 assert(SM.isWrittenInSameFile(Region.getBeginLoc(), EndLoc));
1005 assert(SpellingRegion(SM, Region).isInSourceOrder());
1006 SourceRegions.push_back(Region);
1007 }
1008 RegionStack.pop_back();
1009 }
1010 }
1011
1012 /// Return the currently active region.
1013 SourceMappingRegion &getRegion() {
1014 assert(!RegionStack.empty() && "statement has no region");
1015 return RegionStack.back();
1016 }
1017
1018 /// Propagate counts through the children of \p S if \p VisitChildren is true.
1019 /// Otherwise, only emit a count for \p S itself.
1020 Counter propagateCounts(Counter TopCount, const Stmt *S,
1021 bool VisitChildren = true) {
1022 SourceLocation StartLoc = getStart(S);
1023 SourceLocation EndLoc = getEnd(S);
1024 size_t Index = pushRegion(TopCount, StartLoc, EndLoc);
1025 if (VisitChildren)
1026 Visit(S);
1027 Counter ExitCount = getRegion().getCounter();
1028 popRegions(Index);
1029
1030 // The statement may be spanned by an expansion. Make sure we handle a file
1031 // exit out of this expansion before moving to the next statement.
1032 if (SM.isBeforeInTranslationUnit(StartLoc, S->getBeginLoc()))
1033 MostRecentLocation = EndLoc;
1034
1035 return ExitCount;
1036 }
1037
1038 /// Determine whether the given condition can be constant folded.
1039 bool ConditionFoldsToBool(const Expr *Cond) {
1040 Expr::EvalResult Result;
1041 return (Cond->EvaluateAsInt(Result, CVM.getCodeGenModule().getContext()));
1042 }
1043
1044 /// Create a Branch Region around an instrumentable condition for coverage
1045 /// and add it to the function's SourceRegions. A branch region tracks a
1046 /// "True" counter and a "False" counter for boolean expressions that
1047 /// result in the generation of a branch.
1048 void createBranchRegion(const Expr *C, Counter TrueCnt, Counter FalseCnt,
1049 const mcdc::ConditionIDs &Conds = {}) {
1050 // Check for NULL conditions.
1051 if (!C)
1052 return;
1053
1054 // Ensure we are an instrumentable condition (i.e. no "&&" or "||"). Push
1055 // region onto RegionStack but immediately pop it (which adds it to the
1056 // function's SourceRegions) because it doesn't apply to any other source
1057 // code other than the Condition.
1058 // With !SystemHeadersCoverage, binary logical ops in system headers may be
1059 // treated as instrumentable conditions.
1060 if (CodeGenFunction::isInstrumentedCondition(C) ||
1061 LeafExprSet.count(CodeGenFunction::stripCond(C))) {
1062 mcdc::Parameters BranchParams;
1063 mcdc::ConditionID ID = MCDCBuilder.getCondID(C);
1064 if (ID >= 0)
1065 BranchParams = mcdc::BranchParameters{ID, Conds};
1066
1067 // If a condition can fold to true or false, the corresponding branch
1068 // will be removed. Create a region with both counters hard-coded to
1069 // zero. This allows us to visualize them in a special way.
1070 // Alternatively, we can prevent any optimization done via
1071 // constant-folding by ensuring that ConstantFoldsToSimpleInteger() in
1072 // CodeGenFunction.c always returns false, but that is very heavy-handed.
1073 if (ConditionFoldsToBool(C))
1074 popRegions(pushRegion(Counter::getZero(), getStart(C), getEnd(C),
1075 Counter::getZero(), BranchParams));
1076 else
1077 // Otherwise, create a region with the True counter and False counter.
1078 popRegions(pushRegion(TrueCnt, getStart(C), getEnd(C), FalseCnt,
1079 BranchParams));
1080 }
1081 }
1082
1083 /// Create a Decision Region with a BitmapIdx and number of Conditions. This
1084 /// type of region "contains" branch regions, one for each of the conditions.
1085 /// The visualization tool will group everything together.
1086 void createDecisionRegion(const Expr *C,
1087 const mcdc::DecisionParameters &DecisionParams) {
1088 popRegions(pushRegion(DecisionParams, getStart(C), getEnd(C)));
1089 }
1090
1091 /// Create a Branch Region around a SwitchCase for code coverage
1092 /// and add it to the function's SourceRegions.
1093 void createSwitchCaseRegion(const SwitchCase *SC, Counter TrueCnt,
1094 Counter FalseCnt) {
1095 // Push region onto RegionStack but immediately pop it (which adds it to
1096 // the function's SourceRegions) because it doesn't apply to any other
1097 // source other than the SwitchCase.
1098 popRegions(pushRegion(TrueCnt, getStart(SC), SC->getColonLoc(), FalseCnt));
1099 }
1100
1101 /// Check whether a region with bounds \c StartLoc and \c EndLoc
1102 /// is already added to \c SourceRegions.
1103 bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc,
1104 bool isBranch = false) {
1105 return llvm::any_of(
1106 llvm::reverse(SourceRegions), [&](const SourceMappingRegion &Region) {
1107 return Region.getBeginLoc() == StartLoc &&
1108 Region.getEndLoc() == EndLoc && Region.isBranch() == isBranch;
1109 });
1110 }
1111
1112 /// Adjust the most recently visited location to \c EndLoc.
1113 ///
1114 /// This should be used after visiting any statements in non-source order.
1115 void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
1116 MostRecentLocation = EndLoc;
1117 // The code region for a whole macro is created in handleFileExit() when
1118 // it detects exiting of the virtual file of that macro. If we visited
1119 // statements in non-source order, we might already have such a region
1120 // added, for example, if a body of a loop is divided among multiple
1121 // macros. Avoid adding duplicate regions in such case.
1122 if (getRegion().hasEndLoc() &&
1123 MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) &&
1124 isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
1125 MostRecentLocation, getRegion().isBranch()))
1126 MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
1127 }
1128
1129 /// Adjust regions and state when \c NewLoc exits a file.
1130 ///
1131 /// If moving from our most recently tracked location to \c NewLoc exits any
1132 /// files, this adjusts our current region stack and creates the file regions
1133 /// for the exited file.
1134 void handleFileExit(SourceLocation NewLoc) {
1135 if (NewLoc.isInvalid() ||
1136 SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
1137 return;
1138
1139 // If NewLoc is not in a file that contains MostRecentLocation, walk up to
1140 // find the common ancestor.
1141 SourceLocation LCA = NewLoc;
1142 FileID ParentFile = SM.getFileID(LCA);
1143 while (!isNestedIn(MostRecentLocation, ParentFile)) {
1144 LCA = getIncludeOrExpansionLoc(LCA);
1145 if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
1146 // Since there isn't a common ancestor, no file was exited. We just need
1147 // to adjust our location to the new file.
1148 MostRecentLocation = NewLoc;
1149 return;
1150 }
1151 ParentFile = SM.getFileID(LCA);
1152 }
1153
1154 llvm::SmallSet<SourceLocation, 8> StartLocs;
1155 std::optional<Counter> ParentCounter;
1156 for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
1157 if (!I.hasStartLoc())
1158 continue;
1159 SourceLocation Loc = I.getBeginLoc();
1160 if (!isNestedIn(Loc, ParentFile)) {
1161 ParentCounter = I.getCounter();
1162 break;
1163 }
1164
1165 while (!SM.isInFileID(Loc, ParentFile)) {
1166 // The most nested region for each start location is the one with the
1167 // correct count. We avoid creating redundant regions by stopping once
1168 // we've seen this region.
1169 if (StartLocs.insert(Loc).second) {
1170 if (I.isBranch())
1171 SourceRegions.emplace_back(I.getCounter(), I.getFalseCounter(),
1172 I.getMCDCParams(), Loc,
1173 getEndOfFileOrMacro(Loc), I.isBranch());
1174 else
1175 SourceRegions.emplace_back(I.getCounter(), Loc,
1176 getEndOfFileOrMacro(Loc));
1177 }
1178 Loc = getIncludeOrExpansionLoc(Loc);
1179 }
1180 I.setStartLoc(getPreciseTokenLocEnd(Loc));
1181 }
1182
1183 if (ParentCounter) {
1184 // If the file is contained completely by another region and doesn't
1185 // immediately start its own region, the whole file gets a region
1186 // corresponding to the parent.
1187 SourceLocation Loc = MostRecentLocation;
1188 while (isNestedIn(Loc, ParentFile)) {
1189 SourceLocation FileStart = getStartOfFileOrMacro(Loc);
1190 if (StartLocs.insert(FileStart).second) {
1191 SourceRegions.emplace_back(*ParentCounter, FileStart,
1192 getEndOfFileOrMacro(Loc));
1193 assert(SpellingRegion(SM, SourceRegions.back()).isInSourceOrder());
1194 }
1195 Loc = getIncludeOrExpansionLoc(Loc);
1196 }
1197 }
1198
1199 MostRecentLocation = NewLoc;
1200 }
1201
1202 /// Ensure that \c S is included in the current region.
1203 void extendRegion(const Stmt *S) {
1204 SourceMappingRegion &Region = getRegion();
1205 SourceLocation StartLoc = getStart(S);
1206
1207 handleFileExit(StartLoc);
1208 if (!Region.hasStartLoc())
1209 Region.setStartLoc(StartLoc);
1210 }
1211
1212 /// Mark \c S as a terminator, starting a zero region.
1213 void terminateRegion(const Stmt *S) {
1214 extendRegion(S);
1215 SourceMappingRegion &Region = getRegion();
1216 SourceLocation EndLoc = getEnd(S);
1217 if (!Region.hasEndLoc())
1218 Region.setEndLoc(EndLoc);
1219 pushRegion(Counter::getZero());
1220 HasTerminateStmt = true;
1221 }
1222
1223 /// Find a valid gap range between \p AfterLoc and \p BeforeLoc.
1224 std::optional<SourceRange> findGapAreaBetween(SourceLocation AfterLoc,
1225 SourceLocation BeforeLoc) {
1226 // Some statements (like AttributedStmt and ImplicitValueInitExpr) don't
1227 // have valid source locations. Do not emit a gap region if this is the case
1228 // in either AfterLoc end or BeforeLoc end.
1229 if (AfterLoc.isInvalid() || BeforeLoc.isInvalid())
1230 return std::nullopt;
1231
1232 // If AfterLoc is in function-like macro, use the right parenthesis
1233 // location.
1234 if (AfterLoc.isMacroID()) {
1235 FileID FID = SM.getFileID(AfterLoc);
1236 const SrcMgr::ExpansionInfo *EI = &SM.getSLocEntry(FID).getExpansion();
1237 if (EI->isFunctionMacroExpansion())
1238 AfterLoc = EI->getExpansionLocEnd();
1239 }
1240
1241 size_t StartDepth = locationDepth(AfterLoc);
1242 size_t EndDepth = locationDepth(BeforeLoc);
1243 while (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc)) {
1244 bool UnnestStart = StartDepth >= EndDepth;
1245 bool UnnestEnd = EndDepth >= StartDepth;
1246 if (UnnestEnd) {
1247 assert(SM.isWrittenInSameFile(getStartOfFileOrMacro(BeforeLoc),
1248 BeforeLoc));
1249
1250 BeforeLoc = getIncludeOrExpansionLoc(BeforeLoc);
1251 assert(BeforeLoc.isValid());
1252 EndDepth--;
1253 }
1254 if (UnnestStart) {
1255 assert(SM.isWrittenInSameFile(AfterLoc,
1256 getEndOfFileOrMacro(AfterLoc)));
1257
1258 AfterLoc = getIncludeOrExpansionLoc(AfterLoc);
1259 assert(AfterLoc.isValid());
1260 AfterLoc = getPreciseTokenLocEnd(AfterLoc);
1261 assert(AfterLoc.isValid());
1262 StartDepth--;
1263 }
1264 }
1265 AfterLoc = getPreciseTokenLocEnd(AfterLoc);
1266 // If the start and end locations of the gap are both within the same macro
1267 // file, the range may not be in source order.
1268 if (AfterLoc.isMacroID() || BeforeLoc.isMacroID())
1269 return std::nullopt;
1270 if (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc) ||
1271 !SpellingRegion(SM, AfterLoc, BeforeLoc).isInSourceOrder())
1272 return std::nullopt;
1273 return {{AfterLoc, BeforeLoc}};
1274 }
1275
1276 /// Emit a gap region between \p StartLoc and \p EndLoc with the given count.
1277 void fillGapAreaWithCount(SourceLocation StartLoc, SourceLocation EndLoc,
1278 Counter Count) {
1279 if (StartLoc == EndLoc)
1280 return;
1281 assert(SpellingRegion(SM, StartLoc, EndLoc).isInSourceOrder());
1282 handleFileExit(StartLoc);
1283 size_t Index = pushRegion(Count, StartLoc, EndLoc);
1284 getRegion().setGap(true);
1285 handleFileExit(EndLoc);
1286 popRegions(Index);
1287 }
1288
1289 /// Find a valid range starting with \p StartingLoc and ending before \p
1290 /// BeforeLoc.
1291 std::optional<SourceRange> findAreaStartingFromTo(SourceLocation StartingLoc,
1292 SourceLocation BeforeLoc) {
1293 // If StartingLoc is in function-like macro, use its start location.
1294 if (StartingLoc.isMacroID()) {
1295 FileID FID = SM.getFileID(StartingLoc);
1296 const SrcMgr::ExpansionInfo *EI = &SM.getSLocEntry(FID).getExpansion();
1297 if (EI->isFunctionMacroExpansion())
1298 StartingLoc = EI->getExpansionLocStart();
1299 }
1300
1301 size_t StartDepth = locationDepth(StartingLoc);
1302 size_t EndDepth = locationDepth(BeforeLoc);
1303 while (!SM.isWrittenInSameFile(StartingLoc, BeforeLoc)) {
1304 bool UnnestStart = StartDepth >= EndDepth;
1305 bool UnnestEnd = EndDepth >= StartDepth;
1306 if (UnnestEnd) {
1307 assert(SM.isWrittenInSameFile(getStartOfFileOrMacro(BeforeLoc),
1308 BeforeLoc));
1309
1310 BeforeLoc = getIncludeOrExpansionLoc(BeforeLoc);
1311 assert(BeforeLoc.isValid());
1312 EndDepth--;
1313 }
1314 if (UnnestStart) {
1315 assert(SM.isWrittenInSameFile(StartingLoc,
1316 getStartOfFileOrMacro(StartingLoc)));
1317
1318 StartingLoc = getIncludeOrExpansionLoc(StartingLoc);
1319 assert(StartingLoc.isValid());
1320 StartDepth--;
1321 }
1322 }
1323 // If the start and end locations of the gap are both within the same macro
1324 // file, the range may not be in source order.
1325 if (StartingLoc.isMacroID() || BeforeLoc.isMacroID())
1326 return std::nullopt;
1327 if (!SM.isWrittenInSameFile(StartingLoc, BeforeLoc) ||
1328 !SpellingRegion(SM, StartingLoc, BeforeLoc).isInSourceOrder())
1329 return std::nullopt;
1330 return {{StartingLoc, BeforeLoc}};
1331 }
1332
1333 void markSkipped(SourceLocation StartLoc, SourceLocation BeforeLoc) {
1334 const auto Skipped = findAreaStartingFromTo(StartLoc, BeforeLoc);
1335
1336 if (!Skipped)
1337 return;
1338
1339 const auto NewStartLoc = Skipped->getBegin();
1340 const auto EndLoc = Skipped->getEnd();
1341
1342 if (NewStartLoc == EndLoc)
1343 return;
1344 assert(SpellingRegion(SM, NewStartLoc, EndLoc).isInSourceOrder());
1345 handleFileExit(NewStartLoc);
1346 size_t Index = pushRegion(Counter{}, NewStartLoc, EndLoc);
1347 getRegion().setSkipped(true);
1348 handleFileExit(EndLoc);
1349 popRegions(Index);
1350 }
1351
1352 /// Keep counts of breaks and continues inside loops.
1353 struct BreakContinue {
1354 Counter BreakCount;
1355 Counter ContinueCount;
1356 };
1357 SmallVector<BreakContinue, 8> BreakContinueStack;
1358
1359 CounterCoverageMappingBuilder(
1361 llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
1362 MCDC::State &MCDCState, SourceManager &SM, const LangOptions &LangOpts)
1363 : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
1364 MCDCState(MCDCState), MCDCBuilder(CVM.getCodeGenModule(), MCDCState) {}
1365
1366 /// Write the mapping data to the output stream
1367 void write(llvm::raw_ostream &OS) {
1368 llvm::SmallVector<unsigned, 8> VirtualFileMapping;
1369 gatherFileIDs(VirtualFileMapping);
1370 SourceRegionFilter Filter = emitExpansionRegions();
1371 emitSourceRegions(Filter);
1372 gatherSkippedRegions();
1373
1374 if (MappingRegions.empty())
1375 return;
1376
1377 CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
1378 MappingRegions);
1379 Writer.write(OS);
1380 }
1381
1382 void VisitStmt(const Stmt *S) {
1383 if (S->getBeginLoc().isValid())
1384 extendRegion(S);
1385 const Stmt *LastStmt = nullptr;
1386 bool SaveTerminateStmt = HasTerminateStmt;
1387 HasTerminateStmt = false;
1388 GapRegionCounter = Counter::getZero();
1389 for (const Stmt *Child : S->children())
1390 if (Child) {
1391 // If last statement contains terminate statements, add a gap area
1392 // between the two statements.
1393 if (LastStmt && HasTerminateStmt) {
1394 auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child));
1395 if (Gap)
1396 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(),
1397 GapRegionCounter);
1398 SaveTerminateStmt = true;
1399 HasTerminateStmt = false;
1400 }
1401 this->Visit(Child);
1402 LastStmt = Child;
1403 }
1404 if (SaveTerminateStmt)
1405 HasTerminateStmt = true;
1406 handleFileExit(getEnd(S));
1407 }
1408
1409 void VisitDecl(const Decl *D) {
1410 Stmt *Body = D->getBody();
1411
1412 // Do not propagate region counts into system headers unless collecting
1413 // coverage from system headers is explicitly enabled.
1414 if (!SystemHeadersCoverage && Body &&
1415 SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
1416 return;
1417
1418 // Do not visit the artificial children nodes of defaulted methods. The
1419 // lexer may not be able to report back precise token end locations for
1420 // these children nodes (llvm.org/PR39822), and moreover users will not be
1421 // able to see coverage for them.
1422 Counter BodyCounter = getRegionCounter(Body);
1423 bool Defaulted = false;
1424 if (auto *Method = dyn_cast<CXXMethodDecl>(D))
1425 Defaulted = Method->isDefaulted();
1426 if (auto *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1427 for (auto *Initializer : Ctor->inits()) {
1428 if (Initializer->isWritten()) {
1429 auto *Init = Initializer->getInit();
1430 if (getStart(Init).isValid() && getEnd(Init).isValid())
1431 propagateCounts(BodyCounter, Init);
1432 }
1433 }
1434 }
1435
1436 propagateCounts(BodyCounter, Body,
1437 /*VisitChildren=*/!Defaulted);
1438 assert(RegionStack.empty() && "Regions entered but never exited");
1439 }
1440
1441 void VisitReturnStmt(const ReturnStmt *S) {
1442 extendRegion(S);
1443 if (S->getRetValue())
1444 Visit(S->getRetValue());
1445 terminateRegion(S);
1446 }
1447
1448 void VisitCoroutineBodyStmt(const CoroutineBodyStmt *S) {
1449 extendRegion(S);
1450 Visit(S->getBody());
1451 }
1452
1453 void VisitCoreturnStmt(const CoreturnStmt *S) {
1454 extendRegion(S);
1455 if (S->getOperand())
1456 Visit(S->getOperand());
1457 terminateRegion(S);
1458 }
1459
1460 void VisitCoroutineSuspendExpr(const CoroutineSuspendExpr *E) {
1461 Visit(E->getOperand());
1462 }
1463
1464 void VisitCXXThrowExpr(const CXXThrowExpr *E) {
1465 extendRegion(E);
1466 if (E->getSubExpr())
1467 Visit(E->getSubExpr());
1468 terminateRegion(E);
1469 }
1470
1471 void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
1472
1473 void VisitLabelStmt(const LabelStmt *S) {
1474 Counter LabelCount = getRegionCounter(S);
1475 SourceLocation Start = getStart(S);
1476 // We can't extendRegion here or we risk overlapping with our new region.
1477 handleFileExit(Start);
1478 pushRegion(LabelCount, Start);
1479 Visit(S->getSubStmt());
1480 }
1481
1482 void VisitBreakStmt(const BreakStmt *S) {
1483 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
1485 BreakContinueStack.back().BreakCount = addCounters(
1486 BreakContinueStack.back().BreakCount, getRegion().getCounter());
1487 // FIXME: a break in a switch should terminate regions for all preceding
1488 // case statements, not just the most recent one.
1489 terminateRegion(S);
1490 }
1491
1492 void VisitContinueStmt(const ContinueStmt *S) {
1493 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
1495 BreakContinueStack.back().ContinueCount = addCounters(
1496 BreakContinueStack.back().ContinueCount, getRegion().getCounter());
1497 terminateRegion(S);
1498 }
1499
1500 void VisitCallExpr(const CallExpr *E) {
1501 VisitStmt(E);
1502
1503 // Terminate the region when we hit a noreturn function.
1504 // (This is helpful dealing with switch statements.)
1505 QualType CalleeType = E->getCallee()->getType();
1506 if (getFunctionExtInfo(*CalleeType).getNoReturn())
1507 terminateRegion(E);
1508 }
1509
1510 void VisitWhileStmt(const WhileStmt *S) {
1511 extendRegion(S);
1512
1513 Counter ParentCount = getRegion().getCounter();
1514 Counter BodyCount = llvm::EnableSingleByteCoverage
1515 ? getRegionCounter(S->getBody())
1516 : getRegionCounter(S);
1517
1518 // Handle the body first so that we can get the backedge count.
1519 BreakContinueStack.push_back(BreakContinue());
1520 extendRegion(S->getBody());
1521 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1522 BreakContinue BC = BreakContinueStack.pop_back_val();
1523
1524 bool BodyHasTerminateStmt = HasTerminateStmt;
1525 HasTerminateStmt = false;
1526
1527 // Go back to handle the condition.
1528 Counter CondCount =
1530 ? getRegionCounter(S->getCond())
1531 : addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1532 propagateCounts(CondCount, S->getCond());
1533 adjustForOutOfOrderTraversal(getEnd(S));
1534
1535 // The body count applies to the area immediately after the increment.
1536 auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1537 if (Gap)
1538 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1539
1540 Counter OutCount =
1542 ? getRegionCounter(S)
1543 : addCounters(BC.BreakCount,
1544 subtractCounters(CondCount, BodyCount));
1545
1546 if (OutCount != ParentCount) {
1547 pushRegion(OutCount);
1548 GapRegionCounter = OutCount;
1549 if (BodyHasTerminateStmt)
1550 HasTerminateStmt = true;
1551 }
1552
1553 // Create Branch Region around condition.
1555 createBranchRegion(S->getCond(), BodyCount,
1556 subtractCounters(CondCount, BodyCount));
1557 }
1558
1559 void VisitDoStmt(const DoStmt *S) {
1560 extendRegion(S);
1561
1562 Counter ParentCount = getRegion().getCounter();
1563 Counter BodyCount = llvm::EnableSingleByteCoverage
1564 ? getRegionCounter(S->getBody())
1565 : getRegionCounter(S);
1566
1567 BreakContinueStack.push_back(BreakContinue());
1568 extendRegion(S->getBody());
1569
1570 Counter BackedgeCount;
1572 propagateCounts(BodyCount, S->getBody());
1573 else
1574 BackedgeCount =
1575 propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
1576
1577 BreakContinue BC = BreakContinueStack.pop_back_val();
1578
1579 bool BodyHasTerminateStmt = HasTerminateStmt;
1580 HasTerminateStmt = false;
1581
1582 Counter CondCount = llvm::EnableSingleByteCoverage
1583 ? getRegionCounter(S->getCond())
1584 : addCounters(BackedgeCount, BC.ContinueCount);
1585 propagateCounts(CondCount, S->getCond());
1586
1587 Counter OutCount =
1589 ? getRegionCounter(S)
1590 : addCounters(BC.BreakCount,
1591 subtractCounters(CondCount, BodyCount));
1592 if (OutCount != ParentCount) {
1593 pushRegion(OutCount);
1594 GapRegionCounter = OutCount;
1595 }
1596
1597 // Create Branch Region around condition.
1599 createBranchRegion(S->getCond(), BodyCount,
1600 subtractCounters(CondCount, BodyCount));
1601
1602 if (BodyHasTerminateStmt)
1603 HasTerminateStmt = true;
1604 }
1605
1606 void VisitForStmt(const ForStmt *S) {
1607 extendRegion(S);
1608 if (S->getInit())
1609 Visit(S->getInit());
1610
1611 Counter ParentCount = getRegion().getCounter();
1612 Counter BodyCount = llvm::EnableSingleByteCoverage
1613 ? getRegionCounter(S->getBody())
1614 : getRegionCounter(S);
1615
1616 // The loop increment may contain a break or continue.
1617 if (S->getInc())
1618 BreakContinueStack.emplace_back();
1619
1620 // Handle the body first so that we can get the backedge count.
1621 BreakContinueStack.emplace_back();
1622 extendRegion(S->getBody());
1623 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1624 BreakContinue BodyBC = BreakContinueStack.pop_back_val();
1625
1626 bool BodyHasTerminateStmt = HasTerminateStmt;
1627 HasTerminateStmt = false;
1628
1629 // The increment is essentially part of the body but it needs to include
1630 // the count for all the continue statements.
1631 BreakContinue IncrementBC;
1632 if (const Stmt *Inc = S->getInc()) {
1633 Counter IncCount;
1635 IncCount = getRegionCounter(S->getInc());
1636 else
1637 IncCount = addCounters(BackedgeCount, BodyBC.ContinueCount);
1638 propagateCounts(IncCount, Inc);
1639 IncrementBC = BreakContinueStack.pop_back_val();
1640 }
1641
1642 // Go back to handle the condition.
1643 Counter CondCount =
1645 ? getRegionCounter(S->getCond())
1646 : addCounters(
1647 addCounters(ParentCount, BackedgeCount, BodyBC.ContinueCount),
1648 IncrementBC.ContinueCount);
1649
1650 if (const Expr *Cond = S->getCond()) {
1651 propagateCounts(CondCount, Cond);
1652 adjustForOutOfOrderTraversal(getEnd(S));
1653 }
1654
1655 // The body count applies to the area immediately after the increment.
1656 auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1657 if (Gap)
1658 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1659
1660 Counter OutCount =
1662 ? getRegionCounter(S)
1663 : addCounters(BodyBC.BreakCount, IncrementBC.BreakCount,
1664 subtractCounters(CondCount, BodyCount));
1665 if (OutCount != ParentCount) {
1666 pushRegion(OutCount);
1667 GapRegionCounter = OutCount;
1668 if (BodyHasTerminateStmt)
1669 HasTerminateStmt = true;
1670 }
1671
1672 // Create Branch Region around condition.
1674 createBranchRegion(S->getCond(), BodyCount,
1675 subtractCounters(CondCount, BodyCount));
1676 }
1677
1678 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
1679 extendRegion(S);
1680 if (S->getInit())
1681 Visit(S->getInit());
1682 Visit(S->getLoopVarStmt());
1683 Visit(S->getRangeStmt());
1684
1685 Counter ParentCount = getRegion().getCounter();
1686 Counter BodyCount = llvm::EnableSingleByteCoverage
1687 ? getRegionCounter(S->getBody())
1688 : getRegionCounter(S);
1689
1690 BreakContinueStack.push_back(BreakContinue());
1691 extendRegion(S->getBody());
1692 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1693 BreakContinue BC = BreakContinueStack.pop_back_val();
1694
1695 bool BodyHasTerminateStmt = HasTerminateStmt;
1696 HasTerminateStmt = false;
1697
1698 // The body count applies to the area immediately after the range.
1699 auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1700 if (Gap)
1701 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1702
1703 Counter OutCount;
1704 Counter LoopCount;
1706 OutCount = getRegionCounter(S);
1707 else {
1708 LoopCount = addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1709 OutCount =
1710 addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
1711 }
1712 if (OutCount != ParentCount) {
1713 pushRegion(OutCount);
1714 GapRegionCounter = OutCount;
1715 if (BodyHasTerminateStmt)
1716 HasTerminateStmt = true;
1717 }
1718
1719 // Create Branch Region around condition.
1721 createBranchRegion(S->getCond(), BodyCount,
1722 subtractCounters(LoopCount, BodyCount));
1723 }
1724
1725 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
1726 extendRegion(S);
1727 Visit(S->getElement());
1728
1729 Counter ParentCount = getRegion().getCounter();
1730 Counter BodyCount = getRegionCounter(S);
1731
1732 BreakContinueStack.push_back(BreakContinue());
1733 extendRegion(S->getBody());
1734 Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
1735 BreakContinue BC = BreakContinueStack.pop_back_val();
1736
1737 // The body count applies to the area immediately after the collection.
1738 auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody()));
1739 if (Gap)
1740 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
1741
1742 Counter LoopCount =
1743 addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
1744 Counter OutCount =
1745 addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
1746 if (OutCount != ParentCount) {
1747 pushRegion(OutCount);
1748 GapRegionCounter = OutCount;
1749 }
1750 }
1751
1752 void VisitSwitchStmt(const SwitchStmt *S) {
1753 extendRegion(S);
1754 if (S->getInit())
1755 Visit(S->getInit());
1756 Visit(S->getCond());
1757
1758 BreakContinueStack.push_back(BreakContinue());
1759
1760 const Stmt *Body = S->getBody();
1761 extendRegion(Body);
1762 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
1763 if (!CS->body_empty()) {
1764 // Make a region for the body of the switch. If the body starts with
1765 // a case, that case will reuse this region; otherwise, this covers
1766 // the unreachable code at the beginning of the switch body.
1767 size_t Index = pushRegion(Counter::getZero(), getStart(CS));
1768 getRegion().setGap(true);
1769 Visit(Body);
1770
1771 // Set the end for the body of the switch, if it isn't already set.
1772 for (size_t i = RegionStack.size(); i != Index; --i) {
1773 if (!RegionStack[i - 1].hasEndLoc())
1774 RegionStack[i - 1].setEndLoc(getEnd(CS->body_back()));
1775 }
1776
1777 popRegions(Index);
1778 }
1779 } else
1780 propagateCounts(Counter::getZero(), Body);
1781 BreakContinue BC = BreakContinueStack.pop_back_val();
1782
1783 if (!BreakContinueStack.empty() && !llvm::EnableSingleByteCoverage)
1784 BreakContinueStack.back().ContinueCount = addCounters(
1785 BreakContinueStack.back().ContinueCount, BC.ContinueCount);
1786
1787 Counter ParentCount = getRegion().getCounter();
1788 Counter ExitCount = getRegionCounter(S);
1789 SourceLocation ExitLoc = getEnd(S);
1790 pushRegion(ExitCount);
1791 GapRegionCounter = ExitCount;
1792
1793 // Ensure that handleFileExit recognizes when the end location is located
1794 // in a different file.
1795 MostRecentLocation = getStart(S);
1796 handleFileExit(ExitLoc);
1797
1798 // When single byte coverage mode is enabled, do not create branch region by
1799 // early returning.
1801 return;
1802
1803 // Create a Branch Region around each Case. Subtract the case's
1804 // counter from the Parent counter to track the "False" branch count.
1805 Counter CaseCountSum;
1806 bool HasDefaultCase = false;
1807 const SwitchCase *Case = S->getSwitchCaseList();
1808 for (; Case; Case = Case->getNextSwitchCase()) {
1809 HasDefaultCase = HasDefaultCase || isa<DefaultStmt>(Case);
1810 CaseCountSum =
1811 addCounters(CaseCountSum, getRegionCounter(Case), /*Simplify=*/false);
1812 createSwitchCaseRegion(
1813 Case, getRegionCounter(Case),
1814 subtractCounters(ParentCount, getRegionCounter(Case)));
1815 }
1816 // Simplify is skipped while building the counters above: it can get really
1817 // slow on top of switches with thousands of cases. Instead, trigger
1818 // simplification by adding zero to the last counter.
1819 CaseCountSum = addCounters(CaseCountSum, Counter::getZero());
1820
1821 // If no explicit default case exists, create a branch region to represent
1822 // the hidden branch, which will be added later by the CodeGen. This region
1823 // will be associated with the switch statement's condition.
1824 if (!HasDefaultCase) {
1825 Counter DefaultTrue = subtractCounters(ParentCount, CaseCountSum);
1826 Counter DefaultFalse = subtractCounters(ParentCount, DefaultTrue);
1827 createBranchRegion(S->getCond(), DefaultTrue, DefaultFalse);
1828 }
1829 }
1830
1831 void VisitSwitchCase(const SwitchCase *S) {
1832 extendRegion(S);
1833
1834 SourceMappingRegion &Parent = getRegion();
1835 Counter Count = llvm::EnableSingleByteCoverage
1836 ? getRegionCounter(S)
1837 : addCounters(Parent.getCounter(), getRegionCounter(S));
1838
1839 // Reuse the existing region if it starts at our label. This is typical of
1840 // the first case in a switch.
1841 if (Parent.hasStartLoc() && Parent.getBeginLoc() == getStart(S))
1842 Parent.setCounter(Count);
1843 else
1844 pushRegion(Count, getStart(S));
1845
1846 GapRegionCounter = Count;
1847
1848 if (const auto *CS = dyn_cast<CaseStmt>(S)) {
1849 Visit(CS->getLHS());
1850 if (const Expr *RHS = CS->getRHS())
1851 Visit(RHS);
1852 }
1853 Visit(S->getSubStmt());
1854 }
1855
1856 void coverIfConsteval(const IfStmt *S) {
1857 assert(S->isConsteval());
1858
1859 const auto *Then = S->getThen();
1860 const auto *Else = S->getElse();
1861
1862 // It's better for llvm-cov to create a new region with same counter
1863 // so line-coverage can be properly calculated for lines containing
1864 // a skipped region (without it the line is marked uncovered)
1865 const Counter ParentCount = getRegion().getCounter();
1866
1867 extendRegion(S);
1868
1869 if (S->isNegatedConsteval()) {
1870 // ignore 'if consteval'
1871 markSkipped(S->getIfLoc(), getStart(Then));
1872 propagateCounts(ParentCount, Then);
1873
1874 if (Else) {
1875 // ignore 'else <else>'
1876 markSkipped(getEnd(Then), getEnd(Else));
1877 }
1878 } else {
1879 assert(S->isNonNegatedConsteval());
1880 // ignore 'if consteval <then> [else]'
1881 markSkipped(S->getIfLoc(), Else ? getStart(Else) : getEnd(Then));
1882
1883 if (Else)
1884 propagateCounts(ParentCount, Else);
1885 }
1886 }
1887
1888 void coverIfConstexpr(const IfStmt *S) {
1889 assert(S->isConstexpr());
1890
1891 // evaluate constant condition...
1892 const bool isTrue =
1893 S->getCond()
1894 ->EvaluateKnownConstInt(CVM.getCodeGenModule().getContext())
1895 .getBoolValue();
1896
1897 extendRegion(S);
1898
1899 // I'm using 'propagateCounts' later as new region is better and allows me
1900 // to properly calculate line coverage in llvm-cov utility
1901 const Counter ParentCount = getRegion().getCounter();
1902
1903 // ignore 'if constexpr ('
1904 SourceLocation startOfSkipped = S->getIfLoc();
1905
1906 if (const auto *Init = S->getInit()) {
1907 const auto start = getStart(Init);
1908 const auto end = getEnd(Init);
1909
1910 // this check is to make sure typedef here which doesn't have valid source
1911 // location won't crash it
1912 if (start.isValid() && end.isValid()) {
1913 markSkipped(startOfSkipped, start);
1914 propagateCounts(ParentCount, Init);
1915 startOfSkipped = getEnd(Init);
1916 }
1917 }
1918
1919 const auto *Then = S->getThen();
1920 const auto *Else = S->getElse();
1921
1922 if (isTrue) {
1923 // ignore '<condition>)'
1924 markSkipped(startOfSkipped, getStart(Then));
1925 propagateCounts(ParentCount, Then);
1926
1927 if (Else)
1928 // ignore 'else <else>'
1929 markSkipped(getEnd(Then), getEnd(Else));
1930 } else {
1931 // ignore '<condition>) <then> [else]'
1932 markSkipped(startOfSkipped, Else ? getStart(Else) : getEnd(Then));
1933
1934 if (Else)
1935 propagateCounts(ParentCount, Else);
1936 }
1937 }
1938
1939 void VisitIfStmt(const IfStmt *S) {
1940 // "if constexpr" and "if consteval" are not normal conditional statements,
1941 // their discarded statement should be skipped
1942 if (S->isConsteval())
1943 return coverIfConsteval(S);
1944 else if (S->isConstexpr())
1945 return coverIfConstexpr(S);
1946
1947 extendRegion(S);
1948 if (S->getInit())
1949 Visit(S->getInit());
1950
1951 // Extend into the condition before we propagate through it below - this is
1952 // needed to handle macros that generate the "if" but not the condition.
1953 extendRegion(S->getCond());
1954
1955 Counter ParentCount = getRegion().getCounter();
1956 Counter ThenCount = llvm::EnableSingleByteCoverage
1957 ? getRegionCounter(S->getThen())
1958 : getRegionCounter(S);
1959
1960 // Emitting a counter for the condition makes it easier to interpret the
1961 // counter for the body when looking at the coverage.
1962 propagateCounts(ParentCount, S->getCond());
1963
1964 // The 'then' count applies to the area immediately after the condition.
1965 std::optional<SourceRange> Gap =
1966 findGapAreaBetween(S->getRParenLoc(), getStart(S->getThen()));
1967 if (Gap)
1968 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ThenCount);
1969
1970 extendRegion(S->getThen());
1971 Counter OutCount = propagateCounts(ThenCount, S->getThen());
1972
1973 Counter ElseCount;
1975 ElseCount = subtractCounters(ParentCount, ThenCount);
1976 else if (S->getElse())
1977 ElseCount = getRegionCounter(S->getElse());
1978
1979 if (const Stmt *Else = S->getElse()) {
1980 bool ThenHasTerminateStmt = HasTerminateStmt;
1981 HasTerminateStmt = false;
1982 // The 'else' count applies to the area immediately after the 'then'.
1983 std::optional<SourceRange> Gap =
1984 findGapAreaBetween(getEnd(S->getThen()), getStart(Else));
1985 if (Gap)
1986 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ElseCount);
1987 extendRegion(Else);
1988
1989 Counter ElseOutCount = propagateCounts(ElseCount, Else);
1991 OutCount = addCounters(OutCount, ElseOutCount);
1992
1993 if (ThenHasTerminateStmt)
1994 HasTerminateStmt = true;
1996 OutCount = addCounters(OutCount, ElseCount);
1997
1999 OutCount = getRegionCounter(S);
2000
2001 if (OutCount != ParentCount) {
2002 pushRegion(OutCount);
2003 GapRegionCounter = OutCount;
2004 }
2005
2006 if (!S->isConsteval() && !llvm::EnableSingleByteCoverage)
2007 // Create Branch Region around condition.
2008 createBranchRegion(S->getCond(), ThenCount,
2009 subtractCounters(ParentCount, ThenCount));
2010 }
2011
2012 void VisitCXXTryStmt(const CXXTryStmt *S) {
2013 extendRegion(S);
2014 // Handle macros that generate the "try" but not the rest.
2015 extendRegion(S->getTryBlock());
2016
2017 Counter ParentCount = getRegion().getCounter();
2018 propagateCounts(ParentCount, S->getTryBlock());
2019
2020 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
2021 Visit(S->getHandler(I));
2022
2023 Counter ExitCount = getRegionCounter(S);
2024 pushRegion(ExitCount);
2025 }
2026
2027 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
2028 propagateCounts(getRegionCounter(S), S->getHandlerBlock());
2029 }
2030
2031 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
2032 extendRegion(E);
2033
2034 Counter ParentCount = getRegion().getCounter();
2035 Counter TrueCount = llvm::EnableSingleByteCoverage
2036 ? getRegionCounter(E->getTrueExpr())
2037 : getRegionCounter(E);
2038 Counter OutCount;
2039
2040 if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) {
2041 propagateCounts(ParentCount, BCO->getCommon());
2042 OutCount = TrueCount;
2043 } else {
2044 propagateCounts(ParentCount, E->getCond());
2045 // The 'then' count applies to the area immediately after the condition.
2046 auto Gap =
2047 findGapAreaBetween(E->getQuestionLoc(), getStart(E->getTrueExpr()));
2048 if (Gap)
2049 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), TrueCount);
2050
2051 extendRegion(E->getTrueExpr());
2052 OutCount = propagateCounts(TrueCount, E->getTrueExpr());
2053 }
2054
2055 extendRegion(E->getFalseExpr());
2056 Counter FalseCount = llvm::EnableSingleByteCoverage
2057 ? getRegionCounter(E->getFalseExpr())
2058 : subtractCounters(ParentCount, TrueCount);
2059
2060 Counter FalseOutCount = propagateCounts(FalseCount, E->getFalseExpr());
2062 OutCount = getRegionCounter(E);
2063 else
2064 OutCount = addCounters(OutCount, FalseOutCount);
2065
2066 if (OutCount != ParentCount) {
2067 pushRegion(OutCount);
2068 GapRegionCounter = OutCount;
2069 }
2070
2071 // Create Branch Region around condition.
2073 createBranchRegion(E->getCond(), TrueCount,
2074 subtractCounters(ParentCount, TrueCount));
2075 }
2076
2077 void createDecision(const BinaryOperator *E) {
2078 unsigned NumConds = MCDCBuilder.getTotalConditionsAndReset(E);
2079 if (NumConds == 0)
2080 return;
2081
2082 auto DecisionParams = mcdc::DecisionParameters{
2083 MCDCState.DecisionByStmt[E].BitmapIdx,
2084 NumConds,
2085 };
2086
2087 // Create MCDC Decision Region.
2088 createDecisionRegion(E, DecisionParams);
2089 }
2090
2091 /// Check if E belongs to system headers.
2092 bool isExprInSystemHeader(const BinaryOperator *E) const {
2093 return (!SystemHeadersCoverage &&
2094 SM.isInSystemHeader(SM.getSpellingLoc(E->getOperatorLoc())) &&
2095 SM.isInSystemHeader(SM.getSpellingLoc(E->getBeginLoc())) &&
2096 SM.isInSystemHeader(SM.getSpellingLoc(E->getEndLoc())));
2097 }
2098
2099 void VisitBinLAnd(const BinaryOperator *E) {
2100 if (isExprInSystemHeader(E)) {
2101 LeafExprSet.insert(E);
2102 return;
2103 }
2104
2105 bool IsRootNode = MCDCBuilder.isIdle();
2106
2107 // Keep track of Binary Operator and assign MCDC condition IDs.
2108 MCDCBuilder.pushAndAssignIDs(E);
2109
2110 extendRegion(E->getLHS());
2111 propagateCounts(getRegion().getCounter(), E->getLHS());
2112 handleFileExit(getEnd(E->getLHS()));
2113
2114 // Track LHS True/False Decision.
2115 const auto DecisionLHS = MCDCBuilder.pop();
2116
2117 // Counter tracks the right hand side of a logical and operator.
2118 extendRegion(E->getRHS());
2119 propagateCounts(getRegionCounter(E), E->getRHS());
2120
2121 // Track RHS True/False Decision.
2122 const auto DecisionRHS = MCDCBuilder.back();
2123
2124 // Extract the RHS's Execution Counter.
2125 Counter RHSExecCnt = getRegionCounter(E);
2126
2127 // Extract the RHS's "True" Instance Counter.
2128 Counter RHSTrueCnt = getRegionCounter(E->getRHS());
2129
2130 // Extract the Parent Region Counter.
2131 Counter ParentCnt = getRegion().getCounter();
2132
2133 // Create Branch Region around LHS condition.
2135 createBranchRegion(E->getLHS(), RHSExecCnt,
2136 subtractCounters(ParentCnt, RHSExecCnt), DecisionLHS);
2137
2138 // Create Branch Region around RHS condition.
2140 createBranchRegion(E->getRHS(), RHSTrueCnt,
2141 subtractCounters(RHSExecCnt, RHSTrueCnt), DecisionRHS);
2142
2143 // Create MCDC Decision Region if at top-level (root).
2144 if (IsRootNode)
2145 createDecision(E);
2146 }
2147
2148 // Determine whether the right side of OR operation need to be visited.
2149 bool shouldVisitRHS(const Expr *LHS) {
2150 bool LHSIsTrue = false;
2151 bool LHSIsConst = false;
2152 if (!LHS->isValueDependent())
2153 LHSIsConst = LHS->EvaluateAsBooleanCondition(
2154 LHSIsTrue, CVM.getCodeGenModule().getContext());
2155 return !LHSIsConst || (LHSIsConst && !LHSIsTrue);
2156 }
2157
2158 void VisitBinLOr(const BinaryOperator *E) {
2159 if (isExprInSystemHeader(E)) {
2160 LeafExprSet.insert(E);
2161 return;
2162 }
2163
2164 bool IsRootNode = MCDCBuilder.isIdle();
2165
2166 // Keep track of Binary Operator and assign MCDC condition IDs.
2167 MCDCBuilder.pushAndAssignIDs(E);
2168
2169 extendRegion(E->getLHS());
2170 Counter OutCount = propagateCounts(getRegion().getCounter(), E->getLHS());
2171 handleFileExit(getEnd(E->getLHS()));
2172
2173 // Track LHS True/False Decision.
2174 const auto DecisionLHS = MCDCBuilder.pop();
2175
2176 // Counter tracks the right hand side of a logical or operator.
2177 extendRegion(E->getRHS());
2178 propagateCounts(getRegionCounter(E), E->getRHS());
2179
2180 // Track RHS True/False Decision.
2181 const auto DecisionRHS = MCDCBuilder.back();
2182
2183 // Extract the RHS's Execution Counter.
2184 Counter RHSExecCnt = getRegionCounter(E);
2185
2186 // Extract the RHS's "False" Instance Counter.
2187 Counter RHSFalseCnt = getRegionCounter(E->getRHS());
2188
2189 if (!shouldVisitRHS(E->getLHS())) {
2190 GapRegionCounter = OutCount;
2191 }
2192
2193 // Extract the Parent Region Counter.
2194 Counter ParentCnt = getRegion().getCounter();
2195
2196 // Create Branch Region around LHS condition.
2198 createBranchRegion(E->getLHS(), subtractCounters(ParentCnt, RHSExecCnt),
2199 RHSExecCnt, DecisionLHS);
2200
2201 // Create Branch Region around RHS condition.
2203 createBranchRegion(E->getRHS(), subtractCounters(RHSExecCnt, RHSFalseCnt),
2204 RHSFalseCnt, DecisionRHS);
2205
2206 // Create MCDC Decision Region if at top-level (root).
2207 if (IsRootNode)
2208 createDecision(E);
2209 }
2210
2211 void VisitLambdaExpr(const LambdaExpr *LE) {
2212 // Lambdas are treated as their own functions for now, so we shouldn't
2213 // propagate counts into them.
2214 }
2215
2216 void VisitArrayInitLoopExpr(const ArrayInitLoopExpr *AILE) {
2217 Visit(AILE->getCommonExpr()->getSourceExpr());
2218 }
2219
2220 void VisitPseudoObjectExpr(const PseudoObjectExpr *POE) {
2221 // Just visit syntatic expression as this is what users actually write.
2222 VisitStmt(POE->getSyntacticForm());
2223 }
2224
2225 void VisitOpaqueValueExpr(const OpaqueValueExpr* OVE) {
2226 Visit(OVE->getSourceExpr());
2227 }
2228};
2229
2230} // end anonymous namespace
2231
2232static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
2233 ArrayRef<CounterExpression> Expressions,
2235 OS << FunctionName << ":\n";
2236 CounterMappingContext Ctx(Expressions);
2237 for (const auto &R : Regions) {
2238 OS.indent(2);
2239 switch (R.Kind) {
2240 case CounterMappingRegion::CodeRegion:
2241 break;
2242 case CounterMappingRegion::ExpansionRegion:
2243 OS << "Expansion,";
2244 break;
2245 case CounterMappingRegion::SkippedRegion:
2246 OS << "Skipped,";
2247 break;
2248 case CounterMappingRegion::GapRegion:
2249 OS << "Gap,";
2250 break;
2251 case CounterMappingRegion::BranchRegion:
2252 case CounterMappingRegion::MCDCBranchRegion:
2253 OS << "Branch,";
2254 break;
2255 case CounterMappingRegion::MCDCDecisionRegion:
2256 OS << "Decision,";
2257 break;
2258 }
2259
2260 OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
2261 << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
2262
2263 if (const auto *DecisionParams =
2264 std::get_if<mcdc::DecisionParameters>(&R.MCDCParams)) {
2265 OS << "M:" << DecisionParams->BitmapIdx;
2266 OS << ", C:" << DecisionParams->NumConditions;
2267 } else {
2268 Ctx.dump(R.Count, OS);
2269
2270 if (R.Kind == CounterMappingRegion::BranchRegion ||
2271 R.Kind == CounterMappingRegion::MCDCBranchRegion) {
2272 OS << ", ";
2273 Ctx.dump(R.FalseCount, OS);
2274 }
2275 }
2276
2277 if (const auto *BranchParams =
2278 std::get_if<mcdc::BranchParameters>(&R.MCDCParams)) {
2279 OS << " [" << BranchParams->ID + 1 << ","
2280 << BranchParams->Conds[true] + 1;
2281 OS << "," << BranchParams->Conds[false] + 1 << "] ";
2282 }
2283
2284 if (R.Kind == CounterMappingRegion::ExpansionRegion)
2285 OS << " (Expanded file = " << R.ExpandedFileID << ")";
2286 OS << "\n";
2287 }
2288}
2289
2291 CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
2292 : CGM(CGM), SourceInfo(SourceInfo) {}
2293
2294std::string CoverageMappingModuleGen::getCurrentDirname() {
2295 if (!CGM.getCodeGenOpts().CoverageCompilationDir.empty())
2297
2298 SmallString<256> CWD;
2299 llvm::sys::fs::current_path(CWD);
2300 return CWD.str().str();
2301}
2302
2303std::string CoverageMappingModuleGen::normalizeFilename(StringRef Filename) {
2305 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
2306
2307 /// Traverse coverage prefix map in reverse order because prefix replacements
2308 /// are applied in reverse order starting from the last one when multiple
2309 /// prefix replacement options are provided.
2310 for (const auto &[From, To] :
2311 llvm::reverse(CGM.getCodeGenOpts().CoveragePrefixMap)) {
2312 if (llvm::sys::path::replace_path_prefix(Path, From, To))
2313 break;
2314 }
2315 return Path.str().str();
2316}
2317
2318static std::string getInstrProfSection(const CodeGenModule &CGM,
2319 llvm::InstrProfSectKind SK) {
2320 return llvm::getInstrProfSectionName(
2321 SK, CGM.getContext().getTargetInfo().getTriple().getObjectFormat());
2322}
2323
2324void CoverageMappingModuleGen::emitFunctionMappingRecord(
2325 const FunctionInfo &Info, uint64_t FilenamesRef) {
2326 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
2327
2328 // Assign a name to the function record. This is used to merge duplicates.
2329 std::string FuncRecordName = "__covrec_" + llvm::utohexstr(Info.NameHash);
2330
2331 // A dummy description for a function included-but-not-used in a TU can be
2332 // replaced by full description provided by a different TU. The two kinds of
2333 // descriptions play distinct roles: therefore, assign them different names
2334 // to prevent `linkonce_odr` merging.
2335 if (Info.IsUsed)
2336 FuncRecordName += "u";
2337
2338 // Create the function record type.
2339 const uint64_t NameHash = Info.NameHash;
2340 const uint64_t FuncHash = Info.FuncHash;
2341 const std::string &CoverageMapping = Info.CoverageMapping;
2342#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
2343 llvm::Type *FunctionRecordTypes[] = {
2344#include "llvm/ProfileData/InstrProfData.inc"
2345 };
2346 auto *FunctionRecordTy =
2347 llvm::StructType::get(Ctx, ArrayRef(FunctionRecordTypes),
2348 /*isPacked=*/true);
2349
2350 // Create the function record constant.
2351#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
2352 llvm::Constant *FunctionRecordVals[] = {
2353 #include "llvm/ProfileData/InstrProfData.inc"
2354 };
2355 auto *FuncRecordConstant =
2356 llvm::ConstantStruct::get(FunctionRecordTy, ArrayRef(FunctionRecordVals));
2357
2358 // Create the function record global.
2359 auto *FuncRecord = new llvm::GlobalVariable(
2360 CGM.getModule(), FunctionRecordTy, /*isConstant=*/true,
2361 llvm::GlobalValue::LinkOnceODRLinkage, FuncRecordConstant,
2362 FuncRecordName);
2363 FuncRecord->setVisibility(llvm::GlobalValue::HiddenVisibility);
2364 FuncRecord->setSection(getInstrProfSection(CGM, llvm::IPSK_covfun));
2365 FuncRecord->setAlignment(llvm::Align(8));
2366 if (CGM.supportsCOMDAT())
2367 FuncRecord->setComdat(CGM.getModule().getOrInsertComdat(FuncRecordName));
2368
2369 // Make sure the data doesn't get deleted.
2370 CGM.addUsedGlobal(FuncRecord);
2371}
2372
2374 llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
2375 const std::string &CoverageMapping, bool IsUsed) {
2376 const uint64_t NameHash = llvm::IndexedInstrProf::ComputeHash(NameValue);
2377 FunctionRecords.push_back({NameHash, FuncHash, CoverageMapping, IsUsed});
2378
2379 if (!IsUsed)
2380 FunctionNames.push_back(NamePtr);
2381
2382 if (CGM.getCodeGenOpts().DumpCoverageMapping) {
2383 // Dump the coverage mapping data for this function by decoding the
2384 // encoded data. This allows us to dump the mapping regions which were
2385 // also processed by the CoverageMappingWriter which performs
2386 // additional minimization operations such as reducing the number of
2387 // expressions.
2389 std::vector<StringRef> Filenames;
2390 std::vector<CounterExpression> Expressions;
2391 std::vector<CounterMappingRegion> Regions;
2392 FilenameStrs.resize(FileEntries.size() + 1);
2393 FilenameStrs[0] = normalizeFilename(getCurrentDirname());
2394 for (const auto &Entry : FileEntries) {
2395 auto I = Entry.second;
2396 FilenameStrs[I] = normalizeFilename(Entry.first.getName());
2397 }
2398 ArrayRef<std::string> FilenameRefs = llvm::ArrayRef(FilenameStrs);
2399 RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
2400 Expressions, Regions);
2401 if (Reader.read())
2402 return;
2403 dump(llvm::outs(), NameValue, Expressions, Regions);
2404 }
2405}
2406
2408 if (FunctionRecords.empty())
2409 return;
2410 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
2411 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
2412
2413 // Create the filenames and merge them with coverage mappings
2415 FilenameStrs.resize(FileEntries.size() + 1);
2416 // The first filename is the current working directory.
2417 FilenameStrs[0] = normalizeFilename(getCurrentDirname());
2418 for (const auto &Entry : FileEntries) {
2419 auto I = Entry.second;
2420 FilenameStrs[I] = normalizeFilename(Entry.first.getName());
2421 }
2422
2423 std::string Filenames;
2424 {
2425 llvm::raw_string_ostream OS(Filenames);
2426 CoverageFilenamesSectionWriter(FilenameStrs).write(OS);
2427 }
2428 auto *FilenamesVal =
2429 llvm::ConstantDataArray::getString(Ctx, Filenames, false);
2430 const int64_t FilenamesRef = llvm::IndexedInstrProf::ComputeHash(Filenames);
2431
2432 // Emit the function records.
2433 for (const FunctionInfo &Info : FunctionRecords)
2434 emitFunctionMappingRecord(Info, FilenamesRef);
2435
2436 const unsigned NRecords = 0;
2437 const size_t FilenamesSize = Filenames.size();
2438 const unsigned CoverageMappingSize = 0;
2439 llvm::Type *CovDataHeaderTypes[] = {
2440#define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
2441#include "llvm/ProfileData/InstrProfData.inc"
2442 };
2443 auto CovDataHeaderTy =
2444 llvm::StructType::get(Ctx, ArrayRef(CovDataHeaderTypes));
2445 llvm::Constant *CovDataHeaderVals[] = {
2446#define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
2447#include "llvm/ProfileData/InstrProfData.inc"
2448 };
2449 auto CovDataHeaderVal =
2450 llvm::ConstantStruct::get(CovDataHeaderTy, ArrayRef(CovDataHeaderVals));
2451
2452 // Create the coverage data record
2453 llvm::Type *CovDataTypes[] = {CovDataHeaderTy, FilenamesVal->getType()};
2454 auto CovDataTy = llvm::StructType::get(Ctx, ArrayRef(CovDataTypes));
2455 llvm::Constant *TUDataVals[] = {CovDataHeaderVal, FilenamesVal};
2456 auto CovDataVal = llvm::ConstantStruct::get(CovDataTy, ArrayRef(TUDataVals));
2457 auto CovData = new llvm::GlobalVariable(
2458 CGM.getModule(), CovDataTy, true, llvm::GlobalValue::PrivateLinkage,
2459 CovDataVal, llvm::getCoverageMappingVarName());
2460
2461 CovData->setSection(getInstrProfSection(CGM, llvm::IPSK_covmap));
2462 CovData->setAlignment(llvm::Align(8));
2463
2464 // Make sure the data doesn't get deleted.
2465 CGM.addUsedGlobal(CovData);
2466 // Create the deferred function records array
2467 if (!FunctionNames.empty()) {
2468 auto NamesArrTy = llvm::ArrayType::get(llvm::PointerType::getUnqual(Ctx),
2469 FunctionNames.size());
2470 auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
2471 // This variable will *NOT* be emitted to the object file. It is used
2472 // to pass the list of names referenced to codegen.
2473 new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
2474 llvm::GlobalValue::InternalLinkage, NamesArrVal,
2475 llvm::getCoverageUnusedNamesVarName());
2476 }
2477}
2478
2480 auto It = FileEntries.find(File);
2481 if (It != FileEntries.end())
2482 return It->second;
2483 unsigned FileID = FileEntries.size() + 1;
2484 FileEntries.insert(std::make_pair(File, FileID));
2485 return FileID;
2486}
2487
2489 llvm::raw_ostream &OS) {
2490 assert(CounterMap && MCDCState);
2491 CounterCoverageMappingBuilder Walker(CVM, *CounterMap, *MCDCState, SM,
2492 LangOpts);
2493 Walker.VisitDecl(D);
2494 Walker.write(OS);
2495}
2496
2498 llvm::raw_ostream &OS) {
2499 EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
2500 Walker.VisitDecl(D);
2501 Walker.write(OS);
2502}
NodeId Parent
Definition: ASTDiff.cpp:191
static char ID
Definition: Arena.cpp:183
#define SM(sm)
Definition: Cuda.cpp:83
Defines the Diagnostic-related interfaces.
static const MemRegion * getRegion(const CallEvent &Call, const MutexDescriptor &Descriptor, bool IsLock)
llvm::cl::opt< bool > SystemHeadersCoverage
static std::string getInstrProfSection(const CodeGenModule &CGM, llvm::InstrProfSectKind SK)
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
static llvm::cl::opt< bool > EmptyLineCommentCoverage("emptyline-comment-coverage", llvm::cl::desc("Emit emptylines and comment lines as skipped regions (only " "disable it on test)"), llvm::cl::init(true), llvm::cl::Hidden)
Defines the clang::FileManager interface and associated types.
StringRef Filename
Definition: Format.cpp:2975
llvm::DenseSet< const void * > Visited
Definition: HTMLLogger.cpp:146
SourceRange Range
Definition: SemaObjC.cpp:754
SourceLocation Loc
Definition: SemaObjC.cpp:755
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:757
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Definition: Expr.h:4141
Expr * getCond() const
getCond - Return the expression representing the condition for the ?: operator.
Definition: Expr.h:4319
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition: Expr.h:4325
SourceLocation getQuestionLoc() const
Definition: Expr.h:4168
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition: Expr.h:4331
Represents a loop initializing the elements of an array.
Definition: Expr.h:5511
OpaqueValueExpr * getCommonExpr() const
Get the common subexpression shared by all initializations (the source array).
Definition: Expr.h:5526
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3840
Expr * getLHS() const
Definition: Expr.h:3889
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.h:3894
SourceLocation getOperatorLoc() const
Definition: Expr.h:3881
Expr * getRHS() const
Definition: Expr.h:3891
SourceLocation getEndLoc() const LLVM_READONLY
Definition: Expr.h:3897
Opcode getOpcode() const
Definition: Expr.h:3884
BreakStmt - This represents a break.
Definition: Stmt.h:2980
CXXCatchStmt - This represents a C++ catch block.
Definition: StmtCXX.h:28
CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (ra...
Definition: StmtCXX.h:135
A C++ throw-expression (C++ [except.throw]).
Definition: ExprCXX.h:1202
const Expr * getSubExpr() const
Definition: ExprCXX.h:1222
CXXTryStmt - A C++ try block, including all handlers.
Definition: StmtCXX.h:69
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2820
Expr * getCallee()
Definition: Expr.h:2970
llvm::SmallVector< std::pair< std::string, std::string >, 0 > CoveragePrefixMap
Prefix replacement map for source-based code coverage to remap source file paths in coverage mapping.
std::string CoverageCompilationDir
The string to embed in coverage mapping as the current working directory.
This class organizes the cross-function state that is used while generating LLVM code.
llvm::Module & getModule() const
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
ASTContext & getContext() const
const CodeGenOptions & getCodeGenOpts() const
llvm::LLVMContext & getLLVMContext()
void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS)
Emit the coverage mapping data for an unused function.
void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS)
Emit the coverage mapping data which maps the regions of code to counters that will be used to find t...
Organizes the cross-function state that is used while generating code coverage mapping data.
void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName, StringRef FunctionNameValue, uint64_t FunctionHash, const std::string &CoverageMapping, bool IsUsed=true)
Add a function's coverage mapping record to the collection of the function mapping records.
CoverageSourceInfo & getSourceInfo() const
static CoverageSourceInfo * setUpCoverageCallbacks(Preprocessor &PP)
CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
void emit()
Emit the coverage mapping data for a translation unit.
CodeGenModule & getCodeGenModule()
Return an interface into CodeGenModule.
unsigned getFileID(FileEntryRef File)
Return the coverage mapping translation unit file id for the given file.
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:195
ContinueStmt - This represents a continue.
Definition: Stmt.h:2950
Represents a 'co_return' statement in the C++ Coroutines TS.
Definition: StmtCXX.h:473
Represents the body of a coroutine.
Definition: StmtCXX.h:320
Represents an expression that might suspend coroutine execution; either a co_await or co_yield expres...
Definition: ExprCXX.h:5061
Expr * getOperand() const
Definition: ExprCXX.h:5130
Stores additional source code information like skipped ranges which is required by the coverage mappi...
void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override
Hook called when a source range is skipped.
void updateNextTokLoc(SourceLocation Loc)
void AddSkippedRange(SourceRange Range, SkippedRange::Kind RangeKind)
std::vector< SkippedRange > & getSkippedRanges()
bool HandleComment(Preprocessor &PP, SourceRange Range) override
void HandleEmptyline(SourceRange Range) override
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition: DeclBase.h:1077
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
Definition: DeclBase.h:1083
DoStmt - This represents a 'do/while' stmt.
Definition: Stmt.h:2725
This represents one expression.
Definition: Expr.h:110
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition: Expr.h:175
bool EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsBooleanCondition - Return true if this is a constant which we can fold and convert to a boo...
QualType getType() const
Definition: Expr.h:142
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
ForStmt - This represents a 'for (init;cond;inc)' stmt.
Definition: Stmt.h:2781
bool getNoReturn() const
Definition: Type.h:4415
GotoStmt - This represents a direct goto.
Definition: Stmt.h:2862
IfStmt - This represents an if/then/else.
Definition: Stmt.h:2138
LabelStmt - Represents a label, which has a substatement.
Definition: Stmt.h:2031
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1950
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:461
static unsigned MeasureTokenLength(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
MeasureTokenLength - Relex the token at the specified location and return its length in bytes in the ...
Definition: Lexer.cpp:499
Represents Objective-C's collection statement.
Definition: StmtObjC.h:23
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition: Expr.h:1168
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:1218
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
void addCommentHandler(CommentHandler *Handler)
Add the specified comment handler to the preprocessor.
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
SourceManager & getSourceManager() const
void setPreprocessToken(bool Preprocess)
void setTokenWatcher(llvm::unique_function< void(const clang::Token &)> F)
Register a function that would be called on each token in the final expanded token stream.
void setEmptylineHandler(EmptylineHandler *Handler)
Set empty line handler.
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6305
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition: Expr.h:6347
A (possibly-)qualified type.
Definition: Type.h:940
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition: Stmt.h:3019
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Each ExpansionInfo encodes the expansion location - where the token was ultimately expanded,...
SourceLocation getExpansionLocStart() const
bool isFunctionMacroExpansion() const
SourceLocation getExpansionLocEnd() const
Stmt - This represents one statement.
Definition: Stmt.h:84
SourceLocation getColonLoc() const
Definition: Stmt.h:1780
const SwitchCase * getNextSwitchCase() const
Definition: Stmt.h:1774
SwitchStmt - This represents a 'switch' stmt.
Definition: Stmt.h:2388
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1256
Token - This structure provides full information about a lexed token.
Definition: Token.h:36
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:132
tok::TokenKind getKind() const
Definition: Token.h:94
WhileStmt - This represents a 'while' stmt.
Definition: Stmt.h:2584
llvm::cl::opt< std::string > Filter
The JSON file list parser is used to communicate input to InstallAPI.
FunctionType::ExtInfo getFunctionExtInfo(const Type &t)
Definition: Type.h:7512
unsigned long uint64_t
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
cl::opt< bool > EnableSingleByteCoverage
#define false
Definition: stdbool.h:26
Per-Function MC/DC state.
Definition: MCDCState.h:28
llvm::DenseMap< const Stmt *, Branch > BranchByStmt
Definition: MCDCState.h:41
llvm::DenseMap< const Stmt *, Decision > DecisionByStmt
Definition: MCDCState.h:35
EvalResult is a struct with detailed info about an evaluated expression.
Definition: Expr.h:642