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 "CoverageSummary.h"
20 #include "CoverageViewOptions.h"
21 #include "SourceCoverageView.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ProfileData/CoverageMapping.h"
25 #include "llvm/ProfileData/CoverageMappingReader.h"
26 #include "llvm/ProfileData/InstrProfReader.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/Format.h"
30 #include "llvm/Support/ManagedStatic.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/PrettyStackTrace.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;
95 void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
98 errs() << Whence << ": ";
99 errs() << Message << "\n";
102 ErrorOr<const MemoryBuffer &>
103 CodeCoverageTool::getSourceFile(StringRef SourceFile) {
104 // If we've remapped filenames, look up the real location for this file.
105 if (!RemappedFilenames.empty()) {
106 auto Loc = RemappedFilenames.find(SourceFile);
107 if (Loc != RemappedFilenames.end())
108 SourceFile = Loc->second;
110 for (const auto &Files : LoadedSourceFiles)
111 if (sys::fs::equivalent(SourceFile, Files.first))
112 return *Files.second;
113 auto Buffer = MemoryBuffer::getFile(SourceFile);
114 if (auto EC = Buffer.getError()) {
115 error(EC.message(), SourceFile);
118 LoadedSourceFiles.push_back(
119 std::make_pair(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 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
198 auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename);
199 if (std::error_code EC = CoverageOrErr.getError()) {
200 colored_ostream(errs(), raw_ostream::RED)
201 << "error: Failed to load coverage: " << EC.message();
205 auto Coverage = std::move(CoverageOrErr.get());
206 unsigned Mismatched = Coverage->getMismatchedCount();
208 colored_ostream(errs(), raw_ostream::RED)
209 << "warning: " << Mismatched << " functions have mismatched data. ";
213 if (CompareFilenamesOnly) {
214 auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
215 for (auto &SF : SourceFiles) {
216 StringRef SFBase = sys::path::filename(SF);
217 for (const auto &CF : CoveredFiles)
218 if (SFBase == sys::path::filename(CF)) {
219 RemappedFilenames[CF] = SF;
229 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
230 // Print a stack trace if we signal out.
231 sys::PrintStackTraceOnErrorSignal();
232 PrettyStackTraceProgram X(argc, argv);
233 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
235 cl::opt<std::string, true> ObjectFilename(
236 cl::Positional, cl::Required, cl::location(this->ObjectFilename),
237 cl::desc("Covered executable or object file."));
239 cl::list<std::string> InputSourceFiles(
240 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
242 cl::opt<std::string, true> PGOFilename(
243 "instr-profile", cl::Required, cl::location(this->PGOFilename),
245 "File with the profile data obtained after an instrumented run"));
247 cl::opt<bool> DebugDump("dump", cl::Optional,
248 cl::desc("Show internal debug dump"));
250 cl::opt<bool> FilenameEquivalence(
251 "filename-equivalence", cl::Optional,
252 cl::desc("Treat source files as equivalent to paths in the coverage data "
253 "when the file names match, even if the full paths do not"));
255 cl::OptionCategory FilteringCategory("Function filtering options");
257 cl::list<std::string> NameFilters(
258 "name", cl::Optional,
259 cl::desc("Show code coverage only for functions with the given name"),
260 cl::ZeroOrMore, cl::cat(FilteringCategory));
262 cl::list<std::string> NameRegexFilters(
263 "name-regex", cl::Optional,
264 cl::desc("Show code coverage only for functions that match the given "
265 "regular expression"),
266 cl::ZeroOrMore, cl::cat(FilteringCategory));
268 cl::opt<double> RegionCoverageLtFilter(
269 "region-coverage-lt", cl::Optional,
270 cl::desc("Show code coverage only for functions with region coverage "
271 "less than the given threshold"),
272 cl::cat(FilteringCategory));
274 cl::opt<double> RegionCoverageGtFilter(
275 "region-coverage-gt", cl::Optional,
276 cl::desc("Show code coverage only for functions with region coverage "
277 "greater than the given threshold"),
278 cl::cat(FilteringCategory));
280 cl::opt<double> LineCoverageLtFilter(
281 "line-coverage-lt", cl::Optional,
282 cl::desc("Show code coverage only for functions with line coverage less "
283 "than the given threshold"),
284 cl::cat(FilteringCategory));
286 cl::opt<double> LineCoverageGtFilter(
287 "line-coverage-gt", cl::Optional,
288 cl::desc("Show code coverage only for functions with line coverage "
289 "greater than the given threshold"),
290 cl::cat(FilteringCategory));
292 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
293 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
294 ViewOpts.Debug = DebugDump;
295 CompareFilenamesOnly = FilenameEquivalence;
297 // Create the function filters
298 if (!NameFilters.empty() || !NameRegexFilters.empty()) {
299 auto NameFilterer = new CoverageFilters;
300 for (const auto &Name : NameFilters)
301 NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
302 for (const auto &Regex : NameRegexFilters)
303 NameFilterer->push_back(
304 llvm::make_unique<NameRegexCoverageFilter>(Regex));
305 Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
307 if (RegionCoverageLtFilter.getNumOccurrences() ||
308 RegionCoverageGtFilter.getNumOccurrences() ||
309 LineCoverageLtFilter.getNumOccurrences() ||
310 LineCoverageGtFilter.getNumOccurrences()) {
311 auto StatFilterer = new CoverageFilters;
312 if (RegionCoverageLtFilter.getNumOccurrences())
313 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
314 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
315 if (RegionCoverageGtFilter.getNumOccurrences())
316 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
317 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
318 if (LineCoverageLtFilter.getNumOccurrences())
319 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
320 LineCoverageFilter::LessThan, LineCoverageLtFilter));
321 if (LineCoverageGtFilter.getNumOccurrences())
322 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
323 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
324 Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
327 for (const auto &File : InputSourceFiles) {
328 SmallString<128> Path(File);
329 if (std::error_code EC = sys::fs::make_absolute(Path)) {
330 errs() << "error: " << File << ": " << EC.message();
333 SourceFiles.push_back(Path.str());
340 return show(argc, argv, commandLineParser);
342 return report(argc, argv, commandLineParser);
347 int CodeCoverageTool::show(int argc, const char **argv,
348 CommandLineParserType commandLineParser) {
350 cl::OptionCategory ViewCategory("Viewing options");
352 cl::opt<bool> ShowLineExecutionCounts(
353 "show-line-counts", cl::Optional,
354 cl::desc("Show the execution counts for each line"), cl::init(true),
355 cl::cat(ViewCategory));
357 cl::opt<bool> ShowRegions(
358 "show-regions", cl::Optional,
359 cl::desc("Show the execution counts for each region"),
360 cl::cat(ViewCategory));
362 cl::opt<bool> ShowBestLineRegionsCounts(
363 "show-line-counts-or-regions", cl::Optional,
364 cl::desc("Show the execution counts for each line, or the execution "
365 "counts for each region on lines that have multiple regions"),
366 cl::cat(ViewCategory));
368 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
369 cl::desc("Show expanded source regions"),
370 cl::cat(ViewCategory));
372 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
373 cl::desc("Show function instantiations"),
374 cl::cat(ViewCategory));
376 cl::opt<bool> NoColors("no-colors", cl::Optional,
377 cl::desc("Don't show text colors"), cl::init(false),
378 cl::cat(ViewCategory));
380 auto Err = commandLineParser(argc, argv);
384 ViewOpts.Colors = !NoColors;
385 ViewOpts.ShowLineNumbers = true;
386 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
387 !ShowRegions || ShowBestLineRegionsCounts;
388 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
389 ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
390 ViewOpts.ShowExpandedRegions = ShowExpansions;
391 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
393 auto Coverage = load();
397 if (!Filters.empty()) {
399 for (const auto &Function : Coverage->getCoveredFunctions()) {
400 if (!Filters.matches(Function))
403 auto mainView = createFunctionView(Function, *Coverage);
405 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
406 << "warning: Could not read coverage for '" << Function.Name;
410 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name
413 mainView->render(outs(), /*WholeFile=*/false);
420 bool ShowFilenames = SourceFiles.size() != 1;
422 if (SourceFiles.empty())
423 // Get the source files from the function coverage mapping
424 for (StringRef Filename : Coverage->getUniqueSourceFiles())
425 SourceFiles.push_back(Filename);
427 for (const auto &SourceFile : SourceFiles) {
428 auto mainView = createSourceFileView(SourceFile, *Coverage);
430 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
431 << "warning: The file '" << SourceFile << "' isn't covered.";
437 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":";
440 mainView->render(outs(), /*Wholefile=*/true);
441 if (SourceFiles.size() > 1)
448 int CodeCoverageTool::report(int argc, const char **argv,
449 CommandLineParserType commandLineParser) {
450 cl::opt<bool> NoColors("no-colors", cl::Optional,
451 cl::desc("Don't show text colors"), cl::init(false));
453 auto Err = commandLineParser(argc, argv);
457 ViewOpts.Colors = !NoColors;
459 auto Coverage = load();
463 CoverageSummary Summarizer;
464 Summarizer.createSummaries(*Coverage);
465 CoverageReport Report(ViewOpts, Summarizer);
466 if (SourceFiles.empty() && Filters.empty()) {
467 Report.renderFileReports(llvm::outs());
471 Report.renderFunctionReports(llvm::outs());
475 int showMain(int argc, const char *argv[]) {
476 CodeCoverageTool Tool;
477 return Tool.run(CodeCoverageTool::Show, argc, argv);
480 int reportMain(int argc, const char *argv[]) {
481 CodeCoverageTool Tool;
482 return Tool.run(CodeCoverageTool::Report, argc, argv);