1 //===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // Instrumentation-based code coverage mapping generator
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
14 #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Basic/SourceLocation.h"
18 #include "clang/Lex/PPCallbacks.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/Support/raw_ostream.h"
35 // The location of token before the skipped source range.
36 SourceLocation PrevTokLoc;
37 // The location of token after the skipped source range.
38 SourceLocation NextTokLoc;
40 SkippedRange(SourceRange Range, SourceLocation PrevTokLoc = SourceLocation(),
41 SourceLocation NextTokLoc = SourceLocation())
42 : Range(Range), PrevTokLoc(PrevTokLoc), NextTokLoc(NextTokLoc) {}
45 /// Stores additional source code information like skipped ranges which
46 /// is required by the coverage mapping generator and is obtained from
48 class CoverageSourceInfo : public PPCallbacks,
49 public CommentHandler,
50 public EmptylineHandler {
51 // A vector of skipped source ranges and PrevTokLoc with NextTokLoc.
52 std::vector<SkippedRange> SkippedRanges;
54 SourceManager &SourceMgr;
57 // Location of the token parsed before HandleComment is called. This is
58 // updated every time Preprocessor::Lex lexes a new token.
59 SourceLocation PrevTokLoc;
61 CoverageSourceInfo(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
63 std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; }
65 void AddSkippedRange(SourceRange Range);
67 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
69 void HandleEmptyline(SourceRange Range) override;
71 bool HandleComment(Preprocessor &PP, SourceRange Range) override;
73 void updateNextTokLoc(SourceLocation Loc);
80 /// Organizes the cross-function state that is used while generating
81 /// code coverage mapping data.
82 class CoverageMappingModuleGen {
83 /// Information needed to emit a coverage record for a function.
87 std::string CoverageMapping;
92 CoverageSourceInfo &SourceInfo;
93 llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
94 std::vector<llvm::Constant *> FunctionNames;
95 std::vector<FunctionInfo> FunctionRecords;
96 std::map<std::string, std::string> CoveragePrefixMap;
98 std::string getCurrentDirname();
99 std::string normalizeFilename(StringRef Filename);
101 /// Emit a function record.
102 void emitFunctionMappingRecord(const FunctionInfo &Info,
103 uint64_t FilenamesRef);
106 static CoverageSourceInfo *setUpCoverageCallbacks(Preprocessor &PP);
108 CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo);
110 CoverageSourceInfo &getSourceInfo() const {
114 /// Add a function's coverage mapping record to the collection of the
115 /// function mapping records.
116 void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
117 StringRef FunctionNameValue,
118 uint64_t FunctionHash,
119 const std::string &CoverageMapping,
122 /// Emit the coverage mapping data for a translation unit.
125 /// Return the coverage mapping translation unit file id
126 /// for the given file.
127 unsigned getFileID(const FileEntry *File);
129 /// Return an interface into CodeGenModule.
130 CodeGenModule &getCodeGenModule() { return CGM; }
133 /// Organizes the per-function state that is used while generating
134 /// code coverage mapping data.
135 class CoverageMappingGen {
136 CoverageMappingModuleGen &CVM;
138 const LangOptions &LangOpts;
139 llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
142 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
143 const LangOptions &LangOpts)
144 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
146 CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
147 const LangOptions &LangOpts,
148 llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
149 : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
151 /// Emit the coverage mapping data which maps the regions of
152 /// code to counters that will be used to find the execution
153 /// counts for those regions.
154 void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
156 /// Emit the coverage mapping data for an unused function.
157 /// It creates mapping regions with the counter of zero.
158 void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
161 } // end namespace CodeGen
162 } // end namespace clang