Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 1 | //===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Instrumentation-based code coverage mapping generator |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 13 | #ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H |
| 14 | #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 15 | |
| 16 | #include "clang/Basic/LLVM.h" |
| 17 | #include "clang/Basic/SourceLocation.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 18 | #include "clang/Lex/PPCallbacks.h" |
Zequan Wu | b46176b | 2020-07-22 19:04:59 -0700 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 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 | |
Zequan Wu | b46176b | 2020-07-22 19:04:59 -0700 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 45 | /// Stores additional source code information like skipped ranges which |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 46 | /// is required by the coverage mapping generator and is obtained from |
| 47 | /// the preprocessor. |
Zequan Wu | 9caa3fbe | 2020-09-02 14:50:32 -0700 | [diff] [blame] | 48 | class CoverageSourceInfo : public PPCallbacks, |
| 49 | public CommentHandler, |
| 50 | public EmptylineHandler { |
Zequan Wu | b46176b | 2020-07-22 19:04:59 -0700 | [diff] [blame] | 51 | // A vector of skipped source ranges and PrevTokLoc with NextTokLoc. |
| 52 | std::vector<SkippedRange> SkippedRanges; |
Zequan Wu | 9caa3fbe | 2020-09-02 14:50:32 -0700 | [diff] [blame] | 53 | |
| 54 | SourceManager &SourceMgr; |
Zequan Wu | b46176b | 2020-07-22 19:04:59 -0700 | [diff] [blame] | 55 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 56 | public: |
Zequan Wu | b46176b | 2020-07-22 19:04:59 -0700 | [diff] [blame] | 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; |
Zequan Wu | 9caa3fbe | 2020-09-02 14:50:32 -0700 | [diff] [blame] | 60 | |
| 61 | CoverageSourceInfo(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {} |
Zequan Wu | b46176b | 2020-07-22 19:04:59 -0700 | [diff] [blame] | 62 | |
| 63 | std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; } |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 64 | |
Zequan Wu | 9caa3fbe | 2020-09-02 14:50:32 -0700 | [diff] [blame] | 65 | void AddSkippedRange(SourceRange Range); |
| 66 | |
Vedant Kumar | 3919a50 | 2017-09-11 20:47:42 +0000 | [diff] [blame] | 67 | void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override; |
Zequan Wu | b46176b | 2020-07-22 19:04:59 -0700 | [diff] [blame] | 68 | |
Zequan Wu | 9caa3fbe | 2020-09-02 14:50:32 -0700 | [diff] [blame] | 69 | void HandleEmptyline(SourceRange Range) override; |
| 70 | |
Zequan Wu | b46176b | 2020-07-22 19:04:59 -0700 | [diff] [blame] | 71 | bool HandleComment(Preprocessor &PP, SourceRange Range) override; |
| 72 | |
| 73 | void updateNextTokLoc(SourceLocation Loc); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | namespace CodeGen { |
| 77 | |
| 78 | class CodeGenModule; |
| 79 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 80 | /// Organizes the cross-function state that is used while generating |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 81 | /// code coverage mapping data. |
| 82 | class CoverageMappingModuleGen { |
Vedant Kumar | dd1ea9d | 2019-10-21 11:48:38 -0700 | [diff] [blame] | 83 | /// Information needed to emit a coverage record for a function. |
| 84 | struct FunctionInfo { |
| 85 | uint64_t NameHash; |
| 86 | uint64_t FuncHash; |
| 87 | std::string CoverageMapping; |
| 88 | bool IsUsed; |
| 89 | }; |
| 90 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 91 | CodeGenModule &CGM; |
| 92 | CoverageSourceInfo &SourceInfo; |
| 93 | llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries; |
Xinliang David Li | 2129ae5 | 2016-01-07 20:05:55 +0000 | [diff] [blame] | 94 | std::vector<llvm::Constant *> FunctionNames; |
Vedant Kumar | dd1ea9d | 2019-10-21 11:48:38 -0700 | [diff] [blame] | 95 | std::vector<FunctionInfo> FunctionRecords; |
| 96 | |
| 97 | /// Emit a function record. |
| 98 | void emitFunctionMappingRecord(const FunctionInfo &Info, |
| 99 | uint64_t FilenamesRef); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 100 | |
| 101 | public: |
Zequan Wu | b46176b | 2020-07-22 19:04:59 -0700 | [diff] [blame] | 102 | static CoverageSourceInfo *setUpCoverageCallbacks(Preprocessor &PP); |
| 103 | |
Reid Kleckner | 7cd595d | 2019-10-28 14:40:17 -0700 | [diff] [blame] | 104 | CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo) |
Vedant Kumar | dd1ea9d | 2019-10-21 11:48:38 -0700 | [diff] [blame] | 105 | : CGM(CGM), SourceInfo(SourceInfo) {} |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 106 | |
| 107 | CoverageSourceInfo &getSourceInfo() const { |
| 108 | return SourceInfo; |
| 109 | } |
| 110 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 111 | /// Add a function's coverage mapping record to the collection of the |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 112 | /// function mapping records. |
| 113 | void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName, |
Alex Lorenz | f2cf38e | 2014-08-08 23:41:24 +0000 | [diff] [blame] | 114 | StringRef FunctionNameValue, |
Alex Lorenz | 1d45c5b | 2014-08-21 19:25:27 +0000 | [diff] [blame] | 115 | uint64_t FunctionHash, |
Xinliang David Li | 2129ae5 | 2016-01-07 20:05:55 +0000 | [diff] [blame] | 116 | const std::string &CoverageMapping, |
Xinliang David Li | 848da13 | 2016-01-19 00:49:06 +0000 | [diff] [blame] | 117 | bool IsUsed = true); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 118 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 119 | /// Emit the coverage mapping data for a translation unit. |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 120 | void emit(); |
| 121 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 122 | /// Return the coverage mapping translation unit file id |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 123 | /// for the given file. |
| 124 | unsigned getFileID(const FileEntry *File); |
| 125 | }; |
| 126 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 127 | /// Organizes the per-function state that is used while generating |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 128 | /// code coverage mapping data. |
| 129 | class CoverageMappingGen { |
| 130 | CoverageMappingModuleGen &CVM; |
| 131 | SourceManager &SM; |
| 132 | const LangOptions &LangOpts; |
| 133 | llvm::DenseMap<const Stmt *, unsigned> *CounterMap; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 134 | |
| 135 | public: |
| 136 | CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, |
| 137 | const LangOptions &LangOpts) |
Justin Bogner | e5ee6c5 | 2014-10-02 16:44:01 +0000 | [diff] [blame] | 138 | : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {} |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 139 | |
| 140 | CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, |
| 141 | const LangOptions &LangOpts, |
Justin Bogner | e5ee6c5 | 2014-10-02 16:44:01 +0000 | [diff] [blame] | 142 | llvm::DenseMap<const Stmt *, unsigned> *CounterMap) |
| 143 | : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {} |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 144 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 145 | /// Emit the coverage mapping data which maps the regions of |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 146 | /// code to counters that will be used to find the execution |
| 147 | /// counts for those regions. |
| 148 | void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS); |
| 149 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 150 | /// Emit the coverage mapping data for an unused function. |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 151 | /// It creates mapping regions with the counter of zero. |
| 152 | void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS); |
| 153 | }; |
| 154 | |
| 155 | } // end namespace CodeGen |
| 156 | } // end namespace clang |
| 157 | |
| 158 | #endif |