blob: b6789c2a79f128ccd971670c496dba2eb1d6ff60 [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"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000019#include "clang/Frontend/CodeGenOptions.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000020#include "clang/Lex/PPCallbacks.h"
Alex Lorenzee024992014-08-04 18:41:51 +000021#include "llvm/ADT/DenseMap.h"
22#include "llvm/IR/GlobalValue.h"
23#include "llvm/Support/raw_ostream.h"
24
25namespace clang {
26
27class LangOptions;
28class SourceManager;
29class FileEntry;
30class Preprocessor;
31class Decl;
32class Stmt;
33
34/// \brief Stores additional source code information like skipped ranges which
35/// is required by the coverage mapping generator and is obtained from
36/// the preprocessor.
37class CoverageSourceInfo : public PPCallbacks {
38 std::vector<SourceRange> SkippedRanges;
39public:
40 ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
41
42 void SourceRangeSkipped(SourceRange Range) override;
43};
44
45namespace CodeGen {
46
47class CodeGenModule;
48
49/// \brief Organizes the cross-function state that is used while generating
50/// code coverage mapping data.
51class CoverageMappingModuleGen {
52 CodeGenModule &CGM;
53 CoverageSourceInfo &SourceInfo;
54 llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
55 std::vector<llvm::Constant *> FunctionRecords;
Xinliang David Li2129ae52016-01-07 20:05:55 +000056 std::vector<llvm::Constant *> FunctionNames;
Alex Lorenzee024992014-08-04 18:41:51 +000057 llvm::StructType *FunctionRecordTy;
Vedant Kumarca3326c2016-01-21 19:25:35 +000058 std::vector<std::string> CoverageMappings;
Alex Lorenzee024992014-08-04 18:41:51 +000059
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,
Xinliang David Li2129ae52016-01-07 20:05:55 +000073 const std::string &CoverageMapping,
Xinliang David Li848da132016-01-19 00:49:06 +000074 bool IsUsed = true);
Alex Lorenzee024992014-08-04 18:41:51 +000075
76 /// \brief Emit the coverage mapping data for a translation unit.
77 void emit();
78
79 /// \brief Return the coverage mapping translation unit file id
80 /// for the given file.
81 unsigned getFileID(const FileEntry *File);
82};
83
84/// \brief Organizes the per-function state that is used while generating
85/// code coverage mapping data.
86class CoverageMappingGen {
87 CoverageMappingModuleGen &CVM;
88 SourceManager &SM;
89 const LangOptions &LangOpts;
90 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
Alex Lorenzee024992014-08-04 18:41:51 +000091
92public:
93 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
94 const LangOptions &LangOpts)
Justin Bognere5ee6c52014-10-02 16:44:01 +000095 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
Alex Lorenzee024992014-08-04 18:41:51 +000096
97 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
98 const LangOptions &LangOpts,
Justin Bognere5ee6c52014-10-02 16:44:01 +000099 llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
100 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
Alex Lorenzee024992014-08-04 18:41:51 +0000101
102 /// \brief Emit the coverage mapping data which maps the regions of
103 /// code to counters that will be used to find the execution
104 /// counts for those regions.
105 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
106
107 /// \brief Emit the coverage mapping data for an unused function.
108 /// It creates mapping regions with the counter of zero.
109 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
110};
111
112} // end namespace CodeGen
113} // end namespace clang
114
115#endif