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