CodeCoverage.cpp 37 KB

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