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

source code of llvm/tools/llvm-cov/CodeCoverage.cpp