1 //===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Instrumentation-based code coverage mapping generator
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
14 #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
15 
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"
23 
24 namespace clang {
25 
26 class LangOptions;
27 class SourceManager;
28 class FileEntry;
29 class Preprocessor;
30 class Decl;
31 class Stmt;
32 
33 struct SkippedRange {
34   SourceRange Range;
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;
39 
40   SkippedRange(SourceRange Range, SourceLocation PrevTokLoc = SourceLocation(),
41                SourceLocation NextTokLoc = SourceLocation())
42       : Range(Range), PrevTokLoc(PrevTokLoc), NextTokLoc(NextTokLoc) {}
43 };
44 
45 /// Stores additional source code information like skipped ranges which
46 /// is required by the coverage mapping generator and is obtained from
47 /// the preprocessor.
48 class CoverageSourceInfo : public PPCallbacks, public CommentHandler {
49   // A vector of skipped source ranges and PrevTokLoc with NextTokLoc.
50   std::vector<SkippedRange> SkippedRanges;
51   bool AfterComment = false;
52 
53 public:
54   // Location of the token parsed before HandleComment is called. This is
55   // updated every time Preprocessor::Lex lexes a new token.
56   SourceLocation PrevTokLoc;
57   // The location of token before comment.
58   SourceLocation BeforeCommentLoc;
59 
60   std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; }
61 
62   void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
63 
64   bool HandleComment(Preprocessor &PP, SourceRange Range) override;
65 
66   void updateNextTokLoc(SourceLocation Loc);
67 };
68 
69 namespace CodeGen {
70 
71 class CodeGenModule;
72 
73 /// Organizes the cross-function state that is used while generating
74 /// code coverage mapping data.
75 class CoverageMappingModuleGen {
76   /// Information needed to emit a coverage record for a function.
77   struct FunctionInfo {
78     uint64_t NameHash;
79     uint64_t FuncHash;
80     std::string CoverageMapping;
81     bool IsUsed;
82   };
83 
84   CodeGenModule &CGM;
85   CoverageSourceInfo &SourceInfo;
86   llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
87   std::vector<llvm::Constant *> FunctionNames;
88   std::vector<FunctionInfo> FunctionRecords;
89 
90   /// Emit a function record.
91   void emitFunctionMappingRecord(const FunctionInfo &Info,
92                                  uint64_t FilenamesRef);
93 
94 public:
95   static CoverageSourceInfo *setUpCoverageCallbacks(Preprocessor &PP);
96 
97   CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
98       : CGM(CGM), SourceInfo(SourceInfo) {}
99 
100   CoverageSourceInfo &getSourceInfo() const {
101     return SourceInfo;
102   }
103 
104   /// Add a function's coverage mapping record to the collection of the
105   /// function mapping records.
106   void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
107                                 StringRef FunctionNameValue,
108                                 uint64_t FunctionHash,
109                                 const std::string &CoverageMapping,
110                                 bool IsUsed = true);
111 
112   /// Emit the coverage mapping data for a translation unit.
113   void emit();
114 
115   /// Return the coverage mapping translation unit file id
116   /// for the given file.
117   unsigned getFileID(const FileEntry *File);
118 };
119 
120 /// Organizes the per-function state that is used while generating
121 /// code coverage mapping data.
122 class CoverageMappingGen {
123   CoverageMappingModuleGen &CVM;
124   SourceManager &SM;
125   const LangOptions &LangOpts;
126   llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
127 
128 public:
129   CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
130                      const LangOptions &LangOpts)
131       : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
132 
133   CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
134                      const LangOptions &LangOpts,
135                      llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
136       : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
137 
138   /// Emit the coverage mapping data which maps the regions of
139   /// code to counters that will be used to find the execution
140   /// counts for those regions.
141   void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
142 
143   /// Emit the coverage mapping data for an unused function.
144   /// It creates mapping regions with the counter of zero.
145   void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
146 };
147 
148 } // end namespace CodeGen
149 } // end namespace clang
150 
151 #endif
152