CoverageMappingGen.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===---- CoverageMappingGen.h - 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. #ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
  14. #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
  15. #include "clang/Basic/LLVM.h"
  16. #include "clang/Basic/SourceLocation.h"
  17. #include "clang/Frontend/CodeGenOptions.h"
  18. #include "clang/Lex/PPCallbacks.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/StringMap.h"
  21. #include "llvm/IR/GlobalValue.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. namespace clang {
  24. class LangOptions;
  25. class SourceManager;
  26. class FileEntry;
  27. class Preprocessor;
  28. class Decl;
  29. class Stmt;
  30. /// \brief Stores additional source code information like skipped ranges which
  31. /// is required by the coverage mapping generator and is obtained from
  32. /// the preprocessor.
  33. class CoverageSourceInfo : public PPCallbacks {
  34. std::vector<SourceRange> SkippedRanges;
  35. public:
  36. ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
  37. void SourceRangeSkipped(SourceRange Range) override;
  38. };
  39. namespace CodeGen {
  40. class CodeGenModule;
  41. /// \brief Organizes the cross-function state that is used while generating
  42. /// code coverage mapping data.
  43. class CoverageMappingModuleGen {
  44. CodeGenModule &CGM;
  45. CoverageSourceInfo &SourceInfo;
  46. llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
  47. std::vector<llvm::Constant *> FunctionRecords;
  48. llvm::StructType *FunctionRecordTy;
  49. std::string CoverageMappings;
  50. public:
  51. CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
  52. : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {}
  53. CoverageSourceInfo &getSourceInfo() const {
  54. return SourceInfo;
  55. }
  56. /// \brief Add a function's coverage mapping record to the collection of the
  57. /// function mapping records.
  58. void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
  59. StringRef FunctionNameValue,
  60. uint64_t FunctionHash,
  61. const std::string &CoverageMapping);
  62. /// \brief Emit the coverage mapping data for a translation unit.
  63. void emit();
  64. /// \brief Return the coverage mapping translation unit file id
  65. /// for the given file.
  66. unsigned getFileID(const FileEntry *File);
  67. };
  68. /// \brief Organizes the per-function state that is used while generating
  69. /// code coverage mapping data.
  70. class CoverageMappingGen {
  71. CoverageMappingModuleGen &CVM;
  72. SourceManager &SM;
  73. const LangOptions &LangOpts;
  74. llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
  75. public:
  76. CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
  77. const LangOptions &LangOpts)
  78. : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
  79. CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
  80. const LangOptions &LangOpts,
  81. llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
  82. : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
  83. /// \brief Emit the coverage mapping data which maps the regions of
  84. /// code to counters that will be used to find the execution
  85. /// counts for those regions.
  86. void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
  87. /// \brief Emit the coverage mapping data for an unused function.
  88. /// It creates mapping regions with the counter of zero.
  89. void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
  90. };
  91. } // end namespace CodeGen
  92. } // end namespace clang
  93. #endif