blob: 237d877cde167c4b8a545106d68b191b2dd76468 [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"
Vedant Kumar424f51b2016-07-15 22:44:57 +000029#include "llvm/Support/MemoryBuffer.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000030#include "llvm/Support/Path.h"
Justin Bognercfb53e42015-03-19 00:02:23 +000031#include "llvm/Support/Process.h"
Vedant Kumar424f51b2016-07-15 22:44:57 +000032#include "llvm/Support/Program.h"
Vedant Kumar86b2ac632016-07-13 21:38:36 +000033#include "llvm/Support/ThreadPool.h"
Vedant Kumar424f51b2016-07-15 22:44:57 +000034#include "llvm/Support/ToolOutputFile.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000035#include <functional>
Justin Bognere53be062014-09-09 05:32:18 +000036#include <system_error>
Alex Lorenze82d89c2014-08-22 22:56:03 +000037
38using namespace llvm;
39using namespace coverage;
40
Vedant Kumar7101d732016-07-26 22:50:58 +000041void exportCoverageDataToJson(StringRef ObjectFilename,
42 const coverage::CoverageMapping &CoverageMapping,
43 raw_ostream &OS);
44
Alex Lorenze82d89c2014-08-22 22:56:03 +000045namespace {
Alex Lorenze82d89c2014-08-22 22:56:03 +000046/// \brief The implementation of the coverage tool.
47class CodeCoverageTool {
48public:
49 enum Command {
50 /// \brief The show command.
51 Show,
52 /// \brief The report command.
Vedant Kumar7101d732016-07-26 22:50:58 +000053 Report,
54 /// \brief The export command.
55 Export
Alex Lorenze82d89c2014-08-22 22:56:03 +000056 };
57
Vedant Kumar46103672016-09-22 21:49:47 +000058 int run(Command Cmd, int argc, const char **argv);
59
60private:
Alex Lorenze82d89c2014-08-22 22:56:03 +000061 /// \brief Print the error message to the error output stream.
62 void error(const Twine &Message, StringRef Whence = "");
63
Vedant Kumarb3020632016-07-18 17:53:12 +000064 /// \brief Print the warning message to the error output stream.
65 void warning(const Twine &Message, StringRef Whence = "");
Vedant Kumar86b2ac632016-07-13 21:38:36 +000066
Vedant Kumar2ab08da2016-07-18 18:02:54 +000067 /// \brief Copy \p Path into the list of input source files.
Vedant Kumarcef440f2016-06-28 16:12:18 +000068 void addCollectedPath(const std::string &Path);
69
Vedant Kumar1ce90d82016-09-22 21:49:43 +000070 /// \brief If \p Path is a regular file, collect the path. If it's a
71 /// directory, recursively collect all of the paths within the directory.
72 void collectPaths(const std::string &Path);
73
Alex Lorenze82d89c2014-08-22 22:56:03 +000074 /// \brief Return a memory buffer for the given source file.
75 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
76
Justin Bogner953e2402014-09-20 15:31:56 +000077 /// \brief Create source views for the expansions of the view.
78 void attachExpansionSubViews(SourceCoverageView &View,
79 ArrayRef<ExpansionRecord> Expansions,
Vedant Kumarf681e2e2016-07-15 01:19:33 +000080 const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000081
Justin Bogner953e2402014-09-20 15:31:56 +000082 /// \brief Create the source view of a particular function.
Justin Bogner5a6edad2014-09-19 19:07:17 +000083 std::unique_ptr<SourceCoverageView>
Vedant Kumarf681e2e2016-07-15 01:19:33 +000084 createFunctionView(const FunctionRecord &Function,
85 const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000086
87 /// \brief Create the main source view of a particular source file.
Justin Bogner5a6edad2014-09-19 19:07:17 +000088 std::unique_ptr<SourceCoverageView>
Vedant Kumarf681e2e2016-07-15 01:19:33 +000089 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000090
Vedant Kumarf681e2e2016-07-15 01:19:33 +000091 /// \brief Load the coverage mapping data. Return nullptr if an error occured.
Justin Bogner953e2402014-09-20 15:31:56 +000092 std::unique_ptr<CoverageMapping> load();
Alex Lorenze82d89c2014-08-22 22:56:03 +000093
Vedant Kumar424f51b2016-07-15 22:44:57 +000094 /// \brief If a demangler is available, demangle all symbol names.
95 void demangleSymbols(const CoverageMapping &Coverage);
96
97 /// \brief Demangle \p Sym if possible. Otherwise, just return \p Sym.
98 StringRef getSymbolForHumans(StringRef Sym) const;
99
Benjamin Kramerc321e532016-06-08 19:09:22 +0000100 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000101
102 int show(int argc, const char **argv,
103 CommandLineParserType commandLineParser);
104
105 int report(int argc, const char **argv,
106 CommandLineParserType commandLineParser);
107
Vedant Kumar7101d732016-07-26 22:50:58 +0000108 int export_(int argc, const char **argv,
109 CommandLineParserType commandLineParser);
110
Justin Bognerf6c50552014-10-30 20:51:24 +0000111 std::string ObjectFilename;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000112 CoverageViewOptions ViewOpts;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000113 CoverageFiltersMatchAll Filters;
Vedant Kumar46103672016-09-22 21:49:47 +0000114
115 /// The path to the indexed profile.
116 std::string PGOFilename;
117
118 /// A list of input source files.
Vedant Kumarcef440f2016-06-28 16:12:18 +0000119 std::vector<StringRef> SourceFiles;
Vedant Kumar46103672016-09-22 21:49:47 +0000120
121 /// Whether or not we're in -filename-equivalence mode.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000122 bool CompareFilenamesOnly;
Vedant Kumar46103672016-09-22 21:49:47 +0000123
124 /// In -filename-equivalence mode, this maps absolute paths from the
125 /// coverage mapping data to input source files.
Justin Bogner116c1662014-09-19 08:13:12 +0000126 StringMap<std::string> RemappedFilenames;
Vedant Kumar46103672016-09-22 21:49:47 +0000127
128 /// The architecture the coverage mapping data targets.
Frederic Rissebc162a2015-06-22 21:33:24 +0000129 std::string CoverageArch;
Vedant Kumarcef440f2016-06-28 16:12:18 +0000130
Vedant Kumar424f51b2016-07-15 22:44:57 +0000131 /// A cache for demangled symbol names.
132 StringMap<std::string> DemangledNames;
133
Vedant Kumar6ab6b362016-07-15 22:44:54 +0000134 /// File paths (absolute, or otherwise) to input source files.
Vedant Kumarcef440f2016-06-28 16:12:18 +0000135 std::vector<std::string> CollectedPaths;
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000136
Vedant Kumar6ab6b362016-07-15 22:44:54 +0000137 /// Errors and warnings which have not been printed.
Vedant Kumarb3020632016-07-18 17:53:12 +0000138 std::mutex ErrsLock;
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000139
Vedant Kumar6ab6b362016-07-15 22:44:54 +0000140 /// A container for input source file buffers.
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000141 std::mutex LoadedSourceFilesLock;
142 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
143 LoadedSourceFiles;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000144};
145}
146
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000147static std::string getErrorString(const Twine &Message, StringRef Whence,
148 bool Warning) {
149 std::string Str = (Warning ? "warning" : "error");
150 Str += ": ";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000151 if (!Whence.empty())
Vedant Kumarb95dc462016-07-15 01:53:39 +0000152 Str += Whence.str() + ": ";
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000153 Str += Message.str() + "\n";
154 return Str;
155}
156
157void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000158 std::unique_lock<std::mutex> Guard{ErrsLock};
159 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
160 << getErrorString(Message, Whence, false);
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000161}
162
Vedant Kumarb3020632016-07-18 17:53:12 +0000163void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
164 std::unique_lock<std::mutex> Guard{ErrsLock};
165 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
166 << getErrorString(Message, Whence, true);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000167}
168
Vedant Kumarcef440f2016-06-28 16:12:18 +0000169void CodeCoverageTool::addCollectedPath(const std::string &Path) {
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000170 if (CompareFilenamesOnly) {
171 CollectedPaths.push_back(Path);
172 } else {
173 SmallString<128> EffectivePath(Path);
174 if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
175 error(EC.message(), Path);
176 return;
177 }
178 sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
179 CollectedPaths.push_back(EffectivePath.str());
180 }
181
Vedant Kumarcef440f2016-06-28 16:12:18 +0000182 SourceFiles.emplace_back(CollectedPaths.back());
183}
184
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000185void CodeCoverageTool::collectPaths(const std::string &Path) {
186 llvm::sys::fs::file_status Status;
187 llvm::sys::fs::status(Path, Status);
188 if (!llvm::sys::fs::exists(Status)) {
189 if (CompareFilenamesOnly)
190 addCollectedPath(Path);
191 else
192 error("Missing source file", Path);
193 return;
194 }
195
196 if (llvm::sys::fs::is_regular_file(Status)) {
197 addCollectedPath(Path);
198 return;
199 }
200
201 if (llvm::sys::fs::is_directory(Status)) {
202 std::error_code EC;
203 for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
204 F != E && !EC; F.increment(EC)) {
205 if (llvm::sys::fs::is_regular_file(F->path()))
206 addCollectedPath(F->path());
207 }
208 if (EC)
209 warning(EC.message(), Path);
210 }
211}
212
Alex Lorenze82d89c2014-08-22 22:56:03 +0000213ErrorOr<const MemoryBuffer &>
214CodeCoverageTool::getSourceFile(StringRef SourceFile) {
Justin Bogner116c1662014-09-19 08:13:12 +0000215 // If we've remapped filenames, look up the real location for this file.
Vedant Kumar615b85d2016-07-15 01:19:36 +0000216 std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
Justin Bogner116c1662014-09-19 08:13:12 +0000217 if (!RemappedFilenames.empty()) {
218 auto Loc = RemappedFilenames.find(SourceFile);
219 if (Loc != RemappedFilenames.end())
220 SourceFile = Loc->second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000221 }
Justin Bogner116c1662014-09-19 08:13:12 +0000222 for (const auto &Files : LoadedSourceFiles)
223 if (sys::fs::equivalent(SourceFile, Files.first))
224 return *Files.second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000225 auto Buffer = MemoryBuffer::getFile(SourceFile);
226 if (auto EC = Buffer.getError()) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000227 error(EC.message(), SourceFile);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000228 return EC;
229 }
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000230 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000231 return *LoadedSourceFiles.back().second;
232}
233
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000234void CodeCoverageTool::attachExpansionSubViews(
235 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
236 const CoverageMapping &Coverage) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000237 if (!ViewOpts.ShowExpandedRegions)
238 return;
Justin Bogner953e2402014-09-20 15:31:56 +0000239 for (const auto &Expansion : Expansions) {
240 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
241 if (ExpansionCoverage.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000242 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000243 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
244 if (!SourceBuffer)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000245 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000246
247 auto SubViewExpansions = ExpansionCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000248 auto SubView =
249 SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
250 ViewOpts, std::move(ExpansionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000251 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
252 View.addExpansion(Expansion.Region, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000253 }
254}
255
Justin Bogner5a6edad2014-09-19 19:07:17 +0000256std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +0000257CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000258 const CoverageMapping &Coverage) {
Justin Bogner953e2402014-09-20 15:31:56 +0000259 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
260 if (FunctionCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000261 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000262 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
Justin Bogner5a6edad2014-09-19 19:07:17 +0000263 if (!SourceBuffer)
264 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000265
266 auto Expansions = FunctionCoverage.getExpansions();
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000267 auto View = SourceCoverageView::create(getSymbolForHumans(Function.Name),
268 SourceBuffer.get(), ViewOpts,
269 std::move(FunctionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000270 attachExpansionSubViews(*View, Expansions, Coverage);
271
272 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000273}
274
Justin Bogner953e2402014-09-20 15:31:56 +0000275std::unique_ptr<SourceCoverageView>
276CodeCoverageTool::createSourceFileView(StringRef SourceFile,
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000277 const CoverageMapping &Coverage) {
Justin Bogner5a6edad2014-09-19 19:07:17 +0000278 auto SourceBuffer = getSourceFile(SourceFile);
279 if (!SourceBuffer)
280 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000281 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
282 if (FileCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000283 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000284
285 auto Expansions = FileCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000286 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
287 ViewOpts, std::move(FileCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000288 attachExpansionSubViews(*View, Expansions, Coverage);
289
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000290 for (const auto *Function : Coverage.getInstantiations(SourceFile)) {
Vedant Kumara8c396d2016-09-15 06:44:51 +0000291 std::unique_ptr<SourceCoverageView> SubView{nullptr};
Justin Bogner953e2402014-09-20 15:31:56 +0000292
Vedant Kumare9079772016-09-20 21:27:48 +0000293 StringRef Funcname = getSymbolForHumans(Function->Name);
294
Vedant Kumara8c396d2016-09-15 06:44:51 +0000295 if (Function->ExecutionCount > 0) {
296 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
297 auto SubViewExpansions = SubViewCoverage.getExpansions();
298 SubView = SourceCoverageView::create(
Vedant Kumare9079772016-09-20 21:27:48 +0000299 Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
Vedant Kumara8c396d2016-09-15 06:44:51 +0000300 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000301 }
Vedant Kumara8c396d2016-09-15 06:44:51 +0000302
303 unsigned FileID = Function->CountedRegions.front().FileID;
304 unsigned Line = 0;
305 for (const auto &CR : Function->CountedRegions)
306 if (CR.FileID == FileID)
307 Line = std::max(CR.LineEnd, Line);
Vedant Kumare9079772016-09-20 21:27:48 +0000308 View->addInstantiation(Funcname, Line, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000309 }
Justin Bogner5a6edad2014-09-19 19:07:17 +0000310 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000311}
312
Justin Bogner65337d12015-05-04 04:09:38 +0000313static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
314 sys::fs::file_status Status;
315 if (sys::fs::status(LHS, Status))
316 return false;
317 auto LHSTime = Status.getLastModificationTime();
318 if (sys::fs::status(RHS, Status))
319 return false;
320 auto RHSTime = Status.getLastModificationTime();
321 return LHSTime > RHSTime;
322}
323
Justin Bogner953e2402014-09-20 15:31:56 +0000324std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
Justin Bogner65337d12015-05-04 04:09:38 +0000325 if (modifiedTimeGT(ObjectFilename, PGOFilename))
Vedant Kumarb3020632016-07-18 17:53:12 +0000326 warning("profile data may be out of date - object is newer",
327 ObjectFilename);
328 auto CoverageOrErr =
329 CoverageMapping::load(ObjectFilename, PGOFilename, CoverageArch);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000330 if (Error E = CoverageOrErr.takeError()) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000331 error("Failed to load coverage: " + toString(std::move(E)), ObjectFilename);
Justin Bogner953e2402014-09-20 15:31:56 +0000332 return nullptr;
333 }
334 auto Coverage = std::move(CoverageOrErr.get());
335 unsigned Mismatched = Coverage->getMismatchedCount();
Vedant Kumarb3020632016-07-18 17:53:12 +0000336 if (Mismatched)
337 warning(utostr(Mismatched) + " functions have mismatched data");
Justin Bogner116c1662014-09-19 08:13:12 +0000338
339 if (CompareFilenamesOnly) {
Justin Bogner953e2402014-09-20 15:31:56 +0000340 auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
Justin Bogner116c1662014-09-19 08:13:12 +0000341 for (auto &SF : SourceFiles) {
342 StringRef SFBase = sys::path::filename(SF);
343 for (const auto &CF : CoveredFiles)
344 if (SFBase == sys::path::filename(CF)) {
345 RemappedFilenames[CF] = SF;
346 SF = CF;
347 break;
348 }
349 }
350 }
351
Vedant Kumar424f51b2016-07-15 22:44:57 +0000352 demangleSymbols(*Coverage);
353
Justin Bogner953e2402014-09-20 15:31:56 +0000354 return Coverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000355}
356
Vedant Kumar424f51b2016-07-15 22:44:57 +0000357void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
358 if (!ViewOpts.hasDemangler())
359 return;
360
361 // Pass function names to the demangler in a temporary file.
362 int InputFD;
363 SmallString<256> InputPath;
364 std::error_code EC =
365 sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
366 if (EC) {
367 error(InputPath, EC.message());
368 return;
369 }
370 tool_output_file InputTOF{InputPath, InputFD};
371
372 unsigned NumSymbols = 0;
373 for (const auto &Function : Coverage.getCoveredFunctions()) {
374 InputTOF.os() << Function.Name << '\n';
375 ++NumSymbols;
376 }
Vedant Kumar554357b2016-07-15 23:08:22 +0000377 InputTOF.os().close();
Vedant Kumar424f51b2016-07-15 22:44:57 +0000378
379 // Use another temporary file to store the demangler's output.
380 int OutputFD;
381 SmallString<256> OutputPath;
382 EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
383 OutputPath);
384 if (EC) {
385 error(OutputPath, EC.message());
386 return;
387 }
388 tool_output_file OutputTOF{OutputPath, OutputFD};
Vedant Kumar554357b2016-07-15 23:08:22 +0000389 OutputTOF.os().close();
Vedant Kumar424f51b2016-07-15 22:44:57 +0000390
391 // Invoke the demangler.
392 std::vector<const char *> ArgsV;
393 for (const std::string &Arg : ViewOpts.DemanglerOpts)
394 ArgsV.push_back(Arg.c_str());
395 ArgsV.push_back(nullptr);
Vedant Kumar38202c02016-07-15 23:15:35 +0000396 StringRef InputPathRef = InputPath.str();
397 StringRef OutputPathRef = OutputPath.str();
398 StringRef StderrRef;
Vedant Kumar424f51b2016-07-15 22:44:57 +0000399 const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef};
400 std::string ErrMsg;
401 int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(),
402 /*env=*/nullptr, Redirects, /*secondsToWait=*/0,
403 /*memoryLimit=*/0, &ErrMsg);
404 if (RC) {
405 error(ErrMsg, ViewOpts.DemanglerOpts[0]);
406 return;
407 }
408
409 // Parse the demangler's output.
410 auto BufOrError = MemoryBuffer::getFile(OutputPath);
411 if (!BufOrError) {
412 error(OutputPath, BufOrError.getError().message());
413 return;
414 }
415
416 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
417
418 SmallVector<StringRef, 8> Symbols;
419 StringRef DemanglerData = DemanglerBuf->getBuffer();
420 DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
421 /*KeepEmpty=*/false);
422 if (Symbols.size() != NumSymbols) {
423 error("Demangler did not provide expected number of symbols");
424 return;
425 }
426
427 // Cache the demangled names.
428 unsigned I = 0;
429 for (const auto &Function : Coverage.getCoveredFunctions())
430 DemangledNames[Function.Name] = Symbols[I++];
431}
432
433StringRef CodeCoverageTool::getSymbolForHumans(StringRef Sym) const {
434 const auto DemangledName = DemangledNames.find(Sym);
435 if (DemangledName == DemangledNames.end())
436 return Sym;
437 return DemangledName->getValue();
438}
439
Alex Lorenze82d89c2014-08-22 22:56:03 +0000440int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
Justin Bognerf6c50552014-10-30 20:51:24 +0000441 cl::opt<std::string, true> ObjectFilename(
442 cl::Positional, cl::Required, cl::location(this->ObjectFilename),
443 cl::desc("Covered executable or object file."));
444
Alex Lorenze82d89c2014-08-22 22:56:03 +0000445 cl::list<std::string> InputSourceFiles(
446 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
447
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000448 cl::opt<bool> DebugDumpCollectedPaths(
449 "dump-collected-paths", cl::Optional, cl::Hidden,
450 cl::desc("Show the collected paths to source files"));
451
Justin Bogner953e2402014-09-20 15:31:56 +0000452 cl::opt<std::string, true> PGOFilename(
453 "instr-profile", cl::Required, cl::location(this->PGOFilename),
Alex Lorenze82d89c2014-08-22 22:56:03 +0000454 cl::desc(
455 "File with the profile data obtained after an instrumented run"));
456
Justin Bogner43795352015-03-11 02:30:51 +0000457 cl::opt<std::string> Arch(
458 "arch", cl::desc("architecture of the coverage mapping binary"));
459
Alex Lorenze82d89c2014-08-22 22:56:03 +0000460 cl::opt<bool> DebugDump("dump", cl::Optional,
461 cl::desc("Show internal debug dump"));
462
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000463 cl::opt<CoverageViewOptions::OutputFormat> Format(
464 "format", cl::desc("Output format for line-based coverage reports"),
465 cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
466 "Text output"),
Vedant Kumar4c010922016-07-06 21:44:05 +0000467 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
468 "HTML output"),
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000469 clEnumValEnd),
470 cl::init(CoverageViewOptions::OutputFormat::Text));
471
Alex Lorenze82d89c2014-08-22 22:56:03 +0000472 cl::opt<bool> FilenameEquivalence(
473 "filename-equivalence", cl::Optional,
Justin Bogner116c1662014-09-19 08:13:12 +0000474 cl::desc("Treat source files as equivalent to paths in the coverage data "
475 "when the file names match, even if the full paths do not"));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000476
477 cl::OptionCategory FilteringCategory("Function filtering options");
478
479 cl::list<std::string> NameFilters(
480 "name", cl::Optional,
481 cl::desc("Show code coverage only for functions with the given name"),
482 cl::ZeroOrMore, cl::cat(FilteringCategory));
483
484 cl::list<std::string> NameRegexFilters(
485 "name-regex", cl::Optional,
486 cl::desc("Show code coverage only for functions that match the given "
487 "regular expression"),
488 cl::ZeroOrMore, cl::cat(FilteringCategory));
489
490 cl::opt<double> RegionCoverageLtFilter(
491 "region-coverage-lt", cl::Optional,
492 cl::desc("Show code coverage only for functions with region coverage "
493 "less than the given threshold"),
494 cl::cat(FilteringCategory));
495
496 cl::opt<double> RegionCoverageGtFilter(
497 "region-coverage-gt", cl::Optional,
498 cl::desc("Show code coverage only for functions with region coverage "
499 "greater than the given threshold"),
500 cl::cat(FilteringCategory));
501
502 cl::opt<double> LineCoverageLtFilter(
503 "line-coverage-lt", cl::Optional,
504 cl::desc("Show code coverage only for functions with line coverage less "
505 "than the given threshold"),
506 cl::cat(FilteringCategory));
507
508 cl::opt<double> LineCoverageGtFilter(
509 "line-coverage-gt", cl::Optional,
510 cl::desc("Show code coverage only for functions with line coverage "
511 "greater than the given threshold"),
512 cl::cat(FilteringCategory));
513
Justin Bogner9deb1d42015-03-19 04:45:16 +0000514 cl::opt<cl::boolOrDefault> UseColor(
515 "use-color", cl::desc("Emit colored output (default=autodetect)"),
516 cl::init(cl::BOU_UNSET));
Justin Bognercfb53e42015-03-19 00:02:23 +0000517
Vedant Kumar424f51b2016-07-15 22:44:57 +0000518 cl::list<std::string> DemanglerOpts(
519 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
520
Alex Lorenze82d89c2014-08-22 22:56:03 +0000521 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
522 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
523 ViewOpts.Debug = DebugDump;
524 CompareFilenamesOnly = FilenameEquivalence;
525
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000526 ViewOpts.Format = Format;
Ying Yi84dc9712016-08-24 14:27:23 +0000527 SmallString<128> ObjectFilePath(this->ObjectFilename);
528 if (std::error_code EC = sys::fs::make_absolute(ObjectFilePath)) {
529 error(EC.message(), this->ObjectFilename);
530 return 1;
531 }
Ying Yi76eb2192016-08-30 07:01:37 +0000532 sys::path::native(ObjectFilePath);
Ying Yi84dc9712016-08-24 14:27:23 +0000533 ViewOpts.ObjectFilename = ObjectFilePath.c_str();
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000534 switch (ViewOpts.Format) {
535 case CoverageViewOptions::OutputFormat::Text:
536 ViewOpts.Colors = UseColor == cl::BOU_UNSET
537 ? sys::Process::StandardOutHasColors()
538 : UseColor == cl::BOU_TRUE;
539 break;
Vedant Kumar4c010922016-07-06 21:44:05 +0000540 case CoverageViewOptions::OutputFormat::HTML:
541 if (UseColor == cl::BOU_FALSE)
542 error("Color output cannot be disabled when generating html.");
543 ViewOpts.Colors = true;
544 break;
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000545 }
Justin Bognercfb53e42015-03-19 00:02:23 +0000546
Vedant Kumar424f51b2016-07-15 22:44:57 +0000547 // If a demangler is supplied, check if it exists and register it.
548 if (DemanglerOpts.size()) {
549 auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
550 if (!DemanglerPathOrErr) {
551 error("Could not find the demangler!",
552 DemanglerPathOrErr.getError().message());
553 return 1;
554 }
555 DemanglerOpts[0] = *DemanglerPathOrErr;
556 ViewOpts.DemanglerOpts.swap(DemanglerOpts);
557 }
558
Alex Lorenze82d89c2014-08-22 22:56:03 +0000559 // Create the function filters
560 if (!NameFilters.empty() || !NameRegexFilters.empty()) {
561 auto NameFilterer = new CoverageFilters;
562 for (const auto &Name : NameFilters)
563 NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
564 for (const auto &Regex : NameRegexFilters)
565 NameFilterer->push_back(
566 llvm::make_unique<NameRegexCoverageFilter>(Regex));
567 Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
568 }
569 if (RegionCoverageLtFilter.getNumOccurrences() ||
570 RegionCoverageGtFilter.getNumOccurrences() ||
571 LineCoverageLtFilter.getNumOccurrences() ||
572 LineCoverageGtFilter.getNumOccurrences()) {
573 auto StatFilterer = new CoverageFilters;
574 if (RegionCoverageLtFilter.getNumOccurrences())
575 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
576 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
577 if (RegionCoverageGtFilter.getNumOccurrences())
578 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
579 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
580 if (LineCoverageLtFilter.getNumOccurrences())
581 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
582 LineCoverageFilter::LessThan, LineCoverageLtFilter));
583 if (LineCoverageGtFilter.getNumOccurrences())
584 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
585 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
586 Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
587 }
588
Frederic Rissebc162a2015-06-22 21:33:24 +0000589 if (!Arch.empty() &&
590 Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000591 error("Unknown architecture: " + Arch);
Frederic Rissebc162a2015-06-22 21:33:24 +0000592 return 1;
Justin Bogner43795352015-03-11 02:30:51 +0000593 }
Frederic Rissebc162a2015-06-22 21:33:24 +0000594 CoverageArch = Arch;
Justin Bogner43795352015-03-11 02:30:51 +0000595
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000596 for (const std::string &File : InputSourceFiles)
597 collectPaths(File);
598
599 if (DebugDumpCollectedPaths) {
600 for (StringRef SF : SourceFiles)
601 outs() << SF << '\n';
602 ::exit(0);
Justin Bogner116c1662014-09-19 08:13:12 +0000603 }
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000604
Alex Lorenze82d89c2014-08-22 22:56:03 +0000605 return 0;
606 };
607
Alex Lorenze82d89c2014-08-22 22:56:03 +0000608 switch (Cmd) {
609 case Show:
610 return show(argc, argv, commandLineParser);
611 case Report:
612 return report(argc, argv, commandLineParser);
Vedant Kumar7101d732016-07-26 22:50:58 +0000613 case Export:
614 return export_(argc, argv, commandLineParser);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000615 }
616 return 0;
617}
618
619int CodeCoverageTool::show(int argc, const char **argv,
620 CommandLineParserType commandLineParser) {
621
622 cl::OptionCategory ViewCategory("Viewing options");
623
624 cl::opt<bool> ShowLineExecutionCounts(
625 "show-line-counts", cl::Optional,
626 cl::desc("Show the execution counts for each line"), cl::init(true),
627 cl::cat(ViewCategory));
628
629 cl::opt<bool> ShowRegions(
630 "show-regions", cl::Optional,
631 cl::desc("Show the execution counts for each region"),
632 cl::cat(ViewCategory));
633
634 cl::opt<bool> ShowBestLineRegionsCounts(
635 "show-line-counts-or-regions", cl::Optional,
636 cl::desc("Show the execution counts for each line, or the execution "
637 "counts for each region on lines that have multiple regions"),
638 cl::cat(ViewCategory));
639
640 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
641 cl::desc("Show expanded source regions"),
642 cl::cat(ViewCategory));
643
644 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
645 cl::desc("Show function instantiations"),
646 cl::cat(ViewCategory));
647
Vedant Kumar7937ef32016-06-28 02:09:39 +0000648 cl::opt<std::string> ShowOutputDirectory(
649 "output-dir", cl::init(""),
650 cl::desc("Directory in which coverage information is written out"));
651 cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
652 cl::aliasopt(ShowOutputDirectory));
653
Ying Yi0ef31b72016-08-04 10:39:43 +0000654 cl::opt<uint32_t> TabSize(
Vedant Kumarad547d32016-08-04 18:00:42 +0000655 "tab-size", cl::init(2),
656 cl::desc(
657 "Set tab expansion size for html coverage reports (default = 2)"));
Ying Yi0ef31b72016-08-04 10:39:43 +0000658
Ying Yi84dc9712016-08-24 14:27:23 +0000659 cl::opt<std::string> ProjectTitle(
660 "project-title", cl::Optional,
661 cl::desc("Set project title for the coverage report"));
662
Alex Lorenze82d89c2014-08-22 22:56:03 +0000663 auto Err = commandLineParser(argc, argv);
664 if (Err)
665 return Err;
666
Alex Lorenze82d89c2014-08-22 22:56:03 +0000667 ViewOpts.ShowLineNumbers = true;
668 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
669 !ShowRegions || ShowBestLineRegionsCounts;
670 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
671 ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
672 ViewOpts.ShowExpandedRegions = ShowExpansions;
673 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
Vedant Kumar7937ef32016-06-28 02:09:39 +0000674 ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
Ying Yi0ef31b72016-08-04 10:39:43 +0000675 ViewOpts.TabSize = TabSize;
Ying Yi84dc9712016-08-24 14:27:23 +0000676 ViewOpts.ProjectTitle = ProjectTitle;
Vedant Kumar7937ef32016-06-28 02:09:39 +0000677
Vedant Kumar64d8a022016-06-28 16:12:20 +0000678 if (ViewOpts.hasOutputDirectory()) {
Vedant Kumar7937ef32016-06-28 02:09:39 +0000679 if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
680 error("Could not create output directory!", E.message());
681 return 1;
682 }
683 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000684
Ying Yi84dc9712016-08-24 14:27:23 +0000685 sys::fs::file_status Status;
686 if (sys::fs::status(PGOFilename, Status)) {
687 error("profdata file error: can not get the file status. \n");
688 return 1;
689 }
690
691 auto ModifiedTime = Status.getLastModificationTime();
692 std::string ModifiedTimeStr = ModifiedTime.str();
693 size_t found = ModifiedTimeStr.rfind(":");
694 ViewOpts.CreatedTimeStr = (found != std::string::npos)
695 ? "Created: " + ModifiedTimeStr.substr(0, found)
696 : "Created: " + ModifiedTimeStr;
697
Justin Bogner953e2402014-09-20 15:31:56 +0000698 auto Coverage = load();
699 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000700 return 1;
701
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000702 auto Printer = CoveragePrinter::create(ViewOpts);
703
Alex Lorenze82d89c2014-08-22 22:56:03 +0000704 if (!Filters.empty()) {
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000705 auto OSOrErr = Printer->createViewFile("functions", /*InToplevel=*/true);
706 if (Error E = OSOrErr.takeError()) {
Vedant Kumarb95dc462016-07-15 01:53:39 +0000707 error("Could not create view file!", toString(std::move(E)));
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000708 return 1;
709 }
710 auto OS = std::move(OSOrErr.get());
711
712 // Show functions.
Justin Bogner953e2402014-09-20 15:31:56 +0000713 for (const auto &Function : Coverage->getCoveredFunctions()) {
714 if (!Filters.matches(Function))
Alex Lorenze82d89c2014-08-22 22:56:03 +0000715 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000716
717 auto mainView = createFunctionView(Function, *Coverage);
Justin Bogner5a6edad2014-09-19 19:07:17 +0000718 if (!mainView) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000719 warning("Could not read coverage for '" + Function.Name + "'.");
Justin Bogner5a6edad2014-09-19 19:07:17 +0000720 continue;
721 }
Vedant Kumar7937ef32016-06-28 02:09:39 +0000722
Vedant Kumar7937ef32016-06-28 02:09:39 +0000723 mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000724 }
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000725
726 Printer->closeViewFile(std::move(OS));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000727 return 0;
728 }
729
730 // Show files
Ying Yi84dc9712016-08-24 14:27:23 +0000731 bool ShowFilenames =
Ying Yi24e91bd2016-09-06 21:41:38 +0000732 (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
Ying Yi84dc9712016-08-24 14:27:23 +0000733 (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000734
Justin Bogner116c1662014-09-19 08:13:12 +0000735 if (SourceFiles.empty())
Vedant Kumar64d8a022016-06-28 16:12:20 +0000736 // Get the source files from the function coverage mapping.
Justin Bogner953e2402014-09-20 15:31:56 +0000737 for (StringRef Filename : Coverage->getUniqueSourceFiles())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000738 SourceFiles.push_back(Filename);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000739
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000740 // Create an index out of the source files.
741 if (ViewOpts.hasOutputDirectory()) {
Vedant Kumara59334d2016-09-09 01:32:55 +0000742 if (Error E = Printer->createIndexFile(SourceFiles, *Coverage)) {
Vedant Kumarb95dc462016-07-15 01:53:39 +0000743 error("Could not create index file!", toString(std::move(E)));
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000744 return 1;
745 }
746 }
747
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000748 // In -output-dir mode, it's safe to use multiple threads to print files.
749 unsigned ThreadCount = 1;
750 if (ViewOpts.hasOutputDirectory())
751 ThreadCount = std::thread::hardware_concurrency();
752 ThreadPool Pool(ThreadCount);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000753
Vedant Kumar84c452d2016-07-15 01:19:35 +0000754 for (StringRef SourceFile : SourceFiles) {
755 Pool.async([this, SourceFile, &Coverage, &Printer, ShowFilenames] {
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000756 auto View = createSourceFileView(SourceFile, *Coverage);
757 if (!View) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000758 warning("The file '" + SourceFile.str() + "' isn't covered.");
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000759 return;
760 }
761
762 auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
763 if (Error E = OSOrErr.takeError()) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000764 error("Could not create view file!", toString(std::move(E)));
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000765 return;
766 }
767 auto OS = std::move(OSOrErr.get());
768
769 View->print(*OS.get(), /*Wholefile=*/true,
770 /*ShowSourceName=*/ShowFilenames);
771 Printer->closeViewFile(std::move(OS));
772 });
Alex Lorenze82d89c2014-08-22 22:56:03 +0000773 }
774
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000775 Pool.wait();
776
Alex Lorenze82d89c2014-08-22 22:56:03 +0000777 return 0;
778}
779
780int CodeCoverageTool::report(int argc, const char **argv,
781 CommandLineParserType commandLineParser) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000782 auto Err = commandLineParser(argc, argv);
783 if (Err)
784 return Err;
785
Vedant Kumar4c010922016-07-06 21:44:05 +0000786 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML)
787 error("HTML output for summary reports is not yet supported.");
788
Justin Bogner953e2402014-09-20 15:31:56 +0000789 auto Coverage = load();
790 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000791 return 1;
792
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000793 CoverageReport Report(ViewOpts, *Coverage.get());
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000794 if (SourceFiles.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000795 Report.renderFileReports(llvm::outs());
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000796 else
797 Report.renderFunctionReports(SourceFiles, llvm::outs());
Alex Lorenze82d89c2014-08-22 22:56:03 +0000798 return 0;
799}
800
Vedant Kumar7101d732016-07-26 22:50:58 +0000801int CodeCoverageTool::export_(int argc, const char **argv,
802 CommandLineParserType commandLineParser) {
803
804 auto Err = commandLineParser(argc, argv);
805 if (Err)
806 return Err;
807
808 auto Coverage = load();
809 if (!Coverage) {
810 error("Could not load coverage information");
811 return 1;
812 }
813
814 exportCoverageDataToJson(ObjectFilename, *Coverage.get(), outs());
815
816 return 0;
817}
818
Justin Bognerd249a3b2014-10-30 20:57:49 +0000819int showMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000820 CodeCoverageTool Tool;
821 return Tool.run(CodeCoverageTool::Show, argc, argv);
822}
823
Justin Bognerd249a3b2014-10-30 20:57:49 +0000824int reportMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000825 CodeCoverageTool Tool;
826 return Tool.run(CodeCoverageTool::Report, argc, argv);
827}
Vedant Kumar7101d732016-07-26 22:50:58 +0000828
829int exportMain(int argc, const char *argv[]) {
830 CodeCoverageTool Tool;
831 return Tool.run(CodeCoverageTool::Export, argc, argv);
832}