xref: /llvm-project/llvm/tools/llvm-cov/CodeCoverage.cpp (revision 4379535e3f50f245e50f7820a80d81cc91b38c5f)
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/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/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   llvm::Triple::ArchType CoverageArch;
92 };
93 }
94 
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 &>
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
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>
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>
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 
197 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
198   auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename,
199                                              CoverageArch);
200   if (std::error_code EC = CoverageOrErr.getError()) {
201     colored_ostream(errs(), raw_ostream::RED)
202         << "error: Failed to load coverage: " << EC.message();
203     errs() << "\n";
204     return nullptr;
205   }
206   auto Coverage = std::move(CoverageOrErr.get());
207   unsigned Mismatched = Coverage->getMismatchedCount();
208   if (Mismatched) {
209     colored_ostream(errs(), raw_ostream::RED)
210         << "warning: " << Mismatched << " functions have mismatched data. ";
211     errs() << "\n";
212   }
213 
214   if (CompareFilenamesOnly) {
215     auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
216     for (auto &SF : SourceFiles) {
217       StringRef SFBase = sys::path::filename(SF);
218       for (const auto &CF : CoveredFiles)
219         if (SFBase == sys::path::filename(CF)) {
220           RemappedFilenames[CF] = SF;
221           SF = CF;
222           break;
223         }
224     }
225   }
226 
227   return Coverage;
228 }
229 
230 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
231   // Print a stack trace if we signal out.
232   sys::PrintStackTraceOnErrorSignal();
233   PrettyStackTraceProgram X(argc, argv);
234   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
235 
236   cl::opt<std::string, true> ObjectFilename(
237       cl::Positional, cl::Required, cl::location(this->ObjectFilename),
238       cl::desc("Covered executable or object file."));
239 
240   cl::list<std::string> InputSourceFiles(
241       cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
242 
243   cl::opt<std::string, true> PGOFilename(
244       "instr-profile", cl::Required, cl::location(this->PGOFilename),
245       cl::desc(
246           "File with the profile data obtained after an instrumented run"));
247 
248   cl::opt<std::string> Arch(
249       "arch", cl::desc("architecture of the coverage mapping binary"));
250 
251   cl::opt<bool> DebugDump("dump", cl::Optional,
252                           cl::desc("Show internal debug dump"));
253 
254   cl::opt<bool> FilenameEquivalence(
255       "filename-equivalence", cl::Optional,
256       cl::desc("Treat source files as equivalent to paths in the coverage data "
257                "when the file names match, even if the full paths do not"));
258 
259   cl::OptionCategory FilteringCategory("Function filtering options");
260 
261   cl::list<std::string> NameFilters(
262       "name", cl::Optional,
263       cl::desc("Show code coverage only for functions with the given name"),
264       cl::ZeroOrMore, cl::cat(FilteringCategory));
265 
266   cl::list<std::string> NameRegexFilters(
267       "name-regex", cl::Optional,
268       cl::desc("Show code coverage only for functions that match the given "
269                "regular expression"),
270       cl::ZeroOrMore, cl::cat(FilteringCategory));
271 
272   cl::opt<double> RegionCoverageLtFilter(
273       "region-coverage-lt", cl::Optional,
274       cl::desc("Show code coverage only for functions with region coverage "
275                "less than the given threshold"),
276       cl::cat(FilteringCategory));
277 
278   cl::opt<double> RegionCoverageGtFilter(
279       "region-coverage-gt", cl::Optional,
280       cl::desc("Show code coverage only for functions with region coverage "
281                "greater than the given threshold"),
282       cl::cat(FilteringCategory));
283 
284   cl::opt<double> LineCoverageLtFilter(
285       "line-coverage-lt", cl::Optional,
286       cl::desc("Show code coverage only for functions with line coverage less "
287                "than the given threshold"),
288       cl::cat(FilteringCategory));
289 
290   cl::opt<double> LineCoverageGtFilter(
291       "line-coverage-gt", cl::Optional,
292       cl::desc("Show code coverage only for functions with line coverage "
293                "greater than the given threshold"),
294       cl::cat(FilteringCategory));
295 
296   auto commandLineParser = [&, this](int argc, const char **argv) -> int {
297     cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
298     ViewOpts.Debug = DebugDump;
299     CompareFilenamesOnly = FilenameEquivalence;
300 
301     // Create the function filters
302     if (!NameFilters.empty() || !NameRegexFilters.empty()) {
303       auto NameFilterer = new CoverageFilters;
304       for (const auto &Name : NameFilters)
305         NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
306       for (const auto &Regex : NameRegexFilters)
307         NameFilterer->push_back(
308             llvm::make_unique<NameRegexCoverageFilter>(Regex));
309       Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
310     }
311     if (RegionCoverageLtFilter.getNumOccurrences() ||
312         RegionCoverageGtFilter.getNumOccurrences() ||
313         LineCoverageLtFilter.getNumOccurrences() ||
314         LineCoverageGtFilter.getNumOccurrences()) {
315       auto StatFilterer = new CoverageFilters;
316       if (RegionCoverageLtFilter.getNumOccurrences())
317         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
318             RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
319       if (RegionCoverageGtFilter.getNumOccurrences())
320         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
321             RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
322       if (LineCoverageLtFilter.getNumOccurrences())
323         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
324             LineCoverageFilter::LessThan, LineCoverageLtFilter));
325       if (LineCoverageGtFilter.getNumOccurrences())
326         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
327             RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
328       Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
329     }
330 
331     if (Arch.empty())
332       CoverageArch = llvm::Triple::ArchType::UnknownArch;
333     else {
334       CoverageArch = Triple(Arch).getArch();
335       if (CoverageArch == llvm::Triple::ArchType::UnknownArch) {
336         errs() << "error: Unknown architecture: " << Arch << "\n";
337         return 1;
338       }
339     }
340 
341     for (const auto &File : InputSourceFiles) {
342       SmallString<128> Path(File);
343       if (!CompareFilenamesOnly)
344         if (std::error_code EC = sys::fs::make_absolute(Path)) {
345           errs() << "error: " << File << ": " << EC.message();
346           return 1;
347         }
348       SourceFiles.push_back(Path.str());
349     }
350     return 0;
351   };
352 
353   switch (Cmd) {
354   case Show:
355     return show(argc, argv, commandLineParser);
356   case Report:
357     return report(argc, argv, commandLineParser);
358   }
359   return 0;
360 }
361 
362 int CodeCoverageTool::show(int argc, const char **argv,
363                            CommandLineParserType commandLineParser) {
364 
365   cl::OptionCategory ViewCategory("Viewing options");
366 
367   cl::opt<bool> ShowLineExecutionCounts(
368       "show-line-counts", cl::Optional,
369       cl::desc("Show the execution counts for each line"), cl::init(true),
370       cl::cat(ViewCategory));
371 
372   cl::opt<bool> ShowRegions(
373       "show-regions", cl::Optional,
374       cl::desc("Show the execution counts for each region"),
375       cl::cat(ViewCategory));
376 
377   cl::opt<bool> ShowBestLineRegionsCounts(
378       "show-line-counts-or-regions", cl::Optional,
379       cl::desc("Show the execution counts for each line, or the execution "
380                "counts for each region on lines that have multiple regions"),
381       cl::cat(ViewCategory));
382 
383   cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
384                                cl::desc("Show expanded source regions"),
385                                cl::cat(ViewCategory));
386 
387   cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
388                                    cl::desc("Show function instantiations"),
389                                    cl::cat(ViewCategory));
390 
391   cl::opt<bool> NoColors("no-colors", cl::Optional,
392                          cl::desc("Don't show text colors"), cl::init(false),
393                          cl::cat(ViewCategory));
394 
395   auto Err = commandLineParser(argc, argv);
396   if (Err)
397     return Err;
398 
399   ViewOpts.Colors = !NoColors;
400   ViewOpts.ShowLineNumbers = true;
401   ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
402                            !ShowRegions || ShowBestLineRegionsCounts;
403   ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
404   ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
405   ViewOpts.ShowExpandedRegions = ShowExpansions;
406   ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
407 
408   auto Coverage = load();
409   if (!Coverage)
410     return 1;
411 
412   if (!Filters.empty()) {
413     // Show functions
414     for (const auto &Function : Coverage->getCoveredFunctions()) {
415       if (!Filters.matches(Function))
416         continue;
417 
418       auto mainView = createFunctionView(Function, *Coverage);
419       if (!mainView) {
420         ViewOpts.colored_ostream(outs(), raw_ostream::RED)
421             << "warning: Could not read coverage for '" << Function.Name;
422         outs() << "\n";
423         continue;
424       }
425       ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name
426                                                           << ":";
427       outs() << "\n";
428       mainView->render(outs(), /*WholeFile=*/false);
429       outs() << "\n";
430     }
431     return 0;
432   }
433 
434   // Show files
435   bool ShowFilenames = SourceFiles.size() != 1;
436 
437   if (SourceFiles.empty())
438     // Get the source files from the function coverage mapping
439     for (StringRef Filename : Coverage->getUniqueSourceFiles())
440       SourceFiles.push_back(Filename);
441 
442   for (const auto &SourceFile : SourceFiles) {
443     auto mainView = createSourceFileView(SourceFile, *Coverage);
444     if (!mainView) {
445       ViewOpts.colored_ostream(outs(), raw_ostream::RED)
446           << "warning: The file '" << SourceFile << "' isn't covered.";
447       outs() << "\n";
448       continue;
449     }
450 
451     if (ShowFilenames) {
452       ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":";
453       outs() << "\n";
454     }
455     mainView->render(outs(), /*Wholefile=*/true);
456     if (SourceFiles.size() > 1)
457       outs() << "\n";
458   }
459 
460   return 0;
461 }
462 
463 int CodeCoverageTool::report(int argc, const char **argv,
464                              CommandLineParserType commandLineParser) {
465   cl::opt<bool> NoColors("no-colors", cl::Optional,
466                          cl::desc("Don't show text colors"), cl::init(false));
467 
468   auto Err = commandLineParser(argc, argv);
469   if (Err)
470     return Err;
471 
472   ViewOpts.Colors = !NoColors;
473 
474   auto Coverage = load();
475   if (!Coverage)
476     return 1;
477 
478   CoverageReport Report(ViewOpts, std::move(Coverage));
479   if (SourceFiles.empty())
480     Report.renderFileReports(llvm::outs());
481   else
482     Report.renderFunctionReports(SourceFiles, llvm::outs());
483   return 0;
484 }
485 
486 int showMain(int argc, const char *argv[]) {
487   CodeCoverageTool Tool;
488   return Tool.run(CodeCoverageTool::Show, argc, argv);
489 }
490 
491 int reportMain(int argc, const char *argv[]) {
492   CodeCoverageTool Tool;
493   return Tool.run(CodeCoverageTool::Report, argc, argv);
494 }
495