1 //===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The 'CodeCoverageTool' class implements a command line tool to analyze and
11 // report coverage information using the profiling instrumentation and code
12 // coverage mapping.
13 //
14 //===----------------------------------------------------------------------===//
15
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"
34 #include <functional>
35 #include <system_error>
36
37 using namespace llvm;
38 using namespace coverage;
39
40 namespace {
41 /// \brief The implementation of the coverage tool.
42 class CodeCoverageTool {
43 public:
44 enum Command {
45 /// \brief The show command.
46 Show,
47 /// \brief The report command.
48 Report
49 };
50
51 /// \brief Print the error message to the error output stream.
52 void error(const Twine &Message, StringRef Whence = "");
53
54 /// \brief Return a memory buffer for the given source file.
55 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
56
57 /// \brief Create source views for the expansions of the view.
58 void attachExpansionSubViews(SourceCoverageView &View,
59 ArrayRef<ExpansionRecord> Expansions,
60 CoverageMapping &Coverage);
61
62 /// \brief Create the source view of a particular function.
63 std::unique_ptr<SourceCoverageView>
64 createFunctionView(const FunctionRecord &Function, CoverageMapping &Coverage);
65
66 /// \brief Create the main source view of a particular source file.
67 std::unique_ptr<SourceCoverageView>
68 createSourceFileView(StringRef SourceFile, CoverageMapping &Coverage);
69
70 /// \brief Load the coverage mapping data. Return true if an error occured.
71 std::unique_ptr<CoverageMapping> load();
72
73 int run(Command Cmd, int argc, const char **argv);
74
75 typedef std::function<int(int, const char **)> CommandLineParserType;
76
77 int show(int argc, const char **argv,
78 CommandLineParserType commandLineParser);
79
80 int report(int argc, const char **argv,
81 CommandLineParserType commandLineParser);
82
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>>>
89 LoadedSourceFiles;
90 bool CompareFilenamesOnly;
91 StringMap<std::string> RemappedFilenames;
92 };
93 }
94
error(const Twine & Message,StringRef Whence)95 void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
96 errs() << "error: ";
97 if (!Whence.empty())
98 errs() << Whence << ": ";
99 errs() << Message << "\n";
100 }
101
102 ErrorOr<const MemoryBuffer &>
getSourceFile(StringRef SourceFile)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;
109 }
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);
116 return EC;
117 }
118 LoadedSourceFiles.push_back(
119 std::make_pair(SourceFile, std::move(Buffer.get())));
120 return *LoadedSourceFiles.back().second;
121 }
122
123 void
attachExpansionSubViews(SourceCoverageView & View,ArrayRef<ExpansionRecord> Expansions,CoverageMapping & Coverage)124 CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View,
125 ArrayRef<ExpansionRecord> Expansions,
126 CoverageMapping &Coverage) {
127 if (!ViewOpts.ShowExpandedRegions)
128 return;
129 for (const auto &Expansion : Expansions) {
130 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
131 if (ExpansionCoverage.empty())
132 continue;
133 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
134 if (!SourceBuffer)
135 continue;
136
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));
142 }
143 }
144
145 std::unique_ptr<SourceCoverageView>
createFunctionView(const FunctionRecord & Function,CoverageMapping & Coverage)146 CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
147 CoverageMapping &Coverage) {
148 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
149 if (FunctionCoverage.empty())
150 return nullptr;
151 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
152 if (!SourceBuffer)
153 return nullptr;
154
155 auto Expansions = FunctionCoverage.getExpansions();
156 auto View = llvm::make_unique<SourceCoverageView>(
157 SourceBuffer.get(), ViewOpts, std::move(FunctionCoverage));
158 attachExpansionSubViews(*View, Expansions, Coverage);
159
160 return View;
161 }
162
163 std::unique_ptr<SourceCoverageView>
createSourceFileView(StringRef SourceFile,CoverageMapping & Coverage)164 CodeCoverageTool::createSourceFileView(StringRef SourceFile,
165 CoverageMapping &Coverage) {
166 auto SourceBuffer = getSourceFile(SourceFile);
167 if (!SourceBuffer)
168 return nullptr;
169 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
170 if (FileCoverage.empty())
171 return nullptr;
172
173 auto Expansions = FileCoverage.getExpansions();
174 auto View = llvm::make_unique<SourceCoverageView>(
175 SourceBuffer.get(), ViewOpts, std::move(FileCoverage));
176 attachExpansionSubViews(*View, Expansions, Coverage);
177
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);
184
185 if (SubView) {
186 unsigned FileID = Function->CountedRegions.front().FileID;
187 unsigned Line = 0;
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));
192 }
193 }
194 return View;
195 }
196
load()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();
202 errs() << "\n";
203 return nullptr;
204 }
205 auto Coverage = std::move(CoverageOrErr.get());
206 unsigned Mismatched = Coverage->getMismatchedCount();
207 if (Mismatched) {
208 colored_ostream(errs(), raw_ostream::RED)
209 << "warning: " << Mismatched << " functions have mismatched data. ";
210 errs() << "\n";
211 }
212
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;
220 SF = CF;
221 break;
222 }
223 }
224 }
225
226 return Coverage;
227 }
228
run(Command Cmd,int argc,const char ** argv)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.
234
235 cl::opt<std::string, true> ObjectFilename(
236 cl::Positional, cl::Required, cl::location(this->ObjectFilename),
237 cl::desc("Covered executable or object file."));
238
239 cl::list<std::string> InputSourceFiles(
240 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
241
242 cl::opt<std::string, true> PGOFilename(
243 "instr-profile", cl::Required, cl::location(this->PGOFilename),
244 cl::desc(
245 "File with the profile data obtained after an instrumented run"));
246
247 cl::opt<bool> DebugDump("dump", cl::Optional,
248 cl::desc("Show internal debug dump"));
249
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"));
254
255 cl::OptionCategory FilteringCategory("Function filtering options");
256
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));
261
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));
267
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));
273
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));
279
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));
285
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));
291
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;
296
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));
306 }
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));
325 }
326
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();
331 return 1;
332 }
333 SourceFiles.push_back(Path.str());
334 }
335 return 0;
336 };
337
338 switch (Cmd) {
339 case Show:
340 return show(argc, argv, commandLineParser);
341 case Report:
342 return report(argc, argv, commandLineParser);
343 }
344 return 0;
345 }
346
show(int argc,const char ** argv,CommandLineParserType commandLineParser)347 int CodeCoverageTool::show(int argc, const char **argv,
348 CommandLineParserType commandLineParser) {
349
350 cl::OptionCategory ViewCategory("Viewing options");
351
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));
356
357 cl::opt<bool> ShowRegions(
358 "show-regions", cl::Optional,
359 cl::desc("Show the execution counts for each region"),
360 cl::cat(ViewCategory));
361
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));
367
368 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
369 cl::desc("Show expanded source regions"),
370 cl::cat(ViewCategory));
371
372 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
373 cl::desc("Show function instantiations"),
374 cl::cat(ViewCategory));
375
376 cl::opt<bool> NoColors("no-colors", cl::Optional,
377 cl::desc("Don't show text colors"), cl::init(false),
378 cl::cat(ViewCategory));
379
380 auto Err = commandLineParser(argc, argv);
381 if (Err)
382 return Err;
383
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;
392
393 auto Coverage = load();
394 if (!Coverage)
395 return 1;
396
397 if (!Filters.empty()) {
398 // Show functions
399 for (const auto &Function : Coverage->getCoveredFunctions()) {
400 if (!Filters.matches(Function))
401 continue;
402
403 auto mainView = createFunctionView(Function, *Coverage);
404 if (!mainView) {
405 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
406 << "warning: Could not read coverage for '" << Function.Name;
407 outs() << "\n";
408 continue;
409 }
410 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name
411 << ":";
412 outs() << "\n";
413 mainView->render(outs(), /*WholeFile=*/false);
414 outs() << "\n";
415 }
416 return 0;
417 }
418
419 // Show files
420 bool ShowFilenames = SourceFiles.size() != 1;
421
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);
426
427 for (const auto &SourceFile : SourceFiles) {
428 auto mainView = createSourceFileView(SourceFile, *Coverage);
429 if (!mainView) {
430 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
431 << "warning: The file '" << SourceFile << "' isn't covered.";
432 outs() << "\n";
433 continue;
434 }
435
436 if (ShowFilenames) {
437 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":";
438 outs() << "\n";
439 }
440 mainView->render(outs(), /*Wholefile=*/true);
441 if (SourceFiles.size() > 1)
442 outs() << "\n";
443 }
444
445 return 0;
446 }
447
report(int argc,const char ** argv,CommandLineParserType commandLineParser)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));
452
453 auto Err = commandLineParser(argc, argv);
454 if (Err)
455 return Err;
456
457 ViewOpts.Colors = !NoColors;
458
459 auto Coverage = load();
460 if (!Coverage)
461 return 1;
462
463 CoverageSummary Summarizer;
464 Summarizer.createSummaries(*Coverage);
465 CoverageReport Report(ViewOpts, Summarizer);
466 if (SourceFiles.empty() && Filters.empty()) {
467 Report.renderFileReports(llvm::outs());
468 return 0;
469 }
470
471 Report.renderFunctionReports(llvm::outs());
472 return 0;
473 }
474
showMain(int argc,const char * argv[])475 int showMain(int argc, const char *argv[]) {
476 CodeCoverageTool Tool;
477 return Tool.run(CodeCoverageTool::Show, argc, argv);
478 }
479
reportMain(int argc,const char * argv[])480 int reportMain(int argc, const char *argv[]) {
481 CodeCoverageTool Tool;
482 return Tool.run(CodeCoverageTool::Report, argc, argv);
483 }
484