1 //===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The 'CodeCoverageTool' class implements a command line tool to analyze and
11 // report coverage information using the profiling instrumentation and code
14 //===----------------------------------------------------------------------===//
16 #include "RenderingSupport.h"
17 #include "CoverageFilters.h"
18 #include "CoverageReport.h"
19 #include "CoverageViewOptions.h"
20 #include "SourceCoverageView.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/ProfileData/CoverageMapping.h"
25 #include "llvm/ProfileData/InstrProfReader.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/Format.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/Path.h"
31 #include "llvm/Support/PrettyStackTrace.h"
32 #include "llvm/Support/Process.h"
33 #include "llvm/Support/Signals.h"
35 #include <system_error>
38 using namespace coverage;
41 /// \brief The implementation of the coverage tool.
42 class CodeCoverageTool {
45 /// \brief The show command.
47 /// \brief The report command.
51 /// \brief Print the error message to the error output stream.
52 void error(const Twine &Message, StringRef Whence = "");
54 /// \brief Return a memory buffer for the given source file.
55 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
57 /// \brief Create source views for the expansions of the view.
58 void attachExpansionSubViews(SourceCoverageView &View,
59 ArrayRef<ExpansionRecord> Expansions,
60 CoverageMapping &Coverage);
62 /// \brief Create the source view of a particular function.
63 std::unique_ptr<SourceCoverageView>
64 createFunctionView(const FunctionRecord &Function, CoverageMapping &Coverage);
66 /// \brief Create the main source view of a particular source file.
67 std::unique_ptr<SourceCoverageView>
68 createSourceFileView(StringRef SourceFile, CoverageMapping &Coverage);
70 /// \brief Load the coverage mapping data. Return true if an error occured.
71 std::unique_ptr<CoverageMapping> load();
73 int run(Command Cmd, int argc, const char **argv);
75 typedef std::function<int(int, const char **)> CommandLineParserType;
77 int show(int argc, const char **argv,
78 CommandLineParserType commandLineParser);
80 int report(int argc, const char **argv,
81 CommandLineParserType commandLineParser);
83 std::string ObjectFilename;
84 CoverageViewOptions ViewOpts;
85 std::string PGOFilename;
86 CoverageFiltersMatchAll Filters;
87 std::vector<std::string> SourceFiles;
88 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
90 bool CompareFilenamesOnly;
91 StringMap<std::string> RemappedFilenames;
92 std::string CoverageArch;
96 void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
99 errs() << Whence << ": ";
100 errs() << Message << "\n";
103 ErrorOr<const MemoryBuffer &>
104 CodeCoverageTool::getSourceFile(StringRef SourceFile) {
105 // If we've remapped filenames, look up the real location for this file.
106 if (!RemappedFilenames.empty()) {
107 auto Loc = RemappedFilenames.find(SourceFile);
108 if (Loc != RemappedFilenames.end())
109 SourceFile = Loc->second;
111 for (const auto &Files : LoadedSourceFiles)
112 if (sys::fs::equivalent(SourceFile, Files.first))
113 return *Files.second;
114 auto Buffer = MemoryBuffer::getFile(SourceFile);
115 if (auto EC = Buffer.getError()) {
116 error(EC.message(), SourceFile);
119 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
120 return *LoadedSourceFiles.back().second;
124 CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View,
125 ArrayRef<ExpansionRecord> Expansions,
126 CoverageMapping &Coverage) {
127 if (!ViewOpts.ShowExpandedRegions)
129 for (const auto &Expansion : Expansions) {
130 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
131 if (ExpansionCoverage.empty())
133 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
137 auto SubViewExpansions = ExpansionCoverage.getExpansions();
138 auto SubView = llvm::make_unique<SourceCoverageView>(
139 SourceBuffer.get(), ViewOpts, std::move(ExpansionCoverage));
140 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
141 View.addExpansion(Expansion.Region, std::move(SubView));
145 std::unique_ptr<SourceCoverageView>
146 CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
147 CoverageMapping &Coverage) {
148 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
149 if (FunctionCoverage.empty())
151 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
155 auto Expansions = FunctionCoverage.getExpansions();
156 auto View = llvm::make_unique<SourceCoverageView>(
157 SourceBuffer.get(), ViewOpts, std::move(FunctionCoverage));
158 attachExpansionSubViews(*View, Expansions, Coverage);
163 std::unique_ptr<SourceCoverageView>
164 CodeCoverageTool::createSourceFileView(StringRef SourceFile,
165 CoverageMapping &Coverage) {
166 auto SourceBuffer = getSourceFile(SourceFile);
169 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
170 if (FileCoverage.empty())
173 auto Expansions = FileCoverage.getExpansions();
174 auto View = llvm::make_unique<SourceCoverageView>(
175 SourceBuffer.get(), ViewOpts, std::move(FileCoverage));
176 attachExpansionSubViews(*View, Expansions, Coverage);
178 for (auto Function : Coverage.getInstantiations(SourceFile)) {
179 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
180 auto SubViewExpansions = SubViewCoverage.getExpansions();
181 auto SubView = llvm::make_unique<SourceCoverageView>(
182 SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
183 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
186 unsigned FileID = Function->CountedRegions.front().FileID;
188 for (const auto &CR : Function->CountedRegions)
189 if (CR.FileID == FileID)
190 Line = std::max(CR.LineEnd, Line);
191 View->addInstantiation(Function->Name, Line, std::move(SubView));
197 static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
198 sys::fs::file_status Status;
199 if (sys::fs::status(LHS, Status))
201 auto LHSTime = Status.getLastModificationTime();
202 if (sys::fs::status(RHS, Status))
204 auto RHSTime = Status.getLastModificationTime();
205 return LHSTime > RHSTime;
208 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
209 if (modifiedTimeGT(ObjectFilename, PGOFilename))
210 errs() << "warning: profile data may be out of date - object is newer\n";
211 auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename,
213 if (std::error_code EC = CoverageOrErr.getError()) {
214 colored_ostream(errs(), raw_ostream::RED)
215 << "error: Failed to load coverage: " << EC.message();
219 auto Coverage = std::move(CoverageOrErr.get());
220 unsigned Mismatched = Coverage->getMismatchedCount();
222 colored_ostream(errs(), raw_ostream::RED)
223 << "warning: " << Mismatched << " functions have mismatched data. ";
227 if (CompareFilenamesOnly) {
228 auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
229 for (auto &SF : SourceFiles) {
230 StringRef SFBase = sys::path::filename(SF);
231 for (const auto &CF : CoveredFiles)
232 if (SFBase == sys::path::filename(CF)) {
233 RemappedFilenames[CF] = SF;
243 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
244 // Print a stack trace if we signal out.
245 sys::PrintStackTraceOnErrorSignal();
246 PrettyStackTraceProgram X(argc, argv);
247 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
249 cl::opt<std::string, true> ObjectFilename(
250 cl::Positional, cl::Required, cl::location(this->ObjectFilename),
251 cl::desc("Covered executable or object file."));
253 cl::list<std::string> InputSourceFiles(
254 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
256 cl::opt<std::string, true> PGOFilename(
257 "instr-profile", cl::Required, cl::location(this->PGOFilename),
259 "File with the profile data obtained after an instrumented run"));
261 cl::opt<std::string> Arch(
262 "arch", cl::desc("architecture of the coverage mapping binary"));
264 cl::opt<bool> DebugDump("dump", cl::Optional,
265 cl::desc("Show internal debug dump"));
267 cl::opt<bool> FilenameEquivalence(
268 "filename-equivalence", cl::Optional,
269 cl::desc("Treat source files as equivalent to paths in the coverage data "
270 "when the file names match, even if the full paths do not"));
272 cl::OptionCategory FilteringCategory("Function filtering options");
274 cl::list<std::string> NameFilters(
275 "name", cl::Optional,
276 cl::desc("Show code coverage only for functions with the given name"),
277 cl::ZeroOrMore, cl::cat(FilteringCategory));
279 cl::list<std::string> NameRegexFilters(
280 "name-regex", cl::Optional,
281 cl::desc("Show code coverage only for functions that match the given "
282 "regular expression"),
283 cl::ZeroOrMore, cl::cat(FilteringCategory));
285 cl::opt<double> RegionCoverageLtFilter(
286 "region-coverage-lt", cl::Optional,
287 cl::desc("Show code coverage only for functions with region coverage "
288 "less than the given threshold"),
289 cl::cat(FilteringCategory));
291 cl::opt<double> RegionCoverageGtFilter(
292 "region-coverage-gt", cl::Optional,
293 cl::desc("Show code coverage only for functions with region coverage "
294 "greater than the given threshold"),
295 cl::cat(FilteringCategory));
297 cl::opt<double> LineCoverageLtFilter(
298 "line-coverage-lt", cl::Optional,
299 cl::desc("Show code coverage only for functions with line coverage less "
300 "than the given threshold"),
301 cl::cat(FilteringCategory));
303 cl::opt<double> LineCoverageGtFilter(
304 "line-coverage-gt", cl::Optional,
305 cl::desc("Show code coverage only for functions with line coverage "
306 "greater than the given threshold"),
307 cl::cat(FilteringCategory));
309 cl::opt<cl::boolOrDefault> UseColor(
310 "use-color", cl::desc("Emit colored output (default=autodetect)"),
311 cl::init(cl::BOU_UNSET));
313 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
314 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
315 ViewOpts.Debug = DebugDump;
316 CompareFilenamesOnly = FilenameEquivalence;
318 ViewOpts.Colors = UseColor == cl::BOU_UNSET
319 ? sys::Process::StandardOutHasColors()
320 : UseColor == cl::BOU_TRUE;
322 // Create the function filters
323 if (!NameFilters.empty() || !NameRegexFilters.empty()) {
324 auto NameFilterer = new CoverageFilters;
325 for (const auto &Name : NameFilters)
326 NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
327 for (const auto &Regex : NameRegexFilters)
328 NameFilterer->push_back(
329 llvm::make_unique<NameRegexCoverageFilter>(Regex));
330 Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
332 if (RegionCoverageLtFilter.getNumOccurrences() ||
333 RegionCoverageGtFilter.getNumOccurrences() ||
334 LineCoverageLtFilter.getNumOccurrences() ||
335 LineCoverageGtFilter.getNumOccurrences()) {
336 auto StatFilterer = new CoverageFilters;
337 if (RegionCoverageLtFilter.getNumOccurrences())
338 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
339 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
340 if (RegionCoverageGtFilter.getNumOccurrences())
341 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
342 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
343 if (LineCoverageLtFilter.getNumOccurrences())
344 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
345 LineCoverageFilter::LessThan, LineCoverageLtFilter));
346 if (LineCoverageGtFilter.getNumOccurrences())
347 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
348 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
349 Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
353 Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
354 errs() << "error: Unknown architecture: " << Arch << "\n";
359 for (const auto &File : InputSourceFiles) {
360 SmallString<128> Path(File);
361 if (!CompareFilenamesOnly)
362 if (std::error_code EC = sys::fs::make_absolute(Path)) {
363 errs() << "error: " << File << ": " << EC.message();
366 SourceFiles.push_back(Path.str());
373 return show(argc, argv, commandLineParser);
375 return report(argc, argv, commandLineParser);
380 int CodeCoverageTool::show(int argc, const char **argv,
381 CommandLineParserType commandLineParser) {
383 cl::OptionCategory ViewCategory("Viewing options");
385 cl::opt<bool> ShowLineExecutionCounts(
386 "show-line-counts", cl::Optional,
387 cl::desc("Show the execution counts for each line"), cl::init(true),
388 cl::cat(ViewCategory));
390 cl::opt<bool> ShowRegions(
391 "show-regions", cl::Optional,
392 cl::desc("Show the execution counts for each region"),
393 cl::cat(ViewCategory));
395 cl::opt<bool> ShowBestLineRegionsCounts(
396 "show-line-counts-or-regions", cl::Optional,
397 cl::desc("Show the execution counts for each line, or the execution "
398 "counts for each region on lines that have multiple regions"),
399 cl::cat(ViewCategory));
401 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
402 cl::desc("Show expanded source regions"),
403 cl::cat(ViewCategory));
405 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
406 cl::desc("Show function instantiations"),
407 cl::cat(ViewCategory));
409 auto Err = commandLineParser(argc, argv);
413 ViewOpts.ShowLineNumbers = true;
414 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
415 !ShowRegions || ShowBestLineRegionsCounts;
416 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
417 ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
418 ViewOpts.ShowExpandedRegions = ShowExpansions;
419 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
421 auto Coverage = load();
425 if (!Filters.empty()) {
427 for (const auto &Function : Coverage->getCoveredFunctions()) {
428 if (!Filters.matches(Function))
431 auto mainView = createFunctionView(Function, *Coverage);
433 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
434 << "warning: Could not read coverage for '" << Function.Name;
438 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name
441 mainView->render(outs(), /*WholeFile=*/false);
448 bool ShowFilenames = SourceFiles.size() != 1;
450 if (SourceFiles.empty())
451 // Get the source files from the function coverage mapping
452 for (StringRef Filename : Coverage->getUniqueSourceFiles())
453 SourceFiles.push_back(Filename);
455 for (const auto &SourceFile : SourceFiles) {
456 auto mainView = createSourceFileView(SourceFile, *Coverage);
458 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
459 << "warning: The file '" << SourceFile << "' isn't covered.";
465 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":";
468 mainView->render(outs(), /*Wholefile=*/true);
469 if (SourceFiles.size() > 1)
476 int CodeCoverageTool::report(int argc, const char **argv,
477 CommandLineParserType commandLineParser) {
478 auto Err = commandLineParser(argc, argv);
482 auto Coverage = load();
486 CoverageReport Report(ViewOpts, std::move(Coverage));
487 if (SourceFiles.empty())
488 Report.renderFileReports(llvm::outs());
490 Report.renderFunctionReports(SourceFiles, llvm::outs());
494 int showMain(int argc, const char *argv[]) {
495 CodeCoverageTool Tool;
496 return Tool.run(CodeCoverageTool::Show, argc, argv);
499 int reportMain(int argc, const char *argv[]) {
500 CodeCoverageTool Tool;
501 return Tool.run(CodeCoverageTool::Report, argc, argv);