blob: 0943cebb262b07f042962fc722ed1ead66bc643c [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 "CoverageFilters.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000017#include "CoverageReport.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "CoverageViewOptions.h"
Easwaran Ramandc707122016-04-29 18:53:05 +000019#include "RenderingSupport.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#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"
Easwaran Ramandc707122016-04-29 18:53:05 +000024#include "llvm/ProfileData/Coverage/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"
29#include "llvm/Support/Path.h"
Justin Bognercfb53e42015-03-19 00:02:23 +000030#include "llvm/Support/Process.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000031#include <functional>
Justin Bognere53be062014-09-09 05:32:18 +000032#include <system_error>
Alex Lorenze82d89c2014-08-22 22:56:03 +000033
34using namespace llvm;
35using namespace coverage;
36
37namespace {
Alex Lorenze82d89c2014-08-22 22:56:03 +000038/// \brief The implementation of the coverage tool.
39class CodeCoverageTool {
40public:
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
Justin Bogner953e2402014-09-20 15:31:56 +000054 /// \brief Create source views for the expansions of the view.
55 void attachExpansionSubViews(SourceCoverageView &View,
56 ArrayRef<ExpansionRecord> Expansions,
57 CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000058
Justin Bogner953e2402014-09-20 15:31:56 +000059 /// \brief Create the source view of a particular function.
Justin Bogner5a6edad2014-09-19 19:07:17 +000060 std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +000061 createFunctionView(const FunctionRecord &Function, CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000062
63 /// \brief Create the main source view of a particular source file.
Justin Bogner5a6edad2014-09-19 19:07:17 +000064 std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +000065 createSourceFileView(StringRef SourceFile, CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000066
67 /// \brief Load the coverage mapping data. Return true if an error occured.
Justin Bogner953e2402014-09-20 15:31:56 +000068 std::unique_ptr<CoverageMapping> load();
Alex Lorenze82d89c2014-08-22 22:56:03 +000069
70 int run(Command Cmd, int argc, const char **argv);
71
Benjamin Kramerc321e532016-06-08 19:09:22 +000072 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
Alex Lorenze82d89c2014-08-22 22:56:03 +000073
74 int show(int argc, const char **argv,
75 CommandLineParserType commandLineParser);
76
77 int report(int argc, const char **argv,
78 CommandLineParserType commandLineParser);
79
Justin Bognerf6c50552014-10-30 20:51:24 +000080 std::string ObjectFilename;
Alex Lorenze82d89c2014-08-22 22:56:03 +000081 CoverageViewOptions ViewOpts;
Justin Bogner953e2402014-09-20 15:31:56 +000082 std::string PGOFilename;
Alex Lorenze82d89c2014-08-22 22:56:03 +000083 CoverageFiltersMatchAll Filters;
84 std::vector<std::string> SourceFiles;
85 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
86 LoadedSourceFiles;
Alex Lorenze82d89c2014-08-22 22:56:03 +000087 bool CompareFilenamesOnly;
Justin Bogner116c1662014-09-19 08:13:12 +000088 StringMap<std::string> RemappedFilenames;
Frederic Rissebc162a2015-06-22 21:33:24 +000089 std::string CoverageArch;
Alex Lorenze82d89c2014-08-22 22:56:03 +000090};
91}
92
93void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
94 errs() << "error: ";
95 if (!Whence.empty())
96 errs() << Whence << ": ";
97 errs() << Message << "\n";
98}
99
100ErrorOr<const MemoryBuffer &>
101CodeCoverageTool::getSourceFile(StringRef SourceFile) {
Justin Bogner116c1662014-09-19 08:13:12 +0000102 // 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;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000107 }
Justin Bogner116c1662014-09-19 08:13:12 +0000108 for (const auto &Files : LoadedSourceFiles)
109 if (sys::fs::equivalent(SourceFile, Files.first))
110 return *Files.second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000111 auto Buffer = MemoryBuffer::getFile(SourceFile);
112 if (auto EC = Buffer.getError()) {
113 error(EC.message(), SourceFile);
114 return EC;
115 }
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000116 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000117 return *LoadedSourceFiles.back().second;
118}
119
Justin Bogner953e2402014-09-20 15:31:56 +0000120void
121CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View,
122 ArrayRef<ExpansionRecord> Expansions,
123 CoverageMapping &Coverage) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000124 if (!ViewOpts.ShowExpandedRegions)
125 return;
Justin Bogner953e2402014-09-20 15:31:56 +0000126 for (const auto &Expansion : Expansions) {
127 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
128 if (ExpansionCoverage.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000129 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000130 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
131 if (!SourceBuffer)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000132 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000133
134 auto SubViewExpansions = ExpansionCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000135 auto SubView =
136 SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
137 ViewOpts, std::move(ExpansionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000138 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
139 View.addExpansion(Expansion.Region, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000140 }
141}
142
Justin Bogner5a6edad2014-09-19 19:07:17 +0000143std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +0000144CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
145 CoverageMapping &Coverage) {
146 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
147 if (FunctionCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000148 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000149 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
Justin Bogner5a6edad2014-09-19 19:07:17 +0000150 if (!SourceBuffer)
151 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000152
153 auto Expansions = FunctionCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000154 auto View = SourceCoverageView::create(Function.Name, SourceBuffer.get(),
155 ViewOpts, std::move(FunctionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000156 attachExpansionSubViews(*View, Expansions, Coverage);
157
158 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000159}
160
Justin Bogner953e2402014-09-20 15:31:56 +0000161std::unique_ptr<SourceCoverageView>
162CodeCoverageTool::createSourceFileView(StringRef SourceFile,
163 CoverageMapping &Coverage) {
Justin Bogner5a6edad2014-09-19 19:07:17 +0000164 auto SourceBuffer = getSourceFile(SourceFile);
165 if (!SourceBuffer)
166 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000167 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
168 if (FileCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000169 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000170
171 auto Expansions = FileCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000172 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
173 ViewOpts, std::move(FileCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000174 attachExpansionSubViews(*View, Expansions, Coverage);
175
176 for (auto Function : Coverage.getInstantiations(SourceFile)) {
177 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
178 auto SubViewExpansions = SubViewCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000179 auto SubView =
180 SourceCoverageView::create(Function->Name, SourceBuffer.get(), ViewOpts,
181 std::move(SubViewCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000182 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
183
184 if (SubView) {
Justin Bogner5e1400a2014-09-17 05:33:20 +0000185 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);
Justin Bogner953e2402014-09-20 15:31:56 +0000190 View->addInstantiation(Function->Name, Line, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000191 }
192 }
Justin Bogner5a6edad2014-09-19 19:07:17 +0000193 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000194}
195
Justin Bogner65337d12015-05-04 04:09:38 +0000196static 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
Justin Bogner953e2402014-09-20 15:31:56 +0000207std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
Justin Bogner65337d12015-05-04 04:09:38 +0000208 if (modifiedTimeGT(ObjectFilename, PGOFilename))
209 errs() << "warning: profile data may be out of date - object is newer\n";
Justin Bogner43795352015-03-11 02:30:51 +0000210 auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename,
211 CoverageArch);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000212 if (Error E = CoverageOrErr.takeError()) {
Justin Bogner953e2402014-09-20 15:31:56 +0000213 colored_ostream(errs(), raw_ostream::RED)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000214 << "error: Failed to load coverage: " << toString(std::move(E)) << "\n";
Justin Bogner953e2402014-09-20 15:31:56 +0000215 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";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000223 }
Justin Bogner116c1662014-09-19 08:13:12 +0000224
225 if (CompareFilenamesOnly) {
Justin Bogner953e2402014-09-20 15:31:56 +0000226 auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
Justin Bogner116c1662014-09-19 08:13:12 +0000227 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
Justin Bogner953e2402014-09-20 15:31:56 +0000238 return Coverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000239}
240
241int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
Justin Bognerf6c50552014-10-30 20:51:24 +0000242 cl::opt<std::string, true> ObjectFilename(
243 cl::Positional, cl::Required, cl::location(this->ObjectFilename),
244 cl::desc("Covered executable or object file."));
245
Alex Lorenze82d89c2014-08-22 22:56:03 +0000246 cl::list<std::string> InputSourceFiles(
247 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
248
Justin Bogner953e2402014-09-20 15:31:56 +0000249 cl::opt<std::string, true> PGOFilename(
250 "instr-profile", cl::Required, cl::location(this->PGOFilename),
Alex Lorenze82d89c2014-08-22 22:56:03 +0000251 cl::desc(
252 "File with the profile data obtained after an instrumented run"));
253
Justin Bogner43795352015-03-11 02:30:51 +0000254 cl::opt<std::string> Arch(
255 "arch", cl::desc("architecture of the coverage mapping binary"));
256
Alex Lorenze82d89c2014-08-22 22:56:03 +0000257 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,
Justin Bogner116c1662014-09-19 08:13:12 +0000262 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"));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000264
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
Justin Bogner9deb1d42015-03-19 04:45:16 +0000302 cl::opt<cl::boolOrDefault> UseColor(
303 "use-color", cl::desc("Emit colored output (default=autodetect)"),
304 cl::init(cl::BOU_UNSET));
Justin Bognercfb53e42015-03-19 00:02:23 +0000305
Alex Lorenze82d89c2014-08-22 22:56:03 +0000306 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
Justin Bogner9deb1d42015-03-19 04:45:16 +0000311 ViewOpts.Colors = UseColor == cl::BOU_UNSET
312 ? sys::Process::StandardOutHasColors()
313 : UseColor == cl::BOU_TRUE;
Justin Bognercfb53e42015-03-19 00:02:23 +0000314
Alex Lorenze82d89c2014-08-22 22:56:03 +0000315 // 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
Frederic Rissebc162a2015-06-22 21:33:24 +0000345 if (!Arch.empty() &&
346 Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
347 errs() << "error: Unknown architecture: " << Arch << "\n";
348 return 1;
Justin Bogner43795352015-03-11 02:30:51 +0000349 }
Frederic Rissebc162a2015-06-22 21:33:24 +0000350 CoverageArch = Arch;
Justin Bogner43795352015-03-11 02:30:51 +0000351
Justin Bogner116c1662014-09-19 08:13:12 +0000352 for (const auto &File : InputSourceFiles) {
353 SmallString<128> Path(File);
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000354 if (!CompareFilenamesOnly)
355 if (std::error_code EC = sys::fs::make_absolute(Path)) {
356 errs() << "error: " << File << ": " << EC.message();
357 return 1;
358 }
Justin Bogner116c1662014-09-19 08:13:12 +0000359 SourceFiles.push_back(Path.str());
360 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000361 return 0;
362 };
363
Alex Lorenze82d89c2014-08-22 22:56:03 +0000364 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
373int 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
Vedant Kumar635c83c2016-06-28 00:15:54 +0000402 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
Vedant Kumar7937ef32016-06-28 02:09:39 +0000409 cl::opt<std::string> ShowOutputDirectory(
410 "output-dir", cl::init(""),
411 cl::desc("Directory in which coverage information is written out"));
412 cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
413 cl::aliasopt(ShowOutputDirectory));
414
Alex Lorenze82d89c2014-08-22 22:56:03 +0000415 auto Err = commandLineParser(argc, argv);
416 if (Err)
417 return Err;
418
Alex Lorenze82d89c2014-08-22 22:56:03 +0000419 ViewOpts.ShowLineNumbers = true;
420 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
421 !ShowRegions || ShowBestLineRegionsCounts;
422 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
423 ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
424 ViewOpts.ShowExpandedRegions = ShowExpansions;
425 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
Vedant Kumar635c83c2016-06-28 00:15:54 +0000426 ViewOpts.ShowFormat = ShowFormat;
Vedant Kumar7937ef32016-06-28 02:09:39 +0000427 ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
428
429 if (ViewOpts.ShowOutputDirectory != "") {
430 if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
431 error("Could not create output directory!", E.message());
432 return 1;
433 }
434 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000435
Justin Bogner953e2402014-09-20 15:31:56 +0000436 auto Coverage = load();
437 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000438 return 1;
439
440 if (!Filters.empty()) {
441 // Show functions
Justin Bogner953e2402014-09-20 15:31:56 +0000442 for (const auto &Function : Coverage->getCoveredFunctions()) {
443 if (!Filters.matches(Function))
Alex Lorenze82d89c2014-08-22 22:56:03 +0000444 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000445
446 auto mainView = createFunctionView(Function, *Coverage);
Justin Bogner5a6edad2014-09-19 19:07:17 +0000447 if (!mainView) {
Vedant Kumar2c96e882016-06-24 02:33:01 +0000448 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
449 << "warning: Could not read coverage for '" << Function.Name << "'."
450 << "\n";
Justin Bogner5a6edad2014-09-19 19:07:17 +0000451 continue;
452 }
Vedant Kumar7937ef32016-06-28 02:09:39 +0000453
454 auto OSOrErr =
455 mainView->createOutputFile("functions", /*InToplevel=*/true);
456 if (Error E = OSOrErr.takeError()) {
457 handleAllErrors(OSOrErr.takeError(),
458 [&](const ErrorInfoBase &EI) { error(EI.message()); });
459 return 1;
460 }
461 auto OS = std::move(OSOrErr.get());
462 mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true);
463 mainView->closeOutputFile(std::move(OS));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000464 }
465 return 0;
466 }
467
468 // Show files
469 bool ShowFilenames = SourceFiles.size() != 1;
470
Justin Bogner116c1662014-09-19 08:13:12 +0000471 if (SourceFiles.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000472 // Get the source files from the function coverage mapping
Justin Bogner953e2402014-09-20 15:31:56 +0000473 for (StringRef Filename : Coverage->getUniqueSourceFiles())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000474 SourceFiles.push_back(Filename);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000475
476 for (const auto &SourceFile : SourceFiles) {
Justin Bogner953e2402014-09-20 15:31:56 +0000477 auto mainView = createSourceFileView(SourceFile, *Coverage);
Justin Bogner5a6edad2014-09-19 19:07:17 +0000478 if (!mainView) {
Vedant Kumar2c96e882016-06-24 02:33:01 +0000479 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000480 << "warning: The file '" << SourceFile << "' isn't covered.";
Vedant Kumar2c96e882016-06-24 02:33:01 +0000481 errs() << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000482 continue;
483 }
484
Vedant Kumar7937ef32016-06-28 02:09:39 +0000485 auto OSOrErr = mainView->createOutputFile(SourceFile, /*InToplevel=*/false);
486 if (Error E = OSOrErr.takeError()) {
487 handleAllErrors(OSOrErr.takeError(),
488 [&](const ErrorInfoBase &EI) { error(EI.message()); });
489 return 1;
490 }
491 auto OS = std::move(OSOrErr.get());
492 mainView->print(*OS.get(), /*Wholefile=*/true,
Vedant Kumar727549e2016-06-28 00:18:51 +0000493 /*ShowSourceName=*/ShowFilenames);
Vedant Kumar7937ef32016-06-28 02:09:39 +0000494 mainView->closeOutputFile(std::move(OS));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000495 }
496
497 return 0;
498}
499
500int CodeCoverageTool::report(int argc, const char **argv,
501 CommandLineParserType commandLineParser) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000502 auto Err = commandLineParser(argc, argv);
503 if (Err)
504 return Err;
505
Justin Bogner953e2402014-09-20 15:31:56 +0000506 auto Coverage = load();
507 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000508 return 1;
509
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000510 CoverageReport Report(ViewOpts, std::move(Coverage));
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000511 if (SourceFiles.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000512 Report.renderFileReports(llvm::outs());
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000513 else
514 Report.renderFunctionReports(SourceFiles, llvm::outs());
Alex Lorenze82d89c2014-08-22 22:56:03 +0000515 return 0;
516}
517
Justin Bognerd249a3b2014-10-30 20:57:49 +0000518int showMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000519 CodeCoverageTool Tool;
520 return Tool.run(CodeCoverageTool::Show, argc, argv);
521}
522
Justin Bognerd249a3b2014-10-30 20:57:49 +0000523int reportMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000524 CodeCoverageTool Tool;
525 return Tool.run(CodeCoverageTool::Report, argc, argv);
526}