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 "RenderingSupport.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 17 | #include "CoverageFilters.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 18 | #include "CoverageReport.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 19 | #include "CoverageSummary.h" |
| 20 | #include "CoverageViewOptions.h" |
| 21 | #include "SourceCoverageView.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 24 | #include "llvm/ProfileData/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" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ManagedStatic.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Path.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 31 | #include "llvm/Support/PrettyStackTrace.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Signals.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 33 | #include <functional> |
Justin Bogner | e53be06 | 2014-09-09 05:32:18 +0000 | [diff] [blame] | 34 | #include <system_error> |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
| 37 | using namespace coverage; |
| 38 | |
| 39 | namespace { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 40 | /// \brief The implementation of the coverage tool. |
| 41 | class CodeCoverageTool { |
| 42 | public: |
| 43 | enum Command { |
| 44 | /// \brief The show command. |
| 45 | Show, |
| 46 | /// \brief The report command. |
| 47 | Report |
| 48 | }; |
| 49 | |
| 50 | /// \brief Print the error message to the error output stream. |
| 51 | void error(const Twine &Message, StringRef Whence = ""); |
| 52 | |
| 53 | /// \brief Return a memory buffer for the given source file. |
| 54 | ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile); |
| 55 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 56 | /// \brief Create source views for the expansions of the view. |
| 57 | void attachExpansionSubViews(SourceCoverageView &View, |
| 58 | ArrayRef<ExpansionRecord> Expansions, |
| 59 | CoverageMapping &Coverage); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 60 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 61 | /// \brief Create the source view of a particular function. |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 62 | std::unique_ptr<SourceCoverageView> |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 63 | createFunctionView(const FunctionRecord &Function, CoverageMapping &Coverage); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 64 | |
| 65 | /// \brief Create the main source view of a particular source file. |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 66 | std::unique_ptr<SourceCoverageView> |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 67 | createSourceFileView(StringRef SourceFile, CoverageMapping &Coverage); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 68 | |
| 69 | /// \brief Load the coverage mapping data. Return true if an error occured. |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 70 | std::unique_ptr<CoverageMapping> load(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 71 | |
| 72 | int run(Command Cmd, int argc, const char **argv); |
| 73 | |
| 74 | typedef std::function<int(int, const char **)> CommandLineParserType; |
| 75 | |
| 76 | int show(int argc, const char **argv, |
| 77 | CommandLineParserType commandLineParser); |
| 78 | |
| 79 | int report(int argc, const char **argv, |
| 80 | CommandLineParserType commandLineParser); |
| 81 | |
Justin Bogner | f6c5055 | 2014-10-30 20:51:24 +0000 | [diff] [blame] | 82 | std::string ObjectFilename; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 83 | CoverageViewOptions ViewOpts; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 84 | std::string PGOFilename; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 85 | CoverageFiltersMatchAll Filters; |
| 86 | std::vector<std::string> SourceFiles; |
| 87 | std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>> |
| 88 | LoadedSourceFiles; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 89 | bool CompareFilenamesOnly; |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 90 | StringMap<std::string> RemappedFilenames; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 91 | }; |
| 92 | } |
| 93 | |
| 94 | void CodeCoverageTool::error(const Twine &Message, StringRef Whence) { |
| 95 | errs() << "error: "; |
| 96 | if (!Whence.empty()) |
| 97 | errs() << Whence << ": "; |
| 98 | errs() << Message << "\n"; |
| 99 | } |
| 100 | |
| 101 | ErrorOr<const MemoryBuffer &> |
| 102 | CodeCoverageTool::getSourceFile(StringRef SourceFile) { |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 103 | // If we've remapped filenames, look up the real location for this file. |
| 104 | if (!RemappedFilenames.empty()) { |
| 105 | auto Loc = RemappedFilenames.find(SourceFile); |
| 106 | if (Loc != RemappedFilenames.end()) |
| 107 | SourceFile = Loc->second; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 108 | } |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 109 | for (const auto &Files : LoadedSourceFiles) |
| 110 | if (sys::fs::equivalent(SourceFile, Files.first)) |
| 111 | return *Files.second; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 112 | auto Buffer = MemoryBuffer::getFile(SourceFile); |
| 113 | if (auto EC = Buffer.getError()) { |
| 114 | error(EC.message(), SourceFile); |
| 115 | return EC; |
| 116 | } |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 117 | LoadedSourceFiles.push_back( |
| 118 | std::make_pair(SourceFile, std::move(Buffer.get()))); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 119 | return *LoadedSourceFiles.back().second; |
| 120 | } |
| 121 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 122 | void |
| 123 | CodeCoverageTool::attachExpansionSubViews(SourceCoverageView &View, |
| 124 | ArrayRef<ExpansionRecord> Expansions, |
| 125 | CoverageMapping &Coverage) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 126 | if (!ViewOpts.ShowExpandedRegions) |
| 127 | return; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 128 | for (const auto &Expansion : Expansions) { |
| 129 | auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); |
| 130 | if (ExpansionCoverage.empty()) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 131 | continue; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 132 | auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename()); |
| 133 | if (!SourceBuffer) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 134 | continue; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 135 | |
| 136 | auto SubViewExpansions = ExpansionCoverage.getExpansions(); |
| 137 | auto SubView = llvm::make_unique<SourceCoverageView>( |
| 138 | SourceBuffer.get(), ViewOpts, std::move(ExpansionCoverage)); |
| 139 | attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); |
| 140 | View.addExpansion(Expansion.Region, std::move(SubView)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 144 | std::unique_ptr<SourceCoverageView> |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 145 | CodeCoverageTool::createFunctionView(const FunctionRecord &Function, |
| 146 | CoverageMapping &Coverage) { |
| 147 | auto FunctionCoverage = Coverage.getCoverageForFunction(Function); |
| 148 | if (FunctionCoverage.empty()) |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 149 | return nullptr; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 150 | auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename()); |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 151 | if (!SourceBuffer) |
| 152 | return nullptr; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 153 | |
| 154 | auto Expansions = FunctionCoverage.getExpansions(); |
| 155 | auto View = llvm::make_unique<SourceCoverageView>( |
| 156 | SourceBuffer.get(), ViewOpts, std::move(FunctionCoverage)); |
| 157 | attachExpansionSubViews(*View, Expansions, Coverage); |
| 158 | |
| 159 | return View; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 162 | std::unique_ptr<SourceCoverageView> |
| 163 | CodeCoverageTool::createSourceFileView(StringRef SourceFile, |
| 164 | CoverageMapping &Coverage) { |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 165 | auto SourceBuffer = getSourceFile(SourceFile); |
| 166 | if (!SourceBuffer) |
| 167 | return nullptr; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 168 | auto FileCoverage = Coverage.getCoverageForFile(SourceFile); |
| 169 | if (FileCoverage.empty()) |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 170 | return nullptr; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 171 | |
| 172 | auto Expansions = FileCoverage.getExpansions(); |
| 173 | auto View = llvm::make_unique<SourceCoverageView>( |
| 174 | SourceBuffer.get(), ViewOpts, std::move(FileCoverage)); |
| 175 | attachExpansionSubViews(*View, Expansions, Coverage); |
| 176 | |
| 177 | for (auto Function : Coverage.getInstantiations(SourceFile)) { |
| 178 | auto SubViewCoverage = Coverage.getCoverageForFunction(*Function); |
| 179 | auto SubViewExpansions = SubViewCoverage.getExpansions(); |
| 180 | auto SubView = llvm::make_unique<SourceCoverageView>( |
| 181 | SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage)); |
| 182 | attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); |
| 183 | |
| 184 | if (SubView) { |
Justin Bogner | 5e1400a | 2014-09-17 05:33:20 +0000 | [diff] [blame] | 185 | unsigned FileID = Function->CountedRegions.front().FileID; |
| 186 | unsigned Line = 0; |
| 187 | for (const auto &CR : Function->CountedRegions) |
| 188 | if (CR.FileID == FileID) |
| 189 | Line = std::max(CR.LineEnd, Line); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 190 | View->addInstantiation(Function->Name, Line, std::move(SubView)); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 193 | return View; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 196 | std::unique_ptr<CoverageMapping> CodeCoverageTool::load() { |
Justin Bogner | 19a93ba | 2014-09-20 17:19:52 +0000 | [diff] [blame] | 197 | auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 198 | if (std::error_code EC = CoverageOrErr.getError()) { |
| 199 | colored_ostream(errs(), raw_ostream::RED) |
| 200 | << "error: Failed to load coverage: " << EC.message(); |
| 201 | errs() << "\n"; |
| 202 | return nullptr; |
| 203 | } |
| 204 | auto Coverage = std::move(CoverageOrErr.get()); |
| 205 | unsigned Mismatched = Coverage->getMismatchedCount(); |
| 206 | if (Mismatched) { |
| 207 | colored_ostream(errs(), raw_ostream::RED) |
| 208 | << "warning: " << Mismatched << " functions have mismatched data. "; |
| 209 | errs() << "\n"; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 210 | } |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 211 | |
| 212 | if (CompareFilenamesOnly) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 213 | auto CoveredFiles = Coverage.get()->getUniqueSourceFiles(); |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 214 | for (auto &SF : SourceFiles) { |
| 215 | StringRef SFBase = sys::path::filename(SF); |
| 216 | for (const auto &CF : CoveredFiles) |
| 217 | if (SFBase == sys::path::filename(CF)) { |
| 218 | RemappedFilenames[CF] = SF; |
| 219 | SF = CF; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 225 | return Coverage; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { |
| 229 | // Print a stack trace if we signal out. |
| 230 | sys::PrintStackTraceOnErrorSignal(); |
| 231 | PrettyStackTraceProgram X(argc, argv); |
| 232 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 233 | |
Justin Bogner | f6c5055 | 2014-10-30 20:51:24 +0000 | [diff] [blame] | 234 | cl::opt<std::string, true> ObjectFilename( |
| 235 | cl::Positional, cl::Required, cl::location(this->ObjectFilename), |
| 236 | cl::desc("Covered executable or object file.")); |
| 237 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 238 | cl::list<std::string> InputSourceFiles( |
| 239 | cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore); |
| 240 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 241 | cl::opt<std::string, true> PGOFilename( |
| 242 | "instr-profile", cl::Required, cl::location(this->PGOFilename), |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 243 | cl::desc( |
| 244 | "File with the profile data obtained after an instrumented run")); |
| 245 | |
| 246 | cl::opt<bool> DebugDump("dump", cl::Optional, |
| 247 | cl::desc("Show internal debug dump")); |
| 248 | |
| 249 | cl::opt<bool> FilenameEquivalence( |
| 250 | "filename-equivalence", cl::Optional, |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 251 | cl::desc("Treat source files as equivalent to paths in the coverage data " |
| 252 | "when the file names match, even if the full paths do not")); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 253 | |
| 254 | cl::OptionCategory FilteringCategory("Function filtering options"); |
| 255 | |
| 256 | cl::list<std::string> NameFilters( |
| 257 | "name", cl::Optional, |
| 258 | cl::desc("Show code coverage only for functions with the given name"), |
| 259 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 260 | |
| 261 | cl::list<std::string> NameRegexFilters( |
| 262 | "name-regex", cl::Optional, |
| 263 | cl::desc("Show code coverage only for functions that match the given " |
| 264 | "regular expression"), |
| 265 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 266 | |
| 267 | cl::opt<double> RegionCoverageLtFilter( |
| 268 | "region-coverage-lt", cl::Optional, |
| 269 | cl::desc("Show code coverage only for functions with region coverage " |
| 270 | "less than the given threshold"), |
| 271 | cl::cat(FilteringCategory)); |
| 272 | |
| 273 | cl::opt<double> RegionCoverageGtFilter( |
| 274 | "region-coverage-gt", cl::Optional, |
| 275 | cl::desc("Show code coverage only for functions with region coverage " |
| 276 | "greater than the given threshold"), |
| 277 | cl::cat(FilteringCategory)); |
| 278 | |
| 279 | cl::opt<double> LineCoverageLtFilter( |
| 280 | "line-coverage-lt", cl::Optional, |
| 281 | cl::desc("Show code coverage only for functions with line coverage less " |
| 282 | "than the given threshold"), |
| 283 | cl::cat(FilteringCategory)); |
| 284 | |
| 285 | cl::opt<double> LineCoverageGtFilter( |
| 286 | "line-coverage-gt", cl::Optional, |
| 287 | cl::desc("Show code coverage only for functions with line coverage " |
| 288 | "greater than the given threshold"), |
| 289 | cl::cat(FilteringCategory)); |
| 290 | |
| 291 | auto commandLineParser = [&, this](int argc, const char **argv) -> int { |
| 292 | cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); |
| 293 | ViewOpts.Debug = DebugDump; |
| 294 | CompareFilenamesOnly = FilenameEquivalence; |
| 295 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 296 | // Create the function filters |
| 297 | if (!NameFilters.empty() || !NameRegexFilters.empty()) { |
| 298 | auto NameFilterer = new CoverageFilters; |
| 299 | for (const auto &Name : NameFilters) |
| 300 | NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name)); |
| 301 | for (const auto &Regex : NameRegexFilters) |
| 302 | NameFilterer->push_back( |
| 303 | llvm::make_unique<NameRegexCoverageFilter>(Regex)); |
| 304 | Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer)); |
| 305 | } |
| 306 | if (RegionCoverageLtFilter.getNumOccurrences() || |
| 307 | RegionCoverageGtFilter.getNumOccurrences() || |
| 308 | LineCoverageLtFilter.getNumOccurrences() || |
| 309 | LineCoverageGtFilter.getNumOccurrences()) { |
| 310 | auto StatFilterer = new CoverageFilters; |
| 311 | if (RegionCoverageLtFilter.getNumOccurrences()) |
| 312 | StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>( |
| 313 | RegionCoverageFilter::LessThan, RegionCoverageLtFilter)); |
| 314 | if (RegionCoverageGtFilter.getNumOccurrences()) |
| 315 | StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>( |
| 316 | RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter)); |
| 317 | if (LineCoverageLtFilter.getNumOccurrences()) |
| 318 | StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>( |
| 319 | LineCoverageFilter::LessThan, LineCoverageLtFilter)); |
| 320 | if (LineCoverageGtFilter.getNumOccurrences()) |
| 321 | StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>( |
| 322 | RegionCoverageFilter::GreaterThan, LineCoverageGtFilter)); |
| 323 | Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer)); |
| 324 | } |
| 325 | |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 326 | for (const auto &File : InputSourceFiles) { |
| 327 | SmallString<128> Path(File); |
| 328 | if (std::error_code EC = sys::fs::make_absolute(Path)) { |
| 329 | errs() << "error: " << File << ": " << EC.message(); |
| 330 | return 1; |
| 331 | } |
| 332 | SourceFiles.push_back(Path.str()); |
| 333 | } |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 334 | return 0; |
| 335 | }; |
| 336 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 337 | switch (Cmd) { |
| 338 | case Show: |
| 339 | return show(argc, argv, commandLineParser); |
| 340 | case Report: |
| 341 | return report(argc, argv, commandLineParser); |
| 342 | } |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | int CodeCoverageTool::show(int argc, const char **argv, |
| 347 | CommandLineParserType commandLineParser) { |
| 348 | |
| 349 | cl::OptionCategory ViewCategory("Viewing options"); |
| 350 | |
| 351 | cl::opt<bool> ShowLineExecutionCounts( |
| 352 | "show-line-counts", cl::Optional, |
| 353 | cl::desc("Show the execution counts for each line"), cl::init(true), |
| 354 | cl::cat(ViewCategory)); |
| 355 | |
| 356 | cl::opt<bool> ShowRegions( |
| 357 | "show-regions", cl::Optional, |
| 358 | cl::desc("Show the execution counts for each region"), |
| 359 | cl::cat(ViewCategory)); |
| 360 | |
| 361 | cl::opt<bool> ShowBestLineRegionsCounts( |
| 362 | "show-line-counts-or-regions", cl::Optional, |
| 363 | cl::desc("Show the execution counts for each line, or the execution " |
| 364 | "counts for each region on lines that have multiple regions"), |
| 365 | cl::cat(ViewCategory)); |
| 366 | |
| 367 | cl::opt<bool> ShowExpansions("show-expansions", cl::Optional, |
| 368 | cl::desc("Show expanded source regions"), |
| 369 | cl::cat(ViewCategory)); |
| 370 | |
| 371 | cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional, |
| 372 | cl::desc("Show function instantiations"), |
| 373 | cl::cat(ViewCategory)); |
| 374 | |
| 375 | cl::opt<bool> NoColors("no-colors", cl::Optional, |
| 376 | cl::desc("Don't show text colors"), cl::init(false), |
| 377 | cl::cat(ViewCategory)); |
| 378 | |
| 379 | auto Err = commandLineParser(argc, argv); |
| 380 | if (Err) |
| 381 | return Err; |
| 382 | |
| 383 | ViewOpts.Colors = !NoColors; |
| 384 | ViewOpts.ShowLineNumbers = true; |
| 385 | ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 || |
| 386 | !ShowRegions || ShowBestLineRegionsCounts; |
| 387 | ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts; |
| 388 | ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts; |
| 389 | ViewOpts.ShowExpandedRegions = ShowExpansions; |
| 390 | ViewOpts.ShowFunctionInstantiations = ShowInstantiations; |
| 391 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 392 | auto Coverage = load(); |
| 393 | if (!Coverage) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 394 | return 1; |
| 395 | |
| 396 | if (!Filters.empty()) { |
| 397 | // Show functions |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 398 | for (const auto &Function : Coverage->getCoveredFunctions()) { |
| 399 | if (!Filters.matches(Function)) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 400 | continue; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 401 | |
| 402 | auto mainView = createFunctionView(Function, *Coverage); |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 403 | if (!mainView) { |
| 404 | ViewOpts.colored_ostream(outs(), raw_ostream::RED) |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 405 | << "warning: Could not read coverage for '" << Function.Name; |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 406 | outs() << "\n"; |
| 407 | continue; |
| 408 | } |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 409 | ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name |
| 410 | << ":"; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 411 | outs() << "\n"; |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 412 | mainView->render(outs(), /*WholeFile=*/false); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 413 | outs() << "\n"; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | // Show files |
| 419 | bool ShowFilenames = SourceFiles.size() != 1; |
| 420 | |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 421 | if (SourceFiles.empty()) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 422 | // Get the source files from the function coverage mapping |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 423 | for (StringRef Filename : Coverage->getUniqueSourceFiles()) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 424 | SourceFiles.push_back(Filename); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 425 | |
| 426 | for (const auto &SourceFile : SourceFiles) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 427 | auto mainView = createSourceFileView(SourceFile, *Coverage); |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 428 | if (!mainView) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 429 | ViewOpts.colored_ostream(outs(), raw_ostream::RED) |
| 430 | << "warning: The file '" << SourceFile << "' isn't covered."; |
| 431 | outs() << "\n"; |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | if (ShowFilenames) { |
| 436 | ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":"; |
| 437 | outs() << "\n"; |
| 438 | } |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 439 | mainView->render(outs(), /*Wholefile=*/true); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 440 | if (SourceFiles.size() > 1) |
| 441 | outs() << "\n"; |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | int CodeCoverageTool::report(int argc, const char **argv, |
| 448 | CommandLineParserType commandLineParser) { |
| 449 | cl::opt<bool> NoColors("no-colors", cl::Optional, |
| 450 | cl::desc("Don't show text colors"), cl::init(false)); |
| 451 | |
| 452 | auto Err = commandLineParser(argc, argv); |
| 453 | if (Err) |
| 454 | return Err; |
| 455 | |
| 456 | ViewOpts.Colors = !NoColors; |
| 457 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 458 | auto Coverage = load(); |
| 459 | if (!Coverage) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 460 | return 1; |
| 461 | |
| 462 | CoverageSummary Summarizer; |
Justin Bogner | d5fca92 | 2014-11-14 01:50:32 +0000 | [diff] [blame] | 463 | Summarizer.createSummaries(*Coverage); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 464 | CoverageReport Report(ViewOpts, Summarizer); |
| 465 | if (SourceFiles.empty() && Filters.empty()) { |
| 466 | Report.renderFileReports(llvm::outs()); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | Report.renderFunctionReports(llvm::outs()); |
| 471 | return 0; |
| 472 | } |
| 473 | |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 474 | int showMain(int argc, const char *argv[]) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 475 | CodeCoverageTool Tool; |
| 476 | return Tool.run(CodeCoverageTool::Show, argc, argv); |
| 477 | } |
| 478 | |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 479 | int reportMain(int argc, const char *argv[]) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 480 | CodeCoverageTool Tool; |
| 481 | return Tool.run(CodeCoverageTool::Report, argc, argv); |
| 482 | } |