diff options
Diffstat (limited to 'llvm/tools/sancov/sancov.cc')
| -rw-r--r-- | llvm/tools/sancov/sancov.cc | 91 |
1 files changed, 50 insertions, 41 deletions
diff --git a/llvm/tools/sancov/sancov.cc b/llvm/tools/sancov/sancov.cc index 0baaae16cfe..0d2b48ca868 100644 --- a/llvm/tools/sancov/sancov.cc +++ b/llvm/tools/sancov/sancov.cc @@ -1,4 +1,4 @@ -//===-- sancov.cc --------------------------------------------===// +//===-- sancov.cc ---------------------------------------------------------===// // // The LLVM Compiler Infrastructure // @@ -10,7 +10,13 @@ // This file is a command-line tool for reading and analyzing sanitizer // coverage. //===----------------------------------------------------------------------===// + +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Triple.h" #include "llvm/ADT/Twine.h" #include "llvm/DebugInfo/Symbolize/Symbolize.h" #include "llvm/MC/MCAsmInfo.h" @@ -47,9 +53,13 @@ #include "llvm/Support/raw_ostream.h" #include <algorithm> +#include <cstdint> +#include <cstdlib> +#include <map> +#include <memory> #include <set> -#include <stdio.h> #include <string> +#include <tuple> #include <utility> #include <vector> @@ -83,28 +93,28 @@ cl::opt<ActionType> Action( "Print coverage statistics."), clEnumValEnd)); -static cl::list<std::string> +cl::list<std::string> ClInputFiles(cl::Positional, cl::OneOrMore, cl::desc("(<binary file>|<.sancov file>)...")); -static cl::opt<bool> ClDemangle("demangle", cl::init(true), - cl::desc("Print demangled function name.")); +cl::opt<bool> ClDemangle("demangle", cl::init(true), + cl::desc("Print demangled function name.")); -static cl::opt<std::string> ClStripPathPrefix( +cl::opt<std::string> ClStripPathPrefix( "strip_path_prefix", cl::init(""), cl::desc("Strip this prefix from file paths in reports.")); -static cl::opt<std::string> +cl::opt<std::string> ClBlacklist("blacklist", cl::init(""), cl::desc("Blacklist file (sanitizer blacklist format).")); -static cl::opt<bool> ClUseDefaultBlacklist( +cl::opt<bool> ClUseDefaultBlacklist( "use_default_blacklist", cl::init(true), cl::Hidden, cl::desc("Controls if default blacklist should be used.")); -static const char *const DefaultBlacklistStr = "fun:__sanitizer_.*\n" - "src:/usr/include/.*\n" - "src:.*/libc\\+\\+/.*\n"; +const char *const DefaultBlacklistStr = "fun:__sanitizer_.*\n" + "src:/usr/include/.*\n" + "src:.*/libc\\+\\+/.*\n"; // --------- FORMAT SPECIFICATION --------- @@ -113,37 +123,37 @@ struct FileHeader { uint32_t Magic; }; -static const uint32_t BinCoverageMagic = 0xC0BFFFFF; -static const uint32_t Bitness32 = 0xFFFFFF32; -static const uint32_t Bitness64 = 0xFFFFFF64; +const uint32_t BinCoverageMagic = 0xC0BFFFFF; +const uint32_t Bitness32 = 0xFFFFFF32; +const uint32_t Bitness64 = 0xFFFFFF64; // --------- ERROR HANDLING --------- -static void Fail(const llvm::Twine &E) { +void Fail(const llvm::Twine &E) { errs() << "Error: " << E << "\n"; exit(1); } -static void FailIfError(std::error_code Error) { +void FailIfError(std::error_code Error) { if (!Error) return; errs() << "Error: " << Error.message() << "(" << Error.value() << ")\n"; exit(1); } -template <typename T> static void FailIfError(const ErrorOr<T> &E) { +template <typename T> +void FailIfError(const ErrorOr<T> &E) { FailIfError(E.getError()); } -static void FailIfNotEmpty(const llvm::Twine &E) { +void FailIfNotEmpty(const llvm::Twine &E) { if (E.str().empty()) return; Fail(E); } template <typename T> -static void FailIfEmpty(const std::unique_ptr<T> &Ptr, - const std::string &Message) { +void FailIfEmpty(const std::unique_ptr<T> &Ptr, const std::string &Message) { if (Ptr.get()) return; Fail(Message); @@ -154,7 +164,7 @@ static void FailIfEmpty(const std::unique_ptr<T> &Ptr, // Produces std::map<K, std::vector<E>> grouping input // elements by FuncTy result. template <class RangeTy, class FuncTy> -static inline auto group_by(const RangeTy &R, FuncTy F) +inline auto group_by(const RangeTy &R, FuncTy F) -> std::map<typename std::decay<decltype(F(*R.begin()))>::type, std::vector<typename std::decay<decltype(*R.begin())>::type>> { std::map<typename std::decay<decltype(F(*R.begin()))>::type, @@ -167,8 +177,7 @@ static inline auto group_by(const RangeTy &R, FuncTy F) } template <typename T> -static void readInts(const char *Start, const char *End, - std::set<uint64_t> *Ints) { +void readInts(const char *Start, const char *End, std::set<uint64_t> *Ints) { const T *S = reinterpret_cast<const T *>(Start); const T *E = reinterpret_cast<const T *>(End); std::copy(S, E, std::inserter(*Ints, Ints->end())); @@ -211,7 +220,7 @@ std::string stripPathPrefix(std::string Path) { return Path.substr(Pos + ClStripPathPrefix.size()); } -static std::unique_ptr<symbolize::LLVMSymbolizer> createSymbolizer() { +std::unique_ptr<symbolize::LLVMSymbolizer> createSymbolizer() { symbolize::LLVMSymbolizer::Options SymbolizerOptions; SymbolizerOptions.Demangle = ClDemangle; SymbolizerOptions.UseSymbolTable = true; @@ -276,9 +285,9 @@ private: }; // Collect all debug info for given addresses. -static std::vector<AddrInfo> getAddrInfo(std::string ObjectFile, - const std::set<uint64_t> &Addrs, - bool InlinedCode) { +std::vector<AddrInfo> getAddrInfo(std::string ObjectFile, + const std::set<uint64_t> &Addrs, + bool InlinedCode) { std::vector<AddrInfo> Result; auto Symbolizer(createSymbolizer()); Blacklists B; @@ -306,7 +315,7 @@ static std::vector<AddrInfo> getAddrInfo(std::string ObjectFile, // Locate __sanitizer_cov* function addresses that are used for coverage // reporting. -static std::set<uint64_t> +std::set<uint64_t> findSanitizerCovFunctions(const object::ObjectFile &O) { std::set<uint64_t> Result; @@ -331,8 +340,8 @@ findSanitizerCovFunctions(const object::ObjectFile &O) { // Locate addresses of all coverage points in a file. Coverage point // is defined as the 'address of instruction following __sanitizer_cov // call - 1'. -static void getObjectCoveragePoints(const object::ObjectFile &O, - std::set<uint64_t> *Addrs) { +void getObjectCoveragePoints(const object::ObjectFile &O, + std::set<uint64_t> *Addrs) { Triple TheTriple("unknown-unknown-unknown"); TheTriple.setArch(Triple::ArchType(O.getArch())); auto TripleName = TheTriple.getTriple(); @@ -404,7 +413,7 @@ static void getObjectCoveragePoints(const object::ObjectFile &O, } } -static void +void visitObjectFiles(const object::Archive &A, std::function<void(const object::ObjectFile &)> Fn) { for (auto &ErrorOrChild : A.children()) { @@ -419,7 +428,7 @@ visitObjectFiles(const object::Archive &A, } } -static void +void visitObjectFiles(std::string FileName, std::function<void(const object::ObjectFile &)> Fn) { ErrorOr<object::OwningBinary<object::Binary>> BinaryOrErr = @@ -455,7 +464,7 @@ std::set<uint64_t> getCoveragePoints(std::string FileName) { return Result; } -static void printCovPoints(std::string ObjFile, raw_ostream &OS) { +void printCovPoints(std::string ObjFile, raw_ostream &OS) { for (uint64_t Addr : getCoveragePoints(ObjFile)) { OS << "0x"; OS.write_hex(Addr); @@ -463,7 +472,7 @@ static void printCovPoints(std::string ObjFile, raw_ostream &OS) { } } -static std::string escapeHtml(const std::string &S) { +std::string escapeHtml(const std::string &S) { std::string Result; Result.reserve(S.size()); for (char Ch : S) { @@ -493,7 +502,7 @@ static std::string escapeHtml(const std::string &S) { // Adds leading zeroes wrapped in 'lz' style. // Leading zeroes help locate 000% coverage. -static std::string formatHtmlPct(size_t Pct) { +std::string formatHtmlPct(size_t Pct) { Pct = std::max(std::size_t{0}, std::min(std::size_t{100}, Pct)); std::string Num = std::to_string(Pct); @@ -504,7 +513,7 @@ static std::string formatHtmlPct(size_t Pct) { return Zeroes + Num; } -static std::string anchorName(std::string Anchor) { +std::string anchorName(std::string Anchor) { llvm::MD5 Hasher; llvm::MD5::MD5Result Hash; Hasher.update(Anchor); @@ -515,7 +524,7 @@ static std::string anchorName(std::string Anchor) { return HexString.str().str(); } -static ErrorOr<bool> isCoverageFile(std::string FileName) { +ErrorOr<bool> isCoverageFile(std::string FileName) { ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = MemoryBuffer::getFile(FileName); if (!BufOrErr) { @@ -542,7 +551,7 @@ struct CoverageStats { size_t CovFns; }; -static raw_ostream &operator<<(raw_ostream &OS, const CoverageStats &Stats) { +raw_ostream &operator<<(raw_ostream &OS, const CoverageStats &Stats) { OS << "all-edges: " << Stats.AllPoints << "\n"; OS << "cov-edges: " << Stats.CovPoints << "\n"; OS << "all-functions: " << Stats.AllFns << "\n"; @@ -822,8 +831,8 @@ private: std::vector<AddrInfo> CovAddrInfo; }; -static void printFunctionLocs(const SourceCoverageData::FunctionLocs &FnLocs, - raw_ostream &OS) { +void printFunctionLocs(const SourceCoverageData::FunctionLocs &FnLocs, + raw_ostream &OS) { for (const auto &Fns : FnLocs) { for (const auto &Fn : Fns.second) { OS << stripPathPrefix(Fns.first.FileName) << ":" << Fns.first.Line << " " @@ -1182,7 +1191,7 @@ private: const std::set<std::string> CoverageFiles; }; -} // namespace +} // end anonymous namespace int main(int argc, char **argv) { // Print stack trace if we signal out. |

