blob: 4ff53301881d1b55069d71a99ceb1abccf4d2b9c [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- 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
Alex Lorenze82d89c2014-08-22 22:56:03 +000016#include "RenderingSupport.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000017#include "CoverageFilters.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000018#include "CoverageReport.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000019#include "CoverageViewOptions.h"
20#include "SourceCoverageView.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000021#include "llvm/ADT/SmallString.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000022#include "llvm/ADT/StringRef.h"
Justin Bogner43795352015-03-11 02:30:51 +000023#include "llvm/ADT/Triple.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000024#include "llvm/ProfileData/CoverageMapping.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000025#include "llvm/ProfileData/InstrProfReader.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/FileSystem.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000028#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000029#include "llvm/Support/ManagedStatic.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000030#include "llvm/Support/Path.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000031#include "llvm/Support/PrettyStackTrace.h"
Justin Bognercfb53e42015-03-19 00:02:23 +000032#include "llvm/Support/Process.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000033#include "llvm/Support/Signals.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000034#include <functional>
Justin Bognere53be062014-09-09 05:32:18 +000035#include <system_error>
Alex Lorenze82d89c2014-08-22 22:56:03 +000036
37using namespace llvm;
38using namespace coverage;
39
40namespace {
Alex Lorenze82d89c2014-08-22 22:56:03 +000041/// \brief The implementation of the coverage tool.
42class CodeCoverageTool {
43public:
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
Justin Bogner953e2402014-09-20 15:31:56 +000057 /// \brief Create source views for the expansions of the view.
58 void attachExpansionSubViews(SourceCoverageView &View,
59 ArrayRef<ExpansionRecord> Expansions,
60 CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000061
Justin Bogner953e2402014-09-20 15:31:56 +000062 /// \brief Create the source view of a particular function.
Justin Bogner5a6edad2014-09-19 19:07:17 +000063 std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +000064 createFunctionView(const FunctionRecord &Function, CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000065
66 /// \brief Create the main source view of a particular source file.
Justin Bogner5a6edad2014-09-19 19:07:17 +000067 std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +000068 createSourceFileView(StringRef SourceFile, CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000069
70 /// \brief Load the coverage mapping data. Return true if an error occured.
Justin Bogner953e2402014-09-20 15:31:56 +000071 std::unique_ptr<CoverageMapping> load();
Alex Lorenze82d89c2014-08-22 22:56:03 +000072
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
Justin Bognerf6c50552014-10-30 20:51:24 +000083 std::string ObjectFilename;
Alex Lorenze82d89c2014-08-22 22:56:03 +000084 CoverageViewOptions ViewOpts;
Justin Bogner953e2402014-09-20 15:31:56 +000085 std::string PGOFilename;
Alex Lorenze82d89c2014-08-22 22:56:03 +000086 CoverageFiltersMatchAll Filters;
87 std::vector<std::string> SourceFiles;
88 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
89 LoadedSourceFiles;
Alex Lorenze82d89c2014-08-22 22:56:03 +000090 bool CompareFilenamesOnly;
Justin Bogner116c1662014-09-19 08:13:12 +000091 StringMap<std::string> RemappedFilenames;
Justin Bogner43795352015-03-11 02:30:51 +000092 llvm::Triple::ArchType CoverageArch;
Alex Lorenze82d89c2014-08-22 22:56:03 +000093};
94}
95
96void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
97 errs() << "error: ";
98 if (!Whence.empty())
99 errs() << Whence << ": ";
100 errs() << Message << "\n";
101}
102
103ErrorOr<const MemoryBuffer &>
104CodeCoverageTool::getSourceFile(StringRef SourceFile) {
Justin Bogner116c1662014-09-19 08:13:12 +0000105 // 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;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000110 }
Justin Bogner116c1662014-09-19 08:13:12 +0000111 for (const auto &Files : LoadedSourceFiles)
112 if (sys::fs::equivalent(SourceFile, Files.first))
113 return *Files.second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000114 auto Buffer = MemoryBuffer::getFile(SourceFile);
115 if (auto EC = Buffer.getError()) {
116 error(EC.message(), SourceFile);
117 return EC;
118 }
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000119 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000120 return *LoadedSourceFiles.back().second;
121}
122
Justin Bogner953e2402014-09-20 15:31:56 +0000123void
124CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View,
125 ArrayRef<ExpansionRecord> Expansions,
126 CoverageMapping &Coverage) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000127 if (!ViewOpts.ShowExpandedRegions)
128 return;
Justin Bogner953e2402014-09-20 15:31:56 +0000129 for (const auto &Expansion : Expansions) {
130 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
131 if (ExpansionCoverage.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000132 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000133 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
134 if (!SourceBuffer)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000135 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000136
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));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000142 }
143}
144
Justin Bogner5a6edad2014-09-19 19:07:17 +0000145std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +0000146CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
147 CoverageMapping &Coverage) {
148 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
149 if (FunctionCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000150 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000151 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
Justin Bogner5a6edad2014-09-19 19:07:17 +0000152 if (!SourceBuffer)
153 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000154
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;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000161}
162
Justin Bogner953e2402014-09-20 15:31:56 +0000163std::unique_ptr<SourceCoverageView>
164CodeCoverageTool::createSourceFileView(StringRef SourceFile,
165 CoverageMapping &Coverage) {
Justin Bogner5a6edad2014-09-19 19:07:17 +0000166 auto SourceBuffer = getSourceFile(SourceFile);
167 if (!SourceBuffer)
168 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000169 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
170 if (FileCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000171 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000172
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) {
Justin Bogner5e1400a2014-09-17 05:33:20 +0000186 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);
Justin Bogner953e2402014-09-20 15:31:56 +0000191 View->addInstantiation(Function->Name, Line, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000192 }
193 }
Justin Bogner5a6edad2014-09-19 19:07:17 +0000194 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000195}
196
Justin Bogner65337d12015-05-04 04:09:38 +0000197static 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
Justin Bogner953e2402014-09-20 15:31:56 +0000208std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
Justin Bogner65337d12015-05-04 04:09:38 +0000209 if (modifiedTimeGT(ObjectFilename, PGOFilename))
210 errs() << "warning: profile data may be out of date - object is newer\n";
Justin Bogner43795352015-03-11 02:30:51 +0000211 auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename,
212 CoverageArch);
Justin Bogner953e2402014-09-20 15:31:56 +0000213 if (std::error_code EC = CoverageOrErr.getError()) {
214 colored_ostream(errs(), raw_ostream::RED)
215 << "error: Failed to load coverage: " << EC.message();
216 errs() << "\n";
217 return nullptr;
218 }
219 auto Coverage = std::move(CoverageOrErr.get());
220 unsigned Mismatched = Coverage->getMismatchedCount();
221 if (Mismatched) {
222 colored_ostream(errs(), raw_ostream::RED)
223 << "warning: " << Mismatched << " functions have mismatched data. ";
224 errs() << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000225 }
Justin Bogner116c1662014-09-19 08:13:12 +0000226
227 if (CompareFilenamesOnly) {
Justin Bogner953e2402014-09-20 15:31:56 +0000228 auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
Justin Bogner116c1662014-09-19 08:13:12 +0000229 for (auto &SF : SourceFiles) {
230 StringRef SFBase = sys::path::filename(SF);
231 for (const auto &CF : CoveredFiles)
232 if (SFBase == sys::path::filename(CF)) {
233 RemappedFilenames[CF] = SF;
234 SF = CF;
235 break;
236 }
237 }
238 }
239
Justin Bogner953e2402014-09-20 15:31:56 +0000240 return Coverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000241}
242
243int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
244 // Print a stack trace if we signal out.
245 sys::PrintStackTraceOnErrorSignal();
246 PrettyStackTraceProgram X(argc, argv);
247 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
248
Justin Bognerf6c50552014-10-30 20:51:24 +0000249 cl::opt<std::string, true> ObjectFilename(
250 cl::Positional, cl::Required, cl::location(this->ObjectFilename),
251 cl::desc("Covered executable or object file."));
252
Alex Lorenze82d89c2014-08-22 22:56:03 +0000253 cl::list<std::string> InputSourceFiles(
254 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
255
Justin Bogner953e2402014-09-20 15:31:56 +0000256 cl::opt<std::string, true> PGOFilename(
257 "instr-profile", cl::Required, cl::location(this->PGOFilename),
Alex Lorenze82d89c2014-08-22 22:56:03 +0000258 cl::desc(
259 "File with the profile data obtained after an instrumented run"));
260
Justin Bogner43795352015-03-11 02:30:51 +0000261 cl::opt<std::string> Arch(
262 "arch", cl::desc("architecture of the coverage mapping binary"));
263
Alex Lorenze82d89c2014-08-22 22:56:03 +0000264 cl::opt<bool> DebugDump("dump", cl::Optional,
265 cl::desc("Show internal debug dump"));
266
267 cl::opt<bool> FilenameEquivalence(
268 "filename-equivalence", cl::Optional,
Justin Bogner116c1662014-09-19 08:13:12 +0000269 cl::desc("Treat source files as equivalent to paths in the coverage data "
270 "when the file names match, even if the full paths do not"));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000271
272 cl::OptionCategory FilteringCategory("Function filtering options");
273
274 cl::list<std::string> NameFilters(
275 "name", cl::Optional,
276 cl::desc("Show code coverage only for functions with the given name"),
277 cl::ZeroOrMore, cl::cat(FilteringCategory));
278
279 cl::list<std::string> NameRegexFilters(
280 "name-regex", cl::Optional,
281 cl::desc("Show code coverage only for functions that match the given "
282 "regular expression"),
283 cl::ZeroOrMore, cl::cat(FilteringCategory));
284
285 cl::opt<double> RegionCoverageLtFilter(
286 "region-coverage-lt", cl::Optional,
287 cl::desc("Show code coverage only for functions with region coverage "
288 "less than the given threshold"),
289 cl::cat(FilteringCategory));
290
291 cl::opt<double> RegionCoverageGtFilter(
292 "region-coverage-gt", cl::Optional,
293 cl::desc("Show code coverage only for functions with region coverage "
294 "greater than the given threshold"),
295 cl::cat(FilteringCategory));
296
297 cl::opt<double> LineCoverageLtFilter(
298 "line-coverage-lt", cl::Optional,
299 cl::desc("Show code coverage only for functions with line coverage less "
300 "than the given threshold"),
301 cl::cat(FilteringCategory));
302
303 cl::opt<double> LineCoverageGtFilter(
304 "line-coverage-gt", cl::Optional,
305 cl::desc("Show code coverage only for functions with line coverage "
306 "greater than the given threshold"),
307 cl::cat(FilteringCategory));
308
Justin Bogner9deb1d42015-03-19 04:45:16 +0000309 cl::opt<cl::boolOrDefault> UseColor(
310 "use-color", cl::desc("Emit colored output (default=autodetect)"),
311 cl::init(cl::BOU_UNSET));
Justin Bognercfb53e42015-03-19 00:02:23 +0000312
Alex Lorenze82d89c2014-08-22 22:56:03 +0000313 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
314 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
315 ViewOpts.Debug = DebugDump;
316 CompareFilenamesOnly = FilenameEquivalence;
317
Justin Bogner9deb1d42015-03-19 04:45:16 +0000318 ViewOpts.Colors = UseColor == cl::BOU_UNSET
319 ? sys::Process::StandardOutHasColors()
320 : UseColor == cl::BOU_TRUE;
Justin Bognercfb53e42015-03-19 00:02:23 +0000321
Alex Lorenze82d89c2014-08-22 22:56:03 +0000322 // Create the function filters
323 if (!NameFilters.empty() || !NameRegexFilters.empty()) {
324 auto NameFilterer = new CoverageFilters;
325 for (const auto &Name : NameFilters)
326 NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
327 for (const auto &Regex : NameRegexFilters)
328 NameFilterer->push_back(
329 llvm::make_unique<NameRegexCoverageFilter>(Regex));
330 Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
331 }
332 if (RegionCoverageLtFilter.getNumOccurrences() ||
333 RegionCoverageGtFilter.getNumOccurrences() ||
334 LineCoverageLtFilter.getNumOccurrences() ||
335 LineCoverageGtFilter.getNumOccurrences()) {
336 auto StatFilterer = new CoverageFilters;
337 if (RegionCoverageLtFilter.getNumOccurrences())
338 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
339 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
340 if (RegionCoverageGtFilter.getNumOccurrences())
341 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
342 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
343 if (LineCoverageLtFilter.getNumOccurrences())
344 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
345 LineCoverageFilter::LessThan, LineCoverageLtFilter));
346 if (LineCoverageGtFilter.getNumOccurrences())
347 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
348 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
349 Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
350 }
351
Justin Bogner43795352015-03-11 02:30:51 +0000352 if (Arch.empty())
353 CoverageArch = llvm::Triple::ArchType::UnknownArch;
354 else {
355 CoverageArch = Triple(Arch).getArch();
356 if (CoverageArch == llvm::Triple::ArchType::UnknownArch) {
357 errs() << "error: Unknown architecture: " << Arch << "\n";
358 return 1;
359 }
360 }
361
Justin Bogner116c1662014-09-19 08:13:12 +0000362 for (const auto &File : InputSourceFiles) {
363 SmallString<128> Path(File);
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000364 if (!CompareFilenamesOnly)
365 if (std::error_code EC = sys::fs::make_absolute(Path)) {
366 errs() << "error: " << File << ": " << EC.message();
367 return 1;
368 }
Justin Bogner116c1662014-09-19 08:13:12 +0000369 SourceFiles.push_back(Path.str());
370 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000371 return 0;
372 };
373
Alex Lorenze82d89c2014-08-22 22:56:03 +0000374 switch (Cmd) {
375 case Show:
376 return show(argc, argv, commandLineParser);
377 case Report:
378 return report(argc, argv, commandLineParser);
379 }
380 return 0;
381}
382
383int CodeCoverageTool::show(int argc, const char **argv,
384 CommandLineParserType commandLineParser) {
385
386 cl::OptionCategory ViewCategory("Viewing options");
387
388 cl::opt<bool> ShowLineExecutionCounts(
389 "show-line-counts", cl::Optional,
390 cl::desc("Show the execution counts for each line"), cl::init(true),
391 cl::cat(ViewCategory));
392
393 cl::opt<bool> ShowRegions(
394 "show-regions", cl::Optional,
395 cl::desc("Show the execution counts for each region"),
396 cl::cat(ViewCategory));
397
398 cl::opt<bool> ShowBestLineRegionsCounts(
399 "show-line-counts-or-regions", cl::Optional,
400 cl::desc("Show the execution counts for each line, or the execution "
401 "counts for each region on lines that have multiple regions"),
402 cl::cat(ViewCategory));
403
404 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
405 cl::desc("Show expanded source regions"),
406 cl::cat(ViewCategory));
407
408 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
409 cl::desc("Show function instantiations"),
410 cl::cat(ViewCategory));
411
Alex Lorenze82d89c2014-08-22 22:56:03 +0000412 auto Err = commandLineParser(argc, argv);
413 if (Err)
414 return Err;
415
Alex Lorenze82d89c2014-08-22 22:56:03 +0000416 ViewOpts.ShowLineNumbers = true;
417 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
418 !ShowRegions || ShowBestLineRegionsCounts;
419 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
420 ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
421 ViewOpts.ShowExpandedRegions = ShowExpansions;
422 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
423
Justin Bogner953e2402014-09-20 15:31:56 +0000424 auto Coverage = load();
425 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000426 return 1;
427
428 if (!Filters.empty()) {
429 // Show functions
Justin Bogner953e2402014-09-20 15:31:56 +0000430 for (const auto &Function : Coverage->getCoveredFunctions()) {
431 if (!Filters.matches(Function))
Alex Lorenze82d89c2014-08-22 22:56:03 +0000432 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000433
434 auto mainView = createFunctionView(Function, *Coverage);
Justin Bogner5a6edad2014-09-19 19:07:17 +0000435 if (!mainView) {
436 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
Justin Bogner953e2402014-09-20 15:31:56 +0000437 << "warning: Could not read coverage for '" << Function.Name;
Justin Bogner5a6edad2014-09-19 19:07:17 +0000438 outs() << "\n";
439 continue;
440 }
Justin Bogner953e2402014-09-20 15:31:56 +0000441 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name
442 << ":";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000443 outs() << "\n";
Justin Bogner5a6edad2014-09-19 19:07:17 +0000444 mainView->render(outs(), /*WholeFile=*/false);
Justin Bogner953e2402014-09-20 15:31:56 +0000445 outs() << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000446 }
447 return 0;
448 }
449
450 // Show files
451 bool ShowFilenames = SourceFiles.size() != 1;
452
Justin Bogner116c1662014-09-19 08:13:12 +0000453 if (SourceFiles.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000454 // Get the source files from the function coverage mapping
Justin Bogner953e2402014-09-20 15:31:56 +0000455 for (StringRef Filename : Coverage->getUniqueSourceFiles())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000456 SourceFiles.push_back(Filename);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000457
458 for (const auto &SourceFile : SourceFiles) {
Justin Bogner953e2402014-09-20 15:31:56 +0000459 auto mainView = createSourceFileView(SourceFile, *Coverage);
Justin Bogner5a6edad2014-09-19 19:07:17 +0000460 if (!mainView) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000461 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
462 << "warning: The file '" << SourceFile << "' isn't covered.";
463 outs() << "\n";
464 continue;
465 }
466
467 if (ShowFilenames) {
468 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":";
469 outs() << "\n";
470 }
Justin Bogner5a6edad2014-09-19 19:07:17 +0000471 mainView->render(outs(), /*Wholefile=*/true);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000472 if (SourceFiles.size() > 1)
473 outs() << "\n";
474 }
475
476 return 0;
477}
478
479int CodeCoverageTool::report(int argc, const char **argv,
480 CommandLineParserType commandLineParser) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000481 auto Err = commandLineParser(argc, argv);
482 if (Err)
483 return Err;
484
Justin Bogner953e2402014-09-20 15:31:56 +0000485 auto Coverage = load();
486 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000487 return 1;
488
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000489 CoverageReport Report(ViewOpts, std::move(Coverage));
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000490 if (SourceFiles.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000491 Report.renderFileReports(llvm::outs());
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000492 else
493 Report.renderFunctionReports(SourceFiles, llvm::outs());
Alex Lorenze82d89c2014-08-22 22:56:03 +0000494 return 0;
495}
496
Justin Bognerd249a3b2014-10-30 20:57:49 +0000497int showMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000498 CodeCoverageTool Tool;
499 return Tool.run(CodeCoverageTool::Show, argc, argv);
500}
501
Justin Bognerd249a3b2014-10-30 20:57:49 +0000502int reportMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000503 CodeCoverageTool Tool;
504 return Tool.run(CodeCoverageTool::Report, argc, argv);
505}