clang 20.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/CommandLine.h"
23#include "llvm/Support/raw_ostream.h"
24
25namespace llvm::coverage {
26extern cl::opt<bool> SystemHeadersCoverage;
27}
28
29namespace clang {
30
31class LangOptions;
32class SourceManager;
33class FileEntry;
34class Preprocessor;
35class Decl;
36class Stmt;
37
39 enum Kind {
40 PPIfElse, // Preprocessor #if/#else ...
43 };
44
46 // The location of token before the skipped source range.
48 // The location of token after the skipped source range.
50 // The nature of this skipped range
52
53 bool isComment() { return RangeKind == Comment; }
54 bool isEmptyLine() { return RangeKind == EmptyLine; }
55 bool isPPIfElse() { return RangeKind == PPIfElse; }
56
61 RangeKind(K) {}
62};
63
64/// Stores additional source code information like skipped ranges which
65/// is required by the coverage mapping generator and is obtained from
66/// the preprocessor.
68 public CommentHandler,
69 public EmptylineHandler {
70 // A vector of skipped source ranges and PrevTokLoc with NextTokLoc.
71 std::vector<SkippedRange> SkippedRanges;
72
73 SourceManager &SourceMgr;
74
75public:
76 // Location of the token parsed before HandleComment is called. This is
77 // updated every time Preprocessor::Lex lexes a new token.
79
80 CoverageSourceInfo(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
81
82 std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; }
83
85
86 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
87
88 void HandleEmptyline(SourceRange Range) override;
89
90 bool HandleComment(Preprocessor &PP, SourceRange Range) override;
91
93};
94
95namespace CodeGen {
96
97class CodeGenModule;
98
99namespace MCDC {
100struct State;
101}
102
103/// Organizes the cross-function state that is used while generating
104/// code coverage mapping data.
106 /// Information needed to emit a coverage record for a function.
107 struct FunctionInfo {
108 uint64_t NameHash;
109 uint64_t FuncHash;
110 std::string CoverageMapping;
111 bool IsUsed;
112 };
113
114 CodeGenModule &CGM;
115 CoverageSourceInfo &SourceInfo;
116 llvm::SmallDenseMap<FileEntryRef, unsigned, 8> FileEntries;
117 std::vector<llvm::Constant *> FunctionNames;
118 std::vector<FunctionInfo> FunctionRecords;
119
120 std::string getCurrentDirname();
121 std::string normalizeFilename(StringRef Filename);
122
123 /// Emit a function record.
124 void emitFunctionMappingRecord(const FunctionInfo &Info,
125 uint64_t FilenamesRef);
126
127public:
129
131
133 return SourceInfo;
134 }
135
136 /// Add a function's coverage mapping record to the collection of the
137 /// function mapping records.
138 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
139 StringRef FunctionNameValue,
140 uint64_t FunctionHash,
141 const std::string &CoverageMapping,
142 bool IsUsed = true);
143
144 /// Emit the coverage mapping data for a translation unit.
145 void emit();
146
147 /// Return the coverage mapping translation unit file id
148 /// for the given file.
149 unsigned getFileID(FileEntryRef File);
150
151 /// Return an interface into CodeGenModule.
153};
154
155/// Organizes the per-function state that is used while generating
156/// code coverage mapping data.
159 SourceManager &SM;
160 const LangOptions &LangOpts;
161 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
162 MCDC::State *MCDCState;
163
164public:
166 const LangOptions &LangOpts)
167 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr),
168 MCDCState(nullptr) {}
169
171 const LangOptions &LangOpts,
172 llvm::DenseMap<const Stmt *, unsigned> *CounterMap,
173 MCDC::State *MCDCState)
174 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap),
175 MCDCState(MCDCState) {}
176
177 /// Emit the coverage mapping data which maps the regions of
178 /// code to counters that will be used to find the execution
179 /// counts for those regions.
180 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
181
182 /// Emit the coverage mapping data for an unused function.
183 /// It creates mapping regions with the counter of zero.
184 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
185};
186
187} // end namespace CodeGen
188} // end namespace clang
189
190#endif
#define SM(sm)
Definition: Cuda.cpp:83
const Decl * D
StringRef Filename
Definition: Format.cpp:2989
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the PPCallbacks interface.
Defines the clang::Preprocessor interface.
SourceRange Range
Definition: SemaObjC.cpp:757
SourceLocation Loc
Definition: SemaObjC.cpp:758
Defines the clang::SourceLocation class and associated facilities.
This class organizes the cross-function state that is used while generating LLVM code.
Organizes the per-function state that is used while generating code coverage mapping data.
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 *, unsigned > *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(Preprocessor &PP)
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)
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
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:461
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:36
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:137
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.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
The JSON file list parser is used to communicate input to InstallAPI.
cl::opt< bool > SystemHeadersCoverage
Per-Function MC/DC state.
Definition: MCDCState.h:29
SourceLocation NextTokLoc
SkippedRange(SourceRange Range, Kind K, SourceLocation PrevTokLoc=SourceLocation(), SourceLocation NextTokLoc=SourceLocation())
SourceLocation PrevTokLoc