blob: 2bdc00e256689f997ba0945e30941282af5ddb6b [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;
Reid Kleckner9d4806a2019-10-10 18:01:20 +000057 SmallString<256> CWD;
58
59 /// Make the filename absolute, remove dots, and normalize slashes to local
60 /// path style.
61 std::string normalizeFilename(StringRef Filename);
Alex Lorenzee024992014-08-04 18:41:51 +000062
63public:
Reid Kleckner9d4806a2019-10-10 18:01:20 +000064 CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo);
Alex Lorenzee024992014-08-04 18:41:51 +000065
66 CoverageSourceInfo &getSourceInfo() const {
67 return SourceInfo;
68 }
69
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000070 /// Add a function's coverage mapping record to the collection of the
Alex Lorenzee024992014-08-04 18:41:51 +000071 /// function mapping records.
72 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
Alex Lorenzf2cf38e2014-08-08 23:41:24 +000073 StringRef FunctionNameValue,
Alex Lorenz1d45c5b2014-08-21 19:25:27 +000074 uint64_t FunctionHash,
Xinliang David Li2129ae52016-01-07 20:05:55 +000075 const std::string &CoverageMapping,
Xinliang David Li848da132016-01-19 00:49:06 +000076 bool IsUsed = true);
Alex Lorenzee024992014-08-04 18:41:51 +000077
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000078 /// Emit the coverage mapping data for a translation unit.
Alex Lorenzee024992014-08-04 18:41:51 +000079 void emit();
80
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000081 /// Return the coverage mapping translation unit file id
Alex Lorenzee024992014-08-04 18:41:51 +000082 /// for the given file.
83 unsigned getFileID(const FileEntry *File);
84};
85
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000086/// Organizes the per-function state that is used while generating
Alex Lorenzee024992014-08-04 18:41:51 +000087/// code coverage mapping data.
88class CoverageMappingGen {
89 CoverageMappingModuleGen &CVM;
90 SourceManager &SM;
91 const LangOptions &LangOpts;
92 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
Alex Lorenzee024992014-08-04 18:41:51 +000093
94public:
95 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
96 const LangOptions &LangOpts)
Justin Bognere5ee6c52014-10-02 16:44:01 +000097 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
Alex Lorenzee024992014-08-04 18:41:51 +000098
99 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
100 const LangOptions &LangOpts,
Justin Bognere5ee6c52014-10-02 16:44:01 +0000101 llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
102 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
Alex Lorenzee024992014-08-04 18:41:51 +0000103
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000104 /// Emit the coverage mapping data which maps the regions of
Alex Lorenzee024992014-08-04 18:41:51 +0000105 /// code to counters that will be used to find the execution
106 /// counts for those regions.
107 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
108
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000109 /// Emit the coverage mapping data for an unused function.
Alex Lorenzee024992014-08-04 18:41:51 +0000110 /// It creates mapping regions with the counter of zero.
111 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
112};
113
114} // end namespace CodeGen
115} // end namespace clang
116
117#endif