blob: 45e0db8bce3cac07f605bb5afd4b980d513a641b [file] [log] [blame]
Alex Lorenzee024992014-08-04 18:41:51 +00001//===---- 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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
15#define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
Alex Lorenzee024992014-08-04 18:41:51 +000016
17#include "clang/Basic/LLVM.h"
18#include "clang/Basic/SourceLocation.h"
19#include "clang/Lex/PPCallbacks.h"
20#include "clang/Frontend/CodeGenOptions.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/IR/GlobalValue.h"
24#include "llvm/Support/raw_ostream.h"
25
26namespace clang {
27
28class LangOptions;
29class SourceManager;
30class FileEntry;
31class Preprocessor;
32class Decl;
33class Stmt;
34
35/// \brief Stores additional source code information like skipped ranges which
36/// is required by the coverage mapping generator and is obtained from
37/// the preprocessor.
38class CoverageSourceInfo : public PPCallbacks {
39 std::vector<SourceRange> SkippedRanges;
40public:
41 ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
42
43 void SourceRangeSkipped(SourceRange Range) override;
44};
45
46namespace CodeGen {
47
48class CodeGenModule;
49
50/// \brief Organizes the cross-function state that is used while generating
51/// code coverage mapping data.
52class CoverageMappingModuleGen {
53 CodeGenModule &CGM;
54 CoverageSourceInfo &SourceInfo;
55 llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
56 std::vector<llvm::Constant *> FunctionRecords;
57 llvm::StructType *FunctionRecordTy;
58 std::string CoverageMappings;
59
60public:
61 CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
62 : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {}
63
64 CoverageSourceInfo &getSourceInfo() const {
65 return SourceInfo;
66 }
67
68 /// \brief Add a function's coverage mapping record to the collection of the
69 /// function mapping records.
70 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
Alex Lorenzf2cf38e2014-08-08 23:41:24 +000071 StringRef FunctionNameValue,
Alex Lorenz1d45c5b2014-08-21 19:25:27 +000072 uint64_t FunctionHash,
Alex Lorenzee024992014-08-04 18:41:51 +000073 const std::string &CoverageMapping);
74
75 /// \brief Emit the coverage mapping data for a translation unit.
76 void emit();
77
78 /// \brief Return the coverage mapping translation unit file id
79 /// for the given file.
80 unsigned getFileID(const FileEntry *File);
81};
82
83/// \brief Organizes the per-function state that is used while generating
84/// code coverage mapping data.
85class CoverageMappingGen {
86 CoverageMappingModuleGen &CVM;
87 SourceManager &SM;
88 const LangOptions &LangOpts;
89 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
90 unsigned NumRegionCounters;
91
92public:
93 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
94 const LangOptions &LangOpts)
95 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr),
96 NumRegionCounters(0) {}
97
98 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
99 const LangOptions &LangOpts,
100 llvm::DenseMap<const Stmt *, unsigned> *CounterMap,
101 unsigned NumRegionCounters)
102 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap),
103 NumRegionCounters(NumRegionCounters) {}
104
105 /// \brief Emit the coverage mapping data which maps the regions of
106 /// code to counters that will be used to find the execution
107 /// counts for those regions.
108 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
109
110 /// \brief Emit the coverage mapping data for an unused function.
111 /// It creates mapping regions with the counter of zero.
112 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
113};
114
115} // end namespace CodeGen
116} // end namespace clang
117
118#endif