clang  11.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 "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 namespace clang {
24 
25 class LangOptions;
26 class SourceManager;
27 class FileEntry;
28 class Preprocessor;
29 class Decl;
30 class Stmt;
31 
32 /// Stores additional source code information like skipped ranges which
33 /// is required by the coverage mapping generator and is obtained from
34 /// the preprocessor.
36  std::vector<SourceRange> SkippedRanges;
37 public:
38  ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
39 
40  void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
41 };
42 
43 namespace CodeGen {
44 
45 class CodeGenModule;
46 
47 /// Organizes the cross-function state that is used while generating
48 /// code coverage mapping data.
50  /// Information needed to emit a coverage record for a function.
51  struct FunctionInfo {
52  uint64_t NameHash;
53  uint64_t FuncHash;
54  std::string CoverageMapping;
55  bool IsUsed;
56  };
57 
58  CodeGenModule &CGM;
59  CoverageSourceInfo &SourceInfo;
60  llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
61  std::vector<llvm::Constant *> FunctionNames;
62  std::vector<FunctionInfo> FunctionRecords;
63 
64  /// Emit a function record.
65  void emitFunctionMappingRecord(const FunctionInfo &Info,
66  uint64_t FilenamesRef);
67 
68 public:
70  : CGM(CGM), SourceInfo(SourceInfo) {}
71 
73  return SourceInfo;
74  }
75 
76  /// Add a function's coverage mapping record to the collection of the
77  /// function mapping records.
78  void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
79  StringRef FunctionNameValue,
80  uint64_t FunctionHash,
81  const std::string &CoverageMapping,
82  bool IsUsed = true);
83 
84  /// Emit the coverage mapping data for a translation unit.
85  void emit();
86 
87  /// Return the coverage mapping translation unit file id
88  /// for the given file.
89  unsigned getFileID(const FileEntry *File);
90 };
91 
92 /// Organizes the per-function state that is used while generating
93 /// code coverage mapping data.
97  const LangOptions &LangOpts;
98  llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
99 
100 public:
102  const LangOptions &LangOpts)
103  : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
104 
106  const LangOptions &LangOpts,
107  llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
108  : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
109 
110  /// Emit the coverage mapping data which maps the regions of
111  /// code to counters that will be used to find the execution
112  /// counts for those regions.
113  void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
114 
115  /// Emit the coverage mapping data for an unused function.
116  /// It creates mapping regions with the counter of zero.
117  void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
118 };
119 
120 } // end namespace CodeGen
121 } // end namespace clang
122 
123 #endif
Organizes the per-function state that is used while generating code coverage mapping data...
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts, llvm::DenseMap< const Stmt *, unsigned > *CounterMap)
Stores additional source code information like skipped ranges which is required by the coverage mappi...
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Definition: PPCallbacks.h:35
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:54
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Organizes the cross-function state that is used while generating code coverage mapping data...
void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override
Hook called when a source range is skipped.
CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
#define SM(sm)
Definition: Cuda.cpp:62
Encodes a location in the source.
ArrayRef< SourceRange > getSkippedRanges() const
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:79
CoverageSourceInfo & getSourceInfo() const
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts)
Defines the PPCallbacks interface.
Defines the clang::SourceLocation class and associated facilities.
A trivial tuple used to represent a source range.
This class handles loading and caching of source files into memory.