CoverageMappingGen.cpp 47 KB

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