Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 1 | //===- 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 Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 16 | #include "CoverageFilters.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 17 | #include "CoverageReport.h" |
Vedant Kumar | 6e28bcd | 2017-02-05 20:10:58 +0000 | [diff] [blame^] | 18 | #include "CoverageSummaryInfo.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 19 | #include "CoverageViewOptions.h" |
Easwaran Raman | dc70712 | 2016-04-29 18:53:05 +0000 | [diff] [blame] | 20 | #include "RenderingSupport.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 21 | #include "SourceCoverageView.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Triple.h" |
Easwaran Raman | dc70712 | 2016-04-29 18:53:05 +0000 | [diff] [blame] | 25 | #include "llvm/ProfileData/Coverage/CoverageMapping.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 26 | #include "llvm/ProfileData/InstrProfReader.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/FileSystem.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Format.h" |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MemoryBuffer.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Path.h" |
Justin Bogner | cfb53e4 | 2015-03-19 00:02:23 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Process.h" |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Program.h" |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ScopedPrinter.h" |
Vedant Kumar | 86b2ac63 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ThreadPool.h" |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ToolOutputFile.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 37 | #include <functional> |
Justin Bogner | e53be06 | 2014-09-09 05:32:18 +0000 | [diff] [blame] | 38 | #include <system_error> |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | using namespace coverage; |
| 42 | |
Vedant Kumar | 5c61c70 | 2016-10-25 00:08:33 +0000 | [diff] [blame] | 43 | void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping, |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 44 | raw_ostream &OS); |
| 45 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 46 | namespace { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 47 | /// \brief The implementation of the coverage tool. |
| 48 | class CodeCoverageTool { |
| 49 | public: |
| 50 | enum Command { |
| 51 | /// \brief The show command. |
| 52 | Show, |
| 53 | /// \brief The report command. |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 54 | Report, |
| 55 | /// \brief The export command. |
| 56 | Export |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Vedant Kumar | 4610367 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 59 | int run(Command Cmd, int argc, const char **argv); |
| 60 | |
| 61 | private: |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 62 | /// \brief Print the error message to the error output stream. |
| 63 | void error(const Twine &Message, StringRef Whence = ""); |
| 64 | |
Vedant Kumar | b302063 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 65 | /// \brief Print the warning message to the error output stream. |
| 66 | void warning(const Twine &Message, StringRef Whence = ""); |
Vedant Kumar | 86b2ac63 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 67 | |
Vedant Kumar | bc64798 | 2016-09-23 18:57:32 +0000 | [diff] [blame] | 68 | /// \brief Convert \p Path into an absolute path and append it to the list |
| 69 | /// of collected paths. |
Vedant Kumar | cef440f | 2016-06-28 16:12:18 +0000 | [diff] [blame] | 70 | void addCollectedPath(const std::string &Path); |
| 71 | |
Vedant Kumar | 1ce90d8 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 72 | /// \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 Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 76 | /// \brief Return a memory buffer for the given source file. |
| 77 | ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile); |
| 78 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 79 | /// \brief Create source views for the expansions of the view. |
| 80 | void attachExpansionSubViews(SourceCoverageView &View, |
| 81 | ArrayRef<ExpansionRecord> Expansions, |
Vedant Kumar | f681e2e | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 82 | const CoverageMapping &Coverage); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 83 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 84 | /// \brief Create the source view of a particular function. |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 85 | std::unique_ptr<SourceCoverageView> |
Vedant Kumar | f681e2e | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 86 | createFunctionView(const FunctionRecord &Function, |
| 87 | const CoverageMapping &Coverage); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 88 | |
| 89 | /// \brief Create the main source view of a particular source file. |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 90 | std::unique_ptr<SourceCoverageView> |
Vedant Kumar | f681e2e | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 91 | createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 92 | |
Simon Pilgrim | dae11f7 | 2016-11-20 13:31:13 +0000 | [diff] [blame] | 93 | /// \brief Load the coverage mapping data. Return nullptr if an error occurred. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 94 | std::unique_ptr<CoverageMapping> load(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 95 | |
Vedant Kumar | cab52ad | 2016-09-23 20:13:41 +0000 | [diff] [blame] | 96 | /// \brief Remove input source files which aren't mapped by \p Coverage. |
| 97 | void removeUnmappedInputs(const CoverageMapping &Coverage); |
| 98 | |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 99 | /// \brief If a demangler is available, demangle all symbol names. |
| 100 | void demangleSymbols(const CoverageMapping &Coverage); |
| 101 | |
Vedant Kumar | 6fd94bf | 2016-10-19 17:55:44 +0000 | [diff] [blame] | 102 | /// \brief Write out a source file view to the filesystem. |
| 103 | void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage, |
| 104 | CoveragePrinter *Printer, bool ShowFilenames); |
| 105 | |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 106 | typedef llvm::function_ref<int(int, const char **)> CommandLineParserType; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 107 | |
| 108 | int show(int argc, const char **argv, |
| 109 | CommandLineParserType commandLineParser); |
| 110 | |
| 111 | int report(int argc, const char **argv, |
| 112 | CommandLineParserType commandLineParser); |
| 113 | |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 114 | int export_(int argc, const char **argv, |
| 115 | CommandLineParserType commandLineParser); |
| 116 | |
Vedant Kumar | a3661ef | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 117 | std::vector<StringRef> ObjectFilenames; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 118 | CoverageViewOptions ViewOpts; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 119 | CoverageFiltersMatchAll Filters; |
Vedant Kumar | 4610367 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 120 | |
| 121 | /// The path to the indexed profile. |
| 122 | std::string PGOFilename; |
| 123 | |
| 124 | /// A list of input source files. |
Vedant Kumar | bc64798 | 2016-09-23 18:57:32 +0000 | [diff] [blame] | 125 | std::vector<std::string> SourceFiles; |
Vedant Kumar | 4610367 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 126 | |
| 127 | /// Whether or not we're in -filename-equivalence mode. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 128 | bool CompareFilenamesOnly; |
Vedant Kumar | 4610367 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 129 | |
| 130 | /// In -filename-equivalence mode, this maps absolute paths from the |
| 131 | /// coverage mapping data to input source files. |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 132 | StringMap<std::string> RemappedFilenames; |
Vedant Kumar | 4610367 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 133 | |
| 134 | /// The architecture the coverage mapping data targets. |
Frederic Riss | ebc162a | 2015-06-22 21:33:24 +0000 | [diff] [blame] | 135 | std::string CoverageArch; |
Vedant Kumar | cef440f | 2016-06-28 16:12:18 +0000 | [diff] [blame] | 136 | |
Vedant Kumar | 6e28bcd | 2017-02-05 20:10:58 +0000 | [diff] [blame^] | 137 | /// A cache for demangled symbols. |
| 138 | DemangleCache DC; |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 139 | |
Vedant Kumar | b6bfd47 | 2017-02-05 20:10:55 +0000 | [diff] [blame] | 140 | /// A lock which guards printing to stderr. |
Vedant Kumar | b302063 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 141 | std::mutex ErrsLock; |
Vedant Kumar | 86b2ac63 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 142 | |
Vedant Kumar | 6ab6b36 | 2016-07-15 22:44:54 +0000 | [diff] [blame] | 143 | /// A container for input source file buffers. |
Vedant Kumar | 86b2ac63 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 144 | std::mutex LoadedSourceFilesLock; |
| 145 | std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>> |
| 146 | LoadedSourceFiles; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 147 | }; |
| 148 | } |
| 149 | |
Vedant Kumar | 86b2ac63 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 150 | static std::string getErrorString(const Twine &Message, StringRef Whence, |
| 151 | bool Warning) { |
| 152 | std::string Str = (Warning ? "warning" : "error"); |
| 153 | Str += ": "; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 154 | if (!Whence.empty()) |
Vedant Kumar | b95dc46 | 2016-07-15 01:53:39 +0000 | [diff] [blame] | 155 | Str += Whence.str() + ": "; |
Vedant Kumar | 86b2ac63 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 156 | Str += Message.str() + "\n"; |
| 157 | return Str; |
| 158 | } |
| 159 | |
| 160 | void CodeCoverageTool::error(const Twine &Message, StringRef Whence) { |
Vedant Kumar | b302063 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 161 | std::unique_lock<std::mutex> Guard{ErrsLock}; |
| 162 | ViewOpts.colored_ostream(errs(), raw_ostream::RED) |
| 163 | << getErrorString(Message, Whence, false); |
Vedant Kumar | 86b2ac63 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Vedant Kumar | b302063 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 166 | void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) { |
| 167 | std::unique_lock<std::mutex> Guard{ErrsLock}; |
| 168 | ViewOpts.colored_ostream(errs(), raw_ostream::RED) |
| 169 | << getErrorString(Message, Whence, true); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Vedant Kumar | cef440f | 2016-06-28 16:12:18 +0000 | [diff] [blame] | 172 | void CodeCoverageTool::addCollectedPath(const std::string &Path) { |
Vedant Kumar | 1ce90d8 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 173 | if (CompareFilenamesOnly) { |
Vedant Kumar | bc64798 | 2016-09-23 18:57:32 +0000 | [diff] [blame] | 174 | SourceFiles.emplace_back(Path); |
Vedant Kumar | 1ce90d8 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 175 | } else { |
| 176 | SmallString<128> EffectivePath(Path); |
| 177 | if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) { |
| 178 | error(EC.message(), Path); |
| 179 | return; |
| 180 | } |
| 181 | sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true); |
Vedant Kumar | bc64798 | 2016-09-23 18:57:32 +0000 | [diff] [blame] | 182 | SourceFiles.emplace_back(EffectivePath.str()); |
Vedant Kumar | 1ce90d8 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 183 | } |
Vedant Kumar | cef440f | 2016-06-28 16:12:18 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Vedant Kumar | 1ce90d8 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 186 | void CodeCoverageTool::collectPaths(const std::string &Path) { |
| 187 | llvm::sys::fs::file_status Status; |
| 188 | llvm::sys::fs::status(Path, Status); |
| 189 | if (!llvm::sys::fs::exists(Status)) { |
| 190 | if (CompareFilenamesOnly) |
| 191 | addCollectedPath(Path); |
| 192 | else |
| 193 | error("Missing source file", Path); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | if (llvm::sys::fs::is_regular_file(Status)) { |
| 198 | addCollectedPath(Path); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | if (llvm::sys::fs::is_directory(Status)) { |
| 203 | std::error_code EC; |
| 204 | for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E; |
| 205 | F != E && !EC; F.increment(EC)) { |
| 206 | if (llvm::sys::fs::is_regular_file(F->path())) |
| 207 | addCollectedPath(F->path()); |
| 208 | } |
| 209 | if (EC) |
| 210 | warning(EC.message(), Path); |
| 211 | } |
| 212 | } |
| 213 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 214 | ErrorOr<const MemoryBuffer &> |
| 215 | CodeCoverageTool::getSourceFile(StringRef SourceFile) { |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 216 | // If we've remapped filenames, look up the real location for this file. |
Vedant Kumar | 615b85d | 2016-07-15 01:19:36 +0000 | [diff] [blame] | 217 | std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock}; |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 218 | if (!RemappedFilenames.empty()) { |
| 219 | auto Loc = RemappedFilenames.find(SourceFile); |
| 220 | if (Loc != RemappedFilenames.end()) |
| 221 | SourceFile = Loc->second; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 222 | } |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 223 | for (const auto &Files : LoadedSourceFiles) |
| 224 | if (sys::fs::equivalent(SourceFile, Files.first)) |
| 225 | return *Files.second; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 226 | auto Buffer = MemoryBuffer::getFile(SourceFile); |
| 227 | if (auto EC = Buffer.getError()) { |
Vedant Kumar | b302063 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 228 | error(EC.message(), SourceFile); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 229 | return EC; |
| 230 | } |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 231 | LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get())); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 232 | return *LoadedSourceFiles.back().second; |
| 233 | } |
| 234 | |
Vedant Kumar | f681e2e | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 235 | void CodeCoverageTool::attachExpansionSubViews( |
| 236 | SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions, |
| 237 | const CoverageMapping &Coverage) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 238 | if (!ViewOpts.ShowExpandedRegions) |
| 239 | return; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 240 | for (const auto &Expansion : Expansions) { |
| 241 | auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); |
| 242 | if (ExpansionCoverage.empty()) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 243 | continue; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 244 | auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename()); |
| 245 | if (!SourceBuffer) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 246 | continue; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 247 | |
| 248 | auto SubViewExpansions = ExpansionCoverage.getExpansions(); |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 249 | auto SubView = |
| 250 | SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(), |
| 251 | ViewOpts, std::move(ExpansionCoverage)); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 252 | attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); |
| 253 | View.addExpansion(Expansion.Region, std::move(SubView)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 257 | std::unique_ptr<SourceCoverageView> |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 258 | CodeCoverageTool::createFunctionView(const FunctionRecord &Function, |
Vedant Kumar | f681e2e | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 259 | const CoverageMapping &Coverage) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 260 | auto FunctionCoverage = Coverage.getCoverageForFunction(Function); |
| 261 | if (FunctionCoverage.empty()) |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 262 | return nullptr; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 263 | auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename()); |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 264 | if (!SourceBuffer) |
| 265 | return nullptr; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 266 | |
| 267 | auto Expansions = FunctionCoverage.getExpansions(); |
Vedant Kumar | 6e28bcd | 2017-02-05 20:10:58 +0000 | [diff] [blame^] | 268 | auto View = SourceCoverageView::create(DC.demangle(Function.Name), |
Vedant Kumar | 0053c0b | 2016-09-08 00:56:48 +0000 | [diff] [blame] | 269 | SourceBuffer.get(), ViewOpts, |
| 270 | std::move(FunctionCoverage)); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 271 | attachExpansionSubViews(*View, Expansions, Coverage); |
| 272 | |
| 273 | return View; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 276 | std::unique_ptr<SourceCoverageView> |
| 277 | CodeCoverageTool::createSourceFileView(StringRef SourceFile, |
Vedant Kumar | f681e2e | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 278 | const CoverageMapping &Coverage) { |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 279 | auto SourceBuffer = getSourceFile(SourceFile); |
| 280 | if (!SourceBuffer) |
| 281 | return nullptr; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 282 | auto FileCoverage = Coverage.getCoverageForFile(SourceFile); |
| 283 | if (FileCoverage.empty()) |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 284 | return nullptr; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 285 | |
| 286 | auto Expansions = FileCoverage.getExpansions(); |
Vedant Kumar | f9151b9 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 287 | auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(), |
| 288 | ViewOpts, std::move(FileCoverage)); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 289 | attachExpansionSubViews(*View, Expansions, Coverage); |
| 290 | |
Vedant Kumar | f681e2e | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 291 | for (const auto *Function : Coverage.getInstantiations(SourceFile)) { |
Vedant Kumar | a8c396d | 2016-09-15 06:44:51 +0000 | [diff] [blame] | 292 | std::unique_ptr<SourceCoverageView> SubView{nullptr}; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 293 | |
Vedant Kumar | 6e28bcd | 2017-02-05 20:10:58 +0000 | [diff] [blame^] | 294 | StringRef Funcname = DC.demangle(Function->Name); |
Vedant Kumar | e907977 | 2016-09-20 21:27:48 +0000 | [diff] [blame] | 295 | |
Vedant Kumar | a8c396d | 2016-09-15 06:44:51 +0000 | [diff] [blame] | 296 | if (Function->ExecutionCount > 0) { |
| 297 | auto SubViewCoverage = Coverage.getCoverageForFunction(*Function); |
| 298 | auto SubViewExpansions = SubViewCoverage.getExpansions(); |
| 299 | SubView = SourceCoverageView::create( |
Vedant Kumar | e907977 | 2016-09-20 21:27:48 +0000 | [diff] [blame] | 300 | Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage)); |
Vedant Kumar | a8c396d | 2016-09-15 06:44:51 +0000 | [diff] [blame] | 301 | attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 302 | } |
Vedant Kumar | a8c396d | 2016-09-15 06:44:51 +0000 | [diff] [blame] | 303 | |
| 304 | unsigned FileID = Function->CountedRegions.front().FileID; |
| 305 | unsigned Line = 0; |
| 306 | for (const auto &CR : Function->CountedRegions) |
| 307 | if (CR.FileID == FileID) |
| 308 | Line = std::max(CR.LineEnd, Line); |
Vedant Kumar | e907977 | 2016-09-20 21:27:48 +0000 | [diff] [blame] | 309 | View->addInstantiation(Funcname, Line, std::move(SubView)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 310 | } |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 311 | return View; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Justin Bogner | 65337d1 | 2015-05-04 04:09:38 +0000 | [diff] [blame] | 314 | static bool modifiedTimeGT(StringRef LHS, StringRef RHS) { |
| 315 | sys::fs::file_status Status; |
| 316 | if (sys::fs::status(LHS, Status)) |
| 317 | return false; |
| 318 | auto LHSTime = Status.getLastModificationTime(); |
| 319 | if (sys::fs::status(RHS, Status)) |
| 320 | return false; |
| 321 | auto RHSTime = Status.getLastModificationTime(); |
| 322 | return LHSTime > RHSTime; |
| 323 | } |
| 324 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 325 | std::unique_ptr<CoverageMapping> CodeCoverageTool::load() { |
Vedant Kumar | a3661ef | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 326 | for (StringRef ObjectFilename : ObjectFilenames) |
| 327 | if (modifiedTimeGT(ObjectFilename, PGOFilename)) |
| 328 | warning("profile data may be out of date - object is newer", |
| 329 | ObjectFilename); |
Vedant Kumar | b302063 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 330 | auto CoverageOrErr = |
Vedant Kumar | a3661ef | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 331 | CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArch); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 332 | if (Error E = CoverageOrErr.takeError()) { |
Vedant Kumar | a3661ef | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 333 | error("Failed to load coverage: " + toString(std::move(E)), |
| 334 | join(ObjectFilenames.begin(), ObjectFilenames.end(), ", ")); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 335 | return nullptr; |
| 336 | } |
| 337 | auto Coverage = std::move(CoverageOrErr.get()); |
| 338 | unsigned Mismatched = Coverage->getMismatchedCount(); |
Vedant Kumar | b302063 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 339 | if (Mismatched) |
| 340 | warning(utostr(Mismatched) + " functions have mismatched data"); |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 341 | |
Vedant Kumar | cab52ad | 2016-09-23 20:13:41 +0000 | [diff] [blame] | 342 | if (!SourceFiles.empty()) |
| 343 | removeUnmappedInputs(*Coverage); |
| 344 | |
| 345 | demangleSymbols(*Coverage); |
| 346 | |
| 347 | return Coverage; |
| 348 | } |
| 349 | |
| 350 | void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) { |
| 351 | std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles(); |
Vedant Kumar | 45880880 | 2016-09-23 18:57:35 +0000 | [diff] [blame] | 352 | |
| 353 | auto UncoveredFilesIt = SourceFiles.end(); |
| 354 | if (!CompareFilenamesOnly) { |
| 355 | // The user may have specified source files which aren't in the coverage |
| 356 | // mapping. Filter these files away. |
| 357 | UncoveredFilesIt = std::remove_if( |
| 358 | SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) { |
| 359 | return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(), |
| 360 | SF); |
| 361 | }); |
| 362 | } else { |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 363 | for (auto &SF : SourceFiles) { |
| 364 | StringRef SFBase = sys::path::filename(SF); |
Vedant Kumar | 45880880 | 2016-09-23 18:57:35 +0000 | [diff] [blame] | 365 | for (const auto &CF : CoveredFiles) { |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 366 | if (SFBase == sys::path::filename(CF)) { |
| 367 | RemappedFilenames[CF] = SF; |
| 368 | SF = CF; |
| 369 | break; |
| 370 | } |
Vedant Kumar | 45880880 | 2016-09-23 18:57:35 +0000 | [diff] [blame] | 371 | } |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 372 | } |
Vedant Kumar | 45880880 | 2016-09-23 18:57:35 +0000 | [diff] [blame] | 373 | UncoveredFilesIt = std::remove_if( |
| 374 | SourceFiles.begin(), SourceFiles.end(), |
| 375 | [&](const std::string &SF) { return !RemappedFilenames.count(SF); }); |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Vedant Kumar | 45880880 | 2016-09-23 18:57:35 +0000 | [diff] [blame] | 378 | SourceFiles.erase(UncoveredFilesIt, SourceFiles.end()); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 381 | void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) { |
| 382 | if (!ViewOpts.hasDemangler()) |
| 383 | return; |
| 384 | |
| 385 | // Pass function names to the demangler in a temporary file. |
| 386 | int InputFD; |
| 387 | SmallString<256> InputPath; |
| 388 | std::error_code EC = |
| 389 | sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath); |
| 390 | if (EC) { |
| 391 | error(InputPath, EC.message()); |
| 392 | return; |
| 393 | } |
| 394 | tool_output_file InputTOF{InputPath, InputFD}; |
| 395 | |
| 396 | unsigned NumSymbols = 0; |
| 397 | for (const auto &Function : Coverage.getCoveredFunctions()) { |
| 398 | InputTOF.os() << Function.Name << '\n'; |
| 399 | ++NumSymbols; |
| 400 | } |
Vedant Kumar | 554357b | 2016-07-15 23:08:22 +0000 | [diff] [blame] | 401 | InputTOF.os().close(); |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 402 | |
| 403 | // Use another temporary file to store the demangler's output. |
| 404 | int OutputFD; |
| 405 | SmallString<256> OutputPath; |
| 406 | EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD, |
| 407 | OutputPath); |
| 408 | if (EC) { |
| 409 | error(OutputPath, EC.message()); |
| 410 | return; |
| 411 | } |
| 412 | tool_output_file OutputTOF{OutputPath, OutputFD}; |
Vedant Kumar | 554357b | 2016-07-15 23:08:22 +0000 | [diff] [blame] | 413 | OutputTOF.os().close(); |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 414 | |
| 415 | // Invoke the demangler. |
| 416 | std::vector<const char *> ArgsV; |
| 417 | for (const std::string &Arg : ViewOpts.DemanglerOpts) |
| 418 | ArgsV.push_back(Arg.c_str()); |
| 419 | ArgsV.push_back(nullptr); |
Vedant Kumar | 38202c0 | 2016-07-15 23:15:35 +0000 | [diff] [blame] | 420 | StringRef InputPathRef = InputPath.str(); |
| 421 | StringRef OutputPathRef = OutputPath.str(); |
| 422 | StringRef StderrRef; |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 423 | const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef}; |
| 424 | std::string ErrMsg; |
| 425 | int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(), |
| 426 | /*env=*/nullptr, Redirects, /*secondsToWait=*/0, |
| 427 | /*memoryLimit=*/0, &ErrMsg); |
| 428 | if (RC) { |
| 429 | error(ErrMsg, ViewOpts.DemanglerOpts[0]); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | // Parse the demangler's output. |
| 434 | auto BufOrError = MemoryBuffer::getFile(OutputPath); |
| 435 | if (!BufOrError) { |
| 436 | error(OutputPath, BufOrError.getError().message()); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError); |
| 441 | |
| 442 | SmallVector<StringRef, 8> Symbols; |
| 443 | StringRef DemanglerData = DemanglerBuf->getBuffer(); |
| 444 | DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols, |
| 445 | /*KeepEmpty=*/false); |
| 446 | if (Symbols.size() != NumSymbols) { |
| 447 | error("Demangler did not provide expected number of symbols"); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | // Cache the demangled names. |
| 452 | unsigned I = 0; |
| 453 | for (const auto &Function : Coverage.getCoveredFunctions()) |
Vedant Kumar | 6e28bcd | 2017-02-05 20:10:58 +0000 | [diff] [blame^] | 454 | DC.DemangledNames[Function.Name] = Symbols[I++]; |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Vedant Kumar | 6fd94bf | 2016-10-19 17:55:44 +0000 | [diff] [blame] | 457 | void CodeCoverageTool::writeSourceFileView(StringRef SourceFile, |
| 458 | CoverageMapping *Coverage, |
| 459 | CoveragePrinter *Printer, |
| 460 | bool ShowFilenames) { |
| 461 | auto View = createSourceFileView(SourceFile, *Coverage); |
| 462 | if (!View) { |
| 463 | warning("The file '" + SourceFile + "' isn't covered."); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false); |
| 468 | if (Error E = OSOrErr.takeError()) { |
| 469 | error("Could not create view file!", toString(std::move(E))); |
| 470 | return; |
| 471 | } |
| 472 | auto OS = std::move(OSOrErr.get()); |
| 473 | |
| 474 | View->print(*OS.get(), /*Wholefile=*/true, |
| 475 | /*ShowSourceName=*/ShowFilenames); |
| 476 | Printer->closeViewFile(std::move(OS)); |
| 477 | } |
| 478 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 479 | int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { |
Vedant Kumar | a3661ef | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 480 | cl::opt<std::string> CovFilename( |
| 481 | cl::Positional, cl::desc("Covered executable or object file.")); |
| 482 | |
| 483 | cl::list<std::string> CovFilenames( |
| 484 | "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore, |
| 485 | cl::CommaSeparated); |
Justin Bogner | f6c5055 | 2014-10-30 20:51:24 +0000 | [diff] [blame] | 486 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 487 | cl::list<std::string> InputSourceFiles( |
| 488 | cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore); |
| 489 | |
Vedant Kumar | 1ce90d8 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 490 | cl::opt<bool> DebugDumpCollectedPaths( |
| 491 | "dump-collected-paths", cl::Optional, cl::Hidden, |
| 492 | cl::desc("Show the collected paths to source files")); |
| 493 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 494 | cl::opt<std::string, true> PGOFilename( |
| 495 | "instr-profile", cl::Required, cl::location(this->PGOFilename), |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 496 | cl::desc( |
| 497 | "File with the profile data obtained after an instrumented run")); |
| 498 | |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 499 | cl::opt<std::string> Arch( |
| 500 | "arch", cl::desc("architecture of the coverage mapping binary")); |
| 501 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 502 | cl::opt<bool> DebugDump("dump", cl::Optional, |
| 503 | cl::desc("Show internal debug dump")); |
| 504 | |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 505 | cl::opt<CoverageViewOptions::OutputFormat> Format( |
| 506 | "format", cl::desc("Output format for line-based coverage reports"), |
| 507 | cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text", |
| 508 | "Text output"), |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 509 | clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 510 | "HTML output")), |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 511 | cl::init(CoverageViewOptions::OutputFormat::Text)); |
| 512 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 513 | cl::opt<bool> FilenameEquivalence( |
| 514 | "filename-equivalence", cl::Optional, |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 515 | cl::desc("Treat source files as equivalent to paths in the coverage data " |
| 516 | "when the file names match, even if the full paths do not")); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 517 | |
| 518 | cl::OptionCategory FilteringCategory("Function filtering options"); |
| 519 | |
| 520 | cl::list<std::string> NameFilters( |
| 521 | "name", cl::Optional, |
| 522 | cl::desc("Show code coverage only for functions with the given name"), |
| 523 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 524 | |
| 525 | cl::list<std::string> NameRegexFilters( |
| 526 | "name-regex", cl::Optional, |
| 527 | cl::desc("Show code coverage only for functions that match the given " |
| 528 | "regular expression"), |
| 529 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 530 | |
| 531 | cl::opt<double> RegionCoverageLtFilter( |
| 532 | "region-coverage-lt", cl::Optional, |
| 533 | cl::desc("Show code coverage only for functions with region coverage " |
| 534 | "less than the given threshold"), |
| 535 | cl::cat(FilteringCategory)); |
| 536 | |
| 537 | cl::opt<double> RegionCoverageGtFilter( |
| 538 | "region-coverage-gt", cl::Optional, |
| 539 | cl::desc("Show code coverage only for functions with region coverage " |
| 540 | "greater than the given threshold"), |
| 541 | cl::cat(FilteringCategory)); |
| 542 | |
| 543 | cl::opt<double> LineCoverageLtFilter( |
| 544 | "line-coverage-lt", cl::Optional, |
| 545 | cl::desc("Show code coverage only for functions with line coverage less " |
| 546 | "than the given threshold"), |
| 547 | cl::cat(FilteringCategory)); |
| 548 | |
| 549 | cl::opt<double> LineCoverageGtFilter( |
| 550 | "line-coverage-gt", cl::Optional, |
| 551 | cl::desc("Show code coverage only for functions with line coverage " |
| 552 | "greater than the given threshold"), |
| 553 | cl::cat(FilteringCategory)); |
| 554 | |
Justin Bogner | 9deb1d4 | 2015-03-19 04:45:16 +0000 | [diff] [blame] | 555 | cl::opt<cl::boolOrDefault> UseColor( |
| 556 | "use-color", cl::desc("Emit colored output (default=autodetect)"), |
| 557 | cl::init(cl::BOU_UNSET)); |
Justin Bogner | cfb53e4 | 2015-03-19 00:02:23 +0000 | [diff] [blame] | 558 | |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 559 | cl::list<std::string> DemanglerOpts( |
| 560 | "Xdemangler", cl::desc("<demangler-path>|<demangler-option>")); |
| 561 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 562 | auto commandLineParser = [&, this](int argc, const char **argv) -> int { |
| 563 | cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); |
| 564 | ViewOpts.Debug = DebugDump; |
| 565 | CompareFilenamesOnly = FilenameEquivalence; |
| 566 | |
Vedant Kumar | a3661ef | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 567 | if (!CovFilename.empty()) |
| 568 | ObjectFilenames.emplace_back(CovFilename); |
| 569 | for (const std::string &Filename : CovFilenames) |
| 570 | ObjectFilenames.emplace_back(Filename); |
| 571 | if (ObjectFilenames.empty()) { |
Vedant Kumar | 22c1b7c | 2016-10-25 19:52:57 +0000 | [diff] [blame] | 572 | errs() << "No filenames specified!\n"; |
Vedant Kumar | a3661ef | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 573 | ::exit(1); |
| 574 | } |
| 575 | |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 576 | ViewOpts.Format = Format; |
| 577 | switch (ViewOpts.Format) { |
| 578 | case CoverageViewOptions::OutputFormat::Text: |
| 579 | ViewOpts.Colors = UseColor == cl::BOU_UNSET |
| 580 | ? sys::Process::StandardOutHasColors() |
| 581 | : UseColor == cl::BOU_TRUE; |
| 582 | break; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 583 | case CoverageViewOptions::OutputFormat::HTML: |
| 584 | if (UseColor == cl::BOU_FALSE) |
Vedant Kumar | 22c1b7c | 2016-10-25 19:52:57 +0000 | [diff] [blame] | 585 | errs() << "Color output cannot be disabled when generating html.\n"; |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 586 | ViewOpts.Colors = true; |
| 587 | break; |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 588 | } |
Justin Bogner | cfb53e4 | 2015-03-19 00:02:23 +0000 | [diff] [blame] | 589 | |
Vedant Kumar | 424f51b | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 590 | // If a demangler is supplied, check if it exists and register it. |
| 591 | if (DemanglerOpts.size()) { |
| 592 | auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]); |
| 593 | if (!DemanglerPathOrErr) { |
| 594 | error("Could not find the demangler!", |
| 595 | DemanglerPathOrErr.getError().message()); |
| 596 | return 1; |
| 597 | } |
| 598 | DemanglerOpts[0] = *DemanglerPathOrErr; |
| 599 | ViewOpts.DemanglerOpts.swap(DemanglerOpts); |
| 600 | } |
| 601 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 602 | // Create the function filters |
| 603 | if (!NameFilters.empty() || !NameRegexFilters.empty()) { |
| 604 | auto NameFilterer = new CoverageFilters; |
| 605 | for (const auto &Name : NameFilters) |
| 606 | NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name)); |
| 607 | for (const auto &Regex : NameRegexFilters) |
| 608 | NameFilterer->push_back( |
| 609 | llvm::make_unique<NameRegexCoverageFilter>(Regex)); |
| 610 | Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer)); |
| 611 | } |
| 612 | if (RegionCoverageLtFilter.getNumOccurrences() || |
| 613 | RegionCoverageGtFilter.getNumOccurrences() || |
| 614 | LineCoverageLtFilter.getNumOccurrences() || |
| 615 | LineCoverageGtFilter.getNumOccurrences()) { |
| 616 | auto StatFilterer = new CoverageFilters; |
| 617 | if (RegionCoverageLtFilter.getNumOccurrences()) |
| 618 | StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>( |
| 619 | RegionCoverageFilter::LessThan, RegionCoverageLtFilter)); |
| 620 | if (RegionCoverageGtFilter.getNumOccurrences()) |
| 621 | StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>( |
| 622 | RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter)); |
| 623 | if (LineCoverageLtFilter.getNumOccurrences()) |
| 624 | StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>( |
| 625 | LineCoverageFilter::LessThan, LineCoverageLtFilter)); |
| 626 | if (LineCoverageGtFilter.getNumOccurrences()) |
| 627 | StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>( |
| 628 | RegionCoverageFilter::GreaterThan, LineCoverageGtFilter)); |
| 629 | Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer)); |
| 630 | } |
| 631 | |
Frederic Riss | ebc162a | 2015-06-22 21:33:24 +0000 | [diff] [blame] | 632 | if (!Arch.empty() && |
| 633 | Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) { |
Vedant Kumar | b302063 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 634 | error("Unknown architecture: " + Arch); |
Frederic Riss | ebc162a | 2015-06-22 21:33:24 +0000 | [diff] [blame] | 635 | return 1; |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 636 | } |
Frederic Riss | ebc162a | 2015-06-22 21:33:24 +0000 | [diff] [blame] | 637 | CoverageArch = Arch; |
Justin Bogner | 4379535 | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 638 | |
Vedant Kumar | 1ce90d8 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 639 | for (const std::string &File : InputSourceFiles) |
| 640 | collectPaths(File); |
| 641 | |
| 642 | if (DebugDumpCollectedPaths) { |
Vedant Kumar | bc64798 | 2016-09-23 18:57:32 +0000 | [diff] [blame] | 643 | for (const std::string &SF : SourceFiles) |
Vedant Kumar | 1ce90d8 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 644 | outs() << SF << '\n'; |
| 645 | ::exit(0); |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 646 | } |
Vedant Kumar | 1ce90d8 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 647 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 648 | return 0; |
| 649 | }; |
| 650 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 651 | switch (Cmd) { |
| 652 | case Show: |
| 653 | return show(argc, argv, commandLineParser); |
| 654 | case Report: |
| 655 | return report(argc, argv, commandLineParser); |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 656 | case Export: |
| 657 | return export_(argc, argv, commandLineParser); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 658 | } |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | int CodeCoverageTool::show(int argc, const char **argv, |
| 663 | CommandLineParserType commandLineParser) { |
| 664 | |
| 665 | cl::OptionCategory ViewCategory("Viewing options"); |
| 666 | |
| 667 | cl::opt<bool> ShowLineExecutionCounts( |
| 668 | "show-line-counts", cl::Optional, |
| 669 | cl::desc("Show the execution counts for each line"), cl::init(true), |
| 670 | cl::cat(ViewCategory)); |
| 671 | |
| 672 | cl::opt<bool> ShowRegions( |
| 673 | "show-regions", cl::Optional, |
| 674 | cl::desc("Show the execution counts for each region"), |
| 675 | cl::cat(ViewCategory)); |
| 676 | |
| 677 | cl::opt<bool> ShowBestLineRegionsCounts( |
| 678 | "show-line-counts-or-regions", cl::Optional, |
| 679 | cl::desc("Show the execution counts for each line, or the execution " |
| 680 | "counts for each region on lines that have multiple regions"), |
| 681 | cl::cat(ViewCategory)); |
| 682 | |
| 683 | cl::opt<bool> ShowExpansions("show-expansions", cl::Optional, |
| 684 | cl::desc("Show expanded source regions"), |
| 685 | cl::cat(ViewCategory)); |
| 686 | |
| 687 | cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional, |
| 688 | cl::desc("Show function instantiations"), |
| 689 | cl::cat(ViewCategory)); |
| 690 | |
Vedant Kumar | 7937ef3 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 691 | cl::opt<std::string> ShowOutputDirectory( |
| 692 | "output-dir", cl::init(""), |
| 693 | cl::desc("Directory in which coverage information is written out")); |
| 694 | cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"), |
| 695 | cl::aliasopt(ShowOutputDirectory)); |
| 696 | |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 697 | cl::opt<uint32_t> TabSize( |
Vedant Kumar | ad547d3 | 2016-08-04 18:00:42 +0000 | [diff] [blame] | 698 | "tab-size", cl::init(2), |
| 699 | cl::desc( |
| 700 | "Set tab expansion size for html coverage reports (default = 2)")); |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 701 | |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 702 | cl::opt<std::string> ProjectTitle( |
| 703 | "project-title", cl::Optional, |
| 704 | cl::desc("Set project title for the coverage report")); |
| 705 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 706 | auto Err = commandLineParser(argc, argv); |
| 707 | if (Err) |
| 708 | return Err; |
| 709 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 710 | ViewOpts.ShowLineNumbers = true; |
| 711 | ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 || |
| 712 | !ShowRegions || ShowBestLineRegionsCounts; |
| 713 | ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts; |
| 714 | ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts; |
| 715 | ViewOpts.ShowExpandedRegions = ShowExpansions; |
| 716 | ViewOpts.ShowFunctionInstantiations = ShowInstantiations; |
Vedant Kumar | 7937ef3 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 717 | ViewOpts.ShowOutputDirectory = ShowOutputDirectory; |
Ying Yi | 0ef31b7 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 718 | ViewOpts.TabSize = TabSize; |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 719 | ViewOpts.ProjectTitle = ProjectTitle; |
Vedant Kumar | 7937ef3 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 720 | |
Vedant Kumar | 64d8a02 | 2016-06-28 16:12:20 +0000 | [diff] [blame] | 721 | if (ViewOpts.hasOutputDirectory()) { |
Vedant Kumar | 7937ef3 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 722 | if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) { |
| 723 | error("Could not create output directory!", E.message()); |
| 724 | return 1; |
| 725 | } |
| 726 | } |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 727 | |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 728 | sys::fs::file_status Status; |
| 729 | if (sys::fs::status(PGOFilename, Status)) { |
| 730 | error("profdata file error: can not get the file status. \n"); |
| 731 | return 1; |
| 732 | } |
| 733 | |
| 734 | auto ModifiedTime = Status.getLastModificationTime(); |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 735 | std::string ModifiedTimeStr = to_string(ModifiedTime); |
Benjamin Kramer | e6ba5ef | 2016-11-30 10:01:11 +0000 | [diff] [blame] | 736 | size_t found = ModifiedTimeStr.rfind(':'); |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 737 | ViewOpts.CreatedTimeStr = (found != std::string::npos) |
| 738 | ? "Created: " + ModifiedTimeStr.substr(0, found) |
| 739 | : "Created: " + ModifiedTimeStr; |
| 740 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 741 | auto Coverage = load(); |
| 742 | if (!Coverage) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 743 | return 1; |
| 744 | |
Vedant Kumar | 9cbad2c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 745 | auto Printer = CoveragePrinter::create(ViewOpts); |
| 746 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 747 | if (!Filters.empty()) { |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 748 | auto OSOrErr = Printer->createViewFile("functions", /*InToplevel=*/true); |
| 749 | if (Error E = OSOrErr.takeError()) { |
Vedant Kumar | b95dc46 | 2016-07-15 01:53:39 +0000 | [diff] [blame] | 750 | error("Could not create view file!", toString(std::move(E))); |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 751 | return 1; |
| 752 | } |
| 753 | auto OS = std::move(OSOrErr.get()); |
| 754 | |
| 755 | // Show functions. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 756 | for (const auto &Function : Coverage->getCoveredFunctions()) { |
| 757 | if (!Filters.matches(Function)) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 758 | continue; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 759 | |
| 760 | auto mainView = createFunctionView(Function, *Coverage); |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 761 | if (!mainView) { |
Vedant Kumar | b302063 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 762 | warning("Could not read coverage for '" + Function.Name + "'."); |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 763 | continue; |
| 764 | } |
Vedant Kumar | 7937ef3 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 765 | |
Vedant Kumar | 7937ef3 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 766 | mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 767 | } |
Vedant Kumar | 8d74cb2 | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 768 | |
| 769 | Printer->closeViewFile(std::move(OS)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | // Show files |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 774 | bool ShowFilenames = |
Ying Yi | 24e91bd | 2016-09-06 21:41:38 +0000 | [diff] [blame] | 775 | (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() || |
Ying Yi | 84dc971 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 776 | (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 777 | |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 778 | if (SourceFiles.empty()) |
Vedant Kumar | 64d8a02 | 2016-06-28 16:12:20 +0000 | [diff] [blame] | 779 | // Get the source files from the function coverage mapping. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 780 | for (StringRef Filename : Coverage->getUniqueSourceFiles()) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 781 | SourceFiles.push_back(Filename); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 782 | |
Vedant Kumar | 9cbad2c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 783 | // Create an index out of the source files. |
| 784 | if (ViewOpts.hasOutputDirectory()) { |
Vedant Kumar | a59334d | 2016-09-09 01:32:55 +0000 | [diff] [blame] | 785 | if (Error E = Printer->createIndexFile(SourceFiles, *Coverage)) { |
Vedant Kumar | b95dc46 | 2016-07-15 01:53:39 +0000 | [diff] [blame] | 786 | error("Could not create index file!", toString(std::move(E))); |
Vedant Kumar | 9cbad2c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 787 | return 1; |
| 788 | } |
| 789 | } |
| 790 | |
Vedant Kumar | 6fd94bf | 2016-10-19 17:55:44 +0000 | [diff] [blame] | 791 | // FIXME: Sink the hardware_concurrency() == 1 check into ThreadPool. |
| 792 | if (!ViewOpts.hasOutputDirectory() || |
| 793 | std::thread::hardware_concurrency() == 1) { |
| 794 | for (const std::string &SourceFile : SourceFiles) |
| 795 | writeSourceFileView(SourceFile, Coverage.get(), Printer.get(), |
| 796 | ShowFilenames); |
| 797 | } else { |
| 798 | // In -output-dir mode, it's safe to use multiple threads to print files. |
| 799 | ThreadPool Pool; |
| 800 | for (const std::string &SourceFile : SourceFiles) |
| 801 | Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile, |
| 802 | Coverage.get(), Printer.get(), ShowFilenames); |
| 803 | Pool.wait(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | int CodeCoverageTool::report(int argc, const char **argv, |
| 810 | CommandLineParserType commandLineParser) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 811 | auto Err = commandLineParser(argc, argv); |
| 812 | if (Err) |
| 813 | return Err; |
| 814 | |
Vedant Kumar | 4c01092 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 815 | if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) |
| 816 | error("HTML output for summary reports is not yet supported."); |
| 817 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 818 | auto Coverage = load(); |
| 819 | if (!Coverage) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 820 | return 1; |
| 821 | |
Vedant Kumar | 702bb9d | 2016-09-06 22:45:57 +0000 | [diff] [blame] | 822 | CoverageReport Report(ViewOpts, *Coverage.get()); |
Justin Bogner | 0ef7a2a | 2015-02-14 02:05:05 +0000 | [diff] [blame] | 823 | if (SourceFiles.empty()) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 824 | Report.renderFileReports(llvm::outs()); |
Justin Bogner | 0ef7a2a | 2015-02-14 02:05:05 +0000 | [diff] [blame] | 825 | else |
| 826 | Report.renderFunctionReports(SourceFiles, llvm::outs()); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 830 | int CodeCoverageTool::export_(int argc, const char **argv, |
| 831 | CommandLineParserType commandLineParser) { |
| 832 | |
| 833 | auto Err = commandLineParser(argc, argv); |
| 834 | if (Err) |
| 835 | return Err; |
| 836 | |
| 837 | auto Coverage = load(); |
| 838 | if (!Coverage) { |
| 839 | error("Could not load coverage information"); |
| 840 | return 1; |
| 841 | } |
| 842 | |
Vedant Kumar | 5c61c70 | 2016-10-25 00:08:33 +0000 | [diff] [blame] | 843 | exportCoverageDataToJson(*Coverage.get(), outs()); |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 848 | int showMain(int argc, const char *argv[]) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 849 | CodeCoverageTool Tool; |
| 850 | return Tool.run(CodeCoverageTool::Show, argc, argv); |
| 851 | } |
| 852 | |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 853 | int reportMain(int argc, const char *argv[]) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 854 | CodeCoverageTool Tool; |
| 855 | return Tool.run(CodeCoverageTool::Report, argc, argv); |
| 856 | } |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 857 | |
| 858 | int exportMain(int argc, const char *argv[]) { |
| 859 | CodeCoverageTool Tool; |
| 860 | return Tool.run(CodeCoverageTool::Export, argc, argv); |
| 861 | } |