clang API Documentation

CoverageMappingGen.h
Go to the documentation of this file.
00001 //===---- CoverageMappingGen.h - Coverage mapping generation ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Instrumentation-based code coverage mapping generator
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
00015 #define LLVM_CLANG_LIB_CODEGEN_COVERAGEMAPPINGGEN_H
00016 
00017 #include "clang/Basic/LLVM.h"
00018 #include "clang/Basic/SourceLocation.h"
00019 #include "clang/Lex/PPCallbacks.h"
00020 #include "clang/Frontend/CodeGenOptions.h"
00021 #include "llvm/ADT/StringMap.h"
00022 #include "llvm/ADT/DenseMap.h"
00023 #include "llvm/IR/GlobalValue.h"
00024 #include "llvm/Support/raw_ostream.h"
00025 
00026 namespace clang {
00027 
00028 class LangOptions;
00029 class SourceManager;
00030 class FileEntry;
00031 class Preprocessor;
00032 class Decl;
00033 class Stmt;
00034 
00035 /// \brief Stores additional source code information like skipped ranges which
00036 /// is required by the coverage mapping generator and is obtained from
00037 /// the preprocessor.
00038 class CoverageSourceInfo : public PPCallbacks {
00039   std::vector<SourceRange> SkippedRanges;
00040 public:
00041   ArrayRef<SourceRange> getSkippedRanges() const { return SkippedRanges; }
00042 
00043   void SourceRangeSkipped(SourceRange Range) override;
00044 };
00045 
00046 namespace CodeGen {
00047 
00048 class CodeGenModule;
00049 
00050 /// \brief Organizes the cross-function state that is used while generating
00051 /// code coverage mapping data.
00052 class CoverageMappingModuleGen {
00053   CodeGenModule &CGM;
00054   CoverageSourceInfo &SourceInfo;
00055   llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
00056   std::vector<llvm::Constant *> FunctionRecords;
00057   llvm::StructType *FunctionRecordTy;
00058   std::string CoverageMappings;
00059 
00060 public:
00061   CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
00062       : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {}
00063 
00064   CoverageSourceInfo &getSourceInfo() const {
00065     return SourceInfo;
00066   }
00067 
00068   /// \brief Add a function's coverage mapping record to the collection of the
00069   /// function mapping records.
00070   void addFunctionMappingRecord(llvm::GlobalVariable *FunctionName,
00071                                 StringRef FunctionNameValue,
00072                                 uint64_t FunctionHash,
00073                                 const std::string &CoverageMapping);
00074 
00075   /// \brief Emit the coverage mapping data for a translation unit.
00076   void emit();
00077 
00078   /// \brief Return the coverage mapping translation unit file id
00079   /// for the given file.
00080   unsigned getFileID(const FileEntry *File);
00081 };
00082 
00083 /// \brief Organizes the per-function state that is used while generating
00084 /// code coverage mapping data.
00085 class CoverageMappingGen {
00086   CoverageMappingModuleGen &CVM;
00087   SourceManager &SM;
00088   const LangOptions &LangOpts;
00089   llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
00090 
00091 public:
00092   CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
00093                      const LangOptions &LangOpts)
00094       : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr) {}
00095 
00096   CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
00097                      const LangOptions &LangOpts,
00098                      llvm::DenseMap<const Stmt *, unsigned> *CounterMap)
00099       : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap) {}
00100 
00101   /// \brief Emit the coverage mapping data which maps the regions of
00102   /// code to counters that will be used to find the execution
00103   /// counts for those regions.
00104   void emitCounterMapping(const Decl *D, llvm::raw_ostream &OS);
00105 
00106   /// \brief Emit the coverage mapping data for an unused function.
00107   /// It creates mapping regions with the counter of zero.
00108   void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS);
00109 };
00110 
00111 } // end namespace CodeGen
00112 } // end namespace clang
00113 
00114 #endif