blob: 51ac6ef3d8847ba563eb0b8de100b98a6d9f7327 [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 Kumarbc647982016-09-23 18:57:32 +000067 /// \brief Convert \p Path into an absolute path and append it to the list
68 /// of collected paths.
Vedant Kumarcef440f2016-06-28 16:12:18 +000069 void addCollectedPath(const std::string &Path);
70
Vedant Kumar1ce90d82016-09-22 21:49:43 +000071 /// \brief If \p Path is a regular file, collect the path. If it's a
72 /// directory, recursively collect all of the paths within the directory.
73 void collectPaths(const std::string &Path);
74
Alex Lorenze82d89c2014-08-22 22:56:03 +000075 /// \brief Return a memory buffer for the given source file.
76 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
77
Justin Bogner953e2402014-09-20 15:31:56 +000078 /// \brief Create source views for the expansions of the view.
79 void attachExpansionSubViews(SourceCoverageView &View,
80 ArrayRef<ExpansionRecord> Expansions,
Vedant Kumarf681e2e2016-07-15 01:19:33 +000081 const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000082
Justin Bogner953e2402014-09-20 15:31:56 +000083 /// \brief Create the source view of a particular function.
Justin Bogner5a6edad2014-09-19 19:07:17 +000084 std::unique_ptr<SourceCoverageView>
Vedant Kumarf681e2e2016-07-15 01:19:33 +000085 createFunctionView(const FunctionRecord &Function,
86 const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000087
88 /// \brief Create the main source view of a particular source file.
Justin Bogner5a6edad2014-09-19 19:07:17 +000089 std::unique_ptr<SourceCoverageView>
Vedant Kumarf681e2e2016-07-15 01:19:33 +000090 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000091
Vedant Kumarf681e2e2016-07-15 01:19:33 +000092 /// \brief Load the coverage mapping data. Return nullptr if an error occured.
Justin Bogner953e2402014-09-20 15:31:56 +000093 std::unique_ptr<CoverageMapping> load();
Alex Lorenze82d89c2014-08-22 22:56:03 +000094
Vedant Kumar424f51b2016-07-15 22:44:57 +000095 /// \brief If a demangler is available, demangle all symbol names.
96 void demangleSymbols(const CoverageMapping &Coverage);
97
98 /// \brief Demangle \p Sym if possible. Otherwise, just return \p Sym.
99 StringRef getSymbolForHumans(StringRef Sym) const;
100
Benjamin Kramerc321e532016-06-08 19:09:22 +0000101 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000102
103 int show(int argc, const char **argv,
104 CommandLineParserType commandLineParser);
105
106 int report(int argc, const char **argv,
107 CommandLineParserType commandLineParser);
108
Vedant Kumar7101d732016-07-26 22:50:58 +0000109 int export_(int argc, const char **argv,
110 CommandLineParserType commandLineParser);
111
Justin Bognerf6c50552014-10-30 20:51:24 +0000112 std::string ObjectFilename;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000113 CoverageViewOptions ViewOpts;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000114 CoverageFiltersMatchAll Filters;
Vedant Kumar46103672016-09-22 21:49:47 +0000115
116 /// The path to the indexed profile.
117 std::string PGOFilename;
118
119 /// A list of input source files.
Vedant Kumarbc647982016-09-23 18:57:32 +0000120 std::vector<std::string> SourceFiles;
Vedant Kumar46103672016-09-22 21:49:47 +0000121
122 /// Whether or not we're in -filename-equivalence mode.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000123 bool CompareFilenamesOnly;
Vedant Kumar46103672016-09-22 21:49:47 +0000124
125 /// In -filename-equivalence mode, this maps absolute paths from the
126 /// coverage mapping data to input source files.
Justin Bogner116c1662014-09-19 08:13:12 +0000127 StringMap<std::string> RemappedFilenames;
Vedant Kumar46103672016-09-22 21:49:47 +0000128
129 /// The architecture the coverage mapping data targets.
Frederic Rissebc162a2015-06-22 21:33:24 +0000130 std::string CoverageArch;
Vedant Kumarcef440f2016-06-28 16:12:18 +0000131
Vedant Kumar424f51b2016-07-15 22:44:57 +0000132 /// A cache for demangled symbol names.
133 StringMap<std::string> DemangledNames;
134
Vedant Kumar6ab6b362016-07-15 22:44:54 +0000135 /// Errors and warnings which have not been printed.
Vedant Kumarb3020632016-07-18 17:53:12 +0000136 std::mutex ErrsLock;
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000137
Vedant Kumar6ab6b362016-07-15 22:44:54 +0000138 /// A container for input source file buffers.
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000139 std::mutex LoadedSourceFilesLock;
140 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
141 LoadedSourceFiles;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000142};
143}
144
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000145static std::string getErrorString(const Twine &Message, StringRef Whence,
146 bool Warning) {
147 std::string Str = (Warning ? "warning" : "error");
148 Str += ": ";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000149 if (!Whence.empty())
Vedant Kumarb95dc462016-07-15 01:53:39 +0000150 Str += Whence.str() + ": ";
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000151 Str += Message.str() + "\n";
152 return Str;
153}
154
155void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000156 std::unique_lock<std::mutex> Guard{ErrsLock};
157 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
158 << getErrorString(Message, Whence, false);
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000159}
160
Vedant Kumarb3020632016-07-18 17:53:12 +0000161void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
162 std::unique_lock<std::mutex> Guard{ErrsLock};
163 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
164 << getErrorString(Message, Whence, true);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000165}
166
Vedant Kumarcef440f2016-06-28 16:12:18 +0000167void CodeCoverageTool::addCollectedPath(const std::string &Path) {
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000168 if (CompareFilenamesOnly) {
Vedant Kumarbc647982016-09-23 18:57:32 +0000169 SourceFiles.emplace_back(Path);
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000170 } else {
171 SmallString<128> EffectivePath(Path);
172 if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
173 error(EC.message(), Path);
174 return;
175 }
176 sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
Vedant Kumarbc647982016-09-23 18:57:32 +0000177 SourceFiles.emplace_back(EffectivePath.str());
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000178 }
Vedant Kumarcef440f2016-06-28 16:12:18 +0000179}
180
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000181void CodeCoverageTool::collectPaths(const std::string &Path) {
182 llvm::sys::fs::file_status Status;
183 llvm::sys::fs::status(Path, Status);
184 if (!llvm::sys::fs::exists(Status)) {
185 if (CompareFilenamesOnly)
186 addCollectedPath(Path);
187 else
188 error("Missing source file", Path);
189 return;
190 }
191
192 if (llvm::sys::fs::is_regular_file(Status)) {
193 addCollectedPath(Path);
194 return;
195 }
196
197 if (llvm::sys::fs::is_directory(Status)) {
198 std::error_code EC;
199 for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
200 F != E && !EC; F.increment(EC)) {
201 if (llvm::sys::fs::is_regular_file(F->path()))
202 addCollectedPath(F->path());
203 }
204 if (EC)
205 warning(EC.message(), Path);
206 }
207}
208
Alex Lorenze82d89c2014-08-22 22:56:03 +0000209ErrorOr<const MemoryBuffer &>
210CodeCoverageTool::getSourceFile(StringRef SourceFile) {
Justin Bogner116c1662014-09-19 08:13:12 +0000211 // If we've remapped filenames, look up the real location for this file.
Vedant Kumar615b85d2016-07-15 01:19:36 +0000212 std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
Justin Bogner116c1662014-09-19 08:13:12 +0000213 if (!RemappedFilenames.empty()) {
214 auto Loc = RemappedFilenames.find(SourceFile);
215 if (Loc != RemappedFilenames.end())
216 SourceFile = Loc->second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000217 }
Justin Bogner116c1662014-09-19 08:13:12 +0000218 for (const auto &Files : LoadedSourceFiles)
219 if (sys::fs::equivalent(SourceFile, Files.first))
220 return *Files.second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000221 auto Buffer = MemoryBuffer::getFile(SourceFile);
222 if (auto EC = Buffer.getError()) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000223 error(EC.message(), SourceFile);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000224 return EC;
225 }
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000226 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000227 return *LoadedSourceFiles.back().second;
228}
229
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000230void CodeCoverageTool::attachExpansionSubViews(
231 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
232 const CoverageMapping &Coverage) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000233 if (!ViewOpts.ShowExpandedRegions)
234 return;
Justin Bogner953e2402014-09-20 15:31:56 +0000235 for (const auto &Expansion : Expansions) {
236 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
237 if (ExpansionCoverage.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000238 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000239 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
240 if (!SourceBuffer)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000241 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000242
243 auto SubViewExpansions = ExpansionCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000244 auto SubView =
245 SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
246 ViewOpts, std::move(ExpansionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000247 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
248 View.addExpansion(Expansion.Region, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000249 }
250}
251
Justin Bogner5a6edad2014-09-19 19:07:17 +0000252std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +0000253CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000254 const CoverageMapping &Coverage) {
Justin Bogner953e2402014-09-20 15:31:56 +0000255 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
256 if (FunctionCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000257 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000258 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
Justin Bogner5a6edad2014-09-19 19:07:17 +0000259 if (!SourceBuffer)
260 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000261
262 auto Expansions = FunctionCoverage.getExpansions();
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000263 auto View = SourceCoverageView::create(getSymbolForHumans(Function.Name),
264 SourceBuffer.get(), ViewOpts,
265 std::move(FunctionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000266 attachExpansionSubViews(*View, Expansions, Coverage);
267
268 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000269}
270
Justin Bogner953e2402014-09-20 15:31:56 +0000271std::unique_ptr<SourceCoverageView>
272CodeCoverageTool::createSourceFileView(StringRef SourceFile,
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000273 const CoverageMapping &Coverage) {
Justin Bogner5a6edad2014-09-19 19:07:17 +0000274 auto SourceBuffer = getSourceFile(SourceFile);
275 if (!SourceBuffer)
276 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000277 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
278 if (FileCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000279 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000280
281 auto Expansions = FileCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000282 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
283 ViewOpts, std::move(FileCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000284 attachExpansionSubViews(*View, Expansions, Coverage);
285
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000286 for (const auto *Function : Coverage.getInstantiations(SourceFile)) {
Vedant Kumara8c396d2016-09-15 06:44:51 +0000287 std::unique_ptr<SourceCoverageView> SubView{nullptr};
Justin Bogner953e2402014-09-20 15:31:56 +0000288
Vedant Kumare9079772016-09-20 21:27:48 +0000289 StringRef Funcname = getSymbolForHumans(Function->Name);
290
Vedant Kumara8c396d2016-09-15 06:44:51 +0000291 if (Function->ExecutionCount > 0) {
292 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
293 auto SubViewExpansions = SubViewCoverage.getExpansions();
294 SubView = SourceCoverageView::create(
Vedant Kumare9079772016-09-20 21:27:48 +0000295 Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
Vedant Kumara8c396d2016-09-15 06:44:51 +0000296 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000297 }
Vedant Kumara8c396d2016-09-15 06:44:51 +0000298
299 unsigned FileID = Function->CountedRegions.front().FileID;
300 unsigned Line = 0;
301 for (const auto &CR : Function->CountedRegions)
302 if (CR.FileID == FileID)
303 Line = std::max(CR.LineEnd, Line);
Vedant Kumare9079772016-09-20 21:27:48 +0000304 View->addInstantiation(Funcname, Line, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000305 }
Justin Bogner5a6edad2014-09-19 19:07:17 +0000306 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000307}
308
Justin Bogner65337d12015-05-04 04:09:38 +0000309static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
310 sys::fs::file_status Status;
311 if (sys::fs::status(LHS, Status))
312 return false;
313 auto LHSTime = Status.getLastModificationTime();
314 if (sys::fs::status(RHS, Status))
315 return false;
316 auto RHSTime = Status.getLastModificationTime();
317 return LHSTime > RHSTime;
318}
319
Justin Bogner953e2402014-09-20 15:31:56 +0000320std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
Justin Bogner65337d12015-05-04 04:09:38 +0000321 if (modifiedTimeGT(ObjectFilename, PGOFilename))
Vedant Kumarb3020632016-07-18 17:53:12 +0000322 warning("profile data may be out of date - object is newer",
323 ObjectFilename);
324 auto CoverageOrErr =
325 CoverageMapping::load(ObjectFilename, PGOFilename, CoverageArch);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000326 if (Error E = CoverageOrErr.takeError()) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000327 error("Failed to load coverage: " + toString(std::move(E)), ObjectFilename);
Justin Bogner953e2402014-09-20 15:31:56 +0000328 return nullptr;
329 }
330 auto Coverage = std::move(CoverageOrErr.get());
331 unsigned Mismatched = Coverage->getMismatchedCount();
Vedant Kumarb3020632016-07-18 17:53:12 +0000332 if (Mismatched)
333 warning(utostr(Mismatched) + " functions have mismatched data");
Justin Bogner116c1662014-09-19 08:13:12 +0000334
335 if (CompareFilenamesOnly) {
Justin Bogner953e2402014-09-20 15:31:56 +0000336 auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
Justin Bogner116c1662014-09-19 08:13:12 +0000337 for (auto &SF : SourceFiles) {
338 StringRef SFBase = sys::path::filename(SF);
339 for (const auto &CF : CoveredFiles)
340 if (SFBase == sys::path::filename(CF)) {
341 RemappedFilenames[CF] = SF;
342 SF = CF;
343 break;
344 }
345 }
346 }
347
Vedant Kumar424f51b2016-07-15 22:44:57 +0000348 demangleSymbols(*Coverage);
349
Justin Bogner953e2402014-09-20 15:31:56 +0000350 return Coverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000351}
352
Vedant Kumar424f51b2016-07-15 22:44:57 +0000353void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
354 if (!ViewOpts.hasDemangler())
355 return;
356
357 // Pass function names to the demangler in a temporary file.
358 int InputFD;
359 SmallString<256> InputPath;
360 std::error_code EC =
361 sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
362 if (EC) {
363 error(InputPath, EC.message());
364 return;
365 }
366 tool_output_file InputTOF{InputPath, InputFD};
367
368 unsigned NumSymbols = 0;
369 for (const auto &Function : Coverage.getCoveredFunctions()) {
370 InputTOF.os() << Function.Name << '\n';
371 ++NumSymbols;
372 }
Vedant Kumar554357b2016-07-15 23:08:22 +0000373 InputTOF.os().close();
Vedant Kumar424f51b2016-07-15 22:44:57 +0000374
375 // Use another temporary file to store the demangler's output.
376 int OutputFD;
377 SmallString<256> OutputPath;
378 EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
379 OutputPath);
380 if (EC) {
381 error(OutputPath, EC.message());
382 return;
383 }
384 tool_output_file OutputTOF{OutputPath, OutputFD};
Vedant Kumar554357b2016-07-15 23:08:22 +0000385 OutputTOF.os().close();
Vedant Kumar424f51b2016-07-15 22:44:57 +0000386
387 // Invoke the demangler.
388 std::vector<const char *> ArgsV;
389 for (const std::string &Arg : ViewOpts.DemanglerOpts)
390 ArgsV.push_back(Arg.c_str());
391 ArgsV.push_back(nullptr);
Vedant Kumar38202c02016-07-15 23:15:35 +0000392 StringRef InputPathRef = InputPath.str();
393 StringRef OutputPathRef = OutputPath.str();
394 StringRef StderrRef;
Vedant Kumar424f51b2016-07-15 22:44:57 +0000395 const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef};
396 std::string ErrMsg;
397 int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(),
398 /*env=*/nullptr, Redirects, /*secondsToWait=*/0,
399 /*memoryLimit=*/0, &ErrMsg);
400 if (RC) {
401 error(ErrMsg, ViewOpts.DemanglerOpts[0]);
402 return;
403 }
404
405 // Parse the demangler's output.
406 auto BufOrError = MemoryBuffer::getFile(OutputPath);
407 if (!BufOrError) {
408 error(OutputPath, BufOrError.getError().message());
409 return;
410 }
411
412 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
413
414 SmallVector<StringRef, 8> Symbols;
415 StringRef DemanglerData = DemanglerBuf->getBuffer();
416 DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
417 /*KeepEmpty=*/false);
418 if (Symbols.size() != NumSymbols) {
419 error("Demangler did not provide expected number of symbols");
420 return;
421 }
422
423 // Cache the demangled names.
424 unsigned I = 0;
425 for (const auto &Function : Coverage.getCoveredFunctions())
426 DemangledNames[Function.Name] = Symbols[I++];
427}
428
429StringRef CodeCoverageTool::getSymbolForHumans(StringRef Sym) const {
430 const auto DemangledName = DemangledNames.find(Sym);
431 if (DemangledName == DemangledNames.end())
432 return Sym;
433 return DemangledName->getValue();
434}
435
Alex Lorenze82d89c2014-08-22 22:56:03 +0000436int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
Justin Bognerf6c50552014-10-30 20:51:24 +0000437 cl::opt<std::string, true> ObjectFilename(
438 cl::Positional, cl::Required, cl::location(this->ObjectFilename),
439 cl::desc("Covered executable or object file."));
440
Alex Lorenze82d89c2014-08-22 22:56:03 +0000441 cl::list<std::string> InputSourceFiles(
442 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
443
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000444 cl::opt<bool> DebugDumpCollectedPaths(
445 "dump-collected-paths", cl::Optional, cl::Hidden,
446 cl::desc("Show the collected paths to source files"));
447
Justin Bogner953e2402014-09-20 15:31:56 +0000448 cl::opt<std::string, true> PGOFilename(
449 "instr-profile", cl::Required, cl::location(this->PGOFilename),
Alex Lorenze82d89c2014-08-22 22:56:03 +0000450 cl::desc(
451 "File with the profile data obtained after an instrumented run"));
452
Justin Bogner43795352015-03-11 02:30:51 +0000453 cl::opt<std::string> Arch(
454 "arch", cl::desc("architecture of the coverage mapping binary"));
455
Alex Lorenze82d89c2014-08-22 22:56:03 +0000456 cl::opt<bool> DebugDump("dump", cl::Optional,
457 cl::desc("Show internal debug dump"));
458
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000459 cl::opt<CoverageViewOptions::OutputFormat> Format(
460 "format", cl::desc("Output format for line-based coverage reports"),
461 cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
462 "Text output"),
Vedant Kumar4c010922016-07-06 21:44:05 +0000463 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
464 "HTML output"),
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000465 clEnumValEnd),
466 cl::init(CoverageViewOptions::OutputFormat::Text));
467
Alex Lorenze82d89c2014-08-22 22:56:03 +0000468 cl::opt<bool> FilenameEquivalence(
469 "filename-equivalence", cl::Optional,
Justin Bogner116c1662014-09-19 08:13:12 +0000470 cl::desc("Treat source files as equivalent to paths in the coverage data "
471 "when the file names match, even if the full paths do not"));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000472
473 cl::OptionCategory FilteringCategory("Function filtering options");
474
475 cl::list<std::string> NameFilters(
476 "name", cl::Optional,
477 cl::desc("Show code coverage only for functions with the given name"),
478 cl::ZeroOrMore, cl::cat(FilteringCategory));
479
480 cl::list<std::string> NameRegexFilters(
481 "name-regex", cl::Optional,
482 cl::desc("Show code coverage only for functions that match the given "
483 "regular expression"),
484 cl::ZeroOrMore, cl::cat(FilteringCategory));
485
486 cl::opt<double> RegionCoverageLtFilter(
487 "region-coverage-lt", cl::Optional,
488 cl::desc("Show code coverage only for functions with region coverage "
489 "less than the given threshold"),
490 cl::cat(FilteringCategory));
491
492 cl::opt<double> RegionCoverageGtFilter(
493 "region-coverage-gt", cl::Optional,
494 cl::desc("Show code coverage only for functions with region coverage "
495 "greater than the given threshold"),
496 cl::cat(FilteringCategory));
497
498 cl::opt<double> LineCoverageLtFilter(
499 "line-coverage-lt", cl::Optional,
500 cl::desc("Show code coverage only for functions with line coverage less "
501 "than the given threshold"),
502 cl::cat(FilteringCategory));
503
504 cl::opt<double> LineCoverageGtFilter(
505 "line-coverage-gt", cl::Optional,
506 cl::desc("Show code coverage only for functions with line coverage "
507 "greater than the given threshold"),
508 cl::cat(FilteringCategory));
509
Justin Bogner9deb1d42015-03-19 04:45:16 +0000510 cl::opt<cl::boolOrDefault> UseColor(
511 "use-color", cl::desc("Emit colored output (default=autodetect)"),
512 cl::init(cl::BOU_UNSET));
Justin Bognercfb53e42015-03-19 00:02:23 +0000513
Vedant Kumar424f51b2016-07-15 22:44:57 +0000514 cl::list<std::string> DemanglerOpts(
515 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
516
Alex Lorenze82d89c2014-08-22 22:56:03 +0000517 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
518 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
519 ViewOpts.Debug = DebugDump;
520 CompareFilenamesOnly = FilenameEquivalence;
521
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000522 ViewOpts.Format = Format;
Ying Yi84dc9712016-08-24 14:27:23 +0000523 SmallString<128> ObjectFilePath(this->ObjectFilename);
524 if (std::error_code EC = sys::fs::make_absolute(ObjectFilePath)) {
525 error(EC.message(), this->ObjectFilename);
526 return 1;
527 }
Ying Yi76eb2192016-08-30 07:01:37 +0000528 sys::path::native(ObjectFilePath);
Ying Yi84dc9712016-08-24 14:27:23 +0000529 ViewOpts.ObjectFilename = ObjectFilePath.c_str();
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000530 switch (ViewOpts.Format) {
531 case CoverageViewOptions::OutputFormat::Text:
532 ViewOpts.Colors = UseColor == cl::BOU_UNSET
533 ? sys::Process::StandardOutHasColors()
534 : UseColor == cl::BOU_TRUE;
535 break;
Vedant Kumar4c010922016-07-06 21:44:05 +0000536 case CoverageViewOptions::OutputFormat::HTML:
537 if (UseColor == cl::BOU_FALSE)
538 error("Color output cannot be disabled when generating html.");
539 ViewOpts.Colors = true;
540 break;
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000541 }
Justin Bognercfb53e42015-03-19 00:02:23 +0000542
Vedant Kumar424f51b2016-07-15 22:44:57 +0000543 // If a demangler is supplied, check if it exists and register it.
544 if (DemanglerOpts.size()) {
545 auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
546 if (!DemanglerPathOrErr) {
547 error("Could not find the demangler!",
548 DemanglerPathOrErr.getError().message());
549 return 1;
550 }
551 DemanglerOpts[0] = *DemanglerPathOrErr;
552 ViewOpts.DemanglerOpts.swap(DemanglerOpts);
553 }
554
Alex Lorenze82d89c2014-08-22 22:56:03 +0000555 // Create the function filters
556 if (!NameFilters.empty() || !NameRegexFilters.empty()) {
557 auto NameFilterer = new CoverageFilters;
558 for (const auto &Name : NameFilters)
559 NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
560 for (const auto &Regex : NameRegexFilters)
561 NameFilterer->push_back(
562 llvm::make_unique<NameRegexCoverageFilter>(Regex));
563 Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
564 }
565 if (RegionCoverageLtFilter.getNumOccurrences() ||
566 RegionCoverageGtFilter.getNumOccurrences() ||
567 LineCoverageLtFilter.getNumOccurrences() ||
568 LineCoverageGtFilter.getNumOccurrences()) {
569 auto StatFilterer = new CoverageFilters;
570 if (RegionCoverageLtFilter.getNumOccurrences())
571 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
572 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
573 if (RegionCoverageGtFilter.getNumOccurrences())
574 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
575 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
576 if (LineCoverageLtFilter.getNumOccurrences())
577 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
578 LineCoverageFilter::LessThan, LineCoverageLtFilter));
579 if (LineCoverageGtFilter.getNumOccurrences())
580 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
581 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
582 Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
583 }
584
Frederic Rissebc162a2015-06-22 21:33:24 +0000585 if (!Arch.empty() &&
586 Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000587 error("Unknown architecture: " + Arch);
Frederic Rissebc162a2015-06-22 21:33:24 +0000588 return 1;
Justin Bogner43795352015-03-11 02:30:51 +0000589 }
Frederic Rissebc162a2015-06-22 21:33:24 +0000590 CoverageArch = Arch;
Justin Bogner43795352015-03-11 02:30:51 +0000591
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000592 for (const std::string &File : InputSourceFiles)
593 collectPaths(File);
594
595 if (DebugDumpCollectedPaths) {
Vedant Kumarbc647982016-09-23 18:57:32 +0000596 for (const std::string &SF : SourceFiles)
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000597 outs() << SF << '\n';
598 ::exit(0);
Justin Bogner116c1662014-09-19 08:13:12 +0000599 }
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000600
Alex Lorenze82d89c2014-08-22 22:56:03 +0000601 return 0;
602 };
603
Alex Lorenze82d89c2014-08-22 22:56:03 +0000604 switch (Cmd) {
605 case Show:
606 return show(argc, argv, commandLineParser);
607 case Report:
608 return report(argc, argv, commandLineParser);
Vedant Kumar7101d732016-07-26 22:50:58 +0000609 case Export:
610 return export_(argc, argv, commandLineParser);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000611 }
612 return 0;
613}
614
615int CodeCoverageTool::show(int argc, const char **argv,
616 CommandLineParserType commandLineParser) {
617
618 cl::OptionCategory ViewCategory("Viewing options");
619
620 cl::opt<bool> ShowLineExecutionCounts(
621 "show-line-counts", cl::Optional,
622 cl::desc("Show the execution counts for each line"), cl::init(true),
623 cl::cat(ViewCategory));
624
625 cl::opt<bool> ShowRegions(
626 "show-regions", cl::Optional,
627 cl::desc("Show the execution counts for each region"),
628 cl::cat(ViewCategory));
629
630 cl::opt<bool> ShowBestLineRegionsCounts(
631 "show-line-counts-or-regions", cl::Optional,
632 cl::desc("Show the execution counts for each line, or the execution "
633 "counts for each region on lines that have multiple regions"),
634 cl::cat(ViewCategory));
635
636 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
637 cl::desc("Show expanded source regions"),
638 cl::cat(ViewCategory));
639
640 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
641 cl::desc("Show function instantiations"),
642 cl::cat(ViewCategory));
643
Vedant Kumar7937ef32016-06-28 02:09:39 +0000644 cl::opt<std::string> ShowOutputDirectory(
645 "output-dir", cl::init(""),
646 cl::desc("Directory in which coverage information is written out"));
647 cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
648 cl::aliasopt(ShowOutputDirectory));
649
Ying Yi0ef31b72016-08-04 10:39:43 +0000650 cl::opt<uint32_t> TabSize(
Vedant Kumarad547d32016-08-04 18:00:42 +0000651 "tab-size", cl::init(2),
652 cl::desc(
653 "Set tab expansion size for html coverage reports (default = 2)"));
Ying Yi0ef31b72016-08-04 10:39:43 +0000654
Ying Yi84dc9712016-08-24 14:27:23 +0000655 cl::opt<std::string> ProjectTitle(
656 "project-title", cl::Optional,
657 cl::desc("Set project title for the coverage report"));
658
Alex Lorenze82d89c2014-08-22 22:56:03 +0000659 auto Err = commandLineParser(argc, argv);
660 if (Err)
661 return Err;
662
Alex Lorenze82d89c2014-08-22 22:56:03 +0000663 ViewOpts.ShowLineNumbers = true;
664 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
665 !ShowRegions || ShowBestLineRegionsCounts;
666 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
667 ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
668 ViewOpts.ShowExpandedRegions = ShowExpansions;
669 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
Vedant Kumar7937ef32016-06-28 02:09:39 +0000670 ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
Ying Yi0ef31b72016-08-04 10:39:43 +0000671 ViewOpts.TabSize = TabSize;
Ying Yi84dc9712016-08-24 14:27:23 +0000672 ViewOpts.ProjectTitle = ProjectTitle;
Vedant Kumar7937ef32016-06-28 02:09:39 +0000673
Vedant Kumar64d8a022016-06-28 16:12:20 +0000674 if (ViewOpts.hasOutputDirectory()) {
Vedant Kumar7937ef32016-06-28 02:09:39 +0000675 if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
676 error("Could not create output directory!", E.message());
677 return 1;
678 }
679 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000680
Ying Yi84dc9712016-08-24 14:27:23 +0000681 sys::fs::file_status Status;
682 if (sys::fs::status(PGOFilename, Status)) {
683 error("profdata file error: can not get the file status. \n");
684 return 1;
685 }
686
687 auto ModifiedTime = Status.getLastModificationTime();
688 std::string ModifiedTimeStr = ModifiedTime.str();
689 size_t found = ModifiedTimeStr.rfind(":");
690 ViewOpts.CreatedTimeStr = (found != std::string::npos)
691 ? "Created: " + ModifiedTimeStr.substr(0, found)
692 : "Created: " + ModifiedTimeStr;
693
Justin Bogner953e2402014-09-20 15:31:56 +0000694 auto Coverage = load();
695 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000696 return 1;
697
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000698 auto Printer = CoveragePrinter::create(ViewOpts);
699
Alex Lorenze82d89c2014-08-22 22:56:03 +0000700 if (!Filters.empty()) {
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000701 auto OSOrErr = Printer->createViewFile("functions", /*InToplevel=*/true);
702 if (Error E = OSOrErr.takeError()) {
Vedant Kumarb95dc462016-07-15 01:53:39 +0000703 error("Could not create view file!", toString(std::move(E)));
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000704 return 1;
705 }
706 auto OS = std::move(OSOrErr.get());
707
708 // Show functions.
Justin Bogner953e2402014-09-20 15:31:56 +0000709 for (const auto &Function : Coverage->getCoveredFunctions()) {
710 if (!Filters.matches(Function))
Alex Lorenze82d89c2014-08-22 22:56:03 +0000711 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000712
713 auto mainView = createFunctionView(Function, *Coverage);
Justin Bogner5a6edad2014-09-19 19:07:17 +0000714 if (!mainView) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000715 warning("Could not read coverage for '" + Function.Name + "'.");
Justin Bogner5a6edad2014-09-19 19:07:17 +0000716 continue;
717 }
Vedant Kumar7937ef32016-06-28 02:09:39 +0000718
Vedant Kumar7937ef32016-06-28 02:09:39 +0000719 mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000720 }
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000721
722 Printer->closeViewFile(std::move(OS));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000723 return 0;
724 }
725
726 // Show files
Ying Yi84dc9712016-08-24 14:27:23 +0000727 bool ShowFilenames =
Ying Yi24e91bd2016-09-06 21:41:38 +0000728 (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
Ying Yi84dc9712016-08-24 14:27:23 +0000729 (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000730
Justin Bogner116c1662014-09-19 08:13:12 +0000731 if (SourceFiles.empty())
Vedant Kumar64d8a022016-06-28 16:12:20 +0000732 // Get the source files from the function coverage mapping.
Justin Bogner953e2402014-09-20 15:31:56 +0000733 for (StringRef Filename : Coverage->getUniqueSourceFiles())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000734 SourceFiles.push_back(Filename);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000735
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000736 // Create an index out of the source files.
737 if (ViewOpts.hasOutputDirectory()) {
Vedant Kumara59334d2016-09-09 01:32:55 +0000738 if (Error E = Printer->createIndexFile(SourceFiles, *Coverage)) {
Vedant Kumarb95dc462016-07-15 01:53:39 +0000739 error("Could not create index file!", toString(std::move(E)));
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000740 return 1;
741 }
742 }
743
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000744 // In -output-dir mode, it's safe to use multiple threads to print files.
745 unsigned ThreadCount = 1;
746 if (ViewOpts.hasOutputDirectory())
747 ThreadCount = std::thread::hardware_concurrency();
748 ThreadPool Pool(ThreadCount);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000749
Vedant Kumarbc647982016-09-23 18:57:32 +0000750 for (const std::string &SourceFile : SourceFiles) {
751 Pool.async([this, &SourceFile, &Coverage, &Printer, ShowFilenames] {
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000752 auto View = createSourceFileView(SourceFile, *Coverage);
753 if (!View) {
Vedant Kumarbc647982016-09-23 18:57:32 +0000754 warning("The file '" + SourceFile + "' isn't covered.");
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000755 return;
756 }
757
758 auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
759 if (Error E = OSOrErr.takeError()) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000760 error("Could not create view file!", toString(std::move(E)));
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000761 return;
762 }
763 auto OS = std::move(OSOrErr.get());
764
765 View->print(*OS.get(), /*Wholefile=*/true,
766 /*ShowSourceName=*/ShowFilenames);
767 Printer->closeViewFile(std::move(OS));
768 });
Alex Lorenze82d89c2014-08-22 22:56:03 +0000769 }
770
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000771 Pool.wait();
772
Alex Lorenze82d89c2014-08-22 22:56:03 +0000773 return 0;
774}
775
776int CodeCoverageTool::report(int argc, const char **argv,
777 CommandLineParserType commandLineParser) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000778 auto Err = commandLineParser(argc, argv);
779 if (Err)
780 return Err;
781
Vedant Kumar4c010922016-07-06 21:44:05 +0000782 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML)
783 error("HTML output for summary reports is not yet supported.");
784
Justin Bogner953e2402014-09-20 15:31:56 +0000785 auto Coverage = load();
786 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000787 return 1;
788
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000789 CoverageReport Report(ViewOpts, *Coverage.get());
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000790 if (SourceFiles.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000791 Report.renderFileReports(llvm::outs());
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000792 else
793 Report.renderFunctionReports(SourceFiles, llvm::outs());
Alex Lorenze82d89c2014-08-22 22:56:03 +0000794 return 0;
795}
796
Vedant Kumar7101d732016-07-26 22:50:58 +0000797int CodeCoverageTool::export_(int argc, const char **argv,
798 CommandLineParserType commandLineParser) {
799
800 auto Err = commandLineParser(argc, argv);
801 if (Err)
802 return Err;
803
804 auto Coverage = load();
805 if (!Coverage) {
806 error("Could not load coverage information");
807 return 1;
808 }
809
810 exportCoverageDataToJson(ObjectFilename, *Coverage.get(), outs());
811
812 return 0;
813}
814
Justin Bognerd249a3b2014-10-30 20:57:49 +0000815int showMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000816 CodeCoverageTool Tool;
817 return Tool.run(CodeCoverageTool::Show, argc, argv);
818}
819
Justin Bognerd249a3b2014-10-30 20:57:49 +0000820int reportMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000821 CodeCoverageTool Tool;
822 return Tool.run(CodeCoverageTool::Report, argc, argv);
823}
Vedant Kumar7101d732016-07-26 22:50:58 +0000824
825int exportMain(int argc, const char *argv[]) {
826 CodeCoverageTool Tool;
827 return Tool.run(CodeCoverageTool::Export, argc, argv);
828}