Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 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 LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H |
| 15 | #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H |
| 16 | |
| 17 | #include "clang/Basic/LLVM.h" |
| 18 | #include "clang/Basic/SourceLocation.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 19 | #include "clang/Frontend/CodeGenOptions.h" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 20 | #include "clang/Lex/PPCallbacks.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 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; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 57 | std::vector<llvm::Constant *> FunctionNames; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 58 | llvm::StructType *FunctionRecordTy; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 59 | std::vector<std::string> CoverageMappings; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 60 | |
| 61 | public: |
| 62 | CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo) |
| 63 | : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {} |
| 64 | |
| 65 | CoverageSourceInfo &getSourceInfo() const { |
| 66 | return SourceInfo; |
| 67 | } |
| 68 | |
| 69 | /// \brief Add a function's coverage mapping record to the collection of the |
| 70 | /// function mapping records. |
| 71 | void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName, |
| 72 | StringRef FunctionNameValue, |
| 73 | uint64_t FunctionHash, |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 74 | const std::string &CoverageMapping, |
| 75 | bool IsUsed = true); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 76 | |
| 77 | /// \brief Emit the coverage mapping data for a translation unit. |
| 78 | void emit(); |
| 79 | |
| 80 | /// \brief Return the coverage mapping translation unit file id |
| 81 | /// for the given file. |
| 82 | unsigned getFileID(const FileEntry *File); |
| 83 | }; |
| 84 | |
| 85 | /// \brief Organizes the per-function state that is used while generating |
| 86 | /// code coverage mapping data. |
| 87 | class CoverageMappingGen { |
| 88 | CoverageMappingModuleGen &CVM; |
| 89 | SourceManager &SM; |
| 90 | const LangOptions &LangOpts; |
| 91 | llvm::DenseMap<const Stmt *, unsigned> *CounterMap; |
| 92 | |
| 93 | public: |
| 94 | CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, |
| 95 | const LangOptions &LangOpts) |
| 96 | : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {} |
| 97 | |
| 98 | CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, |
| 99 | const LangOptions &LangOpts, |
| 100 | llvm::DenseMap<const Stmt *, unsigned> *CounterMap) |
| 101 | : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {} |
| 102 | |
| 103 | /// \brief Emit the coverage mapping data which maps the regions of |
| 104 | /// code to counters that will be used to find the execution |
| 105 | /// counts for those regions. |
| 106 | void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS); |
| 107 | |
| 108 | /// \brief Emit the coverage mapping data for an unused function. |
| 109 | /// It creates mapping regions with the counter of zero. |
| 110 | void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS); |
| 111 | }; |
| 112 | |
| 113 | } // end namespace CodeGen |
| 114 | } // end namespace clang |
| 115 | |
| 116 | #endif |