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