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 | |
| 84 | StringRef ObjectFilename; |
| 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() { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 199 | auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename); |
| 200 | if (auto EC = CounterMappingBuff.getError()) { |
| 201 | error(EC.message(), ObjectFilename); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 202 | return nullptr; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 203 | } |
| 204 | ObjectFileCoverageMappingReader MappingReader(CounterMappingBuff.get()); |
| 205 | if (auto EC = MappingReader.readHeader()) { |
| 206 | error(EC.message(), ObjectFilename); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 207 | return nullptr; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 210 | std::unique_ptr<IndexedInstrProfReader> PGOReader; |
| 211 | if (auto EC = IndexedInstrProfReader::create(PGOFilename, PGOReader)) { |
| 212 | error(EC.message(), PGOFilename); |
| 213 | return nullptr; |
| 214 | } |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 215 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 216 | auto CoverageOrErr = CoverageMapping::load(MappingReader, *PGOReader); |
| 217 | if (std::error_code EC = CoverageOrErr.getError()) { |
| 218 | colored_ostream(errs(), raw_ostream::RED) |
| 219 | << "error: Failed to load coverage: " << EC.message(); |
| 220 | errs() << "\n"; |
| 221 | return nullptr; |
| 222 | } |
| 223 | auto Coverage = std::move(CoverageOrErr.get()); |
| 224 | unsigned Mismatched = Coverage->getMismatchedCount(); |
| 225 | if (Mismatched) { |
| 226 | colored_ostream(errs(), raw_ostream::RED) |
| 227 | << "warning: " << Mismatched << " functions have mismatched data. "; |
| 228 | errs() << "\n"; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 229 | } |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 230 | |
| 231 | if (CompareFilenamesOnly) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 232 | auto CoveredFiles = Coverage.get()->getUniqueSourceFiles(); |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 233 | for (auto &SF : SourceFiles) { |
| 234 | StringRef SFBase = sys::path::filename(SF); |
| 235 | for (const auto &CF : CoveredFiles) |
| 236 | if (SFBase == sys::path::filename(CF)) { |
| 237 | RemappedFilenames[CF] = SF; |
| 238 | SF = CF; |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 244 | return Coverage; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) { |
| 248 | // Print a stack trace if we signal out. |
| 249 | sys::PrintStackTraceOnErrorSignal(); |
| 250 | PrettyStackTraceProgram X(argc, argv); |
| 251 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 252 | |
| 253 | cl::list<std::string> InputSourceFiles( |
| 254 | cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore); |
| 255 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 256 | cl::opt<std::string, true> PGOFilename( |
| 257 | "instr-profile", cl::Required, cl::location(this->PGOFilename), |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 258 | cl::desc( |
| 259 | "File with the profile data obtained after an instrumented run")); |
| 260 | |
| 261 | cl::opt<bool> DebugDump("dump", cl::Optional, |
| 262 | cl::desc("Show internal debug dump")); |
| 263 | |
| 264 | cl::opt<bool> FilenameEquivalence( |
| 265 | "filename-equivalence", cl::Optional, |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 266 | cl::desc("Treat source files as equivalent to paths in the coverage data " |
| 267 | "when the file names match, even if the full paths do not")); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 268 | |
| 269 | cl::OptionCategory FilteringCategory("Function filtering options"); |
| 270 | |
| 271 | cl::list<std::string> NameFilters( |
| 272 | "name", cl::Optional, |
| 273 | cl::desc("Show code coverage only for functions with the given name"), |
| 274 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 275 | |
| 276 | cl::list<std::string> NameRegexFilters( |
| 277 | "name-regex", cl::Optional, |
| 278 | cl::desc("Show code coverage only for functions that match the given " |
| 279 | "regular expression"), |
| 280 | cl::ZeroOrMore, cl::cat(FilteringCategory)); |
| 281 | |
| 282 | cl::opt<double> RegionCoverageLtFilter( |
| 283 | "region-coverage-lt", cl::Optional, |
| 284 | cl::desc("Show code coverage only for functions with region coverage " |
| 285 | "less than the given threshold"), |
| 286 | cl::cat(FilteringCategory)); |
| 287 | |
| 288 | cl::opt<double> RegionCoverageGtFilter( |
| 289 | "region-coverage-gt", cl::Optional, |
| 290 | cl::desc("Show code coverage only for functions with region coverage " |
| 291 | "greater than the given threshold"), |
| 292 | cl::cat(FilteringCategory)); |
| 293 | |
| 294 | cl::opt<double> LineCoverageLtFilter( |
| 295 | "line-coverage-lt", cl::Optional, |
| 296 | cl::desc("Show code coverage only for functions with line coverage less " |
| 297 | "than the given threshold"), |
| 298 | cl::cat(FilteringCategory)); |
| 299 | |
| 300 | cl::opt<double> LineCoverageGtFilter( |
| 301 | "line-coverage-gt", cl::Optional, |
| 302 | cl::desc("Show code coverage only for functions with line coverage " |
| 303 | "greater than the given threshold"), |
| 304 | cl::cat(FilteringCategory)); |
| 305 | |
| 306 | auto commandLineParser = [&, this](int argc, const char **argv) -> int { |
| 307 | cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); |
| 308 | ViewOpts.Debug = DebugDump; |
| 309 | CompareFilenamesOnly = FilenameEquivalence; |
| 310 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 311 | // Create the function filters |
| 312 | if (!NameFilters.empty() || !NameRegexFilters.empty()) { |
| 313 | auto NameFilterer = new CoverageFilters; |
| 314 | for (const auto &Name : NameFilters) |
| 315 | NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name)); |
| 316 | for (const auto &Regex : NameRegexFilters) |
| 317 | NameFilterer->push_back( |
| 318 | llvm::make_unique<NameRegexCoverageFilter>(Regex)); |
| 319 | Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer)); |
| 320 | } |
| 321 | if (RegionCoverageLtFilter.getNumOccurrences() || |
| 322 | RegionCoverageGtFilter.getNumOccurrences() || |
| 323 | LineCoverageLtFilter.getNumOccurrences() || |
| 324 | LineCoverageGtFilter.getNumOccurrences()) { |
| 325 | auto StatFilterer = new CoverageFilters; |
| 326 | if (RegionCoverageLtFilter.getNumOccurrences()) |
| 327 | StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>( |
| 328 | RegionCoverageFilter::LessThan, RegionCoverageLtFilter)); |
| 329 | if (RegionCoverageGtFilter.getNumOccurrences()) |
| 330 | StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>( |
| 331 | RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter)); |
| 332 | if (LineCoverageLtFilter.getNumOccurrences()) |
| 333 | StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>( |
| 334 | LineCoverageFilter::LessThan, LineCoverageLtFilter)); |
| 335 | if (LineCoverageGtFilter.getNumOccurrences()) |
| 336 | StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>( |
| 337 | RegionCoverageFilter::GreaterThan, LineCoverageGtFilter)); |
| 338 | Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer)); |
| 339 | } |
| 340 | |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 341 | for (const auto &File : InputSourceFiles) { |
| 342 | SmallString<128> Path(File); |
| 343 | if (std::error_code EC = sys::fs::make_absolute(Path)) { |
| 344 | errs() << "error: " << File << ": " << EC.message(); |
| 345 | return 1; |
| 346 | } |
| 347 | SourceFiles.push_back(Path.str()); |
| 348 | } |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 349 | return 0; |
| 350 | }; |
| 351 | |
| 352 | // Parse the object filename |
| 353 | if (argc > 1) { |
| 354 | StringRef Arg(argv[1]); |
| 355 | if (Arg.equals_lower("-help") || Arg.equals_lower("-version")) { |
| 356 | cl::ParseCommandLineOptions(2, argv, "LLVM code coverage tool\n"); |
| 357 | return 0; |
| 358 | } |
| 359 | ObjectFilename = Arg; |
| 360 | |
| 361 | argv[1] = argv[0]; |
| 362 | --argc; |
| 363 | ++argv; |
| 364 | } else { |
| 365 | errs() << sys::path::filename(argv[0]) << ": No executable file given!\n"; |
| 366 | return 1; |
| 367 | } |
| 368 | |
| 369 | switch (Cmd) { |
| 370 | case Show: |
| 371 | return show(argc, argv, commandLineParser); |
| 372 | case Report: |
| 373 | return report(argc, argv, commandLineParser); |
| 374 | } |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | int CodeCoverageTool::show(int argc, const char **argv, |
| 379 | CommandLineParserType commandLineParser) { |
| 380 | |
| 381 | cl::OptionCategory ViewCategory("Viewing options"); |
| 382 | |
| 383 | cl::opt<bool> ShowLineExecutionCounts( |
| 384 | "show-line-counts", cl::Optional, |
| 385 | cl::desc("Show the execution counts for each line"), cl::init(true), |
| 386 | cl::cat(ViewCategory)); |
| 387 | |
| 388 | cl::opt<bool> ShowRegions( |
| 389 | "show-regions", cl::Optional, |
| 390 | cl::desc("Show the execution counts for each region"), |
| 391 | cl::cat(ViewCategory)); |
| 392 | |
| 393 | cl::opt<bool> ShowBestLineRegionsCounts( |
| 394 | "show-line-counts-or-regions", cl::Optional, |
| 395 | cl::desc("Show the execution counts for each line, or the execution " |
| 396 | "counts for each region on lines that have multiple regions"), |
| 397 | cl::cat(ViewCategory)); |
| 398 | |
| 399 | cl::opt<bool> ShowExpansions("show-expansions", cl::Optional, |
| 400 | cl::desc("Show expanded source regions"), |
| 401 | cl::cat(ViewCategory)); |
| 402 | |
| 403 | cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional, |
| 404 | cl::desc("Show function instantiations"), |
| 405 | cl::cat(ViewCategory)); |
| 406 | |
| 407 | cl::opt<bool> NoColors("no-colors", cl::Optional, |
| 408 | cl::desc("Don't show text colors"), cl::init(false), |
| 409 | cl::cat(ViewCategory)); |
| 410 | |
| 411 | auto Err = commandLineParser(argc, argv); |
| 412 | if (Err) |
| 413 | return Err; |
| 414 | |
| 415 | ViewOpts.Colors = !NoColors; |
| 416 | ViewOpts.ShowLineNumbers = true; |
| 417 | ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 || |
| 418 | !ShowRegions || ShowBestLineRegionsCounts; |
| 419 | ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts; |
| 420 | ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts; |
| 421 | ViewOpts.ShowExpandedRegions = ShowExpansions; |
| 422 | ViewOpts.ShowFunctionInstantiations = ShowInstantiations; |
| 423 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 424 | auto Coverage = load(); |
| 425 | if (!Coverage) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 426 | return 1; |
| 427 | |
| 428 | if (!Filters.empty()) { |
| 429 | // Show functions |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 430 | for (const auto &Function : Coverage->getCoveredFunctions()) { |
| 431 | if (!Filters.matches(Function)) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 432 | continue; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 433 | |
| 434 | auto mainView = createFunctionView(Function, *Coverage); |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 435 | if (!mainView) { |
| 436 | ViewOpts.colored_ostream(outs(), raw_ostream::RED) |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 437 | << "warning: Could not read coverage for '" << Function.Name; |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 438 | outs() << "\n"; |
| 439 | continue; |
| 440 | } |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 441 | ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << Function.Name |
| 442 | << ":"; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 443 | outs() << "\n"; |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 444 | mainView->render(outs(), /*WholeFile=*/false); |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 445 | outs() << "\n"; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | // Show files |
| 451 | bool ShowFilenames = SourceFiles.size() != 1; |
| 452 | |
Justin Bogner | 116c166 | 2014-09-19 08:13:12 +0000 | [diff] [blame] | 453 | if (SourceFiles.empty()) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 454 | // Get the source files from the function coverage mapping |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 455 | for (StringRef Filename : Coverage->getUniqueSourceFiles()) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 456 | SourceFiles.push_back(Filename); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 457 | |
| 458 | for (const auto &SourceFile : SourceFiles) { |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 459 | auto mainView = createSourceFileView(SourceFile, *Coverage); |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 460 | if (!mainView) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 461 | ViewOpts.colored_ostream(outs(), raw_ostream::RED) |
| 462 | << "warning: The file '" << SourceFile << "' isn't covered."; |
| 463 | outs() << "\n"; |
| 464 | continue; |
| 465 | } |
| 466 | |
| 467 | if (ShowFilenames) { |
| 468 | ViewOpts.colored_ostream(outs(), raw_ostream::CYAN) << SourceFile << ":"; |
| 469 | outs() << "\n"; |
| 470 | } |
Justin Bogner | 5a6edad | 2014-09-19 19:07:17 +0000 | [diff] [blame] | 471 | mainView->render(outs(), /*Wholefile=*/true); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 472 | if (SourceFiles.size() > 1) |
| 473 | outs() << "\n"; |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | int CodeCoverageTool::report(int argc, const char **argv, |
| 480 | CommandLineParserType commandLineParser) { |
| 481 | cl::opt<bool> NoColors("no-colors", cl::Optional, |
| 482 | cl::desc("Don't show text colors"), cl::init(false)); |
| 483 | |
| 484 | auto Err = commandLineParser(argc, argv); |
| 485 | if (Err) |
| 486 | return Err; |
| 487 | |
| 488 | ViewOpts.Colors = !NoColors; |
| 489 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 490 | auto Coverage = load(); |
| 491 | if (!Coverage) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 492 | return 1; |
| 493 | |
| 494 | CoverageSummary Summarizer; |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame^] | 495 | Summarizer.createSummaries(Coverage->getCoveredFunctions()); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 496 | CoverageReport Report(ViewOpts, Summarizer); |
| 497 | if (SourceFiles.empty() && Filters.empty()) { |
| 498 | Report.renderFileReports(llvm::outs()); |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | Report.renderFunctionReports(llvm::outs()); |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | int show_main(int argc, const char **argv) { |
| 507 | CodeCoverageTool Tool; |
| 508 | return Tool.run(CodeCoverageTool::Show, argc, argv); |
| 509 | } |
| 510 | |
| 511 | int report_main(int argc, const char **argv) { |
| 512 | CodeCoverageTool Tool; |
| 513 | return Tool.run(CodeCoverageTool::Report, argc, argv); |
| 514 | } |