clang 24.0.0git
CoverageMappingGen.h
Go to the documentation of this file.
1//===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Instrumentation-based code coverage mapping generator
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
14#define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
15
16#include "clang/Basic/LLVM.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/IR/GlobalValue.h"
22#include "llvm/Support/raw_ostream.h"
23
24namespace clang {
25
26class LangOptions;
27class SourceManager;
28class FileEntry;
29class Preprocessor;
30class Decl;
31class Stmt;
32
34 enum Kind {
35 PPIfElse, // Preprocessor #if/#else ...
38 };
39
41 // The location of token before the skipped source range.
43 // The location of token after the skipped source range.
45 // The nature of this skipped range
47
48 bool isComment() { return RangeKind == Comment; }
49 bool isEmptyLine() { return RangeKind == EmptyLine; }
50 bool isPPIfElse() { return RangeKind == PPIfElse; }
51
57};
58
59/// Stores additional source code information like skipped ranges which
60/// is required by the coverage mapping generator and is obtained from
61/// the preprocessor.
63 public CommentHandler,
64 public EmptylineHandler {
65 // A vector of skipped source ranges and PrevTokLoc with NextTokLoc.
66 std::vector<SkippedRange> SkippedRanges;
67
68 SourceManager &SourceMgr;
69
70 bool EmptyLineCommentCoverage;
71
72public:
73 // Location of the token parsed before HandleComment is called. This is
74 // updated every time Preprocessor::Lex lexes a new token.
76
77 CoverageSourceInfo(SourceManager &SourceMgr, bool EmptyLineCommentCoverage)
78 : SourceMgr(SourceMgr),
79 EmptyLineCommentCoverage(EmptyLineCommentCoverage) {}
80
81 std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; }
82
83 void AddSkippedRange(SourceRange Range, SkippedRange::Kind RangeKind);
84
85 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
86
87 void HandleEmptyline(SourceRange Range) override;
88
89 bool HandleComment(Preprocessor &PP, SourceRange Range) override;
90
92};
93
94namespace CodeGen {
95
96struct CodeGenCLOptions;
97class CodeGenModule;
98class CounterPair;
99
100namespace MCDC {
101struct State;
102}
103
104/// Organizes the cross-function state that is used while generating
105/// code coverage mapping data.
107 /// Information needed to emit a coverage record for a function.
108 struct FunctionInfo {
109 uint64_t NameHash;
110 uint64_t FuncHash;
111 std::string CoverageMapping;
112 bool IsUsed;
113 };
114
115 CodeGenModule &CGM;
116 CoverageSourceInfo &SourceInfo;
117 llvm::SmallDenseMap<FileEntryRef, unsigned, 8> FileEntries;
118 std::vector<llvm::Constant *> FunctionNames;
119 std::vector<FunctionInfo> FunctionRecords;
120
121 std::string getCurrentDirname();
122 std::string normalizeFilename(StringRef Filename);
123
124 /// Emit a function record.
125 void emitFunctionMappingRecord(const FunctionInfo &Info,
126 uint64_t FilenamesRef);
127
128public:
129 static CoverageSourceInfo *
130 setUpCoverageCallbacks(const CodeGenCLOptions &CLOpts, Preprocessor &PP);
131
133
135 return SourceInfo;
136 }
137
138 /// Add a function's coverage mapping record to the collection of the
139 /// function mapping records.
140 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
141 StringRef FunctionNameValue,
142 uint64_t FunctionHash,
143 const std::string &CoverageMapping,
144 bool IsUsed = true);
145
146 /// Emit the coverage mapping data for a translation unit.
147 void emit();
148
149 /// Return the coverage mapping translation unit file id
150 /// for the given file.
151 unsigned getFileID(FileEntryRef File);
152
153 /// Return an interface into CodeGenModule.
155};
156
157/// Organizes the per-function state that is used while generating
158/// code coverage mapping data.
161 SourceManager &SM;
162 const LangOptions &LangOpts;
163 llvm::DenseMap<const Stmt *, CounterPair> *CounterMap;
164 MCDC::State *MCDCState;
165
166public:
168 const LangOptions &LangOpts)
169 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr),
170 MCDCState(nullptr) {}
171
173 const LangOptions &LangOpts,
174 llvm::DenseMap<const Stmt *, CounterPair> *CounterMap,
175 MCDC::State *MCDCState)
176 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap),
177 MCDCState(MCDCState) {}
178
179 /// Emit the coverage mapping data which maps the regions of
180 /// code to counters that will be used to find the execution
181 /// counts for those regions.
182 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
183
184 /// Emit the coverage mapping data for an unused function.
185 /// It creates mapping regions with the counter of zero.
186 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
187};
188
189} // end namespace CodeGen
190} // end namespace clang
191
192#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the PPCallbacks interface.
Defines the clang::Preprocessor interface.
Defines the clang::SourceLocation class and associated facilities.
This class organizes the cross-function state that is used while generating LLVM code.
void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS)
Emit the coverage mapping data for an unused function.
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts)
void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS)
Emit the coverage mapping data which maps the regions of code to counters that will be used to find t...
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts, llvm::DenseMap< const Stmt *, CounterPair > *CounterMap, MCDC::State *MCDCState)
Organizes the cross-function state that is used while generating code coverage mapping data.
void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName, StringRef FunctionNameValue, uint64_t FunctionHash, const std::string &CoverageMapping, bool IsUsed=true)
Add a function's coverage mapping record to the collection of the function mapping records.
CoverageSourceInfo & getSourceInfo() const
static CoverageSourceInfo * setUpCoverageCallbacks(const CodeGenCLOptions &CLOpts, Preprocessor &PP)
CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
void emit()
Emit the coverage mapping data for a translation unit.
CodeGenModule & getCodeGenModule()
Return an interface into CodeGenModule.
unsigned getFileID(FileEntryRef File)
Return the coverage mapping translation unit file id for the given file.
Abstract base class that describes a handler that will receive source ranges for each of the comments...
Stores additional source code information like skipped ranges which is required by the coverage mappi...
void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override
Hook called when a source range is skipped.
void updateNextTokLoc(SourceLocation Loc)
void AddSkippedRange(SourceRange Range, SkippedRange::Kind RangeKind)
std::vector< SkippedRange > & getSkippedRanges()
bool HandleComment(Preprocessor &PP, SourceRange Range) override
CoverageSourceInfo(SourceManager &SourceMgr, bool EmptyLineCommentCoverage)
void HandleEmptyline(SourceRange Range) override
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Abstract base class that describes a handler that will receive source ranges for empty lines encounte...
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:273
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition PPCallbacks.h:37
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
Definition Stmt.h:85
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
Top level wrappers for InstallAPI frontend operations.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
Per-Function MC/DC state.
Definition MCDCState.h:32
SourceLocation NextTokLoc
SkippedRange(SourceRange Range, Kind K, SourceLocation PrevTokLoc=SourceLocation(), SourceLocation NextTokLoc=SourceLocation())
SourceLocation PrevTokLoc