CoverageMappingGen.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Instrumentation-based code coverage mapping generator
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
  13. #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
  14. #include "clang/Basic/LLVM.h"
  15. #include "clang/Basic/SourceLocation.h"
  16. #include "clang/Lex/PPCallbacks.h"
  17. #include "clang/Lex/Preprocessor.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/IR/GlobalValue.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. namespace clang {
  22. class LangOptions;
  23. class SourceManager;
  24. class FileEntry;
  25. class Preprocessor;
  26. class Decl;
  27. class Stmt;
  28. struct SkippedRange {
  29. enum Kind {
  30. PPIfElse, // Preprocessor #if/#else ...
  31. EmptyLine,
  32. Comment,
  33. };
  34. SourceRange Range;
  35. // The location of token before the skipped source range.
  36. SourceLocation PrevTokLoc;
  37. // The location of token after the skipped source range.
  38. SourceLocation NextTokLoc;
  39. // The nature of this skipped range
  40. Kind RangeKind;
  41. bool isComment() { return RangeKind == Comment; }
  42. bool isEmptyLine() { return RangeKind == EmptyLine; }
  43. bool isPPIfElse() { return RangeKind == PPIfElse; }
  44. SkippedRange(SourceRange Range, Kind K,
  45. SourceLocation PrevTokLoc = SourceLocation(),
  46. SourceLocation NextTokLoc = SourceLocation())
  47. : Range(Range), PrevTokLoc(PrevTokLoc), NextTokLoc(NextTokLoc),
  48. RangeKind(K) {}
  49. };
  50. /// Stores additional source code information like skipped ranges which
  51. /// is required by the coverage mapping generator and is obtained from
  52. /// the preprocessor.
  53. class CoverageSourceInfo : public PPCallbacks,
  54. public CommentHandler,
  55. public EmptylineHandler {
  56. // A vector of skipped source ranges and PrevTokLoc with NextTokLoc.
  57. std::vector<SkippedRange> SkippedRanges;
  58. SourceManager &SourceMgr;
  59. public:
  60. // Location of the token parsed before HandleComment is called. This is
  61. // updated every time Preprocessor::Lex lexes a new token.
  62. SourceLocation PrevTokLoc;
  63. CoverageSourceInfo(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
  64. std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; }
  65. void AddSkippedRange(SourceRange Range, SkippedRange::Kind RangeKind);
  66. void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
  67. void HandleEmptyline(SourceRange Range) override;
  68. bool HandleComment(Preprocessor &PP, SourceRange Range) override;
  69. void updateNextTokLoc(SourceLocation Loc);
  70. };
  71. namespace CodeGen {
  72. class CodeGenModule;
  73. /// Organizes the cross-function state that is used while generating
  74. /// code coverage mapping data.
  75. class CoverageMappingModuleGen {
  76. /// Information needed to emit a coverage record for a function.
  77. struct FunctionInfo {
  78. uint64_t NameHash;
  79. uint64_t FuncHash;
  80. std::string CoverageMapping;
  81. bool IsUsed;
  82. };
  83. CodeGenModule &CGM;
  84. CoverageSourceInfo &SourceInfo;
  85. llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
  86. std::vector<llvm::Constant *> FunctionNames;
  87. std::vector<FunctionInfo> FunctionRecords;
  88. std::map<std::string, std::string> CoveragePrefixMap;
  89. std::string getCurrentDirname();
  90. std::string normalizeFilename(StringRef Filename);
  91. /// Emit a function record.
  92. void emitFunctionMappingRecord(const FunctionInfo &Info,
  93. uint64_t FilenamesRef);
  94. public:
  95. static CoverageSourceInfo *setUpCoverageCallbacks(Preprocessor &PP);
  96. CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo);
  97. CoverageSourceInfo &getSourceInfo() const {
  98. return SourceInfo;
  99. }
  100. /// Add a function's coverage mapping record to the collection of the
  101. /// function mapping records.
  102. void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
  103. StringRef FunctionNameValue,
  104. uint64_t FunctionHash,
  105. const std::string &CoverageMapping,
  106. bool IsUsed = true);
  107. /// Emit the coverage mapping data for a translation unit.
  108. void emit();
  109. /// Return the coverage mapping translation unit file id
  110. /// for the given file.
  111. unsigned getFileID(const FileEntry *File);
  112. /// Return an interface into CodeGenModule.
  113. CodeGenModule &getCodeGenModule() { return CGM; }
  114. };
  115. /// Organizes the per-function state that is used while generating
  116. /// code coverage mapping data.
  117. class CoverageMappingGen {
  118. CoverageMappingModuleGen &CVM;
  119. SourceManager &SM;
  120. const LangOptions &LangOpts;
  121. llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
  122. public:
  123. CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
  124. const LangOptions &LangOpts)
  125. : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
  126. CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
  127. const LangOptions &LangOpts,
  128. llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
  129. : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
  130. /// Emit the coverage mapping data which maps the regions of
  131. /// code to counters that will be used to find the execution
  132. /// counts for those regions.
  133. void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
  134. /// Emit the coverage mapping data for an unused function.
  135. /// It creates mapping regions with the counter of zero.
  136. void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
  137. };
  138. } // end namespace CodeGen
  139. } // end namespace clang
  140. #endif