1//===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
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// The 'CodeCoverageTool' class implements a command line tool to analyze and
10// report coverage information using the profiling instrumentation and code
11// coverage mapping.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CoverageExporterJson.h"
16#include "CoverageExporterLcov.h"
17#include "CoverageFilters.h"
18#include "CoverageReport.h"
19#include "CoverageSummaryInfo.h"
20#include "CoverageViewOptions.h"
21#include "RenderingSupport.h"
22#include "SourceCoverageView.h"
23#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/Debuginfod/BuildIDFetcher.h"
26#include "llvm/Debuginfod/Debuginfod.h"
27#include "llvm/Debuginfod/HTTPClient.h"
28#include "llvm/Object/BuildID.h"
29#include "llvm/ProfileData/Coverage/CoverageMapping.h"
30#include "llvm/ProfileData/InstrProfReader.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/FileSystem.h"
33#include "llvm/Support/Format.h"
34#include "llvm/Support/MemoryBuffer.h"
35#include "llvm/Support/Path.h"
36#include "llvm/Support/Process.h"
37#include "llvm/Support/Program.h"
38#include "llvm/Support/ScopedPrinter.h"
39#include "llvm/Support/SpecialCaseList.h"
40#include "llvm/Support/ThreadPool.h"
41#include "llvm/Support/Threading.h"
42#include "llvm/Support/ToolOutputFile.h"
43#include "llvm/Support/VirtualFileSystem.h"
44#include "llvm/TargetParser/Triple.h"
45
46#include <functional>
47#include <map>
48#include <optional>
49#include <system_error>
50
51using namespace llvm;
52using namespace coverage;
53
54void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping,
55 const CoverageViewOptions &Options,
56 raw_ostream &OS);
57
58namespace {
59/// The implementation of the coverage tool.
60class CodeCoverageTool {
61public:
62 enum Command {
63 /// The show command.
64 Show,
65 /// The report command.
66 Report,
67 /// The export command.
68 Export
69 };
70
71 int run(Command Cmd, int argc, const char **argv);
72
73private:
74 /// Print the error message to the error output stream.
75 void error(const Twine &Message, StringRef Whence = "");
76
77 /// Print the warning message to the error output stream.
78 void warning(const Twine &Message, StringRef Whence = "");
79
80 /// Convert \p Path into an absolute path and append it to the list
81 /// of collected paths.
82 void addCollectedPath(const std::string &Path);
83
84 /// If \p Path is a regular file, collect the path. If it's a
85 /// directory, recursively collect all of the paths within the directory.
86 void collectPaths(const std::string &Path);
87
88 /// Check if the two given files are the same file.
89 bool isEquivalentFile(StringRef FilePath1, StringRef FilePath2);
90
91 /// Retrieve a file status with a cache.
92 std::optional<sys::fs::file_status> getFileStatus(StringRef FilePath);
93
94 /// Return a memory buffer for the given source file.
95 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
96
97 /// Create source views for the expansions of the view.
98 void attachExpansionSubViews(SourceCoverageView &View,
99 ArrayRef<ExpansionRecord> Expansions,
100 const CoverageMapping &Coverage);
101
102 /// Create source views for the branches of the view.
103 void attachBranchSubViews(SourceCoverageView &View,
104 ArrayRef<CountedRegion> Branches);
105
106 /// Create source views for the MCDC records.
107 void attachMCDCSubViews(SourceCoverageView &View,
108 ArrayRef<MCDCRecord> MCDCRecords);
109
110 /// Create the source view of a particular function.
111 std::unique_ptr<SourceCoverageView>
112 createFunctionView(const FunctionRecord &Function,
113 const CoverageMapping &Coverage);
114
115 /// Create the main source view of a particular source file.
116 std::unique_ptr<SourceCoverageView>
117 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
118
119 /// Load the coverage mapping data. Return nullptr if an error occurred.
120 std::unique_ptr<CoverageMapping> load();
121
122 /// Create a mapping from files in the Coverage data to local copies
123 /// (path-equivalence).
124 void remapPathNames(const CoverageMapping &Coverage);
125
126 /// Remove input source files which aren't mapped by \p Coverage.
127 void removeUnmappedInputs(const CoverageMapping &Coverage);
128
129 /// If a demangler is available, demangle all symbol names.
130 void demangleSymbols(const CoverageMapping &Coverage);
131
132 /// Write out a source file view to the filesystem.
133 void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
134 CoveragePrinter *Printer, bool ShowFilenames);
135
136 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
137
138 int doShow(int argc, const char **argv,
139 CommandLineParserType commandLineParser);
140
141 int doReport(int argc, const char **argv,
142 CommandLineParserType commandLineParser);
143
144 int doExport(int argc, const char **argv,
145 CommandLineParserType commandLineParser);
146
147 std::vector<StringRef> ObjectFilenames;
148 CoverageViewOptions ViewOpts;
149 CoverageFiltersMatchAll Filters;
150 CoverageFilters IgnoreFilenameFilters;
151
152 /// True if InputSourceFiles are provided.
153 bool HadSourceFiles = false;
154
155 /// The path to the indexed profile.
156 std::optional<std::string> PGOFilename;
157
158 /// A list of input source files.
159 std::vector<std::string> SourceFiles;
160
161 /// In -path-equivalence mode, this maps the absolute paths from the coverage
162 /// mapping data to the input source files.
163 StringMap<std::string> RemappedFilenames;
164
165 /// The coverage data path to be remapped from, and the source path to be
166 /// remapped to, when using -path-equivalence.
167 std::optional<std::vector<std::pair<std::string, std::string>>>
168 PathRemappings;
169
170 /// File status cache used when finding the same file.
171 StringMap<std::optional<sys::fs::file_status>> FileStatusCache;
172
173 /// The architecture the coverage mapping data targets.
174 std::vector<StringRef> CoverageArches;
175
176 /// A cache for demangled symbols.
177 DemangleCache DC;
178
179 /// A lock which guards printing to stderr.
180 std::mutex ErrsLock;
181
182 /// A container for input source file buffers.
183 std::mutex LoadedSourceFilesLock;
184 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
185 LoadedSourceFiles;
186
187 /// Allowlist from -name-allowlist to be used for filtering.
188 std::unique_ptr<SpecialCaseList> NameAllowlist;
189
190 std::unique_ptr<object::BuildIDFetcher> BIDFetcher;
191
192 bool CheckBinaryIDs;
193};
194}
195
196static std::string getErrorString(const Twine &Message, StringRef Whence,
197 bool Warning) {
198 std::string Str = (Warning ? "warning" : "error");
199 Str += ": ";
200 if (!Whence.empty())
201 Str += Whence.str() + ": ";
202 Str += Message.str() + "\n";
203 return Str;
204}
205
206void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
207 std::unique_lock<std::mutex> Guard{ErrsLock};
208 ViewOpts.colored_ostream(OS&: errs(), Color: raw_ostream::RED)
209 << getErrorString(Message, Whence, Warning: false);
210}
211
212void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
213 std::unique_lock<std::mutex> Guard{ErrsLock};
214 ViewOpts.colored_ostream(OS&: errs(), Color: raw_ostream::RED)
215 << getErrorString(Message, Whence, Warning: true);
216}
217
218void CodeCoverageTool::addCollectedPath(const std::string &Path) {
219 SmallString<128> EffectivePath(Path);
220 if (std::error_code EC = sys::fs::make_absolute(path&: EffectivePath)) {
221 error(Message: EC.message(), Whence: Path);
222 return;
223 }
224 sys::path::remove_dots(path&: EffectivePath, /*remove_dot_dot=*/true);
225 if (!IgnoreFilenameFilters.matchesFilename(Filename: EffectivePath))
226 SourceFiles.emplace_back(args: EffectivePath.str());
227 HadSourceFiles = !SourceFiles.empty();
228}
229
230void CodeCoverageTool::collectPaths(const std::string &Path) {
231 llvm::sys::fs::file_status Status;
232 llvm::sys::fs::status(path: Path, result&: Status);
233 if (!llvm::sys::fs::exists(status: Status)) {
234 if (PathRemappings)
235 addCollectedPath(Path);
236 else
237 warning(Message: "Source file doesn't exist, proceeded by ignoring it.", Whence: Path);
238 return;
239 }
240
241 if (llvm::sys::fs::is_regular_file(status: Status)) {
242 addCollectedPath(Path);
243 return;
244 }
245
246 if (llvm::sys::fs::is_directory(status: Status)) {
247 std::error_code EC;
248 for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
249 F != E; F.increment(ec&: EC)) {
250
251 auto Status = F->status();
252 if (!Status) {
253 warning(Message: Status.getError().message(), Whence: F->path());
254 continue;
255 }
256
257 if (Status->type() == llvm::sys::fs::file_type::regular_file)
258 addCollectedPath(Path: F->path());
259 }
260 }
261}
262
263std::optional<sys::fs::file_status>
264CodeCoverageTool::getFileStatus(StringRef FilePath) {
265 auto It = FileStatusCache.try_emplace(Key: FilePath);
266 auto &CachedStatus = It.first->getValue();
267 if (!It.second)
268 return CachedStatus;
269
270 sys::fs::file_status Status;
271 if (!sys::fs::status(path: FilePath, result&: Status))
272 CachedStatus = Status;
273 return CachedStatus;
274}
275
276bool CodeCoverageTool::isEquivalentFile(StringRef FilePath1,
277 StringRef FilePath2) {
278 auto Status1 = getFileStatus(FilePath: FilePath1);
279 auto Status2 = getFileStatus(FilePath: FilePath2);
280 return Status1 && Status2 && sys::fs::equivalent(A: *Status1, B: *Status2);
281}
282
283ErrorOr<const MemoryBuffer &>
284CodeCoverageTool::getSourceFile(StringRef SourceFile) {
285 // If we've remapped filenames, look up the real location for this file.
286 std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
287 if (!RemappedFilenames.empty()) {
288 auto Loc = RemappedFilenames.find(Key: SourceFile);
289 if (Loc != RemappedFilenames.end())
290 SourceFile = Loc->second;
291 }
292 for (const auto &Files : LoadedSourceFiles)
293 if (isEquivalentFile(FilePath1: SourceFile, FilePath2: Files.first))
294 return *Files.second;
295 auto Buffer = MemoryBuffer::getFile(Filename: SourceFile);
296 if (auto EC = Buffer.getError()) {
297 error(Message: EC.message(), Whence: SourceFile);
298 return EC;
299 }
300 LoadedSourceFiles.emplace_back(args: std::string(SourceFile),
301 args: std::move(Buffer.get()));
302 return *LoadedSourceFiles.back().second;
303}
304
305void CodeCoverageTool::attachExpansionSubViews(
306 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
307 const CoverageMapping &Coverage) {
308 if (!ViewOpts.ShowExpandedRegions)
309 return;
310 for (const auto &Expansion : Expansions) {
311 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
312 if (ExpansionCoverage.empty())
313 continue;
314 auto SourceBuffer = getSourceFile(SourceFile: ExpansionCoverage.getFilename());
315 if (!SourceBuffer)
316 continue;
317
318 auto SubViewBranches = ExpansionCoverage.getBranches();
319 auto SubViewExpansions = ExpansionCoverage.getExpansions();
320 auto SubView =
321 SourceCoverageView::create(SourceName: Expansion.Function.Name, File: SourceBuffer.get(),
322 Options: ViewOpts, CoverageInfo: std::move(ExpansionCoverage));
323 attachExpansionSubViews(View&: *SubView, Expansions: SubViewExpansions, Coverage);
324 attachBranchSubViews(View&: *SubView, Branches: SubViewBranches);
325 View.addExpansion(Region: Expansion.Region, View: std::move(SubView));
326 }
327}
328
329void CodeCoverageTool::attachBranchSubViews(SourceCoverageView &View,
330 ArrayRef<CountedRegion> Branches) {
331 if (!ViewOpts.ShowBranchCounts && !ViewOpts.ShowBranchPercents)
332 return;
333
334 const auto *NextBranch = Branches.begin();
335 const auto *EndBranch = Branches.end();
336
337 // Group branches that have the same line number into the same subview.
338 while (NextBranch != EndBranch) {
339 SmallVector<CountedRegion, 0> ViewBranches;
340 unsigned CurrentLine = NextBranch->LineStart;
341 while (NextBranch != EndBranch && CurrentLine == NextBranch->LineStart)
342 ViewBranches.push_back(Elt: *NextBranch++);
343
344 View.addBranch(Line: CurrentLine, Regions: std::move(ViewBranches));
345 }
346}
347
348void CodeCoverageTool::attachMCDCSubViews(SourceCoverageView &View,
349 ArrayRef<MCDCRecord> MCDCRecords) {
350 if (!ViewOpts.ShowMCDC)
351 return;
352
353 const auto *NextRecord = MCDCRecords.begin();
354 const auto *EndRecord = MCDCRecords.end();
355
356 // Group and process MCDC records that have the same line number into the
357 // same subview.
358 while (NextRecord != EndRecord) {
359 SmallVector<MCDCRecord, 0> ViewMCDCRecords;
360 unsigned CurrentLine = NextRecord->getDecisionRegion().LineEnd;
361 while (NextRecord != EndRecord &&
362 CurrentLine == NextRecord->getDecisionRegion().LineEnd)
363 ViewMCDCRecords.push_back(Elt: *NextRecord++);
364
365 View.addMCDCRecord(Line: CurrentLine, Records: std::move(ViewMCDCRecords));
366 }
367}
368
369std::unique_ptr<SourceCoverageView>
370CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
371 const CoverageMapping &Coverage) {
372 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
373 if (FunctionCoverage.empty())
374 return nullptr;
375 auto SourceBuffer = getSourceFile(SourceFile: FunctionCoverage.getFilename());
376 if (!SourceBuffer)
377 return nullptr;
378
379 auto Branches = FunctionCoverage.getBranches();
380 auto Expansions = FunctionCoverage.getExpansions();
381 auto MCDCRecords = FunctionCoverage.getMCDCRecords();
382 auto View = SourceCoverageView::create(SourceName: DC.demangle(Sym: Function.Name),
383 File: SourceBuffer.get(), Options: ViewOpts,
384 CoverageInfo: std::move(FunctionCoverage));
385 attachExpansionSubViews(View&: *View, Expansions, Coverage);
386 attachBranchSubViews(View&: *View, Branches);
387 attachMCDCSubViews(View&: *View, MCDCRecords);
388
389 return View;
390}
391
392std::unique_ptr<SourceCoverageView>
393CodeCoverageTool::createSourceFileView(StringRef SourceFile,
394 const CoverageMapping &Coverage) {
395 auto SourceBuffer = getSourceFile(SourceFile);
396 if (!SourceBuffer)
397 return nullptr;
398 auto FileCoverage = Coverage.getCoverageForFile(Filename: SourceFile);
399 if (FileCoverage.empty())
400 return nullptr;
401
402 auto Branches = FileCoverage.getBranches();
403 auto Expansions = FileCoverage.getExpansions();
404 auto MCDCRecords = FileCoverage.getMCDCRecords();
405 auto View = SourceCoverageView::create(SourceName: SourceFile, File: SourceBuffer.get(),
406 Options: ViewOpts, CoverageInfo: std::move(FileCoverage));
407 attachExpansionSubViews(View&: *View, Expansions, Coverage);
408 attachBranchSubViews(View&: *View, Branches);
409 attachMCDCSubViews(View&: *View, MCDCRecords);
410 if (!ViewOpts.ShowFunctionInstantiations)
411 return View;
412
413 for (const auto &Group : Coverage.getInstantiationGroups(Filename: SourceFile)) {
414 // Skip functions which have a single instantiation.
415 if (Group.size() < 2)
416 continue;
417
418 for (const FunctionRecord *Function : Group.getInstantiations()) {
419 std::unique_ptr<SourceCoverageView> SubView{nullptr};
420
421 StringRef Funcname = DC.demangle(Sym: Function->Name);
422
423 if (Function->ExecutionCount > 0) {
424 auto SubViewCoverage = Coverage.getCoverageForFunction(Function: *Function);
425 auto SubViewExpansions = SubViewCoverage.getExpansions();
426 auto SubViewBranches = SubViewCoverage.getBranches();
427 auto SubViewMCDCRecords = SubViewCoverage.getMCDCRecords();
428 SubView = SourceCoverageView::create(
429 SourceName: Funcname, File: SourceBuffer.get(), Options: ViewOpts, CoverageInfo: std::move(SubViewCoverage));
430 attachExpansionSubViews(View&: *SubView, Expansions: SubViewExpansions, Coverage);
431 attachBranchSubViews(View&: *SubView, Branches: SubViewBranches);
432 attachMCDCSubViews(View&: *SubView, MCDCRecords: SubViewMCDCRecords);
433 }
434
435 unsigned FileID = Function->CountedRegions.front().FileID;
436 unsigned Line = 0;
437 for (const auto &CR : Function->CountedRegions)
438 if (CR.FileID == FileID)
439 Line = std::max(a: CR.LineEnd, b: Line);
440 View->addInstantiation(FunctionName: Funcname, Line, View: std::move(SubView));
441 }
442 }
443 return View;
444}
445
446static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
447 sys::fs::file_status Status;
448 if (sys::fs::status(path: LHS, result&: Status))
449 return false;
450 auto LHSTime = Status.getLastModificationTime();
451 if (sys::fs::status(path: RHS, result&: Status))
452 return false;
453 auto RHSTime = Status.getLastModificationTime();
454 return LHSTime > RHSTime;
455}
456
457std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
458 if (PGOFilename) {
459 for (StringRef ObjectFilename : ObjectFilenames)
460 if (modifiedTimeGT(LHS: ObjectFilename, RHS: PGOFilename.value()))
461 warning(Message: "profile data may be out of date - object is newer",
462 Whence: ObjectFilename);
463 }
464 auto FS = vfs::getRealFileSystem();
465 auto CoverageOrErr = CoverageMapping::load(
466 ObjectFilenames, ProfileFilename: PGOFilename, FS&: *FS, Arches: CoverageArches,
467 CompilationDir: ViewOpts.CompilationDirectory, BIDFetcher: BIDFetcher.get(), CheckBinaryIDs);
468 if (Error E = CoverageOrErr.takeError()) {
469 error(Message: "failed to load coverage: " + toString(E: std::move(E)));
470 return nullptr;
471 }
472 auto Coverage = std::move(CoverageOrErr.get());
473 unsigned Mismatched = Coverage->getMismatchedCount();
474 if (Mismatched) {
475 warning(Message: Twine(Mismatched) + " functions have mismatched data");
476
477 if (ViewOpts.Debug) {
478 for (const auto &HashMismatch : Coverage->getHashMismatches())
479 errs() << "hash-mismatch: "
480 << "No profile record found for '" << HashMismatch.first << "'"
481 << " with hash = 0x" << Twine::utohexstr(Val: HashMismatch.second)
482 << '\n';
483 }
484 }
485
486 remapPathNames(Coverage: *Coverage);
487
488 if (!SourceFiles.empty())
489 removeUnmappedInputs(Coverage: *Coverage);
490
491 demangleSymbols(Coverage: *Coverage);
492
493 return Coverage;
494}
495
496void CodeCoverageTool::remapPathNames(const CoverageMapping &Coverage) {
497 if (!PathRemappings)
498 return;
499
500 // Convert remapping paths to native paths with trailing separators.
501 auto nativeWithTrailing = [](StringRef Path) -> std::string {
502 if (Path.empty())
503 return "";
504 SmallString<128> NativePath;
505 sys::path::native(path: Path, result&: NativePath);
506 sys::path::remove_dots(path&: NativePath, remove_dot_dot: true);
507 if (!NativePath.empty() && !sys::path::is_separator(value: NativePath.back()))
508 NativePath += sys::path::get_separator();
509 return NativePath.c_str();
510 };
511
512 for (std::pair<std::string, std::string> &PathRemapping : *PathRemappings) {
513 std::string RemapFrom = nativeWithTrailing(PathRemapping.first);
514 std::string RemapTo = nativeWithTrailing(PathRemapping.second);
515
516 // Create a mapping from coverage data file paths to local paths.
517 for (StringRef Filename : Coverage.getUniqueSourceFiles()) {
518 if (RemappedFilenames.count(Key: Filename) == 1)
519 continue;
520
521 SmallString<128> NativeFilename;
522 sys::path::native(path: Filename, result&: NativeFilename);
523 sys::path::remove_dots(path&: NativeFilename, remove_dot_dot: true);
524 if (NativeFilename.starts_with(Prefix: RemapFrom)) {
525 RemappedFilenames[Filename] =
526 RemapTo + NativeFilename.substr(Start: RemapFrom.size()).str();
527 }
528 }
529 }
530
531 // Convert input files from local paths to coverage data file paths.
532 StringMap<std::string> InvRemappedFilenames;
533 for (const auto &RemappedFilename : RemappedFilenames)
534 InvRemappedFilenames[RemappedFilename.getValue()] =
535 std::string(RemappedFilename.getKey());
536
537 for (std::string &Filename : SourceFiles) {
538 SmallString<128> NativeFilename;
539 sys::path::native(path: Filename, result&: NativeFilename);
540 auto CovFileName = InvRemappedFilenames.find(Key: NativeFilename);
541 if (CovFileName != InvRemappedFilenames.end())
542 Filename = CovFileName->second;
543 }
544}
545
546void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
547 std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
548
549 // The user may have specified source files which aren't in the coverage
550 // mapping. Filter these files away.
551 llvm::erase_if(C&: SourceFiles, P: [&](const std::string &SF) {
552 return !llvm::binary_search(Range&: CoveredFiles, Value: SF);
553 });
554}
555
556void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
557 if (!ViewOpts.hasDemangler())
558 return;
559
560 // Pass function names to the demangler in a temporary file.
561 int InputFD;
562 SmallString<256> InputPath;
563 std::error_code EC =
564 sys::fs::createTemporaryFile(Prefix: "demangle-in", Suffix: "list", ResultFD&: InputFD, ResultPath&: InputPath);
565 if (EC) {
566 error(Message: InputPath, Whence: EC.message());
567 return;
568 }
569 ToolOutputFile InputTOF{InputPath, InputFD};
570
571 unsigned NumSymbols = 0;
572 for (const auto &Function : Coverage.getCoveredFunctions()) {
573 InputTOF.os() << Function.Name << '\n';
574 ++NumSymbols;
575 }
576 InputTOF.os().close();
577
578 // Use another temporary file to store the demangler's output.
579 int OutputFD;
580 SmallString<256> OutputPath;
581 EC = sys::fs::createTemporaryFile(Prefix: "demangle-out", Suffix: "list", ResultFD&: OutputFD,
582 ResultPath&: OutputPath);
583 if (EC) {
584 error(Message: OutputPath, Whence: EC.message());
585 return;
586 }
587 ToolOutputFile OutputTOF{OutputPath, OutputFD};
588 OutputTOF.os().close();
589
590 // Invoke the demangler.
591 std::vector<StringRef> ArgsV;
592 ArgsV.reserve(n: ViewOpts.DemanglerOpts.size());
593 llvm::append_range(C&: ArgsV, R&: ViewOpts.DemanglerOpts);
594 std::optional<StringRef> Redirects[] = {
595 InputPath.str(), OutputPath.str(), {""}};
596 std::string ErrMsg;
597 int RC =
598 sys::ExecuteAndWait(Program: ViewOpts.DemanglerOpts[0], Args: ArgsV,
599 /*env=*/Env: std::nullopt, Redirects, /*secondsToWait=*/SecondsToWait: 0,
600 /*memoryLimit=*/MemoryLimit: 0, ErrMsg: &ErrMsg);
601 if (RC) {
602 error(Message: ErrMsg, Whence: ViewOpts.DemanglerOpts[0]);
603 return;
604 }
605
606 // Parse the demangler's output.
607 auto BufOrError = MemoryBuffer::getFile(Filename: OutputPath);
608 if (!BufOrError) {
609 error(Message: OutputPath, Whence: BufOrError.getError().message());
610 return;
611 }
612
613 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
614
615 SmallVector<StringRef, 8> Symbols;
616 StringRef DemanglerData = DemanglerBuf->getBuffer();
617 DemanglerData.split(A&: Symbols, Separator: '\n', /*MaxSplit=*/NumSymbols,
618 /*KeepEmpty=*/false);
619 if (Symbols.size() != NumSymbols) {
620 error(Message: "demangler did not provide expected number of symbols");
621 return;
622 }
623
624 // Cache the demangled names.
625 unsigned I = 0;
626 for (const auto &Function : Coverage.getCoveredFunctions())
627 // On Windows, lines in the demangler's output file end with "\r\n".
628 // Splitting by '\n' keeps '\r's, so cut them now.
629 DC.DemangledNames[Function.Name] = std::string(Symbols[I++].rtrim());
630}
631
632void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
633 CoverageMapping *Coverage,
634 CoveragePrinter *Printer,
635 bool ShowFilenames) {
636 auto View = createSourceFileView(SourceFile, Coverage: *Coverage);
637 if (!View) {
638 warning(Message: "The file '" + SourceFile + "' isn't covered.");
639 return;
640 }
641
642 auto OSOrErr = Printer->createViewFile(Path: SourceFile, /*InToplevel=*/false);
643 if (Error E = OSOrErr.takeError()) {
644 error(Message: "could not create view file!", Whence: toString(E: std::move(E)));
645 return;
646 }
647 auto OS = std::move(OSOrErr.get());
648
649 View->print(OS&: *OS.get(), /*Wholefile=*/WholeFile: true,
650 /*ShowSourceName=*/ShowFilenames,
651 /*ShowTitle=*/ViewOpts.hasOutputDirectory());
652 Printer->closeViewFile(OS: std::move(OS));
653}
654
655int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
656 cl::opt<std::string> CovFilename(
657 cl::Positional, cl::desc("Covered executable or object file."));
658
659 cl::list<std::string> CovFilenames(
660 "object", cl::desc("Coverage executable or object file"));
661
662 cl::opt<bool> DebugDumpCollectedObjects(
663 "dump-collected-objects", cl::Optional, cl::Hidden,
664 cl::desc("Show the collected coverage object files"));
665
666 cl::list<std::string> InputSourceFiles("sources", cl::Positional,
667 cl::desc("<Source files>"));
668
669 cl::opt<bool> DebugDumpCollectedPaths(
670 "dump-collected-paths", cl::Optional, cl::Hidden,
671 cl::desc("Show the collected paths to source files"));
672
673 cl::opt<std::string> PGOFilename(
674 "instr-profile", cl::Optional,
675 cl::desc(
676 "File with the profile data obtained after an instrumented run"));
677
678 cl::opt<bool> EmptyProfile(
679 "empty-profile", cl::Optional,
680 cl::desc("Use a synthetic profile with no data to generate "
681 "baseline coverage"));
682
683 cl::list<std::string> Arches(
684 "arch", cl::desc("architectures of the coverage mapping binaries"));
685
686 cl::opt<bool> DebugDump("dump", cl::Optional,
687 cl::desc("Show internal debug dump"));
688
689 cl::list<std::string> DebugFileDirectory(
690 "debug-file-directory",
691 cl::desc("Directories to search for object files by build ID"));
692 cl::opt<bool> Debuginfod(
693 "debuginfod", cl::ZeroOrMore,
694 cl::desc("Use debuginfod to look up object files from profile"),
695 cl::init(Val: canUseDebuginfod()));
696
697 cl::opt<CoverageViewOptions::OutputFormat> Format(
698 "format", cl::desc("Output format for line-based coverage reports"),
699 cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
700 "Text output"),
701 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
702 "HTML output"),
703 clEnumValN(CoverageViewOptions::OutputFormat::Lcov, "lcov",
704 "lcov tracefile output")),
705 cl::init(Val: CoverageViewOptions::OutputFormat::Text));
706
707 cl::list<std::string> PathRemaps(
708 "path-equivalence", cl::Optional,
709 cl::desc("<from>,<to> Map coverage data paths to local source file "
710 "paths"));
711
712 cl::OptionCategory FilteringCategory("Function filtering options");
713
714 cl::list<std::string> NameFilters(
715 "name", cl::Optional,
716 cl::desc("Show code coverage only for functions with the given name"),
717 cl::cat(FilteringCategory));
718
719 cl::list<std::string> NameFilterFiles(
720 "name-allowlist", cl::Optional,
721 cl::desc("Show code coverage only for functions listed in the given "
722 "file"),
723 cl::cat(FilteringCategory));
724
725 cl::list<std::string> NameRegexFilters(
726 "name-regex", cl::Optional,
727 cl::desc("Show code coverage only for functions that match the given "
728 "regular expression"),
729 cl::cat(FilteringCategory));
730
731 cl::list<std::string> IgnoreFilenameRegexFilters(
732 "ignore-filename-regex", cl::Optional,
733 cl::desc("Skip source code files with file paths that match the given "
734 "regular expression"),
735 cl::cat(FilteringCategory));
736
737 cl::opt<double> RegionCoverageLtFilter(
738 "region-coverage-lt", cl::Optional,
739 cl::desc("Show code coverage only for functions with region coverage "
740 "less than the given threshold"),
741 cl::cat(FilteringCategory));
742
743 cl::opt<double> RegionCoverageGtFilter(
744 "region-coverage-gt", cl::Optional,
745 cl::desc("Show code coverage only for functions with region coverage "
746 "greater than the given threshold"),
747 cl::cat(FilteringCategory));
748
749 cl::opt<double> LineCoverageLtFilter(
750 "line-coverage-lt", cl::Optional,
751 cl::desc("Show code coverage only for functions with line coverage less "
752 "than the given threshold"),
753 cl::cat(FilteringCategory));
754
755 cl::opt<double> LineCoverageGtFilter(
756 "line-coverage-gt", cl::Optional,
757 cl::desc("Show code coverage only for functions with line coverage "
758 "greater than the given threshold"),
759 cl::cat(FilteringCategory));
760
761 cl::opt<cl::boolOrDefault> UseColor(
762 "use-color", cl::desc("Emit colored output (default=autodetect)"),
763 cl::init(Val: cl::BOU_UNSET));
764
765 cl::list<std::string> DemanglerOpts(
766 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
767
768 cl::opt<bool> RegionSummary(
769 "show-region-summary", cl::Optional,
770 cl::desc("Show region statistics in summary table"),
771 cl::init(Val: true));
772
773 cl::opt<bool> BranchSummary(
774 "show-branch-summary", cl::Optional,
775 cl::desc("Show branch condition statistics in summary table"),
776 cl::init(Val: true));
777
778 cl::opt<bool> MCDCSummary("show-mcdc-summary", cl::Optional,
779 cl::desc("Show MCDC statistics in summary table"),
780 cl::init(Val: false));
781
782 cl::opt<bool> InstantiationSummary(
783 "show-instantiation-summary", cl::Optional,
784 cl::desc("Show instantiation statistics in summary table"));
785
786 cl::opt<bool> SummaryOnly(
787 "summary-only", cl::Optional,
788 cl::desc("Export only summary information for each source file"));
789
790 cl::opt<unsigned> NumThreads(
791 "num-threads", cl::init(Val: 0),
792 cl::desc("Number of merge threads to use (default: autodetect)"));
793 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
794 cl::aliasopt(NumThreads));
795
796 cl::opt<std::string> CompilationDirectory(
797 "compilation-dir", cl::init(Val: ""),
798 cl::desc("Directory used as a base for relative coverage mapping paths"));
799
800 cl::opt<bool> CheckBinaryIDs(
801 "check-binary-ids", cl::desc("Fail if an object couldn't be found for a "
802 "binary ID in the profile"));
803
804 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
805 cl::ParseCommandLineOptions(argc, argv, Overview: "LLVM code coverage tool\n");
806 ViewOpts.Debug = DebugDump;
807 if (Debuginfod) {
808 HTTPClient::initialize();
809 BIDFetcher = std::make_unique<DebuginfodFetcher>(args&: DebugFileDirectory);
810 } else {
811 BIDFetcher = std::make_unique<object::BuildIDFetcher>(args&: DebugFileDirectory);
812 }
813 this->CheckBinaryIDs = CheckBinaryIDs;
814
815 if (!PGOFilename.empty() == EmptyProfile) {
816 error(
817 Message: "exactly one of -instr-profile and -empty-profile must be specified");
818 return 1;
819 }
820 if (!PGOFilename.empty()) {
821 this->PGOFilename = std::make_optional(t&: PGOFilename.getValue());
822 }
823
824 if (!CovFilename.empty())
825 ObjectFilenames.emplace_back(args&: CovFilename);
826 for (const std::string &Filename : CovFilenames)
827 ObjectFilenames.emplace_back(args: Filename);
828 if (ObjectFilenames.empty() && !Debuginfod && DebugFileDirectory.empty()) {
829 errs() << "No filenames specified!\n";
830 ::exit(status: 1);
831 }
832
833 if (DebugDumpCollectedObjects) {
834 for (StringRef OF : ObjectFilenames)
835 outs() << OF << '\n';
836 ::exit(status: 0);
837 }
838
839 ViewOpts.Format = Format;
840 switch (ViewOpts.Format) {
841 case CoverageViewOptions::OutputFormat::Text:
842 ViewOpts.Colors = UseColor == cl::BOU_UNSET
843 ? sys::Process::StandardOutHasColors()
844 : UseColor == cl::BOU_TRUE;
845 break;
846 case CoverageViewOptions::OutputFormat::HTML:
847 if (UseColor == cl::BOU_FALSE)
848 errs() << "Color output cannot be disabled when generating html.\n";
849 ViewOpts.Colors = true;
850 break;
851 case CoverageViewOptions::OutputFormat::Lcov:
852 if (UseColor == cl::BOU_TRUE)
853 errs() << "Color output cannot be enabled when generating lcov.\n";
854 ViewOpts.Colors = false;
855 break;
856 }
857
858 if (!PathRemaps.empty()) {
859 std::vector<std::pair<std::string, std::string>> Remappings;
860
861 for (const std::string &PathRemap : PathRemaps) {
862 auto EquivPair = StringRef(PathRemap).split(Separator: ',');
863 if (EquivPair.first.empty() || EquivPair.second.empty()) {
864 error(Message: "invalid argument '" + PathRemap +
865 "', must be in format 'from,to'",
866 Whence: "-path-equivalence");
867 return 1;
868 }
869
870 Remappings.push_back(
871 x: {std::string(EquivPair.first), std::string(EquivPair.second)});
872 }
873
874 PathRemappings = Remappings;
875 }
876
877 // If a demangler is supplied, check if it exists and register it.
878 if (!DemanglerOpts.empty()) {
879 auto DemanglerPathOrErr = sys::findProgramByName(Name: DemanglerOpts[0]);
880 if (!DemanglerPathOrErr) {
881 error(Message: "could not find the demangler!",
882 Whence: DemanglerPathOrErr.getError().message());
883 return 1;
884 }
885 DemanglerOpts[0] = *DemanglerPathOrErr;
886 ViewOpts.DemanglerOpts.swap(x&: DemanglerOpts);
887 }
888
889 // Read in -name-allowlist files.
890 if (!NameFilterFiles.empty()) {
891 std::string SpecialCaseListErr;
892 NameAllowlist = SpecialCaseList::create(
893 Paths: NameFilterFiles, FS&: *vfs::getRealFileSystem(), Error&: SpecialCaseListErr);
894 if (!NameAllowlist)
895 error(Message: SpecialCaseListErr);
896 }
897
898 // Create the function filters
899 if (!NameFilters.empty() || NameAllowlist || !NameRegexFilters.empty()) {
900 auto NameFilterer = std::make_unique<CoverageFilters>();
901 for (const auto &Name : NameFilters)
902 NameFilterer->push_back(Filter: std::make_unique<NameCoverageFilter>(args: Name));
903 if (NameAllowlist && !NameFilterFiles.empty())
904 NameFilterer->push_back(
905 Filter: std::make_unique<NameAllowlistCoverageFilter>(args&: *NameAllowlist));
906 for (const auto &Regex : NameRegexFilters)
907 NameFilterer->push_back(
908 Filter: std::make_unique<NameRegexCoverageFilter>(args: Regex));
909 Filters.push_back(Filter: std::move(NameFilterer));
910 }
911
912 if (RegionCoverageLtFilter.getNumOccurrences() ||
913 RegionCoverageGtFilter.getNumOccurrences() ||
914 LineCoverageLtFilter.getNumOccurrences() ||
915 LineCoverageGtFilter.getNumOccurrences()) {
916 auto StatFilterer = std::make_unique<CoverageFilters>();
917 if (RegionCoverageLtFilter.getNumOccurrences())
918 StatFilterer->push_back(Filter: std::make_unique<RegionCoverageFilter>(
919 args: RegionCoverageFilter::LessThan, args&: RegionCoverageLtFilter));
920 if (RegionCoverageGtFilter.getNumOccurrences())
921 StatFilterer->push_back(Filter: std::make_unique<RegionCoverageFilter>(
922 args: RegionCoverageFilter::GreaterThan, args&: RegionCoverageGtFilter));
923 if (LineCoverageLtFilter.getNumOccurrences())
924 StatFilterer->push_back(Filter: std::make_unique<LineCoverageFilter>(
925 args: LineCoverageFilter::LessThan, args&: LineCoverageLtFilter));
926 if (LineCoverageGtFilter.getNumOccurrences())
927 StatFilterer->push_back(Filter: std::make_unique<LineCoverageFilter>(
928 args: RegionCoverageFilter::GreaterThan, args&: LineCoverageGtFilter));
929 Filters.push_back(Filter: std::move(StatFilterer));
930 }
931
932 // Create the ignore filename filters.
933 for (const auto &RE : IgnoreFilenameRegexFilters)
934 IgnoreFilenameFilters.push_back(
935 Filter: std::make_unique<NameRegexCoverageFilter>(args: RE));
936
937 if (!Arches.empty()) {
938 for (const std::string &Arch : Arches) {
939 if (Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
940 error(Message: "unknown architecture: " + Arch);
941 return 1;
942 }
943 CoverageArches.emplace_back(args: Arch);
944 }
945 if (CoverageArches.size() != 1 &&
946 CoverageArches.size() != ObjectFilenames.size()) {
947 error(Message: "number of architectures doesn't match the number of objects");
948 return 1;
949 }
950 }
951
952 // IgnoreFilenameFilters are applied even when InputSourceFiles specified.
953 for (const std::string &File : InputSourceFiles)
954 collectPaths(Path: File);
955
956 if (DebugDumpCollectedPaths) {
957 for (const std::string &SF : SourceFiles)
958 outs() << SF << '\n';
959 ::exit(status: 0);
960 }
961
962 ViewOpts.ShowMCDCSummary = MCDCSummary;
963 ViewOpts.ShowBranchSummary = BranchSummary;
964 ViewOpts.ShowRegionSummary = RegionSummary;
965 ViewOpts.ShowInstantiationSummary = InstantiationSummary;
966 ViewOpts.ExportSummaryOnly = SummaryOnly;
967 ViewOpts.NumThreads = NumThreads;
968 ViewOpts.CompilationDirectory = CompilationDirectory;
969
970 return 0;
971 };
972
973 switch (Cmd) {
974 case Show:
975 return doShow(argc, argv, commandLineParser);
976 case Report:
977 return doReport(argc, argv, commandLineParser);
978 case Export:
979 return doExport(argc, argv, commandLineParser);
980 }
981 return 0;
982}
983
984int CodeCoverageTool::doShow(int argc, const char **argv,
985 CommandLineParserType commandLineParser) {
986
987 cl::OptionCategory ViewCategory("Viewing options");
988
989 cl::opt<bool> ShowLineExecutionCounts(
990 "show-line-counts", cl::Optional,
991 cl::desc("Show the execution counts for each line"), cl::init(Val: true),
992 cl::cat(ViewCategory));
993
994 cl::opt<bool> ShowRegions(
995 "show-regions", cl::Optional,
996 cl::desc("Show the execution counts for each region"),
997 cl::cat(ViewCategory));
998
999 cl::opt<CoverageViewOptions::BranchOutputType> ShowBranches(
1000 "show-branches", cl::Optional,
1001 cl::desc("Show coverage for branch conditions"), cl::cat(ViewCategory),
1002 cl::values(clEnumValN(CoverageViewOptions::BranchOutputType::Count,
1003 "count", "Show True/False counts"),
1004 clEnumValN(CoverageViewOptions::BranchOutputType::Percent,
1005 "percent", "Show True/False percent")),
1006 cl::init(Val: CoverageViewOptions::BranchOutputType::Off));
1007
1008 cl::opt<bool> ShowMCDC(
1009 "show-mcdc", cl::Optional,
1010 cl::desc("Show the MCDC Coverage for each applicable boolean expression"),
1011 cl::cat(ViewCategory));
1012
1013 cl::opt<bool> ShowBestLineRegionsCounts(
1014 "show-line-counts-or-regions", cl::Optional,
1015 cl::desc("Show the execution counts for each line, or the execution "
1016 "counts for each region on lines that have multiple regions"),
1017 cl::cat(ViewCategory));
1018
1019 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
1020 cl::desc("Show expanded source regions"),
1021 cl::cat(ViewCategory));
1022
1023 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
1024 cl::desc("Show function instantiations"),
1025 cl::init(Val: true), cl::cat(ViewCategory));
1026
1027 cl::opt<bool> ShowDirectoryCoverage("show-directory-coverage", cl::Optional,
1028 cl::desc("Show directory coverage"),
1029 cl::cat(ViewCategory));
1030
1031 cl::opt<bool> ShowCreatedTime("show-created-time", cl::Optional,
1032 cl::desc("Show created time for each page."),
1033 cl::init(Val: true), cl::cat(ViewCategory));
1034
1035 cl::opt<std::string> ShowOutputDirectory(
1036 "output-dir", cl::init(Val: ""),
1037 cl::desc("Directory in which coverage information is written out"));
1038 cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
1039 cl::aliasopt(ShowOutputDirectory));
1040
1041 cl::opt<bool> BinaryCounters(
1042 "binary-counters", cl::Optional,
1043 cl::desc("Show binary counters (1/0) in lines and branches instead of "
1044 "integer execution counts"),
1045 cl::cat(ViewCategory));
1046
1047 cl::opt<uint32_t> TabSize(
1048 "tab-size", cl::init(Val: 2),
1049 cl::desc(
1050 "Set tab expansion size for html coverage reports (default = 2)"));
1051
1052 cl::opt<std::string> ProjectTitle(
1053 "project-title", cl::Optional,
1054 cl::desc("Set project title for the coverage report"));
1055
1056 cl::opt<std::string> CovWatermark(
1057 "coverage-watermark", cl::Optional,
1058 cl::desc("<high>,<low> value indicate thresholds for high and low"
1059 "coverage watermark"));
1060
1061 auto Err = commandLineParser(argc, argv);
1062 if (Err)
1063 return Err;
1064
1065 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::Lcov) {
1066 error(Message: "lcov format should be used with 'llvm-cov export'.");
1067 return 1;
1068 }
1069
1070 ViewOpts.HighCovWatermark = 100.0;
1071 ViewOpts.LowCovWatermark = 80.0;
1072 if (!CovWatermark.empty()) {
1073 auto WaterMarkPair = StringRef(CovWatermark).split(Separator: ',');
1074 if (WaterMarkPair.first.empty() || WaterMarkPair.second.empty()) {
1075 error(Message: "invalid argument '" + CovWatermark +
1076 "', must be in format 'high,low'",
1077 Whence: "-coverage-watermark");
1078 return 1;
1079 }
1080
1081 char *EndPointer = nullptr;
1082 ViewOpts.HighCovWatermark =
1083 strtod(nptr: WaterMarkPair.first.begin(), endptr: &EndPointer);
1084 if (EndPointer != WaterMarkPair.first.end()) {
1085 error(Message: "invalid number '" + WaterMarkPair.first +
1086 "', invalid value for 'high'",
1087 Whence: "-coverage-watermark");
1088 return 1;
1089 }
1090
1091 ViewOpts.LowCovWatermark =
1092 strtod(nptr: WaterMarkPair.second.begin(), endptr: &EndPointer);
1093 if (EndPointer != WaterMarkPair.second.end()) {
1094 error(Message: "invalid number '" + WaterMarkPair.second +
1095 "', invalid value for 'low'",
1096 Whence: "-coverage-watermark");
1097 return 1;
1098 }
1099
1100 if (ViewOpts.HighCovWatermark > 100 || ViewOpts.LowCovWatermark < 0 ||
1101 ViewOpts.HighCovWatermark <= ViewOpts.LowCovWatermark) {
1102 error(
1103 Message: "invalid number range '" + CovWatermark +
1104 "', must be both high and low should be between 0-100, and high "
1105 "> low",
1106 Whence: "-coverage-watermark");
1107 return 1;
1108 }
1109 }
1110
1111 ViewOpts.ShowLineNumbers = true;
1112 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
1113 !ShowRegions || ShowBestLineRegionsCounts;
1114 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
1115 ViewOpts.ShowExpandedRegions = ShowExpansions;
1116 ViewOpts.ShowBranchCounts =
1117 ShowBranches == CoverageViewOptions::BranchOutputType::Count;
1118 ViewOpts.ShowMCDC = ShowMCDC;
1119 ViewOpts.ShowBranchPercents =
1120 ShowBranches == CoverageViewOptions::BranchOutputType::Percent;
1121 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
1122 ViewOpts.ShowDirectoryCoverage = ShowDirectoryCoverage;
1123 ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
1124 ViewOpts.BinaryCounters = BinaryCounters;
1125 ViewOpts.TabSize = TabSize;
1126 ViewOpts.ProjectTitle = ProjectTitle;
1127
1128 if (ViewOpts.hasOutputDirectory()) {
1129 if (auto E = sys::fs::create_directories(path: ViewOpts.ShowOutputDirectory)) {
1130 error(Message: "could not create output directory!", Whence: E.message());
1131 return 1;
1132 }
1133 }
1134
1135 if (PGOFilename) {
1136 sys::fs::file_status Status;
1137 if (std::error_code EC = sys::fs::status(path: PGOFilename.value(), result&: Status)) {
1138 error(Message: "could not read profile data!" + EC.message(), Whence: PGOFilename.value());
1139 return 1;
1140 }
1141
1142 if (ShowCreatedTime) {
1143 auto ModifiedTime = Status.getLastModificationTime();
1144 std::string ModifiedTimeStr = to_string(Value: ModifiedTime);
1145 size_t found = ModifiedTimeStr.rfind(c: ':');
1146 ViewOpts.CreatedTimeStr =
1147 (found != std::string::npos)
1148 ? "Created: " + ModifiedTimeStr.substr(pos: 0, n: found)
1149 : "Created: " + ModifiedTimeStr;
1150 }
1151 }
1152
1153 auto Coverage = load();
1154 if (!Coverage)
1155 return 1;
1156
1157 auto Printer = CoveragePrinter::create(Opts: ViewOpts);
1158
1159 if (SourceFiles.empty() && !HadSourceFiles)
1160 // Get the source files from the function coverage mapping.
1161 for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
1162 if (!IgnoreFilenameFilters.matchesFilename(Filename))
1163 SourceFiles.push_back(x: std::string(Filename));
1164 }
1165
1166 // Create an index out of the source files.
1167 if (ViewOpts.hasOutputDirectory()) {
1168 if (Error E = Printer->createIndexFile(SourceFiles, Coverage: *Coverage, Filters)) {
1169 error(Message: "could not create index file!", Whence: toString(E: std::move(E)));
1170 return 1;
1171 }
1172 }
1173
1174 if (!Filters.empty()) {
1175 // Build the map of filenames to functions.
1176 std::map<llvm::StringRef, std::vector<const FunctionRecord *>>
1177 FilenameFunctionMap;
1178 for (const auto &SourceFile : SourceFiles)
1179 for (const auto &Function : Coverage->getCoveredFunctions(Filename: SourceFile))
1180 if (Filters.matches(CM: *Coverage, Function))
1181 FilenameFunctionMap[SourceFile].push_back(x: &Function);
1182
1183 // Only print filter matching functions for each file.
1184 for (const auto &FileFunc : FilenameFunctionMap) {
1185 StringRef File = FileFunc.first;
1186 const auto &Functions = FileFunc.second;
1187
1188 auto OSOrErr = Printer->createViewFile(Path: File, /*InToplevel=*/false);
1189 if (Error E = OSOrErr.takeError()) {
1190 error(Message: "could not create view file!", Whence: toString(E: std::move(E)));
1191 return 1;
1192 }
1193 auto OS = std::move(OSOrErr.get());
1194
1195 bool ShowTitle = ViewOpts.hasOutputDirectory();
1196 for (const auto *Function : Functions) {
1197 auto FunctionView = createFunctionView(Function: *Function, Coverage: *Coverage);
1198 if (!FunctionView) {
1199 warning(Message: "Could not read coverage for '" + Function->Name + "'.");
1200 continue;
1201 }
1202 FunctionView->print(OS&: *OS.get(), /*WholeFile=*/false,
1203 /*ShowSourceName=*/true, ShowTitle);
1204 ShowTitle = false;
1205 }
1206
1207 Printer->closeViewFile(OS: std::move(OS));
1208 }
1209 return 0;
1210 }
1211
1212 // Show files
1213 bool ShowFilenames =
1214 (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
1215 (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
1216
1217 ThreadPoolStrategy S = hardware_concurrency(ThreadCount: ViewOpts.NumThreads);
1218 if (ViewOpts.NumThreads == 0) {
1219 // If NumThreads is not specified, create one thread for each input, up to
1220 // the number of hardware cores.
1221 S = heavyweight_hardware_concurrency(ThreadCount: SourceFiles.size());
1222 S.Limit = true;
1223 }
1224
1225 if (!ViewOpts.hasOutputDirectory() || S.ThreadsRequested == 1) {
1226 for (const std::string &SourceFile : SourceFiles)
1227 writeSourceFileView(SourceFile, Coverage: Coverage.get(), Printer: Printer.get(),
1228 ShowFilenames);
1229 } else {
1230 // In -output-dir mode, it's safe to use multiple threads to print files.
1231 DefaultThreadPool Pool(S);
1232 for (const std::string &SourceFile : SourceFiles)
1233 Pool.async(F: &CodeCoverageTool::writeSourceFileView, ArgList: this, ArgList: SourceFile,
1234 ArgList: Coverage.get(), ArgList: Printer.get(), ArgList&: ShowFilenames);
1235 Pool.wait();
1236 }
1237
1238 return 0;
1239}
1240
1241int CodeCoverageTool::doReport(int argc, const char **argv,
1242 CommandLineParserType commandLineParser) {
1243 cl::opt<bool> ShowFunctionSummaries(
1244 "show-functions", cl::Optional, cl::init(Val: false),
1245 cl::desc("Show coverage summaries for each function"));
1246
1247 auto Err = commandLineParser(argc, argv);
1248 if (Err)
1249 return Err;
1250
1251 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) {
1252 error(Message: "HTML output for summary reports is not yet supported.");
1253 return 1;
1254 } else if (ViewOpts.Format == CoverageViewOptions::OutputFormat::Lcov) {
1255 error(Message: "lcov format should be used with 'llvm-cov export'.");
1256 return 1;
1257 }
1258
1259 if (PGOFilename) {
1260 sys::fs::file_status Status;
1261 if (std::error_code EC = sys::fs::status(path: PGOFilename.value(), result&: Status)) {
1262 error(Message: "could not read profile data!" + EC.message(), Whence: PGOFilename.value());
1263 return 1;
1264 }
1265 }
1266
1267 auto Coverage = load();
1268 if (!Coverage)
1269 return 1;
1270
1271 CoverageReport Report(ViewOpts, *Coverage);
1272 if (!ShowFunctionSummaries) {
1273 if (SourceFiles.empty())
1274 Report.renderFileReports(OS&: llvm::outs(), IgnoreFilenameFilters);
1275 else
1276 Report.renderFileReports(OS&: llvm::outs(), Files: SourceFiles);
1277 } else {
1278 if (SourceFiles.empty()) {
1279 error(Message: "source files must be specified when -show-functions=true is "
1280 "specified");
1281 return 1;
1282 }
1283
1284 Report.renderFunctionReports(Files: SourceFiles, DC, OS&: llvm::outs());
1285 }
1286 return 0;
1287}
1288
1289int CodeCoverageTool::doExport(int argc, const char **argv,
1290 CommandLineParserType commandLineParser) {
1291
1292 cl::OptionCategory ExportCategory("Exporting options");
1293
1294 cl::opt<bool> SkipExpansions("skip-expansions", cl::Optional,
1295 cl::desc("Don't export expanded source regions"),
1296 cl::cat(ExportCategory));
1297
1298 cl::opt<bool> SkipFunctions("skip-functions", cl::Optional,
1299 cl::desc("Don't export per-function data"),
1300 cl::cat(ExportCategory));
1301
1302 cl::opt<bool> SkipBranches("skip-branches", cl::Optional,
1303 cl::desc("Don't export branch data (LCOV)"),
1304 cl::cat(ExportCategory));
1305
1306 cl::opt<bool> UnifyInstantiations("unify-instantiations", cl::Optional,
1307 cl::desc("Unify function instantiations"),
1308 cl::init(Val: true), cl::cat(ExportCategory));
1309
1310 auto Err = commandLineParser(argc, argv);
1311 if (Err)
1312 return Err;
1313
1314 ViewOpts.SkipExpansions = SkipExpansions;
1315 ViewOpts.SkipFunctions = SkipFunctions;
1316 ViewOpts.SkipBranches = SkipBranches;
1317 ViewOpts.UnifyFunctionInstantiations = UnifyInstantiations;
1318
1319 if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text &&
1320 ViewOpts.Format != CoverageViewOptions::OutputFormat::Lcov) {
1321 error(Message: "coverage data can only be exported as textual JSON or an "
1322 "lcov tracefile.");
1323 return 1;
1324 }
1325
1326 if (PGOFilename) {
1327 sys::fs::file_status Status;
1328 if (std::error_code EC = sys::fs::status(path: PGOFilename.value(), result&: Status)) {
1329 error(Message: "could not read profile data!" + EC.message(), Whence: PGOFilename.value());
1330 return 1;
1331 }
1332 }
1333
1334 auto Coverage = load();
1335 if (!Coverage) {
1336 error(Message: "could not load coverage information");
1337 return 1;
1338 }
1339
1340 std::unique_ptr<CoverageExporter> Exporter;
1341
1342 switch (ViewOpts.Format) {
1343 case CoverageViewOptions::OutputFormat::Text:
1344 Exporter =
1345 std::make_unique<CoverageExporterJson>(args&: *Coverage, args&: ViewOpts, args&: outs());
1346 break;
1347 case CoverageViewOptions::OutputFormat::HTML:
1348 // Unreachable because we should have gracefully terminated with an error
1349 // above.
1350 llvm_unreachable("Export in HTML is not supported!");
1351 case CoverageViewOptions::OutputFormat::Lcov:
1352 Exporter =
1353 std::make_unique<CoverageExporterLcov>(args&: *Coverage, args&: ViewOpts, args&: outs());
1354 break;
1355 }
1356
1357 if (SourceFiles.empty())
1358 Exporter->renderRoot(IgnoreFilters: IgnoreFilenameFilters);
1359 else
1360 Exporter->renderRoot(SourceFiles);
1361
1362 return 0;
1363}
1364
1365int showMain(int argc, const char *argv[]) {
1366 CodeCoverageTool Tool;
1367 return Tool.run(Cmd: CodeCoverageTool::Show, argc, argv);
1368}
1369
1370int reportMain(int argc, const char *argv[]) {
1371 CodeCoverageTool Tool;
1372 return Tool.run(Cmd: CodeCoverageTool::Report, argc, argv);
1373}
1374
1375int exportMain(int argc, const char *argv[]) {
1376 CodeCoverageTool Tool;
1377 return Tool.run(Cmd: CodeCoverageTool::Export, argc, argv);
1378}
1379