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