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