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