blob: 15d257914a5b1cf68237f9bc7000b735364b223c [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"
Pavel Labath757ca882016-10-24 10:59:17 +000033#include "llvm/Support/ScopedPrinter.h"
Vedant Kumar86b2ac632016-07-13 21:38:36 +000034#include "llvm/Support/ThreadPool.h"
Vedant Kumar424f51b2016-07-15 22:44:57 +000035#include "llvm/Support/ToolOutputFile.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000036#include <functional>
Justin Bognere53be062014-09-09 05:32:18 +000037#include <system_error>
Alex Lorenze82d89c2014-08-22 22:56:03 +000038
39using namespace llvm;
40using namespace coverage;
41
Vedant Kumar7101d732016-07-26 22:50:58 +000042void exportCoverageDataToJson(StringRef ObjectFilename,
43 const coverage::CoverageMapping &CoverageMapping,
44 raw_ostream &OS);
45
Alex Lorenze82d89c2014-08-22 22:56:03 +000046namespace {
Alex Lorenze82d89c2014-08-22 22:56:03 +000047/// \brief The implementation of the coverage tool.
48class CodeCoverageTool {
49public:
50 enum Command {
51 /// \brief The show command.
52 Show,
53 /// \brief The report command.
Vedant Kumar7101d732016-07-26 22:50:58 +000054 Report,
55 /// \brief The export command.
56 Export
Alex Lorenze82d89c2014-08-22 22:56:03 +000057 };
58
Vedant Kumar46103672016-09-22 21:49:47 +000059 int run(Command Cmd, int argc, const char **argv);
60
61private:
Alex Lorenze82d89c2014-08-22 22:56:03 +000062 /// \brief Print the error message to the error output stream.
63 void error(const Twine &Message, StringRef Whence = "");
64
Vedant Kumarb3020632016-07-18 17:53:12 +000065 /// \brief Print the warning message to the error output stream.
66 void warning(const Twine &Message, StringRef Whence = "");
Vedant Kumar86b2ac632016-07-13 21:38:36 +000067
Vedant Kumarbc647982016-09-23 18:57:32 +000068 /// \brief Convert \p Path into an absolute path and append it to the list
69 /// of collected paths.
Vedant Kumarcef440f2016-06-28 16:12:18 +000070 void addCollectedPath(const std::string &Path);
71
Vedant Kumar1ce90d82016-09-22 21:49:43 +000072 /// \brief If \p Path is a regular file, collect the path. If it's a
73 /// directory, recursively collect all of the paths within the directory.
74 void collectPaths(const std::string &Path);
75
Alex Lorenze82d89c2014-08-22 22:56:03 +000076 /// \brief Return a memory buffer for the given source file.
77 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
78
Justin Bogner953e2402014-09-20 15:31:56 +000079 /// \brief Create source views for the expansions of the view.
80 void attachExpansionSubViews(SourceCoverageView &View,
81 ArrayRef<ExpansionRecord> Expansions,
Vedant Kumarf681e2e2016-07-15 01:19:33 +000082 const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000083
Justin Bogner953e2402014-09-20 15:31:56 +000084 /// \brief Create the source view of a particular function.
Justin Bogner5a6edad2014-09-19 19:07:17 +000085 std::unique_ptr<SourceCoverageView>
Vedant Kumarf681e2e2016-07-15 01:19:33 +000086 createFunctionView(const FunctionRecord &Function,
87 const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000088
89 /// \brief Create the main source view of a particular source file.
Justin Bogner5a6edad2014-09-19 19:07:17 +000090 std::unique_ptr<SourceCoverageView>
Vedant Kumarf681e2e2016-07-15 01:19:33 +000091 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000092
Vedant Kumarf681e2e2016-07-15 01:19:33 +000093 /// \brief Load the coverage mapping data. Return nullptr if an error occured.
Justin Bogner953e2402014-09-20 15:31:56 +000094 std::unique_ptr<CoverageMapping> load();
Alex Lorenze82d89c2014-08-22 22:56:03 +000095
Vedant Kumarcab52ad2016-09-23 20:13:41 +000096 /// \brief Remove input source files which aren't mapped by \p Coverage.
97 void removeUnmappedInputs(const CoverageMapping &Coverage);
98
Vedant Kumar424f51b2016-07-15 22:44:57 +000099 /// \brief If a demangler is available, demangle all symbol names.
100 void demangleSymbols(const CoverageMapping &Coverage);
101
102 /// \brief Demangle \p Sym if possible. Otherwise, just return \p Sym.
103 StringRef getSymbolForHumans(StringRef Sym) const;
104
Vedant Kumar6fd94bf2016-10-19 17:55:44 +0000105 /// \brief Write out a source file view to the filesystem.
106 void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
107 CoveragePrinter *Printer, bool ShowFilenames);
108
Benjamin Kramerc321e532016-06-08 19:09:22 +0000109 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000110
111 int show(int argc, const char **argv,
112 CommandLineParserType commandLineParser);
113
114 int report(int argc, const char **argv,
115 CommandLineParserType commandLineParser);
116
Vedant Kumar7101d732016-07-26 22:50:58 +0000117 int export_(int argc, const char **argv,
118 CommandLineParserType commandLineParser);
119
Justin Bognerf6c50552014-10-30 20:51:24 +0000120 std::string ObjectFilename;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000121 CoverageViewOptions ViewOpts;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000122 CoverageFiltersMatchAll Filters;
Vedant Kumar46103672016-09-22 21:49:47 +0000123
124 /// The path to the indexed profile.
125 std::string PGOFilename;
126
127 /// A list of input source files.
Vedant Kumarbc647982016-09-23 18:57:32 +0000128 std::vector<std::string> SourceFiles;
Vedant Kumar46103672016-09-22 21:49:47 +0000129
130 /// Whether or not we're in -filename-equivalence mode.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000131 bool CompareFilenamesOnly;
Vedant Kumar46103672016-09-22 21:49:47 +0000132
133 /// In -filename-equivalence mode, this maps absolute paths from the
134 /// coverage mapping data to input source files.
Justin Bogner116c1662014-09-19 08:13:12 +0000135 StringMap<std::string> RemappedFilenames;
Vedant Kumar46103672016-09-22 21:49:47 +0000136
137 /// The architecture the coverage mapping data targets.
Frederic Rissebc162a2015-06-22 21:33:24 +0000138 std::string CoverageArch;
Vedant Kumarcef440f2016-06-28 16:12:18 +0000139
Vedant Kumar424f51b2016-07-15 22:44:57 +0000140 /// A cache for demangled symbol names.
141 StringMap<std::string> DemangledNames;
142
Vedant Kumar6ab6b362016-07-15 22:44:54 +0000143 /// Errors and warnings which have not been printed.
Vedant Kumarb3020632016-07-18 17:53:12 +0000144 std::mutex ErrsLock;
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000145
Vedant Kumar6ab6b362016-07-15 22:44:54 +0000146 /// A container for input source file buffers.
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000147 std::mutex LoadedSourceFilesLock;
148 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
149 LoadedSourceFiles;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000150};
151}
152
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000153static std::string getErrorString(const Twine &Message, StringRef Whence,
154 bool Warning) {
155 std::string Str = (Warning ? "warning" : "error");
156 Str += ": ";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000157 if (!Whence.empty())
Vedant Kumarb95dc462016-07-15 01:53:39 +0000158 Str += Whence.str() + ": ";
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000159 Str += Message.str() + "\n";
160 return Str;
161}
162
163void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000164 std::unique_lock<std::mutex> Guard{ErrsLock};
165 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
166 << getErrorString(Message, Whence, false);
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000167}
168
Vedant Kumarb3020632016-07-18 17:53:12 +0000169void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
170 std::unique_lock<std::mutex> Guard{ErrsLock};
171 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
172 << getErrorString(Message, Whence, true);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000173}
174
Vedant Kumarcef440f2016-06-28 16:12:18 +0000175void CodeCoverageTool::addCollectedPath(const std::string &Path) {
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000176 if (CompareFilenamesOnly) {
Vedant Kumarbc647982016-09-23 18:57:32 +0000177 SourceFiles.emplace_back(Path);
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000178 } else {
179 SmallString<128> EffectivePath(Path);
180 if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
181 error(EC.message(), Path);
182 return;
183 }
184 sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
Vedant Kumarbc647982016-09-23 18:57:32 +0000185 SourceFiles.emplace_back(EffectivePath.str());
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000186 }
Vedant Kumarcef440f2016-06-28 16:12:18 +0000187}
188
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000189void CodeCoverageTool::collectPaths(const std::string &Path) {
190 llvm::sys::fs::file_status Status;
191 llvm::sys::fs::status(Path, Status);
192 if (!llvm::sys::fs::exists(Status)) {
193 if (CompareFilenamesOnly)
194 addCollectedPath(Path);
195 else
196 error("Missing source file", Path);
197 return;
198 }
199
200 if (llvm::sys::fs::is_regular_file(Status)) {
201 addCollectedPath(Path);
202 return;
203 }
204
205 if (llvm::sys::fs::is_directory(Status)) {
206 std::error_code EC;
207 for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
208 F != E && !EC; F.increment(EC)) {
209 if (llvm::sys::fs::is_regular_file(F->path()))
210 addCollectedPath(F->path());
211 }
212 if (EC)
213 warning(EC.message(), Path);
214 }
215}
216
Alex Lorenze82d89c2014-08-22 22:56:03 +0000217ErrorOr<const MemoryBuffer &>
218CodeCoverageTool::getSourceFile(StringRef SourceFile) {
Justin Bogner116c1662014-09-19 08:13:12 +0000219 // If we've remapped filenames, look up the real location for this file.
Vedant Kumar615b85d2016-07-15 01:19:36 +0000220 std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
Justin Bogner116c1662014-09-19 08:13:12 +0000221 if (!RemappedFilenames.empty()) {
222 auto Loc = RemappedFilenames.find(SourceFile);
223 if (Loc != RemappedFilenames.end())
224 SourceFile = Loc->second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000225 }
Justin Bogner116c1662014-09-19 08:13:12 +0000226 for (const auto &Files : LoadedSourceFiles)
227 if (sys::fs::equivalent(SourceFile, Files.first))
228 return *Files.second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000229 auto Buffer = MemoryBuffer::getFile(SourceFile);
230 if (auto EC = Buffer.getError()) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000231 error(EC.message(), SourceFile);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000232 return EC;
233 }
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000234 LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000235 return *LoadedSourceFiles.back().second;
236}
237
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000238void CodeCoverageTool::attachExpansionSubViews(
239 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
240 const CoverageMapping &Coverage) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000241 if (!ViewOpts.ShowExpandedRegions)
242 return;
Justin Bogner953e2402014-09-20 15:31:56 +0000243 for (const auto &Expansion : Expansions) {
244 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
245 if (ExpansionCoverage.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000246 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000247 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
248 if (!SourceBuffer)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000249 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000250
251 auto SubViewExpansions = ExpansionCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000252 auto SubView =
253 SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
254 ViewOpts, std::move(ExpansionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000255 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
256 View.addExpansion(Expansion.Region, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000257 }
258}
259
Justin Bogner5a6edad2014-09-19 19:07:17 +0000260std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +0000261CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000262 const CoverageMapping &Coverage) {
Justin Bogner953e2402014-09-20 15:31:56 +0000263 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
264 if (FunctionCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000265 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000266 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
Justin Bogner5a6edad2014-09-19 19:07:17 +0000267 if (!SourceBuffer)
268 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000269
270 auto Expansions = FunctionCoverage.getExpansions();
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000271 auto View = SourceCoverageView::create(getSymbolForHumans(Function.Name),
272 SourceBuffer.get(), ViewOpts,
273 std::move(FunctionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000274 attachExpansionSubViews(*View, Expansions, Coverage);
275
276 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000277}
278
Justin Bogner953e2402014-09-20 15:31:56 +0000279std::unique_ptr<SourceCoverageView>
280CodeCoverageTool::createSourceFileView(StringRef SourceFile,
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000281 const CoverageMapping &Coverage) {
Justin Bogner5a6edad2014-09-19 19:07:17 +0000282 auto SourceBuffer = getSourceFile(SourceFile);
283 if (!SourceBuffer)
284 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000285 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
286 if (FileCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000287 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000288
289 auto Expansions = FileCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000290 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
291 ViewOpts, std::move(FileCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000292 attachExpansionSubViews(*View, Expansions, Coverage);
293
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000294 for (const auto *Function : Coverage.getInstantiations(SourceFile)) {
Vedant Kumara8c396d2016-09-15 06:44:51 +0000295 std::unique_ptr<SourceCoverageView> SubView{nullptr};
Justin Bogner953e2402014-09-20 15:31:56 +0000296
Vedant Kumare9079772016-09-20 21:27:48 +0000297 StringRef Funcname = getSymbolForHumans(Function->Name);
298
Vedant Kumara8c396d2016-09-15 06:44:51 +0000299 if (Function->ExecutionCount > 0) {
300 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
301 auto SubViewExpansions = SubViewCoverage.getExpansions();
302 SubView = SourceCoverageView::create(
Vedant Kumare9079772016-09-20 21:27:48 +0000303 Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
Vedant Kumara8c396d2016-09-15 06:44:51 +0000304 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000305 }
Vedant Kumara8c396d2016-09-15 06:44:51 +0000306
307 unsigned FileID = Function->CountedRegions.front().FileID;
308 unsigned Line = 0;
309 for (const auto &CR : Function->CountedRegions)
310 if (CR.FileID == FileID)
311 Line = std::max(CR.LineEnd, Line);
Vedant Kumare9079772016-09-20 21:27:48 +0000312 View->addInstantiation(Funcname, Line, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000313 }
Justin Bogner5a6edad2014-09-19 19:07:17 +0000314 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000315}
316
Justin Bogner65337d12015-05-04 04:09:38 +0000317static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
318 sys::fs::file_status Status;
319 if (sys::fs::status(LHS, Status))
320 return false;
321 auto LHSTime = Status.getLastModificationTime();
322 if (sys::fs::status(RHS, Status))
323 return false;
324 auto RHSTime = Status.getLastModificationTime();
325 return LHSTime > RHSTime;
326}
327
Justin Bogner953e2402014-09-20 15:31:56 +0000328std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
Justin Bogner65337d12015-05-04 04:09:38 +0000329 if (modifiedTimeGT(ObjectFilename, PGOFilename))
Vedant Kumarb3020632016-07-18 17:53:12 +0000330 warning("profile data may be out of date - object is newer",
331 ObjectFilename);
332 auto CoverageOrErr =
333 CoverageMapping::load(ObjectFilename, PGOFilename, CoverageArch);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000334 if (Error E = CoverageOrErr.takeError()) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000335 error("Failed to load coverage: " + toString(std::move(E)), ObjectFilename);
Justin Bogner953e2402014-09-20 15:31:56 +0000336 return nullptr;
337 }
338 auto Coverage = std::move(CoverageOrErr.get());
339 unsigned Mismatched = Coverage->getMismatchedCount();
Vedant Kumarb3020632016-07-18 17:53:12 +0000340 if (Mismatched)
341 warning(utostr(Mismatched) + " functions have mismatched data");
Justin Bogner116c1662014-09-19 08:13:12 +0000342
Vedant Kumarcab52ad2016-09-23 20:13:41 +0000343 if (!SourceFiles.empty())
344 removeUnmappedInputs(*Coverage);
345
346 demangleSymbols(*Coverage);
347
348 return Coverage;
349}
350
351void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
352 std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
Vedant Kumar458808802016-09-23 18:57:35 +0000353
354 auto UncoveredFilesIt = SourceFiles.end();
355 if (!CompareFilenamesOnly) {
356 // The user may have specified source files which aren't in the coverage
357 // mapping. Filter these files away.
358 UncoveredFilesIt = std::remove_if(
359 SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) {
360 return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(),
361 SF);
362 });
363 } else {
Justin Bogner116c1662014-09-19 08:13:12 +0000364 for (auto &SF : SourceFiles) {
365 StringRef SFBase = sys::path::filename(SF);
Vedant Kumar458808802016-09-23 18:57:35 +0000366 for (const auto &CF : CoveredFiles) {
Justin Bogner116c1662014-09-19 08:13:12 +0000367 if (SFBase == sys::path::filename(CF)) {
368 RemappedFilenames[CF] = SF;
369 SF = CF;
370 break;
371 }
Vedant Kumar458808802016-09-23 18:57:35 +0000372 }
Justin Bogner116c1662014-09-19 08:13:12 +0000373 }
Vedant Kumar458808802016-09-23 18:57:35 +0000374 UncoveredFilesIt = std::remove_if(
375 SourceFiles.begin(), SourceFiles.end(),
376 [&](const std::string &SF) { return !RemappedFilenames.count(SF); });
Justin Bogner116c1662014-09-19 08:13:12 +0000377 }
378
Vedant Kumar458808802016-09-23 18:57:35 +0000379 SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
Alex Lorenze82d89c2014-08-22 22:56:03 +0000380}
381
Vedant Kumar424f51b2016-07-15 22:44:57 +0000382void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
383 if (!ViewOpts.hasDemangler())
384 return;
385
386 // Pass function names to the demangler in a temporary file.
387 int InputFD;
388 SmallString<256> InputPath;
389 std::error_code EC =
390 sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
391 if (EC) {
392 error(InputPath, EC.message());
393 return;
394 }
395 tool_output_file InputTOF{InputPath, InputFD};
396
397 unsigned NumSymbols = 0;
398 for (const auto &Function : Coverage.getCoveredFunctions()) {
399 InputTOF.os() << Function.Name << '\n';
400 ++NumSymbols;
401 }
Vedant Kumar554357b2016-07-15 23:08:22 +0000402 InputTOF.os().close();
Vedant Kumar424f51b2016-07-15 22:44:57 +0000403
404 // Use another temporary file to store the demangler's output.
405 int OutputFD;
406 SmallString<256> OutputPath;
407 EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
408 OutputPath);
409 if (EC) {
410 error(OutputPath, EC.message());
411 return;
412 }
413 tool_output_file OutputTOF{OutputPath, OutputFD};
Vedant Kumar554357b2016-07-15 23:08:22 +0000414 OutputTOF.os().close();
Vedant Kumar424f51b2016-07-15 22:44:57 +0000415
416 // Invoke the demangler.
417 std::vector<const char *> ArgsV;
418 for (const std::string &Arg : ViewOpts.DemanglerOpts)
419 ArgsV.push_back(Arg.c_str());
420 ArgsV.push_back(nullptr);
Vedant Kumar38202c02016-07-15 23:15:35 +0000421 StringRef InputPathRef = InputPath.str();
422 StringRef OutputPathRef = OutputPath.str();
423 StringRef StderrRef;
Vedant Kumar424f51b2016-07-15 22:44:57 +0000424 const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef};
425 std::string ErrMsg;
426 int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(),
427 /*env=*/nullptr, Redirects, /*secondsToWait=*/0,
428 /*memoryLimit=*/0, &ErrMsg);
429 if (RC) {
430 error(ErrMsg, ViewOpts.DemanglerOpts[0]);
431 return;
432 }
433
434 // Parse the demangler's output.
435 auto BufOrError = MemoryBuffer::getFile(OutputPath);
436 if (!BufOrError) {
437 error(OutputPath, BufOrError.getError().message());
438 return;
439 }
440
441 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
442
443 SmallVector<StringRef, 8> Symbols;
444 StringRef DemanglerData = DemanglerBuf->getBuffer();
445 DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
446 /*KeepEmpty=*/false);
447 if (Symbols.size() != NumSymbols) {
448 error("Demangler did not provide expected number of symbols");
449 return;
450 }
451
452 // Cache the demangled names.
453 unsigned I = 0;
454 for (const auto &Function : Coverage.getCoveredFunctions())
455 DemangledNames[Function.Name] = Symbols[I++];
456}
457
458StringRef CodeCoverageTool::getSymbolForHumans(StringRef Sym) const {
459 const auto DemangledName = DemangledNames.find(Sym);
460 if (DemangledName == DemangledNames.end())
461 return Sym;
462 return DemangledName->getValue();
463}
464
Vedant Kumar6fd94bf2016-10-19 17:55:44 +0000465void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
466 CoverageMapping *Coverage,
467 CoveragePrinter *Printer,
468 bool ShowFilenames) {
469 auto View = createSourceFileView(SourceFile, *Coverage);
470 if (!View) {
471 warning("The file '" + SourceFile + "' isn't covered.");
472 return;
473 }
474
475 auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
476 if (Error E = OSOrErr.takeError()) {
477 error("Could not create view file!", toString(std::move(E)));
478 return;
479 }
480 auto OS = std::move(OSOrErr.get());
481
482 View->print(*OS.get(), /*Wholefile=*/true,
483 /*ShowSourceName=*/ShowFilenames);
484 Printer->closeViewFile(std::move(OS));
485}
486
Alex Lorenze82d89c2014-08-22 22:56:03 +0000487int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
Justin Bognerf6c50552014-10-30 20:51:24 +0000488 cl::opt<std::string, true> ObjectFilename(
489 cl::Positional, cl::Required, cl::location(this->ObjectFilename),
490 cl::desc("Covered executable or object file."));
491
Alex Lorenze82d89c2014-08-22 22:56:03 +0000492 cl::list<std::string> InputSourceFiles(
493 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
494
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000495 cl::opt<bool> DebugDumpCollectedPaths(
496 "dump-collected-paths", cl::Optional, cl::Hidden,
497 cl::desc("Show the collected paths to source files"));
498
Justin Bogner953e2402014-09-20 15:31:56 +0000499 cl::opt<std::string, true> PGOFilename(
500 "instr-profile", cl::Required, cl::location(this->PGOFilename),
Alex Lorenze82d89c2014-08-22 22:56:03 +0000501 cl::desc(
502 "File with the profile data obtained after an instrumented run"));
503
Justin Bogner43795352015-03-11 02:30:51 +0000504 cl::opt<std::string> Arch(
505 "arch", cl::desc("architecture of the coverage mapping binary"));
506
Alex Lorenze82d89c2014-08-22 22:56:03 +0000507 cl::opt<bool> DebugDump("dump", cl::Optional,
508 cl::desc("Show internal debug dump"));
509
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000510 cl::opt<CoverageViewOptions::OutputFormat> Format(
511 "format", cl::desc("Output format for line-based coverage reports"),
512 cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
513 "Text output"),
Vedant Kumar4c010922016-07-06 21:44:05 +0000514 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
Mehdi Amini732afdd2016-10-08 19:41:06 +0000515 "HTML output")),
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000516 cl::init(CoverageViewOptions::OutputFormat::Text));
517
Alex Lorenze82d89c2014-08-22 22:56:03 +0000518 cl::opt<bool> FilenameEquivalence(
519 "filename-equivalence", cl::Optional,
Justin Bogner116c1662014-09-19 08:13:12 +0000520 cl::desc("Treat source files as equivalent to paths in the coverage data "
521 "when the file names match, even if the full paths do not"));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000522
523 cl::OptionCategory FilteringCategory("Function filtering options");
524
525 cl::list<std::string> NameFilters(
526 "name", cl::Optional,
527 cl::desc("Show code coverage only for functions with the given name"),
528 cl::ZeroOrMore, cl::cat(FilteringCategory));
529
530 cl::list<std::string> NameRegexFilters(
531 "name-regex", cl::Optional,
532 cl::desc("Show code coverage only for functions that match the given "
533 "regular expression"),
534 cl::ZeroOrMore, cl::cat(FilteringCategory));
535
536 cl::opt<double> RegionCoverageLtFilter(
537 "region-coverage-lt", cl::Optional,
538 cl::desc("Show code coverage only for functions with region coverage "
539 "less than the given threshold"),
540 cl::cat(FilteringCategory));
541
542 cl::opt<double> RegionCoverageGtFilter(
543 "region-coverage-gt", cl::Optional,
544 cl::desc("Show code coverage only for functions with region coverage "
545 "greater than the given threshold"),
546 cl::cat(FilteringCategory));
547
548 cl::opt<double> LineCoverageLtFilter(
549 "line-coverage-lt", cl::Optional,
550 cl::desc("Show code coverage only for functions with line coverage less "
551 "than the given threshold"),
552 cl::cat(FilteringCategory));
553
554 cl::opt<double> LineCoverageGtFilter(
555 "line-coverage-gt", cl::Optional,
556 cl::desc("Show code coverage only for functions with line coverage "
557 "greater than the given threshold"),
558 cl::cat(FilteringCategory));
559
Justin Bogner9deb1d42015-03-19 04:45:16 +0000560 cl::opt<cl::boolOrDefault> UseColor(
561 "use-color", cl::desc("Emit colored output (default=autodetect)"),
562 cl::init(cl::BOU_UNSET));
Justin Bognercfb53e42015-03-19 00:02:23 +0000563
Vedant Kumar424f51b2016-07-15 22:44:57 +0000564 cl::list<std::string> DemanglerOpts(
565 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
566
Alex Lorenze82d89c2014-08-22 22:56:03 +0000567 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
568 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
569 ViewOpts.Debug = DebugDump;
570 CompareFilenamesOnly = FilenameEquivalence;
571
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000572 ViewOpts.Format = Format;
Ying Yi84dc9712016-08-24 14:27:23 +0000573 SmallString<128> ObjectFilePath(this->ObjectFilename);
574 if (std::error_code EC = sys::fs::make_absolute(ObjectFilePath)) {
575 error(EC.message(), this->ObjectFilename);
576 return 1;
577 }
Ying Yi76eb2192016-08-30 07:01:37 +0000578 sys::path::native(ObjectFilePath);
Ying Yi84dc9712016-08-24 14:27:23 +0000579 ViewOpts.ObjectFilename = ObjectFilePath.c_str();
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000580 switch (ViewOpts.Format) {
581 case CoverageViewOptions::OutputFormat::Text:
582 ViewOpts.Colors = UseColor == cl::BOU_UNSET
583 ? sys::Process::StandardOutHasColors()
584 : UseColor == cl::BOU_TRUE;
585 break;
Vedant Kumar4c010922016-07-06 21:44:05 +0000586 case CoverageViewOptions::OutputFormat::HTML:
587 if (UseColor == cl::BOU_FALSE)
588 error("Color output cannot be disabled when generating html.");
589 ViewOpts.Colors = true;
590 break;
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000591 }
Justin Bognercfb53e42015-03-19 00:02:23 +0000592
Vedant Kumar424f51b2016-07-15 22:44:57 +0000593 // If a demangler is supplied, check if it exists and register it.
594 if (DemanglerOpts.size()) {
595 auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
596 if (!DemanglerPathOrErr) {
597 error("Could not find the demangler!",
598 DemanglerPathOrErr.getError().message());
599 return 1;
600 }
601 DemanglerOpts[0] = *DemanglerPathOrErr;
602 ViewOpts.DemanglerOpts.swap(DemanglerOpts);
603 }
604
Alex Lorenze82d89c2014-08-22 22:56:03 +0000605 // Create the function filters
606 if (!NameFilters.empty() || !NameRegexFilters.empty()) {
607 auto NameFilterer = new CoverageFilters;
608 for (const auto &Name : NameFilters)
609 NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
610 for (const auto &Regex : NameRegexFilters)
611 NameFilterer->push_back(
612 llvm::make_unique<NameRegexCoverageFilter>(Regex));
613 Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
614 }
615 if (RegionCoverageLtFilter.getNumOccurrences() ||
616 RegionCoverageGtFilter.getNumOccurrences() ||
617 LineCoverageLtFilter.getNumOccurrences() ||
618 LineCoverageGtFilter.getNumOccurrences()) {
619 auto StatFilterer = new CoverageFilters;
620 if (RegionCoverageLtFilter.getNumOccurrences())
621 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
622 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
623 if (RegionCoverageGtFilter.getNumOccurrences())
624 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
625 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
626 if (LineCoverageLtFilter.getNumOccurrences())
627 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
628 LineCoverageFilter::LessThan, LineCoverageLtFilter));
629 if (LineCoverageGtFilter.getNumOccurrences())
630 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
631 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
632 Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
633 }
634
Frederic Rissebc162a2015-06-22 21:33:24 +0000635 if (!Arch.empty() &&
636 Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000637 error("Unknown architecture: " + Arch);
Frederic Rissebc162a2015-06-22 21:33:24 +0000638 return 1;
Justin Bogner43795352015-03-11 02:30:51 +0000639 }
Frederic Rissebc162a2015-06-22 21:33:24 +0000640 CoverageArch = Arch;
Justin Bogner43795352015-03-11 02:30:51 +0000641
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000642 for (const std::string &File : InputSourceFiles)
643 collectPaths(File);
644
645 if (DebugDumpCollectedPaths) {
Vedant Kumarbc647982016-09-23 18:57:32 +0000646 for (const std::string &SF : SourceFiles)
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000647 outs() << SF << '\n';
648 ::exit(0);
Justin Bogner116c1662014-09-19 08:13:12 +0000649 }
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000650
Alex Lorenze82d89c2014-08-22 22:56:03 +0000651 return 0;
652 };
653
Alex Lorenze82d89c2014-08-22 22:56:03 +0000654 switch (Cmd) {
655 case Show:
656 return show(argc, argv, commandLineParser);
657 case Report:
658 return report(argc, argv, commandLineParser);
Vedant Kumar7101d732016-07-26 22:50:58 +0000659 case Export:
660 return export_(argc, argv, commandLineParser);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000661 }
662 return 0;
663}
664
665int CodeCoverageTool::show(int argc, const char **argv,
666 CommandLineParserType commandLineParser) {
667
668 cl::OptionCategory ViewCategory("Viewing options");
669
670 cl::opt<bool> ShowLineExecutionCounts(
671 "show-line-counts", cl::Optional,
672 cl::desc("Show the execution counts for each line"), cl::init(true),
673 cl::cat(ViewCategory));
674
675 cl::opt<bool> ShowRegions(
676 "show-regions", cl::Optional,
677 cl::desc("Show the execution counts for each region"),
678 cl::cat(ViewCategory));
679
680 cl::opt<bool> ShowBestLineRegionsCounts(
681 "show-line-counts-or-regions", cl::Optional,
682 cl::desc("Show the execution counts for each line, or the execution "
683 "counts for each region on lines that have multiple regions"),
684 cl::cat(ViewCategory));
685
686 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
687 cl::desc("Show expanded source regions"),
688 cl::cat(ViewCategory));
689
690 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
691 cl::desc("Show function instantiations"),
692 cl::cat(ViewCategory));
693
Vedant Kumar7937ef32016-06-28 02:09:39 +0000694 cl::opt<std::string> ShowOutputDirectory(
695 "output-dir", cl::init(""),
696 cl::desc("Directory in which coverage information is written out"));
697 cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
698 cl::aliasopt(ShowOutputDirectory));
699
Ying Yi0ef31b72016-08-04 10:39:43 +0000700 cl::opt<uint32_t> TabSize(
Vedant Kumarad547d32016-08-04 18:00:42 +0000701 "tab-size", cl::init(2),
702 cl::desc(
703 "Set tab expansion size for html coverage reports (default = 2)"));
Ying Yi0ef31b72016-08-04 10:39:43 +0000704
Ying Yi84dc9712016-08-24 14:27:23 +0000705 cl::opt<std::string> ProjectTitle(
706 "project-title", cl::Optional,
707 cl::desc("Set project title for the coverage report"));
708
Alex Lorenze82d89c2014-08-22 22:56:03 +0000709 auto Err = commandLineParser(argc, argv);
710 if (Err)
711 return Err;
712
Alex Lorenze82d89c2014-08-22 22:56:03 +0000713 ViewOpts.ShowLineNumbers = true;
714 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
715 !ShowRegions || ShowBestLineRegionsCounts;
716 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
717 ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
718 ViewOpts.ShowExpandedRegions = ShowExpansions;
719 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
Vedant Kumar7937ef32016-06-28 02:09:39 +0000720 ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
Ying Yi0ef31b72016-08-04 10:39:43 +0000721 ViewOpts.TabSize = TabSize;
Ying Yi84dc9712016-08-24 14:27:23 +0000722 ViewOpts.ProjectTitle = ProjectTitle;
Vedant Kumar7937ef32016-06-28 02:09:39 +0000723
Vedant Kumar64d8a022016-06-28 16:12:20 +0000724 if (ViewOpts.hasOutputDirectory()) {
Vedant Kumar7937ef32016-06-28 02:09:39 +0000725 if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
726 error("Could not create output directory!", E.message());
727 return 1;
728 }
729 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000730
Ying Yi84dc9712016-08-24 14:27:23 +0000731 sys::fs::file_status Status;
732 if (sys::fs::status(PGOFilename, Status)) {
733 error("profdata file error: can not get the file status. \n");
734 return 1;
735 }
736
737 auto ModifiedTime = Status.getLastModificationTime();
Pavel Labath757ca882016-10-24 10:59:17 +0000738 std::string ModifiedTimeStr = to_string(ModifiedTime);
Ying Yi84dc9712016-08-24 14:27:23 +0000739 size_t found = ModifiedTimeStr.rfind(":");
740 ViewOpts.CreatedTimeStr = (found != std::string::npos)
741 ? "Created: " + ModifiedTimeStr.substr(0, found)
742 : "Created: " + ModifiedTimeStr;
743
Justin Bogner953e2402014-09-20 15:31:56 +0000744 auto Coverage = load();
745 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000746 return 1;
747
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000748 auto Printer = CoveragePrinter::create(ViewOpts);
749
Alex Lorenze82d89c2014-08-22 22:56:03 +0000750 if (!Filters.empty()) {
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000751 auto OSOrErr = Printer->createViewFile("functions", /*InToplevel=*/true);
752 if (Error E = OSOrErr.takeError()) {
Vedant Kumarb95dc462016-07-15 01:53:39 +0000753 error("Could not create view file!", toString(std::move(E)));
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000754 return 1;
755 }
756 auto OS = std::move(OSOrErr.get());
757
758 // Show functions.
Justin Bogner953e2402014-09-20 15:31:56 +0000759 for (const auto &Function : Coverage->getCoveredFunctions()) {
760 if (!Filters.matches(Function))
Alex Lorenze82d89c2014-08-22 22:56:03 +0000761 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000762
763 auto mainView = createFunctionView(Function, *Coverage);
Justin Bogner5a6edad2014-09-19 19:07:17 +0000764 if (!mainView) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000765 warning("Could not read coverage for '" + Function.Name + "'.");
Justin Bogner5a6edad2014-09-19 19:07:17 +0000766 continue;
767 }
Vedant Kumar7937ef32016-06-28 02:09:39 +0000768
Vedant Kumar7937ef32016-06-28 02:09:39 +0000769 mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000770 }
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000771
772 Printer->closeViewFile(std::move(OS));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000773 return 0;
774 }
775
776 // Show files
Ying Yi84dc9712016-08-24 14:27:23 +0000777 bool ShowFilenames =
Ying Yi24e91bd2016-09-06 21:41:38 +0000778 (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
Ying Yi84dc9712016-08-24 14:27:23 +0000779 (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000780
Justin Bogner116c1662014-09-19 08:13:12 +0000781 if (SourceFiles.empty())
Vedant Kumar64d8a022016-06-28 16:12:20 +0000782 // Get the source files from the function coverage mapping.
Justin Bogner953e2402014-09-20 15:31:56 +0000783 for (StringRef Filename : Coverage->getUniqueSourceFiles())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000784 SourceFiles.push_back(Filename);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000785
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000786 // Create an index out of the source files.
787 if (ViewOpts.hasOutputDirectory()) {
Vedant Kumara59334d2016-09-09 01:32:55 +0000788 if (Error E = Printer->createIndexFile(SourceFiles, *Coverage)) {
Vedant Kumarb95dc462016-07-15 01:53:39 +0000789 error("Could not create index file!", toString(std::move(E)));
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000790 return 1;
791 }
792 }
793
Vedant Kumar6fd94bf2016-10-19 17:55:44 +0000794 // FIXME: Sink the hardware_concurrency() == 1 check into ThreadPool.
795 if (!ViewOpts.hasOutputDirectory() ||
796 std::thread::hardware_concurrency() == 1) {
797 for (const std::string &SourceFile : SourceFiles)
798 writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
799 ShowFilenames);
800 } else {
801 // In -output-dir mode, it's safe to use multiple threads to print files.
802 ThreadPool Pool;
803 for (const std::string &SourceFile : SourceFiles)
804 Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
805 Coverage.get(), Printer.get(), ShowFilenames);
806 Pool.wait();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000807 }
808
809 return 0;
810}
811
812int CodeCoverageTool::report(int argc, const char **argv,
813 CommandLineParserType commandLineParser) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000814 auto Err = commandLineParser(argc, argv);
815 if (Err)
816 return Err;
817
Vedant Kumar4c010922016-07-06 21:44:05 +0000818 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML)
819 error("HTML output for summary reports is not yet supported.");
820
Justin Bogner953e2402014-09-20 15:31:56 +0000821 auto Coverage = load();
822 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000823 return 1;
824
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000825 CoverageReport Report(ViewOpts, *Coverage.get());
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000826 if (SourceFiles.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000827 Report.renderFileReports(llvm::outs());
Justin Bogner0ef7a2a2015-02-14 02:05:05 +0000828 else
829 Report.renderFunctionReports(SourceFiles, llvm::outs());
Alex Lorenze82d89c2014-08-22 22:56:03 +0000830 return 0;
831}
832
Vedant Kumar7101d732016-07-26 22:50:58 +0000833int CodeCoverageTool::export_(int argc, const char **argv,
834 CommandLineParserType commandLineParser) {
835
836 auto Err = commandLineParser(argc, argv);
837 if (Err)
838 return Err;
839
840 auto Coverage = load();
841 if (!Coverage) {
842 error("Could not load coverage information");
843 return 1;
844 }
845
846 exportCoverageDataToJson(ObjectFilename, *Coverage.get(), outs());
847
848 return 0;
849}
850
Justin Bognerd249a3b2014-10-30 20:57:49 +0000851int showMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000852 CodeCoverageTool Tool;
853 return Tool.run(CodeCoverageTool::Show, argc, argv);
854}
855
Justin Bognerd249a3b2014-10-30 20:57:49 +0000856int reportMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000857 CodeCoverageTool Tool;
858 return Tool.run(CodeCoverageTool::Report, argc, argv);
859}
Vedant Kumar7101d732016-07-26 22:50:58 +0000860
861int exportMain(int argc, const char *argv[]) {
862 CodeCoverageTool Tool;
863 return Tool.run(CodeCoverageTool::Export, argc, argv);
864}