xref: /llvm-project/llvm/tools/llvm-cov/CodeCoverage.cpp (revision 727549ee74db73b015efa411f4f8040704736ff5)
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 "CoverageFilters.h"
17 #include "CoverageReport.h"
18 #include "CoverageViewOptions.h"
19 #include "RenderingSupport.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/Coverage/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/Path.h"
30 #include "llvm/Support/Process.h"
31 #include <functional>
32 #include <system_error>
33 
34 using namespace llvm;
35 using namespace coverage;
36 
37 namespace {
38 /// \brief The implementation of the coverage tool.
39 class CodeCoverageTool {
40 public:
41   enum Command {
42     /// \brief The show command.
43     Show,
44     /// \brief The report command.
45     Report
46   };
47 
48   /// \brief Print the error message to the error output stream.
49   void error(const Twine &Message, StringRef Whence = "");
50 
51   /// \brief Return a memory buffer for the given source file.
52   ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
53 
54   /// \brief Create source views for the expansions of the view.
55   void attachExpansionSubViews(SourceCoverageView &View,
56                                ArrayRef<ExpansionRecord> Expansions,
57                                CoverageMapping &Coverage);
58 
59   /// \brief Create the source view of a particular function.
60   std::unique_ptr<SourceCoverageView>
61   createFunctionView(const FunctionRecord &Function, CoverageMapping &Coverage);
62 
63   /// \brief Create the main source view of a particular source file.
64   std::unique_ptr<SourceCoverageView>
65   createSourceFileView(StringRef SourceFile, CoverageMapping &Coverage);
66 
67   /// \brief Load the coverage mapping data. Return true if an error occured.
68   std::unique_ptr<CoverageMapping> load();
69 
70   int run(Command Cmd, int argc, const char **argv);
71 
72   typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
73 
74   int show(int argc, const char **argv,
75            CommandLineParserType commandLineParser);
76 
77   int report(int argc, const char **argv,
78              CommandLineParserType commandLineParser);
79 
80   std::string ObjectFilename;
81   CoverageViewOptions ViewOpts;
82   std::string PGOFilename;
83   CoverageFiltersMatchAll Filters;
84   std::vector<std::string> SourceFiles;
85   std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
86       LoadedSourceFiles;
87   bool CompareFilenamesOnly;
88   StringMap<std::string> RemappedFilenames;
89   std::string CoverageArch;
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.emplace_back(SourceFile, std::move(Buffer.get()));
117   return *LoadedSourceFiles.back().second;
118 }
119 
120 void
121 CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View,
122                                           ArrayRef<ExpansionRecord> Expansions,
123                                           CoverageMapping &Coverage) {
124   if (!ViewOpts.ShowExpandedRegions)
125     return;
126   for (const auto &Expansion : Expansions) {
127     auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
128     if (ExpansionCoverage.empty())
129       continue;
130     auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
131     if (!SourceBuffer)
132       continue;
133 
134     auto SubViewExpansions = ExpansionCoverage.getExpansions();
135     auto SubView =
136         SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
137                                    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 = SourceCoverageView::create(Function.Name, SourceBuffer.get(),
155                                          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 = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
173                                          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 =
180         SourceCoverageView::create(Function->Name, SourceBuffer.get(), ViewOpts,
181                                    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 static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
197   sys::fs::file_status Status;
198   if (sys::fs::status(LHS, Status))
199     return false;
200   auto LHSTime = Status.getLastModificationTime();
201   if (sys::fs::status(RHS, Status))
202     return false;
203   auto RHSTime = Status.getLastModificationTime();
204   return LHSTime > RHSTime;
205 }
206 
207 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
208   if (modifiedTimeGT(ObjectFilename, PGOFilename))
209     errs() << "warning: profile data may be out of date - object is newer\n";
210   auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename,
211                                              CoverageArch);
212   if (Error E = CoverageOrErr.takeError()) {
213     colored_ostream(errs(), raw_ostream::RED)
214         << "error: Failed to load coverage: " << toString(std::move(E)) << "\n";
215     return nullptr;
216   }
217   auto Coverage = std::move(CoverageOrErr.get());
218   unsigned Mismatched = Coverage->getMismatchedCount();
219   if (Mismatched) {
220     colored_ostream(errs(), raw_ostream::RED)
221         << "warning: " << Mismatched << " functions have mismatched data. ";
222     errs() << "\n";
223   }
224 
225   if (CompareFilenamesOnly) {
226     auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
227     for (auto &SF : SourceFiles) {
228       StringRef SFBase = sys::path::filename(SF);
229       for (const auto &CF : CoveredFiles)
230         if (SFBase == sys::path::filename(CF)) {
231           RemappedFilenames[CF] = SF;
232           SF = CF;
233           break;
234         }
235     }
236   }
237 
238   return Coverage;
239 }
240 
241 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
242   cl::opt<std::string, true> ObjectFilename(
243       cl::Positional, cl::Required, cl::location(this->ObjectFilename),
244       cl::desc("Covered executable or object file."));
245 
246   cl::list<std::string> InputSourceFiles(
247       cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
248 
249   cl::opt<std::string, true> PGOFilename(
250       "instr-profile", cl::Required, cl::location(this->PGOFilename),
251       cl::desc(
252           "File with the profile data obtained after an instrumented run"));
253 
254   cl::opt<std::string> Arch(
255       "arch", cl::desc("architecture of the coverage mapping binary"));
256 
257   cl::opt<bool> DebugDump("dump", cl::Optional,
258                           cl::desc("Show internal debug dump"));
259 
260   cl::opt<bool> FilenameEquivalence(
261       "filename-equivalence", cl::Optional,
262       cl::desc("Treat source files as equivalent to paths in the coverage data "
263                "when the file names match, even if the full paths do not"));
264 
265   cl::OptionCategory FilteringCategory("Function filtering options");
266 
267   cl::list<std::string> NameFilters(
268       "name", cl::Optional,
269       cl::desc("Show code coverage only for functions with the given name"),
270       cl::ZeroOrMore, cl::cat(FilteringCategory));
271 
272   cl::list<std::string> NameRegexFilters(
273       "name-regex", cl::Optional,
274       cl::desc("Show code coverage only for functions that match the given "
275                "regular expression"),
276       cl::ZeroOrMore, cl::cat(FilteringCategory));
277 
278   cl::opt<double> RegionCoverageLtFilter(
279       "region-coverage-lt", cl::Optional,
280       cl::desc("Show code coverage only for functions with region coverage "
281                "less than the given threshold"),
282       cl::cat(FilteringCategory));
283 
284   cl::opt<double> RegionCoverageGtFilter(
285       "region-coverage-gt", cl::Optional,
286       cl::desc("Show code coverage only for functions with region coverage "
287                "greater than the given threshold"),
288       cl::cat(FilteringCategory));
289 
290   cl::opt<double> LineCoverageLtFilter(
291       "line-coverage-lt", cl::Optional,
292       cl::desc("Show code coverage only for functions with line coverage less "
293                "than the given threshold"),
294       cl::cat(FilteringCategory));
295 
296   cl::opt<double> LineCoverageGtFilter(
297       "line-coverage-gt", cl::Optional,
298       cl::desc("Show code coverage only for functions with line coverage "
299                "greater than the given threshold"),
300       cl::cat(FilteringCategory));
301 
302   cl::opt<cl::boolOrDefault> UseColor(
303       "use-color", cl::desc("Emit colored output (default=autodetect)"),
304       cl::init(cl::BOU_UNSET));
305 
306   auto commandLineParser = [&, this](int argc, const char **argv) -> int {
307     cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
308     ViewOpts.Debug = DebugDump;
309     CompareFilenamesOnly = FilenameEquivalence;
310 
311     ViewOpts.Colors = UseColor == cl::BOU_UNSET
312                           ? sys::Process::StandardOutHasColors()
313                           : UseColor == cl::BOU_TRUE;
314 
315     // Create the function filters
316     if (!NameFilters.empty() || !NameRegexFilters.empty()) {
317       auto NameFilterer = new CoverageFilters;
318       for (const auto &Name : NameFilters)
319         NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
320       for (const auto &Regex : NameRegexFilters)
321         NameFilterer->push_back(
322             llvm::make_unique<NameRegexCoverageFilter>(Regex));
323       Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
324     }
325     if (RegionCoverageLtFilter.getNumOccurrences() ||
326         RegionCoverageGtFilter.getNumOccurrences() ||
327         LineCoverageLtFilter.getNumOccurrences() ||
328         LineCoverageGtFilter.getNumOccurrences()) {
329       auto StatFilterer = new CoverageFilters;
330       if (RegionCoverageLtFilter.getNumOccurrences())
331         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
332             RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
333       if (RegionCoverageGtFilter.getNumOccurrences())
334         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
335             RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
336       if (LineCoverageLtFilter.getNumOccurrences())
337         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
338             LineCoverageFilter::LessThan, LineCoverageLtFilter));
339       if (LineCoverageGtFilter.getNumOccurrences())
340         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
341             RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
342       Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
343     }
344 
345     if (!Arch.empty() &&
346         Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
347       errs() << "error: Unknown architecture: " << Arch << "\n";
348       return 1;
349     }
350     CoverageArch = Arch;
351 
352     for (const auto &File : InputSourceFiles) {
353       SmallString<128> Path(File);
354       if (!CompareFilenamesOnly)
355         if (std::error_code EC = sys::fs::make_absolute(Path)) {
356           errs() << "error: " << File << ": " << EC.message();
357           return 1;
358         }
359       SourceFiles.push_back(Path.str());
360     }
361     return 0;
362   };
363 
364   switch (Cmd) {
365   case Show:
366     return show(argc, argv, commandLineParser);
367   case Report:
368     return report(argc, argv, commandLineParser);
369   }
370   return 0;
371 }
372 
373 int CodeCoverageTool::show(int argc, const char **argv,
374                            CommandLineParserType commandLineParser) {
375 
376   cl::OptionCategory ViewCategory("Viewing options");
377 
378   cl::opt<bool> ShowLineExecutionCounts(
379       "show-line-counts", cl::Optional,
380       cl::desc("Show the execution counts for each line"), cl::init(true),
381       cl::cat(ViewCategory));
382 
383   cl::opt<bool> ShowRegions(
384       "show-regions", cl::Optional,
385       cl::desc("Show the execution counts for each region"),
386       cl::cat(ViewCategory));
387 
388   cl::opt<bool> ShowBestLineRegionsCounts(
389       "show-line-counts-or-regions", cl::Optional,
390       cl::desc("Show the execution counts for each line, or the execution "
391                "counts for each region on lines that have multiple regions"),
392       cl::cat(ViewCategory));
393 
394   cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
395                                cl::desc("Show expanded source regions"),
396                                cl::cat(ViewCategory));
397 
398   cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
399                                    cl::desc("Show function instantiations"),
400                                    cl::cat(ViewCategory));
401 
402   cl::opt<CoverageViewOptions::OutputFormat> ShowFormat(
403       "format", cl::desc("Output format for line-based coverage reports"),
404       cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
405                             "Text output"),
406                  clEnumValEnd),
407       cl::init(CoverageViewOptions::OutputFormat::Text));
408 
409   auto Err = commandLineParser(argc, argv);
410   if (Err)
411     return Err;
412 
413   ViewOpts.ShowLineNumbers = true;
414   ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
415                            !ShowRegions || ShowBestLineRegionsCounts;
416   ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
417   ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
418   ViewOpts.ShowExpandedRegions = ShowExpansions;
419   ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
420   ViewOpts.ShowFormat = ShowFormat;
421 
422   auto Coverage = load();
423   if (!Coverage)
424     return 1;
425 
426   if (!Filters.empty()) {
427     // Show functions
428     for (const auto &Function : Coverage->getCoveredFunctions()) {
429       if (!Filters.matches(Function))
430         continue;
431 
432       auto mainView = createFunctionView(Function, *Coverage);
433       if (!mainView) {
434         ViewOpts.colored_ostream(errs(), raw_ostream::RED)
435             << "warning: Could not read coverage for '" << Function.Name << "'."
436             << "\n";
437         continue;
438       }
439       mainView->print(outs(), /*WholeFile=*/false, /*ShowSourceName=*/true);
440       outs() << "\n";
441     }
442     return 0;
443   }
444 
445   // Show files
446   bool ShowFilenames = SourceFiles.size() != 1;
447 
448   if (SourceFiles.empty())
449     // Get the source files from the function coverage mapping
450     for (StringRef Filename : Coverage->getUniqueSourceFiles())
451       SourceFiles.push_back(Filename);
452 
453   for (const auto &SourceFile : SourceFiles) {
454     auto mainView = createSourceFileView(SourceFile, *Coverage);
455     if (!mainView) {
456       ViewOpts.colored_ostream(errs(), raw_ostream::RED)
457           << "warning: The file '" << SourceFile << "' isn't covered.";
458       errs() << "\n";
459       continue;
460     }
461 
462     mainView->print(outs(), /*Wholefile=*/true,
463                     /*ShowSourceName=*/ShowFilenames);
464     if (SourceFiles.size() > 1)
465       outs() << "\n";
466   }
467 
468   return 0;
469 }
470 
471 int CodeCoverageTool::report(int argc, const char **argv,
472                              CommandLineParserType commandLineParser) {
473   auto Err = commandLineParser(argc, argv);
474   if (Err)
475     return Err;
476 
477   auto Coverage = load();
478   if (!Coverage)
479     return 1;
480 
481   CoverageReport Report(ViewOpts, std::move(Coverage));
482   if (SourceFiles.empty())
483     Report.renderFileReports(llvm::outs());
484   else
485     Report.renderFunctionReports(SourceFiles, llvm::outs());
486   return 0;
487 }
488 
489 int showMain(int argc, const char *argv[]) {
490   CodeCoverageTool Tool;
491   return Tool.run(CodeCoverageTool::Show, argc, argv);
492 }
493 
494 int reportMain(int argc, const char *argv[]) {
495   CodeCoverageTool Tool;
496   return Tool.run(CodeCoverageTool::Report, argc, argv);
497 }
498