clang  16.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"
18 #include "clang/Lex/PPCallbacks.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 namespace clang {
25 
26 class LangOptions;
27 class SourceManager;
28 class FileEntry;
29 class Preprocessor;
30 class Decl;
31 class Stmt;
32 
33 struct SkippedRange {
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 
56  RangeKind(K) {}
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 public:
71  // Location of the token parsed before HandleComment is called. This is
72  // updated every time Preprocessor::Lex lexes a new token.
74 
75  CoverageSourceInfo(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
76 
77  std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; }
78 
79  void AddSkippedRange(SourceRange Range, SkippedRange::Kind RangeKind);
80 
81  void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
82 
83  void HandleEmptyline(SourceRange Range) override;
84 
85  bool HandleComment(Preprocessor &PP, SourceRange Range) override;
86 
88 };
89 
90 namespace CodeGen {
91 
92 class CodeGenModule;
93 
94 /// Organizes the cross-function state that is used while generating
95 /// code coverage mapping data.
97  /// Information needed to emit a coverage record for a function.
98  struct FunctionInfo {
99  uint64_t NameHash;
100  uint64_t FuncHash;
101  std::string CoverageMapping;
102  bool IsUsed;
103  };
104 
105  CodeGenModule &CGM;
106  CoverageSourceInfo &SourceInfo;
107  llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
108  std::vector<llvm::Constant *> FunctionNames;
109  std::vector<FunctionInfo> FunctionRecords;
110  std::map<std::string, std::string> CoveragePrefixMap;
111 
112  std::string getCurrentDirname();
113  std::string normalizeFilename(StringRef Filename);
114 
115  /// Emit a function record.
116  void emitFunctionMappingRecord(const FunctionInfo &Info,
117  uint64_t FilenamesRef);
118 
119 public:
121 
123 
125  return SourceInfo;
126  }
127 
128  /// Add a function's coverage mapping record to the collection of the
129  /// function mapping records.
130  void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
131  StringRef FunctionNameValue,
132  uint64_t FunctionHash,
133  const std::string &CoverageMapping,
134  bool IsUsed = true);
135 
136  /// Emit the coverage mapping data for a translation unit.
137  void emit();
138 
139  /// Return the coverage mapping translation unit file id
140  /// for the given file.
141  unsigned getFileID(const FileEntry *File);
142 
143  /// Return an interface into CodeGenModule.
144  CodeGenModule &getCodeGenModule() { return CGM; }
145 };
146 
147 /// Organizes the per-function state that is used while generating
148 /// code coverage mapping data.
151  SourceManager &SM;
152  const LangOptions &LangOpts;
153  llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
154 
155 public:
157  const LangOptions &LangOpts)
158  : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
159 
161  const LangOptions &LangOpts,
162  llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
163  : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
164 
165  /// Emit the coverage mapping data which maps the regions of
166  /// code to counters that will be used to find the execution
167  /// counts for those regions.
168  void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
169 
170  /// Emit the coverage mapping data for an unused function.
171  /// It creates mapping regions with the counter of zero.
172  void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
173 };
174 
175 } // end namespace CodeGen
176 } // end namespace clang
177 
178 #endif
clang::CodeGen::CoverageMappingModuleGen::addFunctionMappingRecord
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.
Definition: CoverageMappingGen.cpp:1658
clang::CodeGen::CoverageMappingGen::CoverageMappingGen
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts, llvm::DenseMap< const Stmt *, unsigned > *CounterMap)
Definition: CoverageMappingGen.h:160
clang::CoverageSourceInfo::getSkippedRanges
std::vector< SkippedRange > & getSkippedRanges()
Definition: CoverageMappingGen.h:77
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:210
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::SkippedRange::Comment
@ Comment
Definition: CoverageMappingGen.h:37
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::PPCallbacks
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
clang::SkippedRange::isEmptyLine
bool isEmptyLine()
Definition: CoverageMappingGen.h:49
clang::CodeGen::CoverageMappingModuleGen::getCodeGenModule
CodeGenModule & getCodeGenModule()
Return an interface into CodeGenModule.
Definition: CoverageMappingGen.h:144
clang::SkippedRange::NextTokLoc
SourceLocation NextTokLoc
Definition: CoverageMappingGen.h:44
Filename
StringRef Filename
Definition: Format.cpp:2715
clang::CodeGen::CoverageMappingGen::emitCounterMapping
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...
Definition: CoverageMappingGen.cpp:1776
clang::SkippedRange::Range
SourceRange Range
Definition: CoverageMappingGen.h:40
clang::CodeGen::CoverageMappingGen::CoverageMappingGen
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts)
Definition: CoverageMappingGen.h:156
clang::CoverageSourceInfo
Stores additional source code information like skipped ranges which is required by the coverage mappi...
Definition: CoverageMappingGen.h:62
clang::CommentHandler
Abstract base class that describes a handler that will receive source ranges for each of the comments...
Definition: Preprocessor.h:2697
clang::CodeGen::CoverageMappingModuleGen::setUpCoverageCallbacks
static CoverageSourceInfo * setUpCoverageCallbacks(Preprocessor &PP)
Definition: CoverageMappingGen.cpp:45
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:636
Preprocessor.h
clang::CodeGen::AlignmentSource::Decl
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
clang::SkippedRange::PrevTokLoc
SourceLocation PrevTokLoc
Definition: CoverageMappingGen.h:42
clang::CoverageSourceInfo::AddSkippedRange
void AddSkippedRange(SourceRange Range, SkippedRange::Kind RangeKind)
Definition: CoverageMappingGen.cpp:63
clang::CoverageSourceInfo::SourceRangeSkipped
void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override
Hook called when a source range is skipped.
Definition: CoverageMappingGen.cpp:74
clang::CodeGen::CoverageMappingModuleGen
Organizes the cross-function state that is used while generating code coverage mapping data.
Definition: CoverageMappingGen.h:96
clang::CodeGen::CoverageMappingModuleGen::emit
void emit()
Emit the coverage mapping data for a translation unit.
Definition: CoverageMappingGen.cpp:1694
hlsl::uint64_t
unsigned long uint64_t
Definition: hlsl_basic_types.h:25
clang::CodeGen::CoverageMappingGen
Organizes the per-function state that is used while generating code coverage mapping data.
Definition: CoverageMappingGen.h:149
clang::EmptylineHandler
Abstract base class that describes a handler that will receive source ranges for empty lines encounte...
Definition: Preprocessor.h:2708
clang::CodeGen::CoverageMappingModuleGen::getSourceInfo
CoverageSourceInfo & getSourceInfo() const
Definition: CoverageMappingGen.h:124
clang::CoverageSourceInfo::updateNextTokLoc
void updateNextTokLoc(SourceLocation Loc)
Definition: CoverageMappingGen.cpp:87
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:366
clang::SkippedRange::EmptyLine
@ EmptyLine
Definition: CoverageMappingGen.h:36
clang::SkippedRange::Kind
Kind
Definition: CoverageMappingGen.h:34
SourceLocation.h
clang::SkippedRange
Definition: CoverageMappingGen.h:33
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:279
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
clang::CodeGen::CoverageMappingModuleGen::getFileID
unsigned getFileID(const FileEntry *File)
Return the coverage mapping translation unit file id for the given file.
Definition: CoverageMappingGen.cpp:1767
LLVM.h
clang::CodeGen::CoverageMappingGen::emitEmptyMapping
void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS)
Emit the coverage mapping data for an unused function.
Definition: CoverageMappingGen.cpp:1784
clang::CoverageSourceInfo::HandleComment
bool HandleComment(Preprocessor &PP, SourceRange Range) override
Definition: CoverageMappingGen.cpp:82
clang::SkippedRange::RangeKind
Kind RangeKind
Definition: CoverageMappingGen.h:46
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:81
clang::SkippedRange::PPIfElse
@ PPIfElse
Definition: CoverageMappingGen.h:35
clang::SkippedRange::SkippedRange
SkippedRange(SourceRange Range, Kind K, SourceLocation PrevTokLoc=SourceLocation(), SourceLocation NextTokLoc=SourceLocation())
Definition: CoverageMappingGen.h:52
clang
Definition: CalledOnceCheck.h:17
clang::CoverageSourceInfo::HandleEmptyline
void HandleEmptyline(SourceRange Range) override
Definition: CoverageMappingGen.cpp:78
PPCallbacks.h
clang::SkippedRange::isComment
bool isComment()
Definition: CoverageMappingGen.h:48
clang::CoverageSourceInfo::CoverageSourceInfo
CoverageSourceInfo(SourceManager &SourceMgr)
Definition: CoverageMappingGen.h:75
clang::SkippedRange::isPPIfElse
bool isPPIfElse()
Definition: CoverageMappingGen.h:50
clang::CodeGen::CoverageMappingModuleGen::CoverageMappingModuleGen
CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
Definition: CoverageMappingGen.cpp:1578
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:129
clang::CoverageSourceInfo::PrevTokLoc
SourceLocation PrevTokLoc
Definition: CoverageMappingGen.h:73
SM
#define SM(sm)
Definition: Cuda.cpp:79