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