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