1 //===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
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
7 //===----------------------------------------------------------------------===//
9 // The 'CodeCoverageTool' class implements a command line tool to analyze and
10 // report coverage information using the profiling instrumentation and code
13 //===----------------------------------------------------------------------===//
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/ADT/Triple.h"
26 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
27 #include "llvm/ProfileData/InstrProfReader.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/Format.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/Process.h"
34 #include "llvm/Support/Program.h"
35 #include "llvm/Support/ScopedPrinter.h"
36 #include "llvm/Support/SpecialCaseList.h"
37 #include "llvm/Support/ThreadPool.h"
38 #include "llvm/Support/Threading.h"
39 #include "llvm/Support/ToolOutputFile.h"
40 #include "llvm/Support/VirtualFileSystem.h"
44 #include <system_error>
47 using namespace coverage
;
49 void exportCoverageDataToJson(const coverage::CoverageMapping
&CoverageMapping
,
50 const CoverageViewOptions
&Options
,
54 /// The implementation of the coverage tool.
55 class CodeCoverageTool
{
60 /// The report command.
62 /// The export command.
66 int run(Command Cmd
, int argc
, const char **argv
);
69 /// Print the error message to the error output stream.
70 void error(const Twine
&Message
, StringRef Whence
= "");
72 /// Print the warning message to the error output stream.
73 void warning(const Twine
&Message
, StringRef Whence
= "");
75 /// Convert \p Path into an absolute path and append it to the list
76 /// of collected paths.
77 void addCollectedPath(const std::string
&Path
);
79 /// If \p Path is a regular file, collect the path. If it's a
80 /// directory, recursively collect all of the paths within the directory.
81 void collectPaths(const std::string
&Path
);
83 /// Check if the two given files are the same file.
84 bool isEquivalentFile(StringRef FilePath1
, StringRef FilePath2
);
86 /// Retrieve a file status with a cache.
87 Optional
<sys::fs::file_status
> getFileStatus(StringRef FilePath
);
89 /// Return a memory buffer for the given source file.
90 ErrorOr
<const MemoryBuffer
&> getSourceFile(StringRef SourceFile
);
92 /// Create source views for the expansions of the view.
93 void attachExpansionSubViews(SourceCoverageView
&View
,
94 ArrayRef
<ExpansionRecord
> Expansions
,
95 const CoverageMapping
&Coverage
);
97 /// Create source views for the branches of the view.
98 void attachBranchSubViews(SourceCoverageView
&View
, StringRef SourceName
,
99 ArrayRef
<CountedRegion
> Branches
,
100 const MemoryBuffer
&File
,
101 CoverageData
&CoverageInfo
);
103 /// Create the source view of a particular function.
104 std::unique_ptr
<SourceCoverageView
>
105 createFunctionView(const FunctionRecord
&Function
,
106 const CoverageMapping
&Coverage
);
108 /// Create the main source view of a particular source file.
109 std::unique_ptr
<SourceCoverageView
>
110 createSourceFileView(StringRef SourceFile
, const CoverageMapping
&Coverage
);
112 /// Load the coverage mapping data. Return nullptr if an error occurred.
113 std::unique_ptr
<CoverageMapping
> load();
115 /// Create a mapping from files in the Coverage data to local copies
116 /// (path-equivalence).
117 void remapPathNames(const CoverageMapping
&Coverage
);
119 /// Remove input source files which aren't mapped by \p Coverage.
120 void removeUnmappedInputs(const CoverageMapping
&Coverage
);
122 /// If a demangler is available, demangle all symbol names.
123 void demangleSymbols(const CoverageMapping
&Coverage
);
125 /// Write out a source file view to the filesystem.
126 void writeSourceFileView(StringRef SourceFile
, CoverageMapping
*Coverage
,
127 CoveragePrinter
*Printer
, bool ShowFilenames
);
129 typedef llvm::function_ref
<int(int, const char **)> CommandLineParserType
;
131 int doShow(int argc
, const char **argv
,
132 CommandLineParserType commandLineParser
);
134 int doReport(int argc
, const char **argv
,
135 CommandLineParserType commandLineParser
);
137 int doExport(int argc
, const char **argv
,
138 CommandLineParserType commandLineParser
);
140 std::vector
<StringRef
> ObjectFilenames
;
141 CoverageViewOptions ViewOpts
;
142 CoverageFiltersMatchAll Filters
;
143 CoverageFilters IgnoreFilenameFilters
;
145 /// True if InputSourceFiles are provided.
146 bool HadSourceFiles
= false;
148 /// The path to the indexed profile.
149 std::string PGOFilename
;
151 /// A list of input source files.
152 std::vector
<std::string
> SourceFiles
;
154 /// In -path-equivalence mode, this maps the absolute paths from the coverage
155 /// mapping data to the input source files.
156 StringMap
<std::string
> RemappedFilenames
;
158 /// The coverage data path to be remapped from, and the source path to be
159 /// remapped to, when using -path-equivalence.
160 Optional
<std::pair
<std::string
, std::string
>> PathRemapping
;
162 /// File status cache used when finding the same file.
163 StringMap
<Optional
<sys::fs::file_status
>> FileStatusCache
;
165 /// The architecture the coverage mapping data targets.
166 std::vector
<StringRef
> CoverageArches
;
168 /// A cache for demangled symbols.
171 /// A lock which guards printing to stderr.
174 /// A container for input source file buffers.
175 std::mutex LoadedSourceFilesLock
;
176 std::vector
<std::pair
<std::string
, std::unique_ptr
<MemoryBuffer
>>>
179 /// Allowlist from -name-allowlist to be used for filtering.
180 std::unique_ptr
<SpecialCaseList
> NameAllowlist
;
184 static std::string
getErrorString(const Twine
&Message
, StringRef Whence
,
186 std::string Str
= (Warning
? "warning" : "error");
189 Str
+= Whence
.str() + ": ";
190 Str
+= Message
.str() + "\n";
194 void CodeCoverageTool::error(const Twine
&Message
, StringRef Whence
) {
195 std::unique_lock
<std::mutex
> Guard
{ErrsLock
};
196 ViewOpts
.colored_ostream(errs(), raw_ostream::RED
)
197 << getErrorString(Message
, Whence
, false);
200 void CodeCoverageTool::warning(const Twine
&Message
, StringRef Whence
) {
201 std::unique_lock
<std::mutex
> Guard
{ErrsLock
};
202 ViewOpts
.colored_ostream(errs(), raw_ostream::RED
)
203 << getErrorString(Message
, Whence
, true);
206 void CodeCoverageTool::addCollectedPath(const std::string
&Path
) {
207 SmallString
<128> EffectivePath(Path
);
208 if (std::error_code EC
= sys::fs::make_absolute(EffectivePath
)) {
209 error(EC
.message(), Path
);
212 sys::path::remove_dots(EffectivePath
, /*remove_dot_dot=*/true);
213 if (!IgnoreFilenameFilters
.matchesFilename(EffectivePath
))
214 SourceFiles
.emplace_back(EffectivePath
.str());
215 HadSourceFiles
= !SourceFiles
.empty();
218 void CodeCoverageTool::collectPaths(const std::string
&Path
) {
219 llvm::sys::fs::file_status Status
;
220 llvm::sys::fs::status(Path
, Status
);
221 if (!llvm::sys::fs::exists(Status
)) {
223 addCollectedPath(Path
);
225 warning("Source file doesn't exist, proceeded by ignoring it.", Path
);
229 if (llvm::sys::fs::is_regular_file(Status
)) {
230 addCollectedPath(Path
);
234 if (llvm::sys::fs::is_directory(Status
)) {
236 for (llvm::sys::fs::recursive_directory_iterator
F(Path
, EC
), E
;
237 F
!= E
; F
.increment(EC
)) {
239 auto Status
= F
->status();
241 warning(Status
.getError().message(), F
->path());
245 if (Status
->type() == llvm::sys::fs::file_type::regular_file
)
246 addCollectedPath(F
->path());
251 Optional
<sys::fs::file_status
>
252 CodeCoverageTool::getFileStatus(StringRef FilePath
) {
253 auto It
= FileStatusCache
.try_emplace(FilePath
);
254 auto &CachedStatus
= It
.first
->getValue();
258 sys::fs::file_status Status
;
259 if (!sys::fs::status(FilePath
, Status
))
260 CachedStatus
= Status
;
264 bool CodeCoverageTool::isEquivalentFile(StringRef FilePath1
,
265 StringRef FilePath2
) {
266 auto Status1
= getFileStatus(FilePath1
);
267 auto Status2
= getFileStatus(FilePath2
);
268 return Status1
&& Status2
&& sys::fs::equivalent(*Status1
, *Status2
);
271 ErrorOr
<const MemoryBuffer
&>
272 CodeCoverageTool::getSourceFile(StringRef SourceFile
) {
273 // If we've remapped filenames, look up the real location for this file.
274 std::unique_lock
<std::mutex
> Guard
{LoadedSourceFilesLock
};
275 if (!RemappedFilenames
.empty()) {
276 auto Loc
= RemappedFilenames
.find(SourceFile
);
277 if (Loc
!= RemappedFilenames
.end())
278 SourceFile
= Loc
->second
;
280 for (const auto &Files
: LoadedSourceFiles
)
281 if (isEquivalentFile(SourceFile
, Files
.first
))
282 return *Files
.second
;
283 auto Buffer
= MemoryBuffer::getFile(SourceFile
);
284 if (auto EC
= Buffer
.getError()) {
285 error(EC
.message(), SourceFile
);
288 LoadedSourceFiles
.emplace_back(std::string(SourceFile
),
289 std::move(Buffer
.get()));
290 return *LoadedSourceFiles
.back().second
;
293 void CodeCoverageTool::attachExpansionSubViews(
294 SourceCoverageView
&View
, ArrayRef
<ExpansionRecord
> Expansions
,
295 const CoverageMapping
&Coverage
) {
296 if (!ViewOpts
.ShowExpandedRegions
)
298 for (const auto &Expansion
: Expansions
) {
299 auto ExpansionCoverage
= Coverage
.getCoverageForExpansion(Expansion
);
300 if (ExpansionCoverage
.empty())
302 auto SourceBuffer
= getSourceFile(ExpansionCoverage
.getFilename());
306 auto SubViewBranches
= ExpansionCoverage
.getBranches();
307 auto SubViewExpansions
= ExpansionCoverage
.getExpansions();
309 SourceCoverageView::create(Expansion
.Function
.Name
, SourceBuffer
.get(),
310 ViewOpts
, std::move(ExpansionCoverage
));
311 attachExpansionSubViews(*SubView
, SubViewExpansions
, Coverage
);
312 attachBranchSubViews(*SubView
, Expansion
.Function
.Name
, SubViewBranches
,
313 SourceBuffer
.get(), ExpansionCoverage
);
314 View
.addExpansion(Expansion
.Region
, std::move(SubView
));
318 void CodeCoverageTool::attachBranchSubViews(SourceCoverageView
&View
,
319 StringRef SourceName
,
320 ArrayRef
<CountedRegion
> Branches
,
321 const MemoryBuffer
&File
,
322 CoverageData
&CoverageInfo
) {
323 if (!ViewOpts
.ShowBranchCounts
&& !ViewOpts
.ShowBranchPercents
)
326 const auto *NextBranch
= Branches
.begin();
327 const auto *EndBranch
= Branches
.end();
329 // Group branches that have the same line number into the same subview.
330 while (NextBranch
!= EndBranch
) {
331 std::vector
<CountedRegion
> ViewBranches
;
332 unsigned CurrentLine
= NextBranch
->LineStart
;
334 while (NextBranch
!= EndBranch
&& CurrentLine
== NextBranch
->LineStart
)
335 ViewBranches
.push_back(*NextBranch
++);
337 if (!ViewBranches
.empty()) {
338 auto SubView
= SourceCoverageView::create(SourceName
, File
, ViewOpts
,
339 std::move(CoverageInfo
));
340 View
.addBranch(CurrentLine
, ViewBranches
, std::move(SubView
));
345 std::unique_ptr
<SourceCoverageView
>
346 CodeCoverageTool::createFunctionView(const FunctionRecord
&Function
,
347 const CoverageMapping
&Coverage
) {
348 auto FunctionCoverage
= Coverage
.getCoverageForFunction(Function
);
349 if (FunctionCoverage
.empty())
351 auto SourceBuffer
= getSourceFile(FunctionCoverage
.getFilename());
355 auto Branches
= FunctionCoverage
.getBranches();
356 auto Expansions
= FunctionCoverage
.getExpansions();
357 auto View
= SourceCoverageView::create(DC
.demangle(Function
.Name
),
358 SourceBuffer
.get(), ViewOpts
,
359 std::move(FunctionCoverage
));
360 attachExpansionSubViews(*View
, Expansions
, Coverage
);
361 attachBranchSubViews(*View
, DC
.demangle(Function
.Name
), Branches
,
362 SourceBuffer
.get(), FunctionCoverage
);
367 std::unique_ptr
<SourceCoverageView
>
368 CodeCoverageTool::createSourceFileView(StringRef SourceFile
,
369 const CoverageMapping
&Coverage
) {
370 auto SourceBuffer
= getSourceFile(SourceFile
);
373 auto FileCoverage
= Coverage
.getCoverageForFile(SourceFile
);
374 if (FileCoverage
.empty())
377 auto Branches
= FileCoverage
.getBranches();
378 auto Expansions
= FileCoverage
.getExpansions();
379 auto View
= SourceCoverageView::create(SourceFile
, SourceBuffer
.get(),
380 ViewOpts
, std::move(FileCoverage
));
381 attachExpansionSubViews(*View
, Expansions
, Coverage
);
382 attachBranchSubViews(*View
, SourceFile
, Branches
, SourceBuffer
.get(),
384 if (!ViewOpts
.ShowFunctionInstantiations
)
387 for (const auto &Group
: Coverage
.getInstantiationGroups(SourceFile
)) {
388 // Skip functions which have a single instantiation.
389 if (Group
.size() < 2)
392 for (const FunctionRecord
*Function
: Group
.getInstantiations()) {
393 std::unique_ptr
<SourceCoverageView
> SubView
{nullptr};
395 StringRef Funcname
= DC
.demangle(Function
->Name
);
397 if (Function
->ExecutionCount
> 0) {
398 auto SubViewCoverage
= Coverage
.getCoverageForFunction(*Function
);
399 auto SubViewExpansions
= SubViewCoverage
.getExpansions();
400 auto SubViewBranches
= SubViewCoverage
.getBranches();
401 SubView
= SourceCoverageView::create(
402 Funcname
, SourceBuffer
.get(), ViewOpts
, std::move(SubViewCoverage
));
403 attachExpansionSubViews(*SubView
, SubViewExpansions
, Coverage
);
404 attachBranchSubViews(*SubView
, SourceFile
, SubViewBranches
,
405 SourceBuffer
.get(), SubViewCoverage
);
408 unsigned FileID
= Function
->CountedRegions
.front().FileID
;
410 for (const auto &CR
: Function
->CountedRegions
)
411 if (CR
.FileID
== FileID
)
412 Line
= std::max(CR
.LineEnd
, Line
);
413 View
->addInstantiation(Funcname
, Line
, std::move(SubView
));
419 static bool modifiedTimeGT(StringRef LHS
, StringRef RHS
) {
420 sys::fs::file_status Status
;
421 if (sys::fs::status(LHS
, Status
))
423 auto LHSTime
= Status
.getLastModificationTime();
424 if (sys::fs::status(RHS
, Status
))
426 auto RHSTime
= Status
.getLastModificationTime();
427 return LHSTime
> RHSTime
;
430 std::unique_ptr
<CoverageMapping
> CodeCoverageTool::load() {
431 for (StringRef ObjectFilename
: ObjectFilenames
)
432 if (modifiedTimeGT(ObjectFilename
, PGOFilename
))
433 warning("profile data may be out of date - object is newer",
436 CoverageMapping::load(ObjectFilenames
, PGOFilename
, CoverageArches
,
437 ViewOpts
.CompilationDirectory
);
438 if (Error E
= CoverageOrErr
.takeError()) {
439 error("Failed to load coverage: " + toString(std::move(E
)));
442 auto Coverage
= std::move(CoverageOrErr
.get());
443 unsigned Mismatched
= Coverage
->getMismatchedCount();
445 warning(Twine(Mismatched
) + " functions have mismatched data");
447 if (ViewOpts
.Debug
) {
448 for (const auto &HashMismatch
: Coverage
->getHashMismatches())
449 errs() << "hash-mismatch: "
450 << "No profile record found for '" << HashMismatch
.first
<< "'"
451 << " with hash = 0x" << Twine::utohexstr(HashMismatch
.second
)
456 remapPathNames(*Coverage
);
458 if (!SourceFiles
.empty())
459 removeUnmappedInputs(*Coverage
);
461 demangleSymbols(*Coverage
);
466 void CodeCoverageTool::remapPathNames(const CoverageMapping
&Coverage
) {
470 // Convert remapping paths to native paths with trailing seperators.
471 auto nativeWithTrailing
= [](StringRef Path
) -> std::string
{
474 SmallString
<128> NativePath
;
475 sys::path::native(Path
, NativePath
);
476 sys::path::remove_dots(NativePath
, true);
477 if (!NativePath
.empty() && !sys::path::is_separator(NativePath
.back()))
478 NativePath
+= sys::path::get_separator();
479 return NativePath
.c_str();
481 std::string RemapFrom
= nativeWithTrailing(PathRemapping
->first
);
482 std::string RemapTo
= nativeWithTrailing(PathRemapping
->second
);
484 // Create a mapping from coverage data file paths to local paths.
485 for (StringRef Filename
: Coverage
.getUniqueSourceFiles()) {
486 SmallString
<128> NativeFilename
;
487 sys::path::native(Filename
, NativeFilename
);
488 sys::path::remove_dots(NativeFilename
, true);
489 if (NativeFilename
.startswith(RemapFrom
)) {
490 RemappedFilenames
[Filename
] =
491 RemapTo
+ NativeFilename
.substr(RemapFrom
.size()).str();
495 // Convert input files from local paths to coverage data file paths.
496 StringMap
<std::string
> InvRemappedFilenames
;
497 for (const auto &RemappedFilename
: RemappedFilenames
)
498 InvRemappedFilenames
[RemappedFilename
.getValue()] =
499 std::string(RemappedFilename
.getKey());
501 for (std::string
&Filename
: SourceFiles
) {
502 SmallString
<128> NativeFilename
;
503 sys::path::native(Filename
, NativeFilename
);
504 auto CovFileName
= InvRemappedFilenames
.find(NativeFilename
);
505 if (CovFileName
!= InvRemappedFilenames
.end())
506 Filename
= CovFileName
->second
;
510 void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping
&Coverage
) {
511 std::vector
<StringRef
> CoveredFiles
= Coverage
.getUniqueSourceFiles();
513 // The user may have specified source files which aren't in the coverage
514 // mapping. Filter these files away.
515 llvm::erase_if(SourceFiles
, [&](const std::string
&SF
) {
516 return !std::binary_search(CoveredFiles
.begin(), CoveredFiles
.end(), SF
);
520 void CodeCoverageTool::demangleSymbols(const CoverageMapping
&Coverage
) {
521 if (!ViewOpts
.hasDemangler())
524 // Pass function names to the demangler in a temporary file.
526 SmallString
<256> InputPath
;
528 sys::fs::createTemporaryFile("demangle-in", "list", InputFD
, InputPath
);
530 error(InputPath
, EC
.message());
533 ToolOutputFile InputTOF
{InputPath
, InputFD
};
535 unsigned NumSymbols
= 0;
536 for (const auto &Function
: Coverage
.getCoveredFunctions()) {
537 InputTOF
.os() << Function
.Name
<< '\n';
540 InputTOF
.os().close();
542 // Use another temporary file to store the demangler's output.
544 SmallString
<256> OutputPath
;
545 EC
= sys::fs::createTemporaryFile("demangle-out", "list", OutputFD
,
548 error(OutputPath
, EC
.message());
551 ToolOutputFile OutputTOF
{OutputPath
, OutputFD
};
552 OutputTOF
.os().close();
554 // Invoke the demangler.
555 std::vector
<StringRef
> ArgsV
;
556 for (StringRef Arg
: ViewOpts
.DemanglerOpts
)
557 ArgsV
.push_back(Arg
);
558 Optional
<StringRef
> Redirects
[] = {InputPath
.str(), OutputPath
.str(), {""}};
560 int RC
= sys::ExecuteAndWait(ViewOpts
.DemanglerOpts
[0], ArgsV
,
561 /*env=*/None
, Redirects
, /*secondsToWait=*/0,
562 /*memoryLimit=*/0, &ErrMsg
);
564 error(ErrMsg
, ViewOpts
.DemanglerOpts
[0]);
568 // Parse the demangler's output.
569 auto BufOrError
= MemoryBuffer::getFile(OutputPath
);
571 error(OutputPath
, BufOrError
.getError().message());
575 std::unique_ptr
<MemoryBuffer
> DemanglerBuf
= std::move(*BufOrError
);
577 SmallVector
<StringRef
, 8> Symbols
;
578 StringRef DemanglerData
= DemanglerBuf
->getBuffer();
579 DemanglerData
.split(Symbols
, '\n', /*MaxSplit=*/NumSymbols
,
580 /*KeepEmpty=*/false);
581 if (Symbols
.size() != NumSymbols
) {
582 error("Demangler did not provide expected number of symbols");
586 // Cache the demangled names.
588 for (const auto &Function
: Coverage
.getCoveredFunctions())
589 // On Windows, lines in the demangler's output file end with "\r\n".
590 // Splitting by '\n' keeps '\r's, so cut them now.
591 DC
.DemangledNames
[Function
.Name
] = std::string(Symbols
[I
++].rtrim());
594 void CodeCoverageTool::writeSourceFileView(StringRef SourceFile
,
595 CoverageMapping
*Coverage
,
596 CoveragePrinter
*Printer
,
597 bool ShowFilenames
) {
598 auto View
= createSourceFileView(SourceFile
, *Coverage
);
600 warning("The file '" + SourceFile
+ "' isn't covered.");
604 auto OSOrErr
= Printer
->createViewFile(SourceFile
, /*InToplevel=*/false);
605 if (Error E
= OSOrErr
.takeError()) {
606 error("Could not create view file!", toString(std::move(E
)));
609 auto OS
= std::move(OSOrErr
.get());
611 View
->print(*OS
.get(), /*Wholefile=*/true,
612 /*ShowSourceName=*/ShowFilenames
,
613 /*ShowTitle=*/ViewOpts
.hasOutputDirectory());
614 Printer
->closeViewFile(std::move(OS
));
617 int CodeCoverageTool::run(Command Cmd
, int argc
, const char **argv
) {
618 cl::opt
<std::string
> CovFilename(
619 cl::Positional
, cl::desc("Covered executable or object file."));
621 cl::list
<std::string
> CovFilenames(
622 "object", cl::desc("Coverage executable or object file"));
624 cl::opt
<bool> DebugDumpCollectedObjects(
625 "dump-collected-objects", cl::Optional
, cl::Hidden
,
626 cl::desc("Show the collected coverage object files"));
628 cl::list
<std::string
> InputSourceFiles(cl::Positional
,
629 cl::desc("<Source files>"));
631 cl::opt
<bool> DebugDumpCollectedPaths(
632 "dump-collected-paths", cl::Optional
, cl::Hidden
,
633 cl::desc("Show the collected paths to source files"));
635 cl::opt
<std::string
, true> PGOFilename(
636 "instr-profile", cl::Required
, cl::location(this->PGOFilename
),
638 "File with the profile data obtained after an instrumented run"));
640 cl::list
<std::string
> Arches(
641 "arch", cl::desc("architectures of the coverage mapping binaries"));
643 cl::opt
<bool> DebugDump("dump", cl::Optional
,
644 cl::desc("Show internal debug dump"));
646 cl::opt
<CoverageViewOptions::OutputFormat
> Format(
647 "format", cl::desc("Output format for line-based coverage reports"),
648 cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text
, "text",
650 clEnumValN(CoverageViewOptions::OutputFormat::HTML
, "html",
652 clEnumValN(CoverageViewOptions::OutputFormat::Lcov
, "lcov",
653 "lcov tracefile output")),
654 cl::init(CoverageViewOptions::OutputFormat::Text
));
656 cl::opt
<std::string
> PathRemap(
657 "path-equivalence", cl::Optional
,
658 cl::desc("<from>,<to> Map coverage data paths to local source file "
661 cl::OptionCategory
FilteringCategory("Function filtering options");
663 cl::list
<std::string
> NameFilters(
664 "name", cl::Optional
,
665 cl::desc("Show code coverage only for functions with the given name"),
666 cl::cat(FilteringCategory
));
668 cl::list
<std::string
> NameFilterFiles(
669 "name-allowlist", cl::Optional
,
670 cl::desc("Show code coverage only for functions listed in the given "
672 cl::cat(FilteringCategory
));
674 cl::list
<std::string
> NameRegexFilters(
675 "name-regex", cl::Optional
,
676 cl::desc("Show code coverage only for functions that match the given "
677 "regular expression"),
678 cl::cat(FilteringCategory
));
680 cl::list
<std::string
> IgnoreFilenameRegexFilters(
681 "ignore-filename-regex", cl::Optional
,
682 cl::desc("Skip source code files with file paths that match the given "
683 "regular expression"),
684 cl::cat(FilteringCategory
));
686 cl::opt
<double> RegionCoverageLtFilter(
687 "region-coverage-lt", cl::Optional
,
688 cl::desc("Show code coverage only for functions with region coverage "
689 "less than the given threshold"),
690 cl::cat(FilteringCategory
));
692 cl::opt
<double> RegionCoverageGtFilter(
693 "region-coverage-gt", cl::Optional
,
694 cl::desc("Show code coverage only for functions with region coverage "
695 "greater than the given threshold"),
696 cl::cat(FilteringCategory
));
698 cl::opt
<double> LineCoverageLtFilter(
699 "line-coverage-lt", cl::Optional
,
700 cl::desc("Show code coverage only for functions with line coverage less "
701 "than the given threshold"),
702 cl::cat(FilteringCategory
));
704 cl::opt
<double> LineCoverageGtFilter(
705 "line-coverage-gt", cl::Optional
,
706 cl::desc("Show code coverage only for functions with line coverage "
707 "greater than the given threshold"),
708 cl::cat(FilteringCategory
));
710 cl::opt
<cl::boolOrDefault
> UseColor(
711 "use-color", cl::desc("Emit colored output (default=autodetect)"),
712 cl::init(cl::BOU_UNSET
));
714 cl::list
<std::string
> DemanglerOpts(
715 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
717 cl::opt
<bool> RegionSummary(
718 "show-region-summary", cl::Optional
,
719 cl::desc("Show region statistics in summary table"),
722 cl::opt
<bool> BranchSummary(
723 "show-branch-summary", cl::Optional
,
724 cl::desc("Show branch condition statistics in summary table"),
727 cl::opt
<bool> InstantiationSummary(
728 "show-instantiation-summary", cl::Optional
,
729 cl::desc("Show instantiation statistics in summary table"));
731 cl::opt
<bool> SummaryOnly(
732 "summary-only", cl::Optional
,
733 cl::desc("Export only summary information for each source file"));
735 cl::opt
<unsigned> NumThreads(
736 "num-threads", cl::init(0),
737 cl::desc("Number of merge threads to use (default: autodetect)"));
738 cl::alias
NumThreadsA("j", cl::desc("Alias for --num-threads"),
739 cl::aliasopt(NumThreads
));
741 cl::opt
<std::string
> CompilationDirectory(
742 "compilation-dir", cl::init(""),
743 cl::desc("Directory used as a base for relative coverage mapping paths"));
745 auto commandLineParser
= [&, this](int argc
, const char **argv
) -> int {
746 cl::ParseCommandLineOptions(argc
, argv
, "LLVM code coverage tool\n");
747 ViewOpts
.Debug
= DebugDump
;
749 if (!CovFilename
.empty())
750 ObjectFilenames
.emplace_back(CovFilename
);
751 for (const std::string
&Filename
: CovFilenames
)
752 ObjectFilenames
.emplace_back(Filename
);
753 if (ObjectFilenames
.empty()) {
754 errs() << "No filenames specified!\n";
758 if (DebugDumpCollectedObjects
) {
759 for (StringRef OF
: ObjectFilenames
)
760 outs() << OF
<< '\n';
764 ViewOpts
.Format
= Format
;
765 switch (ViewOpts
.Format
) {
766 case CoverageViewOptions::OutputFormat::Text
:
767 ViewOpts
.Colors
= UseColor
== cl::BOU_UNSET
768 ? sys::Process::StandardOutHasColors()
769 : UseColor
== cl::BOU_TRUE
;
771 case CoverageViewOptions::OutputFormat::HTML
:
772 if (UseColor
== cl::BOU_FALSE
)
773 errs() << "Color output cannot be disabled when generating html.\n";
774 ViewOpts
.Colors
= true;
776 case CoverageViewOptions::OutputFormat::Lcov
:
777 if (UseColor
== cl::BOU_TRUE
)
778 errs() << "Color output cannot be enabled when generating lcov.\n";
779 ViewOpts
.Colors
= false;
783 // If path-equivalence was given and is a comma seperated pair then set
785 if (!PathRemap
.empty()) {
786 auto EquivPair
= StringRef(PathRemap
).split(',');
787 if (EquivPair
.first
.empty() || EquivPair
.second
.empty()) {
788 error("invalid argument '" + PathRemap
+
789 "', must be in format 'from,to'",
790 "-path-equivalence");
794 PathRemapping
= {std::string(EquivPair
.first
),
795 std::string(EquivPair
.second
)};
798 // If a demangler is supplied, check if it exists and register it.
799 if (!DemanglerOpts
.empty()) {
800 auto DemanglerPathOrErr
= sys::findProgramByName(DemanglerOpts
[0]);
801 if (!DemanglerPathOrErr
) {
802 error("Could not find the demangler!",
803 DemanglerPathOrErr
.getError().message());
806 DemanglerOpts
[0] = *DemanglerPathOrErr
;
807 ViewOpts
.DemanglerOpts
.swap(DemanglerOpts
);
810 // Read in -name-allowlist files.
811 if (!NameFilterFiles
.empty()) {
812 std::string SpecialCaseListErr
;
813 NameAllowlist
= SpecialCaseList::create(
814 NameFilterFiles
, *vfs::getRealFileSystem(), SpecialCaseListErr
);
816 error(SpecialCaseListErr
);
819 // Create the function filters
820 if (!NameFilters
.empty() || NameAllowlist
|| !NameRegexFilters
.empty()) {
821 auto NameFilterer
= std::make_unique
<CoverageFilters
>();
822 for (const auto &Name
: NameFilters
)
823 NameFilterer
->push_back(std::make_unique
<NameCoverageFilter
>(Name
));
824 if (NameAllowlist
&& !NameFilterFiles
.empty())
825 NameFilterer
->push_back(
826 std::make_unique
<NameAllowlistCoverageFilter
>(*NameAllowlist
));
827 for (const auto &Regex
: NameRegexFilters
)
828 NameFilterer
->push_back(
829 std::make_unique
<NameRegexCoverageFilter
>(Regex
));
830 Filters
.push_back(std::move(NameFilterer
));
833 if (RegionCoverageLtFilter
.getNumOccurrences() ||
834 RegionCoverageGtFilter
.getNumOccurrences() ||
835 LineCoverageLtFilter
.getNumOccurrences() ||
836 LineCoverageGtFilter
.getNumOccurrences()) {
837 auto StatFilterer
= std::make_unique
<CoverageFilters
>();
838 if (RegionCoverageLtFilter
.getNumOccurrences())
839 StatFilterer
->push_back(std::make_unique
<RegionCoverageFilter
>(
840 RegionCoverageFilter::LessThan
, RegionCoverageLtFilter
));
841 if (RegionCoverageGtFilter
.getNumOccurrences())
842 StatFilterer
->push_back(std::make_unique
<RegionCoverageFilter
>(
843 RegionCoverageFilter::GreaterThan
, RegionCoverageGtFilter
));
844 if (LineCoverageLtFilter
.getNumOccurrences())
845 StatFilterer
->push_back(std::make_unique
<LineCoverageFilter
>(
846 LineCoverageFilter::LessThan
, LineCoverageLtFilter
));
847 if (LineCoverageGtFilter
.getNumOccurrences())
848 StatFilterer
->push_back(std::make_unique
<LineCoverageFilter
>(
849 RegionCoverageFilter::GreaterThan
, LineCoverageGtFilter
));
850 Filters
.push_back(std::move(StatFilterer
));
853 // Create the ignore filename filters.
854 for (const auto &RE
: IgnoreFilenameRegexFilters
)
855 IgnoreFilenameFilters
.push_back(
856 std::make_unique
<NameRegexCoverageFilter
>(RE
));
858 if (!Arches
.empty()) {
859 for (const std::string
&Arch
: Arches
) {
860 if (Triple(Arch
).getArch() == llvm::Triple::ArchType::UnknownArch
) {
861 error("Unknown architecture: " + Arch
);
864 CoverageArches
.emplace_back(Arch
);
866 if (CoverageArches
.size() == 1)
867 CoverageArches
.insert(CoverageArches
.end(), ObjectFilenames
.size() - 1,
869 if (CoverageArches
.size() != ObjectFilenames
.size()) {
870 error("Number of architectures doesn't match the number of objects");
875 // IgnoreFilenameFilters are applied even when InputSourceFiles specified.
876 for (const std::string
&File
: InputSourceFiles
)
879 if (DebugDumpCollectedPaths
) {
880 for (const std::string
&SF
: SourceFiles
)
881 outs() << SF
<< '\n';
885 ViewOpts
.ShowBranchSummary
= BranchSummary
;
886 ViewOpts
.ShowRegionSummary
= RegionSummary
;
887 ViewOpts
.ShowInstantiationSummary
= InstantiationSummary
;
888 ViewOpts
.ExportSummaryOnly
= SummaryOnly
;
889 ViewOpts
.NumThreads
= NumThreads
;
890 ViewOpts
.CompilationDirectory
= CompilationDirectory
;
897 return doShow(argc
, argv
, commandLineParser
);
899 return doReport(argc
, argv
, commandLineParser
);
901 return doExport(argc
, argv
, commandLineParser
);
906 int CodeCoverageTool::doShow(int argc
, const char **argv
,
907 CommandLineParserType commandLineParser
) {
909 cl::OptionCategory
ViewCategory("Viewing options");
911 cl::opt
<bool> ShowLineExecutionCounts(
912 "show-line-counts", cl::Optional
,
913 cl::desc("Show the execution counts for each line"), cl::init(true),
914 cl::cat(ViewCategory
));
916 cl::opt
<bool> ShowRegions(
917 "show-regions", cl::Optional
,
918 cl::desc("Show the execution counts for each region"),
919 cl::cat(ViewCategory
));
921 cl::opt
<CoverageViewOptions::BranchOutputType
> ShowBranches(
922 "show-branches", cl::Optional
,
923 cl::desc("Show coverage for branch conditions"), cl::cat(ViewCategory
),
924 cl::values(clEnumValN(CoverageViewOptions::BranchOutputType::Count
,
925 "count", "Show True/False counts"),
926 clEnumValN(CoverageViewOptions::BranchOutputType::Percent
,
927 "percent", "Show True/False percent")),
928 cl::init(CoverageViewOptions::BranchOutputType::Off
));
930 cl::opt
<bool> ShowBestLineRegionsCounts(
931 "show-line-counts-or-regions", cl::Optional
,
932 cl::desc("Show the execution counts for each line, or the execution "
933 "counts for each region on lines that have multiple regions"),
934 cl::cat(ViewCategory
));
936 cl::opt
<bool> ShowExpansions("show-expansions", cl::Optional
,
937 cl::desc("Show expanded source regions"),
938 cl::cat(ViewCategory
));
940 cl::opt
<bool> ShowInstantiations("show-instantiations", cl::Optional
,
941 cl::desc("Show function instantiations"),
942 cl::init(true), cl::cat(ViewCategory
));
944 cl::opt
<std::string
> ShowOutputDirectory(
945 "output-dir", cl::init(""),
946 cl::desc("Directory in which coverage information is written out"));
947 cl::alias
ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
948 cl::aliasopt(ShowOutputDirectory
));
950 cl::opt
<uint32_t> TabSize(
951 "tab-size", cl::init(2),
953 "Set tab expansion size for html coverage reports (default = 2)"));
955 cl::opt
<std::string
> ProjectTitle(
956 "project-title", cl::Optional
,
957 cl::desc("Set project title for the coverage report"));
959 cl::opt
<std::string
> CovWatermark(
960 "coverage-watermark", cl::Optional
,
961 cl::desc("<high>,<low> value indicate thresholds for high and low"
962 "coverage watermark"));
964 auto Err
= commandLineParser(argc
, argv
);
968 if (ViewOpts
.Format
== CoverageViewOptions::OutputFormat::Lcov
) {
969 error("Lcov format should be used with 'llvm-cov export'.");
973 ViewOpts
.HighCovWatermark
= 100.0;
974 ViewOpts
.LowCovWatermark
= 80.0;
975 if (!CovWatermark
.empty()) {
976 auto WaterMarkPair
= StringRef(CovWatermark
).split(',');
977 if (WaterMarkPair
.first
.empty() || WaterMarkPair
.second
.empty()) {
978 error("invalid argument '" + CovWatermark
+
979 "', must be in format 'high,low'",
980 "-coverage-watermark");
984 char *EndPointer
= nullptr;
985 ViewOpts
.HighCovWatermark
=
986 strtod(WaterMarkPair
.first
.begin(), &EndPointer
);
987 if (EndPointer
!= WaterMarkPair
.first
.end()) {
988 error("invalid number '" + WaterMarkPair
.first
+
989 "', invalid value for 'high'",
990 "-coverage-watermark");
994 ViewOpts
.LowCovWatermark
=
995 strtod(WaterMarkPair
.second
.begin(), &EndPointer
);
996 if (EndPointer
!= WaterMarkPair
.second
.end()) {
997 error("invalid number '" + WaterMarkPair
.second
+
998 "', invalid value for 'low'",
999 "-coverage-watermark");
1003 if (ViewOpts
.HighCovWatermark
> 100 || ViewOpts
.LowCovWatermark
< 0 ||
1004 ViewOpts
.HighCovWatermark
<= ViewOpts
.LowCovWatermark
) {
1006 "invalid number range '" + CovWatermark
+
1007 "', must be both high and low should be between 0-100, and high "
1009 "-coverage-watermark");
1014 ViewOpts
.ShowLineNumbers
= true;
1015 ViewOpts
.ShowLineStats
= ShowLineExecutionCounts
.getNumOccurrences() != 0 ||
1016 !ShowRegions
|| ShowBestLineRegionsCounts
;
1017 ViewOpts
.ShowRegionMarkers
= ShowRegions
|| ShowBestLineRegionsCounts
;
1018 ViewOpts
.ShowExpandedRegions
= ShowExpansions
;
1019 ViewOpts
.ShowBranchCounts
=
1020 ShowBranches
== CoverageViewOptions::BranchOutputType::Count
;
1021 ViewOpts
.ShowBranchPercents
=
1022 ShowBranches
== CoverageViewOptions::BranchOutputType::Percent
;
1023 ViewOpts
.ShowFunctionInstantiations
= ShowInstantiations
;
1024 ViewOpts
.ShowOutputDirectory
= ShowOutputDirectory
;
1025 ViewOpts
.TabSize
= TabSize
;
1026 ViewOpts
.ProjectTitle
= ProjectTitle
;
1028 if (ViewOpts
.hasOutputDirectory()) {
1029 if (auto E
= sys::fs::create_directories(ViewOpts
.ShowOutputDirectory
)) {
1030 error("Could not create output directory!", E
.message());
1035 sys::fs::file_status Status
;
1036 if (std::error_code EC
= sys::fs::status(PGOFilename
, Status
)) {
1037 error("Could not read profile data!" + EC
.message(), PGOFilename
);
1041 auto ModifiedTime
= Status
.getLastModificationTime();
1042 std::string ModifiedTimeStr
= to_string(ModifiedTime
);
1043 size_t found
= ModifiedTimeStr
.rfind(':');
1044 ViewOpts
.CreatedTimeStr
= (found
!= std::string::npos
)
1045 ? "Created: " + ModifiedTimeStr
.substr(0, found
)
1046 : "Created: " + ModifiedTimeStr
;
1048 auto Coverage
= load();
1052 auto Printer
= CoveragePrinter::create(ViewOpts
);
1054 if (SourceFiles
.empty() && !HadSourceFiles
)
1055 // Get the source files from the function coverage mapping.
1056 for (StringRef Filename
: Coverage
->getUniqueSourceFiles()) {
1057 if (!IgnoreFilenameFilters
.matchesFilename(Filename
))
1058 SourceFiles
.push_back(std::string(Filename
));
1061 // Create an index out of the source files.
1062 if (ViewOpts
.hasOutputDirectory()) {
1063 if (Error E
= Printer
->createIndexFile(SourceFiles
, *Coverage
, Filters
)) {
1064 error("Could not create index file!", toString(std::move(E
)));
1069 if (!Filters
.empty()) {
1070 // Build the map of filenames to functions.
1071 std::map
<llvm::StringRef
, std::vector
<const FunctionRecord
*>>
1072 FilenameFunctionMap
;
1073 for (const auto &SourceFile
: SourceFiles
)
1074 for (const auto &Function
: Coverage
->getCoveredFunctions(SourceFile
))
1075 if (Filters
.matches(*Coverage
.get(), Function
))
1076 FilenameFunctionMap
[SourceFile
].push_back(&Function
);
1078 // Only print filter matching functions for each file.
1079 for (const auto &FileFunc
: FilenameFunctionMap
) {
1080 StringRef File
= FileFunc
.first
;
1081 const auto &Functions
= FileFunc
.second
;
1083 auto OSOrErr
= Printer
->createViewFile(File
, /*InToplevel=*/false);
1084 if (Error E
= OSOrErr
.takeError()) {
1085 error("Could not create view file!", toString(std::move(E
)));
1088 auto OS
= std::move(OSOrErr
.get());
1090 bool ShowTitle
= ViewOpts
.hasOutputDirectory();
1091 for (const auto *Function
: Functions
) {
1092 auto FunctionView
= createFunctionView(*Function
, *Coverage
);
1093 if (!FunctionView
) {
1094 warning("Could not read coverage for '" + Function
->Name
+ "'.");
1097 FunctionView
->print(*OS
.get(), /*WholeFile=*/false,
1098 /*ShowSourceName=*/true, ShowTitle
);
1102 Printer
->closeViewFile(std::move(OS
));
1108 bool ShowFilenames
=
1109 (SourceFiles
.size() != 1) || ViewOpts
.hasOutputDirectory() ||
1110 (ViewOpts
.Format
== CoverageViewOptions::OutputFormat::HTML
);
1112 ThreadPoolStrategy S
= hardware_concurrency(ViewOpts
.NumThreads
);
1113 if (ViewOpts
.NumThreads
== 0) {
1114 // If NumThreads is not specified, create one thread for each input, up to
1115 // the number of hardware cores.
1116 S
= heavyweight_hardware_concurrency(SourceFiles
.size());
1120 if (!ViewOpts
.hasOutputDirectory() || S
.ThreadsRequested
== 1) {
1121 for (const std::string
&SourceFile
: SourceFiles
)
1122 writeSourceFileView(SourceFile
, Coverage
.get(), Printer
.get(),
1125 // In -output-dir mode, it's safe to use multiple threads to print files.
1127 for (const std::string
&SourceFile
: SourceFiles
)
1128 Pool
.async(&CodeCoverageTool::writeSourceFileView
, this, SourceFile
,
1129 Coverage
.get(), Printer
.get(), ShowFilenames
);
1136 int CodeCoverageTool::doReport(int argc
, const char **argv
,
1137 CommandLineParserType commandLineParser
) {
1138 cl::opt
<bool> ShowFunctionSummaries(
1139 "show-functions", cl::Optional
, cl::init(false),
1140 cl::desc("Show coverage summaries for each function"));
1142 auto Err
= commandLineParser(argc
, argv
);
1146 if (ViewOpts
.Format
== CoverageViewOptions::OutputFormat::HTML
) {
1147 error("HTML output for summary reports is not yet supported.");
1149 } else if (ViewOpts
.Format
== CoverageViewOptions::OutputFormat::Lcov
) {
1150 error("Lcov format should be used with 'llvm-cov export'.");
1154 sys::fs::file_status Status
;
1155 if (std::error_code EC
= sys::fs::status(PGOFilename
, Status
)) {
1156 error("Could not read profile data!" + EC
.message(), PGOFilename
);
1160 auto Coverage
= load();
1164 CoverageReport
Report(ViewOpts
, *Coverage
.get());
1165 if (!ShowFunctionSummaries
) {
1166 if (SourceFiles
.empty())
1167 Report
.renderFileReports(llvm::outs(), IgnoreFilenameFilters
);
1169 Report
.renderFileReports(llvm::outs(), SourceFiles
);
1171 if (SourceFiles
.empty()) {
1172 error("Source files must be specified when -show-functions=true is "
1177 Report
.renderFunctionReports(SourceFiles
, DC
, llvm::outs());
1182 int CodeCoverageTool::doExport(int argc
, const char **argv
,
1183 CommandLineParserType commandLineParser
) {
1185 cl::OptionCategory
ExportCategory("Exporting options");
1187 cl::opt
<bool> SkipExpansions("skip-expansions", cl::Optional
,
1188 cl::desc("Don't export expanded source regions"),
1189 cl::cat(ExportCategory
));
1191 cl::opt
<bool> SkipFunctions("skip-functions", cl::Optional
,
1192 cl::desc("Don't export per-function data"),
1193 cl::cat(ExportCategory
));
1195 auto Err
= commandLineParser(argc
, argv
);
1199 ViewOpts
.SkipExpansions
= SkipExpansions
;
1200 ViewOpts
.SkipFunctions
= SkipFunctions
;
1202 if (ViewOpts
.Format
!= CoverageViewOptions::OutputFormat::Text
&&
1203 ViewOpts
.Format
!= CoverageViewOptions::OutputFormat::Lcov
) {
1204 error("Coverage data can only be exported as textual JSON or an "
1209 sys::fs::file_status Status
;
1210 if (std::error_code EC
= sys::fs::status(PGOFilename
, Status
)) {
1211 error("Could not read profile data!" + EC
.message(), PGOFilename
);
1215 auto Coverage
= load();
1217 error("Could not load coverage information");
1221 std::unique_ptr
<CoverageExporter
> Exporter
;
1223 switch (ViewOpts
.Format
) {
1224 case CoverageViewOptions::OutputFormat::Text
:
1225 Exporter
= std::make_unique
<CoverageExporterJson
>(*Coverage
.get(),
1228 case CoverageViewOptions::OutputFormat::HTML
:
1229 // Unreachable because we should have gracefully terminated with an error
1231 llvm_unreachable("Export in HTML is not supported!");
1232 case CoverageViewOptions::OutputFormat::Lcov
:
1233 Exporter
= std::make_unique
<CoverageExporterLcov
>(*Coverage
.get(),
1238 if (SourceFiles
.empty())
1239 Exporter
->renderRoot(IgnoreFilenameFilters
);
1241 Exporter
->renderRoot(SourceFiles
);
1246 int showMain(int argc
, const char *argv
[]) {
1247 CodeCoverageTool Tool
;
1248 return Tool
.run(CodeCoverageTool::Show
, argc
, argv
);
1251 int reportMain(int argc
, const char *argv
[]) {
1252 CodeCoverageTool Tool
;
1253 return Tool
.run(CodeCoverageTool::Report
, argc
, argv
);
1256 int exportMain(int argc
, const char *argv
[]) {
1257 CodeCoverageTool Tool
;
1258 return Tool
.run(CodeCoverageTool::Export
, argc
, argv
);