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