CoverageMappingGen.h 3.9 KB

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