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