clang 8.0.0

CoverageMappingGen.h
Go to the documentation of this file.
1 //===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Instrumentation-based code coverage mapping generator
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
15 #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
16
17 #include "clang/Basic/LLVM.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Lex/PPCallbacks.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 /// Stores additional source code information like skipped ranges which
34 /// is required by the coverage mapping generator and is obtained from
35 /// the preprocessor.
36 class CoverageSourceInfo : public PPCallbacks {
37 std::vector<SourceRange> SkippedRanges;
38 public:
39 ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
40
41 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
42 };
43
44 namespace CodeGen {
45
46 class CodeGenModule;
47
48 /// Organizes the cross-function state that is used while generating
49 /// code coverage mapping data.
50 class CoverageMappingModuleGen {
51 CodeGenModule &CGM;
52 CoverageSourceInfo &SourceInfo;
53 llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
54 std::vector<llvm::Constant *> FunctionRecords;
55 std::vector<llvm::Constant *> FunctionNames;
56 llvm::StructType *FunctionRecordTy;
57 std::vector<std::string> CoverageMappings;
58
59 public:
60 CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
61 : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {}
62
63 CoverageSourceInfo &getSourceInfo() const {
64 return SourceInfo;
65 }
66
67 /// Add a function's coverage mapping record to the collection of the
68 /// function mapping records.
69 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
70 StringRef FunctionNameValue,
71 uint64_t FunctionHash,
72 const std::string &CoverageMapping,
73 bool IsUsed = true);
74
75 /// Emit the coverage mapping data for a translation unit.
76 void emit();
77
78 /// Return the coverage mapping translation unit file id
79 /// for the given file.
80 unsigned getFileID(const FileEntry *File);
81 };
82
83 /// Organizes the per-function state that is used while generating
84 /// code coverage mapping data.
85 class CoverageMappingGen {
86 CoverageMappingModuleGen &CVM;
87 SourceManager &SM;
88 const LangOptions &LangOpts;
89 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
90
91 public:
92 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
93 const LangOptions &LangOpts)
94 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
95
96 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
97 const LangOptions &LangOpts,
98 llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
99 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
100
101 /// Emit the coverage mapping data which maps the regions of
102 /// code to counters that will be used to find the execution
103 /// counts for those regions.
104 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
105
106 /// Emit the coverage mapping data for an unused function.
107 /// It creates mapping regions with the counter of zero.
108 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
109 };
110
111 } // end namespace CodeGen
112 } // end namespace clang
113
114 #endif
clang::CodeGen::CoverageMappingGen
Organizes the per-function state that is used while generating code coverage mapping data...
Definition: CoverageMappingGen.h:85
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
clang::CodeGen::CoverageMappingGen::CoverageMappingGen
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts, llvm::DenseMap< const Stmt *, unsigned > *CounterMap)
Definition: CoverageMappingGen.h:96
clang::CoverageSourceInfo
Stores additional source code information like skipped ranges which is required by the coverage mappi...
Definition: CoverageMappingGen.h:36
clang::CodeGen::AlignmentSource::Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
clang::PPCallbacks
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Definition: PPCallbacks.h:36
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:50
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
llvm::ArrayRef
Definition: LLVM.h:32
clang::CodeGen::CoverageMappingModuleGen
Organizes the cross-function state that is used while generating code coverage mapping data...
Definition: CoverageMappingGen.h:50
clang::CoverageSourceInfo::SourceRangeSkipped
void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override
Hook called when a source range is skipped.
Definition: CoverageMappingGen.cpp:32
clang::CodeGen::CoverageMappingModuleGen::CoverageMappingModuleGen
CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
Definition: CoverageMappingGen.h:60
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::CoverageSourceInfo::getSkippedRanges
ArrayRef< SourceRange > getSkippedRanges() const
Definition: CoverageMappingGen.h:39
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:60
clang::CodeGen::CoverageMappingModuleGen::getSourceInfo
CoverageSourceInfo & getSourceInfo() const
Definition: CoverageMappingGen.h:63
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:274
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::CodeGen::CoverageMappingGen::CoverageMappingGen
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts)
Definition: CoverageMappingGen.h:92
PPCallbacks.h
Defines the PPCallbacks interface.
SourceLocation.h
Defines the clang::SourceLocation class and associated facilities.
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:198
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582