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