blob: 5d79d1e6567030ab006b34cfc75d84baed943740 [file] [log] [blame]
Alex Lorenzee024992014-08-04 18:41:51 +00001//===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alex Lorenzee024992014-08-04 18:41:51 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Instrumentation-based code coverage mapping generator
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
14#define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
Alex Lorenzee024992014-08-04 18:41:51 +000015
16#include "clang/Basic/LLVM.h"
17#include "clang/Basic/SourceLocation.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000018#include "clang/Lex/PPCallbacks.h"
Alex Lorenzee024992014-08-04 18:41:51 +000019#include "llvm/ADT/DenseMap.h"
20#include "llvm/IR/GlobalValue.h"
21#include "llvm/Support/raw_ostream.h"
22
23namespace clang {
24
25class LangOptions;
26class SourceManager;
27class FileEntry;
28class Preprocessor;
29class Decl;
30class Stmt;
31
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000032/// Stores additional source code information like skipped ranges which
Alex Lorenzee024992014-08-04 18:41:51 +000033/// is required by the coverage mapping generator and is obtained from
34/// the preprocessor.
35class CoverageSourceInfo : public PPCallbacks {
36 std::vector<SourceRange> SkippedRanges;
37public:
38 ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
39
Vedant Kumar3919a502017-09-11 20:47:42 +000040 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
Alex Lorenzee024992014-08-04 18:41:51 +000041};
42
43namespace CodeGen {
44
45class CodeGenModule;
46
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000047/// Organizes the cross-function state that is used while generating
Alex Lorenzee024992014-08-04 18:41:51 +000048/// code coverage mapping data.
49class CoverageMappingModuleGen {
Vedant Kumardd1ea9d2019-10-21 11:48:38 -070050 /// 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
Alex Lorenzee024992014-08-04 18:41:51 +000058 CodeGenModule &CGM;
59 CoverageSourceInfo &SourceInfo;
60 llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
Xinliang David Li2129ae52016-01-07 20:05:55 +000061 std::vector<llvm::Constant *> FunctionNames;
Vedant Kumardd1ea9d2019-10-21 11:48:38 -070062 std::vector<FunctionInfo> FunctionRecords;
63
64 /// Emit a function record.
65 void emitFunctionMappingRecord(const FunctionInfo &Info,
66 uint64_t FilenamesRef);
Alex Lorenzee024992014-08-04 18:41:51 +000067
68public:
Reid Kleckner7cd595d2019-10-28 14:40:17 -070069 CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
Vedant Kumardd1ea9d2019-10-21 11:48:38 -070070 : CGM(CGM), SourceInfo(SourceInfo) {}
Alex Lorenzee024992014-08-04 18:41:51 +000071
72 CoverageSourceInfo &getSourceInfo() const {
73 return SourceInfo;
74 }
75
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000076 /// Add a function's coverage mapping record to the collection of the
Alex Lorenzee024992014-08-04 18:41:51 +000077 /// function mapping records.
78 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
Alex Lorenzf2cf38e2014-08-08 23:41:24 +000079 StringRef FunctionNameValue,
Alex Lorenz1d45c5b2014-08-21 19:25:27 +000080 uint64_t FunctionHash,
Xinliang David Li2129ae52016-01-07 20:05:55 +000081 const std::string &CoverageMapping,
Xinliang David Li848da132016-01-19 00:49:06 +000082 bool IsUsed = true);
Alex Lorenzee024992014-08-04 18:41:51 +000083
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000084 /// Emit the coverage mapping data for a translation unit.
Alex Lorenzee024992014-08-04 18:41:51 +000085 void emit();
86
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000087 /// Return the coverage mapping translation unit file id
Alex Lorenzee024992014-08-04 18:41:51 +000088 /// for the given file.
89 unsigned getFileID(const FileEntry *File);
90};
91
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000092/// Organizes the per-function state that is used while generating
Alex Lorenzee024992014-08-04 18:41:51 +000093/// code coverage mapping data.
94class CoverageMappingGen {
95 CoverageMappingModuleGen &CVM;
96 SourceManager &SM;
97 const LangOptions &LangOpts;
98 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
Alex Lorenzee024992014-08-04 18:41:51 +000099
100public:
101 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
102 const LangOptions &LangOpts)
Justin Bognere5ee6c52014-10-02 16:44:01 +0000103 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
Alex Lorenzee024992014-08-04 18:41:51 +0000104
105 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
106 const LangOptions &LangOpts,
Justin Bognere5ee6c52014-10-02 16:44:01 +0000107 llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
108 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
Alex Lorenzee024992014-08-04 18:41:51 +0000109
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000110 /// Emit the coverage mapping data which maps the regions of
Alex Lorenzee024992014-08-04 18:41:51 +0000111 /// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000115 /// Emit the coverage mapping data for an unused function.
Alex Lorenzee024992014-08-04 18:41:51 +0000116 /// 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