llvm-cov: Warn if object file is newer than profile
[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/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/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   llvm::Triple::ArchType 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.push_back(
120       std::make_pair(SourceFile, std::move(Buffer.get())));
121   return *LoadedSourceFiles.back().second;
122 }
123
124 void
125 CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View,
126                                           ArrayRef<ExpansionRecord> Expansions,
127                                           CoverageMapping &Coverage) {
128   if (!ViewOpts.ShowExpandedRegions)
129     return;
130   for (const auto &Expansion : Expansions) {
131     auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
132     if (ExpansionCoverage.empty())
133       continue;
134     auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
135     if (!SourceBuffer)
136       continue;
137
138     auto SubViewExpansions = ExpansionCoverage.getExpansions();
139     auto SubView = llvm::make_unique<SourceCoverageView>(
140         SourceBuffer.get(), ViewOpts, std::move(ExpansionCoverage));
141     attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
142     View.addExpansion(Expansion.Region, std::move(SubView));
143   }
144 }
145
146 std::unique_ptr<SourceCoverageView>
147 CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
148                                      CoverageMapping &Coverage) {
149   auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
150   if (FunctionCoverage.empty())
151     return nullptr;
152   auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
153   if (!SourceBuffer)
154     return nullptr;
155
156   auto Expansions = FunctionCoverage.getExpansions();
157   auto View = llvm::make_unique<SourceCoverageView>(
158       SourceBuffer.get(), ViewOpts, std::move(FunctionCoverage));
159   attachExpansionSubViews(*View, Expansions, Coverage);
160
161   return View;
162 }
163
164 std::unique_ptr<SourceCoverageView>
165 CodeCoverageTool::createSourceFileView(StringRef SourceFile,
166                                        CoverageMapping &Coverage) {
167   auto SourceBuffer = getSourceFile(SourceFile);
168   if (!SourceBuffer)
169     return nullptr;
170   auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
171   if (FileCoverage.empty())
172     return nullptr;
173
174   auto Expansions = FileCoverage.getExpansions();
175   auto View = llvm::make_unique<SourceCoverageView>(
176       SourceBuffer.get(), ViewOpts, std::move(FileCoverage));
177   attachExpansionSubViews(*View, Expansions, Coverage);
178
179   for (auto Function : Coverage.getInstantiations(SourceFile)) {
180     auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
181     auto SubViewExpansions = SubViewCoverage.getExpansions();
182     auto SubView = llvm::make_unique<SourceCoverageView>(
183         SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
184     attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
185
186     if (SubView) {
187       unsigned FileID = Function->CountedRegions.front().FileID;
188       unsigned Line = 0;
189       for (const auto &CR : Function->CountedRegions)
190         if (CR.FileID == FileID)
191           Line = std::max(CR.LineEnd, Line);
192       View->addInstantiation(Function->Name, Line, std::move(SubView));
193     }
194   }
195   return View;
196 }
197
198 static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
199   sys::fs::file_status Status;
200   if (sys::fs::status(LHS, Status))
201     return false;
202   auto LHSTime = Status.getLastModificationTime();
203   if (sys::fs::status(RHS, Status))
204     return false;
205   auto RHSTime = Status.getLastModificationTime();
206   return LHSTime > RHSTime;
207 }
208
209 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
210   if (modifiedTimeGT(ObjectFilename, PGOFilename))
211     errs() << "warning: profile data may be out of date - object is newer\n";
212   auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename,
213                                              CoverageArch);
214   if (std::error_code EC = CoverageOrErr.getError()) {
215     colored_ostream(errs(), raw_ostream::RED)
216         << "error: Failed to load coverage: " << EC.message();
217     errs() << "\n";
218     return nullptr;
219   }
220   auto Coverage = std::move(CoverageOrErr.get());
221   unsigned Mismatched = Coverage->getMismatchedCount();
222   if (Mismatched) {
223     colored_ostream(errs(), raw_ostream::RED)
224         << "warning: " << Mismatched << " functions have mismatched data. ";
225     errs() << "\n";
226   }
227
228   if (CompareFilenamesOnly) {
229     auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
230     for (auto &SF : SourceFiles) {
231       StringRef SFBase = sys::path::filename(SF);
232       for (const auto &CF : CoveredFiles)
233         if (SFBase == sys::path::filename(CF)) {
234           RemappedFilenames[CF] = SF;
235           SF = CF;
236           break;
237         }
238     }
239   }
240
241   return Coverage;
242 }
243
244 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
245   // Print a stack trace if we signal out.
246   sys::PrintStackTraceOnErrorSignal();
247   PrettyStackTraceProgram X(argc, argv);
248   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
249
250   cl::opt<std::string, true> ObjectFilename(
251       cl::Positional, cl::Required, cl::location(this->ObjectFilename),
252       cl::desc("Covered executable or object file."));
253
254   cl::list<std::string> InputSourceFiles(
255       cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
256
257   cl::opt<std::string, true> PGOFilename(
258       "instr-profile", cl::Required, cl::location(this->PGOFilename),
259       cl::desc(
260           "File with the profile data obtained after an instrumented run"));
261
262   cl::opt<std::string> Arch(
263       "arch", cl::desc("architecture of the coverage mapping binary"));
264
265   cl::opt<bool> DebugDump("dump", cl::Optional,
266                           cl::desc("Show internal debug dump"));
267
268   cl::opt<bool> FilenameEquivalence(
269       "filename-equivalence", cl::Optional,
270       cl::desc("Treat source files as equivalent to paths in the coverage data "
271                "when the file names match, even if the full paths do not"));
272
273   cl::OptionCategory FilteringCategory("Function filtering options");
274
275   cl::list<std::string> NameFilters(
276       "name", cl::Optional,
277       cl::desc("Show code coverage only for functions with the given name"),
278       cl::ZeroOrMore, cl::cat(FilteringCategory));
279
280   cl::list<std::string> NameRegexFilters(
281       "name-regex", cl::Optional,
282       cl::desc("Show code coverage only for functions that match the given "
283                "regular expression"),
284       cl::ZeroOrMore, cl::cat(FilteringCategory));
285
286   cl::opt<double> RegionCoverageLtFilter(
287       "region-coverage-lt", cl::Optional,
288       cl::desc("Show code coverage only for functions with region coverage "
289                "less than the given threshold"),
290       cl::cat(FilteringCategory));
291
292   cl::opt<double> RegionCoverageGtFilter(
293       "region-coverage-gt", cl::Optional,
294       cl::desc("Show code coverage only for functions with region coverage "
295                "greater than the given threshold"),
296       cl::cat(FilteringCategory));
297
298   cl::opt<double> LineCoverageLtFilter(
299       "line-coverage-lt", cl::Optional,
300       cl::desc("Show code coverage only for functions with line coverage less "
301                "than the given threshold"),
302       cl::cat(FilteringCategory));
303
304   cl::opt<double> LineCoverageGtFilter(
305       "line-coverage-gt", cl::Optional,
306       cl::desc("Show code coverage only for functions with line coverage "
307                "greater than the given threshold"),
308       cl::cat(FilteringCategory));
309
310   cl::opt<cl::boolOrDefault> UseColor(
311       "use-color", cl::desc("Emit colored output (default=autodetect)"),
312       cl::init(cl::BOU_UNSET));
313
314   auto commandLineParser = [&, this](int argc, const char **argv) -> int {
315     cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
316     ViewOpts.Debug = DebugDump;
317     CompareFilenamesOnly = FilenameEquivalence;
318
319     ViewOpts.Colors = UseColor == cl::BOU_UNSET
320                           ? sys::Process::StandardOutHasColors()
321                           : UseColor == cl::BOU_TRUE;
322
323     // Create the function filters
324     if (!NameFilters.empty() || !NameRegexFilters.empty()) {
325       auto NameFilterer = new CoverageFilters;
326       for (const auto &Name : NameFilters)
327         NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
328       for (const auto &Regex : NameRegexFilters)
329         NameFilterer->push_back(
330             llvm::make_unique<NameRegexCoverageFilter>(Regex));
331       Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
332     }
333     if (RegionCoverageLtFilter.getNumOccurrences() ||
334         RegionCoverageGtFilter.getNumOccurrences() ||
335         LineCoverageLtFilter.getNumOccurrences() ||
336         LineCoverageGtFilter.getNumOccurrences()) {
337       auto StatFilterer = new CoverageFilters;
338       if (RegionCoverageLtFilter.getNumOccurrences())
339         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
340             RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
341       if (RegionCoverageGtFilter.getNumOccurrences())
342         StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
343             RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
344       if (LineCoverageLtFilter.getNumOccurrences())
345         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
346             LineCoverageFilter::LessThan, LineCoverageLtFilter));
347       if (LineCoverageGtFilter.getNumOccurrences())
348         StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
349             RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
350       Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
351     }
352
353     if (Arch.empty())
354       CoverageArch = llvm::Triple::ArchType::UnknownArch;
355     else {
356       CoverageArch = Triple(Arch).getArch();
357       if (CoverageArch == llvm::Triple::ArchType::UnknownArch) {
358         errs() << "error: Unknown architecture: " << Arch << "\n";
359         return 1;
360       }
361     }
362
363     for (const auto &File : InputSourceFiles) {
364       SmallString<128> Path(File);
365       if (!CompareFilenamesOnly)
366         if (std::error_code EC = sys::fs::make_absolute(Path)) {
367           errs() << "error: " << File << ": " << EC.message();
368           return 1;
369         }
370       SourceFiles.push_back(Path.str());
371     }
372     return 0;
373   };
374
375   switch (Cmd) {
376   case Show:
377     return show(argc, argv, commandLineParser);
378   case Report:
379     return report(argc, argv, commandLineParser);
380   }
381   return 0;
382 }
383
384 int CodeCoverageTool::show(int argc, const char **argv,
385                            CommandLineParserType commandLineParser) {
386
387   cl::OptionCategory ViewCategory("Viewing options");
388
389   cl::opt<bool> ShowLineExecutionCounts(
390       "show-line-counts", cl::Optional,
391       cl::desc("Show the execution counts for each line"), cl::init(true),
392       cl::cat(ViewCategory));
393
394   cl::opt<bool> ShowRegions(
395       "show-regions", cl::Optional,
396       cl::desc("Show the execution counts for each region"),
397       cl::cat(ViewCategory));
398
399   cl::opt<bool> ShowBestLineRegionsCounts(
400       "show-line-counts-or-regions", cl::Optional,
401       cl::desc("Show the execution counts for each line, or the execution "
402                "counts for each region on lines that have multiple regions"),
403       cl::cat(ViewCategory));
404
405   cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
406                                cl::desc("Show expanded source regions"),
407                                cl::cat(ViewCategory));
408
409   cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
410                                    cl::desc("Show function instantiations"),
411                                    cl::cat(ViewCategory));
412
413   auto Err = commandLineParser(argc, argv);
414   if (Err)
415     return Err;
416
417   ViewOpts.ShowLineNumbers = true;
418   ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
419                            !ShowRegions || ShowBestLineRegionsCounts;
420   ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
421   ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
422   ViewOpts.ShowExpandedRegions = ShowExpansions;
423   ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
424
425   auto Coverage = load();
426   if (!Coverage)
427     return 1;
428
429   if (!Filters.empty()) {
430     // Show functions
431     for (const auto &Function : Coverage->getCoveredFunctions()) {
432       if (!Filters.matches(Function))
433         continue;
434
435       auto mainView = createFunctionView(Function, *Coverage);
436       if (!mainView) {
437         ViewOpts.colored_ostream(outs(), raw_ostream::RED)
438             << "warning: Could not read coverage for '" << Function.Name;
439         outs() << "\n";
440         continue;
441       }
442       ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name
443                                                           << ":";
444       outs() << "\n";
445       mainView->render(outs(), /*WholeFile=*/false);
446       outs() << "\n";
447     }
448     return 0;
449   }
450
451   // Show files
452   bool ShowFilenames = SourceFiles.size() != 1;
453
454   if (SourceFiles.empty())
455     // Get the source files from the function coverage mapping
456     for (StringRef Filename : Coverage->getUniqueSourceFiles())
457       SourceFiles.push_back(Filename);
458
459   for (const auto &SourceFile : SourceFiles) {
460     auto mainView = createSourceFileView(SourceFile, *Coverage);
461     if (!mainView) {
462       ViewOpts.colored_ostream(outs(), raw_ostream::RED)
463           << "warning: The file '" << SourceFile << "' isn't covered.";
464       outs() << "\n";
465       continue;
466     }
467
468     if (ShowFilenames) {
469       ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":";
470       outs() << "\n";
471     }
472     mainView->render(outs(), /*Wholefile=*/true);
473     if (SourceFiles.size() > 1)
474       outs() << "\n";
475   }
476
477   return 0;
478 }
479
480 int CodeCoverageTool::report(int argc, const char **argv,
481                              CommandLineParserType commandLineParser) {
482   auto Err = commandLineParser(argc, argv);
483   if (Err)
484     return Err;
485
486   auto Coverage = load();
487   if (!Coverage)
488     return 1;
489
490   CoverageReport Report(ViewOpts, std::move(Coverage));
491   if (SourceFiles.empty())
492     Report.renderFileReports(llvm::outs());
493   else
494     Report.renderFunctionReports(SourceFiles, llvm::outs());
495   return 0;
496 }
497
498 int showMain(int argc, const char *argv[]) {
499   CodeCoverageTool Tool;
500   return Tool.run(CodeCoverageTool::Show, argc, argv);
501 }
502
503 int reportMain(int argc, const char *argv[]) {
504   CodeCoverageTool Tool;
505   return Tool.run(CodeCoverageTool::Report, argc, argv);
506 }