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" |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/IR/GlobalValue.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | |
| 23 | namespace clang { |
| 24 | |
| 25 | class LangOptions; |
| 26 | class SourceManager; |
| 27 | class FileEntry; |
| 28 | class Preprocessor; |
| 29 | class Decl; |
| 30 | class Stmt; |
| 31 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 32 | /// Stores additional source code information like skipped ranges which |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 33 | /// is required by the coverage mapping generator and is obtained from |
| 34 | /// the preprocessor. |
| 35 | class CoverageSourceInfo : public PPCallbacks { |
| 36 | std::vector<SourceRange> SkippedRanges; |
| 37 | public: |
| 38 | ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; } |
| 39 | |
Vedant Kumar | 3919a50 | 2017-09-11 20:47:42 +0000 | [diff] [blame] | 40 | void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | namespace CodeGen { |
| 44 | |
| 45 | class CodeGenModule; |
| 46 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 47 | /// Organizes the cross-function state that is used while generating |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 48 | /// code coverage mapping data. |
| 49 | class CoverageMappingModuleGen { |
| 50 | CodeGenModule &CGM; |
| 51 | CoverageSourceInfo &SourceInfo; |
| 52 | llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries; |
| 53 | std::vector<llvm::Constant *> FunctionRecords; |
Xinliang David Li | 2129ae5 | 2016-01-07 20:05:55 +0000 | [diff] [blame] | 54 | std::vector<llvm::Constant *> FunctionNames; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 55 | llvm::StructType *FunctionRecordTy; |
Vedant Kumar | ca3326c | 2016-01-21 19:25:35 +0000 | [diff] [blame] | 56 | std::vector<std::string> CoverageMappings; |
Reid Kleckner | 9d4806a | 2019-10-10 18:01:20 +0000 | [diff] [blame] | 57 | 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 Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 62 | |
| 63 | public: |
Reid Kleckner | 9d4806a | 2019-10-10 18:01:20 +0000 | [diff] [blame] | 64 | CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 65 | |
| 66 | CoverageSourceInfo &getSourceInfo() const { |
| 67 | return SourceInfo; |
| 68 | } |
| 69 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 70 | /// Add a function's coverage mapping record to the collection of the |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 71 | /// function mapping records. |
| 72 | void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName, |
Alex Lorenz | f2cf38e | 2014-08-08 23:41:24 +0000 | [diff] [blame] | 73 | StringRef FunctionNameValue, |
Alex Lorenz | 1d45c5b | 2014-08-21 19:25:27 +0000 | [diff] [blame] | 74 | uint64_t FunctionHash, |
Xinliang David Li | 2129ae5 | 2016-01-07 20:05:55 +0000 | [diff] [blame] | 75 | const std::string &CoverageMapping, |
Xinliang David Li | 848da13 | 2016-01-19 00:49:06 +0000 | [diff] [blame] | 76 | bool IsUsed = true); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 77 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 78 | /// Emit the coverage mapping data for a translation unit. |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 79 | void emit(); |
| 80 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 81 | /// Return the coverage mapping translation unit file id |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 82 | /// for the given file. |
| 83 | unsigned getFileID(const FileEntry *File); |
| 84 | }; |
| 85 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 86 | /// Organizes the per-function state that is used while generating |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 87 | /// code coverage mapping data. |
| 88 | class CoverageMappingGen { |
| 89 | CoverageMappingModuleGen &CVM; |
| 90 | SourceManager &SM; |
| 91 | const LangOptions &LangOpts; |
| 92 | llvm::DenseMap<const Stmt *, unsigned> *CounterMap; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 93 | |
| 94 | public: |
| 95 | CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, |
| 96 | const LangOptions &LangOpts) |
Justin Bogner | e5ee6c5 | 2014-10-02 16:44:01 +0000 | [diff] [blame] | 97 | : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {} |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 98 | |
| 99 | CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, |
| 100 | const LangOptions &LangOpts, |
Justin Bogner | e5ee6c5 | 2014-10-02 16:44:01 +0000 | [diff] [blame] | 101 | llvm::DenseMap<const Stmt *, unsigned> *CounterMap) |
| 102 | : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {} |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 103 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 104 | /// Emit the coverage mapping data which maps the regions of |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 105 | /// 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 Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 109 | /// Emit the coverage mapping data for an unused function. |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 110 | /// 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 |