1 //===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The 'CodeCoverageTool' class implements a command line tool to analyze and
11 // report coverage information using the profiling instrumentation and code
14 //===----------------------------------------------------------------------===//
16 #include "RenderingSupport.h"
17 #include "CoverageFilters.h"
18 #include "CoverageReport.h"
19 #include "CoverageViewOptions.h"
20 #include "SourceCoverageView.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ProfileData/CoverageMapping.h"
24 #include "llvm/ProfileData/InstrProfReader.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/PrettyStackTrace.h"
31 #include "llvm/Support/Signals.h"
33 #include <system_error>
36 using namespace coverage;
39 /// \brief The implementation of the coverage tool.
40 class CodeCoverageTool {
43 /// \brief The show command.
45 /// \brief The report command.
49 /// \brief Print the error message to the error output stream.
50 void error(const Twine &Message, StringRef Whence = "");
52 /// \brief Return a memory buffer for the given source file.
53 ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
55 /// \brief Create source views for the expansions of the view.
56 void attachExpansionSubViews(SourceCoverageView &View,
57 ArrayRef<ExpansionRecord> Expansions,
58 CoverageMapping &Coverage);
60 /// \brief Create the source view of a particular function.
61 std::unique_ptr<SourceCoverageView>
62 createFunctionView(const FunctionRecord &Function, CoverageMapping &Coverage);
64 /// \brief Create the main source view of a particular source file.
65 std::unique_ptr<SourceCoverageView>
66 createSourceFileView(StringRef SourceFile, CoverageMapping &Coverage);
68 /// \brief Load the coverage mapping data. Return true if an error occured.
69 std::unique_ptr<CoverageMapping> load();
71 int run(Command Cmd, int argc, const char **argv);
73 typedef std::function<int(int, const char **)> CommandLineParserType;
75 int show(int argc, const char **argv,
76 CommandLineParserType commandLineParser);
78 int report(int argc, const char **argv,
79 CommandLineParserType commandLineParser);
81 std::string ObjectFilename;
82 CoverageViewOptions ViewOpts;
83 std::string PGOFilename;
84 CoverageFiltersMatchAll Filters;
85 std::vector<std::string> SourceFiles;
86 std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
88 bool CompareFilenamesOnly;
89 StringMap<std::string> RemappedFilenames;
93 void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
96 errs() << Whence << ": ";
97 errs() << Message << "\n";
100 ErrorOr<const MemoryBuffer &>
101 CodeCoverageTool::getSourceFile(StringRef SourceFile) {
102 // If we've remapped filenames, look up the real location for this file.
103 if (!RemappedFilenames.empty()) {
104 auto Loc = RemappedFilenames.find(SourceFile);
105 if (Loc != RemappedFilenames.end())
106 SourceFile = Loc->second;
108 for (const auto &Files : LoadedSourceFiles)
109 if (sys::fs::equivalent(SourceFile, Files.first))
110 return *Files.second;
111 auto Buffer = MemoryBuffer::getFile(SourceFile);
112 if (auto EC = Buffer.getError()) {
113 error(EC.message(), SourceFile);
116 LoadedSourceFiles.push_back(
117 std::make_pair(SourceFile, std::move(Buffer.get())));
118 return *LoadedSourceFiles.back().second;
122 CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View,
123 ArrayRef<ExpansionRecord> Expansions,
124 CoverageMapping &Coverage) {
125 if (!ViewOpts.ShowExpandedRegions)
127 for (const auto &Expansion : Expansions) {
128 auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
129 if (ExpansionCoverage.empty())
131 auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
135 auto SubViewExpansions = ExpansionCoverage.getExpansions();
136 auto SubView = llvm::make_unique<SourceCoverageView>(
137 SourceBuffer.get(), ViewOpts, std::move(ExpansionCoverage));
138 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
139 View.addExpansion(Expansion.Region, std::move(SubView));
143 std::unique_ptr<SourceCoverageView>
144 CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
145 CoverageMapping &Coverage) {
146 auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
147 if (FunctionCoverage.empty())
149 auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
153 auto Expansions = FunctionCoverage.getExpansions();
154 auto View = llvm::make_unique<SourceCoverageView>(
155 SourceBuffer.get(), ViewOpts, std::move(FunctionCoverage));
156 attachExpansionSubViews(*View, Expansions, Coverage);
161 std::unique_ptr<SourceCoverageView>
162 CodeCoverageTool::createSourceFileView(StringRef SourceFile,
163 CoverageMapping &Coverage) {
164 auto SourceBuffer = getSourceFile(SourceFile);
167 auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
168 if (FileCoverage.empty())
171 auto Expansions = FileCoverage.getExpansions();
172 auto View = llvm::make_unique<SourceCoverageView>(
173 SourceBuffer.get(), ViewOpts, std::move(FileCoverage));
174 attachExpansionSubViews(*View, Expansions, Coverage);
176 for (auto Function : Coverage.getInstantiations(SourceFile)) {
177 auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
178 auto SubViewExpansions = SubViewCoverage.getExpansions();
179 auto SubView = llvm::make_unique<SourceCoverageView>(
180 SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
181 attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
184 unsigned FileID = Function->CountedRegions.front().FileID;
186 for (const auto &CR : Function->CountedRegions)
187 if (CR.FileID == FileID)
188 Line = std::max(CR.LineEnd, Line);
189 View->addInstantiation(Function->Name, Line, std::move(SubView));
195 std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
196 auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename);
197 if (std::error_code EC = CoverageOrErr.getError()) {
198 colored_ostream(errs(), raw_ostream::RED)
199 << "error: Failed to load coverage: " << EC.message();
203 auto Coverage = std::move(CoverageOrErr.get());
204 unsigned Mismatched = Coverage->getMismatchedCount();
206 colored_ostream(errs(), raw_ostream::RED)
207 << "warning: " << Mismatched << " functions have mismatched data. ";
211 if (CompareFilenamesOnly) {
212 auto CoveredFiles = Coverage.get()->getUniqueSourceFiles();
213 for (auto &SF : SourceFiles) {
214 StringRef SFBase = sys::path::filename(SF);
215 for (const auto &CF : CoveredFiles)
216 if (SFBase == sys::path::filename(CF)) {
217 RemappedFilenames[CF] = SF;
227 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
228 // Print a stack trace if we signal out.
229 sys::PrintStackTraceOnErrorSignal();
230 PrettyStackTraceProgram X(argc, argv);
231 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
233 cl::opt<std::string, true> ObjectFilename(
234 cl::Positional, cl::Required, cl::location(this->ObjectFilename),
235 cl::desc("Covered executable or object file."));
237 cl::list<std::string> InputSourceFiles(
238 cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
240 cl::opt<std::string, true> PGOFilename(
241 "instr-profile", cl::Required, cl::location(this->PGOFilename),
243 "File with the profile data obtained after an instrumented run"));
245 cl::opt<bool> DebugDump("dump", cl::Optional,
246 cl::desc("Show internal debug dump"));
248 cl::opt<bool> FilenameEquivalence(
249 "filename-equivalence", cl::Optional,
250 cl::desc("Treat source files as equivalent to paths in the coverage data "
251 "when the file names match, even if the full paths do not"));
253 cl::OptionCategory FilteringCategory("Function filtering options");
255 cl::list<std::string> NameFilters(
256 "name", cl::Optional,
257 cl::desc("Show code coverage only for functions with the given name"),
258 cl::ZeroOrMore, cl::cat(FilteringCategory));
260 cl::list<std::string> NameRegexFilters(
261 "name-regex", cl::Optional,
262 cl::desc("Show code coverage only for functions that match the given "
263 "regular expression"),
264 cl::ZeroOrMore, cl::cat(FilteringCategory));
266 cl::opt<double> RegionCoverageLtFilter(
267 "region-coverage-lt", cl::Optional,
268 cl::desc("Show code coverage only for functions with region coverage "
269 "less than the given threshold"),
270 cl::cat(FilteringCategory));
272 cl::opt<double> RegionCoverageGtFilter(
273 "region-coverage-gt", cl::Optional,
274 cl::desc("Show code coverage only for functions with region coverage "
275 "greater than the given threshold"),
276 cl::cat(FilteringCategory));
278 cl::opt<double> LineCoverageLtFilter(
279 "line-coverage-lt", cl::Optional,
280 cl::desc("Show code coverage only for functions with line coverage less "
281 "than the given threshold"),
282 cl::cat(FilteringCategory));
284 cl::opt<double> LineCoverageGtFilter(
285 "line-coverage-gt", cl::Optional,
286 cl::desc("Show code coverage only for functions with line coverage "
287 "greater than the given threshold"),
288 cl::cat(FilteringCategory));
290 auto commandLineParser = [&, this](int argc, const char **argv) -> int {
291 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
292 ViewOpts.Debug = DebugDump;
293 CompareFilenamesOnly = FilenameEquivalence;
295 // Create the function filters
296 if (!NameFilters.empty() || !NameRegexFilters.empty()) {
297 auto NameFilterer = new CoverageFilters;
298 for (const auto &Name : NameFilters)
299 NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
300 for (const auto &Regex : NameRegexFilters)
301 NameFilterer->push_back(
302 llvm::make_unique<NameRegexCoverageFilter>(Regex));
303 Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
305 if (RegionCoverageLtFilter.getNumOccurrences() ||
306 RegionCoverageGtFilter.getNumOccurrences() ||
307 LineCoverageLtFilter.getNumOccurrences() ||
308 LineCoverageGtFilter.getNumOccurrences()) {
309 auto StatFilterer = new CoverageFilters;
310 if (RegionCoverageLtFilter.getNumOccurrences())
311 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
312 RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
313 if (RegionCoverageGtFilter.getNumOccurrences())
314 StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
315 RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
316 if (LineCoverageLtFilter.getNumOccurrences())
317 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
318 LineCoverageFilter::LessThan, LineCoverageLtFilter));
319 if (LineCoverageGtFilter.getNumOccurrences())
320 StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
321 RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
322 Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
325 for (const auto &File : InputSourceFiles) {
326 SmallString<128> Path(File);
327 if (!CompareFilenamesOnly)
328 if (std::error_code EC = sys::fs::make_absolute(Path)) {
329 errs() << "error: " << File << ": " << EC.message();
332 SourceFiles.push_back(Path.str());
339 return show(argc, argv, commandLineParser);
341 return report(argc, argv, commandLineParser);
346 int CodeCoverageTool::show(int argc, const char **argv,
347 CommandLineParserType commandLineParser) {
349 cl::OptionCategory ViewCategory("Viewing options");
351 cl::opt<bool> ShowLineExecutionCounts(
352 "show-line-counts", cl::Optional,
353 cl::desc("Show the execution counts for each line"), cl::init(true),
354 cl::cat(ViewCategory));
356 cl::opt<bool> ShowRegions(
357 "show-regions", cl::Optional,
358 cl::desc("Show the execution counts for each region"),
359 cl::cat(ViewCategory));
361 cl::opt<bool> ShowBestLineRegionsCounts(
362 "show-line-counts-or-regions", cl::Optional,
363 cl::desc("Show the execution counts for each line, or the execution "
364 "counts for each region on lines that have multiple regions"),
365 cl::cat(ViewCategory));
367 cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
368 cl::desc("Show expanded source regions"),
369 cl::cat(ViewCategory));
371 cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
372 cl::desc("Show function instantiations"),
373 cl::cat(ViewCategory));
375 cl::opt<bool> NoColors("no-colors", cl::Optional,
376 cl::desc("Don't show text colors"), cl::init(false),
377 cl::cat(ViewCategory));
379 auto Err = commandLineParser(argc, argv);
383 ViewOpts.Colors = !NoColors;
384 ViewOpts.ShowLineNumbers = true;
385 ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
386 !ShowRegions || ShowBestLineRegionsCounts;
387 ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
388 ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
389 ViewOpts.ShowExpandedRegions = ShowExpansions;
390 ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
392 auto Coverage = load();
396 if (!Filters.empty()) {
398 for (const auto &Function : Coverage->getCoveredFunctions()) {
399 if (!Filters.matches(Function))
402 auto mainView = createFunctionView(Function, *Coverage);
404 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
405 << "warning: Could not read coverage for '" << Function.Name;
409 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name
412 mainView->render(outs(), /*WholeFile=*/false);
419 bool ShowFilenames = SourceFiles.size() != 1;
421 if (SourceFiles.empty())
422 // Get the source files from the function coverage mapping
423 for (StringRef Filename : Coverage->getUniqueSourceFiles())
424 SourceFiles.push_back(Filename);
426 for (const auto &SourceFile : SourceFiles) {
427 auto mainView = createSourceFileView(SourceFile, *Coverage);
429 ViewOpts.colored_ostream(outs(), raw_ostream::RED)
430 << "warning: The file '" << SourceFile << "' isn't covered.";
436 ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":";
439 mainView->render(outs(), /*Wholefile=*/true);
440 if (SourceFiles.size() > 1)
447 int CodeCoverageTool::report(int argc, const char **argv,
448 CommandLineParserType commandLineParser) {
449 cl::opt<bool> NoColors("no-colors", cl::Optional,
450 cl::desc("Don't show text colors"), cl::init(false));
452 auto Err = commandLineParser(argc, argv);
456 ViewOpts.Colors = !NoColors;
458 auto Coverage = load();
462 CoverageReport Report(ViewOpts, std::move(Coverage));
463 if (SourceFiles.empty())
464 Report.renderFileReports(llvm::outs());
466 Report.renderFunctionReports(SourceFiles, llvm::outs());
470 int showMain(int argc, const char *argv[]) {
471 CodeCoverageTool Tool;
472 return Tool.run(CodeCoverageTool::Show, argc, argv);
475 int reportMain(int argc, const char *argv[]) {
476 CodeCoverageTool Tool;
477 return Tool.run(CodeCoverageTool::Report, argc, argv);