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