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