feaa3fcd857213a5b412ba2e749be78417989442
[oota-llvm.git] / tools / llvm-cov / CodeCoverage.cpp
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 "CoverageViewOptions.h"
20 #include "SourceCoverageView.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ProfileData/CoverageMapping.h"
24 #include "llvm/ProfileData/InstrProfReader.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/PrettyStackTrace.h"
31 #include "llvm/Support/Signals.h"
32 #include <functional>
33 #include <system_error>
34
35 using namespace llvm;
36 using namespace coverage;
37
38 namespace {
39 /// \brief The implementation of the coverage tool.
40 class CodeCoverageTool {
41 public:
42   enum Command {
43     /// \brief The show command.
44     Show,
45     /// \brief The report command.
46     Report
47   };
48
49   /// \brief Print the error message to the error output stream.
50   void error(const Twine &Message, StringRef Whence = "");
51
52   /// \brief Return a memory buffer for the given source file.
53   ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
54
55   /// \brief Create source views for the expansions of the view.
56   void attachExpansionSubViews(SourceCoverageView &View,
57                                ArrayRef<ExpansionRecord> Expansions,
58                                CoverageMapping &Coverage);
59
60   /// \brief Create the source view of a particular function.
61   std::unique_ptr<SourceCoverageView>
62   createFunctionView(const FunctionRecord &Function, CoverageMapping &Coverage);
63
64   /// \brief Create the main source view of a particular source file.
65   std::unique_ptr<SourceCoverageView>
66   createSourceFileView(StringRef SourceFile, CoverageMapping &Coverage);
67
68   /// \brief Load the coverage mapping data. Return true if an error occured.
69   std::unique_ptr<CoverageMapping> load();
70
71   int run(Command Cmd, int argc, const char **argv);
72
73   typedef std::function<int(int, const char **)> CommandLineParserType;
74
75   int show(int argc, const char **argv,
76            CommandLineParserType commandLineParser);
77
78   int report(int argc, const char **argv,
79              CommandLineParserType commandLineParser);
80
81   std::string ObjectFilename;
82   CoverageViewOptions ViewOpts;
83   std::string PGOFilename;
84   CoverageFiltersMatchAll Filters;
85   std::vector<std::string> SourceFiles;
86   std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
87       LoadedSourceFiles;
88   bool CompareFilenamesOnly;
89   StringMap<std::string> RemappedFilenames;
90 };
91 }
92
93 void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
94   errs() << "error: ";
95   if (!Whence.empty())
96     errs() << Whence << ": ";
97   errs() << Message << "\n";
98 }
99
100 ErrorOr<const MemoryBuffer &>
101 CodeCoverageTool::getSourceFile(StringRef SourceFile) {
102   // If we've remapped filenames, look up the real location for this file.
103   if (!RemappedFilenames.empty()) {
104     auto Loc = RemappedFilenames.find(SourceFile);
105     if (Loc != RemappedFilenames.end())
106       SourceFile = Loc->second;
107   }
108   for (const auto &Files : LoadedSourceFiles)
109     if (sys::fs::equivalent(SourceFile, Files.first))
110       return *Files.second;
111   auto Buffer = MemoryBuffer::getFile(SourceFile);
112   if (auto EC = Buffer.getError()) {
113     error(EC.message(), SourceFile);
114     return EC;
115   }
116   LoadedSourceFiles.push_back(
117       std::make_pair(SourceFile, std::move(Buffer.get())));
118   return *LoadedSourceFiles.back().second;
119 }
120
121 void
122 CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View,
123                                           ArrayRef<ExpansionRecord> Expansions,
124                                           CoverageMapping &Coverage) {
125   if (!ViewOpts.ShowExpandedRegions)
126     return;
127   for (const auto &Expansion : Expansions) {
128     auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
129     if (ExpansionCoverage.empty())
130       continue;
131     auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
132     if (!SourceBuffer)
133       continue;
134
135     auto SubViewExpansions = ExpansionCoverage.getExpansions();
136     auto SubView = llvm::make_unique<SourceCoverageView>(
137         SourceBuffer.get(), ViewOpts, std::move(ExpansionCoverage));
138     attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
139     View.addExpansion(Expansion.Region, std::move(SubView));
140   }
141 }
142
143 std::unique_ptr<SourceCoverageView>
144 CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
145                                      CoverageMapping &Coverage) {
146   auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
147   if (FunctionCoverage.empty())
148     return nullptr;
149   auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
150   if (!SourceBuffer)
151     return nullptr;
152
153   auto Expansions = FunctionCoverage.getExpansions();
154   auto View = llvm::make_unique<SourceCoverageView>(
155       SourceBuffer.get(), ViewOpts, std::move(FunctionCoverage));
156   attachExpansionSubViews(*View, Expansions, Coverage);
157
158   return View;
159 }
160
161 std::unique_ptr<SourceCoverageView>
162 CodeCoverageTool::createSourceFileView(StringRef SourceFile,
163                                        CoverageMapping &Coverage) {
164   auto SourceBuffer = getSourceFile(SourceFile);
165   if (!SourceBuffer)
166     return nullptr;
167   auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
168   if (FileCoverage.empty())
169     return nullptr;
170
171   auto Expansions = FileCoverage.getExpansions();
172   auto View = llvm::make_unique<SourceCoverageView>(
173       SourceBuffer.get(), ViewOpts, std::move(FileCoverage));
174   attachExpansionSubViews(*View, Expansions, Coverage);
175
176   for (auto Function : Coverage.getInstantiations(SourceFile)) {
177     auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
178     auto SubViewExpansions = SubViewCoverage.getExpansions();
179     auto SubView = llvm::make_unique<SourceCoverageView>(
180         SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
181     attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
182
183     if (SubView) {
184       unsigned FileID = Function->CountedRegions.front().FileID;
185       unsigned Line = 0;
186       for (const auto &CR : Function->CountedRegions)
187         if (CR.FileID == FileID)
188           Line = std::max(CR.LineEnd, Line);
189       View->addInstantiation(Function->Name, Line, std::move(SubView));
190     }
191   }
192   return View;
193 }
194
195 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
196   auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename);
197   if (std::error_code EC = CoverageOrErr.getError()) {
198     colored_ostream(errs(), raw_ostream::RED)
199         << "error: Failed to load coverage: " << EC.message();
200     errs() << "\n";
201     return nullptr;
202   }
203   auto Coverage = std::move(CoverageOrErr.get());
204   unsigned Mismatched = Coverage->getMismatchedCount();
205   if (Mismatched) {
206     colored_ostream(errs(), raw_ostream::RED)
207         << "warning: " << Mismatched << " functions have mismatched data. ";
208     errs() << "\n";
209   }
210
211   if (CompareFilenamesOnly) {
212     auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
213     for (auto &SF : SourceFiles) {
214       StringRef SFBase = sys::path::filename(SF);
215       for (const auto &CF : CoveredFiles)
216         if (SFBase == sys::path::filename(CF)) {
217           RemappedFilenames[CF] = SF;
218           SF = CF;
219           break;
220         }
221     }
222   }
223
224   return Coverage;
225 }
226
227 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
228   // Print a stack trace if we signal out.
229   sys::PrintStackTraceOnErrorSignal();
230   PrettyStackTraceProgram X(argc, argv);
231   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
232
233   cl::opt<std::string, true> ObjectFilename(
234       cl::Positional, cl::Required, cl::location(this->ObjectFilename),
235       cl::desc("Covered executable or object file."));
236
237   cl::list<std::string> InputSourceFiles(
238       cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
239
240   cl::opt<std::string, true> PGOFilename(
241       "instr-profile", cl::Required, cl::location(this->PGOFilename),
242       cl::desc(
243           "File with the profile data obtained after an instrumented run"));
244
245   cl::opt<bool> DebugDump("dump", cl::Optional,
246                           cl::desc("Show internal debug dump"));
247
248   cl::opt<bool> FilenameEquivalence(
249       "filename-equivalence", cl::Optional,
250       cl::desc("Treat source files as equivalent to paths in the coverage data "
251                "when the file names match, even if the full paths do not"));
252
253   cl::OptionCategory FilteringCategory("Function filtering options");
254
255   cl::list<std::string> NameFilters(
256       "name", cl::Optional,
257       cl::desc("Show code coverage only for functions with the given name"),
258       cl::ZeroOrMore, cl::cat(FilteringCategory));
259
260   cl::list<std::string> NameRegexFilters(
261       "name-regex", cl::Optional,
262       cl::desc("Show code coverage only for functions that match the given "
263                "regular expression"),
264       cl::ZeroOrMore, cl::cat(FilteringCategory));
265
266   cl::opt<double> RegionCoverageLtFilter(
267       "region-coverage-lt", cl::Optional,
268       cl::desc("Show code coverage only for functions with region coverage "
269                "less than the given threshold"),
270       cl::cat(FilteringCategory));
271
272   cl::opt<double> RegionCoverageGtFilter(
273       "region-coverage-gt", cl::Optional,
274       cl::desc("Show code coverage only for functions with region coverage "
275                "greater than the given threshold"),
276       cl::cat(FilteringCategory));
277
278   cl::opt<double> LineCoverageLtFilter(
279       "line-coverage-lt", cl::Optional,
280       cl::desc("Show code coverage only for functions with line coverage less "
281                "than the given threshold"),
282       cl::cat(FilteringCategory));
283
284   cl::opt<double> LineCoverageGtFilter(
285       "line-coverage-gt", cl::Optional,
286       cl::desc("Show code coverage only for functions with line coverage "
287                "greater than the given threshold"),
288       cl::cat(FilteringCategory));
289
290   auto commandLineParser = [&, this](int argc, const char **argv) -> int {
291     cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
292     ViewOpts.Debug = DebugDump;
293     CompareFilenamesOnly = FilenameEquivalence;
294
295     // Create the function filters
296     if (!NameFilters.empty() || !NameRegexFilters.empty()) {
297       auto NameFilterer = new CoverageFilters;
298       for (const auto &Name : NameFilters)
299         NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
300       for (const auto &Regex : NameRegexFilters)
301         NameFilterer->push_back(
302             llvm::make_unique<NameRegexCoverageFilter>(Regex));
303       Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
304     }
305     if (RegionCoverageLtFilter.getNumOccurrences() ||
306         RegionCoverageGtFilter.getNumOccurrences() ||
307         LineCoverageLtFilter.getNumOccurrences() ||
308         LineCoverageGtFilter.getNumOccurrences()) {
309       auto StatFilterer = new CoverageFilters;
310       if (RegionCoverageLtFilter.getNumOccurrences())
311         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
312             RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
313       if (RegionCoverageGtFilter.getNumOccurrences())
314         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
315             RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
316       if (LineCoverageLtFilter.getNumOccurrences())
317         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
318             LineCoverageFilter::LessThan, LineCoverageLtFilter));
319       if (LineCoverageGtFilter.getNumOccurrences())
320         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
321             RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
322       Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
323     }
324
325     for (const auto &File : InputSourceFiles) {
326       SmallString<128> Path(File);
327       if (std::error_code EC = sys::fs::make_absolute(Path)) {
328         errs() << "error: " << File << ": " << EC.message();
329         return 1;
330       }
331       SourceFiles.push_back(Path.str());
332     }
333     return 0;
334   };
335
336   switch (Cmd) {
337   case Show:
338     return show(argc, argv, commandLineParser);
339   case Report:
340     return report(argc, argv, commandLineParser);
341   }
342   return 0;
343 }
344
345 int CodeCoverageTool::show(int argc, const char **argv,
346                            CommandLineParserType commandLineParser) {
347
348   cl::OptionCategory ViewCategory("Viewing options");
349
350   cl::opt<bool> ShowLineExecutionCounts(
351       "show-line-counts", cl::Optional,
352       cl::desc("Show the execution counts for each line"), cl::init(true),
353       cl::cat(ViewCategory));
354
355   cl::opt<bool> ShowRegions(
356       "show-regions", cl::Optional,
357       cl::desc("Show the execution counts for each region"),
358       cl::cat(ViewCategory));
359
360   cl::opt<bool> ShowBestLineRegionsCounts(
361       "show-line-counts-or-regions", cl::Optional,
362       cl::desc("Show the execution counts for each line, or the execution "
363                "counts for each region on lines that have multiple regions"),
364       cl::cat(ViewCategory));
365
366   cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
367                                cl::desc("Show expanded source regions"),
368                                cl::cat(ViewCategory));
369
370   cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
371                                    cl::desc("Show function instantiations"),
372                                    cl::cat(ViewCategory));
373
374   cl::opt<bool> NoColors("no-colors", cl::Optional,
375                          cl::desc("Don't show text colors"), cl::init(false),
376                          cl::cat(ViewCategory));
377
378   auto Err = commandLineParser(argc, argv);
379   if (Err)
380     return Err;
381
382   ViewOpts.Colors = !NoColors;
383   ViewOpts.ShowLineNumbers = true;
384   ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
385                            !ShowRegions || ShowBestLineRegionsCounts;
386   ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
387   ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
388   ViewOpts.ShowExpandedRegions = ShowExpansions;
389   ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
390
391   auto Coverage = load();
392   if (!Coverage)
393     return 1;
394
395   if (!Filters.empty()) {
396     // Show functions
397     for (const auto &Function : Coverage->getCoveredFunctions()) {
398       if (!Filters.matches(Function))
399         continue;
400
401       auto mainView = createFunctionView(Function, *Coverage);
402       if (!mainView) {
403         ViewOpts.colored_ostream(outs(), raw_ostream::RED)
404             << "warning: Could not read coverage for '" << Function.Name;
405         outs() << "\n";
406         continue;
407       }
408       ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name
409                                                           << ":";
410       outs() << "\n";
411       mainView->render(outs(), /*WholeFile=*/false);
412       outs() << "\n";
413     }
414     return 0;
415   }
416
417   // Show files
418   bool ShowFilenames = SourceFiles.size() != 1;
419
420   if (SourceFiles.empty())
421     // Get the source files from the function coverage mapping
422     for (StringRef Filename : Coverage->getUniqueSourceFiles())
423       SourceFiles.push_back(Filename);
424
425   for (const auto &SourceFile : SourceFiles) {
426     auto mainView = createSourceFileView(SourceFile, *Coverage);
427     if (!mainView) {
428       ViewOpts.colored_ostream(outs(), raw_ostream::RED)
429           << "warning: The file '" << SourceFile << "' isn't covered.";
430       outs() << "\n";
431       continue;
432     }
433
434     if (ShowFilenames) {
435       ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":";
436       outs() << "\n";
437     }
438     mainView->render(outs(), /*Wholefile=*/true);
439     if (SourceFiles.size() > 1)
440       outs() << "\n";
441   }
442
443   return 0;
444 }
445
446 int CodeCoverageTool::report(int argc, const char **argv,
447                              CommandLineParserType commandLineParser) {
448   cl::opt<bool> NoColors("no-colors", cl::Optional,
449                          cl::desc("Don't show text colors"), cl::init(false));
450
451   auto Err = commandLineParser(argc, argv);
452   if (Err)
453     return Err;
454
455   ViewOpts.Colors = !NoColors;
456
457   auto Coverage = load();
458   if (!Coverage)
459     return 1;
460
461   CoverageReport Report(ViewOpts, std::move(Coverage));
462   if (SourceFiles.empty() && Filters.empty()) {
463     Report.renderFileReports(llvm::outs());
464     return 0;
465   }
466
467   Report.renderFunctionReports(llvm::outs());
468   return 0;
469 }
470
471 int showMain(int argc, const char *argv[]) {
472   CodeCoverageTool Tool;
473   return Tool.run(CodeCoverageTool::Show, argc, argv);
474 }
475
476 int reportMain(int argc, const char *argv[]) {
477   CodeCoverageTool Tool;
478   return Tool.run(CodeCoverageTool::Report, argc, argv);
479 }