clang  14.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 {
35  // The location of token before the skipped source range.
37  // The location of token after the skipped source range.
39 
43 };
44 
45 /// Stores additional source code information like skipped ranges which
46 /// is required by the coverage mapping generator and is obtained from
47 /// the preprocessor.
49  public CommentHandler,
50  public EmptylineHandler {
51  // A vector of skipped source ranges and PrevTokLoc with NextTokLoc.
52  std::vector<SkippedRange> SkippedRanges;
53 
54  SourceManager &SourceMgr;
55 
56 public:
57  // Location of the token parsed before HandleComment is called. This is
58  // updated every time Preprocessor::Lex lexes a new token.
60 
61  CoverageSourceInfo(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
62 
63  std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; }
64 
65  void AddSkippedRange(SourceRange Range);
66 
67  void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
68 
69  void HandleEmptyline(SourceRange Range) override;
70 
71  bool HandleComment(Preprocessor &PP, SourceRange Range) override;
72 
74 };
75 
76 namespace CodeGen {
77 
78 class CodeGenModule;
79 
80 /// Organizes the cross-function state that is used while generating
81 /// code coverage mapping data.
83  /// Information needed to emit a coverage record for a function.
84  struct FunctionInfo {
85  uint64_t NameHash;
86  uint64_t FuncHash;
87  std::string CoverageMapping;
88  bool IsUsed;
89  };
90 
91  CodeGenModule &CGM;
92  CoverageSourceInfo &SourceInfo;
93  llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
94  std::vector<llvm::Constant *> FunctionNames;
95  std::vector<FunctionInfo> FunctionRecords;
96  std::map<std::string, std::string> CoveragePrefixMap;
97 
98  std::string getCurrentDirname();
99  std::string normalizeFilename(StringRef Filename);
100 
101  /// Emit a function record.
102  void emitFunctionMappingRecord(const FunctionInfo &Info,
103  uint64_t FilenamesRef);
104 
105 public:
107 
109 
111  return SourceInfo;
112  }
113 
114  /// Add a function's coverage mapping record to the collection of the
115  /// function mapping records.
116  void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
117  StringRef FunctionNameValue,
118  uint64_t FunctionHash,
119  const std::string &CoverageMapping,
120  bool IsUsed = true);
121 
122  /// Emit the coverage mapping data for a translation unit.
123  void emit();
124 
125  /// Return the coverage mapping translation unit file id
126  /// for the given file.
127  unsigned getFileID(const FileEntry *File);
128 
129  /// Return an interface into CodeGenModule.
130  CodeGenModule &getCodeGenModule() { return CGM; }
131 };
132 
133 /// Organizes the per-function state that is used while generating
134 /// code coverage mapping data.
137  SourceManager &SM;
138  const LangOptions &LangOpts;
139  llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
140 
141 public:
143  const LangOptions &LangOpts)
144  : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
145 
147  const LangOptions &LangOpts,
148  llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
149  : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
150 
151  /// Emit the coverage mapping data which maps the regions of
152  /// code to counters that will be used to find the execution
153  /// counts for those regions.
154  void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
155 
156  /// Emit the coverage mapping data for an unused function.
157  /// It creates mapping regions with the counter of zero.
158  void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
159 };
160 
161 } // end namespace CodeGen
162 } // end namespace clang
163 
164 #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:1640
clang::CodeGen::CoverageMappingGen::CoverageMappingGen
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts, llvm::DenseMap< const Stmt *, unsigned > *CounterMap)
Definition: CoverageMappingGen.h:146
clang::CoverageSourceInfo::getSkippedRanges
std::vector< SkippedRange > & getSkippedRanges()
Definition: CoverageMappingGen.h:63
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:212
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::PPCallbacks
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
clang::CodeGen::CoverageMappingModuleGen::getCodeGenModule
CodeGenModule & getCodeGenModule()
Return an interface into CodeGenModule.
Definition: CoverageMappingGen.h:130
clang::SkippedRange::SkippedRange
SkippedRange(SourceRange Range, SourceLocation PrevTokLoc=SourceLocation(), SourceLocation NextTokLoc=SourceLocation())
Definition: CoverageMappingGen.h:40
clang::SkippedRange::NextTokLoc
SourceLocation NextTokLoc
Definition: CoverageMappingGen.h:38
Filename
StringRef Filename
Definition: Format.cpp:2333
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:1758
clang::SkippedRange::Range
SourceRange Range
Definition: CoverageMappingGen.h:34
clang::CodeGen::CoverageMappingGen::CoverageMappingGen
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts)
Definition: CoverageMappingGen.h:142
clang::CoverageSourceInfo
Stores additional source code information like skipped ranges which is required by the coverage mappi...
Definition: CoverageMappingGen.h:48
clang::CommentHandler
Abstract base class that describes a handler that will receive source ranges for each of the comments...
Definition: Preprocessor.h:2490
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:626
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:36
clang::CoverageSourceInfo::SourceRangeSkipped
void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override
Hook called when a source range is skipped.
Definition: CoverageMappingGen.cpp:73
clang::CodeGen::CoverageMappingModuleGen
Organizes the cross-function state that is used while generating code coverage mapping data.
Definition: CoverageMappingGen.h:82
clang::CodeGen::CoverageMappingModuleGen::emit
void emit()
Emit the coverage mapping data for a translation unit.
Definition: CoverageMappingGen.cpp:1676
clang::CodeGen::CoverageMappingGen
Organizes the per-function state that is used while generating code coverage mapping data.
Definition: CoverageMappingGen.h:135
clang::EmptylineHandler
Abstract base class that describes a handler that will receive source ranges for empty lines encounte...
Definition: Preprocessor.h:2501
clang::CodeGen::CoverageMappingModuleGen::getSourceInfo
CoverageSourceInfo & getSourceInfo() const
Definition: CoverageMappingGen.h:110
clang::CoverageSourceInfo::updateNextTokLoc
void updateNextTokLoc(SourceLocation Loc)
Definition: CoverageMappingGen.cpp:86
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:331
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:284
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::CoverageSourceInfo::AddSkippedRange
void AddSkippedRange(SourceRange Range)
Definition: CoverageMappingGen.cpp:63
clang::CodeGen::CoverageMappingModuleGen::getFileID
unsigned getFileID(const FileEntry *File)
Return the coverage mapping translation unit file id for the given file.
Definition: CoverageMappingGen.cpp:1749
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:1766
clang::CoverageSourceInfo::HandleComment
bool HandleComment(Preprocessor &PP, SourceRange Range) override
Definition: CoverageMappingGen.cpp:81
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:58
clang
Definition: CalledOnceCheck.h:17
clang::CoverageSourceInfo::HandleEmptyline
void HandleEmptyline(SourceRange Range) override
Definition: CoverageMappingGen.cpp:77
PPCallbacks.h
clang::CoverageSourceInfo::CoverageSourceInfo
CoverageSourceInfo(SourceManager &SourceMgr)
Definition: CoverageMappingGen.h:61
clang::CodeGen::CoverageMappingModuleGen::CoverageMappingModuleGen
CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
Definition: CoverageMappingGen.cpp:1560
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:130
clang::CoverageSourceInfo::PrevTokLoc
SourceLocation PrevTokLoc
Definition: CoverageMappingGen.h:59
SM
#define SM(sm)
Definition: Cuda.cpp:78