CoverageMappingGen.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. //===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- C++ -*-===//
  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. // Instrumentation-based code coverage mapping generator
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CoverageMappingGen.h"
  14. #include "CodeGenFunction.h"
  15. #include "clang/AST/StmtVisitor.h"
  16. #include "clang/Lex/Lexer.h"
  17. #include "llvm/ADT/SmallSet.h"
  18. #include "llvm/ADT/StringExtras.h"
  19. #include "llvm/ADT/Optional.h"
  20. #include "llvm/ProfileData/Coverage/CoverageMapping.h"
  21. #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
  22. #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
  23. #include "llvm/ProfileData/InstrProfReader.h"
  24. #include "llvm/Support/FileSystem.h"
  25. #include "llvm/Support/Path.h"
  26. using namespace clang;
  27. using namespace CodeGen;
  28. using namespace llvm::coverage;
  29. void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range) {
  30. SkippedRanges.push_back(Range);
  31. }
  32. namespace {
  33. /// \brief A region of source code that can be mapped to a counter.
  34. class SourceMappingRegion {
  35. Counter Count;
  36. /// \brief The region's starting location.
  37. Optional<SourceLocation> LocStart;
  38. /// \brief The region's ending location.
  39. Optional<SourceLocation> LocEnd;
  40. /// Whether this region should be emitted after its parent is emitted.
  41. bool DeferRegion;
  42. public:
  43. SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart,
  44. Optional<SourceLocation> LocEnd, bool DeferRegion = false)
  45. : Count(Count), LocStart(LocStart), LocEnd(LocEnd),
  46. DeferRegion(DeferRegion) {}
  47. const Counter &getCounter() const { return Count; }
  48. void setCounter(Counter C) { Count = C; }
  49. bool hasStartLoc() const { return LocStart.hasValue(); }
  50. void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
  51. SourceLocation getStartLoc() const {
  52. assert(LocStart && "Region has no start location");
  53. return *LocStart;
  54. }
  55. bool hasEndLoc() const { return LocEnd.hasValue(); }
  56. void setEndLoc(SourceLocation Loc) { LocEnd = Loc; }
  57. SourceLocation getEndLoc() const {
  58. assert(LocEnd && "Region has no end location");
  59. return *LocEnd;
  60. }
  61. bool isDeferred() const { return DeferRegion; }
  62. void setDeferred(bool Deferred) { DeferRegion = Deferred; }
  63. };
  64. /// Spelling locations for the start and end of a source region.
  65. struct SpellingRegion {
  66. /// The line where the region starts.
  67. unsigned LineStart;
  68. /// The column where the region starts.
  69. unsigned ColumnStart;
  70. /// The line where the region ends.
  71. unsigned LineEnd;
  72. /// The column where the region ends.
  73. unsigned ColumnEnd;
  74. SpellingRegion(SourceManager &SM, SourceLocation LocStart,
  75. SourceLocation LocEnd) {
  76. LineStart = SM.getSpellingLineNumber(LocStart);
  77. ColumnStart = SM.getSpellingColumnNumber(LocStart);
  78. LineEnd = SM.getSpellingLineNumber(LocEnd);
  79. ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
  80. }
  81. /// Check if the start and end locations appear in source order, i.e
  82. /// top->bottom, left->right.
  83. bool isInSourceOrder() const {
  84. return (LineStart < LineEnd) ||
  85. (LineStart == LineEnd && ColumnStart <= ColumnEnd);
  86. }
  87. };
  88. /// \brief Provides the common functionality for the different
  89. /// coverage mapping region builders.
  90. class CoverageMappingBuilder {
  91. public:
  92. CoverageMappingModuleGen &CVM;
  93. SourceManager &SM;
  94. const LangOptions &LangOpts;
  95. private:
  96. /// \brief Map of clang's FileIDs to IDs used for coverage mapping.
  97. llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
  98. FileIDMapping;
  99. public:
  100. /// \brief The coverage mapping regions for this function
  101. llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
  102. /// \brief The source mapping regions for this function.
  103. std::vector<SourceMappingRegion> SourceRegions;
  104. /// \brief A set of regions which can be used as a filter.
  105. ///
  106. /// It is produced by emitExpansionRegions() and is used in
  107. /// emitSourceRegions() to suppress producing code regions if
  108. /// the same area is covered by expansion regions.
  109. typedef llvm::SmallSet<std::pair<SourceLocation, SourceLocation>, 8>
  110. SourceRegionFilter;
  111. CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
  112. const LangOptions &LangOpts)
  113. : CVM(CVM), SM(SM), LangOpts(LangOpts) {}
  114. /// \brief Return the precise end location for the given token.
  115. SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
  116. // We avoid getLocForEndOfToken here, because it doesn't do what we want for
  117. // macro locations, which we just treat as expanded files.
  118. unsigned TokLen =
  119. Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
  120. return Loc.getLocWithOffset(TokLen);
  121. }
  122. /// \brief Return the start location of an included file or expanded macro.
  123. SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
  124. if (Loc.isMacroID())
  125. return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
  126. return SM.getLocForStartOfFile(SM.getFileID(Loc));
  127. }
  128. /// \brief Return the end location of an included file or expanded macro.
  129. SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
  130. if (Loc.isMacroID())
  131. return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
  132. SM.getFileOffset(Loc));
  133. return SM.getLocForEndOfFile(SM.getFileID(Loc));
  134. }
  135. /// \brief Find out where the current file is included or macro is expanded.
  136. SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
  137. return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).first
  138. : SM.getIncludeLoc(SM.getFileID(Loc));
  139. }
  140. /// \brief Return true if \c Loc is a location in a built-in macro.
  141. bool isInBuiltin(SourceLocation Loc) {
  142. return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>";
  143. }
  144. /// \brief Check whether \c Loc is included or expanded from \c Parent.
  145. bool isNestedIn(SourceLocation Loc, FileID Parent) {
  146. do {
  147. Loc = getIncludeOrExpansionLoc(Loc);
  148. if (Loc.isInvalid())
  149. return false;
  150. } while (!SM.isInFileID(Loc, Parent));
  151. return true;
  152. }
  153. /// \brief Get the start of \c S ignoring macro arguments and builtin macros.
  154. SourceLocation getStart(const Stmt *S) {
  155. SourceLocation Loc = S->getLocStart();
  156. while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
  157. Loc = SM.getImmediateExpansionRange(Loc).first;
  158. return Loc;
  159. }
  160. /// \brief Get the end of \c S ignoring macro arguments and builtin macros.
  161. SourceLocation getEnd(const Stmt *S) {
  162. SourceLocation Loc = S->getLocEnd();
  163. while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
  164. Loc = SM.getImmediateExpansionRange(Loc).first;
  165. return getPreciseTokenLocEnd(Loc);
  166. }
  167. /// \brief Find the set of files we have regions for and assign IDs
  168. ///
  169. /// Fills \c Mapping with the virtual file mapping needed to write out
  170. /// coverage and collects the necessary file information to emit source and
  171. /// expansion regions.
  172. void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
  173. FileIDMapping.clear();
  174. llvm::SmallSet<FileID, 8> Visited;
  175. SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
  176. for (const auto &Region : SourceRegions) {
  177. SourceLocation Loc = Region.getStartLoc();
  178. FileID File = SM.getFileID(Loc);
  179. if (!Visited.insert(File).second)
  180. continue;
  181. // Do not map FileID's associated with system headers.
  182. if (SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
  183. continue;
  184. unsigned Depth = 0;
  185. for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
  186. Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent))
  187. ++Depth;
  188. FileLocs.push_back(std::make_pair(Loc, Depth));
  189. }
  190. std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second());
  191. for (const auto &FL : FileLocs) {
  192. SourceLocation Loc = FL.first;
  193. FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
  194. auto Entry = SM.getFileEntryForID(SpellingFile);
  195. if (!Entry)
  196. continue;
  197. FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
  198. Mapping.push_back(CVM.getFileID(Entry));
  199. }
  200. }
  201. /// \brief Get the coverage mapping file ID for \c Loc.
  202. ///
  203. /// If such file id doesn't exist, return None.
  204. Optional<unsigned> getCoverageFileID(SourceLocation Loc) {
  205. auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
  206. if (Mapping != FileIDMapping.end())
  207. return Mapping->second.first;
  208. return None;
  209. }
  210. /// \brief Gather all the regions that were skipped by the preprocessor
  211. /// using the constructs like #if.
  212. void gatherSkippedRegions() {
  213. /// An array of the minimum lineStarts and the maximum lineEnds
  214. /// for mapping regions from the appropriate source files.
  215. llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
  216. FileLineRanges.resize(
  217. FileIDMapping.size(),
  218. std::make_pair(std::numeric_limits<unsigned>::max(), 0));
  219. for (const auto &R : MappingRegions) {
  220. FileLineRanges[R.FileID].first =
  221. std::min(FileLineRanges[R.FileID].first, R.LineStart);
  222. FileLineRanges[R.FileID].second =
  223. std::max(FileLineRanges[R.FileID].second, R.LineEnd);
  224. }
  225. auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
  226. for (const auto &I : SkippedRanges) {
  227. auto LocStart = I.getBegin();
  228. auto LocEnd = I.getEnd();
  229. assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
  230. "region spans multiple files");
  231. auto CovFileID = getCoverageFileID(LocStart);
  232. if (!CovFileID)
  233. continue;
  234. SpellingRegion SR{SM, LocStart, LocEnd};
  235. auto Region = CounterMappingRegion::makeSkipped(
  236. *CovFileID, SR.LineStart, SR.ColumnStart, SR.LineEnd, SR.ColumnEnd);
  237. // Make sure that we only collect the regions that are inside
  238. // the souce code of this function.
  239. if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
  240. Region.LineEnd <= FileLineRanges[*CovFileID].second)
  241. MappingRegions.push_back(Region);
  242. }
  243. }
  244. /// \brief Generate the coverage counter mapping regions from collected
  245. /// source regions.
  246. void emitSourceRegions(const SourceRegionFilter &Filter) {
  247. for (const auto &Region : SourceRegions) {
  248. assert(Region.hasEndLoc() && "incomplete region");
  249. SourceLocation LocStart = Region.getStartLoc();
  250. assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
  251. // Ignore regions from system headers.
  252. if (SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
  253. continue;
  254. auto CovFileID = getCoverageFileID(LocStart);
  255. // Ignore regions that don't have a file, such as builtin macros.
  256. if (!CovFileID)
  257. continue;
  258. SourceLocation LocEnd = Region.getEndLoc();
  259. assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
  260. "region spans multiple files");
  261. // Don't add code regions for the area covered by expansion regions.
  262. // This not only suppresses redundant regions, but sometimes prevents
  263. // creating regions with wrong counters if, for example, a statement's
  264. // body ends at the end of a nested macro.
  265. if (Filter.count(std::make_pair(LocStart, LocEnd)))
  266. continue;
  267. // Find the spelling locations for the mapping region.
  268. SpellingRegion SR{SM, LocStart, LocEnd};
  269. assert(SR.isInSourceOrder() && "region start and end out of order");
  270. MappingRegions.push_back(CounterMappingRegion::makeRegion(
  271. Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
  272. SR.LineEnd, SR.ColumnEnd));
  273. }
  274. }
  275. /// \brief Generate expansion regions for each virtual file we've seen.
  276. SourceRegionFilter emitExpansionRegions() {
  277. SourceRegionFilter Filter;
  278. for (const auto &FM : FileIDMapping) {
  279. SourceLocation ExpandedLoc = FM.second.second;
  280. SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
  281. if (ParentLoc.isInvalid())
  282. continue;
  283. auto ParentFileID = getCoverageFileID(ParentLoc);
  284. if (!ParentFileID)
  285. continue;
  286. auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
  287. assert(ExpandedFileID && "expansion in uncovered file");
  288. SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
  289. assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
  290. "region spans multiple files");
  291. Filter.insert(std::make_pair(ParentLoc, LocEnd));
  292. SpellingRegion SR{SM, ParentLoc, LocEnd};
  293. assert(SR.isInSourceOrder() && "region start and end out of order");
  294. MappingRegions.push_back(CounterMappingRegion::makeExpansion(
  295. *ParentFileID, *ExpandedFileID, SR.LineStart, SR.ColumnStart,
  296. SR.LineEnd, SR.ColumnEnd));
  297. }
  298. return Filter;
  299. }
  300. };
  301. /// \brief Creates unreachable coverage regions for the functions that
  302. /// are not emitted.
  303. struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
  304. EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
  305. const LangOptions &LangOpts)
  306. : CoverageMappingBuilder(CVM, SM, LangOpts) {}
  307. void VisitDecl(const Decl *D) {
  308. if (!D->hasBody())
  309. return;
  310. auto Body = D->getBody();
  311. SourceLocation Start = getStart(Body);
  312. SourceLocation End = getEnd(Body);
  313. if (!SM.isWrittenInSameFile(Start, End)) {
  314. // Walk up to find the common ancestor.
  315. // Correct the locations accordingly.
  316. FileID StartFileID = SM.getFileID(Start);
  317. FileID EndFileID = SM.getFileID(End);
  318. while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) {
  319. Start = getIncludeOrExpansionLoc(Start);
  320. assert(Start.isValid() &&
  321. "Declaration start location not nested within a known region");
  322. StartFileID = SM.getFileID(Start);
  323. }
  324. while (StartFileID != EndFileID) {
  325. End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
  326. assert(End.isValid() &&
  327. "Declaration end location not nested within a known region");
  328. EndFileID = SM.getFileID(End);
  329. }
  330. }
  331. SourceRegions.emplace_back(Counter(), Start, End);
  332. }
  333. /// \brief Write the mapping data to the output stream
  334. void write(llvm::raw_ostream &OS) {
  335. SmallVector<unsigned, 16> FileIDMapping;
  336. gatherFileIDs(FileIDMapping);
  337. emitSourceRegions(SourceRegionFilter());
  338. if (MappingRegions.empty())
  339. return;
  340. CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions);
  341. Writer.write(OS);
  342. }
  343. };
  344. /// \brief A StmtVisitor that creates coverage mapping regions which map
  345. /// from the source code locations to the PGO counters.
  346. struct CounterCoverageMappingBuilder
  347. : public CoverageMappingBuilder,
  348. public ConstStmtVisitor<CounterCoverageMappingBuilder> {
  349. /// \brief The map of statements to count values.
  350. llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
  351. /// \brief A stack of currently live regions.
  352. std::vector<SourceMappingRegion> RegionStack;
  353. /// The currently deferred region: its end location and count can be set once
  354. /// its parent has been popped from the region stack.
  355. Optional<SourceMappingRegion> DeferredRegion;
  356. CounterExpressionBuilder Builder;
  357. /// \brief A location in the most recently visited file or macro.
  358. ///
  359. /// This is used to adjust the active source regions appropriately when
  360. /// expressions cross file or macro boundaries.
  361. SourceLocation MostRecentLocation;
  362. /// \brief Return a counter for the subtraction of \c RHS from \c LHS
  363. Counter subtractCounters(Counter LHS, Counter RHS) {
  364. return Builder.subtract(LHS, RHS);
  365. }
  366. /// \brief Return a counter for the sum of \c LHS and \c RHS.
  367. Counter addCounters(Counter LHS, Counter RHS) {
  368. return Builder.add(LHS, RHS);
  369. }
  370. Counter addCounters(Counter C1, Counter C2, Counter C3) {
  371. return addCounters(addCounters(C1, C2), C3);
  372. }
  373. /// \brief Return the region counter for the given statement.
  374. ///
  375. /// This should only be called on statements that have a dedicated counter.
  376. Counter getRegionCounter(const Stmt *S) {
  377. return Counter::getCounter(CounterMap[S]);
  378. }
  379. /// \brief Push a region onto the stack.
  380. ///
  381. /// Returns the index on the stack where the region was pushed. This can be
  382. /// used with popRegions to exit a "scope", ending the region that was pushed.
  383. size_t pushRegion(Counter Count, Optional<SourceLocation> StartLoc = None,
  384. Optional<SourceLocation> EndLoc = None) {
  385. if (StartLoc) {
  386. MostRecentLocation = *StartLoc;
  387. completeDeferred(Count, MostRecentLocation);
  388. }
  389. RegionStack.emplace_back(Count, StartLoc, EndLoc);
  390. return RegionStack.size() - 1;
  391. }
  392. /// Complete any pending deferred region by setting its end location and
  393. /// count, and then pushing it onto the region stack.
  394. size_t completeDeferred(Counter Count, SourceLocation DeferredEndLoc) {
  395. size_t Index = RegionStack.size();
  396. if (!DeferredRegion)
  397. return Index;
  398. // Consume the pending region.
  399. SourceMappingRegion DR = DeferredRegion.getValue();
  400. DeferredRegion = None;
  401. // If the region ends in an expansion, find the expansion site.
  402. if (SM.getFileID(DeferredEndLoc) != SM.getMainFileID()) {
  403. FileID StartFile = SM.getFileID(DR.getStartLoc());
  404. if (isNestedIn(DeferredEndLoc, StartFile)) {
  405. do {
  406. DeferredEndLoc = getIncludeOrExpansionLoc(DeferredEndLoc);
  407. } while (StartFile != SM.getFileID(DeferredEndLoc));
  408. }
  409. }
  410. // The parent of this deferred region ends where the containing decl ends,
  411. // so the region isn't useful.
  412. if (DR.getStartLoc() == DeferredEndLoc)
  413. return Index;
  414. // If we're visiting statements in non-source order (e.g switch cases or
  415. // a loop condition) we can't construct a sensible deferred region.
  416. if (!SpellingRegion(SM, DR.getStartLoc(), DeferredEndLoc).isInSourceOrder())
  417. return Index;
  418. DR.setCounter(Count);
  419. DR.setEndLoc(DeferredEndLoc);
  420. handleFileExit(DeferredEndLoc);
  421. RegionStack.push_back(DR);
  422. return Index;
  423. }
  424. /// \brief Pop regions from the stack into the function's list of regions.
  425. ///
  426. /// Adds all regions from \c ParentIndex to the top of the stack to the
  427. /// function's \c SourceRegions.
  428. void popRegions(size_t ParentIndex) {
  429. assert(RegionStack.size() >= ParentIndex && "parent not in stack");
  430. bool ParentOfDeferredRegion = false;
  431. while (RegionStack.size() > ParentIndex) {
  432. SourceMappingRegion &Region = RegionStack.back();
  433. if (Region.hasStartLoc()) {
  434. SourceLocation StartLoc = Region.getStartLoc();
  435. SourceLocation EndLoc = Region.hasEndLoc()
  436. ? Region.getEndLoc()
  437. : RegionStack[ParentIndex].getEndLoc();
  438. while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
  439. // The region ends in a nested file or macro expansion. Create a
  440. // separate region for each expansion.
  441. SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
  442. assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
  443. if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
  444. SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
  445. EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
  446. if (EndLoc.isInvalid())
  447. llvm::report_fatal_error("File exit not handled before popRegions");
  448. }
  449. Region.setEndLoc(EndLoc);
  450. MostRecentLocation = EndLoc;
  451. // If this region happens to span an entire expansion, we need to make
  452. // sure we don't overlap the parent region with it.
  453. if (StartLoc == getStartOfFileOrMacro(StartLoc) &&
  454. EndLoc == getEndOfFileOrMacro(EndLoc))
  455. MostRecentLocation = getIncludeOrExpansionLoc(EndLoc);
  456. assert(SM.isWrittenInSameFile(Region.getStartLoc(), EndLoc));
  457. SourceRegions.push_back(Region);
  458. if (ParentOfDeferredRegion) {
  459. ParentOfDeferredRegion = false;
  460. // If there's an existing deferred region, keep the old one, because
  461. // it means there are two consecutive returns (or a similar pattern).
  462. if (!DeferredRegion.hasValue() &&
  463. // File IDs aren't gathered within macro expansions, so it isn't
  464. // useful to try and create a deferred region inside of one.
  465. (SM.getFileID(EndLoc) == SM.getMainFileID()))
  466. DeferredRegion =
  467. SourceMappingRegion(Counter::getZero(), EndLoc, None);
  468. }
  469. } else if (Region.isDeferred()) {
  470. assert(!ParentOfDeferredRegion && "Consecutive deferred regions");
  471. ParentOfDeferredRegion = true;
  472. }
  473. RegionStack.pop_back();
  474. }
  475. assert(!ParentOfDeferredRegion && "Deferred region with no parent");
  476. }
  477. /// \brief Return the currently active region.
  478. SourceMappingRegion &getRegion() {
  479. assert(!RegionStack.empty() && "statement has no region");
  480. return RegionStack.back();
  481. }
  482. /// \brief Propagate counts through the children of \c S.
  483. Counter propagateCounts(Counter TopCount, const Stmt *S) {
  484. SourceLocation StartLoc = getStart(S);
  485. SourceLocation EndLoc = getEnd(S);
  486. size_t Index = pushRegion(TopCount, StartLoc, EndLoc);
  487. Visit(S);
  488. Counter ExitCount = getRegion().getCounter();
  489. popRegions(Index);
  490. // The statement may be spanned by an expansion. Make sure we handle a file
  491. // exit out of this expansion before moving to the next statement.
  492. if (SM.isBeforeInTranslationUnit(StartLoc, S->getLocStart()))
  493. MostRecentLocation = EndLoc;
  494. return ExitCount;
  495. }
  496. /// \brief Check whether a region with bounds \c StartLoc and \c EndLoc
  497. /// is already added to \c SourceRegions.
  498. bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc) {
  499. return SourceRegions.rend() !=
  500. std::find_if(SourceRegions.rbegin(), SourceRegions.rend(),
  501. [&](const SourceMappingRegion &Region) {
  502. return Region.getStartLoc() == StartLoc &&
  503. Region.getEndLoc() == EndLoc;
  504. });
  505. }
  506. /// \brief Adjust the most recently visited location to \c EndLoc.
  507. ///
  508. /// This should be used after visiting any statements in non-source order.
  509. void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
  510. MostRecentLocation = EndLoc;
  511. // The code region for a whole macro is created in handleFileExit() when
  512. // it detects exiting of the virtual file of that macro. If we visited
  513. // statements in non-source order, we might already have such a region
  514. // added, for example, if a body of a loop is divided among multiple
  515. // macros. Avoid adding duplicate regions in such case.
  516. if (getRegion().hasEndLoc() &&
  517. MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) &&
  518. isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
  519. MostRecentLocation))
  520. MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
  521. }
  522. /// \brief Adjust regions and state when \c NewLoc exits a file.
  523. ///
  524. /// If moving from our most recently tracked location to \c NewLoc exits any
  525. /// files, this adjusts our current region stack and creates the file regions
  526. /// for the exited file.
  527. void handleFileExit(SourceLocation NewLoc) {
  528. if (NewLoc.isInvalid() ||
  529. SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
  530. return;
  531. // If NewLoc is not in a file that contains MostRecentLocation, walk up to
  532. // find the common ancestor.
  533. SourceLocation LCA = NewLoc;
  534. FileID ParentFile = SM.getFileID(LCA);
  535. while (!isNestedIn(MostRecentLocation, ParentFile)) {
  536. LCA = getIncludeOrExpansionLoc(LCA);
  537. if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
  538. // Since there isn't a common ancestor, no file was exited. We just need
  539. // to adjust our location to the new file.
  540. MostRecentLocation = NewLoc;
  541. return;
  542. }
  543. ParentFile = SM.getFileID(LCA);
  544. }
  545. llvm::SmallSet<SourceLocation, 8> StartLocs;
  546. Optional<Counter> ParentCounter;
  547. for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
  548. if (!I.hasStartLoc())
  549. continue;
  550. SourceLocation Loc = I.getStartLoc();
  551. if (!isNestedIn(Loc, ParentFile)) {
  552. ParentCounter = I.getCounter();
  553. break;
  554. }
  555. while (!SM.isInFileID(Loc, ParentFile)) {
  556. // The most nested region for each start location is the one with the
  557. // correct count. We avoid creating redundant regions by stopping once
  558. // we've seen this region.
  559. if (StartLocs.insert(Loc).second)
  560. SourceRegions.emplace_back(I.getCounter(), Loc,
  561. getEndOfFileOrMacro(Loc));
  562. Loc = getIncludeOrExpansionLoc(Loc);
  563. }
  564. I.setStartLoc(getPreciseTokenLocEnd(Loc));
  565. }
  566. if (ParentCounter) {
  567. // If the file is contained completely by another region and doesn't
  568. // immediately start its own region, the whole file gets a region
  569. // corresponding to the parent.
  570. SourceLocation Loc = MostRecentLocation;
  571. while (isNestedIn(Loc, ParentFile)) {
  572. SourceLocation FileStart = getStartOfFileOrMacro(Loc);
  573. if (StartLocs.insert(FileStart).second)
  574. SourceRegions.emplace_back(*ParentCounter, FileStart,
  575. getEndOfFileOrMacro(Loc));
  576. Loc = getIncludeOrExpansionLoc(Loc);
  577. }
  578. }
  579. MostRecentLocation = NewLoc;
  580. }
  581. /// \brief Ensure that \c S is included in the current region.
  582. void extendRegion(const Stmt *S) {
  583. SourceMappingRegion &Region = getRegion();
  584. SourceLocation StartLoc = getStart(S);
  585. handleFileExit(StartLoc);
  586. if (!Region.hasStartLoc())
  587. Region.setStartLoc(StartLoc);
  588. completeDeferred(Region.getCounter(), StartLoc);
  589. }
  590. /// \brief Mark \c S as a terminator, starting a zero region.
  591. void terminateRegion(const Stmt *S) {
  592. extendRegion(S);
  593. SourceMappingRegion &Region = getRegion();
  594. if (!Region.hasEndLoc())
  595. Region.setEndLoc(getEnd(S));
  596. pushRegion(Counter::getZero());
  597. getRegion().setDeferred(true);
  598. }
  599. /// \brief Keep counts of breaks and continues inside loops.
  600. struct BreakContinue {
  601. Counter BreakCount;
  602. Counter ContinueCount;
  603. };
  604. SmallVector<BreakContinue, 8> BreakContinueStack;
  605. CounterCoverageMappingBuilder(
  606. CoverageMappingModuleGen &CVM,
  607. llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
  608. const LangOptions &LangOpts)
  609. : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
  610. DeferredRegion(None) {}
  611. /// \brief Write the mapping data to the output stream
  612. void write(llvm::raw_ostream &OS) {
  613. llvm::SmallVector<unsigned, 8> VirtualFileMapping;
  614. gatherFileIDs(VirtualFileMapping);
  615. SourceRegionFilter Filter = emitExpansionRegions();
  616. assert(!DeferredRegion && "Deferred region never completed");
  617. emitSourceRegions(Filter);
  618. gatherSkippedRegions();
  619. if (MappingRegions.empty())
  620. return;
  621. CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
  622. MappingRegions);
  623. Writer.write(OS);
  624. }
  625. void VisitStmt(const Stmt *S) {
  626. if (S->getLocStart().isValid())
  627. extendRegion(S);
  628. for (const Stmt *Child : S->children())
  629. if (Child)
  630. this->Visit(Child);
  631. handleFileExit(getEnd(S));
  632. }
  633. void VisitDecl(const Decl *D) {
  634. assert(!DeferredRegion && "Deferred region never completed");
  635. Stmt *Body = D->getBody();
  636. // Do not propagate region counts into system headers.
  637. if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
  638. return;
  639. Counter ExitCount = propagateCounts(getRegionCounter(Body), Body);
  640. assert(RegionStack.empty() && "Regions entered but never exited");
  641. // Complete any deferred regions introduced by the last statement in a decl.
  642. popRegions(completeDeferred(ExitCount, getEnd(Body)));
  643. }
  644. void VisitReturnStmt(const ReturnStmt *S) {
  645. extendRegion(S);
  646. if (S->getRetValue())
  647. Visit(S->getRetValue());
  648. terminateRegion(S);
  649. }
  650. void VisitCXXThrowExpr(const CXXThrowExpr *E) {
  651. extendRegion(E);
  652. if (E->getSubExpr())
  653. Visit(E->getSubExpr());
  654. terminateRegion(E);
  655. }
  656. void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
  657. void VisitLabelStmt(const LabelStmt *S) {
  658. SourceLocation Start = getStart(S);
  659. // We can't extendRegion here or we risk overlapping with our new region.
  660. handleFileExit(Start);
  661. pushRegion(getRegionCounter(S), Start);
  662. Visit(S->getSubStmt());
  663. }
  664. void VisitBreakStmt(const BreakStmt *S) {
  665. assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
  666. BreakContinueStack.back().BreakCount = addCounters(
  667. BreakContinueStack.back().BreakCount, getRegion().getCounter());
  668. // FIXME: a break in a switch should terminate regions for all preceding
  669. // case statements, not just the most recent one.
  670. terminateRegion(S);
  671. }
  672. void VisitContinueStmt(const ContinueStmt *S) {
  673. assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
  674. BreakContinueStack.back().ContinueCount = addCounters(
  675. BreakContinueStack.back().ContinueCount, getRegion().getCounter());
  676. terminateRegion(S);
  677. }
  678. void VisitCallExpr(const CallExpr *E) {
  679. VisitStmt(E);
  680. // Terminate the region when we hit a noreturn function.
  681. // (This is helpful dealing with switch statements.)
  682. QualType CalleeType = E->getCallee()->getType();
  683. if (getFunctionExtInfo(*CalleeType).getNoReturn())
  684. terminateRegion(E);
  685. }
  686. void VisitWhileStmt(const WhileStmt *S) {
  687. extendRegion(S);
  688. Counter ParentCount = getRegion().getCounter();
  689. Counter BodyCount = getRegionCounter(S);
  690. // Handle the body first so that we can get the backedge count.
  691. BreakContinueStack.push_back(BreakContinue());
  692. extendRegion(S->getBody());
  693. Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
  694. BreakContinue BC = BreakContinueStack.pop_back_val();
  695. // Go back to handle the condition.
  696. Counter CondCount =
  697. addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
  698. propagateCounts(CondCount, S->getCond());
  699. adjustForOutOfOrderTraversal(getEnd(S));
  700. Counter OutCount =
  701. addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
  702. if (OutCount != ParentCount)
  703. pushRegion(OutCount);
  704. }
  705. void VisitDoStmt(const DoStmt *S) {
  706. extendRegion(S);
  707. Counter ParentCount = getRegion().getCounter();
  708. Counter BodyCount = getRegionCounter(S);
  709. BreakContinueStack.push_back(BreakContinue());
  710. extendRegion(S->getBody());
  711. Counter BackedgeCount =
  712. propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
  713. BreakContinue BC = BreakContinueStack.pop_back_val();
  714. Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
  715. propagateCounts(CondCount, S->getCond());
  716. Counter OutCount =
  717. addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
  718. if (OutCount != ParentCount)
  719. pushRegion(OutCount);
  720. }
  721. void VisitForStmt(const ForStmt *S) {
  722. extendRegion(S);
  723. if (S->getInit())
  724. Visit(S->getInit());
  725. Counter ParentCount = getRegion().getCounter();
  726. Counter BodyCount = getRegionCounter(S);
  727. // Handle the body first so that we can get the backedge count.
  728. BreakContinueStack.push_back(BreakContinue());
  729. extendRegion(S->getBody());
  730. Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
  731. BreakContinue BC = BreakContinueStack.pop_back_val();
  732. // The increment is essentially part of the body but it needs to include
  733. // the count for all the continue statements.
  734. if (const Stmt *Inc = S->getInc())
  735. propagateCounts(addCounters(BackedgeCount, BC.ContinueCount), Inc);
  736. // Go back to handle the condition.
  737. Counter CondCount =
  738. addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
  739. if (const Expr *Cond = S->getCond()) {
  740. propagateCounts(CondCount, Cond);
  741. adjustForOutOfOrderTraversal(getEnd(S));
  742. }
  743. Counter OutCount =
  744. addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
  745. if (OutCount != ParentCount)
  746. pushRegion(OutCount);
  747. }
  748. void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
  749. extendRegion(S);
  750. Visit(S->getLoopVarStmt());
  751. Visit(S->getRangeStmt());
  752. Counter ParentCount = getRegion().getCounter();
  753. Counter BodyCount = getRegionCounter(S);
  754. BreakContinueStack.push_back(BreakContinue());
  755. extendRegion(S->getBody());
  756. Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
  757. BreakContinue BC = BreakContinueStack.pop_back_val();
  758. Counter LoopCount =
  759. addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
  760. Counter OutCount =
  761. addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
  762. if (OutCount != ParentCount)
  763. pushRegion(OutCount);
  764. }
  765. void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
  766. extendRegion(S);
  767. Visit(S->getElement());
  768. Counter ParentCount = getRegion().getCounter();
  769. Counter BodyCount = getRegionCounter(S);
  770. BreakContinueStack.push_back(BreakContinue());
  771. extendRegion(S->getBody());
  772. Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
  773. BreakContinue BC = BreakContinueStack.pop_back_val();
  774. Counter LoopCount =
  775. addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
  776. Counter OutCount =
  777. addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
  778. if (OutCount != ParentCount)
  779. pushRegion(OutCount);
  780. }
  781. void VisitSwitchStmt(const SwitchStmt *S) {
  782. extendRegion(S);
  783. if (S->getInit())
  784. Visit(S->getInit());
  785. Visit(S->getCond());
  786. BreakContinueStack.push_back(BreakContinue());
  787. const Stmt *Body = S->getBody();
  788. extendRegion(Body);
  789. if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
  790. if (!CS->body_empty()) {
  791. // Make a region for the body of the switch. If the body starts with
  792. // a case, that case will reuse this region; otherwise, this covers
  793. // the unreachable code at the beginning of the switch body.
  794. size_t Index =
  795. pushRegion(Counter::getZero(), getStart(CS->body_front()));
  796. for (const auto *Child : CS->children())
  797. Visit(Child);
  798. // Set the end for the body of the switch, if it isn't already set.
  799. for (size_t i = RegionStack.size(); i != Index; --i) {
  800. if (!RegionStack[i - 1].hasEndLoc())
  801. RegionStack[i - 1].setEndLoc(getEnd(CS->body_back()));
  802. }
  803. popRegions(Index);
  804. }
  805. } else
  806. propagateCounts(Counter::getZero(), Body);
  807. BreakContinue BC = BreakContinueStack.pop_back_val();
  808. if (!BreakContinueStack.empty())
  809. BreakContinueStack.back().ContinueCount = addCounters(
  810. BreakContinueStack.back().ContinueCount, BC.ContinueCount);
  811. Counter ExitCount = getRegionCounter(S);
  812. SourceLocation ExitLoc = getEnd(S);
  813. pushRegion(ExitCount);
  814. // Ensure that handleFileExit recognizes when the end location is located
  815. // in a different file.
  816. MostRecentLocation = getStart(S);
  817. handleFileExit(ExitLoc);
  818. }
  819. void VisitSwitchCase(const SwitchCase *S) {
  820. extendRegion(S);
  821. SourceMappingRegion &Parent = getRegion();
  822. Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
  823. // Reuse the existing region if it starts at our label. This is typical of
  824. // the first case in a switch.
  825. if (Parent.hasStartLoc() && Parent.getStartLoc() == getStart(S))
  826. Parent.setCounter(Count);
  827. else
  828. pushRegion(Count, getStart(S));
  829. if (const auto *CS = dyn_cast<CaseStmt>(S)) {
  830. Visit(CS->getLHS());
  831. if (const Expr *RHS = CS->getRHS())
  832. Visit(RHS);
  833. }
  834. Visit(S->getSubStmt());
  835. }
  836. void VisitIfStmt(const IfStmt *S) {
  837. extendRegion(S);
  838. if (S->getInit())
  839. Visit(S->getInit());
  840. // Extend into the condition before we propagate through it below - this is
  841. // needed to handle macros that generate the "if" but not the condition.
  842. extendRegion(S->getCond());
  843. Counter ParentCount = getRegion().getCounter();
  844. Counter ThenCount = getRegionCounter(S);
  845. // Emitting a counter for the condition makes it easier to interpret the
  846. // counter for the body when looking at the coverage.
  847. propagateCounts(ParentCount, S->getCond());
  848. extendRegion(S->getThen());
  849. Counter OutCount = propagateCounts(ThenCount, S->getThen());
  850. Counter ElseCount = subtractCounters(ParentCount, ThenCount);
  851. if (const Stmt *Else = S->getElse()) {
  852. extendRegion(S->getElse());
  853. OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
  854. } else
  855. OutCount = addCounters(OutCount, ElseCount);
  856. if (OutCount != ParentCount)
  857. pushRegion(OutCount);
  858. }
  859. void VisitCXXTryStmt(const CXXTryStmt *S) {
  860. extendRegion(S);
  861. // Handle macros that generate the "try" but not the rest.
  862. extendRegion(S->getTryBlock());
  863. Counter ParentCount = getRegion().getCounter();
  864. propagateCounts(ParentCount, S->getTryBlock());
  865. for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
  866. Visit(S->getHandler(I));
  867. Counter ExitCount = getRegionCounter(S);
  868. pushRegion(ExitCount);
  869. }
  870. void VisitCXXCatchStmt(const CXXCatchStmt *S) {
  871. propagateCounts(getRegionCounter(S), S->getHandlerBlock());
  872. }
  873. void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
  874. extendRegion(E);
  875. Counter ParentCount = getRegion().getCounter();
  876. Counter TrueCount = getRegionCounter(E);
  877. Visit(E->getCond());
  878. if (!isa<BinaryConditionalOperator>(E)) {
  879. extendRegion(E->getTrueExpr());
  880. propagateCounts(TrueCount, E->getTrueExpr());
  881. }
  882. extendRegion(E->getFalseExpr());
  883. propagateCounts(subtractCounters(ParentCount, TrueCount),
  884. E->getFalseExpr());
  885. }
  886. void VisitBinLAnd(const BinaryOperator *E) {
  887. extendRegion(E);
  888. Visit(E->getLHS());
  889. extendRegion(E->getRHS());
  890. propagateCounts(getRegionCounter(E), E->getRHS());
  891. }
  892. void VisitBinLOr(const BinaryOperator *E) {
  893. extendRegion(E);
  894. Visit(E->getLHS());
  895. extendRegion(E->getRHS());
  896. propagateCounts(getRegionCounter(E), E->getRHS());
  897. }
  898. void VisitLambdaExpr(const LambdaExpr *LE) {
  899. // Lambdas are treated as their own functions for now, so we shouldn't
  900. // propagate counts into them.
  901. }
  902. };
  903. std::string getCoverageSection(const CodeGenModule &CGM) {
  904. return llvm::getInstrProfSectionName(
  905. llvm::IPSK_covmap,
  906. CGM.getContext().getTargetInfo().getTriple().getObjectFormat());
  907. }
  908. std::string normalizeFilename(StringRef Filename) {
  909. llvm::SmallString<256> Path(Filename);
  910. llvm::sys::fs::make_absolute(Path);
  911. llvm::sys::path::remove_dots(Path, /*remove_dot_dots=*/true);
  912. return Path.str().str();
  913. }
  914. } // end anonymous namespace
  915. static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
  916. ArrayRef<CounterExpression> Expressions,
  917. ArrayRef<CounterMappingRegion> Regions) {
  918. OS << FunctionName << ":\n";
  919. CounterMappingContext Ctx(Expressions);
  920. for (const auto &R : Regions) {
  921. OS.indent(2);
  922. switch (R.Kind) {
  923. case CounterMappingRegion::CodeRegion:
  924. break;
  925. case CounterMappingRegion::ExpansionRegion:
  926. OS << "Expansion,";
  927. break;
  928. case CounterMappingRegion::SkippedRegion:
  929. OS << "Skipped,";
  930. break;
  931. }
  932. OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
  933. << " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
  934. Ctx.dump(R.Count, OS);
  935. if (R.Kind == CounterMappingRegion::ExpansionRegion)
  936. OS << " (Expanded file = " << R.ExpandedFileID << ")";
  937. OS << "\n";
  938. }
  939. }
  940. void CoverageMappingModuleGen::addFunctionMappingRecord(
  941. llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
  942. const std::string &CoverageMapping, bool IsUsed) {
  943. llvm::LLVMContext &Ctx = CGM.getLLVMContext();
  944. if (!FunctionRecordTy) {
  945. #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
  946. llvm::Type *FunctionRecordTypes[] = {
  947. #include "llvm/ProfileData/InstrProfData.inc"
  948. };
  949. FunctionRecordTy =
  950. llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes),
  951. /*isPacked=*/true);
  952. }
  953. #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
  954. llvm::Constant *FunctionRecordVals[] = {
  955. #include "llvm/ProfileData/InstrProfData.inc"
  956. };
  957. FunctionRecords.push_back(llvm::ConstantStruct::get(
  958. FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
  959. if (!IsUsed)
  960. FunctionNames.push_back(
  961. llvm::ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx)));
  962. CoverageMappings.push_back(CoverageMapping);
  963. if (CGM.getCodeGenOpts().DumpCoverageMapping) {
  964. // Dump the coverage mapping data for this function by decoding the
  965. // encoded data. This allows us to dump the mapping regions which were
  966. // also processed by the CoverageMappingWriter which performs
  967. // additional minimization operations such as reducing the number of
  968. // expressions.
  969. std::vector<StringRef> Filenames;
  970. std::vector<CounterExpression> Expressions;
  971. std::vector<CounterMappingRegion> Regions;
  972. llvm::SmallVector<std::string, 16> FilenameStrs;
  973. llvm::SmallVector<StringRef, 16> FilenameRefs;
  974. FilenameStrs.resize(FileEntries.size());
  975. FilenameRefs.resize(FileEntries.size());
  976. for (const auto &Entry : FileEntries) {
  977. auto I = Entry.second;
  978. FilenameStrs[I] = normalizeFilename(Entry.first->getName());
  979. FilenameRefs[I] = FilenameStrs[I];
  980. }
  981. RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
  982. Expressions, Regions);
  983. if (Reader.read())
  984. return;
  985. dump(llvm::outs(), NameValue, Expressions, Regions);
  986. }
  987. }
  988. void CoverageMappingModuleGen::emit() {
  989. if (FunctionRecords.empty())
  990. return;
  991. llvm::LLVMContext &Ctx = CGM.getLLVMContext();
  992. auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
  993. // Create the filenames and merge them with coverage mappings
  994. llvm::SmallVector<std::string, 16> FilenameStrs;
  995. llvm::SmallVector<StringRef, 16> FilenameRefs;
  996. FilenameStrs.resize(FileEntries.size());
  997. FilenameRefs.resize(FileEntries.size());
  998. for (const auto &Entry : FileEntries) {
  999. auto I = Entry.second;
  1000. FilenameStrs[I] = normalizeFilename(Entry.first->getName());
  1001. FilenameRefs[I] = FilenameStrs[I];
  1002. }
  1003. std::string FilenamesAndCoverageMappings;
  1004. llvm::raw_string_ostream OS(FilenamesAndCoverageMappings);
  1005. CoverageFilenamesSectionWriter(FilenameRefs).write(OS);
  1006. std::string RawCoverageMappings =
  1007. llvm::join(CoverageMappings.begin(), CoverageMappings.end(), "");
  1008. OS << RawCoverageMappings;
  1009. size_t CoverageMappingSize = RawCoverageMappings.size();
  1010. size_t FilenamesSize = OS.str().size() - CoverageMappingSize;
  1011. // Append extra zeroes if necessary to ensure that the size of the filenames
  1012. // and coverage mappings is a multiple of 8.
  1013. if (size_t Rem = OS.str().size() % 8) {
  1014. CoverageMappingSize += 8 - Rem;
  1015. for (size_t I = 0, S = 8 - Rem; I < S; ++I)
  1016. OS << '\0';
  1017. }
  1018. auto *FilenamesAndMappingsVal =
  1019. llvm::ConstantDataArray::getString(Ctx, OS.str(), false);
  1020. // Create the deferred function records array
  1021. auto RecordsTy =
  1022. llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
  1023. auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
  1024. llvm::Type *CovDataHeaderTypes[] = {
  1025. #define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
  1026. #include "llvm/ProfileData/InstrProfData.inc"
  1027. };
  1028. auto CovDataHeaderTy =
  1029. llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes));
  1030. llvm::Constant *CovDataHeaderVals[] = {
  1031. #define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
  1032. #include "llvm/ProfileData/InstrProfData.inc"
  1033. };
  1034. auto CovDataHeaderVal = llvm::ConstantStruct::get(
  1035. CovDataHeaderTy, makeArrayRef(CovDataHeaderVals));
  1036. // Create the coverage data record
  1037. llvm::Type *CovDataTypes[] = {CovDataHeaderTy, RecordsTy,
  1038. FilenamesAndMappingsVal->getType()};
  1039. auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
  1040. llvm::Constant *TUDataVals[] = {CovDataHeaderVal, RecordsVal,
  1041. FilenamesAndMappingsVal};
  1042. auto CovDataVal =
  1043. llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
  1044. auto CovData = new llvm::GlobalVariable(
  1045. CGM.getModule(), CovDataTy, true, llvm::GlobalValue::InternalLinkage,
  1046. CovDataVal, llvm::getCoverageMappingVarName());
  1047. CovData->setSection(getCoverageSection(CGM));
  1048. CovData->setAlignment(8);
  1049. // Make sure the data doesn't get deleted.
  1050. CGM.addUsedGlobal(CovData);
  1051. // Create the deferred function records array
  1052. if (!FunctionNames.empty()) {
  1053. auto NamesArrTy = llvm::ArrayType::get(llvm::Type::getInt8PtrTy(Ctx),
  1054. FunctionNames.size());
  1055. auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
  1056. // This variable will *NOT* be emitted to the object file. It is used
  1057. // to pass the list of names referenced to codegen.
  1058. new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
  1059. llvm::GlobalValue::InternalLinkage, NamesArrVal,
  1060. llvm::getCoverageUnusedNamesVarName());
  1061. }
  1062. }
  1063. unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
  1064. auto It = FileEntries.find(File);
  1065. if (It != FileEntries.end())
  1066. return It->second;
  1067. unsigned FileID = FileEntries.size();
  1068. FileEntries.insert(std::make_pair(File, FileID));
  1069. return FileID;
  1070. }
  1071. void CoverageMappingGen::emitCounterMapping(const Decl *D,
  1072. llvm::raw_ostream &OS) {
  1073. assert(CounterMap);
  1074. CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
  1075. Walker.VisitDecl(D);
  1076. Walker.write(OS);
  1077. }
  1078. void CoverageMappingGen::emitEmptyMapping(const Decl *D,
  1079. llvm::raw_ostream &OS) {
  1080. EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
  1081. Walker.VisitDecl(D);
  1082. Walker.write(OS);
  1083. }