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