clang 10.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"
17 #include "clang/Basic/SourceLocation.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.
35 class CoverageSourceInfo : public PPCallbacks {
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.
49 class CoverageMappingModuleGen {
50 CodeGenModule &CGM;
51 CoverageSourceInfo &SourceInfo;
52 llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
53 std::vector<llvm::Constant *> FunctionRecords;
54 std::vector<llvm::Constant *> FunctionNames;
55 llvm::StructType *FunctionRecordTy;
56 std::vector<std::string> CoverageMappings;
57
58 public:
59 CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
60 : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {}
61
62 CoverageSourceInfo &getSourceInfo() const {
63 return SourceInfo;
64 }
65
66 /// Add a function's coverage mapping record to the collection of the
67 /// function mapping records.
68 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
69 StringRef FunctionNameValue,
70 uint64_t FunctionHash,
71 const std::string &CoverageMapping,
72 bool IsUsed = true);
73
74 /// Emit the coverage mapping data for a translation unit.
75 void emit();
76
77 /// Return the coverage mapping translation unit file id
78 /// for the given file.
79 unsigned getFileID(const FileEntry *File);
80 };
81
82 /// Organizes the per-function state that is used while generating
83 /// code coverage mapping data.
84 class CoverageMappingGen {
85 CoverageMappingModuleGen &CVM;
86 SourceManager &SM;
87 const LangOptions &LangOpts;
88 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
89
90 public:
91 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
92 const LangOptions &LangOpts)
93 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
94
95 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
96 const LangOptions &LangOpts,
97 llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
98 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
99
100 /// Emit the coverage mapping data which maps the regions of
101 /// code to counters that will be used to find the execution
102 /// counts for those regions.
103 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
104
105 /// Emit the coverage mapping data for an unused function.
106 /// It creates mapping regions with the counter of zero.
107 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
108 };
109
110 } // end namespace CodeGen
111 } // end namespace clang
112
113 #endif
clang::CodeGen::CoverageMappingGen
Organizes the per-function state that is used while generating code coverage mapping data...
Definition: CoverageMappingGen.h:84
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
clang::CodeGen::CoverageMappingGen::CoverageMappingGen
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts, llvm::DenseMap< const Stmt *, unsigned > *CounterMap)
Definition: CoverageMappingGen.h:95
clang::CoverageSourceInfo
Stores additional source code information like skipped ranges which is required by the coverage mappi...
Definition: CoverageMappingGen.h:35
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:35
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:53
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
llvm::ArrayRef
Definition: LLVM.h:31
clang::CodeGen::CoverageMappingModuleGen
Organizes the cross-function state that is used while generating code coverage mapping data...
Definition: CoverageMappingGen.h:49
clang::CoverageSourceInfo::SourceRangeSkipped
void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override
Hook called when a source range is skipped.
Definition: CoverageMappingGen.cpp:31
clang::CodeGen::CoverageMappingModuleGen::CoverageMappingModuleGen
CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
Definition: CoverageMappingGen.h:59
SM
const SourceManager & SM
Definition: Format.cpp:1685
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::CoverageSourceInfo::getSkippedRanges
ArrayRef< SourceRange > getSkippedRanges() const
Definition: CoverageMappingGen.h:38
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:78
clang::CodeGen::CoverageMappingModuleGen::getSourceInfo
CoverageSourceInfo & getSourceInfo() const
Definition: CoverageMappingGen.h:62
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:280
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::CodeGen::CoverageMappingGen::CoverageMappingGen
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts)
Definition: CoverageMappingGen.h:91
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:207
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:596