blob: 3bf51f590479ff2937d602a3e8a0bd69f165014c [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 {
50 CodeGenModule &CGM;
51 CoverageSourceInfo &SourceInfo;
52 llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
53 std::vector<llvm::Constant *> FunctionRecords;
Xinliang David Li2129ae52016-01-07 20:05:55 +000054 std::vector<llvm::Constant *> FunctionNames;
Alex Lorenzee024992014-08-04 18:41:51 +000055 llvm::StructType *FunctionRecordTy;
Vedant Kumarca3326c2016-01-21 19:25:35 +000056 std::vector<std::string> CoverageMappings;
Alex Lorenzee024992014-08-04 18:41:51 +000057
58public:
59 CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
60 : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {}
61
62 CoverageSourceInfo &getSourceInfo() const {
63 return SourceInfo;
64 }
65
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000066 /// Add a function's coverage mapping record to the collection of the
Alex Lorenzee024992014-08-04 18:41:51 +000067 /// function mapping records.
68 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
Alex Lorenzf2cf38e2014-08-08 23:41:24 +000069 StringRef FunctionNameValue,
Alex Lorenz1d45c5b2014-08-21 19:25:27 +000070 uint64_t FunctionHash,
Xinliang David Li2129ae52016-01-07 20:05:55 +000071 const std::string &CoverageMapping,
Xinliang David Li848da132016-01-19 00:49:06 +000072 bool IsUsed = true);
Alex Lorenzee024992014-08-04 18:41:51 +000073
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000074 /// Emit the coverage mapping data for a translation unit.
Alex Lorenzee024992014-08-04 18:41:51 +000075 void emit();
76
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000077 /// Return the coverage mapping translation unit file id
Alex Lorenzee024992014-08-04 18:41:51 +000078 /// for the given file.
79 unsigned getFileID(const FileEntry *File);
80};
81
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000082/// Organizes the per-function state that is used while generating
Alex Lorenzee024992014-08-04 18:41:51 +000083/// code coverage mapping data.
84class CoverageMappingGen {
85 CoverageMappingModuleGen &CVM;
86 SourceManager &SM;
87 const LangOptions &LangOpts;
88 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
Alex Lorenzee024992014-08-04 18:41:51 +000089
90public:
91 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
92 const LangOptions &LangOpts)
Justin Bognere5ee6c52014-10-02 16:44:01 +000093 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
Alex Lorenzee024992014-08-04 18:41:51 +000094
95 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
96 const LangOptions &LangOpts,
Justin Bognere5ee6c52014-10-02 16:44:01 +000097 llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
98 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
Alex Lorenzee024992014-08-04 18:41:51 +000099
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000100 /// Emit the coverage mapping data which maps the regions of
Alex Lorenzee024992014-08-04 18:41:51 +0000101 /// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000105 /// Emit the coverage mapping data for an unused function.
Alex Lorenzee024992014-08-04 18:41:51 +0000106 /// 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