CoverageMappingGen.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. SourceRange Range;
  30. // The location of token before the skipped source range.
  31. SourceLocation PrevTokLoc;
  32. // The location of token after the skipped source range.
  33. SourceLocation NextTokLoc;
  34. SkippedRange(SourceRange Range, SourceLocation PrevTokLoc = SourceLocation(),
  35. SourceLocation NextTokLoc = SourceLocation())
  36. : Range(Range), PrevTokLoc(PrevTokLoc), NextTokLoc(NextTokLoc) {}
  37. };
  38. /// Stores additional source code information like skipped ranges which
  39. /// is required by the coverage mapping generator and is obtained from
  40. /// the preprocessor.
  41. class CoverageSourceInfo : public PPCallbacks,
  42. public CommentHandler,
  43. public EmptylineHandler {
  44. // A vector of skipped source ranges and PrevTokLoc with NextTokLoc.
  45. std::vector<SkippedRange> SkippedRanges;
  46. SourceManager &SourceMgr;
  47. public:
  48. // Location of the token parsed before HandleComment is called. This is
  49. // updated every time Preprocessor::Lex lexes a new token.
  50. SourceLocation PrevTokLoc;
  51. CoverageSourceInfo(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
  52. std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; }
  53. void AddSkippedRange(SourceRange Range);
  54. void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
  55. void HandleEmptyline(SourceRange Range) override;
  56. bool HandleComment(Preprocessor &PP, SourceRange Range) override;
  57. void updateNextTokLoc(SourceLocation Loc);
  58. };
  59. namespace CodeGen {
  60. class CodeGenModule;
  61. /// Organizes the cross-function state that is used while generating
  62. /// code coverage mapping data.
  63. class CoverageMappingModuleGen {
  64. /// Information needed to emit a coverage record for a function.
  65. struct FunctionInfo {
  66. uint64_t NameHash;
  67. uint64_t FuncHash;
  68. std::string CoverageMapping;
  69. bool IsUsed;
  70. };
  71. CodeGenModule &CGM;
  72. CoverageSourceInfo &SourceInfo;
  73. llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
  74. std::vector<llvm::Constant *> FunctionNames;
  75. std::vector<FunctionInfo> FunctionRecords;
  76. std::map<std::string, std::string> CoveragePrefixMap;
  77. std::string getCurrentDirname();
  78. std::string normalizeFilename(StringRef Filename);
  79. /// Emit a function record.
  80. void emitFunctionMappingRecord(const FunctionInfo &Info,
  81. uint64_t FilenamesRef);
  82. public:
  83. static CoverageSourceInfo *setUpCoverageCallbacks(Preprocessor &PP);
  84. CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo);
  85. CoverageSourceInfo &getSourceInfo() const {
  86. return SourceInfo;
  87. }
  88. /// Add a function's coverage mapping record to the collection of the
  89. /// function mapping records.
  90. void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
  91. StringRef FunctionNameValue,
  92. uint64_t FunctionHash,
  93. const std::string &CoverageMapping,
  94. bool IsUsed = true);
  95. /// Emit the coverage mapping data for a translation unit.
  96. void emit();
  97. /// Return the coverage mapping translation unit file id
  98. /// for the given file.
  99. unsigned getFileID(const FileEntry *File);
  100. /// Return an interface into CodeGenModule.
  101. CodeGenModule &getCodeGenModule() { return CGM; }
  102. };
  103. /// Organizes the per-function state that is used while generating
  104. /// code coverage mapping data.
  105. class CoverageMappingGen {
  106. CoverageMappingModuleGen &CVM;
  107. SourceManager &SM;
  108. const LangOptions &LangOpts;
  109. llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
  110. public:
  111. CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
  112. const LangOptions &LangOpts)
  113. : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
  114. CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
  115. const LangOptions &LangOpts,
  116. llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
  117. : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
  118. /// Emit the coverage mapping data which maps the regions of
  119. /// code to counters that will be used to find the execution
  120. /// counts for those regions.
  121. void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
  122. /// Emit the coverage mapping data for an unused function.
  123. /// It creates mapping regions with the counter of zero.
  124. void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
  125. };
  126. } // end namespace CodeGen
  127. } // end namespace clang
  128. #endif