1 //===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Instrumentation-based code coverage mapping generator
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
14 #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Basic/SourceLocation.h"
18 #include "clang/Lex/PPCallbacks.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/Support/raw_ostream.h"
32 /// Stores additional source code information like skipped ranges which
33 /// is required by the coverage mapping generator and is obtained from
35 class CoverageSourceInfo : public PPCallbacks {
36 std::vector<SourceRange> SkippedRanges;
38 ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
40 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
47 /// Organizes the cross-function state that is used while generating
48 /// code coverage mapping data.
49 class CoverageMappingModuleGen {
50 /// Information needed to emit a coverage record for a function.
54 std::string CoverageMapping;
59 CoverageSourceInfo &SourceInfo;
60 llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
61 std::vector<llvm::Constant *> FunctionNames;
62 std::vector<FunctionInfo> FunctionRecords;
64 /// Emit a function record.
65 void emitFunctionMappingRecord(const FunctionInfo &Info,
66 uint64_t FilenamesRef);
69 CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
70 : CGM(CGM), SourceInfo(SourceInfo) {}
72 CoverageSourceInfo &getSourceInfo() const {
76 /// Add a function's coverage mapping record to the collection of the
77 /// function mapping records.
78 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
79 StringRef FunctionNameValue,
80 uint64_t FunctionHash,
81 const std::string &CoverageMapping,
84 /// Emit the coverage mapping data for a translation unit.
87 /// Return the coverage mapping translation unit file id
88 /// for the given file.
89 unsigned getFileID(const FileEntry *File);
92 /// Organizes the per-function state that is used while generating
93 /// code coverage mapping data.
94 class CoverageMappingGen {
95 CoverageMappingModuleGen &CVM;
97 const LangOptions &LangOpts;
98 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
101 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
102 const LangOptions &LangOpts)
103 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
105 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
106 const LangOptions &LangOpts,
107 llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
108 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
110 /// Emit the coverage mapping data which maps the regions of
111 /// code to counters that will be used to find the execution
112 /// counts for those regions.
113 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
115 /// Emit the coverage mapping data for an unused function.
116 /// It creates mapping regions with the counter of zero.
117 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
120 } // end namespace CodeGen
121 } // end namespace clang