]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/clang/lib/CodeGen/CoverageMappingGen.h
Merge ^/vendor/lld/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / clang / lib / CodeGen / CoverageMappingGen.h
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 "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
32 /// Stores additional source code information like skipped ranges which
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
40   void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override;
41 };
42
43 namespace CodeGen {
44
45 class CodeGenModule;
46
47 /// Organizes the cross-function state that is used while generating
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;
54   std::vector<llvm::Constant *> FunctionNames;
55   llvm::StructType *FunctionRecordTy;
56   std::vector<std::string> CoverageMappings;
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);
62
63 public:
64   CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo);
65
66   CoverageSourceInfo &getSourceInfo() const {
67     return SourceInfo;
68   }
69
70   /// Add a function's coverage mapping record to the collection of the
71   /// function mapping records.
72   void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
73                                 StringRef FunctionNameValue,
74                                 uint64_t FunctionHash,
75                                 const std::string &CoverageMapping,
76                                 bool IsUsed = true);
77
78   /// Emit the coverage mapping data for a translation unit.
79   void emit();
80
81   /// Return the coverage mapping translation unit file id
82   /// for the given file.
83   unsigned getFileID(const FileEntry *File);
84 };
85
86 /// Organizes the per-function state that is used while generating
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;
93
94 public:
95   CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
96                      const LangOptions &LangOpts)
97       : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
98
99   CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
100                      const LangOptions &LangOpts,
101                      llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
102       : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
103
104   /// Emit the coverage mapping data which maps the regions of
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
109   /// Emit the coverage mapping data for an unused function.
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