clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CoverageMappingGen.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/lib/CodeGen/CoverageMappingGen.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "CoverageMappingGen.h" |
14 | #include "CodeGenFunction.h" |
15 | #include "clang/AST/StmtVisitor.h" |
16 | #include "clang/Basic/Diagnostic.h" |
17 | #include "clang/Basic/FileManager.h" |
18 | #include "clang/Frontend/FrontendDiagnostic.h" |
19 | #include "clang/Lex/Lexer.h" |
20 | #include "llvm/ADT/Optional.h" |
21 | #include "llvm/ADT/SmallSet.h" |
22 | #include "llvm/ADT/StringExtras.h" |
23 | #include "llvm/ProfileData/Coverage/CoverageMapping.h" |
24 | #include "llvm/ProfileData/Coverage/CoverageMappingReader.h" |
25 | #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h" |
26 | #include "llvm/ProfileData/InstrProfReader.h" |
27 | #include "llvm/Support/FileSystem.h" |
28 | #include "llvm/Support/Path.h" |
29 | |
30 | |
31 | |
32 | #define COVMAP_V3 |
33 | |
34 | static llvm::cl::opt<bool> EmptyLineCommentCoverage( |
35 | "emptyline-comment-coverage", |
36 | llvm::cl::desc("Emit emptylines and comment lines as skipped regions (only " |
37 | "disable it on test)"), |
38 | llvm::cl::init(true), llvm::cl::Hidden); |
39 | |
40 | using namespace clang; |
41 | using namespace CodeGen; |
42 | using namespace llvm::coverage; |
43 | |
44 | CoverageSourceInfo * |
45 | CoverageMappingModuleGen::setUpCoverageCallbacks(Preprocessor &PP) { |
46 | CoverageSourceInfo *CoverageInfo = |
47 | new CoverageSourceInfo(PP.getSourceManager()); |
48 | PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(CoverageInfo)); |
49 | if (EmptyLineCommentCoverage) { |
50 | PP.addCommentHandler(CoverageInfo); |
51 | PP.setEmptylineHandler(CoverageInfo); |
52 | PP.setPreprocessToken(true); |
53 | PP.setTokenWatcher([CoverageInfo](clang::Token Tok) { |
54 | |
55 | CoverageInfo->PrevTokLoc = Tok.getLocation(); |
56 | if (Tok.getKind() != clang::tok::eod) |
57 | CoverageInfo->updateNextTokLoc(Tok.getLocation()); |
58 | }); |
59 | } |
60 | return CoverageInfo; |
61 | } |
62 | |
63 | void CoverageSourceInfo::AddSkippedRange(SourceRange Range) { |
64 | if (EmptyLineCommentCoverage && !SkippedRanges.empty() && |
65 | PrevTokLoc == SkippedRanges.back().PrevTokLoc && |
66 | SourceMgr.isWrittenInSameFile(SkippedRanges.back().Range.getEnd(), |
67 | Range.getBegin())) |
68 | SkippedRanges.back().Range.setEnd(Range.getEnd()); |
69 | else |
70 | SkippedRanges.push_back({Range, PrevTokLoc}); |
71 | } |
72 | |
73 | void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range, SourceLocation) { |
74 | AddSkippedRange(Range); |
75 | } |
76 | |
77 | void CoverageSourceInfo::HandleEmptyline(SourceRange Range) { |
78 | AddSkippedRange(Range); |
79 | } |
80 | |
81 | bool CoverageSourceInfo::HandleComment(Preprocessor &PP, SourceRange Range) { |
82 | AddSkippedRange(Range); |
83 | return false; |
84 | } |
85 | |
86 | void CoverageSourceInfo::updateNextTokLoc(SourceLocation Loc) { |
87 | if (!SkippedRanges.empty() && SkippedRanges.back().NextTokLoc.isInvalid()) |
88 | SkippedRanges.back().NextTokLoc = Loc; |
89 | } |
90 | |
91 | namespace { |
92 | |
93 | |
94 | class SourceMappingRegion { |
95 | |
96 | Counter Count; |
97 | |
98 | |
99 | Optional<Counter> FalseCount; |
100 | |
101 | |
102 | Optional<SourceLocation> LocStart; |
103 | |
104 | |
105 | Optional<SourceLocation> LocEnd; |
106 | |
107 | |
108 | |
109 | bool GapRegion; |
110 | |
111 | public: |
112 | SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart, |
113 | Optional<SourceLocation> LocEnd, bool GapRegion = false) |
114 | : Count(Count), LocStart(LocStart), LocEnd(LocEnd), GapRegion(GapRegion) { |
115 | } |
116 | |
117 | SourceMappingRegion(Counter Count, Optional<Counter> FalseCount, |
118 | Optional<SourceLocation> LocStart, |
119 | Optional<SourceLocation> LocEnd, bool GapRegion = false) |
120 | : Count(Count), FalseCount(FalseCount), LocStart(LocStart), |
121 | LocEnd(LocEnd), GapRegion(GapRegion) {} |
122 | |
123 | const Counter &getCounter() const { return Count; } |
124 | |
125 | const Counter &getFalseCounter() const { |
126 | assert(FalseCount && "Region has no alternate counter"); |
127 | return *FalseCount; |
128 | } |
129 | |
130 | void setCounter(Counter C) { Count = C; } |
131 | |
132 | bool hasStartLoc() const { return LocStart.hasValue(); } |
133 | |
134 | void setStartLoc(SourceLocation Loc) { LocStart = Loc; } |
135 | |
136 | SourceLocation getBeginLoc() const { |
137 | assert(LocStart && "Region has no start location"); |
138 | return *LocStart; |
139 | } |
140 | |
141 | bool hasEndLoc() const { return LocEnd.hasValue(); } |
142 | |
143 | void setEndLoc(SourceLocation Loc) { |
144 | assert(Loc.isValid() && "Setting an invalid end location"); |
145 | LocEnd = Loc; |
146 | } |
147 | |
148 | SourceLocation getEndLoc() const { |
149 | assert(LocEnd && "Region has no end location"); |
150 | return *LocEnd; |
151 | } |
152 | |
153 | bool isGap() const { return GapRegion; } |
154 | |
155 | void setGap(bool Gap) { GapRegion = Gap; } |
156 | |
157 | bool isBranch() const { return FalseCount.hasValue(); } |
158 | }; |
159 | |
160 | |
161 | struct SpellingRegion { |
162 | |
163 | unsigned LineStart; |
164 | |
165 | |
166 | unsigned ColumnStart; |
167 | |
168 | |
169 | unsigned LineEnd; |
170 | |
171 | |
172 | unsigned ColumnEnd; |
173 | |
174 | SpellingRegion(SourceManager &SM, SourceLocation LocStart, |
175 | SourceLocation LocEnd) { |
176 | LineStart = SM.getSpellingLineNumber(LocStart); |
177 | ColumnStart = SM.getSpellingColumnNumber(LocStart); |
178 | LineEnd = SM.getSpellingLineNumber(LocEnd); |
179 | ColumnEnd = SM.getSpellingColumnNumber(LocEnd); |
180 | } |
181 | |
182 | SpellingRegion(SourceManager &SM, SourceMappingRegion &R) |
183 | : SpellingRegion(SM, R.getBeginLoc(), R.getEndLoc()) {} |
184 | |
185 | |
186 | |
187 | bool isInSourceOrder() const { |
188 | return (LineStart < LineEnd) || |
189 | (LineStart == LineEnd && ColumnStart <= ColumnEnd); |
190 | } |
191 | }; |
192 | |
193 | |
194 | |
195 | class CoverageMappingBuilder { |
196 | public: |
197 | CoverageMappingModuleGen &CVM; |
198 | SourceManager &SM; |
199 | const LangOptions &LangOpts; |
200 | |
201 | private: |
202 | |
203 | llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8> |
204 | FileIDMapping; |
205 | |
206 | public: |
207 | |
208 | llvm::SmallVector<CounterMappingRegion, 32> MappingRegions; |
209 | |
210 | std::vector<SourceMappingRegion> SourceRegions; |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | typedef llvm::SmallSet<std::pair<SourceLocation, SourceLocation>, 8> |
218 | SourceRegionFilter; |
219 | |
220 | CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM, |
221 | const LangOptions &LangOpts) |
222 | : CVM(CVM), SM(SM), LangOpts(LangOpts) {} |
223 | |
224 | |
225 | SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) { |
226 | |
227 | |
228 | unsigned TokLen = |
229 | Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts); |
230 | return Loc.getLocWithOffset(TokLen); |
231 | } |
232 | |
233 | |
234 | SourceLocation getStartOfFileOrMacro(SourceLocation Loc) { |
235 | if (Loc.isMacroID()) |
236 | return Loc.getLocWithOffset(-SM.getFileOffset(Loc)); |
237 | return SM.getLocForStartOfFile(SM.getFileID(Loc)); |
238 | } |
239 | |
240 | |
241 | SourceLocation getEndOfFileOrMacro(SourceLocation Loc) { |
242 | if (Loc.isMacroID()) |
243 | return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) - |
244 | SM.getFileOffset(Loc)); |
245 | return SM.getLocForEndOfFile(SM.getFileID(Loc)); |
246 | } |
247 | |
248 | |
249 | SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) { |
250 | return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).getBegin() |
251 | : SM.getIncludeLoc(SM.getFileID(Loc)); |
252 | } |
253 | |
254 | |
255 | bool isInBuiltin(SourceLocation Loc) { |
256 | return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>"; |
257 | } |
258 | |
259 | |
260 | bool isNestedIn(SourceLocation Loc, FileID Parent) { |
261 | do { |
262 | Loc = getIncludeOrExpansionLoc(Loc); |
263 | if (Loc.isInvalid()) |
264 | return false; |
265 | } while (!SM.isInFileID(Loc, Parent)); |
266 | return true; |
267 | } |
268 | |
269 | |
270 | SourceLocation getStart(const Stmt *S) { |
271 | SourceLocation Loc = S->getBeginLoc(); |
| 12 | | Called C++ object pointer is null |
|
272 | while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc)) |
273 | Loc = SM.getImmediateExpansionRange(Loc).getBegin(); |
274 | return Loc; |
275 | } |
276 | |
277 | |
278 | SourceLocation getEnd(const Stmt *S) { |
279 | SourceLocation Loc = S->getEndLoc(); |
280 | while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc)) |
281 | Loc = SM.getImmediateExpansionRange(Loc).getBegin(); |
282 | return getPreciseTokenLocEnd(Loc); |
283 | } |
284 | |
285 | |
286 | |
287 | |
288 | |
289 | |
290 | void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) { |
291 | FileIDMapping.clear(); |
292 | |
293 | llvm::SmallSet<FileID, 8> Visited; |
294 | SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs; |
295 | for (const auto &Region : SourceRegions) { |
296 | SourceLocation Loc = Region.getBeginLoc(); |
297 | FileID File = SM.getFileID(Loc); |
298 | if (!Visited.insert(File).second) |
299 | continue; |
300 | |
301 | |
302 | if (SM.isInSystemHeader(SM.getSpellingLoc(Loc))) |
303 | continue; |
304 | |
305 | unsigned Depth = 0; |
306 | for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc); |
307 | Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent)) |
308 | ++Depth; |
309 | FileLocs.push_back(std::make_pair(Loc, Depth)); |
310 | } |
311 | llvm::stable_sort(FileLocs, llvm::less_second()); |
312 | |
313 | for (const auto &FL : FileLocs) { |
314 | SourceLocation Loc = FL.first; |
315 | FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first; |
316 | auto Entry = SM.getFileEntryForID(SpellingFile); |
317 | if (!Entry) |
318 | continue; |
319 | |
320 | FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc); |
321 | Mapping.push_back(CVM.getFileID(Entry)); |
322 | } |
323 | } |
324 | |
325 | |
326 | |
327 | |
328 | Optional<unsigned> getCoverageFileID(SourceLocation Loc) { |
329 | auto Mapping = FileIDMapping.find(SM.getFileID(Loc)); |
330 | if (Mapping != FileIDMapping.end()) |
331 | return Mapping->second.first; |
332 | return None; |
333 | } |
334 | |
335 | |
336 | |
337 | |
338 | Optional<SpellingRegion> adjustSkippedRange(SourceManager &SM, |
339 | SourceLocation LocStart, |
340 | SourceLocation LocEnd, |
341 | SourceLocation PrevTokLoc, |
342 | SourceLocation NextTokLoc) { |
343 | SpellingRegion SR{SM, LocStart, LocEnd}; |
344 | SR.ColumnStart = 1; |
345 | if (PrevTokLoc.isValid() && SM.isWrittenInSameFile(LocStart, PrevTokLoc) && |
346 | SR.LineStart == SM.getSpellingLineNumber(PrevTokLoc)) |
347 | SR.LineStart++; |
348 | if (NextTokLoc.isValid() && SM.isWrittenInSameFile(LocEnd, NextTokLoc) && |
349 | SR.LineEnd == SM.getSpellingLineNumber(NextTokLoc)) { |
350 | SR.LineEnd--; |
351 | SR.ColumnEnd++; |
352 | } |
353 | if (SR.isInSourceOrder()) |
354 | return SR; |
355 | return None; |
356 | } |
357 | |
358 | |
359 | |
360 | void gatherSkippedRegions() { |
361 | |
362 | |
363 | llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges; |
364 | FileLineRanges.resize( |
365 | FileIDMapping.size(), |
366 | std::make_pair(std::numeric_limits<unsigned>::max(), 0)); |
367 | for (const auto &R : MappingRegions) { |
368 | FileLineRanges[R.FileID].first = |
369 | std::min(FileLineRanges[R.FileID].first, R.LineStart); |
370 | FileLineRanges[R.FileID].second = |
371 | std::max(FileLineRanges[R.FileID].second, R.LineEnd); |
372 | } |
373 | |
374 | auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges(); |
375 | for (auto &I : SkippedRanges) { |
376 | SourceRange Range = I.Range; |
377 | auto LocStart = Range.getBegin(); |
378 | auto LocEnd = Range.getEnd(); |
379 | assert(SM.isWrittenInSameFile(LocStart, LocEnd) && |
380 | "region spans multiple files"); |
381 | |
382 | auto CovFileID = getCoverageFileID(LocStart); |
383 | if (!CovFileID) |
384 | continue; |
385 | Optional<SpellingRegion> SR = |
386 | adjustSkippedRange(SM, LocStart, LocEnd, I.PrevTokLoc, I.NextTokLoc); |
387 | if (!SR.hasValue()) |
388 | continue; |
389 | auto Region = CounterMappingRegion::makeSkipped( |
390 | *CovFileID, SR->LineStart, SR->ColumnStart, SR->LineEnd, |
391 | SR->ColumnEnd); |
392 | |
393 | |
394 | if (Region.LineStart >= FileLineRanges[*CovFileID].first && |
395 | Region.LineEnd <= FileLineRanges[*CovFileID].second) |
396 | MappingRegions.push_back(Region); |
397 | } |
398 | } |
399 | |
400 | |
401 | |
402 | void emitSourceRegions(const SourceRegionFilter &Filter) { |
403 | for (const auto &Region : SourceRegions) { |
404 | assert(Region.hasEndLoc() && "incomplete region"); |
405 | |
406 | SourceLocation LocStart = Region.getBeginLoc(); |
407 | assert(SM.getFileID(LocStart).isValid() && "region in invalid file"); |
408 | |
409 | |
410 | if (SM.isInSystemHeader(SM.getSpellingLoc(LocStart))) |
411 | continue; |
412 | |
413 | auto CovFileID = getCoverageFileID(LocStart); |
414 | |
415 | if (!CovFileID) |
416 | continue; |
417 | |
418 | SourceLocation LocEnd = Region.getEndLoc(); |
419 | assert(SM.isWrittenInSameFile(LocStart, LocEnd) && |
420 | "region spans multiple files"); |
421 | |
422 | |
423 | |
424 | |
425 | |
426 | if (Filter.count(std::make_pair(LocStart, LocEnd))) |
427 | continue; |
428 | |
429 | |
430 | SpellingRegion SR{SM, LocStart, LocEnd}; |
431 | assert(SR.isInSourceOrder() && "region start and end out of order"); |
432 | |
433 | if (Region.isGap()) { |
434 | MappingRegions.push_back(CounterMappingRegion::makeGapRegion( |
435 | Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart, |
436 | SR.LineEnd, SR.ColumnEnd)); |
437 | } else if (Region.isBranch()) { |
438 | MappingRegions.push_back(CounterMappingRegion::makeBranchRegion( |
439 | Region.getCounter(), Region.getFalseCounter(), *CovFileID, |
440 | SR.LineStart, SR.ColumnStart, SR.LineEnd, SR.ColumnEnd)); |
441 | } else { |
442 | MappingRegions.push_back(CounterMappingRegion::makeRegion( |
443 | Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart, |
444 | SR.LineEnd, SR.ColumnEnd)); |
445 | } |
446 | } |
447 | } |
448 | |
449 | |
450 | SourceRegionFilter emitExpansionRegions() { |
451 | SourceRegionFilter Filter; |
452 | for (const auto &FM : FileIDMapping) { |
453 | SourceLocation ExpandedLoc = FM.second.second; |
454 | SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc); |
455 | if (ParentLoc.isInvalid()) |
456 | continue; |
457 | |
458 | auto ParentFileID = getCoverageFileID(ParentLoc); |
459 | if (!ParentFileID) |
460 | continue; |
461 | auto ExpandedFileID = getCoverageFileID(ExpandedLoc); |
462 | assert(ExpandedFileID && "expansion in uncovered file"); |
463 | |
464 | SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc); |
465 | assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) && |
466 | "region spans multiple files"); |
467 | Filter.insert(std::make_pair(ParentLoc, LocEnd)); |
468 | |
469 | SpellingRegion SR{SM, ParentLoc, LocEnd}; |
470 | assert(SR.isInSourceOrder() && "region start and end out of order"); |
471 | MappingRegions.push_back(CounterMappingRegion::makeExpansion( |
472 | *ParentFileID, *ExpandedFileID, SR.LineStart, SR.ColumnStart, |
473 | SR.LineEnd, SR.ColumnEnd)); |
474 | } |
475 | return Filter; |
476 | } |
477 | }; |
478 | |
479 | |
480 | |
481 | struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder { |
482 | EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM, |
483 | const LangOptions &LangOpts) |
484 | : CoverageMappingBuilder(CVM, SM, LangOpts) {} |
485 | |
486 | void VisitDecl(const Decl *D) { |
487 | if (!D->hasBody()) |
488 | return; |
489 | auto Body = D->getBody(); |
490 | SourceLocation Start = getStart(Body); |
491 | SourceLocation End = getEnd(Body); |
492 | if (!SM.isWrittenInSameFile(Start, End)) { |
493 | |
494 | |
495 | FileID StartFileID = SM.getFileID(Start); |
496 | FileID EndFileID = SM.getFileID(End); |
497 | while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) { |
498 | Start = getIncludeOrExpansionLoc(Start); |
499 | assert(Start.isValid() && |
500 | "Declaration start location not nested within a known region"); |
501 | StartFileID = SM.getFileID(Start); |
502 | } |
503 | while (StartFileID != EndFileID) { |
504 | End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End)); |
505 | assert(End.isValid() && |
506 | "Declaration end location not nested within a known region"); |
507 | EndFileID = SM.getFileID(End); |
508 | } |
509 | } |
510 | SourceRegions.emplace_back(Counter(), Start, End); |
511 | } |
512 | |
513 | |
514 | void write(llvm::raw_ostream &OS) { |
515 | SmallVector<unsigned, 16> FileIDMapping; |
516 | gatherFileIDs(FileIDMapping); |
517 | emitSourceRegions(SourceRegionFilter()); |
518 | |
519 | if (MappingRegions.empty()) |
520 | return; |
521 | |
522 | CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions); |
523 | Writer.write(OS); |
524 | } |
525 | }; |
526 | |
527 | |
528 | |
529 | struct CounterCoverageMappingBuilder |
530 | : public CoverageMappingBuilder, |
531 | public ConstStmtVisitor<CounterCoverageMappingBuilder> { |
532 | |
533 | llvm::DenseMap<const Stmt *, unsigned> &CounterMap; |
534 | |
535 | |
536 | std::vector<SourceMappingRegion> RegionStack; |
537 | |
538 | CounterExpressionBuilder Builder; |
539 | |
540 | |
541 | |
542 | |
543 | |
544 | SourceLocation MostRecentLocation; |
545 | |
546 | |
547 | bool HasTerminateStmt = false; |
548 | |
549 | |
550 | Counter GapRegionCounter; |
551 | |
552 | |
553 | Counter subtractCounters(Counter LHS, Counter RHS) { |
554 | return Builder.subtract(LHS, RHS); |
555 | } |
556 | |
557 | |
558 | Counter addCounters(Counter LHS, Counter RHS) { |
559 | return Builder.add(LHS, RHS); |
560 | } |
561 | |
562 | Counter addCounters(Counter C1, Counter C2, Counter C3) { |
563 | return addCounters(addCounters(C1, C2), C3); |
564 | } |
565 | |
566 | |
567 | |
568 | |
569 | Counter getRegionCounter(const Stmt *S) { |
570 | return Counter::getCounter(CounterMap[S]); |
571 | } |
572 | |
573 | |
574 | |
575 | |
576 | |
577 | size_t pushRegion(Counter Count, Optional<SourceLocation> StartLoc = None, |
578 | Optional<SourceLocation> EndLoc = None, |
579 | Optional<Counter> FalseCount = None) { |
580 | |
581 | if (StartLoc && !FalseCount.hasValue()) { |
582 | MostRecentLocation = *StartLoc; |
583 | } |
584 | |
585 | RegionStack.emplace_back(Count, FalseCount, StartLoc, EndLoc); |
586 | |
587 | return RegionStack.size() - 1; |
588 | } |
589 | |
590 | size_t locationDepth(SourceLocation Loc) { |
591 | size_t Depth = 0; |
592 | while (Loc.isValid()) { |
593 | Loc = getIncludeOrExpansionLoc(Loc); |
594 | Depth++; |
595 | } |
596 | return Depth; |
597 | } |
598 | |
599 | |
600 | |
601 | |
602 | |
603 | void popRegions(size_t ParentIndex) { |
604 | assert(RegionStack.size() >= ParentIndex && "parent not in stack"); |
605 | while (RegionStack.size() > ParentIndex) { |
606 | SourceMappingRegion &Region = RegionStack.back(); |
607 | if (Region.hasStartLoc()) { |
608 | SourceLocation StartLoc = Region.getBeginLoc(); |
609 | SourceLocation EndLoc = Region.hasEndLoc() |
610 | ? Region.getEndLoc() |
611 | : RegionStack[ParentIndex].getEndLoc(); |
612 | bool isBranch = Region.isBranch(); |
613 | size_t StartDepth = locationDepth(StartLoc); |
614 | size_t EndDepth = locationDepth(EndLoc); |
615 | while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) { |
616 | bool UnnestStart = StartDepth >= EndDepth; |
617 | bool UnnestEnd = EndDepth >= StartDepth; |
618 | if (UnnestEnd) { |
619 | |
620 | |
621 | |
622 | |
623 | |
624 | |
625 | SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc); |
626 | assert(SM.isWrittenInSameFile(NestedLoc, EndLoc)); |
627 | |
628 | if (!isBranch && !isRegionAlreadyAdded(NestedLoc, EndLoc)) |
629 | SourceRegions.emplace_back(Region.getCounter(), NestedLoc, |
630 | EndLoc); |
631 | |
632 | EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc)); |
633 | if (EndLoc.isInvalid()) |
634 | llvm::report_fatal_error( |
635 | "File exit not handled before popRegions"); |
636 | EndDepth--; |
637 | } |
638 | if (UnnestStart) { |
639 | |
640 | |
641 | |
642 | |
643 | |
644 | |
645 | SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc); |
646 | assert(SM.isWrittenInSameFile(StartLoc, NestedLoc)); |
647 | |
648 | if (!isBranch && !isRegionAlreadyAdded(StartLoc, NestedLoc)) |
649 | SourceRegions.emplace_back(Region.getCounter(), StartLoc, |
650 | NestedLoc); |
651 | |
652 | StartLoc = getIncludeOrExpansionLoc(StartLoc); |
653 | if (StartLoc.isInvalid()) |
654 | llvm::report_fatal_error( |
655 | "File exit not handled before popRegions"); |
656 | StartDepth--; |
657 | } |
658 | } |
659 | Region.setStartLoc(StartLoc); |
660 | Region.setEndLoc(EndLoc); |
661 | |
662 | if (!isBranch) { |
663 | MostRecentLocation = EndLoc; |
664 | |
665 | |
666 | if (StartLoc == getStartOfFileOrMacro(StartLoc) && |
667 | EndLoc == getEndOfFileOrMacro(EndLoc)) |
668 | MostRecentLocation = getIncludeOrExpansionLoc(EndLoc); |
669 | } |
670 | |
671 | assert(SM.isWrittenInSameFile(Region.getBeginLoc(), EndLoc)); |
672 | assert(SpellingRegion(SM, Region).isInSourceOrder()); |
673 | SourceRegions.push_back(Region); |
674 | } |
675 | RegionStack.pop_back(); |
676 | } |
677 | } |
678 | |
679 | |
680 | SourceMappingRegion &getRegion() { |
681 | assert(!RegionStack.empty() && "statement has no region"); |
682 | return RegionStack.back(); |
683 | } |
684 | |
685 | |
686 | |
687 | Counter propagateCounts(Counter TopCount, const Stmt *S, |
688 | bool VisitChildren = true) { |
689 | SourceLocation StartLoc = getStart(S); |
| 10 | | Passing null pointer value via 1st parameter 'S' | |
|
| 11 | | Calling 'CoverageMappingBuilder::getStart' | |
|
690 | SourceLocation EndLoc = getEnd(S); |
691 | size_t Index = pushRegion(TopCount, StartLoc, EndLoc); |
692 | if (VisitChildren) |
693 | Visit(S); |
694 | Counter ExitCount = getRegion().getCounter(); |
695 | popRegions(Index); |
696 | |
697 | |
698 | |
699 | if (SM.isBeforeInTranslationUnit(StartLoc, S->getBeginLoc())) |
700 | MostRecentLocation = EndLoc; |
701 | |
702 | return ExitCount; |
703 | } |
704 | |
705 | |
706 | bool ConditionFoldsToBool(const Expr *Cond) { |
707 | Expr::EvalResult Result; |
708 | return (Cond->EvaluateAsInt(Result, CVM.getCodeGenModule().getContext())); |
709 | } |
710 | |
711 | |
712 | |
713 | |
714 | |
715 | void createBranchRegion(const Expr *C, Counter TrueCnt, Counter FalseCnt) { |
716 | |
717 | if (!C) |
718 | return; |
719 | |
720 | |
721 | |
722 | |
723 | |
724 | if (CodeGenFunction::isInstrumentedCondition(C)) { |
725 | |
726 | |
727 | |
728 | |
729 | |
730 | |
731 | if (ConditionFoldsToBool(C)) |
732 | popRegions(pushRegion(Counter::getZero(), getStart(C), getEnd(C), |
733 | Counter::getZero())); |
734 | else |
735 | |
736 | popRegions(pushRegion(TrueCnt, getStart(C), getEnd(C), FalseCnt)); |
737 | } |
738 | } |
739 | |
740 | |
741 | |
742 | void createSwitchCaseRegion(const SwitchCase *SC, Counter TrueCnt, |
743 | Counter FalseCnt) { |
744 | |
745 | |
746 | |
747 | popRegions(pushRegion(TrueCnt, getStart(SC), SC->getColonLoc(), FalseCnt)); |
748 | } |
749 | |
750 | |
751 | |
752 | bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc, |
753 | bool isBranch = false) { |
754 | return SourceRegions.rend() != |
755 | std::find_if(SourceRegions.rbegin(), SourceRegions.rend(), |
756 | [&](const SourceMappingRegion &Region) { |
757 | return Region.getBeginLoc() == StartLoc && |
758 | Region.getEndLoc() == EndLoc && |
759 | Region.isBranch() == isBranch; |
760 | }); |
761 | } |
762 | |
763 | |
764 | |
765 | |
766 | void adjustForOutOfOrderTraversal(SourceLocation EndLoc) { |
767 | MostRecentLocation = EndLoc; |
768 | |
769 | |
770 | |
771 | |
772 | |
773 | if (getRegion().hasEndLoc() && |
774 | MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) && |
775 | isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation), |
776 | MostRecentLocation, getRegion().isBranch())) |
777 | MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation); |
778 | } |
779 | |
780 | |
781 | |
782 | |
783 | |
784 | |
785 | void handleFileExit(SourceLocation NewLoc) { |
786 | if (NewLoc.isInvalid() || |
787 | SM.isWrittenInSameFile(MostRecentLocation, NewLoc)) |
788 | return; |
789 | |
790 | |
791 | |
792 | SourceLocation LCA = NewLoc; |
793 | FileID ParentFile = SM.getFileID(LCA); |
794 | while (!isNestedIn(MostRecentLocation, ParentFile)) { |
795 | LCA = getIncludeOrExpansionLoc(LCA); |
796 | if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) { |
797 | |
798 | |
799 | MostRecentLocation = NewLoc; |
800 | return; |
801 | } |
802 | ParentFile = SM.getFileID(LCA); |
803 | } |
804 | |
805 | llvm::SmallSet<SourceLocation, 8> StartLocs; |
806 | Optional<Counter> ParentCounter; |
807 | for (SourceMappingRegion &I : llvm::reverse(RegionStack)) { |
808 | if (!I.hasStartLoc()) |
809 | continue; |
810 | SourceLocation Loc = I.getBeginLoc(); |
811 | if (!isNestedIn(Loc, ParentFile)) { |
812 | ParentCounter = I.getCounter(); |
813 | break; |
814 | } |
815 | |
816 | while (!SM.isInFileID(Loc, ParentFile)) { |
817 | |
818 | |
819 | |
820 | if (StartLocs.insert(Loc).second) { |
821 | if (I.isBranch()) |
822 | SourceRegions.emplace_back(I.getCounter(), I.getFalseCounter(), Loc, |
823 | getEndOfFileOrMacro(Loc), I.isBranch()); |
824 | else |
825 | SourceRegions.emplace_back(I.getCounter(), Loc, |
826 | getEndOfFileOrMacro(Loc)); |
827 | } |
828 | Loc = getIncludeOrExpansionLoc(Loc); |
829 | } |
830 | I.setStartLoc(getPreciseTokenLocEnd(Loc)); |
831 | } |
832 | |
833 | if (ParentCounter) { |
834 | |
835 | |
836 | |
837 | SourceLocation Loc = MostRecentLocation; |
838 | while (isNestedIn(Loc, ParentFile)) { |
839 | SourceLocation FileStart = getStartOfFileOrMacro(Loc); |
840 | if (StartLocs.insert(FileStart).second) { |
841 | SourceRegions.emplace_back(*ParentCounter, FileStart, |
842 | getEndOfFileOrMacro(Loc)); |
843 | assert(SpellingRegion(SM, SourceRegions.back()).isInSourceOrder()); |
844 | } |
845 | Loc = getIncludeOrExpansionLoc(Loc); |
846 | } |
847 | } |
848 | |
849 | MostRecentLocation = NewLoc; |
850 | } |
851 | |
852 | |
853 | void extendRegion(const Stmt *S) { |
854 | SourceMappingRegion &Region = getRegion(); |
855 | SourceLocation StartLoc = getStart(S); |
856 | |
857 | handleFileExit(StartLoc); |
858 | if (!Region.hasStartLoc()) |
859 | Region.setStartLoc(StartLoc); |
860 | } |
861 | |
862 | |
863 | void terminateRegion(const Stmt *S) { |
864 | extendRegion(S); |
865 | SourceMappingRegion &Region = getRegion(); |
866 | SourceLocation EndLoc = getEnd(S); |
867 | if (!Region.hasEndLoc()) |
868 | Region.setEndLoc(EndLoc); |
869 | pushRegion(Counter::getZero()); |
870 | HasTerminateStmt = true; |
871 | } |
872 | |
873 | |
874 | Optional<SourceRange> findGapAreaBetween(SourceLocation AfterLoc, |
875 | SourceLocation BeforeLoc) { |
876 | |
877 | |
878 | if (AfterLoc.isMacroID()) { |
879 | FileID FID = SM.getFileID(AfterLoc); |
880 | const SrcMgr::ExpansionInfo *EI = &SM.getSLocEntry(FID).getExpansion(); |
881 | if (EI->isFunctionMacroExpansion()) |
882 | AfterLoc = EI->getExpansionLocEnd(); |
883 | } |
884 | |
885 | size_t StartDepth = locationDepth(AfterLoc); |
886 | size_t EndDepth = locationDepth(BeforeLoc); |
887 | while (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc)) { |
888 | bool UnnestStart = StartDepth >= EndDepth; |
889 | bool UnnestEnd = EndDepth >= StartDepth; |
890 | if (UnnestEnd) { |
891 | assert(SM.isWrittenInSameFile(getStartOfFileOrMacro(BeforeLoc), |
892 | BeforeLoc)); |
893 | |
894 | BeforeLoc = getIncludeOrExpansionLoc(BeforeLoc); |
895 | assert(BeforeLoc.isValid()); |
896 | EndDepth--; |
897 | } |
898 | if (UnnestStart) { |
899 | assert(SM.isWrittenInSameFile(AfterLoc, |
900 | getEndOfFileOrMacro(AfterLoc))); |
901 | |
902 | AfterLoc = getIncludeOrExpansionLoc(AfterLoc); |
903 | assert(AfterLoc.isValid()); |
904 | AfterLoc = getPreciseTokenLocEnd(AfterLoc); |
905 | assert(AfterLoc.isValid()); |
906 | StartDepth--; |
907 | } |
908 | } |
909 | AfterLoc = getPreciseTokenLocEnd(AfterLoc); |
910 | |
911 | |
912 | if (AfterLoc.isMacroID() || BeforeLoc.isMacroID()) |
913 | return None; |
914 | if (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc) || |
915 | !SpellingRegion(SM, AfterLoc, BeforeLoc).isInSourceOrder()) |
916 | return None; |
917 | return {{AfterLoc, BeforeLoc}}; |
918 | } |
919 | |
920 | |
921 | void fillGapAreaWithCount(SourceLocation StartLoc, SourceLocation EndLoc, |
922 | Counter Count) { |
923 | if (StartLoc == EndLoc) |
924 | return; |
925 | assert(SpellingRegion(SM, StartLoc, EndLoc).isInSourceOrder()); |
926 | handleFileExit(StartLoc); |
927 | size_t Index = pushRegion(Count, StartLoc, EndLoc); |
928 | getRegion().setGap(true); |
929 | handleFileExit(EndLoc); |
930 | popRegions(Index); |
931 | } |
932 | |
933 | |
934 | struct BreakContinue { |
935 | Counter BreakCount; |
936 | Counter ContinueCount; |
937 | }; |
938 | SmallVector<BreakContinue, 8> BreakContinueStack; |
939 | |
940 | CounterCoverageMappingBuilder( |
941 | CoverageMappingModuleGen &CVM, |
942 | llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM, |
943 | const LangOptions &LangOpts) |
944 | : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap) {} |
945 | |
946 | |
947 | void write(llvm::raw_ostream &OS) { |
948 | llvm::SmallVector<unsigned, 8> VirtualFileMapping; |
949 | gatherFileIDs(VirtualFileMapping); |
950 | SourceRegionFilter Filter = emitExpansionRegions(); |
951 | emitSourceRegions(Filter); |
952 | gatherSkippedRegions(); |
953 | |
954 | if (MappingRegions.empty()) |
955 | return; |
956 | |
957 | CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(), |
958 | MappingRegions); |
959 | Writer.write(OS); |
960 | } |
961 | |
962 | void VisitStmt(const Stmt *S) { |
963 | if (S->getBeginLoc().isValid()) |
964 | extendRegion(S); |
965 | const Stmt *LastStmt = nullptr; |
966 | bool SaveTerminateStmt = HasTerminateStmt; |
967 | HasTerminateStmt = false; |
968 | GapRegionCounter = Counter::getZero(); |
969 | for (const Stmt *Child : S->children()) |
970 | if (Child) { |
971 | |
972 | |
973 | |
974 | if (LastStmt && HasTerminateStmt && !dyn_cast<AttributedStmt>(Child)) { |
975 | auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child)); |
976 | if (Gap) |
977 | fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), |
978 | GapRegionCounter); |
979 | SaveTerminateStmt = true; |
980 | HasTerminateStmt = false; |
981 | } |
982 | this->Visit(Child); |
983 | LastStmt = Child; |
984 | } |
985 | if (SaveTerminateStmt) |
986 | HasTerminateStmt = true; |
987 | handleFileExit(getEnd(S)); |
988 | } |
989 | |
990 | void VisitDecl(const Decl *D) { |
991 | Stmt *Body = D->getBody(); |
| |
992 | |
993 | |
994 | if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body)))) |
| 3 | | Assuming pointer value is null | |
|
| |
| |
995 | return; |
996 | |
997 | |
998 | |
999 | |
1000 | |
1001 | bool Defaulted = false; |
1002 | if (auto *Method = dyn_cast<CXXMethodDecl>(D)) |
| 6 | | Assuming 'D' is not a 'CXXMethodDecl' | |
|
| |
1003 | Defaulted = Method->isDefaulted(); |
1004 | |
1005 | propagateCounts(getRegionCounter(Body), Body, |
| 8 | | Passing null pointer value via 2nd parameter 'S' | |
|
| 9 | | Calling 'CounterCoverageMappingBuilder::propagateCounts' | |
|
1006 | !Defaulted); |
1007 | assert(RegionStack.empty() && "Regions entered but never exited"); |
1008 | } |
1009 | |
1010 | void VisitReturnStmt(const ReturnStmt *S) { |
1011 | extendRegion(S); |
1012 | if (S->getRetValue()) |
1013 | Visit(S->getRetValue()); |
1014 | terminateRegion(S); |
1015 | } |
1016 | |
1017 | void VisitCoroutineBodyStmt(const CoroutineBodyStmt *S) { |
1018 | extendRegion(S); |
1019 | Visit(S->getBody()); |
1020 | } |
1021 | |
1022 | void VisitCoreturnStmt(const CoreturnStmt *S) { |
1023 | extendRegion(S); |
1024 | if (S->getOperand()) |
1025 | Visit(S->getOperand()); |
1026 | terminateRegion(S); |
1027 | } |
1028 | |
1029 | void VisitCXXThrowExpr(const CXXThrowExpr *E) { |
1030 | extendRegion(E); |
1031 | if (E->getSubExpr()) |
1032 | Visit(E->getSubExpr()); |
1033 | terminateRegion(E); |
1034 | } |
1035 | |
1036 | void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); } |
1037 | |
1038 | void VisitLabelStmt(const LabelStmt *S) { |
1039 | Counter LabelCount = getRegionCounter(S); |
1040 | SourceLocation Start = getStart(S); |
1041 | |
1042 | handleFileExit(Start); |
1043 | pushRegion(LabelCount, Start); |
1044 | Visit(S->getSubStmt()); |
1045 | } |
1046 | |
1047 | void VisitBreakStmt(const BreakStmt *S) { |
1048 | assert(!BreakContinueStack.empty() && "break not in a loop or switch!"); |
1049 | BreakContinueStack.back().BreakCount = addCounters( |
1050 | BreakContinueStack.back().BreakCount, getRegion().getCounter()); |
1051 | |
1052 | |
1053 | terminateRegion(S); |
1054 | } |
1055 | |
1056 | void VisitContinueStmt(const ContinueStmt *S) { |
1057 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
1058 | BreakContinueStack.back().ContinueCount = addCounters( |
1059 | BreakContinueStack.back().ContinueCount, getRegion().getCounter()); |
1060 | terminateRegion(S); |
1061 | } |
1062 | |
1063 | void VisitCallExpr(const CallExpr *E) { |
1064 | VisitStmt(E); |
1065 | |
1066 | |
1067 | |
1068 | QualType CalleeType = E->getCallee()->getType(); |
1069 | if (getFunctionExtInfo(*CalleeType).getNoReturn()) |
1070 | terminateRegion(E); |
1071 | } |
1072 | |
1073 | void VisitWhileStmt(const WhileStmt *S) { |
1074 | extendRegion(S); |
1075 | |
1076 | Counter ParentCount = getRegion().getCounter(); |
1077 | Counter BodyCount = getRegionCounter(S); |
1078 | |
1079 | |
1080 | BreakContinueStack.push_back(BreakContinue()); |
1081 | extendRegion(S->getBody()); |
1082 | Counter BackedgeCount = propagateCounts(BodyCount, S->getBody()); |
1083 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
1084 | |
1085 | bool BodyHasTerminateStmt = HasTerminateStmt; |
1086 | HasTerminateStmt = false; |
1087 | |
1088 | |
1089 | Counter CondCount = |
1090 | addCounters(ParentCount, BackedgeCount, BC.ContinueCount); |
1091 | propagateCounts(CondCount, S->getCond()); |
1092 | adjustForOutOfOrderTraversal(getEnd(S)); |
1093 | |
1094 | |
1095 | auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody())); |
1096 | if (Gap) |
1097 | fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount); |
1098 | |
1099 | Counter OutCount = |
1100 | addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount)); |
1101 | if (OutCount != ParentCount) { |
1102 | pushRegion(OutCount); |
1103 | GapRegionCounter = OutCount; |
1104 | if (BodyHasTerminateStmt) |
1105 | HasTerminateStmt = true; |
1106 | } |
1107 | |
1108 | |
1109 | createBranchRegion(S->getCond(), BodyCount, |
1110 | subtractCounters(CondCount, BodyCount)); |
1111 | } |
1112 | |
1113 | void VisitDoStmt(const DoStmt *S) { |
1114 | extendRegion(S); |
1115 | |
1116 | Counter ParentCount = getRegion().getCounter(); |
1117 | Counter BodyCount = getRegionCounter(S); |
1118 | |
1119 | BreakContinueStack.push_back(BreakContinue()); |
1120 | extendRegion(S->getBody()); |
1121 | Counter BackedgeCount = |
1122 | propagateCounts(addCounters(ParentCount, BodyCount), S->getBody()); |
1123 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
1124 | |
1125 | bool BodyHasTerminateStmt = HasTerminateStmt; |
1126 | HasTerminateStmt = false; |
1127 | |
1128 | Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount); |
1129 | propagateCounts(CondCount, S->getCond()); |
1130 | |
1131 | Counter OutCount = |
1132 | addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount)); |
1133 | if (OutCount != ParentCount) { |
1134 | pushRegion(OutCount); |
1135 | GapRegionCounter = OutCount; |
1136 | } |
1137 | |
1138 | |
1139 | createBranchRegion(S->getCond(), BodyCount, |
1140 | subtractCounters(CondCount, BodyCount)); |
1141 | |
1142 | if (BodyHasTerminateStmt) |
1143 | HasTerminateStmt = true; |
1144 | } |
1145 | |
1146 | void VisitForStmt(const ForStmt *S) { |
1147 | extendRegion(S); |
1148 | if (S->getInit()) |
1149 | Visit(S->getInit()); |
1150 | |
1151 | Counter ParentCount = getRegion().getCounter(); |
1152 | Counter BodyCount = getRegionCounter(S); |
1153 | |
1154 | |
1155 | if (S->getInc()) |
1156 | BreakContinueStack.emplace_back(); |
1157 | |
1158 | |
1159 | BreakContinueStack.emplace_back(); |
1160 | extendRegion(S->getBody()); |
1161 | Counter BackedgeCount = propagateCounts(BodyCount, S->getBody()); |
1162 | BreakContinue BodyBC = BreakContinueStack.pop_back_val(); |
1163 | |
1164 | bool BodyHasTerminateStmt = HasTerminateStmt; |
1165 | HasTerminateStmt = false; |
1166 | |
1167 | |
1168 | |
1169 | BreakContinue IncrementBC; |
1170 | if (const Stmt *Inc = S->getInc()) { |
1171 | propagateCounts(addCounters(BackedgeCount, BodyBC.ContinueCount), Inc); |
1172 | IncrementBC = BreakContinueStack.pop_back_val(); |
1173 | } |
1174 | |
1175 | |
1176 | Counter CondCount = addCounters( |
1177 | addCounters(ParentCount, BackedgeCount, BodyBC.ContinueCount), |
1178 | IncrementBC.ContinueCount); |
1179 | if (const Expr *Cond = S->getCond()) { |
1180 | propagateCounts(CondCount, Cond); |
1181 | adjustForOutOfOrderTraversal(getEnd(S)); |
1182 | } |
1183 | |
1184 | |
1185 | auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody())); |
1186 | if (Gap) |
1187 | fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount); |
1188 | |
1189 | Counter OutCount = addCounters(BodyBC.BreakCount, IncrementBC.BreakCount, |
1190 | subtractCounters(CondCount, BodyCount)); |
1191 | if (OutCount != ParentCount) { |
1192 | pushRegion(OutCount); |
1193 | GapRegionCounter = OutCount; |
1194 | if (BodyHasTerminateStmt) |
1195 | HasTerminateStmt = true; |
1196 | } |
1197 | |
1198 | |
1199 | createBranchRegion(S->getCond(), BodyCount, |
1200 | subtractCounters(CondCount, BodyCount)); |
1201 | } |
1202 | |
1203 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
1204 | extendRegion(S); |
1205 | if (S->getInit()) |
1206 | Visit(S->getInit()); |
1207 | Visit(S->getLoopVarStmt()); |
1208 | Visit(S->getRangeStmt()); |
1209 | |
1210 | Counter ParentCount = getRegion().getCounter(); |
1211 | Counter BodyCount = getRegionCounter(S); |
1212 | |
1213 | BreakContinueStack.push_back(BreakContinue()); |
1214 | extendRegion(S->getBody()); |
1215 | Counter BackedgeCount = propagateCounts(BodyCount, S->getBody()); |
1216 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
1217 | |
1218 | bool BodyHasTerminateStmt = HasTerminateStmt; |
1219 | HasTerminateStmt = false; |
1220 | |
1221 | |
1222 | auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody())); |
1223 | if (Gap) |
1224 | fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount); |
1225 | |
1226 | Counter LoopCount = |
1227 | addCounters(ParentCount, BackedgeCount, BC.ContinueCount); |
1228 | Counter OutCount = |
1229 | addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount)); |
1230 | if (OutCount != ParentCount) { |
1231 | pushRegion(OutCount); |
1232 | GapRegionCounter = OutCount; |
1233 | if (BodyHasTerminateStmt) |
1234 | HasTerminateStmt = true; |
1235 | } |
1236 | |
1237 | |
1238 | createBranchRegion(S->getCond(), BodyCount, |
1239 | subtractCounters(LoopCount, BodyCount)); |
1240 | } |
1241 | |
1242 | void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { |
1243 | extendRegion(S); |
1244 | Visit(S->getElement()); |
1245 | |
1246 | Counter ParentCount = getRegion().getCounter(); |
1247 | Counter BodyCount = getRegionCounter(S); |
1248 | |
1249 | BreakContinueStack.push_back(BreakContinue()); |
1250 | extendRegion(S->getBody()); |
1251 | Counter BackedgeCount = propagateCounts(BodyCount, S->getBody()); |
1252 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
1253 | |
1254 | |
1255 | auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getBody())); |
1256 | if (Gap) |
1257 | fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount); |
1258 | |
1259 | Counter LoopCount = |
1260 | addCounters(ParentCount, BackedgeCount, BC.ContinueCount); |
1261 | Counter OutCount = |
1262 | addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount)); |
1263 | if (OutCount != ParentCount) { |
1264 | pushRegion(OutCount); |
1265 | GapRegionCounter = OutCount; |
1266 | } |
1267 | } |
1268 | |
1269 | void VisitSwitchStmt(const SwitchStmt *S) { |
1270 | extendRegion(S); |
1271 | if (S->getInit()) |
1272 | Visit(S->getInit()); |
1273 | Visit(S->getCond()); |
1274 | |
1275 | BreakContinueStack.push_back(BreakContinue()); |
1276 | |
1277 | const Stmt *Body = S->getBody(); |
1278 | extendRegion(Body); |
1279 | if (const auto *CS = dyn_cast<CompoundStmt>(Body)) { |
1280 | if (!CS->body_empty()) { |
1281 | |
1282 | |
1283 | |
1284 | size_t Index = pushRegion(Counter::getZero(), getStart(CS)); |
1285 | getRegion().setGap(true); |
1286 | Visit(Body); |
1287 | |
1288 | |
1289 | for (size_t i = RegionStack.size(); i != Index; --i) { |
1290 | if (!RegionStack[i - 1].hasEndLoc()) |
1291 | RegionStack[i - 1].setEndLoc(getEnd(CS->body_back())); |
1292 | } |
1293 | |
1294 | popRegions(Index); |
1295 | } |
1296 | } else |
1297 | propagateCounts(Counter::getZero(), Body); |
1298 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
1299 | |
1300 | if (!BreakContinueStack.empty()) |
1301 | BreakContinueStack.back().ContinueCount = addCounters( |
1302 | BreakContinueStack.back().ContinueCount, BC.ContinueCount); |
1303 | |
1304 | Counter ParentCount = getRegion().getCounter(); |
1305 | Counter ExitCount = getRegionCounter(S); |
1306 | SourceLocation ExitLoc = getEnd(S); |
1307 | pushRegion(ExitCount); |
1308 | GapRegionCounter = ExitCount; |
1309 | |
1310 | |
1311 | |
1312 | MostRecentLocation = getStart(S); |
1313 | handleFileExit(ExitLoc); |
1314 | |
1315 | |
1316 | |
1317 | Counter CaseCountSum; |
1318 | bool HasDefaultCase = false; |
1319 | const SwitchCase *Case = S->getSwitchCaseList(); |
1320 | for (; Case; Case = Case->getNextSwitchCase()) { |
1321 | HasDefaultCase = HasDefaultCase || isa<DefaultStmt>(Case); |
1322 | CaseCountSum = addCounters(CaseCountSum, getRegionCounter(Case)); |
1323 | createSwitchCaseRegion( |
1324 | Case, getRegionCounter(Case), |
1325 | subtractCounters(ParentCount, getRegionCounter(Case))); |
1326 | } |
1327 | |
1328 | |
1329 | |
1330 | |
1331 | if (!HasDefaultCase) { |
1332 | Counter DefaultTrue = subtractCounters(ParentCount, CaseCountSum); |
1333 | Counter DefaultFalse = subtractCounters(ParentCount, DefaultTrue); |
1334 | createBranchRegion(S->getCond(), DefaultTrue, DefaultFalse); |
1335 | } |
1336 | } |
1337 | |
1338 | void VisitSwitchCase(const SwitchCase *S) { |
1339 | extendRegion(S); |
1340 | |
1341 | SourceMappingRegion &Parent = getRegion(); |
1342 | |
1343 | Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S)); |
1344 | |
1345 | |
1346 | if (Parent.hasStartLoc() && Parent.getBeginLoc() == getStart(S)) |
1347 | Parent.setCounter(Count); |
1348 | else |
1349 | pushRegion(Count, getStart(S)); |
1350 | |
1351 | GapRegionCounter = Count; |
1352 | |
1353 | if (const auto *CS = dyn_cast<CaseStmt>(S)) { |
1354 | Visit(CS->getLHS()); |
1355 | if (const Expr *RHS = CS->getRHS()) |
1356 | Visit(RHS); |
1357 | } |
1358 | Visit(S->getSubStmt()); |
1359 | } |
1360 | |
1361 | void VisitIfStmt(const IfStmt *S) { |
1362 | extendRegion(S); |
1363 | if (S->getInit()) |
1364 | Visit(S->getInit()); |
1365 | |
1366 | |
1367 | |
1368 | extendRegion(S->getCond()); |
1369 | |
1370 | Counter ParentCount = getRegion().getCounter(); |
1371 | Counter ThenCount = getRegionCounter(S); |
1372 | |
1373 | |
1374 | |
1375 | propagateCounts(ParentCount, S->getCond()); |
1376 | |
1377 | |
1378 | auto Gap = findGapAreaBetween(S->getRParenLoc(), getStart(S->getThen())); |
1379 | if (Gap) |
1380 | fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ThenCount); |
1381 | |
1382 | extendRegion(S->getThen()); |
1383 | Counter OutCount = propagateCounts(ThenCount, S->getThen()); |
1384 | |
1385 | Counter ElseCount = subtractCounters(ParentCount, ThenCount); |
1386 | if (const Stmt *Else = S->getElse()) { |
1387 | bool ThenHasTerminateStmt = HasTerminateStmt; |
1388 | HasTerminateStmt = false; |
1389 | |
1390 | |
1391 | Gap = findGapAreaBetween(getEnd(S->getThen()), getStart(Else)); |
1392 | if (Gap) |
1393 | fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ElseCount); |
1394 | extendRegion(Else); |
1395 | OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else)); |
1396 | |
1397 | if (ThenHasTerminateStmt) |
1398 | HasTerminateStmt = true; |
1399 | } else |
1400 | OutCount = addCounters(OutCount, ElseCount); |
1401 | |
1402 | if (OutCount != ParentCount) { |
1403 | pushRegion(OutCount); |
1404 | GapRegionCounter = OutCount; |
1405 | } |
1406 | |
1407 | |
1408 | createBranchRegion(S->getCond(), ThenCount, |
1409 | subtractCounters(ParentCount, ThenCount)); |
1410 | } |
1411 | |
1412 | void VisitCXXTryStmt(const CXXTryStmt *S) { |
1413 | extendRegion(S); |
1414 | |
1415 | extendRegion(S->getTryBlock()); |
1416 | |
1417 | Counter ParentCount = getRegion().getCounter(); |
1418 | propagateCounts(ParentCount, S->getTryBlock()); |
1419 | |
1420 | for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I) |
1421 | Visit(S->getHandler(I)); |
1422 | |
1423 | Counter ExitCount = getRegionCounter(S); |
1424 | pushRegion(ExitCount); |
1425 | } |
1426 | |
1427 | void VisitCXXCatchStmt(const CXXCatchStmt *S) { |
1428 | propagateCounts(getRegionCounter(S), S->getHandlerBlock()); |
1429 | } |
1430 | |
1431 | void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { |
1432 | extendRegion(E); |
1433 | |
1434 | Counter ParentCount = getRegion().getCounter(); |
1435 | Counter TrueCount = getRegionCounter(E); |
1436 | |
1437 | propagateCounts(ParentCount, E->getCond()); |
1438 | |
1439 | if (!isa<BinaryConditionalOperator>(E)) { |
1440 | |
1441 | auto Gap = |
1442 | findGapAreaBetween(E->getQuestionLoc(), getStart(E->getTrueExpr())); |
1443 | if (Gap) |
1444 | fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), TrueCount); |
1445 | |
1446 | extendRegion(E->getTrueExpr()); |
1447 | propagateCounts(TrueCount, E->getTrueExpr()); |
1448 | } |
1449 | |
1450 | extendRegion(E->getFalseExpr()); |
1451 | propagateCounts(subtractCounters(ParentCount, TrueCount), |
1452 | E->getFalseExpr()); |
1453 | |
1454 | |
1455 | createBranchRegion(E->getCond(), TrueCount, |
1456 | subtractCounters(ParentCount, TrueCount)); |
1457 | } |
1458 | |
1459 | void VisitBinLAnd(const BinaryOperator *E) { |
1460 | extendRegion(E->getLHS()); |
1461 | propagateCounts(getRegion().getCounter(), E->getLHS()); |
1462 | handleFileExit(getEnd(E->getLHS())); |
1463 | |
1464 | |
1465 | extendRegion(E->getRHS()); |
1466 | propagateCounts(getRegionCounter(E), E->getRHS()); |
1467 | |
1468 | |
1469 | Counter RHSExecCnt = getRegionCounter(E); |
1470 | |
1471 | |
1472 | Counter RHSTrueCnt = getRegionCounter(E->getRHS()); |
1473 | |
1474 | |
1475 | Counter ParentCnt = getRegion().getCounter(); |
1476 | |
1477 | |
1478 | createBranchRegion(E->getLHS(), RHSExecCnt, |
1479 | subtractCounters(ParentCnt, RHSExecCnt)); |
1480 | |
1481 | |
1482 | createBranchRegion(E->getRHS(), RHSTrueCnt, |
1483 | subtractCounters(RHSExecCnt, RHSTrueCnt)); |
1484 | } |
1485 | |
1486 | void VisitBinLOr(const BinaryOperator *E) { |
1487 | extendRegion(E->getLHS()); |
1488 | propagateCounts(getRegion().getCounter(), E->getLHS()); |
1489 | handleFileExit(getEnd(E->getLHS())); |
1490 | |
1491 | |
1492 | extendRegion(E->getRHS()); |
1493 | propagateCounts(getRegionCounter(E), E->getRHS()); |
1494 | |
1495 | |
1496 | Counter RHSExecCnt = getRegionCounter(E); |
1497 | |
1498 | |
1499 | Counter RHSFalseCnt = getRegionCounter(E->getRHS()); |
1500 | |
1501 | |
1502 | Counter ParentCnt = getRegion().getCounter(); |
1503 | |
1504 | |
1505 | createBranchRegion(E->getLHS(), subtractCounters(ParentCnt, RHSExecCnt), |
1506 | RHSExecCnt); |
1507 | |
1508 | |
1509 | createBranchRegion(E->getRHS(), subtractCounters(RHSExecCnt, RHSFalseCnt), |
1510 | RHSFalseCnt); |
1511 | } |
1512 | |
1513 | void VisitLambdaExpr(const LambdaExpr *LE) { |
1514 | |
1515 | |
1516 | } |
1517 | }; |
1518 | |
1519 | } |
1520 | |
1521 | static void dump(llvm::raw_ostream &OS, StringRef FunctionName, |
1522 | ArrayRef<CounterExpression> Expressions, |
1523 | ArrayRef<CounterMappingRegion> Regions) { |
1524 | OS << FunctionName << ":\n"; |
1525 | CounterMappingContext Ctx(Expressions); |
1526 | for (const auto &R : Regions) { |
1527 | OS.indent(2); |
1528 | switch (R.Kind) { |
1529 | case CounterMappingRegion::CodeRegion: |
1530 | break; |
1531 | case CounterMappingRegion::ExpansionRegion: |
1532 | OS << "Expansion,"; |
1533 | break; |
1534 | case CounterMappingRegion::SkippedRegion: |
1535 | OS << "Skipped,"; |
1536 | break; |
1537 | case CounterMappingRegion::GapRegion: |
1538 | OS << "Gap,"; |
1539 | break; |
1540 | case CounterMappingRegion::BranchRegion: |
1541 | OS << "Branch,"; |
1542 | break; |
1543 | } |
1544 | |
1545 | OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart |
1546 | << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = "; |
1547 | Ctx.dump(R.Count, OS); |
1548 | |
1549 | if (R.Kind == CounterMappingRegion::BranchRegion) { |
1550 | OS << ", "; |
1551 | Ctx.dump(R.FalseCount, OS); |
1552 | } |
1553 | |
1554 | if (R.Kind == CounterMappingRegion::ExpansionRegion) |
1555 | OS << " (Expanded file = " << R.ExpandedFileID << ")"; |
1556 | OS << "\n"; |
1557 | } |
1558 | } |
1559 | |
1560 | CoverageMappingModuleGen::CoverageMappingModuleGen( |
1561 | CodeGenModule &CGM, CoverageSourceInfo &SourceInfo) |
1562 | : CGM(CGM), SourceInfo(SourceInfo) { |
1563 | CoveragePrefixMap = CGM.getCodeGenOpts().CoveragePrefixMap; |
1564 | } |
1565 | |
1566 | std::string CoverageMappingModuleGen::getCurrentDirname() { |
1567 | if (!CGM.getCodeGenOpts().CoverageCompilationDir.empty()) |
1568 | return CGM.getCodeGenOpts().CoverageCompilationDir; |
1569 | |
1570 | SmallString<256> CWD; |
1571 | llvm::sys::fs::current_path(CWD); |
1572 | return CWD.str().str(); |
1573 | } |
1574 | |
1575 | std::string CoverageMappingModuleGen::normalizeFilename(StringRef Filename) { |
1576 | llvm::SmallString<256> Path(Filename); |
1577 | llvm::sys::path::remove_dots(Path, true); |
1578 | for (const auto &Entry : CoveragePrefixMap) { |
1579 | if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second)) |
1580 | break; |
1581 | } |
1582 | return Path.str().str(); |
1583 | } |
1584 | |
1585 | static std::string getInstrProfSection(const CodeGenModule &CGM, |
1586 | llvm::InstrProfSectKind SK) { |
1587 | return llvm::getInstrProfSectionName( |
1588 | SK, CGM.getContext().getTargetInfo().getTriple().getObjectFormat()); |
1589 | } |
1590 | |
1591 | void CoverageMappingModuleGen::emitFunctionMappingRecord( |
1592 | const FunctionInfo &Info, uint64_t FilenamesRef) { |
1593 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
1594 | |
1595 | |
1596 | std::string FuncRecordName = "__covrec_" + llvm::utohexstr(Info.NameHash); |
1597 | |
1598 | |
1599 | |
1600 | |
1601 | |
1602 | if (Info.IsUsed) |
1603 | FuncRecordName += "u"; |
1604 | |
1605 | |
1606 | const uint64_t NameHash = Info.NameHash; |
1607 | const uint64_t FuncHash = Info.FuncHash; |
1608 | const std::string &CoverageMapping = Info.CoverageMapping; |
1609 | #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType, |
1610 | llvm::Type *FunctionRecordTypes[] = { |
1611 | #include "llvm/ProfileData/InstrProfData.inc" |
1612 | }; |
1613 | auto *FunctionRecordTy = |
1614 | llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes), |
1615 | true); |
1616 | |
1617 | |
1618 | #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init, |
1619 | llvm::Constant *FunctionRecordVals[] = { |
1620 | #include "llvm/ProfileData/InstrProfData.inc" |
1621 | }; |
1622 | auto *FuncRecordConstant = llvm::ConstantStruct::get( |
1623 | FunctionRecordTy, makeArrayRef(FunctionRecordVals)); |
1624 | |
1625 | |
1626 | auto *FuncRecord = new llvm::GlobalVariable( |
1627 | CGM.getModule(), FunctionRecordTy, true, |
1628 | llvm::GlobalValue::LinkOnceODRLinkage, FuncRecordConstant, |
1629 | FuncRecordName); |
1630 | FuncRecord->setVisibility(llvm::GlobalValue::HiddenVisibility); |
1631 | FuncRecord->setSection(getInstrProfSection(CGM, llvm::IPSK_covfun)); |
1632 | FuncRecord->setAlignment(llvm::Align(8)); |
1633 | if (CGM.supportsCOMDAT()) |
1634 | FuncRecord->setComdat(CGM.getModule().getOrInsertComdat(FuncRecordName)); |
1635 | |
1636 | |
1637 | CGM.addUsedGlobal(FuncRecord); |
1638 | } |
1639 | |
1640 | void CoverageMappingModuleGen::addFunctionMappingRecord( |
1641 | llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash, |
1642 | const std::string &CoverageMapping, bool IsUsed) { |
1643 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
1644 | const uint64_t NameHash = llvm::IndexedInstrProf::ComputeHash(NameValue); |
1645 | FunctionRecords.push_back({NameHash, FuncHash, CoverageMapping, IsUsed}); |
1646 | |
1647 | if (!IsUsed) |
1648 | FunctionNames.push_back( |
1649 | llvm::ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx))); |
1650 | |
1651 | if (CGM.getCodeGenOpts().DumpCoverageMapping) { |
1652 | |
1653 | |
1654 | |
1655 | |
1656 | |
1657 | llvm::SmallVector<std::string, 16> FilenameStrs; |
1658 | std::vector<StringRef> Filenames; |
1659 | std::vector<CounterExpression> Expressions; |
1660 | std::vector<CounterMappingRegion> Regions; |
1661 | FilenameStrs.resize(FileEntries.size() + 1); |
1662 | FilenameStrs[0] = normalizeFilename(getCurrentDirname()); |
1663 | for (const auto &Entry : FileEntries) { |
1664 | auto I = Entry.second; |
1665 | FilenameStrs[I] = normalizeFilename(Entry.first->getName()); |
1666 | } |
1667 | ArrayRef<std::string> FilenameRefs = llvm::makeArrayRef(FilenameStrs); |
1668 | RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames, |
1669 | Expressions, Regions); |
1670 | if (Reader.read()) |
1671 | return; |
1672 | dump(llvm::outs(), NameValue, Expressions, Regions); |
1673 | } |
1674 | } |
1675 | |
1676 | void CoverageMappingModuleGen::emit() { |
1677 | if (FunctionRecords.empty()) |
1678 | return; |
1679 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
1680 | auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
1681 | |
1682 | |
1683 | llvm::SmallVector<std::string, 16> FilenameStrs; |
1684 | FilenameStrs.resize(FileEntries.size() + 1); |
1685 | |
1686 | FilenameStrs[0] = normalizeFilename(getCurrentDirname()); |
1687 | for (const auto &Entry : FileEntries) { |
1688 | auto I = Entry.second; |
1689 | FilenameStrs[I] = normalizeFilename(Entry.first->getName()); |
1690 | } |
1691 | |
1692 | std::string Filenames; |
1693 | { |
1694 | llvm::raw_string_ostream OS(Filenames); |
1695 | CoverageFilenamesSectionWriter(FilenameStrs).write(OS); |
1696 | } |
1697 | auto *FilenamesVal = |
1698 | llvm::ConstantDataArray::getString(Ctx, Filenames, false); |
1699 | const int64_t FilenamesRef = llvm::IndexedInstrProf::ComputeHash(Filenames); |
1700 | |
1701 | |
1702 | for (const FunctionInfo &Info : FunctionRecords) |
1703 | emitFunctionMappingRecord(Info, FilenamesRef); |
1704 | |
1705 | const unsigned NRecords = 0; |
1706 | const size_t FilenamesSize = Filenames.size(); |
1707 | const unsigned CoverageMappingSize = 0; |
1708 | llvm::Type *CovDataHeaderTypes[] = { |
1709 | #define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType, |
1710 | #include "llvm/ProfileData/InstrProfData.inc" |
1711 | }; |
1712 | auto CovDataHeaderTy = |
1713 | llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes)); |
1714 | llvm::Constant *CovDataHeaderVals[] = { |
1715 | #define COVMAP_HEADER(Type, LLVMType, Name, Init) Init, |
1716 | #include "llvm/ProfileData/InstrProfData.inc" |
1717 | }; |
1718 | auto CovDataHeaderVal = llvm::ConstantStruct::get( |
1719 | CovDataHeaderTy, makeArrayRef(CovDataHeaderVals)); |
1720 | |
1721 | |
1722 | llvm::Type *CovDataTypes[] = {CovDataHeaderTy, FilenamesVal->getType()}; |
1723 | auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes)); |
1724 | llvm::Constant *TUDataVals[] = {CovDataHeaderVal, FilenamesVal}; |
1725 | auto CovDataVal = |
1726 | llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals)); |
1727 | auto CovData = new llvm::GlobalVariable( |
1728 | CGM.getModule(), CovDataTy, true, llvm::GlobalValue::PrivateLinkage, |
1729 | CovDataVal, llvm::getCoverageMappingVarName()); |
1730 | |
1731 | CovData->setSection(getInstrProfSection(CGM, llvm::IPSK_covmap)); |
1732 | CovData->setAlignment(llvm::Align(8)); |
1733 | |
1734 | |
1735 | CGM.addUsedGlobal(CovData); |
1736 | |
1737 | if (!FunctionNames.empty()) { |
1738 | auto NamesArrTy = llvm::ArrayType::get(llvm::Type::getInt8PtrTy(Ctx), |
1739 | FunctionNames.size()); |
1740 | auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames); |
1741 | |
1742 | |
1743 | new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true, |
1744 | llvm::GlobalValue::InternalLinkage, NamesArrVal, |
1745 | llvm::getCoverageUnusedNamesVarName()); |
1746 | } |
1747 | } |
1748 | |
1749 | unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) { |
1750 | auto It = FileEntries.find(File); |
1751 | if (It != FileEntries.end()) |
1752 | return It->second; |
1753 | unsigned FileID = FileEntries.size() + 1; |
1754 | FileEntries.insert(std::make_pair(File, FileID)); |
1755 | return FileID; |
1756 | } |
1757 | |
1758 | void CoverageMappingGen::emitCounterMapping(const Decl *D, |
1759 | llvm::raw_ostream &OS) { |
1760 | assert(CounterMap); |
1761 | CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts); |
1762 | Walker.VisitDecl(D); |
| 1 | Calling 'CounterCoverageMappingBuilder::VisitDecl' | |
|
1763 | Walker.write(OS); |
1764 | } |
1765 | |
1766 | void CoverageMappingGen::emitEmptyMapping(const Decl *D, |
1767 | llvm::raw_ostream &OS) { |
1768 | EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts); |
1769 | Walker.VisitDecl(D); |
1770 | Walker.write(OS); |
1771 | } |