1 //===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Instrumentation-based code coverage mapping generator
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
15 #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
17 #include "clang/Basic/LLVM.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Frontend/CodeGenOptions.h"
20 #include "clang/Lex/PPCallbacks.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/IR/GlobalValue.h"
24 #include "llvm/Support/raw_ostream.h"
35 /// \brief Stores additional source code information like skipped ranges which
36 /// is required by the coverage mapping generator and is obtained from
38 class CoverageSourceInfo : public PPCallbacks {
39 std::vector<SourceRange> SkippedRanges;
41 ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
43 void SourceRangeSkipped(SourceRange Range) override;
50 /// \brief Organizes the cross-function state that is used while generating
51 /// code coverage mapping data.
52 class CoverageMappingModuleGen {
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;
61 CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
62 : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {}
64 CoverageSourceInfo &getSourceInfo() const {
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 uint64_t FunctionHash,
73 const std::string &CoverageMapping);
75 /// \brief Emit the coverage mapping data for a translation unit.
78 /// \brief Return the coverage mapping translation unit file id
79 /// for the given file.
80 unsigned getFileID(const FileEntry *File);
83 /// \brief Organizes the per-function state that is used while generating
84 /// code coverage mapping data.
85 class CoverageMappingGen {
86 CoverageMappingModuleGen &CVM;
88 const LangOptions &LangOpts;
89 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
92 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
93 const LangOptions &LangOpts)
94 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
96 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
97 const LangOptions &LangOpts,
98 llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
99 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
101 /// \brief Emit the coverage mapping data which maps the regions of
102 /// code to counters that will be used to find the execution
103 /// counts for those regions.
104 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
106 /// \brief Emit the coverage mapping data for an unused function.
107 /// It creates mapping regions with the counter of zero.
108 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
111 } // end namespace CodeGen
112 } // end namespace clang