clang  19.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 namespace MCDC {
95 struct State;
96 }
97 
98 /// Organizes the cross-function state that is used while generating
99 /// code coverage mapping data.
101  /// Information needed to emit a coverage record for a function.
102  struct FunctionInfo {
103  uint64_t NameHash;
104  uint64_t FuncHash;
105  std::string CoverageMapping;
106  bool IsUsed;
107  };
108 
109  CodeGenModule &CGM;
110  CoverageSourceInfo &SourceInfo;
111  llvm::SmallDenseMap<FileEntryRef, unsigned, 8> FileEntries;
112  std::vector<llvm::Constant *> FunctionNames;
113  std::vector<FunctionInfo> FunctionRecords;
114 
115  std::string getCurrentDirname();
116  std::string normalizeFilename(StringRef Filename);
117 
118  /// Emit a function record.
119  void emitFunctionMappingRecord(const FunctionInfo &Info,
120  uint64_t FilenamesRef);
121 
122 public:
124 
126 
128  return SourceInfo;
129  }
130 
131  /// Add a function's coverage mapping record to the collection of the
132  /// function mapping records.
133  void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
134  StringRef FunctionNameValue,
135  uint64_t FunctionHash,
136  const std::string &CoverageMapping,
137  bool IsUsed = true);
138 
139  /// Emit the coverage mapping data for a translation unit.
140  void emit();
141 
142  /// Return the coverage mapping translation unit file id
143  /// for the given file.
144  unsigned getFileID(FileEntryRef File);
145 
146  /// Return an interface into CodeGenModule.
147  CodeGenModule &getCodeGenModule() { return CGM; }
148 };
149 
150 /// Organizes the per-function state that is used while generating
151 /// code coverage mapping data.
154  SourceManager &SM;
155  const LangOptions &LangOpts;
156  llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
157  MCDC::State *MCDCState;
158 
159 public:
161  const LangOptions &LangOpts)
162  : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr),
163  MCDCState(nullptr) {}
164 
166  const LangOptions &LangOpts,
167  llvm::DenseMap<const Stmt *, unsigned> *CounterMap,
168  MCDC::State *MCDCState)
169  : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap),
170  MCDCState(MCDCState) {}
171 
172  /// Emit the coverage mapping data which maps the regions of
173  /// code to counters that will be used to find the execution
174  /// counts for those regions.
175  void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
176 
177  /// Emit the coverage mapping data for an unused function.
178  /// It creates mapping regions with the counter of zero.
179  void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
180 };
181 
182 } // end namespace CodeGen
183 } // end namespace clang
184 
185 #endif
#define SM(sm)
Definition: Cuda.cpp:83
StringRef Filename
Definition: Format.cpp:2976
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the PPCallbacks interface.
Defines the clang::Preprocessor interface.
SourceLocation Loc
Definition: SemaObjC.cpp:755
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)
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.
std::vector< SkippedRange > & getSkippedRanges()
void updateNextTokLoc(SourceLocation Loc)
void AddSkippedRange(SourceRange Range, SkippedRange::Kind RangeKind)
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:482
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
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.
unsigned long uint64_t
Per-Function MC/DC state.
Definition: MCDCState.h:28
SourceLocation NextTokLoc
SkippedRange(SourceRange Range, Kind K, SourceLocation PrevTokLoc=SourceLocation(), SourceLocation NextTokLoc=SourceLocation())
SourceLocation PrevTokLoc