CodeCoverage.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. #include "CoverageFilters.h"
  16. #include "CoverageReport.h"
  17. #include "CoverageSummaryInfo.h"
  18. #include "CoverageViewOptions.h"
  19. #include "RenderingSupport.h"
  20. #include "SourceCoverageView.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/ADT/Triple.h"
  24. #include "llvm/ProfileData/Coverage/CoverageMapping.h"
  25. #include "llvm/ProfileData/InstrProfReader.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Support/FileSystem.h"
  28. #include "llvm/Support/Format.h"
  29. #include "llvm/Support/MemoryBuffer.h"
  30. #include "llvm/Support/Path.h"
  31. #include "llvm/Support/Process.h"
  32. #include "llvm/Support/Program.h"
  33. #include "llvm/Support/ScopedPrinter.h"
  34. #include "llvm/Support/Threading.h"
  35. #include "llvm/Support/ThreadPool.h"
  36. #include "llvm/Support/ToolOutputFile.h"
  37. #include <functional>
  38. #include <system_error>
  39. using namespace llvm;
  40. using namespace coverage;
  41. void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping,
  42. raw_ostream &OS);
  43. namespace {
  44. /// \brief The implementation of the coverage tool.
  45. class CodeCoverageTool {
  46. public:
  47. enum Command {
  48. /// \brief The show command.
  49. Show,
  50. /// \brief The report command.
  51. Report,
  52. /// \brief The export command.
  53. Export
  54. };
  55. int run(Command Cmd, int argc, const char **argv);
  56. private:
  57. /// \brief Print the error message to the error output stream.
  58. void error(const Twine &Message, StringRef Whence = "");
  59. /// \brief Print the warning message to the error output stream.
  60. void warning(const Twine &Message, StringRef Whence = "");
  61. /// \brief Convert \p Path into an absolute path and append it to the list
  62. /// of collected paths.
  63. void addCollectedPath(const std::string &Path);
  64. /// \brief If \p Path is a regular file, collect the path. If it's a
  65. /// directory, recursively collect all of the paths within the directory.
  66. void collectPaths(const std::string &Path);
  67. /// \brief Return a memory buffer for the given source file.
  68. ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
  69. /// \brief Create source views for the expansions of the view.
  70. void attachExpansionSubViews(SourceCoverageView &View,
  71. ArrayRef<ExpansionRecord> Expansions,
  72. const CoverageMapping &Coverage);
  73. /// \brief Create the source view of a particular function.
  74. std::unique_ptr<SourceCoverageView>
  75. createFunctionView(const FunctionRecord &Function,
  76. const CoverageMapping &Coverage);
  77. /// \brief Create the main source view of a particular source file.
  78. std::unique_ptr<SourceCoverageView>
  79. createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
  80. /// \brief Load the coverage mapping data. Return nullptr if an error occurred.
  81. std::unique_ptr<CoverageMapping> load();
  82. /// \brief Remove input source files which aren't mapped by \p Coverage.
  83. void removeUnmappedInputs(const CoverageMapping &Coverage);
  84. /// \brief If a demangler is available, demangle all symbol names.
  85. void demangleSymbols(const CoverageMapping &Coverage);
  86. /// \brief Write out a source file view to the filesystem.
  87. void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
  88. CoveragePrinter *Printer, bool ShowFilenames);
  89. typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
  90. int show(int argc, const char **argv,
  91. CommandLineParserType commandLineParser);
  92. int report(int argc, const char **argv,
  93. CommandLineParserType commandLineParser);
  94. int export_(int argc, const char **argv,
  95. CommandLineParserType commandLineParser);
  96. std::vector<StringRef> ObjectFilenames;
  97. CoverageViewOptions ViewOpts;
  98. CoverageFiltersMatchAll Filters;
  99. /// The path to the indexed profile.
  100. std::string PGOFilename;
  101. /// A list of input source files.
  102. std::vector<std::string> SourceFiles;
  103. /// Whether or not we're in -filename-equivalence mode.
  104. bool CompareFilenamesOnly;
  105. /// In -filename-equivalence mode, this maps absolute paths from the
  106. /// coverage mapping data to input source files.
  107. StringMap<std::string> RemappedFilenames;
  108. /// The architecture the coverage mapping data targets.
  109. std::vector<StringRef> CoverageArches;
  110. /// A cache for demangled symbols.
  111. DemangleCache DC;
  112. /// A lock which guards printing to stderr.
  113. std::mutex ErrsLock;
  114. /// A container for input source file buffers.
  115. std::mutex LoadedSourceFilesLock;
  116. std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
  117. LoadedSourceFiles;
  118. };
  119. }
  120. static std::string getErrorString(const Twine &Message, StringRef Whence,
  121. bool Warning) {
  122. std::string Str = (Warning ? "warning" : "error");
  123. Str += ": ";
  124. if (!Whence.empty())
  125. Str += Whence.str() + ": ";
  126. Str += Message.str() + "\n";
  127. return Str;
  128. }
  129. void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
  130. std::unique_lock<std::mutex> Guard{ErrsLock};
  131. ViewOpts.colored_ostream(errs(), raw_ostream::RED)
  132. << getErrorString(Message, Whence, false);
  133. }
  134. void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
  135. std::unique_lock<std::mutex> Guard{ErrsLock};
  136. ViewOpts.colored_ostream(errs(), raw_ostream::RED)
  137. << getErrorString(Message, Whence, true);
  138. }
  139. void CodeCoverageTool::addCollectedPath(const std::string &Path) {
  140. if (CompareFilenamesOnly) {
  141. SourceFiles.emplace_back(Path);
  142. } else {
  143. SmallString<128> EffectivePath(Path);
  144. if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
  145. error(EC.message(), Path);
  146. return;
  147. }
  148. sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
  149. SourceFiles.emplace_back(EffectivePath.str());
  150. }
  151. }
  152. void CodeCoverageTool::collectPaths(const std::string &Path) {
  153. llvm::sys::fs::file_status Status;
  154. llvm::sys::fs::status(Path, Status);
  155. if (!llvm::sys::fs::exists(Status)) {
  156. if (CompareFilenamesOnly)
  157. addCollectedPath(Path);
  158. else
  159. error("Missing source file", Path);
  160. return;
  161. }
  162. if (llvm::sys::fs::is_regular_file(Status)) {
  163. addCollectedPath(Path);
  164. return;
  165. }
  166. if (llvm::sys::fs::is_directory(Status)) {
  167. std::error_code EC;
  168. for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
  169. F != E && !EC; F.increment(EC)) {
  170. if (llvm::sys::fs::is_regular_file(F->path()))
  171. addCollectedPath(F->path());
  172. }
  173. if (EC)
  174. warning(EC.message(), Path);
  175. }
  176. }
  177. ErrorOr<const MemoryBuffer &>
  178. CodeCoverageTool::getSourceFile(StringRef SourceFile) {
  179. // If we've remapped filenames, look up the real location for this file.
  180. std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
  181. if (!RemappedFilenames.empty()) {
  182. auto Loc = RemappedFilenames.find(SourceFile);
  183. if (Loc != RemappedFilenames.end())
  184. SourceFile = Loc->second;
  185. }
  186. for (const auto &Files : LoadedSourceFiles)
  187. if (sys::fs::equivalent(SourceFile, Files.first))
  188. return *Files.second;
  189. auto Buffer = MemoryBuffer::getFile(SourceFile);
  190. if (auto EC = Buffer.getError()) {
  191. error(EC.message(), SourceFile);
  192. return EC;
  193. }
  194. LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
  195. return *LoadedSourceFiles.back().second;
  196. }
  197. void CodeCoverageTool::attachExpansionSubViews(
  198. SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
  199. const CoverageMapping &Coverage) {
  200. if (!ViewOpts.ShowExpandedRegions)
  201. return;
  202. for (const auto &Expansion : Expansions) {
  203. auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
  204. if (ExpansionCoverage.empty())
  205. continue;
  206. auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
  207. if (!SourceBuffer)
  208. continue;
  209. auto SubViewExpansions = ExpansionCoverage.getExpansions();
  210. auto SubView =
  211. SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
  212. ViewOpts, std::move(ExpansionCoverage));
  213. attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
  214. View.addExpansion(Expansion.Region, std::move(SubView));
  215. }
  216. }
  217. std::unique_ptr<SourceCoverageView>
  218. CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
  219. const CoverageMapping &Coverage) {
  220. auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
  221. if (FunctionCoverage.empty())
  222. return nullptr;
  223. auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
  224. if (!SourceBuffer)
  225. return nullptr;
  226. auto Expansions = FunctionCoverage.getExpansions();
  227. auto View = SourceCoverageView::create(DC.demangle(Function.Name),
  228. SourceBuffer.get(), ViewOpts,
  229. std::move(FunctionCoverage));
  230. attachExpansionSubViews(*View, Expansions, Coverage);
  231. return View;
  232. }
  233. std::unique_ptr<SourceCoverageView>
  234. CodeCoverageTool::createSourceFileView(StringRef SourceFile,
  235. const CoverageMapping &Coverage) {
  236. auto SourceBuffer = getSourceFile(SourceFile);
  237. if (!SourceBuffer)
  238. return nullptr;
  239. auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
  240. if (FileCoverage.empty())
  241. return nullptr;
  242. auto Expansions = FileCoverage.getExpansions();
  243. auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
  244. ViewOpts, std::move(FileCoverage));
  245. attachExpansionSubViews(*View, Expansions, Coverage);
  246. if (!ViewOpts.ShowFunctionInstantiations)
  247. return View;
  248. for (const auto *Function : Coverage.getInstantiations(SourceFile)) {
  249. std::unique_ptr<SourceCoverageView> SubView{nullptr};
  250. StringRef Funcname = DC.demangle(Function->Name);
  251. if (Function->ExecutionCount > 0) {
  252. auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
  253. auto SubViewExpansions = SubViewCoverage.getExpansions();
  254. SubView = SourceCoverageView::create(
  255. Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
  256. attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
  257. }
  258. unsigned FileID = Function->CountedRegions.front().FileID;
  259. unsigned Line = 0;
  260. for (const auto &CR : Function->CountedRegions)
  261. if (CR.FileID == FileID)
  262. Line = std::max(CR.LineEnd, Line);
  263. View->addInstantiation(Funcname, Line, std::move(SubView));
  264. }
  265. return View;
  266. }
  267. static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
  268. sys::fs::file_status Status;
  269. if (sys::fs::status(LHS, Status))
  270. return false;
  271. auto LHSTime = Status.getLastModificationTime();
  272. if (sys::fs::status(RHS, Status))
  273. return false;
  274. auto RHSTime = Status.getLastModificationTime();
  275. return LHSTime > RHSTime;
  276. }
  277. std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
  278. for (StringRef ObjectFilename : ObjectFilenames)
  279. if (modifiedTimeGT(ObjectFilename, PGOFilename))
  280. warning("profile data may be out of date - object is newer",
  281. ObjectFilename);
  282. auto CoverageOrErr =
  283. CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches);
  284. if (Error E = CoverageOrErr.takeError()) {
  285. error("Failed to load coverage: " + toString(std::move(E)),
  286. join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "));
  287. return nullptr;
  288. }
  289. auto Coverage = std::move(CoverageOrErr.get());
  290. unsigned Mismatched = Coverage->getMismatchedCount();
  291. if (Mismatched)
  292. warning(utostr(Mismatched) + " functions have mismatched data");
  293. if (!SourceFiles.empty())
  294. removeUnmappedInputs(*Coverage);
  295. demangleSymbols(*Coverage);
  296. return Coverage;
  297. }
  298. void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
  299. std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
  300. auto UncoveredFilesIt = SourceFiles.end();
  301. if (!CompareFilenamesOnly) {
  302. // The user may have specified source files which aren't in the coverage
  303. // mapping. Filter these files away.
  304. UncoveredFilesIt = std::remove_if(
  305. SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) {
  306. return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(),
  307. SF);
  308. });
  309. } else {
  310. for (auto &SF : SourceFiles) {
  311. StringRef SFBase = sys::path::filename(SF);
  312. for (const auto &CF : CoveredFiles) {
  313. if (SFBase == sys::path::filename(CF)) {
  314. RemappedFilenames[CF] = SF;
  315. SF = CF;
  316. break;
  317. }
  318. }
  319. }
  320. UncoveredFilesIt = std::remove_if(
  321. SourceFiles.begin(), SourceFiles.end(),
  322. [&](const std::string &SF) { return !RemappedFilenames.count(SF); });
  323. }
  324. SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
  325. }
  326. void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
  327. if (!ViewOpts.hasDemangler())
  328. return;
  329. // Pass function names to the demangler in a temporary file.
  330. int InputFD;
  331. SmallString<256> InputPath;
  332. std::error_code EC =
  333. sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
  334. if (EC) {
  335. error(InputPath, EC.message());
  336. return;
  337. }
  338. tool_output_file InputTOF{InputPath, InputFD};
  339. unsigned NumSymbols = 0;
  340. for (const auto &Function : Coverage.getCoveredFunctions()) {
  341. InputTOF.os() << Function.Name << '\n';
  342. ++NumSymbols;
  343. }
  344. InputTOF.os().close();
  345. // Use another temporary file to store the demangler's output.
  346. int OutputFD;
  347. SmallString<256> OutputPath;
  348. EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
  349. OutputPath);
  350. if (EC) {
  351. error(OutputPath, EC.message());
  352. return;
  353. }
  354. tool_output_file OutputTOF{OutputPath, OutputFD};
  355. OutputTOF.os().close();
  356. // Invoke the demangler.
  357. std::vector<const char *> ArgsV;
  358. for (const std::string &Arg : ViewOpts.DemanglerOpts)
  359. ArgsV.push_back(Arg.c_str());
  360. ArgsV.push_back(nullptr);
  361. StringRef InputPathRef = InputPath.str();
  362. StringRef OutputPathRef = OutputPath.str();
  363. StringRef StderrRef;
  364. const StringRef *Redirects[] = {&InputPathRef, &OutputPathRef, &StderrRef};
  365. std::string ErrMsg;
  366. int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(),
  367. /*env=*/nullptr, Redirects, /*secondsToWait=*/0,
  368. /*memoryLimit=*/0, &ErrMsg);
  369. if (RC) {
  370. error(ErrMsg, ViewOpts.DemanglerOpts[0]);
  371. return;
  372. }
  373. // Parse the demangler's output.
  374. auto BufOrError = MemoryBuffer::getFile(OutputPath);
  375. if (!BufOrError) {
  376. error(OutputPath, BufOrError.getError().message());
  377. return;
  378. }
  379. std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
  380. SmallVector<StringRef, 8> Symbols;
  381. StringRef DemanglerData = DemanglerBuf->getBuffer();
  382. DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
  383. /*KeepEmpty=*/false);
  384. if (Symbols.size() != NumSymbols) {
  385. error("Demangler did not provide expected number of symbols");
  386. return;
  387. }
  388. // Cache the demangled names.
  389. unsigned I = 0;
  390. for (const auto &Function : Coverage.getCoveredFunctions())
  391. // On Windows, lines in the demangler's output file end with "\r\n".
  392. // Splitting by '\n' keeps '\r's, so cut them now.
  393. DC.DemangledNames[Function.Name] = Symbols[I++].rtrim();
  394. }
  395. void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
  396. CoverageMapping *Coverage,
  397. CoveragePrinter *Printer,
  398. bool ShowFilenames) {
  399. auto View = createSourceFileView(SourceFile, *Coverage);
  400. if (!View) {
  401. warning("The file '" + SourceFile + "' isn't covered.");
  402. return;
  403. }
  404. auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
  405. if (Error E = OSOrErr.takeError()) {
  406. error("Could not create view file!", toString(std::move(E)));
  407. return;
  408. }
  409. auto OS = std::move(OSOrErr.get());
  410. View->print(*OS.get(), /*Wholefile=*/true,
  411. /*ShowSourceName=*/ShowFilenames);
  412. Printer->closeViewFile(std::move(OS));
  413. }
  414. int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
  415. cl::opt<std::string> CovFilename(
  416. cl::Positional, cl::desc("Covered executable or object file."));
  417. cl::list<std::string> CovFilenames(
  418. "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore,
  419. cl::CommaSeparated);
  420. cl::list<std::string> InputSourceFiles(
  421. cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
  422. cl::opt<bool> DebugDumpCollectedPaths(
  423. "dump-collected-paths", cl::Optional, cl::Hidden,
  424. cl::desc("Show the collected paths to source files"));
  425. cl::opt<std::string, true> PGOFilename(
  426. "instr-profile", cl::Required, cl::location(this->PGOFilename),
  427. cl::desc(
  428. "File with the profile data obtained after an instrumented run"));
  429. cl::list<std::string> Arches(
  430. "arch", cl::desc("architectures of the coverage mapping binaries"));
  431. cl::opt<bool> DebugDump("dump", cl::Optional,
  432. cl::desc("Show internal debug dump"));
  433. cl::opt<CoverageViewOptions::OutputFormat> Format(
  434. "format", cl::desc("Output format for line-based coverage reports"),
  435. cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
  436. "Text output"),
  437. clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
  438. "HTML output")),
  439. cl::init(CoverageViewOptions::OutputFormat::Text));
  440. cl::opt<bool> FilenameEquivalence(
  441. "filename-equivalence", cl::Optional,
  442. cl::desc("Treat source files as equivalent to paths in the coverage data "
  443. "when the file names match, even if the full paths do not"));
  444. cl::OptionCategory FilteringCategory("Function filtering options");
  445. cl::list<std::string> NameFilters(
  446. "name", cl::Optional,
  447. cl::desc("Show code coverage only for functions with the given name"),
  448. cl::ZeroOrMore, cl::cat(FilteringCategory));
  449. cl::list<std::string> NameRegexFilters(
  450. "name-regex", cl::Optional,
  451. cl::desc("Show code coverage only for functions that match the given "
  452. "regular expression"),
  453. cl::ZeroOrMore, cl::cat(FilteringCategory));
  454. cl::opt<double> RegionCoverageLtFilter(
  455. "region-coverage-lt", cl::Optional,
  456. cl::desc("Show code coverage only for functions with region coverage "
  457. "less than the given threshold"),
  458. cl::cat(FilteringCategory));
  459. cl::opt<double> RegionCoverageGtFilter(
  460. "region-coverage-gt", cl::Optional,
  461. cl::desc("Show code coverage only for functions with region coverage "
  462. "greater than the given threshold"),
  463. cl::cat(FilteringCategory));
  464. cl::opt<double> LineCoverageLtFilter(
  465. "line-coverage-lt", cl::Optional,
  466. cl::desc("Show code coverage only for functions with line coverage less "
  467. "than the given threshold"),
  468. cl::cat(FilteringCategory));
  469. cl::opt<double> LineCoverageGtFilter(
  470. "line-coverage-gt", cl::Optional,
  471. cl::desc("Show code coverage only for functions with line coverage "
  472. "greater than the given threshold"),
  473. cl::cat(FilteringCategory));
  474. cl::opt<cl::boolOrDefault> UseColor(
  475. "use-color", cl::desc("Emit colored output (default=autodetect)"),
  476. cl::init(cl::BOU_UNSET));
  477. cl::list<std::string> DemanglerOpts(
  478. "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
  479. auto commandLineParser = [&, this](int argc, const char **argv) -> int {
  480. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  481. ViewOpts.Debug = DebugDump;
  482. CompareFilenamesOnly = FilenameEquivalence;
  483. if (!CovFilename.empty())
  484. ObjectFilenames.emplace_back(CovFilename);
  485. for (const std::string &Filename : CovFilenames)
  486. ObjectFilenames.emplace_back(Filename);
  487. if (ObjectFilenames.empty()) {
  488. errs() << "No filenames specified!\n";
  489. ::exit(1);
  490. }
  491. ViewOpts.Format = Format;
  492. switch (ViewOpts.Format) {
  493. case CoverageViewOptions::OutputFormat::Text:
  494. ViewOpts.Colors = UseColor == cl::BOU_UNSET
  495. ? sys::Process::StandardOutHasColors()
  496. : UseColor == cl::BOU_TRUE;
  497. break;
  498. case CoverageViewOptions::OutputFormat::HTML:
  499. if (UseColor == cl::BOU_FALSE)
  500. errs() << "Color output cannot be disabled when generating html.\n";
  501. ViewOpts.Colors = true;
  502. break;
  503. }
  504. // If a demangler is supplied, check if it exists and register it.
  505. if (DemanglerOpts.size()) {
  506. auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
  507. if (!DemanglerPathOrErr) {
  508. error("Could not find the demangler!",
  509. DemanglerPathOrErr.getError().message());
  510. return 1;
  511. }
  512. DemanglerOpts[0] = *DemanglerPathOrErr;
  513. ViewOpts.DemanglerOpts.swap(DemanglerOpts);
  514. }
  515. // Create the function filters
  516. if (!NameFilters.empty() || !NameRegexFilters.empty()) {
  517. auto NameFilterer = new CoverageFilters;
  518. for (const auto &Name : NameFilters)
  519. NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
  520. for (const auto &Regex : NameRegexFilters)
  521. NameFilterer->push_back(
  522. llvm::make_unique<NameRegexCoverageFilter>(Regex));
  523. Filters.push_back(std::unique_ptr<CoverageFilter>(NameFilterer));
  524. }
  525. if (RegionCoverageLtFilter.getNumOccurrences() ||
  526. RegionCoverageGtFilter.getNumOccurrences() ||
  527. LineCoverageLtFilter.getNumOccurrences() ||
  528. LineCoverageGtFilter.getNumOccurrences()) {
  529. auto StatFilterer = new CoverageFilters;
  530. if (RegionCoverageLtFilter.getNumOccurrences())
  531. StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
  532. RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
  533. if (RegionCoverageGtFilter.getNumOccurrences())
  534. StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
  535. RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
  536. if (LineCoverageLtFilter.getNumOccurrences())
  537. StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
  538. LineCoverageFilter::LessThan, LineCoverageLtFilter));
  539. if (LineCoverageGtFilter.getNumOccurrences())
  540. StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
  541. RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
  542. Filters.push_back(std::unique_ptr<CoverageFilter>(StatFilterer));
  543. }
  544. if (!Arches.empty()) {
  545. for (const std::string &Arch : Arches) {
  546. if (Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
  547. error("Unknown architecture: " + Arch);
  548. return 1;
  549. }
  550. CoverageArches.emplace_back(Arch);
  551. }
  552. if (CoverageArches.size() != ObjectFilenames.size()) {
  553. error("Number of architectures doesn't match the number of objects");
  554. return 1;
  555. }
  556. }
  557. for (const std::string &File : InputSourceFiles)
  558. collectPaths(File);
  559. if (DebugDumpCollectedPaths) {
  560. for (const std::string &SF : SourceFiles)
  561. outs() << SF << '\n';
  562. ::exit(0);
  563. }
  564. return 0;
  565. };
  566. switch (Cmd) {
  567. case Show:
  568. return show(argc, argv, commandLineParser);
  569. case Report:
  570. return report(argc, argv, commandLineParser);
  571. case Export:
  572. return export_(argc, argv, commandLineParser);
  573. }
  574. return 0;
  575. }
  576. int CodeCoverageTool::show(int argc, const char **argv,
  577. CommandLineParserType commandLineParser) {
  578. cl::OptionCategory ViewCategory("Viewing options");
  579. cl::opt<bool> ShowLineExecutionCounts(
  580. "show-line-counts", cl::Optional,
  581. cl::desc("Show the execution counts for each line"), cl::init(true),
  582. cl::cat(ViewCategory));
  583. cl::opt<bool> ShowRegions(
  584. "show-regions", cl::Optional,
  585. cl::desc("Show the execution counts for each region"),
  586. cl::cat(ViewCategory));
  587. cl::opt<bool> ShowBestLineRegionsCounts(
  588. "show-line-counts-or-regions", cl::Optional,
  589. cl::desc("Show the execution counts for each line, or the execution "
  590. "counts for each region on lines that have multiple regions"),
  591. cl::cat(ViewCategory));
  592. cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
  593. cl::desc("Show expanded source regions"),
  594. cl::cat(ViewCategory));
  595. cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
  596. cl::desc("Show function instantiations"),
  597. cl::init(true), cl::cat(ViewCategory));
  598. cl::opt<std::string> ShowOutputDirectory(
  599. "output-dir", cl::init(""),
  600. cl::desc("Directory in which coverage information is written out"));
  601. cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
  602. cl::aliasopt(ShowOutputDirectory));
  603. cl::opt<uint32_t> TabSize(
  604. "tab-size", cl::init(2),
  605. cl::desc(
  606. "Set tab expansion size for html coverage reports (default = 2)"));
  607. cl::opt<std::string> ProjectTitle(
  608. "project-title", cl::Optional,
  609. cl::desc("Set project title for the coverage report"));
  610. cl::opt<unsigned> NumThreads(
  611. "num-threads", cl::init(0),
  612. cl::desc("Number of merge threads to use (default: autodetect)"));
  613. cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
  614. cl::aliasopt(NumThreads));
  615. auto Err = commandLineParser(argc, argv);
  616. if (Err)
  617. return Err;
  618. ViewOpts.ShowLineNumbers = true;
  619. ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
  620. !ShowRegions || ShowBestLineRegionsCounts;
  621. ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
  622. ViewOpts.ShowLineStatsOrRegionMarkers = ShowBestLineRegionsCounts;
  623. ViewOpts.ShowExpandedRegions = ShowExpansions;
  624. ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
  625. ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
  626. ViewOpts.TabSize = TabSize;
  627. ViewOpts.ProjectTitle = ProjectTitle;
  628. if (ViewOpts.hasOutputDirectory()) {
  629. if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
  630. error("Could not create output directory!", E.message());
  631. return 1;
  632. }
  633. }
  634. sys::fs::file_status Status;
  635. if (sys::fs::status(PGOFilename, Status)) {
  636. error("profdata file error: can not get the file status. \n");
  637. return 1;
  638. }
  639. auto ModifiedTime = Status.getLastModificationTime();
  640. std::string ModifiedTimeStr = to_string(ModifiedTime);
  641. size_t found = ModifiedTimeStr.rfind(':');
  642. ViewOpts.CreatedTimeStr = (found != std::string::npos)
  643. ? "Created: " + ModifiedTimeStr.substr(0, found)
  644. : "Created: " + ModifiedTimeStr;
  645. auto Coverage = load();
  646. if (!Coverage)
  647. return 1;
  648. auto Printer = CoveragePrinter::create(ViewOpts);
  649. if (!Filters.empty()) {
  650. auto OSOrErr = Printer->createViewFile("functions", /*InToplevel=*/true);
  651. if (Error E = OSOrErr.takeError()) {
  652. error("Could not create view file!", toString(std::move(E)));
  653. return 1;
  654. }
  655. auto OS = std::move(OSOrErr.get());
  656. // Show functions.
  657. for (const auto &Function : Coverage->getCoveredFunctions()) {
  658. if (!Filters.matches(Function))
  659. continue;
  660. auto mainView = createFunctionView(Function, *Coverage);
  661. if (!mainView) {
  662. warning("Could not read coverage for '" + Function.Name + "'.");
  663. continue;
  664. }
  665. mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true);
  666. }
  667. Printer->closeViewFile(std::move(OS));
  668. return 0;
  669. }
  670. // Show files
  671. bool ShowFilenames =
  672. (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
  673. (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
  674. if (SourceFiles.empty())
  675. // Get the source files from the function coverage mapping.
  676. for (StringRef Filename : Coverage->getUniqueSourceFiles())
  677. SourceFiles.push_back(Filename);
  678. // Create an index out of the source files.
  679. if (ViewOpts.hasOutputDirectory()) {
  680. if (Error E = Printer->createIndexFile(SourceFiles, *Coverage)) {
  681. error("Could not create index file!", toString(std::move(E)));
  682. return 1;
  683. }
  684. }
  685. // If NumThreads is not specified, auto-detect a good default.
  686. if (NumThreads == 0)
  687. NumThreads =
  688. std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
  689. unsigned(SourceFiles.size())));
  690. if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) {
  691. for (const std::string &SourceFile : SourceFiles)
  692. writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
  693. ShowFilenames);
  694. } else {
  695. // In -output-dir mode, it's safe to use multiple threads to print files.
  696. ThreadPool Pool(NumThreads);
  697. for (const std::string &SourceFile : SourceFiles)
  698. Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
  699. Coverage.get(), Printer.get(), ShowFilenames);
  700. Pool.wait();
  701. }
  702. return 0;
  703. }
  704. int CodeCoverageTool::report(int argc, const char **argv,
  705. CommandLineParserType commandLineParser) {
  706. cl::opt<bool> ShowFunctionSummaries(
  707. "show-functions", cl::Optional, cl::init(false),
  708. cl::desc("Show coverage summaries for each function"));
  709. auto Err = commandLineParser(argc, argv);
  710. if (Err)
  711. return Err;
  712. if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) {
  713. error("HTML output for summary reports is not yet supported.");
  714. return 1;
  715. }
  716. auto Coverage = load();
  717. if (!Coverage)
  718. return 1;
  719. CoverageReport Report(ViewOpts, *Coverage.get());
  720. if (!ShowFunctionSummaries)
  721. Report.renderFileReports(llvm::outs());
  722. else
  723. Report.renderFunctionReports(SourceFiles, DC, llvm::outs());
  724. return 0;
  725. }
  726. int CodeCoverageTool::export_(int argc, const char **argv,
  727. CommandLineParserType commandLineParser) {
  728. auto Err = commandLineParser(argc, argv);
  729. if (Err)
  730. return Err;
  731. if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text) {
  732. error("Coverage data can only be exported as textual JSON.");
  733. return 1;
  734. }
  735. auto Coverage = load();
  736. if (!Coverage) {
  737. error("Could not load coverage information");
  738. return 1;
  739. }
  740. exportCoverageDataToJson(*Coverage.get(), outs());
  741. return 0;
  742. }
  743. int showMain(int argc, const char *argv[]) {
  744. CodeCoverageTool Tool;
  745. return Tool.run(CodeCoverageTool::Show, argc, argv);
  746. }
  747. int reportMain(int argc, const char *argv[]) {
  748. CodeCoverageTool Tool;
  749. return Tool.run(CodeCoverageTool::Report, argc, argv);
  750. }
  751. int exportMain(int argc, const char *argv[]) {
  752. CodeCoverageTool Tool;
  753. return Tool.run(CodeCoverageTool::Export, argc, argv);
  754. }