blob: 52e9958e92da971cdff0e376a1ea5747c99659b0 [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alex Lorenze82d89c2014-08-22 22:56:03 +00006//
7//===----------------------------------------------------------------------===//
8//
9// The 'CodeCoverageTool' class implements a command line tool to analyze and
10// report coverage information using the profiling instrumentation and code
11// coverage mapping.
12//
13//===----------------------------------------------------------------------===//
14
Max Moroz1ef3a772018-01-04 19:33:29 +000015#include "CoverageExporterJson.h"
Max Morozb2091c92018-11-09 16:10:44 +000016#include "CoverageExporterLcov.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000017#include "CoverageFilters.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000018#include "CoverageReport.h"
Vedant Kumar6e28bcd2017-02-05 20:10:58 +000019#include "CoverageSummaryInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "CoverageViewOptions.h"
Easwaran Ramandc707122016-04-29 18:53:05 +000021#include "RenderingSupport.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000022#include "SourceCoverageView.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000023#include "llvm/ADT/SmallString.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000024#include "llvm/ADT/StringRef.h"
Justin Bogner43795352015-03-11 02:30:51 +000025#include "llvm/ADT/Triple.h"
Easwaran Ramandc707122016-04-29 18:53:05 +000026#include "llvm/ProfileData/Coverage/CoverageMapping.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000027#include "llvm/ProfileData/InstrProfReader.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000028#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/FileSystem.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000030#include "llvm/Support/Format.h"
Vedant Kumar424f51b2016-07-15 22:44:57 +000031#include "llvm/Support/MemoryBuffer.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000032#include "llvm/Support/Path.h"
Justin Bognercfb53e42015-03-19 00:02:23 +000033#include "llvm/Support/Process.h"
Vedant Kumar424f51b2016-07-15 22:44:57 +000034#include "llvm/Support/Program.h"
Pavel Labath757ca882016-10-24 10:59:17 +000035#include "llvm/Support/ScopedPrinter.h"
Vedant Kumar86b2ac632016-07-13 21:38:36 +000036#include "llvm/Support/ThreadPool.h"
Max Morozcc254ba2018-01-05 16:15:07 +000037#include "llvm/Support/Threading.h"
Vedant Kumar424f51b2016-07-15 22:44:57 +000038#include "llvm/Support/ToolOutputFile.h"
Ilya Biryukovaa981c12019-11-21 11:32:17 +010039#include "llvm/Support/VirtualFileSystem.h"
Sean Evesonfa8ef352017-09-28 10:07:30 +000040
Alex Lorenze82d89c2014-08-22 22:56:03 +000041#include <functional>
Sean Evesonfa8ef352017-09-28 10:07:30 +000042#include <map>
Justin Bognere53be062014-09-09 05:32:18 +000043#include <system_error>
Alex Lorenze82d89c2014-08-22 22:56:03 +000044
45using namespace llvm;
46using namespace coverage;
47
Vedant Kumar5c61c702016-10-25 00:08:33 +000048void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping,
Vedant Kumar72c3a112017-09-08 18:44:49 +000049 const CoverageViewOptions &Options,
Vedant Kumar7101d732016-07-26 22:50:58 +000050 raw_ostream &OS);
51
Alex Lorenze82d89c2014-08-22 22:56:03 +000052namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000053/// The implementation of the coverage tool.
Alex Lorenze82d89c2014-08-22 22:56:03 +000054class CodeCoverageTool {
55public:
56 enum Command {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000057 /// The show command.
Alex Lorenze82d89c2014-08-22 22:56:03 +000058 Show,
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000059 /// The report command.
Vedant Kumar7101d732016-07-26 22:50:58 +000060 Report,
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000061 /// The export command.
Vedant Kumar7101d732016-07-26 22:50:58 +000062 Export
Alex Lorenze82d89c2014-08-22 22:56:03 +000063 };
64
Vedant Kumar46103672016-09-22 21:49:47 +000065 int run(Command Cmd, int argc, const char **argv);
66
67private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000068 /// Print the error message to the error output stream.
Alex Lorenze82d89c2014-08-22 22:56:03 +000069 void error(const Twine &Message, StringRef Whence = "");
70
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000071 /// Print the warning message to the error output stream.
Vedant Kumarb3020632016-07-18 17:53:12 +000072 void warning(const Twine &Message, StringRef Whence = "");
Vedant Kumar86b2ac632016-07-13 21:38:36 +000073
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000074 /// Convert \p Path into an absolute path and append it to the list
Vedant Kumarbc647982016-09-23 18:57:32 +000075 /// of collected paths.
Vedant Kumarcef440f2016-06-28 16:12:18 +000076 void addCollectedPath(const std::string &Path);
77
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000078 /// If \p Path is a regular file, collect the path. If it's a
Vedant Kumar1ce90d82016-09-22 21:49:43 +000079 /// directory, recursively collect all of the paths within the directory.
80 void collectPaths(const std::string &Path);
81
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000082 /// Return a memory buffer for the given source file.
Alex Lorenze82d89c2014-08-22 22:56:03 +000083 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
84
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000085 /// Create source views for the expansions of the view.
Justin Bogner953e2402014-09-20 15:31:56 +000086 void attachExpansionSubViews(SourceCoverageView &View,
87 ArrayRef<ExpansionRecord> Expansions,
Vedant Kumarf681e2e2016-07-15 01:19:33 +000088 const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000089
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000090 /// Create the source view of a particular function.
Justin Bogner5a6edad2014-09-19 19:07:17 +000091 std::unique_ptr<SourceCoverageView>
Vedant Kumarf681e2e2016-07-15 01:19:33 +000092 createFunctionView(const FunctionRecord &Function,
93 const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000094
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000095 /// Create the main source view of a particular source file.
Justin Bogner5a6edad2014-09-19 19:07:17 +000096 std::unique_ptr<SourceCoverageView>
Vedant Kumarf681e2e2016-07-15 01:19:33 +000097 createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
Alex Lorenze82d89c2014-08-22 22:56:03 +000098
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000099 /// Load the coverage mapping data. Return nullptr if an error occurred.
Justin Bogner953e2402014-09-20 15:31:56 +0000100 std::unique_ptr<CoverageMapping> load();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000101
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000102 /// Create a mapping from files in the Coverage data to local copies
Sean Eveson9edfeac2017-08-14 10:20:12 +0000103 /// (path-equivalence).
104 void remapPathNames(const CoverageMapping &Coverage);
105
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000106 /// Remove input source files which aren't mapped by \p Coverage.
Vedant Kumarcab52ad2016-09-23 20:13:41 +0000107 void removeUnmappedInputs(const CoverageMapping &Coverage);
108
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000109 /// If a demangler is available, demangle all symbol names.
Vedant Kumar424f51b2016-07-15 22:44:57 +0000110 void demangleSymbols(const CoverageMapping &Coverage);
111
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000112 /// Write out a source file view to the filesystem.
Vedant Kumar6fd94bf2016-10-19 17:55:44 +0000113 void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
114 CoveragePrinter *Printer, bool ShowFilenames);
115
Benjamin Kramerc321e532016-06-08 19:09:22 +0000116 typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000117
Max Moroz1ef3a772018-01-04 19:33:29 +0000118 int doShow(int argc, const char **argv,
Alex Lorenze82d89c2014-08-22 22:56:03 +0000119 CommandLineParserType commandLineParser);
120
Max Moroz1ef3a772018-01-04 19:33:29 +0000121 int doReport(int argc, const char **argv,
122 CommandLineParserType commandLineParser);
123
124 int doExport(int argc, const char **argv,
125 CommandLineParserType commandLineParser);
Vedant Kumar7101d732016-07-26 22:50:58 +0000126
Vedant Kumara3661ef2016-10-25 17:40:55 +0000127 std::vector<StringRef> ObjectFilenames;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000128 CoverageViewOptions ViewOpts;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000129 CoverageFiltersMatchAll Filters;
Vlad Tsyrklevich8d24d722019-10-29 22:38:38 -0700130 CoverageFilters IgnoreFilenameFilters;
Vedant Kumar46103672016-09-22 21:49:47 +0000131
132 /// The path to the indexed profile.
133 std::string PGOFilename;
134
135 /// A list of input source files.
Vedant Kumarbc647982016-09-23 18:57:32 +0000136 std::vector<std::string> SourceFiles;
Vedant Kumar46103672016-09-22 21:49:47 +0000137
Sean Eveson9edfeac2017-08-14 10:20:12 +0000138 /// In -path-equivalence mode, this maps the absolute paths from the coverage
139 /// mapping data to the input source files.
Justin Bogner116c1662014-09-19 08:13:12 +0000140 StringMap<std::string> RemappedFilenames;
Vedant Kumar46103672016-09-22 21:49:47 +0000141
Sean Eveson9edfeac2017-08-14 10:20:12 +0000142 /// The coverage data path to be remapped from, and the source path to be
143 /// remapped to, when using -path-equivalence.
144 Optional<std::pair<std::string, std::string>> PathRemapping;
145
Vedant Kumar46103672016-09-22 21:49:47 +0000146 /// The architecture the coverage mapping data targets.
Vedant Kumar4b102c32017-08-01 21:23:26 +0000147 std::vector<StringRef> CoverageArches;
Vedant Kumarcef440f2016-06-28 16:12:18 +0000148
Vedant Kumar6e28bcd2017-02-05 20:10:58 +0000149 /// A cache for demangled symbols.
150 DemangleCache DC;
Vedant Kumar424f51b2016-07-15 22:44:57 +0000151
Vedant Kumarb6bfd472017-02-05 20:10:55 +0000152 /// A lock which guards printing to stderr.
Vedant Kumarb3020632016-07-18 17:53:12 +0000153 std::mutex ErrsLock;
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000154
Vedant Kumar6ab6b362016-07-15 22:44:54 +0000155 /// A container for input source file buffers.
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000156 std::mutex LoadedSourceFilesLock;
157 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
158 LoadedSourceFiles;
Sean Evesone15300e2017-08-31 09:11:31 +0000159
160 /// Whitelist from -name-whitelist to be used for filtering.
161 std::unique_ptr<SpecialCaseList> NameWhitelist;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000162};
163}
164
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000165static std::string getErrorString(const Twine &Message, StringRef Whence,
166 bool Warning) {
167 std::string Str = (Warning ? "warning" : "error");
168 Str += ": ";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000169 if (!Whence.empty())
Vedant Kumarb95dc462016-07-15 01:53:39 +0000170 Str += Whence.str() + ": ";
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000171 Str += Message.str() + "\n";
172 return Str;
173}
174
175void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000176 std::unique_lock<std::mutex> Guard{ErrsLock};
177 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
178 << getErrorString(Message, Whence, false);
Vedant Kumar86b2ac632016-07-13 21:38:36 +0000179}
180
Vedant Kumarb3020632016-07-18 17:53:12 +0000181void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
182 std::unique_lock<std::mutex> Guard{ErrsLock};
183 ViewOpts.colored_ostream(errs(), raw_ostream::RED)
184 << getErrorString(Message, Whence, true);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000185}
186
Vedant Kumarcef440f2016-06-28 16:12:18 +0000187void CodeCoverageTool::addCollectedPath(const std::string &Path) {
Sean Eveson9edfeac2017-08-14 10:20:12 +0000188 SmallString<128> EffectivePath(Path);
189 if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
190 error(EC.message(), Path);
191 return;
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000192 }
Sean Eveson9edfeac2017-08-14 10:20:12 +0000193 sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
Vlad Tsyrklevich8d24d722019-10-29 22:38:38 -0700194 if (!IgnoreFilenameFilters.matchesFilename(EffectivePath))
Max Moroz4220f892018-04-09 15:20:35 +0000195 SourceFiles.emplace_back(EffectivePath.str());
Vedant Kumarcef440f2016-06-28 16:12:18 +0000196}
197
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000198void CodeCoverageTool::collectPaths(const std::string &Path) {
199 llvm::sys::fs::file_status Status;
200 llvm::sys::fs::status(Path, Status);
201 if (!llvm::sys::fs::exists(Status)) {
Sean Eveson9edfeac2017-08-14 10:20:12 +0000202 if (PathRemapping)
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000203 addCollectedPath(Path);
204 else
Max Moroz650fd6c2018-04-05 19:43:24 +0000205 warning("Source file doesn't exist, proceeded by ignoring it.", Path);
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000206 return;
207 }
208
209 if (llvm::sys::fs::is_regular_file(Status)) {
210 addCollectedPath(Path);
211 return;
212 }
213
214 if (llvm::sys::fs::is_directory(Status)) {
215 std::error_code EC;
216 for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
Max Moroz650fd6c2018-04-05 19:43:24 +0000217 F != E; F.increment(EC)) {
218
Sam McCall79c995c2018-10-01 12:17:05 +0000219 auto Status = F->status();
220 if (!Status) {
221 warning(Status.getError().message(), F->path());
Max Moroz650fd6c2018-04-05 19:43:24 +0000222 continue;
223 }
224
Sam McCall79c995c2018-10-01 12:17:05 +0000225 if (Status->type() == llvm::sys::fs::file_type::regular_file)
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000226 addCollectedPath(F->path());
227 }
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000228 }
229}
230
Alex Lorenze82d89c2014-08-22 22:56:03 +0000231ErrorOr<const MemoryBuffer &>
232CodeCoverageTool::getSourceFile(StringRef SourceFile) {
Justin Bogner116c1662014-09-19 08:13:12 +0000233 // If we've remapped filenames, look up the real location for this file.
Vedant Kumar615b85d2016-07-15 01:19:36 +0000234 std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
Justin Bogner116c1662014-09-19 08:13:12 +0000235 if (!RemappedFilenames.empty()) {
236 auto Loc = RemappedFilenames.find(SourceFile);
237 if (Loc != RemappedFilenames.end())
238 SourceFile = Loc->second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000239 }
Justin Bogner116c1662014-09-19 08:13:12 +0000240 for (const auto &Files : LoadedSourceFiles)
241 if (sys::fs::equivalent(SourceFile, Files.first))
242 return *Files.second;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000243 auto Buffer = MemoryBuffer::getFile(SourceFile);
244 if (auto EC = Buffer.getError()) {
Vedant Kumarb3020632016-07-18 17:53:12 +0000245 error(EC.message(), SourceFile);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000246 return EC;
247 }
Benjamin Kramerbd312432020-01-29 02:57:59 +0100248 LoadedSourceFiles.emplace_back(std::string(SourceFile),
249 std::move(Buffer.get()));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000250 return *LoadedSourceFiles.back().second;
251}
252
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000253void CodeCoverageTool::attachExpansionSubViews(
254 SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
255 const CoverageMapping &Coverage) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000256 if (!ViewOpts.ShowExpandedRegions)
257 return;
Justin Bogner953e2402014-09-20 15:31:56 +0000258 for (const auto &Expansion : Expansions) {
259 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
260 if (ExpansionCoverage.empty())
Alex Lorenze82d89c2014-08-22 22:56:03 +0000261 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000262 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
263 if (!SourceBuffer)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000264 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000265
266 auto SubViewExpansions = ExpansionCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000267 auto SubView =
268 SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
269 ViewOpts, std::move(ExpansionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000270 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
271 View.addExpansion(Expansion.Region, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000272 }
273}
274
Justin Bogner5a6edad2014-09-19 19:07:17 +0000275std::unique_ptr<SourceCoverageView>
Justin Bogner953e2402014-09-20 15:31:56 +0000276CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000277 const CoverageMapping &Coverage) {
Justin Bogner953e2402014-09-20 15:31:56 +0000278 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
279 if (FunctionCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000280 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000281 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
Justin Bogner5a6edad2014-09-19 19:07:17 +0000282 if (!SourceBuffer)
283 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000284
285 auto Expansions = FunctionCoverage.getExpansions();
Vedant Kumar6e28bcd2017-02-05 20:10:58 +0000286 auto View = SourceCoverageView::create(DC.demangle(Function.Name),
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000287 SourceBuffer.get(), ViewOpts,
288 std::move(FunctionCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000289 attachExpansionSubViews(*View, Expansions, Coverage);
290
291 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000292}
293
Justin Bogner953e2402014-09-20 15:31:56 +0000294std::unique_ptr<SourceCoverageView>
295CodeCoverageTool::createSourceFileView(StringRef SourceFile,
Vedant Kumarf681e2e2016-07-15 01:19:33 +0000296 const CoverageMapping &Coverage) {
Justin Bogner5a6edad2014-09-19 19:07:17 +0000297 auto SourceBuffer = getSourceFile(SourceFile);
298 if (!SourceBuffer)
299 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000300 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
301 if (FileCoverage.empty())
Justin Bogner5a6edad2014-09-19 19:07:17 +0000302 return nullptr;
Justin Bogner953e2402014-09-20 15:31:56 +0000303
304 auto Expansions = FileCoverage.getExpansions();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000305 auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
306 ViewOpts, std::move(FileCoverage));
Justin Bogner953e2402014-09-20 15:31:56 +0000307 attachExpansionSubViews(*View, Expansions, Coverage);
Vedant Kumar79554e42017-08-02 23:35:24 +0000308 if (!ViewOpts.ShowFunctionInstantiations)
309 return View;
Justin Bogner953e2402014-09-20 15:31:56 +0000310
Vedant Kumardde19c52017-08-02 23:35:25 +0000311 for (const auto &Group : Coverage.getInstantiationGroups(SourceFile)) {
312 // Skip functions which have a single instantiation.
313 if (Group.size() < 2)
314 continue;
Justin Bogner953e2402014-09-20 15:31:56 +0000315
Vedant Kumardde19c52017-08-02 23:35:25 +0000316 for (const FunctionRecord *Function : Group.getInstantiations()) {
317 std::unique_ptr<SourceCoverageView> SubView{nullptr};
Vedant Kumare9079772016-09-20 21:27:48 +0000318
Vedant Kumardde19c52017-08-02 23:35:25 +0000319 StringRef Funcname = DC.demangle(Function->Name);
320
321 if (Function->ExecutionCount > 0) {
322 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
323 auto SubViewExpansions = SubViewCoverage.getExpansions();
324 SubView = SourceCoverageView::create(
325 Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
326 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
327 }
328
329 unsigned FileID = Function->CountedRegions.front().FileID;
330 unsigned Line = 0;
331 for (const auto &CR : Function->CountedRegions)
332 if (CR.FileID == FileID)
333 Line = std::max(CR.LineEnd, Line);
334 View->addInstantiation(Funcname, Line, std::move(SubView));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000335 }
336 }
Justin Bogner5a6edad2014-09-19 19:07:17 +0000337 return View;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000338}
339
Justin Bogner65337d12015-05-04 04:09:38 +0000340static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
341 sys::fs::file_status Status;
342 if (sys::fs::status(LHS, Status))
343 return false;
344 auto LHSTime = Status.getLastModificationTime();
345 if (sys::fs::status(RHS, Status))
346 return false;
347 auto RHSTime = Status.getLastModificationTime();
348 return LHSTime > RHSTime;
349}
350
Justin Bogner953e2402014-09-20 15:31:56 +0000351std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
Vedant Kumara3661ef2016-10-25 17:40:55 +0000352 for (StringRef ObjectFilename : ObjectFilenames)
353 if (modifiedTimeGT(ObjectFilename, PGOFilename))
354 warning("profile data may be out of date - object is newer",
355 ObjectFilename);
Vedant Kumarb3020632016-07-18 17:53:12 +0000356 auto CoverageOrErr =
Vedant Kumar4b102c32017-08-01 21:23:26 +0000357 CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000358 if (Error E = CoverageOrErr.takeError()) {
Vedant Kumara3661ef2016-10-25 17:40:55 +0000359 error("Failed to load coverage: " + toString(std::move(E)),
360 join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "));
Justin Bogner953e2402014-09-20 15:31:56 +0000361 return nullptr;
362 }
363 auto Coverage = std::move(CoverageOrErr.get());
364 unsigned Mismatched = Coverage->getMismatchedCount();
Vedant Kumar18dd9e82017-09-21 01:11:30 +0000365 if (Mismatched) {
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000366 warning(Twine(Mismatched) + " functions have mismatched data");
Justin Bogner116c1662014-09-19 08:13:12 +0000367
Vedant Kumar18dd9e82017-09-21 01:11:30 +0000368 if (ViewOpts.Debug) {
369 for (const auto &HashMismatch : Coverage->getHashMismatches())
370 errs() << "hash-mismatch: "
371 << "No profile record found for '" << HashMismatch.first << "'"
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000372 << " with hash = 0x" << Twine::utohexstr(HashMismatch.second)
373 << '\n';
Vedant Kumar18dd9e82017-09-21 01:11:30 +0000374 }
375 }
376
Sean Eveson9edfeac2017-08-14 10:20:12 +0000377 remapPathNames(*Coverage);
378
Vedant Kumarcab52ad2016-09-23 20:13:41 +0000379 if (!SourceFiles.empty())
380 removeUnmappedInputs(*Coverage);
381
382 demangleSymbols(*Coverage);
383
384 return Coverage;
385}
386
Sean Eveson9edfeac2017-08-14 10:20:12 +0000387void CodeCoverageTool::remapPathNames(const CoverageMapping &Coverage) {
388 if (!PathRemapping)
389 return;
390
391 // Convert remapping paths to native paths with trailing seperators.
392 auto nativeWithTrailing = [](StringRef Path) -> std::string {
393 if (Path.empty())
394 return "";
395 SmallString<128> NativePath;
396 sys::path::native(Path, NativePath);
397 if (!sys::path::is_separator(NativePath.back()))
398 NativePath += sys::path::get_separator();
399 return NativePath.c_str();
400 };
401 std::string RemapFrom = nativeWithTrailing(PathRemapping->first);
402 std::string RemapTo = nativeWithTrailing(PathRemapping->second);
403
404 // Create a mapping from coverage data file paths to local paths.
405 for (StringRef Filename : Coverage.getUniqueSourceFiles()) {
406 SmallString<128> NativeFilename;
407 sys::path::native(Filename, NativeFilename);
408 if (NativeFilename.startswith(RemapFrom)) {
409 RemappedFilenames[Filename] =
410 RemapTo + NativeFilename.substr(RemapFrom.size()).str();
411 }
412 }
413
414 // Convert input files from local paths to coverage data file paths.
415 StringMap<std::string> InvRemappedFilenames;
416 for (const auto &RemappedFilename : RemappedFilenames)
Benjamin Krameradcd0262020-01-28 20:23:46 +0100417 InvRemappedFilenames[RemappedFilename.getValue()] =
418 std::string(RemappedFilename.getKey());
Sean Eveson9edfeac2017-08-14 10:20:12 +0000419
420 for (std::string &Filename : SourceFiles) {
421 SmallString<128> NativeFilename;
422 sys::path::native(Filename, NativeFilename);
423 auto CovFileName = InvRemappedFilenames.find(NativeFilename);
424 if (CovFileName != InvRemappedFilenames.end())
425 Filename = CovFileName->second;
426 }
427}
428
Vedant Kumarcab52ad2016-09-23 20:13:41 +0000429void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
430 std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
Vedant Kumar458808802016-09-23 18:57:35 +0000431
432 auto UncoveredFilesIt = SourceFiles.end();
Sean Eveson9edfeac2017-08-14 10:20:12 +0000433 // The user may have specified source files which aren't in the coverage
434 // mapping. Filter these files away.
435 UncoveredFilesIt = std::remove_if(
436 SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) {
437 return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(),
438 SF);
439 });
Justin Bogner116c1662014-09-19 08:13:12 +0000440
Vedant Kumar458808802016-09-23 18:57:35 +0000441 SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
Alex Lorenze82d89c2014-08-22 22:56:03 +0000442}
443
Vedant Kumar424f51b2016-07-15 22:44:57 +0000444void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
445 if (!ViewOpts.hasDemangler())
446 return;
447
448 // Pass function names to the demangler in a temporary file.
449 int InputFD;
450 SmallString<256> InputPath;
451 std::error_code EC =
452 sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
453 if (EC) {
454 error(InputPath, EC.message());
455 return;
456 }
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000457 ToolOutputFile InputTOF{InputPath, InputFD};
Vedant Kumar424f51b2016-07-15 22:44:57 +0000458
459 unsigned NumSymbols = 0;
460 for (const auto &Function : Coverage.getCoveredFunctions()) {
461 InputTOF.os() << Function.Name << '\n';
462 ++NumSymbols;
463 }
Vedant Kumar554357b2016-07-15 23:08:22 +0000464 InputTOF.os().close();
Vedant Kumar424f51b2016-07-15 22:44:57 +0000465
466 // Use another temporary file to store the demangler's output.
467 int OutputFD;
468 SmallString<256> OutputPath;
469 EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
470 OutputPath);
471 if (EC) {
472 error(OutputPath, EC.message());
473 return;
474 }
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000475 ToolOutputFile OutputTOF{OutputPath, OutputFD};
Vedant Kumar554357b2016-07-15 23:08:22 +0000476 OutputTOF.os().close();
Vedant Kumar424f51b2016-07-15 22:44:57 +0000477
478 // Invoke the demangler.
Zachary Turner08426e12018-06-12 17:43:52 +0000479 std::vector<StringRef> ArgsV;
480 for (StringRef Arg : ViewOpts.DemanglerOpts)
481 ArgsV.push_back(Arg);
Alexander Kornienko208eecd2017-09-13 17:03:37 +0000482 Optional<StringRef> Redirects[] = {InputPath.str(), OutputPath.str(), {""}};
Vedant Kumar424f51b2016-07-15 22:44:57 +0000483 std::string ErrMsg;
Zachary Turner08426e12018-06-12 17:43:52 +0000484 int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV,
485 /*env=*/None, Redirects, /*secondsToWait=*/0,
Vedant Kumar424f51b2016-07-15 22:44:57 +0000486 /*memoryLimit=*/0, &ErrMsg);
487 if (RC) {
488 error(ErrMsg, ViewOpts.DemanglerOpts[0]);
489 return;
490 }
491
492 // Parse the demangler's output.
493 auto BufOrError = MemoryBuffer::getFile(OutputPath);
494 if (!BufOrError) {
495 error(OutputPath, BufOrError.getError().message());
496 return;
497 }
498
499 std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
500
501 SmallVector<StringRef, 8> Symbols;
502 StringRef DemanglerData = DemanglerBuf->getBuffer();
503 DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
504 /*KeepEmpty=*/false);
505 if (Symbols.size() != NumSymbols) {
506 error("Demangler did not provide expected number of symbols");
507 return;
508 }
509
510 // Cache the demangled names.
511 unsigned I = 0;
512 for (const auto &Function : Coverage.getCoveredFunctions())
Igor Kudrin9e015da2017-02-19 14:26:52 +0000513 // On Windows, lines in the demangler's output file end with "\r\n".
514 // Splitting by '\n' keeps '\r's, so cut them now.
Benjamin Krameradcd0262020-01-28 20:23:46 +0100515 DC.DemangledNames[Function.Name] = std::string(Symbols[I++].rtrim());
Vedant Kumar424f51b2016-07-15 22:44:57 +0000516}
517
Vedant Kumar6fd94bf2016-10-19 17:55:44 +0000518void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
519 CoverageMapping *Coverage,
520 CoveragePrinter *Printer,
521 bool ShowFilenames) {
522 auto View = createSourceFileView(SourceFile, *Coverage);
523 if (!View) {
524 warning("The file '" + SourceFile + "' isn't covered.");
525 return;
526 }
527
528 auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
529 if (Error E = OSOrErr.takeError()) {
530 error("Could not create view file!", toString(std::move(E)));
531 return;
532 }
533 auto OS = std::move(OSOrErr.get());
534
535 View->print(*OS.get(), /*Wholefile=*/true,
Sean Evesonfa8ef352017-09-28 10:07:30 +0000536 /*ShowSourceName=*/ShowFilenames,
537 /*ShowTitle=*/ViewOpts.hasOutputDirectory());
Vedant Kumar6fd94bf2016-10-19 17:55:44 +0000538 Printer->closeViewFile(std::move(OS));
539}
540
Alex Lorenze82d89c2014-08-22 22:56:03 +0000541int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
Vedant Kumara3661ef2016-10-25 17:40:55 +0000542 cl::opt<std::string> CovFilename(
543 cl::Positional, cl::desc("Covered executable or object file."));
544
545 cl::list<std::string> CovFilenames(
546 "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore,
547 cl::CommaSeparated);
Justin Bognerf6c50552014-10-30 20:51:24 +0000548
Alex Lorenze82d89c2014-08-22 22:56:03 +0000549 cl::list<std::string> InputSourceFiles(
550 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
551
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000552 cl::opt<bool> DebugDumpCollectedPaths(
553 "dump-collected-paths", cl::Optional, cl::Hidden,
554 cl::desc("Show the collected paths to source files"));
555
Justin Bogner953e2402014-09-20 15:31:56 +0000556 cl::opt<std::string, true> PGOFilename(
557 "instr-profile", cl::Required, cl::location(this->PGOFilename),
Alex Lorenze82d89c2014-08-22 22:56:03 +0000558 cl::desc(
559 "File with the profile data obtained after an instrumented run"));
560
Vedant Kumar4b102c32017-08-01 21:23:26 +0000561 cl::list<std::string> Arches(
562 "arch", cl::desc("architectures of the coverage mapping binaries"));
Justin Bogner43795352015-03-11 02:30:51 +0000563
Alex Lorenze82d89c2014-08-22 22:56:03 +0000564 cl::opt<bool> DebugDump("dump", cl::Optional,
565 cl::desc("Show internal debug dump"));
566
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000567 cl::opt<CoverageViewOptions::OutputFormat> Format(
568 "format", cl::desc("Output format for line-based coverage reports"),
569 cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
570 "Text output"),
Vedant Kumar4c010922016-07-06 21:44:05 +0000571 clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
Max Morozb2091c92018-11-09 16:10:44 +0000572 "HTML output"),
573 clEnumValN(CoverageViewOptions::OutputFormat::Lcov, "lcov",
574 "lcov tracefile output")),
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000575 cl::init(CoverageViewOptions::OutputFormat::Text));
576
Sean Eveson9edfeac2017-08-14 10:20:12 +0000577 cl::opt<std::string> PathRemap(
578 "path-equivalence", cl::Optional,
579 cl::desc("<from>,<to> Map coverage data paths to local source file "
580 "paths"));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000581
582 cl::OptionCategory FilteringCategory("Function filtering options");
583
584 cl::list<std::string> NameFilters(
585 "name", cl::Optional,
586 cl::desc("Show code coverage only for functions with the given name"),
587 cl::ZeroOrMore, cl::cat(FilteringCategory));
588
Sean Evesone15300e2017-08-31 09:11:31 +0000589 cl::list<std::string> NameFilterFiles(
590 "name-whitelist", cl::Optional,
591 cl::desc("Show code coverage only for functions listed in the given "
592 "file"),
593 cl::ZeroOrMore, cl::cat(FilteringCategory));
594
Alex Lorenze82d89c2014-08-22 22:56:03 +0000595 cl::list<std::string> NameRegexFilters(
596 "name-regex", cl::Optional,
597 cl::desc("Show code coverage only for functions that match the given "
598 "regular expression"),
599 cl::ZeroOrMore, cl::cat(FilteringCategory));
600
Max Moroz4220f892018-04-09 15:20:35 +0000601 cl::list<std::string> IgnoreFilenameRegexFilters(
602 "ignore-filename-regex", cl::Optional,
603 cl::desc("Skip source code files with file paths that match the given "
604 "regular expression"),
605 cl::ZeroOrMore, cl::cat(FilteringCategory));
606
Alex Lorenze82d89c2014-08-22 22:56:03 +0000607 cl::opt<double> RegionCoverageLtFilter(
608 "region-coverage-lt", cl::Optional,
609 cl::desc("Show code coverage only for functions with region coverage "
610 "less than the given threshold"),
611 cl::cat(FilteringCategory));
612
613 cl::opt<double> RegionCoverageGtFilter(
614 "region-coverage-gt", cl::Optional,
615 cl::desc("Show code coverage only for functions with region coverage "
616 "greater than the given threshold"),
617 cl::cat(FilteringCategory));
618
619 cl::opt<double> LineCoverageLtFilter(
620 "line-coverage-lt", cl::Optional,
621 cl::desc("Show code coverage only for functions with line coverage less "
622 "than the given threshold"),
623 cl::cat(FilteringCategory));
624
625 cl::opt<double> LineCoverageGtFilter(
626 "line-coverage-gt", cl::Optional,
627 cl::desc("Show code coverage only for functions with line coverage "
628 "greater than the given threshold"),
629 cl::cat(FilteringCategory));
630
Justin Bogner9deb1d42015-03-19 04:45:16 +0000631 cl::opt<cl::boolOrDefault> UseColor(
632 "use-color", cl::desc("Emit colored output (default=autodetect)"),
633 cl::init(cl::BOU_UNSET));
Justin Bognercfb53e42015-03-19 00:02:23 +0000634
Vedant Kumar424f51b2016-07-15 22:44:57 +0000635 cl::list<std::string> DemanglerOpts(
636 "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
637
Eli Friedman50479f62017-09-11 22:56:20 +0000638 cl::opt<bool> RegionSummary(
639 "show-region-summary", cl::Optional,
640 cl::desc("Show region statistics in summary table"),
641 cl::init(true));
642
643 cl::opt<bool> InstantiationSummary(
644 "show-instantiation-summary", cl::Optional,
645 cl::desc("Show instantiation statistics in summary table"));
646
Max Morozfe4d9042017-12-11 23:17:46 +0000647 cl::opt<bool> SummaryOnly(
648 "summary-only", cl::Optional,
649 cl::desc("Export only summary information for each source file"));
650
Max Morozcc254ba2018-01-05 16:15:07 +0000651 cl::opt<unsigned> NumThreads(
652 "num-threads", cl::init(0),
653 cl::desc("Number of merge threads to use (default: autodetect)"));
654 cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
655 cl::aliasopt(NumThreads));
656
Alex Lorenze82d89c2014-08-22 22:56:03 +0000657 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
658 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
659 ViewOpts.Debug = DebugDump;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000660
Vedant Kumara3661ef2016-10-25 17:40:55 +0000661 if (!CovFilename.empty())
662 ObjectFilenames.emplace_back(CovFilename);
663 for (const std::string &Filename : CovFilenames)
664 ObjectFilenames.emplace_back(Filename);
665 if (ObjectFilenames.empty()) {
Vedant Kumar22c1b7c2016-10-25 19:52:57 +0000666 errs() << "No filenames specified!\n";
Vedant Kumara3661ef2016-10-25 17:40:55 +0000667 ::exit(1);
668 }
669
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000670 ViewOpts.Format = Format;
671 switch (ViewOpts.Format) {
672 case CoverageViewOptions::OutputFormat::Text:
673 ViewOpts.Colors = UseColor == cl::BOU_UNSET
674 ? sys::Process::StandardOutHasColors()
675 : UseColor == cl::BOU_TRUE;
676 break;
Vedant Kumar4c010922016-07-06 21:44:05 +0000677 case CoverageViewOptions::OutputFormat::HTML:
678 if (UseColor == cl::BOU_FALSE)
Vedant Kumar22c1b7c2016-10-25 19:52:57 +0000679 errs() << "Color output cannot be disabled when generating html.\n";
Vedant Kumar4c010922016-07-06 21:44:05 +0000680 ViewOpts.Colors = true;
681 break;
Max Morozb2091c92018-11-09 16:10:44 +0000682 case CoverageViewOptions::OutputFormat::Lcov:
683 if (UseColor == cl::BOU_TRUE)
684 errs() << "Color output cannot be enabled when generating lcov.\n";
685 ViewOpts.Colors = false;
686 break;
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000687 }
Justin Bognercfb53e42015-03-19 00:02:23 +0000688
Sean Eveson9edfeac2017-08-14 10:20:12 +0000689 // If path-equivalence was given and is a comma seperated pair then set
690 // PathRemapping.
691 auto EquivPair = StringRef(PathRemap).split(',');
692 if (!(EquivPair.first.empty() && EquivPair.second.empty()))
Benjamin Krameradcd0262020-01-28 20:23:46 +0100693 PathRemapping = {std::string(EquivPair.first),
694 std::string(EquivPair.second)};
Sean Eveson9edfeac2017-08-14 10:20:12 +0000695
Vedant Kumar424f51b2016-07-15 22:44:57 +0000696 // If a demangler is supplied, check if it exists and register it.
Jordan Rupprecht16a0de22018-12-20 00:57:06 +0000697 if (!DemanglerOpts.empty()) {
Vedant Kumar424f51b2016-07-15 22:44:57 +0000698 auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
699 if (!DemanglerPathOrErr) {
700 error("Could not find the demangler!",
701 DemanglerPathOrErr.getError().message());
702 return 1;
703 }
704 DemanglerOpts[0] = *DemanglerPathOrErr;
705 ViewOpts.DemanglerOpts.swap(DemanglerOpts);
706 }
707
Sean Evesone15300e2017-08-31 09:11:31 +0000708 // Read in -name-whitelist files.
709 if (!NameFilterFiles.empty()) {
710 std::string SpecialCaseListErr;
Ilya Biryukovaa981c12019-11-21 11:32:17 +0100711 NameWhitelist = SpecialCaseList::create(
712 NameFilterFiles, *vfs::getRealFileSystem(), SpecialCaseListErr);
Sean Evesone15300e2017-08-31 09:11:31 +0000713 if (!NameWhitelist)
714 error(SpecialCaseListErr);
715 }
716
Alex Lorenze82d89c2014-08-22 22:56:03 +0000717 // Create the function filters
Sean Evesone15300e2017-08-31 09:11:31 +0000718 if (!NameFilters.empty() || NameWhitelist || !NameRegexFilters.empty()) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000719 auto NameFilterer = std::make_unique<CoverageFilters>();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000720 for (const auto &Name : NameFilters)
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000721 NameFilterer->push_back(std::make_unique<NameCoverageFilter>(Name));
Sean Evesone15300e2017-08-31 09:11:31 +0000722 if (NameWhitelist)
723 NameFilterer->push_back(
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000724 std::make_unique<NameWhitelistCoverageFilter>(*NameWhitelist));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000725 for (const auto &Regex : NameRegexFilters)
726 NameFilterer->push_back(
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000727 std::make_unique<NameRegexCoverageFilter>(Regex));
Vedant Kumar8a622382017-08-04 00:36:24 +0000728 Filters.push_back(std::move(NameFilterer));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000729 }
Max Moroz4220f892018-04-09 15:20:35 +0000730
Alex Lorenze82d89c2014-08-22 22:56:03 +0000731 if (RegionCoverageLtFilter.getNumOccurrences() ||
732 RegionCoverageGtFilter.getNumOccurrences() ||
733 LineCoverageLtFilter.getNumOccurrences() ||
734 LineCoverageGtFilter.getNumOccurrences()) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000735 auto StatFilterer = std::make_unique<CoverageFilters>();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000736 if (RegionCoverageLtFilter.getNumOccurrences())
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000737 StatFilterer->push_back(std::make_unique<RegionCoverageFilter>(
Alex Lorenze82d89c2014-08-22 22:56:03 +0000738 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
739 if (RegionCoverageGtFilter.getNumOccurrences())
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000740 StatFilterer->push_back(std::make_unique<RegionCoverageFilter>(
Alex Lorenze82d89c2014-08-22 22:56:03 +0000741 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
742 if (LineCoverageLtFilter.getNumOccurrences())
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000743 StatFilterer->push_back(std::make_unique<LineCoverageFilter>(
Alex Lorenze82d89c2014-08-22 22:56:03 +0000744 LineCoverageFilter::LessThan, LineCoverageLtFilter));
745 if (LineCoverageGtFilter.getNumOccurrences())
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000746 StatFilterer->push_back(std::make_unique<LineCoverageFilter>(
Alex Lorenze82d89c2014-08-22 22:56:03 +0000747 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
Vedant Kumar8a622382017-08-04 00:36:24 +0000748 Filters.push_back(std::move(StatFilterer));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000749 }
750
Vlad Tsyrklevich8d24d722019-10-29 22:38:38 -0700751 // Create the ignore filename filters.
Max Moroz4220f892018-04-09 15:20:35 +0000752 for (const auto &RE : IgnoreFilenameRegexFilters)
Vlad Tsyrklevich8d24d722019-10-29 22:38:38 -0700753 IgnoreFilenameFilters.push_back(
754 std::make_unique<NameRegexCoverageFilter>(RE));
Max Moroz4220f892018-04-09 15:20:35 +0000755
Vedant Kumar4b102c32017-08-01 21:23:26 +0000756 if (!Arches.empty()) {
757 for (const std::string &Arch : Arches) {
758 if (Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
759 error("Unknown architecture: " + Arch);
760 return 1;
761 }
762 CoverageArches.emplace_back(Arch);
763 }
764 if (CoverageArches.size() != ObjectFilenames.size()) {
765 error("Number of architectures doesn't match the number of objects");
766 return 1;
767 }
Justin Bogner43795352015-03-11 02:30:51 +0000768 }
769
Vlad Tsyrklevich8d24d722019-10-29 22:38:38 -0700770 // IgnoreFilenameFilters are applied even when InputSourceFiles specified.
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000771 for (const std::string &File : InputSourceFiles)
772 collectPaths(File);
773
774 if (DebugDumpCollectedPaths) {
Vedant Kumarbc647982016-09-23 18:57:32 +0000775 for (const std::string &SF : SourceFiles)
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000776 outs() << SF << '\n';
777 ::exit(0);
Justin Bogner116c1662014-09-19 08:13:12 +0000778 }
Vedant Kumar1ce90d82016-09-22 21:49:43 +0000779
Eli Friedman50479f62017-09-11 22:56:20 +0000780 ViewOpts.ShowRegionSummary = RegionSummary;
781 ViewOpts.ShowInstantiationSummary = InstantiationSummary;
Max Morozfe4d9042017-12-11 23:17:46 +0000782 ViewOpts.ExportSummaryOnly = SummaryOnly;
Max Morozcc254ba2018-01-05 16:15:07 +0000783 ViewOpts.NumThreads = NumThreads;
Eli Friedman50479f62017-09-11 22:56:20 +0000784
Alex Lorenze82d89c2014-08-22 22:56:03 +0000785 return 0;
786 };
787
Alex Lorenze82d89c2014-08-22 22:56:03 +0000788 switch (Cmd) {
789 case Show:
Max Moroz1ef3a772018-01-04 19:33:29 +0000790 return doShow(argc, argv, commandLineParser);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000791 case Report:
Max Moroz1ef3a772018-01-04 19:33:29 +0000792 return doReport(argc, argv, commandLineParser);
Vedant Kumar7101d732016-07-26 22:50:58 +0000793 case Export:
Max Moroz1ef3a772018-01-04 19:33:29 +0000794 return doExport(argc, argv, commandLineParser);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000795 }
796 return 0;
797}
798
Max Moroz1ef3a772018-01-04 19:33:29 +0000799int CodeCoverageTool::doShow(int argc, const char **argv,
800 CommandLineParserType commandLineParser) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000801
802 cl::OptionCategory ViewCategory("Viewing options");
803
804 cl::opt<bool> ShowLineExecutionCounts(
805 "show-line-counts", cl::Optional,
806 cl::desc("Show the execution counts for each line"), cl::init(true),
807 cl::cat(ViewCategory));
808
809 cl::opt<bool> ShowRegions(
810 "show-regions", cl::Optional,
811 cl::desc("Show the execution counts for each region"),
812 cl::cat(ViewCategory));
813
814 cl::opt<bool> ShowBestLineRegionsCounts(
815 "show-line-counts-or-regions", cl::Optional,
816 cl::desc("Show the execution counts for each line, or the execution "
817 "counts for each region on lines that have multiple regions"),
818 cl::cat(ViewCategory));
819
820 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
821 cl::desc("Show expanded source regions"),
822 cl::cat(ViewCategory));
823
824 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
825 cl::desc("Show function instantiations"),
Vedant Kumar79554e42017-08-02 23:35:24 +0000826 cl::init(true), cl::cat(ViewCategory));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000827
Vedant Kumar7937ef32016-06-28 02:09:39 +0000828 cl::opt<std::string> ShowOutputDirectory(
829 "output-dir", cl::init(""),
830 cl::desc("Directory in which coverage information is written out"));
831 cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
832 cl::aliasopt(ShowOutputDirectory));
833
Ying Yi0ef31b72016-08-04 10:39:43 +0000834 cl::opt<uint32_t> TabSize(
Vedant Kumarad547d32016-08-04 18:00:42 +0000835 "tab-size", cl::init(2),
836 cl::desc(
837 "Set tab expansion size for html coverage reports (default = 2)"));
Ying Yi0ef31b72016-08-04 10:39:43 +0000838
Ying Yi84dc9712016-08-24 14:27:23 +0000839 cl::opt<std::string> ProjectTitle(
840 "project-title", cl::Optional,
841 cl::desc("Set project title for the coverage report"));
842
Alex Lorenze82d89c2014-08-22 22:56:03 +0000843 auto Err = commandLineParser(argc, argv);
844 if (Err)
845 return Err;
846
Max Morozb2091c92018-11-09 16:10:44 +0000847 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::Lcov) {
848 error("Lcov format should be used with 'llvm-cov export'.");
849 return 1;
850 }
851
Alex Lorenze82d89c2014-08-22 22:56:03 +0000852 ViewOpts.ShowLineNumbers = true;
853 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
854 !ShowRegions || ShowBestLineRegionsCounts;
855 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000856 ViewOpts.ShowExpandedRegions = ShowExpansions;
857 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
Vedant Kumar7937ef32016-06-28 02:09:39 +0000858 ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
Ying Yi0ef31b72016-08-04 10:39:43 +0000859 ViewOpts.TabSize = TabSize;
Ying Yi84dc9712016-08-24 14:27:23 +0000860 ViewOpts.ProjectTitle = ProjectTitle;
Vedant Kumar7937ef32016-06-28 02:09:39 +0000861
Vedant Kumar64d8a022016-06-28 16:12:20 +0000862 if (ViewOpts.hasOutputDirectory()) {
Vedant Kumar7937ef32016-06-28 02:09:39 +0000863 if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
864 error("Could not create output directory!", E.message());
865 return 1;
866 }
867 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000868
Ying Yi84dc9712016-08-24 14:27:23 +0000869 sys::fs::file_status Status;
870 if (sys::fs::status(PGOFilename, Status)) {
871 error("profdata file error: can not get the file status. \n");
872 return 1;
873 }
874
875 auto ModifiedTime = Status.getLastModificationTime();
Pavel Labath757ca882016-10-24 10:59:17 +0000876 std::string ModifiedTimeStr = to_string(ModifiedTime);
Benjamin Kramere6ba5ef2016-11-30 10:01:11 +0000877 size_t found = ModifiedTimeStr.rfind(':');
Ying Yi84dc9712016-08-24 14:27:23 +0000878 ViewOpts.CreatedTimeStr = (found != std::string::npos)
879 ? "Created: " + ModifiedTimeStr.substr(0, found)
880 : "Created: " + ModifiedTimeStr;
881
Justin Bogner953e2402014-09-20 15:31:56 +0000882 auto Coverage = load();
883 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000884 return 1;
885
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000886 auto Printer = CoveragePrinter::create(ViewOpts);
887
Sean Eveson1439fa62017-09-27 16:20:07 +0000888 if (SourceFiles.empty())
889 // Get the source files from the function coverage mapping.
Max Moroz4220f892018-04-09 15:20:35 +0000890 for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
Vlad Tsyrklevich8d24d722019-10-29 22:38:38 -0700891 if (!IgnoreFilenameFilters.matchesFilename(Filename))
Benjamin Krameradcd0262020-01-28 20:23:46 +0100892 SourceFiles.push_back(std::string(Filename));
Max Moroz4220f892018-04-09 15:20:35 +0000893 }
Sean Eveson1439fa62017-09-27 16:20:07 +0000894
895 // Create an index out of the source files.
896 if (ViewOpts.hasOutputDirectory()) {
Sean Evesonfa8ef352017-09-28 10:07:30 +0000897 if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) {
Sean Eveson1439fa62017-09-27 16:20:07 +0000898 error("Could not create index file!", toString(std::move(E)));
899 return 1;
900 }
901 }
902
Sean Evesonfa8ef352017-09-28 10:07:30 +0000903 if (!Filters.empty()) {
904 // Build the map of filenames to functions.
905 std::map<llvm::StringRef, std::vector<const FunctionRecord *>>
906 FilenameFunctionMap;
907 for (const auto &SourceFile : SourceFiles)
908 for (const auto &Function : Coverage->getCoveredFunctions(SourceFile))
909 if (Filters.matches(*Coverage.get(), Function))
910 FilenameFunctionMap[SourceFile].push_back(&Function);
911
912 // Only print filter matching functions for each file.
913 for (const auto &FileFunc : FilenameFunctionMap) {
914 StringRef File = FileFunc.first;
915 const auto &Functions = FileFunc.second;
916
917 auto OSOrErr = Printer->createViewFile(File, /*InToplevel=*/false);
918 if (Error E = OSOrErr.takeError()) {
919 error("Could not create view file!", toString(std::move(E)));
920 return 1;
921 }
922 auto OS = std::move(OSOrErr.get());
923
Sean Evesonea9dcee2017-10-04 08:54:37 +0000924 bool ShowTitle = ViewOpts.hasOutputDirectory();
Sean Evesonfa8ef352017-09-28 10:07:30 +0000925 for (const auto *Function : Functions) {
926 auto FunctionView = createFunctionView(*Function, *Coverage);
927 if (!FunctionView) {
928 warning("Could not read coverage for '" + Function->Name + "'.");
929 continue;
930 }
931 FunctionView->print(*OS.get(), /*WholeFile=*/false,
932 /*ShowSourceName=*/true, ShowTitle);
933 ShowTitle = false;
934 }
935
936 Printer->closeViewFile(std::move(OS));
937 }
938 return 0;
939 }
940
941 // Show files
942 bool ShowFilenames =
943 (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
944 (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
945
Max Morozcc254ba2018-01-05 16:15:07 +0000946 auto NumThreads = ViewOpts.NumThreads;
947
Vedant Kumar7fa75102017-07-11 01:23:29 +0000948 // If NumThreads is not specified, auto-detect a good default.
949 if (NumThreads == 0)
950 NumThreads =
951 std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
952 unsigned(SourceFiles.size())));
953
954 if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) {
Vedant Kumar6fd94bf2016-10-19 17:55:44 +0000955 for (const std::string &SourceFile : SourceFiles)
956 writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
957 ShowFilenames);
958 } else {
959 // In -output-dir mode, it's safe to use multiple threads to print files.
Vedant Kumar7fa75102017-07-11 01:23:29 +0000960 ThreadPool Pool(NumThreads);
Vedant Kumar6fd94bf2016-10-19 17:55:44 +0000961 for (const std::string &SourceFile : SourceFiles)
962 Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
963 Coverage.get(), Printer.get(), ShowFilenames);
964 Pool.wait();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000965 }
966
967 return 0;
968}
969
Max Moroz1ef3a772018-01-04 19:33:29 +0000970int CodeCoverageTool::doReport(int argc, const char **argv,
971 CommandLineParserType commandLineParser) {
Vedant Kumar62eb0fd2017-02-05 20:11:08 +0000972 cl::opt<bool> ShowFunctionSummaries(
973 "show-functions", cl::Optional, cl::init(false),
974 cl::desc("Show coverage summaries for each function"));
975
Alex Lorenze82d89c2014-08-22 22:56:03 +0000976 auto Err = commandLineParser(argc, argv);
977 if (Err)
978 return Err;
979
Vedant Kumar431359a2017-02-28 16:57:28 +0000980 if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) {
Vedant Kumar4c010922016-07-06 21:44:05 +0000981 error("HTML output for summary reports is not yet supported.");
Vedant Kumar431359a2017-02-28 16:57:28 +0000982 return 1;
Max Morozb2091c92018-11-09 16:10:44 +0000983 } else if (ViewOpts.Format == CoverageViewOptions::OutputFormat::Lcov) {
984 error("Lcov format should be used with 'llvm-cov export'.");
985 return 1;
Vedant Kumar431359a2017-02-28 16:57:28 +0000986 }
Vedant Kumar4c010922016-07-06 21:44:05 +0000987
Justin Bogner953e2402014-09-20 15:31:56 +0000988 auto Coverage = load();
989 if (!Coverage)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000990 return 1;
991
Vedant Kumar702bb9d2016-09-06 22:45:57 +0000992 CoverageReport Report(ViewOpts, *Coverage.get());
Vedant Kumarfeb3f522017-09-25 23:10:03 +0000993 if (!ShowFunctionSummaries) {
Max Moroz4a4bfa42017-10-13 14:44:51 +0000994 if (SourceFiles.empty())
Vlad Tsyrklevich8d24d722019-10-29 22:38:38 -0700995 Report.renderFileReports(llvm::outs(), IgnoreFilenameFilters);
Max Moroz4a4bfa42017-10-13 14:44:51 +0000996 else
997 Report.renderFileReports(llvm::outs(), SourceFiles);
Vedant Kumarfeb3f522017-09-25 23:10:03 +0000998 } else {
999 if (SourceFiles.empty()) {
1000 error("Source files must be specified when -show-functions=true is "
1001 "specified");
1002 return 1;
1003 }
1004
Vedant Kumarf2b067c2017-02-05 20:11:03 +00001005 Report.renderFunctionReports(SourceFiles, DC, llvm::outs());
Vedant Kumarfeb3f522017-09-25 23:10:03 +00001006 }
Alex Lorenze82d89c2014-08-22 22:56:03 +00001007 return 0;
1008}
1009
Max Moroz1ef3a772018-01-04 19:33:29 +00001010int CodeCoverageTool::doExport(int argc, const char **argv,
1011 CommandLineParserType commandLineParser) {
Vedant Kumar7101d732016-07-26 22:50:58 +00001012
Max Moroza80d9ce2019-03-14 17:49:27 +00001013 cl::OptionCategory ExportCategory("Exporting options");
1014
1015 cl::opt<bool> SkipExpansions("skip-expansions", cl::Optional,
1016 cl::desc("Don't export expanded source regions"),
1017 cl::cat(ExportCategory));
1018
1019 cl::opt<bool> SkipFunctions("skip-functions", cl::Optional,
1020 cl::desc("Don't export per-function data"),
1021 cl::cat(ExportCategory));
1022
Vedant Kumar7101d732016-07-26 22:50:58 +00001023 auto Err = commandLineParser(argc, argv);
1024 if (Err)
1025 return Err;
1026
Max Moroza80d9ce2019-03-14 17:49:27 +00001027 ViewOpts.SkipExpansions = SkipExpansions;
1028 ViewOpts.SkipFunctions = SkipFunctions;
1029
Max Morozb2091c92018-11-09 16:10:44 +00001030 if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text &&
1031 ViewOpts.Format != CoverageViewOptions::OutputFormat::Lcov) {
1032 error("Coverage data can only be exported as textual JSON or an "
1033 "lcov tracefile.");
Vedant Kumar431359a2017-02-28 16:57:28 +00001034 return 1;
1035 }
1036
Vedant Kumar7101d732016-07-26 22:50:58 +00001037 auto Coverage = load();
1038 if (!Coverage) {
1039 error("Could not load coverage information");
1040 return 1;
1041 }
1042
Max Morozb2091c92018-11-09 16:10:44 +00001043 std::unique_ptr<CoverageExporter> Exporter;
1044
1045 switch (ViewOpts.Format) {
1046 case CoverageViewOptions::OutputFormat::Text:
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001047 Exporter = std::make_unique<CoverageExporterJson>(*Coverage.get(),
Max Morozb2091c92018-11-09 16:10:44 +00001048 ViewOpts, outs());
1049 break;
1050 case CoverageViewOptions::OutputFormat::HTML:
1051 // Unreachable because we should have gracefully terminated with an error
1052 // above.
1053 llvm_unreachable("Export in HTML is not supported!");
1054 case CoverageViewOptions::OutputFormat::Lcov:
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001055 Exporter = std::make_unique<CoverageExporterLcov>(*Coverage.get(),
Max Morozb2091c92018-11-09 16:10:44 +00001056 ViewOpts, outs());
1057 break;
Max Morozb2091c92018-11-09 16:10:44 +00001058 }
Max Moroz1ef3a772018-01-04 19:33:29 +00001059
1060 if (SourceFiles.empty())
Vlad Tsyrklevich8d24d722019-10-29 22:38:38 -07001061 Exporter->renderRoot(IgnoreFilenameFilters);
Max Moroz1ef3a772018-01-04 19:33:29 +00001062 else
Max Morozb2091c92018-11-09 16:10:44 +00001063 Exporter->renderRoot(SourceFiles);
Vedant Kumar7101d732016-07-26 22:50:58 +00001064
1065 return 0;
1066}
1067
Justin Bognerd249a3b2014-10-30 20:57:49 +00001068int showMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +00001069 CodeCoverageTool Tool;
1070 return Tool.run(CodeCoverageTool::Show, argc, argv);
1071}
1072
Justin Bognerd249a3b2014-10-30 20:57:49 +00001073int reportMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +00001074 CodeCoverageTool Tool;
1075 return Tool.run(CodeCoverageTool::Report, argc, argv);
1076}
Vedant Kumar7101d732016-07-26 22:50:58 +00001077
1078int exportMain(int argc, const char *argv[]) {
1079 CodeCoverageTool Tool;
1080 return Tool.run(CodeCoverageTool::Export, argc, argv);
1081}