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