Alex Lorenz | 6c7a6a1 | 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 | |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 16 | #include "CoverageExporterJson.h" |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 17 | #include "CoverageExporterLcov.h" |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 18 | #include "CoverageFilters.h" |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 19 | #include "CoverageReport.h" |
Vedant Kumar | 4858447 | 2017-02-05 20:10:58 +0000 | [diff] [blame] | 20 | #include "CoverageSummaryInfo.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 21 | #include "CoverageViewOptions.h" |
Easwaran Raman | a96d5370 | 2016-04-29 18:53:05 +0000 | [diff] [blame] | 22 | #include "RenderingSupport.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 23 | #include "SourceCoverageView.h" |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringRef.h" |
Justin Bogner | d39109d | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Triple.h" |
Easwaran Raman | a96d5370 | 2016-04-29 18:53:05 +0000 | [diff] [blame] | 27 | #include "llvm/ProfileData/Coverage/CoverageMapping.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 28 | #include "llvm/ProfileData/InstrProfReader.h" |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
| 30 | #include "llvm/Support/FileSystem.h" |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Format.h" |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Path.h" |
Justin Bogner | cc690d6 | 2015-03-19 00:02:23 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Process.h" |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Program.h" |
Pavel Labath | 2864c2a | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ScopedPrinter.h" |
Vedant Kumar | 9240c45 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ThreadPool.h" |
Max Moroz | d6352ca | 2018-01-05 16:15:07 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Threading.h" |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ToolOutputFile.h" |
Sean Eveson | 078c2c3 | 2017-09-28 10:07:30 +0000 | [diff] [blame] | 40 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 41 | #include <functional> |
Sean Eveson | 078c2c3 | 2017-09-28 10:07:30 +0000 | [diff] [blame] | 42 | #include <map> |
Justin Bogner | 39d5e80 | 2014-09-09 05:32:18 +0000 | [diff] [blame] | 43 | #include <system_error> |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 44 | |
| 45 | using namespace llvm; |
| 46 | using namespace coverage; |
| 47 | |
Vedant Kumar | 5b5c5e7 | 2016-10-25 00:08:33 +0000 | [diff] [blame] | 48 | void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping, |
Vedant Kumar | 31b24fc | 2017-09-08 18:44:49 +0000 | [diff] [blame] | 49 | const CoverageViewOptions &Options, |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 50 | raw_ostream &OS); |
| 51 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 52 | namespace { |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 53 | /// The implementation of the coverage tool. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 54 | class CodeCoverageTool { |
| 55 | public: |
| 56 | enum Command { |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 57 | /// The show command. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 58 | Show, |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 59 | /// The report command. |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 60 | Report, |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 61 | /// The export command. |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 62 | Export |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Vedant Kumar | ae0e243 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 65 | int run(Command Cmd, int argc, const char **argv); |
| 66 | |
| 67 | private: |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 68 | /// Print the error message to the error output stream. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 69 | void error(const Twine &Message, StringRef Whence = ""); |
| 70 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 71 | /// Print the warning message to the error output stream. |
Vedant Kumar | a3ff212 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 72 | void warning(const Twine &Message, StringRef Whence = ""); |
Vedant Kumar | 9240c45 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 73 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 74 | /// Convert \p Path into an absolute path and append it to the list |
Vedant Kumar | fd06635 | 2016-09-23 18:57:32 +0000 | [diff] [blame] | 75 | /// of collected paths. |
Vedant Kumar | 75f03af | 2016-06-28 16:12:18 +0000 | [diff] [blame] | 76 | void addCollectedPath(const std::string &Path); |
| 77 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 78 | /// If \p Path is a regular file, collect the path. If it's a |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 79 | /// directory, recursively collect all of the paths within the directory. |
| 80 | void collectPaths(const std::string &Path); |
| 81 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 82 | /// Return a memory buffer for the given source file. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 83 | ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile); |
| 84 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 85 | /// Create source views for the expansions of the view. |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 86 | void attachExpansionSubViews(SourceCoverageView &View, |
| 87 | ArrayRef<ExpansionRecord> Expansions, |
Vedant Kumar | 7b18a76 | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 88 | const CoverageMapping &Coverage); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 89 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 90 | /// Create the source view of a particular function. |
Justin Bogner | 297149b | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 91 | std::unique_ptr<SourceCoverageView> |
Vedant Kumar | 7b18a76 | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 92 | createFunctionView(const FunctionRecord &Function, |
| 93 | const CoverageMapping &Coverage); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 94 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 95 | /// Create the main source view of a particular source file. |
Justin Bogner | 297149b | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 96 | std::unique_ptr<SourceCoverageView> |
Vedant Kumar | 7b18a76 | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 97 | createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 98 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 99 | /// Load the coverage mapping data. Return nullptr if an error occurred. |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 100 | std::unique_ptr<CoverageMapping> load(); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 101 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 102 | /// Create a mapping from files in the Coverage data to local copies |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 103 | /// (path-equivalence). |
| 104 | void remapPathNames(const CoverageMapping &Coverage); |
| 105 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 106 | /// Remove input source files which aren't mapped by \p Coverage. |
Vedant Kumar | 3de4fb3 | 2016-09-23 20:13:41 +0000 | [diff] [blame] | 107 | void removeUnmappedInputs(const CoverageMapping &Coverage); |
| 108 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 109 | /// If a demangler is available, demangle all symbol names. |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 110 | void demangleSymbols(const CoverageMapping &Coverage); |
| 111 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 112 | /// Write out a source file view to the filesystem. |
Vedant Kumar | 4c50ebb | 2016-10-19 17:55:44 +0000 | [diff] [blame] | 113 | void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage, |
| 114 | CoveragePrinter *Printer, bool ShowFilenames); |
| 115 | |
Benjamin Kramer | 36538ff | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 116 | typedef llvm::function_ref<int(int, const char **)> CommandLineParserType; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 117 | |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 118 | int doShow(int argc, const char **argv, |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 119 | CommandLineParserType commandLineParser); |
| 120 | |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 121 | int doReport(int argc, const char **argv, |
| 122 | CommandLineParserType commandLineParser); |
| 123 | |
| 124 | int doExport(int argc, const char **argv, |
| 125 | CommandLineParserType commandLineParser); |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 126 | |
Vedant Kumar | d4998ec | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 127 | std::vector<StringRef> ObjectFilenames; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 128 | CoverageViewOptions ViewOpts; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 129 | CoverageFiltersMatchAll Filters; |
Max Moroz | 06fa683 | 2018-04-09 15:20:35 +0000 | [diff] [blame] | 130 | CoverageFilters IgnoreFilenameFilters; |
Vedant Kumar | ae0e243 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 131 | |
| 132 | /// The path to the indexed profile. |
| 133 | std::string PGOFilename; |
| 134 | |
| 135 | /// A list of input source files. |
Vedant Kumar | fd06635 | 2016-09-23 18:57:32 +0000 | [diff] [blame] | 136 | std::vector<std::string> SourceFiles; |
Vedant Kumar | ae0e243 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 137 | |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 138 | /// In -path-equivalence mode, this maps the absolute paths from the coverage |
| 139 | /// mapping data to the input source files. |
Justin Bogner | ddbcfa0 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 140 | StringMap<std::string> RemappedFilenames; |
Vedant Kumar | ae0e243 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 141 | |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 142 | /// The coverage data path to be remapped from, and the source path to be |
| 143 | /// remapped to, when using -path-equivalence. |
| 144 | Optional<std::pair<std::string, std::string>> PathRemapping; |
| 145 | |
Vedant Kumar | ae0e243 | 2016-09-22 21:49:47 +0000 | [diff] [blame] | 146 | /// The architecture the coverage mapping data targets. |
Vedant Kumar | e72b881 | 2017-08-01 21:23:26 +0000 | [diff] [blame] | 147 | std::vector<StringRef> CoverageArches; |
Vedant Kumar | 75f03af | 2016-06-28 16:12:18 +0000 | [diff] [blame] | 148 | |
Vedant Kumar | 4858447 | 2017-02-05 20:10:58 +0000 | [diff] [blame] | 149 | /// A cache for demangled symbols. |
| 150 | DemangleCache DC; |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 151 | |
Vedant Kumar | 95f9742 | 2017-02-05 20:10:55 +0000 | [diff] [blame] | 152 | /// A lock which guards printing to stderr. |
Vedant Kumar | a3ff212 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 153 | std::mutex ErrsLock; |
Vedant Kumar | 9240c45 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 154 | |
Vedant Kumar | a5ec41d | 2016-07-15 22:44:54 +0000 | [diff] [blame] | 155 | /// A container for input source file buffers. |
Vedant Kumar | 9240c45 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 156 | std::mutex LoadedSourceFilesLock; |
| 157 | std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>> |
| 158 | LoadedSourceFiles; |
Sean Eveson | 10edef1 | 2017-08-31 09:11:31 +0000 | [diff] [blame] | 159 | |
| 160 | /// Whitelist from -name-whitelist to be used for filtering. |
| 161 | std::unique_ptr<SpecialCaseList> NameWhitelist; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 162 | }; |
| 163 | } |
| 164 | |
Vedant Kumar | 9240c45 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 165 | static std::string getErrorString(const Twine &Message, StringRef Whence, |
| 166 | bool Warning) { |
| 167 | std::string Str = (Warning ? "warning" : "error"); |
| 168 | Str += ": "; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 169 | if (!Whence.empty()) |
Vedant Kumar | b972a0f | 2016-07-15 01:53:39 +0000 | [diff] [blame] | 170 | Str += Whence.str() + ": "; |
Vedant Kumar | 9240c45 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 171 | Str += Message.str() + "\n"; |
| 172 | return Str; |
| 173 | } |
| 174 | |
| 175 | void CodeCoverageTool::error(const Twine &Message, StringRef Whence) { |
Vedant Kumar | a3ff212 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 176 | std::unique_lock<std::mutex> Guard{ErrsLock}; |
| 177 | ViewOpts.colored_ostream(errs(), raw_ostream::RED) |
| 178 | << getErrorString(Message, Whence, false); |
Vedant Kumar | 9240c45 | 2016-07-13 21:38:36 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Vedant Kumar | a3ff212 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 181 | void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) { |
| 182 | std::unique_lock<std::mutex> Guard{ErrsLock}; |
| 183 | ViewOpts.colored_ostream(errs(), raw_ostream::RED) |
| 184 | << getErrorString(Message, Whence, true); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Vedant Kumar | 75f03af | 2016-06-28 16:12:18 +0000 | [diff] [blame] | 187 | void CodeCoverageTool::addCollectedPath(const std::string &Path) { |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 188 | SmallString<128> EffectivePath(Path); |
| 189 | if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) { |
| 190 | error(EC.message(), Path); |
| 191 | return; |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 192 | } |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 193 | sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true); |
Max Moroz | 06fa683 | 2018-04-09 15:20:35 +0000 | [diff] [blame] | 194 | if (!IgnoreFilenameFilters.matchesFilename(EffectivePath)) |
| 195 | SourceFiles.emplace_back(EffectivePath.str()); |
Vedant Kumar | 75f03af | 2016-06-28 16:12:18 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 198 | void CodeCoverageTool::collectPaths(const std::string &Path) { |
| 199 | llvm::sys::fs::file_status Status; |
| 200 | llvm::sys::fs::status(Path, Status); |
| 201 | if (!llvm::sys::fs::exists(Status)) { |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 202 | if (PathRemapping) |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 203 | addCollectedPath(Path); |
| 204 | else |
Max Moroz | 2cb4aed | 2018-04-05 19:43:24 +0000 | [diff] [blame] | 205 | warning("Source file doesn't exist, proceeded by ignoring it.", Path); |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
| 209 | if (llvm::sys::fs::is_regular_file(Status)) { |
| 210 | addCollectedPath(Path); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | if (llvm::sys::fs::is_directory(Status)) { |
| 215 | std::error_code EC; |
| 216 | for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E; |
Max Moroz | 2cb4aed | 2018-04-05 19:43:24 +0000 | [diff] [blame] | 217 | F != E; F.increment(EC)) { |
| 218 | |
Sam McCall | a46ae18 | 2018-10-01 12:17:05 +0000 | [diff] [blame] | 219 | auto Status = F->status(); |
| 220 | if (!Status) { |
| 221 | warning(Status.getError().message(), F->path()); |
Max Moroz | 2cb4aed | 2018-04-05 19:43:24 +0000 | [diff] [blame] | 222 | continue; |
| 223 | } |
| 224 | |
Sam McCall | a46ae18 | 2018-10-01 12:17:05 +0000 | [diff] [blame] | 225 | if (Status->type() == llvm::sys::fs::file_type::regular_file) |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 226 | addCollectedPath(F->path()); |
| 227 | } |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 231 | ErrorOr<const MemoryBuffer &> |
| 232 | CodeCoverageTool::getSourceFile(StringRef SourceFile) { |
Justin Bogner | ddbcfa0 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 233 | // If we've remapped filenames, look up the real location for this file. |
Vedant Kumar | 343b271 | 2016-07-15 01:19:36 +0000 | [diff] [blame] | 234 | std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock}; |
Justin Bogner | ddbcfa0 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 235 | if (!RemappedFilenames.empty()) { |
| 236 | auto Loc = RemappedFilenames.find(SourceFile); |
| 237 | if (Loc != RemappedFilenames.end()) |
| 238 | SourceFile = Loc->second; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 239 | } |
Justin Bogner | ddbcfa0 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 240 | for (const auto &Files : LoadedSourceFiles) |
| 241 | if (sys::fs::equivalent(SourceFile, Files.first)) |
| 242 | return *Files.second; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 243 | auto Buffer = MemoryBuffer::getFile(SourceFile); |
| 244 | if (auto EC = Buffer.getError()) { |
Vedant Kumar | a3ff212 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 245 | error(EC.message(), SourceFile); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 246 | return EC; |
| 247 | } |
Benjamin Kramer | 9589ff8 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 248 | LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get())); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 249 | return *LoadedSourceFiles.back().second; |
| 250 | } |
| 251 | |
Vedant Kumar | 7b18a76 | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 252 | void CodeCoverageTool::attachExpansionSubViews( |
| 253 | SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions, |
| 254 | const CoverageMapping &Coverage) { |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 255 | if (!ViewOpts.ShowExpandedRegions) |
| 256 | return; |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 257 | for (const auto &Expansion : Expansions) { |
| 258 | auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); |
| 259 | if (ExpansionCoverage.empty()) |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 260 | continue; |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 261 | auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename()); |
| 262 | if (!SourceBuffer) |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 263 | continue; |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 264 | |
| 265 | auto SubViewExpansions = ExpansionCoverage.getExpansions(); |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 266 | auto SubView = |
| 267 | SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(), |
| 268 | ViewOpts, std::move(ExpansionCoverage)); |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 269 | attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); |
| 270 | View.addExpansion(Expansion.Region, std::move(SubView)); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Justin Bogner | 297149b | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 274 | std::unique_ptr<SourceCoverageView> |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 275 | CodeCoverageTool::createFunctionView(const FunctionRecord &Function, |
Vedant Kumar | 7b18a76 | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 276 | const CoverageMapping &Coverage) { |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 277 | auto FunctionCoverage = Coverage.getCoverageForFunction(Function); |
| 278 | if (FunctionCoverage.empty()) |
Justin Bogner | 297149b | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 279 | return nullptr; |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 280 | auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename()); |
Justin Bogner | 297149b | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 281 | if (!SourceBuffer) |
| 282 | return nullptr; |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 283 | |
| 284 | auto Expansions = FunctionCoverage.getExpansions(); |
Vedant Kumar | 4858447 | 2017-02-05 20:10:58 +0000 | [diff] [blame] | 285 | auto View = SourceCoverageView::create(DC.demangle(Function.Name), |
Vedant Kumar | d2e36ca | 2016-09-08 00:56:48 +0000 | [diff] [blame] | 286 | SourceBuffer.get(), ViewOpts, |
| 287 | std::move(FunctionCoverage)); |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 288 | attachExpansionSubViews(*View, Expansions, Coverage); |
| 289 | |
| 290 | return View; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 293 | std::unique_ptr<SourceCoverageView> |
| 294 | CodeCoverageTool::createSourceFileView(StringRef SourceFile, |
Vedant Kumar | 7b18a76 | 2016-07-15 01:19:33 +0000 | [diff] [blame] | 295 | const CoverageMapping &Coverage) { |
Justin Bogner | 297149b | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 296 | auto SourceBuffer = getSourceFile(SourceFile); |
| 297 | if (!SourceBuffer) |
| 298 | return nullptr; |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 299 | auto FileCoverage = Coverage.getCoverageForFile(SourceFile); |
| 300 | if (FileCoverage.empty()) |
Justin Bogner | 297149b | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 301 | return nullptr; |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 302 | |
| 303 | auto Expansions = FileCoverage.getExpansions(); |
Vedant Kumar | a9bf312 | 2016-06-25 02:58:30 +0000 | [diff] [blame] | 304 | auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(), |
| 305 | ViewOpts, std::move(FileCoverage)); |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 306 | attachExpansionSubViews(*View, Expansions, Coverage); |
Vedant Kumar | f6b5345 | 2017-08-02 23:35:24 +0000 | [diff] [blame] | 307 | if (!ViewOpts.ShowFunctionInstantiations) |
| 308 | return View; |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 309 | |
Vedant Kumar | a8dfa81 | 2017-08-02 23:35:25 +0000 | [diff] [blame] | 310 | for (const auto &Group : Coverage.getInstantiationGroups(SourceFile)) { |
| 311 | // Skip functions which have a single instantiation. |
| 312 | if (Group.size() < 2) |
| 313 | continue; |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 314 | |
Vedant Kumar | a8dfa81 | 2017-08-02 23:35:25 +0000 | [diff] [blame] | 315 | for (const FunctionRecord *Function : Group.getInstantiations()) { |
| 316 | std::unique_ptr<SourceCoverageView> SubView{nullptr}; |
Vedant Kumar | 1390807 | 2016-09-20 21:27:48 +0000 | [diff] [blame] | 317 | |
Vedant Kumar | a8dfa81 | 2017-08-02 23:35:25 +0000 | [diff] [blame] | 318 | StringRef Funcname = DC.demangle(Function->Name); |
| 319 | |
| 320 | if (Function->ExecutionCount > 0) { |
| 321 | auto SubViewCoverage = Coverage.getCoverageForFunction(*Function); |
| 322 | auto SubViewExpansions = SubViewCoverage.getExpansions(); |
| 323 | SubView = SourceCoverageView::create( |
| 324 | Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage)); |
| 325 | attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); |
| 326 | } |
| 327 | |
| 328 | unsigned FileID = Function->CountedRegions.front().FileID; |
| 329 | unsigned Line = 0; |
| 330 | for (const auto &CR : Function->CountedRegions) |
| 331 | if (CR.FileID == FileID) |
| 332 | Line = std::max(CR.LineEnd, Line); |
| 333 | View->addInstantiation(Funcname, Line, std::move(SubView)); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
Justin Bogner | 297149b | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 336 | return View; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Justin Bogner | 0e93e26 | 2015-05-04 04:09:38 +0000 | [diff] [blame] | 339 | static bool modifiedTimeGT(StringRef LHS, StringRef RHS) { |
| 340 | sys::fs::file_status Status; |
| 341 | if (sys::fs::status(LHS, Status)) |
| 342 | return false; |
| 343 | auto LHSTime = Status.getLastModificationTime(); |
| 344 | if (sys::fs::status(RHS, Status)) |
| 345 | return false; |
| 346 | auto RHSTime = Status.getLastModificationTime(); |
| 347 | return LHSTime > RHSTime; |
| 348 | } |
| 349 | |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 350 | std::unique_ptr<CoverageMapping> CodeCoverageTool::load() { |
Vedant Kumar | d4998ec | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 351 | for (StringRef ObjectFilename : ObjectFilenames) |
| 352 | if (modifiedTimeGT(ObjectFilename, PGOFilename)) |
| 353 | warning("profile data may be out of date - object is newer", |
| 354 | ObjectFilename); |
Vedant Kumar | a3ff212 | 2016-07-18 17:53:12 +0000 | [diff] [blame] | 355 | auto CoverageOrErr = |
Vedant Kumar | e72b881 | 2017-08-01 21:23:26 +0000 | [diff] [blame] | 356 | CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches); |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 357 | if (Error E = CoverageOrErr.takeError()) { |
Vedant Kumar | d4998ec | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 358 | error("Failed to load coverage: " + toString(std::move(E)), |
| 359 | join(ObjectFilenames.begin(), ObjectFilenames.end(), ", ")); |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 360 | return nullptr; |
| 361 | } |
| 362 | auto Coverage = std::move(CoverageOrErr.get()); |
| 363 | unsigned Mismatched = Coverage->getMismatchedCount(); |
Vedant Kumar | 3d39fc2 | 2017-09-21 01:11:30 +0000 | [diff] [blame] | 364 | if (Mismatched) { |
Benjamin Kramer | ca5092a | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 365 | warning(Twine(Mismatched) + " functions have mismatched data"); |
Justin Bogner | ddbcfa0 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 366 | |
Vedant Kumar | 3d39fc2 | 2017-09-21 01:11:30 +0000 | [diff] [blame] | 367 | if (ViewOpts.Debug) { |
| 368 | for (const auto &HashMismatch : Coverage->getHashMismatches()) |
| 369 | errs() << "hash-mismatch: " |
| 370 | << "No profile record found for '" << HashMismatch.first << "'" |
Benjamin Kramer | ca5092a | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 371 | << " with hash = 0x" << Twine::utohexstr(HashMismatch.second) |
| 372 | << '\n'; |
Vedant Kumar | 3d39fc2 | 2017-09-21 01:11:30 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 376 | remapPathNames(*Coverage); |
| 377 | |
Vedant Kumar | 3de4fb3 | 2016-09-23 20:13:41 +0000 | [diff] [blame] | 378 | if (!SourceFiles.empty()) |
| 379 | removeUnmappedInputs(*Coverage); |
| 380 | |
| 381 | demangleSymbols(*Coverage); |
| 382 | |
| 383 | return Coverage; |
| 384 | } |
| 385 | |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 386 | void CodeCoverageTool::remapPathNames(const CoverageMapping &Coverage) { |
| 387 | if (!PathRemapping) |
| 388 | return; |
| 389 | |
| 390 | // Convert remapping paths to native paths with trailing seperators. |
| 391 | auto nativeWithTrailing = [](StringRef Path) -> std::string { |
| 392 | if (Path.empty()) |
| 393 | return ""; |
| 394 | SmallString<128> NativePath; |
| 395 | sys::path::native(Path, NativePath); |
| 396 | if (!sys::path::is_separator(NativePath.back())) |
| 397 | NativePath += sys::path::get_separator(); |
| 398 | return NativePath.c_str(); |
| 399 | }; |
| 400 | std::string RemapFrom = nativeWithTrailing(PathRemapping->first); |
| 401 | std::string RemapTo = nativeWithTrailing(PathRemapping->second); |
| 402 | |
| 403 | // Create a mapping from coverage data file paths to local paths. |
| 404 | for (StringRef Filename : Coverage.getUniqueSourceFiles()) { |
| 405 | SmallString<128> NativeFilename; |
| 406 | sys::path::native(Filename, NativeFilename); |
| 407 | if (NativeFilename.startswith(RemapFrom)) { |
| 408 | RemappedFilenames[Filename] = |
| 409 | RemapTo + NativeFilename.substr(RemapFrom.size()).str(); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Convert input files from local paths to coverage data file paths. |
| 414 | StringMap<std::string> InvRemappedFilenames; |
| 415 | for (const auto &RemappedFilename : RemappedFilenames) |
| 416 | InvRemappedFilenames[RemappedFilename.getValue()] = RemappedFilename.getKey(); |
| 417 | |
| 418 | for (std::string &Filename : SourceFiles) { |
| 419 | SmallString<128> NativeFilename; |
| 420 | sys::path::native(Filename, NativeFilename); |
| 421 | auto CovFileName = InvRemappedFilenames.find(NativeFilename); |
| 422 | if (CovFileName != InvRemappedFilenames.end()) |
| 423 | Filename = CovFileName->second; |
| 424 | } |
| 425 | } |
| 426 | |
Vedant Kumar | 3de4fb3 | 2016-09-23 20:13:41 +0000 | [diff] [blame] | 427 | void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) { |
| 428 | std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles(); |
Vedant Kumar | daacf3c | 2016-09-23 18:57:35 +0000 | [diff] [blame] | 429 | |
| 430 | auto UncoveredFilesIt = SourceFiles.end(); |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 431 | // The user may have specified source files which aren't in the coverage |
| 432 | // mapping. Filter these files away. |
| 433 | UncoveredFilesIt = std::remove_if( |
| 434 | SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) { |
| 435 | return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(), |
| 436 | SF); |
| 437 | }); |
Justin Bogner | ddbcfa0 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 438 | |
Vedant Kumar | daacf3c | 2016-09-23 18:57:35 +0000 | [diff] [blame] | 439 | SourceFiles.erase(UncoveredFilesIt, SourceFiles.end()); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 442 | void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) { |
| 443 | if (!ViewOpts.hasDemangler()) |
| 444 | return; |
| 445 | |
| 446 | // Pass function names to the demangler in a temporary file. |
| 447 | int InputFD; |
| 448 | SmallString<256> InputPath; |
| 449 | std::error_code EC = |
| 450 | sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath); |
| 451 | if (EC) { |
| 452 | error(InputPath, EC.message()); |
| 453 | return; |
| 454 | } |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 455 | ToolOutputFile InputTOF{InputPath, InputFD}; |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 456 | |
| 457 | unsigned NumSymbols = 0; |
| 458 | for (const auto &Function : Coverage.getCoveredFunctions()) { |
| 459 | InputTOF.os() << Function.Name << '\n'; |
| 460 | ++NumSymbols; |
| 461 | } |
Vedant Kumar | 84b5112 | 2016-07-15 23:08:22 +0000 | [diff] [blame] | 462 | InputTOF.os().close(); |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 463 | |
| 464 | // Use another temporary file to store the demangler's output. |
| 465 | int OutputFD; |
| 466 | SmallString<256> OutputPath; |
| 467 | EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD, |
| 468 | OutputPath); |
| 469 | if (EC) { |
| 470 | error(OutputPath, EC.message()); |
| 471 | return; |
| 472 | } |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 473 | ToolOutputFile OutputTOF{OutputPath, OutputFD}; |
Vedant Kumar | 84b5112 | 2016-07-15 23:08:22 +0000 | [diff] [blame] | 474 | OutputTOF.os().close(); |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 475 | |
| 476 | // Invoke the demangler. |
Zachary Turner | 0dcc115 | 2018-06-12 17:43:52 +0000 | [diff] [blame] | 477 | std::vector<StringRef> ArgsV; |
| 478 | for (StringRef Arg : ViewOpts.DemanglerOpts) |
| 479 | ArgsV.push_back(Arg); |
Alexander Kornienko | 87e117d | 2017-09-13 17:03:37 +0000 | [diff] [blame] | 480 | Optional<StringRef> Redirects[] = {InputPath.str(), OutputPath.str(), {""}}; |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 481 | std::string ErrMsg; |
Zachary Turner | 0dcc115 | 2018-06-12 17:43:52 +0000 | [diff] [blame] | 482 | int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV, |
| 483 | /*env=*/None, Redirects, /*secondsToWait=*/0, |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 484 | /*memoryLimit=*/0, &ErrMsg); |
| 485 | if (RC) { |
| 486 | error(ErrMsg, ViewOpts.DemanglerOpts[0]); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | // Parse the demangler's output. |
| 491 | auto BufOrError = MemoryBuffer::getFile(OutputPath); |
| 492 | if (!BufOrError) { |
| 493 | error(OutputPath, BufOrError.getError().message()); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError); |
| 498 | |
| 499 | SmallVector<StringRef, 8> Symbols; |
| 500 | StringRef DemanglerData = DemanglerBuf->getBuffer(); |
| 501 | DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols, |
| 502 | /*KeepEmpty=*/false); |
| 503 | if (Symbols.size() != NumSymbols) { |
| 504 | error("Demangler did not provide expected number of symbols"); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | // Cache the demangled names. |
| 509 | unsigned I = 0; |
| 510 | for (const auto &Function : Coverage.getCoveredFunctions()) |
Igor Kudrin | e3a9558 | 2017-02-19 14:26:52 +0000 | [diff] [blame] | 511 | // On Windows, lines in the demangler's output file end with "\r\n". |
| 512 | // Splitting by '\n' keeps '\r's, so cut them now. |
| 513 | DC.DemangledNames[Function.Name] = Symbols[I++].rtrim(); |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Vedant Kumar | 4c50ebb | 2016-10-19 17:55:44 +0000 | [diff] [blame] | 516 | void CodeCoverageTool::writeSourceFileView(StringRef SourceFile, |
| 517 | CoverageMapping *Coverage, |
| 518 | CoveragePrinter *Printer, |
| 519 | bool ShowFilenames) { |
| 520 | auto View = createSourceFileView(SourceFile, *Coverage); |
| 521 | if (!View) { |
| 522 | warning("The file '" + SourceFile + "' isn't covered."); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false); |
| 527 | if (Error E = OSOrErr.takeError()) { |
| 528 | error("Could not create view file!", toString(std::move(E))); |
| 529 | return; |
| 530 | } |
| 531 | auto OS = std::move(OSOrErr.get()); |
| 532 | |
| 533 | View->print(*OS.get(), /*Wholefile=*/true, |
Sean Eveson | 078c2c3 | 2017-09-28 10:07:30 +0000 | [diff] [blame] | 534 | /*ShowSourceName=*/ShowFilenames, |
| 535 | /*ShowTitle=*/ViewOpts.hasOutputDirectory()); |
Vedant Kumar | 4c50ebb | 2016-10-19 17:55:44 +0000 | [diff] [blame] | 536 | Printer->closeViewFile(std::move(OS)); |
| 537 | } |
| 538 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 539 | int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { |
Vedant Kumar | d4998ec | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 540 | cl::opt<std::string> CovFilename( |
| 541 | cl::Positional, cl::desc("Covered executable or object file.")); |
| 542 | |
| 543 | cl::list<std::string> CovFilenames( |
| 544 | "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore, |
| 545 | cl::CommaSeparated); |
Justin Bogner | 6e5def6 | 2014-10-30 20:51:24 +0000 | [diff] [blame] | 546 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 547 | cl::list<std::string> InputSourceFiles( |
| 548 | cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore); |
| 549 | |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 550 | cl::opt<bool> DebugDumpCollectedPaths( |
| 551 | "dump-collected-paths", cl::Optional, cl::Hidden, |
| 552 | cl::desc("Show the collected paths to source files")); |
| 553 | |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 554 | cl::opt<std::string, true> PGOFilename( |
| 555 | "instr-profile", cl::Required, cl::location(this->PGOFilename), |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 556 | cl::desc( |
| 557 | "File with the profile data obtained after an instrumented run")); |
| 558 | |
Vedant Kumar | e72b881 | 2017-08-01 21:23:26 +0000 | [diff] [blame] | 559 | cl::list<std::string> Arches( |
| 560 | "arch", cl::desc("architectures of the coverage mapping binaries")); |
Justin Bogner | d39109d | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 561 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 562 | cl::opt<bool> DebugDump("dump", cl::Optional, |
| 563 | cl::desc("Show internal debug dump")); |
| 564 | |
Vedant Kumar | cd29c6b | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 565 | cl::opt<CoverageViewOptions::OutputFormat> Format( |
| 566 | "format", cl::desc("Output format for line-based coverage reports"), |
| 567 | cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text", |
| 568 | "Text output"), |
Vedant Kumar | 55c8c00 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 569 | clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html", |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 570 | "HTML output"), |
| 571 | clEnumValN(CoverageViewOptions::OutputFormat::Lcov, "lcov", |
| 572 | "lcov tracefile output")), |
Vedant Kumar | cd29c6b | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 573 | cl::init(CoverageViewOptions::OutputFormat::Text)); |
| 574 | |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 575 | cl::opt<std::string> PathRemap( |
| 576 | "path-equivalence", cl::Optional, |
| 577 | cl::desc("<from>,<to> Map coverage data paths to local source file " |
| 578 | "paths")); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 579 | |
| 580 | cl::OptionCategory FilteringCategory("Function filtering options"); |
| 581 | |
| 582 | cl::list<std::string> NameFilters( |
| 583 | "name", cl::Optional, |
| 584 | cl::desc("Show code coverage only for functions with the given name"), |
| 585 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 586 | |
Sean Eveson | 10edef1 | 2017-08-31 09:11:31 +0000 | [diff] [blame] | 587 | cl::list<std::string> NameFilterFiles( |
| 588 | "name-whitelist", cl::Optional, |
| 589 | cl::desc("Show code coverage only for functions listed in the given " |
| 590 | "file"), |
| 591 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 592 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 593 | cl::list<std::string> NameRegexFilters( |
| 594 | "name-regex", cl::Optional, |
| 595 | cl::desc("Show code coverage only for functions that match the given " |
| 596 | "regular expression"), |
| 597 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 598 | |
Max Moroz | 06fa683 | 2018-04-09 15:20:35 +0000 | [diff] [blame] | 599 | cl::list<std::string> IgnoreFilenameRegexFilters( |
| 600 | "ignore-filename-regex", cl::Optional, |
| 601 | cl::desc("Skip source code files with file paths that match the given " |
| 602 | "regular expression"), |
| 603 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 604 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 605 | cl::opt<double> RegionCoverageLtFilter( |
| 606 | "region-coverage-lt", cl::Optional, |
| 607 | cl::desc("Show code coverage only for functions with region coverage " |
| 608 | "less than the given threshold"), |
| 609 | cl::cat(FilteringCategory)); |
| 610 | |
| 611 | cl::opt<double> RegionCoverageGtFilter( |
| 612 | "region-coverage-gt", cl::Optional, |
| 613 | cl::desc("Show code coverage only for functions with region coverage " |
| 614 | "greater than the given threshold"), |
| 615 | cl::cat(FilteringCategory)); |
| 616 | |
| 617 | cl::opt<double> LineCoverageLtFilter( |
| 618 | "line-coverage-lt", cl::Optional, |
| 619 | cl::desc("Show code coverage only for functions with line coverage less " |
| 620 | "than the given threshold"), |
| 621 | cl::cat(FilteringCategory)); |
| 622 | |
| 623 | cl::opt<double> LineCoverageGtFilter( |
| 624 | "line-coverage-gt", cl::Optional, |
| 625 | cl::desc("Show code coverage only for functions with line coverage " |
| 626 | "greater than the given threshold"), |
| 627 | cl::cat(FilteringCategory)); |
| 628 | |
Justin Bogner | 12f0d20 | 2015-03-19 04:45:16 +0000 | [diff] [blame] | 629 | cl::opt<cl::boolOrDefault> UseColor( |
| 630 | "use-color", cl::desc("Emit colored output (default=autodetect)"), |
| 631 | cl::init(cl::BOU_UNSET)); |
Justin Bogner | cc690d6 | 2015-03-19 00:02:23 +0000 | [diff] [blame] | 632 | |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 633 | cl::list<std::string> DemanglerOpts( |
| 634 | "Xdemangler", cl::desc("<demangler-path>|<demangler-option>")); |
| 635 | |
Eli Friedman | d3822e4 | 2017-09-11 22:56:20 +0000 | [diff] [blame] | 636 | cl::opt<bool> RegionSummary( |
| 637 | "show-region-summary", cl::Optional, |
| 638 | cl::desc("Show region statistics in summary table"), |
| 639 | cl::init(true)); |
| 640 | |
| 641 | cl::opt<bool> InstantiationSummary( |
| 642 | "show-instantiation-summary", cl::Optional, |
| 643 | cl::desc("Show instantiation statistics in summary table")); |
| 644 | |
Max Moroz | d4b0ab0 | 2017-12-11 23:17:46 +0000 | [diff] [blame] | 645 | cl::opt<bool> SummaryOnly( |
| 646 | "summary-only", cl::Optional, |
| 647 | cl::desc("Export only summary information for each source file")); |
| 648 | |
Max Moroz | d6352ca | 2018-01-05 16:15:07 +0000 | [diff] [blame] | 649 | cl::opt<unsigned> NumThreads( |
| 650 | "num-threads", cl::init(0), |
| 651 | cl::desc("Number of merge threads to use (default: autodetect)")); |
| 652 | cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"), |
| 653 | cl::aliasopt(NumThreads)); |
| 654 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 655 | auto commandLineParser = [&, this](int argc, const char **argv) -> int { |
| 656 | cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); |
| 657 | ViewOpts.Debug = DebugDump; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 658 | |
Vedant Kumar | d4998ec | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 659 | if (!CovFilename.empty()) |
| 660 | ObjectFilenames.emplace_back(CovFilename); |
| 661 | for (const std::string &Filename : CovFilenames) |
| 662 | ObjectFilenames.emplace_back(Filename); |
| 663 | if (ObjectFilenames.empty()) { |
Vedant Kumar | 1fc13ff | 2016-10-25 19:52:57 +0000 | [diff] [blame] | 664 | errs() << "No filenames specified!\n"; |
Vedant Kumar | d4998ec | 2016-10-25 17:40:55 +0000 | [diff] [blame] | 665 | ::exit(1); |
| 666 | } |
| 667 | |
Vedant Kumar | cd29c6b | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 668 | ViewOpts.Format = Format; |
| 669 | switch (ViewOpts.Format) { |
| 670 | case CoverageViewOptions::OutputFormat::Text: |
| 671 | ViewOpts.Colors = UseColor == cl::BOU_UNSET |
| 672 | ? sys::Process::StandardOutHasColors() |
| 673 | : UseColor == cl::BOU_TRUE; |
| 674 | break; |
Vedant Kumar | 55c8c00 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 675 | case CoverageViewOptions::OutputFormat::HTML: |
| 676 | if (UseColor == cl::BOU_FALSE) |
Vedant Kumar | 1fc13ff | 2016-10-25 19:52:57 +0000 | [diff] [blame] | 677 | errs() << "Color output cannot be disabled when generating html.\n"; |
Vedant Kumar | 55c8c00 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 678 | ViewOpts.Colors = true; |
| 679 | break; |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 680 | case CoverageViewOptions::OutputFormat::Lcov: |
| 681 | if (UseColor == cl::BOU_TRUE) |
| 682 | errs() << "Color output cannot be enabled when generating lcov.\n"; |
| 683 | ViewOpts.Colors = false; |
| 684 | break; |
Vedant Kumar | cd29c6b | 2016-06-29 00:38:21 +0000 | [diff] [blame] | 685 | } |
Justin Bogner | cc690d6 | 2015-03-19 00:02:23 +0000 | [diff] [blame] | 686 | |
Sean Eveson | 8574d59 | 2017-08-14 10:20:12 +0000 | [diff] [blame] | 687 | // If path-equivalence was given and is a comma seperated pair then set |
| 688 | // PathRemapping. |
| 689 | auto EquivPair = StringRef(PathRemap).split(','); |
| 690 | if (!(EquivPair.first.empty() && EquivPair.second.empty())) |
| 691 | PathRemapping = EquivPair; |
| 692 | |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 693 | // If a demangler is supplied, check if it exists and register it. |
Jordan Rupprecht | f6ea129 | 2018-12-20 00:57:06 +0000 | [diff] [blame] | 694 | if (!DemanglerOpts.empty()) { |
Vedant Kumar | 13ca1cc | 2016-07-15 22:44:57 +0000 | [diff] [blame] | 695 | auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]); |
| 696 | if (!DemanglerPathOrErr) { |
| 697 | error("Could not find the demangler!", |
| 698 | DemanglerPathOrErr.getError().message()); |
| 699 | return 1; |
| 700 | } |
| 701 | DemanglerOpts[0] = *DemanglerPathOrErr; |
| 702 | ViewOpts.DemanglerOpts.swap(DemanglerOpts); |
| 703 | } |
| 704 | |
Sean Eveson | 10edef1 | 2017-08-31 09:11:31 +0000 | [diff] [blame] | 705 | // Read in -name-whitelist files. |
| 706 | if (!NameFilterFiles.empty()) { |
| 707 | std::string SpecialCaseListErr; |
| 708 | NameWhitelist = |
| 709 | SpecialCaseList::create(NameFilterFiles, SpecialCaseListErr); |
| 710 | if (!NameWhitelist) |
| 711 | error(SpecialCaseListErr); |
| 712 | } |
| 713 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 714 | // Create the function filters |
Sean Eveson | 10edef1 | 2017-08-31 09:11:31 +0000 | [diff] [blame] | 715 | if (!NameFilters.empty() || NameWhitelist || !NameRegexFilters.empty()) { |
Vedant Kumar | 0f0fd42 | 2017-08-04 00:36:24 +0000 | [diff] [blame] | 716 | auto NameFilterer = llvm::make_unique<CoverageFilters>(); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 717 | for (const auto &Name : NameFilters) |
| 718 | NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name)); |
Sean Eveson | 10edef1 | 2017-08-31 09:11:31 +0000 | [diff] [blame] | 719 | if (NameWhitelist) |
| 720 | NameFilterer->push_back( |
| 721 | llvm::make_unique<NameWhitelistCoverageFilter>(*NameWhitelist)); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 722 | for (const auto &Regex : NameRegexFilters) |
| 723 | NameFilterer->push_back( |
| 724 | llvm::make_unique<NameRegexCoverageFilter>(Regex)); |
Vedant Kumar | 0f0fd42 | 2017-08-04 00:36:24 +0000 | [diff] [blame] | 725 | Filters.push_back(std::move(NameFilterer)); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 726 | } |
Max Moroz | 06fa683 | 2018-04-09 15:20:35 +0000 | [diff] [blame] | 727 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 728 | if (RegionCoverageLtFilter.getNumOccurrences() || |
| 729 | RegionCoverageGtFilter.getNumOccurrences() || |
| 730 | LineCoverageLtFilter.getNumOccurrences() || |
| 731 | LineCoverageGtFilter.getNumOccurrences()) { |
Vedant Kumar | 0f0fd42 | 2017-08-04 00:36:24 +0000 | [diff] [blame] | 732 | auto StatFilterer = llvm::make_unique<CoverageFilters>(); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 733 | if (RegionCoverageLtFilter.getNumOccurrences()) |
| 734 | StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>( |
| 735 | RegionCoverageFilter::LessThan, RegionCoverageLtFilter)); |
| 736 | if (RegionCoverageGtFilter.getNumOccurrences()) |
| 737 | StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>( |
| 738 | RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter)); |
| 739 | if (LineCoverageLtFilter.getNumOccurrences()) |
| 740 | StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>( |
| 741 | LineCoverageFilter::LessThan, LineCoverageLtFilter)); |
| 742 | if (LineCoverageGtFilter.getNumOccurrences()) |
| 743 | StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>( |
| 744 | RegionCoverageFilter::GreaterThan, LineCoverageGtFilter)); |
Vedant Kumar | 0f0fd42 | 2017-08-04 00:36:24 +0000 | [diff] [blame] | 745 | Filters.push_back(std::move(StatFilterer)); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Max Moroz | 06fa683 | 2018-04-09 15:20:35 +0000 | [diff] [blame] | 748 | // Create the ignore filename filters. |
| 749 | for (const auto &RE : IgnoreFilenameRegexFilters) |
| 750 | IgnoreFilenameFilters.push_back( |
| 751 | llvm::make_unique<NameRegexCoverageFilter>(RE)); |
| 752 | |
Vedant Kumar | e72b881 | 2017-08-01 21:23:26 +0000 | [diff] [blame] | 753 | if (!Arches.empty()) { |
| 754 | for (const std::string &Arch : Arches) { |
| 755 | if (Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) { |
| 756 | error("Unknown architecture: " + Arch); |
| 757 | return 1; |
| 758 | } |
| 759 | CoverageArches.emplace_back(Arch); |
| 760 | } |
| 761 | if (CoverageArches.size() != ObjectFilenames.size()) { |
| 762 | error("Number of architectures doesn't match the number of objects"); |
| 763 | return 1; |
| 764 | } |
Justin Bogner | d39109d | 2015-03-11 02:30:51 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Max Moroz | 06fa683 | 2018-04-09 15:20:35 +0000 | [diff] [blame] | 767 | // IgnoreFilenameFilters are applied even when InputSourceFiles specified. |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 768 | for (const std::string &File : InputSourceFiles) |
| 769 | collectPaths(File); |
| 770 | |
| 771 | if (DebugDumpCollectedPaths) { |
Vedant Kumar | fd06635 | 2016-09-23 18:57:32 +0000 | [diff] [blame] | 772 | for (const std::string &SF : SourceFiles) |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 773 | outs() << SF << '\n'; |
| 774 | ::exit(0); |
Justin Bogner | ddbcfa0 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 775 | } |
Vedant Kumar | ef76be0 | 2016-09-22 21:49:43 +0000 | [diff] [blame] | 776 | |
Eli Friedman | d3822e4 | 2017-09-11 22:56:20 +0000 | [diff] [blame] | 777 | ViewOpts.ShowRegionSummary = RegionSummary; |
| 778 | ViewOpts.ShowInstantiationSummary = InstantiationSummary; |
Max Moroz | d4b0ab0 | 2017-12-11 23:17:46 +0000 | [diff] [blame] | 779 | ViewOpts.ExportSummaryOnly = SummaryOnly; |
Max Moroz | d6352ca | 2018-01-05 16:15:07 +0000 | [diff] [blame] | 780 | ViewOpts.NumThreads = NumThreads; |
Eli Friedman | d3822e4 | 2017-09-11 22:56:20 +0000 | [diff] [blame] | 781 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 782 | return 0; |
| 783 | }; |
| 784 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 785 | switch (Cmd) { |
| 786 | case Show: |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 787 | return doShow(argc, argv, commandLineParser); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 788 | case Report: |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 789 | return doReport(argc, argv, commandLineParser); |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 790 | case Export: |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 791 | return doExport(argc, argv, commandLineParser); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 792 | } |
| 793 | return 0; |
| 794 | } |
| 795 | |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 796 | int CodeCoverageTool::doShow(int argc, const char **argv, |
| 797 | CommandLineParserType commandLineParser) { |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 798 | |
| 799 | cl::OptionCategory ViewCategory("Viewing options"); |
| 800 | |
| 801 | cl::opt<bool> ShowLineExecutionCounts( |
| 802 | "show-line-counts", cl::Optional, |
| 803 | cl::desc("Show the execution counts for each line"), cl::init(true), |
| 804 | cl::cat(ViewCategory)); |
| 805 | |
| 806 | cl::opt<bool> ShowRegions( |
| 807 | "show-regions", cl::Optional, |
| 808 | cl::desc("Show the execution counts for each region"), |
| 809 | cl::cat(ViewCategory)); |
| 810 | |
| 811 | cl::opt<bool> ShowBestLineRegionsCounts( |
| 812 | "show-line-counts-or-regions", cl::Optional, |
| 813 | cl::desc("Show the execution counts for each line, or the execution " |
| 814 | "counts for each region on lines that have multiple regions"), |
| 815 | cl::cat(ViewCategory)); |
| 816 | |
| 817 | cl::opt<bool> ShowExpansions("show-expansions", cl::Optional, |
| 818 | cl::desc("Show expanded source regions"), |
| 819 | cl::cat(ViewCategory)); |
| 820 | |
| 821 | cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional, |
| 822 | cl::desc("Show function instantiations"), |
Vedant Kumar | f6b5345 | 2017-08-02 23:35:24 +0000 | [diff] [blame] | 823 | cl::init(true), cl::cat(ViewCategory)); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 824 | |
Vedant Kumar | 40a78f8 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 825 | cl::opt<std::string> ShowOutputDirectory( |
| 826 | "output-dir", cl::init(""), |
| 827 | cl::desc("Directory in which coverage information is written out")); |
| 828 | cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"), |
| 829 | cl::aliasopt(ShowOutputDirectory)); |
| 830 | |
Ying Yi | 65787e2 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 831 | cl::opt<uint32_t> TabSize( |
Vedant Kumar | df48bd9 | 2016-08-04 18:00:42 +0000 | [diff] [blame] | 832 | "tab-size", cl::init(2), |
| 833 | cl::desc( |
| 834 | "Set tab expansion size for html coverage reports (default = 2)")); |
Ying Yi | 65787e2 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 835 | |
Ying Yi | 1461e99 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 836 | cl::opt<std::string> ProjectTitle( |
| 837 | "project-title", cl::Optional, |
| 838 | cl::desc("Set project title for the coverage report")); |
| 839 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 840 | auto Err = commandLineParser(argc, argv); |
| 841 | if (Err) |
| 842 | return Err; |
| 843 | |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 844 | if (ViewOpts.Format == CoverageViewOptions::OutputFormat::Lcov) { |
| 845 | error("Lcov format should be used with 'llvm-cov export'."); |
| 846 | return 1; |
| 847 | } |
| 848 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 849 | ViewOpts.ShowLineNumbers = true; |
| 850 | ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 || |
| 851 | !ShowRegions || ShowBestLineRegionsCounts; |
| 852 | ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 853 | ViewOpts.ShowExpandedRegions = ShowExpansions; |
| 854 | ViewOpts.ShowFunctionInstantiations = ShowInstantiations; |
Vedant Kumar | 40a78f8 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 855 | ViewOpts.ShowOutputDirectory = ShowOutputDirectory; |
Ying Yi | 65787e2 | 2016-08-04 10:39:43 +0000 | [diff] [blame] | 856 | ViewOpts.TabSize = TabSize; |
Ying Yi | 1461e99 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 857 | ViewOpts.ProjectTitle = ProjectTitle; |
Vedant Kumar | 40a78f8 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 858 | |
Vedant Kumar | 184c224 | 2016-06-28 16:12:20 +0000 | [diff] [blame] | 859 | if (ViewOpts.hasOutputDirectory()) { |
Vedant Kumar | 40a78f8 | 2016-06-28 02:09:39 +0000 | [diff] [blame] | 860 | if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) { |
| 861 | error("Could not create output directory!", E.message()); |
| 862 | return 1; |
| 863 | } |
| 864 | } |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 865 | |
Ying Yi | 1461e99 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 866 | sys::fs::file_status Status; |
| 867 | if (sys::fs::status(PGOFilename, Status)) { |
| 868 | error("profdata file error: can not get the file status. \n"); |
| 869 | return 1; |
| 870 | } |
| 871 | |
| 872 | auto ModifiedTime = Status.getLastModificationTime(); |
Pavel Labath | 2864c2a | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 873 | std::string ModifiedTimeStr = to_string(ModifiedTime); |
Benjamin Kramer | bf60ce0 | 2016-11-30 10:01:11 +0000 | [diff] [blame] | 874 | size_t found = ModifiedTimeStr.rfind(':'); |
Ying Yi | 1461e99 | 2016-08-24 14:27:23 +0000 | [diff] [blame] | 875 | ViewOpts.CreatedTimeStr = (found != std::string::npos) |
| 876 | ? "Created: " + ModifiedTimeStr.substr(0, found) |
| 877 | : "Created: " + ModifiedTimeStr; |
| 878 | |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 879 | auto Coverage = load(); |
| 880 | if (!Coverage) |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 881 | return 1; |
| 882 | |
Vedant Kumar | 028d73c | 2016-06-28 16:12:24 +0000 | [diff] [blame] | 883 | auto Printer = CoveragePrinter::create(ViewOpts); |
| 884 | |
Sean Eveson | 950974f | 2017-09-27 16:20:07 +0000 | [diff] [blame] | 885 | if (SourceFiles.empty()) |
| 886 | // Get the source files from the function coverage mapping. |
Max Moroz | 06fa683 | 2018-04-09 15:20:35 +0000 | [diff] [blame] | 887 | for (StringRef Filename : Coverage->getUniqueSourceFiles()) { |
| 888 | if (!IgnoreFilenameFilters.matchesFilename(Filename)) |
| 889 | SourceFiles.push_back(Filename); |
| 890 | } |
Sean Eveson | 950974f | 2017-09-27 16:20:07 +0000 | [diff] [blame] | 891 | |
| 892 | // Create an index out of the source files. |
| 893 | if (ViewOpts.hasOutputDirectory()) { |
Sean Eveson | 078c2c3 | 2017-09-28 10:07:30 +0000 | [diff] [blame] | 894 | if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) { |
Sean Eveson | 950974f | 2017-09-27 16:20:07 +0000 | [diff] [blame] | 895 | error("Could not create index file!", toString(std::move(E))); |
| 896 | return 1; |
| 897 | } |
| 898 | } |
| 899 | |
Sean Eveson | 078c2c3 | 2017-09-28 10:07:30 +0000 | [diff] [blame] | 900 | if (!Filters.empty()) { |
| 901 | // Build the map of filenames to functions. |
| 902 | std::map<llvm::StringRef, std::vector<const FunctionRecord *>> |
| 903 | FilenameFunctionMap; |
| 904 | for (const auto &SourceFile : SourceFiles) |
| 905 | for (const auto &Function : Coverage->getCoveredFunctions(SourceFile)) |
| 906 | if (Filters.matches(*Coverage.get(), Function)) |
| 907 | FilenameFunctionMap[SourceFile].push_back(&Function); |
| 908 | |
| 909 | // Only print filter matching functions for each file. |
| 910 | for (const auto &FileFunc : FilenameFunctionMap) { |
| 911 | StringRef File = FileFunc.first; |
| 912 | const auto &Functions = FileFunc.second; |
| 913 | |
| 914 | auto OSOrErr = Printer->createViewFile(File, /*InToplevel=*/false); |
| 915 | if (Error E = OSOrErr.takeError()) { |
| 916 | error("Could not create view file!", toString(std::move(E))); |
| 917 | return 1; |
| 918 | } |
| 919 | auto OS = std::move(OSOrErr.get()); |
| 920 | |
Sean Eveson | 69863c0 | 2017-10-04 08:54:37 +0000 | [diff] [blame] | 921 | bool ShowTitle = ViewOpts.hasOutputDirectory(); |
Sean Eveson | 078c2c3 | 2017-09-28 10:07:30 +0000 | [diff] [blame] | 922 | for (const auto *Function : Functions) { |
| 923 | auto FunctionView = createFunctionView(*Function, *Coverage); |
| 924 | if (!FunctionView) { |
| 925 | warning("Could not read coverage for '" + Function->Name + "'."); |
| 926 | continue; |
| 927 | } |
| 928 | FunctionView->print(*OS.get(), /*WholeFile=*/false, |
| 929 | /*ShowSourceName=*/true, ShowTitle); |
| 930 | ShowTitle = false; |
| 931 | } |
| 932 | |
| 933 | Printer->closeViewFile(std::move(OS)); |
| 934 | } |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | // Show files |
| 939 | bool ShowFilenames = |
| 940 | (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() || |
| 941 | (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML); |
| 942 | |
Max Moroz | d6352ca | 2018-01-05 16:15:07 +0000 | [diff] [blame] | 943 | auto NumThreads = ViewOpts.NumThreads; |
| 944 | |
Vedant Kumar | 574c516 | 2017-07-11 01:23:29 +0000 | [diff] [blame] | 945 | // If NumThreads is not specified, auto-detect a good default. |
| 946 | if (NumThreads == 0) |
| 947 | NumThreads = |
| 948 | std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(), |
| 949 | unsigned(SourceFiles.size()))); |
| 950 | |
| 951 | if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) { |
Vedant Kumar | 4c50ebb | 2016-10-19 17:55:44 +0000 | [diff] [blame] | 952 | for (const std::string &SourceFile : SourceFiles) |
| 953 | writeSourceFileView(SourceFile, Coverage.get(), Printer.get(), |
| 954 | ShowFilenames); |
| 955 | } else { |
| 956 | // In -output-dir mode, it's safe to use multiple threads to print files. |
Vedant Kumar | 574c516 | 2017-07-11 01:23:29 +0000 | [diff] [blame] | 957 | ThreadPool Pool(NumThreads); |
Vedant Kumar | 4c50ebb | 2016-10-19 17:55:44 +0000 | [diff] [blame] | 958 | for (const std::string &SourceFile : SourceFiles) |
| 959 | Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile, |
| 960 | Coverage.get(), Printer.get(), ShowFilenames); |
| 961 | Pool.wait(); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 967 | int CodeCoverageTool::doReport(int argc, const char **argv, |
| 968 | CommandLineParserType commandLineParser) { |
Vedant Kumar | 2006b41 | 2017-02-05 20:11:08 +0000 | [diff] [blame] | 969 | cl::opt<bool> ShowFunctionSummaries( |
| 970 | "show-functions", cl::Optional, cl::init(false), |
| 971 | cl::desc("Show coverage summaries for each function")); |
| 972 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 973 | auto Err = commandLineParser(argc, argv); |
| 974 | if (Err) |
| 975 | return Err; |
| 976 | |
Vedant Kumar | 09f10e7 | 2017-02-28 16:57:28 +0000 | [diff] [blame] | 977 | if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) { |
Vedant Kumar | 55c8c00 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 978 | error("HTML output for summary reports is not yet supported."); |
Vedant Kumar | 09f10e7 | 2017-02-28 16:57:28 +0000 | [diff] [blame] | 979 | return 1; |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 980 | } else if (ViewOpts.Format == CoverageViewOptions::OutputFormat::Lcov) { |
| 981 | error("Lcov format should be used with 'llvm-cov export'."); |
| 982 | return 1; |
Vedant Kumar | 09f10e7 | 2017-02-28 16:57:28 +0000 | [diff] [blame] | 983 | } |
Vedant Kumar | 55c8c00 | 2016-07-06 21:44:05 +0000 | [diff] [blame] | 984 | |
Justin Bogner | 9eb3816 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 985 | auto Coverage = load(); |
| 986 | if (!Coverage) |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 987 | return 1; |
| 988 | |
Vedant Kumar | ff7a0df | 2016-09-06 22:45:57 +0000 | [diff] [blame] | 989 | CoverageReport Report(ViewOpts, *Coverage.get()); |
Vedant Kumar | 35ccd0b | 2017-09-25 23:10:03 +0000 | [diff] [blame] | 990 | if (!ShowFunctionSummaries) { |
Max Moroz | b5d60b8 | 2017-10-13 14:44:51 +0000 | [diff] [blame] | 991 | if (SourceFiles.empty()) |
Max Moroz | 06fa683 | 2018-04-09 15:20:35 +0000 | [diff] [blame] | 992 | Report.renderFileReports(llvm::outs(), IgnoreFilenameFilters); |
Max Moroz | b5d60b8 | 2017-10-13 14:44:51 +0000 | [diff] [blame] | 993 | else |
| 994 | Report.renderFileReports(llvm::outs(), SourceFiles); |
Vedant Kumar | 35ccd0b | 2017-09-25 23:10:03 +0000 | [diff] [blame] | 995 | } else { |
| 996 | if (SourceFiles.empty()) { |
| 997 | error("Source files must be specified when -show-functions=true is " |
| 998 | "specified"); |
| 999 | return 1; |
| 1000 | } |
| 1001 | |
Vedant Kumar | b766e93 | 2017-02-05 20:11:03 +0000 | [diff] [blame] | 1002 | Report.renderFunctionReports(SourceFiles, DC, llvm::outs()); |
Vedant Kumar | 35ccd0b | 2017-09-25 23:10:03 +0000 | [diff] [blame] | 1003 | } |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 1004 | return 0; |
| 1005 | } |
| 1006 | |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 1007 | int CodeCoverageTool::doExport(int argc, const char **argv, |
| 1008 | CommandLineParserType commandLineParser) { |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 1009 | |
| 1010 | auto Err = commandLineParser(argc, argv); |
| 1011 | if (Err) |
| 1012 | return Err; |
| 1013 | |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 1014 | if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text && |
| 1015 | ViewOpts.Format != CoverageViewOptions::OutputFormat::Lcov) { |
| 1016 | error("Coverage data can only be exported as textual JSON or an " |
| 1017 | "lcov tracefile."); |
Vedant Kumar | 09f10e7 | 2017-02-28 16:57:28 +0000 | [diff] [blame] | 1018 | return 1; |
| 1019 | } |
| 1020 | |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 1021 | auto Coverage = load(); |
| 1022 | if (!Coverage) { |
| 1023 | error("Could not load coverage information"); |
| 1024 | return 1; |
| 1025 | } |
| 1026 | |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 1027 | std::unique_ptr<CoverageExporter> Exporter; |
| 1028 | |
| 1029 | switch (ViewOpts.Format) { |
| 1030 | case CoverageViewOptions::OutputFormat::Text: |
| 1031 | Exporter = llvm::make_unique<CoverageExporterJson>(*Coverage.get(), |
| 1032 | ViewOpts, outs()); |
| 1033 | break; |
| 1034 | case CoverageViewOptions::OutputFormat::HTML: |
| 1035 | // Unreachable because we should have gracefully terminated with an error |
| 1036 | // above. |
| 1037 | llvm_unreachable("Export in HTML is not supported!"); |
| 1038 | case CoverageViewOptions::OutputFormat::Lcov: |
| 1039 | Exporter = llvm::make_unique<CoverageExporterLcov>(*Coverage.get(), |
| 1040 | ViewOpts, outs()); |
| 1041 | break; |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 1042 | } |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 1043 | |
| 1044 | if (SourceFiles.empty()) |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 1045 | Exporter->renderRoot(IgnoreFilenameFilters); |
Max Moroz | fc04e85 | 2018-01-04 19:33:29 +0000 | [diff] [blame] | 1046 | else |
Max Moroz | fcdc267 | 2018-11-09 16:10:44 +0000 | [diff] [blame] | 1047 | Exporter->renderRoot(SourceFiles); |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 1052 | int showMain(int argc, const char *argv[]) { |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 1053 | CodeCoverageTool Tool; |
| 1054 | return Tool.run(CodeCoverageTool::Show, argc, argv); |
| 1055 | } |
| 1056 | |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 1057 | int reportMain(int argc, const char *argv[]) { |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 1058 | CodeCoverageTool Tool; |
| 1059 | return Tool.run(CodeCoverageTool::Report, argc, argv); |
| 1060 | } |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 1061 | |
| 1062 | int exportMain(int argc, const char *argv[]) { |
| 1063 | CodeCoverageTool Tool; |
| 1064 | return Tool.run(CodeCoverageTool::Export, argc, argv); |
| 1065 | } |